]> 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-2005, 2006, 2007, 2008 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.2 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
111 @settitle @value{EDITION} User's Guide @value{PLATFORM}
112 @dircategory GNU Ada tools
113 @direntry
114 * @value{EDITION} User's Guide: (gnat_ugn). @value{PLATFORM}
115 @end direntry
116
117 @include gcc-common.texi
118
119 @setchapternewpage odd
120 @syncodeindex fn cp
121 @c %**end of header
122
123 @titlepage
124 @title @value{EDITION} User's Guide
125 @ifset vms
126 @sp 1
127 @flushright
128 @titlefont{@i{@value{PLATFORM}}}
129 @end flushright
130 @end ifset
131
132 @sp 2
133
134 @subtitle GNAT, The GNU Ada Compiler
135 @versionsubtitle
136 @author AdaCore
137
138 @page
139 @vskip 0pt plus 1filll
140
141 @insertcopying
142
143 @end titlepage
144
145 @ifnottex
146 @node Top, About This Guide, (dir), (dir)
147 @top @value{EDITION} User's Guide
148
149 @noindent
150 @value{EDITION} User's Guide @value{PLATFORM}
151
152 @noindent
153 GNAT, The GNU Ada Compiler@*
154 GCC version @value{version-GCC}@*
155
156 @noindent
157 AdaCore@*
158
159 @menu
160 * About This Guide::
161 * Getting Started with GNAT::
162 * The GNAT Compilation Model::
163 * Compiling Using gcc::
164 * Binding Using gnatbind::
165 * Linking Using gnatlink::
166 * The GNAT Make Program gnatmake::
167 * Improving Performance::
168 * Renaming Files Using gnatchop::
169 * Configuration Pragmas::
170 * Handling Arbitrary File Naming Conventions Using gnatname::
171 * GNAT Project Manager::
172 * The Cross-Referencing Tools gnatxref and gnatfind::
173 * The GNAT Pretty-Printer gnatpp::
174 * The GNAT Metric Tool gnatmetric::
175 * File Name Krunching Using gnatkr::
176 * Preprocessing Using gnatprep::
177 @ifset vms
178 * The GNAT Run-Time Library Builder gnatlbr::
179 @end ifset
180 * The GNAT Library Browser gnatls::
181 * Cleaning Up Using gnatclean::
182 @ifclear vms
183 * GNAT and Libraries::
184 * Using the GNU make Utility::
185 @end ifclear
186 * Memory Management Issues::
187 * Stack Related Facilities::
188 * Verifying Properties Using gnatcheck::
189 * Creating Sample Bodies Using gnatstub::
190 * Generating Ada Bindings for C and C++ headers::
191 * Other Utility Programs::
192 * Running and Debugging Ada Programs::
193 @ifclear vms
194 * Code Coverage and Profiling::
195 @end ifclear
196 @ifset vms
197 * Compatibility with HP Ada::
198 @end ifset
199 * Platform-Specific Information for the Run-Time Libraries::
200 * Example of Binder Output File::
201 * Elaboration Order Handling in GNAT::
202 * Conditional Compilation::
203 * Inline Assembler::
204 * Compatibility and Porting Guide::
205 @ifset unw
206 * Microsoft Windows Topics::
207 @end ifset
208 * GNU Free Documentation License::
209 * Index::
210
211 --- The Detailed Node Listing ---
212
213 About This Guide
214
215 * What This Guide Contains::
216 * What You Should Know before Reading This Guide::
217 * Related Information::
218 * Conventions::
219
220 Getting Started with GNAT
221
222 * Running GNAT::
223 * Running a Simple Ada Program::
224 * Running a Program with Multiple Units::
225 * Using the gnatmake Utility::
226 @ifset vms
227 * Editing with Emacs::
228 @end ifset
229 @ifclear vms
230 * Introduction to GPS::
231 @end ifclear
232
233 The GNAT Compilation Model
234
235 * Source Representation::
236 * Foreign Language Representation::
237 * File Naming Rules::
238 * Using Other File Names::
239 * Alternative File Naming Schemes::
240 * Generating Object Files::
241 * Source Dependencies::
242 * The Ada Library Information Files::
243 * Binding an Ada Program::
244 * Mixed Language Programming::
245 @ifclear vms
246 * Building Mixed Ada & C++ Programs::
247 * Comparison between GNAT and C/C++ Compilation Models::
248 @end ifclear
249 * Comparison between GNAT and Conventional Ada Library Models::
250 @ifset vms
251 * Placement of temporary files::
252 @end ifset
253
254 Foreign Language Representation
255
256 * Latin-1::
257 * Other 8-Bit Codes::
258 * Wide Character Encodings::
259
260 Compiling Ada Programs With gcc
261
262 * Compiling Programs::
263 * Switches for gcc::
264 * Search Paths and the Run-Time Library (RTL)::
265 * Order of Compilation Issues::
266 * Examples::
267
268 Switches for gcc
269
270 * Output and Error Message Control::
271 * Warning Message Control::
272 * Debugging and Assertion Control::
273 * Validity Checking::
274 * Style Checking::
275 * Run-Time Checks::
276 * Using gcc for Syntax Checking::
277 * Using gcc for Semantic Checking::
278 * Compiling Different Versions of Ada::
279 * Character Set Control::
280 * File Naming Control::
281 * Subprogram Inlining Control::
282 * Auxiliary Output Control::
283 * Debugging Control::
284 * Exception Handling Control::
285 * Units to Sources Mapping Files::
286 * Integrated Preprocessing::
287 @ifset vms
288 * Return Codes::
289 @end ifset
290
291 Binding Ada Programs With gnatbind
292
293 * Running gnatbind::
294 * Switches for gnatbind::
295 * Command-Line Access::
296 * Search Paths for gnatbind::
297 * Examples of gnatbind Usage::
298
299 Switches for gnatbind
300
301 * Consistency-Checking Modes::
302 * Binder Error Message Control::
303 * Elaboration Control::
304 * Output Control::
305 * Binding with Non-Ada Main Programs::
306 * Binding Programs with No Main Subprogram::
307
308 Linking Using gnatlink
309
310 * Running gnatlink::
311 * Switches for gnatlink::
312
313 The GNAT Make Program gnatmake
314
315 * Running gnatmake::
316 * Switches for gnatmake::
317 * Mode Switches for gnatmake::
318 * Notes on the Command Line::
319 * How gnatmake Works::
320 * Examples of gnatmake Usage::
321
322 Improving Performance
323 * Performance Considerations::
324 * Text_IO Suggestions::
325 * Reducing Size of Ada Executables with gnatelim::
326 * Reducing Size of Executables with unused subprogram/data elimination::
327
328 Performance Considerations
329 * Controlling Run-Time Checks::
330 * Use of Restrictions::
331 * Optimization Levels::
332 * Debugging Optimized Code::
333 * Inlining of Subprograms::
334 * Other Optimization Switches::
335 * Optimization and Strict Aliasing::
336 @ifset vms
337 * Coverage Analysis::
338 @end ifset
339
340 Reducing Size of Ada Executables with gnatelim
341 * About gnatelim::
342 * Running gnatelim::
343 * Correcting the List of Eliminate Pragmas::
344 * Making Your Executables Smaller::
345 * Summary of the gnatelim Usage Cycle::
346
347 Reducing Size of Executables with unused subprogram/data elimination
348 * About unused subprogram/data elimination::
349 * Compilation options::
350
351 Renaming Files Using gnatchop
352
353 * Handling Files with Multiple Units::
354 * Operating gnatchop in Compilation Mode::
355 * Command Line for gnatchop::
356 * Switches for gnatchop::
357 * Examples of gnatchop Usage::
358
359 Configuration Pragmas
360
361 * Handling of Configuration Pragmas::
362 * The Configuration Pragmas Files::
363
364 Handling Arbitrary File Naming Conventions Using gnatname
365
366 * Arbitrary File Naming Conventions::
367 * Running gnatname::
368 * Switches for gnatname::
369 * Examples of gnatname Usage::
370
371 GNAT Project Manager
372
373 * Introduction::
374 * Examples of Project Files::
375 * Project File Syntax::
376 * Objects and Sources in Project Files::
377 * Importing Projects::
378 * Project Extension::
379 * Project Hierarchy Extension::
380 * External References in Project Files::
381 * Packages in Project Files::
382 * Variables from Imported Projects::
383 * Naming Schemes::
384 * Library Projects::
385 * Stand-alone Library Projects::
386 * Switches Related to Project Files::
387 * Tools Supporting Project Files::
388 * An Extended Example::
389 * Project File Complete Syntax::
390
391 The Cross-Referencing Tools gnatxref and gnatfind
392
393 * gnatxref Switches::
394 * gnatfind Switches::
395 * Project Files for gnatxref and gnatfind::
396 * Regular Expressions in gnatfind and gnatxref::
397 * Examples of gnatxref Usage::
398 * Examples of gnatfind Usage::
399
400 The GNAT Pretty-Printer gnatpp
401
402 * Switches for gnatpp::
403 * Formatting Rules::
404
405 The GNAT Metrics Tool gnatmetric
406
407 * Switches for gnatmetric::
408
409 File Name Krunching Using gnatkr
410
411 * About gnatkr::
412 * Using gnatkr::
413 * Krunching Method::
414 * Examples of gnatkr Usage::
415
416 Preprocessing Using gnatprep
417 * Preprocessing Symbols::
418 * Using gnatprep::
419 * Switches for gnatprep::
420 * Form of Definitions File::
421 * Form of Input Text for gnatprep::
422
423 @ifset vms
424 The GNAT Run-Time Library Builder gnatlbr
425
426 * Running gnatlbr::
427 * Switches for gnatlbr::
428 * Examples of gnatlbr Usage::
429 @end ifset
430
431 The GNAT Library Browser gnatls
432
433 * Running gnatls::
434 * Switches for gnatls::
435 * Examples of gnatls Usage::
436
437 Cleaning Up Using gnatclean
438
439 * Running gnatclean::
440 * Switches for gnatclean::
441 @c * Examples of gnatclean Usage::
442
443 @ifclear vms
444
445 GNAT and Libraries
446
447 * Introduction to Libraries in GNAT::
448 * General Ada Libraries::
449 * Stand-alone Ada Libraries::
450 * Rebuilding the GNAT Run-Time Library::
451
452 Using the GNU make Utility
453
454 * Using gnatmake in a Makefile::
455 * Automatically Creating a List of Directories::
456 * Generating the Command Line Switches::
457 * Overcoming Command Line Length Limits::
458 @end ifclear
459
460 Memory Management Issues
461
462 * Some Useful Memory Pools::
463 * The GNAT Debug Pool Facility::
464 @ifclear vms
465 * The gnatmem Tool::
466 @end ifclear
467
468 Stack Related Facilities
469
470 * Stack Overflow Checking::
471 * Static Stack Usage Analysis::
472 * Dynamic Stack Usage Analysis::
473
474 Some Useful Memory Pools
475
476 The GNAT Debug Pool Facility
477
478 @ifclear vms
479 The gnatmem Tool
480
481 * Running gnatmem::
482 * Switches for gnatmem::
483 * Example of gnatmem Usage::
484 @end ifclear
485
486 Verifying Properties Using gnatcheck
487
488 * Format of the Report File::
489 * General gnatcheck Switches::
490 * gnatcheck Rule Options::
491 * Adding the Results of Compiler Checks to gnatcheck Output::
492 * Project-Wide Checks::
493 * Predefined Rules::
494
495 Sample Bodies Using gnatstub
496
497 * Running gnatstub::
498 * Switches for gnatstub::
499
500 Other Utility Programs
501
502 * Using Other Utility Programs with GNAT::
503 * The External Symbol Naming Scheme of GNAT::
504 * Converting Ada Files to html with gnathtml::
505
506 @ifclear vms
507 Code Coverage and Profiling
508
509 * Code Coverage of Ada Programs using gcov::
510 * Profiling an Ada Program using gprof::
511 @end ifclear
512
513 Running and Debugging Ada Programs
514
515 * The GNAT Debugger GDB::
516 * Running GDB::
517 * Introduction to GDB Commands::
518 * Using Ada Expressions::
519 * Calling User-Defined Subprograms::
520 * Using the Next Command in a Function::
521 * Ada Exceptions::
522 * Ada Tasks::
523 * Debugging Generic Units::
524 * GNAT Abnormal Termination or Failure to Terminate::
525 * Naming Conventions for GNAT Source Files::
526 * Getting Internal Debugging Information::
527 * Stack Traceback::
528
529 @ifset vms
530 * LSE::
531 @end ifset
532
533 @ifset vms
534 Compatibility with HP Ada
535
536 * Ada Language Compatibility::
537 * Differences in the Definition of Package System::
538 * Language-Related Features::
539 * The Package STANDARD::
540 * The Package SYSTEM::
541 * Tasking and Task-Related Features::
542 * Pragmas and Pragma-Related Features::
543 * Library of Predefined Units::
544 * Bindings::
545 * Main Program Definition::
546 * Implementation-Defined Attributes::
547 * Compiler and Run-Time Interfacing::
548 * Program Compilation and Library Management::
549 * Input-Output::
550 * Implementation Limits::
551 * Tools and Utilities::
552
553 Language-Related Features
554
555 * Integer Types and Representations::
556 * Floating-Point Types and Representations::
557 * Pragmas Float_Representation and Long_Float::
558 * Fixed-Point Types and Representations::
559 * Record and Array Component Alignment::
560 * Address Clauses::
561 * Other Representation Clauses::
562
563 Tasking and Task-Related Features
564
565 * Implementation of Tasks in HP Ada for OpenVMS Alpha Systems::
566 * Assigning Task IDs::
567 * Task IDs and Delays::
568 * Task-Related Pragmas::
569 * Scheduling and Task Priority::
570 * The Task Stack::
571 * External Interrupts::
572
573 Pragmas and Pragma-Related Features
574
575 * Restrictions on the Pragma INLINE::
576 * Restrictions on the Pragma INTERFACE::
577 * Restrictions on the Pragma SYSTEM_NAME::
578
579 Library of Predefined Units
580
581 * Changes to DECLIB::
582
583 Bindings
584
585 * Shared Libraries and Options Files::
586 * Interfaces to C::
587 @end ifset
588
589 Platform-Specific Information for the Run-Time Libraries
590
591 * Summary of Run-Time Configurations::
592 * Specifying a Run-Time Library::
593 * Choosing the Scheduling Policy::
594 * Solaris-Specific Considerations::
595 * Linux-Specific Considerations::
596 * AIX-Specific Considerations::
597 * Irix-Specific Considerations::
598
599 Example of Binder Output File
600
601 Elaboration Order Handling in GNAT
602
603 * Elaboration Code::
604 * Checking the Elaboration Order::
605 * Controlling the Elaboration Order::
606 * Controlling Elaboration in GNAT - Internal Calls::
607 * Controlling Elaboration in GNAT - External Calls::
608 * Default Behavior in GNAT - Ensuring Safety::
609 * Treatment of Pragma Elaborate::
610 * Elaboration Issues for Library Tasks::
611 * Mixing Elaboration Models::
612 * What to Do If the Default Elaboration Behavior Fails::
613 * Elaboration for Access-to-Subprogram Values::
614 * Summary of Procedures for Elaboration Control::
615 * Other Elaboration Order Considerations::
616
617 Conditional Compilation
618 * Use of Boolean Constants::
619 * Debugging - A Special Case::
620 * Conditionalizing Declarations::
621 * Use of Alternative Implementations::
622 * Preprocessing::
623
624 Inline Assembler
625
626 * Basic Assembler Syntax::
627 * A Simple Example of Inline Assembler::
628 * Output Variables in Inline Assembler::
629 * Input Variables in Inline Assembler::
630 * Inlining Inline Assembler Code::
631 * Other Asm Functionality::
632
633 Compatibility and Porting Guide
634
635 * Compatibility with Ada 83::
636 * Compatibility between Ada 95 and Ada 2005::
637 * Implementation-dependent characteristics::
638 @ifclear vms
639 @c This brief section is only in the non-VMS version
640 @c The complete chapter on HP Ada issues is in the VMS version
641 * Compatibility with HP Ada 83::
642 @end ifclear
643 * Compatibility with Other Ada Systems::
644 * Representation Clauses::
645 @ifset vms
646 * Transitioning to 64-Bit GNAT for OpenVMS::
647 @end ifset
648
649 @ifset unw
650 Microsoft Windows Topics
651
652 * Using GNAT on Windows::
653 * CONSOLE and WINDOWS subsystems::
654 * Temporary Files::
655 * Mixed-Language Programming on Windows::
656 * Windows Calling Conventions::
657 * Introduction to Dynamic Link Libraries (DLLs)::
658 * Using DLLs with GNAT::
659 * Building DLLs with GNAT::
660 * GNAT and Windows Resources::
661 * Debugging a DLL::
662 * Setting Stack Size from gnatlink::
663 * Setting Heap Size from gnatlink::
664 @end ifset
665
666 * Index::
667 @end menu
668 @end ifnottex
669
670 @node About This Guide
671 @unnumbered About This Guide
672
673 @noindent
674 @ifset vms
675 This guide describes the use of @value{EDITION},
676 a compiler and software development toolset for the full Ada
677 programming language, implemented on OpenVMS for HP's Alpha and
678 Integrity server (I64) platforms.
679 @end ifset
680 @ifclear vms
681 This guide describes the use of @value{EDITION},
682 a compiler and software development
683 toolset for the full Ada programming language.
684 @end ifclear
685 It documents the features of the compiler and tools, and explains
686 how to use them to build Ada applications.
687
688 @value{EDITION} implements Ada 95 and Ada 2005, and it may also be invoked in
689 Ada 83 compatibility mode.
690 By default, @value{EDITION} assumes @value{DEFAULTLANGUAGEVERSION},
691 but you can override with a compiler switch
692 (@pxref{Compiling Different Versions of Ada})
693 to explicitly specify the language version.
694 Throughout this manual, references to ``Ada'' without a year suffix
695 apply to both the Ada 95 and Ada 2005 versions of the language.
696
697
698 @ifclear FSFEDITION
699 For ease of exposition, ``@value{EDITION}'' will be referred to simply as
700 ``GNAT'' in the remainder of this document.
701 @end ifclear
702
703
704
705
706 @menu
707 * What This Guide Contains::
708 * What You Should Know before Reading This Guide::
709 * Related Information::
710 * Conventions::
711 @end menu
712
713 @node What This Guide Contains
714 @unnumberedsec What This Guide Contains
715
716 @noindent
717 This guide contains the following chapters:
718 @itemize @bullet
719
720 @item
721 @ref{Getting Started with GNAT}, describes how to get started compiling
722 and running Ada programs with the GNAT Ada programming environment.
723 @item
724 @ref{The GNAT Compilation Model}, describes the compilation model used
725 by GNAT.
726
727 @item
728 @ref{Compiling Using gcc}, describes how to compile
729 Ada programs with @command{gcc}, the Ada compiler.
730
731 @item
732 @ref{Binding Using gnatbind}, describes how to
733 perform binding of Ada programs with @code{gnatbind}, the GNAT binding
734 utility.
735
736 @item
737 @ref{Linking Using gnatlink},
738 describes @command{gnatlink}, a
739 program that provides for linking using the GNAT run-time library to
740 construct a program. @command{gnatlink} can also incorporate foreign language
741 object units into the executable.
742
743 @item
744 @ref{The GNAT Make Program gnatmake}, describes @command{gnatmake}, a
745 utility that automatically determines the set of sources
746 needed by an Ada compilation unit, and executes the necessary compilations
747 binding and link.
748
749 @item
750 @ref{Improving Performance}, shows various techniques for making your
751 Ada program run faster or take less space.
752 It discusses the effect of the compiler's optimization switch and
753 also describes the @command{gnatelim} tool and unused subprogram/data
754 elimination.
755
756 @item
757 @ref{Renaming Files Using gnatchop}, describes
758 @code{gnatchop}, a utility that allows you to preprocess a file that
759 contains Ada source code, and split it into one or more new files, one
760 for each compilation unit.
761
762 @item
763 @ref{Configuration Pragmas}, describes the configuration pragmas
764 handled by GNAT.
765
766 @item
767 @ref{Handling Arbitrary File Naming Conventions Using gnatname},
768 shows how to override the default GNAT file naming conventions,
769 either for an individual unit or globally.
770
771 @item
772 @ref{GNAT Project Manager}, describes how to use project files
773 to organize large projects.
774
775 @item
776 @ref{The Cross-Referencing Tools gnatxref and gnatfind}, discusses
777 @code{gnatxref} and @code{gnatfind}, two tools that provide an easy
778 way to navigate through sources.
779
780 @item
781 @ref{The GNAT Pretty-Printer gnatpp}, shows how to produce a reformatted
782 version of an Ada source file with control over casing, indentation,
783 comment placement, and other elements of program presentation style.
784
785 @item
786 @ref{The GNAT Metric Tool gnatmetric}, shows how to compute various
787 metrics for an Ada source file, such as the number of types and subprograms,
788 and assorted complexity measures.
789
790 @item
791 @ref{File Name Krunching Using gnatkr}, describes the @code{gnatkr}
792 file name krunching utility, used to handle shortened
793 file names on operating systems with a limit on the length of names.
794
795 @item
796 @ref{Preprocessing Using gnatprep}, describes @code{gnatprep}, a
797 preprocessor utility that allows a single source file to be used to
798 generate multiple or parameterized source files by means of macro
799 substitution.
800
801 @ifset vms
802 @item
803 @ref{The GNAT Run-Time Library Builder gnatlbr}, describes @command{gnatlbr},
804 a tool for rebuilding the GNAT run time with user-supplied
805 configuration pragmas.
806 @end ifset
807
808 @item
809 @ref{The GNAT Library Browser gnatls}, describes @code{gnatls}, a
810 utility that displays information about compiled units, including dependences
811 on the corresponding sources files, and consistency of compilations.
812
813 @item
814 @ref{Cleaning Up Using gnatclean}, describes @code{gnatclean}, a utility
815 to delete files that are produced by the compiler, binder and linker.
816
817 @ifclear vms
818 @item
819 @ref{GNAT and Libraries}, describes the process of creating and using
820 Libraries with GNAT. It also describes how to recompile the GNAT run-time
821 library.
822
823 @item
824 @ref{Using the GNU make Utility}, describes some techniques for using
825 the GNAT toolset in Makefiles.
826 @end ifclear
827
828 @item
829 @ref{Memory Management Issues}, describes some useful predefined storage pools
830 and in particular the GNAT Debug Pool facility, which helps detect incorrect
831 memory references.
832 @ifclear vms
833 It also describes @command{gnatmem}, a utility that monitors dynamic
834 allocation and deallocation and helps detect ``memory leaks''.
835 @end ifclear
836
837 @item
838 @ref{Stack Related Facilities}, describes some useful tools associated with
839 stack checking and analysis.
840
841 @item
842 @ref{Verifying Properties Using gnatcheck}, discusses @code{gnatcheck},
843 a utility that checks Ada code against a set of rules.
844
845 @item
846 @ref{Creating Sample Bodies Using gnatstub}, discusses @code{gnatstub},
847 a utility that generates empty but compilable bodies for library units.
848
849 @item
850 @ref{Generating Ada Bindings for C and C++ headers}, describes how to
851 generate automatically Ada bindings from C and C++ headers.
852
853 @item
854 @ref{Other Utility Programs}, discusses several other GNAT utilities,
855 including @code{gnathtml}.
856
857 @ifclear vms
858 @item
859 @ref{Code Coverage and Profiling}, describes how to perform a structural
860 coverage and profile the execution of Ada programs.
861 @end ifclear
862
863 @item
864 @ref{Running and Debugging Ada Programs}, describes how to run and debug
865 Ada programs.
866
867 @ifset vms
868 @item
869 @ref{Compatibility with HP Ada}, details the compatibility of GNAT with
870 HP Ada 83 @footnote{``HP Ada'' refers to the legacy product originally
871 developed by Digital Equipment Corporation and currently supported by HP.}
872 for OpenVMS Alpha. This product was formerly known as DEC Ada,
873 @cindex DEC Ada
874 and for
875 historical compatibility reasons, the relevant libraries still use the
876 DEC prefix.
877 @end ifset
878
879 @item
880 @ref{Platform-Specific Information for the Run-Time Libraries},
881 describes the various run-time
882 libraries supported by GNAT on various platforms and explains how to
883 choose a particular library.
884
885 @item
886 @ref{Example of Binder Output File}, shows the source code for the binder
887 output file for a sample program.
888
889 @item
890 @ref{Elaboration Order Handling in GNAT}, describes how GNAT helps
891 you deal with elaboration order issues.
892
893 @item
894 @ref{Conditional Compilation}, describes how to model conditional compilation,
895 both with Ada in general and with GNAT facilities in particular.
896
897 @item
898 @ref{Inline Assembler}, shows how to use the inline assembly facility
899 in an Ada program.
900
901 @item
902 @ref{Compatibility and Porting Guide}, contains sections on compatibility
903 of GNAT with other Ada development environments (including Ada 83 systems),
904 to assist in porting code from those environments.
905
906 @ifset unw
907 @item
908 @ref{Microsoft Windows Topics}, presents information relevant to the
909 Microsoft Windows platform.
910 @end ifset
911 @end itemize
912
913 @c *************************************************
914 @node What You Should Know before Reading This Guide
915 @c *************************************************
916 @unnumberedsec What You Should Know before Reading This Guide
917
918 @cindex Ada 95 Language Reference Manual
919 @cindex Ada 2005 Language Reference Manual
920 @noindent
921 This guide assumes a basic familiarity with the Ada 95 language, as
922 described in the International Standard ANSI/ISO/IEC-8652:1995, January
923 1995.
924 It does not require knowledge of the new features introduced by Ada 2005,
925 (officially known as ISO/IEC 8652:1995 with Technical Corrigendum 1
926 and Amendment 1).
927 Both reference manuals are included in the GNAT documentation
928 package.
929
930 @node Related Information
931 @unnumberedsec Related Information
932
933 @noindent
934 For further information about related tools, refer to the following
935 documents:
936
937 @itemize @bullet
938 @item
939 @xref{Top, GNAT Reference Manual, About This Guide, gnat_rm, GNAT
940 Reference Manual}, which contains all reference material for the GNAT
941 implementation of Ada.
942
943 @ifset unw
944 @item
945 @cite{Using the GNAT Programming Studio}, which describes the GPS
946 Integrated Development Environment.
947
948 @item
949 @cite{GNAT Programming Studio Tutorial}, which introduces the
950 main GPS features through examples.
951 @end ifset
952
953 @item
954 @cite{Ada 95 Reference Manual}, which contains reference
955 material for the Ada 95 programming language.
956
957 @item
958 @cite{Ada 2005 Reference Manual}, which contains reference
959 material for the Ada 2005 programming language.
960
961 @item
962 @xref{Top,, Debugging with GDB, gdb, Debugging with GDB},
963 @ifset vms
964 in the GNU:[DOCS] directory,
965 @end ifset
966 for all details on the use of the GNU source-level debugger.
967
968 @item
969 @xref{Top,, The extensible self-documenting text editor, emacs,
970 GNU Emacs Manual},
971 @ifset vms
972 located in the GNU:[DOCS] directory if the EMACS kit is installed,
973 @end ifset
974 for full information on the extensible editor and programming
975 environment Emacs.
976
977 @end itemize
978
979 @c **************
980 @node Conventions
981 @unnumberedsec Conventions
982 @cindex Conventions
983 @cindex Typographical conventions
984
985 @noindent
986 Following are examples of the typographical and graphic conventions used
987 in this guide:
988
989 @itemize @bullet
990 @item
991 @code{Functions}, @command{utility program names}, @code{standard names},
992 and @code{classes}.
993
994 @item
995 @option{Option flags}
996
997 @item
998 @file{File names}, @samp{button names}, and @samp{field names}.
999
1000 @item
1001 @code{Variables}, @env{environment variables}, and @var{metasyntactic
1002 variables}.
1003
1004 @item
1005 @emph{Emphasis}.
1006
1007 @item
1008 @r{[}optional information or parameters@r{]}
1009
1010 @item
1011 Examples are described by text
1012 @smallexample
1013 and then shown this way.
1014 @end smallexample
1015 @end itemize
1016
1017 @noindent
1018 Commands that are entered by the user are preceded in this manual by the
1019 characters @w{``@code{$ }''} (dollar sign followed by space). If your system
1020 uses this sequence as a prompt, then the commands will appear exactly as
1021 you see them in the manual. If your system uses some other prompt, then
1022 the command will appear with the @code{$} replaced by whatever prompt
1023 character you are using.
1024
1025 @ifset unw
1026 Full file names are shown with the ``@code{/}'' character
1027 as the directory separator; e.g., @file{parent-dir/subdir/myfile.adb}.
1028 If you are using GNAT on a Windows platform, please note that
1029 the ``@code{\}'' character should be used instead.
1030 @end ifset
1031
1032 @c ****************************
1033 @node Getting Started with GNAT
1034 @chapter Getting Started with GNAT
1035
1036 @noindent
1037 This chapter describes some simple ways of using GNAT to build
1038 executable Ada programs.
1039 @ifset unw
1040 @ref{Running GNAT}, through @ref{Using the gnatmake Utility},
1041 show how to use the command line environment.
1042 @ref{Introduction to GPS}, provides a brief
1043 introduction to the GNAT Programming Studio, a visually-oriented
1044 Integrated Development Environment for GNAT.
1045 GPS offers a graphical ``look and feel'', support for development in
1046 other programming languages, comprehensive browsing features, and
1047 many other capabilities.
1048 For information on GPS please refer to
1049 @cite{Using the GNAT Programming Studio}.
1050 @end ifset
1051
1052 @menu
1053 * Running GNAT::
1054 * Running a Simple Ada Program::
1055 * Running a Program with Multiple Units::
1056 * Using the gnatmake Utility::
1057 @ifset vms
1058 * Editing with Emacs::
1059 @end ifset
1060 @ifclear vms
1061 * Introduction to GPS::
1062 @end ifclear
1063 @end menu
1064
1065 @node Running GNAT
1066 @section Running GNAT
1067
1068 @noindent
1069 Three steps are needed to create an executable file from an Ada source
1070 file:
1071
1072 @enumerate
1073 @item
1074 The source file(s) must be compiled.
1075 @item
1076 The file(s) must be bound using the GNAT binder.
1077 @item
1078 All appropriate object files must be linked to produce an executable.
1079 @end enumerate
1080
1081 @noindent
1082 All three steps are most commonly handled by using the @command{gnatmake}
1083 utility program that, given the name of the main program, automatically
1084 performs the necessary compilation, binding and linking steps.
1085
1086 @node Running a Simple Ada Program
1087 @section Running a Simple Ada Program
1088
1089 @noindent
1090 Any text editor may be used to prepare an Ada program.
1091 (If @code{Emacs} is
1092 used, the optional Ada mode may be helpful in laying out the program.)
1093 The
1094 program text is a normal text file. We will assume in our initial
1095 example that you have used your editor to prepare the following
1096 standard format text file:
1097
1098 @smallexample @c ada
1099 @cartouche
1100 with Ada.Text_IO; use Ada.Text_IO;
1101 procedure Hello is
1102 begin
1103 Put_Line ("Hello WORLD!");
1104 end Hello;
1105 @end cartouche
1106 @end smallexample
1107
1108 @noindent
1109 This file should be named @file{hello.adb}.
1110 With the normal default file naming conventions, GNAT requires
1111 that each file
1112 contain a single compilation unit whose file name is the
1113 unit name,
1114 with periods replaced by hyphens; the
1115 extension is @file{ads} for a
1116 spec and @file{adb} for a body.
1117 You can override this default file naming convention by use of the
1118 special pragma @code{Source_File_Name} (@pxref{Using Other File Names}).
1119 Alternatively, if you want to rename your files according to this default
1120 convention, which is probably more convenient if you will be using GNAT
1121 for all your compilations, then the @code{gnatchop} utility
1122 can be used to generate correctly-named source files
1123 (@pxref{Renaming Files Using gnatchop}).
1124
1125 You can compile the program using the following command (@code{$} is used
1126 as the command prompt in the examples in this document):
1127
1128 @smallexample
1129 $ gcc -c hello.adb
1130 @end smallexample
1131
1132 @noindent
1133 @command{gcc} is the command used to run the compiler. This compiler is
1134 capable of compiling programs in several languages, including Ada and
1135 C. It assumes that you have given it an Ada program if the file extension is
1136 either @file{.ads} or @file{.adb}, and it will then call
1137 the GNAT compiler to compile the specified file.
1138
1139 @ifclear vms
1140 The @option{-c} switch is required. It tells @command{gcc} to only do a
1141 compilation. (For C programs, @command{gcc} can also do linking, but this
1142 capability is not used directly for Ada programs, so the @option{-c}
1143 switch must always be present.)
1144 @end ifclear
1145
1146 This compile command generates a file
1147 @file{hello.o}, which is the object
1148 file corresponding to your Ada program. It also generates
1149 an ``Ada Library Information'' file @file{hello.ali},
1150 which contains additional information used to check
1151 that an Ada program is consistent.
1152 To build an executable file,
1153 use @code{gnatbind} to bind the program
1154 and @command{gnatlink} to link it. The
1155 argument to both @code{gnatbind} and @command{gnatlink} is the name of the
1156 @file{ALI} file, but the default extension of @file{.ali} can
1157 be omitted. This means that in the most common case, the argument
1158 is simply the name of the main program:
1159
1160 @smallexample
1161 $ gnatbind hello
1162 $ gnatlink hello
1163 @end smallexample
1164
1165 @noindent
1166 A simpler method of carrying out these steps is to use
1167 @command{gnatmake},
1168 a master program that invokes all the required
1169 compilation, binding and linking tools in the correct order. In particular,
1170 @command{gnatmake} automatically recompiles any sources that have been
1171 modified since they were last compiled, or sources that depend
1172 on such modified sources, so that ``version skew'' is avoided.
1173 @cindex Version skew (avoided by @command{gnatmake})
1174
1175 @smallexample
1176 $ gnatmake hello.adb
1177 @end smallexample
1178
1179 @noindent
1180 The result is an executable program called @file{hello}, which can be
1181 run by entering:
1182
1183 @smallexample
1184 $ ^hello^RUN HELLO^
1185 @end smallexample
1186
1187 @noindent
1188 assuming that the current directory is on the search path
1189 for executable programs.
1190
1191 @noindent
1192 and, if all has gone well, you will see
1193
1194 @smallexample
1195 Hello WORLD!
1196 @end smallexample
1197
1198 @noindent
1199 appear in response to this command.
1200
1201 @c ****************************************
1202 @node Running a Program with Multiple Units
1203 @section Running a Program with Multiple Units
1204
1205 @noindent
1206 Consider a slightly more complicated example that has three files: a
1207 main program, and the spec and body of a package:
1208
1209 @smallexample @c ada
1210 @cartouche
1211 @group
1212 package Greetings is
1213 procedure Hello;
1214 procedure Goodbye;
1215 end Greetings;
1216
1217 with Ada.Text_IO; use Ada.Text_IO;
1218 package body Greetings is
1219 procedure Hello is
1220 begin
1221 Put_Line ("Hello WORLD!");
1222 end Hello;
1223
1224 procedure Goodbye is
1225 begin
1226 Put_Line ("Goodbye WORLD!");
1227 end Goodbye;
1228 end Greetings;
1229 @end group
1230
1231 @group
1232 with Greetings;
1233 procedure Gmain is
1234 begin
1235 Greetings.Hello;
1236 Greetings.Goodbye;
1237 end Gmain;
1238 @end group
1239 @end cartouche
1240 @end smallexample
1241
1242 @noindent
1243 Following the one-unit-per-file rule, place this program in the
1244 following three separate files:
1245
1246 @table @file
1247 @item greetings.ads
1248 spec of package @code{Greetings}
1249
1250 @item greetings.adb
1251 body of package @code{Greetings}
1252
1253 @item gmain.adb
1254 body of main program
1255 @end table
1256
1257 @noindent
1258 To build an executable version of
1259 this program, we could use four separate steps to compile, bind, and link
1260 the program, as follows:
1261
1262 @smallexample
1263 $ gcc -c gmain.adb
1264 $ gcc -c greetings.adb
1265 $ gnatbind gmain
1266 $ gnatlink gmain
1267 @end smallexample
1268
1269 @noindent
1270 Note that there is no required order of compilation when using GNAT.
1271 In particular it is perfectly fine to compile the main program first.
1272 Also, it is not necessary to compile package specs in the case where
1273 there is an accompanying body; you only need to compile the body. If you want
1274 to submit these files to the compiler for semantic checking and not code
1275 generation, then use the
1276 @option{-gnatc} switch:
1277
1278 @smallexample
1279 $ gcc -c greetings.ads -gnatc
1280 @end smallexample
1281
1282 @noindent
1283 Although the compilation can be done in separate steps as in the
1284 above example, in practice it is almost always more convenient
1285 to use the @command{gnatmake} tool. All you need to know in this case
1286 is the name of the main program's source file. The effect of the above four
1287 commands can be achieved with a single one:
1288
1289 @smallexample
1290 $ gnatmake gmain.adb
1291 @end smallexample
1292
1293 @noindent
1294 In the next section we discuss the advantages of using @command{gnatmake} in
1295 more detail.
1296
1297 @c *****************************
1298 @node Using the gnatmake Utility
1299 @section Using the @command{gnatmake} Utility
1300
1301 @noindent
1302 If you work on a program by compiling single components at a time using
1303 @command{gcc}, you typically keep track of the units you modify. In order to
1304 build a consistent system, you compile not only these units, but also any
1305 units that depend on the units you have modified.
1306 For example, in the preceding case,
1307 if you edit @file{gmain.adb}, you only need to recompile that file. But if
1308 you edit @file{greetings.ads}, you must recompile both
1309 @file{greetings.adb} and @file{gmain.adb}, because both files contain
1310 units that depend on @file{greetings.ads}.
1311
1312 @code{gnatbind} will warn you if you forget one of these compilation
1313 steps, so that it is impossible to generate an inconsistent program as a
1314 result of forgetting to do a compilation. Nevertheless it is tedious and
1315 error-prone to keep track of dependencies among units.
1316 One approach to handle the dependency-bookkeeping is to use a
1317 makefile. However, makefiles present maintenance problems of their own:
1318 if the dependencies change as you change the program, you must make
1319 sure that the makefile is kept up-to-date manually, which is also an
1320 error-prone process.
1321
1322 The @command{gnatmake} utility takes care of these details automatically.
1323 Invoke it using either one of the following forms:
1324
1325 @smallexample
1326 $ gnatmake gmain.adb
1327 $ gnatmake ^gmain^GMAIN^
1328 @end smallexample
1329
1330 @noindent
1331 The argument is the name of the file containing the main program;
1332 you may omit the extension. @command{gnatmake}
1333 examines the environment, automatically recompiles any files that need
1334 recompiling, and binds and links the resulting set of object files,
1335 generating the executable file, @file{^gmain^GMAIN.EXE^}.
1336 In a large program, it
1337 can be extremely helpful to use @command{gnatmake}, because working out by hand
1338 what needs to be recompiled can be difficult.
1339
1340 Note that @command{gnatmake}
1341 takes into account all the Ada rules that
1342 establish dependencies among units. These include dependencies that result
1343 from inlining subprogram bodies, and from
1344 generic instantiation. Unlike some other
1345 Ada make tools, @command{gnatmake} does not rely on the dependencies that were
1346 found by the compiler on a previous compilation, which may possibly
1347 be wrong when sources change. @command{gnatmake} determines the exact set of
1348 dependencies from scratch each time it is run.
1349
1350 @ifset vms
1351 @node Editing with Emacs
1352 @section Editing with Emacs
1353 @cindex Emacs
1354
1355 @noindent
1356 Emacs is an extensible self-documenting text editor that is available in a
1357 separate VMSINSTAL kit.
1358
1359 Invoke Emacs by typing @kbd{Emacs} at the command prompt. To get started,
1360 click on the Emacs Help menu and run the Emacs Tutorial.
1361 In a character cell terminal, Emacs help is invoked with @kbd{Ctrl-h} (also
1362 written as @kbd{C-h}), and the tutorial by @kbd{C-h t}.
1363
1364 Documentation on Emacs and other tools is available in Emacs under the
1365 pull-down menu button: @code{Help - Info}. After selecting @code{Info},
1366 use the middle mouse button to select a topic (e.g.@: Emacs).
1367
1368 In a character cell terminal, do @kbd{C-h i} to invoke info, and then @kbd{m}
1369 (stands for menu) followed by the menu item desired, as in @kbd{m Emacs}, to
1370 get to the Emacs manual.
1371 Help on Emacs is also available by typing @kbd{HELP EMACS} at the DCL command
1372 prompt.
1373
1374 The tutorial is highly recommended in order to learn the intricacies of Emacs,
1375 which is sufficiently extensible to provide for a complete programming
1376 environment and shell for the sophisticated user.
1377 @end ifset
1378
1379 @ifclear vms
1380 @node Introduction to GPS
1381 @section Introduction to GPS
1382 @cindex GPS (GNAT Programming Studio)
1383 @cindex GNAT Programming Studio (GPS)
1384 @noindent
1385 Although the command line interface (@command{gnatmake}, etc.) alone
1386 is sufficient, a graphical Interactive Development
1387 Environment can make it easier for you to compose, navigate, and debug
1388 programs. This section describes the main features of GPS
1389 (``GNAT Programming Studio''), the GNAT graphical IDE.
1390 You will see how to use GPS to build and debug an executable, and
1391 you will also learn some of the basics of the GNAT ``project'' facility.
1392
1393 GPS enables you to do much more than is presented here;
1394 e.g., you can produce a call graph, interface to a third-party
1395 Version Control System, and inspect the generated assembly language
1396 for a program.
1397 Indeed, GPS also supports languages other than Ada.
1398 Such additional information, and an explanation of all of the GPS menu
1399 items. may be found in the on-line help, which includes
1400 a user's guide and a tutorial (these are also accessible from the GNAT
1401 startup menu).
1402
1403 @menu
1404 * Building a New Program with GPS::
1405 * Simple Debugging with GPS::
1406 @end menu
1407
1408 @node Building a New Program with GPS
1409 @subsection Building a New Program with GPS
1410 @noindent
1411 GPS invokes the GNAT compilation tools using information
1412 contained in a @emph{project} (also known as a @emph{project file}):
1413 a collection of properties such
1414 as source directories, identities of main subprograms, tool switches, etc.,
1415 and their associated values.
1416 See @ref{GNAT Project Manager} for details.
1417 In order to run GPS, you will need to either create a new project
1418 or else open an existing one.
1419
1420 This section will explain how you can use GPS to create a project,
1421 to associate Ada source files with a project, and to build and run
1422 programs.
1423
1424 @enumerate
1425 @item @emph{Creating a project}
1426
1427 Invoke GPS, either from the command line or the platform's IDE.
1428 After it starts, GPS will display a ``Welcome'' screen with three
1429 radio buttons:
1430
1431 @itemize @bullet
1432 @item
1433 @code{Start with default project in directory}
1434
1435 @item
1436 @code{Create new project with wizard}
1437
1438 @item
1439 @code{Open existing project}
1440 @end itemize
1441
1442 @noindent
1443 Select @code{Create new project with wizard} and press @code{OK}.
1444 A new window will appear. In the text box labeled with
1445 @code{Enter the name of the project to create}, type @file{sample}
1446 as the project name.
1447 In the next box, browse to choose the directory in which you
1448 would like to create the project file.
1449 After selecting an appropriate directory, press @code{Forward}.
1450
1451 A window will appear with the title
1452 @code{Version Control System Configuration}.
1453 Simply press @code{Forward}.
1454
1455 A window will appear with the title
1456 @code{Please select the source directories for this project}.
1457 The directory that you specified for the project file will be selected
1458 by default as the one to use for sources; simply press @code{Forward}.
1459
1460 A window will appear with the title
1461 @code{Please select the build directory for this project}.
1462 The directory that you specified for the project file will be selected
1463 by default for object files and executables;
1464 simply press @code{Forward}.
1465
1466 A window will appear with the title
1467 @code{Please select the main units for this project}.
1468 You will supply this information later, after creating the source file.
1469 Simply press @code{Forward} for now.
1470
1471 A window will appear with the title
1472 @code{Please select the switches to build the project}.
1473 Press @code{Apply}. This will create a project file named
1474 @file{sample.prj} in the directory that you had specified.
1475
1476 @item @emph{Creating and saving the source file}
1477
1478 After you create the new project, a GPS window will appear, which is
1479 partitioned into two main sections:
1480
1481 @itemize @bullet
1482 @item
1483 A @emph{Workspace area}, initially greyed out, which you will use for
1484 creating and editing source files
1485
1486 @item
1487 Directly below, a @emph{Messages area}, which initially displays a
1488 ``Welcome'' message.
1489 (If the Messages area is not visible, drag its border upward to expand it.)
1490 @end itemize
1491
1492 @noindent
1493 Select @code{File} on the menu bar, and then the @code{New} command.
1494 The Workspace area will become white, and you can now
1495 enter the source program explicitly.
1496 Type the following text
1497
1498 @smallexample @c ada
1499 @group
1500 with Ada.Text_IO; use Ada.Text_IO;
1501 procedure Hello is
1502 begin
1503 Put_Line("Hello from GPS!");
1504 end Hello;
1505 @end group
1506 @end smallexample
1507
1508 @noindent
1509 Select @code{File}, then @code{Save As}, and enter the source file name
1510 @file{hello.adb}.
1511 The file will be saved in the same directory you specified as the
1512 location of the default project file.
1513
1514 @item @emph{Updating the project file}
1515
1516 You need to add the new source file to the project.
1517 To do this, select
1518 the @code{Project} menu and then @code{Edit project properties}.
1519 Click the @code{Main files} tab on the left, and then the
1520 @code{Add} button.
1521 Choose @file{hello.adb} from the list, and press @code{Open}.
1522 The project settings window will reflect this action.
1523 Click @code{OK}.
1524
1525 @item @emph{Building and running the program}
1526
1527 In the main GPS window, now choose the @code{Build} menu, then @code{Make},
1528 and select @file{hello.adb}.
1529 The Messages window will display the resulting invocations of @command{gcc},
1530 @command{gnatbind}, and @command{gnatlink}
1531 (reflecting the default switch settings from the
1532 project file that you created) and then a ``successful compilation/build''
1533 message.
1534
1535 To run the program, choose the @code{Build} menu, then @code{Run}, and
1536 select @command{hello}.
1537 An @emph{Arguments Selection} window will appear.
1538 There are no command line arguments, so just click @code{OK}.
1539
1540 The Messages window will now display the program's output (the string
1541 @code{Hello from GPS}), and at the bottom of the GPS window a status
1542 update is displayed (@code{Run: hello}).
1543 Close the GPS window (or select @code{File}, then @code{Exit}) to
1544 terminate this GPS session.
1545 @end enumerate
1546
1547 @node Simple Debugging with GPS
1548 @subsection Simple Debugging with GPS
1549 @noindent
1550 This section illustrates basic debugging techniques (setting breakpoints,
1551 examining/modifying variables, single stepping).
1552
1553 @enumerate
1554 @item @emph{Opening a project}
1555
1556 Start GPS and select @code{Open existing project}; browse to
1557 specify the project file @file{sample.prj} that you had created in the
1558 earlier example.
1559
1560 @item @emph{Creating a source file}
1561
1562 Select @code{File}, then @code{New}, and type in the following program:
1563
1564 @smallexample @c ada
1565 @group
1566 with Ada.Text_IO; use Ada.Text_IO;
1567 procedure Example is
1568 Line : String (1..80);
1569 N : Natural;
1570 begin
1571 Put_Line("Type a line of text at each prompt; an empty line to exit");
1572 loop
1573 Put(": ");
1574 Get_Line (Line, N);
1575 Put_Line (Line (1..N) );
1576 exit when N=0;
1577 end loop;
1578 end Example;
1579 @end group
1580 @end smallexample
1581
1582 @noindent
1583 Select @code{File}, then @code{Save as}, and enter the file name
1584 @file{example.adb}.
1585
1586 @item @emph{Updating the project file}
1587
1588 Add @code{Example} as a new main unit for the project:
1589 @enumerate a
1590 @item
1591 Select @code{Project}, then @code{Edit Project Properties}.
1592
1593 @item
1594 Select the @code{Main files} tab, click @code{Add}, then
1595 select the file @file{example.adb} from the list, and
1596 click @code{Open}.
1597 You will see the file name appear in the list of main units
1598
1599 @item
1600 Click @code{OK}
1601 @end enumerate
1602
1603 @item @emph{Building/running the executable}
1604
1605 To build the executable
1606 select @code{Build}, then @code{Make}, and then choose @file{example.adb}.
1607
1608 Run the program to see its effect (in the Messages area).
1609 Each line that you enter is displayed; an empty line will
1610 cause the loop to exit and the program to terminate.
1611
1612 @item @emph{Debugging the program}
1613
1614 Note that the @option{-g} switches to @command{gcc} and @command{gnatlink},
1615 which are required for debugging, are on by default when you create
1616 a new project.
1617 Thus unless you intentionally remove these settings, you will be able
1618 to debug any program that you develop using GPS.
1619
1620 @enumerate a
1621 @item @emph{Initializing}
1622
1623 Select @code{Debug}, then @code{Initialize}, then @file{example}
1624
1625 @item @emph{Setting a breakpoint}
1626
1627 After performing the initialization step, you will observe a small
1628 icon to the right of each line number.
1629 This serves as a toggle for breakpoints; clicking the icon will
1630 set a breakpoint at the corresponding line (the icon will change to
1631 a red circle with an ``x''), and clicking it again
1632 will remove the breakpoint / reset the icon.
1633
1634 For purposes of this example, set a breakpoint at line 10 (the
1635 statement @code{Put_Line@ (Line@ (1..N));}
1636
1637 @item @emph{Starting program execution}
1638
1639 Select @code{Debug}, then @code{Run}. When the
1640 @code{Program Arguments} window appears, click @code{OK}.
1641 A console window will appear; enter some line of text,
1642 e.g.@: @code{abcde}, at the prompt.
1643 The program will pause execution when it gets to the
1644 breakpoint, and the corresponding line is highlighted.
1645
1646 @item @emph{Examining a variable}
1647
1648 Move the mouse over one of the occurrences of the variable @code{N}.
1649 You will see the value (5) displayed, in ``tool tip'' fashion.
1650 Right click on @code{N}, select @code{Debug}, then select @code{Display N}.
1651 You will see information about @code{N} appear in the @code{Debugger Data}
1652 pane, showing the value as 5.
1653
1654 @item @emph{Assigning a new value to a variable}
1655
1656 Right click on the @code{N} in the @code{Debugger Data} pane, and
1657 select @code{Set value of N}.
1658 When the input window appears, enter the value @code{4} and click
1659 @code{OK}.
1660 This value does not automatically appear in the @code{Debugger Data}
1661 pane; to see it, right click again on the @code{N} in the
1662 @code{Debugger Data} pane and select @code{Update value}.
1663 The new value, 4, will appear in red.
1664
1665 @item @emph{Single stepping}
1666
1667 Select @code{Debug}, then @code{Next}.
1668 This will cause the next statement to be executed, in this case the
1669 call of @code{Put_Line} with the string slice.
1670 Notice in the console window that the displayed string is simply
1671 @code{abcd} and not @code{abcde} which you had entered.
1672 This is because the upper bound of the slice is now 4 rather than 5.
1673
1674 @item @emph{Removing a breakpoint}
1675
1676 Toggle the breakpoint icon at line 10.
1677
1678 @item @emph{Resuming execution from a breakpoint}
1679
1680 Select @code{Debug}, then @code{Continue}.
1681 The program will reach the next iteration of the loop, and
1682 wait for input after displaying the prompt.
1683 This time, just hit the @kbd{Enter} key.
1684 The value of @code{N} will be 0, and the program will terminate.
1685 The console window will disappear.
1686 @end enumerate
1687 @end enumerate
1688 @end ifclear
1689
1690 @node The GNAT Compilation Model
1691 @chapter The GNAT Compilation Model
1692 @cindex GNAT compilation model
1693 @cindex Compilation model
1694
1695 @menu
1696 * Source Representation::
1697 * Foreign Language Representation::
1698 * File Naming Rules::
1699 * Using Other File Names::
1700 * Alternative File Naming Schemes::
1701 * Generating Object Files::
1702 * Source Dependencies::
1703 * The Ada Library Information Files::
1704 * Binding an Ada Program::
1705 * Mixed Language Programming::
1706 @ifclear vms
1707 * Building Mixed Ada & C++ Programs::
1708 * Comparison between GNAT and C/C++ Compilation Models::
1709 @end ifclear
1710 * Comparison between GNAT and Conventional Ada Library Models::
1711 @ifset vms
1712 * Placement of temporary files::
1713 @end ifset
1714 @end menu
1715
1716 @noindent
1717 This chapter describes the compilation model used by GNAT. Although
1718 similar to that used by other languages, such as C and C++, this model
1719 is substantially different from the traditional Ada compilation models,
1720 which are based on a library. The model is initially described without
1721 reference to the library-based model. If you have not previously used an
1722 Ada compiler, you need only read the first part of this chapter. The
1723 last section describes and discusses the differences between the GNAT
1724 model and the traditional Ada compiler models. If you have used other
1725 Ada compilers, this section will help you to understand those
1726 differences, and the advantages of the GNAT model.
1727
1728 @node Source Representation
1729 @section Source Representation
1730 @cindex Latin-1
1731
1732 @noindent
1733 Ada source programs are represented in standard text files, using
1734 Latin-1 coding. Latin-1 is an 8-bit code that includes the familiar
1735 7-bit ASCII set, plus additional characters used for
1736 representing foreign languages (@pxref{Foreign Language Representation}
1737 for support of non-USA character sets). The format effector characters
1738 are represented using their standard ASCII encodings, as follows:
1739
1740 @table @code
1741 @item VT
1742 @findex VT
1743 Vertical tab, @code{16#0B#}
1744
1745 @item HT
1746 @findex HT
1747 Horizontal tab, @code{16#09#}
1748
1749 @item CR
1750 @findex CR
1751 Carriage return, @code{16#0D#}
1752
1753 @item LF
1754 @findex LF
1755 Line feed, @code{16#0A#}
1756
1757 @item FF
1758 @findex FF
1759 Form feed, @code{16#0C#}
1760 @end table
1761
1762 @noindent
1763 Source files are in standard text file format. In addition, GNAT will
1764 recognize a wide variety of stream formats, in which the end of
1765 physical lines is marked by any of the following sequences:
1766 @code{LF}, @code{CR}, @code{CR-LF}, or @code{LF-CR}. This is useful
1767 in accommodating files that are imported from other operating systems.
1768
1769 @cindex End of source file
1770 @cindex Source file, end
1771 @findex SUB
1772 The end of a source file is normally represented by the physical end of
1773 file. However, the control character @code{16#1A#} (@code{SUB}) is also
1774 recognized as signalling the end of the source file. Again, this is
1775 provided for compatibility with other operating systems where this
1776 code is used to represent the end of file.
1777
1778 Each file contains a single Ada compilation unit, including any pragmas
1779 associated with the unit. For example, this means you must place a
1780 package declaration (a package @dfn{spec}) and the corresponding body in
1781 separate files. An Ada @dfn{compilation} (which is a sequence of
1782 compilation units) is represented using a sequence of files. Similarly,
1783 you will place each subunit or child unit in a separate file.
1784
1785 @node Foreign Language Representation
1786 @section Foreign Language Representation
1787
1788 @noindent
1789 GNAT supports the standard character sets defined in Ada as well as
1790 several other non-standard character sets for use in localized versions
1791 of the compiler (@pxref{Character Set Control}).
1792 @menu
1793 * Latin-1::
1794 * Other 8-Bit Codes::
1795 * Wide Character Encodings::
1796 @end menu
1797
1798 @node Latin-1
1799 @subsection Latin-1
1800 @cindex Latin-1
1801
1802 @noindent
1803 The basic character set is Latin-1. This character set is defined by ISO
1804 standard 8859, part 1. The lower half (character codes @code{16#00#}
1805 @dots{} @code{16#7F#)} is identical to standard ASCII coding, but the upper half
1806 is used to represent additional characters. These include extended letters
1807 used by European languages, such as French accents, the vowels with umlauts
1808 used in German, and the extra letter A-ring used in Swedish.
1809
1810 @findex Ada.Characters.Latin_1
1811 For a complete list of Latin-1 codes and their encodings, see the source
1812 file of library unit @code{Ada.Characters.Latin_1} in file
1813 @file{a-chlat1.ads}.
1814 You may use any of these extended characters freely in character or
1815 string literals. In addition, the extended characters that represent
1816 letters can be used in identifiers.
1817
1818 @node Other 8-Bit Codes
1819 @subsection Other 8-Bit Codes
1820
1821 @noindent
1822 GNAT also supports several other 8-bit coding schemes:
1823
1824 @table @asis
1825 @item ISO 8859-2 (Latin-2)
1826 @cindex Latin-2
1827 @cindex ISO 8859-2
1828 Latin-2 letters allowed in identifiers, with uppercase and lowercase
1829 equivalence.
1830
1831 @item ISO 8859-3 (Latin-3)
1832 @cindex Latin-3
1833 @cindex ISO 8859-3
1834 Latin-3 letters allowed in identifiers, with uppercase and lowercase
1835 equivalence.
1836
1837 @item ISO 8859-4 (Latin-4)
1838 @cindex Latin-4
1839 @cindex ISO 8859-4
1840 Latin-4 letters allowed in identifiers, with uppercase and lowercase
1841 equivalence.
1842
1843 @item ISO 8859-5 (Cyrillic)
1844 @cindex ISO 8859-5
1845 @cindex Cyrillic
1846 ISO 8859-5 letters (Cyrillic) allowed in identifiers, with uppercase and
1847 lowercase equivalence.
1848
1849 @item ISO 8859-15 (Latin-9)
1850 @cindex ISO 8859-15
1851 @cindex Latin-9
1852 ISO 8859-15 (Latin-9) letters allowed in identifiers, with uppercase and
1853 lowercase equivalence
1854
1855 @item IBM PC (code page 437)
1856 @cindex code page 437
1857 This code page is the normal default for PCs in the U.S. It corresponds
1858 to the original IBM PC character set. This set has some, but not all, of
1859 the extended Latin-1 letters, but these letters do not have the same
1860 encoding as Latin-1. In this mode, these letters are allowed in
1861 identifiers with uppercase and lowercase equivalence.
1862
1863 @item IBM PC (code page 850)
1864 @cindex code page 850
1865 This code page is a modification of 437 extended to include all the
1866 Latin-1 letters, but still not with the usual Latin-1 encoding. In this
1867 mode, all these letters are allowed in identifiers with uppercase and
1868 lowercase equivalence.
1869
1870 @item Full Upper 8-bit
1871 Any character in the range 80-FF allowed in identifiers, and all are
1872 considered distinct. In other words, there are no uppercase and lowercase
1873 equivalences in this range. This is useful in conjunction with
1874 certain encoding schemes used for some foreign character sets (e.g.,
1875 the typical method of representing Chinese characters on the PC).
1876
1877 @item No Upper-Half
1878 No upper-half characters in the range 80-FF are allowed in identifiers.
1879 This gives Ada 83 compatibility for identifier names.
1880 @end table
1881
1882 @noindent
1883 For precise data on the encodings permitted, and the uppercase and lowercase
1884 equivalences that are recognized, see the file @file{csets.adb} in
1885 the GNAT compiler sources. You will need to obtain a full source release
1886 of GNAT to obtain this file.
1887
1888 @node Wide Character Encodings
1889 @subsection Wide Character Encodings
1890
1891 @noindent
1892 GNAT allows wide character codes to appear in character and string
1893 literals, and also optionally in identifiers, by means of the following
1894 possible encoding schemes:
1895
1896 @table @asis
1897
1898 @item Hex Coding
1899 In this encoding, a wide character is represented by the following five
1900 character sequence:
1901
1902 @smallexample
1903 ESC a b c d
1904 @end smallexample
1905
1906 @noindent
1907 Where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
1908 characters (using uppercase letters) of the wide character code. For
1909 example, ESC A345 is used to represent the wide character with code
1910 @code{16#A345#}.
1911 This scheme is compatible with use of the full Wide_Character set.
1912
1913 @item Upper-Half Coding
1914 @cindex Upper-Half Coding
1915 The wide character with encoding @code{16#abcd#} where the upper bit is on
1916 (in other words, ``a'' is in the range 8-F) is represented as two bytes,
1917 @code{16#ab#} and @code{16#cd#}. The second byte cannot be a format control
1918 character, but is not required to be in the upper half. This method can
1919 be also used for shift-JIS or EUC, where the internal coding matches the
1920 external coding.
1921
1922 @item Shift JIS Coding
1923 @cindex Shift JIS Coding
1924 A wide character is represented by a two-character sequence,
1925 @code{16#ab#} and
1926 @code{16#cd#}, with the restrictions described for upper-half encoding as
1927 described above. The internal character code is the corresponding JIS
1928 character according to the standard algorithm for Shift-JIS
1929 conversion. Only characters defined in the JIS code set table can be
1930 used with this encoding method.
1931
1932 @item EUC Coding
1933 @cindex EUC Coding
1934 A wide character is represented by a two-character sequence
1935 @code{16#ab#} and
1936 @code{16#cd#}, with both characters being in the upper half. The internal
1937 character code is the corresponding JIS character according to the EUC
1938 encoding algorithm. Only characters defined in the JIS code set table
1939 can be used with this encoding method.
1940
1941 @item UTF-8 Coding
1942 A wide character is represented using
1943 UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO
1944 10646-1/Am.2. Depending on the character value, the representation
1945 is a one, two, or three byte sequence:
1946 @smallexample
1947 @iftex
1948 @leftskip=.7cm
1949 @end iftex
1950 16#0000#-16#007f#: 2#0@var{xxxxxxx}#
1951 16#0080#-16#07ff#: 2#110@var{xxxxx}# 2#10@var{xxxxxx}#
1952 16#0800#-16#ffff#: 2#1110@var{xxxx}# 2#10@var{xxxxxx}# 2#10@var{xxxxxx}#
1953
1954 @end smallexample
1955
1956 @noindent
1957 where the @var{xxx} bits correspond to the left-padded bits of the
1958 16-bit character value. Note that all lower half ASCII characters
1959 are represented as ASCII bytes and all upper half characters and
1960 other wide characters are represented as sequences of upper-half
1961 (The full UTF-8 scheme allows for encoding 31-bit characters as
1962 6-byte sequences, but in this implementation, all UTF-8 sequences
1963 of four or more bytes length will be treated as illegal).
1964 @item Brackets Coding
1965 In this encoding, a wide character is represented by the following eight
1966 character sequence:
1967
1968 @smallexample
1969 [ " a b c d " ]
1970 @end smallexample
1971
1972 @noindent
1973 Where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
1974 characters (using uppercase letters) of the wide character code. For
1975 example, [``A345''] is used to represent the wide character with code
1976 @code{16#A345#}. It is also possible (though not required) to use the
1977 Brackets coding for upper half characters. For example, the code
1978 @code{16#A3#} can be represented as @code{[``A3'']}.
1979
1980 This scheme is compatible with use of the full Wide_Character set,
1981 and is also the method used for wide character encoding in the standard
1982 ACVC (Ada Compiler Validation Capability) test suite distributions.
1983
1984 @end table
1985
1986 @noindent
1987 Note: Some of these coding schemes do not permit the full use of the
1988 Ada character set. For example, neither Shift JIS, nor EUC allow the
1989 use of the upper half of the Latin-1 set.
1990
1991 @node File Naming Rules
1992 @section File Naming Rules
1993
1994 @noindent
1995 The default file name is determined by the name of the unit that the
1996 file contains. The name is formed by taking the full expanded name of
1997 the unit and replacing the separating dots with hyphens and using
1998 ^lowercase^uppercase^ for all letters.
1999
2000 An exception arises if the file name generated by the above rules starts
2001 with one of the characters
2002 @ifset vms
2003 @samp{A}, @samp{G}, @samp{I}, or @samp{S},
2004 @end ifset
2005 @ifclear vms
2006 @samp{a}, @samp{g}, @samp{i}, or @samp{s},
2007 @end ifclear
2008 and the second character is a
2009 minus. In this case, the character ^tilde^dollar sign^ is used in place
2010 of the minus. The reason for this special rule is to avoid clashes with
2011 the standard names for child units of the packages System, Ada,
2012 Interfaces, and GNAT, which use the prefixes
2013 @ifset vms
2014 @samp{S-}, @samp{A-}, @samp{I-}, and @samp{G-},
2015 @end ifset
2016 @ifclear vms
2017 @samp{s-}, @samp{a-}, @samp{i-}, and @samp{g-},
2018 @end ifclear
2019 respectively.
2020
2021 The file extension is @file{.ads} for a spec and
2022 @file{.adb} for a body. The following list shows some
2023 examples of these rules.
2024
2025 @table @file
2026 @item main.ads
2027 Main (spec)
2028 @item main.adb
2029 Main (body)
2030 @item arith_functions.ads
2031 Arith_Functions (package spec)
2032 @item arith_functions.adb
2033 Arith_Functions (package body)
2034 @item func-spec.ads
2035 Func.Spec (child package spec)
2036 @item func-spec.adb
2037 Func.Spec (child package body)
2038 @item main-sub.adb
2039 Sub (subunit of Main)
2040 @item ^a~bad.adb^A$BAD.ADB^
2041 A.Bad (child package body)
2042 @end table
2043
2044 @noindent
2045 Following these rules can result in excessively long
2046 file names if corresponding
2047 unit names are long (for example, if child units or subunits are
2048 heavily nested). An option is available to shorten such long file names
2049 (called file name ``krunching''). This may be particularly useful when
2050 programs being developed with GNAT are to be used on operating systems
2051 with limited file name lengths. @xref{Using gnatkr}.
2052
2053 Of course, no file shortening algorithm can guarantee uniqueness over
2054 all possible unit names; if file name krunching is used, it is your
2055 responsibility to ensure no name clashes occur. Alternatively you
2056 can specify the exact file names that you want used, as described
2057 in the next section. Finally, if your Ada programs are migrating from a
2058 compiler with a different naming convention, you can use the gnatchop
2059 utility to produce source files that follow the GNAT naming conventions.
2060 (For details @pxref{Renaming Files Using gnatchop}.)
2061
2062 Note: in the case of @code{Windows NT/XP} or @code{OpenVMS} operating
2063 systems, case is not significant. So for example on @code{Windows XP}
2064 if the canonical name is @code{main-sub.adb}, you can use the file name
2065 @code{Main-Sub.adb} instead. However, case is significant for other
2066 operating systems, so for example, if you want to use other than
2067 canonically cased file names on a Unix system, you need to follow
2068 the procedures described in the next section.
2069
2070 @node Using Other File Names
2071 @section Using Other File Names
2072 @cindex File names
2073
2074 @noindent
2075 In the previous section, we have described the default rules used by
2076 GNAT to determine the file name in which a given unit resides. It is
2077 often convenient to follow these default rules, and if you follow them,
2078 the compiler knows without being explicitly told where to find all
2079 the files it needs.
2080
2081 However, in some cases, particularly when a program is imported from
2082 another Ada compiler environment, it may be more convenient for the
2083 programmer to specify which file names contain which units. GNAT allows
2084 arbitrary file names to be used by means of the Source_File_Name pragma.
2085 The form of this pragma is as shown in the following examples:
2086 @cindex Source_File_Name pragma
2087
2088 @smallexample @c ada
2089 @cartouche
2090 pragma Source_File_Name (My_Utilities.Stacks,
2091 Spec_File_Name => "myutilst_a.ada");
2092 pragma Source_File_name (My_Utilities.Stacks,
2093 Body_File_Name => "myutilst.ada");
2094 @end cartouche
2095 @end smallexample
2096
2097 @noindent
2098 As shown in this example, the first argument for the pragma is the unit
2099 name (in this example a child unit). The second argument has the form
2100 of a named association. The identifier
2101 indicates whether the file name is for a spec or a body;
2102 the file name itself is given by a string literal.
2103
2104 The source file name pragma is a configuration pragma, which means that
2105 normally it will be placed in the @file{gnat.adc}
2106 file used to hold configuration
2107 pragmas that apply to a complete compilation environment.
2108 For more details on how the @file{gnat.adc} file is created and used
2109 see @ref{Handling of Configuration Pragmas}.
2110 @cindex @file{gnat.adc}
2111
2112 @ifclear vms
2113 GNAT allows completely arbitrary file names to be specified using the
2114 source file name pragma. However, if the file name specified has an
2115 extension other than @file{.ads} or @file{.adb} it is necessary to use
2116 a special syntax when compiling the file. The name in this case must be
2117 preceded by the special sequence @option{-x} followed by a space and the name
2118 of the language, here @code{ada}, as in:
2119
2120 @smallexample
2121 $ gcc -c -x ada peculiar_file_name.sim
2122 @end smallexample
2123 @end ifclear
2124
2125 @noindent
2126 @command{gnatmake} handles non-standard file names in the usual manner (the
2127 non-standard file name for the main program is simply used as the
2128 argument to gnatmake). Note that if the extension is also non-standard,
2129 then it must be included in the @command{gnatmake} command, it may not
2130 be omitted.
2131
2132 @node Alternative File Naming Schemes
2133 @section Alternative File Naming Schemes
2134 @cindex File naming schemes, alternative
2135 @cindex File names
2136
2137 In the previous section, we described the use of the @code{Source_File_Name}
2138 pragma to allow arbitrary names to be assigned to individual source files.
2139 However, this approach requires one pragma for each file, and especially in
2140 large systems can result in very long @file{gnat.adc} files, and also create
2141 a maintenance problem.
2142
2143 GNAT also provides a facility for specifying systematic file naming schemes
2144 other than the standard default naming scheme previously described. An
2145 alternative scheme for naming is specified by the use of
2146 @code{Source_File_Name} pragmas having the following format:
2147 @cindex Source_File_Name pragma
2148
2149 @smallexample @c ada
2150 pragma Source_File_Name (
2151 Spec_File_Name => FILE_NAME_PATTERN
2152 @r{[},Casing => CASING_SPEC@r{]}
2153 @r{[},Dot_Replacement => STRING_LITERAL@r{]});
2154
2155 pragma Source_File_Name (
2156 Body_File_Name => FILE_NAME_PATTERN
2157 @r{[},Casing => CASING_SPEC@r{]}
2158 @r{[},Dot_Replacement => STRING_LITERAL@r{]});
2159
2160 pragma Source_File_Name (
2161 Subunit_File_Name => FILE_NAME_PATTERN
2162 @r{[},Casing => CASING_SPEC@r{]}
2163 @r{[},Dot_Replacement => STRING_LITERAL@r{]});
2164
2165 FILE_NAME_PATTERN ::= STRING_LITERAL
2166 CASING_SPEC ::= Lowercase | Uppercase | Mixedcase
2167 @end smallexample
2168
2169 @noindent
2170 The @code{FILE_NAME_PATTERN} string shows how the file name is constructed.
2171 It contains a single asterisk character, and the unit name is substituted
2172 systematically for this asterisk. The optional parameter
2173 @code{Casing} indicates
2174 whether the unit name is to be all upper-case letters, all lower-case letters,
2175 or mixed-case. If no
2176 @code{Casing} parameter is used, then the default is all
2177 ^lower-case^upper-case^.
2178
2179 The optional @code{Dot_Replacement} string is used to replace any periods
2180 that occur in subunit or child unit names. If no @code{Dot_Replacement}
2181 argument is used then separating dots appear unchanged in the resulting
2182 file name.
2183 Although the above syntax indicates that the
2184 @code{Casing} argument must appear
2185 before the @code{Dot_Replacement} argument, but it
2186 is also permissible to write these arguments in the opposite order.
2187
2188 As indicated, it is possible to specify different naming schemes for
2189 bodies, specs, and subunits. Quite often the rule for subunits is the
2190 same as the rule for bodies, in which case, there is no need to give
2191 a separate @code{Subunit_File_Name} rule, and in this case the
2192 @code{Body_File_name} rule is used for subunits as well.
2193
2194 The separate rule for subunits can also be used to implement the rather
2195 unusual case of a compilation environment (e.g.@: a single directory) which
2196 contains a subunit and a child unit with the same unit name. Although
2197 both units cannot appear in the same partition, the Ada Reference Manual
2198 allows (but does not require) the possibility of the two units coexisting
2199 in the same environment.
2200
2201 The file name translation works in the following steps:
2202
2203 @itemize @bullet
2204
2205 @item
2206 If there is a specific @code{Source_File_Name} pragma for the given unit,
2207 then this is always used, and any general pattern rules are ignored.
2208
2209 @item
2210 If there is a pattern type @code{Source_File_Name} pragma that applies to
2211 the unit, then the resulting file name will be used if the file exists. If
2212 more than one pattern matches, the latest one will be tried first, and the
2213 first attempt resulting in a reference to a file that exists will be used.
2214
2215 @item
2216 If no pattern type @code{Source_File_Name} pragma that applies to the unit
2217 for which the corresponding file exists, then the standard GNAT default
2218 naming rules are used.
2219
2220 @end itemize
2221
2222 @noindent
2223 As an example of the use of this mechanism, consider a commonly used scheme
2224 in which file names are all lower case, with separating periods copied
2225 unchanged to the resulting file name, and specs end with @file{.1.ada}, and
2226 bodies end with @file{.2.ada}. GNAT will follow this scheme if the following
2227 two pragmas appear:
2228
2229 @smallexample @c ada
2230 pragma Source_File_Name
2231 (Spec_File_Name => "*.1.ada");
2232 pragma Source_File_Name
2233 (Body_File_Name => "*.2.ada");
2234 @end smallexample
2235
2236 @noindent
2237 The default GNAT scheme is actually implemented by providing the following
2238 default pragmas internally:
2239
2240 @smallexample @c ada
2241 pragma Source_File_Name
2242 (Spec_File_Name => "*.ads", Dot_Replacement => "-");
2243 pragma Source_File_Name
2244 (Body_File_Name => "*.adb", Dot_Replacement => "-");
2245 @end smallexample
2246
2247 @noindent
2248 Our final example implements a scheme typically used with one of the
2249 Ada 83 compilers, where the separator character for subunits was ``__''
2250 (two underscores), specs were identified by adding @file{_.ADA}, bodies
2251 by adding @file{.ADA}, and subunits by
2252 adding @file{.SEP}. All file names were
2253 upper case. Child units were not present of course since this was an
2254 Ada 83 compiler, but it seems reasonable to extend this scheme to use
2255 the same double underscore separator for child units.
2256
2257 @smallexample @c ada
2258 pragma Source_File_Name
2259 (Spec_File_Name => "*_.ADA",
2260 Dot_Replacement => "__",
2261 Casing = Uppercase);
2262 pragma Source_File_Name
2263 (Body_File_Name => "*.ADA",
2264 Dot_Replacement => "__",
2265 Casing = Uppercase);
2266 pragma Source_File_Name
2267 (Subunit_File_Name => "*.SEP",
2268 Dot_Replacement => "__",
2269 Casing = Uppercase);
2270 @end smallexample
2271
2272 @node Generating Object Files
2273 @section Generating Object Files
2274
2275 @noindent
2276 An Ada program consists of a set of source files, and the first step in
2277 compiling the program is to generate the corresponding object files.
2278 These are generated by compiling a subset of these source files.
2279 The files you need to compile are the following:
2280
2281 @itemize @bullet
2282 @item
2283 If a package spec has no body, compile the package spec to produce the
2284 object file for the package.
2285
2286 @item
2287 If a package has both a spec and a body, compile the body to produce the
2288 object file for the package. The source file for the package spec need
2289 not be compiled in this case because there is only one object file, which
2290 contains the code for both the spec and body of the package.
2291
2292 @item
2293 For a subprogram, compile the subprogram body to produce the object file
2294 for the subprogram. The spec, if one is present, is as usual in a
2295 separate file, and need not be compiled.
2296
2297 @item
2298 @cindex Subunits
2299 In the case of subunits, only compile the parent unit. A single object
2300 file is generated for the entire subunit tree, which includes all the
2301 subunits.
2302
2303 @item
2304 Compile child units independently of their parent units
2305 (though, of course, the spec of all the ancestor unit must be present in order
2306 to compile a child unit).
2307
2308 @item
2309 @cindex Generics
2310 Compile generic units in the same manner as any other units. The object
2311 files in this case are small dummy files that contain at most the
2312 flag used for elaboration checking. This is because GNAT always handles generic
2313 instantiation by means of macro expansion. However, it is still necessary to
2314 compile generic units, for dependency checking and elaboration purposes.
2315 @end itemize
2316
2317 @noindent
2318 The preceding rules describe the set of files that must be compiled to
2319 generate the object files for a program. Each object file has the same
2320 name as the corresponding source file, except that the extension is
2321 @file{.o} as usual.
2322
2323 You may wish to compile other files for the purpose of checking their
2324 syntactic and semantic correctness. For example, in the case where a
2325 package has a separate spec and body, you would not normally compile the
2326 spec. However, it is convenient in practice to compile the spec to make
2327 sure it is error-free before compiling clients of this spec, because such
2328 compilations will fail if there is an error in the spec.
2329
2330 GNAT provides an option for compiling such files purely for the
2331 purposes of checking correctness; such compilations are not required as
2332 part of the process of building a program. To compile a file in this
2333 checking mode, use the @option{-gnatc} switch.
2334
2335 @node Source Dependencies
2336 @section Source Dependencies
2337
2338 @noindent
2339 A given object file clearly depends on the source file which is compiled
2340 to produce it. Here we are using @dfn{depends} in the sense of a typical
2341 @code{make} utility; in other words, an object file depends on a source
2342 file if changes to the source file require the object file to be
2343 recompiled.
2344 In addition to this basic dependency, a given object may depend on
2345 additional source files as follows:
2346
2347 @itemize @bullet
2348 @item
2349 If a file being compiled @code{with}'s a unit @var{X}, the object file
2350 depends on the file containing the spec of unit @var{X}. This includes
2351 files that are @code{with}'ed implicitly either because they are parents
2352 of @code{with}'ed child units or they are run-time units required by the
2353 language constructs used in a particular unit.
2354
2355 @item
2356 If a file being compiled instantiates a library level generic unit, the
2357 object file depends on both the spec and body files for this generic
2358 unit.
2359
2360 @item
2361 If a file being compiled instantiates a generic unit defined within a
2362 package, the object file depends on the body file for the package as
2363 well as the spec file.
2364
2365 @item
2366 @findex Inline
2367 @cindex @option{-gnatn} switch
2368 If a file being compiled contains a call to a subprogram for which
2369 pragma @code{Inline} applies and inlining is activated with the
2370 @option{-gnatn} switch, the object file depends on the file containing the
2371 body of this subprogram as well as on the file containing the spec. Note
2372 that for inlining to actually occur as a result of the use of this switch,
2373 it is necessary to compile in optimizing mode.
2374
2375 @cindex @option{-gnatN} switch
2376 The use of @option{-gnatN} activates inlining optimization
2377 that is performed by the front end of the compiler. This inlining does
2378 not require that the code generation be optimized. Like @option{-gnatn},
2379 the use of this switch generates additional dependencies.
2380
2381 When using a gcc-based back end (in practice this means using any version
2382 of GNAT other than the JGNAT, .NET or GNAAMP versions), then the use of
2383 @option{-gnatN} is deprecated, and the use of @option{-gnatn} is preferred.
2384 Historically front end inlining was more extensive than the gcc back end
2385 inlining, but that is no longer the case.
2386
2387 @item
2388 If an object file @file{O} depends on the proper body of a subunit through
2389 inlining or instantiation, it depends on the parent unit of the subunit.
2390 This means that any modification of the parent unit or one of its subunits
2391 affects the compilation of @file{O}.
2392
2393 @item
2394 The object file for a parent unit depends on all its subunit body files.
2395
2396 @item
2397 The previous two rules meant that for purposes of computing dependencies and
2398 recompilation, a body and all its subunits are treated as an indivisible whole.
2399
2400 @noindent
2401 These rules are applied transitively: if unit @code{A} @code{with}'s
2402 unit @code{B}, whose elaboration calls an inlined procedure in package
2403 @code{C}, the object file for unit @code{A} will depend on the body of
2404 @code{C}, in file @file{c.adb}.
2405
2406 The set of dependent files described by these rules includes all the
2407 files on which the unit is semantically dependent, as dictated by the
2408 Ada language standard. However, it is a superset of what the
2409 standard describes, because it includes generic, inline, and subunit
2410 dependencies.
2411
2412 An object file must be recreated by recompiling the corresponding source
2413 file if any of the source files on which it depends are modified. For
2414 example, if the @code{make} utility is used to control compilation,
2415 the rule for an Ada object file must mention all the source files on
2416 which the object file depends, according to the above definition.
2417 The determination of the necessary
2418 recompilations is done automatically when one uses @command{gnatmake}.
2419 @end itemize
2420
2421 @node The Ada Library Information Files
2422 @section The Ada Library Information Files
2423 @cindex Ada Library Information files
2424 @cindex @file{ALI} files
2425
2426 @noindent
2427 Each compilation actually generates two output files. The first of these
2428 is the normal object file that has a @file{.o} extension. The second is a
2429 text file containing full dependency information. It has the same
2430 name as the source file, but an @file{.ali} extension.
2431 This file is known as the Ada Library Information (@file{ALI}) file.
2432 The following information is contained in the @file{ALI} file.
2433
2434 @itemize @bullet
2435 @item
2436 Version information (indicates which version of GNAT was used to compile
2437 the unit(s) in question)
2438
2439 @item
2440 Main program information (including priority and time slice settings,
2441 as well as the wide character encoding used during compilation).
2442
2443 @item
2444 List of arguments used in the @command{gcc} command for the compilation
2445
2446 @item
2447 Attributes of the unit, including configuration pragmas used, an indication
2448 of whether the compilation was successful, exception model used etc.
2449
2450 @item
2451 A list of relevant restrictions applying to the unit (used for consistency)
2452 checking.
2453
2454 @item
2455 Categorization information (e.g.@: use of pragma @code{Pure}).
2456
2457 @item
2458 Information on all @code{with}'ed units, including presence of
2459 @code{Elaborate} or @code{Elaborate_All} pragmas.
2460
2461 @item
2462 Information from any @code{Linker_Options} pragmas used in the unit
2463
2464 @item
2465 Information on the use of @code{Body_Version} or @code{Version}
2466 attributes in the unit.
2467
2468 @item
2469 Dependency information. This is a list of files, together with
2470 time stamp and checksum information. These are files on which
2471 the unit depends in the sense that recompilation is required
2472 if any of these units are modified.
2473
2474 @item
2475 Cross-reference data. Contains information on all entities referenced
2476 in the unit. Used by tools like @code{gnatxref} and @code{gnatfind} to
2477 provide cross-reference information.
2478
2479 @end itemize
2480
2481 @noindent
2482 For a full detailed description of the format of the @file{ALI} file,
2483 see the source of the body of unit @code{Lib.Writ}, contained in file
2484 @file{lib-writ.adb} in the GNAT compiler sources.
2485
2486 @node Binding an Ada Program
2487 @section Binding an Ada Program
2488
2489 @noindent
2490 When using languages such as C and C++, once the source files have been
2491 compiled the only remaining step in building an executable program
2492 is linking the object modules together. This means that it is possible to
2493 link an inconsistent version of a program, in which two units have
2494 included different versions of the same header.
2495
2496 The rules of Ada do not permit such an inconsistent program to be built.
2497 For example, if two clients have different versions of the same package,
2498 it is illegal to build a program containing these two clients.
2499 These rules are enforced by the GNAT binder, which also determines an
2500 elaboration order consistent with the Ada rules.
2501
2502 The GNAT binder is run after all the object files for a program have
2503 been created. It is given the name of the main program unit, and from
2504 this it determines the set of units required by the program, by reading the
2505 corresponding ALI files. It generates error messages if the program is
2506 inconsistent or if no valid order of elaboration exists.
2507
2508 If no errors are detected, the binder produces a main program, in Ada by
2509 default, that contains calls to the elaboration procedures of those
2510 compilation unit that require them, followed by
2511 a call to the main program. This Ada program is compiled to generate the
2512 object file for the main program. The name of
2513 the Ada file is @file{b~@var{xxx}.adb} (with the corresponding spec
2514 @file{b~@var{xxx}.ads}) where @var{xxx} is the name of the
2515 main program unit.
2516
2517 Finally, the linker is used to build the resulting executable program,
2518 using the object from the main program from the bind step as well as the
2519 object files for the Ada units of the program.
2520
2521 @node Mixed Language Programming
2522 @section Mixed Language Programming
2523 @cindex Mixed Language Programming
2524
2525 @noindent
2526 This section describes how to develop a mixed-language program,
2527 specifically one that comprises units in both Ada and C.
2528
2529 @menu
2530 * Interfacing to C::
2531 * Calling Conventions::
2532 @end menu
2533
2534 @node Interfacing to C
2535 @subsection Interfacing to C
2536 @noindent
2537 Interfacing Ada with a foreign language such as C involves using
2538 compiler directives to import and/or export entity definitions in each
2539 language---using @code{extern} statements in C, for instance, and the
2540 @code{Import}, @code{Export}, and @code{Convention} pragmas in Ada.
2541 A full treatment of these topics is provided in Appendix B, section 1
2542 of the Ada Reference Manual.
2543
2544 There are two ways to build a program using GNAT that contains some Ada
2545 sources and some foreign language sources, depending on whether or not
2546 the main subprogram is written in Ada. Here is a source example with
2547 the main subprogram in Ada:
2548
2549 @smallexample
2550 /* file1.c */
2551 #include <stdio.h>
2552
2553 void print_num (int num)
2554 @{
2555 printf ("num is %d.\n", num);
2556 return;
2557 @}
2558
2559 /* file2.c */
2560
2561 /* num_from_Ada is declared in my_main.adb */
2562 extern int num_from_Ada;
2563
2564 int get_num (void)
2565 @{
2566 return num_from_Ada;
2567 @}
2568 @end smallexample
2569
2570 @smallexample @c ada
2571 -- my_main.adb
2572 procedure My_Main is
2573
2574 -- Declare then export an Integer entity called num_from_Ada
2575 My_Num : Integer := 10;
2576 pragma Export (C, My_Num, "num_from_Ada");
2577
2578 -- Declare an Ada function spec for Get_Num, then use
2579 -- C function get_num for the implementation.
2580 function Get_Num return Integer;
2581 pragma Import (C, Get_Num, "get_num");
2582
2583 -- Declare an Ada procedure spec for Print_Num, then use
2584 -- C function print_num for the implementation.
2585 procedure Print_Num (Num : Integer);
2586 pragma Import (C, Print_Num, "print_num");
2587
2588 begin
2589 Print_Num (Get_Num);
2590 end My_Main;
2591 @end smallexample
2592
2593 @enumerate
2594 @item
2595 To build this example, first compile the foreign language files to
2596 generate object files:
2597 @smallexample
2598 ^gcc -c file1.c^gcc -c FILE1.C^
2599 ^gcc -c file2.c^gcc -c FILE2.C^
2600 @end smallexample
2601
2602 @item
2603 Then, compile the Ada units to produce a set of object files and ALI
2604 files:
2605 @smallexample
2606 gnatmake ^-c^/ACTIONS=COMPILE^ my_main.adb
2607 @end smallexample
2608
2609 @item
2610 Run the Ada binder on the Ada main program:
2611 @smallexample
2612 gnatbind my_main.ali
2613 @end smallexample
2614
2615 @item
2616 Link the Ada main program, the Ada objects and the other language
2617 objects:
2618 @smallexample
2619 gnatlink my_main.ali file1.o file2.o
2620 @end smallexample
2621 @end enumerate
2622
2623 The last three steps can be grouped in a single command:
2624 @smallexample
2625 gnatmake my_main.adb -largs file1.o file2.o
2626 @end smallexample
2627
2628 @cindex Binder output file
2629 @noindent
2630 If the main program is in a language other than Ada, then you may have
2631 more than one entry point into the Ada subsystem. You must use a special
2632 binder option to generate callable routines that initialize and
2633 finalize the Ada units (@pxref{Binding with Non-Ada Main Programs}).
2634 Calls to the initialization and finalization routines must be inserted
2635 in the main program, or some other appropriate point in the code. The
2636 call to initialize the Ada units must occur before the first Ada
2637 subprogram is called, and the call to finalize the Ada units must occur
2638 after the last Ada subprogram returns. The binder will place the
2639 initialization and finalization subprograms into the
2640 @file{b~@var{xxx}.adb} file where they can be accessed by your C
2641 sources. To illustrate, we have the following example:
2642
2643 @smallexample
2644 /* main.c */
2645 extern void adainit (void);
2646 extern void adafinal (void);
2647 extern int add (int, int);
2648 extern int sub (int, int);
2649
2650 int main (int argc, char *argv[])
2651 @{
2652 int a = 21, b = 7;
2653
2654 adainit();
2655
2656 /* Should print "21 + 7 = 28" */
2657 printf ("%d + %d = %d\n", a, b, add (a, b));
2658 /* Should print "21 - 7 = 14" */
2659 printf ("%d - %d = %d\n", a, b, sub (a, b));
2660
2661 adafinal();
2662 @}
2663 @end smallexample
2664
2665 @smallexample @c ada
2666 -- unit1.ads
2667 package Unit1 is
2668 function Add (A, B : Integer) return Integer;
2669 pragma Export (C, Add, "add");
2670 end Unit1;
2671
2672 -- unit1.adb
2673 package body Unit1 is
2674 function Add (A, B : Integer) return Integer is
2675 begin
2676 return A + B;
2677 end Add;
2678 end Unit1;
2679
2680 -- unit2.ads
2681 package Unit2 is
2682 function Sub (A, B : Integer) return Integer;
2683 pragma Export (C, Sub, "sub");
2684 end Unit2;
2685
2686 -- unit2.adb
2687 package body Unit2 is
2688 function Sub (A, B : Integer) return Integer is
2689 begin
2690 return A - B;
2691 end Sub;
2692 end Unit2;
2693 @end smallexample
2694
2695 @enumerate
2696 @item
2697 The build procedure for this application is similar to the last
2698 example's. First, compile the foreign language files to generate object
2699 files:
2700 @smallexample
2701 ^gcc -c main.c^gcc -c main.c^
2702 @end smallexample
2703
2704 @item
2705 Next, compile the Ada units to produce a set of object files and ALI
2706 files:
2707 @smallexample
2708 gnatmake ^-c^/ACTIONS=COMPILE^ unit1.adb
2709 gnatmake ^-c^/ACTIONS=COMPILE^ unit2.adb
2710 @end smallexample
2711
2712 @item
2713 Run the Ada binder on every generated ALI file. Make sure to use the
2714 @option{-n} option to specify a foreign main program:
2715 @smallexample
2716 gnatbind ^-n^/NOMAIN^ unit1.ali unit2.ali
2717 @end smallexample
2718
2719 @item
2720 Link the Ada main program, the Ada objects and the foreign language
2721 objects. You need only list the last ALI file here:
2722 @smallexample
2723 gnatlink unit2.ali main.o -o exec_file
2724 @end smallexample
2725
2726 This procedure yields a binary executable called @file{exec_file}.
2727 @end enumerate
2728
2729 @noindent
2730 Depending on the circumstances (for example when your non-Ada main object
2731 does not provide symbol @code{main}), you may also need to instruct the
2732 GNAT linker not to include the standard startup objects by passing the
2733 @option{^-nostartfiles^/NOSTART_FILES^} switch to @command{gnatlink}.
2734
2735 @node Calling Conventions
2736 @subsection Calling Conventions
2737 @cindex Foreign Languages
2738 @cindex Calling Conventions
2739 GNAT follows standard calling sequence conventions and will thus interface
2740 to any other language that also follows these conventions. The following
2741 Convention identifiers are recognized by GNAT:
2742
2743 @table @code
2744 @cindex Interfacing to Ada
2745 @cindex Other Ada compilers
2746 @cindex Convention Ada
2747 @item Ada
2748 This indicates that the standard Ada calling sequence will be
2749 used and all Ada data items may be passed without any limitations in the
2750 case where GNAT is used to generate both the caller and callee. It is also
2751 possible to mix GNAT generated code and code generated by another Ada
2752 compiler. In this case, the data types should be restricted to simple
2753 cases, including primitive types. Whether complex data types can be passed
2754 depends on the situation. Probably it is safe to pass simple arrays, such
2755 as arrays of integers or floats. Records may or may not work, depending
2756 on whether both compilers lay them out identically. Complex structures
2757 involving variant records, access parameters, tasks, or protected types,
2758 are unlikely to be able to be passed.
2759
2760 Note that in the case of GNAT running
2761 on a platform that supports HP Ada 83, a higher degree of compatibility
2762 can be guaranteed, and in particular records are layed out in an identical
2763 manner in the two compilers. Note also that if output from two different
2764 compilers is mixed, the program is responsible for dealing with elaboration
2765 issues. Probably the safest approach is to write the main program in the
2766 version of Ada other than GNAT, so that it takes care of its own elaboration
2767 requirements, and then call the GNAT-generated adainit procedure to ensure
2768 elaboration of the GNAT components. Consult the documentation of the other
2769 Ada compiler for further details on elaboration.
2770
2771 However, it is not possible to mix the tasking run time of GNAT and
2772 HP Ada 83, All the tasking operations must either be entirely within
2773 GNAT compiled sections of the program, or entirely within HP Ada 83
2774 compiled sections of the program.
2775
2776 @cindex Interfacing to Assembly
2777 @cindex Convention Assembler
2778 @item Assembler
2779 Specifies assembler as the convention. In practice this has the
2780 same effect as convention Ada (but is not equivalent in the sense of being
2781 considered the same convention).
2782
2783 @cindex Convention Asm
2784 @findex Asm
2785 @item Asm
2786 Equivalent to Assembler.
2787
2788 @cindex Interfacing to COBOL
2789 @cindex Convention COBOL
2790 @findex COBOL
2791 @item COBOL
2792 Data will be passed according to the conventions described
2793 in section B.4 of the Ada Reference Manual.
2794
2795 @findex C
2796 @cindex Interfacing to C
2797 @cindex Convention C
2798 @item C
2799 Data will be passed according to the conventions described
2800 in section B.3 of the Ada Reference Manual.
2801
2802 A note on interfacing to a C ``varargs'' function:
2803 @findex C varargs function
2804 @cindex Interfacing to C varargs function
2805 @cindex varargs function interfaces
2806
2807 @itemize @bullet
2808 @item
2809 In C, @code{varargs} allows a function to take a variable number of
2810 arguments. There is no direct equivalent in this to Ada. One
2811 approach that can be used is to create a C wrapper for each
2812 different profile and then interface to this C wrapper. For
2813 example, to print an @code{int} value using @code{printf},
2814 create a C function @code{printfi} that takes two arguments, a
2815 pointer to a string and an int, and calls @code{printf}.
2816 Then in the Ada program, use pragma @code{Import} to
2817 interface to @code{printfi}.
2818
2819 @item
2820 It may work on some platforms to directly interface to
2821 a @code{varargs} function by providing a specific Ada profile
2822 for a particular call. However, this does not work on
2823 all platforms, since there is no guarantee that the
2824 calling sequence for a two argument normal C function
2825 is the same as for calling a @code{varargs} C function with
2826 the same two arguments.
2827 @end itemize
2828
2829 @cindex Convention Default
2830 @findex Default
2831 @item Default
2832 Equivalent to C.
2833
2834 @cindex Convention External
2835 @findex External
2836 @item External
2837 Equivalent to C.
2838
2839 @ifclear vms
2840 @findex C++
2841 @cindex Interfacing to C++
2842 @cindex Convention C++
2843 @item C_Plus_Plus (or CPP)
2844 This stands for C++. For most purposes this is identical to C.
2845 See the separate description of the specialized GNAT pragmas relating to
2846 C++ interfacing for further details.
2847 @end ifclear
2848
2849 @findex Fortran
2850 @cindex Interfacing to Fortran
2851 @cindex Convention Fortran
2852 @item Fortran
2853 Data will be passed according to the conventions described
2854 in section B.5 of the Ada Reference Manual.
2855
2856 @item Intrinsic
2857 This applies to an intrinsic operation, as defined in the Ada
2858 Reference Manual. If a pragma Import (Intrinsic) applies to a subprogram,
2859 this means that the body of the subprogram is provided by the compiler itself,
2860 usually by means of an efficient code sequence, and that the user does not
2861 supply an explicit body for it. In an application program, the pragma may
2862 be applied to the following sets of names:
2863
2864 @itemize @bullet
2865 @item
2866 Rotate_Left, Rotate_Right, Shift_Left, Shift_Right,
2867 Shift_Right_Arithmetic. The corresponding subprogram declaration must have
2868 two formal parameters. The
2869 first one must be a signed integer type or a modular type with a binary
2870 modulus, and the second parameter must be of type Natural.
2871 The return type must be the same as the type of the first argument. The size
2872 of this type can only be 8, 16, 32, or 64.
2873
2874 @item
2875 Binary arithmetic operators: ``+'', ``-'', ``*'', ``/''
2876 The corresponding operator declaration must have parameters and result type
2877 that have the same root numeric type (for example, all three are long_float
2878 types). This simplifies the definition of operations that use type checking
2879 to perform dimensional checks:
2880
2881 @smallexample @c ada
2882 type Distance is new Long_Float;
2883 type Time is new Long_Float;
2884 type Velocity is new Long_Float;
2885 function "/" (D : Distance; T : Time)
2886 return Velocity;
2887 pragma Import (Intrinsic, "/");
2888 @end smallexample
2889
2890 @noindent
2891 This common idiom is often programmed with a generic definition and an
2892 explicit body. The pragma makes it simpler to introduce such declarations.
2893 It incurs no overhead in compilation time or code size, because it is
2894 implemented as a single machine instruction.
2895
2896 @item
2897 General subprogram entities, to bind an Ada subprogram declaration to
2898 a compiler builtin by name with back-ends where such interfaces are
2899 available. A typical example is the set of ``__builtin'' functions
2900 exposed by the GCC back-end, as in the following example:
2901
2902 @smallexample @c ada
2903 function builtin_sqrt (F : Float) return Float;
2904 pragma Import (Intrinsic, builtin_sqrt, "__builtin_sqrtf");
2905 @end smallexample
2906
2907 Most of the GCC builtins are accessible this way, and as for other
2908 import conventions (e.g. C), it is the user's responsibility to ensure
2909 that the Ada subprogram profile matches the underlying builtin
2910 expectations.
2911 @end itemize
2912
2913 @noindent
2914
2915 @ifset unw
2916 @findex Stdcall
2917 @cindex Convention Stdcall
2918 @item Stdcall
2919 This is relevant only to Windows XP/2000/NT implementations of GNAT,
2920 and specifies that the @code{Stdcall} calling sequence will be used,
2921 as defined by the NT API. Nevertheless, to ease building
2922 cross-platform bindings this convention will be handled as a @code{C} calling
2923 convention on non-Windows platforms.
2924
2925 @findex DLL
2926 @cindex Convention DLL
2927 @item DLL
2928 This is equivalent to @code{Stdcall}.
2929
2930 @findex Win32
2931 @cindex Convention Win32
2932 @item Win32
2933 This is equivalent to @code{Stdcall}.
2934 @end ifset
2935
2936 @findex Stubbed
2937 @cindex Convention Stubbed
2938 @item Stubbed
2939 This is a special convention that indicates that the compiler
2940 should provide a stub body that raises @code{Program_Error}.
2941 @end table
2942
2943 @noindent
2944 GNAT additionally provides a useful pragma @code{Convention_Identifier}
2945 that can be used to parametrize conventions and allow additional synonyms
2946 to be specified. For example if you have legacy code in which the convention
2947 identifier Fortran77 was used for Fortran, you can use the configuration
2948 pragma:
2949
2950 @smallexample @c ada
2951 pragma Convention_Identifier (Fortran77, Fortran);
2952 @end smallexample
2953
2954 @noindent
2955 And from now on the identifier Fortran77 may be used as a convention
2956 identifier (for example in an @code{Import} pragma) with the same
2957 meaning as Fortran.
2958
2959 @ifclear vms
2960 @node Building Mixed Ada & C++ Programs
2961 @section Building Mixed Ada and C++ Programs
2962
2963 @noindent
2964 A programmer inexperienced with mixed-language development may find that
2965 building an application containing both Ada and C++ code can be a
2966 challenge. This section gives a few
2967 hints that should make this task easier. The first section addresses
2968 the differences between interfacing with C and interfacing with C++.
2969 The second section
2970 looks into the delicate problem of linking the complete application from
2971 its Ada and C++ parts. The last section gives some hints on how the GNAT
2972 run-time library can be adapted in order to allow inter-language dispatching
2973 with a new C++ compiler.
2974
2975 @menu
2976 * Interfacing to C++::
2977 * Linking a Mixed C++ & Ada Program::
2978 * A Simple Example::
2979 * Interfacing with C++ at the Class Level::
2980 @end menu
2981
2982 @node Interfacing to C++
2983 @subsection Interfacing to C++
2984
2985 @noindent
2986 GNAT supports interfacing with the G++ compiler (or any C++ compiler
2987 generating code that is compatible with the G++ Application Binary
2988 Interface ---see http://www.codesourcery.com/archives/cxx-abi).
2989
2990 @noindent
2991 Interfacing can be done at 3 levels: simple data, subprograms, and
2992 classes. In the first two cases, GNAT offers a specific @code{Convention
2993 C_Plus_Plus} (or @code{CPP}) that behaves exactly like @code{Convention C}.
2994 Usually, C++ mangles the names of subprograms, and currently, GNAT does
2995 not provide any help to solve the demangling problem. This problem can be
2996 addressed in two ways:
2997 @itemize @bullet
2998 @item
2999 by modifying the C++ code in order to force a C convention using
3000 the @code{extern "C"} syntax.
3001
3002 @item
3003 by figuring out the mangled name and use it as the Link_Name argument of
3004 the pragma import.
3005 @end itemize
3006
3007 @noindent
3008 Interfacing at the class level can be achieved by using the GNAT specific
3009 pragmas such as @code{CPP_Constructor}. @xref{Interfacing to C++,,,
3010 gnat_rm, GNAT Reference Manual}, for additional information.
3011
3012 @node Linking a Mixed C++ & Ada Program
3013 @subsection Linking a Mixed C++ & Ada Program
3014
3015 @noindent
3016 Usually the linker of the C++ development system must be used to link
3017 mixed applications because most C++ systems will resolve elaboration
3018 issues (such as calling constructors on global class instances)
3019 transparently during the link phase. GNAT has been adapted to ease the
3020 use of a foreign linker for the last phase. Three cases can be
3021 considered:
3022 @enumerate
3023
3024 @item
3025 Using GNAT and G++ (GNU C++ compiler) from the same GCC installation:
3026 The C++ linker can simply be called by using the C++ specific driver
3027 called @code{c++}. Note that this setup is not very common because it
3028 may involve recompiling the whole GCC tree from sources, which makes it
3029 harder to upgrade the compilation system for one language without
3030 destabilizing the other.
3031
3032 @smallexample
3033 $ c++ -c file1.C
3034 $ c++ -c file2.C
3035 $ gnatmake ada_unit -largs file1.o file2.o --LINK=c++
3036 @end smallexample
3037
3038 @item
3039 Using GNAT and G++ from two different GCC installations: If both
3040 compilers are on the @env{PATH}, the previous method may be used. It is
3041 important to note that environment variables such as
3042 @env{C_INCLUDE_PATH}, @env{GCC_EXEC_PREFIX}, @env{BINUTILS_ROOT}, and
3043 @env{GCC_ROOT} will affect both compilers
3044 at the same time and may make one of the two compilers operate
3045 improperly if set during invocation of the wrong compiler. It is also
3046 very important that the linker uses the proper @file{libgcc.a} GCC
3047 library -- that is, the one from the C++ compiler installation. The
3048 implicit link command as suggested in the @command{gnatmake} command
3049 from the former example can be replaced by an explicit link command with
3050 the full-verbosity option in order to verify which library is used:
3051 @smallexample
3052 $ gnatbind ada_unit
3053 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=c++
3054 @end smallexample
3055 If there is a problem due to interfering environment variables, it can
3056 be worked around by using an intermediate script. The following example
3057 shows the proper script to use when GNAT has not been installed at its
3058 default location and g++ has been installed at its default location:
3059
3060 @smallexample
3061 $ cat ./my_script
3062 #!/bin/sh
3063 unset BINUTILS_ROOT
3064 unset GCC_ROOT
3065 c++ $*
3066 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=./my_script
3067 @end smallexample
3068
3069 @item
3070 Using a non-GNU C++ compiler: The commands previously described can be
3071 used to insure that the C++ linker is used. Nonetheless, you need to add
3072 a few more parameters to the link command line, depending on the exception
3073 mechanism used.
3074
3075 If the @code{setjmp/longjmp} exception mechanism is used, only the paths
3076 to the libgcc libraries are required:
3077
3078 @smallexample
3079 $ cat ./my_script
3080 #!/bin/sh
3081 CC $* `gcc -print-file-name=libgcc.a` `gcc -print-file-name=libgcc_eh.a`
3082 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
3083 @end smallexample
3084
3085 Where CC is the name of the non-GNU C++ compiler.
3086
3087 If the @code{zero cost} exception mechanism is used, and the platform
3088 supports automatic registration of exception tables (e.g.@: Solaris or IRIX),
3089 paths to more objects are required:
3090
3091 @smallexample
3092 $ cat ./my_script
3093 #!/bin/sh
3094 CC `gcc -print-file-name=crtbegin.o` $* \
3095 `gcc -print-file-name=libgcc.a` `gcc -print-file-name=libgcc_eh.a` \
3096 `gcc -print-file-name=crtend.o`
3097 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
3098 @end smallexample
3099
3100 If the @code{zero cost} exception mechanism is used, and the platform
3101 doesn't support automatic registration of exception tables (e.g.@: HP-UX,
3102 Tru64 or AIX), the simple approach described above will not work and
3103 a pre-linking phase using GNAT will be necessary.
3104
3105 @end enumerate
3106
3107 @node A Simple Example
3108 @subsection A Simple Example
3109 @noindent
3110 The following example, provided as part of the GNAT examples, shows how
3111 to achieve procedural interfacing between Ada and C++ in both
3112 directions. The C++ class A has two methods. The first method is exported
3113 to Ada by the means of an extern C wrapper function. The second method
3114 calls an Ada subprogram. On the Ada side, The C++ calls are modelled by
3115 a limited record with a layout comparable to the C++ class. The Ada
3116 subprogram, in turn, calls the C++ method. So, starting from the C++
3117 main program, the process passes back and forth between the two
3118 languages.
3119
3120 @noindent
3121 Here are the compilation commands:
3122 @smallexample
3123 $ gnatmake -c simple_cpp_interface
3124 $ c++ -c cpp_main.C
3125 $ c++ -c ex7.C
3126 $ gnatbind -n simple_cpp_interface
3127 $ gnatlink simple_cpp_interface -o cpp_main --LINK=$(CPLUSPLUS)
3128 -lstdc++ ex7.o cpp_main.o
3129 @end smallexample
3130
3131 @noindent
3132 Here are the corresponding sources:
3133 @smallexample
3134
3135 //cpp_main.C
3136
3137 #include "ex7.h"
3138
3139 extern "C" @{
3140 void adainit (void);
3141 void adafinal (void);
3142 void method1 (A *t);
3143 @}
3144
3145 void method1 (A *t)
3146 @{
3147 t->method1 ();
3148 @}
3149
3150 int main ()
3151 @{
3152 A obj;
3153 adainit ();
3154 obj.method2 (3030);
3155 adafinal ();
3156 @}
3157
3158 //ex7.h
3159
3160 class Origin @{
3161 public:
3162 int o_value;
3163 @};
3164 class A : public Origin @{
3165 public:
3166 void method1 (void);
3167 void method2 (int v);
3168 A();
3169 int a_value;
3170 @};
3171
3172 //ex7.C
3173
3174 #include "ex7.h"
3175 #include <stdio.h>
3176
3177 extern "C" @{ void ada_method2 (A *t, int v);@}
3178
3179 void A::method1 (void)
3180 @{
3181 a_value = 2020;
3182 printf ("in A::method1, a_value = %d \n",a_value);
3183
3184 @}
3185
3186 void A::method2 (int v)
3187 @{
3188 ada_method2 (this, v);
3189 printf ("in A::method2, a_value = %d \n",a_value);
3190
3191 @}
3192
3193 A::A(void)
3194 @{
3195 a_value = 1010;
3196 printf ("in A::A, a_value = %d \n",a_value);
3197 @}
3198 @end smallexample
3199
3200 @smallexample @c ada
3201 -- Ada sources
3202 package body Simple_Cpp_Interface is
3203
3204 procedure Ada_Method2 (This : in out A; V : Integer) is
3205 begin
3206 Method1 (This);
3207 This.A_Value := V;
3208 end Ada_Method2;
3209
3210 end Simple_Cpp_Interface;
3211
3212 with System;
3213 package Simple_Cpp_Interface is
3214 type A is limited
3215 record
3216 Vptr : System.Address;
3217 O_Value : Integer;
3218 A_Value : Integer;
3219 end record;
3220 pragma Convention (C, A);
3221
3222 procedure Method1 (This : in out A);
3223 pragma Import (C, Method1);
3224
3225 procedure Ada_Method2 (This : in out A; V : Integer);
3226 pragma Export (C, Ada_Method2);
3227
3228 end Simple_Cpp_Interface;
3229 @end smallexample
3230
3231 @node Interfacing with C++ at the Class Level
3232 @subsection Interfacing with C++ at the Class Level
3233 @noindent
3234 In this section we demonstrate the GNAT features for interfacing with
3235 C++ by means of an example making use of Ada 2005 abstract interface
3236 types. This example consists of a classification of animals; classes
3237 have been used to model our main classification of animals, and
3238 interfaces provide support for the management of secondary
3239 classifications. We first demonstrate a case in which the types and
3240 constructors are defined on the C++ side and imported from the Ada
3241 side, and latter the reverse case.
3242
3243 The root of our derivation will be the @code{Animal} class, with a
3244 single private attribute (the @code{Age} of the animal) and two public
3245 primitives to set and get the value of this attribute.
3246
3247 @smallexample
3248 @b{class} Animal @{
3249 @b{public}:
3250 @b{virtual} void Set_Age (int New_Age);
3251 @b{virtual} int Age ();
3252 @b{private}:
3253 int Age_Count;
3254 @};
3255 @end smallexample
3256
3257 Abstract interface types are defined in C++ by means of classes with pure
3258 virtual functions and no data members. In our example we will use two
3259 interfaces that provide support for the common management of @code{Carnivore}
3260 and @code{Domestic} animals:
3261
3262 @smallexample
3263 @b{class} Carnivore @{
3264 @b{public}:
3265 @b{virtual} int Number_Of_Teeth () = 0;
3266 @};
3267
3268 @b{class} Domestic @{
3269 @b{public}:
3270 @b{virtual void} Set_Owner (char* Name) = 0;
3271 @};
3272 @end smallexample
3273
3274 Using these declarations, we can now say that a @code{Dog} is an animal that is
3275 both Carnivore and Domestic, that is:
3276
3277 @smallexample
3278 @b{class} Dog : Animal, Carnivore, Domestic @{
3279 @b{public}:
3280 @b{virtual} int Number_Of_Teeth ();
3281 @b{virtual} void Set_Owner (char* Name);
3282
3283 Dog(); // Constructor
3284 @b{private}:
3285 int Tooth_Count;
3286 char *Owner;
3287 @};
3288 @end smallexample
3289
3290 In the following examples we will assume that the previous declarations are
3291 located in a file named @code{animals.h}. The following package demonstrates
3292 how to import these C++ declarations from the Ada side:
3293
3294 @smallexample @c ada
3295 with Interfaces.C.Strings; use Interfaces.C.Strings;
3296 package Animals is
3297 type Carnivore is interface;
3298 pragma Convention (C_Plus_Plus, Carnivore);
3299 function Number_Of_Teeth (X : Carnivore)
3300 return Natural is abstract;
3301
3302 type Domestic is interface;
3303 pragma Convention (C_Plus_Plus, Set_Owner);
3304 procedure Set_Owner
3305 (X : in out Domestic;
3306 Name : Chars_Ptr) is abstract;
3307
3308 type Animal is tagged record
3309 Age : Natural := 0;
3310 end record;
3311 pragma Import (C_Plus_Plus, Animal);
3312
3313 procedure Set_Age (X : in out Animal; Age : Integer);
3314 pragma Import (C_Plus_Plus, Set_Age);
3315
3316 function Age (X : Animal) return Integer;
3317 pragma Import (C_Plus_Plus, Age);
3318
3319 type Dog is new Animal and Carnivore and Domestic with record
3320 Tooth_Count : Natural;
3321 Owner : String (1 .. 30);
3322 end record;
3323 pragma Import (C_Plus_Plus, Dog);
3324
3325 function Number_Of_Teeth (A : Dog) return Integer;
3326 pragma Import (C_Plus_Plus, Number_Of_Teeth);
3327
3328 procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
3329 pragma Import (C_Plus_Plus, Set_Owner);
3330
3331 function New_Dog return Dog'Class;
3332 pragma CPP_Constructor (New_Dog);
3333 pragma Import (CPP, New_Dog, "_ZN3DogC2Ev");
3334 end Animals;
3335 @end smallexample
3336
3337 Thanks to the compatibility between GNAT run-time structures and the C++ ABI,
3338 interfacing with these C++ classes is easy. The only requirement is that all
3339 the primitives and components must be declared exactly in the same order in
3340 the two languages.
3341
3342 Regarding the abstract interfaces, we must indicate to the GNAT compiler by
3343 means of a @code{pragma Convention (C_Plus_Plus)}, the convention used to pass
3344 the arguments to the called primitives will be the same as for C++. For the
3345 imported classes we use @code{pragma Import} with convention @code{C_Plus_Plus}
3346 to indicate that they have been defined on the C++ side; this is required
3347 because the dispatch table associated with these tagged types will be built
3348 in the C++ side and therefore will not contain the predefined Ada primitives
3349 which Ada would otherwise expect.
3350
3351 As the reader can see there is no need to indicate the C++ mangled names
3352 associated with each subprogram because it is assumed that all the calls to
3353 these primitives will be dispatching calls. The only exception is the
3354 constructor, which must be registered with the compiler by means of
3355 @code{pragma CPP_Constructor} and needs to provide its associated C++
3356 mangled name because the Ada compiler generates direct calls to it.
3357
3358 With the above packages we can now declare objects of type Dog on the Ada side
3359 and dispatch calls to the corresponding subprograms on the C++ side. We can
3360 also extend the tagged type Dog with further fields and primitives, and
3361 override some of its C++ primitives on the Ada side. For example, here we have
3362 a type derivation defined on the Ada side that inherits all the dispatching
3363 primitives of the ancestor from the C++ side.
3364
3365 @smallexample
3366 @b{with} Animals; @b{use} Animals;
3367 @b{package} Vaccinated_Animals @b{is}
3368 @b{type} Vaccinated_Dog @b{is new} Dog @b{with null record};
3369 @b{function} Vaccination_Expired (A : Vaccinated_Dog) @b{return} Boolean;
3370 @b{end} Vaccinated_Animals;
3371 @end smallexample
3372
3373 It is important to note that, because of the ABI compatibility, the programmer
3374 does not need to add any further information to indicate either the object
3375 layout or the dispatch table entry associated with each dispatching operation.
3376
3377 Now let us define all the types and constructors on the Ada side and export
3378 them to C++, using the same hierarchy of our previous example:
3379
3380 @smallexample @c ada
3381 with Interfaces.C.Strings;
3382 use Interfaces.C.Strings;
3383 package Animals is
3384 type Carnivore is interface;
3385 pragma Convention (C_Plus_Plus, Carnivore);
3386 function Number_Of_Teeth (X : Carnivore)
3387 return Natural is abstract;
3388
3389 type Domestic is interface;
3390 pragma Convention (C_Plus_Plus, Set_Owner);
3391 procedure Set_Owner
3392 (X : in out Domestic;
3393 Name : Chars_Ptr) is abstract;
3394
3395 type Animal is tagged record
3396 Age : Natural := 0;
3397 end record;
3398 pragma Convention (C_Plus_Plus, Animal);
3399
3400 procedure Set_Age (X : in out Animal; Age : Integer);
3401 pragma Export (C_Plus_Plus, Set_Age);
3402
3403 function Age (X : Animal) return Integer;
3404 pragma Export (C_Plus_Plus, Age);
3405
3406 type Dog is new Animal and Carnivore and Domestic with record
3407 Tooth_Count : Natural;
3408 Owner : String (1 .. 30);
3409 end record;
3410 pragma Convention (C_Plus_Plus, Dog);
3411
3412 function Number_Of_Teeth (A : Dog) return Integer;
3413 pragma Export (C_Plus_Plus, Number_Of_Teeth);
3414
3415 procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
3416 pragma Export (C_Plus_Plus, Set_Owner);
3417
3418 function New_Dog return Dog'Class;
3419 pragma Export (C_Plus_Plus, New_Dog);
3420 end Animals;
3421 @end smallexample
3422
3423 Compared with our previous example the only difference is the use of
3424 @code{pragma Export} to indicate to the GNAT compiler that the primitives will
3425 be available to C++. Thanks to the ABI compatibility, on the C++ side there is
3426 nothing else to be done; as explained above, the only requirement is that all
3427 the primitives and components are declared in exactly the same order.
3428
3429 For completeness, let us see a brief C++ main program that uses the
3430 declarations available in @code{animals.h} (presented in our first example) to
3431 import and use the declarations from the Ada side, properly initializing and
3432 finalizing the Ada run-time system along the way:
3433
3434 @smallexample
3435 @b{#include} "animals.h"
3436 @b{#include} <iostream>
3437 @b{using namespace} std;
3438
3439 void Check_Carnivore (Carnivore *obj) @{@dots{}@}
3440 void Check_Domestic (Domestic *obj) @{@dots{}@}
3441 void Check_Animal (Animal *obj) @{@dots{}@}
3442 void Check_Dog (Dog *obj) @{@dots{}@}
3443
3444 @b{extern} "C" @{
3445 void adainit (void);
3446 void adafinal (void);
3447 Dog* new_dog ();
3448 @}
3449
3450 void test ()
3451 @{
3452 Dog *obj = new_dog(); // Ada constructor
3453 Check_Carnivore (obj); // Check secondary DT
3454 Check_Domestic (obj); // Check secondary DT
3455 Check_Animal (obj); // Check primary DT
3456 Check_Dog (obj); // Check primary DT
3457 @}
3458
3459 int main ()
3460 @{
3461 adainit (); test(); adafinal ();
3462 return 0;
3463 @}
3464 @end smallexample
3465
3466 @node Comparison between GNAT and C/C++ Compilation Models
3467 @section Comparison between GNAT and C/C++ Compilation Models
3468
3469 @noindent
3470 The GNAT model of compilation is close to the C and C++ models. You can
3471 think of Ada specs as corresponding to header files in C. As in C, you
3472 don't need to compile specs; they are compiled when they are used. The
3473 Ada @code{with} is similar in effect to the @code{#include} of a C
3474 header.
3475
3476 One notable difference is that, in Ada, you may compile specs separately
3477 to check them for semantic and syntactic accuracy. This is not always
3478 possible with C headers because they are fragments of programs that have
3479 less specific syntactic or semantic rules.
3480
3481 The other major difference is the requirement for running the binder,
3482 which performs two important functions. First, it checks for
3483 consistency. In C or C++, the only defense against assembling
3484 inconsistent programs lies outside the compiler, in a makefile, for
3485 example. The binder satisfies the Ada requirement that it be impossible
3486 to construct an inconsistent program when the compiler is used in normal
3487 mode.
3488
3489 @cindex Elaboration order control
3490 The other important function of the binder is to deal with elaboration
3491 issues. There are also elaboration issues in C++ that are handled
3492 automatically. This automatic handling has the advantage of being
3493 simpler to use, but the C++ programmer has no control over elaboration.
3494 Where @code{gnatbind} might complain there was no valid order of
3495 elaboration, a C++ compiler would simply construct a program that
3496 malfunctioned at run time.
3497 @end ifclear
3498
3499 @node Comparison between GNAT and Conventional Ada Library Models
3500 @section Comparison between GNAT and Conventional Ada Library Models
3501
3502 @noindent
3503 This section is intended for Ada programmers who have
3504 used an Ada compiler implementing the traditional Ada library
3505 model, as described in the Ada Reference Manual.
3506
3507 @cindex GNAT library
3508 In GNAT, there is no ``library'' in the normal sense. Instead, the set of
3509 source files themselves acts as the library. Compiling Ada programs does
3510 not generate any centralized information, but rather an object file and
3511 a ALI file, which are of interest only to the binder and linker.
3512 In a traditional system, the compiler reads information not only from
3513 the source file being compiled, but also from the centralized library.
3514 This means that the effect of a compilation depends on what has been
3515 previously compiled. In particular:
3516
3517 @itemize @bullet
3518 @item
3519 When a unit is @code{with}'ed, the unit seen by the compiler corresponds
3520 to the version of the unit most recently compiled into the library.
3521
3522 @item
3523 Inlining is effective only if the necessary body has already been
3524 compiled into the library.
3525
3526 @item
3527 Compiling a unit may obsolete other units in the library.
3528 @end itemize
3529
3530 @noindent
3531 In GNAT, compiling one unit never affects the compilation of any other
3532 units because the compiler reads only source files. Only changes to source
3533 files can affect the results of a compilation. In particular:
3534
3535 @itemize @bullet
3536 @item
3537 When a unit is @code{with}'ed, the unit seen by the compiler corresponds
3538 to the source version of the unit that is currently accessible to the
3539 compiler.
3540
3541 @item
3542 @cindex Inlining
3543 Inlining requires the appropriate source files for the package or
3544 subprogram bodies to be available to the compiler. Inlining is always
3545 effective, independent of the order in which units are complied.
3546
3547 @item
3548 Compiling a unit never affects any other compilations. The editing of
3549 sources may cause previous compilations to be out of date if they
3550 depended on the source file being modified.
3551 @end itemize
3552
3553 @noindent
3554 The most important result of these differences is that order of compilation
3555 is never significant in GNAT. There is no situation in which one is
3556 required to do one compilation before another. What shows up as order of
3557 compilation requirements in the traditional Ada library becomes, in
3558 GNAT, simple source dependencies; in other words, there is only a set
3559 of rules saying what source files must be present when a file is
3560 compiled.
3561
3562 @ifset vms
3563 @node Placement of temporary files
3564 @section Placement of temporary files
3565 @cindex Temporary files (user control over placement)
3566
3567 @noindent
3568 GNAT creates temporary files in the directory designated by the environment
3569 variable @env{TMPDIR}.
3570 (See the HP @emph{C RTL Reference Manual} on the function @code{getenv()}
3571 for detailed information on how environment variables are resolved.
3572 For most users the easiest way to make use of this feature is to simply
3573 define @env{TMPDIR} as a job level logical name).
3574 For example, if you wish to use a Ramdisk (assuming DECRAM is installed)
3575 for compiler temporary files, then you can include something like the
3576 following command in your @file{LOGIN.COM} file:
3577
3578 @smallexample
3579 $ define/job TMPDIR "/disk$scratchram/000000/temp/"
3580 @end smallexample
3581
3582 @noindent
3583 If @env{TMPDIR} is not defined, then GNAT uses the directory designated by
3584 @env{TMP}; if @env{TMP} is not defined, then GNAT uses the directory
3585 designated by @env{TEMP}.
3586 If none of these environment variables are defined then GNAT uses the
3587 directory designated by the logical name @code{SYS$SCRATCH:}
3588 (by default the user's home directory). If all else fails
3589 GNAT uses the current directory for temporary files.
3590 @end ifset
3591
3592 @c *************************
3593 @node Compiling Using gcc
3594 @chapter Compiling Using @command{gcc}
3595
3596 @noindent
3597 This chapter discusses how to compile Ada programs using the @command{gcc}
3598 command. It also describes the set of switches
3599 that can be used to control the behavior of the compiler.
3600 @menu
3601 * Compiling Programs::
3602 * Switches for gcc::
3603 * Search Paths and the Run-Time Library (RTL)::
3604 * Order of Compilation Issues::
3605 * Examples::
3606 @end menu
3607
3608 @node Compiling Programs
3609 @section Compiling Programs
3610
3611 @noindent
3612 The first step in creating an executable program is to compile the units
3613 of the program using the @command{gcc} command. You must compile the
3614 following files:
3615
3616 @itemize @bullet
3617 @item
3618 the body file (@file{.adb}) for a library level subprogram or generic
3619 subprogram
3620
3621 @item
3622 the spec file (@file{.ads}) for a library level package or generic
3623 package that has no body
3624
3625 @item
3626 the body file (@file{.adb}) for a library level package
3627 or generic package that has a body
3628
3629 @end itemize
3630
3631 @noindent
3632 You need @emph{not} compile the following files
3633
3634 @itemize @bullet
3635
3636 @item
3637 the spec of a library unit which has a body
3638
3639 @item
3640 subunits
3641 @end itemize
3642
3643 @noindent
3644 because they are compiled as part of compiling related units. GNAT
3645 package specs
3646 when the corresponding body is compiled, and subunits when the parent is
3647 compiled.
3648
3649 @cindex cannot generate code
3650 If you attempt to compile any of these files, you will get one of the
3651 following error messages (where @var{fff} is the name of the file you compiled):
3652
3653 @smallexample
3654 cannot generate code for file @var{fff} (package spec)
3655 to check package spec, use -gnatc
3656
3657 cannot generate code for file @var{fff} (missing subunits)
3658 to check parent unit, use -gnatc
3659
3660 cannot generate code for file @var{fff} (subprogram spec)
3661 to check subprogram spec, use -gnatc
3662
3663 cannot generate code for file @var{fff} (subunit)
3664 to check subunit, use -gnatc
3665 @end smallexample
3666
3667 @noindent
3668 As indicated by the above error messages, if you want to submit
3669 one of these files to the compiler to check for correct semantics
3670 without generating code, then use the @option{-gnatc} switch.
3671
3672 The basic command for compiling a file containing an Ada unit is
3673
3674 @smallexample
3675 $ gcc -c @ovar{switches} @file{file name}
3676 @end smallexample
3677
3678 @noindent
3679 where @var{file name} is the name of the Ada file (usually
3680 having an extension
3681 @file{.ads} for a spec or @file{.adb} for a body).
3682 @ifclear vms
3683 You specify the
3684 @option{-c} switch to tell @command{gcc} to compile, but not link, the file.
3685 @end ifclear
3686 The result of a successful compilation is an object file, which has the
3687 same name as the source file but an extension of @file{.o} and an Ada
3688 Library Information (ALI) file, which also has the same name as the
3689 source file, but with @file{.ali} as the extension. GNAT creates these
3690 two output files in the current directory, but you may specify a source
3691 file in any directory using an absolute or relative path specification
3692 containing the directory information.
3693
3694 @findex gnat1
3695 @command{gcc} is actually a driver program that looks at the extensions of
3696 the file arguments and loads the appropriate compiler. For example, the
3697 GNU C compiler is @file{cc1}, and the Ada compiler is @file{gnat1}.
3698 These programs are in directories known to the driver program (in some
3699 configurations via environment variables you set), but need not be in
3700 your path. The @command{gcc} driver also calls the assembler and any other
3701 utilities needed to complete the generation of the required object
3702 files.
3703
3704 It is possible to supply several file names on the same @command{gcc}
3705 command. This causes @command{gcc} to call the appropriate compiler for
3706 each file. For example, the following command lists three separate
3707 files to be compiled:
3708
3709 @smallexample
3710 $ gcc -c x.adb y.adb z.c
3711 @end smallexample
3712
3713 @noindent
3714 calls @code{gnat1} (the Ada compiler) twice to compile @file{x.adb} and
3715 @file{y.adb}, and @code{cc1} (the C compiler) once to compile @file{z.c}.
3716 The compiler generates three object files @file{x.o}, @file{y.o} and
3717 @file{z.o} and the two ALI files @file{x.ali} and @file{y.ali} from the
3718 Ada compilations. Any switches apply to all the files ^listed,^listed.^
3719 @ifclear vms
3720 except for
3721 @option{-gnat@var{x}} switches, which apply only to Ada compilations.
3722 @end ifclear
3723
3724 @node Switches for gcc
3725 @section Switches for @command{gcc}
3726
3727 @noindent
3728 The @command{gcc} command accepts switches that control the
3729 compilation process. These switches are fully described in this section.
3730 First we briefly list all the switches, in alphabetical order, then we
3731 describe the switches in more detail in functionally grouped sections.
3732
3733 More switches exist for GCC than those documented here, especially
3734 for specific targets. However, their use is not recommended as
3735 they may change code generation in ways that are incompatible with
3736 the Ada run-time library, or can cause inconsistencies between
3737 compilation units.
3738
3739 @menu
3740 * Output and Error Message Control::
3741 * Warning Message Control::
3742 * Debugging and Assertion Control::
3743 * Validity Checking::
3744 * Style Checking::
3745 * Run-Time Checks::
3746 * Using gcc for Syntax Checking::
3747 * Using gcc for Semantic Checking::
3748 * Compiling Different Versions of Ada::
3749 * Character Set Control::
3750 * File Naming Control::
3751 * Subprogram Inlining Control::
3752 * Auxiliary Output Control::
3753 * Debugging Control::
3754 * Exception Handling Control::
3755 * Units to Sources Mapping Files::
3756 * Integrated Preprocessing::
3757 * Code Generation Control::
3758 @ifset vms
3759 * Return Codes::
3760 @end ifset
3761 @end menu
3762
3763 @table @option
3764 @c !sort!
3765 @ifclear vms
3766 @cindex @option{-b} (@command{gcc})
3767 @item -b @var{target}
3768 Compile your program to run on @var{target}, which is the name of a
3769 system configuration. You must have a GNAT cross-compiler built if
3770 @var{target} is not the same as your host system.
3771
3772 @item -B@var{dir}
3773 @cindex @option{-B} (@command{gcc})
3774 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
3775 from @var{dir} instead of the default location. Only use this switch
3776 when multiple versions of the GNAT compiler are available.
3777 @xref{Directory Options,, Options for Directory Search, gcc, Using the
3778 GNU Compiler Collection (GCC)}, for further details. You would normally
3779 use the @option{-b} or @option{-V} switch instead.
3780
3781 @item -c
3782 @cindex @option{-c} (@command{gcc})
3783 Compile. Always use this switch when compiling Ada programs.
3784
3785 Note: for some other languages when using @command{gcc}, notably in
3786 the case of C and C++, it is possible to use
3787 use @command{gcc} without a @option{-c} switch to
3788 compile and link in one step. In the case of GNAT, you
3789 cannot use this approach, because the binder must be run
3790 and @command{gcc} cannot be used to run the GNAT binder.
3791 @end ifclear
3792
3793 @item -fno-inline
3794 @cindex @option{-fno-inline} (@command{gcc})
3795 Suppresses all back-end inlining, even if other optimization or inlining
3796 switches are set.
3797 This includes suppression of inlining that results
3798 from the use of the pragma @code{Inline_Always}.
3799 Any occurrences of pragma @code{Inline} or @code{Inline_Always}
3800 are ignored, and @option{-gnatn} and @option{-gnatN} have no
3801 effect if this switch is present.
3802
3803 @item -fno-inline-functions
3804 @cindex @option{-fno-inline-functions} (@command{gcc})
3805 Suppresses automatic inlining of simple subprograms, which is enabled
3806 if @option{-O3} is used.
3807
3808 @item -fno-inline-small-functions
3809 @cindex @option{-fno-inline-small-functions} (@command{gcc})
3810 Suppresses automatic inlining of small subprograms, which is enabled
3811 if @option{-O2} is used.
3812
3813 @item -fno-inline-functions-called-once
3814 @cindex @option{-fno-inline-functions-called-once} (@command{gcc})
3815 Suppresses inlining of subprograms local to the unit and called once
3816 from within it, which is enabled if @option{-O1} is used.
3817
3818 @item -fno-ivopts
3819 @cindex @option{-fno-ivopts} (@command{gcc})
3820 Suppresses high-level loop induction variable optimizations, which are
3821 enabled if @option{-O1} is used. These optimizations are generally
3822 profitable but, for some specific cases of loops with numerous uses
3823 of the iteration variable that follow a common pattern, they may end
3824 up destroying the regularity that could be exploited at a lower level
3825 and thus producing inferior code.
3826
3827 @item -fno-strict-aliasing
3828 @cindex @option{-fno-strict-aliasing} (@command{gcc})
3829 Causes the compiler to avoid assumptions regarding non-aliasing
3830 of objects of different types. See
3831 @ref{Optimization and Strict Aliasing} for details.
3832
3833 @item -fstack-check
3834 @cindex @option{-fstack-check} (@command{gcc})
3835 Activates stack checking.
3836 See @ref{Stack Overflow Checking} for details.
3837
3838 @item -fstack-usage
3839 @cindex @option{-fstack-usage} (@command{gcc})
3840 Makes the compiler output stack usage information for the program, on a
3841 per-function basis. See @ref{Static Stack Usage Analysis} for details.
3842
3843 @item -fcallgraph-info@r{[}=su@r{]}
3844 @cindex @option{-fcallgraph-info} (@command{gcc})
3845 Makes the compiler output callgraph information for the program, on a
3846 per-file basis. The information is generated in the VCG format. It can
3847 be decorated with stack-usage per-node information.
3848
3849 @item ^-g^/DEBUG^
3850 @cindex @option{^-g^/DEBUG^} (@command{gcc})
3851 Generate debugging information. This information is stored in the object
3852 file and copied from there to the final executable file by the linker,
3853 where it can be read by the debugger. You must use the
3854 @option{^-g^/DEBUG^} switch if you plan on using the debugger.
3855
3856 @item -gnat83
3857 @cindex @option{-gnat83} (@command{gcc})
3858 Enforce Ada 83 restrictions.
3859
3860 @item -gnat95
3861 @cindex @option{-gnat95} (@command{gcc})
3862 Enforce Ada 95 restrictions.
3863
3864 @item -gnat05
3865 @cindex @option{-gnat05} (@command{gcc})
3866 Allow full Ada 2005 features.
3867
3868 @item -gnata
3869 @cindex @option{-gnata} (@command{gcc})
3870 Assertions enabled. @code{Pragma Assert} and @code{pragma Debug} to be
3871 activated. Note that these pragmas can also be controlled using the
3872 configuration pragmas @code{Assertion_Policy} and @code{Debug_Policy}.
3873 It also activates pragmas @code{Check}, @code{Precondition}, and
3874 @code{Postcondition}. Note that these pragmas can also be controlled
3875 using the configuration pragma @code{Check_Policy}.
3876
3877 @item -gnatA
3878 @cindex @option{-gnatA} (@command{gcc})
3879 Avoid processing @file{gnat.adc}. If a @file{gnat.adc} file is present,
3880 it will be ignored.
3881
3882 @item -gnatb
3883 @cindex @option{-gnatb} (@command{gcc})
3884 Generate brief messages to @file{stderr} even if verbose mode set.
3885
3886 @item -gnatB
3887 @cindex @option{-gnatB} (@command{gcc})
3888 Assume no invalid (bad) values except for 'Valid attribute use.
3889
3890 @item -gnatc
3891 @cindex @option{-gnatc} (@command{gcc})
3892 Check syntax and semantics only (no code generation attempted).
3893
3894 @item -gnatd
3895 @cindex @option{-gnatd} (@command{gcc})
3896 Specify debug options for the compiler. The string of characters after
3897 the @option{-gnatd} specify the specific debug options. The possible
3898 characters are 0-9, a-z, A-Z, optionally preceded by a dot. See
3899 compiler source file @file{debug.adb} for details of the implemented
3900 debug options. Certain debug options are relevant to applications
3901 programmers, and these are documented at appropriate points in this
3902 users guide.
3903
3904 @ifclear vms
3905 @item -gnatD
3906 @cindex @option{-gnatD[nn]} (@command{gcc})
3907 @end ifclear
3908 @ifset vms
3909 @item /XDEBUG /LXDEBUG=nnn
3910 @end ifset
3911 Create expanded source files for source level debugging. This switch
3912 also suppress generation of cross-reference information
3913 (see @option{-gnatx}).
3914
3915 @item -gnatec=@var{path}
3916 @cindex @option{-gnatec} (@command{gcc})
3917 Specify a configuration pragma file
3918 @ifclear vms
3919 (the equal sign is optional)
3920 @end ifclear
3921 (@pxref{The Configuration Pragmas Files}).
3922
3923 @item ^-gnateD^/DATA_PREPROCESSING=^symbol@r{[}=@var{value}@r{]}
3924 @cindex @option{-gnateD} (@command{gcc})
3925 Defines a symbol, associated with @var{value}, for preprocessing.
3926 (@pxref{Integrated Preprocessing}).
3927
3928 @item -gnatef
3929 @cindex @option{-gnatef} (@command{gcc})
3930 Display full source path name in brief error messages.
3931
3932 @item -gnateG
3933 @cindex @option{-gnateG} (@command{gcc})
3934 Save result of preprocessing in a text file.
3935
3936 @item -gnatem=@var{path}
3937 @cindex @option{-gnatem} (@command{gcc})
3938 Specify a mapping file
3939 @ifclear vms
3940 (the equal sign is optional)
3941 @end ifclear
3942 (@pxref{Units to Sources Mapping Files}).
3943
3944 @item -gnatep=@var{file}
3945 @cindex @option{-gnatep} (@command{gcc})
3946 Specify a preprocessing data file
3947 @ifclear vms
3948 (the equal sign is optional)
3949 @end ifclear
3950 (@pxref{Integrated Preprocessing}).
3951
3952 @item -gnatE
3953 @cindex @option{-gnatE} (@command{gcc})
3954 Full dynamic elaboration checks.
3955
3956 @item -gnatf
3957 @cindex @option{-gnatf} (@command{gcc})
3958 Full errors. Multiple errors per line, all undefined references, do not
3959 attempt to suppress cascaded errors.
3960
3961 @item -gnatF
3962 @cindex @option{-gnatF} (@command{gcc})
3963 Externals names are folded to all uppercase.
3964
3965 @item ^-gnatg^/GNAT_INTERNAL^
3966 @cindex @option{^-gnatg^/GNAT_INTERNAL^} (@command{gcc})
3967 Internal GNAT implementation mode. This should not be used for
3968 applications programs, it is intended only for use by the compiler
3969 and its run-time library. For documentation, see the GNAT sources.
3970 Note that @option{^-gnatg^/GNAT_INTERNAL^} implies
3971 @option{^-gnatwae^/WARNINGS=ALL,ERRORS^} and
3972 @option{^-gnatyg^/STYLE_CHECKS=GNAT^}
3973 so that all standard warnings and all standard style options are turned on.
3974 All warnings and style error messages are treated as errors.
3975
3976 @ifclear vms
3977 @item -gnatG=nn
3978 @cindex @option{-gnatG[nn]} (@command{gcc})
3979 @end ifclear
3980 @ifset vms
3981 @item /EXPAND_SOURCE, /LEXPAND_SOURCE=nnn
3982 @end ifset
3983 List generated expanded code in source form.
3984
3985 @item ^-gnath^/HELP^
3986 @cindex @option{^-gnath^/HELP^} (@command{gcc})
3987 Output usage information. The output is written to @file{stdout}.
3988
3989 @item ^-gnati^/IDENTIFIER_CHARACTER_SET=^@var{c}
3990 @cindex @option{^-gnati^/IDENTIFIER_CHARACTER_SET^} (@command{gcc})
3991 Identifier character set
3992 @ifclear vms
3993 (@var{c}=1/2/3/4/8/9/p/f/n/w).
3994 @end ifclear
3995 For details of the possible selections for @var{c},
3996 see @ref{Character Set Control}.
3997
3998 @item ^-gnatI^/IGNORE_REP_CLAUSES^
3999 @cindex @option{^-gnatI^IGNORE_REP_CLAUSES^} (@command{gcc})
4000 Ignore representation clauses. When this switch is used, all
4001 representation clauses are treated as comments. This is useful
4002 when initially porting code where you want to ignore rep clause
4003 problems, and also for compiling foreign code (particularly
4004 for use with ASIS).
4005
4006 @item -gnatjnn
4007 @cindex @option{-gnatjnn} (@command{gcc})
4008 Reformat error messages to fit on nn character lines
4009
4010 @item -gnatk=@var{n}
4011 @cindex @option{-gnatk} (@command{gcc})
4012 Limit file names to @var{n} (1-999) characters ^(@code{k} = krunch)^^.
4013
4014 @item -gnatl
4015 @cindex @option{-gnatl} (@command{gcc})
4016 Output full source listing with embedded error messages.
4017
4018 @item -gnatL
4019 @cindex @option{-gnatL} (@command{gcc})
4020 Used in conjunction with -gnatG or -gnatD to intersperse original
4021 source lines (as comment lines with line numbers) in the expanded
4022 source output.
4023
4024 @item -gnatm=@var{n}
4025 @cindex @option{-gnatm} (@command{gcc})
4026 Limit number of detected error or warning messages to @var{n}
4027 where @var{n} is in the range 1..999999. The default setting if
4028 no switch is given is 9999. If the number of warnings reaches this
4029 limit, then a message is output and further warnings are suppressed,
4030 but the compilation is continued. If the number of error messages
4031 reaches this limit, then a message is output and the compilation
4032 is abandoned. The equal sign here is optional. A value of zero
4033 means that no limit applies.
4034
4035 @item -gnatn
4036 @cindex @option{-gnatn} (@command{gcc})
4037 Activate inlining for subprograms for which
4038 pragma @code{inline} is specified. This inlining is performed
4039 by the GCC back-end.
4040
4041 @item -gnatN
4042 @cindex @option{-gnatN} (@command{gcc})
4043 Activate front end inlining for subprograms for which
4044 pragma @code{Inline} is specified. This inlining is performed
4045 by the front end and will be visible in the
4046 @option{-gnatG} output.
4047
4048 When using a gcc-based back end (in practice this means using any version
4049 of GNAT other than the JGNAT, .NET or GNAAMP versions), then the use of
4050 @option{-gnatN} is deprecated, and the use of @option{-gnatn} is preferred.
4051 Historically front end inlining was more extensive than the gcc back end
4052 inlining, but that is no longer the case.
4053
4054 @item -gnato
4055 @cindex @option{-gnato} (@command{gcc})
4056 Enable numeric overflow checking (which is not normally enabled by
4057 default). Note that division by zero is a separate check that is not
4058 controlled by this switch (division by zero checking is on by default).
4059
4060 @item -gnatp
4061 @cindex @option{-gnatp} (@command{gcc})
4062 Suppress all checks. See @ref{Run-Time Checks} for details.
4063
4064 @item -gnatP
4065 @cindex @option{-gnatP} (@command{gcc})
4066 Enable polling. This is required on some systems (notably Windows NT) to
4067 obtain asynchronous abort and asynchronous transfer of control capability.
4068 @xref{Pragma Polling,,, gnat_rm, GNAT Reference Manual}, for full
4069 details.
4070
4071 @item -gnatq
4072 @cindex @option{-gnatq} (@command{gcc})
4073 Don't quit. Try semantics, even if parse errors.
4074
4075 @item -gnatQ
4076 @cindex @option{-gnatQ} (@command{gcc})
4077 Don't quit. Generate @file{ALI} and tree files even if illegalities.
4078
4079 @item -gnatr
4080 @cindex @option{-gnatr} (@command{gcc})
4081 Treat pragma Restrictions as Restriction_Warnings.
4082
4083 @item ^-gnatR@r{[}0@r{/}1@r{/}2@r{/}3@r{[}s@r{]]}^/REPRESENTATION_INFO^
4084 @cindex @option{-gnatR} (@command{gcc})
4085 Output representation information for declared types and objects.
4086
4087 @item -gnats
4088 @cindex @option{-gnats} (@command{gcc})
4089 Syntax check only.
4090
4091 @item -gnatS
4092 @cindex @option{-gnatS} (@command{gcc})
4093 Print package Standard.
4094
4095 @item -gnatt
4096 @cindex @option{-gnatt} (@command{gcc})
4097 Generate tree output file.
4098
4099 @item ^-gnatT^/TABLE_MULTIPLIER=^@var{nnn}
4100 @cindex @option{^-gnatT^/TABLE_MULTIPLIER^} (@command{gcc})
4101 All compiler tables start at @var{nnn} times usual starting size.
4102
4103 @item -gnatu
4104 @cindex @option{-gnatu} (@command{gcc})
4105 List units for this compilation.
4106
4107 @item -gnatU
4108 @cindex @option{-gnatU} (@command{gcc})
4109 Tag all error messages with the unique string ``error:''
4110
4111 @item -gnatv
4112 @cindex @option{-gnatv} (@command{gcc})
4113 Verbose mode. Full error output with source lines to @file{stdout}.
4114
4115 @item -gnatV
4116 @cindex @option{-gnatV} (@command{gcc})
4117 Control level of validity checking. See separate section describing
4118 this feature.
4119
4120 @item ^-gnatw@var{xxx}^/WARNINGS=(@var{option}@r{[},@dots{}@r{]})^
4121 @cindex @option{^-gnatw^/WARNINGS^} (@command{gcc})
4122 Warning mode where
4123 ^@var{xxx} is a string of option letters that^the list of options^ denotes
4124 the exact warnings that
4125 are enabled or disabled (@pxref{Warning Message Control}).
4126
4127 @item ^-gnatW^/WIDE_CHARACTER_ENCODING=^@var{e}
4128 @cindex @option{^-gnatW^/WIDE_CHARACTER_ENCODING^} (@command{gcc})
4129 Wide character encoding method
4130 @ifclear vms
4131 (@var{e}=n/h/u/s/e/8).
4132 @end ifclear
4133 @ifset vms
4134 (@var{e}=@code{BRACKETS, NONE, HEX, UPPER, SHIFT_JIS, EUC, UTF8})
4135 @end ifset
4136
4137 @item -gnatx
4138 @cindex @option{-gnatx} (@command{gcc})
4139 Suppress generation of cross-reference information.
4140
4141 @item ^-gnaty^/STYLE_CHECKS=(option,option@dots{})^
4142 @cindex @option{^-gnaty^/STYLE_CHECKS^} (@command{gcc})
4143 Enable built-in style checks (@pxref{Style Checking}).
4144
4145 @item ^-gnatz^/DISTRIBUTION_STUBS=^@var{m}
4146 @cindex @option{^-gnatz^/DISTRIBUTION_STUBS^} (@command{gcc})
4147 Distribution stub generation and compilation
4148 @ifclear vms
4149 (@var{m}=r/c for receiver/caller stubs).
4150 @end ifclear
4151 @ifset vms
4152 (@var{m}=@code{RECEIVER} or @code{CALLER} to specify the type of stubs
4153 to be generated and compiled).
4154 @end ifset
4155
4156 @item ^-I^/SEARCH=^@var{dir}
4157 @cindex @option{^-I^/SEARCH^} (@command{gcc})
4158 @cindex RTL
4159 Direct GNAT to search the @var{dir} directory for source files needed by
4160 the current compilation
4161 (@pxref{Search Paths and the Run-Time Library (RTL)}).
4162
4163 @item ^-I-^/NOCURRENT_DIRECTORY^
4164 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gcc})
4165 @cindex RTL
4166 Except for the source file named in the command line, do not look for source
4167 files in the directory containing the source file named in the command line
4168 (@pxref{Search Paths and the Run-Time Library (RTL)}).
4169
4170 @ifclear vms
4171 @item -mbig-switch
4172 @cindex @option{-mbig-switch} (@command{gcc})
4173 @cindex @code{case} statement (effect of @option{-mbig-switch} option)
4174 This standard gcc switch causes the compiler to use larger offsets in its
4175 jump table representation for @code{case} statements.
4176 This may result in less efficient code, but is sometimes necessary
4177 (for example on HP-UX targets)
4178 @cindex HP-UX and @option{-mbig-switch} option
4179 in order to compile large and/or nested @code{case} statements.
4180
4181 @item -o @var{file}
4182 @cindex @option{-o} (@command{gcc})
4183 This switch is used in @command{gcc} to redirect the generated object file
4184 and its associated ALI file. Beware of this switch with GNAT, because it may
4185 cause the object file and ALI file to have different names which in turn
4186 may confuse the binder and the linker.
4187 @end ifclear
4188
4189 @item -nostdinc
4190 @cindex @option{-nostdinc} (@command{gcc})
4191 Inhibit the search of the default location for the GNAT Run Time
4192 Library (RTL) source files.
4193
4194 @item -nostdlib
4195 @cindex @option{-nostdlib} (@command{gcc})
4196 Inhibit the search of the default location for the GNAT Run Time
4197 Library (RTL) ALI files.
4198
4199 @ifclear vms
4200 @item -O@ovar{n}
4201 @cindex @option{-O} (@command{gcc})
4202 @var{n} controls the optimization level.
4203
4204 @table @asis
4205 @item n = 0
4206 No optimization, the default setting if no @option{-O} appears
4207
4208 @item n = 1
4209 Normal optimization, the default if you specify @option{-O} without
4210 an operand. A good compromise between code quality and compilation
4211 time.
4212
4213 @item n = 2
4214 Extensive optimization, may improve execution time, possibly at the cost of
4215 substantially increased compilation time.
4216
4217 @item n = 3
4218 Same as @option{-O2}, and also includes inline expansion for small subprograms
4219 in the same unit.
4220
4221 @item n = s
4222 Optimize space usage
4223 @end table
4224
4225 @noindent
4226 See also @ref{Optimization Levels}.
4227 @end ifclear
4228
4229 @ifset vms
4230 @item /NOOPTIMIZE
4231 @cindex @option{/NOOPTIMIZE} (@code{GNAT COMPILE})
4232 Equivalent to @option{/OPTIMIZE=NONE}.
4233 This is the default behavior in the absence of an @option{/OPTIMIZE}
4234 qualifier.
4235
4236 @item /OPTIMIZE@r{[}=(keyword@r{[},@dots{}@r{]})@r{]}
4237 @cindex @option{/OPTIMIZE} (@code{GNAT COMPILE})
4238 Selects the level of optimization for your program. The supported
4239 keywords are as follows:
4240 @table @code
4241 @item ALL
4242 Perform most optimizations, including those that
4243 are expensive.
4244 This is the default if the @option{/OPTIMIZE} qualifier is supplied
4245 without keyword options.
4246
4247 @item NONE
4248 Do not do any optimizations. Same as @code{/NOOPTIMIZE}.
4249
4250 @item SOME
4251 Perform some optimizations, but omit ones that are costly.
4252
4253 @item DEVELOPMENT
4254 Same as @code{SOME}.
4255
4256 @item INLINING
4257 Full optimization as in @option{/OPTIMIZE=ALL}, and also attempts
4258 automatic inlining of small subprograms within a unit
4259
4260 @item UNROLL_LOOPS
4261 Try to unroll loops. This keyword may be specified together with
4262 any keyword above other than @code{NONE}. Loop unrolling
4263 usually, but not always, improves the performance of programs.
4264
4265 @item SPACE
4266 Optimize space usage
4267 @end table
4268
4269 @noindent
4270 See also @ref{Optimization Levels}.
4271 @end ifset
4272
4273 @ifclear vms
4274 @item -pass-exit-codes
4275 @cindex @option{-pass-exit-codes} (@command{gcc})
4276 Catch exit codes from the compiler and use the most meaningful as
4277 exit status.
4278 @end ifclear
4279
4280 @item --RTS=@var{rts-path}
4281 @cindex @option{--RTS} (@command{gcc})
4282 Specifies the default location of the runtime library. Same meaning as the
4283 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
4284
4285 @item ^-S^/ASM^
4286 @cindex @option{^-S^/ASM^} (@command{gcc})
4287 ^Used in place of @option{-c} to^Used to^
4288 cause the assembler source file to be
4289 generated, using @file{^.s^.S^} as the extension,
4290 instead of the object file.
4291 This may be useful if you need to examine the generated assembly code.
4292
4293 @item ^-fverbose-asm^/VERBOSE_ASM^
4294 @cindex @option{^-fverbose-asm^/VERBOSE_ASM^} (@command{gcc})
4295 ^Used in conjunction with @option{-S}^Used in place of @option{/ASM}^
4296 to cause the generated assembly code file to be annotated with variable
4297 names, making it significantly easier to follow.
4298
4299 @item ^-v^/VERBOSE^
4300 @cindex @option{^-v^/VERBOSE^} (@command{gcc})
4301 Show commands generated by the @command{gcc} driver. Normally used only for
4302 debugging purposes or if you need to be sure what version of the
4303 compiler you are executing.
4304
4305 @ifclear vms
4306 @item -V @var{ver}
4307 @cindex @option{-V} (@command{gcc})
4308 Execute @var{ver} version of the compiler. This is the @command{gcc}
4309 version, not the GNAT version.
4310 @end ifclear
4311
4312 @item ^-w^/NO_BACK_END_WARNINGS^
4313 @cindex @option{-w} (@command{gcc})
4314 Turn off warnings generated by the back end of the compiler. Use of
4315 this switch also causes the default for front end warnings to be set
4316 to suppress (as though @option{-gnatws} had appeared at the start of
4317 the options).
4318
4319 @end table
4320
4321 @ifclear vms
4322 @c Combining qualifiers does not work on VMS
4323 You may combine a sequence of GNAT switches into a single switch. For
4324 example, the combined switch
4325
4326 @cindex Combining GNAT switches
4327 @smallexample
4328 -gnatofi3
4329 @end smallexample
4330
4331 @noindent
4332 is equivalent to specifying the following sequence of switches:
4333
4334 @smallexample
4335 -gnato -gnatf -gnati3
4336 @end smallexample
4337 @end ifclear
4338
4339 @noindent
4340 The following restrictions apply to the combination of switches
4341 in this manner:
4342
4343 @itemize @bullet
4344 @item
4345 The switch @option{-gnatc} if combined with other switches must come
4346 first in the string.
4347
4348 @item
4349 The switch @option{-gnats} if combined with other switches must come
4350 first in the string.
4351
4352 @item
4353 The switches
4354 @option{^-gnatz^/DISTRIBUTION_STUBS^}, @option{-gnatzc}, and @option{-gnatzr}
4355 may not be combined with any other switches.
4356
4357 @ifclear vms
4358 @item
4359 Once a ``y'' appears in the string (that is a use of the @option{-gnaty}
4360 switch), then all further characters in the switch are interpreted
4361 as style modifiers (see description of @option{-gnaty}).
4362
4363 @item
4364 Once a ``d'' appears in the string (that is a use of the @option{-gnatd}
4365 switch), then all further characters in the switch are interpreted
4366 as debug flags (see description of @option{-gnatd}).
4367
4368 @item
4369 Once a ``w'' appears in the string (that is a use of the @option{-gnatw}
4370 switch), then all further characters in the switch are interpreted
4371 as warning mode modifiers (see description of @option{-gnatw}).
4372
4373 @item
4374 Once a ``V'' appears in the string (that is a use of the @option{-gnatV}
4375 switch), then all further characters in the switch are interpreted
4376 as validity checking options (see description of @option{-gnatV}).
4377 @end ifclear
4378 @end itemize
4379
4380 @node Output and Error Message Control
4381 @subsection Output and Error Message Control
4382 @findex stderr
4383
4384 @noindent
4385 The standard default format for error messages is called ``brief format''.
4386 Brief format messages are written to @file{stderr} (the standard error
4387 file) and have the following form:
4388
4389 @smallexample
4390 e.adb:3:04: Incorrect spelling of keyword "function"
4391 e.adb:4:20: ";" should be "is"
4392 @end smallexample
4393
4394 @noindent
4395 The first integer after the file name is the line number in the file,
4396 and the second integer is the column number within the line.
4397 @ifclear vms
4398 @code{GPS} can parse the error messages
4399 and point to the referenced character.
4400 @end ifclear
4401 The following switches provide control over the error message
4402 format:
4403
4404 @table @option
4405 @c !sort!
4406 @item -gnatv
4407 @cindex @option{-gnatv} (@command{gcc})
4408 @findex stdout
4409 @ifclear vms
4410 The v stands for verbose.
4411 @end ifclear
4412 The effect of this setting is to write long-format error
4413 messages to @file{stdout} (the standard output file.
4414 The same program compiled with the
4415 @option{-gnatv} switch would generate:
4416
4417 @smallexample
4418 @cartouche
4419 3. funcion X (Q : Integer)
4420 |
4421 >>> Incorrect spelling of keyword "function"
4422 4. return Integer;
4423 |
4424 >>> ";" should be "is"
4425 @end cartouche
4426 @end smallexample
4427
4428 @noindent
4429 The vertical bar indicates the location of the error, and the @samp{>>>}
4430 prefix can be used to search for error messages. When this switch is
4431 used the only source lines output are those with errors.
4432
4433 @item -gnatl
4434 @cindex @option{-gnatl} (@command{gcc})
4435 @ifclear vms
4436 The @code{l} stands for list.
4437 @end ifclear
4438 This switch causes a full listing of
4439 the file to be generated. In the case where a body is
4440 compiled, the corresponding spec is also listed, along
4441 with any subunits. Typical output from compiling a package
4442 body @file{p.adb} might look like:
4443
4444 @smallexample @c ada
4445 @cartouche
4446 Compiling: p.adb
4447
4448 1. package body p is
4449 2. procedure a;
4450 3. procedure a is separate;
4451 4. begin
4452 5. null
4453 |
4454 >>> missing ";"
4455
4456 6. end;
4457
4458 Compiling: p.ads
4459
4460 1. package p is
4461 2. pragma Elaborate_Body
4462 |
4463 >>> missing ";"
4464
4465 3. end p;
4466
4467 Compiling: p-a.adb
4468
4469 1. separate p
4470 |
4471 >>> missing "("
4472
4473 2. procedure a is
4474 3. begin
4475 4. null
4476 |
4477 >>> missing ";"
4478
4479 5. end;
4480 @end cartouche
4481 @end smallexample
4482
4483 @noindent
4484 @findex stderr
4485 When you specify the @option{-gnatv} or @option{-gnatl} switches and
4486 standard output is redirected, a brief summary is written to
4487 @file{stderr} (standard error) giving the number of error messages and
4488 warning messages generated.
4489
4490 @item -^gnatl^OUTPUT_FILE^=file
4491 @cindex @option{^-gnatl^OUTPUT_FILE^=fname} (@command{gcc})
4492 This has the same effect as @option{-gnatl} except that the output is
4493 written to a file instead of to standard output. If the given name
4494 @file{fname} does not start with a period, then it is the full name
4495 of the file to be written. If @file{fname} is an extension, it is
4496 appended to the name of the file being compiled. For example, if
4497 file @file{xyz.adb} is compiled with @option{^-gnatl^OUTPUT_FILE^=.lst},
4498 then the output is written to file ^xyz.adb.lst^xyz.adb_lst^.
4499
4500 @item -gnatU
4501 @cindex @option{-gnatU} (@command{gcc})
4502 This switch forces all error messages to be preceded by the unique
4503 string ``error:''. This means that error messages take a few more
4504 characters in space, but allows easy searching for and identification
4505 of error messages.
4506
4507 @item -gnatb
4508 @cindex @option{-gnatb} (@command{gcc})
4509 @ifclear vms
4510 The @code{b} stands for brief.
4511 @end ifclear
4512 This switch causes GNAT to generate the
4513 brief format error messages to @file{stderr} (the standard error
4514 file) as well as the verbose
4515 format message or full listing (which as usual is written to
4516 @file{stdout} (the standard output file).
4517
4518 @item -gnatm=@var{n}
4519 @cindex @option{-gnatm} (@command{gcc})
4520 @ifclear vms
4521 The @code{m} stands for maximum.
4522 @end ifclear
4523 @var{n} is a decimal integer in the
4524 range of 1 to 999999 and limits the number of error or warning
4525 messages to be generated. For example, using
4526 @option{-gnatm2} might yield
4527
4528 @smallexample
4529 e.adb:3:04: Incorrect spelling of keyword "function"
4530 e.adb:5:35: missing ".."
4531 fatal error: maximum number of errors detected
4532 compilation abandoned
4533 @end smallexample
4534
4535 @noindent
4536 The default setting if
4537 no switch is given is 9999. If the number of warnings reaches this
4538 limit, then a message is output and further warnings are suppressed,
4539 but the compilation is continued. If the number of error messages
4540 reaches this limit, then a message is output and the compilation
4541 is abandoned. A value of zero means that no limit applies.
4542
4543 @noindent
4544 Note that the equal sign is optional, so the switches
4545 @option{-gnatm2} and @option{-gnatm=2} are equivalent.
4546
4547 @item -gnatf
4548 @cindex @option{-gnatf} (@command{gcc})
4549 @cindex Error messages, suppressing
4550 @ifclear vms
4551 The @code{f} stands for full.
4552 @end ifclear
4553 Normally, the compiler suppresses error messages that are likely to be
4554 redundant. This switch causes all error
4555 messages to be generated. In particular, in the case of
4556 references to undefined variables. If a given variable is referenced
4557 several times, the normal format of messages is
4558 @smallexample
4559 e.adb:7:07: "V" is undefined (more references follow)
4560 @end smallexample
4561
4562 @noindent
4563 where the parenthetical comment warns that there are additional
4564 references to the variable @code{V}. Compiling the same program with the
4565 @option{-gnatf} switch yields
4566
4567 @smallexample
4568 e.adb:7:07: "V" is undefined
4569 e.adb:8:07: "V" is undefined
4570 e.adb:8:12: "V" is undefined
4571 e.adb:8:16: "V" is undefined
4572 e.adb:9:07: "V" is undefined
4573 e.adb:9:12: "V" is undefined
4574 @end smallexample
4575
4576 @noindent
4577 The @option{-gnatf} switch also generates additional information for
4578 some error messages. Some examples are:
4579
4580 @itemize @bullet
4581 @item
4582 Full details on entities not available in high integrity mode
4583 @item
4584 Details on possibly non-portable unchecked conversion
4585 @item
4586 List possible interpretations for ambiguous calls
4587 @item
4588 Additional details on incorrect parameters
4589 @end itemize
4590
4591 @item -gnatjnn
4592 @cindex @option{-gnatjnn} (@command{gcc})
4593 In normal operation mode (or if @option{-gnatj0} is used, then error messages
4594 with continuation lines are treated as though the continuation lines were
4595 separate messages (and so a warning with two continuation lines counts as
4596 three warnings, and is listed as three separate messages).
4597
4598 If the @option{-gnatjnn} switch is used with a positive value for nn, then
4599 messages are output in a different manner. A message and all its continuation
4600 lines are treated as a unit, and count as only one warning or message in the
4601 statistics totals. Furthermore, the message is reformatted so that no line
4602 is longer than nn characters.
4603
4604 @item -gnatq
4605 @cindex @option{-gnatq} (@command{gcc})
4606 @ifclear vms
4607 The @code{q} stands for quit (really ``don't quit'').
4608 @end ifclear
4609 In normal operation mode, the compiler first parses the program and
4610 determines if there are any syntax errors. If there are, appropriate
4611 error messages are generated and compilation is immediately terminated.
4612 This switch tells
4613 GNAT to continue with semantic analysis even if syntax errors have been
4614 found. This may enable the detection of more errors in a single run. On
4615 the other hand, the semantic analyzer is more likely to encounter some
4616 internal fatal error when given a syntactically invalid tree.
4617
4618 @item -gnatQ
4619 @cindex @option{-gnatQ} (@command{gcc})
4620 In normal operation mode, the @file{ALI} file is not generated if any
4621 illegalities are detected in the program. The use of @option{-gnatQ} forces
4622 generation of the @file{ALI} file. This file is marked as being in
4623 error, so it cannot be used for binding purposes, but it does contain
4624 reasonably complete cross-reference information, and thus may be useful
4625 for use by tools (e.g., semantic browsing tools or integrated development
4626 environments) that are driven from the @file{ALI} file. This switch
4627 implies @option{-gnatq}, since the semantic phase must be run to get a
4628 meaningful ALI file.
4629
4630 In addition, if @option{-gnatt} is also specified, then the tree file is
4631 generated even if there are illegalities. It may be useful in this case
4632 to also specify @option{-gnatq} to ensure that full semantic processing
4633 occurs. The resulting tree file can be processed by ASIS, for the purpose
4634 of providing partial information about illegal units, but if the error
4635 causes the tree to be badly malformed, then ASIS may crash during the
4636 analysis.
4637
4638 When @option{-gnatQ} is used and the generated @file{ALI} file is marked as
4639 being in error, @command{gnatmake} will attempt to recompile the source when it
4640 finds such an @file{ALI} file, including with switch @option{-gnatc}.
4641
4642 Note that @option{-gnatQ} has no effect if @option{-gnats} is specified,
4643 since ALI files are never generated if @option{-gnats} is set.
4644
4645 @end table
4646
4647 @node Warning Message Control
4648 @subsection Warning Message Control
4649 @cindex Warning messages
4650 @noindent
4651 In addition to error messages, which correspond to illegalities as defined
4652 in the Ada Reference Manual, the compiler detects two kinds of warning
4653 situations.
4654
4655 First, the compiler considers some constructs suspicious and generates a
4656 warning message to alert you to a possible error. Second, if the
4657 compiler detects a situation that is sure to raise an exception at
4658 run time, it generates a warning message. The following shows an example
4659 of warning messages:
4660 @smallexample
4661 e.adb:4:24: warning: creation of object may raise Storage_Error
4662 e.adb:10:17: warning: static value out of range
4663 e.adb:10:17: warning: "Constraint_Error" will be raised at run time
4664 @end smallexample
4665
4666 @noindent
4667 GNAT considers a large number of situations as appropriate
4668 for the generation of warning messages. As always, warnings are not
4669 definite indications of errors. For example, if you do an out-of-range
4670 assignment with the deliberate intention of raising a
4671 @code{Constraint_Error} exception, then the warning that may be
4672 issued does not indicate an error. Some of the situations for which GNAT
4673 issues warnings (at least some of the time) are given in the following
4674 list. This list is not complete, and new warnings are often added to
4675 subsequent versions of GNAT. The list is intended to give a general idea
4676 of the kinds of warnings that are generated.
4677
4678 @itemize @bullet
4679 @item
4680 Possible infinitely recursive calls
4681
4682 @item
4683 Out-of-range values being assigned
4684
4685 @item
4686 Possible order of elaboration problems
4687
4688 @item
4689 Assertions (pragma Assert) that are sure to fail
4690
4691 @item
4692 Unreachable code
4693
4694 @item
4695 Address clauses with possibly unaligned values, or where an attempt is
4696 made to overlay a smaller variable with a larger one.
4697
4698 @item
4699 Fixed-point type declarations with a null range
4700
4701 @item
4702 Direct_IO or Sequential_IO instantiated with a type that has access values
4703
4704 @item
4705 Variables that are never assigned a value
4706
4707 @item
4708 Variables that are referenced before being initialized
4709
4710 @item
4711 Task entries with no corresponding @code{accept} statement
4712
4713 @item
4714 Duplicate accepts for the same task entry in a @code{select}
4715
4716 @item
4717 Objects that take too much storage
4718
4719 @item
4720 Unchecked conversion between types of differing sizes
4721
4722 @item
4723 Missing @code{return} statement along some execution path in a function
4724
4725 @item
4726 Incorrect (unrecognized) pragmas
4727
4728 @item
4729 Incorrect external names
4730
4731 @item
4732 Allocation from empty storage pool
4733
4734 @item
4735 Potentially blocking operation in protected type
4736
4737 @item
4738 Suspicious parenthesization of expressions
4739
4740 @item
4741 Mismatching bounds in an aggregate
4742
4743 @item
4744 Attempt to return local value by reference
4745
4746 @item
4747 Premature instantiation of a generic body
4748
4749 @item
4750 Attempt to pack aliased components
4751
4752 @item
4753 Out of bounds array subscripts
4754
4755 @item
4756 Wrong length on string assignment
4757
4758 @item
4759 Violations of style rules if style checking is enabled
4760
4761 @item
4762 Unused @code{with} clauses
4763
4764 @item
4765 @code{Bit_Order} usage that does not have any effect
4766
4767 @item
4768 @code{Standard.Duration} used to resolve universal fixed expression
4769
4770 @item
4771 Dereference of possibly null value
4772
4773 @item
4774 Declaration that is likely to cause storage error
4775
4776 @item
4777 Internal GNAT unit @code{with}'ed by application unit
4778
4779 @item
4780 Values known to be out of range at compile time
4781
4782 @item
4783 Unreferenced labels and variables
4784
4785 @item
4786 Address overlays that could clobber memory
4787
4788 @item
4789 Unexpected initialization when address clause present
4790
4791 @item
4792 Bad alignment for address clause
4793
4794 @item
4795 Useless type conversions
4796
4797 @item
4798 Redundant assignment statements and other redundant constructs
4799
4800 @item
4801 Useless exception handlers
4802
4803 @item
4804 Accidental hiding of name by child unit
4805
4806 @item
4807 Access before elaboration detected at compile time
4808
4809 @item
4810 A range in a @code{for} loop that is known to be null or might be null
4811
4812 @end itemize
4813
4814 @noindent
4815 The following section lists compiler switches that are available
4816 to control the handling of warning messages. It is also possible
4817 to exercise much finer control over what warnings are issued and
4818 suppressed using the GNAT pragma Warnings, @xref{Pragma Warnings,,,
4819 gnat_rm, GNAT Reference manual}.
4820
4821 @table @option
4822 @c !sort!
4823 @item -gnatwa
4824 @emph{Activate all optional errors.}
4825 @cindex @option{-gnatwa} (@command{gcc})
4826 This switch activates most optional warning messages, see remaining list
4827 in this section for details on optional warning messages that can be
4828 individually controlled. The warnings that are not turned on by this
4829 switch are
4830 @option{-gnatwd} (implicit dereferencing),
4831 @option{-gnatwh} (hiding),
4832 @option{-gnatwl} (elaboration warnings),
4833 @option{-gnatw.o} (warn on values set by out parameters ignored)
4834 and @option{-gnatwt} (tracking of deleted conditional code).
4835 All other optional warnings are turned on.
4836
4837 @item -gnatwA
4838 @emph{Suppress all optional errors.}
4839 @cindex @option{-gnatwA} (@command{gcc})
4840 This switch suppresses all optional warning messages, see remaining list
4841 in this section for details on optional warning messages that can be
4842 individually controlled.
4843
4844 @item -gnatw.a
4845 @emph{Activate warnings on failing assertions.}
4846 @cindex @option{-gnatw.a} (@command{gcc})
4847 @cindex Assert failures
4848 This switch activates warnings for assertions where the compiler can tell at
4849 compile time that the assertion will fail. Note that this warning is given
4850 even if assertions are disabled. The default is that such warnings are
4851 generated.
4852
4853 @item -gnatw.A
4854 @emph{Suppress warnings on failing assertions.}
4855 @cindex @option{-gnatw.A} (@command{gcc})
4856 @cindex Assert failures
4857 This switch suppresses warnings for assertions where the compiler can tell at
4858 compile time that the assertion will fail.
4859
4860 @item -gnatwb
4861 @emph{Activate warnings on bad fixed values.}
4862 @cindex @option{-gnatwb} (@command{gcc})
4863 @cindex Bad fixed values
4864 @cindex Fixed-point Small value
4865 @cindex Small value
4866 This switch activates warnings for static fixed-point expressions whose
4867 value is not an exact multiple of Small. Such values are implementation
4868 dependent, since an implementation is free to choose either of the multiples
4869 that surround the value. GNAT always chooses the closer one, but this is not
4870 required behavior, and it is better to specify a value that is an exact
4871 multiple, ensuring predictable execution. The default is that such warnings
4872 are not generated.
4873
4874 @item -gnatwB
4875 @emph{Suppress warnings on bad fixed values.}
4876 @cindex @option{-gnatwB} (@command{gcc})
4877 This switch suppresses warnings for static fixed-point expressions whose
4878 value is not an exact multiple of Small.
4879
4880 @item -gnatw.b
4881 @emph{Activate warnings on biased representation.}
4882 @cindex @option{-gnatw.b} (@command{gcc})
4883 @cindex Biased representation
4884 This switch activates warnings when a size clause, value size clause, component
4885 clause, or component size clause forces the use of biased representation for an
4886 integer type (e.g. representing a range of 10..11 in a single bit by using 0/1
4887 to represent 10/11). The default is that such warnings are generated.
4888
4889 @item -gnatw.B
4890 @emph{Suppress warnings on biased representation.}
4891 @cindex @option{-gnatwB} (@command{gcc})
4892 This switch suppresses warnings for representation clauses that force the use
4893 of biased representation.
4894
4895 @item -gnatwc
4896 @emph{Activate warnings on conditionals.}
4897 @cindex @option{-gnatwc} (@command{gcc})
4898 @cindex Conditionals, constant
4899 This switch activates warnings for conditional expressions used in
4900 tests that are known to be True or False at compile time. The default
4901 is that such warnings are not generated.
4902 Note that this warning does
4903 not get issued for the use of boolean variables or constants whose
4904 values are known at compile time, since this is a standard technique
4905 for conditional compilation in Ada, and this would generate too many
4906 false positive warnings.
4907
4908 This warning option also activates a special test for comparisons using
4909 the operators ``>='' and`` <=''.
4910 If the compiler can tell that only the equality condition is possible,
4911 then it will warn that the ``>'' or ``<'' part of the test
4912 is useless and that the operator could be replaced by ``=''.
4913 An example would be comparing a @code{Natural} variable <= 0.
4914
4915 This warning option also generates warnings if
4916 one or both tests is optimized away in a membership test for integer
4917 values if the result can be determined at compile time. Range tests on
4918 enumeration types are not included, since it is common for such tests
4919 to include an end point.
4920
4921 This warning can also be turned on using @option{-gnatwa}.
4922
4923 @item -gnatwC
4924 @emph{Suppress warnings on conditionals.}
4925 @cindex @option{-gnatwC} (@command{gcc})
4926 This switch suppresses warnings for conditional expressions used in
4927 tests that are known to be True or False at compile time.
4928
4929 @item -gnatw.c
4930 @emph{Activate warnings on missing component clauses.}
4931 @cindex @option{-gnatw.c} (@command{gcc})
4932 @cindex Component clause, missing
4933 This switch activates warnings for record components where a record
4934 representation clause is present and has component clauses for the
4935 majority, but not all, of the components. A warning is given for each
4936 component for which no component clause is present.
4937
4938 This warning can also be turned on using @option{-gnatwa}.
4939
4940 @item -gnatw.C
4941 @emph{Suppress warnings on missing component clauses.}
4942 @cindex @option{-gnatwC} (@command{gcc})
4943 This switch suppresses warnings for record components that are
4944 missing a component clause in the situation described above.
4945
4946 @item -gnatwd
4947 @emph{Activate warnings on implicit dereferencing.}
4948 @cindex @option{-gnatwd} (@command{gcc})
4949 If this switch is set, then the use of a prefix of an access type
4950 in an indexed component, slice, or selected component without an
4951 explicit @code{.all} will generate a warning. With this warning
4952 enabled, access checks occur only at points where an explicit
4953 @code{.all} appears in the source code (assuming no warnings are
4954 generated as a result of this switch). The default is that such
4955 warnings are not generated.
4956 Note that @option{-gnatwa} does not affect the setting of
4957 this warning option.
4958
4959 @item -gnatwD
4960 @emph{Suppress warnings on implicit dereferencing.}
4961 @cindex @option{-gnatwD} (@command{gcc})
4962 @cindex Implicit dereferencing
4963 @cindex Dereferencing, implicit
4964 This switch suppresses warnings for implicit dereferences in
4965 indexed components, slices, and selected components.
4966
4967 @item -gnatwe
4968 @emph{Treat warnings as errors.}
4969 @cindex @option{-gnatwe} (@command{gcc})
4970 @cindex Warnings, treat as error
4971 This switch causes warning messages to be treated as errors.
4972 The warning string still appears, but the warning messages are counted
4973 as errors, and prevent the generation of an object file.
4974
4975 @item -gnatw.e
4976 @emph{Activate every optional warning}
4977 @cindex @option{-gnatw.e} (@command{gcc})
4978 @cindex Warnings, activate every optional warning
4979 This switch activates all optional warnings, including those which
4980 are not activated by @code{-gnatwa}.
4981
4982 @item -gnatwf
4983 @emph{Activate warnings on unreferenced formals.}
4984 @cindex @option{-gnatwf} (@command{gcc})
4985 @cindex Formals, unreferenced
4986 This switch causes a warning to be generated if a formal parameter
4987 is not referenced in the body of the subprogram. This warning can
4988 also be turned on using @option{-gnatwa} or @option{-gnatwu}. The
4989 default is that these warnings are not generated.
4990
4991 @item -gnatwF
4992 @emph{Suppress warnings on unreferenced formals.}
4993 @cindex @option{-gnatwF} (@command{gcc})
4994 This switch suppresses warnings for unreferenced formal
4995 parameters. Note that the
4996 combination @option{-gnatwu} followed by @option{-gnatwF} has the
4997 effect of warning on unreferenced entities other than subprogram
4998 formals.
4999
5000 @item -gnatwg
5001 @emph{Activate warnings on unrecognized pragmas.}
5002 @cindex @option{-gnatwg} (@command{gcc})
5003 @cindex Pragmas, unrecognized
5004 This switch causes a warning to be generated if an unrecognized
5005 pragma is encountered. Apart from issuing this warning, the
5006 pragma is ignored and has no effect. This warning can
5007 also be turned on using @option{-gnatwa}. The default
5008 is that such warnings are issued (satisfying the Ada Reference
5009 Manual requirement that such warnings appear).
5010
5011 @item -gnatwG
5012 @emph{Suppress warnings on unrecognized pragmas.}
5013 @cindex @option{-gnatwG} (@command{gcc})
5014 This switch suppresses warnings for unrecognized pragmas.
5015
5016 @item -gnatwh
5017 @emph{Activate warnings on hiding.}
5018 @cindex @option{-gnatwh} (@command{gcc})
5019 @cindex Hiding of Declarations
5020 This switch activates warnings on hiding declarations.
5021 A declaration is considered hiding
5022 if it is for a non-overloadable entity, and it declares an entity with the
5023 same name as some other entity that is directly or use-visible. The default
5024 is that such warnings are not generated.
5025 Note that @option{-gnatwa} does not affect the setting of this warning option.
5026
5027 @item -gnatwH
5028 @emph{Suppress warnings on hiding.}
5029 @cindex @option{-gnatwH} (@command{gcc})
5030 This switch suppresses warnings on hiding declarations.
5031
5032 @item -gnatwi
5033 @emph{Activate warnings on implementation units.}
5034 @cindex @option{-gnatwi} (@command{gcc})
5035 This switch activates warnings for a @code{with} of an internal GNAT
5036 implementation unit, defined as any unit from the @code{Ada},
5037 @code{Interfaces}, @code{GNAT},
5038 ^^@code{DEC},^ or @code{System}
5039 hierarchies that is not
5040 documented in either the Ada Reference Manual or the GNAT
5041 Programmer's Reference Manual. Such units are intended only
5042 for internal implementation purposes and should not be @code{with}'ed
5043 by user programs. The default is that such warnings are generated
5044 This warning can also be turned on using @option{-gnatwa}.
5045
5046 @item -gnatwI
5047 @emph{Disable warnings on implementation units.}
5048 @cindex @option{-gnatwI} (@command{gcc})
5049 This switch disables warnings for a @code{with} of an internal GNAT
5050 implementation unit.
5051
5052 @item -gnatwj
5053 @emph{Activate warnings on obsolescent features (Annex J).}
5054 @cindex @option{-gnatwj} (@command{gcc})
5055 @cindex Features, obsolescent
5056 @cindex Obsolescent features
5057 If this warning option is activated, then warnings are generated for
5058 calls to subprograms marked with @code{pragma Obsolescent} and
5059 for use of features in Annex J of the Ada Reference Manual. In the
5060 case of Annex J, not all features are flagged. In particular use
5061 of the renamed packages (like @code{Text_IO}) and use of package
5062 @code{ASCII} are not flagged, since these are very common and
5063 would generate many annoying positive warnings. The default is that
5064 such warnings are not generated. This warning is also turned on by
5065 the use of @option{-gnatwa}.
5066
5067 In addition to the above cases, warnings are also generated for
5068 GNAT features that have been provided in past versions but which
5069 have been superseded (typically by features in the new Ada standard).
5070 For example, @code{pragma Ravenscar} will be flagged since its
5071 function is replaced by @code{pragma Profile(Ravenscar)}.
5072
5073 Note that this warning option functions differently from the
5074 restriction @code{No_Obsolescent_Features} in two respects.
5075 First, the restriction applies only to annex J features.
5076 Second, the restriction does flag uses of package @code{ASCII}.
5077
5078 @item -gnatwJ
5079 @emph{Suppress warnings on obsolescent features (Annex J).}
5080 @cindex @option{-gnatwJ} (@command{gcc})
5081 This switch disables warnings on use of obsolescent features.
5082
5083 @item -gnatwk
5084 @emph{Activate warnings on variables that could be constants.}
5085 @cindex @option{-gnatwk} (@command{gcc})
5086 This switch activates warnings for variables that are initialized but
5087 never modified, and then could be declared constants. The default is that
5088 such warnings are not given.
5089 This warning can also be turned on using @option{-gnatwa}.
5090
5091 @item -gnatwK
5092 @emph{Suppress warnings on variables that could be constants.}
5093 @cindex @option{-gnatwK} (@command{gcc})
5094 This switch disables warnings on variables that could be declared constants.
5095
5096 @item -gnatwl
5097 @emph{Activate warnings for elaboration pragmas.}
5098 @cindex @option{-gnatwl} (@command{gcc})
5099 @cindex Elaboration, warnings
5100 This switch activates warnings on missing
5101 @code{Elaborate_All} and @code{Elaborate} pragmas.
5102 See the section in this guide on elaboration checking for details on
5103 when such pragmas should be used. In dynamic elaboration mode, this switch
5104 generations warnings about the need to add elaboration pragmas. Note however,
5105 that if you blindly follow these warnings, and add @code{Elaborate_All}
5106 warnings wherever they are recommended, you basically end up with the
5107 equivalent of the static elaboration model, which may not be what you want for
5108 legacy code for which the static model does not work.
5109
5110 For the static model, the messages generated are labeled "info:" (for
5111 information messages). They are not warnings to add elaboration pragmas,
5112 merely informational messages showing what implicit elaboration pragmas
5113 have been added, for use in analyzing elaboration circularity problems.
5114
5115 Warnings are also generated if you
5116 are using the static mode of elaboration, and a @code{pragma Elaborate}
5117 is encountered. The default is that such warnings
5118 are not generated.
5119 This warning is not automatically turned on by the use of @option{-gnatwa}.
5120
5121 @item -gnatwL
5122 @emph{Suppress warnings for elaboration pragmas.}
5123 @cindex @option{-gnatwL} (@command{gcc})
5124 This switch suppresses warnings on missing Elaborate and Elaborate_All pragmas.
5125 See the section in this guide on elaboration checking for details on
5126 when such pragmas should be used.
5127
5128 @item -gnatwm
5129 @emph{Activate warnings on modified but unreferenced variables.}
5130 @cindex @option{-gnatwm} (@command{gcc})
5131 This switch activates warnings for variables that are assigned (using
5132 an initialization value or with one or more assignment statements) but
5133 whose value is never read. The warning is suppressed for volatile
5134 variables and also for variables that are renamings of other variables
5135 or for which an address clause is given.
5136 This warning can also be turned on using @option{-gnatwa}.
5137 The default is that these warnings are not given.
5138
5139 @item -gnatwM
5140 @emph{Disable warnings on modified but unreferenced variables.}
5141 @cindex @option{-gnatwM} (@command{gcc})
5142 This switch disables warnings for variables that are assigned or
5143 initialized, but never read.
5144
5145 @item -gnatwn
5146 @emph{Set normal warnings mode.}
5147 @cindex @option{-gnatwn} (@command{gcc})
5148 This switch sets normal warning mode, in which enabled warnings are
5149 issued and treated as warnings rather than errors. This is the default
5150 mode. the switch @option{-gnatwn} can be used to cancel the effect of
5151 an explicit @option{-gnatws} or
5152 @option{-gnatwe}. It also cancels the effect of the
5153 implicit @option{-gnatwe} that is activated by the
5154 use of @option{-gnatg}.
5155
5156 @item -gnatwo
5157 @emph{Activate warnings on address clause overlays.}
5158 @cindex @option{-gnatwo} (@command{gcc})
5159 @cindex Address Clauses, warnings
5160 This switch activates warnings for possibly unintended initialization
5161 effects of defining address clauses that cause one variable to overlap
5162 another. The default is that such warnings are generated.
5163 This warning can also be turned on using @option{-gnatwa}.
5164
5165 @item -gnatwO
5166 @emph{Suppress warnings on address clause overlays.}
5167 @cindex @option{-gnatwO} (@command{gcc})
5168 This switch suppresses warnings on possibly unintended initialization
5169 effects of defining address clauses that cause one variable to overlap
5170 another.
5171
5172 @item -gnatw.o
5173 @emph{Activate warnings on modified but unreferenced out parameters.}
5174 @cindex @option{-gnatw.o} (@command{gcc})
5175 This switch activates warnings for variables that are modified by using
5176 them as actuals for a call to a procedure with an out mode formal, where
5177 the resulting assigned value is never read. It is applicable in the case
5178 where there is more than one out mode formal. If there is only one out
5179 mode formal, the warning is issued by default (controlled by -gnatwu).
5180 The warning is suppressed for volatile
5181 variables and also for variables that are renamings of other variables
5182 or for which an address clause is given.
5183 The default is that these warnings are not given. Note that this warning
5184 is not included in -gnatwa, it must be activated explicitly.
5185
5186 @item -gnatw.O
5187 @emph{Disable warnings on modified but unreferenced out parameters.}
5188 @cindex @option{-gnatw.O} (@command{gcc})
5189 This switch suppresses warnings for variables that are modified by using
5190 them as actuals for a call to a procedure with an out mode formal, where
5191 the resulting assigned value is never read.
5192
5193 @item -gnatwp
5194 @emph{Activate warnings on ineffective pragma Inlines.}
5195 @cindex @option{-gnatwp} (@command{gcc})
5196 @cindex Inlining, warnings
5197 This switch activates warnings for failure of front end inlining
5198 (activated by @option{-gnatN}) to inline a particular call. There are
5199 many reasons for not being able to inline a call, including most
5200 commonly that the call is too complex to inline. The default is
5201 that such warnings are not given.
5202 This warning can also be turned on using @option{-gnatwa}.
5203 Warnings on ineffective inlining by the gcc back-end can be activated
5204 separately, using the gcc switch -Winline.
5205
5206 @item -gnatwP
5207 @emph{Suppress warnings on ineffective pragma Inlines.}
5208 @cindex @option{-gnatwP} (@command{gcc})
5209 This switch suppresses warnings on ineffective pragma Inlines. If the
5210 inlining mechanism cannot inline a call, it will simply ignore the
5211 request silently.
5212
5213 @item -gnatw.p
5214 @emph{Activate warnings on parameter ordering.}
5215 @cindex @option{-gnatw.p} (@command{gcc})
5216 @cindex Parameter order, warnings
5217 This switch activates warnings for cases of suspicious parameter
5218 ordering when the list of arguments are all simple identifiers that
5219 match the names of the formals, but are in a different order. The
5220 warning is suppressed if any use of named parameter notation is used,
5221 so this is the appropriate way to suppress a false positive (and
5222 serves to emphasize that the "misordering" is deliberate). The
5223 default is
5224 that such warnings are not given.
5225 This warning can also be turned on using @option{-gnatwa}.
5226
5227 @item -gnatw.P
5228 @emph{Suppress warnings on parameter ordering.}
5229 @cindex @option{-gnatw.P} (@command{gcc})
5230 This switch suppresses warnings on cases of suspicious parameter
5231 ordering.
5232
5233 @item -gnatwq
5234 @emph{Activate warnings on questionable missing parentheses.}
5235 @cindex @option{-gnatwq} (@command{gcc})
5236 @cindex Parentheses, warnings
5237 This switch activates warnings for cases where parentheses are not used and
5238 the result is potential ambiguity from a readers point of view. For example
5239 (not a > b) when a and b are modular means ((not a) > b) and very likely the
5240 programmer intended (not (a > b)). Similarly (-x mod 5) means (-(x mod 5)) and
5241 quite likely ((-x) mod 5) was intended. In such situations it seems best to
5242 follow the rule of always parenthesizing to make the association clear, and
5243 this warning switch warns if such parentheses are not present. The default
5244 is that these warnings are given.
5245 This warning can also be turned on using @option{-gnatwa}.
5246
5247 @item -gnatwQ
5248 @emph{Suppress warnings on questionable missing parentheses.}
5249 @cindex @option{-gnatwQ} (@command{gcc})
5250 This switch suppresses warnings for cases where the association is not
5251 clear and the use of parentheses is preferred.
5252
5253 @item -gnatwr
5254 @emph{Activate warnings on redundant constructs.}
5255 @cindex @option{-gnatwr} (@command{gcc})
5256 This switch activates warnings for redundant constructs. The following
5257 is the current list of constructs regarded as redundant:
5258
5259 @itemize @bullet
5260 @item
5261 Assignment of an item to itself.
5262 @item
5263 Type conversion that converts an expression to its own type.
5264 @item
5265 Use of the attribute @code{Base} where @code{typ'Base} is the same
5266 as @code{typ}.
5267 @item
5268 Use of pragma @code{Pack} when all components are placed by a record
5269 representation clause.
5270 @item
5271 Exception handler containing only a reraise statement (raise with no
5272 operand) which has no effect.
5273 @item
5274 Use of the operator abs on an operand that is known at compile time
5275 to be non-negative
5276 @item
5277 Comparison of boolean expressions to an explicit True value.
5278 @end itemize
5279
5280 This warning can also be turned on using @option{-gnatwa}.
5281 The default is that warnings for redundant constructs are not given.
5282
5283 @item -gnatwR
5284 @emph{Suppress warnings on redundant constructs.}
5285 @cindex @option{-gnatwR} (@command{gcc})
5286 This switch suppresses warnings for redundant constructs.
5287
5288 @item -gnatws
5289 @emph{Suppress all warnings.}
5290 @cindex @option{-gnatws} (@command{gcc})
5291 This switch completely suppresses the
5292 output of all warning messages from the GNAT front end.
5293 Note that it does not suppress warnings from the @command{gcc} back end.
5294 To suppress these back end warnings as well, use the switch @option{-w}
5295 in addition to @option{-gnatws}.
5296
5297 @item -gnatwt
5298 @emph{Activate warnings for tracking of deleted conditional code.}
5299 @cindex @option{-gnatwt} (@command{gcc})
5300 @cindex Deactivated code, warnings
5301 @cindex Deleted code, warnings
5302 This switch activates warnings for tracking of code in conditionals (IF and
5303 CASE statements) that is detected to be dead code which cannot be executed, and
5304 which is removed by the front end. This warning is off by default, and is not
5305 turned on by @option{-gnatwa}, it has to be turned on explicitly. This may be
5306 useful for detecting deactivated code in certified applications.
5307
5308 @item -gnatwT
5309 @emph{Suppress warnings for tracking of deleted conditional code.}
5310 @cindex @option{-gnatwT} (@command{gcc})
5311 This switch suppresses warnings for tracking of deleted conditional code.
5312
5313 @item -gnatwu
5314 @emph{Activate warnings on unused entities.}
5315 @cindex @option{-gnatwu} (@command{gcc})
5316 This switch activates warnings to be generated for entities that
5317 are declared but not referenced, and for units that are @code{with}'ed
5318 and not
5319 referenced. In the case of packages, a warning is also generated if
5320 no entities in the package are referenced. This means that if the package
5321 is referenced but the only references are in @code{use}
5322 clauses or @code{renames}
5323 declarations, a warning is still generated. A warning is also generated
5324 for a generic package that is @code{with}'ed but never instantiated.
5325 In the case where a package or subprogram body is compiled, and there
5326 is a @code{with} on the corresponding spec
5327 that is only referenced in the body,
5328 a warning is also generated, noting that the
5329 @code{with} can be moved to the body. The default is that
5330 such warnings are not generated.
5331 This switch also activates warnings on unreferenced formals
5332 (it includes the effect of @option{-gnatwf}).
5333 This warning can also be turned on using @option{-gnatwa}.
5334
5335 @item -gnatwU
5336 @emph{Suppress warnings on unused entities.}
5337 @cindex @option{-gnatwU} (@command{gcc})
5338 This switch suppresses warnings for unused entities and packages.
5339 It also turns off warnings on unreferenced formals (and thus includes
5340 the effect of @option{-gnatwF}).
5341
5342 @item -gnatwv
5343 @emph{Activate warnings on unassigned variables.}
5344 @cindex @option{-gnatwv} (@command{gcc})
5345 @cindex Unassigned variable warnings
5346 This switch activates warnings for access to variables which
5347 may not be properly initialized. The default is that
5348 such warnings are generated.
5349 This warning can also be turned on using @option{-gnatwa}.
5350
5351 @item -gnatwV
5352 @emph{Suppress warnings on unassigned variables.}
5353 @cindex @option{-gnatwV} (@command{gcc})
5354 This switch suppresses warnings for access to variables which
5355 may not be properly initialized.
5356 For variables of a composite type, the warning can also be suppressed in
5357 Ada 2005 by using a default initialization with a box. For example, if
5358 Table is an array of records whose components are only partially uninitialized,
5359 then the following code:
5360
5361 @smallexample @c ada
5362 Tab : Table := (others => <>);
5363 @end smallexample
5364
5365 will suppress warnings on subsequent statements that access components
5366 of variable Tab.
5367
5368 @item -gnatww
5369 @emph{Activate warnings on wrong low bound assumption.}
5370 @cindex @option{-gnatww} (@command{gcc})
5371 @cindex String indexing warnings
5372 This switch activates warnings for indexing an unconstrained string parameter
5373 with a literal or S'Length. This is a case where the code is assuming that the
5374 low bound is one, which is in general not true (for example when a slice is
5375 passed). The default is that such warnings are generated.
5376 This warning can also be turned on using @option{-gnatwa}.
5377
5378 @item -gnatwW
5379 @emph{Suppress warnings on wrong low bound assumption.}
5380 @cindex @option{-gnatwW} (@command{gcc})
5381 This switch suppresses warnings for indexing an unconstrained string parameter
5382 with a literal or S'Length. Note that this warning can also be suppressed
5383 in a particular case by adding an
5384 assertion that the lower bound is 1,
5385 as shown in the following example.
5386
5387 @smallexample @c ada
5388 procedure K (S : String) is
5389 pragma Assert (S'First = 1);
5390 @dots{}
5391 @end smallexample
5392
5393 @item -gnatw.w
5394 @emph{Activate warnings on unnecessary Warnings Off pragmas}
5395 @cindex @option{-gnatw.w} (@command{gcc})
5396 @cindex Warnings Off control
5397 This switch activates warnings for use of @code{pragma Warnings (Off, entity}
5398 where either the pragma is entirely useless (because it suppresses no
5399 warnings), or it could be replaced by @code{pragma Unreferenced} or
5400 @code{pragma Unmodified}.The default is that these warnings are not given.
5401 Note that this warning is not included in -gnatwa, it must be
5402 activated explicitly.
5403
5404 @item -gnatw.W
5405 @emph{Suppress warnings on unnecessary Warnings Off pragmas}
5406 @cindex @option{-gnatw.W} (@command{gcc})
5407 This switch suppresses warnings for use of @code{pragma Warnings (Off, entity}.
5408
5409 @item -gnatwx
5410 @emph{Activate warnings on Export/Import pragmas.}
5411 @cindex @option{-gnatwx} (@command{gcc})
5412 @cindex Export/Import pragma warnings
5413 This switch activates warnings on Export/Import pragmas when
5414 the compiler detects a possible conflict between the Ada and
5415 foreign language calling sequences. For example, the use of
5416 default parameters in a convention C procedure is dubious
5417 because the C compiler cannot supply the proper default, so
5418 a warning is issued. The default is that such warnings are
5419 generated.
5420 This warning can also be turned on using @option{-gnatwa}.
5421
5422 @item -gnatwX
5423 @emph{Suppress warnings on Export/Import pragmas.}
5424 @cindex @option{-gnatwX} (@command{gcc})
5425 This switch suppresses warnings on Export/Import pragmas.
5426 The sense of this is that you are telling the compiler that
5427 you know what you are doing in writing the pragma, and it
5428 should not complain at you.
5429
5430 @item -gnatw.x
5431 @emph{Activate warnings for No_Exception_Propagation mode.}
5432 @cindex @option{-gnatwm} (@command{gcc})
5433 This switch activates warnings for exception usage when pragma Restrictions
5434 (No_Exception_Propagation) is in effect. Warnings are given for implicit or
5435 explicit exception raises which are not covered by a local handler, and for
5436 exception handlers which do not cover a local raise. The default is that these
5437 warnings are not given.
5438
5439 @item -gnatw.X
5440 @emph{Disable warnings for No_Exception_Propagation mode.}
5441 This switch disables warnings for exception usage when pragma Restrictions
5442 (No_Exception_Propagation) is in effect.
5443
5444 @item -gnatwy
5445 @emph{Activate warnings for Ada 2005 compatibility issues.}
5446 @cindex @option{-gnatwy} (@command{gcc})
5447 @cindex Ada 2005 compatibility issues warnings
5448 For the most part Ada 2005 is upwards compatible with Ada 95,
5449 but there are some exceptions (for example the fact that
5450 @code{interface} is now a reserved word in Ada 2005). This
5451 switch activates several warnings to help in identifying
5452 and correcting such incompatibilities. The default is that
5453 these warnings are generated. Note that at one point Ada 2005
5454 was called Ada 0Y, hence the choice of character.
5455 This warning can also be turned on using @option{-gnatwa}.
5456
5457 @item -gnatwY
5458 @emph{Disable warnings for Ada 2005 compatibility issues.}
5459 @cindex @option{-gnatwY} (@command{gcc})
5460 @cindex Ada 2005 compatibility issues warnings
5461 This switch suppresses several warnings intended to help in identifying
5462 incompatibilities between Ada 95 and Ada 2005.
5463
5464 @item -gnatwz
5465 @emph{Activate warnings on unchecked conversions.}
5466 @cindex @option{-gnatwz} (@command{gcc})
5467 @cindex Unchecked_Conversion warnings
5468 This switch activates warnings for unchecked conversions
5469 where the types are known at compile time to have different
5470 sizes. The default
5471 is that such warnings are generated. Warnings are also
5472 generated for subprogram pointers with different conventions,
5473 and, on VMS only, for data pointers with different conventions.
5474 This warning can also be turned on using @option{-gnatwa}.
5475
5476 @item -gnatwZ
5477 @emph{Suppress warnings on unchecked conversions.}
5478 @cindex @option{-gnatwZ} (@command{gcc})
5479 This switch suppresses warnings for unchecked conversions
5480 where the types are known at compile time to have different
5481 sizes or conventions.
5482
5483 @item ^-Wunused^WARNINGS=UNUSED^
5484 @cindex @option{-Wunused}
5485 The warnings controlled by the @option{-gnatw} switch are generated by
5486 the front end of the compiler. The @option{GCC} back end can provide
5487 additional warnings and they are controlled by the @option{-W} switch.
5488 For example, @option{^-Wunused^WARNINGS=UNUSED^} activates back end
5489 warnings for entities that are declared but not referenced.
5490
5491 @item ^-Wuninitialized^WARNINGS=UNINITIALIZED^
5492 @cindex @option{-Wuninitialized}
5493 Similarly, @option{^-Wuninitialized^WARNINGS=UNINITIALIZED^} activates
5494 the back end warning for uninitialized variables. This switch must be
5495 used in conjunction with an optimization level greater than zero.
5496
5497 @item ^-Wall^/ALL_BACK_END_WARNINGS^
5498 @cindex @option{-Wall}
5499 This switch enables all the above warnings from the @option{GCC} back end.
5500 The code generator detects a number of warning situations that are missed
5501 by the @option{GNAT} front end, and this switch can be used to activate them.
5502 The use of this switch also sets the default front end warning mode to
5503 @option{-gnatwa}, that is, most front end warnings activated as well.
5504
5505 @item ^-w^/NO_BACK_END_WARNINGS^
5506 @cindex @option{-w}
5507 Conversely, this switch suppresses warnings from the @option{GCC} back end.
5508 The use of this switch also sets the default front end warning mode to
5509 @option{-gnatws}, that is, front end warnings suppressed as well.
5510
5511 @end table
5512
5513 @noindent
5514 @ifclear vms
5515 A string of warning parameters can be used in the same parameter. For example:
5516
5517 @smallexample
5518 -gnatwaLe
5519 @end smallexample
5520
5521 @noindent
5522 will turn on all optional warnings except for elaboration pragma warnings,
5523 and also specify that warnings should be treated as errors.
5524 @end ifclear
5525 When no switch @option{^-gnatw^/WARNINGS^} is used, this is equivalent to:
5526
5527 @table @option
5528 @c !sort!
5529 @item -gnatwC
5530 @item -gnatwD
5531 @item -gnatwF
5532 @item -gnatwg
5533 @item -gnatwH
5534 @item -gnatwi
5535 @item -gnatwJ
5536 @item -gnatwK
5537 @item -gnatwL
5538 @item -gnatwM
5539 @item -gnatwn
5540 @item -gnatwo
5541 @item -gnatwP
5542 @item -gnatwR
5543 @item -gnatwU
5544 @item -gnatwv
5545 @item -gnatwz
5546 @item -gnatwx
5547
5548 @end table
5549
5550 @node Debugging and Assertion Control
5551 @subsection Debugging and Assertion Control
5552
5553 @table @option
5554 @item -gnata
5555 @cindex @option{-gnata} (@command{gcc})
5556 @findex Assert
5557 @findex Debug
5558 @cindex Assertions
5559
5560 @noindent
5561 The pragmas @code{Assert} and @code{Debug} normally have no effect and
5562 are ignored. This switch, where @samp{a} stands for assert, causes
5563 @code{Assert} and @code{Debug} pragmas to be activated.
5564
5565 The pragmas have the form:
5566
5567 @smallexample
5568 @cartouche
5569 @b{pragma} Assert (@var{Boolean-expression} @r{[},
5570 @var{static-string-expression}@r{]})
5571 @b{pragma} Debug (@var{procedure call})
5572 @end cartouche
5573 @end smallexample
5574
5575 @noindent
5576 The @code{Assert} pragma causes @var{Boolean-expression} to be tested.
5577 If the result is @code{True}, the pragma has no effect (other than
5578 possible side effects from evaluating the expression). If the result is
5579 @code{False}, the exception @code{Assert_Failure} declared in the package
5580 @code{System.Assertions} is
5581 raised (passing @var{static-string-expression}, if present, as the
5582 message associated with the exception). If no string expression is
5583 given the default is a string giving the file name and line number
5584 of the pragma.
5585
5586 The @code{Debug} pragma causes @var{procedure} to be called. Note that
5587 @code{pragma Debug} may appear within a declaration sequence, allowing
5588 debugging procedures to be called between declarations.
5589
5590 @ifset vms
5591 @item /DEBUG@r{[}=debug-level@r{]}
5592 @itemx /NODEBUG
5593 Specifies how much debugging information is to be included in
5594 the resulting object file where 'debug-level' is one of the following:
5595 @table @code
5596 @item TRACEBACK
5597 Include both debugger symbol records and traceback
5598 the object file.
5599 This is the default setting.
5600 @item ALL
5601 Include both debugger symbol records and traceback in
5602 object file.
5603 @item NONE
5604 Excludes both debugger symbol records and traceback
5605 the object file. Same as /NODEBUG.
5606 @item SYMBOLS
5607 Includes only debugger symbol records in the object
5608 file. Note that this doesn't include traceback information.
5609 @end table
5610 @end ifset
5611 @end table
5612
5613 @node Validity Checking
5614 @subsection Validity Checking
5615 @findex Validity Checking
5616
5617 @noindent
5618 The Ada Reference Manual has specific requirements for checking
5619 for invalid values. In particular, RM 13.9.1 requires that the
5620 evaluation of invalid values (for example from unchecked conversions),
5621 not result in erroneous execution. In GNAT, the result of such an
5622 evaluation in normal default mode is to either use the value
5623 unmodified, or to raise Constraint_Error in those cases where use
5624 of the unmodified value would cause erroneous execution. The cases
5625 where unmodified values might lead to erroneous execution are case
5626 statements (where a wild jump might result from an invalid value),
5627 and subscripts on the left hand side (where memory corruption could
5628 occur as a result of an invalid value).
5629
5630 The @option{-gnatB} switch tells the compiler to assume that all
5631 values are valid (that is, within their declared subtype range)
5632 except in the context of a use of the Valid attribute. This means
5633 the compiler can generate more efficient code, since the range
5634 of values is better known at compile time.
5635
5636 The @option{-gnatV^@var{x}^^} switch allows more control over the validity
5637 checking mode.
5638 @ifclear vms
5639 The @code{x} argument is a string of letters that
5640 indicate validity checks that are performed or not performed in addition
5641 to the default checks described above.
5642 @end ifclear
5643 @ifset vms
5644 The options allowed for this qualifier
5645 indicate validity checks that are performed or not performed in addition
5646 to the default checks described above.
5647 @end ifset
5648
5649 @table @option
5650 @c !sort!
5651 @item -gnatVa
5652 @emph{All validity checks.}
5653 @cindex @option{-gnatVa} (@command{gcc})
5654 All validity checks are turned on.
5655 @ifclear vms
5656 That is, @option{-gnatVa} is
5657 equivalent to @option{gnatVcdfimorst}.
5658 @end ifclear
5659
5660 @item -gnatVc
5661 @emph{Validity checks for copies.}
5662 @cindex @option{-gnatVc} (@command{gcc})
5663 The right hand side of assignments, and the initializing values of
5664 object declarations are validity checked.
5665
5666 @item -gnatVd
5667 @emph{Default (RM) validity checks.}
5668 @cindex @option{-gnatVd} (@command{gcc})
5669 Some validity checks are done by default following normal Ada semantics
5670 (RM 13.9.1 (9-11)).
5671 A check is done in case statements that the expression is within the range
5672 of the subtype. If it is not, Constraint_Error is raised.
5673 For assignments to array components, a check is done that the expression used
5674 as index is within the range. If it is not, Constraint_Error is raised.
5675 Both these validity checks may be turned off using switch @option{-gnatVD}.
5676 They are turned on by default. If @option{-gnatVD} is specified, a subsequent
5677 switch @option{-gnatVd} will leave the checks turned on.
5678 Switch @option{-gnatVD} should be used only if you are sure that all such
5679 expressions have valid values. If you use this switch and invalid values
5680 are present, then the program is erroneous, and wild jumps or memory
5681 overwriting may occur.
5682
5683 @item -gnatVe
5684 @emph{Validity checks for elementary components.}
5685 @cindex @option{-gnatVe} (@command{gcc})
5686 In the absence of this switch, assignments to record or array components are
5687 not validity checked, even if validity checks for assignments generally
5688 (@option{-gnatVc}) are turned on. In Ada, assignment of composite values do not
5689 require valid data, but assignment of individual components does. So for
5690 example, there is a difference between copying the elements of an array with a
5691 slice assignment, compared to assigning element by element in a loop. This
5692 switch allows you to turn off validity checking for components, even when they
5693 are assigned component by component.
5694
5695 @item -gnatVf
5696 @emph{Validity checks for floating-point values.}
5697 @cindex @option{-gnatVf} (@command{gcc})
5698 In the absence of this switch, validity checking occurs only for discrete
5699 values. If @option{-gnatVf} is specified, then validity checking also applies
5700 for floating-point values, and NaNs and infinities are considered invalid,
5701 as well as out of range values for constrained types. Note that this means
5702 that standard IEEE infinity mode is not allowed. The exact contexts
5703 in which floating-point values are checked depends on the setting of other
5704 options. For example,
5705 @option{^-gnatVif^VALIDITY_CHECKING=(IN_PARAMS,FLOATS)^} or
5706 @option{^-gnatVfi^VALIDITY_CHECKING=(FLOATS,IN_PARAMS)^}
5707 (the order does not matter) specifies that floating-point parameters of mode
5708 @code{in} should be validity checked.
5709
5710 @item -gnatVi
5711 @emph{Validity checks for @code{in} mode parameters}
5712 @cindex @option{-gnatVi} (@command{gcc})
5713 Arguments for parameters of mode @code{in} are validity checked in function
5714 and procedure calls at the point of call.
5715
5716 @item -gnatVm
5717 @emph{Validity checks for @code{in out} mode parameters.}
5718 @cindex @option{-gnatVm} (@command{gcc})
5719 Arguments for parameters of mode @code{in out} are validity checked in
5720 procedure calls at the point of call. The @code{'m'} here stands for
5721 modify, since this concerns parameters that can be modified by the call.
5722 Note that there is no specific option to test @code{out} parameters,
5723 but any reference within the subprogram will be tested in the usual
5724 manner, and if an invalid value is copied back, any reference to it
5725 will be subject to validity checking.
5726
5727 @item -gnatVn
5728 @emph{No validity checks.}
5729 @cindex @option{-gnatVn} (@command{gcc})
5730 This switch turns off all validity checking, including the default checking
5731 for case statements and left hand side subscripts. Note that the use of
5732 the switch @option{-gnatp} suppresses all run-time checks, including
5733 validity checks, and thus implies @option{-gnatVn}. When this switch
5734 is used, it cancels any other @option{-gnatV} previously issued.
5735
5736 @item -gnatVo
5737 @emph{Validity checks for operator and attribute operands.}
5738 @cindex @option{-gnatVo} (@command{gcc})
5739 Arguments for predefined operators and attributes are validity checked.
5740 This includes all operators in package @code{Standard},
5741 the shift operators defined as intrinsic in package @code{Interfaces}
5742 and operands for attributes such as @code{Pos}. Checks are also made
5743 on individual component values for composite comparisons, and on the
5744 expressions in type conversions and qualified expressions. Checks are
5745 also made on explicit ranges using @samp{..} (e.g.@: slices, loops etc).
5746
5747 @item -gnatVp
5748 @emph{Validity checks for parameters.}
5749 @cindex @option{-gnatVp} (@command{gcc})
5750 This controls the treatment of parameters within a subprogram (as opposed
5751 to @option{-gnatVi} and @option{-gnatVm} which control validity testing
5752 of parameters on a call. If either of these call options is used, then
5753 normally an assumption is made within a subprogram that the input arguments
5754 have been validity checking at the point of call, and do not need checking
5755 again within a subprogram). If @option{-gnatVp} is set, then this assumption
5756 is not made, and parameters are not assumed to be valid, so their validity
5757 will be checked (or rechecked) within the subprogram.
5758
5759 @item -gnatVr
5760 @emph{Validity checks for function returns.}
5761 @cindex @option{-gnatVr} (@command{gcc})
5762 The expression in @code{return} statements in functions is validity
5763 checked.
5764
5765 @item -gnatVs
5766 @emph{Validity checks for subscripts.}
5767 @cindex @option{-gnatVs} (@command{gcc})
5768 All subscripts expressions are checked for validity, whether they appear
5769 on the right side or left side (in default mode only left side subscripts
5770 are validity checked).
5771
5772 @item -gnatVt
5773 @emph{Validity checks for tests.}
5774 @cindex @option{-gnatVt} (@command{gcc})
5775 Expressions used as conditions in @code{if}, @code{while} or @code{exit}
5776 statements are checked, as well as guard expressions in entry calls.
5777
5778 @end table
5779
5780 @noindent
5781 The @option{-gnatV} switch may be followed by
5782 ^a string of letters^a list of options^
5783 to turn on a series of validity checking options.
5784 For example,
5785 @option{^-gnatVcr^/VALIDITY_CHECKING=(COPIES, RETURNS)^}
5786 specifies that in addition to the default validity checking, copies and
5787 function return expressions are to be validity checked.
5788 In order to make it easier
5789 to specify the desired combination of effects,
5790 @ifclear vms
5791 the upper case letters @code{CDFIMORST} may
5792 be used to turn off the corresponding lower case option.
5793 @end ifclear
5794 @ifset vms
5795 the prefix @code{NO} on an option turns off the corresponding validity
5796 checking:
5797 @itemize @bullet
5798 @item @code{NOCOPIES}
5799 @item @code{NODEFAULT}
5800 @item @code{NOFLOATS}
5801 @item @code{NOIN_PARAMS}
5802 @item @code{NOMOD_PARAMS}
5803 @item @code{NOOPERANDS}
5804 @item @code{NORETURNS}
5805 @item @code{NOSUBSCRIPTS}
5806 @item @code{NOTESTS}
5807 @end itemize
5808 @end ifset
5809 Thus
5810 @option{^-gnatVaM^/VALIDITY_CHECKING=(ALL, NOMOD_PARAMS)^}
5811 turns on all validity checking options except for
5812 checking of @code{@b{in out}} procedure arguments.
5813
5814 The specification of additional validity checking generates extra code (and
5815 in the case of @option{-gnatVa} the code expansion can be substantial).
5816 However, these additional checks can be very useful in detecting
5817 uninitialized variables, incorrect use of unchecked conversion, and other
5818 errors leading to invalid values. The use of pragma @code{Initialize_Scalars}
5819 is useful in conjunction with the extra validity checking, since this
5820 ensures that wherever possible uninitialized variables have invalid values.
5821
5822 See also the pragma @code{Validity_Checks} which allows modification of
5823 the validity checking mode at the program source level, and also allows for
5824 temporary disabling of validity checks.
5825
5826 @node Style Checking
5827 @subsection Style Checking
5828 @findex Style checking
5829
5830 @noindent
5831 The @option{-gnaty^x^(option,option,@dots{})^} switch
5832 @cindex @option{-gnaty} (@command{gcc})
5833 causes the compiler to
5834 enforce specified style rules. A limited set of style rules has been used
5835 in writing the GNAT sources themselves. This switch allows user programs
5836 to activate all or some of these checks. If the source program fails a
5837 specified style check, an appropriate warning message is given, preceded by
5838 the character sequence ``(style)''.
5839 @ifset vms
5840 @code{(option,option,@dots{})} is a sequence of keywords
5841 @end ifset
5842 @ifclear vms
5843 The string @var{x} is a sequence of letters or digits
5844 @end ifclear
5845 indicating the particular style
5846 checks to be performed. The following checks are defined:
5847
5848 @table @option
5849 @c !sort!
5850 @item 0-9
5851 @emph{Specify indentation level.}
5852 If a digit from 1-9 appears
5853 ^in the string after @option{-gnaty}^as an option for /STYLE_CHECKS^
5854 then proper indentation is checked, with the digit indicating the
5855 indentation level required. A value of zero turns off this style check.
5856 The general style of required indentation is as specified by
5857 the examples in the Ada Reference Manual. Full line comments must be
5858 aligned with the @code{--} starting on a column that is a multiple of
5859 the alignment level, or they may be aligned the same way as the following
5860 non-blank line (this is useful when full line comments appear in the middle
5861 of a statement.
5862
5863 @item ^a^ATTRIBUTE^
5864 @emph{Check attribute casing.}
5865 Attribute names, including the case of keywords such as @code{digits}
5866 used as attributes names, must be written in mixed case, that is, the
5867 initial letter and any letter following an underscore must be uppercase.
5868 All other letters must be lowercase.
5869
5870 @item ^A^ARRAY_INDEXES^
5871 @emph{Use of array index numbers in array attributes.}
5872 When using the array attributes First, Last, Range,
5873 or Length, the index number must be omitted for one-dimensional arrays
5874 and is required for multi-dimensional arrays.
5875
5876 @item ^b^BLANKS^
5877 @emph{Blanks not allowed at statement end.}
5878 Trailing blanks are not allowed at the end of statements. The purpose of this
5879 rule, together with h (no horizontal tabs), is to enforce a canonical format
5880 for the use of blanks to separate source tokens.
5881
5882 @item ^c^COMMENTS^
5883 @emph{Check comments.}
5884 Comments must meet the following set of rules:
5885
5886 @itemize @bullet
5887
5888 @item
5889 The ``@code{--}'' that starts the column must either start in column one,
5890 or else at least one blank must precede this sequence.
5891
5892 @item
5893 Comments that follow other tokens on a line must have at least one blank
5894 following the ``@code{--}'' at the start of the comment.
5895
5896 @item
5897 Full line comments must have two blanks following the ``@code{--}'' that
5898 starts the comment, with the following exceptions.
5899
5900 @item
5901 A line consisting only of the ``@code{--}'' characters, possibly preceded
5902 by blanks is permitted.
5903
5904 @item
5905 A comment starting with ``@code{--x}'' where @code{x} is a special character
5906 is permitted.
5907 This allows proper processing of the output generated by specialized tools
5908 including @command{gnatprep} (where ``@code{--!}'' is used) and the SPARK
5909 annotation
5910 language (where ``@code{--#}'' is used). For the purposes of this rule, a
5911 special character is defined as being in one of the ASCII ranges
5912 @code{16#21#@dots{}16#2F#} or @code{16#3A#@dots{}16#3F#}.
5913 Note that this usage is not permitted
5914 in GNAT implementation units (i.e., when @option{-gnatg} is used).
5915
5916 @item
5917 A line consisting entirely of minus signs, possibly preceded by blanks, is
5918 permitted. This allows the construction of box comments where lines of minus
5919 signs are used to form the top and bottom of the box.
5920
5921 @item
5922 A comment that starts and ends with ``@code{--}'' is permitted as long as at
5923 least one blank follows the initial ``@code{--}''. Together with the preceding
5924 rule, this allows the construction of box comments, as shown in the following
5925 example:
5926 @smallexample
5927 ---------------------------
5928 -- This is a box comment --
5929 -- with two text lines. --
5930 ---------------------------
5931 @end smallexample
5932 @end itemize
5933
5934 @item ^d^DOS_LINE_ENDINGS^
5935 @emph{Check no DOS line terminators present.}
5936 All lines must be terminated by a single ASCII.LF
5937 character (in particular the DOS line terminator sequence CR/LF is not
5938 allowed).
5939
5940 @item ^e^END^
5941 @emph{Check end/exit labels.}
5942 Optional labels on @code{end} statements ending subprograms and on
5943 @code{exit} statements exiting named loops, are required to be present.
5944
5945 @item ^f^VTABS^
5946 @emph{No form feeds or vertical tabs.}
5947 Neither form feeds nor vertical tab characters are permitted
5948 in the source text.
5949
5950 @item ^g^GNAT^
5951 @emph{GNAT style mode}
5952 The set of style check switches is set to match that used by the GNAT sources.
5953 This may be useful when developing code that is eventually intended to be
5954 incorporated into GNAT. For further details, see GNAT sources.
5955
5956 @item ^h^HTABS^
5957 @emph{No horizontal tabs.}
5958 Horizontal tab characters are not permitted in the source text.
5959 Together with the b (no blanks at end of line) check, this
5960 enforces a canonical form for the use of blanks to separate
5961 source tokens.
5962
5963 @item ^i^IF_THEN^
5964 @emph{Check if-then layout.}
5965 The keyword @code{then} must appear either on the same
5966 line as corresponding @code{if}, or on a line on its own, lined
5967 up under the @code{if} with at least one non-blank line in between
5968 containing all or part of the condition to be tested.
5969
5970 @item ^I^IN_MODE^
5971 @emph{check mode IN keywords}
5972 Mode @code{in} (the default mode) is not
5973 allowed to be given explicitly. @code{in out} is fine,
5974 but not @code{in} on its own.
5975
5976 @item ^k^KEYWORD^
5977 @emph{Check keyword casing.}
5978 All keywords must be in lower case (with the exception of keywords
5979 such as @code{digits} used as attribute names to which this check
5980 does not apply).
5981
5982 @item ^l^LAYOUT^
5983 @emph{Check layout.}
5984 Layout of statement and declaration constructs must follow the
5985 recommendations in the Ada Reference Manual, as indicated by the
5986 form of the syntax rules. For example an @code{else} keyword must
5987 be lined up with the corresponding @code{if} keyword.
5988
5989 There are two respects in which the style rule enforced by this check
5990 option are more liberal than those in the Ada Reference Manual. First
5991 in the case of record declarations, it is permissible to put the
5992 @code{record} keyword on the same line as the @code{type} keyword, and
5993 then the @code{end} in @code{end record} must line up under @code{type}.
5994 This is also permitted when the type declaration is split on two lines.
5995 For example, any of the following three layouts is acceptable:
5996
5997 @smallexample @c ada
5998 @cartouche
5999 type q is record
6000 a : integer;
6001 b : integer;
6002 end record;
6003
6004 type q is
6005 record
6006 a : integer;
6007 b : integer;
6008 end record;
6009
6010 type q is
6011 record
6012 a : integer;
6013 b : integer;
6014 end record;
6015
6016 @end cartouche
6017 @end smallexample
6018
6019 @noindent
6020 Second, in the case of a block statement, a permitted alternative
6021 is to put the block label on the same line as the @code{declare} or
6022 @code{begin} keyword, and then line the @code{end} keyword up under
6023 the block label. For example both the following are permitted:
6024
6025 @smallexample @c ada
6026 @cartouche
6027 Block : declare
6028 A : Integer := 3;
6029 begin
6030 Proc (A, A);
6031 end Block;
6032
6033 Block :
6034 declare
6035 A : Integer := 3;
6036 begin
6037 Proc (A, A);
6038 end Block;
6039 @end cartouche
6040 @end smallexample
6041
6042 @noindent
6043 The same alternative format is allowed for loops. For example, both of
6044 the following are permitted:
6045
6046 @smallexample @c ada
6047 @cartouche
6048 Clear : while J < 10 loop
6049 A (J) := 0;
6050 end loop Clear;
6051
6052 Clear :
6053 while J < 10 loop
6054 A (J) := 0;
6055 end loop Clear;
6056 @end cartouche
6057 @end smallexample
6058
6059 @item ^Lnnn^MAX_NESTING=nnn^
6060 @emph{Set maximum nesting level}
6061 The maximum level of nesting of constructs (including subprograms, loops,
6062 blocks, packages, and conditionals) may not exceed the given value
6063 @option{nnn}. A value of zero disconnects this style check.
6064
6065 @item ^m^LINE_LENGTH^
6066 @emph{Check maximum line length.}
6067 The length of source lines must not exceed 79 characters, including
6068 any trailing blanks. The value of 79 allows convenient display on an
6069 80 character wide device or window, allowing for possible special
6070 treatment of 80 character lines. Note that this count is of
6071 characters in the source text. This means that a tab character counts
6072 as one character in this count but a wide character sequence counts as
6073 a single character (however many bytes are needed in the encoding).
6074
6075 @item ^Mnnn^MAX_LENGTH=nnn^
6076 @emph{Set maximum line length.}
6077 The length of lines must not exceed the
6078 given value @option{nnn}. The maximum value that can be specified is 32767.
6079
6080 @item ^n^STANDARD_CASING^
6081 @emph{Check casing of entities in Standard.}
6082 Any identifier from Standard must be cased
6083 to match the presentation in the Ada Reference Manual (for example,
6084 @code{Integer} and @code{ASCII.NUL}).
6085
6086 @item ^N^NONE^
6087 @emph{Turn off all style checks}
6088 All style check options are turned off.
6089
6090 @item ^o^ORDERED_SUBPROGRAMS^
6091 @emph{Check order of subprogram bodies.}
6092 All subprogram bodies in a given scope
6093 (e.g.@: a package body) must be in alphabetical order. The ordering
6094 rule uses normal Ada rules for comparing strings, ignoring casing
6095 of letters, except that if there is a trailing numeric suffix, then
6096 the value of this suffix is used in the ordering (e.g.@: Junk2 comes
6097 before Junk10).
6098
6099 @item ^O^OVERRIDING_INDICATORS^
6100 @emph{Check that overriding subprograms are explicitly marked as such.}
6101 The declaration of a primitive operation of a type extension that overrides
6102 an inherited operation must carry an overriding indicator.
6103
6104 @item ^p^PRAGMA^
6105 @emph{Check pragma casing.}
6106 Pragma names must be written in mixed case, that is, the
6107 initial letter and any letter following an underscore must be uppercase.
6108 All other letters must be lowercase.
6109
6110 @item ^r^REFERENCES^
6111 @emph{Check references.}
6112 All identifier references must be cased in the same way as the
6113 corresponding declaration. No specific casing style is imposed on
6114 identifiers. The only requirement is for consistency of references
6115 with declarations.
6116
6117 @item ^S^STATEMENTS_AFTER_THEN_ELSE^
6118 @emph{Check no statements after THEN/ELSE.}
6119 No statements are allowed
6120 on the same line as a THEN or ELSE keyword following the
6121 keyword in an IF statement. OR ELSE and AND THEN are not affected,
6122 and a special exception allows a pragma to appear after ELSE.
6123
6124 @item ^s^SPECS^
6125 @emph{Check separate specs.}
6126 Separate declarations (``specs'') are required for subprograms (a
6127 body is not allowed to serve as its own declaration). The only
6128 exception is that parameterless library level procedures are
6129 not required to have a separate declaration. This exception covers
6130 the most frequent form of main program procedures.
6131
6132 @item ^t^TOKEN^
6133 @emph{Check token spacing.}
6134 The following token spacing rules are enforced:
6135
6136 @itemize @bullet
6137
6138 @item
6139 The keywords @code{@b{abs}} and @code{@b{not}} must be followed by a space.
6140
6141 @item
6142 The token @code{=>} must be surrounded by spaces.
6143
6144 @item
6145 The token @code{<>} must be preceded by a space or a left parenthesis.
6146
6147 @item
6148 Binary operators other than @code{**} must be surrounded by spaces.
6149 There is no restriction on the layout of the @code{**} binary operator.
6150
6151 @item
6152 Colon must be surrounded by spaces.
6153
6154 @item
6155 Colon-equal (assignment, initialization) must be surrounded by spaces.
6156
6157 @item
6158 Comma must be the first non-blank character on the line, or be
6159 immediately preceded by a non-blank character, and must be followed
6160 by a space.
6161
6162 @item
6163 If the token preceding a left parenthesis ends with a letter or digit, then
6164 a space must separate the two tokens.
6165
6166 @item
6167 A right parenthesis must either be the first non-blank character on
6168 a line, or it must be preceded by a non-blank character.
6169
6170 @item
6171 A semicolon must not be preceded by a space, and must not be followed by
6172 a non-blank character.
6173
6174 @item
6175 A unary plus or minus may not be followed by a space.
6176
6177 @item
6178 A vertical bar must be surrounded by spaces.
6179 @end itemize
6180
6181 @item ^u^UNNECESSARY_BLANK_LINES^
6182 @emph{Check unnecessary blank lines.}
6183 Unnecessary blank lines are not allowed. A blank line is considered
6184 unnecessary if it appears at the end of the file, or if more than
6185 one blank line occurs in sequence.
6186
6187 @item ^x^XTRA_PARENS^
6188 @emph{Check extra parentheses.}
6189 Unnecessary extra level of parentheses (C-style) are not allowed
6190 around conditions in @code{if} statements, @code{while} statements and
6191 @code{exit} statements.
6192
6193 @item ^y^ALL_BUILTIN^
6194 @emph{Set all standard style check options}
6195 This is equivalent to @code{gnaty3aAbcefhiklmnprst}, that is all checking
6196 options enabled with the exception of @option{-gnatyo}, @option{-gnatyI},
6197 @option{-gnatyS}, @option{-gnatyLnnn},
6198 @option{-gnatyd}, @option{-gnatyu}, and @option{-gnatyx}.
6199
6200 @ifclear vms
6201 @item -
6202 @emph{Remove style check options}
6203 This causes any subsequent options in the string to act as canceling the
6204 corresponding style check option. To cancel maximum nesting level control,
6205 use @option{L} parameter witout any integer value after that, because any
6206 digit following @option{-} in the parameter string of the @option{-gnaty}
6207 option will be threated as canceling indentation check. The same is true
6208 for @option{M} parameter. @option{y} and @option{N} parameters are not
6209 allowed after @option{-}.
6210
6211 @item +
6212 This causes any subsequent options in the string to enable the corresponding
6213 style check option. That is, it cancels the effect of a previous ^-^REMOVE^,
6214 if any.
6215 @end ifclear
6216
6217 @ifset vms
6218 @item NOxxx
6219 @emph{Removing style check options}
6220 If the name of a style check is preceded by @option{NO} then the corresponding
6221 style check is turned off. For example @option{NOCOMMENTS} turns off style
6222 checking for comments.
6223 @end ifset
6224 @end table
6225
6226 @noindent
6227 In the above rules, appearing in column one is always permitted, that is,
6228 counts as meeting either a requirement for a required preceding space,
6229 or as meeting a requirement for no preceding space.
6230
6231 Appearing at the end of a line is also always permitted, that is, counts
6232 as meeting either a requirement for a following space, or as meeting
6233 a requirement for no following space.
6234
6235 @noindent
6236 If any of these style rules is violated, a message is generated giving
6237 details on the violation. The initial characters of such messages are
6238 always ``@code{(style)}''. Note that these messages are treated as warning
6239 messages, so they normally do not prevent the generation of an object
6240 file. The @option{-gnatwe} switch can be used to treat warning messages,
6241 including style messages, as fatal errors.
6242
6243 The switch
6244 @ifclear vms
6245 @option{-gnaty} on its own (that is not
6246 followed by any letters or digits), then the effect is equivalent
6247 to the use of @option{-gnatyy}, as described above, that is all
6248 built-in standard style check options are enabled.
6249
6250 @end ifclear
6251 @ifset vms
6252 /STYLE_CHECKS=ALL_BUILTIN enables all checking options with
6253 the exception of ORDERED_SUBPROGRAMS, UNNECESSARY_BLANK_LINES,
6254 XTRA_PARENS, and DOS_LINE_ENDINGS. In addition
6255 @end ifset
6256
6257
6258
6259 The switch
6260 @ifclear vms
6261 @option{-gnatyN}
6262 @end ifclear
6263 @ifset vms
6264 /STYLE_CHECKS=NONE
6265 @end ifset
6266 clears any previously set style checks.
6267
6268 @node Run-Time Checks
6269 @subsection Run-Time Checks
6270 @cindex Division by zero
6271 @cindex Access before elaboration
6272 @cindex Checks, division by zero
6273 @cindex Checks, access before elaboration
6274 @cindex Checks, stack overflow checking
6275
6276 @noindent
6277 By default, the following checks are suppressed: integer overflow
6278 checks, stack overflow checks, and checks for access before
6279 elaboration on subprogram calls. All other checks, including range
6280 checks and array bounds checks, are turned on by default. The
6281 following @command{gcc} switches refine this default behavior.
6282
6283 @table @option
6284 @c !sort!
6285 @item -gnatp
6286 @cindex @option{-gnatp} (@command{gcc})
6287 @cindex Suppressing checks
6288 @cindex Checks, suppressing
6289 @findex Suppress
6290 Suppress all run-time checks as though @code{pragma Suppress (All_checks)}
6291 had been present in the source. Validity checks are also suppressed (in
6292 other words @option{-gnatp} also implies @option{-gnatVn}.
6293 Use this switch to improve the performance
6294 of the code at the expense of safety in the presence of invalid data or
6295 program bugs.
6296
6297 Note that when checks are suppressed, the compiler is allowed, but not
6298 required, to omit the checking code. If the run-time cost of the
6299 checking code is zero or near-zero, the compiler will generate it even
6300 if checks are suppressed. In particular, if the compiler can prove
6301 that a certain check will necessarily fail, it will generate code to
6302 do an unconditional ``raise'', even if checks are suppressed. The
6303 compiler warns in this case.
6304
6305 Of course, run-time checks are omitted whenever the compiler can prove
6306 that they will not fail, whether or not checks are suppressed.
6307
6308 Note that if you suppress a check that would have failed, program
6309 execution is erroneous, which means the behavior is totally
6310 unpredictable. The program might crash, or print wrong answers, or
6311 do anything else. It might even do exactly what you wanted it to do
6312 (and then it might start failing mysteriously next week or next
6313 year). The compiler will generate code based on the assumption that
6314 the condition being checked is true, which can result in disaster if
6315 that assumption is wrong.
6316
6317 @item -gnato
6318 @cindex @option{-gnato} (@command{gcc})
6319 @cindex Overflow checks
6320 @cindex Check, overflow
6321 Enables overflow checking for integer operations.
6322 This causes GNAT to generate slower and larger executable
6323 programs by adding code to check for overflow (resulting in raising
6324 @code{Constraint_Error} as required by standard Ada
6325 semantics). These overflow checks correspond to situations in which
6326 the true value of the result of an operation may be outside the base
6327 range of the result type. The following example shows the distinction:
6328
6329 @smallexample @c ada
6330 X1 : Integer := "Integer'Last";
6331 X2 : Integer range 1 .. 5 := "5";
6332 X3 : Integer := "Integer'Last";
6333 X4 : Integer range 1 .. 5 := "5";
6334 F : Float := "2.0E+20";
6335 @dots{}
6336 X1 := X1 + 1;
6337 X2 := X2 + 1;
6338 X3 := Integer (F);
6339 X4 := Integer (F);
6340 @end smallexample
6341
6342 @noindent
6343 Note that if explicit values are assigned at compile time, the
6344 compiler may be able to detect overflow at compile time, in which case
6345 no actual run-time checking code is required, and Constraint_Error
6346 will be raised unconditionally, with or without
6347 @option{-gnato}. That's why the assigned values in the above fragment
6348 are in quotes, the meaning is "assign a value not known to the
6349 compiler that happens to be equal to ...". The remaining discussion
6350 assumes that the compiler cannot detect the values at compile time.
6351
6352 Here the first addition results in a value that is outside the base range
6353 of Integer, and hence requires an overflow check for detection of the
6354 constraint error. Thus the first assignment to @code{X1} raises a
6355 @code{Constraint_Error} exception only if @option{-gnato} is set.
6356
6357 The second increment operation results in a violation of the explicit
6358 range constraint; such range checks are performed by default, and are
6359 unaffected by @option{-gnato}.
6360
6361 The two conversions of @code{F} both result in values that are outside
6362 the base range of type @code{Integer} and thus will raise
6363 @code{Constraint_Error} exceptions only if @option{-gnato} is used.
6364 The fact that the result of the second conversion is assigned to
6365 variable @code{X4} with a restricted range is irrelevant, since the problem
6366 is in the conversion, not the assignment.
6367
6368 Basically the rule is that in the default mode (@option{-gnato} not
6369 used), the generated code assures that all integer variables stay
6370 within their declared ranges, or within the base range if there is
6371 no declared range. This prevents any serious problems like indexes
6372 out of range for array operations.
6373
6374 What is not checked in default mode is an overflow that results in
6375 an in-range, but incorrect value. In the above example, the assignments
6376 to @code{X1}, @code{X2}, @code{X3} all give results that are within the
6377 range of the target variable, but the result is wrong in the sense that
6378 it is too large to be represented correctly. Typically the assignment
6379 to @code{X1} will result in wrap around to the largest negative number.
6380 The conversions of @code{F} will result in some @code{Integer} value
6381 and if that integer value is out of the @code{X4} range then the
6382 subsequent assignment would generate an exception.
6383
6384 @findex Machine_Overflows
6385 Note that the @option{-gnato} switch does not affect the code generated
6386 for any floating-point operations; it applies only to integer
6387 semantics).
6388 For floating-point, GNAT has the @code{Machine_Overflows}
6389 attribute set to @code{False} and the normal mode of operation is to
6390 generate IEEE NaN and infinite values on overflow or invalid operations
6391 (such as dividing 0.0 by 0.0).
6392
6393 The reason that we distinguish overflow checking from other kinds of
6394 range constraint checking is that a failure of an overflow check, unlike
6395 for example the failure of a range check, can result in an incorrect
6396 value, but cannot cause random memory destruction (like an out of range
6397 subscript), or a wild jump (from an out of range case value). Overflow
6398 checking is also quite expensive in time and space, since in general it
6399 requires the use of double length arithmetic.
6400
6401 Note again that @option{-gnato} is off by default, so overflow checking is
6402 not performed in default mode. This means that out of the box, with the
6403 default settings, GNAT does not do all the checks expected from the
6404 language description in the Ada Reference Manual. If you want all constraint
6405 checks to be performed, as described in this Manual, then you must
6406 explicitly use the -gnato switch either on the @command{gnatmake} or
6407 @command{gcc} command.
6408
6409 @item -gnatE
6410 @cindex @option{-gnatE} (@command{gcc})
6411 @cindex Elaboration checks
6412 @cindex Check, elaboration
6413 Enables dynamic checks for access-before-elaboration
6414 on subprogram calls and generic instantiations.
6415 Note that @option{-gnatE} is not necessary for safety, because in the
6416 default mode, GNAT ensures statically that the checks would not fail.
6417 For full details of the effect and use of this switch,
6418 @xref{Compiling Using gcc}.
6419
6420 @item -fstack-check
6421 @cindex @option{-fstack-check} (@command{gcc})
6422 @cindex Stack Overflow Checking
6423 @cindex Checks, stack overflow checking
6424 Activates stack overflow checking. For full details of the effect and use of
6425 this switch see @ref{Stack Overflow Checking}.
6426 @end table
6427
6428 @findex Unsuppress
6429 @noindent
6430 The setting of these switches only controls the default setting of the
6431 checks. You may modify them using either @code{Suppress} (to remove
6432 checks) or @code{Unsuppress} (to add back suppressed checks) pragmas in
6433 the program source.
6434
6435 @node Using gcc for Syntax Checking
6436 @subsection Using @command{gcc} for Syntax Checking
6437 @table @option
6438 @item -gnats
6439 @cindex @option{-gnats} (@command{gcc})
6440 @ifclear vms
6441
6442 @noindent
6443 The @code{s} stands for ``syntax''.
6444 @end ifclear
6445
6446 Run GNAT in syntax checking only mode. For
6447 example, the command
6448
6449 @smallexample
6450 $ gcc -c -gnats x.adb
6451 @end smallexample
6452
6453 @noindent
6454 compiles file @file{x.adb} in syntax-check-only mode. You can check a
6455 series of files in a single command
6456 @ifclear vms
6457 , and can use wild cards to specify such a group of files.
6458 Note that you must specify the @option{-c} (compile
6459 only) flag in addition to the @option{-gnats} flag.
6460 @end ifclear
6461 .
6462 You may use other switches in conjunction with @option{-gnats}. In
6463 particular, @option{-gnatl} and @option{-gnatv} are useful to control the
6464 format of any generated error messages.
6465
6466 When the source file is empty or contains only empty lines and/or comments,
6467 the output is a warning:
6468
6469 @smallexample
6470 $ gcc -c -gnats -x ada toto.txt
6471 toto.txt:1:01: warning: empty file, contains no compilation units
6472 $
6473 @end smallexample
6474
6475 Otherwise, the output is simply the error messages, if any. No object file or
6476 ALI file is generated by a syntax-only compilation. Also, no units other
6477 than the one specified are accessed. For example, if a unit @code{X}
6478 @code{with}'s a unit @code{Y}, compiling unit @code{X} in syntax
6479 check only mode does not access the source file containing unit
6480 @code{Y}.
6481
6482 @cindex Multiple units, syntax checking
6483 Normally, GNAT allows only a single unit in a source file. However, this
6484 restriction does not apply in syntax-check-only mode, and it is possible
6485 to check a file containing multiple compilation units concatenated
6486 together. This is primarily used by the @code{gnatchop} utility
6487 (@pxref{Renaming Files Using gnatchop}).
6488 @end table
6489
6490 @node Using gcc for Semantic Checking
6491 @subsection Using @command{gcc} for Semantic Checking
6492 @table @option
6493 @item -gnatc
6494 @cindex @option{-gnatc} (@command{gcc})
6495
6496 @ifclear vms
6497 @noindent
6498 The @code{c} stands for ``check''.
6499 @end ifclear
6500 Causes the compiler to operate in semantic check mode,
6501 with full checking for all illegalities specified in the
6502 Ada Reference Manual, but without generation of any object code
6503 (no object file is generated).
6504
6505 Because dependent files must be accessed, you must follow the GNAT
6506 semantic restrictions on file structuring to operate in this mode:
6507
6508 @itemize @bullet
6509 @item
6510 The needed source files must be accessible
6511 (@pxref{Search Paths and the Run-Time Library (RTL)}).
6512
6513 @item
6514 Each file must contain only one compilation unit.
6515
6516 @item
6517 The file name and unit name must match (@pxref{File Naming Rules}).
6518 @end itemize
6519
6520 The output consists of error messages as appropriate. No object file is
6521 generated. An @file{ALI} file is generated for use in the context of
6522 cross-reference tools, but this file is marked as not being suitable
6523 for binding (since no object file is generated).
6524 The checking corresponds exactly to the notion of
6525 legality in the Ada Reference Manual.
6526
6527 Any unit can be compiled in semantics-checking-only mode, including
6528 units that would not normally be compiled (subunits,
6529 and specifications where a separate body is present).
6530 @end table
6531
6532 @node Compiling Different Versions of Ada
6533 @subsection Compiling Different Versions of Ada
6534
6535 @noindent
6536 The switches described in this section allow you to explicitly specify
6537 the version of the Ada language that your programs are written in.
6538 By default @value{EDITION} assumes @value{DEFAULTLANGUAGEVERSION},
6539 but you can also specify @value{NONDEFAULTLANGUAGEVERSION} or
6540 indicate Ada 83 compatibility mode.
6541
6542 @table @option
6543 @cindex Compatibility with Ada 83
6544
6545 @item -gnat83 (Ada 83 Compatibility Mode)
6546 @cindex @option{-gnat83} (@command{gcc})
6547 @cindex ACVC, Ada 83 tests
6548 @cindex Ada 83 mode
6549
6550 @noindent
6551 Although GNAT is primarily an Ada 95 / Ada 2005 compiler, this switch
6552 specifies that the program is to be compiled in Ada 83 mode. With
6553 @option{-gnat83}, GNAT rejects most post-Ada 83 extensions and applies Ada 83
6554 semantics where this can be done easily.
6555 It is not possible to guarantee this switch does a perfect
6556 job; some subtle tests, such as are
6557 found in earlier ACVC tests (and that have been removed from the ACATS suite
6558 for Ada 95), might not compile correctly.
6559 Nevertheless, this switch may be useful in some circumstances, for example
6560 where, due to contractual reasons, existing code needs to be maintained
6561 using only Ada 83 features.
6562
6563 With few exceptions (most notably the need to use @code{<>} on
6564 @cindex Generic formal parameters
6565 unconstrained generic formal parameters, the use of the new Ada 95 / Ada 2005
6566 reserved words, and the use of packages
6567 with optional bodies), it is not necessary to specify the
6568 @option{-gnat83} switch when compiling Ada 83 programs, because, with rare
6569 exceptions, Ada 95 and Ada 2005 are upwardly compatible with Ada 83. Thus
6570 a correct Ada 83 program is usually also a correct program
6571 in these later versions of the language standard.
6572 For further information, please refer to @ref{Compatibility and Porting Guide}.
6573
6574 @item -gnat95 (Ada 95 mode)
6575 @cindex @option{-gnat95} (@command{gcc})
6576 @cindex Ada 95 mode
6577
6578 @noindent
6579 This switch directs the compiler to implement the Ada 95 version of the
6580 language.
6581 Since Ada 95 is almost completely upwards
6582 compatible with Ada 83, Ada 83 programs may generally be compiled using
6583 this switch (see the description of the @option{-gnat83} switch for further
6584 information about Ada 83 mode).
6585 If an Ada 2005 program is compiled in Ada 95 mode,
6586 uses of the new Ada 2005 features will cause error
6587 messages or warnings.
6588
6589 This switch also can be used to cancel the effect of a previous
6590 @option{-gnat83} or @option{-gnat05} switch earlier in the command line.
6591
6592 @item -gnat05 (Ada 2005 mode)
6593 @cindex @option{-gnat05} (@command{gcc})
6594 @cindex Ada 2005 mode
6595
6596 @noindent
6597 This switch directs the compiler to implement the Ada 2005 version of the
6598 language.
6599 Since Ada 2005 is almost completely upwards
6600 compatible with Ada 95 (and thus also with Ada 83), Ada 83 and Ada 95 programs
6601 may generally be compiled using this switch (see the description of the
6602 @option{-gnat83} and @option{-gnat95} switches for further
6603 information).
6604
6605 For information about the approved ``Ada Issues'' that have been incorporated
6606 into Ada 2005, see @url{http://www.ada-auth.org/cgi-bin/cvsweb.cgi/AIs}.
6607 Included with GNAT releases is a file @file{features-ada0y} that describes
6608 the set of implemented Ada 2005 features.
6609 @end table
6610
6611
6612 @node Character Set Control
6613 @subsection Character Set Control
6614 @table @option
6615 @item ^-gnati^/IDENTIFIER_CHARACTER_SET=^@var{c}
6616 @cindex @option{^-gnati^/IDENTIFIER_CHARACTER_SET^} (@command{gcc})
6617
6618 @noindent
6619 Normally GNAT recognizes the Latin-1 character set in source program
6620 identifiers, as described in the Ada Reference Manual.
6621 This switch causes
6622 GNAT to recognize alternate character sets in identifiers. @var{c} is a
6623 single character ^^or word^ indicating the character set, as follows:
6624
6625 @table @code
6626 @item 1
6627 ISO 8859-1 (Latin-1) identifiers
6628
6629 @item 2
6630 ISO 8859-2 (Latin-2) letters allowed in identifiers
6631
6632 @item 3
6633 ISO 8859-3 (Latin-3) letters allowed in identifiers
6634
6635 @item 4
6636 ISO 8859-4 (Latin-4) letters allowed in identifiers
6637
6638 @item 5
6639 ISO 8859-5 (Cyrillic) letters allowed in identifiers
6640
6641 @item 9
6642 ISO 8859-15 (Latin-9) letters allowed in identifiers
6643
6644 @item ^p^PC^
6645 IBM PC letters (code page 437) allowed in identifiers
6646
6647 @item ^8^PC850^
6648 IBM PC letters (code page 850) allowed in identifiers
6649
6650 @item ^f^FULL_UPPER^
6651 Full upper-half codes allowed in identifiers
6652
6653 @item ^n^NO_UPPER^
6654 No upper-half codes allowed in identifiers
6655
6656 @item ^w^WIDE^
6657 Wide-character codes (that is, codes greater than 255)
6658 allowed in identifiers
6659 @end table
6660
6661 @xref{Foreign Language Representation}, for full details on the
6662 implementation of these character sets.
6663
6664 @item ^-gnatW^/WIDE_CHARACTER_ENCODING=^@var{e}
6665 @cindex @option{^-gnatW^/WIDE_CHARACTER_ENCODING^} (@command{gcc})
6666 Specify the method of encoding for wide characters.
6667 @var{e} is one of the following:
6668
6669 @table @code
6670
6671 @item ^h^HEX^
6672 Hex encoding (brackets coding also recognized)
6673
6674 @item ^u^UPPER^
6675 Upper half encoding (brackets encoding also recognized)
6676
6677 @item ^s^SHIFT_JIS^
6678 Shift/JIS encoding (brackets encoding also recognized)
6679
6680 @item ^e^EUC^
6681 EUC encoding (brackets encoding also recognized)
6682
6683 @item ^8^UTF8^
6684 UTF-8 encoding (brackets encoding also recognized)
6685
6686 @item ^b^BRACKETS^
6687 Brackets encoding only (default value)
6688 @end table
6689 For full details on these encoding
6690 methods see @ref{Wide Character Encodings}.
6691 Note that brackets coding is always accepted, even if one of the other
6692 options is specified, so for example @option{-gnatW8} specifies that both
6693 brackets and UTF-8 encodings will be recognized. The units that are
6694 with'ed directly or indirectly will be scanned using the specified
6695 representation scheme, and so if one of the non-brackets scheme is
6696 used, it must be used consistently throughout the program. However,
6697 since brackets encoding is always recognized, it may be conveniently
6698 used in standard libraries, allowing these libraries to be used with
6699 any of the available coding schemes.
6700 scheme.
6701
6702 If no @option{-gnatW?} parameter is present, then the default
6703 representation is normally Brackets encoding only. However, if the
6704 first three characters of the file are 16#EF# 16#BB# 16#BF# (the standard
6705 byte order mark or BOM for UTF-8), then these three characters are
6706 skipped and the default representation for the file is set to UTF-8.
6707
6708 Note that the wide character representation that is specified (explicitly
6709 or by default) for the main program also acts as the default encoding used
6710 for Wide_Text_IO files if not specifically overridden by a WCEM form
6711 parameter.
6712
6713 @end table
6714 @node File Naming Control
6715 @subsection File Naming Control
6716
6717 @table @option
6718 @item ^-gnatk^/FILE_NAME_MAX_LENGTH=^@var{n}
6719 @cindex @option{-gnatk} (@command{gcc})
6720 Activates file name ``krunching''. @var{n}, a decimal integer in the range
6721 1-999, indicates the maximum allowable length of a file name (not
6722 including the @file{.ads} or @file{.adb} extension). The default is not
6723 to enable file name krunching.
6724
6725 For the source file naming rules, @xref{File Naming Rules}.
6726 @end table
6727
6728 @node Subprogram Inlining Control
6729 @subsection Subprogram Inlining Control
6730
6731 @table @option
6732 @c !sort!
6733 @item -gnatn
6734 @cindex @option{-gnatn} (@command{gcc})
6735 @ifclear vms
6736 The @code{n} here is intended to suggest the first syllable of the
6737 word ``inline''.
6738 @end ifclear
6739 GNAT recognizes and processes @code{Inline} pragmas. However, for the
6740 inlining to actually occur, optimization must be enabled. To enable
6741 inlining of subprograms specified by pragma @code{Inline},
6742 you must also specify this switch.
6743 In the absence of this switch, GNAT does not attempt
6744 inlining and does not need to access the bodies of
6745 subprograms for which @code{pragma Inline} is specified if they are not
6746 in the current unit.
6747
6748 If you specify this switch the compiler will access these bodies,
6749 creating an extra source dependency for the resulting object file, and
6750 where possible, the call will be inlined.
6751 For further details on when inlining is possible
6752 see @ref{Inlining of Subprograms}.
6753
6754 @item -gnatN
6755 @cindex @option{-gnatN} (@command{gcc})
6756 This switch activates front-end inlining which also
6757 generates additional dependencies.
6758
6759 When using a gcc-based back end (in practice this means using any version
6760 of GNAT other than the JGNAT, .NET or GNAAMP versions), then the use of
6761 @option{-gnatN} is deprecated, and the use of @option{-gnatn} is preferred.
6762 Historically front end inlining was more extensive than the gcc back end
6763 inlining, but that is no longer the case.
6764 @end table
6765
6766 @node Auxiliary Output Control
6767 @subsection Auxiliary Output Control
6768
6769 @table @option
6770 @item -gnatt
6771 @cindex @option{-gnatt} (@command{gcc})
6772 @cindex Writing internal trees
6773 @cindex Internal trees, writing to file
6774 Causes GNAT to write the internal tree for a unit to a file (with the
6775 extension @file{.adt}.
6776 This not normally required, but is used by separate analysis tools.
6777 Typically
6778 these tools do the necessary compilations automatically, so you should
6779 not have to specify this switch in normal operation.
6780
6781 @item -gnatu
6782 @cindex @option{-gnatu} (@command{gcc})
6783 Print a list of units required by this compilation on @file{stdout}.
6784 The listing includes all units on which the unit being compiled depends
6785 either directly or indirectly.
6786
6787 @ifclear vms
6788 @item -pass-exit-codes
6789 @cindex @option{-pass-exit-codes} (@command{gcc})
6790 If this switch is not used, the exit code returned by @command{gcc} when
6791 compiling multiple files indicates whether all source files have
6792 been successfully used to generate object files or not.
6793
6794 When @option{-pass-exit-codes} is used, @command{gcc} exits with an extended
6795 exit status and allows an integrated development environment to better
6796 react to a compilation failure. Those exit status are:
6797
6798 @table @asis
6799 @item 5
6800 There was an error in at least one source file.
6801 @item 3
6802 At least one source file did not generate an object file.
6803 @item 2
6804 The compiler died unexpectedly (internal error for example).
6805 @item 0
6806 An object file has been generated for every source file.
6807 @end table
6808 @end ifclear
6809 @end table
6810
6811 @node Debugging Control
6812 @subsection Debugging Control
6813
6814 @table @option
6815 @c !sort!
6816 @cindex Debugging options
6817 @ifclear vms
6818 @item -gnatd@var{x}
6819 @cindex @option{-gnatd} (@command{gcc})
6820 Activate internal debugging switches. @var{x} is a letter or digit, or
6821 string of letters or digits, which specifies the type of debugging
6822 outputs desired. Normally these are used only for internal development
6823 or system debugging purposes. You can find full documentation for these
6824 switches in the body of the @code{Debug} unit in the compiler source
6825 file @file{debug.adb}.
6826 @end ifclear
6827
6828 @item -gnatG[=nn]
6829 @cindex @option{-gnatG} (@command{gcc})
6830 This switch causes the compiler to generate auxiliary output containing
6831 a pseudo-source listing of the generated expanded code. Like most Ada
6832 compilers, GNAT works by first transforming the high level Ada code into
6833 lower level constructs. For example, tasking operations are transformed
6834 into calls to the tasking run-time routines. A unique capability of GNAT
6835 is to list this expanded code in a form very close to normal Ada source.
6836 This is very useful in understanding the implications of various Ada
6837 usage on the efficiency of the generated code. There are many cases in
6838 Ada (e.g.@: the use of controlled types), where simple Ada statements can
6839 generate a lot of run-time code. By using @option{-gnatG} you can identify
6840 these cases, and consider whether it may be desirable to modify the coding
6841 approach to improve efficiency.
6842
6843 The optional parameter @code{nn} if present after -gnatG specifies an
6844 alternative maximum line length that overrides the normal default of 72.
6845 This value is in the range 40-999999, values less than 40 being silently
6846 reset to 40. The equal sign is optional.
6847
6848 The format of the output is very similar to standard Ada source, and is
6849 easily understood by an Ada programmer. The following special syntactic
6850 additions correspond to low level features used in the generated code that
6851 do not have any exact analogies in pure Ada source form. The following
6852 is a partial list of these special constructions. See the spec
6853 of package @code{Sprint} in file @file{sprint.ads} for a full list.
6854
6855 If the switch @option{-gnatL} is used in conjunction with
6856 @cindex @option{-gnatL} (@command{gcc})
6857 @option{-gnatG}, then the original source lines are interspersed
6858 in the expanded source (as comment lines with the original line number).
6859
6860 @table @code
6861 @item new @var{xxx} @r{[}storage_pool = @var{yyy}@r{]}
6862 Shows the storage pool being used for an allocator.
6863
6864 @item at end @var{procedure-name};
6865 Shows the finalization (cleanup) procedure for a scope.
6866
6867 @item (if @var{expr} then @var{expr} else @var{expr})
6868 Conditional expression equivalent to the @code{x?y:z} construction in C.
6869
6870 @item @var{target}^^^(@var{source})
6871 A conversion with floating-point truncation instead of rounding.
6872
6873 @item @var{target}?(@var{source})
6874 A conversion that bypasses normal Ada semantic checking. In particular
6875 enumeration types and fixed-point types are treated simply as integers.
6876
6877 @item @var{target}?^^^(@var{source})
6878 Combines the above two cases.
6879
6880 @item @var{x} #/ @var{y}
6881 @itemx @var{x} #mod @var{y}
6882 @itemx @var{x} #* @var{y}
6883 @itemx @var{x} #rem @var{y}
6884 A division or multiplication of fixed-point values which are treated as
6885 integers without any kind of scaling.
6886
6887 @item free @var{expr} @r{[}storage_pool = @var{xxx}@r{]}
6888 Shows the storage pool associated with a @code{free} statement.
6889
6890 @item [subtype or type declaration]
6891 Used to list an equivalent declaration for an internally generated
6892 type that is referenced elsewhere in the listing.
6893
6894 @item freeze @var{type-name} @ovar{actions}
6895 Shows the point at which @var{type-name} is frozen, with possible
6896 associated actions to be performed at the freeze point.
6897
6898 @item reference @var{itype}
6899 Reference (and hence definition) to internal type @var{itype}.
6900
6901 @item @var{function-name}! (@var{arg}, @var{arg}, @var{arg})
6902 Intrinsic function call.
6903
6904 @item @var{label-name} : label
6905 Declaration of label @var{labelname}.
6906
6907 @item #$ @var{subprogram-name}
6908 An implicit call to a run-time support routine
6909 (to meet the requirement of H.3.1(9) in a
6910 convenient manner).
6911
6912 @item @var{expr} && @var{expr} && @var{expr} @dots{} && @var{expr}
6913 A multiple concatenation (same effect as @var{expr} & @var{expr} &
6914 @var{expr}, but handled more efficiently).
6915
6916 @item [constraint_error]
6917 Raise the @code{Constraint_Error} exception.
6918
6919 @item @var{expression}'reference
6920 A pointer to the result of evaluating @var{expression}.
6921
6922 @item @var{target-type}!(@var{source-expression})
6923 An unchecked conversion of @var{source-expression} to @var{target-type}.
6924
6925 @item [@var{numerator}/@var{denominator}]
6926 Used to represent internal real literals (that) have no exact
6927 representation in base 2-16 (for example, the result of compile time
6928 evaluation of the expression 1.0/27.0).
6929 @end table
6930
6931 @item -gnatD[=nn]
6932 @cindex @option{-gnatD} (@command{gcc})
6933 When used in conjunction with @option{-gnatG}, this switch causes
6934 the expanded source, as described above for
6935 @option{-gnatG} to be written to files with names
6936 @file{^xxx.dg^XXX_DG^}, where @file{xxx} is the normal file name,
6937 instead of to the standard output file. For
6938 example, if the source file name is @file{hello.adb}, then a file
6939 @file{^hello.adb.dg^HELLO.ADB_DG^} will be written. The debugging
6940 information generated by the @command{gcc} @option{^-g^/DEBUG^} switch
6941 will refer to the generated @file{^xxx.dg^XXX_DG^} file. This allows
6942 you to do source level debugging using the generated code which is
6943 sometimes useful for complex code, for example to find out exactly
6944 which part of a complex construction raised an exception. This switch
6945 also suppress generation of cross-reference information (see
6946 @option{-gnatx}) since otherwise the cross-reference information
6947 would refer to the @file{^.dg^.DG^} file, which would cause
6948 confusion since this is not the original source file.
6949
6950 Note that @option{-gnatD} actually implies @option{-gnatG}
6951 automatically, so it is not necessary to give both options.
6952 In other words @option{-gnatD} is equivalent to @option{-gnatDG}).
6953
6954 If the switch @option{-gnatL} is used in conjunction with
6955 @cindex @option{-gnatL} (@command{gcc})
6956 @option{-gnatDG}, then the original source lines are interspersed
6957 in the expanded source (as comment lines with the original line number).
6958
6959 The optional parameter @code{nn} if present after -gnatD specifies an
6960 alternative maximum line length that overrides the normal default of 72.
6961 This value is in the range 40-999999, values less than 40 being silently
6962 reset to 40. The equal sign is optional.
6963
6964 @item -gnatr
6965 @cindex @option{-gnatr} (@command{gcc})
6966 @cindex pragma Restrictions
6967 This switch causes pragma Restrictions to be treated as Restriction_Warnings
6968 so that violation of restrictions causes warnings rather than illegalities.
6969 This is useful during the development process when new restrictions are added
6970 or investigated. The switch also causes pragma Profile to be treated as
6971 Profile_Warnings, and pragma Restricted_Run_Time and pragma Ravenscar set
6972 restriction warnings rather than restrictions.
6973
6974 @ifclear vms
6975 @item -gnatR@r{[}0@r{|}1@r{|}2@r{|}3@r{[}s@r{]]}
6976 @cindex @option{-gnatR} (@command{gcc})
6977 This switch controls output from the compiler of a listing showing
6978 representation information for declared types and objects. For
6979 @option{-gnatR0}, no information is output (equivalent to omitting
6980 the @option{-gnatR} switch). For @option{-gnatR1} (which is the default,
6981 so @option{-gnatR} with no parameter has the same effect), size and alignment
6982 information is listed for declared array and record types. For
6983 @option{-gnatR2}, size and alignment information is listed for all
6984 declared types and objects. Finally @option{-gnatR3} includes symbolic
6985 expressions for values that are computed at run time for
6986 variant records. These symbolic expressions have a mostly obvious
6987 format with #n being used to represent the value of the n'th
6988 discriminant. See source files @file{repinfo.ads/adb} in the
6989 @code{GNAT} sources for full details on the format of @option{-gnatR3}
6990 output. If the switch is followed by an s (e.g.@: @option{-gnatR2s}), then
6991 the output is to a file with the name @file{^file.rep^file_REP^} where
6992 file is the name of the corresponding source file.
6993 @end ifclear
6994 @ifset vms
6995 @item /REPRESENTATION_INFO
6996 @cindex @option{/REPRESENTATION_INFO} (@command{gcc})
6997 This qualifier controls output from the compiler of a listing showing
6998 representation information for declared types and objects. For
6999 @option{/REPRESENTATION_INFO=NONE}, no information is output
7000 (equivalent to omitting the @option{/REPRESENTATION_INFO} qualifier).
7001 @option{/REPRESENTATION_INFO} without option is equivalent to
7002 @option{/REPRESENTATION_INFO=ARRAYS}.
7003 For @option{/REPRESENTATION_INFO=ARRAYS}, size and alignment
7004 information is listed for declared array and record types. For
7005 @option{/REPRESENTATION_INFO=OBJECTS}, size and alignment information
7006 is listed for all expression information for values that are computed
7007 at run time for variant records. These symbolic expressions have a mostly
7008 obvious format with #n being used to represent the value of the n'th
7009 discriminant. See source files @file{REPINFO.ADS/ADB} in the
7010 @code{GNAT} sources for full details on the format of
7011 @option{/REPRESENTATION_INFO=SYMBOLIC} output.
7012 If _FILE is added at the end of an option
7013 (e.g.@: @option{/REPRESENTATION_INFO=ARRAYS_FILE}),
7014 then the output is to a file with the name @file{file_REP} where
7015 file is the name of the corresponding source file.
7016 @end ifset
7017 Note that it is possible for record components to have zero size. In
7018 this case, the component clause uses an obvious extension of permitted
7019 Ada syntax, for example @code{at 0 range 0 .. -1}.
7020
7021 Representation information requires that code be generated (since it is the
7022 code generator that lays out complex data structures). If an attempt is made
7023 to output representation information when no code is generated, for example
7024 when a subunit is compiled on its own, then no information can be generated
7025 and the compiler outputs a message to this effect.
7026
7027 @item -gnatS
7028 @cindex @option{-gnatS} (@command{gcc})
7029 The use of the switch @option{-gnatS} for an
7030 Ada compilation will cause the compiler to output a
7031 representation of package Standard in a form very
7032 close to standard Ada. It is not quite possible to
7033 do this entirely in standard Ada (since new
7034 numeric base types cannot be created in standard
7035 Ada), but the output is easily
7036 readable to any Ada programmer, and is useful to
7037 determine the characteristics of target dependent
7038 types in package Standard.
7039
7040 @item -gnatx
7041 @cindex @option{-gnatx} (@command{gcc})
7042 Normally the compiler generates full cross-referencing information in
7043 the @file{ALI} file. This information is used by a number of tools,
7044 including @code{gnatfind} and @code{gnatxref}. The @option{-gnatx} switch
7045 suppresses this information. This saves some space and may slightly
7046 speed up compilation, but means that these tools cannot be used.
7047 @end table
7048
7049 @node Exception Handling Control
7050 @subsection Exception Handling Control
7051
7052 @noindent
7053 GNAT uses two methods for handling exceptions at run-time. The
7054 @code{setjmp/longjmp} method saves the context when entering
7055 a frame with an exception handler. Then when an exception is
7056 raised, the context can be restored immediately, without the
7057 need for tracing stack frames. This method provides very fast
7058 exception propagation, but introduces significant overhead for
7059 the use of exception handlers, even if no exception is raised.
7060
7061 The other approach is called ``zero cost'' exception handling.
7062 With this method, the compiler builds static tables to describe
7063 the exception ranges. No dynamic code is required when entering
7064 a frame containing an exception handler. When an exception is
7065 raised, the tables are used to control a back trace of the
7066 subprogram invocation stack to locate the required exception
7067 handler. This method has considerably poorer performance for
7068 the propagation of exceptions, but there is no overhead for
7069 exception handlers if no exception is raised. Note that in this
7070 mode and in the context of mixed Ada and C/C++ programming,
7071 to propagate an exception through a C/C++ code, the C/C++ code
7072 must be compiled with the @option{-funwind-tables} GCC's
7073 option.
7074
7075 The following switches may be used to control which of the
7076 two exception handling methods is used.
7077
7078 @table @option
7079 @c !sort!
7080
7081 @item --RTS=sjlj
7082 @cindex @option{--RTS=sjlj} (@command{gnatmake})
7083 This switch causes the setjmp/longjmp run-time (when available) to be used
7084 for exception handling. If the default
7085 mechanism for the target is zero cost exceptions, then
7086 this switch can be used to modify this default, and must be
7087 used for all units in the partition.
7088 This option is rarely used. One case in which it may be
7089 advantageous is if you have an application where exception
7090 raising is common and the overall performance of the
7091 application is improved by favoring exception propagation.
7092
7093 @item --RTS=zcx
7094 @cindex @option{--RTS=zcx} (@command{gnatmake})
7095 @cindex Zero Cost Exceptions
7096 This switch causes the zero cost approach to be used
7097 for exception handling. If this is the default mechanism for the
7098 target (see below), then this switch is unneeded. If the default
7099 mechanism for the target is setjmp/longjmp exceptions, then
7100 this switch can be used to modify this default, and must be
7101 used for all units in the partition.
7102 This option can only be used if the zero cost approach
7103 is available for the target in use, otherwise it will generate an error.
7104 @end table
7105
7106 @noindent
7107 The same option @option{--RTS} must be used both for @command{gcc}
7108 and @command{gnatbind}. Passing this option to @command{gnatmake}
7109 (@pxref{Switches for gnatmake}) will ensure the required consistency
7110 through the compilation and binding steps.
7111
7112 @node Units to Sources Mapping Files
7113 @subsection Units to Sources Mapping Files
7114
7115 @table @option
7116
7117 @item -gnatem^^=^@var{path}
7118 @cindex @option{-gnatem} (@command{gcc})
7119 A mapping file is a way to communicate to the compiler two mappings:
7120 from unit names to file names (without any directory information) and from
7121 file names to path names (with full directory information). These mappings
7122 are used by the compiler to short-circuit the path search.
7123
7124 The use of mapping files is not required for correct operation of the
7125 compiler, but mapping files can improve efficiency, particularly when
7126 sources are read over a slow network connection. In normal operation,
7127 you need not be concerned with the format or use of mapping files,
7128 and the @option{-gnatem} switch is not a switch that you would use
7129 explicitly. it is intended only for use by automatic tools such as
7130 @command{gnatmake} running under the project file facility. The
7131 description here of the format of mapping files is provided
7132 for completeness and for possible use by other tools.
7133
7134 A mapping file is a sequence of sets of three lines. In each set,
7135 the first line is the unit name, in lower case, with ``@code{%s}''
7136 appended for
7137 specs and ``@code{%b}'' appended for bodies; the second line is the
7138 file name; and the third line is the path name.
7139
7140 Example:
7141 @smallexample
7142 main%b
7143 main.2.ada
7144 /gnat/project1/sources/main.2.ada
7145 @end smallexample
7146
7147 When the switch @option{-gnatem} is specified, the compiler will create
7148 in memory the two mappings from the specified file. If there is any problem
7149 (nonexistent file, truncated file or duplicate entries), no mapping will
7150 be created.
7151
7152 Several @option{-gnatem} switches may be specified; however, only the last
7153 one on the command line will be taken into account.
7154
7155 When using a project file, @command{gnatmake} create a temporary mapping file
7156 and communicates it to the compiler using this switch.
7157
7158 @end table
7159
7160 @node Integrated Preprocessing
7161 @subsection Integrated Preprocessing
7162
7163 @noindent
7164 GNAT sources may be preprocessed immediately before compilation.
7165 In this case, the actual
7166 text of the source is not the text of the source file, but is derived from it
7167 through a process called preprocessing. Integrated preprocessing is specified
7168 through switches @option{-gnatep} and/or @option{-gnateD}. @option{-gnatep}
7169 indicates, through a text file, the preprocessing data to be used.
7170 @option{-gnateD} specifies or modifies the values of preprocessing symbol.
7171
7172 @noindent
7173 Note that when integrated preprocessing is used, the output from the
7174 preprocessor is not written to any external file. Instead it is passed
7175 internally to the compiler. If you need to preserve the result of
7176 preprocessing in a file, then you should use @command{gnatprep}
7177 to perform the desired preprocessing in stand-alone mode.
7178
7179 @noindent
7180 It is recommended that @command{gnatmake} switch ^-s^/SWITCH_CHECK^ should be
7181 used when Integrated Preprocessing is used. The reason is that preprocessing
7182 with another Preprocessing Data file without changing the sources will
7183 not trigger recompilation without this switch.
7184
7185 @noindent
7186 Note that @command{gnatmake} switch ^-m^/MINIMAL_RECOMPILATION^ will almost
7187 always trigger recompilation for sources that are preprocessed,
7188 because @command{gnatmake} cannot compute the checksum of the source after
7189 preprocessing.
7190
7191 @noindent
7192 The actual preprocessing function is described in details in section
7193 @ref{Preprocessing Using gnatprep}. This section only describes how integrated
7194 preprocessing is triggered and parameterized.
7195
7196 @table @code
7197
7198 @item -gnatep=@var{file}
7199 @cindex @option{-gnatep} (@command{gcc})
7200 This switch indicates to the compiler the file name (without directory
7201 information) of the preprocessor data file to use. The preprocessor data file
7202 should be found in the source directories.
7203
7204 @noindent
7205 A preprocessing data file is a text file with significant lines indicating
7206 how should be preprocessed either a specific source or all sources not
7207 mentioned in other lines. A significant line is a nonempty, non-comment line.
7208 Comments are similar to Ada comments.
7209
7210 @noindent
7211 Each significant line starts with either a literal string or the character '*'.
7212 A literal string is the file name (without directory information) of the source
7213 to preprocess. A character '*' indicates the preprocessing for all the sources
7214 that are not specified explicitly on other lines (order of the lines is not
7215 significant). It is an error to have two lines with the same file name or two
7216 lines starting with the character '*'.
7217
7218 @noindent
7219 After the file name or the character '*', another optional literal string
7220 indicating the file name of the definition file to be used for preprocessing
7221 (@pxref{Form of Definitions File}). The definition files are found by the
7222 compiler in one of the source directories. In some cases, when compiling
7223 a source in a directory other than the current directory, if the definition
7224 file is in the current directory, it may be necessary to add the current
7225 directory as a source directory through switch ^-I.^/SEARCH=[]^, otherwise
7226 the compiler would not find the definition file.
7227
7228 @noindent
7229 Then, optionally, ^switches^switches^ similar to those of @code{gnatprep} may
7230 be found. Those ^switches^switches^ are:
7231
7232 @table @code
7233
7234 @item -b
7235 Causes both preprocessor lines and the lines deleted by
7236 preprocessing to be replaced by blank lines, preserving the line number.
7237 This ^switch^switch^ is always implied; however, if specified after @option{-c}
7238 it cancels the effect of @option{-c}.
7239
7240 @item -c
7241 Causes both preprocessor lines and the lines deleted
7242 by preprocessing to be retained as comments marked
7243 with the special string ``@code{--! }''.
7244
7245 @item -Dsymbol=value
7246 Define or redefine a symbol, associated with value. A symbol is an Ada
7247 identifier, or an Ada reserved word, with the exception of @code{if},
7248 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
7249 @code{value} is either a literal string, an Ada identifier or any Ada reserved
7250 word. A symbol declared with this ^switch^switch^ replaces a symbol with the
7251 same name defined in a definition file.
7252
7253 @item -s
7254 Causes a sorted list of symbol names and values to be
7255 listed on the standard output file.
7256
7257 @item -u
7258 Causes undefined symbols to be treated as having the value @code{FALSE}
7259 in the context
7260 of a preprocessor test. In the absence of this option, an undefined symbol in
7261 a @code{#if} or @code{#elsif} test will be treated as an error.
7262
7263 @end table
7264
7265 @noindent
7266 Examples of valid lines in a preprocessor data file:
7267
7268 @smallexample
7269 "toto.adb" "prep.def" -u
7270 -- preprocess "toto.adb", using definition file "prep.def",
7271 -- undefined symbol are False.
7272
7273 * -c -DVERSION=V101
7274 -- preprocess all other sources without a definition file;
7275 -- suppressed lined are commented; symbol VERSION has the value V101.
7276
7277 "titi.adb" "prep2.def" -s
7278 -- preprocess "titi.adb", using definition file "prep2.def";
7279 -- list all symbols with their values.
7280 @end smallexample
7281
7282 @item ^-gnateD^/DATA_PREPROCESSING=^symbol@r{[}=value@r{]}
7283 @cindex @option{-gnateD} (@command{gcc})
7284 Define or redefine a preprocessing symbol, associated with value. If no value
7285 is given on the command line, then the value of the symbol is @code{True}.
7286 A symbol is an identifier, following normal Ada (case-insensitive)
7287 rules for its syntax, and value is any sequence (including an empty sequence)
7288 of characters from the set (letters, digits, period, underline).
7289 Ada reserved words may be used as symbols, with the exceptions of @code{if},
7290 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
7291
7292 @noindent
7293 A symbol declared with this ^switch^switch^ on the command line replaces a
7294 symbol with the same name either in a definition file or specified with a
7295 ^switch^switch^ -D in the preprocessor data file.
7296
7297 @noindent
7298 This switch is similar to switch @option{^-D^/ASSOCIATE^} of @code{gnatprep}.
7299
7300 @item -gnateG
7301 When integrated preprocessing is performed and the preprocessor modifies
7302 the source text, write the result of this preprocessing into a file
7303 <source>^.prep^_prep^.
7304
7305 @end table
7306
7307 @node Code Generation Control
7308 @subsection Code Generation Control
7309
7310 @noindent
7311
7312 The GCC technology provides a wide range of target dependent
7313 @option{-m} switches for controlling
7314 details of code generation with respect to different versions of
7315 architectures. This includes variations in instruction sets (e.g.@:
7316 different members of the power pc family), and different requirements
7317 for optimal arrangement of instructions (e.g.@: different members of
7318 the x86 family). The list of available @option{-m} switches may be
7319 found in the GCC documentation.
7320
7321 Use of these @option{-m} switches may in some cases result in improved
7322 code performance.
7323
7324 The GNAT Pro technology is tested and qualified without any
7325 @option{-m} switches,
7326 so generally the most reliable approach is to avoid the use of these
7327 switches. However, we generally expect most of these switches to work
7328 successfully with GNAT Pro, and many customers have reported successful
7329 use of these options.
7330
7331 Our general advice is to avoid the use of @option{-m} switches unless
7332 special needs lead to requirements in this area. In particular,
7333 there is no point in using @option{-m} switches to improve performance
7334 unless you actually see a performance improvement.
7335
7336 @ifset vms
7337 @node Return Codes
7338 @subsection Return Codes
7339 @cindex Return Codes
7340 @cindex @option{/RETURN_CODES=VMS}
7341
7342 @noindent
7343 On VMS, GNAT compiled programs return POSIX-style codes by default,
7344 e.g.@: @option{/RETURN_CODES=POSIX}.
7345
7346 To enable VMS style return codes, use GNAT BIND and LINK with the option
7347 @option{/RETURN_CODES=VMS}. For example:
7348
7349 @smallexample
7350 GNAT BIND MYMAIN.ALI /RETURN_CODES=VMS
7351 GNAT LINK MYMAIN.ALI /RETURN_CODES=VMS
7352 @end smallexample
7353
7354 @noindent
7355 Programs built with /RETURN_CODES=VMS are suitable to be called in
7356 VMS DCL scripts. Programs compiled with the default /RETURN_CODES=POSIX
7357 are suitable for spawning with appropriate GNAT RTL routines.
7358
7359 @end ifset
7360
7361 @node Search Paths and the Run-Time Library (RTL)
7362 @section Search Paths and the Run-Time Library (RTL)
7363
7364 @noindent
7365 With the GNAT source-based library system, the compiler must be able to
7366 find source files for units that are needed by the unit being compiled.
7367 Search paths are used to guide this process.
7368
7369 The compiler compiles one source file whose name must be given
7370 explicitly on the command line. In other words, no searching is done
7371 for this file. To find all other source files that are needed (the most
7372 common being the specs of units), the compiler examines the following
7373 directories, in the following order:
7374
7375 @enumerate
7376 @item
7377 The directory containing the source file of the main unit being compiled
7378 (the file name on the command line).
7379
7380 @item
7381 Each directory named by an @option{^-I^/SOURCE_SEARCH^} switch given on the
7382 @command{gcc} command line, in the order given.
7383
7384 @item
7385 @findex ADA_PRJ_INCLUDE_FILE
7386 Each of the directories listed in the text file whose name is given
7387 by the @env{ADA_PRJ_INCLUDE_FILE} ^environment variable^logical name^.
7388
7389 @noindent
7390 @env{ADA_PRJ_INCLUDE_FILE} is normally set by gnatmake or by the ^gnat^GNAT^
7391 driver when project files are used. It should not normally be set
7392 by other means.
7393
7394 @item
7395 @findex ADA_INCLUDE_PATH
7396 Each of the directories listed in the value of the
7397 @env{ADA_INCLUDE_PATH} ^environment variable^logical name^.
7398 @ifclear vms
7399 Construct this value
7400 exactly as the @env{PATH} environment variable: a list of directory
7401 names separated by colons (semicolons when working with the NT version).
7402 @end ifclear
7403 @ifset vms
7404 Normally, define this value as a logical name containing a comma separated
7405 list of directory names.
7406
7407 This variable can also be defined by means of an environment string
7408 (an argument to the HP C exec* set of functions).
7409
7410 Logical Name:
7411 @smallexample
7412 DEFINE ANOTHER_PATH FOO:[BAG]
7413 DEFINE ADA_INCLUDE_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
7414 @end smallexample
7415
7416 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
7417 first, followed by the standard Ada
7418 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADAINCLUDE].
7419 If this is not redefined, the user will obtain the HP Ada 83 IO packages
7420 (Text_IO, Sequential_IO, etc)
7421 instead of the standard Ada packages. Thus, in order to get the standard Ada
7422 packages by default, ADA_INCLUDE_PATH must be redefined.
7423 @end ifset
7424
7425 @item
7426 The content of the @file{ada_source_path} file which is part of the GNAT
7427 installation tree and is used to store standard libraries such as the
7428 GNAT Run Time Library (RTL) source files.
7429 @ifclear vms
7430 @ref{Installing a library}
7431 @end ifclear
7432 @end enumerate
7433
7434 @noindent
7435 Specifying the switch @option{^-I-^/NOCURRENT_DIRECTORY^}
7436 inhibits the use of the directory
7437 containing the source file named in the command line. You can still
7438 have this directory on your search path, but in this case it must be
7439 explicitly requested with a @option{^-I^/SOURCE_SEARCH^} switch.
7440
7441 Specifying the switch @option{-nostdinc}
7442 inhibits the search of the default location for the GNAT Run Time
7443 Library (RTL) source files.
7444
7445 The compiler outputs its object files and ALI files in the current
7446 working directory.
7447 @ifclear vms
7448 Caution: The object file can be redirected with the @option{-o} switch;
7449 however, @command{gcc} and @code{gnat1} have not been coordinated on this
7450 so the @file{ALI} file will not go to the right place. Therefore, you should
7451 avoid using the @option{-o} switch.
7452 @end ifclear
7453
7454 @findex System.IO
7455 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
7456 children make up the GNAT RTL, together with the simple @code{System.IO}
7457 package used in the @code{"Hello World"} example. The sources for these units
7458 are needed by the compiler and are kept together in one directory. Not
7459 all of the bodies are needed, but all of the sources are kept together
7460 anyway. In a normal installation, you need not specify these directory
7461 names when compiling or binding. Either the environment variables or
7462 the built-in defaults cause these files to be found.
7463
7464 In addition to the language-defined hierarchies (@code{System}, @code{Ada} and
7465 @code{Interfaces}), the GNAT distribution provides a fourth hierarchy,
7466 consisting of child units of @code{GNAT}. This is a collection of generally
7467 useful types, subprograms, etc. @xref{Top, GNAT Reference Manual, About
7468 This Guid, gnat_rm, GNAT Reference Manual}, for further details.
7469
7470 Besides simplifying access to the RTL, a major use of search paths is
7471 in compiling sources from multiple directories. This can make
7472 development environments much more flexible.
7473
7474 @node Order of Compilation Issues
7475 @section Order of Compilation Issues
7476
7477 @noindent
7478 If, in our earlier example, there was a spec for the @code{hello}
7479 procedure, it would be contained in the file @file{hello.ads}; yet this
7480 file would not have to be explicitly compiled. This is the result of the
7481 model we chose to implement library management. Some of the consequences
7482 of this model are as follows:
7483
7484 @itemize @bullet
7485 @item
7486 There is no point in compiling specs (except for package
7487 specs with no bodies) because these are compiled as needed by clients. If
7488 you attempt a useless compilation, you will receive an error message.
7489 It is also useless to compile subunits because they are compiled as needed
7490 by the parent.
7491
7492 @item
7493 There are no order of compilation requirements: performing a
7494 compilation never obsoletes anything. The only way you can obsolete
7495 something and require recompilations is to modify one of the
7496 source files on which it depends.
7497
7498 @item
7499 There is no library as such, apart from the ALI files
7500 (@pxref{The Ada Library Information Files}, for information on the format
7501 of these files). For now we find it convenient to create separate ALI files,
7502 but eventually the information therein may be incorporated into the object
7503 file directly.
7504
7505 @item
7506 When you compile a unit, the source files for the specs of all units
7507 that it @code{with}'s, all its subunits, and the bodies of any generics it
7508 instantiates must be available (reachable by the search-paths mechanism
7509 described above), or you will receive a fatal error message.
7510 @end itemize
7511
7512 @node Examples
7513 @section Examples
7514
7515 @noindent
7516 The following are some typical Ada compilation command line examples:
7517
7518 @table @code
7519 @item $ gcc -c xyz.adb
7520 Compile body in file @file{xyz.adb} with all default options.
7521
7522 @ifclear vms
7523 @item $ gcc -c -O2 -gnata xyz-def.adb
7524 @end ifclear
7525 @ifset vms
7526 @item $ GNAT COMPILE /OPTIMIZE=ALL -gnata xyz-def.adb
7527 @end ifset
7528
7529 Compile the child unit package in file @file{xyz-def.adb} with extensive
7530 optimizations, and pragma @code{Assert}/@code{Debug} statements
7531 enabled.
7532
7533 @item $ gcc -c -gnatc abc-def.adb
7534 Compile the subunit in file @file{abc-def.adb} in semantic-checking-only
7535 mode.
7536 @end table
7537
7538 @node Binding Using gnatbind
7539 @chapter Binding Using @code{gnatbind}
7540 @findex gnatbind
7541
7542 @menu
7543 * Running gnatbind::
7544 * Switches for gnatbind::
7545 * Command-Line Access::
7546 * Search Paths for gnatbind::
7547 * Examples of gnatbind Usage::
7548 @end menu
7549
7550 @noindent
7551 This chapter describes the GNAT binder, @code{gnatbind}, which is used
7552 to bind compiled GNAT objects.
7553
7554 Note: to invoke @code{gnatbind} with a project file, use the @code{gnat}
7555 driver (see @ref{The GNAT Driver and Project Files}).
7556
7557 The @code{gnatbind} program performs four separate functions:
7558
7559 @enumerate
7560 @item
7561 Checks that a program is consistent, in accordance with the rules in
7562 Chapter 10 of the Ada Reference Manual. In particular, error
7563 messages are generated if a program uses inconsistent versions of a
7564 given unit.
7565
7566 @item
7567 Checks that an acceptable order of elaboration exists for the program
7568 and issues an error message if it cannot find an order of elaboration
7569 that satisfies the rules in Chapter 10 of the Ada Language Manual.
7570
7571 @item
7572 Generates a main program incorporating the given elaboration order.
7573 This program is a small Ada package (body and spec) that
7574 must be subsequently compiled
7575 using the GNAT compiler. The necessary compilation step is usually
7576 performed automatically by @command{gnatlink}. The two most important
7577 functions of this program
7578 are to call the elaboration routines of units in an appropriate order
7579 and to call the main program.
7580
7581 @item
7582 Determines the set of object files required by the given main program.
7583 This information is output in the forms of comments in the generated program,
7584 to be read by the @command{gnatlink} utility used to link the Ada application.
7585 @end enumerate
7586
7587 @node Running gnatbind
7588 @section Running @code{gnatbind}
7589
7590 @noindent
7591 The form of the @code{gnatbind} command is
7592
7593 @smallexample
7594 $ gnatbind @ovar{switches} @var{mainprog}@r{[}.ali@r{]} @ovar{switches}
7595 @end smallexample
7596
7597 @noindent
7598 where @file{@var{mainprog}.adb} is the Ada file containing the main program
7599 unit body. If no switches are specified, @code{gnatbind} constructs an Ada
7600 package in two files whose names are
7601 @file{b~@var{mainprog}.ads}, and @file{b~@var{mainprog}.adb}.
7602 For example, if given the
7603 parameter @file{hello.ali}, for a main program contained in file
7604 @file{hello.adb}, the binder output files would be @file{b~hello.ads}
7605 and @file{b~hello.adb}.
7606
7607 When doing consistency checking, the binder takes into consideration
7608 any source files it can locate. For example, if the binder determines
7609 that the given main program requires the package @code{Pack}, whose
7610 @file{.ALI}
7611 file is @file{pack.ali} and whose corresponding source spec file is
7612 @file{pack.ads}, it attempts to locate the source file @file{pack.ads}
7613 (using the same search path conventions as previously described for the
7614 @command{gcc} command). If it can locate this source file, it checks that
7615 the time stamps
7616 or source checksums of the source and its references to in @file{ALI} files
7617 match. In other words, any @file{ALI} files that mentions this spec must have
7618 resulted from compiling this version of the source file (or in the case
7619 where the source checksums match, a version close enough that the
7620 difference does not matter).
7621
7622 @cindex Source files, use by binder
7623 The effect of this consistency checking, which includes source files, is
7624 that the binder ensures that the program is consistent with the latest
7625 version of the source files that can be located at bind time. Editing a
7626 source file without compiling files that depend on the source file cause
7627 error messages to be generated by the binder.
7628
7629 For example, suppose you have a main program @file{hello.adb} and a
7630 package @code{P}, from file @file{p.ads} and you perform the following
7631 steps:
7632
7633 @enumerate
7634 @item
7635 Enter @code{gcc -c hello.adb} to compile the main program.
7636
7637 @item
7638 Enter @code{gcc -c p.ads} to compile package @code{P}.
7639
7640 @item
7641 Edit file @file{p.ads}.
7642
7643 @item
7644 Enter @code{gnatbind hello}.
7645 @end enumerate
7646
7647 @noindent
7648 At this point, the file @file{p.ali} contains an out-of-date time stamp
7649 because the file @file{p.ads} has been edited. The attempt at binding
7650 fails, and the binder generates the following error messages:
7651
7652 @smallexample
7653 error: "hello.adb" must be recompiled ("p.ads" has been modified)
7654 error: "p.ads" has been modified and must be recompiled
7655 @end smallexample
7656
7657 @noindent
7658 Now both files must be recompiled as indicated, and then the bind can
7659 succeed, generating a main program. You need not normally be concerned
7660 with the contents of this file, but for reference purposes a sample
7661 binder output file is given in @ref{Example of Binder Output File}.
7662
7663 In most normal usage, the default mode of @command{gnatbind} which is to
7664 generate the main package in Ada, as described in the previous section.
7665 In particular, this means that any Ada programmer can read and understand
7666 the generated main program. It can also be debugged just like any other
7667 Ada code provided the @option{^-g^/DEBUG^} switch is used for
7668 @command{gnatbind} and @command{gnatlink}.
7669
7670 However for some purposes it may be convenient to generate the main
7671 program in C rather than Ada. This may for example be helpful when you
7672 are generating a mixed language program with the main program in C. The
7673 GNAT compiler itself is an example.
7674 The use of the @option{^-C^/BIND_FILE=C^} switch
7675 for both @code{gnatbind} and @command{gnatlink} will cause the program to
7676 be generated in C (and compiled using the gnu C compiler).
7677
7678 @node Switches for gnatbind
7679 @section Switches for @command{gnatbind}
7680
7681 @noindent
7682 The following switches are available with @code{gnatbind}; details will
7683 be presented in subsequent sections.
7684
7685 @menu
7686 * Consistency-Checking Modes::
7687 * Binder Error Message Control::
7688 * Elaboration Control::
7689 * Output Control::
7690 * Binding with Non-Ada Main Programs::
7691 * Binding Programs with No Main Subprogram::
7692 @end menu
7693
7694 @table @option
7695 @c !sort!
7696
7697 @item --version
7698 @cindex @option{--version} @command{gnatbind}
7699 Display Copyright and version, then exit disregarding all other options.
7700
7701 @item --help
7702 @cindex @option{--help} @command{gnatbind}
7703 If @option{--version} was not used, display usage, then exit disregarding
7704 all other options.
7705
7706 @item -a
7707 @cindex @option{-a} @command{gnatbind}
7708 Indicates that, if supported by the platform, the adainit procedure should
7709 be treated as an initialisation routine by the linker (a constructor). This
7710 is intended to be used by the Project Manager to automatically initialize
7711 shared Stand-Alone Libraries.
7712
7713 @item ^-aO^/OBJECT_SEARCH^
7714 @cindex @option{^-aO^/OBJECT_SEARCH^} (@command{gnatbind})
7715 Specify directory to be searched for ALI files.
7716
7717 @item ^-aI^/SOURCE_SEARCH^
7718 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatbind})
7719 Specify directory to be searched for source file.
7720
7721 @item ^-A^/BIND_FILE=ADA^
7722 @cindex @option{^-A^/BIND_FILE=ADA^} (@command{gnatbind})
7723 Generate binder program in Ada (default)
7724
7725 @item ^-b^/REPORT_ERRORS=BRIEF^
7726 @cindex @option{^-b^/REPORT_ERRORS=BRIEF^} (@command{gnatbind})
7727 Generate brief messages to @file{stderr} even if verbose mode set.
7728
7729 @item ^-c^/NOOUTPUT^
7730 @cindex @option{^-c^/NOOUTPUT^} (@command{gnatbind})
7731 Check only, no generation of binder output file.
7732
7733 @item ^-C^/BIND_FILE=C^
7734 @cindex @option{^-C^/BIND_FILE=C^} (@command{gnatbind})
7735 Generate binder program in C
7736
7737 @item ^-d^/DEFAULT_STACK_SIZE=^@var{nn}@r{[}k@r{|}m@r{]}
7738 @cindex @option{^-d^/DEFAULT_STACK_SIZE=^@var{nn}@r{[}k@r{|}m@r{]}} (@command{gnatbind})
7739 This switch can be used to change the default task stack size value
7740 to a specified size @var{nn}, which is expressed in bytes by default, or
7741 in kilobytes when suffixed with @var{k} or in megabytes when suffixed
7742 with @var{m}.
7743 In the absence of a @samp{@r{[}k@r{|}m@r{]}} suffix, this switch is equivalent,
7744 in effect, to completing all task specs with
7745 @smallexample @c ada
7746 pragma Storage_Size (nn);
7747 @end smallexample
7748 When they do not already have such a pragma.
7749
7750 @item ^-D^/DEFAULT_SECONDARY_STACK_SIZE=^@var{nn}@r{[}k@r{|}m@r{]}
7751 @cindex @option{^-D^/DEFAULT_SECONDARY_STACK_SIZE=nnnnn^} (@command{gnatbind})
7752 This switch can be used to change the default secondary stack size value
7753 to a specified size @var{nn}, which is expressed in bytes by default, or
7754 in kilobytes when suffixed with @var{k} or in megabytes when suffixed
7755 with @var{m}.
7756
7757 The secondary stack is used to deal with functions that return a variable
7758 sized result, for example a function returning an unconstrained
7759 String. There are two ways in which this secondary stack is allocated.
7760
7761 For most targets, the secondary stack is growing on demand and is allocated
7762 as a chain of blocks in the heap. The -D option is not very
7763 relevant. It only give some control over the size of the allocated
7764 blocks (whose size is the minimum of the default secondary stack size value,
7765 and the actual size needed for the current allocation request).
7766
7767 For certain targets, notably VxWorks 653,
7768 the secondary stack is allocated by carving off a fixed ratio chunk of the
7769 primary task stack. The -D option is used to define the
7770 size of the environment task's secondary stack.
7771
7772 @item ^-e^/ELABORATION_DEPENDENCIES^
7773 @cindex @option{^-e^/ELABORATION_DEPENDENCIES^} (@command{gnatbind})
7774 Output complete list of elaboration-order dependencies.
7775
7776 @item ^-E^/STORE_TRACEBACKS^
7777 @cindex @option{^-E^/STORE_TRACEBACKS^} (@command{gnatbind})
7778 Store tracebacks in exception occurrences when the target supports it.
7779 This is the default with the zero cost exception mechanism.
7780 @ignore
7781 @c The following may get moved to an appendix
7782 This option is currently supported on the following targets:
7783 all x86 ports, Solaris, Windows, HP-UX, AIX, PowerPC VxWorks and Alpha VxWorks.
7784 @end ignore
7785 See also the packages @code{GNAT.Traceback} and
7786 @code{GNAT.Traceback.Symbolic} for more information.
7787 @ifclear vms
7788 Note that on x86 ports, you must not use @option{-fomit-frame-pointer}
7789 @command{gcc} option.
7790 @end ifclear
7791
7792 @item ^-F^/FORCE_ELABS_FLAGS^
7793 @cindex @option{^-F^/FORCE_ELABS_FLAGS^} (@command{gnatbind})
7794 Force the checks of elaboration flags. @command{gnatbind} does not normally
7795 generate checks of elaboration flags for the main executable, except when
7796 a Stand-Alone Library is used. However, there are cases when this cannot be
7797 detected by gnatbind. An example is importing an interface of a Stand-Alone
7798 Library through a pragma Import and only specifying through a linker switch
7799 this Stand-Alone Library. This switch is used to guarantee that elaboration
7800 flag checks are generated.
7801
7802 @item ^-h^/HELP^
7803 @cindex @option{^-h^/HELP^} (@command{gnatbind})
7804 Output usage (help) information
7805
7806 @item ^-I^/SEARCH^
7807 @cindex @option{^-I^/SEARCH^} (@command{gnatbind})
7808 Specify directory to be searched for source and ALI files.
7809
7810 @item ^-I-^/NOCURRENT_DIRECTORY^
7811 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gnatbind})
7812 Do not look for sources in the current directory where @code{gnatbind} was
7813 invoked, and do not look for ALI files in the directory containing the
7814 ALI file named in the @code{gnatbind} command line.
7815
7816 @item ^-l^/ORDER_OF_ELABORATION^
7817 @cindex @option{^-l^/ORDER_OF_ELABORATION^} (@command{gnatbind})
7818 Output chosen elaboration order.
7819
7820 @item ^-L@var{xxx}^/BUILD_LIBRARY=@var{xxx}^
7821 @cindex @option{^-L^/BUILD_LIBRARY^} (@command{gnatbind})
7822 Bind the units for library building. In this case the adainit and
7823 adafinal procedures (@pxref{Binding with Non-Ada Main Programs})
7824 are renamed to ^@var{xxx}init^@var{XXX}INIT^ and
7825 ^@var{xxx}final^@var{XXX}FINAL^.
7826 Implies ^-n^/NOCOMPILE^.
7827 @ifclear vms
7828 (@xref{GNAT and Libraries}, for more details.)
7829 @end ifclear
7830 @ifset vms
7831 On OpenVMS, these init and final procedures are exported in uppercase
7832 letters. For example if /BUILD_LIBRARY=toto is used, the exported name of
7833 the init procedure will be "TOTOINIT" and the exported name of the final
7834 procedure will be "TOTOFINAL".
7835 @end ifset
7836
7837 @item ^-Mxyz^/RENAME_MAIN=xyz^
7838 @cindex @option{^-M^/RENAME_MAIN^} (@command{gnatbind})
7839 Rename generated main program from main to xyz. This option is
7840 supported on cross environments only.
7841
7842 @item ^-m^/ERROR_LIMIT=^@var{n}
7843 @cindex @option{^-m^/ERROR_LIMIT^} (@command{gnatbind})
7844 Limit number of detected errors or warnings to @var{n}, where @var{n} is
7845 in the range 1..999999. The default value if no switch is
7846 given is 9999. If the number of warnings reaches this limit, then a
7847 message is output and further warnings are suppressed, the bind
7848 continues in this case. If the number of errors reaches this
7849 limit, then a message is output and the bind is abandoned.
7850 A value of zero means that no limit is enforced. The equal
7851 sign is optional.
7852
7853 @ifset unw
7854 Furthermore, under Windows, the sources pointed to by the libraries path
7855 set in the registry are not searched for.
7856 @end ifset
7857
7858 @item ^-n^/NOMAIN^
7859 @cindex @option{^-n^/NOMAIN^} (@command{gnatbind})
7860 No main program.
7861
7862 @item -nostdinc
7863 @cindex @option{-nostdinc} (@command{gnatbind})
7864 Do not look for sources in the system default directory.
7865
7866 @item -nostdlib
7867 @cindex @option{-nostdlib} (@command{gnatbind})
7868 Do not look for library files in the system default directory.
7869
7870 @item --RTS=@var{rts-path}
7871 @cindex @option{--RTS} (@code{gnatbind})
7872 Specifies the default location of the runtime library. Same meaning as the
7873 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
7874
7875 @item ^-o ^/OUTPUT=^@var{file}
7876 @cindex @option{^-o ^/OUTPUT^} (@command{gnatbind})
7877 Name the output file @var{file} (default is @file{b~@var{xxx}.adb}).
7878 Note that if this option is used, then linking must be done manually,
7879 gnatlink cannot be used.
7880
7881 @item ^-O^/OBJECT_LIST^
7882 @cindex @option{^-O^/OBJECT_LIST^} (@command{gnatbind})
7883 Output object list.
7884
7885 @item ^-p^/PESSIMISTIC_ELABORATION^
7886 @cindex @option{^-p^/PESSIMISTIC_ELABORATION^} (@command{gnatbind})
7887 Pessimistic (worst-case) elaboration order
7888
7889 @item ^-R^-R^
7890 @cindex @option{^-R^-R^} (@command{gnatbind})
7891 Output closure source list.
7892
7893 @item ^-s^/READ_SOURCES=ALL^
7894 @cindex @option{^-s^/READ_SOURCES=ALL^} (@command{gnatbind})
7895 Require all source files to be present.
7896
7897 @item ^-S@var{xxx}^/INITIALIZE_SCALARS=@var{xxx}^
7898 @cindex @option{^-S^/INITIALIZE_SCALARS^} (@command{gnatbind})
7899 Specifies the value to be used when detecting uninitialized scalar
7900 objects with pragma Initialize_Scalars.
7901 The @var{xxx} ^string specified with the switch^option^ may be either
7902 @itemize @bullet
7903 @item ``@option{^in^INVALID^}'' requesting an invalid value where possible
7904 @item ``@option{^lo^LOW^}'' for the lowest possible value
7905 @item ``@option{^hi^HIGH^}'' for the highest possible value
7906 @item ``@option{@var{xx}}'' for a value consisting of repeated bytes with the
7907 value @code{16#@var{xx}#} (i.e., @var{xx} is a string of two hexadecimal digits).
7908 @end itemize
7909
7910 In addition, you can specify @option{-Sev} to indicate that the value is
7911 to be set at run time. In this case, the program will look for an environment
7912 @cindex GNAT_INIT_SCALARS
7913 variable of the form @env{GNAT_INIT_SCALARS=@var{xx}}, where @var{xx} is one
7914 of @option{in/lo/hi/@var{xx}} with the same meanings as above.
7915 If no environment variable is found, or if it does not have a valid value,
7916 then the default is @option{in} (invalid values).
7917
7918 @ifclear vms
7919 @item -static
7920 @cindex @option{-static} (@code{gnatbind})
7921 Link against a static GNAT run time.
7922
7923 @item -shared
7924 @cindex @option{-shared} (@code{gnatbind})
7925 Link against a shared GNAT run time when available.
7926 @end ifclear
7927
7928 @item ^-t^/NOTIME_STAMP_CHECK^
7929 @cindex @option{^-t^/NOTIME_STAMP_CHECK^} (@code{gnatbind})
7930 Tolerate time stamp and other consistency errors
7931
7932 @item ^-T@var{n}^/TIME_SLICE=@var{n}^
7933 @cindex @option{^-T^/TIME_SLICE^} (@code{gnatbind})
7934 Set the time slice value to @var{n} milliseconds. If the system supports
7935 the specification of a specific time slice value, then the indicated value
7936 is used. If the system does not support specific time slice values, but
7937 does support some general notion of round-robin scheduling, then any
7938 nonzero value will activate round-robin scheduling.
7939
7940 A value of zero is treated specially. It turns off time
7941 slicing, and in addition, indicates to the tasking run time that the
7942 semantics should match as closely as possible the Annex D
7943 requirements of the Ada RM, and in particular sets the default
7944 scheduling policy to @code{FIFO_Within_Priorities}.
7945
7946 @item ^-u@var{n}^/DYNAMIC_STACK_USAGE=@var{n}^
7947 @cindex @option{^-u^/DYNAMIC_STACK_USAGE^} (@code{gnatbind})
7948 Enable dynamic stack usage, with @var{n} results stored and displayed
7949 at program termination. A result is generated when a task
7950 terminates. Results that can't be stored are displayed on the fly, at
7951 task termination. This option is currently not supported on Itanium
7952 platforms. (See @ref{Dynamic Stack Usage Analysis} for details.)
7953
7954 @item ^-v^/REPORT_ERRORS=VERBOSE^
7955 @cindex @option{^-v^/REPORT_ERRORS=VERBOSE^} (@code{gnatbind})
7956 Verbose mode. Write error messages, header, summary output to
7957 @file{stdout}.
7958
7959 @ifclear vms
7960 @item -w@var{x}
7961 @cindex @option{-w} (@code{gnatbind})
7962 Warning mode (@var{x}=s/e for suppress/treat as error)
7963 @end ifclear
7964
7965 @ifset vms
7966 @item /WARNINGS=NORMAL
7967 @cindex @option{/WARNINGS} (@code{gnatbind})
7968 Normal warnings mode. Warnings are issued but ignored
7969
7970 @item /WARNINGS=SUPPRESS
7971 @cindex @option{/WARNINGS} (@code{gnatbind})
7972 All warning messages are suppressed
7973
7974 @item /WARNINGS=ERROR
7975 @cindex @option{/WARNINGS} (@code{gnatbind})
7976 Warning messages are treated as fatal errors
7977 @end ifset
7978
7979 @item ^-Wx^/WIDE_CHARACTER_ENCODING=^@var{e}
7980 @cindex @option{^-Wx^/WIDE_CHARACTER_ENCODING^} (@code{gnatbind})
7981 Override default wide character encoding for standard Text_IO files.
7982
7983 @item ^-x^/READ_SOURCES=NONE^
7984 @cindex @option{^-x^/READ_SOURCES^} (@code{gnatbind})
7985 Exclude source files (check object consistency only).
7986
7987 @ifset vms
7988 @item /READ_SOURCES=AVAILABLE
7989 @cindex @option{/READ_SOURCES} (@code{gnatbind})
7990 Default mode, in which sources are checked for consistency only if
7991 they are available.
7992 @end ifset
7993
7994 @item ^-y^/ENABLE_LEAP_SECONDS^
7995 @cindex @option{^-y^/ENABLE_LEAP_SECONDS^} (@code{gnatbind})
7996 Enable leap seconds support in @code{Ada.Calendar} and its children.
7997
7998 @item ^-z^/ZERO_MAIN^
7999 @cindex @option{^-z^/ZERO_MAIN^} (@code{gnatbind})
8000 No main subprogram.
8001 @end table
8002
8003 @ifclear vms
8004 @noindent
8005 You may obtain this listing of switches by running @code{gnatbind} with
8006 no arguments.
8007 @end ifclear
8008
8009 @node Consistency-Checking Modes
8010 @subsection Consistency-Checking Modes
8011
8012 @noindent
8013 As described earlier, by default @code{gnatbind} checks
8014 that object files are consistent with one another and are consistent
8015 with any source files it can locate. The following switches control binder
8016 access to sources.
8017
8018 @table @option
8019 @c !sort!
8020 @item ^-s^/READ_SOURCES=ALL^
8021 @cindex @option{^-s^/READ_SOURCES=ALL^} (@code{gnatbind})
8022 Require source files to be present. In this mode, the binder must be
8023 able to locate all source files that are referenced, in order to check
8024 their consistency. In normal mode, if a source file cannot be located it
8025 is simply ignored. If you specify this switch, a missing source
8026 file is an error.
8027
8028 @item ^-Wx^/WIDE_CHARACTER_ENCODING=^@var{e}
8029 @cindex @option{^-Wx^/WIDE_CHARACTER_ENCODING^} (@code{gnatbind})
8030 Override default wide character encoding for standard Text_IO files.
8031 Normally the default wide character encoding method used for standard
8032 [Wide_[Wide_]]Text_IO files is taken from the encoding specified for
8033 the main source input (see description of switch
8034 @option{^-gnatWx^/WIDE_CHARACTER_ENCODING^} for the compiler). The
8035 use of this switch for the binder (which has the same set of
8036 possible arguments) overrides this default as specified.
8037
8038 @item ^-x^/READ_SOURCES=NONE^
8039 @cindex @option{^-x^/READ_SOURCES=NONE^} (@code{gnatbind})
8040 Exclude source files. In this mode, the binder only checks that ALI
8041 files are consistent with one another. Source files are not accessed.
8042 The binder runs faster in this mode, and there is still a guarantee that
8043 the resulting program is self-consistent.
8044 If a source file has been edited since it was last compiled, and you
8045 specify this switch, the binder will not detect that the object
8046 file is out of date with respect to the source file. Note that this is the
8047 mode that is automatically used by @command{gnatmake} because in this
8048 case the checking against sources has already been performed by
8049 @command{gnatmake} in the course of compilation (i.e.@: before binding).
8050
8051 @ifset vms
8052 @item /READ_SOURCES=AVAILABLE
8053 @cindex @code{/READ_SOURCES=AVAILABLE} (@code{gnatbind})
8054 This is the default mode in which source files are checked if they are
8055 available, and ignored if they are not available.
8056 @end ifset
8057 @end table
8058
8059 @node Binder Error Message Control
8060 @subsection Binder Error Message Control
8061
8062 @noindent
8063 The following switches provide control over the generation of error
8064 messages from the binder:
8065
8066 @table @option
8067 @c !sort!
8068 @item ^-v^/REPORT_ERRORS=VERBOSE^
8069 @cindex @option{^-v^/REPORT_ERRORS=VERBOSE^} (@code{gnatbind})
8070 Verbose mode. In the normal mode, brief error messages are generated to
8071 @file{stderr}. If this switch is present, a header is written
8072 to @file{stdout} and any error messages are directed to @file{stdout}.
8073 All that is written to @file{stderr} is a brief summary message.
8074
8075 @item ^-b^/REPORT_ERRORS=BRIEF^
8076 @cindex @option{^-b^/REPORT_ERRORS=BRIEF^} (@code{gnatbind})
8077 Generate brief error messages to @file{stderr} even if verbose mode is
8078 specified. This is relevant only when used with the
8079 @option{^-v^/REPORT_ERRORS=VERBOSE^} switch.
8080
8081 @ifclear vms
8082 @item -m@var{n}
8083 @cindex @option{-m} (@code{gnatbind})
8084 Limits the number of error messages to @var{n}, a decimal integer in the
8085 range 1-999. The binder terminates immediately if this limit is reached.
8086
8087 @item -M@var{xxx}
8088 @cindex @option{-M} (@code{gnatbind})
8089 Renames the generated main program from @code{main} to @code{xxx}.
8090 This is useful in the case of some cross-building environments, where
8091 the actual main program is separate from the one generated
8092 by @code{gnatbind}.
8093 @end ifclear
8094
8095 @item ^-ws^/WARNINGS=SUPPRESS^
8096 @cindex @option{^-ws^/WARNINGS=SUPPRESS^} (@code{gnatbind})
8097 @cindex Warnings
8098 Suppress all warning messages.
8099
8100 @item ^-we^/WARNINGS=ERROR^
8101 @cindex @option{^-we^/WARNINGS=ERROR^} (@code{gnatbind})
8102 Treat any warning messages as fatal errors.
8103
8104 @ifset vms
8105 @item /WARNINGS=NORMAL
8106 Standard mode with warnings generated, but warnings do not get treated
8107 as errors.
8108 @end ifset
8109
8110 @item ^-t^/NOTIME_STAMP_CHECK^
8111 @cindex @option{^-t^/NOTIME_STAMP_CHECK^} (@code{gnatbind})
8112 @cindex Time stamp checks, in binder
8113 @cindex Binder consistency checks
8114 @cindex Consistency checks, in binder
8115 The binder performs a number of consistency checks including:
8116
8117 @itemize @bullet
8118 @item
8119 Check that time stamps of a given source unit are consistent
8120 @item
8121 Check that checksums of a given source unit are consistent
8122 @item
8123 Check that consistent versions of @code{GNAT} were used for compilation
8124 @item
8125 Check consistency of configuration pragmas as required
8126 @end itemize
8127
8128 @noindent
8129 Normally failure of such checks, in accordance with the consistency
8130 requirements of the Ada Reference Manual, causes error messages to be
8131 generated which abort the binder and prevent the output of a binder
8132 file and subsequent link to obtain an executable.
8133
8134 The @option{^-t^/NOTIME_STAMP_CHECK^} switch converts these error messages
8135 into warnings, so that
8136 binding and linking can continue to completion even in the presence of such
8137 errors. The result may be a failed link (due to missing symbols), or a
8138 non-functional executable which has undefined semantics.
8139 @emph{This means that
8140 @option{^-t^/NOTIME_STAMP_CHECK^} should be used only in unusual situations,
8141 with extreme care.}
8142 @end table
8143
8144 @node Elaboration Control
8145 @subsection Elaboration Control
8146
8147 @noindent
8148 The following switches provide additional control over the elaboration
8149 order. For full details see @ref{Elaboration Order Handling in GNAT}.
8150
8151 @table @option
8152 @item ^-p^/PESSIMISTIC_ELABORATION^
8153 @cindex @option{^-p^/PESSIMISTIC_ELABORATION^} (@code{gnatbind})
8154 Normally the binder attempts to choose an elaboration order that is
8155 likely to minimize the likelihood of an elaboration order error resulting
8156 in raising a @code{Program_Error} exception. This switch reverses the
8157 action of the binder, and requests that it deliberately choose an order
8158 that is likely to maximize the likelihood of an elaboration error.
8159 This is useful in ensuring portability and avoiding dependence on
8160 accidental fortuitous elaboration ordering.
8161
8162 Normally it only makes sense to use the @option{^-p^/PESSIMISTIC_ELABORATION^}
8163 switch if dynamic
8164 elaboration checking is used (@option{-gnatE} switch used for compilation).
8165 This is because in the default static elaboration mode, all necessary
8166 @code{Elaborate} and @code{Elaborate_All} pragmas are implicitly inserted.
8167 These implicit pragmas are still respected by the binder in
8168 @option{^-p^/PESSIMISTIC_ELABORATION^} mode, so a
8169 safe elaboration order is assured.
8170 @end table
8171
8172 @node Output Control
8173 @subsection Output Control
8174
8175 @noindent
8176 The following switches allow additional control over the output
8177 generated by the binder.
8178
8179 @table @option
8180 @c !sort!
8181
8182 @item ^-A^/BIND_FILE=ADA^
8183 @cindex @option{^-A^/BIND_FILE=ADA^} (@code{gnatbind})
8184 Generate binder program in Ada (default). The binder program is named
8185 @file{b~@var{mainprog}.adb} by default. This can be changed with
8186 @option{^-o^/OUTPUT^} @code{gnatbind} option.
8187
8188 @item ^-c^/NOOUTPUT^
8189 @cindex @option{^-c^/NOOUTPUT^} (@code{gnatbind})
8190 Check only. Do not generate the binder output file. In this mode the
8191 binder performs all error checks but does not generate an output file.
8192
8193 @item ^-C^/BIND_FILE=C^
8194 @cindex @option{^-C^/BIND_FILE=C^} (@code{gnatbind})
8195 Generate binder program in C. The binder program is named
8196 @file{b_@var{mainprog}.c}.
8197 This can be changed with @option{^-o^/OUTPUT^} @code{gnatbind}
8198 option.
8199
8200 @item ^-e^/ELABORATION_DEPENDENCIES^
8201 @cindex @option{^-e^/ELABORATION_DEPENDENCIES^} (@code{gnatbind})
8202 Output complete list of elaboration-order dependencies, showing the
8203 reason for each dependency. This output can be rather extensive but may
8204 be useful in diagnosing problems with elaboration order. The output is
8205 written to @file{stdout}.
8206
8207 @item ^-h^/HELP^
8208 @cindex @option{^-h^/HELP^} (@code{gnatbind})
8209 Output usage information. The output is written to @file{stdout}.
8210
8211 @item ^-K^/LINKER_OPTION_LIST^
8212 @cindex @option{^-K^/LINKER_OPTION_LIST^} (@code{gnatbind})
8213 Output linker options to @file{stdout}. Includes library search paths,
8214 contents of pragmas Ident and Linker_Options, and libraries added
8215 by @code{gnatbind}.
8216
8217 @item ^-l^/ORDER_OF_ELABORATION^
8218 @cindex @option{^-l^/ORDER_OF_ELABORATION^} (@code{gnatbind})
8219 Output chosen elaboration order. The output is written to @file{stdout}.
8220
8221 @item ^-O^/OBJECT_LIST^
8222 @cindex @option{^-O^/OBJECT_LIST^} (@code{gnatbind})
8223 Output full names of all the object files that must be linked to provide
8224 the Ada component of the program. The output is written to @file{stdout}.
8225 This list includes the files explicitly supplied and referenced by the user
8226 as well as implicitly referenced run-time unit files. The latter are
8227 omitted if the corresponding units reside in shared libraries. The
8228 directory names for the run-time units depend on the system configuration.
8229
8230 @item ^-o ^/OUTPUT=^@var{file}
8231 @cindex @option{^-o^/OUTPUT^} (@code{gnatbind})
8232 Set name of output file to @var{file} instead of the normal
8233 @file{b~@var{mainprog}.adb} default. Note that @var{file} denote the Ada
8234 binder generated body filename. In C mode you would normally give
8235 @var{file} an extension of @file{.c} because it will be a C source program.
8236 Note that if this option is used, then linking must be done manually.
8237 It is not possible to use gnatlink in this case, since it cannot locate
8238 the binder file.
8239
8240 @item ^-r^/RESTRICTION_LIST^
8241 @cindex @option{^-r^/RESTRICTION_LIST^} (@code{gnatbind})
8242 Generate list of @code{pragma Restrictions} that could be applied to
8243 the current unit. This is useful for code audit purposes, and also may
8244 be used to improve code generation in some cases.
8245
8246 @end table
8247
8248 @node Binding with Non-Ada Main Programs
8249 @subsection Binding with Non-Ada Main Programs
8250
8251 @noindent
8252 In our description so far we have assumed that the main
8253 program is in Ada, and that the task of the binder is to generate a
8254 corresponding function @code{main} that invokes this Ada main
8255 program. GNAT also supports the building of executable programs where
8256 the main program is not in Ada, but some of the called routines are
8257 written in Ada and compiled using GNAT (@pxref{Mixed Language Programming}).
8258 The following switch is used in this situation:
8259
8260 @table @option
8261 @item ^-n^/NOMAIN^
8262 @cindex @option{^-n^/NOMAIN^} (@code{gnatbind})
8263 No main program. The main program is not in Ada.
8264 @end table
8265
8266 @noindent
8267 In this case, most of the functions of the binder are still required,
8268 but instead of generating a main program, the binder generates a file
8269 containing the following callable routines:
8270
8271 @table @code
8272 @item adainit
8273 @findex adainit
8274 You must call this routine to initialize the Ada part of the program by
8275 calling the necessary elaboration routines. A call to @code{adainit} is
8276 required before the first call to an Ada subprogram.
8277
8278 Note that it is assumed that the basic execution environment must be setup
8279 to be appropriate for Ada execution at the point where the first Ada
8280 subprogram is called. In particular, if the Ada code will do any
8281 floating-point operations, then the FPU must be setup in an appropriate
8282 manner. For the case of the x86, for example, full precision mode is
8283 required. The procedure GNAT.Float_Control.Reset may be used to ensure
8284 that the FPU is in the right state.
8285
8286 @item adafinal
8287 @findex adafinal
8288 You must call this routine to perform any library-level finalization
8289 required by the Ada subprograms. A call to @code{adafinal} is required
8290 after the last call to an Ada subprogram, and before the program
8291 terminates.
8292 @end table
8293
8294 @noindent
8295 If the @option{^-n^/NOMAIN^} switch
8296 @cindex @option{^-n^/NOMAIN^} (@command{gnatbind})
8297 @cindex Binder, multiple input files
8298 is given, more than one ALI file may appear on
8299 the command line for @code{gnatbind}. The normal @dfn{closure}
8300 calculation is performed for each of the specified units. Calculating
8301 the closure means finding out the set of units involved by tracing
8302 @code{with} references. The reason it is necessary to be able to
8303 specify more than one ALI file is that a given program may invoke two or
8304 more quite separate groups of Ada units.
8305
8306 The binder takes the name of its output file from the last specified ALI
8307 file, unless overridden by the use of the @option{^-o file^/OUTPUT=file^}.
8308 @cindex @option{^-o^/OUTPUT^} (@command{gnatbind})
8309 The output is an Ada unit in source form that can
8310 be compiled with GNAT unless the -C switch is used in which case the
8311 output is a C source file, which must be compiled using the C compiler.
8312 This compilation occurs automatically as part of the @command{gnatlink}
8313 processing.
8314
8315 Currently the GNAT run time requires a FPU using 80 bits mode
8316 precision. Under targets where this is not the default it is required to
8317 call GNAT.Float_Control.Reset before using floating point numbers (this
8318 include float computation, float input and output) in the Ada code. A
8319 side effect is that this could be the wrong mode for the foreign code
8320 where floating point computation could be broken after this call.
8321
8322 @node Binding Programs with No Main Subprogram
8323 @subsection Binding Programs with No Main Subprogram
8324
8325 @noindent
8326 It is possible to have an Ada program which does not have a main
8327 subprogram. This program will call the elaboration routines of all the
8328 packages, then the finalization routines.
8329
8330 The following switch is used to bind programs organized in this manner:
8331
8332 @table @option
8333 @item ^-z^/ZERO_MAIN^
8334 @cindex @option{^-z^/ZERO_MAIN^} (@code{gnatbind})
8335 Normally the binder checks that the unit name given on the command line
8336 corresponds to a suitable main subprogram. When this switch is used,
8337 a list of ALI files can be given, and the execution of the program
8338 consists of elaboration of these units in an appropriate order. Note
8339 that the default wide character encoding method for standard Text_IO
8340 files is always set to Brackets if this switch is set (you can use
8341 the binder switch
8342 @option{^-Wx^WIDE_CHARACTER_ENCODING^} to override this default).
8343 @end table
8344
8345 @node Command-Line Access
8346 @section Command-Line Access
8347
8348 @noindent
8349 The package @code{Ada.Command_Line} provides access to the command-line
8350 arguments and program name. In order for this interface to operate
8351 correctly, the two variables
8352
8353 @smallexample
8354 @group
8355 int gnat_argc;
8356 char **gnat_argv;
8357 @end group
8358 @end smallexample
8359
8360 @noindent
8361 @findex gnat_argv
8362 @findex gnat_argc
8363 are declared in one of the GNAT library routines. These variables must
8364 be set from the actual @code{argc} and @code{argv} values passed to the
8365 main program. With no @option{^n^/NOMAIN^} present, @code{gnatbind}
8366 generates the C main program to automatically set these variables.
8367 If the @option{^n^/NOMAIN^} switch is used, there is no automatic way to
8368 set these variables. If they are not set, the procedures in
8369 @code{Ada.Command_Line} will not be available, and any attempt to use
8370 them will raise @code{Constraint_Error}. If command line access is
8371 required, your main program must set @code{gnat_argc} and
8372 @code{gnat_argv} from the @code{argc} and @code{argv} values passed to
8373 it.
8374
8375 @node Search Paths for gnatbind
8376 @section Search Paths for @code{gnatbind}
8377
8378 @noindent
8379 The binder takes the name of an ALI file as its argument and needs to
8380 locate source files as well as other ALI files to verify object consistency.
8381
8382 For source files, it follows exactly the same search rules as @command{gcc}
8383 (@pxref{Search Paths and the Run-Time Library (RTL)}). For ALI files the
8384 directories searched are:
8385
8386 @enumerate
8387 @item
8388 The directory containing the ALI file named in the command line, unless
8389 the switch @option{^-I-^/NOCURRENT_DIRECTORY^} is specified.
8390
8391 @item
8392 All directories specified by @option{^-I^/SEARCH^}
8393 switches on the @code{gnatbind}
8394 command line, in the order given.
8395
8396 @item
8397 @findex ADA_PRJ_OBJECTS_FILE
8398 Each of the directories listed in the text file whose name is given
8399 by the @env{ADA_PRJ_OBJECTS_FILE} ^environment variable^logical name^.
8400
8401 @noindent
8402 @env{ADA_PRJ_OBJECTS_FILE} is normally set by gnatmake or by the ^gnat^GNAT^
8403 driver when project files are used. It should not normally be set
8404 by other means.
8405
8406 @item
8407 @findex ADA_OBJECTS_PATH
8408 Each of the directories listed in the value of the
8409 @env{ADA_OBJECTS_PATH} ^environment variable^logical name^.
8410 @ifset unw
8411 Construct this value
8412 exactly as the @env{PATH} environment variable: a list of directory
8413 names separated by colons (semicolons when working with the NT version
8414 of GNAT).
8415 @end ifset
8416 @ifset vms
8417 Normally, define this value as a logical name containing a comma separated
8418 list of directory names.
8419
8420 This variable can also be defined by means of an environment string
8421 (an argument to the HP C exec* set of functions).
8422
8423 Logical Name:
8424 @smallexample
8425 DEFINE ANOTHER_PATH FOO:[BAG]
8426 DEFINE ADA_OBJECTS_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
8427 @end smallexample
8428
8429 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
8430 first, followed by the standard Ada
8431 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADALIB].
8432 If this is not redefined, the user will obtain the HP Ada 83 IO packages
8433 (Text_IO, Sequential_IO, etc)
8434 instead of the standard Ada packages. Thus, in order to get the standard Ada
8435 packages by default, ADA_OBJECTS_PATH must be redefined.
8436 @end ifset
8437
8438 @item
8439 The content of the @file{ada_object_path} file which is part of the GNAT
8440 installation tree and is used to store standard libraries such as the
8441 GNAT Run Time Library (RTL) unless the switch @option{-nostdlib} is
8442 specified.
8443 @ifclear vms
8444 @ref{Installing a library}
8445 @end ifclear
8446 @end enumerate
8447
8448 @noindent
8449 In the binder the switch @option{^-I^/SEARCH^}
8450 @cindex @option{^-I^/SEARCH^} (@command{gnatbind})
8451 is used to specify both source and
8452 library file paths. Use @option{^-aI^/SOURCE_SEARCH^}
8453 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatbind})
8454 instead if you want to specify
8455 source paths only, and @option{^-aO^/LIBRARY_SEARCH^}
8456 @cindex @option{^-aO^/LIBRARY_SEARCH^} (@command{gnatbind})
8457 if you want to specify library paths
8458 only. This means that for the binder
8459 @option{^-I^/SEARCH=^}@var{dir} is equivalent to
8460 @option{^-aI^/SOURCE_SEARCH=^}@var{dir}
8461 @option{^-aO^/OBJECT_SEARCH=^}@var{dir}.
8462 The binder generates the bind file (a C language source file) in the
8463 current working directory.
8464
8465 @findex Ada
8466 @findex System
8467 @findex Interfaces
8468 @findex GNAT
8469 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
8470 children make up the GNAT Run-Time Library, together with the package
8471 GNAT and its children, which contain a set of useful additional
8472 library functions provided by GNAT. The sources for these units are
8473 needed by the compiler and are kept together in one directory. The ALI
8474 files and object files generated by compiling the RTL are needed by the
8475 binder and the linker and are kept together in one directory, typically
8476 different from the directory containing the sources. In a normal
8477 installation, you need not specify these directory names when compiling
8478 or binding. Either the environment variables or the built-in defaults
8479 cause these files to be found.
8480
8481 Besides simplifying access to the RTL, a major use of search paths is
8482 in compiling sources from multiple directories. This can make
8483 development environments much more flexible.
8484
8485 @node Examples of gnatbind Usage
8486 @section Examples of @code{gnatbind} Usage
8487
8488 @noindent
8489 This section contains a number of examples of using the GNAT binding
8490 utility @code{gnatbind}.
8491
8492 @table @code
8493 @item gnatbind hello
8494 The main program @code{Hello} (source program in @file{hello.adb}) is
8495 bound using the standard switch settings. The generated main program is
8496 @file{b~hello.adb}. This is the normal, default use of the binder.
8497
8498 @ifclear vms
8499 @item gnatbind hello -o mainprog.adb
8500 @end ifclear
8501 @ifset vms
8502 @item gnatbind HELLO.ALI /OUTPUT=Mainprog.ADB
8503 @end ifset
8504 The main program @code{Hello} (source program in @file{hello.adb}) is
8505 bound using the standard switch settings. The generated main program is
8506 @file{mainprog.adb} with the associated spec in
8507 @file{mainprog.ads}. Note that you must specify the body here not the
8508 spec, in the case where the output is in Ada. Note that if this option
8509 is used, then linking must be done manually, since gnatlink will not
8510 be able to find the generated file.
8511
8512 @ifclear vms
8513 @item gnatbind main -C -o mainprog.c -x
8514 @end ifclear
8515 @ifset vms
8516 @item gnatbind MAIN.ALI /BIND_FILE=C /OUTPUT=Mainprog.C /READ_SOURCES=NONE
8517 @end ifset
8518 The main program @code{Main} (source program in
8519 @file{main.adb}) is bound, excluding source files from the
8520 consistency checking, generating
8521 the file @file{mainprog.c}.
8522
8523 @ifclear vms
8524 @item gnatbind -x main_program -C -o mainprog.c
8525 This command is exactly the same as the previous example. Switches may
8526 appear anywhere in the command line, and single letter switches may be
8527 combined into a single switch.
8528 @end ifclear
8529
8530 @ifclear vms
8531 @item gnatbind -n math dbase -C -o ada-control.c
8532 @end ifclear
8533 @ifset vms
8534 @item gnatbind /NOMAIN math dbase /BIND_FILE=C /OUTPUT=ada-control.c
8535 @end ifset
8536 The main program is in a language other than Ada, but calls to
8537 subprograms in packages @code{Math} and @code{Dbase} appear. This call
8538 to @code{gnatbind} generates the file @file{ada-control.c} containing
8539 the @code{adainit} and @code{adafinal} routines to be called before and
8540 after accessing the Ada units.
8541 @end table
8542
8543 @c ------------------------------------
8544 @node Linking Using gnatlink
8545 @chapter Linking Using @command{gnatlink}
8546 @c ------------------------------------
8547 @findex gnatlink
8548
8549 @noindent
8550 This chapter discusses @command{gnatlink}, a tool that links
8551 an Ada program and builds an executable file. This utility
8552 invokes the system linker ^(via the @command{gcc} command)^^
8553 with a correct list of object files and library references.
8554 @command{gnatlink} automatically determines the list of files and
8555 references for the Ada part of a program. It uses the binder file
8556 generated by the @command{gnatbind} to determine this list.
8557
8558 Note: to invoke @code{gnatlink} with a project file, use the @code{gnat}
8559 driver (see @ref{The GNAT Driver and Project Files}).
8560
8561 @menu
8562 * Running gnatlink::
8563 * Switches for gnatlink::
8564 @end menu
8565
8566 @node Running gnatlink
8567 @section Running @command{gnatlink}
8568
8569 @noindent
8570 The form of the @command{gnatlink} command is
8571
8572 @smallexample
8573 $ gnatlink @ovar{switches} @var{mainprog}@r{[}.ali@r{]}
8574 @ovar{non-Ada objects} @ovar{linker options}
8575 @end smallexample
8576
8577 @noindent
8578 The arguments of @command{gnatlink} (switches, main @file{ALI} file,
8579 non-Ada objects
8580 or linker options) may be in any order, provided that no non-Ada object may
8581 be mistaken for a main @file{ALI} file.
8582 Any file name @file{F} without the @file{.ali}
8583 extension will be taken as the main @file{ALI} file if a file exists
8584 whose name is the concatenation of @file{F} and @file{.ali}.
8585
8586 @noindent
8587 @file{@var{mainprog}.ali} references the ALI file of the main program.
8588 The @file{.ali} extension of this file can be omitted. From this
8589 reference, @command{gnatlink} locates the corresponding binder file
8590 @file{b~@var{mainprog}.adb} and, using the information in this file along
8591 with the list of non-Ada objects and linker options, constructs a
8592 linker command file to create the executable.
8593
8594 The arguments other than the @command{gnatlink} switches and the main
8595 @file{ALI} file are passed to the linker uninterpreted.
8596 They typically include the names of
8597 object files for units written in other languages than Ada and any library
8598 references required to resolve references in any of these foreign language
8599 units, or in @code{Import} pragmas in any Ada units.
8600
8601 @var{linker options} is an optional list of linker specific
8602 switches.
8603 The default linker called by gnatlink is @command{gcc} which in
8604 turn calls the appropriate system linker.
8605 Standard options for the linker such as @option{-lmy_lib} or
8606 @option{-Ldir} can be added as is.
8607 For options that are not recognized by
8608 @command{gcc} as linker options, use the @command{gcc} switches
8609 @option{-Xlinker} or @option{-Wl,}.
8610 Refer to the GCC documentation for
8611 details. Here is an example showing how to generate a linker map:
8612
8613 @smallexample
8614 $ ^gnatlink my_prog -Wl,-Map,MAPFILE^GNAT LINK my_prog.ali /MAP^
8615 @end smallexample
8616
8617 Using @var{linker options} it is possible to set the program stack and
8618 heap size.
8619 @ifset unw
8620 See @ref{Setting Stack Size from gnatlink} and
8621 @ref{Setting Heap Size from gnatlink}.
8622 @end ifset
8623
8624 @command{gnatlink} determines the list of objects required by the Ada
8625 program and prepends them to the list of objects passed to the linker.
8626 @command{gnatlink} also gathers any arguments set by the use of
8627 @code{pragma Linker_Options} and adds them to the list of arguments
8628 presented to the linker.
8629
8630 @ifset vms
8631 @command{gnatlink} accepts the following types of extra files on the command
8632 line: objects (@file{.OBJ}), libraries (@file{.OLB}), sharable images
8633 (@file{.EXE}), and options files (@file{.OPT}). These are recognized and
8634 handled according to their extension.
8635 @end ifset
8636
8637 @node Switches for gnatlink
8638 @section Switches for @command{gnatlink}
8639
8640 @noindent
8641 The following switches are available with the @command{gnatlink} utility:
8642
8643 @table @option
8644 @c !sort!
8645
8646 @item --version
8647 @cindex @option{--version} @command{gnatlink}
8648 Display Copyright and version, then exit disregarding all other options.
8649
8650 @item --help
8651 @cindex @option{--help} @command{gnatlink}
8652 If @option{--version} was not used, display usage, then exit disregarding
8653 all other options.
8654
8655 @item ^-A^/BIND_FILE=ADA^
8656 @cindex @option{^-A^/BIND_FILE=ADA^} (@command{gnatlink})
8657 The binder has generated code in Ada. This is the default.
8658
8659 @item ^-C^/BIND_FILE=C^
8660 @cindex @option{^-C^/BIND_FILE=C^} (@command{gnatlink})
8661 If instead of generating a file in Ada, the binder has generated one in
8662 C, then the linker needs to know about it. Use this switch to signal
8663 to @command{gnatlink} that the binder has generated C code rather than
8664 Ada code.
8665
8666 @item ^-f^/FORCE_OBJECT_FILE_LIST^
8667 @cindex Command line length
8668 @cindex @option{^-f^/FORCE_OBJECT_FILE_LIST^} (@command{gnatlink})
8669 On some targets, the command line length is limited, and @command{gnatlink}
8670 will generate a separate file for the linker if the list of object files
8671 is too long.
8672 The @option{^-f^/FORCE_OBJECT_FILE_LIST^} switch forces this file
8673 to be generated even if
8674 the limit is not exceeded. This is useful in some cases to deal with
8675 special situations where the command line length is exceeded.
8676
8677 @item ^-g^/DEBUG^
8678 @cindex Debugging information, including
8679 @cindex @option{^-g^/DEBUG^} (@command{gnatlink})
8680 The option to include debugging information causes the Ada bind file (in
8681 other words, @file{b~@var{mainprog}.adb}) to be compiled with
8682 @option{^-g^/DEBUG^}.
8683 In addition, the binder does not delete the @file{b~@var{mainprog}.adb},
8684 @file{b~@var{mainprog}.o} and @file{b~@var{mainprog}.ali} files.
8685 Without @option{^-g^/DEBUG^}, the binder removes these files by
8686 default. The same procedure apply if a C bind file was generated using
8687 @option{^-C^/BIND_FILE=C^} @code{gnatbind} option, in this case the filenames
8688 are @file{b_@var{mainprog}.c} and @file{b_@var{mainprog}.o}.
8689
8690 @item ^-n^/NOCOMPILE^
8691 @cindex @option{^-n^/NOCOMPILE^} (@command{gnatlink})
8692 Do not compile the file generated by the binder. This may be used when
8693 a link is rerun with different options, but there is no need to recompile
8694 the binder file.
8695
8696 @item ^-v^/VERBOSE^
8697 @cindex @option{^-v^/VERBOSE^} (@command{gnatlink})
8698 Causes additional information to be output, including a full list of the
8699 included object files. This switch option is most useful when you want
8700 to see what set of object files are being used in the link step.
8701
8702 @item ^-v -v^/VERBOSE/VERBOSE^
8703 @cindex @option{^-v -v^/VERBOSE/VERBOSE^} (@command{gnatlink})
8704 Very verbose mode. Requests that the compiler operate in verbose mode when
8705 it compiles the binder file, and that the system linker run in verbose mode.
8706
8707 @item ^-o ^/EXECUTABLE=^@var{exec-name}
8708 @cindex @option{^-o^/EXECUTABLE^} (@command{gnatlink})
8709 @var{exec-name} specifies an alternate name for the generated
8710 executable program. If this switch is omitted, the executable has the same
8711 name as the main unit. For example, @code{gnatlink try.ali} creates
8712 an executable called @file{^try^TRY.EXE^}.
8713
8714 @ifclear vms
8715 @item -b @var{target}
8716 @cindex @option{-b} (@command{gnatlink})
8717 Compile your program to run on @var{target}, which is the name of a
8718 system configuration. You must have a GNAT cross-compiler built if
8719 @var{target} is not the same as your host system.
8720
8721 @item -B@var{dir}
8722 @cindex @option{-B} (@command{gnatlink})
8723 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
8724 from @var{dir} instead of the default location. Only use this switch
8725 when multiple versions of the GNAT compiler are available.
8726 @xref{Directory Options,,, gcc, The GNU Compiler Collection},
8727 for further details. You would normally use the @option{-b} or
8728 @option{-V} switch instead.
8729
8730 @item --GCC=@var{compiler_name}
8731 @cindex @option{--GCC=compiler_name} (@command{gnatlink})
8732 Program used for compiling the binder file. The default is
8733 @command{gcc}. You need to use quotes around @var{compiler_name} if
8734 @code{compiler_name} contains spaces or other separator characters.
8735 As an example @option{--GCC="foo -x -y"} will instruct @command{gnatlink} to
8736 use @code{foo -x -y} as your compiler. Note that switch @option{-c} is always
8737 inserted after your command name. Thus in the above example the compiler
8738 command that will be used by @command{gnatlink} will be @code{foo -c -x -y}.
8739 A limitation of this syntax is that the name and path name of the executable
8740 itself must not include any embedded spaces. If the compiler executable is
8741 different from the default one (gcc or <prefix>-gcc), then the back-end
8742 switches in the ALI file are not used to compile the binder generated source.
8743 For example, this is the case with @option{--GCC="foo -x -y"}. But the back end
8744 switches will be used for @option{--GCC="gcc -gnatv"}. If several
8745 @option{--GCC=compiler_name} are used, only the last @var{compiler_name}
8746 is taken into account. However, all the additional switches are also taken
8747 into account. Thus,
8748 @option{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
8749 @option{--GCC="bar -x -y -z -t"}.
8750
8751 @item --LINK=@var{name}
8752 @cindex @option{--LINK=} (@command{gnatlink})
8753 @var{name} is the name of the linker to be invoked. This is especially
8754 useful in mixed language programs since languages such as C++ require
8755 their own linker to be used. When this switch is omitted, the default
8756 name for the linker is @command{gcc}. When this switch is used, the
8757 specified linker is called instead of @command{gcc} with exactly the same
8758 parameters that would have been passed to @command{gcc} so if the desired
8759 linker requires different parameters it is necessary to use a wrapper
8760 script that massages the parameters before invoking the real linker. It
8761 may be useful to control the exact invocation by using the verbose
8762 switch.
8763
8764 @end ifclear
8765
8766 @ifset vms
8767 @item /DEBUG=TRACEBACK
8768 @cindex @code{/DEBUG=TRACEBACK} (@command{gnatlink})
8769 This qualifier causes sufficient information to be included in the
8770 executable file to allow a traceback, but does not include the full
8771 symbol information needed by the debugger.
8772
8773 @item /IDENTIFICATION="<string>"
8774 @code{"<string>"} specifies the string to be stored in the image file
8775 identification field in the image header.
8776 It overrides any pragma @code{Ident} specified string.
8777
8778 @item /NOINHIBIT-EXEC
8779 Generate the executable file even if there are linker warnings.
8780
8781 @item /NOSTART_FILES
8782 Don't link in the object file containing the ``main'' transfer address.
8783 Used when linking with a foreign language main program compiled with an
8784 HP compiler.
8785
8786 @item /STATIC
8787 Prefer linking with object libraries over sharable images, even without
8788 /DEBUG.
8789 @end ifset
8790
8791 @end table
8792
8793 @node The GNAT Make Program gnatmake
8794 @chapter The GNAT Make Program @command{gnatmake}
8795 @findex gnatmake
8796
8797 @menu
8798 * Running gnatmake::
8799 * Switches for gnatmake::
8800 * Mode Switches for gnatmake::
8801 * Notes on the Command Line::
8802 * How gnatmake Works::
8803 * Examples of gnatmake Usage::
8804 @end menu
8805 @noindent
8806 A typical development cycle when working on an Ada program consists of
8807 the following steps:
8808
8809 @enumerate
8810 @item
8811 Edit some sources to fix bugs.
8812
8813 @item
8814 Add enhancements.
8815
8816 @item
8817 Compile all sources affected.
8818
8819 @item
8820 Rebind and relink.
8821
8822 @item
8823 Test.
8824 @end enumerate
8825
8826 @noindent
8827 The third step can be tricky, because not only do the modified files
8828 @cindex Dependency rules
8829 have to be compiled, but any files depending on these files must also be
8830 recompiled. The dependency rules in Ada can be quite complex, especially
8831 in the presence of overloading, @code{use} clauses, generics and inlined
8832 subprograms.
8833
8834 @command{gnatmake} automatically takes care of the third and fourth steps
8835 of this process. It determines which sources need to be compiled,
8836 compiles them, and binds and links the resulting object files.
8837
8838 Unlike some other Ada make programs, the dependencies are always
8839 accurately recomputed from the new sources. The source based approach of
8840 the GNAT compilation model makes this possible. This means that if
8841 changes to the source program cause corresponding changes in
8842 dependencies, they will always be tracked exactly correctly by
8843 @command{gnatmake}.
8844
8845 @node Running gnatmake
8846 @section Running @command{gnatmake}
8847
8848 @noindent
8849 The usual form of the @command{gnatmake} command is
8850
8851 @smallexample
8852 $ gnatmake @ovar{switches} @var{file_name}
8853 @ovar{file_names} @ovar{mode_switches}
8854 @end smallexample
8855
8856 @noindent
8857 The only required argument is one @var{file_name}, which specifies
8858 a compilation unit that is a main program. Several @var{file_names} can be
8859 specified: this will result in several executables being built.
8860 If @code{switches} are present, they can be placed before the first
8861 @var{file_name}, between @var{file_names} or after the last @var{file_name}.
8862 If @var{mode_switches} are present, they must always be placed after
8863 the last @var{file_name} and all @code{switches}.
8864
8865 If you are using standard file extensions (@file{.adb} and @file{.ads}), then the
8866 extension may be omitted from the @var{file_name} arguments. However, if
8867 you are using non-standard extensions, then it is required that the
8868 extension be given. A relative or absolute directory path can be
8869 specified in a @var{file_name}, in which case, the input source file will
8870 be searched for in the specified directory only. Otherwise, the input
8871 source file will first be searched in the directory where
8872 @command{gnatmake} was invoked and if it is not found, it will be search on
8873 the source path of the compiler as described in
8874 @ref{Search Paths and the Run-Time Library (RTL)}.
8875
8876 All @command{gnatmake} output (except when you specify
8877 @option{^-M^/DEPENDENCIES_LIST^}) is to
8878 @file{stderr}. The output produced by the
8879 @option{^-M^/DEPENDENCIES_LIST^} switch is send to
8880 @file{stdout}.
8881
8882 @node Switches for gnatmake
8883 @section Switches for @command{gnatmake}
8884
8885 @noindent
8886 You may specify any of the following switches to @command{gnatmake}:
8887
8888 @table @option
8889 @c !sort!
8890
8891 @item --version
8892 @cindex @option{--version} @command{gnatmake}
8893 Display Copyright and version, then exit disregarding all other options.
8894
8895 @item --help
8896 @cindex @option{--help} @command{gnatmake}
8897 If @option{--version} was not used, display usage, then exit disregarding
8898 all other options.
8899
8900 @ifclear vms
8901 @item --GCC=@var{compiler_name}
8902 @cindex @option{--GCC=compiler_name} (@command{gnatmake})
8903 Program used for compiling. The default is `@command{gcc}'. You need to use
8904 quotes around @var{compiler_name} if @code{compiler_name} contains
8905 spaces or other separator characters. As an example @option{--GCC="foo -x
8906 -y"} will instruct @command{gnatmake} to use @code{foo -x -y} as your
8907 compiler. A limitation of this syntax is that the name and path name of
8908 the executable itself must not include any embedded spaces. Note that
8909 switch @option{-c} is always inserted after your command name. Thus in the
8910 above example the compiler command that will be used by @command{gnatmake}
8911 will be @code{foo -c -x -y}. If several @option{--GCC=compiler_name} are
8912 used, only the last @var{compiler_name} is taken into account. However,
8913 all the additional switches are also taken into account. Thus,
8914 @option{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
8915 @option{--GCC="bar -x -y -z -t"}.
8916
8917 @item --GNATBIND=@var{binder_name}
8918 @cindex @option{--GNATBIND=binder_name} (@command{gnatmake})
8919 Program used for binding. The default is `@code{gnatbind}'. You need to
8920 use quotes around @var{binder_name} if @var{binder_name} contains spaces
8921 or other separator characters. As an example @option{--GNATBIND="bar -x
8922 -y"} will instruct @command{gnatmake} to use @code{bar -x -y} as your
8923 binder. Binder switches that are normally appended by @command{gnatmake}
8924 to `@code{gnatbind}' are now appended to the end of @code{bar -x -y}.
8925 A limitation of this syntax is that the name and path name of the executable
8926 itself must not include any embedded spaces.
8927
8928 @item --GNATLINK=@var{linker_name}
8929 @cindex @option{--GNATLINK=linker_name} (@command{gnatmake})
8930 Program used for linking. The default is `@command{gnatlink}'. You need to
8931 use quotes around @var{linker_name} if @var{linker_name} contains spaces
8932 or other separator characters. As an example @option{--GNATLINK="lan -x
8933 -y"} will instruct @command{gnatmake} to use @code{lan -x -y} as your
8934 linker. Linker switches that are normally appended by @command{gnatmake} to
8935 `@command{gnatlink}' are now appended to the end of @code{lan -x -y}.
8936 A limitation of this syntax is that the name and path name of the executable
8937 itself must not include any embedded spaces.
8938
8939 @end ifclear
8940
8941 @item ^-a^/ALL_FILES^
8942 @cindex @option{^-a^/ALL_FILES^} (@command{gnatmake})
8943 Consider all files in the make process, even the GNAT internal system
8944 files (for example, the predefined Ada library files), as well as any
8945 locked files. Locked files are files whose ALI file is write-protected.
8946 By default,
8947 @command{gnatmake} does not check these files,
8948 because the assumption is that the GNAT internal files are properly up
8949 to date, and also that any write protected ALI files have been properly
8950 installed. Note that if there is an installation problem, such that one
8951 of these files is not up to date, it will be properly caught by the
8952 binder.
8953 You may have to specify this switch if you are working on GNAT
8954 itself. The switch @option{^-a^/ALL_FILES^} is also useful
8955 in conjunction with @option{^-f^/FORCE_COMPILE^}
8956 if you need to recompile an entire application,
8957 including run-time files, using special configuration pragmas,
8958 such as a @code{Normalize_Scalars} pragma.
8959
8960 By default
8961 @code{gnatmake ^-a^/ALL_FILES^} compiles all GNAT
8962 internal files with
8963 @ifclear vms
8964 @code{gcc -c -gnatpg} rather than @code{gcc -c}.
8965 @end ifclear
8966 @ifset vms
8967 the @code{/CHECKS=SUPPRESS_ALL /STYLE_CHECKS=GNAT} switch.
8968 @end ifset
8969
8970 @item ^-b^/ACTIONS=BIND^
8971 @cindex @option{^-b^/ACTIONS=BIND^} (@command{gnatmake})
8972 Bind only. Can be combined with @option{^-c^/ACTIONS=COMPILE^} to do
8973 compilation and binding, but no link.
8974 Can be combined with @option{^-l^/ACTIONS=LINK^}
8975 to do binding and linking. When not combined with
8976 @option{^-c^/ACTIONS=COMPILE^}
8977 all the units in the closure of the main program must have been previously
8978 compiled and must be up to date. The root unit specified by @var{file_name}
8979 may be given without extension, with the source extension or, if no GNAT
8980 Project File is specified, with the ALI file extension.
8981
8982 @item ^-c^/ACTIONS=COMPILE^
8983 @cindex @option{^-c^/ACTIONS=COMPILE^} (@command{gnatmake})
8984 Compile only. Do not perform binding, except when @option{^-b^/ACTIONS=BIND^}
8985 is also specified. Do not perform linking, except if both
8986 @option{^-b^/ACTIONS=BIND^} and
8987 @option{^-l^/ACTIONS=LINK^} are also specified.
8988 If the root unit specified by @var{file_name} is not a main unit, this is the
8989 default. Otherwise @command{gnatmake} will attempt binding and linking
8990 unless all objects are up to date and the executable is more recent than
8991 the objects.
8992
8993 @item ^-C^/MAPPING^
8994 @cindex @option{^-C^/MAPPING^} (@command{gnatmake})
8995 Use a temporary mapping file. A mapping file is a way to communicate to the
8996 compiler two mappings: from unit names to file names (without any directory
8997 information) and from file names to path names (with full directory
8998 information). These mappings are used by the compiler to short-circuit the path
8999 search. When @command{gnatmake} is invoked with this switch, it will create
9000 a temporary mapping file, initially populated by the project manager,
9001 if @option{^-P^/PROJECT_FILE^} is used, otherwise initially empty.
9002 Each invocation of the compiler will add the newly accessed sources to the
9003 mapping file. This will improve the source search during the next invocation
9004 of the compiler.
9005
9006 @item ^-C=^/USE_MAPPING_FILE=^@var{file}
9007 @cindex @option{^-C=^/USE_MAPPING^} (@command{gnatmake})
9008 Use a specific mapping file. The file, specified as a path name (absolute or
9009 relative) by this switch, should already exist, otherwise the switch is
9010 ineffective. The specified mapping file will be communicated to the compiler.
9011 This switch is not compatible with a project file
9012 (^-P^/PROJECT_FILE=^@var{file}) or with multiple compiling processes
9013 (^-j^/PROCESSES=^nnn, when nnn is greater than 1).
9014
9015 @item ^-d^/DISPLAY_PROGRESS^
9016 @cindex @option{^-d^/DISPLAY_PROGRESS^} (@command{gnatmake})
9017 Display progress for each source, up to date or not, as a single line
9018
9019 @smallexample
9020 completed x out of y (zz%)
9021 @end smallexample
9022
9023 If the file needs to be compiled this is displayed after the invocation of
9024 the compiler. These lines are displayed even in quiet output mode.
9025
9026 @item ^-D ^/DIRECTORY_OBJECTS=^@var{dir}
9027 @cindex @option{^-D^/DIRECTORY_OBJECTS^} (@command{gnatmake})
9028 Put all object files and ALI file in directory @var{dir}.
9029 If the @option{^-D^/DIRECTORY_OBJECTS^} switch is not used, all object files
9030 and ALI files go in the current working directory.
9031
9032 This switch cannot be used when using a project file.
9033
9034 @ifclear vms
9035 @item -eL
9036 @cindex @option{-eL} (@command{gnatmake})
9037 Follow all symbolic links when processing project files.
9038 @end ifclear
9039
9040 @item ^-eS^/STANDARD_OUTPUT_FOR_COMMANDS^
9041 @cindex @option{^-eS^/STANDARD_OUTPUT_FOR_COMMANDS^} (@command{gnatmake})
9042 Output the commands for the compiler, the binder and the linker
9043 on ^standard output^SYS$OUTPUT^,
9044 instead of ^standard error^SYS$ERROR^.
9045
9046 @item ^-f^/FORCE_COMPILE^
9047 @cindex @option{^-f^/FORCE_COMPILE^} (@command{gnatmake})
9048 Force recompilations. Recompile all sources, even though some object
9049 files may be up to date, but don't recompile predefined or GNAT internal
9050 files or locked files (files with a write-protected ALI file),
9051 unless the @option{^-a^/ALL_FILES^} switch is also specified.
9052
9053 @item ^-F^/FULL_PATH_IN_BRIEF_MESSAGES^
9054 @cindex @option{^-F^/FULL_PATH_IN_BRIEF_MESSAGES^} (@command{gnatmake})
9055 When using project files, if some errors or warnings are detected during
9056 parsing and verbose mode is not in effect (no use of switch
9057 ^-v^/VERBOSE^), then error lines start with the full path name of the project
9058 file, rather than its simple file name.
9059
9060 @item ^-g^/DEBUG^
9061 @cindex @option{^-g^/DEBUG^} (@command{gnatmake})
9062 Enable debugging. This switch is simply passed to the compiler and to the
9063 linker.
9064
9065 @item ^-i^/IN_PLACE^
9066 @cindex @option{^-i^/IN_PLACE^} (@command{gnatmake})
9067 In normal mode, @command{gnatmake} compiles all object files and ALI files
9068 into the current directory. If the @option{^-i^/IN_PLACE^} switch is used,
9069 then instead object files and ALI files that already exist are overwritten
9070 in place. This means that once a large project is organized into separate
9071 directories in the desired manner, then @command{gnatmake} will automatically
9072 maintain and update this organization. If no ALI files are found on the
9073 Ada object path (@ref{Search Paths and the Run-Time Library (RTL)}),
9074 the new object and ALI files are created in the
9075 directory containing the source being compiled. If another organization
9076 is desired, where objects and sources are kept in different directories,
9077 a useful technique is to create dummy ALI files in the desired directories.
9078 When detecting such a dummy file, @command{gnatmake} will be forced to
9079 recompile the corresponding source file, and it will be put the resulting
9080 object and ALI files in the directory where it found the dummy file.
9081
9082 @item ^-j^/PROCESSES=^@var{n}
9083 @cindex @option{^-j^/PROCESSES^} (@command{gnatmake})
9084 @cindex Parallel make
9085 Use @var{n} processes to carry out the (re)compilations. On a
9086 multiprocessor machine compilations will occur in parallel. In the
9087 event of compilation errors, messages from various compilations might
9088 get interspersed (but @command{gnatmake} will give you the full ordered
9089 list of failing compiles at the end). If this is problematic, rerun
9090 the make process with n set to 1 to get a clean list of messages.
9091
9092 @item ^-k^/CONTINUE_ON_ERROR^
9093 @cindex @option{^-k^/CONTINUE_ON_ERROR^} (@command{gnatmake})
9094 Keep going. Continue as much as possible after a compilation error. To
9095 ease the programmer's task in case of compilation errors, the list of
9096 sources for which the compile fails is given when @command{gnatmake}
9097 terminates.
9098
9099 If @command{gnatmake} is invoked with several @file{file_names} and with this
9100 switch, if there are compilation errors when building an executable,
9101 @command{gnatmake} will not attempt to build the following executables.
9102
9103 @item ^-l^/ACTIONS=LINK^
9104 @cindex @option{^-l^/ACTIONS=LINK^} (@command{gnatmake})
9105 Link only. Can be combined with @option{^-b^/ACTIONS=BIND^} to binding
9106 and linking. Linking will not be performed if combined with
9107 @option{^-c^/ACTIONS=COMPILE^}
9108 but not with @option{^-b^/ACTIONS=BIND^}.
9109 When not combined with @option{^-b^/ACTIONS=BIND^}
9110 all the units in the closure of the main program must have been previously
9111 compiled and must be up to date, and the main program needs to have been bound.
9112 The root unit specified by @var{file_name}
9113 may be given without extension, with the source extension or, if no GNAT
9114 Project File is specified, with the ALI file extension.
9115
9116 @item ^-m^/MINIMAL_RECOMPILATION^
9117 @cindex @option{^-m^/MINIMAL_RECOMPILATION^} (@command{gnatmake})
9118 Specify that the minimum necessary amount of recompilations
9119 be performed. In this mode @command{gnatmake} ignores time
9120 stamp differences when the only
9121 modifications to a source file consist in adding/removing comments,
9122 empty lines, spaces or tabs. This means that if you have changed the
9123 comments in a source file or have simply reformatted it, using this
9124 switch will tell @command{gnatmake} not to recompile files that depend on it
9125 (provided other sources on which these files depend have undergone no
9126 semantic modifications). Note that the debugging information may be
9127 out of date with respect to the sources if the @option{-m} switch causes
9128 a compilation to be switched, so the use of this switch represents a
9129 trade-off between compilation time and accurate debugging information.
9130
9131 @item ^-M^/DEPENDENCIES_LIST^
9132 @cindex Dependencies, producing list
9133 @cindex @option{^-M^/DEPENDENCIES_LIST^} (@command{gnatmake})
9134 Check if all objects are up to date. If they are, output the object
9135 dependences to @file{stdout} in a form that can be directly exploited in
9136 a @file{Makefile}. By default, each source file is prefixed with its
9137 (relative or absolute) directory name. This name is whatever you
9138 specified in the various @option{^-aI^/SOURCE_SEARCH^}
9139 and @option{^-I^/SEARCH^} switches. If you use
9140 @code{gnatmake ^-M^/DEPENDENCIES_LIST^}
9141 @option{^-q^/QUIET^}
9142 (see below), only the source file names,
9143 without relative paths, are output. If you just specify the
9144 @option{^-M^/DEPENDENCIES_LIST^}
9145 switch, dependencies of the GNAT internal system files are omitted. This
9146 is typically what you want. If you also specify
9147 the @option{^-a^/ALL_FILES^} switch,
9148 dependencies of the GNAT internal files are also listed. Note that
9149 dependencies of the objects in external Ada libraries (see switch
9150 @option{^-aL^/SKIP_MISSING=^}@var{dir} in the following list)
9151 are never reported.
9152
9153 @item ^-n^/DO_OBJECT_CHECK^
9154 @cindex @option{^-n^/DO_OBJECT_CHECK^} (@command{gnatmake})
9155 Don't compile, bind, or link. Checks if all objects are up to date.
9156 If they are not, the full name of the first file that needs to be
9157 recompiled is printed.
9158 Repeated use of this option, followed by compiling the indicated source
9159 file, will eventually result in recompiling all required units.
9160
9161 @item ^-o ^/EXECUTABLE=^@var{exec_name}
9162 @cindex @option{^-o^/EXECUTABLE^} (@command{gnatmake})
9163 Output executable name. The name of the final executable program will be
9164 @var{exec_name}. If the @option{^-o^/EXECUTABLE^} switch is omitted the default
9165 name for the executable will be the name of the input file in appropriate form
9166 for an executable file on the host system.
9167
9168 This switch cannot be used when invoking @command{gnatmake} with several
9169 @file{file_names}.
9170
9171 @item ^-p or --create-missing-dirs^/CREATE_MISSING_DIRS^
9172 @cindex @option{^-p^/CREATE_MISSING_DIRS^} (@command{gnatmake})
9173 When using project files (^-P^/PROJECT_FILE=^@var{project}), create
9174 automatically missing object directories, library directories and exec
9175 directories.
9176
9177 @item ^-P^/PROJECT_FILE=^@var{project}
9178 @cindex @option{^-P^/PROJECT_FILE^} (@command{gnatmake})
9179 Use project file @var{project}. Only one such switch can be used.
9180 @xref{gnatmake and Project Files}.
9181
9182 @item ^-q^/QUIET^
9183 @cindex @option{^-q^/QUIET^} (@command{gnatmake})
9184 Quiet. When this flag is not set, the commands carried out by
9185 @command{gnatmake} are displayed.
9186
9187 @item ^-s^/SWITCH_CHECK/^
9188 @cindex @option{^-s^/SWITCH_CHECK^} (@command{gnatmake})
9189 Recompile if compiler switches have changed since last compilation.
9190 All compiler switches but -I and -o are taken into account in the
9191 following way:
9192 orders between different ``first letter'' switches are ignored, but
9193 orders between same switches are taken into account. For example,
9194 @option{-O -O2} is different than @option{-O2 -O}, but @option{-g -O}
9195 is equivalent to @option{-O -g}.
9196
9197 This switch is recommended when Integrated Preprocessing is used.
9198
9199 @item ^-u^/UNIQUE^
9200 @cindex @option{^-u^/UNIQUE^} (@command{gnatmake})
9201 Unique. Recompile at most the main files. It implies -c. Combined with
9202 -f, it is equivalent to calling the compiler directly. Note that using
9203 ^-u^/UNIQUE^ with a project file and no main has a special meaning
9204 (@pxref{Project Files and Main Subprograms}).
9205
9206 @item ^-U^/ALL_PROJECTS^
9207 @cindex @option{^-U^/ALL_PROJECTS^} (@command{gnatmake})
9208 When used without a project file or with one or several mains on the command
9209 line, is equivalent to ^-u^/UNIQUE^. When used with a project file and no main
9210 on the command line, all sources of all project files are checked and compiled
9211 if not up to date, and libraries are rebuilt, if necessary.
9212
9213 @item ^-v^/REASONS^
9214 @cindex @option{^-v^/REASONS^} (@command{gnatmake})
9215 Verbose. Display the reason for all recompilations @command{gnatmake}
9216 decides are necessary, with the highest verbosity level.
9217
9218 @item ^-vl^/LOW_VERBOSITY^
9219 @cindex @option{^-vl^/LOW_VERBOSITY^} (@command{gnatmake})
9220 Verbosity level Low. Display fewer lines than in verbosity Medium.
9221
9222 @item ^-vm^/MEDIUM_VERBOSITY^
9223 @cindex @option{^-vm^/MEDIUM_VERBOSITY^} (@command{gnatmake})
9224 Verbosity level Medium. Potentially display fewer lines than in verbosity High.
9225
9226 @item ^-vh^/HIGH_VERBOSITY^
9227 @cindex @option{^-vm^/HIGH_VERBOSITY^} (@command{gnatmake})
9228 Verbosity level High. Equivalent to ^-v^/REASONS^.
9229
9230 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
9231 Indicate the verbosity of the parsing of GNAT project files.
9232 @xref{Switches Related to Project Files}.
9233
9234 @item ^-x^/NON_PROJECT_UNIT_COMPILATION^
9235 @cindex @option{^-x^/NON_PROJECT_UNIT_COMPILATION^} (@command{gnatmake})
9236 Indicate that sources that are not part of any Project File may be compiled.
9237 Normally, when using Project Files, only sources that are part of a Project
9238 File may be compile. When this switch is used, a source outside of all Project
9239 Files may be compiled. The ALI file and the object file will be put in the
9240 object directory of the main Project. The compilation switches used will only
9241 be those specified on the command line. Even when
9242 @option{^-x^/NON_PROJECT_UNIT_COMPILATION^} is used, mains specified on the
9243 command line need to be sources of a project file.
9244
9245 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
9246 Indicate that external variable @var{name} has the value @var{value}.
9247 The Project Manager will use this value for occurrences of
9248 @code{external(name)} when parsing the project file.
9249 @xref{Switches Related to Project Files}.
9250
9251 @item ^-z^/NOMAIN^
9252 @cindex @option{^-z^/NOMAIN^} (@command{gnatmake})
9253 No main subprogram. Bind and link the program even if the unit name
9254 given on the command line is a package name. The resulting executable
9255 will execute the elaboration routines of the package and its closure,
9256 then the finalization routines.
9257
9258 @end table
9259
9260 @table @asis
9261 @item @command{gcc} @asis{switches}
9262 @ifclear vms
9263 Any uppercase or multi-character switch that is not a @command{gnatmake} switch
9264 is passed to @command{gcc} (e.g.@: @option{-O}, @option{-gnato,} etc.)
9265 @end ifclear
9266 @ifset vms
9267 Any qualifier that cannot be recognized as a qualifier for @code{GNAT MAKE}
9268 but is recognizable as a valid qualifier for @code{GNAT COMPILE} is
9269 automatically treated as a compiler switch, and passed on to all
9270 compilations that are carried out.
9271 @end ifset
9272 @end table
9273
9274 @noindent
9275 Source and library search path switches:
9276
9277 @table @option
9278 @c !sort!
9279 @item ^-aI^/SOURCE_SEARCH=^@var{dir}
9280 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatmake})
9281 When looking for source files also look in directory @var{dir}.
9282 The order in which source files search is undertaken is
9283 described in @ref{Search Paths and the Run-Time Library (RTL)}.
9284
9285 @item ^-aL^/SKIP_MISSING=^@var{dir}
9286 @cindex @option{^-aL^/SKIP_MISSING^} (@command{gnatmake})
9287 Consider @var{dir} as being an externally provided Ada library.
9288 Instructs @command{gnatmake} to skip compilation units whose @file{.ALI}
9289 files have been located in directory @var{dir}. This allows you to have
9290 missing bodies for the units in @var{dir} and to ignore out of date bodies
9291 for the same units. You still need to specify
9292 the location of the specs for these units by using the switches
9293 @option{^-aI^/SOURCE_SEARCH=^@var{dir}}
9294 or @option{^-I^/SEARCH=^@var{dir}}.
9295 Note: this switch is provided for compatibility with previous versions
9296 of @command{gnatmake}. The easier method of causing standard libraries
9297 to be excluded from consideration is to write-protect the corresponding
9298 ALI files.
9299
9300 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
9301 @cindex @option{^-aO^/OBJECT_SEARCH^} (@command{gnatmake})
9302 When searching for library and object files, look in directory
9303 @var{dir}. The order in which library files are searched is described in
9304 @ref{Search Paths for gnatbind}.
9305
9306 @item ^-A^/CONDITIONAL_SOURCE_SEARCH=^@var{dir}
9307 @cindex Search paths, for @command{gnatmake}
9308 @cindex @option{^-A^/CONDITIONAL_SOURCE_SEARCH^} (@command{gnatmake})
9309 Equivalent to @option{^-aL^/SKIP_MISSING=^@var{dir}
9310 ^-aI^/SOURCE_SEARCH=^@var{dir}}.
9311
9312 @item ^-I^/SEARCH=^@var{dir}
9313 @cindex @option{^-I^/SEARCH^} (@command{gnatmake})
9314 Equivalent to @option{^-aO^/OBJECT_SEARCH=^@var{dir}
9315 ^-aI^/SOURCE_SEARCH=^@var{dir}}.
9316
9317 @item ^-I-^/NOCURRENT_DIRECTORY^
9318 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gnatmake})
9319 @cindex Source files, suppressing search
9320 Do not look for source files in the directory containing the source
9321 file named in the command line.
9322 Do not look for ALI or object files in the directory
9323 where @command{gnatmake} was invoked.
9324
9325 @item ^-L^/LIBRARY_SEARCH=^@var{dir}
9326 @cindex @option{^-L^/LIBRARY_SEARCH^} (@command{gnatmake})
9327 @cindex Linker libraries
9328 Add directory @var{dir} to the list of directories in which the linker
9329 will search for libraries. This is equivalent to
9330 @option{-largs ^-L^/LIBRARY_SEARCH=^}@var{dir}.
9331 @ifclear vms
9332 Furthermore, under Windows, the sources pointed to by the libraries path
9333 set in the registry are not searched for.
9334 @end ifclear
9335
9336 @item -nostdinc
9337 @cindex @option{-nostdinc} (@command{gnatmake})
9338 Do not look for source files in the system default directory.
9339
9340 @item -nostdlib
9341 @cindex @option{-nostdlib} (@command{gnatmake})
9342 Do not look for library files in the system default directory.
9343
9344 @item --RTS=@var{rts-path}
9345 @cindex @option{--RTS} (@command{gnatmake})
9346 Specifies the default location of the runtime library. GNAT looks for the
9347 runtime
9348 in the following directories, and stops as soon as a valid runtime is found
9349 (@file{adainclude} or @file{ada_source_path}, and @file{adalib} or
9350 @file{ada_object_path} present):
9351
9352 @itemize @bullet
9353 @item <current directory>/$rts_path
9354
9355 @item <default-search-dir>/$rts_path
9356
9357 @item <default-search-dir>/rts-$rts_path
9358 @end itemize
9359
9360 @noindent
9361 The selected path is handled like a normal RTS path.
9362
9363 @end table
9364
9365 @node Mode Switches for gnatmake
9366 @section Mode Switches for @command{gnatmake}
9367
9368 @noindent
9369 The mode switches (referred to as @code{mode_switches}) allow the
9370 inclusion of switches that are to be passed to the compiler itself, the
9371 binder or the linker. The effect of a mode switch is to cause all
9372 subsequent switches up to the end of the switch list, or up to the next
9373 mode switch, to be interpreted as switches to be passed on to the
9374 designated component of GNAT.
9375
9376 @table @option
9377 @c !sort!
9378 @item -cargs @var{switches}
9379 @cindex @option{-cargs} (@command{gnatmake})
9380 Compiler switches. Here @var{switches} is a list of switches
9381 that are valid switches for @command{gcc}. They will be passed on to
9382 all compile steps performed by @command{gnatmake}.
9383
9384 @item -bargs @var{switches}
9385 @cindex @option{-bargs} (@command{gnatmake})
9386 Binder switches. Here @var{switches} is a list of switches
9387 that are valid switches for @code{gnatbind}. They will be passed on to
9388 all bind steps performed by @command{gnatmake}.
9389
9390 @item -largs @var{switches}
9391 @cindex @option{-largs} (@command{gnatmake})
9392 Linker switches. Here @var{switches} is a list of switches
9393 that are valid switches for @command{gnatlink}. They will be passed on to
9394 all link steps performed by @command{gnatmake}.
9395
9396 @item -margs @var{switches}
9397 @cindex @option{-margs} (@command{gnatmake})
9398 Make switches. The switches are directly interpreted by @command{gnatmake},
9399 regardless of any previous occurrence of @option{-cargs}, @option{-bargs}
9400 or @option{-largs}.
9401 @end table
9402
9403 @node Notes on the Command Line
9404 @section Notes on the Command Line
9405
9406 @noindent
9407 This section contains some additional useful notes on the operation
9408 of the @command{gnatmake} command.
9409
9410 @itemize @bullet
9411 @item
9412 @cindex Recompilation, by @command{gnatmake}
9413 If @command{gnatmake} finds no ALI files, it recompiles the main program
9414 and all other units required by the main program.
9415 This means that @command{gnatmake}
9416 can be used for the initial compile, as well as during subsequent steps of
9417 the development cycle.
9418
9419 @item
9420 If you enter @code{gnatmake @var{file}.adb}, where @file{@var{file}.adb}
9421 is a subunit or body of a generic unit, @command{gnatmake} recompiles
9422 @file{@var{file}.adb} (because it finds no ALI) and stops, issuing a
9423 warning.
9424
9425 @item
9426 In @command{gnatmake} the switch @option{^-I^/SEARCH^}
9427 is used to specify both source and
9428 library file paths. Use @option{^-aI^/SOURCE_SEARCH^}
9429 instead if you just want to specify
9430 source paths only and @option{^-aO^/OBJECT_SEARCH^}
9431 if you want to specify library paths
9432 only.
9433
9434 @item
9435 @command{gnatmake} will ignore any files whose ALI file is write-protected.
9436 This may conveniently be used to exclude standard libraries from
9437 consideration and in particular it means that the use of the
9438 @option{^-f^/FORCE_COMPILE^} switch will not recompile these files
9439 unless @option{^-a^/ALL_FILES^} is also specified.
9440
9441 @item
9442 @command{gnatmake} has been designed to make the use of Ada libraries
9443 particularly convenient. Assume you have an Ada library organized
9444 as follows: @i{^obj-dir^[OBJ_DIR]^} contains the objects and ALI files for
9445 of your Ada compilation units,
9446 whereas @i{^include-dir^[INCLUDE_DIR]^} contains the
9447 specs of these units, but no bodies. Then to compile a unit
9448 stored in @code{main.adb}, which uses this Ada library you would just type
9449
9450 @smallexample
9451 @ifclear vms
9452 $ gnatmake -aI@var{include-dir} -aL@var{obj-dir} main
9453 @end ifclear
9454 @ifset vms
9455 $ gnatmake /SOURCE_SEARCH=@i{[INCLUDE_DIR]}
9456 /SKIP_MISSING=@i{[OBJ_DIR]} main
9457 @end ifset
9458 @end smallexample
9459
9460 @item
9461 Using @command{gnatmake} along with the
9462 @option{^-m (minimal recompilation)^/MINIMAL_RECOMPILATION^}
9463 switch provides a mechanism for avoiding unnecessary recompilations. Using
9464 this switch,
9465 you can update the comments/format of your
9466 source files without having to recompile everything. Note, however, that
9467 adding or deleting lines in a source files may render its debugging
9468 info obsolete. If the file in question is a spec, the impact is rather
9469 limited, as that debugging info will only be useful during the
9470 elaboration phase of your program. For bodies the impact can be more
9471 significant. In all events, your debugger will warn you if a source file
9472 is more recent than the corresponding object, and alert you to the fact
9473 that the debugging information may be out of date.
9474 @end itemize
9475
9476 @node How gnatmake Works
9477 @section How @command{gnatmake} Works
9478
9479 @noindent
9480 Generally @command{gnatmake} automatically performs all necessary
9481 recompilations and you don't need to worry about how it works. However,
9482 it may be useful to have some basic understanding of the @command{gnatmake}
9483 approach and in particular to understand how it uses the results of
9484 previous compilations without incorrectly depending on them.
9485
9486 First a definition: an object file is considered @dfn{up to date} if the
9487 corresponding ALI file exists and if all the source files listed in the
9488 dependency section of this ALI file have time stamps matching those in
9489 the ALI file. This means that neither the source file itself nor any
9490 files that it depends on have been modified, and hence there is no need
9491 to recompile this file.
9492
9493 @command{gnatmake} works by first checking if the specified main unit is up
9494 to date. If so, no compilations are required for the main unit. If not,
9495 @command{gnatmake} compiles the main program to build a new ALI file that
9496 reflects the latest sources. Then the ALI file of the main unit is
9497 examined to find all the source files on which the main program depends,
9498 and @command{gnatmake} recursively applies the above procedure on all these
9499 files.
9500
9501 This process ensures that @command{gnatmake} only trusts the dependencies
9502 in an existing ALI file if they are known to be correct. Otherwise it
9503 always recompiles to determine a new, guaranteed accurate set of
9504 dependencies. As a result the program is compiled ``upside down'' from what may
9505 be more familiar as the required order of compilation in some other Ada
9506 systems. In particular, clients are compiled before the units on which
9507 they depend. The ability of GNAT to compile in any order is critical in
9508 allowing an order of compilation to be chosen that guarantees that
9509 @command{gnatmake} will recompute a correct set of new dependencies if
9510 necessary.
9511
9512 When invoking @command{gnatmake} with several @var{file_names}, if a unit is
9513 imported by several of the executables, it will be recompiled at most once.
9514
9515 Note: when using non-standard naming conventions
9516 (@pxref{Using Other File Names}), changing through a configuration pragmas
9517 file the version of a source and invoking @command{gnatmake} to recompile may
9518 have no effect, if the previous version of the source is still accessible
9519 by @command{gnatmake}. It may be necessary to use the switch
9520 ^-f^/FORCE_COMPILE^.
9521
9522 @node Examples of gnatmake Usage
9523 @section Examples of @command{gnatmake} Usage
9524
9525 @table @code
9526 @item gnatmake hello.adb
9527 Compile all files necessary to bind and link the main program
9528 @file{hello.adb} (containing unit @code{Hello}) and bind and link the
9529 resulting object files to generate an executable file @file{^hello^HELLO.EXE^}.
9530
9531 @item gnatmake main1 main2 main3
9532 Compile all files necessary to bind and link the main programs
9533 @file{main1.adb} (containing unit @code{Main1}), @file{main2.adb}
9534 (containing unit @code{Main2}) and @file{main3.adb}
9535 (containing unit @code{Main3}) and bind and link the resulting object files
9536 to generate three executable files @file{^main1^MAIN1.EXE^},
9537 @file{^main2^MAIN2.EXE^}
9538 and @file{^main3^MAIN3.EXE^}.
9539
9540 @ifclear vms
9541 @item gnatmake -q Main_Unit -cargs -O2 -bargs -l
9542 @end ifclear
9543
9544 @ifset vms
9545 @item gnatmake Main_Unit /QUIET
9546 /COMPILER_QUALIFIERS /OPTIMIZE=ALL
9547 /BINDER_QUALIFIERS /ORDER_OF_ELABORATION
9548 @end ifset
9549 Compile all files necessary to bind and link the main program unit
9550 @code{Main_Unit} (from file @file{main_unit.adb}). All compilations will
9551 be done with optimization level 2 and the order of elaboration will be
9552 listed by the binder. @command{gnatmake} will operate in quiet mode, not
9553 displaying commands it is executing.
9554 @end table
9555
9556 @c *************************
9557 @node Improving Performance
9558 @chapter Improving Performance
9559 @cindex Improving performance
9560
9561 @noindent
9562 This chapter presents several topics related to program performance.
9563 It first describes some of the tradeoffs that need to be considered
9564 and some of the techniques for making your program run faster.
9565 It then documents the @command{gnatelim} tool and unused subprogram/data
9566 elimination feature, which can reduce the size of program executables.
9567
9568 Note: to invoke @command{gnatelim} with a project file, use the @code{gnat}
9569 driver (see @ref{The GNAT Driver and Project Files}).
9570
9571 @ifnottex
9572 @menu
9573 * Performance Considerations::
9574 * Text_IO Suggestions::
9575 * Reducing Size of Ada Executables with gnatelim::
9576 * Reducing Size of Executables with unused subprogram/data elimination::
9577 @end menu
9578 @end ifnottex
9579
9580 @c *****************************
9581 @node Performance Considerations
9582 @section Performance Considerations
9583
9584 @noindent
9585 The GNAT system provides a number of options that allow a trade-off
9586 between
9587
9588 @itemize @bullet
9589 @item
9590 performance of the generated code
9591
9592 @item
9593 speed of compilation
9594
9595 @item
9596 minimization of dependences and recompilation
9597
9598 @item
9599 the degree of run-time checking.
9600 @end itemize
9601
9602 @noindent
9603 The defaults (if no options are selected) aim at improving the speed
9604 of compilation and minimizing dependences, at the expense of performance
9605 of the generated code:
9606
9607 @itemize @bullet
9608 @item
9609 no optimization
9610
9611 @item
9612 no inlining of subprogram calls
9613
9614 @item
9615 all run-time checks enabled except overflow and elaboration checks
9616 @end itemize
9617
9618 @noindent
9619 These options are suitable for most program development purposes. This
9620 chapter describes how you can modify these choices, and also provides
9621 some guidelines on debugging optimized code.
9622
9623 @menu
9624 * Controlling Run-Time Checks::
9625 * Use of Restrictions::
9626 * Optimization Levels::
9627 * Debugging Optimized Code::
9628 * Inlining of Subprograms::
9629 * Other Optimization Switches::
9630 * Optimization and Strict Aliasing::
9631
9632 @ifset vms
9633 * Coverage Analysis::
9634 @end ifset
9635 @end menu
9636
9637 @node Controlling Run-Time Checks
9638 @subsection Controlling Run-Time Checks
9639
9640 @noindent
9641 By default, GNAT generates all run-time checks, except integer overflow
9642 checks, stack overflow checks, and checks for access before elaboration on
9643 subprogram calls. The latter are not required in default mode, because all
9644 necessary checking is done at compile time.
9645 @cindex @option{-gnatp} (@command{gcc})
9646 @cindex @option{-gnato} (@command{gcc})
9647 Two gnat switches, @option{-gnatp} and @option{-gnato} allow this default to
9648 be modified. @xref{Run-Time Checks}.
9649
9650 Our experience is that the default is suitable for most development
9651 purposes.
9652
9653 We treat integer overflow specially because these
9654 are quite expensive and in our experience are not as important as other
9655 run-time checks in the development process. Note that division by zero
9656 is not considered an overflow check, and divide by zero checks are
9657 generated where required by default.
9658
9659 Elaboration checks are off by default, and also not needed by default, since
9660 GNAT uses a static elaboration analysis approach that avoids the need for
9661 run-time checking. This manual contains a full chapter discussing the issue
9662 of elaboration checks, and if the default is not satisfactory for your use,
9663 you should read this chapter.
9664
9665 For validity checks, the minimal checks required by the Ada Reference
9666 Manual (for case statements and assignments to array elements) are on
9667 by default. These can be suppressed by use of the @option{-gnatVn} switch.
9668 Note that in Ada 83, there were no validity checks, so if the Ada 83 mode
9669 is acceptable (or when comparing GNAT performance with an Ada 83 compiler),
9670 it may be reasonable to routinely use @option{-gnatVn}. Validity checks
9671 are also suppressed entirely if @option{-gnatp} is used.
9672
9673 @cindex Overflow checks
9674 @cindex Checks, overflow
9675 @findex Suppress
9676 @findex Unsuppress
9677 @cindex pragma Suppress
9678 @cindex pragma Unsuppress
9679 Note that the setting of the switches controls the default setting of
9680 the checks. They may be modified using either @code{pragma Suppress} (to
9681 remove checks) or @code{pragma Unsuppress} (to add back suppressed
9682 checks) in the program source.
9683
9684 @node Use of Restrictions
9685 @subsection Use of Restrictions
9686
9687 @noindent
9688 The use of pragma Restrictions allows you to control which features are
9689 permitted in your program. Apart from the obvious point that if you avoid
9690 relatively expensive features like finalization (enforceable by the use
9691 of pragma Restrictions (No_Finalization), the use of this pragma does not
9692 affect the generated code in most cases.
9693
9694 One notable exception to this rule is that the possibility of task abort
9695 results in some distributed overhead, particularly if finalization or
9696 exception handlers are used. The reason is that certain sections of code
9697 have to be marked as non-abortable.
9698
9699 If you use neither the @code{abort} statement, nor asynchronous transfer
9700 of control (@code{select @dots{} then abort}), then this distributed overhead
9701 is removed, which may have a general positive effect in improving
9702 overall performance. Especially code involving frequent use of tasking
9703 constructs and controlled types will show much improved performance.
9704 The relevant restrictions pragmas are
9705
9706 @smallexample @c ada
9707 pragma Restrictions (No_Abort_Statements);
9708 pragma Restrictions (Max_Asynchronous_Select_Nesting => 0);
9709 @end smallexample
9710
9711 @noindent
9712 It is recommended that these restriction pragmas be used if possible. Note
9713 that this also means that you can write code without worrying about the
9714 possibility of an immediate abort at any point.
9715
9716 @node Optimization Levels
9717 @subsection Optimization Levels
9718 @cindex @option{^-O^/OPTIMIZE^} (@command{gcc})
9719
9720 @noindent
9721 Without any optimization ^option,^qualifier,^
9722 the compiler's goal is to reduce the cost of
9723 compilation and to make debugging produce the expected results.
9724 Statements are independent: if you stop the program with a breakpoint between
9725 statements, you can then assign a new value to any variable or change
9726 the program counter to any other statement in the subprogram and get exactly
9727 the results you would expect from the source code.
9728
9729 Turning on optimization makes the compiler attempt to improve the
9730 performance and/or code size at the expense of compilation time and
9731 possibly the ability to debug the program.
9732
9733 If you use multiple
9734 ^-O options, with or without level numbers,^/OPTIMIZE qualifiers,^
9735 the last such option is the one that is effective.
9736
9737 @noindent
9738 The default is optimization off. This results in the fastest compile
9739 times, but GNAT makes absolutely no attempt to optimize, and the
9740 generated programs are considerably larger and slower than when
9741 optimization is enabled. You can use the
9742 @ifclear vms
9743 @option{-O} switch (the permitted forms are @option{-O0}, @option{-O1}
9744 @option{-O2}, @option{-O3}, and @option{-Os})
9745 @end ifclear
9746 @ifset vms
9747 @code{OPTIMIZE} qualifier
9748 @end ifset
9749 to @command{gcc} to control the optimization level:
9750
9751 @table @option
9752 @item ^-O0^/OPTIMIZE=NONE^
9753 No optimization (the default);
9754 generates unoptimized code but has
9755 the fastest compilation time.
9756
9757 Note that many other compilers do fairly extensive optimization
9758 even if ``no optimization'' is specified. With gcc, it is
9759 very unusual to use ^-O0^/OPTIMIZE=NONE^ for production if
9760 execution time is of any concern, since ^-O0^/OPTIMIZE=NONE^
9761 really does mean no optimization at all. This difference between
9762 gcc and other compilers should be kept in mind when doing
9763 performance comparisons.
9764
9765 @item ^-O1^/OPTIMIZE=SOME^
9766 Moderate optimization;
9767 optimizes reasonably well but does not
9768 degrade compilation time significantly.
9769
9770 @item ^-O2^/OPTIMIZE=ALL^
9771 @ifset vms
9772 @itemx /OPTIMIZE=DEVELOPMENT
9773 @end ifset
9774 Full optimization;
9775 generates highly optimized code and has
9776 the slowest compilation time.
9777
9778 @item ^-O3^/OPTIMIZE=INLINING^
9779 Full optimization as in @option{-O2},
9780 and also attempts automatic inlining of small
9781 subprograms within a unit (@pxref{Inlining of Subprograms}).
9782
9783 @item ^-Os^/OPTIMIZE=SPACE^
9784 Optimize space usage of resulting program.
9785 @end table
9786
9787 @noindent
9788 Higher optimization levels perform more global transformations on the
9789 program and apply more expensive analysis algorithms in order to generate
9790 faster and more compact code. The price in compilation time, and the
9791 resulting improvement in execution time,
9792 both depend on the particular application and the hardware environment.
9793 You should experiment to find the best level for your application.
9794
9795 Since the precise set of optimizations done at each level will vary from
9796 release to release (and sometime from target to target), it is best to think
9797 of the optimization settings in general terms.
9798 @xref{Optimize Options,, Options That Control Optimization, gcc, Using
9799 the GNU Compiler Collection (GCC)}, for details about
9800 ^the @option{-O} settings and a number of @option{-f} options that^how to^
9801 individually enable or disable specific optimizations.
9802
9803 Unlike some other compilation systems, ^@command{gcc}^GNAT^ has
9804 been tested extensively at all optimization levels. There are some bugs
9805 which appear only with optimization turned on, but there have also been
9806 bugs which show up only in @emph{unoptimized} code. Selecting a lower
9807 level of optimization does not improve the reliability of the code
9808 generator, which in practice is highly reliable at all optimization
9809 levels.
9810
9811 Note regarding the use of @option{-O3}: The use of this optimization level
9812 is generally discouraged with GNAT, since it often results in larger
9813 executables which run more slowly. See further discussion of this point
9814 in @ref{Inlining of Subprograms}.
9815
9816 @node Debugging Optimized Code
9817 @subsection Debugging Optimized Code
9818 @cindex Debugging optimized code
9819 @cindex Optimization and debugging
9820
9821 @noindent
9822 Although it is possible to do a reasonable amount of debugging at
9823 @ifclear vms
9824 nonzero optimization levels,
9825 the higher the level the more likely that
9826 @end ifclear
9827 @ifset vms
9828 @option{/OPTIMIZE} settings other than @code{NONE},
9829 such settings will make it more likely that
9830 @end ifset
9831 source-level constructs will have been eliminated by optimization.
9832 For example, if a loop is strength-reduced, the loop
9833 control variable may be completely eliminated and thus cannot be
9834 displayed in the debugger.
9835 This can only happen at @option{-O2} or @option{-O3}.
9836 Explicit temporary variables that you code might be eliminated at
9837 ^level^setting^ @option{-O1} or higher.
9838
9839 The use of the @option{^-g^/DEBUG^} switch,
9840 @cindex @option{^-g^/DEBUG^} (@command{gcc})
9841 which is needed for source-level debugging,
9842 affects the size of the program executable on disk,
9843 and indeed the debugging information can be quite large.
9844 However, it has no effect on the generated code (and thus does not
9845 degrade performance)
9846
9847 Since the compiler generates debugging tables for a compilation unit before
9848 it performs optimizations, the optimizing transformations may invalidate some
9849 of the debugging data. You therefore need to anticipate certain
9850 anomalous situations that may arise while debugging optimized code.
9851 These are the most common cases:
9852
9853 @enumerate
9854 @item
9855 @i{The ``hopping Program Counter'':} Repeated @code{step} or @code{next}
9856 commands show
9857 the PC bouncing back and forth in the code. This may result from any of
9858 the following optimizations:
9859
9860 @itemize @bullet
9861 @item
9862 @i{Common subexpression elimination:} using a single instance of code for a
9863 quantity that the source computes several times. As a result you
9864 may not be able to stop on what looks like a statement.
9865
9866 @item
9867 @i{Invariant code motion:} moving an expression that does not change within a
9868 loop, to the beginning of the loop.
9869
9870 @item
9871 @i{Instruction scheduling:} moving instructions so as to
9872 overlap loads and stores (typically) with other code, or in
9873 general to move computations of values closer to their uses. Often
9874 this causes you to pass an assignment statement without the assignment
9875 happening and then later bounce back to the statement when the
9876 value is actually needed. Placing a breakpoint on a line of code
9877 and then stepping over it may, therefore, not always cause all the
9878 expected side-effects.
9879 @end itemize
9880
9881 @item
9882 @i{The ``big leap'':} More commonly known as @emph{cross-jumping}, in which
9883 two identical pieces of code are merged and the program counter suddenly
9884 jumps to a statement that is not supposed to be executed, simply because
9885 it (and the code following) translates to the same thing as the code
9886 that @emph{was} supposed to be executed. This effect is typically seen in
9887 sequences that end in a jump, such as a @code{goto}, a @code{return}, or
9888 a @code{break} in a C @code{^switch^switch^} statement.
9889
9890 @item
9891 @i{The ``roving variable'':} The symptom is an unexpected value in a variable.
9892 There are various reasons for this effect:
9893
9894 @itemize @bullet
9895 @item
9896 In a subprogram prologue, a parameter may not yet have been moved to its
9897 ``home''.
9898
9899 @item
9900 A variable may be dead, and its register re-used. This is
9901 probably the most common cause.
9902
9903 @item
9904 As mentioned above, the assignment of a value to a variable may
9905 have been moved.
9906
9907 @item
9908 A variable may be eliminated entirely by value propagation or
9909 other means. In this case, GCC may incorrectly generate debugging
9910 information for the variable
9911 @end itemize
9912
9913 @noindent
9914 In general, when an unexpected value appears for a local variable or parameter
9915 you should first ascertain if that value was actually computed by
9916 your program, as opposed to being incorrectly reported by the debugger.
9917 Record fields or
9918 array elements in an object designated by an access value
9919 are generally less of a problem, once you have ascertained that the access
9920 value is sensible.
9921 Typically, this means checking variables in the preceding code and in the
9922 calling subprogram to verify that the value observed is explainable from other
9923 values (one must apply the procedure recursively to those
9924 other values); or re-running the code and stopping a little earlier
9925 (perhaps before the call) and stepping to better see how the variable obtained
9926 the value in question; or continuing to step @emph{from} the point of the
9927 strange value to see if code motion had simply moved the variable's
9928 assignments later.
9929 @end enumerate
9930
9931 @noindent
9932 In light of such anomalies, a recommended technique is to use @option{-O0}
9933 early in the software development cycle, when extensive debugging capabilities
9934 are most needed, and then move to @option{-O1} and later @option{-O2} as
9935 the debugger becomes less critical.
9936 Whether to use the @option{^-g^/DEBUG^} switch in the release version is
9937 a release management issue.
9938 @ifclear vms
9939 Note that if you use @option{-g} you can then use the @command{strip} program
9940 on the resulting executable,
9941 which removes both debugging information and global symbols.
9942 @end ifclear
9943
9944 @node Inlining of Subprograms
9945 @subsection Inlining of Subprograms
9946
9947 @noindent
9948 A call to a subprogram in the current unit is inlined if all the
9949 following conditions are met:
9950
9951 @itemize @bullet
9952 @item
9953 The optimization level is at least @option{-O1}.
9954
9955 @item
9956 The called subprogram is suitable for inlining: It must be small enough
9957 and not contain something that @command{gcc} cannot support in inlined
9958 subprograms.
9959
9960 @item
9961 @cindex pragma Inline
9962 @findex Inline
9963 Either @code{pragma Inline} applies to the subprogram, or it is local
9964 to the unit and called once from within it, or it is small and automatic
9965 inlining (optimization level @option{-O3}) is specified.
9966 @end itemize
9967
9968 @noindent
9969 Calls to subprograms in @code{with}'ed units are normally not inlined.
9970 To achieve actual inlining (that is, replacement of the call by the code
9971 in the body of the subprogram), the following conditions must all be true.
9972
9973 @itemize @bullet
9974 @item
9975 The optimization level is at least @option{-O1}.
9976
9977 @item
9978 The called subprogram is suitable for inlining: It must be small enough
9979 and not contain something that @command{gcc} cannot support in inlined
9980 subprograms.
9981
9982 @item
9983 The call appears in a body (not in a package spec).
9984
9985 @item
9986 There is a @code{pragma Inline} for the subprogram.
9987
9988 @item
9989 @cindex @option{-gnatn} (@command{gcc})
9990 The @option{^-gnatn^/INLINE^} switch
9991 is used in the @command{gcc} command line
9992 @end itemize
9993
9994 Even if all these conditions are met, it may not be possible for
9995 the compiler to inline the call, due to the length of the body,
9996 or features in the body that make it impossible for the compiler
9997 to do the inlining.
9998
9999 Note that specifying the @option{-gnatn} switch causes additional
10000 compilation dependencies. Consider the following:
10001
10002 @smallexample @c ada
10003 @cartouche
10004 package R is
10005 procedure Q;
10006 pragma Inline (Q);
10007 end R;
10008 package body R is
10009 @dots{}
10010 end R;
10011
10012 with R;
10013 procedure Main is
10014 begin
10015 @dots{}
10016 R.Q;
10017 end Main;
10018 @end cartouche
10019 @end smallexample
10020
10021 @noindent
10022 With the default behavior (no @option{-gnatn} switch specified), the
10023 compilation of the @code{Main} procedure depends only on its own source,
10024 @file{main.adb}, and the spec of the package in file @file{r.ads}. This
10025 means that editing the body of @code{R} does not require recompiling
10026 @code{Main}.
10027
10028 On the other hand, the call @code{R.Q} is not inlined under these
10029 circumstances. If the @option{-gnatn} switch is present when @code{Main}
10030 is compiled, the call will be inlined if the body of @code{Q} is small
10031 enough, but now @code{Main} depends on the body of @code{R} in
10032 @file{r.adb} as well as on the spec. This means that if this body is edited,
10033 the main program must be recompiled. Note that this extra dependency
10034 occurs whether or not the call is in fact inlined by @command{gcc}.
10035
10036 The use of front end inlining with @option{-gnatN} generates similar
10037 additional dependencies.
10038
10039 @cindex @option{^-fno-inline^/INLINE=SUPPRESS^} (@command{gcc})
10040 Note: The @option{^-fno-inline^/INLINE=SUPPRESS^} switch
10041 can be used to prevent
10042 all inlining. This switch overrides all other conditions and ensures
10043 that no inlining occurs. The extra dependences resulting from
10044 @option{-gnatn} will still be active, even if
10045 this switch is used to suppress the resulting inlining actions.
10046
10047 @cindex @option{-fno-inline-functions} (@command{gcc})
10048 Note: The @option{-fno-inline-functions} switch can be used to prevent
10049 automatic inlining of small subprograms if @option{-O3} is used.
10050
10051 @cindex @option{-fno-inline-functions-called-once} (@command{gcc})
10052 Note: The @option{-fno-inline-functions-called-once} switch
10053 can be used to prevent inlining of subprograms local to the unit
10054 and called once from within it if @option{-O1} is used.
10055
10056 Note regarding the use of @option{-O3}: There is no difference in inlining
10057 behavior between @option{-O2} and @option{-O3} for subprograms with an explicit
10058 pragma @code{Inline} assuming the use of @option{-gnatn}
10059 or @option{-gnatN} (the switches that activate inlining). If you have used
10060 pragma @code{Inline} in appropriate cases, then it is usually much better
10061 to use @option{-O2} and @option{-gnatn} and avoid the use of @option{-O3} which
10062 in this case only has the effect of inlining subprograms you did not
10063 think should be inlined. We often find that the use of @option{-O3} slows
10064 down code by performing excessive inlining, leading to increased instruction
10065 cache pressure from the increased code size. So the bottom line here is
10066 that you should not automatically assume that @option{-O3} is better than
10067 @option{-O2}, and indeed you should use @option{-O3} only if tests show that
10068 it actually improves performance.
10069
10070 @node Other Optimization Switches
10071 @subsection Other Optimization Switches
10072 @cindex Optimization Switches
10073
10074 Since @code{GNAT} uses the @command{gcc} back end, all the specialized
10075 @command{gcc} optimization switches are potentially usable. These switches
10076 have not been extensively tested with GNAT but can generally be expected
10077 to work. Examples of switches in this category are
10078 @option{-funroll-loops} and
10079 the various target-specific @option{-m} options (in particular, it has been
10080 observed that @option{-march=pentium4} can significantly improve performance
10081 on appropriate machines). For full details of these switches, see
10082 @ref{Submodel Options,, Hardware Models and Configurations, gcc, Using
10083 the GNU Compiler Collection (GCC)}.
10084
10085 @node Optimization and Strict Aliasing
10086 @subsection Optimization and Strict Aliasing
10087 @cindex Aliasing
10088 @cindex Strict Aliasing
10089 @cindex No_Strict_Aliasing
10090
10091 @noindent
10092 The strong typing capabilities of Ada allow an optimizer to generate
10093 efficient code in situations where other languages would be forced to
10094 make worst case assumptions preventing such optimizations. Consider
10095 the following example:
10096
10097 @smallexample @c ada
10098 @cartouche
10099 procedure R is
10100 type Int1 is new Integer;
10101 type Int2 is new Integer;
10102 type Int1A is access Int1;
10103 type Int2A is access Int2;
10104 Int1V : Int1A;
10105 Int2V : Int2A;
10106 @dots{}
10107
10108 begin
10109 @dots{}
10110 for J in Data'Range loop
10111 if Data (J) = Int1V.all then
10112 Int2V.all := Int2V.all + 1;
10113 end if;
10114 end loop;
10115 @dots{}
10116 end R;
10117 @end cartouche
10118 @end smallexample
10119
10120 @noindent
10121 In this example, since the variable @code{Int1V} can only access objects
10122 of type @code{Int1}, and @code{Int2V} can only access objects of type
10123 @code{Int2}, there is no possibility that the assignment to
10124 @code{Int2V.all} affects the value of @code{Int1V.all}. This means that
10125 the compiler optimizer can "know" that the value @code{Int1V.all} is constant
10126 for all iterations of the loop and avoid the extra memory reference
10127 required to dereference it each time through the loop.
10128
10129 This kind of optimization, called strict aliasing analysis, is
10130 triggered by specifying an optimization level of @option{-O2} or
10131 higher and allows @code{GNAT} to generate more efficient code
10132 when access values are involved.
10133
10134 However, although this optimization is always correct in terms of
10135 the formal semantics of the Ada Reference Manual, difficulties can
10136 arise if features like @code{Unchecked_Conversion} are used to break
10137 the typing system. Consider the following complete program example:
10138
10139 @smallexample @c ada
10140 @cartouche
10141 package p1 is
10142 type int1 is new integer;
10143 type int2 is new integer;
10144 type a1 is access int1;
10145 type a2 is access int2;
10146 end p1;
10147
10148 with p1; use p1;
10149 package p2 is
10150 function to_a2 (Input : a1) return a2;
10151 end p2;
10152
10153 with Unchecked_Conversion;
10154 package body p2 is
10155 function to_a2 (Input : a1) return a2 is
10156 function to_a2u is
10157 new Unchecked_Conversion (a1, a2);
10158 begin
10159 return to_a2u (Input);
10160 end to_a2;
10161 end p2;
10162
10163 with p2; use p2;
10164 with p1; use p1;
10165 with Text_IO; use Text_IO;
10166 procedure m is
10167 v1 : a1 := new int1;
10168 v2 : a2 := to_a2 (v1);
10169 begin
10170 v1.all := 1;
10171 v2.all := 0;
10172 put_line (int1'image (v1.all));
10173 end;
10174 @end cartouche
10175 @end smallexample
10176
10177 @noindent
10178 This program prints out 0 in @option{-O0} or @option{-O1}
10179 mode, but it prints out 1 in @option{-O2} mode. That's
10180 because in strict aliasing mode, the compiler can and
10181 does assume that the assignment to @code{v2.all} could not
10182 affect the value of @code{v1.all}, since different types
10183 are involved.
10184
10185 This behavior is not a case of non-conformance with the standard, since
10186 the Ada RM specifies that an unchecked conversion where the resulting
10187 bit pattern is not a correct value of the target type can result in an
10188 abnormal value and attempting to reference an abnormal value makes the
10189 execution of a program erroneous. That's the case here since the result
10190 does not point to an object of type @code{int2}. This means that the
10191 effect is entirely unpredictable.
10192
10193 However, although that explanation may satisfy a language
10194 lawyer, in practice an applications programmer expects an
10195 unchecked conversion involving pointers to create true
10196 aliases and the behavior of printing 1 seems plain wrong.
10197 In this case, the strict aliasing optimization is unwelcome.
10198
10199 Indeed the compiler recognizes this possibility, and the
10200 unchecked conversion generates a warning:
10201
10202 @smallexample
10203 p2.adb:5:07: warning: possible aliasing problem with type "a2"
10204 p2.adb:5:07: warning: use -fno-strict-aliasing switch for references
10205 p2.adb:5:07: warning: or use "pragma No_Strict_Aliasing (a2);"
10206 @end smallexample
10207
10208 @noindent
10209 Unfortunately the problem is recognized when compiling the body of
10210 package @code{p2}, but the actual "bad" code is generated while
10211 compiling the body of @code{m} and this latter compilation does not see
10212 the suspicious @code{Unchecked_Conversion}.
10213
10214 As implied by the warning message, there are approaches you can use to
10215 avoid the unwanted strict aliasing optimization in a case like this.
10216
10217 One possibility is to simply avoid the use of @option{-O2}, but
10218 that is a bit drastic, since it throws away a number of useful
10219 optimizations that do not involve strict aliasing assumptions.
10220
10221 A less drastic approach is to compile the program using the
10222 option @option{-fno-strict-aliasing}. Actually it is only the
10223 unit containing the dereferencing of the suspicious pointer
10224 that needs to be compiled. So in this case, if we compile
10225 unit @code{m} with this switch, then we get the expected
10226 value of zero printed. Analyzing which units might need
10227 the switch can be painful, so a more reasonable approach
10228 is to compile the entire program with options @option{-O2}
10229 and @option{-fno-strict-aliasing}. If the performance is
10230 satisfactory with this combination of options, then the
10231 advantage is that the entire issue of possible "wrong"
10232 optimization due to strict aliasing is avoided.
10233
10234 To avoid the use of compiler switches, the configuration
10235 pragma @code{No_Strict_Aliasing} with no parameters may be
10236 used to specify that for all access types, the strict
10237 aliasing optimization should be suppressed.
10238
10239 However, these approaches are still overkill, in that they causes
10240 all manipulations of all access values to be deoptimized. A more
10241 refined approach is to concentrate attention on the specific
10242 access type identified as problematic.
10243
10244 First, if a careful analysis of uses of the pointer shows
10245 that there are no possible problematic references, then
10246 the warning can be suppressed by bracketing the
10247 instantiation of @code{Unchecked_Conversion} to turn
10248 the warning off:
10249
10250 @smallexample @c ada
10251 pragma Warnings (Off);
10252 function to_a2u is
10253 new Unchecked_Conversion (a1, a2);
10254 pragma Warnings (On);
10255 @end smallexample
10256
10257 @noindent
10258 Of course that approach is not appropriate for this particular
10259 example, since indeed there is a problematic reference. In this
10260 case we can take one of two other approaches.
10261
10262 The first possibility is to move the instantiation of unchecked
10263 conversion to the unit in which the type is declared. In
10264 this example, we would move the instantiation of
10265 @code{Unchecked_Conversion} from the body of package
10266 @code{p2} to the spec of package @code{p1}. Now the
10267 warning disappears. That's because any use of the
10268 access type knows there is a suspicious unchecked
10269 conversion, and the strict aliasing optimization
10270 is automatically suppressed for the type.
10271
10272 If it is not practical to move the unchecked conversion to the same unit
10273 in which the destination access type is declared (perhaps because the
10274 source type is not visible in that unit), you may use pragma
10275 @code{No_Strict_Aliasing} for the type. This pragma must occur in the
10276 same declarative sequence as the declaration of the access type:
10277
10278 @smallexample @c ada
10279 type a2 is access int2;
10280 pragma No_Strict_Aliasing (a2);
10281 @end smallexample
10282
10283 @noindent
10284 Here again, the compiler now knows that the strict aliasing optimization
10285 should be suppressed for any reference to type @code{a2} and the
10286 expected behavior is obtained.
10287
10288 Finally, note that although the compiler can generate warnings for
10289 simple cases of unchecked conversions, there are tricker and more
10290 indirect ways of creating type incorrect aliases which the compiler
10291 cannot detect. Examples are the use of address overlays and unchecked
10292 conversions involving composite types containing access types as
10293 components. In such cases, no warnings are generated, but there can
10294 still be aliasing problems. One safe coding practice is to forbid the
10295 use of address clauses for type overlaying, and to allow unchecked
10296 conversion only for primitive types. This is not really a significant
10297 restriction since any possible desired effect can be achieved by
10298 unchecked conversion of access values.
10299
10300 @ifset vms
10301 @node Coverage Analysis
10302 @subsection Coverage Analysis
10303
10304 @noindent
10305 GNAT supports the HP Performance Coverage Analyzer (PCA), which allows
10306 the user to determine the distribution of execution time across a program,
10307 @pxref{Profiling} for details of usage.
10308 @end ifset
10309
10310
10311 @node Text_IO Suggestions
10312 @section @code{Text_IO} Suggestions
10313 @cindex @code{Text_IO} and performance
10314
10315 @noindent
10316 The @code{Ada.Text_IO} package has fairly high overheads due in part to
10317 the requirement of maintaining page and line counts. If performance
10318 is critical, a recommendation is to use @code{Stream_IO} instead of
10319 @code{Text_IO} for volume output, since this package has less overhead.
10320
10321 If @code{Text_IO} must be used, note that by default output to the standard
10322 output and standard error files is unbuffered (this provides better
10323 behavior when output statements are used for debugging, or if the
10324 progress of a program is observed by tracking the output, e.g. by
10325 using the Unix @command{tail -f} command to watch redirected output.
10326
10327 If you are generating large volumes of output with @code{Text_IO} and
10328 performance is an important factor, use a designated file instead
10329 of the standard output file, or change the standard output file to
10330 be buffered using @code{Interfaces.C_Streams.setvbuf}.
10331
10332
10333
10334 @node Reducing Size of Ada Executables with gnatelim
10335 @section Reducing Size of Ada Executables with @code{gnatelim}
10336 @findex gnatelim
10337
10338 @noindent
10339 This section describes @command{gnatelim}, a tool which detects unused
10340 subprograms and helps the compiler to create a smaller executable for your
10341 program.
10342
10343 @menu
10344 * About gnatelim::
10345 * Running gnatelim::
10346 * Correcting the List of Eliminate Pragmas::
10347 * Making Your Executables Smaller::
10348 * Summary of the gnatelim Usage Cycle::
10349 @end menu
10350
10351 @node About gnatelim
10352 @subsection About @code{gnatelim}
10353
10354 @noindent
10355 When a program shares a set of Ada
10356 packages with other programs, it may happen that this program uses
10357 only a fraction of the subprograms defined in these packages. The code
10358 created for these unused subprograms increases the size of the executable.
10359
10360 @code{gnatelim} tracks unused subprograms in an Ada program and
10361 outputs a list of GNAT-specific pragmas @code{Eliminate} marking all the
10362 subprograms that are declared but never called. By placing the list of
10363 @code{Eliminate} pragmas in the GNAT configuration file @file{gnat.adc} and
10364 recompiling your program, you may decrease the size of its executable,
10365 because the compiler will not generate the code for 'eliminated' subprograms.
10366 @xref{Pragma Eliminate,,, gnat_rm, GNAT Reference Manual}, for more
10367 information about this pragma.
10368
10369 @code{gnatelim} needs as its input data the name of the main subprogram
10370 and a bind file for a main subprogram.
10371
10372 To create a bind file for @code{gnatelim}, run @code{gnatbind} for
10373 the main subprogram. @code{gnatelim} can work with both Ada and C
10374 bind files; when both are present, it uses the Ada bind file.
10375 The following commands will build the program and create the bind file:
10376
10377 @smallexample
10378 $ gnatmake ^-c Main_Prog^/ACTIONS=COMPILE MAIN_PROG^
10379 $ gnatbind main_prog
10380 @end smallexample
10381
10382 Note that @code{gnatelim} needs neither object nor ALI files.
10383
10384 @node Running gnatelim
10385 @subsection Running @code{gnatelim}
10386
10387 @noindent
10388 @code{gnatelim} has the following command-line interface:
10389
10390 @smallexample
10391 $ gnatelim @ovar{options} name
10392 @end smallexample
10393
10394 @noindent
10395 @code{name} should be a name of a source file that contains the main subprogram
10396 of a program (partition).
10397
10398 @code{gnatelim} has the following switches:
10399
10400 @table @option
10401 @c !sort!
10402 @item ^-q^/QUIET^
10403 @cindex @option{^-q^/QUIET^} (@command{gnatelim})
10404 Quiet mode: by default @code{gnatelim} outputs to the standard error
10405 stream the number of program units left to be processed. This option turns
10406 this trace off.
10407
10408 @item ^-v^/VERBOSE^
10409 @cindex @option{^-v^/VERBOSE^} (@command{gnatelim})
10410 Verbose mode: @code{gnatelim} version information is printed as Ada
10411 comments to the standard output stream. Also, in addition to the number of
10412 program units left @code{gnatelim} will output the name of the current unit
10413 being processed.
10414
10415 @item ^-a^/ALL^
10416 @cindex @option{^-a^/ALL^} (@command{gnatelim})
10417 Also look for subprograms from the GNAT run time that can be eliminated. Note
10418 that when @file{gnat.adc} is produced using this switch, the entire program
10419 must be recompiled with switch @option{^-a^/ALL_FILES^} to @command{gnatmake}.
10420
10421 @item ^-I^/INCLUDE_DIRS=^@var{dir}
10422 @cindex @option{^-I^/INCLUDE_DIRS^} (@command{gnatelim})
10423 When looking for source files also look in directory @var{dir}. Specifying
10424 @option{^-I-^/INCLUDE_DIRS=-^} instructs @code{gnatelim} not to look for
10425 sources in the current directory.
10426
10427 @item ^-b^/BIND_FILE=^@var{bind_file}
10428 @cindex @option{^-b^/BIND_FILE^} (@command{gnatelim})
10429 Specifies @var{bind_file} as the bind file to process. If not set, the name
10430 of the bind file is computed from the full expanded Ada name
10431 of a main subprogram.
10432
10433 @item ^-C^/CONFIG_FILE=^@var{config_file}
10434 @cindex @option{^-C^/CONFIG_FILE^} (@command{gnatelim})
10435 Specifies a file @var{config_file} that contains configuration pragmas. The
10436 file must be specified with full path.
10437
10438 @item ^--GCC^/COMPILER^=@var{compiler_name}
10439 @cindex @option{^-GCC^/COMPILER^} (@command{gnatelim})
10440 Instructs @code{gnatelim} to use specific @command{gcc} compiler instead of one
10441 available on the path.
10442
10443 @item ^--GNATMAKE^/GNATMAKE^=@var{gnatmake_name}
10444 @cindex @option{^--GNATMAKE^/GNATMAKE^} (@command{gnatelim})
10445 Instructs @code{gnatelim} to use specific @command{gnatmake} instead of one
10446 available on the path.
10447 @end table
10448
10449 @noindent
10450 @code{gnatelim} sends its output to the standard output stream, and all the
10451 tracing and debug information is sent to the standard error stream.
10452 In order to produce a proper GNAT configuration file
10453 @file{gnat.adc}, redirection must be used:
10454
10455 @smallexample
10456 @ifset vms
10457 $ PIPE GNAT ELIM MAIN_PROG.ADB > GNAT.ADC
10458 @end ifset
10459 @ifclear vms
10460 $ gnatelim main_prog.adb > gnat.adc
10461 @end ifclear
10462 @end smallexample
10463
10464 @ifclear vms
10465 @noindent
10466 or
10467
10468 @smallexample
10469 $ gnatelim main_prog.adb >> gnat.adc
10470 @end smallexample
10471
10472 @noindent
10473 in order to append the @code{gnatelim} output to the existing contents of
10474 @file{gnat.adc}.
10475 @end ifclear
10476
10477 @node Correcting the List of Eliminate Pragmas
10478 @subsection Correcting the List of Eliminate Pragmas
10479
10480 @noindent
10481 In some rare cases @code{gnatelim} may try to eliminate
10482 subprograms that are actually called in the program. In this case, the
10483 compiler will generate an error message of the form:
10484
10485 @smallexample
10486 file.adb:106:07: cannot call eliminated subprogram "My_Prog"
10487 @end smallexample
10488
10489 @noindent
10490 You will need to manually remove the wrong @code{Eliminate} pragmas from
10491 the @file{gnat.adc} file. You should recompile your program
10492 from scratch after that, because you need a consistent @file{gnat.adc} file
10493 during the entire compilation.
10494
10495 @node Making Your Executables Smaller
10496 @subsection Making Your Executables Smaller
10497
10498 @noindent
10499 In order to get a smaller executable for your program you now have to
10500 recompile the program completely with the new @file{gnat.adc} file
10501 created by @code{gnatelim} in your current directory:
10502
10503 @smallexample
10504 $ gnatmake ^-f main_prog^/FORCE_COMPILE MAIN_PROG^
10505 @end smallexample
10506
10507 @noindent
10508 (Use the @option{^-f^/FORCE_COMPILE^} option for @command{gnatmake} to
10509 recompile everything
10510 with the set of pragmas @code{Eliminate} that you have obtained with
10511 @command{gnatelim}).
10512
10513 Be aware that the set of @code{Eliminate} pragmas is specific to each
10514 program. It is not recommended to merge sets of @code{Eliminate}
10515 pragmas created for different programs in one @file{gnat.adc} file.
10516
10517 @node Summary of the gnatelim Usage Cycle
10518 @subsection Summary of the gnatelim Usage Cycle
10519
10520 @noindent
10521 Here is a quick summary of the steps to be taken in order to reduce
10522 the size of your executables with @code{gnatelim}. You may use
10523 other GNAT options to control the optimization level,
10524 to produce the debugging information, to set search path, etc.
10525
10526 @enumerate
10527 @item
10528 Produce a bind file
10529
10530 @smallexample
10531 $ gnatmake ^-c main_prog^/ACTIONS=COMPILE MAIN_PROG^
10532 $ gnatbind main_prog
10533 @end smallexample
10534
10535 @item
10536 Generate a list of @code{Eliminate} pragmas
10537 @smallexample
10538 @ifset vms
10539 $ PIPE GNAT ELIM MAIN_PROG > GNAT.ADC
10540 @end ifset
10541 @ifclear vms
10542 $ gnatelim main_prog >@r{[}>@r{]} gnat.adc
10543 @end ifclear
10544 @end smallexample
10545
10546 @item
10547 Recompile the application
10548
10549 @smallexample
10550 $ gnatmake ^-f main_prog^/FORCE_COMPILE MAIN_PROG^
10551 @end smallexample
10552
10553 @end enumerate
10554
10555 @node Reducing Size of Executables with unused subprogram/data elimination
10556 @section Reducing Size of Executables with Unused Subprogram/Data Elimination
10557 @findex unused subprogram/data elimination
10558
10559 @noindent
10560 This section describes how you can eliminate unused subprograms and data from
10561 your executable just by setting options at compilation time.
10562
10563 @menu
10564 * About unused subprogram/data elimination::
10565 * Compilation options::
10566 * Example of unused subprogram/data elimination::
10567 @end menu
10568
10569 @node About unused subprogram/data elimination
10570 @subsection About unused subprogram/data elimination
10571
10572 @noindent
10573 By default, an executable contains all code and data of its composing objects
10574 (directly linked or coming from statically linked libraries), even data or code
10575 never used by this executable.
10576
10577 This feature will allow you to eliminate such unused code from your
10578 executable, making it smaller (in disk and in memory).
10579
10580 This functionality is available on all Linux platforms except for the IA-64
10581 architecture and on all cross platforms using the ELF binary file format.
10582 In both cases GNU binutils version 2.16 or later are required to enable it.
10583
10584 @node Compilation options
10585 @subsection Compilation options
10586
10587 @noindent
10588 The operation of eliminating the unused code and data from the final executable
10589 is directly performed by the linker.
10590
10591 In order to do this, it has to work with objects compiled with the
10592 following options:
10593 @option{-ffunction-sections} @option{-fdata-sections}.
10594 @cindex @option{-ffunction-sections} (@command{gcc})
10595 @cindex @option{-fdata-sections} (@command{gcc})
10596 These options are usable with C and Ada files.
10597 They will place respectively each
10598 function or data in a separate section in the resulting object file.
10599
10600 Once the objects and static libraries are created with these options, the
10601 linker can perform the dead code elimination. You can do this by setting
10602 the @option{-Wl,--gc-sections} option to gcc command or in the
10603 @option{-largs} section of @command{gnatmake}. This will perform a
10604 garbage collection of code and data never referenced.
10605
10606 If the linker performs a partial link (@option{-r} ld linker option), then you
10607 will need to provide one or several entry point using the
10608 @option{-e} / @option{--entry} ld option.
10609
10610 Note that objects compiled without the @option{-ffunction-sections} and
10611 @option{-fdata-sections} options can still be linked with the executable.
10612 However, no dead code elimination will be performed on those objects (they will
10613 be linked as is).
10614
10615 The GNAT static library is now compiled with -ffunction-sections and
10616 -fdata-sections on some platforms. This allows you to eliminate the unused code
10617 and data of the GNAT library from your executable.
10618
10619 @node Example of unused subprogram/data elimination
10620 @subsection Example of unused subprogram/data elimination
10621
10622 @noindent
10623 Here is a simple example:
10624
10625 @smallexample @c ada
10626 with Aux;
10627
10628 procedure Test is
10629 begin
10630 Aux.Used (10);
10631 end Test;
10632
10633 package Aux is
10634 Used_Data : Integer;
10635 Unused_Data : Integer;
10636
10637 procedure Used (Data : Integer);
10638 procedure Unused (Data : Integer);
10639 end Aux;
10640
10641 package body Aux is
10642 procedure Used (Data : Integer) is
10643 begin
10644 Used_Data := Data;
10645 end Used;
10646
10647 procedure Unused (Data : Integer) is
10648 begin
10649 Unused_Data := Data;
10650 end Unused;
10651 end Aux;
10652 @end smallexample
10653
10654 @noindent
10655 @code{Unused} and @code{Unused_Data} are never referenced in this code
10656 excerpt, and hence they may be safely removed from the final executable.
10657
10658 @smallexample
10659 $ gnatmake test
10660
10661 $ nm test | grep used
10662 020015f0 T aux__unused
10663 02005d88 B aux__unused_data
10664 020015cc T aux__used
10665 02005d84 B aux__used_data
10666
10667 $ gnatmake test -cargs -fdata-sections -ffunction-sections \
10668 -largs -Wl,--gc-sections
10669
10670 $ nm test | grep used
10671 02005350 T aux__used
10672 0201ffe0 B aux__used_data
10673 @end smallexample
10674
10675 @noindent
10676 It can be observed that the procedure @code{Unused} and the object
10677 @code{Unused_Data} are removed by the linker when using the
10678 appropriate options.
10679
10680 @c ********************************
10681 @node Renaming Files Using gnatchop
10682 @chapter Renaming Files Using @code{gnatchop}
10683 @findex gnatchop
10684
10685 @noindent
10686 This chapter discusses how to handle files with multiple units by using
10687 the @code{gnatchop} utility. This utility is also useful in renaming
10688 files to meet the standard GNAT default file naming conventions.
10689
10690 @menu
10691 * Handling Files with Multiple Units::
10692 * Operating gnatchop in Compilation Mode::
10693 * Command Line for gnatchop::
10694 * Switches for gnatchop::
10695 * Examples of gnatchop Usage::
10696 @end menu
10697
10698 @node Handling Files with Multiple Units
10699 @section Handling Files with Multiple Units
10700
10701 @noindent
10702 The basic compilation model of GNAT requires that a file submitted to the
10703 compiler have only one unit and there be a strict correspondence
10704 between the file name and the unit name.
10705
10706 The @code{gnatchop} utility allows both of these rules to be relaxed,
10707 allowing GNAT to process files which contain multiple compilation units
10708 and files with arbitrary file names. @code{gnatchop}
10709 reads the specified file and generates one or more output files,
10710 containing one unit per file. The unit and the file name correspond,
10711 as required by GNAT.
10712
10713 If you want to permanently restructure a set of ``foreign'' files so that
10714 they match the GNAT rules, and do the remaining development using the
10715 GNAT structure, you can simply use @command{gnatchop} once, generate the
10716 new set of files and work with them from that point on.
10717
10718 Alternatively, if you want to keep your files in the ``foreign'' format,
10719 perhaps to maintain compatibility with some other Ada compilation
10720 system, you can set up a procedure where you use @command{gnatchop} each
10721 time you compile, regarding the source files that it writes as temporary
10722 files that you throw away.
10723
10724 Note that if your file containing multiple units starts with a byte order
10725 mark (BOM) specifying UTF-8 encoding, then the files generated by gnatchop
10726 will each start with a copy of this BOM, meaning that they can be compiled
10727 automatically in UTF-8 mode without needing to specify an explicit encoding.
10728
10729 @node Operating gnatchop in Compilation Mode
10730 @section Operating gnatchop in Compilation Mode
10731
10732 @noindent
10733 The basic function of @code{gnatchop} is to take a file with multiple units
10734 and split it into separate files. The boundary between files is reasonably
10735 clear, except for the issue of comments and pragmas. In default mode, the
10736 rule is that any pragmas between units belong to the previous unit, except
10737 that configuration pragmas always belong to the following unit. Any comments
10738 belong to the following unit. These rules
10739 almost always result in the right choice of
10740 the split point without needing to mark it explicitly and most users will
10741 find this default to be what they want. In this default mode it is incorrect to
10742 submit a file containing only configuration pragmas, or one that ends in
10743 configuration pragmas, to @code{gnatchop}.
10744
10745 However, using a special option to activate ``compilation mode'',
10746 @code{gnatchop}
10747 can perform another function, which is to provide exactly the semantics
10748 required by the RM for handling of configuration pragmas in a compilation.
10749 In the absence of configuration pragmas (at the main file level), this
10750 option has no effect, but it causes such configuration pragmas to be handled
10751 in a quite different manner.
10752
10753 First, in compilation mode, if @code{gnatchop} is given a file that consists of
10754 only configuration pragmas, then this file is appended to the
10755 @file{gnat.adc} file in the current directory. This behavior provides
10756 the required behavior described in the RM for the actions to be taken
10757 on submitting such a file to the compiler, namely that these pragmas
10758 should apply to all subsequent compilations in the same compilation
10759 environment. Using GNAT, the current directory, possibly containing a
10760 @file{gnat.adc} file is the representation
10761 of a compilation environment. For more information on the
10762 @file{gnat.adc} file, see @ref{Handling of Configuration Pragmas}.
10763
10764 Second, in compilation mode, if @code{gnatchop}
10765 is given a file that starts with
10766 configuration pragmas, and contains one or more units, then these
10767 configuration pragmas are prepended to each of the chopped files. This
10768 behavior provides the required behavior described in the RM for the
10769 actions to be taken on compiling such a file, namely that the pragmas
10770 apply to all units in the compilation, but not to subsequently compiled
10771 units.
10772
10773 Finally, if configuration pragmas appear between units, they are appended
10774 to the previous unit. This results in the previous unit being illegal,
10775 since the compiler does not accept configuration pragmas that follow
10776 a unit. This provides the required RM behavior that forbids configuration
10777 pragmas other than those preceding the first compilation unit of a
10778 compilation.
10779
10780 For most purposes, @code{gnatchop} will be used in default mode. The
10781 compilation mode described above is used only if you need exactly
10782 accurate behavior with respect to compilations, and you have files
10783 that contain multiple units and configuration pragmas. In this
10784 circumstance the use of @code{gnatchop} with the compilation mode
10785 switch provides the required behavior, and is for example the mode
10786 in which GNAT processes the ACVC tests.
10787
10788 @node Command Line for gnatchop
10789 @section Command Line for @code{gnatchop}
10790
10791 @noindent
10792 The @code{gnatchop} command has the form:
10793
10794 @smallexample
10795 $ gnatchop switches @var{file name} @r{[}@var{file name} @dots{}@r{]}
10796 @ovar{directory}
10797 @end smallexample
10798
10799 @noindent
10800 The only required argument is the file name of the file to be chopped.
10801 There are no restrictions on the form of this file name. The file itself
10802 contains one or more Ada units, in normal GNAT format, concatenated
10803 together. As shown, more than one file may be presented to be chopped.
10804
10805 When run in default mode, @code{gnatchop} generates one output file in
10806 the current directory for each unit in each of the files.
10807
10808 @var{directory}, if specified, gives the name of the directory to which
10809 the output files will be written. If it is not specified, all files are
10810 written to the current directory.
10811
10812 For example, given a
10813 file called @file{hellofiles} containing
10814
10815 @smallexample @c ada
10816 @group
10817 @cartouche
10818 procedure hello;
10819
10820 with Text_IO; use Text_IO;
10821 procedure hello is
10822 begin
10823 Put_Line ("Hello");
10824 end hello;
10825 @end cartouche
10826 @end group
10827 @end smallexample
10828
10829 @noindent
10830 the command
10831
10832 @smallexample
10833 $ gnatchop ^hellofiles^HELLOFILES.^
10834 @end smallexample
10835
10836 @noindent
10837 generates two files in the current directory, one called
10838 @file{hello.ads} containing the single line that is the procedure spec,
10839 and the other called @file{hello.adb} containing the remaining text. The
10840 original file is not affected. The generated files can be compiled in
10841 the normal manner.
10842
10843 @noindent
10844 When gnatchop is invoked on a file that is empty or that contains only empty
10845 lines and/or comments, gnatchop will not fail, but will not produce any
10846 new sources.
10847
10848 For example, given a
10849 file called @file{toto.txt} containing
10850
10851 @smallexample @c ada
10852 @group
10853 @cartouche
10854 -- Just a comment
10855 @end cartouche
10856 @end group
10857 @end smallexample
10858
10859 @noindent
10860 the command
10861
10862 @smallexample
10863 $ gnatchop ^toto.txt^TOT.TXT^
10864 @end smallexample
10865
10866 @noindent
10867 will not produce any new file and will result in the following warnings:
10868
10869 @smallexample
10870 toto.txt:1:01: warning: empty file, contains no compilation units
10871 no compilation units found
10872 no source files written
10873 @end smallexample
10874
10875 @node Switches for gnatchop
10876 @section Switches for @code{gnatchop}
10877
10878 @noindent
10879 @command{gnatchop} recognizes the following switches:
10880
10881 @table @option
10882 @c !sort!
10883
10884 @item --version
10885 @cindex @option{--version} @command{gnatchop}
10886 Display Copyright and version, then exit disregarding all other options.
10887
10888 @item --help
10889 @cindex @option{--help} @command{gnatchop}
10890 If @option{--version} was not used, display usage, then exit disregarding
10891 all other options.
10892
10893 @item ^-c^/COMPILATION^
10894 @cindex @option{^-c^/COMPILATION^} (@code{gnatchop})
10895 Causes @code{gnatchop} to operate in compilation mode, in which
10896 configuration pragmas are handled according to strict RM rules. See
10897 previous section for a full description of this mode.
10898
10899 @ifclear vms
10900 @item -gnat@var{xxx}
10901 This passes the given @option{-gnat@var{xxx}} switch to @code{gnat} which is
10902 used to parse the given file. Not all @var{xxx} options make sense,
10903 but for example, the use of @option{-gnati2} allows @code{gnatchop} to
10904 process a source file that uses Latin-2 coding for identifiers.
10905 @end ifclear
10906
10907 @item ^-h^/HELP^
10908 Causes @code{gnatchop} to generate a brief help summary to the standard
10909 output file showing usage information.
10910
10911 @item ^-k@var{mm}^/FILE_NAME_MAX_LENGTH=@var{mm}^
10912 @cindex @option{^-k^/FILE_NAME_MAX_LENGTH^} (@code{gnatchop})
10913 Limit generated file names to the specified number @code{mm}
10914 of characters.
10915 This is useful if the
10916 resulting set of files is required to be interoperable with systems
10917 which limit the length of file names.
10918 @ifset vms
10919 If no value is given, or
10920 if no @code{/FILE_NAME_MAX_LENGTH} qualifier is given,
10921 a default of 39, suitable for OpenVMS Alpha
10922 Systems, is assumed
10923 @end ifset
10924 @ifclear vms
10925 No space is allowed between the @option{-k} and the numeric value. The numeric
10926 value may be omitted in which case a default of @option{-k8},
10927 suitable for use
10928 with DOS-like file systems, is used. If no @option{-k} switch
10929 is present then
10930 there is no limit on the length of file names.
10931 @end ifclear
10932
10933 @item ^-p^/PRESERVE^
10934 @cindex @option{^-p^/PRESERVE^} (@code{gnatchop})
10935 Causes the file ^modification^creation^ time stamp of the input file to be
10936 preserved and used for the time stamp of the output file(s). This may be
10937 useful for preserving coherency of time stamps in an environment where
10938 @code{gnatchop} is used as part of a standard build process.
10939
10940 @item ^-q^/QUIET^
10941 @cindex @option{^-q^/QUIET^} (@code{gnatchop})
10942 Causes output of informational messages indicating the set of generated
10943 files to be suppressed. Warnings and error messages are unaffected.
10944
10945 @item ^-r^/REFERENCE^
10946 @cindex @option{^-r^/REFERENCE^} (@code{gnatchop})
10947 @findex Source_Reference
10948 Generate @code{Source_Reference} pragmas. Use this switch if the output
10949 files are regarded as temporary and development is to be done in terms
10950 of the original unchopped file. This switch causes
10951 @code{Source_Reference} pragmas to be inserted into each of the
10952 generated files to refers back to the original file name and line number.
10953 The result is that all error messages refer back to the original
10954 unchopped file.
10955 In addition, the debugging information placed into the object file (when
10956 the @option{^-g^/DEBUG^} switch of @command{gcc} or @command{gnatmake} is
10957 specified)
10958 also refers back to this original file so that tools like profilers and
10959 debuggers will give information in terms of the original unchopped file.
10960
10961 If the original file to be chopped itself contains
10962 a @code{Source_Reference}
10963 pragma referencing a third file, then gnatchop respects
10964 this pragma, and the generated @code{Source_Reference} pragmas
10965 in the chopped file refer to the original file, with appropriate
10966 line numbers. This is particularly useful when @code{gnatchop}
10967 is used in conjunction with @code{gnatprep} to compile files that
10968 contain preprocessing statements and multiple units.
10969
10970 @item ^-v^/VERBOSE^
10971 @cindex @option{^-v^/VERBOSE^} (@code{gnatchop})
10972 Causes @code{gnatchop} to operate in verbose mode. The version
10973 number and copyright notice are output, as well as exact copies of
10974 the gnat1 commands spawned to obtain the chop control information.
10975
10976 @item ^-w^/OVERWRITE^
10977 @cindex @option{^-w^/OVERWRITE^} (@code{gnatchop})
10978 Overwrite existing file names. Normally @code{gnatchop} regards it as a
10979 fatal error if there is already a file with the same name as a
10980 file it would otherwise output, in other words if the files to be
10981 chopped contain duplicated units. This switch bypasses this
10982 check, and causes all but the last instance of such duplicated
10983 units to be skipped.
10984
10985 @ifclear vms
10986 @item --GCC=@var{xxxx}
10987 @cindex @option{--GCC=} (@code{gnatchop})
10988 Specify the path of the GNAT parser to be used. When this switch is used,
10989 no attempt is made to add the prefix to the GNAT parser executable.
10990 @end ifclear
10991 @end table
10992
10993 @node Examples of gnatchop Usage
10994 @section Examples of @code{gnatchop} Usage
10995
10996 @table @code
10997 @ifset vms
10998 @item gnatchop /OVERWRITE HELLO_S.ADA [PRERELEASE.FILES]
10999 @end ifset
11000 @ifclear vms
11001 @item gnatchop -w hello_s.ada prerelease/files
11002 @end ifclear
11003
11004 Chops the source file @file{hello_s.ada}. The output files will be
11005 placed in the directory @file{^prerelease/files^[PRERELEASE.FILES]^},
11006 overwriting any
11007 files with matching names in that directory (no files in the current
11008 directory are modified).
11009
11010 @item gnatchop ^archive^ARCHIVE.^
11011 Chops the source file @file{^archive^ARCHIVE.^}
11012 into the current directory. One
11013 useful application of @code{gnatchop} is in sending sets of sources
11014 around, for example in email messages. The required sources are simply
11015 concatenated (for example, using a ^Unix @code{cat}^VMS @code{APPEND/NEW}^
11016 command), and then
11017 @command{gnatchop} is used at the other end to reconstitute the original
11018 file names.
11019
11020 @item gnatchop file1 file2 file3 direc
11021 Chops all units in files @file{file1}, @file{file2}, @file{file3}, placing
11022 the resulting files in the directory @file{direc}. Note that if any units
11023 occur more than once anywhere within this set of files, an error message
11024 is generated, and no files are written. To override this check, use the
11025 @option{^-w^/OVERWRITE^} switch,
11026 in which case the last occurrence in the last file will
11027 be the one that is output, and earlier duplicate occurrences for a given
11028 unit will be skipped.
11029 @end table
11030
11031 @node Configuration Pragmas
11032 @chapter Configuration Pragmas
11033 @cindex Configuration pragmas
11034 @cindex Pragmas, configuration
11035
11036 @noindent
11037 Configuration pragmas include those pragmas described as
11038 such in the Ada Reference Manual, as well as
11039 implementation-dependent pragmas that are configuration pragmas.
11040 @xref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference Manual},
11041 for details on these additional GNAT-specific configuration pragmas.
11042 Most notably, the pragma @code{Source_File_Name}, which allows
11043 specifying non-default names for source files, is a configuration
11044 pragma. The following is a complete list of configuration pragmas
11045 recognized by GNAT:
11046
11047 @smallexample
11048 Ada_83
11049 Ada_95
11050 Ada_05
11051 Ada_2005
11052 Assertion_Policy
11053 C_Pass_By_Copy
11054 Check_Name
11055 Check_Policy
11056 Compile_Time_Error
11057 Compile_Time_Warning
11058 Compiler_Unit
11059 Component_Alignment
11060 Debug_Policy
11061 Detect_Blocking
11062 Discard_Names
11063 Elaboration_Checks
11064 Eliminate
11065 Extend_System
11066 External_Name_Casing
11067 Fast_Math
11068 Favor_Top_Level
11069 Float_Representation
11070 Implicit_Packing
11071 Initialize_Scalars
11072 Interrupt_State
11073 License
11074 Locking_Policy
11075 Long_Float
11076 No_Run_Time
11077 No_Strict_Aliasing
11078 Normalize_Scalars
11079 Optimize_Alignment
11080 Persistent_BSS
11081 Polling
11082 Priority_Specific_Dispatching
11083 Profile
11084 Profile_Warnings
11085 Propagate_Exceptions
11086 Queuing_Policy
11087 Ravenscar
11088 Restricted_Run_Time
11089 Restrictions
11090 Restrictions_Warnings
11091 Reviewable
11092 Source_File_Name
11093 Source_File_Name_Project
11094 Style_Checks
11095 Suppress
11096 Suppress_Exception_Locations
11097 Task_Dispatching_Policy
11098 Universal_Data
11099 Unsuppress
11100 Use_VADS_Size
11101 Validity_Checks
11102 Warnings
11103 Wide_Character_Encoding
11104
11105 @end smallexample
11106
11107 @menu
11108 * Handling of Configuration Pragmas::
11109 * The Configuration Pragmas Files::
11110 @end menu
11111
11112 @node Handling of Configuration Pragmas
11113 @section Handling of Configuration Pragmas
11114
11115 Configuration pragmas may either appear at the start of a compilation
11116 unit, in which case they apply only to that unit, or they may apply to
11117 all compilations performed in a given compilation environment.
11118
11119 GNAT also provides the @code{gnatchop} utility to provide an automatic
11120 way to handle configuration pragmas following the semantics for
11121 compilations (that is, files with multiple units), described in the RM.
11122 See @ref{Operating gnatchop in Compilation Mode} for details.
11123 However, for most purposes, it will be more convenient to edit the
11124 @file{gnat.adc} file that contains configuration pragmas directly,
11125 as described in the following section.
11126
11127 @node The Configuration Pragmas Files
11128 @section The Configuration Pragmas Files
11129 @cindex @file{gnat.adc}
11130
11131 @noindent
11132 In GNAT a compilation environment is defined by the current
11133 directory at the time that a compile command is given. This current
11134 directory is searched for a file whose name is @file{gnat.adc}. If
11135 this file is present, it is expected to contain one or more
11136 configuration pragmas that will be applied to the current compilation.
11137 However, if the switch @option{-gnatA} is used, @file{gnat.adc} is not
11138 considered.
11139
11140 Configuration pragmas may be entered into the @file{gnat.adc} file
11141 either by running @code{gnatchop} on a source file that consists only of
11142 configuration pragmas, or more conveniently by
11143 direct editing of the @file{gnat.adc} file, which is a standard format
11144 source file.
11145
11146 In addition to @file{gnat.adc}, additional files containing configuration
11147 pragmas may be applied to the current compilation using the switch
11148 @option{-gnatec}@var{path}. @var{path} must designate an existing file that
11149 contains only configuration pragmas. These configuration pragmas are
11150 in addition to those found in @file{gnat.adc} (provided @file{gnat.adc}
11151 is present and switch @option{-gnatA} is not used).
11152
11153 It is allowed to specify several switches @option{-gnatec}, all of which
11154 will be taken into account.
11155
11156 If you are using project file, a separate mechanism is provided using
11157 project attributes, see @ref{Specifying Configuration Pragmas} for more
11158 details.
11159
11160 @ifset vms
11161 Of special interest to GNAT OpenVMS Alpha is the following
11162 configuration pragma:
11163
11164 @smallexample @c ada
11165 @cartouche
11166 pragma Extend_System (Aux_DEC);
11167 @end cartouche
11168 @end smallexample
11169
11170 @noindent
11171 In the presence of this pragma, GNAT adds to the definition of the
11172 predefined package SYSTEM all the additional types and subprograms that are
11173 defined in HP Ada. See @ref{Compatibility with HP Ada} for details.
11174 @end ifset
11175
11176 @node Handling Arbitrary File Naming Conventions Using gnatname
11177 @chapter Handling Arbitrary File Naming Conventions Using @code{gnatname}
11178 @cindex Arbitrary File Naming Conventions
11179
11180 @menu
11181 * Arbitrary File Naming Conventions::
11182 * Running gnatname::
11183 * Switches for gnatname::
11184 * Examples of gnatname Usage::
11185 @end menu
11186
11187 @node Arbitrary File Naming Conventions
11188 @section Arbitrary File Naming Conventions
11189
11190 @noindent
11191 The GNAT compiler must be able to know the source file name of a compilation
11192 unit. When using the standard GNAT default file naming conventions
11193 (@code{.ads} for specs, @code{.adb} for bodies), the GNAT compiler
11194 does not need additional information.
11195
11196 @noindent
11197 When the source file names do not follow the standard GNAT default file naming
11198 conventions, the GNAT compiler must be given additional information through
11199 a configuration pragmas file (@pxref{Configuration Pragmas})
11200 or a project file.
11201 When the non-standard file naming conventions are well-defined,
11202 a small number of pragmas @code{Source_File_Name} specifying a naming pattern
11203 (@pxref{Alternative File Naming Schemes}) may be sufficient. However,
11204 if the file naming conventions are irregular or arbitrary, a number
11205 of pragma @code{Source_File_Name} for individual compilation units
11206 must be defined.
11207 To help maintain the correspondence between compilation unit names and
11208 source file names within the compiler,
11209 GNAT provides a tool @code{gnatname} to generate the required pragmas for a
11210 set of files.
11211
11212 @node Running gnatname
11213 @section Running @code{gnatname}
11214
11215 @noindent
11216 The usual form of the @code{gnatname} command is
11217
11218 @smallexample
11219 $ gnatname @ovar{switches} @var{naming_pattern} @ovar{naming_patterns}
11220 @r{[}--and @ovar{switches} @var{naming_pattern} @ovar{naming_patterns}@r{]}
11221 @end smallexample
11222
11223 @noindent
11224 All of the arguments are optional. If invoked without any argument,
11225 @code{gnatname} will display its usage.
11226
11227 @noindent
11228 When used with at least one naming pattern, @code{gnatname} will attempt to
11229 find all the compilation units in files that follow at least one of the
11230 naming patterns. To find these compilation units,
11231 @code{gnatname} will use the GNAT compiler in syntax-check-only mode on all
11232 regular files.
11233
11234 @noindent
11235 One or several Naming Patterns may be given as arguments to @code{gnatname}.
11236 Each Naming Pattern is enclosed between double quotes.
11237 A Naming Pattern is a regular expression similar to the wildcard patterns
11238 used in file names by the Unix shells or the DOS prompt.
11239
11240 @noindent
11241 @code{gnatname} may be called with several sections of directories/patterns.
11242 Sections are separated by switch @code{--and}. In each section, there must be
11243 at least one pattern. If no directory is specified in a section, the current
11244 directory (or the project directory is @code{-P} is used) is implied.
11245 The options other that the directory switches and the patterns apply globally
11246 even if they are in different sections.
11247
11248 @noindent
11249 Examples of Naming Patterns are
11250
11251 @smallexample
11252 "*.[12].ada"
11253 "*.ad[sb]*"
11254 "body_*" "spec_*"
11255 @end smallexample
11256
11257 @noindent
11258 For a more complete description of the syntax of Naming Patterns,
11259 see the second kind of regular expressions described in @file{g-regexp.ads}
11260 (the ``Glob'' regular expressions).
11261
11262 @noindent
11263 When invoked with no switch @code{-P}, @code{gnatname} will create a
11264 configuration pragmas file @file{gnat.adc} in the current working directory,
11265 with pragmas @code{Source_File_Name} for each file that contains a valid Ada
11266 unit.
11267
11268 @node Switches for gnatname
11269 @section Switches for @code{gnatname}
11270
11271 @noindent
11272 Switches for @code{gnatname} must precede any specified Naming Pattern.
11273
11274 @noindent
11275 You may specify any of the following switches to @code{gnatname}:
11276
11277 @table @option
11278 @c !sort!
11279
11280 @item --version
11281 @cindex @option{--version} @command{gnatname}
11282 Display Copyright and version, then exit disregarding all other options.
11283
11284 @item --help
11285 @cindex @option{--help} @command{gnatname}
11286 If @option{--version} was not used, display usage, then exit disregarding
11287 all other options.
11288
11289 @item --and
11290 Start another section of directories/patterns.
11291
11292 @item ^-c^/CONFIG_FILE=^@file{file}
11293 @cindex @option{^-c^/CONFIG_FILE^} (@code{gnatname})
11294 Create a configuration pragmas file @file{file} (instead of the default
11295 @file{gnat.adc}).
11296 @ifclear vms
11297 There may be zero, one or more space between @option{-c} and
11298 @file{file}.
11299 @end ifclear
11300 @file{file} may include directory information. @file{file} must be
11301 writable. There may be only one switch @option{^-c^/CONFIG_FILE^}.
11302 When a switch @option{^-c^/CONFIG_FILE^} is
11303 specified, no switch @option{^-P^/PROJECT_FILE^} may be specified (see below).
11304
11305 @item ^-d^/SOURCE_DIRS=^@file{dir}
11306 @cindex @option{^-d^/SOURCE_DIRS^} (@code{gnatname})
11307 Look for source files in directory @file{dir}. There may be zero, one or more
11308 spaces between @option{^-d^/SOURCE_DIRS=^} and @file{dir}.
11309 When a switch @option{^-d^/SOURCE_DIRS^}
11310 is specified, the current working directory will not be searched for source
11311 files, unless it is explicitly specified with a @option{^-d^/SOURCE_DIRS^}
11312 or @option{^-D^/DIR_FILES^} switch.
11313 Several switches @option{^-d^/SOURCE_DIRS^} may be specified.
11314 If @file{dir} is a relative path, it is relative to the directory of
11315 the configuration pragmas file specified with switch
11316 @option{^-c^/CONFIG_FILE^},
11317 or to the directory of the project file specified with switch
11318 @option{^-P^/PROJECT_FILE^} or,
11319 if neither switch @option{^-c^/CONFIG_FILE^}
11320 nor switch @option{^-P^/PROJECT_FILE^} are specified, it is relative to the
11321 current working directory. The directory
11322 specified with switch @option{^-d^/SOURCE_DIRS^} must exist and be readable.
11323
11324 @item ^-D^/DIRS_FILE=^@file{file}
11325 @cindex @option{^-D^/DIRS_FILE^} (@code{gnatname})
11326 Look for source files in all directories listed in text file @file{file}.
11327 There may be zero, one or more spaces between @option{^-D^/DIRS_FILE=^}
11328 and @file{file}.
11329 @file{file} must be an existing, readable text file.
11330 Each nonempty line in @file{file} must be a directory.
11331 Specifying switch @option{^-D^/DIRS_FILE^} is equivalent to specifying as many
11332 switches @option{^-d^/SOURCE_DIRS^} as there are nonempty lines in
11333 @file{file}.
11334
11335 @item ^-f^/FOREIGN_PATTERN=^@file{pattern}
11336 @cindex @option{^-f^/FOREIGN_PATTERN^} (@code{gnatname})
11337 Foreign patterns. Using this switch, it is possible to add sources of languages
11338 other than Ada to the list of sources of a project file.
11339 It is only useful if a ^-P^/PROJECT_FILE^ switch is used.
11340 For example,
11341 @smallexample
11342 gnatname ^-Pprj -f"*.c"^/PROJECT_FILE=PRJ /FOREIGN_PATTERN=*.C^ "*.ada"
11343 @end smallexample
11344 @noindent
11345 will look for Ada units in all files with the @file{.ada} extension,
11346 and will add to the list of file for project @file{prj.gpr} the C files
11347 with extension @file{.^c^C^}.
11348
11349 @item ^-h^/HELP^
11350 @cindex @option{^-h^/HELP^} (@code{gnatname})
11351 Output usage (help) information. The output is written to @file{stdout}.
11352
11353 @item ^-P^/PROJECT_FILE=^@file{proj}
11354 @cindex @option{^-P^/PROJECT_FILE^} (@code{gnatname})
11355 Create or update project file @file{proj}. There may be zero, one or more space
11356 between @option{-P} and @file{proj}. @file{proj} may include directory
11357 information. @file{proj} must be writable.
11358 There may be only one switch @option{^-P^/PROJECT_FILE^}.
11359 When a switch @option{^-P^/PROJECT_FILE^} is specified,
11360 no switch @option{^-c^/CONFIG_FILE^} may be specified.
11361
11362 @item ^-v^/VERBOSE^
11363 @cindex @option{^-v^/VERBOSE^} (@code{gnatname})
11364 Verbose mode. Output detailed explanation of behavior to @file{stdout}.
11365 This includes name of the file written, the name of the directories to search
11366 and, for each file in those directories whose name matches at least one of
11367 the Naming Patterns, an indication of whether the file contains a unit,
11368 and if so the name of the unit.
11369
11370 @item ^-v -v^/VERBOSE /VERBOSE^
11371 @cindex @option{^-v -v^/VERBOSE /VERBOSE^} (@code{gnatname})
11372 Very Verbose mode. In addition to the output produced in verbose mode,
11373 for each file in the searched directories whose name matches none of
11374 the Naming Patterns, an indication is given that there is no match.
11375
11376 @item ^-x^/EXCLUDED_PATTERN=^@file{pattern}
11377 @cindex @option{^-x^/EXCLUDED_PATTERN^} (@code{gnatname})
11378 Excluded patterns. Using this switch, it is possible to exclude some files
11379 that would match the name patterns. For example,
11380 @smallexample
11381 gnatname ^-x "*_nt.ada"^/EXCLUDED_PATTERN=*_nt.ada^ "*.ada"
11382 @end smallexample
11383 @noindent
11384 will look for Ada units in all files with the @file{.ada} extension,
11385 except those whose names end with @file{_nt.ada}.
11386
11387 @end table
11388
11389 @node Examples of gnatname Usage
11390 @section Examples of @code{gnatname} Usage
11391
11392 @ifset vms
11393 @smallexample
11394 $ gnatname /CONFIG_FILE=[HOME.ME]NAMES.ADC /SOURCE_DIRS=SOURCES "[a-z]*.ada*"
11395 @end smallexample
11396 @end ifset
11397
11398 @ifclear vms
11399 @smallexample
11400 $ gnatname -c /home/me/names.adc -d sources "[a-z]*.ada*"
11401 @end smallexample
11402 @end ifclear
11403
11404 @noindent
11405 In this example, the directory @file{^/home/me^[HOME.ME]^} must already exist
11406 and be writable. In addition, the directory
11407 @file{^/home/me/sources^[HOME.ME.SOURCES]^} (specified by
11408 @option{^-d sources^/SOURCE_DIRS=SOURCES^}) must exist and be readable.
11409
11410 @ifclear vms
11411 Note the optional spaces after @option{-c} and @option{-d}.
11412 @end ifclear
11413
11414 @smallexample
11415 @ifclear vms
11416 $ gnatname -P/home/me/proj -x "*_nt_body.ada"
11417 -dsources -dsources/plus -Dcommon_dirs.txt "body_*" "spec_*"
11418 @end ifclear
11419 @ifset vms
11420 $ gnatname /PROJECT_FILE=[HOME.ME]PROJ
11421 /EXCLUDED_PATTERN=*_nt_body.ada
11422 /SOURCE_DIRS=(SOURCES,[SOURCES.PLUS])
11423 /DIRS_FILE=COMMON_DIRS.TXT "body_*" "spec_*"
11424 @end ifset
11425 @end smallexample
11426
11427 Note that several switches @option{^-d^/SOURCE_DIRS^} may be used,
11428 even in conjunction with one or several switches
11429 @option{^-D^/DIRS_FILE^}. Several Naming Patterns and one excluded pattern
11430 are used in this example.
11431
11432 @c *****************************************
11433 @c * G N A T P r o j e c t M a n a g e r *
11434 @c *****************************************
11435 @node GNAT Project Manager
11436 @chapter GNAT Project Manager
11437
11438 @menu
11439 * Introduction::
11440 * Examples of Project Files::
11441 * Project File Syntax::
11442 * Objects and Sources in Project Files::
11443 * Importing Projects::
11444 * Project Extension::
11445 * Project Hierarchy Extension::
11446 * External References in Project Files::
11447 * Packages in Project Files::
11448 * Variables from Imported Projects::
11449 * Naming Schemes::
11450 * Library Projects::
11451 * Stand-alone Library Projects::
11452 * Switches Related to Project Files::
11453 * Tools Supporting Project Files::
11454 * An Extended Example::
11455 * Project File Complete Syntax::
11456 @end menu
11457
11458 @c ****************
11459 @c * Introduction *
11460 @c ****************
11461
11462 @node Introduction
11463 @section Introduction
11464
11465 @noindent
11466 This chapter describes GNAT's @emph{Project Manager}, a facility that allows
11467 you to manage complex builds involving a number of source files, directories,
11468 and compilation options for different system configurations. In particular,
11469 project files allow you to specify:
11470 @itemize @bullet
11471 @item
11472 The directory or set of directories containing the source files, and/or the
11473 names of the specific source files themselves
11474 @item
11475 The directory in which the compiler's output
11476 (@file{ALI} files, object files, tree files) is to be placed
11477 @item
11478 The directory in which the executable programs is to be placed
11479 @item
11480 ^Switch^Switch^ settings for any of the project-enabled tools
11481 (@command{gnatmake}, compiler, binder, linker, @code{gnatls}, @code{gnatxref},
11482 @code{gnatfind}); you can apply these settings either globally or to individual
11483 compilation units.
11484 @item
11485 The source files containing the main subprogram(s) to be built
11486 @item
11487 The source programming language(s) (currently Ada and/or C)
11488 @item
11489 Source file naming conventions; you can specify these either globally or for
11490 individual compilation units
11491 @end itemize
11492
11493 @menu
11494 * Project Files::
11495 @end menu
11496
11497 @node Project Files
11498 @subsection Project Files
11499
11500 @noindent
11501 Project files are written in a syntax close to that of Ada, using familiar
11502 notions such as packages, context clauses, declarations, default values,
11503 assignments, and inheritance. Finally, project files can be built
11504 hierarchically from other project files, simplifying complex system
11505 integration and project reuse.
11506
11507 A @dfn{project} is a specific set of values for various compilation properties.
11508 The settings for a given project are described by means of
11509 a @dfn{project file}, which is a text file written in an Ada-like syntax.
11510 Property values in project files are either strings or lists of strings.
11511 Properties that are not explicitly set receive default values. A project
11512 file may interrogate the values of @dfn{external variables} (user-defined
11513 command-line switches or environment variables), and it may specify property
11514 settings conditionally, based on the value of such variables.
11515
11516 In simple cases, a project's source files depend only on other source files
11517 in the same project, or on the predefined libraries. (@emph{Dependence} is
11518 used in
11519 the Ada technical sense; as in one Ada unit @code{with}ing another.) However,
11520 the Project Manager also allows more sophisticated arrangements,
11521 where the source files in one project depend on source files in other
11522 projects:
11523 @itemize @bullet
11524 @item
11525 One project can @emph{import} other projects containing needed source files.
11526 @item
11527 You can organize GNAT projects in a hierarchy: a @emph{child} project
11528 can extend a @emph{parent} project, inheriting the parent's source files and
11529 optionally overriding any of them with alternative versions
11530 @end itemize
11531
11532 @noindent
11533 More generally, the Project Manager lets you structure large development
11534 efforts into hierarchical subsystems, where build decisions are delegated
11535 to the subsystem level, and thus different compilation environments
11536 (^switch^switch^ settings) used for different subsystems.
11537
11538 The Project Manager is invoked through the
11539 @option{^-P^/PROJECT_FILE=^@emph{projectfile}}
11540 switch to @command{gnatmake} or to the @command{^gnat^GNAT^} front driver.
11541 @ifclear vms
11542 There may be zero, one or more spaces between @option{-P} and
11543 @option{@emph{projectfile}}.
11544 @end ifclear
11545 If you want to define (on the command line) an external variable that is
11546 queried by the project file, you must use the
11547 @option{^-X^/EXTERNAL_REFERENCE=^@emph{vbl}=@emph{value}} switch.
11548 The Project Manager parses and interprets the project file, and drives the
11549 invoked tool based on the project settings.
11550
11551 The Project Manager supports a wide range of development strategies,
11552 for systems of all sizes. Here are some typical practices that are
11553 easily handled:
11554 @itemize @bullet
11555 @item
11556 Using a common set of source files, but generating object files in different
11557 directories via different ^switch^switch^ settings
11558 @item
11559 Using a mostly-shared set of source files, but with different versions of
11560 some unit or units
11561 @end itemize
11562
11563 @noindent
11564 The destination of an executable can be controlled inside a project file
11565 using the @option{^-o^-o^}
11566 ^switch^switch^.
11567 In the absence of such a ^switch^switch^ either inside
11568 the project file or on the command line, any executable files generated by
11569 @command{gnatmake} are placed in the directory @code{Exec_Dir} specified
11570 in the project file. If no @code{Exec_Dir} is specified, they will be placed
11571 in the object directory of the project.
11572
11573 You can use project files to achieve some of the effects of a source
11574 versioning system (for example, defining separate projects for
11575 the different sets of sources that comprise different releases) but the
11576 Project Manager is independent of any source configuration management tools
11577 that might be used by the developers.
11578
11579 The next section introduces the main features of GNAT's project facility
11580 through a sequence of examples; subsequent sections will present the syntax
11581 and semantics in more detail. A more formal description of the project
11582 facility appears in @ref{Project File Reference,,, gnat_rm, GNAT
11583 Reference Manual}.
11584
11585 @c *****************************
11586 @c * Examples of Project Files *
11587 @c *****************************
11588
11589 @node Examples of Project Files
11590 @section Examples of Project Files
11591 @noindent
11592 This section illustrates some of the typical uses of project files and
11593 explains their basic structure and behavior.
11594
11595 @menu
11596 * Common Sources with Different ^Switches^Switches^ and Directories::
11597 * Using External Variables::
11598 * Importing Other Projects::
11599 * Extending a Project::
11600 @end menu
11601
11602 @node Common Sources with Different ^Switches^Switches^ and Directories
11603 @subsection Common Sources with Different ^Switches^Switches^ and Directories
11604
11605 @menu
11606 * Source Files::
11607 * Specifying the Object Directory::
11608 * Specifying the Exec Directory::
11609 * Project File Packages::
11610 * Specifying ^Switch^Switch^ Settings::
11611 * Main Subprograms::
11612 * Executable File Names::
11613 * Source File Naming Conventions::
11614 * Source Language(s)::
11615 @end menu
11616
11617 @noindent
11618 Suppose that the Ada source files @file{pack.ads}, @file{pack.adb}, and
11619 @file{proc.adb} are in the @file{/common} directory. The file
11620 @file{proc.adb} contains an Ada main subprogram @code{Proc} that @code{with}s
11621 package @code{Pack}. We want to compile these source files under two sets
11622 of ^switches^switches^:
11623 @itemize @bullet
11624 @item
11625 When debugging, we want to pass the @option{-g} switch to @command{gnatmake},
11626 and the @option{^-gnata^-gnata^},
11627 @option{^-gnato^-gnato^},
11628 and @option{^-gnatE^-gnatE^} switches to the
11629 compiler; the compiler's output is to appear in @file{/common/debug}
11630 @item
11631 When preparing a release version, we want to pass the @option{^-O2^O2^} switch
11632 to the compiler; the compiler's output is to appear in @file{/common/release}
11633 @end itemize
11634
11635 @noindent
11636 The GNAT project files shown below, respectively @file{debug.gpr} and
11637 @file{release.gpr} in the @file{/common} directory, achieve these effects.
11638
11639 Schematically:
11640 @smallexample
11641 @group
11642 ^/common^[COMMON]^
11643 debug.gpr
11644 release.gpr
11645 pack.ads
11646 pack.adb
11647 proc.adb
11648 @end group
11649 @group
11650 ^/common/debug^[COMMON.DEBUG]^
11651 proc.ali, proc.o
11652 pack.ali, pack.o
11653 @end group
11654 @group
11655 ^/common/release^[COMMON.RELEASE]^
11656 proc.ali, proc.o
11657 pack.ali, pack.o
11658 @end group
11659 @end smallexample
11660 Here are the corresponding project files:
11661
11662 @smallexample @c projectfile
11663 @group
11664 project Debug is
11665 for Object_Dir use "debug";
11666 for Main use ("proc");
11667
11668 package Builder is
11669 for ^Default_Switches^Default_Switches^ ("Ada")
11670 use ("^-g^-g^");
11671 for Executable ("proc.adb") use "proc1";
11672 end Builder;
11673 @end group
11674
11675 @group
11676 package Compiler is
11677 for ^Default_Switches^Default_Switches^ ("Ada")
11678 use ("-fstack-check",
11679 "^-gnata^-gnata^",
11680 "^-gnato^-gnato^",
11681 "^-gnatE^-gnatE^");
11682 end Compiler;
11683 end Debug;
11684 @end group
11685 @end smallexample
11686
11687 @smallexample @c projectfile
11688 @group
11689 project Release is
11690 for Object_Dir use "release";
11691 for Exec_Dir use ".";
11692 for Main use ("proc");
11693
11694 package Compiler is
11695 for ^Default_Switches^Default_Switches^ ("Ada")
11696 use ("^-O2^-O2^");
11697 end Compiler;
11698 end Release;
11699 @end group
11700 @end smallexample
11701
11702 @noindent
11703 The name of the project defined by @file{debug.gpr} is @code{"Debug"} (case
11704 insensitive), and analogously the project defined by @file{release.gpr} is
11705 @code{"Release"}. For consistency the file should have the same name as the
11706 project, and the project file's extension should be @code{"gpr"}. These
11707 conventions are not required, but a warning is issued if they are not followed.
11708
11709 If the current directory is @file{^/temp^[TEMP]^}, then the command
11710 @smallexample
11711 gnatmake ^-P/common/debug.gpr^/PROJECT_FILE=[COMMON]DEBUG^
11712 @end smallexample
11713
11714 @noindent
11715 generates object and ALI files in @file{^/common/debug^[COMMON.DEBUG]^},
11716 as well as the @code{^proc1^PROC1.EXE^} executable,
11717 using the ^switch^switch^ settings defined in the project file.
11718
11719 Likewise, the command
11720 @smallexample
11721 gnatmake ^-P/common/release.gpr^/PROJECT_FILE=[COMMON]RELEASE^
11722 @end smallexample
11723
11724 @noindent
11725 generates object and ALI files in @file{^/common/release^[COMMON.RELEASE]^},
11726 and the @code{^proc^PROC.EXE^}
11727 executable in @file{^/common^[COMMON]^},
11728 using the ^switch^switch^ settings from the project file.
11729
11730 @node Source Files
11731 @unnumberedsubsubsec Source Files
11732
11733 @noindent
11734 If a project file does not explicitly specify a set of source directories or
11735 a set of source files, then by default the project's source files are the
11736 Ada source files in the project file directory. Thus @file{pack.ads},
11737 @file{pack.adb}, and @file{proc.adb} are the source files for both projects.
11738
11739 @node Specifying the Object Directory
11740 @unnumberedsubsubsec Specifying the Object Directory
11741
11742 @noindent
11743 Several project properties are modeled by Ada-style @emph{attributes};
11744 a property is defined by supplying the equivalent of an Ada attribute
11745 definition clause in the project file.
11746 A project's object directory is another such a property; the corresponding
11747 attribute is @code{Object_Dir}, and its value is also a string expression,
11748 specified either as absolute or relative. In the later case,
11749 it is relative to the project file directory. Thus the compiler's
11750 output is directed to @file{^/common/debug^[COMMON.DEBUG]^}
11751 (for the @code{Debug} project)
11752 and to @file{^/common/release^[COMMON.RELEASE]^}
11753 (for the @code{Release} project).
11754 If @code{Object_Dir} is not specified, then the default is the project file
11755 directory itself.
11756
11757 @node Specifying the Exec Directory
11758 @unnumberedsubsubsec Specifying the Exec Directory
11759
11760 @noindent
11761 A project's exec directory is another property; the corresponding
11762 attribute is @code{Exec_Dir}, and its value is also a string expression,
11763 either specified as relative or absolute. If @code{Exec_Dir} is not specified,
11764 then the default is the object directory (which may also be the project file
11765 directory if attribute @code{Object_Dir} is not specified). Thus the executable
11766 is placed in @file{^/common/debug^[COMMON.DEBUG]^}
11767 for the @code{Debug} project (attribute @code{Exec_Dir} not specified)
11768 and in @file{^/common^[COMMON]^} for the @code{Release} project.
11769
11770 @node Project File Packages
11771 @unnumberedsubsubsec Project File Packages
11772
11773 @noindent
11774 A GNAT tool that is integrated with the Project Manager is modeled by a
11775 corresponding package in the project file. In the example above,
11776 The @code{Debug} project defines the packages @code{Builder}
11777 (for @command{gnatmake}) and @code{Compiler};
11778 the @code{Release} project defines only the @code{Compiler} package.
11779
11780 The Ada-like package syntax is not to be taken literally. Although packages in
11781 project files bear a surface resemblance to packages in Ada source code, the
11782 notation is simply a way to convey a grouping of properties for a named
11783 entity. Indeed, the package names permitted in project files are restricted
11784 to a predefined set, corresponding to the project-aware tools, and the contents
11785 of packages are limited to a small set of constructs.
11786 The packages in the example above contain attribute definitions.
11787
11788 @node Specifying ^Switch^Switch^ Settings
11789 @unnumberedsubsubsec Specifying ^Switch^Switch^ Settings
11790
11791 @noindent
11792 ^Switch^Switch^ settings for a project-aware tool can be specified through
11793 attributes in the package that corresponds to the tool.
11794 The example above illustrates one of the relevant attributes,
11795 @code{^Default_Switches^Default_Switches^}, which is defined in packages
11796 in both project files.
11797 Unlike simple attributes like @code{Source_Dirs},
11798 @code{^Default_Switches^Default_Switches^} is
11799 known as an @emph{associative array}. When you define this attribute, you must
11800 supply an ``index'' (a literal string), and the effect of the attribute
11801 definition is to set the value of the array at the specified index.
11802 For the @code{^Default_Switches^Default_Switches^} attribute,
11803 the index is a programming language (in our case, Ada),
11804 and the value specified (after @code{use}) must be a list
11805 of string expressions.
11806
11807 The attributes permitted in project files are restricted to a predefined set.
11808 Some may appear at project level, others in packages.
11809 For any attribute that is an associative array, the index must always be a
11810 literal string, but the restrictions on this string (e.g., a file name or a
11811 language name) depend on the individual attribute.
11812 Also depending on the attribute, its specified value will need to be either a
11813 string or a string list.
11814
11815 In the @code{Debug} project, we set the switches for two tools,
11816 @command{gnatmake} and the compiler, and thus we include the two corresponding
11817 packages; each package defines the @code{^Default_Switches^Default_Switches^}
11818 attribute with index @code{"Ada"}.
11819 Note that the package corresponding to
11820 @command{gnatmake} is named @code{Builder}. The @code{Release} project is
11821 similar, but only includes the @code{Compiler} package.
11822
11823 In project @code{Debug} above, the ^switches^switches^ starting with
11824 @option{-gnat} that are specified in package @code{Compiler}
11825 could have been placed in package @code{Builder}, since @command{gnatmake}
11826 transmits all such ^switches^switches^ to the compiler.
11827
11828 @node Main Subprograms
11829 @unnumberedsubsubsec Main Subprograms
11830
11831 @noindent
11832 One of the specifiable properties of a project is a list of files that contain
11833 main subprograms. This property is captured in the @code{Main} attribute,
11834 whose value is a list of strings. If a project defines the @code{Main}
11835 attribute, it is not necessary to identify the main subprogram(s) when
11836 invoking @command{gnatmake} (@pxref{gnatmake and Project Files}).
11837
11838 @node Executable File Names
11839 @unnumberedsubsubsec Executable File Names
11840
11841 @noindent
11842 By default, the executable file name corresponding to a main source is
11843 deduced from the main source file name. Through the attributes
11844 @code{Executable} and @code{Executable_Suffix} of package @code{Builder},
11845 it is possible to change this default.
11846 In project @code{Debug} above, the executable file name
11847 for main source @file{^proc.adb^PROC.ADB^} is
11848 @file{^proc1^PROC1.EXE^}.
11849 Attribute @code{Executable_Suffix}, when specified, may change the suffix
11850 of the executable files, when no attribute @code{Executable} applies:
11851 its value replace the platform-specific executable suffix.
11852 Attributes @code{Executable} and @code{Executable_Suffix} are the only ways to
11853 specify a non-default executable file name when several mains are built at once
11854 in a single @command{gnatmake} command.
11855
11856 @node Source File Naming Conventions
11857 @unnumberedsubsubsec Source File Naming Conventions
11858
11859 @noindent
11860 Since the project files above do not specify any source file naming
11861 conventions, the GNAT defaults are used. The mechanism for defining source
11862 file naming conventions -- a package named @code{Naming} --
11863 is described below (@pxref{Naming Schemes}).
11864
11865 @node Source Language(s)
11866 @unnumberedsubsubsec Source Language(s)
11867
11868 @noindent
11869 Since the project files do not specify a @code{Languages} attribute, by
11870 default the GNAT tools assume that the language of the project file is Ada.
11871 More generally, a project can comprise source files
11872 in Ada, C, and/or other languages.
11873
11874 @node Using External Variables
11875 @subsection Using External Variables
11876
11877 @noindent
11878 Instead of supplying different project files for debug and release, we can
11879 define a single project file that queries an external variable (set either
11880 on the command line or via an ^environment variable^logical name^) in order to
11881 conditionally define the appropriate settings. Again, assume that the
11882 source files @file{pack.ads}, @file{pack.adb}, and @file{proc.adb} are
11883 located in directory @file{^/common^[COMMON]^}. The following project file,
11884 @file{build.gpr}, queries the external variable named @code{STYLE} and
11885 defines an object directory and ^switch^switch^ settings based on whether
11886 the value is @code{"deb"} (debug) or @code{"rel"} (release), and where
11887 the default is @code{"deb"}.
11888
11889 @smallexample @c projectfile
11890 @group
11891 project Build is
11892 for Main use ("proc");
11893
11894 type Style_Type is ("deb", "rel");
11895 Style : Style_Type := external ("STYLE", "deb");
11896
11897 case Style is
11898 when "deb" =>
11899 for Object_Dir use "debug";
11900
11901 when "rel" =>
11902 for Object_Dir use "release";
11903 for Exec_Dir use ".";
11904 end case;
11905 @end group
11906
11907 @group
11908 package Builder is
11909
11910 case Style is
11911 when "deb" =>
11912 for ^Default_Switches^Default_Switches^ ("Ada")
11913 use ("^-g^-g^");
11914 for Executable ("proc") use "proc1";
11915 when others =>
11916 null;
11917 end case;
11918
11919 end Builder;
11920 @end group
11921
11922 @group
11923 package Compiler is
11924
11925 case Style is
11926 when "deb" =>
11927 for ^Default_Switches^Default_Switches^ ("Ada")
11928 use ("^-gnata^-gnata^",
11929 "^-gnato^-gnato^",
11930 "^-gnatE^-gnatE^");
11931
11932 when "rel" =>
11933 for ^Default_Switches^Default_Switches^ ("Ada")
11934 use ("^-O2^-O2^");
11935 end case;
11936
11937 end Compiler;
11938
11939 end Build;
11940 @end group
11941 @end smallexample
11942
11943 @noindent
11944 @code{Style_Type} is an example of a @emph{string type}, which is the project
11945 file analog of an Ada enumeration type but whose components are string literals
11946 rather than identifiers. @code{Style} is declared as a variable of this type.
11947
11948 The form @code{external("STYLE", "deb")} is known as an
11949 @emph{external reference}; its first argument is the name of an
11950 @emph{external variable}, and the second argument is a default value to be
11951 used if the external variable doesn't exist. You can define an external
11952 variable on the command line via the @option{^-X^/EXTERNAL_REFERENCE^} switch,
11953 or you can use ^an environment variable^a logical name^
11954 as an external variable.
11955
11956 Each @code{case} construct is expanded by the Project Manager based on the
11957 value of @code{Style}. Thus the command
11958 @ifclear vms
11959 @smallexample
11960 gnatmake -P/common/build.gpr -XSTYLE=deb
11961 @end smallexample
11962 @end ifclear
11963
11964 @ifset vms
11965 @smallexample
11966 gnatmake /PROJECT_FILE=[COMMON]BUILD.GPR /EXTERNAL_REFERENCE=STYLE=deb
11967 @end smallexample
11968 @end ifset
11969
11970 @noindent
11971 is equivalent to the @command{gnatmake} invocation using the project file
11972 @file{debug.gpr} in the earlier example. So is the command
11973 @smallexample
11974 gnatmake ^-P/common/build.gpr^/PROJECT_FILE=[COMMON]BUILD.GPR^
11975 @end smallexample
11976
11977 @noindent
11978 since @code{"deb"} is the default for @code{STYLE}.
11979
11980 Analogously,
11981
11982 @ifclear vms
11983 @smallexample
11984 gnatmake -P/common/build.gpr -XSTYLE=rel
11985 @end smallexample
11986 @end ifclear
11987
11988 @ifset vms
11989 @smallexample
11990 GNAT MAKE /PROJECT_FILE=[COMMON]BUILD.GPR /EXTERNAL_REFERENCE=STYLE=rel
11991 @end smallexample
11992 @end ifset
11993
11994 @noindent
11995 is equivalent to the @command{gnatmake} invocation using the project file
11996 @file{release.gpr} in the earlier example.
11997
11998 @node Importing Other Projects
11999 @subsection Importing Other Projects
12000 @cindex @code{ADA_PROJECT_PATH}
12001
12002 @noindent
12003 A compilation unit in a source file in one project may depend on compilation
12004 units in source files in other projects. To compile this unit under
12005 control of a project file, the
12006 dependent project must @emph{import} the projects containing the needed source
12007 files.
12008 This effect is obtained using syntax similar to an Ada @code{with} clause,
12009 but where @code{with}ed entities are strings that denote project files.
12010
12011 As an example, suppose that the two projects @code{GUI_Proj} and
12012 @code{Comm_Proj} are defined in the project files @file{gui_proj.gpr} and
12013 @file{comm_proj.gpr} in directories @file{^/gui^[GUI]^}
12014 and @file{^/comm^[COMM]^}, respectively.
12015 Suppose that the source files for @code{GUI_Proj} are
12016 @file{gui.ads} and @file{gui.adb}, and that the source files for
12017 @code{Comm_Proj} are @file{comm.ads} and @file{comm.adb}, where each set of
12018 files is located in its respective project file directory. Schematically:
12019
12020 @smallexample
12021 @group
12022 ^/gui^[GUI]^
12023 gui_proj.gpr
12024 gui.ads
12025 gui.adb
12026 @end group
12027
12028 @group
12029 ^/comm^[COMM]^
12030 comm_proj.gpr
12031 comm.ads
12032 comm.adb
12033 @end group
12034 @end smallexample
12035
12036 @noindent
12037 We want to develop an application in directory @file{^/app^[APP]^} that
12038 @code{with} the packages @code{GUI} and @code{Comm}, using the properties of
12039 the corresponding project files (e.g.@: the ^switch^switch^ settings
12040 and object directory).
12041 Skeletal code for a main procedure might be something like the following:
12042
12043 @smallexample @c ada
12044 @group
12045 with GUI, Comm;
12046 procedure App_Main is
12047 @dots{}
12048 begin
12049 @dots{}
12050 end App_Main;
12051 @end group
12052 @end smallexample
12053
12054 @noindent
12055 Here is a project file, @file{app_proj.gpr}, that achieves the desired
12056 effect:
12057
12058 @smallexample @c projectfile
12059 @group
12060 with "/gui/gui_proj", "/comm/comm_proj";
12061 project App_Proj is
12062 for Main use ("app_main");
12063 end App_Proj;
12064 @end group
12065 @end smallexample
12066
12067 @noindent
12068 Building an executable is achieved through the command:
12069 @smallexample
12070 gnatmake ^-P/app/app_proj^/PROJECT_FILE=[APP]APP_PROJ^
12071 @end smallexample
12072 @noindent
12073 which will generate the @code{^app_main^APP_MAIN.EXE^} executable
12074 in the directory where @file{app_proj.gpr} resides.
12075
12076 If an imported project file uses the standard extension (@code{^gpr^GPR^}) then
12077 (as illustrated above) the @code{with} clause can omit the extension.
12078
12079 Our example specified an absolute path for each imported project file.
12080 Alternatively, the directory name of an imported object can be omitted
12081 if either
12082 @itemize @bullet
12083 @item
12084 The imported project file is in the same directory as the importing project
12085 file, or
12086 @item
12087 You have defined ^an environment variable^a logical name^
12088 that includes the directory containing
12089 the needed project file. The syntax of @code{ADA_PROJECT_PATH} is the same as
12090 the syntax of @code{ADA_INCLUDE_PATH} and @code{ADA_OBJECTS_PATH}: a list of
12091 directory names separated by colons (semicolons on Windows).
12092 @end itemize
12093
12094 @noindent
12095 Thus, if we define @code{ADA_PROJECT_PATH} to include @file{^/gui^[GUI]^} and
12096 @file{^/comm^[COMM]^}, then our project file @file{app_proj.gpr} can be written
12097 as follows:
12098
12099 @smallexample @c projectfile
12100 @group
12101 with "gui_proj", "comm_proj";
12102 project App_Proj is
12103 for Main use ("app_main");
12104 end App_Proj;
12105 @end group
12106 @end smallexample
12107
12108 @noindent
12109 Importing other projects can create ambiguities.
12110 For example, the same unit might be present in different imported projects, or
12111 it might be present in both the importing project and in an imported project.
12112 Both of these conditions are errors. Note that in the current version of
12113 the Project Manager, it is illegal to have an ambiguous unit even if the
12114 unit is never referenced by the importing project. This restriction may be
12115 relaxed in a future release.
12116
12117 @node Extending a Project
12118 @subsection Extending a Project
12119
12120 @noindent
12121 In large software systems it is common to have multiple
12122 implementations of a common interface; in Ada terms, multiple versions of a
12123 package body for the same spec. For example, one implementation
12124 might be safe for use in tasking programs, while another might only be used
12125 in sequential applications. This can be modeled in GNAT using the concept
12126 of @emph{project extension}. If one project (the ``child'') @emph{extends}
12127 another project (the ``parent'') then by default all source files of the
12128 parent project are inherited by the child, but the child project can
12129 override any of the parent's source files with new versions, and can also
12130 add new files. This facility is the project analog of a type extension in
12131 Object-Oriented Programming. Project hierarchies are permitted (a child
12132 project may be the parent of yet another project), and a project that
12133 inherits one project can also import other projects.
12134
12135 As an example, suppose that directory @file{^/seq^[SEQ]^} contains the project
12136 file @file{seq_proj.gpr} as well as the source files @file{pack.ads},
12137 @file{pack.adb}, and @file{proc.adb}:
12138
12139 @smallexample
12140 @group
12141 ^/seq^[SEQ]^
12142 pack.ads
12143 pack.adb
12144 proc.adb
12145 seq_proj.gpr
12146 @end group
12147 @end smallexample
12148
12149 @noindent
12150 Note that the project file can simply be empty (that is, no attribute or
12151 package is defined):
12152
12153 @smallexample @c projectfile
12154 @group
12155 project Seq_Proj is
12156 end Seq_Proj;
12157 @end group
12158 @end smallexample
12159
12160 @noindent
12161 implying that its source files are all the Ada source files in the project
12162 directory.
12163
12164 Suppose we want to supply an alternate version of @file{pack.adb}, in
12165 directory @file{^/tasking^[TASKING]^}, but use the existing versions of
12166 @file{pack.ads} and @file{proc.adb}. We can define a project
12167 @code{Tasking_Proj} that inherits @code{Seq_Proj}:
12168
12169 @smallexample
12170 @group
12171 ^/tasking^[TASKING]^
12172 pack.adb
12173 tasking_proj.gpr
12174 @end group
12175
12176 @group
12177 project Tasking_Proj extends "/seq/seq_proj" is
12178 end Tasking_Proj;
12179 @end group
12180 @end smallexample
12181
12182 @noindent
12183 The version of @file{pack.adb} used in a build depends on which project file
12184 is specified.
12185
12186 Note that we could have obtained the desired behavior using project import
12187 rather than project inheritance; a @code{base} project would contain the
12188 sources for @file{pack.ads} and @file{proc.adb}, a sequential project would
12189 import @code{base} and add @file{pack.adb}, and likewise a tasking project
12190 would import @code{base} and add a different version of @file{pack.adb}. The
12191 choice depends on whether other sources in the original project need to be
12192 overridden. If they do, then project extension is necessary, otherwise,
12193 importing is sufficient.
12194
12195 @noindent
12196 In a project file that extends another project file, it is possible to
12197 indicate that an inherited source is not part of the sources of the extending
12198 project. This is necessary sometimes when a package spec has been overloaded
12199 and no longer requires a body: in this case, it is necessary to indicate that
12200 the inherited body is not part of the sources of the project, otherwise there
12201 will be a compilation error when compiling the spec.
12202
12203 For that purpose, the attribute @code{Excluded_Source_Files} is used.
12204 Its value is a string list: a list of file names. It is also possible to use
12205 attribute @code{Excluded_Source_List_File}. Its value is a single string:
12206 the file name of a text file containing a list of file names, one per line.
12207
12208 @smallexample @c @projectfile
12209 project B extends "a" is
12210 for Source_Files use ("pkg.ads");
12211 -- New spec of Pkg does not need a completion
12212 for Excluded_Source_Files use ("pkg.adb");
12213 end B;
12214 @end smallexample
12215
12216 Attribute @code{Excluded_Source_Files} may also be used to check if a source
12217 is still needed: if it is possible to build using @command{gnatmake} when such
12218 a source is put in attribute @code{Excluded_Source_Files} of a project P, then
12219 it is possible to remove the source completely from a system that includes
12220 project P.
12221
12222 @c ***********************
12223 @c * Project File Syntax *
12224 @c ***********************
12225
12226 @node Project File Syntax
12227 @section Project File Syntax
12228
12229 @menu
12230 * Basic Syntax::
12231 * Qualified Projects::
12232 * Packages::
12233 * Expressions::
12234 * String Types::
12235 * Variables::
12236 * Attributes::
12237 * Associative Array Attributes::
12238 * case Constructions::
12239 @end menu
12240
12241 @noindent
12242 This section describes the structure of project files.
12243
12244 A project may be an @emph{independent project}, entirely defined by a single
12245 project file. Any Ada source file in an independent project depends only
12246 on the predefined library and other Ada source files in the same project.
12247
12248 @noindent
12249 A project may also @dfn{depend on} other projects, in either or both of
12250 the following ways:
12251 @itemize @bullet
12252 @item It may import any number of projects
12253 @item It may extend at most one other project
12254 @end itemize
12255
12256 @noindent
12257 The dependence relation is a directed acyclic graph (the subgraph reflecting
12258 the ``extends'' relation is a tree).
12259
12260 A project's @dfn{immediate sources} are the source files directly defined by
12261 that project, either implicitly by residing in the project file's directory,
12262 or explicitly through any of the source-related attributes described below.
12263 More generally, a project @var{proj}'s @dfn{sources} are the immediate sources
12264 of @var{proj} together with the immediate sources (unless overridden) of any
12265 project on which @var{proj} depends (either directly or indirectly).
12266
12267 @node Basic Syntax
12268 @subsection Basic Syntax
12269
12270 @noindent
12271 As seen in the earlier examples, project files have an Ada-like syntax.
12272 The minimal project file is:
12273 @smallexample @c projectfile
12274 @group
12275 project Empty is
12276
12277 end Empty;
12278 @end group
12279 @end smallexample
12280
12281 @noindent
12282 The identifier @code{Empty} is the name of the project.
12283 This project name must be present after the reserved
12284 word @code{end} at the end of the project file, followed by a semi-colon.
12285
12286 Any name in a project file, such as the project name or a variable name,
12287 has the same syntax as an Ada identifier.
12288
12289 The reserved words of project files are the Ada 95 reserved words plus
12290 @code{extends}, @code{external}, and @code{project}. Note that the only Ada
12291 reserved words currently used in project file syntax are:
12292
12293 @itemize @bullet
12294 @item
12295 @code{all}
12296 @item
12297 @code{at}
12298 @item
12299 @code{case}
12300 @item
12301 @code{end}
12302 @item
12303 @code{for}
12304 @item
12305 @code{is}
12306 @item
12307 @code{limited}
12308 @item
12309 @code{null}
12310 @item
12311 @code{others}
12312 @item
12313 @code{package}
12314 @item
12315 @code{renames}
12316 @item
12317 @code{type}
12318 @item
12319 @code{use}
12320 @item
12321 @code{when}
12322 @item
12323 @code{with}
12324 @end itemize
12325
12326 @noindent
12327 Comments in project files have the same syntax as in Ada, two consecutive
12328 hyphens through the end of the line.
12329
12330 @node Qualified Projects
12331 @subsection Qualified Projects
12332
12333 @noindent
12334 Before the reserved @code{project}, there may be one or two "qualifiers", that
12335 is identifiers or other reserved words, to qualify the project.
12336
12337 The current list of qualifiers is:
12338
12339 @itemize @bullet
12340 @item
12341 @code{abstract}: qualify a project with no sources. An abstract project must
12342 have a declaration specifying that there are no sources in the project, and,
12343 if it extends another project, the project it extends must also be a qualified
12344 abstract project.
12345
12346 @item
12347 @code{standard}: a standard project is a non library project with sources.
12348
12349 @item
12350 @code{aggregate}: for future extension
12351
12352 @item
12353 @code{aggregate library}: for future extension
12354
12355 @item
12356 @code{library}: a library project must declare both attributes
12357 @code{Library_Name} and @code{Library_Dir}.
12358
12359 @item
12360 @code{configuration}: a configuration project cannot be in a project tree.
12361 @end itemize
12362
12363 @node Packages
12364 @subsection Packages
12365
12366 @noindent
12367 A project file may contain @emph{packages}. The name of a package must be one
12368 of the identifiers from the following list. A package
12369 with a given name may only appear once in a project file. Package names are
12370 case insensitive. The following package names are legal:
12371
12372 @itemize @bullet
12373 @item
12374 @code{Naming}
12375 @item
12376 @code{Builder}
12377 @item
12378 @code{Compiler}
12379 @item
12380 @code{Binder}
12381 @item
12382 @code{Linker}
12383 @item
12384 @code{Finder}
12385 @item
12386 @code{Cross_Reference}
12387 @item
12388 @code{Eliminate}
12389 @item
12390 @code{Pretty_Printer}
12391 @item
12392 @code{Metrics}
12393 @item
12394 @code{gnatls}
12395 @item
12396 @code{gnatstub}
12397 @item
12398 @code{IDE}
12399 @item
12400 @code{Language_Processing}
12401 @end itemize
12402
12403 @noindent
12404 In its simplest form, a package may be empty:
12405
12406 @smallexample @c projectfile
12407 @group
12408 project Simple is
12409 package Builder is
12410 end Builder;
12411 end Simple;
12412 @end group
12413 @end smallexample
12414
12415 @noindent
12416 A package may contain @emph{attribute declarations},
12417 @emph{variable declarations} and @emph{case constructions}, as will be
12418 described below.
12419
12420 When there is ambiguity between a project name and a package name,
12421 the name always designates the project. To avoid possible confusion, it is
12422 always a good idea to avoid naming a project with one of the
12423 names allowed for packages or any name that starts with @code{gnat}.
12424
12425 @node Expressions
12426 @subsection Expressions
12427
12428 @noindent
12429 An @emph{expression} is either a @emph{string expression} or a
12430 @emph{string list expression}.
12431
12432 A @emph{string expression} is either a @emph{simple string expression} or a
12433 @emph{compound string expression}.
12434
12435 A @emph{simple string expression} is one of the following:
12436 @itemize @bullet
12437 @item A literal string; e.g.@: @code{"comm/my_proj.gpr"}
12438 @item A string-valued variable reference (@pxref{Variables})
12439 @item A string-valued attribute reference (@pxref{Attributes})
12440 @item An external reference (@pxref{External References in Project Files})
12441 @end itemize
12442
12443 @noindent
12444 A @emph{compound string expression} is a concatenation of string expressions,
12445 using the operator @code{"&"}
12446 @smallexample
12447 Path & "/" & File_Name & ".ads"
12448 @end smallexample
12449
12450 @noindent
12451 A @emph{string list expression} is either a
12452 @emph{simple string list expression} or a
12453 @emph{compound string list expression}.
12454
12455 A @emph{simple string list expression} is one of the following:
12456 @itemize @bullet
12457 @item A parenthesized list of zero or more string expressions,
12458 separated by commas
12459 @smallexample
12460 File_Names := (File_Name, "gnat.adc", File_Name & ".orig");
12461 Empty_List := ();
12462 @end smallexample
12463 @item A string list-valued variable reference
12464 @item A string list-valued attribute reference
12465 @end itemize
12466
12467 @noindent
12468 A @emph{compound string list expression} is the concatenation (using
12469 @code{"&"}) of a simple string list expression and an expression. Note that
12470 each term in a compound string list expression, except the first, may be
12471 either a string expression or a string list expression.
12472
12473 @smallexample @c projectfile
12474 @group
12475 File_Name_List := () & File_Name; -- One string in this list
12476 Extended_File_Name_List := File_Name_List & (File_Name & ".orig");
12477 -- Two strings
12478 Big_List := File_Name_List & Extended_File_Name_List;
12479 -- Concatenation of two string lists: three strings
12480 Illegal_List := "gnat.adc" & Extended_File_Name_List;
12481 -- Illegal: must start with a string list
12482 @end group
12483 @end smallexample
12484
12485 @node String Types
12486 @subsection String Types
12487
12488 @noindent
12489 A @emph{string type declaration} introduces a discrete set of string literals.
12490 If a string variable is declared to have this type, its value
12491 is restricted to the given set of literals.
12492
12493 Here is an example of a string type declaration:
12494
12495 @smallexample @c projectfile
12496 type OS is ("NT", "nt", "Unix", "GNU/Linux", "other OS");
12497 @end smallexample
12498
12499 @noindent
12500 Variables of a string type are called @emph{typed variables}; all other
12501 variables are called @emph{untyped variables}. Typed variables are
12502 particularly useful in @code{case} constructions, to support conditional
12503 attribute declarations.
12504 (@pxref{case Constructions}).
12505
12506 The string literals in the list are case sensitive and must all be different.
12507 They may include any graphic characters allowed in Ada, including spaces.
12508
12509 A string type may only be declared at the project level, not inside a package.
12510
12511 A string type may be referenced by its name if it has been declared in the same
12512 project file, or by an expanded name whose prefix is the name of the project
12513 in which it is declared.
12514
12515 @node Variables
12516 @subsection Variables
12517
12518 @noindent
12519 A variable may be declared at the project file level, or within a package.
12520 Here are some examples of variable declarations:
12521
12522 @smallexample @c projectfile
12523 @group
12524 This_OS : OS := external ("OS"); -- a typed variable declaration
12525 That_OS := "GNU/Linux"; -- an untyped variable declaration
12526 @end group
12527 @end smallexample
12528
12529 @noindent
12530 The syntax of a @emph{typed variable declaration} is identical to the Ada
12531 syntax for an object declaration. By contrast, the syntax of an untyped
12532 variable declaration is identical to an Ada assignment statement. In fact,
12533 variable declarations in project files have some of the characteristics of
12534 an assignment, in that successive declarations for the same variable are
12535 allowed. Untyped variable declarations do establish the expected kind of the
12536 variable (string or string list), and successive declarations for it must
12537 respect the initial kind.
12538
12539 @noindent
12540 A string variable declaration (typed or untyped) declares a variable
12541 whose value is a string. This variable may be used as a string expression.
12542 @smallexample @c projectfile
12543 File_Name := "readme.txt";
12544 Saved_File_Name := File_Name & ".saved";
12545 @end smallexample
12546
12547 @noindent
12548 A string list variable declaration declares a variable whose value is a list
12549 of strings. The list may contain any number (zero or more) of strings.
12550
12551 @smallexample @c projectfile
12552 Empty_List := ();
12553 List_With_One_Element := ("^-gnaty^-gnaty^");
12554 List_With_Two_Elements := List_With_One_Element & "^-gnatg^-gnatg^";
12555 Long_List := ("main.ada", "pack1_.ada", "pack1.ada", "pack2_.ada"
12556 "pack2.ada", "util_.ada", "util.ada");
12557 @end smallexample
12558
12559 @noindent
12560 The same typed variable may not be declared more than once at project level,
12561 and it may not be declared more than once in any package; it is in effect
12562 a constant.
12563
12564 The same untyped variable may be declared several times. Declarations are
12565 elaborated in the order in which they appear, so the new value replaces
12566 the old one, and any subsequent reference to the variable uses the new value.
12567 However, as noted above, if a variable has been declared as a string, all
12568 subsequent
12569 declarations must give it a string value. Similarly, if a variable has
12570 been declared as a string list, all subsequent declarations
12571 must give it a string list value.
12572
12573 A @emph{variable reference} may take several forms:
12574
12575 @itemize @bullet
12576 @item The simple variable name, for a variable in the current package (if any)
12577 or in the current project
12578 @item An expanded name, whose prefix is a context name.
12579 @end itemize
12580
12581 @noindent
12582 A @emph{context} may be one of the following:
12583
12584 @itemize @bullet
12585 @item The name of an existing package in the current project
12586 @item The name of an imported project of the current project
12587 @item The name of an ancestor project (i.e., a project extended by the current
12588 project, either directly or indirectly)
12589 @item An expanded name whose prefix is an imported/parent project name, and
12590 whose selector is a package name in that project.
12591 @end itemize
12592
12593 @noindent
12594 A variable reference may be used in an expression.
12595
12596 @node Attributes
12597 @subsection Attributes
12598
12599 @noindent
12600 A project (and its packages) may have @emph{attributes} that define
12601 the project's properties. Some attributes have values that are strings;
12602 others have values that are string lists.
12603
12604 There are two categories of attributes: @emph{simple attributes}
12605 and @emph{associative arrays} (@pxref{Associative Array Attributes}).
12606
12607 Legal project attribute names, and attribute names for each legal package are
12608 listed below. Attributes names are case-insensitive.
12609
12610 The following attributes are defined on projects (all are simple attributes):
12611
12612 @multitable @columnfractions .4 .3
12613 @item @emph{Attribute Name}
12614 @tab @emph{Value}
12615 @item @code{Source_Files}
12616 @tab string list
12617 @item @code{Source_Dirs}
12618 @tab string list
12619 @item @code{Source_List_File}
12620 @tab string
12621 @item @code{Object_Dir}
12622 @tab string
12623 @item @code{Exec_Dir}
12624 @tab string
12625 @item @code{Excluded_Source_Dirs}
12626 @tab string list
12627 @item @code{Excluded_Source_Files}
12628 @tab string list
12629 @item @code{Excluded_Source_List_File}
12630 @tab string
12631 @item @code{Languages}
12632 @tab string list
12633 @item @code{Main}
12634 @tab string list
12635 @item @code{Library_Dir}
12636 @tab string
12637 @item @code{Library_Name}
12638 @tab string
12639 @item @code{Library_Kind}
12640 @tab string
12641 @item @code{Library_Version}
12642 @tab string
12643 @item @code{Library_Interface}
12644 @tab string
12645 @item @code{Library_Auto_Init}
12646 @tab string
12647 @item @code{Library_Options}
12648 @tab string list
12649 @item @code{Library_Src_Dir}
12650 @tab string
12651 @item @code{Library_ALI_Dir}
12652 @tab string
12653 @item @code{Library_GCC}
12654 @tab string
12655 @item @code{Library_Symbol_File}
12656 @tab string
12657 @item @code{Library_Symbol_Policy}
12658 @tab string
12659 @item @code{Library_Reference_Symbol_File}
12660 @tab string
12661 @item @code{Externally_Built}
12662 @tab string
12663 @end multitable
12664
12665 @noindent
12666 The following attributes are defined for package @code{Naming}
12667 (@pxref{Naming Schemes}):
12668
12669 @multitable @columnfractions .4 .2 .2 .2
12670 @item Attribute Name @tab Category @tab Index @tab Value
12671 @item @code{Spec_Suffix}
12672 @tab associative array
12673 @tab language name
12674 @tab string
12675 @item @code{Body_Suffix}
12676 @tab associative array
12677 @tab language name
12678 @tab string
12679 @item @code{Separate_Suffix}
12680 @tab simple attribute
12681 @tab n/a
12682 @tab string
12683 @item @code{Casing}
12684 @tab simple attribute
12685 @tab n/a
12686 @tab string
12687 @item @code{Dot_Replacement}
12688 @tab simple attribute
12689 @tab n/a
12690 @tab string
12691 @item @code{Spec}
12692 @tab associative array
12693 @tab Ada unit name
12694 @tab string
12695 @item @code{Body}
12696 @tab associative array
12697 @tab Ada unit name
12698 @tab string
12699 @item @code{Specification_Exceptions}
12700 @tab associative array
12701 @tab language name
12702 @tab string list
12703 @item @code{Implementation_Exceptions}
12704 @tab associative array
12705 @tab language name
12706 @tab string list
12707 @end multitable
12708
12709 @noindent
12710 The following attributes are defined for packages @code{Builder},
12711 @code{Compiler}, @code{Binder},
12712 @code{Linker}, @code{Cross_Reference}, and @code{Finder}
12713 (@pxref{^Switches^Switches^ and Project Files}).
12714
12715 @multitable @columnfractions .4 .2 .2 .2
12716 @item Attribute Name @tab Category @tab Index @tab Value
12717 @item @code{^Default_Switches^Default_Switches^}
12718 @tab associative array
12719 @tab language name
12720 @tab string list
12721 @item @code{^Switches^Switches^}
12722 @tab associative array
12723 @tab file name
12724 @tab string list
12725 @end multitable
12726
12727 @noindent
12728 In addition, package @code{Compiler} has a single string attribute
12729 @code{Local_Configuration_Pragmas} and package @code{Builder} has a single
12730 string attribute @code{Global_Configuration_Pragmas}.
12731
12732 @noindent
12733 Each simple attribute has a default value: the empty string (for string-valued
12734 attributes) and the empty list (for string list-valued attributes).
12735
12736 An attribute declaration defines a new value for an attribute.
12737
12738 Examples of simple attribute declarations:
12739
12740 @smallexample @c projectfile
12741 for Object_Dir use "objects";
12742 for Source_Dirs use ("units", "test/drivers");
12743 @end smallexample
12744
12745 @noindent
12746 The syntax of a @dfn{simple attribute declaration} is similar to that of an
12747 attribute definition clause in Ada.
12748
12749 Attributes references may be appear in expressions.
12750 The general form for such a reference is @code{<entity>'<attribute>}:
12751 Associative array attributes are functions. Associative
12752 array attribute references must have an argument that is a string literal.
12753
12754 Examples are:
12755
12756 @smallexample @c projectfile
12757 project'Object_Dir
12758 Naming'Dot_Replacement
12759 Imported_Project'Source_Dirs
12760 Imported_Project.Naming'Casing
12761 Builder'^Default_Switches^Default_Switches^("Ada")
12762 @end smallexample
12763
12764 @noindent
12765 The prefix of an attribute may be:
12766 @itemize @bullet
12767 @item @code{project} for an attribute of the current project
12768 @item The name of an existing package of the current project
12769 @item The name of an imported project
12770 @item The name of a parent project that is extended by the current project
12771 @item An expanded name whose prefix is imported/parent project name,
12772 and whose selector is a package name
12773 @end itemize
12774
12775 @noindent
12776 Example:
12777 @smallexample @c projectfile
12778 @group
12779 project Prj is
12780 for Source_Dirs use project'Source_Dirs & "units";
12781 for Source_Dirs use project'Source_Dirs & "test/drivers"
12782 end Prj;
12783 @end group
12784 @end smallexample
12785
12786 @noindent
12787 In the first attribute declaration, initially the attribute @code{Source_Dirs}
12788 has the default value: an empty string list. After this declaration,
12789 @code{Source_Dirs} is a string list of one element: @code{"units"}.
12790 After the second attribute declaration @code{Source_Dirs} is a string list of
12791 two elements: @code{"units"} and @code{"test/drivers"}.
12792
12793 Note: this example is for illustration only. In practice,
12794 the project file would contain only one attribute declaration:
12795
12796 @smallexample @c projectfile
12797 for Source_Dirs use ("units", "test/drivers");
12798 @end smallexample
12799
12800 @node Associative Array Attributes
12801 @subsection Associative Array Attributes
12802
12803 @noindent
12804 Some attributes are defined as @emph{associative arrays}. An associative
12805 array may be regarded as a function that takes a string as a parameter
12806 and delivers a string or string list value as its result.
12807
12808 Here are some examples of single associative array attribute associations:
12809
12810 @smallexample @c projectfile
12811 for Body ("main") use "Main.ada";
12812 for ^Switches^Switches^ ("main.ada")
12813 use ("^-v^-v^",
12814 "^-gnatv^-gnatv^");
12815 for ^Switches^Switches^ ("main.ada")
12816 use Builder'^Switches^Switches^ ("main.ada")
12817 & "^-g^-g^";
12818 @end smallexample
12819
12820 @noindent
12821 Like untyped variables and simple attributes, associative array attributes
12822 may be declared several times. Each declaration supplies a new value for the
12823 attribute, and replaces the previous setting.
12824
12825 @noindent
12826 An associative array attribute may be declared as a full associative array
12827 declaration, with the value of the same attribute in an imported or extended
12828 project.
12829
12830 @smallexample @c projectfile
12831 package Builder is
12832 for Default_Switches use Default.Builder'Default_Switches;
12833 end Builder;
12834 @end smallexample
12835
12836 @noindent
12837 In this example, @code{Default} must be either a project imported by the
12838 current project, or the project that the current project extends. If the
12839 attribute is in a package (in this case, in package @code{Builder}), the same
12840 package needs to be specified.
12841
12842 @noindent
12843 A full associative array declaration replaces any other declaration for the
12844 attribute, including other full associative array declaration. Single
12845 associative array associations may be declare after a full associative
12846 declaration, modifying the value for a single association of the attribute.
12847
12848 @node case Constructions
12849 @subsection @code{case} Constructions
12850
12851 @noindent
12852 A @code{case} construction is used in a project file to effect conditional
12853 behavior.
12854 Here is a typical example:
12855
12856 @smallexample @c projectfile
12857 @group
12858 project MyProj is
12859 type OS_Type is ("GNU/Linux", "Unix", "NT", "VMS");
12860
12861 OS : OS_Type := external ("OS", "GNU/Linux");
12862 @end group
12863
12864 @group
12865 package Compiler is
12866 case OS is
12867 when "GNU/Linux" | "Unix" =>
12868 for ^Default_Switches^Default_Switches^ ("Ada")
12869 use ("^-gnath^-gnath^");
12870 when "NT" =>
12871 for ^Default_Switches^Default_Switches^ ("Ada")
12872 use ("^-gnatP^-gnatP^");
12873 when others =>
12874 end case;
12875 end Compiler;
12876 end MyProj;
12877 @end group
12878 @end smallexample
12879
12880 @noindent
12881 The syntax of a @code{case} construction is based on the Ada case statement
12882 (although there is no @code{null} construction for empty alternatives).
12883
12884 The case expression must be a typed string variable.
12885 Each alternative comprises the reserved word @code{when}, either a list of
12886 literal strings separated by the @code{"|"} character or the reserved word
12887 @code{others}, and the @code{"=>"} token.
12888 Each literal string must belong to the string type that is the type of the
12889 case variable.
12890 An @code{others} alternative, if present, must occur last.
12891
12892 After each @code{=>}, there are zero or more constructions. The only
12893 constructions allowed in a case construction are other case constructions,
12894 attribute declarations and variable declarations. String type declarations and
12895 package declarations are not allowed. Variable declarations are restricted to
12896 variables that have already been declared before the case construction.
12897
12898 The value of the case variable is often given by an external reference
12899 (@pxref{External References in Project Files}).
12900
12901 @c ****************************************
12902 @c * Objects and Sources in Project Files *
12903 @c ****************************************
12904
12905 @node Objects and Sources in Project Files
12906 @section Objects and Sources in Project Files
12907
12908 @menu
12909 * Object Directory::
12910 * Exec Directory::
12911 * Source Directories::
12912 * Source File Names::
12913 @end menu
12914
12915 @noindent
12916 Each project has exactly one object directory and one or more source
12917 directories. The source directories must contain at least one source file,
12918 unless the project file explicitly specifies that no source files are present
12919 (@pxref{Source File Names}).
12920
12921 @node Object Directory
12922 @subsection Object Directory
12923
12924 @noindent
12925 The object directory for a project is the directory containing the compiler's
12926 output (such as @file{ALI} files and object files) for the project's immediate
12927 sources.
12928
12929 The object directory is given by the value of the attribute @code{Object_Dir}
12930 in the project file.
12931
12932 @smallexample @c projectfile
12933 for Object_Dir use "objects";
12934 @end smallexample
12935
12936 @noindent
12937 The attribute @code{Object_Dir} has a string value, the path name of the object
12938 directory. The path name may be absolute or relative to the directory of the
12939 project file. This directory must already exist, and be readable and writable.
12940
12941 By default, when the attribute @code{Object_Dir} is not given an explicit value
12942 or when its value is the empty string, the object directory is the same as the
12943 directory containing the project file.
12944
12945 @node Exec Directory
12946 @subsection Exec Directory
12947
12948 @noindent
12949 The exec directory for a project is the directory containing the executables
12950 for the project's main subprograms.
12951
12952 The exec directory is given by the value of the attribute @code{Exec_Dir}
12953 in the project file.
12954
12955 @smallexample @c projectfile
12956 for Exec_Dir use "executables";
12957 @end smallexample
12958
12959 @noindent
12960 The attribute @code{Exec_Dir} has a string value, the path name of the exec
12961 directory. The path name may be absolute or relative to the directory of the
12962 project file. This directory must already exist, and be writable.
12963
12964 By default, when the attribute @code{Exec_Dir} is not given an explicit value
12965 or when its value is the empty string, the exec directory is the same as the
12966 object directory of the project file.
12967
12968 @node Source Directories
12969 @subsection Source Directories
12970
12971 @noindent
12972 The source directories of a project are specified by the project file
12973 attribute @code{Source_Dirs}.
12974
12975 This attribute's value is a string list. If the attribute is not given an
12976 explicit value, then there is only one source directory, the one where the
12977 project file resides.
12978
12979 A @code{Source_Dirs} attribute that is explicitly defined to be the empty list,
12980 as in
12981
12982 @smallexample @c projectfile
12983 for Source_Dirs use ();
12984 @end smallexample
12985
12986 @noindent
12987 indicates that the project contains no source files.
12988
12989 Otherwise, each string in the string list designates one or more
12990 source directories.
12991
12992 @smallexample @c projectfile
12993 for Source_Dirs use ("sources", "test/drivers");
12994 @end smallexample
12995
12996 @noindent
12997 If a string in the list ends with @code{"/**"}, then the directory whose path
12998 name precedes the two asterisks, as well as all its subdirectories
12999 (recursively), are source directories.
13000
13001 @smallexample @c projectfile
13002 for Source_Dirs use ("/system/sources/**");
13003 @end smallexample
13004
13005 @noindent
13006 Here the directory @code{/system/sources} and all of its subdirectories
13007 (recursively) are source directories.
13008
13009 To specify that the source directories are the directory of the project file
13010 and all of its subdirectories, you can declare @code{Source_Dirs} as follows:
13011 @smallexample @c projectfile
13012 for Source_Dirs use ("./**");
13013 @end smallexample
13014
13015 @noindent
13016 Each of the source directories must exist and be readable.
13017
13018 @node Source File Names
13019 @subsection Source File Names
13020
13021 @noindent
13022 In a project that contains source files, their names may be specified by the
13023 attributes @code{Source_Files} (a string list) or @code{Source_List_File}
13024 (a string). Source file names never include any directory information.
13025
13026 If the attribute @code{Source_Files} is given an explicit value, then each
13027 element of the list is a source file name.
13028
13029 @smallexample @c projectfile
13030 for Source_Files use ("main.adb");
13031 for Source_Files use ("main.adb", "pack1.ads", "pack2.adb");
13032 @end smallexample
13033
13034 @noindent
13035 If the attribute @code{Source_Files} is not given an explicit value,
13036 but the attribute @code{Source_List_File} is given a string value,
13037 then the source file names are contained in the text file whose path name
13038 (absolute or relative to the directory of the project file) is the
13039 value of the attribute @code{Source_List_File}.
13040
13041 Each line in the file that is not empty or is not a comment
13042 contains a source file name.
13043
13044 @smallexample @c projectfile
13045 for Source_List_File use "source_list.txt";
13046 @end smallexample
13047
13048 @noindent
13049 By default, if neither the attribute @code{Source_Files} nor the attribute
13050 @code{Source_List_File} is given an explicit value, then each file in the
13051 source directories that conforms to the project's naming scheme
13052 (@pxref{Naming Schemes}) is an immediate source of the project.
13053
13054 A warning is issued if both attributes @code{Source_Files} and
13055 @code{Source_List_File} are given explicit values. In this case, the attribute
13056 @code{Source_Files} prevails.
13057
13058 Each source file name must be the name of one existing source file
13059 in one of the source directories.
13060
13061 A @code{Source_Files} attribute whose value is an empty list
13062 indicates that there are no source files in the project.
13063
13064 If the order of the source directories is known statically, that is if
13065 @code{"/**"} is not used in the string list @code{Source_Dirs}, then there may
13066 be several files with the same source file name. In this case, only the file
13067 in the first directory is considered as an immediate source of the project
13068 file. If the order of the source directories is not known statically, it is
13069 an error to have several files with the same source file name.
13070
13071 Projects can be specified to have no Ada source
13072 files: the value of (@code{Source_Dirs} or @code{Source_Files} may be an empty
13073 list, or the @code{"Ada"} may be absent from @code{Languages}:
13074
13075 @smallexample @c projectfile
13076 for Source_Dirs use ();
13077 for Source_Files use ();
13078 for Languages use ("C", "C++");
13079 @end smallexample
13080
13081 @noindent
13082 Otherwise, a project must contain at least one immediate source.
13083
13084 Projects with no source files are useful as template packages
13085 (@pxref{Packages in Project Files}) for other projects; in particular to
13086 define a package @code{Naming} (@pxref{Naming Schemes}).
13087
13088 @c ****************************
13089 @c * Importing Projects *
13090 @c ****************************
13091
13092 @node Importing Projects
13093 @section Importing Projects
13094 @cindex @code{ADA_PROJECT_PATH}
13095
13096 @noindent
13097 An immediate source of a project P may depend on source files that
13098 are neither immediate sources of P nor in the predefined library.
13099 To get this effect, P must @emph{import} the projects that contain the needed
13100 source files.
13101
13102 @smallexample @c projectfile
13103 @group
13104 with "project1", "utilities.gpr";
13105 with "/namings/apex.gpr";
13106 project Main is
13107 @dots{}
13108 @end group
13109 @end smallexample
13110
13111 @noindent
13112 As can be seen in this example, the syntax for importing projects is similar
13113 to the syntax for importing compilation units in Ada. However, project files
13114 use literal strings instead of names, and the @code{with} clause identifies
13115 project files rather than packages.
13116
13117 Each literal string is the file name or path name (absolute or relative) of a
13118 project file. If a string corresponds to a file name, with no path or a
13119 relative path, then its location is determined by the @emph{project path}. The
13120 latter can be queried using @code{gnatls -v}. It contains:
13121
13122 @itemize @bullet
13123 @item
13124 In first position, the directory containing the current project file.
13125 @item
13126 In last position, the default project directory. This default project directory
13127 is part of the GNAT installation and is the standard place to install project
13128 files giving access to standard support libraries.
13129 @ifclear vms
13130 @ref{Installing a library}
13131 @end ifclear
13132
13133 @item
13134 In between, all the directories referenced in the
13135 ^environment variable^logical name^ @env{ADA_PROJECT_PATH} if it exists.
13136 @end itemize
13137
13138 @noindent
13139 If a relative pathname is used, as in
13140
13141 @smallexample @c projectfile
13142 with "tests/proj";
13143 @end smallexample
13144
13145 @noindent
13146 then the full path for the project is constructed by concatenating this
13147 relative path to those in the project path, in order, until a matching file is
13148 found. Any symbolic link will be fully resolved in the directory of the
13149 importing project file before the imported project file is examined.
13150
13151 If the @code{with}'ed project file name does not have an extension,
13152 the default is @file{^.gpr^.GPR^}. If a file with this extension is not found,
13153 then the file name as specified in the @code{with} clause (no extension) will
13154 be used. In the above example, if a file @code{project1.gpr} is found, then it
13155 will be used; otherwise, if a file @code{^project1^PROJECT1^} exists
13156 then it will be used; if neither file exists, this is an error.
13157
13158 A warning is issued if the name of the project file does not match the
13159 name of the project; this check is case insensitive.
13160
13161 Any source file that is an immediate source of the imported project can be
13162 used by the immediate sources of the importing project, transitively. Thus
13163 if @code{A} imports @code{B}, and @code{B} imports @code{C}, the immediate
13164 sources of @code{A} may depend on the immediate sources of @code{C}, even if
13165 @code{A} does not import @code{C} explicitly. However, this is not recommended,
13166 because if and when @code{B} ceases to import @code{C}, some sources in
13167 @code{A} will no longer compile.
13168
13169 A side effect of this capability is that normally cyclic dependencies are not
13170 permitted: if @code{A} imports @code{B} (directly or indirectly) then @code{B}
13171 is not allowed to import @code{A}. However, there are cases when cyclic
13172 dependencies would be beneficial. For these cases, another form of import
13173 between projects exists, the @code{limited with}: a project @code{A} that
13174 imports a project @code{B} with a straight @code{with} may also be imported,
13175 directly or indirectly, by @code{B} on the condition that imports from @code{B}
13176 to @code{A} include at least one @code{limited with}.
13177
13178 @smallexample @c 0projectfile
13179 with "../b/b.gpr";
13180 with "../c/c.gpr";
13181 project A is
13182 end A;
13183
13184 limited with "../a/a.gpr";
13185 project B is
13186 end B;
13187
13188 with "../d/d.gpr";
13189 project C is
13190 end C;
13191
13192 limited with "../a/a.gpr";
13193 project D is
13194 end D;
13195 @end smallexample
13196
13197 @noindent
13198 In the above legal example, there are two project cycles:
13199 @itemize @bullet
13200 @item A-> B-> A
13201 @item A -> C -> D -> A
13202 @end itemize
13203
13204 @noindent
13205 In each of these cycle there is one @code{limited with}: import of @code{A}
13206 from @code{B} and import of @code{A} from @code{D}.
13207
13208 The difference between straight @code{with} and @code{limited with} is that
13209 the name of a project imported with a @code{limited with} cannot be used in the
13210 project that imports it. In particular, its packages cannot be renamed and
13211 its variables cannot be referred to.
13212
13213 An exception to the above rules for @code{limited with} is that for the main
13214 project specified to @command{gnatmake} or to the @command{GNAT} driver a
13215 @code{limited with} is equivalent to a straight @code{with}. For example,
13216 in the example above, projects @code{B} and @code{D} could not be main
13217 projects for @command{gnatmake} or to the @command{GNAT} driver, because they
13218 each have a @code{limited with} that is the only one in a cycle of importing
13219 projects.
13220
13221 @c *********************
13222 @c * Project Extension *
13223 @c *********************
13224
13225 @node Project Extension
13226 @section Project Extension
13227
13228 @noindent
13229 During development of a large system, it is sometimes necessary to use
13230 modified versions of some of the source files, without changing the original
13231 sources. This can be achieved through the @emph{project extension} facility.
13232
13233 @smallexample @c projectfile
13234 project Modified_Utilities extends "/baseline/utilities.gpr" is @dots{}
13235 @end smallexample
13236
13237 @noindent
13238 A project extension declaration introduces an extending project
13239 (the @emph{child}) and a project being extended (the @emph{parent}).
13240
13241 By default, a child project inherits all the sources of its parent.
13242 However, inherited sources can be overridden: a unit in a parent is hidden
13243 by a unit of the same name in the child.
13244
13245 Inherited sources are considered to be sources (but not immediate sources)
13246 of the child project; see @ref{Project File Syntax}.
13247
13248 An inherited source file retains any switches specified in the parent project.
13249
13250 For example if the project @code{Utilities} contains the spec and the
13251 body of an Ada package @code{Util_IO}, then the project
13252 @code{Modified_Utilities} can contain a new body for package @code{Util_IO}.
13253 The original body of @code{Util_IO} will not be considered in program builds.
13254 However, the package spec will still be found in the project
13255 @code{Utilities}.
13256
13257 A child project can have only one parent, except when it is qualified as
13258 abstract. But it may import any number of other projects.
13259
13260 A project is not allowed to import directly or indirectly at the same time a
13261 child project and any of its ancestors.
13262
13263 @c *******************************
13264 @c * Project Hierarchy Extension *
13265 @c *******************************
13266
13267 @node Project Hierarchy Extension
13268 @section Project Hierarchy Extension
13269
13270 @noindent
13271 When extending a large system spanning multiple projects, it is often
13272 inconvenient to extend every project in the hierarchy that is impacted by a
13273 small change introduced. In such cases, it is possible to create a virtual
13274 extension of entire hierarchy using @code{extends all} relationship.
13275
13276 When the project is extended using @code{extends all} inheritance, all projects
13277 that are imported by it, both directly and indirectly, are considered virtually
13278 extended. That is, the Project Manager creates "virtual projects"
13279 that extend every project in the hierarchy; all these virtual projects have
13280 no sources of their own and have as object directory the object directory of
13281 the root of "extending all" project.
13282
13283 It is possible to explicitly extend one or more projects in the hierarchy
13284 in order to modify the sources. These extending projects must be imported by
13285 the "extending all" project, which will replace the corresponding virtual
13286 projects with the explicit ones.
13287
13288 When building such a project hierarchy extension, the Project Manager will
13289 ensure that both modified sources and sources in virtual extending projects
13290 that depend on them, are recompiled.
13291
13292 By means of example, consider the following hierarchy of projects.
13293
13294 @enumerate
13295 @item
13296 project A, containing package P1
13297 @item
13298 project B importing A and containing package P2 which depends on P1
13299 @item
13300 project C importing B and containing package P3 which depends on P2
13301 @end enumerate
13302
13303 @noindent
13304 We want to modify packages P1 and P3.
13305
13306 This project hierarchy will need to be extended as follows:
13307
13308 @enumerate
13309 @item
13310 Create project A1 that extends A, placing modified P1 there:
13311
13312 @smallexample @c 0projectfile
13313 project A1 extends "(@dots{})/A" is
13314 end A1;
13315 @end smallexample
13316
13317 @item
13318 Create project C1 that "extends all" C and imports A1, placing modified
13319 P3 there:
13320
13321 @smallexample @c 0projectfile
13322 with "(@dots{})/A1";
13323 project C1 extends all "(@dots{})/C" is
13324 end C1;
13325 @end smallexample
13326 @end enumerate
13327
13328 When you build project C1, your entire modified project space will be
13329 recompiled, including the virtual project B1 that has been impacted by the
13330 "extending all" inheritance of project C.
13331
13332 Note that if a Library Project in the hierarchy is virtually extended,
13333 the virtual project that extends the Library Project is not a Library Project.
13334
13335 @c ****************************************
13336 @c * External References in Project Files *
13337 @c ****************************************
13338
13339 @node External References in Project Files
13340 @section External References in Project Files
13341
13342 @noindent
13343 A project file may contain references to external variables; such references
13344 are called @emph{external references}.
13345
13346 An external variable is either defined as part of the environment (an
13347 environment variable in Unix, for example) or else specified on the command
13348 line via the @option{^-X^/EXTERNAL_REFERENCE=^@emph{vbl}=@emph{value}} switch.
13349 If both, then the command line value is used.
13350
13351 The value of an external reference is obtained by means of the built-in
13352 function @code{external}, which returns a string value.
13353 This function has two forms:
13354 @itemize @bullet
13355 @item @code{external (external_variable_name)}
13356 @item @code{external (external_variable_name, default_value)}
13357 @end itemize
13358
13359 @noindent
13360 Each parameter must be a string literal. For example:
13361
13362 @smallexample @c projectfile
13363 external ("USER")
13364 external ("OS", "GNU/Linux")
13365 @end smallexample
13366
13367 @noindent
13368 In the form with one parameter, the function returns the value of
13369 the external variable given as parameter. If this name is not present in the
13370 environment, the function returns an empty string.
13371
13372 In the form with two string parameters, the second argument is
13373 the value returned when the variable given as the first argument is not
13374 present in the environment. In the example above, if @code{"OS"} is not
13375 the name of ^an environment variable^a logical name^ and is not passed on
13376 the command line, then the returned value is @code{"GNU/Linux"}.
13377
13378 An external reference may be part of a string expression or of a string
13379 list expression, and can therefore appear in a variable declaration or
13380 an attribute declaration.
13381
13382 @smallexample @c projectfile
13383 @group
13384 type Mode_Type is ("Debug", "Release");
13385 Mode : Mode_Type := external ("MODE");
13386 case Mode is
13387 when "Debug" =>
13388 @dots{}
13389 @end group
13390 @end smallexample
13391
13392 @c *****************************
13393 @c * Packages in Project Files *
13394 @c *****************************
13395
13396 @node Packages in Project Files
13397 @section Packages in Project Files
13398
13399 @noindent
13400 A @emph{package} defines the settings for project-aware tools within a
13401 project.
13402 For each such tool one can declare a package; the names for these
13403 packages are preset (@pxref{Packages}).
13404 A package may contain variable declarations, attribute declarations, and case
13405 constructions.
13406
13407 @smallexample @c projectfile
13408 @group
13409 project Proj is
13410 package Builder is -- used by gnatmake
13411 for ^Default_Switches^Default_Switches^ ("Ada")
13412 use ("^-v^-v^",
13413 "^-g^-g^");
13414 end Builder;
13415 end Proj;
13416 @end group
13417 @end smallexample
13418
13419 @noindent
13420 The syntax of package declarations mimics that of package in Ada.
13421
13422 Most of the packages have an attribute
13423 @code{^Default_Switches^Default_Switches^}.
13424 This attribute is an associative array, and its value is a string list.
13425 The index of the associative array is the name of a programming language (case
13426 insensitive). This attribute indicates the ^switch^switch^
13427 or ^switches^switches^ to be used
13428 with the corresponding tool.
13429
13430 Some packages also have another attribute, @code{^Switches^Switches^},
13431 an associative array whose value is a string list.
13432 The index is the name of a source file.
13433 This attribute indicates the ^switch^switch^
13434 or ^switches^switches^ to be used by the corresponding
13435 tool when dealing with this specific file.
13436
13437 Further information on these ^switch^switch^-related attributes is found in
13438 @ref{^Switches^Switches^ and Project Files}.
13439
13440 A package may be declared as a @emph{renaming} of another package; e.g., from
13441 the project file for an imported project.
13442
13443 @smallexample @c projectfile
13444 @group
13445 with "/global/apex.gpr";
13446 project Example is
13447 package Naming renames Apex.Naming;
13448 @dots{}
13449 end Example;
13450 @end group
13451 @end smallexample
13452
13453 @noindent
13454 Packages that are renamed in other project files often come from project files
13455 that have no sources: they are just used as templates. Any modification in the
13456 template will be reflected automatically in all the project files that rename
13457 a package from the template.
13458
13459 In addition to the tool-oriented packages, you can also declare a package
13460 named @code{Naming} to establish specialized source file naming conventions
13461 (@pxref{Naming Schemes}).
13462
13463 @c ************************************
13464 @c * Variables from Imported Projects *
13465 @c ************************************
13466
13467 @node Variables from Imported Projects
13468 @section Variables from Imported Projects
13469
13470 @noindent
13471 An attribute or variable defined in an imported or parent project can
13472 be used in expressions in the importing / extending project.
13473 Such an attribute or variable is denoted by an expanded name whose prefix
13474 is either the name of the project or the expanded name of a package within
13475 a project.
13476
13477 @smallexample @c projectfile
13478 @group
13479 with "imported";
13480 project Main extends "base" is
13481 Var1 := Imported.Var;
13482 Var2 := Base.Var & ".new";
13483 @end group
13484
13485 @group
13486 package Builder is
13487 for ^Default_Switches^Default_Switches^ ("Ada")
13488 use Imported.Builder'Ada_^Switches^Switches^ &
13489 "^-gnatg^-gnatg^" &
13490 "^-v^-v^";
13491 end Builder;
13492 @end group
13493
13494 @group
13495 package Compiler is
13496 for ^Default_Switches^Default_Switches^ ("Ada")
13497 use Base.Compiler'Ada_^Switches^Switches^;
13498 end Compiler;
13499 end Main;
13500 @end group
13501 @end smallexample
13502
13503 @noindent
13504 In this example:
13505
13506 @itemize @bullet
13507 @item
13508 The value of @code{Var1} is a copy of the variable @code{Var} defined
13509 in the project file @file{"imported.gpr"}
13510 @item
13511 the value of @code{Var2} is a copy of the value of variable @code{Var}
13512 defined in the project file @file{base.gpr}, concatenated with @code{".new"}
13513 @item
13514 attribute @code{^Default_Switches^Default_Switches^ ("Ada")} in package
13515 @code{Builder} is a string list that includes in its value a copy of the value
13516 of @code{Ada_^Switches^Switches^} defined in the @code{Builder} package
13517 in project file @file{imported.gpr} plus two new elements:
13518 @option{"^-gnatg^-gnatg^"}
13519 and @option{"^-v^-v^"};
13520 @item
13521 attribute @code{^Default_Switches^Default_Switches^ ("Ada")} in package
13522 @code{Compiler} is a copy of the variable @code{Ada_^Switches^Switches^}
13523 defined in the @code{Compiler} package in project file @file{base.gpr},
13524 the project being extended.
13525 @end itemize
13526
13527 @c ******************
13528 @c * Naming Schemes *
13529 @c ******************
13530
13531 @node Naming Schemes
13532 @section Naming Schemes
13533
13534 @noindent
13535 Sometimes an Ada software system is ported from a foreign compilation
13536 environment to GNAT, and the file names do not use the default GNAT
13537 conventions. Instead of changing all the file names (which for a variety
13538 of reasons might not be possible), you can define the relevant file
13539 naming scheme in the @code{Naming} package in your project file.
13540
13541 @noindent
13542 Note that the use of pragmas described in
13543 @ref{Alternative File Naming Schemes} by mean of a configuration
13544 pragmas file is not supported when using project files. You must use
13545 the features described in this paragraph. You can however use specify
13546 other configuration pragmas (@pxref{Specifying Configuration Pragmas}).
13547
13548 @ifclear vms
13549 For example, the following
13550 package models the Apex file naming rules:
13551
13552 @smallexample @c projectfile
13553 @group
13554 package Naming is
13555 for Casing use "lowercase";
13556 for Dot_Replacement use ".";
13557 for Spec_Suffix ("Ada") use ".1.ada";
13558 for Body_Suffix ("Ada") use ".2.ada";
13559 end Naming;
13560 @end group
13561 @end smallexample
13562 @end ifclear
13563
13564 @ifset vms
13565 For example, the following package models the HP Ada file naming rules:
13566
13567 @smallexample @c projectfile
13568 @group
13569 package Naming is
13570 for Casing use "lowercase";
13571 for Dot_Replacement use "__";
13572 for Spec_Suffix ("Ada") use "_.^ada^ada^";
13573 for Body_Suffix ("Ada") use ".^ada^ada^";
13574 end Naming;
13575 @end group
13576 @end smallexample
13577
13578 @noindent
13579 (Note that @code{Casing} is @code{"lowercase"} because GNAT gets the file
13580 names in lower case)
13581 @end ifset
13582
13583 @noindent
13584 You can define the following attributes in package @code{Naming}:
13585
13586 @table @code
13587
13588 @item @code{Casing}
13589 This must be a string with one of the three values @code{"lowercase"},
13590 @code{"uppercase"} or @code{"mixedcase"}; these strings are case insensitive.
13591
13592 @noindent
13593 If @code{Casing} is not specified, then the default is @code{"lowercase"}.
13594
13595 @item @code{Dot_Replacement}
13596 This must be a string whose value satisfies the following conditions:
13597
13598 @itemize @bullet
13599 @item It must not be empty
13600 @item It cannot start or end with an alphanumeric character
13601 @item It cannot be a single underscore
13602 @item It cannot start with an underscore followed by an alphanumeric
13603 @item It cannot contain a dot @code{'.'} except if the entire string
13604 is @code{"."}
13605 @end itemize
13606
13607 @noindent
13608 If @code{Dot_Replacement} is not specified, then the default is @code{"-"}.
13609
13610 @item @code{Spec_Suffix}
13611 This is an associative array (indexed by the programming language name, case
13612 insensitive) whose value is a string that must satisfy the following
13613 conditions:
13614
13615 @itemize @bullet
13616 @item It must not be empty
13617 @item It must include at least one dot
13618 @end itemize
13619 @noindent
13620 If @code{Spec_Suffix ("Ada")} is not specified, then the default is
13621 @code{"^.ads^.ADS^"}.
13622
13623 @item @code{Body_Suffix}
13624 This is an associative array (indexed by the programming language name, case
13625 insensitive) whose value is a string that must satisfy the following
13626 conditions:
13627
13628 @itemize @bullet
13629 @item It must not be empty
13630 @item It must include at least one dot
13631 @item It cannot be the same as @code{Spec_Suffix ("Ada")}
13632 @end itemize
13633 @noindent
13634 If @code{Body_Suffix ("Ada")} and @code{Spec_Suffix ("Ada")} end with the
13635 same string, then a file name that ends with the longest of these two suffixes
13636 will be a body if the longest suffix is @code{Body_Suffix ("Ada")} or a spec
13637 if the longest suffix is @code{Spec_Suffix ("Ada")}.
13638
13639 If @code{Body_Suffix ("Ada")} is not specified, then the default is
13640 @code{"^.adb^.ADB^"}.
13641
13642 @item @code{Separate_Suffix}
13643 This must be a string whose value satisfies the same conditions as
13644 @code{Body_Suffix}. The same "longest suffix" rules apply.
13645
13646 @noindent
13647 If @code{Separate_Suffix ("Ada")} is not specified, then it defaults to same
13648 value as @code{Body_Suffix ("Ada")}.
13649
13650 @item @code{Spec}
13651 @noindent
13652 You can use the associative array attribute @code{Spec} to define
13653 the source file name for an individual Ada compilation unit's spec. The array
13654 index must be a string literal that identifies the Ada unit (case insensitive).
13655 The value of this attribute must be a string that identifies the file that
13656 contains this unit's spec (case sensitive or insensitive depending on the
13657 operating system).
13658
13659 @smallexample @c projectfile
13660 for Spec ("MyPack.MyChild") use "mypack.mychild.spec";
13661 @end smallexample
13662
13663 @item @code{Body}
13664
13665 You can use the associative array attribute @code{Body} to
13666 define the source file name for an individual Ada compilation unit's body
13667 (possibly a subunit). The array index must be a string literal that identifies
13668 the Ada unit (case insensitive). The value of this attribute must be a string
13669 that identifies the file that contains this unit's body or subunit (case
13670 sensitive or insensitive depending on the operating system).
13671
13672 @smallexample @c projectfile
13673 for Body ("MyPack.MyChild") use "mypack.mychild.body";
13674 @end smallexample
13675 @end table
13676
13677 @c ********************
13678 @c * Library Projects *
13679 @c ********************
13680
13681 @node Library Projects
13682 @section Library Projects
13683
13684 @noindent
13685 @emph{Library projects} are projects whose object code is placed in a library.
13686 (Note that this facility is not yet supported on all platforms)
13687
13688 To create a library project, you need to define in its project file
13689 two project-level attributes: @code{Library_Name} and @code{Library_Dir}.
13690 Additionally, you may define other library-related attributes such as
13691 @code{Library_Kind}, @code{Library_Version}, @code{Library_Interface},
13692 @code{Library_Auto_Init}, @code{Library_Options} and @code{Library_GCC}.
13693
13694 The @code{Library_Name} attribute has a string value. There is no restriction
13695 on the name of a library. It is the responsibility of the developer to
13696 choose a name that will be accepted by the platform. It is recommended to
13697 choose names that could be Ada identifiers; such names are almost guaranteed
13698 to be acceptable on all platforms.
13699
13700 The @code{Library_Dir} attribute has a string value that designates the path
13701 (absolute or relative) of the directory where the library will reside.
13702 It must designate an existing directory, and this directory must be writable,
13703 different from the project's object directory and from any source directory
13704 in the project tree.
13705
13706 If both @code{Library_Name} and @code{Library_Dir} are specified and
13707 are legal, then the project file defines a library project. The optional
13708 library-related attributes are checked only for such project files.
13709
13710 The @code{Library_Kind} attribute has a string value that must be one of the
13711 following (case insensitive): @code{"static"}, @code{"dynamic"} or
13712 @code{"relocatable"} (which is a synonym for @code{"dynamic"}). If this
13713 attribute is not specified, the library is a static library, that is
13714 an archive of object files that can be potentially linked into a
13715 static executable. Otherwise, the library may be dynamic or
13716 relocatable, that is a library that is loaded only at the start of execution.
13717
13718 If you need to build both a static and a dynamic library, you should use two
13719 different object directories, since in some cases some extra code needs to
13720 be generated for the latter. For such cases, it is recommended to either use
13721 two different project files, or a single one which uses external variables
13722 to indicate what kind of library should be build.
13723
13724 The @code{Library_ALI_Dir} attribute may be specified to indicate the
13725 directory where the ALI files of the library will be copied. When it is
13726 not specified, the ALI files are copied to the directory specified in
13727 attribute @code{Library_Dir}. The directory specified by @code{Library_ALI_Dir}
13728 must be writable and different from the project's object directory and from
13729 any source directory in the project tree.
13730
13731 The @code{Library_Version} attribute has a string value whose interpretation
13732 is platform dependent. It has no effect on VMS and Windows. On Unix, it is
13733 used only for dynamic/relocatable libraries as the internal name of the
13734 library (the @code{"soname"}). If the library file name (built from the
13735 @code{Library_Name}) is different from the @code{Library_Version}, then the
13736 library file will be a symbolic link to the actual file whose name will be
13737 @code{Library_Version}.
13738
13739 Example (on Unix):
13740
13741 @smallexample @c projectfile
13742 @group
13743 project Plib is
13744
13745 Version := "1";
13746
13747 for Library_Dir use "lib_dir";
13748 for Library_Name use "dummy";
13749 for Library_Kind use "relocatable";
13750 for Library_Version use "libdummy.so." & Version;
13751
13752 end Plib;
13753 @end group
13754 @end smallexample
13755
13756 @noindent
13757 Directory @file{lib_dir} will contain the internal library file whose name
13758 will be @file{libdummy.so.1}, and @file{libdummy.so} will be a symbolic link to
13759 @file{libdummy.so.1}.
13760
13761 When @command{gnatmake} detects that a project file
13762 is a library project file, it will check all immediate sources of the project
13763 and rebuild the library if any of the sources have been recompiled.
13764
13765 Standard project files can import library project files. In such cases,
13766 the libraries will only be rebuilt if some of its sources are recompiled
13767 because they are in the closure of some other source in an importing project.
13768 Sources of the library project files that are not in such a closure will
13769 not be checked, unless the full library is checked, because one of its sources
13770 needs to be recompiled.
13771
13772 For instance, assume the project file @code{A} imports the library project file
13773 @code{L}. The immediate sources of A are @file{a1.adb}, @file{a2.ads} and
13774 @file{a2.adb}. The immediate sources of L are @file{l1.ads}, @file{l1.adb},
13775 @file{l2.ads}, @file{l2.adb}.
13776
13777 If @file{l1.adb} has been modified, then the library associated with @code{L}
13778 will be rebuilt when compiling all the immediate sources of @code{A} only
13779 if @file{a1.ads}, @file{a2.ads} or @file{a2.adb} includes a statement
13780 @code{"with L1;"}.
13781
13782 To be sure that all the sources in the library associated with @code{L} are
13783 up to date, and that all the sources of project @code{A} are also up to date,
13784 the following two commands needs to be used:
13785
13786 @smallexample
13787 gnatmake -Pl.gpr
13788 gnatmake -Pa.gpr
13789 @end smallexample
13790
13791 When a library is built or rebuilt, an attempt is made first to delete all
13792 files in the library directory.
13793 All @file{ALI} files will also be copied from the object directory to the
13794 library directory. To build executables, @command{gnatmake} will use the
13795 library rather than the individual object files.
13796
13797 @ifclear vms
13798 It is also possible to create library project files for third-party libraries
13799 that are precompiled and cannot be compiled locally thanks to the
13800 @code{externally_built} attribute. (See @ref{Installing a library}).
13801 @end ifclear
13802
13803 @c *******************************
13804 @c * Stand-alone Library Projects *
13805 @c *******************************
13806
13807 @node Stand-alone Library Projects
13808 @section Stand-alone Library Projects
13809
13810 @noindent
13811 A Stand-alone Library is a library that contains the necessary code to
13812 elaborate the Ada units that are included in the library. A Stand-alone
13813 Library is suitable to be used in an executable when the main is not
13814 in Ada. However, Stand-alone Libraries may also be used with an Ada main
13815 subprogram.
13816
13817 A Stand-alone Library Project is a Library Project where the library is
13818 a Stand-alone Library.
13819
13820 To be a Stand-alone Library Project, in addition to the two attributes
13821 that make a project a Library Project (@code{Library_Name} and
13822 @code{Library_Dir}, see @ref{Library Projects}), the attribute
13823 @code{Library_Interface} must be defined.
13824
13825 @smallexample @c projectfile
13826 @group
13827 for Library_Dir use "lib_dir";
13828 for Library_Name use "dummy";
13829 for Library_Interface use ("int1", "int1.child");
13830 @end group
13831 @end smallexample
13832
13833 Attribute @code{Library_Interface} has a nonempty string list value,
13834 each string in the list designating a unit contained in an immediate source
13835 of the project file.
13836
13837 When a Stand-alone Library is built, first the binder is invoked to build
13838 a package whose name depends on the library name
13839 (^b~dummy.ads/b^B$DUMMY.ADS/B^ in the example above).
13840 This binder-generated package includes initialization and
13841 finalization procedures whose
13842 names depend on the library name (dummyinit and dummyfinal in the example
13843 above). The object corresponding to this package is included in the library.
13844
13845 A dynamic or relocatable Stand-alone Library is automatically initialized
13846 if automatic initialization of Stand-alone Libraries is supported on the
13847 platform and if attribute @code{Library_Auto_Init} is not specified or
13848 is specified with the value "true". A static Stand-alone Library is never
13849 automatically initialized.
13850
13851 Single string attribute @code{Library_Auto_Init} may be specified with only
13852 two possible values: "false" or "true" (case-insensitive). Specifying
13853 "false" for attribute @code{Library_Auto_Init} will prevent automatic
13854 initialization of dynamic or relocatable libraries.
13855
13856 When a non-automatically initialized Stand-alone Library is used
13857 in an executable, its initialization procedure must be called before
13858 any service of the library is used.
13859 When the main subprogram is in Ada, it may mean that the initialization
13860 procedure has to be called during elaboration of another package.
13861
13862 For a Stand-Alone Library, only the @file{ALI} files of the Interface Units
13863 (those that are listed in attribute @code{Library_Interface}) are copied to
13864 the Library Directory. As a consequence, only the Interface Units may be
13865 imported from Ada units outside of the library. If other units are imported,
13866 the binding phase will fail.
13867
13868 When a Stand-Alone Library is bound, the switches that are specified in
13869 the attribute @code{Default_Switches ("Ada")} in package @code{Binder} are
13870 used in the call to @command{gnatbind}.
13871
13872 The string list attribute @code{Library_Options} may be used to specified
13873 additional switches to the call to @command{gcc} to link the library.
13874
13875 The attribute @code{Library_Src_Dir}, may be specified for a
13876 Stand-Alone Library. @code{Library_Src_Dir} is a simple attribute that has a
13877 single string value. Its value must be the path (absolute or relative to the
13878 project directory) of an existing directory. This directory cannot be the
13879 object directory or one of the source directories, but it can be the same as
13880 the library directory. The sources of the Interface
13881 Units of the library, necessary to an Ada client of the library, will be
13882 copied to the designated directory, called Interface Copy directory.
13883 These sources includes the specs of the Interface Units, but they may also
13884 include bodies and subunits, when pragmas @code{Inline} or @code{Inline_Always}
13885 are used, or when there is a generic units in the spec. Before the sources
13886 are copied to the Interface Copy directory, an attempt is made to delete all
13887 files in the Interface Copy directory.
13888
13889 @c *************************************
13890 @c * Switches Related to Project Files *
13891 @c *************************************
13892 @node Switches Related to Project Files
13893 @section Switches Related to Project Files
13894
13895 @noindent
13896 The following switches are used by GNAT tools that support project files:
13897
13898 @table @option
13899
13900 @item ^-P^/PROJECT_FILE=^@var{project}
13901 @cindex @option{^-P^/PROJECT_FILE^} (any project-aware tool)
13902 Indicates the name of a project file. This project file will be parsed with
13903 the verbosity indicated by @option{^-vP^MESSAGE_PROJECT_FILES=^@emph{x}},
13904 if any, and using the external references indicated
13905 by @option{^-X^/EXTERNAL_REFERENCE^} switches, if any.
13906 @ifclear vms
13907 There may zero, one or more spaces between @option{-P} and @var{project}.
13908 @end ifclear
13909
13910 @noindent
13911 There must be only one @option{^-P^/PROJECT_FILE^} switch on the command line.
13912
13913 @noindent
13914 Since the Project Manager parses the project file only after all the switches
13915 on the command line are checked, the order of the switches
13916 @option{^-P^/PROJECT_FILE^},
13917 @option{^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}}
13918 or @option{^-X^/EXTERNAL_REFERENCE^} is not significant.
13919
13920 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
13921 @cindex @option{^-X^/EXTERNAL_REFERENCE^} (any project-aware tool)
13922 Indicates that external variable @var{name} has the value @var{value}.
13923 The Project Manager will use this value for occurrences of
13924 @code{external(name)} when parsing the project file.
13925
13926 @ifclear vms
13927 @noindent
13928 If @var{name} or @var{value} includes a space, then @var{name=value} should be
13929 put between quotes.
13930 @smallexample
13931 -XOS=NT
13932 -X"user=John Doe"
13933 @end smallexample
13934 @end ifclear
13935
13936 @noindent
13937 Several @option{^-X^/EXTERNAL_REFERENCE^} switches can be used simultaneously.
13938 If several @option{^-X^/EXTERNAL_REFERENCE^} switches specify the same
13939 @var{name}, only the last one is used.
13940
13941 @noindent
13942 An external variable specified with a @option{^-X^/EXTERNAL_REFERENCE^} switch
13943 takes precedence over the value of the same name in the environment.
13944
13945 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
13946 @cindex @option{^-vP^/MESSAGES_PROJECT_FILE^} (any project-aware tool)
13947 Indicates the verbosity of the parsing of GNAT project files.
13948
13949 @ifclear vms
13950 @option{-vP0} means Default;
13951 @option{-vP1} means Medium;
13952 @option{-vP2} means High.
13953 @end ifclear
13954
13955 @ifset vms
13956 There are three possible options for this qualifier: DEFAULT, MEDIUM and
13957 HIGH.
13958 @end ifset
13959
13960 @noindent
13961 The default is ^Default^DEFAULT^: no output for syntactically correct
13962 project files.
13963 @noindent
13964 If several @option{^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}} switches are present,
13965 only the last one is used.
13966
13967 @item ^-aP^/ADD_PROJECT_SEARCH_DIR=^<dir>
13968 @cindex @option{^-aP^/ADD_PROJECT_SEARCH_DIR=^} (any project-aware tool)
13969 Add directory <dir> at the beginning of the project search path, in order,
13970 after the current working directory.
13971
13972 @ifclear vms
13973 @item -eL
13974 @cindex @option{-eL} (any project-aware tool)
13975 Follow all symbolic links when processing project files.
13976 @end ifclear
13977
13978 @item ^--subdirs^/SUBDIRS^=<subdir>
13979 @cindex @option{^--subdirs^/SUBDIRS^=} (gnatmake and gnatclean)
13980 This switch is recognized by gnatmake and gnatclean. It indicate that the real
13981 directories (except the source directories) are the subdirectories <subdir>
13982 of the directories specified in the project files. This applies in particular
13983 to object directories, library directories and exec directories. If the
13984 subdirectories do not exist, they are created automatically.
13985
13986 @end table
13987
13988 @c **********************************
13989 @c * Tools Supporting Project Files *
13990 @c **********************************
13991
13992 @node Tools Supporting Project Files
13993 @section Tools Supporting Project Files
13994
13995 @menu
13996 * gnatmake and Project Files::
13997 * The GNAT Driver and Project Files::
13998 @end menu
13999
14000 @node gnatmake and Project Files
14001 @subsection gnatmake and Project Files
14002
14003 @noindent
14004 This section covers several topics related to @command{gnatmake} and
14005 project files: defining ^switches^switches^ for @command{gnatmake}
14006 and for the tools that it invokes; specifying configuration pragmas;
14007 the use of the @code{Main} attribute; building and rebuilding library project
14008 files.
14009
14010 @menu
14011 * ^Switches^Switches^ and Project Files::
14012 * Specifying Configuration Pragmas::
14013 * Project Files and Main Subprograms::
14014 * Library Project Files::
14015 @end menu
14016
14017 @node ^Switches^Switches^ and Project Files
14018 @subsubsection ^Switches^Switches^ and Project Files
14019
14020 @ifset vms
14021 It is not currently possible to specify VMS style qualifiers in the project
14022 files; only Unix style ^switches^switches^ may be specified.
14023 @end ifset
14024
14025 @noindent
14026 For each of the packages @code{Builder}, @code{Compiler}, @code{Binder}, and
14027 @code{Linker}, you can specify a @code{^Default_Switches^Default_Switches^}
14028 attribute, a @code{^Switches^Switches^} attribute, or both;
14029 as their names imply, these ^switch^switch^-related
14030 attributes affect the ^switches^switches^ that are used for each of these GNAT
14031 components when
14032 @command{gnatmake} is invoked. As will be explained below, these
14033 component-specific ^switches^switches^ precede
14034 the ^switches^switches^ provided on the @command{gnatmake} command line.
14035
14036 The @code{^Default_Switches^Default_Switches^} attribute is an associative
14037 array indexed by language name (case insensitive) whose value is a string list.
14038 For example:
14039
14040 @smallexample @c projectfile
14041 @group
14042 package Compiler is
14043 for ^Default_Switches^Default_Switches^ ("Ada")
14044 use ("^-gnaty^-gnaty^",
14045 "^-v^-v^");
14046 end Compiler;
14047 @end group
14048 @end smallexample
14049
14050 @noindent
14051 The @code{^Switches^Switches^} attribute is also an associative array,
14052 indexed by a file name (which may or may not be case sensitive, depending
14053 on the operating system) whose value is a string list. For example:
14054
14055 @smallexample @c projectfile
14056 @group
14057 package Builder is
14058 for ^Switches^Switches^ ("main1.adb")
14059 use ("^-O2^-O2^");
14060 for ^Switches^Switches^ ("main2.adb")
14061 use ("^-g^-g^");
14062 end Builder;
14063 @end group
14064 @end smallexample
14065
14066 @noindent
14067 For the @code{Builder} package, the file names must designate source files
14068 for main subprograms. For the @code{Binder} and @code{Linker} packages, the
14069 file names must designate @file{ALI} or source files for main subprograms.
14070 In each case just the file name without an explicit extension is acceptable.
14071
14072 For each tool used in a program build (@command{gnatmake}, the compiler, the
14073 binder, and the linker), the corresponding package @dfn{contributes} a set of
14074 ^switches^switches^ for each file on which the tool is invoked, based on the
14075 ^switch^switch^-related attributes defined in the package.
14076 In particular, the ^switches^switches^
14077 that each of these packages contributes for a given file @var{f} comprise:
14078
14079 @itemize @bullet
14080 @item
14081 the value of attribute @code{^Switches^Switches^ (@var{f})},
14082 if it is specified in the package for the given file,
14083 @item
14084 otherwise, the value of @code{^Default_Switches^Default_Switches^ ("Ada")},
14085 if it is specified in the package.
14086 @end itemize
14087
14088 @noindent
14089 If neither of these attributes is defined in the package, then the package does
14090 not contribute any ^switches^switches^ for the given file.
14091
14092 When @command{gnatmake} is invoked on a file, the ^switches^switches^ comprise
14093 two sets, in the following order: those contributed for the file
14094 by the @code{Builder} package;
14095 and the switches passed on the command line.
14096
14097 When @command{gnatmake} invokes a tool (compiler, binder, linker) on a file,
14098 the ^switches^switches^ passed to the tool comprise three sets,
14099 in the following order:
14100
14101 @enumerate
14102 @item
14103 the applicable ^switches^switches^ contributed for the file
14104 by the @code{Builder} package in the project file supplied on the command line;
14105
14106 @item
14107 those contributed for the file by the package (in the relevant project file --
14108 see below) corresponding to the tool; and
14109
14110 @item
14111 the applicable switches passed on the command line.
14112 @end enumerate
14113
14114 @noindent
14115 The term @emph{applicable ^switches^switches^} reflects the fact that
14116 @command{gnatmake} ^switches^switches^ may or may not be passed to individual
14117 tools, depending on the individual ^switch^switch^.
14118
14119 @command{gnatmake} may invoke the compiler on source files from different
14120 projects. The Project Manager will use the appropriate project file to
14121 determine the @code{Compiler} package for each source file being compiled.
14122 Likewise for the @code{Binder} and @code{Linker} packages.
14123
14124 As an example, consider the following package in a project file:
14125
14126 @smallexample @c projectfile
14127 @group
14128 project Proj1 is
14129 package Compiler is
14130 for ^Default_Switches^Default_Switches^ ("Ada")
14131 use ("^-g^-g^");
14132 for ^Switches^Switches^ ("a.adb")
14133 use ("^-O1^-O1^");
14134 for ^Switches^Switches^ ("b.adb")
14135 use ("^-O2^-O2^",
14136 "^-gnaty^-gnaty^");
14137 end Compiler;
14138 end Proj1;
14139 @end group
14140 @end smallexample
14141
14142 @noindent
14143 If @command{gnatmake} is invoked with this project file, and it needs to
14144 compile, say, the files @file{a.adb}, @file{b.adb}, and @file{c.adb}, then
14145 @file{a.adb} will be compiled with the ^switch^switch^
14146 @option{^-O1^-O1^},
14147 @file{b.adb} with ^switches^switches^
14148 @option{^-O2^-O2^}
14149 and @option{^-gnaty^-gnaty^},
14150 and @file{c.adb} with @option{^-g^-g^}.
14151
14152 The following example illustrates the ordering of the ^switches^switches^
14153 contributed by different packages:
14154
14155 @smallexample @c projectfile
14156 @group
14157 project Proj2 is
14158 package Builder is
14159 for ^Switches^Switches^ ("main.adb")
14160 use ("^-g^-g^",
14161 "^-O1^-)1^",
14162 "^-f^-f^");
14163 end Builder;
14164 @end group
14165
14166 @group
14167 package Compiler is
14168 for ^Switches^Switches^ ("main.adb")
14169 use ("^-O2^-O2^");
14170 end Compiler;
14171 end Proj2;
14172 @end group
14173 @end smallexample
14174
14175 @noindent
14176 If you issue the command:
14177
14178 @smallexample
14179 gnatmake ^-Pproj2^/PROJECT_FILE=PROJ2^ -O0 main
14180 @end smallexample
14181
14182 @noindent
14183 then the compiler will be invoked on @file{main.adb} with the following
14184 sequence of ^switches^switches^
14185
14186 @smallexample
14187 ^-g -O1 -O2 -O0^-g -O1 -O2 -O0^
14188 @end smallexample
14189
14190 with the last @option{^-O^-O^}
14191 ^switch^switch^ having precedence over the earlier ones;
14192 several other ^switches^switches^
14193 (such as @option{^-c^-c^}) are added implicitly.
14194
14195 The ^switches^switches^
14196 @option{^-g^-g^}
14197 and @option{^-O1^-O1^} are contributed by package
14198 @code{Builder}, @option{^-O2^-O2^} is contributed
14199 by the package @code{Compiler}
14200 and @option{^-O0^-O0^} comes from the command line.
14201
14202 The @option{^-g^-g^}
14203 ^switch^switch^ will also be passed in the invocation of
14204 @command{Gnatlink.}
14205
14206 A final example illustrates switch contributions from packages in different
14207 project files:
14208
14209 @smallexample @c projectfile
14210 @group
14211 project Proj3 is
14212 for Source_Files use ("pack.ads", "pack.adb");
14213 package Compiler is
14214 for ^Default_Switches^Default_Switches^ ("Ada")
14215 use ("^-gnata^-gnata^");
14216 end Compiler;
14217 end Proj3;
14218 @end group
14219
14220 @group
14221 with "Proj3";
14222 project Proj4 is
14223 for Source_Files use ("foo_main.adb", "bar_main.adb");
14224 package Builder is
14225 for ^Switches^Switches^ ("foo_main.adb")
14226 use ("^-s^-s^",
14227 "^-g^-g^");
14228 end Builder;
14229 end Proj4;
14230 @end group
14231
14232 @group
14233 -- Ada source file:
14234 with Pack;
14235 procedure Foo_Main is
14236 @dots{}
14237 end Foo_Main;
14238 @end group
14239 @end smallexample
14240
14241 If the command is
14242 @smallexample
14243 gnatmake ^-PProj4^/PROJECT_FILE=PROJ4^ foo_main.adb -cargs -gnato
14244 @end smallexample
14245
14246 @noindent
14247 then the ^switches^switches^ passed to the compiler for @file{foo_main.adb} are
14248 @option{^-g^-g^} (contributed by the package @code{Proj4.Builder}) and
14249 @option{^-gnato^-gnato^} (passed on the command line).
14250 When the imported package @code{Pack} is compiled, the ^switches^switches^ used
14251 are @option{^-g^-g^} from @code{Proj4.Builder},
14252 @option{^-gnata^-gnata^} (contributed from package @code{Proj3.Compiler},
14253 and @option{^-gnato^-gnato^} from the command line.
14254
14255 @noindent
14256 When using @command{gnatmake} with project files, some ^switches^switches^ or
14257 arguments may be expressed as relative paths. As the working directory where
14258 compilation occurs may change, these relative paths are converted to absolute
14259 paths. For the ^switches^switches^ found in a project file, the relative paths
14260 are relative to the project file directory, for the switches on the command
14261 line, they are relative to the directory where @command{gnatmake} is invoked.
14262 The ^switches^switches^ for which this occurs are:
14263 ^-I^-I^,
14264 ^-A^-A^,
14265 ^-L^-L^,
14266 ^-aO^-aO^,
14267 ^-aL^-aL^,
14268 ^-aI^-aI^, as well as all arguments that are not switches (arguments to
14269 ^switch^switch^
14270 ^-o^-o^, object files specified in package @code{Linker} or after
14271 -largs on the command line). The exception to this rule is the ^switch^switch^
14272 ^--RTS=^--RTS=^ for which a relative path argument is never converted.
14273
14274 @node Specifying Configuration Pragmas
14275 @subsubsection Specifying Configuration Pragmas
14276
14277 When using @command{gnatmake} with project files, if there exists a file
14278 @file{gnat.adc} that contains configuration pragmas, this file will be
14279 ignored.
14280
14281 Configuration pragmas can be defined by means of the following attributes in
14282 project files: @code{Global_Configuration_Pragmas} in package @code{Builder}
14283 and @code{Local_Configuration_Pragmas} in package @code{Compiler}.
14284
14285 Both these attributes are single string attributes. Their values is the path
14286 name of a file containing configuration pragmas. If a path name is relative,
14287 then it is relative to the project directory of the project file where the
14288 attribute is defined.
14289
14290 When compiling a source, the configuration pragmas used are, in order,
14291 those listed in the file designated by attribute
14292 @code{Global_Configuration_Pragmas} in package @code{Builder} of the main
14293 project file, if it is specified, and those listed in the file designated by
14294 attribute @code{Local_Configuration_Pragmas} in package @code{Compiler} of
14295 the project file of the source, if it exists.
14296
14297 @node Project Files and Main Subprograms
14298 @subsubsection Project Files and Main Subprograms
14299
14300 @noindent
14301 When using a project file, you can invoke @command{gnatmake}
14302 with one or several main subprograms, by specifying their source files on the
14303 command line.
14304
14305 @smallexample
14306 gnatmake ^-P^/PROJECT_FILE=^prj main1 main2 main3
14307 @end smallexample
14308
14309 @noindent
14310 Each of these needs to be a source file of the same project, except
14311 when the switch ^-u^/UNIQUE^ is used.
14312
14313 @noindent
14314 When ^-u^/UNIQUE^ is not used, all the mains need to be sources of the
14315 same project, one of the project in the tree rooted at the project specified
14316 on the command line. The package @code{Builder} of this common project, the
14317 "main project" is the one that is considered by @command{gnatmake}.
14318
14319 @noindent
14320 When ^-u^/UNIQUE^ is used, the specified source files may be in projects
14321 imported directly or indirectly by the project specified on the command line.
14322 Note that if such a source file is not part of the project specified on the
14323 command line, the ^switches^switches^ found in package @code{Builder} of the
14324 project specified on the command line, if any, that are transmitted
14325 to the compiler will still be used, not those found in the project file of
14326 the source file.
14327
14328 @noindent
14329 When using a project file, you can also invoke @command{gnatmake} without
14330 explicitly specifying any main, and the effect depends on whether you have
14331 defined the @code{Main} attribute. This attribute has a string list value,
14332 where each element in the list is the name of a source file (the file
14333 extension is optional) that contains a unit that can be a main subprogram.
14334
14335 If the @code{Main} attribute is defined in a project file as a non-empty
14336 string list and the switch @option{^-u^/UNIQUE^} is not used on the command
14337 line, then invoking @command{gnatmake} with this project file but without any
14338 main on the command line is equivalent to invoking @command{gnatmake} with all
14339 the file names in the @code{Main} attribute on the command line.
14340
14341 Example:
14342 @smallexample @c projectfile
14343 @group
14344 project Prj is
14345 for Main use ("main1", "main2", "main3");
14346 end Prj;
14347 @end group
14348 @end smallexample
14349
14350 @noindent
14351 With this project file, @code{"gnatmake ^-Pprj^/PROJECT_FILE=PRJ^"}
14352 is equivalent to
14353 @code{"gnatmake ^-Pprj^/PROJECT_FILE=PRJ^ main1 main2 main3"}.
14354
14355 When the project attribute @code{Main} is not specified, or is specified
14356 as an empty string list, or when the switch @option{-u} is used on the command
14357 line, then invoking @command{gnatmake} with no main on the command line will
14358 result in all immediate sources of the project file being checked, and
14359 potentially recompiled. Depending on the presence of the switch @option{-u},
14360 sources from other project files on which the immediate sources of the main
14361 project file depend are also checked and potentially recompiled. In other
14362 words, the @option{-u} switch is applied to all of the immediate sources of the
14363 main project file.
14364
14365 When no main is specified on the command line and attribute @code{Main} exists
14366 and includes several mains, or when several mains are specified on the
14367 command line, the default ^switches^switches^ in package @code{Builder} will
14368 be used for all mains, even if there are specific ^switches^switches^
14369 specified for one or several mains.
14370
14371 But the ^switches^switches^ from package @code{Binder} or @code{Linker} will be
14372 the specific ^switches^switches^ for each main, if they are specified.
14373
14374 @node Library Project Files
14375 @subsubsection Library Project Files
14376
14377 @noindent
14378 When @command{gnatmake} is invoked with a main project file that is a library
14379 project file, it is not allowed to specify one or more mains on the command
14380 line.
14381
14382 @noindent
14383 When a library project file is specified, switches ^-b^/ACTION=BIND^ and
14384 ^-l^/ACTION=LINK^ have special meanings.
14385
14386 @itemize @bullet
14387 @item ^-b^/ACTION=BIND^ is only allowed for stand-alone libraries. It indicates
14388 to @command{gnatmake} that @command{gnatbind} should be invoked for the
14389 library.
14390
14391 @item ^-l^/ACTION=LINK^ may be used for all library projects. It indicates
14392 to @command{gnatmake} that the binder generated file should be compiled
14393 (in the case of a stand-alone library) and that the library should be built.
14394
14395 @end itemize
14396
14397 @node The GNAT Driver and Project Files
14398 @subsection The GNAT Driver and Project Files
14399
14400 @noindent
14401 A number of GNAT tools, other than @command{^gnatmake^gnatmake^}
14402 can benefit from project files:
14403 @command{^gnatbind^gnatbind^},
14404 @command{^gnatcheck^gnatcheck^}),
14405 @command{^gnatclean^gnatclean^}),
14406 @command{^gnatelim^gnatelim^},
14407 @command{^gnatfind^gnatfind^},
14408 @command{^gnatlink^gnatlink^},
14409 @command{^gnatls^gnatls^},
14410 @command{^gnatmetric^gnatmetric^},
14411 @command{^gnatpp^gnatpp^},
14412 @command{^gnatstub^gnatstub^},
14413 and @command{^gnatxref^gnatxref^}. However, none of these tools can be invoked
14414 directly with a project file switch (@option{^-P^/PROJECT_FILE=^}).
14415 They must be invoked through the @command{gnat} driver.
14416
14417 The @command{gnat} driver is a wrapper that accepts a number of commands and
14418 calls the corresponding tool. It was designed initially for VMS platforms (to
14419 convert VMS qualifiers to Unix-style switches), but it is now available on all
14420 GNAT platforms.
14421
14422 On non-VMS platforms, the @command{gnat} driver accepts the following commands
14423 (case insensitive):
14424
14425 @itemize @bullet
14426 @item
14427 BIND to invoke @command{^gnatbind^gnatbind^}
14428 @item
14429 CHOP to invoke @command{^gnatchop^gnatchop^}
14430 @item
14431 CLEAN to invoke @command{^gnatclean^gnatclean^}
14432 @item
14433 COMP or COMPILE to invoke the compiler
14434 @item
14435 ELIM to invoke @command{^gnatelim^gnatelim^}
14436 @item
14437 FIND to invoke @command{^gnatfind^gnatfind^}
14438 @item
14439 KR or KRUNCH to invoke @command{^gnatkr^gnatkr^}
14440 @item
14441 LINK to invoke @command{^gnatlink^gnatlink^}
14442 @item
14443 LS or LIST to invoke @command{^gnatls^gnatls^}
14444 @item
14445 MAKE to invoke @command{^gnatmake^gnatmake^}
14446 @item
14447 NAME to invoke @command{^gnatname^gnatname^}
14448 @item
14449 PREP or PREPROCESS to invoke @command{^gnatprep^gnatprep^}
14450 @item
14451 PP or PRETTY to invoke @command{^gnatpp^gnatpp^}
14452 @item
14453 METRIC to invoke @command{^gnatmetric^gnatmetric^}
14454 @item
14455 STUB to invoke @command{^gnatstub^gnatstub^}
14456 @item
14457 XREF to invoke @command{^gnatxref^gnatxref^}
14458 @end itemize
14459
14460 @noindent
14461 (note that the compiler is invoked using the command
14462 @command{^gnatmake -f -u -c^gnatmake -f -u -c^}).
14463
14464 @noindent
14465 On non-VMS platforms, between @command{gnat} and the command, two
14466 special switches may be used:
14467
14468 @itemize @bullet
14469 @item
14470 @command{-v} to display the invocation of the tool.
14471 @item
14472 @command{-dn} to prevent the @command{gnat} driver from removing
14473 the temporary files it has created. These temporary files are
14474 configuration files and temporary file list files.
14475 @end itemize
14476
14477 @noindent
14478 The command may be followed by switches and arguments for the invoked
14479 tool.
14480
14481 @smallexample
14482 gnat bind -C main.ali
14483 gnat ls -a main
14484 gnat chop foo.txt
14485 @end smallexample
14486
14487 @noindent
14488 Switches may also be put in text files, one switch per line, and the text
14489 files may be specified with their path name preceded by '@@'.
14490
14491 @smallexample
14492 gnat bind @@args.txt main.ali
14493 @end smallexample
14494
14495 @noindent
14496 In addition, for commands BIND, COMP or COMPILE, FIND, ELIM, LS or LIST, LINK,
14497 METRIC, PP or PRETTY, STUB and XREF, the project file related switches
14498 (@option{^-P^/PROJECT_FILE^},
14499 @option{^-X^/EXTERNAL_REFERENCE^} and
14500 @option{^-vP^/MESSAGES_PROJECT_FILE=^x}) may be used in addition to
14501 the switches of the invoking tool.
14502
14503 @noindent
14504 When GNAT PP or GNAT PRETTY is used with a project file, but with no source
14505 specified on the command line, it invokes @command{^gnatpp^gnatpp^} with all
14506 the immediate sources of the specified project file.
14507
14508 @noindent
14509 When GNAT METRIC is used with a project file, but with no source
14510 specified on the command line, it invokes @command{^gnatmetric^gnatmetric^}
14511 with all the immediate sources of the specified project file and with
14512 @option{^-d^/DIRECTORY^} with the parameter pointing to the object directory
14513 of the project.
14514
14515 @noindent
14516 In addition, when GNAT PP, GNAT PRETTY or GNAT METRIC is used with
14517 a project file, no source is specified on the command line and
14518 switch ^-U^/ALL_PROJECTS^ is specified on the command line, then
14519 the underlying tool (^gnatpp^gnatpp^ or
14520 ^gnatmetric^gnatmetric^) is invoked for all sources of all projects,
14521 not only for the immediate sources of the main project.
14522 @ifclear vms
14523 (-U stands for Universal or Union of the project files of the project tree)
14524 @end ifclear
14525
14526 @noindent
14527 For each of the following commands, there is optionally a corresponding
14528 package in the main project.
14529
14530 @itemize @bullet
14531 @item
14532 package @code{Binder} for command BIND (invoking @code{^gnatbind^gnatbind^})
14533
14534 @item
14535 package @code{Check} for command CHECK (invoking
14536 @code{^gnatcheck^gnatcheck^})
14537
14538 @item
14539 package @code{Compiler} for command COMP or COMPILE (invoking the compiler)
14540
14541 @item
14542 package @code{Cross_Reference} for command XREF (invoking
14543 @code{^gnatxref^gnatxref^})
14544
14545 @item
14546 package @code{Eliminate} for command ELIM (invoking
14547 @code{^gnatelim^gnatelim^})
14548
14549 @item
14550 package @code{Finder} for command FIND (invoking @code{^gnatfind^gnatfind^})
14551
14552 @item
14553 package @code{Gnatls} for command LS or LIST (invoking @code{^gnatls^gnatls^})
14554
14555 @item
14556 package @code{Gnatstub} for command STUB
14557 (invoking @code{^gnatstub^gnatstub^})
14558
14559 @item
14560 package @code{Linker} for command LINK (invoking @code{^gnatlink^gnatlink^})
14561
14562 @item
14563 package @code{Metrics} for command METRIC
14564 (invoking @code{^gnatmetric^gnatmetric^})
14565
14566 @item
14567 package @code{Pretty_Printer} for command PP or PRETTY
14568 (invoking @code{^gnatpp^gnatpp^})
14569
14570 @end itemize
14571
14572 @noindent
14573 Package @code{Gnatls} has a unique attribute @code{^Switches^Switches^},
14574 a simple variable with a string list value. It contains ^switches^switches^
14575 for the invocation of @code{^gnatls^gnatls^}.
14576
14577 @smallexample @c projectfile
14578 @group
14579 project Proj1 is
14580 package gnatls is
14581 for ^Switches^Switches^
14582 use ("^-a^-a^",
14583 "^-v^-v^");
14584 end gnatls;
14585 end Proj1;
14586 @end group
14587 @end smallexample
14588
14589 @noindent
14590 All other packages have two attribute @code{^Switches^Switches^} and
14591 @code{^Default_Switches^Default_Switches^}.
14592
14593 @noindent
14594 @code{^Switches^Switches^} is an associative array attribute, indexed by the
14595 source file name, that has a string list value: the ^switches^switches^ to be
14596 used when the tool corresponding to the package is invoked for the specific
14597 source file.
14598
14599 @noindent
14600 @code{^Default_Switches^Default_Switches^} is an associative array attribute,
14601 indexed by the programming language that has a string list value.
14602 @code{^Default_Switches^Default_Switches^ ("Ada")} contains the
14603 ^switches^switches^ for the invocation of the tool corresponding
14604 to the package, except if a specific @code{^Switches^Switches^} attribute
14605 is specified for the source file.
14606
14607 @smallexample @c projectfile
14608 @group
14609 project Proj is
14610
14611 for Source_Dirs use ("./**");
14612
14613 package gnatls is
14614 for ^Switches^Switches^ use
14615 ("^-a^-a^",
14616 "^-v^-v^");
14617 end gnatls;
14618 @end group
14619 @group
14620
14621 package Compiler is
14622 for ^Default_Switches^Default_Switches^ ("Ada")
14623 use ("^-gnatv^-gnatv^",
14624 "^-gnatwa^-gnatwa^");
14625 end Binder;
14626 @end group
14627 @group
14628
14629 package Binder is
14630 for ^Default_Switches^Default_Switches^ ("Ada")
14631 use ("^-C^-C^",
14632 "^-e^-e^");
14633 end Binder;
14634 @end group
14635 @group
14636
14637 package Linker is
14638 for ^Default_Switches^Default_Switches^ ("Ada")
14639 use ("^-C^-C^");
14640 for ^Switches^Switches^ ("main.adb")
14641 use ("^-C^-C^",
14642 "^-v^-v^",
14643 "^-v^-v^");
14644 end Linker;
14645 @end group
14646 @group
14647
14648 package Finder is
14649 for ^Default_Switches^Default_Switches^ ("Ada")
14650 use ("^-a^-a^",
14651 "^-f^-f^");
14652 end Finder;
14653 @end group
14654 @group
14655
14656 package Cross_Reference is
14657 for ^Default_Switches^Default_Switches^ ("Ada")
14658 use ("^-a^-a^",
14659 "^-f^-f^",
14660 "^-d^-d^",
14661 "^-u^-u^");
14662 end Cross_Reference;
14663 end Proj;
14664 @end group
14665 @end smallexample
14666
14667 @noindent
14668 With the above project file, commands such as
14669
14670 @smallexample
14671 ^gnat comp -Pproj main^GNAT COMP /PROJECT_FILE=PROJ MAIN^
14672 ^gnat ls -Pproj main^GNAT LIST /PROJECT_FILE=PROJ MAIN^
14673 ^gnat xref -Pproj main^GNAT XREF /PROJECT_FILE=PROJ MAIN^
14674 ^gnat bind -Pproj main.ali^GNAT BIND /PROJECT_FILE=PROJ MAIN.ALI^
14675 ^gnat link -Pproj main.ali^GNAT LINK /PROJECT_FILE=PROJ MAIN.ALI^
14676 @end smallexample
14677
14678 @noindent
14679 will set up the environment properly and invoke the tool with the switches
14680 found in the package corresponding to the tool:
14681 @code{^Default_Switches^Default_Switches^ ("Ada")} for all tools,
14682 except @code{^Switches^Switches^ ("main.adb")}
14683 for @code{^gnatlink^gnatlink^}.
14684 It is also possible to invoke some of the tools,
14685 @code{^gnatcheck^gnatcheck^}),
14686 @code{^gnatmetric^gnatmetric^}),
14687 and @code{^gnatpp^gnatpp^})
14688 on a set of project units thanks to the combination of the switches
14689 @option{-P}, @option{-U} and possibly the main unit when one is interested
14690 in its closure. For instance,
14691 @smallexample
14692 gnat metric -Pproj
14693 @end smallexample
14694 will compute the metrics for all the immediate units of project
14695 @code{proj}.
14696 @smallexample
14697 gnat metric -Pproj -U
14698 @end smallexample
14699 will compute the metrics for all the units of the closure of projects
14700 rooted at @code{proj}.
14701 @smallexample
14702 gnat metric -Pproj -U main_unit
14703 @end smallexample
14704 will compute the metrics for the closure of units rooted at
14705 @code{main_unit}. This last possibility relies implicitly
14706 on @command{gnatbind}'s option @option{-R}.
14707
14708 @c **********************
14709 @node An Extended Example
14710 @section An Extended Example
14711
14712 @noindent
14713 Suppose that we have two programs, @var{prog1} and @var{prog2},
14714 whose sources are in corresponding directories. We would like
14715 to build them with a single @command{gnatmake} command, and we want to place
14716 their object files into @file{build} subdirectories of the source directories.
14717 Furthermore, we want to have to have two separate subdirectories
14718 in @file{build} -- @file{release} and @file{debug} -- which will contain
14719 the object files compiled with different set of compilation flags.
14720
14721 In other words, we have the following structure:
14722
14723 @smallexample
14724 @group
14725 main
14726 |- prog1
14727 | |- build
14728 | | debug
14729 | | release
14730 |- prog2
14731 |- build
14732 | debug
14733 | release
14734 @end group
14735 @end smallexample
14736
14737 @noindent
14738 Here are the project files that we must place in a directory @file{main}
14739 to maintain this structure:
14740
14741 @enumerate
14742
14743 @item We create a @code{Common} project with a package @code{Compiler} that
14744 specifies the compilation ^switches^switches^:
14745
14746 @smallexample
14747 File "common.gpr":
14748 @group
14749 @b{project} Common @b{is}
14750
14751 @b{for} Source_Dirs @b{use} (); -- No source files
14752 @end group
14753
14754 @group
14755 @b{type} Build_Type @b{is} ("release", "debug");
14756 Build : Build_Type := External ("BUILD", "debug");
14757 @end group
14758 @group
14759 @b{package} Compiler @b{is}
14760 @b{case} Build @b{is}
14761 @b{when} "release" =>
14762 @b{for} ^Default_Switches^Default_Switches^ ("Ada")
14763 @b{use} ("^-O2^-O2^");
14764 @b{when} "debug" =>
14765 @b{for} ^Default_Switches^Default_Switches^ ("Ada")
14766 @b{use} ("^-g^-g^");
14767 @b{end case};
14768 @b{end} Compiler;
14769
14770 @b{end} Common;
14771 @end group
14772 @end smallexample
14773
14774 @item We create separate projects for the two programs:
14775
14776 @smallexample
14777 @group
14778 File "prog1.gpr":
14779
14780 @b{with} "common";
14781 @b{project} Prog1 @b{is}
14782
14783 @b{for} Source_Dirs @b{use} ("prog1");
14784 @b{for} Object_Dir @b{use} "prog1/build/" & Common.Build;
14785
14786 @b{package} Compiler @b{renames} Common.Compiler;
14787
14788 @b{end} Prog1;
14789 @end group
14790 @end smallexample
14791
14792 @smallexample
14793 @group
14794 File "prog2.gpr":
14795
14796 @b{with} "common";
14797 @b{project} Prog2 @b{is}
14798
14799 @b{for} Source_Dirs @b{use} ("prog2");
14800 @b{for} Object_Dir @b{use} "prog2/build/" & Common.Build;
14801
14802 @b{package} Compiler @b{renames} Common.Compiler;
14803
14804 @end group
14805 @b{end} Prog2;
14806 @end smallexample
14807
14808 @item We create a wrapping project @code{Main}:
14809
14810 @smallexample
14811 @group
14812 File "main.gpr":
14813
14814 @b{with} "common";
14815 @b{with} "prog1";
14816 @b{with} "prog2";
14817 @b{project} Main @b{is}
14818
14819 @b{package} Compiler @b{renames} Common.Compiler;
14820
14821 @b{end} Main;
14822 @end group
14823 @end smallexample
14824
14825 @item Finally we need to create a dummy procedure that @code{with}s (either
14826 explicitly or implicitly) all the sources of our two programs.
14827
14828 @end enumerate
14829
14830 @noindent
14831 Now we can build the programs using the command
14832
14833 @smallexample
14834 gnatmake ^-P^/PROJECT_FILE=^main dummy
14835 @end smallexample
14836
14837 @noindent
14838 for the Debug mode, or
14839
14840 @ifclear vms
14841 @smallexample
14842 gnatmake -Pmain -XBUILD=release
14843 @end smallexample
14844 @end ifclear
14845
14846 @ifset vms
14847 @smallexample
14848 GNAT MAKE /PROJECT_FILE=main /EXTERNAL_REFERENCE=BUILD=release
14849 @end smallexample
14850 @end ifset
14851
14852 @noindent
14853 for the Release mode.
14854
14855 @c ********************************
14856 @c * Project File Complete Syntax *
14857 @c ********************************
14858
14859 @node Project File Complete Syntax
14860 @section Project File Complete Syntax
14861
14862 @smallexample
14863 project ::=
14864 context_clause project_declaration
14865
14866 context_clause ::=
14867 @{with_clause@}
14868
14869 with_clause ::=
14870 @b{with} path_name @{ , path_name @} ;
14871
14872 path_name ::=
14873 string_literal
14874
14875 project_declaration ::=
14876 simple_project_declaration | project_extension
14877
14878 simple_project_declaration ::=
14879 @b{project} <project_>simple_name @b{is}
14880 @{declarative_item@}
14881 @b{end} <project_>simple_name;
14882
14883 project_extension ::=
14884 @b{project} <project_>simple_name @b{extends} path_name @b{is}
14885 @{declarative_item@}
14886 @b{end} <project_>simple_name;
14887
14888 declarative_item ::=
14889 package_declaration |
14890 typed_string_declaration |
14891 other_declarative_item
14892
14893 package_declaration ::=
14894 package_spec | package_renaming
14895
14896 package_spec ::=
14897 @b{package} package_identifier @b{is}
14898 @{simple_declarative_item@}
14899 @b{end} package_identifier ;
14900
14901 package_identifier ::=
14902 @code{Naming} | @code{Builder} | @code{Compiler} | @code{Binder} |
14903 @code{Linker} | @code{Finder} | @code{Cross_Reference} |
14904 @code{^gnatls^gnatls^} | @code{IDE} | @code{Pretty_Printer}
14905
14906 package_renaming ::==
14907 @b{package} package_identifier @b{renames}
14908 <project_>simple_name.package_identifier ;
14909
14910 typed_string_declaration ::=
14911 @b{type} <typed_string_>_simple_name @b{is}
14912 ( string_literal @{, string_literal@} );
14913
14914 other_declarative_item ::=
14915 attribute_declaration |
14916 typed_variable_declaration |
14917 variable_declaration |
14918 case_construction
14919
14920 attribute_declaration ::=
14921 full_associative_array_declaration |
14922 @b{for} attribute_designator @b{use} expression ;
14923
14924 full_associative_array_declaration ::=
14925 @b{for} <associative_array_attribute_>simple_name @b{use}
14926 <project_>simple_name [ . <package_>simple_Name ] ' <attribute_>simple_name ;
14927
14928 attribute_designator ::=
14929 <simple_attribute_>simple_name |
14930 <associative_array_attribute_>simple_name ( string_literal )
14931
14932 typed_variable_declaration ::=
14933 <typed_variable_>simple_name : <typed_string_>name := string_expression ;
14934
14935 variable_declaration ::=
14936 <variable_>simple_name := expression;
14937
14938 expression ::=
14939 term @{& term@}
14940
14941 term ::=
14942 literal_string |
14943 string_list |
14944 <variable_>name |
14945 external_value |
14946 attribute_reference
14947
14948 string_literal ::=
14949 (same as Ada)
14950
14951 string_list ::=
14952 ( <string_>expression @{ , <string_>expression @} )
14953
14954 external_value ::=
14955 @b{external} ( string_literal [, string_literal] )
14956
14957 attribute_reference ::=
14958 attribute_prefix ' <simple_attribute_>simple_name [ ( literal_string ) ]
14959
14960 attribute_prefix ::=
14961 @b{project} |
14962 <project_>simple_name | package_identifier |
14963 <project_>simple_name . package_identifier
14964
14965 case_construction ::=
14966 @b{case} <typed_variable_>name @b{is}
14967 @{case_item@}
14968 @b{end case} ;
14969
14970 case_item ::=
14971 @b{when} discrete_choice_list =>
14972 @{case_construction | attribute_declaration@}
14973
14974 discrete_choice_list ::=
14975 string_literal @{| string_literal@} |
14976 @b{others}
14977
14978 name ::=
14979 simple_name @{. simple_name@}
14980
14981 simple_name ::=
14982 identifier (same as Ada)
14983
14984 @end smallexample
14985
14986 @node The Cross-Referencing Tools gnatxref and gnatfind
14987 @chapter The Cross-Referencing Tools @code{gnatxref} and @code{gnatfind}
14988 @findex gnatxref
14989 @findex gnatfind
14990
14991 @noindent
14992 The compiler generates cross-referencing information (unless
14993 you set the @samp{-gnatx} switch), which are saved in the @file{.ali} files.
14994 This information indicates where in the source each entity is declared and
14995 referenced. Note that entities in package Standard are not included, but
14996 entities in all other predefined units are included in the output.
14997
14998 Before using any of these two tools, you need to compile successfully your
14999 application, so that GNAT gets a chance to generate the cross-referencing
15000 information.
15001
15002 The two tools @code{gnatxref} and @code{gnatfind} take advantage of this
15003 information to provide the user with the capability to easily locate the
15004 declaration and references to an entity. These tools are quite similar,
15005 the difference being that @code{gnatfind} is intended for locating
15006 definitions and/or references to a specified entity or entities, whereas
15007 @code{gnatxref} is oriented to generating a full report of all
15008 cross-references.
15009
15010 To use these tools, you must not compile your application using the
15011 @option{-gnatx} switch on the @command{gnatmake} command line
15012 (@pxref{The GNAT Make Program gnatmake}). Otherwise, cross-referencing
15013 information will not be generated.
15014
15015 Note: to invoke @code{gnatxref} or @code{gnatfind} with a project file,
15016 use the @code{gnat} driver (see @ref{The GNAT Driver and Project Files}).
15017
15018 @menu
15019 * gnatxref Switches::
15020 * gnatfind Switches::
15021 * Project Files for gnatxref and gnatfind::
15022 * Regular Expressions in gnatfind and gnatxref::
15023 * Examples of gnatxref Usage::
15024 * Examples of gnatfind Usage::
15025 @end menu
15026
15027 @node gnatxref Switches
15028 @section @code{gnatxref} Switches
15029
15030 @noindent
15031 The command invocation for @code{gnatxref} is:
15032 @smallexample
15033 $ gnatxref @ovar{switches} @var{sourcefile1} @r{[}@var{sourcefile2} @dots{}@r{]}
15034 @end smallexample
15035
15036 @noindent
15037 where
15038
15039 @table @var
15040 @item sourcefile1
15041 @itemx sourcefile2
15042 identifies the source files for which a report is to be generated. The
15043 ``with''ed units will be processed too. You must provide at least one file.
15044
15045 These file names are considered to be regular expressions, so for instance
15046 specifying @file{source*.adb} is the same as giving every file in the current
15047 directory whose name starts with @file{source} and whose extension is
15048 @file{adb}.
15049
15050 You shouldn't specify any directory name, just base names. @command{gnatxref}
15051 and @command{gnatfind} will be able to locate these files by themselves using
15052 the source path. If you specify directories, no result is produced.
15053
15054 @end table
15055
15056 @noindent
15057 The switches can be:
15058 @table @option
15059 @c !sort!
15060 @item --version
15061 @cindex @option{--version} @command{gnatxref}
15062 Display Copyright and version, then exit disregarding all other options.
15063
15064 @item --help
15065 @cindex @option{--help} @command{gnatxref}
15066 If @option{--version} was not used, display usage, then exit disregarding
15067 all other options.
15068
15069 @item ^-a^/ALL_FILES^
15070 @cindex @option{^-a^/ALL_FILES^} (@command{gnatxref})
15071 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
15072 the read-only files found in the library search path. Otherwise, these files
15073 will be ignored. This option can be used to protect Gnat sources or your own
15074 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
15075 much faster, and their output much smaller. Read-only here refers to access
15076 or permissions status in the file system for the current user.
15077
15078 @item -aIDIR
15079 @cindex @option{-aIDIR} (@command{gnatxref})
15080 When looking for source files also look in directory DIR. The order in which
15081 source file search is undertaken is the same as for @command{gnatmake}.
15082
15083 @item -aODIR
15084 @cindex @option{-aODIR} (@command{gnatxref})
15085 When searching for library and object files, look in directory
15086 DIR. The order in which library files are searched is the same as for
15087 @command{gnatmake}.
15088
15089 @item -nostdinc
15090 @cindex @option{-nostdinc} (@command{gnatxref})
15091 Do not look for sources in the system default directory.
15092
15093 @item -nostdlib
15094 @cindex @option{-nostdlib} (@command{gnatxref})
15095 Do not look for library files in the system default directory.
15096
15097 @item --RTS=@var{rts-path}
15098 @cindex @option{--RTS} (@command{gnatxref})
15099 Specifies the default location of the runtime library. Same meaning as the
15100 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
15101
15102 @item ^-d^/DERIVED_TYPES^
15103 @cindex @option{^-d^/DERIVED_TYPES^} (@command{gnatxref})
15104 If this switch is set @code{gnatxref} will output the parent type
15105 reference for each matching derived types.
15106
15107 @item ^-f^/FULL_PATHNAME^
15108 @cindex @option{^-f^/FULL_PATHNAME^} (@command{gnatxref})
15109 If this switch is set, the output file names will be preceded by their
15110 directory (if the file was found in the search path). If this switch is
15111 not set, the directory will not be printed.
15112
15113 @item ^-g^/IGNORE_LOCALS^
15114 @cindex @option{^-g^/IGNORE_LOCALS^} (@command{gnatxref})
15115 If this switch is set, information is output only for library-level
15116 entities, ignoring local entities. The use of this switch may accelerate
15117 @code{gnatfind} and @code{gnatxref}.
15118
15119 @item -IDIR
15120 @cindex @option{-IDIR} (@command{gnatxref})
15121 Equivalent to @samp{-aODIR -aIDIR}.
15122
15123 @item -pFILE
15124 @cindex @option{-pFILE} (@command{gnatxref})
15125 Specify a project file to use @xref{Project Files}.
15126 If you need to use the @file{.gpr}
15127 project files, you should use gnatxref through the GNAT driver
15128 (@command{gnat xref -Pproject}).
15129
15130 By default, @code{gnatxref} and @code{gnatfind} will try to locate a
15131 project file in the current directory.
15132
15133 If a project file is either specified or found by the tools, then the content
15134 of the source directory and object directory lines are added as if they
15135 had been specified respectively by @samp{^-aI^/SOURCE_SEARCH^}
15136 and @samp{^-aO^OBJECT_SEARCH^}.
15137 @item ^-u^/UNUSED^
15138 Output only unused symbols. This may be really useful if you give your
15139 main compilation unit on the command line, as @code{gnatxref} will then
15140 display every unused entity and 'with'ed package.
15141
15142 @ifclear vms
15143 @item -v
15144 Instead of producing the default output, @code{gnatxref} will generate a
15145 @file{tags} file that can be used by vi. For examples how to use this
15146 feature, see @ref{Examples of gnatxref Usage}. The tags file is output
15147 to the standard output, thus you will have to redirect it to a file.
15148 @end ifclear
15149
15150 @end table
15151
15152 @noindent
15153 All these switches may be in any order on the command line, and may even
15154 appear after the file names. They need not be separated by spaces, thus
15155 you can say @samp{gnatxref ^-ag^/ALL_FILES/IGNORE_LOCALS^} instead of
15156 @samp{gnatxref ^-a -g^/ALL_FILES /IGNORE_LOCALS^}.
15157
15158 @node gnatfind Switches
15159 @section @code{gnatfind} Switches
15160
15161 @noindent
15162 The command line for @code{gnatfind} is:
15163
15164 @smallexample
15165 $ gnatfind @ovar{switches} @var{pattern}@r{[}:@var{sourcefile}@r{[}:@var{line}@r{[}:@var{column}@r{]]]}
15166 @r{[}@var{file1} @var{file2} @dots{}]
15167 @end smallexample
15168
15169 @noindent
15170 where
15171
15172 @table @var
15173 @item pattern
15174 An entity will be output only if it matches the regular expression found
15175 in @var{pattern}, see @ref{Regular Expressions in gnatfind and gnatxref}.
15176
15177 Omitting the pattern is equivalent to specifying @samp{*}, which
15178 will match any entity. Note that if you do not provide a pattern, you
15179 have to provide both a sourcefile and a line.
15180
15181 Entity names are given in Latin-1, with uppercase/lowercase equivalence
15182 for matching purposes. At the current time there is no support for
15183 8-bit codes other than Latin-1, or for wide characters in identifiers.
15184
15185 @item sourcefile
15186 @code{gnatfind} will look for references, bodies or declarations
15187 of symbols referenced in @file{@var{sourcefile}}, at line @var{line}
15188 and column @var{column}. See @ref{Examples of gnatfind Usage}
15189 for syntax examples.
15190
15191 @item line
15192 is a decimal integer identifying the line number containing
15193 the reference to the entity (or entities) to be located.
15194
15195 @item column
15196 is a decimal integer identifying the exact location on the
15197 line of the first character of the identifier for the
15198 entity reference. Columns are numbered from 1.
15199
15200 @item file1 file2 @dots{}
15201 The search will be restricted to these source files. If none are given, then
15202 the search will be done for every library file in the search path.
15203 These file must appear only after the pattern or sourcefile.
15204
15205 These file names are considered to be regular expressions, so for instance
15206 specifying @file{source*.adb} is the same as giving every file in the current
15207 directory whose name starts with @file{source} and whose extension is
15208 @file{adb}.
15209
15210 The location of the spec of the entity will always be displayed, even if it
15211 isn't in one of @file{@var{file1}}, @file{@var{file2}},@enddots{} The
15212 occurrences of the entity in the separate units of the ones given on the
15213 command line will also be displayed.
15214
15215 Note that if you specify at least one file in this part, @code{gnatfind} may
15216 sometimes not be able to find the body of the subprograms.
15217
15218 @end table
15219
15220 @noindent
15221 At least one of 'sourcefile' or 'pattern' has to be present on
15222 the command line.
15223
15224 The following switches are available:
15225 @table @option
15226 @c !sort!
15227
15228 @cindex @option{--version} @command{gnatfind}
15229 Display Copyright and version, then exit disregarding all other options.
15230
15231 @item --help
15232 @cindex @option{--help} @command{gnatfind}
15233 If @option{--version} was not used, display usage, then exit disregarding
15234 all other options.
15235
15236 @item ^-a^/ALL_FILES^
15237 @cindex @option{^-a^/ALL_FILES^} (@command{gnatfind})
15238 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
15239 the read-only files found in the library search path. Otherwise, these files
15240 will be ignored. This option can be used to protect Gnat sources or your own
15241 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
15242 much faster, and their output much smaller. Read-only here refers to access
15243 or permission status in the file system for the current user.
15244
15245 @item -aIDIR
15246 @cindex @option{-aIDIR} (@command{gnatfind})
15247 When looking for source files also look in directory DIR. The order in which
15248 source file search is undertaken is the same as for @command{gnatmake}.
15249
15250 @item -aODIR
15251 @cindex @option{-aODIR} (@command{gnatfind})
15252 When searching for library and object files, look in directory
15253 DIR. The order in which library files are searched is the same as for
15254 @command{gnatmake}.
15255
15256 @item -nostdinc
15257 @cindex @option{-nostdinc} (@command{gnatfind})
15258 Do not look for sources in the system default directory.
15259
15260 @item -nostdlib
15261 @cindex @option{-nostdlib} (@command{gnatfind})
15262 Do not look for library files in the system default directory.
15263
15264 @item --RTS=@var{rts-path}
15265 @cindex @option{--RTS} (@command{gnatfind})
15266 Specifies the default location of the runtime library. Same meaning as the
15267 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
15268
15269 @item ^-d^/DERIVED_TYPE_INFORMATION^
15270 @cindex @option{^-d^/DERIVED_TYPE_INFORMATION^} (@code{gnatfind})
15271 If this switch is set, then @code{gnatfind} will output the parent type
15272 reference for each matching derived types.
15273
15274 @item ^-e^/EXPRESSIONS^
15275 @cindex @option{^-e^/EXPRESSIONS^} (@command{gnatfind})
15276 By default, @code{gnatfind} accept the simple regular expression set for
15277 @samp{pattern}. If this switch is set, then the pattern will be
15278 considered as full Unix-style regular expression.
15279
15280 @item ^-f^/FULL_PATHNAME^
15281 @cindex @option{^-f^/FULL_PATHNAME^} (@command{gnatfind})
15282 If this switch is set, the output file names will be preceded by their
15283 directory (if the file was found in the search path). If this switch is
15284 not set, the directory will not be printed.
15285
15286 @item ^-g^/IGNORE_LOCALS^
15287 @cindex @option{^-g^/IGNORE_LOCALS^} (@command{gnatfind})
15288 If this switch is set, information is output only for library-level
15289 entities, ignoring local entities. The use of this switch may accelerate
15290 @code{gnatfind} and @code{gnatxref}.
15291
15292 @item -IDIR
15293 @cindex @option{-IDIR} (@command{gnatfind})
15294 Equivalent to @samp{-aODIR -aIDIR}.
15295
15296 @item -pFILE
15297 @cindex @option{-pFILE} (@command{gnatfind})
15298 Specify a project file (@pxref{Project Files}) to use.
15299 By default, @code{gnatxref} and @code{gnatfind} will try to locate a
15300 project file in the current directory.
15301
15302 If a project file is either specified or found by the tools, then the content
15303 of the source directory and object directory lines are added as if they
15304 had been specified respectively by @samp{^-aI^/SOURCE_SEARCH^} and
15305 @samp{^-aO^/OBJECT_SEARCH^}.
15306
15307 @item ^-r^/REFERENCES^
15308 @cindex @option{^-r^/REFERENCES^} (@command{gnatfind})
15309 By default, @code{gnatfind} will output only the information about the
15310 declaration, body or type completion of the entities. If this switch is
15311 set, the @code{gnatfind} will locate every reference to the entities in
15312 the files specified on the command line (or in every file in the search
15313 path if no file is given on the command line).
15314
15315 @item ^-s^/PRINT_LINES^
15316 @cindex @option{^-s^/PRINT_LINES^} (@command{gnatfind})
15317 If this switch is set, then @code{gnatfind} will output the content
15318 of the Ada source file lines were the entity was found.
15319
15320 @item ^-t^/TYPE_HIERARCHY^
15321 @cindex @option{^-t^/TYPE_HIERARCHY^} (@command{gnatfind})
15322 If this switch is set, then @code{gnatfind} will output the type hierarchy for
15323 the specified type. It act like -d option but recursively from parent
15324 type to parent type. When this switch is set it is not possible to
15325 specify more than one file.
15326
15327 @end table
15328
15329 @noindent
15330 All these switches may be in any order on the command line, and may even
15331 appear after the file names. They need not be separated by spaces, thus
15332 you can say @samp{gnatxref ^-ag^/ALL_FILES/IGNORE_LOCALS^} instead of
15333 @samp{gnatxref ^-a -g^/ALL_FILES /IGNORE_LOCALS^}.
15334
15335 As stated previously, gnatfind will search in every directory in the
15336 search path. You can force it to look only in the current directory if
15337 you specify @code{*} at the end of the command line.
15338
15339 @node Project Files for gnatxref and gnatfind
15340 @section Project Files for @command{gnatxref} and @command{gnatfind}
15341
15342 @noindent
15343 Project files allow a programmer to specify how to compile its
15344 application, where to find sources, etc. These files are used
15345 @ifclear vms
15346 primarily by GPS, but they can also be used
15347 @end ifclear
15348 by the two tools
15349 @code{gnatxref} and @code{gnatfind}.
15350
15351 A project file name must end with @file{.gpr}. If a single one is
15352 present in the current directory, then @code{gnatxref} and @code{gnatfind} will
15353 extract the information from it. If multiple project files are found, none of
15354 them is read, and you have to use the @samp{-p} switch to specify the one
15355 you want to use.
15356
15357 The following lines can be included, even though most of them have default
15358 values which can be used in most cases.
15359 The lines can be entered in any order in the file.
15360 Except for @file{src_dir} and @file{obj_dir}, you can only have one instance of
15361 each line. If you have multiple instances, only the last one is taken into
15362 account.
15363
15364 @table @code
15365 @item src_dir=DIR
15366 [default: @code{"^./^[]^"}]
15367 specifies a directory where to look for source files. Multiple @code{src_dir}
15368 lines can be specified and they will be searched in the order they
15369 are specified.
15370
15371 @item obj_dir=DIR
15372 [default: @code{"^./^[]^"}]
15373 specifies a directory where to look for object and library files. Multiple
15374 @code{obj_dir} lines can be specified, and they will be searched in the order
15375 they are specified
15376
15377 @item comp_opt=SWITCHES
15378 [default: @code{""}]
15379 creates a variable which can be referred to subsequently by using
15380 the @code{$@{comp_opt@}} notation. This is intended to store the default
15381 switches given to @command{gnatmake} and @command{gcc}.
15382
15383 @item bind_opt=SWITCHES
15384 [default: @code{""}]
15385 creates a variable which can be referred to subsequently by using
15386 the @samp{$@{bind_opt@}} notation. This is intended to store the default
15387 switches given to @command{gnatbind}.
15388
15389 @item link_opt=SWITCHES
15390 [default: @code{""}]
15391 creates a variable which can be referred to subsequently by using
15392 the @samp{$@{link_opt@}} notation. This is intended to store the default
15393 switches given to @command{gnatlink}.
15394
15395 @item main=EXECUTABLE
15396 [default: @code{""}]
15397 specifies the name of the executable for the application. This variable can
15398 be referred to in the following lines by using the @samp{$@{main@}} notation.
15399
15400 @ifset vms
15401 @item comp_cmd=COMMAND
15402 [default: @code{"GNAT COMPILE /SEARCH=$@{src_dir@} /DEBUG /TRY_SEMANTICS"}]
15403 @end ifset
15404 @ifclear vms
15405 @item comp_cmd=COMMAND
15406 [default: @code{"gcc -c -I$@{src_dir@} -g -gnatq"}]
15407 @end ifclear
15408 specifies the command used to compile a single file in the application.
15409
15410 @ifset vms
15411 @item make_cmd=COMMAND
15412 [default: @code{"GNAT MAKE $@{main@}
15413 /SOURCE_SEARCH=$@{src_dir@} /OBJECT_SEARCH=$@{obj_dir@}
15414 /DEBUG /TRY_SEMANTICS /COMPILER_QUALIFIERS $@{comp_opt@}
15415 /BINDER_QUALIFIERS $@{bind_opt@} /LINKER_QUALIFIERS $@{link_opt@}"}]
15416 @end ifset
15417 @ifclear vms
15418 @item make_cmd=COMMAND
15419 [default: @code{"gnatmake $@{main@} -aI$@{src_dir@}
15420 -aO$@{obj_dir@} -g -gnatq -cargs $@{comp_opt@}
15421 -bargs $@{bind_opt@} -largs $@{link_opt@}"}]
15422 @end ifclear
15423 specifies the command used to recompile the whole application.
15424
15425 @item run_cmd=COMMAND
15426 [default: @code{"$@{main@}"}]
15427 specifies the command used to run the application.
15428
15429 @item debug_cmd=COMMAND
15430 [default: @code{"gdb $@{main@}"}]
15431 specifies the command used to debug the application
15432
15433 @end table
15434
15435 @noindent
15436 @command{gnatxref} and @command{gnatfind} only take into account the
15437 @code{src_dir} and @code{obj_dir} lines, and ignore the others.
15438
15439 @node Regular Expressions in gnatfind and gnatxref
15440 @section Regular Expressions in @code{gnatfind} and @code{gnatxref}
15441
15442 @noindent
15443 As specified in the section about @command{gnatfind}, the pattern can be a
15444 regular expression. Actually, there are to set of regular expressions
15445 which are recognized by the program:
15446
15447 @table @code
15448 @item globbing patterns
15449 These are the most usual regular expression. They are the same that you
15450 generally used in a Unix shell command line, or in a DOS session.
15451
15452 Here is a more formal grammar:
15453 @smallexample
15454 @group
15455 @iftex
15456 @leftskip=.5cm
15457 @end iftex
15458 regexp ::= term
15459 term ::= elmt -- matches elmt
15460 term ::= elmt elmt -- concatenation (elmt then elmt)
15461 term ::= * -- any string of 0 or more characters
15462 term ::= ? -- matches any character
15463 term ::= [char @{char@}] -- matches any character listed
15464 term ::= [char - char] -- matches any character in range
15465 @end group
15466 @end smallexample
15467
15468 @item full regular expression
15469 The second set of regular expressions is much more powerful. This is the
15470 type of regular expressions recognized by utilities such a @file{grep}.
15471
15472 The following is the form of a regular expression, expressed in Ada
15473 reference manual style BNF is as follows
15474
15475 @smallexample
15476 @iftex
15477 @leftskip=.5cm
15478 @end iftex
15479 @group
15480 regexp ::= term @{| term@} -- alternation (term or term @dots{})
15481
15482 term ::= item @{item@} -- concatenation (item then item)
15483
15484 item ::= elmt -- match elmt
15485 item ::= elmt * -- zero or more elmt's
15486 item ::= elmt + -- one or more elmt's
15487 item ::= elmt ? -- matches elmt or nothing
15488 @end group
15489 @group
15490 elmt ::= nschar -- matches given character
15491 elmt ::= [nschar @{nschar@}] -- matches any character listed
15492 elmt ::= [^^^ nschar @{nschar@}] -- matches any character not listed
15493 elmt ::= [char - char] -- matches chars in given range
15494 elmt ::= \ char -- matches given character
15495 elmt ::= . -- matches any single character
15496 elmt ::= ( regexp ) -- parens used for grouping
15497
15498 char ::= any character, including special characters
15499 nschar ::= any character except ()[].*+?^^^
15500 @end group
15501 @end smallexample
15502
15503 Following are a few examples:
15504
15505 @table @samp
15506 @item abcde|fghi
15507 will match any of the two strings @samp{abcde} and @samp{fghi},
15508
15509 @item abc*d
15510 will match any string like @samp{abd}, @samp{abcd}, @samp{abccd},
15511 @samp{abcccd}, and so on,
15512
15513 @item [a-z]+
15514 will match any string which has only lowercase characters in it (and at
15515 least one character.
15516
15517 @end table
15518 @end table
15519
15520 @node Examples of gnatxref Usage
15521 @section Examples of @code{gnatxref} Usage
15522
15523 @subsection General Usage
15524
15525 @noindent
15526 For the following examples, we will consider the following units:
15527
15528 @smallexample @c ada
15529 @group
15530 @cartouche
15531 main.ads:
15532 1: with Bar;
15533 2: package Main is
15534 3: procedure Foo (B : in Integer);
15535 4: C : Integer;
15536 5: private
15537 6: D : Integer;
15538 7: end Main;
15539
15540 main.adb:
15541 1: package body Main is
15542 2: procedure Foo (B : in Integer) is
15543 3: begin
15544 4: C := B;
15545 5: D := B;
15546 6: Bar.Print (B);
15547 7: Bar.Print (C);
15548 8: end Foo;
15549 9: end Main;
15550
15551 bar.ads:
15552 1: package Bar is
15553 2: procedure Print (B : Integer);
15554 3: end bar;
15555 @end cartouche
15556 @end group
15557 @end smallexample
15558
15559 @table @code
15560
15561 @noindent
15562 The first thing to do is to recompile your application (for instance, in
15563 that case just by doing a @samp{gnatmake main}, so that GNAT generates
15564 the cross-referencing information.
15565 You can then issue any of the following commands:
15566
15567 @item gnatxref main.adb
15568 @code{gnatxref} generates cross-reference information for main.adb
15569 and every unit 'with'ed by main.adb.
15570
15571 The output would be:
15572 @smallexample
15573 @iftex
15574 @leftskip=0cm
15575 @end iftex
15576 B Type: Integer
15577 Decl: bar.ads 2:22
15578 B Type: Integer
15579 Decl: main.ads 3:20
15580 Body: main.adb 2:20
15581 Ref: main.adb 4:13 5:13 6:19
15582 Bar Type: Unit
15583 Decl: bar.ads 1:9
15584 Ref: main.adb 6:8 7:8
15585 main.ads 1:6
15586 C Type: Integer
15587 Decl: main.ads 4:5
15588 Modi: main.adb 4:8
15589 Ref: main.adb 7:19
15590 D Type: Integer
15591 Decl: main.ads 6:5
15592 Modi: main.adb 5:8
15593 Foo Type: Unit
15594 Decl: main.ads 3:15
15595 Body: main.adb 2:15
15596 Main Type: Unit
15597 Decl: main.ads 2:9
15598 Body: main.adb 1:14
15599 Print Type: Unit
15600 Decl: bar.ads 2:15
15601 Ref: main.adb 6:12 7:12
15602 @end smallexample
15603
15604 @noindent
15605 that is the entity @code{Main} is declared in main.ads, line 2, column 9,
15606 its body is in main.adb, line 1, column 14 and is not referenced any where.
15607
15608 The entity @code{Print} is declared in bar.ads, line 2, column 15 and it
15609 it referenced in main.adb, line 6 column 12 and line 7 column 12.
15610
15611 @item gnatxref package1.adb package2.ads
15612 @code{gnatxref} will generates cross-reference information for
15613 package1.adb, package2.ads and any other package 'with'ed by any
15614 of these.
15615
15616 @end table
15617
15618 @ifclear vms
15619 @subsection Using gnatxref with vi
15620
15621 @code{gnatxref} can generate a tags file output, which can be used
15622 directly from @command{vi}. Note that the standard version of @command{vi}
15623 will not work properly with overloaded symbols. Consider using another
15624 free implementation of @command{vi}, such as @command{vim}.
15625
15626 @smallexample
15627 $ gnatxref -v gnatfind.adb > tags
15628 @end smallexample
15629
15630 @noindent
15631 will generate the tags file for @code{gnatfind} itself (if the sources
15632 are in the search path!).
15633
15634 From @command{vi}, you can then use the command @samp{:tag @var{entity}}
15635 (replacing @var{entity} by whatever you are looking for), and vi will
15636 display a new file with the corresponding declaration of entity.
15637 @end ifclear
15638
15639 @node Examples of gnatfind Usage
15640 @section Examples of @code{gnatfind} Usage
15641
15642 @table @code
15643
15644 @item gnatfind ^-f^/FULL_PATHNAME^ xyz:main.adb
15645 Find declarations for all entities xyz referenced at least once in
15646 main.adb. The references are search in every library file in the search
15647 path.
15648
15649 The directories will be printed as well (as the @samp{^-f^/FULL_PATHNAME^}
15650 switch is set)
15651
15652 The output will look like:
15653 @smallexample
15654 ^directory/^[directory]^main.ads:106:14: xyz <= declaration
15655 ^directory/^[directory]^main.adb:24:10: xyz <= body
15656 ^directory/^[directory]^foo.ads:45:23: xyz <= declaration
15657 @end smallexample
15658
15659 @noindent
15660 that is to say, one of the entities xyz found in main.adb is declared at
15661 line 12 of main.ads (and its body is in main.adb), and another one is
15662 declared at line 45 of foo.ads
15663
15664 @item gnatfind ^-fs^/FULL_PATHNAME/SOURCE_LINE^ xyz:main.adb
15665 This is the same command as the previous one, instead @code{gnatfind} will
15666 display the content of the Ada source file lines.
15667
15668 The output will look like:
15669
15670 @smallexample
15671 ^directory/^[directory]^main.ads:106:14: xyz <= declaration
15672 procedure xyz;
15673 ^directory/^[directory]^main.adb:24:10: xyz <= body
15674 procedure xyz is
15675 ^directory/^[directory]^foo.ads:45:23: xyz <= declaration
15676 xyz : Integer;
15677 @end smallexample
15678
15679 @noindent
15680 This can make it easier to find exactly the location your are looking
15681 for.
15682
15683 @item gnatfind ^-r^/REFERENCES^ "*x*":main.ads:123 foo.adb
15684 Find references to all entities containing an x that are
15685 referenced on line 123 of main.ads.
15686 The references will be searched only in main.ads and foo.adb.
15687
15688 @item gnatfind main.ads:123
15689 Find declarations and bodies for all entities that are referenced on
15690 line 123 of main.ads.
15691
15692 This is the same as @code{gnatfind "*":main.adb:123}.
15693
15694 @item gnatfind ^mydir/^[mydir]^main.adb:123:45
15695 Find the declaration for the entity referenced at column 45 in
15696 line 123 of file main.adb in directory mydir. Note that it
15697 is usual to omit the identifier name when the column is given,
15698 since the column position identifies a unique reference.
15699
15700 The column has to be the beginning of the identifier, and should not
15701 point to any character in the middle of the identifier.
15702
15703 @end table
15704
15705 @c *********************************
15706 @node The GNAT Pretty-Printer gnatpp
15707 @chapter The GNAT Pretty-Printer @command{gnatpp}
15708 @findex gnatpp
15709 @cindex Pretty-Printer
15710
15711 @noindent
15712 ^The @command{gnatpp} tool^GNAT PRETTY^ is an ASIS-based utility
15713 for source reformatting / pretty-printing.
15714 It takes an Ada source file as input and generates a reformatted
15715 version as output.
15716 You can specify various style directives via switches; e.g.,
15717 identifier case conventions, rules of indentation, and comment layout.
15718
15719 To produce a reformatted file, @command{gnatpp} generates and uses the ASIS
15720 tree for the input source and thus requires the input to be syntactically and
15721 semantically legal.
15722 If this condition is not met, @command{gnatpp} will terminate with an
15723 error message; no output file will be generated.
15724
15725 If the source files presented to @command{gnatpp} contain
15726 preprocessing directives, then the output file will
15727 correspond to the generated source after all
15728 preprocessing is carried out. There is no way
15729 using @command{gnatpp} to obtain pretty printed files that
15730 include the preprocessing directives.
15731
15732 If the compilation unit
15733 contained in the input source depends semantically upon units located
15734 outside the current directory, you have to provide the source search path
15735 when invoking @command{gnatpp}, if these units are contained in files with
15736 names that do not follow the GNAT file naming rules, you have to provide
15737 the configuration file describing the corresponding naming scheme;
15738 see the description of the @command{gnatpp}
15739 switches below. Another possibility is to use a project file and to
15740 call @command{gnatpp} through the @command{gnat} driver
15741
15742 The @command{gnatpp} command has the form
15743
15744 @smallexample
15745 $ gnatpp @ovar{switches} @var{filename}
15746 @end smallexample
15747
15748 @noindent
15749 where
15750 @itemize @bullet
15751 @item
15752 @var{switches} is an optional sequence of switches defining such properties as
15753 the formatting rules, the source search path, and the destination for the
15754 output source file
15755
15756 @item
15757 @var{filename} is the name (including the extension) of the source file to
15758 reformat; ``wildcards'' or several file names on the same gnatpp command are
15759 allowed. The file name may contain path information; it does not have to
15760 follow the GNAT file naming rules
15761 @end itemize
15762
15763 @menu
15764 * Switches for gnatpp::
15765 * Formatting Rules::
15766 @end menu
15767
15768 @node Switches for gnatpp
15769 @section Switches for @command{gnatpp}
15770
15771 @noindent
15772 The following subsections describe the various switches accepted by
15773 @command{gnatpp}, organized by category.
15774
15775 @ifclear vms
15776 You specify a switch by supplying a name and generally also a value.
15777 In many cases the values for a switch with a given name are incompatible with
15778 each other
15779 (for example the switch that controls the casing of a reserved word may have
15780 exactly one value: upper case, lower case, or
15781 mixed case) and thus exactly one such switch can be in effect for an
15782 invocation of @command{gnatpp}.
15783 If more than one is supplied, the last one is used.
15784 However, some values for the same switch are mutually compatible.
15785 You may supply several such switches to @command{gnatpp}, but then
15786 each must be specified in full, with both the name and the value.
15787 Abbreviated forms (the name appearing once, followed by each value) are
15788 not permitted.
15789 For example, to set
15790 the alignment of the assignment delimiter both in declarations and in
15791 assignment statements, you must write @option{-A2A3}
15792 (or @option{-A2 -A3}), but not @option{-A23}.
15793 @end ifclear
15794
15795 @ifset vms
15796 In many cases the set of options for a given qualifier are incompatible with
15797 each other (for example the qualifier that controls the casing of a reserved
15798 word may have exactly one option, which specifies either upper case, lower
15799 case, or mixed case), and thus exactly one such option can be in effect for
15800 an invocation of @command{gnatpp}.
15801 If more than one is supplied, the last one is used.
15802 However, some qualifiers have options that are mutually compatible,
15803 and then you may then supply several such options when invoking
15804 @command{gnatpp}.
15805 @end ifset
15806
15807 In most cases, it is obvious whether or not the
15808 ^values for a switch with a given name^options for a given qualifier^
15809 are compatible with each other.
15810 When the semantics might not be evident, the summaries below explicitly
15811 indicate the effect.
15812
15813 @menu
15814 * Alignment Control::
15815 * Casing Control::
15816 * Construct Layout Control::
15817 * General Text Layout Control::
15818 * Other Formatting Options::
15819 * Setting the Source Search Path::
15820 * Output File Control::
15821 * Other gnatpp Switches::
15822 @end menu
15823
15824 @node Alignment Control
15825 @subsection Alignment Control
15826 @cindex Alignment control in @command{gnatpp}
15827
15828 @noindent
15829 Programs can be easier to read if certain constructs are vertically aligned.
15830 By default all alignments are set ON.
15831 Through the @option{^-A0^/ALIGN=OFF^} switch you may reset the default to
15832 OFF, and then use one or more of the other
15833 ^@option{-A@var{n}} switches^@option{/ALIGN} options^
15834 to activate alignment for specific constructs.
15835
15836 @table @option
15837 @cindex @option{^-A@var{n}^/ALIGN^} (@command{gnatpp})
15838
15839 @ifset vms
15840 @item /ALIGN=ON
15841 Set all alignments to ON
15842 @end ifset
15843
15844 @item ^-A0^/ALIGN=OFF^
15845 Set all alignments to OFF
15846
15847 @item ^-A1^/ALIGN=COLONS^
15848 Align @code{:} in declarations
15849
15850 @item ^-A2^/ALIGN=DECLARATIONS^
15851 Align @code{:=} in initializations in declarations
15852
15853 @item ^-A3^/ALIGN=STATEMENTS^
15854 Align @code{:=} in assignment statements
15855
15856 @item ^-A4^/ALIGN=ARROWS^
15857 Align @code{=>} in associations
15858
15859 @item ^-A5^/ALIGN=COMPONENT_CLAUSES^
15860 Align @code{at} keywords in the component clauses in record
15861 representation clauses
15862 @end table
15863
15864 @noindent
15865 The @option{^-A^/ALIGN^} switches are mutually compatible; any combination
15866 is allowed.
15867
15868 @node Casing Control
15869 @subsection Casing Control
15870 @cindex Casing control in @command{gnatpp}
15871
15872 @noindent
15873 @command{gnatpp} allows you to specify the casing for reserved words,
15874 pragma names, attribute designators and identifiers.
15875 For identifiers you may define a
15876 general rule for name casing but also override this rule
15877 via a set of dictionary files.
15878
15879 Three types of casing are supported: lower case, upper case, and mixed case.
15880 Lower and upper case are self-explanatory (but since some letters in
15881 Latin1 and other GNAT-supported character sets
15882 exist only in lower-case form, an upper case conversion will have no
15883 effect on them.)
15884 ``Mixed case'' means that the first letter, and also each letter immediately
15885 following an underscore, are converted to their uppercase forms;
15886 all the other letters are converted to their lowercase forms.
15887
15888 @table @option
15889 @cindex @option{^-a@var{x}^/ATTRIBUTE^} (@command{gnatpp})
15890 @item ^-aL^/ATTRIBUTE_CASING=LOWER_CASE^
15891 Attribute designators are lower case
15892
15893 @item ^-aU^/ATTRIBUTE_CASING=UPPER_CASE^
15894 Attribute designators are upper case
15895
15896 @item ^-aM^/ATTRIBUTE_CASING=MIXED_CASE^
15897 Attribute designators are mixed case (this is the default)
15898
15899 @cindex @option{^-k@var{x}^/KEYWORD_CASING^} (@command{gnatpp})
15900 @item ^-kL^/KEYWORD_CASING=LOWER_CASE^
15901 Keywords (technically, these are known in Ada as @emph{reserved words}) are
15902 lower case (this is the default)
15903
15904 @item ^-kU^/KEYWORD_CASING=UPPER_CASE^
15905 Keywords are upper case
15906
15907 @cindex @option{^-n@var{x}^/NAME_CASING^} (@command{gnatpp})
15908 @item ^-nD^/NAME_CASING=AS_DECLARED^
15909 Name casing for defining occurrences are as they appear in the source file
15910 (this is the default)
15911
15912 @item ^-nU^/NAME_CASING=UPPER_CASE^
15913 Names are in upper case
15914
15915 @item ^-nL^/NAME_CASING=LOWER_CASE^
15916 Names are in lower case
15917
15918 @item ^-nM^/NAME_CASING=MIXED_CASE^
15919 Names are in mixed case
15920
15921 @cindex @option{^-p@var{x}^/PRAGMA_CASING^} (@command{gnatpp})
15922 @item ^-pL^/PRAGMA_CASING=LOWER_CASE^
15923 Pragma names are lower case
15924
15925 @item ^-pU^/PRAGMA_CASING=UPPER_CASE^
15926 Pragma names are upper case
15927
15928 @item ^-pM^/PRAGMA_CASING=MIXED_CASE^
15929 Pragma names are mixed case (this is the default)
15930
15931 @item ^-D@var{file}^/DICTIONARY=@var{file}^
15932 @cindex @option{^-D^/DICTIONARY^} (@command{gnatpp})
15933 Use @var{file} as a @emph{dictionary file} that defines
15934 the casing for a set of specified names,
15935 thereby overriding the effect on these names by
15936 any explicit or implicit
15937 ^-n^/NAME_CASING^ switch.
15938 To supply more than one dictionary file,
15939 use ^several @option{-D} switches^a list of files as options^.
15940
15941 @noindent
15942 @option{gnatpp} implicitly uses a @emph{default dictionary file}
15943 to define the casing for the Ada predefined names and
15944 the names declared in the GNAT libraries.
15945
15946 @item ^-D-^/SPECIFIC_CASING^
15947 @cindex @option{^-D-^/SPECIFIC_CASING^} (@command{gnatpp})
15948 Do not use the default dictionary file;
15949 instead, use the casing
15950 defined by a @option{^-n^/NAME_CASING^} switch and any explicit
15951 dictionary file(s)
15952 @end table
15953
15954 @noindent
15955 The structure of a dictionary file, and details on the conventions
15956 used in the default dictionary file, are defined in @ref{Name Casing}.
15957
15958 The @option{^-D-^/SPECIFIC_CASING^} and
15959 @option{^-D@var{file}^/DICTIONARY=@var{file}^} switches are mutually
15960 compatible.
15961
15962 @node Construct Layout Control
15963 @subsection Construct Layout Control
15964 @cindex Layout control in @command{gnatpp}
15965
15966 @noindent
15967 This group of @command{gnatpp} switches controls the layout of comments and
15968 complex syntactic constructs. See @ref{Formatting Comments} for details
15969 on their effect.
15970
15971 @table @option
15972 @cindex @option{^-c@var{n}^/COMMENTS_LAYOUT^} (@command{gnatpp})
15973 @item ^-c0^/COMMENTS_LAYOUT=UNTOUCHED^
15974 All the comments remain unchanged
15975
15976 @item ^-c1^/COMMENTS_LAYOUT=DEFAULT^
15977 GNAT-style comment line indentation (this is the default).
15978
15979 @item ^-c2^/COMMENTS_LAYOUT=STANDARD_INDENT^
15980 Reference-manual comment line indentation.
15981
15982 @item ^-c3^/COMMENTS_LAYOUT=GNAT_BEGINNING^
15983 GNAT-style comment beginning
15984
15985 @item ^-c4^/COMMENTS_LAYOUT=REFORMAT^
15986 Reformat comment blocks
15987
15988 @item ^-c5^/COMMENTS_LAYOUT=KEEP_SPECIAL^
15989 Keep unchanged special form comments
15990
15991 Reformat comment blocks
15992
15993 @cindex @option{^-l@var{n}^/CONSTRUCT_LAYOUT^} (@command{gnatpp})
15994 @item ^-l1^/CONSTRUCT_LAYOUT=GNAT^
15995 GNAT-style layout (this is the default)
15996
15997 @item ^-l2^/CONSTRUCT_LAYOUT=COMPACT^
15998 Compact layout
15999
16000 @item ^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^
16001 Uncompact layout
16002
16003 @cindex @option{^-N^/NOTABS^} (@command{gnatpp})
16004 @item ^-N^/NOTABS^
16005 All the VT characters are removed from the comment text. All the HT characters
16006 are expanded with the sequences of space characters to get to the next tab
16007 stops.
16008
16009 @cindex @option{^--no-separate-is^/NO_SEPARATE_IS^} (@command{gnatpp})
16010 @item ^--no-separate-is^/NO_SEPARATE_IS^
16011 Do not place the keyword @code{is} on a separate line in a subprogram body in
16012 case if the spec occupies more then one line.
16013
16014 @cindex @option{^--separate-loop-then^/SEPARATE_LOOP_THEN^} (@command{gnatpp})
16015 @item ^--separate-loop-then^/SEPARATE_LOOP_THEN^
16016 Place the keyword @code{loop} in FOR and WHILE loop statements and the
16017 keyword @code{then} in IF statements on a separate line.
16018
16019 @cindex @option{^--no-separate-loop-then^/NO_SEPARATE_LOOP_THEN^} (@command{gnatpp})
16020 @item ^--no-separate-loop-then^/NO_SEPARATE_LOOP_THEN^
16021 Do not place the keyword @code{loop} in FOR and WHILE loop statements and the
16022 keyword @code{then} in IF statements on a separate line. This option is
16023 incompatible with @option{^--separate-loop-then^/SEPARATE_LOOP_THEN^} option.
16024
16025 @cindex @option{^--use-on-new-line^/USE_ON_NEW_LINE^} (@command{gnatpp})
16026 @item ^--use-on-new-line^/USE_ON_NEW_LINE^
16027 Start each USE clause in a context clause from a separate line.
16028
16029 @cindex @option{^--separate-stmt-name^/STMT_NAME_ON_NEW_LINE^} (@command{gnatpp})
16030 @item ^--separate-stmt-name^/STMT_NAME_ON_NEW_LINE^
16031 Use a separate line for a loop or block statement name, but do not use an extra
16032 indentation level for the statement itself.
16033
16034 @end table
16035
16036 @ifclear vms
16037 @noindent
16038 The @option{-c1} and @option{-c2} switches are incompatible.
16039 The @option{-c3} and @option{-c4} switches are compatible with each other and
16040 also with @option{-c1} and @option{-c2}. The @option{-c0} switch disables all
16041 the other comment formatting switches.
16042
16043 The @option{-l1}, @option{-l2}, and @option{-l3} switches are incompatible.
16044 @end ifclear
16045
16046 @ifset vms
16047 @noindent
16048 For the @option{/COMMENTS_LAYOUT} qualifier:
16049 @itemize @bullet
16050 @item
16051 The @option{DEFAULT} and @option{STANDARD_INDENT} options are incompatible.
16052 @item
16053 The @option{GNAT_BEGINNING} and @option{REFORMAT} options are compatible with
16054 each other and also with @option{DEFAULT} and @option{STANDARD_INDENT}.
16055 @end itemize
16056
16057 @noindent
16058 The @option{GNAT}, @option{COMPACT}, and @option{UNCOMPACT} options for the
16059 @option{/CONSTRUCT_LAYOUT} qualifier are incompatible.
16060 @end ifset
16061
16062 @node General Text Layout Control
16063 @subsection General Text Layout Control
16064
16065 @noindent
16066 These switches allow control over line length and indentation.
16067
16068 @table @option
16069 @item ^-M@var{nnn}^/LINE_LENGTH_MAX=@var{nnn}^
16070 @cindex @option{^-M^/LINE_LENGTH^} (@command{gnatpp})
16071 Maximum line length, @var{nnn} from 32@dots{}256, the default value is 79
16072
16073 @item ^-i@var{nnn}^/INDENTATION_LEVEL=@var{nnn}^
16074 @cindex @option{^-i^/INDENTATION_LEVEL^} (@command{gnatpp})
16075 Indentation level, @var{nnn} from 1@dots{}9, the default value is 3
16076
16077 @item ^-cl@var{nnn}^/CONTINUATION_INDENT=@var{nnn}^
16078 @cindex @option{^-cl^/CONTINUATION_INDENT^} (@command{gnatpp})
16079 Indentation level for continuation lines (relative to the line being
16080 continued), @var{nnn} from 1@dots{}9.
16081 The default
16082 value is one less then the (normal) indentation level, unless the
16083 indentation is set to 1 (in which case the default value for continuation
16084 line indentation is also 1)
16085 @end table
16086
16087 @node Other Formatting Options
16088 @subsection Other Formatting Options
16089
16090 @noindent
16091 These switches control the inclusion of missing end/exit labels, and
16092 the indentation level in @b{case} statements.
16093
16094 @table @option
16095 @item ^-e^/NO_MISSED_LABELS^
16096 @cindex @option{^-e^/NO_MISSED_LABELS^} (@command{gnatpp})
16097 Do not insert missing end/exit labels. An end label is the name of
16098 a construct that may optionally be repeated at the end of the
16099 construct's declaration;
16100 e.g., the names of packages, subprograms, and tasks.
16101 An exit label is the name of a loop that may appear as target
16102 of an exit statement within the loop.
16103 By default, @command{gnatpp} inserts these end/exit labels when
16104 they are absent from the original source. This option suppresses such
16105 insertion, so that the formatted source reflects the original.
16106
16107 @item ^-ff^/FORM_FEED_AFTER_PRAGMA_PAGE^
16108 @cindex @option{^-ff^/FORM_FEED_AFTER_PRAGMA_PAGE^} (@command{gnatpp})
16109 Insert a Form Feed character after a pragma Page.
16110
16111 @item ^-T@var{nnn}^/MAX_INDENT=@var{nnn}^
16112 @cindex @option{^-T^/MAX_INDENT^} (@command{gnatpp})
16113 Do not use an additional indentation level for @b{case} alternatives
16114 and variants if there are @var{nnn} or more (the default
16115 value is 10).
16116 If @var{nnn} is 0, an additional indentation level is
16117 used for @b{case} alternatives and variants regardless of their number.
16118 @end table
16119
16120 @node Setting the Source Search Path
16121 @subsection Setting the Source Search Path
16122
16123 @noindent
16124 To define the search path for the input source file, @command{gnatpp}
16125 uses the same switches as the GNAT compiler, with the same effects.
16126
16127 @table @option
16128 @item ^-I^/SEARCH=^@var{dir}
16129 @cindex @option{^-I^/SEARCH^} (@code{gnatpp})
16130 The same as the corresponding gcc switch
16131
16132 @item ^-I-^/NOCURRENT_DIRECTORY^
16133 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatpp})
16134 The same as the corresponding gcc switch
16135
16136 @item ^-gnatec^/CONFIGURATION_PRAGMAS_FILE^=@var{path}
16137 @cindex @option{^-gnatec^/CONFIGURATION_PRAGMAS_FILE^} (@code{gnatpp})
16138 The same as the corresponding gcc switch
16139
16140 @item ^--RTS^/RUNTIME_SYSTEM^=@var{path}
16141 @cindex @option{^--RTS^/RUNTIME_SYSTEM^} (@code{gnatpp})
16142 The same as the corresponding gcc switch
16143
16144 @end table
16145
16146 @node Output File Control
16147 @subsection Output File Control
16148
16149 @noindent
16150 By default the output is sent to the file whose name is obtained by appending
16151 the ^@file{.pp}^@file{$PP}^ suffix to the name of the input file
16152 (if the file with this name already exists, it is unconditionally overwritten).
16153 Thus if the input file is @file{^my_ada_proc.adb^MY_ADA_PROC.ADB^} then
16154 @command{gnatpp} will produce @file{^my_ada_proc.adb.pp^MY_ADA_PROC.ADB$PP^}
16155 as output file.
16156 The output may be redirected by the following switches:
16157
16158 @table @option
16159 @item ^-pipe^/STANDARD_OUTPUT^
16160 @cindex @option{^-pipe^/STANDARD_OUTPUT^} (@code{gnatpp})
16161 Send the output to @code{Standard_Output}
16162
16163 @item ^-o @var{output_file}^/OUTPUT=@var{output_file}^
16164 @cindex @option{^-o^/OUTPUT^} (@code{gnatpp})
16165 Write the output into @var{output_file}.
16166 If @var{output_file} already exists, @command{gnatpp} terminates without
16167 reading or processing the input file.
16168
16169 @item ^-of ^/FORCED_OUTPUT=^@var{output_file}
16170 @cindex @option{^-of^/FORCED_OUTPUT^} (@code{gnatpp})
16171 Write the output into @var{output_file}, overwriting the existing file
16172 (if one is present).
16173
16174 @item ^-r^/REPLACE^
16175 @cindex @option{^-r^/REPLACE^} (@code{gnatpp})
16176 Replace the input source file with the reformatted output, and copy the
16177 original input source into the file whose name is obtained by appending the
16178 ^@file{.npp}^@file{$NPP}^ suffix to the name of the input file.
16179 If a file with this name already exists, @command{gnatpp} terminates without
16180 reading or processing the input file.
16181
16182 @item ^-rf^/OVERRIDING_REPLACE^
16183 @cindex @option{^-rf^/OVERRIDING_REPLACE^} (@code{gnatpp})
16184 Like @option{^-r^/REPLACE^} except that if the file with the specified name
16185 already exists, it is overwritten.
16186
16187 @item ^-rnb^/REPLACE_NO_BACKUP^
16188 @cindex @option{^-rnb^/REPLACE_NO_BACKUP^} (@code{gnatpp})
16189 Replace the input source file with the reformatted output without
16190 creating any backup copy of the input source.
16191
16192 @item ^--eol=@var{xxx}^/END_OF_LINE=@var{xxx}^
16193 @cindex @option{^--eol^/END_OF_LINE^} (@code{gnatpp})
16194 Specifies the format of the reformatted output file. The @var{xxx}
16195 ^string specified with the switch^option^ may be either
16196 @itemize @bullet
16197 @item ``@option{^dos^DOS^}'' MS DOS style, lines end with CR LF characters
16198 @item ``@option{^crlf^CRLF^}''
16199 the same as @option{^crlf^CRLF^}
16200 @item ``@option{^unix^UNIX^}'' UNIX style, lines end with LF character
16201 @item ``@option{^lf^LF^}''
16202 the same as @option{^unix^UNIX^}
16203 @end itemize
16204
16205 @item ^-W^/RESULT_ENCODING=^@var{e}
16206 @cindex @option{^-W^/RESULT_ENCODING=^} (@command{gnatpp})
16207 Specify the wide character encoding method used to write the code in the
16208 result file
16209 @var{e} is one of the following:
16210
16211 @itemize @bullet
16212
16213 @item ^h^HEX^
16214 Hex encoding
16215
16216 @item ^u^UPPER^
16217 Upper half encoding
16218
16219 @item ^s^SHIFT_JIS^
16220 Shift/JIS encoding
16221
16222 @item ^e^EUC^
16223 EUC encoding
16224
16225 @item ^8^UTF8^
16226 UTF-8 encoding
16227
16228 @item ^b^BRACKETS^
16229 Brackets encoding (default value)
16230 @end itemize
16231
16232 @end table
16233
16234 @noindent
16235 Options @option{^-pipe^/STANDARD_OUTPUT^},
16236 @option{^-o^/OUTPUT^} and
16237 @option{^-of^/FORCED_OUTPUT^} are allowed only if the call to gnatpp
16238 contains only one file to reformat.
16239 Option
16240 @option{^--eol^/END_OF_LINE^}
16241 and
16242 @option{^-W^/RESULT_ENCODING^}
16243 cannot be used together
16244 with @option{^-pipe^/STANDARD_OUTPUT^} option.
16245
16246 @node Other gnatpp Switches
16247 @subsection Other @code{gnatpp} Switches
16248
16249 @noindent
16250 The additional @command{gnatpp} switches are defined in this subsection.
16251
16252 @table @option
16253 @item ^-files @var{filename}^/FILES=@var{output_file}^
16254 @cindex @option{^-files^/FILES^} (@code{gnatpp})
16255 Take the argument source files from the specified file. This file should be an
16256 ordinary textual file containing file names separated by spaces or
16257 line breaks. You can use this switch more then once in the same call to
16258 @command{gnatpp}. You also can combine this switch with explicit list of
16259 files.
16260
16261 @item ^-v^/VERBOSE^
16262 @cindex @option{^-v^/VERBOSE^} (@code{gnatpp})
16263 Verbose mode;
16264 @command{gnatpp} generates version information and then
16265 a trace of the actions it takes to produce or obtain the ASIS tree.
16266
16267 @item ^-w^/WARNINGS^
16268 @cindex @option{^-w^/WARNINGS^} (@code{gnatpp})
16269 Warning mode;
16270 @command{gnatpp} generates a warning whenever it cannot provide
16271 a required layout in the result source.
16272 @end table
16273
16274 @node Formatting Rules
16275 @section Formatting Rules
16276
16277 @noindent
16278 The following subsections show how @command{gnatpp} treats ``white space'',
16279 comments, program layout, and name casing.
16280 They provide the detailed descriptions of the switches shown above.
16281
16282 @menu
16283 * White Space and Empty Lines::
16284 * Formatting Comments::
16285 * Construct Layout::
16286 * Name Casing::
16287 @end menu
16288
16289 @node White Space and Empty Lines
16290 @subsection White Space and Empty Lines
16291
16292 @noindent
16293 @command{gnatpp} does not have an option to control space characters.
16294 It will add or remove spaces according to the style illustrated by the
16295 examples in the @cite{Ada Reference Manual}.
16296
16297 The only format effectors
16298 (see @cite{Ada Reference Manual}, paragraph 2.1(13))
16299 that will appear in the output file are platform-specific line breaks,
16300 and also format effectors within (but not at the end of) comments.
16301 In particular, each horizontal tab character that is not inside
16302 a comment will be treated as a space and thus will appear in the
16303 output file as zero or more spaces depending on
16304 the reformatting of the line in which it appears.
16305 The only exception is a Form Feed character, which is inserted after a
16306 pragma @code{Page} when @option{-ff} is set.
16307
16308 The output file will contain no lines with trailing ``white space'' (spaces,
16309 format effectors).
16310
16311 Empty lines in the original source are preserved
16312 only if they separate declarations or statements.
16313 In such contexts, a
16314 sequence of two or more empty lines is replaced by exactly one empty line.
16315 Note that a blank line will be removed if it separates two ``comment blocks''
16316 (a comment block is a sequence of whole-line comments).
16317 In order to preserve a visual separation between comment blocks, use an
16318 ``empty comment'' (a line comprising only hyphens) rather than an empty line.
16319 Likewise, if for some reason you wish to have a sequence of empty lines,
16320 use a sequence of empty comments instead.
16321
16322 @node Formatting Comments
16323 @subsection Formatting Comments
16324
16325 @noindent
16326 Comments in Ada code are of two kinds:
16327 @itemize @bullet
16328 @item
16329 a @emph{whole-line comment}, which appears by itself (possibly preceded by
16330 ``white space'') on a line
16331
16332 @item
16333 an @emph{end-of-line comment}, which follows some other Ada lexical element
16334 on the same line.
16335 @end itemize
16336
16337 @noindent
16338 The indentation of a whole-line comment is that of either
16339 the preceding or following line in
16340 the formatted source, depending on switch settings as will be described below.
16341
16342 For an end-of-line comment, @command{gnatpp} leaves the same number of spaces
16343 between the end of the preceding Ada lexical element and the beginning
16344 of the comment as appear in the original source,
16345 unless either the comment has to be split to
16346 satisfy the line length limitation, or else the next line contains a
16347 whole line comment that is considered a continuation of this end-of-line
16348 comment (because it starts at the same position).
16349 In the latter two
16350 cases, the start of the end-of-line comment is moved right to the nearest
16351 multiple of the indentation level.
16352 This may result in a ``line overflow'' (the right-shifted comment extending
16353 beyond the maximum line length), in which case the comment is split as
16354 described below.
16355
16356 There is a difference between @option{^-c1^/COMMENTS_LAYOUT=DEFAULT^}
16357 (GNAT-style comment line indentation)
16358 and @option{^-c2^/COMMENTS_LAYOUT=STANDARD_INDENT^}
16359 (reference-manual comment line indentation).
16360 With reference-manual style, a whole-line comment is indented as if it
16361 were a declaration or statement at the same place
16362 (i.e., according to the indentation of the preceding line(s)).
16363 With GNAT style, a whole-line comment that is immediately followed by an
16364 @b{if} or @b{case} statement alternative, a record variant, or the reserved
16365 word @b{begin}, is indented based on the construct that follows it.
16366
16367 For example:
16368 @smallexample @c ada
16369 @cartouche
16370 if A then
16371 null;
16372 -- some comment
16373 else
16374 null;
16375 end if;
16376 @end cartouche
16377 @end smallexample
16378
16379 @noindent
16380 Reference-manual indentation produces:
16381
16382 @smallexample @c ada
16383 @cartouche
16384 if A then
16385 null;
16386 -- some comment
16387 else
16388 null;
16389 end if;
16390 @end cartouche
16391 @end smallexample
16392
16393 @noindent
16394 while GNAT-style indentation produces:
16395
16396 @smallexample @c ada
16397 @cartouche
16398 if A then
16399 null;
16400 -- some comment
16401 else
16402 null;
16403 end if;
16404 @end cartouche
16405 @end smallexample
16406
16407 @noindent
16408 The @option{^-c3^/COMMENTS_LAYOUT=GNAT_BEGINNING^} switch
16409 (GNAT style comment beginning) has the following
16410 effect:
16411
16412 @itemize @bullet
16413 @item
16414 For each whole-line comment that does not end with two hyphens,
16415 @command{gnatpp} inserts spaces if necessary after the starting two hyphens
16416 to ensure that there are at least two spaces between these hyphens and the
16417 first non-blank character of the comment.
16418 @end itemize
16419
16420 @noindent
16421 For an end-of-line comment, if in the original source the next line is a
16422 whole-line comment that starts at the same position
16423 as the end-of-line comment,
16424 then the whole-line comment (and all whole-line comments
16425 that follow it and that start at the same position)
16426 will start at this position in the output file.
16427
16428 @noindent
16429 That is, if in the original source we have:
16430
16431 @smallexample @c ada
16432 @cartouche
16433 begin
16434 A := B + C; -- B must be in the range Low1..High1
16435 -- C must be in the range Low2..High2
16436 --B+C will be in the range Low1+Low2..High1+High2
16437 X := X + 1;
16438 @end cartouche
16439 @end smallexample
16440
16441 @noindent
16442 Then in the formatted source we get
16443
16444 @smallexample @c ada
16445 @cartouche
16446 begin
16447 A := B + C; -- B must be in the range Low1..High1
16448 -- C must be in the range Low2..High2
16449 -- B+C will be in the range Low1+Low2..High1+High2
16450 X := X + 1;
16451 @end cartouche
16452 @end smallexample
16453
16454 @noindent
16455 A comment that exceeds the line length limit will be split.
16456 Unless switch
16457 @option{^-c4^/COMMENTS_LAYOUT=REFORMAT^} (reformat comment blocks) is set and
16458 the line belongs to a reformattable block, splitting the line generates a
16459 @command{gnatpp} warning.
16460 The @option{^-c4^/COMMENTS_LAYOUT=REFORMAT^} switch specifies that whole-line
16461 comments may be reformatted in typical
16462 word processor style (that is, moving words between lines and putting as
16463 many words in a line as possible).
16464
16465 @noindent
16466 The @option{^-c5^/COMMENTS_LAYOUT=KEEP_SPECIAL^} switch specifies, that comments
16467 that has a special format (that is, a character that is neither a letter nor digit
16468 not white space nor line break immediately following the leading @code{--} of
16469 the comment) should be without any change moved from the argument source
16470 into reformatted source. This switch allows to preserve comments that are used
16471 as a special marks in the code (e.g.@: SPARK annotation).
16472
16473 @node Construct Layout
16474 @subsection Construct Layout
16475
16476 @noindent
16477 In several cases the suggested layout in the Ada Reference Manual includes
16478 an extra level of indentation that many programmers prefer to avoid. The
16479 affected cases include:
16480
16481 @itemize @bullet
16482
16483 @item Record type declaration (RM 3.8)
16484
16485 @item Record representation clause (RM 13.5.1)
16486
16487 @item Loop statement in case if a loop has a statement identifier (RM 5.6)
16488
16489 @item Block statement in case if a block has a statement identifier (RM 5.6)
16490 @end itemize
16491
16492 @noindent
16493 In compact mode (when GNAT style layout or compact layout is set),
16494 the pretty printer uses one level of indentation instead
16495 of two. This is achieved in the record definition and record representation
16496 clause cases by putting the @code{record} keyword on the same line as the
16497 start of the declaration or representation clause, and in the block and loop
16498 case by putting the block or loop header on the same line as the statement
16499 identifier.
16500
16501 @noindent
16502 The difference between GNAT style @option{^-l1^/CONSTRUCT_LAYOUT=GNAT^}
16503 and compact @option{^-l2^/CONSTRUCT_LAYOUT=COMPACT^}
16504 layout on the one hand, and uncompact layout
16505 @option{^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^} on the other hand,
16506 can be illustrated by the following examples:
16507
16508 @iftex
16509 @cartouche
16510 @multitable @columnfractions .5 .5
16511 @item @i{GNAT style, compact layout} @tab @i{Uncompact layout}
16512
16513 @item
16514 @smallexample @c ada
16515 type q is record
16516 a : integer;
16517 b : integer;
16518 end record;
16519 @end smallexample
16520 @tab
16521 @smallexample @c ada
16522 type q is
16523 record
16524 a : integer;
16525 b : integer;
16526 end record;
16527 @end smallexample
16528
16529 @item
16530 @smallexample @c ada
16531 for q use record
16532 a at 0 range 0 .. 31;
16533 b at 4 range 0 .. 31;
16534 end record;
16535 @end smallexample
16536 @tab
16537 @smallexample @c ada
16538 for q use
16539 record
16540 a at 0 range 0 .. 31;
16541 b at 4 range 0 .. 31;
16542 end record;
16543 @end smallexample
16544
16545 @item
16546 @smallexample @c ada
16547 Block : declare
16548 A : Integer := 3;
16549 begin
16550 Proc (A, A);
16551 end Block;
16552 @end smallexample
16553 @tab
16554 @smallexample @c ada
16555 Block :
16556 declare
16557 A : Integer := 3;
16558 begin
16559 Proc (A, A);
16560 end Block;
16561 @end smallexample
16562
16563 @item
16564 @smallexample @c ada
16565 Clear : for J in 1 .. 10 loop
16566 A (J) := 0;
16567 end loop Clear;
16568 @end smallexample
16569 @tab
16570 @smallexample @c ada
16571 Clear :
16572 for J in 1 .. 10 loop
16573 A (J) := 0;
16574 end loop Clear;
16575 @end smallexample
16576 @end multitable
16577 @end cartouche
16578 @end iftex
16579
16580 @ifnottex
16581 @smallexample
16582 @cartouche
16583 GNAT style, compact layout Uncompact layout
16584
16585 type q is record type q is
16586 a : integer; record
16587 b : integer; a : integer;
16588 end record; b : integer;
16589 end record;
16590
16591 for q use record for q use
16592 a at 0 range 0 .. 31; record
16593 b at 4 range 0 .. 31; a at 0 range 0 .. 31;
16594 end record; b at 4 range 0 .. 31;
16595 end record;
16596
16597 Block : declare Block :
16598 A : Integer := 3; declare
16599 begin A : Integer := 3;
16600 Proc (A, A); begin
16601 end Block; Proc (A, A);
16602 end Block;
16603
16604 Clear : for J in 1 .. 10 loop Clear :
16605 A (J) := 0; for J in 1 .. 10 loop
16606 end loop Clear; A (J) := 0;
16607 end loop Clear;
16608 @end cartouche
16609 @end smallexample
16610 @end ifnottex
16611
16612 @noindent
16613 A further difference between GNAT style layout and compact layout is that
16614 GNAT style layout inserts empty lines as separation for
16615 compound statements, return statements and bodies.
16616
16617 Note that the layout specified by
16618 @option{^--separate-stmt-name^/STMT_NAME_ON_NEW_LINE^}
16619 for named block and loop statements overrides the layout defined by these
16620 constructs by @option{^-l1^/CONSTRUCT_LAYOUT=GNAT^},
16621 @option{^-l2^/CONSTRUCT_LAYOUT=COMPACT^} or
16622 @option{^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^} option.
16623
16624 @node Name Casing
16625 @subsection Name Casing
16626
16627 @noindent
16628 @command{gnatpp} always converts the usage occurrence of a (simple) name to
16629 the same casing as the corresponding defining identifier.
16630
16631 You control the casing for defining occurrences via the
16632 @option{^-n^/NAME_CASING^} switch.
16633 @ifclear vms
16634 With @option{-nD} (``as declared'', which is the default),
16635 @end ifclear
16636 @ifset vms
16637 With @option{/NAME_CASING=AS_DECLARED}, which is the default,
16638 @end ifset
16639 defining occurrences appear exactly as in the source file
16640 where they are declared.
16641 The other ^values for this switch^options for this qualifier^ ---
16642 @option{^-nU^UPPER_CASE^},
16643 @option{^-nL^LOWER_CASE^},
16644 @option{^-nM^MIXED_CASE^} ---
16645 result in
16646 ^upper, lower, or mixed case, respectively^the corresponding casing^.
16647 If @command{gnatpp} changes the casing of a defining
16648 occurrence, it analogously changes the casing of all the
16649 usage occurrences of this name.
16650
16651 If the defining occurrence of a name is not in the source compilation unit
16652 currently being processed by @command{gnatpp}, the casing of each reference to
16653 this name is changed according to the value of the @option{^-n^/NAME_CASING^}
16654 switch (subject to the dictionary file mechanism described below).
16655 Thus @command{gnatpp} acts as though the @option{^-n^/NAME_CASING^} switch
16656 had affected the
16657 casing for the defining occurrence of the name.
16658
16659 Some names may need to be spelled with casing conventions that are not
16660 covered by the upper-, lower-, and mixed-case transformations.
16661 You can arrange correct casing by placing such names in a
16662 @emph{dictionary file},
16663 and then supplying a @option{^-D^/DICTIONARY^} switch.
16664 The casing of names from dictionary files overrides
16665 any @option{^-n^/NAME_CASING^} switch.
16666
16667 To handle the casing of Ada predefined names and the names from GNAT libraries,
16668 @command{gnatpp} assumes a default dictionary file.
16669 The name of each predefined entity is spelled with the same casing as is used
16670 for the entity in the @cite{Ada Reference Manual}.
16671 The name of each entity in the GNAT libraries is spelled with the same casing
16672 as is used in the declaration of that entity.
16673
16674 The @w{@option{^-D-^/SPECIFIC_CASING^}} switch suppresses the use of the
16675 default dictionary file.
16676 Instead, the casing for predefined and GNAT-defined names will be established
16677 by the @option{^-n^/NAME_CASING^} switch or explicit dictionary files.
16678 For example, by default the names @code{Ada.Text_IO} and @code{GNAT.OS_Lib}
16679 will appear as just shown,
16680 even in the presence of a @option{^-nU^/NAME_CASING=UPPER_CASE^} switch.
16681 To ensure that even such names are rendered in uppercase,
16682 additionally supply the @w{@option{^-D-^/SPECIFIC_CASING^}} switch
16683 (or else, less conveniently, place these names in upper case in a dictionary
16684 file).
16685
16686 A dictionary file is
16687 a plain text file; each line in this file can be either a blank line
16688 (containing only space characters and ASCII.HT characters), an Ada comment
16689 line, or the specification of exactly one @emph{casing schema}.
16690
16691 A casing schema is a string that has the following syntax:
16692
16693 @smallexample
16694 @cartouche
16695 @var{casing_schema} ::= @var{identifier} | *@var{simple_identifier}*
16696
16697 @var{simple_identifier} ::= @var{letter}@{@var{letter_or_digit}@}
16698 @end cartouche
16699 @end smallexample
16700
16701 @noindent
16702 (See @cite{Ada Reference Manual}, Section 2.3) for the definition of the
16703 @var{identifier} lexical element and the @var{letter_or_digit} category.)
16704
16705 The casing schema string can be followed by white space and/or an Ada-style
16706 comment; any amount of white space is allowed before the string.
16707
16708 If a dictionary file is passed as
16709 @ifclear vms
16710 the value of a @option{-D@var{file}} switch
16711 @end ifclear
16712 @ifset vms
16713 an option to the @option{/DICTIONARY} qualifier
16714 @end ifset
16715 then for every
16716 simple name and every identifier, @command{gnatpp} checks if the dictionary
16717 defines the casing for the name or for some of its parts (the term ``subword''
16718 is used below to denote the part of a name which is delimited by ``_'' or by
16719 the beginning or end of the word and which does not contain any ``_'' inside):
16720
16721 @itemize @bullet
16722 @item
16723 if the whole name is in the dictionary, @command{gnatpp} uses for this name
16724 the casing defined by the dictionary; no subwords are checked for this word
16725
16726 @item
16727 for every subword @command{gnatpp} checks if the dictionary contains the
16728 corresponding string of the form @code{*@var{simple_identifier}*},
16729 and if it does, the casing of this @var{simple_identifier} is used
16730 for this subword
16731
16732 @item
16733 if the whole name does not contain any ``_'' inside, and if for this name
16734 the dictionary contains two entries - one of the form @var{identifier},
16735 and another - of the form *@var{simple_identifier}*, then the first one
16736 is applied to define the casing of this name
16737
16738 @item
16739 if more than one dictionary file is passed as @command{gnatpp} switches, each
16740 dictionary adds new casing exceptions and overrides all the existing casing
16741 exceptions set by the previous dictionaries
16742
16743 @item
16744 when @command{gnatpp} checks if the word or subword is in the dictionary,
16745 this check is not case sensitive
16746 @end itemize
16747
16748 @noindent
16749 For example, suppose we have the following source to reformat:
16750
16751 @smallexample @c ada
16752 @cartouche
16753 procedure test is
16754 name1 : integer := 1;
16755 name4_name3_name2 : integer := 2;
16756 name2_name3_name4 : Boolean;
16757 name1_var : Float;
16758 begin
16759 name2_name3_name4 := name4_name3_name2 > name1;
16760 end;
16761 @end cartouche
16762 @end smallexample
16763
16764 @noindent
16765 And suppose we have two dictionaries:
16766
16767 @smallexample
16768 @cartouche
16769 @i{dict1:}
16770 NAME1
16771 *NaMe3*
16772 *Name1*
16773 @end cartouche
16774
16775 @cartouche
16776 @i{dict2:}
16777 *NAME3*
16778 @end cartouche
16779 @end smallexample
16780
16781 @noindent
16782 If @command{gnatpp} is called with the following switches:
16783
16784 @smallexample
16785 @ifclear vms
16786 @command{gnatpp -nM -D dict1 -D dict2 test.adb}
16787 @end ifclear
16788 @ifset vms
16789 @command{gnatpp test.adb /NAME_CASING=MIXED_CASE /DICTIONARY=(dict1, dict2)}
16790 @end ifset
16791 @end smallexample
16792
16793 @noindent
16794 then we will get the following name casing in the @command{gnatpp} output:
16795
16796 @smallexample @c ada
16797 @cartouche
16798 procedure Test is
16799 NAME1 : Integer := 1;
16800 Name4_NAME3_Name2 : Integer := 2;
16801 Name2_NAME3_Name4 : Boolean;
16802 Name1_Var : Float;
16803 begin
16804 Name2_NAME3_Name4 := Name4_NAME3_Name2 > NAME1;
16805 end Test;
16806 @end cartouche
16807 @end smallexample
16808
16809 @c *********************************
16810 @node The GNAT Metric Tool gnatmetric
16811 @chapter The GNAT Metric Tool @command{gnatmetric}
16812 @findex gnatmetric
16813 @cindex Metric tool
16814
16815 @noindent
16816 ^The @command{gnatmetric} tool^@command{GNAT METRIC}^ is an ASIS-based utility
16817 for computing various program metrics.
16818 It takes an Ada source file as input and generates a file containing the
16819 metrics data as output. Various switches control which
16820 metrics are computed and output.
16821
16822 @command{gnatmetric} generates and uses the ASIS
16823 tree for the input source and thus requires the input to be syntactically and
16824 semantically legal.
16825 If this condition is not met, @command{gnatmetric} will generate
16826 an error message; no metric information for this file will be
16827 computed and reported.
16828
16829 If the compilation unit contained in the input source depends semantically
16830 upon units in files located outside the current directory, you have to provide
16831 the source search path when invoking @command{gnatmetric}.
16832 If it depends semantically upon units that are contained
16833 in files with names that do not follow the GNAT file naming rules, you have to
16834 provide the configuration file describing the corresponding naming scheme (see
16835 the description of the @command{gnatmetric} switches below.)
16836 Alternatively, you may use a project file and invoke @command{gnatmetric}
16837 through the @command{gnat} driver.
16838
16839 The @command{gnatmetric} command has the form
16840
16841 @smallexample
16842 $ gnatmetric @ovar{switches} @{@var{filename}@} @r{[}-cargs @var{gcc_switches}@r{]}
16843 @end smallexample
16844
16845 @noindent
16846 where
16847 @itemize @bullet
16848 @item
16849 @var{switches} specify the metrics to compute and define the destination for
16850 the output
16851
16852 @item
16853 Each @var{filename} is the name (including the extension) of a source
16854 file to process. ``Wildcards'' are allowed, and
16855 the file name may contain path information.
16856 If no @var{filename} is supplied, then the @var{switches} list must contain
16857 at least one
16858 @option{-files} switch (@pxref{Other gnatmetric Switches}).
16859 Including both a @option{-files} switch and one or more
16860 @var{filename} arguments is permitted.
16861
16862 @item
16863 @samp{-cargs @var{gcc_switches}} is a list of switches for
16864 @command{gcc}. They will be passed on to all compiler invocations made by
16865 @command{gnatmetric} to generate the ASIS trees. Here you can provide
16866 @option{^-I^/INCLUDE_DIRS=^} switches to form the source search path,
16867 and use the @option{-gnatec} switch to set the configuration file.
16868 @end itemize
16869
16870 @menu
16871 * Switches for gnatmetric::
16872 @end menu
16873
16874 @node Switches for gnatmetric
16875 @section Switches for @command{gnatmetric}
16876
16877 @noindent
16878 The following subsections describe the various switches accepted by
16879 @command{gnatmetric}, organized by category.
16880
16881 @menu
16882 * Output Files Control::
16883 * Disable Metrics For Local Units::
16884 * Specifying a set of metrics to compute::
16885 * Other gnatmetric Switches::
16886 * Generate project-wide metrics::
16887 @end menu
16888
16889 @node Output Files Control
16890 @subsection Output File Control
16891 @cindex Output file control in @command{gnatmetric}
16892
16893 @noindent
16894 @command{gnatmetric} has two output formats. It can generate a
16895 textual (human-readable) form, and also XML. By default only textual
16896 output is generated.
16897
16898 When generating the output in textual form, @command{gnatmetric} creates
16899 for each Ada source file a corresponding text file
16900 containing the computed metrics, except for the case when the set of metrics
16901 specified by gnatmetric parameters consists only of metrics that are computed
16902 for the whole set of analyzed sources, but not for each Ada source.
16903 By default, this file is placed in the same directory as where the source
16904 file is located, and its name is obtained
16905 by appending the ^@file{.metrix}^@file{$METRIX}^ suffix to the name of the
16906 input file.
16907
16908 All the output information generated in XML format is placed in a single
16909 file. By default this file is placed in the current directory and has the
16910 name ^@file{metrix.xml}^@file{METRIX$XML}^.
16911
16912 Some of the computed metrics are summed over the units passed to
16913 @command{gnatmetric}; for example, the total number of lines of code.
16914 By default this information is sent to @file{stdout}, but a file
16915 can be specified with the @option{-og} switch.
16916
16917 The following switches control the @command{gnatmetric} output:
16918
16919 @table @option
16920 @cindex @option{^-x^/XML^} (@command{gnatmetric})
16921 @item ^-x^/XML^
16922 Generate the XML output
16923
16924 @cindex @option{^-nt^/NO_TEXT^} (@command{gnatmetric})
16925 @item ^-nt^/NO_TEXT^
16926 Do not generate the output in text form (implies @option{^-x^/XML^})
16927
16928 @cindex @option{^-d^/DIRECTORY^} (@command{gnatmetric})
16929 @item ^-d @var{output_dir}^/DIRECTORY=@var{output_dir}^
16930 Put textual files with detailed metrics into @var{output_dir}
16931
16932 @cindex @option{^-o^/SUFFIX_DETAILS^} (@command{gnatmetric})
16933 @item ^-o @var{file_suffix}^/SUFFIX_DETAILS=@var{file_suffix}^
16934 Use @var{file_suffix}, instead of ^@file{.metrix}^@file{$METRIX}^
16935 in the name of the output file.
16936
16937 @cindex @option{^-og^/GLOBAL_OUTPUT^} (@command{gnatmetric})
16938 @item ^-og @var{file_name}^/GLOBAL_OUTPUT=@var{file_name}^
16939 Put global metrics into @var{file_name}
16940
16941 @cindex @option{^-ox^/XML_OUTPUT^} (@command{gnatmetric})
16942 @item ^-ox @var{file_name}^/XML_OUTPUT=@var{file_name}^
16943 Put the XML output into @var{file_name} (also implies @option{^-x^/XML^})
16944
16945 @cindex @option{^-sfn^/SHORT_SOURCE_FILE_NAME^} (@command{gnatmetric})
16946 @item ^-sfn^/SHORT_SOURCE_FILE_NAME^
16947 Use ``short'' source file names in the output. (The @command{gnatmetric}
16948 output includes the name(s) of the Ada source file(s) from which the metrics
16949 are computed. By default each name includes the absolute path. The
16950 @option{^-sfn^/SHORT_SOURCE_FILE_NAME^} switch causes @command{gnatmetric}
16951 to exclude all directory information from the file names that are output.)
16952
16953 @end table
16954
16955 @node Disable Metrics For Local Units
16956 @subsection Disable Metrics For Local Units
16957 @cindex Disable Metrics For Local Units in @command{gnatmetric}
16958
16959 @noindent
16960 @command{gnatmetric} relies on the GNAT compilation model @minus{}
16961 one compilation
16962 unit per one source file. It computes line metrics for the whole source
16963 file, and it also computes syntax
16964 and complexity metrics for the file's outermost unit.
16965
16966 By default, @command{gnatmetric} will also compute all metrics for certain
16967 kinds of locally declared program units:
16968
16969 @itemize @bullet
16970 @item
16971 subprogram (and generic subprogram) bodies;
16972
16973 @item
16974 package (and generic package) specs and bodies;
16975
16976 @item
16977 task object and type specifications and bodies;
16978
16979 @item
16980 protected object and type specifications and bodies.
16981 @end itemize
16982
16983 @noindent
16984 These kinds of entities will be referred to as
16985 @emph{eligible local program units}, or simply @emph{eligible local units},
16986 @cindex Eligible local unit (for @command{gnatmetric})
16987 in the discussion below.
16988
16989 Note that a subprogram declaration, generic instantiation,
16990 or renaming declaration only receives metrics
16991 computation when it appear as the outermost entity
16992 in a source file.
16993
16994 Suppression of metrics computation for eligible local units can be
16995 obtained via the following switch:
16996
16997 @table @option
16998 @cindex @option{^-n@var{x}^/SUPPRESS^} (@command{gnatmetric})
16999 @item ^-nolocal^/SUPPRESS=LOCAL_DETAILS^
17000 Do not compute detailed metrics for eligible local program units
17001
17002 @end table
17003
17004 @node Specifying a set of metrics to compute
17005 @subsection Specifying a set of metrics to compute
17006
17007 @noindent
17008 By default all the metrics are computed and reported. The switches
17009 described in this subsection allow you to control, on an individual
17010 basis, whether metrics are computed and
17011 reported. If at least one positive metric
17012 switch is specified (that is, a switch that defines that a given
17013 metric or set of metrics is to be computed), then only
17014 explicitly specified metrics are reported.
17015
17016 @menu
17017 * Line Metrics Control::
17018 * Syntax Metrics Control::
17019 * Complexity Metrics Control::
17020 * Object-Oriented Metrics Control::
17021 @end menu
17022
17023 @node Line Metrics Control
17024 @subsubsection Line Metrics Control
17025 @cindex Line metrics control in @command{gnatmetric}
17026
17027 @noindent
17028 For any (legal) source file, and for each of its
17029 eligible local program units, @command{gnatmetric} computes the following
17030 metrics:
17031
17032 @itemize @bullet
17033 @item
17034 the total number of lines;
17035
17036 @item
17037 the total number of code lines (i.e., non-blank lines that are not comments)
17038
17039 @item
17040 the number of comment lines
17041
17042 @item
17043 the number of code lines containing end-of-line comments;
17044
17045 @item
17046 the comment percentage: the ratio between the number of lines that contain
17047 comments and the number of all non-blank lines, expressed as a percentage;
17048
17049 @item
17050 the number of empty lines and lines containing only space characters and/or
17051 format effectors (blank lines)
17052
17053 @item
17054 the average number of code lines in subprogram bodies, task bodies, entry
17055 bodies and statement sequences in package bodies (this metric is only computed
17056 across the whole set of the analyzed units)
17057
17058 @end itemize
17059
17060 @noindent
17061 @command{gnatmetric} sums the values of the line metrics for all the
17062 files being processed and then generates the cumulative results. The tool
17063 also computes for all the files being processed the average number of code
17064 lines in bodies.
17065
17066 You can use the following switches to select the specific line metrics
17067 to be computed and reported.
17068
17069 @table @option
17070 @cindex @option{^--lines@var{x}^/LINE_COUNT_METRICS^} (@command{gnatmetric})
17071
17072 @ifclear vms
17073 @cindex @option{--no-lines@var{x}}
17074 @end ifclear
17075
17076 @item ^--lines-all^/LINE_COUNT_METRICS=ALL_ON^
17077 Report all the line metrics
17078
17079 @item ^--no-lines-all^/LINE_COUNT_METRICS=ALL_OFF^
17080 Do not report any of line metrics
17081
17082 @item ^--lines^/LINE_COUNT_METRICS=ALL_LINES_ON^
17083 Report the number of all lines
17084
17085 @item ^--no-lines^/LINE_COUNT_METRICS=ALL_LINES_OFF^
17086 Do not report the number of all lines
17087
17088 @item ^--lines-code^/LINE_COUNT_METRICS=CODE_LINES_ON^
17089 Report the number of code lines
17090
17091 @item ^--no-lines-code^/LINE_COUNT_METRICS=CODE_LINES_OFF^
17092 Do not report the number of code lines
17093
17094 @item ^--lines-comment^/LINE_COUNT_METRICS=COMMENT_LINES_ON^
17095 Report the number of comment lines
17096
17097 @item ^--no-lines-comment^/LINE_COUNT_METRICS=COMMENT_LINES_OFF^
17098 Do not report the number of comment lines
17099
17100 @item ^--lines-eol-comment^/LINE_COUNT_METRICS=CODE_COMMENT_LINES_ON^
17101 Report the number of code lines containing
17102 end-of-line comments
17103
17104 @item ^--no-lines-eol-comment^/LINE_COUNT_METRICS=CODE_COMMENT_LINES_OFF^
17105 Do not report the number of code lines containing
17106 end-of-line comments
17107
17108 @item ^--lines-ratio^/LINE_COUNT_METRICS=COMMENT_PERCENTAGE_ON^
17109 Report the comment percentage in the program text
17110
17111 @item ^--no-lines-ratio^/LINE_COUNT_METRICS=COMMENT_PERCENTAGE_OFF^
17112 Do not report the comment percentage in the program text
17113
17114 @item ^--lines-blank^/LINE_COUNT_METRICS=BLANK_LINES_ON^
17115 Report the number of blank lines
17116
17117 @item ^--no-lines-blank^/LINE_COUNT_METRICS=BLANK_LINES_OFF^
17118 Do not report the number of blank lines
17119
17120 @item ^--lines-average^/LINE_COUNT_METRICS=AVERAGE_BODY_LINES_ON^
17121 Report the average number of code lines in subprogram bodies, task bodies,
17122 entry bodies and statement sequences in package bodies. The metric is computed
17123 and reported for the whole set of processed Ada sources only.
17124
17125 @item ^--no-lines-average^/LINE_COUNT_METRICS=AVERAGE_BODY_LINES_OFF^
17126 Do not report the average number of code lines in subprogram bodies,
17127 task bodies, entry bodies and statement sequences in package bodies.
17128
17129 @end table
17130
17131 @node Syntax Metrics Control
17132 @subsubsection Syntax Metrics Control
17133 @cindex Syntax metrics control in @command{gnatmetric}
17134
17135 @noindent
17136 @command{gnatmetric} computes various syntactic metrics for the
17137 outermost unit and for each eligible local unit:
17138
17139 @table @emph
17140 @item LSLOC (``Logical Source Lines Of Code'')
17141 The total number of declarations and the total number of statements
17142
17143 @item Maximal static nesting level of inner program units
17144 According to
17145 @cite{Ada Reference Manual}, 10.1(1), ``A program unit is either a
17146 package, a task unit, a protected unit, a
17147 protected entry, a generic unit, or an explicitly declared subprogram other
17148 than an enumeration literal.''
17149
17150 @item Maximal nesting level of composite syntactic constructs
17151 This corresponds to the notion of the
17152 maximum nesting level in the GNAT built-in style checks
17153 (@pxref{Style Checking})
17154 @end table
17155
17156 @noindent
17157 For the outermost unit in the file, @command{gnatmetric} additionally computes
17158 the following metrics:
17159
17160 @table @emph
17161 @item Public subprograms
17162 This metric is computed for package specs. It is the
17163 number of subprograms and generic subprograms declared in the visible
17164 part (including the visible part of nested packages, protected objects, and
17165 protected types).
17166
17167 @item All subprograms
17168 This metric is computed for bodies and subunits. The
17169 metric is equal to a total number of subprogram bodies in the compilation
17170 unit.
17171 Neither generic instantiations nor renamings-as-a-body nor body stubs
17172 are counted. Any subprogram body is counted, independently of its nesting
17173 level and enclosing constructs. Generic bodies and bodies of protected
17174 subprograms are counted in the same way as ``usual'' subprogram bodies.
17175
17176 @item Public types
17177 This metric is computed for package specs and
17178 generic package declarations. It is the total number of types
17179 that can be referenced from outside this compilation unit, plus the
17180 number of types from all the visible parts of all the visible generic
17181 packages. Generic formal types are not counted. Only types, not subtypes,
17182 are included.
17183
17184 @noindent
17185 Along with the total number of public types, the following
17186 types are counted and reported separately:
17187
17188 @itemize @bullet
17189 @item
17190 Abstract types
17191
17192 @item
17193 Root tagged types (abstract, non-abstract, private, non-private). Type
17194 extensions are @emph{not} counted
17195
17196 @item
17197 Private types (including private extensions)
17198
17199 @item
17200 Task types
17201
17202 @item
17203 Protected types
17204
17205 @end itemize
17206
17207 @item All types
17208 This metric is computed for any compilation unit. It is equal to the total
17209 number of the declarations of different types given in the compilation unit.
17210 The private and the corresponding full type declaration are counted as one
17211 type declaration. Incomplete type declarations and generic formal types
17212 are not counted.
17213 No distinction is made among different kinds of types (abstract,
17214 private etc.); the total number of types is computed and reported.
17215
17216 @end table
17217
17218 @noindent
17219 By default, all the syntax metrics are computed and reported. You can use the
17220 following switches to select specific syntax metrics.
17221
17222 @table @option
17223
17224 @cindex @option{^--syntax@var{x}^/SYNTAX_METRICS^} (@command{gnatmetric})
17225
17226 @ifclear vms
17227 @cindex @option{--no-syntax@var{x}} (@command{gnatmetric})
17228 @end ifclear
17229
17230 @item ^--syntax-all^/SYNTAX_METRICS=ALL_ON^
17231 Report all the syntax metrics
17232
17233 @item ^--no-syntax-all^/ALL_OFF^
17234 Do not report any of syntax metrics
17235
17236 @item ^--declarations^/SYNTAX_METRICS=DECLARATIONS_ON^
17237 Report the total number of declarations
17238
17239 @item ^--no-declarations^/SYNTAX_METRICS=DECLARATIONS_OFF^
17240 Do not report the total number of declarations
17241
17242 @item ^--statements^/SYNTAX_METRICS=STATEMENTS_ON^
17243 Report the total number of statements
17244
17245 @item ^--no-statements^/SYNTAX_METRICS=STATEMENTS_OFF^
17246 Do not report the total number of statements
17247
17248 @item ^--public-subprograms^/SYNTAX_METRICS=PUBLIC_SUBPROGRAMS_ON^
17249 Report the number of public subprograms in a compilation unit
17250
17251 @item ^--no-public-subprograms^/SYNTAX_METRICS=PUBLIC_SUBPROGRAMS_OFF^
17252 Do not report the number of public subprograms in a compilation unit
17253
17254 @item ^--all-subprograms^/SYNTAX_METRICS=ALL_SUBPROGRAMS_ON^
17255 Report the number of all the subprograms in a compilation unit
17256
17257 @item ^--no-all-subprograms^/SYNTAX_METRICS=ALL_SUBPROGRAMS_OFF^
17258 Do not report the number of all the subprograms in a compilation unit
17259
17260 @item ^--public-types^/SYNTAX_METRICS=PUBLIC_TYPES_ON^
17261 Report the number of public types in a compilation unit
17262
17263 @item ^--no-public-types^/SYNTAX_METRICS=PUBLIC_TYPES_OFF^
17264 Do not report the number of public types in a compilation unit
17265
17266 @item ^--all-types^/SYNTAX_METRICS=ALL_TYPES_ON^
17267 Report the number of all the types in a compilation unit
17268
17269 @item ^--no-all-types^/SYNTAX_METRICS=ALL_TYPES_OFF^
17270 Do not report the number of all the types in a compilation unit
17271
17272 @item ^--unit-nesting^/SYNTAX_METRICS=UNIT_NESTING_ON^
17273 Report the maximal program unit nesting level
17274
17275 @item ^--no-unit-nesting^/SYNTAX_METRICS=UNIT_NESTING_OFF^
17276 Do not report the maximal program unit nesting level
17277
17278 @item ^--construct-nesting^/SYNTAX_METRICS=CONSTRUCT_NESTING_ON^
17279 Report the maximal construct nesting level
17280
17281 @item ^--no-construct-nesting^/SYNTAX_METRICS=CONSTRUCT_NESTING_OFF^
17282 Do not report the maximal construct nesting level
17283
17284 @end table
17285
17286 @node Complexity Metrics Control
17287 @subsubsection Complexity Metrics Control
17288 @cindex Complexity metrics control in @command{gnatmetric}
17289
17290 @noindent
17291 For a program unit that is an executable body (a subprogram body (including
17292 generic bodies), task body, entry body or a package body containing
17293 its own statement sequence) @command{gnatmetric} computes the following
17294 complexity metrics:
17295
17296 @itemize @bullet
17297 @item
17298 McCabe cyclomatic complexity;
17299
17300 @item
17301 McCabe essential complexity;
17302
17303 @item
17304 maximal loop nesting level
17305
17306 @end itemize
17307
17308 @noindent
17309 The McCabe complexity metrics are defined
17310 in @url{http://www.mccabe.com/pdf/nist235r.pdf}
17311
17312 According to McCabe, both control statements and short-circuit control forms
17313 should be taken into account when computing cyclomatic complexity. For each
17314 body, we compute three metric values:
17315
17316 @itemize @bullet
17317 @item
17318 the complexity introduced by control
17319 statements only, without taking into account short-circuit forms,
17320
17321 @item
17322 the complexity introduced by short-circuit control forms only, and
17323
17324 @item
17325 the total
17326 cyclomatic complexity, which is the sum of these two values.
17327 @end itemize
17328
17329 @noindent
17330 When computing cyclomatic and essential complexity, @command{gnatmetric} skips
17331 the code in the exception handlers and in all the nested program units.
17332
17333 By default, all the complexity metrics are computed and reported.
17334 For more fine-grained control you can use
17335 the following switches:
17336
17337 @table @option
17338 @cindex @option{^-complexity@var{x}^/COMPLEXITY_METRICS^} (@command{gnatmetric})
17339
17340 @ifclear vms
17341 @cindex @option{--no-complexity@var{x}}
17342 @end ifclear
17343
17344 @item ^--complexity-all^/COMPLEXITY_METRICS=ALL_ON^
17345 Report all the complexity metrics
17346
17347 @item ^--no-complexity-all^/COMPLEXITY_METRICS=ALL_OFF^
17348 Do not report any of complexity metrics
17349
17350 @item ^--complexity-cyclomatic^/COMPLEXITY_METRICS=CYCLOMATIC_ON^
17351 Report the McCabe Cyclomatic Complexity
17352
17353 @item ^--no-complexity-cyclomatic^/COMPLEXITY_METRICS=CYCLOMATIC_OFF^
17354 Do not report the McCabe Cyclomatic Complexity
17355
17356 @item ^--complexity-essential^/COMPLEXITY_METRICS=ESSENTIAL_ON^
17357 Report the Essential Complexity
17358
17359 @item ^--no-complexity-essential^/COMPLEXITY_METRICS=ESSENTIAL_OFF^
17360 Do not report the Essential Complexity
17361
17362 @item ^--loop-nesting^/COMPLEXITY_METRICS=LOOP_NESTING_ON^
17363 Report maximal loop nesting level
17364
17365 @item ^--no-loop-nesting^/COMPLEXITY_METRICS=LOOP_NESTING_OFF^
17366 Do not report maximal loop nesting level
17367
17368 @item ^--complexity-average^/COMPLEXITY_METRICS=AVERAGE_COMPLEXITY_ON^
17369 Report the average McCabe Cyclomatic Complexity for all the subprogram bodies,
17370 task bodies, entry bodies and statement sequences in package bodies.
17371 The metric is computed and reported for whole set of processed Ada sources
17372 only.
17373
17374 @item ^--no-complexity-average^/COMPLEXITY_METRICS=AVERAGE_COMPLEXITY_OFF^
17375 Do not report the average McCabe Cyclomatic Complexity for all the subprogram
17376 bodies, task bodies, entry bodies and statement sequences in package bodies
17377
17378 @cindex @option{^-ne^/NO_EXITS_AS_GOTOS^} (@command{gnatmetric})
17379 @item ^-ne^/NO_EXITS_AS_GOTOS^
17380 Do not consider @code{exit} statements as @code{goto}s when
17381 computing Essential Complexity
17382
17383 @end table
17384
17385
17386 @node Object-Oriented Metrics Control
17387 @subsubsection Object-Oriented Metrics Control
17388 @cindex Object-Oriented metrics control in @command{gnatmetric}
17389
17390 @noindent
17391 @cindex Coupling metrics (in in @command{gnatmetric})
17392 Coupling metrics are object-oriented metrics that measure the
17393 dependencies between a given class (or a group of classes) and the
17394 ``external world'' (that is, the other classes in the program). In this
17395 subsection the term ``class'' is used in its
17396 traditional object-oriented programming sense
17397 (an instantiable module that contains data and/or method members).
17398 A @emph{category} (of classes)
17399 is a group of closely related classes that are reused and/or
17400 modified together.
17401
17402 A class @code{K}'s @emph{efferent coupling} is the number of classes
17403 that @code{K} depends upon.
17404 A category's efferent coupling is the number of classes outside the
17405 category that the classes inside the category depend upon.
17406
17407 A class @code{K}'s @emph{afferent coupling} is the number of classes
17408 that depend upon @code{K}.
17409 A category's afferent coupling is the number of classes outside the
17410 category that depend on classes belonging to the category.
17411
17412 Ada's implementation of the object-oriented paradigm does not use the
17413 traditional class notion, so the definition of the coupling
17414 metrics for Ada maps the class and class category notions
17415 onto Ada constructs.
17416
17417 For the coupling metrics, several kinds of modules -- a library package,
17418 a library generic package, and a library generic package instantiation --
17419 that define a tagged type or an interface type are
17420 considered to be a class. A category consists of a library package (or
17421 a library generic package) that defines a tagged or an interface type,
17422 together with all its descendant (generic) packages that define tagged
17423 or interface types. For any package counted as a class,
17424 its body (if any) is considered
17425 together with its spec when counting the dependencies. For dependencies
17426 between classes, the Ada semantic dependencies are considered.
17427 For coupling metrics, only dependencies on units that are considered as
17428 classes, are considered.
17429
17430 When computing coupling metrics, @command{gnatmetric} counts only
17431 dependencies between units that are arguments of the gnatmetric call.
17432 Coupling metrics are program-wide (or project-wide) metrics, so to
17433 get a valid result, you should call @command{gnatmetric} for
17434 the whole set of sources that make up your program. It can be done
17435 by calling @command{gnatmetric} from the GNAT driver with @option{-U}
17436 option (see See @ref{The GNAT Driver and Project Files} for details.
17437
17438 By default, all the coupling metrics are disabled. You can use the following
17439 switches to specify the coupling metrics to be computed and reported:
17440
17441 @table @option
17442
17443 @ifclear vms
17444 @cindex @option{--package@var{x}} (@command{gnatmetric})
17445 @cindex @option{--no-package@var{x}} (@command{gnatmetric})
17446 @cindex @option{--category@var{x}} (@command{gnatmetric})
17447 @cindex @option{--no-category@var{x}} (@command{gnatmetric})
17448 @end ifclear
17449
17450 @ifset vms
17451 @cindex @option{/COUPLING_METRICS} (@command{gnatmetric})
17452 @end ifset
17453
17454 @item ^--coupling-all^/COUPLING_METRICS=ALL_ON^
17455 Report all the coupling metrics
17456
17457 @item ^--no-coupling-all^/COUPLING_METRICS=ALL_OFF^
17458 Do not report any of metrics
17459
17460 @item ^--package-efferent-coupling^/COUPLING_METRICS=PACKAGE_EFFERENT_ON^
17461 Report package efferent coupling
17462
17463 @item ^--no-package-efferent-coupling^/COUPLING_METRICS=PACKAGE_EFFERENT_OFF^
17464 Do not report package efferent coupling
17465
17466 @item ^--package-afferent-coupling^/COUPLING_METRICS=PACKAGE_AFFERENT_ON^
17467 Report package afferent coupling
17468
17469 @item ^--no-package-afferent-coupling^/COUPLING_METRICS=PACKAGE_AFFERENT_OFF^
17470 Do not report package afferent coupling
17471
17472 @item ^--category-efferent-coupling^/COUPLING_METRICS=CATEGORY_EFFERENT_ON^
17473 Report category efferent coupling
17474
17475 @item ^--no-category-efferent-coupling^/COUPLING_METRICS=CATEGORY_EFFERENT_OFF^
17476 Do not report category efferent coupling
17477
17478 @item ^--category-afferent-coupling^/COUPLING_METRICS=CATEGORY_AFFERENT_ON^
17479 Report category afferent coupling
17480
17481 @item ^--no-category-afferent-coupling^/COUPLING_METRICS=CATEGORY_AFFERENT_OFF^
17482 Do not report category afferent coupling
17483
17484 @end table
17485
17486 @node Other gnatmetric Switches
17487 @subsection Other @code{gnatmetric} Switches
17488
17489 @noindent
17490 Additional @command{gnatmetric} switches are as follows:
17491
17492 @table @option
17493 @item ^-files @var{filename}^/FILES=@var{filename}^
17494 @cindex @option{^-files^/FILES^} (@code{gnatmetric})
17495 Take the argument source files from the specified file. This file should be an
17496 ordinary text file containing file names separated by spaces or
17497 line breaks. You can use this switch more then once in the same call to
17498 @command{gnatmetric}. You also can combine this switch with
17499 an explicit list of files.
17500
17501 @item ^-v^/VERBOSE^
17502 @cindex @option{^-v^/VERBOSE^} (@code{gnatmetric})
17503 Verbose mode;
17504 @command{gnatmetric} generates version information and then
17505 a trace of sources being processed.
17506
17507 @item ^-dv^/DEBUG_OUTPUT^
17508 @cindex @option{^-dv^/DEBUG_OUTPUT^} (@code{gnatmetric})
17509 Debug mode;
17510 @command{gnatmetric} generates various messages useful to understand what
17511 happens during the metrics computation
17512
17513 @item ^-q^/QUIET^
17514 @cindex @option{^-q^/QUIET^} (@code{gnatmetric})
17515 Quiet mode.
17516 @end table
17517
17518 @node Generate project-wide metrics
17519 @subsection Generate project-wide metrics
17520
17521 In order to compute metrics on all units of a given project, you can use
17522 the @command{gnat} driver along with the @option{-P} option:
17523 @smallexample
17524 gnat metric -Pproj
17525 @end smallexample
17526
17527 @noindent
17528 If the project @code{proj} depends upon other projects, you can compute
17529 the metrics on the project closure using the @option{-U} option:
17530 @smallexample
17531 gnat metric -Pproj -U
17532 @end smallexample
17533
17534 @noindent
17535 Finally, if not all the units are relevant to a particular main
17536 program in the project closure, you can generate metrics for the set
17537 of units needed to create a given main program (unit closure) using
17538 the @option{-U} option followed by the name of the main unit:
17539 @smallexample
17540 gnat metric -Pproj -U main
17541 @end smallexample
17542
17543
17544 @c ***********************************
17545 @node File Name Krunching Using gnatkr
17546 @chapter File Name Krunching Using @code{gnatkr}
17547 @findex gnatkr
17548
17549 @noindent
17550 This chapter discusses the method used by the compiler to shorten
17551 the default file names chosen for Ada units so that they do not
17552 exceed the maximum length permitted. It also describes the
17553 @code{gnatkr} utility that can be used to determine the result of
17554 applying this shortening.
17555 @menu
17556 * About gnatkr::
17557 * Using gnatkr::
17558 * Krunching Method::
17559 * Examples of gnatkr Usage::
17560 @end menu
17561
17562 @node About gnatkr
17563 @section About @code{gnatkr}
17564
17565 @noindent
17566 The default file naming rule in GNAT
17567 is that the file name must be derived from
17568 the unit name. The exact default rule is as follows:
17569 @itemize @bullet
17570 @item
17571 Take the unit name and replace all dots by hyphens.
17572 @item
17573 If such a replacement occurs in the
17574 second character position of a name, and the first character is
17575 ^@samp{a}, @samp{g}, @samp{s}, or @samp{i}, ^@samp{A}, @samp{G}, @samp{S}, or @samp{I},^
17576 then replace the dot by the character
17577 ^@samp{~} (tilde)^@samp{$} (dollar sign)^
17578 instead of a minus.
17579 @end itemize
17580 The reason for this exception is to avoid clashes
17581 with the standard names for children of System, Ada, Interfaces,
17582 and GNAT, which use the prefixes
17583 ^@samp{s-}, @samp{a-}, @samp{i-}, and @samp{g-},^@samp{S-}, @samp{A-}, @samp{I-}, and @samp{G-},^
17584 respectively.
17585
17586 The @option{^-gnatk^/FILE_NAME_MAX_LENGTH=^@var{nn}}
17587 switch of the compiler activates a ``krunching''
17588 circuit that limits file names to nn characters (where nn is a decimal
17589 integer). For example, using OpenVMS,
17590 where the maximum file name length is
17591 39, the value of nn is usually set to 39, but if you want to generate
17592 a set of files that would be usable if ported to a system with some
17593 different maximum file length, then a different value can be specified.
17594 The default value of 39 for OpenVMS need not be specified.
17595
17596 The @code{gnatkr} utility can be used to determine the krunched name for
17597 a given file, when krunched to a specified maximum length.
17598
17599 @node Using gnatkr
17600 @section Using @code{gnatkr}
17601
17602 @noindent
17603 The @code{gnatkr} command has the form
17604
17605 @ifclear vms
17606 @smallexample
17607 $ gnatkr @var{name} @ovar{length}
17608 @end smallexample
17609 @end ifclear
17610
17611 @ifset vms
17612 @smallexample
17613 $ gnatkr @var{name} /COUNT=nn
17614 @end smallexample
17615 @end ifset
17616
17617 @noindent
17618 @var{name} is the uncrunched file name, derived from the name of the unit
17619 in the standard manner described in the previous section (i.e., in particular
17620 all dots are replaced by hyphens). The file name may or may not have an
17621 extension (defined as a suffix of the form period followed by arbitrary
17622 characters other than period). If an extension is present then it will
17623 be preserved in the output. For example, when krunching @file{hellofile.ads}
17624 to eight characters, the result will be hellofil.ads.
17625
17626 Note: for compatibility with previous versions of @code{gnatkr} dots may
17627 appear in the name instead of hyphens, but the last dot will always be
17628 taken as the start of an extension. So if @code{gnatkr} is given an argument
17629 such as @file{Hello.World.adb} it will be treated exactly as if the first
17630 period had been a hyphen, and for example krunching to eight characters
17631 gives the result @file{hellworl.adb}.
17632
17633 Note that the result is always all lower case (except on OpenVMS where it is
17634 all upper case). Characters of the other case are folded as required.
17635
17636 @var{length} represents the length of the krunched name. The default
17637 when no argument is given is ^8^39^ characters. A length of zero stands for
17638 unlimited, in other words do not chop except for system files where the
17639 implied crunching length is always eight characters.
17640
17641 @noindent
17642 The output is the krunched name. The output has an extension only if the
17643 original argument was a file name with an extension.
17644
17645 @node Krunching Method
17646 @section Krunching Method
17647
17648 @noindent
17649 The initial file name is determined by the name of the unit that the file
17650 contains. The name is formed by taking the full expanded name of the
17651 unit and replacing the separating dots with hyphens and
17652 using ^lowercase^uppercase^
17653 for all letters, except that a hyphen in the second character position is
17654 replaced by a ^tilde^dollar sign^ if the first character is
17655 ^@samp{a}, @samp{i}, @samp{g}, or @samp{s}^@samp{A}, @samp{I}, @samp{G}, or @samp{S}^.
17656 The extension is @code{.ads} for a
17657 spec and @code{.adb} for a body.
17658 Krunching does not affect the extension, but the file name is shortened to
17659 the specified length by following these rules:
17660
17661 @itemize @bullet
17662 @item
17663 The name is divided into segments separated by hyphens, tildes or
17664 underscores and all hyphens, tildes, and underscores are
17665 eliminated. If this leaves the name short enough, we are done.
17666
17667 @item
17668 If the name is too long, the longest segment is located (left-most
17669 if there are two of equal length), and shortened by dropping
17670 its last character. This is repeated until the name is short enough.
17671
17672 As an example, consider the krunching of @*@file{our-strings-wide_fixed.adb}
17673 to fit the name into 8 characters as required by some operating systems.
17674
17675 @smallexample
17676 our-strings-wide_fixed 22
17677 our strings wide fixed 19
17678 our string wide fixed 18
17679 our strin wide fixed 17
17680 our stri wide fixed 16
17681 our stri wide fixe 15
17682 our str wide fixe 14
17683 our str wid fixe 13
17684 our str wid fix 12
17685 ou str wid fix 11
17686 ou st wid fix 10
17687 ou st wi fix 9
17688 ou st wi fi 8
17689 Final file name: oustwifi.adb
17690 @end smallexample
17691
17692 @item
17693 The file names for all predefined units are always krunched to eight
17694 characters. The krunching of these predefined units uses the following
17695 special prefix replacements:
17696
17697 @table @file
17698 @item ada-
17699 replaced by @file{^a^A^-}
17700
17701 @item gnat-
17702 replaced by @file{^g^G^-}
17703
17704 @item interfaces-
17705 replaced by @file{^i^I^-}
17706
17707 @item system-
17708 replaced by @file{^s^S^-}
17709 @end table
17710
17711 These system files have a hyphen in the second character position. That
17712 is why normal user files replace such a character with a
17713 ^tilde^dollar sign^, to
17714 avoid confusion with system file names.
17715
17716 As an example of this special rule, consider
17717 @*@file{ada-strings-wide_fixed.adb}, which gets krunched as follows:
17718
17719 @smallexample
17720 ada-strings-wide_fixed 22
17721 a- strings wide fixed 18
17722 a- string wide fixed 17
17723 a- strin wide fixed 16
17724 a- stri wide fixed 15
17725 a- stri wide fixe 14
17726 a- str wide fixe 13
17727 a- str wid fixe 12
17728 a- str wid fix 11
17729 a- st wid fix 10
17730 a- st wi fix 9
17731 a- st wi fi 8
17732 Final file name: a-stwifi.adb
17733 @end smallexample
17734 @end itemize
17735
17736 Of course no file shortening algorithm can guarantee uniqueness over all
17737 possible unit names, and if file name krunching is used then it is your
17738 responsibility to ensure that no name clashes occur. The utility
17739 program @code{gnatkr} is supplied for conveniently determining the
17740 krunched name of a file.
17741
17742 @node Examples of gnatkr Usage
17743 @section Examples of @code{gnatkr} Usage
17744
17745 @smallexample
17746 @iftex
17747 @leftskip=0cm
17748 @end iftex
17749 @ifclear vms
17750 $ gnatkr very_long_unit_name.ads --> velounna.ads
17751 $ gnatkr grandparent-parent-child.ads --> grparchi.ads
17752 $ gnatkr Grandparent.Parent.Child.ads --> grparchi.ads
17753 $ gnatkr grandparent-parent-child --> grparchi
17754 @end ifclear
17755 $ gnatkr very_long_unit_name.ads/count=6 --> vlunna.ads
17756 $ gnatkr very_long_unit_name.ads/count=0 --> very_long_unit_name.ads
17757 @end smallexample
17758
17759 @node Preprocessing Using gnatprep
17760 @chapter Preprocessing Using @code{gnatprep}
17761 @findex gnatprep
17762
17763 @noindent
17764 This chapter discusses how to use GNAT's @code{gnatprep} utility for simple
17765 preprocessing.
17766 Although designed for use with GNAT, @code{gnatprep} does not depend on any
17767 special GNAT features.
17768 For further discussion of conditional compilation in general, see
17769 @ref{Conditional Compilation}.
17770
17771 @menu
17772 * Preprocessing Symbols::
17773 * Using gnatprep::
17774 * Switches for gnatprep::
17775 * Form of Definitions File::
17776 * Form of Input Text for gnatprep::
17777 @end menu
17778
17779 @node Preprocessing Symbols
17780 @section Preprocessing Symbols
17781
17782 @noindent
17783 Preprocessing symbols are defined in definition files and referred to in
17784 sources to be preprocessed. A Preprocessing symbol is an identifier, following
17785 normal Ada (case-insensitive) rules for its syntax, with the restriction that
17786 all characters need to be in the ASCII set (no accented letters).
17787
17788 @node Using gnatprep
17789 @section Using @code{gnatprep}
17790
17791 @noindent
17792 To call @code{gnatprep} use
17793
17794 @smallexample
17795 $ gnatprep @ovar{switches} @var{infile} @var{outfile} @ovar{deffile}
17796 @end smallexample
17797
17798 @noindent
17799 where
17800 @table @var
17801 @item switches
17802 is an optional sequence of switches as described in the next section.
17803
17804 @item infile
17805 is the full name of the input file, which is an Ada source
17806 file containing preprocessor directives.
17807
17808 @item outfile
17809 is the full name of the output file, which is an Ada source
17810 in standard Ada form. When used with GNAT, this file name will
17811 normally have an ads or adb suffix.
17812
17813 @item deffile
17814 is the full name of a text file containing definitions of
17815 preprocessing symbols to be referenced by the preprocessor. This argument is
17816 optional, and can be replaced by the use of the @option{-D} switch.
17817
17818 @end table
17819
17820 @node Switches for gnatprep
17821 @section Switches for @code{gnatprep}
17822
17823 @table @option
17824 @c !sort!
17825
17826 @item ^-b^/BLANK_LINES^
17827 @cindex @option{^-b^/BLANK_LINES^} (@command{gnatprep})
17828 Causes both preprocessor lines and the lines deleted by
17829 preprocessing to be replaced by blank lines in the output source file,
17830 preserving line numbers in the output file.
17831
17832 @item ^-c^/COMMENTS^
17833 @cindex @option{^-c^/COMMENTS^} (@command{gnatprep})
17834 Causes both preprocessor lines and the lines deleted
17835 by preprocessing to be retained in the output source as comments marked
17836 with the special string @code{"--! "}. This option will result in line numbers
17837 being preserved in the output file.
17838
17839 @item ^-C^/REPLACE_IN_COMMENTS^
17840 @cindex @option{^-C^/REPLACE_IN_COMMENTS^} (@command{gnatprep})
17841 Causes comments to be scanned. Normally comments are ignored by gnatprep.
17842 If this option is specified, then comments are scanned and any $symbol
17843 substitutions performed as in program text. This is particularly useful
17844 when structured comments are used (e.g., when writing programs in the
17845 SPARK dialect of Ada). Note that this switch is not available when
17846 doing integrated preprocessing (it would be useless in this context
17847 since comments are ignored by the compiler in any case).
17848
17849 @item ^-Dsymbol=value^/ASSOCIATE="symbol=value"^
17850 @cindex @option{^-D^/ASSOCIATE^} (@command{gnatprep})
17851 Defines a new preprocessing symbol, associated with value. If no value is given
17852 on the command line, then symbol is considered to be @code{True}. This switch
17853 can be used in place of a definition file.
17854
17855 @ifset vms
17856 @item /REMOVE
17857 @cindex @option{/REMOVE} (@command{gnatprep})
17858 This is the default setting which causes lines deleted by preprocessing
17859 to be entirely removed from the output file.
17860 @end ifset
17861
17862 @item ^-r^/REFERENCE^
17863 @cindex @option{^-r^/REFERENCE^} (@command{gnatprep})
17864 Causes a @code{Source_Reference} pragma to be generated that
17865 references the original input file, so that error messages will use
17866 the file name of this original file. The use of this switch implies
17867 that preprocessor lines are not to be removed from the file, so its
17868 use will force @option{^-b^/BLANK_LINES^} mode if
17869 @option{^-c^/COMMENTS^}
17870 has not been specified explicitly.
17871
17872 Note that if the file to be preprocessed contains multiple units, then
17873 it will be necessary to @code{gnatchop} the output file from
17874 @code{gnatprep}. If a @code{Source_Reference} pragma is present
17875 in the preprocessed file, it will be respected by
17876 @code{gnatchop ^-r^/REFERENCE^}
17877 so that the final chopped files will correctly refer to the original
17878 input source file for @code{gnatprep}.
17879
17880 @item ^-s^/SYMBOLS^
17881 @cindex @option{^-s^/SYMBOLS^} (@command{gnatprep})
17882 Causes a sorted list of symbol names and values to be
17883 listed on the standard output file.
17884
17885 @item ^-u^/UNDEFINED^
17886 @cindex @option{^-u^/UNDEFINED^} (@command{gnatprep})
17887 Causes undefined symbols to be treated as having the value FALSE in the context
17888 of a preprocessor test. In the absence of this option, an undefined symbol in
17889 a @code{#if} or @code{#elsif} test will be treated as an error.
17890
17891 @end table
17892
17893 @ifclear vms
17894 @noindent
17895 Note: if neither @option{-b} nor @option{-c} is present,
17896 then preprocessor lines and
17897 deleted lines are completely removed from the output, unless -r is
17898 specified, in which case -b is assumed.
17899 @end ifclear
17900
17901 @node Form of Definitions File
17902 @section Form of Definitions File
17903
17904 @noindent
17905 The definitions file contains lines of the form
17906
17907 @smallexample
17908 symbol := value
17909 @end smallexample
17910
17911 @noindent
17912 where symbol is a preprocessing symbol, and value is one of the following:
17913
17914 @itemize @bullet
17915 @item
17916 Empty, corresponding to a null substitution
17917 @item
17918 A string literal using normal Ada syntax
17919 @item
17920 Any sequence of characters from the set
17921 (letters, digits, period, underline).
17922 @end itemize
17923
17924 @noindent
17925 Comment lines may also appear in the definitions file, starting with
17926 the usual @code{--},
17927 and comments may be added to the definitions lines.
17928
17929 @node Form of Input Text for gnatprep
17930 @section Form of Input Text for @code{gnatprep}
17931
17932 @noindent
17933 The input text may contain preprocessor conditional inclusion lines,
17934 as well as general symbol substitution sequences.
17935
17936 The preprocessor conditional inclusion commands have the form
17937
17938 @smallexample
17939 @group
17940 @cartouche
17941 #if @i{expression} @r{[}then@r{]}
17942 lines
17943 #elsif @i{expression} @r{[}then@r{]}
17944 lines
17945 #elsif @i{expression} @r{[}then@r{]}
17946 lines
17947 @dots{}
17948 #else
17949 lines
17950 #end if;
17951 @end cartouche
17952 @end group
17953 @end smallexample
17954
17955 @noindent
17956 In this example, @i{expression} is defined by the following grammar:
17957 @smallexample
17958 @i{expression} ::= <symbol>
17959 @i{expression} ::= <symbol> = "<value>"
17960 @i{expression} ::= <symbol> = <symbol>
17961 @i{expression} ::= <symbol> 'Defined
17962 @i{expression} ::= not @i{expression}
17963 @i{expression} ::= @i{expression} and @i{expression}
17964 @i{expression} ::= @i{expression} or @i{expression}
17965 @i{expression} ::= @i{expression} and then @i{expression}
17966 @i{expression} ::= @i{expression} or else @i{expression}
17967 @i{expression} ::= ( @i{expression} )
17968 @end smallexample
17969
17970 The following restriction exists: it is not allowed to have "and" or "or"
17971 following "not" in the same expression without parentheses. For example, this
17972 is not allowed:
17973
17974 @smallexample
17975 not X or Y
17976 @end smallexample
17977
17978 This should be one of the following:
17979
17980 @smallexample
17981 (not X) or Y
17982 not (X or Y)
17983 @end smallexample
17984
17985 @noindent
17986 For the first test (@i{expression} ::= <symbol>) the symbol must have
17987 either the value true or false, that is to say the right-hand of the
17988 symbol definition must be one of the (case-insensitive) literals
17989 @code{True} or @code{False}. If the value is true, then the
17990 corresponding lines are included, and if the value is false, they are
17991 excluded.
17992
17993 The test (@i{expression} ::= <symbol> @code{'Defined}) is true only if
17994 the symbol has been defined in the definition file or by a @option{-D}
17995 switch on the command line. Otherwise, the test is false.
17996
17997 The equality tests are case insensitive, as are all the preprocessor lines.
17998
17999 If the symbol referenced is not defined in the symbol definitions file,
18000 then the effect depends on whether or not switch @option{-u}
18001 is specified. If so, then the symbol is treated as if it had the value
18002 false and the test fails. If this switch is not specified, then
18003 it is an error to reference an undefined symbol. It is also an error to
18004 reference a symbol that is defined with a value other than @code{True}
18005 or @code{False}.
18006
18007 The use of the @code{not} operator inverts the sense of this logical test.
18008 The @code{not} operator cannot be combined with the @code{or} or @code{and}
18009 operators, without parentheses. For example, "if not X or Y then" is not
18010 allowed, but "if (not X) or Y then" and "if not (X or Y) then" are.
18011
18012 The @code{then} keyword is optional as shown
18013
18014 The @code{#} must be the first non-blank character on a line, but
18015 otherwise the format is free form. Spaces or tabs may appear between
18016 the @code{#} and the keyword. The keywords and the symbols are case
18017 insensitive as in normal Ada code. Comments may be used on a
18018 preprocessor line, but other than that, no other tokens may appear on a
18019 preprocessor line. Any number of @code{elsif} clauses can be present,
18020 including none at all. The @code{else} is optional, as in Ada.
18021
18022 The @code{#} marking the start of a preprocessor line must be the first
18023 non-blank character on the line, i.e., it must be preceded only by
18024 spaces or horizontal tabs.
18025
18026 Symbol substitution outside of preprocessor lines is obtained by using
18027 the sequence
18028
18029 @smallexample
18030 $symbol
18031 @end smallexample
18032
18033 @noindent
18034 anywhere within a source line, except in a comment or within a
18035 string literal. The identifier
18036 following the @code{$} must match one of the symbols defined in the symbol
18037 definition file, and the result is to substitute the value of the
18038 symbol in place of @code{$symbol} in the output file.
18039
18040 Note that although the substitution of strings within a string literal
18041 is not possible, it is possible to have a symbol whose defined value is
18042 a string literal. So instead of setting XYZ to @code{hello} and writing:
18043
18044 @smallexample
18045 Header : String := "$XYZ";
18046 @end smallexample
18047
18048 @noindent
18049 you should set XYZ to @code{"hello"} and write:
18050
18051 @smallexample
18052 Header : String := $XYZ;
18053 @end smallexample
18054
18055 @noindent
18056 and then the substitution will occur as desired.
18057
18058 @ifset vms
18059 @node The GNAT Run-Time Library Builder gnatlbr
18060 @chapter The GNAT Run-Time Library Builder @code{gnatlbr}
18061 @findex gnatlbr
18062 @cindex Library builder
18063
18064 @noindent
18065 @code{gnatlbr} is a tool for rebuilding the GNAT run time with user
18066 supplied configuration pragmas.
18067
18068 @menu
18069 * Running gnatlbr::
18070 * Switches for gnatlbr::
18071 * Examples of gnatlbr Usage::
18072 @end menu
18073
18074 @node Running gnatlbr
18075 @section Running @code{gnatlbr}
18076
18077 @noindent
18078 The @code{gnatlbr} command has the form
18079
18080 @smallexample
18081 $ GNAT LIBRARY /@r{[}CREATE@r{|}SET@r{|}DELETE@r{]}=directory @r{[}/CONFIG=file@r{]}
18082 @end smallexample
18083
18084 @node Switches for gnatlbr
18085 @section Switches for @code{gnatlbr}
18086
18087 @noindent
18088 @code{gnatlbr} recognizes the following switches:
18089
18090 @table @option
18091 @c !sort!
18092 @item /CREATE=directory
18093 @cindex @code{/CREATE} (@code{gnatlbr})
18094 Create the new run-time library in the specified directory.
18095
18096 @item /SET=directory
18097 @cindex @code{/SET} (@code{gnatlbr})
18098 Make the library in the specified directory the current run-time library.
18099
18100 @item /DELETE=directory
18101 @cindex @code{/DELETE} (@code{gnatlbr})
18102 Delete the run-time library in the specified directory.
18103
18104 @item /CONFIG=file
18105 @cindex @code{/CONFIG} (@code{gnatlbr})
18106 With /CREATE: Use the configuration pragmas in the specified file when
18107 building the library.
18108
18109 With /SET: Use the configuration pragmas in the specified file when
18110 compiling.
18111
18112 @end table
18113
18114 @node Examples of gnatlbr Usage
18115 @section Example of @code{gnatlbr} Usage
18116
18117 @smallexample
18118 Contents of VAXFLOAT.ADC:
18119 pragma Float_Representation (VAX_Float);
18120
18121 $ GNAT LIBRARY /CREATE=[.VAXFLOAT] /CONFIG=VAXFLOAT.ADC
18122
18123 GNAT LIBRARY rebuilds the run-time library in directory [.VAXFLOAT]
18124
18125 @end smallexample
18126 @end ifset
18127
18128 @node The GNAT Library Browser gnatls
18129 @chapter The GNAT Library Browser @code{gnatls}
18130 @findex gnatls
18131 @cindex Library browser
18132
18133 @noindent
18134 @code{gnatls} is a tool that outputs information about compiled
18135 units. It gives the relationship between objects, unit names and source
18136 files. It can also be used to check the source dependencies of a unit
18137 as well as various characteristics.
18138
18139 Note: to invoke @code{gnatls} with a project file, use the @code{gnat}
18140 driver (see @ref{The GNAT Driver and Project Files}).
18141
18142 @menu
18143 * Running gnatls::
18144 * Switches for gnatls::
18145 * Examples of gnatls Usage::
18146 @end menu
18147
18148 @node Running gnatls
18149 @section Running @code{gnatls}
18150
18151 @noindent
18152 The @code{gnatls} command has the form
18153
18154 @smallexample
18155 $ gnatls switches @var{object_or_ali_file}
18156 @end smallexample
18157
18158 @noindent
18159 The main argument is the list of object or @file{ali} files
18160 (@pxref{The Ada Library Information Files})
18161 for which information is requested.
18162
18163 In normal mode, without additional option, @code{gnatls} produces a
18164 four-column listing. Each line represents information for a specific
18165 object. The first column gives the full path of the object, the second
18166 column gives the name of the principal unit in this object, the third
18167 column gives the status of the source and the fourth column gives the
18168 full path of the source representing this unit.
18169 Here is a simple example of use:
18170
18171 @smallexample
18172 $ gnatls *.o
18173 ^./^[]^demo1.o demo1 DIF demo1.adb
18174 ^./^[]^demo2.o demo2 OK demo2.adb
18175 ^./^[]^hello.o h1 OK hello.adb
18176 ^./^[]^instr-child.o instr.child MOK instr-child.adb
18177 ^./^[]^instr.o instr OK instr.adb
18178 ^./^[]^tef.o tef DIF tef.adb
18179 ^./^[]^text_io_example.o text_io_example OK text_io_example.adb
18180 ^./^[]^tgef.o tgef DIF tgef.adb
18181 @end smallexample
18182
18183 @noindent
18184 The first line can be interpreted as follows: the main unit which is
18185 contained in
18186 object file @file{demo1.o} is demo1, whose main source is in
18187 @file{demo1.adb}. Furthermore, the version of the source used for the
18188 compilation of demo1 has been modified (DIF). Each source file has a status
18189 qualifier which can be:
18190
18191 @table @code
18192 @item OK (unchanged)
18193 The version of the source file used for the compilation of the
18194 specified unit corresponds exactly to the actual source file.
18195
18196 @item MOK (slightly modified)
18197 The version of the source file used for the compilation of the
18198 specified unit differs from the actual source file but not enough to
18199 require recompilation. If you use gnatmake with the qualifier
18200 @option{^-m (minimal recompilation)^/MINIMAL_RECOMPILATION^}, a file marked
18201 MOK will not be recompiled.
18202
18203 @item DIF (modified)
18204 No version of the source found on the path corresponds to the source
18205 used to build this object.
18206
18207 @item ??? (file not found)
18208 No source file was found for this unit.
18209
18210 @item HID (hidden, unchanged version not first on PATH)
18211 The version of the source that corresponds exactly to the source used
18212 for compilation has been found on the path but it is hidden by another
18213 version of the same source that has been modified.
18214
18215 @end table
18216
18217 @node Switches for gnatls
18218 @section Switches for @code{gnatls}
18219
18220 @noindent
18221 @code{gnatls} recognizes the following switches:
18222
18223 @table @option
18224 @c !sort!
18225 @cindex @option{--version} @command{gnatls}
18226 Display Copyright and version, then exit disregarding all other options.
18227
18228 @item --help
18229 @cindex @option{--help} @command{gnatls}
18230 If @option{--version} was not used, display usage, then exit disregarding
18231 all other options.
18232
18233 @item ^-a^/ALL_UNITS^
18234 @cindex @option{^-a^/ALL_UNITS^} (@code{gnatls})
18235 Consider all units, including those of the predefined Ada library.
18236 Especially useful with @option{^-d^/DEPENDENCIES^}.
18237
18238 @item ^-d^/DEPENDENCIES^
18239 @cindex @option{^-d^/DEPENDENCIES^} (@code{gnatls})
18240 List sources from which specified units depend on.
18241
18242 @item ^-h^/OUTPUT=OPTIONS^
18243 @cindex @option{^-h^/OUTPUT=OPTIONS^} (@code{gnatls})
18244 Output the list of options.
18245
18246 @item ^-o^/OUTPUT=OBJECTS^
18247 @cindex @option{^-o^/OUTPUT=OBJECTS^} (@code{gnatls})
18248 Only output information about object files.
18249
18250 @item ^-s^/OUTPUT=SOURCES^
18251 @cindex @option{^-s^/OUTPUT=SOURCES^} (@code{gnatls})
18252 Only output information about source files.
18253
18254 @item ^-u^/OUTPUT=UNITS^
18255 @cindex @option{^-u^/OUTPUT=UNITS^} (@code{gnatls})
18256 Only output information about compilation units.
18257
18258 @item ^-files^/FILES^=@var{file}
18259 @cindex @option{^-files^/FILES^} (@code{gnatls})
18260 Take as arguments the files listed in text file @var{file}.
18261 Text file @var{file} may contain empty lines that are ignored.
18262 Each nonempty line should contain the name of an existing file.
18263 Several such switches may be specified simultaneously.
18264
18265 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
18266 @itemx ^-aI^/SOURCE_SEARCH=^@var{dir}
18267 @itemx ^-I^/SEARCH=^@var{dir}
18268 @itemx ^-I-^/NOCURRENT_DIRECTORY^
18269 @itemx -nostdinc
18270 @cindex @option{^-aO^/OBJECT_SEARCH^} (@code{gnatls})
18271 @cindex @option{^-aI^/SOURCE_SEARCH^} (@code{gnatls})
18272 @cindex @option{^-I^/SEARCH^} (@code{gnatls})
18273 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatls})
18274 Source path manipulation. Same meaning as the equivalent @command{gnatmake}
18275 flags (@pxref{Switches for gnatmake}).
18276
18277 @item --RTS=@var{rts-path}
18278 @cindex @option{--RTS} (@code{gnatls})
18279 Specifies the default location of the runtime library. Same meaning as the
18280 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
18281
18282 @item ^-v^/OUTPUT=VERBOSE^
18283 @cindex @option{^-v^/OUTPUT=VERBOSE^} (@code{gnatls})
18284 Verbose mode. Output the complete source, object and project paths. Do not use
18285 the default column layout but instead use long format giving as much as
18286 information possible on each requested units, including special
18287 characteristics such as:
18288
18289 @table @code
18290 @item Preelaborable
18291 The unit is preelaborable in the Ada sense.
18292
18293 @item No_Elab_Code
18294 No elaboration code has been produced by the compiler for this unit.
18295
18296 @item Pure
18297 The unit is pure in the Ada sense.
18298
18299 @item Elaborate_Body
18300 The unit contains a pragma Elaborate_Body.
18301
18302 @item Remote_Types
18303 The unit contains a pragma Remote_Types.
18304
18305 @item Shared_Passive
18306 The unit contains a pragma Shared_Passive.
18307
18308 @item Predefined
18309 This unit is part of the predefined environment and cannot be modified
18310 by the user.
18311
18312 @item Remote_Call_Interface
18313 The unit contains a pragma Remote_Call_Interface.
18314
18315 @end table
18316
18317 @end table
18318
18319 @node Examples of gnatls Usage
18320 @section Example of @code{gnatls} Usage
18321 @ifclear vms
18322
18323 @noindent
18324 Example of using the verbose switch. Note how the source and
18325 object paths are affected by the -I switch.
18326
18327 @smallexample
18328 $ gnatls -v -I.. demo1.o
18329
18330 GNATLS 5.03w (20041123-34)
18331 Copyright 1997-2004 Free Software Foundation, Inc.
18332
18333 Source Search Path:
18334 <Current_Directory>
18335 ../
18336 /home/comar/local/adainclude/
18337
18338 Object Search Path:
18339 <Current_Directory>
18340 ../
18341 /home/comar/local/lib/gcc-lib/x86-linux/3.4.3/adalib/
18342
18343 Project Search Path:
18344 <Current_Directory>
18345 /home/comar/local/lib/gnat/
18346
18347 ./demo1.o
18348 Unit =>
18349 Name => demo1
18350 Kind => subprogram body
18351 Flags => No_Elab_Code
18352 Source => demo1.adb modified
18353 @end smallexample
18354
18355 @noindent
18356 The following is an example of use of the dependency list.
18357 Note the use of the -s switch
18358 which gives a straight list of source files. This can be useful for
18359 building specialized scripts.
18360
18361 @smallexample
18362 $ gnatls -d demo2.o
18363 ./demo2.o demo2 OK demo2.adb
18364 OK gen_list.ads
18365 OK gen_list.adb
18366 OK instr.ads
18367 OK instr-child.ads
18368
18369 $ gnatls -d -s -a demo1.o
18370 demo1.adb
18371 /home/comar/local/adainclude/ada.ads
18372 /home/comar/local/adainclude/a-finali.ads
18373 /home/comar/local/adainclude/a-filico.ads
18374 /home/comar/local/adainclude/a-stream.ads
18375 /home/comar/local/adainclude/a-tags.ads
18376 gen_list.ads
18377 gen_list.adb
18378 /home/comar/local/adainclude/gnat.ads
18379 /home/comar/local/adainclude/g-io.ads
18380 instr.ads
18381 /home/comar/local/adainclude/system.ads
18382 /home/comar/local/adainclude/s-exctab.ads
18383 /home/comar/local/adainclude/s-finimp.ads
18384 /home/comar/local/adainclude/s-finroo.ads
18385 /home/comar/local/adainclude/s-secsta.ads
18386 /home/comar/local/adainclude/s-stalib.ads
18387 /home/comar/local/adainclude/s-stoele.ads
18388 /home/comar/local/adainclude/s-stratt.ads
18389 /home/comar/local/adainclude/s-tasoli.ads
18390 /home/comar/local/adainclude/s-unstyp.ads
18391 /home/comar/local/adainclude/unchconv.ads
18392 @end smallexample
18393 @end ifclear
18394
18395 @ifset vms
18396 @smallexample
18397 GNAT LIST /DEPENDENCIES /OUTPUT=SOURCES /ALL_UNITS DEMO1.ADB
18398
18399 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]ada.ads
18400 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-finali.ads
18401 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-filico.ads
18402 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-stream.ads
18403 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-tags.ads
18404 demo1.adb
18405 gen_list.ads
18406 gen_list.adb
18407 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]gnat.ads
18408 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]g-io.ads
18409 instr.ads
18410 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]system.ads
18411 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-exctab.ads
18412 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-finimp.ads
18413 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-finroo.ads
18414 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-secsta.ads
18415 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stalib.ads
18416 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stoele.ads
18417 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stratt.ads
18418 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-tasoli.ads
18419 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-unstyp.ads
18420 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]unchconv.ads
18421 @end smallexample
18422 @end ifset
18423
18424 @node Cleaning Up Using gnatclean
18425 @chapter Cleaning Up Using @code{gnatclean}
18426 @findex gnatclean
18427 @cindex Cleaning tool
18428
18429 @noindent
18430 @code{gnatclean} is a tool that allows the deletion of files produced by the
18431 compiler, binder and linker, including ALI files, object files, tree files,
18432 expanded source files, library files, interface copy source files, binder
18433 generated files and executable files.
18434
18435 @menu
18436 * Running gnatclean::
18437 * Switches for gnatclean::
18438 @c * Examples of gnatclean Usage::
18439 @end menu
18440
18441 @node Running gnatclean
18442 @section Running @code{gnatclean}
18443
18444 @noindent
18445 The @code{gnatclean} command has the form:
18446
18447 @smallexample
18448 $ gnatclean switches @var{names}
18449 @end smallexample
18450
18451 @noindent
18452 @var{names} is a list of source file names. Suffixes @code{.^ads^ADS^} and
18453 @code{^adb^ADB^} may be omitted. If a project file is specified using switch
18454 @code{^-P^/PROJECT_FILE=^}, then @var{names} may be completely omitted.
18455
18456 @noindent
18457 In normal mode, @code{gnatclean} delete the files produced by the compiler and,
18458 if switch @code{^-c^/COMPILER_FILES_ONLY^} is not specified, by the binder and
18459 the linker. In informative-only mode, specified by switch
18460 @code{^-n^/NODELETE^}, the list of files that would have been deleted in
18461 normal mode is listed, but no file is actually deleted.
18462
18463 @node Switches for gnatclean
18464 @section Switches for @code{gnatclean}
18465
18466 @noindent
18467 @code{gnatclean} recognizes the following switches:
18468
18469 @table @option
18470 @c !sort!
18471 @cindex @option{--version} @command{gnatclean}
18472 Display Copyright and version, then exit disregarding all other options.
18473
18474 @item --help
18475 @cindex @option{--help} @command{gnatclean}
18476 If @option{--version} was not used, display usage, then exit disregarding
18477 all other options.
18478
18479 @item ^-c^/COMPILER_FILES_ONLY^
18480 @cindex @option{^-c^/COMPILER_FILES_ONLY^} (@code{gnatclean})
18481 Only attempt to delete the files produced by the compiler, not those produced
18482 by the binder or the linker. The files that are not to be deleted are library
18483 files, interface copy files, binder generated files and executable files.
18484
18485 @item ^-D ^/DIRECTORY_OBJECTS=^@var{dir}
18486 @cindex @option{^-D^/DIRECTORY_OBJECTS^} (@code{gnatclean})
18487 Indicate that ALI and object files should normally be found in directory
18488 @var{dir}.
18489
18490 @item ^-F^/FULL_PATH_IN_BRIEF_MESSAGES^
18491 @cindex @option{^-F^/FULL_PATH_IN_BRIEF_MESSAGES^} (@code{gnatclean})
18492 When using project files, if some errors or warnings are detected during
18493 parsing and verbose mode is not in effect (no use of switch
18494 ^-v^/VERBOSE^), then error lines start with the full path name of the project
18495 file, rather than its simple file name.
18496
18497 @item ^-h^/HELP^
18498 @cindex @option{^-h^/HELP^} (@code{gnatclean})
18499 Output a message explaining the usage of @code{^gnatclean^gnatclean^}.
18500
18501 @item ^-n^/NODELETE^
18502 @cindex @option{^-n^/NODELETE^} (@code{gnatclean})
18503 Informative-only mode. Do not delete any files. Output the list of the files
18504 that would have been deleted if this switch was not specified.
18505
18506 @item ^-P^/PROJECT_FILE=^@var{project}
18507 @cindex @option{^-P^/PROJECT_FILE^} (@code{gnatclean})
18508 Use project file @var{project}. Only one such switch can be used.
18509 When cleaning a project file, the files produced by the compilation of the
18510 immediate sources or inherited sources of the project files are to be
18511 deleted. This is not depending on the presence or not of executable names
18512 on the command line.
18513
18514 @item ^-q^/QUIET^
18515 @cindex @option{^-q^/QUIET^} (@code{gnatclean})
18516 Quiet output. If there are no errors, do not output anything, except in
18517 verbose mode (switch ^-v^/VERBOSE^) or in informative-only mode
18518 (switch ^-n^/NODELETE^).
18519
18520 @item ^-r^/RECURSIVE^
18521 @cindex @option{^-r^/RECURSIVE^} (@code{gnatclean})
18522 When a project file is specified (using switch ^-P^/PROJECT_FILE=^),
18523 clean all imported and extended project files, recursively. If this switch
18524 is not specified, only the files related to the main project file are to be
18525 deleted. This switch has no effect if no project file is specified.
18526
18527 @item ^-v^/VERBOSE^
18528 @cindex @option{^-v^/VERBOSE^} (@code{gnatclean})
18529 Verbose mode.
18530
18531 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
18532 @cindex @option{^-vP^/MESSAGES_PROJECT_FILE^} (@code{gnatclean})
18533 Indicates the verbosity of the parsing of GNAT project files.
18534 @xref{Switches Related to Project Files}.
18535
18536 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
18537 @cindex @option{^-X^/EXTERNAL_REFERENCE^} (@code{gnatclean})
18538 Indicates that external variable @var{name} has the value @var{value}.
18539 The Project Manager will use this value for occurrences of
18540 @code{external(name)} when parsing the project file.
18541 @xref{Switches Related to Project Files}.
18542
18543 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
18544 @cindex @option{^-aO^/OBJECT_SEARCH^} (@code{gnatclean})
18545 When searching for ALI and object files, look in directory
18546 @var{dir}.
18547
18548 @item ^-I^/SEARCH=^@var{dir}
18549 @cindex @option{^-I^/SEARCH^} (@code{gnatclean})
18550 Equivalent to @option{^-aO^/OBJECT_SEARCH=^@var{dir}}.
18551
18552 @item ^-I-^/NOCURRENT_DIRECTORY^
18553 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatclean})
18554 @cindex Source files, suppressing search
18555 Do not look for ALI or object files in the directory
18556 where @code{gnatclean} was invoked.
18557
18558 @end table
18559
18560 @c @node Examples of gnatclean Usage
18561 @c @section Examples of @code{gnatclean} Usage
18562
18563 @ifclear vms
18564 @node GNAT and Libraries
18565 @chapter GNAT and Libraries
18566 @cindex Library, building, installing, using
18567
18568 @noindent
18569 This chapter describes how to build and use libraries with GNAT, and also shows
18570 how to recompile the GNAT run-time library. You should be familiar with the
18571 Project Manager facility (@pxref{GNAT Project Manager}) before reading this
18572 chapter.
18573
18574 @menu
18575 * Introduction to Libraries in GNAT::
18576 * General Ada Libraries::
18577 * Stand-alone Ada Libraries::
18578 * Rebuilding the GNAT Run-Time Library::
18579 @end menu
18580
18581 @node Introduction to Libraries in GNAT
18582 @section Introduction to Libraries in GNAT
18583
18584 @noindent
18585 A library is, conceptually, a collection of objects which does not have its
18586 own main thread of execution, but rather provides certain services to the
18587 applications that use it. A library can be either statically linked with the
18588 application, in which case its code is directly included in the application,
18589 or, on platforms that support it, be dynamically linked, in which case
18590 its code is shared by all applications making use of this library.
18591
18592 GNAT supports both types of libraries.
18593 In the static case, the compiled code can be provided in different ways. The
18594 simplest approach is to provide directly the set of objects resulting from
18595 compilation of the library source files. Alternatively, you can group the
18596 objects into an archive using whatever commands are provided by the operating
18597 system. For the latter case, the objects are grouped into a shared library.
18598
18599 In the GNAT environment, a library has three types of components:
18600 @itemize @bullet
18601 @item
18602 Source files.
18603 @item
18604 @file{ALI} files.
18605 @xref{The Ada Library Information Files}.
18606 @item
18607 Object files, an archive or a shared library.
18608 @end itemize
18609
18610 @noindent
18611 A GNAT library may expose all its source files, which is useful for
18612 documentation purposes. Alternatively, it may expose only the units needed by
18613 an external user to make use of the library. That is to say, the specs
18614 reflecting the library services along with all the units needed to compile
18615 those specs, which can include generic bodies or any body implementing an
18616 inlined routine. In the case of @emph{stand-alone libraries} those exposed
18617 units are called @emph{interface units} (@pxref{Stand-alone Ada Libraries}).
18618
18619 All compilation units comprising an application, including those in a library,
18620 need to be elaborated in an order partially defined by Ada's semantics. GNAT
18621 computes the elaboration order from the @file{ALI} files and this is why they
18622 constitute a mandatory part of GNAT libraries. Except in the case of
18623 @emph{stand-alone libraries}, where a specific library elaboration routine is
18624 produced independently of the application(s) using the library.
18625
18626 @node General Ada Libraries
18627 @section General Ada Libraries
18628
18629 @menu
18630 * Building a library::
18631 * Installing a library::
18632 * Using a library::
18633 @end menu
18634
18635 @node Building a library
18636 @subsection Building a library
18637
18638 @noindent
18639 The easiest way to build a library is to use the Project Manager,
18640 which supports a special type of project called a @emph{Library Project}
18641 (@pxref{Library Projects}).
18642
18643 A project is considered a library project, when two project-level attributes
18644 are defined in it: @code{Library_Name} and @code{Library_Dir}. In order to
18645 control different aspects of library configuration, additional optional
18646 project-level attributes can be specified:
18647 @table @code
18648 @item Library_Kind
18649 This attribute controls whether the library is to be static or dynamic
18650
18651 @item Library_Version
18652 This attribute specifies the library version; this value is used
18653 during dynamic linking of shared libraries to determine if the currently
18654 installed versions of the binaries are compatible.
18655
18656 @item Library_Options
18657 @item Library_GCC
18658 These attributes specify additional low-level options to be used during
18659 library generation, and redefine the actual application used to generate
18660 library.
18661 @end table
18662
18663 @noindent
18664 The GNAT Project Manager takes full care of the library maintenance task,
18665 including recompilation of the source files for which objects do not exist
18666 or are not up to date, assembly of the library archive, and installation of
18667 the library (i.e., copying associated source, object and @file{ALI} files
18668 to the specified location).
18669
18670 Here is a simple library project file:
18671 @smallexample @c ada
18672 project My_Lib is
18673 for Source_Dirs use ("src1", "src2");
18674 for Object_Dir use "obj";
18675 for Library_Name use "mylib";
18676 for Library_Dir use "lib";
18677 for Library_Kind use "dynamic";
18678 end My_lib;
18679 @end smallexample
18680
18681 @noindent
18682 and the compilation command to build and install the library:
18683
18684 @smallexample @c ada
18685 $ gnatmake -Pmy_lib
18686 @end smallexample
18687
18688 @noindent
18689 It is not entirely trivial to perform manually all the steps required to
18690 produce a library. We recommend that you use the GNAT Project Manager
18691 for this task. In special cases where this is not desired, the necessary
18692 steps are discussed below.
18693
18694 There are various possibilities for compiling the units that make up the
18695 library: for example with a Makefile (@pxref{Using the GNU make Utility}) or
18696 with a conventional script. For simple libraries, it is also possible to create
18697 a dummy main program which depends upon all the packages that comprise the
18698 interface of the library. This dummy main program can then be given to
18699 @command{gnatmake}, which will ensure that all necessary objects are built.
18700
18701 After this task is accomplished, you should follow the standard procedure
18702 of the underlying operating system to produce the static or shared library.
18703
18704 Here is an example of such a dummy program:
18705 @smallexample @c ada
18706 @group
18707 with My_Lib.Service1;
18708 with My_Lib.Service2;
18709 with My_Lib.Service3;
18710 procedure My_Lib_Dummy is
18711 begin
18712 null;
18713 end;
18714 @end group
18715 @end smallexample
18716
18717 @noindent
18718 Here are the generic commands that will build an archive or a shared library.
18719
18720 @smallexample
18721 # compiling the library
18722 $ gnatmake -c my_lib_dummy.adb
18723
18724 # we don't need the dummy object itself
18725 $ rm my_lib_dummy.o my_lib_dummy.ali
18726
18727 # create an archive with the remaining objects
18728 $ ar rc libmy_lib.a *.o
18729 # some systems may require "ranlib" to be run as well
18730
18731 # or create a shared library
18732 $ gcc -shared -o libmy_lib.so *.o
18733 # some systems may require the code to have been compiled with -fPIC
18734
18735 # remove the object files that are now in the library
18736 $ rm *.o
18737
18738 # Make the ALI files read-only so that gnatmake will not try to
18739 # regenerate the objects that are in the library
18740 $ chmod -w *.ali
18741 @end smallexample
18742
18743 @noindent
18744 Please note that the library must have a name of the form @file{lib@var{xxx}.a}
18745 or @file{lib@var{xxx}.so} (or @file{lib@var{xxx}.dll} on Windows) in order to
18746 be accessed by the directive @option{-l@var{xxx}} at link time.
18747
18748 @node Installing a library
18749 @subsection Installing a library
18750 @cindex @code{ADA_PROJECT_PATH}
18751
18752 @noindent
18753 If you use project files, library installation is part of the library build
18754 process. Thus no further action is needed in order to make use of the
18755 libraries that are built as part of the general application build. A usable
18756 version of the library is installed in the directory specified by the
18757 @code{Library_Dir} attribute of the library project file.
18758
18759 You may want to install a library in a context different from where the library
18760 is built. This situation arises with third party suppliers, who may want
18761 to distribute a library in binary form where the user is not expected to be
18762 able to recompile the library. The simplest option in this case is to provide
18763 a project file slightly different from the one used to build the library, by
18764 using the @code{externally_built} attribute. For instance, the project
18765 file used to build the library in the previous section can be changed into the
18766 following one when the library is installed:
18767
18768 @smallexample @c projectfile
18769 project My_Lib is
18770 for Source_Dirs use ("src1", "src2");
18771 for Library_Name use "mylib";
18772 for Library_Dir use "lib";
18773 for Library_Kind use "dynamic";
18774 for Externally_Built use "true";
18775 end My_lib;
18776 @end smallexample
18777
18778 @noindent
18779 This project file assumes that the directories @file{src1},
18780 @file{src2}, and @file{lib} exist in
18781 the directory containing the project file. The @code{externally_built}
18782 attribute makes it clear to the GNAT builder that it should not attempt to
18783 recompile any of the units from this library. It allows the library provider to
18784 restrict the source set to the minimum necessary for clients to make use of the
18785 library as described in the first section of this chapter. It is the
18786 responsibility of the library provider to install the necessary sources, ALI
18787 files and libraries in the directories mentioned in the project file. For
18788 convenience, the user's library project file should be installed in a location
18789 that will be searched automatically by the GNAT
18790 builder. These are the directories referenced in the @env{ADA_PROJECT_PATH}
18791 environment variable (@pxref{Importing Projects}), and also the default GNAT
18792 library location that can be queried with @command{gnatls -v} and is usually of
18793 the form $gnat_install_root/lib/gnat.
18794
18795 When project files are not an option, it is also possible, but not recommended,
18796 to install the library so that the sources needed to use the library are on the
18797 Ada source path and the ALI files & libraries be on the Ada Object path (see
18798 @ref{Search Paths and the Run-Time Library (RTL)}. Alternatively, the system
18799 administrator can place general-purpose libraries in the default compiler
18800 paths, by specifying the libraries' location in the configuration files
18801 @file{ada_source_path} and @file{ada_object_path}. These configuration files
18802 must be located in the GNAT installation tree at the same place as the gcc spec
18803 file. The location of the gcc spec file can be determined as follows:
18804 @smallexample
18805 $ gcc -v
18806 @end smallexample
18807
18808 @noindent
18809 The configuration files mentioned above have a simple format: each line
18810 must contain one unique directory name.
18811 Those names are added to the corresponding path
18812 in their order of appearance in the file. The names can be either absolute
18813 or relative; in the latter case, they are relative to where theses files
18814 are located.
18815
18816 The files @file{ada_source_path} and @file{ada_object_path} might not be
18817 present in a
18818 GNAT installation, in which case, GNAT will look for its run-time library in
18819 the directories @file{adainclude} (for the sources) and @file{adalib} (for the
18820 objects and @file{ALI} files). When the files exist, the compiler does not
18821 look in @file{adainclude} and @file{adalib}, and thus the
18822 @file{ada_source_path} file
18823 must contain the location for the GNAT run-time sources (which can simply
18824 be @file{adainclude}). In the same way, the @file{ada_object_path} file must
18825 contain the location for the GNAT run-time objects (which can simply
18826 be @file{adalib}).
18827
18828 You can also specify a new default path to the run-time library at compilation
18829 time with the switch @option{--RTS=rts-path}. You can thus choose / change
18830 the run-time library you want your program to be compiled with. This switch is
18831 recognized by @command{gcc}, @command{gnatmake}, @command{gnatbind},
18832 @command{gnatls}, @command{gnatfind} and @command{gnatxref}.
18833
18834 It is possible to install a library before or after the standard GNAT
18835 library, by reordering the lines in the configuration files. In general, a
18836 library must be installed before the GNAT library if it redefines
18837 any part of it.
18838
18839 @node Using a library
18840 @subsection Using a library
18841
18842 @noindent Once again, the project facility greatly simplifies the use of
18843 libraries. In this context, using a library is just a matter of adding a
18844 @code{with} clause in the user project. For instance, to make use of the
18845 library @code{My_Lib} shown in examples in earlier sections, you can
18846 write:
18847
18848 @smallexample @c projectfile
18849 with "my_lib";
18850 project My_Proj is
18851 @dots{}
18852 end My_Proj;
18853 @end smallexample
18854
18855 Even if you have a third-party, non-Ada library, you can still use GNAT's
18856 Project Manager facility to provide a wrapper for it. For example, the
18857 following project, when @code{with}ed by your main project, will link with the
18858 third-party library @file{liba.a}:
18859
18860 @smallexample @c projectfile
18861 @group
18862 project Liba is
18863 for Externally_Built use "true";
18864 for Source_Files use ();
18865 for Library_Dir use "lib";
18866 for Library_Name use "a";
18867 for Library_Kind use "static";
18868 end Liba;
18869 @end group
18870 @end smallexample
18871 This is an alternative to the use of @code{pragma Linker_Options}. It is
18872 especially interesting in the context of systems with several interdependent
18873 static libraries where finding a proper linker order is not easy and best be
18874 left to the tools having visibility over project dependence information.
18875
18876 @noindent
18877 In order to use an Ada library manually, you need to make sure that this
18878 library is on both your source and object path
18879 (see @ref{Search Paths and the Run-Time Library (RTL)}
18880 and @ref{Search Paths for gnatbind}). Furthermore, when the objects are grouped
18881 in an archive or a shared library, you need to specify the desired
18882 library at link time.
18883
18884 For example, you can use the library @file{mylib} installed in
18885 @file{/dir/my_lib_src} and @file{/dir/my_lib_obj} with the following commands:
18886
18887 @smallexample
18888 $ gnatmake -aI/dir/my_lib_src -aO/dir/my_lib_obj my_appl \
18889 -largs -lmy_lib
18890 @end smallexample
18891
18892 @noindent
18893 This can be expressed more simply:
18894 @smallexample
18895 $ gnatmake my_appl
18896 @end smallexample
18897 @noindent
18898 when the following conditions are met:
18899 @itemize @bullet
18900 @item
18901 @file{/dir/my_lib_src} has been added by the user to the environment
18902 variable @env{ADA_INCLUDE_PATH}, or by the administrator to the file
18903 @file{ada_source_path}
18904 @item
18905 @file{/dir/my_lib_obj} has been added by the user to the environment
18906 variable @env{ADA_OBJECTS_PATH}, or by the administrator to the file
18907 @file{ada_object_path}
18908 @item
18909 a pragma @code{Linker_Options} has been added to one of the sources.
18910 For example:
18911
18912 @smallexample @c ada
18913 pragma Linker_Options ("-lmy_lib");
18914 @end smallexample
18915 @end itemize
18916
18917 @node Stand-alone Ada Libraries
18918 @section Stand-alone Ada Libraries
18919 @cindex Stand-alone library, building, using
18920
18921 @menu
18922 * Introduction to Stand-alone Libraries::
18923 * Building a Stand-alone Library::
18924 * Creating a Stand-alone Library to be used in a non-Ada context::
18925 * Restrictions in Stand-alone Libraries::
18926 @end menu
18927
18928 @node Introduction to Stand-alone Libraries
18929 @subsection Introduction to Stand-alone Libraries
18930
18931 @noindent
18932 A Stand-alone Library (abbreviated ``SAL'') is a library that contains the
18933 necessary code to
18934 elaborate the Ada units that are included in the library. In contrast with
18935 an ordinary library, which consists of all sources, objects and @file{ALI}
18936 files of the
18937 library, a SAL may specify a restricted subset of compilation units
18938 to serve as a library interface. In this case, the fully
18939 self-sufficient set of files will normally consist of an objects
18940 archive, the sources of interface units' specs, and the @file{ALI}
18941 files of interface units.
18942 If an interface spec contains a generic unit or an inlined subprogram,
18943 the body's
18944 source must also be provided; if the units that must be provided in the source
18945 form depend on other units, the source and @file{ALI} files of those must
18946 also be provided.
18947
18948 The main purpose of a SAL is to minimize the recompilation overhead of client
18949 applications when a new version of the library is installed. Specifically,
18950 if the interface sources have not changed, client applications do not need to
18951 be recompiled. If, furthermore, a SAL is provided in the shared form and its
18952 version, controlled by @code{Library_Version} attribute, is not changed,
18953 then the clients do not need to be relinked.
18954
18955 SALs also allow the library providers to minimize the amount of library source
18956 text exposed to the clients. Such ``information hiding'' might be useful or
18957 necessary for various reasons.
18958
18959 Stand-alone libraries are also well suited to be used in an executable whose
18960 main routine is not written in Ada.
18961
18962 @node Building a Stand-alone Library
18963 @subsection Building a Stand-alone Library
18964
18965 @noindent
18966 GNAT's Project facility provides a simple way of building and installing
18967 stand-alone libraries; see @ref{Stand-alone Library Projects}.
18968 To be a Stand-alone Library Project, in addition to the two attributes
18969 that make a project a Library Project (@code{Library_Name} and
18970 @code{Library_Dir}; see @ref{Library Projects}), the attribute
18971 @code{Library_Interface} must be defined. For example:
18972
18973 @smallexample @c projectfile
18974 @group
18975 for Library_Dir use "lib_dir";
18976 for Library_Name use "dummy";
18977 for Library_Interface use ("int1", "int1.child");
18978 @end group
18979 @end smallexample
18980
18981 @noindent
18982 Attribute @code{Library_Interface} has a non-empty string list value,
18983 each string in the list designating a unit contained in an immediate source
18984 of the project file.
18985
18986 When a Stand-alone Library is built, first the binder is invoked to build
18987 a package whose name depends on the library name
18988 (@file{^b~dummy.ads/b^B$DUMMY.ADS/B^} in the example above).
18989 This binder-generated package includes initialization and
18990 finalization procedures whose
18991 names depend on the library name (@code{dummyinit} and @code{dummyfinal}
18992 in the example
18993 above). The object corresponding to this package is included in the library.
18994
18995 You must ensure timely (e.g., prior to any use of interfaces in the SAL)
18996 calling of these procedures if a static SAL is built, or if a shared SAL
18997 is built
18998 with the project-level attribute @code{Library_Auto_Init} set to
18999 @code{"false"}.
19000
19001 For a Stand-Alone Library, only the @file{ALI} files of the Interface Units
19002 (those that are listed in attribute @code{Library_Interface}) are copied to
19003 the Library Directory. As a consequence, only the Interface Units may be
19004 imported from Ada units outside of the library. If other units are imported,
19005 the binding phase will fail.
19006
19007 The attribute @code{Library_Src_Dir} may be specified for a
19008 Stand-Alone Library. @code{Library_Src_Dir} is a simple attribute that has a
19009 single string value. Its value must be the path (absolute or relative to the
19010 project directory) of an existing directory. This directory cannot be the
19011 object directory or one of the source directories, but it can be the same as
19012 the library directory. The sources of the Interface
19013 Units of the library that are needed by an Ada client of the library will be
19014 copied to the designated directory, called the Interface Copy directory.
19015 These sources include the specs of the Interface Units, but they may also
19016 include bodies and subunits, when pragmas @code{Inline} or @code{Inline_Always}
19017 are used, or when there is a generic unit in the spec. Before the sources
19018 are copied to the Interface Copy directory, an attempt is made to delete all
19019 files in the Interface Copy directory.
19020
19021 Building stand-alone libraries by hand is somewhat tedious, but for those
19022 occasions when it is necessary here are the steps that you need to perform:
19023 @itemize @bullet
19024 @item
19025 Compile all library sources.
19026
19027 @item
19028 Invoke the binder with the switch @option{-n} (No Ada main program),
19029 with all the @file{ALI} files of the interfaces, and
19030 with the switch @option{-L} to give specific names to the @code{init}
19031 and @code{final} procedures. For example:
19032 @smallexample
19033 gnatbind -n int1.ali int2.ali -Lsal1
19034 @end smallexample
19035
19036 @item
19037 Compile the binder generated file:
19038 @smallexample
19039 gcc -c b~int2.adb
19040 @end smallexample
19041
19042 @item
19043 Link the dynamic library with all the necessary object files,
19044 indicating to the linker the names of the @code{init} (and possibly
19045 @code{final}) procedures for automatic initialization (and finalization).
19046 The built library should be placed in a directory different from
19047 the object directory.
19048
19049 @item
19050 Copy the @code{ALI} files of the interface to the library directory,
19051 add in this copy an indication that it is an interface to a SAL
19052 (i.e., add a word @option{SL} on the line in the @file{ALI} file that starts
19053 with letter ``P'') and make the modified copy of the @file{ALI} file
19054 read-only.
19055 @end itemize
19056
19057 @noindent
19058 Using SALs is not different from using other libraries
19059 (see @ref{Using a library}).
19060
19061 @node Creating a Stand-alone Library to be used in a non-Ada context
19062 @subsection Creating a Stand-alone Library to be used in a non-Ada context
19063
19064 @noindent
19065 It is easy to adapt the SAL build procedure discussed above for use of a SAL in
19066 a non-Ada context.
19067
19068 The only extra step required is to ensure that library interface subprograms
19069 are compatible with the main program, by means of @code{pragma Export}
19070 or @code{pragma Convention}.
19071
19072 Here is an example of simple library interface for use with C main program:
19073
19074 @smallexample @c ada
19075 package Interface is
19076
19077 procedure Do_Something;
19078 pragma Export (C, Do_Something, "do_something");
19079
19080 procedure Do_Something_Else;
19081 pragma Export (C, Do_Something_Else, "do_something_else");
19082
19083 end Interface;
19084 @end smallexample
19085
19086 @noindent
19087 On the foreign language side, you must provide a ``foreign'' view of the
19088 library interface; remember that it should contain elaboration routines in
19089 addition to interface subprograms.
19090
19091 The example below shows the content of @code{mylib_interface.h} (note
19092 that there is no rule for the naming of this file, any name can be used)
19093 @smallexample
19094 /* the library elaboration procedure */
19095 extern void mylibinit (void);
19096
19097 /* the library finalization procedure */
19098 extern void mylibfinal (void);
19099
19100 /* the interface exported by the library */
19101 extern void do_something (void);
19102 extern void do_something_else (void);
19103 @end smallexample
19104
19105 @noindent
19106 Libraries built as explained above can be used from any program, provided
19107 that the elaboration procedures (named @code{mylibinit} in the previous
19108 example) are called before the library services are used. Any number of
19109 libraries can be used simultaneously, as long as the elaboration
19110 procedure of each library is called.
19111
19112 Below is an example of a C program that uses the @code{mylib} library.
19113
19114 @smallexample
19115 #include "mylib_interface.h"
19116
19117 int
19118 main (void)
19119 @{
19120 /* First, elaborate the library before using it */
19121 mylibinit ();
19122
19123 /* Main program, using the library exported entities */
19124 do_something ();
19125 do_something_else ();
19126
19127 /* Library finalization at the end of the program */
19128 mylibfinal ();
19129 return 0;
19130 @}
19131 @end smallexample
19132
19133 @noindent
19134 Note that invoking any library finalization procedure generated by
19135 @code{gnatbind} shuts down the Ada run-time environment.
19136 Consequently, the
19137 finalization of all Ada libraries must be performed at the end of the program.
19138 No call to these libraries or to the Ada run-time library should be made
19139 after the finalization phase.
19140
19141 @node Restrictions in Stand-alone Libraries
19142 @subsection Restrictions in Stand-alone Libraries
19143
19144 @noindent
19145 The pragmas listed below should be used with caution inside libraries,
19146 as they can create incompatibilities with other Ada libraries:
19147 @itemize @bullet
19148 @item pragma @code{Locking_Policy}
19149 @item pragma @code{Queuing_Policy}
19150 @item pragma @code{Task_Dispatching_Policy}
19151 @item pragma @code{Unreserve_All_Interrupts}
19152 @end itemize
19153
19154 @noindent
19155 When using a library that contains such pragmas, the user must make sure
19156 that all libraries use the same pragmas with the same values. Otherwise,
19157 @code{Program_Error} will
19158 be raised during the elaboration of the conflicting
19159 libraries. The usage of these pragmas and its consequences for the user
19160 should therefore be well documented.
19161
19162 Similarly, the traceback in the exception occurrence mechanism should be
19163 enabled or disabled in a consistent manner across all libraries.
19164 Otherwise, Program_Error will be raised during the elaboration of the
19165 conflicting libraries.
19166
19167 If the @code{Version} or @code{Body_Version}
19168 attributes are used inside a library, then you need to
19169 perform a @code{gnatbind} step that specifies all @file{ALI} files in all
19170 libraries, so that version identifiers can be properly computed.
19171 In practice these attributes are rarely used, so this is unlikely
19172 to be a consideration.
19173
19174 @node Rebuilding the GNAT Run-Time Library
19175 @section Rebuilding the GNAT Run-Time Library
19176 @cindex GNAT Run-Time Library, rebuilding
19177 @cindex Building the GNAT Run-Time Library
19178 @cindex Rebuilding the GNAT Run-Time Library
19179 @cindex Run-Time Library, rebuilding
19180
19181 @noindent
19182 It may be useful to recompile the GNAT library in various contexts, the
19183 most important one being the use of partition-wide configuration pragmas
19184 such as @code{Normalize_Scalars}. A special Makefile called
19185 @code{Makefile.adalib} is provided to that effect and can be found in
19186 the directory containing the GNAT library. The location of this
19187 directory depends on the way the GNAT environment has been installed and can
19188 be determined by means of the command:
19189
19190 @smallexample
19191 $ gnatls -v
19192 @end smallexample
19193
19194 @noindent
19195 The last entry in the object search path usually contains the
19196 gnat library. This Makefile contains its own documentation and in
19197 particular the set of instructions needed to rebuild a new library and
19198 to use it.
19199
19200 @node Using the GNU make Utility
19201 @chapter Using the GNU @code{make} Utility
19202 @findex make
19203
19204 @noindent
19205 This chapter offers some examples of makefiles that solve specific
19206 problems. It does not explain how to write a makefile (@pxref{Top,, GNU
19207 make, make, GNU @code{make}}), nor does it try to replace the
19208 @command{gnatmake} utility (@pxref{The GNAT Make Program gnatmake}).
19209
19210 All the examples in this section are specific to the GNU version of
19211 make. Although @command{make} is a standard utility, and the basic language
19212 is the same, these examples use some advanced features found only in
19213 @code{GNU make}.
19214
19215 @menu
19216 * Using gnatmake in a Makefile::
19217 * Automatically Creating a List of Directories::
19218 * Generating the Command Line Switches::
19219 * Overcoming Command Line Length Limits::
19220 @end menu
19221
19222 @node Using gnatmake in a Makefile
19223 @section Using gnatmake in a Makefile
19224 @findex makefile
19225 @cindex GNU make
19226
19227 @noindent
19228 Complex project organizations can be handled in a very powerful way by
19229 using GNU make combined with gnatmake. For instance, here is a Makefile
19230 which allows you to build each subsystem of a big project into a separate
19231 shared library. Such a makefile allows you to significantly reduce the link
19232 time of very big applications while maintaining full coherence at
19233 each step of the build process.
19234
19235 The list of dependencies are handled automatically by
19236 @command{gnatmake}. The Makefile is simply used to call gnatmake in each of
19237 the appropriate directories.
19238
19239 Note that you should also read the example on how to automatically
19240 create the list of directories
19241 (@pxref{Automatically Creating a List of Directories})
19242 which might help you in case your project has a lot of subdirectories.
19243
19244 @smallexample
19245 @iftex
19246 @leftskip=0cm
19247 @font@heightrm=cmr8
19248 @heightrm
19249 @end iftex
19250 ## This Makefile is intended to be used with the following directory
19251 ## configuration:
19252 ## - The sources are split into a series of csc (computer software components)
19253 ## Each of these csc is put in its own directory.
19254 ## Their name are referenced by the directory names.
19255 ## They will be compiled into shared library (although this would also work
19256 ## with static libraries
19257 ## - The main program (and possibly other packages that do not belong to any
19258 ## csc is put in the top level directory (where the Makefile is).
19259 ## toplevel_dir __ first_csc (sources) __ lib (will contain the library)
19260 ## \_ second_csc (sources) __ lib (will contain the library)
19261 ## \_ @dots{}
19262 ## Although this Makefile is build for shared library, it is easy to modify
19263 ## to build partial link objects instead (modify the lines with -shared and
19264 ## gnatlink below)
19265 ##
19266 ## With this makefile, you can change any file in the system or add any new
19267 ## file, and everything will be recompiled correctly (only the relevant shared
19268 ## objects will be recompiled, and the main program will be re-linked).
19269
19270 # The list of computer software component for your project. This might be
19271 # generated automatically.
19272 CSC_LIST=aa bb cc
19273
19274 # Name of the main program (no extension)
19275 MAIN=main
19276
19277 # If we need to build objects with -fPIC, uncomment the following line
19278 #NEED_FPIC=-fPIC
19279
19280 # The following variable should give the directory containing libgnat.so
19281 # You can get this directory through 'gnatls -v'. This is usually the last
19282 # directory in the Object_Path.
19283 GLIB=@dots{}
19284
19285 # The directories for the libraries
19286 # (This macro expands the list of CSC to the list of shared libraries, you
19287 # could simply use the expanded form:
19288 # LIB_DIR=aa/lib/libaa.so bb/lib/libbb.so cc/lib/libcc.so
19289 LIB_DIR=$@{foreach dir,$@{CSC_LIST@},$@{dir@}/lib/lib$@{dir@}.so@}
19290
19291 $@{MAIN@}: objects $@{LIB_DIR@}
19292 gnatbind $@{MAIN@} $@{CSC_LIST:%=-aO%/lib@} -shared
19293 gnatlink $@{MAIN@} $@{CSC_LIST:%=-l%@}
19294
19295 objects::
19296 # recompile the sources
19297 gnatmake -c -i $@{MAIN@}.adb $@{NEED_FPIC@} $@{CSC_LIST:%=-I%@}
19298
19299 # Note: In a future version of GNAT, the following commands will be simplified
19300 # by a new tool, gnatmlib
19301 $@{LIB_DIR@}:
19302 mkdir -p $@{dir $@@ @}
19303 cd $@{dir $@@ @} && gcc -shared -o $@{notdir $@@ @} ../*.o -L$@{GLIB@} -lgnat
19304 cd $@{dir $@@ @} && cp -f ../*.ali .
19305
19306 # The dependencies for the modules
19307 # Note that we have to force the expansion of *.o, since in some cases
19308 # make won't be able to do it itself.
19309 aa/lib/libaa.so: $@{wildcard aa/*.o@}
19310 bb/lib/libbb.so: $@{wildcard bb/*.o@}
19311 cc/lib/libcc.so: $@{wildcard cc/*.o@}
19312
19313 # Make sure all of the shared libraries are in the path before starting the
19314 # program
19315 run::
19316 LD_LIBRARY_PATH=`pwd`/aa/lib:`pwd`/bb/lib:`pwd`/cc/lib ./$@{MAIN@}
19317
19318 clean::
19319 $@{RM@} -rf $@{CSC_LIST:%=%/lib@}
19320 $@{RM@} $@{CSC_LIST:%=%/*.ali@}
19321 $@{RM@} $@{CSC_LIST:%=%/*.o@}
19322 $@{RM@} *.o *.ali $@{MAIN@}
19323 @end smallexample
19324
19325 @node Automatically Creating a List of Directories
19326 @section Automatically Creating a List of Directories
19327
19328 @noindent
19329 In most makefiles, you will have to specify a list of directories, and
19330 store it in a variable. For small projects, it is often easier to
19331 specify each of them by hand, since you then have full control over what
19332 is the proper order for these directories, which ones should be
19333 included.
19334
19335 However, in larger projects, which might involve hundreds of
19336 subdirectories, it might be more convenient to generate this list
19337 automatically.
19338
19339 The example below presents two methods. The first one, although less
19340 general, gives you more control over the list. It involves wildcard
19341 characters, that are automatically expanded by @command{make}. Its
19342 shortcoming is that you need to explicitly specify some of the
19343 organization of your project, such as for instance the directory tree
19344 depth, whether some directories are found in a separate tree, @enddots{}
19345
19346 The second method is the most general one. It requires an external
19347 program, called @command{find}, which is standard on all Unix systems. All
19348 the directories found under a given root directory will be added to the
19349 list.
19350
19351 @smallexample
19352 @iftex
19353 @leftskip=0cm
19354 @font@heightrm=cmr8
19355 @heightrm
19356 @end iftex
19357 # The examples below are based on the following directory hierarchy:
19358 # All the directories can contain any number of files
19359 # ROOT_DIRECTORY -> a -> aa -> aaa
19360 # -> ab
19361 # -> ac
19362 # -> b -> ba -> baa
19363 # -> bb
19364 # -> bc
19365 # This Makefile creates a variable called DIRS, that can be reused any time
19366 # you need this list (see the other examples in this section)
19367
19368 # The root of your project's directory hierarchy
19369 ROOT_DIRECTORY=.
19370
19371 ####
19372 # First method: specify explicitly the list of directories
19373 # This allows you to specify any subset of all the directories you need.
19374 ####
19375
19376 DIRS := a/aa/ a/ab/ b/ba/
19377
19378 ####
19379 # Second method: use wildcards
19380 # Note that the argument(s) to wildcard below should end with a '/'.
19381 # Since wildcards also return file names, we have to filter them out
19382 # to avoid duplicate directory names.
19383 # We thus use make's @code{dir} and @code{sort} functions.
19384 # It sets DIRs to the following value (note that the directories aaa and baa
19385 # are not given, unless you change the arguments to wildcard).
19386 # DIRS= ./a/a/ ./b/ ./a/aa/ ./a/ab/ ./a/ac/ ./b/ba/ ./b/bb/ ./b/bc/
19387 ####
19388
19389 DIRS := $@{sort $@{dir $@{wildcard $@{ROOT_DIRECTORY@}/*/
19390 $@{ROOT_DIRECTORY@}/*/*/@}@}@}
19391
19392 ####
19393 # Third method: use an external program
19394 # This command is much faster if run on local disks, avoiding NFS slowdowns.
19395 # This is the most complete command: it sets DIRs to the following value:
19396 # DIRS= ./a ./a/aa ./a/aa/aaa ./a/ab ./a/ac ./b ./b/ba ./b/ba/baa ./b/bb ./b/bc
19397 ####
19398
19399 DIRS := $@{shell find $@{ROOT_DIRECTORY@} -type d -print@}
19400
19401 @end smallexample
19402
19403 @node Generating the Command Line Switches
19404 @section Generating the Command Line Switches
19405
19406 @noindent
19407 Once you have created the list of directories as explained in the
19408 previous section (@pxref{Automatically Creating a List of Directories}),
19409 you can easily generate the command line arguments to pass to gnatmake.
19410
19411 For the sake of completeness, this example assumes that the source path
19412 is not the same as the object path, and that you have two separate lists
19413 of directories.
19414
19415 @smallexample
19416 # see "Automatically creating a list of directories" to create
19417 # these variables
19418 SOURCE_DIRS=
19419 OBJECT_DIRS=
19420
19421 GNATMAKE_SWITCHES := $@{patsubst %,-aI%,$@{SOURCE_DIRS@}@}
19422 GNATMAKE_SWITCHES += $@{patsubst %,-aO%,$@{OBJECT_DIRS@}@}
19423
19424 all:
19425 gnatmake $@{GNATMAKE_SWITCHES@} main_unit
19426 @end smallexample
19427
19428 @node Overcoming Command Line Length Limits
19429 @section Overcoming Command Line Length Limits
19430
19431 @noindent
19432 One problem that might be encountered on big projects is that many
19433 operating systems limit the length of the command line. It is thus hard to give
19434 gnatmake the list of source and object directories.
19435
19436 This example shows how you can set up environment variables, which will
19437 make @command{gnatmake} behave exactly as if the directories had been
19438 specified on the command line, but have a much higher length limit (or
19439 even none on most systems).
19440
19441 It assumes that you have created a list of directories in your Makefile,
19442 using one of the methods presented in
19443 @ref{Automatically Creating a List of Directories}.
19444 For the sake of completeness, we assume that the object
19445 path (where the ALI files are found) is different from the sources patch.
19446
19447 Note a small trick in the Makefile below: for efficiency reasons, we
19448 create two temporary variables (SOURCE_LIST and OBJECT_LIST), that are
19449 expanded immediately by @code{make}. This way we overcome the standard
19450 make behavior which is to expand the variables only when they are
19451 actually used.
19452
19453 On Windows, if you are using the standard Windows command shell, you must
19454 replace colons with semicolons in the assignments to these variables.
19455
19456 @smallexample
19457 @iftex
19458 @leftskip=0cm
19459 @font@heightrm=cmr8
19460 @heightrm
19461 @end iftex
19462 # In this example, we create both ADA_INCLUDE_PATH and ADA_OBJECT_PATH.
19463 # This is the same thing as putting the -I arguments on the command line.
19464 # (the equivalent of using -aI on the command line would be to define
19465 # only ADA_INCLUDE_PATH, the equivalent of -aO is ADA_OBJECT_PATH).
19466 # You can of course have different values for these variables.
19467 #
19468 # Note also that we need to keep the previous values of these variables, since
19469 # they might have been set before running 'make' to specify where the GNAT
19470 # library is installed.
19471
19472 # see "Automatically creating a list of directories" to create these
19473 # variables
19474 SOURCE_DIRS=
19475 OBJECT_DIRS=
19476
19477 empty:=
19478 space:=$@{empty@} $@{empty@}
19479 SOURCE_LIST := $@{subst $@{space@},:,$@{SOURCE_DIRS@}@}
19480 OBJECT_LIST := $@{subst $@{space@},:,$@{OBJECT_DIRS@}@}
19481 ADA_INCLUDE_PATH += $@{SOURCE_LIST@}
19482 ADA_OBJECT_PATH += $@{OBJECT_LIST@}
19483 export ADA_INCLUDE_PATH
19484 export ADA_OBJECT_PATH
19485
19486 all:
19487 gnatmake main_unit
19488 @end smallexample
19489 @end ifclear
19490
19491 @node Memory Management Issues
19492 @chapter Memory Management Issues
19493
19494 @noindent
19495 This chapter describes some useful memory pools provided in the GNAT library
19496 and in particular the GNAT Debug Pool facility, which can be used to detect
19497 incorrect uses of access values (including ``dangling references'').
19498 @ifclear vms
19499 It also describes the @command{gnatmem} tool, which can be used to track down
19500 ``memory leaks''.
19501 @end ifclear
19502
19503 @menu
19504 * Some Useful Memory Pools::
19505 * The GNAT Debug Pool Facility::
19506 @ifclear vms
19507 * The gnatmem Tool::
19508 @end ifclear
19509 @end menu
19510
19511 @node Some Useful Memory Pools
19512 @section Some Useful Memory Pools
19513 @findex Memory Pool
19514 @cindex storage, pool
19515
19516 @noindent
19517 The @code{System.Pool_Global} package offers the Unbounded_No_Reclaim_Pool
19518 storage pool. Allocations use the standard system call @code{malloc} while
19519 deallocations use the standard system call @code{free}. No reclamation is
19520 performed when the pool goes out of scope. For performance reasons, the
19521 standard default Ada allocators/deallocators do not use any explicit storage
19522 pools but if they did, they could use this storage pool without any change in
19523 behavior. That is why this storage pool is used when the user
19524 manages to make the default implicit allocator explicit as in this example:
19525 @smallexample @c ada
19526 type T1 is access Something;
19527 -- no Storage pool is defined for T2
19528 type T2 is access Something_Else;
19529 for T2'Storage_Pool use T1'Storage_Pool;
19530 -- the above is equivalent to
19531 for T2'Storage_Pool use System.Pool_Global.Global_Pool_Object;
19532 @end smallexample
19533
19534 @noindent
19535 The @code{System.Pool_Local} package offers the Unbounded_Reclaim_Pool storage
19536 pool. The allocation strategy is similar to @code{Pool_Local}'s
19537 except that the all
19538 storage allocated with this pool is reclaimed when the pool object goes out of
19539 scope. This pool provides a explicit mechanism similar to the implicit one
19540 provided by several Ada 83 compilers for allocations performed through a local
19541 access type and whose purpose was to reclaim memory when exiting the
19542 scope of a given local access. As an example, the following program does not
19543 leak memory even though it does not perform explicit deallocation:
19544
19545 @smallexample @c ada
19546 with System.Pool_Local;
19547 procedure Pooloc1 is
19548 procedure Internal is
19549 type A is access Integer;
19550 X : System.Pool_Local.Unbounded_Reclaim_Pool;
19551 for A'Storage_Pool use X;
19552 v : A;
19553 begin
19554 for I in 1 .. 50 loop
19555 v := new Integer;
19556 end loop;
19557 end Internal;
19558 begin
19559 for I in 1 .. 100 loop
19560 Internal;
19561 end loop;
19562 end Pooloc1;
19563 @end smallexample
19564
19565 @noindent
19566 The @code{System.Pool_Size} package implements the Stack_Bounded_Pool used when
19567 @code{Storage_Size} is specified for an access type.
19568 The whole storage for the pool is
19569 allocated at once, usually on the stack at the point where the access type is
19570 elaborated. It is automatically reclaimed when exiting the scope where the
19571 access type is defined. This package is not intended to be used directly by the
19572 user and it is implicitly used for each such declaration:
19573
19574 @smallexample @c ada
19575 type T1 is access Something;
19576 for T1'Storage_Size use 10_000;
19577 @end smallexample
19578
19579 @node The GNAT Debug Pool Facility
19580 @section The GNAT Debug Pool Facility
19581 @findex Debug Pool
19582 @cindex storage, pool, memory corruption
19583
19584 @noindent
19585 The use of unchecked deallocation and unchecked conversion can easily
19586 lead to incorrect memory references. The problems generated by such
19587 references are usually difficult to tackle because the symptoms can be
19588 very remote from the origin of the problem. In such cases, it is
19589 very helpful to detect the problem as early as possible. This is the
19590 purpose of the Storage Pool provided by @code{GNAT.Debug_Pools}.
19591
19592 In order to use the GNAT specific debugging pool, the user must
19593 associate a debug pool object with each of the access types that may be
19594 related to suspected memory problems. See Ada Reference Manual 13.11.
19595 @smallexample @c ada
19596 type Ptr is access Some_Type;
19597 Pool : GNAT.Debug_Pools.Debug_Pool;
19598 for Ptr'Storage_Pool use Pool;
19599 @end smallexample
19600
19601 @noindent
19602 @code{GNAT.Debug_Pools} is derived from a GNAT-specific kind of
19603 pool: the @code{Checked_Pool}. Such pools, like standard Ada storage pools,
19604 allow the user to redefine allocation and deallocation strategies. They
19605 also provide a checkpoint for each dereference, through the use of
19606 the primitive operation @code{Dereference} which is implicitly called at
19607 each dereference of an access value.
19608
19609 Once an access type has been associated with a debug pool, operations on
19610 values of the type may raise four distinct exceptions,
19611 which correspond to four potential kinds of memory corruption:
19612 @itemize @bullet
19613 @item
19614 @code{GNAT.Debug_Pools.Accessing_Not_Allocated_Storage}
19615 @item
19616 @code{GNAT.Debug_Pools.Accessing_Deallocated_Storage}
19617 @item
19618 @code{GNAT.Debug_Pools.Freeing_Not_Allocated_Storage}
19619 @item
19620 @code{GNAT.Debug_Pools.Freeing_Deallocated_Storage }
19621 @end itemize
19622
19623 @noindent
19624 For types associated with a Debug_Pool, dynamic allocation is performed using
19625 the standard GNAT allocation routine. References to all allocated chunks of
19626 memory are kept in an internal dictionary. Several deallocation strategies are
19627 provided, whereupon the user can choose to release the memory to the system,
19628 keep it allocated for further invalid access checks, or fill it with an easily
19629 recognizable pattern for debug sessions. The memory pattern is the old IBM
19630 hexadecimal convention: @code{16#DEADBEEF#}.
19631
19632 See the documentation in the file g-debpoo.ads for more information on the
19633 various strategies.
19634
19635 Upon each dereference, a check is made that the access value denotes a
19636 properly allocated memory location. Here is a complete example of use of
19637 @code{Debug_Pools}, that includes typical instances of memory corruption:
19638 @smallexample @c ada
19639 @iftex
19640 @leftskip=0cm
19641 @end iftex
19642 with Gnat.Io; use Gnat.Io;
19643 with Unchecked_Deallocation;
19644 with Unchecked_Conversion;
19645 with GNAT.Debug_Pools;
19646 with System.Storage_Elements;
19647 with Ada.Exceptions; use Ada.Exceptions;
19648 procedure Debug_Pool_Test is
19649
19650 type T is access Integer;
19651 type U is access all T;
19652
19653 P : GNAT.Debug_Pools.Debug_Pool;
19654 for T'Storage_Pool use P;
19655
19656 procedure Free is new Unchecked_Deallocation (Integer, T);
19657 function UC is new Unchecked_Conversion (U, T);
19658 A, B : aliased T;
19659
19660 procedure Info is new GNAT.Debug_Pools.Print_Info(Put_Line);
19661
19662 begin
19663 Info (P);
19664 A := new Integer;
19665 B := new Integer;
19666 B := A;
19667 Info (P);
19668 Free (A);
19669 begin
19670 Put_Line (Integer'Image(B.all));
19671 exception
19672 when E : others => Put_Line ("raised: " & Exception_Name (E));
19673 end;
19674 begin
19675 Free (B);
19676 exception
19677 when E : others => Put_Line ("raised: " & Exception_Name (E));
19678 end;
19679 B := UC(A'Access);
19680 begin
19681 Put_Line (Integer'Image(B.all));
19682 exception
19683 when E : others => Put_Line ("raised: " & Exception_Name (E));
19684 end;
19685 begin
19686 Free (B);
19687 exception
19688 when E : others => Put_Line ("raised: " & Exception_Name (E));
19689 end;
19690 Info (P);
19691 end Debug_Pool_Test;
19692 @end smallexample
19693
19694 @noindent
19695 The debug pool mechanism provides the following precise diagnostics on the
19696 execution of this erroneous program:
19697 @smallexample
19698 Debug Pool info:
19699 Total allocated bytes : 0
19700 Total deallocated bytes : 0
19701 Current Water Mark: 0
19702 High Water Mark: 0
19703
19704 Debug Pool info:
19705 Total allocated bytes : 8
19706 Total deallocated bytes : 0
19707 Current Water Mark: 8
19708 High Water Mark: 8
19709
19710 raised: GNAT.DEBUG_POOLS.ACCESSING_DEALLOCATED_STORAGE
19711 raised: GNAT.DEBUG_POOLS.FREEING_DEALLOCATED_STORAGE
19712 raised: GNAT.DEBUG_POOLS.ACCESSING_NOT_ALLOCATED_STORAGE
19713 raised: GNAT.DEBUG_POOLS.FREEING_NOT_ALLOCATED_STORAGE
19714 Debug Pool info:
19715 Total allocated bytes : 8
19716 Total deallocated bytes : 4
19717 Current Water Mark: 4
19718 High Water Mark: 8
19719 @end smallexample
19720
19721 @ifclear vms
19722 @node The gnatmem Tool
19723 @section The @command{gnatmem} Tool
19724 @findex gnatmem
19725
19726 @noindent
19727 The @code{gnatmem} utility monitors dynamic allocation and
19728 deallocation activity in a program, and displays information about
19729 incorrect deallocations and possible sources of memory leaks.
19730 It is designed to work in association with a static runtime library
19731 only and in this context provides three types of information:
19732 @itemize @bullet
19733 @item
19734 General information concerning memory management, such as the total
19735 number of allocations and deallocations, the amount of allocated
19736 memory and the high water mark, i.e.@: the largest amount of allocated
19737 memory in the course of program execution.
19738
19739 @item
19740 Backtraces for all incorrect deallocations, that is to say deallocations
19741 which do not correspond to a valid allocation.
19742
19743 @item
19744 Information on each allocation that is potentially the origin of a memory
19745 leak.
19746 @end itemize
19747
19748 @menu
19749 * Running gnatmem::
19750 * Switches for gnatmem::
19751 * Example of gnatmem Usage::
19752 @end menu
19753
19754 @node Running gnatmem
19755 @subsection Running @code{gnatmem}
19756
19757 @noindent
19758 @code{gnatmem} makes use of the output created by the special version of
19759 allocation and deallocation routines that record call information. This
19760 allows to obtain accurate dynamic memory usage history at a minimal cost to
19761 the execution speed. Note however, that @code{gnatmem} is not supported on
19762 all platforms (currently, it is supported on AIX, HP-UX, GNU/Linux,
19763 Solaris and Windows NT/2000/XP (x86).
19764
19765 @noindent
19766 The @code{gnatmem} command has the form
19767
19768 @smallexample
19769 $ gnatmem @ovar{switches} user_program
19770 @end smallexample
19771
19772 @noindent
19773 The program must have been linked with the instrumented version of the
19774 allocation and deallocation routines. This is done by linking with the
19775 @file{libgmem.a} library. For correct symbolic backtrace information,
19776 the user program should be compiled with debugging options
19777 (see @ref{Switches for gcc}). For example to build @file{my_program}:
19778
19779 @smallexample
19780 $ gnatmake -g my_program -largs -lgmem
19781 @end smallexample
19782
19783 @noindent
19784 As library @file{libgmem.a} contains an alternate body for package
19785 @code{System.Memory}, @file{s-memory.adb} should not be compiled and linked
19786 when an executable is linked with library @file{libgmem.a}. It is then not
19787 recommended to use @command{gnatmake} with switch @option{^-a^/ALL_FILES^}.
19788
19789 @noindent
19790 When @file{my_program} is executed, the file @file{gmem.out} is produced.
19791 This file contains information about all allocations and deallocations
19792 performed by the program. It is produced by the instrumented allocations and
19793 deallocations routines and will be used by @code{gnatmem}.
19794
19795 In order to produce symbolic backtrace information for allocations and
19796 deallocations performed by the GNAT run-time library, you need to use a
19797 version of that library that has been compiled with the @option{-g} switch
19798 (see @ref{Rebuilding the GNAT Run-Time Library}).
19799
19800 Gnatmem must be supplied with the @file{gmem.out} file and the executable to
19801 examine. If the location of @file{gmem.out} file was not explicitly supplied by
19802 @option{-i} switch, gnatmem will assume that this file can be found in the
19803 current directory. For example, after you have executed @file{my_program},
19804 @file{gmem.out} can be analyzed by @code{gnatmem} using the command:
19805
19806 @smallexample
19807 $ gnatmem my_program
19808 @end smallexample
19809
19810 @noindent
19811 This will produce the output with the following format:
19812
19813 *************** debut cc
19814 @smallexample
19815 $ gnatmem my_program
19816
19817 Global information
19818 ------------------
19819 Total number of allocations : 45
19820 Total number of deallocations : 6
19821 Final Water Mark (non freed mem) : 11.29 Kilobytes
19822 High Water Mark : 11.40 Kilobytes
19823
19824 .
19825 .
19826 .
19827 Allocation Root # 2
19828 -------------------
19829 Number of non freed allocations : 11
19830 Final Water Mark (non freed mem) : 1.16 Kilobytes
19831 High Water Mark : 1.27 Kilobytes
19832 Backtrace :
19833 my_program.adb:23 my_program.alloc
19834 .
19835 .
19836 .
19837 @end smallexample
19838
19839 The first block of output gives general information. In this case, the
19840 Ada construct ``@code{@b{new}}'' was executed 45 times, and only 6 calls to an
19841 Unchecked_Deallocation routine occurred.
19842
19843 @noindent
19844 Subsequent paragraphs display information on all allocation roots.
19845 An allocation root is a specific point in the execution of the program
19846 that generates some dynamic allocation, such as a ``@code{@b{new}}''
19847 construct. This root is represented by an execution backtrace (or subprogram
19848 call stack). By default the backtrace depth for allocations roots is 1, so
19849 that a root corresponds exactly to a source location. The backtrace can
19850 be made deeper, to make the root more specific.
19851
19852 @node Switches for gnatmem
19853 @subsection Switches for @code{gnatmem}
19854
19855 @noindent
19856 @code{gnatmem} recognizes the following switches:
19857
19858 @table @option
19859
19860 @item -q
19861 @cindex @option{-q} (@code{gnatmem})
19862 Quiet. Gives the minimum output needed to identify the origin of the
19863 memory leaks. Omits statistical information.
19864
19865 @item @var{N}
19866 @cindex @var{N} (@code{gnatmem})
19867 N is an integer literal (usually between 1 and 10) which controls the
19868 depth of the backtraces defining allocation root. The default value for
19869 N is 1. The deeper the backtrace, the more precise the localization of
19870 the root. Note that the total number of roots can depend on this
19871 parameter. This parameter must be specified @emph{before} the name of the
19872 executable to be analyzed, to avoid ambiguity.
19873
19874 @item -b n
19875 @cindex @option{-b} (@code{gnatmem})
19876 This switch has the same effect as just depth parameter.
19877
19878 @item -i @var{file}
19879 @cindex @option{-i} (@code{gnatmem})
19880 Do the @code{gnatmem} processing starting from @file{file}, rather than
19881 @file{gmem.out} in the current directory.
19882
19883 @item -m n
19884 @cindex @option{-m} (@code{gnatmem})
19885 This switch causes @code{gnatmem} to mask the allocation roots that have less
19886 than n leaks. The default value is 1. Specifying the value of 0 will allow to
19887 examine even the roots that didn't result in leaks.
19888
19889 @item -s order
19890 @cindex @option{-s} (@code{gnatmem})
19891 This switch causes @code{gnatmem} to sort the allocation roots according to the
19892 specified order of sort criteria, each identified by a single letter. The
19893 currently supported criteria are @code{n, h, w} standing respectively for
19894 number of unfreed allocations, high watermark, and final watermark
19895 corresponding to a specific root. The default order is @code{nwh}.
19896
19897 @end table
19898
19899 @node Example of gnatmem Usage
19900 @subsection Example of @code{gnatmem} Usage
19901
19902 @noindent
19903 The following example shows the use of @code{gnatmem}
19904 on a simple memory-leaking program.
19905 Suppose that we have the following Ada program:
19906
19907 @smallexample @c ada
19908 @group
19909 @cartouche
19910 with Unchecked_Deallocation;
19911 procedure Test_Gm is
19912
19913 type T is array (1..1000) of Integer;
19914 type Ptr is access T;
19915 procedure Free is new Unchecked_Deallocation (T, Ptr);
19916 A : Ptr;
19917
19918 procedure My_Alloc is
19919 begin
19920 A := new T;
19921 end My_Alloc;
19922
19923 procedure My_DeAlloc is
19924 B : Ptr := A;
19925 begin
19926 Free (B);
19927 end My_DeAlloc;
19928
19929 begin
19930 My_Alloc;
19931 for I in 1 .. 5 loop
19932 for J in I .. 5 loop
19933 My_Alloc;
19934 end loop;
19935 My_Dealloc;
19936 end loop;
19937 end;
19938 @end cartouche
19939 @end group
19940 @end smallexample
19941
19942 @noindent
19943 The program needs to be compiled with debugging option and linked with
19944 @code{gmem} library:
19945
19946 @smallexample
19947 $ gnatmake -g test_gm -largs -lgmem
19948 @end smallexample
19949
19950 @noindent
19951 Then we execute the program as usual:
19952
19953 @smallexample
19954 $ test_gm
19955 @end smallexample
19956
19957 @noindent
19958 Then @code{gnatmem} is invoked simply with
19959 @smallexample
19960 $ gnatmem test_gm
19961 @end smallexample
19962
19963 @noindent
19964 which produces the following output (result may vary on different platforms):
19965
19966 @smallexample
19967 Global information
19968 ------------------
19969 Total number of allocations : 18
19970 Total number of deallocations : 5
19971 Final Water Mark (non freed mem) : 53.00 Kilobytes
19972 High Water Mark : 56.90 Kilobytes
19973
19974 Allocation Root # 1
19975 -------------------
19976 Number of non freed allocations : 11
19977 Final Water Mark (non freed mem) : 42.97 Kilobytes
19978 High Water Mark : 46.88 Kilobytes
19979 Backtrace :
19980 test_gm.adb:11 test_gm.my_alloc
19981
19982 Allocation Root # 2
19983 -------------------
19984 Number of non freed allocations : 1
19985 Final Water Mark (non freed mem) : 10.02 Kilobytes
19986 High Water Mark : 10.02 Kilobytes
19987 Backtrace :
19988 s-secsta.adb:81 system.secondary_stack.ss_init
19989
19990 Allocation Root # 3
19991 -------------------
19992 Number of non freed allocations : 1
19993 Final Water Mark (non freed mem) : 12 Bytes
19994 High Water Mark : 12 Bytes
19995 Backtrace :
19996 s-secsta.adb:181 system.secondary_stack.ss_init
19997 @end smallexample
19998
19999 @noindent
20000 Note that the GNAT run time contains itself a certain number of
20001 allocations that have no corresponding deallocation,
20002 as shown here for root #2 and root
20003 #3. This is a normal behavior when the number of non-freed allocations
20004 is one, it allocates dynamic data structures that the run time needs for
20005 the complete lifetime of the program. Note also that there is only one
20006 allocation root in the user program with a single line back trace:
20007 test_gm.adb:11 test_gm.my_alloc, whereas a careful analysis of the
20008 program shows that 'My_Alloc' is called at 2 different points in the
20009 source (line 21 and line 24). If those two allocation roots need to be
20010 distinguished, the backtrace depth parameter can be used:
20011
20012 @smallexample
20013 $ gnatmem 3 test_gm
20014 @end smallexample
20015
20016 @noindent
20017 which will give the following output:
20018
20019 @smallexample
20020 Global information
20021 ------------------
20022 Total number of allocations : 18
20023 Total number of deallocations : 5
20024 Final Water Mark (non freed mem) : 53.00 Kilobytes
20025 High Water Mark : 56.90 Kilobytes
20026
20027 Allocation Root # 1
20028 -------------------
20029 Number of non freed allocations : 10
20030 Final Water Mark (non freed mem) : 39.06 Kilobytes
20031 High Water Mark : 42.97 Kilobytes
20032 Backtrace :
20033 test_gm.adb:11 test_gm.my_alloc
20034 test_gm.adb:24 test_gm
20035 b_test_gm.c:52 main
20036
20037 Allocation Root # 2
20038 -------------------
20039 Number of non freed allocations : 1
20040 Final Water Mark (non freed mem) : 10.02 Kilobytes
20041 High Water Mark : 10.02 Kilobytes
20042 Backtrace :
20043 s-secsta.adb:81 system.secondary_stack.ss_init
20044 s-secsta.adb:283 <system__secondary_stack___elabb>
20045 b_test_gm.c:33 adainit
20046
20047 Allocation Root # 3
20048 -------------------
20049 Number of non freed allocations : 1
20050 Final Water Mark (non freed mem) : 3.91 Kilobytes
20051 High Water Mark : 3.91 Kilobytes
20052 Backtrace :
20053 test_gm.adb:11 test_gm.my_alloc
20054 test_gm.adb:21 test_gm
20055 b_test_gm.c:52 main
20056
20057 Allocation Root # 4
20058 -------------------
20059 Number of non freed allocations : 1
20060 Final Water Mark (non freed mem) : 12 Bytes
20061 High Water Mark : 12 Bytes
20062 Backtrace :
20063 s-secsta.adb:181 system.secondary_stack.ss_init
20064 s-secsta.adb:283 <system__secondary_stack___elabb>
20065 b_test_gm.c:33 adainit
20066 @end smallexample
20067
20068 @noindent
20069 The allocation root #1 of the first example has been split in 2 roots #1
20070 and #3 thanks to the more precise associated backtrace.
20071
20072 @end ifclear
20073
20074 @node Stack Related Facilities
20075 @chapter Stack Related Facilities
20076
20077 @noindent
20078 This chapter describes some useful tools associated with stack
20079 checking and analysis. In
20080 particular, it deals with dynamic and static stack usage measurements.
20081
20082 @menu
20083 * Stack Overflow Checking::
20084 * Static Stack Usage Analysis::
20085 * Dynamic Stack Usage Analysis::
20086 @end menu
20087
20088 @node Stack Overflow Checking
20089 @section Stack Overflow Checking
20090 @cindex Stack Overflow Checking
20091 @cindex -fstack-check
20092
20093 @noindent
20094 For most operating systems, @command{gcc} does not perform stack overflow
20095 checking by default. This means that if the main environment task or
20096 some other task exceeds the available stack space, then unpredictable
20097 behavior will occur. Most native systems offer some level of protection by
20098 adding a guard page at the end of each task stack. This mechanism is usually
20099 not enough for dealing properly with stack overflow situations because
20100 a large local variable could ``jump'' above the guard page.
20101 Furthermore, when the
20102 guard page is hit, there may not be any space left on the stack for executing
20103 the exception propagation code. Enabling stack checking avoids
20104 such situations.
20105
20106 To activate stack checking, compile all units with the gcc option
20107 @option{-fstack-check}. For example:
20108
20109 @smallexample
20110 gcc -c -fstack-check package1.adb
20111 @end smallexample
20112
20113 @noindent
20114 Units compiled with this option will generate extra instructions to check
20115 that any use of the stack (for procedure calls or for declaring local
20116 variables in declare blocks) does not exceed the available stack space.
20117 If the space is exceeded, then a @code{Storage_Error} exception is raised.
20118
20119 For declared tasks, the stack size is controlled by the size
20120 given in an applicable @code{Storage_Size} pragma or by the value specified
20121 at bind time with @option{-d} (@pxref{Switches for gnatbind}) or is set to
20122 the default size as defined in the GNAT runtime otherwise.
20123
20124 For the environment task, the stack size depends on
20125 system defaults and is unknown to the compiler. Stack checking
20126 may still work correctly if a fixed
20127 size stack is allocated, but this cannot be guaranteed.
20128 @ifclear vms
20129 To ensure that a clean exception is signalled for stack
20130 overflow, set the environment variable
20131 @env{GNAT_STACK_LIMIT} to indicate the maximum
20132 stack area that can be used, as in:
20133 @cindex GNAT_STACK_LIMIT
20134
20135 @smallexample
20136 SET GNAT_STACK_LIMIT 1600
20137 @end smallexample
20138
20139 @noindent
20140 The limit is given in kilobytes, so the above declaration would
20141 set the stack limit of the environment task to 1.6 megabytes.
20142 Note that the only purpose of this usage is to limit the amount
20143 of stack used by the environment task. If it is necessary to
20144 increase the amount of stack for the environment task, then this
20145 is an operating systems issue, and must be addressed with the
20146 appropriate operating systems commands.
20147 @end ifclear
20148 @ifset vms
20149 To have a fixed size stack in the environment task, the stack must be put
20150 in the P0 address space and its size specified. Use these switches to
20151 create a p0 image:
20152
20153 @smallexample
20154 gnatmake my_progs -largs "-Wl,--opt=STACK=4000,/p0image"
20155 @end smallexample
20156
20157 @noindent
20158 The quotes are required to keep case. The number after @samp{STACK=} is the
20159 size of the environmental task stack in pagelets (512 bytes). In this example
20160 the stack size is about 2 megabytes.
20161
20162 @noindent
20163 A consequence of the @option{/p0image} qualifier is also to makes RMS buffers
20164 be placed in P0 space. Refer to @cite{HP OpenVMS Linker Utility Manual} for
20165 more details about the @option{/p0image} qualifier and the @option{stack}
20166 option.
20167 @end ifset
20168
20169 @node Static Stack Usage Analysis
20170 @section Static Stack Usage Analysis
20171 @cindex Static Stack Usage Analysis
20172 @cindex -fstack-usage
20173
20174 @noindent
20175 A unit compiled with @option{-fstack-usage} will generate an extra file
20176 that specifies
20177 the maximum amount of stack used, on a per-function basis.
20178 The file has the same
20179 basename as the target object file with a @file{.su} extension.
20180 Each line of this file is made up of three fields:
20181
20182 @itemize
20183 @item
20184 The name of the function.
20185 @item
20186 A number of bytes.
20187 @item
20188 One or more qualifiers: @code{static}, @code{dynamic}, @code{bounded}.
20189 @end itemize
20190
20191 The second field corresponds to the size of the known part of the function
20192 frame.
20193
20194 The qualifier @code{static} means that the function frame size
20195 is purely static.
20196 It usually means that all local variables have a static size.
20197 In this case, the second field is a reliable measure of the function stack
20198 utilization.
20199
20200 The qualifier @code{dynamic} means that the function frame size is not static.
20201 It happens mainly when some local variables have a dynamic size. When this
20202 qualifier appears alone, the second field is not a reliable measure
20203 of the function stack analysis. When it is qualified with @code{bounded}, it
20204 means that the second field is a reliable maximum of the function stack
20205 utilization.
20206
20207 @node Dynamic Stack Usage Analysis
20208 @section Dynamic Stack Usage Analysis
20209
20210 @noindent
20211 It is possible to measure the maximum amount of stack used by a task, by
20212 adding a switch to @command{gnatbind}, as:
20213
20214 @smallexample
20215 $ gnatbind -u0 file
20216 @end smallexample
20217
20218 @noindent
20219 With this option, at each task termination, its stack usage is output on
20220 @file{stderr}.
20221 It is not always convenient to output the stack usage when the program
20222 is still running. Hence, it is possible to delay this output until program
20223 termination. for a given number of tasks specified as the argument of the
20224 @option{-u} option. For instance:
20225
20226 @smallexample
20227 $ gnatbind -u100 file
20228 @end smallexample
20229
20230 @noindent
20231 will buffer the stack usage information of the first 100 tasks to terminate and
20232 output this info at program termination. Results are displayed in four
20233 columns:
20234
20235 @noindent
20236 Index | Task Name | Stack Size | Actual Use [min - max]
20237
20238 @noindent
20239 where:
20240
20241 @table @emph
20242 @item Index
20243 is a number associated with each task.
20244
20245 @item Task Name
20246 is the name of the task analyzed.
20247
20248 @item Stack Size
20249 is the maximum size for the stack.
20250
20251 @item Actual Use
20252 is the measure done by the stack analyzer. In order to prevent overflow,
20253 the stack is not entirely analyzed, and it's not possible to know exactly how
20254 much has actually been used. The real amount of stack used is between the min
20255 and max values.
20256
20257 @end table
20258
20259 @noindent
20260 The environment task stack, e.g., the stack that contains the main unit, is
20261 only processed when the environment variable GNAT_STACK_LIMIT is set.
20262
20263
20264 @c *********************************
20265 @c * GNATCHECK *
20266 @c *********************************
20267 @node Verifying Properties Using gnatcheck
20268 @chapter Verifying Properties Using @command{gnatcheck}
20269 @findex gnatcheck
20270 @cindex @command{gnatcheck}
20271
20272 @noindent
20273 The @command{gnatcheck} tool is an ASIS-based utility that checks properties
20274 of Ada source files according to a given set of semantic rules.
20275 @cindex ASIS
20276
20277 In order to check compliance with a given rule, @command{gnatcheck} has to
20278 semantically analyze the Ada sources.
20279 Therefore, checks can only be performed on
20280 legal Ada units. Moreover, when a unit depends semantically upon units located
20281 outside the current directory, the source search path has to be provided when
20282 calling @command{gnatcheck}, either through a specified project file or
20283 through @command{gnatcheck} switches as described below.
20284
20285 A number of rules are predefined in @command{gnatcheck} and are described
20286 later in this chapter.
20287 You can also add new rules, by modifying the @command{gnatcheck} code and
20288 rebuilding the tool. In order to add a simple rule making some local checks,
20289 a small amount of straightforward ASIS-based programming is usually needed.
20290
20291 Project support for @command{gnatcheck} is provided by the GNAT
20292 driver (see @ref{The GNAT Driver and Project Files}).
20293
20294 Invoking @command{gnatcheck} on the command line has the form:
20295
20296 @smallexample
20297 $ gnatcheck @ovar{switches} @{@var{filename}@}
20298 @r{[}^-files^/FILES^=@{@var{arg_list_filename}@}@r{]}
20299 @r{[}-cargs @var{gcc_switches}@r{]} @r{[}-rules @var{rule_options}@r{]}
20300 @end smallexample
20301
20302 @noindent
20303 where
20304 @itemize @bullet
20305 @item
20306 @var{switches} specify the general tool options
20307
20308 @item
20309 Each @var{filename} is the name (including the extension) of a source
20310 file to process. ``Wildcards'' are allowed, and
20311 the file name may contain path information.
20312
20313 @item
20314 Each @var{arg_list_filename} is the name (including the extension) of a text
20315 file containing the names of the source files to process, separated by spaces
20316 or line breaks.
20317
20318 @item
20319 @var{gcc_switches} is a list of switches for
20320 @command{gcc}. They will be passed on to all compiler invocations made by
20321 @command{gnatcheck} to generate the ASIS trees. Here you can provide
20322 @option{^-I^/INCLUDE_DIRS=^} switches to form the source search path,
20323 and use the @option{-gnatec} switch to set the configuration file.
20324
20325 @item
20326 @var{rule_options} is a list of options for controlling a set of
20327 rules to be checked by @command{gnatcheck} (@pxref{gnatcheck Rule Options}).
20328 @end itemize
20329
20330 @noindent
20331 Either a @file{@var{filename}} or an @file{@var{arg_list_filename}} must be supplied.
20332
20333 @menu
20334 * Format of the Report File::
20335 * General gnatcheck Switches::
20336 * gnatcheck Rule Options::
20337 * Adding the Results of Compiler Checks to gnatcheck Output::
20338 * Project-Wide Checks::
20339 * Predefined Rules::
20340 @end menu
20341
20342 @node Format of the Report File
20343 @section Format of the Report File
20344 @cindex Report file (for @code{gnatcheck})
20345
20346 @noindent
20347 The @command{gnatcheck} tool outputs on @file{stdout} all messages concerning
20348 rule violations.
20349 It also creates, in the current
20350 directory, a text file named @file{^gnatcheck.out^GNATCHECK.OUT^} that
20351 contains the complete report of the last gnatcheck run. This report contains:
20352 @itemize @bullet
20353 @item a list of the Ada source files being checked,
20354 @item a list of enabled and disabled rules,
20355 @item a list of the diagnostic messages, ordered in three different ways
20356 and collected in three separate
20357 sections. Section 1 contains the raw list of diagnostic messages. It
20358 corresponds to the output going to @file{stdout}. Section 2 contains
20359 messages ordered by rules.
20360 Section 3 contains messages ordered by source files.
20361 @end itemize
20362
20363 @node General gnatcheck Switches
20364 @section General @command{gnatcheck} Switches
20365
20366 @noindent
20367 The following switches control the general @command{gnatcheck} behavior
20368
20369 @table @option
20370 @c !sort!
20371 @cindex @option{^-a^/ALL^} (@command{gnatcheck})
20372 @item ^-a^/ALL^
20373 Process all units including those with read-only ALI files such as
20374 those from GNAT Run-Time library.
20375
20376 @ifclear vms
20377 @ignore
20378 @cindex @option{-d} (@command{gnatcheck})
20379 @item -d
20380 Debug mode
20381 @end ignore
20382
20383 @cindex @option{-dd} (@command{gnatcheck})
20384 @item -dd
20385 Progress indicator mode (for use in GPS)
20386 @end ifclear
20387
20388 @cindex @option{^-h^/HELP^} (@command{gnatcheck})
20389 @item ^-h^/HELP^
20390 List the predefined and user-defined rules. For more details see
20391 @ref{Predefined Rules}.
20392
20393 @cindex @option{^-l^/LOCS^} (@command{gnatcheck})
20394 @item ^-l^/LOCS^
20395 Use full source locations references in the report file. For a construct from
20396 a generic instantiation a full source location is a chain from the location
20397 of this construct in the generic unit to the place where this unit is
20398 instantiated.
20399
20400 @cindex @option{^-m^/DIAGNOSTIC_LIMIT^} (@command{gnatcheck})
20401 @item ^-m@i{nnn}^/DIAGNOSTIC_LIMIT=@i{nnn}^
20402 Maximum number of diagnoses to be sent to Stdout, @i{nnn} from o@dots{}1000,
20403 the default value is 500. Zero means that there is no limitation on
20404 the number of diagnostic messages to be printed into Stdout.
20405
20406 @cindex @option{^-q^/QUIET^} (@command{gnatcheck})
20407 @item ^-q^/QUIET^
20408 Quiet mode. All the diagnoses about rule violations are placed in the
20409 @command{gnatcheck} report file only, without duplicating in @file{stdout}.
20410
20411 @cindex @option{^-s^/SHORT^} (@command{gnatcheck})
20412 @item ^-s^/SHORT^
20413 Short format of the report file (no version information, no list of applied
20414 rules, no list of checked sources is included)
20415
20416 @cindex @option{^-s1^/COMPILER_STYLE^} (@command{gnatcheck})
20417 @item ^-s1^/COMPILER_STYLE^
20418 Include the compiler-style section in the report file
20419
20420 @cindex @option{^-s2^/BY_RULES^} (@command{gnatcheck})
20421 @item ^-s2^/BY_RULES^
20422 Include the section containing diagnoses ordered by rules in the report file
20423
20424 @cindex @option{^-s3^/BY_FILES_BY_RULES^} (@command{gnatcheck})
20425 @item ^-s3^/BY_FILES_BY_RULES^
20426 Include the section containing diagnoses ordered by files and then by rules
20427 in the report file
20428
20429 @cindex @option{^-v^/VERBOSE^} (@command{gnatcheck})
20430 @item ^-v^/VERBOSE^
20431 Verbose mode; @command{gnatcheck} generates version information and then
20432 a trace of sources being processed.
20433
20434 @end table
20435
20436 @noindent
20437 Note that if any of the options @option{^-s1^/COMPILER_STYLE^},
20438 @option{^-s2^/BY_RULES^} or
20439 @option{^-s3^/BY_FILES_BY_RULES^} is specified,
20440 then the @command{gnatcheck} report file will only contain sections
20441 explicitly denoted by these options.
20442
20443 @node gnatcheck Rule Options
20444 @section @command{gnatcheck} Rule Options
20445
20446 @noindent
20447 The following options control the processing performed by
20448 @command{gnatcheck}.
20449
20450 @table @option
20451 @cindex @option{+ALL} (@command{gnatcheck})
20452 @item +ALL
20453 Turn all the rule checks ON.
20454
20455 @cindex @option{-ALL} (@command{gnatcheck})
20456 @item -ALL
20457 Turn all the rule checks OFF.
20458
20459 @cindex @option{+R} (@command{gnatcheck})
20460 @item +R@var{rule_id}@r{[}:@var{param}@r{]}
20461 Turn on the check for a specified rule with the specified parameter, if any.
20462 @var{rule_id} must be the identifier of one of the currently implemented rules
20463 (use @option{^-h^/HELP^} for the list of implemented rules). Rule identifiers
20464 are not case-sensitive. The @var{param} item must
20465 be a string representing a valid parameter(s) for the specified rule.
20466 If it contains any space characters then this string must be enclosed in
20467 quotation marks.
20468
20469 @cindex @option{-R} (@command{gnatcheck})
20470 @item -R@var{rule_id}@r{[}:@var{param}@r{]}
20471 Turn off the check for a specified rule with the specified parameter, if any.
20472
20473 @cindex @option{-from} (@command{gnatcheck})
20474 @item -from=@var{rule_option_filename}
20475 Read the rule options from the text file @var{rule_option_filename}, referred as
20476 ``rule file'' below.
20477
20478 @end table
20479
20480 @noindent
20481 The default behavior is that all the rule checks are disabled.
20482
20483 A rule file is a text file containing a set of rule options.
20484 @cindex Rule file (for @code{gnatcheck})
20485 The file may contain empty lines and Ada-style comments (comment
20486 lines and end-of-line comments). The rule file has free format; that is,
20487 you do not have to start a new rule option on a new line.
20488
20489 A rule file may contain other @option{-from=@var{rule_option_filename}}
20490 options, each such option being replaced with the content of the
20491 corresponding rule file during the rule files processing. In case a
20492 cycle is detected (that is, @file{@var{rule_file_1}} reads rule options
20493 from @file{@var{rule_file_2}}, and @file{@var{rule_file_2}} reads
20494 (directly or indirectly) rule options from @file{@var{rule_file_1}}),
20495 the processing of rule files is interrupted and a part of their content
20496 is ignored.
20497
20498
20499 @node Adding the Results of Compiler Checks to gnatcheck Output
20500 @section Adding the Results of Compiler Checks to @command{gnatcheck} Output
20501
20502 @noindent
20503 The @command{gnatcheck} tool can include in the generated diagnostic messages
20504 and in
20505 the report file the results of the checks performed by the compiler. Though
20506 disabled by default, this effect may be obtained by using @option{+R} with
20507 the following rule identifiers and parameters:
20508
20509 @table @option
20510 @item Restrictions
20511 To record restrictions violations (that are performed by the compiler if the
20512 pragma @code{Restrictions} or @code{Restriction_Warnings} are given),
20513 use the rule named
20514 @code{Restrictions} with the same parameters as pragma
20515 @code{Restrictions} or @code{Restriction_Warnings}.
20516
20517 @item Style_Checks
20518 To record compiler style checks(@pxref{Style Checking}), use the rule named
20519 @code{Style_Checks}. A parameter of this rule can be either @code{All_Checks},
20520 which enables all the standard style checks that corresponds to @option{-gnatyy}
20521 GNAT style check option, or a string that has exactly the same
20522 structure and semantics as the @code{string_LITERAL} parameter of GNAT pragma
20523 @code{Style_Checks} (for further information about this pragma,
20524 @pxref{Pragma Style_Checks,,, gnat_rm, GNAT Reference Manual}).
20525
20526 @item Warnings
20527 To record compiler warnings (@pxref{Warning Message Control}), use the rule
20528 named @code{Warnings} with a parameter that is a valid
20529 @i{static_string_expression} argument of GNAT pragma @code{Warnings}
20530 (for further information about this pragma, @pxref{Pragma Warnings,,,
20531 gnat_rm, GNAT Reference Manual}). Note, that in case of gnatcheck
20532 's' parameter, that corresponds to the GNAT @option{-gnatws} option, disables
20533 all the specific warnings, but not suppresses the warning mode,
20534 and 'e' parameter, corresponding to @option{-gnatwe} that means
20535 "treat warnings as errors", does not have any effect.
20536
20537 @end table
20538
20539 To disable a specific restriction check, use @code{-RStyle_Checks} gnatcheck
20540 option with the corresponding restriction name as a parameter. @code{-R} is
20541 not available for @code{Style_Checks} and @code{Warnings} options, to disable
20542 warnings and style checks, use the corresponding warning and style options.
20543
20544 @node Project-Wide Checks
20545 @section Project-Wide Checks
20546 @cindex Project-wide checks (for @command{gnatcheck})
20547
20548 @noindent
20549 In order to perform checks on all units of a given project, you can use
20550 the GNAT driver along with the @option{-P} option:
20551 @smallexample
20552 gnat check -Pproj -rules -from=my_rules
20553 @end smallexample
20554
20555 @noindent
20556 If the project @code{proj} depends upon other projects, you can perform
20557 checks on the project closure using the @option{-U} option:
20558 @smallexample
20559 gnat check -Pproj -U -rules -from=my_rules
20560 @end smallexample
20561
20562 @noindent
20563 Finally, if not all the units are relevant to a particular main
20564 program in the project closure, you can perform checks for the set
20565 of units needed to create a given main program (unit closure) using
20566 the @option{-U} option followed by the name of the main unit:
20567 @smallexample
20568 gnat check -Pproj -U main -rules -from=my_rules
20569 @end smallexample
20570
20571
20572 @node Predefined Rules
20573 @section Predefined Rules
20574 @cindex Predefined rules (for @command{gnatcheck})
20575
20576 @ignore
20577 @c (Jan 2007) Since the global rules are still under development and are not
20578 @c documented, there is no point in explaining the difference between
20579 @c global and local rules
20580 @noindent
20581 A rule in @command{gnatcheck} is either local or global.
20582 A @emph{local rule} is a rule that applies to a well-defined section
20583 of a program and that can be checked by analyzing only this section.
20584 A @emph{global rule} requires analysis of some global properties of the
20585 whole program (mostly related to the program call graph).
20586 As of @value{NOW}, the implementation of global rules should be
20587 considered to be at a preliminary stage. You can use the
20588 @option{+GLOBAL} option to enable all the global rules, and the
20589 @option{-GLOBAL} rule option to disable all the global rules.
20590
20591 All the global rules in the list below are
20592 so indicated by marking them ``GLOBAL''.
20593 This +GLOBAL and -GLOBAL options are not
20594 included in the list of gnatcheck options above, because at the moment they
20595 are considered as a temporary debug options.
20596
20597 @command{gnatcheck} performs rule checks for generic
20598 instances only for global rules. This limitation may be relaxed in a later
20599 release.
20600 @end ignore
20601
20602 @noindent
20603 The following subsections document the rules implemented in
20604 @command{gnatcheck}.
20605 The subsection title is the same as the rule identifier, which may be
20606 used as a parameter of the @option{+R} or @option{-R} options.
20607
20608
20609 @menu
20610 * Abstract_Type_Declarations::
20611 * Anonymous_Arrays::
20612 * Anonymous_Subtypes::
20613 * Blocks::
20614 * Boolean_Relational_Operators::
20615 @ignore
20616 * Ceiling_Violations::
20617 @end ignore
20618 * Controlled_Type_Declarations::
20619 * Declarations_In_Blocks::
20620 * Default_Parameters::
20621 * Discriminated_Records::
20622 * Enumeration_Ranges_In_CASE_Statements::
20623 * Exceptions_As_Control_Flow::
20624 * EXIT_Statements_With_No_Loop_Name::
20625 * Expanded_Loop_Exit_Names::
20626 * Explicit_Full_Discrete_Ranges::
20627 * Float_Equality_Checks::
20628 * Forbidden_Pragmas::
20629 * Function_Style_Procedures::
20630 * Generics_In_Subprograms::
20631 * GOTO_Statements::
20632 * Implicit_IN_Mode_Parameters::
20633 * Implicit_SMALL_For_Fixed_Point_Types::
20634 * Improperly_Located_Instantiations::
20635 * Improper_Returns::
20636 * Library_Level_Subprograms::
20637 * Local_Packages::
20638 @ignore
20639 * Improperly_Called_Protected_Entries::
20640 @end ignore
20641 * Metrics::
20642 * Misnamed_Identifiers::
20643 * Multiple_Entries_In_Protected_Definitions::
20644 * Name_Clashes::
20645 * Non_Qualified_Aggregates::
20646 * Non_Short_Circuit_Operators::
20647 * Non_SPARK_Attributes::
20648 * Non_Tagged_Derived_Types::
20649 * Non_Visible_Exceptions::
20650 * Numeric_Literals::
20651 * OTHERS_In_Aggregates::
20652 * OTHERS_In_CASE_Statements::
20653 * OTHERS_In_Exception_Handlers::
20654 * Outer_Loop_Exits::
20655 * Overloaded_Operators::
20656 * Overly_Nested_Control_Structures::
20657 * Parameters_Out_Of_Order::
20658 * Positional_Actuals_For_Defaulted_Generic_Parameters::
20659 * Positional_Actuals_For_Defaulted_Parameters::
20660 * Positional_Components::
20661 * Positional_Generic_Parameters::
20662 * Positional_Parameters::
20663 * Predefined_Numeric_Types::
20664 * Raising_External_Exceptions::
20665 * Raising_Predefined_Exceptions::
20666 * Separate_Numeric_Error_Handlers::
20667 @ignore
20668 * Recursion::
20669 * Side_Effect_Functions::
20670 @end ignore
20671 * Slices::
20672 * Unassigned_OUT_Parameters::
20673 * Uncommented_BEGIN_In_Package_Bodies::
20674 * Unconstrained_Array_Returns::
20675 * Universal_Ranges::
20676 * Unnamed_Blocks_And_Loops::
20677 @ignore
20678 * Unused_Subprograms::
20679 @end ignore
20680 * USE_PACKAGE_Clauses::
20681 * Volatile_Objects_Without_Address_Clauses::
20682 @end menu
20683
20684
20685 @node Abstract_Type_Declarations
20686 @subsection @code{Abstract_Type_Declarations}
20687 @cindex @code{Abstract_Type_Declarations} rule (for @command{gnatcheck})
20688
20689 @noindent
20690 Flag all declarations of abstract types. For an abstract private
20691 type, both the private and full type declarations are flagged.
20692
20693 This rule has no parameters.
20694
20695
20696 @node Anonymous_Arrays
20697 @subsection @code{Anonymous_Arrays}
20698 @cindex @code{Anonymous_Arrays} rule (for @command{gnatcheck})
20699
20700 @noindent
20701 Flag all anonymous array type definitions (by Ada semantics these can only
20702 occur in object declarations).
20703
20704 This rule has no parameters.
20705
20706 @node Anonymous_Subtypes
20707 @subsection @code{Anonymous_Subtypes}
20708 @cindex @code{Anonymous_Subtypes} rule (for @command{gnatcheck})
20709
20710 @noindent
20711 Flag all uses of anonymous subtypes. A use of an anonymous subtype is
20712 any instance of a subtype indication with a constraint, other than one
20713 that occurs immediately within a subtype declaration. Any use of a range
20714 other than as a constraint used immediately within a subtype declaration
20715 is considered as an anonymous subtype.
20716
20717 An effect of this rule is that @code{for} loops such as the following are
20718 flagged (since @code{1..N} is formally a ``range''):
20719
20720 @smallexample @c ada
20721 for I in 1 .. N loop
20722 @dots{}
20723 end loop;
20724 @end smallexample
20725
20726 @noindent
20727 Declaring an explicit subtype solves the problem:
20728
20729 @smallexample @c ada
20730 subtype S is Integer range 1..N;
20731 @dots{}
20732 for I in S loop
20733 @dots{}
20734 end loop;
20735 @end smallexample
20736
20737 @noindent
20738 This rule has no parameters.
20739
20740 @node Blocks
20741 @subsection @code{Blocks}
20742 @cindex @code{Blocks} rule (for @command{gnatcheck})
20743
20744 @noindent
20745 Flag each block statement.
20746
20747 This rule has no parameters.
20748
20749 @node Boolean_Relational_Operators
20750 @subsection @code{Boolean_Relational_Operators}
20751 @cindex @code{Boolean_Relational_Operators} rule (for @command{gnatcheck})
20752
20753 @noindent
20754 Flag each call to a predefined relational operator (``<'', ``>'', ``<='',
20755 ``>='', ``='' and ``/='') for the predefined Boolean type.
20756 (This rule is useful in enforcing the SPARK language restrictions.)
20757
20758 Calls to predefined relational operators of any type derived from
20759 @code{Standard.Boolean} are not detected. Calls to user-defined functions
20760 with these designators, and uses of operators that are renamings
20761 of the predefined relational operators for @code{Standard.Boolean},
20762 are likewise not detected.
20763
20764 This rule has no parameters.
20765
20766 @ignore
20767 @node Ceiling_Violations
20768 @subsection @code{Ceiling_Violations} (under construction, GLOBAL)
20769 @cindex @code{Ceiling_Violations} rule (for @command{gnatcheck})
20770
20771 @noindent
20772 Flag invocations of a protected operation by a task whose priority exceeds
20773 the protected object's ceiling.
20774
20775 As of @value{NOW}, this rule has the following limitations:
20776
20777 @itemize @bullet
20778
20779 @item
20780 We consider only pragmas Priority and Interrupt_Priority as means to define
20781 a task/protected operation priority. We do not consider the effect of using
20782 Ada.Dynamic_Priorities.Set_Priority procedure;
20783
20784 @item
20785 We consider only base task priorities, and no priority inheritance. That is,
20786 we do not make a difference between calls issued during task activation and
20787 execution of the sequence of statements from task body;
20788
20789 @item
20790 Any situation when the priority of protected operation caller is set by a
20791 dynamic expression (that is, the corresponding Priority or
20792 Interrupt_Priority pragma has a non-static expression as an argument) we
20793 treat as a priority inconsistency (and, therefore, detect this situation).
20794 @end itemize
20795
20796 @noindent
20797 At the moment the notion of the main subprogram is not implemented in
20798 gnatcheck, so any pragma Priority in a library level subprogram body (in case
20799 if this subprogram can be a main subprogram of a partition) changes the
20800 priority of an environment task. So if we have more then one such pragma in
20801 the set of processed sources, the pragma that is processed last, defines the
20802 priority of an environment task.
20803
20804 This rule has no parameters.
20805 @end ignore
20806
20807 @node Controlled_Type_Declarations
20808 @subsection @code{Controlled_Type_Declarations}
20809 @cindex @code{Controlled_Type_Declarations} rule (for @command{gnatcheck})
20810
20811 @noindent
20812 Flag all declarations of controlled types. A declaration of a private type
20813 is flagged if its full declaration declares a controlled type. A declaration
20814 of a derived type is flagged if its ancestor type is controlled. Subtype
20815 declarations are not checked. A declaration of a type that itself is not a
20816 descendant of a type declared in @code{Ada.Finalization} but has a controlled
20817 component is not checked.
20818
20819 This rule has no parameters.
20820
20821
20822
20823 @node Declarations_In_Blocks
20824 @subsection @code{Declarations_In_Blocks}
20825 @cindex @code{Declarations_In_Blocks} rule (for @command{gnatcheck})
20826
20827 @noindent
20828 Flag all block statements containing local declarations. A @code{declare}
20829 block with an empty @i{declarative_part} or with a @i{declarative part}
20830 containing only pragmas and/or @code{use} clauses is not flagged.
20831
20832 This rule has no parameters.
20833
20834
20835 @node Default_Parameters
20836 @subsection @code{Default_Parameters}
20837 @cindex @code{Default_Parameters} rule (for @command{gnatcheck})
20838
20839 @noindent
20840 Flag all default expressions for subprogram parameters. Parameter
20841 declarations of formal and generic subprograms are also checked.
20842
20843 This rule has no parameters.
20844
20845
20846 @node Discriminated_Records
20847 @subsection @code{Discriminated_Records}
20848 @cindex @code{Discriminated_Records} rule (for @command{gnatcheck})
20849
20850 @noindent
20851 Flag all declarations of record types with discriminants. Only the
20852 declarations of record and record extension types are checked. Incomplete,
20853 formal, private, derived and private extension type declarations are not
20854 checked. Task and protected type declarations also are not checked.
20855
20856 This rule has no parameters.
20857
20858
20859 @node Enumeration_Ranges_In_CASE_Statements
20860 @subsection @code{Enumeration_Ranges_In_CASE_Statements}
20861 @cindex @code{Enumeration_Ranges_In_CASE_Statements} (for @command{gnatcheck})
20862
20863 @noindent
20864 Flag each use of a range of enumeration literals as a choice in a
20865 @code{case} statement.
20866 All forms for specifying a range (explicit ranges
20867 such as @code{A .. B}, subtype marks and @code{'Range} attributes) are flagged.
20868 An enumeration range is
20869 flagged even if contains exactly one enumeration value or no values at all. A
20870 type derived from an enumeration type is considered as an enumeration type.
20871
20872 This rule helps prevent maintenance problems arising from adding an
20873 enumeration value to a type and having it implicitly handled by an existing
20874 @code{case} statement with an enumeration range that includes the new literal.
20875
20876 This rule has no parameters.
20877
20878
20879 @node Exceptions_As_Control_Flow
20880 @subsection @code{Exceptions_As_Control_Flow}
20881 @cindex @code{Exceptions_As_Control_Flow} (for @command{gnatcheck})
20882
20883 @noindent
20884 Flag each place where an exception is explicitly raised and handled in the
20885 same subprogram body. A @code{raise} statement in an exception handler,
20886 package body, task body or entry body is not flagged.
20887
20888 The rule has no parameters.
20889
20890 @node EXIT_Statements_With_No_Loop_Name
20891 @subsection @code{EXIT_Statements_With_No_Loop_Name}
20892 @cindex @code{EXIT_Statements_With_No_Loop_Name} (for @command{gnatcheck})
20893
20894 @noindent
20895 Flag each @code{exit} statement that does not specify the name of the loop
20896 being exited.
20897
20898 The rule has no parameters.
20899
20900
20901 @node Expanded_Loop_Exit_Names
20902 @subsection @code{Expanded_Loop_Exit_Names}
20903 @cindex @code{Expanded_Loop_Exit_Names} rule (for @command{gnatcheck})
20904
20905 @noindent
20906 Flag all expanded loop names in @code{exit} statements.
20907
20908 This rule has no parameters.
20909
20910 @node Explicit_Full_Discrete_Ranges
20911 @subsection @code{Explicit_Full_Discrete_Ranges}
20912 @cindex @code{Explicit_Full_Discrete_Ranges} rule (for @command{gnatcheck})
20913
20914 @noindent
20915 Flag each discrete range that has the form @code{A'First .. A'Last}.
20916
20917 This rule has no parameters.
20918
20919 @node Float_Equality_Checks
20920 @subsection @code{Float_Equality_Checks}
20921 @cindex @code{Float_Equality_Checks} rule (for @command{gnatcheck})
20922
20923 @noindent
20924 Flag all calls to the predefined equality operations for floating-point types.
20925 Both ``@code{=}'' and ``@code{/=}'' operations are checked.
20926 User-defined equality operations are not flagged, nor are ``@code{=}''
20927 and ``@code{/=}'' operations for fixed-point types.
20928
20929 This rule has no parameters.
20930
20931
20932 @node Forbidden_Pragmas
20933 @subsection @code{Forbidden_Pragmas}
20934 @cindex @code{Forbidden_Pragmas} rule (for @command{gnatcheck})
20935
20936 @noindent
20937 Flag each use of the specified pragmas. The pragmas to be detected
20938 are named in the rule's parameters.
20939
20940 This rule has the following parameters:
20941
20942 @itemize @bullet
20943 @item For the @option{+R} option
20944
20945 @table @asis
20946 @item @emph{Pragma_Name}
20947 Adds the specified pragma to the set of pragmas to be
20948 checked and sets the checks for all the specified pragmas
20949 ON. @emph{Pragma_Name} is treated as a name of a pragma. If it
20950 does not correspond to any pragma name defined in the Ada
20951 standard or to the name of a GNAT-specific pragma defined
20952 in @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference
20953 Manual}, it is treated as the name of unknown pragma.
20954
20955 @item @code{GNAT}
20956 All the GNAT-specific pragmas are detected; this sets
20957 the checks for all the specified pragmas ON.
20958
20959 @item @code{ALL}
20960 All pragmas are detected; this sets the rule ON.
20961 @end table
20962
20963 @item For the @option{-R} option
20964 @table @asis
20965 @item @emph{Pragma_Name}
20966 Removes the specified pragma from the set of pragmas to be
20967 checked without affecting checks for
20968 other pragmas. @emph{Pragma_Name} is treated as a name
20969 of a pragma. If it does not correspond to any pragma
20970 defined in the Ada standard or to any name defined in
20971 @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference Manual},
20972 this option is treated as turning OFF detection of all unknown pragmas.
20973
20974 @item GNAT
20975 Turn OFF detection of all GNAT-specific pragmas
20976
20977 @item ALL
20978 Clear the list of the pragmas to be detected and
20979 turn the rule OFF.
20980 @end table
20981 @end itemize
20982
20983 @noindent
20984 Parameters are not case sensitive. If @emph{Pragma_Name} does not have
20985 the syntax of an Ada identifier and therefore can not be considered
20986 as a pragma name, a diagnostic message is generated and the corresponding
20987 parameter is ignored.
20988
20989 When more then one parameter is given in the same rule option, the parameters
20990 must be separated by a comma.
20991
20992 If more then one option for this rule is specified for the @command{gnatcheck}
20993 call, a new option overrides the previous one(s).
20994
20995 The @option{+R} option with no parameters turns the rule ON with the set of
20996 pragmas to be detected defined by the previous rule options.
20997 (By default this set is empty, so if the only option specified for the rule is
20998 @option{+RForbidden_Pragmas} (with
20999 no parameter), then the rule is enabled, but it does not detect anything).
21000 The @option{-R} option with no parameter turns the rule OFF, but it does not
21001 affect the set of pragmas to be detected.
21002
21003
21004
21005
21006 @node Function_Style_Procedures
21007 @subsection @code{Function_Style_Procedures}
21008 @cindex @code{Function_Style_Procedures} rule (for @command{gnatcheck})
21009
21010 @noindent
21011 Flag each procedure that can be rewritten as a function. A procedure can be
21012 converted into a function if it has exactly one parameter of mode @code{out}
21013 and no parameters of mode @code{in out}. Procedure declarations,
21014 formal procedure declarations, and generic procedure declarations are always
21015 checked. Procedure
21016 bodies and body stubs are flagged only if they do not have corresponding
21017 separate declarations. Procedure renamings and procedure instantiations are
21018 not flagged.
21019
21020 If a procedure can be rewritten as a function, but its @code{out} parameter is
21021 of a limited type, it is not flagged.
21022
21023 Protected procedures are not flagged. Null procedures also are not flagged.
21024
21025 This rule has no parameters.
21026
21027
21028 @node Generics_In_Subprograms
21029 @subsection @code{Generics_In_Subprograms}
21030 @cindex @code{Generics_In_Subprograms} rule (for @command{gnatcheck})
21031
21032 @noindent
21033 Flag each declaration of a generic unit in a subprogram. Generic
21034 declarations in the bodies of generic subprograms are also flagged.
21035 A generic unit nested in another generic unit is not flagged.
21036 If a generic unit is
21037 declared in a local package that is declared in a subprogram body, the
21038 generic unit is flagged.
21039
21040 This rule has no parameters.
21041
21042
21043 @node GOTO_Statements
21044 @subsection @code{GOTO_Statements}
21045 @cindex @code{GOTO_Statements} rule (for @command{gnatcheck})
21046
21047 @noindent
21048 Flag each occurrence of a @code{goto} statement.
21049
21050 This rule has no parameters.
21051
21052
21053 @node Implicit_IN_Mode_Parameters
21054 @subsection @code{Implicit_IN_Mode_Parameters}
21055 @cindex @code{Implicit_IN_Mode_Parameters} rule (for @command{gnatcheck})
21056
21057 @noindent
21058 Flag each occurrence of a formal parameter with an implicit @code{in} mode.
21059 Note that @code{access} parameters, although they technically behave
21060 like @code{in} parameters, are not flagged.
21061
21062 This rule has no parameters.
21063
21064
21065 @node Implicit_SMALL_For_Fixed_Point_Types
21066 @subsection @code{Implicit_SMALL_For_Fixed_Point_Types}
21067 @cindex @code{Implicit_SMALL_For_Fixed_Point_Types} rule (for @command{gnatcheck})
21068
21069 @noindent
21070 Flag each fixed point type declaration that lacks an explicit
21071 representation clause to define its @code{'Small} value.
21072 Since @code{'Small} can be defined only for ordinary fixed point types,
21073 decimal fixed point type declarations are not checked.
21074
21075 This rule has no parameters.
21076
21077
21078 @node Improperly_Located_Instantiations
21079 @subsection @code{Improperly_Located_Instantiations}
21080 @cindex @code{Improperly_Located_Instantiations} rule (for @command{gnatcheck})
21081
21082 @noindent
21083 Flag all generic instantiations in library-level package specs
21084 (including library generic packages) and in all subprogram bodies.
21085
21086 Instantiations in task and entry bodies are not flagged. Instantiations in the
21087 bodies of protected subprograms are flagged.
21088
21089 This rule has no parameters.
21090
21091
21092
21093 @node Improper_Returns
21094 @subsection @code{Improper_Returns}
21095 @cindex @code{Improper_Returns} rule (for @command{gnatcheck})
21096
21097 @noindent
21098 Flag each explicit @code{return} statement in procedures, and
21099 multiple @code{return} statements in functions.
21100 Diagnostic messages are generated for all @code{return} statements
21101 in a procedure (thus each procedure must be written so that it
21102 returns implicitly at the end of its statement part),
21103 and for all @code{return} statements in a function after the first one.
21104 This rule supports the stylistic convention that each subprogram
21105 should have no more than one point of normal return.
21106
21107 This rule has no parameters.
21108
21109
21110 @node Library_Level_Subprograms
21111 @subsection @code{Library_Level_Subprograms}
21112 @cindex @code{Library_Level_Subprograms} rule (for @command{gnatcheck})
21113
21114 @noindent
21115 Flag all library-level subprograms (including generic subprogram instantiations).
21116
21117 This rule has no parameters.
21118
21119
21120 @node Local_Packages
21121 @subsection @code{Local_Packages}
21122 @cindex @code{Local_Packages} rule (for @command{gnatcheck})
21123
21124 @noindent
21125 Flag all local packages declared in package and generic package
21126 specs.
21127 Local packages in bodies are not flagged.
21128
21129 This rule has no parameters.
21130
21131 @ignore
21132 @node Improperly_Called_Protected_Entries
21133 @subsection @code{Improperly_Called_Protected_Entries} (under construction, GLOBAL)
21134 @cindex @code{Improperly_Called_Protected_Entries} rule (for @command{gnatcheck})
21135
21136 @noindent
21137 Flag each protected entry that can be called from more than one task.
21138
21139 This rule has no parameters.
21140 @end ignore
21141
21142 @node Metrics
21143 @subsection @code{Metrics}
21144 @cindex @code{Metrics} rule (for @command{gnatcheck})
21145
21146 @noindent
21147 There is a set of checks based on computing a metric value and comparing the
21148 result with the specified upper (or lower, depending on a specific metric)
21149 value specified for a given metric. A construct is flagged if a given metric
21150 is applicable (can be computed) for it and the computed value is greater
21151 then (lover then) the specified upper (lower) bound.
21152
21153 The name of any metric-based rule consists of the prefix @code{Metrics_}
21154 followed by the name of the corresponding metric (see the table below).
21155 For @option{+R} option, each metric-based rule has a numeric parameter
21156 specifying the bound (integer or real, depending on a metric), @option{-R}
21157 option for metric rules does not have a parameter.
21158
21159 The following table shows the metric names for that the corresponding
21160 metrics-based checks are supported by gnatcheck, including the
21161 constraint that must be satisfied by the bound that is specified for the check
21162 and what bound - upper (U) or lower (L) - should be specified.
21163
21164 @multitable {@code{Cyclomatic_Complexity}}{Cyclomatic complexity}{Positive integer}
21165 @ifnothtml
21166 @headitem Check Name @tab Description @tab Bounds Value
21167 @end ifnothtml
21168 @ifhtml
21169 @item @b{Check Name} @tab @b{Description} @tab @b{Bounds Value}
21170 @end ifhtml
21171 @c Above conditional code is workaround to bug in texi2html (Feb 2008)
21172 @item @code{Essential_Complexity} @tab Essential complexity @tab Positive integer (U)
21173 @item @code{Cyclomatic_Complexity} @tab Cyclomatic complexity @tab Positive integer (U)
21174 @item @code{LSLOC} @tab Logical Source Lines of Code @tab Positive integer (U)
21175 @end multitable
21176
21177 @noindent
21178 The meaning and the computed values for all these metrics are exactly
21179 the same as for the corresponding metrics in @command{gnatmetric}.
21180
21181 @emph{Example:} the rule
21182 @smallexample
21183 +RMetrics_Cyclomatic_Complexity : 7
21184 @end smallexample
21185 @noindent
21186 means that all bodies with cyclomatic complexity exceeding 7 will be flagged.
21187
21188 To turn OFF the check for cyclomatic complexity metric, use the following option:
21189 @smallexample
21190 -RMetrics_Cyclomatic_Complexity
21191 @end smallexample
21192
21193 @node Misnamed_Identifiers
21194 @subsection @code{Misnamed_Identifiers}
21195 @cindex @code{Misnamed_Identifiers} rule (for @command{gnatcheck})
21196
21197 @noindent
21198 Flag the declaration of each identifier that does not have a suffix
21199 corresponding to the kind of entity being declared.
21200 The following declarations are checked:
21201
21202 @itemize @bullet
21203 @item
21204 type declarations
21205
21206 @item
21207 constant declarations (but not number declarations)
21208
21209 @item
21210 package renaming declarations (but not generic package renaming
21211 declarations)
21212 @end itemize
21213
21214 @noindent
21215 This rule may have parameters. When used without parameters, the rule enforces
21216 the following checks:
21217
21218 @itemize @bullet
21219 @item
21220 type-defining names end with @code{_T}, unless the type is an access type,
21221 in which case the suffix must be @code{_A}
21222 @item
21223 constant names end with @code{_C}
21224 @item
21225 names defining package renamings end with @code{_R}
21226 @end itemize
21227
21228 @noindent
21229 For a private or incomplete type declaration the following checks are
21230 made for the defining name suffix:
21231
21232 @itemize @bullet
21233 @item
21234 For an incomplete type declaration: if the corresponding full type
21235 declaration is available, the defining identifier from the full type
21236 declaration is checked, but the defining identifier from the incomplete type
21237 declaration is not; otherwise the defining identifier from the incomplete
21238 type declaration is checked against the suffix specified for type
21239 declarations.
21240
21241 @item
21242 For a private type declaration (including private extensions), the defining
21243 identifier from the private type declaration is checked against the type
21244 suffix (even if the corresponding full declaration is an access type
21245 declaration), and the defining identifier from the corresponding full type
21246 declaration is not checked.
21247 @end itemize
21248
21249 @noindent
21250 For a deferred constant, the defining name in the corresponding full constant
21251 declaration is not checked.
21252
21253 Defining names of formal types are not checked.
21254
21255 The rule may have the following parameters:
21256
21257 @itemize @bullet
21258 @item
21259 For the @option{+R} option:
21260 @table @code
21261 @item Default
21262 Sets the default listed above for all the names to be checked.
21263
21264 @item Type_Suffix=@emph{string}
21265 Specifies the suffix for a type name.
21266
21267 @item Access_Suffix=@emph{string}
21268 Specifies the suffix for an access type name. If
21269 this parameter is set, it overrides for access
21270 types the suffix set by the @code{Type_Suffix} parameter.
21271
21272 @item Constant_Suffix=@emph{string}
21273 Specifies the suffix for a constant name.
21274
21275 @item Renaming_Suffix=@emph{string}
21276 Specifies the suffix for a package renaming name.
21277 @end table
21278
21279 @item
21280 For the @option{-R} option:
21281 @table @code
21282 @item All_Suffixes
21283 Remove all the suffixes specified for the
21284 identifier suffix checks, whether by default or
21285 as specified by other rule parameters. All the
21286 checks for this rule are disabled as a result.
21287
21288 @item Type_Suffix
21289 Removes the suffix specified for types. This
21290 disables checks for types but does not disable
21291 any other checks for this rule (including the
21292 check for access type names if @code{Access_Suffix} is
21293 set).
21294
21295 @item Access_Suffix
21296 Removes the suffix specified for access types.
21297 This disables checks for access type names but
21298 does not disable any other checks for this rule.
21299 If @code{Type_Suffix} is set, access type names are
21300 checked as ordinary type names.
21301
21302 @item Constant_Suffix
21303 Removes the suffix specified for constants. This
21304 disables checks for constant names but does not
21305 disable any other checks for this rule.
21306
21307 @item Renaming_Suffix
21308 Removes the suffix specified for package
21309 renamings. This disables checks for package
21310 renamings but does not disable any other checks
21311 for this rule.
21312 @end table
21313 @end itemize
21314
21315 @noindent
21316 If more than one parameter is used, parameters must be separated by commas.
21317
21318 If more than one option is specified for the @command{gnatcheck} invocation,
21319 a new option overrides the previous one(s).
21320
21321 The @option{+RMisnamed_Identifiers} option (with no parameter) enables
21322 checks for all the
21323 name suffixes specified by previous options used for this rule.
21324
21325 The @option{-RMisnamed_Identifiers} option (with no parameter) disables
21326 all the checks but keeps
21327 all the suffixes specified by previous options used for this rule.
21328
21329 The @emph{string} value must be a valid suffix for an Ada identifier (after
21330 trimming all the leading and trailing space characters, if any).
21331 Parameters are not case sensitive, except the @emph{string} part.
21332
21333 If any error is detected in a rule parameter, the parameter is ignored.
21334 In such a case the options that are set for the rule are not
21335 specified.
21336
21337
21338
21339 @node Multiple_Entries_In_Protected_Definitions
21340 @subsection @code{Multiple_Entries_In_Protected_Definitions}
21341 @cindex @code{Multiple_Entries_In_Protected_Definitions} rule (for @command{gnatcheck})
21342
21343 @noindent
21344 Flag each protected definition (i.e., each protected object/type declaration)
21345 that defines more than one entry.
21346 Diagnostic messages are generated for all the entry declarations
21347 except the first one. An entry family is counted as one entry. Entries from
21348 the private part of the protected definition are also checked.
21349
21350 This rule has no parameters.
21351
21352 @node Name_Clashes
21353 @subsection @code{Name_Clashes}
21354 @cindex @code{Name_Clashes} rule (for @command{gnatcheck})
21355
21356 @noindent
21357 Check that certain names are not used as defining identifiers. To activate
21358 this rule, you need to supply a reference to the dictionary file(s) as a rule
21359 parameter(s) (more then one dictionary file can be specified). If no
21360 dictionary file is set, this rule will not cause anything to be flagged.
21361 Only defining occurrences, not references, are checked.
21362 The check is not case-sensitive.
21363
21364 This rule is enabled by default, but without setting any corresponding
21365 dictionary file(s); thus the default effect is to do no checks.
21366
21367 A dictionary file is a plain text file. The maximum line length for this file
21368 is 1024 characters. If the line is longer then this limit, extra characters
21369 are ignored.
21370
21371 Each line can be either an empty line, a comment line, or a line containing
21372 a list of identifiers separated by space or HT characters.
21373 A comment is an Ada-style comment (from @code{--} to end-of-line).
21374 Identifiers must follow the Ada syntax for identifiers.
21375 A line containing one or more identifiers may end with a comment.
21376
21377 @node Non_Qualified_Aggregates
21378 @subsection @code{Non_Qualified_Aggregates}
21379 @cindex @code{Non_Qualified_Aggregates} rule (for @command{gnatcheck})
21380
21381 @noindent
21382 Flag each non-qualified aggregate.
21383 A non-qualified aggregate is an
21384 aggregate that is not the expression of a qualified expression. A
21385 string literal is not considered an aggregate, but an array
21386 aggregate of a string type is considered as a normal aggregate.
21387 Aggregates of anonymous array types are not flagged.
21388
21389 This rule has no parameters.
21390
21391
21392 @node Non_Short_Circuit_Operators
21393 @subsection @code{Non_Short_Circuit_Operators}
21394 @cindex @code{Non_Short_Circuit_Operators} rule (for @command{gnatcheck})
21395
21396 @noindent
21397 Flag all calls to predefined @code{and} and @code{or} operators for
21398 any boolean type. Calls to
21399 user-defined @code{and} and @code{or} and to operators defined by renaming
21400 declarations are not flagged. Calls to predefined @code{and} and @code{or}
21401 operators for modular types or boolean array types are not flagged.
21402
21403 This rule has no parameters.
21404
21405
21406
21407 @node Non_SPARK_Attributes
21408 @subsection @code{Non_SPARK_Attributes}
21409 @cindex @code{Non_SPARK_Attributes} rule (for @command{gnatcheck})
21410
21411 @noindent
21412 The SPARK language defines the following subset of Ada 95 attribute
21413 designators as those that can be used in SPARK programs. The use of
21414 any other attribute is flagged.
21415
21416 @itemize @bullet
21417 @item @code{'Adjacent}
21418 @item @code{'Aft}
21419 @item @code{'Base}
21420 @item @code{'Ceiling}
21421 @item @code{'Component_Size}
21422 @item @code{'Compose}
21423 @item @code{'Copy_Sign}
21424 @item @code{'Delta}
21425 @item @code{'Denorm}
21426 @item @code{'Digits}
21427 @item @code{'Exponent}
21428 @item @code{'First}
21429 @item @code{'Floor}
21430 @item @code{'Fore}
21431 @item @code{'Fraction}
21432 @item @code{'Last}
21433 @item @code{'Leading_Part}
21434 @item @code{'Length}
21435 @item @code{'Machine}
21436 @item @code{'Machine_Emax}
21437 @item @code{'Machine_Emin}
21438 @item @code{'Machine_Mantissa}
21439 @item @code{'Machine_Overflows}
21440 @item @code{'Machine_Radix}
21441 @item @code{'Machine_Rounds}
21442 @item @code{'Max}
21443 @item @code{'Min}
21444 @item @code{'Model}
21445 @item @code{'Model_Emin}
21446 @item @code{'Model_Epsilon}
21447 @item @code{'Model_Mantissa}
21448 @item @code{'Model_Small}
21449 @item @code{'Modulus}
21450 @item @code{'Pos}
21451 @item @code{'Pred}
21452 @item @code{'Range}
21453 @item @code{'Remainder}
21454 @item @code{'Rounding}
21455 @item @code{'Safe_First}
21456 @item @code{'Safe_Last}
21457 @item @code{'Scaling}
21458 @item @code{'Signed_Zeros}
21459 @item @code{'Size}
21460 @item @code{'Small}
21461 @item @code{'Succ}
21462 @item @code{'Truncation}
21463 @item @code{'Unbiased_Rounding}
21464 @item @code{'Val}
21465 @item @code{'Valid}
21466 @end itemize
21467
21468 @noindent
21469 This rule has no parameters.
21470
21471
21472 @node Non_Tagged_Derived_Types
21473 @subsection @code{Non_Tagged_Derived_Types}
21474 @cindex @code{Non_Tagged_Derived_Types} rule (for @command{gnatcheck})
21475
21476 @noindent
21477 Flag all derived type declarations that do not have a record extension part.
21478
21479 This rule has no parameters.
21480
21481
21482
21483 @node Non_Visible_Exceptions
21484 @subsection @code{Non_Visible_Exceptions}
21485 @cindex @code{Non_Visible_Exceptions} rule (for @command{gnatcheck})
21486
21487 @noindent
21488 Flag constructs leading to the possibility of propagating an exception
21489 out of the scope in which the exception is declared.
21490 Two cases are detected:
21491
21492 @itemize @bullet
21493 @item
21494 An exception declaration in a subprogram body, task body or block
21495 statement is flagged if the body or statement does not contain a handler for
21496 that exception or a handler with an @code{others} choice.
21497
21498 @item
21499 A @code{raise} statement in an exception handler of a subprogram body,
21500 task body or block statement is flagged if it (re)raises a locally
21501 declared exception. This may occur under the following circumstances:
21502 @itemize @minus
21503 @item
21504 it explicitly raises a locally declared exception, or
21505 @item
21506 it does not specify an exception name (i.e., it is simply @code{raise;})
21507 and the enclosing handler contains a locally declared exception in its
21508 exception choices.
21509 @end itemize
21510 @end itemize
21511
21512 @noindent
21513 Renamings of local exceptions are not flagged.
21514
21515 This rule has no parameters.
21516
21517
21518 @node Numeric_Literals
21519 @subsection @code{Numeric_Literals}
21520 @cindex @code{Numeric_Literals} rule (for @command{gnatcheck})
21521
21522 @noindent
21523 Flag each use of a numeric literal in an index expression, and in any
21524 circumstance except for the following:
21525
21526 @itemize @bullet
21527 @item
21528 a literal occurring in the initialization expression for a constant
21529 declaration or a named number declaration, or
21530
21531 @item
21532 an integer literal that is less than or equal to a value
21533 specified by the @option{N} rule parameter.
21534 @end itemize
21535
21536 @noindent
21537 This rule may have the following parameters for the @option{+R} option:
21538
21539 @table @asis
21540 @item @emph{N}
21541 @emph{N} is an integer literal used as the maximal value that is not flagged
21542 (i.e., integer literals not exceeding this value are allowed)
21543
21544 @item @code{ALL}
21545 All integer literals are flagged
21546 @end table
21547
21548 @noindent
21549 If no parameters are set, the maximum unflagged value is 1.
21550
21551 The last specified check limit (or the fact that there is no limit at
21552 all) is used when multiple @option{+R} options appear.
21553
21554 The @option{-R} option for this rule has no parameters.
21555 It disables the rule but retains the last specified maximum unflagged value.
21556 If the @option{+R} option subsequently appears, this value is used as the
21557 threshold for the check.
21558
21559
21560 @node OTHERS_In_Aggregates
21561 @subsection @code{OTHERS_In_Aggregates}
21562 @cindex @code{OTHERS_In_Aggregates} rule (for @command{gnatcheck})
21563
21564 @noindent
21565 Flag each use of an @code{others} choice in extension aggregates.
21566 In record and array aggregates, an @code{others} choice is flagged unless
21567 it is used to refer to all components, or to all but one component.
21568
21569 If, in case of a named array aggregate, there are two associations, one
21570 with an @code{others} choice and another with a discrete range, the
21571 @code{others} choice is flagged even if the discrete range specifies
21572 exactly one component; for example, @code{(1..1 => 0, others => 1)}.
21573
21574 This rule has no parameters.
21575
21576 @node OTHERS_In_CASE_Statements
21577 @subsection @code{OTHERS_In_CASE_Statements}
21578 @cindex @code{OTHERS_In_CASE_Statements} rule (for @command{gnatcheck})
21579
21580 @noindent
21581 Flag any use of an @code{others} choice in a @code{case} statement.
21582
21583 This rule has no parameters.
21584
21585 @node OTHERS_In_Exception_Handlers
21586 @subsection @code{OTHERS_In_Exception_Handlers}
21587 @cindex @code{OTHERS_In_Exception_Handlers} rule (for @command{gnatcheck})
21588
21589 @noindent
21590 Flag any use of an @code{others} choice in an exception handler.
21591
21592 This rule has no parameters.
21593
21594
21595 @node Outer_Loop_Exits
21596 @subsection @code{Outer_Loop_Exits}
21597 @cindex @code{Outer_Loop_Exits} rule (for @command{gnatcheck})
21598
21599 @noindent
21600 Flag each @code{exit} statement containing a loop name that is not the name
21601 of the immediately enclosing @code{loop} statement.
21602
21603 This rule has no parameters.
21604
21605
21606 @node Overloaded_Operators
21607 @subsection @code{Overloaded_Operators}
21608 @cindex @code{Overloaded_Operators} rule (for @command{gnatcheck})
21609
21610 @noindent
21611 Flag each function declaration that overloads an operator symbol.
21612 A function body is checked only if the body does not have a
21613 separate spec. Formal functions are also checked. For a
21614 renaming declaration, only renaming-as-declaration is checked
21615
21616 This rule has no parameters.
21617
21618
21619 @node Overly_Nested_Control_Structures
21620 @subsection @code{Overly_Nested_Control_Structures}
21621 @cindex @code{Overly_Nested_Control_Structures} rule (for @command{gnatcheck})
21622
21623 @noindent
21624 Flag each control structure whose nesting level exceeds the value provided
21625 in the rule parameter.
21626
21627 The control structures checked are the following:
21628
21629 @itemize @bullet
21630 @item @code{if} statement
21631 @item @code{case} statement
21632 @item @code{loop} statement
21633 @item Selective accept statement
21634 @item Timed entry call statement
21635 @item Conditional entry call
21636 @item Asynchronous select statement
21637 @end itemize
21638
21639 @noindent
21640 The rule has the following parameter for the @option{+R} option:
21641
21642 @table @emph
21643 @item N
21644 Positive integer specifying the maximal control structure nesting
21645 level that is not flagged
21646 @end table
21647
21648 @noindent
21649 If the parameter for the @option{+R} option is not specified or
21650 if it is not a positive integer, @option{+R} option is ignored.
21651
21652 If more then one option is specified for the gnatcheck call, the later option and
21653 new parameter override the previous one(s).
21654
21655
21656 @node Parameters_Out_Of_Order
21657 @subsection @code{Parameters_Out_Of_Order}
21658 @cindex @code{Parameters_Out_Of_Order} rule (for @command{gnatcheck})
21659
21660 @noindent
21661 Flag each subprogram and entry declaration whose formal parameters are not
21662 ordered according to the following scheme:
21663
21664 @itemize @bullet
21665
21666 @item @code{in} and @code{access} parameters first,
21667 then @code{in out} parameters,
21668 and then @code{out} parameters;
21669
21670 @item for @code{in} mode, parameters with default initialization expressions
21671 occur last
21672 @end itemize
21673
21674 @noindent
21675 Only the first violation of the described order is flagged.
21676
21677 The following constructs are checked:
21678
21679 @itemize @bullet
21680 @item subprogram declarations (including null procedures);
21681 @item generic subprogram declarations;
21682 @item formal subprogram declarations;
21683 @item entry declarations;
21684 @item subprogram bodies and subprogram body stubs that do not
21685 have separate specifications
21686 @end itemize
21687
21688 @noindent
21689 Subprogram renamings are not checked.
21690
21691 This rule has no parameters.
21692
21693
21694 @node Positional_Actuals_For_Defaulted_Generic_Parameters
21695 @subsection @code{Positional_Actuals_For_Defaulted_Generic_Parameters}
21696 @cindex @code{Positional_Actuals_For_Defaulted_Generic_Parameters} rule (for @command{gnatcheck})
21697
21698 @noindent
21699 Flag each generic actual parameter corresponding to a generic formal
21700 parameter with a default initialization, if positional notation is used.
21701
21702 This rule has no parameters.
21703
21704 @node Positional_Actuals_For_Defaulted_Parameters
21705 @subsection @code{Positional_Actuals_For_Defaulted_Parameters}
21706 @cindex @code{Positional_Actuals_For_Defaulted_Parameters} rule (for @command{gnatcheck})
21707
21708 @noindent
21709 Flag each actual parameter to a subprogram or entry call where the
21710 corresponding formal parameter has a default expression, if positional
21711 notation is used.
21712
21713 This rule has no parameters.
21714
21715 @node Positional_Components
21716 @subsection @code{Positional_Components}
21717 @cindex @code{Positional_Components} rule (for @command{gnatcheck})
21718
21719 @noindent
21720 Flag each array, record and extension aggregate that includes positional
21721 notation.
21722
21723 This rule has no parameters.
21724
21725
21726 @node Positional_Generic_Parameters
21727 @subsection @code{Positional_Generic_Parameters}
21728 @cindex @code{Positional_Generic_Parameters} rule (for @command{gnatcheck})
21729
21730 @noindent
21731 Flag each instantiation using positional parameter notation.
21732
21733 This rule has no parameters.
21734
21735
21736 @node Positional_Parameters
21737 @subsection @code{Positional_Parameters}
21738 @cindex @code{Positional_Parameters} rule (for @command{gnatcheck})
21739
21740 @noindent
21741 Flag each subprogram or entry call using positional parameter notation,
21742 except for the following:
21743
21744 @itemize @bullet
21745 @item
21746 Invocations of prefix or infix operators are not flagged
21747 @item
21748 If the called subprogram or entry has only one formal parameter,
21749 the call is not flagged;
21750 @item
21751 If a subprogram call uses the @emph{Object.Operation} notation, then
21752 @itemize @minus
21753 @item
21754 the first parameter (that is, @emph{Object}) is not flagged;
21755 @item
21756 if the called subprogram has only two parameters, the second parameter
21757 of the call is not flagged;
21758 @end itemize
21759 @end itemize
21760
21761 @noindent
21762 This rule has no parameters.
21763
21764
21765
21766
21767 @node Predefined_Numeric_Types
21768 @subsection @code{Predefined_Numeric_Types}
21769 @cindex @code{Predefined_Numeric_Types} rule (for @command{gnatcheck})
21770
21771 @noindent
21772 Flag each explicit use of the name of any numeric type or subtype defined
21773 in package @code{Standard}.
21774
21775 The rationale for this rule is to detect when the
21776 program may depend on platform-specific characteristics of the implementation
21777 of the predefined numeric types. Note that this rule is over-pessimistic;
21778 for example, a program that uses @code{String} indexing
21779 likely needs a variable of type @code{Integer}.
21780 Another example is the flagging of predefined numeric types with explicit
21781 constraints:
21782
21783 @smallexample @c ada
21784 subtype My_Integer is Integer range Left .. Right;
21785 Vy_Var : My_Integer;
21786 @end smallexample
21787
21788 @noindent
21789 This rule detects only numeric types and subtypes defined in
21790 @code{Standard}. The use of numeric types and subtypes defined in other
21791 predefined packages (such as @code{System.Any_Priority} or
21792 @code{Ada.Text_IO.Count}) is not flagged
21793
21794 This rule has no parameters.
21795
21796
21797
21798 @node Raising_External_Exceptions
21799 @subsection @code{Raising_External_Exceptions}
21800 @cindex @code{Raising_External_Exceptions} rule (for @command{gnatcheck})
21801
21802 @noindent
21803 Flag any @code{raise} statement, in a program unit declared in a library
21804 package or in a generic library package, for an exception that is
21805 neither a predefined exception nor an exception that is also declared (or
21806 renamed) in the visible part of the package.
21807
21808 This rule has no parameters.
21809
21810
21811
21812 @node Raising_Predefined_Exceptions
21813 @subsection @code{Raising_Predefined_Exceptions}
21814 @cindex @code{Raising_Predefined_Exceptions} rule (for @command{gnatcheck})
21815
21816 @noindent
21817 Flag each @code{raise} statement that raises a predefined exception
21818 (i.e., one of the exceptions @code{Constraint_Error}, @code{Numeric_Error},
21819 @code{Program_Error}, @code{Storage_Error}, or @code{Tasking_Error}).
21820
21821 This rule has no parameters.
21822
21823 @node Separate_Numeric_Error_Handlers
21824 @subsection @code{Separate_Numeric_Error_Handlers}
21825 @cindex @code{Separate_Numeric_Error_Handlers} rule (for @command{gnatcheck})
21826
21827 @noindent
21828 Flags each exception handler that contains a choice for
21829 the predefined @code{Constraint_Error} exception, but does not contain
21830 the choice for the predefined @code{Numeric_Error} exception, or
21831 that contains the choice for @code{Numeric_Error}, but does not contain the
21832 choice for @code{Constraint_Error}.
21833
21834 This rule has no parameters.
21835
21836 @ignore
21837 @node Recursion
21838 @subsection @code{Recursion} (under construction, GLOBAL)
21839 @cindex @code{Recursion} rule (for @command{gnatcheck})
21840
21841 @noindent
21842 Flag recursive subprograms (cycles in the call graph). Declarations, and not
21843 calls, of recursive subprograms are detected.
21844
21845 This rule has no parameters.
21846 @end ignore
21847
21848 @ignore
21849 @node Side_Effect_Functions
21850 @subsection @code{Side_Effect_Functions} (under construction, GLOBAL)
21851 @cindex @code{Side_Effect_Functions} rule (for @command{gnatcheck})
21852
21853 @noindent
21854 Flag functions with side effects.
21855
21856 We define a side effect as changing any data object that is not local for the
21857 body of this function.
21858
21859 At the moment, we do NOT consider a side effect any input-output operations
21860 (changing a state or a content of any file).
21861
21862 We do not consider protected functions for this rule (???)
21863
21864 There are the following sources of side effect:
21865
21866 @enumerate
21867 @item Explicit (or direct) side-effect:
21868
21869 @itemize @bullet
21870 @item
21871 direct assignment to a non-local variable;
21872
21873 @item
21874 direct call to an entity that is known to change some data object that is
21875 not local for the body of this function (Note, that if F1 calls F2 and F2
21876 does have a side effect, this does not automatically mean that F1 also
21877 have a side effect, because it may be the case that F2 is declared in
21878 F1's body and it changes some data object that is global for F2, but
21879 local for F1);
21880 @end itemize
21881
21882 @item Indirect side-effect:
21883 @itemize @bullet
21884 @item
21885 Subprogram calls implicitly issued by:
21886 @itemize @bullet
21887 @item
21888 computing initialization expressions from type declarations as a part
21889 of object elaboration or allocator evaluation;
21890 @item
21891 computing implicit parameters of subprogram or entry calls or generic
21892 instantiations;
21893 @end itemize
21894
21895 @item
21896 activation of a task that change some non-local data object (directly or
21897 indirectly);
21898
21899 @item
21900 elaboration code of a package that is a result of a package instantiation;
21901
21902 @item
21903 controlled objects;
21904 @end itemize
21905
21906 @item Situations when we can suspect a side-effect, but the full static check
21907 is either impossible or too hard:
21908 @itemize @bullet
21909 @item
21910 assignment to access variables or to the objects pointed by access
21911 variables;
21912
21913 @item
21914 call to a subprogram pointed by access-to-subprogram value
21915
21916 @item
21917 dispatching calls;
21918 @end itemize
21919 @end enumerate
21920
21921 @noindent
21922 This rule has no parameters.
21923 @end ignore
21924
21925 @node Slices
21926 @subsection @code{Slices}
21927 @cindex @code{Slices} rule (for @command{gnatcheck})
21928
21929 @noindent
21930 Flag all uses of array slicing
21931
21932 This rule has no parameters.
21933
21934
21935 @node Unassigned_OUT_Parameters
21936 @subsection @code{Unassigned_OUT_Parameters}
21937 @cindex @code{Unassigned_OUT_Parameters} rule (for @command{gnatcheck})
21938
21939 @noindent
21940 Flags procedures' @code{out} parameters that are not assigned, and
21941 identifies the contexts in which the assignments are missing.
21942
21943 An @code{out} parameter is flagged in the statements in the procedure
21944 body's handled sequence of statements (before the procedure body's
21945 @code{exception} part, if any) if this sequence of statements contains
21946 no assignments to the parameter.
21947
21948 An @code{out} parameter is flagged in an exception handler in the exception
21949 part of the procedure body's handled sequence of statements if the handler
21950 contains no assignment to the parameter.
21951
21952 Bodies of generic procedures are also considered.
21953
21954 The following are treated as assignments to an @code{out} parameter:
21955
21956 @itemize @bullet
21957 @item
21958 an assignment statement, with the parameter or some component as the target;
21959
21960 @item
21961 passing the parameter (or one of its components) as an @code{out} or
21962 @code{in out} parameter.
21963 @end itemize
21964
21965 @noindent
21966 This rule does not have any parameters.
21967
21968
21969
21970 @node Uncommented_BEGIN_In_Package_Bodies
21971 @subsection @code{Uncommented_BEGIN_In_Package_Bodies}
21972 @cindex @code{Uncommented_BEGIN_In_Package_Bodies} rule (for @command{gnatcheck})
21973
21974 @noindent
21975 Flags each package body with declarations and a statement part that does not
21976 include a trailing comment on the line containing the @code{begin} keyword;
21977 this trailing comment needs to specify the package name and nothing else.
21978 The @code{begin} is not flagged if the package body does not
21979 contain any declarations.
21980
21981 If the @code{begin} keyword is placed on the
21982 same line as the last declaration or the first statement, it is flagged
21983 independently of whether the line contains a trailing comment. The
21984 diagnostic message is attached to the line containing the first statement.
21985
21986 This rule has no parameters.
21987
21988
21989 @node Unconstrained_Array_Returns
21990 @subsection @code{Unconstrained_Array_Returns}
21991 @cindex @code{Unconstrained_Array_Returns} rule (for @command{gnatcheck})
21992
21993 @noindent
21994 Flag each function returning an unconstrained array. Function declarations,
21995 function bodies (and body stubs) having no separate specifications,
21996 and generic function instantiations are checked.
21997 Generic function declarations, function calls and function renamings are
21998 not checked.
21999
22000 This rule has no parameters.
22001
22002 @node Universal_Ranges
22003 @subsection @code{Universal_Ranges}
22004 @cindex @code{Universal_Ranges} rule (for @command{gnatcheck})
22005
22006 @noindent
22007 Flag discrete ranges that are a part of an index constraint, constrained
22008 array definition, or @code{for}-loop parameter specification, and whose bounds
22009 are both of type @i{universal_integer}. Ranges that have at least one
22010 bound of a specific type (such as @code{1 .. N}, where @code{N} is a variable
22011 or an expression of non-universal type) are not flagged.
22012
22013 This rule has no parameters.
22014
22015
22016 @node Unnamed_Blocks_And_Loops
22017 @subsection @code{Unnamed_Blocks_And_Loops}
22018 @cindex @code{Unnamed_Blocks_And_Loops} rule (for @command{gnatcheck})
22019
22020 @noindent
22021 Flag each unnamed block statement and loop statement.
22022
22023 The rule has no parameters.
22024
22025
22026
22027 @ignore
22028 @node Unused_Subprograms
22029 @subsection @code{Unused_Subprograms} (under construction, GLOBAL)
22030 @cindex @code{Unused_Subprograms} rule (for @command{gnatcheck})
22031
22032 @noindent
22033 Flag all unused subprograms.
22034
22035 This rule has no parameters.
22036 @end ignore
22037
22038
22039
22040
22041 @node USE_PACKAGE_Clauses
22042 @subsection @code{USE_PACKAGE_Clauses}
22043 @cindex @code{USE_PACKAGE_Clauses} rule (for @command{gnatcheck})
22044
22045 @noindent
22046 Flag all @code{use} clauses for packages; @code{use type} clauses are
22047 not flagged.
22048
22049 This rule has no parameters.
22050
22051
22052
22053 @node Volatile_Objects_Without_Address_Clauses
22054 @subsection @code{Volatile_Objects_Without_Address_Clauses}
22055 @cindex @code{Volatile_Objects_Without_Address_Clauses} rule (for @command{gnatcheck})
22056
22057 @noindent
22058 Flag each volatile object that does not have an address clause.
22059
22060 The following check is made: if the pragma @code{Volatile} is applied to a
22061 data object or to its type, then an address clause must
22062 be supplied for this object.
22063
22064 This rule does not check the components of data objects,
22065 array components that are volatile as a result of the pragma
22066 @code{Volatile_Components}, or objects that are volatile because
22067 they are atomic as a result of pragmas @code{Atomic} or
22068 @code{Atomic_Components}.
22069
22070 Only variable declarations, and not constant declarations, are checked.
22071
22072 This rule has no parameters.
22073
22074
22075 @c *********************************
22076 @node Creating Sample Bodies Using gnatstub
22077 @chapter Creating Sample Bodies Using @command{gnatstub}
22078 @findex gnatstub
22079
22080 @noindent
22081 @command{gnatstub} creates body stubs, that is, empty but compilable bodies
22082 for library unit declarations.
22083
22084 Note: to invoke @code{gnatstub} with a project file, use the @code{gnat}
22085 driver (see @ref{The GNAT Driver and Project Files}).
22086
22087 To create a body stub, @command{gnatstub} has to compile the library
22088 unit declaration. Therefore, bodies can be created only for legal
22089 library units. Moreover, if a library unit depends semantically upon
22090 units located outside the current directory, you have to provide
22091 the source search path when calling @command{gnatstub}, see the description
22092 of @command{gnatstub} switches below.
22093
22094 By default, all the program unit body stubs generated by @code{gnatstub}
22095 raise the predefined @code{Program_Error} exception, which will catch
22096 accidental calls of generated stubs. This behavior can be changed with
22097 option @option{^--no-exception^/NO_EXCEPTION^} (see below).
22098
22099 @menu
22100 * Running gnatstub::
22101 * Switches for gnatstub::
22102 @end menu
22103
22104 @node Running gnatstub
22105 @section Running @command{gnatstub}
22106
22107 @noindent
22108 @command{gnatstub} has the command-line interface of the form
22109
22110 @smallexample
22111 $ gnatstub @ovar{switches} @var{filename} @ovar{directory}
22112 @end smallexample
22113
22114 @noindent
22115 where
22116 @table @var
22117 @item filename
22118 is the name of the source file that contains a library unit declaration
22119 for which a body must be created. The file name may contain the path
22120 information.
22121 The file name does not have to follow the GNAT file name conventions. If the
22122 name
22123 does not follow GNAT file naming conventions, the name of the body file must
22124 be provided
22125 explicitly as the value of the @option{^-o^/BODY=^@var{body-name}} option.
22126 If the file name follows the GNAT file naming
22127 conventions and the name of the body file is not provided,
22128 @command{gnatstub}
22129 creates the name
22130 of the body file from the argument file name by replacing the @file{.ads}
22131 suffix
22132 with the @file{.adb} suffix.
22133
22134 @item directory
22135 indicates the directory in which the body stub is to be placed (the default
22136 is the
22137 current directory)
22138
22139 @item switches
22140 is an optional sequence of switches as described in the next section
22141 @end table
22142
22143 @node Switches for gnatstub
22144 @section Switches for @command{gnatstub}
22145
22146 @table @option
22147 @c !sort!
22148
22149 @item ^-f^/FULL^
22150 @cindex @option{^-f^/FULL^} (@command{gnatstub})
22151 If the destination directory already contains a file with the name of the
22152 body file
22153 for the argument spec file, replace it with the generated body stub.
22154
22155 @item ^-hs^/HEADER=SPEC^
22156 @cindex @option{^-hs^/HEADER=SPEC^} (@command{gnatstub})
22157 Put the comment header (i.e., all the comments preceding the
22158 compilation unit) from the source of the library unit declaration
22159 into the body stub.
22160
22161 @item ^-hg^/HEADER=GENERAL^
22162 @cindex @option{^-hg^/HEADER=GENERAL^} (@command{gnatstub})
22163 Put a sample comment header into the body stub.
22164
22165 @item ^--header-file=@var{filename}^/FROM_HEADER_FILE=@var{filename}^
22166 @cindex @option{^--header-file^/FROM_HEADER_FILE=^} (@command{gnatstub})
22167 Use the content of the file as the comment header for a generated body stub.
22168
22169 @ifclear vms
22170 @item -IDIR
22171 @cindex @option{-IDIR} (@command{gnatstub})
22172 @itemx -I-
22173 @cindex @option{-I-} (@command{gnatstub})
22174 @end ifclear
22175 @ifset vms
22176 @item /NOCURRENT_DIRECTORY
22177 @cindex @option{/NOCURRENT_DIRECTORY} (@command{gnatstub})
22178 @end ifset
22179 ^These switches have ^This switch has^ the same meaning as in calls to
22180 @command{gcc}.
22181 ^They define ^It defines ^ the source search path in the call to
22182 @command{gcc} issued
22183 by @command{gnatstub} to compile an argument source file.
22184
22185 @item ^-gnatec^/CONFIGURATION_PRAGMAS_FILE=^@var{PATH}
22186 @cindex @option{^-gnatec^/CONFIGURATION_PRAGMAS_FILE^} (@command{gnatstub})
22187 This switch has the same meaning as in calls to @command{gcc}.
22188 It defines the additional configuration file to be passed to the call to
22189 @command{gcc} issued
22190 by @command{gnatstub} to compile an argument source file.
22191
22192 @item ^-gnatyM^/MAX_LINE_LENGTH=^@var{n}
22193 @cindex @option{^-gnatyM^/MAX_LINE_LENGTH^} (@command{gnatstub})
22194 (@var{n} is a non-negative integer). Set the maximum line length in the
22195 body stub to @var{n}; the default is 79. The maximum value that can be
22196 specified is 32767. Note that in the special case of configuration
22197 pragma files, the maximum is always 32767 regardless of whether or
22198 not this switch appears.
22199
22200 @item ^-gnaty^/STYLE_CHECKS=^@var{n}
22201 @cindex @option{^-gnaty^/STYLE_CHECKS=^} (@command{gnatstub})
22202 (@var{n} is a non-negative integer from 1 to 9). Set the indentation level in
22203 the generated body sample to @var{n}.
22204 The default indentation is 3.
22205
22206 @item ^-gnatyo^/ORDERED_SUBPROGRAMS^
22207 @cindex @option{^-gnato^/ORDERED_SUBPROGRAMS^} (@command{gnatstub})
22208 Order local bodies alphabetically. (By default local bodies are ordered
22209 in the same way as the corresponding local specs in the argument spec file.)
22210
22211 @item ^-i^/INDENTATION=^@var{n}
22212 @cindex @option{^-i^/INDENTATION^} (@command{gnatstub})
22213 Same as @option{^-gnaty^/STYLE_CHECKS=^@var{n}}
22214
22215 @item ^-k^/TREE_FILE=SAVE^
22216 @cindex @option{^-k^/TREE_FILE=SAVE^} (@command{gnatstub})
22217 Do not remove the tree file (i.e., the snapshot of the compiler internal
22218 structures used by @command{gnatstub}) after creating the body stub.
22219
22220 @item ^-l^/LINE_LENGTH=^@var{n}
22221 @cindex @option{^-l^/LINE_LENGTH^} (@command{gnatstub})
22222 Same as @option{^-gnatyM^/MAX_LINE_LENGTH=^@var{n}}
22223
22224 @item ^--no-exception^/NO_EXCEPTION^
22225 @cindex @option{^--no-exception^/NO_EXCEPTION^} (@command{gnatstub})
22226 Avoind raising PROGRAM_ERROR in the generated bodies of program unit stubs.
22227 This is not always possible for function stubs.
22228
22229 @item ^-o ^/BODY=^@var{body-name}
22230 @cindex @option{^-o^/BODY^} (@command{gnatstub})
22231 Body file name. This should be set if the argument file name does not
22232 follow
22233 the GNAT file naming
22234 conventions. If this switch is omitted the default name for the body will be
22235 obtained
22236 from the argument file name according to the GNAT file naming conventions.
22237
22238 @item ^-q^/QUIET^
22239 @cindex @option{^-q^/QUIET^} (@command{gnatstub})
22240 Quiet mode: do not generate a confirmation when a body is
22241 successfully created, and do not generate a message when a body is not
22242 required for an
22243 argument unit.
22244
22245 @item ^-r^/TREE_FILE=REUSE^
22246 @cindex @option{^-r^/TREE_FILE=REUSE^} (@command{gnatstub})
22247 Reuse the tree file (if it exists) instead of creating it. Instead of
22248 creating the tree file for the library unit declaration, @command{gnatstub}
22249 tries to find it in the current directory and use it for creating
22250 a body. If the tree file is not found, no body is created. This option
22251 also implies @option{^-k^/SAVE^}, whether or not
22252 the latter is set explicitly.
22253
22254 @item ^-t^/TREE_FILE=OVERWRITE^
22255 @cindex @option{^-t^/TREE_FILE=OVERWRITE^} (@command{gnatstub})
22256 Overwrite the existing tree file. If the current directory already
22257 contains the file which, according to the GNAT file naming rules should
22258 be considered as a tree file for the argument source file,
22259 @command{gnatstub}
22260 will refuse to create the tree file needed to create a sample body
22261 unless this option is set.
22262
22263 @item ^-v^/VERBOSE^
22264 @cindex @option{^-v^/VERBOSE^} (@command{gnatstub})
22265 Verbose mode: generate version information.
22266
22267 @end table
22268
22269 @c *********************************
22270 @node Generating Ada Bindings for C and C++ headers
22271 @chapter Generating Ada Bindings for C and C++ headers
22272 @findex binding
22273
22274 @noindent
22275 GNAT now comes with a new experimental binding generator for C and C++
22276 headers which is intended to do 95% of the tedious work of generating
22277 Ada specs from C or C++ header files. Note that this still is a work in
22278 progress, not designed to generate 100% correct Ada specs.
22279
22280 The code generated is using the Ada 2005 syntax, which makes it
22281 easier to interface with other languages than previous versions of Ada.
22282
22283 @menu
22284 * Running the binding generator::
22285 * Generating bindings for C++ headers::
22286 * Switches::
22287 @end menu
22288
22289 @node Running the binding generator
22290 @section Running the binding generator
22291
22292 @noindent
22293 The binding generator is part of the @command{gcc} compiler and can be
22294 invoked via the @option{-fdump-ada-spec} switch, which will generate Ada
22295 spec files for the header files specified on the command line, and all
22296 header files needed by these files transitivitely. For example:
22297
22298 @smallexample
22299 $ g++ -c -fdump-ada-spec -C /usr/include/time.h
22300 $ gcc -c -gnat05 *.ads
22301 @end smallexample
22302
22303 will generate, under GNU/Linux, the following files: @file{time_h.ads},
22304 @file{bits_time_h.ads}, @file{stddef_h.ads}, @file{bits_types_h.ads} which
22305 correspond to the files @file{/usr/include/time.h},
22306 @file{/usr/include/bits/time.h}, etc@dots{}, and will then compile in Ada 2005
22307 mode these Ada specs.
22308
22309 The @code{-C} switch tells @command{gcc} to extract comments from headers,
22310 and will attempt to generate corresponding Ada comments.
22311
22312 If you want to generate a single Ada file and not the transitive closure, you
22313 can use instead the @option{-fdump-ada-spec-slim} switch.
22314
22315 Note that we recommend when possible to use the @command{g++} driver to
22316 generate bindings, even for most C headers, since this will in general
22317 generate better Ada specs. For generating bindings for C++ headers, it is
22318 mandatory to use the @command{g++} command, or @command{gcc -x c++} which
22319 is equivalent in this case. If @command{g++} cannot work on your C headers
22320 because of incompatibilities between C and C++, then you can fallback to
22321 @command{gcc} instead.
22322
22323 For an example of better bindings generated from the C++ front-end,
22324 the name of the parameters (when available) are actually ignored by the C
22325 front-end. Consider the following C header:
22326
22327 @smallexample
22328 extern void foo (int variable);
22329 @end smallexample
22330
22331 with the C front-end, @code{variable} is ignored, and the above is handled as:
22332
22333 @smallexample
22334 extern void foo (int);
22335 @end smallexample
22336
22337 generating a generic:
22338
22339 @smallexample
22340 procedure foo (param1 : int);
22341 @end smallexample
22342
22343 with the C++ front-end, the name is available, and we generate:
22344
22345 @smallexample
22346 procedure foo (variable : int);
22347 @end smallexample
22348
22349 In some cases, the generated bindings will be more complete or more meaningful
22350 when defining some macros, which you can do via the @option{-D} switch. This
22351 is for example the case with @file{Xlib.h} under GNU/Linux:
22352
22353 @smallexample
22354 g++ -c -fdump-ada-spec -DXLIB_ILLEGAL_ACCESS -C /usr/include/X11/Xlib.h
22355 @end smallexample
22356
22357 The above will generate more complete bindings than a straight call without
22358 the @option{-DXLIB_ILLEGAL_ACCESS} switch.
22359
22360 In other cases, it is not possible to parse a header file in a stand alone
22361 manner, because other include files need to be included first. In this
22362 case, the solution is to create a small header file including the needed
22363 @code{#include} and possible @code{#define} directives. For example, to
22364 generate Ada bindings for @file{readline/readline.h}, you need to first
22365 include @file{stdio.h}, so you can create a file with the following two
22366 lines in e.g. @file{readline1.h}:
22367
22368 @smallexample
22369 #include <stdio.h>
22370 #include <readline/readline.h>
22371 @end smallexample
22372
22373 and then generate Ada bindings from this file:
22374
22375 @smallexample
22376 $ g++ -c -fdump-ada-spec readline1.h
22377 @end smallexample
22378
22379 @node Generating bindings for C++ headers
22380 @section Generating bindings for C++ headers
22381
22382 @noindent
22383 Generating bindings for C++ headers is done using the same options, always
22384 with the @command{g++} compiler.
22385
22386 In this mode, C++ classes will be mapped to Ada tagged types, constructors
22387 will be mapped using the @code{CPP_Constructor} pragma, and when possible,
22388 multiple inheritance of abstract classes will be mapped to Ada interfaces
22389 (@xref{Interfacing to C++,,,gnat_rm, GNAT Reference Manual}, for additional
22390 information on interfacing to C++).
22391
22392 For example, given the following C++ header file:
22393
22394 @smallexample
22395 @group
22396 @cartouche
22397 class Carnivore @{
22398 public:
22399 virtual int Number_Of_Teeth () = 0;
22400 @};
22401
22402 class Domestic @{
22403 public:
22404 virtual void Set_Owner (char* Name) = 0;
22405 @};
22406
22407 class Animal @{
22408 public:
22409 int Age_Count;
22410 virtual void Set_Age (int New_Age);
22411 @};
22412
22413 class Dog : Animal, Carnivore, Domestic @{
22414 public:
22415 int Tooth_Count;
22416 char *Owner;
22417
22418 virtual int Number_Of_Teeth ();
22419 virtual void Set_Owner (char* Name);
22420
22421 Dog();
22422 @};
22423 @end cartouche
22424 @end group
22425 @end smallexample
22426
22427 The corresponding Ada code is generated:
22428
22429 @smallexample @c ada
22430 @group
22431 @cartouche
22432 package Class_Carnivore is
22433 type Carnivore is limited interface;
22434 pragma Import (CPP, Carnivore);
22435
22436 function Number_Of_Teeth (this : access Carnivore) return int is abstract;
22437 end;
22438 use Class_Carnivore;
22439
22440 package Class_Domestic is
22441 type Domestic is limited interface;
22442 pragma Import (CPP, Domestic);
22443
22444 procedure Set_Owner
22445 (this : access Domestic;
22446 Name : Interfaces.C.Strings.chars_ptr) is abstract;
22447 end;
22448 use Class_Domestic;
22449
22450 package Class_Animal is
22451 type Animal is tagged limited record
22452 Age_Count : aliased int;
22453 end record;
22454 pragma Import (CPP, Animal);
22455
22456 procedure Set_Age (this : access Animal; New_Age : int);
22457 pragma Import (CPP, Set_Age, "_ZN6Animal7Set_AgeEi");
22458 end;
22459 use Class_Animal;
22460
22461 package Class_Dog is
22462 type Dog is new Animal and Carnivore and Domestic with record
22463 Tooth_Count : aliased int;
22464 Owner : Interfaces.C.Strings.chars_ptr;
22465 end record;
22466 pragma Import (CPP, Dog);
22467
22468 function Number_Of_Teeth (this : access Dog) return int;
22469 pragma Import (CPP, Number_Of_Teeth, "_ZN3Dog15Number_Of_TeethEv");
22470
22471 procedure Set_Owner
22472 (this : access Dog; Name : Interfaces.C.Strings.chars_ptr);
22473 pragma Import (CPP, Set_Owner, "_ZN3Dog9Set_OwnerEPc");
22474
22475 function New_Dog return Dog'Class;
22476 pragma CPP_Constructor (New_Dog);
22477 pragma Import (CPP, New_Dog, "_ZN3DogC1Ev");
22478 end;
22479 use Class_Dog;
22480 @end cartouche
22481 @end group
22482 @end smallexample
22483
22484 @node Switches
22485 @section Switches
22486
22487 @table @option
22488 @item -fdump-ada-spec
22489 @cindex @option{-fdump-ada-spec} (@command{gcc})
22490 Generate Ada spec files for the given header files transitively (including
22491 all header files that these headers depend upon).
22492
22493 @item -fdump-ada-spec-slim
22494 @cindex @option{-fdump-ada-spec-slim} (@command{gcc})
22495 Generate Ada spec files for the header files specified on the command line
22496 only.
22497
22498 @item -C
22499 @cindex @option{-C} (@command{gcc})
22500 Extract comments from headers and generate Ada comments in the Ada spec files.
22501 @end table
22502
22503 @node Other Utility Programs
22504 @chapter Other Utility Programs
22505
22506 @noindent
22507 This chapter discusses some other utility programs available in the Ada
22508 environment.
22509
22510 @menu
22511 * Using Other Utility Programs with GNAT::
22512 * The External Symbol Naming Scheme of GNAT::
22513 * Converting Ada Files to html with gnathtml::
22514 * Installing gnathtml::
22515 @ifset vms
22516 * LSE::
22517 * Profiling::
22518 @end ifset
22519 @end menu
22520
22521 @node Using Other Utility Programs with GNAT
22522 @section Using Other Utility Programs with GNAT
22523
22524 @noindent
22525 The object files generated by GNAT are in standard system format and in
22526 particular the debugging information uses this format. This means
22527 programs generated by GNAT can be used with existing utilities that
22528 depend on these formats.
22529
22530 @ifclear vms
22531 In general, any utility program that works with C will also often work with
22532 Ada programs generated by GNAT. This includes software utilities such as
22533 gprof (a profiling program), @code{gdb} (the FSF debugger), and utilities such
22534 as Purify.
22535 @end ifclear
22536
22537 @node The External Symbol Naming Scheme of GNAT
22538 @section The External Symbol Naming Scheme of GNAT
22539
22540 @noindent
22541 In order to interpret the output from GNAT, when using tools that are
22542 originally intended for use with other languages, it is useful to
22543 understand the conventions used to generate link names from the Ada
22544 entity names.
22545
22546 All link names are in all lowercase letters. With the exception of library
22547 procedure names, the mechanism used is simply to use the full expanded
22548 Ada name with dots replaced by double underscores. For example, suppose
22549 we have the following package spec:
22550
22551 @smallexample @c ada
22552 @group
22553 @cartouche
22554 package QRS is
22555 MN : Integer;
22556 end QRS;
22557 @end cartouche
22558 @end group
22559 @end smallexample
22560
22561 @noindent
22562 The variable @code{MN} has a full expanded Ada name of @code{QRS.MN}, so
22563 the corresponding link name is @code{qrs__mn}.
22564 @findex Export
22565 Of course if a @code{pragma Export} is used this may be overridden:
22566
22567 @smallexample @c ada
22568 @group
22569 @cartouche
22570 package Exports is
22571 Var1 : Integer;
22572 pragma Export (Var1, C, External_Name => "var1_name");
22573 Var2 : Integer;
22574 pragma Export (Var2, C, Link_Name => "var2_link_name");
22575 end Exports;
22576 @end cartouche
22577 @end group
22578 @end smallexample
22579
22580 @noindent
22581 In this case, the link name for @var{Var1} is whatever link name the
22582 C compiler would assign for the C function @var{var1_name}. This typically
22583 would be either @var{var1_name} or @var{_var1_name}, depending on operating
22584 system conventions, but other possibilities exist. The link name for
22585 @var{Var2} is @var{var2_link_name}, and this is not operating system
22586 dependent.
22587
22588 @findex _main
22589 One exception occurs for library level procedures. A potential ambiguity
22590 arises between the required name @code{_main} for the C main program,
22591 and the name we would otherwise assign to an Ada library level procedure
22592 called @code{Main} (which might well not be the main program).
22593
22594 To avoid this ambiguity, we attach the prefix @code{_ada_} to such
22595 names. So if we have a library level procedure such as
22596
22597 @smallexample @c ada
22598 @group
22599 @cartouche
22600 procedure Hello (S : String);
22601 @end cartouche
22602 @end group
22603 @end smallexample
22604
22605 @noindent
22606 the external name of this procedure will be @var{_ada_hello}.
22607
22608
22609 @node Converting Ada Files to html with gnathtml
22610 @section Converting Ada Files to HTML with @code{gnathtml}
22611
22612 @noindent
22613 This @code{Perl} script allows Ada source files to be browsed using
22614 standard Web browsers. For installation procedure, see the section
22615 @xref{Installing gnathtml}.
22616
22617 Ada reserved keywords are highlighted in a bold font and Ada comments in
22618 a blue font. Unless your program was compiled with the gcc @option{-gnatx}
22619 switch to suppress the generation of cross-referencing information, user
22620 defined variables and types will appear in a different color; you will
22621 be able to click on any identifier and go to its declaration.
22622
22623 The command line is as follow:
22624 @smallexample
22625 $ perl gnathtml.pl @ovar{^switches^options^} @var{ada-files}
22626 @end smallexample
22627
22628 @noindent
22629 You can pass it as many Ada files as you want. @code{gnathtml} will generate
22630 an html file for every ada file, and a global file called @file{index.htm}.
22631 This file is an index of every identifier defined in the files.
22632
22633 The available ^switches^options^ are the following ones:
22634
22635 @table @option
22636 @item -83
22637 @cindex @option{-83} (@code{gnathtml})
22638 Only the Ada 83 subset of keywords will be highlighted.
22639
22640 @item -cc @var{color}
22641 @cindex @option{-cc} (@code{gnathtml})
22642 This option allows you to change the color used for comments. The default
22643 value is green. The color argument can be any name accepted by html.
22644
22645 @item -d
22646 @cindex @option{-d} (@code{gnathtml})
22647 If the Ada files depend on some other files (for instance through
22648 @code{with} clauses, the latter files will also be converted to html.
22649 Only the files in the user project will be converted to html, not the files
22650 in the run-time library itself.
22651
22652 @item -D
22653 @cindex @option{-D} (@code{gnathtml})
22654 This command is the same as @option{-d} above, but @command{gnathtml} will
22655 also look for files in the run-time library, and generate html files for them.
22656
22657 @item -ext @var{extension}
22658 @cindex @option{-ext} (@code{gnathtml})
22659 This option allows you to change the extension of the generated HTML files.
22660 If you do not specify an extension, it will default to @file{htm}.
22661
22662 @item -f
22663 @cindex @option{-f} (@code{gnathtml})
22664 By default, gnathtml will generate html links only for global entities
22665 ('with'ed units, global variables and types,@dots{}). If you specify
22666 @option{-f} on the command line, then links will be generated for local
22667 entities too.
22668
22669 @item -l @var{number}
22670 @cindex @option{-l} (@code{gnathtml})
22671 If this ^switch^option^ is provided and @var{number} is not 0, then
22672 @code{gnathtml} will number the html files every @var{number} line.
22673
22674 @item -I @var{dir}
22675 @cindex @option{-I} (@code{gnathtml})
22676 Specify a directory to search for library files (@file{.ALI} files) and
22677 source files. You can provide several -I switches on the command line,
22678 and the directories will be parsed in the order of the command line.
22679
22680 @item -o @var{dir}
22681 @cindex @option{-o} (@code{gnathtml})
22682 Specify the output directory for html files. By default, gnathtml will
22683 saved the generated html files in a subdirectory named @file{html/}.
22684
22685 @item -p @var{file}
22686 @cindex @option{-p} (@code{gnathtml})
22687 If you are using Emacs and the most recent Emacs Ada mode, which provides
22688 a full Integrated Development Environment for compiling, checking,
22689 running and debugging applications, you may use @file{.gpr} files
22690 to give the directories where Emacs can find sources and object files.
22691
22692 Using this ^switch^option^, you can tell gnathtml to use these files.
22693 This allows you to get an html version of your application, even if it
22694 is spread over multiple directories.
22695
22696 @item -sc @var{color}
22697 @cindex @option{-sc} (@code{gnathtml})
22698 This ^switch^option^ allows you to change the color used for symbol
22699 definitions.
22700 The default value is red. The color argument can be any name accepted by html.
22701
22702 @item -t @var{file}
22703 @cindex @option{-t} (@code{gnathtml})
22704 This ^switch^option^ provides the name of a file. This file contains a list of
22705 file names to be converted, and the effect is exactly as though they had
22706 appeared explicitly on the command line. This
22707 is the recommended way to work around the command line length limit on some
22708 systems.
22709
22710 @end table
22711
22712 @node Installing gnathtml
22713 @section Installing @code{gnathtml}
22714
22715 @noindent
22716 @code{Perl} needs to be installed on your machine to run this script.
22717 @code{Perl} is freely available for almost every architecture and
22718 Operating System via the Internet.
22719
22720 On Unix systems, you may want to modify the first line of the script
22721 @code{gnathtml}, to explicitly tell the Operating system where Perl
22722 is. The syntax of this line is:
22723 @smallexample
22724 #!full_path_name_to_perl
22725 @end smallexample
22726
22727 @noindent
22728 Alternatively, you may run the script using the following command line:
22729
22730 @smallexample
22731 $ perl gnathtml.pl @ovar{switches} @var{files}
22732 @end smallexample
22733
22734 @ifset vms
22735 @node LSE
22736 @section LSE
22737 @findex LSE
22738
22739 @noindent
22740 The GNAT distribution provides an Ada 95 template for the HP Language
22741 Sensitive Editor (LSE), a component of DECset. In order to
22742 access it, invoke LSE with the qualifier /ENVIRONMENT=GNU:[LIB]ADA95.ENV.
22743
22744 @node Profiling
22745 @section Profiling
22746 @findex PCA
22747
22748 @noindent
22749 GNAT supports The HP Performance Coverage Analyzer (PCA), a component
22750 of DECset. To use it proceed as outlined under ``HELP PCA'', except for running
22751 the collection phase with the /DEBUG qualifier.
22752
22753 @smallexample
22754 $ GNAT MAKE /DEBUG <PROGRAM_NAME>
22755 $ DEFINE LIB$DEBUG PCA$COLLECTOR
22756 $ RUN/DEBUG <PROGRAM_NAME>
22757 @end smallexample
22758 @noindent
22759 @end ifset
22760
22761 @ifclear vms
22762 @c ******************************
22763 @node Code Coverage and Profiling
22764 @chapter Code Coverage and Profiling
22765 @cindex Code Coverage
22766 @cindex Profiling
22767
22768 @noindent
22769 This chapter describes how to use @code{gcov} - coverage testing tool - and
22770 @code{gprof} - profiler tool - on your Ada programs.
22771
22772 @menu
22773 * Code Coverage of Ada Programs using gcov::
22774 * Profiling an Ada Program using gprof::
22775 @end menu
22776
22777 @node Code Coverage of Ada Programs using gcov
22778 @section Code Coverage of Ada Programs using gcov
22779 @cindex gcov
22780 @cindex -fprofile-arcs
22781 @cindex -ftest-coverage
22782 @cindex -coverage
22783 @cindex Code Coverage
22784
22785 @noindent
22786 @code{gcov} is a test coverage program: it analyzes the execution of a given
22787 program on selected tests, to help you determine the portions of the program
22788 that are still untested.
22789
22790 @code{gcov} is part of the GCC suite, and is described in detail in the GCC
22791 User's Guide. You can refer to this documentation for a more complete
22792 description.
22793
22794 This chapter provides a quick startup guide, and
22795 details some Gnat-specific features.
22796
22797 @menu
22798 * Quick startup guide::
22799 * Gnat specifics::
22800 @end menu
22801
22802 @node Quick startup guide
22803 @subsection Quick startup guide
22804
22805 In order to perform coverage analysis of a program using @code{gcov}, 3
22806 steps are needed:
22807
22808 @itemize @bullet
22809 @item
22810 Code instrumentation during the compilation process
22811 @item
22812 Execution of the instrumented program
22813 @item
22814 Execution of the @code{gcov} tool to generate the result.
22815 @end itemize
22816
22817 The code instrumentation needed by gcov is created at the object level:
22818 The source code is not modified in any way, because the instrumentation code is
22819 inserted by gcc during the compilation process. To compile your code with code
22820 coverage activated, you need to recompile your whole project using the
22821 switches
22822 @code{-fprofile-arcs} and @code{-ftest-coverage}, and link it using
22823 @code{-fprofile-arcs}.
22824
22825 @smallexample
22826 $ gnatmake -P my_project.gpr -f -cargs -fprofile-arcs -ftest-coverage \
22827 -largs -fprofile-arcs
22828 @end smallexample
22829
22830 This compilation process will create @file{.gcno} files together with
22831 the usual object files.
22832
22833 Once the program is compiled with coverage instrumentation, you can
22834 run it as many times as needed - on portions of a test suite for
22835 example. The first execution will produce @file{.gcda} files at the
22836 same location as the @file{.gcno} files. The following executions
22837 will update those files, so that a cumulative result of the covered
22838 portions of the program is generated.
22839
22840 Finally, you need to call the @code{gcov} tool. The different options of
22841 @code{gcov} are available in the GCC User's Guide, section 'Invoking gcov'.
22842
22843 This will create annotated source files with a @file{.gcov} extension:
22844 @file{my_main.adb} file will be analysed in @file{my_main.adb.gcov}.
22845
22846 @node Gnat specifics
22847 @subsection Gnat specifics
22848
22849 Because Ada semantics, portions of the source code may be shared among
22850 several object files. This is the case for example when generics are
22851 involved, when inlining is active or when declarations generate initialisation
22852 calls. In order to take
22853 into account this shared code, you need to call @code{gcov} on all
22854 source files of the tested program at once.
22855
22856 The list of source files might exceed the system's maximum command line
22857 length. In order to bypass this limitation, a new mechanism has been
22858 implemented in @code{gcov}: you can now list all your project's files into a
22859 text file, and provide this file to gcov as a parameter, preceded by a @@
22860 (e.g. @samp{gcov @@mysrclist.txt}).
22861
22862 Note that on AIX compiling a static library with @code{-fprofile-arcs} is
22863 not supported as there can be unresolved symbols during the final link.
22864
22865 @node Profiling an Ada Program using gprof
22866 @section Profiling an Ada Program using gprof
22867 @cindex gprof
22868 @cindex -pg
22869 @cindex Profiling
22870
22871 @noindent
22872 This section is not meant to be an exhaustive documentation of @code{gprof}.
22873 Full documentation for it can be found in the GNU Profiler User's Guide
22874 documentation that is part of this GNAT distribution.
22875
22876 Profiling a program helps determine the parts of a program that are executed
22877 most often, and are therefore the most time-consuming.
22878
22879 @code{gprof} is the standard GNU profiling tool; it has been enhanced to
22880 better handle Ada programs and multitasking.
22881 It is currently supported on the following platforms
22882 @itemize @bullet
22883 @item
22884 linux x86/x86_64
22885 @item
22886 solaris sparc/sparc64/x86
22887 @item
22888 windows x86
22889 @end itemize
22890
22891 @noindent
22892 In order to profile a program using @code{gprof}, 3 steps are needed:
22893
22894 @itemize @bullet
22895 @item
22896 Code instrumentation, requiring a full recompilation of the project with the
22897 proper switches.
22898 @item
22899 Execution of the program under the analysis conditions, i.e. with the desired
22900 input.
22901 @item
22902 Analysis of the results using the @code{gprof} tool.
22903 @end itemize
22904
22905 @noindent
22906 The following sections detail the different steps, and indicate how
22907 to interpret the results:
22908 @menu
22909 * Compilation for profiling::
22910 * Program execution::
22911 * Running gprof::
22912 * Interpretation of profiling results::
22913 @end menu
22914
22915 @node Compilation for profiling
22916 @subsection Compilation for profiling
22917 @cindex -pg
22918 @cindex Profiling
22919
22920 In order to profile a program the first step is to tell the compiler
22921 to generate the necessary profiling information. The compiler switch to be used
22922 is @code{-pg}, which must be added to other compilation switches. This
22923 switch needs to be specified both during compilation and link stages, and can
22924 be specified once when using gnatmake:
22925
22926 @smallexample
22927 gnatmake -f -pg -P my_project
22928 @end smallexample
22929
22930 @noindent
22931 Note that only the objects that were compiled with the @samp{-pg} switch will be
22932 profiled; if you need to profile your whole project, use the
22933 @samp{-f} gnatmake switch to force full recompilation.
22934
22935 @node Program execution
22936 @subsection Program execution
22937
22938 @noindent
22939 Once the program has been compiled for profiling, you can run it as usual.
22940
22941 The only constraint imposed by profiling is that the program must terminate
22942 normally. An interrupted program (via a Ctrl-C, kill, etc.) will not be
22943 properly analyzed.
22944
22945 Once the program completes execution, a data file called @file{gmon.out} is
22946 generated in the directory where the program was launched from. If this file
22947 already exists, it will be overwritten.
22948
22949 @node Running gprof
22950 @subsection Running gprof
22951
22952 @noindent
22953 The @code{gprof} tool is called as follow:
22954
22955 @smallexample
22956 gprof my_prog gmon.out
22957 @end smallexample
22958
22959 @noindent
22960 or simpler:
22961
22962 @smallexample
22963 gprof my_prog
22964 @end smallexample
22965
22966 @noindent
22967 The complete form of the gprof command line is the following:
22968
22969 @smallexample
22970 gprof [^switches^options^] [executable [data-file]]
22971 @end smallexample
22972
22973 @noindent
22974 @code{gprof} supports numerous ^switch^options^. The order of these
22975 ^switch^options^ does not matter. The full list of options can be found in
22976 the GNU Profiler User's Guide documentation that comes with this documentation.
22977
22978 The following is the subset of those switches that is most relevant:
22979
22980 @table @option
22981
22982 @item --demangle[=@var{style}]
22983 @itemx --no-demangle
22984 @cindex @option{--demangle} (@code{gprof})
22985 These options control whether symbol names should be demangled when
22986 printing output. The default is to demangle C++ symbols. The
22987 @code{--no-demangle} option may be used to turn off demangling. Different
22988 compilers have different mangling styles. The optional demangling style
22989 argument can be used to choose an appropriate demangling style for your
22990 compiler, in particular Ada symbols generated by GNAT can be demangled using
22991 @code{--demangle=gnat}.
22992
22993 @item -e @var{function_name}
22994 @cindex @option{-e} (@code{gprof})
22995 The @samp{-e @var{function}} option tells @code{gprof} not to print
22996 information about the function @var{function_name} (and its
22997 children@dots{}) in the call graph. The function will still be listed
22998 as a child of any functions that call it, but its index number will be
22999 shown as @samp{[not printed]}. More than one @samp{-e} option may be
23000 given; only one @var{function_name} may be indicated with each @samp{-e}
23001 option.
23002
23003 @item -E @var{function_name}
23004 @cindex @option{-E} (@code{gprof})
23005 The @code{-E @var{function}} option works like the @code{-e} option, but
23006 execution time spent in the function (and children who were not called from
23007 anywhere else), will not be used to compute the percentages-of-time for
23008 the call graph. More than one @samp{-E} option may be given; only one
23009 @var{function_name} may be indicated with each @samp{-E} option.
23010
23011 @item -f @var{function_name}
23012 @cindex @option{-f} (@code{gprof})
23013 The @samp{-f @var{function}} option causes @code{gprof} to limit the
23014 call graph to the function @var{function_name} and its children (and
23015 their children@dots{}). More than one @samp{-f} option may be given;
23016 only one @var{function_name} may be indicated with each @samp{-f}
23017 option.
23018
23019 @item -F @var{function_name}
23020 @cindex @option{-F} (@code{gprof})
23021 The @samp{-F @var{function}} option works like the @code{-f} option, but
23022 only time spent in the function and its children (and their
23023 children@dots{}) will be used to determine total-time and
23024 percentages-of-time for the call graph. More than one @samp{-F} option
23025 may be given; only one @var{function_name} may be indicated with each
23026 @samp{-F} option. The @samp{-F} option overrides the @samp{-E} option.
23027
23028 @end table
23029
23030 @node Interpretation of profiling results
23031 @subsection Interpretation of profiling results
23032
23033 @noindent
23034
23035 The results of the profiling analysis are represented by two arrays: the
23036 'flat profile' and the 'call graph'. Full documentation of those outputs
23037 can be found in the GNU Profiler User's Guide.
23038
23039 The flat profile shows the time spent in each function of the program, and how
23040 many time it has been called. This allows you to locate easily the most
23041 time-consuming functions.
23042
23043 The call graph shows, for each subprogram, the subprograms that call it,
23044 and the subprograms that it calls. It also provides an estimate of the time
23045 spent in each of those callers/called subprograms.
23046 @end ifclear
23047
23048 @c ******************************
23049 @node Running and Debugging Ada Programs
23050 @chapter Running and Debugging Ada Programs
23051 @cindex Debugging
23052
23053 @noindent
23054 This chapter discusses how to debug Ada programs.
23055 @ifset vms
23056 It applies to GNAT on the Alpha OpenVMS platform;
23057 for I64 OpenVMS please refer to the @cite{OpenVMS Debugger Manual},
23058 since HP has implemented Ada support in the OpenVMS debugger on I64.
23059 @end ifset
23060
23061 An incorrect Ada program may be handled in three ways by the GNAT compiler:
23062
23063 @enumerate
23064 @item
23065 The illegality may be a violation of the static semantics of Ada. In
23066 that case GNAT diagnoses the constructs in the program that are illegal.
23067 It is then a straightforward matter for the user to modify those parts of
23068 the program.
23069
23070 @item
23071 The illegality may be a violation of the dynamic semantics of Ada. In
23072 that case the program compiles and executes, but may generate incorrect
23073 results, or may terminate abnormally with some exception.
23074
23075 @item
23076 When presented with a program that contains convoluted errors, GNAT
23077 itself may terminate abnormally without providing full diagnostics on
23078 the incorrect user program.
23079 @end enumerate
23080
23081 @menu
23082 * The GNAT Debugger GDB::
23083 * Running GDB::
23084 * Introduction to GDB Commands::
23085 * Using Ada Expressions::
23086 * Calling User-Defined Subprograms::
23087 * Using the Next Command in a Function::
23088 * Ada Exceptions::
23089 * Ada Tasks::
23090 * Debugging Generic Units::
23091 * GNAT Abnormal Termination or Failure to Terminate::
23092 * Naming Conventions for GNAT Source Files::
23093 * Getting Internal Debugging Information::
23094 * Stack Traceback::
23095 @end menu
23096
23097 @cindex Debugger
23098 @findex gdb
23099
23100 @node The GNAT Debugger GDB
23101 @section The GNAT Debugger GDB
23102
23103 @noindent
23104 @code{GDB} is a general purpose, platform-independent debugger that
23105 can be used to debug mixed-language programs compiled with @command{gcc},
23106 and in particular is capable of debugging Ada programs compiled with
23107 GNAT. The latest versions of @code{GDB} are Ada-aware and can handle
23108 complex Ada data structures.
23109
23110 @xref{Top,, Debugging with GDB, gdb, Debugging with GDB},
23111 @ifset vms
23112 located in the GNU:[DOCS] directory,
23113 @end ifset
23114 for full details on the usage of @code{GDB}, including a section on
23115 its usage on programs. This manual should be consulted for full
23116 details. The section that follows is a brief introduction to the
23117 philosophy and use of @code{GDB}.
23118
23119 When GNAT programs are compiled, the compiler optionally writes debugging
23120 information into the generated object file, including information on
23121 line numbers, and on declared types and variables. This information is
23122 separate from the generated code. It makes the object files considerably
23123 larger, but it does not add to the size of the actual executable that
23124 will be loaded into memory, and has no impact on run-time performance. The
23125 generation of debug information is triggered by the use of the
23126 ^-g^/DEBUG^ switch in the @command{gcc} or @command{gnatmake} command
23127 used to carry out the compilations. It is important to emphasize that
23128 the use of these options does not change the generated code.
23129
23130 The debugging information is written in standard system formats that
23131 are used by many tools, including debuggers and profilers. The format
23132 of the information is typically designed to describe C types and
23133 semantics, but GNAT implements a translation scheme which allows full
23134 details about Ada types and variables to be encoded into these
23135 standard C formats. Details of this encoding scheme may be found in
23136 the file exp_dbug.ads in the GNAT source distribution. However, the
23137 details of this encoding are, in general, of no interest to a user,
23138 since @code{GDB} automatically performs the necessary decoding.
23139
23140 When a program is bound and linked, the debugging information is
23141 collected from the object files, and stored in the executable image of
23142 the program. Again, this process significantly increases the size of
23143 the generated executable file, but it does not increase the size of
23144 the executable program itself. Furthermore, if this program is run in
23145 the normal manner, it runs exactly as if the debug information were
23146 not present, and takes no more actual memory.
23147
23148 However, if the program is run under control of @code{GDB}, the
23149 debugger is activated. The image of the program is loaded, at which
23150 point it is ready to run. If a run command is given, then the program
23151 will run exactly as it would have if @code{GDB} were not present. This
23152 is a crucial part of the @code{GDB} design philosophy. @code{GDB} is
23153 entirely non-intrusive until a breakpoint is encountered. If no
23154 breakpoint is ever hit, the program will run exactly as it would if no
23155 debugger were present. When a breakpoint is hit, @code{GDB} accesses
23156 the debugging information and can respond to user commands to inspect
23157 variables, and more generally to report on the state of execution.
23158
23159 @c **************
23160 @node Running GDB
23161 @section Running GDB
23162
23163 @noindent
23164 This section describes how to initiate the debugger.
23165 @c The above sentence is really just filler, but it was otherwise
23166 @c clumsy to get the first paragraph nonindented given the conditional
23167 @c nature of the description
23168
23169 @ifclear vms
23170 The debugger can be launched from a @code{GPS} menu or
23171 directly from the command line. The description below covers the latter use.
23172 All the commands shown can be used in the @code{GPS} debug console window,
23173 but there are usually more GUI-based ways to achieve the same effect.
23174 @end ifclear
23175
23176 The command to run @code{GDB} is
23177
23178 @smallexample
23179 $ ^gdb program^GDB PROGRAM^
23180 @end smallexample
23181
23182 @noindent
23183 where @code{^program^PROGRAM^} is the name of the executable file. This
23184 activates the debugger and results in a prompt for debugger commands.
23185 The simplest command is simply @code{run}, which causes the program to run
23186 exactly as if the debugger were not present. The following section
23187 describes some of the additional commands that can be given to @code{GDB}.
23188
23189 @c *******************************
23190 @node Introduction to GDB Commands
23191 @section Introduction to GDB Commands
23192
23193 @noindent
23194 @code{GDB} contains a large repertoire of commands. @xref{Top,,
23195 Debugging with GDB, gdb, Debugging with GDB},
23196 @ifset vms
23197 located in the GNU:[DOCS] directory,
23198 @end ifset
23199 for extensive documentation on the use
23200 of these commands, together with examples of their use. Furthermore,
23201 the command @command{help} invoked from within GDB activates a simple help
23202 facility which summarizes the available commands and their options.
23203 In this section we summarize a few of the most commonly
23204 used commands to give an idea of what @code{GDB} is about. You should create
23205 a simple program with debugging information and experiment with the use of
23206 these @code{GDB} commands on the program as you read through the
23207 following section.
23208
23209 @table @code
23210 @item set args @var{arguments}
23211 The @var{arguments} list above is a list of arguments to be passed to
23212 the program on a subsequent run command, just as though the arguments
23213 had been entered on a normal invocation of the program. The @code{set args}
23214 command is not needed if the program does not require arguments.
23215
23216 @item run
23217 The @code{run} command causes execution of the program to start from
23218 the beginning. If the program is already running, that is to say if
23219 you are currently positioned at a breakpoint, then a prompt will ask
23220 for confirmation that you want to abandon the current execution and
23221 restart.
23222
23223 @item breakpoint @var{location}
23224 The breakpoint command sets a breakpoint, that is to say a point at which
23225 execution will halt and @code{GDB} will await further
23226 commands. @var{location} is
23227 either a line number within a file, given in the format @code{file:linenumber},
23228 or it is the name of a subprogram. If you request that a breakpoint be set on
23229 a subprogram that is overloaded, a prompt will ask you to specify on which of
23230 those subprograms you want to breakpoint. You can also
23231 specify that all of them should be breakpointed. If the program is run
23232 and execution encounters the breakpoint, then the program
23233 stops and @code{GDB} signals that the breakpoint was encountered by
23234 printing the line of code before which the program is halted.
23235
23236 @item breakpoint exception @var{name}
23237 A special form of the breakpoint command which breakpoints whenever
23238 exception @var{name} is raised.
23239 If @var{name} is omitted,
23240 then a breakpoint will occur when any exception is raised.
23241
23242 @item print @var{expression}
23243 This will print the value of the given expression. Most simple
23244 Ada expression formats are properly handled by @code{GDB}, so the expression
23245 can contain function calls, variables, operators, and attribute references.
23246
23247 @item continue
23248 Continues execution following a breakpoint, until the next breakpoint or the
23249 termination of the program.
23250
23251 @item step
23252 Executes a single line after a breakpoint. If the next statement
23253 is a subprogram call, execution continues into (the first statement of)
23254 the called subprogram.
23255
23256 @item next
23257 Executes a single line. If this line is a subprogram call, executes and
23258 returns from the call.
23259
23260 @item list
23261 Lists a few lines around the current source location. In practice, it
23262 is usually more convenient to have a separate edit window open with the
23263 relevant source file displayed. Successive applications of this command
23264 print subsequent lines. The command can be given an argument which is a
23265 line number, in which case it displays a few lines around the specified one.
23266
23267 @item backtrace
23268 Displays a backtrace of the call chain. This command is typically
23269 used after a breakpoint has occurred, to examine the sequence of calls that
23270 leads to the current breakpoint. The display includes one line for each
23271 activation record (frame) corresponding to an active subprogram.
23272
23273 @item up
23274 At a breakpoint, @code{GDB} can display the values of variables local
23275 to the current frame. The command @code{up} can be used to
23276 examine the contents of other active frames, by moving the focus up
23277 the stack, that is to say from callee to caller, one frame at a time.
23278
23279 @item down
23280 Moves the focus of @code{GDB} down from the frame currently being
23281 examined to the frame of its callee (the reverse of the previous command),
23282
23283 @item frame @var{n}
23284 Inspect the frame with the given number. The value 0 denotes the frame
23285 of the current breakpoint, that is to say the top of the call stack.
23286
23287 @end table
23288
23289 @noindent
23290 The above list is a very short introduction to the commands that
23291 @code{GDB} provides. Important additional capabilities, including conditional
23292 breakpoints, the ability to execute command sequences on a breakpoint,
23293 the ability to debug at the machine instruction level and many other
23294 features are described in detail in @ref{Top,, Debugging with GDB, gdb,
23295 Debugging with GDB}. Note that most commands can be abbreviated
23296 (for example, c for continue, bt for backtrace).
23297
23298 @node Using Ada Expressions
23299 @section Using Ada Expressions
23300 @cindex Ada expressions
23301
23302 @noindent
23303 @code{GDB} supports a fairly large subset of Ada expression syntax, with some
23304 extensions. The philosophy behind the design of this subset is
23305
23306 @itemize @bullet
23307 @item
23308 That @code{GDB} should provide basic literals and access to operations for
23309 arithmetic, dereferencing, field selection, indexing, and subprogram calls,
23310 leaving more sophisticated computations to subprograms written into the
23311 program (which therefore may be called from @code{GDB}).
23312
23313 @item
23314 That type safety and strict adherence to Ada language restrictions
23315 are not particularly important to the @code{GDB} user.
23316
23317 @item
23318 That brevity is important to the @code{GDB} user.
23319 @end itemize
23320
23321 @noindent
23322 Thus, for brevity, the debugger acts as if there were
23323 implicit @code{with} and @code{use} clauses in effect for all user-written
23324 packages, thus making it unnecessary to fully qualify most names with
23325 their packages, regardless of context. Where this causes ambiguity,
23326 @code{GDB} asks the user's intent.
23327
23328 For details on the supported Ada syntax, see @ref{Top,, Debugging with
23329 GDB, gdb, Debugging with GDB}.
23330
23331 @node Calling User-Defined Subprograms
23332 @section Calling User-Defined Subprograms
23333
23334 @noindent
23335 An important capability of @code{GDB} is the ability to call user-defined
23336 subprograms while debugging. This is achieved simply by entering
23337 a subprogram call statement in the form:
23338
23339 @smallexample
23340 call subprogram-name (parameters)
23341 @end smallexample
23342
23343 @noindent
23344 The keyword @code{call} can be omitted in the normal case where the
23345 @code{subprogram-name} does not coincide with any of the predefined
23346 @code{GDB} commands.
23347
23348 The effect is to invoke the given subprogram, passing it the
23349 list of parameters that is supplied. The parameters can be expressions and
23350 can include variables from the program being debugged. The
23351 subprogram must be defined
23352 at the library level within your program, and @code{GDB} will call the
23353 subprogram within the environment of your program execution (which
23354 means that the subprogram is free to access or even modify variables
23355 within your program).
23356
23357 The most important use of this facility is in allowing the inclusion of
23358 debugging routines that are tailored to particular data structures
23359 in your program. Such debugging routines can be written to provide a suitably
23360 high-level description of an abstract type, rather than a low-level dump
23361 of its physical layout. After all, the standard
23362 @code{GDB print} command only knows the physical layout of your
23363 types, not their abstract meaning. Debugging routines can provide information
23364 at the desired semantic level and are thus enormously useful.
23365
23366 For example, when debugging GNAT itself, it is crucial to have access to
23367 the contents of the tree nodes used to represent the program internally.
23368 But tree nodes are represented simply by an integer value (which in turn
23369 is an index into a table of nodes).
23370 Using the @code{print} command on a tree node would simply print this integer
23371 value, which is not very useful. But the PN routine (defined in file
23372 treepr.adb in the GNAT sources) takes a tree node as input, and displays
23373 a useful high level representation of the tree node, which includes the
23374 syntactic category of the node, its position in the source, the integers
23375 that denote descendant nodes and parent node, as well as varied
23376 semantic information. To study this example in more detail, you might want to
23377 look at the body of the PN procedure in the stated file.
23378
23379 @node Using the Next Command in a Function
23380 @section Using the Next Command in a Function
23381
23382 @noindent
23383 When you use the @code{next} command in a function, the current source
23384 location will advance to the next statement as usual. A special case
23385 arises in the case of a @code{return} statement.
23386
23387 Part of the code for a return statement is the ``epilog'' of the function.
23388 This is the code that returns to the caller. There is only one copy of
23389 this epilog code, and it is typically associated with the last return
23390 statement in the function if there is more than one return. In some
23391 implementations, this epilog is associated with the first statement
23392 of the function.
23393
23394 The result is that if you use the @code{next} command from a return
23395 statement that is not the last return statement of the function you
23396 may see a strange apparent jump to the last return statement or to
23397 the start of the function. You should simply ignore this odd jump.
23398 The value returned is always that from the first return statement
23399 that was stepped through.
23400
23401 @node Ada Exceptions
23402 @section Breaking on Ada Exceptions
23403 @cindex Exceptions
23404
23405 @noindent
23406 You can set breakpoints that trip when your program raises
23407 selected exceptions.
23408
23409 @table @code
23410 @item break exception
23411 Set a breakpoint that trips whenever (any task in the) program raises
23412 any exception.
23413
23414 @item break exception @var{name}
23415 Set a breakpoint that trips whenever (any task in the) program raises
23416 the exception @var{name}.
23417
23418 @item break exception unhandled
23419 Set a breakpoint that trips whenever (any task in the) program raises an
23420 exception for which there is no handler.
23421
23422 @item info exceptions
23423 @itemx info exceptions @var{regexp}
23424 The @code{info exceptions} command permits the user to examine all defined
23425 exceptions within Ada programs. With a regular expression, @var{regexp}, as
23426 argument, prints out only those exceptions whose name matches @var{regexp}.
23427 @end table
23428
23429 @node Ada Tasks
23430 @section Ada Tasks
23431 @cindex Tasks
23432
23433 @noindent
23434 @code{GDB} allows the following task-related commands:
23435
23436 @table @code
23437 @item info tasks
23438 This command shows a list of current Ada tasks, as in the following example:
23439
23440 @smallexample
23441 @iftex
23442 @leftskip=0cm
23443 @end iftex
23444 (gdb) info tasks
23445 ID TID P-ID Thread Pri State Name
23446 1 8088000 0 807e000 15 Child Activation Wait main_task
23447 2 80a4000 1 80ae000 15 Accept/Select Wait b
23448 3 809a800 1 80a4800 15 Child Activation Wait a
23449 * 4 80ae800 3 80b8000 15 Running c
23450 @end smallexample
23451
23452 @noindent
23453 In this listing, the asterisk before the first task indicates it to be the
23454 currently running task. The first column lists the task ID that is used
23455 to refer to tasks in the following commands.
23456
23457 @item break @var{linespec} task @var{taskid}
23458 @itemx break @var{linespec} task @var{taskid} if @dots{}
23459 @cindex Breakpoints and tasks
23460 These commands are like the @code{break @dots{} thread @dots{}}.
23461 @var{linespec} specifies source lines.
23462
23463 Use the qualifier @samp{task @var{taskid}} with a breakpoint command
23464 to specify that you only want @code{GDB} to stop the program when a
23465 particular Ada task reaches this breakpoint. @var{taskid} is one of the
23466 numeric task identifiers assigned by @code{GDB}, shown in the first
23467 column of the @samp{info tasks} display.
23468
23469 If you do not specify @samp{task @var{taskid}} when you set a
23470 breakpoint, the breakpoint applies to @emph{all} tasks of your
23471 program.
23472
23473 You can use the @code{task} qualifier on conditional breakpoints as
23474 well; in this case, place @samp{task @var{taskid}} before the
23475 breakpoint condition (before the @code{if}).
23476
23477 @item task @var{taskno}
23478 @cindex Task switching
23479
23480 This command allows to switch to the task referred by @var{taskno}. In
23481 particular, This allows to browse the backtrace of the specified
23482 task. It is advised to switch back to the original task before
23483 continuing execution otherwise the scheduling of the program may be
23484 perturbed.
23485 @end table
23486
23487 @noindent
23488 For more detailed information on the tasking support,
23489 see @ref{Top,, Debugging with GDB, gdb, Debugging with GDB}.
23490
23491 @node Debugging Generic Units
23492 @section Debugging Generic Units
23493 @cindex Debugging Generic Units
23494 @cindex Generics
23495
23496 @noindent
23497 GNAT always uses code expansion for generic instantiation. This means that
23498 each time an instantiation occurs, a complete copy of the original code is
23499 made, with appropriate substitutions of formals by actuals.
23500
23501 It is not possible to refer to the original generic entities in
23502 @code{GDB}, but it is always possible to debug a particular instance of
23503 a generic, by using the appropriate expanded names. For example, if we have
23504
23505 @smallexample @c ada
23506 @group
23507 @cartouche
23508 procedure g is
23509
23510 generic package k is
23511 procedure kp (v1 : in out integer);
23512 end k;
23513
23514 package body k is
23515 procedure kp (v1 : in out integer) is
23516 begin
23517 v1 := v1 + 1;
23518 end kp;
23519 end k;
23520
23521 package k1 is new k;
23522 package k2 is new k;
23523
23524 var : integer := 1;
23525
23526 begin
23527 k1.kp (var);
23528 k2.kp (var);
23529 k1.kp (var);
23530 k2.kp (var);
23531 end;
23532 @end cartouche
23533 @end group
23534 @end smallexample
23535
23536 @noindent
23537 Then to break on a call to procedure kp in the k2 instance, simply
23538 use the command:
23539
23540 @smallexample
23541 (gdb) break g.k2.kp
23542 @end smallexample
23543
23544 @noindent
23545 When the breakpoint occurs, you can step through the code of the
23546 instance in the normal manner and examine the values of local variables, as for
23547 other units.
23548
23549 @node GNAT Abnormal Termination or Failure to Terminate
23550 @section GNAT Abnormal Termination or Failure to Terminate
23551 @cindex GNAT Abnormal Termination or Failure to Terminate
23552
23553 @noindent
23554 When presented with programs that contain serious errors in syntax
23555 or semantics,
23556 GNAT may on rare occasions experience problems in operation, such
23557 as aborting with a
23558 segmentation fault or illegal memory access, raising an internal
23559 exception, terminating abnormally, or failing to terminate at all.
23560 In such cases, you can activate
23561 various features of GNAT that can help you pinpoint the construct in your
23562 program that is the likely source of the problem.
23563
23564 The following strategies are presented in increasing order of
23565 difficulty, corresponding to your experience in using GNAT and your
23566 familiarity with compiler internals.
23567
23568 @enumerate
23569 @item
23570 Run @command{gcc} with the @option{-gnatf}. This first
23571 switch causes all errors on a given line to be reported. In its absence,
23572 only the first error on a line is displayed.
23573
23574 The @option{-gnatdO} switch causes errors to be displayed as soon as they
23575 are encountered, rather than after compilation is terminated. If GNAT
23576 terminates prematurely or goes into an infinite loop, the last error
23577 message displayed may help to pinpoint the culprit.
23578
23579 @item
23580 Run @command{gcc} with the @option{^-v (verbose)^/VERBOSE^} switch. In this
23581 mode, @command{gcc} produces ongoing information about the progress of the
23582 compilation and provides the name of each procedure as code is
23583 generated. This switch allows you to find which Ada procedure was being
23584 compiled when it encountered a code generation problem.
23585
23586 @item
23587 @cindex @option{-gnatdc} switch
23588 Run @command{gcc} with the @option{-gnatdc} switch. This is a GNAT specific
23589 switch that does for the front-end what @option{^-v^VERBOSE^} does
23590 for the back end. The system prints the name of each unit,
23591 either a compilation unit or nested unit, as it is being analyzed.
23592 @item
23593 Finally, you can start
23594 @code{gdb} directly on the @code{gnat1} executable. @code{gnat1} is the
23595 front-end of GNAT, and can be run independently (normally it is just
23596 called from @command{gcc}). You can use @code{gdb} on @code{gnat1} as you
23597 would on a C program (but @pxref{The GNAT Debugger GDB} for caveats). The
23598 @code{where} command is the first line of attack; the variable
23599 @code{lineno} (seen by @code{print lineno}), used by the second phase of
23600 @code{gnat1} and by the @command{gcc} backend, indicates the source line at
23601 which the execution stopped, and @code{input_file name} indicates the name of
23602 the source file.
23603 @end enumerate
23604
23605 @node Naming Conventions for GNAT Source Files
23606 @section Naming Conventions for GNAT Source Files
23607
23608 @noindent
23609 In order to examine the workings of the GNAT system, the following
23610 brief description of its organization may be helpful:
23611
23612 @itemize @bullet
23613 @item
23614 Files with prefix @file{^sc^SC^} contain the lexical scanner.
23615
23616 @item
23617 All files prefixed with @file{^par^PAR^} are components of the parser. The
23618 numbers correspond to chapters of the Ada Reference Manual. For example,
23619 parsing of select statements can be found in @file{par-ch9.adb}.
23620
23621 @item
23622 All files prefixed with @file{^sem^SEM^} perform semantic analysis. The
23623 numbers correspond to chapters of the Ada standard. For example, all
23624 issues involving context clauses can be found in @file{sem_ch10.adb}. In
23625 addition, some features of the language require sufficient special processing
23626 to justify their own semantic files: sem_aggr for aggregates, sem_disp for
23627 dynamic dispatching, etc.
23628
23629 @item
23630 All files prefixed with @file{^exp^EXP^} perform normalization and
23631 expansion of the intermediate representation (abstract syntax tree, or AST).
23632 these files use the same numbering scheme as the parser and semantics files.
23633 For example, the construction of record initialization procedures is done in
23634 @file{exp_ch3.adb}.
23635
23636 @item
23637 The files prefixed with @file{^bind^BIND^} implement the binder, which
23638 verifies the consistency of the compilation, determines an order of
23639 elaboration, and generates the bind file.
23640
23641 @item
23642 The files @file{atree.ads} and @file{atree.adb} detail the low-level
23643 data structures used by the front-end.
23644
23645 @item
23646 The files @file{sinfo.ads} and @file{sinfo.adb} detail the structure of
23647 the abstract syntax tree as produced by the parser.
23648
23649 @item
23650 The files @file{einfo.ads} and @file{einfo.adb} detail the attributes of
23651 all entities, computed during semantic analysis.
23652
23653 @item
23654 Library management issues are dealt with in files with prefix
23655 @file{^lib^LIB^}.
23656
23657 @item
23658 @findex Ada
23659 @cindex Annex A
23660 Ada files with the prefix @file{^a-^A-^} are children of @code{Ada}, as
23661 defined in Annex A.
23662
23663 @item
23664 @findex Interfaces
23665 @cindex Annex B
23666 Files with prefix @file{^i-^I-^} are children of @code{Interfaces}, as
23667 defined in Annex B.
23668
23669 @item
23670 @findex System
23671 Files with prefix @file{^s-^S-^} are children of @code{System}. This includes
23672 both language-defined children and GNAT run-time routines.
23673
23674 @item
23675 @findex GNAT
23676 Files with prefix @file{^g-^G-^} are children of @code{GNAT}. These are useful
23677 general-purpose packages, fully documented in their specs. All
23678 the other @file{.c} files are modifications of common @command{gcc} files.
23679 @end itemize
23680
23681 @node Getting Internal Debugging Information
23682 @section Getting Internal Debugging Information
23683
23684 @noindent
23685 Most compilers have internal debugging switches and modes. GNAT
23686 does also, except GNAT internal debugging switches and modes are not
23687 secret. A summary and full description of all the compiler and binder
23688 debug flags are in the file @file{debug.adb}. You must obtain the
23689 sources of the compiler to see the full detailed effects of these flags.
23690
23691 The switches that print the source of the program (reconstructed from
23692 the internal tree) are of general interest for user programs, as are the
23693 options to print
23694 the full internal tree, and the entity table (the symbol table
23695 information). The reconstructed source provides a readable version of the
23696 program after the front-end has completed analysis and expansion,
23697 and is useful when studying the performance of specific constructs.
23698 For example, constraint checks are indicated, complex aggregates
23699 are replaced with loops and assignments, and tasking primitives
23700 are replaced with run-time calls.
23701
23702 @node Stack Traceback
23703 @section Stack Traceback
23704 @cindex traceback
23705 @cindex stack traceback
23706 @cindex stack unwinding
23707
23708 @noindent
23709 Traceback is a mechanism to display the sequence of subprogram calls that
23710 leads to a specified execution point in a program. Often (but not always)
23711 the execution point is an instruction at which an exception has been raised.
23712 This mechanism is also known as @i{stack unwinding} because it obtains
23713 its information by scanning the run-time stack and recovering the activation
23714 records of all active subprograms. Stack unwinding is one of the most
23715 important tools for program debugging.
23716
23717 The first entry stored in traceback corresponds to the deepest calling level,
23718 that is to say the subprogram currently executing the instruction
23719 from which we want to obtain the traceback.
23720
23721 Note that there is no runtime performance penalty when stack traceback
23722 is enabled, and no exception is raised during program execution.
23723
23724 @menu
23725 * Non-Symbolic Traceback::
23726 * Symbolic Traceback::
23727 @end menu
23728
23729 @node Non-Symbolic Traceback
23730 @subsection Non-Symbolic Traceback
23731 @cindex traceback, non-symbolic
23732
23733 @noindent
23734 Note: this feature is not supported on all platforms. See
23735 @file{GNAT.Traceback spec in g-traceb.ads} for a complete list of supported
23736 platforms.
23737
23738 @menu
23739 * Tracebacks From an Unhandled Exception::
23740 * Tracebacks From Exception Occurrences (non-symbolic)::
23741 * Tracebacks From Anywhere in a Program (non-symbolic)::
23742 @end menu
23743
23744 @node Tracebacks From an Unhandled Exception
23745 @subsubsection Tracebacks From an Unhandled Exception
23746
23747 @noindent
23748 A runtime non-symbolic traceback is a list of addresses of call instructions.
23749 To enable this feature you must use the @option{-E}
23750 @code{gnatbind}'s option. With this option a stack traceback is stored as part
23751 of exception information. You can retrieve this information using the
23752 @code{addr2line} tool.
23753
23754 Here is a simple example:
23755
23756 @smallexample @c ada
23757 @cartouche
23758 procedure STB is
23759
23760 procedure P1 is
23761 begin
23762 raise Constraint_Error;
23763 end P1;
23764
23765 procedure P2 is
23766 begin
23767 P1;
23768 end P2;
23769
23770 begin
23771 P2;
23772 end STB;
23773 @end cartouche
23774 @end smallexample
23775
23776 @smallexample
23777 $ gnatmake stb -bargs -E
23778 $ stb
23779
23780 Execution terminated by unhandled exception
23781 Exception name: CONSTRAINT_ERROR
23782 Message: stb.adb:5
23783 Call stack traceback locations:
23784 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
23785 @end smallexample
23786
23787 @noindent
23788 As we see the traceback lists a sequence of addresses for the unhandled
23789 exception @code{CONSTRAINT_ERROR} raised in procedure P1. It is easy to
23790 guess that this exception come from procedure P1. To translate these
23791 addresses into the source lines where the calls appear, the
23792 @code{addr2line} tool, described below, is invaluable. The use of this tool
23793 requires the program to be compiled with debug information.
23794
23795 @smallexample
23796 $ gnatmake -g stb -bargs -E
23797 $ stb
23798
23799 Execution terminated by unhandled exception
23800 Exception name: CONSTRAINT_ERROR
23801 Message: stb.adb:5
23802 Call stack traceback locations:
23803 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
23804
23805 $ addr2line --exe=stb 0x401373 0x40138b 0x40139c 0x401335 0x4011c4
23806 0x4011f1 0x77e892a4
23807
23808 00401373 at d:/stb/stb.adb:5
23809 0040138B at d:/stb/stb.adb:10
23810 0040139C at d:/stb/stb.adb:14
23811 00401335 at d:/stb/b~stb.adb:104
23812 004011C4 at /build/@dots{}/crt1.c:200
23813 004011F1 at /build/@dots{}/crt1.c:222
23814 77E892A4 in ?? at ??:0
23815 @end smallexample
23816
23817 @noindent
23818 The @code{addr2line} tool has several other useful options:
23819
23820 @table @code
23821 @item --functions
23822 to get the function name corresponding to any location
23823
23824 @item --demangle=gnat
23825 to use the gnat decoding mode for the function names. Note that
23826 for binutils version 2.9.x the option is simply @option{--demangle}.
23827 @end table
23828
23829 @smallexample
23830 $ addr2line --exe=stb --functions --demangle=gnat 0x401373 0x40138b
23831 0x40139c 0x401335 0x4011c4 0x4011f1
23832
23833 00401373 in stb.p1 at d:/stb/stb.adb:5
23834 0040138B in stb.p2 at d:/stb/stb.adb:10
23835 0040139C in stb at d:/stb/stb.adb:14
23836 00401335 in main at d:/stb/b~stb.adb:104
23837 004011C4 in <__mingw_CRTStartup> at /build/@dots{}/crt1.c:200
23838 004011F1 in <mainCRTStartup> at /build/@dots{}/crt1.c:222
23839 @end smallexample
23840
23841 @noindent
23842 From this traceback we can see that the exception was raised in
23843 @file{stb.adb} at line 5, which was reached from a procedure call in
23844 @file{stb.adb} at line 10, and so on. The @file{b~std.adb} is the binder file,
23845 which contains the call to the main program.
23846 @xref{Running gnatbind}. The remaining entries are assorted runtime routines,
23847 and the output will vary from platform to platform.
23848
23849 It is also possible to use @code{GDB} with these traceback addresses to debug
23850 the program. For example, we can break at a given code location, as reported
23851 in the stack traceback:
23852
23853 @smallexample
23854 $ gdb -nw stb
23855 @ifclear vms
23856 @noindent
23857 Furthermore, this feature is not implemented inside Windows DLL. Only
23858 the non-symbolic traceback is reported in this case.
23859 @end ifclear
23860
23861 (gdb) break *0x401373
23862 Breakpoint 1 at 0x401373: file stb.adb, line 5.
23863 @end smallexample
23864
23865 @noindent
23866 It is important to note that the stack traceback addresses
23867 do not change when debug information is included. This is particularly useful
23868 because it makes it possible to release software without debug information (to
23869 minimize object size), get a field report that includes a stack traceback
23870 whenever an internal bug occurs, and then be able to retrieve the sequence
23871 of calls with the same program compiled with debug information.
23872
23873 @node Tracebacks From Exception Occurrences (non-symbolic)
23874 @subsubsection Tracebacks From Exception Occurrences
23875
23876 @noindent
23877 Non-symbolic tracebacks are obtained by using the @option{-E} binder argument.
23878 The stack traceback is attached to the exception information string, and can
23879 be retrieved in an exception handler within the Ada program, by means of the
23880 Ada facilities defined in @code{Ada.Exceptions}. Here is a simple example:
23881
23882 @smallexample @c ada
23883 with Ada.Text_IO;
23884 with Ada.Exceptions;
23885
23886 procedure STB is
23887
23888 use Ada;
23889 use Ada.Exceptions;
23890
23891 procedure P1 is
23892 K : Positive := 1;
23893 begin
23894 K := K - 1;
23895 exception
23896 when E : others =>
23897 Text_IO.Put_Line (Exception_Information (E));
23898 end P1;
23899
23900 procedure P2 is
23901 begin
23902 P1;
23903 end P2;
23904
23905 begin
23906 P2;
23907 end STB;
23908 @end smallexample
23909
23910 @noindent
23911 This program will output:
23912
23913 @smallexample
23914 $ stb
23915
23916 Exception name: CONSTRAINT_ERROR
23917 Message: stb.adb:12
23918 Call stack traceback locations:
23919 0x4015e4 0x401633 0x401644 0x401461 0x4011c4 0x4011f1 0x77e892a4
23920 @end smallexample
23921
23922 @node Tracebacks From Anywhere in a Program (non-symbolic)
23923 @subsubsection Tracebacks From Anywhere in a Program
23924
23925 @noindent
23926 It is also possible to retrieve a stack traceback from anywhere in a
23927 program. For this you need to
23928 use the @code{GNAT.Traceback} API. This package includes a procedure called
23929 @code{Call_Chain} that computes a complete stack traceback, as well as useful
23930 display procedures described below. It is not necessary to use the
23931 @option{-E gnatbind} option in this case, because the stack traceback mechanism
23932 is invoked explicitly.
23933
23934 @noindent
23935 In the following example we compute a traceback at a specific location in
23936 the program, and we display it using @code{GNAT.Debug_Utilities.Image} to
23937 convert addresses to strings:
23938
23939 @smallexample @c ada
23940 with Ada.Text_IO;
23941 with GNAT.Traceback;
23942 with GNAT.Debug_Utilities;
23943
23944 procedure STB is
23945
23946 use Ada;
23947 use GNAT;
23948 use GNAT.Traceback;
23949
23950 procedure P1 is
23951 TB : Tracebacks_Array (1 .. 10);
23952 -- We are asking for a maximum of 10 stack frames.
23953 Len : Natural;
23954 -- Len will receive the actual number of stack frames returned.
23955 begin
23956 Call_Chain (TB, Len);
23957
23958 Text_IO.Put ("In STB.P1 : ");
23959
23960 for K in 1 .. Len loop
23961 Text_IO.Put (Debug_Utilities.Image (TB (K)));
23962 Text_IO.Put (' ');
23963 end loop;
23964
23965 Text_IO.New_Line;
23966 end P1;
23967
23968 procedure P2 is
23969 begin
23970 P1;
23971 end P2;
23972
23973 begin
23974 P2;
23975 end STB;
23976 @end smallexample
23977
23978 @smallexample
23979 $ gnatmake -g stb
23980 $ stb
23981
23982 In STB.P1 : 16#0040_F1E4# 16#0040_14F2# 16#0040_170B# 16#0040_171C#
23983 16#0040_1461# 16#0040_11C4# 16#0040_11F1# 16#77E8_92A4#
23984 @end smallexample
23985
23986 @noindent
23987 You can then get further information by invoking the @code{addr2line}
23988 tool as described earlier (note that the hexadecimal addresses
23989 need to be specified in C format, with a leading ``0x'').
23990
23991 @node Symbolic Traceback
23992 @subsection Symbolic Traceback
23993 @cindex traceback, symbolic
23994
23995 @noindent
23996 A symbolic traceback is a stack traceback in which procedure names are
23997 associated with each code location.
23998
23999 @noindent
24000 Note that this feature is not supported on all platforms. See
24001 @file{GNAT.Traceback.Symbolic spec in g-trasym.ads} for a complete
24002 list of currently supported platforms.
24003
24004 @noindent
24005 Note that the symbolic traceback requires that the program be compiled
24006 with debug information. If it is not compiled with debug information
24007 only the non-symbolic information will be valid.
24008
24009 @menu
24010 * Tracebacks From Exception Occurrences (symbolic)::
24011 * Tracebacks From Anywhere in a Program (symbolic)::
24012 @end menu
24013
24014 @node Tracebacks From Exception Occurrences (symbolic)
24015 @subsubsection Tracebacks From Exception Occurrences
24016
24017 @smallexample @c ada
24018 with Ada.Text_IO;
24019 with GNAT.Traceback.Symbolic;
24020
24021 procedure STB is
24022
24023 procedure P1 is
24024 begin
24025 raise Constraint_Error;
24026 end P1;
24027
24028 procedure P2 is
24029 begin
24030 P1;
24031 end P2;
24032
24033 procedure P3 is
24034 begin
24035 P2;
24036 end P3;
24037
24038 begin
24039 P3;
24040 exception
24041 when E : others =>
24042 Ada.Text_IO.Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
24043 end STB;
24044 @end smallexample
24045
24046 @smallexample
24047 $ gnatmake -g .\stb -bargs -E -largs -lgnat -laddr2line -lintl
24048 $ stb
24049
24050 0040149F in stb.p1 at stb.adb:8
24051 004014B7 in stb.p2 at stb.adb:13
24052 004014CF in stb.p3 at stb.adb:18
24053 004015DD in ada.stb at stb.adb:22
24054 00401461 in main at b~stb.adb:168
24055 004011C4 in __mingw_CRTStartup at crt1.c:200
24056 004011F1 in mainCRTStartup at crt1.c:222
24057 77E892A4 in ?? at ??:0
24058 @end smallexample
24059
24060 @noindent
24061 In the above example the ``.\'' syntax in the @command{gnatmake} command
24062 is currently required by @command{addr2line} for files that are in
24063 the current working directory.
24064 Moreover, the exact sequence of linker options may vary from platform
24065 to platform.
24066 The above @option{-largs} section is for Windows platforms. By contrast,
24067 under Unix there is no need for the @option{-largs} section.
24068 Differences across platforms are due to details of linker implementation.
24069
24070 @node Tracebacks From Anywhere in a Program (symbolic)
24071 @subsubsection Tracebacks From Anywhere in a Program
24072
24073 @noindent
24074 It is possible to get a symbolic stack traceback
24075 from anywhere in a program, just as for non-symbolic tracebacks.
24076 The first step is to obtain a non-symbolic
24077 traceback, and then call @code{Symbolic_Traceback} to compute the symbolic
24078 information. Here is an example:
24079
24080 @smallexample @c ada
24081 with Ada.Text_IO;
24082 with GNAT.Traceback;
24083 with GNAT.Traceback.Symbolic;
24084
24085 procedure STB is
24086
24087 use Ada;
24088 use GNAT.Traceback;
24089 use GNAT.Traceback.Symbolic;
24090
24091 procedure P1 is
24092 TB : Tracebacks_Array (1 .. 10);
24093 -- We are asking for a maximum of 10 stack frames.
24094 Len : Natural;
24095 -- Len will receive the actual number of stack frames returned.
24096 begin
24097 Call_Chain (TB, Len);
24098 Text_IO.Put_Line (Symbolic_Traceback (TB (1 .. Len)));
24099 end P1;
24100
24101 procedure P2 is
24102 begin
24103 P1;
24104 end P2;
24105
24106 begin
24107 P2;
24108 end STB;
24109 @end smallexample
24110
24111 @c ******************************
24112 @ifset vms
24113 @node Compatibility with HP Ada
24114 @chapter Compatibility with HP Ada
24115 @cindex Compatibility
24116
24117 @noindent
24118 @cindex DEC Ada
24119 @cindex HP Ada
24120 @cindex Compatibility between GNAT and HP Ada
24121 This chapter compares HP Ada (formerly known as ``DEC Ada'')
24122 for OpenVMS Alpha and GNAT for OpenVMS for Alpha and for I64.
24123 GNAT is highly compatible
24124 with HP Ada, and it should generally be straightforward to port code
24125 from the HP Ada environment to GNAT. However, there are a few language
24126 and implementation differences of which the user must be aware. These
24127 differences are discussed in this chapter. In
24128 addition, the operating environment and command structure for the
24129 compiler are different, and these differences are also discussed.
24130
24131 For further details on these and other compatibility issues,
24132 see Appendix E of the HP publication
24133 @cite{HP Ada, Technical Overview and Comparison on HP Platforms}.
24134
24135 Except where otherwise indicated, the description of GNAT for OpenVMS
24136 applies to both the Alpha and I64 platforms.
24137
24138 For information on porting Ada code from GNAT on Alpha OpenVMS to GNAT on
24139 I64 OpenVMS, see @ref{Transitioning to 64-Bit GNAT for OpenVMS}.
24140
24141 The discussion in this chapter addresses specifically the implementation
24142 of Ada 83 for HP OpenVMS Alpha Systems. In cases where the implementation
24143 of HP Ada differs between OpenVMS Alpha Systems and OpenVMS VAX Systems,
24144 GNAT always follows the Alpha implementation.
24145
24146 For GNAT running on other than VMS systems, all the HP Ada 83 pragmas and
24147 attributes are recognized, although only a subset of them can sensibly
24148 be implemented. The description of pragmas in
24149 @xref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference Manual}
24150 indicates whether or not they are applicable to non-VMS systems.
24151
24152 @menu
24153 * Ada Language Compatibility::
24154 * Differences in the Definition of Package System::
24155 * Language-Related Features::
24156 * The Package STANDARD::
24157 * The Package SYSTEM::
24158 * Tasking and Task-Related Features::
24159 * Pragmas and Pragma-Related Features::
24160 * Library of Predefined Units::
24161 * Bindings::
24162 * Main Program Definition::
24163 * Implementation-Defined Attributes::
24164 * Compiler and Run-Time Interfacing::
24165 * Program Compilation and Library Management::
24166 * Input-Output::
24167 * Implementation Limits::
24168 * Tools and Utilities::
24169 @end menu
24170
24171 @node Ada Language Compatibility
24172 @section Ada Language Compatibility
24173
24174 @noindent
24175 GNAT handles Ada 95 and Ada 2005 as well as Ada 83, whereas HP Ada is only
24176 for Ada 83. Ada 95 and Ada 2005 are almost completely upwards compatible
24177 with Ada 83, and therefore Ada 83 programs will compile
24178 and run under GNAT with
24179 no changes or only minor changes. The @cite{Annotated Ada Reference Manual}
24180 provides details on specific incompatibilities.
24181
24182 GNAT provides the switch @option{/83} on the @command{GNAT COMPILE} command,
24183 as well as the pragma @code{ADA_83}, to force the compiler to
24184 operate in Ada 83 mode. This mode does not guarantee complete
24185 conformance to Ada 83, but in practice is sufficient to
24186 eliminate most sources of incompatibilities.
24187 In particular, it eliminates the recognition of the
24188 additional Ada 95 and Ada 2005 keywords, so that their use as identifiers
24189 in Ada 83 programs is legal, and handles the cases of packages
24190 with optional bodies, and generics that instantiate unconstrained
24191 types without the use of @code{(<>)}.
24192
24193 @node Differences in the Definition of Package System
24194 @section Differences in the Definition of Package @code{System}
24195
24196 @noindent
24197 An Ada compiler is allowed to add
24198 implementation-dependent declarations to package @code{System}.
24199 In normal mode,
24200 GNAT does not take advantage of this permission, and the version of
24201 @code{System} provided by GNAT exactly matches that defined in the Ada
24202 Reference Manual.
24203
24204 However, HP Ada adds an extensive set of declarations to package
24205 @code{System},
24206 as fully documented in the HP Ada manuals. To minimize changes required
24207 for programs that make use of these extensions, GNAT provides the pragma
24208 @code{Extend_System} for extending the definition of package System. By using:
24209 @cindex pragma @code{Extend_System}
24210 @cindex @code{Extend_System} pragma
24211
24212 @smallexample @c ada
24213 @group
24214 @cartouche
24215 pragma Extend_System (Aux_DEC);
24216 @end cartouche
24217 @end group
24218 @end smallexample
24219
24220 @noindent
24221 the set of definitions in @code{System} is extended to include those in
24222 package @code{System.Aux_DEC}.
24223 @cindex @code{System.Aux_DEC} package
24224 @cindex @code{Aux_DEC} package (child of @code{System})
24225 These definitions are incorporated directly into package @code{System},
24226 as though they had been declared there. For a
24227 list of the declarations added, see the spec of this package,
24228 which can be found in the file @file{s-auxdec.ads} in the GNAT library.
24229 @cindex @file{s-auxdec.ads} file
24230 The pragma @code{Extend_System} is a configuration pragma, which means that
24231 it can be placed in the file @file{gnat.adc}, so that it will automatically
24232 apply to all subsequent compilations. See @ref{Configuration Pragmas},
24233 for further details.
24234
24235 An alternative approach that avoids the use of the non-standard
24236 @code{Extend_System} pragma is to add a context clause to the unit that
24237 references these facilities:
24238
24239 @smallexample @c ada
24240 @cartouche
24241 with System.Aux_DEC;
24242 use System.Aux_DEC;
24243 @end cartouche
24244 @end smallexample
24245
24246 @noindent
24247 The effect is not quite semantically identical to incorporating
24248 the declarations directly into package @code{System},
24249 but most programs will not notice a difference
24250 unless they use prefix notation (e.g.@: @code{System.Integer_8})
24251 to reference the entities directly in package @code{System}.
24252 For units containing such references,
24253 the prefixes must either be removed, or the pragma @code{Extend_System}
24254 must be used.
24255
24256 @node Language-Related Features
24257 @section Language-Related Features
24258
24259 @noindent
24260 The following sections highlight differences in types,
24261 representations of types, operations, alignment, and
24262 related topics.
24263
24264 @menu
24265 * Integer Types and Representations::
24266 * Floating-Point Types and Representations::
24267 * Pragmas Float_Representation and Long_Float::
24268 * Fixed-Point Types and Representations::
24269 * Record and Array Component Alignment::
24270 * Address Clauses::
24271 * Other Representation Clauses::
24272 @end menu
24273
24274 @node Integer Types and Representations
24275 @subsection Integer Types and Representations
24276
24277 @noindent
24278 The set of predefined integer types is identical in HP Ada and GNAT.
24279 Furthermore the representation of these integer types is also identical,
24280 including the capability of size clauses forcing biased representation.
24281
24282 In addition,
24283 HP Ada for OpenVMS Alpha systems has defined the
24284 following additional integer types in package @code{System}:
24285
24286 @itemize @bullet
24287
24288 @item
24289 @code{INTEGER_8}
24290
24291 @item
24292 @code{INTEGER_16}
24293
24294 @item
24295 @code{INTEGER_32}
24296
24297 @item
24298 @code{INTEGER_64}
24299
24300 @item
24301 @code{LARGEST_INTEGER}
24302 @end itemize
24303
24304 @noindent
24305 In GNAT, the first four of these types may be obtained from the
24306 standard Ada package @code{Interfaces}.
24307 Alternatively, by use of the pragma @code{Extend_System}, identical
24308 declarations can be referenced directly in package @code{System}.
24309 On both GNAT and HP Ada, the maximum integer size is 64 bits.
24310
24311 @node Floating-Point Types and Representations
24312 @subsection Floating-Point Types and Representations
24313 @cindex Floating-Point types
24314
24315 @noindent
24316 The set of predefined floating-point types is identical in HP Ada and GNAT.
24317 Furthermore the representation of these floating-point
24318 types is also identical. One important difference is that the default
24319 representation for HP Ada is @code{VAX_Float}, but the default representation
24320 for GNAT is IEEE.
24321
24322 Specific types may be declared to be @code{VAX_Float} or IEEE, using the
24323 pragma @code{Float_Representation} as described in the HP Ada
24324 documentation.
24325 For example, the declarations:
24326
24327 @smallexample @c ada
24328 @cartouche
24329 type F_Float is digits 6;
24330 pragma Float_Representation (VAX_Float, F_Float);
24331 @end cartouche
24332 @end smallexample
24333
24334 @noindent
24335 declares a type @code{F_Float} that will be represented in @code{VAX_Float}
24336 format.
24337 This set of declarations actually appears in @code{System.Aux_DEC},
24338 which contains
24339 the full set of additional floating-point declarations provided in
24340 the HP Ada version of package @code{System}.
24341 This and similar declarations may be accessed in a user program
24342 by using pragma @code{Extend_System}. The use of this
24343 pragma, and the related pragma @code{Long_Float} is described in further
24344 detail in the following section.
24345
24346 @node Pragmas Float_Representation and Long_Float
24347 @subsection Pragmas @code{Float_Representation} and @code{Long_Float}
24348
24349 @noindent
24350 HP Ada provides the pragma @code{Float_Representation}, which
24351 acts as a program library switch to allow control over
24352 the internal representation chosen for the predefined
24353 floating-point types declared in the package @code{Standard}.
24354 The format of this pragma is as follows:
24355
24356 @smallexample @c ada
24357 @cartouche
24358 pragma Float_Representation(VAX_Float | IEEE_Float);
24359 @end cartouche
24360 @end smallexample
24361
24362 @noindent
24363 This pragma controls the representation of floating-point
24364 types as follows:
24365
24366 @itemize @bullet
24367 @item
24368 @code{VAX_Float} specifies that floating-point
24369 types are represented by default with the VAX system hardware types
24370 @code{F-floating}, @code{D-floating}, @code{G-floating}.
24371 Note that the @code{H-floating}
24372 type was available only on VAX systems, and is not available
24373 in either HP Ada or GNAT.
24374
24375 @item
24376 @code{IEEE_Float} specifies that floating-point
24377 types are represented by default with the IEEE single and
24378 double floating-point types.
24379 @end itemize
24380
24381 @noindent
24382 GNAT provides an identical implementation of the pragma
24383 @code{Float_Representation}, except that it functions as a
24384 configuration pragma. Note that the
24385 notion of configuration pragma corresponds closely to the
24386 HP Ada notion of a program library switch.
24387
24388 When no pragma is used in GNAT, the default is @code{IEEE_Float},
24389 which is different
24390 from HP Ada 83, where the default is @code{VAX_Float}. In addition, the
24391 predefined libraries in GNAT are built using @code{IEEE_Float}, so it is not
24392 advisable to change the format of numbers passed to standard library
24393 routines, and if necessary explicit type conversions may be needed.
24394
24395 The use of @code{IEEE_Float} is recommended in GNAT since it is more
24396 efficient, and (given that it conforms to an international standard)
24397 potentially more portable.
24398 The situation in which @code{VAX_Float} may be useful is in interfacing
24399 to existing code and data that expect the use of @code{VAX_Float}.
24400 In such a situation use the predefined @code{VAX_Float}
24401 types in package @code{System}, as extended by
24402 @code{Extend_System}. For example, use @code{System.F_Float}
24403 to specify the 32-bit @code{F-Float} format.
24404
24405 @noindent
24406 On OpenVMS systems, HP Ada provides the pragma @code{Long_Float}
24407 to allow control over the internal representation chosen
24408 for the predefined type @code{Long_Float} and for floating-point
24409 type declarations with digits specified in the range 7 .. 15.
24410 The format of this pragma is as follows:
24411
24412 @smallexample @c ada
24413 @cartouche
24414 pragma Long_Float (D_FLOAT | G_FLOAT);
24415 @end cartouche
24416 @end smallexample
24417
24418 @node Fixed-Point Types and Representations
24419 @subsection Fixed-Point Types and Representations
24420
24421 @noindent
24422 On HP Ada for OpenVMS Alpha systems, rounding is
24423 away from zero for both positive and negative numbers.
24424 Therefore, @code{+0.5} rounds to @code{1},
24425 and @code{-0.5} rounds to @code{-1}.
24426
24427 On GNAT the results of operations
24428 on fixed-point types are in accordance with the Ada
24429 rules. In particular, results of operations on decimal
24430 fixed-point types are truncated.
24431
24432 @node Record and Array Component Alignment
24433 @subsection Record and Array Component Alignment
24434
24435 @noindent
24436 On HP Ada for OpenVMS Alpha, all non-composite components
24437 are aligned on natural boundaries. For example, 1-byte
24438 components are aligned on byte boundaries, 2-byte
24439 components on 2-byte boundaries, 4-byte components on 4-byte
24440 byte boundaries, and so on. The OpenVMS Alpha hardware
24441 runs more efficiently with naturally aligned data.
24442
24443 On GNAT, alignment rules are compatible
24444 with HP Ada for OpenVMS Alpha.
24445
24446 @node Address Clauses
24447 @subsection Address Clauses
24448
24449 @noindent
24450 In HP Ada and GNAT, address clauses are supported for
24451 objects and imported subprograms.
24452 The predefined type @code{System.Address} is a private type
24453 in both compilers on Alpha OpenVMS, with the same representation
24454 (it is simply a machine pointer). Addition, subtraction, and comparison
24455 operations are available in the standard Ada package
24456 @code{System.Storage_Elements}, or in package @code{System}
24457 if it is extended to include @code{System.Aux_DEC} using a
24458 pragma @code{Extend_System} as previously described.
24459
24460 Note that code that @code{with}'s both this extended package @code{System}
24461 and the package @code{System.Storage_Elements} should not @code{use}
24462 both packages, or ambiguities will result. In general it is better
24463 not to mix these two sets of facilities. The Ada package was
24464 designed specifically to provide the kind of features that HP Ada
24465 adds directly to package @code{System}.
24466
24467 The type @code{System.Address} is a 64-bit integer type in GNAT for
24468 I64 OpenVMS. For more information,
24469 see @ref{Transitioning to 64-Bit GNAT for OpenVMS}.
24470
24471 GNAT is compatible with HP Ada in its handling of address
24472 clauses, except for some limitations in
24473 the form of address clauses for composite objects with
24474 initialization. Such address clauses are easily replaced
24475 by the use of an explicitly-defined constant as described
24476 in the Ada Reference Manual (13.1(22)). For example, the sequence
24477 of declarations:
24478
24479 @smallexample @c ada
24480 @cartouche
24481 X, Y : Integer := Init_Func;
24482 Q : String (X .. Y) := "abc";
24483 @dots{}
24484 for Q'Address use Compute_Address;
24485 @end cartouche
24486 @end smallexample
24487
24488 @noindent
24489 will be rejected by GNAT, since the address cannot be computed at the time
24490 that @code{Q} is declared. To achieve the intended effect, write instead:
24491
24492 @smallexample @c ada
24493 @group
24494 @cartouche
24495 X, Y : Integer := Init_Func;
24496 Q_Address : constant Address := Compute_Address;
24497 Q : String (X .. Y) := "abc";
24498 @dots{}
24499 for Q'Address use Q_Address;
24500 @end cartouche
24501 @end group
24502 @end smallexample
24503
24504 @noindent
24505 which will be accepted by GNAT (and other Ada compilers), and is also
24506 compatible with Ada 83. A fuller description of the restrictions
24507 on address specifications is found in @ref{Top, GNAT Reference Manual,
24508 About This Guide, gnat_rm, GNAT Reference Manual}.
24509
24510 @node Other Representation Clauses
24511 @subsection Other Representation Clauses
24512
24513 @noindent
24514 GNAT implements in a compatible manner all the representation
24515 clauses supported by HP Ada. In addition, GNAT
24516 implements the representation clause forms that were introduced in Ada 95,
24517 including @code{COMPONENT_SIZE} and @code{SIZE} clauses for objects.
24518
24519 @node The Package STANDARD
24520 @section The Package @code{STANDARD}
24521
24522 @noindent
24523 The package @code{STANDARD}, as implemented by HP Ada, is fully
24524 described in the @cite{Ada Reference Manual} and in the
24525 @cite{HP Ada Language Reference Manual}. As implemented by GNAT, the
24526 package @code{STANDARD} is described in the @cite{Ada Reference Manual}.
24527
24528 In addition, HP Ada supports the Latin-1 character set in
24529 the type @code{CHARACTER}. GNAT supports the Latin-1 character set
24530 in the type @code{CHARACTER} and also Unicode (ISO 10646 BMP) in
24531 the type @code{WIDE_CHARACTER}.
24532
24533 The floating-point types supported by GNAT are those
24534 supported by HP Ada, but the defaults are different, and are controlled by
24535 pragmas. See @ref{Floating-Point Types and Representations}, for details.
24536
24537 @node The Package SYSTEM
24538 @section The Package @code{SYSTEM}
24539
24540 @noindent
24541 HP Ada provides a specific version of the package
24542 @code{SYSTEM} for each platform on which the language is implemented.
24543 For the complete spec of the package @code{SYSTEM}, see
24544 Appendix F of the @cite{HP Ada Language Reference Manual}.
24545
24546 On HP Ada, the package @code{SYSTEM} includes the following conversion
24547 functions:
24548 @itemize @bullet
24549 @item @code{TO_ADDRESS(INTEGER)}
24550
24551 @item @code{TO_ADDRESS(UNSIGNED_LONGWORD)}
24552
24553 @item @code{TO_ADDRESS(}@i{universal_integer}@code{)}
24554
24555 @item @code{TO_INTEGER(ADDRESS)}
24556
24557 @item @code{TO_UNSIGNED_LONGWORD(ADDRESS)}
24558
24559 @item Function @code{IMPORT_VALUE return UNSIGNED_LONGWORD} and the
24560 functions @code{IMPORT_ADDRESS} and @code{IMPORT_LARGEST_VALUE}
24561 @end itemize
24562
24563 @noindent
24564 By default, GNAT supplies a version of @code{SYSTEM} that matches
24565 the definition given in the @cite{Ada Reference Manual}.
24566 This
24567 is a subset of the HP system definitions, which is as
24568 close as possible to the original definitions. The only difference
24569 is that the definition of @code{SYSTEM_NAME} is different:
24570
24571 @smallexample @c ada
24572 @cartouche
24573 type Name is (SYSTEM_NAME_GNAT);
24574 System_Name : constant Name := SYSTEM_NAME_GNAT;
24575 @end cartouche
24576 @end smallexample
24577
24578 @noindent
24579 Also, GNAT adds the Ada declarations for
24580 @code{BIT_ORDER} and @code{DEFAULT_BIT_ORDER}.
24581
24582 However, the use of the following pragma causes GNAT
24583 to extend the definition of package @code{SYSTEM} so that it
24584 encompasses the full set of HP-specific extensions,
24585 including the functions listed above:
24586
24587 @smallexample @c ada
24588 @cartouche
24589 pragma Extend_System (Aux_DEC);
24590 @end cartouche
24591 @end smallexample
24592
24593 @noindent
24594 The pragma @code{Extend_System} is a configuration pragma that
24595 is most conveniently placed in the @file{gnat.adc} file. @xref{Pragma
24596 Extend_System,,, gnat_rm, GNAT Reference Manual} for further details.
24597
24598 HP Ada does not allow the recompilation of the package
24599 @code{SYSTEM}. Instead HP Ada provides several pragmas
24600 (@code{SYSTEM_NAME}, @code{STORAGE_UNIT}, and @code{MEMORY_SIZE})
24601 to modify values in the package @code{SYSTEM}.
24602 On OpenVMS Alpha systems, the pragma
24603 @code{SYSTEM_NAME} takes the enumeration literal @code{OPENVMS_AXP} as
24604 its single argument.
24605
24606 GNAT does permit the recompilation of package @code{SYSTEM} using
24607 the special switch @option{-gnatg}, and this switch can be used if
24608 it is necessary to modify the definitions in @code{SYSTEM}. GNAT does
24609 not permit the specification of @code{SYSTEM_NAME}, @code{STORAGE_UNIT}
24610 or @code{MEMORY_SIZE} by any other means.
24611
24612 On GNAT systems, the pragma @code{SYSTEM_NAME} takes the
24613 enumeration literal @code{SYSTEM_NAME_GNAT}.
24614
24615 The definitions provided by the use of
24616
24617 @smallexample @c ada
24618 pragma Extend_System (AUX_Dec);
24619 @end smallexample
24620
24621 @noindent
24622 are virtually identical to those provided by the HP Ada 83 package
24623 @code{SYSTEM}. One important difference is that the name of the
24624 @code{TO_ADDRESS}
24625 function for type @code{UNSIGNED_LONGWORD} is changed to
24626 @code{TO_ADDRESS_LONG}.
24627 @xref{Address Clauses,,, gnat_rm, GNAT Reference Manual} for a
24628 discussion of why this change was necessary.
24629
24630 @noindent
24631 The version of @code{TO_ADDRESS} taking a @i{universal_integer} argument
24632 is in fact
24633 an extension to Ada 83 not strictly compatible with the reference manual.
24634 GNAT, in order to be exactly compatible with the standard,
24635 does not provide this capability. In HP Ada 83, the
24636 point of this definition is to deal with a call like:
24637
24638 @smallexample @c ada
24639 TO_ADDRESS (16#12777#);
24640 @end smallexample
24641
24642 @noindent
24643 Normally, according to Ada 83 semantics, one would expect this to be
24644 ambiguous, since it matches both the @code{INTEGER} and
24645 @code{UNSIGNED_LONGWORD} forms of @code{TO_ADDRESS}.
24646 However, in HP Ada 83, there is no ambiguity, since the
24647 definition using @i{universal_integer} takes precedence.
24648
24649 In GNAT, since the version with @i{universal_integer} cannot be supplied,
24650 it is
24651 not possible to be 100% compatible. Since there are many programs using
24652 numeric constants for the argument to @code{TO_ADDRESS}, the decision in
24653 GNAT was
24654 to change the name of the function in the @code{UNSIGNED_LONGWORD} case,
24655 so the declarations provided in the GNAT version of @code{AUX_Dec} are:
24656
24657 @smallexample @c ada
24658 function To_Address (X : Integer) return Address;
24659 pragma Pure_Function (To_Address);
24660
24661 function To_Address_Long (X : Unsigned_Longword) return Address;
24662 pragma Pure_Function (To_Address_Long);
24663 @end smallexample
24664
24665 @noindent
24666 This means that programs using @code{TO_ADDRESS} for
24667 @code{UNSIGNED_LONGWORD} must change the name to @code{TO_ADDRESS_LONG}.
24668
24669 @node Tasking and Task-Related Features
24670 @section Tasking and Task-Related Features
24671
24672 @noindent
24673 This section compares the treatment of tasking in GNAT
24674 and in HP Ada for OpenVMS Alpha.
24675 The GNAT description applies to both Alpha and I64 OpenVMS.
24676 For detailed information on tasking in
24677 HP Ada, see the @cite{HP Ada Language Reference Manual} and the
24678 relevant run-time reference manual.
24679
24680 @menu
24681 * Implementation of Tasks in HP Ada for OpenVMS Alpha Systems::
24682 * Assigning Task IDs::
24683 * Task IDs and Delays::
24684 * Task-Related Pragmas::
24685 * Scheduling and Task Priority::
24686 * The Task Stack::
24687 * External Interrupts::
24688 @end menu
24689
24690 @node Implementation of Tasks in HP Ada for OpenVMS Alpha Systems
24691 @subsection Implementation of Tasks in HP Ada for OpenVMS Alpha Systems
24692
24693 @noindent
24694 On OpenVMS Alpha systems, each Ada task (except a passive
24695 task) is implemented as a single stream of execution
24696 that is created and managed by the kernel. On these
24697 systems, HP Ada tasking support is based on DECthreads,
24698 an implementation of the POSIX standard for threads.
24699
24700 Also, on OpenVMS Alpha systems, HP Ada tasks and foreign
24701 code that calls DECthreads routines can be used together.
24702 The interaction between Ada tasks and DECthreads routines
24703 can have some benefits. For example when on OpenVMS Alpha,
24704 HP Ada can call C code that is already threaded.
24705
24706 GNAT uses the facilities of DECthreads,
24707 and Ada tasks are mapped to threads.
24708
24709 @node Assigning Task IDs
24710 @subsection Assigning Task IDs
24711
24712 @noindent
24713 The HP Ada Run-Time Library always assigns @code{%TASK 1} to
24714 the environment task that executes the main program. On
24715 OpenVMS Alpha systems, @code{%TASK 0} is often used for tasks
24716 that have been created but are not yet activated.
24717
24718 On OpenVMS Alpha systems, task IDs are assigned at
24719 activation. On GNAT systems, task IDs are also assigned at
24720 task creation but do not have the same form or values as
24721 task ID values in HP Ada. There is no null task, and the
24722 environment task does not have a specific task ID value.
24723
24724 @node Task IDs and Delays
24725 @subsection Task IDs and Delays
24726
24727 @noindent
24728 On OpenVMS Alpha systems, tasking delays are implemented
24729 using Timer System Services. The Task ID is used for the
24730 identification of the timer request (the @code{REQIDT} parameter).
24731 If Timers are used in the application take care not to use
24732 @code{0} for the identification, because cancelling such a timer
24733 will cancel all timers and may lead to unpredictable results.
24734
24735 @node Task-Related Pragmas
24736 @subsection Task-Related Pragmas
24737
24738 @noindent
24739 Ada supplies the pragma @code{TASK_STORAGE}, which allows
24740 specification of the size of the guard area for a task
24741 stack. (The guard area forms an area of memory that has no
24742 read or write access and thus helps in the detection of
24743 stack overflow.) On OpenVMS Alpha systems, if the pragma
24744 @code{TASK_STORAGE} specifies a value of zero, a minimal guard
24745 area is created. In the absence of a pragma @code{TASK_STORAGE},
24746 a default guard area is created.
24747
24748 GNAT supplies the following task-related pragmas:
24749
24750 @itemize @bullet
24751 @item @code{TASK_INFO}
24752
24753 This pragma appears within a task definition and
24754 applies to the task in which it appears. The argument
24755 must be of type @code{SYSTEM.TASK_INFO.TASK_INFO_TYPE}.
24756
24757 @item @code{TASK_STORAGE}
24758
24759 GNAT implements pragma @code{TASK_STORAGE} in the same way as HP Ada.
24760 Both HP Ada and GNAT supply the pragmas @code{PASSIVE},
24761 @code{SUPPRESS}, and @code{VOLATILE}.
24762 @end itemize
24763 @node Scheduling and Task Priority
24764 @subsection Scheduling and Task Priority
24765
24766 @noindent
24767 HP Ada implements the Ada language requirement that
24768 when two tasks are eligible for execution and they have
24769 different priorities, the lower priority task does not
24770 execute while the higher priority task is waiting. The HP
24771 Ada Run-Time Library keeps a task running until either the
24772 task is suspended or a higher priority task becomes ready.
24773
24774 On OpenVMS Alpha systems, the default strategy is round-
24775 robin with preemption. Tasks of equal priority take turns
24776 at the processor. A task is run for a certain period of
24777 time and then placed at the tail of the ready queue for
24778 its priority level.
24779
24780 HP Ada provides the implementation-defined pragma @code{TIME_SLICE},
24781 which can be used to enable or disable round-robin
24782 scheduling of tasks with the same priority.
24783 See the relevant HP Ada run-time reference manual for
24784 information on using the pragmas to control HP Ada task
24785 scheduling.
24786
24787 GNAT follows the scheduling rules of Annex D (Real-Time
24788 Annex) of the @cite{Ada Reference Manual}. In general, this
24789 scheduling strategy is fully compatible with HP Ada
24790 although it provides some additional constraints (as
24791 fully documented in Annex D).
24792 GNAT implements time slicing control in a manner compatible with
24793 HP Ada 83, by means of the pragma @code{Time_Slice}, whose semantics
24794 are identical to the HP Ada 83 pragma of the same name.
24795 Note that it is not possible to mix GNAT tasking and
24796 HP Ada 83 tasking in the same program, since the two run-time
24797 libraries are not compatible.
24798
24799 @node The Task Stack
24800 @subsection The Task Stack
24801
24802 @noindent
24803 In HP Ada, a task stack is allocated each time a
24804 non-passive task is activated. As soon as the task is
24805 terminated, the storage for the task stack is deallocated.
24806 If you specify a size of zero (bytes) with @code{T'STORAGE_SIZE},
24807 a default stack size is used. Also, regardless of the size
24808 specified, some additional space is allocated for task
24809 management purposes. On OpenVMS Alpha systems, at least
24810 one page is allocated.
24811
24812 GNAT handles task stacks in a similar manner. In accordance with
24813 the Ada rules, it provides the pragma @code{STORAGE_SIZE} as
24814 an alternative method for controlling the task stack size.
24815 The specification of the attribute @code{T'STORAGE_SIZE} is also
24816 supported in a manner compatible with HP Ada.
24817
24818 @node External Interrupts
24819 @subsection External Interrupts
24820
24821 @noindent
24822 On HP Ada, external interrupts can be associated with task entries.
24823 GNAT is compatible with HP Ada in its handling of external interrupts.
24824
24825 @node Pragmas and Pragma-Related Features
24826 @section Pragmas and Pragma-Related Features
24827
24828 @noindent
24829 Both HP Ada and GNAT supply all language-defined pragmas
24830 as specified by the Ada 83 standard. GNAT also supplies all
24831 language-defined pragmas introduced by Ada 95 and Ada 2005.
24832 In addition, GNAT implements the implementation-defined pragmas
24833 from HP Ada 83.
24834
24835 @itemize @bullet
24836 @item @code{AST_ENTRY}
24837
24838 @item @code{COMMON_OBJECT}
24839
24840 @item @code{COMPONENT_ALIGNMENT}
24841
24842 @item @code{EXPORT_EXCEPTION}
24843
24844 @item @code{EXPORT_FUNCTION}
24845
24846 @item @code{EXPORT_OBJECT}
24847
24848 @item @code{EXPORT_PROCEDURE}
24849
24850 @item @code{EXPORT_VALUED_PROCEDURE}
24851
24852 @item @code{FLOAT_REPRESENTATION}
24853
24854 @item @code{IDENT}
24855
24856 @item @code{IMPORT_EXCEPTION}
24857
24858 @item @code{IMPORT_FUNCTION}
24859
24860 @item @code{IMPORT_OBJECT}
24861
24862 @item @code{IMPORT_PROCEDURE}
24863
24864 @item @code{IMPORT_VALUED_PROCEDURE}
24865
24866 @item @code{INLINE_GENERIC}
24867
24868 @item @code{INTERFACE_NAME}
24869
24870 @item @code{LONG_FLOAT}
24871
24872 @item @code{MAIN_STORAGE}
24873
24874 @item @code{PASSIVE}
24875
24876 @item @code{PSECT_OBJECT}
24877
24878 @item @code{SHARE_GENERIC}
24879
24880 @item @code{SUPPRESS_ALL}
24881
24882 @item @code{TASK_STORAGE}
24883
24884 @item @code{TIME_SLICE}
24885
24886 @item @code{TITLE}
24887 @end itemize
24888
24889 @noindent
24890 These pragmas are all fully implemented, with the exception of @code{TITLE},
24891 @code{PASSIVE}, and @code{SHARE_GENERIC}, which are
24892 recognized, but which have no
24893 effect in GNAT. The effect of @code{PASSIVE} may be obtained by the
24894 use of Ada protected objects. In GNAT, all generics are inlined.
24895
24896 Unlike HP Ada, the GNAT ``@code{EXPORT_}@i{subprogram}'' pragmas require
24897 a separate subprogram specification which must appear before the
24898 subprogram body.
24899
24900 GNAT also supplies a number of implementation-defined pragmas as follows:
24901 @itemize @bullet
24902 @item @code{ABORT_DEFER}
24903
24904 @item @code{ADA_83}
24905
24906 @item @code{ADA_95}
24907
24908 @item @code{ADA_05}
24909
24910 @item @code{ANNOTATE}
24911
24912 @item @code{ASSERT}
24913
24914 @item @code{C_PASS_BY_COPY}
24915
24916 @item @code{CPP_CLASS}
24917
24918 @item @code{CPP_CONSTRUCTOR}
24919
24920 @item @code{CPP_DESTRUCTOR}
24921
24922 @item @code{DEBUG}
24923
24924 @item @code{EXTEND_SYSTEM}
24925
24926 @item @code{LINKER_ALIAS}
24927
24928 @item @code{LINKER_SECTION}
24929
24930 @item @code{MACHINE_ATTRIBUTE}
24931
24932 @item @code{NO_RETURN}
24933
24934 @item @code{PURE_FUNCTION}
24935
24936 @item @code{SOURCE_FILE_NAME}
24937
24938 @item @code{SOURCE_REFERENCE}
24939
24940 @item @code{TASK_INFO}
24941
24942 @item @code{UNCHECKED_UNION}
24943
24944 @item @code{UNIMPLEMENTED_UNIT}
24945
24946 @item @code{UNIVERSAL_DATA}
24947
24948 @item @code{UNSUPPRESS}
24949
24950 @item @code{WARNINGS}
24951
24952 @item @code{WEAK_EXTERNAL}
24953 @end itemize
24954
24955 @noindent
24956 For full details on these GNAT implementation-defined pragmas,
24957 see @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference
24958 Manual}.
24959
24960 @menu
24961 * Restrictions on the Pragma INLINE::
24962 * Restrictions on the Pragma INTERFACE::
24963 * Restrictions on the Pragma SYSTEM_NAME::
24964 @end menu
24965
24966 @node Restrictions on the Pragma INLINE
24967 @subsection Restrictions on Pragma @code{INLINE}
24968
24969 @noindent
24970 HP Ada enforces the following restrictions on the pragma @code{INLINE}:
24971 @itemize @bullet
24972 @item Parameters cannot have a task type.
24973
24974 @item Function results cannot be task types, unconstrained
24975 array types, or unconstrained types with discriminants.
24976
24977 @item Bodies cannot declare the following:
24978 @itemize @bullet
24979 @item Subprogram body or stub (imported subprogram is allowed)
24980
24981 @item Tasks
24982
24983 @item Generic declarations
24984
24985 @item Instantiations
24986
24987 @item Exceptions
24988
24989 @item Access types (types derived from access types allowed)
24990
24991 @item Array or record types
24992
24993 @item Dependent tasks
24994
24995 @item Direct recursive calls of subprogram or containing
24996 subprogram, directly or via a renaming
24997
24998 @end itemize
24999 @end itemize
25000
25001 @noindent
25002 In GNAT, the only restriction on pragma @code{INLINE} is that the
25003 body must occur before the call if both are in the same
25004 unit, and the size must be appropriately small. There are
25005 no other specific restrictions which cause subprograms to
25006 be incapable of being inlined.
25007
25008 @node Restrictions on the Pragma INTERFACE
25009 @subsection Restrictions on Pragma @code{INTERFACE}
25010
25011 @noindent
25012 The following restrictions on pragma @code{INTERFACE}
25013 are enforced by both HP Ada and GNAT:
25014 @itemize @bullet
25015 @item Languages accepted: Ada, Bliss, C, Fortran, Default.
25016 Default is the default on OpenVMS Alpha systems.
25017
25018 @item Parameter passing: Language specifies default
25019 mechanisms but can be overridden with an @code{EXPORT} pragma.
25020
25021 @itemize @bullet
25022 @item Ada: Use internal Ada rules.
25023
25024 @item Bliss, C: Parameters must be mode @code{in}; cannot be
25025 record or task type. Result cannot be a string, an
25026 array, or a record.
25027
25028 @item Fortran: Parameters cannot have a task type. Result cannot
25029 be a string, an array, or a record.
25030 @end itemize
25031 @end itemize
25032
25033 @noindent
25034 GNAT is entirely upwards compatible with HP Ada, and in addition allows
25035 record parameters for all languages.
25036
25037 @node Restrictions on the Pragma SYSTEM_NAME
25038 @subsection Restrictions on Pragma @code{SYSTEM_NAME}
25039
25040 @noindent
25041 For HP Ada for OpenVMS Alpha, the enumeration literal
25042 for the type @code{NAME} is @code{OPENVMS_AXP}.
25043 In GNAT, the enumeration
25044 literal for the type @code{NAME} is @code{SYSTEM_NAME_GNAT}.
25045
25046 @node Library of Predefined Units
25047 @section Library of Predefined Units
25048
25049 @noindent
25050 A library of predefined units is provided as part of the
25051 HP Ada and GNAT implementations. HP Ada does not provide
25052 the package @code{MACHINE_CODE} but instead recommends importing
25053 assembler code.
25054
25055 The GNAT versions of the HP Ada Run-Time Library (@code{ADA$PREDEFINED:})
25056 units are taken from the OpenVMS Alpha version, not the OpenVMS VAX
25057 version.
25058 The HP Ada Predefined Library units are modified to remove post-Ada 83
25059 incompatibilities and to make them interoperable with GNAT
25060 (@pxref{Changes to DECLIB}, for details).
25061 The units are located in the @file{DECLIB} directory.
25062
25063 The GNAT RTL is contained in
25064 the @file{ADALIB} directory, and
25065 the default search path is set up to find @code{DECLIB} units in preference
25066 to @code{ADALIB} units with the same name (@code{TEXT_IO},
25067 @code{SEQUENTIAL_IO}, and @code{DIRECT_IO}, for example).
25068
25069 @menu
25070 * Changes to DECLIB::
25071 @end menu
25072
25073 @node Changes to DECLIB
25074 @subsection Changes to @code{DECLIB}
25075
25076 @noindent
25077 The changes made to the HP Ada predefined library for GNAT and post-Ada 83
25078 compatibility are minor and include the following:
25079
25080 @itemize @bullet
25081 @item Adjusting the location of pragmas and record representation
25082 clauses to obey Ada 95 (and thus Ada 2005) rules
25083
25084 @item Adding the proper notation to generic formal parameters
25085 that take unconstrained types in instantiation
25086
25087 @item Adding pragma @code{ELABORATE_BODY} to package specs
25088 that have package bodies not otherwise allowed
25089
25090 @item Replacing occurrences of the identifier ``@code{PROTECTED}'' by
25091 ``@code{PROTECTD}''.
25092 Currently these are found only in the @code{STARLET} package spec.
25093
25094 @item Changing @code{SYSTEM.ADDRESS} to @code{SYSTEM.SHORT_ADDRESS}
25095 where the address size is constrained to 32 bits.
25096 @end itemize
25097
25098 @noindent
25099 None of the above changes is visible to users.
25100
25101 @node Bindings
25102 @section Bindings
25103
25104 @noindent
25105 On OpenVMS Alpha, HP Ada provides the following strongly-typed bindings:
25106 @itemize @bullet
25107
25108 @item Command Language Interpreter (CLI interface)
25109
25110 @item DECtalk Run-Time Library (DTK interface)
25111
25112 @item Librarian utility routines (LBR interface)
25113
25114 @item General Purpose Run-Time Library (LIB interface)
25115
25116 @item Math Run-Time Library (MTH interface)
25117
25118 @item National Character Set Run-Time Library (NCS interface)
25119
25120 @item Compiled Code Support Run-Time Library (OTS interface)
25121
25122 @item Parallel Processing Run-Time Library (PPL interface)
25123
25124 @item Screen Management Run-Time Library (SMG interface)
25125
25126 @item Sort Run-Time Library (SOR interface)
25127
25128 @item String Run-Time Library (STR interface)
25129
25130 @item STARLET System Library
25131 @findex Starlet
25132
25133 @item X Window System Version 11R4 and 11R5 (X, XLIB interface)
25134
25135 @item X Windows Toolkit (XT interface)
25136
25137 @item X/Motif Version 1.1.3 and 1.2 (XM interface)
25138 @end itemize
25139
25140 @noindent
25141 GNAT provides implementations of these HP bindings in the @code{DECLIB}
25142 directory, on both the Alpha and I64 OpenVMS platforms.
25143
25144 The X/Motif bindings used to build @code{DECLIB} are whatever versions are
25145 in the
25146 HP Ada @file{ADA$PREDEFINED} directory with extension @file{.ADC}.
25147 A pragma @code{Linker_Options} has been added to packages @code{Xm},
25148 @code{Xt}, and @code{X_Lib}
25149 causing the default X/Motif sharable image libraries to be linked in. This
25150 is done via options files named @file{xm.opt}, @file{xt.opt}, and
25151 @file{x_lib.opt} (also located in the @file{DECLIB} directory).
25152
25153 It may be necessary to edit these options files to update or correct the
25154 library names if, for example, the newer X/Motif bindings from
25155 @file{ADA$EXAMPLES}
25156 had been (previous to installing GNAT) copied and renamed to supersede the
25157 default @file{ADA$PREDEFINED} versions.
25158
25159 @menu
25160 * Shared Libraries and Options Files::
25161 * Interfaces to C::
25162 @end menu
25163
25164 @node Shared Libraries and Options Files
25165 @subsection Shared Libraries and Options Files
25166
25167 @noindent
25168 When using the HP Ada
25169 predefined X and Motif bindings, the linking with their sharable images is
25170 done automatically by @command{GNAT LINK}.
25171 When using other X and Motif bindings, you need
25172 to add the corresponding sharable images to the command line for
25173 @code{GNAT LINK}. When linking with shared libraries, or with
25174 @file{.OPT} files, you must
25175 also add them to the command line for @command{GNAT LINK}.
25176
25177 A shared library to be used with GNAT is built in the same way as other
25178 libraries under VMS. The VMS Link command can be used in standard fashion.
25179
25180 @node Interfaces to C
25181 @subsection Interfaces to C
25182
25183 @noindent
25184 HP Ada
25185 provides the following Ada types and operations:
25186
25187 @itemize @bullet
25188 @item C types package (@code{C_TYPES})
25189
25190 @item C strings (@code{C_TYPES.NULL_TERMINATED})
25191
25192 @item Other_types (@code{SHORT_INT})
25193 @end itemize
25194
25195 @noindent
25196 Interfacing to C with GNAT, you can use the above approach
25197 described for HP Ada or the facilities of Annex B of
25198 the @cite{Ada Reference Manual} (packages @code{INTERFACES.C},
25199 @code{INTERFACES.C.STRINGS} and @code{INTERFACES.C.POINTERS}). For more
25200 information, see @ref{Interfacing to C,,, gnat_rm, GNAT Reference Manual}.
25201
25202 The @option{-gnatF} qualifier forces default and explicit
25203 @code{External_Name} parameters in pragmas @code{Import} and @code{Export}
25204 to be uppercased for compatibility with the default behavior
25205 of HP C. The qualifier has no effect on @code{Link_Name} parameters.
25206
25207 @node Main Program Definition
25208 @section Main Program Definition
25209
25210 @noindent
25211 The following section discusses differences in the
25212 definition of main programs on HP Ada and GNAT.
25213 On HP Ada, main programs are defined to meet the
25214 following conditions:
25215 @itemize @bullet
25216 @item Procedure with no formal parameters (returns @code{0} upon
25217 normal completion)
25218
25219 @item Procedure with no formal parameters (returns @code{42} when
25220 an unhandled exception is raised)
25221
25222 @item Function with no formal parameters whose returned value
25223 is of a discrete type
25224
25225 @item Procedure with one @code{out} formal of a discrete type for
25226 which a specification of pragma @code{EXPORT_VALUED_PROCEDURE} is given.
25227
25228 @end itemize
25229
25230 @noindent
25231 When declared with the pragma @code{EXPORT_VALUED_PROCEDURE},
25232 a main function or main procedure returns a discrete
25233 value whose size is less than 64 bits (32 on VAX systems),
25234 the value is zero- or sign-extended as appropriate.
25235 On GNAT, main programs are defined as follows:
25236 @itemize @bullet
25237 @item Must be a non-generic, parameterless subprogram that
25238 is either a procedure or function returning an Ada
25239 @code{STANDARD.INTEGER} (the predefined type)
25240
25241 @item Cannot be a generic subprogram or an instantiation of a
25242 generic subprogram
25243 @end itemize
25244
25245 @node Implementation-Defined Attributes
25246 @section Implementation-Defined Attributes
25247
25248 @noindent
25249 GNAT provides all HP Ada implementation-defined
25250 attributes.
25251
25252 @node Compiler and Run-Time Interfacing
25253 @section Compiler and Run-Time Interfacing
25254
25255 @noindent
25256 HP Ada provides the following qualifiers to pass options to the linker
25257 (ACS LINK):
25258 @itemize @bullet
25259 @item @option{/WAIT} and @option{/SUBMIT}
25260
25261 @item @option{/COMMAND}
25262
25263 @item @option{/@r{[}NO@r{]}MAP}
25264
25265 @item @option{/OUTPUT=@var{file-spec}}
25266
25267 @item @option{/@r{[}NO@r{]}DEBUG} and @option{/@r{[}NO@r{]}TRACEBACK}
25268 @end itemize
25269
25270 @noindent
25271 To pass options to the linker, GNAT provides the following
25272 switches:
25273
25274 @itemize @bullet
25275 @item @option{/EXECUTABLE=@var{exec-name}}
25276
25277 @item @option{/VERBOSE}
25278
25279 @item @option{/@r{[}NO@r{]}DEBUG} and @option{/@r{[}NO@r{]}TRACEBACK}
25280 @end itemize
25281
25282 @noindent
25283 For more information on these switches, see
25284 @ref{Switches for gnatlink}.
25285 In HP Ada, the command-line switch @option{/OPTIMIZE} is available
25286 to control optimization. HP Ada also supplies the
25287 following pragmas:
25288 @itemize @bullet
25289 @item @code{OPTIMIZE}
25290
25291 @item @code{INLINE}
25292
25293 @item @code{INLINE_GENERIC}
25294
25295 @item @code{SUPPRESS_ALL}
25296
25297 @item @code{PASSIVE}
25298 @end itemize
25299
25300 @noindent
25301 In GNAT, optimization is controlled strictly by command
25302 line parameters, as described in the corresponding section of this guide.
25303 The HP pragmas for control of optimization are
25304 recognized but ignored.
25305
25306 Note that in GNAT, the default is optimization off, whereas in HP Ada
25307 the default is that optimization is turned on.
25308
25309 @node Program Compilation and Library Management
25310 @section Program Compilation and Library Management
25311
25312 @noindent
25313 HP Ada and GNAT provide a comparable set of commands to
25314 build programs. HP Ada also provides a program library,
25315 which is a concept that does not exist on GNAT. Instead,
25316 GNAT provides directories of sources that are compiled as
25317 needed.
25318
25319 The following table summarizes
25320 the HP Ada commands and provides
25321 equivalent GNAT commands. In this table, some GNAT
25322 equivalents reflect the fact that GNAT does not use the
25323 concept of a program library. Instead, it uses a model
25324 in which collections of source and object files are used
25325 in a manner consistent with other languages like C and
25326 Fortran. Therefore, standard system file commands are used
25327 to manipulate these elements. Those GNAT commands are marked with
25328 an asterisk.
25329 Note that, unlike HP Ada, none of the GNAT commands accepts wild cards.
25330
25331 @need 1500
25332 @multitable @columnfractions .35 .65
25333
25334 @item @emph{HP Ada Command}
25335 @tab @emph{GNAT Equivalent / Description}
25336
25337 @item @command{ADA}
25338 @tab @command{GNAT COMPILE}@*
25339 Invokes the compiler to compile one or more Ada source files.
25340
25341 @item @command{ACS ATTACH}@*
25342 @tab [No equivalent]@*
25343 Switches control of terminal from current process running the program
25344 library manager.
25345
25346 @item @command{ACS CHECK}
25347 @tab @command{GNAT MAKE /DEPENDENCY_LIST}@*
25348 Forms the execution closure of one
25349 or more compiled units and checks completeness and currency.
25350
25351 @item @command{ACS COMPILE}
25352 @tab @command{GNAT MAKE /ACTIONS=COMPILE}@*
25353 Forms the execution closure of one or
25354 more specified units, checks completeness and currency,
25355 identifies units that have revised source files, compiles same,
25356 and recompiles units that are or will become obsolete.
25357 Also completes incomplete generic instantiations.
25358
25359 @item @command{ACS COPY FOREIGN}
25360 @tab Copy (*)@*
25361 Copies a foreign object file into the program library as a
25362 library unit body.
25363
25364 @item @command{ACS COPY UNIT}
25365 @tab Copy (*)@*
25366 Copies a compiled unit from one program library to another.
25367
25368 @item @command{ACS CREATE LIBRARY}
25369 @tab Create /directory (*)@*
25370 Creates a program library.
25371
25372 @item @command{ACS CREATE SUBLIBRARY}
25373 @tab Create /directory (*)@*
25374 Creates a program sublibrary.
25375
25376 @item @command{ACS DELETE LIBRARY}
25377 @tab @*
25378 Deletes a program library and its contents.
25379
25380 @item @command{ACS DELETE SUBLIBRARY}
25381 @tab @*
25382 Deletes a program sublibrary and its contents.
25383
25384 @item @command{ACS DELETE UNIT}
25385 @tab Delete file (*)@*
25386 On OpenVMS systems, deletes one or more compiled units from
25387 the current program library.
25388
25389 @item @command{ACS DIRECTORY}
25390 @tab Directory (*)@*
25391 On OpenVMS systems, lists units contained in the current
25392 program library.
25393
25394 @item @command{ACS ENTER FOREIGN}
25395 @tab Copy (*)@*
25396 Allows the import of a foreign body as an Ada library
25397 spec and enters a reference to a pointer.
25398
25399 @item @command{ACS ENTER UNIT}
25400 @tab Copy (*)@*
25401 Enters a reference (pointer) from the current program library to
25402 a unit compiled into another program library.
25403
25404 @item @command{ACS EXIT}
25405 @tab [No equivalent]@*
25406 Exits from the program library manager.
25407
25408 @item @command{ACS EXPORT}
25409 @tab Copy (*)@*
25410 Creates an object file that contains system-specific object code
25411 for one or more units. With GNAT, object files can simply be copied
25412 into the desired directory.
25413
25414 @item @command{ACS EXTRACT SOURCE}
25415 @tab Copy (*)@*
25416 Allows access to the copied source file for each Ada compilation unit
25417
25418 @item @command{ACS HELP}
25419 @tab @command{HELP GNAT}@*
25420 Provides online help.
25421
25422 @item @command{ACS LINK}
25423 @tab @command{GNAT LINK}@*
25424 Links an object file containing Ada units into an executable file.
25425
25426 @item @command{ACS LOAD}
25427 @tab Copy (*)@*
25428 Loads (partially compiles) Ada units into the program library.
25429 Allows loading a program from a collection of files into a library
25430 without knowing the relationship among units.
25431
25432 @item @command{ACS MERGE}
25433 @tab Copy (*)@*
25434 Merges into the current program library, one or more units from
25435 another library where they were modified.
25436
25437 @item @command{ACS RECOMPILE}
25438 @tab @command{GNAT MAKE /ACTIONS=COMPILE}@*
25439 Recompiles from external or copied source files any obsolete
25440 unit in the closure. Also, completes any incomplete generic
25441 instantiations.
25442
25443 @item @command{ACS REENTER}
25444 @tab @command{GNAT MAKE}@*
25445 Reenters current references to units compiled after last entered
25446 with the @command{ACS ENTER UNIT} command.
25447
25448 @item @command{ACS SET LIBRARY}
25449 @tab Set default (*)@*
25450 Defines a program library to be the compilation context as well
25451 as the target library for compiler output and commands in general.
25452
25453 @item @command{ACS SET PRAGMA}
25454 @tab Edit @file{gnat.adc} (*)@*
25455 Redefines specified values of the library characteristics
25456 @code{LONG_ FLOAT}, @code{MEMORY_SIZE}, @code{SYSTEM_NAME},
25457 and @code{Float_Representation}.
25458
25459 @item @command{ACS SET SOURCE}
25460 @tab Define @code{ADA_INCLUDE_PATH} path (*)@*
25461 Defines the source file search list for the @command{ACS COMPILE} command.
25462
25463 @item @command{ACS SHOW LIBRARY}
25464 @tab Directory (*)@*
25465 Lists information about one or more program libraries.
25466
25467 @item @command{ACS SHOW PROGRAM}
25468 @tab [No equivalent]@*
25469 Lists information about the execution closure of one or
25470 more units in the program library.
25471
25472 @item @command{ACS SHOW SOURCE}
25473 @tab Show logical @code{ADA_INCLUDE_PATH}@*
25474 Shows the source file search used when compiling units.
25475
25476 @item @command{ACS SHOW VERSION}
25477 @tab Compile with @option{VERBOSE} option
25478 Displays the version number of the compiler and program library
25479 manager used.
25480
25481 @item @command{ACS SPAWN}
25482 @tab [No equivalent]@*
25483 Creates a subprocess of the current process (same as @command{DCL SPAWN}
25484 command).
25485
25486 @item @command{ACS VERIFY}
25487 @tab [No equivalent]@*
25488 Performs a series of consistency checks on a program library to
25489 determine whether the library structure and library files are in
25490 valid form.
25491 @end multitable
25492
25493 @noindent
25494
25495 @node Input-Output
25496 @section Input-Output
25497
25498 @noindent
25499 On OpenVMS Alpha systems, HP Ada uses OpenVMS Record
25500 Management Services (RMS) to perform operations on
25501 external files.
25502
25503 @noindent
25504 HP Ada and GNAT predefine an identical set of input-
25505 output packages. To make the use of the
25506 generic @code{TEXT_IO} operations more convenient, HP Ada
25507 provides predefined library packages that instantiate the
25508 integer and floating-point operations for the predefined
25509 integer and floating-point types as shown in the following table.
25510
25511 @multitable @columnfractions .45 .55
25512 @item @emph{Package Name} @tab Instantiation
25513
25514 @item @code{INTEGER_TEXT_IO}
25515 @tab @code{INTEGER_IO(INTEGER)}
25516
25517 @item @code{SHORT_INTEGER_TEXT_IO}
25518 @tab @code{INTEGER_IO(SHORT_INTEGER)}
25519
25520 @item @code{SHORT_SHORT_INTEGER_TEXT_IO}
25521 @tab @code{INTEGER_IO(SHORT_SHORT_INTEGER)}
25522
25523 @item @code{FLOAT_TEXT_IO}
25524 @tab @code{FLOAT_IO(FLOAT)}
25525
25526 @item @code{LONG_FLOAT_TEXT_IO}
25527 @tab @code{FLOAT_IO(LONG_FLOAT)}
25528 @end multitable
25529
25530 @noindent
25531 The HP Ada predefined packages and their operations
25532 are implemented using OpenVMS Alpha files and input-output
25533 facilities. HP Ada supports asynchronous input-output on OpenVMS Alpha.
25534 Familiarity with the following is recommended:
25535 @itemize @bullet
25536 @item RMS file organizations and access methods
25537
25538 @item OpenVMS file specifications and directories
25539
25540 @item OpenVMS File Definition Language (FDL)
25541 @end itemize
25542
25543 @noindent
25544 GNAT provides I/O facilities that are completely
25545 compatible with HP Ada. The distribution includes the
25546 standard HP Ada versions of all I/O packages, operating
25547 in a manner compatible with HP Ada. In particular, the
25548 following packages are by default the HP Ada (Ada 83)
25549 versions of these packages rather than the renamings
25550 suggested in Annex J of the Ada Reference Manual:
25551 @itemize @bullet
25552 @item @code{TEXT_IO}
25553
25554 @item @code{SEQUENTIAL_IO}
25555
25556 @item @code{DIRECT_IO}
25557 @end itemize
25558
25559 @noindent
25560 The use of the standard child package syntax (for
25561 example, @code{ADA.TEXT_IO}) retrieves the post-Ada 83 versions of these
25562 packages.
25563 GNAT provides HP-compatible predefined instantiations
25564 of the @code{TEXT_IO} packages, and also
25565 provides the standard predefined instantiations required
25566 by the @cite{Ada Reference Manual}.
25567
25568 For further information on how GNAT interfaces to the file
25569 system or how I/O is implemented in programs written in
25570 mixed languages, see @ref{Implementation of the Standard I/O,,,
25571 gnat_rm, GNAT Reference Manual}.
25572 This chapter covers the following:
25573 @itemize @bullet
25574 @item Standard I/O packages
25575
25576 @item @code{FORM} strings
25577
25578 @item @code{ADA.DIRECT_IO}
25579
25580 @item @code{ADA.SEQUENTIAL_IO}
25581
25582 @item @code{ADA.TEXT_IO}
25583
25584 @item Stream pointer positioning
25585
25586 @item Reading and writing non-regular files
25587
25588 @item @code{GET_IMMEDIATE}
25589
25590 @item Treating @code{TEXT_IO} files as streams
25591
25592 @item Shared files
25593
25594 @item Open modes
25595 @end itemize
25596
25597 @node Implementation Limits
25598 @section Implementation Limits
25599
25600 @noindent
25601 The following table lists implementation limits for HP Ada
25602 and GNAT systems.
25603 @multitable @columnfractions .60 .20 .20
25604 @sp 1
25605 @item @emph{Compilation Parameter}
25606 @tab @emph{HP Ada}
25607 @tab @emph{GNAT}
25608 @sp 1
25609
25610 @item In a subprogram or entry declaration, maximum number of
25611 formal parameters that are of an unconstrained record type
25612 @tab 32
25613 @tab No set limit
25614 @sp 1
25615
25616 @item Maximum identifier length (number of characters)
25617 @tab 255
25618 @tab 32766
25619 @sp 1
25620
25621 @item Maximum number of characters in a source line
25622 @tab 255
25623 @tab 32766
25624 @sp 1
25625
25626 @item Maximum collection size (number of bytes)
25627 @tab 2**31-1
25628 @tab 2**31-1
25629 @sp 1
25630
25631 @item Maximum number of discriminants for a record type
25632 @tab 245
25633 @tab No set limit
25634 @sp 1
25635
25636 @item Maximum number of formal parameters in an entry or
25637 subprogram declaration
25638 @tab 246
25639 @tab No set limit
25640 @sp 1
25641
25642 @item Maximum number of dimensions in an array type
25643 @tab 255
25644 @tab No set limit
25645 @sp 1
25646
25647 @item Maximum number of library units and subunits in a compilation.
25648 @tab 4095
25649 @tab No set limit
25650 @sp 1
25651
25652 @item Maximum number of library units and subunits in an execution.
25653 @tab 16383
25654 @tab No set limit
25655 @sp 1
25656
25657 @item Maximum number of objects declared with the pragma @code{COMMON_OBJECT}
25658 or @code{PSECT_OBJECT}
25659 @tab 32757
25660 @tab No set limit
25661 @sp 1
25662
25663 @item Maximum number of enumeration literals in an enumeration type
25664 definition
25665 @tab 65535
25666 @tab No set limit
25667 @sp 1
25668
25669 @item Maximum number of lines in a source file
25670 @tab 65534
25671 @tab No set limit
25672 @sp 1
25673
25674 @item Maximum number of bits in any object
25675 @tab 2**31-1
25676 @tab 2**31-1
25677 @sp 1
25678
25679 @item Maximum size of the static portion of a stack frame (approximate)
25680 @tab 2**31-1
25681 @tab 2**31-1
25682 @end multitable
25683
25684 @node Tools and Utilities
25685 @section Tools and Utilities
25686
25687 @noindent
25688 The following table lists some of the OpenVMS development tools
25689 available for HP Ada, and the corresponding tools for
25690 use with @value{EDITION} on Alpha and I64 platforms.
25691 Aside from the debugger, all the OpenVMS tools identified are part
25692 of the DECset package.
25693
25694 @iftex
25695 @c Specify table in TeX since Texinfo does a poor job
25696 @tex
25697 \smallskip
25698 \smallskip
25699 \settabs\+Language-Sensitive Editor\quad
25700 &Product with HP Ada\quad
25701 &\cr
25702 \+\it Tool
25703 &\it Product with HP Ada
25704 & \it Product with GNAT Pro\cr
25705 \smallskip
25706 \+Code Management System
25707 &HP CMS
25708 & HP CMS\cr
25709 \smallskip
25710 \+Language-Sensitive Editor
25711 &HP LSE
25712 & emacs or HP LSE (Alpha)\cr
25713 \+
25714 &
25715 & HP LSE (I64)\cr
25716 \smallskip
25717 \+Debugger
25718 &OpenVMS Debug
25719 & gdb (Alpha),\cr
25720 \+
25721 &
25722 & OpenVMS Debug (I64)\cr
25723 \smallskip
25724 \+Source Code Analyzer /
25725 &HP SCA
25726 & GNAT XREF\cr
25727 \+Cross Referencer
25728 &
25729 &\cr
25730 \smallskip
25731 \+Test Manager
25732 &HP Digital Test
25733 & HP DTM\cr
25734 \+
25735 &Manager (DTM)
25736 &\cr
25737 \smallskip
25738 \+Performance and
25739 & HP PCA
25740 & HP PCA\cr
25741 \+Coverage Analyzer
25742 &
25743 &\cr
25744 \smallskip
25745 \+Module Management
25746 & HP MMS
25747 & Not applicable\cr
25748 \+ System
25749 &
25750 &\cr
25751 \smallskip
25752 \smallskip
25753 @end tex
25754 @end iftex
25755
25756 @ifnottex
25757 @c This is the Texinfo version of the table. It renders poorly in pdf, hence
25758 @c the TeX version above for the printed version
25759 @flushleft
25760 @c @multitable @columnfractions .3 .4 .4
25761 @multitable {Source Code Analyzer /}{Tool with HP Ada}{Tool with GNAT Pro}
25762 @item @i{Tool}
25763 @tab @i{Tool with HP Ada}
25764 @tab @i{Tool with @value{EDITION}}
25765 @item Code Management@*System
25766 @tab HP CMS
25767 @tab HP CMS
25768 @item Language-Sensitive@*Editor
25769 @tab HP LSE
25770 @tab emacs or HP LSE (Alpha)
25771 @item
25772 @tab
25773 @tab HP LSE (I64)
25774 @item Debugger
25775 @tab OpenVMS Debug
25776 @tab gdb (Alpha),
25777 @item
25778 @tab
25779 @tab OpenVMS Debug (I64)
25780 @item Source Code Analyzer /@*Cross Referencer
25781 @tab HP SCA
25782 @tab GNAT XREF
25783 @item Test Manager
25784 @tab HP Digital Test@*Manager (DTM)
25785 @tab HP DTM
25786 @item Performance and@*Coverage Analyzer
25787 @tab HP PCA
25788 @tab HP PCA
25789 @item Module Management@*System
25790 @tab HP MMS
25791 @tab Not applicable
25792 @end multitable
25793 @end flushleft
25794 @end ifnottex
25795
25796 @end ifset
25797
25798 @c **************************************
25799 @node Platform-Specific Information for the Run-Time Libraries
25800 @appendix Platform-Specific Information for the Run-Time Libraries
25801 @cindex Tasking and threads libraries
25802 @cindex Threads libraries and tasking
25803 @cindex Run-time libraries (platform-specific information)
25804
25805 @noindent
25806 The GNAT run-time implementation may vary with respect to both the
25807 underlying threads library and the exception handling scheme.
25808 For threads support, one or more of the following are supplied:
25809 @itemize @bullet
25810 @item @b{native threads library}, a binding to the thread package from
25811 the underlying operating system
25812
25813 @item @b{pthreads library} (Sparc Solaris only), a binding to the Solaris
25814 POSIX thread package
25815 @end itemize
25816
25817 @noindent
25818 For exception handling, either or both of two models are supplied:
25819 @itemize @bullet
25820 @item @b{Zero-Cost Exceptions} (``ZCX''),@footnote{
25821 Most programs should experience a substantial speed improvement by
25822 being compiled with a ZCX run-time.
25823 This is especially true for
25824 tasking applications or applications with many exception handlers.}
25825 @cindex Zero-Cost Exceptions
25826 @cindex ZCX (Zero-Cost Exceptions)
25827 which uses binder-generated tables that
25828 are interrogated at run time to locate a handler
25829
25830 @item @b{setjmp / longjmp} (``SJLJ''),
25831 @cindex setjmp/longjmp Exception Model
25832 @cindex SJLJ (setjmp/longjmp Exception Model)
25833 which uses dynamically-set data to establish
25834 the set of handlers
25835 @end itemize
25836
25837 @noindent
25838 This appendix summarizes which combinations of threads and exception support
25839 are supplied on various GNAT platforms.
25840 It then shows how to select a particular library either
25841 permanently or temporarily,
25842 explains the properties of (and tradeoffs among) the various threads
25843 libraries, and provides some additional
25844 information about several specific platforms.
25845
25846 @menu
25847 * Summary of Run-Time Configurations::
25848 * Specifying a Run-Time Library::
25849 * Choosing the Scheduling Policy::
25850 * Solaris-Specific Considerations::
25851 * Linux-Specific Considerations::
25852 * AIX-Specific Considerations::
25853 * Irix-Specific Considerations::
25854 * RTX-Specific Considerations::
25855 @end menu
25856
25857 @node Summary of Run-Time Configurations
25858 @section Summary of Run-Time Configurations
25859
25860 @multitable @columnfractions .30 .70
25861 @item @b{alpha-openvms}
25862 @item @code{@ @ }@i{rts-native (default)}
25863 @item @code{@ @ @ @ }Tasking @tab native VMS threads
25864 @item @code{@ @ @ @ }Exceptions @tab ZCX
25865 @*
25866 @item @b{alpha-tru64}
25867 @item @code{@ @ }@i{rts-native (default)}
25868 @item @code{@ @ @ @ }Tasking @tab native TRU64 threads
25869 @item @code{@ @ @ @ }Exceptions @tab ZCX
25870 @*
25871 @item @code{@ @ }@i{rts-sjlj}
25872 @item @code{@ @ @ @ }Tasking @tab native TRU64 threads
25873 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25874 @*
25875 @item @b{ia64-hp_linux}
25876 @item @code{@ @ }@i{rts-native (default)}
25877 @item @code{@ @ @ @ }Tasking @tab pthread library
25878 @item @code{@ @ @ @ }Exceptions @tab ZCX
25879 @*
25880 @item @b{ia64-hpux}
25881 @item @code{@ @ }@i{rts-native (default)}
25882 @item @code{@ @ @ @ }Tasking @tab native HP-UX threads
25883 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25884 @*
25885 @item @b{ia64-openvms}
25886 @item @code{@ @ }@i{rts-native (default)}
25887 @item @code{@ @ @ @ }Tasking @tab native VMS threads
25888 @item @code{@ @ @ @ }Exceptions @tab ZCX
25889 @*
25890 @item @b{ia64-sgi_linux}
25891 @item @code{@ @ }@i{rts-native (default)}
25892 @item @code{@ @ @ @ }Tasking @tab pthread library
25893 @item @code{@ @ @ @ }Exceptions @tab ZCX
25894 @*
25895 @item @b{mips-irix}
25896 @item @code{@ @ }@i{rts-native (default)}
25897 @item @code{@ @ @ @ }Tasking @tab native IRIX threads
25898 @item @code{@ @ @ @ }Exceptions @tab ZCX
25899 @*
25900 @item @b{pa-hpux}
25901 @item @code{@ @ }@i{rts-native (default)}
25902 @item @code{@ @ @ @ }Tasking @tab native HP-UX threads
25903 @item @code{@ @ @ @ }Exceptions @tab ZCX
25904 @*
25905 @item @code{@ @ }@i{rts-sjlj}
25906 @item @code{@ @ @ @ }Tasking @tab native HP-UX threads
25907 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25908 @*
25909 @item @b{ppc-aix}
25910 @item @code{@ @ }@i{rts-native (default)}
25911 @item @code{@ @ @ @ }Tasking @tab native AIX threads
25912 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25913 @*
25914 @item @b{ppc-darwin}
25915 @item @code{@ @ }@i{rts-native (default)}
25916 @item @code{@ @ @ @ }Tasking @tab native MacOS threads
25917 @item @code{@ @ @ @ }Exceptions @tab ZCX
25918 @*
25919 @item @b{sparc-solaris} @tab
25920 @item @code{@ @ }@i{rts-native (default)}
25921 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
25922 @item @code{@ @ @ @ }Exceptions @tab ZCX
25923 @*
25924 @item @code{@ @ }@i{rts-pthread}
25925 @item @code{@ @ @ @ }Tasking @tab pthread library
25926 @item @code{@ @ @ @ }Exceptions @tab ZCX
25927 @*
25928 @item @code{@ @ }@i{rts-sjlj}
25929 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
25930 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25931 @*
25932 @item @b{sparc64-solaris} @tab
25933 @item @code{@ @ }@i{rts-native (default)}
25934 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
25935 @item @code{@ @ @ @ }Exceptions @tab ZCX
25936 @*
25937 @item @b{x86-linux}
25938 @item @code{@ @ }@i{rts-native (default)}
25939 @item @code{@ @ @ @ }Tasking @tab pthread library
25940 @item @code{@ @ @ @ }Exceptions @tab ZCX
25941 @*
25942 @item @code{@ @ }@i{rts-sjlj}
25943 @item @code{@ @ @ @ }Tasking @tab pthread library
25944 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25945 @*
25946 @item @b{x86-lynx}
25947 @item @code{@ @ }@i{rts-native (default)}
25948 @item @code{@ @ @ @ }Tasking @tab native LynxOS threads
25949 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25950 @*
25951 @item @b{x86-solaris}
25952 @item @code{@ @ }@i{rts-native (default)}
25953 @item @code{@ @ @ @ }Tasking @tab native Solaris threads
25954 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25955 @*
25956 @item @b{x86-windows}
25957 @item @code{@ @ }@i{rts-native (default)}
25958 @item @code{@ @ @ @ }Tasking @tab native Win32 threads
25959 @item @code{@ @ @ @ }Exceptions @tab ZCX
25960 @*
25961 @item @code{@ @ }@i{rts-sjlj (default)}
25962 @item @code{@ @ @ @ }Tasking @tab native Win32 threads
25963 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25964 @*
25965 @item @b{x86-windows-rtx}
25966 @item @code{@ @ }@i{rts-rtx-rtss (default)}
25967 @item @code{@ @ @ @ }Tasking @tab RTX real-time subsystem RTSS threads (kernel mode)
25968 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25969 @*
25970 @item @code{@ @ }@i{rts-rtx-w32}
25971 @item @code{@ @ @ @ }Tasking @tab RTX Win32 threads (user mode)
25972 @item @code{@ @ @ @ }Exceptions @tab ZCX
25973 @*
25974 @item @b{x86_64-linux}
25975 @item @code{@ @ }@i{rts-native (default)}
25976 @item @code{@ @ @ @ }Tasking @tab pthread library
25977 @item @code{@ @ @ @ }Exceptions @tab ZCX
25978 @*
25979 @item @code{@ @ }@i{rts-sjlj}
25980 @item @code{@ @ @ @ }Tasking @tab pthread library
25981 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25982 @*
25983 @end multitable
25984
25985 @node Specifying a Run-Time Library
25986 @section Specifying a Run-Time Library
25987
25988 @noindent
25989 The @file{adainclude} subdirectory containing the sources of the GNAT
25990 run-time library, and the @file{adalib} subdirectory containing the
25991 @file{ALI} files and the static and/or shared GNAT library, are located
25992 in the gcc target-dependent area:
25993
25994 @smallexample
25995 target=$prefix/lib/gcc/gcc-@i{dumpmachine}/gcc-@i{dumpversion}/
25996 @end smallexample
25997
25998 @noindent
25999 As indicated above, on some platforms several run-time libraries are supplied.
26000 These libraries are installed in the target dependent area and
26001 contain a complete source and binary subdirectory. The detailed description
26002 below explains the differences between the different libraries in terms of
26003 their thread support.
26004
26005 The default run-time library (when GNAT is installed) is @emph{rts-native}.
26006 This default run time is selected by the means of soft links.
26007 For example on x86-linux:
26008
26009 @smallexample
26010 @group
26011 $(target-dir)
26012 |
26013 +--- adainclude----------+
26014 | |
26015 +--- adalib-----------+ |
26016 | | |
26017 +--- rts-native | |
26018 | | | |
26019 | +--- adainclude <---+
26020 | | |
26021 | +--- adalib <----+
26022 |
26023 +--- rts-sjlj
26024 |
26025 +--- adainclude
26026 |
26027 +--- adalib
26028 @end group
26029 @end smallexample
26030
26031 @noindent
26032 If the @i{rts-sjlj} library is to be selected on a permanent basis,
26033 these soft links can be modified with the following commands:
26034
26035 @smallexample
26036 $ cd $target
26037 $ rm -f adainclude adalib
26038 $ ln -s rts-sjlj/adainclude adainclude
26039 $ ln -s rts-sjlj/adalib adalib
26040 @end smallexample
26041
26042 @noindent
26043 Alternatively, you can specify @file{rts-sjlj/adainclude} in the file
26044 @file{$target/ada_source_path} and @file{rts-sjlj/adalib} in
26045 @file{$target/ada_object_path}.
26046
26047 Selecting another run-time library temporarily can be
26048 achieved by using the @option{--RTS} switch, e.g., @option{--RTS=sjlj}
26049 @cindex @option{--RTS} option
26050
26051 @node Choosing the Scheduling Policy
26052 @section Choosing the Scheduling Policy
26053
26054 @noindent
26055 When using a POSIX threads implementation, you have a choice of several
26056 scheduling policies: @code{SCHED_FIFO},
26057 @cindex @code{SCHED_FIFO} scheduling policy
26058 @code{SCHED_RR}
26059 @cindex @code{SCHED_RR} scheduling policy
26060 and @code{SCHED_OTHER}.
26061 @cindex @code{SCHED_OTHER} scheduling policy
26062 Typically, the default is @code{SCHED_OTHER}, while using @code{SCHED_FIFO}
26063 or @code{SCHED_RR} requires special (e.g., root) privileges.
26064
26065 By default, GNAT uses the @code{SCHED_OTHER} policy. To specify
26066 @code{SCHED_FIFO},
26067 @cindex @code{SCHED_FIFO} scheduling policy
26068 you can use one of the following:
26069
26070 @itemize @bullet
26071 @item
26072 @code{pragma Time_Slice (0.0)}
26073 @cindex pragma Time_Slice
26074 @item
26075 the corresponding binder option @option{-T0}
26076 @cindex @option{-T0} option
26077 @item
26078 @code{pragma Task_Dispatching_Policy (FIFO_Within_Priorities)}
26079 @cindex pragma Task_Dispatching_Policy
26080 @end itemize
26081
26082 @noindent
26083 To specify @code{SCHED_RR},
26084 @cindex @code{SCHED_RR} scheduling policy
26085 you should use @code{pragma Time_Slice} with a
26086 value greater than @code{0.0}, or else use the corresponding @option{-T}
26087 binder option.
26088
26089 @node Solaris-Specific Considerations
26090 @section Solaris-Specific Considerations
26091 @cindex Solaris Sparc threads libraries
26092
26093 @noindent
26094 This section addresses some topics related to the various threads libraries
26095 on Sparc Solaris.
26096
26097 @menu
26098 * Solaris Threads Issues::
26099 @end menu
26100
26101 @node Solaris Threads Issues
26102 @subsection Solaris Threads Issues
26103
26104 @noindent
26105 GNAT under Solaris/Sparc 32 bits comes with an alternate tasking run-time
26106 library based on POSIX threads --- @emph{rts-pthread}.
26107 @cindex rts-pthread threads library
26108 This run-time library has the advantage of being mostly shared across all
26109 POSIX-compliant thread implementations, and it also provides under
26110 @w{Solaris 8} the @code{PTHREAD_PRIO_INHERIT}
26111 @cindex @code{PTHREAD_PRIO_INHERIT} policy (under rts-pthread)
26112 and @code{PTHREAD_PRIO_PROTECT}
26113 @cindex @code{PTHREAD_PRIO_PROTECT} policy (under rts-pthread)
26114 semantics that can be selected using the predefined pragma
26115 @code{Locking_Policy}
26116 @cindex pragma Locking_Policy (under rts-pthread)
26117 with respectively
26118 @code{Inheritance_Locking} and @code{Ceiling_Locking} as the policy.
26119 @cindex @code{Inheritance_Locking} (under rts-pthread)
26120 @cindex @code{Ceiling_Locking} (under rts-pthread)
26121
26122 As explained above, the native run-time library is based on the Solaris thread
26123 library (@code{libthread}) and is the default library.
26124
26125 When the Solaris threads library is used (this is the default), programs
26126 compiled with GNAT can automatically take advantage of
26127 and can thus execute on multiple processors.
26128 The user can alternatively specify a processor on which the program should run
26129 to emulate a single-processor system. The multiprocessor / uniprocessor choice
26130 is made by
26131 setting the environment variable @env{GNAT_PROCESSOR}
26132 @cindex @env{GNAT_PROCESSOR} environment variable (on Sparc Solaris)
26133 to one of the following:
26134
26135 @table @code
26136 @item -2
26137 Use the default configuration (run the program on all
26138 available processors) - this is the same as having @code{GNAT_PROCESSOR}
26139 unset
26140
26141 @item -1
26142 Let the run-time implementation choose one processor and run the program on
26143 that processor
26144
26145 @item 0 .. Last_Proc
26146 Run the program on the specified processor.
26147 @code{Last_Proc} is equal to @code{_SC_NPROCESSORS_CONF - 1}
26148 (where @code{_SC_NPROCESSORS_CONF} is a system variable).
26149 @end table
26150
26151 @node Linux-Specific Considerations
26152 @section Linux-Specific Considerations
26153 @cindex Linux threads libraries
26154
26155 @noindent
26156 On GNU/Linux without NPTL support (usually system with GNU C Library
26157 older than 2.3), the signal model is not POSIX compliant, which means
26158 that to send a signal to the process, you need to send the signal to all
26159 threads, e.g.@: by using @code{killpg()}.
26160
26161 @node AIX-Specific Considerations
26162 @section AIX-Specific Considerations
26163 @cindex AIX resolver library
26164
26165 @noindent
26166 On AIX, the resolver library initializes some internal structure on
26167 the first call to @code{get*by*} functions, which are used to implement
26168 @code{GNAT.Sockets.Get_Host_By_Name} and
26169 @code{GNAT.Sockets.Get_Host_By_Address}.
26170 If such initialization occurs within an Ada task, and the stack size for
26171 the task is the default size, a stack overflow may occur.
26172
26173 To avoid this overflow, the user should either ensure that the first call
26174 to @code{GNAT.Sockets.Get_Host_By_Name} or
26175 @code{GNAT.Sockets.Get_Host_By_Addrss}
26176 occurs in the environment task, or use @code{pragma Storage_Size} to
26177 specify a sufficiently large size for the stack of the task that contains
26178 this call.
26179
26180 @node Irix-Specific Considerations
26181 @section Irix-Specific Considerations
26182 @cindex Irix libraries
26183
26184 @noindent
26185 The GCC support libraries coming with the Irix compiler have moved to
26186 their canonical place with respect to the general Irix ABI related
26187 conventions. Running applications built with the default shared GNAT
26188 run-time now requires the LD_LIBRARY_PATH environment variable to
26189 include this location. A possible way to achieve this is to issue the
26190 following command line on a bash prompt:
26191
26192 @smallexample
26193 @group
26194 $ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`dirname \`gcc --print-file-name=libgcc_s.so\``
26195 @end group
26196 @end smallexample
26197
26198 @node RTX-Specific Considerations
26199 @section RTX-Specific Considerations
26200 @cindex RTX libraries
26201
26202 @noindent
26203 The Real-time Extension (RTX) to Windows is based on the Windows Win32
26204 API. Applications can be built to work in two different modes:
26205
26206 @itemize @bullet
26207 @item
26208 Windows executables that run in Ring 3 to utilize memory protection
26209 (@emph{rts-rtx-w32}).
26210
26211 @item
26212 Real-time subsystem (RTSS) executables that run in Ring 0, where
26213 performance can be optimized with RTSS applications taking precedent
26214 over all Windows applications (@emph{rts-rtx-rtss}).
26215
26216 @end itemize
26217
26218 @c *******************************
26219 @node Example of Binder Output File
26220 @appendix Example of Binder Output File
26221
26222 @noindent
26223 This Appendix displays the source code for @command{gnatbind}'s output
26224 file generated for a simple ``Hello World'' program.
26225 Comments have been added for clarification purposes.
26226
26227 @smallexample @c adanocomment
26228 @iftex
26229 @leftskip=0cm
26230 @end iftex
26231 -- The package is called Ada_Main unless this name is actually used
26232 -- as a unit name in the partition, in which case some other unique
26233 -- name is used.
26234
26235 with System;
26236 package ada_main is
26237
26238 Elab_Final_Code : Integer;
26239 pragma Import (C, Elab_Final_Code, "__gnat_inside_elab_final_code");
26240
26241 -- The main program saves the parameters (argument count,
26242 -- argument values, environment pointer) in global variables
26243 -- for later access by other units including
26244 -- Ada.Command_Line.
26245
26246 gnat_argc : Integer;
26247 gnat_argv : System.Address;
26248 gnat_envp : System.Address;
26249
26250 -- The actual variables are stored in a library routine. This
26251 -- is useful for some shared library situations, where there
26252 -- are problems if variables are not in the library.
26253
26254 pragma Import (C, gnat_argc);
26255 pragma Import (C, gnat_argv);
26256 pragma Import (C, gnat_envp);
26257
26258 -- The exit status is similarly an external location
26259
26260 gnat_exit_status : Integer;
26261 pragma Import (C, gnat_exit_status);
26262
26263 GNAT_Version : constant String :=
26264 "GNAT Version: 6.0.0w (20061115)";
26265 pragma Export (C, GNAT_Version, "__gnat_version");
26266
26267 -- This is the generated adafinal routine that performs
26268 -- finalization at the end of execution. In the case where
26269 -- Ada is the main program, this main program makes a call
26270 -- to adafinal at program termination.
26271
26272 procedure adafinal;
26273 pragma Export (C, adafinal, "adafinal");
26274
26275 -- This is the generated adainit routine that performs
26276 -- initialization at the start of execution. In the case
26277 -- where Ada is the main program, this main program makes
26278 -- a call to adainit at program startup.
26279
26280 procedure adainit;
26281 pragma Export (C, adainit, "adainit");
26282
26283 -- This routine is called at the start of execution. It is
26284 -- a dummy routine that is used by the debugger to breakpoint
26285 -- at the start of execution.
26286
26287 procedure Break_Start;
26288 pragma Import (C, Break_Start, "__gnat_break_start");
26289
26290 -- This is the actual generated main program (it would be
26291 -- suppressed if the no main program switch were used). As
26292 -- required by standard system conventions, this program has
26293 -- the external name main.
26294
26295 function main
26296 (argc : Integer;
26297 argv : System.Address;
26298 envp : System.Address)
26299 return Integer;
26300 pragma Export (C, main, "main");
26301
26302 -- The following set of constants give the version
26303 -- identification values for every unit in the bound
26304 -- partition. This identification is computed from all
26305 -- dependent semantic units, and corresponds to the
26306 -- string that would be returned by use of the
26307 -- Body_Version or Version attributes.
26308
26309 type Version_32 is mod 2 ** 32;
26310 u00001 : constant Version_32 := 16#7880BEB3#;
26311 u00002 : constant Version_32 := 16#0D24CBD0#;
26312 u00003 : constant Version_32 := 16#3283DBEB#;
26313 u00004 : constant Version_32 := 16#2359F9ED#;
26314 u00005 : constant Version_32 := 16#664FB847#;
26315 u00006 : constant Version_32 := 16#68E803DF#;
26316 u00007 : constant Version_32 := 16#5572E604#;
26317 u00008 : constant Version_32 := 16#46B173D8#;
26318 u00009 : constant Version_32 := 16#156A40CF#;
26319 u00010 : constant Version_32 := 16#033DABE0#;
26320 u00011 : constant Version_32 := 16#6AB38FEA#;
26321 u00012 : constant Version_32 := 16#22B6217D#;
26322 u00013 : constant Version_32 := 16#68A22947#;
26323 u00014 : constant Version_32 := 16#18CC4A56#;
26324 u00015 : constant Version_32 := 16#08258E1B#;
26325 u00016 : constant Version_32 := 16#367D5222#;
26326 u00017 : constant Version_32 := 16#20C9ECA4#;
26327 u00018 : constant Version_32 := 16#50D32CB6#;
26328 u00019 : constant Version_32 := 16#39A8BB77#;
26329 u00020 : constant Version_32 := 16#5CF8FA2B#;
26330 u00021 : constant Version_32 := 16#2F1EB794#;
26331 u00022 : constant Version_32 := 16#31AB6444#;
26332 u00023 : constant Version_32 := 16#1574B6E9#;
26333 u00024 : constant Version_32 := 16#5109C189#;
26334 u00025 : constant Version_32 := 16#56D770CD#;
26335 u00026 : constant Version_32 := 16#02F9DE3D#;
26336 u00027 : constant Version_32 := 16#08AB6B2C#;
26337 u00028 : constant Version_32 := 16#3FA37670#;
26338 u00029 : constant Version_32 := 16#476457A0#;
26339 u00030 : constant Version_32 := 16#731E1B6E#;
26340 u00031 : constant Version_32 := 16#23C2E789#;
26341 u00032 : constant Version_32 := 16#0F1BD6A1#;
26342 u00033 : constant Version_32 := 16#7C25DE96#;
26343 u00034 : constant Version_32 := 16#39ADFFA2#;
26344 u00035 : constant Version_32 := 16#571DE3E7#;
26345 u00036 : constant Version_32 := 16#5EB646AB#;
26346 u00037 : constant Version_32 := 16#4249379B#;
26347 u00038 : constant Version_32 := 16#0357E00A#;
26348 u00039 : constant Version_32 := 16#3784FB72#;
26349 u00040 : constant Version_32 := 16#2E723019#;
26350 u00041 : constant Version_32 := 16#623358EA#;
26351 u00042 : constant Version_32 := 16#107F9465#;
26352 u00043 : constant Version_32 := 16#6843F68A#;
26353 u00044 : constant Version_32 := 16#63305874#;
26354 u00045 : constant Version_32 := 16#31E56CE1#;
26355 u00046 : constant Version_32 := 16#02917970#;
26356 u00047 : constant Version_32 := 16#6CCBA70E#;
26357 u00048 : constant Version_32 := 16#41CD4204#;
26358 u00049 : constant Version_32 := 16#572E3F58#;
26359 u00050 : constant Version_32 := 16#20729FF5#;
26360 u00051 : constant Version_32 := 16#1D4F93E8#;
26361 u00052 : constant Version_32 := 16#30B2EC3D#;
26362 u00053 : constant Version_32 := 16#34054F96#;
26363 u00054 : constant Version_32 := 16#5A199860#;
26364 u00055 : constant Version_32 := 16#0E7F912B#;
26365 u00056 : constant Version_32 := 16#5760634A#;
26366 u00057 : constant Version_32 := 16#5D851835#;
26367
26368 -- The following Export pragmas export the version numbers
26369 -- with symbolic names ending in B (for body) or S
26370 -- (for spec) so that they can be located in a link. The
26371 -- information provided here is sufficient to track down
26372 -- the exact versions of units used in a given build.
26373
26374 pragma Export (C, u00001, "helloB");
26375 pragma Export (C, u00002, "system__standard_libraryB");
26376 pragma Export (C, u00003, "system__standard_libraryS");
26377 pragma Export (C, u00004, "adaS");
26378 pragma Export (C, u00005, "ada__text_ioB");
26379 pragma Export (C, u00006, "ada__text_ioS");
26380 pragma Export (C, u00007, "ada__exceptionsB");
26381 pragma Export (C, u00008, "ada__exceptionsS");
26382 pragma Export (C, u00009, "gnatS");
26383 pragma Export (C, u00010, "gnat__heap_sort_aB");
26384 pragma Export (C, u00011, "gnat__heap_sort_aS");
26385 pragma Export (C, u00012, "systemS");
26386 pragma Export (C, u00013, "system__exception_tableB");
26387 pragma Export (C, u00014, "system__exception_tableS");
26388 pragma Export (C, u00015, "gnat__htableB");
26389 pragma Export (C, u00016, "gnat__htableS");
26390 pragma Export (C, u00017, "system__exceptionsS");
26391 pragma Export (C, u00018, "system__machine_state_operationsB");
26392 pragma Export (C, u00019, "system__machine_state_operationsS");
26393 pragma Export (C, u00020, "system__machine_codeS");
26394 pragma Export (C, u00021, "system__storage_elementsB");
26395 pragma Export (C, u00022, "system__storage_elementsS");
26396 pragma Export (C, u00023, "system__secondary_stackB");
26397 pragma Export (C, u00024, "system__secondary_stackS");
26398 pragma Export (C, u00025, "system__parametersB");
26399 pragma Export (C, u00026, "system__parametersS");
26400 pragma Export (C, u00027, "system__soft_linksB");
26401 pragma Export (C, u00028, "system__soft_linksS");
26402 pragma Export (C, u00029, "system__stack_checkingB");
26403 pragma Export (C, u00030, "system__stack_checkingS");
26404 pragma Export (C, u00031, "system__tracebackB");
26405 pragma Export (C, u00032, "system__tracebackS");
26406 pragma Export (C, u00033, "ada__streamsS");
26407 pragma Export (C, u00034, "ada__tagsB");
26408 pragma Export (C, u00035, "ada__tagsS");
26409 pragma Export (C, u00036, "system__string_opsB");
26410 pragma Export (C, u00037, "system__string_opsS");
26411 pragma Export (C, u00038, "interfacesS");
26412 pragma Export (C, u00039, "interfaces__c_streamsB");
26413 pragma Export (C, u00040, "interfaces__c_streamsS");
26414 pragma Export (C, u00041, "system__file_ioB");
26415 pragma Export (C, u00042, "system__file_ioS");
26416 pragma Export (C, u00043, "ada__finalizationB");
26417 pragma Export (C, u00044, "ada__finalizationS");
26418 pragma Export (C, u00045, "system__finalization_rootB");
26419 pragma Export (C, u00046, "system__finalization_rootS");
26420 pragma Export (C, u00047, "system__finalization_implementationB");
26421 pragma Export (C, u00048, "system__finalization_implementationS");
26422 pragma Export (C, u00049, "system__string_ops_concat_3B");
26423 pragma Export (C, u00050, "system__string_ops_concat_3S");
26424 pragma Export (C, u00051, "system__stream_attributesB");
26425 pragma Export (C, u00052, "system__stream_attributesS");
26426 pragma Export (C, u00053, "ada__io_exceptionsS");
26427 pragma Export (C, u00054, "system__unsigned_typesS");
26428 pragma Export (C, u00055, "system__file_control_blockS");
26429 pragma Export (C, u00056, "ada__finalization__list_controllerB");
26430 pragma Export (C, u00057, "ada__finalization__list_controllerS");
26431
26432 -- BEGIN ELABORATION ORDER
26433 -- ada (spec)
26434 -- gnat (spec)
26435 -- gnat.heap_sort_a (spec)
26436 -- gnat.heap_sort_a (body)
26437 -- gnat.htable (spec)
26438 -- gnat.htable (body)
26439 -- interfaces (spec)
26440 -- system (spec)
26441 -- system.machine_code (spec)
26442 -- system.parameters (spec)
26443 -- system.parameters (body)
26444 -- interfaces.c_streams (spec)
26445 -- interfaces.c_streams (body)
26446 -- system.standard_library (spec)
26447 -- ada.exceptions (spec)
26448 -- system.exception_table (spec)
26449 -- system.exception_table (body)
26450 -- ada.io_exceptions (spec)
26451 -- system.exceptions (spec)
26452 -- system.storage_elements (spec)
26453 -- system.storage_elements (body)
26454 -- system.machine_state_operations (spec)
26455 -- system.machine_state_operations (body)
26456 -- system.secondary_stack (spec)
26457 -- system.stack_checking (spec)
26458 -- system.soft_links (spec)
26459 -- system.soft_links (body)
26460 -- system.stack_checking (body)
26461 -- system.secondary_stack (body)
26462 -- system.standard_library (body)
26463 -- system.string_ops (spec)
26464 -- system.string_ops (body)
26465 -- ada.tags (spec)
26466 -- ada.tags (body)
26467 -- ada.streams (spec)
26468 -- system.finalization_root (spec)
26469 -- system.finalization_root (body)
26470 -- system.string_ops_concat_3 (spec)
26471 -- system.string_ops_concat_3 (body)
26472 -- system.traceback (spec)
26473 -- system.traceback (body)
26474 -- ada.exceptions (body)
26475 -- system.unsigned_types (spec)
26476 -- system.stream_attributes (spec)
26477 -- system.stream_attributes (body)
26478 -- system.finalization_implementation (spec)
26479 -- system.finalization_implementation (body)
26480 -- ada.finalization (spec)
26481 -- ada.finalization (body)
26482 -- ada.finalization.list_controller (spec)
26483 -- ada.finalization.list_controller (body)
26484 -- system.file_control_block (spec)
26485 -- system.file_io (spec)
26486 -- system.file_io (body)
26487 -- ada.text_io (spec)
26488 -- ada.text_io (body)
26489 -- hello (body)
26490 -- END ELABORATION ORDER
26491
26492 end ada_main;
26493
26494 -- The following source file name pragmas allow the generated file
26495 -- names to be unique for different main programs. They are needed
26496 -- since the package name will always be Ada_Main.
26497
26498 pragma Source_File_Name (ada_main, Spec_File_Name => "b~hello.ads");
26499 pragma Source_File_Name (ada_main, Body_File_Name => "b~hello.adb");
26500
26501 -- Generated package body for Ada_Main starts here
26502
26503 package body ada_main is
26504
26505 -- The actual finalization is performed by calling the
26506 -- library routine in System.Standard_Library.Adafinal
26507
26508 procedure Do_Finalize;
26509 pragma Import (C, Do_Finalize, "system__standard_library__adafinal");
26510
26511 -------------
26512 -- adainit --
26513 -------------
26514
26515 @findex adainit
26516 procedure adainit is
26517
26518 -- These booleans are set to True once the associated unit has
26519 -- been elaborated. It is also used to avoid elaborating the
26520 -- same unit twice.
26521
26522 E040 : Boolean;
26523 pragma Import (Ada, E040, "interfaces__c_streams_E");
26524
26525 E008 : Boolean;
26526 pragma Import (Ada, E008, "ada__exceptions_E");
26527
26528 E014 : Boolean;
26529 pragma Import (Ada, E014, "system__exception_table_E");
26530
26531 E053 : Boolean;
26532 pragma Import (Ada, E053, "ada__io_exceptions_E");
26533
26534 E017 : Boolean;
26535 pragma Import (Ada, E017, "system__exceptions_E");
26536
26537 E024 : Boolean;
26538 pragma Import (Ada, E024, "system__secondary_stack_E");
26539
26540 E030 : Boolean;
26541 pragma Import (Ada, E030, "system__stack_checking_E");
26542
26543 E028 : Boolean;
26544 pragma Import (Ada, E028, "system__soft_links_E");
26545
26546 E035 : Boolean;
26547 pragma Import (Ada, E035, "ada__tags_E");
26548
26549 E033 : Boolean;
26550 pragma Import (Ada, E033, "ada__streams_E");
26551
26552 E046 : Boolean;
26553 pragma Import (Ada, E046, "system__finalization_root_E");
26554
26555 E048 : Boolean;
26556 pragma Import (Ada, E048, "system__finalization_implementation_E");
26557
26558 E044 : Boolean;
26559 pragma Import (Ada, E044, "ada__finalization_E");
26560
26561 E057 : Boolean;
26562 pragma Import (Ada, E057, "ada__finalization__list_controller_E");
26563
26564 E055 : Boolean;
26565 pragma Import (Ada, E055, "system__file_control_block_E");
26566
26567 E042 : Boolean;
26568 pragma Import (Ada, E042, "system__file_io_E");
26569
26570 E006 : Boolean;
26571 pragma Import (Ada, E006, "ada__text_io_E");
26572
26573 -- Set_Globals is a library routine that stores away the
26574 -- value of the indicated set of global values in global
26575 -- variables within the library.
26576
26577 procedure Set_Globals
26578 (Main_Priority : Integer;
26579 Time_Slice_Value : Integer;
26580 WC_Encoding : Character;
26581 Locking_Policy : Character;
26582 Queuing_Policy : Character;
26583 Task_Dispatching_Policy : Character;
26584 Adafinal : System.Address;
26585 Unreserve_All_Interrupts : Integer;
26586 Exception_Tracebacks : Integer);
26587 @findex __gnat_set_globals
26588 pragma Import (C, Set_Globals, "__gnat_set_globals");
26589
26590 -- SDP_Table_Build is a library routine used to build the
26591 -- exception tables. See unit Ada.Exceptions in files
26592 -- a-except.ads/adb for full details of how zero cost
26593 -- exception handling works. This procedure, the call to
26594 -- it, and the two following tables are all omitted if the
26595 -- build is in longjmp/setjmp exception mode.
26596
26597 @findex SDP_Table_Build
26598 @findex Zero Cost Exceptions
26599 procedure SDP_Table_Build
26600 (SDP_Addresses : System.Address;
26601 SDP_Count : Natural;
26602 Elab_Addresses : System.Address;
26603 Elab_Addr_Count : Natural);
26604 pragma Import (C, SDP_Table_Build, "__gnat_SDP_Table_Build");
26605
26606 -- Table of Unit_Exception_Table addresses. Used for zero
26607 -- cost exception handling to build the top level table.
26608
26609 ST : aliased constant array (1 .. 23) of System.Address := (
26610 Hello'UET_Address,
26611 Ada.Text_Io'UET_Address,
26612 Ada.Exceptions'UET_Address,
26613 Gnat.Heap_Sort_A'UET_Address,
26614 System.Exception_Table'UET_Address,
26615 System.Machine_State_Operations'UET_Address,
26616 System.Secondary_Stack'UET_Address,
26617 System.Parameters'UET_Address,
26618 System.Soft_Links'UET_Address,
26619 System.Stack_Checking'UET_Address,
26620 System.Traceback'UET_Address,
26621 Ada.Streams'UET_Address,
26622 Ada.Tags'UET_Address,
26623 System.String_Ops'UET_Address,
26624 Interfaces.C_Streams'UET_Address,
26625 System.File_Io'UET_Address,
26626 Ada.Finalization'UET_Address,
26627 System.Finalization_Root'UET_Address,
26628 System.Finalization_Implementation'UET_Address,
26629 System.String_Ops_Concat_3'UET_Address,
26630 System.Stream_Attributes'UET_Address,
26631 System.File_Control_Block'UET_Address,
26632 Ada.Finalization.List_Controller'UET_Address);
26633
26634 -- Table of addresses of elaboration routines. Used for
26635 -- zero cost exception handling to make sure these
26636 -- addresses are included in the top level procedure
26637 -- address table.
26638
26639 EA : aliased constant array (1 .. 23) of System.Address := (
26640 adainit'Code_Address,
26641 Do_Finalize'Code_Address,
26642 Ada.Exceptions'Elab_Spec'Address,
26643 System.Exceptions'Elab_Spec'Address,
26644 Interfaces.C_Streams'Elab_Spec'Address,
26645 System.Exception_Table'Elab_Body'Address,
26646 Ada.Io_Exceptions'Elab_Spec'Address,
26647 System.Stack_Checking'Elab_Spec'Address,
26648 System.Soft_Links'Elab_Body'Address,
26649 System.Secondary_Stack'Elab_Body'Address,
26650 Ada.Tags'Elab_Spec'Address,
26651 Ada.Tags'Elab_Body'Address,
26652 Ada.Streams'Elab_Spec'Address,
26653 System.Finalization_Root'Elab_Spec'Address,
26654 Ada.Exceptions'Elab_Body'Address,
26655 System.Finalization_Implementation'Elab_Spec'Address,
26656 System.Finalization_Implementation'Elab_Body'Address,
26657 Ada.Finalization'Elab_Spec'Address,
26658 Ada.Finalization.List_Controller'Elab_Spec'Address,
26659 System.File_Control_Block'Elab_Spec'Address,
26660 System.File_Io'Elab_Body'Address,
26661 Ada.Text_Io'Elab_Spec'Address,
26662 Ada.Text_Io'Elab_Body'Address);
26663
26664 -- Start of processing for adainit
26665
26666 begin
26667
26668 -- Call SDP_Table_Build to build the top level procedure
26669 -- table for zero cost exception handling (omitted in
26670 -- longjmp/setjmp mode).
26671
26672 SDP_Table_Build (ST'Address, 23, EA'Address, 23);
26673
26674 -- Call Set_Globals to record various information for
26675 -- this partition. The values are derived by the binder
26676 -- from information stored in the ali files by the compiler.
26677
26678 @findex __gnat_set_globals
26679 Set_Globals
26680 (Main_Priority => -1,
26681 -- Priority of main program, -1 if no pragma Priority used
26682
26683 Time_Slice_Value => -1,
26684 -- Time slice from Time_Slice pragma, -1 if none used
26685
26686 WC_Encoding => 'b',
26687 -- Wide_Character encoding used, default is brackets
26688
26689 Locking_Policy => ' ',
26690 -- Locking_Policy used, default of space means not
26691 -- specified, otherwise it is the first character of
26692 -- the policy name.
26693
26694 Queuing_Policy => ' ',
26695 -- Queuing_Policy used, default of space means not
26696 -- specified, otherwise it is the first character of
26697 -- the policy name.
26698
26699 Task_Dispatching_Policy => ' ',
26700 -- Task_Dispatching_Policy used, default of space means
26701 -- not specified, otherwise first character of the
26702 -- policy name.
26703
26704 Adafinal => System.Null_Address,
26705 -- Address of Adafinal routine, not used anymore
26706
26707 Unreserve_All_Interrupts => 0,
26708 -- Set true if pragma Unreserve_All_Interrupts was used
26709
26710 Exception_Tracebacks => 0);
26711 -- Indicates if exception tracebacks are enabled
26712
26713 Elab_Final_Code := 1;
26714
26715 -- Now we have the elaboration calls for all units in the partition.
26716 -- The Elab_Spec and Elab_Body attributes generate references to the
26717 -- implicit elaboration procedures generated by the compiler for
26718 -- each unit that requires elaboration.
26719
26720 if not E040 then
26721 Interfaces.C_Streams'Elab_Spec;
26722 end if;
26723 E040 := True;
26724 if not E008 then
26725 Ada.Exceptions'Elab_Spec;
26726 end if;
26727 if not E014 then
26728 System.Exception_Table'Elab_Body;
26729 E014 := True;
26730 end if;
26731 if not E053 then
26732 Ada.Io_Exceptions'Elab_Spec;
26733 E053 := True;
26734 end if;
26735 if not E017 then
26736 System.Exceptions'Elab_Spec;
26737 E017 := True;
26738 end if;
26739 if not E030 then
26740 System.Stack_Checking'Elab_Spec;
26741 end if;
26742 if not E028 then
26743 System.Soft_Links'Elab_Body;
26744 E028 := True;
26745 end if;
26746 E030 := True;
26747 if not E024 then
26748 System.Secondary_Stack'Elab_Body;
26749 E024 := True;
26750 end if;
26751 if not E035 then
26752 Ada.Tags'Elab_Spec;
26753 end if;
26754 if not E035 then
26755 Ada.Tags'Elab_Body;
26756 E035 := True;
26757 end if;
26758 if not E033 then
26759 Ada.Streams'Elab_Spec;
26760 E033 := True;
26761 end if;
26762 if not E046 then
26763 System.Finalization_Root'Elab_Spec;
26764 end if;
26765 E046 := True;
26766 if not E008 then
26767 Ada.Exceptions'Elab_Body;
26768 E008 := True;
26769 end if;
26770 if not E048 then
26771 System.Finalization_Implementation'Elab_Spec;
26772 end if;
26773 if not E048 then
26774 System.Finalization_Implementation'Elab_Body;
26775 E048 := True;
26776 end if;
26777 if not E044 then
26778 Ada.Finalization'Elab_Spec;
26779 end if;
26780 E044 := True;
26781 if not E057 then
26782 Ada.Finalization.List_Controller'Elab_Spec;
26783 end if;
26784 E057 := True;
26785 if not E055 then
26786 System.File_Control_Block'Elab_Spec;
26787 E055 := True;
26788 end if;
26789 if not E042 then
26790 System.File_Io'Elab_Body;
26791 E042 := True;
26792 end if;
26793 if not E006 then
26794 Ada.Text_Io'Elab_Spec;
26795 end if;
26796 if not E006 then
26797 Ada.Text_Io'Elab_Body;
26798 E006 := True;
26799 end if;
26800
26801 Elab_Final_Code := 0;
26802 end adainit;
26803
26804 --------------
26805 -- adafinal --
26806 --------------
26807
26808 @findex adafinal
26809 procedure adafinal is
26810 begin
26811 Do_Finalize;
26812 end adafinal;
26813
26814 ----------
26815 -- main --
26816 ----------
26817
26818 -- main is actually a function, as in the ANSI C standard,
26819 -- defined to return the exit status. The three parameters
26820 -- are the argument count, argument values and environment
26821 -- pointer.
26822
26823 @findex Main Program
26824 function main
26825 (argc : Integer;
26826 argv : System.Address;
26827 envp : System.Address)
26828 return Integer
26829 is
26830 -- The initialize routine performs low level system
26831 -- initialization using a standard library routine which
26832 -- sets up signal handling and performs any other
26833 -- required setup. The routine can be found in file
26834 -- a-init.c.
26835
26836 @findex __gnat_initialize
26837 procedure initialize;
26838 pragma Import (C, initialize, "__gnat_initialize");
26839
26840 -- The finalize routine performs low level system
26841 -- finalization using a standard library routine. The
26842 -- routine is found in file a-final.c and in the standard
26843 -- distribution is a dummy routine that does nothing, so
26844 -- really this is a hook for special user finalization.
26845
26846 @findex __gnat_finalize
26847 procedure finalize;
26848 pragma Import (C, finalize, "__gnat_finalize");
26849
26850 -- We get to the main program of the partition by using
26851 -- pragma Import because if we try to with the unit and
26852 -- call it Ada style, then not only do we waste time
26853 -- recompiling it, but also, we don't really know the right
26854 -- switches (e.g.@: identifier character set) to be used
26855 -- to compile it.
26856
26857 procedure Ada_Main_Program;
26858 pragma Import (Ada, Ada_Main_Program, "_ada_hello");
26859
26860 -- Start of processing for main
26861
26862 begin
26863 -- Save global variables
26864
26865 gnat_argc := argc;
26866 gnat_argv := argv;
26867 gnat_envp := envp;
26868
26869 -- Call low level system initialization
26870
26871 Initialize;
26872
26873 -- Call our generated Ada initialization routine
26874
26875 adainit;
26876
26877 -- This is the point at which we want the debugger to get
26878 -- control
26879
26880 Break_Start;
26881
26882 -- Now we call the main program of the partition
26883
26884 Ada_Main_Program;
26885
26886 -- Perform Ada finalization
26887
26888 adafinal;
26889
26890 -- Perform low level system finalization
26891
26892 Finalize;
26893
26894 -- Return the proper exit status
26895 return (gnat_exit_status);
26896 end;
26897
26898 -- This section is entirely comments, so it has no effect on the
26899 -- compilation of the Ada_Main package. It provides the list of
26900 -- object files and linker options, as well as some standard
26901 -- libraries needed for the link. The gnatlink utility parses
26902 -- this b~hello.adb file to read these comment lines to generate
26903 -- the appropriate command line arguments for the call to the
26904 -- system linker. The BEGIN/END lines are used for sentinels for
26905 -- this parsing operation.
26906
26907 -- The exact file names will of course depend on the environment,
26908 -- host/target and location of files on the host system.
26909
26910 @findex Object file list
26911 -- BEGIN Object file/option list
26912 -- ./hello.o
26913 -- -L./
26914 -- -L/usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/
26915 -- /usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a
26916 -- END Object file/option list
26917
26918 end ada_main;
26919 @end smallexample
26920
26921 @noindent
26922 The Ada code in the above example is exactly what is generated by the
26923 binder. We have added comments to more clearly indicate the function
26924 of each part of the generated @code{Ada_Main} package.
26925
26926 The code is standard Ada in all respects, and can be processed by any
26927 tools that handle Ada. In particular, it is possible to use the debugger
26928 in Ada mode to debug the generated @code{Ada_Main} package. For example,
26929 suppose that for reasons that you do not understand, your program is crashing
26930 during elaboration of the body of @code{Ada.Text_IO}. To locate this bug,
26931 you can place a breakpoint on the call:
26932
26933 @smallexample @c ada
26934 Ada.Text_Io'Elab_Body;
26935 @end smallexample
26936
26937 @noindent
26938 and trace the elaboration routine for this package to find out where
26939 the problem might be (more usually of course you would be debugging
26940 elaboration code in your own application).
26941
26942 @node Elaboration Order Handling in GNAT
26943 @appendix Elaboration Order Handling in GNAT
26944 @cindex Order of elaboration
26945 @cindex Elaboration control
26946
26947 @menu
26948 * Elaboration Code::
26949 * Checking the Elaboration Order::
26950 * Controlling the Elaboration Order::
26951 * Controlling Elaboration in GNAT - Internal Calls::
26952 * Controlling Elaboration in GNAT - External Calls::
26953 * Default Behavior in GNAT - Ensuring Safety::
26954 * Treatment of Pragma Elaborate::
26955 * Elaboration Issues for Library Tasks::
26956 * Mixing Elaboration Models::
26957 * What to Do If the Default Elaboration Behavior Fails::
26958 * Elaboration for Access-to-Subprogram Values::
26959 * Summary of Procedures for Elaboration Control::
26960 * Other Elaboration Order Considerations::
26961 @end menu
26962
26963 @noindent
26964 This chapter describes the handling of elaboration code in Ada and
26965 in GNAT, and discusses how the order of elaboration of program units can
26966 be controlled in GNAT, either automatically or with explicit programming
26967 features.
26968
26969 @node Elaboration Code
26970 @section Elaboration Code
26971
26972 @noindent
26973 Ada provides rather general mechanisms for executing code at elaboration
26974 time, that is to say before the main program starts executing. Such code arises
26975 in three contexts:
26976
26977 @table @asis
26978 @item Initializers for variables.
26979 Variables declared at the library level, in package specs or bodies, can
26980 require initialization that is performed at elaboration time, as in:
26981 @smallexample @c ada
26982 @cartouche
26983 Sqrt_Half : Float := Sqrt (0.5);
26984 @end cartouche
26985 @end smallexample
26986
26987 @item Package initialization code
26988 Code in a @code{BEGIN-END} section at the outer level of a package body is
26989 executed as part of the package body elaboration code.
26990
26991 @item Library level task allocators
26992 Tasks that are declared using task allocators at the library level
26993 start executing immediately and hence can execute at elaboration time.
26994 @end table
26995
26996 @noindent
26997 Subprogram calls are possible in any of these contexts, which means that
26998 any arbitrary part of the program may be executed as part of the elaboration
26999 code. It is even possible to write a program which does all its work at
27000 elaboration time, with a null main program, although stylistically this
27001 would usually be considered an inappropriate way to structure
27002 a program.
27003
27004 An important concern arises in the context of elaboration code:
27005 we have to be sure that it is executed in an appropriate order. What we
27006 have is a series of elaboration code sections, potentially one section
27007 for each unit in the program. It is important that these execute
27008 in the correct order. Correctness here means that, taking the above
27009 example of the declaration of @code{Sqrt_Half},
27010 if some other piece of
27011 elaboration code references @code{Sqrt_Half},
27012 then it must run after the
27013 section of elaboration code that contains the declaration of
27014 @code{Sqrt_Half}.
27015
27016 There would never be any order of elaboration problem if we made a rule
27017 that whenever you @code{with} a unit, you must elaborate both the spec and body
27018 of that unit before elaborating the unit doing the @code{with}'ing:
27019
27020 @smallexample @c ada
27021 @group
27022 @cartouche
27023 with Unit_1;
27024 package Unit_2 is @dots{}
27025 @end cartouche
27026 @end group
27027 @end smallexample
27028
27029 @noindent
27030 would require that both the body and spec of @code{Unit_1} be elaborated
27031 before the spec of @code{Unit_2}. However, a rule like that would be far too
27032 restrictive. In particular, it would make it impossible to have routines
27033 in separate packages that were mutually recursive.
27034
27035 You might think that a clever enough compiler could look at the actual
27036 elaboration code and determine an appropriate correct order of elaboration,
27037 but in the general case, this is not possible. Consider the following
27038 example.
27039
27040 In the body of @code{Unit_1}, we have a procedure @code{Func_1}
27041 that references
27042 the variable @code{Sqrt_1}, which is declared in the elaboration code
27043 of the body of @code{Unit_1}:
27044
27045 @smallexample @c ada
27046 @cartouche
27047 Sqrt_1 : Float := Sqrt (0.1);
27048 @end cartouche
27049 @end smallexample
27050
27051 @noindent
27052 The elaboration code of the body of @code{Unit_1} also contains:
27053
27054 @smallexample @c ada
27055 @group
27056 @cartouche
27057 if expression_1 = 1 then
27058 Q := Unit_2.Func_2;
27059 end if;
27060 @end cartouche
27061 @end group
27062 @end smallexample
27063
27064 @noindent
27065 @code{Unit_2} is exactly parallel,
27066 it has a procedure @code{Func_2} that references
27067 the variable @code{Sqrt_2}, which is declared in the elaboration code of
27068 the body @code{Unit_2}:
27069
27070 @smallexample @c ada
27071 @cartouche
27072 Sqrt_2 : Float := Sqrt (0.1);
27073 @end cartouche
27074 @end smallexample
27075
27076 @noindent
27077 The elaboration code of the body of @code{Unit_2} also contains:
27078
27079 @smallexample @c ada
27080 @group
27081 @cartouche
27082 if expression_2 = 2 then
27083 Q := Unit_1.Func_1;
27084 end if;
27085 @end cartouche
27086 @end group
27087 @end smallexample
27088
27089 @noindent
27090 Now the question is, which of the following orders of elaboration is
27091 acceptable:
27092
27093 @smallexample
27094 @group
27095 Spec of Unit_1
27096 Spec of Unit_2
27097 Body of Unit_1
27098 Body of Unit_2
27099 @end group
27100 @end smallexample
27101
27102 @noindent
27103 or
27104
27105 @smallexample
27106 @group
27107 Spec of Unit_2
27108 Spec of Unit_1
27109 Body of Unit_2
27110 Body of Unit_1
27111 @end group
27112 @end smallexample
27113
27114 @noindent
27115 If you carefully analyze the flow here, you will see that you cannot tell
27116 at compile time the answer to this question.
27117 If @code{expression_1} is not equal to 1,
27118 and @code{expression_2} is not equal to 2,
27119 then either order is acceptable, because neither of the function calls is
27120 executed. If both tests evaluate to true, then neither order is acceptable
27121 and in fact there is no correct order.
27122
27123 If one of the two expressions is true, and the other is false, then one
27124 of the above orders is correct, and the other is incorrect. For example,
27125 if @code{expression_1} /= 1 and @code{expression_2} = 2,
27126 then the call to @code{Func_1}
27127 will occur, but not the call to @code{Func_2.}
27128 This means that it is essential
27129 to elaborate the body of @code{Unit_1} before
27130 the body of @code{Unit_2}, so the first
27131 order of elaboration is correct and the second is wrong.
27132
27133 By making @code{expression_1} and @code{expression_2}
27134 depend on input data, or perhaps
27135 the time of day, we can make it impossible for the compiler or binder
27136 to figure out which of these expressions will be true, and hence it
27137 is impossible to guarantee a safe order of elaboration at run time.
27138
27139 @node Checking the Elaboration Order
27140 @section Checking the Elaboration Order
27141
27142 @noindent
27143 In some languages that involve the same kind of elaboration problems,
27144 e.g.@: Java and C++, the programmer is expected to worry about these
27145 ordering problems himself, and it is common to
27146 write a program in which an incorrect elaboration order gives
27147 surprising results, because it references variables before they
27148 are initialized.
27149 Ada is designed to be a safe language, and a programmer-beware approach is
27150 clearly not sufficient. Consequently, the language provides three lines
27151 of defense:
27152
27153 @table @asis
27154 @item Standard rules
27155 Some standard rules restrict the possible choice of elaboration
27156 order. In particular, if you @code{with} a unit, then its spec is always
27157 elaborated before the unit doing the @code{with}. Similarly, a parent
27158 spec is always elaborated before the child spec, and finally
27159 a spec is always elaborated before its corresponding body.
27160
27161 @item Dynamic elaboration checks
27162 @cindex Elaboration checks
27163 @cindex Checks, elaboration
27164 Dynamic checks are made at run time, so that if some entity is accessed
27165 before it is elaborated (typically by means of a subprogram call)
27166 then the exception (@code{Program_Error}) is raised.
27167
27168 @item Elaboration control
27169 Facilities are provided for the programmer to specify the desired order
27170 of elaboration.
27171 @end table
27172
27173 Let's look at these facilities in more detail. First, the rules for
27174 dynamic checking. One possible rule would be simply to say that the
27175 exception is raised if you access a variable which has not yet been
27176 elaborated. The trouble with this approach is that it could require
27177 expensive checks on every variable reference. Instead Ada has two
27178 rules which are a little more restrictive, but easier to check, and
27179 easier to state:
27180
27181 @table @asis
27182 @item Restrictions on calls
27183 A subprogram can only be called at elaboration time if its body
27184 has been elaborated. The rules for elaboration given above guarantee
27185 that the spec of the subprogram has been elaborated before the
27186 call, but not the body. If this rule is violated, then the
27187 exception @code{Program_Error} is raised.
27188
27189 @item Restrictions on instantiations
27190 A generic unit can only be instantiated if the body of the generic
27191 unit has been elaborated. Again, the rules for elaboration given above
27192 guarantee that the spec of the generic unit has been elaborated
27193 before the instantiation, but not the body. If this rule is
27194 violated, then the exception @code{Program_Error} is raised.
27195 @end table
27196
27197 @noindent
27198 The idea is that if the body has been elaborated, then any variables
27199 it references must have been elaborated; by checking for the body being
27200 elaborated we guarantee that none of its references causes any
27201 trouble. As we noted above, this is a little too restrictive, because a
27202 subprogram that has no non-local references in its body may in fact be safe
27203 to call. However, it really would be unsafe to rely on this, because
27204 it would mean that the caller was aware of details of the implementation
27205 in the body. This goes against the basic tenets of Ada.
27206
27207 A plausible implementation can be described as follows.
27208 A Boolean variable is associated with each subprogram
27209 and each generic unit. This variable is initialized to False, and is set to
27210 True at the point body is elaborated. Every call or instantiation checks the
27211 variable, and raises @code{Program_Error} if the variable is False.
27212
27213 Note that one might think that it would be good enough to have one Boolean
27214 variable for each package, but that would not deal with cases of trying
27215 to call a body in the same package as the call
27216 that has not been elaborated yet.
27217 Of course a compiler may be able to do enough analysis to optimize away
27218 some of the Boolean variables as unnecessary, and @code{GNAT} indeed
27219 does such optimizations, but still the easiest conceptual model is to
27220 think of there being one variable per subprogram.
27221
27222 @node Controlling the Elaboration Order
27223 @section Controlling the Elaboration Order
27224
27225 @noindent
27226 In the previous section we discussed the rules in Ada which ensure
27227 that @code{Program_Error} is raised if an incorrect elaboration order is
27228 chosen. This prevents erroneous executions, but we need mechanisms to
27229 specify a correct execution and avoid the exception altogether.
27230 To achieve this, Ada provides a number of features for controlling
27231 the order of elaboration. We discuss these features in this section.
27232
27233 First, there are several ways of indicating to the compiler that a given
27234 unit has no elaboration problems:
27235
27236 @table @asis
27237 @item packages that do not require a body
27238 A library package that does not require a body does not permit
27239 a body (this rule was introduced in Ada 95).
27240 Thus if we have a such a package, as in:
27241
27242 @smallexample @c ada
27243 @group
27244 @cartouche
27245 package Definitions is
27246 generic
27247 type m is new integer;
27248 package Subp is
27249 type a is array (1 .. 10) of m;
27250 type b is array (1 .. 20) of m;
27251 end Subp;
27252 end Definitions;
27253 @end cartouche
27254 @end group
27255 @end smallexample
27256
27257 @noindent
27258 A package that @code{with}'s @code{Definitions} may safely instantiate
27259 @code{Definitions.Subp} because the compiler can determine that there
27260 definitely is no package body to worry about in this case
27261
27262 @item pragma Pure
27263 @cindex pragma Pure
27264 @findex Pure
27265 Places sufficient restrictions on a unit to guarantee that
27266 no call to any subprogram in the unit can result in an
27267 elaboration problem. This means that the compiler does not need
27268 to worry about the point of elaboration of such units, and in
27269 particular, does not need to check any calls to any subprograms
27270 in this unit.
27271
27272 @item pragma Preelaborate
27273 @findex Preelaborate
27274 @cindex pragma Preelaborate
27275 This pragma places slightly less stringent restrictions on a unit than
27276 does pragma Pure,
27277 but these restrictions are still sufficient to ensure that there
27278 are no elaboration problems with any calls to the unit.
27279
27280 @item pragma Elaborate_Body
27281 @findex Elaborate_Body
27282 @cindex pragma Elaborate_Body
27283 This pragma requires that the body of a unit be elaborated immediately
27284 after its spec. Suppose a unit @code{A} has such a pragma,
27285 and unit @code{B} does
27286 a @code{with} of unit @code{A}. Recall that the standard rules require
27287 the spec of unit @code{A}
27288 to be elaborated before the @code{with}'ing unit; given the pragma in
27289 @code{A}, we also know that the body of @code{A}
27290 will be elaborated before @code{B}, so
27291 that calls to @code{A} are safe and do not need a check.
27292 @end table
27293
27294 @noindent
27295 Note that,
27296 unlike pragma @code{Pure} and pragma @code{Preelaborate},
27297 the use of
27298 @code{Elaborate_Body} does not guarantee that the program is
27299 free of elaboration problems, because it may not be possible
27300 to satisfy the requested elaboration order.
27301 Let's go back to the example with @code{Unit_1} and @code{Unit_2}.
27302 If a programmer
27303 marks @code{Unit_1} as @code{Elaborate_Body},
27304 and not @code{Unit_2,} then the order of
27305 elaboration will be:
27306
27307 @smallexample
27308 @group
27309 Spec of Unit_2
27310 Spec of Unit_1
27311 Body of Unit_1
27312 Body of Unit_2
27313 @end group
27314 @end smallexample
27315
27316 @noindent
27317 Now that means that the call to @code{Func_1} in @code{Unit_2}
27318 need not be checked,
27319 it must be safe. But the call to @code{Func_2} in
27320 @code{Unit_1} may still fail if
27321 @code{Expression_1} is equal to 1,
27322 and the programmer must still take
27323 responsibility for this not being the case.
27324
27325 If all units carry a pragma @code{Elaborate_Body}, then all problems are
27326 eliminated, except for calls entirely within a body, which are
27327 in any case fully under programmer control. However, using the pragma
27328 everywhere is not always possible.
27329 In particular, for our @code{Unit_1}/@code{Unit_2} example, if
27330 we marked both of them as having pragma @code{Elaborate_Body}, then
27331 clearly there would be no possible elaboration order.
27332
27333 The above pragmas allow a server to guarantee safe use by clients, and
27334 clearly this is the preferable approach. Consequently a good rule
27335 is to mark units as @code{Pure} or @code{Preelaborate} if possible,
27336 and if this is not possible,
27337 mark them as @code{Elaborate_Body} if possible.
27338 As we have seen, there are situations where neither of these
27339 three pragmas can be used.
27340 So we also provide methods for clients to control the
27341 order of elaboration of the servers on which they depend:
27342
27343 @table @asis
27344 @item pragma Elaborate (unit)
27345 @findex Elaborate
27346 @cindex pragma Elaborate
27347 This pragma is placed in the context clause, after a @code{with} clause,
27348 and it requires that the body of the named unit be elaborated before
27349 the unit in which the pragma occurs. The idea is to use this pragma
27350 if the current unit calls at elaboration time, directly or indirectly,
27351 some subprogram in the named unit.
27352
27353 @item pragma Elaborate_All (unit)
27354 @findex Elaborate_All
27355 @cindex pragma Elaborate_All
27356 This is a stronger version of the Elaborate pragma. Consider the
27357 following example:
27358
27359 @smallexample
27360 Unit A @code{with}'s unit B and calls B.Func in elab code
27361 Unit B @code{with}'s unit C, and B.Func calls C.Func
27362 @end smallexample
27363
27364 @noindent
27365 Now if we put a pragma @code{Elaborate (B)}
27366 in unit @code{A}, this ensures that the
27367 body of @code{B} is elaborated before the call, but not the
27368 body of @code{C}, so
27369 the call to @code{C.Func} could still cause @code{Program_Error} to
27370 be raised.
27371
27372 The effect of a pragma @code{Elaborate_All} is stronger, it requires
27373 not only that the body of the named unit be elaborated before the
27374 unit doing the @code{with}, but also the bodies of all units that the
27375 named unit uses, following @code{with} links transitively. For example,
27376 if we put a pragma @code{Elaborate_All (B)} in unit @code{A},
27377 then it requires
27378 not only that the body of @code{B} be elaborated before @code{A},
27379 but also the
27380 body of @code{C}, because @code{B} @code{with}'s @code{C}.
27381 @end table
27382
27383 @noindent
27384 We are now in a position to give a usage rule in Ada for avoiding
27385 elaboration problems, at least if dynamic dispatching and access to
27386 subprogram values are not used. We will handle these cases separately
27387 later.
27388
27389 The rule is simple. If a unit has elaboration code that can directly or
27390 indirectly make a call to a subprogram in a @code{with}'ed unit, or instantiate
27391 a generic package in a @code{with}'ed unit,
27392 then if the @code{with}'ed unit does not have
27393 pragma @code{Pure} or @code{Preelaborate}, then the client should have
27394 a pragma @code{Elaborate_All}
27395 for the @code{with}'ed unit. By following this rule a client is
27396 assured that calls can be made without risk of an exception.
27397
27398 For generic subprogram instantiations, the rule can be relaxed to
27399 require only a pragma @code{Elaborate} since elaborating the body
27400 of a subprogram cannot cause any transitive elaboration (we are
27401 not calling the subprogram in this case, just elaborating its
27402 declaration).
27403
27404 If this rule is not followed, then a program may be in one of four
27405 states:
27406
27407 @table @asis
27408 @item No order exists
27409 No order of elaboration exists which follows the rules, taking into
27410 account any @code{Elaborate}, @code{Elaborate_All},
27411 or @code{Elaborate_Body} pragmas. In
27412 this case, an Ada compiler must diagnose the situation at bind
27413 time, and refuse to build an executable program.
27414
27415 @item One or more orders exist, all incorrect
27416 One or more acceptable elaboration orders exist, and all of them
27417 generate an elaboration order problem. In this case, the binder
27418 can build an executable program, but @code{Program_Error} will be raised
27419 when the program is run.
27420
27421 @item Several orders exist, some right, some incorrect
27422 One or more acceptable elaboration orders exists, and some of them
27423 work, and some do not. The programmer has not controlled
27424 the order of elaboration, so the binder may or may not pick one of
27425 the correct orders, and the program may or may not raise an
27426 exception when it is run. This is the worst case, because it means
27427 that the program may fail when moved to another compiler, or even
27428 another version of the same compiler.
27429
27430 @item One or more orders exists, all correct
27431 One ore more acceptable elaboration orders exist, and all of them
27432 work. In this case the program runs successfully. This state of
27433 affairs can be guaranteed by following the rule we gave above, but
27434 may be true even if the rule is not followed.
27435 @end table
27436
27437 @noindent
27438 Note that one additional advantage of following our rules on the use
27439 of @code{Elaborate} and @code{Elaborate_All}
27440 is that the program continues to stay in the ideal (all orders OK) state
27441 even if maintenance
27442 changes some bodies of some units. Conversely, if a program that does
27443 not follow this rule happens to be safe at some point, this state of affairs
27444 may deteriorate silently as a result of maintenance changes.
27445
27446 You may have noticed that the above discussion did not mention
27447 the use of @code{Elaborate_Body}. This was a deliberate omission. If you
27448 @code{with} an @code{Elaborate_Body} unit, it still may be the case that
27449 code in the body makes calls to some other unit, so it is still necessary
27450 to use @code{Elaborate_All} on such units.
27451
27452 @node Controlling Elaboration in GNAT - Internal Calls
27453 @section Controlling Elaboration in GNAT - Internal Calls
27454
27455 @noindent
27456 In the case of internal calls, i.e., calls within a single package, the
27457 programmer has full control over the order of elaboration, and it is up
27458 to the programmer to elaborate declarations in an appropriate order. For
27459 example writing:
27460
27461 @smallexample @c ada
27462 @group
27463 @cartouche
27464 function One return Float;
27465
27466 Q : Float := One;
27467
27468 function One return Float is
27469 begin
27470 return 1.0;
27471 end One;
27472 @end cartouche
27473 @end group
27474 @end smallexample
27475
27476 @noindent
27477 will obviously raise @code{Program_Error} at run time, because function
27478 One will be called before its body is elaborated. In this case GNAT will
27479 generate a warning that the call will raise @code{Program_Error}:
27480
27481 @smallexample
27482 @group
27483 @cartouche
27484 1. procedure y is
27485 2. function One return Float;
27486 3.
27487 4. Q : Float := One;
27488 |
27489 >>> warning: cannot call "One" before body is elaborated
27490 >>> warning: Program_Error will be raised at run time
27491
27492 5.
27493 6. function One return Float is
27494 7. begin
27495 8. return 1.0;
27496 9. end One;
27497 10.
27498 11. begin
27499 12. null;
27500 13. end;
27501 @end cartouche
27502 @end group
27503 @end smallexample
27504
27505 @noindent
27506 Note that in this particular case, it is likely that the call is safe, because
27507 the function @code{One} does not access any global variables.
27508 Nevertheless in Ada, we do not want the validity of the check to depend on
27509 the contents of the body (think about the separate compilation case), so this
27510 is still wrong, as we discussed in the previous sections.
27511
27512 The error is easily corrected by rearranging the declarations so that the
27513 body of @code{One} appears before the declaration containing the call
27514 (note that in Ada 95 and Ada 2005,
27515 declarations can appear in any order, so there is no restriction that
27516 would prevent this reordering, and if we write:
27517
27518 @smallexample @c ada
27519 @group
27520 @cartouche
27521 function One return Float;
27522
27523 function One return Float is
27524 begin
27525 return 1.0;
27526 end One;
27527
27528 Q : Float := One;
27529 @end cartouche
27530 @end group
27531 @end smallexample
27532
27533 @noindent
27534 then all is well, no warning is generated, and no
27535 @code{Program_Error} exception
27536 will be raised.
27537 Things are more complicated when a chain of subprograms is executed:
27538
27539 @smallexample @c ada
27540 @group
27541 @cartouche
27542 function A return Integer;
27543 function B return Integer;
27544 function C return Integer;
27545
27546 function B return Integer is begin return A; end;
27547 function C return Integer is begin return B; end;
27548
27549 X : Integer := C;
27550
27551 function A return Integer is begin return 1; end;
27552 @end cartouche
27553 @end group
27554 @end smallexample
27555
27556 @noindent
27557 Now the call to @code{C}
27558 at elaboration time in the declaration of @code{X} is correct, because
27559 the body of @code{C} is already elaborated,
27560 and the call to @code{B} within the body of
27561 @code{C} is correct, but the call
27562 to @code{A} within the body of @code{B} is incorrect, because the body
27563 of @code{A} has not been elaborated, so @code{Program_Error}
27564 will be raised on the call to @code{A}.
27565 In this case GNAT will generate a
27566 warning that @code{Program_Error} may be
27567 raised at the point of the call. Let's look at the warning:
27568
27569 @smallexample
27570 @group
27571 @cartouche
27572 1. procedure x is
27573 2. function A return Integer;
27574 3. function B return Integer;
27575 4. function C return Integer;
27576 5.
27577 6. function B return Integer is begin return A; end;
27578 |
27579 >>> warning: call to "A" before body is elaborated may
27580 raise Program_Error
27581 >>> warning: "B" called at line 7
27582 >>> warning: "C" called at line 9
27583
27584 7. function C return Integer is begin return B; end;
27585 8.
27586 9. X : Integer := C;
27587 10.
27588 11. function A return Integer is begin return 1; end;
27589 12.
27590 13. begin
27591 14. null;
27592 15. end;
27593 @end cartouche
27594 @end group
27595 @end smallexample
27596
27597 @noindent
27598 Note that the message here says ``may raise'', instead of the direct case,
27599 where the message says ``will be raised''. That's because whether
27600 @code{A} is
27601 actually called depends in general on run-time flow of control.
27602 For example, if the body of @code{B} said
27603
27604 @smallexample @c ada
27605 @group
27606 @cartouche
27607 function B return Integer is
27608 begin
27609 if some-condition-depending-on-input-data then
27610 return A;
27611 else
27612 return 1;
27613 end if;
27614 end B;
27615 @end cartouche
27616 @end group
27617 @end smallexample
27618
27619 @noindent
27620 then we could not know until run time whether the incorrect call to A would
27621 actually occur, so @code{Program_Error} might
27622 or might not be raised. It is possible for a compiler to
27623 do a better job of analyzing bodies, to
27624 determine whether or not @code{Program_Error}
27625 might be raised, but it certainly
27626 couldn't do a perfect job (that would require solving the halting problem
27627 and is provably impossible), and because this is a warning anyway, it does
27628 not seem worth the effort to do the analysis. Cases in which it
27629 would be relevant are rare.
27630
27631 In practice, warnings of either of the forms given
27632 above will usually correspond to
27633 real errors, and should be examined carefully and eliminated.
27634 In the rare case where a warning is bogus, it can be suppressed by any of
27635 the following methods:
27636
27637 @itemize @bullet
27638 @item
27639 Compile with the @option{-gnatws} switch set
27640
27641 @item
27642 Suppress @code{Elaboration_Check} for the called subprogram
27643
27644 @item
27645 Use pragma @code{Warnings_Off} to turn warnings off for the call
27646 @end itemize
27647
27648 @noindent
27649 For the internal elaboration check case,
27650 GNAT by default generates the
27651 necessary run-time checks to ensure
27652 that @code{Program_Error} is raised if any
27653 call fails an elaboration check. Of course this can only happen if a
27654 warning has been issued as described above. The use of pragma
27655 @code{Suppress (Elaboration_Check)} may (but is not guaranteed to) suppress
27656 some of these checks, meaning that it may be possible (but is not
27657 guaranteed) for a program to be able to call a subprogram whose body
27658 is not yet elaborated, without raising a @code{Program_Error} exception.
27659
27660 @node Controlling Elaboration in GNAT - External Calls
27661 @section Controlling Elaboration in GNAT - External Calls
27662
27663 @noindent
27664 The previous section discussed the case in which the execution of a
27665 particular thread of elaboration code occurred entirely within a
27666 single unit. This is the easy case to handle, because a programmer
27667 has direct and total control over the order of elaboration, and
27668 furthermore, checks need only be generated in cases which are rare
27669 and which the compiler can easily detect.
27670 The situation is more complex when separate compilation is taken into account.
27671 Consider the following:
27672
27673 @smallexample @c ada
27674 @cartouche
27675 @group
27676 package Math is
27677 function Sqrt (Arg : Float) return Float;
27678 end Math;
27679
27680 package body Math is
27681 function Sqrt (Arg : Float) return Float is
27682 begin
27683 @dots{}
27684 end Sqrt;
27685 end Math;
27686 @end group
27687 @group
27688 with Math;
27689 package Stuff is
27690 X : Float := Math.Sqrt (0.5);
27691 end Stuff;
27692
27693 with Stuff;
27694 procedure Main is
27695 begin
27696 @dots{}
27697 end Main;
27698 @end group
27699 @end cartouche
27700 @end smallexample
27701
27702 @noindent
27703 where @code{Main} is the main program. When this program is executed, the
27704 elaboration code must first be executed, and one of the jobs of the
27705 binder is to determine the order in which the units of a program are
27706 to be elaborated. In this case we have four units: the spec and body
27707 of @code{Math},
27708 the spec of @code{Stuff} and the body of @code{Main}).
27709 In what order should the four separate sections of elaboration code
27710 be executed?
27711
27712 There are some restrictions in the order of elaboration that the binder
27713 can choose. In particular, if unit U has a @code{with}
27714 for a package @code{X}, then you
27715 are assured that the spec of @code{X}
27716 is elaborated before U , but you are
27717 not assured that the body of @code{X}
27718 is elaborated before U.
27719 This means that in the above case, the binder is allowed to choose the
27720 order:
27721
27722 @smallexample
27723 spec of Math
27724 spec of Stuff
27725 body of Math
27726 body of Main
27727 @end smallexample
27728
27729 @noindent
27730 but that's not good, because now the call to @code{Math.Sqrt}
27731 that happens during
27732 the elaboration of the @code{Stuff}
27733 spec happens before the body of @code{Math.Sqrt} is
27734 elaborated, and hence causes @code{Program_Error} exception to be raised.
27735 At first glance, one might say that the binder is misbehaving, because
27736 obviously you want to elaborate the body of something you @code{with}
27737 first, but
27738 that is not a general rule that can be followed in all cases. Consider
27739
27740 @smallexample @c ada
27741 @group
27742 @cartouche
27743 package X is @dots{}
27744
27745 package Y is @dots{}
27746
27747 with X;
27748 package body Y is @dots{}
27749
27750 with Y;
27751 package body X is @dots{}
27752 @end cartouche
27753 @end group
27754 @end smallexample
27755
27756 @noindent
27757 This is a common arrangement, and, apart from the order of elaboration
27758 problems that might arise in connection with elaboration code, this works fine.
27759 A rule that says that you must first elaborate the body of anything you
27760 @code{with} cannot work in this case:
27761 the body of @code{X} @code{with}'s @code{Y},
27762 which means you would have to
27763 elaborate the body of @code{Y} first, but that @code{with}'s @code{X},
27764 which means
27765 you have to elaborate the body of @code{X} first, but @dots{} and we have a
27766 loop that cannot be broken.
27767
27768 It is true that the binder can in many cases guess an order of elaboration
27769 that is unlikely to cause a @code{Program_Error}
27770 exception to be raised, and it tries to do so (in the
27771 above example of @code{Math/Stuff/Spec}, the GNAT binder will
27772 by default
27773 elaborate the body of @code{Math} right after its spec, so all will be well).
27774
27775 However, a program that blindly relies on the binder to be helpful can
27776 get into trouble, as we discussed in the previous sections, so
27777 GNAT
27778 provides a number of facilities for assisting the programmer in
27779 developing programs that are robust with respect to elaboration order.
27780
27781 @node Default Behavior in GNAT - Ensuring Safety
27782 @section Default Behavior in GNAT - Ensuring Safety
27783
27784 @noindent
27785 The default behavior in GNAT ensures elaboration safety. In its
27786 default mode GNAT implements the
27787 rule we previously described as the right approach. Let's restate it:
27788
27789 @itemize
27790 @item
27791 @emph{If a unit has elaboration code that can directly or indirectly make a
27792 call to a subprogram in a @code{with}'ed unit, or instantiate a generic
27793 package in a @code{with}'ed unit, then if the @code{with}'ed unit
27794 does not have pragma @code{Pure} or
27795 @code{Preelaborate}, then the client should have an
27796 @code{Elaborate_All} pragma for the @code{with}'ed unit.}
27797
27798 @emph{In the case of instantiating a generic subprogram, it is always
27799 sufficient to have only an @code{Elaborate} pragma for the
27800 @code{with}'ed unit.}
27801 @end itemize
27802
27803 @noindent
27804 By following this rule a client is assured that calls and instantiations
27805 can be made without risk of an exception.
27806
27807 In this mode GNAT traces all calls that are potentially made from
27808 elaboration code, and puts in any missing implicit @code{Elaborate}
27809 and @code{Elaborate_All} pragmas.
27810 The advantage of this approach is that no elaboration problems
27811 are possible if the binder can find an elaboration order that is
27812 consistent with these implicit @code{Elaborate} and
27813 @code{Elaborate_All} pragmas. The
27814 disadvantage of this approach is that no such order may exist.
27815
27816 If the binder does not generate any diagnostics, then it means that it has
27817 found an elaboration order that is guaranteed to be safe. However, the binder
27818 may still be relying on implicitly generated @code{Elaborate} and
27819 @code{Elaborate_All} pragmas so portability to other compilers than GNAT is not
27820 guaranteed.
27821
27822 If it is important to guarantee portability, then the compilations should
27823 use the
27824 @option{-gnatwl}
27825 (warn on elaboration problems) switch. This will cause warning messages
27826 to be generated indicating the missing @code{Elaborate} and
27827 @code{Elaborate_All} pragmas.
27828 Consider the following source program:
27829
27830 @smallexample @c ada
27831 @group
27832 @cartouche
27833 with k;
27834 package j is
27835 m : integer := k.r;
27836 end;
27837 @end cartouche
27838 @end group
27839 @end smallexample
27840
27841 @noindent
27842 where it is clear that there
27843 should be a pragma @code{Elaborate_All}
27844 for unit @code{k}. An implicit pragma will be generated, and it is
27845 likely that the binder will be able to honor it. However, if you want
27846 to port this program to some other Ada compiler than GNAT.
27847 it is safer to include the pragma explicitly in the source. If this
27848 unit is compiled with the
27849 @option{-gnatwl}
27850 switch, then the compiler outputs a warning:
27851
27852 @smallexample
27853 @group
27854 @cartouche
27855 1. with k;
27856 2. package j is
27857 3. m : integer := k.r;
27858 |
27859 >>> warning: call to "r" may raise Program_Error
27860 >>> warning: missing pragma Elaborate_All for "k"
27861
27862 4. end;
27863 @end cartouche
27864 @end group
27865 @end smallexample
27866
27867 @noindent
27868 and these warnings can be used as a guide for supplying manually
27869 the missing pragmas. It is usually a bad idea to use this warning
27870 option during development. That's because it will warn you when
27871 you need to put in a pragma, but cannot warn you when it is time
27872 to take it out. So the use of pragma @code{Elaborate_All} may lead to
27873 unnecessary dependencies and even false circularities.
27874
27875 This default mode is more restrictive than the Ada Reference
27876 Manual, and it is possible to construct programs which will compile
27877 using the dynamic model described there, but will run into a
27878 circularity using the safer static model we have described.
27879
27880 Of course any Ada compiler must be able to operate in a mode
27881 consistent with the requirements of the Ada Reference Manual,
27882 and in particular must have the capability of implementing the
27883 standard dynamic model of elaboration with run-time checks.
27884
27885 In GNAT, this standard mode can be achieved either by the use of
27886 the @option{-gnatE} switch on the compiler (@command{gcc} or
27887 @command{gnatmake}) command, or by the use of the configuration pragma:
27888
27889 @smallexample @c ada
27890 pragma Elaboration_Checks (RM);
27891 @end smallexample
27892
27893 @noindent
27894 Either approach will cause the unit affected to be compiled using the
27895 standard dynamic run-time elaboration checks described in the Ada
27896 Reference Manual. The static model is generally preferable, since it
27897 is clearly safer to rely on compile and link time checks rather than
27898 run-time checks. However, in the case of legacy code, it may be
27899 difficult to meet the requirements of the static model. This
27900 issue is further discussed in
27901 @ref{What to Do If the Default Elaboration Behavior Fails}.
27902
27903 Note that the static model provides a strict subset of the allowed
27904 behavior and programs of the Ada Reference Manual, so if you do
27905 adhere to the static model and no circularities exist,
27906 then you are assured that your program will
27907 work using the dynamic model, providing that you remove any
27908 pragma Elaborate statements from the source.
27909
27910 @node Treatment of Pragma Elaborate
27911 @section Treatment of Pragma Elaborate
27912 @cindex Pragma Elaborate
27913
27914 @noindent
27915 The use of @code{pragma Elaborate}
27916 should generally be avoided in Ada 95 and Ada 2005 programs,
27917 since there is no guarantee that transitive calls
27918 will be properly handled. Indeed at one point, this pragma was placed
27919 in Annex J (Obsolescent Features), on the grounds that it is never useful.
27920
27921 Now that's a bit restrictive. In practice, the case in which
27922 @code{pragma Elaborate} is useful is when the caller knows that there
27923 are no transitive calls, or that the called unit contains all necessary
27924 transitive @code{pragma Elaborate} statements, and legacy code often
27925 contains such uses.
27926
27927 Strictly speaking the static mode in GNAT should ignore such pragmas,
27928 since there is no assurance at compile time that the necessary safety
27929 conditions are met. In practice, this would cause GNAT to be incompatible
27930 with correctly written Ada 83 code that had all necessary
27931 @code{pragma Elaborate} statements in place. Consequently, we made the
27932 decision that GNAT in its default mode will believe that if it encounters
27933 a @code{pragma Elaborate} then the programmer knows what they are doing,
27934 and it will trust that no elaboration errors can occur.
27935
27936 The result of this decision is two-fold. First to be safe using the
27937 static mode, you should remove all @code{pragma Elaborate} statements.
27938 Second, when fixing circularities in existing code, you can selectively
27939 use @code{pragma Elaborate} statements to convince the static mode of
27940 GNAT that it need not generate an implicit @code{pragma Elaborate_All}
27941 statement.
27942
27943 When using the static mode with @option{-gnatwl}, any use of
27944 @code{pragma Elaborate} will generate a warning about possible
27945 problems.
27946
27947 @node Elaboration Issues for Library Tasks
27948 @section Elaboration Issues for Library Tasks
27949 @cindex Library tasks, elaboration issues
27950 @cindex Elaboration of library tasks
27951
27952 @noindent
27953 In this section we examine special elaboration issues that arise for
27954 programs that declare library level tasks.
27955
27956 Generally the model of execution of an Ada program is that all units are
27957 elaborated, and then execution of the program starts. However, the
27958 declaration of library tasks definitely does not fit this model. The
27959 reason for this is that library tasks start as soon as they are declared
27960 (more precisely, as soon as the statement part of the enclosing package
27961 body is reached), that is to say before elaboration
27962 of the program is complete. This means that if such a task calls a
27963 subprogram, or an entry in another task, the callee may or may not be
27964 elaborated yet, and in the standard
27965 Reference Manual model of dynamic elaboration checks, you can even
27966 get timing dependent Program_Error exceptions, since there can be
27967 a race between the elaboration code and the task code.
27968
27969 The static model of elaboration in GNAT seeks to avoid all such
27970 dynamic behavior, by being conservative, and the conservative
27971 approach in this particular case is to assume that all the code
27972 in a task body is potentially executed at elaboration time if
27973 a task is declared at the library level.
27974
27975 This can definitely result in unexpected circularities. Consider
27976 the following example
27977
27978 @smallexample @c ada
27979 package Decls is
27980 task Lib_Task is
27981 entry Start;
27982 end Lib_Task;
27983
27984 type My_Int is new Integer;
27985
27986 function Ident (M : My_Int) return My_Int;
27987 end Decls;
27988
27989 with Utils;
27990 package body Decls is
27991 task body Lib_Task is
27992 begin
27993 accept Start;
27994 Utils.Put_Val (2);
27995 end Lib_Task;
27996
27997 function Ident (M : My_Int) return My_Int is
27998 begin
27999 return M;
28000 end Ident;
28001 end Decls;
28002
28003 with Decls;
28004 package Utils is
28005 procedure Put_Val (Arg : Decls.My_Int);
28006 end Utils;
28007
28008 with Text_IO;
28009 package body Utils is
28010 procedure Put_Val (Arg : Decls.My_Int) is
28011 begin
28012 Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
28013 end Put_Val;
28014 end Utils;
28015
28016 with Decls;
28017 procedure Main is
28018 begin
28019 Decls.Lib_Task.Start;
28020 end;
28021 @end smallexample
28022
28023 @noindent
28024 If the above example is compiled in the default static elaboration
28025 mode, then a circularity occurs. The circularity comes from the call
28026 @code{Utils.Put_Val} in the task body of @code{Decls.Lib_Task}. Since
28027 this call occurs in elaboration code, we need an implicit pragma
28028 @code{Elaborate_All} for @code{Utils}. This means that not only must
28029 the spec and body of @code{Utils} be elaborated before the body
28030 of @code{Decls}, but also the spec and body of any unit that is
28031 @code{with'ed} by the body of @code{Utils} must also be elaborated before
28032 the body of @code{Decls}. This is the transitive implication of
28033 pragma @code{Elaborate_All} and it makes sense, because in general
28034 the body of @code{Put_Val} might have a call to something in a
28035 @code{with'ed} unit.
28036
28037 In this case, the body of Utils (actually its spec) @code{with's}
28038 @code{Decls}. Unfortunately this means that the body of @code{Decls}
28039 must be elaborated before itself, in case there is a call from the
28040 body of @code{Utils}.
28041
28042 Here is the exact chain of events we are worrying about:
28043
28044 @enumerate
28045 @item
28046 In the body of @code{Decls} a call is made from within the body of a library
28047 task to a subprogram in the package @code{Utils}. Since this call may
28048 occur at elaboration time (given that the task is activated at elaboration
28049 time), we have to assume the worst, i.e., that the
28050 call does happen at elaboration time.
28051
28052 @item
28053 This means that the body and spec of @code{Util} must be elaborated before
28054 the body of @code{Decls} so that this call does not cause an access before
28055 elaboration.
28056
28057 @item
28058 Within the body of @code{Util}, specifically within the body of
28059 @code{Util.Put_Val} there may be calls to any unit @code{with}'ed
28060 by this package.
28061
28062 @item
28063 One such @code{with}'ed package is package @code{Decls}, so there
28064 might be a call to a subprogram in @code{Decls} in @code{Put_Val}.
28065 In fact there is such a call in this example, but we would have to
28066 assume that there was such a call even if it were not there, since
28067 we are not supposed to write the body of @code{Decls} knowing what
28068 is in the body of @code{Utils}; certainly in the case of the
28069 static elaboration model, the compiler does not know what is in
28070 other bodies and must assume the worst.
28071
28072 @item
28073 This means that the spec and body of @code{Decls} must also be
28074 elaborated before we elaborate the unit containing the call, but
28075 that unit is @code{Decls}! This means that the body of @code{Decls}
28076 must be elaborated before itself, and that's a circularity.
28077 @end enumerate
28078
28079 @noindent
28080 Indeed, if you add an explicit pragma @code{Elaborate_All} for @code{Utils} in
28081 the body of @code{Decls} you will get a true Ada Reference Manual
28082 circularity that makes the program illegal.
28083
28084 In practice, we have found that problems with the static model of
28085 elaboration in existing code often arise from library tasks, so
28086 we must address this particular situation.
28087
28088 Note that if we compile and run the program above, using the dynamic model of
28089 elaboration (that is to say use the @option{-gnatE} switch),
28090 then it compiles, binds,
28091 links, and runs, printing the expected result of 2. Therefore in some sense
28092 the circularity here is only apparent, and we need to capture
28093 the properties of this program that distinguish it from other library-level
28094 tasks that have real elaboration problems.
28095
28096 We have four possible answers to this question:
28097
28098 @itemize @bullet
28099
28100 @item
28101 Use the dynamic model of elaboration.
28102
28103 If we use the @option{-gnatE} switch, then as noted above, the program works.
28104 Why is this? If we examine the task body, it is apparent that the task cannot
28105 proceed past the
28106 @code{accept} statement until after elaboration has been completed, because
28107 the corresponding entry call comes from the main program, not earlier.
28108 This is why the dynamic model works here. But that's really giving
28109 up on a precise analysis, and we prefer to take this approach only if we cannot
28110 solve the
28111 problem in any other manner. So let us examine two ways to reorganize
28112 the program to avoid the potential elaboration problem.
28113
28114 @item
28115 Split library tasks into separate packages.
28116
28117 Write separate packages, so that library tasks are isolated from
28118 other declarations as much as possible. Let us look at a variation on
28119 the above program.
28120
28121 @smallexample @c ada
28122 package Decls1 is
28123 task Lib_Task is
28124 entry Start;
28125 end Lib_Task;
28126 end Decls1;
28127
28128 with Utils;
28129 package body Decls1 is
28130 task body Lib_Task is
28131 begin
28132 accept Start;
28133 Utils.Put_Val (2);
28134 end Lib_Task;
28135 end Decls1;
28136
28137 package Decls2 is
28138 type My_Int is new Integer;
28139 function Ident (M : My_Int) return My_Int;
28140 end Decls2;
28141
28142 with Utils;
28143 package body Decls2 is
28144 function Ident (M : My_Int) return My_Int is
28145 begin
28146 return M;
28147 end Ident;
28148 end Decls2;
28149
28150 with Decls2;
28151 package Utils is
28152 procedure Put_Val (Arg : Decls2.My_Int);
28153 end Utils;
28154
28155 with Text_IO;
28156 package body Utils is
28157 procedure Put_Val (Arg : Decls2.My_Int) is
28158 begin
28159 Text_IO.Put_Line (Decls2.My_Int'Image (Decls2.Ident (Arg)));
28160 end Put_Val;
28161 end Utils;
28162
28163 with Decls1;
28164 procedure Main is
28165 begin
28166 Decls1.Lib_Task.Start;
28167 end;
28168 @end smallexample
28169
28170 @noindent
28171 All we have done is to split @code{Decls} into two packages, one
28172 containing the library task, and one containing everything else. Now
28173 there is no cycle, and the program compiles, binds, links and executes
28174 using the default static model of elaboration.
28175
28176 @item
28177 Declare separate task types.
28178
28179 A significant part of the problem arises because of the use of the
28180 single task declaration form. This means that the elaboration of
28181 the task type, and the elaboration of the task itself (i.e.@: the
28182 creation of the task) happen at the same time. A good rule
28183 of style in Ada is to always create explicit task types. By
28184 following the additional step of placing task objects in separate
28185 packages from the task type declaration, many elaboration problems
28186 are avoided. Here is another modified example of the example program:
28187
28188 @smallexample @c ada
28189 package Decls is
28190 task type Lib_Task_Type is
28191 entry Start;
28192 end Lib_Task_Type;
28193
28194 type My_Int is new Integer;
28195
28196 function Ident (M : My_Int) return My_Int;
28197 end Decls;
28198
28199 with Utils;
28200 package body Decls is
28201 task body Lib_Task_Type is
28202 begin
28203 accept Start;
28204 Utils.Put_Val (2);
28205 end Lib_Task_Type;
28206
28207 function Ident (M : My_Int) return My_Int is
28208 begin
28209 return M;
28210 end Ident;
28211 end Decls;
28212
28213 with Decls;
28214 package Utils is
28215 procedure Put_Val (Arg : Decls.My_Int);
28216 end Utils;
28217
28218 with Text_IO;
28219 package body Utils is
28220 procedure Put_Val (Arg : Decls.My_Int) is
28221 begin
28222 Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
28223 end Put_Val;
28224 end Utils;
28225
28226 with Decls;
28227 package Declst is
28228 Lib_Task : Decls.Lib_Task_Type;
28229 end Declst;
28230
28231 with Declst;
28232 procedure Main is
28233 begin
28234 Declst.Lib_Task.Start;
28235 end;
28236 @end smallexample
28237
28238 @noindent
28239 What we have done here is to replace the @code{task} declaration in
28240 package @code{Decls} with a @code{task type} declaration. Then we
28241 introduce a separate package @code{Declst} to contain the actual
28242 task object. This separates the elaboration issues for
28243 the @code{task type}
28244 declaration, which causes no trouble, from the elaboration issues
28245 of the task object, which is also unproblematic, since it is now independent
28246 of the elaboration of @code{Utils}.
28247 This separation of concerns also corresponds to
28248 a generally sound engineering principle of separating declarations
28249 from instances. This version of the program also compiles, binds, links,
28250 and executes, generating the expected output.
28251
28252 @item
28253 Use No_Entry_Calls_In_Elaboration_Code restriction.
28254 @cindex No_Entry_Calls_In_Elaboration_Code
28255
28256 The previous two approaches described how a program can be restructured
28257 to avoid the special problems caused by library task bodies. in practice,
28258 however, such restructuring may be difficult to apply to existing legacy code,
28259 so we must consider solutions that do not require massive rewriting.
28260
28261 Let us consider more carefully why our original sample program works
28262 under the dynamic model of elaboration. The reason is that the code
28263 in the task body blocks immediately on the @code{accept}
28264 statement. Now of course there is nothing to prohibit elaboration
28265 code from making entry calls (for example from another library level task),
28266 so we cannot tell in isolation that
28267 the task will not execute the accept statement during elaboration.
28268
28269 However, in practice it is very unusual to see elaboration code
28270 make any entry calls, and the pattern of tasks starting
28271 at elaboration time and then immediately blocking on @code{accept} or
28272 @code{select} statements is very common. What this means is that
28273 the compiler is being too pessimistic when it analyzes the
28274 whole package body as though it might be executed at elaboration
28275 time.
28276
28277 If we know that the elaboration code contains no entry calls, (a very safe
28278 assumption most of the time, that could almost be made the default
28279 behavior), then we can compile all units of the program under control
28280 of the following configuration pragma:
28281
28282 @smallexample
28283 pragma Restrictions (No_Entry_Calls_In_Elaboration_Code);
28284 @end smallexample
28285
28286 @noindent
28287 This pragma can be placed in the @file{gnat.adc} file in the usual
28288 manner. If we take our original unmodified program and compile it
28289 in the presence of a @file{gnat.adc} containing the above pragma,
28290 then once again, we can compile, bind, link, and execute, obtaining
28291 the expected result. In the presence of this pragma, the compiler does
28292 not trace calls in a task body, that appear after the first @code{accept}
28293 or @code{select} statement, and therefore does not report a potential
28294 circularity in the original program.
28295
28296 The compiler will check to the extent it can that the above
28297 restriction is not violated, but it is not always possible to do a
28298 complete check at compile time, so it is important to use this
28299 pragma only if the stated restriction is in fact met, that is to say
28300 no task receives an entry call before elaboration of all units is completed.
28301
28302 @end itemize
28303
28304 @node Mixing Elaboration Models
28305 @section Mixing Elaboration Models
28306 @noindent
28307 So far, we have assumed that the entire program is either compiled
28308 using the dynamic model or static model, ensuring consistency. It
28309 is possible to mix the two models, but rules have to be followed
28310 if this mixing is done to ensure that elaboration checks are not
28311 omitted.
28312
28313 The basic rule is that @emph{a unit compiled with the static model cannot
28314 be @code{with'ed} by a unit compiled with the dynamic model}. The
28315 reason for this is that in the static model, a unit assumes that
28316 its clients guarantee to use (the equivalent of) pragma
28317 @code{Elaborate_All} so that no elaboration checks are required
28318 in inner subprograms, and this assumption is violated if the
28319 client is compiled with dynamic checks.
28320
28321 The precise rule is as follows. A unit that is compiled with dynamic
28322 checks can only @code{with} a unit that meets at least one of the
28323 following criteria:
28324
28325 @itemize @bullet
28326
28327 @item
28328 The @code{with'ed} unit is itself compiled with dynamic elaboration
28329 checks (that is with the @option{-gnatE} switch.
28330
28331 @item
28332 The @code{with'ed} unit is an internal GNAT implementation unit from
28333 the System, Interfaces, Ada, or GNAT hierarchies.
28334
28335 @item
28336 The @code{with'ed} unit has pragma Preelaborate or pragma Pure.
28337
28338 @item
28339 The @code{with'ing} unit (that is the client) has an explicit pragma
28340 @code{Elaborate_All} for the @code{with'ed} unit.
28341
28342 @end itemize
28343
28344 @noindent
28345 If this rule is violated, that is if a unit with dynamic elaboration
28346 checks @code{with's} a unit that does not meet one of the above four
28347 criteria, then the binder (@code{gnatbind}) will issue a warning
28348 similar to that in the following example:
28349
28350 @smallexample
28351 warning: "x.ads" has dynamic elaboration checks and with's
28352 warning: "y.ads" which has static elaboration checks
28353 @end smallexample
28354
28355 @noindent
28356 These warnings indicate that the rule has been violated, and that as a result
28357 elaboration checks may be missed in the resulting executable file.
28358 This warning may be suppressed using the @option{-ws} binder switch
28359 in the usual manner.
28360
28361 One useful application of this mixing rule is in the case of a subsystem
28362 which does not itself @code{with} units from the remainder of the
28363 application. In this case, the entire subsystem can be compiled with
28364 dynamic checks to resolve a circularity in the subsystem, while
28365 allowing the main application that uses this subsystem to be compiled
28366 using the more reliable default static model.
28367
28368 @node What to Do If the Default Elaboration Behavior Fails
28369 @section What to Do If the Default Elaboration Behavior Fails
28370
28371 @noindent
28372 If the binder cannot find an acceptable order, it outputs detailed
28373 diagnostics. For example:
28374 @smallexample
28375 @group
28376 @iftex
28377 @leftskip=0cm
28378 @end iftex
28379 error: elaboration circularity detected
28380 info: "proc (body)" must be elaborated before "pack (body)"
28381 info: reason: Elaborate_All probably needed in unit "pack (body)"
28382 info: recompile "pack (body)" with -gnatwl
28383 info: for full details
28384 info: "proc (body)"
28385 info: is needed by its spec:
28386 info: "proc (spec)"
28387 info: which is withed by:
28388 info: "pack (body)"
28389 info: "pack (body)" must be elaborated before "proc (body)"
28390 info: reason: pragma Elaborate in unit "proc (body)"
28391 @end group
28392
28393 @end smallexample
28394
28395 @noindent
28396 In this case we have a cycle that the binder cannot break. On the one
28397 hand, there is an explicit pragma Elaborate in @code{proc} for
28398 @code{pack}. This means that the body of @code{pack} must be elaborated
28399 before the body of @code{proc}. On the other hand, there is elaboration
28400 code in @code{pack} that calls a subprogram in @code{proc}. This means
28401 that for maximum safety, there should really be a pragma
28402 Elaborate_All in @code{pack} for @code{proc} which would require that
28403 the body of @code{proc} be elaborated before the body of
28404 @code{pack}. Clearly both requirements cannot be satisfied.
28405 Faced with a circularity of this kind, you have three different options.
28406
28407 @table @asis
28408 @item Fix the program
28409 The most desirable option from the point of view of long-term maintenance
28410 is to rearrange the program so that the elaboration problems are avoided.
28411 One useful technique is to place the elaboration code into separate
28412 child packages. Another is to move some of the initialization code to
28413 explicitly called subprograms, where the program controls the order
28414 of initialization explicitly. Although this is the most desirable option,
28415 it may be impractical and involve too much modification, especially in
28416 the case of complex legacy code.
28417
28418 @item Perform dynamic checks
28419 If the compilations are done using the
28420 @option{-gnatE}
28421 (dynamic elaboration check) switch, then GNAT behaves in a quite different
28422 manner. Dynamic checks are generated for all calls that could possibly result
28423 in raising an exception. With this switch, the compiler does not generate
28424 implicit @code{Elaborate} or @code{Elaborate_All} pragmas. The behavior then is
28425 exactly as specified in the @cite{Ada Reference Manual}.
28426 The binder will generate
28427 an executable program that may or may not raise @code{Program_Error}, and then
28428 it is the programmer's job to ensure that it does not raise an exception. Note
28429 that it is important to compile all units with the switch, it cannot be used
28430 selectively.
28431
28432 @item Suppress checks
28433 The drawback of dynamic checks is that they generate a
28434 significant overhead at run time, both in space and time. If you
28435 are absolutely sure that your program cannot raise any elaboration
28436 exceptions, and you still want to use the dynamic elaboration model,
28437 then you can use the configuration pragma
28438 @code{Suppress (Elaboration_Check)} to suppress all such checks. For
28439 example this pragma could be placed in the @file{gnat.adc} file.
28440
28441 @item Suppress checks selectively
28442 When you know that certain calls or instantiations in elaboration code cannot
28443 possibly lead to an elaboration error, and the binder nevertheless complains
28444 about implicit @code{Elaborate} and @code{Elaborate_All} pragmas that lead to
28445 elaboration circularities, it is possible to remove those warnings locally and
28446 obtain a program that will bind. Clearly this can be unsafe, and it is the
28447 responsibility of the programmer to make sure that the resulting program has no
28448 elaboration anomalies. The pragma @code{Suppress (Elaboration_Check)} can be
28449 used with different granularity to suppress warnings and break elaboration
28450 circularities:
28451
28452 @itemize @bullet
28453 @item
28454 Place the pragma that names the called subprogram in the declarative part
28455 that contains the call.
28456
28457 @item
28458 Place the pragma in the declarative part, without naming an entity. This
28459 disables warnings on all calls in the corresponding declarative region.
28460
28461 @item
28462 Place the pragma in the package spec that declares the called subprogram,
28463 and name the subprogram. This disables warnings on all elaboration calls to
28464 that subprogram.
28465
28466 @item
28467 Place the pragma in the package spec that declares the called subprogram,
28468 without naming any entity. This disables warnings on all elaboration calls to
28469 all subprograms declared in this spec.
28470
28471 @item Use Pragma Elaborate
28472 As previously described in section @xref{Treatment of Pragma Elaborate},
28473 GNAT in static mode assumes that a @code{pragma} Elaborate indicates correctly
28474 that no elaboration checks are required on calls to the designated unit.
28475 There may be cases in which the caller knows that no transitive calls
28476 can occur, so that a @code{pragma Elaborate} will be sufficient in a
28477 case where @code{pragma Elaborate_All} would cause a circularity.
28478 @end itemize
28479
28480 @noindent
28481 These five cases are listed in order of decreasing safety, and therefore
28482 require increasing programmer care in their application. Consider the
28483 following program:
28484
28485 @smallexample @c adanocomment
28486 package Pack1 is
28487 function F1 return Integer;
28488 X1 : Integer;
28489 end Pack1;
28490
28491 package Pack2 is
28492 function F2 return Integer;
28493 function Pure (x : integer) return integer;
28494 -- pragma Suppress (Elaboration_Check, On => Pure); -- (3)
28495 -- pragma Suppress (Elaboration_Check); -- (4)
28496 end Pack2;
28497
28498 with Pack2;
28499 package body Pack1 is
28500 function F1 return Integer is
28501 begin
28502 return 100;
28503 end F1;
28504 Val : integer := Pack2.Pure (11); -- Elab. call (1)
28505 begin
28506 declare
28507 -- pragma Suppress(Elaboration_Check, Pack2.F2); -- (1)
28508 -- pragma Suppress(Elaboration_Check); -- (2)
28509 begin
28510 X1 := Pack2.F2 + 1; -- Elab. call (2)
28511 end;
28512 end Pack1;
28513
28514 with Pack1;
28515 package body Pack2 is
28516 function F2 return Integer is
28517 begin
28518 return Pack1.F1;
28519 end F2;
28520 function Pure (x : integer) return integer is
28521 begin
28522 return x ** 3 - 3 * x;
28523 end;
28524 end Pack2;
28525
28526 with Pack1, Ada.Text_IO;
28527 procedure Proc3 is
28528 begin
28529 Ada.Text_IO.Put_Line(Pack1.X1'Img); -- 101
28530 end Proc3;
28531 @end smallexample
28532 In the absence of any pragmas, an attempt to bind this program produces
28533 the following diagnostics:
28534 @smallexample
28535 @group
28536 @iftex
28537 @leftskip=.5cm
28538 @end iftex
28539 error: elaboration circularity detected
28540 info: "pack1 (body)" must be elaborated before "pack1 (body)"
28541 info: reason: Elaborate_All probably needed in unit "pack1 (body)"
28542 info: recompile "pack1 (body)" with -gnatwl for full details
28543 info: "pack1 (body)"
28544 info: must be elaborated along with its spec:
28545 info: "pack1 (spec)"
28546 info: which is withed by:
28547 info: "pack2 (body)"
28548 info: which must be elaborated along with its spec:
28549 info: "pack2 (spec)"
28550 info: which is withed by:
28551 info: "pack1 (body)"
28552 @end group
28553 @end smallexample
28554 The sources of the circularity are the two calls to @code{Pack2.Pure} and
28555 @code{Pack2.F2} in the body of @code{Pack1}. We can see that the call to
28556 F2 is safe, even though F2 calls F1, because the call appears after the
28557 elaboration of the body of F1. Therefore the pragma (1) is safe, and will
28558 remove the warning on the call. It is also possible to use pragma (2)
28559 because there are no other potentially unsafe calls in the block.
28560
28561 @noindent
28562 The call to @code{Pure} is safe because this function does not depend on the
28563 state of @code{Pack2}. Therefore any call to this function is safe, and it
28564 is correct to place pragma (3) in the corresponding package spec.
28565
28566 @noindent
28567 Finally, we could place pragma (4) in the spec of @code{Pack2} to disable
28568 warnings on all calls to functions declared therein. Note that this is not
28569 necessarily safe, and requires more detailed examination of the subprogram
28570 bodies involved. In particular, a call to @code{F2} requires that @code{F1}
28571 be already elaborated.
28572 @end table
28573
28574 @noindent
28575 It is hard to generalize on which of these four approaches should be
28576 taken. Obviously if it is possible to fix the program so that the default
28577 treatment works, this is preferable, but this may not always be practical.
28578 It is certainly simple enough to use
28579 @option{-gnatE}
28580 but the danger in this case is that, even if the GNAT binder
28581 finds a correct elaboration order, it may not always do so,
28582 and certainly a binder from another Ada compiler might not. A
28583 combination of testing and analysis (for which the warnings generated
28584 with the
28585 @option{-gnatwl}
28586 switch can be useful) must be used to ensure that the program is free
28587 of errors. One switch that is useful in this testing is the
28588 @option{^-p (pessimistic elaboration order)^/PESSIMISTIC_ELABORATION_ORDER^}
28589 switch for
28590 @code{gnatbind}.
28591 Normally the binder tries to find an order that has the best chance
28592 of avoiding elaboration problems. However, if this switch is used, the binder
28593 plays a devil's advocate role, and tries to choose the order that
28594 has the best chance of failing. If your program works even with this
28595 switch, then it has a better chance of being error free, but this is still
28596 not a guarantee.
28597
28598 For an example of this approach in action, consider the C-tests (executable
28599 tests) from the ACVC suite. If these are compiled and run with the default
28600 treatment, then all but one of them succeed without generating any error
28601 diagnostics from the binder. However, there is one test that fails, and
28602 this is not surprising, because the whole point of this test is to ensure
28603 that the compiler can handle cases where it is impossible to determine
28604 a correct order statically, and it checks that an exception is indeed
28605 raised at run time.
28606
28607 This one test must be compiled and run using the
28608 @option{-gnatE}
28609 switch, and then it passes. Alternatively, the entire suite can
28610 be run using this switch. It is never wrong to run with the dynamic
28611 elaboration switch if your code is correct, and we assume that the
28612 C-tests are indeed correct (it is less efficient, but efficiency is
28613 not a factor in running the ACVC tests.)
28614
28615 @node Elaboration for Access-to-Subprogram Values
28616 @section Elaboration for Access-to-Subprogram Values
28617 @cindex Access-to-subprogram
28618
28619 @noindent
28620 Access-to-subprogram types (introduced in Ada 95) complicate
28621 the handling of elaboration. The trouble is that it becomes
28622 impossible to tell at compile time which procedure
28623 is being called. This means that it is not possible for the binder
28624 to analyze the elaboration requirements in this case.
28625
28626 If at the point at which the access value is created
28627 (i.e., the evaluation of @code{P'Access} for a subprogram @code{P}),
28628 the body of the subprogram is
28629 known to have been elaborated, then the access value is safe, and its use
28630 does not require a check. This may be achieved by appropriate arrangement
28631 of the order of declarations if the subprogram is in the current unit,
28632 or, if the subprogram is in another unit, by using pragma
28633 @code{Pure}, @code{Preelaborate}, or @code{Elaborate_Body}
28634 on the referenced unit.
28635
28636 If the referenced body is not known to have been elaborated at the point
28637 the access value is created, then any use of the access value must do a
28638 dynamic check, and this dynamic check will fail and raise a
28639 @code{Program_Error} exception if the body has not been elaborated yet.
28640 GNAT will generate the necessary checks, and in addition, if the
28641 @option{-gnatwl}
28642 switch is set, will generate warnings that such checks are required.
28643
28644 The use of dynamic dispatching for tagged types similarly generates
28645 a requirement for dynamic checks, and premature calls to any primitive
28646 operation of a tagged type before the body of the operation has been
28647 elaborated, will result in the raising of @code{Program_Error}.
28648
28649 @node Summary of Procedures for Elaboration Control
28650 @section Summary of Procedures for Elaboration Control
28651 @cindex Elaboration control
28652
28653 @noindent
28654 First, compile your program with the default options, using none of
28655 the special elaboration control switches. If the binder successfully
28656 binds your program, then you can be confident that, apart from issues
28657 raised by the use of access-to-subprogram types and dynamic dispatching,
28658 the program is free of elaboration errors. If it is important that the
28659 program be portable, then use the
28660 @option{-gnatwl}
28661 switch to generate warnings about missing @code{Elaborate} or
28662 @code{Elaborate_All} pragmas, and supply the missing pragmas.
28663
28664 If the program fails to bind using the default static elaboration
28665 handling, then you can fix the program to eliminate the binder
28666 message, or recompile the entire program with the
28667 @option{-gnatE} switch to generate dynamic elaboration checks,
28668 and, if you are sure there really are no elaboration problems,
28669 use a global pragma @code{Suppress (Elaboration_Check)}.
28670
28671 @node Other Elaboration Order Considerations
28672 @section Other Elaboration Order Considerations
28673 @noindent
28674 This section has been entirely concerned with the issue of finding a valid
28675 elaboration order, as defined by the Ada Reference Manual. In a case
28676 where several elaboration orders are valid, the task is to find one
28677 of the possible valid elaboration orders (and the static model in GNAT
28678 will ensure that this is achieved).
28679
28680 The purpose of the elaboration rules in the Ada Reference Manual is to
28681 make sure that no entity is accessed before it has been elaborated. For
28682 a subprogram, this means that the spec and body must have been elaborated
28683 before the subprogram is called. For an object, this means that the object
28684 must have been elaborated before its value is read or written. A violation
28685 of either of these two requirements is an access before elaboration order,
28686 and this section has been all about avoiding such errors.
28687
28688 In the case where more than one order of elaboration is possible, in the
28689 sense that access before elaboration errors are avoided, then any one of
28690 the orders is ``correct'' in the sense that it meets the requirements of
28691 the Ada Reference Manual, and no such error occurs.
28692
28693 However, it may be the case for a given program, that there are
28694 constraints on the order of elaboration that come not from consideration
28695 of avoiding elaboration errors, but rather from extra-lingual logic
28696 requirements. Consider this example:
28697
28698 @smallexample @c ada
28699 with Init_Constants;
28700 package Constants is
28701 X : Integer := 0;
28702 Y : Integer := 0;
28703 end Constants;
28704
28705 package Init_Constants is
28706 procedure P; -- require a body
28707 end Init_Constants;
28708
28709 with Constants;
28710 package body Init_Constants is
28711 procedure P is begin null; end;
28712 begin
28713 Constants.X := 3;
28714 Constants.Y := 4;
28715 end Init_Constants;
28716
28717 with Constants;
28718 package Calc is
28719 Z : Integer := Constants.X + Constants.Y;
28720 end Calc;
28721
28722 with Calc;
28723 with Text_IO; use Text_IO;
28724 procedure Main is
28725 begin
28726 Put_Line (Calc.Z'Img);
28727 end Main;
28728 @end smallexample
28729
28730 @noindent
28731 In this example, there is more than one valid order of elaboration. For
28732 example both the following are correct orders:
28733
28734 @smallexample
28735 Init_Constants spec
28736 Constants spec
28737 Calc spec
28738 Init_Constants body
28739 Main body
28740
28741 and
28742
28743 Init_Constants spec
28744 Init_Constants body
28745 Constants spec
28746 Calc spec
28747 Main body
28748 @end smallexample
28749
28750 @noindent
28751 There is no language rule to prefer one or the other, both are correct
28752 from an order of elaboration point of view. But the programmatic effects
28753 of the two orders are very different. In the first, the elaboration routine
28754 of @code{Calc} initializes @code{Z} to zero, and then the main program
28755 runs with this value of zero. But in the second order, the elaboration
28756 routine of @code{Calc} runs after the body of Init_Constants has set
28757 @code{X} and @code{Y} and thus @code{Z} is set to 7 before @code{Main}
28758 runs.
28759
28760 One could perhaps by applying pretty clever non-artificial intelligence
28761 to the situation guess that it is more likely that the second order of
28762 elaboration is the one desired, but there is no formal linguistic reason
28763 to prefer one over the other. In fact in this particular case, GNAT will
28764 prefer the second order, because of the rule that bodies are elaborated
28765 as soon as possible, but it's just luck that this is what was wanted
28766 (if indeed the second order was preferred).
28767
28768 If the program cares about the order of elaboration routines in a case like
28769 this, it is important to specify the order required. In this particular
28770 case, that could have been achieved by adding to the spec of Calc:
28771
28772 @smallexample @c ada
28773 pragma Elaborate_All (Constants);
28774 @end smallexample
28775
28776 @noindent
28777 which requires that the body (if any) and spec of @code{Constants},
28778 as well as the body and spec of any unit @code{with}'ed by
28779 @code{Constants} be elaborated before @code{Calc} is elaborated.
28780
28781 Clearly no automatic method can always guess which alternative you require,
28782 and if you are working with legacy code that had constraints of this kind
28783 which were not properly specified by adding @code{Elaborate} or
28784 @code{Elaborate_All} pragmas, then indeed it is possible that two different
28785 compilers can choose different orders.
28786
28787 However, GNAT does attempt to diagnose the common situation where there
28788 are uninitialized variables in the visible part of a package spec, and the
28789 corresponding package body has an elaboration block that directly or
28790 indirectly initialized one or more of these variables. This is the situation
28791 in which a pragma Elaborate_Body is usually desirable, and GNAT will generate
28792 a warning that suggests this addition if it detects this situation.
28793
28794 The @code{gnatbind}
28795 @option{^-p^/PESSIMISTIC_ELABORATION^} switch may be useful in smoking
28796 out problems. This switch causes bodies to be elaborated as late as possible
28797 instead of as early as possible. In the example above, it would have forced
28798 the choice of the first elaboration order. If you get different results
28799 when using this switch, and particularly if one set of results is right,
28800 and one is wrong as far as you are concerned, it shows that you have some
28801 missing @code{Elaborate} pragmas. For the example above, we have the
28802 following output:
28803
28804 @smallexample
28805 gnatmake -f -q main
28806 main
28807 7
28808 gnatmake -f -q main -bargs -p
28809 main
28810 0
28811 @end smallexample
28812
28813 @noindent
28814 It is of course quite unlikely that both these results are correct, so
28815 it is up to you in a case like this to investigate the source of the
28816 difference, by looking at the two elaboration orders that are chosen,
28817 and figuring out which is correct, and then adding the necessary
28818 @code{Elaborate} or @code{Elaborate_All} pragmas to ensure the desired order.
28819
28820
28821
28822 @c *******************************
28823 @node Conditional Compilation
28824 @appendix Conditional Compilation
28825 @c *******************************
28826 @cindex Conditional compilation
28827
28828 @noindent
28829 It is often necessary to arrange for a single source program
28830 to serve multiple purposes, where it is compiled in different
28831 ways to achieve these different goals. Some examples of the
28832 need for this feature are
28833
28834 @itemize @bullet
28835 @item Adapting a program to a different hardware environment
28836 @item Adapting a program to a different target architecture
28837 @item Turning debugging features on and off
28838 @item Arranging for a program to compile with different compilers
28839 @end itemize
28840
28841 @noindent
28842 In C, or C++, the typical approach would be to use the preprocessor
28843 that is defined as part of the language. The Ada language does not
28844 contain such a feature. This is not an oversight, but rather a very
28845 deliberate design decision, based on the experience that overuse of
28846 the preprocessing features in C and C++ can result in programs that
28847 are extremely difficult to maintain. For example, if we have ten
28848 switches that can be on or off, this means that there are a thousand
28849 separate programs, any one of which might not even be syntactically
28850 correct, and even if syntactically correct, the resulting program
28851 might not work correctly. Testing all combinations can quickly become
28852 impossible.
28853
28854 Nevertheless, the need to tailor programs certainly exists, and in
28855 this Appendix we will discuss how this can
28856 be achieved using Ada in general, and GNAT in particular.
28857
28858 @menu
28859 * Use of Boolean Constants::
28860 * Debugging - A Special Case::
28861 * Conditionalizing Declarations::
28862 * Use of Alternative Implementations::
28863 * Preprocessing::
28864 @end menu
28865
28866 @node Use of Boolean Constants
28867 @section Use of Boolean Constants
28868
28869 @noindent
28870 In the case where the difference is simply which code
28871 sequence is executed, the cleanest solution is to use Boolean
28872 constants to control which code is executed.
28873
28874 @smallexample @c ada
28875 @group
28876 FP_Initialize_Required : constant Boolean := True;
28877 @dots{}
28878 if FP_Initialize_Required then
28879 @dots{}
28880 end if;
28881 @end group
28882 @end smallexample
28883
28884 @noindent
28885 Not only will the code inside the @code{if} statement not be executed if
28886 the constant Boolean is @code{False}, but it will also be completely
28887 deleted from the program.
28888 However, the code is only deleted after the @code{if} statement
28889 has been checked for syntactic and semantic correctness.
28890 (In contrast, with preprocessors the code is deleted before the
28891 compiler ever gets to see it, so it is not checked until the switch
28892 is turned on.)
28893 @cindex Preprocessors (contrasted with conditional compilation)
28894
28895 Typically the Boolean constants will be in a separate package,
28896 something like:
28897
28898 @smallexample @c ada
28899 @group
28900 package Config is
28901 FP_Initialize_Required : constant Boolean := True;
28902 Reset_Available : constant Boolean := False;
28903 @dots{}
28904 end Config;
28905 @end group
28906 @end smallexample
28907
28908 @noindent
28909 The @code{Config} package exists in multiple forms for the various targets,
28910 with an appropriate script selecting the version of @code{Config} needed.
28911 Then any other unit requiring conditional compilation can do a @code{with}
28912 of @code{Config} to make the constants visible.
28913
28914
28915 @node Debugging - A Special Case
28916 @section Debugging - A Special Case
28917
28918 @noindent
28919 A common use of conditional code is to execute statements (for example
28920 dynamic checks, or output of intermediate results) under control of a
28921 debug switch, so that the debugging behavior can be turned on and off.
28922 This can be done using a Boolean constant to control whether the code
28923 is active:
28924
28925 @smallexample @c ada
28926 @group
28927 if Debugging then
28928 Put_Line ("got to the first stage!");
28929 end if;
28930 @end group
28931 @end smallexample
28932
28933 @noindent
28934 or
28935
28936 @smallexample @c ada
28937 @group
28938 if Debugging and then Temperature > 999.0 then
28939 raise Temperature_Crazy;
28940 end if;
28941 @end group
28942 @end smallexample
28943
28944 @noindent
28945 Since this is a common case, there are special features to deal with
28946 this in a convenient manner. For the case of tests, Ada 2005 has added
28947 a pragma @code{Assert} that can be used for such tests. This pragma is modeled
28948 @cindex pragma @code{Assert}
28949 on the @code{Assert} pragma that has always been available in GNAT, so this
28950 feature may be used with GNAT even if you are not using Ada 2005 features.
28951 The use of pragma @code{Assert} is described in
28952 @ref{Pragma Assert,,, gnat_rm, GNAT Reference Manual}, but as an
28953 example, the last test could be written:
28954
28955 @smallexample @c ada
28956 pragma Assert (Temperature <= 999.0, "Temperature Crazy");
28957 @end smallexample
28958
28959 @noindent
28960 or simply
28961
28962 @smallexample @c ada
28963 pragma Assert (Temperature <= 999.0);
28964 @end smallexample
28965
28966 @noindent
28967 In both cases, if assertions are active and the temperature is excessive,
28968 the exception @code{Assert_Failure} will be raised, with the given string in
28969 the first case or a string indicating the location of the pragma in the second
28970 case used as the exception message.
28971
28972 You can turn assertions on and off by using the @code{Assertion_Policy}
28973 pragma.
28974 @cindex pragma @code{Assertion_Policy}
28975 This is an Ada 2005 pragma which is implemented in all modes by
28976 GNAT, but only in the latest versions of GNAT which include Ada 2005
28977 capability. Alternatively, you can use the @option{-gnata} switch
28978 @cindex @option{-gnata} switch
28979 to enable assertions from the command line (this is recognized by all versions
28980 of GNAT).
28981
28982 For the example above with the @code{Put_Line}, the GNAT-specific pragma
28983 @code{Debug} can be used:
28984 @cindex pragma @code{Debug}
28985
28986 @smallexample @c ada
28987 pragma Debug (Put_Line ("got to the first stage!"));
28988 @end smallexample
28989
28990 @noindent
28991 If debug pragmas are enabled, the argument, which must be of the form of
28992 a procedure call, is executed (in this case, @code{Put_Line} will be called).
28993 Only one call can be present, but of course a special debugging procedure
28994 containing any code you like can be included in the program and then
28995 called in a pragma @code{Debug} argument as needed.
28996
28997 One advantage of pragma @code{Debug} over the @code{if Debugging then}
28998 construct is that pragma @code{Debug} can appear in declarative contexts,
28999 such as at the very beginning of a procedure, before local declarations have
29000 been elaborated.
29001
29002 Debug pragmas are enabled using either the @option{-gnata} switch that also
29003 controls assertions, or with a separate Debug_Policy pragma.
29004 @cindex pragma @code{Debug_Policy}
29005 The latter pragma is new in the Ada 2005 versions of GNAT (but it can be used
29006 in Ada 95 and Ada 83 programs as well), and is analogous to
29007 pragma @code{Assertion_Policy} to control assertions.
29008
29009 @code{Assertion_Policy} and @code{Debug_Policy} are configuration pragmas,
29010 and thus they can appear in @file{gnat.adc} if you are not using a
29011 project file, or in the file designated to contain configuration pragmas
29012 in a project file.
29013 They then apply to all subsequent compilations. In practice the use of
29014 the @option{-gnata} switch is often the most convenient method of controlling
29015 the status of these pragmas.
29016
29017 Note that a pragma is not a statement, so in contexts where a statement
29018 sequence is required, you can't just write a pragma on its own. You have
29019 to add a @code{null} statement.
29020
29021 @smallexample @c ada
29022 @group
29023 if @dots{} then
29024 @dots{} -- some statements
29025 else
29026 pragma Assert (Num_Cases < 10);
29027 null;
29028 end if;
29029 @end group
29030 @end smallexample
29031
29032
29033 @node Conditionalizing Declarations
29034 @section Conditionalizing Declarations
29035
29036 @noindent
29037 In some cases, it may be necessary to conditionalize declarations to meet
29038 different requirements. For example we might want a bit string whose length
29039 is set to meet some hardware message requirement.
29040
29041 In some cases, it may be possible to do this using declare blocks controlled
29042 by conditional constants:
29043
29044 @smallexample @c ada
29045 @group
29046 if Small_Machine then
29047 declare
29048 X : Bit_String (1 .. 10);
29049 begin
29050 @dots{}
29051 end;
29052 else
29053 declare
29054 X : Large_Bit_String (1 .. 1000);
29055 begin
29056 @dots{}
29057 end;
29058 end if;
29059 @end group
29060 @end smallexample
29061
29062 @noindent
29063 Note that in this approach, both declarations are analyzed by the
29064 compiler so this can only be used where both declarations are legal,
29065 even though one of them will not be used.
29066
29067 Another approach is to define integer constants, e.g.@: @code{Bits_Per_Word}, or
29068 Boolean constants, e.g.@: @code{Little_Endian}, and then write declarations
29069 that are parameterized by these constants. For example
29070
29071 @smallexample @c ada
29072 @group
29073 for Rec use
29074 Field1 at 0 range Boolean'Pos (Little_Endian) * 10 .. Bits_Per_Word;
29075 end record;
29076 @end group
29077 @end smallexample
29078
29079 @noindent
29080 If @code{Bits_Per_Word} is set to 32, this generates either
29081
29082 @smallexample @c ada
29083 @group
29084 for Rec use
29085 Field1 at 0 range 0 .. 32;
29086 end record;
29087 @end group
29088 @end smallexample
29089
29090 @noindent
29091 for the big endian case, or
29092
29093 @smallexample @c ada
29094 @group
29095 for Rec use record
29096 Field1 at 0 range 10 .. 32;
29097 end record;
29098 @end group
29099 @end smallexample
29100
29101 @noindent
29102 for the little endian case. Since a powerful subset of Ada expression
29103 notation is usable for creating static constants, clever use of this
29104 feature can often solve quite difficult problems in conditionalizing
29105 compilation (note incidentally that in Ada 95, the little endian
29106 constant was introduced as @code{System.Default_Bit_Order}, so you do not
29107 need to define this one yourself).
29108
29109
29110 @node Use of Alternative Implementations
29111 @section Use of Alternative Implementations
29112
29113 @noindent
29114 In some cases, none of the approaches described above are adequate. This
29115 can occur for example if the set of declarations required is radically
29116 different for two different configurations.
29117
29118 In this situation, the official Ada way of dealing with conditionalizing
29119 such code is to write separate units for the different cases. As long as
29120 this does not result in excessive duplication of code, this can be done
29121 without creating maintenance problems. The approach is to share common
29122 code as far as possible, and then isolate the code and declarations
29123 that are different. Subunits are often a convenient method for breaking
29124 out a piece of a unit that is to be conditionalized, with separate files
29125 for different versions of the subunit for different targets, where the
29126 build script selects the right one to give to the compiler.
29127 @cindex Subunits (and conditional compilation)
29128
29129 As an example, consider a situation where a new feature in Ada 2005
29130 allows something to be done in a really nice way. But your code must be able
29131 to compile with an Ada 95 compiler. Conceptually you want to say:
29132
29133 @smallexample @c ada
29134 @group
29135 if Ada_2005 then
29136 @dots{} neat Ada 2005 code
29137 else
29138 @dots{} not quite as neat Ada 95 code
29139 end if;
29140 @end group
29141 @end smallexample
29142
29143 @noindent
29144 where @code{Ada_2005} is a Boolean constant.
29145
29146 But this won't work when @code{Ada_2005} is set to @code{False},
29147 since the @code{then} clause will be illegal for an Ada 95 compiler.
29148 (Recall that although such unreachable code would eventually be deleted
29149 by the compiler, it still needs to be legal. If it uses features
29150 introduced in Ada 2005, it will be illegal in Ada 95.)
29151
29152 So instead we write
29153
29154 @smallexample @c ada
29155 procedure Insert is separate;
29156 @end smallexample
29157
29158 @noindent
29159 Then we have two files for the subunit @code{Insert}, with the two sets of
29160 code.
29161 If the package containing this is called @code{File_Queries}, then we might
29162 have two files
29163
29164 @itemize @bullet
29165 @item @file{file_queries-insert-2005.adb}
29166 @item @file{file_queries-insert-95.adb}
29167 @end itemize
29168
29169 @noindent
29170 and the build script renames the appropriate file to
29171
29172 @smallexample
29173 file_queries-insert.adb
29174 @end smallexample
29175
29176 @noindent
29177 and then carries out the compilation.
29178
29179 This can also be done with project files' naming schemes. For example:
29180
29181 @smallexample @c project
29182 For Body ("File_Queries.Insert") use "file_queries-insert-2005.ada";
29183 @end smallexample
29184
29185 @noindent
29186 Note also that with project files it is desirable to use a different extension
29187 than @file{ads} / @file{adb} for alternative versions. Otherwise a naming
29188 conflict may arise through another commonly used feature: to declare as part
29189 of the project a set of directories containing all the sources obeying the
29190 default naming scheme.
29191
29192 The use of alternative units is certainly feasible in all situations,
29193 and for example the Ada part of the GNAT run-time is conditionalized
29194 based on the target architecture using this approach. As a specific example,
29195 consider the implementation of the AST feature in VMS. There is one
29196 spec:
29197
29198 @smallexample
29199 s-asthan.ads
29200 @end smallexample
29201
29202 @noindent
29203 which is the same for all architectures, and three bodies:
29204
29205 @table @file
29206 @item s-asthan.adb
29207 used for all non-VMS operating systems
29208 @item s-asthan-vms-alpha.adb
29209 used for VMS on the Alpha
29210 @item s-asthan-vms-ia64.adb
29211 used for VMS on the ia64
29212 @end table
29213
29214 @noindent
29215 The dummy version @file{s-asthan.adb} simply raises exceptions noting that
29216 this operating system feature is not available, and the two remaining
29217 versions interface with the corresponding versions of VMS to provide
29218 VMS-compatible AST handling. The GNAT build script knows the architecture
29219 and operating system, and automatically selects the right version,
29220 renaming it if necessary to @file{s-asthan.adb} before the run-time build.
29221
29222 Another style for arranging alternative implementations is through Ada's
29223 access-to-subprogram facility.
29224 In case some functionality is to be conditionally included,
29225 you can declare an access-to-procedure variable @code{Ref} that is initialized
29226 to designate a ``do nothing'' procedure, and then invoke @code{Ref.all}
29227 when appropriate.
29228 In some library package, set @code{Ref} to @code{Proc'Access} for some
29229 procedure @code{Proc} that performs the relevant processing.
29230 The initialization only occurs if the library package is included in the
29231 program.
29232 The same idea can also be implemented using tagged types and dispatching
29233 calls.
29234
29235
29236 @node Preprocessing
29237 @section Preprocessing
29238 @cindex Preprocessing
29239
29240 @noindent
29241 Although it is quite possible to conditionalize code without the use of
29242 C-style preprocessing, as described earlier in this section, it is
29243 nevertheless convenient in some cases to use the C approach. Moreover,
29244 older Ada compilers have often provided some preprocessing capability,
29245 so legacy code may depend on this approach, even though it is not
29246 standard.
29247
29248 To accommodate such use, GNAT provides a preprocessor (modeled to a large
29249 extent on the various preprocessors that have been used
29250 with legacy code on other compilers, to enable easier transition).
29251
29252 The preprocessor may be used in two separate modes. It can be used quite
29253 separately from the compiler, to generate a separate output source file
29254 that is then fed to the compiler as a separate step. This is the
29255 @code{gnatprep} utility, whose use is fully described in
29256 @ref{Preprocessing Using gnatprep}.
29257 @cindex @code{gnatprep}
29258
29259 The preprocessing language allows such constructs as
29260
29261 @smallexample
29262 @group
29263 #if DEBUG or PRIORITY > 4 then
29264 bunch of declarations
29265 #else
29266 completely different bunch of declarations
29267 #end if;
29268 @end group
29269 @end smallexample
29270
29271 @noindent
29272 The values of the symbols @code{DEBUG} and @code{PRIORITY} can be
29273 defined either on the command line or in a separate file.
29274
29275 The other way of running the preprocessor is even closer to the C style and
29276 often more convenient. In this approach the preprocessing is integrated into
29277 the compilation process. The compiler is fed the preprocessor input which
29278 includes @code{#if} lines etc, and then the compiler carries out the
29279 preprocessing internally and processes the resulting output.
29280 For more details on this approach, see @ref{Integrated Preprocessing}.
29281
29282
29283 @c *******************************
29284 @node Inline Assembler
29285 @appendix Inline Assembler
29286 @c *******************************
29287
29288 @noindent
29289 If you need to write low-level software that interacts directly
29290 with the hardware, Ada provides two ways to incorporate assembly
29291 language code into your program. First, you can import and invoke
29292 external routines written in assembly language, an Ada feature fully
29293 supported by GNAT@. However, for small sections of code it may be simpler
29294 or more efficient to include assembly language statements directly
29295 in your Ada source program, using the facilities of the implementation-defined
29296 package @code{System.Machine_Code}, which incorporates the gcc
29297 Inline Assembler. The Inline Assembler approach offers a number of advantages,
29298 including the following:
29299
29300 @itemize @bullet
29301 @item No need to use non-Ada tools
29302 @item Consistent interface over different targets
29303 @item Automatic usage of the proper calling conventions
29304 @item Access to Ada constants and variables
29305 @item Definition of intrinsic routines
29306 @item Possibility of inlining a subprogram comprising assembler code
29307 @item Code optimizer can take Inline Assembler code into account
29308 @end itemize
29309
29310 This chapter presents a series of examples to show you how to use
29311 the Inline Assembler. Although it focuses on the Intel x86,
29312 the general approach applies also to other processors.
29313 It is assumed that you are familiar with Ada
29314 and with assembly language programming.
29315
29316 @menu
29317 * Basic Assembler Syntax::
29318 * A Simple Example of Inline Assembler::
29319 * Output Variables in Inline Assembler::
29320 * Input Variables in Inline Assembler::
29321 * Inlining Inline Assembler Code::
29322 * Other Asm Functionality::
29323 @end menu
29324
29325 @c ---------------------------------------------------------------------------
29326 @node Basic Assembler Syntax
29327 @section Basic Assembler Syntax
29328
29329 @noindent
29330 The assembler used by GNAT and gcc is based not on the Intel assembly
29331 language, but rather on a language that descends from the AT&T Unix
29332 assembler @emph{as} (and which is often referred to as ``AT&T syntax'').
29333 The following table summarizes the main features of @emph{as} syntax
29334 and points out the differences from the Intel conventions.
29335 See the gcc @emph{as} and @emph{gas} (an @emph{as} macro
29336 pre-processor) documentation for further information.
29337
29338 @table @asis
29339 @item Register names
29340 gcc / @emph{as}: Prefix with ``%''; for example @code{%eax}
29341 @*
29342 Intel: No extra punctuation; for example @code{eax}
29343
29344 @item Immediate operand
29345 gcc / @emph{as}: Prefix with ``$''; for example @code{$4}
29346 @*
29347 Intel: No extra punctuation; for example @code{4}
29348
29349 @item Address
29350 gcc / @emph{as}: Prefix with ``$''; for example @code{$loc}
29351 @*
29352 Intel: No extra punctuation; for example @code{loc}
29353
29354 @item Memory contents
29355 gcc / @emph{as}: No extra punctuation; for example @code{loc}
29356 @*
29357 Intel: Square brackets; for example @code{[loc]}
29358
29359 @item Register contents
29360 gcc / @emph{as}: Parentheses; for example @code{(%eax)}
29361 @*
29362 Intel: Square brackets; for example @code{[eax]}
29363
29364 @item Hexadecimal numbers
29365 gcc / @emph{as}: Leading ``0x'' (C language syntax); for example @code{0xA0}
29366 @*
29367 Intel: Trailing ``h''; for example @code{A0h}
29368
29369 @item Operand size
29370 gcc / @emph{as}: Explicit in op code; for example @code{movw} to move
29371 a 16-bit word
29372 @*
29373 Intel: Implicit, deduced by assembler; for example @code{mov}
29374
29375 @item Instruction repetition
29376 gcc / @emph{as}: Split into two lines; for example
29377 @*
29378 @code{rep}
29379 @*
29380 @code{stosl}
29381 @*
29382 Intel: Keep on one line; for example @code{rep stosl}
29383
29384 @item Order of operands
29385 gcc / @emph{as}: Source first; for example @code{movw $4, %eax}
29386 @*
29387 Intel: Destination first; for example @code{mov eax, 4}
29388 @end table
29389
29390 @c ---------------------------------------------------------------------------
29391 @node A Simple Example of Inline Assembler
29392 @section A Simple Example of Inline Assembler
29393
29394 @noindent
29395 The following example will generate a single assembly language statement,
29396 @code{nop}, which does nothing. Despite its lack of run-time effect,
29397 the example will be useful in illustrating the basics of
29398 the Inline Assembler facility.
29399
29400 @smallexample @c ada
29401 @group
29402 with System.Machine_Code; use System.Machine_Code;
29403 procedure Nothing is
29404 begin
29405 Asm ("nop");
29406 end Nothing;
29407 @end group
29408 @end smallexample
29409
29410 @code{Asm} is a procedure declared in package @code{System.Machine_Code};
29411 here it takes one parameter, a @emph{template string} that must be a static
29412 expression and that will form the generated instruction.
29413 @code{Asm} may be regarded as a compile-time procedure that parses
29414 the template string and additional parameters (none here),
29415 from which it generates a sequence of assembly language instructions.
29416
29417 The examples in this chapter will illustrate several of the forms
29418 for invoking @code{Asm}; a complete specification of the syntax
29419 is found in @ref{Machine Code Insertions,,, gnat_rm, GNAT Reference
29420 Manual}.
29421
29422 Under the standard GNAT conventions, the @code{Nothing} procedure
29423 should be in a file named @file{nothing.adb}.
29424 You can build the executable in the usual way:
29425 @smallexample
29426 gnatmake nothing
29427 @end smallexample
29428 However, the interesting aspect of this example is not its run-time behavior
29429 but rather the generated assembly code.
29430 To see this output, invoke the compiler as follows:
29431 @smallexample
29432 gcc -c -S -fomit-frame-pointer -gnatp @file{nothing.adb}
29433 @end smallexample
29434 where the options are:
29435
29436 @table @code
29437 @item -c
29438 compile only (no bind or link)
29439 @item -S
29440 generate assembler listing
29441 @item -fomit-frame-pointer
29442 do not set up separate stack frames
29443 @item -gnatp
29444 do not add runtime checks
29445 @end table
29446
29447 This gives a human-readable assembler version of the code. The resulting
29448 file will have the same name as the Ada source file, but with a @code{.s}
29449 extension. In our example, the file @file{nothing.s} has the following
29450 contents:
29451
29452 @smallexample
29453 @group
29454 .file "nothing.adb"
29455 gcc2_compiled.:
29456 ___gnu_compiled_ada:
29457 .text
29458 .align 4
29459 .globl __ada_nothing
29460 __ada_nothing:
29461 #APP
29462 nop
29463 #NO_APP
29464 jmp L1
29465 .align 2,0x90
29466 L1:
29467 ret
29468 @end group
29469 @end smallexample
29470
29471 The assembly code you included is clearly indicated by
29472 the compiler, between the @code{#APP} and @code{#NO_APP}
29473 delimiters. The character before the 'APP' and 'NOAPP'
29474 can differ on different targets. For example, GNU/Linux uses '#APP' while
29475 on NT you will see '/APP'.
29476
29477 If you make a mistake in your assembler code (such as using the
29478 wrong size modifier, or using a wrong operand for the instruction) GNAT
29479 will report this error in a temporary file, which will be deleted when
29480 the compilation is finished. Generating an assembler file will help
29481 in such cases, since you can assemble this file separately using the
29482 @emph{as} assembler that comes with gcc.
29483
29484 Assembling the file using the command
29485
29486 @smallexample
29487 as @file{nothing.s}
29488 @end smallexample
29489 @noindent
29490 will give you error messages whose lines correspond to the assembler
29491 input file, so you can easily find and correct any mistakes you made.
29492 If there are no errors, @emph{as} will generate an object file
29493 @file{nothing.out}.
29494
29495 @c ---------------------------------------------------------------------------
29496 @node Output Variables in Inline Assembler
29497 @section Output Variables in Inline Assembler
29498
29499 @noindent
29500 The examples in this section, showing how to access the processor flags,
29501 illustrate how to specify the destination operands for assembly language
29502 statements.
29503
29504 @smallexample @c ada
29505 @group
29506 with Interfaces; use Interfaces;
29507 with Ada.Text_IO; use Ada.Text_IO;
29508 with System.Machine_Code; use System.Machine_Code;
29509 procedure Get_Flags is
29510 Flags : Unsigned_32;
29511 use ASCII;
29512 begin
29513 Asm ("pushfl" & LF & HT & -- push flags on stack
29514 "popl %%eax" & LF & HT & -- load eax with flags
29515 "movl %%eax, %0", -- store flags in variable
29516 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29517 Put_Line ("Flags register:" & Flags'Img);
29518 end Get_Flags;
29519 @end group
29520 @end smallexample
29521
29522 In order to have a nicely aligned assembly listing, we have separated
29523 multiple assembler statements in the Asm template string with linefeed
29524 (ASCII.LF) and horizontal tab (ASCII.HT) characters.
29525 The resulting section of the assembly output file is:
29526
29527 @smallexample
29528 @group
29529 #APP
29530 pushfl
29531 popl %eax
29532 movl %eax, -40(%ebp)
29533 #NO_APP
29534 @end group
29535 @end smallexample
29536
29537 It would have been legal to write the Asm invocation as:
29538
29539 @smallexample
29540 Asm ("pushfl popl %%eax movl %%eax, %0")
29541 @end smallexample
29542
29543 but in the generated assembler file, this would come out as:
29544
29545 @smallexample
29546 #APP
29547 pushfl popl %eax movl %eax, -40(%ebp)
29548 #NO_APP
29549 @end smallexample
29550
29551 which is not so convenient for the human reader.
29552
29553 We use Ada comments
29554 at the end of each line to explain what the assembler instructions
29555 actually do. This is a useful convention.
29556
29557 When writing Inline Assembler instructions, you need to precede each register
29558 and variable name with a percent sign. Since the assembler already requires
29559 a percent sign at the beginning of a register name, you need two consecutive
29560 percent signs for such names in the Asm template string, thus @code{%%eax}.
29561 In the generated assembly code, one of the percent signs will be stripped off.
29562
29563 Names such as @code{%0}, @code{%1}, @code{%2}, etc., denote input or output
29564 variables: operands you later define using @code{Input} or @code{Output}
29565 parameters to @code{Asm}.
29566 An output variable is illustrated in
29567 the third statement in the Asm template string:
29568 @smallexample
29569 movl %%eax, %0
29570 @end smallexample
29571 The intent is to store the contents of the eax register in a variable that can
29572 be accessed in Ada. Simply writing @code{movl %%eax, Flags} would not
29573 necessarily work, since the compiler might optimize by using a register
29574 to hold Flags, and the expansion of the @code{movl} instruction would not be
29575 aware of this optimization. The solution is not to store the result directly
29576 but rather to advise the compiler to choose the correct operand form;
29577 that is the purpose of the @code{%0} output variable.
29578
29579 Information about the output variable is supplied in the @code{Outputs}
29580 parameter to @code{Asm}:
29581 @smallexample
29582 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29583 @end smallexample
29584
29585 The output is defined by the @code{Asm_Output} attribute of the target type;
29586 the general format is
29587 @smallexample
29588 Type'Asm_Output (constraint_string, variable_name)
29589 @end smallexample
29590
29591 The constraint string directs the compiler how
29592 to store/access the associated variable. In the example
29593 @smallexample
29594 Unsigned_32'Asm_Output ("=m", Flags);
29595 @end smallexample
29596 the @code{"m"} (memory) constraint tells the compiler that the variable
29597 @code{Flags} should be stored in a memory variable, thus preventing
29598 the optimizer from keeping it in a register. In contrast,
29599 @smallexample
29600 Unsigned_32'Asm_Output ("=r", Flags);
29601 @end smallexample
29602 uses the @code{"r"} (register) constraint, telling the compiler to
29603 store the variable in a register.
29604
29605 If the constraint is preceded by the equal character (@strong{=}), it tells
29606 the compiler that the variable will be used to store data into it.
29607
29608 In the @code{Get_Flags} example, we used the @code{"g"} (global) constraint,
29609 allowing the optimizer to choose whatever it deems best.
29610
29611 There are a fairly large number of constraints, but the ones that are
29612 most useful (for the Intel x86 processor) are the following:
29613
29614 @table @code
29615 @item =
29616 output constraint
29617 @item g
29618 global (i.e.@: can be stored anywhere)
29619 @item m
29620 in memory
29621 @item I
29622 a constant
29623 @item a
29624 use eax
29625 @item b
29626 use ebx
29627 @item c
29628 use ecx
29629 @item d
29630 use edx
29631 @item S
29632 use esi
29633 @item D
29634 use edi
29635 @item r
29636 use one of eax, ebx, ecx or edx
29637 @item q
29638 use one of eax, ebx, ecx, edx, esi or edi
29639 @end table
29640
29641 The full set of constraints is described in the gcc and @emph{as}
29642 documentation; note that it is possible to combine certain constraints
29643 in one constraint string.
29644
29645 You specify the association of an output variable with an assembler operand
29646 through the @code{%}@emph{n} notation, where @emph{n} is a non-negative
29647 integer. Thus in
29648 @smallexample @c ada
29649 @group
29650 Asm ("pushfl" & LF & HT & -- push flags on stack
29651 "popl %%eax" & LF & HT & -- load eax with flags
29652 "movl %%eax, %0", -- store flags in variable
29653 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29654 @end group
29655 @end smallexample
29656 @noindent
29657 @code{%0} will be replaced in the expanded code by the appropriate operand,
29658 whatever
29659 the compiler decided for the @code{Flags} variable.
29660
29661 In general, you may have any number of output variables:
29662 @itemize @bullet
29663 @item
29664 Count the operands starting at 0; thus @code{%0}, @code{%1}, etc.
29665 @item
29666 Specify the @code{Outputs} parameter as a parenthesized comma-separated list
29667 of @code{Asm_Output} attributes
29668 @end itemize
29669
29670 For example:
29671 @smallexample @c ada
29672 @group
29673 Asm ("movl %%eax, %0" & LF & HT &
29674 "movl %%ebx, %1" & LF & HT &
29675 "movl %%ecx, %2",
29676 Outputs => (Unsigned_32'Asm_Output ("=g", Var_A), -- %0 = Var_A
29677 Unsigned_32'Asm_Output ("=g", Var_B), -- %1 = Var_B
29678 Unsigned_32'Asm_Output ("=g", Var_C))); -- %2 = Var_C
29679 @end group
29680 @end smallexample
29681 @noindent
29682 where @code{Var_A}, @code{Var_B}, and @code{Var_C} are variables
29683 in the Ada program.
29684
29685 As a variation on the @code{Get_Flags} example, we can use the constraints
29686 string to direct the compiler to store the eax register into the @code{Flags}
29687 variable, instead of including the store instruction explicitly in the
29688 @code{Asm} template string:
29689
29690 @smallexample @c ada
29691 @group
29692 with Interfaces; use Interfaces;
29693 with Ada.Text_IO; use Ada.Text_IO;
29694 with System.Machine_Code; use System.Machine_Code;
29695 procedure Get_Flags_2 is
29696 Flags : Unsigned_32;
29697 use ASCII;
29698 begin
29699 Asm ("pushfl" & LF & HT & -- push flags on stack
29700 "popl %%eax", -- save flags in eax
29701 Outputs => Unsigned_32'Asm_Output ("=a", Flags));
29702 Put_Line ("Flags register:" & Flags'Img);
29703 end Get_Flags_2;
29704 @end group
29705 @end smallexample
29706
29707 @noindent
29708 The @code{"a"} constraint tells the compiler that the @code{Flags}
29709 variable will come from the eax register. Here is the resulting code:
29710
29711 @smallexample
29712 @group
29713 #APP
29714 pushfl
29715 popl %eax
29716 #NO_APP
29717 movl %eax,-40(%ebp)
29718 @end group
29719 @end smallexample
29720
29721 @noindent
29722 The compiler generated the store of eax into Flags after
29723 expanding the assembler code.
29724
29725 Actually, there was no need to pop the flags into the eax register;
29726 more simply, we could just pop the flags directly into the program variable:
29727
29728 @smallexample @c ada
29729 @group
29730 with Interfaces; use Interfaces;
29731 with Ada.Text_IO; use Ada.Text_IO;
29732 with System.Machine_Code; use System.Machine_Code;
29733 procedure Get_Flags_3 is
29734 Flags : Unsigned_32;
29735 use ASCII;
29736 begin
29737 Asm ("pushfl" & LF & HT & -- push flags on stack
29738 "pop %0", -- save flags in Flags
29739 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29740 Put_Line ("Flags register:" & Flags'Img);
29741 end Get_Flags_3;
29742 @end group
29743 @end smallexample
29744
29745 @c ---------------------------------------------------------------------------
29746 @node Input Variables in Inline Assembler
29747 @section Input Variables in Inline Assembler
29748
29749 @noindent
29750 The example in this section illustrates how to specify the source operands
29751 for assembly language statements.
29752 The program simply increments its input value by 1:
29753
29754 @smallexample @c ada
29755 @group
29756 with Interfaces; use Interfaces;
29757 with Ada.Text_IO; use Ada.Text_IO;
29758 with System.Machine_Code; use System.Machine_Code;
29759 procedure Increment is
29760
29761 function Incr (Value : Unsigned_32) return Unsigned_32 is
29762 Result : Unsigned_32;
29763 begin
29764 Asm ("incl %0",
29765 Inputs => Unsigned_32'Asm_Input ("a", Value),
29766 Outputs => Unsigned_32'Asm_Output ("=a", Result));
29767 return Result;
29768 end Incr;
29769
29770 Value : Unsigned_32;
29771
29772 begin
29773 Value := 5;
29774 Put_Line ("Value before is" & Value'Img);
29775 Value := Incr (Value);
29776 Put_Line ("Value after is" & Value'Img);
29777 end Increment;
29778 @end group
29779 @end smallexample
29780
29781 The @code{Outputs} parameter to @code{Asm} specifies
29782 that the result will be in the eax register and that it is to be stored
29783 in the @code{Result} variable.
29784
29785 The @code{Inputs} parameter looks much like the @code{Outputs} parameter,
29786 but with an @code{Asm_Input} attribute.
29787 The @code{"="} constraint, indicating an output value, is not present.
29788
29789 You can have multiple input variables, in the same way that you can have more
29790 than one output variable.
29791
29792 The parameter count (%0, %1) etc, now starts at the first input
29793 statement, and continues with the output statements.
29794 When both parameters use the same variable, the
29795 compiler will treat them as the same %n operand, which is the case here.
29796
29797 Just as the @code{Outputs} parameter causes the register to be stored into the
29798 target variable after execution of the assembler statements, so does the
29799 @code{Inputs} parameter cause its variable to be loaded into the register
29800 before execution of the assembler statements.
29801
29802 Thus the effect of the @code{Asm} invocation is:
29803 @enumerate
29804 @item load the 32-bit value of @code{Value} into eax
29805 @item execute the @code{incl %eax} instruction
29806 @item store the contents of eax into the @code{Result} variable
29807 @end enumerate
29808
29809 The resulting assembler file (with @option{-O2} optimization) contains:
29810 @smallexample
29811 @group
29812 _increment__incr.1:
29813 subl $4,%esp
29814 movl 8(%esp),%eax
29815 #APP
29816 incl %eax
29817 #NO_APP
29818 movl %eax,%edx
29819 movl %ecx,(%esp)
29820 addl $4,%esp
29821 ret
29822 @end group
29823 @end smallexample
29824
29825 @c ---------------------------------------------------------------------------
29826 @node Inlining Inline Assembler Code
29827 @section Inlining Inline Assembler Code
29828
29829 @noindent
29830 For a short subprogram such as the @code{Incr} function in the previous
29831 section, the overhead of the call and return (creating / deleting the stack
29832 frame) can be significant, compared to the amount of code in the subprogram
29833 body. A solution is to apply Ada's @code{Inline} pragma to the subprogram,
29834 which directs the compiler to expand invocations of the subprogram at the
29835 point(s) of call, instead of setting up a stack frame for out-of-line calls.
29836 Here is the resulting program:
29837
29838 @smallexample @c ada
29839 @group
29840 with Interfaces; use Interfaces;
29841 with Ada.Text_IO; use Ada.Text_IO;
29842 with System.Machine_Code; use System.Machine_Code;
29843 procedure Increment_2 is
29844
29845 function Incr (Value : Unsigned_32) return Unsigned_32 is
29846 Result : Unsigned_32;
29847 begin
29848 Asm ("incl %0",
29849 Inputs => Unsigned_32'Asm_Input ("a", Value),
29850 Outputs => Unsigned_32'Asm_Output ("=a", Result));
29851 return Result;
29852 end Incr;
29853 pragma Inline (Increment);
29854
29855 Value : Unsigned_32;
29856
29857 begin
29858 Value := 5;
29859 Put_Line ("Value before is" & Value'Img);
29860 Value := Increment (Value);
29861 Put_Line ("Value after is" & Value'Img);
29862 end Increment_2;
29863 @end group
29864 @end smallexample
29865
29866 Compile the program with both optimization (@option{-O2}) and inlining
29867 (@option{-gnatn}) enabled.
29868
29869 The @code{Incr} function is still compiled as usual, but at the
29870 point in @code{Increment} where our function used to be called:
29871
29872 @smallexample
29873 @group
29874 pushl %edi
29875 call _increment__incr.1
29876 @end group
29877 @end smallexample
29878
29879 @noindent
29880 the code for the function body directly appears:
29881
29882 @smallexample
29883 @group
29884 movl %esi,%eax
29885 #APP
29886 incl %eax
29887 #NO_APP
29888 movl %eax,%edx
29889 @end group
29890 @end smallexample
29891
29892 @noindent
29893 thus saving the overhead of stack frame setup and an out-of-line call.
29894
29895 @c ---------------------------------------------------------------------------
29896 @node Other Asm Functionality
29897 @section Other @code{Asm} Functionality
29898
29899 @noindent
29900 This section describes two important parameters to the @code{Asm}
29901 procedure: @code{Clobber}, which identifies register usage;
29902 and @code{Volatile}, which inhibits unwanted optimizations.
29903
29904 @menu
29905 * The Clobber Parameter::
29906 * The Volatile Parameter::
29907 @end menu
29908
29909 @c ---------------------------------------------------------------------------
29910 @node The Clobber Parameter
29911 @subsection The @code{Clobber} Parameter
29912
29913 @noindent
29914 One of the dangers of intermixing assembly language and a compiled language
29915 such as Ada is that the compiler needs to be aware of which registers are
29916 being used by the assembly code. In some cases, such as the earlier examples,
29917 the constraint string is sufficient to indicate register usage (e.g.,
29918 @code{"a"} for
29919 the eax register). But more generally, the compiler needs an explicit
29920 identification of the registers that are used by the Inline Assembly
29921 statements.
29922
29923 Using a register that the compiler doesn't know about
29924 could be a side effect of an instruction (like @code{mull}
29925 storing its result in both eax and edx).
29926 It can also arise from explicit register usage in your
29927 assembly code; for example:
29928 @smallexample
29929 @group
29930 Asm ("movl %0, %%ebx" & LF & HT &
29931 "movl %%ebx, %1",
29932 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
29933 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out));
29934 @end group
29935 @end smallexample
29936 @noindent
29937 where the compiler (since it does not analyze the @code{Asm} template string)
29938 does not know you are using the ebx register.
29939
29940 In such cases you need to supply the @code{Clobber} parameter to @code{Asm},
29941 to identify the registers that will be used by your assembly code:
29942
29943 @smallexample
29944 @group
29945 Asm ("movl %0, %%ebx" & LF & HT &
29946 "movl %%ebx, %1",
29947 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
29948 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
29949 Clobber => "ebx");
29950 @end group
29951 @end smallexample
29952
29953 The Clobber parameter is a static string expression specifying the
29954 register(s) you are using. Note that register names are @emph{not} prefixed
29955 by a percent sign. Also, if more than one register is used then their names
29956 are separated by commas; e.g., @code{"eax, ebx"}
29957
29958 The @code{Clobber} parameter has several additional uses:
29959 @enumerate
29960 @item Use ``register'' name @code{cc} to indicate that flags might have changed
29961 @item Use ``register'' name @code{memory} if you changed a memory location
29962 @end enumerate
29963
29964 @c ---------------------------------------------------------------------------
29965 @node The Volatile Parameter
29966 @subsection The @code{Volatile} Parameter
29967 @cindex Volatile parameter
29968
29969 @noindent
29970 Compiler optimizations in the presence of Inline Assembler may sometimes have
29971 unwanted effects. For example, when an @code{Asm} invocation with an input
29972 variable is inside a loop, the compiler might move the loading of the input
29973 variable outside the loop, regarding it as a one-time initialization.
29974
29975 If this effect is not desired, you can disable such optimizations by setting
29976 the @code{Volatile} parameter to @code{True}; for example:
29977
29978 @smallexample @c ada
29979 @group
29980 Asm ("movl %0, %%ebx" & LF & HT &
29981 "movl %%ebx, %1",
29982 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
29983 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
29984 Clobber => "ebx",
29985 Volatile => True);
29986 @end group
29987 @end smallexample
29988
29989 By default, @code{Volatile} is set to @code{False} unless there is no
29990 @code{Outputs} parameter.
29991
29992 Although setting @code{Volatile} to @code{True} prevents unwanted
29993 optimizations, it will also disable other optimizations that might be
29994 important for efficiency. In general, you should set @code{Volatile}
29995 to @code{True} only if the compiler's optimizations have created
29996 problems.
29997 @c END OF INLINE ASSEMBLER CHAPTER
29998 @c ===============================
29999
30000 @c ***********************************
30001 @c * Compatibility and Porting Guide *
30002 @c ***********************************
30003 @node Compatibility and Porting Guide
30004 @appendix Compatibility and Porting Guide
30005
30006 @noindent
30007 This chapter describes the compatibility issues that may arise between
30008 GNAT and other Ada compilation systems (including those for Ada 83),
30009 and shows how GNAT can expedite porting
30010 applications developed in other Ada environments.
30011
30012 @menu
30013 * Compatibility with Ada 83::
30014 * Compatibility between Ada 95 and Ada 2005::
30015 * Implementation-dependent characteristics::
30016 * Compatibility with Other Ada Systems::
30017 * Representation Clauses::
30018 @ifclear vms
30019 @c Brief section is only in non-VMS version
30020 @c Full chapter is in VMS version
30021 * Compatibility with HP Ada 83::
30022 @end ifclear
30023 @ifset vms
30024 * Transitioning to 64-Bit GNAT for OpenVMS::
30025 @end ifset
30026 @end menu
30027
30028 @node Compatibility with Ada 83
30029 @section Compatibility with Ada 83
30030 @cindex Compatibility (between Ada 83 and Ada 95 / Ada 2005)
30031
30032 @noindent
30033 Ada 95 and Ada 2005 are highly upwards compatible with Ada 83. In
30034 particular, the design intention was that the difficulties associated
30035 with moving from Ada 83 to Ada 95 or Ada 2005 should be no greater than those
30036 that occur when moving from one Ada 83 system to another.
30037
30038 However, there are a number of points at which there are minor
30039 incompatibilities. The @cite{Ada 95 Annotated Reference Manual} contains
30040 full details of these issues,
30041 and should be consulted for a complete treatment.
30042 In practice the
30043 following subsections treat the most likely issues to be encountered.
30044
30045 @menu
30046 * Legal Ada 83 programs that are illegal in Ada 95::
30047 * More deterministic semantics::
30048 * Changed semantics::
30049 * Other language compatibility issues::
30050 @end menu
30051
30052 @node Legal Ada 83 programs that are illegal in Ada 95
30053 @subsection Legal Ada 83 programs that are illegal in Ada 95
30054
30055 Some legal Ada 83 programs are illegal (i.e., they will fail to compile) in
30056 Ada 95 and thus also in Ada 2005:
30057
30058 @table @emph
30059 @item Character literals
30060 Some uses of character literals are ambiguous. Since Ada 95 has introduced
30061 @code{Wide_Character} as a new predefined character type, some uses of
30062 character literals that were legal in Ada 83 are illegal in Ada 95.
30063 For example:
30064 @smallexample @c ada
30065 for Char in 'A' .. 'Z' loop @dots{} end loop;
30066 @end smallexample
30067
30068 @noindent
30069 The problem is that @code{'A'} and @code{'Z'} could be from either
30070 @code{Character} or @code{Wide_Character}. The simplest correction
30071 is to make the type explicit; e.g.:
30072 @smallexample @c ada
30073 for Char in Character range 'A' .. 'Z' loop @dots{} end loop;
30074 @end smallexample
30075
30076 @item New reserved words
30077 The identifiers @code{abstract}, @code{aliased}, @code{protected},
30078 @code{requeue}, @code{tagged}, and @code{until} are reserved in Ada 95.
30079 Existing Ada 83 code using any of these identifiers must be edited to
30080 use some alternative name.
30081
30082 @item Freezing rules
30083 The rules in Ada 95 are slightly different with regard to the point at
30084 which entities are frozen, and representation pragmas and clauses are
30085 not permitted past the freeze point. This shows up most typically in
30086 the form of an error message complaining that a representation item
30087 appears too late, and the appropriate corrective action is to move
30088 the item nearer to the declaration of the entity to which it refers.
30089
30090 A particular case is that representation pragmas
30091 @ifset vms
30092 (including the
30093 extended HP Ada 83 compatibility pragmas such as @code{Export_Procedure})
30094 @end ifset
30095 cannot be applied to a subprogram body. If necessary, a separate subprogram
30096 declaration must be introduced to which the pragma can be applied.
30097
30098 @item Optional bodies for library packages
30099 In Ada 83, a package that did not require a package body was nevertheless
30100 allowed to have one. This lead to certain surprises in compiling large
30101 systems (situations in which the body could be unexpectedly ignored by the
30102 binder). In Ada 95, if a package does not require a body then it is not
30103 permitted to have a body. To fix this problem, simply remove a redundant
30104 body if it is empty, or, if it is non-empty, introduce a dummy declaration
30105 into the spec that makes the body required. One approach is to add a private
30106 part to the package declaration (if necessary), and define a parameterless
30107 procedure called @code{Requires_Body}, which must then be given a dummy
30108 procedure body in the package body, which then becomes required.
30109 Another approach (assuming that this does not introduce elaboration
30110 circularities) is to add an @code{Elaborate_Body} pragma to the package spec,
30111 since one effect of this pragma is to require the presence of a package body.
30112
30113 @item @code{Numeric_Error} is now the same as @code{Constraint_Error}
30114 In Ada 95, the exception @code{Numeric_Error} is a renaming of
30115 @code{Constraint_Error}.
30116 This means that it is illegal to have separate exception handlers for
30117 the two exceptions. The fix is simply to remove the handler for the
30118 @code{Numeric_Error} case (since even in Ada 83, a compiler was free to raise
30119 @code{Constraint_Error} in place of @code{Numeric_Error} in all cases).
30120
30121 @item Indefinite subtypes in generics
30122 In Ada 83, it was permissible to pass an indefinite type (e.g.@: @code{String})
30123 as the actual for a generic formal private type, but then the instantiation
30124 would be illegal if there were any instances of declarations of variables
30125 of this type in the generic body. In Ada 95, to avoid this clear violation
30126 of the methodological principle known as the ``contract model'',
30127 the generic declaration explicitly indicates whether
30128 or not such instantiations are permitted. If a generic formal parameter
30129 has explicit unknown discriminants, indicated by using @code{(<>)} after the
30130 type name, then it can be instantiated with indefinite types, but no
30131 stand-alone variables can be declared of this type. Any attempt to declare
30132 such a variable will result in an illegality at the time the generic is
30133 declared. If the @code{(<>)} notation is not used, then it is illegal
30134 to instantiate the generic with an indefinite type.
30135 This is the potential incompatibility issue when porting Ada 83 code to Ada 95.
30136 It will show up as a compile time error, and
30137 the fix is usually simply to add the @code{(<>)} to the generic declaration.
30138 @end table
30139
30140 @node More deterministic semantics
30141 @subsection More deterministic semantics
30142
30143 @table @emph
30144 @item Conversions
30145 Conversions from real types to integer types round away from 0. In Ada 83
30146 the conversion Integer(2.5) could deliver either 2 or 3 as its value. This
30147 implementation freedom was intended to support unbiased rounding in
30148 statistical applications, but in practice it interfered with portability.
30149 In Ada 95 the conversion semantics are unambiguous, and rounding away from 0
30150 is required. Numeric code may be affected by this change in semantics.
30151 Note, though, that this issue is no worse than already existed in Ada 83
30152 when porting code from one vendor to another.
30153
30154 @item Tasking
30155 The Real-Time Annex introduces a set of policies that define the behavior of
30156 features that were implementation dependent in Ada 83, such as the order in
30157 which open select branches are executed.
30158 @end table
30159
30160 @node Changed semantics
30161 @subsection Changed semantics
30162
30163 @noindent
30164 The worst kind of incompatibility is one where a program that is legal in
30165 Ada 83 is also legal in Ada 95 but can have an effect in Ada 95 that was not
30166 possible in Ada 83. Fortunately this is extremely rare, but the one
30167 situation that you should be alert to is the change in the predefined type
30168 @code{Character} from 7-bit ASCII to 8-bit Latin-1.
30169
30170 @table @emph
30171 @item Range of type @code{Character}
30172 The range of @code{Standard.Character} is now the full 256 characters
30173 of Latin-1, whereas in most Ada 83 implementations it was restricted
30174 to 128 characters. Although some of the effects of
30175 this change will be manifest in compile-time rejection of legal
30176 Ada 83 programs it is possible for a working Ada 83 program to have
30177 a different effect in Ada 95, one that was not permitted in Ada 83.
30178 As an example, the expression
30179 @code{Character'Pos(Character'Last)} returned @code{127} in Ada 83 and now
30180 delivers @code{255} as its value.
30181 In general, you should look at the logic of any
30182 character-processing Ada 83 program and see whether it needs to be adapted
30183 to work correctly with Latin-1. Note that the predefined Ada 95 API has a
30184 character handling package that may be relevant if code needs to be adapted
30185 to account for the additional Latin-1 elements.
30186 The desirable fix is to
30187 modify the program to accommodate the full character set, but in some cases
30188 it may be convenient to define a subtype or derived type of Character that
30189 covers only the restricted range.
30190 @cindex Latin-1
30191 @end table
30192
30193 @node Other language compatibility issues
30194 @subsection Other language compatibility issues
30195
30196 @table @emph
30197 @item @option{-gnat83} switch
30198 All implementations of GNAT provide a switch that causes GNAT to operate
30199 in Ada 83 mode. In this mode, some but not all compatibility problems
30200 of the type described above are handled automatically. For example, the
30201 new reserved words introduced in Ada 95 and Ada 2005 are treated simply
30202 as identifiers as in Ada 83.
30203 However,
30204 in practice, it is usually advisable to make the necessary modifications
30205 to the program to remove the need for using this switch.
30206 See @ref{Compiling Different Versions of Ada}.
30207
30208 @item Support for removed Ada 83 pragmas and attributes
30209 A number of pragmas and attributes from Ada 83 were removed from Ada 95,
30210 generally because they were replaced by other mechanisms. Ada 95 and Ada 2005
30211 compilers are allowed, but not required, to implement these missing
30212 elements. In contrast with some other compilers, GNAT implements all
30213 such pragmas and attributes, eliminating this compatibility concern. These
30214 include @code{pragma Interface} and the floating point type attributes
30215 (@code{Emax}, @code{Mantissa}, etc.), among other items.
30216 @end table
30217
30218
30219 @node Compatibility between Ada 95 and Ada 2005
30220 @section Compatibility between Ada 95 and Ada 2005
30221 @cindex Compatibility between Ada 95 and Ada 2005
30222
30223 @noindent
30224 Although Ada 2005 was designed to be upwards compatible with Ada 95, there are
30225 a number of incompatibilities. Several are enumerated below;
30226 for a complete description please see the
30227 Annotated Ada 2005 Reference Manual, or section 9.1.1 in
30228 @cite{Rationale for Ada 2005}.
30229
30230 @table @emph
30231 @item New reserved words.
30232 The words @code{interface}, @code{overriding} and @code{synchronized} are
30233 reserved in Ada 2005.
30234 A pre-Ada 2005 program that uses any of these as an identifier will be
30235 illegal.
30236
30237 @item New declarations in predefined packages.
30238 A number of packages in the predefined environment contain new declarations:
30239 @code{Ada.Exceptions}, @code{Ada.Real_Time}, @code{Ada.Strings},
30240 @code{Ada.Strings.Fixed}, @code{Ada.Strings.Bounded},
30241 @code{Ada.Strings.Unbounded}, @code{Ada.Strings.Wide_Fixed},
30242 @code{Ada.Strings.Wide_Bounded}, @code{Ada.Strings.Wide_Unbounded},
30243 @code{Ada.Tags}, @code{Ada.Text_IO}, and @code{Interfaces.C}.
30244 If an Ada 95 program does a @code{with} and @code{use} of any of these
30245 packages, the new declarations may cause name clashes.
30246
30247 @item Access parameters.
30248 A nondispatching subprogram with an access parameter cannot be renamed
30249 as a dispatching operation. This was permitted in Ada 95.
30250
30251 @item Access types, discriminants, and constraints.
30252 Rule changes in this area have led to some incompatibilities; for example,
30253 constrained subtypes of some access types are not permitted in Ada 2005.
30254
30255 @item Aggregates for limited types.
30256 The allowance of aggregates for limited types in Ada 2005 raises the
30257 possibility of ambiguities in legal Ada 95 programs, since additional types
30258 now need to be considered in expression resolution.
30259
30260 @item Fixed-point multiplication and division.
30261 Certain expressions involving ``*'' or ``/'' for a fixed-point type, which
30262 were legal in Ada 95 and invoked the predefined versions of these operations,
30263 are now ambiguous.
30264 The ambiguity may be resolved either by applying a type conversion to the
30265 expression, or by explicitly invoking the operation from package
30266 @code{Standard}.
30267
30268 @item Return-by-reference types.
30269 The Ada 95 return-by-reference mechanism has been removed. Instead, the user
30270 can declare a function returning a value from an anonymous access type.
30271 @end table
30272
30273
30274 @node Implementation-dependent characteristics
30275 @section Implementation-dependent characteristics
30276 @noindent
30277 Although the Ada language defines the semantics of each construct as
30278 precisely as practical, in some situations (for example for reasons of
30279 efficiency, or where the effect is heavily dependent on the host or target
30280 platform) the implementation is allowed some freedom. In porting Ada 83
30281 code to GNAT, you need to be aware of whether / how the existing code
30282 exercised such implementation dependencies. Such characteristics fall into
30283 several categories, and GNAT offers specific support in assisting the
30284 transition from certain Ada 83 compilers.
30285
30286 @menu
30287 * Implementation-defined pragmas::
30288 * Implementation-defined attributes::
30289 * Libraries::
30290 * Elaboration order::
30291 * Target-specific aspects::
30292 @end menu
30293
30294 @node Implementation-defined pragmas
30295 @subsection Implementation-defined pragmas
30296
30297 @noindent
30298 Ada compilers are allowed to supplement the language-defined pragmas, and
30299 these are a potential source of non-portability. All GNAT-defined pragmas
30300 are described in @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT
30301 Reference Manual}, and these include several that are specifically
30302 intended to correspond to other vendors' Ada 83 pragmas.
30303 For migrating from VADS, the pragma @code{Use_VADS_Size} may be useful.
30304 For compatibility with HP Ada 83, GNAT supplies the pragmas
30305 @code{Extend_System}, @code{Ident}, @code{Inline_Generic},
30306 @code{Interface_Name}, @code{Passive}, @code{Suppress_All},
30307 and @code{Volatile}.
30308 Other relevant pragmas include @code{External} and @code{Link_With}.
30309 Some vendor-specific
30310 Ada 83 pragmas (@code{Share_Generic}, @code{Subtitle}, and @code{Title}) are
30311 recognized, thus
30312 avoiding compiler rejection of units that contain such pragmas; they are not
30313 relevant in a GNAT context and hence are not otherwise implemented.
30314
30315 @node Implementation-defined attributes
30316 @subsection Implementation-defined attributes
30317
30318 Analogous to pragmas, the set of attributes may be extended by an
30319 implementation. All GNAT-defined attributes are described in
30320 @ref{Implementation Defined Attributes,,, gnat_rm, GNAT Reference
30321 Manual}, and these include several that are specifically intended
30322 to correspond to other vendors' Ada 83 attributes. For migrating from VADS,
30323 the attribute @code{VADS_Size} may be useful. For compatibility with HP
30324 Ada 83, GNAT supplies the attributes @code{Bit}, @code{Machine_Size} and
30325 @code{Type_Class}.
30326
30327 @node Libraries
30328 @subsection Libraries
30329 @noindent
30330 Vendors may supply libraries to supplement the standard Ada API. If Ada 83
30331 code uses vendor-specific libraries then there are several ways to manage
30332 this in Ada 95 or Ada 2005:
30333 @enumerate
30334 @item
30335 If the source code for the libraries (specs and bodies) are
30336 available, then the libraries can be migrated in the same way as the
30337 application.
30338 @item
30339 If the source code for the specs but not the bodies are
30340 available, then you can reimplement the bodies.
30341 @item
30342 Some features introduced by Ada 95 obviate the need for library support. For
30343 example most Ada 83 vendors supplied a package for unsigned integers. The
30344 Ada 95 modular type feature is the preferred way to handle this need, so
30345 instead of migrating or reimplementing the unsigned integer package it may
30346 be preferable to retrofit the application using modular types.
30347 @end enumerate
30348
30349 @node Elaboration order
30350 @subsection Elaboration order
30351 @noindent
30352 The implementation can choose any elaboration order consistent with the unit
30353 dependency relationship. This freedom means that some orders can result in
30354 Program_Error being raised due to an ``Access Before Elaboration'': an attempt
30355 to invoke a subprogram its body has been elaborated, or to instantiate a
30356 generic before the generic body has been elaborated. By default GNAT
30357 attempts to choose a safe order (one that will not encounter access before
30358 elaboration problems) by implicitly inserting @code{Elaborate} or
30359 @code{Elaborate_All} pragmas where
30360 needed. However, this can lead to the creation of elaboration circularities
30361 and a resulting rejection of the program by gnatbind. This issue is
30362 thoroughly described in @ref{Elaboration Order Handling in GNAT}.
30363 In brief, there are several
30364 ways to deal with this situation:
30365
30366 @itemize @bullet
30367 @item
30368 Modify the program to eliminate the circularities, e.g.@: by moving
30369 elaboration-time code into explicitly-invoked procedures
30370 @item
30371 Constrain the elaboration order by including explicit @code{Elaborate_Body} or
30372 @code{Elaborate} pragmas, and then inhibit the generation of implicit
30373 @code{Elaborate_All}
30374 pragmas either globally (as an effect of the @option{-gnatE} switch) or locally
30375 (by selectively suppressing elaboration checks via pragma
30376 @code{Suppress(Elaboration_Check)} when it is safe to do so).
30377 @end itemize
30378
30379 @node Target-specific aspects
30380 @subsection Target-specific aspects
30381 @noindent
30382 Low-level applications need to deal with machine addresses, data
30383 representations, interfacing with assembler code, and similar issues. If
30384 such an Ada 83 application is being ported to different target hardware (for
30385 example where the byte endianness has changed) then you will need to
30386 carefully examine the program logic; the porting effort will heavily depend
30387 on the robustness of the original design. Moreover, Ada 95 (and thus
30388 Ada 2005) are sometimes
30389 incompatible with typical Ada 83 compiler practices regarding implicit
30390 packing, the meaning of the Size attribute, and the size of access values.
30391 GNAT's approach to these issues is described in @ref{Representation Clauses}.
30392
30393 @node Compatibility with Other Ada Systems
30394 @section Compatibility with Other Ada Systems
30395
30396 @noindent
30397 If programs avoid the use of implementation dependent and
30398 implementation defined features, as documented in the @cite{Ada
30399 Reference Manual}, there should be a high degree of portability between
30400 GNAT and other Ada systems. The following are specific items which
30401 have proved troublesome in moving Ada 95 programs from GNAT to other Ada 95
30402 compilers, but do not affect porting code to GNAT@.
30403 (As of @value{NOW}, GNAT is the only compiler available for Ada 2005;
30404 the following issues may or may not arise for Ada 2005 programs
30405 when other compilers appear.)
30406
30407 @table @emph
30408 @item Ada 83 Pragmas and Attributes
30409 Ada 95 compilers are allowed, but not required, to implement the missing
30410 Ada 83 pragmas and attributes that are no longer defined in Ada 95.
30411 GNAT implements all such pragmas and attributes, eliminating this as
30412 a compatibility concern, but some other Ada 95 compilers reject these
30413 pragmas and attributes.
30414
30415 @item Specialized Needs Annexes
30416 GNAT implements the full set of special needs annexes. At the
30417 current time, it is the only Ada 95 compiler to do so. This means that
30418 programs making use of these features may not be portable to other Ada
30419 95 compilation systems.
30420
30421 @item Representation Clauses
30422 Some other Ada 95 compilers implement only the minimal set of
30423 representation clauses required by the Ada 95 reference manual. GNAT goes
30424 far beyond this minimal set, as described in the next section.
30425 @end table
30426
30427 @node Representation Clauses
30428 @section Representation Clauses
30429
30430 @noindent
30431 The Ada 83 reference manual was quite vague in describing both the minimal
30432 required implementation of representation clauses, and also their precise
30433 effects. Ada 95 (and thus also Ada 2005) are much more explicit, but the
30434 minimal set of capabilities required is still quite limited.
30435
30436 GNAT implements the full required set of capabilities in
30437 Ada 95 and Ada 2005, but also goes much further, and in particular
30438 an effort has been made to be compatible with existing Ada 83 usage to the
30439 greatest extent possible.
30440
30441 A few cases exist in which Ada 83 compiler behavior is incompatible with
30442 the requirements in Ada 95 (and thus also Ada 2005). These are instances of
30443 intentional or accidental dependence on specific implementation dependent
30444 characteristics of these Ada 83 compilers. The following is a list of
30445 the cases most likely to arise in existing Ada 83 code.
30446
30447 @table @emph
30448 @item Implicit Packing
30449 Some Ada 83 compilers allowed a Size specification to cause implicit
30450 packing of an array or record. This could cause expensive implicit
30451 conversions for change of representation in the presence of derived
30452 types, and the Ada design intends to avoid this possibility.
30453 Subsequent AI's were issued to make it clear that such implicit
30454 change of representation in response to a Size clause is inadvisable,
30455 and this recommendation is represented explicitly in the Ada 95 (and Ada 2005)
30456 Reference Manuals as implementation advice that is followed by GNAT@.
30457 The problem will show up as an error
30458 message rejecting the size clause. The fix is simply to provide
30459 the explicit pragma @code{Pack}, or for more fine tuned control, provide
30460 a Component_Size clause.
30461
30462 @item Meaning of Size Attribute
30463 The Size attribute in Ada 95 (and Ada 2005) for discrete types is defined as
30464 the minimal number of bits required to hold values of the type. For example,
30465 on a 32-bit machine, the size of @code{Natural} will typically be 31 and not
30466 32 (since no sign bit is required). Some Ada 83 compilers gave 31, and
30467 some 32 in this situation. This problem will usually show up as a compile
30468 time error, but not always. It is a good idea to check all uses of the
30469 'Size attribute when porting Ada 83 code. The GNAT specific attribute
30470 Object_Size can provide a useful way of duplicating the behavior of
30471 some Ada 83 compiler systems.
30472
30473 @item Size of Access Types
30474 A common assumption in Ada 83 code is that an access type is in fact a pointer,
30475 and that therefore it will be the same size as a System.Address value. This
30476 assumption is true for GNAT in most cases with one exception. For the case of
30477 a pointer to an unconstrained array type (where the bounds may vary from one
30478 value of the access type to another), the default is to use a ``fat pointer'',
30479 which is represented as two separate pointers, one to the bounds, and one to
30480 the array. This representation has a number of advantages, including improved
30481 efficiency. However, it may cause some difficulties in porting existing Ada 83
30482 code which makes the assumption that, for example, pointers fit in 32 bits on
30483 a machine with 32-bit addressing.
30484
30485 To get around this problem, GNAT also permits the use of ``thin pointers'' for
30486 access types in this case (where the designated type is an unconstrained array
30487 type). These thin pointers are indeed the same size as a System.Address value.
30488 To specify a thin pointer, use a size clause for the type, for example:
30489
30490 @smallexample @c ada
30491 type X is access all String;
30492 for X'Size use Standard'Address_Size;
30493 @end smallexample
30494
30495 @noindent
30496 which will cause the type X to be represented using a single pointer.
30497 When using this representation, the bounds are right behind the array.
30498 This representation is slightly less efficient, and does not allow quite
30499 such flexibility in the use of foreign pointers or in using the
30500 Unrestricted_Access attribute to create pointers to non-aliased objects.
30501 But for any standard portable use of the access type it will work in
30502 a functionally correct manner and allow porting of existing code.
30503 Note that another way of forcing a thin pointer representation
30504 is to use a component size clause for the element size in an array,
30505 or a record representation clause for an access field in a record.
30506 @end table
30507
30508 @ifclear vms
30509 @c This brief section is only in the non-VMS version
30510 @c The complete chapter on HP Ada is in the VMS version
30511 @node Compatibility with HP Ada 83
30512 @section Compatibility with HP Ada 83
30513
30514 @noindent
30515 The VMS version of GNAT fully implements all the pragmas and attributes
30516 provided by HP Ada 83, as well as providing the standard HP Ada 83
30517 libraries, including Starlet. In addition, data layouts and parameter
30518 passing conventions are highly compatible. This means that porting
30519 existing HP Ada 83 code to GNAT in VMS systems should be easier than
30520 most other porting efforts. The following are some of the most
30521 significant differences between GNAT and HP Ada 83.
30522
30523 @table @emph
30524 @item Default floating-point representation
30525 In GNAT, the default floating-point format is IEEE, whereas in HP Ada 83,
30526 it is VMS format. GNAT does implement the necessary pragmas
30527 (Long_Float, Float_Representation) for changing this default.
30528
30529 @item System
30530 The package System in GNAT exactly corresponds to the definition in the
30531 Ada 95 reference manual, which means that it excludes many of the
30532 HP Ada 83 extensions. However, a separate package Aux_DEC is provided
30533 that contains the additional definitions, and a special pragma,
30534 Extend_System allows this package to be treated transparently as an
30535 extension of package System.
30536
30537 @item To_Address
30538 The definitions provided by Aux_DEC are exactly compatible with those
30539 in the HP Ada 83 version of System, with one exception.
30540 HP Ada provides the following declarations:
30541
30542 @smallexample @c ada
30543 TO_ADDRESS (INTEGER)
30544 TO_ADDRESS (UNSIGNED_LONGWORD)
30545 TO_ADDRESS (@i{universal_integer})
30546 @end smallexample
30547
30548 @noindent
30549 The version of TO_ADDRESS taking a @i{universal integer} argument is in fact
30550 an extension to Ada 83 not strictly compatible with the reference manual.
30551 In GNAT, we are constrained to be exactly compatible with the standard,
30552 and this means we cannot provide this capability. In HP Ada 83, the
30553 point of this definition is to deal with a call like:
30554
30555 @smallexample @c ada
30556 TO_ADDRESS (16#12777#);
30557 @end smallexample
30558
30559 @noindent
30560 Normally, according to the Ada 83 standard, one would expect this to be
30561 ambiguous, since it matches both the INTEGER and UNSIGNED_LONGWORD forms
30562 of TO_ADDRESS@. However, in HP Ada 83, there is no ambiguity, since the
30563 definition using @i{universal_integer} takes precedence.
30564
30565 In GNAT, since the version with @i{universal_integer} cannot be supplied, it
30566 is not possible to be 100% compatible. Since there are many programs using
30567 numeric constants for the argument to TO_ADDRESS, the decision in GNAT was
30568 to change the name of the function in the UNSIGNED_LONGWORD case, so the
30569 declarations provided in the GNAT version of AUX_Dec are:
30570
30571 @smallexample @c ada
30572 function To_Address (X : Integer) return Address;
30573 pragma Pure_Function (To_Address);
30574
30575 function To_Address_Long (X : Unsigned_Longword)
30576 return Address;
30577 pragma Pure_Function (To_Address_Long);
30578 @end smallexample
30579
30580 @noindent
30581 This means that programs using TO_ADDRESS for UNSIGNED_LONGWORD must
30582 change the name to TO_ADDRESS_LONG@.
30583
30584 @item Task_Id values
30585 The Task_Id values assigned will be different in the two systems, and GNAT
30586 does not provide a specified value for the Task_Id of the environment task,
30587 which in GNAT is treated like any other declared task.
30588 @end table
30589
30590 @noindent
30591 For full details on these and other less significant compatibility issues,
30592 see appendix E of the HP publication entitled @cite{HP Ada, Technical
30593 Overview and Comparison on HP Platforms}.
30594
30595 For GNAT running on other than VMS systems, all the HP Ada 83 pragmas and
30596 attributes are recognized, although only a subset of them can sensibly
30597 be implemented. The description of pragmas in @ref{Implementation
30598 Defined Pragmas,,, gnat_rm, GNAT Reference Manual}
30599 indicates whether or not they are applicable to non-VMS systems.
30600 @end ifclear
30601
30602 @ifset vms
30603 @node Transitioning to 64-Bit GNAT for OpenVMS
30604 @section Transitioning to 64-Bit @value{EDITION} for OpenVMS
30605
30606 @noindent
30607 This section is meant to assist users of pre-2006 @value{EDITION}
30608 for Alpha OpenVMS who are transitioning to 64-bit @value{EDITION},
30609 the version of the GNAT technology supplied in 2006 and later for
30610 OpenVMS on both Alpha and I64.
30611
30612 @menu
30613 * Introduction to transitioning::
30614 * Migration of 32 bit code::
30615 * Taking advantage of 64 bit addressing::
30616 * Technical details::
30617 @end menu
30618
30619 @node Introduction to transitioning
30620 @subsection Introduction
30621
30622 @noindent
30623 64-bit @value{EDITION} for Open VMS has been designed to meet
30624 three main goals:
30625
30626 @enumerate
30627 @item
30628 Providing a full conforming implementation of Ada 95 and Ada 2005
30629
30630 @item
30631 Allowing maximum backward compatibility, thus easing migration of existing
30632 Ada source code
30633
30634 @item
30635 Supplying a path for exploiting the full 64-bit address range
30636 @end enumerate
30637
30638 @noindent
30639 Ada's strong typing semantics has made it
30640 impractical to have different 32-bit and 64-bit modes. As soon as
30641 one object could possibly be outside the 32-bit address space, this
30642 would make it necessary for the @code{System.Address} type to be 64 bits.
30643 In particular, this would cause inconsistencies if 32-bit code is
30644 called from 64-bit code that raises an exception.
30645
30646 This issue has been resolved by always using 64-bit addressing
30647 at the system level, but allowing for automatic conversions between
30648 32-bit and 64-bit addresses where required. Thus users who
30649 do not currently require 64-bit addressing capabilities, can
30650 recompile their code with only minimal changes (and indeed
30651 if the code is written in portable Ada, with no assumptions about
30652 the size of the @code{Address} type, then no changes at all are necessary).
30653 At the same time,
30654 this approach provides a simple, gradual upgrade path to future
30655 use of larger memories than available for 32-bit systems.
30656 Also, newly written applications or libraries will by default
30657 be fully compatible with future systems exploiting 64-bit
30658 addressing capabilities.
30659
30660 @ref{Migration of 32 bit code}, will focus on porting applications
30661 that do not require more than 2 GB of
30662 addressable memory. This code will be referred to as
30663 @emph{32-bit code}.
30664 For applications intending to exploit the full 64-bit address space,
30665 @ref{Taking advantage of 64 bit addressing},
30666 will consider further changes that may be required.
30667 Such code will be referred to below as @emph{64-bit code}.
30668
30669 @node Migration of 32 bit code
30670 @subsection Migration of 32-bit code
30671
30672 @menu
30673 * Address types::
30674 * Access types::
30675 * Unchecked conversions::
30676 * Predefined constants::
30677 * Interfacing with C::
30678 * Experience with source compatibility::
30679 @end menu
30680
30681 @node Address types
30682 @subsubsection Address types
30683
30684 @noindent
30685 To solve the problem of mixing 64-bit and 32-bit addressing,
30686 while maintaining maximum backward compatibility, the following
30687 approach has been taken:
30688
30689 @itemize @bullet
30690 @item
30691 @code{System.Address} always has a size of 64 bits
30692
30693 @item
30694 @code{System.Short_Address} is a 32-bit subtype of @code{System.Address}
30695 @end itemize
30696
30697 @noindent
30698 Since @code{System.Short_Address} is a subtype of @code{System.Address},
30699 a @code{Short_Address}
30700 may be used where an @code{Address} is required, and vice versa, without
30701 needing explicit type conversions.
30702 By virtue of the Open VMS parameter passing conventions,
30703 even imported
30704 and exported subprograms that have 32-bit address parameters are
30705 compatible with those that have 64-bit address parameters.
30706 (See @ref{Making code 64 bit clean} for details.)
30707
30708 The areas that may need attention are those where record types have
30709 been defined that contain components of the type @code{System.Address}, and
30710 where objects of this type are passed to code expecting a record layout with
30711 32-bit addresses.
30712
30713 Different compilers on different platforms cannot be
30714 expected to represent the same type in the same way,
30715 since alignment constraints
30716 and other system-dependent properties affect the compiler's decision.
30717 For that reason, Ada code
30718 generally uses representation clauses to specify the expected
30719 layout where required.
30720
30721 If such a representation clause uses 32 bits for a component having
30722 the type @code{System.Address}, 64-bit @value{EDITION} for OpenVMS
30723 will detect that error and produce a specific diagnostic message.
30724 The developer should then determine whether the representation
30725 should be 64 bits or not and make either of two changes:
30726 change the size to 64 bits and leave the type as @code{System.Address}, or
30727 leave the size as 32 bits and change the type to @code{System.Short_Address}.
30728 Since @code{Short_Address} is a subtype of @code{Address}, no changes are
30729 required in any code setting or accessing the field; the compiler will
30730 automatically perform any needed conversions between address
30731 formats.
30732
30733 @node Access types
30734 @subsubsection Access types
30735
30736 @noindent
30737 By default, objects designated by access values are always
30738 allocated in the 32-bit
30739 address space. Thus legacy code will never contain
30740 any objects that are not addressable with 32-bit addresses, and
30741 the compiler will never raise exceptions as result of mixing
30742 32-bit and 64-bit addresses.
30743
30744 However, the access values themselves are represented in 64 bits, for optimum
30745 performance and future compatibility with 64-bit code. As was
30746 the case with @code{System.Address}, the compiler will give an error message
30747 if an object or record component has a representation clause that
30748 requires the access value to fit in 32 bits. In such a situation,
30749 an explicit size clause for the access type, specifying 32 bits,
30750 will have the desired effect.
30751
30752 General access types (declared with @code{access all}) can never be
30753 32 bits, as values of such types must be able to refer to any object
30754 of the designated type,
30755 including objects residing outside the 32-bit address range.
30756 Existing Ada 83 code will not contain such type definitions,
30757 however, since general access types were introduced in Ada 95.
30758
30759 @node Unchecked conversions
30760 @subsubsection Unchecked conversions
30761
30762 @noindent
30763 In the case of an @code{Unchecked_Conversion} where the source type is a
30764 64-bit access type or the type @code{System.Address}, and the target
30765 type is a 32-bit type, the compiler will generate a warning.
30766 Even though the generated code will still perform the required
30767 conversions, it is highly recommended in these cases to use
30768 respectively a 32-bit access type or @code{System.Short_Address}
30769 as the source type.
30770
30771 @node Predefined constants
30772 @subsubsection Predefined constants
30773
30774 @noindent
30775 The following table shows the correspondence between pre-2006 versions of
30776 @value{EDITION} on Alpha OpenVMS (``Old'') and 64-bit @value{EDITION}
30777 (``New''):
30778
30779 @multitable {@code{System.Short_Memory_Size}} {2**32} {2**64}
30780 @item @b{Constant} @tab @b{Old} @tab @b{New}
30781 @item @code{System.Word_Size} @tab 32 @tab 64
30782 @item @code{System.Memory_Size} @tab 2**32 @tab 2**64
30783 @item @code{System.Short_Memory_Size} @tab 2**32 @tab 2**32
30784 @item @code{System.Address_Size} @tab 32 @tab 64
30785 @end multitable
30786
30787 @noindent
30788 If you need to refer to the specific
30789 memory size of a 32-bit implementation, instead of the
30790 actual memory size, use @code{System.Short_Memory_Size}
30791 rather than @code{System.Memory_Size}.
30792 Similarly, references to @code{System.Address_Size} may need
30793 to be replaced by @code{System.Short_Address'Size}.
30794 The program @command{gnatfind} may be useful for locating
30795 references to the above constants, so that you can verify that they
30796 are still correct.
30797
30798 @node Interfacing with C
30799 @subsubsection Interfacing with C
30800
30801 @noindent
30802 In order to minimize the impact of the transition to 64-bit addresses on
30803 legacy programs, some fundamental types in the @code{Interfaces.C}
30804 package hierarchy continue to be represented in 32 bits.
30805 These types are: @code{ptrdiff_t}, @code{size_t}, and @code{chars_ptr}.
30806 This eases integration with the default HP C layout choices, for example
30807 as found in the system routines in @code{DECC$SHR.EXE}.
30808 Because of this implementation choice, the type fully compatible with
30809 @code{chars_ptr} is now @code{Short_Address} and not @code{Address}.
30810 Depending on the context the compiler will issue a
30811 warning or an error when type @code{Address} is used, alerting the user to a
30812 potential problem. Otherwise 32-bit programs that use
30813 @code{Interfaces.C} should normally not require code modifications
30814
30815 The other issue arising with C interfacing concerns pragma @code{Convention}.
30816 For VMS 64-bit systems, there is an issue of the appropriate default size
30817 of C convention pointers in the absence of an explicit size clause. The HP
30818 C compiler can choose either 32 or 64 bits depending on compiler options.
30819 GNAT chooses 32-bits rather than 64-bits in the default case where no size
30820 clause is given. This proves a better choice for porting 32-bit legacy
30821 applications. In order to have a 64-bit representation, it is necessary to
30822 specify a size representation clause. For example:
30823
30824 @smallexample @c ada
30825 type int_star is access Interfaces.C.int;
30826 pragma Convention(C, int_star);
30827 for int_star'Size use 64; -- Necessary to get 64 and not 32 bits
30828 @end smallexample
30829
30830 @node Experience with source compatibility
30831 @subsubsection Experience with source compatibility
30832
30833 @noindent
30834 The Security Server and STARLET on I64 provide an interesting ``test case''
30835 for source compatibility issues, since it is in such system code
30836 where assumptions about @code{Address} size might be expected to occur.
30837 Indeed, there were a small number of occasions in the Security Server
30838 file @file{jibdef.ads}
30839 where a representation clause for a record type specified
30840 32 bits for a component of type @code{Address}.
30841 All of these errors were detected by the compiler.
30842 The repair was obvious and immediate; to simply replace @code{Address} by
30843 @code{Short_Address}.
30844
30845 In the case of STARLET, there were several record types that should
30846 have had representation clauses but did not. In these record types
30847 there was an implicit assumption that an @code{Address} value occupied
30848 32 bits.
30849 These compiled without error, but their usage resulted in run-time error
30850 returns from STARLET system calls.
30851 Future GNAT technology enhancements may include a tool that detects and flags
30852 these sorts of potential source code porting problems.
30853
30854 @c ****************************************
30855 @node Taking advantage of 64 bit addressing
30856 @subsection Taking advantage of 64-bit addressing
30857
30858 @menu
30859 * Making code 64 bit clean::
30860 * Allocating memory from the 64 bit storage pool::
30861 * Restrictions on use of 64 bit objects::
30862 * Using 64 bit storage pools by default::
30863 * General access types::
30864 * STARLET and other predefined libraries::
30865 @end menu
30866
30867 @node Making code 64 bit clean
30868 @subsubsection Making code 64-bit clean
30869
30870 @noindent
30871 In order to prevent problems that may occur when (parts of) a
30872 system start using memory outside the 32-bit address range,
30873 we recommend some additional guidelines:
30874
30875 @itemize @bullet
30876 @item
30877 For imported subprograms that take parameters of the
30878 type @code{System.Address}, ensure that these subprograms can
30879 indeed handle 64-bit addresses. If not, or when in doubt,
30880 change the subprogram declaration to specify
30881 @code{System.Short_Address} instead.
30882
30883 @item
30884 Resolve all warnings related to size mismatches in
30885 unchecked conversions. Failing to do so causes
30886 erroneous execution if the source object is outside
30887 the 32-bit address space.
30888
30889 @item
30890 (optional) Explicitly use the 32-bit storage pool
30891 for access types used in a 32-bit context, or use
30892 generic access types where possible
30893 (@pxref{Restrictions on use of 64 bit objects}).
30894 @end itemize
30895
30896 @noindent
30897 If these rules are followed, the compiler will automatically insert
30898 any necessary checks to ensure that no addresses or access values
30899 passed to 32-bit code ever refer to objects outside the 32-bit
30900 address range.
30901 Any attempt to do this will raise @code{Constraint_Error}.
30902
30903 @node Allocating memory from the 64 bit storage pool
30904 @subsubsection Allocating memory from the 64-bit storage pool
30905
30906 @noindent
30907 For any access type @code{T} that potentially requires memory allocations
30908 beyond the 32-bit address space,
30909 use the following representation clause:
30910
30911 @smallexample @c ada
30912 for T'Storage_Pool use System.Pool_64;
30913 @end smallexample
30914
30915 @node Restrictions on use of 64 bit objects
30916 @subsubsection Restrictions on use of 64-bit objects
30917
30918 @noindent
30919 Taking the address of an object allocated from a 64-bit storage pool,
30920 and then passing this address to a subprogram expecting
30921 @code{System.Short_Address},
30922 or assigning it to a variable of type @code{Short_Address}, will cause
30923 @code{Constraint_Error} to be raised. In case the code is not 64-bit clean
30924 (@pxref{Making code 64 bit clean}), or checks are suppressed,
30925 no exception is raised and execution
30926 will become erroneous.
30927
30928 @node Using 64 bit storage pools by default
30929 @subsubsection Using 64-bit storage pools by default
30930
30931 @noindent
30932 In some cases it may be desirable to have the compiler allocate
30933 from 64-bit storage pools by default. This may be the case for
30934 libraries that are 64-bit clean, but may be used in both 32-bit
30935 and 64-bit contexts. For these cases the following configuration
30936 pragma may be specified:
30937
30938 @smallexample @c ada
30939 pragma Pool_64_Default;
30940 @end smallexample
30941
30942 @noindent
30943 Any code compiled in the context of this pragma will by default
30944 use the @code{System.Pool_64} storage pool. This default may be overridden
30945 for a specific access type @code{T} by the representation clause:
30946
30947 @smallexample @c ada
30948 for T'Storage_Pool use System.Pool_32;
30949 @end smallexample
30950
30951 @noindent
30952 Any object whose address may be passed to a subprogram with a
30953 @code{Short_Address} argument, or assigned to a variable of type
30954 @code{Short_Address}, needs to be allocated from this pool.
30955
30956 @node General access types
30957 @subsubsection General access types
30958
30959 @noindent
30960 Objects designated by access values from a
30961 general access type (declared with @code{access all}) are never allocated
30962 from a 64-bit storage pool. Code that uses general access types will
30963 accept objects allocated in either 32-bit or 64-bit address spaces,
30964 but never allocate objects outside the 32-bit address space.
30965 Using general access types ensures maximum compatibility with both
30966 32-bit and 64-bit code.
30967
30968 @node STARLET and other predefined libraries
30969 @subsubsection STARLET and other predefined libraries
30970
30971 @noindent
30972 All code that comes as part of GNAT is 64-bit clean, but the
30973 restrictions given in @ref{Restrictions on use of 64 bit objects},
30974 still apply. Look at the package
30975 specs to see in which contexts objects allocated
30976 in 64-bit address space are acceptable.
30977
30978 @node Technical details
30979 @subsection Technical details
30980
30981 @noindent
30982 64-bit @value{EDITION} for Open VMS takes advantage of the freedom given in the
30983 Ada standard with respect to the type of @code{System.Address}. Previous
30984 versions of GNAT Pro have defined this type as private and implemented it as a
30985 modular type.
30986
30987 In order to allow defining @code{System.Short_Address} as a proper subtype,
30988 and to match the implicit sign extension in parameter passing,
30989 in 64-bit @value{EDITION} for Open VMS, @code{System.Address} is defined as a
30990 visible (i.e., non-private) integer type.
30991 Standard operations on the type, such as the binary operators ``+'', ``-'',
30992 etc., that take @code{Address} operands and return an @code{Address} result,
30993 have been hidden by declaring these
30994 @code{abstract}, a feature introduced in Ada 95 that helps avoid the potential
30995 ambiguities that would otherwise result from overloading.
30996 (Note that, although @code{Address} is a visible integer type,
30997 good programming practice dictates against exploiting the type's
30998 integer properties such as literals, since this will compromise
30999 code portability.)
31000
31001 Defining @code{Address} as a visible integer type helps achieve
31002 maximum compatibility for existing Ada code,
31003 without sacrificing the capabilities of the 64-bit architecture.
31004 @end ifset
31005
31006 @c ************************************************
31007 @ifset unw
31008 @node Microsoft Windows Topics
31009 @appendix Microsoft Windows Topics
31010 @cindex Windows NT
31011 @cindex Windows 95
31012 @cindex Windows 98
31013
31014 @noindent
31015 This chapter describes topics that are specific to the Microsoft Windows
31016 platforms (NT, 2000, and XP Professional).
31017
31018 @menu
31019 * Using GNAT on Windows::
31020 * Using a network installation of GNAT::
31021 * CONSOLE and WINDOWS subsystems::
31022 * Temporary Files::
31023 * Mixed-Language Programming on Windows::
31024 * Windows Calling Conventions::
31025 * Introduction to Dynamic Link Libraries (DLLs)::
31026 * Using DLLs with GNAT::
31027 * Building DLLs with GNAT::
31028 * Building DLLs with GNAT Project files::
31029 * Building DLLs with gnatdll::
31030 * GNAT and Windows Resources::
31031 * Debugging a DLL::
31032 * Setting Stack Size from gnatlink::
31033 * Setting Heap Size from gnatlink::
31034 @end menu
31035
31036 @node Using GNAT on Windows
31037 @section Using GNAT on Windows
31038
31039 @noindent
31040 One of the strengths of the GNAT technology is that its tool set
31041 (@command{gcc}, @command{gnatbind}, @command{gnatlink}, @command{gnatmake}, the
31042 @code{gdb} debugger, etc.) is used in the same way regardless of the
31043 platform.
31044
31045 On Windows this tool set is complemented by a number of Microsoft-specific
31046 tools that have been provided to facilitate interoperability with Windows
31047 when this is required. With these tools:
31048
31049 @itemize @bullet
31050
31051 @item
31052 You can build applications using the @code{CONSOLE} or @code{WINDOWS}
31053 subsystems.
31054
31055 @item
31056 You can use any Dynamically Linked Library (DLL) in your Ada code (both
31057 relocatable and non-relocatable DLLs are supported).
31058
31059 @item
31060 You can build Ada DLLs for use in other applications. These applications
31061 can be written in a language other than Ada (e.g., C, C++, etc). Again both
31062 relocatable and non-relocatable Ada DLLs are supported.
31063
31064 @item
31065 You can include Windows resources in your Ada application.
31066
31067 @item
31068 You can use or create COM/DCOM objects.
31069 @end itemize
31070
31071 @noindent
31072 Immediately below are listed all known general GNAT-for-Windows restrictions.
31073 Other restrictions about specific features like Windows Resources and DLLs
31074 are listed in separate sections below.
31075
31076 @itemize @bullet
31077
31078 @item
31079 It is not possible to use @code{GetLastError} and @code{SetLastError}
31080 when tasking, protected records, or exceptions are used. In these
31081 cases, in order to implement Ada semantics, the GNAT run-time system
31082 calls certain Win32 routines that set the last error variable to 0 upon
31083 success. It should be possible to use @code{GetLastError} and
31084 @code{SetLastError} when tasking, protected record, and exception
31085 features are not used, but it is not guaranteed to work.
31086
31087 @item
31088 It is not possible to link against Microsoft libraries except for
31089 import libraries. The library must be built to be compatible with
31090 @file{MSVCRT.LIB} (/MD Microsoft compiler option), @file{LIBC.LIB} and
31091 @file{LIBCMT.LIB} (/ML or /MT Microsoft compiler options) are known to
31092 not be compatible with the GNAT runtime. Even if the library is
31093 compatible with @file{MSVCRT.LIB} it is not guaranteed to work.
31094
31095 @item
31096 When the compilation environment is located on FAT32 drives, users may
31097 experience recompilations of the source files that have not changed if
31098 Daylight Saving Time (DST) state has changed since the last time files
31099 were compiled. NTFS drives do not have this problem.
31100
31101 @item
31102 No components of the GNAT toolset use any entries in the Windows
31103 registry. The only entries that can be created are file associations and
31104 PATH settings, provided the user has chosen to create them at installation
31105 time, as well as some minimal book-keeping information needed to correctly
31106 uninstall or integrate different GNAT products.
31107 @end itemize
31108
31109 @node Using a network installation of GNAT
31110 @section Using a network installation of GNAT
31111
31112 @noindent
31113 Make sure the system on which GNAT is installed is accessible from the
31114 current machine, i.e., the install location is shared over the network.
31115 Shared resources are accessed on Windows by means of UNC paths, which
31116 have the format @code{\\server\sharename\path}
31117
31118 In order to use such a network installation, simply add the UNC path of the
31119 @file{bin} directory of your GNAT installation in front of your PATH. For
31120 example, if GNAT is installed in @file{\GNAT} directory of a share location
31121 called @file{c-drive} on a machine @file{LOKI}, the following command will
31122 make it available:
31123
31124 @code{@ @ @ path \\loki\c-drive\gnat\bin;%path%}
31125
31126 Be aware that every compilation using the network installation results in the
31127 transfer of large amounts of data across the network and will likely cause
31128 serious performance penalty.
31129
31130 @node CONSOLE and WINDOWS subsystems
31131 @section CONSOLE and WINDOWS subsystems
31132 @cindex CONSOLE Subsystem
31133 @cindex WINDOWS Subsystem
31134 @cindex -mwindows
31135
31136 @noindent
31137 There are two main subsystems under Windows. The @code{CONSOLE} subsystem
31138 (which is the default subsystem) will always create a console when
31139 launching the application. This is not something desirable when the
31140 application has a Windows GUI. To get rid of this console the
31141 application must be using the @code{WINDOWS} subsystem. To do so
31142 the @option{-mwindows} linker option must be specified.
31143
31144 @smallexample
31145 $ gnatmake winprog -largs -mwindows
31146 @end smallexample
31147
31148 @node Temporary Files
31149 @section Temporary Files
31150 @cindex Temporary files
31151
31152 @noindent
31153 It is possible to control where temporary files gets created by setting
31154 the @env{TMP} environment variable. The file will be created:
31155
31156 @itemize
31157 @item Under the directory pointed to by the @env{TMP} environment variable if
31158 this directory exists.
31159
31160 @item Under @file{c:\temp}, if the @env{TMP} environment variable is not
31161 set (or not pointing to a directory) and if this directory exists.
31162
31163 @item Under the current working directory otherwise.
31164 @end itemize
31165
31166 @noindent
31167 This allows you to determine exactly where the temporary
31168 file will be created. This is particularly useful in networked
31169 environments where you may not have write access to some
31170 directories.
31171
31172 @node Mixed-Language Programming on Windows
31173 @section Mixed-Language Programming on Windows
31174
31175 @noindent
31176 Developing pure Ada applications on Windows is no different than on
31177 other GNAT-supported platforms. However, when developing or porting an
31178 application that contains a mix of Ada and C/C++, the choice of your
31179 Windows C/C++ development environment conditions your overall
31180 interoperability strategy.
31181
31182 If you use @command{gcc} to compile the non-Ada part of your application,
31183 there are no Windows-specific restrictions that affect the overall
31184 interoperability with your Ada code. If you plan to use
31185 Microsoft tools (e.g.@: Microsoft Visual C/C++), you should be aware of
31186 the following limitations:
31187
31188 @itemize @bullet
31189 @item
31190 You cannot link your Ada code with an object or library generated with
31191 Microsoft tools if these use the @code{.tls} section (Thread Local
31192 Storage section) since the GNAT linker does not yet support this section.
31193
31194 @item
31195 You cannot link your Ada code with an object or library generated with
31196 Microsoft tools if these use I/O routines other than those provided in
31197 the Microsoft DLL: @code{msvcrt.dll}. This is because the GNAT run time
31198 uses the services of @code{msvcrt.dll} for its I/Os. Use of other I/O
31199 libraries can cause a conflict with @code{msvcrt.dll} services. For
31200 instance Visual C++ I/O stream routines conflict with those in
31201 @code{msvcrt.dll}.
31202 @end itemize
31203
31204 @noindent
31205 If you do want to use the Microsoft tools for your non-Ada code and hit one
31206 of the above limitations, you have two choices:
31207
31208 @enumerate
31209 @item
31210 Encapsulate your non-Ada code in a DLL to be linked with your Ada
31211 application. In this case, use the Microsoft or whatever environment to
31212 build the DLL and use GNAT to build your executable
31213 (@pxref{Using DLLs with GNAT}).
31214
31215 @item
31216 Or you can encapsulate your Ada code in a DLL to be linked with the
31217 other part of your application. In this case, use GNAT to build the DLL
31218 (@pxref{Building DLLs with GNAT}) and use the Microsoft or whatever
31219 environment to build your executable.
31220 @end enumerate
31221
31222 @node Windows Calling Conventions
31223 @section Windows Calling Conventions
31224 @findex Stdcall
31225 @findex APIENTRY
31226
31227 @menu
31228 * C Calling Convention::
31229 * Stdcall Calling Convention::
31230 * Win32 Calling Convention::
31231 * DLL Calling Convention::
31232 @end menu
31233
31234 @noindent
31235 When a subprogram @code{F} (caller) calls a subprogram @code{G}
31236 (callee), there are several ways to push @code{G}'s parameters on the
31237 stack and there are several possible scenarios to clean up the stack
31238 upon @code{G}'s return. A calling convention is an agreed upon software
31239 protocol whereby the responsibilities between the caller (@code{F}) and
31240 the callee (@code{G}) are clearly defined. Several calling conventions
31241 are available for Windows:
31242
31243 @itemize @bullet
31244 @item
31245 @code{C} (Microsoft defined)
31246
31247 @item
31248 @code{Stdcall} (Microsoft defined)
31249
31250 @item
31251 @code{Win32} (GNAT specific)
31252
31253 @item
31254 @code{DLL} (GNAT specific)
31255 @end itemize
31256
31257 @node C Calling Convention
31258 @subsection @code{C} Calling Convention
31259
31260 @noindent
31261 This is the default calling convention used when interfacing to C/C++
31262 routines compiled with either @command{gcc} or Microsoft Visual C++.
31263
31264 In the @code{C} calling convention subprogram parameters are pushed on the
31265 stack by the caller from right to left. The caller itself is in charge of
31266 cleaning up the stack after the call. In addition, the name of a routine
31267 with @code{C} calling convention is mangled by adding a leading underscore.
31268
31269 The name to use on the Ada side when importing (or exporting) a routine
31270 with @code{C} calling convention is the name of the routine. For
31271 instance the C function:
31272
31273 @smallexample
31274 int get_val (long);
31275 @end smallexample
31276
31277 @noindent
31278 should be imported from Ada as follows:
31279
31280 @smallexample @c ada
31281 @group
31282 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
31283 pragma Import (C, Get_Val, External_Name => "get_val");
31284 @end group
31285 @end smallexample
31286
31287 @noindent
31288 Note that in this particular case the @code{External_Name} parameter could
31289 have been omitted since, when missing, this parameter is taken to be the
31290 name of the Ada entity in lower case. When the @code{Link_Name} parameter
31291 is missing, as in the above example, this parameter is set to be the
31292 @code{External_Name} with a leading underscore.
31293
31294 When importing a variable defined in C, you should always use the @code{C}
31295 calling convention unless the object containing the variable is part of a
31296 DLL (in which case you should use the @code{Stdcall} calling
31297 convention, @pxref{Stdcall Calling Convention}).
31298
31299 @node Stdcall Calling Convention
31300 @subsection @code{Stdcall} Calling Convention
31301
31302 @noindent
31303 This convention, which was the calling convention used for Pascal
31304 programs, is used by Microsoft for all the routines in the Win32 API for
31305 efficiency reasons. It must be used to import any routine for which this
31306 convention was specified.
31307
31308 In the @code{Stdcall} calling convention subprogram parameters are pushed
31309 on the stack by the caller from right to left. The callee (and not the
31310 caller) is in charge of cleaning the stack on routine exit. In addition,
31311 the name of a routine with @code{Stdcall} calling convention is mangled by
31312 adding a leading underscore (as for the @code{C} calling convention) and a
31313 trailing @code{@@}@code{@var{nn}}, where @var{nn} is the overall size (in
31314 bytes) of the parameters passed to the routine.
31315
31316 The name to use on the Ada side when importing a C routine with a
31317 @code{Stdcall} calling convention is the name of the C routine. The leading
31318 underscore and trailing @code{@@}@code{@var{nn}} are added automatically by
31319 the compiler. For instance the Win32 function:
31320
31321 @smallexample
31322 @b{APIENTRY} int get_val (long);
31323 @end smallexample
31324
31325 @noindent
31326 should be imported from Ada as follows:
31327
31328 @smallexample @c ada
31329 @group
31330 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
31331 pragma Import (Stdcall, Get_Val);
31332 -- On the x86 a long is 4 bytes, so the Link_Name is "_get_val@@4"
31333 @end group
31334 @end smallexample
31335
31336 @noindent
31337 As for the @code{C} calling convention, when the @code{External_Name}
31338 parameter is missing, it is taken to be the name of the Ada entity in lower
31339 case. If instead of writing the above import pragma you write:
31340
31341 @smallexample @c ada
31342 @group
31343 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
31344 pragma Import (Stdcall, Get_Val, External_Name => "retrieve_val");
31345 @end group
31346 @end smallexample
31347
31348 @noindent
31349 then the imported routine is @code{_retrieve_val@@4}. However, if instead
31350 of specifying the @code{External_Name} parameter you specify the
31351 @code{Link_Name} as in the following example:
31352
31353 @smallexample @c ada
31354 @group
31355 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
31356 pragma Import (Stdcall, Get_Val, Link_Name => "retrieve_val");
31357 @end group
31358 @end smallexample
31359
31360 @noindent
31361 then the imported routine is @code{retrieve_val}, that is, there is no
31362 decoration at all. No leading underscore and no Stdcall suffix
31363 @code{@@}@code{@var{nn}}.
31364
31365 @noindent
31366 This is especially important as in some special cases a DLL's entry
31367 point name lacks a trailing @code{@@}@code{@var{nn}} while the exported
31368 name generated for a call has it.
31369
31370 @noindent
31371 It is also possible to import variables defined in a DLL by using an
31372 import pragma for a variable. As an example, if a DLL contains a
31373 variable defined as:
31374
31375 @smallexample
31376 int my_var;
31377 @end smallexample
31378
31379 @noindent
31380 then, to access this variable from Ada you should write:
31381
31382 @smallexample @c ada
31383 @group
31384 My_Var : Interfaces.C.int;
31385 pragma Import (Stdcall, My_Var);
31386 @end group
31387 @end smallexample
31388
31389 @noindent
31390 Note that to ease building cross-platform bindings this convention
31391 will be handled as a @code{C} calling convention on non-Windows platforms.
31392
31393 @node Win32 Calling Convention
31394 @subsection @code{Win32} Calling Convention
31395
31396 @noindent
31397 This convention, which is GNAT-specific is fully equivalent to the
31398 @code{Stdcall} calling convention described above.
31399
31400 @node DLL Calling Convention
31401 @subsection @code{DLL} Calling Convention
31402
31403 @noindent
31404 This convention, which is GNAT-specific is fully equivalent to the
31405 @code{Stdcall} calling convention described above.
31406
31407 @node Introduction to Dynamic Link Libraries (DLLs)
31408 @section Introduction to Dynamic Link Libraries (DLLs)
31409 @findex DLL
31410
31411 @noindent
31412 A Dynamically Linked Library (DLL) is a library that can be shared by
31413 several applications running under Windows. A DLL can contain any number of
31414 routines and variables.
31415
31416 One advantage of DLLs is that you can change and enhance them without
31417 forcing all the applications that depend on them to be relinked or
31418 recompiled. However, you should be aware than all calls to DLL routines are
31419 slower since, as you will understand below, such calls are indirect.
31420
31421 To illustrate the remainder of this section, suppose that an application
31422 wants to use the services of a DLL @file{API.dll}. To use the services
31423 provided by @file{API.dll} you must statically link against the DLL or
31424 an import library which contains a jump table with an entry for each
31425 routine and variable exported by the DLL. In the Microsoft world this
31426 import library is called @file{API.lib}. When using GNAT this import
31427 library is called either @file{libAPI.dll.a}, @file{libapi.dll.a},
31428 @file{libAPI.a} or @file{libapi.a} (names are case insensitive).
31429
31430 After you have linked your application with the DLL or the import library
31431 and you run your application, here is what happens:
31432
31433 @enumerate
31434 @item
31435 Your application is loaded into memory.
31436
31437 @item
31438 The DLL @file{API.dll} is mapped into the address space of your
31439 application. This means that:
31440
31441 @itemize @bullet
31442 @item
31443 The DLL will use the stack of the calling thread.
31444
31445 @item
31446 The DLL will use the virtual address space of the calling process.
31447
31448 @item
31449 The DLL will allocate memory from the virtual address space of the calling
31450 process.
31451
31452 @item
31453 Handles (pointers) can be safely exchanged between routines in the DLL
31454 routines and routines in the application using the DLL.
31455 @end itemize
31456
31457 @item
31458 The entries in the jump table (from the import library @file{libAPI.dll.a}
31459 or @file{API.lib} or automatically created when linking against a DLL)
31460 which is part of your application are initialized with the addresses
31461 of the routines and variables in @file{API.dll}.
31462
31463 @item
31464 If present in @file{API.dll}, routines @code{DllMain} or
31465 @code{DllMainCRTStartup} are invoked. These routines typically contain
31466 the initialization code needed for the well-being of the routines and
31467 variables exported by the DLL.
31468 @end enumerate
31469
31470 @noindent
31471 There is an additional point which is worth mentioning. In the Windows
31472 world there are two kind of DLLs: relocatable and non-relocatable
31473 DLLs. Non-relocatable DLLs can only be loaded at a very specific address
31474 in the target application address space. If the addresses of two
31475 non-relocatable DLLs overlap and these happen to be used by the same
31476 application, a conflict will occur and the application will run
31477 incorrectly. Hence, when possible, it is always preferable to use and
31478 build relocatable DLLs. Both relocatable and non-relocatable DLLs are
31479 supported by GNAT. Note that the @option{-s} linker option (see GNU Linker
31480 User's Guide) removes the debugging symbols from the DLL but the DLL can
31481 still be relocated.
31482
31483 As a side note, an interesting difference between Microsoft DLLs and
31484 Unix shared libraries, is the fact that on most Unix systems all public
31485 routines are exported by default in a Unix shared library, while under
31486 Windows it is possible (but not required) to list exported routines in
31487 a definition file (@pxref{The Definition File}).
31488
31489 @node Using DLLs with GNAT
31490 @section Using DLLs with GNAT
31491
31492 @menu
31493 * Creating an Ada Spec for the DLL Services::
31494 * Creating an Import Library::
31495 @end menu
31496
31497 @noindent
31498 To use the services of a DLL, say @file{API.dll}, in your Ada application
31499 you must have:
31500
31501 @enumerate
31502 @item
31503 The Ada spec for the routines and/or variables you want to access in
31504 @file{API.dll}. If not available this Ada spec must be built from the C/C++
31505 header files provided with the DLL.
31506
31507 @item
31508 The import library (@file{libAPI.dll.a} or @file{API.lib}). As previously
31509 mentioned an import library is a statically linked library containing the
31510 import table which will be filled at load time to point to the actual
31511 @file{API.dll} routines. Sometimes you don't have an import library for the
31512 DLL you want to use. The following sections will explain how to build
31513 one. Note that this is optional.
31514
31515 @item
31516 The actual DLL, @file{API.dll}.
31517 @end enumerate
31518
31519 @noindent
31520 Once you have all the above, to compile an Ada application that uses the
31521 services of @file{API.dll} and whose main subprogram is @code{My_Ada_App},
31522 you simply issue the command
31523
31524 @smallexample
31525 $ gnatmake my_ada_app -largs -lAPI
31526 @end smallexample
31527
31528 @noindent
31529 The argument @option{-largs -lAPI} at the end of the @command{gnatmake} command
31530 tells the GNAT linker to look first for a library named @file{API.lib}
31531 (Microsoft-style name) and if not found for a libraries named
31532 @file{libAPI.dll.a}, @file{API.dll.a} or @file{libAPI.a}.
31533 (GNAT-style name). Note that if the Ada package spec for @file{API.dll}
31534 contains the following pragma
31535
31536 @smallexample @c ada
31537 pragma Linker_Options ("-lAPI");
31538 @end smallexample
31539
31540 @noindent
31541 you do not have to add @option{-largs -lAPI} at the end of the
31542 @command{gnatmake} command.
31543
31544 If any one of the items above is missing you will have to create it
31545 yourself. The following sections explain how to do so using as an
31546 example a fictitious DLL called @file{API.dll}.
31547
31548 @node Creating an Ada Spec for the DLL Services
31549 @subsection Creating an Ada Spec for the DLL Services
31550
31551 @noindent
31552 A DLL typically comes with a C/C++ header file which provides the
31553 definitions of the routines and variables exported by the DLL. The Ada
31554 equivalent of this header file is a package spec that contains definitions
31555 for the imported entities. If the DLL you intend to use does not come with
31556 an Ada spec you have to generate one such spec yourself. For example if
31557 the header file of @file{API.dll} is a file @file{api.h} containing the
31558 following two definitions:
31559
31560 @smallexample
31561 @group
31562 @cartouche
31563 int some_var;
31564 int get (char *);
31565 @end cartouche
31566 @end group
31567 @end smallexample
31568
31569 @noindent
31570 then the equivalent Ada spec could be:
31571
31572 @smallexample @c ada
31573 @group
31574 @cartouche
31575 with Interfaces.C.Strings;
31576 package API is
31577 use Interfaces;
31578
31579 Some_Var : C.int;
31580 function Get (Str : C.Strings.Chars_Ptr) return C.int;
31581
31582 private
31583 pragma Import (C, Get);
31584 pragma Import (DLL, Some_Var);
31585 end API;
31586 @end cartouche
31587 @end group
31588 @end smallexample
31589
31590 @noindent
31591 Note that a variable is
31592 @strong{always imported with a Stdcall convention}. A function
31593 can have @code{C} or @code{Stdcall} convention.
31594 (@pxref{Windows Calling Conventions}).
31595
31596 @node Creating an Import Library
31597 @subsection Creating an Import Library
31598 @cindex Import library
31599
31600 @menu
31601 * The Definition File::
31602 * GNAT-Style Import Library::
31603 * Microsoft-Style Import Library::
31604 @end menu
31605
31606 @noindent
31607 If a Microsoft-style import library @file{API.lib} or a GNAT-style
31608 import library @file{libAPI.dll.a} or @file{libAPI.a} is available
31609 with @file{API.dll} you can skip this section. You can also skip this
31610 section if @file{API.dll} or @file{libAPI.dll} is built with GNU tools
31611 as in this case it is possible to link directly against the
31612 DLL. Otherwise read on.
31613
31614 @node The Definition File
31615 @subsubsection The Definition File
31616 @cindex Definition file
31617 @findex .def
31618
31619 @noindent
31620 As previously mentioned, and unlike Unix systems, the list of symbols
31621 that are exported from a DLL must be provided explicitly in Windows.
31622 The main goal of a definition file is precisely that: list the symbols
31623 exported by a DLL. A definition file (usually a file with a @code{.def}
31624 suffix) has the following structure:
31625
31626 @smallexample
31627 @group
31628 @cartouche
31629 @r{[}LIBRARY @var{name}@r{]}
31630 @r{[}DESCRIPTION @var{string}@r{]}
31631 EXPORTS
31632 @var{symbol1}
31633 @var{symbol2}
31634 @dots{}
31635 @end cartouche
31636 @end group
31637 @end smallexample
31638
31639 @table @code
31640 @item LIBRARY @var{name}
31641 This section, which is optional, gives the name of the DLL.
31642
31643 @item DESCRIPTION @var{string}
31644 This section, which is optional, gives a description string that will be
31645 embedded in the import library.
31646
31647 @item EXPORTS
31648 This section gives the list of exported symbols (procedures, functions or
31649 variables). For instance in the case of @file{API.dll} the @code{EXPORTS}
31650 section of @file{API.def} looks like:
31651
31652 @smallexample
31653 @group
31654 @cartouche
31655 EXPORTS
31656 some_var
31657 get
31658 @end cartouche
31659 @end group
31660 @end smallexample
31661 @end table
31662
31663 @noindent
31664 Note that you must specify the correct suffix (@code{@@}@code{@var{nn}})
31665 (@pxref{Windows Calling Conventions}) for a Stdcall
31666 calling convention function in the exported symbols list.
31667
31668 @noindent
31669 There can actually be other sections in a definition file, but these
31670 sections are not relevant to the discussion at hand.
31671
31672 @node GNAT-Style Import Library
31673 @subsubsection GNAT-Style Import Library
31674
31675 @noindent
31676 To create a static import library from @file{API.dll} with the GNAT tools
31677 you should proceed as follows:
31678
31679 @enumerate
31680 @item
31681 Create the definition file @file{API.def} (@pxref{The Definition File}).
31682 For that use the @code{dll2def} tool as follows:
31683
31684 @smallexample
31685 $ dll2def API.dll > API.def
31686 @end smallexample
31687
31688 @noindent
31689 @code{dll2def} is a very simple tool: it takes as input a DLL and prints
31690 to standard output the list of entry points in the DLL. Note that if
31691 some routines in the DLL have the @code{Stdcall} convention
31692 (@pxref{Windows Calling Conventions}) with stripped @code{@@}@var{nn}
31693 suffix then you'll have to edit @file{api.def} to add it, and specify
31694 @option{-k} to @command{gnatdll} when creating the import library.
31695
31696 @noindent
31697 Here are some hints to find the right @code{@@}@var{nn} suffix.
31698
31699 @enumerate
31700 @item
31701 If you have the Microsoft import library (.lib), it is possible to get
31702 the right symbols by using Microsoft @code{dumpbin} tool (see the
31703 corresponding Microsoft documentation for further details).
31704
31705 @smallexample
31706 $ dumpbin /exports api.lib
31707 @end smallexample
31708
31709 @item
31710 If you have a message about a missing symbol at link time the compiler
31711 tells you what symbol is expected. You just have to go back to the
31712 definition file and add the right suffix.
31713 @end enumerate
31714
31715 @item
31716 Build the import library @code{libAPI.dll.a}, using @code{gnatdll}
31717 (@pxref{Using gnatdll}) as follows:
31718
31719 @smallexample
31720 $ gnatdll -e API.def -d API.dll
31721 @end smallexample
31722
31723 @noindent
31724 @code{gnatdll} takes as input a definition file @file{API.def} and the
31725 name of the DLL containing the services listed in the definition file
31726 @file{API.dll}. The name of the static import library generated is
31727 computed from the name of the definition file as follows: if the
31728 definition file name is @var{xyz}@code{.def}, the import library name will
31729 be @code{lib}@var{xyz}@code{.a}. Note that in the previous example option
31730 @option{-e} could have been removed because the name of the definition
31731 file (before the ``@code{.def}'' suffix) is the same as the name of the
31732 DLL (@pxref{Using gnatdll} for more information about @code{gnatdll}).
31733 @end enumerate
31734
31735 @node Microsoft-Style Import Library
31736 @subsubsection Microsoft-Style Import Library
31737
31738 @noindent
31739 With GNAT you can either use a GNAT-style or Microsoft-style import
31740 library. A Microsoft import library is needed only if you plan to make an
31741 Ada DLL available to applications developed with Microsoft
31742 tools (@pxref{Mixed-Language Programming on Windows}).
31743
31744 To create a Microsoft-style import library for @file{API.dll} you
31745 should proceed as follows:
31746
31747 @enumerate
31748 @item
31749 Create the definition file @file{API.def} from the DLL. For this use either
31750 the @code{dll2def} tool as described above or the Microsoft @code{dumpbin}
31751 tool (see the corresponding Microsoft documentation for further details).
31752
31753 @item
31754 Build the actual import library using Microsoft's @code{lib} utility:
31755
31756 @smallexample
31757 $ lib -machine:IX86 -def:API.def -out:API.lib
31758 @end smallexample
31759
31760 @noindent
31761 If you use the above command the definition file @file{API.def} must
31762 contain a line giving the name of the DLL:
31763
31764 @smallexample
31765 LIBRARY "API"
31766 @end smallexample
31767
31768 @noindent
31769 See the Microsoft documentation for further details about the usage of
31770 @code{lib}.
31771 @end enumerate
31772
31773 @node Building DLLs with GNAT
31774 @section Building DLLs with GNAT
31775 @cindex DLLs, building
31776
31777 @noindent
31778 This section explain how to build DLLs using the GNAT built-in DLL
31779 support. With the following procedure it is straight forward to build
31780 and use DLLs with GNAT.
31781
31782 @enumerate
31783
31784 @item building object files
31785
31786 The first step is to build all objects files that are to be included
31787 into the DLL. This is done by using the standard @command{gnatmake} tool.
31788
31789 @item building the DLL
31790
31791 To build the DLL you must use @command{gcc}'s @option{-shared}
31792 option. It is quite simple to use this method:
31793
31794 @smallexample
31795 $ gcc -shared -o api.dll obj1.o obj2.o @dots{}
31796 @end smallexample
31797
31798 It is important to note that in this case all symbols found in the
31799 object files are automatically exported. It is possible to restrict
31800 the set of symbols to export by passing to @command{gcc} a definition
31801 file, @pxref{The Definition File}. For example:
31802
31803 @smallexample
31804 $ gcc -shared -o api.dll api.def obj1.o obj2.o @dots{}
31805 @end smallexample
31806
31807 If you use a definition file you must export the elaboration procedures
31808 for every package that required one. Elaboration procedures are named
31809 using the package name followed by "_E".
31810
31811 @item preparing DLL to be used
31812
31813 For the DLL to be used by client programs the bodies must be hidden
31814 from it and the .ali set with read-only attribute. This is very important
31815 otherwise GNAT will recompile all packages and will not actually use
31816 the code in the DLL. For example:
31817
31818 @smallexample
31819 $ mkdir apilib
31820 $ copy *.ads *.ali api.dll apilib
31821 $ attrib +R apilib\*.ali
31822 @end smallexample
31823
31824 @end enumerate
31825
31826 At this point it is possible to use the DLL by directly linking
31827 against it. Note that you must use the GNAT shared runtime when using
31828 GNAT shared libraries. This is achieved by using @option{-shared} binder's
31829 option.
31830
31831 @smallexample
31832 $ gnatmake main -Iapilib -bargs -shared -largs -Lapilib -lAPI
31833 @end smallexample
31834
31835 @node Building DLLs with GNAT Project files
31836 @section Building DLLs with GNAT Project files
31837 @cindex DLLs, building
31838
31839 @noindent
31840 There is nothing specific to Windows in the build process.
31841 @pxref{Library Projects}.
31842
31843 @noindent
31844 Due to a system limitation, it is not possible under Windows to create threads
31845 when inside the @code{DllMain} routine which is used for auto-initialization
31846 of shared libraries, so it is not possible to have library level tasks in SALs.
31847
31848 @node Building DLLs with gnatdll
31849 @section Building DLLs with gnatdll
31850 @cindex DLLs, building
31851
31852 @menu
31853 * Limitations When Using Ada DLLs from Ada::
31854 * Exporting Ada Entities::
31855 * Ada DLLs and Elaboration::
31856 * Ada DLLs and Finalization::
31857 * Creating a Spec for Ada DLLs::
31858 * Creating the Definition File::
31859 * Using gnatdll::
31860 @end menu
31861
31862 @noindent
31863 Note that it is preferred to use the built-in GNAT DLL support
31864 (@pxref{Building DLLs with GNAT}) or GNAT Project files
31865 (@pxref{Building DLLs with GNAT Project files}) to build DLLs.
31866
31867 This section explains how to build DLLs containing Ada code using
31868 @code{gnatdll}. These DLLs will be referred to as Ada DLLs in the
31869 remainder of this section.
31870
31871 The steps required to build an Ada DLL that is to be used by Ada as well as
31872 non-Ada applications are as follows:
31873
31874 @enumerate
31875 @item
31876 You need to mark each Ada @i{entity} exported by the DLL with a @code{C} or
31877 @code{Stdcall} calling convention to avoid any Ada name mangling for the
31878 entities exported by the DLL (@pxref{Exporting Ada Entities}). You can
31879 skip this step if you plan to use the Ada DLL only from Ada applications.
31880
31881 @item
31882 Your Ada code must export an initialization routine which calls the routine
31883 @code{adainit} generated by @command{gnatbind} to perform the elaboration of
31884 the Ada code in the DLL (@pxref{Ada DLLs and Elaboration}). The initialization
31885 routine exported by the Ada DLL must be invoked by the clients of the DLL
31886 to initialize the DLL.
31887
31888 @item
31889 When useful, the DLL should also export a finalization routine which calls
31890 routine @code{adafinal} generated by @command{gnatbind} to perform the
31891 finalization of the Ada code in the DLL (@pxref{Ada DLLs and Finalization}).
31892 The finalization routine exported by the Ada DLL must be invoked by the
31893 clients of the DLL when the DLL services are no further needed.
31894
31895 @item
31896 You must provide a spec for the services exported by the Ada DLL in each
31897 of the programming languages to which you plan to make the DLL available.
31898
31899 @item
31900 You must provide a definition file listing the exported entities
31901 (@pxref{The Definition File}).
31902
31903 @item
31904 Finally you must use @code{gnatdll} to produce the DLL and the import
31905 library (@pxref{Using gnatdll}).
31906 @end enumerate
31907
31908 @noindent
31909 Note that a relocatable DLL stripped using the @code{strip}
31910 binutils tool will not be relocatable anymore. To build a DLL without
31911 debug information pass @code{-largs -s} to @code{gnatdll}. This
31912 restriction does not apply to a DLL built using a Library Project.
31913 @pxref{Library Projects}.
31914
31915 @node Limitations When Using Ada DLLs from Ada
31916 @subsection Limitations When Using Ada DLLs from Ada
31917
31918 @noindent
31919 When using Ada DLLs from Ada applications there is a limitation users
31920 should be aware of. Because on Windows the GNAT run time is not in a DLL of
31921 its own, each Ada DLL includes a part of the GNAT run time. Specifically,
31922 each Ada DLL includes the services of the GNAT run time that are necessary
31923 to the Ada code inside the DLL. As a result, when an Ada program uses an
31924 Ada DLL there are two independent GNAT run times: one in the Ada DLL and
31925 one in the main program.
31926
31927 It is therefore not possible to exchange GNAT run-time objects between the
31928 Ada DLL and the main Ada program. Example of GNAT run-time objects are file
31929 handles (e.g.@: @code{Text_IO.File_Type}), tasks types, protected objects
31930 types, etc.
31931
31932 It is completely safe to exchange plain elementary, array or record types,
31933 Windows object handles, etc.
31934
31935 @node Exporting Ada Entities
31936 @subsection Exporting Ada Entities
31937 @cindex Export table
31938
31939 @noindent
31940 Building a DLL is a way to encapsulate a set of services usable from any
31941 application. As a result, the Ada entities exported by a DLL should be
31942 exported with the @code{C} or @code{Stdcall} calling conventions to avoid
31943 any Ada name mangling. As an example here is an Ada package
31944 @code{API}, spec and body, exporting two procedures, a function, and a
31945 variable:
31946
31947 @smallexample @c ada
31948 @group
31949 @cartouche
31950 with Interfaces.C; use Interfaces;
31951 package API is
31952 Count : C.int := 0;
31953 function Factorial (Val : C.int) return C.int;
31954
31955 procedure Initialize_API;
31956 procedure Finalize_API;
31957 -- Initialization & Finalization routines. More in the next section.
31958 private
31959 pragma Export (C, Initialize_API);
31960 pragma Export (C, Finalize_API);
31961 pragma Export (C, Count);
31962 pragma Export (C, Factorial);
31963 end API;
31964 @end cartouche
31965 @end group
31966 @end smallexample
31967
31968 @smallexample @c ada
31969 @group
31970 @cartouche
31971 package body API is
31972 function Factorial (Val : C.int) return C.int is
31973 Fact : C.int := 1;
31974 begin
31975 Count := Count + 1;
31976 for K in 1 .. Val loop
31977 Fact := Fact * K;
31978 end loop;
31979 return Fact;
31980 end Factorial;
31981
31982 procedure Initialize_API is
31983 procedure Adainit;
31984 pragma Import (C, Adainit);
31985 begin
31986 Adainit;
31987 end Initialize_API;
31988
31989 procedure Finalize_API is
31990 procedure Adafinal;
31991 pragma Import (C, Adafinal);
31992 begin
31993 Adafinal;
31994 end Finalize_API;
31995 end API;
31996 @end cartouche
31997 @end group
31998 @end smallexample
31999
32000 @noindent
32001 If the Ada DLL you are building will only be used by Ada applications
32002 you do not have to export Ada entities with a @code{C} or @code{Stdcall}
32003 convention. As an example, the previous package could be written as
32004 follows:
32005
32006 @smallexample @c ada
32007 @group
32008 @cartouche
32009 package API is
32010 Count : Integer := 0;
32011 function Factorial (Val : Integer) return Integer;
32012
32013 procedure Initialize_API;
32014 procedure Finalize_API;
32015 -- Initialization and Finalization routines.
32016 end API;
32017 @end cartouche
32018 @end group
32019 @end smallexample
32020
32021 @smallexample @c ada
32022 @group
32023 @cartouche
32024 package body API is
32025 function Factorial (Val : Integer) return Integer is
32026 Fact : Integer := 1;
32027 begin
32028 Count := Count + 1;
32029 for K in 1 .. Val loop
32030 Fact := Fact * K;
32031 end loop;
32032 return Fact;
32033 end Factorial;
32034
32035 @dots{}
32036 -- The remainder of this package body is unchanged.
32037 end API;
32038 @end cartouche
32039 @end group
32040 @end smallexample
32041
32042 @noindent
32043 Note that if you do not export the Ada entities with a @code{C} or
32044 @code{Stdcall} convention you will have to provide the mangled Ada names
32045 in the definition file of the Ada DLL
32046 (@pxref{Creating the Definition File}).
32047
32048 @node Ada DLLs and Elaboration
32049 @subsection Ada DLLs and Elaboration
32050 @cindex DLLs and elaboration
32051
32052 @noindent
32053 The DLL that you are building contains your Ada code as well as all the
32054 routines in the Ada library that are needed by it. The first thing a
32055 user of your DLL must do is elaborate the Ada code
32056 (@pxref{Elaboration Order Handling in GNAT}).
32057
32058 To achieve this you must export an initialization routine
32059 (@code{Initialize_API} in the previous example), which must be invoked
32060 before using any of the DLL services. This elaboration routine must call
32061 the Ada elaboration routine @code{adainit} generated by the GNAT binder
32062 (@pxref{Binding with Non-Ada Main Programs}). See the body of
32063 @code{Initialize_Api} for an example. Note that the GNAT binder is
32064 automatically invoked during the DLL build process by the @code{gnatdll}
32065 tool (@pxref{Using gnatdll}).
32066
32067 When a DLL is loaded, Windows systematically invokes a routine called
32068 @code{DllMain}. It would therefore be possible to call @code{adainit}
32069 directly from @code{DllMain} without having to provide an explicit
32070 initialization routine. Unfortunately, it is not possible to call
32071 @code{adainit} from the @code{DllMain} if your program has library level
32072 tasks because access to the @code{DllMain} entry point is serialized by
32073 the system (that is, only a single thread can execute ``through'' it at a
32074 time), which means that the GNAT run time will deadlock waiting for the
32075 newly created task to complete its initialization.
32076
32077 @node Ada DLLs and Finalization
32078 @subsection Ada DLLs and Finalization
32079 @cindex DLLs and finalization
32080
32081 @noindent
32082 When the services of an Ada DLL are no longer needed, the client code should
32083 invoke the DLL finalization routine, if available. The DLL finalization
32084 routine is in charge of releasing all resources acquired by the DLL. In the
32085 case of the Ada code contained in the DLL, this is achieved by calling
32086 routine @code{adafinal} generated by the GNAT binder
32087 (@pxref{Binding with Non-Ada Main Programs}).
32088 See the body of @code{Finalize_Api} for an
32089 example. As already pointed out the GNAT binder is automatically invoked
32090 during the DLL build process by the @code{gnatdll} tool
32091 (@pxref{Using gnatdll}).
32092
32093 @node Creating a Spec for Ada DLLs
32094 @subsection Creating a Spec for Ada DLLs
32095
32096 @noindent
32097 To use the services exported by the Ada DLL from another programming
32098 language (e.g.@: C), you have to translate the specs of the exported Ada
32099 entities in that language. For instance in the case of @code{API.dll},
32100 the corresponding C header file could look like:
32101
32102 @smallexample
32103 @group
32104 @cartouche
32105 extern int *_imp__count;
32106 #define count (*_imp__count)
32107 int factorial (int);
32108 @end cartouche
32109 @end group
32110 @end smallexample
32111
32112 @noindent
32113 It is important to understand that when building an Ada DLL to be used by
32114 other Ada applications, you need two different specs for the packages
32115 contained in the DLL: one for building the DLL and the other for using
32116 the DLL. This is because the @code{DLL} calling convention is needed to
32117 use a variable defined in a DLL, but when building the DLL, the variable
32118 must have either the @code{Ada} or @code{C} calling convention. As an
32119 example consider a DLL comprising the following package @code{API}:
32120
32121 @smallexample @c ada
32122 @group
32123 @cartouche
32124 package API is
32125 Count : Integer := 0;
32126 @dots{}
32127 -- Remainder of the package omitted.
32128 end API;
32129 @end cartouche
32130 @end group
32131 @end smallexample
32132
32133 @noindent
32134 After producing a DLL containing package @code{API}, the spec that
32135 must be used to import @code{API.Count} from Ada code outside of the
32136 DLL is:
32137
32138 @smallexample @c ada
32139 @group
32140 @cartouche
32141 package API is
32142 Count : Integer;
32143 pragma Import (DLL, Count);
32144 end API;
32145 @end cartouche
32146 @end group
32147 @end smallexample
32148
32149 @node Creating the Definition File
32150 @subsection Creating the Definition File
32151
32152 @noindent
32153 The definition file is the last file needed to build the DLL. It lists
32154 the exported symbols. As an example, the definition file for a DLL
32155 containing only package @code{API} (where all the entities are exported
32156 with a @code{C} calling convention) is:
32157
32158 @smallexample
32159 @group
32160 @cartouche
32161 EXPORTS
32162 count
32163 factorial
32164 finalize_api
32165 initialize_api
32166 @end cartouche
32167 @end group
32168 @end smallexample
32169
32170 @noindent
32171 If the @code{C} calling convention is missing from package @code{API},
32172 then the definition file contains the mangled Ada names of the above
32173 entities, which in this case are:
32174
32175 @smallexample
32176 @group
32177 @cartouche
32178 EXPORTS
32179 api__count
32180 api__factorial
32181 api__finalize_api
32182 api__initialize_api
32183 @end cartouche
32184 @end group
32185 @end smallexample
32186
32187 @node Using gnatdll
32188 @subsection Using @code{gnatdll}
32189 @findex gnatdll
32190
32191 @menu
32192 * gnatdll Example::
32193 * gnatdll behind the Scenes::
32194 * Using dlltool::
32195 @end menu
32196
32197 @noindent
32198 @code{gnatdll} is a tool to automate the DLL build process once all the Ada
32199 and non-Ada sources that make up your DLL have been compiled.
32200 @code{gnatdll} is actually in charge of two distinct tasks: build the
32201 static import library for the DLL and the actual DLL. The form of the
32202 @code{gnatdll} command is
32203
32204 @smallexample
32205 @cartouche
32206 $ gnatdll @ovar{switches} @var{list-of-files} @r{[}-largs @var{opts}@r{]}
32207 @end cartouche
32208 @end smallexample
32209
32210 @noindent
32211 where @var{list-of-files} is a list of ALI and object files. The object
32212 file list must be the exact list of objects corresponding to the non-Ada
32213 sources whose services are to be included in the DLL. The ALI file list
32214 must be the exact list of ALI files for the corresponding Ada sources
32215 whose services are to be included in the DLL. If @var{list-of-files} is
32216 missing, only the static import library is generated.
32217
32218 @noindent
32219 You may specify any of the following switches to @code{gnatdll}:
32220
32221 @table @code
32222 @item -a@ovar{address}
32223 @cindex @option{-a} (@code{gnatdll})
32224 Build a non-relocatable DLL at @var{address}. If @var{address} is not
32225 specified the default address @var{0x11000000} will be used. By default,
32226 when this switch is missing, @code{gnatdll} builds relocatable DLL. We
32227 advise the reader to build relocatable DLL.
32228
32229 @item -b @var{address}
32230 @cindex @option{-b} (@code{gnatdll})
32231 Set the relocatable DLL base address. By default the address is
32232 @code{0x11000000}.
32233
32234 @item -bargs @var{opts}
32235 @cindex @option{-bargs} (@code{gnatdll})
32236 Binder options. Pass @var{opts} to the binder.
32237
32238 @item -d @var{dllfile}
32239 @cindex @option{-d} (@code{gnatdll})
32240 @var{dllfile} is the name of the DLL. This switch must be present for
32241 @code{gnatdll} to do anything. The name of the generated import library is
32242 obtained algorithmically from @var{dllfile} as shown in the following
32243 example: if @var{dllfile} is @code{xyz.dll}, the import library name is
32244 @code{libxyz.dll.a}. The name of the definition file to use (if not specified
32245 by option @option{-e}) is obtained algorithmically from @var{dllfile}
32246 as shown in the following example:
32247 if @var{dllfile} is @code{xyz.dll}, the definition
32248 file used is @code{xyz.def}.
32249
32250 @item -e @var{deffile}
32251 @cindex @option{-e} (@code{gnatdll})
32252 @var{deffile} is the name of the definition file.
32253
32254 @item -g
32255 @cindex @option{-g} (@code{gnatdll})
32256 Generate debugging information. This information is stored in the object
32257 file and copied from there to the final DLL file by the linker,
32258 where it can be read by the debugger. You must use the
32259 @option{-g} switch if you plan on using the debugger or the symbolic
32260 stack traceback.
32261
32262 @item -h
32263 @cindex @option{-h} (@code{gnatdll})
32264 Help mode. Displays @code{gnatdll} switch usage information.
32265
32266 @item -Idir
32267 @cindex @option{-I} (@code{gnatdll})
32268 Direct @code{gnatdll} to search the @var{dir} directory for source and
32269 object files needed to build the DLL.
32270 (@pxref{Search Paths and the Run-Time Library (RTL)}).
32271
32272 @item -k
32273 @cindex @option{-k} (@code{gnatdll})
32274 Removes the @code{@@}@var{nn} suffix from the import library's exported
32275 names, but keeps them for the link names. You must specify this
32276 option if you want to use a @code{Stdcall} function in a DLL for which
32277 the @code{@@}@var{nn} suffix has been removed. This is the case for most
32278 of the Windows NT DLL for example. This option has no effect when
32279 @option{-n} option is specified.
32280
32281 @item -l @var{file}
32282 @cindex @option{-l} (@code{gnatdll})
32283 The list of ALI and object files used to build the DLL are listed in
32284 @var{file}, instead of being given in the command line. Each line in
32285 @var{file} contains the name of an ALI or object file.
32286
32287 @item -n
32288 @cindex @option{-n} (@code{gnatdll})
32289 No Import. Do not create the import library.
32290
32291 @item -q
32292 @cindex @option{-q} (@code{gnatdll})
32293 Quiet mode. Do not display unnecessary messages.
32294
32295 @item -v
32296 @cindex @option{-v} (@code{gnatdll})
32297 Verbose mode. Display extra information.
32298
32299 @item -largs @var{opts}
32300 @cindex @option{-largs} (@code{gnatdll})
32301 Linker options. Pass @var{opts} to the linker.
32302 @end table
32303
32304 @node gnatdll Example
32305 @subsubsection @code{gnatdll} Example
32306
32307 @noindent
32308 As an example the command to build a relocatable DLL from @file{api.adb}
32309 once @file{api.adb} has been compiled and @file{api.def} created is
32310
32311 @smallexample
32312 $ gnatdll -d api.dll api.ali
32313 @end smallexample
32314
32315 @noindent
32316 The above command creates two files: @file{libapi.dll.a} (the import
32317 library) and @file{api.dll} (the actual DLL). If you want to create
32318 only the DLL, just type:
32319
32320 @smallexample
32321 $ gnatdll -d api.dll -n api.ali
32322 @end smallexample
32323
32324 @noindent
32325 Alternatively if you want to create just the import library, type:
32326
32327 @smallexample
32328 $ gnatdll -d api.dll
32329 @end smallexample
32330
32331 @node gnatdll behind the Scenes
32332 @subsubsection @code{gnatdll} behind the Scenes
32333
32334 @noindent
32335 This section details the steps involved in creating a DLL. @code{gnatdll}
32336 does these steps for you. Unless you are interested in understanding what
32337 goes on behind the scenes, you should skip this section.
32338
32339 We use the previous example of a DLL containing the Ada package @code{API},
32340 to illustrate the steps necessary to build a DLL. The starting point is a
32341 set of objects that will make up the DLL and the corresponding ALI
32342 files. In the case of this example this means that @file{api.o} and
32343 @file{api.ali} are available. To build a relocatable DLL, @code{gnatdll} does
32344 the following:
32345
32346 @enumerate
32347 @item
32348 @code{gnatdll} builds the base file (@file{api.base}). A base file gives
32349 the information necessary to generate relocation information for the
32350 DLL.
32351
32352 @smallexample
32353 @group
32354 $ gnatbind -n api
32355 $ gnatlink api -o api.jnk -mdll -Wl,--base-file,api.base
32356 @end group
32357 @end smallexample
32358
32359 @noindent
32360 In addition to the base file, the @command{gnatlink} command generates an
32361 output file @file{api.jnk} which can be discarded. The @option{-mdll} switch
32362 asks @command{gnatlink} to generate the routines @code{DllMain} and
32363 @code{DllMainCRTStartup} that are called by the Windows loader when the DLL
32364 is loaded into memory.
32365
32366 @item
32367 @code{gnatdll} uses @code{dlltool} (@pxref{Using dlltool}) to build the
32368 export table (@file{api.exp}). The export table contains the relocation
32369 information in a form which can be used during the final link to ensure
32370 that the Windows loader is able to place the DLL anywhere in memory.
32371
32372 @smallexample
32373 @group
32374 $ dlltool --dllname api.dll --def api.def --base-file api.base \
32375 --output-exp api.exp
32376 @end group
32377 @end smallexample
32378
32379 @item
32380 @code{gnatdll} builds the base file using the new export table. Note that
32381 @command{gnatbind} must be called once again since the binder generated file
32382 has been deleted during the previous call to @command{gnatlink}.
32383
32384 @smallexample
32385 @group
32386 $ gnatbind -n api
32387 $ gnatlink api -o api.jnk api.exp -mdll
32388 -Wl,--base-file,api.base
32389 @end group
32390 @end smallexample
32391
32392 @item
32393 @code{gnatdll} builds the new export table using the new base file and
32394 generates the DLL import library @file{libAPI.dll.a}.
32395
32396 @smallexample
32397 @group
32398 $ dlltool --dllname api.dll --def api.def --base-file api.base \
32399 --output-exp api.exp --output-lib libAPI.a
32400 @end group
32401 @end smallexample
32402
32403 @item
32404 Finally @code{gnatdll} builds the relocatable DLL using the final export
32405 table.
32406
32407 @smallexample
32408 @group
32409 $ gnatbind -n api
32410 $ gnatlink api api.exp -o api.dll -mdll
32411 @end group
32412 @end smallexample
32413 @end enumerate
32414
32415 @node Using dlltool
32416 @subsubsection Using @code{dlltool}
32417
32418 @noindent
32419 @code{dlltool} is the low-level tool used by @code{gnatdll} to build
32420 DLLs and static import libraries. This section summarizes the most
32421 common @code{dlltool} switches. The form of the @code{dlltool} command
32422 is
32423
32424 @smallexample
32425 $ dlltool @ovar{switches}
32426 @end smallexample
32427
32428 @noindent
32429 @code{dlltool} switches include:
32430
32431 @table @option
32432 @item --base-file @var{basefile}
32433 @cindex @option{--base-file} (@command{dlltool})
32434 Read the base file @var{basefile} generated by the linker. This switch
32435 is used to create a relocatable DLL.
32436
32437 @item --def @var{deffile}
32438 @cindex @option{--def} (@command{dlltool})
32439 Read the definition file.
32440
32441 @item --dllname @var{name}
32442 @cindex @option{--dllname} (@command{dlltool})
32443 Gives the name of the DLL. This switch is used to embed the name of the
32444 DLL in the static import library generated by @code{dlltool} with switch
32445 @option{--output-lib}.
32446
32447 @item -k
32448 @cindex @option{-k} (@command{dlltool})
32449 Kill @code{@@}@var{nn} from exported names
32450 (@pxref{Windows Calling Conventions}
32451 for a discussion about @code{Stdcall}-style symbols.
32452
32453 @item --help
32454 @cindex @option{--help} (@command{dlltool})
32455 Prints the @code{dlltool} switches with a concise description.
32456
32457 @item --output-exp @var{exportfile}
32458 @cindex @option{--output-exp} (@command{dlltool})
32459 Generate an export file @var{exportfile}. The export file contains the
32460 export table (list of symbols in the DLL) and is used to create the DLL.
32461
32462 @item --output-lib @var{libfile}
32463 @cindex @option{--output-lib} (@command{dlltool})
32464 Generate a static import library @var{libfile}.
32465
32466 @item -v
32467 @cindex @option{-v} (@command{dlltool})
32468 Verbose mode.
32469
32470 @item --as @var{assembler-name}
32471 @cindex @option{--as} (@command{dlltool})
32472 Use @var{assembler-name} as the assembler. The default is @code{as}.
32473 @end table
32474
32475 @node GNAT and Windows Resources
32476 @section GNAT and Windows Resources
32477 @cindex Resources, windows
32478
32479 @menu
32480 * Building Resources::
32481 * Compiling Resources::
32482 * Using Resources::
32483 @end menu
32484
32485 @noindent
32486 Resources are an easy way to add Windows specific objects to your
32487 application. The objects that can be added as resources include:
32488
32489 @itemize @bullet
32490 @item
32491 menus
32492
32493 @item
32494 accelerators
32495
32496 @item
32497 dialog boxes
32498
32499 @item
32500 string tables
32501
32502 @item
32503 bitmaps
32504
32505 @item
32506 cursors
32507
32508 @item
32509 icons
32510
32511 @item
32512 fonts
32513 @end itemize
32514
32515 @noindent
32516 This section explains how to build, compile and use resources.
32517
32518 @node Building Resources
32519 @subsection Building Resources
32520 @cindex Resources, building
32521
32522 @noindent
32523 A resource file is an ASCII file. By convention resource files have an
32524 @file{.rc} extension.
32525 The easiest way to build a resource file is to use Microsoft tools
32526 such as @code{imagedit.exe} to build bitmaps, icons and cursors and
32527 @code{dlgedit.exe} to build dialogs.
32528 It is always possible to build an @file{.rc} file yourself by writing a
32529 resource script.
32530
32531 It is not our objective to explain how to write a resource file. A
32532 complete description of the resource script language can be found in the
32533 Microsoft documentation.
32534
32535 @node Compiling Resources
32536 @subsection Compiling Resources
32537 @findex rc
32538 @findex windres
32539 @cindex Resources, compiling
32540
32541 @noindent
32542 This section describes how to build a GNAT-compatible (COFF) object file
32543 containing the resources. This is done using the Resource Compiler
32544 @code{windres} as follows:
32545
32546 @smallexample
32547 $ windres -i myres.rc -o myres.o
32548 @end smallexample
32549
32550 @noindent
32551 By default @code{windres} will run @command{gcc} to preprocess the @file{.rc}
32552 file. You can specify an alternate preprocessor (usually named
32553 @file{cpp.exe}) using the @code{windres} @option{--preprocessor}
32554 parameter. A list of all possible options may be obtained by entering
32555 the command @code{windres} @option{--help}.
32556
32557 It is also possible to use the Microsoft resource compiler @code{rc.exe}
32558 to produce a @file{.res} file (binary resource file). See the
32559 corresponding Microsoft documentation for further details. In this case
32560 you need to use @code{windres} to translate the @file{.res} file to a
32561 GNAT-compatible object file as follows:
32562
32563 @smallexample
32564 $ windres -i myres.res -o myres.o
32565 @end smallexample
32566
32567 @node Using Resources
32568 @subsection Using Resources
32569 @cindex Resources, using
32570
32571 @noindent
32572 To include the resource file in your program just add the
32573 GNAT-compatible object file for the resource(s) to the linker
32574 arguments. With @command{gnatmake} this is done by using the @option{-largs}
32575 option:
32576
32577 @smallexample
32578 $ gnatmake myprog -largs myres.o
32579 @end smallexample
32580
32581 @node Debugging a DLL
32582 @section Debugging a DLL
32583 @cindex DLL debugging
32584
32585 @menu
32586 * Program and DLL Both Built with GCC/GNAT::
32587 * Program Built with Foreign Tools and DLL Built with GCC/GNAT::
32588 @end menu
32589
32590 @noindent
32591 Debugging a DLL is similar to debugging a standard program. But
32592 we have to deal with two different executable parts: the DLL and the
32593 program that uses it. We have the following four possibilities:
32594
32595 @enumerate 1
32596 @item
32597 The program and the DLL are built with @code{GCC/GNAT}.
32598 @item
32599 The program is built with foreign tools and the DLL is built with
32600 @code{GCC/GNAT}.
32601 @item
32602 The program is built with @code{GCC/GNAT} and the DLL is built with
32603 foreign tools.
32604 @item
32605 @end enumerate
32606
32607 @noindent
32608 In this section we address only cases one and two above.
32609 There is no point in trying to debug
32610 a DLL with @code{GNU/GDB}, if there is no GDB-compatible debugging
32611 information in it. To do so you must use a debugger compatible with the
32612 tools suite used to build the DLL.
32613
32614 @node Program and DLL Both Built with GCC/GNAT
32615 @subsection Program and DLL Both Built with GCC/GNAT
32616
32617 @noindent
32618 This is the simplest case. Both the DLL and the program have @code{GDB}
32619 compatible debugging information. It is then possible to break anywhere in
32620 the process. Let's suppose here that the main procedure is named
32621 @code{ada_main} and that in the DLL there is an entry point named
32622 @code{ada_dll}.
32623
32624 @noindent
32625 The DLL (@pxref{Introduction to Dynamic Link Libraries (DLLs)}) and
32626 program must have been built with the debugging information (see GNAT -g
32627 switch). Here are the step-by-step instructions for debugging it:
32628
32629 @enumerate 1
32630 @item Launch @code{GDB} on the main program.
32631
32632 @smallexample
32633 $ gdb -nw ada_main
32634 @end smallexample
32635
32636 @item Start the program and stop at the beginning of the main procedure
32637
32638 @smallexample
32639 (gdb) start
32640 @end smallexample
32641
32642 @noindent
32643 This step is required to be able to set a breakpoint inside the DLL. As long
32644 as the program is not run, the DLL is not loaded. This has the
32645 consequence that the DLL debugging information is also not loaded, so it is not
32646 possible to set a breakpoint in the DLL.
32647
32648 @item Set a breakpoint inside the DLL
32649
32650 @smallexample
32651 (gdb) break ada_dll
32652 (gdb) cont
32653 @end smallexample
32654
32655 @end enumerate
32656
32657 @noindent
32658 At this stage a breakpoint is set inside the DLL. From there on
32659 you can use the standard approach to debug the whole program
32660 (@pxref{Running and Debugging Ada Programs}).
32661
32662 @ignore
32663 @c This used to work, probably because the DLLs were non-relocatable
32664 @c keep this section around until the problem is sorted out.
32665
32666 To break on the @code{DllMain} routine it is not possible to follow
32667 the procedure above. At the time the program stop on @code{ada_main}
32668 the @code{DllMain} routine as already been called. Either you can use
32669 the procedure below @pxref{Debugging the DLL Directly} or this procedure:
32670
32671 @enumerate 1
32672 @item Launch @code{GDB} on the main program.
32673
32674 @smallexample
32675 $ gdb ada_main
32676 @end smallexample
32677
32678 @item Load DLL symbols
32679
32680 @smallexample
32681 (gdb) add-sym api.dll
32682 @end smallexample
32683
32684 @item Set a breakpoint inside the DLL
32685
32686 @smallexample
32687 (gdb) break ada_dll.adb:45
32688 @end smallexample
32689
32690 Note that at this point it is not possible to break using the routine symbol
32691 directly as the program is not yet running. The solution is to break
32692 on the proper line (break in @file{ada_dll.adb} line 45).
32693
32694 @item Start the program
32695
32696 @smallexample
32697 (gdb) run
32698 @end smallexample
32699
32700 @end enumerate
32701 @end ignore
32702
32703 @node Program Built with Foreign Tools and DLL Built with GCC/GNAT
32704 @subsection Program Built with Foreign Tools and DLL Built with GCC/GNAT
32705
32706 @menu
32707 * Debugging the DLL Directly::
32708 * Attaching to a Running Process::
32709 @end menu
32710
32711 @noindent
32712 In this case things are slightly more complex because it is not possible to
32713 start the main program and then break at the beginning to load the DLL and the
32714 associated DLL debugging information. It is not possible to break at the
32715 beginning of the program because there is no @code{GDB} debugging information,
32716 and therefore there is no direct way of getting initial control. This
32717 section addresses this issue by describing some methods that can be used
32718 to break somewhere in the DLL to debug it.
32719
32720 @noindent
32721 First suppose that the main procedure is named @code{main} (this is for
32722 example some C code built with Microsoft Visual C) and that there is a
32723 DLL named @code{test.dll} containing an Ada entry point named
32724 @code{ada_dll}.
32725
32726 @noindent
32727 The DLL (@pxref{Introduction to Dynamic Link Libraries (DLLs)}) must have
32728 been built with debugging information (see GNAT -g option).
32729
32730 @node Debugging the DLL Directly
32731 @subsubsection Debugging the DLL Directly
32732
32733 @enumerate 1
32734 @item
32735 Find out the executable starting address
32736
32737 @smallexample
32738 $ objdump --file-header main.exe
32739 @end smallexample
32740
32741 The starting address is reported on the last line. For example:
32742
32743 @smallexample
32744 main.exe: file format pei-i386
32745 architecture: i386, flags 0x0000010a:
32746 EXEC_P, HAS_DEBUG, D_PAGED
32747 start address 0x00401010
32748 @end smallexample
32749
32750 @item
32751 Launch the debugger on the executable.
32752
32753 @smallexample
32754 $ gdb main.exe
32755 @end smallexample
32756
32757 @item
32758 Set a breakpoint at the starting address, and launch the program.
32759
32760 @smallexample
32761 $ (gdb) break *0x00401010
32762 $ (gdb) run
32763 @end smallexample
32764
32765 The program will stop at the given address.
32766
32767 @item
32768 Set a breakpoint on a DLL subroutine.
32769
32770 @smallexample
32771 (gdb) break ada_dll.adb:45
32772 @end smallexample
32773
32774 Or if you want to break using a symbol on the DLL, you need first to
32775 select the Ada language (language used by the DLL).
32776
32777 @smallexample
32778 (gdb) set language ada
32779 (gdb) break ada_dll
32780 @end smallexample
32781
32782 @item
32783 Continue the program.
32784
32785 @smallexample
32786 (gdb) cont
32787 @end smallexample
32788
32789 @noindent
32790 This will run the program until it reaches the breakpoint that has been
32791 set. From that point you can use the standard way to debug a program
32792 as described in (@pxref{Running and Debugging Ada Programs}).
32793
32794 @end enumerate
32795
32796 @noindent
32797 It is also possible to debug the DLL by attaching to a running process.
32798
32799 @node Attaching to a Running Process
32800 @subsubsection Attaching to a Running Process
32801 @cindex DLL debugging, attach to process
32802
32803 @noindent
32804 With @code{GDB} it is always possible to debug a running process by
32805 attaching to it. It is possible to debug a DLL this way. The limitation
32806 of this approach is that the DLL must run long enough to perform the
32807 attach operation. It may be useful for instance to insert a time wasting
32808 loop in the code of the DLL to meet this criterion.
32809
32810 @enumerate 1
32811
32812 @item Launch the main program @file{main.exe}.
32813
32814 @smallexample
32815 $ main
32816 @end smallexample
32817
32818 @item Use the Windows @i{Task Manager} to find the process ID. Let's say
32819 that the process PID for @file{main.exe} is 208.
32820
32821 @item Launch gdb.
32822
32823 @smallexample
32824 $ gdb
32825 @end smallexample
32826
32827 @item Attach to the running process to be debugged.
32828
32829 @smallexample
32830 (gdb) attach 208
32831 @end smallexample
32832
32833 @item Load the process debugging information.
32834
32835 @smallexample
32836 (gdb) symbol-file main.exe
32837 @end smallexample
32838
32839 @item Break somewhere in the DLL.
32840
32841 @smallexample
32842 (gdb) break ada_dll
32843 @end smallexample
32844
32845 @item Continue process execution.
32846
32847 @smallexample
32848 (gdb) cont
32849 @end smallexample
32850
32851 @end enumerate
32852
32853 @noindent
32854 This last step will resume the process execution, and stop at
32855 the breakpoint we have set. From there you can use the standard
32856 approach to debug a program as described in
32857 (@pxref{Running and Debugging Ada Programs}).
32858
32859 @node Setting Stack Size from gnatlink
32860 @section Setting Stack Size from @command{gnatlink}
32861
32862 @noindent
32863 It is possible to specify the program stack size at link time. On modern
32864 versions of Windows, starting with XP, this is mostly useful to set the size of
32865 the main stack (environment task). The other task stacks are set with pragma
32866 Storage_Size or with the @command{gnatbind -d} command.
32867
32868 Since older versions of Windows (2000, NT4, etc.) do not allow setting the
32869 reserve size of individual tasks, the link-time stack size applies to all
32870 tasks, and pragma Storage_Size has no effect.
32871 In particular, Stack Overflow checks are made against this
32872 link-time specified size.
32873
32874 This setting can be done with
32875 @command{gnatlink} using either:
32876
32877 @itemize @bullet
32878
32879 @item using @option{-Xlinker} linker option
32880
32881 @smallexample
32882 $ gnatlink hello -Xlinker --stack=0x10000,0x1000
32883 @end smallexample
32884
32885 This sets the stack reserve size to 0x10000 bytes and the stack commit
32886 size to 0x1000 bytes.
32887
32888 @item using @option{-Wl} linker option
32889
32890 @smallexample
32891 $ gnatlink hello -Wl,--stack=0x1000000
32892 @end smallexample
32893
32894 This sets the stack reserve size to 0x1000000 bytes. Note that with
32895 @option{-Wl} option it is not possible to set the stack commit size
32896 because the coma is a separator for this option.
32897
32898 @end itemize
32899
32900 @node Setting Heap Size from gnatlink
32901 @section Setting Heap Size from @command{gnatlink}
32902
32903 @noindent
32904 Under Windows systems, it is possible to specify the program heap size from
32905 @command{gnatlink} using either:
32906
32907 @itemize @bullet
32908
32909 @item using @option{-Xlinker} linker option
32910
32911 @smallexample
32912 $ gnatlink hello -Xlinker --heap=0x10000,0x1000
32913 @end smallexample
32914
32915 This sets the heap reserve size to 0x10000 bytes and the heap commit
32916 size to 0x1000 bytes.
32917
32918 @item using @option{-Wl} linker option
32919
32920 @smallexample
32921 $ gnatlink hello -Wl,--heap=0x1000000
32922 @end smallexample
32923
32924 This sets the heap reserve size to 0x1000000 bytes. Note that with
32925 @option{-Wl} option it is not possible to set the heap commit size
32926 because the coma is a separator for this option.
32927
32928 @end itemize
32929
32930 @end ifset
32931
32932 @c **********************************
32933 @c * GNU Free Documentation License *
32934 @c **********************************
32935 @include fdl.texi
32936 @c GNU Free Documentation License
32937
32938 @node Index,,GNU Free Documentation License, Top
32939 @unnumbered Index
32940
32941 @printindex cp
32942
32943 @contents
32944 @c Put table of contents at end, otherwise it precedes the "title page" in
32945 @c the .txt version
32946 @c Edit the pdf file to move the contents to the beginning, after the title
32947 @c page
32948
32949 @bye