gnat_ugn.texi: Minor updates.
[gcc.git] / gcc / ada / gnat_ugn.texi
1 \input texinfo @c -*-texinfo-*-
2 @c %**start of header
3
4 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
5 @c o
6 @c GNAT DOCUMENTATION o
7 @c o
8 @c G N A T _ U G N o
9 @c o
10 @c GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). o
11 @c o
12 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
13
14 @setfilename gnat_ugn.info
15
16 @copying
17 Copyright @copyright{} 1995-2009 Free Software Foundation,
18 Inc.
19
20 Permission is granted to copy, distribute and/or modify this document
21 under the terms of the GNU Free Documentation License, Version 1.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++ constructors::
2980 * Interfacing with C++ at the Class Level::
2981 @end menu
2982
2983 @node Interfacing to C++
2984 @subsection Interfacing to C++
2985
2986 @noindent
2987 GNAT supports interfacing with the G++ compiler (or any C++ compiler
2988 generating code that is compatible with the G++ Application Binary
2989 Interface ---see http://www.codesourcery.com/archives/cxx-abi).
2990
2991 @noindent
2992 Interfacing can be done at 3 levels: simple data, subprograms, and
2993 classes. In the first two cases, GNAT offers a specific @code{Convention
2994 C_Plus_Plus} (or @code{CPP}) that behaves exactly like @code{Convention C}.
2995 Usually, C++ mangles the names of subprograms. To generate proper mangled
2996 names automatically, see @ref{Generating Ada Bindings for C and C++ headers}).
2997 This problem can also be addressed manually in two ways:
2998
2999 @itemize @bullet
3000 @item
3001 by modifying the C++ code in order to force a C convention using
3002 the @code{extern "C"} syntax.
3003
3004 @item
3005 by figuring out the mangled name (using e.g. @command{nm}) and using it as the
3006 Link_Name argument of the pragma import.
3007 @end itemize
3008
3009 @noindent
3010 Interfacing at the class level can be achieved by using the GNAT specific
3011 pragmas such as @code{CPP_Constructor}. @xref{Interfacing to C++,,,
3012 gnat_rm, GNAT Reference Manual}, for additional information.
3013
3014 @node Linking a Mixed C++ & Ada Program
3015 @subsection Linking a Mixed C++ & Ada Program
3016
3017 @noindent
3018 Usually the linker of the C++ development system must be used to link
3019 mixed applications because most C++ systems will resolve elaboration
3020 issues (such as calling constructors on global class instances)
3021 transparently during the link phase. GNAT has been adapted to ease the
3022 use of a foreign linker for the last phase. Three cases can be
3023 considered:
3024 @enumerate
3025
3026 @item
3027 Using GNAT and G++ (GNU C++ compiler) from the same GCC installation:
3028 The C++ linker can simply be called by using the C++ specific driver
3029 called @code{g++}.
3030
3031 Note that if the C++ code uses inline functions, you will need to
3032 compile your C++ code with the @code{-fkeep-inline-functions} switch in
3033 order to provide an existing function implementation that the Ada code can
3034 link with.
3035
3036 @smallexample
3037 $ g++ -c -fkeep-inline-functions file1.C
3038 $ g++ -c -fkeep-inline-functions file2.C
3039 $ gnatmake ada_unit -largs file1.o file2.o --LINK=g++
3040 @end smallexample
3041
3042 @item
3043 Using GNAT and G++ from two different GCC installations: If both
3044 compilers are on the @env{PATH}, the previous method may be used. It is
3045 important to note that environment variables such as
3046 @env{C_INCLUDE_PATH}, @env{GCC_EXEC_PREFIX}, @env{BINUTILS_ROOT}, and
3047 @env{GCC_ROOT} will affect both compilers
3048 at the same time and may make one of the two compilers operate
3049 improperly if set during invocation of the wrong compiler. It is also
3050 very important that the linker uses the proper @file{libgcc.a} GCC
3051 library -- that is, the one from the C++ compiler installation. The
3052 implicit link command as suggested in the @command{gnatmake} command
3053 from the former example can be replaced by an explicit link command with
3054 the full-verbosity option in order to verify which library is used:
3055 @smallexample
3056 $ gnatbind ada_unit
3057 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=c++
3058 @end smallexample
3059 If there is a problem due to interfering environment variables, it can
3060 be worked around by using an intermediate script. The following example
3061 shows the proper script to use when GNAT has not been installed at its
3062 default location and g++ has been installed at its default location:
3063
3064 @smallexample
3065 $ cat ./my_script
3066 #!/bin/sh
3067 unset BINUTILS_ROOT
3068 unset GCC_ROOT
3069 c++ $*
3070 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=./my_script
3071 @end smallexample
3072
3073 @item
3074 Using a non-GNU C++ compiler: The commands previously described can be
3075 used to insure that the C++ linker is used. Nonetheless, you need to add
3076 a few more parameters to the link command line, depending on the exception
3077 mechanism used.
3078
3079 If the @code{setjmp/longjmp} exception mechanism is used, only the paths
3080 to the libgcc libraries are required:
3081
3082 @smallexample
3083 $ cat ./my_script
3084 #!/bin/sh
3085 CC $* `gcc -print-file-name=libgcc.a` `gcc -print-file-name=libgcc_eh.a`
3086 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
3087 @end smallexample
3088
3089 Where CC is the name of the non-GNU C++ compiler.
3090
3091 If the @code{zero cost} exception mechanism is used, and the platform
3092 supports automatic registration of exception tables (e.g.@: Solaris or IRIX),
3093 paths to more objects are required:
3094
3095 @smallexample
3096 $ cat ./my_script
3097 #!/bin/sh
3098 CC `gcc -print-file-name=crtbegin.o` $* \
3099 `gcc -print-file-name=libgcc.a` `gcc -print-file-name=libgcc_eh.a` \
3100 `gcc -print-file-name=crtend.o`
3101 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
3102 @end smallexample
3103
3104 If the @code{zero cost} exception mechanism is used, and the platform
3105 doesn't support automatic registration of exception tables (e.g.@: HP-UX,
3106 Tru64 or AIX), the simple approach described above will not work and
3107 a pre-linking phase using GNAT will be necessary.
3108
3109 @end enumerate
3110
3111 Another alternative is to use the @command{gprbuild} multi-language builder
3112 which has a large knowledge base and knows how to link Ada and C++ code
3113 together automatically in most cases.
3114
3115 @node A Simple Example
3116 @subsection A Simple Example
3117 @noindent
3118 The following example, provided as part of the GNAT examples, shows how
3119 to achieve procedural interfacing between Ada and C++ in both
3120 directions. The C++ class A has two methods. The first method is exported
3121 to Ada by the means of an extern C wrapper function. The second method
3122 calls an Ada subprogram. On the Ada side, The C++ calls are modelled by
3123 a limited record with a layout comparable to the C++ class. The Ada
3124 subprogram, in turn, calls the C++ method. So, starting from the C++
3125 main program, the process passes back and forth between the two
3126 languages.
3127
3128 @noindent
3129 Here are the compilation commands:
3130 @smallexample
3131 $ gnatmake -c simple_cpp_interface
3132 $ g++ -c cpp_main.C
3133 $ g++ -c ex7.C
3134 $ gnatbind -n simple_cpp_interface
3135 $ gnatlink simple_cpp_interface -o cpp_main --LINK=g++
3136 -lstdc++ ex7.o cpp_main.o
3137 @end smallexample
3138
3139 @noindent
3140 Here are the corresponding sources:
3141 @smallexample
3142
3143 //cpp_main.C
3144
3145 #include "ex7.h"
3146
3147 extern "C" @{
3148 void adainit (void);
3149 void adafinal (void);
3150 void method1 (A *t);
3151 @}
3152
3153 void method1 (A *t)
3154 @{
3155 t->method1 ();
3156 @}
3157
3158 int main ()
3159 @{
3160 A obj;
3161 adainit ();
3162 obj.method2 (3030);
3163 adafinal ();
3164 @}
3165
3166 //ex7.h
3167
3168 class Origin @{
3169 public:
3170 int o_value;
3171 @};
3172 class A : public Origin @{
3173 public:
3174 void method1 (void);
3175 void method2 (int v);
3176 A();
3177 int a_value;
3178 @};
3179
3180 //ex7.C
3181
3182 #include "ex7.h"
3183 #include <stdio.h>
3184
3185 extern "C" @{ void ada_method2 (A *t, int v);@}
3186
3187 void A::method1 (void)
3188 @{
3189 a_value = 2020;
3190 printf ("in A::method1, a_value = %d \n",a_value);
3191
3192 @}
3193
3194 void A::method2 (int v)
3195 @{
3196 ada_method2 (this, v);
3197 printf ("in A::method2, a_value = %d \n",a_value);
3198
3199 @}
3200
3201 A::A(void)
3202 @{
3203 a_value = 1010;
3204 printf ("in A::A, a_value = %d \n",a_value);
3205 @}
3206 @end smallexample
3207
3208 @smallexample @c ada
3209 -- Ada sources
3210 package body Simple_Cpp_Interface is
3211
3212 procedure Ada_Method2 (This : in out A; V : Integer) is
3213 begin
3214 Method1 (This);
3215 This.A_Value := V;
3216 end Ada_Method2;
3217
3218 end Simple_Cpp_Interface;
3219
3220 with System;
3221 package Simple_Cpp_Interface is
3222 type A is limited
3223 record
3224 Vptr : System.Address;
3225 O_Value : Integer;
3226 A_Value : Integer;
3227 end record;
3228 pragma Convention (C, A);
3229
3230 procedure Method1 (This : in out A);
3231 pragma Import (C, Method1);
3232
3233 procedure Ada_Method2 (This : in out A; V : Integer);
3234 pragma Export (C, Ada_Method2);
3235
3236 end Simple_Cpp_Interface;
3237 @end smallexample
3238
3239 @node Interfacing with C++ constructors
3240 @subsection Interfacing with C++ constructors
3241 @noindent
3242
3243 In order to interface with C++ constructors GNAT provides the
3244 @code{pragma CPP_Constructor} (@xref{Interfacing to C++,,,
3245 gnat_rm, GNAT Reference Manual}, for additional information).
3246 In this section we present some common uses of C++ constructors
3247 in mixed-languages programs in GNAT.
3248
3249 Let us assume that we need to interface with the following
3250 C++ class:
3251
3252 @smallexample
3253 @b{class} Root @{
3254 @b{public}:
3255 int a_value;
3256 int b_value;
3257 @b{virtual} int Get_Value ();
3258 Root(); // Default constructor
3259 Root(int v); // 1st non-default constructor
3260 Root(int v, int w); // 2nd non-default constructor
3261 @};
3262 @end smallexample
3263
3264 For this purpose we can write the following package spec (further
3265 information on how to build this spec is available in
3266 @ref{Interfacing with C++ at the Class Level} and
3267 @ref{Generating Ada Bindings for C and C++ headers}).
3268
3269 @smallexample @c ada
3270 with Interfaces.C; use Interfaces.C;
3271 package Pkg_Root is
3272 type Root is tagged limited record
3273 A_Value : int;
3274 B_Value : int;
3275 end record;
3276 pragma Import (CPP, Root);
3277
3278 function Get_Value (Obj : Root) return int;
3279 pragma Import (CPP, Get_Value);
3280
3281 function Constructor return Root;
3282 pragma Cpp_Constructor (Constructor, "_ZN4RootC1Ev");
3283
3284 function Constructor (v : Integer) return Root;
3285 pragma Cpp_Constructor (Constructor, "_ZN4RootC1Ei");
3286
3287 function Constructor (v, w : Integer) return Root;
3288 pragma Cpp_Constructor (Constructor, "_ZN4RootC1Eii");
3289 end Pkg_Root;
3290 @end smallexample
3291
3292 On the Ada side the constructor is represented by a function (whose
3293 name is arbitrary) that returns the classwide type corresponding to
3294 the imported C++ class. Although the constructor is described as a
3295 function, it is typically a procedure with an extra implicit argument
3296 (the object being initialized) at the implementation level. GNAT
3297 issues the appropriate call, whatever it is, to get the object
3298 properly initialized.
3299
3300 Constructors can only appear in the following contexts:
3301
3302 @itemize @bullet
3303 @item
3304 On the right side of an initialization of an object of type @var{T}.
3305 @item
3306 On the right side of an initialization of a record component of type @var{T}.
3307 @item
3308 In an Ada 2005 limited aggregate.
3309 @item
3310 In an Ada 2005 nested limited aggregate.
3311 @item
3312 In an Ada 2005 limited aggregate that initializes an object built in
3313 place by an extended return statement.
3314 @end itemize
3315
3316 @noindent
3317 In a declaration of an object whose type is a class imported from C++,
3318 either the default C++ constructor is implicitly called by GNAT, or
3319 else the required C++ constructor must be explicitly called in the
3320 expression that initializes the object. For example:
3321
3322 @smallexample @c ada
3323 Obj1 : Root;
3324 Obj2 : Root := Constructor;
3325 Obj3 : Root := Constructor (v => 10);
3326 Obj4 : Root := Constructor (30, 40);
3327 @end smallexample
3328
3329 The first two declarations are equivalent: in both cases the default C++
3330 constructor is invoked (in the former case the call to the constructor is
3331 implicit, and in the latter case the call is explicit in the object
3332 declaration). @code{Obj3} is initialized by the C++ non-default constructor
3333 that takes an integer argument, and @code{Obj4} is initialized by the
3334 non-default C++ constructor that takes two integers.
3335
3336 Let us derive the imported C++ class in the Ada side. For example:
3337
3338 @smallexample @c ada
3339 type DT is new Root with record
3340 C_Value : Natural := 2009;
3341 end record;
3342 @end smallexample
3343
3344 In this case the components DT inherited from the C++ side must be
3345 initialized by a C++ constructor, and the additional Ada components
3346 of type DT are initialized by GNAT. The initialization of such an
3347 object is done either by default, or by means of a function returning
3348 an aggregate of type DT, or by means of an extension aggregate.
3349
3350 @smallexample @c ada
3351 Obj5 : DT;
3352 Obj6 : DT := Function_Returning_DT (50);
3353 Obj7 : DT := (Constructor (30,40) with C_Value => 50);
3354 @end smallexample
3355
3356 The declaration of @code{Obj5} invokes the default constructors: the
3357 C++ default constructor of the parent type takes care of the initialization
3358 of the components inherited from Root, and GNAT takes care of the default
3359 initialization of the additional Ada components of type DT (that is,
3360 @code{C_Value} is initialized to value 2009). The order of invocation of
3361 the constructors is consistent with the order of elaboration required by
3362 Ada and C++. That is, the constructor of the parent type is always called
3363 before the constructor of the derived type.
3364
3365 Let us now consider a record that has components whose type is imported
3366 from C++. For example:
3367
3368 @smallexample @c ada
3369 type Rec1 is limited record
3370 Data1 : Root := Constructor (10);
3371 Value : Natural := 1000;
3372 end record;
3373
3374 type Rec2 (D : Integer := 20) is limited record
3375 Rec : Rec1;
3376 Data2 : Root := Constructor (D, 30);
3377 end record;
3378 @end smallexample
3379
3380 The initialization of an object of type @code{Rec2} will call the
3381 non-default C++ constructors specified for the imported components.
3382 For example:
3383
3384 @smallexample @c ada
3385 Obj8 : Rec2 (40);
3386 @end smallexample
3387
3388 Using Ada 2005 we can use limited aggregates to initialize an object
3389 invoking C++ constructors that differ from those specified in the type
3390 declarations. For example:
3391
3392 @smallexample @c ada
3393 Obj9 : Rec2 := (Rec => (Data1 => Constructor (15, 16),
3394 others => <>),
3395 others => <>);
3396 @end smallexample
3397
3398 The above declaration uses an Ada 2005 limited aggregate to
3399 initialize @code{Obj9}, and the C++ constructor that has two integer
3400 arguments is invoked to initialize the @code{Data1} component instead
3401 of the constructor specified in the declaration of type @code{Rec1}. In
3402 Ada 2005 the box in the aggregate indicates that unspecified components
3403 are initialized using the expression (if any) available in the component
3404 declaration. That is, in this case discriminant @code{D} is initialized
3405 to value @code{20}, @code{Value} is initialized to value 1000, and the
3406 non-default C++ constructor that handles two integers takes care of
3407 initializing component @code{Data2} with values @code{20,30}.
3408
3409 In Ada 2005 we can use the extended return statement to build the Ada
3410 equivalent to C++ non-default constructors. For example:
3411
3412 @smallexample @c ada
3413 function Constructor (V : Integer) return Rec2 is
3414 begin
3415 return Obj : Rec2 := (Rec => (Data1 => Constructor (V, 20),
3416 others => <>),
3417 others => <>) do
3418 -- Further actions required for construction of
3419 -- objects of type Rec2
3420 ...
3421 end record;
3422 end Constructor;
3423 @end smallexample
3424
3425 In this example the extended return statement construct is used to
3426 build in place the returned object whose components are initialized
3427 by means of a limited aggregate. Any further action associated with
3428 the constructor can be placed inside the construct.
3429
3430 @node Interfacing with C++ at the Class Level
3431 @subsection Interfacing with C++ at the Class Level
3432 @noindent
3433 In this section we demonstrate the GNAT features for interfacing with
3434 C++ by means of an example making use of Ada 2005 abstract interface
3435 types. This example consists of a classification of animals; classes
3436 have been used to model our main classification of animals, and
3437 interfaces provide support for the management of secondary
3438 classifications. We first demonstrate a case in which the types and
3439 constructors are defined on the C++ side and imported from the Ada
3440 side, and latter the reverse case.
3441
3442 The root of our derivation will be the @code{Animal} class, with a
3443 single private attribute (the @code{Age} of the animal) and two public
3444 primitives to set and get the value of this attribute.
3445
3446 @smallexample
3447 @b{class} Animal @{
3448 @b{public}:
3449 @b{virtual} void Set_Age (int New_Age);
3450 @b{virtual} int Age ();
3451 @b{private}:
3452 int Age_Count;
3453 @};
3454 @end smallexample
3455
3456 Abstract interface types are defined in C++ by means of classes with pure
3457 virtual functions and no data members. In our example we will use two
3458 interfaces that provide support for the common management of @code{Carnivore}
3459 and @code{Domestic} animals:
3460
3461 @smallexample
3462 @b{class} Carnivore @{
3463 @b{public}:
3464 @b{virtual} int Number_Of_Teeth () = 0;
3465 @};
3466
3467 @b{class} Domestic @{
3468 @b{public}:
3469 @b{virtual void} Set_Owner (char* Name) = 0;
3470 @};
3471 @end smallexample
3472
3473 Using these declarations, we can now say that a @code{Dog} is an animal that is
3474 both Carnivore and Domestic, that is:
3475
3476 @smallexample
3477 @b{class} Dog : Animal, Carnivore, Domestic @{
3478 @b{public}:
3479 @b{virtual} int Number_Of_Teeth ();
3480 @b{virtual} void Set_Owner (char* Name);
3481
3482 Dog(); // Constructor
3483 @b{private}:
3484 int Tooth_Count;
3485 char *Owner;
3486 @};
3487 @end smallexample
3488
3489 In the following examples we will assume that the previous declarations are
3490 located in a file named @code{animals.h}. The following package demonstrates
3491 how to import these C++ declarations from the Ada side:
3492
3493 @smallexample @c ada
3494 with Interfaces.C.Strings; use Interfaces.C.Strings;
3495 package Animals is
3496 type Carnivore is interface;
3497 pragma Convention (C_Plus_Plus, Carnivore);
3498 function Number_Of_Teeth (X : Carnivore)
3499 return Natural is abstract;
3500
3501 type Domestic is interface;
3502 pragma Convention (C_Plus_Plus, Set_Owner);
3503 procedure Set_Owner
3504 (X : in out Domestic;
3505 Name : Chars_Ptr) is abstract;
3506
3507 type Animal is tagged record
3508 Age : Natural := 0;
3509 end record;
3510 pragma Import (C_Plus_Plus, Animal);
3511
3512 procedure Set_Age (X : in out Animal; Age : Integer);
3513 pragma Import (C_Plus_Plus, Set_Age);
3514
3515 function Age (X : Animal) return Integer;
3516 pragma Import (C_Plus_Plus, Age);
3517
3518 type Dog is new Animal and Carnivore and Domestic with record
3519 Tooth_Count : Natural;
3520 Owner : String (1 .. 30);
3521 end record;
3522 pragma Import (C_Plus_Plus, Dog);
3523
3524 function Number_Of_Teeth (A : Dog) return Integer;
3525 pragma Import (C_Plus_Plus, Number_Of_Teeth);
3526
3527 procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
3528 pragma Import (C_Plus_Plus, Set_Owner);
3529
3530 function New_Dog return Dog;
3531 pragma CPP_Constructor (New_Dog);
3532 pragma Import (CPP, New_Dog, "_ZN3DogC2Ev");
3533 end Animals;
3534 @end smallexample
3535
3536 Thanks to the compatibility between GNAT run-time structures and the C++ ABI,
3537 interfacing with these C++ classes is easy. The only requirement is that all
3538 the primitives and components must be declared exactly in the same order in
3539 the two languages.
3540
3541 Regarding the abstract interfaces, we must indicate to the GNAT compiler by
3542 means of a @code{pragma Convention (C_Plus_Plus)}, the convention used to pass
3543 the arguments to the called primitives will be the same as for C++. For the
3544 imported classes we use @code{pragma Import} with convention @code{C_Plus_Plus}
3545 to indicate that they have been defined on the C++ side; this is required
3546 because the dispatch table associated with these tagged types will be built
3547 in the C++ side and therefore will not contain the predefined Ada primitives
3548 which Ada would otherwise expect.
3549
3550 As the reader can see there is no need to indicate the C++ mangled names
3551 associated with each subprogram because it is assumed that all the calls to
3552 these primitives will be dispatching calls. The only exception is the
3553 constructor, which must be registered with the compiler by means of
3554 @code{pragma CPP_Constructor} and needs to provide its associated C++
3555 mangled name because the Ada compiler generates direct calls to it.
3556
3557 With the above packages we can now declare objects of type Dog on the Ada side
3558 and dispatch calls to the corresponding subprograms on the C++ side. We can
3559 also extend the tagged type Dog with further fields and primitives, and
3560 override some of its C++ primitives on the Ada side. For example, here we have
3561 a type derivation defined on the Ada side that inherits all the dispatching
3562 primitives of the ancestor from the C++ side.
3563
3564 @smallexample
3565 @b{with} Animals; @b{use} Animals;
3566 @b{package} Vaccinated_Animals @b{is}
3567 @b{type} Vaccinated_Dog @b{is new} Dog @b{with null record};
3568 @b{function} Vaccination_Expired (A : Vaccinated_Dog) @b{return} Boolean;
3569 @b{end} Vaccinated_Animals;
3570 @end smallexample
3571
3572 It is important to note that, because of the ABI compatibility, the programmer
3573 does not need to add any further information to indicate either the object
3574 layout or the dispatch table entry associated with each dispatching operation.
3575
3576 Now let us define all the types and constructors on the Ada side and export
3577 them to C++, using the same hierarchy of our previous example:
3578
3579 @smallexample @c ada
3580 with Interfaces.C.Strings;
3581 use Interfaces.C.Strings;
3582 package Animals is
3583 type Carnivore is interface;
3584 pragma Convention (C_Plus_Plus, Carnivore);
3585 function Number_Of_Teeth (X : Carnivore)
3586 return Natural is abstract;
3587
3588 type Domestic is interface;
3589 pragma Convention (C_Plus_Plus, Set_Owner);
3590 procedure Set_Owner
3591 (X : in out Domestic;
3592 Name : Chars_Ptr) is abstract;
3593
3594 type Animal is tagged record
3595 Age : Natural := 0;
3596 end record;
3597 pragma Convention (C_Plus_Plus, Animal);
3598
3599 procedure Set_Age (X : in out Animal; Age : Integer);
3600 pragma Export (C_Plus_Plus, Set_Age);
3601
3602 function Age (X : Animal) return Integer;
3603 pragma Export (C_Plus_Plus, Age);
3604
3605 type Dog is new Animal and Carnivore and Domestic with record
3606 Tooth_Count : Natural;
3607 Owner : String (1 .. 30);
3608 end record;
3609 pragma Convention (C_Plus_Plus, Dog);
3610
3611 function Number_Of_Teeth (A : Dog) return Integer;
3612 pragma Export (C_Plus_Plus, Number_Of_Teeth);
3613
3614 procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
3615 pragma Export (C_Plus_Plus, Set_Owner);
3616
3617 function New_Dog return Dog'Class;
3618 pragma Export (C_Plus_Plus, New_Dog);
3619 end Animals;
3620 @end smallexample
3621
3622 Compared with our previous example the only difference is the use of
3623 @code{pragma Export} to indicate to the GNAT compiler that the primitives will
3624 be available to C++. Thanks to the ABI compatibility, on the C++ side there is
3625 nothing else to be done; as explained above, the only requirement is that all
3626 the primitives and components are declared in exactly the same order.
3627
3628 For completeness, let us see a brief C++ main program that uses the
3629 declarations available in @code{animals.h} (presented in our first example) to
3630 import and use the declarations from the Ada side, properly initializing and
3631 finalizing the Ada run-time system along the way:
3632
3633 @smallexample
3634 @b{#include} "animals.h"
3635 @b{#include} <iostream>
3636 @b{using namespace} std;
3637
3638 void Check_Carnivore (Carnivore *obj) @{@dots{}@}
3639 void Check_Domestic (Domestic *obj) @{@dots{}@}
3640 void Check_Animal (Animal *obj) @{@dots{}@}
3641 void Check_Dog (Dog *obj) @{@dots{}@}
3642
3643 @b{extern} "C" @{
3644 void adainit (void);
3645 void adafinal (void);
3646 Dog* new_dog ();
3647 @}
3648
3649 void test ()
3650 @{
3651 Dog *obj = new_dog(); // Ada constructor
3652 Check_Carnivore (obj); // Check secondary DT
3653 Check_Domestic (obj); // Check secondary DT
3654 Check_Animal (obj); // Check primary DT
3655 Check_Dog (obj); // Check primary DT
3656 @}
3657
3658 int main ()
3659 @{
3660 adainit (); test(); adafinal ();
3661 return 0;
3662 @}
3663 @end smallexample
3664
3665 @node Comparison between GNAT and C/C++ Compilation Models
3666 @section Comparison between GNAT and C/C++ Compilation Models
3667
3668 @noindent
3669 The GNAT model of compilation is close to the C and C++ models. You can
3670 think of Ada specs as corresponding to header files in C. As in C, you
3671 don't need to compile specs; they are compiled when they are used. The
3672 Ada @code{with} is similar in effect to the @code{#include} of a C
3673 header.
3674
3675 One notable difference is that, in Ada, you may compile specs separately
3676 to check them for semantic and syntactic accuracy. This is not always
3677 possible with C headers because they are fragments of programs that have
3678 less specific syntactic or semantic rules.
3679
3680 The other major difference is the requirement for running the binder,
3681 which performs two important functions. First, it checks for
3682 consistency. In C or C++, the only defense against assembling
3683 inconsistent programs lies outside the compiler, in a makefile, for
3684 example. The binder satisfies the Ada requirement that it be impossible
3685 to construct an inconsistent program when the compiler is used in normal
3686 mode.
3687
3688 @cindex Elaboration order control
3689 The other important function of the binder is to deal with elaboration
3690 issues. There are also elaboration issues in C++ that are handled
3691 automatically. This automatic handling has the advantage of being
3692 simpler to use, but the C++ programmer has no control over elaboration.
3693 Where @code{gnatbind} might complain there was no valid order of
3694 elaboration, a C++ compiler would simply construct a program that
3695 malfunctioned at run time.
3696 @end ifclear
3697
3698 @node Comparison between GNAT and Conventional Ada Library Models
3699 @section Comparison between GNAT and Conventional Ada Library Models
3700
3701 @noindent
3702 This section is intended for Ada programmers who have
3703 used an Ada compiler implementing the traditional Ada library
3704 model, as described in the Ada Reference Manual.
3705
3706 @cindex GNAT library
3707 In GNAT, there is no ``library'' in the normal sense. Instead, the set of
3708 source files themselves acts as the library. Compiling Ada programs does
3709 not generate any centralized information, but rather an object file and
3710 a ALI file, which are of interest only to the binder and linker.
3711 In a traditional system, the compiler reads information not only from
3712 the source file being compiled, but also from the centralized library.
3713 This means that the effect of a compilation depends on what has been
3714 previously compiled. In particular:
3715
3716 @itemize @bullet
3717 @item
3718 When a unit is @code{with}'ed, the unit seen by the compiler corresponds
3719 to the version of the unit most recently compiled into the library.
3720
3721 @item
3722 Inlining is effective only if the necessary body has already been
3723 compiled into the library.
3724
3725 @item
3726 Compiling a unit may obsolete other units in the library.
3727 @end itemize
3728
3729 @noindent
3730 In GNAT, compiling one unit never affects the compilation of any other
3731 units because the compiler reads only source files. Only changes to source
3732 files can affect the results of a compilation. In particular:
3733
3734 @itemize @bullet
3735 @item
3736 When a unit is @code{with}'ed, the unit seen by the compiler corresponds
3737 to the source version of the unit that is currently accessible to the
3738 compiler.
3739
3740 @item
3741 @cindex Inlining
3742 Inlining requires the appropriate source files for the package or
3743 subprogram bodies to be available to the compiler. Inlining is always
3744 effective, independent of the order in which units are complied.
3745
3746 @item
3747 Compiling a unit never affects any other compilations. The editing of
3748 sources may cause previous compilations to be out of date if they
3749 depended on the source file being modified.
3750 @end itemize
3751
3752 @noindent
3753 The most important result of these differences is that order of compilation
3754 is never significant in GNAT. There is no situation in which one is
3755 required to do one compilation before another. What shows up as order of
3756 compilation requirements in the traditional Ada library becomes, in
3757 GNAT, simple source dependencies; in other words, there is only a set
3758 of rules saying what source files must be present when a file is
3759 compiled.
3760
3761 @ifset vms
3762 @node Placement of temporary files
3763 @section Placement of temporary files
3764 @cindex Temporary files (user control over placement)
3765
3766 @noindent
3767 GNAT creates temporary files in the directory designated by the environment
3768 variable @env{TMPDIR}.
3769 (See the HP @emph{C RTL Reference Manual} on the function @code{getenv()}
3770 for detailed information on how environment variables are resolved.
3771 For most users the easiest way to make use of this feature is to simply
3772 define @env{TMPDIR} as a job level logical name).
3773 For example, if you wish to use a Ramdisk (assuming DECRAM is installed)
3774 for compiler temporary files, then you can include something like the
3775 following command in your @file{LOGIN.COM} file:
3776
3777 @smallexample
3778 $ define/job TMPDIR "/disk$scratchram/000000/temp/"
3779 @end smallexample
3780
3781 @noindent
3782 If @env{TMPDIR} is not defined, then GNAT uses the directory designated by
3783 @env{TMP}; if @env{TMP} is not defined, then GNAT uses the directory
3784 designated by @env{TEMP}.
3785 If none of these environment variables are defined then GNAT uses the
3786 directory designated by the logical name @code{SYS$SCRATCH:}
3787 (by default the user's home directory). If all else fails
3788 GNAT uses the current directory for temporary files.
3789 @end ifset
3790
3791 @c *************************
3792 @node Compiling Using gcc
3793 @chapter Compiling Using @command{gcc}
3794
3795 @noindent
3796 This chapter discusses how to compile Ada programs using the @command{gcc}
3797 command. It also describes the set of switches
3798 that can be used to control the behavior of the compiler.
3799 @menu
3800 * Compiling Programs::
3801 * Switches for gcc::
3802 * Search Paths and the Run-Time Library (RTL)::
3803 * Order of Compilation Issues::
3804 * Examples::
3805 @end menu
3806
3807 @node Compiling Programs
3808 @section Compiling Programs
3809
3810 @noindent
3811 The first step in creating an executable program is to compile the units
3812 of the program using the @command{gcc} command. You must compile the
3813 following files:
3814
3815 @itemize @bullet
3816 @item
3817 the body file (@file{.adb}) for a library level subprogram or generic
3818 subprogram
3819
3820 @item
3821 the spec file (@file{.ads}) for a library level package or generic
3822 package that has no body
3823
3824 @item
3825 the body file (@file{.adb}) for a library level package
3826 or generic package that has a body
3827
3828 @end itemize
3829
3830 @noindent
3831 You need @emph{not} compile the following files
3832
3833 @itemize @bullet
3834
3835 @item
3836 the spec of a library unit which has a body
3837
3838 @item
3839 subunits
3840 @end itemize
3841
3842 @noindent
3843 because they are compiled as part of compiling related units. GNAT
3844 package specs
3845 when the corresponding body is compiled, and subunits when the parent is
3846 compiled.
3847
3848 @cindex cannot generate code
3849 If you attempt to compile any of these files, you will get one of the
3850 following error messages (where @var{fff} is the name of the file you compiled):
3851
3852 @smallexample
3853 cannot generate code for file @var{fff} (package spec)
3854 to check package spec, use -gnatc
3855
3856 cannot generate code for file @var{fff} (missing subunits)
3857 to check parent unit, use -gnatc
3858
3859 cannot generate code for file @var{fff} (subprogram spec)
3860 to check subprogram spec, use -gnatc
3861
3862 cannot generate code for file @var{fff} (subunit)
3863 to check subunit, use -gnatc
3864 @end smallexample
3865
3866 @noindent
3867 As indicated by the above error messages, if you want to submit
3868 one of these files to the compiler to check for correct semantics
3869 without generating code, then use the @option{-gnatc} switch.
3870
3871 The basic command for compiling a file containing an Ada unit is
3872
3873 @smallexample
3874 $ gcc -c @ovar{switches} @file{file name}
3875 @end smallexample
3876
3877 @noindent
3878 where @var{file name} is the name of the Ada file (usually
3879 having an extension
3880 @file{.ads} for a spec or @file{.adb} for a body).
3881 @ifclear vms
3882 You specify the
3883 @option{-c} switch to tell @command{gcc} to compile, but not link, the file.
3884 @end ifclear
3885 The result of a successful compilation is an object file, which has the
3886 same name as the source file but an extension of @file{.o} and an Ada
3887 Library Information (ALI) file, which also has the same name as the
3888 source file, but with @file{.ali} as the extension. GNAT creates these
3889 two output files in the current directory, but you may specify a source
3890 file in any directory using an absolute or relative path specification
3891 containing the directory information.
3892
3893 @findex gnat1
3894 @command{gcc} is actually a driver program that looks at the extensions of
3895 the file arguments and loads the appropriate compiler. For example, the
3896 GNU C compiler is @file{cc1}, and the Ada compiler is @file{gnat1}.
3897 These programs are in directories known to the driver program (in some
3898 configurations via environment variables you set), but need not be in
3899 your path. The @command{gcc} driver also calls the assembler and any other
3900 utilities needed to complete the generation of the required object
3901 files.
3902
3903 It is possible to supply several file names on the same @command{gcc}
3904 command. This causes @command{gcc} to call the appropriate compiler for
3905 each file. For example, the following command lists three separate
3906 files to be compiled:
3907
3908 @smallexample
3909 $ gcc -c x.adb y.adb z.c
3910 @end smallexample
3911
3912 @noindent
3913 calls @code{gnat1} (the Ada compiler) twice to compile @file{x.adb} and
3914 @file{y.adb}, and @code{cc1} (the C compiler) once to compile @file{z.c}.
3915 The compiler generates three object files @file{x.o}, @file{y.o} and
3916 @file{z.o} and the two ALI files @file{x.ali} and @file{y.ali} from the
3917 Ada compilations. Any switches apply to all the files ^listed,^listed.^
3918 @ifclear vms
3919 except for
3920 @option{-gnat@var{x}} switches, which apply only to Ada compilations.
3921 @end ifclear
3922
3923 @node Switches for gcc
3924 @section Switches for @command{gcc}
3925
3926 @noindent
3927 The @command{gcc} command accepts switches that control the
3928 compilation process. These switches are fully described in this section.
3929 First we briefly list all the switches, in alphabetical order, then we
3930 describe the switches in more detail in functionally grouped sections.
3931
3932 More switches exist for GCC than those documented here, especially
3933 for specific targets. However, their use is not recommended as
3934 they may change code generation in ways that are incompatible with
3935 the Ada run-time library, or can cause inconsistencies between
3936 compilation units.
3937
3938 @menu
3939 * Output and Error Message Control::
3940 * Warning Message Control::
3941 * Debugging and Assertion Control::
3942 * Validity Checking::
3943 * Style Checking::
3944 * Run-Time Checks::
3945 * Using gcc for Syntax Checking::
3946 * Using gcc for Semantic Checking::
3947 * Compiling Different Versions of Ada::
3948 * Character Set Control::
3949 * File Naming Control::
3950 * Subprogram Inlining Control::
3951 * Auxiliary Output Control::
3952 * Debugging Control::
3953 * Exception Handling Control::
3954 * Units to Sources Mapping Files::
3955 * Integrated Preprocessing::
3956 * Code Generation Control::
3957 @ifset vms
3958 * Return Codes::
3959 @end ifset
3960 @end menu
3961
3962 @table @option
3963 @c !sort!
3964 @ifclear vms
3965 @cindex @option{-b} (@command{gcc})
3966 @item -b @var{target}
3967 Compile your program to run on @var{target}, which is the name of a
3968 system configuration. You must have a GNAT cross-compiler built if
3969 @var{target} is not the same as your host system.
3970
3971 @item -B@var{dir}
3972 @cindex @option{-B} (@command{gcc})
3973 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
3974 from @var{dir} instead of the default location. Only use this switch
3975 when multiple versions of the GNAT compiler are available.
3976 @xref{Directory Options,, Options for Directory Search, gcc, Using the
3977 GNU Compiler Collection (GCC)}, for further details. You would normally
3978 use the @option{-b} or @option{-V} switch instead.
3979
3980 @item -c
3981 @cindex @option{-c} (@command{gcc})
3982 Compile. Always use this switch when compiling Ada programs.
3983
3984 Note: for some other languages when using @command{gcc}, notably in
3985 the case of C and C++, it is possible to use
3986 use @command{gcc} without a @option{-c} switch to
3987 compile and link in one step. In the case of GNAT, you
3988 cannot use this approach, because the binder must be run
3989 and @command{gcc} cannot be used to run the GNAT binder.
3990 @end ifclear
3991
3992 @item -fno-inline
3993 @cindex @option{-fno-inline} (@command{gcc})
3994 Suppresses all back-end inlining, even if other optimization or inlining
3995 switches are set.
3996 This includes suppression of inlining that results
3997 from the use of the pragma @code{Inline_Always}.
3998 Any occurrences of pragma @code{Inline} or @code{Inline_Always}
3999 are ignored, and @option{-gnatn} and @option{-gnatN} have no
4000 effect if this switch is present.
4001
4002 @item -fno-inline-functions
4003 @cindex @option{-fno-inline-functions} (@command{gcc})
4004 Suppresses automatic inlining of simple subprograms, which is enabled
4005 if @option{-O3} is used.
4006
4007 @item -fno-inline-small-functions
4008 @cindex @option{-fno-inline-small-functions} (@command{gcc})
4009 Suppresses automatic inlining of small subprograms, which is enabled
4010 if @option{-O2} is used.
4011
4012 @item -fno-inline-functions-called-once
4013 @cindex @option{-fno-inline-functions-called-once} (@command{gcc})
4014 Suppresses inlining of subprograms local to the unit and called once
4015 from within it, which is enabled if @option{-O1} is used.
4016
4017 @item -fno-ivopts
4018 @cindex @option{-fno-ivopts} (@command{gcc})
4019 Suppresses high-level loop induction variable optimizations, which are
4020 enabled if @option{-O1} is used. These optimizations are generally
4021 profitable but, for some specific cases of loops with numerous uses
4022 of the iteration variable that follow a common pattern, they may end
4023 up destroying the regularity that could be exploited at a lower level
4024 and thus producing inferior code.
4025
4026 @item -fno-strict-aliasing
4027 @cindex @option{-fno-strict-aliasing} (@command{gcc})
4028 Causes the compiler to avoid assumptions regarding non-aliasing
4029 of objects of different types. See
4030 @ref{Optimization and Strict Aliasing} for details.
4031
4032 @item -fstack-check
4033 @cindex @option{-fstack-check} (@command{gcc})
4034 Activates stack checking.
4035 See @ref{Stack Overflow Checking} for details.
4036
4037 @item -fstack-usage
4038 @cindex @option{-fstack-usage} (@command{gcc})
4039 Makes the compiler output stack usage information for the program, on a
4040 per-function basis. See @ref{Static Stack Usage Analysis} for details.
4041
4042 @item -fcallgraph-info@r{[}=su@r{]}
4043 @cindex @option{-fcallgraph-info} (@command{gcc})
4044 Makes the compiler output callgraph information for the program, on a
4045 per-file basis. The information is generated in the VCG format. It can
4046 be decorated with stack-usage per-node information.
4047
4048 @item ^-g^/DEBUG^
4049 @cindex @option{^-g^/DEBUG^} (@command{gcc})
4050 Generate debugging information. This information is stored in the object
4051 file and copied from there to the final executable file by the linker,
4052 where it can be read by the debugger. You must use the
4053 @option{^-g^/DEBUG^} switch if you plan on using the debugger.
4054
4055 @item -gnat83
4056 @cindex @option{-gnat83} (@command{gcc})
4057 Enforce Ada 83 restrictions.
4058
4059 @item -gnat95
4060 @cindex @option{-gnat95} (@command{gcc})
4061 Enforce Ada 95 restrictions.
4062
4063 @item -gnat05
4064 @cindex @option{-gnat05} (@command{gcc})
4065 Allow full Ada 2005 features.
4066
4067 @item -gnata
4068 @cindex @option{-gnata} (@command{gcc})
4069 Assertions enabled. @code{Pragma Assert} and @code{pragma Debug} to be
4070 activated. Note that these pragmas can also be controlled using the
4071 configuration pragmas @code{Assertion_Policy} and @code{Debug_Policy}.
4072 It also activates pragmas @code{Check}, @code{Precondition}, and
4073 @code{Postcondition}. Note that these pragmas can also be controlled
4074 using the configuration pragma @code{Check_Policy}.
4075
4076 @item -gnatA
4077 @cindex @option{-gnatA} (@command{gcc})
4078 Avoid processing @file{gnat.adc}. If a @file{gnat.adc} file is present,
4079 it will be ignored.
4080
4081 @item -gnatb
4082 @cindex @option{-gnatb} (@command{gcc})
4083 Generate brief messages to @file{stderr} even if verbose mode set.
4084
4085 @item -gnatB
4086 @cindex @option{-gnatB} (@command{gcc})
4087 Assume no invalid (bad) values except for 'Valid attribute use.
4088
4089 @item -gnatc
4090 @cindex @option{-gnatc} (@command{gcc})
4091 Check syntax and semantics only (no code generation attempted).
4092
4093 @item -gnatC
4094 @cindex @option{-gnatC} (@command{gcc})
4095 Generate CodePeer information (no code generation attempted).
4096 This switch will generate an intermediate representation suitable for
4097 use by CodePeer (@file{.scil} files). This switch is not compatible with
4098 code generation (it will, among other things, disable some switches such
4099 as -gnatn, and enable others such as -gnata).
4100
4101 @item -gnatd
4102 @cindex @option{-gnatd} (@command{gcc})
4103 Specify debug options for the compiler. The string of characters after
4104 the @option{-gnatd} specify the specific debug options. The possible
4105 characters are 0-9, a-z, A-Z, optionally preceded by a dot. See
4106 compiler source file @file{debug.adb} for details of the implemented
4107 debug options. Certain debug options are relevant to applications
4108 programmers, and these are documented at appropriate points in this
4109 users guide.
4110
4111 @ifclear vms
4112 @item -gnatD
4113 @cindex @option{-gnatD[nn]} (@command{gcc})
4114 @end ifclear
4115 @ifset vms
4116 @item /XDEBUG /LXDEBUG=nnn
4117 @end ifset
4118 Create expanded source files for source level debugging. This switch
4119 also suppress generation of cross-reference information
4120 (see @option{-gnatx}).
4121
4122 @item -gnatec=@var{path}
4123 @cindex @option{-gnatec} (@command{gcc})
4124 Specify a configuration pragma file
4125 @ifclear vms
4126 (the equal sign is optional)
4127 @end ifclear
4128 (@pxref{The Configuration Pragmas Files}).
4129
4130 @item ^-gnateD^/DATA_PREPROCESSING=^symbol@r{[}=@var{value}@r{]}
4131 @cindex @option{-gnateD} (@command{gcc})
4132 Defines a symbol, associated with @var{value}, for preprocessing.
4133 (@pxref{Integrated Preprocessing}).
4134
4135 @item -gnatef
4136 @cindex @option{-gnatef} (@command{gcc})
4137 Display full source path name in brief error messages.
4138
4139 @item -gnateG
4140 @cindex @option{-gnateG} (@command{gcc})
4141 Save result of preprocessing in a text file.
4142
4143 @item -gnatem=@var{path}
4144 @cindex @option{-gnatem} (@command{gcc})
4145 Specify a mapping file
4146 @ifclear vms
4147 (the equal sign is optional)
4148 @end ifclear
4149 (@pxref{Units to Sources Mapping Files}).
4150
4151 @item -gnatep=@var{file}
4152 @cindex @option{-gnatep} (@command{gcc})
4153 Specify a preprocessing data file
4154 @ifclear vms
4155 (the equal sign is optional)
4156 @end ifclear
4157 (@pxref{Integrated Preprocessing}).
4158
4159 @item -gnatE
4160 @cindex @option{-gnatE} (@command{gcc})
4161 Full dynamic elaboration checks.
4162
4163 @item -gnatf
4164 @cindex @option{-gnatf} (@command{gcc})
4165 Full errors. Multiple errors per line, all undefined references, do not
4166 attempt to suppress cascaded errors.
4167
4168 @item -gnatF
4169 @cindex @option{-gnatF} (@command{gcc})
4170 Externals names are folded to all uppercase.
4171
4172 @item ^-gnatg^/GNAT_INTERNAL^
4173 @cindex @option{^-gnatg^/GNAT_INTERNAL^} (@command{gcc})
4174 Internal GNAT implementation mode. This should not be used for
4175 applications programs, it is intended only for use by the compiler
4176 and its run-time library. For documentation, see the GNAT sources.
4177 Note that @option{^-gnatg^/GNAT_INTERNAL^} implies
4178 @option{^-gnatwae^/WARNINGS=ALL,ERRORS^} and
4179 @option{^-gnatyg^/STYLE_CHECKS=GNAT^}
4180 so that all standard warnings and all standard style options are turned on.
4181 All warnings and style error messages are treated as errors.
4182
4183 @ifclear vms
4184 @item -gnatG=nn
4185 @cindex @option{-gnatG[nn]} (@command{gcc})
4186 @end ifclear
4187 @ifset vms
4188 @item /EXPAND_SOURCE, /LEXPAND_SOURCE=nnn
4189 @end ifset
4190 List generated expanded code in source form.
4191
4192 @item ^-gnath^/HELP^
4193 @cindex @option{^-gnath^/HELP^} (@command{gcc})
4194 Output usage information. The output is written to @file{stdout}.
4195
4196 @item ^-gnati^/IDENTIFIER_CHARACTER_SET=^@var{c}
4197 @cindex @option{^-gnati^/IDENTIFIER_CHARACTER_SET^} (@command{gcc})
4198 Identifier character set
4199 @ifclear vms
4200 (@var{c}=1/2/3/4/8/9/p/f/n/w).
4201 @end ifclear
4202 For details of the possible selections for @var{c},
4203 see @ref{Character Set Control}.
4204
4205 @item ^-gnatI^/IGNORE_REP_CLAUSES^
4206 @cindex @option{^-gnatI^IGNORE_REP_CLAUSES^} (@command{gcc})
4207 Ignore representation clauses. When this switch is used,
4208 representation clauses are treated as comments. This is useful
4209 when initially porting code where you want to ignore rep clause
4210 problems, and also for compiling foreign code (particularly
4211 for use with ASIS). The representation clauses that are ignored
4212 are: enumeration_representation_clause, record_representation_clause,
4213 and attribute_definition_clause for the following attributes:
4214 Address, Alignment, Bit_Order, Component_Size, Machine_Radix,
4215 Object_Size, Size, Small, Stream_Size, and Value_Size.
4216 Note that this option should be used only for compiling -- the
4217 code is likely to malfunction at run time.
4218
4219 @item -gnatjnn
4220 @cindex @option{-gnatjnn} (@command{gcc})
4221 Reformat error messages to fit on nn character lines
4222
4223 @item -gnatk=@var{n}
4224 @cindex @option{-gnatk} (@command{gcc})
4225 Limit file names to @var{n} (1-999) characters ^(@code{k} = krunch)^^.
4226
4227 @item -gnatl
4228 @cindex @option{-gnatl} (@command{gcc})
4229 Output full source listing with embedded error messages.
4230
4231 @item -gnatL
4232 @cindex @option{-gnatL} (@command{gcc})
4233 Used in conjunction with -gnatG or -gnatD to intersperse original
4234 source lines (as comment lines with line numbers) in the expanded
4235 source output.
4236
4237 @item -gnatm=@var{n}
4238 @cindex @option{-gnatm} (@command{gcc})
4239 Limit number of detected error or warning messages to @var{n}
4240 where @var{n} is in the range 1..999999. The default setting if
4241 no switch is given is 9999. If the number of warnings reaches this
4242 limit, then a message is output and further warnings are suppressed,
4243 but the compilation is continued. If the number of error messages
4244 reaches this limit, then a message is output and the compilation
4245 is abandoned. The equal sign here is optional. A value of zero
4246 means that no limit applies.
4247
4248 @item -gnatn
4249 @cindex @option{-gnatn} (@command{gcc})
4250 Activate inlining for subprograms for which
4251 pragma @code{inline} is specified. This inlining is performed
4252 by the GCC back-end.
4253
4254 @item -gnatN
4255 @cindex @option{-gnatN} (@command{gcc})
4256 Activate front end inlining for subprograms for which
4257 pragma @code{Inline} is specified. This inlining is performed
4258 by the front end and will be visible in the
4259 @option{-gnatG} output.
4260
4261 When using a gcc-based back end (in practice this means using any version
4262 of GNAT other than the JGNAT, .NET or GNAAMP versions), then the use of
4263 @option{-gnatN} is deprecated, and the use of @option{-gnatn} is preferred.
4264 Historically front end inlining was more extensive than the gcc back end
4265 inlining, but that is no longer the case.
4266
4267 @item -gnato
4268 @cindex @option{-gnato} (@command{gcc})
4269 Enable numeric overflow checking (which is not normally enabled by
4270 default). Note that division by zero is a separate check that is not
4271 controlled by this switch (division by zero checking is on by default).
4272
4273 @item -gnatp
4274 @cindex @option{-gnatp} (@command{gcc})
4275 Suppress all checks. See @ref{Run-Time Checks} for details.
4276
4277 @item -gnatP
4278 @cindex @option{-gnatP} (@command{gcc})
4279 Enable polling. This is required on some systems (notably Windows NT) to
4280 obtain asynchronous abort and asynchronous transfer of control capability.
4281 @xref{Pragma Polling,,, gnat_rm, GNAT Reference Manual}, for full
4282 details.
4283
4284 @item -gnatq
4285 @cindex @option{-gnatq} (@command{gcc})
4286 Don't quit. Try semantics, even if parse errors.
4287
4288 @item -gnatQ
4289 @cindex @option{-gnatQ} (@command{gcc})
4290 Don't quit. Generate @file{ALI} and tree files even if illegalities.
4291
4292 @item -gnatr
4293 @cindex @option{-gnatr} (@command{gcc})
4294 Treat pragma Restrictions as Restriction_Warnings.
4295
4296 @item ^-gnatR@r{[}0@r{/}1@r{/}2@r{/}3@r{[}s@r{]]}^/REPRESENTATION_INFO^
4297 @cindex @option{-gnatR} (@command{gcc})
4298 Output representation information for declared types and objects.
4299
4300 @item -gnats
4301 @cindex @option{-gnats} (@command{gcc})
4302 Syntax check only.
4303
4304 @item -gnatS
4305 @cindex @option{-gnatS} (@command{gcc})
4306 Print package Standard.
4307
4308 @item -gnatt
4309 @cindex @option{-gnatt} (@command{gcc})
4310 Generate tree output file.
4311
4312 @item ^-gnatT^/TABLE_MULTIPLIER=^@var{nnn}
4313 @cindex @option{^-gnatT^/TABLE_MULTIPLIER^} (@command{gcc})
4314 All compiler tables start at @var{nnn} times usual starting size.
4315
4316 @item -gnatu
4317 @cindex @option{-gnatu} (@command{gcc})
4318 List units for this compilation.
4319
4320 @item -gnatU
4321 @cindex @option{-gnatU} (@command{gcc})
4322 Tag all error messages with the unique string ``error:''
4323
4324 @item -gnatv
4325 @cindex @option{-gnatv} (@command{gcc})
4326 Verbose mode. Full error output with source lines to @file{stdout}.
4327
4328 @item -gnatV
4329 @cindex @option{-gnatV} (@command{gcc})
4330 Control level of validity checking. See separate section describing
4331 this feature.
4332
4333 @item ^-gnatw@var{xxx}^/WARNINGS=(@var{option}@r{[},@dots{}@r{]})^
4334 @cindex @option{^-gnatw^/WARNINGS^} (@command{gcc})
4335 Warning mode where
4336 ^@var{xxx} is a string of option letters that^the list of options^ denotes
4337 the exact warnings that
4338 are enabled or disabled (@pxref{Warning Message Control}).
4339
4340 @item ^-gnatW^/WIDE_CHARACTER_ENCODING=^@var{e}
4341 @cindex @option{^-gnatW^/WIDE_CHARACTER_ENCODING^} (@command{gcc})
4342 Wide character encoding method
4343 @ifclear vms
4344 (@var{e}=n/h/u/s/e/8).
4345 @end ifclear
4346 @ifset vms
4347 (@var{e}=@code{BRACKETS, NONE, HEX, UPPER, SHIFT_JIS, EUC, UTF8})
4348 @end ifset
4349
4350 @item -gnatx
4351 @cindex @option{-gnatx} (@command{gcc})
4352 Suppress generation of cross-reference information.
4353
4354 @item ^-gnaty^/STYLE_CHECKS=(option,option@dots{})^
4355 @cindex @option{^-gnaty^/STYLE_CHECKS^} (@command{gcc})
4356 Enable built-in style checks (@pxref{Style Checking}).
4357
4358 @item ^-gnatz^/DISTRIBUTION_STUBS=^@var{m}
4359 @cindex @option{^-gnatz^/DISTRIBUTION_STUBS^} (@command{gcc})
4360 Distribution stub generation and compilation
4361 @ifclear vms
4362 (@var{m}=r/c for receiver/caller stubs).
4363 @end ifclear
4364 @ifset vms
4365 (@var{m}=@code{RECEIVER} or @code{CALLER} to specify the type of stubs
4366 to be generated and compiled).
4367 @end ifset
4368
4369 @item ^-I^/SEARCH=^@var{dir}
4370 @cindex @option{^-I^/SEARCH^} (@command{gcc})
4371 @cindex RTL
4372 Direct GNAT to search the @var{dir} directory for source files needed by
4373 the current compilation
4374 (@pxref{Search Paths and the Run-Time Library (RTL)}).
4375
4376 @item ^-I-^/NOCURRENT_DIRECTORY^
4377 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gcc})
4378 @cindex RTL
4379 Except for the source file named in the command line, do not look for source
4380 files in the directory containing the source file named in the command line
4381 (@pxref{Search Paths and the Run-Time Library (RTL)}).
4382
4383 @ifclear vms
4384 @item -mbig-switch
4385 @cindex @option{-mbig-switch} (@command{gcc})
4386 @cindex @code{case} statement (effect of @option{-mbig-switch} option)
4387 This standard gcc switch causes the compiler to use larger offsets in its
4388 jump table representation for @code{case} statements.
4389 This may result in less efficient code, but is sometimes necessary
4390 (for example on HP-UX targets)
4391 @cindex HP-UX and @option{-mbig-switch} option
4392 in order to compile large and/or nested @code{case} statements.
4393
4394 @item -o @var{file}
4395 @cindex @option{-o} (@command{gcc})
4396 This switch is used in @command{gcc} to redirect the generated object file
4397 and its associated ALI file. Beware of this switch with GNAT, because it may
4398 cause the object file and ALI file to have different names which in turn
4399 may confuse the binder and the linker.
4400 @end ifclear
4401
4402 @item -nostdinc
4403 @cindex @option{-nostdinc} (@command{gcc})
4404 Inhibit the search of the default location for the GNAT Run Time
4405 Library (RTL) source files.
4406
4407 @item -nostdlib
4408 @cindex @option{-nostdlib} (@command{gcc})
4409 Inhibit the search of the default location for the GNAT Run Time
4410 Library (RTL) ALI files.
4411
4412 @ifclear vms
4413 @item -O@ovar{n}
4414 @cindex @option{-O} (@command{gcc})
4415 @var{n} controls the optimization level.
4416
4417 @table @asis
4418 @item n = 0
4419 No optimization, the default setting if no @option{-O} appears
4420
4421 @item n = 1
4422 Normal optimization, the default if you specify @option{-O} without
4423 an operand. A good compromise between code quality and compilation
4424 time.
4425
4426 @item n = 2
4427 Extensive optimization, may improve execution time, possibly at the cost of
4428 substantially increased compilation time.
4429
4430 @item n = 3
4431 Same as @option{-O2}, and also includes inline expansion for small subprograms
4432 in the same unit.
4433
4434 @item n = s
4435 Optimize space usage
4436 @end table
4437
4438 @noindent
4439 See also @ref{Optimization Levels}.
4440 @end ifclear
4441
4442 @ifset vms
4443 @item /NOOPTIMIZE
4444 @cindex @option{/NOOPTIMIZE} (@code{GNAT COMPILE})
4445 Equivalent to @option{/OPTIMIZE=NONE}.
4446 This is the default behavior in the absence of an @option{/OPTIMIZE}
4447 qualifier.
4448
4449 @item /OPTIMIZE@r{[}=(keyword@r{[},@dots{}@r{]})@r{]}
4450 @cindex @option{/OPTIMIZE} (@code{GNAT COMPILE})
4451 Selects the level of optimization for your program. The supported
4452 keywords are as follows:
4453 @table @code
4454 @item ALL
4455 Perform most optimizations, including those that
4456 are expensive.
4457 This is the default if the @option{/OPTIMIZE} qualifier is supplied
4458 without keyword options.
4459
4460 @item NONE
4461 Do not do any optimizations. Same as @code{/NOOPTIMIZE}.
4462
4463 @item SOME
4464 Perform some optimizations, but omit ones that are costly.
4465
4466 @item DEVELOPMENT
4467 Same as @code{SOME}.
4468
4469 @item INLINING
4470 Full optimization as in @option{/OPTIMIZE=ALL}, and also attempts
4471 automatic inlining of small subprograms within a unit
4472
4473 @item UNROLL_LOOPS
4474 Try to unroll loops. This keyword may be specified together with
4475 any keyword above other than @code{NONE}. Loop unrolling
4476 usually, but not always, improves the performance of programs.
4477
4478 @item SPACE
4479 Optimize space usage
4480 @end table
4481
4482 @noindent
4483 See also @ref{Optimization Levels}.
4484 @end ifset
4485
4486 @ifclear vms
4487 @item -pass-exit-codes
4488 @cindex @option{-pass-exit-codes} (@command{gcc})
4489 Catch exit codes from the compiler and use the most meaningful as
4490 exit status.
4491 @end ifclear
4492
4493 @item --RTS=@var{rts-path}
4494 @cindex @option{--RTS} (@command{gcc})
4495 Specifies the default location of the runtime library. Same meaning as the
4496 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
4497
4498 @item ^-S^/ASM^
4499 @cindex @option{^-S^/ASM^} (@command{gcc})
4500 ^Used in place of @option{-c} to^Used to^
4501 cause the assembler source file to be
4502 generated, using @file{^.s^.S^} as the extension,
4503 instead of the object file.
4504 This may be useful if you need to examine the generated assembly code.
4505
4506 @item ^-fverbose-asm^/VERBOSE_ASM^
4507 @cindex @option{^-fverbose-asm^/VERBOSE_ASM^} (@command{gcc})
4508 ^Used in conjunction with @option{-S}^Used in place of @option{/ASM}^
4509 to cause the generated assembly code file to be annotated with variable
4510 names, making it significantly easier to follow.
4511
4512 @item ^-v^/VERBOSE^
4513 @cindex @option{^-v^/VERBOSE^} (@command{gcc})
4514 Show commands generated by the @command{gcc} driver. Normally used only for
4515 debugging purposes or if you need to be sure what version of the
4516 compiler you are executing.
4517
4518 @ifclear vms
4519 @item -V @var{ver}
4520 @cindex @option{-V} (@command{gcc})
4521 Execute @var{ver} version of the compiler. This is the @command{gcc}
4522 version, not the GNAT version.
4523 @end ifclear
4524
4525 @item ^-w^/NO_BACK_END_WARNINGS^
4526 @cindex @option{-w} (@command{gcc})
4527 Turn off warnings generated by the back end of the compiler. Use of
4528 this switch also causes the default for front end warnings to be set
4529 to suppress (as though @option{-gnatws} had appeared at the start of
4530 the options).
4531
4532 @end table
4533
4534 @ifclear vms
4535 @c Combining qualifiers does not work on VMS
4536 You may combine a sequence of GNAT switches into a single switch. For
4537 example, the combined switch
4538
4539 @cindex Combining GNAT switches
4540 @smallexample
4541 -gnatofi3
4542 @end smallexample
4543
4544 @noindent
4545 is equivalent to specifying the following sequence of switches:
4546
4547 @smallexample
4548 -gnato -gnatf -gnati3
4549 @end smallexample
4550 @end ifclear
4551
4552 @noindent
4553 The following restrictions apply to the combination of switches
4554 in this manner:
4555
4556 @itemize @bullet
4557 @item
4558 The switch @option{-gnatc} if combined with other switches must come
4559 first in the string.
4560
4561 @item
4562 The switch @option{-gnats} if combined with other switches must come
4563 first in the string.
4564
4565 @item
4566 The switches
4567 @option{^-gnatz^/DISTRIBUTION_STUBS^}, @option{-gnatzc}, and @option{-gnatzr}
4568 may not be combined with any other switches.
4569
4570 @ifclear vms
4571 @item
4572 Once a ``y'' appears in the string (that is a use of the @option{-gnaty}
4573 switch), then all further characters in the switch are interpreted
4574 as style modifiers (see description of @option{-gnaty}).
4575
4576 @item
4577 Once a ``d'' appears in the string (that is a use of the @option{-gnatd}
4578 switch), then all further characters in the switch are interpreted
4579 as debug flags (see description of @option{-gnatd}).
4580
4581 @item
4582 Once a ``w'' appears in the string (that is a use of the @option{-gnatw}
4583 switch), then all further characters in the switch are interpreted
4584 as warning mode modifiers (see description of @option{-gnatw}).
4585
4586 @item
4587 Once a ``V'' appears in the string (that is a use of the @option{-gnatV}
4588 switch), then all further characters in the switch are interpreted
4589 as validity checking options (see description of @option{-gnatV}).
4590 @end ifclear
4591 @end itemize
4592
4593 @node Output and Error Message Control
4594 @subsection Output and Error Message Control
4595 @findex stderr
4596
4597 @noindent
4598 The standard default format for error messages is called ``brief format''.
4599 Brief format messages are written to @file{stderr} (the standard error
4600 file) and have the following form:
4601
4602 @smallexample
4603 e.adb:3:04: Incorrect spelling of keyword "function"
4604 e.adb:4:20: ";" should be "is"
4605 @end smallexample
4606
4607 @noindent
4608 The first integer after the file name is the line number in the file,
4609 and the second integer is the column number within the line.
4610 @ifclear vms
4611 @code{GPS} can parse the error messages
4612 and point to the referenced character.
4613 @end ifclear
4614 The following switches provide control over the error message
4615 format:
4616
4617 @table @option
4618 @c !sort!
4619 @item -gnatv
4620 @cindex @option{-gnatv} (@command{gcc})
4621 @findex stdout
4622 @ifclear vms
4623 The v stands for verbose.
4624 @end ifclear
4625 The effect of this setting is to write long-format error
4626 messages to @file{stdout} (the standard output file.
4627 The same program compiled with the
4628 @option{-gnatv} switch would generate:
4629
4630 @smallexample
4631 @cartouche
4632 3. funcion X (Q : Integer)
4633 |
4634 >>> Incorrect spelling of keyword "function"
4635 4. return Integer;
4636 |
4637 >>> ";" should be "is"
4638 @end cartouche
4639 @end smallexample
4640
4641 @noindent
4642 The vertical bar indicates the location of the error, and the @samp{>>>}
4643 prefix can be used to search for error messages. When this switch is
4644 used the only source lines output are those with errors.
4645
4646 @item -gnatl
4647 @cindex @option{-gnatl} (@command{gcc})
4648 @ifclear vms
4649 The @code{l} stands for list.
4650 @end ifclear
4651 This switch causes a full listing of
4652 the file to be generated. In the case where a body is
4653 compiled, the corresponding spec is also listed, along
4654 with any subunits. Typical output from compiling a package
4655 body @file{p.adb} might look like:
4656
4657 @smallexample @c ada
4658 @cartouche
4659 Compiling: p.adb
4660
4661 1. package body p is
4662 2. procedure a;
4663 3. procedure a is separate;
4664 4. begin
4665 5. null
4666 |
4667 >>> missing ";"
4668
4669 6. end;
4670
4671 Compiling: p.ads
4672
4673 1. package p is
4674 2. pragma Elaborate_Body
4675 |
4676 >>> missing ";"
4677
4678 3. end p;
4679
4680 Compiling: p-a.adb
4681
4682 1. separate p
4683 |
4684 >>> missing "("
4685
4686 2. procedure a is
4687 3. begin
4688 4. null
4689 |
4690 >>> missing ";"
4691
4692 5. end;
4693 @end cartouche
4694 @end smallexample
4695
4696 @noindent
4697 @findex stderr
4698 When you specify the @option{-gnatv} or @option{-gnatl} switches and
4699 standard output is redirected, a brief summary is written to
4700 @file{stderr} (standard error) giving the number of error messages and
4701 warning messages generated.
4702
4703 @item -^gnatl^OUTPUT_FILE^=file
4704 @cindex @option{^-gnatl^OUTPUT_FILE^=fname} (@command{gcc})
4705 This has the same effect as @option{-gnatl} except that the output is
4706 written to a file instead of to standard output. If the given name
4707 @file{fname} does not start with a period, then it is the full name
4708 of the file to be written. If @file{fname} is an extension, it is
4709 appended to the name of the file being compiled. For example, if
4710 file @file{xyz.adb} is compiled with @option{^-gnatl^OUTPUT_FILE^=.lst},
4711 then the output is written to file ^xyz.adb.lst^xyz.adb_lst^.
4712
4713 @item -gnatU
4714 @cindex @option{-gnatU} (@command{gcc})
4715 This switch forces all error messages to be preceded by the unique
4716 string ``error:''. This means that error messages take a few more
4717 characters in space, but allows easy searching for and identification
4718 of error messages.
4719
4720 @item -gnatb
4721 @cindex @option{-gnatb} (@command{gcc})
4722 @ifclear vms
4723 The @code{b} stands for brief.
4724 @end ifclear
4725 This switch causes GNAT to generate the
4726 brief format error messages to @file{stderr} (the standard error
4727 file) as well as the verbose
4728 format message or full listing (which as usual is written to
4729 @file{stdout} (the standard output file).
4730
4731 @item -gnatm=@var{n}
4732 @cindex @option{-gnatm} (@command{gcc})
4733 @ifclear vms
4734 The @code{m} stands for maximum.
4735 @end ifclear
4736 @var{n} is a decimal integer in the
4737 range of 1 to 999999 and limits the number of error or warning
4738 messages to be generated. For example, using
4739 @option{-gnatm2} might yield
4740
4741 @smallexample
4742 e.adb:3:04: Incorrect spelling of keyword "function"
4743 e.adb:5:35: missing ".."
4744 fatal error: maximum number of errors detected
4745 compilation abandoned
4746 @end smallexample
4747
4748 @noindent
4749 The default setting if
4750 no switch is given is 9999. If the number of warnings reaches this
4751 limit, then a message is output and further warnings are suppressed,
4752 but the compilation is continued. If the number of error messages
4753 reaches this limit, then a message is output and the compilation
4754 is abandoned. A value of zero means that no limit applies.
4755
4756 @noindent
4757 Note that the equal sign is optional, so the switches
4758 @option{-gnatm2} and @option{-gnatm=2} are equivalent.
4759
4760 @item -gnatf
4761 @cindex @option{-gnatf} (@command{gcc})
4762 @cindex Error messages, suppressing
4763 @ifclear vms
4764 The @code{f} stands for full.
4765 @end ifclear
4766 Normally, the compiler suppresses error messages that are likely to be
4767 redundant. This switch causes all error
4768 messages to be generated. In particular, in the case of
4769 references to undefined variables. If a given variable is referenced
4770 several times, the normal format of messages is
4771 @smallexample
4772 e.adb:7:07: "V" is undefined (more references follow)
4773 @end smallexample
4774
4775 @noindent
4776 where the parenthetical comment warns that there are additional
4777 references to the variable @code{V}. Compiling the same program with the
4778 @option{-gnatf} switch yields
4779
4780 @smallexample
4781 e.adb:7:07: "V" is undefined
4782 e.adb:8:07: "V" is undefined
4783 e.adb:8:12: "V" is undefined
4784 e.adb:8:16: "V" is undefined
4785 e.adb:9:07: "V" is undefined
4786 e.adb:9:12: "V" is undefined
4787 @end smallexample
4788
4789 @noindent
4790 The @option{-gnatf} switch also generates additional information for
4791 some error messages. Some examples are:
4792
4793 @itemize @bullet
4794 @item
4795 Details on possibly non-portable unchecked conversion
4796 @item
4797 List possible interpretations for ambiguous calls
4798 @item
4799 Additional details on incorrect parameters
4800 @end itemize
4801
4802 @item -gnatjnn
4803 @cindex @option{-gnatjnn} (@command{gcc})
4804 In normal operation mode (or if @option{-gnatj0} is used, then error messages
4805 with continuation lines are treated as though the continuation lines were
4806 separate messages (and so a warning with two continuation lines counts as
4807 three warnings, and is listed as three separate messages).
4808
4809 If the @option{-gnatjnn} switch is used with a positive value for nn, then
4810 messages are output in a different manner. A message and all its continuation
4811 lines are treated as a unit, and count as only one warning or message in the
4812 statistics totals. Furthermore, the message is reformatted so that no line
4813 is longer than nn characters.
4814
4815 @item -gnatq
4816 @cindex @option{-gnatq} (@command{gcc})
4817 @ifclear vms
4818 The @code{q} stands for quit (really ``don't quit'').
4819 @end ifclear
4820 In normal operation mode, the compiler first parses the program and
4821 determines if there are any syntax errors. If there are, appropriate
4822 error messages are generated and compilation is immediately terminated.
4823 This switch tells
4824 GNAT to continue with semantic analysis even if syntax errors have been
4825 found. This may enable the detection of more errors in a single run. On
4826 the other hand, the semantic analyzer is more likely to encounter some
4827 internal fatal error when given a syntactically invalid tree.
4828
4829 @item -gnatQ
4830 @cindex @option{-gnatQ} (@command{gcc})
4831 In normal operation mode, the @file{ALI} file is not generated if any
4832 illegalities are detected in the program. The use of @option{-gnatQ} forces
4833 generation of the @file{ALI} file. This file is marked as being in
4834 error, so it cannot be used for binding purposes, but it does contain
4835 reasonably complete cross-reference information, and thus may be useful
4836 for use by tools (e.g., semantic browsing tools or integrated development
4837 environments) that are driven from the @file{ALI} file. This switch
4838 implies @option{-gnatq}, since the semantic phase must be run to get a
4839 meaningful ALI file.
4840
4841 In addition, if @option{-gnatt} is also specified, then the tree file is
4842 generated even if there are illegalities. It may be useful in this case
4843 to also specify @option{-gnatq} to ensure that full semantic processing
4844 occurs. The resulting tree file can be processed by ASIS, for the purpose
4845 of providing partial information about illegal units, but if the error
4846 causes the tree to be badly malformed, then ASIS may crash during the
4847 analysis.
4848
4849 When @option{-gnatQ} is used and the generated @file{ALI} file is marked as
4850 being in error, @command{gnatmake} will attempt to recompile the source when it
4851 finds such an @file{ALI} file, including with switch @option{-gnatc}.
4852
4853 Note that @option{-gnatQ} has no effect if @option{-gnats} is specified,
4854 since ALI files are never generated if @option{-gnats} is set.
4855
4856 @end table
4857
4858 @node Warning Message Control
4859 @subsection Warning Message Control
4860 @cindex Warning messages
4861 @noindent
4862 In addition to error messages, which correspond to illegalities as defined
4863 in the Ada Reference Manual, the compiler detects two kinds of warning
4864 situations.
4865
4866 First, the compiler considers some constructs suspicious and generates a
4867 warning message to alert you to a possible error. Second, if the
4868 compiler detects a situation that is sure to raise an exception at
4869 run time, it generates a warning message. The following shows an example
4870 of warning messages:
4871 @smallexample
4872 e.adb:4:24: warning: creation of object may raise Storage_Error
4873 e.adb:10:17: warning: static value out of range
4874 e.adb:10:17: warning: "Constraint_Error" will be raised at run time
4875 @end smallexample
4876
4877 @noindent
4878 GNAT considers a large number of situations as appropriate
4879 for the generation of warning messages. As always, warnings are not
4880 definite indications of errors. For example, if you do an out-of-range
4881 assignment with the deliberate intention of raising a
4882 @code{Constraint_Error} exception, then the warning that may be
4883 issued does not indicate an error. Some of the situations for which GNAT
4884 issues warnings (at least some of the time) are given in the following
4885 list. This list is not complete, and new warnings are often added to
4886 subsequent versions of GNAT. The list is intended to give a general idea
4887 of the kinds of warnings that are generated.
4888
4889 @itemize @bullet
4890 @item
4891 Possible infinitely recursive calls
4892
4893 @item
4894 Out-of-range values being assigned
4895
4896 @item
4897 Possible order of elaboration problems
4898
4899 @item
4900 Assertions (pragma Assert) that are sure to fail
4901
4902 @item
4903 Unreachable code
4904
4905 @item
4906 Address clauses with possibly unaligned values, or where an attempt is
4907 made to overlay a smaller variable with a larger one.
4908
4909 @item
4910 Fixed-point type declarations with a null range
4911
4912 @item
4913 Direct_IO or Sequential_IO instantiated with a type that has access values
4914
4915 @item
4916 Variables that are never assigned a value
4917
4918 @item
4919 Variables that are referenced before being initialized
4920
4921 @item
4922 Task entries with no corresponding @code{accept} statement
4923
4924 @item
4925 Duplicate accepts for the same task entry in a @code{select}
4926
4927 @item
4928 Objects that take too much storage
4929
4930 @item
4931 Unchecked conversion between types of differing sizes
4932
4933 @item
4934 Missing @code{return} statement along some execution path in a function
4935
4936 @item
4937 Incorrect (unrecognized) pragmas
4938
4939 @item
4940 Incorrect external names
4941
4942 @item
4943 Allocation from empty storage pool
4944
4945 @item
4946 Potentially blocking operation in protected type
4947
4948 @item
4949 Suspicious parenthesization of expressions
4950
4951 @item
4952 Mismatching bounds in an aggregate
4953
4954 @item
4955 Attempt to return local value by reference
4956
4957 @item
4958 Premature instantiation of a generic body
4959
4960 @item
4961 Attempt to pack aliased components
4962
4963 @item
4964 Out of bounds array subscripts
4965
4966 @item
4967 Wrong length on string assignment
4968
4969 @item
4970 Violations of style rules if style checking is enabled
4971
4972 @item
4973 Unused @code{with} clauses
4974
4975 @item
4976 @code{Bit_Order} usage that does not have any effect
4977
4978 @item
4979 @code{Standard.Duration} used to resolve universal fixed expression
4980
4981 @item
4982 Dereference of possibly null value
4983
4984 @item
4985 Declaration that is likely to cause storage error
4986
4987 @item
4988 Internal GNAT unit @code{with}'ed by application unit
4989
4990 @item
4991 Values known to be out of range at compile time
4992
4993 @item
4994 Unreferenced labels and variables
4995
4996 @item
4997 Address overlays that could clobber memory
4998
4999 @item
5000 Unexpected initialization when address clause present
5001
5002 @item
5003 Bad alignment for address clause
5004
5005 @item
5006 Useless type conversions
5007
5008 @item
5009 Redundant assignment statements and other redundant constructs
5010
5011 @item
5012 Useless exception handlers
5013
5014 @item
5015 Accidental hiding of name by child unit
5016
5017 @item
5018 Access before elaboration detected at compile time
5019
5020 @item
5021 A range in a @code{for} loop that is known to be null or might be null
5022
5023 @end itemize
5024
5025 @noindent
5026 The following section lists compiler switches that are available
5027 to control the handling of warning messages. It is also possible
5028 to exercise much finer control over what warnings are issued and
5029 suppressed using the GNAT pragma Warnings, @xref{Pragma Warnings,,,
5030 gnat_rm, GNAT Reference manual}.
5031
5032 @table @option
5033 @c !sort!
5034 @item -gnatwa
5035 @emph{Activate all optional errors.}
5036 @cindex @option{-gnatwa} (@command{gcc})
5037 This switch activates most optional warning messages, see remaining list
5038 in this section for details on optional warning messages that can be
5039 individually controlled. The warnings that are not turned on by this
5040 switch are
5041 @option{-gnatwd} (implicit dereferencing),
5042 @option{-gnatwh} (hiding),
5043 @option{-gnatwl} (elaboration warnings),
5044 @option{-gnatw.o} (warn on values set by out parameters ignored)
5045 and @option{-gnatwt} (tracking of deleted conditional code).
5046 All other optional warnings are turned on.
5047
5048 @item -gnatwA
5049 @emph{Suppress all optional errors.}
5050 @cindex @option{-gnatwA} (@command{gcc})
5051 This switch suppresses all optional warning messages, see remaining list
5052 in this section for details on optional warning messages that can be
5053 individually controlled.
5054
5055 @item -gnatw.a
5056 @emph{Activate warnings on failing assertions.}
5057 @cindex @option{-gnatw.a} (@command{gcc})
5058 @cindex Assert failures
5059 This switch activates warnings for assertions where the compiler can tell at
5060 compile time that the assertion will fail. Note that this warning is given
5061 even if assertions are disabled. The default is that such warnings are
5062 generated.
5063
5064 @item -gnatw.A
5065 @emph{Suppress warnings on failing assertions.}
5066 @cindex @option{-gnatw.A} (@command{gcc})
5067 @cindex Assert failures
5068 This switch suppresses warnings for assertions where the compiler can tell at
5069 compile time that the assertion will fail.
5070
5071 @item -gnatwb
5072 @emph{Activate warnings on bad fixed values.}
5073 @cindex @option{-gnatwb} (@command{gcc})
5074 @cindex Bad fixed values
5075 @cindex Fixed-point Small value
5076 @cindex Small value
5077 This switch activates warnings for static fixed-point expressions whose
5078 value is not an exact multiple of Small. Such values are implementation
5079 dependent, since an implementation is free to choose either of the multiples
5080 that surround the value. GNAT always chooses the closer one, but this is not
5081 required behavior, and it is better to specify a value that is an exact
5082 multiple, ensuring predictable execution. The default is that such warnings
5083 are not generated.
5084
5085 @item -gnatwB
5086 @emph{Suppress warnings on bad fixed values.}
5087 @cindex @option{-gnatwB} (@command{gcc})
5088 This switch suppresses warnings for static fixed-point expressions whose
5089 value is not an exact multiple of Small.
5090
5091 @item -gnatw.b
5092 @emph{Activate warnings on biased representation.}
5093 @cindex @option{-gnatw.b} (@command{gcc})
5094 @cindex Biased representation
5095 This switch activates warnings when a size clause, value size clause, component
5096 clause, or component size clause forces the use of biased representation for an
5097 integer type (e.g. representing a range of 10..11 in a single bit by using 0/1
5098 to represent 10/11). The default is that such warnings are generated.
5099
5100 @item -gnatw.B
5101 @emph{Suppress warnings on biased representation.}
5102 @cindex @option{-gnatwB} (@command{gcc})
5103 This switch suppresses warnings for representation clauses that force the use
5104 of biased representation.
5105
5106 @item -gnatwc
5107 @emph{Activate warnings on conditionals.}
5108 @cindex @option{-gnatwc} (@command{gcc})
5109 @cindex Conditionals, constant
5110 This switch activates warnings for conditional expressions used in
5111 tests that are known to be True or False at compile time. The default
5112 is that such warnings are not generated.
5113 Note that this warning does
5114 not get issued for the use of boolean variables or constants whose
5115 values are known at compile time, since this is a standard technique
5116 for conditional compilation in Ada, and this would generate too many
5117 false positive warnings.
5118
5119 This warning option also activates a special test for comparisons using
5120 the operators ``>='' and`` <=''.
5121 If the compiler can tell that only the equality condition is possible,
5122 then it will warn that the ``>'' or ``<'' part of the test
5123 is useless and that the operator could be replaced by ``=''.
5124 An example would be comparing a @code{Natural} variable <= 0.
5125
5126 This warning option also generates warnings if
5127 one or both tests is optimized away in a membership test for integer
5128 values if the result can be determined at compile time. Range tests on
5129 enumeration types are not included, since it is common for such tests
5130 to include an end point.
5131
5132 This warning can also be turned on using @option{-gnatwa}.
5133
5134 @item -gnatwC
5135 @emph{Suppress warnings on conditionals.}
5136 @cindex @option{-gnatwC} (@command{gcc})
5137 This switch suppresses warnings for conditional expressions used in
5138 tests that are known to be True or False at compile time.
5139
5140 @item -gnatw.c
5141 @emph{Activate warnings on missing component clauses.}
5142 @cindex @option{-gnatw.c} (@command{gcc})
5143 @cindex Component clause, missing
5144 This switch activates warnings for record components where a record
5145 representation clause is present and has component clauses for the
5146 majority, but not all, of the components. A warning is given for each
5147 component for which no component clause is present.
5148
5149 This warning can also be turned on using @option{-gnatwa}.
5150
5151 @item -gnatw.C
5152 @emph{Suppress warnings on missing component clauses.}
5153 @cindex @option{-gnatwC} (@command{gcc})
5154 This switch suppresses warnings for record components that are
5155 missing a component clause in the situation described above.
5156
5157 @item -gnatwd
5158 @emph{Activate warnings on implicit dereferencing.}
5159 @cindex @option{-gnatwd} (@command{gcc})
5160 If this switch is set, then the use of a prefix of an access type
5161 in an indexed component, slice, or selected component without an
5162 explicit @code{.all} will generate a warning. With this warning
5163 enabled, access checks occur only at points where an explicit
5164 @code{.all} appears in the source code (assuming no warnings are
5165 generated as a result of this switch). The default is that such
5166 warnings are not generated.
5167 Note that @option{-gnatwa} does not affect the setting of
5168 this warning option.
5169
5170 @item -gnatwD
5171 @emph{Suppress warnings on implicit dereferencing.}
5172 @cindex @option{-gnatwD} (@command{gcc})
5173 @cindex Implicit dereferencing
5174 @cindex Dereferencing, implicit
5175 This switch suppresses warnings for implicit dereferences in
5176 indexed components, slices, and selected components.
5177
5178 @item -gnatwe
5179 @emph{Treat warnings as errors.}
5180 @cindex @option{-gnatwe} (@command{gcc})
5181 @cindex Warnings, treat as error
5182 This switch causes warning messages to be treated as errors.
5183 The warning string still appears, but the warning messages are counted
5184 as errors, and prevent the generation of an object file.
5185
5186 @item -gnatw.e
5187 @emph{Activate every optional warning}
5188 @cindex @option{-gnatw.e} (@command{gcc})
5189 @cindex Warnings, activate every optional warning
5190 This switch activates all optional warnings, including those which
5191 are not activated by @code{-gnatwa}.
5192
5193 @item -gnatwf
5194 @emph{Activate warnings on unreferenced formals.}
5195 @cindex @option{-gnatwf} (@command{gcc})
5196 @cindex Formals, unreferenced
5197 This switch causes a warning to be generated if a formal parameter
5198 is not referenced in the body of the subprogram. This warning can
5199 also be turned on using @option{-gnatwa} or @option{-gnatwu}. The
5200 default is that these warnings are not generated.
5201
5202 @item -gnatwF
5203 @emph{Suppress warnings on unreferenced formals.}
5204 @cindex @option{-gnatwF} (@command{gcc})
5205 This switch suppresses warnings for unreferenced formal
5206 parameters. Note that the
5207 combination @option{-gnatwu} followed by @option{-gnatwF} has the
5208 effect of warning on unreferenced entities other than subprogram
5209 formals.
5210
5211 @item -gnatwg
5212 @emph{Activate warnings on unrecognized pragmas.}
5213 @cindex @option{-gnatwg} (@command{gcc})
5214 @cindex Pragmas, unrecognized
5215 This switch causes a warning to be generated if an unrecognized
5216 pragma is encountered. Apart from issuing this warning, the
5217 pragma is ignored and has no effect. This warning can
5218 also be turned on using @option{-gnatwa}. The default
5219 is that such warnings are issued (satisfying the Ada Reference
5220 Manual requirement that such warnings appear).
5221
5222 @item -gnatwG
5223 @emph{Suppress warnings on unrecognized pragmas.}
5224 @cindex @option{-gnatwG} (@command{gcc})
5225 This switch suppresses warnings for unrecognized pragmas.
5226
5227 @item -gnatwh
5228 @emph{Activate warnings on hiding.}
5229 @cindex @option{-gnatwh} (@command{gcc})
5230 @cindex Hiding of Declarations
5231 This switch activates warnings on hiding declarations.
5232 A declaration is considered hiding
5233 if it is for a non-overloadable entity, and it declares an entity with the
5234 same name as some other entity that is directly or use-visible. The default
5235 is that such warnings are not generated.
5236 Note that @option{-gnatwa} does not affect the setting of this warning option.
5237
5238 @item -gnatwH
5239 @emph{Suppress warnings on hiding.}
5240 @cindex @option{-gnatwH} (@command{gcc})
5241 This switch suppresses warnings on hiding declarations.
5242
5243 @item -gnatwi
5244 @emph{Activate warnings on implementation units.}
5245 @cindex @option{-gnatwi} (@command{gcc})
5246 This switch activates warnings for a @code{with} of an internal GNAT
5247 implementation unit, defined as any unit from the @code{Ada},
5248 @code{Interfaces}, @code{GNAT},
5249 ^^@code{DEC},^ or @code{System}
5250 hierarchies that is not
5251 documented in either the Ada Reference Manual or the GNAT
5252 Programmer's Reference Manual. Such units are intended only
5253 for internal implementation purposes and should not be @code{with}'ed
5254 by user programs. The default is that such warnings are generated
5255 This warning can also be turned on using @option{-gnatwa}.
5256
5257 @item -gnatwI
5258 @emph{Disable warnings on implementation units.}
5259 @cindex @option{-gnatwI} (@command{gcc})
5260 This switch disables warnings for a @code{with} of an internal GNAT
5261 implementation unit.
5262
5263 @item -gnatwj
5264 @emph{Activate warnings on obsolescent features (Annex J).}
5265 @cindex @option{-gnatwj} (@command{gcc})
5266 @cindex Features, obsolescent
5267 @cindex Obsolescent features
5268 If this warning option is activated, then warnings are generated for
5269 calls to subprograms marked with @code{pragma Obsolescent} and
5270 for use of features in Annex J of the Ada Reference Manual. In the
5271 case of Annex J, not all features are flagged. In particular use
5272 of the renamed packages (like @code{Text_IO}) and use of package
5273 @code{ASCII} are not flagged, since these are very common and
5274 would generate many annoying positive warnings. The default is that
5275 such warnings are not generated. This warning is also turned on by
5276 the use of @option{-gnatwa}.
5277
5278 In addition to the above cases, warnings are also generated for
5279 GNAT features that have been provided in past versions but which
5280 have been superseded (typically by features in the new Ada standard).
5281 For example, @code{pragma Ravenscar} will be flagged since its
5282 function is replaced by @code{pragma Profile(Ravenscar)}.
5283
5284 Note that this warning option functions differently from the
5285 restriction @code{No_Obsolescent_Features} in two respects.
5286 First, the restriction applies only to annex J features.
5287 Second, the restriction does flag uses of package @code{ASCII}.
5288
5289 @item -gnatwJ
5290 @emph{Suppress warnings on obsolescent features (Annex J).}
5291 @cindex @option{-gnatwJ} (@command{gcc})
5292 This switch disables warnings on use of obsolescent features.
5293
5294 @item -gnatwk
5295 @emph{Activate warnings on variables that could be constants.}
5296 @cindex @option{-gnatwk} (@command{gcc})
5297 This switch activates warnings for variables that are initialized but
5298 never modified, and then could be declared constants. The default is that
5299 such warnings are not given.
5300 This warning can also be turned on using @option{-gnatwa}.
5301
5302 @item -gnatwK
5303 @emph{Suppress warnings on variables that could be constants.}
5304 @cindex @option{-gnatwK} (@command{gcc})
5305 This switch disables warnings on variables that could be declared constants.
5306
5307 @item -gnatwl
5308 @emph{Activate warnings for elaboration pragmas.}
5309 @cindex @option{-gnatwl} (@command{gcc})
5310 @cindex Elaboration, warnings
5311 This switch activates warnings on missing
5312 @code{Elaborate_All} and @code{Elaborate} pragmas.
5313 See the section in this guide on elaboration checking for details on
5314 when such pragmas should be used. In dynamic elaboration mode, this switch
5315 generations warnings about the need to add elaboration pragmas. Note however,
5316 that if you blindly follow these warnings, and add @code{Elaborate_All}
5317 warnings wherever they are recommended, you basically end up with the
5318 equivalent of the static elaboration model, which may not be what you want for
5319 legacy code for which the static model does not work.
5320
5321 For the static model, the messages generated are labeled "info:" (for
5322 information messages). They are not warnings to add elaboration pragmas,
5323 merely informational messages showing what implicit elaboration pragmas
5324 have been added, for use in analyzing elaboration circularity problems.
5325
5326 Warnings are also generated if you
5327 are using the static mode of elaboration, and a @code{pragma Elaborate}
5328 is encountered. The default is that such warnings
5329 are not generated.
5330 This warning is not automatically turned on by the use of @option{-gnatwa}.
5331
5332 @item -gnatwL
5333 @emph{Suppress warnings for elaboration pragmas.}
5334 @cindex @option{-gnatwL} (@command{gcc})
5335 This switch suppresses warnings on missing Elaborate and Elaborate_All pragmas.
5336 See the section in this guide on elaboration checking for details on
5337 when such pragmas should be used.
5338
5339 @item -gnatwm
5340 @emph{Activate warnings on modified but unreferenced variables.}
5341 @cindex @option{-gnatwm} (@command{gcc})
5342 This switch activates warnings for variables that are assigned (using
5343 an initialization value or with one or more assignment statements) but
5344 whose value is never read. The warning is suppressed for volatile
5345 variables and also for variables that are renamings of other variables
5346 or for which an address clause is given.
5347 This warning can also be turned on using @option{-gnatwa}.
5348 The default is that these warnings are not given.
5349
5350 @item -gnatwM
5351 @emph{Disable warnings on modified but unreferenced variables.}
5352 @cindex @option{-gnatwM} (@command{gcc})
5353 This switch disables warnings for variables that are assigned or
5354 initialized, but never read.
5355
5356 @item -gnatw.m
5357 @emph{Activate warnings on suspicious modulus values.}
5358 @cindex @option{-gnatw.m} (@command{gcc})
5359 This switch activates warnings for modulus values that seem suspicious.
5360 The cases caught are where the size is the same as the modulus (e.g.
5361 a modulus of 7 with a size of 7 bits), and modulus values of 32 or 64
5362 with no size clause. The guess in both cases is that 2**x was intended
5363 rather than x. The default is that these warnings are given.
5364
5365 @item -gnatw.M
5366 @emph{Disable warnings on suspicious modulus values.}
5367 @cindex @option{-gnatw.M} (@command{gcc})
5368 This switch disables warnings for suspicious modulus values.
5369
5370 @item -gnatwn
5371 @emph{Set normal warnings mode.}
5372 @cindex @option{-gnatwn} (@command{gcc})
5373 This switch sets normal warning mode, in which enabled warnings are
5374 issued and treated as warnings rather than errors. This is the default
5375 mode. the switch @option{-gnatwn} can be used to cancel the effect of
5376 an explicit @option{-gnatws} or
5377 @option{-gnatwe}. It also cancels the effect of the
5378 implicit @option{-gnatwe} that is activated by the
5379 use of @option{-gnatg}.
5380
5381 @item -gnatwo
5382 @emph{Activate warnings on address clause overlays.}
5383 @cindex @option{-gnatwo} (@command{gcc})
5384 @cindex Address Clauses, warnings
5385 This switch activates warnings for possibly unintended initialization
5386 effects of defining address clauses that cause one variable to overlap
5387 another. The default is that such warnings are generated.
5388 This warning can also be turned on using @option{-gnatwa}.
5389
5390 @item -gnatwO
5391 @emph{Suppress warnings on address clause overlays.}
5392 @cindex @option{-gnatwO} (@command{gcc})
5393 This switch suppresses warnings on possibly unintended initialization
5394 effects of defining address clauses that cause one variable to overlap
5395 another.
5396
5397 @item -gnatw.o
5398 @emph{Activate warnings on modified but unreferenced out parameters.}
5399 @cindex @option{-gnatw.o} (@command{gcc})
5400 This switch activates warnings for variables that are modified by using
5401 them as actuals for a call to a procedure with an out mode formal, where
5402 the resulting assigned value is never read. It is applicable in the case
5403 where there is more than one out mode formal. If there is only one out
5404 mode formal, the warning is issued by default (controlled by -gnatwu).
5405 The warning is suppressed for volatile
5406 variables and also for variables that are renamings of other variables
5407 or for which an address clause is given.
5408 The default is that these warnings are not given. Note that this warning
5409 is not included in -gnatwa, it must be activated explicitly.
5410
5411 @item -gnatw.O
5412 @emph{Disable warnings on modified but unreferenced out parameters.}
5413 @cindex @option{-gnatw.O} (@command{gcc})
5414 This switch suppresses warnings for variables that are modified by using
5415 them as actuals for a call to a procedure with an out mode formal, where
5416 the resulting assigned value is never read.
5417
5418 @item -gnatwp
5419 @emph{Activate warnings on ineffective pragma Inlines.}
5420 @cindex @option{-gnatwp} (@command{gcc})
5421 @cindex Inlining, warnings
5422 This switch activates warnings for failure of front end inlining
5423 (activated by @option{-gnatN}) to inline a particular call. There are
5424 many reasons for not being able to inline a call, including most
5425 commonly that the call is too complex to inline. The default is
5426 that such warnings are not given.
5427 This warning can also be turned on using @option{-gnatwa}.
5428 Warnings on ineffective inlining by the gcc back-end can be activated
5429 separately, using the gcc switch -Winline.
5430
5431 @item -gnatwP
5432 @emph{Suppress warnings on ineffective pragma Inlines.}
5433 @cindex @option{-gnatwP} (@command{gcc})
5434 This switch suppresses warnings on ineffective pragma Inlines. If the
5435 inlining mechanism cannot inline a call, it will simply ignore the
5436 request silently.
5437
5438 @item -gnatw.p
5439 @emph{Activate warnings on parameter ordering.}
5440 @cindex @option{-gnatw.p} (@command{gcc})
5441 @cindex Parameter order, warnings
5442 This switch activates warnings for cases of suspicious parameter
5443 ordering when the list of arguments are all simple identifiers that
5444 match the names of the formals, but are in a different order. The
5445 warning is suppressed if any use of named parameter notation is used,
5446 so this is the appropriate way to suppress a false positive (and
5447 serves to emphasize that the "misordering" is deliberate). The
5448 default is
5449 that such warnings are not given.
5450 This warning can also be turned on using @option{-gnatwa}.
5451
5452 @item -gnatw.P
5453 @emph{Suppress warnings on parameter ordering.}
5454 @cindex @option{-gnatw.P} (@command{gcc})
5455 This switch suppresses warnings on cases of suspicious parameter
5456 ordering.
5457
5458 @item -gnatwq
5459 @emph{Activate warnings on questionable missing parentheses.}
5460 @cindex @option{-gnatwq} (@command{gcc})
5461 @cindex Parentheses, warnings
5462 This switch activates warnings for cases where parentheses are not used and
5463 the result is potential ambiguity from a readers point of view. For example
5464 (not a > b) when a and b are modular means ((not a) > b) and very likely the
5465 programmer intended (not (a > b)). Similarly (-x mod 5) means (-(x mod 5)) and
5466 quite likely ((-x) mod 5) was intended. In such situations it seems best to
5467 follow the rule of always parenthesizing to make the association clear, and
5468 this warning switch warns if such parentheses are not present. The default
5469 is that these warnings are given.
5470 This warning can also be turned on using @option{-gnatwa}.
5471
5472 @item -gnatwQ
5473 @emph{Suppress warnings on questionable missing parentheses.}
5474 @cindex @option{-gnatwQ} (@command{gcc})
5475 This switch suppresses warnings for cases where the association is not
5476 clear and the use of parentheses is preferred.
5477
5478 @item -gnatwr
5479 @emph{Activate warnings on redundant constructs.}
5480 @cindex @option{-gnatwr} (@command{gcc})
5481 This switch activates warnings for redundant constructs. The following
5482 is the current list of constructs regarded as redundant:
5483
5484 @itemize @bullet
5485 @item
5486 Assignment of an item to itself.
5487 @item
5488 Type conversion that converts an expression to its own type.
5489 @item
5490 Use of the attribute @code{Base} where @code{typ'Base} is the same
5491 as @code{typ}.
5492 @item
5493 Use of pragma @code{Pack} when all components are placed by a record
5494 representation clause.
5495 @item
5496 Exception handler containing only a reraise statement (raise with no
5497 operand) which has no effect.
5498 @item
5499 Use of the operator abs on an operand that is known at compile time
5500 to be non-negative
5501 @item
5502 Comparison of boolean expressions to an explicit True value.
5503 @end itemize
5504
5505 This warning can also be turned on using @option{-gnatwa}.
5506 The default is that warnings for redundant constructs are not given.
5507
5508 @item -gnatwR
5509 @emph{Suppress warnings on redundant constructs.}
5510 @cindex @option{-gnatwR} (@command{gcc})
5511 This switch suppresses warnings for redundant constructs.
5512
5513 @item -gnatw.r
5514 @emph{Activate warnings for object renaming function.}
5515 @cindex @option{-gnatw.r} (@command{gcc})
5516 This switch activates warnings for an object renaming that renames a
5517 function call, which is equivalent to a constant declaration (as
5518 opposed to renaming the function itself). The default is that these
5519 warnings are given. This warning can also be turned on using
5520 @option{-gnatwa}.
5521
5522 @item -gnatw.R
5523 @emph{Suppress warnings for object renaming function.}
5524 @cindex @option{-gnatwT} (@command{gcc})
5525 This switch suppresses warnings for object renaming function.
5526
5527 @item -gnatws
5528 @emph{Suppress all warnings.}
5529 @cindex @option{-gnatws} (@command{gcc})
5530 This switch completely suppresses the
5531 output of all warning messages from the GNAT front end.
5532 Note that it does not suppress warnings from the @command{gcc} back end.
5533 To suppress these back end warnings as well, use the switch @option{-w}
5534 in addition to @option{-gnatws}.
5535
5536 @item -gnatwt
5537 @emph{Activate warnings for tracking of deleted conditional code.}
5538 @cindex @option{-gnatwt} (@command{gcc})
5539 @cindex Deactivated code, warnings
5540 @cindex Deleted code, warnings
5541 This switch activates warnings for tracking of code in conditionals (IF and
5542 CASE statements) that is detected to be dead code which cannot be executed, and
5543 which is removed by the front end. This warning is off by default, and is not
5544 turned on by @option{-gnatwa}, it has to be turned on explicitly. This may be
5545 useful for detecting deactivated code in certified applications.
5546
5547 @item -gnatwT
5548 @emph{Suppress warnings for tracking of deleted conditional code.}
5549 @cindex @option{-gnatwT} (@command{gcc})
5550 This switch suppresses warnings for tracking of deleted conditional code.
5551
5552 @item -gnatwu
5553 @emph{Activate warnings on unused entities.}
5554 @cindex @option{-gnatwu} (@command{gcc})
5555 This switch activates warnings to be generated for entities that
5556 are declared but not referenced, and for units that are @code{with}'ed
5557 and not
5558 referenced. In the case of packages, a warning is also generated if
5559 no entities in the package are referenced. This means that if the package
5560 is referenced but the only references are in @code{use}
5561 clauses or @code{renames}
5562 declarations, a warning is still generated. A warning is also generated
5563 for a generic package that is @code{with}'ed but never instantiated.
5564 In the case where a package or subprogram body is compiled, and there
5565 is a @code{with} on the corresponding spec
5566 that is only referenced in the body,
5567 a warning is also generated, noting that the
5568 @code{with} can be moved to the body. The default is that
5569 such warnings are not generated.
5570 This switch also activates warnings on unreferenced formals
5571 (it includes the effect of @option{-gnatwf}).
5572 This warning can also be turned on using @option{-gnatwa}.
5573
5574 @item -gnatwU
5575 @emph{Suppress warnings on unused entities.}
5576 @cindex @option{-gnatwU} (@command{gcc})
5577 This switch suppresses warnings for unused entities and packages.
5578 It also turns off warnings on unreferenced formals (and thus includes
5579 the effect of @option{-gnatwF}).
5580
5581 @item -gnatwv
5582 @emph{Activate warnings on unassigned variables.}
5583 @cindex @option{-gnatwv} (@command{gcc})
5584 @cindex Unassigned variable warnings
5585 This switch activates warnings for access to variables which
5586 may not be properly initialized. The default is that
5587 such warnings are generated.
5588 This warning can also be turned on using @option{-gnatwa}.
5589
5590 @item -gnatwV
5591 @emph{Suppress warnings on unassigned variables.}
5592 @cindex @option{-gnatwV} (@command{gcc})
5593 This switch suppresses warnings for access to variables which
5594 may not be properly initialized.
5595 For variables of a composite type, the warning can also be suppressed in
5596 Ada 2005 by using a default initialization with a box. For example, if
5597 Table is an array of records whose components are only partially uninitialized,
5598 then the following code:
5599
5600 @smallexample @c ada
5601 Tab : Table := (others => <>);
5602 @end smallexample
5603
5604 will suppress warnings on subsequent statements that access components
5605 of variable Tab.
5606
5607 @item -gnatww
5608 @emph{Activate warnings on wrong low bound assumption.}
5609 @cindex @option{-gnatww} (@command{gcc})
5610 @cindex String indexing warnings
5611 This switch activates warnings for indexing an unconstrained string parameter
5612 with a literal or S'Length. This is a case where the code is assuming that the
5613 low bound is one, which is in general not true (for example when a slice is
5614 passed). The default is that such warnings are generated.
5615 This warning can also be turned on using @option{-gnatwa}.
5616
5617 @item -gnatwW
5618 @emph{Suppress warnings on wrong low bound assumption.}
5619 @cindex @option{-gnatwW} (@command{gcc})
5620 This switch suppresses warnings for indexing an unconstrained string parameter
5621 with a literal or S'Length. Note that this warning can also be suppressed
5622 in a particular case by adding an
5623 assertion that the lower bound is 1,
5624 as shown in the following example.
5625
5626 @smallexample @c ada
5627 procedure K (S : String) is
5628 pragma Assert (S'First = 1);
5629 @dots{}
5630 @end smallexample
5631
5632 @item -gnatw.w
5633 @emph{Activate warnings on unnecessary Warnings Off pragmas}
5634 @cindex @option{-gnatw.w} (@command{gcc})
5635 @cindex Warnings Off control
5636 This switch activates warnings for use of @code{pragma Warnings (Off, entity}
5637 where either the pragma is entirely useless (because it suppresses no
5638 warnings), or it could be replaced by @code{pragma Unreferenced} or
5639 @code{pragma Unmodified}.The default is that these warnings are not given.
5640 Note that this warning is not included in -gnatwa, it must be
5641 activated explicitly.
5642
5643 @item -gnatw.W
5644 @emph{Suppress warnings on unnecessary Warnings Off pragmas}
5645 @cindex @option{-gnatw.W} (@command{gcc})
5646 This switch suppresses warnings for use of @code{pragma Warnings (Off, entity}.
5647
5648 @item -gnatwx
5649 @emph{Activate warnings on Export/Import pragmas.}
5650 @cindex @option{-gnatwx} (@command{gcc})
5651 @cindex Export/Import pragma warnings
5652 This switch activates warnings on Export/Import pragmas when
5653 the compiler detects a possible conflict between the Ada and
5654 foreign language calling sequences. For example, the use of
5655 default parameters in a convention C procedure is dubious
5656 because the C compiler cannot supply the proper default, so
5657 a warning is issued. The default is that such warnings are
5658 generated.
5659 This warning can also be turned on using @option{-gnatwa}.
5660
5661 @item -gnatwX
5662 @emph{Suppress warnings on Export/Import pragmas.}
5663 @cindex @option{-gnatwX} (@command{gcc})
5664 This switch suppresses warnings on Export/Import pragmas.
5665 The sense of this is that you are telling the compiler that
5666 you know what you are doing in writing the pragma, and it
5667 should not complain at you.
5668
5669 @item -gnatw.x
5670 @emph{Activate warnings for No_Exception_Propagation mode.}
5671 @cindex @option{-gnatwm} (@command{gcc})
5672 This switch activates warnings for exception usage when pragma Restrictions
5673 (No_Exception_Propagation) is in effect. Warnings are given for implicit or
5674 explicit exception raises which are not covered by a local handler, and for
5675 exception handlers which do not cover a local raise. The default is that these
5676 warnings are not given.
5677
5678 @item -gnatw.X
5679 @emph{Disable warnings for No_Exception_Propagation mode.}
5680 This switch disables warnings for exception usage when pragma Restrictions
5681 (No_Exception_Propagation) is in effect.
5682
5683 @item -gnatwy
5684 @emph{Activate warnings for Ada 2005 compatibility issues.}
5685 @cindex @option{-gnatwy} (@command{gcc})
5686 @cindex Ada 2005 compatibility issues warnings
5687 For the most part Ada 2005 is upwards compatible with Ada 95,
5688 but there are some exceptions (for example the fact that
5689 @code{interface} is now a reserved word in Ada 2005). This
5690 switch activates several warnings to help in identifying
5691 and correcting such incompatibilities. The default is that
5692 these warnings are generated. Note that at one point Ada 2005
5693 was called Ada 0Y, hence the choice of character.
5694 This warning can also be turned on using @option{-gnatwa}.
5695
5696 @item -gnatwY
5697 @emph{Disable warnings for Ada 2005 compatibility issues.}
5698 @cindex @option{-gnatwY} (@command{gcc})
5699 @cindex Ada 2005 compatibility issues warnings
5700 This switch suppresses several warnings intended to help in identifying
5701 incompatibilities between Ada 95 and Ada 2005.
5702
5703 @item -gnatwz
5704 @emph{Activate warnings on unchecked conversions.}
5705 @cindex @option{-gnatwz} (@command{gcc})
5706 @cindex Unchecked_Conversion warnings
5707 This switch activates warnings for unchecked conversions
5708 where the types are known at compile time to have different
5709 sizes. The default
5710 is that such warnings are generated. Warnings are also
5711 generated for subprogram pointers with different conventions,
5712 and, on VMS only, for data pointers with different conventions.
5713 This warning can also be turned on using @option{-gnatwa}.
5714
5715 @item -gnatwZ
5716 @emph{Suppress warnings on unchecked conversions.}
5717 @cindex @option{-gnatwZ} (@command{gcc})
5718 This switch suppresses warnings for unchecked conversions
5719 where the types are known at compile time to have different
5720 sizes or conventions.
5721
5722 @item ^-Wunused^WARNINGS=UNUSED^
5723 @cindex @option{-Wunused}
5724 The warnings controlled by the @option{-gnatw} switch are generated by
5725 the front end of the compiler. The @option{GCC} back end can provide
5726 additional warnings and they are controlled by the @option{-W} switch.
5727 For example, @option{^-Wunused^WARNINGS=UNUSED^} activates back end
5728 warnings for entities that are declared but not referenced.
5729
5730 @item ^-Wuninitialized^WARNINGS=UNINITIALIZED^
5731 @cindex @option{-Wuninitialized}
5732 Similarly, @option{^-Wuninitialized^WARNINGS=UNINITIALIZED^} activates
5733 the back end warning for uninitialized variables. This switch must be
5734 used in conjunction with an optimization level greater than zero.
5735
5736 @item ^-Wall^/ALL_BACK_END_WARNINGS^
5737 @cindex @option{-Wall}
5738 This switch enables all the above warnings from the @option{GCC} back end.
5739 The code generator detects a number of warning situations that are missed
5740 by the @option{GNAT} front end, and this switch can be used to activate them.
5741 The use of this switch also sets the default front end warning mode to
5742 @option{-gnatwa}, that is, most front end warnings activated as well.
5743
5744 @item ^-w^/NO_BACK_END_WARNINGS^
5745 @cindex @option{-w}
5746 Conversely, this switch suppresses warnings from the @option{GCC} back end.
5747 The use of this switch also sets the default front end warning mode to
5748 @option{-gnatws}, that is, front end warnings suppressed as well.
5749
5750 @end table
5751
5752 @noindent
5753 @ifclear vms
5754 A string of warning parameters can be used in the same parameter. For example:
5755
5756 @smallexample
5757 -gnatwaLe
5758 @end smallexample
5759
5760 @noindent
5761 will turn on all optional warnings except for elaboration pragma warnings,
5762 and also specify that warnings should be treated as errors.
5763 @end ifclear
5764 When no switch @option{^-gnatw^/WARNINGS^} is used, this is equivalent to:
5765
5766 @table @option
5767 @c !sort!
5768 @item -gnatwC
5769 @item -gnatwD
5770 @item -gnatwF
5771 @item -gnatwg
5772 @item -gnatwH
5773 @item -gnatwi
5774 @item -gnatwJ
5775 @item -gnatwK
5776 @item -gnatwL
5777 @item -gnatwM
5778 @item -gnatwn
5779 @item -gnatwo
5780 @item -gnatwP
5781 @item -gnatwR
5782 @item -gnatwU
5783 @item -gnatwv
5784 @item -gnatwz
5785 @item -gnatwx
5786
5787 @end table
5788
5789 @node Debugging and Assertion Control
5790 @subsection Debugging and Assertion Control
5791
5792 @table @option
5793 @item -gnata
5794 @cindex @option{-gnata} (@command{gcc})
5795 @findex Assert
5796 @findex Debug
5797 @cindex Assertions
5798
5799 @noindent
5800 The pragmas @code{Assert} and @code{Debug} normally have no effect and
5801 are ignored. This switch, where @samp{a} stands for assert, causes
5802 @code{Assert} and @code{Debug} pragmas to be activated.
5803
5804 The pragmas have the form:
5805
5806 @smallexample
5807 @cartouche
5808 @b{pragma} Assert (@var{Boolean-expression} @r{[},
5809 @var{static-string-expression}@r{]})
5810 @b{pragma} Debug (@var{procedure call})
5811 @end cartouche
5812 @end smallexample
5813
5814 @noindent
5815 The @code{Assert} pragma causes @var{Boolean-expression} to be tested.
5816 If the result is @code{True}, the pragma has no effect (other than
5817 possible side effects from evaluating the expression). If the result is
5818 @code{False}, the exception @code{Assert_Failure} declared in the package
5819 @code{System.Assertions} is
5820 raised (passing @var{static-string-expression}, if present, as the
5821 message associated with the exception). If no string expression is
5822 given the default is a string giving the file name and line number
5823 of the pragma.
5824
5825 The @code{Debug} pragma causes @var{procedure} to be called. Note that
5826 @code{pragma Debug} may appear within a declaration sequence, allowing
5827 debugging procedures to be called between declarations.
5828
5829 @ifset vms
5830 @item /DEBUG@r{[}=debug-level@r{]}
5831 @itemx /NODEBUG
5832 Specifies how much debugging information is to be included in
5833 the resulting object file where 'debug-level' is one of the following:
5834 @table @code
5835 @item TRACEBACK
5836 Include both debugger symbol records and traceback
5837 the object file.
5838 This is the default setting.
5839 @item ALL
5840 Include both debugger symbol records and traceback in
5841 object file.
5842 @item NONE
5843 Excludes both debugger symbol records and traceback
5844 the object file. Same as /NODEBUG.
5845 @item SYMBOLS
5846 Includes only debugger symbol records in the object
5847 file. Note that this doesn't include traceback information.
5848 @end table
5849 @end ifset
5850 @end table
5851
5852 @node Validity Checking
5853 @subsection Validity Checking
5854 @findex Validity Checking
5855
5856 @noindent
5857 The Ada Reference Manual has specific requirements for checking
5858 for invalid values. In particular, RM 13.9.1 requires that the
5859 evaluation of invalid values (for example from unchecked conversions),
5860 not result in erroneous execution. In GNAT, the result of such an
5861 evaluation in normal default mode is to either use the value
5862 unmodified, or to raise Constraint_Error in those cases where use
5863 of the unmodified value would cause erroneous execution. The cases
5864 where unmodified values might lead to erroneous execution are case
5865 statements (where a wild jump might result from an invalid value),
5866 and subscripts on the left hand side (where memory corruption could
5867 occur as a result of an invalid value).
5868
5869 The @option{-gnatB} switch tells the compiler to assume that all
5870 values are valid (that is, within their declared subtype range)
5871 except in the context of a use of the Valid attribute. This means
5872 the compiler can generate more efficient code, since the range
5873 of values is better known at compile time.
5874
5875 The @option{-gnatV^@var{x}^^} switch allows more control over the validity
5876 checking mode.
5877 @ifclear vms
5878 The @code{x} argument is a string of letters that
5879 indicate validity checks that are performed or not performed in addition
5880 to the default checks described above.
5881 @end ifclear
5882 @ifset vms
5883 The options allowed for this qualifier
5884 indicate validity checks that are performed or not performed in addition
5885 to the default checks described above.
5886 @end ifset
5887
5888 @table @option
5889 @c !sort!
5890 @item -gnatVa
5891 @emph{All validity checks.}
5892 @cindex @option{-gnatVa} (@command{gcc})
5893 All validity checks are turned on.
5894 @ifclear vms
5895 That is, @option{-gnatVa} is
5896 equivalent to @option{gnatVcdfimorst}.
5897 @end ifclear
5898
5899 @item -gnatVc
5900 @emph{Validity checks for copies.}
5901 @cindex @option{-gnatVc} (@command{gcc})
5902 The right hand side of assignments, and the initializing values of
5903 object declarations are validity checked.
5904
5905 @item -gnatVd
5906 @emph{Default (RM) validity checks.}
5907 @cindex @option{-gnatVd} (@command{gcc})
5908 Some validity checks are done by default following normal Ada semantics
5909 (RM 13.9.1 (9-11)).
5910 A check is done in case statements that the expression is within the range
5911 of the subtype. If it is not, Constraint_Error is raised.
5912 For assignments to array components, a check is done that the expression used
5913 as index is within the range. If it is not, Constraint_Error is raised.
5914 Both these validity checks may be turned off using switch @option{-gnatVD}.
5915 They are turned on by default. If @option{-gnatVD} is specified, a subsequent
5916 switch @option{-gnatVd} will leave the checks turned on.
5917 Switch @option{-gnatVD} should be used only if you are sure that all such
5918 expressions have valid values. If you use this switch and invalid values
5919 are present, then the program is erroneous, and wild jumps or memory
5920 overwriting may occur.
5921
5922 @item -gnatVe
5923 @emph{Validity checks for elementary components.}
5924 @cindex @option{-gnatVe} (@command{gcc})
5925 In the absence of this switch, assignments to record or array components are
5926 not validity checked, even if validity checks for assignments generally
5927 (@option{-gnatVc}) are turned on. In Ada, assignment of composite values do not
5928 require valid data, but assignment of individual components does. So for
5929 example, there is a difference between copying the elements of an array with a
5930 slice assignment, compared to assigning element by element in a loop. This
5931 switch allows you to turn off validity checking for components, even when they
5932 are assigned component by component.
5933
5934 @item -gnatVf
5935 @emph{Validity checks for floating-point values.}
5936 @cindex @option{-gnatVf} (@command{gcc})
5937 In the absence of this switch, validity checking occurs only for discrete
5938 values. If @option{-gnatVf} is specified, then validity checking also applies
5939 for floating-point values, and NaNs and infinities are considered invalid,
5940 as well as out of range values for constrained types. Note that this means
5941 that standard IEEE infinity mode is not allowed. The exact contexts
5942 in which floating-point values are checked depends on the setting of other
5943 options. For example,
5944 @option{^-gnatVif^VALIDITY_CHECKING=(IN_PARAMS,FLOATS)^} or
5945 @option{^-gnatVfi^VALIDITY_CHECKING=(FLOATS,IN_PARAMS)^}
5946 (the order does not matter) specifies that floating-point parameters of mode
5947 @code{in} should be validity checked.
5948
5949 @item -gnatVi
5950 @emph{Validity checks for @code{in} mode parameters}
5951 @cindex @option{-gnatVi} (@command{gcc})
5952 Arguments for parameters of mode @code{in} are validity checked in function
5953 and procedure calls at the point of call.
5954
5955 @item -gnatVm
5956 @emph{Validity checks for @code{in out} mode parameters.}
5957 @cindex @option{-gnatVm} (@command{gcc})
5958 Arguments for parameters of mode @code{in out} are validity checked in
5959 procedure calls at the point of call. The @code{'m'} here stands for
5960 modify, since this concerns parameters that can be modified by the call.
5961 Note that there is no specific option to test @code{out} parameters,
5962 but any reference within the subprogram will be tested in the usual
5963 manner, and if an invalid value is copied back, any reference to it
5964 will be subject to validity checking.
5965
5966 @item -gnatVn
5967 @emph{No validity checks.}
5968 @cindex @option{-gnatVn} (@command{gcc})
5969 This switch turns off all validity checking, including the default checking
5970 for case statements and left hand side subscripts. Note that the use of
5971 the switch @option{-gnatp} suppresses all run-time checks, including
5972 validity checks, and thus implies @option{-gnatVn}. When this switch
5973 is used, it cancels any other @option{-gnatV} previously issued.
5974
5975 @item -gnatVo
5976 @emph{Validity checks for operator and attribute operands.}
5977 @cindex @option{-gnatVo} (@command{gcc})
5978 Arguments for predefined operators and attributes are validity checked.
5979 This includes all operators in package @code{Standard},
5980 the shift operators defined as intrinsic in package @code{Interfaces}
5981 and operands for attributes such as @code{Pos}. Checks are also made
5982 on individual component values for composite comparisons, and on the
5983 expressions in type conversions and qualified expressions. Checks are
5984 also made on explicit ranges using @samp{..} (e.g.@: slices, loops etc).
5985
5986 @item -gnatVp
5987 @emph{Validity checks for parameters.}
5988 @cindex @option{-gnatVp} (@command{gcc})
5989 This controls the treatment of parameters within a subprogram (as opposed
5990 to @option{-gnatVi} and @option{-gnatVm} which control validity testing
5991 of parameters on a call. If either of these call options is used, then
5992 normally an assumption is made within a subprogram that the input arguments
5993 have been validity checking at the point of call, and do not need checking
5994 again within a subprogram). If @option{-gnatVp} is set, then this assumption
5995 is not made, and parameters are not assumed to be valid, so their validity
5996 will be checked (or rechecked) within the subprogram.
5997
5998 @item -gnatVr
5999 @emph{Validity checks for function returns.}
6000 @cindex @option{-gnatVr} (@command{gcc})
6001 The expression in @code{return} statements in functions is validity
6002 checked.
6003
6004 @item -gnatVs
6005 @emph{Validity checks for subscripts.}
6006 @cindex @option{-gnatVs} (@command{gcc})
6007 All subscripts expressions are checked for validity, whether they appear
6008 on the right side or left side (in default mode only left side subscripts
6009 are validity checked).
6010
6011 @item -gnatVt
6012 @emph{Validity checks for tests.}
6013 @cindex @option{-gnatVt} (@command{gcc})
6014 Expressions used as conditions in @code{if}, @code{while} or @code{exit}
6015 statements are checked, as well as guard expressions in entry calls.
6016
6017 @end table
6018
6019 @noindent
6020 The @option{-gnatV} switch may be followed by
6021 ^a string of letters^a list of options^
6022 to turn on a series of validity checking options.
6023 For example,
6024 @option{^-gnatVcr^/VALIDITY_CHECKING=(COPIES, RETURNS)^}
6025 specifies that in addition to the default validity checking, copies and
6026 function return expressions are to be validity checked.
6027 In order to make it easier
6028 to specify the desired combination of effects,
6029 @ifclear vms
6030 the upper case letters @code{CDFIMORST} may
6031 be used to turn off the corresponding lower case option.
6032 @end ifclear
6033 @ifset vms
6034 the prefix @code{NO} on an option turns off the corresponding validity
6035 checking:
6036 @itemize @bullet
6037 @item @code{NOCOPIES}
6038 @item @code{NODEFAULT}
6039 @item @code{NOFLOATS}
6040 @item @code{NOIN_PARAMS}
6041 @item @code{NOMOD_PARAMS}
6042 @item @code{NOOPERANDS}
6043 @item @code{NORETURNS}
6044 @item @code{NOSUBSCRIPTS}
6045 @item @code{NOTESTS}
6046 @end itemize
6047 @end ifset
6048 Thus
6049 @option{^-gnatVaM^/VALIDITY_CHECKING=(ALL, NOMOD_PARAMS)^}
6050 turns on all validity checking options except for
6051 checking of @code{@b{in out}} procedure arguments.
6052
6053 The specification of additional validity checking generates extra code (and
6054 in the case of @option{-gnatVa} the code expansion can be substantial).
6055 However, these additional checks can be very useful in detecting
6056 uninitialized variables, incorrect use of unchecked conversion, and other
6057 errors leading to invalid values. The use of pragma @code{Initialize_Scalars}
6058 is useful in conjunction with the extra validity checking, since this
6059 ensures that wherever possible uninitialized variables have invalid values.
6060
6061 See also the pragma @code{Validity_Checks} which allows modification of
6062 the validity checking mode at the program source level, and also allows for
6063 temporary disabling of validity checks.
6064
6065 @node Style Checking
6066 @subsection Style Checking
6067 @findex Style checking
6068
6069 @noindent
6070 The @option{-gnaty^x^(option,option,@dots{})^} switch
6071 @cindex @option{-gnaty} (@command{gcc})
6072 causes the compiler to
6073 enforce specified style rules. A limited set of style rules has been used
6074 in writing the GNAT sources themselves. This switch allows user programs
6075 to activate all or some of these checks. If the source program fails a
6076 specified style check, an appropriate warning message is given, preceded by
6077 the character sequence ``(style)''.
6078 @ifset vms
6079 @code{(option,option,@dots{})} is a sequence of keywords
6080 @end ifset
6081 @ifclear vms
6082 The string @var{x} is a sequence of letters or digits
6083 @end ifclear
6084 indicating the particular style
6085 checks to be performed. The following checks are defined:
6086
6087 @table @option
6088 @c !sort!
6089 @item 0-9
6090 @emph{Specify indentation level.}
6091 If a digit from 1-9 appears
6092 ^in the string after @option{-gnaty}^as an option for /STYLE_CHECKS^
6093 then proper indentation is checked, with the digit indicating the
6094 indentation level required. A value of zero turns off this style check.
6095 The general style of required indentation is as specified by
6096 the examples in the Ada Reference Manual. Full line comments must be
6097 aligned with the @code{--} starting on a column that is a multiple of
6098 the alignment level, or they may be aligned the same way as the following
6099 non-blank line (this is useful when full line comments appear in the middle
6100 of a statement.
6101
6102 @item ^a^ATTRIBUTE^
6103 @emph{Check attribute casing.}
6104 Attribute names, including the case of keywords such as @code{digits}
6105 used as attributes names, must be written in mixed case, that is, the
6106 initial letter and any letter following an underscore must be uppercase.
6107 All other letters must be lowercase.
6108
6109 @item ^A^ARRAY_INDEXES^
6110 @emph{Use of array index numbers in array attributes.}
6111 When using the array attributes First, Last, Range,
6112 or Length, the index number must be omitted for one-dimensional arrays
6113 and is required for multi-dimensional arrays.
6114
6115 @item ^b^BLANKS^
6116 @emph{Blanks not allowed at statement end.}
6117 Trailing blanks are not allowed at the end of statements. The purpose of this
6118 rule, together with h (no horizontal tabs), is to enforce a canonical format
6119 for the use of blanks to separate source tokens.
6120
6121 @item ^c^COMMENTS^
6122 @emph{Check comments.}
6123 Comments must meet the following set of rules:
6124
6125 @itemize @bullet
6126
6127 @item
6128 The ``@code{--}'' that starts the column must either start in column one,
6129 or else at least one blank must precede this sequence.
6130
6131 @item
6132 Comments that follow other tokens on a line must have at least one blank
6133 following the ``@code{--}'' at the start of the comment.
6134
6135 @item
6136 Full line comments must have two blanks following the ``@code{--}'' that
6137 starts the comment, with the following exceptions.
6138
6139 @item
6140 A line consisting only of the ``@code{--}'' characters, possibly preceded
6141 by blanks is permitted.
6142
6143 @item
6144 A comment starting with ``@code{--x}'' where @code{x} is a special character
6145 is permitted.
6146 This allows proper processing of the output generated by specialized tools
6147 including @command{gnatprep} (where ``@code{--!}'' is used) and the SPARK
6148 annotation
6149 language (where ``@code{--#}'' is used). For the purposes of this rule, a
6150 special character is defined as being in one of the ASCII ranges
6151 @code{16#21#@dots{}16#2F#} or @code{16#3A#@dots{}16#3F#}.
6152 Note that this usage is not permitted
6153 in GNAT implementation units (i.e., when @option{-gnatg} is used).
6154
6155 @item
6156 A line consisting entirely of minus signs, possibly preceded by blanks, is
6157 permitted. This allows the construction of box comments where lines of minus
6158 signs are used to form the top and bottom of the box.
6159
6160 @item
6161 A comment that starts and ends with ``@code{--}'' is permitted as long as at
6162 least one blank follows the initial ``@code{--}''. Together with the preceding
6163 rule, this allows the construction of box comments, as shown in the following
6164 example:
6165 @smallexample
6166 ---------------------------
6167 -- This is a box comment --
6168 -- with two text lines. --
6169 ---------------------------
6170 @end smallexample
6171 @end itemize
6172
6173 @item ^d^DOS_LINE_ENDINGS^
6174 @emph{Check no DOS line terminators present.}
6175 All lines must be terminated by a single ASCII.LF
6176 character (in particular the DOS line terminator sequence CR/LF is not
6177 allowed).
6178
6179 @item ^e^END^
6180 @emph{Check end/exit labels.}
6181 Optional labels on @code{end} statements ending subprograms and on
6182 @code{exit} statements exiting named loops, are required to be present.
6183
6184 @item ^f^VTABS^
6185 @emph{No form feeds or vertical tabs.}
6186 Neither form feeds nor vertical tab characters are permitted
6187 in the source text.
6188
6189 @item ^g^GNAT^
6190 @emph{GNAT style mode}
6191 The set of style check switches is set to match that used by the GNAT sources.
6192 This may be useful when developing code that is eventually intended to be
6193 incorporated into GNAT. For further details, see GNAT sources.
6194
6195 @item ^h^HTABS^
6196 @emph{No horizontal tabs.}
6197 Horizontal tab characters are not permitted in the source text.
6198 Together with the b (no blanks at end of line) check, this
6199 enforces a canonical form for the use of blanks to separate
6200 source tokens.
6201
6202 @item ^i^IF_THEN^
6203 @emph{Check if-then layout.}
6204 The keyword @code{then} must appear either on the same
6205 line as corresponding @code{if}, or on a line on its own, lined
6206 up under the @code{if} with at least one non-blank line in between
6207 containing all or part of the condition to be tested.
6208
6209 @item ^I^IN_MODE^
6210 @emph{check mode IN keywords}
6211 Mode @code{in} (the default mode) is not
6212 allowed to be given explicitly. @code{in out} is fine,
6213 but not @code{in} on its own.
6214
6215 @item ^k^KEYWORD^
6216 @emph{Check keyword casing.}
6217 All keywords must be in lower case (with the exception of keywords
6218 such as @code{digits} used as attribute names to which this check
6219 does not apply).
6220
6221 @item ^l^LAYOUT^
6222 @emph{Check layout.}
6223 Layout of statement and declaration constructs must follow the
6224 recommendations in the Ada Reference Manual, as indicated by the
6225 form of the syntax rules. For example an @code{else} keyword must
6226 be lined up with the corresponding @code{if} keyword.
6227
6228 There are two respects in which the style rule enforced by this check
6229 option are more liberal than those in the Ada Reference Manual. First
6230 in the case of record declarations, it is permissible to put the
6231 @code{record} keyword on the same line as the @code{type} keyword, and
6232 then the @code{end} in @code{end record} must line up under @code{type}.
6233 This is also permitted when the type declaration is split on two lines.
6234 For example, any of the following three layouts is acceptable:
6235
6236 @smallexample @c ada
6237 @cartouche
6238 type q is record
6239 a : integer;
6240 b : integer;
6241 end record;
6242
6243 type q is
6244 record
6245 a : integer;
6246 b : integer;
6247 end record;
6248
6249 type q is
6250 record
6251 a : integer;
6252 b : integer;
6253 end record;
6254
6255 @end cartouche
6256 @end smallexample
6257
6258 @noindent
6259 Second, in the case of a block statement, a permitted alternative
6260 is to put the block label on the same line as the @code{declare} or
6261 @code{begin} keyword, and then line the @code{end} keyword up under
6262 the block label. For example both the following are permitted:
6263
6264 @smallexample @c ada
6265 @cartouche
6266 Block : declare
6267 A : Integer := 3;
6268 begin
6269 Proc (A, A);
6270 end Block;
6271
6272 Block :
6273 declare
6274 A : Integer := 3;
6275 begin
6276 Proc (A, A);
6277 end Block;
6278 @end cartouche
6279 @end smallexample
6280
6281 @noindent
6282 The same alternative format is allowed for loops. For example, both of
6283 the following are permitted:
6284
6285 @smallexample @c ada
6286 @cartouche
6287 Clear : while J < 10 loop
6288 A (J) := 0;
6289 end loop Clear;
6290
6291 Clear :
6292 while J < 10 loop
6293 A (J) := 0;
6294 end loop Clear;
6295 @end cartouche
6296 @end smallexample
6297
6298 @item ^Lnnn^MAX_NESTING=nnn^
6299 @emph{Set maximum nesting level}
6300 The maximum level of nesting of constructs (including subprograms, loops,
6301 blocks, packages, and conditionals) may not exceed the given value
6302 @option{nnn}. A value of zero disconnects this style check.
6303
6304 @item ^m^LINE_LENGTH^
6305 @emph{Check maximum line length.}
6306 The length of source lines must not exceed 79 characters, including
6307 any trailing blanks. The value of 79 allows convenient display on an
6308 80 character wide device or window, allowing for possible special
6309 treatment of 80 character lines. Note that this count is of
6310 characters in the source text. This means that a tab character counts
6311 as one character in this count but a wide character sequence counts as
6312 a single character (however many bytes are needed in the encoding).
6313
6314 @item ^Mnnn^MAX_LENGTH=nnn^
6315 @emph{Set maximum line length.}
6316 The length of lines must not exceed the
6317 given value @option{nnn}. The maximum value that can be specified is 32767.
6318
6319 @item ^n^STANDARD_CASING^
6320 @emph{Check casing of entities in Standard.}
6321 Any identifier from Standard must be cased
6322 to match the presentation in the Ada Reference Manual (for example,
6323 @code{Integer} and @code{ASCII.NUL}).
6324
6325 @item ^N^NONE^
6326 @emph{Turn off all style checks}
6327 All style check options are turned off.
6328
6329 @item ^o^ORDERED_SUBPROGRAMS^
6330 @emph{Check order of subprogram bodies.}
6331 All subprogram bodies in a given scope
6332 (e.g.@: a package body) must be in alphabetical order. The ordering
6333 rule uses normal Ada rules for comparing strings, ignoring casing
6334 of letters, except that if there is a trailing numeric suffix, then
6335 the value of this suffix is used in the ordering (e.g.@: Junk2 comes
6336 before Junk10).
6337
6338 @item ^O^OVERRIDING_INDICATORS^
6339 @emph{Check that overriding subprograms are explicitly marked as such.}
6340 The declaration of a primitive operation of a type extension that overrides
6341 an inherited operation must carry an overriding indicator.
6342
6343 @item ^p^PRAGMA^
6344 @emph{Check pragma casing.}
6345 Pragma names must be written in mixed case, that is, the
6346 initial letter and any letter following an underscore must be uppercase.
6347 All other letters must be lowercase.
6348
6349 @item ^r^REFERENCES^
6350 @emph{Check references.}
6351 All identifier references must be cased in the same way as the
6352 corresponding declaration. No specific casing style is imposed on
6353 identifiers. The only requirement is for consistency of references
6354 with declarations.
6355
6356 @item ^S^STATEMENTS_AFTER_THEN_ELSE^
6357 @emph{Check no statements after THEN/ELSE.}
6358 No statements are allowed
6359 on the same line as a THEN or ELSE keyword following the
6360 keyword in an IF statement. OR ELSE and AND THEN are not affected,
6361 and a special exception allows a pragma to appear after ELSE.
6362
6363 @item ^s^SPECS^
6364 @emph{Check separate specs.}
6365 Separate declarations (``specs'') are required for subprograms (a
6366 body is not allowed to serve as its own declaration). The only
6367 exception is that parameterless library level procedures are
6368 not required to have a separate declaration. This exception covers
6369 the most frequent form of main program procedures.
6370
6371 @item ^t^TOKEN^
6372 @emph{Check token spacing.}
6373 The following token spacing rules are enforced:
6374
6375 @itemize @bullet
6376
6377 @item
6378 The keywords @code{@b{abs}} and @code{@b{not}} must be followed by a space.
6379
6380 @item
6381 The token @code{=>} must be surrounded by spaces.
6382
6383 @item
6384 The token @code{<>} must be preceded by a space or a left parenthesis.
6385
6386 @item
6387 Binary operators other than @code{**} must be surrounded by spaces.
6388 There is no restriction on the layout of the @code{**} binary operator.
6389
6390 @item
6391 Colon must be surrounded by spaces.
6392
6393 @item
6394 Colon-equal (assignment, initialization) must be surrounded by spaces.
6395
6396 @item
6397 Comma must be the first non-blank character on the line, or be
6398 immediately preceded by a non-blank character, and must be followed
6399 by a space.
6400
6401 @item
6402 If the token preceding a left parenthesis ends with a letter or digit, then
6403 a space must separate the two tokens.
6404
6405 @item
6406 A right parenthesis must either be the first non-blank character on
6407 a line, or it must be preceded by a non-blank character.
6408
6409 @item
6410 A semicolon must not be preceded by a space, and must not be followed by
6411 a non-blank character.
6412
6413 @item
6414 A unary plus or minus may not be followed by a space.
6415
6416 @item
6417 A vertical bar must be surrounded by spaces.
6418 @end itemize
6419
6420 @item ^u^UNNECESSARY_BLANK_LINES^
6421 @emph{Check unnecessary blank lines.}
6422 Unnecessary blank lines are not allowed. A blank line is considered
6423 unnecessary if it appears at the end of the file, or if more than
6424 one blank line occurs in sequence.
6425
6426 @item ^x^XTRA_PARENS^
6427 @emph{Check extra parentheses.}
6428 Unnecessary extra level of parentheses (C-style) are not allowed
6429 around conditions in @code{if} statements, @code{while} statements and
6430 @code{exit} statements.
6431
6432 @item ^y^ALL_BUILTIN^
6433 @emph{Set all standard style check options}
6434 This is equivalent to @code{gnaty3aAbcefhiklmnprst}, that is all checking
6435 options enabled with the exception of @option{-gnatyo}, @option{-gnatyI},
6436 @option{-gnatyS}, @option{-gnatyLnnn},
6437 @option{-gnatyd}, @option{-gnatyu}, and @option{-gnatyx}.
6438
6439 @ifclear vms
6440 @item -
6441 @emph{Remove style check options}
6442 This causes any subsequent options in the string to act as canceling the
6443 corresponding style check option. To cancel maximum nesting level control,
6444 use @option{L} parameter witout any integer value after that, because any
6445 digit following @option{-} in the parameter string of the @option{-gnaty}
6446 option will be threated as canceling indentation check. The same is true
6447 for @option{M} parameter. @option{y} and @option{N} parameters are not
6448 allowed after @option{-}.
6449
6450 @item +
6451 This causes any subsequent options in the string to enable the corresponding
6452 style check option. That is, it cancels the effect of a previous ^-^REMOVE^,
6453 if any.
6454 @end ifclear
6455
6456 @ifset vms
6457 @item NOxxx
6458 @emph{Removing style check options}
6459 If the name of a style check is preceded by @option{NO} then the corresponding
6460 style check is turned off. For example @option{NOCOMMENTS} turns off style
6461 checking for comments.
6462 @end ifset
6463 @end table
6464
6465 @noindent
6466 In the above rules, appearing in column one is always permitted, that is,
6467 counts as meeting either a requirement for a required preceding space,
6468 or as meeting a requirement for no preceding space.
6469
6470 Appearing at the end of a line is also always permitted, that is, counts
6471 as meeting either a requirement for a following space, or as meeting
6472 a requirement for no following space.
6473
6474 @noindent
6475 If any of these style rules is violated, a message is generated giving
6476 details on the violation. The initial characters of such messages are
6477 always ``@code{(style)}''. Note that these messages are treated as warning
6478 messages, so they normally do not prevent the generation of an object
6479 file. The @option{-gnatwe} switch can be used to treat warning messages,
6480 including style messages, as fatal errors.
6481
6482 The switch
6483 @ifclear vms
6484 @option{-gnaty} on its own (that is not
6485 followed by any letters or digits), then the effect is equivalent
6486 to the use of @option{-gnatyy}, as described above, that is all
6487 built-in standard style check options are enabled.
6488
6489 @end ifclear
6490 @ifset vms
6491 /STYLE_CHECKS=ALL_BUILTIN enables all checking options with
6492 the exception of ORDERED_SUBPROGRAMS, UNNECESSARY_BLANK_LINES,
6493 XTRA_PARENS, and DOS_LINE_ENDINGS. In addition
6494 @end ifset
6495
6496
6497
6498 The switch
6499 @ifclear vms
6500 @option{-gnatyN}
6501 @end ifclear
6502 @ifset vms
6503 /STYLE_CHECKS=NONE
6504 @end ifset
6505 clears any previously set style checks.
6506
6507 @node Run-Time Checks
6508 @subsection Run-Time Checks
6509 @cindex Division by zero
6510 @cindex Access before elaboration
6511 @cindex Checks, division by zero
6512 @cindex Checks, access before elaboration
6513 @cindex Checks, stack overflow checking
6514
6515 @noindent
6516 By default, the following checks are suppressed: integer overflow
6517 checks, stack overflow checks, and checks for access before
6518 elaboration on subprogram calls. All other checks, including range
6519 checks and array bounds checks, are turned on by default. The
6520 following @command{gcc} switches refine this default behavior.
6521
6522 @table @option
6523 @c !sort!
6524 @item -gnatp
6525 @cindex @option{-gnatp} (@command{gcc})
6526 @cindex Suppressing checks
6527 @cindex Checks, suppressing
6528 @findex Suppress
6529 This switch causes the unit to be compiled
6530 as though @code{pragma Suppress (All_checks)}
6531 had been present in the source. Validity checks are also eliminated (in
6532 other words @option{-gnatp} also implies @option{-gnatVn}.
6533 Use this switch to improve the performance
6534 of the code at the expense of safety in the presence of invalid data or
6535 program bugs.
6536
6537 Note that when checks are suppressed, the compiler is allowed, but not
6538 required, to omit the checking code. If the run-time cost of the
6539 checking code is zero or near-zero, the compiler will generate it even
6540 if checks are suppressed. In particular, if the compiler can prove
6541 that a certain check will necessarily fail, it will generate code to
6542 do an unconditional ``raise'', even if checks are suppressed. The
6543 compiler warns in this case. Another case in which checks may not be
6544 eliminated is when they are embedded in certain run time routines such
6545 as math library routines.
6546
6547 Of course, run-time checks are omitted whenever the compiler can prove
6548 that they will not fail, whether or not checks are suppressed.
6549
6550 Note that if you suppress a check that would have failed, program
6551 execution is erroneous, which means the behavior is totally
6552 unpredictable. The program might crash, or print wrong answers, or
6553 do anything else. It might even do exactly what you wanted it to do
6554 (and then it might start failing mysteriously next week or next
6555 year). The compiler will generate code based on the assumption that
6556 the condition being checked is true, which can result in disaster if
6557 that assumption is wrong.
6558
6559 @item -gnato
6560 @cindex @option{-gnato} (@command{gcc})
6561 @cindex Overflow checks
6562 @cindex Check, overflow
6563 Enables overflow checking for integer operations.
6564 This causes GNAT to generate slower and larger executable
6565 programs by adding code to check for overflow (resulting in raising
6566 @code{Constraint_Error} as required by standard Ada
6567 semantics). These overflow checks correspond to situations in which
6568 the true value of the result of an operation may be outside the base
6569 range of the result type. The following example shows the distinction:
6570
6571 @smallexample @c ada
6572 X1 : Integer := "Integer'Last";
6573 X2 : Integer range 1 .. 5 := "5";
6574 X3 : Integer := "Integer'Last";
6575 X4 : Integer range 1 .. 5 := "5";
6576 F : Float := "2.0E+20";
6577 @dots{}
6578 X1 := X1 + 1;
6579 X2 := X2 + 1;
6580 X3 := Integer (F);
6581 X4 := Integer (F);
6582 @end smallexample
6583
6584 @noindent
6585 Note that if explicit values are assigned at compile time, the
6586 compiler may be able to detect overflow at compile time, in which case
6587 no actual run-time checking code is required, and Constraint_Error
6588 will be raised unconditionally, with or without
6589 @option{-gnato}. That's why the assigned values in the above fragment
6590 are in quotes, the meaning is "assign a value not known to the
6591 compiler that happens to be equal to ...". The remaining discussion
6592 assumes that the compiler cannot detect the values at compile time.
6593
6594 Here the first addition results in a value that is outside the base range
6595 of Integer, and hence requires an overflow check for detection of the
6596 constraint error. Thus the first assignment to @code{X1} raises a
6597 @code{Constraint_Error} exception only if @option{-gnato} is set.
6598
6599 The second increment operation results in a violation of the explicit
6600 range constraint; such range checks are performed by default, and are
6601 unaffected by @option{-gnato}.
6602
6603 The two conversions of @code{F} both result in values that are outside
6604 the base range of type @code{Integer} and thus will raise
6605 @code{Constraint_Error} exceptions only if @option{-gnato} is used.
6606 The fact that the result of the second conversion is assigned to
6607 variable @code{X4} with a restricted range is irrelevant, since the problem
6608 is in the conversion, not the assignment.
6609
6610 Basically the rule is that in the default mode (@option{-gnato} not
6611 used), the generated code assures that all integer variables stay
6612 within their declared ranges, or within the base range if there is
6613 no declared range. This prevents any serious problems like indexes
6614 out of range for array operations.
6615
6616 What is not checked in default mode is an overflow that results in
6617 an in-range, but incorrect value. In the above example, the assignments
6618 to @code{X1}, @code{X2}, @code{X3} all give results that are within the
6619 range of the target variable, but the result is wrong in the sense that
6620 it is too large to be represented correctly. Typically the assignment
6621 to @code{X1} will result in wrap around to the largest negative number.
6622 The conversions of @code{F} will result in some @code{Integer} value
6623 and if that integer value is out of the @code{X4} range then the
6624 subsequent assignment would generate an exception.
6625
6626 @findex Machine_Overflows
6627 Note that the @option{-gnato} switch does not affect the code generated
6628 for any floating-point operations; it applies only to integer
6629 semantics).
6630 For floating-point, GNAT has the @code{Machine_Overflows}
6631 attribute set to @code{False} and the normal mode of operation is to
6632 generate IEEE NaN and infinite values on overflow or invalid operations
6633 (such as dividing 0.0 by 0.0).
6634
6635 The reason that we distinguish overflow checking from other kinds of
6636 range constraint checking is that a failure of an overflow check, unlike
6637 for example the failure of a range check, can result in an incorrect
6638 value, but cannot cause random memory destruction (like an out of range
6639 subscript), or a wild jump (from an out of range case value). Overflow
6640 checking is also quite expensive in time and space, since in general it
6641 requires the use of double length arithmetic.
6642
6643 Note again that @option{-gnato} is off by default, so overflow checking is
6644 not performed in default mode. This means that out of the box, with the
6645 default settings, GNAT does not do all the checks expected from the
6646 language description in the Ada Reference Manual. If you want all constraint
6647 checks to be performed, as described in this Manual, then you must
6648 explicitly use the -gnato switch either on the @command{gnatmake} or
6649 @command{gcc} command.
6650
6651 @item -gnatE
6652 @cindex @option{-gnatE} (@command{gcc})
6653 @cindex Elaboration checks
6654 @cindex Check, elaboration
6655 Enables dynamic checks for access-before-elaboration
6656 on subprogram calls and generic instantiations.
6657 Note that @option{-gnatE} is not necessary for safety, because in the
6658 default mode, GNAT ensures statically that the checks would not fail.
6659 For full details of the effect and use of this switch,
6660 @xref{Compiling Using gcc}.
6661
6662 @item -fstack-check
6663 @cindex @option{-fstack-check} (@command{gcc})
6664 @cindex Stack Overflow Checking
6665 @cindex Checks, stack overflow checking
6666 Activates stack overflow checking. For full details of the effect and use of
6667 this switch see @ref{Stack Overflow Checking}.
6668 @end table
6669
6670 @findex Unsuppress
6671 @noindent
6672 The setting of these switches only controls the default setting of the
6673 checks. You may modify them using either @code{Suppress} (to remove
6674 checks) or @code{Unsuppress} (to add back suppressed checks) pragmas in
6675 the program source.
6676
6677 @node Using gcc for Syntax Checking
6678 @subsection Using @command{gcc} for Syntax Checking
6679 @table @option
6680 @item -gnats
6681 @cindex @option{-gnats} (@command{gcc})
6682 @ifclear vms
6683
6684 @noindent
6685 The @code{s} stands for ``syntax''.
6686 @end ifclear
6687
6688 Run GNAT in syntax checking only mode. For
6689 example, the command
6690
6691 @smallexample
6692 $ gcc -c -gnats x.adb
6693 @end smallexample
6694
6695 @noindent
6696 compiles file @file{x.adb} in syntax-check-only mode. You can check a
6697 series of files in a single command
6698 @ifclear vms
6699 , and can use wild cards to specify such a group of files.
6700 Note that you must specify the @option{-c} (compile
6701 only) flag in addition to the @option{-gnats} flag.
6702 @end ifclear
6703 .
6704 You may use other switches in conjunction with @option{-gnats}. In
6705 particular, @option{-gnatl} and @option{-gnatv} are useful to control the
6706 format of any generated error messages.
6707
6708 When the source file is empty or contains only empty lines and/or comments,
6709 the output is a warning:
6710
6711 @smallexample
6712 $ gcc -c -gnats -x ada toto.txt
6713 toto.txt:1:01: warning: empty file, contains no compilation units
6714 $
6715 @end smallexample
6716
6717 Otherwise, the output is simply the error messages, if any. No object file or
6718 ALI file is generated by a syntax-only compilation. Also, no units other
6719 than the one specified are accessed. For example, if a unit @code{X}
6720 @code{with}'s a unit @code{Y}, compiling unit @code{X} in syntax
6721 check only mode does not access the source file containing unit
6722 @code{Y}.
6723
6724 @cindex Multiple units, syntax checking
6725 Normally, GNAT allows only a single unit in a source file. However, this
6726 restriction does not apply in syntax-check-only mode, and it is possible
6727 to check a file containing multiple compilation units concatenated
6728 together. This is primarily used by the @code{gnatchop} utility
6729 (@pxref{Renaming Files Using gnatchop}).
6730 @end table
6731
6732 @node Using gcc for Semantic Checking
6733 @subsection Using @command{gcc} for Semantic Checking
6734 @table @option
6735 @item -gnatc
6736 @cindex @option{-gnatc} (@command{gcc})
6737
6738 @ifclear vms
6739 @noindent
6740 The @code{c} stands for ``check''.
6741 @end ifclear
6742 Causes the compiler to operate in semantic check mode,
6743 with full checking for all illegalities specified in the
6744 Ada Reference Manual, but without generation of any object code
6745 (no object file is generated).
6746
6747 Because dependent files must be accessed, you must follow the GNAT
6748 semantic restrictions on file structuring to operate in this mode:
6749
6750 @itemize @bullet
6751 @item
6752 The needed source files must be accessible
6753 (@pxref{Search Paths and the Run-Time Library (RTL)}).
6754
6755 @item
6756 Each file must contain only one compilation unit.
6757
6758 @item
6759 The file name and unit name must match (@pxref{File Naming Rules}).
6760 @end itemize
6761
6762 The output consists of error messages as appropriate. No object file is
6763 generated. An @file{ALI} file is generated for use in the context of
6764 cross-reference tools, but this file is marked as not being suitable
6765 for binding (since no object file is generated).
6766 The checking corresponds exactly to the notion of
6767 legality in the Ada Reference Manual.
6768
6769 Any unit can be compiled in semantics-checking-only mode, including
6770 units that would not normally be compiled (subunits,
6771 and specifications where a separate body is present).
6772 @end table
6773
6774 @node Compiling Different Versions of Ada
6775 @subsection Compiling Different Versions of Ada
6776
6777 @noindent
6778 The switches described in this section allow you to explicitly specify
6779 the version of the Ada language that your programs are written in.
6780 By default @value{EDITION} assumes @value{DEFAULTLANGUAGEVERSION},
6781 but you can also specify @value{NONDEFAULTLANGUAGEVERSION} or
6782 indicate Ada 83 compatibility mode.
6783
6784 @table @option
6785 @cindex Compatibility with Ada 83
6786
6787 @item -gnat83 (Ada 83 Compatibility Mode)
6788 @cindex @option{-gnat83} (@command{gcc})
6789 @cindex ACVC, Ada 83 tests
6790 @cindex Ada 83 mode
6791
6792 @noindent
6793 Although GNAT is primarily an Ada 95 / Ada 2005 compiler, this switch
6794 specifies that the program is to be compiled in Ada 83 mode. With
6795 @option{-gnat83}, GNAT rejects most post-Ada 83 extensions and applies Ada 83
6796 semantics where this can be done easily.
6797 It is not possible to guarantee this switch does a perfect
6798 job; some subtle tests, such as are
6799 found in earlier ACVC tests (and that have been removed from the ACATS suite
6800 for Ada 95), might not compile correctly.
6801 Nevertheless, this switch may be useful in some circumstances, for example
6802 where, due to contractual reasons, existing code needs to be maintained
6803 using only Ada 83 features.
6804
6805 With few exceptions (most notably the need to use @code{<>} on
6806 @cindex Generic formal parameters
6807 unconstrained generic formal parameters, the use of the new Ada 95 / Ada 2005
6808 reserved words, and the use of packages
6809 with optional bodies), it is not necessary to specify the
6810 @option{-gnat83} switch when compiling Ada 83 programs, because, with rare
6811 exceptions, Ada 95 and Ada 2005 are upwardly compatible with Ada 83. Thus
6812 a correct Ada 83 program is usually also a correct program
6813 in these later versions of the language standard.
6814 For further information, please refer to @ref{Compatibility and Porting Guide}.
6815
6816 @item -gnat95 (Ada 95 mode)
6817 @cindex @option{-gnat95} (@command{gcc})
6818 @cindex Ada 95 mode
6819
6820 @noindent
6821 This switch directs the compiler to implement the Ada 95 version of the
6822 language.
6823 Since Ada 95 is almost completely upwards
6824 compatible with Ada 83, Ada 83 programs may generally be compiled using
6825 this switch (see the description of the @option{-gnat83} switch for further
6826 information about Ada 83 mode).
6827 If an Ada 2005 program is compiled in Ada 95 mode,
6828 uses of the new Ada 2005 features will cause error
6829 messages or warnings.
6830
6831 This switch also can be used to cancel the effect of a previous
6832 @option{-gnat83} or @option{-gnat05} switch earlier in the command line.
6833
6834 @item -gnat05 (Ada 2005 mode)
6835 @cindex @option{-gnat05} (@command{gcc})
6836 @cindex Ada 2005 mode
6837
6838 @noindent
6839 This switch directs the compiler to implement the Ada 2005 version of the
6840 language.
6841 Since Ada 2005 is almost completely upwards
6842 compatible with Ada 95 (and thus also with Ada 83), Ada 83 and Ada 95 programs
6843 may generally be compiled using this switch (see the description of the
6844 @option{-gnat83} and @option{-gnat95} switches for further
6845 information).
6846
6847 For information about the approved ``Ada Issues'' that have been incorporated
6848 into Ada 2005, see @url{http://www.ada-auth.org/cgi-bin/cvsweb.cgi/AIs}.
6849 Included with GNAT releases is a file @file{features-ada0y} that describes
6850 the set of implemented Ada 2005 features.
6851 @end table
6852
6853
6854 @node Character Set Control
6855 @subsection Character Set Control
6856 @table @option
6857 @item ^-gnati^/IDENTIFIER_CHARACTER_SET=^@var{c}
6858 @cindex @option{^-gnati^/IDENTIFIER_CHARACTER_SET^} (@command{gcc})
6859
6860 @noindent
6861 Normally GNAT recognizes the Latin-1 character set in source program
6862 identifiers, as described in the Ada Reference Manual.
6863 This switch causes
6864 GNAT to recognize alternate character sets in identifiers. @var{c} is a
6865 single character ^^or word^ indicating the character set, as follows:
6866
6867 @table @code
6868 @item 1
6869 ISO 8859-1 (Latin-1) identifiers
6870
6871 @item 2
6872 ISO 8859-2 (Latin-2) letters allowed in identifiers
6873
6874 @item 3
6875 ISO 8859-3 (Latin-3) letters allowed in identifiers
6876
6877 @item 4
6878 ISO 8859-4 (Latin-4) letters allowed in identifiers
6879
6880 @item 5
6881 ISO 8859-5 (Cyrillic) letters allowed in identifiers
6882
6883 @item 9
6884 ISO 8859-15 (Latin-9) letters allowed in identifiers
6885
6886 @item ^p^PC^
6887 IBM PC letters (code page 437) allowed in identifiers
6888
6889 @item ^8^PC850^
6890 IBM PC letters (code page 850) allowed in identifiers
6891
6892 @item ^f^FULL_UPPER^
6893 Full upper-half codes allowed in identifiers
6894
6895 @item ^n^NO_UPPER^
6896 No upper-half codes allowed in identifiers
6897
6898 @item ^w^WIDE^
6899 Wide-character codes (that is, codes greater than 255)
6900 allowed in identifiers
6901 @end table
6902
6903 @xref{Foreign Language Representation}, for full details on the
6904 implementation of these character sets.
6905
6906 @item ^-gnatW^/WIDE_CHARACTER_ENCODING=^@var{e}
6907 @cindex @option{^-gnatW^/WIDE_CHARACTER_ENCODING^} (@command{gcc})
6908 Specify the method of encoding for wide characters.
6909 @var{e} is one of the following:
6910
6911 @table @code
6912
6913 @item ^h^HEX^
6914 Hex encoding (brackets coding also recognized)
6915
6916 @item ^u^UPPER^
6917 Upper half encoding (brackets encoding also recognized)
6918
6919 @item ^s^SHIFT_JIS^
6920 Shift/JIS encoding (brackets encoding also recognized)
6921
6922 @item ^e^EUC^
6923 EUC encoding (brackets encoding also recognized)
6924
6925 @item ^8^UTF8^
6926 UTF-8 encoding (brackets encoding also recognized)
6927
6928 @item ^b^BRACKETS^
6929 Brackets encoding only (default value)
6930 @end table
6931 For full details on these encoding
6932 methods see @ref{Wide Character Encodings}.
6933 Note that brackets coding is always accepted, even if one of the other
6934 options is specified, so for example @option{-gnatW8} specifies that both
6935 brackets and UTF-8 encodings will be recognized. The units that are
6936 with'ed directly or indirectly will be scanned using the specified
6937 representation scheme, and so if one of the non-brackets scheme is
6938 used, it must be used consistently throughout the program. However,
6939 since brackets encoding is always recognized, it may be conveniently
6940 used in standard libraries, allowing these libraries to be used with
6941 any of the available coding schemes.
6942 scheme.
6943
6944 If no @option{-gnatW?} parameter is present, then the default
6945 representation is normally Brackets encoding only. However, if the
6946 first three characters of the file are 16#EF# 16#BB# 16#BF# (the standard
6947 byte order mark or BOM for UTF-8), then these three characters are
6948 skipped and the default representation for the file is set to UTF-8.
6949
6950 Note that the wide character representation that is specified (explicitly
6951 or by default) for the main program also acts as the default encoding used
6952 for Wide_Text_IO files if not specifically overridden by a WCEM form
6953 parameter.
6954
6955 @end table
6956 @node File Naming Control
6957 @subsection File Naming Control
6958
6959 @table @option
6960 @item ^-gnatk^/FILE_NAME_MAX_LENGTH=^@var{n}
6961 @cindex @option{-gnatk} (@command{gcc})
6962 Activates file name ``krunching''. @var{n}, a decimal integer in the range
6963 1-999, indicates the maximum allowable length of a file name (not
6964 including the @file{.ads} or @file{.adb} extension). The default is not
6965 to enable file name krunching.
6966
6967 For the source file naming rules, @xref{File Naming Rules}.
6968 @end table
6969
6970 @node Subprogram Inlining Control
6971 @subsection Subprogram Inlining Control
6972
6973 @table @option
6974 @c !sort!
6975 @item -gnatn
6976 @cindex @option{-gnatn} (@command{gcc})
6977 @ifclear vms
6978 The @code{n} here is intended to suggest the first syllable of the
6979 word ``inline''.
6980 @end ifclear
6981 GNAT recognizes and processes @code{Inline} pragmas. However, for the
6982 inlining to actually occur, optimization must be enabled. To enable
6983 inlining of subprograms specified by pragma @code{Inline},
6984 you must also specify this switch.
6985 In the absence of this switch, GNAT does not attempt
6986 inlining and does not need to access the bodies of
6987 subprograms for which @code{pragma Inline} is specified if they are not
6988 in the current unit.
6989
6990 If you specify this switch the compiler will access these bodies,
6991 creating an extra source dependency for the resulting object file, and
6992 where possible, the call will be inlined.
6993 For further details on when inlining is possible
6994 see @ref{Inlining of Subprograms}.
6995
6996 @item -gnatN
6997 @cindex @option{-gnatN} (@command{gcc})
6998 This switch activates front-end inlining which also
6999 generates additional dependencies.
7000
7001 When using a gcc-based back end (in practice this means using any version
7002 of GNAT other than the JGNAT, .NET or GNAAMP versions), then the use of
7003 @option{-gnatN} is deprecated, and the use of @option{-gnatn} is preferred.
7004 Historically front end inlining was more extensive than the gcc back end
7005 inlining, but that is no longer the case.
7006 @end table
7007
7008 @node Auxiliary Output Control
7009 @subsection Auxiliary Output Control
7010
7011 @table @option
7012 @item -gnatt
7013 @cindex @option{-gnatt} (@command{gcc})
7014 @cindex Writing internal trees
7015 @cindex Internal trees, writing to file
7016 Causes GNAT to write the internal tree for a unit to a file (with the
7017 extension @file{.adt}.
7018 This not normally required, but is used by separate analysis tools.
7019 Typically
7020 these tools do the necessary compilations automatically, so you should
7021 not have to specify this switch in normal operation.
7022 Note that the combination of switches @option{-gnatct}
7023 generates a tree in the form required by ASIS applications.
7024
7025 @item -gnatu
7026 @cindex @option{-gnatu} (@command{gcc})
7027 Print a list of units required by this compilation on @file{stdout}.
7028 The listing includes all units on which the unit being compiled depends
7029 either directly or indirectly.
7030
7031 @ifclear vms
7032 @item -pass-exit-codes
7033 @cindex @option{-pass-exit-codes} (@command{gcc})
7034 If this switch is not used, the exit code returned by @command{gcc} when
7035 compiling multiple files indicates whether all source files have
7036 been successfully used to generate object files or not.
7037
7038 When @option{-pass-exit-codes} is used, @command{gcc} exits with an extended
7039 exit status and allows an integrated development environment to better
7040 react to a compilation failure. Those exit status are:
7041
7042 @table @asis
7043 @item 5
7044 There was an error in at least one source file.
7045 @item 3
7046 At least one source file did not generate an object file.
7047 @item 2
7048 The compiler died unexpectedly (internal error for example).
7049 @item 0
7050 An object file has been generated for every source file.
7051 @end table
7052 @end ifclear
7053 @end table
7054
7055 @node Debugging Control
7056 @subsection Debugging Control
7057
7058 @table @option
7059 @c !sort!
7060 @cindex Debugging options
7061 @ifclear vms
7062 @item -gnatd@var{x}
7063 @cindex @option{-gnatd} (@command{gcc})
7064 Activate internal debugging switches. @var{x} is a letter or digit, or
7065 string of letters or digits, which specifies the type of debugging
7066 outputs desired. Normally these are used only for internal development
7067 or system debugging purposes. You can find full documentation for these
7068 switches in the body of the @code{Debug} unit in the compiler source
7069 file @file{debug.adb}.
7070 @end ifclear
7071
7072 @item -gnatG[=nn]
7073 @cindex @option{-gnatG} (@command{gcc})
7074 This switch causes the compiler to generate auxiliary output containing
7075 a pseudo-source listing of the generated expanded code. Like most Ada
7076 compilers, GNAT works by first transforming the high level Ada code into
7077 lower level constructs. For example, tasking operations are transformed
7078 into calls to the tasking run-time routines. A unique capability of GNAT
7079 is to list this expanded code in a form very close to normal Ada source.
7080 This is very useful in understanding the implications of various Ada
7081 usage on the efficiency of the generated code. There are many cases in
7082 Ada (e.g.@: the use of controlled types), where simple Ada statements can
7083 generate a lot of run-time code. By using @option{-gnatG} you can identify
7084 these cases, and consider whether it may be desirable to modify the coding
7085 approach to improve efficiency.
7086
7087 The optional parameter @code{nn} if present after -gnatG specifies an
7088 alternative maximum line length that overrides the normal default of 72.
7089 This value is in the range 40-999999, values less than 40 being silently
7090 reset to 40. The equal sign is optional.
7091
7092 The format of the output is very similar to standard Ada source, and is
7093 easily understood by an Ada programmer. The following special syntactic
7094 additions correspond to low level features used in the generated code that
7095 do not have any exact analogies in pure Ada source form. The following
7096 is a partial list of these special constructions. See the spec
7097 of package @code{Sprint} in file @file{sprint.ads} for a full list.
7098
7099 If the switch @option{-gnatL} is used in conjunction with
7100 @cindex @option{-gnatL} (@command{gcc})
7101 @option{-gnatG}, then the original source lines are interspersed
7102 in the expanded source (as comment lines with the original line number).
7103
7104 @table @code
7105 @item new @var{xxx} @r{[}storage_pool = @var{yyy}@r{]}
7106 Shows the storage pool being used for an allocator.
7107
7108 @item at end @var{procedure-name};
7109 Shows the finalization (cleanup) procedure for a scope.
7110
7111 @item (if @var{expr} then @var{expr} else @var{expr})
7112 Conditional expression equivalent to the @code{x?y:z} construction in C.
7113
7114 @item @var{target}^^^(@var{source})
7115 A conversion with floating-point truncation instead of rounding.
7116
7117 @item @var{target}?(@var{source})
7118 A conversion that bypasses normal Ada semantic checking. In particular
7119 enumeration types and fixed-point types are treated simply as integers.
7120
7121 @item @var{target}?^^^(@var{source})
7122 Combines the above two cases.
7123
7124 @item @var{x} #/ @var{y}
7125 @itemx @var{x} #mod @var{y}
7126 @itemx @var{x} #* @var{y}
7127 @itemx @var{x} #rem @var{y}
7128 A division or multiplication of fixed-point values which are treated as
7129 integers without any kind of scaling.
7130
7131 @item free @var{expr} @r{[}storage_pool = @var{xxx}@r{]}
7132 Shows the storage pool associated with a @code{free} statement.
7133
7134 @item [subtype or type declaration]
7135 Used to list an equivalent declaration for an internally generated
7136 type that is referenced elsewhere in the listing.
7137
7138 @item freeze @var{type-name} @ovar{actions}
7139 Shows the point at which @var{type-name} is frozen, with possible
7140 associated actions to be performed at the freeze point.
7141
7142 @item reference @var{itype}
7143 Reference (and hence definition) to internal type @var{itype}.
7144
7145 @item @var{function-name}! (@var{arg}, @var{arg}, @var{arg})
7146 Intrinsic function call.
7147
7148 @item @var{label-name} : label
7149 Declaration of label @var{labelname}.
7150
7151 @item #$ @var{subprogram-name}
7152 An implicit call to a run-time support routine
7153 (to meet the requirement of H.3.1(9) in a
7154 convenient manner).
7155
7156 @item @var{expr} && @var{expr} && @var{expr} @dots{} && @var{expr}
7157 A multiple concatenation (same effect as @var{expr} & @var{expr} &
7158 @var{expr}, but handled more efficiently).
7159
7160 @item [constraint_error]
7161 Raise the @code{Constraint_Error} exception.
7162
7163 @item @var{expression}'reference
7164 A pointer to the result of evaluating @var{expression}.
7165
7166 @item @var{target-type}!(@var{source-expression})
7167 An unchecked conversion of @var{source-expression} to @var{target-type}.
7168
7169 @item [@var{numerator}/@var{denominator}]
7170 Used to represent internal real literals (that) have no exact
7171 representation in base 2-16 (for example, the result of compile time
7172 evaluation of the expression 1.0/27.0).
7173 @end table
7174
7175 @item -gnatD[=nn]
7176 @cindex @option{-gnatD} (@command{gcc})
7177 When used in conjunction with @option{-gnatG}, this switch causes
7178 the expanded source, as described above for
7179 @option{-gnatG} to be written to files with names
7180 @file{^xxx.dg^XXX_DG^}, where @file{xxx} is the normal file name,
7181 instead of to the standard output file. For
7182 example, if the source file name is @file{hello.adb}, then a file
7183 @file{^hello.adb.dg^HELLO.ADB_DG^} will be written. The debugging
7184 information generated by the @command{gcc} @option{^-g^/DEBUG^} switch
7185 will refer to the generated @file{^xxx.dg^XXX_DG^} file. This allows
7186 you to do source level debugging using the generated code which is
7187 sometimes useful for complex code, for example to find out exactly
7188 which part of a complex construction raised an exception. This switch
7189 also suppress generation of cross-reference information (see
7190 @option{-gnatx}) since otherwise the cross-reference information
7191 would refer to the @file{^.dg^.DG^} file, which would cause
7192 confusion since this is not the original source file.
7193
7194 Note that @option{-gnatD} actually implies @option{-gnatG}
7195 automatically, so it is not necessary to give both options.
7196 In other words @option{-gnatD} is equivalent to @option{-gnatDG}).
7197
7198 If the switch @option{-gnatL} is used in conjunction with
7199 @cindex @option{-gnatL} (@command{gcc})
7200 @option{-gnatDG}, then the original source lines are interspersed
7201 in the expanded source (as comment lines with the original line number).
7202
7203 The optional parameter @code{nn} if present after -gnatD specifies an
7204 alternative maximum line length that overrides the normal default of 72.
7205 This value is in the range 40-999999, values less than 40 being silently
7206 reset to 40. The equal sign is optional.
7207
7208 @item -gnatr
7209 @cindex @option{-gnatr} (@command{gcc})
7210 @cindex pragma Restrictions
7211 This switch causes pragma Restrictions to be treated as Restriction_Warnings
7212 so that violation of restrictions causes warnings rather than illegalities.
7213 This is useful during the development process when new restrictions are added
7214 or investigated. The switch also causes pragma Profile to be treated as
7215 Profile_Warnings, and pragma Restricted_Run_Time and pragma Ravenscar set
7216 restriction warnings rather than restrictions.
7217
7218 @ifclear vms
7219 @item -gnatR@r{[}0@r{|}1@r{|}2@r{|}3@r{[}s@r{]]}
7220 @cindex @option{-gnatR} (@command{gcc})
7221 This switch controls output from the compiler of a listing showing
7222 representation information for declared types and objects. For
7223 @option{-gnatR0}, no information is output (equivalent to omitting
7224 the @option{-gnatR} switch). For @option{-gnatR1} (which is the default,
7225 so @option{-gnatR} with no parameter has the same effect), size and alignment
7226 information is listed for declared array and record types. For
7227 @option{-gnatR2}, size and alignment information is listed for all
7228 declared types and objects. Finally @option{-gnatR3} includes symbolic
7229 expressions for values that are computed at run time for
7230 variant records. These symbolic expressions have a mostly obvious
7231 format with #n being used to represent the value of the n'th
7232 discriminant. See source files @file{repinfo.ads/adb} in the
7233 @code{GNAT} sources for full details on the format of @option{-gnatR3}
7234 output. If the switch is followed by an s (e.g.@: @option{-gnatR2s}), then
7235 the output is to a file with the name @file{^file.rep^file_REP^} where
7236 file is the name of the corresponding source file.
7237 @end ifclear
7238 @ifset vms
7239 @item /REPRESENTATION_INFO
7240 @cindex @option{/REPRESENTATION_INFO} (@command{gcc})
7241 This qualifier controls output from the compiler of a listing showing
7242 representation information for declared types and objects. For
7243 @option{/REPRESENTATION_INFO=NONE}, no information is output
7244 (equivalent to omitting the @option{/REPRESENTATION_INFO} qualifier).
7245 @option{/REPRESENTATION_INFO} without option is equivalent to
7246 @option{/REPRESENTATION_INFO=ARRAYS}.
7247 For @option{/REPRESENTATION_INFO=ARRAYS}, size and alignment
7248 information is listed for declared array and record types. For
7249 @option{/REPRESENTATION_INFO=OBJECTS}, size and alignment information
7250 is listed for all expression information for values that are computed
7251 at run time for variant records. These symbolic expressions have a mostly
7252 obvious format with #n being used to represent the value of the n'th
7253 discriminant. See source files @file{REPINFO.ADS/ADB} in the
7254 @code{GNAT} sources for full details on the format of
7255 @option{/REPRESENTATION_INFO=SYMBOLIC} output.
7256 If _FILE is added at the end of an option
7257 (e.g.@: @option{/REPRESENTATION_INFO=ARRAYS_FILE}),
7258 then the output is to a file with the name @file{file_REP} where
7259 file is the name of the corresponding source file.
7260 @end ifset
7261 Note that it is possible for record components to have zero size. In
7262 this case, the component clause uses an obvious extension of permitted
7263 Ada syntax, for example @code{at 0 range 0 .. -1}.
7264
7265 Representation information requires that code be generated (since it is the
7266 code generator that lays out complex data structures). If an attempt is made
7267 to output representation information when no code is generated, for example
7268 when a subunit is compiled on its own, then no information can be generated
7269 and the compiler outputs a message to this effect.
7270
7271 @item -gnatS
7272 @cindex @option{-gnatS} (@command{gcc})
7273 The use of the switch @option{-gnatS} for an
7274 Ada compilation will cause the compiler to output a
7275 representation of package Standard in a form very
7276 close to standard Ada. It is not quite possible to
7277 do this entirely in standard Ada (since new
7278 numeric base types cannot be created in standard
7279 Ada), but the output is easily
7280 readable to any Ada programmer, and is useful to
7281 determine the characteristics of target dependent
7282 types in package Standard.
7283
7284 @item -gnatx
7285 @cindex @option{-gnatx} (@command{gcc})
7286 Normally the compiler generates full cross-referencing information in
7287 the @file{ALI} file. This information is used by a number of tools,
7288 including @code{gnatfind} and @code{gnatxref}. The @option{-gnatx} switch
7289 suppresses this information. This saves some space and may slightly
7290 speed up compilation, but means that these tools cannot be used.
7291 @end table
7292
7293 @node Exception Handling Control
7294 @subsection Exception Handling Control
7295
7296 @noindent
7297 GNAT uses two methods for handling exceptions at run-time. The
7298 @code{setjmp/longjmp} method saves the context when entering
7299 a frame with an exception handler. Then when an exception is
7300 raised, the context can be restored immediately, without the
7301 need for tracing stack frames. This method provides very fast
7302 exception propagation, but introduces significant overhead for
7303 the use of exception handlers, even if no exception is raised.
7304
7305 The other approach is called ``zero cost'' exception handling.
7306 With this method, the compiler builds static tables to describe
7307 the exception ranges. No dynamic code is required when entering
7308 a frame containing an exception handler. When an exception is
7309 raised, the tables are used to control a back trace of the
7310 subprogram invocation stack to locate the required exception
7311 handler. This method has considerably poorer performance for
7312 the propagation of exceptions, but there is no overhead for
7313 exception handlers if no exception is raised. Note that in this
7314 mode and in the context of mixed Ada and C/C++ programming,
7315 to propagate an exception through a C/C++ code, the C/C++ code
7316 must be compiled with the @option{-funwind-tables} GCC's
7317 option.
7318
7319 The following switches may be used to control which of the
7320 two exception handling methods is used.
7321
7322 @table @option
7323 @c !sort!
7324
7325 @item --RTS=sjlj
7326 @cindex @option{--RTS=sjlj} (@command{gnatmake})
7327 This switch causes the setjmp/longjmp run-time (when available) to be used
7328 for exception handling. If the default
7329 mechanism for the target is zero cost exceptions, then
7330 this switch can be used to modify this default, and must be
7331 used for all units in the partition.
7332 This option is rarely used. One case in which it may be
7333 advantageous is if you have an application where exception
7334 raising is common and the overall performance of the
7335 application is improved by favoring exception propagation.
7336
7337 @item --RTS=zcx
7338 @cindex @option{--RTS=zcx} (@command{gnatmake})
7339 @cindex Zero Cost Exceptions
7340 This switch causes the zero cost approach to be used
7341 for exception handling. If this is the default mechanism for the
7342 target (see below), then this switch is unneeded. If the default
7343 mechanism for the target is setjmp/longjmp exceptions, then
7344 this switch can be used to modify this default, and must be
7345 used for all units in the partition.
7346 This option can only be used if the zero cost approach
7347 is available for the target in use, otherwise it will generate an error.
7348 @end table
7349
7350 @noindent
7351 The same option @option{--RTS} must be used both for @command{gcc}
7352 and @command{gnatbind}. Passing this option to @command{gnatmake}
7353 (@pxref{Switches for gnatmake}) will ensure the required consistency
7354 through the compilation and binding steps.
7355
7356 @node Units to Sources Mapping Files
7357 @subsection Units to Sources Mapping Files
7358
7359 @table @option
7360
7361 @item -gnatem^^=^@var{path}
7362 @cindex @option{-gnatem} (@command{gcc})
7363 A mapping file is a way to communicate to the compiler two mappings:
7364 from unit names to file names (without any directory information) and from
7365 file names to path names (with full directory information). These mappings
7366 are used by the compiler to short-circuit the path search.
7367
7368 The use of mapping files is not required for correct operation of the
7369 compiler, but mapping files can improve efficiency, particularly when
7370 sources are read over a slow network connection. In normal operation,
7371 you need not be concerned with the format or use of mapping files,
7372 and the @option{-gnatem} switch is not a switch that you would use
7373 explicitly. it is intended only for use by automatic tools such as
7374 @command{gnatmake} running under the project file facility. The
7375 description here of the format of mapping files is provided
7376 for completeness and for possible use by other tools.
7377
7378 A mapping file is a sequence of sets of three lines. In each set,
7379 the first line is the unit name, in lower case, with ``@code{%s}''
7380 appended for
7381 specs and ``@code{%b}'' appended for bodies; the second line is the
7382 file name; and the third line is the path name.
7383
7384 Example:
7385 @smallexample
7386 main%b
7387 main.2.ada
7388 /gnat/project1/sources/main.2.ada
7389 @end smallexample
7390
7391 When the switch @option{-gnatem} is specified, the compiler will create
7392 in memory the two mappings from the specified file. If there is any problem
7393 (nonexistent file, truncated file or duplicate entries), no mapping will
7394 be created.
7395
7396 Several @option{-gnatem} switches may be specified; however, only the last
7397 one on the command line will be taken into account.
7398
7399 When using a project file, @command{gnatmake} create a temporary mapping file
7400 and communicates it to the compiler using this switch.
7401
7402 @end table
7403
7404 @node Integrated Preprocessing
7405 @subsection Integrated Preprocessing
7406
7407 @noindent
7408 GNAT sources may be preprocessed immediately before compilation.
7409 In this case, the actual
7410 text of the source is not the text of the source file, but is derived from it
7411 through a process called preprocessing. Integrated preprocessing is specified
7412 through switches @option{-gnatep} and/or @option{-gnateD}. @option{-gnatep}
7413 indicates, through a text file, the preprocessing data to be used.
7414 @option{-gnateD} specifies or modifies the values of preprocessing symbol.
7415
7416 @noindent
7417 Note that when integrated preprocessing is used, the output from the
7418 preprocessor is not written to any external file. Instead it is passed
7419 internally to the compiler. If you need to preserve the result of
7420 preprocessing in a file, then you should use @command{gnatprep}
7421 to perform the desired preprocessing in stand-alone mode.
7422
7423 @noindent
7424 It is recommended that @command{gnatmake} switch ^-s^/SWITCH_CHECK^ should be
7425 used when Integrated Preprocessing is used. The reason is that preprocessing
7426 with another Preprocessing Data file without changing the sources will
7427 not trigger recompilation without this switch.
7428
7429 @noindent
7430 Note that @command{gnatmake} switch ^-m^/MINIMAL_RECOMPILATION^ will almost
7431 always trigger recompilation for sources that are preprocessed,
7432 because @command{gnatmake} cannot compute the checksum of the source after
7433 preprocessing.
7434
7435 @noindent
7436 The actual preprocessing function is described in details in section
7437 @ref{Preprocessing Using gnatprep}. This section only describes how integrated
7438 preprocessing is triggered and parameterized.
7439
7440 @table @code
7441
7442 @item -gnatep=@var{file}
7443 @cindex @option{-gnatep} (@command{gcc})
7444 This switch indicates to the compiler the file name (without directory
7445 information) of the preprocessor data file to use. The preprocessor data file
7446 should be found in the source directories.
7447
7448 @noindent
7449 A preprocessing data file is a text file with significant lines indicating
7450 how should be preprocessed either a specific source or all sources not
7451 mentioned in other lines. A significant line is a nonempty, non-comment line.
7452 Comments are similar to Ada comments.
7453
7454 @noindent
7455 Each significant line starts with either a literal string or the character '*'.
7456 A literal string is the file name (without directory information) of the source
7457 to preprocess. A character '*' indicates the preprocessing for all the sources
7458 that are not specified explicitly on other lines (order of the lines is not
7459 significant). It is an error to have two lines with the same file name or two
7460 lines starting with the character '*'.
7461
7462 @noindent
7463 After the file name or the character '*', another optional literal string
7464 indicating the file name of the definition file to be used for preprocessing
7465 (@pxref{Form of Definitions File}). The definition files are found by the
7466 compiler in one of the source directories. In some cases, when compiling
7467 a source in a directory other than the current directory, if the definition
7468 file is in the current directory, it may be necessary to add the current
7469 directory as a source directory through switch ^-I.^/SEARCH=[]^, otherwise
7470 the compiler would not find the definition file.
7471
7472 @noindent
7473 Then, optionally, ^switches^switches^ similar to those of @code{gnatprep} may
7474 be found. Those ^switches^switches^ are:
7475
7476 @table @code
7477
7478 @item -b
7479 Causes both preprocessor lines and the lines deleted by
7480 preprocessing to be replaced by blank lines, preserving the line number.
7481 This ^switch^switch^ is always implied; however, if specified after @option{-c}
7482 it cancels the effect of @option{-c}.
7483
7484 @item -c
7485 Causes both preprocessor lines and the lines deleted
7486 by preprocessing to be retained as comments marked
7487 with the special string ``@code{--! }''.
7488
7489 @item -Dsymbol=value
7490 Define or redefine a symbol, associated with value. A symbol is an Ada
7491 identifier, or an Ada reserved word, with the exception of @code{if},
7492 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
7493 @code{value} is either a literal string, an Ada identifier or any Ada reserved
7494 word. A symbol declared with this ^switch^switch^ replaces a symbol with the
7495 same name defined in a definition file.
7496
7497 @item -s
7498 Causes a sorted list of symbol names and values to be
7499 listed on the standard output file.
7500
7501 @item -u
7502 Causes undefined symbols to be treated as having the value @code{FALSE}
7503 in the context
7504 of a preprocessor test. In the absence of this option, an undefined symbol in
7505 a @code{#if} or @code{#elsif} test will be treated as an error.
7506
7507 @end table
7508
7509 @noindent
7510 Examples of valid lines in a preprocessor data file:
7511
7512 @smallexample
7513 "toto.adb" "prep.def" -u
7514 -- preprocess "toto.adb", using definition file "prep.def",
7515 -- undefined symbol are False.
7516
7517 * -c -DVERSION=V101
7518 -- preprocess all other sources without a definition file;
7519 -- suppressed lined are commented; symbol VERSION has the value V101.
7520
7521 "titi.adb" "prep2.def" -s
7522 -- preprocess "titi.adb", using definition file "prep2.def";
7523 -- list all symbols with their values.
7524 @end smallexample
7525
7526 @item ^-gnateD^/DATA_PREPROCESSING=^symbol@r{[}=value@r{]}
7527 @cindex @option{-gnateD} (@command{gcc})
7528 Define or redefine a preprocessing symbol, associated with value. If no value
7529 is given on the command line, then the value of the symbol is @code{True}.
7530 A symbol is an identifier, following normal Ada (case-insensitive)
7531 rules for its syntax, and value is any sequence (including an empty sequence)
7532 of characters from the set (letters, digits, period, underline).
7533 Ada reserved words may be used as symbols, with the exceptions of @code{if},
7534 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
7535
7536 @noindent
7537 A symbol declared with this ^switch^switch^ on the command line replaces a
7538 symbol with the same name either in a definition file or specified with a
7539 ^switch^switch^ -D in the preprocessor data file.
7540
7541 @noindent
7542 This switch is similar to switch @option{^-D^/ASSOCIATE^} of @code{gnatprep}.
7543
7544 @item -gnateG
7545 When integrated preprocessing is performed and the preprocessor modifies
7546 the source text, write the result of this preprocessing into a file
7547 <source>^.prep^_prep^.
7548
7549 @end table
7550
7551 @node Code Generation Control
7552 @subsection Code Generation Control
7553
7554 @noindent
7555
7556 The GCC technology provides a wide range of target dependent
7557 @option{-m} switches for controlling
7558 details of code generation with respect to different versions of
7559 architectures. This includes variations in instruction sets (e.g.@:
7560 different members of the power pc family), and different requirements
7561 for optimal arrangement of instructions (e.g.@: different members of
7562 the x86 family). The list of available @option{-m} switches may be
7563 found in the GCC documentation.
7564
7565 Use of these @option{-m} switches may in some cases result in improved
7566 code performance.
7567
7568 The GNAT Pro technology is tested and qualified without any
7569 @option{-m} switches,
7570 so generally the most reliable approach is to avoid the use of these
7571 switches. However, we generally expect most of these switches to work
7572 successfully with GNAT Pro, and many customers have reported successful
7573 use of these options.
7574
7575 Our general advice is to avoid the use of @option{-m} switches unless
7576 special needs lead to requirements in this area. In particular,
7577 there is no point in using @option{-m} switches to improve performance
7578 unless you actually see a performance improvement.
7579
7580 @ifset vms
7581 @node Return Codes
7582 @subsection Return Codes
7583 @cindex Return Codes
7584 @cindex @option{/RETURN_CODES=VMS}
7585
7586 @noindent
7587 On VMS, GNAT compiled programs return POSIX-style codes by default,
7588 e.g.@: @option{/RETURN_CODES=POSIX}.
7589
7590 To enable VMS style return codes, use GNAT BIND and LINK with the option
7591 @option{/RETURN_CODES=VMS}. For example:
7592
7593 @smallexample
7594 GNAT BIND MYMAIN.ALI /RETURN_CODES=VMS
7595 GNAT LINK MYMAIN.ALI /RETURN_CODES=VMS
7596 @end smallexample
7597
7598 @noindent
7599 Programs built with /RETURN_CODES=VMS are suitable to be called in
7600 VMS DCL scripts. Programs compiled with the default /RETURN_CODES=POSIX
7601 are suitable for spawning with appropriate GNAT RTL routines.
7602
7603 @end ifset
7604
7605 @node Search Paths and the Run-Time Library (RTL)
7606 @section Search Paths and the Run-Time Library (RTL)
7607
7608 @noindent
7609 With the GNAT source-based library system, the compiler must be able to
7610 find source files for units that are needed by the unit being compiled.
7611 Search paths are used to guide this process.
7612
7613 The compiler compiles one source file whose name must be given
7614 explicitly on the command line. In other words, no searching is done
7615 for this file. To find all other source files that are needed (the most
7616 common being the specs of units), the compiler examines the following
7617 directories, in the following order:
7618
7619 @enumerate
7620 @item
7621 The directory containing the source file of the main unit being compiled
7622 (the file name on the command line).
7623
7624 @item
7625 Each directory named by an @option{^-I^/SOURCE_SEARCH^} switch given on the
7626 @command{gcc} command line, in the order given.
7627
7628 @item
7629 @findex ADA_PRJ_INCLUDE_FILE
7630 Each of the directories listed in the text file whose name is given
7631 by the @env{ADA_PRJ_INCLUDE_FILE} ^environment variable^logical name^.
7632
7633 @noindent
7634 @env{ADA_PRJ_INCLUDE_FILE} is normally set by gnatmake or by the ^gnat^GNAT^
7635 driver when project files are used. It should not normally be set
7636 by other means.
7637
7638 @item
7639 @findex ADA_INCLUDE_PATH
7640 Each of the directories listed in the value of the
7641 @env{ADA_INCLUDE_PATH} ^environment variable^logical name^.
7642 @ifclear vms
7643 Construct this value
7644 exactly as the @env{PATH} environment variable: a list of directory
7645 names separated by colons (semicolons when working with the NT version).
7646 @end ifclear
7647 @ifset vms
7648 Normally, define this value as a logical name containing a comma separated
7649 list of directory names.
7650
7651 This variable can also be defined by means of an environment string
7652 (an argument to the HP C exec* set of functions).
7653
7654 Logical Name:
7655 @smallexample
7656 DEFINE ANOTHER_PATH FOO:[BAG]
7657 DEFINE ADA_INCLUDE_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
7658 @end smallexample
7659
7660 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
7661 first, followed by the standard Ada
7662 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADAINCLUDE].
7663 If this is not redefined, the user will obtain the HP Ada 83 IO packages
7664 (Text_IO, Sequential_IO, etc)
7665 instead of the standard Ada packages. Thus, in order to get the standard Ada
7666 packages by default, ADA_INCLUDE_PATH must be redefined.
7667 @end ifset
7668
7669 @item
7670 The content of the @file{ada_source_path} file which is part of the GNAT
7671 installation tree and is used to store standard libraries such as the
7672 GNAT Run Time Library (RTL) source files.
7673 @ifclear vms
7674 @ref{Installing a library}
7675 @end ifclear
7676 @end enumerate
7677
7678 @noindent
7679 Specifying the switch @option{^-I-^/NOCURRENT_DIRECTORY^}
7680 inhibits the use of the directory
7681 containing the source file named in the command line. You can still
7682 have this directory on your search path, but in this case it must be
7683 explicitly requested with a @option{^-I^/SOURCE_SEARCH^} switch.
7684
7685 Specifying the switch @option{-nostdinc}
7686 inhibits the search of the default location for the GNAT Run Time
7687 Library (RTL) source files.
7688
7689 The compiler outputs its object files and ALI files in the current
7690 working directory.
7691 @ifclear vms
7692 Caution: The object file can be redirected with the @option{-o} switch;
7693 however, @command{gcc} and @code{gnat1} have not been coordinated on this
7694 so the @file{ALI} file will not go to the right place. Therefore, you should
7695 avoid using the @option{-o} switch.
7696 @end ifclear
7697
7698 @findex System.IO
7699 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
7700 children make up the GNAT RTL, together with the simple @code{System.IO}
7701 package used in the @code{"Hello World"} example. The sources for these units
7702 are needed by the compiler and are kept together in one directory. Not
7703 all of the bodies are needed, but all of the sources are kept together
7704 anyway. In a normal installation, you need not specify these directory
7705 names when compiling or binding. Either the environment variables or
7706 the built-in defaults cause these files to be found.
7707
7708 In addition to the language-defined hierarchies (@code{System}, @code{Ada} and
7709 @code{Interfaces}), the GNAT distribution provides a fourth hierarchy,
7710 consisting of child units of @code{GNAT}. This is a collection of generally
7711 useful types, subprograms, etc. @xref{Top, GNAT Reference Manual, About
7712 This Guid, gnat_rm, GNAT Reference Manual}, for further details.
7713
7714 Besides simplifying access to the RTL, a major use of search paths is
7715 in compiling sources from multiple directories. This can make
7716 development environments much more flexible.
7717
7718 @node Order of Compilation Issues
7719 @section Order of Compilation Issues
7720
7721 @noindent
7722 If, in our earlier example, there was a spec for the @code{hello}
7723 procedure, it would be contained in the file @file{hello.ads}; yet this
7724 file would not have to be explicitly compiled. This is the result of the
7725 model we chose to implement library management. Some of the consequences
7726 of this model are as follows:
7727
7728 @itemize @bullet
7729 @item
7730 There is no point in compiling specs (except for package
7731 specs with no bodies) because these are compiled as needed by clients. If
7732 you attempt a useless compilation, you will receive an error message.
7733 It is also useless to compile subunits because they are compiled as needed
7734 by the parent.
7735
7736 @item
7737 There are no order of compilation requirements: performing a
7738 compilation never obsoletes anything. The only way you can obsolete
7739 something and require recompilations is to modify one of the
7740 source files on which it depends.
7741
7742 @item
7743 There is no library as such, apart from the ALI files
7744 (@pxref{The Ada Library Information Files}, for information on the format
7745 of these files). For now we find it convenient to create separate ALI files,
7746 but eventually the information therein may be incorporated into the object
7747 file directly.
7748
7749 @item
7750 When you compile a unit, the source files for the specs of all units
7751 that it @code{with}'s, all its subunits, and the bodies of any generics it
7752 instantiates must be available (reachable by the search-paths mechanism
7753 described above), or you will receive a fatal error message.
7754 @end itemize
7755
7756 @node Examples
7757 @section Examples
7758
7759 @noindent
7760 The following are some typical Ada compilation command line examples:
7761
7762 @table @code
7763 @item $ gcc -c xyz.adb
7764 Compile body in file @file{xyz.adb} with all default options.
7765
7766 @ifclear vms
7767 @item $ gcc -c -O2 -gnata xyz-def.adb
7768 @end ifclear
7769 @ifset vms
7770 @item $ GNAT COMPILE /OPTIMIZE=ALL -gnata xyz-def.adb
7771 @end ifset
7772
7773 Compile the child unit package in file @file{xyz-def.adb} with extensive
7774 optimizations, and pragma @code{Assert}/@code{Debug} statements
7775 enabled.
7776
7777 @item $ gcc -c -gnatc abc-def.adb
7778 Compile the subunit in file @file{abc-def.adb} in semantic-checking-only
7779 mode.
7780 @end table
7781
7782 @node Binding Using gnatbind
7783 @chapter Binding Using @code{gnatbind}
7784 @findex gnatbind
7785
7786 @menu
7787 * Running gnatbind::
7788 * Switches for gnatbind::
7789 * Command-Line Access::
7790 * Search Paths for gnatbind::
7791 * Examples of gnatbind Usage::
7792 @end menu
7793
7794 @noindent
7795 This chapter describes the GNAT binder, @code{gnatbind}, which is used
7796 to bind compiled GNAT objects.
7797
7798 Note: to invoke @code{gnatbind} with a project file, use the @code{gnat}
7799 driver (see @ref{The GNAT Driver and Project Files}).
7800
7801 The @code{gnatbind} program performs four separate functions:
7802
7803 @enumerate
7804 @item
7805 Checks that a program is consistent, in accordance with the rules in
7806 Chapter 10 of the Ada Reference Manual. In particular, error
7807 messages are generated if a program uses inconsistent versions of a
7808 given unit.
7809
7810 @item
7811 Checks that an acceptable order of elaboration exists for the program
7812 and issues an error message if it cannot find an order of elaboration
7813 that satisfies the rules in Chapter 10 of the Ada Language Manual.
7814
7815 @item
7816 Generates a main program incorporating the given elaboration order.
7817 This program is a small Ada package (body and spec) that
7818 must be subsequently compiled
7819 using the GNAT compiler. The necessary compilation step is usually
7820 performed automatically by @command{gnatlink}. The two most important
7821 functions of this program
7822 are to call the elaboration routines of units in an appropriate order
7823 and to call the main program.
7824
7825 @item
7826 Determines the set of object files required by the given main program.
7827 This information is output in the forms of comments in the generated program,
7828 to be read by the @command{gnatlink} utility used to link the Ada application.
7829 @end enumerate
7830
7831 @node Running gnatbind
7832 @section Running @code{gnatbind}
7833
7834 @noindent
7835 The form of the @code{gnatbind} command is
7836
7837 @smallexample
7838 $ gnatbind @ovar{switches} @var{mainprog}@r{[}.ali@r{]} @ovar{switches}
7839 @end smallexample
7840
7841 @noindent
7842 where @file{@var{mainprog}.adb} is the Ada file containing the main program
7843 unit body. If no switches are specified, @code{gnatbind} constructs an Ada
7844 package in two files whose names are
7845 @file{b~@var{mainprog}.ads}, and @file{b~@var{mainprog}.adb}.
7846 For example, if given the
7847 parameter @file{hello.ali}, for a main program contained in file
7848 @file{hello.adb}, the binder output files would be @file{b~hello.ads}
7849 and @file{b~hello.adb}.
7850
7851 When doing consistency checking, the binder takes into consideration
7852 any source files it can locate. For example, if the binder determines
7853 that the given main program requires the package @code{Pack}, whose
7854 @file{.ALI}
7855 file is @file{pack.ali} and whose corresponding source spec file is
7856 @file{pack.ads}, it attempts to locate the source file @file{pack.ads}
7857 (using the same search path conventions as previously described for the
7858 @command{gcc} command). If it can locate this source file, it checks that
7859 the time stamps
7860 or source checksums of the source and its references to in @file{ALI} files
7861 match. In other words, any @file{ALI} files that mentions this spec must have
7862 resulted from compiling this version of the source file (or in the case
7863 where the source checksums match, a version close enough that the
7864 difference does not matter).
7865
7866 @cindex Source files, use by binder
7867 The effect of this consistency checking, which includes source files, is
7868 that the binder ensures that the program is consistent with the latest
7869 version of the source files that can be located at bind time. Editing a
7870 source file without compiling files that depend on the source file cause
7871 error messages to be generated by the binder.
7872
7873 For example, suppose you have a main program @file{hello.adb} and a
7874 package @code{P}, from file @file{p.ads} and you perform the following
7875 steps:
7876
7877 @enumerate
7878 @item
7879 Enter @code{gcc -c hello.adb} to compile the main program.
7880
7881 @item
7882 Enter @code{gcc -c p.ads} to compile package @code{P}.
7883
7884 @item
7885 Edit file @file{p.ads}.
7886
7887 @item
7888 Enter @code{gnatbind hello}.
7889 @end enumerate
7890
7891 @noindent
7892 At this point, the file @file{p.ali} contains an out-of-date time stamp
7893 because the file @file{p.ads} has been edited. The attempt at binding
7894 fails, and the binder generates the following error messages:
7895
7896 @smallexample
7897 error: "hello.adb" must be recompiled ("p.ads" has been modified)
7898 error: "p.ads" has been modified and must be recompiled
7899 @end smallexample
7900
7901 @noindent
7902 Now both files must be recompiled as indicated, and then the bind can
7903 succeed, generating a main program. You need not normally be concerned
7904 with the contents of this file, but for reference purposes a sample
7905 binder output file is given in @ref{Example of Binder Output File}.
7906
7907 In most normal usage, the default mode of @command{gnatbind} which is to
7908 generate the main package in Ada, as described in the previous section.
7909 In particular, this means that any Ada programmer can read and understand
7910 the generated main program. It can also be debugged just like any other
7911 Ada code provided the @option{^-g^/DEBUG^} switch is used for
7912 @command{gnatbind} and @command{gnatlink}.
7913
7914 However for some purposes it may be convenient to generate the main
7915 program in C rather than Ada. This may for example be helpful when you
7916 are generating a mixed language program with the main program in C. The
7917 GNAT compiler itself is an example.
7918 The use of the @option{^-C^/BIND_FILE=C^} switch
7919 for both @code{gnatbind} and @command{gnatlink} will cause the program to
7920 be generated in C (and compiled using the gnu C compiler).
7921
7922 @node Switches for gnatbind
7923 @section Switches for @command{gnatbind}
7924
7925 @noindent
7926 The following switches are available with @code{gnatbind}; details will
7927 be presented in subsequent sections.
7928
7929 @menu
7930 * Consistency-Checking Modes::
7931 * Binder Error Message Control::
7932 * Elaboration Control::
7933 * Output Control::
7934 * Binding with Non-Ada Main Programs::
7935 * Binding Programs with No Main Subprogram::
7936 @end menu
7937
7938 @table @option
7939 @c !sort!
7940
7941 @item --version
7942 @cindex @option{--version} @command{gnatbind}
7943 Display Copyright and version, then exit disregarding all other options.
7944
7945 @item --help
7946 @cindex @option{--help} @command{gnatbind}
7947 If @option{--version} was not used, display usage, then exit disregarding
7948 all other options.
7949
7950 @item -a
7951 @cindex @option{-a} @command{gnatbind}
7952 Indicates that, if supported by the platform, the adainit procedure should
7953 be treated as an initialisation routine by the linker (a constructor). This
7954 is intended to be used by the Project Manager to automatically initialize
7955 shared Stand-Alone Libraries.
7956
7957 @item ^-aO^/OBJECT_SEARCH^
7958 @cindex @option{^-aO^/OBJECT_SEARCH^} (@command{gnatbind})
7959 Specify directory to be searched for ALI files.
7960
7961 @item ^-aI^/SOURCE_SEARCH^
7962 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatbind})
7963 Specify directory to be searched for source file.
7964
7965 @item ^-A^/BIND_FILE=ADA^
7966 @cindex @option{^-A^/BIND_FILE=ADA^} (@command{gnatbind})
7967 Generate binder program in Ada (default)
7968
7969 @item ^-b^/REPORT_ERRORS=BRIEF^
7970 @cindex @option{^-b^/REPORT_ERRORS=BRIEF^} (@command{gnatbind})
7971 Generate brief messages to @file{stderr} even if verbose mode set.
7972
7973 @item ^-c^/NOOUTPUT^
7974 @cindex @option{^-c^/NOOUTPUT^} (@command{gnatbind})
7975 Check only, no generation of binder output file.
7976
7977 @item ^-C^/BIND_FILE=C^
7978 @cindex @option{^-C^/BIND_FILE=C^} (@command{gnatbind})
7979 Generate binder program in C
7980
7981 @item ^-d^/DEFAULT_STACK_SIZE=^@var{nn}@r{[}k@r{|}m@r{]}
7982 @cindex @option{^-d^/DEFAULT_STACK_SIZE=^@var{nn}@r{[}k@r{|}m@r{]}} (@command{gnatbind})
7983 This switch can be used to change the default task stack size value
7984 to a specified size @var{nn}, which is expressed in bytes by default, or
7985 in kilobytes when suffixed with @var{k} or in megabytes when suffixed
7986 with @var{m}.
7987 In the absence of a @samp{@r{[}k@r{|}m@r{]}} suffix, this switch is equivalent,
7988 in effect, to completing all task specs with
7989 @smallexample @c ada
7990 pragma Storage_Size (nn);
7991 @end smallexample
7992 When they do not already have such a pragma.
7993
7994 @item ^-D^/DEFAULT_SECONDARY_STACK_SIZE=^@var{nn}@r{[}k@r{|}m@r{]}
7995 @cindex @option{^-D^/DEFAULT_SECONDARY_STACK_SIZE=nnnnn^} (@command{gnatbind})
7996 This switch can be used to change the default secondary stack size value
7997 to a specified size @var{nn}, which is expressed in bytes by default, or
7998 in kilobytes when suffixed with @var{k} or in megabytes when suffixed
7999 with @var{m}.
8000
8001 The secondary stack is used to deal with functions that return a variable
8002 sized result, for example a function returning an unconstrained
8003 String. There are two ways in which this secondary stack is allocated.
8004
8005 For most targets, the secondary stack is growing on demand and is allocated
8006 as a chain of blocks in the heap. The -D option is not very
8007 relevant. It only give some control over the size of the allocated
8008 blocks (whose size is the minimum of the default secondary stack size value,
8009 and the actual size needed for the current allocation request).
8010
8011 For certain targets, notably VxWorks 653,
8012 the secondary stack is allocated by carving off a fixed ratio chunk of the
8013 primary task stack. The -D option is used to define the
8014 size of the environment task's secondary stack.
8015
8016 @item ^-e^/ELABORATION_DEPENDENCIES^
8017 @cindex @option{^-e^/ELABORATION_DEPENDENCIES^} (@command{gnatbind})
8018 Output complete list of elaboration-order dependencies.
8019
8020 @item ^-E^/STORE_TRACEBACKS^
8021 @cindex @option{^-E^/STORE_TRACEBACKS^} (@command{gnatbind})
8022 Store tracebacks in exception occurrences when the target supports it.
8023 This is the default with the zero cost exception mechanism.
8024 @ignore
8025 @c The following may get moved to an appendix
8026 This option is currently supported on the following targets:
8027 all x86 ports, Solaris, Windows, HP-UX, AIX, PowerPC VxWorks and Alpha VxWorks.
8028 @end ignore
8029 See also the packages @code{GNAT.Traceback} and
8030 @code{GNAT.Traceback.Symbolic} for more information.
8031 @ifclear vms
8032 Note that on x86 ports, you must not use @option{-fomit-frame-pointer}
8033 @command{gcc} option.
8034 @end ifclear
8035
8036 @item ^-F^/FORCE_ELABS_FLAGS^
8037 @cindex @option{^-F^/FORCE_ELABS_FLAGS^} (@command{gnatbind})
8038 Force the checks of elaboration flags. @command{gnatbind} does not normally
8039 generate checks of elaboration flags for the main executable, except when
8040 a Stand-Alone Library is used. However, there are cases when this cannot be
8041 detected by gnatbind. An example is importing an interface of a Stand-Alone
8042 Library through a pragma Import and only specifying through a linker switch
8043 this Stand-Alone Library. This switch is used to guarantee that elaboration
8044 flag checks are generated.
8045
8046 @item ^-h^/HELP^
8047 @cindex @option{^-h^/HELP^} (@command{gnatbind})
8048 Output usage (help) information
8049
8050 @item ^-I^/SEARCH^
8051 @cindex @option{^-I^/SEARCH^} (@command{gnatbind})
8052 Specify directory to be searched for source and ALI files.
8053
8054 @item ^-I-^/NOCURRENT_DIRECTORY^
8055 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gnatbind})
8056 Do not look for sources in the current directory where @code{gnatbind} was
8057 invoked, and do not look for ALI files in the directory containing the
8058 ALI file named in the @code{gnatbind} command line.
8059
8060 @item ^-l^/ORDER_OF_ELABORATION^
8061 @cindex @option{^-l^/ORDER_OF_ELABORATION^} (@command{gnatbind})
8062 Output chosen elaboration order.
8063
8064 @item ^-L@var{xxx}^/BUILD_LIBRARY=@var{xxx}^
8065 @cindex @option{^-L^/BUILD_LIBRARY^} (@command{gnatbind})
8066 Bind the units for library building. In this case the adainit and
8067 adafinal procedures (@pxref{Binding with Non-Ada Main Programs})
8068 are renamed to ^@var{xxx}init^@var{XXX}INIT^ and
8069 ^@var{xxx}final^@var{XXX}FINAL^.
8070 Implies ^-n^/NOCOMPILE^.
8071 @ifclear vms
8072 (@xref{GNAT and Libraries}, for more details.)
8073 @end ifclear
8074 @ifset vms
8075 On OpenVMS, these init and final procedures are exported in uppercase
8076 letters. For example if /BUILD_LIBRARY=toto is used, the exported name of
8077 the init procedure will be "TOTOINIT" and the exported name of the final
8078 procedure will be "TOTOFINAL".
8079 @end ifset
8080
8081 @item ^-Mxyz^/RENAME_MAIN=xyz^
8082 @cindex @option{^-M^/RENAME_MAIN^} (@command{gnatbind})
8083 Rename generated main program from main to xyz. This option is
8084 supported on cross environments only.
8085
8086 @item ^-m^/ERROR_LIMIT=^@var{n}
8087 @cindex @option{^-m^/ERROR_LIMIT^} (@command{gnatbind})
8088 Limit number of detected errors or warnings to @var{n}, where @var{n} is
8089 in the range 1..999999. The default value if no switch is
8090 given is 9999. If the number of warnings reaches this limit, then a
8091 message is output and further warnings are suppressed, the bind
8092 continues in this case. If the number of errors reaches this
8093 limit, then a message is output and the bind is abandoned.
8094 A value of zero means that no limit is enforced. The equal
8095 sign is optional.
8096
8097 @ifset unw
8098 Furthermore, under Windows, the sources pointed to by the libraries path
8099 set in the registry are not searched for.
8100 @end ifset
8101
8102 @item ^-n^/NOMAIN^
8103 @cindex @option{^-n^/NOMAIN^} (@command{gnatbind})
8104 No main program.
8105
8106 @item -nostdinc
8107 @cindex @option{-nostdinc} (@command{gnatbind})
8108 Do not look for sources in the system default directory.
8109
8110 @item -nostdlib
8111 @cindex @option{-nostdlib} (@command{gnatbind})
8112 Do not look for library files in the system default directory.
8113
8114 @item --RTS=@var{rts-path}
8115 @cindex @option{--RTS} (@code{gnatbind})
8116 Specifies the default location of the runtime library. Same meaning as the
8117 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
8118
8119 @item ^-o ^/OUTPUT=^@var{file}
8120 @cindex @option{^-o ^/OUTPUT^} (@command{gnatbind})
8121 Name the output file @var{file} (default is @file{b~@var{xxx}.adb}).
8122 Note that if this option is used, then linking must be done manually,
8123 gnatlink cannot be used.
8124
8125 @item ^-O^/OBJECT_LIST^
8126 @cindex @option{^-O^/OBJECT_LIST^} (@command{gnatbind})
8127 Output object list.
8128
8129 @item ^-p^/PESSIMISTIC_ELABORATION^
8130 @cindex @option{^-p^/PESSIMISTIC_ELABORATION^} (@command{gnatbind})
8131 Pessimistic (worst-case) elaboration order
8132
8133 @item ^-R^-R^
8134 @cindex @option{^-R^-R^} (@command{gnatbind})
8135 Output closure source list.
8136
8137 @item ^-s^/READ_SOURCES=ALL^
8138 @cindex @option{^-s^/READ_SOURCES=ALL^} (@command{gnatbind})
8139 Require all source files to be present.
8140
8141 @item ^-S@var{xxx}^/INITIALIZE_SCALARS=@var{xxx}^
8142 @cindex @option{^-S^/INITIALIZE_SCALARS^} (@command{gnatbind})
8143 Specifies the value to be used when detecting uninitialized scalar
8144 objects with pragma Initialize_Scalars.
8145 The @var{xxx} ^string specified with the switch^option^ may be either
8146 @itemize @bullet
8147 @item ``@option{^in^INVALID^}'' requesting an invalid value where possible
8148 @item ``@option{^lo^LOW^}'' for the lowest possible value
8149 @item ``@option{^hi^HIGH^}'' for the highest possible value
8150 @item ``@option{@var{xx}}'' for a value consisting of repeated bytes with the
8151 value @code{16#@var{xx}#} (i.e., @var{xx} is a string of two hexadecimal digits).
8152 @end itemize
8153
8154 In addition, you can specify @option{-Sev} to indicate that the value is
8155 to be set at run time. In this case, the program will look for an environment
8156 @cindex GNAT_INIT_SCALARS
8157 variable of the form @env{GNAT_INIT_SCALARS=@var{xx}}, where @var{xx} is one
8158 of @option{in/lo/hi/@var{xx}} with the same meanings as above.
8159 If no environment variable is found, or if it does not have a valid value,
8160 then the default is @option{in} (invalid values).
8161
8162 @ifclear vms
8163 @item -static
8164 @cindex @option{-static} (@code{gnatbind})
8165 Link against a static GNAT run time.
8166
8167 @item -shared
8168 @cindex @option{-shared} (@code{gnatbind})
8169 Link against a shared GNAT run time when available.
8170 @end ifclear
8171
8172 @item ^-t^/NOTIME_STAMP_CHECK^
8173 @cindex @option{^-t^/NOTIME_STAMP_CHECK^} (@code{gnatbind})
8174 Tolerate time stamp and other consistency errors
8175
8176 @item ^-T@var{n}^/TIME_SLICE=@var{n}^
8177 @cindex @option{^-T^/TIME_SLICE^} (@code{gnatbind})
8178 Set the time slice value to @var{n} milliseconds. If the system supports
8179 the specification of a specific time slice value, then the indicated value
8180 is used. If the system does not support specific time slice values, but
8181 does support some general notion of round-robin scheduling, then any
8182 nonzero value will activate round-robin scheduling.
8183
8184 A value of zero is treated specially. It turns off time
8185 slicing, and in addition, indicates to the tasking run time that the
8186 semantics should match as closely as possible the Annex D
8187 requirements of the Ada RM, and in particular sets the default
8188 scheduling policy to @code{FIFO_Within_Priorities}.
8189
8190 @item ^-u@var{n}^/DYNAMIC_STACK_USAGE=@var{n}^
8191 @cindex @option{^-u^/DYNAMIC_STACK_USAGE^} (@code{gnatbind})
8192 Enable dynamic stack usage, with @var{n} results stored and displayed
8193 at program termination. A result is generated when a task
8194 terminates. Results that can't be stored are displayed on the fly, at
8195 task termination. This option is currently not supported on Itanium
8196 platforms. (See @ref{Dynamic Stack Usage Analysis} for details.)
8197
8198 @item ^-v^/REPORT_ERRORS=VERBOSE^
8199 @cindex @option{^-v^/REPORT_ERRORS=VERBOSE^} (@code{gnatbind})
8200 Verbose mode. Write error messages, header, summary output to
8201 @file{stdout}.
8202
8203 @ifclear vms
8204 @item -w@var{x}
8205 @cindex @option{-w} (@code{gnatbind})
8206 Warning mode (@var{x}=s/e for suppress/treat as error)
8207 @end ifclear
8208
8209 @ifset vms
8210 @item /WARNINGS=NORMAL
8211 @cindex @option{/WARNINGS} (@code{gnatbind})
8212 Normal warnings mode. Warnings are issued but ignored
8213
8214 @item /WARNINGS=SUPPRESS
8215 @cindex @option{/WARNINGS} (@code{gnatbind})
8216 All warning messages are suppressed
8217
8218 @item /WARNINGS=ERROR
8219 @cindex @option{/WARNINGS} (@code{gnatbind})
8220 Warning messages are treated as fatal errors
8221 @end ifset
8222
8223 @item ^-Wx^/WIDE_CHARACTER_ENCODING=^@var{e}
8224 @cindex @option{^-Wx^/WIDE_CHARACTER_ENCODING^} (@code{gnatbind})
8225 Override default wide character encoding for standard Text_IO files.
8226
8227 @item ^-x^/READ_SOURCES=NONE^
8228 @cindex @option{^-x^/READ_SOURCES^} (@code{gnatbind})
8229 Exclude source files (check object consistency only).
8230
8231 @ifset vms
8232 @item /READ_SOURCES=AVAILABLE
8233 @cindex @option{/READ_SOURCES} (@code{gnatbind})
8234 Default mode, in which sources are checked for consistency only if
8235 they are available.
8236 @end ifset
8237
8238 @item ^-y^/ENABLE_LEAP_SECONDS^
8239 @cindex @option{^-y^/ENABLE_LEAP_SECONDS^} (@code{gnatbind})
8240 Enable leap seconds support in @code{Ada.Calendar} and its children.
8241
8242 @item ^-z^/ZERO_MAIN^
8243 @cindex @option{^-z^/ZERO_MAIN^} (@code{gnatbind})
8244 No main subprogram.
8245 @end table
8246
8247 @ifclear vms
8248 @noindent
8249 You may obtain this listing of switches by running @code{gnatbind} with
8250 no arguments.
8251 @end ifclear
8252
8253 @node Consistency-Checking Modes
8254 @subsection Consistency-Checking Modes
8255
8256 @noindent
8257 As described earlier, by default @code{gnatbind} checks
8258 that object files are consistent with one another and are consistent
8259 with any source files it can locate. The following switches control binder
8260 access to sources.
8261
8262 @table @option
8263 @c !sort!
8264 @item ^-s^/READ_SOURCES=ALL^
8265 @cindex @option{^-s^/READ_SOURCES=ALL^} (@code{gnatbind})
8266 Require source files to be present. In this mode, the binder must be
8267 able to locate all source files that are referenced, in order to check
8268 their consistency. In normal mode, if a source file cannot be located it
8269 is simply ignored. If you specify this switch, a missing source
8270 file is an error.
8271
8272 @item ^-Wx^/WIDE_CHARACTER_ENCODING=^@var{e}
8273 @cindex @option{^-Wx^/WIDE_CHARACTER_ENCODING^} (@code{gnatbind})
8274 Override default wide character encoding for standard Text_IO files.
8275 Normally the default wide character encoding method used for standard
8276 [Wide_[Wide_]]Text_IO files is taken from the encoding specified for
8277 the main source input (see description of switch
8278 @option{^-gnatWx^/WIDE_CHARACTER_ENCODING^} for the compiler). The
8279 use of this switch for the binder (which has the same set of
8280 possible arguments) overrides this default as specified.
8281
8282 @item ^-x^/READ_SOURCES=NONE^
8283 @cindex @option{^-x^/READ_SOURCES=NONE^} (@code{gnatbind})
8284 Exclude source files. In this mode, the binder only checks that ALI
8285 files are consistent with one another. Source files are not accessed.
8286 The binder runs faster in this mode, and there is still a guarantee that
8287 the resulting program is self-consistent.
8288 If a source file has been edited since it was last compiled, and you
8289 specify this switch, the binder will not detect that the object
8290 file is out of date with respect to the source file. Note that this is the
8291 mode that is automatically used by @command{gnatmake} because in this
8292 case the checking against sources has already been performed by
8293 @command{gnatmake} in the course of compilation (i.e.@: before binding).
8294
8295 @ifset vms
8296 @item /READ_SOURCES=AVAILABLE
8297 @cindex @code{/READ_SOURCES=AVAILABLE} (@code{gnatbind})
8298 This is the default mode in which source files are checked if they are
8299 available, and ignored if they are not available.
8300 @end ifset
8301 @end table
8302
8303 @node Binder Error Message Control
8304 @subsection Binder Error Message Control
8305
8306 @noindent
8307 The following switches provide control over the generation of error
8308 messages from the binder:
8309
8310 @table @option
8311 @c !sort!
8312 @item ^-v^/REPORT_ERRORS=VERBOSE^
8313 @cindex @option{^-v^/REPORT_ERRORS=VERBOSE^} (@code{gnatbind})
8314 Verbose mode. In the normal mode, brief error messages are generated to
8315 @file{stderr}. If this switch is present, a header is written
8316 to @file{stdout} and any error messages are directed to @file{stdout}.
8317 All that is written to @file{stderr} is a brief summary message.
8318
8319 @item ^-b^/REPORT_ERRORS=BRIEF^
8320 @cindex @option{^-b^/REPORT_ERRORS=BRIEF^} (@code{gnatbind})
8321 Generate brief error messages to @file{stderr} even if verbose mode is
8322 specified. This is relevant only when used with the
8323 @option{^-v^/REPORT_ERRORS=VERBOSE^} switch.
8324
8325 @ifclear vms
8326 @item -m@var{n}
8327 @cindex @option{-m} (@code{gnatbind})
8328 Limits the number of error messages to @var{n}, a decimal integer in the
8329 range 1-999. The binder terminates immediately if this limit is reached.
8330
8331 @item -M@var{xxx}
8332 @cindex @option{-M} (@code{gnatbind})
8333 Renames the generated main program from @code{main} to @code{xxx}.
8334 This is useful in the case of some cross-building environments, where
8335 the actual main program is separate from the one generated
8336 by @code{gnatbind}.
8337 @end ifclear
8338
8339 @item ^-ws^/WARNINGS=SUPPRESS^
8340 @cindex @option{^-ws^/WARNINGS=SUPPRESS^} (@code{gnatbind})
8341 @cindex Warnings
8342 Suppress all warning messages.
8343
8344 @item ^-we^/WARNINGS=ERROR^
8345 @cindex @option{^-we^/WARNINGS=ERROR^} (@code{gnatbind})
8346 Treat any warning messages as fatal errors.
8347
8348 @ifset vms
8349 @item /WARNINGS=NORMAL
8350 Standard mode with warnings generated, but warnings do not get treated
8351 as errors.
8352 @end ifset
8353
8354 @item ^-t^/NOTIME_STAMP_CHECK^
8355 @cindex @option{^-t^/NOTIME_STAMP_CHECK^} (@code{gnatbind})
8356 @cindex Time stamp checks, in binder
8357 @cindex Binder consistency checks
8358 @cindex Consistency checks, in binder
8359 The binder performs a number of consistency checks including:
8360
8361 @itemize @bullet
8362 @item
8363 Check that time stamps of a given source unit are consistent
8364 @item
8365 Check that checksums of a given source unit are consistent
8366 @item
8367 Check that consistent versions of @code{GNAT} were used for compilation
8368 @item
8369 Check consistency of configuration pragmas as required
8370 @end itemize
8371
8372 @noindent
8373 Normally failure of such checks, in accordance with the consistency
8374 requirements of the Ada Reference Manual, causes error messages to be
8375 generated which abort the binder and prevent the output of a binder
8376 file and subsequent link to obtain an executable.
8377
8378 The @option{^-t^/NOTIME_STAMP_CHECK^} switch converts these error messages
8379 into warnings, so that
8380 binding and linking can continue to completion even in the presence of such
8381 errors. The result may be a failed link (due to missing symbols), or a
8382 non-functional executable which has undefined semantics.
8383 @emph{This means that
8384 @option{^-t^/NOTIME_STAMP_CHECK^} should be used only in unusual situations,
8385 with extreme care.}
8386 @end table
8387
8388 @node Elaboration Control
8389 @subsection Elaboration Control
8390
8391 @noindent
8392 The following switches provide additional control over the elaboration
8393 order. For full details see @ref{Elaboration Order Handling in GNAT}.
8394
8395 @table @option
8396 @item ^-p^/PESSIMISTIC_ELABORATION^
8397 @cindex @option{^-p^/PESSIMISTIC_ELABORATION^} (@code{gnatbind})
8398 Normally the binder attempts to choose an elaboration order that is
8399 likely to minimize the likelihood of an elaboration order error resulting
8400 in raising a @code{Program_Error} exception. This switch reverses the
8401 action of the binder, and requests that it deliberately choose an order
8402 that is likely to maximize the likelihood of an elaboration error.
8403 This is useful in ensuring portability and avoiding dependence on
8404 accidental fortuitous elaboration ordering.
8405
8406 Normally it only makes sense to use the @option{^-p^/PESSIMISTIC_ELABORATION^}
8407 switch if dynamic
8408 elaboration checking is used (@option{-gnatE} switch used for compilation).
8409 This is because in the default static elaboration mode, all necessary
8410 @code{Elaborate} and @code{Elaborate_All} pragmas are implicitly inserted.
8411 These implicit pragmas are still respected by the binder in
8412 @option{^-p^/PESSIMISTIC_ELABORATION^} mode, so a
8413 safe elaboration order is assured.
8414 @end table
8415
8416 @node Output Control
8417 @subsection Output Control
8418
8419 @noindent
8420 The following switches allow additional control over the output
8421 generated by the binder.
8422
8423 @table @option
8424 @c !sort!
8425
8426 @item ^-A^/BIND_FILE=ADA^
8427 @cindex @option{^-A^/BIND_FILE=ADA^} (@code{gnatbind})
8428 Generate binder program in Ada (default). The binder program is named
8429 @file{b~@var{mainprog}.adb} by default. This can be changed with
8430 @option{^-o^/OUTPUT^} @code{gnatbind} option.
8431
8432 @item ^-c^/NOOUTPUT^
8433 @cindex @option{^-c^/NOOUTPUT^} (@code{gnatbind})
8434 Check only. Do not generate the binder output file. In this mode the
8435 binder performs all error checks but does not generate an output file.
8436
8437 @item ^-C^/BIND_FILE=C^
8438 @cindex @option{^-C^/BIND_FILE=C^} (@code{gnatbind})
8439 Generate binder program in C. The binder program is named
8440 @file{b_@var{mainprog}.c}.
8441 This can be changed with @option{^-o^/OUTPUT^} @code{gnatbind}
8442 option.
8443
8444 @item ^-e^/ELABORATION_DEPENDENCIES^
8445 @cindex @option{^-e^/ELABORATION_DEPENDENCIES^} (@code{gnatbind})
8446 Output complete list of elaboration-order dependencies, showing the
8447 reason for each dependency. This output can be rather extensive but may
8448 be useful in diagnosing problems with elaboration order. The output is
8449 written to @file{stdout}.
8450
8451 @item ^-h^/HELP^
8452 @cindex @option{^-h^/HELP^} (@code{gnatbind})
8453 Output usage information. The output is written to @file{stdout}.
8454
8455 @item ^-K^/LINKER_OPTION_LIST^
8456 @cindex @option{^-K^/LINKER_OPTION_LIST^} (@code{gnatbind})
8457 Output linker options to @file{stdout}. Includes library search paths,
8458 contents of pragmas Ident and Linker_Options, and libraries added
8459 by @code{gnatbind}.
8460
8461 @item ^-l^/ORDER_OF_ELABORATION^
8462 @cindex @option{^-l^/ORDER_OF_ELABORATION^} (@code{gnatbind})
8463 Output chosen elaboration order. The output is written to @file{stdout}.
8464
8465 @item ^-O^/OBJECT_LIST^
8466 @cindex @option{^-O^/OBJECT_LIST^} (@code{gnatbind})
8467 Output full names of all the object files that must be linked to provide
8468 the Ada component of the program. The output is written to @file{stdout}.
8469 This list includes the files explicitly supplied and referenced by the user
8470 as well as implicitly referenced run-time unit files. The latter are
8471 omitted if the corresponding units reside in shared libraries. The
8472 directory names for the run-time units depend on the system configuration.
8473
8474 @item ^-o ^/OUTPUT=^@var{file}
8475 @cindex @option{^-o^/OUTPUT^} (@code{gnatbind})
8476 Set name of output file to @var{file} instead of the normal
8477 @file{b~@var{mainprog}.adb} default. Note that @var{file} denote the Ada
8478 binder generated body filename. In C mode you would normally give
8479 @var{file} an extension of @file{.c} because it will be a C source program.
8480 Note that if this option is used, then linking must be done manually.
8481 It is not possible to use gnatlink in this case, since it cannot locate
8482 the binder file.
8483
8484 @item ^-r^/RESTRICTION_LIST^
8485 @cindex @option{^-r^/RESTRICTION_LIST^} (@code{gnatbind})
8486 Generate list of @code{pragma Restrictions} that could be applied to
8487 the current unit. This is useful for code audit purposes, and also may
8488 be used to improve code generation in some cases.
8489
8490 @end table
8491
8492 @node Binding with Non-Ada Main Programs
8493 @subsection Binding with Non-Ada Main Programs
8494
8495 @noindent
8496 In our description so far we have assumed that the main
8497 program is in Ada, and that the task of the binder is to generate a
8498 corresponding function @code{main} that invokes this Ada main
8499 program. GNAT also supports the building of executable programs where
8500 the main program is not in Ada, but some of the called routines are
8501 written in Ada and compiled using GNAT (@pxref{Mixed Language Programming}).
8502 The following switch is used in this situation:
8503
8504 @table @option
8505 @item ^-n^/NOMAIN^
8506 @cindex @option{^-n^/NOMAIN^} (@code{gnatbind})
8507 No main program. The main program is not in Ada.
8508 @end table
8509
8510 @noindent
8511 In this case, most of the functions of the binder are still required,
8512 but instead of generating a main program, the binder generates a file
8513 containing the following callable routines:
8514
8515 @table @code
8516 @item adainit
8517 @findex adainit
8518 You must call this routine to initialize the Ada part of the program by
8519 calling the necessary elaboration routines. A call to @code{adainit} is
8520 required before the first call to an Ada subprogram.
8521
8522 Note that it is assumed that the basic execution environment must be setup
8523 to be appropriate for Ada execution at the point where the first Ada
8524 subprogram is called. In particular, if the Ada code will do any
8525 floating-point operations, then the FPU must be setup in an appropriate
8526 manner. For the case of the x86, for example, full precision mode is
8527 required. The procedure GNAT.Float_Control.Reset may be used to ensure
8528 that the FPU is in the right state.
8529
8530 @item adafinal
8531 @findex adafinal
8532 You must call this routine to perform any library-level finalization
8533 required by the Ada subprograms. A call to @code{adafinal} is required
8534 after the last call to an Ada subprogram, and before the program
8535 terminates.
8536 @end table
8537
8538 @noindent
8539 If the @option{^-n^/NOMAIN^} switch
8540 @cindex @option{^-n^/NOMAIN^} (@command{gnatbind})
8541 @cindex Binder, multiple input files
8542 is given, more than one ALI file may appear on
8543 the command line for @code{gnatbind}. The normal @dfn{closure}
8544 calculation is performed for each of the specified units. Calculating
8545 the closure means finding out the set of units involved by tracing
8546 @code{with} references. The reason it is necessary to be able to
8547 specify more than one ALI file is that a given program may invoke two or
8548 more quite separate groups of Ada units.
8549
8550 The binder takes the name of its output file from the last specified ALI
8551 file, unless overridden by the use of the @option{^-o file^/OUTPUT=file^}.
8552 @cindex @option{^-o^/OUTPUT^} (@command{gnatbind})
8553 The output is an Ada unit in source form that can
8554 be compiled with GNAT unless the -C switch is used in which case the
8555 output is a C source file, which must be compiled using the C compiler.
8556 This compilation occurs automatically as part of the @command{gnatlink}
8557 processing.
8558
8559 Currently the GNAT run time requires a FPU using 80 bits mode
8560 precision. Under targets where this is not the default it is required to
8561 call GNAT.Float_Control.Reset before using floating point numbers (this
8562 include float computation, float input and output) in the Ada code. A
8563 side effect is that this could be the wrong mode for the foreign code
8564 where floating point computation could be broken after this call.
8565
8566 @node Binding Programs with No Main Subprogram
8567 @subsection Binding Programs with No Main Subprogram
8568
8569 @noindent
8570 It is possible to have an Ada program which does not have a main
8571 subprogram. This program will call the elaboration routines of all the
8572 packages, then the finalization routines.
8573
8574 The following switch is used to bind programs organized in this manner:
8575
8576 @table @option
8577 @item ^-z^/ZERO_MAIN^
8578 @cindex @option{^-z^/ZERO_MAIN^} (@code{gnatbind})
8579 Normally the binder checks that the unit name given on the command line
8580 corresponds to a suitable main subprogram. When this switch is used,
8581 a list of ALI files can be given, and the execution of the program
8582 consists of elaboration of these units in an appropriate order. Note
8583 that the default wide character encoding method for standard Text_IO
8584 files is always set to Brackets if this switch is set (you can use
8585 the binder switch
8586 @option{^-Wx^WIDE_CHARACTER_ENCODING^} to override this default).
8587 @end table
8588
8589 @node Command-Line Access
8590 @section Command-Line Access
8591
8592 @noindent
8593 The package @code{Ada.Command_Line} provides access to the command-line
8594 arguments and program name. In order for this interface to operate
8595 correctly, the two variables
8596
8597 @smallexample
8598 @group
8599 int gnat_argc;
8600 char **gnat_argv;
8601 @end group
8602 @end smallexample
8603
8604 @noindent
8605 @findex gnat_argv
8606 @findex gnat_argc
8607 are declared in one of the GNAT library routines. These variables must
8608 be set from the actual @code{argc} and @code{argv} values passed to the
8609 main program. With no @option{^n^/NOMAIN^} present, @code{gnatbind}
8610 generates the C main program to automatically set these variables.
8611 If the @option{^n^/NOMAIN^} switch is used, there is no automatic way to
8612 set these variables. If they are not set, the procedures in
8613 @code{Ada.Command_Line} will not be available, and any attempt to use
8614 them will raise @code{Constraint_Error}. If command line access is
8615 required, your main program must set @code{gnat_argc} and
8616 @code{gnat_argv} from the @code{argc} and @code{argv} values passed to
8617 it.
8618
8619 @node Search Paths for gnatbind
8620 @section Search Paths for @code{gnatbind}
8621
8622 @noindent
8623 The binder takes the name of an ALI file as its argument and needs to
8624 locate source files as well as other ALI files to verify object consistency.
8625
8626 For source files, it follows exactly the same search rules as @command{gcc}
8627 (@pxref{Search Paths and the Run-Time Library (RTL)}). For ALI files the
8628 directories searched are:
8629
8630 @enumerate
8631 @item
8632 The directory containing the ALI file named in the command line, unless
8633 the switch @option{^-I-^/NOCURRENT_DIRECTORY^} is specified.
8634
8635 @item
8636 All directories specified by @option{^-I^/SEARCH^}
8637 switches on the @code{gnatbind}
8638 command line, in the order given.
8639
8640 @item
8641 @findex ADA_PRJ_OBJECTS_FILE
8642 Each of the directories listed in the text file whose name is given
8643 by the @env{ADA_PRJ_OBJECTS_FILE} ^environment variable^logical name^.
8644
8645 @noindent
8646 @env{ADA_PRJ_OBJECTS_FILE} is normally set by gnatmake or by the ^gnat^GNAT^
8647 driver when project files are used. It should not normally be set
8648 by other means.
8649
8650 @item
8651 @findex ADA_OBJECTS_PATH
8652 Each of the directories listed in the value of the
8653 @env{ADA_OBJECTS_PATH} ^environment variable^logical name^.
8654 @ifset unw
8655 Construct this value
8656 exactly as the @env{PATH} environment variable: a list of directory
8657 names separated by colons (semicolons when working with the NT version
8658 of GNAT).
8659 @end ifset
8660 @ifset vms
8661 Normally, define this value as a logical name containing a comma separated
8662 list of directory names.
8663
8664 This variable can also be defined by means of an environment string
8665 (an argument to the HP C exec* set of functions).
8666
8667 Logical Name:
8668 @smallexample
8669 DEFINE ANOTHER_PATH FOO:[BAG]
8670 DEFINE ADA_OBJECTS_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
8671 @end smallexample
8672
8673 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
8674 first, followed by the standard Ada
8675 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADALIB].
8676 If this is not redefined, the user will obtain the HP Ada 83 IO packages
8677 (Text_IO, Sequential_IO, etc)
8678 instead of the standard Ada packages. Thus, in order to get the standard Ada
8679 packages by default, ADA_OBJECTS_PATH must be redefined.
8680 @end ifset
8681
8682 @item
8683 The content of the @file{ada_object_path} file which is part of the GNAT
8684 installation tree and is used to store standard libraries such as the
8685 GNAT Run Time Library (RTL) unless the switch @option{-nostdlib} is
8686 specified.
8687 @ifclear vms
8688 @ref{Installing a library}
8689 @end ifclear
8690 @end enumerate
8691
8692 @noindent
8693 In the binder the switch @option{^-I^/SEARCH^}
8694 @cindex @option{^-I^/SEARCH^} (@command{gnatbind})
8695 is used to specify both source and
8696 library file paths. Use @option{^-aI^/SOURCE_SEARCH^}
8697 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatbind})
8698 instead if you want to specify
8699 source paths only, and @option{^-aO^/LIBRARY_SEARCH^}
8700 @cindex @option{^-aO^/LIBRARY_SEARCH^} (@command{gnatbind})
8701 if you want to specify library paths
8702 only. This means that for the binder
8703 @option{^-I^/SEARCH=^}@var{dir} is equivalent to
8704 @option{^-aI^/SOURCE_SEARCH=^}@var{dir}
8705 @option{^-aO^/OBJECT_SEARCH=^}@var{dir}.
8706 The binder generates the bind file (a C language source file) in the
8707 current working directory.
8708
8709 @findex Ada
8710 @findex System
8711 @findex Interfaces
8712 @findex GNAT
8713 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
8714 children make up the GNAT Run-Time Library, together with the package
8715 GNAT and its children, which contain a set of useful additional
8716 library functions provided by GNAT. The sources for these units are
8717 needed by the compiler and are kept together in one directory. The ALI
8718 files and object files generated by compiling the RTL are needed by the
8719 binder and the linker and are kept together in one directory, typically
8720 different from the directory containing the sources. In a normal
8721 installation, you need not specify these directory names when compiling
8722 or binding. Either the environment variables or the built-in defaults
8723 cause these files to be found.
8724
8725 Besides simplifying access to the RTL, a major use of search paths is
8726 in compiling sources from multiple directories. This can make
8727 development environments much more flexible.
8728
8729 @node Examples of gnatbind Usage
8730 @section Examples of @code{gnatbind} Usage
8731
8732 @noindent
8733 This section contains a number of examples of using the GNAT binding
8734 utility @code{gnatbind}.
8735
8736 @table @code
8737 @item gnatbind hello
8738 The main program @code{Hello} (source program in @file{hello.adb}) is
8739 bound using the standard switch settings. The generated main program is
8740 @file{b~hello.adb}. This is the normal, default use of the binder.
8741
8742 @ifclear vms
8743 @item gnatbind hello -o mainprog.adb
8744 @end ifclear
8745 @ifset vms
8746 @item gnatbind HELLO.ALI /OUTPUT=Mainprog.ADB
8747 @end ifset
8748 The main program @code{Hello} (source program in @file{hello.adb}) is
8749 bound using the standard switch settings. The generated main program is
8750 @file{mainprog.adb} with the associated spec in
8751 @file{mainprog.ads}. Note that you must specify the body here not the
8752 spec, in the case where the output is in Ada. Note that if this option
8753 is used, then linking must be done manually, since gnatlink will not
8754 be able to find the generated file.
8755
8756 @ifclear vms
8757 @item gnatbind main -C -o mainprog.c -x
8758 @end ifclear
8759 @ifset vms
8760 @item gnatbind MAIN.ALI /BIND_FILE=C /OUTPUT=Mainprog.C /READ_SOURCES=NONE
8761 @end ifset
8762 The main program @code{Main} (source program in
8763 @file{main.adb}) is bound, excluding source files from the
8764 consistency checking, generating
8765 the file @file{mainprog.c}.
8766
8767 @ifclear vms
8768 @item gnatbind -x main_program -C -o mainprog.c
8769 This command is exactly the same as the previous example. Switches may
8770 appear anywhere in the command line, and single letter switches may be
8771 combined into a single switch.
8772 @end ifclear
8773
8774 @ifclear vms
8775 @item gnatbind -n math dbase -C -o ada-control.c
8776 @end ifclear
8777 @ifset vms
8778 @item gnatbind /NOMAIN math dbase /BIND_FILE=C /OUTPUT=ada-control.c
8779 @end ifset
8780 The main program is in a language other than Ada, but calls to
8781 subprograms in packages @code{Math} and @code{Dbase} appear. This call
8782 to @code{gnatbind} generates the file @file{ada-control.c} containing
8783 the @code{adainit} and @code{adafinal} routines to be called before and
8784 after accessing the Ada units.
8785 @end table
8786
8787 @c ------------------------------------
8788 @node Linking Using gnatlink
8789 @chapter Linking Using @command{gnatlink}
8790 @c ------------------------------------
8791 @findex gnatlink
8792
8793 @noindent
8794 This chapter discusses @command{gnatlink}, a tool that links
8795 an Ada program and builds an executable file. This utility
8796 invokes the system linker ^(via the @command{gcc} command)^^
8797 with a correct list of object files and library references.
8798 @command{gnatlink} automatically determines the list of files and
8799 references for the Ada part of a program. It uses the binder file
8800 generated by the @command{gnatbind} to determine this list.
8801
8802 Note: to invoke @code{gnatlink} with a project file, use the @code{gnat}
8803 driver (see @ref{The GNAT Driver and Project Files}).
8804
8805 @menu
8806 * Running gnatlink::
8807 * Switches for gnatlink::
8808 @end menu
8809
8810 @node Running gnatlink
8811 @section Running @command{gnatlink}
8812
8813 @noindent
8814 The form of the @command{gnatlink} command is
8815
8816 @smallexample
8817 $ gnatlink @ovar{switches} @var{mainprog}@r{[}.ali@r{]}
8818 @ovar{non-Ada objects} @ovar{linker options}
8819 @end smallexample
8820
8821 @noindent
8822 The arguments of @command{gnatlink} (switches, main @file{ALI} file,
8823 non-Ada objects
8824 or linker options) may be in any order, provided that no non-Ada object may
8825 be mistaken for a main @file{ALI} file.
8826 Any file name @file{F} without the @file{.ali}
8827 extension will be taken as the main @file{ALI} file if a file exists
8828 whose name is the concatenation of @file{F} and @file{.ali}.
8829
8830 @noindent
8831 @file{@var{mainprog}.ali} references the ALI file of the main program.
8832 The @file{.ali} extension of this file can be omitted. From this
8833 reference, @command{gnatlink} locates the corresponding binder file
8834 @file{b~@var{mainprog}.adb} and, using the information in this file along
8835 with the list of non-Ada objects and linker options, constructs a
8836 linker command file to create the executable.
8837
8838 The arguments other than the @command{gnatlink} switches and the main
8839 @file{ALI} file are passed to the linker uninterpreted.
8840 They typically include the names of
8841 object files for units written in other languages than Ada and any library
8842 references required to resolve references in any of these foreign language
8843 units, or in @code{Import} pragmas in any Ada units.
8844
8845 @var{linker options} is an optional list of linker specific
8846 switches.
8847 The default linker called by gnatlink is @command{gcc} which in
8848 turn calls the appropriate system linker.
8849 Standard options for the linker such as @option{-lmy_lib} or
8850 @option{-Ldir} can be added as is.
8851 For options that are not recognized by
8852 @command{gcc} as linker options, use the @command{gcc} switches
8853 @option{-Xlinker} or @option{-Wl,}.
8854 Refer to the GCC documentation for
8855 details. Here is an example showing how to generate a linker map:
8856
8857 @smallexample
8858 $ ^gnatlink my_prog -Wl,-Map,MAPFILE^GNAT LINK my_prog.ali /MAP^
8859 @end smallexample
8860
8861 Using @var{linker options} it is possible to set the program stack and
8862 heap size.
8863 @ifset unw
8864 See @ref{Setting Stack Size from gnatlink} and
8865 @ref{Setting Heap Size from gnatlink}.
8866 @end ifset
8867
8868 @command{gnatlink} determines the list of objects required by the Ada
8869 program and prepends them to the list of objects passed to the linker.
8870 @command{gnatlink} also gathers any arguments set by the use of
8871 @code{pragma Linker_Options} and adds them to the list of arguments
8872 presented to the linker.
8873
8874 @ifset vms
8875 @command{gnatlink} accepts the following types of extra files on the command
8876 line: objects (@file{.OBJ}), libraries (@file{.OLB}), sharable images
8877 (@file{.EXE}), and options files (@file{.OPT}). These are recognized and
8878 handled according to their extension.
8879 @end ifset
8880
8881 @node Switches for gnatlink
8882 @section Switches for @command{gnatlink}
8883
8884 @noindent
8885 The following switches are available with the @command{gnatlink} utility:
8886
8887 @table @option
8888 @c !sort!
8889
8890 @item --version
8891 @cindex @option{--version} @command{gnatlink}
8892 Display Copyright and version, then exit disregarding all other options.
8893
8894 @item --help
8895 @cindex @option{--help} @command{gnatlink}
8896 If @option{--version} was not used, display usage, then exit disregarding
8897 all other options.
8898
8899 @item ^-A^/BIND_FILE=ADA^
8900 @cindex @option{^-A^/BIND_FILE=ADA^} (@command{gnatlink})
8901 The binder has generated code in Ada. This is the default.
8902
8903 @item ^-C^/BIND_FILE=C^
8904 @cindex @option{^-C^/BIND_FILE=C^} (@command{gnatlink})
8905 If instead of generating a file in Ada, the binder has generated one in
8906 C, then the linker needs to know about it. Use this switch to signal
8907 to @command{gnatlink} that the binder has generated C code rather than
8908 Ada code.
8909
8910 @item ^-f^/FORCE_OBJECT_FILE_LIST^
8911 @cindex Command line length
8912 @cindex @option{^-f^/FORCE_OBJECT_FILE_LIST^} (@command{gnatlink})
8913 On some targets, the command line length is limited, and @command{gnatlink}
8914 will generate a separate file for the linker if the list of object files
8915 is too long.
8916 The @option{^-f^/FORCE_OBJECT_FILE_LIST^} switch forces this file
8917 to be generated even if
8918 the limit is not exceeded. This is useful in some cases to deal with
8919 special situations where the command line length is exceeded.
8920
8921 @item ^-g^/DEBUG^
8922 @cindex Debugging information, including
8923 @cindex @option{^-g^/DEBUG^} (@command{gnatlink})
8924 The option to include debugging information causes the Ada bind file (in
8925 other words, @file{b~@var{mainprog}.adb}) to be compiled with
8926 @option{^-g^/DEBUG^}.
8927 In addition, the binder does not delete the @file{b~@var{mainprog}.adb},
8928 @file{b~@var{mainprog}.o} and @file{b~@var{mainprog}.ali} files.
8929 Without @option{^-g^/DEBUG^}, the binder removes these files by
8930 default. The same procedure apply if a C bind file was generated using
8931 @option{^-C^/BIND_FILE=C^} @code{gnatbind} option, in this case the filenames
8932 are @file{b_@var{mainprog}.c} and @file{b_@var{mainprog}.o}.
8933
8934 @item ^-n^/NOCOMPILE^
8935 @cindex @option{^-n^/NOCOMPILE^} (@command{gnatlink})
8936 Do not compile the file generated by the binder. This may be used when
8937 a link is rerun with different options, but there is no need to recompile
8938 the binder file.
8939
8940 @item ^-v^/VERBOSE^
8941 @cindex @option{^-v^/VERBOSE^} (@command{gnatlink})
8942 Causes additional information to be output, including a full list of the
8943 included object files. This switch option is most useful when you want
8944 to see what set of object files are being used in the link step.
8945
8946 @item ^-v -v^/VERBOSE/VERBOSE^
8947 @cindex @option{^-v -v^/VERBOSE/VERBOSE^} (@command{gnatlink})
8948 Very verbose mode. Requests that the compiler operate in verbose mode when
8949 it compiles the binder file, and that the system linker run in verbose mode.
8950
8951 @item ^-o ^/EXECUTABLE=^@var{exec-name}
8952 @cindex @option{^-o^/EXECUTABLE^} (@command{gnatlink})
8953 @var{exec-name} specifies an alternate name for the generated
8954 executable program. If this switch is omitted, the executable has the same
8955 name as the main unit. For example, @code{gnatlink try.ali} creates
8956 an executable called @file{^try^TRY.EXE^}.
8957
8958 @ifclear vms
8959 @item -b @var{target}
8960 @cindex @option{-b} (@command{gnatlink})
8961 Compile your program to run on @var{target}, which is the name of a
8962 system configuration. You must have a GNAT cross-compiler built if
8963 @var{target} is not the same as your host system.
8964
8965 @item -B@var{dir}
8966 @cindex @option{-B} (@command{gnatlink})
8967 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
8968 from @var{dir} instead of the default location. Only use this switch
8969 when multiple versions of the GNAT compiler are available.
8970 @xref{Directory Options,,, gcc, The GNU Compiler Collection},
8971 for further details. You would normally use the @option{-b} or
8972 @option{-V} switch instead.
8973
8974 @item --GCC=@var{compiler_name}
8975 @cindex @option{--GCC=compiler_name} (@command{gnatlink})
8976 Program used for compiling the binder file. The default is
8977 @command{gcc}. You need to use quotes around @var{compiler_name} if
8978 @code{compiler_name} contains spaces or other separator characters.
8979 As an example @option{--GCC="foo -x -y"} will instruct @command{gnatlink} to
8980 use @code{foo -x -y} as your compiler. Note that switch @option{-c} is always
8981 inserted after your command name. Thus in the above example the compiler
8982 command that will be used by @command{gnatlink} will be @code{foo -c -x -y}.
8983 A limitation of this syntax is that the name and path name of the executable
8984 itself must not include any embedded spaces. If the compiler executable is
8985 different from the default one (gcc or <prefix>-gcc), then the back-end
8986 switches in the ALI file are not used to compile the binder generated source.
8987 For example, this is the case with @option{--GCC="foo -x -y"}. But the back end
8988 switches will be used for @option{--GCC="gcc -gnatv"}. If several
8989 @option{--GCC=compiler_name} are used, only the last @var{compiler_name}
8990 is taken into account. However, all the additional switches are also taken
8991 into account. Thus,
8992 @option{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
8993 @option{--GCC="bar -x -y -z -t"}.
8994
8995 @item --LINK=@var{name}
8996 @cindex @option{--LINK=} (@command{gnatlink})
8997 @var{name} is the name of the linker to be invoked. This is especially
8998 useful in mixed language programs since languages such as C++ require
8999 their own linker to be used. When this switch is omitted, the default
9000 name for the linker is @command{gcc}. When this switch is used, the
9001 specified linker is called instead of @command{gcc} with exactly the same
9002 parameters that would have been passed to @command{gcc} so if the desired
9003 linker requires different parameters it is necessary to use a wrapper
9004 script that massages the parameters before invoking the real linker. It
9005 may be useful to control the exact invocation by using the verbose
9006 switch.
9007
9008 @end ifclear
9009
9010 @ifset vms
9011 @item /DEBUG=TRACEBACK
9012 @cindex @code{/DEBUG=TRACEBACK} (@command{gnatlink})
9013 This qualifier causes sufficient information to be included in the
9014 executable file to allow a traceback, but does not include the full
9015 symbol information needed by the debugger.
9016
9017 @item /IDENTIFICATION="<string>"
9018 @code{"<string>"} specifies the string to be stored in the image file
9019 identification field in the image header.
9020 It overrides any pragma @code{Ident} specified string.
9021
9022 @item /NOINHIBIT-EXEC
9023 Generate the executable file even if there are linker warnings.
9024
9025 @item /NOSTART_FILES
9026 Don't link in the object file containing the ``main'' transfer address.
9027 Used when linking with a foreign language main program compiled with an
9028 HP compiler.
9029
9030 @item /STATIC
9031 Prefer linking with object libraries over sharable images, even without
9032 /DEBUG.
9033 @end ifset
9034
9035 @end table
9036
9037 @node The GNAT Make Program gnatmake
9038 @chapter The GNAT Make Program @command{gnatmake}
9039 @findex gnatmake
9040
9041 @menu
9042 * Running gnatmake::
9043 * Switches for gnatmake::
9044 * Mode Switches for gnatmake::
9045 * Notes on the Command Line::
9046 * How gnatmake Works::
9047 * Examples of gnatmake Usage::
9048 @end menu
9049 @noindent
9050 A typical development cycle when working on an Ada program consists of
9051 the following steps:
9052
9053 @enumerate
9054 @item
9055 Edit some sources to fix bugs.
9056
9057 @item
9058 Add enhancements.
9059
9060 @item
9061 Compile all sources affected.
9062
9063 @item
9064 Rebind and relink.
9065
9066 @item
9067 Test.
9068 @end enumerate
9069
9070 @noindent
9071 The third step can be tricky, because not only do the modified files
9072 @cindex Dependency rules
9073 have to be compiled, but any files depending on these files must also be
9074 recompiled. The dependency rules in Ada can be quite complex, especially
9075 in the presence of overloading, @code{use} clauses, generics and inlined
9076 subprograms.
9077
9078 @command{gnatmake} automatically takes care of the third and fourth steps
9079 of this process. It determines which sources need to be compiled,
9080 compiles them, and binds and links the resulting object files.
9081
9082 Unlike some other Ada make programs, the dependencies are always
9083 accurately recomputed from the new sources. The source based approach of
9084 the GNAT compilation model makes this possible. This means that if
9085 changes to the source program cause corresponding changes in
9086 dependencies, they will always be tracked exactly correctly by
9087 @command{gnatmake}.
9088
9089 @node Running gnatmake
9090 @section Running @command{gnatmake}
9091
9092 @noindent
9093 The usual form of the @command{gnatmake} command is
9094
9095 @smallexample
9096 $ gnatmake @ovar{switches} @var{file_name}
9097 @ovar{file_names} @ovar{mode_switches}
9098 @end smallexample
9099
9100 @noindent
9101 The only required argument is one @var{file_name}, which specifies
9102 a compilation unit that is a main program. Several @var{file_names} can be
9103 specified: this will result in several executables being built.
9104 If @code{switches} are present, they can be placed before the first
9105 @var{file_name}, between @var{file_names} or after the last @var{file_name}.
9106 If @var{mode_switches} are present, they must always be placed after
9107 the last @var{file_name} and all @code{switches}.
9108
9109 If you are using standard file extensions (@file{.adb} and @file{.ads}), then the
9110 extension may be omitted from the @var{file_name} arguments. However, if
9111 you are using non-standard extensions, then it is required that the
9112 extension be given. A relative or absolute directory path can be
9113 specified in a @var{file_name}, in which case, the input source file will
9114 be searched for in the specified directory only. Otherwise, the input
9115 source file will first be searched in the directory where
9116 @command{gnatmake} was invoked and if it is not found, it will be search on
9117 the source path of the compiler as described in
9118 @ref{Search Paths and the Run-Time Library (RTL)}.
9119
9120 All @command{gnatmake} output (except when you specify
9121 @option{^-M^/DEPENDENCIES_LIST^}) is to
9122 @file{stderr}. The output produced by the
9123 @option{^-M^/DEPENDENCIES_LIST^} switch is send to
9124 @file{stdout}.
9125
9126 @node Switches for gnatmake
9127 @section Switches for @command{gnatmake}
9128
9129 @noindent
9130 You may specify any of the following switches to @command{gnatmake}:
9131
9132 @table @option
9133 @c !sort!
9134
9135 @item --version
9136 @cindex @option{--version} @command{gnatmake}
9137 Display Copyright and version, then exit disregarding all other options.
9138
9139 @item --help
9140 @cindex @option{--help} @command{gnatmake}
9141 If @option{--version} was not used, display usage, then exit disregarding
9142 all other options.
9143
9144 @ifclear vms
9145 @item --GCC=@var{compiler_name}
9146 @cindex @option{--GCC=compiler_name} (@command{gnatmake})
9147 Program used for compiling. The default is `@command{gcc}'. You need to use
9148 quotes around @var{compiler_name} if @code{compiler_name} contains
9149 spaces or other separator characters. As an example @option{--GCC="foo -x
9150 -y"} will instruct @command{gnatmake} to use @code{foo -x -y} as your
9151 compiler. A limitation of this syntax is that the name and path name of
9152 the executable itself must not include any embedded spaces. Note that
9153 switch @option{-c} is always inserted after your command name. Thus in the
9154 above example the compiler command that will be used by @command{gnatmake}
9155 will be @code{foo -c -x -y}. If several @option{--GCC=compiler_name} are
9156 used, only the last @var{compiler_name} is taken into account. However,
9157 all the additional switches are also taken into account. Thus,
9158 @option{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
9159 @option{--GCC="bar -x -y -z -t"}.
9160
9161 @item --GNATBIND=@var{binder_name}
9162 @cindex @option{--GNATBIND=binder_name} (@command{gnatmake})
9163 Program used for binding. The default is `@code{gnatbind}'. You need to
9164 use quotes around @var{binder_name} if @var{binder_name} contains spaces
9165 or other separator characters. As an example @option{--GNATBIND="bar -x
9166 -y"} will instruct @command{gnatmake} to use @code{bar -x -y} as your
9167 binder. Binder switches that are normally appended by @command{gnatmake}
9168 to `@code{gnatbind}' are now appended to the end of @code{bar -x -y}.
9169 A limitation of this syntax is that the name and path name of the executable
9170 itself must not include any embedded spaces.
9171
9172 @item --GNATLINK=@var{linker_name}
9173 @cindex @option{--GNATLINK=linker_name} (@command{gnatmake})
9174 Program used for linking. The default is `@command{gnatlink}'. You need to
9175 use quotes around @var{linker_name} if @var{linker_name} contains spaces
9176 or other separator characters. As an example @option{--GNATLINK="lan -x
9177 -y"} will instruct @command{gnatmake} to use @code{lan -x -y} as your
9178 linker. Linker switches that are normally appended by @command{gnatmake} to
9179 `@command{gnatlink}' are now appended to the end of @code{lan -x -y}.
9180 A limitation of this syntax is that the name and path name of the executable
9181 itself must not include any embedded spaces.
9182
9183 @end ifclear
9184
9185 @item ^-a^/ALL_FILES^
9186 @cindex @option{^-a^/ALL_FILES^} (@command{gnatmake})
9187 Consider all files in the make process, even the GNAT internal system
9188 files (for example, the predefined Ada library files), as well as any
9189 locked files. Locked files are files whose ALI file is write-protected.
9190 By default,
9191 @command{gnatmake} does not check these files,
9192 because the assumption is that the GNAT internal files are properly up
9193 to date, and also that any write protected ALI files have been properly
9194 installed. Note that if there is an installation problem, such that one
9195 of these files is not up to date, it will be properly caught by the
9196 binder.
9197 You may have to specify this switch if you are working on GNAT
9198 itself. The switch @option{^-a^/ALL_FILES^} is also useful
9199 in conjunction with @option{^-f^/FORCE_COMPILE^}
9200 if you need to recompile an entire application,
9201 including run-time files, using special configuration pragmas,
9202 such as a @code{Normalize_Scalars} pragma.
9203
9204 By default
9205 @code{gnatmake ^-a^/ALL_FILES^} compiles all GNAT
9206 internal files with
9207 @ifclear vms
9208 @code{gcc -c -gnatpg} rather than @code{gcc -c}.
9209 @end ifclear
9210 @ifset vms
9211 the @code{/CHECKS=SUPPRESS_ALL /STYLE_CHECKS=GNAT} switch.
9212 @end ifset
9213
9214 @item ^-b^/ACTIONS=BIND^
9215 @cindex @option{^-b^/ACTIONS=BIND^} (@command{gnatmake})
9216 Bind only. Can be combined with @option{^-c^/ACTIONS=COMPILE^} to do
9217 compilation and binding, but no link.
9218 Can be combined with @option{^-l^/ACTIONS=LINK^}
9219 to do binding and linking. When not combined with
9220 @option{^-c^/ACTIONS=COMPILE^}
9221 all the units in the closure of the main program must have been previously
9222 compiled and must be up to date. The root unit specified by @var{file_name}
9223 may be given without extension, with the source extension or, if no GNAT
9224 Project File is specified, with the ALI file extension.
9225
9226 @item ^-c^/ACTIONS=COMPILE^
9227 @cindex @option{^-c^/ACTIONS=COMPILE^} (@command{gnatmake})
9228 Compile only. Do not perform binding, except when @option{^-b^/ACTIONS=BIND^}
9229 is also specified. Do not perform linking, except if both
9230 @option{^-b^/ACTIONS=BIND^} and
9231 @option{^-l^/ACTIONS=LINK^} are also specified.
9232 If the root unit specified by @var{file_name} is not a main unit, this is the
9233 default. Otherwise @command{gnatmake} will attempt binding and linking
9234 unless all objects are up to date and the executable is more recent than
9235 the objects.
9236
9237 @item ^-C^/MAPPING^
9238 @cindex @option{^-C^/MAPPING^} (@command{gnatmake})
9239 Use a temporary mapping file. A mapping file is a way to communicate to the
9240 compiler two mappings: from unit names to file names (without any directory
9241 information) and from file names to path names (with full directory
9242 information). These mappings are used by the compiler to short-circuit the path
9243 search. When @command{gnatmake} is invoked with this switch, it will create
9244 a temporary mapping file, initially populated by the project manager,
9245 if @option{^-P^/PROJECT_FILE^} is used, otherwise initially empty.
9246 Each invocation of the compiler will add the newly accessed sources to the
9247 mapping file. This will improve the source search during the next invocation
9248 of the compiler.
9249
9250 @item ^-C=^/USE_MAPPING_FILE=^@var{file}
9251 @cindex @option{^-C=^/USE_MAPPING^} (@command{gnatmake})
9252 Use a specific mapping file. The file, specified as a path name (absolute or
9253 relative) by this switch, should already exist, otherwise the switch is
9254 ineffective. The specified mapping file will be communicated to the compiler.
9255 This switch is not compatible with a project file
9256 (^-P^/PROJECT_FILE=^@var{file}) or with multiple compiling processes
9257 (^-j^/PROCESSES=^nnn, when nnn is greater than 1).
9258
9259 @item ^-d^/DISPLAY_PROGRESS^
9260 @cindex @option{^-d^/DISPLAY_PROGRESS^} (@command{gnatmake})
9261 Display progress for each source, up to date or not, as a single line
9262
9263 @smallexample
9264 completed x out of y (zz%)
9265 @end smallexample
9266
9267 If the file needs to be compiled this is displayed after the invocation of
9268 the compiler. These lines are displayed even in quiet output mode.
9269
9270 @item ^-D ^/DIRECTORY_OBJECTS=^@var{dir}
9271 @cindex @option{^-D^/DIRECTORY_OBJECTS^} (@command{gnatmake})
9272 Put all object files and ALI file in directory @var{dir}.
9273 If the @option{^-D^/DIRECTORY_OBJECTS^} switch is not used, all object files
9274 and ALI files go in the current working directory.
9275
9276 This switch cannot be used when using a project file.
9277
9278 @ifclear vms
9279 @item -eL
9280 @cindex @option{-eL} (@command{gnatmake})
9281 Follow all symbolic links when processing project files.
9282 @end ifclear
9283
9284 @item ^-eS^/STANDARD_OUTPUT_FOR_COMMANDS^
9285 @cindex @option{^-eS^/STANDARD_OUTPUT_FOR_COMMANDS^} (@command{gnatmake})
9286 Output the commands for the compiler, the binder and the linker
9287 on ^standard output^SYS$OUTPUT^,
9288 instead of ^standard error^SYS$ERROR^.
9289
9290 @item ^-f^/FORCE_COMPILE^
9291 @cindex @option{^-f^/FORCE_COMPILE^} (@command{gnatmake})
9292 Force recompilations. Recompile all sources, even though some object
9293 files may be up to date, but don't recompile predefined or GNAT internal
9294 files or locked files (files with a write-protected ALI file),
9295 unless the @option{^-a^/ALL_FILES^} switch is also specified.
9296
9297 @item ^-F^/FULL_PATH_IN_BRIEF_MESSAGES^
9298 @cindex @option{^-F^/FULL_PATH_IN_BRIEF_MESSAGES^} (@command{gnatmake})
9299 When using project files, if some errors or warnings are detected during
9300 parsing and verbose mode is not in effect (no use of switch
9301 ^-v^/VERBOSE^), then error lines start with the full path name of the project
9302 file, rather than its simple file name.
9303
9304 @item ^-g^/DEBUG^
9305 @cindex @option{^-g^/DEBUG^} (@command{gnatmake})
9306 Enable debugging. This switch is simply passed to the compiler and to the
9307 linker.
9308
9309 @item ^-i^/IN_PLACE^
9310 @cindex @option{^-i^/IN_PLACE^} (@command{gnatmake})
9311 In normal mode, @command{gnatmake} compiles all object files and ALI files
9312 into the current directory. If the @option{^-i^/IN_PLACE^} switch is used,
9313 then instead object files and ALI files that already exist are overwritten
9314 in place. This means that once a large project is organized into separate
9315 directories in the desired manner, then @command{gnatmake} will automatically
9316 maintain and update this organization. If no ALI files are found on the
9317 Ada object path (@ref{Search Paths and the Run-Time Library (RTL)}),
9318 the new object and ALI files are created in the
9319 directory containing the source being compiled. If another organization
9320 is desired, where objects and sources are kept in different directories,
9321 a useful technique is to create dummy ALI files in the desired directories.
9322 When detecting such a dummy file, @command{gnatmake} will be forced to
9323 recompile the corresponding source file, and it will be put the resulting
9324 object and ALI files in the directory where it found the dummy file.
9325
9326 @item ^-j^/PROCESSES=^@var{n}
9327 @cindex @option{^-j^/PROCESSES^} (@command{gnatmake})
9328 @cindex Parallel make
9329 Use @var{n} processes to carry out the (re)compilations. On a
9330 multiprocessor machine compilations will occur in parallel. In the
9331 event of compilation errors, messages from various compilations might
9332 get interspersed (but @command{gnatmake} will give you the full ordered
9333 list of failing compiles at the end). If this is problematic, rerun
9334 the make process with n set to 1 to get a clean list of messages.
9335
9336 @item ^-k^/CONTINUE_ON_ERROR^
9337 @cindex @option{^-k^/CONTINUE_ON_ERROR^} (@command{gnatmake})
9338 Keep going. Continue as much as possible after a compilation error. To
9339 ease the programmer's task in case of compilation errors, the list of
9340 sources for which the compile fails is given when @command{gnatmake}
9341 terminates.
9342
9343 If @command{gnatmake} is invoked with several @file{file_names} and with this
9344 switch, if there are compilation errors when building an executable,
9345 @command{gnatmake} will not attempt to build the following executables.
9346
9347 @item ^-l^/ACTIONS=LINK^
9348 @cindex @option{^-l^/ACTIONS=LINK^} (@command{gnatmake})
9349 Link only. Can be combined with @option{^-b^/ACTIONS=BIND^} to binding
9350 and linking. Linking will not be performed if combined with
9351 @option{^-c^/ACTIONS=COMPILE^}
9352 but not with @option{^-b^/ACTIONS=BIND^}.
9353 When not combined with @option{^-b^/ACTIONS=BIND^}
9354 all the units in the closure of the main program must have been previously
9355 compiled and must be up to date, and the main program needs to have been bound.
9356 The root unit specified by @var{file_name}
9357 may be given without extension, with the source extension or, if no GNAT
9358 Project File is specified, with the ALI file extension.
9359
9360 @item ^-m^/MINIMAL_RECOMPILATION^
9361 @cindex @option{^-m^/MINIMAL_RECOMPILATION^} (@command{gnatmake})
9362 Specify that the minimum necessary amount of recompilations
9363 be performed. In this mode @command{gnatmake} ignores time
9364 stamp differences when the only
9365 modifications to a source file consist in adding/removing comments,
9366 empty lines, spaces or tabs. This means that if you have changed the
9367 comments in a source file or have simply reformatted it, using this
9368 switch will tell @command{gnatmake} not to recompile files that depend on it
9369 (provided other sources on which these files depend have undergone no
9370 semantic modifications). Note that the debugging information may be
9371 out of date with respect to the sources if the @option{-m} switch causes
9372 a compilation to be switched, so the use of this switch represents a
9373 trade-off between compilation time and accurate debugging information.
9374
9375 @item ^-M^/DEPENDENCIES_LIST^
9376 @cindex Dependencies, producing list
9377 @cindex @option{^-M^/DEPENDENCIES_LIST^} (@command{gnatmake})
9378 Check if all objects are up to date. If they are, output the object
9379 dependences to @file{stdout} in a form that can be directly exploited in
9380 a @file{Makefile}. By default, each source file is prefixed with its
9381 (relative or absolute) directory name. This name is whatever you
9382 specified in the various @option{^-aI^/SOURCE_SEARCH^}
9383 and @option{^-I^/SEARCH^} switches. If you use
9384 @code{gnatmake ^-M^/DEPENDENCIES_LIST^}
9385 @option{^-q^/QUIET^}
9386 (see below), only the source file names,
9387 without relative paths, are output. If you just specify the
9388 @option{^-M^/DEPENDENCIES_LIST^}
9389 switch, dependencies of the GNAT internal system files are omitted. This
9390 is typically what you want. If you also specify
9391 the @option{^-a^/ALL_FILES^} switch,
9392 dependencies of the GNAT internal files are also listed. Note that
9393 dependencies of the objects in external Ada libraries (see switch
9394 @option{^-aL^/SKIP_MISSING=^}@var{dir} in the following list)
9395 are never reported.
9396
9397 @item ^-n^/DO_OBJECT_CHECK^
9398 @cindex @option{^-n^/DO_OBJECT_CHECK^} (@command{gnatmake})
9399 Don't compile, bind, or link. Checks if all objects are up to date.
9400 If they are not, the full name of the first file that needs to be
9401 recompiled is printed.
9402 Repeated use of this option, followed by compiling the indicated source
9403 file, will eventually result in recompiling all required units.
9404
9405 @item ^-o ^/EXECUTABLE=^@var{exec_name}
9406 @cindex @option{^-o^/EXECUTABLE^} (@command{gnatmake})
9407 Output executable name. The name of the final executable program will be
9408 @var{exec_name}. If the @option{^-o^/EXECUTABLE^} switch is omitted the default
9409 name for the executable will be the name of the input file in appropriate form
9410 for an executable file on the host system.
9411
9412 This switch cannot be used when invoking @command{gnatmake} with several
9413 @file{file_names}.
9414
9415 @item ^-p or --create-missing-dirs^/CREATE_MISSING_DIRS^
9416 @cindex @option{^-p^/CREATE_MISSING_DIRS^} (@command{gnatmake})
9417 When using project files (^-P^/PROJECT_FILE=^@var{project}), create
9418 automatically missing object directories, library directories and exec
9419 directories.
9420
9421 @item ^-P^/PROJECT_FILE=^@var{project}
9422 @cindex @option{^-P^/PROJECT_FILE^} (@command{gnatmake})
9423 Use project file @var{project}. Only one such switch can be used.
9424 @xref{gnatmake and Project Files}.
9425
9426 @item ^-q^/QUIET^
9427 @cindex @option{^-q^/QUIET^} (@command{gnatmake})
9428 Quiet. When this flag is not set, the commands carried out by
9429 @command{gnatmake} are displayed.
9430
9431 @item ^-s^/SWITCH_CHECK/^
9432 @cindex @option{^-s^/SWITCH_CHECK^} (@command{gnatmake})
9433 Recompile if compiler switches have changed since last compilation.
9434 All compiler switches but -I and -o are taken into account in the
9435 following way:
9436 orders between different ``first letter'' switches are ignored, but
9437 orders between same switches are taken into account. For example,
9438 @option{-O -O2} is different than @option{-O2 -O}, but @option{-g -O}
9439 is equivalent to @option{-O -g}.
9440
9441 This switch is recommended when Integrated Preprocessing is used.
9442
9443 @item ^-u^/UNIQUE^
9444 @cindex @option{^-u^/UNIQUE^} (@command{gnatmake})
9445 Unique. Recompile at most the main files. It implies -c. Combined with
9446 -f, it is equivalent to calling the compiler directly. Note that using
9447 ^-u^/UNIQUE^ with a project file and no main has a special meaning
9448 (@pxref{Project Files and Main Subprograms}).
9449
9450 @item ^-U^/ALL_PROJECTS^
9451 @cindex @option{^-U^/ALL_PROJECTS^} (@command{gnatmake})
9452 When used without a project file or with one or several mains on the command
9453 line, is equivalent to ^-u^/UNIQUE^. When used with a project file and no main
9454 on the command line, all sources of all project files are checked and compiled
9455 if not up to date, and libraries are rebuilt, if necessary.
9456
9457 @item ^-v^/REASONS^
9458 @cindex @option{^-v^/REASONS^} (@command{gnatmake})
9459 Verbose. Display the reason for all recompilations @command{gnatmake}
9460 decides are necessary, with the highest verbosity level.
9461
9462 @item ^-vl^/LOW_VERBOSITY^
9463 @cindex @option{^-vl^/LOW_VERBOSITY^} (@command{gnatmake})
9464 Verbosity level Low. Display fewer lines than in verbosity Medium.
9465
9466 @item ^-vm^/MEDIUM_VERBOSITY^
9467 @cindex @option{^-vm^/MEDIUM_VERBOSITY^} (@command{gnatmake})
9468 Verbosity level Medium. Potentially display fewer lines than in verbosity High.
9469
9470 @item ^-vh^/HIGH_VERBOSITY^
9471 @cindex @option{^-vm^/HIGH_VERBOSITY^} (@command{gnatmake})
9472 Verbosity level High. Equivalent to ^-v^/REASONS^.
9473
9474 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
9475 Indicate the verbosity of the parsing of GNAT project files.
9476 @xref{Switches Related to Project Files}.
9477
9478 @item ^-x^/NON_PROJECT_UNIT_COMPILATION^
9479 @cindex @option{^-x^/NON_PROJECT_UNIT_COMPILATION^} (@command{gnatmake})
9480 Indicate that sources that are not part of any Project File may be compiled.
9481 Normally, when using Project Files, only sources that are part of a Project
9482 File may be compile. When this switch is used, a source outside of all Project
9483 Files may be compiled. The ALI file and the object file will be put in the
9484 object directory of the main Project. The compilation switches used will only
9485 be those specified on the command line. Even when
9486 @option{^-x^/NON_PROJECT_UNIT_COMPILATION^} is used, mains specified on the
9487 command line need to be sources of a project file.
9488
9489 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
9490 Indicate that external variable @var{name} has the value @var{value}.
9491 The Project Manager will use this value for occurrences of
9492 @code{external(name)} when parsing the project file.
9493 @xref{Switches Related to Project Files}.
9494
9495 @item ^-z^/NOMAIN^
9496 @cindex @option{^-z^/NOMAIN^} (@command{gnatmake})
9497 No main subprogram. Bind and link the program even if the unit name
9498 given on the command line is a package name. The resulting executable
9499 will execute the elaboration routines of the package and its closure,
9500 then the finalization routines.
9501
9502 @end table
9503
9504 @table @asis
9505 @item @command{gcc} @asis{switches}
9506 @ifclear vms
9507 Any uppercase or multi-character switch that is not a @command{gnatmake} switch
9508 is passed to @command{gcc} (e.g.@: @option{-O}, @option{-gnato,} etc.)
9509 @end ifclear
9510 @ifset vms
9511 Any qualifier that cannot be recognized as a qualifier for @code{GNAT MAKE}
9512 but is recognizable as a valid qualifier for @code{GNAT COMPILE} is
9513 automatically treated as a compiler switch, and passed on to all
9514 compilations that are carried out.
9515 @end ifset
9516 @end table
9517
9518 @noindent
9519 Source and library search path switches:
9520
9521 @table @option
9522 @c !sort!
9523 @item ^-aI^/SOURCE_SEARCH=^@var{dir}
9524 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatmake})
9525 When looking for source files also look in directory @var{dir}.
9526 The order in which source files search is undertaken is
9527 described in @ref{Search Paths and the Run-Time Library (RTL)}.
9528
9529 @item ^-aL^/SKIP_MISSING=^@var{dir}
9530 @cindex @option{^-aL^/SKIP_MISSING^} (@command{gnatmake})
9531 Consider @var{dir} as being an externally provided Ada library.
9532 Instructs @command{gnatmake} to skip compilation units whose @file{.ALI}
9533 files have been located in directory @var{dir}. This allows you to have
9534 missing bodies for the units in @var{dir} and to ignore out of date bodies
9535 for the same units. You still need to specify
9536 the location of the specs for these units by using the switches
9537 @option{^-aI^/SOURCE_SEARCH=^@var{dir}}
9538 or @option{^-I^/SEARCH=^@var{dir}}.
9539 Note: this switch is provided for compatibility with previous versions
9540 of @command{gnatmake}. The easier method of causing standard libraries
9541 to be excluded from consideration is to write-protect the corresponding
9542 ALI files.
9543
9544 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
9545 @cindex @option{^-aO^/OBJECT_SEARCH^} (@command{gnatmake})
9546 When searching for library and object files, look in directory
9547 @var{dir}. The order in which library files are searched is described in
9548 @ref{Search Paths for gnatbind}.
9549
9550 @item ^-A^/CONDITIONAL_SOURCE_SEARCH=^@var{dir}
9551 @cindex Search paths, for @command{gnatmake}
9552 @cindex @option{^-A^/CONDITIONAL_SOURCE_SEARCH^} (@command{gnatmake})
9553 Equivalent to @option{^-aL^/SKIP_MISSING=^@var{dir}
9554 ^-aI^/SOURCE_SEARCH=^@var{dir}}.
9555
9556 @item ^-I^/SEARCH=^@var{dir}
9557 @cindex @option{^-I^/SEARCH^} (@command{gnatmake})
9558 Equivalent to @option{^-aO^/OBJECT_SEARCH=^@var{dir}
9559 ^-aI^/SOURCE_SEARCH=^@var{dir}}.
9560
9561 @item ^-I-^/NOCURRENT_DIRECTORY^
9562 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gnatmake})
9563 @cindex Source files, suppressing search
9564 Do not look for source files in the directory containing the source
9565 file named in the command line.
9566 Do not look for ALI or object files in the directory
9567 where @command{gnatmake} was invoked.
9568
9569 @item ^-L^/LIBRARY_SEARCH=^@var{dir}
9570 @cindex @option{^-L^/LIBRARY_SEARCH^} (@command{gnatmake})
9571 @cindex Linker libraries
9572 Add directory @var{dir} to the list of directories in which the linker
9573 will search for libraries. This is equivalent to
9574 @option{-largs ^-L^/LIBRARY_SEARCH=^}@var{dir}.
9575 @ifclear vms
9576 Furthermore, under Windows, the sources pointed to by the libraries path
9577 set in the registry are not searched for.
9578 @end ifclear
9579
9580 @item -nostdinc
9581 @cindex @option{-nostdinc} (@command{gnatmake})
9582 Do not look for source files in the system default directory.
9583
9584 @item -nostdlib
9585 @cindex @option{-nostdlib} (@command{gnatmake})
9586 Do not look for library files in the system default directory.
9587
9588 @item --RTS=@var{rts-path}
9589 @cindex @option{--RTS} (@command{gnatmake})
9590 Specifies the default location of the runtime library. GNAT looks for the
9591 runtime
9592 in the following directories, and stops as soon as a valid runtime is found
9593 (@file{adainclude} or @file{ada_source_path}, and @file{adalib} or
9594 @file{ada_object_path} present):
9595
9596 @itemize @bullet
9597 @item <current directory>/$rts_path
9598
9599 @item <default-search-dir>/$rts_path
9600
9601 @item <default-search-dir>/rts-$rts_path
9602 @end itemize
9603
9604 @noindent
9605 The selected path is handled like a normal RTS path.
9606
9607 @end table
9608
9609 @node Mode Switches for gnatmake
9610 @section Mode Switches for @command{gnatmake}
9611
9612 @noindent
9613 The mode switches (referred to as @code{mode_switches}) allow the
9614 inclusion of switches that are to be passed to the compiler itself, the
9615 binder or the linker. The effect of a mode switch is to cause all
9616 subsequent switches up to the end of the switch list, or up to the next
9617 mode switch, to be interpreted as switches to be passed on to the
9618 designated component of GNAT.
9619
9620 @table @option
9621 @c !sort!
9622 @item -cargs @var{switches}
9623 @cindex @option{-cargs} (@command{gnatmake})
9624 Compiler switches. Here @var{switches} is a list of switches
9625 that are valid switches for @command{gcc}. They will be passed on to
9626 all compile steps performed by @command{gnatmake}.
9627
9628 @item -bargs @var{switches}
9629 @cindex @option{-bargs} (@command{gnatmake})
9630 Binder switches. Here @var{switches} is a list of switches
9631 that are valid switches for @code{gnatbind}. They will be passed on to
9632 all bind steps performed by @command{gnatmake}.
9633
9634 @item -largs @var{switches}
9635 @cindex @option{-largs} (@command{gnatmake})
9636 Linker switches. Here @var{switches} is a list of switches
9637 that are valid switches for @command{gnatlink}. They will be passed on to
9638 all link steps performed by @command{gnatmake}.
9639
9640 @item -margs @var{switches}
9641 @cindex @option{-margs} (@command{gnatmake})
9642 Make switches. The switches are directly interpreted by @command{gnatmake},
9643 regardless of any previous occurrence of @option{-cargs}, @option{-bargs}
9644 or @option{-largs}.
9645 @end table
9646
9647 @node Notes on the Command Line
9648 @section Notes on the Command Line
9649
9650 @noindent
9651 This section contains some additional useful notes on the operation
9652 of the @command{gnatmake} command.
9653
9654 @itemize @bullet
9655 @item
9656 @cindex Recompilation, by @command{gnatmake}
9657 If @command{gnatmake} finds no ALI files, it recompiles the main program
9658 and all other units required by the main program.
9659 This means that @command{gnatmake}
9660 can be used for the initial compile, as well as during subsequent steps of
9661 the development cycle.
9662
9663 @item
9664 If you enter @code{gnatmake @var{file}.adb}, where @file{@var{file}.adb}
9665 is a subunit or body of a generic unit, @command{gnatmake} recompiles
9666 @file{@var{file}.adb} (because it finds no ALI) and stops, issuing a
9667 warning.
9668
9669 @item
9670 In @command{gnatmake} the switch @option{^-I^/SEARCH^}
9671 is used to specify both source and
9672 library file paths. Use @option{^-aI^/SOURCE_SEARCH^}
9673 instead if you just want to specify
9674 source paths only and @option{^-aO^/OBJECT_SEARCH^}
9675 if you want to specify library paths
9676 only.
9677
9678 @item
9679 @command{gnatmake} will ignore any files whose ALI file is write-protected.
9680 This may conveniently be used to exclude standard libraries from
9681 consideration and in particular it means that the use of the
9682 @option{^-f^/FORCE_COMPILE^} switch will not recompile these files
9683 unless @option{^-a^/ALL_FILES^} is also specified.
9684
9685 @item
9686 @command{gnatmake} has been designed to make the use of Ada libraries
9687 particularly convenient. Assume you have an Ada library organized
9688 as follows: @i{^obj-dir^[OBJ_DIR]^} contains the objects and ALI files for
9689 of your Ada compilation units,
9690 whereas @i{^include-dir^[INCLUDE_DIR]^} contains the
9691 specs of these units, but no bodies. Then to compile a unit
9692 stored in @code{main.adb}, which uses this Ada library you would just type
9693
9694 @smallexample
9695 @ifclear vms
9696 $ gnatmake -aI@var{include-dir} -aL@var{obj-dir} main
9697 @end ifclear
9698 @ifset vms
9699 $ gnatmake /SOURCE_SEARCH=@i{[INCLUDE_DIR]}
9700 /SKIP_MISSING=@i{[OBJ_DIR]} main
9701 @end ifset
9702 @end smallexample
9703
9704 @item
9705 Using @command{gnatmake} along with the
9706 @option{^-m (minimal recompilation)^/MINIMAL_RECOMPILATION^}
9707 switch provides a mechanism for avoiding unnecessary recompilations. Using
9708 this switch,
9709 you can update the comments/format of your
9710 source files without having to recompile everything. Note, however, that
9711 adding or deleting lines in a source files may render its debugging
9712 info obsolete. If the file in question is a spec, the impact is rather
9713 limited, as that debugging info will only be useful during the
9714 elaboration phase of your program. For bodies the impact can be more
9715 significant. In all events, your debugger will warn you if a source file
9716 is more recent than the corresponding object, and alert you to the fact
9717 that the debugging information may be out of date.
9718 @end itemize
9719
9720 @node How gnatmake Works
9721 @section How @command{gnatmake} Works
9722
9723 @noindent
9724 Generally @command{gnatmake} automatically performs all necessary
9725 recompilations and you don't need to worry about how it works. However,
9726 it may be useful to have some basic understanding of the @command{gnatmake}
9727 approach and in particular to understand how it uses the results of
9728 previous compilations without incorrectly depending on them.
9729
9730 First a definition: an object file is considered @dfn{up to date} if the
9731 corresponding ALI file exists and if all the source files listed in the
9732 dependency section of this ALI file have time stamps matching those in
9733 the ALI file. This means that neither the source file itself nor any
9734 files that it depends on have been modified, and hence there is no need
9735 to recompile this file.
9736
9737 @command{gnatmake} works by first checking if the specified main unit is up
9738 to date. If so, no compilations are required for the main unit. If not,
9739 @command{gnatmake} compiles the main program to build a new ALI file that
9740 reflects the latest sources. Then the ALI file of the main unit is
9741 examined to find all the source files on which the main program depends,
9742 and @command{gnatmake} recursively applies the above procedure on all these
9743 files.
9744
9745 This process ensures that @command{gnatmake} only trusts the dependencies
9746 in an existing ALI file if they are known to be correct. Otherwise it
9747 always recompiles to determine a new, guaranteed accurate set of
9748 dependencies. As a result the program is compiled ``upside down'' from what may
9749 be more familiar as the required order of compilation in some other Ada
9750 systems. In particular, clients are compiled before the units on which
9751 they depend. The ability of GNAT to compile in any order is critical in
9752 allowing an order of compilation to be chosen that guarantees that
9753 @command{gnatmake} will recompute a correct set of new dependencies if
9754 necessary.
9755
9756 When invoking @command{gnatmake} with several @var{file_names}, if a unit is
9757 imported by several of the executables, it will be recompiled at most once.
9758
9759 Note: when using non-standard naming conventions
9760 (@pxref{Using Other File Names}), changing through a configuration pragmas
9761 file the version of a source and invoking @command{gnatmake} to recompile may
9762 have no effect, if the previous version of the source is still accessible
9763 by @command{gnatmake}. It may be necessary to use the switch
9764 ^-f^/FORCE_COMPILE^.
9765
9766 @node Examples of gnatmake Usage
9767 @section Examples of @command{gnatmake} Usage
9768
9769 @table @code
9770 @item gnatmake hello.adb
9771 Compile all files necessary to bind and link the main program
9772 @file{hello.adb} (containing unit @code{Hello}) and bind and link the
9773 resulting object files to generate an executable file @file{^hello^HELLO.EXE^}.
9774
9775 @item gnatmake main1 main2 main3
9776 Compile all files necessary to bind and link the main programs
9777 @file{main1.adb} (containing unit @code{Main1}), @file{main2.adb}
9778 (containing unit @code{Main2}) and @file{main3.adb}
9779 (containing unit @code{Main3}) and bind and link the resulting object files
9780 to generate three executable files @file{^main1^MAIN1.EXE^},
9781 @file{^main2^MAIN2.EXE^}
9782 and @file{^main3^MAIN3.EXE^}.
9783
9784 @ifclear vms
9785 @item gnatmake -q Main_Unit -cargs -O2 -bargs -l
9786 @end ifclear
9787
9788 @ifset vms
9789 @item gnatmake Main_Unit /QUIET
9790 /COMPILER_QUALIFIERS /OPTIMIZE=ALL
9791 /BINDER_QUALIFIERS /ORDER_OF_ELABORATION
9792 @end ifset
9793 Compile all files necessary to bind and link the main program unit
9794 @code{Main_Unit} (from file @file{main_unit.adb}). All compilations will
9795 be done with optimization level 2 and the order of elaboration will be
9796 listed by the binder. @command{gnatmake} will operate in quiet mode, not
9797 displaying commands it is executing.
9798 @end table
9799
9800 @c *************************
9801 @node Improving Performance
9802 @chapter Improving Performance
9803 @cindex Improving performance
9804
9805 @noindent
9806 This chapter presents several topics related to program performance.
9807 It first describes some of the tradeoffs that need to be considered
9808 and some of the techniques for making your program run faster.
9809 It then documents the @command{gnatelim} tool and unused subprogram/data
9810 elimination feature, which can reduce the size of program executables.
9811
9812 Note: to invoke @command{gnatelim} with a project file, use the @code{gnat}
9813 driver (see @ref{The GNAT Driver and Project Files}).
9814
9815 @ifnottex
9816 @menu
9817 * Performance Considerations::
9818 * Text_IO Suggestions::
9819 * Reducing Size of Ada Executables with gnatelim::
9820 * Reducing Size of Executables with unused subprogram/data elimination::
9821 @end menu
9822 @end ifnottex
9823
9824 @c *****************************
9825 @node Performance Considerations
9826 @section Performance Considerations
9827
9828 @noindent
9829 The GNAT system provides a number of options that allow a trade-off
9830 between
9831
9832 @itemize @bullet
9833 @item
9834 performance of the generated code
9835
9836 @item
9837 speed of compilation
9838
9839 @item
9840 minimization of dependences and recompilation
9841
9842 @item
9843 the degree of run-time checking.
9844 @end itemize
9845
9846 @noindent
9847 The defaults (if no options are selected) aim at improving the speed
9848 of compilation and minimizing dependences, at the expense of performance
9849 of the generated code:
9850
9851 @itemize @bullet
9852 @item
9853 no optimization
9854
9855 @item
9856 no inlining of subprogram calls
9857
9858 @item
9859 all run-time checks enabled except overflow and elaboration checks
9860 @end itemize
9861
9862 @noindent
9863 These options are suitable for most program development purposes. This
9864 chapter describes how you can modify these choices, and also provides
9865 some guidelines on debugging optimized code.
9866
9867 @menu
9868 * Controlling Run-Time Checks::
9869 * Use of Restrictions::
9870 * Optimization Levels::
9871 * Debugging Optimized Code::
9872 * Inlining of Subprograms::
9873 * Other Optimization Switches::
9874 * Optimization and Strict Aliasing::
9875
9876 @ifset vms
9877 * Coverage Analysis::
9878 @end ifset
9879 @end menu
9880
9881 @node Controlling Run-Time Checks
9882 @subsection Controlling Run-Time Checks
9883
9884 @noindent
9885 By default, GNAT generates all run-time checks, except integer overflow
9886 checks, stack overflow checks, and checks for access before elaboration on
9887 subprogram calls. The latter are not required in default mode, because all
9888 necessary checking is done at compile time.
9889 @cindex @option{-gnatp} (@command{gcc})
9890 @cindex @option{-gnato} (@command{gcc})
9891 Two gnat switches, @option{-gnatp} and @option{-gnato} allow this default to
9892 be modified. @xref{Run-Time Checks}.
9893
9894 Our experience is that the default is suitable for most development
9895 purposes.
9896
9897 We treat integer overflow specially because these
9898 are quite expensive and in our experience are not as important as other
9899 run-time checks in the development process. Note that division by zero
9900 is not considered an overflow check, and divide by zero checks are
9901 generated where required by default.
9902
9903 Elaboration checks are off by default, and also not needed by default, since
9904 GNAT uses a static elaboration analysis approach that avoids the need for
9905 run-time checking. This manual contains a full chapter discussing the issue
9906 of elaboration checks, and if the default is not satisfactory for your use,
9907 you should read this chapter.
9908
9909 For validity checks, the minimal checks required by the Ada Reference
9910 Manual (for case statements and assignments to array elements) are on
9911 by default. These can be suppressed by use of the @option{-gnatVn} switch.
9912 Note that in Ada 83, there were no validity checks, so if the Ada 83 mode
9913 is acceptable (or when comparing GNAT performance with an Ada 83 compiler),
9914 it may be reasonable to routinely use @option{-gnatVn}. Validity checks
9915 are also suppressed entirely if @option{-gnatp} is used.
9916
9917 @cindex Overflow checks
9918 @cindex Checks, overflow
9919 @findex Suppress
9920 @findex Unsuppress
9921 @cindex pragma Suppress
9922 @cindex pragma Unsuppress
9923 Note that the setting of the switches controls the default setting of
9924 the checks. They may be modified using either @code{pragma Suppress} (to
9925 remove checks) or @code{pragma Unsuppress} (to add back suppressed
9926 checks) in the program source.
9927
9928 @node Use of Restrictions
9929 @subsection Use of Restrictions
9930
9931 @noindent
9932 The use of pragma Restrictions allows you to control which features are
9933 permitted in your program. Apart from the obvious point that if you avoid
9934 relatively expensive features like finalization (enforceable by the use
9935 of pragma Restrictions (No_Finalization), the use of this pragma does not
9936 affect the generated code in most cases.
9937
9938 One notable exception to this rule is that the possibility of task abort
9939 results in some distributed overhead, particularly if finalization or
9940 exception handlers are used. The reason is that certain sections of code
9941 have to be marked as non-abortable.
9942
9943 If you use neither the @code{abort} statement, nor asynchronous transfer
9944 of control (@code{select @dots{} then abort}), then this distributed overhead
9945 is removed, which may have a general positive effect in improving
9946 overall performance. Especially code involving frequent use of tasking
9947 constructs and controlled types will show much improved performance.
9948 The relevant restrictions pragmas are
9949
9950 @smallexample @c ada
9951 pragma Restrictions (No_Abort_Statements);
9952 pragma Restrictions (Max_Asynchronous_Select_Nesting => 0);
9953 @end smallexample
9954
9955 @noindent
9956 It is recommended that these restriction pragmas be used if possible. Note
9957 that this also means that you can write code without worrying about the
9958 possibility of an immediate abort at any point.
9959
9960 @node Optimization Levels
9961 @subsection Optimization Levels
9962 @cindex @option{^-O^/OPTIMIZE^} (@command{gcc})
9963
9964 @noindent
9965 Without any optimization ^option,^qualifier,^
9966 the compiler's goal is to reduce the cost of
9967 compilation and to make debugging produce the expected results.
9968 Statements are independent: if you stop the program with a breakpoint between
9969 statements, you can then assign a new value to any variable or change
9970 the program counter to any other statement in the subprogram and get exactly
9971 the results you would expect from the source code.
9972
9973 Turning on optimization makes the compiler attempt to improve the
9974 performance and/or code size at the expense of compilation time and
9975 possibly the ability to debug the program.
9976
9977 If you use multiple
9978 ^-O options, with or without level numbers,^/OPTIMIZE qualifiers,^
9979 the last such option is the one that is effective.
9980
9981 @noindent
9982 The default is optimization off. This results in the fastest compile
9983 times, but GNAT makes absolutely no attempt to optimize, and the
9984 generated programs are considerably larger and slower than when
9985 optimization is enabled. You can use the
9986 @ifclear vms
9987 @option{-O} switch (the permitted forms are @option{-O0}, @option{-O1}
9988 @option{-O2}, @option{-O3}, and @option{-Os})
9989 @end ifclear
9990 @ifset vms
9991 @code{OPTIMIZE} qualifier
9992 @end ifset
9993 to @command{gcc} to control the optimization level:
9994
9995 @table @option
9996 @item ^-O0^/OPTIMIZE=NONE^
9997 No optimization (the default);
9998 generates unoptimized code but has
9999 the fastest compilation time.
10000
10001 Note that many other compilers do fairly extensive optimization
10002 even if ``no optimization'' is specified. With gcc, it is
10003 very unusual to use ^-O0^/OPTIMIZE=NONE^ for production if
10004 execution time is of any concern, since ^-O0^/OPTIMIZE=NONE^
10005 really does mean no optimization at all. This difference between
10006 gcc and other compilers should be kept in mind when doing
10007 performance comparisons.
10008
10009 @item ^-O1^/OPTIMIZE=SOME^
10010 Moderate optimization;
10011 optimizes reasonably well but does not
10012 degrade compilation time significantly.
10013
10014 @item ^-O2^/OPTIMIZE=ALL^
10015 @ifset vms
10016 @itemx /OPTIMIZE=DEVELOPMENT
10017 @end ifset
10018 Full optimization;
10019 generates highly optimized code and has
10020 the slowest compilation time.
10021
10022 @item ^-O3^/OPTIMIZE=INLINING^
10023 Full optimization as in @option{-O2},
10024 and also attempts automatic inlining of small
10025 subprograms within a unit (@pxref{Inlining of Subprograms}).
10026
10027 @item ^-Os^/OPTIMIZE=SPACE^
10028 Optimize space usage of resulting program.
10029 @end table
10030
10031 @noindent
10032 Higher optimization levels perform more global transformations on the
10033 program and apply more expensive analysis algorithms in order to generate
10034 faster and more compact code. The price in compilation time, and the
10035 resulting improvement in execution time,
10036 both depend on the particular application and the hardware environment.
10037 You should experiment to find the best level for your application.
10038
10039 Since the precise set of optimizations done at each level will vary from
10040 release to release (and sometime from target to target), it is best to think
10041 of the optimization settings in general terms.
10042 @xref{Optimize Options,, Options That Control Optimization, gcc, Using
10043 the GNU Compiler Collection (GCC)}, for details about
10044 ^the @option{-O} settings and a number of @option{-f} options that^how to^
10045 individually enable or disable specific optimizations.
10046
10047 Unlike some other compilation systems, ^@command{gcc}^GNAT^ has
10048 been tested extensively at all optimization levels. There are some bugs
10049 which appear only with optimization turned on, but there have also been
10050 bugs which show up only in @emph{unoptimized} code. Selecting a lower
10051 level of optimization does not improve the reliability of the code
10052 generator, which in practice is highly reliable at all optimization
10053 levels.
10054
10055 Note regarding the use of @option{-O3}: The use of this optimization level
10056 is generally discouraged with GNAT, since it often results in larger
10057 executables which run more slowly. See further discussion of this point
10058 in @ref{Inlining of Subprograms}.
10059
10060 @node Debugging Optimized Code
10061 @subsection Debugging Optimized Code
10062 @cindex Debugging optimized code
10063 @cindex Optimization and debugging
10064
10065 @noindent
10066 Although it is possible to do a reasonable amount of debugging at
10067 @ifclear vms
10068 nonzero optimization levels,
10069 the higher the level the more likely that
10070 @end ifclear
10071 @ifset vms
10072 @option{/OPTIMIZE} settings other than @code{NONE},
10073 such settings will make it more likely that
10074 @end ifset
10075 source-level constructs will have been eliminated by optimization.
10076 For example, if a loop is strength-reduced, the loop
10077 control variable may be completely eliminated and thus cannot be
10078 displayed in the debugger.
10079 This can only happen at @option{-O2} or @option{-O3}.
10080 Explicit temporary variables that you code might be eliminated at
10081 ^level^setting^ @option{-O1} or higher.
10082
10083 The use of the @option{^-g^/DEBUG^} switch,
10084 @cindex @option{^-g^/DEBUG^} (@command{gcc})
10085 which is needed for source-level debugging,
10086 affects the size of the program executable on disk,
10087 and indeed the debugging information can be quite large.
10088 However, it has no effect on the generated code (and thus does not
10089 degrade performance)
10090
10091 Since the compiler generates debugging tables for a compilation unit before
10092 it performs optimizations, the optimizing transformations may invalidate some
10093 of the debugging data. You therefore need to anticipate certain
10094 anomalous situations that may arise while debugging optimized code.
10095 These are the most common cases:
10096
10097 @enumerate
10098 @item
10099 @i{The ``hopping Program Counter'':} Repeated @code{step} or @code{next}
10100 commands show
10101 the PC bouncing back and forth in the code. This may result from any of
10102 the following optimizations:
10103
10104 @itemize @bullet
10105 @item
10106 @i{Common subexpression elimination:} using a single instance of code for a
10107 quantity that the source computes several times. As a result you
10108 may not be able to stop on what looks like a statement.
10109
10110 @item
10111 @i{Invariant code motion:} moving an expression that does not change within a
10112 loop, to the beginning of the loop.
10113
10114 @item
10115 @i{Instruction scheduling:} moving instructions so as to
10116 overlap loads and stores (typically) with other code, or in
10117 general to move computations of values closer to their uses. Often
10118 this causes you to pass an assignment statement without the assignment
10119 happening and then later bounce back to the statement when the
10120 value is actually needed. Placing a breakpoint on a line of code
10121 and then stepping over it may, therefore, not always cause all the
10122 expected side-effects.
10123 @end itemize
10124
10125 @item
10126 @i{The ``big leap'':} More commonly known as @emph{cross-jumping}, in which
10127 two identical pieces of code are merged and the program counter suddenly
10128 jumps to a statement that is not supposed to be executed, simply because
10129 it (and the code following) translates to the same thing as the code
10130 that @emph{was} supposed to be executed. This effect is typically seen in
10131 sequences that end in a jump, such as a @code{goto}, a @code{return}, or
10132 a @code{break} in a C @code{^switch^switch^} statement.
10133
10134 @item
10135 @i{The ``roving variable'':} The symptom is an unexpected value in a variable.
10136 There are various reasons for this effect:
10137
10138 @itemize @bullet
10139 @item
10140 In a subprogram prologue, a parameter may not yet have been moved to its
10141 ``home''.
10142
10143 @item
10144 A variable may be dead, and its register re-used. This is
10145 probably the most common cause.
10146
10147 @item
10148 As mentioned above, the assignment of a value to a variable may
10149 have been moved.
10150
10151 @item
10152 A variable may be eliminated entirely by value propagation or
10153 other means. In this case, GCC may incorrectly generate debugging
10154 information for the variable
10155 @end itemize
10156
10157 @noindent
10158 In general, when an unexpected value appears for a local variable or parameter
10159 you should first ascertain if that value was actually computed by
10160 your program, as opposed to being incorrectly reported by the debugger.
10161 Record fields or
10162 array elements in an object designated by an access value
10163 are generally less of a problem, once you have ascertained that the access
10164 value is sensible.
10165 Typically, this means checking variables in the preceding code and in the
10166 calling subprogram to verify that the value observed is explainable from other
10167 values (one must apply the procedure recursively to those
10168 other values); or re-running the code and stopping a little earlier
10169 (perhaps before the call) and stepping to better see how the variable obtained
10170 the value in question; or continuing to step @emph{from} the point of the
10171 strange value to see if code motion had simply moved the variable's
10172 assignments later.
10173 @end enumerate
10174
10175 @noindent
10176 In light of such anomalies, a recommended technique is to use @option{-O0}
10177 early in the software development cycle, when extensive debugging capabilities
10178 are most needed, and then move to @option{-O1} and later @option{-O2} as
10179 the debugger becomes less critical.
10180 Whether to use the @option{^-g^/DEBUG^} switch in the release version is
10181 a release management issue.
10182 @ifclear vms
10183 Note that if you use @option{-g} you can then use the @command{strip} program
10184 on the resulting executable,
10185 which removes both debugging information and global symbols.
10186 @end ifclear
10187
10188 @node Inlining of Subprograms
10189 @subsection Inlining of Subprograms
10190
10191 @noindent
10192 A call to a subprogram in the current unit is inlined if all the
10193 following conditions are met:
10194
10195 @itemize @bullet
10196 @item
10197 The optimization level is at least @option{-O1}.
10198
10199 @item
10200 The called subprogram is suitable for inlining: It must be small enough
10201 and not contain something that @command{gcc} cannot support in inlined
10202 subprograms.
10203
10204 @item
10205 @cindex pragma Inline
10206 @findex Inline
10207 Either @code{pragma Inline} applies to the subprogram, or it is local
10208 to the unit and called once from within it, or it is small and automatic
10209 inlining (optimization level @option{-O3}) is specified.
10210 @end itemize
10211
10212 @noindent
10213 Calls to subprograms in @code{with}'ed units are normally not inlined.
10214 To achieve actual inlining (that is, replacement of the call by the code
10215 in the body of the subprogram), the following conditions must all be true.
10216
10217 @itemize @bullet
10218 @item
10219 The optimization level is at least @option{-O1}.
10220
10221 @item
10222 The called subprogram is suitable for inlining: It must be small enough
10223 and not contain something that @command{gcc} cannot support in inlined
10224 subprograms.
10225
10226 @item
10227 The call appears in a body (not in a package spec).
10228
10229 @item
10230 There is a @code{pragma Inline} for the subprogram.
10231
10232 @item
10233 @cindex @option{-gnatn} (@command{gcc})
10234 The @option{^-gnatn^/INLINE^} switch
10235 is used in the @command{gcc} command line
10236 @end itemize
10237
10238 Even if all these conditions are met, it may not be possible for
10239 the compiler to inline the call, due to the length of the body,
10240 or features in the body that make it impossible for the compiler
10241 to do the inlining.
10242
10243 Note that specifying the @option{-gnatn} switch causes additional
10244 compilation dependencies. Consider the following:
10245
10246 @smallexample @c ada
10247 @cartouche
10248 package R is
10249 procedure Q;
10250 pragma Inline (Q);
10251 end R;
10252 package body R is
10253 @dots{}
10254 end R;
10255
10256 with R;
10257 procedure Main is
10258 begin
10259 @dots{}
10260 R.Q;
10261 end Main;
10262 @end cartouche
10263 @end smallexample
10264
10265 @noindent
10266 With the default behavior (no @option{-gnatn} switch specified), the
10267 compilation of the @code{Main} procedure depends only on its own source,
10268 @file{main.adb}, and the spec of the package in file @file{r.ads}. This
10269 means that editing the body of @code{R} does not require recompiling
10270 @code{Main}.
10271
10272 On the other hand, the call @code{R.Q} is not inlined under these
10273 circumstances. If the @option{-gnatn} switch is present when @code{Main}
10274 is compiled, the call will be inlined if the body of @code{Q} is small
10275 enough, but now @code{Main} depends on the body of @code{R} in
10276 @file{r.adb} as well as on the spec. This means that if this body is edited,
10277 the main program must be recompiled. Note that this extra dependency
10278 occurs whether or not the call is in fact inlined by @command{gcc}.
10279
10280 The use of front end inlining with @option{-gnatN} generates similar
10281 additional dependencies.
10282
10283 @cindex @option{^-fno-inline^/INLINE=SUPPRESS^} (@command{gcc})
10284 Note: The @option{^-fno-inline^/INLINE=SUPPRESS^} switch
10285 can be used to prevent
10286 all inlining. This switch overrides all other conditions and ensures
10287 that no inlining occurs. The extra dependences resulting from
10288 @option{-gnatn} will still be active, even if
10289 this switch is used to suppress the resulting inlining actions.
10290
10291 @cindex @option{-fno-inline-functions} (@command{gcc})
10292 Note: The @option{-fno-inline-functions} switch can be used to prevent
10293 automatic inlining of small subprograms if @option{-O3} is used.
10294
10295 @cindex @option{-fno-inline-functions-called-once} (@command{gcc})
10296 Note: The @option{-fno-inline-functions-called-once} switch
10297 can be used to prevent inlining of subprograms local to the unit
10298 and called once from within it if @option{-O1} is used.
10299
10300 Note regarding the use of @option{-O3}: There is no difference in inlining
10301 behavior between @option{-O2} and @option{-O3} for subprograms with an explicit
10302 pragma @code{Inline} assuming the use of @option{-gnatn}
10303 or @option{-gnatN} (the switches that activate inlining). If you have used
10304 pragma @code{Inline} in appropriate cases, then it is usually much better
10305 to use @option{-O2} and @option{-gnatn} and avoid the use of @option{-O3} which
10306 in this case only has the effect of inlining subprograms you did not
10307 think should be inlined. We often find that the use of @option{-O3} slows
10308 down code by performing excessive inlining, leading to increased instruction
10309 cache pressure from the increased code size. So the bottom line here is
10310 that you should not automatically assume that @option{-O3} is better than
10311 @option{-O2}, and indeed you should use @option{-O3} only if tests show that
10312 it actually improves performance.
10313
10314 @node Other Optimization Switches
10315 @subsection Other Optimization Switches
10316 @cindex Optimization Switches
10317
10318 Since @code{GNAT} uses the @command{gcc} back end, all the specialized
10319 @command{gcc} optimization switches are potentially usable. These switches
10320 have not been extensively tested with GNAT but can generally be expected
10321 to work. Examples of switches in this category are
10322 @option{-funroll-loops} and
10323 the various target-specific @option{-m} options (in particular, it has been
10324 observed that @option{-march=pentium4} can significantly improve performance
10325 on appropriate machines). For full details of these switches, see
10326 @ref{Submodel Options,, Hardware Models and Configurations, gcc, Using
10327 the GNU Compiler Collection (GCC)}.
10328
10329 @node Optimization and Strict Aliasing
10330 @subsection Optimization and Strict Aliasing
10331 @cindex Aliasing
10332 @cindex Strict Aliasing
10333 @cindex No_Strict_Aliasing
10334
10335 @noindent
10336 The strong typing capabilities of Ada allow an optimizer to generate
10337 efficient code in situations where other languages would be forced to
10338 make worst case assumptions preventing such optimizations. Consider
10339 the following example:
10340
10341 @smallexample @c ada
10342 @cartouche
10343 procedure R is
10344 type Int1 is new Integer;
10345 type Int2 is new Integer;
10346 type Int1A is access Int1;
10347 type Int2A is access Int2;
10348 Int1V : Int1A;
10349 Int2V : Int2A;
10350 @dots{}
10351
10352 begin
10353 @dots{}
10354 for J in Data'Range loop
10355 if Data (J) = Int1V.all then
10356 Int2V.all := Int2V.all + 1;
10357 end if;
10358 end loop;
10359 @dots{}
10360 end R;
10361 @end cartouche
10362 @end smallexample
10363
10364 @noindent
10365 In this example, since the variable @code{Int1V} can only access objects
10366 of type @code{Int1}, and @code{Int2V} can only access objects of type
10367 @code{Int2}, there is no possibility that the assignment to
10368 @code{Int2V.all} affects the value of @code{Int1V.all}. This means that
10369 the compiler optimizer can "know" that the value @code{Int1V.all} is constant
10370 for all iterations of the loop and avoid the extra memory reference
10371 required to dereference it each time through the loop.
10372
10373 This kind of optimization, called strict aliasing analysis, is
10374 triggered by specifying an optimization level of @option{-O2} or
10375 higher or @option{-Os} and allows @code{GNAT} to generate more efficient code
10376 when access values are involved.
10377
10378 However, although this optimization is always correct in terms of
10379 the formal semantics of the Ada Reference Manual, difficulties can
10380 arise if features like @code{Unchecked_Conversion} are used to break
10381 the typing system. Consider the following complete program example:
10382
10383 @smallexample @c ada
10384 @cartouche
10385 package p1 is
10386 type int1 is new integer;
10387 type int2 is new integer;
10388 type a1 is access int1;
10389 type a2 is access int2;
10390 end p1;
10391
10392 with p1; use p1;
10393 package p2 is
10394 function to_a2 (Input : a1) return a2;
10395 end p2;
10396
10397 with Unchecked_Conversion;
10398 package body p2 is
10399 function to_a2 (Input : a1) return a2 is
10400 function to_a2u is
10401 new Unchecked_Conversion (a1, a2);
10402 begin
10403 return to_a2u (Input);
10404 end to_a2;
10405 end p2;
10406
10407 with p2; use p2;
10408 with p1; use p1;
10409 with Text_IO; use Text_IO;
10410 procedure m is
10411 v1 : a1 := new int1;
10412 v2 : a2 := to_a2 (v1);
10413 begin
10414 v1.all := 1;
10415 v2.all := 0;
10416 put_line (int1'image (v1.all));
10417 end;
10418 @end cartouche
10419 @end smallexample
10420
10421 @noindent
10422 This program prints out 0 in @option{-O0} or @option{-O1}
10423 mode, but it prints out 1 in @option{-O2} mode. That's
10424 because in strict aliasing mode, the compiler can and
10425 does assume that the assignment to @code{v2.all} could not
10426 affect the value of @code{v1.all}, since different types
10427 are involved.
10428
10429 This behavior is not a case of non-conformance with the standard, since
10430 the Ada RM specifies that an unchecked conversion where the resulting
10431 bit pattern is not a correct value of the target type can result in an
10432 abnormal value and attempting to reference an abnormal value makes the
10433 execution of a program erroneous. That's the case here since the result
10434 does not point to an object of type @code{int2}. This means that the
10435 effect is entirely unpredictable.
10436
10437 However, although that explanation may satisfy a language
10438 lawyer, in practice an applications programmer expects an
10439 unchecked conversion involving pointers to create true
10440 aliases and the behavior of printing 1 seems plain wrong.
10441 In this case, the strict aliasing optimization is unwelcome.
10442
10443 Indeed the compiler recognizes this possibility, and the
10444 unchecked conversion generates a warning:
10445
10446 @smallexample
10447 p2.adb:5:07: warning: possible aliasing problem with type "a2"
10448 p2.adb:5:07: warning: use -fno-strict-aliasing switch for references
10449 p2.adb:5:07: warning: or use "pragma No_Strict_Aliasing (a2);"
10450 @end smallexample
10451
10452 @noindent
10453 Unfortunately the problem is recognized when compiling the body of
10454 package @code{p2}, but the actual "bad" code is generated while
10455 compiling the body of @code{m} and this latter compilation does not see
10456 the suspicious @code{Unchecked_Conversion}.
10457
10458 As implied by the warning message, there are approaches you can use to
10459 avoid the unwanted strict aliasing optimization in a case like this.
10460
10461 One possibility is to simply avoid the use of @option{-O2}, but
10462 that is a bit drastic, since it throws away a number of useful
10463 optimizations that do not involve strict aliasing assumptions.
10464
10465 A less drastic approach is to compile the program using the
10466 option @option{-fno-strict-aliasing}. Actually it is only the
10467 unit containing the dereferencing of the suspicious pointer
10468 that needs to be compiled. So in this case, if we compile
10469 unit @code{m} with this switch, then we get the expected
10470 value of zero printed. Analyzing which units might need
10471 the switch can be painful, so a more reasonable approach
10472 is to compile the entire program with options @option{-O2}
10473 and @option{-fno-strict-aliasing}. If the performance is
10474 satisfactory with this combination of options, then the
10475 advantage is that the entire issue of possible "wrong"
10476 optimization due to strict aliasing is avoided.
10477
10478 To avoid the use of compiler switches, the configuration
10479 pragma @code{No_Strict_Aliasing} with no parameters may be
10480 used to specify that for all access types, the strict
10481 aliasing optimization should be suppressed.
10482
10483 However, these approaches are still overkill, in that they causes
10484 all manipulations of all access values to be deoptimized. A more
10485 refined approach is to concentrate attention on the specific
10486 access type identified as problematic.
10487
10488 First, if a careful analysis of uses of the pointer shows
10489 that there are no possible problematic references, then
10490 the warning can be suppressed by bracketing the
10491 instantiation of @code{Unchecked_Conversion} to turn
10492 the warning off:
10493
10494 @smallexample @c ada
10495 pragma Warnings (Off);
10496 function to_a2u is
10497 new Unchecked_Conversion (a1, a2);
10498 pragma Warnings (On);
10499 @end smallexample
10500
10501 @noindent
10502 Of course that approach is not appropriate for this particular
10503 example, since indeed there is a problematic reference. In this
10504 case we can take one of two other approaches.
10505
10506 The first possibility is to move the instantiation of unchecked
10507 conversion to the unit in which the type is declared. In
10508 this example, we would move the instantiation of
10509 @code{Unchecked_Conversion} from the body of package
10510 @code{p2} to the spec of package @code{p1}. Now the
10511 warning disappears. That's because any use of the
10512 access type knows there is a suspicious unchecked
10513 conversion, and the strict aliasing optimization
10514 is automatically suppressed for the type.
10515
10516 If it is not practical to move the unchecked conversion to the same unit
10517 in which the destination access type is declared (perhaps because the
10518 source type is not visible in that unit), you may use pragma
10519 @code{No_Strict_Aliasing} for the type. This pragma must occur in the
10520 same declarative sequence as the declaration of the access type:
10521
10522 @smallexample @c ada
10523 type a2 is access int2;
10524 pragma No_Strict_Aliasing (a2);
10525 @end smallexample
10526
10527 @noindent
10528 Here again, the compiler now knows that the strict aliasing optimization
10529 should be suppressed for any reference to type @code{a2} and the
10530 expected behavior is obtained.
10531
10532 Finally, note that although the compiler can generate warnings for
10533 simple cases of unchecked conversions, there are tricker and more
10534 indirect ways of creating type incorrect aliases which the compiler
10535 cannot detect. Examples are the use of address overlays and unchecked
10536 conversions involving composite types containing access types as
10537 components. In such cases, no warnings are generated, but there can
10538 still be aliasing problems. One safe coding practice is to forbid the
10539 use of address clauses for type overlaying, and to allow unchecked
10540 conversion only for primitive types. This is not really a significant
10541 restriction since any possible desired effect can be achieved by
10542 unchecked conversion of access values.
10543
10544 The aliasing analysis done in strict aliasing mode can certainly
10545 have significant benefits. We have seen cases of large scale
10546 application code where the time is increased by up to 5% by turning
10547 this optimization off. If you have code that includes significant
10548 usage of unchecked conversion, you might want to just stick with
10549 @option{-O1} and avoid the entire issue. If you get adequate
10550 performance at this level of optimization level, that's probably
10551 the safest approach. If tests show that you really need higher
10552 levels of optimization, then you can experiment with @option{-O2}
10553 and @option{-O2 -fno-strict-aliasing} to see how much effect this
10554 has on size and speed of the code. If you really need to use
10555 @option{-O2} with strict aliasing in effect, then you should
10556 review any uses of unchecked conversion of access types,
10557 particularly if you are getting the warnings described above.
10558
10559 @ifset vms
10560 @node Coverage Analysis
10561 @subsection Coverage Analysis
10562
10563 @noindent
10564 GNAT supports the HP Performance Coverage Analyzer (PCA), which allows
10565 the user to determine the distribution of execution time across a program,
10566 @pxref{Profiling} for details of usage.
10567 @end ifset
10568
10569
10570 @node Text_IO Suggestions
10571 @section @code{Text_IO} Suggestions
10572 @cindex @code{Text_IO} and performance
10573
10574 @noindent
10575 The @code{Ada.Text_IO} package has fairly high overheads due in part to
10576 the requirement of maintaining page and line counts. If performance
10577 is critical, a recommendation is to use @code{Stream_IO} instead of
10578 @code{Text_IO} for volume output, since this package has less overhead.
10579
10580 If @code{Text_IO} must be used, note that by default output to the standard
10581 output and standard error files is unbuffered (this provides better
10582 behavior when output statements are used for debugging, or if the
10583 progress of a program is observed by tracking the output, e.g. by
10584 using the Unix @command{tail -f} command to watch redirected output.
10585
10586 If you are generating large volumes of output with @code{Text_IO} and
10587 performance is an important factor, use a designated file instead
10588 of the standard output file, or change the standard output file to
10589 be buffered using @code{Interfaces.C_Streams.setvbuf}.
10590
10591
10592
10593 @node Reducing Size of Ada Executables with gnatelim
10594 @section Reducing Size of Ada Executables with @code{gnatelim}
10595 @findex gnatelim
10596
10597 @noindent
10598 This section describes @command{gnatelim}, a tool which detects unused
10599 subprograms and helps the compiler to create a smaller executable for your
10600 program.
10601
10602 @menu
10603 * About gnatelim::
10604 * Running gnatelim::
10605 * Correcting the List of Eliminate Pragmas::
10606 * Making Your Executables Smaller::
10607 * Summary of the gnatelim Usage Cycle::
10608 @end menu
10609
10610 @node About gnatelim
10611 @subsection About @code{gnatelim}
10612
10613 @noindent
10614 When a program shares a set of Ada
10615 packages with other programs, it may happen that this program uses
10616 only a fraction of the subprograms defined in these packages. The code
10617 created for these unused subprograms increases the size of the executable.
10618
10619 @code{gnatelim} tracks unused subprograms in an Ada program and
10620 outputs a list of GNAT-specific pragmas @code{Eliminate} marking all the
10621 subprograms that are declared but never called. By placing the list of
10622 @code{Eliminate} pragmas in the GNAT configuration file @file{gnat.adc} and
10623 recompiling your program, you may decrease the size of its executable,
10624 because the compiler will not generate the code for 'eliminated' subprograms.
10625 @xref{Pragma Eliminate,,, gnat_rm, GNAT Reference Manual}, for more
10626 information about this pragma.
10627
10628 @code{gnatelim} needs as its input data the name of the main subprogram
10629 and a bind file for a main subprogram.
10630
10631 To create a bind file for @code{gnatelim}, run @code{gnatbind} for
10632 the main subprogram. @code{gnatelim} can work with both Ada and C
10633 bind files; when both are present, it uses the Ada bind file.
10634 The following commands will build the program and create the bind file:
10635
10636 @smallexample
10637 $ gnatmake ^-c Main_Prog^/ACTIONS=COMPILE MAIN_PROG^
10638 $ gnatbind main_prog
10639 @end smallexample
10640
10641 Note that @code{gnatelim} needs neither object nor ALI files.
10642
10643 @node Running gnatelim
10644 @subsection Running @code{gnatelim}
10645
10646 @noindent
10647 @code{gnatelim} has the following command-line interface:
10648
10649 @smallexample
10650 $ gnatelim @ovar{options} name
10651 @end smallexample
10652
10653 @noindent
10654 @code{name} should be a name of a source file that contains the main subprogram
10655 of a program (partition).
10656
10657 @code{gnatelim} has the following switches:
10658
10659 @table @option
10660 @c !sort!
10661 @item ^-q^/QUIET^
10662 @cindex @option{^-q^/QUIET^} (@command{gnatelim})
10663 Quiet mode: by default @code{gnatelim} outputs to the standard error
10664 stream the number of program units left to be processed. This option turns
10665 this trace off.
10666
10667 @item ^-v^/VERBOSE^
10668 @cindex @option{^-v^/VERBOSE^} (@command{gnatelim})
10669 Verbose mode: @code{gnatelim} version information is printed as Ada
10670 comments to the standard output stream. Also, in addition to the number of
10671 program units left @code{gnatelim} will output the name of the current unit
10672 being processed.
10673
10674 @item ^-a^/ALL^
10675 @cindex @option{^-a^/ALL^} (@command{gnatelim})
10676 Also look for subprograms from the GNAT run time that can be eliminated. Note
10677 that when @file{gnat.adc} is produced using this switch, the entire program
10678 must be recompiled with switch @option{^-a^/ALL_FILES^} to @command{gnatmake}.
10679
10680 @item ^-I^/INCLUDE_DIRS=^@var{dir}
10681 @cindex @option{^-I^/INCLUDE_DIRS^} (@command{gnatelim})
10682 When looking for source files also look in directory @var{dir}. Specifying
10683 @option{^-I-^/INCLUDE_DIRS=-^} instructs @code{gnatelim} not to look for
10684 sources in the current directory.
10685
10686 @item ^-b^/BIND_FILE=^@var{bind_file}
10687 @cindex @option{^-b^/BIND_FILE^} (@command{gnatelim})
10688 Specifies @var{bind_file} as the bind file to process. If not set, the name
10689 of the bind file is computed from the full expanded Ada name
10690 of a main subprogram.
10691
10692 @item ^-C^/CONFIG_FILE=^@var{config_file}
10693 @cindex @option{^-C^/CONFIG_FILE^} (@command{gnatelim})
10694 Specifies a file @var{config_file} that contains configuration pragmas. The
10695 file must be specified with full path.
10696
10697 @item ^--GCC^/COMPILER^=@var{compiler_name}
10698 @cindex @option{^-GCC^/COMPILER^} (@command{gnatelim})
10699 Instructs @code{gnatelim} to use specific @command{gcc} compiler instead of one
10700 available on the path.
10701
10702 @item ^--GNATMAKE^/GNATMAKE^=@var{gnatmake_name}
10703 @cindex @option{^--GNATMAKE^/GNATMAKE^} (@command{gnatelim})
10704 Instructs @code{gnatelim} to use specific @command{gnatmake} instead of one
10705 available on the path.
10706 @end table
10707
10708 @noindent
10709 @code{gnatelim} sends its output to the standard output stream, and all the
10710 tracing and debug information is sent to the standard error stream.
10711 In order to produce a proper GNAT configuration file
10712 @file{gnat.adc}, redirection must be used:
10713
10714 @smallexample
10715 @ifset vms
10716 $ PIPE GNAT ELIM MAIN_PROG.ADB > GNAT.ADC
10717 @end ifset
10718 @ifclear vms
10719 $ gnatelim main_prog.adb > gnat.adc
10720 @end ifclear
10721 @end smallexample
10722
10723 @ifclear vms
10724 @noindent
10725 or
10726
10727 @smallexample
10728 $ gnatelim main_prog.adb >> gnat.adc
10729 @end smallexample
10730
10731 @noindent
10732 in order to append the @code{gnatelim} output to the existing contents of
10733 @file{gnat.adc}.
10734 @end ifclear
10735
10736 @node Correcting the List of Eliminate Pragmas
10737 @subsection Correcting the List of Eliminate Pragmas
10738
10739 @noindent
10740 In some rare cases @code{gnatelim} may try to eliminate
10741 subprograms that are actually called in the program. In this case, the
10742 compiler will generate an error message of the form:
10743
10744 @smallexample
10745 file.adb:106:07: cannot call eliminated subprogram "My_Prog"
10746 @end smallexample
10747
10748 @noindent
10749 You will need to manually remove the wrong @code{Eliminate} pragmas from
10750 the @file{gnat.adc} file. You should recompile your program
10751 from scratch after that, because you need a consistent @file{gnat.adc} file
10752 during the entire compilation.
10753
10754 @node Making Your Executables Smaller
10755 @subsection Making Your Executables Smaller
10756
10757 @noindent
10758 In order to get a smaller executable for your program you now have to
10759 recompile the program completely with the new @file{gnat.adc} file
10760 created by @code{gnatelim} in your current directory:
10761
10762 @smallexample
10763 $ gnatmake ^-f main_prog^/FORCE_COMPILE MAIN_PROG^
10764 @end smallexample
10765
10766 @noindent
10767 (Use the @option{^-f^/FORCE_COMPILE^} option for @command{gnatmake} to
10768 recompile everything
10769 with the set of pragmas @code{Eliminate} that you have obtained with
10770 @command{gnatelim}).
10771
10772 Be aware that the set of @code{Eliminate} pragmas is specific to each
10773 program. It is not recommended to merge sets of @code{Eliminate}
10774 pragmas created for different programs in one @file{gnat.adc} file.
10775
10776 @node Summary of the gnatelim Usage Cycle
10777 @subsection Summary of the gnatelim Usage Cycle
10778
10779 @noindent
10780 Here is a quick summary of the steps to be taken in order to reduce
10781 the size of your executables with @code{gnatelim}. You may use
10782 other GNAT options to control the optimization level,
10783 to produce the debugging information, to set search path, etc.
10784
10785 @enumerate
10786 @item
10787 Produce a bind file
10788
10789 @smallexample
10790 $ gnatmake ^-c main_prog^/ACTIONS=COMPILE MAIN_PROG^
10791 $ gnatbind main_prog
10792 @end smallexample
10793
10794 @item
10795 Generate a list of @code{Eliminate} pragmas
10796 @smallexample
10797 @ifset vms
10798 $ PIPE GNAT ELIM MAIN_PROG > GNAT.ADC
10799 @end ifset
10800 @ifclear vms
10801 $ gnatelim main_prog >@r{[}>@r{]} gnat.adc
10802 @end ifclear
10803 @end smallexample
10804
10805 @item
10806 Recompile the application
10807
10808 @smallexample
10809 $ gnatmake ^-f main_prog^/FORCE_COMPILE MAIN_PROG^
10810 @end smallexample
10811
10812 @end enumerate
10813
10814 @node Reducing Size of Executables with unused subprogram/data elimination
10815 @section Reducing Size of Executables with Unused Subprogram/Data Elimination
10816 @findex unused subprogram/data elimination
10817
10818 @noindent
10819 This section describes how you can eliminate unused subprograms and data from
10820 your executable just by setting options at compilation time.
10821
10822 @menu
10823 * About unused subprogram/data elimination::
10824 * Compilation options::
10825 * Example of unused subprogram/data elimination::
10826 @end menu
10827
10828 @node About unused subprogram/data elimination
10829 @subsection About unused subprogram/data elimination
10830
10831 @noindent
10832 By default, an executable contains all code and data of its composing objects
10833 (directly linked or coming from statically linked libraries), even data or code
10834 never used by this executable.
10835
10836 This feature will allow you to eliminate such unused code from your
10837 executable, making it smaller (in disk and in memory).
10838
10839 This functionality is available on all Linux platforms except for the IA-64
10840 architecture and on all cross platforms using the ELF binary file format.
10841 In both cases GNU binutils version 2.16 or later are required to enable it.
10842
10843 @node Compilation options
10844 @subsection Compilation options
10845
10846 @noindent
10847 The operation of eliminating the unused code and data from the final executable
10848 is directly performed by the linker.
10849
10850 In order to do this, it has to work with objects compiled with the
10851 following options:
10852 @option{-ffunction-sections} @option{-fdata-sections}.
10853 @cindex @option{-ffunction-sections} (@command{gcc})
10854 @cindex @option{-fdata-sections} (@command{gcc})
10855 These options are usable with C and Ada files.
10856 They will place respectively each
10857 function or data in a separate section in the resulting object file.
10858
10859 Once the objects and static libraries are created with these options, the
10860 linker can perform the dead code elimination. You can do this by setting
10861 the @option{-Wl,--gc-sections} option to gcc command or in the
10862 @option{-largs} section of @command{gnatmake}. This will perform a
10863 garbage collection of code and data never referenced.
10864
10865 If the linker performs a partial link (@option{-r} ld linker option), then you
10866 will need to provide one or several entry point using the
10867 @option{-e} / @option{--entry} ld option.
10868
10869 Note that objects compiled without the @option{-ffunction-sections} and
10870 @option{-fdata-sections} options can still be linked with the executable.
10871 However, no dead code elimination will be performed on those objects (they will
10872 be linked as is).
10873
10874 The GNAT static library is now compiled with -ffunction-sections and
10875 -fdata-sections on some platforms. This allows you to eliminate the unused code
10876 and data of the GNAT library from your executable.
10877
10878 @node Example of unused subprogram/data elimination
10879 @subsection Example of unused subprogram/data elimination
10880
10881 @noindent
10882 Here is a simple example:
10883
10884 @smallexample @c ada
10885 with Aux;
10886
10887 procedure Test is
10888 begin
10889 Aux.Used (10);
10890 end Test;
10891
10892 package Aux is
10893 Used_Data : Integer;
10894 Unused_Data : Integer;
10895
10896 procedure Used (Data : Integer);
10897 procedure Unused (Data : Integer);
10898 end Aux;
10899
10900 package body Aux is
10901 procedure Used (Data : Integer) is
10902 begin
10903 Used_Data := Data;
10904 end Used;
10905
10906 procedure Unused (Data : Integer) is
10907 begin
10908 Unused_Data := Data;
10909 end Unused;
10910 end Aux;
10911 @end smallexample
10912
10913 @noindent
10914 @code{Unused} and @code{Unused_Data} are never referenced in this code
10915 excerpt, and hence they may be safely removed from the final executable.
10916
10917 @smallexample
10918 $ gnatmake test
10919
10920 $ nm test | grep used
10921 020015f0 T aux__unused
10922 02005d88 B aux__unused_data
10923 020015cc T aux__used
10924 02005d84 B aux__used_data
10925
10926 $ gnatmake test -cargs -fdata-sections -ffunction-sections \
10927 -largs -Wl,--gc-sections
10928
10929 $ nm test | grep used
10930 02005350 T aux__used
10931 0201ffe0 B aux__used_data
10932 @end smallexample
10933
10934 @noindent
10935 It can be observed that the procedure @code{Unused} and the object
10936 @code{Unused_Data} are removed by the linker when using the
10937 appropriate options.
10938
10939 @c ********************************
10940 @node Renaming Files Using gnatchop
10941 @chapter Renaming Files Using @code{gnatchop}
10942 @findex gnatchop
10943
10944 @noindent
10945 This chapter discusses how to handle files with multiple units by using
10946 the @code{gnatchop} utility. This utility is also useful in renaming
10947 files to meet the standard GNAT default file naming conventions.
10948
10949 @menu
10950 * Handling Files with Multiple Units::
10951 * Operating gnatchop in Compilation Mode::
10952 * Command Line for gnatchop::
10953 * Switches for gnatchop::
10954 * Examples of gnatchop Usage::
10955 @end menu
10956
10957 @node Handling Files with Multiple Units
10958 @section Handling Files with Multiple Units
10959
10960 @noindent
10961 The basic compilation model of GNAT requires that a file submitted to the
10962 compiler have only one unit and there be a strict correspondence
10963 between the file name and the unit name.
10964
10965 The @code{gnatchop} utility allows both of these rules to be relaxed,
10966 allowing GNAT to process files which contain multiple compilation units
10967 and files with arbitrary file names. @code{gnatchop}
10968 reads the specified file and generates one or more output files,
10969 containing one unit per file. The unit and the file name correspond,
10970 as required by GNAT.
10971
10972 If you want to permanently restructure a set of ``foreign'' files so that
10973 they match the GNAT rules, and do the remaining development using the
10974 GNAT structure, you can simply use @command{gnatchop} once, generate the
10975 new set of files and work with them from that point on.
10976
10977 Alternatively, if you want to keep your files in the ``foreign'' format,
10978 perhaps to maintain compatibility with some other Ada compilation
10979 system, you can set up a procedure where you use @command{gnatchop} each
10980 time you compile, regarding the source files that it writes as temporary
10981 files that you throw away.
10982
10983 Note that if your file containing multiple units starts with a byte order
10984 mark (BOM) specifying UTF-8 encoding, then the files generated by gnatchop
10985 will each start with a copy of this BOM, meaning that they can be compiled
10986 automatically in UTF-8 mode without needing to specify an explicit encoding.
10987
10988 @node Operating gnatchop in Compilation Mode
10989 @section Operating gnatchop in Compilation Mode
10990
10991 @noindent
10992 The basic function of @code{gnatchop} is to take a file with multiple units
10993 and split it into separate files. The boundary between files is reasonably
10994 clear, except for the issue of comments and pragmas. In default mode, the
10995 rule is that any pragmas between units belong to the previous unit, except
10996 that configuration pragmas always belong to the following unit. Any comments
10997 belong to the following unit. These rules
10998 almost always result in the right choice of
10999 the split point without needing to mark it explicitly and most users will
11000 find this default to be what they want. In this default mode it is incorrect to
11001 submit a file containing only configuration pragmas, or one that ends in
11002 configuration pragmas, to @code{gnatchop}.
11003
11004 However, using a special option to activate ``compilation mode'',
11005 @code{gnatchop}
11006 can perform another function, which is to provide exactly the semantics
11007 required by the RM for handling of configuration pragmas in a compilation.
11008 In the absence of configuration pragmas (at the main file level), this
11009 option has no effect, but it causes such configuration pragmas to be handled
11010 in a quite different manner.
11011
11012 First, in compilation mode, if @code{gnatchop} is given a file that consists of
11013 only configuration pragmas, then this file is appended to the
11014 @file{gnat.adc} file in the current directory. This behavior provides
11015 the required behavior described in the RM for the actions to be taken
11016 on submitting such a file to the compiler, namely that these pragmas
11017 should apply to all subsequent compilations in the same compilation
11018 environment. Using GNAT, the current directory, possibly containing a
11019 @file{gnat.adc} file is the representation
11020 of a compilation environment. For more information on the
11021 @file{gnat.adc} file, see @ref{Handling of Configuration Pragmas}.
11022
11023 Second, in compilation mode, if @code{gnatchop}
11024 is given a file that starts with
11025 configuration pragmas, and contains one or more units, then these
11026 configuration pragmas are prepended to each of the chopped files. This
11027 behavior provides the required behavior described in the RM for the
11028 actions to be taken on compiling such a file, namely that the pragmas
11029 apply to all units in the compilation, but not to subsequently compiled
11030 units.
11031
11032 Finally, if configuration pragmas appear between units, they are appended
11033 to the previous unit. This results in the previous unit being illegal,
11034 since the compiler does not accept configuration pragmas that follow
11035 a unit. This provides the required RM behavior that forbids configuration
11036 pragmas other than those preceding the first compilation unit of a
11037 compilation.
11038
11039 For most purposes, @code{gnatchop} will be used in default mode. The
11040 compilation mode described above is used only if you need exactly
11041 accurate behavior with respect to compilations, and you have files
11042 that contain multiple units and configuration pragmas. In this
11043 circumstance the use of @code{gnatchop} with the compilation mode
11044 switch provides the required behavior, and is for example the mode
11045 in which GNAT processes the ACVC tests.
11046
11047 @node Command Line for gnatchop
11048 @section Command Line for @code{gnatchop}
11049
11050 @noindent
11051 The @code{gnatchop} command has the form:
11052
11053 @smallexample
11054 $ gnatchop switches @var{file name} @r{[}@var{file name} @dots{}@r{]}
11055 @ovar{directory}
11056 @end smallexample
11057
11058 @noindent
11059 The only required argument is the file name of the file to be chopped.
11060 There are no restrictions on the form of this file name. The file itself
11061 contains one or more Ada units, in normal GNAT format, concatenated
11062 together. As shown, more than one file may be presented to be chopped.
11063
11064 When run in default mode, @code{gnatchop} generates one output file in
11065 the current directory for each unit in each of the files.
11066
11067 @var{directory}, if specified, gives the name of the directory to which
11068 the output files will be written. If it is not specified, all files are
11069 written to the current directory.
11070
11071 For example, given a
11072 file called @file{hellofiles} containing
11073
11074 @smallexample @c ada
11075 @group
11076 @cartouche
11077 procedure hello;
11078
11079 with Text_IO; use Text_IO;
11080 procedure hello is
11081 begin
11082 Put_Line ("Hello");
11083 end hello;
11084 @end cartouche
11085 @end group
11086 @end smallexample
11087
11088 @noindent
11089 the command
11090
11091 @smallexample
11092 $ gnatchop ^hellofiles^HELLOFILES.^
11093 @end smallexample
11094
11095 @noindent
11096 generates two files in the current directory, one called
11097 @file{hello.ads} containing the single line that is the procedure spec,
11098 and the other called @file{hello.adb} containing the remaining text. The
11099 original file is not affected. The generated files can be compiled in
11100 the normal manner.
11101
11102 @noindent
11103 When gnatchop is invoked on a file that is empty or that contains only empty
11104 lines and/or comments, gnatchop will not fail, but will not produce any
11105 new sources.
11106
11107 For example, given a
11108 file called @file{toto.txt} containing
11109
11110 @smallexample @c ada
11111 @group
11112 @cartouche
11113 -- Just a comment
11114 @end cartouche
11115 @end group
11116 @end smallexample
11117
11118 @noindent
11119 the command
11120
11121 @smallexample
11122 $ gnatchop ^toto.txt^TOT.TXT^
11123 @end smallexample
11124
11125 @noindent
11126 will not produce any new file and will result in the following warnings:
11127
11128 @smallexample
11129 toto.txt:1:01: warning: empty file, contains no compilation units
11130 no compilation units found
11131 no source files written
11132 @end smallexample
11133
11134 @node Switches for gnatchop
11135 @section Switches for @code{gnatchop}
11136
11137 @noindent
11138 @command{gnatchop} recognizes the following switches:
11139
11140 @table @option
11141 @c !sort!
11142
11143 @item --version
11144 @cindex @option{--version} @command{gnatchop}
11145 Display Copyright and version, then exit disregarding all other options.
11146
11147 @item --help
11148 @cindex @option{--help} @command{gnatchop}
11149 If @option{--version} was not used, display usage, then exit disregarding
11150 all other options.
11151
11152 @item ^-c^/COMPILATION^
11153 @cindex @option{^-c^/COMPILATION^} (@code{gnatchop})
11154 Causes @code{gnatchop} to operate in compilation mode, in which
11155 configuration pragmas are handled according to strict RM rules. See
11156 previous section for a full description of this mode.
11157
11158 @ifclear vms
11159 @item -gnat@var{xxx}
11160 This passes the given @option{-gnat@var{xxx}} switch to @code{gnat} which is
11161 used to parse the given file. Not all @var{xxx} options make sense,
11162 but for example, the use of @option{-gnati2} allows @code{gnatchop} to
11163 process a source file that uses Latin-2 coding for identifiers.
11164 @end ifclear
11165
11166 @item ^-h^/HELP^
11167 Causes @code{gnatchop} to generate a brief help summary to the standard
11168 output file showing usage information.
11169
11170 @item ^-k@var{mm}^/FILE_NAME_MAX_LENGTH=@var{mm}^
11171 @cindex @option{^-k^/FILE_NAME_MAX_LENGTH^} (@code{gnatchop})
11172 Limit generated file names to the specified number @code{mm}
11173 of characters.
11174 This is useful if the
11175 resulting set of files is required to be interoperable with systems
11176 which limit the length of file names.
11177 @ifset vms
11178 If no value is given, or
11179 if no @code{/FILE_NAME_MAX_LENGTH} qualifier is given,
11180 a default of 39, suitable for OpenVMS Alpha
11181 Systems, is assumed
11182 @end ifset
11183 @ifclear vms
11184 No space is allowed between the @option{-k} and the numeric value. The numeric
11185 value may be omitted in which case a default of @option{-k8},
11186 suitable for use
11187 with DOS-like file systems, is used. If no @option{-k} switch
11188 is present then
11189 there is no limit on the length of file names.
11190 @end ifclear
11191
11192 @item ^-p^/PRESERVE^
11193 @cindex @option{^-p^/PRESERVE^} (@code{gnatchop})
11194 Causes the file ^modification^creation^ time stamp of the input file to be
11195 preserved and used for the time stamp of the output file(s). This may be
11196 useful for preserving coherency of time stamps in an environment where
11197 @code{gnatchop} is used as part of a standard build process.
11198
11199 @item ^-q^/QUIET^
11200 @cindex @option{^-q^/QUIET^} (@code{gnatchop})
11201 Causes output of informational messages indicating the set of generated
11202 files to be suppressed. Warnings and error messages are unaffected.
11203
11204 @item ^-r^/REFERENCE^
11205 @cindex @option{^-r^/REFERENCE^} (@code{gnatchop})
11206 @findex Source_Reference
11207 Generate @code{Source_Reference} pragmas. Use this switch if the output
11208 files are regarded as temporary and development is to be done in terms
11209 of the original unchopped file. This switch causes
11210 @code{Source_Reference} pragmas to be inserted into each of the
11211 generated files to refers back to the original file name and line number.
11212 The result is that all error messages refer back to the original
11213 unchopped file.
11214 In addition, the debugging information placed into the object file (when
11215 the @option{^-g^/DEBUG^} switch of @command{gcc} or @command{gnatmake} is
11216 specified)
11217 also refers back to this original file so that tools like profilers and
11218 debuggers will give information in terms of the original unchopped file.
11219
11220 If the original file to be chopped itself contains
11221 a @code{Source_Reference}
11222 pragma referencing a third file, then gnatchop respects
11223 this pragma, and the generated @code{Source_Reference} pragmas
11224 in the chopped file refer to the original file, with appropriate
11225 line numbers. This is particularly useful when @code{gnatchop}
11226 is used in conjunction with @code{gnatprep} to compile files that
11227 contain preprocessing statements and multiple units.
11228
11229 @item ^-v^/VERBOSE^
11230 @cindex @option{^-v^/VERBOSE^} (@code{gnatchop})
11231 Causes @code{gnatchop} to operate in verbose mode. The version
11232 number and copyright notice are output, as well as exact copies of
11233 the gnat1 commands spawned to obtain the chop control information.
11234
11235 @item ^-w^/OVERWRITE^
11236 @cindex @option{^-w^/OVERWRITE^} (@code{gnatchop})
11237 Overwrite existing file names. Normally @code{gnatchop} regards it as a
11238 fatal error if there is already a file with the same name as a
11239 file it would otherwise output, in other words if the files to be
11240 chopped contain duplicated units. This switch bypasses this
11241 check, and causes all but the last instance of such duplicated
11242 units to be skipped.
11243
11244 @ifclear vms
11245 @item --GCC=@var{xxxx}
11246 @cindex @option{--GCC=} (@code{gnatchop})
11247 Specify the path of the GNAT parser to be used. When this switch is used,
11248 no attempt is made to add the prefix to the GNAT parser executable.
11249 @end ifclear
11250 @end table
11251
11252 @node Examples of gnatchop Usage
11253 @section Examples of @code{gnatchop} Usage
11254
11255 @table @code
11256 @ifset vms
11257 @item gnatchop /OVERWRITE HELLO_S.ADA [PRERELEASE.FILES]
11258 @end ifset
11259 @ifclear vms
11260 @item gnatchop -w hello_s.ada prerelease/files
11261 @end ifclear
11262
11263 Chops the source file @file{hello_s.ada}. The output files will be
11264 placed in the directory @file{^prerelease/files^[PRERELEASE.FILES]^},
11265 overwriting any
11266 files with matching names in that directory (no files in the current
11267 directory are modified).
11268
11269 @item gnatchop ^archive^ARCHIVE.^
11270 Chops the source file @file{^archive^ARCHIVE.^}
11271 into the current directory. One
11272 useful application of @code{gnatchop} is in sending sets of sources
11273 around, for example in email messages. The required sources are simply
11274 concatenated (for example, using a ^Unix @code{cat}^VMS @code{APPEND/NEW}^
11275 command), and then
11276 @command{gnatchop} is used at the other end to reconstitute the original
11277 file names.
11278
11279 @item gnatchop file1 file2 file3 direc
11280 Chops all units in files @file{file1}, @file{file2}, @file{file3}, placing
11281 the resulting files in the directory @file{direc}. Note that if any units
11282 occur more than once anywhere within this set of files, an error message
11283 is generated, and no files are written. To override this check, use the
11284 @option{^-w^/OVERWRITE^} switch,
11285 in which case the last occurrence in the last file will
11286 be the one that is output, and earlier duplicate occurrences for a given
11287 unit will be skipped.
11288 @end table
11289
11290 @node Configuration Pragmas
11291 @chapter Configuration Pragmas
11292 @cindex Configuration pragmas
11293 @cindex Pragmas, configuration
11294
11295 @noindent
11296 Configuration pragmas include those pragmas described as
11297 such in the Ada Reference Manual, as well as
11298 implementation-dependent pragmas that are configuration pragmas.
11299 @xref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference Manual},
11300 for details on these additional GNAT-specific configuration pragmas.
11301 Most notably, the pragma @code{Source_File_Name}, which allows
11302 specifying non-default names for source files, is a configuration
11303 pragma. The following is a complete list of configuration pragmas
11304 recognized by GNAT:
11305
11306 @smallexample
11307 Ada_83
11308 Ada_95
11309 Ada_05
11310 Ada_2005
11311 Assertion_Policy
11312 Assume_No_Invalid_Values
11313 C_Pass_By_Copy
11314 Check_Name
11315 Check_Policy
11316 Compile_Time_Error
11317 Compile_Time_Warning
11318 Compiler_Unit
11319 Component_Alignment
11320 Convention_Identifier
11321 Debug_Policy
11322 Detect_Blocking
11323 Discard_Names
11324 Elaboration_Checks
11325 Eliminate
11326 Extend_System
11327 Extensions_Allowed
11328 External_Name_Casing
11329 Fast_Math
11330 Favor_Top_Level
11331 Float_Representation
11332 Implicit_Packing
11333 Initialize_Scalars
11334 Interrupt_State
11335 License
11336 Locking_Policy
11337 Long_Float
11338 No_Run_Time
11339 No_Strict_Aliasing
11340 Normalize_Scalars
11341 Optimize_Alignment
11342 Persistent_BSS
11343 Polling
11344 Priority_Specific_Dispatching
11345 Profile
11346 Profile_Warnings
11347 Propagate_Exceptions
11348 Queuing_Policy
11349 Ravenscar
11350 Restricted_Run_Time
11351 Restrictions
11352 Restrictions_Warnings
11353 Reviewable
11354 Source_File_Name
11355 Source_File_Name_Project
11356 Style_Checks
11357 Suppress
11358 Suppress_Exception_Locations
11359 Task_Dispatching_Policy
11360 Universal_Data
11361 Unsuppress
11362 Use_VADS_Size
11363 Validity_Checks
11364 Warnings
11365 Wide_Character_Encoding
11366
11367 @end smallexample
11368
11369 @menu
11370 * Handling of Configuration Pragmas::
11371 * The Configuration Pragmas Files::
11372 @end menu
11373
11374 @node Handling of Configuration Pragmas
11375 @section Handling of Configuration Pragmas
11376
11377 Configuration pragmas may either appear at the start of a compilation
11378 unit, in which case they apply only to that unit, or they may apply to
11379 all compilations performed in a given compilation environment.
11380
11381 GNAT also provides the @code{gnatchop} utility to provide an automatic
11382 way to handle configuration pragmas following the semantics for
11383 compilations (that is, files with multiple units), described in the RM.
11384 See @ref{Operating gnatchop in Compilation Mode} for details.
11385 However, for most purposes, it will be more convenient to edit the
11386 @file{gnat.adc} file that contains configuration pragmas directly,
11387 as described in the following section.
11388
11389 @node The Configuration Pragmas Files
11390 @section The Configuration Pragmas Files
11391 @cindex @file{gnat.adc}
11392
11393 @noindent
11394 In GNAT a compilation environment is defined by the current
11395 directory at the time that a compile command is given. This current
11396 directory is searched for a file whose name is @file{gnat.adc}. If
11397 this file is present, it is expected to contain one or more
11398 configuration pragmas that will be applied to the current compilation.
11399 However, if the switch @option{-gnatA} is used, @file{gnat.adc} is not
11400 considered.
11401
11402 Configuration pragmas may be entered into the @file{gnat.adc} file
11403 either by running @code{gnatchop} on a source file that consists only of
11404 configuration pragmas, or more conveniently by
11405 direct editing of the @file{gnat.adc} file, which is a standard format
11406 source file.
11407
11408 In addition to @file{gnat.adc}, additional files containing configuration
11409 pragmas may be applied to the current compilation using the switch
11410 @option{-gnatec}@var{path}. @var{path} must designate an existing file that
11411 contains only configuration pragmas. These configuration pragmas are
11412 in addition to those found in @file{gnat.adc} (provided @file{gnat.adc}
11413 is present and switch @option{-gnatA} is not used).
11414
11415 It is allowed to specify several switches @option{-gnatec}, all of which
11416 will be taken into account.
11417
11418 If you are using project file, a separate mechanism is provided using
11419 project attributes, see @ref{Specifying Configuration Pragmas} for more
11420 details.
11421
11422 @ifset vms
11423 Of special interest to GNAT OpenVMS Alpha is the following
11424 configuration pragma:
11425
11426 @smallexample @c ada
11427 @cartouche
11428 pragma Extend_System (Aux_DEC);
11429 @end cartouche
11430 @end smallexample
11431
11432 @noindent
11433 In the presence of this pragma, GNAT adds to the definition of the
11434 predefined package SYSTEM all the additional types and subprograms that are
11435 defined in HP Ada. See @ref{Compatibility with HP Ada} for details.
11436 @end ifset
11437
11438 @node Handling Arbitrary File Naming Conventions Using gnatname
11439 @chapter Handling Arbitrary File Naming Conventions Using @code{gnatname}
11440 @cindex Arbitrary File Naming Conventions
11441
11442 @menu
11443 * Arbitrary File Naming Conventions::
11444 * Running gnatname::
11445 * Switches for gnatname::
11446 * Examples of gnatname Usage::
11447 @end menu
11448
11449 @node Arbitrary File Naming Conventions
11450 @section Arbitrary File Naming Conventions
11451
11452 @noindent
11453 The GNAT compiler must be able to know the source file name of a compilation
11454 unit. When using the standard GNAT default file naming conventions
11455 (@code{.ads} for specs, @code{.adb} for bodies), the GNAT compiler
11456 does not need additional information.
11457
11458 @noindent
11459 When the source file names do not follow the standard GNAT default file naming
11460 conventions, the GNAT compiler must be given additional information through
11461 a configuration pragmas file (@pxref{Configuration Pragmas})
11462 or a project file.
11463 When the non-standard file naming conventions are well-defined,
11464 a small number of pragmas @code{Source_File_Name} specifying a naming pattern
11465 (@pxref{Alternative File Naming Schemes}) may be sufficient. However,
11466 if the file naming conventions are irregular or arbitrary, a number
11467 of pragma @code{Source_File_Name} for individual compilation units
11468 must be defined.
11469 To help maintain the correspondence between compilation unit names and
11470 source file names within the compiler,
11471 GNAT provides a tool @code{gnatname} to generate the required pragmas for a
11472 set of files.
11473
11474 @node Running gnatname
11475 @section Running @code{gnatname}
11476
11477 @noindent
11478 The usual form of the @code{gnatname} command is
11479
11480 @smallexample
11481 $ gnatname @ovar{switches} @var{naming_pattern} @ovar{naming_patterns}
11482 @r{[}--and @ovar{switches} @var{naming_pattern} @ovar{naming_patterns}@r{]}
11483 @end smallexample
11484
11485 @noindent
11486 All of the arguments are optional. If invoked without any argument,
11487 @code{gnatname} will display its usage.
11488
11489 @noindent
11490 When used with at least one naming pattern, @code{gnatname} will attempt to
11491 find all the compilation units in files that follow at least one of the
11492 naming patterns. To find these compilation units,
11493 @code{gnatname} will use the GNAT compiler in syntax-check-only mode on all
11494 regular files.
11495
11496 @noindent
11497 One or several Naming Patterns may be given as arguments to @code{gnatname}.
11498 Each Naming Pattern is enclosed between double quotes.
11499 A Naming Pattern is a regular expression similar to the wildcard patterns
11500 used in file names by the Unix shells or the DOS prompt.
11501
11502 @noindent
11503 @code{gnatname} may be called with several sections of directories/patterns.
11504 Sections are separated by switch @code{--and}. In each section, there must be
11505 at least one pattern. If no directory is specified in a section, the current
11506 directory (or the project directory is @code{-P} is used) is implied.
11507 The options other that the directory switches and the patterns apply globally
11508 even if they are in different sections.
11509
11510 @noindent
11511 Examples of Naming Patterns are
11512
11513 @smallexample
11514 "*.[12].ada"
11515 "*.ad[sb]*"
11516 "body_*" "spec_*"
11517 @end smallexample
11518
11519 @noindent
11520 For a more complete description of the syntax of Naming Patterns,
11521 see the second kind of regular expressions described in @file{g-regexp.ads}
11522 (the ``Glob'' regular expressions).
11523
11524 @noindent
11525 When invoked with no switch @code{-P}, @code{gnatname} will create a
11526 configuration pragmas file @file{gnat.adc} in the current working directory,
11527 with pragmas @code{Source_File_Name} for each file that contains a valid Ada
11528 unit.
11529
11530 @node Switches for gnatname
11531 @section Switches for @code{gnatname}
11532
11533 @noindent
11534 Switches for @code{gnatname} must precede any specified Naming Pattern.
11535
11536 @noindent
11537 You may specify any of the following switches to @code{gnatname}:
11538
11539 @table @option
11540 @c !sort!
11541
11542 @item --version
11543 @cindex @option{--version} @command{gnatname}
11544 Display Copyright and version, then exit disregarding all other options.
11545
11546 @item --help
11547 @cindex @option{--help} @command{gnatname}
11548 If @option{--version} was not used, display usage, then exit disregarding
11549 all other options.
11550
11551 @item --and
11552 Start another section of directories/patterns.
11553
11554 @item ^-c^/CONFIG_FILE=^@file{file}
11555 @cindex @option{^-c^/CONFIG_FILE^} (@code{gnatname})
11556 Create a configuration pragmas file @file{file} (instead of the default
11557 @file{gnat.adc}).
11558 @ifclear vms
11559 There may be zero, one or more space between @option{-c} and
11560 @file{file}.
11561 @end ifclear
11562 @file{file} may include directory information. @file{file} must be
11563 writable. There may be only one switch @option{^-c^/CONFIG_FILE^}.
11564 When a switch @option{^-c^/CONFIG_FILE^} is
11565 specified, no switch @option{^-P^/PROJECT_FILE^} may be specified (see below).
11566
11567 @item ^-d^/SOURCE_DIRS=^@file{dir}
11568 @cindex @option{^-d^/SOURCE_DIRS^} (@code{gnatname})
11569 Look for source files in directory @file{dir}. There may be zero, one or more
11570 spaces between @option{^-d^/SOURCE_DIRS=^} and @file{dir}.
11571 When a switch @option{^-d^/SOURCE_DIRS^}
11572 is specified, the current working directory will not be searched for source
11573 files, unless it is explicitly specified with a @option{^-d^/SOURCE_DIRS^}
11574 or @option{^-D^/DIR_FILES^} switch.
11575 Several switches @option{^-d^/SOURCE_DIRS^} may be specified.
11576 If @file{dir} is a relative path, it is relative to the directory of
11577 the configuration pragmas file specified with switch
11578 @option{^-c^/CONFIG_FILE^},
11579 or to the directory of the project file specified with switch
11580 @option{^-P^/PROJECT_FILE^} or,
11581 if neither switch @option{^-c^/CONFIG_FILE^}
11582 nor switch @option{^-P^/PROJECT_FILE^} are specified, it is relative to the
11583 current working directory. The directory
11584 specified with switch @option{^-d^/SOURCE_DIRS^} must exist and be readable.
11585
11586 @item ^-D^/DIRS_FILE=^@file{file}
11587 @cindex @option{^-D^/DIRS_FILE^} (@code{gnatname})
11588 Look for source files in all directories listed in text file @file{file}.
11589 There may be zero, one or more spaces between @option{^-D^/DIRS_FILE=^}
11590 and @file{file}.
11591 @file{file} must be an existing, readable text file.
11592 Each nonempty line in @file{file} must be a directory.
11593 Specifying switch @option{^-D^/DIRS_FILE^} is equivalent to specifying as many
11594 switches @option{^-d^/SOURCE_DIRS^} as there are nonempty lines in
11595 @file{file}.
11596
11597 @item ^-f^/FOREIGN_PATTERN=^@file{pattern}
11598 @cindex @option{^-f^/FOREIGN_PATTERN^} (@code{gnatname})
11599 Foreign patterns. Using this switch, it is possible to add sources of languages
11600 other than Ada to the list of sources of a project file.
11601 It is only useful if a ^-P^/PROJECT_FILE^ switch is used.
11602 For example,
11603 @smallexample
11604 gnatname ^-Pprj -f"*.c"^/PROJECT_FILE=PRJ /FOREIGN_PATTERN=*.C^ "*.ada"
11605 @end smallexample
11606 @noindent
11607 will look for Ada units in all files with the @file{.ada} extension,
11608 and will add to the list of file for project @file{prj.gpr} the C files
11609 with extension @file{.^c^C^}.
11610
11611 @item ^-h^/HELP^
11612 @cindex @option{^-h^/HELP^} (@code{gnatname})
11613 Output usage (help) information. The output is written to @file{stdout}.
11614
11615 @item ^-P^/PROJECT_FILE=^@file{proj}
11616 @cindex @option{^-P^/PROJECT_FILE^} (@code{gnatname})
11617 Create or update project file @file{proj}. There may be zero, one or more space
11618 between @option{-P} and @file{proj}. @file{proj} may include directory
11619 information. @file{proj} must be writable.
11620 There may be only one switch @option{^-P^/PROJECT_FILE^}.
11621 When a switch @option{^-P^/PROJECT_FILE^} is specified,
11622 no switch @option{^-c^/CONFIG_FILE^} may be specified.
11623
11624 @item ^-v^/VERBOSE^
11625 @cindex @option{^-v^/VERBOSE^} (@code{gnatname})
11626 Verbose mode. Output detailed explanation of behavior to @file{stdout}.
11627 This includes name of the file written, the name of the directories to search
11628 and, for each file in those directories whose name matches at least one of
11629 the Naming Patterns, an indication of whether the file contains a unit,
11630 and if so the name of the unit.
11631
11632 @item ^-v -v^/VERBOSE /VERBOSE^
11633 @cindex @option{^-v -v^/VERBOSE /VERBOSE^} (@code{gnatname})
11634 Very Verbose mode. In addition to the output produced in verbose mode,
11635 for each file in the searched directories whose name matches none of
11636 the Naming Patterns, an indication is given that there is no match.
11637
11638 @item ^-x^/EXCLUDED_PATTERN=^@file{pattern}
11639 @cindex @option{^-x^/EXCLUDED_PATTERN^} (@code{gnatname})
11640 Excluded patterns. Using this switch, it is possible to exclude some files
11641 that would match the name patterns. For example,
11642 @smallexample
11643 gnatname ^-x "*_nt.ada"^/EXCLUDED_PATTERN=*_nt.ada^ "*.ada"
11644 @end smallexample
11645 @noindent
11646 will look for Ada units in all files with the @file{.ada} extension,
11647 except those whose names end with @file{_nt.ada}.
11648
11649 @end table
11650
11651 @node Examples of gnatname Usage
11652 @section Examples of @code{gnatname} Usage
11653
11654 @ifset vms
11655 @smallexample
11656 $ gnatname /CONFIG_FILE=[HOME.ME]NAMES.ADC /SOURCE_DIRS=SOURCES "[a-z]*.ada*"
11657 @end smallexample
11658 @end ifset
11659
11660 @ifclear vms
11661 @smallexample
11662 $ gnatname -c /home/me/names.adc -d sources "[a-z]*.ada*"
11663 @end smallexample
11664 @end ifclear
11665
11666 @noindent
11667 In this example, the directory @file{^/home/me^[HOME.ME]^} must already exist
11668 and be writable. In addition, the directory
11669 @file{^/home/me/sources^[HOME.ME.SOURCES]^} (specified by
11670 @option{^-d sources^/SOURCE_DIRS=SOURCES^}) must exist and be readable.
11671
11672 @ifclear vms
11673 Note the optional spaces after @option{-c} and @option{-d}.
11674 @end ifclear
11675
11676 @smallexample
11677 @ifclear vms
11678 $ gnatname -P/home/me/proj -x "*_nt_body.ada"
11679 -dsources -dsources/plus -Dcommon_dirs.txt "body_*" "spec_*"
11680 @end ifclear
11681 @ifset vms
11682 $ gnatname /PROJECT_FILE=[HOME.ME]PROJ
11683 /EXCLUDED_PATTERN=*_nt_body.ada
11684 /SOURCE_DIRS=(SOURCES,[SOURCES.PLUS])
11685 /DIRS_FILE=COMMON_DIRS.TXT "body_*" "spec_*"
11686 @end ifset
11687 @end smallexample
11688
11689 Note that several switches @option{^-d^/SOURCE_DIRS^} may be used,
11690 even in conjunction with one or several switches
11691 @option{^-D^/DIRS_FILE^}. Several Naming Patterns and one excluded pattern
11692 are used in this example.
11693
11694 @c *****************************************
11695 @c * G N A T P r o j e c t M a n a g e r *
11696 @c *****************************************
11697 @node GNAT Project Manager
11698 @chapter GNAT Project Manager
11699
11700 @menu
11701 * Introduction::
11702 * Examples of Project Files::
11703 * Project File Syntax::
11704 * Objects and Sources in Project Files::
11705 * Importing Projects::
11706 * Project Extension::
11707 * Project Hierarchy Extension::
11708 * External References in Project Files::
11709 * Packages in Project Files::
11710 * Variables from Imported Projects::
11711 * Naming Schemes::
11712 * Library Projects::
11713 * Stand-alone Library Projects::
11714 * Switches Related to Project Files::
11715 * Tools Supporting Project Files::
11716 * An Extended Example::
11717 * Project File Complete Syntax::
11718 @end menu
11719
11720 @c ****************
11721 @c * Introduction *
11722 @c ****************
11723
11724 @node Introduction
11725 @section Introduction
11726
11727 @noindent
11728 This chapter describes GNAT's @emph{Project Manager}, a facility that allows
11729 you to manage complex builds involving a number of source files, directories,
11730 and compilation options for different system configurations. In particular,
11731 project files allow you to specify:
11732 @itemize @bullet
11733 @item
11734 The directory or set of directories containing the source files, and/or the
11735 names of the specific source files themselves
11736 @item
11737 The directory in which the compiler's output
11738 (@file{ALI} files, object files, tree files) is to be placed
11739 @item
11740 The directory in which the executable programs is to be placed
11741 @item
11742 ^Switch^Switch^ settings for any of the project-enabled tools
11743 (@command{gnatmake}, compiler, binder, linker, @code{gnatls}, @code{gnatxref},
11744 @code{gnatfind}); you can apply these settings either globally or to individual
11745 compilation units.
11746 @item
11747 The source files containing the main subprogram(s) to be built
11748 @item
11749 The source programming language(s) (currently Ada and/or C)
11750 @item
11751 Source file naming conventions; you can specify these either globally or for
11752 individual compilation units
11753 @end itemize
11754
11755 @menu
11756 * Project Files::
11757 @end menu
11758
11759 @node Project Files
11760 @subsection Project Files
11761
11762 @noindent
11763 Project files are written in a syntax close to that of Ada, using familiar
11764 notions such as packages, context clauses, declarations, default values,
11765 assignments, and inheritance. Finally, project files can be built
11766 hierarchically from other project files, simplifying complex system
11767 integration and project reuse.
11768
11769 A @dfn{project} is a specific set of values for various compilation properties.
11770 The settings for a given project are described by means of
11771 a @dfn{project file}, which is a text file written in an Ada-like syntax.
11772 Property values in project files are either strings or lists of strings.
11773 Properties that are not explicitly set receive default values. A project
11774 file may interrogate the values of @dfn{external variables} (user-defined
11775 command-line switches or environment variables), and it may specify property
11776 settings conditionally, based on the value of such variables.
11777
11778 In simple cases, a project's source files depend only on other source files
11779 in the same project, or on the predefined libraries. (@emph{Dependence} is
11780 used in
11781 the Ada technical sense; as in one Ada unit @code{with}ing another.) However,
11782 the Project Manager also allows more sophisticated arrangements,
11783 where the source files in one project depend on source files in other
11784 projects:
11785 @itemize @bullet
11786 @item
11787 One project can @emph{import} other projects containing needed source files.
11788 @item
11789 You can organize GNAT projects in a hierarchy: a @emph{child} project
11790 can extend a @emph{parent} project, inheriting the parent's source files and
11791 optionally overriding any of them with alternative versions
11792 @end itemize
11793
11794 @noindent
11795 More generally, the Project Manager lets you structure large development
11796 efforts into hierarchical subsystems, where build decisions are delegated
11797 to the subsystem level, and thus different compilation environments
11798 (^switch^switch^ settings) used for different subsystems.
11799
11800 The Project Manager is invoked through the
11801 @option{^-P^/PROJECT_FILE=^@emph{projectfile}}
11802 switch to @command{gnatmake} or to the @command{^gnat^GNAT^} front driver.
11803 @ifclear vms
11804 There may be zero, one or more spaces between @option{-P} and
11805 @option{@emph{projectfile}}.
11806 @end ifclear
11807 If you want to define (on the command line) an external variable that is
11808 queried by the project file, you must use the
11809 @option{^-X^/EXTERNAL_REFERENCE=^@emph{vbl}=@emph{value}} switch.
11810 The Project Manager parses and interprets the project file, and drives the
11811 invoked tool based on the project settings.
11812
11813 The Project Manager supports a wide range of development strategies,
11814 for systems of all sizes. Here are some typical practices that are
11815 easily handled:
11816 @itemize @bullet
11817 @item
11818 Using a common set of source files, but generating object files in different
11819 directories via different ^switch^switch^ settings
11820 @item
11821 Using a mostly-shared set of source files, but with different versions of
11822 some unit or units
11823 @end itemize
11824
11825 @noindent
11826 The destination of an executable can be controlled inside a project file
11827 using the @option{^-o^-o^}
11828 ^switch^switch^.
11829 In the absence of such a ^switch^switch^ either inside
11830 the project file or on the command line, any executable files generated by
11831 @command{gnatmake} are placed in the directory @code{Exec_Dir} specified
11832 in the project file. If no @code{Exec_Dir} is specified, they will be placed
11833 in the object directory of the project.
11834
11835 You can use project files to achieve some of the effects of a source
11836 versioning system (for example, defining separate projects for
11837 the different sets of sources that comprise different releases) but the
11838 Project Manager is independent of any source configuration management tools
11839 that might be used by the developers.
11840
11841 The next section introduces the main features of GNAT's project facility
11842 through a sequence of examples; subsequent sections will present the syntax
11843 and semantics in more detail. A more formal description of the project
11844 facility appears in @ref{Project File Reference,,, gnat_rm, GNAT
11845 Reference Manual}.
11846
11847 @c *****************************
11848 @c * Examples of Project Files *
11849 @c *****************************
11850
11851 @node Examples of Project Files
11852 @section Examples of Project Files
11853 @noindent
11854 This section illustrates some of the typical uses of project files and
11855 explains their basic structure and behavior.
11856
11857 @menu
11858 * Common Sources with Different ^Switches^Switches^ and Directories::
11859 * Using External Variables::
11860 * Importing Other Projects::
11861 * Extending a Project::
11862 @end menu
11863
11864 @node Common Sources with Different ^Switches^Switches^ and Directories
11865 @subsection Common Sources with Different ^Switches^Switches^ and Directories
11866
11867 @menu
11868 * Source Files::
11869 * Specifying the Object Directory::
11870 * Specifying the Exec Directory::
11871 * Project File Packages::
11872 * Specifying ^Switch^Switch^ Settings::
11873 * Main Subprograms::
11874 * Executable File Names::
11875 * Source File Naming Conventions::
11876 * Source Language(s)::
11877 @end menu
11878
11879 @noindent
11880 Suppose that the Ada source files @file{pack.ads}, @file{pack.adb}, and
11881 @file{proc.adb} are in the @file{/common} directory. The file
11882 @file{proc.adb} contains an Ada main subprogram @code{Proc} that @code{with}s
11883 package @code{Pack}. We want to compile these source files under two sets
11884 of ^switches^switches^:
11885 @itemize @bullet
11886 @item
11887 When debugging, we want to pass the @option{-g} switch to @command{gnatmake},
11888 and the @option{^-gnata^-gnata^},
11889 @option{^-gnato^-gnato^},
11890 and @option{^-gnatE^-gnatE^} switches to the
11891 compiler; the compiler's output is to appear in @file{/common/debug}
11892 @item
11893 When preparing a release version, we want to pass the @option{^-O2^O2^} switch
11894 to the compiler; the compiler's output is to appear in @file{/common/release}
11895 @end itemize
11896
11897 @noindent
11898 The GNAT project files shown below, respectively @file{debug.gpr} and
11899 @file{release.gpr} in the @file{/common} directory, achieve these effects.
11900
11901 Schematically:
11902 @smallexample
11903 @group
11904 ^/common^[COMMON]^
11905 debug.gpr
11906 release.gpr
11907 pack.ads
11908 pack.adb
11909 proc.adb
11910 @end group
11911 @group
11912 ^/common/debug^[COMMON.DEBUG]^
11913 proc.ali, proc.o
11914 pack.ali, pack.o
11915 @end group
11916 @group
11917 ^/common/release^[COMMON.RELEASE]^
11918 proc.ali, proc.o
11919 pack.ali, pack.o
11920 @end group
11921 @end smallexample
11922 Here are the corresponding project files:
11923
11924 @smallexample @c projectfile
11925 @group
11926 project Debug is
11927 for Object_Dir use "debug";
11928 for Main use ("proc");
11929
11930 package Builder is
11931 for ^Default_Switches^Default_Switches^ ("Ada")
11932 use ("^-g^-g^");
11933 for Executable ("proc.adb") use "proc1";
11934 end Builder;
11935 @end group
11936
11937 @group
11938 package Compiler is
11939 for ^Default_Switches^Default_Switches^ ("Ada")
11940 use ("-fstack-check",
11941 "^-gnata^-gnata^",
11942 "^-gnato^-gnato^",
11943 "^-gnatE^-gnatE^");
11944 end Compiler;
11945 end Debug;
11946 @end group
11947 @end smallexample
11948
11949 @smallexample @c projectfile
11950 @group
11951 project Release is
11952 for Object_Dir use "release";
11953 for Exec_Dir use ".";
11954 for Main use ("proc");
11955
11956 package Compiler is
11957 for ^Default_Switches^Default_Switches^ ("Ada")
11958 use ("^-O2^-O2^");
11959 end Compiler;
11960 end Release;
11961 @end group
11962 @end smallexample
11963
11964 @noindent
11965 The name of the project defined by @file{debug.gpr} is @code{"Debug"} (case
11966 insensitive), and analogously the project defined by @file{release.gpr} is
11967 @code{"Release"}. For consistency the file should have the same name as the
11968 project, and the project file's extension should be @code{"gpr"}. These
11969 conventions are not required, but a warning is issued if they are not followed.
11970
11971 If the current directory is @file{^/temp^[TEMP]^}, then the command
11972 @smallexample
11973 gnatmake ^-P/common/debug.gpr^/PROJECT_FILE=[COMMON]DEBUG^
11974 @end smallexample
11975
11976 @noindent
11977 generates object and ALI files in @file{^/common/debug^[COMMON.DEBUG]^},
11978 as well as the @code{^proc1^PROC1.EXE^} executable,
11979 using the ^switch^switch^ settings defined in the project file.
11980
11981 Likewise, the command
11982 @smallexample
11983 gnatmake ^-P/common/release.gpr^/PROJECT_FILE=[COMMON]RELEASE^
11984 @end smallexample
11985
11986 @noindent
11987 generates object and ALI files in @file{^/common/release^[COMMON.RELEASE]^},
11988 and the @code{^proc^PROC.EXE^}
11989 executable in @file{^/common^[COMMON]^},
11990 using the ^switch^switch^ settings from the project file.
11991
11992 @node Source Files
11993 @unnumberedsubsubsec Source Files
11994
11995 @noindent
11996 If a project file does not explicitly specify a set of source directories or
11997 a set of source files, then by default the project's source files are the
11998 Ada source files in the project file directory. Thus @file{pack.ads},
11999 @file{pack.adb}, and @file{proc.adb} are the source files for both projects.
12000
12001 @node Specifying the Object Directory
12002 @unnumberedsubsubsec Specifying the Object Directory
12003
12004 @noindent
12005 Several project properties are modeled by Ada-style @emph{attributes};
12006 a property is defined by supplying the equivalent of an Ada attribute
12007 definition clause in the project file.
12008 A project's object directory is another such a property; the corresponding
12009 attribute is @code{Object_Dir}, and its value is also a string expression,
12010 specified either as absolute or relative. In the later case,
12011 it is relative to the project file directory. Thus the compiler's
12012 output is directed to @file{^/common/debug^[COMMON.DEBUG]^}
12013 (for the @code{Debug} project)
12014 and to @file{^/common/release^[COMMON.RELEASE]^}
12015 (for the @code{Release} project).
12016 If @code{Object_Dir} is not specified, then the default is the project file
12017 directory itself.
12018
12019 @node Specifying the Exec Directory
12020 @unnumberedsubsubsec Specifying the Exec Directory
12021
12022 @noindent
12023 A project's exec directory is another property; the corresponding
12024 attribute is @code{Exec_Dir}, and its value is also a string expression,
12025 either specified as relative or absolute. If @code{Exec_Dir} is not specified,
12026 then the default is the object directory (which may also be the project file
12027 directory if attribute @code{Object_Dir} is not specified). Thus the executable
12028 is placed in @file{^/common/debug^[COMMON.DEBUG]^}
12029 for the @code{Debug} project (attribute @code{Exec_Dir} not specified)
12030 and in @file{^/common^[COMMON]^} for the @code{Release} project.
12031
12032 @node Project File Packages
12033 @unnumberedsubsubsec Project File Packages
12034
12035 @noindent
12036 A GNAT tool that is integrated with the Project Manager is modeled by a
12037 corresponding package in the project file. In the example above,
12038 The @code{Debug} project defines the packages @code{Builder}
12039 (for @command{gnatmake}) and @code{Compiler};
12040 the @code{Release} project defines only the @code{Compiler} package.
12041
12042 The Ada-like package syntax is not to be taken literally. Although packages in
12043 project files bear a surface resemblance to packages in Ada source code, the
12044 notation is simply a way to convey a grouping of properties for a named
12045 entity. Indeed, the package names permitted in project files are restricted
12046 to a predefined set, corresponding to the project-aware tools, and the contents
12047 of packages are limited to a small set of constructs.
12048 The packages in the example above contain attribute definitions.
12049
12050 @node Specifying ^Switch^Switch^ Settings
12051 @unnumberedsubsubsec Specifying ^Switch^Switch^ Settings
12052
12053 @noindent
12054 ^Switch^Switch^ settings for a project-aware tool can be specified through
12055 attributes in the package that corresponds to the tool.
12056 The example above illustrates one of the relevant attributes,
12057 @code{^Default_Switches^Default_Switches^}, which is defined in packages
12058 in both project files.
12059 Unlike simple attributes like @code{Source_Dirs},
12060 @code{^Default_Switches^Default_Switches^} is
12061 known as an @emph{associative array}. When you define this attribute, you must
12062 supply an ``index'' (a literal string), and the effect of the attribute
12063 definition is to set the value of the array at the specified index.
12064 For the @code{^Default_Switches^Default_Switches^} attribute,
12065 the index is a programming language (in our case, Ada),
12066 and the value specified (after @code{use}) must be a list
12067 of string expressions.
12068
12069 The attributes permitted in project files are restricted to a predefined set.
12070 Some may appear at project level, others in packages.
12071 For any attribute that is an associative array, the index must always be a
12072 literal string, but the restrictions on this string (e.g., a file name or a
12073 language name) depend on the individual attribute.
12074 Also depending on the attribute, its specified value will need to be either a
12075 string or a string list.
12076
12077 In the @code{Debug} project, we set the switches for two tools,
12078 @command{gnatmake} and the compiler, and thus we include the two corresponding
12079 packages; each package defines the @code{^Default_Switches^Default_Switches^}
12080 attribute with index @code{"Ada"}.
12081 Note that the package corresponding to
12082 @command{gnatmake} is named @code{Builder}. The @code{Release} project is
12083 similar, but only includes the @code{Compiler} package.
12084
12085 In project @code{Debug} above, the ^switches^switches^ starting with
12086 @option{-gnat} that are specified in package @code{Compiler}
12087 could have been placed in package @code{Builder}, since @command{gnatmake}
12088 transmits all such ^switches^switches^ to the compiler.
12089
12090 @node Main Subprograms
12091 @unnumberedsubsubsec Main Subprograms
12092
12093 @noindent
12094 One of the specifiable properties of a project is a list of files that contain
12095 main subprograms. This property is captured in the @code{Main} attribute,
12096 whose value is a list of strings. If a project defines the @code{Main}
12097 attribute, it is not necessary to identify the main subprogram(s) when
12098 invoking @command{gnatmake} (@pxref{gnatmake and Project Files}).
12099
12100 @node Executable File Names
12101 @unnumberedsubsubsec Executable File Names
12102
12103 @noindent
12104 By default, the executable file name corresponding to a main source is
12105 deduced from the main source file name. Through the attributes
12106 @code{Executable} and @code{Executable_Suffix} of package @code{Builder},
12107 it is possible to change this default.
12108 In project @code{Debug} above, the executable file name
12109 for main source @file{^proc.adb^PROC.ADB^} is
12110 @file{^proc1^PROC1.EXE^}.
12111 Attribute @code{Executable_Suffix}, when specified, may change the suffix
12112 of the executable files, when no attribute @code{Executable} applies:
12113 its value replace the platform-specific executable suffix.
12114 Attributes @code{Executable} and @code{Executable_Suffix} are the only ways to
12115 specify a non-default executable file name when several mains are built at once
12116 in a single @command{gnatmake} command.
12117
12118 @node Source File Naming Conventions
12119 @unnumberedsubsubsec Source File Naming Conventions
12120
12121 @noindent
12122 Since the project files above do not specify any source file naming
12123 conventions, the GNAT defaults are used. The mechanism for defining source
12124 file naming conventions -- a package named @code{Naming} --
12125 is described below (@pxref{Naming Schemes}).
12126
12127 @node Source Language(s)
12128 @unnumberedsubsubsec Source Language(s)
12129
12130 @noindent
12131 Since the project files do not specify a @code{Languages} attribute, by
12132 default the GNAT tools assume that the language of the project file is Ada.
12133 More generally, a project can comprise source files
12134 in Ada, C, and/or other languages.
12135
12136 @node Using External Variables
12137 @subsection Using External Variables
12138
12139 @noindent
12140 Instead of supplying different project files for debug and release, we can
12141 define a single project file that queries an external variable (set either
12142 on the command line or via an ^environment variable^logical name^) in order to
12143 conditionally define the appropriate settings. Again, assume that the
12144 source files @file{pack.ads}, @file{pack.adb}, and @file{proc.adb} are
12145 located in directory @file{^/common^[COMMON]^}. The following project file,
12146 @file{build.gpr}, queries the external variable named @code{STYLE} and
12147 defines an object directory and ^switch^switch^ settings based on whether
12148 the value is @code{"deb"} (debug) or @code{"rel"} (release), and where
12149 the default is @code{"deb"}.
12150
12151 @smallexample @c projectfile
12152 @group
12153 project Build is
12154 for Main use ("proc");
12155
12156 type Style_Type is ("deb", "rel");
12157 Style : Style_Type := external ("STYLE", "deb");
12158
12159 case Style is
12160 when "deb" =>
12161 for Object_Dir use "debug";
12162
12163 when "rel" =>
12164 for Object_Dir use "release";
12165 for Exec_Dir use ".";
12166 end case;
12167 @end group
12168
12169 @group
12170 package Builder is
12171
12172 case Style is
12173 when "deb" =>
12174 for ^Default_Switches^Default_Switches^ ("Ada")
12175 use ("^-g^-g^");
12176 for Executable ("proc") use "proc1";
12177 when others =>
12178 null;
12179 end case;
12180
12181 end Builder;
12182 @end group
12183
12184 @group
12185 package Compiler is
12186
12187 case Style is
12188 when "deb" =>
12189 for ^Default_Switches^Default_Switches^ ("Ada")
12190 use ("^-gnata^-gnata^",
12191 "^-gnato^-gnato^",
12192 "^-gnatE^-gnatE^");
12193
12194 when "rel" =>
12195 for ^Default_Switches^Default_Switches^ ("Ada")
12196 use ("^-O2^-O2^");
12197 end case;
12198
12199 end Compiler;
12200
12201 end Build;
12202 @end group
12203 @end smallexample
12204
12205 @noindent
12206 @code{Style_Type} is an example of a @emph{string type}, which is the project
12207 file analog of an Ada enumeration type but whose components are string literals
12208 rather than identifiers. @code{Style} is declared as a variable of this type.
12209
12210 The form @code{external("STYLE", "deb")} is known as an
12211 @emph{external reference}; its first argument is the name of an
12212 @emph{external variable}, and the second argument is a default value to be
12213 used if the external variable doesn't exist. You can define an external
12214 variable on the command line via the @option{^-X^/EXTERNAL_REFERENCE^} switch,
12215 or you can use ^an environment variable^a logical name^
12216 as an external variable.
12217
12218 Each @code{case} construct is expanded by the Project Manager based on the
12219 value of @code{Style}. Thus the command
12220 @ifclear vms
12221 @smallexample
12222 gnatmake -P/common/build.gpr -XSTYLE=deb
12223 @end smallexample
12224 @end ifclear
12225
12226 @ifset vms
12227 @smallexample
12228 gnatmake /PROJECT_FILE=[COMMON]BUILD.GPR /EXTERNAL_REFERENCE=STYLE=deb
12229 @end smallexample
12230 @end ifset
12231
12232 @noindent
12233 is equivalent to the @command{gnatmake} invocation using the project file
12234 @file{debug.gpr} in the earlier example. So is the command
12235 @smallexample
12236 gnatmake ^-P/common/build.gpr^/PROJECT_FILE=[COMMON]BUILD.GPR^
12237 @end smallexample
12238
12239 @noindent
12240 since @code{"deb"} is the default for @code{STYLE}.
12241
12242 Analogously,
12243
12244 @ifclear vms
12245 @smallexample
12246 gnatmake -P/common/build.gpr -XSTYLE=rel
12247 @end smallexample
12248 @end ifclear
12249
12250 @ifset vms
12251 @smallexample
12252 GNAT MAKE /PROJECT_FILE=[COMMON]BUILD.GPR /EXTERNAL_REFERENCE=STYLE=rel
12253 @end smallexample
12254 @end ifset
12255
12256 @noindent
12257 is equivalent to the @command{gnatmake} invocation using the project file
12258 @file{release.gpr} in the earlier example.
12259
12260 @node Importing Other Projects
12261 @subsection Importing Other Projects
12262 @cindex @code{ADA_PROJECT_PATH}
12263 @cindex @code{GPR_PROJECT_PATH}
12264
12265 @noindent
12266 A compilation unit in a source file in one project may depend on compilation
12267 units in source files in other projects. To compile this unit under
12268 control of a project file, the
12269 dependent project must @emph{import} the projects containing the needed source
12270 files.
12271 This effect is obtained using syntax similar to an Ada @code{with} clause,
12272 but where @code{with}ed entities are strings that denote project files.
12273
12274 As an example, suppose that the two projects @code{GUI_Proj} and
12275 @code{Comm_Proj} are defined in the project files @file{gui_proj.gpr} and
12276 @file{comm_proj.gpr} in directories @file{^/gui^[GUI]^}
12277 and @file{^/comm^[COMM]^}, respectively.
12278 Suppose that the source files for @code{GUI_Proj} are
12279 @file{gui.ads} and @file{gui.adb}, and that the source files for
12280 @code{Comm_Proj} are @file{comm.ads} and @file{comm.adb}, where each set of
12281 files is located in its respective project file directory. Schematically:
12282
12283 @smallexample
12284 @group
12285 ^/gui^[GUI]^
12286 gui_proj.gpr
12287 gui.ads
12288 gui.adb
12289 @end group
12290
12291 @group
12292 ^/comm^[COMM]^
12293 comm_proj.gpr
12294 comm.ads
12295 comm.adb
12296 @end group
12297 @end smallexample
12298
12299 @noindent
12300 We want to develop an application in directory @file{^/app^[APP]^} that
12301 @code{with} the packages @code{GUI} and @code{Comm}, using the properties of
12302 the corresponding project files (e.g.@: the ^switch^switch^ settings
12303 and object directory).
12304 Skeletal code for a main procedure might be something like the following:
12305
12306 @smallexample @c ada
12307 @group
12308 with GUI, Comm;
12309 procedure App_Main is
12310 @dots{}
12311 begin
12312 @dots{}
12313 end App_Main;
12314 @end group
12315 @end smallexample
12316
12317 @noindent
12318 Here is a project file, @file{app_proj.gpr}, that achieves the desired
12319 effect:
12320
12321 @smallexample @c projectfile
12322 @group
12323 with "/gui/gui_proj", "/comm/comm_proj";
12324 project App_Proj is
12325 for Main use ("app_main");
12326 end App_Proj;
12327 @end group
12328 @end smallexample
12329
12330 @noindent
12331 Building an executable is achieved through the command:
12332 @smallexample
12333 gnatmake ^-P/app/app_proj^/PROJECT_FILE=[APP]APP_PROJ^
12334 @end smallexample
12335 @noindent
12336 which will generate the @code{^app_main^APP_MAIN.EXE^} executable
12337 in the directory where @file{app_proj.gpr} resides.
12338
12339 If an imported project file uses the standard extension (@code{^gpr^GPR^}) then
12340 (as illustrated above) the @code{with} clause can omit the extension.
12341
12342 Our example specified an absolute path for each imported project file.
12343 Alternatively, the directory name of an imported object can be omitted
12344 if either
12345 @itemize @bullet
12346 @item
12347 The imported project file is in the same directory as the importing project
12348 file, or
12349 @item
12350 You have defined one or two ^environment variables^logical names^
12351 that includes the directory containing
12352 the needed project file. The syntax of @code{GPR_PROJECT_PATH} and
12353 @code{ADA_PROJECT_PATH} is the same as
12354 the syntax of @code{ADA_INCLUDE_PATH} and @code{ADA_OBJECTS_PATH}: a list of
12355 directory names separated by colons (semicolons on Windows).
12356 @end itemize
12357
12358 @noindent
12359 Thus, if we define @code{ADA_PROJECT_PATH} or @code{GPR_PROJECT_PATH}
12360 to include @file{^/gui^[GUI]^} and
12361 @file{^/comm^[COMM]^}, then our project file @file{app_proj.gpr} can be written
12362 as follows:
12363
12364 @smallexample @c projectfile
12365 @group
12366 with "gui_proj", "comm_proj";
12367 project App_Proj is
12368 for Main use ("app_main");
12369 end App_Proj;
12370 @end group
12371 @end smallexample
12372
12373 @noindent
12374 Importing other projects can create ambiguities.
12375 For example, the same unit might be present in different imported projects, or
12376 it might be present in both the importing project and in an imported project.
12377 Both of these conditions are errors. Note that in the current version of
12378 the Project Manager, it is illegal to have an ambiguous unit even if the
12379 unit is never referenced by the importing project. This restriction may be
12380 relaxed in a future release.
12381
12382 @node Extending a Project
12383 @subsection Extending a Project
12384
12385 @noindent
12386 In large software systems it is common to have multiple
12387 implementations of a common interface; in Ada terms, multiple versions of a
12388 package body for the same spec. For example, one implementation
12389 might be safe for use in tasking programs, while another might only be used
12390 in sequential applications. This can be modeled in GNAT using the concept
12391 of @emph{project extension}. If one project (the ``child'') @emph{extends}
12392 another project (the ``parent'') then by default all source files of the
12393 parent project are inherited by the child, but the child project can
12394 override any of the parent's source files with new versions, and can also
12395 add new files. This facility is the project analog of a type extension in
12396 Object-Oriented Programming. Project hierarchies are permitted (a child
12397 project may be the parent of yet another project), and a project that
12398 inherits one project can also import other projects.
12399
12400 As an example, suppose that directory @file{^/seq^[SEQ]^} contains the project
12401 file @file{seq_proj.gpr} as well as the source files @file{pack.ads},
12402 @file{pack.adb}, and @file{proc.adb}:
12403
12404 @smallexample
12405 @group
12406 ^/seq^[SEQ]^
12407 pack.ads
12408 pack.adb
12409 proc.adb
12410 seq_proj.gpr
12411 @end group
12412 @end smallexample
12413
12414 @noindent
12415 Note that the project file can simply be empty (that is, no attribute or
12416 package is defined):
12417
12418 @smallexample @c projectfile
12419 @group
12420 project Seq_Proj is
12421 end Seq_Proj;
12422 @end group
12423 @end smallexample
12424
12425 @noindent
12426 implying that its source files are all the Ada source files in the project
12427 directory.
12428
12429 Suppose we want to supply an alternate version of @file{pack.adb}, in
12430 directory @file{^/tasking^[TASKING]^}, but use the existing versions of
12431 @file{pack.ads} and @file{proc.adb}. We can define a project
12432 @code{Tasking_Proj} that inherits @code{Seq_Proj}:
12433
12434 @smallexample
12435 @group
12436 ^/tasking^[TASKING]^
12437 pack.adb
12438 tasking_proj.gpr
12439 @end group
12440
12441 @group
12442 project Tasking_Proj extends "/seq/seq_proj" is
12443 end Tasking_Proj;
12444 @end group
12445 @end smallexample
12446
12447 @noindent
12448 The version of @file{pack.adb} used in a build depends on which project file
12449 is specified.
12450
12451 Note that we could have obtained the desired behavior using project import
12452 rather than project inheritance; a @code{base} project would contain the
12453 sources for @file{pack.ads} and @file{proc.adb}, a sequential project would
12454 import @code{base} and add @file{pack.adb}, and likewise a tasking project
12455 would import @code{base} and add a different version of @file{pack.adb}. The
12456 choice depends on whether other sources in the original project need to be
12457 overridden. If they do, then project extension is necessary, otherwise,
12458 importing is sufficient.
12459
12460 @noindent
12461 In a project file that extends another project file, it is possible to
12462 indicate that an inherited source is not part of the sources of the extending
12463 project. This is necessary sometimes when a package spec has been overloaded
12464 and no longer requires a body: in this case, it is necessary to indicate that
12465 the inherited body is not part of the sources of the project, otherwise there
12466 will be a compilation error when compiling the spec.
12467
12468 For that purpose, the attribute @code{Excluded_Source_Files} is used.
12469 Its value is a string list: a list of file names. It is also possible to use
12470 attribute @code{Excluded_Source_List_File}. Its value is a single string:
12471 the file name of a text file containing a list of file names, one per line.
12472
12473 @smallexample @c @projectfile
12474 project B extends "a" is
12475 for Source_Files use ("pkg.ads");
12476 -- New spec of Pkg does not need a completion
12477 for Excluded_Source_Files use ("pkg.adb");
12478 end B;
12479 @end smallexample
12480
12481 Attribute @code{Excluded_Source_Files} may also be used to check if a source
12482 is still needed: if it is possible to build using @command{gnatmake} when such
12483 a source is put in attribute @code{Excluded_Source_Files} of a project P, then
12484 it is possible to remove the source completely from a system that includes
12485 project P.
12486
12487 @c ***********************
12488 @c * Project File Syntax *
12489 @c ***********************
12490
12491 @node Project File Syntax
12492 @section Project File Syntax
12493
12494 @menu
12495 * Basic Syntax::
12496 * Qualified Projects::
12497 * Packages::
12498 * Expressions::
12499 * String Types::
12500 * Variables::
12501 * Attributes::
12502 * Associative Array Attributes::
12503 * case Constructions::
12504 @end menu
12505
12506 @noindent
12507 This section describes the structure of project files.
12508
12509 A project may be an @emph{independent project}, entirely defined by a single
12510 project file. Any Ada source file in an independent project depends only
12511 on the predefined library and other Ada source files in the same project.
12512
12513 @noindent
12514 A project may also @dfn{depend on} other projects, in either or both of
12515 the following ways:
12516 @itemize @bullet
12517 @item It may import any number of projects
12518 @item It may extend at most one other project
12519 @end itemize
12520
12521 @noindent
12522 The dependence relation is a directed acyclic graph (the subgraph reflecting
12523 the ``extends'' relation is a tree).
12524
12525 A project's @dfn{immediate sources} are the source files directly defined by
12526 that project, either implicitly by residing in the project file's directory,
12527 or explicitly through any of the source-related attributes described below.
12528 More generally, a project @var{proj}'s @dfn{sources} are the immediate sources
12529 of @var{proj} together with the immediate sources (unless overridden) of any
12530 project on which @var{proj} depends (either directly or indirectly).
12531
12532 @node Basic Syntax
12533 @subsection Basic Syntax
12534
12535 @noindent
12536 As seen in the earlier examples, project files have an Ada-like syntax.
12537 The minimal project file is:
12538 @smallexample @c projectfile
12539 @group
12540 project Empty is
12541
12542 end Empty;
12543 @end group
12544 @end smallexample
12545
12546 @noindent
12547 The identifier @code{Empty} is the name of the project.
12548 This project name must be present after the reserved
12549 word @code{end} at the end of the project file, followed by a semi-colon.
12550
12551 Any name in a project file, such as the project name or a variable name,
12552 has the same syntax as an Ada identifier.
12553
12554 The reserved words of project files are the Ada 95 reserved words plus
12555 @code{extends}, @code{external}, and @code{project}. Note that the only Ada
12556 reserved words currently used in project file syntax are:
12557
12558 @itemize @bullet
12559 @item
12560 @code{all}
12561 @item
12562 @code{at}
12563 @item
12564 @code{case}
12565 @item
12566 @code{end}
12567 @item
12568 @code{for}
12569 @item
12570 @code{is}
12571 @item
12572 @code{limited}
12573 @item
12574 @code{null}
12575 @item
12576 @code{others}
12577 @item
12578 @code{package}
12579 @item
12580 @code{renames}
12581 @item
12582 @code{type}
12583 @item
12584 @code{use}
12585 @item
12586 @code{when}
12587 @item
12588 @code{with}
12589 @end itemize
12590
12591 @noindent
12592 Comments in project files have the same syntax as in Ada, two consecutive
12593 hyphens through the end of the line.
12594
12595 @node Qualified Projects
12596 @subsection Qualified Projects
12597
12598 @noindent
12599 Before the reserved @code{project}, there may be one or two "qualifiers", that
12600 is identifiers or other reserved words, to qualify the project.
12601
12602 The current list of qualifiers is:
12603
12604 @itemize @bullet
12605 @item
12606 @code{abstract}: qualify a project with no sources. A qualified abstract
12607 project must either have no declaration of attributes @code{Source_Dirs},
12608 @code{Source_Files}, @code{Languages} or @code{Source_List_File}, or one of
12609 @code{Source_Dirs}, @code{Source_Files}, or @code{Languages} must be declared
12610 as empty. If it extends another project, the project it extends must also be a
12611 qualified abstract project.
12612
12613 @item
12614 @code{standard}: a standard project is a non library project with sources.
12615
12616 @item
12617 @code{aggregate}: for future extension
12618
12619 @item
12620 @code{aggregate library}: for future extension
12621
12622 @item
12623 @code{library}: a library project must declare both attributes
12624 @code{Library_Name} and @code{Library_Dir}.
12625
12626 @item
12627 @code{configuration}: a configuration project cannot be in a project tree.
12628 @end itemize
12629
12630 @node Packages
12631 @subsection Packages
12632
12633 @noindent
12634 A project file may contain @emph{packages}. The name of a package must be one
12635 of the identifiers from the following list. A package
12636 with a given name may only appear once in a project file. Package names are
12637 case insensitive. The following package names are legal:
12638
12639 @itemize @bullet
12640 @item
12641 @code{Naming}
12642 @item
12643 @code{Builder}
12644 @item
12645 @code{Compiler}
12646 @item
12647 @code{Binder}
12648 @item
12649 @code{Linker}
12650 @item
12651 @code{Finder}
12652 @item
12653 @code{Cross_Reference}
12654 @item
12655 @code{Eliminate}
12656 @item
12657 @code{Pretty_Printer}
12658 @item
12659 @code{Metrics}
12660 @item
12661 @code{gnatls}
12662 @item
12663 @code{gnatstub}
12664 @item
12665 @code{IDE}
12666 @item
12667 @code{Language_Processing}
12668 @end itemize
12669
12670 @noindent
12671 In its simplest form, a package may be empty:
12672
12673 @smallexample @c projectfile
12674 @group
12675 project Simple is
12676 package Builder is
12677 end Builder;
12678 end Simple;
12679 @end group
12680 @end smallexample
12681
12682 @noindent
12683 A package may contain @emph{attribute declarations},
12684 @emph{variable declarations} and @emph{case constructions}, as will be
12685 described below.
12686
12687 When there is ambiguity between a project name and a package name,
12688 the name always designates the project. To avoid possible confusion, it is
12689 always a good idea to avoid naming a project with one of the
12690 names allowed for packages or any name that starts with @code{gnat}.
12691
12692 @node Expressions
12693 @subsection Expressions
12694
12695 @noindent
12696 An @emph{expression} is either a @emph{string expression} or a
12697 @emph{string list expression}.
12698
12699 A @emph{string expression} is either a @emph{simple string expression} or a
12700 @emph{compound string expression}.
12701
12702 A @emph{simple string expression} is one of the following:
12703 @itemize @bullet
12704 @item A literal string; e.g.@: @code{"comm/my_proj.gpr"}
12705 @item A string-valued variable reference (@pxref{Variables})
12706 @item A string-valued attribute reference (@pxref{Attributes})
12707 @item An external reference (@pxref{External References in Project Files})
12708 @end itemize
12709
12710 @noindent
12711 A @emph{compound string expression} is a concatenation of string expressions,
12712 using the operator @code{"&"}
12713 @smallexample
12714 Path & "/" & File_Name & ".ads"
12715 @end smallexample
12716
12717 @noindent
12718 A @emph{string list expression} is either a
12719 @emph{simple string list expression} or a
12720 @emph{compound string list expression}.
12721
12722 A @emph{simple string list expression} is one of the following:
12723 @itemize @bullet
12724 @item A parenthesized list of zero or more string expressions,
12725 separated by commas
12726 @smallexample
12727 File_Names := (File_Name, "gnat.adc", File_Name & ".orig");
12728 Empty_List := ();
12729 @end smallexample
12730 @item A string list-valued variable reference
12731 @item A string list-valued attribute reference
12732 @end itemize
12733
12734 @noindent
12735 A @emph{compound string list expression} is the concatenation (using
12736 @code{"&"}) of a simple string list expression and an expression. Note that
12737 each term in a compound string list expression, except the first, may be
12738 either a string expression or a string list expression.
12739
12740 @smallexample @c projectfile
12741 @group
12742 File_Name_List := () & File_Name; -- One string in this list
12743 Extended_File_Name_List := File_Name_List & (File_Name & ".orig");
12744 -- Two strings
12745 Big_List := File_Name_List & Extended_File_Name_List;
12746 -- Concatenation of two string lists: three strings
12747 Illegal_List := "gnat.adc" & Extended_File_Name_List;
12748 -- Illegal: must start with a string list
12749 @end group
12750 @end smallexample
12751
12752 @node String Types
12753 @subsection String Types
12754
12755 @noindent
12756 A @emph{string type declaration} introduces a discrete set of string literals.
12757 If a string variable is declared to have this type, its value
12758 is restricted to the given set of literals.
12759
12760 Here is an example of a string type declaration:
12761
12762 @smallexample @c projectfile
12763 type OS is ("NT", "nt", "Unix", "GNU/Linux", "other OS");
12764 @end smallexample
12765
12766 @noindent
12767 Variables of a string type are called @emph{typed variables}; all other
12768 variables are called @emph{untyped variables}. Typed variables are
12769 particularly useful in @code{case} constructions, to support conditional
12770 attribute declarations.
12771 (@pxref{case Constructions}).
12772
12773 The string literals in the list are case sensitive and must all be different.
12774 They may include any graphic characters allowed in Ada, including spaces.
12775
12776 A string type may only be declared at the project level, not inside a package.
12777
12778 A string type may be referenced by its name if it has been declared in the same
12779 project file, or by an expanded name whose prefix is the name of the project
12780 in which it is declared.
12781
12782 @node Variables
12783 @subsection Variables
12784
12785 @noindent
12786 A variable may be declared at the project file level, or within a package.
12787 Here are some examples of variable declarations:
12788
12789 @smallexample @c projectfile
12790 @group
12791 This_OS : OS := external ("OS"); -- a typed variable declaration
12792 That_OS := "GNU/Linux"; -- an untyped variable declaration
12793 @end group
12794 @end smallexample
12795
12796 @noindent
12797 The syntax of a @emph{typed variable declaration} is identical to the Ada
12798 syntax for an object declaration. By contrast, the syntax of an untyped
12799 variable declaration is identical to an Ada assignment statement. In fact,
12800 variable declarations in project files have some of the characteristics of
12801 an assignment, in that successive declarations for the same variable are
12802 allowed. Untyped variable declarations do establish the expected kind of the
12803 variable (string or string list), and successive declarations for it must
12804 respect the initial kind.
12805
12806 @noindent
12807 A string variable declaration (typed or untyped) declares a variable
12808 whose value is a string. This variable may be used as a string expression.
12809 @smallexample @c projectfile
12810 File_Name := "readme.txt";
12811 Saved_File_Name := File_Name & ".saved";
12812 @end smallexample
12813
12814 @noindent
12815 A string list variable declaration declares a variable whose value is a list
12816 of strings. The list may contain any number (zero or more) of strings.
12817
12818 @smallexample @c projectfile
12819 Empty_List := ();
12820 List_With_One_Element := ("^-gnaty^-gnaty^");
12821 List_With_Two_Elements := List_With_One_Element & "^-gnatg^-gnatg^";
12822 Long_List := ("main.ada", "pack1_.ada", "pack1.ada", "pack2_.ada"
12823 "pack2.ada", "util_.ada", "util.ada");
12824 @end smallexample
12825
12826 @noindent
12827 The same typed variable may not be declared more than once at project level,
12828 and it may not be declared more than once in any package; it is in effect
12829 a constant.
12830
12831 The same untyped variable may be declared several times. Declarations are
12832 elaborated in the order in which they appear, so the new value replaces
12833 the old one, and any subsequent reference to the variable uses the new value.
12834 However, as noted above, if a variable has been declared as a string, all
12835 subsequent
12836 declarations must give it a string value. Similarly, if a variable has
12837 been declared as a string list, all subsequent declarations
12838 must give it a string list value.
12839
12840 A @emph{variable reference} may take several forms:
12841
12842 @itemize @bullet
12843 @item The simple variable name, for a variable in the current package (if any)
12844 or in the current project
12845 @item An expanded name, whose prefix is a context name.
12846 @end itemize
12847
12848 @noindent
12849 A @emph{context} may be one of the following:
12850
12851 @itemize @bullet
12852 @item The name of an existing package in the current project
12853 @item The name of an imported project of the current project
12854 @item The name of an ancestor project (i.e., a project extended by the current
12855 project, either directly or indirectly)
12856 @item An expanded name whose prefix is an imported/parent project name, and
12857 whose selector is a package name in that project.
12858 @end itemize
12859
12860 @noindent
12861 A variable reference may be used in an expression.
12862
12863 @node Attributes
12864 @subsection Attributes
12865
12866 @noindent
12867 A project (and its packages) may have @emph{attributes} that define
12868 the project's properties. Some attributes have values that are strings;
12869 others have values that are string lists.
12870
12871 There are two categories of attributes: @emph{simple attributes}
12872 and @emph{associative arrays} (@pxref{Associative Array Attributes}).
12873
12874 Legal project attribute names, and attribute names for each legal package are
12875 listed below. Attributes names are case-insensitive.
12876
12877 The following attributes are defined on projects (all are simple attributes):
12878
12879 @multitable @columnfractions .4 .3
12880 @item @emph{Attribute Name}
12881 @tab @emph{Value}
12882 @item @code{Source_Files}
12883 @tab string list
12884 @item @code{Source_Dirs}
12885 @tab string list
12886 @item @code{Source_List_File}
12887 @tab string
12888 @item @code{Object_Dir}
12889 @tab string
12890 @item @code{Exec_Dir}
12891 @tab string
12892 @item @code{Excluded_Source_Dirs}
12893 @tab string list
12894 @item @code{Excluded_Source_Files}
12895 @tab string list
12896 @item @code{Excluded_Source_List_File}
12897 @tab string
12898 @item @code{Languages}
12899 @tab string list
12900 @item @code{Main}
12901 @tab string list
12902 @item @code{Library_Dir}
12903 @tab string
12904 @item @code{Library_Name}
12905 @tab string
12906 @item @code{Library_Kind}
12907 @tab string
12908 @item @code{Library_Version}
12909 @tab string
12910 @item @code{Library_Interface}
12911 @tab string
12912 @item @code{Library_Auto_Init}
12913 @tab string
12914 @item @code{Library_Options}
12915 @tab string list
12916 @item @code{Library_Src_Dir}
12917 @tab string
12918 @item @code{Library_ALI_Dir}
12919 @tab string
12920 @item @code{Library_GCC}
12921 @tab string
12922 @item @code{Library_Symbol_File}
12923 @tab string
12924 @item @code{Library_Symbol_Policy}
12925 @tab string
12926 @item @code{Library_Reference_Symbol_File}
12927 @tab string
12928 @item @code{Externally_Built}
12929 @tab string
12930 @end multitable
12931
12932 @noindent
12933 The following attributes are defined for package @code{Naming}
12934 (@pxref{Naming Schemes}):
12935
12936 @multitable @columnfractions .4 .2 .2 .2
12937 @item Attribute Name @tab Category @tab Index @tab Value
12938 @item @code{Spec_Suffix}
12939 @tab associative array
12940 @tab language name
12941 @tab string
12942 @item @code{Body_Suffix}
12943 @tab associative array
12944 @tab language name
12945 @tab string
12946 @item @code{Separate_Suffix}
12947 @tab simple attribute
12948 @tab n/a
12949 @tab string
12950 @item @code{Casing}
12951 @tab simple attribute
12952 @tab n/a
12953 @tab string
12954 @item @code{Dot_Replacement}
12955 @tab simple attribute
12956 @tab n/a
12957 @tab string
12958 @item @code{Spec}
12959 @tab associative array
12960 @tab Ada unit name
12961 @tab string
12962 @item @code{Body}
12963 @tab associative array
12964 @tab Ada unit name
12965 @tab string
12966 @item @code{Specification_Exceptions}
12967 @tab associative array
12968 @tab language name
12969 @tab string list
12970 @item @code{Implementation_Exceptions}
12971 @tab associative array
12972 @tab language name
12973 @tab string list
12974 @end multitable
12975
12976 @noindent
12977 The following attributes are defined for packages @code{Builder},
12978 @code{Compiler}, @code{Binder},
12979 @code{Linker}, @code{Cross_Reference}, and @code{Finder}
12980 (@pxref{^Switches^Switches^ and Project Files}).
12981
12982 @multitable @columnfractions .4 .2 .2 .2
12983 @item Attribute Name @tab Category @tab Index @tab Value
12984 @item @code{^Default_Switches^Default_Switches^}
12985 @tab associative array
12986 @tab language name
12987 @tab string list
12988 @item @code{^Switches^Switches^}
12989 @tab associative array
12990 @tab file name
12991 @tab string list
12992 @end multitable
12993
12994 @noindent
12995 In addition, package @code{Compiler} has a single string attribute
12996 @code{Local_Configuration_Pragmas} and package @code{Builder} has a single
12997 string attribute @code{Global_Configuration_Pragmas}.
12998
12999 @noindent
13000 Each simple attribute has a default value: the empty string (for string-valued
13001 attributes) and the empty list (for string list-valued attributes).
13002
13003 An attribute declaration defines a new value for an attribute.
13004
13005 Examples of simple attribute declarations:
13006
13007 @smallexample @c projectfile
13008 for Object_Dir use "objects";
13009 for Source_Dirs use ("units", "test/drivers");
13010 @end smallexample
13011
13012 @noindent
13013 The syntax of a @dfn{simple attribute declaration} is similar to that of an
13014 attribute definition clause in Ada.
13015
13016 Attributes references may be appear in expressions.
13017 The general form for such a reference is @code{<entity>'<attribute>}:
13018 Associative array attributes are functions. Associative
13019 array attribute references must have an argument that is a string literal.
13020
13021 Examples are:
13022
13023 @smallexample @c projectfile
13024 project'Object_Dir
13025 Naming'Dot_Replacement
13026 Imported_Project'Source_Dirs
13027 Imported_Project.Naming'Casing
13028 Builder'^Default_Switches^Default_Switches^("Ada")
13029 @end smallexample
13030
13031 @noindent
13032 The prefix of an attribute may be:
13033 @itemize @bullet
13034 @item @code{project} for an attribute of the current project
13035 @item The name of an existing package of the current project
13036 @item The name of an imported project
13037 @item The name of a parent project that is extended by the current project
13038 @item An expanded name whose prefix is imported/parent project name,
13039 and whose selector is a package name
13040 @end itemize
13041
13042 @noindent
13043 Example:
13044 @smallexample @c projectfile
13045 @group
13046 project Prj is
13047 for Source_Dirs use project'Source_Dirs & "units";
13048 for Source_Dirs use project'Source_Dirs & "test/drivers"
13049 end Prj;
13050 @end group
13051 @end smallexample
13052
13053 @noindent
13054 In the first attribute declaration, initially the attribute @code{Source_Dirs}
13055 has the default value: an empty string list. After this declaration,
13056 @code{Source_Dirs} is a string list of one element: @code{"units"}.
13057 After the second attribute declaration @code{Source_Dirs} is a string list of
13058 two elements: @code{"units"} and @code{"test/drivers"}.
13059
13060 Note: this example is for illustration only. In practice,
13061 the project file would contain only one attribute declaration:
13062
13063 @smallexample @c projectfile
13064 for Source_Dirs use ("units", "test/drivers");
13065 @end smallexample
13066
13067 @node Associative Array Attributes
13068 @subsection Associative Array Attributes
13069
13070 @noindent
13071 Some attributes are defined as @emph{associative arrays}. An associative
13072 array may be regarded as a function that takes a string as a parameter
13073 and delivers a string or string list value as its result.
13074
13075 Here are some examples of single associative array attribute associations:
13076
13077 @smallexample @c projectfile
13078 for Body ("main") use "Main.ada";
13079 for ^Switches^Switches^ ("main.ada")
13080 use ("^-v^-v^",
13081 "^-gnatv^-gnatv^");
13082 for ^Switches^Switches^ ("main.ada")
13083 use Builder'^Switches^Switches^ ("main.ada")
13084 & "^-g^-g^";
13085 @end smallexample
13086
13087 @noindent
13088 Like untyped variables and simple attributes, associative array attributes
13089 may be declared several times. Each declaration supplies a new value for the
13090 attribute, and replaces the previous setting.
13091
13092 @noindent
13093 An associative array attribute may be declared as a full associative array
13094 declaration, with the value of the same attribute in an imported or extended
13095 project.
13096
13097 @smallexample @c projectfile
13098 package Builder is
13099 for Default_Switches use Default.Builder'Default_Switches;
13100 end Builder;
13101 @end smallexample
13102
13103 @noindent
13104 In this example, @code{Default} must be either a project imported by the
13105 current project, or the project that the current project extends. If the
13106 attribute is in a package (in this case, in package @code{Builder}), the same
13107 package needs to be specified.
13108
13109 @noindent
13110 A full associative array declaration replaces any other declaration for the
13111 attribute, including other full associative array declaration. Single
13112 associative array associations may be declare after a full associative
13113 declaration, modifying the value for a single association of the attribute.
13114
13115 @node case Constructions
13116 @subsection @code{case} Constructions
13117
13118 @noindent
13119 A @code{case} construction is used in a project file to effect conditional
13120 behavior.
13121 Here is a typical example:
13122
13123 @smallexample @c projectfile
13124 @group
13125 project MyProj is
13126 type OS_Type is ("GNU/Linux", "Unix", "NT", "VMS");
13127
13128 OS : OS_Type := external ("OS", "GNU/Linux");
13129 @end group
13130
13131 @group
13132 package Compiler is
13133 case OS is
13134 when "GNU/Linux" | "Unix" =>
13135 for ^Default_Switches^Default_Switches^ ("Ada")
13136 use ("^-gnath^-gnath^");
13137 when "NT" =>
13138 for ^Default_Switches^Default_Switches^ ("Ada")
13139 use ("^-gnatP^-gnatP^");
13140 when others =>
13141 end case;
13142 end Compiler;
13143 end MyProj;
13144 @end group
13145 @end smallexample
13146
13147 @noindent
13148 The syntax of a @code{case} construction is based on the Ada case statement
13149 (although there is no @code{null} construction for empty alternatives).
13150
13151 The case expression must be a typed string variable.
13152 Each alternative comprises the reserved word @code{when}, either a list of
13153 literal strings separated by the @code{"|"} character or the reserved word
13154 @code{others}, and the @code{"=>"} token.
13155 Each literal string must belong to the string type that is the type of the
13156 case variable.
13157 An @code{others} alternative, if present, must occur last.
13158
13159 After each @code{=>}, there are zero or more constructions. The only
13160 constructions allowed in a case construction are other case constructions,
13161 attribute declarations and variable declarations. String type declarations and
13162 package declarations are not allowed. Variable declarations are restricted to
13163 variables that have already been declared before the case construction.
13164
13165 The value of the case variable is often given by an external reference
13166 (@pxref{External References in Project Files}).
13167
13168 @c ****************************************
13169 @c * Objects and Sources in Project Files *
13170 @c ****************************************
13171
13172 @node Objects and Sources in Project Files
13173 @section Objects and Sources in Project Files
13174
13175 @menu
13176 * Object Directory::
13177 * Exec Directory::
13178 * Source Directories::
13179 * Source File Names::
13180 @end menu
13181
13182 @noindent
13183 Each project has exactly one object directory and one or more source
13184 directories. The source directories must contain at least one source file,
13185 unless the project file explicitly specifies that no source files are present
13186 (@pxref{Source File Names}).
13187
13188 @node Object Directory
13189 @subsection Object Directory
13190
13191 @noindent
13192 The object directory for a project is the directory containing the compiler's
13193 output (such as @file{ALI} files and object files) for the project's immediate
13194 sources.
13195
13196 The object directory is given by the value of the attribute @code{Object_Dir}
13197 in the project file.
13198
13199 @smallexample @c projectfile
13200 for Object_Dir use "objects";
13201 @end smallexample
13202
13203 @noindent
13204 The attribute @code{Object_Dir} has a string value, the path name of the object
13205 directory. The path name may be absolute or relative to the directory of the
13206 project file. This directory must already exist, and be readable and writable.
13207
13208 By default, when the attribute @code{Object_Dir} is not given an explicit value
13209 or when its value is the empty string, the object directory is the same as the
13210 directory containing the project file.
13211
13212 @node Exec Directory
13213 @subsection Exec Directory
13214
13215 @noindent
13216 The exec directory for a project is the directory containing the executables
13217 for the project's main subprograms.
13218
13219 The exec directory is given by the value of the attribute @code{Exec_Dir}
13220 in the project file.
13221
13222 @smallexample @c projectfile
13223 for Exec_Dir use "executables";
13224 @end smallexample
13225
13226 @noindent
13227 The attribute @code{Exec_Dir} has a string value, the path name of the exec
13228 directory. The path name may be absolute or relative to the directory of the
13229 project file. This directory must already exist, and be writable.
13230
13231 By default, when the attribute @code{Exec_Dir} is not given an explicit value
13232 or when its value is the empty string, the exec directory is the same as the
13233 object directory of the project file.
13234
13235 @node Source Directories
13236 @subsection Source Directories
13237
13238 @noindent
13239 The source directories of a project are specified by the project file
13240 attribute @code{Source_Dirs}.
13241
13242 This attribute's value is a string list. If the attribute is not given an
13243 explicit value, then there is only one source directory, the one where the
13244 project file resides.
13245
13246 A @code{Source_Dirs} attribute that is explicitly defined to be the empty list,
13247 as in
13248
13249 @smallexample @c projectfile
13250 for Source_Dirs use ();
13251 @end smallexample
13252
13253 @noindent
13254 indicates that the project contains no source files.
13255
13256 Otherwise, each string in the string list designates one or more
13257 source directories.
13258
13259 @smallexample @c projectfile
13260 for Source_Dirs use ("sources", "test/drivers");
13261 @end smallexample
13262
13263 @noindent
13264 If a string in the list ends with @code{"/**"}, then the directory whose path
13265 name precedes the two asterisks, as well as all its subdirectories
13266 (recursively), are source directories.
13267
13268 @smallexample @c projectfile
13269 for Source_Dirs use ("/system/sources/**");
13270 @end smallexample
13271
13272 @noindent
13273 Here the directory @code{/system/sources} and all of its subdirectories
13274 (recursively) are source directories.
13275
13276 To specify that the source directories are the directory of the project file
13277 and all of its subdirectories, you can declare @code{Source_Dirs} as follows:
13278 @smallexample @c projectfile
13279 for Source_Dirs use ("./**");
13280 @end smallexample
13281
13282 @noindent
13283 Each of the source directories must exist and be readable.
13284
13285 @node Source File Names
13286 @subsection Source File Names
13287
13288 @noindent
13289 In a project that contains source files, their names may be specified by the
13290 attributes @code{Source_Files} (a string list) or @code{Source_List_File}
13291 (a string). Source file names never include any directory information.
13292
13293 If the attribute @code{Source_Files} is given an explicit value, then each
13294 element of the list is a source file name.
13295
13296 @smallexample @c projectfile
13297 for Source_Files use ("main.adb");
13298 for Source_Files use ("main.adb", "pack1.ads", "pack2.adb");
13299 @end smallexample
13300
13301 @noindent
13302 If the attribute @code{Source_Files} is not given an explicit value,
13303 but the attribute @code{Source_List_File} is given a string value,
13304 then the source file names are contained in the text file whose path name
13305 (absolute or relative to the directory of the project file) is the
13306 value of the attribute @code{Source_List_File}.
13307
13308 Each line in the file that is not empty or is not a comment
13309 contains a source file name.
13310
13311 @smallexample @c projectfile
13312 for Source_List_File use "source_list.txt";
13313 @end smallexample
13314
13315 @noindent
13316 By default, if neither the attribute @code{Source_Files} nor the attribute
13317 @code{Source_List_File} is given an explicit value, then each file in the
13318 source directories that conforms to the project's naming scheme
13319 (@pxref{Naming Schemes}) is an immediate source of the project.
13320
13321 A warning is issued if both attributes @code{Source_Files} and
13322 @code{Source_List_File} are given explicit values. In this case, the attribute
13323 @code{Source_Files} prevails.
13324
13325 Each source file name must be the name of one existing source file
13326 in one of the source directories.
13327
13328 A @code{Source_Files} attribute whose value is an empty list
13329 indicates that there are no source files in the project.
13330
13331 If the order of the source directories is known statically, that is if
13332 @code{"/**"} is not used in the string list @code{Source_Dirs}, then there may
13333 be several files with the same source file name. In this case, only the file
13334 in the first directory is considered as an immediate source of the project
13335 file. If the order of the source directories is not known statically, it is
13336 an error to have several files with the same source file name.
13337
13338 Projects can be specified to have no Ada source
13339 files: the value of @code{Source_Dirs} or @code{Source_Files} may be an empty
13340 list, or the @code{"Ada"} may be absent from @code{Languages}:
13341
13342 @smallexample @c projectfile
13343 for Source_Dirs use ();
13344 for Source_Files use ();
13345 for Languages use ("C", "C++");
13346 @end smallexample
13347
13348 @noindent
13349 Otherwise, a project must contain at least one immediate source.
13350
13351 Projects with no source files are useful as template packages
13352 (@pxref{Packages in Project Files}) for other projects; in particular to
13353 define a package @code{Naming} (@pxref{Naming Schemes}).
13354
13355 @c ****************************
13356 @c * Importing Projects *
13357 @c ****************************
13358
13359 @node Importing Projects
13360 @section Importing Projects
13361 @cindex @code{ADA_PROJECT_PATH}
13362 @cindex @code{GPR_PROJECT_PATH}
13363
13364 @noindent
13365 An immediate source of a project P may depend on source files that
13366 are neither immediate sources of P nor in the predefined library.
13367 To get this effect, P must @emph{import} the projects that contain the needed
13368 source files.
13369
13370 @smallexample @c projectfile
13371 @group
13372 with "project1", "utilities.gpr";
13373 with "/namings/apex.gpr";
13374 project Main is
13375 @dots{}
13376 @end group
13377 @end smallexample
13378
13379 @noindent
13380 As can be seen in this example, the syntax for importing projects is similar
13381 to the syntax for importing compilation units in Ada. However, project files
13382 use literal strings instead of names, and the @code{with} clause identifies
13383 project files rather than packages.
13384
13385 Each literal string is the file name or path name (absolute or relative) of a
13386 project file. If a string corresponds to a file name, with no path or a
13387 relative path, then its location is determined by the @emph{project path}. The
13388 latter can be queried using @code{gnatls -v}. It contains:
13389
13390 @itemize @bullet
13391 @item
13392 In first position, the directory containing the current project file.
13393 @item
13394 In last position, the default project directory. This default project directory
13395 is part of the GNAT installation and is the standard place to install project
13396 files giving access to standard support libraries.
13397 @ifclear vms
13398 @ref{Installing a library}
13399 @end ifclear
13400
13401 @item
13402 In between, all the directories referenced in the
13403 ^environment variables^logical names^ @env{GPR_PROJECT_PATH}
13404 and @env{ADA_PROJECT_PATH} if they exist, and in that order.
13405 @end itemize
13406
13407 @noindent
13408 If a relative pathname is used, as in
13409
13410 @smallexample @c projectfile
13411 with "tests/proj";
13412 @end smallexample
13413
13414 @noindent
13415 then the full path for the project is constructed by concatenating this
13416 relative path to those in the project path, in order, until a matching file is
13417 found. Any symbolic link will be fully resolved in the directory of the
13418 importing project file before the imported project file is examined.
13419
13420 If the @code{with}'ed project file name does not have an extension,
13421 the default is @file{^.gpr^.GPR^}. If a file with this extension is not found,
13422 then the file name as specified in the @code{with} clause (no extension) will
13423 be used. In the above example, if a file @code{project1.gpr} is found, then it
13424 will be used; otherwise, if a file @code{^project1^PROJECT1^} exists
13425 then it will be used; if neither file exists, this is an error.
13426
13427 A warning is issued if the name of the project file does not match the
13428 name of the project; this check is case insensitive.
13429
13430 Any source file that is an immediate source of the imported project can be
13431 used by the immediate sources of the importing project, transitively. Thus
13432 if @code{A} imports @code{B}, and @code{B} imports @code{C}, the immediate
13433 sources of @code{A} may depend on the immediate sources of @code{C}, even if
13434 @code{A} does not import @code{C} explicitly. However, this is not recommended,
13435 because if and when @code{B} ceases to import @code{C}, some sources in
13436 @code{A} will no longer compile.
13437
13438 A side effect of this capability is that normally cyclic dependencies are not
13439 permitted: if @code{A} imports @code{B} (directly or indirectly) then @code{B}
13440 is not allowed to import @code{A}. However, there are cases when cyclic
13441 dependencies would be beneficial. For these cases, another form of import
13442 between projects exists, the @code{limited with}: a project @code{A} that
13443 imports a project @code{B} with a straight @code{with} may also be imported,
13444 directly or indirectly, by @code{B} on the condition that imports from @code{B}
13445 to @code{A} include at least one @code{limited with}.
13446
13447 @smallexample @c 0projectfile
13448 with "../b/b.gpr";
13449 with "../c/c.gpr";
13450 project A is
13451 end A;
13452
13453 limited with "../a/a.gpr";
13454 project B is
13455 end B;
13456
13457 with "../d/d.gpr";
13458 project C is
13459 end C;
13460
13461 limited with "../a/a.gpr";
13462 project D is
13463 end D;
13464 @end smallexample
13465
13466 @noindent
13467 In the above legal example, there are two project cycles:
13468 @itemize @bullet
13469 @item A-> B-> A
13470 @item A -> C -> D -> A
13471 @end itemize
13472
13473 @noindent
13474 In each of these cycle there is one @code{limited with}: import of @code{A}
13475 from @code{B} and import of @code{A} from @code{D}.
13476
13477 The difference between straight @code{with} and @code{limited with} is that
13478 the name of a project imported with a @code{limited with} cannot be used in the
13479 project that imports it. In particular, its packages cannot be renamed and
13480 its variables cannot be referred to.
13481
13482 An exception to the above rules for @code{limited with} is that for the main
13483 project specified to @command{gnatmake} or to the @command{GNAT} driver a
13484 @code{limited with} is equivalent to a straight @code{with}. For example,
13485 in the example above, projects @code{B} and @code{D} could not be main
13486 projects for @command{gnatmake} or to the @command{GNAT} driver, because they
13487 each have a @code{limited with} that is the only one in a cycle of importing
13488 projects.
13489
13490 @c *********************
13491 @c * Project Extension *
13492 @c *********************
13493
13494 @node Project Extension
13495 @section Project Extension
13496
13497 @noindent
13498 During development of a large system, it is sometimes necessary to use
13499 modified versions of some of the source files, without changing the original
13500 sources. This can be achieved through the @emph{project extension} facility.
13501
13502 @smallexample @c projectfile
13503 project Modified_Utilities extends "/baseline/utilities.gpr" is @dots{}
13504 @end smallexample
13505
13506 @noindent
13507 A project extension declaration introduces an extending project
13508 (the @emph{child}) and a project being extended (the @emph{parent}).
13509
13510 By default, a child project inherits all the sources of its parent.
13511 However, inherited sources can be overridden: a unit in a parent is hidden
13512 by a unit of the same name in the child.
13513
13514 Inherited sources are considered to be sources (but not immediate sources)
13515 of the child project; see @ref{Project File Syntax}.
13516
13517 An inherited source file retains any switches specified in the parent project.
13518
13519 For example if the project @code{Utilities} contains the spec and the
13520 body of an Ada package @code{Util_IO}, then the project
13521 @code{Modified_Utilities} can contain a new body for package @code{Util_IO}.
13522 The original body of @code{Util_IO} will not be considered in program builds.
13523 However, the package spec will still be found in the project
13524 @code{Utilities}.
13525
13526 A child project can have only one parent, except when it is qualified as
13527 abstract. But it may import any number of other projects.
13528
13529 A project is not allowed to import directly or indirectly at the same time a
13530 child project and any of its ancestors.
13531
13532 @c *******************************
13533 @c * Project Hierarchy Extension *
13534 @c *******************************
13535
13536 @node Project Hierarchy Extension
13537 @section Project Hierarchy Extension
13538
13539 @noindent
13540 When extending a large system spanning multiple projects, it is often
13541 inconvenient to extend every project in the hierarchy that is impacted by a
13542 small change introduced. In such cases, it is possible to create a virtual
13543 extension of entire hierarchy using @code{extends all} relationship.
13544
13545 When the project is extended using @code{extends all} inheritance, all projects
13546 that are imported by it, both directly and indirectly, are considered virtually
13547 extended. That is, the Project Manager creates "virtual projects"
13548 that extend every project in the hierarchy; all these virtual projects have
13549 no sources of their own and have as object directory the object directory of
13550 the root of "extending all" project.
13551
13552 It is possible to explicitly extend one or more projects in the hierarchy
13553 in order to modify the sources. These extending projects must be imported by
13554 the "extending all" project, which will replace the corresponding virtual
13555 projects with the explicit ones.
13556
13557 When building such a project hierarchy extension, the Project Manager will
13558 ensure that both modified sources and sources in virtual extending projects
13559 that depend on them, are recompiled.
13560
13561 By means of example, consider the following hierarchy of projects.
13562
13563 @enumerate
13564 @item
13565 project A, containing package P1
13566 @item
13567 project B importing A and containing package P2 which depends on P1
13568 @item
13569 project C importing B and containing package P3 which depends on P2
13570 @end enumerate
13571
13572 @noindent
13573 We want to modify packages P1 and P3.
13574
13575 This project hierarchy will need to be extended as follows:
13576
13577 @enumerate
13578 @item
13579 Create project A1 that extends A, placing modified P1 there:
13580
13581 @smallexample @c 0projectfile
13582 project A1 extends "(@dots{})/A" is
13583 end A1;
13584 @end smallexample
13585
13586 @item
13587 Create project C1 that "extends all" C and imports A1, placing modified
13588 P3 there:
13589
13590 @smallexample @c 0projectfile
13591 with "(@dots{})/A1";
13592 project C1 extends all "(@dots{})/C" is
13593 end C1;
13594 @end smallexample
13595 @end enumerate
13596
13597 When you build project C1, your entire modified project space will be
13598 recompiled, including the virtual project B1 that has been impacted by the
13599 "extending all" inheritance of project C.
13600
13601 Note that if a Library Project in the hierarchy is virtually extended,
13602 the virtual project that extends the Library Project is not a Library Project.
13603
13604 @c ****************************************
13605 @c * External References in Project Files *
13606 @c ****************************************
13607
13608 @node External References in Project Files
13609 @section External References in Project Files
13610
13611 @noindent
13612 A project file may contain references to external variables; such references
13613 are called @emph{external references}.
13614
13615 An external variable is either defined as part of the environment (an
13616 environment variable in Unix, for example) or else specified on the command
13617 line via the @option{^-X^/EXTERNAL_REFERENCE=^@emph{vbl}=@emph{value}} switch.
13618 If both, then the command line value is used.
13619
13620 The value of an external reference is obtained by means of the built-in
13621 function @code{external}, which returns a string value.
13622 This function has two forms:
13623 @itemize @bullet
13624 @item @code{external (external_variable_name)}
13625 @item @code{external (external_variable_name, default_value)}
13626 @end itemize
13627
13628 @noindent
13629 Each parameter must be a string literal. For example:
13630
13631 @smallexample @c projectfile
13632 external ("USER")
13633 external ("OS", "GNU/Linux")
13634 @end smallexample
13635
13636 @noindent
13637 In the form with one parameter, the function returns the value of
13638 the external variable given as parameter. If this name is not present in the
13639 environment, the function returns an empty string.
13640
13641 In the form with two string parameters, the second argument is
13642 the value returned when the variable given as the first argument is not
13643 present in the environment. In the example above, if @code{"OS"} is not
13644 the name of ^an environment variable^a logical name^ and is not passed on
13645 the command line, then the returned value is @code{"GNU/Linux"}.
13646
13647 An external reference may be part of a string expression or of a string
13648 list expression, and can therefore appear in a variable declaration or
13649 an attribute declaration.
13650
13651 @smallexample @c projectfile
13652 @group
13653 type Mode_Type is ("Debug", "Release");
13654 Mode : Mode_Type := external ("MODE");
13655 case Mode is
13656 when "Debug" =>
13657 @dots{}
13658 @end group
13659 @end smallexample
13660
13661 @c *****************************
13662 @c * Packages in Project Files *
13663 @c *****************************
13664
13665 @node Packages in Project Files
13666 @section Packages in Project Files
13667
13668 @noindent
13669 A @emph{package} defines the settings for project-aware tools within a
13670 project.
13671 For each such tool one can declare a package; the names for these
13672 packages are preset (@pxref{Packages}).
13673 A package may contain variable declarations, attribute declarations, and case
13674 constructions.
13675
13676 @smallexample @c projectfile
13677 @group
13678 project Proj is
13679 package Builder is -- used by gnatmake
13680 for ^Default_Switches^Default_Switches^ ("Ada")
13681 use ("^-v^-v^",
13682 "^-g^-g^");
13683 end Builder;
13684 end Proj;
13685 @end group
13686 @end smallexample
13687
13688 @noindent
13689 The syntax of package declarations mimics that of package in Ada.
13690
13691 Most of the packages have an attribute
13692 @code{^Default_Switches^Default_Switches^}.
13693 This attribute is an associative array, and its value is a string list.
13694 The index of the associative array is the name of a programming language (case
13695 insensitive). This attribute indicates the ^switch^switch^
13696 or ^switches^switches^ to be used
13697 with the corresponding tool.
13698
13699 Some packages also have another attribute, @code{^Switches^Switches^},
13700 an associative array whose value is a string list.
13701 The index is the name of a source file.
13702 This attribute indicates the ^switch^switch^
13703 or ^switches^switches^ to be used by the corresponding
13704 tool when dealing with this specific file.
13705
13706 Further information on these ^switch^switch^-related attributes is found in
13707 @ref{^Switches^Switches^ and Project Files}.
13708
13709 A package may be declared as a @emph{renaming} of another package; e.g., from
13710 the project file for an imported project.
13711
13712 @smallexample @c projectfile
13713 @group
13714 with "/global/apex.gpr";
13715 project Example is
13716 package Naming renames Apex.Naming;
13717 @dots{}
13718 end Example;
13719 @end group
13720 @end smallexample
13721
13722 @noindent
13723 Packages that are renamed in other project files often come from project files
13724 that have no sources: they are just used as templates. Any modification in the
13725 template will be reflected automatically in all the project files that rename
13726 a package from the template.
13727
13728 In addition to the tool-oriented packages, you can also declare a package
13729 named @code{Naming} to establish specialized source file naming conventions
13730 (@pxref{Naming Schemes}).
13731
13732 @c ************************************
13733 @c * Variables from Imported Projects *
13734 @c ************************************
13735
13736 @node Variables from Imported Projects
13737 @section Variables from Imported Projects
13738
13739 @noindent
13740 An attribute or variable defined in an imported or parent project can
13741 be used in expressions in the importing / extending project.
13742 Such an attribute or variable is denoted by an expanded name whose prefix
13743 is either the name of the project or the expanded name of a package within
13744 a project.
13745
13746 @smallexample @c projectfile
13747 @group
13748 with "imported";
13749 project Main extends "base" is
13750 Var1 := Imported.Var;
13751 Var2 := Base.Var & ".new";
13752 @end group
13753
13754 @group
13755 package Builder is
13756 for ^Default_Switches^Default_Switches^ ("Ada")
13757 use Imported.Builder'Ada_^Switches^Switches^ &
13758 "^-gnatg^-gnatg^" &
13759 "^-v^-v^";
13760 end Builder;
13761 @end group
13762
13763 @group
13764 package Compiler is
13765 for ^Default_Switches^Default_Switches^ ("Ada")
13766 use Base.Compiler'Ada_^Switches^Switches^;
13767 end Compiler;
13768 end Main;
13769 @end group
13770 @end smallexample
13771
13772 @noindent
13773 In this example:
13774
13775 @itemize @bullet
13776 @item
13777 The value of @code{Var1} is a copy of the variable @code{Var} defined
13778 in the project file @file{"imported.gpr"}
13779 @item
13780 the value of @code{Var2} is a copy of the value of variable @code{Var}
13781 defined in the project file @file{base.gpr}, concatenated with @code{".new"}
13782 @item
13783 attribute @code{^Default_Switches^Default_Switches^ ("Ada")} in package
13784 @code{Builder} is a string list that includes in its value a copy of the value
13785 of @code{Ada_^Switches^Switches^} defined in the @code{Builder} package
13786 in project file @file{imported.gpr} plus two new elements:
13787 @option{"^-gnatg^-gnatg^"}
13788 and @option{"^-v^-v^"};
13789 @item
13790 attribute @code{^Default_Switches^Default_Switches^ ("Ada")} in package
13791 @code{Compiler} is a copy of the variable @code{Ada_^Switches^Switches^}
13792 defined in the @code{Compiler} package in project file @file{base.gpr},
13793 the project being extended.
13794 @end itemize
13795
13796 @c ******************
13797 @c * Naming Schemes *
13798 @c ******************
13799
13800 @node Naming Schemes
13801 @section Naming Schemes
13802
13803 @noindent
13804 Sometimes an Ada software system is ported from a foreign compilation
13805 environment to GNAT, and the file names do not use the default GNAT
13806 conventions. Instead of changing all the file names (which for a variety
13807 of reasons might not be possible), you can define the relevant file
13808 naming scheme in the @code{Naming} package in your project file.
13809
13810 @noindent
13811 Note that the use of pragmas described in
13812 @ref{Alternative File Naming Schemes} by mean of a configuration
13813 pragmas file is not supported when using project files. You must use
13814 the features described in this paragraph. You can however use specify
13815 other configuration pragmas (@pxref{Specifying Configuration Pragmas}).
13816
13817 @ifclear vms
13818 For example, the following
13819 package models the Apex file naming rules:
13820
13821 @smallexample @c projectfile
13822 @group
13823 package Naming is
13824 for Casing use "lowercase";
13825 for Dot_Replacement use ".";
13826 for Spec_Suffix ("Ada") use ".1.ada";
13827 for Body_Suffix ("Ada") use ".2.ada";
13828 end Naming;
13829 @end group
13830 @end smallexample
13831 @end ifclear
13832
13833 @ifset vms
13834 For example, the following package models the HP Ada file naming rules:
13835
13836 @smallexample @c projectfile
13837 @group
13838 package Naming is
13839 for Casing use "lowercase";
13840 for Dot_Replacement use "__";
13841 for Spec_Suffix ("Ada") use "_.^ada^ada^";
13842 for Body_Suffix ("Ada") use ".^ada^ada^";
13843 end Naming;
13844 @end group
13845 @end smallexample
13846
13847 @noindent
13848 (Note that @code{Casing} is @code{"lowercase"} because GNAT gets the file
13849 names in lower case)
13850 @end ifset
13851
13852 @noindent
13853 You can define the following attributes in package @code{Naming}:
13854
13855 @table @code
13856
13857 @item @code{Casing}
13858 This must be a string with one of the three values @code{"lowercase"},
13859 @code{"uppercase"} or @code{"mixedcase"}; these strings are case insensitive.
13860
13861 @noindent
13862 If @code{Casing} is not specified, then the default is @code{"lowercase"}.
13863
13864 @item @code{Dot_Replacement}
13865 This must be a string whose value satisfies the following conditions:
13866
13867 @itemize @bullet
13868 @item It must not be empty
13869 @item It cannot start or end with an alphanumeric character
13870 @item It cannot be a single underscore
13871 @item It cannot start with an underscore followed by an alphanumeric
13872 @item It cannot contain a dot @code{'.'} except if the entire string
13873 is @code{"."}
13874 @end itemize
13875
13876 @noindent
13877 If @code{Dot_Replacement} is not specified, then the default is @code{"-"}.
13878
13879 @item @code{Spec_Suffix}
13880 This is an associative array (indexed by the programming language name, case
13881 insensitive) whose value is a string that must satisfy the following
13882 conditions:
13883
13884 @itemize @bullet
13885 @item It must not be empty
13886 @item It must include at least one dot
13887 @end itemize
13888 @noindent
13889 If @code{Spec_Suffix ("Ada")} is not specified, then the default is
13890 @code{"^.ads^.ADS^"}.
13891
13892 @item @code{Body_Suffix}
13893 This is an associative array (indexed by the programming language name, case
13894 insensitive) whose value is a string that must satisfy the following
13895 conditions:
13896
13897 @itemize @bullet
13898 @item It must not be empty
13899 @item It must include at least one dot
13900 @item It cannot be the same as @code{Spec_Suffix ("Ada")}
13901 @end itemize
13902 @noindent
13903 If @code{Body_Suffix ("Ada")} and @code{Spec_Suffix ("Ada")} end with the
13904 same string, then a file name that ends with the longest of these two suffixes
13905 will be a body if the longest suffix is @code{Body_Suffix ("Ada")} or a spec
13906 if the longest suffix is @code{Spec_Suffix ("Ada")}.
13907
13908 If the suffix does not start with a '.', a file with a name exactly equal
13909 to the suffix will also be part of the project (for instance if you define
13910 the suffix as @code{Makefile}, a file called @file{Makefile} will be part
13911 of the project. This is not interesting in general when using projects to
13912 compile. However, it might become useful when a project is also used to
13913 find the list of source files in an editor, like the GNAT Programming System
13914 (GPS).
13915
13916 If @code{Body_Suffix ("Ada")} is not specified, then the default is
13917 @code{"^.adb^.ADB^"}.
13918
13919 @item @code{Separate_Suffix}
13920 This must be a string whose value satisfies the same conditions as
13921 @code{Body_Suffix}. The same "longest suffix" rules apply.
13922
13923 @noindent
13924 If @code{Separate_Suffix ("Ada")} is not specified, then it defaults to same
13925 value as @code{Body_Suffix ("Ada")}.
13926
13927 @item @code{Spec}
13928 @noindent
13929 You can use the associative array attribute @code{Spec} to define
13930 the source file name for an individual Ada compilation unit's spec. The array
13931 index must be a string literal that identifies the Ada unit (case insensitive).
13932 The value of this attribute must be a string that identifies the file that
13933 contains this unit's spec (case sensitive or insensitive depending on the
13934 operating system).
13935
13936 @smallexample @c projectfile
13937 for Spec ("MyPack.MyChild") use "mypack.mychild.spec";
13938 @end smallexample
13939
13940 When the source file contains several units, you can indicate at what
13941 position the unit occurs in the file, with the following. The first unit
13942 in the file has index 1
13943
13944 @smallexample @c projectfile
13945 for Body ("top") use "foo.a" at 1;
13946 for Body ("foo") use "foo.a" at 2;
13947 @end smallexample
13948
13949 @item @code{Body}
13950
13951 You can use the associative array attribute @code{Body} to
13952 define the source file name for an individual Ada compilation unit's body
13953 (possibly a subunit). The array index must be a string literal that identifies
13954 the Ada unit (case insensitive). The value of this attribute must be a string
13955 that identifies the file that contains this unit's body or subunit (case
13956 sensitive or insensitive depending on the operating system).
13957
13958 @smallexample @c projectfile
13959 for Body ("MyPack.MyChild") use "mypack.mychild.body";
13960 @end smallexample
13961 @end table
13962
13963 @c ********************
13964 @c * Library Projects *
13965 @c ********************
13966
13967 @node Library Projects
13968 @section Library Projects
13969
13970 @noindent
13971 @emph{Library projects} are projects whose object code is placed in a library.
13972 (Note that this facility is not yet supported on all platforms).
13973
13974 @code{gnatmake} or @code{gprbuild} will collect all object files into a
13975 single archive, which might either be a shared or a static library. This
13976 library can later on be linked with multiple executables, potentially
13977 reducing their sizes.
13978
13979 If your project file specifies languages other than Ada, but you are still
13980 using @code{gnatmake} to compile and link, the latter will not try to
13981 compile your sources other than Ada (you should use @code{gprbuild} if that
13982 is your intent). However, @code{gnatmake} will automatically link all object
13983 files found in the object directory, whether or not they were compiled from
13984 an Ada source file. This specific behavior only applies when multiple
13985 languages are specified.
13986
13987 To create a library project, you need to define in its project file
13988 two project-level attributes: @code{Library_Name} and @code{Library_Dir}.
13989 Additionally, you may define other library-related attributes such as
13990 @code{Library_Kind}, @code{Library_Version}, @code{Library_Interface},
13991 @code{Library_Auto_Init}, @code{Library_Options} and @code{Library_GCC}.
13992
13993 The @code{Library_Name} attribute has a string value. There is no restriction
13994 on the name of a library. It is the responsibility of the developer to
13995 choose a name that will be accepted by the platform. It is recommended to
13996 choose names that could be Ada identifiers; such names are almost guaranteed
13997 to be acceptable on all platforms.
13998
13999 The @code{Library_Dir} attribute has a string value that designates the path
14000 (absolute or relative) of the directory where the library will reside.
14001 It must designate an existing directory, and this directory must be writable,
14002 different from the project's object directory and from any source directory
14003 in the project tree.
14004
14005 If both @code{Library_Name} and @code{Library_Dir} are specified and
14006 are legal, then the project file defines a library project. The optional
14007 library-related attributes are checked only for such project files.
14008
14009 The @code{Library_Kind} attribute has a string value that must be one of the
14010 following (case insensitive): @code{"static"}, @code{"dynamic"} or
14011 @code{"relocatable"} (which is a synonym for @code{"dynamic"}). If this
14012 attribute is not specified, the library is a static library, that is
14013 an archive of object files that can be potentially linked into a
14014 static executable. Otherwise, the library may be dynamic or
14015 relocatable, that is a library that is loaded only at the start of execution.
14016
14017 If you need to build both a static and a dynamic library, you should use two
14018 different object directories, since in some cases some extra code needs to
14019 be generated for the latter. For such cases, it is recommended to either use
14020 two different project files, or a single one which uses external variables
14021 to indicate what kind of library should be build.
14022
14023 The @code{Library_ALI_Dir} attribute may be specified to indicate the
14024 directory where the ALI files of the library will be copied. When it is
14025 not specified, the ALI files are copied to the directory specified in
14026 attribute @code{Library_Dir}. The directory specified by @code{Library_ALI_Dir}
14027 must be writable and different from the project's object directory and from
14028 any source directory in the project tree.
14029
14030 The @code{Library_Version} attribute has a string value whose interpretation
14031 is platform dependent. It has no effect on VMS and Windows. On Unix, it is
14032 used only for dynamic/relocatable libraries as the internal name of the
14033 library (the @code{"soname"}). If the library file name (built from the
14034 @code{Library_Name}) is different from the @code{Library_Version}, then the
14035 library file will be a symbolic link to the actual file whose name will be
14036 @code{Library_Version}.
14037
14038 Example (on Unix):
14039
14040 @smallexample @c projectfile
14041 @group
14042 project Plib is
14043
14044 Version := "1";
14045
14046 for Library_Dir use "lib_dir";
14047 for Library_Name use "dummy";
14048 for Library_Kind use "relocatable";
14049 for Library_Version use "libdummy.so." & Version;
14050
14051 end Plib;
14052 @end group
14053 @end smallexample
14054
14055 @noindent
14056 Directory @file{lib_dir} will contain the internal library file whose name
14057 will be @file{libdummy.so.1}, and @file{libdummy.so} will be a symbolic link to
14058 @file{libdummy.so.1}.
14059
14060 When @command{gnatmake} detects that a project file
14061 is a library project file, it will check all immediate sources of the project
14062 and rebuild the library if any of the sources have been recompiled.
14063
14064 Standard project files can import library project files. In such cases,
14065 the libraries will only be rebuilt if some of its sources are recompiled
14066 because they are in the closure of some other source in an importing project.
14067 Sources of the library project files that are not in such a closure will
14068 not be checked, unless the full library is checked, because one of its sources
14069 needs to be recompiled.
14070
14071 For instance, assume the project file @code{A} imports the library project file
14072 @code{L}. The immediate sources of A are @file{a1.adb}, @file{a2.ads} and
14073 @file{a2.adb}. The immediate sources of L are @file{l1.ads}, @file{l1.adb},
14074 @file{l2.ads}, @file{l2.adb}.
14075
14076 If @file{l1.adb} has been modified, then the library associated with @code{L}
14077 will be rebuilt when compiling all the immediate sources of @code{A} only
14078 if @file{a1.ads}, @file{a2.ads} or @file{a2.adb} includes a statement
14079 @code{"with L1;"}.
14080
14081 To be sure that all the sources in the library associated with @code{L} are
14082 up to date, and that all the sources of project @code{A} are also up to date,
14083 the following two commands needs to be used:
14084
14085 @smallexample
14086 gnatmake -Pl.gpr
14087 gnatmake -Pa.gpr
14088 @end smallexample
14089
14090 When a library is built or rebuilt, an attempt is made first to delete all
14091 files in the library directory.
14092 All @file{ALI} files will also be copied from the object directory to the
14093 library directory. To build executables, @command{gnatmake} will use the
14094 library rather than the individual object files.
14095
14096 @ifclear vms
14097 It is also possible to create library project files for third-party libraries
14098 that are precompiled and cannot be compiled locally thanks to the
14099 @code{externally_built} attribute. (See @ref{Installing a library}).
14100 @end ifclear
14101
14102 @c *******************************
14103 @c * Stand-alone Library Projects *
14104 @c *******************************
14105
14106 @node Stand-alone Library Projects
14107 @section Stand-alone Library Projects
14108
14109 @noindent
14110 A Stand-alone Library is a library that contains the necessary code to
14111 elaborate the Ada units that are included in the library. A Stand-alone
14112 Library is suitable to be used in an executable when the main is not
14113 in Ada. However, Stand-alone Libraries may also be used with an Ada main
14114 subprogram.
14115
14116 A Stand-alone Library Project is a Library Project where the library is
14117 a Stand-alone Library.
14118
14119 To be a Stand-alone Library Project, in addition to the two attributes
14120 that make a project a Library Project (@code{Library_Name} and
14121 @code{Library_Dir}, see @ref{Library Projects}), the attribute
14122 @code{Library_Interface} must be defined.
14123
14124 @smallexample @c projectfile
14125 @group
14126 for Library_Dir use "lib_dir";
14127 for Library_Name use "dummy";
14128 for Library_Interface use ("int1", "int1.child");
14129 @end group
14130 @end smallexample
14131
14132 Attribute @code{Library_Interface} has a nonempty string list value,
14133 each string in the list designating a unit contained in an immediate source
14134 of the project file.
14135
14136 When a Stand-alone Library is built, first the binder is invoked to build
14137 a package whose name depends on the library name
14138 (^b~dummy.ads/b^B$DUMMY.ADS/B^ in the example above).
14139 This binder-generated package includes initialization and
14140 finalization procedures whose
14141 names depend on the library name (dummyinit and dummyfinal in the example
14142 above). The object corresponding to this package is included in the library.
14143
14144 A dynamic or relocatable Stand-alone Library is automatically initialized
14145 if automatic initialization of Stand-alone Libraries is supported on the
14146 platform and if attribute @code{Library_Auto_Init} is not specified or
14147 is specified with the value "true". A static Stand-alone Library is never
14148 automatically initialized.
14149
14150 Single string attribute @code{Library_Auto_Init} may be specified with only
14151 two possible values: "false" or "true" (case-insensitive). Specifying
14152 "false" for attribute @code{Library_Auto_Init} will prevent automatic
14153 initialization of dynamic or relocatable libraries.
14154
14155 When a non-automatically initialized Stand-alone Library is used
14156 in an executable, its initialization procedure must be called before
14157 any service of the library is used.
14158 When the main subprogram is in Ada, it may mean that the initialization
14159 procedure has to be called during elaboration of another package.
14160
14161 For a Stand-Alone Library, only the @file{ALI} files of the Interface Units
14162 (those that are listed in attribute @code{Library_Interface}) are copied to
14163 the Library Directory. As a consequence, only the Interface Units may be
14164 imported from Ada units outside of the library. If other units are imported,
14165 the binding phase will fail.
14166
14167 When a Stand-Alone Library is bound, the switches that are specified in
14168 the attribute @code{Default_Switches ("Ada")} in package @code{Binder} are
14169 used in the call to @command{gnatbind}.
14170
14171 The string list attribute @code{Library_Options} may be used to specified
14172 additional switches to the call to @command{gcc} to link the library.
14173
14174 The attribute @code{Library_Src_Dir}, may be specified for a
14175 Stand-Alone Library. @code{Library_Src_Dir} is a simple attribute that has a
14176 single string value. Its value must be the path (absolute or relative to the
14177 project directory) of an existing directory. This directory cannot be the
14178 object directory or one of the source directories, but it can be the same as
14179 the library directory. The sources of the Interface
14180 Units of the library, necessary to an Ada client of the library, will be
14181 copied to the designated directory, called Interface Copy directory.
14182 These sources includes the specs of the Interface Units, but they may also
14183 include bodies and subunits, when pragmas @code{Inline} or @code{Inline_Always}
14184 are used, or when there is a generic units in the spec. Before the sources
14185 are copied to the Interface Copy directory, an attempt is made to delete all
14186 files in the Interface Copy directory.
14187
14188 @c *************************************
14189 @c * Switches Related to Project Files *
14190 @c *************************************
14191 @node Switches Related to Project Files
14192 @section Switches Related to Project Files
14193
14194 @noindent
14195 The following switches are used by GNAT tools that support project files:
14196
14197 @table @option
14198
14199 @item ^-P^/PROJECT_FILE=^@var{project}
14200 @cindex @option{^-P^/PROJECT_FILE^} (any project-aware tool)
14201 Indicates the name of a project file. This project file will be parsed with
14202 the verbosity indicated by @option{^-vP^MESSAGE_PROJECT_FILES=^@emph{x}},
14203 if any, and using the external references indicated
14204 by @option{^-X^/EXTERNAL_REFERENCE^} switches, if any.
14205 @ifclear vms
14206 There may zero, one or more spaces between @option{-P} and @var{project}.
14207 @end ifclear
14208
14209 @noindent
14210 There must be only one @option{^-P^/PROJECT_FILE^} switch on the command line.
14211
14212 @noindent
14213 Since the Project Manager parses the project file only after all the switches
14214 on the command line are checked, the order of the switches
14215 @option{^-P^/PROJECT_FILE^},
14216 @option{^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}}
14217 or @option{^-X^/EXTERNAL_REFERENCE^} is not significant.
14218
14219 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
14220 @cindex @option{^-X^/EXTERNAL_REFERENCE^} (any project-aware tool)
14221 Indicates that external variable @var{name} has the value @var{value}.
14222 The Project Manager will use this value for occurrences of
14223 @code{external(name)} when parsing the project file.
14224
14225 @ifclear vms
14226 @noindent
14227 If @var{name} or @var{value} includes a space, then @var{name=value} should be
14228 put between quotes.
14229 @smallexample
14230 -XOS=NT
14231 -X"user=John Doe"
14232 @end smallexample
14233 @end ifclear
14234
14235 @noindent
14236 Several @option{^-X^/EXTERNAL_REFERENCE^} switches can be used simultaneously.
14237 If several @option{^-X^/EXTERNAL_REFERENCE^} switches specify the same
14238 @var{name}, only the last one is used.
14239
14240 @noindent
14241 An external variable specified with a @option{^-X^/EXTERNAL_REFERENCE^} switch
14242 takes precedence over the value of the same name in the environment.
14243
14244 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
14245 @cindex @option{^-vP^/MESSAGES_PROJECT_FILE^} (any project-aware tool)
14246 Indicates the verbosity of the parsing of GNAT project files.
14247
14248 @ifclear vms
14249 @option{-vP0} means Default;
14250 @option{-vP1} means Medium;
14251 @option{-vP2} means High.
14252 @end ifclear
14253
14254 @ifset vms
14255 There are three possible options for this qualifier: DEFAULT, MEDIUM and
14256 HIGH.
14257 @end ifset
14258
14259 @noindent
14260 The default is ^Default^DEFAULT^: no output for syntactically correct
14261 project files.
14262 @noindent
14263 If several @option{^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}} switches are present,
14264 only the last one is used.
14265
14266 @item ^-aP^/ADD_PROJECT_SEARCH_DIR=^<dir>
14267 @cindex @option{^-aP^/ADD_PROJECT_SEARCH_DIR=^} (any project-aware tool)
14268 Add directory <dir> at the beginning of the project search path, in order,
14269 after the current working directory.
14270
14271 @ifclear vms
14272 @item -eL
14273 @cindex @option{-eL} (any project-aware tool)
14274 Follow all symbolic links when processing project files.
14275 @end ifclear
14276
14277 @item ^--subdirs^/SUBDIRS^=<subdir>
14278 @cindex @option{^--subdirs^/SUBDIRS^=} (gnatmake and gnatclean)
14279 This switch is recognized by gnatmake and gnatclean. It indicate that the real
14280 directories (except the source directories) are the subdirectories <subdir>
14281 of the directories specified in the project files. This applies in particular
14282 to object directories, library directories and exec directories. If the
14283 subdirectories do not exist, they are created automatically.
14284
14285 @end table
14286
14287 @c **********************************
14288 @c * Tools Supporting Project Files *
14289 @c **********************************
14290
14291 @node Tools Supporting Project Files
14292 @section Tools Supporting Project Files
14293
14294 @menu
14295 * gnatmake and Project Files::
14296 * The GNAT Driver and Project Files::
14297 @end menu
14298
14299 @node gnatmake and Project Files
14300 @subsection gnatmake and Project Files
14301
14302 @noindent
14303 This section covers several topics related to @command{gnatmake} and
14304 project files: defining ^switches^switches^ for @command{gnatmake}
14305 and for the tools that it invokes; specifying configuration pragmas;
14306 the use of the @code{Main} attribute; building and rebuilding library project
14307 files.
14308
14309 @menu
14310 * ^Switches^Switches^ and Project Files::
14311 * Specifying Configuration Pragmas::
14312 * Project Files and Main Subprograms::
14313 * Library Project Files::
14314 @end menu
14315
14316 @node ^Switches^Switches^ and Project Files
14317 @subsubsection ^Switches^Switches^ and Project Files
14318
14319 @ifset vms
14320 It is not currently possible to specify VMS style qualifiers in the project
14321 files; only Unix style ^switches^switches^ may be specified.
14322 @end ifset
14323
14324 @noindent
14325 For each of the packages @code{Builder}, @code{Compiler}, @code{Binder}, and
14326 @code{Linker}, you can specify a @code{^Default_Switches^Default_Switches^}
14327 attribute, a @code{^Switches^Switches^} attribute, or both;
14328 as their names imply, these ^switch^switch^-related
14329 attributes affect the ^switches^switches^ that are used for each of these GNAT
14330 components when
14331 @command{gnatmake} is invoked. As will be explained below, these
14332 component-specific ^switches^switches^ precede
14333 the ^switches^switches^ provided on the @command{gnatmake} command line.
14334
14335 The @code{^Default_Switches^Default_Switches^} attribute is an associative
14336 array indexed by language name (case insensitive) whose value is a string list.
14337 For example:
14338
14339 @smallexample @c projectfile
14340 @group
14341 package Compiler is
14342 for ^Default_Switches^Default_Switches^ ("Ada")
14343 use ("^-gnaty^-gnaty^",
14344 "^-v^-v^");
14345 end Compiler;
14346 @end group
14347 @end smallexample
14348
14349 @noindent
14350 The @code{^Switches^Switches^} attribute is also an associative array,
14351 indexed by a file name (which may or may not be case sensitive, depending
14352 on the operating system) whose value is a string list. For example:
14353
14354 @smallexample @c projectfile
14355 @group
14356 package Builder is
14357 for ^Switches^Switches^ ("main1.adb")
14358 use ("^-O2^-O2^");
14359 for ^Switches^Switches^ ("main2.adb")
14360 use ("^-g^-g^");
14361 end Builder;
14362 @end group
14363 @end smallexample
14364
14365 @noindent
14366 For the @code{Builder} package, the file names must designate source files
14367 for main subprograms. For the @code{Binder} and @code{Linker} packages, the
14368 file names must designate @file{ALI} or source files for main subprograms.
14369 In each case just the file name without an explicit extension is acceptable.
14370
14371 For each tool used in a program build (@command{gnatmake}, the compiler, the
14372 binder, and the linker), the corresponding package @dfn{contributes} a set of
14373 ^switches^switches^ for each file on which the tool is invoked, based on the
14374 ^switch^switch^-related attributes defined in the package.
14375 In particular, the ^switches^switches^
14376 that each of these packages contributes for a given file @var{f} comprise:
14377
14378 @itemize @bullet
14379 @item
14380 the value of attribute @code{^Switches^Switches^ (@var{f})},
14381 if it is specified in the package for the given file,
14382 @item
14383 otherwise, the value of @code{^Default_Switches^Default_Switches^ ("Ada")},
14384 if it is specified in the package.
14385 @end itemize
14386
14387 @noindent
14388 If neither of these attributes is defined in the package, then the package does
14389 not contribute any ^switches^switches^ for the given file.
14390
14391 When @command{gnatmake} is invoked on a file, the ^switches^switches^ comprise
14392 two sets, in the following order: those contributed for the file
14393 by the @code{Builder} package;
14394 and the switches passed on the command line.
14395
14396 When @command{gnatmake} invokes a tool (compiler, binder, linker) on a file,
14397 the ^switches^switches^ passed to the tool comprise three sets,
14398 in the following order:
14399
14400 @enumerate
14401 @item
14402 the applicable ^switches^switches^ contributed for the file
14403 by the @code{Builder} package in the project file supplied on the command line;
14404
14405 @item
14406 those contributed for the file by the package (in the relevant project file --
14407 see below) corresponding to the tool; and
14408
14409 @item
14410 the applicable switches passed on the command line.
14411 @end enumerate
14412
14413 @noindent
14414 The term @emph{applicable ^switches^switches^} reflects the fact that
14415 @command{gnatmake} ^switches^switches^ may or may not be passed to individual
14416 tools, depending on the individual ^switch^switch^.
14417
14418 @command{gnatmake} may invoke the compiler on source files from different
14419 projects. The Project Manager will use the appropriate project file to
14420 determine the @code{Compiler} package for each source file being compiled.
14421 Likewise for the @code{Binder} and @code{Linker} packages.
14422
14423 As an example, consider the following package in a project file:
14424
14425 @smallexample @c projectfile
14426 @group
14427 project Proj1 is
14428 package Compiler is
14429 for ^Default_Switches^Default_Switches^ ("Ada")
14430 use ("^-g^-g^");
14431 for ^Switches^Switches^ ("a.adb")
14432 use ("^-O1^-O1^");
14433 for ^Switches^Switches^ ("b.adb")
14434 use ("^-O2^-O2^",
14435 "^-gnaty^-gnaty^");
14436 end Compiler;
14437 end Proj1;
14438 @end group
14439 @end smallexample
14440
14441 @noindent
14442 If @command{gnatmake} is invoked with this project file, and it needs to
14443 compile, say, the files @file{a.adb}, @file{b.adb}, and @file{c.adb}, then
14444 @file{a.adb} will be compiled with the ^switch^switch^
14445 @option{^-O1^-O1^},
14446 @file{b.adb} with ^switches^switches^
14447 @option{^-O2^-O2^}
14448 and @option{^-gnaty^-gnaty^},
14449 and @file{c.adb} with @option{^-g^-g^}.
14450
14451 The following example illustrates the ordering of the ^switches^switches^
14452 contributed by different packages:
14453
14454 @smallexample @c projectfile
14455 @group
14456 project Proj2 is
14457 package Builder is
14458 for ^Switches^Switches^ ("main.adb")
14459 use ("^-g^-g^",
14460 "^-O1^-)1^",
14461 "^-f^-f^");
14462 end Builder;
14463 @end group
14464
14465 @group
14466 package Compiler is
14467 for ^Switches^Switches^ ("main.adb")
14468 use ("^-O2^-O2^");
14469 end Compiler;
14470 end Proj2;
14471 @end group
14472 @end smallexample
14473
14474 @noindent
14475 If you issue the command:
14476
14477 @smallexample
14478 gnatmake ^-Pproj2^/PROJECT_FILE=PROJ2^ -O0 main
14479 @end smallexample
14480
14481 @noindent
14482 then the compiler will be invoked on @file{main.adb} with the following
14483 sequence of ^switches^switches^
14484
14485 @smallexample
14486 ^-g -O1 -O2 -O0^-g -O1 -O2 -O0^
14487 @end smallexample
14488
14489 with the last @option{^-O^-O^}
14490 ^switch^switch^ having precedence over the earlier ones;
14491 several other ^switches^switches^
14492 (such as @option{^-c^-c^}) are added implicitly.
14493
14494 The ^switches^switches^
14495 @option{^-g^-g^}
14496 and @option{^-O1^-O1^} are contributed by package
14497 @code{Builder}, @option{^-O2^-O2^} is contributed
14498 by the package @code{Compiler}
14499 and @option{^-O0^-O0^} comes from the command line.
14500
14501 The @option{^-g^-g^}
14502 ^switch^switch^ will also be passed in the invocation of
14503 @command{Gnatlink.}
14504
14505 A final example illustrates switch contributions from packages in different
14506 project files:
14507
14508 @smallexample @c projectfile
14509 @group
14510 project Proj3 is
14511 for Source_Files use ("pack.ads", "pack.adb");
14512 package Compiler is
14513 for ^Default_Switches^Default_Switches^ ("Ada")
14514 use ("^-gnata^-gnata^");
14515 end Compiler;
14516 end Proj3;
14517 @end group
14518
14519 @group
14520 with "Proj3";
14521 project Proj4 is
14522 for Source_Files use ("foo_main.adb", "bar_main.adb");
14523 package Builder is
14524 for ^Switches^Switches^ ("foo_main.adb")
14525 use ("^-s^-s^",
14526 "^-g^-g^");
14527 end Builder;
14528 end Proj4;
14529 @end group
14530
14531 @group
14532 -- Ada source file:
14533 with Pack;
14534 procedure Foo_Main is
14535 @dots{}
14536 end Foo_Main;
14537 @end group
14538 @end smallexample
14539
14540 If the command is
14541 @smallexample
14542 gnatmake ^-PProj4^/PROJECT_FILE=PROJ4^ foo_main.adb -cargs -gnato
14543 @end smallexample
14544
14545 @noindent
14546 then the ^switches^switches^ passed to the compiler for @file{foo_main.adb} are
14547 @option{^-g^-g^} (contributed by the package @code{Proj4.Builder}) and
14548 @option{^-gnato^-gnato^} (passed on the command line).
14549 When the imported package @code{Pack} is compiled, the ^switches^switches^ used
14550 are @option{^-g^-g^} from @code{Proj4.Builder},
14551 @option{^-gnata^-gnata^} (contributed from package @code{Proj3.Compiler},
14552 and @option{^-gnato^-gnato^} from the command line.
14553
14554 @noindent
14555 When using @command{gnatmake} with project files, some ^switches^switches^ or
14556 arguments may be expressed as relative paths. As the working directory where
14557 compilation occurs may change, these relative paths are converted to absolute
14558 paths. For the ^switches^switches^ found in a project file, the relative paths
14559 are relative to the project file directory, for the switches on the command
14560 line, they are relative to the directory where @command{gnatmake} is invoked.
14561 The ^switches^switches^ for which this occurs are:
14562 ^-I^-I^,
14563 ^-A^-A^,
14564 ^-L^-L^,
14565 ^-aO^-aO^,
14566 ^-aL^-aL^,
14567 ^-aI^-aI^, as well as all arguments that are not switches (arguments to
14568 ^switch^switch^
14569 ^-o^-o^, object files specified in package @code{Linker} or after
14570 -largs on the command line). The exception to this rule is the ^switch^switch^
14571 ^--RTS=^--RTS=^ for which a relative path argument is never converted.
14572
14573 @node Specifying Configuration Pragmas
14574 @subsubsection Specifying Configuration Pragmas
14575
14576 When using @command{gnatmake} with project files, if there exists a file
14577 @file{gnat.adc} that contains configuration pragmas, this file will be
14578 ignored.
14579
14580 Configuration pragmas can be defined by means of the following attributes in
14581 project files: @code{Global_Configuration_Pragmas} in package @code{Builder}
14582 and @code{Local_Configuration_Pragmas} in package @code{Compiler}.
14583
14584 Both these attributes are single string attributes. Their values is the path
14585 name of a file containing configuration pragmas. If a path name is relative,
14586 then it is relative to the project directory of the project file where the
14587 attribute is defined.
14588
14589 When compiling a source, the configuration pragmas used are, in order,
14590 those listed in the file designated by attribute
14591 @code{Global_Configuration_Pragmas} in package @code{Builder} of the main
14592 project file, if it is specified, and those listed in the file designated by
14593 attribute @code{Local_Configuration_Pragmas} in package @code{Compiler} of
14594 the project file of the source, if it exists.
14595
14596 @node Project Files and Main Subprograms
14597 @subsubsection Project Files and Main Subprograms
14598
14599 @noindent
14600 When using a project file, you can invoke @command{gnatmake}
14601 with one or several main subprograms, by specifying their source files on the
14602 command line.
14603
14604 @smallexample
14605 gnatmake ^-P^/PROJECT_FILE=^prj main1 main2 main3
14606 @end smallexample
14607
14608 @noindent
14609 Each of these needs to be a source file of the same project, except
14610 when the switch ^-u^/UNIQUE^ is used.
14611
14612 @noindent
14613 When ^-u^/UNIQUE^ is not used, all the mains need to be sources of the
14614 same project, one of the project in the tree rooted at the project specified
14615 on the command line. The package @code{Builder} of this common project, the
14616 "main project" is the one that is considered by @command{gnatmake}.
14617
14618 @noindent
14619 When ^-u^/UNIQUE^ is used, the specified source files may be in projects
14620 imported directly or indirectly by the project specified on the command line.
14621 Note that if such a source file is not part of the project specified on the
14622 command line, the ^switches^switches^ found in package @code{Builder} of the
14623 project specified on the command line, if any, that are transmitted
14624 to the compiler will still be used, not those found in the project file of
14625 the source file.
14626
14627 @noindent
14628 When using a project file, you can also invoke @command{gnatmake} without
14629 explicitly specifying any main, and the effect depends on whether you have
14630 defined the @code{Main} attribute. This attribute has a string list value,
14631 where each element in the list is the name of a source file (the file
14632 extension is optional) that contains a unit that can be a main subprogram.
14633
14634 If the @code{Main} attribute is defined in a project file as a non-empty
14635 string list and the switch @option{^-u^/UNIQUE^} is not used on the command
14636 line, then invoking @command{gnatmake} with this project file but without any
14637 main on the command line is equivalent to invoking @command{gnatmake} with all
14638 the file names in the @code{Main} attribute on the command line.
14639
14640 Example:
14641 @smallexample @c projectfile
14642 @group
14643 project Prj is
14644 for Main use ("main1", "main2", "main3");
14645 end Prj;
14646 @end group
14647 @end smallexample
14648
14649 @noindent
14650 With this project file, @code{"gnatmake ^-Pprj^/PROJECT_FILE=PRJ^"}
14651 is equivalent to
14652 @code{"gnatmake ^-Pprj^/PROJECT_FILE=PRJ^ main1 main2 main3"}.
14653
14654 When the project attribute @code{Main} is not specified, or is specified
14655 as an empty string list, or when the switch @option{-u} is used on the command
14656 line, then invoking @command{gnatmake} with no main on the command line will
14657 result in all immediate sources of the project file being checked, and
14658 potentially recompiled. Depending on the presence of the switch @option{-u},
14659 sources from other project files on which the immediate sources of the main
14660 project file depend are also checked and potentially recompiled. In other
14661 words, the @option{-u} switch is applied to all of the immediate sources of the
14662 main project file.
14663
14664 When no main is specified on the command line and attribute @code{Main} exists
14665 and includes several mains, or when several mains are specified on the
14666 command line, the default ^switches^switches^ in package @code{Builder} will
14667 be used for all mains, even if there are specific ^switches^switches^
14668 specified for one or several mains.
14669
14670 But the ^switches^switches^ from package @code{Binder} or @code{Linker} will be
14671 the specific ^switches^switches^ for each main, if they are specified.
14672
14673 @node Library Project Files
14674 @subsubsection Library Project Files
14675
14676 @noindent
14677 When @command{gnatmake} is invoked with a main project file that is a library
14678 project file, it is not allowed to specify one or more mains on the command
14679 line.
14680
14681 @noindent
14682 When a library project file is specified, switches ^-b^/ACTION=BIND^ and
14683 ^-l^/ACTION=LINK^ have special meanings.
14684
14685 @itemize @bullet
14686 @item ^-b^/ACTION=BIND^ is only allowed for stand-alone libraries. It indicates
14687 to @command{gnatmake} that @command{gnatbind} should be invoked for the
14688 library.
14689
14690 @item ^-l^/ACTION=LINK^ may be used for all library projects. It indicates
14691 to @command{gnatmake} that the binder generated file should be compiled
14692 (in the case of a stand-alone library) and that the library should be built.
14693
14694 @end itemize
14695
14696 @node The GNAT Driver and Project Files
14697 @subsection The GNAT Driver and Project Files
14698
14699 @noindent
14700 A number of GNAT tools, other than @command{^gnatmake^gnatmake^}
14701 can benefit from project files:
14702 @command{^gnatbind^gnatbind^},
14703 @command{^gnatcheck^gnatcheck^}),
14704 @command{^gnatclean^gnatclean^}),
14705 @command{^gnatelim^gnatelim^},
14706 @command{^gnatfind^gnatfind^},
14707 @command{^gnatlink^gnatlink^},
14708 @command{^gnatls^gnatls^},
14709 @command{^gnatmetric^gnatmetric^},
14710 @command{^gnatpp^gnatpp^},
14711 @command{^gnatstub^gnatstub^},
14712 and @command{^gnatxref^gnatxref^}. However, none of these tools can be invoked
14713 directly with a project file switch (@option{^-P^/PROJECT_FILE=^}).
14714 They must be invoked through the @command{gnat} driver.
14715
14716 The @command{gnat} driver is a wrapper that accepts a number of commands and
14717 calls the corresponding tool. It was designed initially for VMS platforms (to
14718 convert VMS qualifiers to Unix-style switches), but it is now available on all
14719 GNAT platforms.
14720
14721 On non-VMS platforms, the @command{gnat} driver accepts the following commands
14722 (case insensitive):
14723
14724 @itemize @bullet
14725 @item
14726 BIND to invoke @command{^gnatbind^gnatbind^}
14727 @item
14728 CHOP to invoke @command{^gnatchop^gnatchop^}
14729 @item
14730 CLEAN to invoke @command{^gnatclean^gnatclean^}
14731 @item
14732 COMP or COMPILE to invoke the compiler
14733 @item
14734 ELIM to invoke @command{^gnatelim^gnatelim^}
14735 @item
14736 FIND to invoke @command{^gnatfind^gnatfind^}
14737 @item
14738 KR or KRUNCH to invoke @command{^gnatkr^gnatkr^}
14739 @item
14740 LINK to invoke @command{^gnatlink^gnatlink^}
14741 @item
14742 LS or LIST to invoke @command{^gnatls^gnatls^}
14743 @item
14744 MAKE to invoke @command{^gnatmake^gnatmake^}
14745 @item
14746 NAME to invoke @command{^gnatname^gnatname^}
14747 @item
14748 PREP or PREPROCESS to invoke @command{^gnatprep^gnatprep^}
14749 @item
14750 PP or PRETTY to invoke @command{^gnatpp^gnatpp^}
14751 @item
14752 METRIC to invoke @command{^gnatmetric^gnatmetric^}
14753 @item
14754 STUB to invoke @command{^gnatstub^gnatstub^}
14755 @item
14756 XREF to invoke @command{^gnatxref^gnatxref^}
14757 @end itemize
14758
14759 @noindent
14760 (note that the compiler is invoked using the command
14761 @command{^gnatmake -f -u -c^gnatmake -f -u -c^}).
14762
14763 @noindent
14764 On non-VMS platforms, between @command{gnat} and the command, two
14765 special switches may be used:
14766
14767 @itemize @bullet
14768 @item
14769 @command{-v} to display the invocation of the tool.
14770 @item
14771 @command{-dn} to prevent the @command{gnat} driver from removing
14772 the temporary files it has created. These temporary files are
14773 configuration files and temporary file list files.
14774 @end itemize
14775
14776 @noindent
14777 The command may be followed by switches and arguments for the invoked
14778 tool.
14779
14780 @smallexample
14781 gnat bind -C main.ali
14782 gnat ls -a main
14783 gnat chop foo.txt
14784 @end smallexample
14785
14786 @noindent
14787 Switches may also be put in text files, one switch per line, and the text
14788 files may be specified with their path name preceded by '@@'.
14789
14790 @smallexample
14791 gnat bind @@args.txt main.ali
14792 @end smallexample
14793
14794 @noindent
14795 In addition, for commands BIND, COMP or COMPILE, FIND, ELIM, LS or LIST, LINK,
14796 METRIC, PP or PRETTY, STUB and XREF, the project file related switches
14797 (@option{^-P^/PROJECT_FILE^},
14798 @option{^-X^/EXTERNAL_REFERENCE^} and
14799 @option{^-vP^/MESSAGES_PROJECT_FILE=^x}) may be used in addition to
14800 the switches of the invoking tool.
14801
14802 @noindent
14803 When GNAT PP or GNAT PRETTY is used with a project file, but with no source
14804 specified on the command line, it invokes @command{^gnatpp^gnatpp^} with all
14805 the immediate sources of the specified project file.
14806
14807 @noindent
14808 When GNAT METRIC is used with a project file, but with no source
14809 specified on the command line, it invokes @command{^gnatmetric^gnatmetric^}
14810 with all the immediate sources of the specified project file and with
14811 @option{^-d^/DIRECTORY^} with the parameter pointing to the object directory
14812 of the project.
14813
14814 @noindent
14815 In addition, when GNAT PP, GNAT PRETTY or GNAT METRIC is used with
14816 a project file, no source is specified on the command line and
14817 switch ^-U^/ALL_PROJECTS^ is specified on the command line, then
14818 the underlying tool (^gnatpp^gnatpp^ or
14819 ^gnatmetric^gnatmetric^) is invoked for all sources of all projects,
14820 not only for the immediate sources of the main project.
14821 @ifclear vms
14822 (-U stands for Universal or Union of the project files of the project tree)
14823 @end ifclear
14824
14825 @noindent
14826 For each of the following commands, there is optionally a corresponding
14827 package in the main project.
14828
14829 @itemize @bullet
14830 @item
14831 package @code{Binder} for command BIND (invoking @code{^gnatbind^gnatbind^})
14832
14833 @item
14834 package @code{Check} for command CHECK (invoking
14835 @code{^gnatcheck^gnatcheck^})
14836
14837 @item
14838 package @code{Compiler} for command COMP or COMPILE (invoking the compiler)
14839
14840 @item
14841 package @code{Cross_Reference} for command XREF (invoking
14842 @code{^gnatxref^gnatxref^})
14843
14844 @item
14845 package @code{Eliminate} for command ELIM (invoking
14846 @code{^gnatelim^gnatelim^})
14847
14848 @item
14849 package @code{Finder} for command FIND (invoking @code{^gnatfind^gnatfind^})
14850
14851 @item
14852 package @code{Gnatls} for command LS or LIST (invoking @code{^gnatls^gnatls^})
14853
14854 @item
14855 package @code{Gnatstub} for command STUB
14856 (invoking @code{^gnatstub^gnatstub^})
14857
14858 @item
14859 package @code{Linker} for command LINK (invoking @code{^gnatlink^gnatlink^})
14860
14861 @item
14862 package @code{Metrics} for command METRIC
14863 (invoking @code{^gnatmetric^gnatmetric^})
14864
14865 @item
14866 package @code{Pretty_Printer} for command PP or PRETTY
14867 (invoking @code{^gnatpp^gnatpp^})
14868
14869 @end itemize
14870
14871 @noindent
14872 Package @code{Gnatls} has a unique attribute @code{^Switches^Switches^},
14873 a simple variable with a string list value. It contains ^switches^switches^
14874 for the invocation of @code{^gnatls^gnatls^}.
14875
14876 @smallexample @c projectfile
14877 @group
14878 project Proj1 is
14879 package gnatls is
14880 for ^Switches^Switches^
14881 use ("^-a^-a^",
14882 "^-v^-v^");
14883 end gnatls;
14884 end Proj1;
14885 @end group
14886 @end smallexample
14887
14888 @noindent
14889 All other packages have two attribute @code{^Switches^Switches^} and
14890 @code{^Default_Switches^Default_Switches^}.
14891
14892 @noindent
14893 @code{^Switches^Switches^} is an associative array attribute, indexed by the
14894 source file name, that has a string list value: the ^switches^switches^ to be
14895 used when the tool corresponding to the package is invoked for the specific
14896 source file.
14897
14898 @noindent
14899 @code{^Default_Switches^Default_Switches^} is an associative array attribute,
14900 indexed by the programming language that has a string list value.
14901 @code{^Default_Switches^Default_Switches^ ("Ada")} contains the
14902 ^switches^switches^ for the invocation of the tool corresponding
14903 to the package, except if a specific @code{^Switches^Switches^} attribute
14904 is specified for the source file.
14905
14906 @smallexample @c projectfile
14907 @group
14908 project Proj is
14909
14910 for Source_Dirs use ("./**");
14911
14912 package gnatls is
14913 for ^Switches^Switches^ use
14914 ("^-a^-a^",
14915 "^-v^-v^");
14916 end gnatls;
14917 @end group
14918 @group
14919
14920 package Compiler is
14921 for ^Default_Switches^Default_Switches^ ("Ada")
14922 use ("^-gnatv^-gnatv^",
14923 "^-gnatwa^-gnatwa^");
14924 end Binder;
14925 @end group
14926 @group
14927
14928 package Binder is
14929 for ^Default_Switches^Default_Switches^ ("Ada")
14930 use ("^-C^-C^",
14931 "^-e^-e^");
14932 end Binder;
14933 @end group
14934 @group
14935
14936 package Linker is
14937 for ^Default_Switches^Default_Switches^ ("Ada")
14938 use ("^-C^-C^");
14939 for ^Switches^Switches^ ("main.adb")
14940 use ("^-C^-C^",
14941 "^-v^-v^",
14942 "^-v^-v^");
14943 end Linker;
14944 @end group
14945 @group
14946
14947 package Finder is
14948 for ^Default_Switches^Default_Switches^ ("Ada")
14949 use ("^-a^-a^",
14950 "^-f^-f^");
14951 end Finder;
14952 @end group
14953 @group
14954
14955 package Cross_Reference is
14956 for ^Default_Switches^Default_Switches^ ("Ada")
14957 use ("^-a^-a^",
14958 "^-f^-f^",
14959 "^-d^-d^",
14960 "^-u^-u^");
14961 end Cross_Reference;
14962 end Proj;
14963 @end group
14964 @end smallexample
14965
14966 @noindent
14967 With the above project file, commands such as
14968
14969 @smallexample
14970 ^gnat comp -Pproj main^GNAT COMP /PROJECT_FILE=PROJ MAIN^
14971 ^gnat ls -Pproj main^GNAT LIST /PROJECT_FILE=PROJ MAIN^
14972 ^gnat xref -Pproj main^GNAT XREF /PROJECT_FILE=PROJ MAIN^
14973 ^gnat bind -Pproj main.ali^GNAT BIND /PROJECT_FILE=PROJ MAIN.ALI^
14974 ^gnat link -Pproj main.ali^GNAT LINK /PROJECT_FILE=PROJ MAIN.ALI^
14975 @end smallexample
14976
14977 @noindent
14978 will set up the environment properly and invoke the tool with the switches
14979 found in the package corresponding to the tool:
14980 @code{^Default_Switches^Default_Switches^ ("Ada")} for all tools,
14981 except @code{^Switches^Switches^ ("main.adb")}
14982 for @code{^gnatlink^gnatlink^}.
14983 It is also possible to invoke some of the tools,
14984 @code{^gnatcheck^gnatcheck^}),
14985 @code{^gnatmetric^gnatmetric^}),
14986 and @code{^gnatpp^gnatpp^})
14987 on a set of project units thanks to the combination of the switches
14988 @option{-P}, @option{-U} and possibly the main unit when one is interested
14989 in its closure. For instance,
14990 @smallexample
14991 gnat metric -Pproj
14992 @end smallexample
14993 will compute the metrics for all the immediate units of project
14994 @code{proj}.
14995 @smallexample
14996 gnat metric -Pproj -U
14997 @end smallexample
14998 will compute the metrics for all the units of the closure of projects
14999 rooted at @code{proj}.
15000 @smallexample
15001 gnat metric -Pproj -U main_unit
15002 @end smallexample
15003 will compute the metrics for the closure of units rooted at
15004 @code{main_unit}. This last possibility relies implicitly
15005 on @command{gnatbind}'s option @option{-R}.
15006
15007 @c **********************
15008 @node An Extended Example
15009 @section An Extended Example
15010
15011 @noindent
15012 Suppose that we have two programs, @var{prog1} and @var{prog2},
15013 whose sources are in corresponding directories. We would like
15014 to build them with a single @command{gnatmake} command, and we want to place
15015 their object files into @file{build} subdirectories of the source directories.
15016 Furthermore, we want to have to have two separate subdirectories
15017 in @file{build} -- @file{release} and @file{debug} -- which will contain
15018 the object files compiled with different set of compilation flags.
15019
15020 In other words, we have the following structure:
15021
15022 @smallexample
15023 @group
15024 main
15025 |- prog1
15026 | |- build
15027 | | debug
15028 | | release
15029 |- prog2
15030 |- build
15031 | debug
15032 | release
15033 @end group
15034 @end smallexample
15035
15036 @noindent
15037 Here are the project files that we must place in a directory @file{main}
15038 to maintain this structure:
15039
15040 @enumerate
15041
15042 @item We create a @code{Common} project with a package @code{Compiler} that
15043 specifies the compilation ^switches^switches^:
15044
15045 @smallexample
15046 File "common.gpr":
15047 @group
15048 @b{project} Common @b{is}
15049
15050 @b{for} Source_Dirs @b{use} (); -- No source files
15051 @end group
15052
15053 @group
15054 @b{type} Build_Type @b{is} ("release", "debug");
15055 Build : Build_Type := External ("BUILD", "debug");
15056 @end group
15057 @group
15058 @b{package} Compiler @b{is}
15059 @b{case} Build @b{is}
15060 @b{when} "release" =>
15061 @b{for} ^Default_Switches^Default_Switches^ ("Ada")
15062 @b{use} ("^-O2^-O2^");
15063 @b{when} "debug" =>
15064 @b{for} ^Default_Switches^Default_Switches^ ("Ada")
15065 @b{use} ("^-g^-g^");
15066 @b{end case};
15067 @b{end} Compiler;
15068
15069 @b{end} Common;
15070 @end group
15071 @end smallexample
15072
15073 @item We create separate projects for the two programs:
15074
15075 @smallexample
15076 @group
15077 File "prog1.gpr":
15078
15079 @b{with} "common";
15080 @b{project} Prog1 @b{is}
15081
15082 @b{for} Source_Dirs @b{use} ("prog1");
15083 @b{for} Object_Dir @b{use} "prog1/build/" & Common.Build;
15084
15085 @b{package} Compiler @b{renames} Common.Compiler;
15086
15087 @b{end} Prog1;
15088 @end group
15089 @end smallexample
15090
15091 @smallexample
15092 @group
15093 File "prog2.gpr":
15094
15095 @b{with} "common";
15096 @b{project} Prog2 @b{is}
15097
15098 @b{for} Source_Dirs @b{use} ("prog2");
15099 @b{for} Object_Dir @b{use} "prog2/build/" & Common.Build;
15100
15101 @b{package} Compiler @b{renames} Common.Compiler;
15102
15103 @end group
15104 @b{end} Prog2;
15105 @end smallexample
15106
15107 @item We create a wrapping project @code{Main}:
15108
15109 @smallexample
15110 @group
15111 File "main.gpr":
15112
15113 @b{with} "common";
15114 @b{with} "prog1";
15115 @b{with} "prog2";
15116 @b{project} Main @b{is}
15117
15118 @b{package} Compiler @b{renames} Common.Compiler;
15119
15120 @b{end} Main;
15121 @end group
15122 @end smallexample
15123
15124 @item Finally we need to create a dummy procedure that @code{with}s (either
15125 explicitly or implicitly) all the sources of our two programs.
15126
15127 @end enumerate
15128
15129 @noindent
15130 Now we can build the programs using the command
15131
15132 @smallexample
15133 gnatmake ^-P^/PROJECT_FILE=^main dummy
15134 @end smallexample
15135
15136 @noindent
15137 for the Debug mode, or
15138
15139 @ifclear vms
15140 @smallexample
15141 gnatmake -Pmain -XBUILD=release
15142 @end smallexample
15143 @end ifclear
15144
15145 @ifset vms
15146 @smallexample
15147 GNAT MAKE /PROJECT_FILE=main /EXTERNAL_REFERENCE=BUILD=release
15148 @end smallexample
15149 @end ifset
15150
15151 @noindent
15152 for the Release mode.
15153
15154 @c ********************************
15155 @c * Project File Complete Syntax *
15156 @c ********************************
15157
15158 @node Project File Complete Syntax
15159 @section Project File Complete Syntax
15160
15161 @smallexample
15162 project ::=
15163 context_clause project_declaration
15164
15165 context_clause ::=
15166 @{with_clause@}
15167
15168 with_clause ::=
15169 @b{with} path_name @{ , path_name @} ;
15170
15171 path_name ::=
15172 string_literal
15173
15174 project_declaration ::=
15175 simple_project_declaration | project_extension
15176
15177 simple_project_declaration ::=
15178 @b{project} <project_>simple_name @b{is}
15179 @{declarative_item@}
15180 @b{end} <project_>simple_name;
15181
15182 project_extension ::=
15183 @b{project} <project_>simple_name @b{extends} path_name @b{is}
15184 @{declarative_item@}
15185 @b{end} <project_>simple_name;
15186
15187 declarative_item ::=
15188 package_declaration |
15189 typed_string_declaration |
15190 other_declarative_item
15191
15192 package_declaration ::=
15193 package_spec | package_renaming
15194
15195 package_spec ::=
15196 @b{package} package_identifier @b{is}
15197 @{simple_declarative_item@}
15198 @b{end} package_identifier ;
15199
15200 package_identifier ::=
15201 @code{Naming} | @code{Builder} | @code{Compiler} | @code{Binder} |
15202 @code{Linker} | @code{Finder} | @code{Cross_Reference} |
15203 @code{^gnatls^gnatls^} | @code{IDE} | @code{Pretty_Printer}
15204
15205 package_renaming ::==
15206 @b{package} package_identifier @b{renames}
15207 <project_>simple_name.package_identifier ;
15208
15209 typed_string_declaration ::=
15210 @b{type} <typed_string_>_simple_name @b{is}
15211 ( string_literal @{, string_literal@} );
15212
15213 other_declarative_item ::=
15214 attribute_declaration |
15215 typed_variable_declaration |
15216 variable_declaration |
15217 case_construction
15218
15219 attribute_declaration ::=
15220 full_associative_array_declaration |
15221 @b{for} attribute_designator @b{use} expression ;
15222
15223 full_associative_array_declaration ::=
15224 @b{for} <associative_array_attribute_>simple_name @b{use}
15225 <project_>simple_name [ . <package_>simple_Name ] ' <attribute_>simple_name ;
15226
15227 attribute_designator ::=
15228 <simple_attribute_>simple_name |
15229 <associative_array_attribute_>simple_name ( string_literal )
15230
15231 typed_variable_declaration ::=
15232 <typed_variable_>simple_name : <typed_string_>name := string_expression ;
15233
15234 variable_declaration ::=
15235 <variable_>simple_name := expression;
15236
15237 expression ::=
15238 term @{& term@}
15239
15240 term ::=
15241 literal_string |
15242 string_list |
15243 <variable_>name |
15244 external_value |
15245 attribute_reference
15246
15247 string_literal ::=
15248 (same as Ada)
15249
15250 string_list ::=
15251 ( <string_>expression @{ , <string_>expression @} )
15252
15253 external_value ::=
15254 @b{external} ( string_literal [, string_literal] )
15255
15256 attribute_reference ::=
15257 attribute_prefix ' <simple_attribute_>simple_name [ ( literal_string ) ]
15258
15259 attribute_prefix ::=
15260 @b{project} |
15261 <project_>simple_name | package_identifier |
15262 <project_>simple_name . package_identifier
15263
15264 case_construction ::=
15265 @b{case} <typed_variable_>name @b{is}
15266 @{case_item@}
15267 @b{end case} ;
15268
15269 case_item ::=
15270 @b{when} discrete_choice_list =>
15271 @{case_construction | attribute_declaration@}
15272
15273 discrete_choice_list ::=
15274 string_literal @{| string_literal@} |
15275 @b{others}
15276
15277 name ::=
15278 simple_name @{. simple_name@}
15279
15280 simple_name ::=
15281 identifier (same as Ada)
15282
15283 @end smallexample
15284
15285 @node The Cross-Referencing Tools gnatxref and gnatfind
15286 @chapter The Cross-Referencing Tools @code{gnatxref} and @code{gnatfind}
15287 @findex gnatxref
15288 @findex gnatfind
15289
15290 @noindent
15291 The compiler generates cross-referencing information (unless
15292 you set the @samp{-gnatx} switch), which are saved in the @file{.ali} files.
15293 This information indicates where in the source each entity is declared and
15294 referenced. Note that entities in package Standard are not included, but
15295 entities in all other predefined units are included in the output.
15296
15297 Before using any of these two tools, you need to compile successfully your
15298 application, so that GNAT gets a chance to generate the cross-referencing
15299 information.
15300
15301 The two tools @code{gnatxref} and @code{gnatfind} take advantage of this
15302 information to provide the user with the capability to easily locate the
15303 declaration and references to an entity. These tools are quite similar,
15304 the difference being that @code{gnatfind} is intended for locating
15305 definitions and/or references to a specified entity or entities, whereas
15306 @code{gnatxref} is oriented to generating a full report of all
15307 cross-references.
15308
15309 To use these tools, you must not compile your application using the
15310 @option{-gnatx} switch on the @command{gnatmake} command line
15311 (@pxref{The GNAT Make Program gnatmake}). Otherwise, cross-referencing
15312 information will not be generated.
15313
15314 Note: to invoke @code{gnatxref} or @code{gnatfind} with a project file,
15315 use the @code{gnat} driver (see @ref{The GNAT Driver and Project Files}).
15316
15317 @menu
15318 * gnatxref Switches::
15319 * gnatfind Switches::
15320 * Project Files for gnatxref and gnatfind::
15321 * Regular Expressions in gnatfind and gnatxref::
15322 * Examples of gnatxref Usage::
15323 * Examples of gnatfind Usage::
15324 @end menu
15325
15326 @node gnatxref Switches
15327 @section @code{gnatxref} Switches
15328
15329 @noindent
15330 The command invocation for @code{gnatxref} is:
15331 @smallexample
15332 $ gnatxref @ovar{switches} @var{sourcefile1} @r{[}@var{sourcefile2} @dots{}@r{]}
15333 @end smallexample
15334
15335 @noindent
15336 where
15337
15338 @table @var
15339 @item sourcefile1
15340 @itemx sourcefile2
15341 identifies the source files for which a report is to be generated. The
15342 ``with''ed units will be processed too. You must provide at least one file.
15343
15344 These file names are considered to be regular expressions, so for instance
15345 specifying @file{source*.adb} is the same as giving every file in the current
15346 directory whose name starts with @file{source} and whose extension is
15347 @file{adb}.
15348
15349 You shouldn't specify any directory name, just base names. @command{gnatxref}
15350 and @command{gnatfind} will be able to locate these files by themselves using
15351 the source path. If you specify directories, no result is produced.
15352
15353 @end table
15354
15355 @noindent
15356 The switches can be:
15357 @table @option
15358 @c !sort!
15359 @item --version
15360 @cindex @option{--version} @command{gnatxref}
15361 Display Copyright and version, then exit disregarding all other options.
15362
15363 @item --help
15364 @cindex @option{--help} @command{gnatxref}
15365 If @option{--version} was not used, display usage, then exit disregarding
15366 all other options.
15367
15368 @item ^-a^/ALL_FILES^
15369 @cindex @option{^-a^/ALL_FILES^} (@command{gnatxref})
15370 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
15371 the read-only files found in the library search path. Otherwise, these files
15372 will be ignored. This option can be used to protect Gnat sources or your own
15373 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
15374 much faster, and their output much smaller. Read-only here refers to access
15375 or permissions status in the file system for the current user.
15376
15377 @item -aIDIR
15378 @cindex @option{-aIDIR} (@command{gnatxref})
15379 When looking for source files also look in directory DIR. The order in which
15380 source file search is undertaken is the same as for @command{gnatmake}.
15381
15382 @item -aODIR
15383 @cindex @option{-aODIR} (@command{gnatxref})
15384 When searching for library and object files, look in directory
15385 DIR. The order in which library files are searched is the same as for
15386 @command{gnatmake}.
15387
15388 @item -nostdinc
15389 @cindex @option{-nostdinc} (@command{gnatxref})
15390 Do not look for sources in the system default directory.
15391
15392 @item -nostdlib
15393 @cindex @option{-nostdlib} (@command{gnatxref})
15394 Do not look for library files in the system default directory.
15395
15396 @item --RTS=@var{rts-path}
15397 @cindex @option{--RTS} (@command{gnatxref})
15398 Specifies the default location of the runtime library. Same meaning as the
15399 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
15400
15401 @item ^-d^/DERIVED_TYPES^
15402 @cindex @option{^-d^/DERIVED_TYPES^} (@command{gnatxref})
15403 If this switch is set @code{gnatxref} will output the parent type
15404 reference for each matching derived types.
15405
15406 @item ^-f^/FULL_PATHNAME^
15407 @cindex @option{^-f^/FULL_PATHNAME^} (@command{gnatxref})
15408 If this switch is set, the output file names will be preceded by their
15409 directory (if the file was found in the search path). If this switch is
15410 not set, the directory will not be printed.
15411
15412 @item ^-g^/IGNORE_LOCALS^
15413 @cindex @option{^-g^/IGNORE_LOCALS^} (@command{gnatxref})
15414 If this switch is set, information is output only for library-level
15415 entities, ignoring local entities. The use of this switch may accelerate
15416 @code{gnatfind} and @code{gnatxref}.
15417
15418 @item -IDIR
15419 @cindex @option{-IDIR} (@command{gnatxref})
15420 Equivalent to @samp{-aODIR -aIDIR}.
15421
15422 @item -pFILE
15423 @cindex @option{-pFILE} (@command{gnatxref})
15424 Specify a project file to use @xref{Project Files}.
15425 If you need to use the @file{.gpr}
15426 project files, you should use gnatxref through the GNAT driver
15427 (@command{gnat xref -Pproject}).
15428
15429 By default, @code{gnatxref} and @code{gnatfind} will try to locate a
15430 project file in the current directory.
15431
15432 If a project file is either specified or found by the tools, then the content
15433 of the source directory and object directory lines are added as if they
15434 had been specified respectively by @samp{^-aI^/SOURCE_SEARCH^}
15435 and @samp{^-aO^OBJECT_SEARCH^}.
15436 @item ^-u^/UNUSED^
15437 Output only unused symbols. This may be really useful if you give your
15438 main compilation unit on the command line, as @code{gnatxref} will then
15439 display every unused entity and 'with'ed package.
15440
15441 @ifclear vms
15442 @item -v
15443 Instead of producing the default output, @code{gnatxref} will generate a
15444 @file{tags} file that can be used by vi. For examples how to use this
15445 feature, see @ref{Examples of gnatxref Usage}. The tags file is output
15446 to the standard output, thus you will have to redirect it to a file.
15447 @end ifclear
15448
15449 @end table
15450
15451 @noindent
15452 All these switches may be in any order on the command line, and may even
15453 appear after the file names. They need not be separated by spaces, thus
15454 you can say @samp{gnatxref ^-ag^/ALL_FILES/IGNORE_LOCALS^} instead of
15455 @samp{gnatxref ^-a -g^/ALL_FILES /IGNORE_LOCALS^}.
15456
15457 @node gnatfind Switches
15458 @section @code{gnatfind} Switches
15459
15460 @noindent
15461 The command line for @code{gnatfind} is:
15462
15463 @smallexample
15464 $ gnatfind @ovar{switches} @var{pattern}@r{[}:@var{sourcefile}@r{[}:@var{line}@r{[}:@var{column}@r{]]]}
15465 @r{[}@var{file1} @var{file2} @dots{}]
15466 @end smallexample
15467
15468 @noindent
15469 where
15470
15471 @table @var
15472 @item pattern
15473 An entity will be output only if it matches the regular expression found
15474 in @var{pattern}, see @ref{Regular Expressions in gnatfind and gnatxref}.
15475
15476 Omitting the pattern is equivalent to specifying @samp{*}, which
15477 will match any entity. Note that if you do not provide a pattern, you
15478 have to provide both a sourcefile and a line.
15479
15480 Entity names are given in Latin-1, with uppercase/lowercase equivalence
15481 for matching purposes. At the current time there is no support for
15482 8-bit codes other than Latin-1, or for wide characters in identifiers.
15483
15484 @item sourcefile
15485 @code{gnatfind} will look for references, bodies or declarations
15486 of symbols referenced in @file{@var{sourcefile}}, at line @var{line}
15487 and column @var{column}. See @ref{Examples of gnatfind Usage}
15488 for syntax examples.
15489
15490 @item line
15491 is a decimal integer identifying the line number containing
15492 the reference to the entity (or entities) to be located.
15493
15494 @item column
15495 is a decimal integer identifying the exact location on the
15496 line of the first character of the identifier for the
15497 entity reference. Columns are numbered from 1.
15498
15499 @item file1 file2 @dots{}
15500 The search will be restricted to these source files. If none are given, then
15501 the search will be done for every library file in the search path.
15502 These file must appear only after the pattern or sourcefile.
15503
15504 These file names are considered to be regular expressions, so for instance
15505 specifying @file{source*.adb} is the same as giving every file in the current
15506 directory whose name starts with @file{source} and whose extension is
15507 @file{adb}.
15508
15509 The location of the spec of the entity will always be displayed, even if it
15510 isn't in one of @file{@var{file1}}, @file{@var{file2}},@enddots{} The
15511 occurrences of the entity in the separate units of the ones given on the
15512 command line will also be displayed.
15513
15514 Note that if you specify at least one file in this part, @code{gnatfind} may
15515 sometimes not be able to find the body of the subprograms.
15516
15517 @end table
15518
15519 @noindent
15520 At least one of 'sourcefile' or 'pattern' has to be present on
15521 the command line.
15522
15523 The following switches are available:
15524 @table @option
15525 @c !sort!
15526
15527 @cindex @option{--version} @command{gnatfind}
15528 Display Copyright and version, then exit disregarding all other options.
15529
15530 @item --help
15531 @cindex @option{--help} @command{gnatfind}
15532 If @option{--version} was not used, display usage, then exit disregarding
15533 all other options.
15534
15535 @item ^-a^/ALL_FILES^
15536 @cindex @option{^-a^/ALL_FILES^} (@command{gnatfind})
15537 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
15538 the read-only files found in the library search path. Otherwise, these files
15539 will be ignored. This option can be used to protect Gnat sources or your own
15540 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
15541 much faster, and their output much smaller. Read-only here refers to access
15542 or permission status in the file system for the current user.
15543
15544 @item -aIDIR
15545 @cindex @option{-aIDIR} (@command{gnatfind})
15546 When looking for source files also look in directory DIR. The order in which
15547 source file search is undertaken is the same as for @command{gnatmake}.
15548
15549 @item -aODIR
15550 @cindex @option{-aODIR} (@command{gnatfind})
15551 When searching for library and object files, look in directory
15552 DIR. The order in which library files are searched is the same as for
15553 @command{gnatmake}.
15554
15555 @item -nostdinc
15556 @cindex @option{-nostdinc} (@command{gnatfind})
15557 Do not look for sources in the system default directory.
15558
15559 @item -nostdlib
15560 @cindex @option{-nostdlib} (@command{gnatfind})
15561 Do not look for library files in the system default directory.
15562
15563 @item --RTS=@var{rts-path}
15564 @cindex @option{--RTS} (@command{gnatfind})
15565 Specifies the default location of the runtime library. Same meaning as the
15566 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
15567
15568 @item ^-d^/DERIVED_TYPE_INFORMATION^
15569 @cindex @option{^-d^/DERIVED_TYPE_INFORMATION^} (@code{gnatfind})
15570 If this switch is set, then @code{gnatfind} will output the parent type
15571 reference for each matching derived types.
15572
15573 @item ^-e^/EXPRESSIONS^
15574 @cindex @option{^-e^/EXPRESSIONS^} (@command{gnatfind})
15575 By default, @code{gnatfind} accept the simple regular expression set for
15576 @samp{pattern}. If this switch is set, then the pattern will be
15577 considered as full Unix-style regular expression.
15578
15579 @item ^-f^/FULL_PATHNAME^
15580 @cindex @option{^-f^/FULL_PATHNAME^} (@command{gnatfind})
15581 If this switch is set, the output file names will be preceded by their
15582 directory (if the file was found in the search path). If this switch is
15583 not set, the directory will not be printed.
15584
15585 @item ^-g^/IGNORE_LOCALS^
15586 @cindex @option{^-g^/IGNORE_LOCALS^} (@command{gnatfind})
15587 If this switch is set, information is output only for library-level
15588 entities, ignoring local entities. The use of this switch may accelerate
15589 @code{gnatfind} and @code{gnatxref}.
15590
15591 @item -IDIR
15592 @cindex @option{-IDIR} (@command{gnatfind})
15593 Equivalent to @samp{-aODIR -aIDIR}.
15594
15595 @item -pFILE
15596 @cindex @option{-pFILE} (@command{gnatfind})
15597 Specify a project file (@pxref{Project Files}) to use.
15598 By default, @code{gnatxref} and @code{gnatfind} will try to locate a
15599 project file in the current directory.
15600
15601 If a project file is either specified or found by the tools, then the content
15602 of the source directory and object directory lines are added as if they
15603 had been specified respectively by @samp{^-aI^/SOURCE_SEARCH^} and
15604 @samp{^-aO^/OBJECT_SEARCH^}.
15605
15606 @item ^-r^/REFERENCES^
15607 @cindex @option{^-r^/REFERENCES^} (@command{gnatfind})
15608 By default, @code{gnatfind} will output only the information about the
15609 declaration, body or type completion of the entities. If this switch is
15610 set, the @code{gnatfind} will locate every reference to the entities in
15611 the files specified on the command line (or in every file in the search
15612 path if no file is given on the command line).
15613
15614 @item ^-s^/PRINT_LINES^
15615 @cindex @option{^-s^/PRINT_LINES^} (@command{gnatfind})
15616 If this switch is set, then @code{gnatfind} will output the content
15617 of the Ada source file lines were the entity was found.
15618
15619 @item ^-t^/TYPE_HIERARCHY^
15620 @cindex @option{^-t^/TYPE_HIERARCHY^} (@command{gnatfind})
15621 If this switch is set, then @code{gnatfind} will output the type hierarchy for
15622 the specified type. It act like -d option but recursively from parent
15623 type to parent type. When this switch is set it is not possible to
15624 specify more than one file.
15625
15626 @end table
15627
15628 @noindent
15629 All these switches may be in any order on the command line, and may even
15630 appear after the file names. They need not be separated by spaces, thus
15631 you can say @samp{gnatxref ^-ag^/ALL_FILES/IGNORE_LOCALS^} instead of
15632 @samp{gnatxref ^-a -g^/ALL_FILES /IGNORE_LOCALS^}.
15633
15634 As stated previously, gnatfind will search in every directory in the
15635 search path. You can force it to look only in the current directory if
15636 you specify @code{*} at the end of the command line.
15637
15638 @node Project Files for gnatxref and gnatfind
15639 @section Project Files for @command{gnatxref} and @command{gnatfind}
15640
15641 @noindent
15642 Project files allow a programmer to specify how to compile its
15643 application, where to find sources, etc. These files are used
15644 @ifclear vms
15645 primarily by GPS, but they can also be used
15646 @end ifclear
15647 by the two tools
15648 @code{gnatxref} and @code{gnatfind}.
15649
15650 A project file name must end with @file{.gpr}. If a single one is
15651 present in the current directory, then @code{gnatxref} and @code{gnatfind} will
15652 extract the information from it. If multiple project files are found, none of
15653 them is read, and you have to use the @samp{-p} switch to specify the one
15654 you want to use.
15655
15656 The following lines can be included, even though most of them have default
15657 values which can be used in most cases.
15658 The lines can be entered in any order in the file.
15659 Except for @file{src_dir} and @file{obj_dir}, you can only have one instance of
15660 each line. If you have multiple instances, only the last one is taken into
15661 account.
15662
15663 @table @code
15664 @item src_dir=DIR
15665 [default: @code{"^./^[]^"}]
15666 specifies a directory where to look for source files. Multiple @code{src_dir}
15667 lines can be specified and they will be searched in the order they
15668 are specified.
15669
15670 @item obj_dir=DIR
15671 [default: @code{"^./^[]^"}]
15672 specifies a directory where to look for object and library files. Multiple
15673 @code{obj_dir} lines can be specified, and they will be searched in the order
15674 they are specified
15675
15676 @item comp_opt=SWITCHES
15677 [default: @code{""}]
15678 creates a variable which can be referred to subsequently by using
15679 the @code{$@{comp_opt@}} notation. This is intended to store the default
15680 switches given to @command{gnatmake} and @command{gcc}.
15681
15682 @item bind_opt=SWITCHES
15683 [default: @code{""}]
15684 creates a variable which can be referred to subsequently by using
15685 the @samp{$@{bind_opt@}} notation. This is intended to store the default
15686 switches given to @command{gnatbind}.
15687
15688 @item link_opt=SWITCHES
15689 [default: @code{""}]
15690 creates a variable which can be referred to subsequently by using
15691 the @samp{$@{link_opt@}} notation. This is intended to store the default
15692 switches given to @command{gnatlink}.
15693
15694 @item main=EXECUTABLE
15695 [default: @code{""}]
15696 specifies the name of the executable for the application. This variable can
15697 be referred to in the following lines by using the @samp{$@{main@}} notation.
15698
15699 @ifset vms
15700 @item comp_cmd=COMMAND
15701 [default: @code{"GNAT COMPILE /SEARCH=$@{src_dir@} /DEBUG /TRY_SEMANTICS"}]
15702 @end ifset
15703 @ifclear vms
15704 @item comp_cmd=COMMAND
15705 [default: @code{"gcc -c -I$@{src_dir@} -g -gnatq"}]
15706 @end ifclear
15707 specifies the command used to compile a single file in the application.
15708
15709 @ifset vms
15710 @item make_cmd=COMMAND
15711 [default: @code{"GNAT MAKE $@{main@}
15712 /SOURCE_SEARCH=$@{src_dir@} /OBJECT_SEARCH=$@{obj_dir@}
15713 /DEBUG /TRY_SEMANTICS /COMPILER_QUALIFIERS $@{comp_opt@}
15714 /BINDER_QUALIFIERS $@{bind_opt@} /LINKER_QUALIFIERS $@{link_opt@}"}]
15715 @end ifset
15716 @ifclear vms
15717 @item make_cmd=COMMAND
15718 [default: @code{"gnatmake $@{main@} -aI$@{src_dir@}
15719 -aO$@{obj_dir@} -g -gnatq -cargs $@{comp_opt@}
15720 -bargs $@{bind_opt@} -largs $@{link_opt@}"}]
15721 @end ifclear
15722 specifies the command used to recompile the whole application.
15723
15724 @item run_cmd=COMMAND
15725 [default: @code{"$@{main@}"}]
15726 specifies the command used to run the application.
15727
15728 @item debug_cmd=COMMAND
15729 [default: @code{"gdb $@{main@}"}]
15730 specifies the command used to debug the application
15731
15732 @end table
15733
15734 @noindent
15735 @command{gnatxref} and @command{gnatfind} only take into account the
15736 @code{src_dir} and @code{obj_dir} lines, and ignore the others.
15737
15738 @node Regular Expressions in gnatfind and gnatxref
15739 @section Regular Expressions in @code{gnatfind} and @code{gnatxref}
15740
15741 @noindent
15742 As specified in the section about @command{gnatfind}, the pattern can be a
15743 regular expression. Actually, there are to set of regular expressions
15744 which are recognized by the program:
15745
15746 @table @code
15747 @item globbing patterns
15748 These are the most usual regular expression. They are the same that you
15749 generally used in a Unix shell command line, or in a DOS session.
15750
15751 Here is a more formal grammar:
15752 @smallexample
15753 @group
15754 @iftex
15755 @leftskip=.5cm
15756 @end iftex
15757 regexp ::= term
15758 term ::= elmt -- matches elmt
15759 term ::= elmt elmt -- concatenation (elmt then elmt)
15760 term ::= * -- any string of 0 or more characters
15761 term ::= ? -- matches any character
15762 term ::= [char @{char@}] -- matches any character listed
15763 term ::= [char - char] -- matches any character in range
15764 @end group
15765 @end smallexample
15766
15767 @item full regular expression
15768 The second set of regular expressions is much more powerful. This is the
15769 type of regular expressions recognized by utilities such a @file{grep}.
15770
15771 The following is the form of a regular expression, expressed in Ada
15772 reference manual style BNF is as follows
15773
15774 @smallexample
15775 @iftex
15776 @leftskip=.5cm
15777 @end iftex
15778 @group
15779 regexp ::= term @{| term@} -- alternation (term or term @dots{})
15780
15781 term ::= item @{item@} -- concatenation (item then item)
15782
15783 item ::= elmt -- match elmt
15784 item ::= elmt * -- zero or more elmt's
15785 item ::= elmt + -- one or more elmt's
15786 item ::= elmt ? -- matches elmt or nothing
15787 @end group
15788 @group
15789 elmt ::= nschar -- matches given character
15790 elmt ::= [nschar @{nschar@}] -- matches any character listed
15791 elmt ::= [^^^ nschar @{nschar@}] -- matches any character not listed
15792 elmt ::= [char - char] -- matches chars in given range
15793 elmt ::= \ char -- matches given character
15794 elmt ::= . -- matches any single character
15795 elmt ::= ( regexp ) -- parens used for grouping
15796
15797 char ::= any character, including special characters
15798 nschar ::= any character except ()[].*+?^^^
15799 @end group
15800 @end smallexample
15801
15802 Following are a few examples:
15803
15804 @table @samp
15805 @item abcde|fghi
15806 will match any of the two strings @samp{abcde} and @samp{fghi},
15807
15808 @item abc*d
15809 will match any string like @samp{abd}, @samp{abcd}, @samp{abccd},
15810 @samp{abcccd}, and so on,
15811
15812 @item [a-z]+
15813 will match any string which has only lowercase characters in it (and at
15814 least one character.
15815
15816 @end table
15817 @end table
15818
15819 @node Examples of gnatxref Usage
15820 @section Examples of @code{gnatxref} Usage
15821
15822 @subsection General Usage
15823
15824 @noindent
15825 For the following examples, we will consider the following units:
15826
15827 @smallexample @c ada
15828 @group
15829 @cartouche
15830 main.ads:
15831 1: with Bar;
15832 2: package Main is
15833 3: procedure Foo (B : in Integer);
15834 4: C : Integer;
15835 5: private
15836 6: D : Integer;
15837 7: end Main;
15838
15839 main.adb:
15840 1: package body Main is
15841 2: procedure Foo (B : in Integer) is
15842 3: begin
15843 4: C := B;
15844 5: D := B;
15845 6: Bar.Print (B);
15846 7: Bar.Print (C);
15847 8: end Foo;
15848 9: end Main;
15849
15850 bar.ads:
15851 1: package Bar is
15852 2: procedure Print (B : Integer);
15853 3: end bar;
15854 @end cartouche
15855 @end group
15856 @end smallexample
15857
15858 @table @code
15859
15860 @noindent
15861 The first thing to do is to recompile your application (for instance, in
15862 that case just by doing a @samp{gnatmake main}, so that GNAT generates
15863 the cross-referencing information.
15864 You can then issue any of the following commands:
15865
15866 @item gnatxref main.adb
15867 @code{gnatxref} generates cross-reference information for main.adb
15868 and every unit 'with'ed by main.adb.
15869
15870 The output would be:
15871 @smallexample
15872 @iftex
15873 @leftskip=0cm
15874 @end iftex
15875 B Type: Integer
15876 Decl: bar.ads 2:22
15877 B Type: Integer
15878 Decl: main.ads 3:20
15879 Body: main.adb 2:20
15880 Ref: main.adb 4:13 5:13 6:19
15881 Bar Type: Unit
15882 Decl: bar.ads 1:9
15883 Ref: main.adb 6:8 7:8
15884 main.ads 1:6
15885 C Type: Integer
15886 Decl: main.ads 4:5
15887 Modi: main.adb 4:8
15888 Ref: main.adb 7:19
15889 D Type: Integer
15890 Decl: main.ads 6:5
15891 Modi: main.adb 5:8
15892 Foo Type: Unit
15893 Decl: main.ads 3:15
15894 Body: main.adb 2:15
15895 Main Type: Unit
15896 Decl: main.ads 2:9
15897 Body: main.adb 1:14
15898 Print Type: Unit
15899 Decl: bar.ads 2:15
15900 Ref: main.adb 6:12 7:12
15901 @end smallexample
15902
15903 @noindent
15904 that is the entity @code{Main} is declared in main.ads, line 2, column 9,
15905 its body is in main.adb, line 1, column 14 and is not referenced any where.
15906
15907 The entity @code{Print} is declared in bar.ads, line 2, column 15 and it
15908 it referenced in main.adb, line 6 column 12 and line 7 column 12.
15909
15910 @item gnatxref package1.adb package2.ads
15911 @code{gnatxref} will generates cross-reference information for
15912 package1.adb, package2.ads and any other package 'with'ed by any
15913 of these.
15914
15915 @end table
15916
15917 @ifclear vms
15918 @subsection Using gnatxref with vi
15919
15920 @code{gnatxref} can generate a tags file output, which can be used
15921 directly from @command{vi}. Note that the standard version of @command{vi}
15922 will not work properly with overloaded symbols. Consider using another
15923 free implementation of @command{vi}, such as @command{vim}.
15924
15925 @smallexample
15926 $ gnatxref -v gnatfind.adb > tags
15927 @end smallexample
15928
15929 @noindent
15930 will generate the tags file for @code{gnatfind} itself (if the sources
15931 are in the search path!).
15932
15933 From @command{vi}, you can then use the command @samp{:tag @var{entity}}
15934 (replacing @var{entity} by whatever you are looking for), and vi will
15935 display a new file with the corresponding declaration of entity.
15936 @end ifclear
15937
15938 @node Examples of gnatfind Usage
15939 @section Examples of @code{gnatfind} Usage
15940
15941 @table @code
15942
15943 @item gnatfind ^-f^/FULL_PATHNAME^ xyz:main.adb
15944 Find declarations for all entities xyz referenced at least once in
15945 main.adb. The references are search in every library file in the search
15946 path.
15947
15948 The directories will be printed as well (as the @samp{^-f^/FULL_PATHNAME^}
15949 switch is set)
15950
15951 The output will look like:
15952 @smallexample
15953 ^directory/^[directory]^main.ads:106:14: xyz <= declaration
15954 ^directory/^[directory]^main.adb:24:10: xyz <= body
15955 ^directory/^[directory]^foo.ads:45:23: xyz <= declaration
15956 @end smallexample
15957
15958 @noindent
15959 that is to say, one of the entities xyz found in main.adb is declared at
15960 line 12 of main.ads (and its body is in main.adb), and another one is
15961 declared at line 45 of foo.ads
15962
15963 @item gnatfind ^-fs^/FULL_PATHNAME/SOURCE_LINE^ xyz:main.adb
15964 This is the same command as the previous one, instead @code{gnatfind} will
15965 display the content of the Ada source file lines.
15966
15967 The output will look like:
15968
15969 @smallexample
15970 ^directory/^[directory]^main.ads:106:14: xyz <= declaration
15971 procedure xyz;
15972 ^directory/^[directory]^main.adb:24:10: xyz <= body
15973 procedure xyz is
15974 ^directory/^[directory]^foo.ads:45:23: xyz <= declaration
15975 xyz : Integer;
15976 @end smallexample
15977
15978 @noindent
15979 This can make it easier to find exactly the location your are looking
15980 for.
15981
15982 @item gnatfind ^-r^/REFERENCES^ "*x*":main.ads:123 foo.adb
15983 Find references to all entities containing an x that are
15984 referenced on line 123 of main.ads.
15985 The references will be searched only in main.ads and foo.adb.
15986
15987 @item gnatfind main.ads:123
15988 Find declarations and bodies for all entities that are referenced on
15989 line 123 of main.ads.
15990
15991 This is the same as @code{gnatfind "*":main.adb:123}.
15992
15993 @item gnatfind ^mydir/^[mydir]^main.adb:123:45
15994 Find the declaration for the entity referenced at column 45 in
15995 line 123 of file main.adb in directory mydir. Note that it
15996 is usual to omit the identifier name when the column is given,
15997 since the column position identifies a unique reference.
15998
15999 The column has to be the beginning of the identifier, and should not
16000 point to any character in the middle of the identifier.
16001
16002 @end table
16003
16004 @c *********************************
16005 @node The GNAT Pretty-Printer gnatpp
16006 @chapter The GNAT Pretty-Printer @command{gnatpp}
16007 @findex gnatpp
16008 @cindex Pretty-Printer
16009
16010 @noindent
16011 ^The @command{gnatpp} tool^GNAT PRETTY^ is an ASIS-based utility
16012 for source reformatting / pretty-printing.
16013 It takes an Ada source file as input and generates a reformatted
16014 version as output.
16015 You can specify various style directives via switches; e.g.,
16016 identifier case conventions, rules of indentation, and comment layout.
16017
16018 To produce a reformatted file, @command{gnatpp} generates and uses the ASIS
16019 tree for the input source and thus requires the input to be syntactically and
16020 semantically legal.
16021 If this condition is not met, @command{gnatpp} will terminate with an
16022 error message; no output file will be generated.
16023
16024 If the source files presented to @command{gnatpp} contain
16025 preprocessing directives, then the output file will
16026 correspond to the generated source after all
16027 preprocessing is carried out. There is no way
16028 using @command{gnatpp} to obtain pretty printed files that
16029 include the preprocessing directives.
16030
16031 If the compilation unit
16032 contained in the input source depends semantically upon units located
16033 outside the current directory, you have to provide the source search path
16034 when invoking @command{gnatpp}, if these units are contained in files with
16035 names that do not follow the GNAT file naming rules, you have to provide
16036 the configuration file describing the corresponding naming scheme;
16037 see the description of the @command{gnatpp}
16038 switches below. Another possibility is to use a project file and to
16039 call @command{gnatpp} through the @command{gnat} driver
16040
16041 The @command{gnatpp} command has the form
16042
16043 @smallexample
16044 $ gnatpp @ovar{switches} @var{filename}
16045 @end smallexample
16046
16047 @noindent
16048 where
16049 @itemize @bullet
16050 @item
16051 @var{switches} is an optional sequence of switches defining such properties as
16052 the formatting rules, the source search path, and the destination for the
16053 output source file
16054
16055 @item
16056 @var{filename} is the name (including the extension) of the source file to
16057 reformat; ``wildcards'' or several file names on the same gnatpp command are
16058 allowed. The file name may contain path information; it does not have to
16059 follow the GNAT file naming rules
16060 @end itemize
16061
16062 @menu
16063 * Switches for gnatpp::
16064 * Formatting Rules::
16065 @end menu
16066
16067 @node Switches for gnatpp
16068 @section Switches for @command{gnatpp}
16069
16070 @noindent
16071 The following subsections describe the various switches accepted by
16072 @command{gnatpp}, organized by category.
16073
16074 @ifclear vms
16075 You specify a switch by supplying a name and generally also a value.
16076 In many cases the values for a switch with a given name are incompatible with
16077 each other
16078 (for example the switch that controls the casing of a reserved word may have
16079 exactly one value: upper case, lower case, or
16080 mixed case) and thus exactly one such switch can be in effect for an
16081 invocation of @command{gnatpp}.
16082 If more than one is supplied, the last one is used.
16083 However, some values for the same switch are mutually compatible.
16084 You may supply several such switches to @command{gnatpp}, but then
16085 each must be specified in full, with both the name and the value.
16086 Abbreviated forms (the name appearing once, followed by each value) are
16087 not permitted.
16088 For example, to set
16089 the alignment of the assignment delimiter both in declarations and in
16090 assignment statements, you must write @option{-A2A3}
16091 (or @option{-A2 -A3}), but not @option{-A23}.
16092 @end ifclear
16093
16094 @ifset vms
16095 In many cases the set of options for a given qualifier are incompatible with
16096 each other (for example the qualifier that controls the casing of a reserved
16097 word may have exactly one option, which specifies either upper case, lower
16098 case, or mixed case), and thus exactly one such option can be in effect for
16099 an invocation of @command{gnatpp}.
16100 If more than one is supplied, the last one is used.
16101 However, some qualifiers have options that are mutually compatible,
16102 and then you may then supply several such options when invoking
16103 @command{gnatpp}.
16104 @end ifset
16105
16106 In most cases, it is obvious whether or not the
16107 ^values for a switch with a given name^options for a given qualifier^
16108 are compatible with each other.
16109 When the semantics might not be evident, the summaries below explicitly
16110 indicate the effect.
16111
16112 @menu
16113 * Alignment Control::
16114 * Casing Control::
16115 * Construct Layout Control::
16116 * General Text Layout Control::
16117 * Other Formatting Options::
16118 * Setting the Source Search Path::
16119 * Output File Control::
16120 * Other gnatpp Switches::
16121 @end menu
16122
16123 @node Alignment Control
16124 @subsection Alignment Control
16125 @cindex Alignment control in @command{gnatpp}
16126
16127 @noindent
16128 Programs can be easier to read if certain constructs are vertically aligned.
16129 By default all alignments are set ON.
16130 Through the @option{^-A0^/ALIGN=OFF^} switch you may reset the default to
16131 OFF, and then use one or more of the other
16132 ^@option{-A@var{n}} switches^@option{/ALIGN} options^
16133 to activate alignment for specific constructs.
16134
16135 @table @option
16136 @cindex @option{^-A@var{n}^/ALIGN^} (@command{gnatpp})
16137
16138 @ifset vms
16139 @item /ALIGN=ON
16140 Set all alignments to ON
16141 @end ifset
16142
16143 @item ^-A0^/ALIGN=OFF^
16144 Set all alignments to OFF
16145
16146 @item ^-A1^/ALIGN=COLONS^
16147 Align @code{:} in declarations
16148
16149 @item ^-A2^/ALIGN=DECLARATIONS^
16150 Align @code{:=} in initializations in declarations
16151
16152 @item ^-A3^/ALIGN=STATEMENTS^
16153 Align @code{:=} in assignment statements
16154
16155 @item ^-A4^/ALIGN=ARROWS^
16156 Align @code{=>} in associations
16157
16158 @item ^-A5^/ALIGN=COMPONENT_CLAUSES^
16159 Align @code{at} keywords in the component clauses in record
16160 representation clauses
16161 @end table
16162
16163 @noindent
16164 The @option{^-A^/ALIGN^} switches are mutually compatible; any combination
16165 is allowed.
16166
16167 @node Casing Control
16168 @subsection Casing Control
16169 @cindex Casing control in @command{gnatpp}
16170
16171 @noindent
16172 @command{gnatpp} allows you to specify the casing for reserved words,
16173 pragma names, attribute designators and identifiers.
16174 For identifiers you may define a
16175 general rule for name casing but also override this rule
16176 via a set of dictionary files.
16177
16178 Three types of casing are supported: lower case, upper case, and mixed case.
16179 Lower and upper case are self-explanatory (but since some letters in
16180 Latin1 and other GNAT-supported character sets
16181 exist only in lower-case form, an upper case conversion will have no
16182 effect on them.)
16183 ``Mixed case'' means that the first letter, and also each letter immediately
16184 following an underscore, are converted to their uppercase forms;
16185 all the other letters are converted to their lowercase forms.
16186
16187 @table @option
16188 @cindex @option{^-a@var{x}^/ATTRIBUTE^} (@command{gnatpp})
16189 @item ^-aL^/ATTRIBUTE_CASING=LOWER_CASE^
16190 Attribute designators are lower case
16191
16192 @item ^-aU^/ATTRIBUTE_CASING=UPPER_CASE^
16193 Attribute designators are upper case
16194
16195 @item ^-aM^/ATTRIBUTE_CASING=MIXED_CASE^
16196 Attribute designators are mixed case (this is the default)
16197
16198 @cindex @option{^-k@var{x}^/KEYWORD_CASING^} (@command{gnatpp})
16199 @item ^-kL^/KEYWORD_CASING=LOWER_CASE^
16200 Keywords (technically, these are known in Ada as @emph{reserved words}) are
16201 lower case (this is the default)
16202
16203 @item ^-kU^/KEYWORD_CASING=UPPER_CASE^
16204 Keywords are upper case
16205
16206 @cindex @option{^-n@var{x}^/NAME_CASING^} (@command{gnatpp})
16207 @item ^-nD^/NAME_CASING=AS_DECLARED^
16208 Name casing for defining occurrences are as they appear in the source file
16209 (this is the default)
16210
16211 @item ^-nU^/NAME_CASING=UPPER_CASE^
16212 Names are in upper case
16213
16214 @item ^-nL^/NAME_CASING=LOWER_CASE^
16215 Names are in lower case
16216
16217 @item ^-nM^/NAME_CASING=MIXED_CASE^
16218 Names are in mixed case
16219
16220 @cindex @option{^-p@var{x}^/PRAGMA_CASING^} (@command{gnatpp})
16221 @item ^-pL^/PRAGMA_CASING=LOWER_CASE^
16222 Pragma names are lower case
16223
16224 @item ^-pU^/PRAGMA_CASING=UPPER_CASE^
16225 Pragma names are upper case
16226
16227 @item ^-pM^/PRAGMA_CASING=MIXED_CASE^
16228 Pragma names are mixed case (this is the default)
16229
16230 @item ^-D@var{file}^/DICTIONARY=@var{file}^
16231 @cindex @option{^-D^/DICTIONARY^} (@command{gnatpp})
16232 Use @var{file} as a @emph{dictionary file} that defines
16233 the casing for a set of specified names,
16234 thereby overriding the effect on these names by
16235 any explicit or implicit
16236 ^-n^/NAME_CASING^ switch.
16237 To supply more than one dictionary file,
16238 use ^several @option{-D} switches^a list of files as options^.
16239
16240 @noindent
16241 @option{gnatpp} implicitly uses a @emph{default dictionary file}
16242 to define the casing for the Ada predefined names and
16243 the names declared in the GNAT libraries.
16244
16245 @item ^-D-^/SPECIFIC_CASING^
16246 @cindex @option{^-D-^/SPECIFIC_CASING^} (@command{gnatpp})
16247 Do not use the default dictionary file;
16248 instead, use the casing
16249 defined by a @option{^-n^/NAME_CASING^} switch and any explicit
16250 dictionary file(s)
16251 @end table
16252
16253 @noindent
16254 The structure of a dictionary file, and details on the conventions
16255 used in the default dictionary file, are defined in @ref{Name Casing}.
16256
16257 The @option{^-D-^/SPECIFIC_CASING^} and
16258 @option{^-D@var{file}^/DICTIONARY=@var{file}^} switches are mutually
16259 compatible.
16260
16261 @node Construct Layout Control
16262 @subsection Construct Layout Control
16263 @cindex Layout control in @command{gnatpp}
16264
16265 @noindent
16266 This group of @command{gnatpp} switches controls the layout of comments and
16267 complex syntactic constructs. See @ref{Formatting Comments} for details
16268 on their effect.
16269
16270 @table @option
16271 @cindex @option{^-c@var{n}^/COMMENTS_LAYOUT^} (@command{gnatpp})
16272 @item ^-c0^/COMMENTS_LAYOUT=UNTOUCHED^
16273 All the comments remain unchanged
16274
16275 @item ^-c1^/COMMENTS_LAYOUT=DEFAULT^
16276 GNAT-style comment line indentation (this is the default).
16277
16278 @item ^-c2^/COMMENTS_LAYOUT=STANDARD_INDENT^
16279 Reference-manual comment line indentation.
16280
16281 @item ^-c3^/COMMENTS_LAYOUT=GNAT_BEGINNING^
16282 GNAT-style comment beginning
16283
16284 @item ^-c4^/COMMENTS_LAYOUT=REFORMAT^
16285 Reformat comment blocks
16286
16287 @item ^-c5^/COMMENTS_LAYOUT=KEEP_SPECIAL^
16288 Keep unchanged special form comments
16289
16290 Reformat comment blocks
16291
16292 @cindex @option{^-l@var{n}^/CONSTRUCT_LAYOUT^} (@command{gnatpp})
16293 @item ^-l1^/CONSTRUCT_LAYOUT=GNAT^
16294 GNAT-style layout (this is the default)
16295
16296 @item ^-l2^/CONSTRUCT_LAYOUT=COMPACT^
16297 Compact layout
16298
16299 @item ^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^
16300 Uncompact layout
16301
16302 @cindex @option{^-N^/NOTABS^} (@command{gnatpp})
16303 @item ^-N^/NOTABS^
16304 All the VT characters are removed from the comment text. All the HT characters
16305 are expanded with the sequences of space characters to get to the next tab
16306 stops.
16307
16308 @cindex @option{^--no-separate-is^/NO_SEPARATE_IS^} (@command{gnatpp})
16309 @item ^--no-separate-is^/NO_SEPARATE_IS^
16310 Do not place the keyword @code{is} on a separate line in a subprogram body in
16311 case if the spec occupies more then one line.
16312
16313 @cindex @option{^--separate-loop-then^/SEPARATE_LOOP_THEN^} (@command{gnatpp})
16314 @item ^--separate-loop-then^/SEPARATE_LOOP_THEN^
16315 Place the keyword @code{loop} in FOR and WHILE loop statements and the
16316 keyword @code{then} in IF statements on a separate line.
16317
16318 @cindex @option{^--no-separate-loop-then^/NO_SEPARATE_LOOP_THEN^} (@command{gnatpp})
16319 @item ^--no-separate-loop-then^/NO_SEPARATE_LOOP_THEN^
16320 Do not place the keyword @code{loop} in FOR and WHILE loop statements and the
16321 keyword @code{then} in IF statements on a separate line. This option is
16322 incompatible with @option{^--separate-loop-then^/SEPARATE_LOOP_THEN^} option.
16323
16324 @cindex @option{^--use-on-new-line^/USE_ON_NEW_LINE^} (@command{gnatpp})
16325 @item ^--use-on-new-line^/USE_ON_NEW_LINE^
16326 Start each USE clause in a context clause from a separate line.
16327
16328 @cindex @option{^--separate-stmt-name^/STMT_NAME_ON_NEW_LINE^} (@command{gnatpp})
16329 @item ^--separate-stmt-name^/STMT_NAME_ON_NEW_LINE^
16330 Use a separate line for a loop or block statement name, but do not use an extra
16331 indentation level for the statement itself.
16332
16333 @end table
16334
16335 @ifclear vms
16336 @noindent
16337 The @option{-c1} and @option{-c2} switches are incompatible.
16338 The @option{-c3} and @option{-c4} switches are compatible with each other and
16339 also with @option{-c1} and @option{-c2}. The @option{-c0} switch disables all
16340 the other comment formatting switches.
16341
16342 The @option{-l1}, @option{-l2}, and @option{-l3} switches are incompatible.
16343 @end ifclear
16344
16345 @ifset vms
16346 @noindent
16347 For the @option{/COMMENTS_LAYOUT} qualifier:
16348 @itemize @bullet
16349 @item
16350 The @option{DEFAULT} and @option{STANDARD_INDENT} options are incompatible.
16351 @item
16352 The @option{GNAT_BEGINNING} and @option{REFORMAT} options are compatible with
16353 each other and also with @option{DEFAULT} and @option{STANDARD_INDENT}.
16354 @end itemize
16355
16356 @noindent
16357 The @option{GNAT}, @option{COMPACT}, and @option{UNCOMPACT} options for the
16358 @option{/CONSTRUCT_LAYOUT} qualifier are incompatible.
16359 @end ifset
16360
16361 @node General Text Layout Control
16362 @subsection General Text Layout Control
16363
16364 @noindent
16365 These switches allow control over line length and indentation.
16366
16367 @table @option
16368 @item ^-M@var{nnn}^/LINE_LENGTH_MAX=@var{nnn}^
16369 @cindex @option{^-M^/LINE_LENGTH^} (@command{gnatpp})
16370 Maximum line length, @var{nnn} from 32@dots{}256, the default value is 79
16371
16372 @item ^-i@var{nnn}^/INDENTATION_LEVEL=@var{nnn}^
16373 @cindex @option{^-i^/INDENTATION_LEVEL^} (@command{gnatpp})
16374 Indentation level, @var{nnn} from 1@dots{}9, the default value is 3
16375
16376 @item ^-cl@var{nnn}^/CONTINUATION_INDENT=@var{nnn}^
16377 @cindex @option{^-cl^/CONTINUATION_INDENT^} (@command{gnatpp})
16378 Indentation level for continuation lines (relative to the line being
16379 continued), @var{nnn} from 1@dots{}9.
16380 The default
16381 value is one less then the (normal) indentation level, unless the
16382 indentation is set to 1 (in which case the default value for continuation
16383 line indentation is also 1)
16384 @end table
16385
16386 @node Other Formatting Options
16387 @subsection Other Formatting Options
16388
16389 @noindent
16390 These switches control the inclusion of missing end/exit labels, and
16391 the indentation level in @b{case} statements.
16392
16393 @table @option
16394 @item ^-e^/NO_MISSED_LABELS^
16395 @cindex @option{^-e^/NO_MISSED_LABELS^} (@command{gnatpp})
16396 Do not insert missing end/exit labels. An end label is the name of
16397 a construct that may optionally be repeated at the end of the
16398 construct's declaration;
16399 e.g., the names of packages, subprograms, and tasks.
16400 An exit label is the name of a loop that may appear as target
16401 of an exit statement within the loop.
16402 By default, @command{gnatpp} inserts these end/exit labels when
16403 they are absent from the original source. This option suppresses such
16404 insertion, so that the formatted source reflects the original.
16405
16406 @item ^-ff^/FORM_FEED_AFTER_PRAGMA_PAGE^
16407 @cindex @option{^-ff^/FORM_FEED_AFTER_PRAGMA_PAGE^} (@command{gnatpp})
16408 Insert a Form Feed character after a pragma Page.
16409
16410 @item ^-T@var{nnn}^/MAX_INDENT=@var{nnn}^
16411 @cindex @option{^-T^/MAX_INDENT^} (@command{gnatpp})
16412 Do not use an additional indentation level for @b{case} alternatives
16413 and variants if there are @var{nnn} or more (the default
16414 value is 10).
16415 If @var{nnn} is 0, an additional indentation level is
16416 used for @b{case} alternatives and variants regardless of their number.
16417 @end table
16418
16419 @node Setting the Source Search Path
16420 @subsection Setting the Source Search Path
16421
16422 @noindent
16423 To define the search path for the input source file, @command{gnatpp}
16424 uses the same switches as the GNAT compiler, with the same effects.
16425
16426 @table @option
16427 @item ^-I^/SEARCH=^@var{dir}
16428 @cindex @option{^-I^/SEARCH^} (@code{gnatpp})
16429 The same as the corresponding gcc switch
16430
16431 @item ^-I-^/NOCURRENT_DIRECTORY^
16432 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatpp})
16433 The same as the corresponding gcc switch
16434
16435 @item ^-gnatec^/CONFIGURATION_PRAGMAS_FILE^=@var{path}
16436 @cindex @option{^-gnatec^/CONFIGURATION_PRAGMAS_FILE^} (@code{gnatpp})
16437 The same as the corresponding gcc switch
16438
16439 @item ^--RTS^/RUNTIME_SYSTEM^=@var{path}
16440 @cindex @option{^--RTS^/RUNTIME_SYSTEM^} (@code{gnatpp})
16441 The same as the corresponding gcc switch
16442
16443 @end table
16444
16445 @node Output File Control
16446 @subsection Output File Control
16447
16448 @noindent
16449 By default the output is sent to the file whose name is obtained by appending
16450 the ^@file{.pp}^@file{$PP}^ suffix to the name of the input file
16451 (if the file with this name already exists, it is unconditionally overwritten).
16452 Thus if the input file is @file{^my_ada_proc.adb^MY_ADA_PROC.ADB^} then
16453 @command{gnatpp} will produce @file{^my_ada_proc.adb.pp^MY_ADA_PROC.ADB$PP^}
16454 as output file.
16455 The output may be redirected by the following switches:
16456
16457 @table @option
16458 @item ^-pipe^/STANDARD_OUTPUT^
16459 @cindex @option{^-pipe^/STANDARD_OUTPUT^} (@code{gnatpp})
16460 Send the output to @code{Standard_Output}
16461
16462 @item ^-o @var{output_file}^/OUTPUT=@var{output_file}^
16463 @cindex @option{^-o^/OUTPUT^} (@code{gnatpp})
16464 Write the output into @var{output_file}.
16465 If @var{output_file} already exists, @command{gnatpp} terminates without
16466 reading or processing the input file.
16467
16468 @item ^-of ^/FORCED_OUTPUT=^@var{output_file}
16469 @cindex @option{^-of^/FORCED_OUTPUT^} (@code{gnatpp})
16470 Write the output into @var{output_file}, overwriting the existing file
16471 (if one is present).
16472
16473 @item ^-r^/REPLACE^
16474 @cindex @option{^-r^/REPLACE^} (@code{gnatpp})
16475 Replace the input source file with the reformatted output, and copy the
16476 original input source into the file whose name is obtained by appending the
16477 ^@file{.npp}^@file{$NPP}^ suffix to the name of the input file.
16478 If a file with this name already exists, @command{gnatpp} terminates without
16479 reading or processing the input file.
16480
16481 @item ^-rf^/OVERRIDING_REPLACE^
16482 @cindex @option{^-rf^/OVERRIDING_REPLACE^} (@code{gnatpp})
16483 Like @option{^-r^/REPLACE^} except that if the file with the specified name
16484 already exists, it is overwritten.
16485
16486 @item ^-rnb^/REPLACE_NO_BACKUP^
16487 @cindex @option{^-rnb^/REPLACE_NO_BACKUP^} (@code{gnatpp})
16488 Replace the input source file with the reformatted output without
16489 creating any backup copy of the input source.
16490
16491 @item ^--eol=@var{xxx}^/END_OF_LINE=@var{xxx}^
16492 @cindex @option{^--eol^/END_OF_LINE^} (@code{gnatpp})
16493 Specifies the format of the reformatted output file. The @var{xxx}
16494 ^string specified with the switch^option^ may be either
16495 @itemize @bullet
16496 @item ``@option{^dos^DOS^}'' MS DOS style, lines end with CR LF characters
16497 @item ``@option{^crlf^CRLF^}''
16498 the same as @option{^crlf^CRLF^}
16499 @item ``@option{^unix^UNIX^}'' UNIX style, lines end with LF character
16500 @item ``@option{^lf^LF^}''
16501 the same as @option{^unix^UNIX^}
16502 @end itemize
16503
16504 @item ^-W^/RESULT_ENCODING=^@var{e}
16505 @cindex @option{^-W^/RESULT_ENCODING=^} (@command{gnatpp})
16506 Specify the wide character encoding method used to write the code in the
16507 result file
16508 @var{e} is one of the following:
16509
16510 @itemize @bullet
16511
16512 @item ^h^HEX^
16513 Hex encoding
16514
16515 @item ^u^UPPER^
16516 Upper half encoding
16517
16518 @item ^s^SHIFT_JIS^
16519 Shift/JIS encoding
16520
16521 @item ^e^EUC^
16522 EUC encoding
16523
16524 @item ^8^UTF8^
16525 UTF-8 encoding
16526
16527 @item ^b^BRACKETS^
16528 Brackets encoding (default value)
16529 @end itemize
16530
16531 @end table
16532
16533 @noindent
16534 Options @option{^-pipe^/STANDARD_OUTPUT^},
16535 @option{^-o^/OUTPUT^} and
16536 @option{^-of^/FORCED_OUTPUT^} are allowed only if the call to gnatpp
16537 contains only one file to reformat.
16538 Option
16539 @option{^--eol^/END_OF_LINE^}
16540 and
16541 @option{^-W^/RESULT_ENCODING^}
16542 cannot be used together
16543 with @option{^-pipe^/STANDARD_OUTPUT^} option.
16544
16545 @node Other gnatpp Switches
16546 @subsection Other @code{gnatpp} Switches
16547
16548 @noindent
16549 The additional @command{gnatpp} switches are defined in this subsection.
16550
16551 @table @option
16552 @item ^-files @var{filename}^/FILES=@var{output_file}^
16553 @cindex @option{^-files^/FILES^} (@code{gnatpp})
16554 Take the argument source files from the specified file. This file should be an
16555 ordinary textual file containing file names separated by spaces or
16556 line breaks. You can use this switch more then once in the same call to
16557 @command{gnatpp}. You also can combine this switch with explicit list of
16558 files.
16559
16560 @item ^-v^/VERBOSE^
16561 @cindex @option{^-v^/VERBOSE^} (@code{gnatpp})
16562 Verbose mode;
16563 @command{gnatpp} generates version information and then
16564 a trace of the actions it takes to produce or obtain the ASIS tree.
16565
16566 @item ^-w^/WARNINGS^
16567 @cindex @option{^-w^/WARNINGS^} (@code{gnatpp})
16568 Warning mode;
16569 @command{gnatpp} generates a warning whenever it cannot provide
16570 a required layout in the result source.
16571 @end table
16572
16573 @node Formatting Rules
16574 @section Formatting Rules
16575
16576 @noindent
16577 The following subsections show how @command{gnatpp} treats ``white space'',
16578 comments, program layout, and name casing.
16579 They provide the detailed descriptions of the switches shown above.
16580
16581 @menu
16582 * White Space and Empty Lines::
16583 * Formatting Comments::
16584 * Construct Layout::
16585 * Name Casing::
16586 @end menu
16587
16588 @node White Space and Empty Lines
16589 @subsection White Space and Empty Lines
16590
16591 @noindent
16592 @command{gnatpp} does not have an option to control space characters.
16593 It will add or remove spaces according to the style illustrated by the
16594 examples in the @cite{Ada Reference Manual}.
16595
16596 The only format effectors
16597 (see @cite{Ada Reference Manual}, paragraph 2.1(13))
16598 that will appear in the output file are platform-specific line breaks,
16599 and also format effectors within (but not at the end of) comments.
16600 In particular, each horizontal tab character that is not inside
16601 a comment will be treated as a space and thus will appear in the
16602 output file as zero or more spaces depending on
16603 the reformatting of the line in which it appears.
16604 The only exception is a Form Feed character, which is inserted after a
16605 pragma @code{Page} when @option{-ff} is set.
16606
16607 The output file will contain no lines with trailing ``white space'' (spaces,
16608 format effectors).
16609
16610 Empty lines in the original source are preserved
16611 only if they separate declarations or statements.
16612 In such contexts, a
16613 sequence of two or more empty lines is replaced by exactly one empty line.
16614 Note that a blank line will be removed if it separates two ``comment blocks''
16615 (a comment block is a sequence of whole-line comments).
16616 In order to preserve a visual separation between comment blocks, use an
16617 ``empty comment'' (a line comprising only hyphens) rather than an empty line.
16618 Likewise, if for some reason you wish to have a sequence of empty lines,
16619 use a sequence of empty comments instead.
16620
16621 @node Formatting Comments
16622 @subsection Formatting Comments
16623
16624 @noindent
16625 Comments in Ada code are of two kinds:
16626 @itemize @bullet
16627 @item
16628 a @emph{whole-line comment}, which appears by itself (possibly preceded by
16629 ``white space'') on a line
16630
16631 @item
16632 an @emph{end-of-line comment}, which follows some other Ada lexical element
16633 on the same line.
16634 @end itemize
16635
16636 @noindent
16637 The indentation of a whole-line comment is that of either
16638 the preceding or following line in
16639 the formatted source, depending on switch settings as will be described below.
16640
16641 For an end-of-line comment, @command{gnatpp} leaves the same number of spaces
16642 between the end of the preceding Ada lexical element and the beginning
16643 of the comment as appear in the original source,
16644 unless either the comment has to be split to
16645 satisfy the line length limitation, or else the next line contains a
16646 whole line comment that is considered a continuation of this end-of-line
16647 comment (because it starts at the same position).
16648 In the latter two
16649 cases, the start of the end-of-line comment is moved right to the nearest
16650 multiple of the indentation level.
16651 This may result in a ``line overflow'' (the right-shifted comment extending
16652 beyond the maximum line length), in which case the comment is split as
16653 described below.
16654
16655 There is a difference between @option{^-c1^/COMMENTS_LAYOUT=DEFAULT^}
16656 (GNAT-style comment line indentation)
16657 and @option{^-c2^/COMMENTS_LAYOUT=STANDARD_INDENT^}
16658 (reference-manual comment line indentation).
16659 With reference-manual style, a whole-line comment is indented as if it
16660 were a declaration or statement at the same place
16661 (i.e., according to the indentation of the preceding line(s)).
16662 With GNAT style, a whole-line comment that is immediately followed by an
16663 @b{if} or @b{case} statement alternative, a record variant, or the reserved
16664 word @b{begin}, is indented based on the construct that follows it.
16665
16666 For example:
16667 @smallexample @c ada
16668 @cartouche
16669 if A then
16670 null;
16671 -- some comment
16672 else
16673 null;
16674 end if;
16675 @end cartouche
16676 @end smallexample
16677
16678 @noindent
16679 Reference-manual indentation produces:
16680
16681 @smallexample @c ada
16682 @cartouche
16683 if A then
16684 null;
16685 -- some comment
16686 else
16687 null;
16688 end if;
16689 @end cartouche
16690 @end smallexample
16691
16692 @noindent
16693 while GNAT-style indentation produces:
16694
16695 @smallexample @c ada
16696 @cartouche
16697 if A then
16698 null;
16699 -- some comment
16700 else
16701 null;
16702 end if;
16703 @end cartouche
16704 @end smallexample
16705
16706 @noindent
16707 The @option{^-c3^/COMMENTS_LAYOUT=GNAT_BEGINNING^} switch
16708 (GNAT style comment beginning) has the following
16709 effect:
16710
16711 @itemize @bullet
16712 @item
16713 For each whole-line comment that does not end with two hyphens,
16714 @command{gnatpp} inserts spaces if necessary after the starting two hyphens
16715 to ensure that there are at least two spaces between these hyphens and the
16716 first non-blank character of the comment.
16717 @end itemize
16718
16719 @noindent
16720 For an end-of-line comment, if in the original source the next line is a
16721 whole-line comment that starts at the same position
16722 as the end-of-line comment,
16723 then the whole-line comment (and all whole-line comments
16724 that follow it and that start at the same position)
16725 will start at this position in the output file.
16726
16727 @noindent
16728 That is, if in the original source we have:
16729
16730 @smallexample @c ada
16731 @cartouche
16732 begin
16733 A := B + C; -- B must be in the range Low1..High1
16734 -- C must be in the range Low2..High2
16735 --B+C will be in the range Low1+Low2..High1+High2
16736 X := X + 1;
16737 @end cartouche
16738 @end smallexample
16739
16740 @noindent
16741 Then in the formatted source we get
16742
16743 @smallexample @c ada
16744 @cartouche
16745 begin
16746 A := B + C; -- B must be in the range Low1..High1
16747 -- C must be in the range Low2..High2
16748 -- B+C will be in the range Low1+Low2..High1+High2
16749 X := X + 1;
16750 @end cartouche
16751 @end smallexample
16752
16753 @noindent
16754 A comment that exceeds the line length limit will be split.
16755 Unless switch
16756 @option{^-c4^/COMMENTS_LAYOUT=REFORMAT^} (reformat comment blocks) is set and
16757 the line belongs to a reformattable block, splitting the line generates a
16758 @command{gnatpp} warning.
16759 The @option{^-c4^/COMMENTS_LAYOUT=REFORMAT^} switch specifies that whole-line
16760 comments may be reformatted in typical
16761 word processor style (that is, moving words between lines and putting as
16762 many words in a line as possible).
16763
16764 @noindent
16765 The @option{^-c5^/COMMENTS_LAYOUT=KEEP_SPECIAL^} switch specifies, that comments
16766 that has a special format (that is, a character that is neither a letter nor digit
16767 not white space nor line break immediately following the leading @code{--} of
16768 the comment) should be without any change moved from the argument source
16769 into reformatted source. This switch allows to preserve comments that are used
16770 as a special marks in the code (e.g.@: SPARK annotation).
16771
16772 @node Construct Layout
16773 @subsection Construct Layout
16774
16775 @noindent
16776 In several cases the suggested layout in the Ada Reference Manual includes
16777 an extra level of indentation that many programmers prefer to avoid. The
16778 affected cases include:
16779
16780 @itemize @bullet
16781
16782 @item Record type declaration (RM 3.8)
16783
16784 @item Record representation clause (RM 13.5.1)
16785
16786 @item Loop statement in case if a loop has a statement identifier (RM 5.6)
16787
16788 @item Block statement in case if a block has a statement identifier (RM 5.6)
16789 @end itemize
16790
16791 @noindent
16792 In compact mode (when GNAT style layout or compact layout is set),
16793 the pretty printer uses one level of indentation instead
16794 of two. This is achieved in the record definition and record representation
16795 clause cases by putting the @code{record} keyword on the same line as the
16796 start of the declaration or representation clause, and in the block and loop
16797 case by putting the block or loop header on the same line as the statement
16798 identifier.
16799
16800 @noindent
16801 The difference between GNAT style @option{^-l1^/CONSTRUCT_LAYOUT=GNAT^}
16802 and compact @option{^-l2^/CONSTRUCT_LAYOUT=COMPACT^}
16803 layout on the one hand, and uncompact layout
16804 @option{^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^} on the other hand,
16805 can be illustrated by the following examples:
16806
16807 @iftex
16808 @cartouche
16809 @multitable @columnfractions .5 .5
16810 @item @i{GNAT style, compact layout} @tab @i{Uncompact layout}
16811
16812 @item
16813 @smallexample @c ada
16814 type q is record
16815 a : integer;
16816 b : integer;
16817 end record;
16818 @end smallexample
16819 @tab
16820 @smallexample @c ada
16821 type q is
16822 record
16823 a : integer;
16824 b : integer;
16825 end record;
16826 @end smallexample
16827
16828 @item
16829 @smallexample @c ada
16830 for q use record
16831 a at 0 range 0 .. 31;
16832 b at 4 range 0 .. 31;
16833 end record;
16834 @end smallexample
16835 @tab
16836 @smallexample @c ada
16837 for q use
16838 record
16839 a at 0 range 0 .. 31;
16840 b at 4 range 0 .. 31;
16841 end record;
16842 @end smallexample
16843
16844 @item
16845 @smallexample @c ada
16846 Block : declare
16847 A : Integer := 3;
16848 begin
16849 Proc (A, A);
16850 end Block;
16851 @end smallexample
16852 @tab
16853 @smallexample @c ada
16854 Block :
16855 declare
16856 A : Integer := 3;
16857 begin
16858 Proc (A, A);
16859 end Block;
16860 @end smallexample
16861
16862 @item
16863 @smallexample @c ada
16864 Clear : for J in 1 .. 10 loop
16865 A (J) := 0;
16866 end loop Clear;
16867 @end smallexample
16868 @tab
16869 @smallexample @c ada
16870 Clear :
16871 for J in 1 .. 10 loop
16872 A (J) := 0;
16873 end loop Clear;
16874 @end smallexample
16875 @end multitable
16876 @end cartouche
16877 @end iftex
16878
16879 @ifnottex
16880 @smallexample
16881 @cartouche
16882 GNAT style, compact layout Uncompact layout
16883
16884 type q is record type q is
16885 a : integer; record
16886 b : integer; a : integer;
16887 end record; b : integer;
16888 end record;
16889
16890 for q use record for q use
16891 a at 0 range 0 .. 31; record
16892 b at 4 range 0 .. 31; a at 0 range 0 .. 31;
16893 end record; b at 4 range 0 .. 31;
16894 end record;
16895
16896 Block : declare Block :
16897 A : Integer := 3; declare
16898 begin A : Integer := 3;
16899 Proc (A, A); begin
16900 end Block; Proc (A, A);
16901 end Block;
16902
16903 Clear : for J in 1 .. 10 loop Clear :
16904 A (J) := 0; for J in 1 .. 10 loop
16905 end loop Clear; A (J) := 0;
16906 end loop Clear;
16907 @end cartouche
16908 @end smallexample
16909 @end ifnottex
16910
16911 @noindent
16912 A further difference between GNAT style layout and compact layout is that
16913 GNAT style layout inserts empty lines as separation for
16914 compound statements, return statements and bodies.
16915
16916 Note that the layout specified by
16917 @option{^--separate-stmt-name^/STMT_NAME_ON_NEW_LINE^}
16918 for named block and loop statements overrides the layout defined by these
16919 constructs by @option{^-l1^/CONSTRUCT_LAYOUT=GNAT^},
16920 @option{^-l2^/CONSTRUCT_LAYOUT=COMPACT^} or
16921 @option{^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^} option.
16922
16923 @node Name Casing
16924 @subsection Name Casing
16925
16926 @noindent
16927 @command{gnatpp} always converts the usage occurrence of a (simple) name to
16928 the same casing as the corresponding defining identifier.
16929
16930 You control the casing for defining occurrences via the
16931 @option{^-n^/NAME_CASING^} switch.
16932 @ifclear vms
16933 With @option{-nD} (``as declared'', which is the default),
16934 @end ifclear
16935 @ifset vms
16936 With @option{/NAME_CASING=AS_DECLARED}, which is the default,
16937 @end ifset
16938 defining occurrences appear exactly as in the source file
16939 where they are declared.
16940 The other ^values for this switch^options for this qualifier^ ---
16941 @option{^-nU^UPPER_CASE^},
16942 @option{^-nL^LOWER_CASE^},
16943 @option{^-nM^MIXED_CASE^} ---
16944 result in
16945 ^upper, lower, or mixed case, respectively^the corresponding casing^.
16946 If @command{gnatpp} changes the casing of a defining
16947 occurrence, it analogously changes the casing of all the
16948 usage occurrences of this name.
16949
16950 If the defining occurrence of a name is not in the source compilation unit
16951 currently being processed by @command{gnatpp}, the casing of each reference to
16952 this name is changed according to the value of the @option{^-n^/NAME_CASING^}
16953 switch (subject to the dictionary file mechanism described below).
16954 Thus @command{gnatpp} acts as though the @option{^-n^/NAME_CASING^} switch
16955 had affected the
16956 casing for the defining occurrence of the name.
16957
16958 Some names may need to be spelled with casing conventions that are not
16959 covered by the upper-, lower-, and mixed-case transformations.
16960 You can arrange correct casing by placing such names in a
16961 @emph{dictionary file},
16962 and then supplying a @option{^-D^/DICTIONARY^} switch.
16963 The casing of names from dictionary files overrides
16964 any @option{^-n^/NAME_CASING^} switch.
16965
16966 To handle the casing of Ada predefined names and the names from GNAT libraries,
16967 @command{gnatpp} assumes a default dictionary file.
16968 The name of each predefined entity is spelled with the same casing as is used
16969 for the entity in the @cite{Ada Reference Manual}.
16970 The name of each entity in the GNAT libraries is spelled with the same casing
16971 as is used in the declaration of that entity.
16972
16973 The @w{@option{^-D-^/SPECIFIC_CASING^}} switch suppresses the use of the
16974 default dictionary file.
16975 Instead, the casing for predefined and GNAT-defined names will be established
16976 by the @option{^-n^/NAME_CASING^} switch or explicit dictionary files.
16977 For example, by default the names @code{Ada.Text_IO} and @code{GNAT.OS_Lib}
16978 will appear as just shown,
16979 even in the presence of a @option{^-nU^/NAME_CASING=UPPER_CASE^} switch.
16980 To ensure that even such names are rendered in uppercase,
16981 additionally supply the @w{@option{^-D-^/SPECIFIC_CASING^}} switch
16982 (or else, less conveniently, place these names in upper case in a dictionary
16983 file).
16984
16985 A dictionary file is
16986 a plain text file; each line in this file can be either a blank line
16987 (containing only space characters and ASCII.HT characters), an Ada comment
16988 line, or the specification of exactly one @emph{casing schema}.
16989
16990 A casing schema is a string that has the following syntax:
16991
16992 @smallexample
16993 @cartouche
16994 @var{casing_schema} ::= @var{identifier} | *@var{simple_identifier}*
16995
16996 @var{simple_identifier} ::= @var{letter}@{@var{letter_or_digit}@}
16997 @end cartouche
16998 @end smallexample
16999
17000 @noindent
17001 (See @cite{Ada Reference Manual}, Section 2.3) for the definition of the
17002 @var{identifier} lexical element and the @var{letter_or_digit} category.)
17003
17004 The casing schema string can be followed by white space and/or an Ada-style
17005 comment; any amount of white space is allowed before the string.
17006
17007 If a dictionary file is passed as
17008 @ifclear vms
17009 the value of a @option{-D@var{file}} switch
17010 @end ifclear
17011 @ifset vms
17012 an option to the @option{/DICTIONARY} qualifier
17013 @end ifset
17014 then for every
17015 simple name and every identifier, @command{gnatpp} checks if the dictionary
17016 defines the casing for the name or for some of its parts (the term ``subword''
17017 is used below to denote the part of a name which is delimited by ``_'' or by
17018 the beginning or end of the word and which does not contain any ``_'' inside):
17019
17020 @itemize @bullet
17021 @item
17022 if the whole name is in the dictionary, @command{gnatpp} uses for this name
17023 the casing defined by the dictionary; no subwords are checked for this word
17024
17025 @item
17026 for every subword @command{gnatpp} checks if the dictionary contains the
17027 corresponding string of the form @code{*@var{simple_identifier}*},
17028 and if it does, the casing of this @var{simple_identifier} is used
17029 for this subword
17030
17031 @item
17032 if the whole name does not contain any ``_'' inside, and if for this name
17033 the dictionary contains two entries - one of the form @var{identifier},
17034 and another - of the form *@var{simple_identifier}*, then the first one
17035 is applied to define the casing of this name
17036
17037 @item
17038 if more than one dictionary file is passed as @command{gnatpp} switches, each
17039 dictionary adds new casing exceptions and overrides all the existing casing
17040 exceptions set by the previous dictionaries
17041
17042 @item
17043 when @command{gnatpp} checks if the word or subword is in the dictionary,
17044 this check is not case sensitive
17045 @end itemize
17046
17047 @noindent
17048 For example, suppose we have the following source to reformat:
17049
17050 @smallexample @c ada
17051 @cartouche
17052 procedure test is
17053 name1 : integer := 1;
17054 name4_name3_name2 : integer := 2;
17055 name2_name3_name4 : Boolean;
17056 name1_var : Float;
17057 begin
17058 name2_name3_name4 := name4_name3_name2 > name1;
17059 end;
17060 @end cartouche
17061 @end smallexample
17062
17063 @noindent
17064 And suppose we have two dictionaries:
17065
17066 @smallexample
17067 @cartouche
17068 @i{dict1:}
17069 NAME1
17070 *NaMe3*
17071 *Name1*
17072 @end cartouche
17073
17074 @cartouche
17075 @i{dict2:}
17076 *NAME3*
17077 @end cartouche
17078 @end smallexample
17079
17080 @noindent
17081 If @command{gnatpp} is called with the following switches:
17082
17083 @smallexample
17084 @ifclear vms
17085 @command{gnatpp -nM -D dict1 -D dict2 test.adb}
17086 @end ifclear
17087 @ifset vms
17088 @command{gnatpp test.adb /NAME_CASING=MIXED_CASE /DICTIONARY=(dict1, dict2)}
17089 @end ifset
17090 @end smallexample
17091
17092 @noindent
17093 then we will get the following name casing in the @command{gnatpp} output:
17094
17095 @smallexample @c ada
17096 @cartouche
17097 procedure Test is
17098 NAME1 : Integer := 1;
17099 Name4_NAME3_Name2 : Integer := 2;
17100 Name2_NAME3_Name4 : Boolean;
17101 Name1_Var : Float;
17102 begin
17103 Name2_NAME3_Name4 := Name4_NAME3_Name2 > NAME1;
17104 end Test;
17105 @end cartouche
17106 @end smallexample
17107
17108 @c *********************************
17109 @node The GNAT Metric Tool gnatmetric
17110 @chapter The GNAT Metric Tool @command{gnatmetric}
17111 @findex gnatmetric
17112 @cindex Metric tool
17113
17114 @noindent
17115 ^The @command{gnatmetric} tool^@command{GNAT METRIC}^ is an ASIS-based utility
17116 for computing various program metrics.
17117 It takes an Ada source file as input and generates a file containing the
17118 metrics data as output. Various switches control which
17119 metrics are computed and output.
17120
17121 @command{gnatmetric} generates and uses the ASIS
17122 tree for the input source and thus requires the input to be syntactically and
17123 semantically legal.
17124 If this condition is not met, @command{gnatmetric} will generate
17125 an error message; no metric information for this file will be
17126 computed and reported.
17127
17128 If the compilation unit contained in the input source depends semantically
17129 upon units in files located outside the current directory, you have to provide
17130 the source search path when invoking @command{gnatmetric}.
17131 If it depends semantically upon units that are contained
17132 in files with names that do not follow the GNAT file naming rules, you have to
17133 provide the configuration file describing the corresponding naming scheme (see
17134 the description of the @command{gnatmetric} switches below.)
17135 Alternatively, you may use a project file and invoke @command{gnatmetric}
17136 through the @command{gnat} driver.
17137
17138 The @command{gnatmetric} command has the form
17139
17140 @smallexample
17141 $ gnatmetric @ovar{switches} @{@var{filename}@} @r{[}-cargs @var{gcc_switches}@r{]}
17142 @end smallexample
17143
17144 @noindent
17145 where
17146 @itemize @bullet
17147 @item
17148 @var{switches} specify the metrics to compute and define the destination for
17149 the output
17150
17151 @item
17152 Each @var{filename} is the name (including the extension) of a source
17153 file to process. ``Wildcards'' are allowed, and
17154 the file name may contain path information.
17155 If no @var{filename} is supplied, then the @var{switches} list must contain
17156 at least one
17157 @option{-files} switch (@pxref{Other gnatmetric Switches}).
17158 Including both a @option{-files} switch and one or more
17159 @var{filename} arguments is permitted.
17160
17161 @item
17162 @samp{-cargs @var{gcc_switches}} is a list of switches for
17163 @command{gcc}. They will be passed on to all compiler invocations made by
17164 @command{gnatmetric} to generate the ASIS trees. Here you can provide
17165 @option{^-I^/INCLUDE_DIRS=^} switches to form the source search path,
17166 and use the @option{-gnatec} switch to set the configuration file.
17167 @end itemize
17168
17169 @menu
17170 * Switches for gnatmetric::
17171 @end menu
17172
17173 @node Switches for gnatmetric
17174 @section Switches for @command{gnatmetric}
17175
17176 @noindent
17177 The following subsections describe the various switches accepted by
17178 @command{gnatmetric}, organized by category.
17179
17180 @menu
17181 * Output Files Control::
17182 * Disable Metrics For Local Units::
17183 * Specifying a set of metrics to compute::
17184 * Other gnatmetric Switches::
17185 * Generate project-wide metrics::
17186 @end menu
17187
17188 @node Output Files Control
17189 @subsection Output File Control
17190 @cindex Output file control in @command{gnatmetric}
17191
17192 @noindent
17193 @command{gnatmetric} has two output formats. It can generate a
17194 textual (human-readable) form, and also XML. By default only textual
17195 output is generated.
17196
17197 When generating the output in textual form, @command{gnatmetric} creates
17198 for each Ada source file a corresponding text file
17199 containing the computed metrics, except for the case when the set of metrics
17200 specified by gnatmetric parameters consists only of metrics that are computed
17201 for the whole set of analyzed sources, but not for each Ada source.
17202 By default, this file is placed in the same directory as where the source
17203 file is located, and its name is obtained
17204 by appending the ^@file{.metrix}^@file{$METRIX}^ suffix to the name of the
17205 input file.
17206
17207 All the output information generated in XML format is placed in a single
17208 file. By default this file is placed in the current directory and has the
17209 name ^@file{metrix.xml}^@file{METRIX$XML}^.
17210
17211 Some of the computed metrics are summed over the units passed to
17212 @command{gnatmetric}; for example, the total number of lines of code.
17213 By default this information is sent to @file{stdout}, but a file
17214 can be specified with the @option{-og} switch.
17215
17216 The following switches control the @command{gnatmetric} output:
17217
17218 @table @option
17219 @cindex @option{^-x^/XML^} (@command{gnatmetric})
17220 @item ^-x^/XML^
17221 Generate the XML output
17222
17223 @cindex @option{^-xs^/XSD^} (@command{gnatmetric})
17224 @item ^-xs^/XSD^
17225 Generate the XML output and the XML schema file that describes the structure
17226 of the XML metric report, this schema is assigned to the XML file. The schema
17227 file has the same name as the XML output file with @file{.xml} suffix replaced
17228 with @file{.xsd}
17229
17230 @cindex @option{^-nt^/NO_TEXT^} (@command{gnatmetric})
17231 @item ^-nt^/NO_TEXT^
17232 Do not generate the output in text form (implies @option{^-x^/XML^})
17233
17234 @cindex @option{^-d^/DIRECTORY^} (@command{gnatmetric})
17235 @item ^-d @var{output_dir}^/DIRECTORY=@var{output_dir}^
17236 Put textual files with detailed metrics into @var{output_dir}
17237
17238 @cindex @option{^-o^/SUFFIX_DETAILS^} (@command{gnatmetric})
17239 @item ^-o @var{file_suffix}^/SUFFIX_DETAILS=@var{file_suffix}^
17240 Use @var{file_suffix}, instead of ^@file{.metrix}^@file{$METRIX}^
17241 in the name of the output file.
17242
17243 @cindex @option{^-og^/GLOBAL_OUTPUT^} (@command{gnatmetric})
17244 @item ^-og @var{file_name}^/GLOBAL_OUTPUT=@var{file_name}^
17245 Put global metrics into @var{file_name}
17246
17247 @cindex @option{^-ox^/XML_OUTPUT^} (@command{gnatmetric})
17248 @item ^-ox @var{file_name}^/XML_OUTPUT=@var{file_name}^
17249 Put the XML output into @var{file_name} (also implies @option{^-x^/XML^})
17250
17251 @cindex @option{^-sfn^/SHORT_SOURCE_FILE_NAME^} (@command{gnatmetric})
17252 @item ^-sfn^/SHORT_SOURCE_FILE_NAME^
17253 Use ``short'' source file names in the output. (The @command{gnatmetric}
17254 output includes the name(s) of the Ada source file(s) from which the metrics
17255 are computed. By default each name includes the absolute path. The
17256 @option{^-sfn^/SHORT_SOURCE_FILE_NAME^} switch causes @command{gnatmetric}
17257 to exclude all directory information from the file names that are output.)
17258
17259 @end table
17260
17261 @node Disable Metrics For Local Units
17262 @subsection Disable Metrics For Local Units
17263 @cindex Disable Metrics For Local Units in @command{gnatmetric}
17264
17265 @noindent
17266 @command{gnatmetric} relies on the GNAT compilation model @minus{}
17267 one compilation
17268 unit per one source file. It computes line metrics for the whole source
17269 file, and it also computes syntax
17270 and complexity metrics for the file's outermost unit.
17271
17272 By default, @command{gnatmetric} will also compute all metrics for certain
17273 kinds of locally declared program units:
17274
17275 @itemize @bullet
17276 @item
17277 subprogram (and generic subprogram) bodies;
17278
17279 @item
17280 package (and generic package) specs and bodies;
17281
17282 @item
17283 task object and type specifications and bodies;
17284
17285 @item
17286 protected object and type specifications and bodies.
17287 @end itemize
17288
17289 @noindent
17290 These kinds of entities will be referred to as
17291 @emph{eligible local program units}, or simply @emph{eligible local units},
17292 @cindex Eligible local unit (for @command{gnatmetric})
17293 in the discussion below.
17294
17295 Note that a subprogram declaration, generic instantiation,
17296 or renaming declaration only receives metrics
17297 computation when it appear as the outermost entity
17298 in a source file.
17299
17300 Suppression of metrics computation for eligible local units can be
17301 obtained via the following switch:
17302
17303 @table @option
17304 @cindex @option{^-n@var{x}^/SUPPRESS^} (@command{gnatmetric})
17305 @item ^-nolocal^/SUPPRESS=LOCAL_DETAILS^
17306 Do not compute detailed metrics for eligible local program units
17307
17308 @end table
17309
17310 @node Specifying a set of metrics to compute
17311 @subsection Specifying a set of metrics to compute
17312
17313 @noindent
17314 By default all the metrics are computed and reported. The switches
17315 described in this subsection allow you to control, on an individual
17316 basis, whether metrics are computed and
17317 reported. If at least one positive metric
17318 switch is specified (that is, a switch that defines that a given
17319 metric or set of metrics is to be computed), then only
17320 explicitly specified metrics are reported.
17321
17322 @menu
17323 * Line Metrics Control::
17324 * Syntax Metrics Control::
17325 * Complexity Metrics Control::
17326 * Object-Oriented Metrics Control::
17327 @end menu
17328
17329 @node Line Metrics Control
17330 @subsubsection Line Metrics Control
17331 @cindex Line metrics control in @command{gnatmetric}
17332
17333 @noindent
17334 For any (legal) source file, and for each of its
17335 eligible local program units, @command{gnatmetric} computes the following
17336 metrics:
17337
17338 @itemize @bullet
17339 @item
17340 the total number of lines;
17341
17342 @item
17343 the total number of code lines (i.e., non-blank lines that are not comments)
17344
17345 @item
17346 the number of comment lines
17347
17348 @item
17349 the number of code lines containing end-of-line comments;
17350
17351 @item
17352 the comment percentage: the ratio between the number of lines that contain
17353 comments and the number of all non-blank lines, expressed as a percentage;
17354
17355 @item
17356 the number of empty lines and lines containing only space characters and/or
17357 format effectors (blank lines)
17358
17359 @item
17360 the average number of code lines in subprogram bodies, task bodies, entry
17361 bodies and statement sequences in package bodies (this metric is only computed
17362 across the whole set of the analyzed units)
17363
17364 @end itemize
17365
17366 @noindent
17367 @command{gnatmetric} sums the values of the line metrics for all the
17368 files being processed and then generates the cumulative results. The tool
17369 also computes for all the files being processed the average number of code
17370 lines in bodies.
17371
17372 You can use the following switches to select the specific line metrics
17373 to be computed and reported.
17374
17375 @table @option
17376 @cindex @option{^--lines@var{x}^/LINE_COUNT_METRICS^} (@command{gnatmetric})
17377
17378 @ifclear vms
17379 @cindex @option{--no-lines@var{x}}
17380 @end ifclear
17381
17382 @item ^--lines-all^/LINE_COUNT_METRICS=ALL^
17383 Report all the line metrics
17384
17385 @item ^--no-lines-all^/LINE_COUNT_METRICS=NONE^
17386 Do not report any of line metrics
17387
17388 @item ^--lines^/LINE_COUNT_METRICS=ALL_LINES^
17389 Report the number of all lines
17390
17391 @item ^--no-lines^/LINE_COUNT_METRICS=NOALL_LINES^
17392 Do not report the number of all lines
17393
17394 @item ^--lines-code^/LINE_COUNT_METRICS=CODE_LINES^
17395 Report the number of code lines
17396
17397 @item ^--no-lines-code^/LINE_COUNT_METRICS=NOCODE_LINES^
17398 Do not report the number of code lines
17399
17400 @item ^--lines-comment^/LINE_COUNT_METRICS=COMMENT_LINES^
17401 Report the number of comment lines
17402
17403 @item ^--no-lines-comment^/LINE_COUNT_METRICS=NOCOMMENT_LINES^
17404 Do not report the number of comment lines
17405
17406 @item ^--lines-eol-comment^/LINE_COUNT_METRICS=CODE_COMMENT_LINES^
17407 Report the number of code lines containing
17408 end-of-line comments
17409
17410 @item ^--no-lines-eol-comment^/LINE_COUNT_METRICS=NOCODE_COMMENT_LINES^
17411 Do not report the number of code lines containing
17412 end-of-line comments
17413
17414 @item ^--lines-ratio^/LINE_COUNT_METRICS=COMMENT_PERCENTAGE^
17415 Report the comment percentage in the program text
17416
17417 @item ^--no-lines-ratio^/LINE_COUNT_METRICS=NOCOMMENT_PERCENTAGE^
17418 Do not report the comment percentage in the program text
17419
17420 @item ^--lines-blank^/LINE_COUNT_METRICS=BLANK_LINES^
17421 Report the number of blank lines
17422
17423 @item ^--no-lines-blank^/LINE_COUNT_METRICS=NOBLANK_LINES^
17424 Do not report the number of blank lines
17425
17426 @item ^--lines-average^/LINE_COUNT_METRICS=AVERAGE_BODY_LINES^
17427 Report the average number of code lines in subprogram bodies, task bodies,
17428 entry bodies and statement sequences in package bodies. The metric is computed
17429 and reported for the whole set of processed Ada sources only.
17430
17431 @item ^--no-lines-average^/LINE_COUNT_METRICS=NOAVERAGE_BODY_LINES^
17432 Do not report the average number of code lines in subprogram bodies,
17433 task bodies, entry bodies and statement sequences in package bodies.
17434
17435 @end table
17436
17437 @node Syntax Metrics Control
17438 @subsubsection Syntax Metrics Control
17439 @cindex Syntax metrics control in @command{gnatmetric}
17440
17441 @noindent
17442 @command{gnatmetric} computes various syntactic metrics for the
17443 outermost unit and for each eligible local unit:
17444
17445 @table @emph
17446 @item LSLOC (``Logical Source Lines Of Code'')
17447 The total number of declarations and the total number of statements
17448
17449 @item Maximal static nesting level of inner program units
17450 According to
17451 @cite{Ada Reference Manual}, 10.1(1), ``A program unit is either a
17452 package, a task unit, a protected unit, a
17453 protected entry, a generic unit, or an explicitly declared subprogram other
17454 than an enumeration literal.''
17455
17456 @item Maximal nesting level of composite syntactic constructs
17457 This corresponds to the notion of the
17458 maximum nesting level in the GNAT built-in style checks
17459 (@pxref{Style Checking})
17460 @end table
17461
17462 @noindent
17463 For the outermost unit in the file, @command{gnatmetric} additionally computes
17464 the following metrics:
17465
17466 @table @emph
17467 @item Public subprograms
17468 This metric is computed for package specs. It is the
17469 number of subprograms and generic subprograms declared in the visible
17470 part (including the visible part of nested packages, protected objects, and
17471 protected types).
17472
17473 @item All subprograms
17474 This metric is computed for bodies and subunits. The
17475 metric is equal to a total number of subprogram bodies in the compilation
17476 unit.
17477 Neither generic instantiations nor renamings-as-a-body nor body stubs
17478 are counted. Any subprogram body is counted, independently of its nesting
17479 level and enclosing constructs. Generic bodies and bodies of protected
17480 subprograms are counted in the same way as ``usual'' subprogram bodies.
17481
17482 @item Public types
17483 This metric is computed for package specs and
17484 generic package declarations. It is the total number of types
17485 that can be referenced from outside this compilation unit, plus the
17486 number of types from all the visible parts of all the visible generic
17487 packages. Generic formal types are not counted. Only types, not subtypes,
17488 are included.
17489
17490 @noindent
17491 Along with the total number of public types, the following
17492 types are counted and reported separately:
17493
17494 @itemize @bullet
17495 @item
17496 Abstract types
17497
17498 @item
17499 Root tagged types (abstract, non-abstract, private, non-private). Type
17500 extensions are @emph{not} counted
17501
17502 @item
17503 Private types (including private extensions)
17504
17505 @item
17506 Task types
17507
17508 @item
17509 Protected types
17510
17511 @end itemize
17512
17513 @item All types
17514 This metric is computed for any compilation unit. It is equal to the total
17515 number of the declarations of different types given in the compilation unit.
17516 The private and the corresponding full type declaration are counted as one
17517 type declaration. Incomplete type declarations and generic formal types
17518 are not counted.
17519 No distinction is made among different kinds of types (abstract,
17520 private etc.); the total number of types is computed and reported.
17521
17522 @end table
17523
17524 @noindent
17525 By default, all the syntax metrics are computed and reported. You can use the
17526 following switches to select specific syntax metrics.
17527
17528 @table @option
17529
17530 @cindex @option{^--syntax@var{x}^/SYNTAX_METRICS^} (@command{gnatmetric})
17531
17532 @ifclear vms
17533 @cindex @option{--no-syntax@var{x}} (@command{gnatmetric})
17534 @end ifclear
17535
17536 @item ^--syntax-all^/SYNTAX_METRICS=ALL^
17537 Report all the syntax metrics
17538
17539 @item ^--no-syntax-all^/SYNTAX_METRICS=NONE^
17540 Do not report any of syntax metrics
17541
17542 @item ^--declarations^/SYNTAX_METRICS=DECLARATIONS^
17543 Report the total number of declarations
17544
17545 @item ^--no-declarations^/SYNTAX_METRICS=NODECLARATIONS^
17546 Do not report the total number of declarations
17547
17548 @item ^--statements^/SYNTAX_METRICS=STATEMENTS^
17549 Report the total number of statements
17550
17551 @item ^--no-statements^/SYNTAX_METRICS=NOSTATEMENTS^
17552 Do not report the total number of statements
17553
17554 @item ^--public-subprograms^/SYNTAX_METRICS=PUBLIC_SUBPROGRAMS^
17555 Report the number of public subprograms in a compilation unit
17556
17557 @item ^--no-public-subprograms^/SYNTAX_METRICS=NOPUBLIC_SUBPROGRAMS^
17558 Do not report the number of public subprograms in a compilation unit
17559
17560 @item ^--all-subprograms^/SYNTAX_METRICS=ALL_SUBPROGRAMS^
17561 Report the number of all the subprograms in a compilation unit
17562
17563 @item ^--no-all-subprograms^/SYNTAX_METRICS=NOALL_SUBPROGRAMS^
17564 Do not report the number of all the subprograms in a compilation unit
17565
17566 @item ^--public-types^/SYNTAX_METRICS=PUBLIC_TYPES^
17567 Report the number of public types in a compilation unit
17568
17569 @item ^--no-public-types^/SYNTAX_METRICS=NOPUBLIC_TYPES^
17570 Do not report the number of public types in a compilation unit
17571
17572 @item ^--all-types^/SYNTAX_METRICS=ALL_TYPES^
17573 Report the number of all the types in a compilation unit
17574
17575 @item ^--no-all-types^/SYNTAX_METRICS=NOALL_TYPES^
17576 Do not report the number of all the types in a compilation unit
17577
17578 @item ^--unit-nesting^/SYNTAX_METRICS=UNIT_NESTING^
17579 Report the maximal program unit nesting level
17580
17581 @item ^--no-unit-nesting^/SYNTAX_METRICS=UNIT_NESTING_OFF^
17582 Do not report the maximal program unit nesting level
17583
17584 @item ^--construct-nesting^/SYNTAX_METRICS=CONSTRUCT_NESTING^
17585 Report the maximal construct nesting level
17586
17587 @item ^--no-construct-nesting^/SYNTAX_METRICS=NOCONSTRUCT_NESTING^
17588 Do not report the maximal construct nesting level
17589
17590 @end table
17591
17592 @node Complexity Metrics Control
17593 @subsubsection Complexity Metrics Control
17594 @cindex Complexity metrics control in @command{gnatmetric}
17595
17596 @noindent
17597 For a program unit that is an executable body (a subprogram body (including
17598 generic bodies), task body, entry body or a package body containing
17599 its own statement sequence) @command{gnatmetric} computes the following
17600 complexity metrics:
17601
17602 @itemize @bullet
17603 @item
17604 McCabe cyclomatic complexity;
17605
17606 @item
17607 McCabe essential complexity;
17608
17609 @item
17610 maximal loop nesting level
17611
17612 @end itemize
17613
17614 @noindent
17615 The McCabe complexity metrics are defined
17616 in @url{http://www.mccabe.com/pdf/nist235r.pdf}
17617
17618 According to McCabe, both control statements and short-circuit control forms
17619 should be taken into account when computing cyclomatic complexity. For each
17620 body, we compute three metric values:
17621
17622 @itemize @bullet
17623 @item
17624 the complexity introduced by control
17625 statements only, without taking into account short-circuit forms,
17626
17627 @item
17628 the complexity introduced by short-circuit control forms only, and
17629
17630 @item
17631 the total
17632 cyclomatic complexity, which is the sum of these two values.
17633 @end itemize
17634
17635 @noindent
17636 When computing cyclomatic and essential complexity, @command{gnatmetric} skips
17637 the code in the exception handlers and in all the nested program units.
17638
17639 By default, all the complexity metrics are computed and reported.
17640 For more fine-grained control you can use
17641 the following switches:
17642
17643 @table @option
17644 @cindex @option{^-complexity@var{x}^/COMPLEXITY_METRICS^} (@command{gnatmetric})
17645
17646 @ifclear vms
17647 @cindex @option{--no-complexity@var{x}}
17648 @end ifclear
17649
17650 @item ^--complexity-all^/COMPLEXITY_METRICS=ALL^
17651 Report all the complexity metrics
17652
17653 @item ^--no-complexity-all^/COMPLEXITY_METRICS=NONE^
17654 Do not report any of complexity metrics
17655
17656 @item ^--complexity-cyclomatic^/COMPLEXITY_METRICS=CYCLOMATIC^
17657 Report the McCabe Cyclomatic Complexity
17658
17659 @item ^--no-complexity-cyclomatic^/COMPLEXITY_METRICS=NOCYCLOMATIC^
17660 Do not report the McCabe Cyclomatic Complexity
17661
17662 @item ^--complexity-essential^/COMPLEXITY_METRICS=ESSENTIAL^
17663 Report the Essential Complexity
17664
17665 @item ^--no-complexity-essential^/COMPLEXITY_METRICS=NOESSENTIAL^
17666 Do not report the Essential Complexity
17667
17668 @item ^--loop-nesting^/COMPLEXITY_METRICS=LOOP_NESTING_ON^
17669 Report maximal loop nesting level
17670
17671 @item ^--no-loop-nesting^/COMPLEXITY_METRICS=NOLOOP_NESTING^
17672 Do not report maximal loop nesting level
17673
17674 @item ^--complexity-average^/COMPLEXITY_METRICS=AVERAGE_COMPLEXITY^
17675 Report the average McCabe Cyclomatic Complexity for all the subprogram bodies,
17676 task bodies, entry bodies and statement sequences in package bodies.
17677 The metric is computed and reported for whole set of processed Ada sources
17678 only.
17679
17680 @item ^--no-complexity-average^/COMPLEXITY_METRICS=NOAVERAGE_COMPLEXITY^
17681 Do not report the average McCabe Cyclomatic Complexity for all the subprogram
17682 bodies, task bodies, entry bodies and statement sequences in package bodies
17683
17684 @cindex @option{^-ne^/NO_EXITS_AS_GOTOS^} (@command{gnatmetric})
17685 @item ^-ne^/NO_EXITS_AS_GOTOS^
17686 Do not consider @code{exit} statements as @code{goto}s when
17687 computing Essential Complexity
17688
17689 @item ^--extra-exit-points^/EXTRA_EXIT_POINTS^
17690 Report the extra exit points for subprogram bodies. As an exit point, this
17691 metric counts @code{return} statements and raise statements in case when the
17692 raised exception is not handled in the same body. In case of a function this
17693 metric subtracts 1 from the number of exit points, because a function body
17694 must contain at least one @code{return} statement.
17695
17696 @item ^--no-extra-exit-points^/NOEXTRA_EXIT_POINTS^
17697 Do not report the extra exit points for subprogram bodies
17698 @end table
17699
17700
17701 @node Object-Oriented Metrics Control
17702 @subsubsection Object-Oriented Metrics Control
17703 @cindex Object-Oriented metrics control in @command{gnatmetric}
17704
17705 @noindent
17706 @cindex Coupling metrics (in in @command{gnatmetric})
17707 Coupling metrics are object-oriented metrics that measure the
17708 dependencies between a given class (or a group of classes) and the
17709 ``external world'' (that is, the other classes in the program). In this
17710 subsection the term ``class'' is used in its
17711 traditional object-oriented programming sense
17712 (an instantiable module that contains data and/or method members).
17713 A @emph{category} (of classes)
17714 is a group of closely related classes that are reused and/or
17715 modified together.
17716
17717 A class @code{K}'s @emph{efferent coupling} is the number of classes
17718 that @code{K} depends upon.
17719 A category's efferent coupling is the number of classes outside the
17720 category that the classes inside the category depend upon.
17721
17722 A class @code{K}'s @emph{afferent coupling} is the number of classes
17723 that depend upon @code{K}.
17724 A category's afferent coupling is the number of classes outside the
17725 category that depend on classes belonging to the category.
17726
17727 Ada's implementation of the object-oriented paradigm does not use the
17728 traditional class notion, so the definition of the coupling
17729 metrics for Ada maps the class and class category notions
17730 onto Ada constructs.
17731
17732 For the coupling metrics, several kinds of modules -- a library package,
17733 a library generic package, and a library generic package instantiation --
17734 that define a tagged type or an interface type are
17735 considered to be a class. A category consists of a library package (or
17736 a library generic package) that defines a tagged or an interface type,
17737 together with all its descendant (generic) packages that define tagged
17738 or interface types. For any package counted as a class,
17739 its body and subunits (if any) are considered
17740 together with its spec when counting the dependencies, and coupling
17741 metrics are reported for spec units only. For dependencies
17742 between classes, the Ada semantic dependencies are considered.
17743 For coupling metrics, only dependencies on units that are considered as
17744 classes, are considered.
17745
17746 When computing coupling metrics, @command{gnatmetric} counts only
17747 dependencies between units that are arguments of the gnatmetric call.
17748 Coupling metrics are program-wide (or project-wide) metrics, so to
17749 get a valid result, you should call @command{gnatmetric} for
17750 the whole set of sources that make up your program. It can be done
17751 by calling @command{gnatmetric} from the GNAT driver with @option{-U}
17752 option (see See @ref{The GNAT Driver and Project Files} for details.
17753
17754 By default, all the coupling metrics are disabled. You can use the following
17755 switches to specify the coupling metrics to be computed and reported:
17756
17757 @table @option
17758
17759 @ifclear vms
17760 @cindex @option{--package@var{x}} (@command{gnatmetric})
17761 @cindex @option{--no-package@var{x}} (@command{gnatmetric})
17762 @cindex @option{--category@var{x}} (@command{gnatmetric})
17763 @cindex @option{--no-category@var{x}} (@command{gnatmetric})
17764 @end ifclear
17765
17766 @ifset vms
17767 @cindex @option{/COUPLING_METRICS} (@command{gnatmetric})
17768 @end ifset
17769
17770 @item ^--coupling-all^/COUPLING_METRICS=ALL^
17771 Report all the coupling metrics
17772
17773 @item ^--no-coupling-all^/COUPLING_METRICS=NONE^
17774 Do not report any of metrics
17775
17776 @item ^--package-efferent-coupling^/COUPLING_METRICS=PACKAGE_EFFERENT^
17777 Report package efferent coupling
17778
17779 @item ^--no-package-efferent-coupling^/COUPLING_METRICS=NOPACKAGE_EFFERENT^
17780 Do not report package efferent coupling
17781
17782 @item ^--package-afferent-coupling^/COUPLING_METRICS=PACKAGE_AFFERENT^
17783 Report package afferent coupling
17784
17785 @item ^--no-package-afferent-coupling^/COUPLING_METRICS=NOPACKAGE_AFFERENT^
17786 Do not report package afferent coupling
17787
17788 @item ^--category-efferent-coupling^/COUPLING_METRICS=CATEGORY_EFFERENT^
17789 Report category efferent coupling
17790
17791 @item ^--no-category-efferent-coupling^/COUPLING_METRICS=NOCATEGORY_EFFERENT^
17792 Do not report category efferent coupling
17793
17794 @item ^--category-afferent-coupling^/COUPLING_METRICS=CATEGORY_AFFERENT^
17795 Report category afferent coupling
17796
17797 @item ^--no-category-afferent-coupling^/COUPLING_METRICS=NOCATEGORY_AFFERENT^
17798 Do not report category afferent coupling
17799
17800 @end table
17801
17802 @node Other gnatmetric Switches
17803 @subsection Other @code{gnatmetric} Switches
17804
17805 @noindent
17806 Additional @command{gnatmetric} switches are as follows:
17807
17808 @table @option
17809 @item ^-files @var{filename}^/FILES=@var{filename}^
17810 @cindex @option{^-files^/FILES^} (@code{gnatmetric})
17811 Take the argument source files from the specified file. This file should be an
17812 ordinary text file containing file names separated by spaces or
17813 line breaks. You can use this switch more then once in the same call to
17814 @command{gnatmetric}. You also can combine this switch with
17815 an explicit list of files.
17816
17817 @item ^-v^/VERBOSE^
17818 @cindex @option{^-v^/VERBOSE^} (@code{gnatmetric})
17819 Verbose mode;
17820 @command{gnatmetric} generates version information and then
17821 a trace of sources being processed.
17822
17823 @item ^-dv^/DEBUG_OUTPUT^
17824 @cindex @option{^-dv^/DEBUG_OUTPUT^} (@code{gnatmetric})
17825 Debug mode;
17826 @command{gnatmetric} generates various messages useful to understand what
17827 happens during the metrics computation
17828
17829 @item ^-q^/QUIET^
17830 @cindex @option{^-q^/QUIET^} (@code{gnatmetric})
17831 Quiet mode.
17832 @end table
17833
17834 @node Generate project-wide metrics
17835 @subsection Generate project-wide metrics
17836
17837 In order to compute metrics on all units of a given project, you can use
17838 the @command{gnat} driver along with the @option{-P} option:
17839 @smallexample
17840 gnat metric -Pproj
17841 @end smallexample
17842
17843 @noindent
17844 If the project @code{proj} depends upon other projects, you can compute
17845 the metrics on the project closure using the @option{-U} option:
17846 @smallexample
17847 gnat metric -Pproj -U
17848 @end smallexample
17849
17850 @noindent
17851 Finally, if not all the units are relevant to a particular main
17852 program in the project closure, you can generate metrics for the set
17853 of units needed to create a given main program (unit closure) using
17854 the @option{-U} option followed by the name of the main unit:
17855 @smallexample
17856 gnat metric -Pproj -U main
17857 @end smallexample
17858
17859
17860 @c ***********************************
17861 @node File Name Krunching Using gnatkr
17862 @chapter File Name Krunching Using @code{gnatkr}
17863 @findex gnatkr
17864
17865 @noindent
17866 This chapter discusses the method used by the compiler to shorten
17867 the default file names chosen for Ada units so that they do not
17868 exceed the maximum length permitted. It also describes the
17869 @code{gnatkr} utility that can be used to determine the result of
17870 applying this shortening.
17871 @menu
17872 * About gnatkr::
17873 * Using gnatkr::
17874 * Krunching Method::
17875 * Examples of gnatkr Usage::
17876 @end menu
17877
17878 @node About gnatkr
17879 @section About @code{gnatkr}
17880
17881 @noindent
17882 The default file naming rule in GNAT
17883 is that the file name must be derived from
17884 the unit name. The exact default rule is as follows:
17885 @itemize @bullet
17886 @item
17887 Take the unit name and replace all dots by hyphens.
17888 @item
17889 If such a replacement occurs in the
17890 second character position of a name, and the first character is
17891 ^@samp{a}, @samp{g}, @samp{s}, or @samp{i}, ^@samp{A}, @samp{G}, @samp{S}, or @samp{I},^
17892 then replace the dot by the character
17893 ^@samp{~} (tilde)^@samp{$} (dollar sign)^
17894 instead of a minus.
17895 @end itemize
17896 The reason for this exception is to avoid clashes
17897 with the standard names for children of System, Ada, Interfaces,
17898 and GNAT, which use the prefixes
17899 ^@samp{s-}, @samp{a-}, @samp{i-}, and @samp{g-},^@samp{S-}, @samp{A-}, @samp{I-}, and @samp{G-},^
17900 respectively.
17901
17902 The @option{^-gnatk^/FILE_NAME_MAX_LENGTH=^@var{nn}}
17903 switch of the compiler activates a ``krunching''
17904 circuit that limits file names to nn characters (where nn is a decimal
17905 integer). For example, using OpenVMS,
17906 where the maximum file name length is
17907 39, the value of nn is usually set to 39, but if you want to generate
17908 a set of files that would be usable if ported to a system with some
17909 different maximum file length, then a different value can be specified.
17910 The default value of 39 for OpenVMS need not be specified.
17911
17912 The @code{gnatkr} utility can be used to determine the krunched name for
17913 a given file, when krunched to a specified maximum length.
17914
17915 @node Using gnatkr
17916 @section Using @code{gnatkr}
17917
17918 @noindent
17919 The @code{gnatkr} command has the form
17920
17921 @ifclear vms
17922 @smallexample
17923 $ gnatkr @var{name} @ovar{length}
17924 @end smallexample
17925 @end ifclear
17926
17927 @ifset vms
17928 @smallexample
17929 $ gnatkr @var{name} /COUNT=nn
17930 @end smallexample
17931 @end ifset
17932
17933 @noindent
17934 @var{name} is the uncrunched file name, derived from the name of the unit
17935 in the standard manner described in the previous section (i.e., in particular
17936 all dots are replaced by hyphens). The file name may or may not have an
17937 extension (defined as a suffix of the form period followed by arbitrary
17938 characters other than period). If an extension is present then it will
17939 be preserved in the output. For example, when krunching @file{hellofile.ads}
17940 to eight characters, the result will be hellofil.ads.
17941
17942 Note: for compatibility with previous versions of @code{gnatkr} dots may
17943 appear in the name instead of hyphens, but the last dot will always be
17944 taken as the start of an extension. So if @code{gnatkr} is given an argument
17945 such as @file{Hello.World.adb} it will be treated exactly as if the first
17946 period had been a hyphen, and for example krunching to eight characters
17947 gives the result @file{hellworl.adb}.
17948
17949 Note that the result is always all lower case (except on OpenVMS where it is
17950 all upper case). Characters of the other case are folded as required.
17951
17952 @var{length} represents the length of the krunched name. The default
17953 when no argument is given is ^8^39^ characters. A length of zero stands for
17954 unlimited, in other words do not chop except for system files where the
17955 implied crunching length is always eight characters.
17956
17957 @noindent
17958 The output is the krunched name. The output has an extension only if the
17959 original argument was a file name with an extension.
17960
17961 @node Krunching Method
17962 @section Krunching Method
17963
17964 @noindent
17965 The initial file name is determined by the name of the unit that the file
17966 contains. The name is formed by taking the full expanded name of the
17967 unit and replacing the separating dots with hyphens and
17968 using ^lowercase^uppercase^
17969 for all letters, except that a hyphen in the second character position is
17970 replaced by a ^tilde^dollar sign^ if the first character is
17971 ^@samp{a}, @samp{i}, @samp{g}, or @samp{s}^@samp{A}, @samp{I}, @samp{G}, or @samp{S}^.
17972 The extension is @code{.ads} for a
17973 spec and @code{.adb} for a body.
17974 Krunching does not affect the extension, but the file name is shortened to
17975 the specified length by following these rules:
17976
17977 @itemize @bullet
17978 @item
17979 The name is divided into segments separated by hyphens, tildes or
17980 underscores and all hyphens, tildes, and underscores are
17981 eliminated. If this leaves the name short enough, we are done.
17982
17983 @item
17984 If the name is too long, the longest segment is located (left-most
17985 if there are two of equal length), and shortened by dropping
17986 its last character. This is repeated until the name is short enough.
17987
17988 As an example, consider the krunching of @*@file{our-strings-wide_fixed.adb}
17989 to fit the name into 8 characters as required by some operating systems.
17990
17991 @smallexample
17992 our-strings-wide_fixed 22
17993 our strings wide fixed 19
17994 our string wide fixed 18
17995 our strin wide fixed 17
17996 our stri wide fixed 16
17997 our stri wide fixe 15
17998 our str wide fixe 14
17999 our str wid fixe 13
18000 our str wid fix 12
18001 ou str wid fix 11
18002 ou st wid fix 10
18003 ou st wi fix 9
18004 ou st wi fi 8
18005 Final file name: oustwifi.adb
18006 @end smallexample
18007
18008 @item
18009 The file names for all predefined units are always krunched to eight
18010 characters. The krunching of these predefined units uses the following
18011 special prefix replacements:
18012
18013 @table @file
18014 @item ada-
18015 replaced by @file{^a^A^-}
18016
18017 @item gnat-
18018 replaced by @file{^g^G^-}
18019
18020 @item interfaces-
18021 replaced by @file{^i^I^-}
18022
18023 @item system-
18024 replaced by @file{^s^S^-}
18025 @end table
18026
18027 These system files have a hyphen in the second character position. That
18028 is why normal user files replace such a character with a
18029 ^tilde^dollar sign^, to
18030 avoid confusion with system file names.
18031
18032 As an example of this special rule, consider
18033 @*@file{ada-strings-wide_fixed.adb}, which gets krunched as follows:
18034
18035 @smallexample
18036 ada-strings-wide_fixed 22
18037 a- strings wide fixed 18
18038 a- string wide fixed 17
18039 a- strin wide fixed 16
18040 a- stri wide fixed 15
18041 a- stri wide fixe 14
18042 a- str wide fixe 13
18043 a- str wid fixe 12
18044 a- str wid fix 11
18045 a- st wid fix 10
18046 a- st wi fix 9
18047 a- st wi fi 8
18048 Final file name: a-stwifi.adb
18049 @end smallexample
18050 @end itemize
18051
18052 Of course no file shortening algorithm can guarantee uniqueness over all
18053 possible unit names, and if file name krunching is used then it is your
18054 responsibility to ensure that no name clashes occur. The utility
18055 program @code{gnatkr} is supplied for conveniently determining the
18056 krunched name of a file.
18057
18058 @node Examples of gnatkr Usage
18059 @section Examples of @code{gnatkr} Usage
18060
18061 @smallexample
18062 @iftex
18063 @leftskip=0cm
18064 @end iftex
18065 @ifclear vms
18066 $ gnatkr very_long_unit_name.ads --> velounna.ads
18067 $ gnatkr grandparent-parent-child.ads --> grparchi.ads
18068 $ gnatkr Grandparent.Parent.Child.ads --> grparchi.ads
18069 $ gnatkr grandparent-parent-child --> grparchi
18070 @end ifclear
18071 $ gnatkr very_long_unit_name.ads/count=6 --> vlunna.ads
18072 $ gnatkr very_long_unit_name.ads/count=0 --> very_long_unit_name.ads
18073 @end smallexample
18074
18075 @node Preprocessing Using gnatprep
18076 @chapter Preprocessing Using @code{gnatprep}
18077 @findex gnatprep
18078
18079 @noindent
18080 This chapter discusses how to use GNAT's @code{gnatprep} utility for simple
18081 preprocessing.
18082 Although designed for use with GNAT, @code{gnatprep} does not depend on any
18083 special GNAT features.
18084 For further discussion of conditional compilation in general, see
18085 @ref{Conditional Compilation}.
18086
18087 @menu
18088 * Preprocessing Symbols::
18089 * Using gnatprep::
18090 * Switches for gnatprep::
18091 * Form of Definitions File::
18092 * Form of Input Text for gnatprep::
18093 @end menu
18094
18095 @node Preprocessing Symbols
18096 @section Preprocessing Symbols
18097
18098 @noindent
18099 Preprocessing symbols are defined in definition files and referred to in
18100 sources to be preprocessed. A Preprocessing symbol is an identifier, following
18101 normal Ada (case-insensitive) rules for its syntax, with the restriction that
18102 all characters need to be in the ASCII set (no accented letters).
18103
18104 @node Using gnatprep
18105 @section Using @code{gnatprep}
18106
18107 @noindent
18108 To call @code{gnatprep} use
18109
18110 @smallexample
18111 $ gnatprep @ovar{switches} @var{infile} @var{outfile} @ovar{deffile}
18112 @end smallexample
18113
18114 @noindent
18115 where
18116 @table @var
18117 @item switches
18118 is an optional sequence of switches as described in the next section.
18119
18120 @item infile
18121 is the full name of the input file, which is an Ada source
18122 file containing preprocessor directives.
18123
18124 @item outfile
18125 is the full name of the output file, which is an Ada source
18126 in standard Ada form. When used with GNAT, this file name will
18127 normally have an ads or adb suffix.
18128
18129 @item deffile
18130 is the full name of a text file containing definitions of
18131 preprocessing symbols to be referenced by the preprocessor. This argument is
18132 optional, and can be replaced by the use of the @option{-D} switch.
18133
18134 @end table
18135
18136 @node Switches for gnatprep
18137 @section Switches for @code{gnatprep}
18138
18139 @table @option
18140 @c !sort!
18141
18142 @item ^-b^/BLANK_LINES^
18143 @cindex @option{^-b^/BLANK_LINES^} (@command{gnatprep})
18144 Causes both preprocessor lines and the lines deleted by
18145 preprocessing to be replaced by blank lines in the output source file,
18146 preserving line numbers in the output file.
18147
18148 @item ^-c^/COMMENTS^
18149 @cindex @option{^-c^/COMMENTS^} (@command{gnatprep})
18150 Causes both preprocessor lines and the lines deleted
18151 by preprocessing to be retained in the output source as comments marked
18152 with the special string @code{"--! "}. This option will result in line numbers
18153 being preserved in the output file.
18154
18155 @item ^-C^/REPLACE_IN_COMMENTS^
18156 @cindex @option{^-C^/REPLACE_IN_COMMENTS^} (@command{gnatprep})
18157 Causes comments to be scanned. Normally comments are ignored by gnatprep.
18158 If this option is specified, then comments are scanned and any $symbol
18159 substitutions performed as in program text. This is particularly useful
18160 when structured comments are used (e.g., when writing programs in the
18161 SPARK dialect of Ada). Note that this switch is not available when
18162 doing integrated preprocessing (it would be useless in this context
18163 since comments are ignored by the compiler in any case).
18164
18165 @item ^-Dsymbol=value^/ASSOCIATE="symbol=value"^
18166 @cindex @option{^-D^/ASSOCIATE^} (@command{gnatprep})
18167 Defines a new preprocessing symbol, associated with value. If no value is given
18168 on the command line, then symbol is considered to be @code{True}. This switch
18169 can be used in place of a definition file.
18170
18171 @ifset vms
18172 @item /REMOVE
18173 @cindex @option{/REMOVE} (@command{gnatprep})
18174 This is the default setting which causes lines deleted by preprocessing
18175 to be entirely removed from the output file.
18176 @end ifset
18177
18178 @item ^-r^/REFERENCE^
18179 @cindex @option{^-r^/REFERENCE^} (@command{gnatprep})
18180 Causes a @code{Source_Reference} pragma to be generated that
18181 references the original input file, so that error messages will use
18182 the file name of this original file. The use of this switch implies
18183 that preprocessor lines are not to be removed from the file, so its
18184 use will force @option{^-b^/BLANK_LINES^} mode if
18185 @option{^-c^/COMMENTS^}
18186 has not been specified explicitly.
18187
18188 Note that if the file to be preprocessed contains multiple units, then
18189 it will be necessary to @code{gnatchop} the output file from
18190 @code{gnatprep}. If a @code{Source_Reference} pragma is present
18191 in the preprocessed file, it will be respected by
18192 @code{gnatchop ^-r^/REFERENCE^}
18193 so that the final chopped files will correctly refer to the original
18194 input source file for @code{gnatprep}.
18195
18196 @item ^-s^/SYMBOLS^
18197 @cindex @option{^-s^/SYMBOLS^} (@command{gnatprep})
18198 Causes a sorted list of symbol names and values to be
18199 listed on the standard output file.
18200
18201 @item ^-u^/UNDEFINED^
18202 @cindex @option{^-u^/UNDEFINED^} (@command{gnatprep})
18203 Causes undefined symbols to be treated as having the value FALSE in the context
18204 of a preprocessor test. In the absence of this option, an undefined symbol in
18205 a @code{#if} or @code{#elsif} test will be treated as an error.
18206
18207 @end table
18208
18209 @ifclear vms
18210 @noindent
18211 Note: if neither @option{-b} nor @option{-c} is present,
18212 then preprocessor lines and
18213 deleted lines are completely removed from the output, unless -r is
18214 specified, in which case -b is assumed.
18215 @end ifclear
18216
18217 @node Form of Definitions File
18218 @section Form of Definitions File
18219
18220 @noindent
18221 The definitions file contains lines of the form
18222
18223 @smallexample
18224 symbol := value
18225 @end smallexample
18226
18227 @noindent
18228 where symbol is a preprocessing symbol, and value is one of the following:
18229
18230 @itemize @bullet
18231 @item
18232 Empty, corresponding to a null substitution
18233 @item
18234 A string literal using normal Ada syntax
18235 @item
18236 Any sequence of characters from the set
18237 (letters, digits, period, underline).
18238 @end itemize
18239
18240 @noindent
18241 Comment lines may also appear in the definitions file, starting with
18242 the usual @code{--},
18243 and comments may be added to the definitions lines.
18244
18245 @node Form of Input Text for gnatprep
18246 @section Form of Input Text for @code{gnatprep}
18247
18248 @noindent
18249 The input text may contain preprocessor conditional inclusion lines,
18250 as well as general symbol substitution sequences.
18251
18252 The preprocessor conditional inclusion commands have the form
18253
18254 @smallexample
18255 @group
18256 @cartouche
18257 #if @i{expression} @r{[}then@r{]}
18258 lines
18259 #elsif @i{expression} @r{[}then@r{]}
18260 lines
18261 #elsif @i{expression} @r{[}then@r{]}
18262 lines
18263 @dots{}
18264 #else
18265 lines
18266 #end if;
18267 @end cartouche
18268 @end group
18269 @end smallexample
18270
18271 @noindent
18272 In this example, @i{expression} is defined by the following grammar:
18273 @smallexample
18274 @i{expression} ::= <symbol>
18275 @i{expression} ::= <symbol> = "<value>"
18276 @i{expression} ::= <symbol> = <symbol>
18277 @i{expression} ::= <symbol> 'Defined
18278 @i{expression} ::= not @i{expression}
18279 @i{expression} ::= @i{expression} and @i{expression}
18280 @i{expression} ::= @i{expression} or @i{expression}
18281 @i{expression} ::= @i{expression} and then @i{expression}
18282 @i{expression} ::= @i{expression} or else @i{expression}
18283 @i{expression} ::= ( @i{expression} )
18284 @end smallexample
18285
18286 The following restriction exists: it is not allowed to have "and" or "or"
18287 following "not" in the same expression without parentheses. For example, this
18288 is not allowed:
18289
18290 @smallexample
18291 not X or Y
18292 @end smallexample
18293
18294 This should be one of the following:
18295
18296 @smallexample
18297 (not X) or Y
18298 not (X or Y)
18299 @end smallexample
18300
18301 @noindent
18302 For the first test (@i{expression} ::= <symbol>) the symbol must have
18303 either the value true or false, that is to say the right-hand of the
18304 symbol definition must be one of the (case-insensitive) literals
18305 @code{True} or @code{False}. If the value is true, then the
18306 corresponding lines are included, and if the value is false, they are
18307 excluded.
18308
18309 The test (@i{expression} ::= <symbol> @code{'Defined}) is true only if
18310 the symbol has been defined in the definition file or by a @option{-D}
18311 switch on the command line. Otherwise, the test is false.
18312
18313 The equality tests are case insensitive, as are all the preprocessor lines.
18314
18315 If the symbol referenced is not defined in the symbol definitions file,
18316 then the effect depends on whether or not switch @option{-u}
18317 is specified. If so, then the symbol is treated as if it had the value
18318 false and the test fails. If this switch is not specified, then
18319 it is an error to reference an undefined symbol. It is also an error to
18320 reference a symbol that is defined with a value other than @code{True}
18321 or @code{False}.
18322
18323 The use of the @code{not} operator inverts the sense of this logical test.
18324 The @code{not} operator cannot be combined with the @code{or} or @code{and}
18325 operators, without parentheses. For example, "if not X or Y then" is not
18326 allowed, but "if (not X) or Y then" and "if not (X or Y) then" are.
18327
18328 The @code{then} keyword is optional as shown
18329
18330 The @code{#} must be the first non-blank character on a line, but
18331 otherwise the format is free form. Spaces or tabs may appear between
18332 the @code{#} and the keyword. The keywords and the symbols are case
18333 insensitive as in normal Ada code. Comments may be used on a
18334 preprocessor line, but other than that, no other tokens may appear on a
18335 preprocessor line. Any number of @code{elsif} clauses can be present,
18336 including none at all. The @code{else} is optional, as in Ada.
18337
18338 The @code{#} marking the start of a preprocessor line must be the first
18339 non-blank character on the line, i.e., it must be preceded only by
18340 spaces or horizontal tabs.
18341
18342 Symbol substitution outside of preprocessor lines is obtained by using
18343 the sequence
18344
18345 @smallexample
18346 $symbol
18347 @end smallexample
18348
18349 @noindent
18350 anywhere within a source line, except in a comment or within a
18351 string literal. The identifier
18352 following the @code{$} must match one of the symbols defined in the symbol
18353 definition file, and the result is to substitute the value of the
18354 symbol in place of @code{$symbol} in the output file.
18355
18356 Note that although the substitution of strings within a string literal
18357 is not possible, it is possible to have a symbol whose defined value is
18358 a string literal. So instead of setting XYZ to @code{hello} and writing:
18359
18360 @smallexample
18361 Header : String := "$XYZ";
18362 @end smallexample
18363
18364 @noindent
18365 you should set XYZ to @code{"hello"} and write:
18366
18367 @smallexample
18368 Header : String := $XYZ;
18369 @end smallexample
18370
18371 @noindent
18372 and then the substitution will occur as desired.
18373
18374 @ifset vms
18375 @node The GNAT Run-Time Library Builder gnatlbr
18376 @chapter The GNAT Run-Time Library Builder @code{gnatlbr}
18377 @findex gnatlbr
18378 @cindex Library builder
18379
18380 @noindent
18381 @code{gnatlbr} is a tool for rebuilding the GNAT run time with user
18382 supplied configuration pragmas.
18383
18384 @menu
18385 * Running gnatlbr::
18386 * Switches for gnatlbr::
18387 * Examples of gnatlbr Usage::
18388 @end menu
18389
18390 @node Running gnatlbr
18391 @section Running @code{gnatlbr}
18392
18393 @noindent
18394 The @code{gnatlbr} command has the form
18395
18396 @smallexample
18397 $ GNAT LIBRARY /@r{[}CREATE@r{|}SET@r{|}DELETE@r{]}=directory @r{[}/CONFIG=file@r{]}
18398 @end smallexample
18399
18400 @node Switches for gnatlbr
18401 @section Switches for @code{gnatlbr}
18402
18403 @noindent
18404 @code{gnatlbr} recognizes the following switches:
18405
18406 @table @option
18407 @c !sort!
18408 @item /CREATE=directory
18409 @cindex @code{/CREATE} (@code{gnatlbr})
18410 Create the new run-time library in the specified directory.
18411
18412 @item /SET=directory
18413 @cindex @code{/SET} (@code{gnatlbr})
18414 Make the library in the specified directory the current run-time library.
18415
18416 @item /DELETE=directory
18417 @cindex @code{/DELETE} (@code{gnatlbr})
18418 Delete the run-time library in the specified directory.
18419
18420 @item /CONFIG=file
18421 @cindex @code{/CONFIG} (@code{gnatlbr})
18422 With /CREATE: Use the configuration pragmas in the specified file when
18423 building the library.
18424
18425 With /SET: Use the configuration pragmas in the specified file when
18426 compiling.
18427
18428 @end table
18429
18430 @node Examples of gnatlbr Usage
18431 @section Example of @code{gnatlbr} Usage
18432
18433 @smallexample
18434 Contents of VAXFLOAT.ADC:
18435 pragma Float_Representation (VAX_Float);
18436
18437 $ GNAT LIBRARY /CREATE=[.VAXFLOAT] /CONFIG=VAXFLOAT.ADC
18438
18439 GNAT LIBRARY rebuilds the run-time library in directory [.VAXFLOAT]
18440
18441 @end smallexample
18442 @end ifset
18443
18444 @node The GNAT Library Browser gnatls
18445 @chapter The GNAT Library Browser @code{gnatls}
18446 @findex gnatls
18447 @cindex Library browser
18448
18449 @noindent
18450 @code{gnatls} is a tool that outputs information about compiled
18451 units. It gives the relationship between objects, unit names and source
18452 files. It can also be used to check the source dependencies of a unit
18453 as well as various characteristics.
18454
18455 Note: to invoke @code{gnatls} with a project file, use the @code{gnat}
18456 driver (see @ref{The GNAT Driver and Project Files}).
18457
18458 @menu
18459 * Running gnatls::
18460 * Switches for gnatls::
18461 * Examples of gnatls Usage::
18462 @end menu
18463
18464 @node Running gnatls
18465 @section Running @code{gnatls}
18466
18467 @noindent
18468 The @code{gnatls} command has the form
18469
18470 @smallexample
18471 $ gnatls switches @var{object_or_ali_file}
18472 @end smallexample
18473
18474 @noindent
18475 The main argument is the list of object or @file{ali} files
18476 (@pxref{The Ada Library Information Files})
18477 for which information is requested.
18478
18479 In normal mode, without additional option, @code{gnatls} produces a
18480 four-column listing. Each line represents information for a specific
18481 object. The first column gives the full path of the object, the second
18482 column gives the name of the principal unit in this object, the third
18483 column gives the status of the source and the fourth column gives the
18484 full path of the source representing this unit.
18485 Here is a simple example of use:
18486
18487 @smallexample
18488 $ gnatls *.o
18489 ^./^[]^demo1.o demo1 DIF demo1.adb
18490 ^./^[]^demo2.o demo2 OK demo2.adb
18491 ^./^[]^hello.o h1 OK hello.adb
18492 ^./^[]^instr-child.o instr.child MOK instr-child.adb
18493 ^./^[]^instr.o instr OK instr.adb
18494 ^./^[]^tef.o tef DIF tef.adb
18495 ^./^[]^text_io_example.o text_io_example OK text_io_example.adb
18496 ^./^[]^tgef.o tgef DIF tgef.adb
18497 @end smallexample
18498
18499 @noindent
18500 The first line can be interpreted as follows: the main unit which is
18501 contained in
18502 object file @file{demo1.o} is demo1, whose main source is in
18503 @file{demo1.adb}. Furthermore, the version of the source used for the
18504 compilation of demo1 has been modified (DIF). Each source file has a status
18505 qualifier which can be:
18506
18507 @table @code
18508 @item OK (unchanged)
18509 The version of the source file used for the compilation of the
18510 specified unit corresponds exactly to the actual source file.
18511
18512 @item MOK (slightly modified)
18513 The version of the source file used for the compilation of the
18514 specified unit differs from the actual source file but not enough to
18515 require recompilation. If you use gnatmake with the qualifier
18516 @option{^-m (minimal recompilation)^/MINIMAL_RECOMPILATION^}, a file marked
18517 MOK will not be recompiled.
18518
18519 @item DIF (modified)
18520 No version of the source found on the path corresponds to the source
18521 used to build this object.
18522
18523 @item ??? (file not found)
18524 No source file was found for this unit.
18525
18526 @item HID (hidden, unchanged version not first on PATH)
18527 The version of the source that corresponds exactly to the source used
18528 for compilation has been found on the path but it is hidden by another
18529 version of the same source that has been modified.
18530
18531 @end table
18532
18533 @node Switches for gnatls
18534 @section Switches for @code{gnatls}
18535
18536 @noindent
18537 @code{gnatls} recognizes the following switches:
18538
18539 @table @option
18540 @c !sort!
18541 @cindex @option{--version} @command{gnatls}
18542 Display Copyright and version, then exit disregarding all other options.
18543
18544 @item --help
18545 @cindex @option{--help} @command{gnatls}
18546 If @option{--version} was not used, display usage, then exit disregarding
18547 all other options.
18548
18549 @item ^-a^/ALL_UNITS^
18550 @cindex @option{^-a^/ALL_UNITS^} (@code{gnatls})
18551 Consider all units, including those of the predefined Ada library.
18552 Especially useful with @option{^-d^/DEPENDENCIES^}.
18553
18554 @item ^-d^/DEPENDENCIES^
18555 @cindex @option{^-d^/DEPENDENCIES^} (@code{gnatls})
18556 List sources from which specified units depend on.
18557
18558 @item ^-h^/OUTPUT=OPTIONS^
18559 @cindex @option{^-h^/OUTPUT=OPTIONS^} (@code{gnatls})
18560 Output the list of options.
18561
18562 @item ^-o^/OUTPUT=OBJECTS^
18563 @cindex @option{^-o^/OUTPUT=OBJECTS^} (@code{gnatls})
18564 Only output information about object files.
18565
18566 @item ^-s^/OUTPUT=SOURCES^
18567 @cindex @option{^-s^/OUTPUT=SOURCES^} (@code{gnatls})
18568 Only output information about source files.
18569
18570 @item ^-u^/OUTPUT=UNITS^
18571 @cindex @option{^-u^/OUTPUT=UNITS^} (@code{gnatls})
18572 Only output information about compilation units.
18573
18574 @item ^-files^/FILES^=@var{file}
18575 @cindex @option{^-files^/FILES^} (@code{gnatls})
18576 Take as arguments the files listed in text file @var{file}.
18577 Text file @var{file} may contain empty lines that are ignored.
18578 Each nonempty line should contain the name of an existing file.
18579 Several such switches may be specified simultaneously.
18580
18581 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
18582 @itemx ^-aI^/SOURCE_SEARCH=^@var{dir}
18583 @itemx ^-I^/SEARCH=^@var{dir}
18584 @itemx ^-I-^/NOCURRENT_DIRECTORY^
18585 @itemx -nostdinc
18586 @cindex @option{^-aO^/OBJECT_SEARCH^} (@code{gnatls})
18587 @cindex @option{^-aI^/SOURCE_SEARCH^} (@code{gnatls})
18588 @cindex @option{^-I^/SEARCH^} (@code{gnatls})
18589 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatls})
18590 Source path manipulation. Same meaning as the equivalent @command{gnatmake}
18591 flags (@pxref{Switches for gnatmake}).
18592
18593 @item --RTS=@var{rts-path}
18594 @cindex @option{--RTS} (@code{gnatls})
18595 Specifies the default location of the runtime library. Same meaning as the
18596 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
18597
18598 @item ^-v^/OUTPUT=VERBOSE^
18599 @cindex @option{^-v^/OUTPUT=VERBOSE^} (@code{gnatls})
18600 Verbose mode. Output the complete source, object and project paths. Do not use
18601 the default column layout but instead use long format giving as much as
18602 information possible on each requested units, including special
18603 characteristics such as:
18604
18605 @table @code
18606 @item Preelaborable
18607 The unit is preelaborable in the Ada sense.
18608
18609 @item No_Elab_Code
18610 No elaboration code has been produced by the compiler for this unit.
18611
18612 @item Pure
18613 The unit is pure in the Ada sense.
18614
18615 @item Elaborate_Body
18616 The unit contains a pragma Elaborate_Body.
18617
18618 @item Remote_Types
18619 The unit contains a pragma Remote_Types.
18620
18621 @item Shared_Passive
18622 The unit contains a pragma Shared_Passive.
18623
18624 @item Predefined
18625 This unit is part of the predefined environment and cannot be modified
18626 by the user.
18627
18628 @item Remote_Call_Interface
18629 The unit contains a pragma Remote_Call_Interface.
18630
18631 @end table
18632
18633 @end table
18634
18635 @node Examples of gnatls Usage
18636 @section Example of @code{gnatls} Usage
18637 @ifclear vms
18638
18639 @noindent
18640 Example of using the verbose switch. Note how the source and
18641 object paths are affected by the -I switch.
18642
18643 @smallexample
18644 $ gnatls -v -I.. demo1.o
18645
18646 GNATLS 5.03w (20041123-34)
18647 Copyright 1997-2004 Free Software Foundation, Inc.
18648
18649 Source Search Path:
18650 <Current_Directory>
18651 ../
18652 /home/comar/local/adainclude/
18653
18654 Object Search Path:
18655 <Current_Directory>
18656 ../
18657 /home/comar/local/lib/gcc-lib/x86-linux/3.4.3/adalib/
18658
18659 Project Search Path:
18660 <Current_Directory>
18661 /home/comar/local/lib/gnat/
18662
18663 ./demo1.o
18664 Unit =>
18665 Name => demo1
18666 Kind => subprogram body
18667 Flags => No_Elab_Code
18668 Source => demo1.adb modified
18669 @end smallexample
18670
18671 @noindent
18672 The following is an example of use of the dependency list.
18673 Note the use of the -s switch
18674 which gives a straight list of source files. This can be useful for
18675 building specialized scripts.
18676
18677 @smallexample
18678 $ gnatls -d demo2.o
18679 ./demo2.o demo2 OK demo2.adb
18680 OK gen_list.ads
18681 OK gen_list.adb
18682 OK instr.ads
18683 OK instr-child.ads
18684
18685 $ gnatls -d -s -a demo1.o
18686 demo1.adb
18687 /home/comar/local/adainclude/ada.ads
18688 /home/comar/local/adainclude/a-finali.ads
18689 /home/comar/local/adainclude/a-filico.ads
18690 /home/comar/local/adainclude/a-stream.ads
18691 /home/comar/local/adainclude/a-tags.ads
18692 gen_list.ads
18693 gen_list.adb
18694 /home/comar/local/adainclude/gnat.ads
18695 /home/comar/local/adainclude/g-io.ads
18696 instr.ads
18697 /home/comar/local/adainclude/system.ads
18698 /home/comar/local/adainclude/s-exctab.ads
18699 /home/comar/local/adainclude/s-finimp.ads
18700 /home/comar/local/adainclude/s-finroo.ads
18701 /home/comar/local/adainclude/s-secsta.ads
18702 /home/comar/local/adainclude/s-stalib.ads
18703 /home/comar/local/adainclude/s-stoele.ads
18704 /home/comar/local/adainclude/s-stratt.ads
18705 /home/comar/local/adainclude/s-tasoli.ads
18706 /home/comar/local/adainclude/s-unstyp.ads
18707 /home/comar/local/adainclude/unchconv.ads
18708 @end smallexample
18709 @end ifclear
18710
18711 @ifset vms
18712 @smallexample
18713 GNAT LIST /DEPENDENCIES /OUTPUT=SOURCES /ALL_UNITS DEMO1.ADB
18714
18715 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]ada.ads
18716 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-finali.ads
18717 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-filico.ads
18718 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-stream.ads
18719 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-tags.ads
18720 demo1.adb
18721 gen_list.ads
18722 gen_list.adb
18723 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]gnat.ads
18724 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]g-io.ads
18725 instr.ads
18726 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]system.ads
18727 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-exctab.ads
18728 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-finimp.ads
18729 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-finroo.ads
18730 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-secsta.ads
18731 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stalib.ads
18732 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stoele.ads
18733 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stratt.ads
18734 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-tasoli.ads
18735 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-unstyp.ads
18736 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]unchconv.ads
18737 @end smallexample
18738 @end ifset
18739
18740 @node Cleaning Up Using gnatclean
18741 @chapter Cleaning Up Using @code{gnatclean}
18742 @findex gnatclean
18743 @cindex Cleaning tool
18744
18745 @noindent
18746 @code{gnatclean} is a tool that allows the deletion of files produced by the
18747 compiler, binder and linker, including ALI files, object files, tree files,
18748 expanded source files, library files, interface copy source files, binder
18749 generated files and executable files.
18750
18751 @menu
18752 * Running gnatclean::
18753 * Switches for gnatclean::
18754 @c * Examples of gnatclean Usage::
18755 @end menu
18756
18757 @node Running gnatclean
18758 @section Running @code{gnatclean}
18759
18760 @noindent
18761 The @code{gnatclean} command has the form:
18762
18763 @smallexample
18764 $ gnatclean switches @var{names}
18765 @end smallexample
18766
18767 @noindent
18768 @var{names} is a list of source file names. Suffixes @code{.^ads^ADS^} and
18769 @code{^adb^ADB^} may be omitted. If a project file is specified using switch
18770 @code{^-P^/PROJECT_FILE=^}, then @var{names} may be completely omitted.
18771
18772 @noindent
18773 In normal mode, @code{gnatclean} delete the files produced by the compiler and,
18774 if switch @code{^-c^/COMPILER_FILES_ONLY^} is not specified, by the binder and
18775 the linker. In informative-only mode, specified by switch
18776 @code{^-n^/NODELETE^}, the list of files that would have been deleted in
18777 normal mode is listed, but no file is actually deleted.
18778
18779 @node Switches for gnatclean
18780 @section Switches for @code{gnatclean}
18781
18782 @noindent
18783 @code{gnatclean} recognizes the following switches:
18784
18785 @table @option
18786 @c !sort!
18787 @cindex @option{--version} @command{gnatclean}
18788 Display Copyright and version, then exit disregarding all other options.
18789
18790 @item --help
18791 @cindex @option{--help} @command{gnatclean}
18792 If @option{--version} was not used, display usage, then exit disregarding
18793 all other options.
18794
18795 @item ^-c^/COMPILER_FILES_ONLY^
18796 @cindex @option{^-c^/COMPILER_FILES_ONLY^} (@code{gnatclean})
18797 Only attempt to delete the files produced by the compiler, not those produced
18798 by the binder or the linker. The files that are not to be deleted are library
18799 files, interface copy files, binder generated files and executable files.
18800
18801 @item ^-D ^/DIRECTORY_OBJECTS=^@var{dir}
18802 @cindex @option{^-D^/DIRECTORY_OBJECTS^} (@code{gnatclean})
18803 Indicate that ALI and object files should normally be found in directory
18804 @var{dir}.
18805
18806 @item ^-F^/FULL_PATH_IN_BRIEF_MESSAGES^
18807 @cindex @option{^-F^/FULL_PATH_IN_BRIEF_MESSAGES^} (@code{gnatclean})
18808 When using project files, if some errors or warnings are detected during
18809 parsing and verbose mode is not in effect (no use of switch
18810 ^-v^/VERBOSE^), then error lines start with the full path name of the project
18811 file, rather than its simple file name.
18812
18813 @item ^-h^/HELP^
18814 @cindex @option{^-h^/HELP^} (@code{gnatclean})
18815 Output a message explaining the usage of @code{^gnatclean^gnatclean^}.
18816
18817 @item ^-n^/NODELETE^
18818 @cindex @option{^-n^/NODELETE^} (@code{gnatclean})
18819 Informative-only mode. Do not delete any files. Output the list of the files
18820 that would have been deleted if this switch was not specified.
18821
18822 @item ^-P^/PROJECT_FILE=^@var{project}
18823 @cindex @option{^-P^/PROJECT_FILE^} (@code{gnatclean})
18824 Use project file @var{project}. Only one such switch can be used.
18825 When cleaning a project file, the files produced by the compilation of the
18826 immediate sources or inherited sources of the project files are to be
18827 deleted. This is not depending on the presence or not of executable names
18828 on the command line.
18829
18830 @item ^-q^/QUIET^
18831 @cindex @option{^-q^/QUIET^} (@code{gnatclean})
18832 Quiet output. If there are no errors, do not output anything, except in
18833 verbose mode (switch ^-v^/VERBOSE^) or in informative-only mode
18834 (switch ^-n^/NODELETE^).
18835
18836 @item ^-r^/RECURSIVE^
18837 @cindex @option{^-r^/RECURSIVE^} (@code{gnatclean})
18838 When a project file is specified (using switch ^-P^/PROJECT_FILE=^),
18839 clean all imported and extended project files, recursively. If this switch
18840 is not specified, only the files related to the main project file are to be
18841 deleted. This switch has no effect if no project file is specified.
18842
18843 @item ^-v^/VERBOSE^
18844 @cindex @option{^-v^/VERBOSE^} (@code{gnatclean})
18845 Verbose mode.
18846
18847 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
18848 @cindex @option{^-vP^/MESSAGES_PROJECT_FILE^} (@code{gnatclean})
18849 Indicates the verbosity of the parsing of GNAT project files.
18850 @xref{Switches Related to Project Files}.
18851
18852 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
18853 @cindex @option{^-X^/EXTERNAL_REFERENCE^} (@code{gnatclean})
18854 Indicates that external variable @var{name} has the value @var{value}.
18855 The Project Manager will use this value for occurrences of
18856 @code{external(name)} when parsing the project file.
18857 @xref{Switches Related to Project Files}.
18858
18859 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
18860 @cindex @option{^-aO^/OBJECT_SEARCH^} (@code{gnatclean})
18861 When searching for ALI and object files, look in directory
18862 @var{dir}.
18863
18864 @item ^-I^/SEARCH=^@var{dir}
18865 @cindex @option{^-I^/SEARCH^} (@code{gnatclean})
18866 Equivalent to @option{^-aO^/OBJECT_SEARCH=^@var{dir}}.
18867
18868 @item ^-I-^/NOCURRENT_DIRECTORY^
18869 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatclean})
18870 @cindex Source files, suppressing search
18871 Do not look for ALI or object files in the directory
18872 where @code{gnatclean} was invoked.
18873
18874 @end table
18875
18876 @c @node Examples of gnatclean Usage
18877 @c @section Examples of @code{gnatclean} Usage
18878
18879 @ifclear vms
18880 @node GNAT and Libraries
18881 @chapter GNAT and Libraries
18882 @cindex Library, building, installing, using
18883
18884 @noindent
18885 This chapter describes how to build and use libraries with GNAT, and also shows
18886 how to recompile the GNAT run-time library. You should be familiar with the
18887 Project Manager facility (@pxref{GNAT Project Manager}) before reading this
18888 chapter.
18889
18890 @menu
18891 * Introduction to Libraries in GNAT::
18892 * General Ada Libraries::
18893 * Stand-alone Ada Libraries::
18894 * Rebuilding the GNAT Run-Time Library::
18895 @end menu
18896
18897 @node Introduction to Libraries in GNAT
18898 @section Introduction to Libraries in GNAT
18899
18900 @noindent
18901 A library is, conceptually, a collection of objects which does not have its
18902 own main thread of execution, but rather provides certain services to the
18903 applications that use it. A library can be either statically linked with the
18904 application, in which case its code is directly included in the application,
18905 or, on platforms that support it, be dynamically linked, in which case
18906 its code is shared by all applications making use of this library.
18907
18908 GNAT supports both types of libraries.
18909 In the static case, the compiled code can be provided in different ways. The
18910 simplest approach is to provide directly the set of objects resulting from
18911 compilation of the library source files. Alternatively, you can group the
18912 objects into an archive using whatever commands are provided by the operating
18913 system. For the latter case, the objects are grouped into a shared library.
18914
18915 In the GNAT environment, a library has three types of components:
18916 @itemize @bullet
18917 @item
18918 Source files.
18919 @item
18920 @file{ALI} files.
18921 @xref{The Ada Library Information Files}.
18922 @item
18923 Object files, an archive or a shared library.
18924 @end itemize
18925
18926 @noindent
18927 A GNAT library may expose all its source files, which is useful for
18928 documentation purposes. Alternatively, it may expose only the units needed by
18929 an external user to make use of the library. That is to say, the specs
18930 reflecting the library services along with all the units needed to compile
18931 those specs, which can include generic bodies or any body implementing an
18932 inlined routine. In the case of @emph{stand-alone libraries} those exposed
18933 units are called @emph{interface units} (@pxref{Stand-alone Ada Libraries}).
18934
18935 All compilation units comprising an application, including those in a library,
18936 need to be elaborated in an order partially defined by Ada's semantics. GNAT
18937 computes the elaboration order from the @file{ALI} files and this is why they
18938 constitute a mandatory part of GNAT libraries.
18939 @emph{Stand-alone libraries} are the exception to this rule because a specific
18940 library elaboration routine is produced independently of the application(s)
18941 using the library.
18942
18943 @node General Ada Libraries
18944 @section General Ada Libraries
18945
18946 @menu
18947 * Building a library::
18948 * Installing a library::
18949 * Using a library::
18950 @end menu
18951
18952 @node Building a library
18953 @subsection Building a library
18954
18955 @noindent
18956 The easiest way to build a library is to use the Project Manager,
18957 which supports a special type of project called a @emph{Library Project}
18958 (@pxref{Library Projects}).
18959
18960 A project is considered a library project, when two project-level attributes
18961 are defined in it: @code{Library_Name} and @code{Library_Dir}. In order to
18962 control different aspects of library configuration, additional optional
18963 project-level attributes can be specified:
18964 @table @code
18965 @item Library_Kind
18966 This attribute controls whether the library is to be static or dynamic
18967
18968 @item Library_Version
18969 This attribute specifies the library version; this value is used
18970 during dynamic linking of shared libraries to determine if the currently
18971 installed versions of the binaries are compatible.
18972
18973 @item Library_Options
18974 @item Library_GCC
18975 These attributes specify additional low-level options to be used during
18976 library generation, and redefine the actual application used to generate
18977 library.
18978 @end table
18979
18980 @noindent
18981 The GNAT Project Manager takes full care of the library maintenance task,
18982 including recompilation of the source files for which objects do not exist
18983 or are not up to date, assembly of the library archive, and installation of
18984 the library (i.e., copying associated source, object and @file{ALI} files
18985 to the specified location).
18986
18987 Here is a simple library project file:
18988 @smallexample @c ada
18989 project My_Lib is
18990 for Source_Dirs use ("src1", "src2");
18991 for Object_Dir use "obj";
18992 for Library_Name use "mylib";
18993 for Library_Dir use "lib";
18994 for Library_Kind use "dynamic";
18995 end My_lib;
18996 @end smallexample
18997
18998 @noindent
18999 and the compilation command to build and install the library:
19000
19001 @smallexample @c ada
19002 $ gnatmake -Pmy_lib
19003 @end smallexample
19004
19005 @noindent
19006 It is not entirely trivial to perform manually all the steps required to
19007 produce a library. We recommend that you use the GNAT Project Manager
19008 for this task. In special cases where this is not desired, the necessary
19009 steps are discussed below.
19010
19011 There are various possibilities for compiling the units that make up the
19012 library: for example with a Makefile (@pxref{Using the GNU make Utility}) or
19013 with a conventional script. For simple libraries, it is also possible to create
19014 a dummy main program which depends upon all the packages that comprise the
19015 interface of the library. This dummy main program can then be given to
19016 @command{gnatmake}, which will ensure that all necessary objects are built.
19017
19018 After this task is accomplished, you should follow the standard procedure
19019 of the underlying operating system to produce the static or shared library.
19020
19021 Here is an example of such a dummy program:
19022 @smallexample @c ada
19023 @group
19024 with My_Lib.Service1;
19025 with My_Lib.Service2;
19026 with My_Lib.Service3;
19027 procedure My_Lib_Dummy is
19028 begin
19029 null;
19030 end;
19031 @end group
19032 @end smallexample
19033
19034 @noindent
19035 Here are the generic commands that will build an archive or a shared library.
19036
19037 @smallexample
19038 # compiling the library
19039 $ gnatmake -c my_lib_dummy.adb
19040
19041 # we don't need the dummy object itself
19042 $ rm my_lib_dummy.o my_lib_dummy.ali
19043
19044 # create an archive with the remaining objects
19045 $ ar rc libmy_lib.a *.o
19046 # some systems may require "ranlib" to be run as well
19047
19048 # or create a shared library
19049 $ gcc -shared -o libmy_lib.so *.o
19050 # some systems may require the code to have been compiled with -fPIC
19051
19052 # remove the object files that are now in the library
19053 $ rm *.o
19054
19055 # Make the ALI files read-only so that gnatmake will not try to
19056 # regenerate the objects that are in the library
19057 $ chmod -w *.ali
19058 @end smallexample
19059
19060 @noindent
19061 Please note that the library must have a name of the form @file{lib@var{xxx}.a}
19062 or @file{lib@var{xxx}.so} (or @file{lib@var{xxx}.dll} on Windows) in order to
19063 be accessed by the directive @option{-l@var{xxx}} at link time.
19064
19065 @node Installing a library
19066 @subsection Installing a library
19067 @cindex @code{ADA_PROJECT_PATH}
19068 @cindex @code{GPR_PROJECT_PATH}
19069
19070 @noindent
19071 If you use project files, library installation is part of the library build
19072 process. Thus no further action is needed in order to make use of the
19073 libraries that are built as part of the general application build. A usable
19074 version of the library is installed in the directory specified by the
19075 @code{Library_Dir} attribute of the library project file.
19076
19077 You may want to install a library in a context different from where the library
19078 is built. This situation arises with third party suppliers, who may want
19079 to distribute a library in binary form where the user is not expected to be
19080 able to recompile the library. The simplest option in this case is to provide
19081 a project file slightly different from the one used to build the library, by
19082 using the @code{externally_built} attribute. For instance, the project
19083 file used to build the library in the previous section can be changed into the
19084 following one when the library is installed:
19085
19086 @smallexample @c projectfile
19087 project My_Lib is
19088 for Source_Dirs use ("src1", "src2");
19089 for Library_Name use "mylib";
19090 for Library_Dir use "lib";
19091 for Library_Kind use "dynamic";
19092 for Externally_Built use "true";
19093 end My_lib;
19094 @end smallexample
19095
19096 @noindent
19097 This project file assumes that the directories @file{src1},
19098 @file{src2}, and @file{lib} exist in
19099 the directory containing the project file. The @code{externally_built}
19100 attribute makes it clear to the GNAT builder that it should not attempt to
19101 recompile any of the units from this library. It allows the library provider to
19102 restrict the source set to the minimum necessary for clients to make use of the
19103 library as described in the first section of this chapter. It is the
19104 responsibility of the library provider to install the necessary sources, ALI
19105 files and libraries in the directories mentioned in the project file. For
19106 convenience, the user's library project file should be installed in a location
19107 that will be searched automatically by the GNAT
19108 builder. These are the directories referenced in the @env{GPR_PROJECT_PATH}
19109 environment variable (@pxref{Importing Projects}), and also the default GNAT
19110 library location that can be queried with @command{gnatls -v} and is usually of
19111 the form $gnat_install_root/lib/gnat.
19112
19113 When project files are not an option, it is also possible, but not recommended,
19114 to install the library so that the sources needed to use the library are on the
19115 Ada source path and the ALI files & libraries be on the Ada Object path (see
19116 @ref{Search Paths and the Run-Time Library (RTL)}. Alternatively, the system
19117 administrator can place general-purpose libraries in the default compiler
19118 paths, by specifying the libraries' location in the configuration files
19119 @file{ada_source_path} and @file{ada_object_path}. These configuration files
19120 must be located in the GNAT installation tree at the same place as the gcc spec
19121 file. The location of the gcc spec file can be determined as follows:
19122 @smallexample
19123 $ gcc -v
19124 @end smallexample
19125
19126 @noindent
19127 The configuration files mentioned above have a simple format: each line
19128 must contain one unique directory name.
19129 Those names are added to the corresponding path
19130 in their order of appearance in the file. The names can be either absolute
19131 or relative; in the latter case, they are relative to where theses files
19132 are located.
19133
19134 The files @file{ada_source_path} and @file{ada_object_path} might not be
19135 present in a
19136 GNAT installation, in which case, GNAT will look for its run-time library in
19137 the directories @file{adainclude} (for the sources) and @file{adalib} (for the
19138 objects and @file{ALI} files). When the files exist, the compiler does not
19139 look in @file{adainclude} and @file{adalib}, and thus the
19140 @file{ada_source_path} file
19141 must contain the location for the GNAT run-time sources (which can simply
19142 be @file{adainclude}). In the same way, the @file{ada_object_path} file must
19143 contain the location for the GNAT run-time objects (which can simply
19144 be @file{adalib}).
19145
19146 You can also specify a new default path to the run-time library at compilation
19147 time with the switch @option{--RTS=rts-path}. You can thus choose / change
19148 the run-time library you want your program to be compiled with. This switch is
19149 recognized by @command{gcc}, @command{gnatmake}, @command{gnatbind},
19150 @command{gnatls}, @command{gnatfind} and @command{gnatxref}.
19151
19152 It is possible to install a library before or after the standard GNAT
19153 library, by reordering the lines in the configuration files. In general, a
19154 library must be installed before the GNAT library if it redefines
19155 any part of it.
19156
19157 @node Using a library
19158 @subsection Using a library
19159
19160 @noindent Once again, the project facility greatly simplifies the use of
19161 libraries. In this context, using a library is just a matter of adding a
19162 @code{with} clause in the user project. For instance, to make use of the
19163 library @code{My_Lib} shown in examples in earlier sections, you can
19164 write:
19165
19166 @smallexample @c projectfile
19167 with "my_lib";
19168 project My_Proj is
19169 @dots{}
19170 end My_Proj;
19171 @end smallexample
19172
19173 Even if you have a third-party, non-Ada library, you can still use GNAT's
19174 Project Manager facility to provide a wrapper for it. For example, the
19175 following project, when @code{with}ed by your main project, will link with the
19176 third-party library @file{liba.a}:
19177
19178 @smallexample @c projectfile
19179 @group
19180 project Liba is
19181 for Externally_Built use "true";
19182 for Source_Files use ();
19183 for Library_Dir use "lib";
19184 for Library_Name use "a";
19185 for Library_Kind use "static";
19186 end Liba;
19187 @end group
19188 @end smallexample
19189 This is an alternative to the use of @code{pragma Linker_Options}. It is
19190 especially interesting in the context of systems with several interdependent
19191 static libraries where finding a proper linker order is not easy and best be
19192 left to the tools having visibility over project dependence information.
19193
19194 @noindent
19195 In order to use an Ada library manually, you need to make sure that this
19196 library is on both your source and object path
19197 (see @ref{Search Paths and the Run-Time Library (RTL)}
19198 and @ref{Search Paths for gnatbind}). Furthermore, when the objects are grouped
19199 in an archive or a shared library, you need to specify the desired
19200 library at link time.
19201
19202 For example, you can use the library @file{mylib} installed in
19203 @file{/dir/my_lib_src} and @file{/dir/my_lib_obj} with the following commands:
19204
19205 @smallexample
19206 $ gnatmake -aI/dir/my_lib_src -aO/dir/my_lib_obj my_appl \
19207 -largs -lmy_lib
19208 @end smallexample
19209
19210 @noindent
19211 This can be expressed more simply:
19212 @smallexample
19213 $ gnatmake my_appl
19214 @end smallexample
19215 @noindent
19216 when the following conditions are met:
19217 @itemize @bullet
19218 @item
19219 @file{/dir/my_lib_src} has been added by the user to the environment
19220 variable @env{ADA_INCLUDE_PATH}, or by the administrator to the file
19221 @file{ada_source_path}
19222 @item
19223 @file{/dir/my_lib_obj} has been added by the user to the environment
19224 variable @env{ADA_OBJECTS_PATH}, or by the administrator to the file
19225 @file{ada_object_path}
19226 @item
19227 a pragma @code{Linker_Options} has been added to one of the sources.
19228 For example:
19229
19230 @smallexample @c ada
19231 pragma Linker_Options ("-lmy_lib");
19232 @end smallexample
19233 @end itemize
19234
19235 @node Stand-alone Ada Libraries
19236 @section Stand-alone Ada Libraries
19237 @cindex Stand-alone library, building, using
19238
19239 @menu
19240 * Introduction to Stand-alone Libraries::
19241 * Building a Stand-alone Library::
19242 * Creating a Stand-alone Library to be used in a non-Ada context::
19243 * Restrictions in Stand-alone Libraries::
19244 @end menu
19245
19246 @node Introduction to Stand-alone Libraries
19247 @subsection Introduction to Stand-alone Libraries
19248
19249 @noindent
19250 A Stand-alone Library (abbreviated ``SAL'') is a library that contains the
19251 necessary code to
19252 elaborate the Ada units that are included in the library. In contrast with
19253 an ordinary library, which consists of all sources, objects and @file{ALI}
19254 files of the
19255 library, a SAL may specify a restricted subset of compilation units
19256 to serve as a library interface. In this case, the fully
19257 self-sufficient set of files will normally consist of an objects
19258 archive, the sources of interface units' specs, and the @file{ALI}
19259 files of interface units.
19260 If an interface spec contains a generic unit or an inlined subprogram,
19261 the body's
19262 source must also be provided; if the units that must be provided in the source
19263 form depend on other units, the source and @file{ALI} files of those must
19264 also be provided.
19265
19266 The main purpose of a SAL is to minimize the recompilation overhead of client
19267 applications when a new version of the library is installed. Specifically,
19268 if the interface sources have not changed, client applications do not need to
19269 be recompiled. If, furthermore, a SAL is provided in the shared form and its
19270 version, controlled by @code{Library_Version} attribute, is not changed,
19271 then the clients do not need to be relinked.
19272
19273 SALs also allow the library providers to minimize the amount of library source
19274 text exposed to the clients. Such ``information hiding'' might be useful or
19275 necessary for various reasons.
19276
19277 Stand-alone libraries are also well suited to be used in an executable whose
19278 main routine is not written in Ada.
19279
19280 @node Building a Stand-alone Library
19281 @subsection Building a Stand-alone Library
19282
19283 @noindent
19284 GNAT's Project facility provides a simple way of building and installing
19285 stand-alone libraries; see @ref{Stand-alone Library Projects}.
19286 To be a Stand-alone Library Project, in addition to the two attributes
19287 that make a project a Library Project (@code{Library_Name} and
19288 @code{Library_Dir}; see @ref{Library Projects}), the attribute
19289 @code{Library_Interface} must be defined. For example:
19290
19291 @smallexample @c projectfile
19292 @group
19293 for Library_Dir use "lib_dir";
19294 for Library_Name use "dummy";
19295 for Library_Interface use ("int1", "int1.child");
19296 @end group
19297 @end smallexample
19298
19299 @noindent
19300 Attribute @code{Library_Interface} has a non-empty string list value,
19301 each string in the list designating a unit contained in an immediate source
19302 of the project file.
19303
19304 When a Stand-alone Library is built, first the binder is invoked to build
19305 a package whose name depends on the library name
19306 (@file{^b~dummy.ads/b^B$DUMMY.ADS/B^} in the example above).
19307 This binder-generated package includes initialization and
19308 finalization procedures whose
19309 names depend on the library name (@code{dummyinit} and @code{dummyfinal}
19310 in the example
19311 above). The object corresponding to this package is included in the library.
19312
19313 You must ensure timely (e.g., prior to any use of interfaces in the SAL)
19314 calling of these procedures if a static SAL is built, or if a shared SAL
19315 is built
19316 with the project-level attribute @code{Library_Auto_Init} set to
19317 @code{"false"}.
19318
19319 For a Stand-Alone Library, only the @file{ALI} files of the Interface Units
19320 (those that are listed in attribute @code{Library_Interface}) are copied to
19321 the Library Directory. As a consequence, only the Interface Units may be
19322 imported from Ada units outside of the library. If other units are imported,
19323 the binding phase will fail.
19324
19325 The attribute @code{Library_Src_Dir} may be specified for a
19326 Stand-Alone Library. @code{Library_Src_Dir} is a simple attribute that has a
19327 single string value. Its value must be the path (absolute or relative to the
19328 project directory) of an existing directory. This directory cannot be the
19329 object directory or one of the source directories, but it can be the same as
19330 the library directory. The sources of the Interface
19331 Units of the library that are needed by an Ada client of the library will be
19332 copied to the designated directory, called the Interface Copy directory.
19333 These sources include the specs of the Interface Units, but they may also
19334 include bodies and subunits, when pragmas @code{Inline} or @code{Inline_Always}
19335 are used, or when there is a generic unit in the spec. Before the sources
19336 are copied to the Interface Copy directory, an attempt is made to delete all
19337 files in the Interface Copy directory.
19338
19339 Building stand-alone libraries by hand is somewhat tedious, but for those
19340 occasions when it is necessary here are the steps that you need to perform:
19341 @itemize @bullet
19342 @item
19343 Compile all library sources.
19344
19345 @item
19346 Invoke the binder with the switch @option{-n} (No Ada main program),
19347 with all the @file{ALI} files of the interfaces, and
19348 with the switch @option{-L} to give specific names to the @code{init}
19349 and @code{final} procedures. For example:
19350 @smallexample
19351 gnatbind -n int1.ali int2.ali -Lsal1
19352 @end smallexample
19353
19354 @item
19355 Compile the binder generated file:
19356 @smallexample
19357 gcc -c b~int2.adb
19358 @end smallexample
19359
19360 @item
19361 Link the dynamic library with all the necessary object files,
19362 indicating to the linker the names of the @code{init} (and possibly
19363 @code{final}) procedures for automatic initialization (and finalization).
19364 The built library should be placed in a directory different from
19365 the object directory.
19366
19367 @item
19368 Copy the @code{ALI} files of the interface to the library directory,
19369 add in this copy an indication that it is an interface to a SAL
19370 (i.e., add a word @option{SL} on the line in the @file{ALI} file that starts
19371 with letter ``P'') and make the modified copy of the @file{ALI} file
19372 read-only.
19373 @end itemize
19374
19375 @noindent
19376 Using SALs is not different from using other libraries
19377 (see @ref{Using a library}).
19378
19379 @node Creating a Stand-alone Library to be used in a non-Ada context
19380 @subsection Creating a Stand-alone Library to be used in a non-Ada context
19381
19382 @noindent
19383 It is easy to adapt the SAL build procedure discussed above for use of a SAL in
19384 a non-Ada context.
19385
19386 The only extra step required is to ensure that library interface subprograms
19387 are compatible with the main program, by means of @code{pragma Export}
19388 or @code{pragma Convention}.
19389
19390 Here is an example of simple library interface for use with C main program:
19391
19392 @smallexample @c ada
19393 package Interface is
19394
19395 procedure Do_Something;
19396 pragma Export (C, Do_Something, "do_something");
19397
19398 procedure Do_Something_Else;
19399 pragma Export (C, Do_Something_Else, "do_something_else");
19400
19401 end Interface;
19402 @end smallexample
19403
19404 @noindent
19405 On the foreign language side, you must provide a ``foreign'' view of the
19406 library interface; remember that it should contain elaboration routines in
19407 addition to interface subprograms.
19408
19409 The example below shows the content of @code{mylib_interface.h} (note
19410 that there is no rule for the naming of this file, any name can be used)
19411 @smallexample
19412 /* the library elaboration procedure */
19413 extern void mylibinit (void);
19414
19415 /* the library finalization procedure */
19416 extern void mylibfinal (void);
19417
19418 /* the interface exported by the library */
19419 extern void do_something (void);
19420 extern void do_something_else (void);
19421 @end smallexample
19422
19423 @noindent
19424 Libraries built as explained above can be used from any program, provided
19425 that the elaboration procedures (named @code{mylibinit} in the previous
19426 example) are called before the library services are used. Any number of
19427 libraries can be used simultaneously, as long as the elaboration
19428 procedure of each library is called.
19429
19430 Below is an example of a C program that uses the @code{mylib} library.
19431
19432 @smallexample
19433 #include "mylib_interface.h"
19434
19435 int
19436 main (void)
19437 @{
19438 /* First, elaborate the library before using it */
19439 mylibinit ();
19440
19441 /* Main program, using the library exported entities */
19442 do_something ();
19443 do_something_else ();
19444
19445 /* Library finalization at the end of the program */
19446 mylibfinal ();
19447 return 0;
19448 @}
19449 @end smallexample
19450
19451 @noindent
19452 Note that invoking any library finalization procedure generated by
19453 @code{gnatbind} shuts down the Ada run-time environment.
19454 Consequently, the
19455 finalization of all Ada libraries must be performed at the end of the program.
19456 No call to these libraries or to the Ada run-time library should be made
19457 after the finalization phase.
19458
19459 @node Restrictions in Stand-alone Libraries
19460 @subsection Restrictions in Stand-alone Libraries
19461
19462 @noindent
19463 The pragmas listed below should be used with caution inside libraries,
19464 as they can create incompatibilities with other Ada libraries:
19465 @itemize @bullet
19466 @item pragma @code{Locking_Policy}
19467 @item pragma @code{Queuing_Policy}
19468 @item pragma @code{Task_Dispatching_Policy}
19469 @item pragma @code{Unreserve_All_Interrupts}
19470 @end itemize
19471
19472 @noindent
19473 When using a library that contains such pragmas, the user must make sure
19474 that all libraries use the same pragmas with the same values. Otherwise,
19475 @code{Program_Error} will
19476 be raised during the elaboration of the conflicting
19477 libraries. The usage of these pragmas and its consequences for the user
19478 should therefore be well documented.
19479
19480 Similarly, the traceback in the exception occurrence mechanism should be
19481 enabled or disabled in a consistent manner across all libraries.
19482 Otherwise, Program_Error will be raised during the elaboration of the
19483 conflicting libraries.
19484
19485 If the @code{Version} or @code{Body_Version}
19486 attributes are used inside a library, then you need to
19487 perform a @code{gnatbind} step that specifies all @file{ALI} files in all
19488 libraries, so that version identifiers can be properly computed.
19489 In practice these attributes are rarely used, so this is unlikely
19490 to be a consideration.
19491
19492 @node Rebuilding the GNAT Run-Time Library
19493 @section Rebuilding the GNAT Run-Time Library
19494 @cindex GNAT Run-Time Library, rebuilding
19495 @cindex Building the GNAT Run-Time Library
19496 @cindex Rebuilding the GNAT Run-Time Library
19497 @cindex Run-Time Library, rebuilding
19498
19499 @noindent
19500 It may be useful to recompile the GNAT library in various contexts, the
19501 most important one being the use of partition-wide configuration pragmas
19502 such as @code{Normalize_Scalars}. A special Makefile called
19503 @code{Makefile.adalib} is provided to that effect and can be found in
19504 the directory containing the GNAT library. The location of this
19505 directory depends on the way the GNAT environment has been installed and can
19506 be determined by means of the command:
19507
19508 @smallexample
19509 $ gnatls -v
19510 @end smallexample
19511
19512 @noindent
19513 The last entry in the object search path usually contains the
19514 gnat library. This Makefile contains its own documentation and in
19515 particular the set of instructions needed to rebuild a new library and
19516 to use it.
19517
19518 @node Using the GNU make Utility
19519 @chapter Using the GNU @code{make} Utility
19520 @findex make
19521
19522 @noindent
19523 This chapter offers some examples of makefiles that solve specific
19524 problems. It does not explain how to write a makefile (@pxref{Top,, GNU
19525 make, make, GNU @code{make}}), nor does it try to replace the
19526 @command{gnatmake} utility (@pxref{The GNAT Make Program gnatmake}).
19527
19528 All the examples in this section are specific to the GNU version of
19529 make. Although @command{make} is a standard utility, and the basic language
19530 is the same, these examples use some advanced features found only in
19531 @code{GNU make}.
19532
19533 @menu
19534 * Using gnatmake in a Makefile::
19535 * Automatically Creating a List of Directories::
19536 * Generating the Command Line Switches::
19537 * Overcoming Command Line Length Limits::
19538 @end menu
19539
19540 @node Using gnatmake in a Makefile
19541 @section Using gnatmake in a Makefile
19542 @findex makefile
19543 @cindex GNU make
19544
19545 @noindent
19546 Complex project organizations can be handled in a very powerful way by
19547 using GNU make combined with gnatmake. For instance, here is a Makefile
19548 which allows you to build each subsystem of a big project into a separate
19549 shared library. Such a makefile allows you to significantly reduce the link
19550 time of very big applications while maintaining full coherence at
19551 each step of the build process.
19552
19553 The list of dependencies are handled automatically by
19554 @command{gnatmake}. The Makefile is simply used to call gnatmake in each of
19555 the appropriate directories.
19556
19557 Note that you should also read the example on how to automatically
19558 create the list of directories
19559 (@pxref{Automatically Creating a List of Directories})
19560 which might help you in case your project has a lot of subdirectories.
19561
19562 @smallexample
19563 @iftex
19564 @leftskip=0cm
19565 @font@heightrm=cmr8
19566 @heightrm
19567 @end iftex
19568 ## This Makefile is intended to be used with the following directory
19569 ## configuration:
19570 ## - The sources are split into a series of csc (computer software components)
19571 ## Each of these csc is put in its own directory.
19572 ## Their name are referenced by the directory names.
19573 ## They will be compiled into shared library (although this would also work
19574 ## with static libraries
19575 ## - The main program (and possibly other packages that do not belong to any
19576 ## csc is put in the top level directory (where the Makefile is).
19577 ## toplevel_dir __ first_csc (sources) __ lib (will contain the library)
19578 ## \_ second_csc (sources) __ lib (will contain the library)
19579 ## \_ @dots{}
19580 ## Although this Makefile is build for shared library, it is easy to modify
19581 ## to build partial link objects instead (modify the lines with -shared and
19582 ## gnatlink below)
19583 ##
19584 ## With this makefile, you can change any file in the system or add any new
19585 ## file, and everything will be recompiled correctly (only the relevant shared
19586 ## objects will be recompiled, and the main program will be re-linked).
19587
19588 # The list of computer software component for your project. This might be
19589 # generated automatically.
19590 CSC_LIST=aa bb cc
19591
19592 # Name of the main program (no extension)
19593 MAIN=main
19594
19595 # If we need to build objects with -fPIC, uncomment the following line
19596 #NEED_FPIC=-fPIC
19597
19598 # The following variable should give the directory containing libgnat.so
19599 # You can get this directory through 'gnatls -v'. This is usually the last
19600 # directory in the Object_Path.
19601 GLIB=@dots{}
19602
19603 # The directories for the libraries
19604 # (This macro expands the list of CSC to the list of shared libraries, you
19605 # could simply use the expanded form:
19606 # LIB_DIR=aa/lib/libaa.so bb/lib/libbb.so cc/lib/libcc.so
19607 LIB_DIR=$@{foreach dir,$@{CSC_LIST@},$@{dir@}/lib/lib$@{dir@}.so@}
19608
19609 $@{MAIN@}: objects $@{LIB_DIR@}
19610 gnatbind $@{MAIN@} $@{CSC_LIST:%=-aO%/lib@} -shared
19611 gnatlink $@{MAIN@} $@{CSC_LIST:%=-l%@}
19612
19613 objects::
19614 # recompile the sources
19615 gnatmake -c -i $@{MAIN@}.adb $@{NEED_FPIC@} $@{CSC_LIST:%=-I%@}
19616
19617 # Note: In a future version of GNAT, the following commands will be simplified
19618 # by a new tool, gnatmlib
19619 $@{LIB_DIR@}:
19620 mkdir -p $@{dir $@@ @}
19621 cd $@{dir $@@ @} && gcc -shared -o $@{notdir $@@ @} ../*.o -L$@{GLIB@} -lgnat
19622 cd $@{dir $@@ @} && cp -f ../*.ali .
19623
19624 # The dependencies for the modules
19625 # Note that we have to force the expansion of *.o, since in some cases
19626 # make won't be able to do it itself.
19627 aa/lib/libaa.so: $@{wildcard aa/*.o@}
19628 bb/lib/libbb.so: $@{wildcard bb/*.o@}
19629 cc/lib/libcc.so: $@{wildcard cc/*.o@}
19630
19631 # Make sure all of the shared libraries are in the path before starting the
19632 # program
19633 run::
19634 LD_LIBRARY_PATH=`pwd`/aa/lib:`pwd`/bb/lib:`pwd`/cc/lib ./$@{MAIN@}
19635
19636 clean::
19637 $@{RM@} -rf $@{CSC_LIST:%=%/lib@}
19638 $@{RM@} $@{CSC_LIST:%=%/*.ali@}
19639 $@{RM@} $@{CSC_LIST:%=%/*.o@}
19640 $@{RM@} *.o *.ali $@{MAIN@}
19641 @end smallexample
19642
19643 @node Automatically Creating a List of Directories
19644 @section Automatically Creating a List of Directories
19645
19646 @noindent
19647 In most makefiles, you will have to specify a list of directories, and
19648 store it in a variable. For small projects, it is often easier to
19649 specify each of them by hand, since you then have full control over what
19650 is the proper order for these directories, which ones should be
19651 included.
19652
19653 However, in larger projects, which might involve hundreds of
19654 subdirectories, it might be more convenient to generate this list
19655 automatically.
19656
19657 The example below presents two methods. The first one, although less
19658 general, gives you more control over the list. It involves wildcard
19659 characters, that are automatically expanded by @command{make}. Its
19660 shortcoming is that you need to explicitly specify some of the
19661 organization of your project, such as for instance the directory tree
19662 depth, whether some directories are found in a separate tree, @enddots{}
19663
19664 The second method is the most general one. It requires an external
19665 program, called @command{find}, which is standard on all Unix systems. All
19666 the directories found under a given root directory will be added to the
19667 list.
19668
19669 @smallexample
19670 @iftex
19671 @leftskip=0cm
19672 @font@heightrm=cmr8
19673 @heightrm
19674 @end iftex
19675 # The examples below are based on the following directory hierarchy:
19676 # All the directories can contain any number of files
19677 # ROOT_DIRECTORY -> a -> aa -> aaa
19678 # -> ab
19679 # -> ac
19680 # -> b -> ba -> baa
19681 # -> bb
19682 # -> bc
19683 # This Makefile creates a variable called DIRS, that can be reused any time
19684 # you need this list (see the other examples in this section)
19685
19686 # The root of your project's directory hierarchy
19687 ROOT_DIRECTORY=.
19688
19689 ####
19690 # First method: specify explicitly the list of directories
19691 # This allows you to specify any subset of all the directories you need.
19692 ####
19693
19694 DIRS := a/aa/ a/ab/ b/ba/
19695
19696 ####
19697 # Second method: use wildcards
19698 # Note that the argument(s) to wildcard below should end with a '/'.
19699 # Since wildcards also return file names, we have to filter them out
19700 # to avoid duplicate directory names.
19701 # We thus use make's @code{dir} and @code{sort} functions.
19702 # It sets DIRs to the following value (note that the directories aaa and baa
19703 # are not given, unless you change the arguments to wildcard).
19704 # DIRS= ./a/a/ ./b/ ./a/aa/ ./a/ab/ ./a/ac/ ./b/ba/ ./b/bb/ ./b/bc/
19705 ####
19706
19707 DIRS := $@{sort $@{dir $@{wildcard $@{ROOT_DIRECTORY@}/*/
19708 $@{ROOT_DIRECTORY@}/*/*/@}@}@}
19709
19710 ####
19711 # Third method: use an external program
19712 # This command is much faster if run on local disks, avoiding NFS slowdowns.
19713 # This is the most complete command: it sets DIRs to the following value:
19714 # DIRS= ./a ./a/aa ./a/aa/aaa ./a/ab ./a/ac ./b ./b/ba ./b/ba/baa ./b/bb ./b/bc
19715 ####
19716
19717 DIRS := $@{shell find $@{ROOT_DIRECTORY@} -type d -print@}
19718
19719 @end smallexample
19720
19721 @node Generating the Command Line Switches
19722 @section Generating the Command Line Switches
19723
19724 @noindent
19725 Once you have created the list of directories as explained in the
19726 previous section (@pxref{Automatically Creating a List of Directories}),
19727 you can easily generate the command line arguments to pass to gnatmake.
19728
19729 For the sake of completeness, this example assumes that the source path
19730 is not the same as the object path, and that you have two separate lists
19731 of directories.
19732
19733 @smallexample
19734 # see "Automatically creating a list of directories" to create
19735 # these variables
19736 SOURCE_DIRS=
19737 OBJECT_DIRS=
19738
19739 GNATMAKE_SWITCHES := $@{patsubst %,-aI%,$@{SOURCE_DIRS@}@}
19740 GNATMAKE_SWITCHES += $@{patsubst %,-aO%,$@{OBJECT_DIRS@}@}
19741
19742 all:
19743 gnatmake $@{GNATMAKE_SWITCHES@} main_unit
19744 @end smallexample
19745
19746 @node Overcoming Command Line Length Limits
19747 @section Overcoming Command Line Length Limits
19748
19749 @noindent
19750 One problem that might be encountered on big projects is that many
19751 operating systems limit the length of the command line. It is thus hard to give
19752 gnatmake the list of source and object directories.
19753
19754 This example shows how you can set up environment variables, which will
19755 make @command{gnatmake} behave exactly as if the directories had been
19756 specified on the command line, but have a much higher length limit (or
19757 even none on most systems).
19758
19759 It assumes that you have created a list of directories in your Makefile,
19760 using one of the methods presented in
19761 @ref{Automatically Creating a List of Directories}.
19762 For the sake of completeness, we assume that the object
19763 path (where the ALI files are found) is different from the sources patch.
19764
19765 Note a small trick in the Makefile below: for efficiency reasons, we
19766 create two temporary variables (SOURCE_LIST and OBJECT_LIST), that are
19767 expanded immediately by @code{make}. This way we overcome the standard
19768 make behavior which is to expand the variables only when they are
19769 actually used.
19770
19771 On Windows, if you are using the standard Windows command shell, you must
19772 replace colons with semicolons in the assignments to these variables.
19773
19774 @smallexample
19775 @iftex
19776 @leftskip=0cm
19777 @font@heightrm=cmr8
19778 @heightrm
19779 @end iftex
19780 # In this example, we create both ADA_INCLUDE_PATH and ADA_OBJECT_PATH.
19781 # This is the same thing as putting the -I arguments on the command line.
19782 # (the equivalent of using -aI on the command line would be to define
19783 # only ADA_INCLUDE_PATH, the equivalent of -aO is ADA_OBJECT_PATH).
19784 # You can of course have different values for these variables.
19785 #
19786 # Note also that we need to keep the previous values of these variables, since
19787 # they might have been set before running 'make' to specify where the GNAT
19788 # library is installed.
19789
19790 # see "Automatically creating a list of directories" to create these
19791 # variables
19792 SOURCE_DIRS=
19793 OBJECT_DIRS=
19794
19795 empty:=
19796 space:=$@{empty@} $@{empty@}
19797 SOURCE_LIST := $@{subst $@{space@},:,$@{SOURCE_DIRS@}@}
19798 OBJECT_LIST := $@{subst $@{space@},:,$@{OBJECT_DIRS@}@}
19799 ADA_INCLUDE_PATH += $@{SOURCE_LIST@}
19800 ADA_OBJECT_PATH += $@{OBJECT_LIST@}
19801 export ADA_INCLUDE_PATH
19802 export ADA_OBJECT_PATH
19803
19804 all:
19805 gnatmake main_unit
19806 @end smallexample
19807 @end ifclear
19808
19809 @node Memory Management Issues
19810 @chapter Memory Management Issues
19811
19812 @noindent
19813 This chapter describes some useful memory pools provided in the GNAT library
19814 and in particular the GNAT Debug Pool facility, which can be used to detect
19815 incorrect uses of access values (including ``dangling references'').
19816 @ifclear vms
19817 It also describes the @command{gnatmem} tool, which can be used to track down
19818 ``memory leaks''.
19819 @end ifclear
19820
19821 @menu
19822 * Some Useful Memory Pools::
19823 * The GNAT Debug Pool Facility::
19824 @ifclear vms
19825 * The gnatmem Tool::
19826 @end ifclear
19827 @end menu
19828
19829 @node Some Useful Memory Pools
19830 @section Some Useful Memory Pools
19831 @findex Memory Pool
19832 @cindex storage, pool
19833
19834 @noindent
19835 The @code{System.Pool_Global} package offers the Unbounded_No_Reclaim_Pool
19836 storage pool. Allocations use the standard system call @code{malloc} while
19837 deallocations use the standard system call @code{free}. No reclamation is
19838 performed when the pool goes out of scope. For performance reasons, the
19839 standard default Ada allocators/deallocators do not use any explicit storage
19840 pools but if they did, they could use this storage pool without any change in
19841 behavior. That is why this storage pool is used when the user
19842 manages to make the default implicit allocator explicit as in this example:
19843 @smallexample @c ada
19844 type T1 is access Something;
19845 -- no Storage pool is defined for T2
19846 type T2 is access Something_Else;
19847 for T2'Storage_Pool use T1'Storage_Pool;
19848 -- the above is equivalent to
19849 for T2'Storage_Pool use System.Pool_Global.Global_Pool_Object;
19850 @end smallexample
19851
19852 @noindent
19853 The @code{System.Pool_Local} package offers the Unbounded_Reclaim_Pool storage
19854 pool. The allocation strategy is similar to @code{Pool_Local}'s
19855 except that the all
19856 storage allocated with this pool is reclaimed when the pool object goes out of
19857 scope. This pool provides a explicit mechanism similar to the implicit one
19858 provided by several Ada 83 compilers for allocations performed through a local
19859 access type and whose purpose was to reclaim memory when exiting the
19860 scope of a given local access. As an example, the following program does not
19861 leak memory even though it does not perform explicit deallocation:
19862
19863 @smallexample @c ada
19864 with System.Pool_Local;
19865 procedure Pooloc1 is
19866 procedure Internal is
19867 type A is access Integer;
19868 X : System.Pool_Local.Unbounded_Reclaim_Pool;
19869 for A'Storage_Pool use X;
19870 v : A;
19871 begin
19872 for I in 1 .. 50 loop
19873 v := new Integer;
19874 end loop;
19875 end Internal;
19876 begin
19877 for I in 1 .. 100 loop
19878 Internal;
19879 end loop;
19880 end Pooloc1;
19881 @end smallexample
19882
19883 @noindent
19884 The @code{System.Pool_Size} package implements the Stack_Bounded_Pool used when
19885 @code{Storage_Size} is specified for an access type.
19886 The whole storage for the pool is
19887 allocated at once, usually on the stack at the point where the access type is
19888 elaborated. It is automatically reclaimed when exiting the scope where the
19889 access type is defined. This package is not intended to be used directly by the
19890 user and it is implicitly used for each such declaration:
19891
19892 @smallexample @c ada
19893 type T1 is access Something;
19894 for T1'Storage_Size use 10_000;
19895 @end smallexample
19896
19897 @node The GNAT Debug Pool Facility
19898 @section The GNAT Debug Pool Facility
19899 @findex Debug Pool
19900 @cindex storage, pool, memory corruption
19901
19902 @noindent
19903 The use of unchecked deallocation and unchecked conversion can easily
19904 lead to incorrect memory references. The problems generated by such
19905 references are usually difficult to tackle because the symptoms can be
19906 very remote from the origin of the problem. In such cases, it is
19907 very helpful to detect the problem as early as possible. This is the
19908 purpose of the Storage Pool provided by @code{GNAT.Debug_Pools}.
19909
19910 In order to use the GNAT specific debugging pool, the user must
19911 associate a debug pool object with each of the access types that may be
19912 related to suspected memory problems. See Ada Reference Manual 13.11.
19913 @smallexample @c ada
19914 type Ptr is access Some_Type;
19915 Pool : GNAT.Debug_Pools.Debug_Pool;
19916 for Ptr'Storage_Pool use Pool;
19917 @end smallexample
19918
19919 @noindent
19920 @code{GNAT.Debug_Pools} is derived from a GNAT-specific kind of
19921 pool: the @code{Checked_Pool}. Such pools, like standard Ada storage pools,
19922 allow the user to redefine allocation and deallocation strategies. They
19923 also provide a checkpoint for each dereference, through the use of
19924 the primitive operation @code{Dereference} which is implicitly called at
19925 each dereference of an access value.
19926
19927 Once an access type has been associated with a debug pool, operations on
19928 values of the type may raise four distinct exceptions,
19929 which correspond to four potential kinds of memory corruption:
19930 @itemize @bullet
19931 @item
19932 @code{GNAT.Debug_Pools.Accessing_Not_Allocated_Storage}
19933 @item
19934 @code{GNAT.Debug_Pools.Accessing_Deallocated_Storage}
19935 @item
19936 @code{GNAT.Debug_Pools.Freeing_Not_Allocated_Storage}
19937 @item
19938 @code{GNAT.Debug_Pools.Freeing_Deallocated_Storage }
19939 @end itemize
19940
19941 @noindent
19942 For types associated with a Debug_Pool, dynamic allocation is performed using
19943 the standard GNAT allocation routine. References to all allocated chunks of
19944 memory are kept in an internal dictionary. Several deallocation strategies are
19945 provided, whereupon the user can choose to release the memory to the system,
19946 keep it allocated for further invalid access checks, or fill it with an easily
19947 recognizable pattern for debug sessions. The memory pattern is the old IBM
19948 hexadecimal convention: @code{16#DEADBEEF#}.
19949
19950 See the documentation in the file g-debpoo.ads for more information on the
19951 various strategies.
19952
19953 Upon each dereference, a check is made that the access value denotes a
19954 properly allocated memory location. Here is a complete example of use of
19955 @code{Debug_Pools}, that includes typical instances of memory corruption:
19956 @smallexample @c ada
19957 @iftex
19958 @leftskip=0cm
19959 @end iftex
19960 with Gnat.Io; use Gnat.Io;
19961 with Unchecked_Deallocation;
19962 with Unchecked_Conversion;
19963 with GNAT.Debug_Pools;
19964 with System.Storage_Elements;
19965 with Ada.Exceptions; use Ada.Exceptions;
19966 procedure Debug_Pool_Test is
19967
19968 type T is access Integer;
19969 type U is access all T;
19970
19971 P : GNAT.Debug_Pools.Debug_Pool;
19972 for T'Storage_Pool use P;
19973
19974 procedure Free is new Unchecked_Deallocation (Integer, T);
19975 function UC is new Unchecked_Conversion (U, T);
19976 A, B : aliased T;
19977
19978 procedure Info is new GNAT.Debug_Pools.Print_Info(Put_Line);
19979
19980 begin
19981 Info (P);
19982 A := new Integer;
19983 B := new Integer;
19984 B := A;
19985 Info (P);
19986 Free (A);
19987 begin
19988 Put_Line (Integer'Image(B.all));
19989 exception
19990 when E : others => Put_Line ("raised: " & Exception_Name (E));
19991 end;
19992 begin
19993 Free (B);
19994 exception
19995 when E : others => Put_Line ("raised: " & Exception_Name (E));
19996 end;
19997 B := UC(A'Access);
19998 begin
19999 Put_Line (Integer'Image(B.all));
20000 exception
20001 when E : others => Put_Line ("raised: " & Exception_Name (E));
20002 end;
20003 begin
20004 Free (B);
20005 exception
20006 when E : others => Put_Line ("raised: " & Exception_Name (E));
20007 end;
20008 Info (P);
20009 end Debug_Pool_Test;
20010 @end smallexample
20011
20012 @noindent
20013 The debug pool mechanism provides the following precise diagnostics on the
20014 execution of this erroneous program:
20015 @smallexample
20016 Debug Pool info:
20017 Total allocated bytes : 0
20018 Total deallocated bytes : 0
20019 Current Water Mark: 0
20020 High Water Mark: 0
20021
20022 Debug Pool info:
20023 Total allocated bytes : 8
20024 Total deallocated bytes : 0
20025 Current Water Mark: 8
20026 High Water Mark: 8
20027
20028 raised: GNAT.DEBUG_POOLS.ACCESSING_DEALLOCATED_STORAGE
20029 raised: GNAT.DEBUG_POOLS.FREEING_DEALLOCATED_STORAGE
20030 raised: GNAT.DEBUG_POOLS.ACCESSING_NOT_ALLOCATED_STORAGE
20031 raised: GNAT.DEBUG_POOLS.FREEING_NOT_ALLOCATED_STORAGE
20032 Debug Pool info:
20033 Total allocated bytes : 8
20034 Total deallocated bytes : 4
20035 Current Water Mark: 4
20036 High Water Mark: 8
20037 @end smallexample
20038
20039 @ifclear vms
20040 @node The gnatmem Tool
20041 @section The @command{gnatmem} Tool
20042 @findex gnatmem
20043
20044 @noindent
20045 The @code{gnatmem} utility monitors dynamic allocation and
20046 deallocation activity in a program, and displays information about
20047 incorrect deallocations and possible sources of memory leaks.
20048 It is designed to work in association with a static runtime library
20049 only and in this context provides three types of information:
20050 @itemize @bullet
20051 @item
20052 General information concerning memory management, such as the total
20053 number of allocations and deallocations, the amount of allocated
20054 memory and the high water mark, i.e.@: the largest amount of allocated
20055 memory in the course of program execution.
20056
20057 @item
20058 Backtraces for all incorrect deallocations, that is to say deallocations
20059 which do not correspond to a valid allocation.
20060
20061 @item
20062 Information on each allocation that is potentially the origin of a memory
20063 leak.
20064 @end itemize
20065
20066 @menu
20067 * Running gnatmem::
20068 * Switches for gnatmem::
20069 * Example of gnatmem Usage::
20070 @end menu
20071
20072 @node Running gnatmem
20073 @subsection Running @code{gnatmem}
20074
20075 @noindent
20076 @code{gnatmem} makes use of the output created by the special version of
20077 allocation and deallocation routines that record call information. This
20078 allows to obtain accurate dynamic memory usage history at a minimal cost to
20079 the execution speed. Note however, that @code{gnatmem} is not supported on
20080 all platforms (currently, it is supported on AIX, HP-UX, GNU/Linux,
20081 Solaris and Windows NT/2000/XP (x86).
20082
20083 @noindent
20084 The @code{gnatmem} command has the form
20085
20086 @smallexample
20087 $ gnatmem @ovar{switches} user_program
20088 @end smallexample
20089
20090 @noindent
20091 The program must have been linked with the instrumented version of the
20092 allocation and deallocation routines. This is done by linking with the
20093 @file{libgmem.a} library. For correct symbolic backtrace information,
20094 the user program should be compiled with debugging options
20095 (see @ref{Switches for gcc}). For example to build @file{my_program}:
20096
20097 @smallexample
20098 $ gnatmake -g my_program -largs -lgmem
20099 @end smallexample
20100
20101 @noindent
20102 As library @file{libgmem.a} contains an alternate body for package
20103 @code{System.Memory}, @file{s-memory.adb} should not be compiled and linked
20104 when an executable is linked with library @file{libgmem.a}. It is then not
20105 recommended to use @command{gnatmake} with switch @option{^-a^/ALL_FILES^}.
20106
20107 @noindent
20108 When @file{my_program} is executed, the file @file{gmem.out} is produced.
20109 This file contains information about all allocations and deallocations
20110 performed by the program. It is produced by the instrumented allocations and
20111 deallocations routines and will be used by @code{gnatmem}.
20112
20113 In order to produce symbolic backtrace information for allocations and
20114 deallocations performed by the GNAT run-time library, you need to use a
20115 version of that library that has been compiled with the @option{-g} switch
20116 (see @ref{Rebuilding the GNAT Run-Time Library}).
20117
20118 Gnatmem must be supplied with the @file{gmem.out} file and the executable to
20119 examine. If the location of @file{gmem.out} file was not explicitly supplied by
20120 @option{-i} switch, gnatmem will assume that this file can be found in the
20121 current directory. For example, after you have executed @file{my_program},
20122 @file{gmem.out} can be analyzed by @code{gnatmem} using the command:
20123
20124 @smallexample
20125 $ gnatmem my_program
20126 @end smallexample
20127
20128 @noindent
20129 This will produce the output with the following format:
20130
20131 *************** debut cc
20132 @smallexample
20133 $ gnatmem my_program
20134
20135 Global information
20136 ------------------
20137 Total number of allocations : 45
20138 Total number of deallocations : 6
20139 Final Water Mark (non freed mem) : 11.29 Kilobytes
20140 High Water Mark : 11.40 Kilobytes
20141
20142 .
20143 .
20144 .
20145 Allocation Root # 2
20146 -------------------
20147 Number of non freed allocations : 11
20148 Final Water Mark (non freed mem) : 1.16 Kilobytes
20149 High Water Mark : 1.27 Kilobytes
20150 Backtrace :
20151 my_program.adb:23 my_program.alloc
20152 .
20153 .
20154 .
20155 @end smallexample
20156
20157 The first block of output gives general information. In this case, the
20158 Ada construct ``@code{@b{new}}'' was executed 45 times, and only 6 calls to an
20159 Unchecked_Deallocation routine occurred.
20160
20161 @noindent
20162 Subsequent paragraphs display information on all allocation roots.
20163 An allocation root is a specific point in the execution of the program
20164 that generates some dynamic allocation, such as a ``@code{@b{new}}''
20165 construct. This root is represented by an execution backtrace (or subprogram
20166 call stack). By default the backtrace depth for allocations roots is 1, so
20167 that a root corresponds exactly to a source location. The backtrace can
20168 be made deeper, to make the root more specific.
20169
20170 @node Switches for gnatmem
20171 @subsection Switches for @code{gnatmem}
20172
20173 @noindent
20174 @code{gnatmem} recognizes the following switches:
20175
20176 @table @option
20177
20178 @item -q
20179 @cindex @option{-q} (@code{gnatmem})
20180 Quiet. Gives the minimum output needed to identify the origin of the
20181 memory leaks. Omits statistical information.
20182
20183 @item @var{N}
20184 @cindex @var{N} (@code{gnatmem})
20185 N is an integer literal (usually between 1 and 10) which controls the
20186 depth of the backtraces defining allocation root. The default value for
20187 N is 1. The deeper the backtrace, the more precise the localization of
20188 the root. Note that the total number of roots can depend on this
20189 parameter. This parameter must be specified @emph{before} the name of the
20190 executable to be analyzed, to avoid ambiguity.
20191
20192 @item -b n
20193 @cindex @option{-b} (@code{gnatmem})
20194 This switch has the same effect as just depth parameter.
20195
20196 @item -i @var{file}
20197 @cindex @option{-i} (@code{gnatmem})
20198 Do the @code{gnatmem} processing starting from @file{file}, rather than
20199 @file{gmem.out} in the current directory.
20200
20201 @item -m n
20202 @cindex @option{-m} (@code{gnatmem})
20203 This switch causes @code{gnatmem} to mask the allocation roots that have less
20204 than n leaks. The default value is 1. Specifying the value of 0 will allow to
20205 examine even the roots that didn't result in leaks.
20206
20207 @item -s order
20208 @cindex @option{-s} (@code{gnatmem})
20209 This switch causes @code{gnatmem} to sort the allocation roots according to the
20210 specified order of sort criteria, each identified by a single letter. The
20211 currently supported criteria are @code{n, h, w} standing respectively for
20212 number of unfreed allocations, high watermark, and final watermark
20213 corresponding to a specific root. The default order is @code{nwh}.
20214
20215 @end table
20216
20217 @node Example of gnatmem Usage
20218 @subsection Example of @code{gnatmem} Usage
20219
20220 @noindent
20221 The following example shows the use of @code{gnatmem}
20222 on a simple memory-leaking program.
20223 Suppose that we have the following Ada program:
20224
20225 @smallexample @c ada
20226 @group
20227 @cartouche
20228 with Unchecked_Deallocation;
20229 procedure Test_Gm is
20230
20231 type T is array (1..1000) of Integer;
20232 type Ptr is access T;
20233 procedure Free is new Unchecked_Deallocation (T, Ptr);
20234 A : Ptr;
20235
20236 procedure My_Alloc is
20237 begin
20238 A := new T;
20239 end My_Alloc;
20240
20241 procedure My_DeAlloc is
20242 B : Ptr := A;
20243 begin
20244 Free (B);
20245 end My_DeAlloc;
20246
20247 begin
20248 My_Alloc;
20249 for I in 1 .. 5 loop
20250 for J in I .. 5 loop
20251 My_Alloc;
20252 end loop;
20253 My_Dealloc;
20254 end loop;
20255 end;
20256 @end cartouche
20257 @end group
20258 @end smallexample
20259
20260 @noindent
20261 The program needs to be compiled with debugging option and linked with
20262 @code{gmem} library:
20263
20264 @smallexample
20265 $ gnatmake -g test_gm -largs -lgmem
20266 @end smallexample
20267
20268 @noindent
20269 Then we execute the program as usual:
20270
20271 @smallexample
20272 $ test_gm
20273 @end smallexample
20274
20275 @noindent
20276 Then @code{gnatmem} is invoked simply with
20277 @smallexample
20278 $ gnatmem test_gm
20279 @end smallexample
20280
20281 @noindent
20282 which produces the following output (result may vary on different platforms):
20283
20284 @smallexample
20285 Global information
20286 ------------------
20287 Total number of allocations : 18
20288 Total number of deallocations : 5
20289 Final Water Mark (non freed mem) : 53.00 Kilobytes
20290 High Water Mark : 56.90 Kilobytes
20291
20292 Allocation Root # 1
20293 -------------------
20294 Number of non freed allocations : 11
20295 Final Water Mark (non freed mem) : 42.97 Kilobytes
20296 High Water Mark : 46.88 Kilobytes
20297 Backtrace :
20298 test_gm.adb:11 test_gm.my_alloc
20299
20300 Allocation Root # 2
20301 -------------------
20302 Number of non freed allocations : 1
20303 Final Water Mark (non freed mem) : 10.02 Kilobytes
20304 High Water Mark : 10.02 Kilobytes
20305 Backtrace :
20306 s-secsta.adb:81 system.secondary_stack.ss_init
20307
20308 Allocation Root # 3
20309 -------------------
20310 Number of non freed allocations : 1
20311 Final Water Mark (non freed mem) : 12 Bytes
20312 High Water Mark : 12 Bytes
20313 Backtrace :
20314 s-secsta.adb:181 system.secondary_stack.ss_init
20315 @end smallexample
20316
20317 @noindent
20318 Note that the GNAT run time contains itself a certain number of
20319 allocations that have no corresponding deallocation,
20320 as shown here for root #2 and root
20321 #3. This is a normal behavior when the number of non-freed allocations
20322 is one, it allocates dynamic data structures that the run time needs for
20323 the complete lifetime of the program. Note also that there is only one
20324 allocation root in the user program with a single line back trace:
20325 test_gm.adb:11 test_gm.my_alloc, whereas a careful analysis of the
20326 program shows that 'My_Alloc' is called at 2 different points in the
20327 source (line 21 and line 24). If those two allocation roots need to be
20328 distinguished, the backtrace depth parameter can be used:
20329
20330 @smallexample
20331 $ gnatmem 3 test_gm
20332 @end smallexample
20333
20334 @noindent
20335 which will give the following output:
20336
20337 @smallexample
20338 Global information
20339 ------------------
20340 Total number of allocations : 18
20341 Total number of deallocations : 5
20342 Final Water Mark (non freed mem) : 53.00 Kilobytes
20343 High Water Mark : 56.90 Kilobytes
20344
20345 Allocation Root # 1
20346 -------------------
20347 Number of non freed allocations : 10
20348 Final Water Mark (non freed mem) : 39.06 Kilobytes
20349 High Water Mark : 42.97 Kilobytes
20350 Backtrace :
20351 test_gm.adb:11 test_gm.my_alloc
20352 test_gm.adb:24 test_gm
20353 b_test_gm.c:52 main
20354
20355 Allocation Root # 2
20356 -------------------
20357 Number of non freed allocations : 1
20358 Final Water Mark (non freed mem) : 10.02 Kilobytes
20359 High Water Mark : 10.02 Kilobytes
20360 Backtrace :
20361 s-secsta.adb:81 system.secondary_stack.ss_init
20362 s-secsta.adb:283 <system__secondary_stack___elabb>
20363 b_test_gm.c:33 adainit
20364
20365 Allocation Root # 3
20366 -------------------
20367 Number of non freed allocations : 1
20368 Final Water Mark (non freed mem) : 3.91 Kilobytes
20369 High Water Mark : 3.91 Kilobytes
20370 Backtrace :
20371 test_gm.adb:11 test_gm.my_alloc
20372 test_gm.adb:21 test_gm
20373 b_test_gm.c:52 main
20374
20375 Allocation Root # 4
20376 -------------------
20377 Number of non freed allocations : 1
20378 Final Water Mark (non freed mem) : 12 Bytes
20379 High Water Mark : 12 Bytes
20380 Backtrace :
20381 s-secsta.adb:181 system.secondary_stack.ss_init
20382 s-secsta.adb:283 <system__secondary_stack___elabb>
20383 b_test_gm.c:33 adainit
20384 @end smallexample
20385
20386 @noindent
20387 The allocation root #1 of the first example has been split in 2 roots #1
20388 and #3 thanks to the more precise associated backtrace.
20389
20390 @end ifclear
20391
20392 @node Stack Related Facilities
20393 @chapter Stack Related Facilities
20394
20395 @noindent
20396 This chapter describes some useful tools associated with stack
20397 checking and analysis. In
20398 particular, it deals with dynamic and static stack usage measurements.
20399
20400 @menu
20401 * Stack Overflow Checking::
20402 * Static Stack Usage Analysis::
20403 * Dynamic Stack Usage Analysis::
20404 @end menu
20405
20406 @node Stack Overflow Checking
20407 @section Stack Overflow Checking
20408 @cindex Stack Overflow Checking
20409 @cindex -fstack-check
20410
20411 @noindent
20412 For most operating systems, @command{gcc} does not perform stack overflow
20413 checking by default. This means that if the main environment task or
20414 some other task exceeds the available stack space, then unpredictable
20415 behavior will occur. Most native systems offer some level of protection by
20416 adding a guard page at the end of each task stack. This mechanism is usually
20417 not enough for dealing properly with stack overflow situations because
20418 a large local variable could ``jump'' above the guard page.
20419 Furthermore, when the
20420 guard page is hit, there may not be any space left on the stack for executing
20421 the exception propagation code. Enabling stack checking avoids
20422 such situations.
20423
20424 To activate stack checking, compile all units with the gcc option
20425 @option{-fstack-check}. For example:
20426
20427 @smallexample
20428 gcc -c -fstack-check package1.adb
20429 @end smallexample
20430
20431 @noindent
20432 Units compiled with this option will generate extra instructions to check
20433 that any use of the stack (for procedure calls or for declaring local
20434 variables in declare blocks) does not exceed the available stack space.
20435 If the space is exceeded, then a @code{Storage_Error} exception is raised.
20436
20437 For declared tasks, the stack size is controlled by the size
20438 given in an applicable @code{Storage_Size} pragma or by the value specified
20439 at bind time with @option{-d} (@pxref{Switches for gnatbind}) or is set to
20440 the default size as defined in the GNAT runtime otherwise.
20441
20442 For the environment task, the stack size depends on
20443 system defaults and is unknown to the compiler. Stack checking
20444 may still work correctly if a fixed
20445 size stack is allocated, but this cannot be guaranteed.
20446 @ifclear vms
20447 To ensure that a clean exception is signalled for stack
20448 overflow, set the environment variable
20449 @env{GNAT_STACK_LIMIT} to indicate the maximum
20450 stack area that can be used, as in:
20451 @cindex GNAT_STACK_LIMIT
20452
20453 @smallexample
20454 SET GNAT_STACK_LIMIT 1600
20455 @end smallexample
20456
20457 @noindent
20458 The limit is given in kilobytes, so the above declaration would
20459 set the stack limit of the environment task to 1.6 megabytes.
20460 Note that the only purpose of this usage is to limit the amount
20461 of stack used by the environment task. If it is necessary to
20462 increase the amount of stack for the environment task, then this
20463 is an operating systems issue, and must be addressed with the
20464 appropriate operating systems commands.
20465 @end ifclear
20466 @ifset vms
20467 To have a fixed size stack in the environment task, the stack must be put
20468 in the P0 address space and its size specified. Use these switches to
20469 create a p0 image:
20470
20471 @smallexample
20472 gnatmake my_progs -largs "-Wl,--opt=STACK=4000,/p0image"
20473 @end smallexample
20474
20475 @noindent
20476 The quotes are required to keep case. The number after @samp{STACK=} is the
20477 size of the environmental task stack in pagelets (512 bytes). In this example
20478 the stack size is about 2 megabytes.
20479
20480 @noindent
20481 A consequence of the @option{/p0image} qualifier is also to makes RMS buffers
20482 be placed in P0 space. Refer to @cite{HP OpenVMS Linker Utility Manual} for
20483 more details about the @option{/p0image} qualifier and the @option{stack}
20484 option.
20485 @end ifset
20486
20487 @node Static Stack Usage Analysis
20488 @section Static Stack Usage Analysis
20489 @cindex Static Stack Usage Analysis
20490 @cindex -fstack-usage
20491
20492 @noindent
20493 A unit compiled with @option{-fstack-usage} will generate an extra file
20494 that specifies
20495 the maximum amount of stack used, on a per-function basis.
20496 The file has the same
20497 basename as the target object file with a @file{.su} extension.
20498 Each line of this file is made up of three fields:
20499
20500 @itemize
20501 @item
20502 The name of the function.
20503 @item
20504 A number of bytes.
20505 @item
20506 One or more qualifiers: @code{static}, @code{dynamic}, @code{bounded}.
20507 @end itemize
20508
20509 The second field corresponds to the size of the known part of the function
20510 frame.
20511
20512 The qualifier @code{static} means that the function frame size
20513 is purely static.
20514 It usually means that all local variables have a static size.
20515 In this case, the second field is a reliable measure of the function stack
20516 utilization.
20517
20518 The qualifier @code{dynamic} means that the function frame size is not static.
20519 It happens mainly when some local variables have a dynamic size. When this
20520 qualifier appears alone, the second field is not a reliable measure
20521 of the function stack analysis. When it is qualified with @code{bounded}, it
20522 means that the second field is a reliable maximum of the function stack
20523 utilization.
20524
20525 @node Dynamic Stack Usage Analysis
20526 @section Dynamic Stack Usage Analysis
20527
20528 @noindent
20529 It is possible to measure the maximum amount of stack used by a task, by
20530 adding a switch to @command{gnatbind}, as:
20531
20532 @smallexample
20533 $ gnatbind -u0 file
20534 @end smallexample
20535
20536 @noindent
20537 With this option, at each task termination, its stack usage is output on
20538 @file{stderr}.
20539 It is not always convenient to output the stack usage when the program
20540 is still running. Hence, it is possible to delay this output until program
20541 termination. for a given number of tasks specified as the argument of the
20542 @option{-u} option. For instance:
20543
20544 @smallexample
20545 $ gnatbind -u100 file
20546 @end smallexample
20547
20548 @noindent
20549 will buffer the stack usage information of the first 100 tasks to terminate and
20550 output this info at program termination. Results are displayed in four
20551 columns:
20552
20553 @noindent
20554 Index | Task Name | Stack Size | Stack Usage [Value +/- Variation]
20555
20556 @noindent
20557 where:
20558
20559 @table @emph
20560 @item Index
20561 is a number associated with each task.
20562
20563 @item Task Name
20564 is the name of the task analyzed.
20565
20566 @item Stack Size
20567 is the maximum size for the stack.
20568
20569 @item Stack Usage
20570 is the measure done by the stack analyzer. In order to prevent overflow, the stack
20571 is not entirely analyzed, and it's not possible to know exactly how
20572 much has actually been used. The report thus contains the theoretical stack usage
20573 (Value) and the possible variation (Variation) around this value.
20574
20575 @end table
20576
20577 @noindent
20578 The environment task stack, e.g., the stack that contains the main unit, is
20579 only processed when the environment variable GNAT_STACK_LIMIT is set.
20580
20581
20582 @c *********************************
20583 @c * GNATCHECK *
20584 @c *********************************
20585 @node Verifying Properties Using gnatcheck
20586 @chapter Verifying Properties Using @command{gnatcheck}
20587 @findex gnatcheck
20588 @cindex @command{gnatcheck}
20589
20590 @noindent
20591 The @command{gnatcheck} tool is an ASIS-based utility that checks properties
20592 of Ada source files according to a given set of semantic rules.
20593 @cindex ASIS
20594
20595 In order to check compliance with a given rule, @command{gnatcheck} has to
20596 semantically analyze the Ada sources.
20597 Therefore, checks can only be performed on
20598 legal Ada units. Moreover, when a unit depends semantically upon units located
20599 outside the current directory, the source search path has to be provided when
20600 calling @command{gnatcheck}, either through a specified project file or
20601 through @command{gnatcheck} switches as described below.
20602
20603 A number of rules are predefined in @command{gnatcheck} and are described
20604 later in this chapter.
20605 You can also add new rules, by modifying the @command{gnatcheck} code and
20606 rebuilding the tool. In order to add a simple rule making some local checks,
20607 a small amount of straightforward ASIS-based programming is usually needed.
20608
20609 Project support for @command{gnatcheck} is provided by the GNAT
20610 driver (see @ref{The GNAT Driver and Project Files}).
20611
20612 Invoking @command{gnatcheck} on the command line has the form:
20613
20614 @smallexample
20615 $ gnatcheck @ovar{switches} @{@var{filename}@}
20616 @r{[}^-files^/FILES^=@{@var{arg_list_filename}@}@r{]}
20617 @r{[}-cargs @var{gcc_switches}@r{]} @r{[}-rules @var{rule_options}@r{]}
20618 @end smallexample
20619
20620 @noindent
20621 where
20622 @itemize @bullet
20623 @item
20624 @var{switches} specify the general tool options
20625
20626 @item
20627 Each @var{filename} is the name (including the extension) of a source
20628 file to process. ``Wildcards'' are allowed, and
20629 the file name may contain path information.
20630
20631 @item
20632 Each @var{arg_list_filename} is the name (including the extension) of a text
20633 file containing the names of the source files to process, separated by spaces
20634 or line breaks.
20635
20636 @item
20637 @var{gcc_switches} is a list of switches for
20638 @command{gcc}. They will be passed on to all compiler invocations made by
20639 @command{gnatcheck} to generate the ASIS trees. Here you can provide
20640 @option{^-I^/INCLUDE_DIRS=^} switches to form the source search path,
20641 and use the @option{-gnatec} switch to set the configuration file.
20642
20643 @item
20644 @var{rule_options} is a list of options for controlling a set of
20645 rules to be checked by @command{gnatcheck} (@pxref{gnatcheck Rule Options}).
20646 @end itemize
20647
20648 @noindent
20649 Either a @file{@var{filename}} or an @file{@var{arg_list_filename}} must be supplied.
20650
20651 @menu
20652 * Format of the Report File::
20653 * General gnatcheck Switches::
20654 * gnatcheck Rule Options::
20655 * Adding the Results of Compiler Checks to gnatcheck Output::
20656 * Project-Wide Checks::
20657 * Predefined Rules::
20658 @end menu
20659
20660 @node Format of the Report File
20661 @section Format of the Report File
20662 @cindex Report file (for @code{gnatcheck})
20663
20664 @noindent
20665 The @command{gnatcheck} tool outputs on @file{stdout} all messages concerning
20666 rule violations.
20667 It also creates a text file that
20668 contains the complete report of the last gnatcheck run. By default this file is
20669 named named @file{^gnatcheck.out^GNATCHECK.OUT^} and it is located in the current
20670 directory, @option{^-o^/OUTPUT^} option can be used to change the name and/or
20671 location of the report file. This report contains:
20672 @itemize @bullet
20673 @item a list of the Ada source files being checked,
20674 @item a list of enabled and disabled rules,
20675 @item a list of the diagnostic messages, ordered in three different ways
20676 and collected in three separate
20677 sections. Section 1 contains the raw list of diagnostic messages. It
20678 corresponds to the output going to @file{stdout}. Section 2 contains
20679 messages ordered by rules.
20680 Section 3 contains messages ordered by source files.
20681 @end itemize
20682
20683 @node General gnatcheck Switches
20684 @section General @command{gnatcheck} Switches
20685
20686 @noindent
20687 The following switches control the general @command{gnatcheck} behavior
20688
20689 @table @option
20690 @c !sort!
20691 @cindex @option{^-a^/ALL^} (@command{gnatcheck})
20692 @item ^-a^/ALL^
20693 Process all units including those with read-only ALI files such as
20694 those from GNAT Run-Time library.
20695
20696 @ifclear vms
20697 @ignore
20698 @cindex @option{-d} (@command{gnatcheck})
20699 @item -d
20700 Debug mode
20701 @end ignore
20702
20703 @cindex @option{-dd} (@command{gnatcheck})
20704 @item -dd
20705 Progress indicator mode (for use in GPS)
20706 @end ifclear
20707
20708 @cindex @option{^-h^/HELP^} (@command{gnatcheck})
20709 @item ^-h^/HELP^
20710 List the predefined and user-defined rules. For more details see
20711 @ref{Predefined Rules}.
20712
20713 @cindex @option{^-l^/LOCS^} (@command{gnatcheck})
20714 @item ^-l^/LOCS^
20715 Use full source locations references in the report file. For a construct from
20716 a generic instantiation a full source location is a chain from the location
20717 of this construct in the generic unit to the place where this unit is
20718 instantiated.
20719
20720 @cindex @option{^-log^/LOG^} (@command{gnatcheck})
20721 @item ^-log^/LOG^
20722 Duplicate all the output sent to Stderr into a log file. The log file is
20723 named @var{gnatcheck.log} and is located in the current directory.
20724
20725 @cindex @option{^-m^/DIAGNOSTIC_LIMIT^} (@command{gnatcheck})
20726 @item ^-m@i{nnn}^/DIAGNOSTIC_LIMIT=@i{nnn}^
20727 Maximum number of diagnoses to be sent to Stdout, @i{nnn} from o@dots{}1000,
20728 the default value is 500. Zero means that there is no limitation on
20729 the number of diagnostic messages to be printed into Stdout.
20730
20731 @cindex @option{^-q^/QUIET^} (@command{gnatcheck})
20732 @item ^-q^/QUIET^
20733 Quiet mode. All the diagnoses about rule violations are placed in the
20734 @command{gnatcheck} report file only, without duplicating in @file{stdout}.
20735
20736 @cindex @option{^-s^/SHORT^} (@command{gnatcheck})
20737 @item ^-s^/SHORT^
20738 Short format of the report file (no version information, no list of applied
20739 rules, no list of checked sources is included)
20740
20741 @cindex @option{^-s1^/COMPILER_STYLE^} (@command{gnatcheck})
20742 @item ^-s1^/COMPILER_STYLE^
20743 Include the compiler-style section in the report file
20744
20745 @cindex @option{^-s2^/BY_RULES^} (@command{gnatcheck})
20746 @item ^-s2^/BY_RULES^
20747 Include the section containing diagnoses ordered by rules in the report file
20748
20749 @cindex @option{^-s3^/BY_FILES_BY_RULES^} (@command{gnatcheck})
20750 @item ^-s3^/BY_FILES_BY_RULES^
20751 Include the section containing diagnoses ordered by files and then by rules
20752 in the report file
20753
20754 @cindex @option{^-t^/TIME^} (@command{gnatcheck})
20755 @item ^-t^/TIME^
20756 Print out execution time.
20757
20758 @cindex @option{^-v^/VERBOSE^} (@command{gnatcheck})
20759 @item ^-v^/VERBOSE^
20760 Verbose mode; @command{gnatcheck} generates version information and then
20761 a trace of sources being processed.
20762
20763 @cindex @option{^-o ^/OUTPUT^} (@command{gnatcheck})
20764 @item ^-o ^/OUTPUT=^@var{report_file}
20765 Set name of report file file to @var{report_file} .
20766
20767 @end table
20768
20769 @noindent
20770 Note that if any of the options @option{^-s1^/COMPILER_STYLE^},
20771 @option{^-s2^/BY_RULES^} or
20772 @option{^-s3^/BY_FILES_BY_RULES^} is specified,
20773 then the @command{gnatcheck} report file will only contain sections
20774 explicitly denoted by these options.
20775
20776 @node gnatcheck Rule Options
20777 @section @command{gnatcheck} Rule Options
20778
20779 @noindent
20780 The following options control the processing performed by
20781 @command{gnatcheck}.
20782
20783 @table @option
20784 @cindex @option{+ALL} (@command{gnatcheck})
20785 @item +ALL
20786 Turn all the rule checks ON.
20787
20788 @cindex @option{-ALL} (@command{gnatcheck})
20789 @item -ALL
20790 Turn all the rule checks OFF.
20791
20792 @cindex @option{+R} (@command{gnatcheck})
20793 @item +R@var{rule_id}@r{[}:@var{param}@r{]}
20794 Turn on the check for a specified rule with the specified parameter, if any.
20795 @var{rule_id} must be the identifier of one of the currently implemented rules
20796 (use @option{^-h^/HELP^} for the list of implemented rules). Rule identifiers
20797 are not case-sensitive. The @var{param} item must
20798 be a string representing a valid parameter(s) for the specified rule.
20799 If it contains any space characters then this string must be enclosed in
20800 quotation marks.
20801
20802 @cindex @option{-R} (@command{gnatcheck})
20803 @item -R@var{rule_id}@r{[}:@var{param}@r{]}
20804 Turn off the check for a specified rule with the specified parameter, if any.
20805
20806 @cindex @option{-from} (@command{gnatcheck})
20807 @item -from=@var{rule_option_filename}
20808 Read the rule options from the text file @var{rule_option_filename}, referred as
20809 ``rule file'' below.
20810
20811 @end table
20812
20813 @noindent
20814 The default behavior is that all the rule checks are disabled.
20815
20816 A rule file is a text file containing a set of rule options.
20817 @cindex Rule file (for @code{gnatcheck})
20818 The file may contain empty lines and Ada-style comments (comment
20819 lines and end-of-line comments). The rule file has free format; that is,
20820 you do not have to start a new rule option on a new line.
20821
20822 A rule file may contain other @option{-from=@var{rule_option_filename}}
20823 options, each such option being replaced with the content of the
20824 corresponding rule file during the rule files processing. In case a
20825 cycle is detected (that is, @file{@var{rule_file_1}} reads rule options
20826 from @file{@var{rule_file_2}}, and @file{@var{rule_file_2}} reads
20827 (directly or indirectly) rule options from @file{@var{rule_file_1}}),
20828 the processing of rule files is interrupted and a part of their content
20829 is ignored.
20830
20831
20832 @node Adding the Results of Compiler Checks to gnatcheck Output
20833 @section Adding the Results of Compiler Checks to @command{gnatcheck} Output
20834
20835 @noindent
20836 The @command{gnatcheck} tool can include in the generated diagnostic messages
20837 and in
20838 the report file the results of the checks performed by the compiler. Though
20839 disabled by default, this effect may be obtained by using @option{+R} with
20840 the following rule identifiers and parameters:
20841
20842 @table @option
20843 @item Restrictions
20844 To record restrictions violations (that are performed by the compiler if the
20845 pragma @code{Restrictions} or @code{Restriction_Warnings} are given),
20846 use the rule named
20847 @code{Restrictions} with the same parameters as pragma
20848 @code{Restrictions} or @code{Restriction_Warnings}.
20849
20850 @item Style_Checks
20851 To record compiler style checks(@pxref{Style Checking}), use the rule named
20852 @code{Style_Checks}. A parameter of this rule can be either @code{All_Checks},
20853 which enables all the standard style checks that corresponds to @option{-gnatyy}
20854 GNAT style check option, or a string that has exactly the same
20855 structure and semantics as the @code{string_LITERAL} parameter of GNAT pragma
20856 @code{Style_Checks} (for further information about this pragma,
20857 @pxref{Pragma Style_Checks,,, gnat_rm, GNAT Reference Manual}). For example,
20858 @code{+RStyle_Checks:O} rule option activates and adds to @command{gnatcheck}
20859 output the compiler style check that corresponds to
20860 @code{-gnatyO} style check option.
20861
20862 @item Warnings
20863 To record compiler warnings (@pxref{Warning Message Control}), use the rule
20864 named @code{Warnings} with a parameter that is a valid
20865 @i{static_string_expression} argument of GNAT pragma @code{Warnings}
20866 (for further information about this pragma, @pxref{Pragma Warnings,,,
20867 gnat_rm, GNAT Reference Manual}). Note, that in case of gnatcheck
20868 's' parameter, that corresponds to the GNAT @option{-gnatws} option, disables
20869 all the specific warnings, but not suppresses the warning mode,
20870 and 'e' parameter, corresponding to @option{-gnatwe} that means
20871 "treat warnings as errors", does not have any effect.
20872
20873 @end table
20874
20875 To disable a specific restriction check, use @code{-RStyle_Checks} gnatcheck
20876 option with the corresponding restriction name as a parameter. @code{-R} is
20877 not available for @code{Style_Checks} and @code{Warnings} options, to disable
20878 warnings and style checks, use the corresponding warning and style options.
20879
20880 @node Project-Wide Checks
20881 @section Project-Wide Checks
20882 @cindex Project-wide checks (for @command{gnatcheck})
20883
20884 @noindent
20885 In order to perform checks on all units of a given project, you can use
20886 the GNAT driver along with the @option{-P} option:
20887 @smallexample
20888 gnat check -Pproj -rules -from=my_rules
20889 @end smallexample
20890
20891 @noindent
20892 If the project @code{proj} depends upon other projects, you can perform
20893 checks on the project closure using the @option{-U} option:
20894 @smallexample
20895 gnat check -Pproj -U -rules -from=my_rules
20896 @end smallexample
20897
20898 @noindent
20899 Finally, if not all the units are relevant to a particular main
20900 program in the project closure, you can perform checks for the set
20901 of units needed to create a given main program (unit closure) using
20902 the @option{-U} option followed by the name of the main unit:
20903 @smallexample
20904 gnat check -Pproj -U main -rules -from=my_rules
20905 @end smallexample
20906
20907
20908 @node Predefined Rules
20909 @section Predefined Rules
20910 @cindex Predefined rules (for @command{gnatcheck})
20911
20912 @ignore
20913 @c (Jan 2007) Since the global rules are still under development and are not
20914 @c documented, there is no point in explaining the difference between
20915 @c global and local rules
20916 @noindent
20917 A rule in @command{gnatcheck} is either local or global.
20918 A @emph{local rule} is a rule that applies to a well-defined section
20919 of a program and that can be checked by analyzing only this section.
20920 A @emph{global rule} requires analysis of some global properties of the
20921 whole program (mostly related to the program call graph).
20922 As of @value{NOW}, the implementation of global rules should be
20923 considered to be at a preliminary stage. You can use the
20924 @option{+GLOBAL} option to enable all the global rules, and the
20925 @option{-GLOBAL} rule option to disable all the global rules.
20926
20927 All the global rules in the list below are
20928 so indicated by marking them ``GLOBAL''.
20929 This +GLOBAL and -GLOBAL options are not
20930 included in the list of gnatcheck options above, because at the moment they
20931 are considered as a temporary debug options.
20932
20933 @command{gnatcheck} performs rule checks for generic
20934 instances only for global rules. This limitation may be relaxed in a later
20935 release.
20936 @end ignore
20937
20938 @noindent
20939 The following subsections document the rules implemented in
20940 @command{gnatcheck}.
20941 The subsection title is the same as the rule identifier, which may be
20942 used as a parameter of the @option{+R} or @option{-R} options.
20943
20944
20945 @menu
20946 * Abstract_Type_Declarations::
20947 * Anonymous_Arrays::
20948 * Anonymous_Subtypes::
20949 * Blocks::
20950 * Boolean_Relational_Operators::
20951 @ignore
20952 * Ceiling_Violations::
20953 @end ignore
20954 * Controlled_Type_Declarations::
20955 * Declarations_In_Blocks::
20956 * Default_Parameters::
20957 * Discriminated_Records::
20958 * Enumeration_Ranges_In_CASE_Statements::
20959 * Exceptions_As_Control_Flow::
20960 * Exits_From_Conditional_Loops::
20961 * EXIT_Statements_With_No_Loop_Name::
20962 * Expanded_Loop_Exit_Names::
20963 * Explicit_Full_Discrete_Ranges::
20964 * Float_Equality_Checks::
20965 * Forbidden_Pragmas::
20966 * Function_Style_Procedures::
20967 * Generics_In_Subprograms::
20968 * GOTO_Statements::
20969 * Implicit_IN_Mode_Parameters::
20970 * Implicit_SMALL_For_Fixed_Point_Types::
20971 * Improperly_Located_Instantiations::
20972 * Improper_Returns::
20973 * Library_Level_Subprograms::
20974 * Local_Packages::
20975 @ignore
20976 * Improperly_Called_Protected_Entries::
20977 @end ignore
20978 * Metrics::
20979 * Misnamed_Identifiers::
20980 * Multiple_Entries_In_Protected_Definitions::
20981 * Name_Clashes::
20982 * Non_Qualified_Aggregates::
20983 * Non_Short_Circuit_Operators::
20984 * Non_SPARK_Attributes::
20985 * Non_Tagged_Derived_Types::
20986 * Non_Visible_Exceptions::
20987 * Numeric_Literals::
20988 * OTHERS_In_Aggregates::
20989 * OTHERS_In_CASE_Statements::
20990 * OTHERS_In_Exception_Handlers::
20991 * Outer_Loop_Exits::
20992 * Overloaded_Operators::
20993 * Overly_Nested_Control_Structures::
20994 * Parameters_Out_Of_Order::
20995 * Positional_Actuals_For_Defaulted_Generic_Parameters::
20996 * Positional_Actuals_For_Defaulted_Parameters::
20997 * Positional_Components::
20998 * Positional_Generic_Parameters::
20999 * Positional_Parameters::
21000 * Predefined_Numeric_Types::
21001 * Raising_External_Exceptions::
21002 * Raising_Predefined_Exceptions::
21003 * Separate_Numeric_Error_Handlers::
21004 @ignore
21005 * Recursion::
21006 * Side_Effect_Functions::
21007 @end ignore
21008 * Slices::
21009 * Unassigned_OUT_Parameters::
21010 * Uncommented_BEGIN_In_Package_Bodies::
21011 * Unconditional_Exits::
21012 * Unconstrained_Array_Returns::
21013 * Universal_Ranges::
21014 * Unnamed_Blocks_And_Loops::
21015 @ignore
21016 * Unused_Subprograms::
21017 @end ignore
21018 * USE_PACKAGE_Clauses::
21019 * Volatile_Objects_Without_Address_Clauses::
21020 @end menu
21021
21022
21023 @node Abstract_Type_Declarations
21024 @subsection @code{Abstract_Type_Declarations}
21025 @cindex @code{Abstract_Type_Declarations} rule (for @command{gnatcheck})
21026
21027 @noindent
21028 Flag all declarations of abstract types. For an abstract private
21029 type, both the private and full type declarations are flagged.
21030
21031 This rule has no parameters.
21032
21033
21034 @node Anonymous_Arrays
21035 @subsection @code{Anonymous_Arrays}
21036 @cindex @code{Anonymous_Arrays} rule (for @command{gnatcheck})
21037
21038 @noindent
21039 Flag all anonymous array type definitions (by Ada semantics these can only
21040 occur in object declarations).
21041
21042 This rule has no parameters.
21043
21044 @node Anonymous_Subtypes
21045 @subsection @code{Anonymous_Subtypes}
21046 @cindex @code{Anonymous_Subtypes} rule (for @command{gnatcheck})
21047
21048 @noindent
21049 Flag all uses of anonymous subtypes. A use of an anonymous subtype is
21050 any instance of a subtype indication with a constraint, other than one
21051 that occurs immediately within a subtype declaration. Any use of a range
21052 other than as a constraint used immediately within a subtype declaration
21053 is considered as an anonymous subtype.
21054
21055 An effect of this rule is that @code{for} loops such as the following are
21056 flagged (since @code{1..N} is formally a ``range''):
21057
21058 @smallexample @c ada
21059 for I in 1 .. N loop
21060 @dots{}
21061 end loop;
21062 @end smallexample
21063
21064 @noindent
21065 Declaring an explicit subtype solves the problem:
21066
21067 @smallexample @c ada
21068 subtype S is Integer range 1..N;
21069 @dots{}
21070 for I in S loop
21071 @dots{}
21072 end loop;
21073 @end smallexample
21074
21075 @noindent
21076 This rule has no parameters.
21077
21078 @node Blocks
21079 @subsection @code{Blocks}
21080 @cindex @code{Blocks} rule (for @command{gnatcheck})
21081
21082 @noindent
21083 Flag each block statement.
21084
21085 This rule has no parameters.
21086
21087 @node Boolean_Relational_Operators
21088 @subsection @code{Boolean_Relational_Operators}
21089 @cindex @code{Boolean_Relational_Operators} rule (for @command{gnatcheck})
21090
21091 @noindent
21092 Flag each call to a predefined relational operator (``<'', ``>'', ``<='',
21093 ``>='', ``='' and ``/='') for the predefined Boolean type.
21094 (This rule is useful in enforcing the SPARK language restrictions.)
21095
21096 Calls to predefined relational operators of any type derived from
21097 @code{Standard.Boolean} are not detected. Calls to user-defined functions
21098 with these designators, and uses of operators that are renamings
21099 of the predefined relational operators for @code{Standard.Boolean},
21100 are likewise not detected.
21101
21102 This rule has no parameters.
21103
21104 @ignore
21105 @node Ceiling_Violations
21106 @subsection @code{Ceiling_Violations} (under construction, GLOBAL)
21107 @cindex @code{Ceiling_Violations} rule (for @command{gnatcheck})
21108
21109 @noindent
21110 Flag invocations of a protected operation by a task whose priority exceeds
21111 the protected object's ceiling.
21112
21113 As of @value{NOW}, this rule has the following limitations:
21114
21115 @itemize @bullet
21116
21117 @item
21118 We consider only pragmas Priority and Interrupt_Priority as means to define
21119 a task/protected operation priority. We do not consider the effect of using
21120 Ada.Dynamic_Priorities.Set_Priority procedure;
21121
21122 @item
21123 We consider only base task priorities, and no priority inheritance. That is,
21124 we do not make a difference between calls issued during task activation and
21125 execution of the sequence of statements from task body;
21126
21127 @item
21128 Any situation when the priority of protected operation caller is set by a
21129 dynamic expression (that is, the corresponding Priority or
21130 Interrupt_Priority pragma has a non-static expression as an argument) we
21131 treat as a priority inconsistency (and, therefore, detect this situation).
21132 @end itemize
21133
21134 @noindent
21135 At the moment the notion of the main subprogram is not implemented in
21136 gnatcheck, so any pragma Priority in a library level subprogram body (in case
21137 if this subprogram can be a main subprogram of a partition) changes the
21138 priority of an environment task. So if we have more then one such pragma in
21139 the set of processed sources, the pragma that is processed last, defines the
21140 priority of an environment task.
21141
21142 This rule has no parameters.
21143 @end ignore
21144
21145 @node Controlled_Type_Declarations
21146 @subsection @code{Controlled_Type_Declarations}
21147 @cindex @code{Controlled_Type_Declarations} rule (for @command{gnatcheck})
21148
21149 @noindent
21150 Flag all declarations of controlled types. A declaration of a private type
21151 is flagged if its full declaration declares a controlled type. A declaration
21152 of a derived type is flagged if its ancestor type is controlled. Subtype
21153 declarations are not checked. A declaration of a type that itself is not a
21154 descendant of a type declared in @code{Ada.Finalization} but has a controlled
21155 component is not checked.
21156
21157 This rule has no parameters.
21158
21159
21160
21161 @node Declarations_In_Blocks
21162 @subsection @code{Declarations_In_Blocks}
21163 @cindex @code{Declarations_In_Blocks} rule (for @command{gnatcheck})
21164
21165 @noindent
21166 Flag all block statements containing local declarations. A @code{declare}
21167 block with an empty @i{declarative_part} or with a @i{declarative part}
21168 containing only pragmas and/or @code{use} clauses is not flagged.
21169
21170 This rule has no parameters.
21171
21172
21173 @node Default_Parameters
21174 @subsection @code{Default_Parameters}
21175 @cindex @code{Default_Parameters} rule (for @command{gnatcheck})
21176
21177 @noindent
21178 Flag all default expressions for subprogram parameters. Parameter
21179 declarations of formal and generic subprograms are also checked.
21180
21181 This rule has no parameters.
21182
21183
21184 @node Discriminated_Records
21185 @subsection @code{Discriminated_Records}
21186 @cindex @code{Discriminated_Records} rule (for @command{gnatcheck})
21187
21188 @noindent
21189 Flag all declarations of record types with discriminants. Only the
21190 declarations of record and record extension types are checked. Incomplete,
21191 formal, private, derived and private extension type declarations are not
21192 checked. Task and protected type declarations also are not checked.
21193
21194 This rule has no parameters.
21195
21196
21197 @node Enumeration_Ranges_In_CASE_Statements
21198 @subsection @code{Enumeration_Ranges_In_CASE_Statements}
21199 @cindex @code{Enumeration_Ranges_In_CASE_Statements} (for @command{gnatcheck})
21200
21201 @noindent
21202 Flag each use of a range of enumeration literals as a choice in a
21203 @code{case} statement.
21204 All forms for specifying a range (explicit ranges
21205 such as @code{A .. B}, subtype marks and @code{'Range} attributes) are flagged.
21206 An enumeration range is
21207 flagged even if contains exactly one enumeration value or no values at all. A
21208 type derived from an enumeration type is considered as an enumeration type.
21209
21210 This rule helps prevent maintenance problems arising from adding an
21211 enumeration value to a type and having it implicitly handled by an existing
21212 @code{case} statement with an enumeration range that includes the new literal.
21213
21214 This rule has no parameters.
21215
21216
21217 @node Exceptions_As_Control_Flow
21218 @subsection @code{Exceptions_As_Control_Flow}
21219 @cindex @code{Exceptions_As_Control_Flow} (for @command{gnatcheck})
21220
21221 @noindent
21222 Flag each place where an exception is explicitly raised and handled in the
21223 same subprogram body. A @code{raise} statement in an exception handler,
21224 package body, task body or entry body is not flagged.
21225
21226 The rule has no parameters.
21227
21228 @node Exits_From_Conditional_Loops
21229 @subsection @code{Exits_From_Conditional_Loops}
21230 @cindex @code{Exits_From_Conditional_Loops} (for @command{gnatcheck})
21231
21232 @noindent
21233 Flag any exit statement if it transfers the control out of a @code{for} loop
21234 or a @code{while} loop. This includes cases when the @code{exit} statement
21235 applies to a @code{FOR} or @code{while} loop, and cases when it is enclosed
21236 in some @code{for} or @code{while} loop, but transfers the control from some
21237 outer (inconditional) @code{loop} statement.
21238
21239 The rule has no parameters.
21240
21241
21242 @node EXIT_Statements_With_No_Loop_Name
21243 @subsection @code{EXIT_Statements_With_No_Loop_Name}
21244 @cindex @code{EXIT_Statements_With_No_Loop_Name} (for @command{gnatcheck})
21245
21246 @noindent
21247 Flag each @code{exit} statement that does not specify the name of the loop
21248 being exited.
21249
21250 The rule has no parameters.
21251
21252
21253 @node Expanded_Loop_Exit_Names
21254 @subsection @code{Expanded_Loop_Exit_Names}
21255 @cindex @code{Expanded_Loop_Exit_Names} rule (for @command{gnatcheck})
21256
21257 @noindent
21258 Flag all expanded loop names in @code{exit} statements.
21259
21260 This rule has no parameters.
21261
21262 @node Explicit_Full_Discrete_Ranges
21263 @subsection @code{Explicit_Full_Discrete_Ranges}
21264 @cindex @code{Explicit_Full_Discrete_Ranges} rule (for @command{gnatcheck})
21265
21266 @noindent
21267 Flag each discrete range that has the form @code{A'First .. A'Last}.
21268
21269 This rule has no parameters.
21270
21271 @node Float_Equality_Checks
21272 @subsection @code{Float_Equality_Checks}
21273 @cindex @code{Float_Equality_Checks} rule (for @command{gnatcheck})
21274
21275 @noindent
21276 Flag all calls to the predefined equality operations for floating-point types.
21277 Both ``@code{=}'' and ``@code{/=}'' operations are checked.
21278 User-defined equality operations are not flagged, nor are ``@code{=}''
21279 and ``@code{/=}'' operations for fixed-point types.
21280
21281 This rule has no parameters.
21282
21283
21284 @node Forbidden_Pragmas
21285 @subsection @code{Forbidden_Pragmas}
21286 @cindex @code{Forbidden_Pragmas} rule (for @command{gnatcheck})
21287
21288 @noindent
21289 Flag each use of the specified pragmas. The pragmas to be detected
21290 are named in the rule's parameters.
21291
21292 This rule has the following parameters:
21293
21294 @itemize @bullet
21295 @item For the @option{+R} option
21296
21297 @table @asis
21298 @item @emph{Pragma_Name}
21299 Adds the specified pragma to the set of pragmas to be
21300 checked and sets the checks for all the specified pragmas
21301 ON. @emph{Pragma_Name} is treated as a name of a pragma. If it
21302 does not correspond to any pragma name defined in the Ada
21303 standard or to the name of a GNAT-specific pragma defined
21304 in @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference
21305 Manual}, it is treated as the name of unknown pragma.
21306
21307 @item @code{GNAT}
21308 All the GNAT-specific pragmas are detected; this sets
21309 the checks for all the specified pragmas ON.
21310
21311 @item @code{ALL}
21312 All pragmas are detected; this sets the rule ON.
21313 @end table
21314
21315 @item For the @option{-R} option
21316 @table @asis
21317 @item @emph{Pragma_Name}
21318 Removes the specified pragma from the set of pragmas to be
21319 checked without affecting checks for
21320 other pragmas. @emph{Pragma_Name} is treated as a name
21321 of a pragma. If it does not correspond to any pragma
21322 defined in the Ada standard or to any name defined in
21323 @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference Manual},
21324 this option is treated as turning OFF detection of all unknown pragmas.
21325
21326 @item GNAT
21327 Turn OFF detection of all GNAT-specific pragmas
21328
21329 @item ALL
21330 Clear the list of the pragmas to be detected and
21331 turn the rule OFF.
21332 @end table
21333 @end itemize
21334
21335 @noindent
21336 Parameters are not case sensitive. If @emph{Pragma_Name} does not have
21337 the syntax of an Ada identifier and therefore can not be considered
21338 as a pragma name, a diagnostic message is generated and the corresponding
21339 parameter is ignored.
21340
21341 When more then one parameter is given in the same rule option, the parameters
21342 must be separated by a comma.
21343
21344 If more then one option for this rule is specified for the @command{gnatcheck}
21345 call, a new option overrides the previous one(s).
21346
21347 The @option{+R} option with no parameters turns the rule ON with the set of
21348 pragmas to be detected defined by the previous rule options.
21349 (By default this set is empty, so if the only option specified for the rule is
21350 @option{+RForbidden_Pragmas} (with
21351 no parameter), then the rule is enabled, but it does not detect anything).
21352 The @option{-R} option with no parameter turns the rule OFF, but it does not
21353 affect the set of pragmas to be detected.
21354
21355
21356
21357
21358 @node Function_Style_Procedures
21359 @subsection @code{Function_Style_Procedures}
21360 @cindex @code{Function_Style_Procedures} rule (for @command{gnatcheck})
21361
21362 @noindent
21363 Flag each procedure that can be rewritten as a function. A procedure can be
21364 converted into a function if it has exactly one parameter of mode @code{out}
21365 and no parameters of mode @code{in out}. Procedure declarations,
21366 formal procedure declarations, and generic procedure declarations are always
21367 checked. Procedure
21368 bodies and body stubs are flagged only if they do not have corresponding
21369 separate declarations. Procedure renamings and procedure instantiations are
21370 not flagged.
21371
21372 If a procedure can be rewritten as a function, but its @code{out} parameter is
21373 of a limited type, it is not flagged.
21374
21375 Protected procedures are not flagged. Null procedures also are not flagged.
21376
21377 This rule has no parameters.
21378
21379
21380 @node Generics_In_Subprograms
21381 @subsection @code{Generics_In_Subprograms}
21382 @cindex @code{Generics_In_Subprograms} rule (for @command{gnatcheck})
21383
21384 @noindent
21385 Flag each declaration of a generic unit in a subprogram. Generic
21386 declarations in the bodies of generic subprograms are also flagged.
21387 A generic unit nested in another generic unit is not flagged.
21388 If a generic unit is
21389 declared in a local package that is declared in a subprogram body, the
21390 generic unit is flagged.
21391
21392 This rule has no parameters.
21393
21394
21395 @node GOTO_Statements
21396 @subsection @code{GOTO_Statements}
21397 @cindex @code{GOTO_Statements} rule (for @command{gnatcheck})
21398
21399 @noindent
21400 Flag each occurrence of a @code{goto} statement.
21401
21402 This rule has no parameters.
21403
21404
21405 @node Implicit_IN_Mode_Parameters
21406 @subsection @code{Implicit_IN_Mode_Parameters}
21407 @cindex @code{Implicit_IN_Mode_Parameters} rule (for @command{gnatcheck})
21408
21409 @noindent
21410 Flag each occurrence of a formal parameter with an implicit @code{in} mode.
21411 Note that @code{access} parameters, although they technically behave
21412 like @code{in} parameters, are not flagged.
21413
21414 This rule has no parameters.
21415
21416
21417 @node Implicit_SMALL_For_Fixed_Point_Types
21418 @subsection @code{Implicit_SMALL_For_Fixed_Point_Types}
21419 @cindex @code{Implicit_SMALL_For_Fixed_Point_Types} rule (for @command{gnatcheck})
21420
21421 @noindent
21422 Flag each fixed point type declaration that lacks an explicit
21423 representation clause to define its @code{'Small} value.
21424 Since @code{'Small} can be defined only for ordinary fixed point types,
21425 decimal fixed point type declarations are not checked.
21426
21427 This rule has no parameters.
21428
21429
21430 @node Improperly_Located_Instantiations
21431 @subsection @code{Improperly_Located_Instantiations}
21432 @cindex @code{Improperly_Located_Instantiations} rule (for @command{gnatcheck})
21433
21434 @noindent
21435 Flag all generic instantiations in library-level package specs
21436 (including library generic packages) and in all subprogram bodies.
21437
21438 Instantiations in task and entry bodies are not flagged. Instantiations in the
21439 bodies of protected subprograms are flagged.
21440
21441 This rule has no parameters.
21442
21443
21444
21445 @node Improper_Returns
21446 @subsection @code{Improper_Returns}
21447 @cindex @code{Improper_Returns} rule (for @command{gnatcheck})
21448
21449 @noindent
21450 Flag each explicit @code{return} statement in procedures, and
21451 multiple @code{return} statements in functions.
21452 Diagnostic messages are generated for all @code{return} statements
21453 in a procedure (thus each procedure must be written so that it
21454 returns implicitly at the end of its statement part),
21455 and for all @code{return} statements in a function after the first one.
21456 This rule supports the stylistic convention that each subprogram
21457 should have no more than one point of normal return.
21458
21459 This rule has no parameters.
21460
21461
21462 @node Library_Level_Subprograms
21463 @subsection @code{Library_Level_Subprograms}
21464 @cindex @code{Library_Level_Subprograms} rule (for @command{gnatcheck})
21465
21466 @noindent
21467 Flag all library-level subprograms (including generic subprogram instantiations).
21468
21469 This rule has no parameters.
21470
21471
21472 @node Local_Packages
21473 @subsection @code{Local_Packages}
21474 @cindex @code{Local_Packages} rule (for @command{gnatcheck})
21475
21476 @noindent
21477 Flag all local packages declared in package and generic package
21478 specs.
21479 Local packages in bodies are not flagged.
21480
21481 This rule has no parameters.
21482
21483 @ignore
21484 @node Improperly_Called_Protected_Entries
21485 @subsection @code{Improperly_Called_Protected_Entries} (under construction, GLOBAL)
21486 @cindex @code{Improperly_Called_Protected_Entries} rule (for @command{gnatcheck})
21487
21488 @noindent
21489 Flag each protected entry that can be called from more than one task.
21490
21491 This rule has no parameters.
21492 @end ignore
21493
21494 @node Metrics
21495 @subsection @code{Metrics}
21496 @cindex @code{Metrics} rule (for @command{gnatcheck})
21497
21498 @noindent
21499 There is a set of checks based on computing a metric value and comparing the
21500 result with the specified upper (or lower, depending on a specific metric)
21501 value specified for a given metric. A construct is flagged if a given metric
21502 is applicable (can be computed) for it and the computed value is greater
21503 then (lover then) the specified upper (lower) bound.
21504
21505 The name of any metric-based rule consists of the prefix @code{Metrics_}
21506 followed by the name of the corresponding metric (see the table below).
21507 For @option{+R} option, each metric-based rule has a numeric parameter
21508 specifying the bound (integer or real, depending on a metric), @option{-R}
21509 option for metric rules does not have a parameter.
21510
21511 The following table shows the metric names for that the corresponding
21512 metrics-based checks are supported by gnatcheck, including the
21513 constraint that must be satisfied by the bound that is specified for the check
21514 and what bound - upper (U) or lower (L) - should be specified.
21515
21516 @multitable {@code{Cyclomatic_Complexity}}{Cyclomatic complexity}{Positive integer}
21517 @ifnothtml
21518 @headitem Check Name @tab Description @tab Bounds Value
21519 @end ifnothtml
21520 @ifhtml
21521 @item @b{Check Name} @tab @b{Description} @tab @b{Bounds Value}
21522 @end ifhtml
21523 @c Above conditional code is workaround to bug in texi2html (Feb 2008)
21524 @item @code{Essential_Complexity} @tab Essential complexity @tab Positive integer (U)
21525 @item @code{Cyclomatic_Complexity} @tab Cyclomatic complexity @tab Positive integer (U)
21526 @item @code{LSLOC} @tab Logical Source Lines of Code @tab Positive integer (U)
21527 @end multitable
21528
21529 @noindent
21530 The meaning and the computed values for all these metrics are exactly
21531 the same as for the corresponding metrics in @command{gnatmetric}.
21532
21533 @emph{Example:} the rule
21534 @smallexample
21535 +RMetrics_Cyclomatic_Complexity : 7
21536 @end smallexample
21537 @noindent
21538 means that all bodies with cyclomatic complexity exceeding 7 will be flagged.
21539
21540 To turn OFF the check for cyclomatic complexity metric, use the following option:
21541 @smallexample
21542 -RMetrics_Cyclomatic_Complexity
21543 @end smallexample
21544
21545 @node Misnamed_Identifiers
21546 @subsection @code{Misnamed_Identifiers}
21547 @cindex @code{Misnamed_Identifiers} rule (for @command{gnatcheck})
21548
21549 @noindent
21550 Flag the declaration of each identifier that does not have a suffix
21551 corresponding to the kind of entity being declared.
21552 The following declarations are checked:
21553
21554 @itemize @bullet
21555 @item
21556 type declarations
21557
21558 @item
21559 subtype declarations
21560
21561 @item
21562 constant declarations (but not number declarations)
21563
21564 @item
21565 package renaming declarations (but not generic package renaming
21566 declarations)
21567 @end itemize
21568
21569 @noindent
21570 This rule may have parameters. When used without parameters, the rule enforces
21571 the following checks:
21572
21573 @itemize @bullet
21574 @item
21575 type-defining names end with @code{_T}, unless the type is an access type,
21576 in which case the suffix must be @code{_A}
21577 @item
21578 constant names end with @code{_C}
21579 @item
21580 names defining package renamings end with @code{_R}
21581 @end itemize
21582
21583 @noindent
21584 For a private or incomplete type declaration the following checks are
21585 made for the defining name suffix:
21586
21587 @itemize @bullet
21588 @item
21589 For an incomplete type declaration: if the corresponding full type
21590 declaration is available, the defining identifier from the full type
21591 declaration is checked, but the defining identifier from the incomplete type
21592 declaration is not; otherwise the defining identifier from the incomplete
21593 type declaration is checked against the suffix specified for type
21594 declarations.
21595
21596 @item
21597 For a private type declaration (including private extensions), the defining
21598 identifier from the private type declaration is checked against the type
21599 suffix (even if the corresponding full declaration is an access type
21600 declaration), and the defining identifier from the corresponding full type
21601 declaration is not checked.
21602 @end itemize
21603
21604 @noindent
21605 For a deferred constant, the defining name in the corresponding full constant
21606 declaration is not checked.
21607
21608 Defining names of formal types are not checked.
21609
21610 The rule may have the following parameters:
21611
21612 @itemize @bullet
21613 @item
21614 For the @option{+R} option:
21615 @table @code
21616 @item Default
21617 Sets the default listed above for all the names to be checked.
21618
21619 @item Type_Suffix=@emph{string}
21620 Specifies the suffix for a type name.
21621
21622 @item Access_Suffix=@emph{string}
21623 Specifies the suffix for an access type name. If
21624 this parameter is set, it overrides for access
21625 types the suffix set by the @code{Type_Suffix} parameter.
21626 For access types, @emph{string} may have the following format:
21627 @emph{suffix1(suffix2)}. That means that an access type name
21628 should have the @emph{suffix1} suffix except for the case when
21629 the designated type is also an access type, in this case the
21630 type name should have the @emph{suffix1 & suffix2} suffix.
21631
21632 @item Class_Access_Suffix=@emph{string}
21633 Specifies the suffix for the name of an access type that points to some class-wide
21634 type. If this parameter is set, it overrides for such access
21635 types the suffix set by the @code{Type_Suffix} or @code{Access_Suffix}
21636 parameter.
21637
21638 @item Class_Subtype_Suffix=@emph{string}
21639 Specifies the suffix for the name of a subtype that denotes a class-wide type.
21640
21641 @item Constant_Suffix=@emph{string}
21642 Specifies the suffix for a constant name.
21643
21644 @item Renaming_Suffix=@emph{string}
21645 Specifies the suffix for a package renaming name.
21646 @end table
21647
21648 @item
21649 For the @option{-R} option:
21650 @table @code
21651 @item All_Suffixes
21652 Remove all the suffixes specified for the
21653 identifier suffix checks, whether by default or
21654 as specified by other rule parameters. All the
21655 checks for this rule are disabled as a result.
21656
21657 @item Type_Suffix
21658 Removes the suffix specified for types. This
21659 disables checks for types but does not disable
21660 any other checks for this rule (including the
21661 check for access type names if @code{Access_Suffix} is
21662 set).
21663
21664 @item Access_Suffix
21665 Removes the suffix specified for access types.
21666 This disables checks for access type names but
21667 does not disable any other checks for this rule.
21668 If @code{Type_Suffix} is set, access type names are
21669 checked as ordinary type names.
21670
21671 @item Class_Access_Suffix
21672 Removes the suffix specified for access types pointing to class-wide
21673 type. This disables specific checks for names of access types pointing to
21674 class-wide types but does not disable any other checks for this rule.
21675 If @code{Type_Suffix} is set, access type names are
21676 checked as ordinary type names. If @code{Access_Suffix} is set, these
21677 access types are checked as any other access type name.
21678
21679 @item Class_Subtype_Suffix=@emph{string}
21680 Removes the suffix specified for subtype names.
21681 This disables checks for subtype names but
21682 does not disable any other checks for this rule.
21683
21684 @item Constant_Suffix
21685 Removes the suffix specified for constants. This
21686 disables checks for constant names but does not
21687 disable any other checks for this rule.
21688
21689 @item Renaming_Suffix
21690 Removes the suffix specified for package
21691 renamings. This disables checks for package
21692 renamings but does not disable any other checks
21693 for this rule.
21694 @end table
21695 @end itemize
21696
21697 @noindent
21698 If more than one parameter is used, parameters must be separated by commas.
21699
21700 If more than one option is specified for the @command{gnatcheck} invocation,
21701 a new option overrides the previous one(s).
21702
21703 The @option{+RMisnamed_Identifiers} option (with no parameter) enables
21704 checks for all the
21705 name suffixes specified by previous options used for this rule.
21706
21707 The @option{-RMisnamed_Identifiers} option (with no parameter) disables
21708 all the checks but keeps
21709 all the suffixes specified by previous options used for this rule.
21710
21711 The @emph{string} value must be a valid suffix for an Ada identifier (after
21712 trimming all the leading and trailing space characters, if any).
21713 Parameters are not case sensitive, except the @emph{string} part.
21714
21715 If any error is detected in a rule parameter, the parameter is ignored.
21716 In such a case the options that are set for the rule are not
21717 specified.
21718
21719
21720
21721 @node Multiple_Entries_In_Protected_Definitions
21722 @subsection @code{Multiple_Entries_In_Protected_Definitions}
21723 @cindex @code{Multiple_Entries_In_Protected_Definitions} rule (for @command{gnatcheck})
21724
21725 @noindent
21726 Flag each protected definition (i.e., each protected object/type declaration)
21727 that defines more than one entry.
21728 Diagnostic messages are generated for all the entry declarations
21729 except the first one. An entry family is counted as one entry. Entries from
21730 the private part of the protected definition are also checked.
21731
21732 This rule has no parameters.
21733
21734 @node Name_Clashes
21735 @subsection @code{Name_Clashes}
21736 @cindex @code{Name_Clashes} rule (for @command{gnatcheck})
21737
21738 @noindent
21739 Check that certain names are not used as defining identifiers. To activate
21740 this rule, you need to supply a reference to the dictionary file(s) as a rule
21741 parameter(s) (more then one dictionary file can be specified). If no
21742 dictionary file is set, this rule will not cause anything to be flagged.
21743 Only defining occurrences, not references, are checked.
21744 The check is not case-sensitive.
21745
21746 This rule is enabled by default, but without setting any corresponding
21747 dictionary file(s); thus the default effect is to do no checks.
21748
21749 A dictionary file is a plain text file. The maximum line length for this file
21750 is 1024 characters. If the line is longer then this limit, extra characters
21751 are ignored.
21752
21753 Each line can be either an empty line, a comment line, or a line containing
21754 a list of identifiers separated by space or HT characters.
21755 A comment is an Ada-style comment (from @code{--} to end-of-line).
21756 Identifiers must follow the Ada syntax for identifiers.
21757 A line containing one or more identifiers may end with a comment.
21758
21759 @node Non_Qualified_Aggregates
21760 @subsection @code{Non_Qualified_Aggregates}
21761 @cindex @code{Non_Qualified_Aggregates} rule (for @command{gnatcheck})
21762
21763 @noindent
21764 Flag each non-qualified aggregate.
21765 A non-qualified aggregate is an
21766 aggregate that is not the expression of a qualified expression. A
21767 string literal is not considered an aggregate, but an array
21768 aggregate of a string type is considered as a normal aggregate.
21769 Aggregates of anonymous array types are not flagged.
21770
21771 This rule has no parameters.
21772
21773
21774 @node Non_Short_Circuit_Operators
21775 @subsection @code{Non_Short_Circuit_Operators}
21776 @cindex @code{Non_Short_Circuit_Operators} rule (for @command{gnatcheck})
21777
21778 @noindent
21779 Flag all calls to predefined @code{and} and @code{or} operators for
21780 any boolean type. Calls to
21781 user-defined @code{and} and @code{or} and to operators defined by renaming
21782 declarations are not flagged. Calls to predefined @code{and} and @code{or}
21783 operators for modular types or boolean array types are not flagged.
21784
21785 This rule has no parameters.
21786
21787
21788
21789 @node Non_SPARK_Attributes
21790 @subsection @code{Non_SPARK_Attributes}
21791 @cindex @code{Non_SPARK_Attributes} rule (for @command{gnatcheck})
21792
21793 @noindent
21794 The SPARK language defines the following subset of Ada 95 attribute
21795 designators as those that can be used in SPARK programs. The use of
21796 any other attribute is flagged.
21797
21798 @itemize @bullet
21799 @item @code{'Adjacent}
21800 @item @code{'Aft}
21801 @item @code{'Base}
21802 @item @code{'Ceiling}
21803 @item @code{'Component_Size}
21804 @item @code{'Compose}
21805 @item @code{'Copy_Sign}
21806 @item @code{'Delta}
21807 @item @code{'Denorm}
21808 @item @code{'Digits}
21809 @item @code{'Exponent}
21810 @item @code{'First}
21811 @item @code{'Floor}
21812 @item @code{'Fore}
21813 @item @code{'Fraction}
21814 @item @code{'Last}
21815 @item @code{'Leading_Part}
21816 @item @code{'Length}
21817 @item @code{'Machine}
21818 @item @code{'Machine_Emax}
21819 @item @code{'Machine_Emin}
21820 @item @code{'Machine_Mantissa}
21821 @item @code{'Machine_Overflows}
21822 @item @code{'Machine_Radix}
21823 @item @code{'Machine_Rounds}
21824 @item @code{'Max}
21825 @item @code{'Min}
21826 @item @code{'Model}
21827 @item @code{'Model_Emin}
21828 @item @code{'Model_Epsilon}
21829 @item @code{'Model_Mantissa}
21830 @item @code{'Model_Small}
21831 @item @code{'Modulus}
21832 @item @code{'Pos}
21833 @item @code{'Pred}
21834 @item @code{'Range}
21835 @item @code{'Remainder}
21836 @item @code{'Rounding}
21837 @item @code{'Safe_First}
21838 @item @code{'Safe_Last}
21839 @item @code{'Scaling}
21840 @item @code{'Signed_Zeros}
21841 @item @code{'Size}
21842 @item @code{'Small}
21843 @item @code{'Succ}
21844 @item @code{'Truncation}
21845 @item @code{'Unbiased_Rounding}
21846 @item @code{'Val}
21847 @item @code{'Valid}
21848 @end itemize
21849
21850 @noindent
21851 This rule has no parameters.
21852
21853
21854 @node Non_Tagged_Derived_Types
21855 @subsection @code{Non_Tagged_Derived_Types}
21856 @cindex @code{Non_Tagged_Derived_Types} rule (for @command{gnatcheck})
21857
21858 @noindent
21859 Flag all derived type declarations that do not have a record extension part.
21860
21861 This rule has no parameters.
21862
21863
21864
21865 @node Non_Visible_Exceptions
21866 @subsection @code{Non_Visible_Exceptions}
21867 @cindex @code{Non_Visible_Exceptions} rule (for @command{gnatcheck})
21868
21869 @noindent
21870 Flag constructs leading to the possibility of propagating an exception
21871 out of the scope in which the exception is declared.
21872 Two cases are detected:
21873
21874 @itemize @bullet
21875 @item
21876 An exception declaration in a subprogram body, task body or block
21877 statement is flagged if the body or statement does not contain a handler for
21878 that exception or a handler with an @code{others} choice.
21879
21880 @item
21881 A @code{raise} statement in an exception handler of a subprogram body,
21882 task body or block statement is flagged if it (re)raises a locally
21883 declared exception. This may occur under the following circumstances:
21884 @itemize @minus
21885 @item
21886 it explicitly raises a locally declared exception, or
21887 @item
21888 it does not specify an exception name (i.e., it is simply @code{raise;})
21889 and the enclosing handler contains a locally declared exception in its
21890 exception choices.
21891 @end itemize
21892 @end itemize
21893
21894 @noindent
21895 Renamings of local exceptions are not flagged.
21896
21897 This rule has no parameters.
21898
21899
21900 @node Numeric_Literals
21901 @subsection @code{Numeric_Literals}
21902 @cindex @code{Numeric_Literals} rule (for @command{gnatcheck})
21903
21904 @noindent
21905 Flag each use of a numeric literal in an index expression, and in any
21906 circumstance except for the following:
21907
21908 @itemize @bullet
21909 @item
21910 a literal occurring in the initialization expression for a constant
21911 declaration or a named number declaration, or
21912
21913 @item
21914 an integer literal that is less than or equal to a value
21915 specified by the @option{N} rule parameter.
21916 @end itemize
21917
21918 @noindent
21919 This rule may have the following parameters for the @option{+R} option:
21920
21921 @table @asis
21922 @item @emph{N}
21923 @emph{N} is an integer literal used as the maximal value that is not flagged
21924 (i.e., integer literals not exceeding this value are allowed)
21925
21926 @item @code{ALL}
21927 All integer literals are flagged
21928 @end table
21929
21930 @noindent
21931 If no parameters are set, the maximum unflagged value is 1.
21932
21933 The last specified check limit (or the fact that there is no limit at
21934 all) is used when multiple @option{+R} options appear.
21935
21936 The @option{-R} option for this rule has no parameters.
21937 It disables the rule but retains the last specified maximum unflagged value.
21938 If the @option{+R} option subsequently appears, this value is used as the
21939 threshold for the check.
21940
21941
21942 @node OTHERS_In_Aggregates
21943 @subsection @code{OTHERS_In_Aggregates}
21944 @cindex @code{OTHERS_In_Aggregates} rule (for @command{gnatcheck})
21945
21946 @noindent
21947 Flag each use of an @code{others} choice in extension aggregates.
21948 In record and array aggregates, an @code{others} choice is flagged unless
21949 it is used to refer to all components, or to all but one component.
21950
21951 If, in case of a named array aggregate, there are two associations, one
21952 with an @code{others} choice and another with a discrete range, the
21953 @code{others} choice is flagged even if the discrete range specifies
21954 exactly one component; for example, @code{(1..1 => 0, others => 1)}.
21955
21956 This rule has no parameters.
21957
21958 @node OTHERS_In_CASE_Statements
21959 @subsection @code{OTHERS_In_CASE_Statements}
21960 @cindex @code{OTHERS_In_CASE_Statements} rule (for @command{gnatcheck})
21961
21962 @noindent
21963 Flag any use of an @code{others} choice in a @code{case} statement.
21964
21965 This rule has no parameters.
21966
21967 @node OTHERS_In_Exception_Handlers
21968 @subsection @code{OTHERS_In_Exception_Handlers}
21969 @cindex @code{OTHERS_In_Exception_Handlers} rule (for @command{gnatcheck})
21970
21971 @noindent
21972 Flag any use of an @code{others} choice in an exception handler.
21973
21974 This rule has no parameters.
21975
21976
21977 @node Outer_Loop_Exits
21978 @subsection @code{Outer_Loop_Exits}
21979 @cindex @code{Outer_Loop_Exits} rule (for @command{gnatcheck})
21980
21981 @noindent
21982 Flag each @code{exit} statement containing a loop name that is not the name
21983 of the immediately enclosing @code{loop} statement.
21984
21985 This rule has no parameters.
21986
21987
21988 @node Overloaded_Operators
21989 @subsection @code{Overloaded_Operators}
21990 @cindex @code{Overloaded_Operators} rule (for @command{gnatcheck})
21991
21992 @noindent
21993 Flag each function declaration that overloads an operator symbol.
21994 A function body is checked only if the body does not have a
21995 separate spec. Formal functions are also checked. For a
21996 renaming declaration, only renaming-as-declaration is checked
21997
21998 This rule has no parameters.
21999
22000
22001 @node Overly_Nested_Control_Structures
22002 @subsection @code{Overly_Nested_Control_Structures}
22003 @cindex @code{Overly_Nested_Control_Structures} rule (for @command{gnatcheck})
22004
22005 @noindent
22006 Flag each control structure whose nesting level exceeds the value provided
22007 in the rule parameter.
22008
22009 The control structures checked are the following:
22010
22011 @itemize @bullet
22012 @item @code{if} statement
22013 @item @code{case} statement
22014 @item @code{loop} statement
22015 @item Selective accept statement
22016 @item Timed entry call statement
22017 @item Conditional entry call
22018 @item Asynchronous select statement
22019 @end itemize
22020
22021 @noindent
22022 The rule has the following parameter for the @option{+R} option:
22023
22024 @table @emph
22025 @item N
22026 Positive integer specifying the maximal control structure nesting
22027 level that is not flagged
22028 @end table
22029
22030 @noindent
22031 If the parameter for the @option{+R} option is not specified or
22032 if it is not a positive integer, @option{+R} option is ignored.
22033
22034 If more then one option is specified for the gnatcheck call, the later option and
22035 new parameter override the previous one(s).
22036
22037
22038 @node Parameters_Out_Of_Order
22039 @subsection @code{Parameters_Out_Of_Order}
22040 @cindex @code{Parameters_Out_Of_Order} rule (for @command{gnatcheck})
22041
22042 @noindent
22043 Flag each subprogram and entry declaration whose formal parameters are not
22044 ordered according to the following scheme:
22045
22046 @itemize @bullet
22047
22048 @item @code{in} and @code{access} parameters first,
22049 then @code{in out} parameters,
22050 and then @code{out} parameters;
22051
22052 @item for @code{in} mode, parameters with default initialization expressions
22053 occur last
22054 @end itemize
22055
22056 @noindent
22057 Only the first violation of the described order is flagged.
22058
22059 The following constructs are checked:
22060
22061 @itemize @bullet
22062 @item subprogram declarations (including null procedures);
22063 @item generic subprogram declarations;
22064 @item formal subprogram declarations;
22065 @item entry declarations;
22066 @item subprogram bodies and subprogram body stubs that do not
22067 have separate specifications
22068 @end itemize
22069
22070 @noindent
22071 Subprogram renamings are not checked.
22072
22073 This rule has no parameters.
22074
22075
22076 @node Positional_Actuals_For_Defaulted_Generic_Parameters
22077 @subsection @code{Positional_Actuals_For_Defaulted_Generic_Parameters}
22078 @cindex @code{Positional_Actuals_For_Defaulted_Generic_Parameters} rule (for @command{gnatcheck})
22079
22080 @noindent
22081 Flag each generic actual parameter corresponding to a generic formal
22082 parameter with a default initialization, if positional notation is used.
22083
22084 This rule has no parameters.
22085
22086 @node Positional_Actuals_For_Defaulted_Parameters
22087 @subsection @code{Positional_Actuals_For_Defaulted_Parameters}
22088 @cindex @code{Positional_Actuals_For_Defaulted_Parameters} rule (for @command{gnatcheck})
22089
22090 @noindent
22091 Flag each actual parameter to a subprogram or entry call where the
22092 corresponding formal parameter has a default expression, if positional
22093 notation is used.
22094
22095 This rule has no parameters.
22096
22097 @node Positional_Components
22098 @subsection @code{Positional_Components}
22099 @cindex @code{Positional_Components} rule (for @command{gnatcheck})
22100
22101 @noindent
22102 Flag each array, record and extension aggregate that includes positional
22103 notation.
22104
22105 This rule has no parameters.
22106
22107
22108 @node Positional_Generic_Parameters
22109 @subsection @code{Positional_Generic_Parameters}
22110 @cindex @code{Positional_Generic_Parameters} rule (for @command{gnatcheck})
22111
22112 @noindent
22113 Flag each instantiation using positional parameter notation.
22114
22115 This rule has no parameters.
22116
22117
22118 @node Positional_Parameters
22119 @subsection @code{Positional_Parameters}
22120 @cindex @code{Positional_Parameters} rule (for @command{gnatcheck})
22121
22122 @noindent
22123 Flag each subprogram or entry call using positional parameter notation,
22124 except for the following:
22125
22126 @itemize @bullet
22127 @item
22128 Invocations of prefix or infix operators are not flagged
22129 @item
22130 If the called subprogram or entry has only one formal parameter,
22131 the call is not flagged;
22132 @item
22133 If a subprogram call uses the @emph{Object.Operation} notation, then
22134 @itemize @minus
22135 @item
22136 the first parameter (that is, @emph{Object}) is not flagged;
22137 @item
22138 if the called subprogram has only two parameters, the second parameter
22139 of the call is not flagged;
22140 @end itemize
22141 @end itemize
22142
22143 @noindent
22144 This rule has no parameters.
22145
22146
22147
22148
22149 @node Predefined_Numeric_Types
22150 @subsection @code{Predefined_Numeric_Types}
22151 @cindex @code{Predefined_Numeric_Types} rule (for @command{gnatcheck})
22152
22153 @noindent
22154 Flag each explicit use of the name of any numeric type or subtype defined
22155 in package @code{Standard}.
22156
22157 The rationale for this rule is to detect when the
22158 program may depend on platform-specific characteristics of the implementation
22159 of the predefined numeric types. Note that this rule is over-pessimistic;
22160 for example, a program that uses @code{String} indexing
22161 likely needs a variable of type @code{Integer}.
22162 Another example is the flagging of predefined numeric types with explicit
22163 constraints:
22164
22165 @smallexample @c ada
22166 subtype My_Integer is Integer range Left .. Right;
22167 Vy_Var : My_Integer;
22168 @end smallexample
22169
22170 @noindent
22171 This rule detects only numeric types and subtypes defined in
22172 @code{Standard}. The use of numeric types and subtypes defined in other
22173 predefined packages (such as @code{System.Any_Priority} or
22174 @code{Ada.Text_IO.Count}) is not flagged
22175
22176 This rule has no parameters.
22177
22178
22179
22180 @node Raising_External_Exceptions
22181 @subsection @code{Raising_External_Exceptions}
22182 @cindex @code{Raising_External_Exceptions} rule (for @command{gnatcheck})
22183
22184 @noindent
22185 Flag any @code{raise} statement, in a program unit declared in a library
22186 package or in a generic library package, for an exception that is
22187 neither a predefined exception nor an exception that is also declared (or
22188 renamed) in the visible part of the package.
22189
22190 This rule has no parameters.
22191
22192
22193
22194 @node Raising_Predefined_Exceptions
22195 @subsection @code{Raising_Predefined_Exceptions}
22196 @cindex @code{Raising_Predefined_Exceptions} rule (for @command{gnatcheck})
22197
22198 @noindent
22199 Flag each @code{raise} statement that raises a predefined exception
22200 (i.e., one of the exceptions @code{Constraint_Error}, @code{Numeric_Error},
22201 @code{Program_Error}, @code{Storage_Error}, or @code{Tasking_Error}).
22202
22203 This rule has no parameters.
22204
22205 @node Separate_Numeric_Error_Handlers
22206 @subsection @code{Separate_Numeric_Error_Handlers}
22207 @cindex @code{Separate_Numeric_Error_Handlers} rule (for @command{gnatcheck})
22208
22209 @noindent
22210 Flags each exception handler that contains a choice for
22211 the predefined @code{Constraint_Error} exception, but does not contain
22212 the choice for the predefined @code{Numeric_Error} exception, or
22213 that contains the choice for @code{Numeric_Error}, but does not contain the
22214 choice for @code{Constraint_Error}.
22215
22216 This rule has no parameters.
22217
22218 @ignore
22219 @node Recursion
22220 @subsection @code{Recursion} (under construction, GLOBAL)
22221 @cindex @code{Recursion} rule (for @command{gnatcheck})
22222
22223 @noindent
22224 Flag recursive subprograms (cycles in the call graph). Declarations, and not
22225 calls, of recursive subprograms are detected.
22226
22227 This rule has no parameters.
22228 @end ignore
22229
22230 @ignore
22231 @node Side_Effect_Functions
22232 @subsection @code{Side_Effect_Functions} (under construction, GLOBAL)
22233 @cindex @code{Side_Effect_Functions} rule (for @command{gnatcheck})
22234
22235 @noindent
22236 Flag functions with side effects.
22237
22238 We define a side effect as changing any data object that is not local for the
22239 body of this function.
22240
22241 At the moment, we do NOT consider a side effect any input-output operations
22242 (changing a state or a content of any file).
22243
22244 We do not consider protected functions for this rule (???)
22245
22246 There are the following sources of side effect:
22247
22248 @enumerate
22249 @item Explicit (or direct) side-effect:
22250
22251 @itemize @bullet
22252 @item
22253 direct assignment to a non-local variable;
22254
22255 @item
22256 direct call to an entity that is known to change some data object that is
22257 not local for the body of this function (Note, that if F1 calls F2 and F2
22258 does have a side effect, this does not automatically mean that F1 also
22259 have a side effect, because it may be the case that F2 is declared in
22260 F1's body and it changes some data object that is global for F2, but
22261 local for F1);
22262 @end itemize
22263
22264 @item Indirect side-effect:
22265 @itemize @bullet
22266 @item
22267 Subprogram calls implicitly issued by:
22268 @itemize @bullet
22269 @item
22270 computing initialization expressions from type declarations as a part
22271 of object elaboration or allocator evaluation;
22272 @item
22273 computing implicit parameters of subprogram or entry calls or generic
22274 instantiations;
22275 @end itemize
22276
22277 @item
22278 activation of a task that change some non-local data object (directly or
22279 indirectly);
22280
22281 @item
22282 elaboration code of a package that is a result of a package instantiation;
22283
22284 @item
22285 controlled objects;
22286 @end itemize
22287
22288 @item Situations when we can suspect a side-effect, but the full static check
22289 is either impossible or too hard:
22290 @itemize @bullet
22291 @item
22292 assignment to access variables or to the objects pointed by access
22293 variables;
22294
22295 @item
22296 call to a subprogram pointed by access-to-subprogram value
22297
22298 @item
22299 dispatching calls;
22300 @end itemize
22301 @end enumerate
22302
22303 @noindent
22304 This rule has no parameters.
22305 @end ignore
22306
22307 @node Slices
22308 @subsection @code{Slices}
22309 @cindex @code{Slices} rule (for @command{gnatcheck})
22310
22311 @noindent
22312 Flag all uses of array slicing
22313
22314 This rule has no parameters.
22315
22316
22317 @node Unassigned_OUT_Parameters
22318 @subsection @code{Unassigned_OUT_Parameters}
22319 @cindex @code{Unassigned_OUT_Parameters} rule (for @command{gnatcheck})
22320
22321 @noindent
22322 Flags procedures' @code{out} parameters that are not assigned, and
22323 identifies the contexts in which the assignments are missing.
22324
22325 An @code{out} parameter is flagged in the statements in the procedure
22326 body's handled sequence of statements (before the procedure body's
22327 @code{exception} part, if any) if this sequence of statements contains
22328 no assignments to the parameter.
22329
22330 An @code{out} parameter is flagged in an exception handler in the exception
22331 part of the procedure body's handled sequence of statements if the handler
22332 contains no assignment to the parameter.
22333
22334 Bodies of generic procedures are also considered.
22335
22336 The following are treated as assignments to an @code{out} parameter:
22337
22338 @itemize @bullet
22339 @item
22340 an assignment statement, with the parameter or some component as the target;
22341
22342 @item
22343 passing the parameter (or one of its components) as an @code{out} or
22344 @code{in out} parameter.
22345 @end itemize
22346
22347 @noindent
22348 This rule does not have any parameters.
22349
22350
22351
22352 @node Uncommented_BEGIN_In_Package_Bodies
22353 @subsection @code{Uncommented_BEGIN_In_Package_Bodies}
22354 @cindex @code{Uncommented_BEGIN_In_Package_Bodies} rule (for @command{gnatcheck})
22355
22356 @noindent
22357 Flags each package body with declarations and a statement part that does not
22358 include a trailing comment on the line containing the @code{begin} keyword;
22359 this trailing comment needs to specify the package name and nothing else.
22360 The @code{begin} is not flagged if the package body does not
22361 contain any declarations.
22362
22363 If the @code{begin} keyword is placed on the
22364 same line as the last declaration or the first statement, it is flagged
22365 independently of whether the line contains a trailing comment. The
22366 diagnostic message is attached to the line containing the first statement.
22367
22368 This rule has no parameters.
22369
22370 @node Unconditional_Exits
22371 @subsection @code{Unconditional_Exits}
22372 @cindex @code{Unconditional_Exits} rule (for @command{gnatcheck})
22373
22374 @noindent
22375 Flag unconditional @code{exit} statements.
22376
22377 This rule has no parameters.
22378
22379 @node Unconstrained_Array_Returns
22380 @subsection @code{Unconstrained_Array_Returns}
22381 @cindex @code{Unconstrained_Array_Returns} rule (for @command{gnatcheck})
22382
22383 @noindent
22384 Flag each function returning an unconstrained array. Function declarations,
22385 function bodies (and body stubs) having no separate specifications,
22386 and generic function instantiations are checked.
22387 Generic function declarations, function calls and function renamings are
22388 not checked.
22389
22390 This rule has no parameters.
22391
22392 @node Universal_Ranges
22393 @subsection @code{Universal_Ranges}
22394 @cindex @code{Universal_Ranges} rule (for @command{gnatcheck})
22395
22396 @noindent
22397 Flag discrete ranges that are a part of an index constraint, constrained
22398 array definition, or @code{for}-loop parameter specification, and whose bounds
22399 are both of type @i{universal_integer}. Ranges that have at least one
22400 bound of a specific type (such as @code{1 .. N}, where @code{N} is a variable
22401 or an expression of non-universal type) are not flagged.
22402
22403 This rule has no parameters.
22404
22405
22406 @node Unnamed_Blocks_And_Loops
22407 @subsection @code{Unnamed_Blocks_And_Loops}
22408 @cindex @code{Unnamed_Blocks_And_Loops} rule (for @command{gnatcheck})
22409
22410 @noindent
22411 Flag each unnamed block statement and loop statement.
22412
22413 The rule has no parameters.
22414
22415
22416
22417 @ignore
22418 @node Unused_Subprograms
22419 @subsection @code{Unused_Subprograms} (under construction, GLOBAL)
22420 @cindex @code{Unused_Subprograms} rule (for @command{gnatcheck})
22421
22422 @noindent
22423 Flag all unused subprograms.
22424
22425 This rule has no parameters.
22426 @end ignore
22427
22428
22429
22430
22431 @node USE_PACKAGE_Clauses
22432 @subsection @code{USE_PACKAGE_Clauses}
22433 @cindex @code{USE_PACKAGE_Clauses} rule (for @command{gnatcheck})
22434
22435 @noindent
22436 Flag all @code{use} clauses for packages; @code{use type} clauses are
22437 not flagged.
22438
22439 This rule has no parameters.
22440
22441
22442
22443 @node Volatile_Objects_Without_Address_Clauses
22444 @subsection @code{Volatile_Objects_Without_Address_Clauses}
22445 @cindex @code{Volatile_Objects_Without_Address_Clauses} rule (for @command{gnatcheck})
22446
22447 @noindent
22448 Flag each volatile object that does not have an address clause.
22449
22450 The following check is made: if the pragma @code{Volatile} is applied to a
22451 data object or to its type, then an address clause must
22452 be supplied for this object.
22453
22454 This rule does not check the components of data objects,
22455 array components that are volatile as a result of the pragma
22456 @code{Volatile_Components}, or objects that are volatile because
22457 they are atomic as a result of pragmas @code{Atomic} or
22458 @code{Atomic_Components}.
22459
22460 Only variable declarations, and not constant declarations, are checked.
22461
22462 This rule has no parameters.
22463
22464
22465 @c *********************************
22466 @node Creating Sample Bodies Using gnatstub
22467 @chapter Creating Sample Bodies Using @command{gnatstub}
22468 @findex gnatstub
22469
22470 @noindent
22471 @command{gnatstub} creates body stubs, that is, empty but compilable bodies
22472 for library unit declarations.
22473
22474 Note: to invoke @code{gnatstub} with a project file, use the @code{gnat}
22475 driver (see @ref{The GNAT Driver and Project Files}).
22476
22477 To create a body stub, @command{gnatstub} has to compile the library
22478 unit declaration. Therefore, bodies can be created only for legal
22479 library units. Moreover, if a library unit depends semantically upon
22480 units located outside the current directory, you have to provide
22481 the source search path when calling @command{gnatstub}, see the description
22482 of @command{gnatstub} switches below.
22483
22484 By default, all the program unit body stubs generated by @code{gnatstub}
22485 raise the predefined @code{Program_Error} exception, which will catch
22486 accidental calls of generated stubs. This behavior can be changed with
22487 option @option{^--no-exception^/NO_EXCEPTION^} (see below).
22488
22489 @menu
22490 * Running gnatstub::
22491 * Switches for gnatstub::
22492 @end menu
22493
22494 @node Running gnatstub
22495 @section Running @command{gnatstub}
22496
22497 @noindent
22498 @command{gnatstub} has the command-line interface of the form
22499
22500 @smallexample
22501 $ gnatstub @ovar{switches} @var{filename} @ovar{directory}
22502 @end smallexample
22503
22504 @noindent
22505 where
22506 @table @var
22507 @item filename
22508 is the name of the source file that contains a library unit declaration
22509 for which a body must be created. The file name may contain the path
22510 information.
22511 The file name does not have to follow the GNAT file name conventions. If the
22512 name
22513 does not follow GNAT file naming conventions, the name of the body file must
22514 be provided
22515 explicitly as the value of the @option{^-o^/BODY=^@var{body-name}} option.
22516 If the file name follows the GNAT file naming
22517 conventions and the name of the body file is not provided,
22518 @command{gnatstub}
22519 creates the name
22520 of the body file from the argument file name by replacing the @file{.ads}
22521 suffix
22522 with the @file{.adb} suffix.
22523
22524 @item directory
22525 indicates the directory in which the body stub is to be placed (the default
22526 is the
22527 current directory)
22528
22529 @item switches
22530 is an optional sequence of switches as described in the next section
22531 @end table
22532
22533 @node Switches for gnatstub
22534 @section Switches for @command{gnatstub}
22535
22536 @table @option
22537 @c !sort!
22538
22539 @item ^-f^/FULL^
22540 @cindex @option{^-f^/FULL^} (@command{gnatstub})
22541 If the destination directory already contains a file with the name of the
22542 body file
22543 for the argument spec file, replace it with the generated body stub.
22544
22545 @item ^-hs^/HEADER=SPEC^
22546 @cindex @option{^-hs^/HEADER=SPEC^} (@command{gnatstub})
22547 Put the comment header (i.e., all the comments preceding the
22548 compilation unit) from the source of the library unit declaration
22549 into the body stub.
22550
22551 @item ^-hg^/HEADER=GENERAL^
22552 @cindex @option{^-hg^/HEADER=GENERAL^} (@command{gnatstub})
22553 Put a sample comment header into the body stub.
22554
22555 @item ^--header-file=@var{filename}^/FROM_HEADER_FILE=@var{filename}^
22556 @cindex @option{^--header-file^/FROM_HEADER_FILE=^} (@command{gnatstub})
22557 Use the content of the file as the comment header for a generated body stub.
22558
22559 @ifclear vms
22560 @item -IDIR
22561 @cindex @option{-IDIR} (@command{gnatstub})
22562 @itemx -I-
22563 @cindex @option{-I-} (@command{gnatstub})
22564 @end ifclear
22565 @ifset vms
22566 @item /NOCURRENT_DIRECTORY
22567 @cindex @option{/NOCURRENT_DIRECTORY} (@command{gnatstub})
22568 @end ifset
22569 ^These switches have ^This switch has^ the same meaning as in calls to
22570 @command{gcc}.
22571 ^They define ^It defines ^ the source search path in the call to
22572 @command{gcc} issued
22573 by @command{gnatstub} to compile an argument source file.
22574
22575 @item ^-gnatec^/CONFIGURATION_PRAGMAS_FILE=^@var{PATH}
22576 @cindex @option{^-gnatec^/CONFIGURATION_PRAGMAS_FILE^} (@command{gnatstub})
22577 This switch has the same meaning as in calls to @command{gcc}.
22578 It defines the additional configuration file to be passed to the call to
22579 @command{gcc} issued
22580 by @command{gnatstub} to compile an argument source file.
22581
22582 @item ^-gnatyM^/MAX_LINE_LENGTH=^@var{n}
22583 @cindex @option{^-gnatyM^/MAX_LINE_LENGTH^} (@command{gnatstub})
22584 (@var{n} is a non-negative integer). Set the maximum line length in the
22585 body stub to @var{n}; the default is 79. The maximum value that can be
22586 specified is 32767. Note that in the special case of configuration
22587 pragma files, the maximum is always 32767 regardless of whether or
22588 not this switch appears.
22589
22590 @item ^-gnaty^/STYLE_CHECKS=^@var{n}
22591 @cindex @option{^-gnaty^/STYLE_CHECKS=^} (@command{gnatstub})
22592 (@var{n} is a non-negative integer from 1 to 9). Set the indentation level in
22593 the generated body sample to @var{n}.
22594 The default indentation is 3.
22595
22596 @item ^-gnatyo^/ORDERED_SUBPROGRAMS^
22597 @cindex @option{^-gnato^/ORDERED_SUBPROGRAMS^} (@command{gnatstub})
22598 Order local bodies alphabetically. (By default local bodies are ordered
22599 in the same way as the corresponding local specs in the argument spec file.)
22600
22601 @item ^-i^/INDENTATION=^@var{n}
22602 @cindex @option{^-i^/INDENTATION^} (@command{gnatstub})
22603 Same as @option{^-gnaty^/STYLE_CHECKS=^@var{n}}
22604
22605 @item ^-k^/TREE_FILE=SAVE^
22606 @cindex @option{^-k^/TREE_FILE=SAVE^} (@command{gnatstub})
22607 Do not remove the tree file (i.e., the snapshot of the compiler internal
22608 structures used by @command{gnatstub}) after creating the body stub.
22609
22610 @item ^-l^/LINE_LENGTH=^@var{n}
22611 @cindex @option{^-l^/LINE_LENGTH^} (@command{gnatstub})
22612 Same as @option{^-gnatyM^/MAX_LINE_LENGTH=^@var{n}}
22613
22614 @item ^--no-exception^/NO_EXCEPTION^
22615 @cindex @option{^--no-exception^/NO_EXCEPTION^} (@command{gnatstub})
22616 Avoind raising PROGRAM_ERROR in the generated bodies of program unit stubs.
22617 This is not always possible for function stubs.
22618
22619 @item ^-o ^/BODY=^@var{body-name}
22620 @cindex @option{^-o^/BODY^} (@command{gnatstub})
22621 Body file name. This should be set if the argument file name does not
22622 follow
22623 the GNAT file naming
22624 conventions. If this switch is omitted the default name for the body will be
22625 obtained
22626 from the argument file name according to the GNAT file naming conventions.
22627
22628 @item ^-q^/QUIET^
22629 @cindex @option{^-q^/QUIET^} (@command{gnatstub})
22630 Quiet mode: do not generate a confirmation when a body is
22631 successfully created, and do not generate a message when a body is not
22632 required for an
22633 argument unit.
22634
22635 @item ^-r^/TREE_FILE=REUSE^
22636 @cindex @option{^-r^/TREE_FILE=REUSE^} (@command{gnatstub})
22637 Reuse the tree file (if it exists) instead of creating it. Instead of
22638 creating the tree file for the library unit declaration, @command{gnatstub}
22639 tries to find it in the current directory and use it for creating
22640 a body. If the tree file is not found, no body is created. This option
22641 also implies @option{^-k^/SAVE^}, whether or not
22642 the latter is set explicitly.
22643
22644 @item ^-t^/TREE_FILE=OVERWRITE^
22645 @cindex @option{^-t^/TREE_FILE=OVERWRITE^} (@command{gnatstub})
22646 Overwrite the existing tree file. If the current directory already
22647 contains the file which, according to the GNAT file naming rules should
22648 be considered as a tree file for the argument source file,
22649 @command{gnatstub}
22650 will refuse to create the tree file needed to create a sample body
22651 unless this option is set.
22652
22653 @item ^-v^/VERBOSE^
22654 @cindex @option{^-v^/VERBOSE^} (@command{gnatstub})
22655 Verbose mode: generate version information.
22656
22657 @end table
22658
22659 @c *********************************
22660 @node Generating Ada Bindings for C and C++ headers
22661 @chapter Generating Ada Bindings for C and C++ headers
22662 @findex binding
22663
22664 @noindent
22665 GNAT now comes with a new experimental binding generator for C and C++
22666 headers which is intended to do 95% of the tedious work of generating
22667 Ada specs from C or C++ header files. Note that this still is a work in
22668 progress, not designed to generate 100% correct Ada specs.
22669
22670 The code generated is using the Ada 2005 syntax, which makes it
22671 easier to interface with other languages than previous versions of Ada.
22672
22673 @menu
22674 * Running the binding generator::
22675 * Generating bindings for C++ headers::
22676 * Switches::
22677 @end menu
22678
22679 @node Running the binding generator
22680 @section Running the binding generator
22681
22682 @noindent
22683 The binding generator is part of the @command{gcc} compiler and can be
22684 invoked via the @option{-fdump-ada-spec} switch, which will generate Ada
22685 spec files for the header files specified on the command line, and all
22686 header files needed by these files transitivitely. For example:
22687
22688 @smallexample
22689 $ g++ -c -fdump-ada-spec -C /usr/include/time.h
22690 $ gcc -c -gnat05 *.ads
22691 @end smallexample
22692
22693 will generate, under GNU/Linux, the following files: @file{time_h.ads},
22694 @file{bits_time_h.ads}, @file{stddef_h.ads}, @file{bits_types_h.ads} which
22695 correspond to the files @file{/usr/include/time.h},
22696 @file{/usr/include/bits/time.h}, etc@dots{}, and will then compile in Ada 2005
22697 mode these Ada specs.
22698
22699 The @code{-C} switch tells @command{gcc} to extract comments from headers,
22700 and will attempt to generate corresponding Ada comments.
22701
22702 If you want to generate a single Ada file and not the transitive closure, you
22703 can use instead the @option{-fdump-ada-spec-slim} switch.
22704
22705 Note that we recommend when possible to use the @command{g++} driver to
22706 generate bindings, even for most C headers, since this will in general
22707 generate better Ada specs. For generating bindings for C++ headers, it is
22708 mandatory to use the @command{g++} command, or @command{gcc -x c++} which
22709 is equivalent in this case. If @command{g++} cannot work on your C headers
22710 because of incompatibilities between C and C++, then you can fallback to
22711 @command{gcc} instead.
22712
22713 For an example of better bindings generated from the C++ front-end,
22714 the name of the parameters (when available) are actually ignored by the C
22715 front-end. Consider the following C header:
22716
22717 @smallexample
22718 extern void foo (int variable);
22719 @end smallexample
22720
22721 with the C front-end, @code{variable} is ignored, and the above is handled as:
22722
22723 @smallexample
22724 extern void foo (int);
22725 @end smallexample
22726
22727 generating a generic:
22728
22729 @smallexample
22730 procedure foo (param1 : int);
22731 @end smallexample
22732
22733 with the C++ front-end, the name is available, and we generate:
22734
22735 @smallexample
22736 procedure foo (variable : int);
22737 @end smallexample
22738
22739 In some cases, the generated bindings will be more complete or more meaningful
22740 when defining some macros, which you can do via the @option{-D} switch. This
22741 is for example the case with @file{Xlib.h} under GNU/Linux:
22742
22743 @smallexample
22744 g++ -c -fdump-ada-spec -DXLIB_ILLEGAL_ACCESS -C /usr/include/X11/Xlib.h
22745 @end smallexample
22746
22747 The above will generate more complete bindings than a straight call without
22748 the @option{-DXLIB_ILLEGAL_ACCESS} switch.
22749
22750 In other cases, it is not possible to parse a header file in a stand alone
22751 manner, because other include files need to be included first. In this
22752 case, the solution is to create a small header file including the needed
22753 @code{#include} and possible @code{#define} directives. For example, to
22754 generate Ada bindings for @file{readline/readline.h}, you need to first
22755 include @file{stdio.h}, so you can create a file with the following two
22756 lines in e.g. @file{readline1.h}:
22757
22758 @smallexample
22759 #include <stdio.h>
22760 #include <readline/readline.h>
22761 @end smallexample
22762
22763 and then generate Ada bindings from this file:
22764
22765 @smallexample
22766 $ g++ -c -fdump-ada-spec readline1.h
22767 @end smallexample
22768
22769 @node Generating bindings for C++ headers
22770 @section Generating bindings for C++ headers
22771
22772 @noindent
22773 Generating bindings for C++ headers is done using the same options, always
22774 with the @command{g++} compiler.
22775
22776 In this mode, C++ classes will be mapped to Ada tagged types, constructors
22777 will be mapped using the @code{CPP_Constructor} pragma, and when possible,
22778 multiple inheritance of abstract classes will be mapped to Ada interfaces
22779 (@xref{Interfacing to C++,,,gnat_rm, GNAT Reference Manual}, for additional
22780 information on interfacing to C++).
22781
22782 For example, given the following C++ header file:
22783
22784 @smallexample
22785 @group
22786 @cartouche
22787 class Carnivore @{
22788 public:
22789 virtual int Number_Of_Teeth () = 0;
22790 @};
22791
22792 class Domestic @{
22793 public:
22794 virtual void Set_Owner (char* Name) = 0;
22795 @};
22796
22797 class Animal @{
22798 public:
22799 int Age_Count;
22800 virtual void Set_Age (int New_Age);
22801 @};
22802
22803 class Dog : Animal, Carnivore, Domestic @{
22804 public:
22805 int Tooth_Count;
22806 char *Owner;
22807
22808 virtual int Number_Of_Teeth ();
22809 virtual void Set_Owner (char* Name);
22810
22811 Dog();
22812 @};
22813 @end cartouche
22814 @end group
22815 @end smallexample
22816
22817 The corresponding Ada code is generated:
22818
22819 @smallexample @c ada
22820 @group
22821 @cartouche
22822 package Class_Carnivore is
22823 type Carnivore is limited interface;
22824 pragma Import (CPP, Carnivore);
22825
22826 function Number_Of_Teeth (this : access Carnivore) return int is abstract;
22827 end;
22828 use Class_Carnivore;
22829
22830 package Class_Domestic is
22831 type Domestic is limited interface;
22832 pragma Import (CPP, Domestic);
22833
22834 procedure Set_Owner
22835 (this : access Domestic;
22836 Name : Interfaces.C.Strings.chars_ptr) is abstract;
22837 end;
22838 use Class_Domestic;
22839
22840 package Class_Animal is
22841 type Animal is tagged limited record
22842 Age_Count : aliased int;
22843 end record;
22844 pragma Import (CPP, Animal);
22845
22846 procedure Set_Age (this : access Animal; New_Age : int);
22847 pragma Import (CPP, Set_Age, "_ZN6Animal7Set_AgeEi");
22848 end;
22849 use Class_Animal;
22850
22851 package Class_Dog is
22852 type Dog is new Animal and Carnivore and Domestic with record
22853 Tooth_Count : aliased int;
22854 Owner : Interfaces.C.Strings.chars_ptr;
22855 end record;
22856 pragma Import (CPP, Dog);
22857
22858 function Number_Of_Teeth (this : access Dog) return int;
22859 pragma Import (CPP, Number_Of_Teeth, "_ZN3Dog15Number_Of_TeethEv");
22860
22861 procedure Set_Owner
22862 (this : access Dog; Name : Interfaces.C.Strings.chars_ptr);
22863 pragma Import (CPP, Set_Owner, "_ZN3Dog9Set_OwnerEPc");
22864
22865 function New_Dog return Dog;
22866 pragma CPP_Constructor (New_Dog);
22867 pragma Import (CPP, New_Dog, "_ZN3DogC1Ev");
22868 end;
22869 use Class_Dog;
22870 @end cartouche
22871 @end group
22872 @end smallexample
22873
22874 @node Switches
22875 @section Switches
22876
22877 @table @option
22878 @item -fdump-ada-spec
22879 @cindex @option{-fdump-ada-spec} (@command{gcc})
22880 Generate Ada spec files for the given header files transitively (including
22881 all header files that these headers depend upon).
22882
22883 @item -fdump-ada-spec-slim
22884 @cindex @option{-fdump-ada-spec-slim} (@command{gcc})
22885 Generate Ada spec files for the header files specified on the command line
22886 only.
22887
22888 @item -C
22889 @cindex @option{-C} (@command{gcc})
22890 Extract comments from headers and generate Ada comments in the Ada spec files.
22891 @end table
22892
22893 @node Other Utility Programs
22894 @chapter Other Utility Programs
22895
22896 @noindent
22897 This chapter discusses some other utility programs available in the Ada
22898 environment.
22899
22900 @menu
22901 * Using Other Utility Programs with GNAT::
22902 * The External Symbol Naming Scheme of GNAT::
22903 * Converting Ada Files to html with gnathtml::
22904 * Installing gnathtml::
22905 @ifset vms
22906 * LSE::
22907 * Profiling::
22908 @end ifset
22909 @end menu
22910
22911 @node Using Other Utility Programs with GNAT
22912 @section Using Other Utility Programs with GNAT
22913
22914 @noindent
22915 The object files generated by GNAT are in standard system format and in
22916 particular the debugging information uses this format. This means
22917 programs generated by GNAT can be used with existing utilities that
22918 depend on these formats.
22919
22920 @ifclear vms
22921 In general, any utility program that works with C will also often work with
22922 Ada programs generated by GNAT. This includes software utilities such as
22923 gprof (a profiling program), @code{gdb} (the FSF debugger), and utilities such
22924 as Purify.
22925 @end ifclear
22926
22927 @node The External Symbol Naming Scheme of GNAT
22928 @section The External Symbol Naming Scheme of GNAT
22929
22930 @noindent
22931 In order to interpret the output from GNAT, when using tools that are
22932 originally intended for use with other languages, it is useful to
22933 understand the conventions used to generate link names from the Ada
22934 entity names.
22935
22936 All link names are in all lowercase letters. With the exception of library
22937 procedure names, the mechanism used is simply to use the full expanded
22938 Ada name with dots replaced by double underscores. For example, suppose
22939 we have the following package spec:
22940
22941 @smallexample @c ada
22942 @group
22943 @cartouche
22944 package QRS is
22945 MN : Integer;
22946 end QRS;
22947 @end cartouche
22948 @end group
22949 @end smallexample
22950
22951 @noindent
22952 The variable @code{MN} has a full expanded Ada name of @code{QRS.MN}, so
22953 the corresponding link name is @code{qrs__mn}.
22954 @findex Export
22955 Of course if a @code{pragma Export} is used this may be overridden:
22956
22957 @smallexample @c ada
22958 @group
22959 @cartouche
22960 package Exports is
22961 Var1 : Integer;
22962 pragma Export (Var1, C, External_Name => "var1_name");
22963 Var2 : Integer;
22964 pragma Export (Var2, C, Link_Name => "var2_link_name");
22965 end Exports;
22966 @end cartouche
22967 @end group
22968 @end smallexample
22969
22970 @noindent
22971 In this case, the link name for @var{Var1} is whatever link name the
22972 C compiler would assign for the C function @var{var1_name}. This typically
22973 would be either @var{var1_name} or @var{_var1_name}, depending on operating
22974 system conventions, but other possibilities exist. The link name for
22975 @var{Var2} is @var{var2_link_name}, and this is not operating system
22976 dependent.
22977
22978 @findex _main
22979 One exception occurs for library level procedures. A potential ambiguity
22980 arises between the required name @code{_main} for the C main program,
22981 and the name we would otherwise assign to an Ada library level procedure
22982 called @code{Main} (which might well not be the main program).
22983
22984 To avoid this ambiguity, we attach the prefix @code{_ada_} to such
22985 names. So if we have a library level procedure such as
22986
22987 @smallexample @c ada
22988 @group
22989 @cartouche
22990 procedure Hello (S : String);
22991 @end cartouche
22992 @end group
22993 @end smallexample
22994
22995 @noindent
22996 the external name of this procedure will be @var{_ada_hello}.
22997
22998
22999 @node Converting Ada Files to html with gnathtml
23000 @section Converting Ada Files to HTML with @code{gnathtml}
23001
23002 @noindent
23003 This @code{Perl} script allows Ada source files to be browsed using
23004 standard Web browsers. For installation procedure, see the section
23005 @xref{Installing gnathtml}.
23006
23007 Ada reserved keywords are highlighted in a bold font and Ada comments in
23008 a blue font. Unless your program was compiled with the gcc @option{-gnatx}
23009 switch to suppress the generation of cross-referencing information, user
23010 defined variables and types will appear in a different color; you will
23011 be able to click on any identifier and go to its declaration.
23012
23013 The command line is as follow:
23014 @smallexample
23015 $ perl gnathtml.pl @ovar{^switches^options^} @var{ada-files}
23016 @end smallexample
23017
23018 @noindent
23019 You can pass it as many Ada files as you want. @code{gnathtml} will generate
23020 an html file for every ada file, and a global file called @file{index.htm}.
23021 This file is an index of every identifier defined in the files.
23022
23023 The available ^switches^options^ are the following ones:
23024
23025 @table @option
23026 @item -83
23027 @cindex @option{-83} (@code{gnathtml})
23028 Only the Ada 83 subset of keywords will be highlighted.
23029
23030 @item -cc @var{color}
23031 @cindex @option{-cc} (@code{gnathtml})
23032 This option allows you to change the color used for comments. The default
23033 value is green. The color argument can be any name accepted by html.
23034
23035 @item -d
23036 @cindex @option{-d} (@code{gnathtml})
23037 If the Ada files depend on some other files (for instance through
23038 @code{with} clauses, the latter files will also be converted to html.
23039 Only the files in the user project will be converted to html, not the files
23040 in the run-time library itself.
23041
23042 @item -D
23043 @cindex @option{-D} (@code{gnathtml})
23044 This command is the same as @option{-d} above, but @command{gnathtml} will
23045 also look for files in the run-time library, and generate html files for them.
23046
23047 @item -ext @var{extension}
23048 @cindex @option{-ext} (@code{gnathtml})
23049 This option allows you to change the extension of the generated HTML files.
23050 If you do not specify an extension, it will default to @file{htm}.
23051
23052 @item -f
23053 @cindex @option{-f} (@code{gnathtml})
23054 By default, gnathtml will generate html links only for global entities
23055 ('with'ed units, global variables and types,@dots{}). If you specify
23056 @option{-f} on the command line, then links will be generated for local
23057 entities too.
23058
23059 @item -l @var{number}
23060 @cindex @option{-l} (@code{gnathtml})
23061 If this ^switch^option^ is provided and @var{number} is not 0, then
23062 @code{gnathtml} will number the html files every @var{number} line.
23063
23064 @item -I @var{dir}
23065 @cindex @option{-I} (@code{gnathtml})
23066 Specify a directory to search for library files (@file{.ALI} files) and
23067 source files. You can provide several -I switches on the command line,
23068 and the directories will be parsed in the order of the command line.
23069
23070 @item -o @var{dir}
23071 @cindex @option{-o} (@code{gnathtml})
23072 Specify the output directory for html files. By default, gnathtml will
23073 saved the generated html files in a subdirectory named @file{html/}.
23074
23075 @item -p @var{file}
23076 @cindex @option{-p} (@code{gnathtml})
23077 If you are using Emacs and the most recent Emacs Ada mode, which provides
23078 a full Integrated Development Environment for compiling, checking,
23079 running and debugging applications, you may use @file{.gpr} files
23080 to give the directories where Emacs can find sources and object files.
23081
23082 Using this ^switch^option^, you can tell gnathtml to use these files.
23083 This allows you to get an html version of your application, even if it
23084 is spread over multiple directories.
23085
23086 @item -sc @var{color}
23087 @cindex @option{-sc} (@code{gnathtml})
23088 This ^switch^option^ allows you to change the color used for symbol
23089 definitions.
23090 The default value is red. The color argument can be any name accepted by html.
23091
23092 @item -t @var{file}
23093 @cindex @option{-t} (@code{gnathtml})
23094 This ^switch^option^ provides the name of a file. This file contains a list of
23095 file names to be converted, and the effect is exactly as though they had
23096 appeared explicitly on the command line. This
23097 is the recommended way to work around the command line length limit on some
23098 systems.
23099
23100 @end table
23101
23102 @node Installing gnathtml
23103 @section Installing @code{gnathtml}
23104
23105 @noindent
23106 @code{Perl} needs to be installed on your machine to run this script.
23107 @code{Perl} is freely available for almost every architecture and
23108 Operating System via the Internet.
23109
23110 On Unix systems, you may want to modify the first line of the script
23111 @code{gnathtml}, to explicitly tell the Operating system where Perl
23112 is. The syntax of this line is:
23113 @smallexample
23114 #!full_path_name_to_perl
23115 @end smallexample
23116
23117 @noindent
23118 Alternatively, you may run the script using the following command line:
23119
23120 @smallexample
23121 $ perl gnathtml.pl @ovar{switches} @var{files}
23122 @end smallexample
23123
23124 @ifset vms
23125 @node LSE
23126 @section LSE
23127 @findex LSE
23128
23129 @noindent
23130 The GNAT distribution provides an Ada 95 template for the HP Language
23131 Sensitive Editor (LSE), a component of DECset. In order to
23132 access it, invoke LSE with the qualifier /ENVIRONMENT=GNU:[LIB]ADA95.ENV.
23133
23134 @node Profiling
23135 @section Profiling
23136 @findex PCA
23137
23138 @noindent
23139 GNAT supports The HP Performance Coverage Analyzer (PCA), a component
23140 of DECset. To use it proceed as outlined under ``HELP PCA'', except for running
23141 the collection phase with the /DEBUG qualifier.
23142
23143 @smallexample
23144 $ GNAT MAKE /DEBUG <PROGRAM_NAME>
23145 $ DEFINE LIB$DEBUG PCA$COLLECTOR
23146 $ RUN/DEBUG <PROGRAM_NAME>
23147 @end smallexample
23148 @noindent
23149 @end ifset
23150
23151 @ifclear vms
23152 @c ******************************
23153 @node Code Coverage and Profiling
23154 @chapter Code Coverage and Profiling
23155 @cindex Code Coverage
23156 @cindex Profiling
23157
23158 @noindent
23159 This chapter describes how to use @code{gcov} - coverage testing tool - and
23160 @code{gprof} - profiler tool - on your Ada programs.
23161
23162 @menu
23163 * Code Coverage of Ada Programs using gcov::
23164 * Profiling an Ada Program using gprof::
23165 @end menu
23166
23167 @node Code Coverage of Ada Programs using gcov
23168 @section Code Coverage of Ada Programs using gcov
23169 @cindex gcov
23170 @cindex -fprofile-arcs
23171 @cindex -ftest-coverage
23172 @cindex -coverage
23173 @cindex Code Coverage
23174
23175 @noindent
23176 @code{gcov} is a test coverage program: it analyzes the execution of a given
23177 program on selected tests, to help you determine the portions of the program
23178 that are still untested.
23179
23180 @code{gcov} is part of the GCC suite, and is described in detail in the GCC
23181 User's Guide. You can refer to this documentation for a more complete
23182 description.
23183
23184 This chapter provides a quick startup guide, and
23185 details some Gnat-specific features.
23186
23187 @menu
23188 * Quick startup guide::
23189 * Gnat specifics::
23190 @end menu
23191
23192 @node Quick startup guide
23193 @subsection Quick startup guide
23194
23195 In order to perform coverage analysis of a program using @code{gcov}, 3
23196 steps are needed:
23197
23198 @itemize @bullet
23199 @item
23200 Code instrumentation during the compilation process
23201 @item
23202 Execution of the instrumented program
23203 @item
23204 Execution of the @code{gcov} tool to generate the result.
23205 @end itemize
23206
23207 The code instrumentation needed by gcov is created at the object level:
23208 The source code is not modified in any way, because the instrumentation code is
23209 inserted by gcc during the compilation process. To compile your code with code
23210 coverage activated, you need to recompile your whole project using the
23211 switches
23212 @code{-fprofile-arcs} and @code{-ftest-coverage}, and link it using
23213 @code{-fprofile-arcs}.
23214
23215 @smallexample
23216 $ gnatmake -P my_project.gpr -f -cargs -fprofile-arcs -ftest-coverage \
23217 -largs -fprofile-arcs
23218 @end smallexample
23219
23220 This compilation process will create @file{.gcno} files together with
23221 the usual object files.
23222
23223 Once the program is compiled with coverage instrumentation, you can
23224 run it as many times as needed - on portions of a test suite for
23225 example. The first execution will produce @file{.gcda} files at the
23226 same location as the @file{.gcno} files. The following executions
23227 will update those files, so that a cumulative result of the covered
23228 portions of the program is generated.
23229
23230 Finally, you need to call the @code{gcov} tool. The different options of
23231 @code{gcov} are available in the GCC User's Guide, section 'Invoking gcov'.
23232
23233 This will create annotated source files with a @file{.gcov} extension:
23234 @file{my_main.adb} file will be analysed in @file{my_main.adb.gcov}.
23235
23236 @node Gnat specifics
23237 @subsection Gnat specifics
23238
23239 Because Ada semantics, portions of the source code may be shared among
23240 several object files. This is the case for example when generics are
23241 involved, when inlining is active or when declarations generate initialisation
23242 calls. In order to take
23243 into account this shared code, you need to call @code{gcov} on all
23244 source files of the tested program at once.
23245
23246 The list of source files might exceed the system's maximum command line
23247 length. In order to bypass this limitation, a new mechanism has been
23248 implemented in @code{gcov}: you can now list all your project's files into a
23249 text file, and provide this file to gcov as a parameter, preceded by a @@
23250 (e.g. @samp{gcov @@mysrclist.txt}).
23251
23252 Note that on AIX compiling a static library with @code{-fprofile-arcs} is
23253 not supported as there can be unresolved symbols during the final link.
23254
23255 @node Profiling an Ada Program using gprof
23256 @section Profiling an Ada Program using gprof
23257 @cindex gprof
23258 @cindex -pg
23259 @cindex Profiling
23260
23261 @noindent
23262 This section is not meant to be an exhaustive documentation of @code{gprof}.
23263 Full documentation for it can be found in the GNU Profiler User's Guide
23264 documentation that is part of this GNAT distribution.
23265
23266 Profiling a program helps determine the parts of a program that are executed
23267 most often, and are therefore the most time-consuming.
23268
23269 @code{gprof} is the standard GNU profiling tool; it has been enhanced to
23270 better handle Ada programs and multitasking.
23271 It is currently supported on the following platforms
23272 @itemize @bullet
23273 @item
23274 linux x86/x86_64
23275 @item
23276 solaris sparc/sparc64/x86
23277 @item
23278 windows x86
23279 @end itemize
23280
23281 @noindent
23282 In order to profile a program using @code{gprof}, 3 steps are needed:
23283
23284 @itemize @bullet
23285 @item
23286 Code instrumentation, requiring a full recompilation of the project with the
23287 proper switches.
23288 @item
23289 Execution of the program under the analysis conditions, i.e. with the desired
23290 input.
23291 @item
23292 Analysis of the results using the @code{gprof} tool.
23293 @end itemize
23294
23295 @noindent
23296 The following sections detail the different steps, and indicate how
23297 to interpret the results:
23298 @menu
23299 * Compilation for profiling::
23300 * Program execution::
23301 * Running gprof::
23302 * Interpretation of profiling results::
23303 @end menu
23304
23305 @node Compilation for profiling
23306 @subsection Compilation for profiling
23307 @cindex -pg
23308 @cindex Profiling
23309
23310 In order to profile a program the first step is to tell the compiler
23311 to generate the necessary profiling information. The compiler switch to be used
23312 is @code{-pg}, which must be added to other compilation switches. This
23313 switch needs to be specified both during compilation and link stages, and can
23314 be specified once when using gnatmake:
23315
23316 @smallexample
23317 gnatmake -f -pg -P my_project
23318 @end smallexample
23319
23320 @noindent
23321 Note that only the objects that were compiled with the @samp{-pg} switch will be
23322 profiled; if you need to profile your whole project, use the
23323 @samp{-f} gnatmake switch to force full recompilation.
23324
23325 @node Program execution
23326 @subsection Program execution
23327
23328 @noindent
23329 Once the program has been compiled for profiling, you can run it as usual.
23330
23331 The only constraint imposed by profiling is that the program must terminate
23332 normally. An interrupted program (via a Ctrl-C, kill, etc.) will not be
23333 properly analyzed.
23334
23335 Once the program completes execution, a data file called @file{gmon.out} is
23336 generated in the directory where the program was launched from. If this file
23337 already exists, it will be overwritten.
23338
23339 @node Running gprof
23340 @subsection Running gprof
23341
23342 @noindent
23343 The @code{gprof} tool is called as follow:
23344
23345 @smallexample
23346 gprof my_prog gmon.out
23347 @end smallexample
23348
23349 @noindent
23350 or simpler:
23351
23352 @smallexample
23353 gprof my_prog
23354 @end smallexample
23355
23356 @noindent
23357 The complete form of the gprof command line is the following:
23358
23359 @smallexample
23360 gprof [^switches^options^] [executable [data-file]]
23361 @end smallexample
23362
23363 @noindent
23364 @code{gprof} supports numerous ^switch^options^. The order of these
23365 ^switch^options^ does not matter. The full list of options can be found in
23366 the GNU Profiler User's Guide documentation that comes with this documentation.
23367
23368 The following is the subset of those switches that is most relevant:
23369
23370 @table @option
23371
23372 @item --demangle[=@var{style}]
23373 @itemx --no-demangle
23374 @cindex @option{--demangle} (@code{gprof})
23375 These options control whether symbol names should be demangled when
23376 printing output. The default is to demangle C++ symbols. The
23377 @code{--no-demangle} option may be used to turn off demangling. Different
23378 compilers have different mangling styles. The optional demangling style
23379 argument can be used to choose an appropriate demangling style for your
23380 compiler, in particular Ada symbols generated by GNAT can be demangled using
23381 @code{--demangle=gnat}.
23382
23383 @item -e @var{function_name}
23384 @cindex @option{-e} (@code{gprof})
23385 The @samp{-e @var{function}} option tells @code{gprof} not to print
23386 information about the function @var{function_name} (and its
23387 children@dots{}) in the call graph. The function will still be listed
23388 as a child of any functions that call it, but its index number will be
23389 shown as @samp{[not printed]}. More than one @samp{-e} option may be
23390 given; only one @var{function_name} may be indicated with each @samp{-e}
23391 option.
23392
23393 @item -E @var{function_name}
23394 @cindex @option{-E} (@code{gprof})
23395 The @code{-E @var{function}} option works like the @code{-e} option, but
23396 execution time spent in the function (and children who were not called from
23397 anywhere else), will not be used to compute the percentages-of-time for
23398 the call graph. More than one @samp{-E} option may be given; only one
23399 @var{function_name} may be indicated with each @samp{-E} option.
23400
23401 @item -f @var{function_name}
23402 @cindex @option{-f} (@code{gprof})
23403 The @samp{-f @var{function}} option causes @code{gprof} to limit the
23404 call graph to the function @var{function_name} and its children (and
23405 their children@dots{}). More than one @samp{-f} option may be given;
23406 only one @var{function_name} may be indicated with each @samp{-f}
23407 option.
23408
23409 @item -F @var{function_name}
23410 @cindex @option{-F} (@code{gprof})
23411 The @samp{-F @var{function}} option works like the @code{-f} option, but
23412 only time spent in the function and its children (and their
23413 children@dots{}) will be used to determine total-time and
23414 percentages-of-time for the call graph. More than one @samp{-F} option
23415 may be given; only one @var{function_name} may be indicated with each
23416 @samp{-F} option. The @samp{-F} option overrides the @samp{-E} option.
23417
23418 @end table
23419
23420 @node Interpretation of profiling results
23421 @subsection Interpretation of profiling results
23422
23423 @noindent
23424
23425 The results of the profiling analysis are represented by two arrays: the
23426 'flat profile' and the 'call graph'. Full documentation of those outputs
23427 can be found in the GNU Profiler User's Guide.
23428
23429 The flat profile shows the time spent in each function of the program, and how
23430 many time it has been called. This allows you to locate easily the most
23431 time-consuming functions.
23432
23433 The call graph shows, for each subprogram, the subprograms that call it,
23434 and the subprograms that it calls. It also provides an estimate of the time
23435 spent in each of those callers/called subprograms.
23436 @end ifclear
23437
23438 @c ******************************
23439 @node Running and Debugging Ada Programs
23440 @chapter Running and Debugging Ada Programs
23441 @cindex Debugging
23442
23443 @noindent
23444 This chapter discusses how to debug Ada programs.
23445 @ifset vms
23446 It applies to GNAT on the Alpha OpenVMS platform;
23447 for I64 OpenVMS please refer to the @cite{OpenVMS Debugger Manual},
23448 since HP has implemented Ada support in the OpenVMS debugger on I64.
23449 @end ifset
23450
23451 An incorrect Ada program may be handled in three ways by the GNAT compiler:
23452
23453 @enumerate
23454 @item
23455 The illegality may be a violation of the static semantics of Ada. In
23456 that case GNAT diagnoses the constructs in the program that are illegal.
23457 It is then a straightforward matter for the user to modify those parts of
23458 the program.
23459
23460 @item
23461 The illegality may be a violation of the dynamic semantics of Ada. In
23462 that case the program compiles and executes, but may generate incorrect
23463 results, or may terminate abnormally with some exception.
23464
23465 @item
23466 When presented with a program that contains convoluted errors, GNAT
23467 itself may terminate abnormally without providing full diagnostics on
23468 the incorrect user program.
23469 @end enumerate
23470
23471 @menu
23472 * The GNAT Debugger GDB::
23473 * Running GDB::
23474 * Introduction to GDB Commands::
23475 * Using Ada Expressions::
23476 * Calling User-Defined Subprograms::
23477 * Using the Next Command in a Function::
23478 * Ada Exceptions::
23479 * Ada Tasks::
23480 * Debugging Generic Units::
23481 * GNAT Abnormal Termination or Failure to Terminate::
23482 * Naming Conventions for GNAT Source Files::
23483 * Getting Internal Debugging Information::
23484 * Stack Traceback::
23485 @end menu
23486
23487 @cindex Debugger
23488 @findex gdb
23489
23490 @node The GNAT Debugger GDB
23491 @section The GNAT Debugger GDB
23492
23493 @noindent
23494 @code{GDB} is a general purpose, platform-independent debugger that
23495 can be used to debug mixed-language programs compiled with @command{gcc},
23496 and in particular is capable of debugging Ada programs compiled with
23497 GNAT. The latest versions of @code{GDB} are Ada-aware and can handle
23498 complex Ada data structures.
23499
23500 @xref{Top,, Debugging with GDB, gdb, Debugging with GDB},
23501 @ifset vms
23502 located in the GNU:[DOCS] directory,
23503 @end ifset
23504 for full details on the usage of @code{GDB}, including a section on
23505 its usage on programs. This manual should be consulted for full
23506 details. The section that follows is a brief introduction to the
23507 philosophy and use of @code{GDB}.
23508
23509 When GNAT programs are compiled, the compiler optionally writes debugging
23510 information into the generated object file, including information on
23511 line numbers, and on declared types and variables. This information is
23512 separate from the generated code. It makes the object files considerably
23513 larger, but it does not add to the size of the actual executable that
23514 will be loaded into memory, and has no impact on run-time performance. The
23515 generation of debug information is triggered by the use of the
23516 ^-g^/DEBUG^ switch in the @command{gcc} or @command{gnatmake} command
23517 used to carry out the compilations. It is important to emphasize that
23518 the use of these options does not change the generated code.
23519
23520 The debugging information is written in standard system formats that
23521 are used by many tools, including debuggers and profilers. The format
23522 of the information is typically designed to describe C types and
23523 semantics, but GNAT implements a translation scheme which allows full
23524 details about Ada types and variables to be encoded into these
23525 standard C formats. Details of this encoding scheme may be found in
23526 the file exp_dbug.ads in the GNAT source distribution. However, the
23527 details of this encoding are, in general, of no interest to a user,
23528 since @code{GDB} automatically performs the necessary decoding.
23529
23530 When a program is bound and linked, the debugging information is
23531 collected from the object files, and stored in the executable image of
23532 the program. Again, this process significantly increases the size of
23533 the generated executable file, but it does not increase the size of
23534 the executable program itself. Furthermore, if this program is run in
23535 the normal manner, it runs exactly as if the debug information were
23536 not present, and takes no more actual memory.
23537
23538 However, if the program is run under control of @code{GDB}, the
23539 debugger is activated. The image of the program is loaded, at which
23540 point it is ready to run. If a run command is given, then the program
23541 will run exactly as it would have if @code{GDB} were not present. This
23542 is a crucial part of the @code{GDB} design philosophy. @code{GDB} is
23543 entirely non-intrusive until a breakpoint is encountered. If no
23544 breakpoint is ever hit, the program will run exactly as it would if no
23545 debugger were present. When a breakpoint is hit, @code{GDB} accesses
23546 the debugging information and can respond to user commands to inspect
23547 variables, and more generally to report on the state of execution.
23548
23549 @c **************
23550 @node Running GDB
23551 @section Running GDB
23552
23553 @noindent
23554 This section describes how to initiate the debugger.
23555 @c The above sentence is really just filler, but it was otherwise
23556 @c clumsy to get the first paragraph nonindented given the conditional
23557 @c nature of the description
23558
23559 @ifclear vms
23560 The debugger can be launched from a @code{GPS} menu or
23561 directly from the command line. The description below covers the latter use.
23562 All the commands shown can be used in the @code{GPS} debug console window,
23563 but there are usually more GUI-based ways to achieve the same effect.
23564 @end ifclear
23565
23566 The command to run @code{GDB} is
23567
23568 @smallexample
23569 $ ^gdb program^GDB PROGRAM^
23570 @end smallexample
23571
23572 @noindent
23573 where @code{^program^PROGRAM^} is the name of the executable file. This
23574 activates the debugger and results in a prompt for debugger commands.
23575 The simplest command is simply @code{run}, which causes the program to run
23576 exactly as if the debugger were not present. The following section
23577 describes some of the additional commands that can be given to @code{GDB}.
23578
23579 @c *******************************
23580 @node Introduction to GDB Commands
23581 @section Introduction to GDB Commands
23582
23583 @noindent
23584 @code{GDB} contains a large repertoire of commands. @xref{Top,,
23585 Debugging with GDB, gdb, Debugging with GDB},
23586 @ifset vms
23587 located in the GNU:[DOCS] directory,
23588 @end ifset
23589 for extensive documentation on the use
23590 of these commands, together with examples of their use. Furthermore,
23591 the command @command{help} invoked from within GDB activates a simple help
23592 facility which summarizes the available commands and their options.
23593 In this section we summarize a few of the most commonly
23594 used commands to give an idea of what @code{GDB} is about. You should create
23595 a simple program with debugging information and experiment with the use of
23596 these @code{GDB} commands on the program as you read through the
23597 following section.
23598
23599 @table @code
23600 @item set args @var{arguments}
23601 The @var{arguments} list above is a list of arguments to be passed to
23602 the program on a subsequent run command, just as though the arguments
23603 had been entered on a normal invocation of the program. The @code{set args}
23604 command is not needed if the program does not require arguments.
23605
23606 @item run
23607 The @code{run} command causes execution of the program to start from
23608 the beginning. If the program is already running, that is to say if
23609 you are currently positioned at a breakpoint, then a prompt will ask
23610 for confirmation that you want to abandon the current execution and
23611 restart.
23612
23613 @item breakpoint @var{location}
23614 The breakpoint command sets a breakpoint, that is to say a point at which
23615 execution will halt and @code{GDB} will await further
23616 commands. @var{location} is
23617 either a line number within a file, given in the format @code{file:linenumber},
23618 or it is the name of a subprogram. If you request that a breakpoint be set on
23619 a subprogram that is overloaded, a prompt will ask you to specify on which of
23620 those subprograms you want to breakpoint. You can also
23621 specify that all of them should be breakpointed. If the program is run
23622 and execution encounters the breakpoint, then the program
23623 stops and @code{GDB} signals that the breakpoint was encountered by
23624 printing the line of code before which the program is halted.
23625
23626 @item breakpoint exception @var{name}
23627 A special form of the breakpoint command which breakpoints whenever
23628 exception @var{name} is raised.
23629 If @var{name} is omitted,
23630 then a breakpoint will occur when any exception is raised.
23631
23632 @item print @var{expression}
23633 This will print the value of the given expression. Most simple
23634 Ada expression formats are properly handled by @code{GDB}, so the expression
23635 can contain function calls, variables, operators, and attribute references.
23636
23637 @item continue
23638 Continues execution following a breakpoint, until the next breakpoint or the
23639 termination of the program.
23640
23641 @item step
23642 Executes a single line after a breakpoint. If the next statement
23643 is a subprogram call, execution continues into (the first statement of)
23644 the called subprogram.
23645
23646 @item next
23647 Executes a single line. If this line is a subprogram call, executes and
23648 returns from the call.
23649
23650 @item list
23651 Lists a few lines around the current source location. In practice, it
23652 is usually more convenient to have a separate edit window open with the
23653 relevant source file displayed. Successive applications of this command
23654 print subsequent lines. The command can be given an argument which is a
23655 line number, in which case it displays a few lines around the specified one.
23656
23657 @item backtrace
23658 Displays a backtrace of the call chain. This command is typically
23659 used after a breakpoint has occurred, to examine the sequence of calls that
23660 leads to the current breakpoint. The display includes one line for each
23661 activation record (frame) corresponding to an active subprogram.
23662
23663 @item up
23664 At a breakpoint, @code{GDB} can display the values of variables local
23665 to the current frame. The command @code{up} can be used to
23666 examine the contents of other active frames, by moving the focus up
23667 the stack, that is to say from callee to caller, one frame at a time.
23668
23669 @item down
23670 Moves the focus of @code{GDB} down from the frame currently being
23671 examined to the frame of its callee (the reverse of the previous command),
23672
23673 @item frame @var{n}
23674 Inspect the frame with the given number. The value 0 denotes the frame
23675 of the current breakpoint, that is to say the top of the call stack.
23676
23677 @end table
23678
23679 @noindent
23680 The above list is a very short introduction to the commands that
23681 @code{GDB} provides. Important additional capabilities, including conditional
23682 breakpoints, the ability to execute command sequences on a breakpoint,
23683 the ability to debug at the machine instruction level and many other
23684 features are described in detail in @ref{Top,, Debugging with GDB, gdb,
23685 Debugging with GDB}. Note that most commands can be abbreviated
23686 (for example, c for continue, bt for backtrace).
23687
23688 @node Using Ada Expressions
23689 @section Using Ada Expressions
23690 @cindex Ada expressions
23691
23692 @noindent
23693 @code{GDB} supports a fairly large subset of Ada expression syntax, with some
23694 extensions. The philosophy behind the design of this subset is
23695
23696 @itemize @bullet
23697 @item
23698 That @code{GDB} should provide basic literals and access to operations for
23699 arithmetic, dereferencing, field selection, indexing, and subprogram calls,
23700 leaving more sophisticated computations to subprograms written into the
23701 program (which therefore may be called from @code{GDB}).
23702
23703 @item
23704 That type safety and strict adherence to Ada language restrictions
23705 are not particularly important to the @code{GDB} user.
23706
23707 @item
23708 That brevity is important to the @code{GDB} user.
23709 @end itemize
23710
23711 @noindent
23712 Thus, for brevity, the debugger acts as if there were
23713 implicit @code{with} and @code{use} clauses in effect for all user-written
23714 packages, thus making it unnecessary to fully qualify most names with
23715 their packages, regardless of context. Where this causes ambiguity,
23716 @code{GDB} asks the user's intent.
23717
23718 For details on the supported Ada syntax, see @ref{Top,, Debugging with
23719 GDB, gdb, Debugging with GDB}.
23720
23721 @node Calling User-Defined Subprograms
23722 @section Calling User-Defined Subprograms
23723
23724 @noindent
23725 An important capability of @code{GDB} is the ability to call user-defined
23726 subprograms while debugging. This is achieved simply by entering
23727 a subprogram call statement in the form:
23728
23729 @smallexample
23730 call subprogram-name (parameters)
23731 @end smallexample
23732
23733 @noindent
23734 The keyword @code{call} can be omitted in the normal case where the
23735 @code{subprogram-name} does not coincide with any of the predefined
23736 @code{GDB} commands.
23737
23738 The effect is to invoke the given subprogram, passing it the
23739 list of parameters that is supplied. The parameters can be expressions and
23740 can include variables from the program being debugged. The
23741 subprogram must be defined
23742 at the library level within your program, and @code{GDB} will call the
23743 subprogram within the environment of your program execution (which
23744 means that the subprogram is free to access or even modify variables
23745 within your program).
23746
23747 The most important use of this facility is in allowing the inclusion of
23748 debugging routines that are tailored to particular data structures
23749 in your program. Such debugging routines can be written to provide a suitably
23750 high-level description of an abstract type, rather than a low-level dump
23751 of its physical layout. After all, the standard
23752 @code{GDB print} command only knows the physical layout of your
23753 types, not their abstract meaning. Debugging routines can provide information
23754 at the desired semantic level and are thus enormously useful.
23755
23756 For example, when debugging GNAT itself, it is crucial to have access to
23757 the contents of the tree nodes used to represent the program internally.
23758 But tree nodes are represented simply by an integer value (which in turn
23759 is an index into a table of nodes).
23760 Using the @code{print} command on a tree node would simply print this integer
23761 value, which is not very useful. But the PN routine (defined in file
23762 treepr.adb in the GNAT sources) takes a tree node as input, and displays
23763 a useful high level representation of the tree node, which includes the
23764 syntactic category of the node, its position in the source, the integers
23765 that denote descendant nodes and parent node, as well as varied
23766 semantic information. To study this example in more detail, you might want to
23767 look at the body of the PN procedure in the stated file.
23768
23769 @node Using the Next Command in a Function
23770 @section Using the Next Command in a Function
23771
23772 @noindent
23773 When you use the @code{next} command in a function, the current source
23774 location will advance to the next statement as usual. A special case
23775 arises in the case of a @code{return} statement.
23776
23777 Part of the code for a return statement is the ``epilog'' of the function.
23778 This is the code that returns to the caller. There is only one copy of
23779 this epilog code, and it is typically associated with the last return
23780 statement in the function if there is more than one return. In some
23781 implementations, this epilog is associated with the first statement
23782 of the function.
23783
23784 The result is that if you use the @code{next} command from a return
23785 statement that is not the last return statement of the function you
23786 may see a strange apparent jump to the last return statement or to
23787 the start of the function. You should simply ignore this odd jump.
23788 The value returned is always that from the first return statement
23789 that was stepped through.
23790
23791 @node Ada Exceptions
23792 @section Breaking on Ada Exceptions
23793 @cindex Exceptions
23794
23795 @noindent
23796 You can set breakpoints that trip when your program raises
23797 selected exceptions.
23798
23799 @table @code
23800 @item break exception
23801 Set a breakpoint that trips whenever (any task in the) program raises
23802 any exception.
23803
23804 @item break exception @var{name}
23805 Set a breakpoint that trips whenever (any task in the) program raises
23806 the exception @var{name}.
23807
23808 @item break exception unhandled
23809 Set a breakpoint that trips whenever (any task in the) program raises an
23810 exception for which there is no handler.
23811
23812 @item info exceptions
23813 @itemx info exceptions @var{regexp}
23814 The @code{info exceptions} command permits the user to examine all defined
23815 exceptions within Ada programs. With a regular expression, @var{regexp}, as
23816 argument, prints out only those exceptions whose name matches @var{regexp}.
23817 @end table
23818
23819 @node Ada Tasks
23820 @section Ada Tasks
23821 @cindex Tasks
23822
23823 @noindent
23824 @code{GDB} allows the following task-related commands:
23825
23826 @table @code
23827 @item info tasks
23828 This command shows a list of current Ada tasks, as in the following example:
23829
23830 @smallexample
23831 @iftex
23832 @leftskip=0cm
23833 @end iftex
23834 (gdb) info tasks
23835 ID TID P-ID Thread Pri State Name
23836 1 8088000 0 807e000 15 Child Activation Wait main_task
23837 2 80a4000 1 80ae000 15 Accept/Select Wait b
23838 3 809a800 1 80a4800 15 Child Activation Wait a
23839 * 4 80ae800 3 80b8000 15 Running c
23840 @end smallexample
23841
23842 @noindent
23843 In this listing, the asterisk before the first task indicates it to be the
23844 currently running task. The first column lists the task ID that is used
23845 to refer to tasks in the following commands.
23846
23847 @item break @var{linespec} task @var{taskid}
23848 @itemx break @var{linespec} task @var{taskid} if @dots{}
23849 @cindex Breakpoints and tasks
23850 These commands are like the @code{break @dots{} thread @dots{}}.
23851 @var{linespec} specifies source lines.
23852
23853 Use the qualifier @samp{task @var{taskid}} with a breakpoint command
23854 to specify that you only want @code{GDB} to stop the program when a
23855 particular Ada task reaches this breakpoint. @var{taskid} is one of the
23856 numeric task identifiers assigned by @code{GDB}, shown in the first
23857 column of the @samp{info tasks} display.
23858
23859 If you do not specify @samp{task @var{taskid}} when you set a
23860 breakpoint, the breakpoint applies to @emph{all} tasks of your
23861 program.
23862
23863 You can use the @code{task} qualifier on conditional breakpoints as
23864 well; in this case, place @samp{task @var{taskid}} before the
23865 breakpoint condition (before the @code{if}).
23866
23867 @item task @var{taskno}
23868 @cindex Task switching
23869
23870 This command allows to switch to the task referred by @var{taskno}. In
23871 particular, This allows to browse the backtrace of the specified
23872 task. It is advised to switch back to the original task before
23873 continuing execution otherwise the scheduling of the program may be
23874 perturbed.
23875 @end table
23876
23877 @noindent
23878 For more detailed information on the tasking support,
23879 see @ref{Top,, Debugging with GDB, gdb, Debugging with GDB}.
23880
23881 @node Debugging Generic Units
23882 @section Debugging Generic Units
23883 @cindex Debugging Generic Units
23884 @cindex Generics
23885
23886 @noindent
23887 GNAT always uses code expansion for generic instantiation. This means that
23888 each time an instantiation occurs, a complete copy of the original code is
23889 made, with appropriate substitutions of formals by actuals.
23890
23891 It is not possible to refer to the original generic entities in
23892 @code{GDB}, but it is always possible to debug a particular instance of
23893 a generic, by using the appropriate expanded names. For example, if we have
23894
23895 @smallexample @c ada
23896 @group
23897 @cartouche
23898 procedure g is
23899
23900 generic package k is
23901 procedure kp (v1 : in out integer);
23902 end k;
23903
23904 package body k is
23905 procedure kp (v1 : in out integer) is
23906 begin
23907 v1 := v1 + 1;
23908 end kp;
23909 end k;
23910
23911 package k1 is new k;
23912 package k2 is new k;
23913
23914 var : integer := 1;
23915
23916 begin
23917 k1.kp (var);
23918 k2.kp (var);
23919 k1.kp (var);
23920 k2.kp (var);
23921 end;
23922 @end cartouche
23923 @end group
23924 @end smallexample
23925
23926 @noindent
23927 Then to break on a call to procedure kp in the k2 instance, simply
23928 use the command:
23929
23930 @smallexample
23931 (gdb) break g.k2.kp
23932 @end smallexample
23933
23934 @noindent
23935 When the breakpoint occurs, you can step through the code of the
23936 instance in the normal manner and examine the values of local variables, as for
23937 other units.
23938
23939 @node GNAT Abnormal Termination or Failure to Terminate
23940 @section GNAT Abnormal Termination or Failure to Terminate
23941 @cindex GNAT Abnormal Termination or Failure to Terminate
23942
23943 @noindent
23944 When presented with programs that contain serious errors in syntax
23945 or semantics,
23946 GNAT may on rare occasions experience problems in operation, such
23947 as aborting with a
23948 segmentation fault or illegal memory access, raising an internal
23949 exception, terminating abnormally, or failing to terminate at all.
23950 In such cases, you can activate
23951 various features of GNAT that can help you pinpoint the construct in your
23952 program that is the likely source of the problem.
23953
23954 The following strategies are presented in increasing order of
23955 difficulty, corresponding to your experience in using GNAT and your
23956 familiarity with compiler internals.
23957
23958 @enumerate
23959 @item
23960 Run @command{gcc} with the @option{-gnatf}. This first
23961 switch causes all errors on a given line to be reported. In its absence,
23962 only the first error on a line is displayed.
23963
23964 The @option{-gnatdO} switch causes errors to be displayed as soon as they
23965 are encountered, rather than after compilation is terminated. If GNAT
23966 terminates prematurely or goes into an infinite loop, the last error
23967 message displayed may help to pinpoint the culprit.
23968
23969 @item
23970 Run @command{gcc} with the @option{^-v (verbose)^/VERBOSE^} switch. In this
23971 mode, @command{gcc} produces ongoing information about the progress of the
23972 compilation and provides the name of each procedure as code is
23973 generated. This switch allows you to find which Ada procedure was being
23974 compiled when it encountered a code generation problem.
23975
23976 @item
23977 @cindex @option{-gnatdc} switch
23978 Run @command{gcc} with the @option{-gnatdc} switch. This is a GNAT specific
23979 switch that does for the front-end what @option{^-v^VERBOSE^} does
23980 for the back end. The system prints the name of each unit,
23981 either a compilation unit or nested unit, as it is being analyzed.
23982 @item
23983 Finally, you can start
23984 @code{gdb} directly on the @code{gnat1} executable. @code{gnat1} is the
23985 front-end of GNAT, and can be run independently (normally it is just
23986 called from @command{gcc}). You can use @code{gdb} on @code{gnat1} as you
23987 would on a C program (but @pxref{The GNAT Debugger GDB} for caveats). The
23988 @code{where} command is the first line of attack; the variable
23989 @code{lineno} (seen by @code{print lineno}), used by the second phase of
23990 @code{gnat1} and by the @command{gcc} backend, indicates the source line at
23991 which the execution stopped, and @code{input_file name} indicates the name of
23992 the source file.
23993 @end enumerate
23994
23995 @node Naming Conventions for GNAT Source Files
23996 @section Naming Conventions for GNAT Source Files
23997
23998 @noindent
23999 In order to examine the workings of the GNAT system, the following
24000 brief description of its organization may be helpful:
24001
24002 @itemize @bullet
24003 @item
24004 Files with prefix @file{^sc^SC^} contain the lexical scanner.
24005
24006 @item
24007 All files prefixed with @file{^par^PAR^} are components of the parser. The
24008 numbers correspond to chapters of the Ada Reference Manual. For example,
24009 parsing of select statements can be found in @file{par-ch9.adb}.
24010
24011 @item
24012 All files prefixed with @file{^sem^SEM^} perform semantic analysis. The
24013 numbers correspond to chapters of the Ada standard. For example, all
24014 issues involving context clauses can be found in @file{sem_ch10.adb}. In
24015 addition, some features of the language require sufficient special processing
24016 to justify their own semantic files: sem_aggr for aggregates, sem_disp for
24017 dynamic dispatching, etc.
24018
24019 @item
24020 All files prefixed with @file{^exp^EXP^} perform normalization and
24021 expansion of the intermediate representation (abstract syntax tree, or AST).
24022 these files use the same numbering scheme as the parser and semantics files.
24023 For example, the construction of record initialization procedures is done in
24024 @file{exp_ch3.adb}.
24025
24026 @item
24027 The files prefixed with @file{^bind^BIND^} implement the binder, which
24028 verifies the consistency of the compilation, determines an order of
24029 elaboration, and generates the bind file.
24030
24031 @item
24032 The files @file{atree.ads} and @file{atree.adb} detail the low-level
24033 data structures used by the front-end.
24034
24035 @item
24036 The files @file{sinfo.ads} and @file{sinfo.adb} detail the structure of
24037 the abstract syntax tree as produced by the parser.
24038
24039 @item
24040 The files @file{einfo.ads} and @file{einfo.adb} detail the attributes of
24041 all entities, computed during semantic analysis.
24042
24043 @item
24044 Library management issues are dealt with in files with prefix
24045 @file{^lib^LIB^}.
24046
24047 @item
24048 @findex Ada
24049 @cindex Annex A
24050 Ada files with the prefix @file{^a-^A-^} are children of @code{Ada}, as
24051 defined in Annex A.
24052
24053 @item
24054 @findex Interfaces
24055 @cindex Annex B
24056 Files with prefix @file{^i-^I-^} are children of @code{Interfaces}, as
24057 defined in Annex B.
24058
24059 @item
24060 @findex System
24061 Files with prefix @file{^s-^S-^} are children of @code{System}. This includes
24062 both language-defined children and GNAT run-time routines.
24063
24064 @item
24065 @findex GNAT
24066 Files with prefix @file{^g-^G-^} are children of @code{GNAT}. These are useful
24067 general-purpose packages, fully documented in their specs. All
24068 the other @file{.c} files are modifications of common @command{gcc} files.
24069 @end itemize
24070
24071 @node Getting Internal Debugging Information
24072 @section Getting Internal Debugging Information
24073
24074 @noindent
24075 Most compilers have internal debugging switches and modes. GNAT
24076 does also, except GNAT internal debugging switches and modes are not
24077 secret. A summary and full description of all the compiler and binder
24078 debug flags are in the file @file{debug.adb}. You must obtain the
24079 sources of the compiler to see the full detailed effects of these flags.
24080
24081 The switches that print the source of the program (reconstructed from
24082 the internal tree) are of general interest for user programs, as are the
24083 options to print
24084 the full internal tree, and the entity table (the symbol table
24085 information). The reconstructed source provides a readable version of the
24086 program after the front-end has completed analysis and expansion,
24087 and is useful when studying the performance of specific constructs.
24088 For example, constraint checks are indicated, complex aggregates
24089 are replaced with loops and assignments, and tasking primitives
24090 are replaced with run-time calls.
24091
24092 @node Stack Traceback
24093 @section Stack Traceback
24094 @cindex traceback
24095 @cindex stack traceback
24096 @cindex stack unwinding
24097
24098 @noindent
24099 Traceback is a mechanism to display the sequence of subprogram calls that
24100 leads to a specified execution point in a program. Often (but not always)
24101 the execution point is an instruction at which an exception has been raised.
24102 This mechanism is also known as @i{stack unwinding} because it obtains
24103 its information by scanning the run-time stack and recovering the activation
24104 records of all active subprograms. Stack unwinding is one of the most
24105 important tools for program debugging.
24106
24107 The first entry stored in traceback corresponds to the deepest calling level,
24108 that is to say the subprogram currently executing the instruction
24109 from which we want to obtain the traceback.
24110
24111 Note that there is no runtime performance penalty when stack traceback
24112 is enabled, and no exception is raised during program execution.
24113
24114 @menu
24115 * Non-Symbolic Traceback::
24116 * Symbolic Traceback::
24117 @end menu
24118
24119 @node Non-Symbolic Traceback
24120 @subsection Non-Symbolic Traceback
24121 @cindex traceback, non-symbolic
24122
24123 @noindent
24124 Note: this feature is not supported on all platforms. See
24125 @file{GNAT.Traceback spec in g-traceb.ads} for a complete list of supported
24126 platforms.
24127
24128 @menu
24129 * Tracebacks From an Unhandled Exception::
24130 * Tracebacks From Exception Occurrences (non-symbolic)::
24131 * Tracebacks From Anywhere in a Program (non-symbolic)::
24132 @end menu
24133
24134 @node Tracebacks From an Unhandled Exception
24135 @subsubsection Tracebacks From an Unhandled Exception
24136
24137 @noindent
24138 A runtime non-symbolic traceback is a list of addresses of call instructions.
24139 To enable this feature you must use the @option{-E}
24140 @code{gnatbind}'s option. With this option a stack traceback is stored as part
24141 of exception information. You can retrieve this information using the
24142 @code{addr2line} tool.
24143
24144 Here is a simple example:
24145
24146 @smallexample @c ada
24147 @cartouche
24148 procedure STB is
24149
24150 procedure P1 is
24151 begin
24152 raise Constraint_Error;
24153 end P1;
24154
24155 procedure P2 is
24156 begin
24157 P1;
24158 end P2;
24159
24160 begin
24161 P2;
24162 end STB;
24163 @end cartouche
24164 @end smallexample
24165
24166 @smallexample
24167 $ gnatmake stb -bargs -E
24168 $ stb
24169
24170 Execution terminated by unhandled exception
24171 Exception name: CONSTRAINT_ERROR
24172 Message: stb.adb:5
24173 Call stack traceback locations:
24174 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
24175 @end smallexample
24176
24177 @noindent
24178 As we see the traceback lists a sequence of addresses for the unhandled
24179 exception @code{CONSTRAINT_ERROR} raised in procedure P1. It is easy to
24180 guess that this exception come from procedure P1. To translate these
24181 addresses into the source lines where the calls appear, the
24182 @code{addr2line} tool, described below, is invaluable. The use of this tool
24183 requires the program to be compiled with debug information.
24184
24185 @smallexample
24186 $ gnatmake -g stb -bargs -E
24187 $ stb
24188
24189 Execution terminated by unhandled exception
24190 Exception name: CONSTRAINT_ERROR
24191 Message: stb.adb:5
24192 Call stack traceback locations:
24193 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
24194
24195 $ addr2line --exe=stb 0x401373 0x40138b 0x40139c 0x401335 0x4011c4
24196 0x4011f1 0x77e892a4
24197
24198 00401373 at d:/stb/stb.adb:5
24199 0040138B at d:/stb/stb.adb:10
24200 0040139C at d:/stb/stb.adb:14
24201 00401335 at d:/stb/b~stb.adb:104
24202 004011C4 at /build/@dots{}/crt1.c:200
24203 004011F1 at /build/@dots{}/crt1.c:222
24204 77E892A4 in ?? at ??:0
24205 @end smallexample
24206
24207 @noindent
24208 The @code{addr2line} tool has several other useful options:
24209
24210 @table @code
24211 @item --functions
24212 to get the function name corresponding to any location
24213
24214 @item --demangle=gnat
24215 to use the gnat decoding mode for the function names. Note that
24216 for binutils version 2.9.x the option is simply @option{--demangle}.
24217 @end table
24218
24219 @smallexample
24220 $ addr2line --exe=stb --functions --demangle=gnat 0x401373 0x40138b
24221 0x40139c 0x401335 0x4011c4 0x4011f1
24222
24223 00401373 in stb.p1 at d:/stb/stb.adb:5
24224 0040138B in stb.p2 at d:/stb/stb.adb:10
24225 0040139C in stb at d:/stb/stb.adb:14
24226 00401335 in main at d:/stb/b~stb.adb:104
24227 004011C4 in <__mingw_CRTStartup> at /build/@dots{}/crt1.c:200
24228 004011F1 in <mainCRTStartup> at /build/@dots{}/crt1.c:222
24229 @end smallexample
24230
24231 @noindent
24232 From this traceback we can see that the exception was raised in
24233 @file{stb.adb} at line 5, which was reached from a procedure call in
24234 @file{stb.adb} at line 10, and so on. The @file{b~std.adb} is the binder file,
24235 which contains the call to the main program.
24236 @xref{Running gnatbind}. The remaining entries are assorted runtime routines,
24237 and the output will vary from platform to platform.
24238
24239 It is also possible to use @code{GDB} with these traceback addresses to debug
24240 the program. For example, we can break at a given code location, as reported
24241 in the stack traceback:
24242
24243 @smallexample
24244 $ gdb -nw stb
24245 @ifclear vms
24246 @noindent
24247 Furthermore, this feature is not implemented inside Windows DLL. Only
24248 the non-symbolic traceback is reported in this case.
24249 @end ifclear
24250
24251 (gdb) break *0x401373
24252 Breakpoint 1 at 0x401373: file stb.adb, line 5.
24253 @end smallexample
24254
24255 @noindent
24256 It is important to note that the stack traceback addresses
24257 do not change when debug information is included. This is particularly useful
24258 because it makes it possible to release software without debug information (to
24259 minimize object size), get a field report that includes a stack traceback
24260 whenever an internal bug occurs, and then be able to retrieve the sequence
24261 of calls with the same program compiled with debug information.
24262
24263 @node Tracebacks From Exception Occurrences (non-symbolic)
24264 @subsubsection Tracebacks From Exception Occurrences
24265
24266 @noindent
24267 Non-symbolic tracebacks are obtained by using the @option{-E} binder argument.
24268 The stack traceback is attached to the exception information string, and can
24269 be retrieved in an exception handler within the Ada program, by means of the
24270 Ada facilities defined in @code{Ada.Exceptions}. Here is a simple example:
24271
24272 @smallexample @c ada
24273 with Ada.Text_IO;
24274 with Ada.Exceptions;
24275
24276 procedure STB is
24277
24278 use Ada;
24279 use Ada.Exceptions;
24280
24281 procedure P1 is
24282 K : Positive := 1;
24283 begin
24284 K := K - 1;
24285 exception
24286 when E : others =>
24287 Text_IO.Put_Line (Exception_Information (E));
24288 end P1;
24289
24290 procedure P2 is
24291 begin
24292 P1;
24293 end P2;
24294
24295 begin
24296 P2;
24297 end STB;
24298 @end smallexample
24299
24300 @noindent
24301 This program will output:
24302
24303 @smallexample
24304 $ stb
24305
24306 Exception name: CONSTRAINT_ERROR
24307 Message: stb.adb:12
24308 Call stack traceback locations:
24309 0x4015e4 0x401633 0x401644 0x401461 0x4011c4 0x4011f1 0x77e892a4
24310 @end smallexample
24311
24312 @node Tracebacks From Anywhere in a Program (non-symbolic)
24313 @subsubsection Tracebacks From Anywhere in a Program
24314
24315 @noindent
24316 It is also possible to retrieve a stack traceback from anywhere in a
24317 program. For this you need to
24318 use the @code{GNAT.Traceback} API. This package includes a procedure called
24319 @code{Call_Chain} that computes a complete stack traceback, as well as useful
24320 display procedures described below. It is not necessary to use the
24321 @option{-E gnatbind} option in this case, because the stack traceback mechanism
24322 is invoked explicitly.
24323
24324 @noindent
24325 In the following example we compute a traceback at a specific location in
24326 the program, and we display it using @code{GNAT.Debug_Utilities.Image} to
24327 convert addresses to strings:
24328
24329 @smallexample @c ada
24330 with Ada.Text_IO;
24331 with GNAT.Traceback;
24332 with GNAT.Debug_Utilities;
24333
24334 procedure STB is
24335
24336 use Ada;
24337 use GNAT;
24338 use GNAT.Traceback;
24339
24340 procedure P1 is
24341 TB : Tracebacks_Array (1 .. 10);
24342 -- We are asking for a maximum of 10 stack frames.
24343 Len : Natural;
24344 -- Len will receive the actual number of stack frames returned.
24345 begin
24346 Call_Chain (TB, Len);
24347
24348 Text_IO.Put ("In STB.P1 : ");
24349
24350 for K in 1 .. Len loop
24351 Text_IO.Put (Debug_Utilities.Image (TB (K)));
24352 Text_IO.Put (' ');
24353 end loop;
24354
24355 Text_IO.New_Line;
24356 end P1;
24357
24358 procedure P2 is
24359 begin
24360 P1;
24361 end P2;
24362
24363 begin
24364 P2;
24365 end STB;
24366 @end smallexample
24367
24368 @smallexample
24369 $ gnatmake -g stb
24370 $ stb
24371
24372 In STB.P1 : 16#0040_F1E4# 16#0040_14F2# 16#0040_170B# 16#0040_171C#
24373 16#0040_1461# 16#0040_11C4# 16#0040_11F1# 16#77E8_92A4#
24374 @end smallexample
24375
24376 @noindent
24377 You can then get further information by invoking the @code{addr2line}
24378 tool as described earlier (note that the hexadecimal addresses
24379 need to be specified in C format, with a leading ``0x'').
24380
24381 @node Symbolic Traceback
24382 @subsection Symbolic Traceback
24383 @cindex traceback, symbolic
24384
24385 @noindent
24386 A symbolic traceback is a stack traceback in which procedure names are
24387 associated with each code location.
24388
24389 @noindent
24390 Note that this feature is not supported on all platforms. See
24391 @file{GNAT.Traceback.Symbolic spec in g-trasym.ads} for a complete
24392 list of currently supported platforms.
24393
24394 @noindent
24395 Note that the symbolic traceback requires that the program be compiled
24396 with debug information. If it is not compiled with debug information
24397 only the non-symbolic information will be valid.
24398
24399 @menu
24400 * Tracebacks From Exception Occurrences (symbolic)::
24401 * Tracebacks From Anywhere in a Program (symbolic)::
24402 @end menu
24403
24404 @node Tracebacks From Exception Occurrences (symbolic)
24405 @subsubsection Tracebacks From Exception Occurrences
24406
24407 @smallexample @c ada
24408 with Ada.Text_IO;
24409 with GNAT.Traceback.Symbolic;
24410
24411 procedure STB is
24412
24413 procedure P1 is
24414 begin
24415 raise Constraint_Error;
24416 end P1;
24417
24418 procedure P2 is
24419 begin
24420 P1;
24421 end P2;
24422
24423 procedure P3 is
24424 begin
24425 P2;
24426 end P3;
24427
24428 begin
24429 P3;
24430 exception
24431 when E : others =>
24432 Ada.Text_IO.Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
24433 end STB;
24434 @end smallexample
24435
24436 @smallexample
24437 $ gnatmake -g .\stb -bargs -E -largs -lgnat -laddr2line -lintl
24438 $ stb
24439
24440 0040149F in stb.p1 at stb.adb:8
24441 004014B7 in stb.p2 at stb.adb:13
24442 004014CF in stb.p3 at stb.adb:18
24443 004015DD in ada.stb at stb.adb:22
24444 00401461 in main at b~stb.adb:168
24445 004011C4 in __mingw_CRTStartup at crt1.c:200
24446 004011F1 in mainCRTStartup at crt1.c:222
24447 77E892A4 in ?? at ??:0
24448 @end smallexample
24449
24450 @noindent
24451 In the above example the ``.\'' syntax in the @command{gnatmake} command
24452 is currently required by @command{addr2line} for files that are in
24453 the current working directory.
24454 Moreover, the exact sequence of linker options may vary from platform
24455 to platform.
24456 The above @option{-largs} section is for Windows platforms. By contrast,
24457 under Unix there is no need for the @option{-largs} section.
24458 Differences across platforms are due to details of linker implementation.
24459
24460 @node Tracebacks From Anywhere in a Program (symbolic)
24461 @subsubsection Tracebacks From Anywhere in a Program
24462
24463 @noindent
24464 It is possible to get a symbolic stack traceback
24465 from anywhere in a program, just as for non-symbolic tracebacks.
24466 The first step is to obtain a non-symbolic
24467 traceback, and then call @code{Symbolic_Traceback} to compute the symbolic
24468 information. Here is an example:
24469
24470 @smallexample @c ada
24471 with Ada.Text_IO;
24472 with GNAT.Traceback;
24473 with GNAT.Traceback.Symbolic;
24474
24475 procedure STB is
24476
24477 use Ada;
24478 use GNAT.Traceback;
24479 use GNAT.Traceback.Symbolic;
24480
24481 procedure P1 is
24482 TB : Tracebacks_Array (1 .. 10);
24483 -- We are asking for a maximum of 10 stack frames.
24484 Len : Natural;
24485 -- Len will receive the actual number of stack frames returned.
24486 begin
24487 Call_Chain (TB, Len);
24488 Text_IO.Put_Line (Symbolic_Traceback (TB (1 .. Len)));
24489 end P1;
24490
24491 procedure P2 is
24492 begin
24493 P1;
24494 end P2;
24495
24496 begin
24497 P2;
24498 end STB;
24499 @end smallexample
24500
24501 @c ******************************
24502 @ifset vms
24503 @node Compatibility with HP Ada
24504 @chapter Compatibility with HP Ada
24505 @cindex Compatibility
24506
24507 @noindent
24508 @cindex DEC Ada
24509 @cindex HP Ada
24510 @cindex Compatibility between GNAT and HP Ada
24511 This chapter compares HP Ada (formerly known as ``DEC Ada'')
24512 for OpenVMS Alpha and GNAT for OpenVMS for Alpha and for I64.
24513 GNAT is highly compatible
24514 with HP Ada, and it should generally be straightforward to port code
24515 from the HP Ada environment to GNAT. However, there are a few language
24516 and implementation differences of which the user must be aware. These
24517 differences are discussed in this chapter. In
24518 addition, the operating environment and command structure for the
24519 compiler are different, and these differences are also discussed.
24520
24521 For further details on these and other compatibility issues,
24522 see Appendix E of the HP publication
24523 @cite{HP Ada, Technical Overview and Comparison on HP Platforms}.
24524
24525 Except where otherwise indicated, the description of GNAT for OpenVMS
24526 applies to both the Alpha and I64 platforms.
24527
24528 For information on porting Ada code from GNAT on Alpha OpenVMS to GNAT on
24529 I64 OpenVMS, see @ref{Transitioning to 64-Bit GNAT for OpenVMS}.
24530
24531 The discussion in this chapter addresses specifically the implementation
24532 of Ada 83 for HP OpenVMS Alpha Systems. In cases where the implementation
24533 of HP Ada differs between OpenVMS Alpha Systems and OpenVMS VAX Systems,
24534 GNAT always follows the Alpha implementation.
24535
24536 For GNAT running on other than VMS systems, all the HP Ada 83 pragmas and
24537 attributes are recognized, although only a subset of them can sensibly
24538 be implemented. The description of pragmas in
24539 @xref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference Manual}
24540 indicates whether or not they are applicable to non-VMS systems.
24541
24542 @menu
24543 * Ada Language Compatibility::
24544 * Differences in the Definition of Package System::
24545 * Language-Related Features::
24546 * The Package STANDARD::
24547 * The Package SYSTEM::
24548 * Tasking and Task-Related Features::
24549 * Pragmas and Pragma-Related Features::
24550 * Library of Predefined Units::
24551 * Bindings::
24552 * Main Program Definition::
24553 * Implementation-Defined Attributes::
24554 * Compiler and Run-Time Interfacing::
24555 * Program Compilation and Library Management::
24556 * Input-Output::
24557 * Implementation Limits::
24558 * Tools and Utilities::
24559 @end menu
24560
24561 @node Ada Language Compatibility
24562 @section Ada Language Compatibility
24563
24564 @noindent
24565 GNAT handles Ada 95 and Ada 2005 as well as Ada 83, whereas HP Ada is only
24566 for Ada 83. Ada 95 and Ada 2005 are almost completely upwards compatible
24567 with Ada 83, and therefore Ada 83 programs will compile
24568 and run under GNAT with
24569 no changes or only minor changes. The @cite{Annotated Ada Reference Manual}
24570 provides details on specific incompatibilities.
24571
24572 GNAT provides the switch @option{/83} on the @command{GNAT COMPILE} command,
24573 as well as the pragma @code{ADA_83}, to force the compiler to
24574 operate in Ada 83 mode. This mode does not guarantee complete
24575 conformance to Ada 83, but in practice is sufficient to
24576 eliminate most sources of incompatibilities.
24577 In particular, it eliminates the recognition of the
24578 additional Ada 95 and Ada 2005 keywords, so that their use as identifiers
24579 in Ada 83 programs is legal, and handles the cases of packages
24580 with optional bodies, and generics that instantiate unconstrained
24581 types without the use of @code{(<>)}.
24582
24583 @node Differences in the Definition of Package System
24584 @section Differences in the Definition of Package @code{System}
24585
24586 @noindent
24587 An Ada compiler is allowed to add
24588 implementation-dependent declarations to package @code{System}.
24589 In normal mode,
24590 GNAT does not take advantage of this permission, and the version of
24591 @code{System} provided by GNAT exactly matches that defined in the Ada
24592 Reference Manual.
24593
24594 However, HP Ada adds an extensive set of declarations to package
24595 @code{System},
24596 as fully documented in the HP Ada manuals. To minimize changes required
24597 for programs that make use of these extensions, GNAT provides the pragma
24598 @code{Extend_System} for extending the definition of package System. By using:
24599 @cindex pragma @code{Extend_System}
24600 @cindex @code{Extend_System} pragma
24601
24602 @smallexample @c ada
24603 @group
24604 @cartouche
24605 pragma Extend_System (Aux_DEC);
24606 @end cartouche
24607 @end group
24608 @end smallexample
24609
24610 @noindent
24611 the set of definitions in @code{System} is extended to include those in
24612 package @code{System.Aux_DEC}.
24613 @cindex @code{System.Aux_DEC} package
24614 @cindex @code{Aux_DEC} package (child of @code{System})
24615 These definitions are incorporated directly into package @code{System},
24616 as though they had been declared there. For a
24617 list of the declarations added, see the spec of this package,
24618 which can be found in the file @file{s-auxdec.ads} in the GNAT library.
24619 @cindex @file{s-auxdec.ads} file
24620 The pragma @code{Extend_System} is a configuration pragma, which means that
24621 it can be placed in the file @file{gnat.adc}, so that it will automatically
24622 apply to all subsequent compilations. See @ref{Configuration Pragmas},
24623 for further details.
24624
24625 An alternative approach that avoids the use of the non-standard
24626 @code{Extend_System} pragma is to add a context clause to the unit that
24627 references these facilities:
24628
24629 @smallexample @c ada
24630 @cartouche
24631 with System.Aux_DEC;
24632 use System.Aux_DEC;
24633 @end cartouche
24634 @end smallexample
24635
24636 @noindent
24637 The effect is not quite semantically identical to incorporating
24638 the declarations directly into package @code{System},
24639 but most programs will not notice a difference
24640 unless they use prefix notation (e.g.@: @code{System.Integer_8})
24641 to reference the entities directly in package @code{System}.
24642 For units containing such references,
24643 the prefixes must either be removed, or the pragma @code{Extend_System}
24644 must be used.
24645
24646 @node Language-Related Features
24647 @section Language-Related Features
24648
24649 @noindent
24650 The following sections highlight differences in types,
24651 representations of types, operations, alignment, and
24652 related topics.
24653
24654 @menu
24655 * Integer Types and Representations::
24656 * Floating-Point Types and Representations::
24657 * Pragmas Float_Representation and Long_Float::
24658 * Fixed-Point Types and Representations::
24659 * Record and Array Component Alignment::
24660 * Address Clauses::
24661 * Other Representation Clauses::
24662 @end menu
24663
24664 @node Integer Types and Representations
24665 @subsection Integer Types and Representations
24666
24667 @noindent
24668 The set of predefined integer types is identical in HP Ada and GNAT.
24669 Furthermore the representation of these integer types is also identical,
24670 including the capability of size clauses forcing biased representation.
24671
24672 In addition,
24673 HP Ada for OpenVMS Alpha systems has defined the
24674 following additional integer types in package @code{System}:
24675
24676 @itemize @bullet
24677
24678 @item
24679 @code{INTEGER_8}
24680
24681 @item
24682 @code{INTEGER_16}
24683
24684 @item
24685 @code{INTEGER_32}
24686
24687 @item
24688 @code{INTEGER_64}
24689
24690 @item
24691 @code{LARGEST_INTEGER}
24692 @end itemize
24693
24694 @noindent
24695 In GNAT, the first four of these types may be obtained from the
24696 standard Ada package @code{Interfaces}.
24697 Alternatively, by use of the pragma @code{Extend_System}, identical
24698 declarations can be referenced directly in package @code{System}.
24699 On both GNAT and HP Ada, the maximum integer size is 64 bits.
24700
24701 @node Floating-Point Types and Representations
24702 @subsection Floating-Point Types and Representations
24703 @cindex Floating-Point types
24704
24705 @noindent
24706 The set of predefined floating-point types is identical in HP Ada and GNAT.
24707 Furthermore the representation of these floating-point
24708 types is also identical. One important difference is that the default
24709 representation for HP Ada is @code{VAX_Float}, but the default representation
24710 for GNAT is IEEE.
24711
24712 Specific types may be declared to be @code{VAX_Float} or IEEE, using the
24713 pragma @code{Float_Representation} as described in the HP Ada
24714 documentation.
24715 For example, the declarations:
24716
24717 @smallexample @c ada
24718 @cartouche
24719 type F_Float is digits 6;
24720 pragma Float_Representation (VAX_Float, F_Float);
24721 @end cartouche
24722 @end smallexample
24723
24724 @noindent
24725 declares a type @code{F_Float} that will be represented in @code{VAX_Float}
24726 format.
24727 This set of declarations actually appears in @code{System.Aux_DEC},
24728 which contains
24729 the full set of additional floating-point declarations provided in
24730 the HP Ada version of package @code{System}.
24731 This and similar declarations may be accessed in a user program
24732 by using pragma @code{Extend_System}. The use of this
24733 pragma, and the related pragma @code{Long_Float} is described in further
24734 detail in the following section.
24735
24736 @node Pragmas Float_Representation and Long_Float
24737 @subsection Pragmas @code{Float_Representation} and @code{Long_Float}
24738
24739 @noindent
24740 HP Ada provides the pragma @code{Float_Representation}, which
24741 acts as a program library switch to allow control over
24742 the internal representation chosen for the predefined
24743 floating-point types declared in the package @code{Standard}.
24744 The format of this pragma is as follows:
24745
24746 @smallexample @c ada
24747 @cartouche
24748 pragma Float_Representation(VAX_Float | IEEE_Float);
24749 @end cartouche
24750 @end smallexample
24751
24752 @noindent
24753 This pragma controls the representation of floating-point
24754 types as follows:
24755
24756 @itemize @bullet
24757 @item
24758 @code{VAX_Float} specifies that floating-point
24759 types are represented by default with the VAX system hardware types
24760 @code{F-floating}, @code{D-floating}, @code{G-floating}.
24761 Note that the @code{H-floating}
24762 type was available only on VAX systems, and is not available
24763 in either HP Ada or GNAT.
24764
24765 @item
24766 @code{IEEE_Float} specifies that floating-point
24767 types are represented by default with the IEEE single and
24768 double floating-point types.
24769 @end itemize
24770
24771 @noindent
24772 GNAT provides an identical implementation of the pragma
24773 @code{Float_Representation}, except that it functions as a
24774 configuration pragma. Note that the
24775 notion of configuration pragma corresponds closely to the
24776 HP Ada notion of a program library switch.
24777
24778 When no pragma is used in GNAT, the default is @code{IEEE_Float},
24779 which is different
24780 from HP Ada 83, where the default is @code{VAX_Float}. In addition, the
24781 predefined libraries in GNAT are built using @code{IEEE_Float}, so it is not
24782 advisable to change the format of numbers passed to standard library
24783 routines, and if necessary explicit type conversions may be needed.
24784
24785 The use of @code{IEEE_Float} is recommended in GNAT since it is more
24786 efficient, and (given that it conforms to an international standard)
24787 potentially more portable.
24788 The situation in which @code{VAX_Float} may be useful is in interfacing
24789 to existing code and data that expect the use of @code{VAX_Float}.
24790 In such a situation use the predefined @code{VAX_Float}
24791 types in package @code{System}, as extended by
24792 @code{Extend_System}. For example, use @code{System.F_Float}
24793 to specify the 32-bit @code{F-Float} format.
24794
24795 @noindent
24796 On OpenVMS systems, HP Ada provides the pragma @code{Long_Float}
24797 to allow control over the internal representation chosen
24798 for the predefined type @code{Long_Float} and for floating-point
24799 type declarations with digits specified in the range 7 .. 15.
24800 The format of this pragma is as follows:
24801
24802 @smallexample @c ada
24803 @cartouche
24804 pragma Long_Float (D_FLOAT | G_FLOAT);
24805 @end cartouche
24806 @end smallexample
24807
24808 @node Fixed-Point Types and Representations
24809 @subsection Fixed-Point Types and Representations
24810
24811 @noindent
24812 On HP Ada for OpenVMS Alpha systems, rounding is
24813 away from zero for both positive and negative numbers.
24814 Therefore, @code{+0.5} rounds to @code{1},
24815 and @code{-0.5} rounds to @code{-1}.
24816
24817 On GNAT the results of operations
24818 on fixed-point types are in accordance with the Ada
24819 rules. In particular, results of operations on decimal
24820 fixed-point types are truncated.
24821
24822 @node Record and Array Component Alignment
24823 @subsection Record and Array Component Alignment
24824
24825 @noindent
24826 On HP Ada for OpenVMS Alpha, all non-composite components
24827 are aligned on natural boundaries. For example, 1-byte
24828 components are aligned on byte boundaries, 2-byte
24829 components on 2-byte boundaries, 4-byte components on 4-byte
24830 byte boundaries, and so on. The OpenVMS Alpha hardware
24831 runs more efficiently with naturally aligned data.
24832
24833 On GNAT, alignment rules are compatible
24834 with HP Ada for OpenVMS Alpha.
24835
24836 @node Address Clauses
24837 @subsection Address Clauses
24838
24839 @noindent
24840 In HP Ada and GNAT, address clauses are supported for
24841 objects and imported subprograms.
24842 The predefined type @code{System.Address} is a private type
24843 in both compilers on Alpha OpenVMS, with the same representation
24844 (it is simply a machine pointer). Addition, subtraction, and comparison
24845 operations are available in the standard Ada package
24846 @code{System.Storage_Elements}, or in package @code{System}
24847 if it is extended to include @code{System.Aux_DEC} using a
24848 pragma @code{Extend_System} as previously described.
24849
24850 Note that code that @code{with}'s both this extended package @code{System}
24851 and the package @code{System.Storage_Elements} should not @code{use}
24852 both packages, or ambiguities will result. In general it is better
24853 not to mix these two sets of facilities. The Ada package was
24854 designed specifically to provide the kind of features that HP Ada
24855 adds directly to package @code{System}.
24856
24857 The type @code{System.Address} is a 64-bit integer type in GNAT for
24858 I64 OpenVMS. For more information,
24859 see @ref{Transitioning to 64-Bit GNAT for OpenVMS}.
24860
24861 GNAT is compatible with HP Ada in its handling of address
24862 clauses, except for some limitations in
24863 the form of address clauses for composite objects with
24864 initialization. Such address clauses are easily replaced
24865 by the use of an explicitly-defined constant as described
24866 in the Ada Reference Manual (13.1(22)). For example, the sequence
24867 of declarations:
24868
24869 @smallexample @c ada
24870 @cartouche
24871 X, Y : Integer := Init_Func;
24872 Q : String (X .. Y) := "abc";
24873 @dots{}
24874 for Q'Address use Compute_Address;
24875 @end cartouche
24876 @end smallexample
24877
24878 @noindent
24879 will be rejected by GNAT, since the address cannot be computed at the time
24880 that @code{Q} is declared. To achieve the intended effect, write instead:
24881
24882 @smallexample @c ada
24883 @group
24884 @cartouche
24885 X, Y : Integer := Init_Func;
24886 Q_Address : constant Address := Compute_Address;
24887 Q : String (X .. Y) := "abc";
24888 @dots{}
24889 for Q'Address use Q_Address;
24890 @end cartouche
24891 @end group
24892 @end smallexample
24893
24894 @noindent
24895 which will be accepted by GNAT (and other Ada compilers), and is also
24896 compatible with Ada 83. A fuller description of the restrictions
24897 on address specifications is found in @ref{Top, GNAT Reference Manual,
24898 About This Guide, gnat_rm, GNAT Reference Manual}.
24899
24900 @node Other Representation Clauses
24901 @subsection Other Representation Clauses
24902
24903 @noindent
24904 GNAT implements in a compatible manner all the representation
24905 clauses supported by HP Ada. In addition, GNAT
24906 implements the representation clause forms that were introduced in Ada 95,
24907 including @code{COMPONENT_SIZE} and @code{SIZE} clauses for objects.
24908
24909 @node The Package STANDARD
24910 @section The Package @code{STANDARD}
24911
24912 @noindent
24913 The package @code{STANDARD}, as implemented by HP Ada, is fully
24914 described in the @cite{Ada Reference Manual} and in the
24915 @cite{HP Ada Language Reference Manual}. As implemented by GNAT, the
24916 package @code{STANDARD} is described in the @cite{Ada Reference Manual}.
24917
24918 In addition, HP Ada supports the Latin-1 character set in
24919 the type @code{CHARACTER}. GNAT supports the Latin-1 character set
24920 in the type @code{CHARACTER} and also Unicode (ISO 10646 BMP) in
24921 the type @code{WIDE_CHARACTER}.
24922
24923 The floating-point types supported by GNAT are those
24924 supported by HP Ada, but the defaults are different, and are controlled by
24925 pragmas. See @ref{Floating-Point Types and Representations}, for details.
24926
24927 @node The Package SYSTEM
24928 @section The Package @code{SYSTEM}
24929
24930 @noindent
24931 HP Ada provides a specific version of the package
24932 @code{SYSTEM} for each platform on which the language is implemented.
24933 For the complete spec of the package @code{SYSTEM}, see
24934 Appendix F of the @cite{HP Ada Language Reference Manual}.
24935
24936 On HP Ada, the package @code{SYSTEM} includes the following conversion
24937 functions:
24938 @itemize @bullet
24939 @item @code{TO_ADDRESS(INTEGER)}
24940
24941 @item @code{TO_ADDRESS(UNSIGNED_LONGWORD)}
24942
24943 @item @code{TO_ADDRESS(}@i{universal_integer}@code{)}
24944
24945 @item @code{TO_INTEGER(ADDRESS)}
24946
24947 @item @code{TO_UNSIGNED_LONGWORD(ADDRESS)}
24948
24949 @item Function @code{IMPORT_VALUE return UNSIGNED_LONGWORD} and the
24950 functions @code{IMPORT_ADDRESS} and @code{IMPORT_LARGEST_VALUE}
24951 @end itemize
24952
24953 @noindent
24954 By default, GNAT supplies a version of @code{SYSTEM} that matches
24955 the definition given in the @cite{Ada Reference Manual}.
24956 This
24957 is a subset of the HP system definitions, which is as
24958 close as possible to the original definitions. The only difference
24959 is that the definition of @code{SYSTEM_NAME} is different:
24960
24961 @smallexample @c ada
24962 @cartouche
24963 type Name is (SYSTEM_NAME_GNAT);
24964 System_Name : constant Name := SYSTEM_NAME_GNAT;
24965 @end cartouche
24966 @end smallexample
24967
24968 @noindent
24969 Also, GNAT adds the Ada declarations for
24970 @code{BIT_ORDER} and @code{DEFAULT_BIT_ORDER}.
24971
24972 However, the use of the following pragma causes GNAT
24973 to extend the definition of package @code{SYSTEM} so that it
24974 encompasses the full set of HP-specific extensions,
24975 including the functions listed above:
24976
24977 @smallexample @c ada
24978 @cartouche
24979 pragma Extend_System (Aux_DEC);
24980 @end cartouche
24981 @end smallexample
24982
24983 @noindent
24984 The pragma @code{Extend_System} is a configuration pragma that
24985 is most conveniently placed in the @file{gnat.adc} file. @xref{Pragma
24986 Extend_System,,, gnat_rm, GNAT Reference Manual} for further details.
24987
24988 HP Ada does not allow the recompilation of the package
24989 @code{SYSTEM}. Instead HP Ada provides several pragmas
24990 (@code{SYSTEM_NAME}, @code{STORAGE_UNIT}, and @code{MEMORY_SIZE})
24991 to modify values in the package @code{SYSTEM}.
24992 On OpenVMS Alpha systems, the pragma
24993 @code{SYSTEM_NAME} takes the enumeration literal @code{OPENVMS_AXP} as
24994 its single argument.
24995
24996 GNAT does permit the recompilation of package @code{SYSTEM} using
24997 the special switch @option{-gnatg}, and this switch can be used if
24998 it is necessary to modify the definitions in @code{SYSTEM}. GNAT does
24999 not permit the specification of @code{SYSTEM_NAME}, @code{STORAGE_UNIT}
25000 or @code{MEMORY_SIZE} by any other means.
25001
25002 On GNAT systems, the pragma @code{SYSTEM_NAME} takes the
25003 enumeration literal @code{SYSTEM_NAME_GNAT}.
25004
25005 The definitions provided by the use of
25006
25007 @smallexample @c ada
25008 pragma Extend_System (AUX_Dec);
25009 @end smallexample
25010
25011 @noindent
25012 are virtually identical to those provided by the HP Ada 83 package
25013 @code{SYSTEM}. One important difference is that the name of the
25014 @code{TO_ADDRESS}
25015 function for type @code{UNSIGNED_LONGWORD} is changed to
25016 @code{TO_ADDRESS_LONG}.
25017 @xref{Address Clauses,,, gnat_rm, GNAT Reference Manual} for a
25018 discussion of why this change was necessary.
25019
25020 @noindent
25021 The version of @code{TO_ADDRESS} taking a @i{universal_integer} argument
25022 is in fact
25023 an extension to Ada 83 not strictly compatible with the reference manual.
25024 GNAT, in order to be exactly compatible with the standard,
25025 does not provide this capability. In HP Ada 83, the
25026 point of this definition is to deal with a call like:
25027
25028 @smallexample @c ada
25029 TO_ADDRESS (16#12777#);
25030 @end smallexample
25031
25032 @noindent
25033 Normally, according to Ada 83 semantics, one would expect this to be
25034 ambiguous, since it matches both the @code{INTEGER} and
25035 @code{UNSIGNED_LONGWORD} forms of @code{TO_ADDRESS}.
25036 However, in HP Ada 83, there is no ambiguity, since the
25037 definition using @i{universal_integer} takes precedence.
25038
25039 In GNAT, since the version with @i{universal_integer} cannot be supplied,
25040 it is
25041 not possible to be 100% compatible. Since there are many programs using
25042 numeric constants for the argument to @code{TO_ADDRESS}, the decision in
25043 GNAT was
25044 to change the name of the function in the @code{UNSIGNED_LONGWORD} case,
25045 so the declarations provided in the GNAT version of @code{AUX_Dec} are:
25046
25047 @smallexample @c ada
25048 function To_Address (X : Integer) return Address;
25049 pragma Pure_Function (To_Address);
25050
25051 function To_Address_Long (X : Unsigned_Longword) return Address;
25052 pragma Pure_Function (To_Address_Long);
25053 @end smallexample
25054
25055 @noindent
25056 This means that programs using @code{TO_ADDRESS} for
25057 @code{UNSIGNED_LONGWORD} must change the name to @code{TO_ADDRESS_LONG}.
25058
25059 @node Tasking and Task-Related Features
25060 @section Tasking and Task-Related Features
25061
25062 @noindent
25063 This section compares the treatment of tasking in GNAT
25064 and in HP Ada for OpenVMS Alpha.
25065 The GNAT description applies to both Alpha and I64 OpenVMS.
25066 For detailed information on tasking in
25067 HP Ada, see the @cite{HP Ada Language Reference Manual} and the
25068 relevant run-time reference manual.
25069
25070 @menu
25071 * Implementation of Tasks in HP Ada for OpenVMS Alpha Systems::
25072 * Assigning Task IDs::
25073 * Task IDs and Delays::
25074 * Task-Related Pragmas::
25075 * Scheduling and Task Priority::
25076 * The Task Stack::
25077 * External Interrupts::
25078 @end menu
25079
25080 @node Implementation of Tasks in HP Ada for OpenVMS Alpha Systems
25081 @subsection Implementation of Tasks in HP Ada for OpenVMS Alpha Systems
25082
25083 @noindent
25084 On OpenVMS Alpha systems, each Ada task (except a passive
25085 task) is implemented as a single stream of execution
25086 that is created and managed by the kernel. On these
25087 systems, HP Ada tasking support is based on DECthreads,
25088 an implementation of the POSIX standard for threads.
25089
25090 Also, on OpenVMS Alpha systems, HP Ada tasks and foreign
25091 code that calls DECthreads routines can be used together.
25092 The interaction between Ada tasks and DECthreads routines
25093 can have some benefits. For example when on OpenVMS Alpha,
25094 HP Ada can call C code that is already threaded.
25095
25096 GNAT uses the facilities of DECthreads,
25097 and Ada tasks are mapped to threads.
25098
25099 @node Assigning Task IDs
25100 @subsection Assigning Task IDs
25101
25102 @noindent
25103 The HP Ada Run-Time Library always assigns @code{%TASK 1} to
25104 the environment task that executes the main program. On
25105 OpenVMS Alpha systems, @code{%TASK 0} is often used for tasks
25106 that have been created but are not yet activated.
25107
25108 On OpenVMS Alpha systems, task IDs are assigned at
25109 activation. On GNAT systems, task IDs are also assigned at
25110 task creation but do not have the same form or values as
25111 task ID values in HP Ada. There is no null task, and the
25112 environment task does not have a specific task ID value.
25113
25114 @node Task IDs and Delays
25115 @subsection Task IDs and Delays
25116
25117 @noindent
25118 On OpenVMS Alpha systems, tasking delays are implemented
25119 using Timer System Services. The Task ID is used for the
25120 identification of the timer request (the @code{REQIDT} parameter).
25121 If Timers are used in the application take care not to use
25122 @code{0} for the identification, because cancelling such a timer
25123 will cancel all timers and may lead to unpredictable results.
25124
25125 @node Task-Related Pragmas
25126 @subsection Task-Related Pragmas
25127
25128 @noindent
25129 Ada supplies the pragma @code{TASK_STORAGE}, which allows
25130 specification of the size of the guard area for a task
25131 stack. (The guard area forms an area of memory that has no
25132 read or write access and thus helps in the detection of
25133 stack overflow.) On OpenVMS Alpha systems, if the pragma
25134 @code{TASK_STORAGE} specifies a value of zero, a minimal guard
25135 area is created. In the absence of a pragma @code{TASK_STORAGE},
25136 a default guard area is created.
25137
25138 GNAT supplies the following task-related pragmas:
25139
25140 @itemize @bullet
25141 @item @code{TASK_INFO}
25142
25143 This pragma appears within a task definition and
25144 applies to the task in which it appears. The argument
25145 must be of type @code{SYSTEM.TASK_INFO.TASK_INFO_TYPE}.
25146
25147 @item @code{TASK_STORAGE}
25148
25149 GNAT implements pragma @code{TASK_STORAGE} in the same way as HP Ada.
25150 Both HP Ada and GNAT supply the pragmas @code{PASSIVE},
25151 @code{SUPPRESS}, and @code{VOLATILE}.
25152 @end itemize
25153 @node Scheduling and Task Priority
25154 @subsection Scheduling and Task Priority
25155
25156 @noindent
25157 HP Ada implements the Ada language requirement that
25158 when two tasks are eligible for execution and they have
25159 different priorities, the lower priority task does not
25160 execute while the higher priority task is waiting. The HP
25161 Ada Run-Time Library keeps a task running until either the
25162 task is suspended or a higher priority task becomes ready.
25163
25164 On OpenVMS Alpha systems, the default strategy is round-
25165 robin with preemption. Tasks of equal priority take turns
25166 at the processor. A task is run for a certain period of
25167 time and then placed at the tail of the ready queue for
25168 its priority level.
25169
25170 HP Ada provides the implementation-defined pragma @code{TIME_SLICE},
25171 which can be used to enable or disable round-robin
25172 scheduling of tasks with the same priority.
25173 See the relevant HP Ada run-time reference manual for
25174 information on using the pragmas to control HP Ada task
25175 scheduling.
25176
25177 GNAT follows the scheduling rules of Annex D (Real-Time
25178 Annex) of the @cite{Ada Reference Manual}. In general, this
25179 scheduling strategy is fully compatible with HP Ada
25180 although it provides some additional constraints (as
25181 fully documented in Annex D).
25182 GNAT implements time slicing control in a manner compatible with
25183 HP Ada 83, by means of the pragma @code{Time_Slice}, whose semantics
25184 are identical to the HP Ada 83 pragma of the same name.
25185 Note that it is not possible to mix GNAT tasking and
25186 HP Ada 83 tasking in the same program, since the two run-time
25187 libraries are not compatible.
25188
25189 @node The Task Stack
25190 @subsection The Task Stack
25191
25192 @noindent
25193 In HP Ada, a task stack is allocated each time a
25194 non-passive task is activated. As soon as the task is
25195 terminated, the storage for the task stack is deallocated.
25196 If you specify a size of zero (bytes) with @code{T'STORAGE_SIZE},
25197 a default stack size is used. Also, regardless of the size
25198 specified, some additional space is allocated for task
25199 management purposes. On OpenVMS Alpha systems, at least
25200 one page is allocated.
25201
25202 GNAT handles task stacks in a similar manner. In accordance with
25203 the Ada rules, it provides the pragma @code{STORAGE_SIZE} as
25204 an alternative method for controlling the task stack size.
25205 The specification of the attribute @code{T'STORAGE_SIZE} is also
25206 supported in a manner compatible with HP Ada.
25207
25208 @node External Interrupts
25209 @subsection External Interrupts
25210
25211 @noindent
25212 On HP Ada, external interrupts can be associated with task entries.
25213 GNAT is compatible with HP Ada in its handling of external interrupts.
25214
25215 @node Pragmas and Pragma-Related Features
25216 @section Pragmas and Pragma-Related Features
25217
25218 @noindent
25219 Both HP Ada and GNAT supply all language-defined pragmas
25220 as specified by the Ada 83 standard. GNAT also supplies all
25221 language-defined pragmas introduced by Ada 95 and Ada 2005.
25222 In addition, GNAT implements the implementation-defined pragmas
25223 from HP Ada 83.
25224
25225 @itemize @bullet
25226 @item @code{AST_ENTRY}
25227
25228 @item @code{COMMON_OBJECT}
25229
25230 @item @code{COMPONENT_ALIGNMENT}
25231
25232 @item @code{EXPORT_EXCEPTION}
25233
25234 @item @code{EXPORT_FUNCTION}
25235
25236 @item @code{EXPORT_OBJECT}
25237
25238 @item @code{EXPORT_PROCEDURE}
25239
25240 @item @code{EXPORT_VALUED_PROCEDURE}
25241
25242 @item @code{FLOAT_REPRESENTATION}
25243
25244 @item @code{IDENT}
25245
25246 @item @code{IMPORT_EXCEPTION}
25247
25248 @item @code{IMPORT_FUNCTION}
25249
25250 @item @code{IMPORT_OBJECT}
25251
25252 @item @code{IMPORT_PROCEDURE}
25253
25254 @item @code{IMPORT_VALUED_PROCEDURE}
25255
25256 @item @code{INLINE_GENERIC}
25257
25258 @item @code{INTERFACE_NAME}
25259
25260 @item @code{LONG_FLOAT}
25261
25262 @item @code{MAIN_STORAGE}
25263
25264 @item @code{PASSIVE}
25265
25266 @item @code{PSECT_OBJECT}
25267
25268 @item @code{SHARE_GENERIC}
25269
25270 @item @code{SUPPRESS_ALL}
25271
25272 @item @code{TASK_STORAGE}
25273
25274 @item @code{TIME_SLICE}
25275
25276 @item @code{TITLE}
25277 @end itemize
25278
25279 @noindent
25280 These pragmas are all fully implemented, with the exception of @code{TITLE},
25281 @code{PASSIVE}, and @code{SHARE_GENERIC}, which are
25282 recognized, but which have no
25283 effect in GNAT. The effect of @code{PASSIVE} may be obtained by the
25284 use of Ada protected objects. In GNAT, all generics are inlined.
25285
25286 Unlike HP Ada, the GNAT ``@code{EXPORT_}@i{subprogram}'' pragmas require
25287 a separate subprogram specification which must appear before the
25288 subprogram body.
25289
25290 GNAT also supplies a number of implementation-defined pragmas as follows:
25291 @itemize @bullet
25292 @item @code{ABORT_DEFER}
25293
25294 @item @code{ADA_83}
25295
25296 @item @code{ADA_95}
25297
25298 @item @code{ADA_05}
25299
25300 @item @code{ANNOTATE}
25301
25302 @item @code{ASSERT}
25303
25304 @item @code{C_PASS_BY_COPY}
25305
25306 @item @code{CPP_CLASS}
25307
25308 @item @code{CPP_CONSTRUCTOR}
25309
25310 @item @code{CPP_DESTRUCTOR}
25311
25312 @item @code{DEBUG}
25313
25314 @item @code{EXTEND_SYSTEM}
25315
25316 @item @code{LINKER_ALIAS}
25317
25318 @item @code{LINKER_SECTION}
25319
25320 @item @code{MACHINE_ATTRIBUTE}
25321
25322 @item @code{NO_RETURN}
25323
25324 @item @code{PURE_FUNCTION}
25325
25326 @item @code{SOURCE_FILE_NAME}
25327
25328 @item @code{SOURCE_REFERENCE}
25329
25330 @item @code{TASK_INFO}
25331
25332 @item @code{UNCHECKED_UNION}
25333
25334 @item @code{UNIMPLEMENTED_UNIT}
25335
25336 @item @code{UNIVERSAL_DATA}
25337
25338 @item @code{UNSUPPRESS}
25339
25340 @item @code{WARNINGS}
25341
25342 @item @code{WEAK_EXTERNAL}
25343 @end itemize
25344
25345 @noindent
25346 For full details on these GNAT implementation-defined pragmas,
25347 see @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference
25348 Manual}.
25349
25350 @menu
25351 * Restrictions on the Pragma INLINE::
25352 * Restrictions on the Pragma INTERFACE::
25353 * Restrictions on the Pragma SYSTEM_NAME::
25354 @end menu
25355
25356 @node Restrictions on the Pragma INLINE
25357 @subsection Restrictions on Pragma @code{INLINE}
25358
25359 @noindent
25360 HP Ada enforces the following restrictions on the pragma @code{INLINE}:
25361 @itemize @bullet
25362 @item Parameters cannot have a task type.
25363
25364 @item Function results cannot be task types, unconstrained
25365 array types, or unconstrained types with discriminants.
25366
25367 @item Bodies cannot declare the following:
25368 @itemize @bullet
25369 @item Subprogram body or stub (imported subprogram is allowed)
25370
25371 @item Tasks
25372
25373 @item Generic declarations
25374
25375 @item Instantiations
25376
25377 @item Exceptions
25378
25379 @item Access types (types derived from access types allowed)
25380
25381 @item Array or record types
25382
25383 @item Dependent tasks
25384
25385 @item Direct recursive calls of subprogram or containing
25386 subprogram, directly or via a renaming
25387
25388 @end itemize
25389 @end itemize
25390
25391 @noindent
25392 In GNAT, the only restriction on pragma @code{INLINE} is that the
25393 body must occur before the call if both are in the same
25394 unit, and the size must be appropriately small. There are
25395 no other specific restrictions which cause subprograms to
25396 be incapable of being inlined.
25397
25398 @node Restrictions on the Pragma INTERFACE
25399 @subsection Restrictions on Pragma @code{INTERFACE}
25400
25401 @noindent
25402 The following restrictions on pragma @code{INTERFACE}
25403 are enforced by both HP Ada and GNAT:
25404 @itemize @bullet
25405 @item Languages accepted: Ada, Bliss, C, Fortran, Default.
25406 Default is the default on OpenVMS Alpha systems.
25407
25408 @item Parameter passing: Language specifies default
25409 mechanisms but can be overridden with an @code{EXPORT} pragma.
25410
25411 @itemize @bullet
25412 @item Ada: Use internal Ada rules.
25413
25414 @item Bliss, C: Parameters must be mode @code{in}; cannot be
25415 record or task type. Result cannot be a string, an
25416 array, or a record.
25417
25418 @item Fortran: Parameters cannot have a task type. Result cannot
25419 be a string, an array, or a record.
25420 @end itemize
25421 @end itemize
25422
25423 @noindent
25424 GNAT is entirely upwards compatible with HP Ada, and in addition allows
25425 record parameters for all languages.
25426
25427 @node Restrictions on the Pragma SYSTEM_NAME
25428 @subsection Restrictions on Pragma @code{SYSTEM_NAME}
25429
25430 @noindent
25431 For HP Ada for OpenVMS Alpha, the enumeration literal
25432 for the type @code{NAME} is @code{OPENVMS_AXP}.
25433 In GNAT, the enumeration
25434 literal for the type @code{NAME} is @code{SYSTEM_NAME_GNAT}.
25435
25436 @node Library of Predefined Units
25437 @section Library of Predefined Units
25438
25439 @noindent
25440 A library of predefined units is provided as part of the
25441 HP Ada and GNAT implementations. HP Ada does not provide
25442 the package @code{MACHINE_CODE} but instead recommends importing
25443 assembler code.
25444
25445 The GNAT versions of the HP Ada Run-Time Library (@code{ADA$PREDEFINED:})
25446 units are taken from the OpenVMS Alpha version, not the OpenVMS VAX
25447 version.
25448 The HP Ada Predefined Library units are modified to remove post-Ada 83
25449 incompatibilities and to make them interoperable with GNAT
25450 (@pxref{Changes to DECLIB}, for details).
25451 The units are located in the @file{DECLIB} directory.
25452
25453 The GNAT RTL is contained in
25454 the @file{ADALIB} directory, and
25455 the default search path is set up to find @code{DECLIB} units in preference
25456 to @code{ADALIB} units with the same name (@code{TEXT_IO},
25457 @code{SEQUENTIAL_IO}, and @code{DIRECT_IO}, for example).
25458
25459 @menu
25460 * Changes to DECLIB::
25461 @end menu
25462
25463 @node Changes to DECLIB
25464 @subsection Changes to @code{DECLIB}
25465
25466 @noindent
25467 The changes made to the HP Ada predefined library for GNAT and post-Ada 83
25468 compatibility are minor and include the following:
25469
25470 @itemize @bullet
25471 @item Adjusting the location of pragmas and record representation
25472 clauses to obey Ada 95 (and thus Ada 2005) rules
25473
25474 @item Adding the proper notation to generic formal parameters
25475 that take unconstrained types in instantiation
25476
25477 @item Adding pragma @code{ELABORATE_BODY} to package specs
25478 that have package bodies not otherwise allowed
25479
25480 @item Replacing occurrences of the identifier ``@code{PROTECTED}'' by
25481 ``@code{PROTECTD}''.
25482 Currently these are found only in the @code{STARLET} package spec.
25483
25484 @item Changing @code{SYSTEM.ADDRESS} to @code{SYSTEM.SHORT_ADDRESS}
25485 where the address size is constrained to 32 bits.
25486 @end itemize
25487
25488 @noindent
25489 None of the above changes is visible to users.
25490
25491 @node Bindings
25492 @section Bindings
25493
25494 @noindent
25495 On OpenVMS Alpha, HP Ada provides the following strongly-typed bindings:
25496 @itemize @bullet
25497
25498 @item Command Language Interpreter (CLI interface)
25499
25500 @item DECtalk Run-Time Library (DTK interface)
25501
25502 @item Librarian utility routines (LBR interface)
25503
25504 @item General Purpose Run-Time Library (LIB interface)
25505
25506 @item Math Run-Time Library (MTH interface)
25507
25508 @item National Character Set Run-Time Library (NCS interface)
25509
25510 @item Compiled Code Support Run-Time Library (OTS interface)
25511
25512 @item Parallel Processing Run-Time Library (PPL interface)
25513
25514 @item Screen Management Run-Time Library (SMG interface)
25515
25516 @item Sort Run-Time Library (SOR interface)
25517
25518 @item String Run-Time Library (STR interface)
25519
25520 @item STARLET System Library
25521 @findex Starlet
25522
25523 @item X Window System Version 11R4 and 11R5 (X, XLIB interface)
25524
25525 @item X Windows Toolkit (XT interface)
25526
25527 @item X/Motif Version 1.1.3 and 1.2 (XM interface)
25528 @end itemize
25529
25530 @noindent
25531 GNAT provides implementations of these HP bindings in the @code{DECLIB}
25532 directory, on both the Alpha and I64 OpenVMS platforms.
25533
25534 The X/Motif bindings used to build @code{DECLIB} are whatever versions are
25535 in the
25536 HP Ada @file{ADA$PREDEFINED} directory with extension @file{.ADC}.
25537 A pragma @code{Linker_Options} has been added to packages @code{Xm},
25538 @code{Xt}, and @code{X_Lib}
25539 causing the default X/Motif sharable image libraries to be linked in. This
25540 is done via options files named @file{xm.opt}, @file{xt.opt}, and
25541 @file{x_lib.opt} (also located in the @file{DECLIB} directory).
25542
25543 It may be necessary to edit these options files to update or correct the
25544 library names if, for example, the newer X/Motif bindings from
25545 @file{ADA$EXAMPLES}
25546 had been (previous to installing GNAT) copied and renamed to supersede the
25547 default @file{ADA$PREDEFINED} versions.
25548
25549 @menu
25550 * Shared Libraries and Options Files::
25551 * Interfaces to C::
25552 @end menu
25553
25554 @node Shared Libraries and Options Files
25555 @subsection Shared Libraries and Options Files
25556
25557 @noindent
25558 When using the HP Ada
25559 predefined X and Motif bindings, the linking with their sharable images is
25560 done automatically by @command{GNAT LINK}.
25561 When using other X and Motif bindings, you need
25562 to add the corresponding sharable images to the command line for
25563 @code{GNAT LINK}. When linking with shared libraries, or with
25564 @file{.OPT} files, you must
25565 also add them to the command line for @command{GNAT LINK}.
25566
25567 A shared library to be used with GNAT is built in the same way as other
25568 libraries under VMS. The VMS Link command can be used in standard fashion.
25569
25570 @node Interfaces to C
25571 @subsection Interfaces to C
25572
25573 @noindent
25574 HP Ada
25575 provides the following Ada types and operations:
25576
25577 @itemize @bullet
25578 @item C types package (@code{C_TYPES})
25579
25580 @item C strings (@code{C_TYPES.NULL_TERMINATED})
25581
25582 @item Other_types (@code{SHORT_INT})
25583 @end itemize
25584
25585 @noindent
25586 Interfacing to C with GNAT, you can use the above approach
25587 described for HP Ada or the facilities of Annex B of
25588 the @cite{Ada Reference Manual} (packages @code{INTERFACES.C},
25589 @code{INTERFACES.C.STRINGS} and @code{INTERFACES.C.POINTERS}). For more
25590 information, see @ref{Interfacing to C,,, gnat_rm, GNAT Reference Manual}.
25591
25592 The @option{-gnatF} qualifier forces default and explicit
25593 @code{External_Name} parameters in pragmas @code{Import} and @code{Export}
25594 to be uppercased for compatibility with the default behavior
25595 of HP C. The qualifier has no effect on @code{Link_Name} parameters.
25596
25597 @node Main Program Definition
25598 @section Main Program Definition
25599
25600 @noindent
25601 The following section discusses differences in the
25602 definition of main programs on HP Ada and GNAT.
25603 On HP Ada, main programs are defined to meet the
25604 following conditions:
25605 @itemize @bullet
25606 @item Procedure with no formal parameters (returns @code{0} upon
25607 normal completion)
25608
25609 @item Procedure with no formal parameters (returns @code{42} when
25610 an unhandled exception is raised)
25611
25612 @item Function with no formal parameters whose returned value
25613 is of a discrete type
25614
25615 @item Procedure with one @code{out} formal of a discrete type for
25616 which a specification of pragma @code{EXPORT_VALUED_PROCEDURE} is given.
25617
25618 @end itemize
25619
25620 @noindent
25621 When declared with the pragma @code{EXPORT_VALUED_PROCEDURE},
25622 a main function or main procedure returns a discrete
25623 value whose size is less than 64 bits (32 on VAX systems),
25624 the value is zero- or sign-extended as appropriate.
25625 On GNAT, main programs are defined as follows:
25626 @itemize @bullet
25627 @item Must be a non-generic, parameterless subprogram that
25628 is either a procedure or function returning an Ada
25629 @code{STANDARD.INTEGER} (the predefined type)
25630
25631 @item Cannot be a generic subprogram or an instantiation of a
25632 generic subprogram
25633 @end itemize
25634
25635 @node Implementation-Defined Attributes
25636 @section Implementation-Defined Attributes
25637
25638 @noindent
25639 GNAT provides all HP Ada implementation-defined
25640 attributes.
25641
25642 @node Compiler and Run-Time Interfacing
25643 @section Compiler and Run-Time Interfacing
25644
25645 @noindent
25646 HP Ada provides the following qualifiers to pass options to the linker
25647 (ACS LINK):
25648 @itemize @bullet
25649 @item @option{/WAIT} and @option{/SUBMIT}
25650
25651 @item @option{/COMMAND}
25652
25653 @item @option{/@r{[}NO@r{]}MAP}
25654
25655 @item @option{/OUTPUT=@var{file-spec}}
25656
25657 @item @option{/@r{[}NO@r{]}DEBUG} and @option{/@r{[}NO@r{]}TRACEBACK}
25658 @end itemize
25659
25660 @noindent
25661 To pass options to the linker, GNAT provides the following
25662 switches:
25663
25664 @itemize @bullet
25665 @item @option{/EXECUTABLE=@var{exec-name}}
25666
25667 @item @option{/VERBOSE}
25668
25669 @item @option{/@r{[}NO@r{]}DEBUG} and @option{/@r{[}NO@r{]}TRACEBACK}
25670 @end itemize
25671
25672 @noindent
25673 For more information on these switches, see
25674 @ref{Switches for gnatlink}.
25675 In HP Ada, the command-line switch @option{/OPTIMIZE} is available
25676 to control optimization. HP Ada also supplies the
25677 following pragmas:
25678 @itemize @bullet
25679 @item @code{OPTIMIZE}
25680
25681 @item @code{INLINE}
25682
25683 @item @code{INLINE_GENERIC}
25684
25685 @item @code{SUPPRESS_ALL}
25686
25687 @item @code{PASSIVE}
25688 @end itemize
25689
25690 @noindent
25691 In GNAT, optimization is controlled strictly by command
25692 line parameters, as described in the corresponding section of this guide.
25693 The HP pragmas for control of optimization are
25694 recognized but ignored.
25695
25696 Note that in GNAT, the default is optimization off, whereas in HP Ada
25697 the default is that optimization is turned on.
25698
25699 @node Program Compilation and Library Management
25700 @section Program Compilation and Library Management
25701
25702 @noindent
25703 HP Ada and GNAT provide a comparable set of commands to
25704 build programs. HP Ada also provides a program library,
25705 which is a concept that does not exist on GNAT. Instead,
25706 GNAT provides directories of sources that are compiled as
25707 needed.
25708
25709 The following table summarizes
25710 the HP Ada commands and provides
25711 equivalent GNAT commands. In this table, some GNAT
25712 equivalents reflect the fact that GNAT does not use the
25713 concept of a program library. Instead, it uses a model
25714 in which collections of source and object files are used
25715 in a manner consistent with other languages like C and
25716 Fortran. Therefore, standard system file commands are used
25717 to manipulate these elements. Those GNAT commands are marked with
25718 an asterisk.
25719 Note that, unlike HP Ada, none of the GNAT commands accepts wild cards.
25720
25721 @need 1500
25722 @multitable @columnfractions .35 .65
25723
25724 @item @emph{HP Ada Command}
25725 @tab @emph{GNAT Equivalent / Description}
25726
25727 @item @command{ADA}
25728 @tab @command{GNAT COMPILE}@*
25729 Invokes the compiler to compile one or more Ada source files.
25730
25731 @item @command{ACS ATTACH}@*
25732 @tab [No equivalent]@*
25733 Switches control of terminal from current process running the program
25734 library manager.
25735
25736 @item @command{ACS CHECK}
25737 @tab @command{GNAT MAKE /DEPENDENCY_LIST}@*
25738 Forms the execution closure of one
25739 or more compiled units and checks completeness and currency.
25740
25741 @item @command{ACS COMPILE}
25742 @tab @command{GNAT MAKE /ACTIONS=COMPILE}@*
25743 Forms the execution closure of one or
25744 more specified units, checks completeness and currency,
25745 identifies units that have revised source files, compiles same,
25746 and recompiles units that are or will become obsolete.
25747 Also completes incomplete generic instantiations.
25748
25749 @item @command{ACS COPY FOREIGN}
25750 @tab Copy (*)@*
25751 Copies a foreign object file into the program library as a
25752 library unit body.
25753
25754 @item @command{ACS COPY UNIT}
25755 @tab Copy (*)@*
25756 Copies a compiled unit from one program library to another.
25757
25758 @item @command{ACS CREATE LIBRARY}
25759 @tab Create /directory (*)@*
25760 Creates a program library.
25761
25762 @item @command{ACS CREATE SUBLIBRARY}
25763 @tab Create /directory (*)@*
25764 Creates a program sublibrary.
25765
25766 @item @command{ACS DELETE LIBRARY}
25767 @tab @*
25768 Deletes a program library and its contents.
25769
25770 @item @command{ACS DELETE SUBLIBRARY}
25771 @tab @*
25772 Deletes a program sublibrary and its contents.
25773
25774 @item @command{ACS DELETE UNIT}
25775 @tab Delete file (*)@*
25776 On OpenVMS systems, deletes one or more compiled units from
25777 the current program library.
25778
25779 @item @command{ACS DIRECTORY}
25780 @tab Directory (*)@*
25781 On OpenVMS systems, lists units contained in the current
25782 program library.
25783
25784 @item @command{ACS ENTER FOREIGN}
25785 @tab Copy (*)@*
25786 Allows the import of a foreign body as an Ada library
25787 spec and enters a reference to a pointer.
25788
25789 @item @command{ACS ENTER UNIT}
25790 @tab Copy (*)@*
25791 Enters a reference (pointer) from the current program library to
25792 a unit compiled into another program library.
25793
25794 @item @command{ACS EXIT}
25795 @tab [No equivalent]@*
25796 Exits from the program library manager.
25797
25798 @item @command{ACS EXPORT}
25799 @tab Copy (*)@*
25800 Creates an object file that contains system-specific object code
25801 for one or more units. With GNAT, object files can simply be copied
25802 into the desired directory.
25803
25804 @item @command{ACS EXTRACT SOURCE}
25805 @tab Copy (*)@*
25806 Allows access to the copied source file for each Ada compilation unit
25807
25808 @item @command{ACS HELP}
25809 @tab @command{HELP GNAT}@*
25810 Provides online help.
25811
25812 @item @command{ACS LINK}
25813 @tab @command{GNAT LINK}@*
25814 Links an object file containing Ada units into an executable file.
25815
25816 @item @command{ACS LOAD}
25817 @tab Copy (*)@*
25818 Loads (partially compiles) Ada units into the program library.
25819 Allows loading a program from a collection of files into a library
25820 without knowing the relationship among units.
25821
25822 @item @command{ACS MERGE}
25823 @tab Copy (*)@*
25824 Merges into the current program library, one or more units from
25825 another library where they were modified.
25826
25827 @item @command{ACS RECOMPILE}
25828 @tab @command{GNAT MAKE /ACTIONS=COMPILE}@*
25829 Recompiles from external or copied source files any obsolete
25830 unit in the closure. Also, completes any incomplete generic
25831 instantiations.
25832
25833 @item @command{ACS REENTER}
25834 @tab @command{GNAT MAKE}@*
25835 Reenters current references to units compiled after last entered
25836 with the @command{ACS ENTER UNIT} command.
25837
25838 @item @command{ACS SET LIBRARY}
25839 @tab Set default (*)@*
25840 Defines a program library to be the compilation context as well
25841 as the target library for compiler output and commands in general.
25842
25843 @item @command{ACS SET PRAGMA}
25844 @tab Edit @file{gnat.adc} (*)@*
25845 Redefines specified values of the library characteristics
25846 @code{LONG_ FLOAT}, @code{MEMORY_SIZE}, @code{SYSTEM_NAME},
25847 and @code{Float_Representation}.
25848
25849 @item @command{ACS SET SOURCE}
25850 @tab Define @code{ADA_INCLUDE_PATH} path (*)@*
25851 Defines the source file search list for the @command{ACS COMPILE} command.
25852
25853 @item @command{ACS SHOW LIBRARY}
25854 @tab Directory (*)@*
25855 Lists information about one or more program libraries.
25856
25857 @item @command{ACS SHOW PROGRAM}
25858 @tab [No equivalent]@*
25859 Lists information about the execution closure of one or
25860 more units in the program library.
25861
25862 @item @command{ACS SHOW SOURCE}
25863 @tab Show logical @code{ADA_INCLUDE_PATH}@*
25864 Shows the source file search used when compiling units.
25865
25866 @item @command{ACS SHOW VERSION}
25867 @tab Compile with @option{VERBOSE} option
25868 Displays the version number of the compiler and program library
25869 manager used.
25870
25871 @item @command{ACS SPAWN}
25872 @tab [No equivalent]@*
25873 Creates a subprocess of the current process (same as @command{DCL SPAWN}
25874 command).
25875
25876 @item @command{ACS VERIFY}
25877 @tab [No equivalent]@*
25878 Performs a series of consistency checks on a program library to
25879 determine whether the library structure and library files are in
25880 valid form.
25881 @end multitable
25882
25883 @noindent
25884
25885 @node Input-Output
25886 @section Input-Output
25887
25888 @noindent
25889 On OpenVMS Alpha systems, HP Ada uses OpenVMS Record
25890 Management Services (RMS) to perform operations on
25891 external files.
25892
25893 @noindent
25894 HP Ada and GNAT predefine an identical set of input-
25895 output packages. To make the use of the
25896 generic @code{TEXT_IO} operations more convenient, HP Ada
25897 provides predefined library packages that instantiate the
25898 integer and floating-point operations for the predefined
25899 integer and floating-point types as shown in the following table.
25900
25901 @multitable @columnfractions .45 .55
25902 @item @emph{Package Name} @tab Instantiation
25903
25904 @item @code{INTEGER_TEXT_IO}
25905 @tab @code{INTEGER_IO(INTEGER)}
25906
25907 @item @code{SHORT_INTEGER_TEXT_IO}
25908 @tab @code{INTEGER_IO(SHORT_INTEGER)}
25909
25910 @item @code{SHORT_SHORT_INTEGER_TEXT_IO}
25911 @tab @code{INTEGER_IO(SHORT_SHORT_INTEGER)}
25912
25913 @item @code{FLOAT_TEXT_IO}
25914 @tab @code{FLOAT_IO(FLOAT)}
25915
25916 @item @code{LONG_FLOAT_TEXT_IO}
25917 @tab @code{FLOAT_IO(LONG_FLOAT)}
25918 @end multitable
25919
25920 @noindent
25921 The HP Ada predefined packages and their operations
25922 are implemented using OpenVMS Alpha files and input-output
25923 facilities. HP Ada supports asynchronous input-output on OpenVMS Alpha.
25924 Familiarity with the following is recommended:
25925 @itemize @bullet
25926 @item RMS file organizations and access methods
25927
25928 @item OpenVMS file specifications and directories
25929
25930 @item OpenVMS File Definition Language (FDL)
25931 @end itemize
25932
25933 @noindent
25934 GNAT provides I/O facilities that are completely
25935 compatible with HP Ada. The distribution includes the
25936 standard HP Ada versions of all I/O packages, operating
25937 in a manner compatible with HP Ada. In particular, the
25938 following packages are by default the HP Ada (Ada 83)
25939 versions of these packages rather than the renamings
25940 suggested in Annex J of the Ada Reference Manual:
25941 @itemize @bullet
25942 @item @code{TEXT_IO}
25943
25944 @item @code{SEQUENTIAL_IO}
25945
25946 @item @code{DIRECT_IO}
25947 @end itemize
25948
25949 @noindent
25950 The use of the standard child package syntax (for
25951 example, @code{ADA.TEXT_IO}) retrieves the post-Ada 83 versions of these
25952 packages.
25953 GNAT provides HP-compatible predefined instantiations
25954 of the @code{TEXT_IO} packages, and also
25955 provides the standard predefined instantiations required
25956 by the @cite{Ada Reference Manual}.
25957
25958 For further information on how GNAT interfaces to the file
25959 system or how I/O is implemented in programs written in
25960 mixed languages, see @ref{Implementation of the Standard I/O,,,
25961 gnat_rm, GNAT Reference Manual}.
25962 This chapter covers the following:
25963 @itemize @bullet
25964 @item Standard I/O packages
25965
25966 @item @code{FORM} strings
25967
25968 @item @code{ADA.DIRECT_IO}
25969
25970 @item @code{ADA.SEQUENTIAL_IO}
25971
25972 @item @code{ADA.TEXT_IO}
25973
25974 @item Stream pointer positioning
25975
25976 @item Reading and writing non-regular files
25977
25978 @item @code{GET_IMMEDIATE}
25979
25980 @item Treating @code{TEXT_IO} files as streams
25981
25982 @item Shared files
25983
25984 @item Open modes
25985 @end itemize
25986
25987 @node Implementation Limits
25988 @section Implementation Limits
25989
25990 @noindent
25991 The following table lists implementation limits for HP Ada
25992 and GNAT systems.
25993 @multitable @columnfractions .60 .20 .20
25994 @sp 1
25995 @item @emph{Compilation Parameter}
25996 @tab @emph{HP Ada}
25997 @tab @emph{GNAT}
25998 @sp 1
25999
26000 @item In a subprogram or entry declaration, maximum number of
26001 formal parameters that are of an unconstrained record type
26002 @tab 32
26003 @tab No set limit
26004 @sp 1
26005
26006 @item Maximum identifier length (number of characters)
26007 @tab 255
26008 @tab 32766
26009 @sp 1
26010
26011 @item Maximum number of characters in a source line
26012 @tab 255
26013 @tab 32766
26014 @sp 1
26015
26016 @item Maximum collection size (number of bytes)
26017 @tab 2**31-1
26018 @tab 2**31-1
26019 @sp 1
26020
26021 @item Maximum number of discriminants for a record type
26022 @tab 245
26023 @tab No set limit
26024 @sp 1
26025
26026 @item Maximum number of formal parameters in an entry or
26027 subprogram declaration
26028 @tab 246
26029 @tab No set limit
26030 @sp 1
26031
26032 @item Maximum number of dimensions in an array type
26033 @tab 255
26034 @tab No set limit
26035 @sp 1
26036
26037 @item Maximum number of library units and subunits in a compilation.
26038 @tab 4095
26039 @tab No set limit
26040 @sp 1
26041
26042 @item Maximum number of library units and subunits in an execution.
26043 @tab 16383
26044 @tab No set limit
26045 @sp 1
26046
26047 @item Maximum number of objects declared with the pragma @code{COMMON_OBJECT}
26048 or @code{PSECT_OBJECT}
26049 @tab 32757
26050 @tab No set limit
26051 @sp 1
26052
26053 @item Maximum number of enumeration literals in an enumeration type
26054 definition
26055 @tab 65535
26056 @tab No set limit
26057 @sp 1
26058
26059 @item Maximum number of lines in a source file
26060 @tab 65534
26061 @tab No set limit
26062 @sp 1
26063
26064 @item Maximum number of bits in any object
26065 @tab 2**31-1
26066 @tab 2**31-1
26067 @sp 1
26068
26069 @item Maximum size of the static portion of a stack frame (approximate)
26070 @tab 2**31-1
26071 @tab 2**31-1
26072 @end multitable
26073
26074 @node Tools and Utilities
26075 @section Tools and Utilities
26076
26077 @noindent
26078 The following table lists some of the OpenVMS development tools
26079 available for HP Ada, and the corresponding tools for
26080 use with @value{EDITION} on Alpha and I64 platforms.
26081 Aside from the debugger, all the OpenVMS tools identified are part
26082 of the DECset package.
26083
26084 @iftex
26085 @c Specify table in TeX since Texinfo does a poor job
26086 @tex
26087 \smallskip
26088 \smallskip
26089 \settabs\+Language-Sensitive Editor\quad
26090 &Product with HP Ada\quad
26091 &\cr
26092 \+\it Tool
26093 &\it Product with HP Ada
26094 & \it Product with GNAT Pro\cr
26095 \smallskip
26096 \+Code Management System
26097 &HP CMS
26098 & HP CMS\cr
26099 \smallskip
26100 \+Language-Sensitive Editor
26101 &HP LSE
26102 & emacs or HP LSE (Alpha)\cr
26103 \+
26104 &
26105 & HP LSE (I64)\cr
26106 \smallskip
26107 \+Debugger
26108 &OpenVMS Debug
26109 & gdb (Alpha),\cr
26110 \+
26111 &
26112 & OpenVMS Debug (I64)\cr
26113 \smallskip
26114 \+Source Code Analyzer /
26115 &HP SCA
26116 & GNAT XREF\cr
26117 \+Cross Referencer
26118 &
26119 &\cr
26120 \smallskip
26121 \+Test Manager
26122 &HP Digital Test
26123 & HP DTM\cr
26124 \+
26125 &Manager (DTM)
26126 &\cr
26127 \smallskip
26128 \+Performance and
26129 & HP PCA
26130 & HP PCA\cr
26131 \+Coverage Analyzer
26132 &
26133 &\cr
26134 \smallskip
26135 \+Module Management
26136 & HP MMS
26137 & Not applicable\cr
26138 \+ System
26139 &
26140 &\cr
26141 \smallskip
26142 \smallskip
26143 @end tex
26144 @end iftex
26145
26146 @ifnottex
26147 @c This is the Texinfo version of the table. It renders poorly in pdf, hence
26148 @c the TeX version above for the printed version
26149 @flushleft
26150 @c @multitable @columnfractions .3 .4 .4
26151 @multitable {Source Code Analyzer /}{Tool with HP Ada}{Tool with GNAT Pro}
26152 @item @i{Tool}
26153 @tab @i{Tool with HP Ada}
26154 @tab @i{Tool with @value{EDITION}}
26155 @item Code Management@*System
26156 @tab HP CMS
26157 @tab HP CMS
26158 @item Language-Sensitive@*Editor
26159 @tab HP LSE
26160 @tab emacs or HP LSE (Alpha)
26161 @item
26162 @tab
26163 @tab HP LSE (I64)
26164 @item Debugger
26165 @tab OpenVMS Debug
26166 @tab gdb (Alpha),
26167 @item
26168 @tab
26169 @tab OpenVMS Debug (I64)
26170 @item Source Code Analyzer /@*Cross Referencer
26171 @tab HP SCA
26172 @tab GNAT XREF
26173 @item Test Manager
26174 @tab HP Digital Test@*Manager (DTM)
26175 @tab HP DTM
26176 @item Performance and@*Coverage Analyzer
26177 @tab HP PCA
26178 @tab HP PCA
26179 @item Module Management@*System
26180 @tab HP MMS
26181 @tab Not applicable
26182 @end multitable
26183 @end flushleft
26184 @end ifnottex
26185
26186 @end ifset
26187
26188 @c **************************************
26189 @node Platform-Specific Information for the Run-Time Libraries
26190 @appendix Platform-Specific Information for the Run-Time Libraries
26191 @cindex Tasking and threads libraries
26192 @cindex Threads libraries and tasking
26193 @cindex Run-time libraries (platform-specific information)
26194
26195 @noindent
26196 The GNAT run-time implementation may vary with respect to both the
26197 underlying threads library and the exception handling scheme.
26198 For threads support, one or more of the following are supplied:
26199 @itemize @bullet
26200 @item @b{native threads library}, a binding to the thread package from
26201 the underlying operating system
26202
26203 @item @b{pthreads library} (Sparc Solaris only), a binding to the Solaris
26204 POSIX thread package
26205 @end itemize
26206
26207 @noindent
26208 For exception handling, either or both of two models are supplied:
26209 @itemize @bullet
26210 @item @b{Zero-Cost Exceptions} (``ZCX''),@footnote{
26211 Most programs should experience a substantial speed improvement by
26212 being compiled with a ZCX run-time.
26213 This is especially true for
26214 tasking applications or applications with many exception handlers.}
26215 @cindex Zero-Cost Exceptions
26216 @cindex ZCX (Zero-Cost Exceptions)
26217 which uses binder-generated tables that
26218 are interrogated at run time to locate a handler
26219
26220 @item @b{setjmp / longjmp} (``SJLJ''),
26221 @cindex setjmp/longjmp Exception Model
26222 @cindex SJLJ (setjmp/longjmp Exception Model)
26223 which uses dynamically-set data to establish
26224 the set of handlers
26225 @end itemize
26226
26227 @noindent
26228 This appendix summarizes which combinations of threads and exception support
26229 are supplied on various GNAT platforms.
26230 It then shows how to select a particular library either
26231 permanently or temporarily,
26232 explains the properties of (and tradeoffs among) the various threads
26233 libraries, and provides some additional
26234 information about several specific platforms.
26235
26236 @menu
26237 * Summary of Run-Time Configurations::
26238 * Specifying a Run-Time Library::
26239 * Choosing the Scheduling Policy::
26240 * Solaris-Specific Considerations::
26241 * Linux-Specific Considerations::
26242 * AIX-Specific Considerations::
26243 * Irix-Specific Considerations::
26244 * RTX-Specific Considerations::
26245 @end menu
26246
26247 @node Summary of Run-Time Configurations
26248 @section Summary of Run-Time Configurations
26249
26250 @multitable @columnfractions .30 .70
26251 @item @b{alpha-openvms}
26252 @item @code{@ @ }@i{rts-native (default)}
26253 @item @code{@ @ @ @ }Tasking @tab native VMS threads
26254 @item @code{@ @ @ @ }Exceptions @tab ZCX
26255 @*
26256 @item @b{alpha-tru64}
26257 @item @code{@ @ }@i{rts-native (default)}
26258 @item @code{@ @ @ @ }Tasking @tab native TRU64 threads
26259 @item @code{@ @ @ @ }Exceptions @tab ZCX
26260 @*
26261 @item @code{@ @ }@i{rts-sjlj}
26262 @item @code{@ @ @ @ }Tasking @tab native TRU64 threads
26263 @item @code{@ @ @ @ }Exceptions @tab SJLJ
26264 @*
26265 @item @b{ia64-hp_linux}
26266 @item @code{@ @ }@i{rts-native (default)}
26267 @item @code{@ @ @ @ }Tasking @tab pthread library
26268 @item @code{@ @ @ @ }Exceptions @tab ZCX
26269 @*
26270 @item @b{ia64-hpux}
26271 @item @code{@ @ }@i{rts-native (default)}
26272 @item @code{@ @ @ @ }Tasking @tab native HP-UX threads
26273 @item @code{@ @ @ @ }Exceptions @tab SJLJ
26274 @*
26275 @item @b{ia64-openvms}
26276 @item @code{@ @ }@i{rts-native (default)}
26277 @item @code{@ @ @ @ }Tasking @tab native VMS threads
26278 @item @code{@ @ @ @ }Exceptions @tab ZCX
26279 @*
26280 @item @b{ia64-sgi_linux}
26281 @item @code{@ @ }@i{rts-native (default)}
26282 @item @code{@ @ @ @ }Tasking @tab pthread library
26283 @item @code{@ @ @ @ }Exceptions @tab ZCX
26284 @*
26285 @item @b{mips-irix}
26286 @item @code{@ @ }@i{rts-native (default)}
26287 @item @code{@ @ @ @ }Tasking @tab native IRIX threads
26288 @item @code{@ @ @ @ }Exceptions @tab ZCX
26289 @*
26290 @item @b{pa-hpux}
26291 @item @code{@ @ }@i{rts-native (default)}
26292 @item @code{@ @ @ @ }Tasking @tab native HP-UX threads
26293 @item @code{@ @ @ @ }Exceptions @tab ZCX
26294 @*
26295 @item @code{@ @ }@i{rts-sjlj}
26296 @item @code{@ @ @ @ }Tasking @tab native HP-UX threads
26297 @item @code{@ @ @ @ }Exceptions @tab SJLJ
26298 @*
26299 @item @b{ppc-aix}
26300 @item @code{@ @ }@i{rts-native (default)}
26301 @item @code{@ @ @ @ }Tasking @tab native AIX threads
26302 @item @code{@ @ @ @ }Exceptions @tab SJLJ
26303 @*
26304 @item @b{ppc-darwin}
26305 @item @code{@ @ }@i{rts-native (default)}
26306 @item @code{@ @ @ @ }Tasking @tab native MacOS threads
26307 @item @code{@ @ @ @ }Exceptions @tab ZCX
26308 @*
26309 @item @b{sparc-solaris} @tab
26310 @item @code{@ @ }@i{rts-native (default)}
26311 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
26312 @item @code{@ @ @ @ }Exceptions @tab ZCX
26313 @*
26314 @item @code{@ @ }@i{rts-pthread}
26315 @item @code{@ @ @ @ }Tasking @tab pthread library
26316 @item @code{@ @ @ @ }Exceptions @tab ZCX
26317 @*
26318 @item @code{@ @ }@i{rts-sjlj}
26319 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
26320 @item @code{@ @ @ @ }Exceptions @tab SJLJ
26321 @*
26322 @item @b{sparc64-solaris} @tab
26323 @item @code{@ @ }@i{rts-native (default)}
26324 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
26325 @item @code{@ @ @ @ }Exceptions @tab ZCX
26326 @*
26327 @item @b{x86-linux}
26328 @item @code{@ @ }@i{rts-native (default)}
26329 @item @code{@ @ @ @ }Tasking @tab pthread library
26330 @item @code{@ @ @ @ }Exceptions @tab ZCX
26331 @*
26332 @item @code{@ @ }@i{rts-sjlj}
26333 @item @code{@ @ @ @ }Tasking @tab pthread library
26334 @item @code{@ @ @ @ }Exceptions @tab SJLJ
26335 @*
26336 @item @b{x86-lynx}
26337 @item @code{@ @ }@i{rts-native (default)}
26338 @item @code{@ @ @ @ }Tasking @tab native LynxOS threads
26339 @item @code{@ @ @ @ }Exceptions @tab SJLJ
26340 @*
26341 @item @b{x86-solaris}
26342 @item @code{@ @ }@i{rts-native (default)}
26343 @item @code{@ @ @ @ }Tasking @tab native Solaris threads
26344 @item @code{@ @ @ @ }Exceptions @tab SJLJ
26345 @*
26346 @item @b{x86-windows}
26347 @item @code{@ @ }@i{rts-native (default)}
26348 @item @code{@ @ @ @ }Tasking @tab native Win32 threads
26349 @item @code{@ @ @ @ }Exceptions @tab ZCX
26350 @*
26351 @item @code{@ @ }@i{rts-sjlj (default)}
26352 @item @code{@ @ @ @ }Tasking @tab native Win32 threads
26353 @item @code{@ @ @ @ }Exceptions @tab SJLJ
26354 @*
26355 @item @b{x86-windows-rtx}
26356 @item @code{@ @ }@i{rts-rtx-rtss (default)}
26357 @item @code{@ @ @ @ }Tasking @tab RTX real-time subsystem RTSS threads (kernel mode)
26358 @item @code{@ @ @ @ }Exceptions @tab SJLJ
26359 @*
26360 @item @code{@ @ }@i{rts-rtx-w32}
26361 @item @code{@ @ @ @ }Tasking @tab RTX Win32 threads (user mode)
26362 @item @code{@ @ @ @ }Exceptions @tab ZCX
26363 @*
26364 @item @b{x86_64-linux}
26365 @item @code{@ @ }@i{rts-native (default)}
26366 @item @code{@ @ @ @ }Tasking @tab pthread library
26367 @item @code{@ @ @ @ }Exceptions @tab ZCX
26368 @*
26369 @item @code{@ @ }@i{rts-sjlj}
26370 @item @code{@ @ @ @ }Tasking @tab pthread library
26371 @item @code{@ @ @ @ }Exceptions @tab SJLJ
26372 @*
26373 @end multitable
26374
26375 @node Specifying a Run-Time Library
26376 @section Specifying a Run-Time Library
26377
26378 @noindent
26379 The @file{adainclude} subdirectory containing the sources of the GNAT
26380 run-time library, and the @file{adalib} subdirectory containing the
26381 @file{ALI} files and the static and/or shared GNAT library, are located
26382 in the gcc target-dependent area:
26383
26384 @smallexample
26385 target=$prefix/lib/gcc/gcc-@i{dumpmachine}/gcc-@i{dumpversion}/
26386 @end smallexample
26387
26388 @noindent
26389 As indicated above, on some platforms several run-time libraries are supplied.
26390 These libraries are installed in the target dependent area and
26391 contain a complete source and binary subdirectory. The detailed description
26392 below explains the differences between the different libraries in terms of
26393 their thread support.
26394
26395 The default run-time library (when GNAT is installed) is @emph{rts-native}.
26396 This default run time is selected by the means of soft links.
26397 For example on x86-linux:
26398
26399 @smallexample
26400 @group
26401 $(target-dir)
26402 |
26403 +--- adainclude----------+
26404 | |
26405 +--- adalib-----------+ |
26406 | | |
26407 +--- rts-native | |
26408 | | | |
26409 | +--- adainclude <---+
26410 | | |
26411 | +--- adalib <----+
26412 |
26413 +--- rts-sjlj
26414 |
26415 +--- adainclude
26416 |
26417 +--- adalib
26418 @end group
26419 @end smallexample
26420
26421 @noindent
26422 If the @i{rts-sjlj} library is to be selected on a permanent basis,
26423 these soft links can be modified with the following commands:
26424
26425 @smallexample
26426 $ cd $target
26427 $ rm -f adainclude adalib
26428 $ ln -s rts-sjlj/adainclude adainclude
26429 $ ln -s rts-sjlj/adalib adalib
26430 @end smallexample
26431
26432 @noindent
26433 Alternatively, you can specify @file{rts-sjlj/adainclude} in the file
26434 @file{$target/ada_source_path} and @file{rts-sjlj/adalib} in
26435 @file{$target/ada_object_path}.
26436
26437 Selecting another run-time library temporarily can be
26438 achieved by using the @option{--RTS} switch, e.g., @option{--RTS=sjlj}
26439 @cindex @option{--RTS} option
26440
26441 @node Choosing the Scheduling Policy
26442 @section Choosing the Scheduling Policy
26443
26444 @noindent
26445 When using a POSIX threads implementation, you have a choice of several
26446 scheduling policies: @code{SCHED_FIFO},
26447 @cindex @code{SCHED_FIFO} scheduling policy
26448 @code{SCHED_RR}
26449 @cindex @code{SCHED_RR} scheduling policy
26450 and @code{SCHED_OTHER}.
26451 @cindex @code{SCHED_OTHER} scheduling policy
26452 Typically, the default is @code{SCHED_OTHER}, while using @code{SCHED_FIFO}
26453 or @code{SCHED_RR} requires special (e.g., root) privileges.
26454
26455 By default, GNAT uses the @code{SCHED_OTHER} policy. To specify
26456 @code{SCHED_FIFO},
26457 @cindex @code{SCHED_FIFO} scheduling policy
26458 you can use one of the following:
26459
26460 @itemize @bullet
26461 @item
26462 @code{pragma Time_Slice (0.0)}
26463 @cindex pragma Time_Slice
26464 @item
26465 the corresponding binder option @option{-T0}
26466 @cindex @option{-T0} option
26467 @item
26468 @code{pragma Task_Dispatching_Policy (FIFO_Within_Priorities)}
26469 @cindex pragma Task_Dispatching_Policy
26470 @end itemize
26471
26472 @noindent
26473 To specify @code{SCHED_RR},
26474 @cindex @code{SCHED_RR} scheduling policy
26475 you should use @code{pragma Time_Slice} with a
26476 value greater than @code{0.0}, or else use the corresponding @option{-T}
26477 binder option.
26478
26479 @node Solaris-Specific Considerations
26480 @section Solaris-Specific Considerations
26481 @cindex Solaris Sparc threads libraries
26482
26483 @noindent
26484 This section addresses some topics related to the various threads libraries
26485 on Sparc Solaris.
26486
26487 @menu
26488 * Solaris Threads Issues::
26489 @end menu
26490
26491 @node Solaris Threads Issues
26492 @subsection Solaris Threads Issues
26493
26494 @noindent
26495 GNAT under Solaris/Sparc 32 bits comes with an alternate tasking run-time
26496 library based on POSIX threads --- @emph{rts-pthread}.
26497 @cindex rts-pthread threads library
26498 This run-time library has the advantage of being mostly shared across all
26499 POSIX-compliant thread implementations, and it also provides under
26500 @w{Solaris 8} the @code{PTHREAD_PRIO_INHERIT}
26501 @cindex @code{PTHREAD_PRIO_INHERIT} policy (under rts-pthread)
26502 and @code{PTHREAD_PRIO_PROTECT}
26503 @cindex @code{PTHREAD_PRIO_PROTECT} policy (under rts-pthread)
26504 semantics that can be selected using the predefined pragma
26505 @code{Locking_Policy}
26506 @cindex pragma Locking_Policy (under rts-pthread)
26507 with respectively
26508 @code{Inheritance_Locking} and @code{Ceiling_Locking} as the policy.
26509 @cindex @code{Inheritance_Locking} (under rts-pthread)
26510 @cindex @code{Ceiling_Locking} (under rts-pthread)
26511
26512 As explained above, the native run-time library is based on the Solaris thread
26513 library (@code{libthread}) and is the default library.
26514
26515 When the Solaris threads library is used (this is the default), programs
26516 compiled with GNAT can automatically take advantage of
26517 and can thus execute on multiple processors.
26518 The user can alternatively specify a processor on which the program should run
26519 to emulate a single-processor system. The multiprocessor / uniprocessor choice
26520 is made by
26521 setting the environment variable @env{GNAT_PROCESSOR}
26522 @cindex @env{GNAT_PROCESSOR} environment variable (on Sparc Solaris)
26523 to one of the following:
26524
26525 @table @code
26526 @item -2
26527 Use the default configuration (run the program on all
26528 available processors) - this is the same as having @code{GNAT_PROCESSOR}
26529 unset
26530
26531 @item -1
26532 Let the run-time implementation choose one processor and run the program on
26533 that processor
26534
26535 @item 0 .. Last_Proc
26536 Run the program on the specified processor.
26537 @code{Last_Proc} is equal to @code{_SC_NPROCESSORS_CONF - 1}
26538 (where @code{_SC_NPROCESSORS_CONF} is a system variable).
26539 @end table
26540
26541 @node Linux-Specific Considerations
26542 @section Linux-Specific Considerations
26543 @cindex Linux threads libraries
26544
26545 @noindent
26546 On GNU/Linux without NPTL support (usually system with GNU C Library
26547 older than 2.3), the signal model is not POSIX compliant, which means
26548 that to send a signal to the process, you need to send the signal to all
26549 threads, e.g.@: by using @code{killpg()}.
26550
26551 @node AIX-Specific Considerations
26552 @section AIX-Specific Considerations
26553 @cindex AIX resolver library
26554
26555 @noindent
26556 On AIX, the resolver library initializes some internal structure on
26557 the first call to @code{get*by*} functions, which are used to implement
26558 @code{GNAT.Sockets.Get_Host_By_Name} and
26559 @code{GNAT.Sockets.Get_Host_By_Address}.
26560 If such initialization occurs within an Ada task, and the stack size for
26561 the task is the default size, a stack overflow may occur.
26562
26563 To avoid this overflow, the user should either ensure that the first call
26564 to @code{GNAT.Sockets.Get_Host_By_Name} or
26565 @code{GNAT.Sockets.Get_Host_By_Addrss}
26566 occurs in the environment task, or use @code{pragma Storage_Size} to
26567 specify a sufficiently large size for the stack of the task that contains
26568 this call.
26569
26570 @node Irix-Specific Considerations
26571 @section Irix-Specific Considerations
26572 @cindex Irix libraries
26573
26574 @noindent
26575 The GCC support libraries coming with the Irix compiler have moved to
26576 their canonical place with respect to the general Irix ABI related
26577 conventions. Running applications built with the default shared GNAT
26578 run-time now requires the LD_LIBRARY_PATH environment variable to
26579 include this location. A possible way to achieve this is to issue the
26580 following command line on a bash prompt:
26581
26582 @smallexample
26583 @group
26584 $ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`dirname \`gcc --print-file-name=libgcc_s.so\``
26585 @end group
26586 @end smallexample
26587
26588 @node RTX-Specific Considerations
26589 @section RTX-Specific Considerations
26590 @cindex RTX libraries
26591
26592 @noindent
26593 The Real-time Extension (RTX) to Windows is based on the Windows Win32
26594 API. Applications can be built to work in two different modes:
26595
26596 @itemize @bullet
26597 @item
26598 Windows executables that run in Ring 3 to utilize memory protection
26599 (@emph{rts-rtx-w32}).
26600
26601 @item
26602 Real-time subsystem (RTSS) executables that run in Ring 0, where
26603 performance can be optimized with RTSS applications taking precedent
26604 over all Windows applications (@emph{rts-rtx-rtss}).
26605
26606 @end itemize
26607
26608 @c *******************************
26609 @node Example of Binder Output File
26610 @appendix Example of Binder Output File
26611
26612 @noindent
26613 This Appendix displays the source code for @command{gnatbind}'s output
26614 file generated for a simple ``Hello World'' program.
26615 Comments have been added for clarification purposes.
26616
26617 @smallexample @c adanocomment
26618 @iftex
26619 @leftskip=0cm
26620 @end iftex
26621 -- The package is called Ada_Main unless this name is actually used
26622 -- as a unit name in the partition, in which case some other unique
26623 -- name is used.
26624
26625 with System;
26626 package ada_main is
26627
26628 Elab_Final_Code : Integer;
26629 pragma Import (C, Elab_Final_Code, "__gnat_inside_elab_final_code");
26630
26631 -- The main program saves the parameters (argument count,
26632 -- argument values, environment pointer) in global variables
26633 -- for later access by other units including
26634 -- Ada.Command_Line.
26635
26636 gnat_argc : Integer;
26637 gnat_argv : System.Address;
26638 gnat_envp : System.Address;
26639
26640 -- The actual variables are stored in a library routine. This
26641 -- is useful for some shared library situations, where there
26642 -- are problems if variables are not in the library.
26643
26644 pragma Import (C, gnat_argc);
26645 pragma Import (C, gnat_argv);
26646 pragma Import (C, gnat_envp);
26647
26648 -- The exit status is similarly an external location
26649
26650 gnat_exit_status : Integer;
26651 pragma Import (C, gnat_exit_status);
26652
26653 GNAT_Version : constant String :=
26654 "GNAT Version: 6.0.0w (20061115)";
26655 pragma Export (C, GNAT_Version, "__gnat_version");
26656
26657 -- This is the generated adafinal routine that performs
26658 -- finalization at the end of execution. In the case where
26659 -- Ada is the main program, this main program makes a call
26660 -- to adafinal at program termination.
26661
26662 procedure adafinal;
26663 pragma Export (C, adafinal, "adafinal");
26664
26665 -- This is the generated adainit routine that performs
26666 -- initialization at the start of execution. In the case
26667 -- where Ada is the main program, this main program makes
26668 -- a call to adainit at program startup.
26669
26670 procedure adainit;
26671 pragma Export (C, adainit, "adainit");
26672
26673 -- This routine is called at the start of execution. It is
26674 -- a dummy routine that is used by the debugger to breakpoint
26675 -- at the start of execution.
26676
26677 procedure Break_Start;
26678 pragma Import (C, Break_Start, "__gnat_break_start");
26679
26680 -- This is the actual generated main program (it would be
26681 -- suppressed if the no main program switch were used). As
26682 -- required by standard system conventions, this program has
26683 -- the external name main.
26684
26685 function main
26686 (argc : Integer;
26687 argv : System.Address;
26688 envp : System.Address)
26689 return Integer;
26690 pragma Export (C, main, "main");
26691
26692 -- The following set of constants give the version
26693 -- identification values for every unit in the bound
26694 -- partition. This identification is computed from all
26695 -- dependent semantic units, and corresponds to the
26696 -- string that would be returned by use of the
26697 -- Body_Version or Version attributes.
26698
26699 type Version_32 is mod 2 ** 32;
26700 u00001 : constant Version_32 := 16#7880BEB3#;
26701 u00002 : constant Version_32 := 16#0D24CBD0#;
26702 u00003 : constant Version_32 := 16#3283DBEB#;
26703 u00004 : constant Version_32 := 16#2359F9ED#;
26704 u00005 : constant Version_32 := 16#664FB847#;
26705 u00006 : constant Version_32 := 16#68E803DF#;
26706 u00007 : constant Version_32 := 16#5572E604#;
26707 u00008 : constant Version_32 := 16#46B173D8#;
26708 u00009 : constant Version_32 := 16#156A40CF#;
26709 u00010 : constant Version_32 := 16#033DABE0#;
26710 u00011 : constant Version_32 := 16#6AB38FEA#;
26711 u00012 : constant Version_32 := 16#22B6217D#;
26712 u00013 : constant Version_32 := 16#68A22947#;
26713 u00014 : constant Version_32 := 16#18CC4A56#;
26714 u00015 : constant Version_32 := 16#08258E1B#;
26715 u00016 : constant Version_32 := 16#367D5222#;
26716 u00017 : constant Version_32 := 16#20C9ECA4#;
26717 u00018 : constant Version_32 := 16#50D32CB6#;
26718 u00019 : constant Version_32 := 16#39A8BB77#;
26719 u00020 : constant Version_32 := 16#5CF8FA2B#;
26720 u00021 : constant Version_32 := 16#2F1EB794#;
26721 u00022 : constant Version_32 := 16#31AB6444#;
26722 u00023 : constant Version_32 := 16#1574B6E9#;
26723 u00024 : constant Version_32 := 16#5109C189#;
26724 u00025 : constant Version_32 := 16#56D770CD#;
26725 u00026 : constant Version_32 := 16#02F9DE3D#;
26726 u00027 : constant Version_32 := 16#08AB6B2C#;
26727 u00028 : constant Version_32 := 16#3FA37670#;
26728 u00029 : constant Version_32 := 16#476457A0#;
26729 u00030 : constant Version_32 := 16#731E1B6E#;
26730 u00031 : constant Version_32 := 16#23C2E789#;
26731 u00032 : constant Version_32 := 16#0F1BD6A1#;
26732 u00033 : constant Version_32 := 16#7C25DE96#;
26733 u00034 : constant Version_32 := 16#39ADFFA2#;
26734 u00035 : constant Version_32 := 16#571DE3E7#;
26735 u00036 : constant Version_32 := 16#5EB646AB#;
26736 u00037 : constant Version_32 := 16#4249379B#;
26737 u00038 : constant Version_32 := 16#0357E00A#;
26738 u00039 : constant Version_32 := 16#3784FB72#;
26739 u00040 : constant Version_32 := 16#2E723019#;
26740 u00041 : constant Version_32 := 16#623358EA#;
26741 u00042 : constant Version_32 := 16#107F9465#;
26742 u00043 : constant Version_32 := 16#6843F68A#;
26743 u00044 : constant Version_32 := 16#63305874#;
26744 u00045 : constant Version_32 := 16#31E56CE1#;
26745 u00046 : constant Version_32 := 16#02917970#;
26746 u00047 : constant Version_32 := 16#6CCBA70E#;
26747 u00048 : constant Version_32 := 16#41CD4204#;
26748 u00049 : constant Version_32 := 16#572E3F58#;
26749 u00050 : constant Version_32 := 16#20729FF5#;
26750 u00051 : constant Version_32 := 16#1D4F93E8#;
26751 u00052 : constant Version_32 := 16#30B2EC3D#;
26752 u00053 : constant Version_32 := 16#34054F96#;
26753 u00054 : constant Version_32 := 16#5A199860#;
26754 u00055 : constant Version_32 := 16#0E7F912B#;
26755 u00056 : constant Version_32 := 16#5760634A#;
26756 u00057 : constant Version_32 := 16#5D851835#;
26757
26758 -- The following Export pragmas export the version numbers
26759 -- with symbolic names ending in B (for body) or S
26760 -- (for spec) so that they can be located in a link. The
26761 -- information provided here is sufficient to track down
26762 -- the exact versions of units used in a given build.
26763
26764 pragma Export (C, u00001, "helloB");
26765 pragma Export (C, u00002, "system__standard_libraryB");
26766 pragma Export (C, u00003, "system__standard_libraryS");
26767 pragma Export (C, u00004, "adaS");
26768 pragma Export (C, u00005, "ada__text_ioB");
26769 pragma Export (C, u00006, "ada__text_ioS");
26770 pragma Export (C, u00007, "ada__exceptionsB");
26771 pragma Export (C, u00008, "ada__exceptionsS");
26772 pragma Export (C, u00009, "gnatS");
26773 pragma Export (C, u00010, "gnat__heap_sort_aB");
26774 pragma Export (C, u00011, "gnat__heap_sort_aS");
26775 pragma Export (C, u00012, "systemS");
26776 pragma Export (C, u00013, "system__exception_tableB");
26777 pragma Export (C, u00014, "system__exception_tableS");
26778 pragma Export (C, u00015, "gnat__htableB");
26779 pragma Export (C, u00016, "gnat__htableS");
26780 pragma Export (C, u00017, "system__exceptionsS");
26781 pragma Export (C, u00018, "system__machine_state_operationsB");
26782 pragma Export (C, u00019, "system__machine_state_operationsS");
26783 pragma Export (C, u00020, "system__machine_codeS");
26784 pragma Export (C, u00021, "system__storage_elementsB");
26785 pragma Export (C, u00022, "system__storage_elementsS");
26786 pragma Export (C, u00023, "system__secondary_stackB");
26787 pragma Export (C, u00024, "system__secondary_stackS");
26788 pragma Export (C, u00025, "system__parametersB");
26789 pragma Export (C, u00026, "system__parametersS");
26790 pragma Export (C, u00027, "system__soft_linksB");
26791 pragma Export (C, u00028, "system__soft_linksS");
26792 pragma Export (C, u00029, "system__stack_checkingB");
26793 pragma Export (C, u00030, "system__stack_checkingS");
26794 pragma Export (C, u00031, "system__tracebackB");
26795 pragma Export (C, u00032, "system__tracebackS");
26796 pragma Export (C, u00033, "ada__streamsS");
26797 pragma Export (C, u00034, "ada__tagsB");
26798 pragma Export (C, u00035, "ada__tagsS");
26799 pragma Export (C, u00036, "system__string_opsB");
26800 pragma Export (C, u00037, "system__string_opsS");
26801 pragma Export (C, u00038, "interfacesS");
26802 pragma Export (C, u00039, "interfaces__c_streamsB");
26803 pragma Export (C, u00040, "interfaces__c_streamsS");
26804 pragma Export (C, u00041, "system__file_ioB");
26805 pragma Export (C, u00042, "system__file_ioS");
26806 pragma Export (C, u00043, "ada__finalizationB");
26807 pragma Export (C, u00044, "ada__finalizationS");
26808 pragma Export (C, u00045, "system__finalization_rootB");
26809 pragma Export (C, u00046, "system__finalization_rootS");
26810 pragma Export (C, u00047, "system__finalization_implementationB");
26811 pragma Export (C, u00048, "system__finalization_implementationS");
26812 pragma Export (C, u00049, "system__string_ops_concat_3B");
26813 pragma Export (C, u00050, "system__string_ops_concat_3S");
26814 pragma Export (C, u00051, "system__stream_attributesB");
26815 pragma Export (C, u00052, "system__stream_attributesS");
26816 pragma Export (C, u00053, "ada__io_exceptionsS");
26817 pragma Export (C, u00054, "system__unsigned_typesS");
26818 pragma Export (C, u00055, "system__file_control_blockS");
26819 pragma Export (C, u00056, "ada__finalization__list_controllerB");
26820 pragma Export (C, u00057, "ada__finalization__list_controllerS");
26821
26822 -- BEGIN ELABORATION ORDER
26823 -- ada (spec)
26824 -- gnat (spec)
26825 -- gnat.heap_sort_a (spec)
26826 -- gnat.heap_sort_a (body)
26827 -- gnat.htable (spec)
26828 -- gnat.htable (body)
26829 -- interfaces (spec)
26830 -- system (spec)
26831 -- system.machine_code (spec)
26832 -- system.parameters (spec)
26833 -- system.parameters (body)
26834 -- interfaces.c_streams (spec)
26835 -- interfaces.c_streams (body)
26836 -- system.standard_library (spec)
26837 -- ada.exceptions (spec)
26838 -- system.exception_table (spec)
26839 -- system.exception_table (body)
26840 -- ada.io_exceptions (spec)
26841 -- system.exceptions (spec)
26842 -- system.storage_elements (spec)
26843 -- system.storage_elements (body)
26844 -- system.machine_state_operations (spec)
26845 -- system.machine_state_operations (body)
26846 -- system.secondary_stack (spec)
26847 -- system.stack_checking (spec)
26848 -- system.soft_links (spec)
26849 -- system.soft_links (body)
26850 -- system.stack_checking (body)
26851 -- system.secondary_stack (body)
26852 -- system.standard_library (body)
26853 -- system.string_ops (spec)
26854 -- system.string_ops (body)
26855 -- ada.tags (spec)
26856 -- ada.tags (body)
26857 -- ada.streams (spec)
26858 -- system.finalization_root (spec)
26859 -- system.finalization_root (body)
26860 -- system.string_ops_concat_3 (spec)
26861 -- system.string_ops_concat_3 (body)
26862 -- system.traceback (spec)
26863 -- system.traceback (body)
26864 -- ada.exceptions (body)
26865 -- system.unsigned_types (spec)
26866 -- system.stream_attributes (spec)
26867 -- system.stream_attributes (body)
26868 -- system.finalization_implementation (spec)
26869 -- system.finalization_implementation (body)
26870 -- ada.finalization (spec)
26871 -- ada.finalization (body)
26872 -- ada.finalization.list_controller (spec)
26873 -- ada.finalization.list_controller (body)
26874 -- system.file_control_block (spec)
26875 -- system.file_io (spec)
26876 -- system.file_io (body)
26877 -- ada.text_io (spec)
26878 -- ada.text_io (body)
26879 -- hello (body)
26880 -- END ELABORATION ORDER
26881
26882 end ada_main;
26883
26884 -- The following source file name pragmas allow the generated file
26885 -- names to be unique for different main programs. They are needed
26886 -- since the package name will always be Ada_Main.
26887
26888 pragma Source_File_Name (ada_main, Spec_File_Name => "b~hello.ads");
26889 pragma Source_File_Name (ada_main, Body_File_Name => "b~hello.adb");
26890
26891 -- Generated package body for Ada_Main starts here
26892
26893 package body ada_main is
26894
26895 -- The actual finalization is performed by calling the
26896 -- library routine in System.Standard_Library.Adafinal
26897
26898 procedure Do_Finalize;
26899 pragma Import (C, Do_Finalize, "system__standard_library__adafinal");
26900
26901 -------------
26902 -- adainit --
26903 -------------
26904
26905 @findex adainit
26906 procedure adainit is
26907
26908 -- These booleans are set to True once the associated unit has
26909 -- been elaborated. It is also used to avoid elaborating the
26910 -- same unit twice.
26911
26912 E040 : Boolean;
26913 pragma Import (Ada, E040, "interfaces__c_streams_E");
26914
26915 E008 : Boolean;
26916 pragma Import (Ada, E008, "ada__exceptions_E");
26917
26918 E014 : Boolean;
26919 pragma Import (Ada, E014, "system__exception_table_E");
26920
26921 E053 : Boolean;
26922 pragma Import (Ada, E053, "ada__io_exceptions_E");
26923
26924 E017 : Boolean;
26925 pragma Import (Ada, E017, "system__exceptions_E");
26926
26927 E024 : Boolean;
26928 pragma Import (Ada, E024, "system__secondary_stack_E");
26929
26930 E030 : Boolean;
26931 pragma Import (Ada, E030, "system__stack_checking_E");
26932
26933 E028 : Boolean;
26934 pragma Import (Ada, E028, "system__soft_links_E");
26935
26936 E035 : Boolean;
26937 pragma Import (Ada, E035, "ada__tags_E");
26938
26939 E033 : Boolean;
26940 pragma Import (Ada, E033, "ada__streams_E");
26941
26942 E046 : Boolean;
26943 pragma Import (Ada, E046, "system__finalization_root_E");
26944
26945 E048 : Boolean;
26946 pragma Import (Ada, E048, "system__finalization_implementation_E");
26947
26948 E044 : Boolean;
26949 pragma Import (Ada, E044, "ada__finalization_E");
26950
26951 E057 : Boolean;
26952 pragma Import (Ada, E057, "ada__finalization__list_controller_E");
26953
26954 E055 : Boolean;
26955 pragma Import (Ada, E055, "system__file_control_block_E");
26956
26957 E042 : Boolean;
26958 pragma Import (Ada, E042, "system__file_io_E");
26959
26960 E006 : Boolean;
26961 pragma Import (Ada, E006, "ada__text_io_E");
26962
26963 -- Set_Globals is a library routine that stores away the
26964 -- value of the indicated set of global values in global
26965 -- variables within the library.
26966
26967 procedure Set_Globals
26968 (Main_Priority : Integer;
26969 Time_Slice_Value : Integer;
26970 WC_Encoding : Character;
26971 Locking_Policy : Character;
26972 Queuing_Policy : Character;
26973 Task_Dispatching_Policy : Character;
26974 Adafinal : System.Address;
26975 Unreserve_All_Interrupts : Integer;
26976 Exception_Tracebacks : Integer);
26977 @findex __gnat_set_globals
26978 pragma Import (C, Set_Globals, "__gnat_set_globals");
26979
26980 -- SDP_Table_Build is a library routine used to build the
26981 -- exception tables. See unit Ada.Exceptions in files
26982 -- a-except.ads/adb for full details of how zero cost
26983 -- exception handling works. This procedure, the call to
26984 -- it, and the two following tables are all omitted if the
26985 -- build is in longjmp/setjmp exception mode.
26986
26987 @findex SDP_Table_Build
26988 @findex Zero Cost Exceptions
26989 procedure SDP_Table_Build
26990 (SDP_Addresses : System.Address;
26991 SDP_Count : Natural;
26992 Elab_Addresses : System.Address;
26993 Elab_Addr_Count : Natural);
26994 pragma Import (C, SDP_Table_Build, "__gnat_SDP_Table_Build");
26995
26996 -- Table of Unit_Exception_Table addresses. Used for zero
26997 -- cost exception handling to build the top level table.
26998
26999 ST : aliased constant array (1 .. 23) of System.Address := (
27000 Hello'UET_Address,
27001 Ada.Text_Io'UET_Address,
27002 Ada.Exceptions'UET_Address,
27003 Gnat.Heap_Sort_A'UET_Address,
27004 System.Exception_Table'UET_Address,
27005 System.Machine_State_Operations'UET_Address,
27006 System.Secondary_Stack'UET_Address,
27007 System.Parameters'UET_Address,
27008 System.Soft_Links'UET_Address,
27009 System.Stack_Checking'UET_Address,
27010 System.Traceback'UET_Address,
27011 Ada.Streams'UET_Address,
27012 Ada.Tags'UET_Address,
27013 System.String_Ops'UET_Address,
27014 Interfaces.C_Streams'UET_Address,
27015 System.File_Io'UET_Address,
27016 Ada.Finalization'UET_Address,
27017 System.Finalization_Root'UET_Address,
27018 System.Finalization_Implementation'UET_Address,
27019 System.String_Ops_Concat_3'UET_Address,
27020 System.Stream_Attributes'UET_Address,
27021 System.File_Control_Block'UET_Address,
27022 Ada.Finalization.List_Controller'UET_Address);
27023
27024 -- Table of addresses of elaboration routines. Used for
27025 -- zero cost exception handling to make sure these
27026 -- addresses are included in the top level procedure
27027 -- address table.
27028
27029 EA : aliased constant array (1 .. 23) of System.Address := (
27030 adainit'Code_Address,
27031 Do_Finalize'Code_Address,
27032 Ada.Exceptions'Elab_Spec'Address,
27033 System.Exceptions'Elab_Spec'Address,
27034 Interfaces.C_Streams'Elab_Spec'Address,
27035 System.Exception_Table'Elab_Body'Address,
27036 Ada.Io_Exceptions'Elab_Spec'Address,
27037 System.Stack_Checking'Elab_Spec'Address,
27038 System.Soft_Links'Elab_Body'Address,
27039 System.Secondary_Stack'Elab_Body'Address,
27040 Ada.Tags'Elab_Spec'Address,
27041 Ada.Tags'Elab_Body'Address,
27042 Ada.Streams'Elab_Spec'Address,
27043 System.Finalization_Root'Elab_Spec'Address,
27044 Ada.Exceptions'Elab_Body'Address,
27045 System.Finalization_Implementation'Elab_Spec'Address,
27046 System.Finalization_Implementation'Elab_Body'Address,
27047 Ada.Finalization'Elab_Spec'Address,
27048 Ada.Finalization.List_Controller'Elab_Spec'Address,
27049 System.File_Control_Block'Elab_Spec'Address,
27050 System.File_Io'Elab_Body'Address,
27051 Ada.Text_Io'Elab_Spec'Address,
27052 Ada.Text_Io'Elab_Body'Address);
27053
27054 -- Start of processing for adainit
27055
27056 begin
27057
27058 -- Call SDP_Table_Build to build the top level procedure
27059 -- table for zero cost exception handling (omitted in
27060 -- longjmp/setjmp mode).
27061
27062 SDP_Table_Build (ST'Address, 23, EA'Address, 23);
27063
27064 -- Call Set_Globals to record various information for
27065 -- this partition. The values are derived by the binder
27066 -- from information stored in the ali files by the compiler.
27067
27068 @findex __gnat_set_globals
27069 Set_Globals
27070 (Main_Priority => -1,
27071 -- Priority of main program, -1 if no pragma Priority used
27072
27073 Time_Slice_Value => -1,
27074 -- Time slice from Time_Slice pragma, -1 if none used
27075
27076 WC_Encoding => 'b',
27077 -- Wide_Character encoding used, default is brackets
27078
27079 Locking_Policy => ' ',
27080 -- Locking_Policy used, default of space means not
27081 -- specified, otherwise it is the first character of
27082 -- the policy name.
27083
27084 Queuing_Policy => ' ',
27085 -- Queuing_Policy used, default of space means not
27086 -- specified, otherwise it is the first character of
27087 -- the policy name.
27088
27089 Task_Dispatching_Policy => ' ',
27090 -- Task_Dispatching_Policy used, default of space means
27091 -- not specified, otherwise first character of the
27092 -- policy name.
27093
27094 Adafinal => System.Null_Address,
27095 -- Address of Adafinal routine, not used anymore
27096
27097 Unreserve_All_Interrupts => 0,
27098 -- Set true if pragma Unreserve_All_Interrupts was used
27099
27100 Exception_Tracebacks => 0);
27101 -- Indicates if exception tracebacks are enabled
27102
27103 Elab_Final_Code := 1;
27104
27105 -- Now we have the elaboration calls for all units in the partition.
27106 -- The Elab_Spec and Elab_Body attributes generate references to the
27107 -- implicit elaboration procedures generated by the compiler for
27108 -- each unit that requires elaboration.
27109
27110 if not E040 then
27111 Interfaces.C_Streams'Elab_Spec;
27112 end if;
27113 E040 := True;
27114 if not E008 then
27115 Ada.Exceptions'Elab_Spec;
27116 end if;
27117 if not E014 then
27118 System.Exception_Table'Elab_Body;
27119 E014 := True;
27120 end if;
27121 if not E053 then
27122 Ada.Io_Exceptions'Elab_Spec;
27123 E053 := True;
27124 end if;
27125 if not E017 then
27126 System.Exceptions'Elab_Spec;
27127 E017 := True;
27128 end if;
27129 if not E030 then
27130 System.Stack_Checking'Elab_Spec;
27131 end if;
27132 if not E028 then
27133 System.Soft_Links'Elab_Body;
27134 E028 := True;
27135 end if;
27136 E030 := True;
27137 if not E024 then
27138 System.Secondary_Stack'Elab_Body;
27139 E024 := True;
27140 end if;
27141 if not E035 then
27142 Ada.Tags'Elab_Spec;
27143 end if;
27144 if not E035 then
27145 Ada.Tags'Elab_Body;
27146 E035 := True;
27147 end if;
27148 if not E033 then
27149 Ada.Streams'Elab_Spec;
27150 E033 := True;
27151 end if;
27152 if not E046 then
27153 System.Finalization_Root'Elab_Spec;
27154 end if;
27155 E046 := True;
27156 if not E008 then
27157 Ada.Exceptions'Elab_Body;
27158 E008 := True;
27159 end if;
27160 if not E048 then
27161 System.Finalization_Implementation'Elab_Spec;
27162 end if;
27163 if not E048 then
27164 System.Finalization_Implementation'Elab_Body;
27165 E048 := True;
27166 end if;
27167 if not E044 then
27168 Ada.Finalization'Elab_Spec;
27169 end if;
27170 E044 := True;
27171 if not E057 then
27172 Ada.Finalization.List_Controller'Elab_Spec;
27173 end if;
27174 E057 := True;
27175 if not E055 then
27176 System.File_Control_Block'Elab_Spec;
27177 E055 := True;
27178 end if;
27179 if not E042 then
27180 System.File_Io'Elab_Body;
27181 E042 := True;
27182 end if;
27183 if not E006 then
27184 Ada.Text_Io'Elab_Spec;
27185 end if;
27186 if not E006 then
27187 Ada.Text_Io'Elab_Body;
27188 E006 := True;
27189 end if;
27190
27191 Elab_Final_Code := 0;
27192 end adainit;
27193
27194 --------------
27195 -- adafinal --
27196 --------------
27197
27198 @findex adafinal
27199 procedure adafinal is
27200 begin
27201 Do_Finalize;
27202 end adafinal;
27203
27204 ----------
27205 -- main --
27206 ----------
27207
27208 -- main is actually a function, as in the ANSI C standard,
27209 -- defined to return the exit status. The three parameters
27210 -- are the argument count, argument values and environment
27211 -- pointer.
27212
27213 @findex Main Program
27214 function main
27215 (argc : Integer;
27216 argv : System.Address;
27217 envp : System.Address)
27218 return Integer
27219 is
27220 -- The initialize routine performs low level system
27221 -- initialization using a standard library routine which
27222 -- sets up signal handling and performs any other
27223 -- required setup. The routine can be found in file
27224 -- a-init.c.
27225
27226 @findex __gnat_initialize
27227 procedure initialize;
27228 pragma Import (C, initialize, "__gnat_initialize");
27229
27230 -- The finalize routine performs low level system
27231 -- finalization using a standard library routine. The
27232 -- routine is found in file a-final.c and in the standard
27233 -- distribution is a dummy routine that does nothing, so
27234 -- really this is a hook for special user finalization.
27235
27236 @findex __gnat_finalize
27237 procedure finalize;
27238 pragma Import (C, finalize, "__gnat_finalize");
27239
27240 -- We get to the main program of the partition by using
27241 -- pragma Import because if we try to with the unit and
27242 -- call it Ada style, then not only do we waste time
27243 -- recompiling it, but also, we don't really know the right
27244 -- switches (e.g.@: identifier character set) to be used
27245 -- to compile it.
27246
27247 procedure Ada_Main_Program;
27248 pragma Import (Ada, Ada_Main_Program, "_ada_hello");
27249
27250 -- Start of processing for main
27251
27252 begin
27253 -- Save global variables
27254
27255 gnat_argc := argc;
27256 gnat_argv := argv;
27257 gnat_envp := envp;
27258
27259 -- Call low level system initialization
27260
27261 Initialize;
27262
27263 -- Call our generated Ada initialization routine
27264
27265 adainit;
27266
27267 -- This is the point at which we want the debugger to get
27268 -- control
27269
27270 Break_Start;
27271
27272 -- Now we call the main program of the partition
27273
27274 Ada_Main_Program;
27275
27276 -- Perform Ada finalization
27277
27278 adafinal;
27279
27280 -- Perform low level system finalization
27281
27282 Finalize;
27283
27284 -- Return the proper exit status
27285 return (gnat_exit_status);
27286 end;
27287
27288 -- This section is entirely comments, so it has no effect on the
27289 -- compilation of the Ada_Main package. It provides the list of
27290 -- object files and linker options, as well as some standard
27291 -- libraries needed for the link. The gnatlink utility parses
27292 -- this b~hello.adb file to read these comment lines to generate
27293 -- the appropriate command line arguments for the call to the
27294 -- system linker. The BEGIN/END lines are used for sentinels for
27295 -- this parsing operation.
27296
27297 -- The exact file names will of course depend on the environment,
27298 -- host/target and location of files on the host system.
27299
27300 @findex Object file list
27301 -- BEGIN Object file/option list
27302 -- ./hello.o
27303 -- -L./
27304 -- -L/usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/
27305 -- /usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a
27306 -- END Object file/option list
27307
27308 end ada_main;
27309 @end smallexample
27310
27311 @noindent
27312 The Ada code in the above example is exactly what is generated by the
27313 binder. We have added comments to more clearly indicate the function
27314 of each part of the generated @code{Ada_Main} package.
27315
27316 The code is standard Ada in all respects, and can be processed by any
27317 tools that handle Ada. In particular, it is possible to use the debugger
27318 in Ada mode to debug the generated @code{Ada_Main} package. For example,
27319 suppose that for reasons that you do not understand, your program is crashing
27320 during elaboration of the body of @code{Ada.Text_IO}. To locate this bug,
27321 you can place a breakpoint on the call:
27322
27323 @smallexample @c ada
27324 Ada.Text_Io'Elab_Body;
27325 @end smallexample
27326
27327 @noindent
27328 and trace the elaboration routine for this package to find out where
27329 the problem might be (more usually of course you would be debugging
27330 elaboration code in your own application).
27331
27332 @node Elaboration Order Handling in GNAT
27333 @appendix Elaboration Order Handling in GNAT
27334 @cindex Order of elaboration
27335 @cindex Elaboration control
27336
27337 @menu
27338 * Elaboration Code::
27339 * Checking the Elaboration Order::
27340 * Controlling the Elaboration Order::
27341 * Controlling Elaboration in GNAT - Internal Calls::
27342 * Controlling Elaboration in GNAT - External Calls::
27343 * Default Behavior in GNAT - Ensuring Safety::
27344 * Treatment of Pragma Elaborate::
27345 * Elaboration Issues for Library Tasks::
27346 * Mixing Elaboration Models::
27347 * What to Do If the Default Elaboration Behavior Fails::
27348 * Elaboration for Access-to-Subprogram Values::
27349 * Summary of Procedures for Elaboration Control::
27350 * Other Elaboration Order Considerations::
27351 @end menu
27352
27353 @noindent
27354 This chapter describes the handling of elaboration code in Ada and
27355 in GNAT, and discusses how the order of elaboration of program units can
27356 be controlled in GNAT, either automatically or with explicit programming
27357 features.
27358
27359 @node Elaboration Code
27360 @section Elaboration Code
27361
27362 @noindent
27363 Ada provides rather general mechanisms for executing code at elaboration
27364 time, that is to say before the main program starts executing. Such code arises
27365 in three contexts:
27366
27367 @table @asis
27368 @item Initializers for variables.
27369 Variables declared at the library level, in package specs or bodies, can
27370 require initialization that is performed at elaboration time, as in:
27371 @smallexample @c ada
27372 @cartouche
27373 Sqrt_Half : Float := Sqrt (0.5);
27374 @end cartouche
27375 @end smallexample
27376
27377 @item Package initialization code
27378 Code in a @code{BEGIN-END} section at the outer level of a package body is
27379 executed as part of the package body elaboration code.
27380
27381 @item Library level task allocators
27382 Tasks that are declared using task allocators at the library level
27383 start executing immediately and hence can execute at elaboration time.
27384 @end table
27385
27386 @noindent
27387 Subprogram calls are possible in any of these contexts, which means that
27388 any arbitrary part of the program may be executed as part of the elaboration
27389 code. It is even possible to write a program which does all its work at
27390 elaboration time, with a null main program, although stylistically this
27391 would usually be considered an inappropriate way to structure
27392 a program.
27393
27394 An important concern arises in the context of elaboration code:
27395 we have to be sure that it is executed in an appropriate order. What we
27396 have is a series of elaboration code sections, potentially one section
27397 for each unit in the program. It is important that these execute
27398 in the correct order. Correctness here means that, taking the above
27399 example of the declaration of @code{Sqrt_Half},
27400 if some other piece of
27401 elaboration code references @code{Sqrt_Half},
27402 then it must run after the
27403 section of elaboration code that contains the declaration of
27404 @code{Sqrt_Half}.
27405
27406 There would never be any order of elaboration problem if we made a rule
27407 that whenever you @code{with} a unit, you must elaborate both the spec and body
27408 of that unit before elaborating the unit doing the @code{with}'ing:
27409
27410 @smallexample @c ada
27411 @group
27412 @cartouche
27413 with Unit_1;
27414 package Unit_2 is @dots{}
27415 @end cartouche
27416 @end group
27417 @end smallexample
27418
27419 @noindent
27420 would require that both the body and spec of @code{Unit_1} be elaborated
27421 before the spec of @code{Unit_2}. However, a rule like that would be far too
27422 restrictive. In particular, it would make it impossible to have routines
27423 in separate packages that were mutually recursive.
27424
27425 You might think that a clever enough compiler could look at the actual
27426 elaboration code and determine an appropriate correct order of elaboration,
27427 but in the general case, this is not possible. Consider the following
27428 example.
27429
27430 In the body of @code{Unit_1}, we have a procedure @code{Func_1}
27431 that references
27432 the variable @code{Sqrt_1}, which is declared in the elaboration code
27433 of the body of @code{Unit_1}:
27434
27435 @smallexample @c ada
27436 @cartouche
27437 Sqrt_1 : Float := Sqrt (0.1);
27438 @end cartouche
27439 @end smallexample
27440
27441 @noindent
27442 The elaboration code of the body of @code{Unit_1} also contains:
27443
27444 @smallexample @c ada
27445 @group
27446 @cartouche
27447 if expression_1 = 1 then
27448 Q := Unit_2.Func_2;
27449 end if;
27450 @end cartouche
27451 @end group
27452 @end smallexample
27453
27454 @noindent
27455 @code{Unit_2} is exactly parallel,
27456 it has a procedure @code{Func_2} that references
27457 the variable @code{Sqrt_2}, which is declared in the elaboration code of
27458 the body @code{Unit_2}:
27459
27460 @smallexample @c ada
27461 @cartouche
27462 Sqrt_2 : Float := Sqrt (0.1);
27463 @end cartouche
27464 @end smallexample
27465
27466 @noindent
27467 The elaboration code of the body of @code{Unit_2} also contains:
27468
27469 @smallexample @c ada
27470 @group
27471 @cartouche
27472 if expression_2 = 2 then
27473 Q := Unit_1.Func_1;
27474 end if;
27475 @end cartouche
27476 @end group
27477 @end smallexample
27478
27479 @noindent
27480 Now the question is, which of the following orders of elaboration is
27481 acceptable:
27482
27483 @smallexample
27484 @group
27485 Spec of Unit_1
27486 Spec of Unit_2
27487 Body of Unit_1
27488 Body of Unit_2
27489 @end group
27490 @end smallexample
27491
27492 @noindent
27493 or
27494
27495 @smallexample
27496 @group
27497 Spec of Unit_2
27498 Spec of Unit_1
27499 Body of Unit_2
27500 Body of Unit_1
27501 @end group
27502 @end smallexample
27503
27504 @noindent
27505 If you carefully analyze the flow here, you will see that you cannot tell
27506 at compile time the answer to this question.
27507 If @code{expression_1} is not equal to 1,
27508 and @code{expression_2} is not equal to 2,
27509 then either order is acceptable, because neither of the function calls is
27510 executed. If both tests evaluate to true, then neither order is acceptable
27511 and in fact there is no correct order.
27512
27513 If one of the two expressions is true, and the other is false, then one
27514 of the above orders is correct, and the other is incorrect. For example,
27515 if @code{expression_1} /= 1 and @code{expression_2} = 2,
27516 then the call to @code{Func_1}
27517 will occur, but not the call to @code{Func_2.}
27518 This means that it is essential
27519 to elaborate the body of @code{Unit_1} before
27520 the body of @code{Unit_2}, so the first
27521 order of elaboration is correct and the second is wrong.
27522
27523 By making @code{expression_1} and @code{expression_2}
27524 depend on input data, or perhaps
27525 the time of day, we can make it impossible for the compiler or binder
27526 to figure out which of these expressions will be true, and hence it
27527 is impossible to guarantee a safe order of elaboration at run time.
27528
27529 @node Checking the Elaboration Order
27530 @section Checking the Elaboration Order
27531
27532 @noindent
27533 In some languages that involve the same kind of elaboration problems,
27534 e.g.@: Java and C++, the programmer is expected to worry about these
27535 ordering problems himself, and it is common to
27536 write a program in which an incorrect elaboration order gives
27537 surprising results, because it references variables before they
27538 are initialized.
27539 Ada is designed to be a safe language, and a programmer-beware approach is
27540 clearly not sufficient. Consequently, the language provides three lines
27541 of defense:
27542
27543 @table @asis
27544 @item Standard rules
27545 Some standard rules restrict the possible choice of elaboration
27546 order. In particular, if you @code{with} a unit, then its spec is always
27547 elaborated before the unit doing the @code{with}. Similarly, a parent
27548 spec is always elaborated before the child spec, and finally
27549 a spec is always elaborated before its corresponding body.
27550
27551 @item Dynamic elaboration checks
27552 @cindex Elaboration checks
27553 @cindex Checks, elaboration
27554 Dynamic checks are made at run time, so that if some entity is accessed
27555 before it is elaborated (typically by means of a subprogram call)
27556 then the exception (@code{Program_Error}) is raised.
27557
27558 @item Elaboration control
27559 Facilities are provided for the programmer to specify the desired order
27560 of elaboration.
27561 @end table
27562
27563 Let's look at these facilities in more detail. First, the rules for
27564 dynamic checking. One possible rule would be simply to say that the
27565 exception is raised if you access a variable which has not yet been
27566 elaborated. The trouble with this approach is that it could require
27567 expensive checks on every variable reference. Instead Ada has two
27568 rules which are a little more restrictive, but easier to check, and
27569 easier to state:
27570
27571 @table @asis
27572 @item Restrictions on calls
27573 A subprogram can only be called at elaboration time if its body
27574 has been elaborated. The rules for elaboration given above guarantee
27575 that the spec of the subprogram has been elaborated before the
27576 call, but not the body. If this rule is violated, then the
27577 exception @code{Program_Error} is raised.
27578
27579 @item Restrictions on instantiations
27580 A generic unit can only be instantiated if the body of the generic
27581 unit has been elaborated. Again, the rules for elaboration given above
27582 guarantee that the spec of the generic unit has been elaborated
27583 before the instantiation, but not the body. If this rule is
27584 violated, then the exception @code{Program_Error} is raised.
27585 @end table
27586
27587 @noindent
27588 The idea is that if the body has been elaborated, then any variables
27589 it references must have been elaborated; by checking for the body being
27590 elaborated we guarantee that none of its references causes any
27591 trouble. As we noted above, this is a little too restrictive, because a
27592 subprogram that has no non-local references in its body may in fact be safe
27593 to call. However, it really would be unsafe to rely on this, because
27594 it would mean that the caller was aware of details of the implementation
27595 in the body. This goes against the basic tenets of Ada.
27596
27597 A plausible implementation can be described as follows.
27598 A Boolean variable is associated with each subprogram
27599 and each generic unit. This variable is initialized to False, and is set to
27600 True at the point body is elaborated. Every call or instantiation checks the
27601 variable, and raises @code{Program_Error} if the variable is False.
27602
27603 Note that one might think that it would be good enough to have one Boolean
27604 variable for each package, but that would not deal with cases of trying
27605 to call a body in the same package as the call
27606 that has not been elaborated yet.
27607 Of course a compiler may be able to do enough analysis to optimize away
27608 some of the Boolean variables as unnecessary, and @code{GNAT} indeed
27609 does such optimizations, but still the easiest conceptual model is to
27610 think of there being one variable per subprogram.
27611
27612 @node Controlling the Elaboration Order
27613 @section Controlling the Elaboration Order
27614
27615 @noindent
27616 In the previous section we discussed the rules in Ada which ensure
27617 that @code{Program_Error} is raised if an incorrect elaboration order is
27618 chosen. This prevents erroneous executions, but we need mechanisms to
27619 specify a correct execution and avoid the exception altogether.
27620 To achieve this, Ada provides a number of features for controlling
27621 the order of elaboration. We discuss these features in this section.
27622
27623 First, there are several ways of indicating to the compiler that a given
27624 unit has no elaboration problems:
27625
27626 @table @asis
27627 @item packages that do not require a body
27628 A library package that does not require a body does not permit
27629 a body (this rule was introduced in Ada 95).
27630 Thus if we have a such a package, as in:
27631
27632 @smallexample @c ada
27633 @group
27634 @cartouche
27635 package Definitions is
27636 generic
27637 type m is new integer;
27638 package Subp is
27639 type a is array (1 .. 10) of m;
27640 type b is array (1 .. 20) of m;
27641 end Subp;
27642 end Definitions;
27643 @end cartouche
27644 @end group
27645 @end smallexample
27646
27647 @noindent
27648 A package that @code{with}'s @code{Definitions} may safely instantiate
27649 @code{Definitions.Subp} because the compiler can determine that there
27650 definitely is no package body to worry about in this case
27651
27652 @item pragma Pure
27653 @cindex pragma Pure
27654 @findex Pure
27655 Places sufficient restrictions on a unit to guarantee that
27656 no call to any subprogram in the unit can result in an
27657 elaboration problem. This means that the compiler does not need
27658 to worry about the point of elaboration of such units, and in
27659 particular, does not need to check any calls to any subprograms
27660 in this unit.
27661
27662 @item pragma Preelaborate
27663 @findex Preelaborate
27664 @cindex pragma Preelaborate
27665 This pragma places slightly less stringent restrictions on a unit than
27666 does pragma Pure,
27667 but these restrictions are still sufficient to ensure that there
27668 are no elaboration problems with any calls to the unit.
27669
27670 @item pragma Elaborate_Body
27671 @findex Elaborate_Body
27672 @cindex pragma Elaborate_Body
27673 This pragma requires that the body of a unit be elaborated immediately
27674 after its spec. Suppose a unit @code{A} has such a pragma,
27675 and unit @code{B} does
27676 a @code{with} of unit @code{A}. Recall that the standard rules require
27677 the spec of unit @code{A}
27678 to be elaborated before the @code{with}'ing unit; given the pragma in
27679 @code{A}, we also know that the body of @code{A}
27680 will be elaborated before @code{B}, so
27681 that calls to @code{A} are safe and do not need a check.
27682 @end table
27683
27684 @noindent
27685 Note that,
27686 unlike pragma @code{Pure} and pragma @code{Preelaborate},
27687 the use of
27688 @code{Elaborate_Body} does not guarantee that the program is
27689 free of elaboration problems, because it may not be possible
27690 to satisfy the requested elaboration order.
27691 Let's go back to the example with @code{Unit_1} and @code{Unit_2}.
27692 If a programmer
27693 marks @code{Unit_1} as @code{Elaborate_Body},
27694 and not @code{Unit_2,} then the order of
27695 elaboration will be:
27696
27697 @smallexample
27698 @group
27699 Spec of Unit_2
27700 Spec of Unit_1
27701 Body of Unit_1
27702 Body of Unit_2
27703 @end group
27704 @end smallexample
27705
27706 @noindent
27707 Now that means that the call to @code{Func_1} in @code{Unit_2}
27708 need not be checked,
27709 it must be safe. But the call to @code{Func_2} in
27710 @code{Unit_1} may still fail if
27711 @code{Expression_1} is equal to 1,
27712 and the programmer must still take
27713 responsibility for this not being the case.
27714
27715 If all units carry a pragma @code{Elaborate_Body}, then all problems are
27716 eliminated, except for calls entirely within a body, which are
27717 in any case fully under programmer control. However, using the pragma
27718 everywhere is not always possible.
27719 In particular, for our @code{Unit_1}/@code{Unit_2} example, if
27720 we marked both of them as having pragma @code{Elaborate_Body}, then
27721 clearly there would be no possible elaboration order.
27722
27723 The above pragmas allow a server to guarantee safe use by clients, and
27724 clearly this is the preferable approach. Consequently a good rule
27725 is to mark units as @code{Pure} or @code{Preelaborate} if possible,
27726 and if this is not possible,
27727 mark them as @code{Elaborate_Body} if possible.
27728 As we have seen, there are situations where neither of these
27729 three pragmas can be used.
27730 So we also provide methods for clients to control the
27731 order of elaboration of the servers on which they depend:
27732
27733 @table @asis
27734 @item pragma Elaborate (unit)
27735 @findex Elaborate
27736 @cindex pragma Elaborate
27737 This pragma is placed in the context clause, after a @code{with} clause,
27738 and it requires that the body of the named unit be elaborated before
27739 the unit in which the pragma occurs. The idea is to use this pragma
27740 if the current unit calls at elaboration time, directly or indirectly,
27741 some subprogram in the named unit.
27742
27743 @item pragma Elaborate_All (unit)
27744 @findex Elaborate_All
27745 @cindex pragma Elaborate_All
27746 This is a stronger version of the Elaborate pragma. Consider the
27747 following example:
27748
27749 @smallexample
27750 Unit A @code{with}'s unit B and calls B.Func in elab code
27751 Unit B @code{with}'s unit C, and B.Func calls C.Func
27752 @end smallexample
27753
27754 @noindent
27755 Now if we put a pragma @code{Elaborate (B)}
27756 in unit @code{A}, this ensures that the
27757 body of @code{B} is elaborated before the call, but not the
27758 body of @code{C}, so
27759 the call to @code{C.Func} could still cause @code{Program_Error} to
27760 be raised.
27761
27762 The effect of a pragma @code{Elaborate_All} is stronger, it requires
27763 not only that the body of the named unit be elaborated before the
27764 unit doing the @code{with}, but also the bodies of all units that the
27765 named unit uses, following @code{with} links transitively. For example,
27766 if we put a pragma @code{Elaborate_All (B)} in unit @code{A},
27767 then it requires
27768 not only that the body of @code{B} be elaborated before @code{A},
27769 but also the
27770 body of @code{C}, because @code{B} @code{with}'s @code{C}.
27771 @end table
27772
27773 @noindent
27774 We are now in a position to give a usage rule in Ada for avoiding
27775 elaboration problems, at least if dynamic dispatching and access to
27776 subprogram values are not used. We will handle these cases separately
27777 later.
27778
27779 The rule is simple. If a unit has elaboration code that can directly or
27780 indirectly make a call to a subprogram in a @code{with}'ed unit, or instantiate
27781 a generic package in a @code{with}'ed unit,
27782 then if the @code{with}'ed unit does not have
27783 pragma @code{Pure} or @code{Preelaborate}, then the client should have
27784 a pragma @code{Elaborate_All}
27785 for the @code{with}'ed unit. By following this rule a client is
27786 assured that calls can be made without risk of an exception.
27787
27788 For generic subprogram instantiations, the rule can be relaxed to
27789 require only a pragma @code{Elaborate} since elaborating the body
27790 of a subprogram cannot cause any transitive elaboration (we are
27791 not calling the subprogram in this case, just elaborating its
27792 declaration).
27793
27794 If this rule is not followed, then a program may be in one of four
27795 states:
27796
27797 @table @asis
27798 @item No order exists
27799 No order of elaboration exists which follows the rules, taking into
27800 account any @code{Elaborate}, @code{Elaborate_All},
27801 or @code{Elaborate_Body} pragmas. In
27802 this case, an Ada compiler must diagnose the situation at bind
27803 time, and refuse to build an executable program.
27804
27805 @item One or more orders exist, all incorrect
27806 One or more acceptable elaboration orders exist, and all of them
27807 generate an elaboration order problem. In this case, the binder
27808 can build an executable program, but @code{Program_Error} will be raised
27809 when the program is run.
27810
27811 @item Several orders exist, some right, some incorrect
27812 One or more acceptable elaboration orders exists, and some of them
27813 work, and some do not. The programmer has not controlled
27814 the order of elaboration, so the binder may or may not pick one of
27815 the correct orders, and the program may or may not raise an
27816 exception when it is run. This is the worst case, because it means
27817 that the program may fail when moved to another compiler, or even
27818 another version of the same compiler.
27819
27820 @item One or more orders exists, all correct
27821 One ore more acceptable elaboration orders exist, and all of them
27822 work. In this case the program runs successfully. This state of
27823 affairs can be guaranteed by following the rule we gave above, but
27824 may be true even if the rule is not followed.
27825 @end table
27826
27827 @noindent
27828 Note that one additional advantage of following our rules on the use
27829 of @code{Elaborate} and @code{Elaborate_All}
27830 is that the program continues to stay in the ideal (all orders OK) state
27831 even if maintenance
27832 changes some bodies of some units. Conversely, if a program that does
27833 not follow this rule happens to be safe at some point, this state of affairs
27834 may deteriorate silently as a result of maintenance changes.
27835
27836 You may have noticed that the above discussion did not mention
27837 the use of @code{Elaborate_Body}. This was a deliberate omission. If you
27838 @code{with} an @code{Elaborate_Body} unit, it still may be the case that
27839 code in the body makes calls to some other unit, so it is still necessary
27840 to use @code{Elaborate_All} on such units.
27841
27842 @node Controlling Elaboration in GNAT - Internal Calls
27843 @section Controlling Elaboration in GNAT - Internal Calls
27844
27845 @noindent
27846 In the case of internal calls, i.e., calls within a single package, the
27847 programmer has full control over the order of elaboration, and it is up
27848 to the programmer to elaborate declarations in an appropriate order. For
27849 example writing:
27850
27851 @smallexample @c ada
27852 @group
27853 @cartouche
27854 function One return Float;
27855
27856 Q : Float := One;
27857
27858 function One return Float is
27859 begin
27860 return 1.0;
27861 end One;
27862 @end cartouche
27863 @end group
27864 @end smallexample
27865
27866 @noindent
27867 will obviously raise @code{Program_Error} at run time, because function
27868 One will be called before its body is elaborated. In this case GNAT will
27869 generate a warning that the call will raise @code{Program_Error}:
27870
27871 @smallexample
27872 @group
27873 @cartouche
27874 1. procedure y is
27875 2. function One return Float;
27876 3.
27877 4. Q : Float := One;
27878 |
27879 >>> warning: cannot call "One" before body is elaborated
27880 >>> warning: Program_Error will be raised at run time
27881
27882 5.
27883 6. function One return Float is
27884 7. begin
27885 8. return 1.0;
27886 9. end One;
27887 10.
27888 11. begin
27889 12. null;
27890 13. end;
27891 @end cartouche
27892 @end group
27893 @end smallexample
27894
27895 @noindent
27896 Note that in this particular case, it is likely that the call is safe, because
27897 the function @code{One} does not access any global variables.
27898 Nevertheless in Ada, we do not want the validity of the check to depend on
27899 the contents of the body (think about the separate compilation case), so this
27900 is still wrong, as we discussed in the previous sections.
27901
27902 The error is easily corrected by rearranging the declarations so that the
27903 body of @code{One} appears before the declaration containing the call
27904 (note that in Ada 95 and Ada 2005,
27905 declarations can appear in any order, so there is no restriction that
27906 would prevent this reordering, and if we write:
27907
27908 @smallexample @c ada
27909 @group
27910 @cartouche
27911 function One return Float;
27912
27913 function One return Float is
27914 begin
27915 return 1.0;
27916 end One;
27917
27918 Q : Float := One;
27919 @end cartouche
27920 @end group
27921 @end smallexample
27922
27923 @noindent
27924 then all is well, no warning is generated, and no
27925 @code{Program_Error} exception
27926 will be raised.
27927 Things are more complicated when a chain of subprograms is executed:
27928
27929 @smallexample @c ada
27930 @group
27931 @cartouche
27932 function A return Integer;
27933 function B return Integer;
27934 function C return Integer;
27935
27936 function B return Integer is begin return A; end;
27937 function C return Integer is begin return B; end;
27938
27939 X : Integer := C;
27940
27941 function A return Integer is begin return 1; end;
27942 @end cartouche
27943 @end group
27944 @end smallexample
27945
27946 @noindent
27947 Now the call to @code{C}
27948 at elaboration time in the declaration of @code{X} is correct, because
27949 the body of @code{C} is already elaborated,
27950 and the call to @code{B} within the body of
27951 @code{C} is correct, but the call
27952 to @code{A} within the body of @code{B} is incorrect, because the body
27953 of @code{A} has not been elaborated, so @code{Program_Error}
27954 will be raised on the call to @code{A}.
27955 In this case GNAT will generate a
27956 warning that @code{Program_Error} may be
27957 raised at the point of the call. Let's look at the warning:
27958
27959 @smallexample
27960 @group
27961 @cartouche
27962 1. procedure x is
27963 2. function A return Integer;
27964 3. function B return Integer;
27965 4. function C return Integer;
27966 5.
27967 6. function B return Integer is begin return A; end;
27968 |
27969 >>> warning: call to "A" before body is elaborated may
27970 raise Program_Error
27971 >>> warning: "B" called at line 7
27972 >>> warning: "C" called at line 9
27973
27974 7. function C return Integer is begin return B; end;
27975 8.
27976 9. X : Integer := C;
27977 10.
27978 11. function A return Integer is begin return 1; end;
27979 12.
27980 13. begin
27981 14. null;
27982 15. end;
27983 @end cartouche
27984 @end group
27985 @end smallexample
27986
27987 @noindent
27988 Note that the message here says ``may raise'', instead of the direct case,
27989 where the message says ``will be raised''. That's because whether
27990 @code{A} is
27991 actually called depends in general on run-time flow of control.
27992 For example, if the body of @code{B} said
27993
27994 @smallexample @c ada
27995 @group
27996 @cartouche
27997 function B return Integer is
27998 begin
27999 if some-condition-depending-on-input-data then
28000 return A;
28001 else
28002 return 1;
28003 end if;
28004 end B;
28005 @end cartouche
28006 @end group
28007 @end smallexample
28008
28009 @noindent
28010 then we could not know until run time whether the incorrect call to A would
28011 actually occur, so @code{Program_Error} might
28012 or might not be raised. It is possible for a compiler to
28013 do a better job of analyzing bodies, to
28014 determine whether or not @code{Program_Error}
28015 might be raised, but it certainly
28016 couldn't do a perfect job (that would require solving the halting problem
28017 and is provably impossible), and because this is a warning anyway, it does
28018 not seem worth the effort to do the analysis. Cases in which it
28019 would be relevant are rare.
28020
28021 In practice, warnings of either of the forms given
28022 above will usually correspond to
28023 real errors, and should be examined carefully and eliminated.
28024 In the rare case where a warning is bogus, it can be suppressed by any of
28025 the following methods:
28026
28027 @itemize @bullet
28028 @item
28029 Compile with the @option{-gnatws} switch set
28030
28031 @item
28032 Suppress @code{Elaboration_Check} for the called subprogram
28033
28034 @item
28035 Use pragma @code{Warnings_Off} to turn warnings off for the call
28036 @end itemize
28037
28038 @noindent
28039 For the internal elaboration check case,
28040 GNAT by default generates the
28041 necessary run-time checks to ensure
28042 that @code{Program_Error} is raised if any
28043 call fails an elaboration check. Of course this can only happen if a
28044 warning has been issued as described above. The use of pragma
28045 @code{Suppress (Elaboration_Check)} may (but is not guaranteed to) suppress
28046 some of these checks, meaning that it may be possible (but is not
28047 guaranteed) for a program to be able to call a subprogram whose body
28048 is not yet elaborated, without raising a @code{Program_Error} exception.
28049
28050 @node Controlling Elaboration in GNAT - External Calls
28051 @section Controlling Elaboration in GNAT - External Calls
28052
28053 @noindent
28054 The previous section discussed the case in which the execution of a
28055 particular thread of elaboration code occurred entirely within a
28056 single unit. This is the easy case to handle, because a programmer
28057 has direct and total control over the order of elaboration, and
28058 furthermore, checks need only be generated in cases which are rare
28059 and which the compiler can easily detect.
28060 The situation is more complex when separate compilation is taken into account.
28061 Consider the following:
28062
28063 @smallexample @c ada
28064 @cartouche
28065 @group
28066 package Math is
28067 function Sqrt (Arg : Float) return Float;
28068 end Math;
28069
28070 package body Math is
28071 function Sqrt (Arg : Float) return Float is
28072 begin
28073 @dots{}
28074 end Sqrt;
28075 end Math;
28076 @end group
28077 @group
28078 with Math;
28079 package Stuff is
28080 X : Float := Math.Sqrt (0.5);
28081 end Stuff;
28082
28083 with Stuff;
28084 procedure Main is
28085 begin
28086 @dots{}
28087 end Main;
28088 @end group
28089 @end cartouche
28090 @end smallexample
28091
28092 @noindent
28093 where @code{Main} is the main program. When this program is executed, the
28094 elaboration code must first be executed, and one of the jobs of the
28095 binder is to determine the order in which the units of a program are
28096 to be elaborated. In this case we have four units: the spec and body
28097 of @code{Math},
28098 the spec of @code{Stuff} and the body of @code{Main}).
28099 In what order should the four separate sections of elaboration code
28100 be executed?
28101
28102 There are some restrictions in the order of elaboration that the binder
28103 can choose. In particular, if unit U has a @code{with}
28104 for a package @code{X}, then you
28105 are assured that the spec of @code{X}
28106 is elaborated before U , but you are
28107 not assured that the body of @code{X}
28108 is elaborated before U.
28109 This means that in the above case, the binder is allowed to choose the
28110 order:
28111
28112 @smallexample
28113 spec of Math
28114 spec of Stuff
28115 body of Math
28116 body of Main
28117 @end smallexample
28118
28119 @noindent
28120 but that's not good, because now the call to @code{Math.Sqrt}
28121 that happens during
28122 the elaboration of the @code{Stuff}
28123 spec happens before the body of @code{Math.Sqrt} is
28124 elaborated, and hence causes @code{Program_Error} exception to be raised.
28125 At first glance, one might say that the binder is misbehaving, because
28126 obviously you want to elaborate the body of something you @code{with}
28127 first, but
28128 that is not a general rule that can be followed in all cases. Consider
28129
28130 @smallexample @c ada
28131 @group
28132 @cartouche
28133 package X is @dots{}
28134
28135 package Y is @dots{}
28136
28137 with X;
28138 package body Y is @dots{}
28139
28140 with Y;
28141 package body X is @dots{}
28142 @end cartouche
28143 @end group
28144 @end smallexample
28145
28146 @noindent
28147 This is a common arrangement, and, apart from the order of elaboration
28148 problems that might arise in connection with elaboration code, this works fine.
28149 A rule that says that you must first elaborate the body of anything you
28150 @code{with} cannot work in this case:
28151 the body of @code{X} @code{with}'s @code{Y},
28152 which means you would have to
28153 elaborate the body of @code{Y} first, but that @code{with}'s @code{X},
28154 which means
28155 you have to elaborate the body of @code{X} first, but @dots{} and we have a
28156 loop that cannot be broken.
28157
28158 It is true that the binder can in many cases guess an order of elaboration
28159 that is unlikely to cause a @code{Program_Error}
28160 exception to be raised, and it tries to do so (in the
28161 above example of @code{Math/Stuff/Spec}, the GNAT binder will
28162 by default
28163 elaborate the body of @code{Math} right after its spec, so all will be well).
28164
28165 However, a program that blindly relies on the binder to be helpful can
28166 get into trouble, as we discussed in the previous sections, so
28167 GNAT
28168 provides a number of facilities for assisting the programmer in
28169 developing programs that are robust with respect to elaboration order.
28170
28171 @node Default Behavior in GNAT - Ensuring Safety
28172 @section Default Behavior in GNAT - Ensuring Safety
28173
28174 @noindent
28175 The default behavior in GNAT ensures elaboration safety. In its
28176 default mode GNAT implements the
28177 rule we previously described as the right approach. Let's restate it:
28178
28179 @itemize
28180 @item
28181 @emph{If a unit has elaboration code that can directly or indirectly make a
28182 call to a subprogram in a @code{with}'ed unit, or instantiate a generic
28183 package in a @code{with}'ed unit, then if the @code{with}'ed unit
28184 does not have pragma @code{Pure} or
28185 @code{Preelaborate}, then the client should have an
28186 @code{Elaborate_All} pragma for the @code{with}'ed unit.}
28187
28188 @emph{In the case of instantiating a generic subprogram, it is always
28189 sufficient to have only an @code{Elaborate} pragma for the
28190 @code{with}'ed unit.}
28191 @end itemize
28192
28193 @noindent
28194 By following this rule a client is assured that calls and instantiations
28195 can be made without risk of an exception.
28196
28197 In this mode GNAT traces all calls that are potentially made from
28198 elaboration code, and puts in any missing implicit @code{Elaborate}
28199 and @code{Elaborate_All} pragmas.
28200 The advantage of this approach is that no elaboration problems
28201 are possible if the binder can find an elaboration order that is
28202 consistent with these implicit @code{Elaborate} and
28203 @code{Elaborate_All} pragmas. The
28204 disadvantage of this approach is that no such order may exist.
28205
28206 If the binder does not generate any diagnostics, then it means that it has
28207 found an elaboration order that is guaranteed to be safe. However, the binder
28208 may still be relying on implicitly generated @code{Elaborate} and
28209 @code{Elaborate_All} pragmas so portability to other compilers than GNAT is not
28210 guaranteed.
28211
28212 If it is important to guarantee portability, then the compilations should
28213 use the
28214 @option{-gnatwl}
28215 (warn on elaboration problems) switch. This will cause warning messages
28216 to be generated indicating the missing @code{Elaborate} and
28217 @code{Elaborate_All} pragmas.
28218 Consider the following source program:
28219
28220 @smallexample @c ada
28221 @group
28222 @cartouche
28223 with k;
28224 package j is
28225 m : integer := k.r;
28226 end;
28227 @end cartouche
28228 @end group
28229 @end smallexample
28230
28231 @noindent
28232 where it is clear that there
28233 should be a pragma @code{Elaborate_All}
28234 for unit @code{k}. An implicit pragma will be generated, and it is
28235 likely that the binder will be able to honor it. However, if you want
28236 to port this program to some other Ada compiler than GNAT.
28237 it is safer to include the pragma explicitly in the source. If this
28238 unit is compiled with the
28239 @option{-gnatwl}
28240 switch, then the compiler outputs a warning:
28241
28242 @smallexample
28243 @group
28244 @cartouche
28245 1. with k;
28246 2. package j is
28247 3. m : integer := k.r;
28248 |
28249 >>> warning: call to "r" may raise Program_Error
28250 >>> warning: missing pragma Elaborate_All for "k"
28251
28252 4. end;
28253 @end cartouche
28254 @end group
28255 @end smallexample
28256
28257 @noindent
28258 and these warnings can be used as a guide for supplying manually
28259 the missing pragmas. It is usually a bad idea to use this warning
28260 option during development. That's because it will warn you when
28261 you need to put in a pragma, but cannot warn you when it is time
28262 to take it out. So the use of pragma @code{Elaborate_All} may lead to
28263 unnecessary dependencies and even false circularities.
28264
28265 This default mode is more restrictive than the Ada Reference
28266 Manual, and it is possible to construct programs which will compile
28267 using the dynamic model described there, but will run into a
28268 circularity using the safer static model we have described.
28269
28270 Of course any Ada compiler must be able to operate in a mode
28271 consistent with the requirements of the Ada Reference Manual,
28272 and in particular must have the capability of implementing the
28273 standard dynamic model of elaboration with run-time checks.
28274
28275 In GNAT, this standard mode can be achieved either by the use of
28276 the @option{-gnatE} switch on the compiler (@command{gcc} or
28277 @command{gnatmake}) command, or by the use of the configuration pragma:
28278
28279 @smallexample @c ada
28280 pragma Elaboration_Checks (DYNAMIC);
28281 @end smallexample
28282
28283 @noindent
28284 Either approach will cause the unit affected to be compiled using the
28285 standard dynamic run-time elaboration checks described in the Ada
28286 Reference Manual. The static model is generally preferable, since it
28287 is clearly safer to rely on compile and link time checks rather than
28288 run-time checks. However, in the case of legacy code, it may be
28289 difficult to meet the requirements of the static model. This
28290 issue is further discussed in
28291 @ref{What to Do If the Default Elaboration Behavior Fails}.
28292
28293 Note that the static model provides a strict subset of the allowed
28294 behavior and programs of the Ada Reference Manual, so if you do
28295 adhere to the static model and no circularities exist,
28296 then you are assured that your program will
28297 work using the dynamic model, providing that you remove any
28298 pragma Elaborate statements from the source.
28299
28300 @node Treatment of Pragma Elaborate
28301 @section Treatment of Pragma Elaborate
28302 @cindex Pragma Elaborate
28303
28304 @noindent
28305 The use of @code{pragma Elaborate}
28306 should generally be avoided in Ada 95 and Ada 2005 programs,
28307 since there is no guarantee that transitive calls
28308 will be properly handled. Indeed at one point, this pragma was placed
28309 in Annex J (Obsolescent Features), on the grounds that it is never useful.
28310
28311 Now that's a bit restrictive. In practice, the case in which
28312 @code{pragma Elaborate} is useful is when the caller knows that there
28313 are no transitive calls, or that the called unit contains all necessary
28314 transitive @code{pragma Elaborate} statements, and legacy code often
28315 contains such uses.
28316
28317 Strictly speaking the static mode in GNAT should ignore such pragmas,
28318 since there is no assurance at compile time that the necessary safety
28319 conditions are met. In practice, this would cause GNAT to be incompatible
28320 with correctly written Ada 83 code that had all necessary
28321 @code{pragma Elaborate} statements in place. Consequently, we made the
28322 decision that GNAT in its default mode will believe that if it encounters
28323 a @code{pragma Elaborate} then the programmer knows what they are doing,
28324 and it will trust that no elaboration errors can occur.
28325
28326 The result of this decision is two-fold. First to be safe using the
28327 static mode, you should remove all @code{pragma Elaborate} statements.
28328 Second, when fixing circularities in existing code, you can selectively
28329 use @code{pragma Elaborate} statements to convince the static mode of
28330 GNAT that it need not generate an implicit @code{pragma Elaborate_All}
28331 statement.
28332
28333 When using the static mode with @option{-gnatwl}, any use of
28334 @code{pragma Elaborate} will generate a warning about possible
28335 problems.
28336
28337 @node Elaboration Issues for Library Tasks
28338 @section Elaboration Issues for Library Tasks
28339 @cindex Library tasks, elaboration issues
28340 @cindex Elaboration of library tasks
28341
28342 @noindent
28343 In this section we examine special elaboration issues that arise for
28344 programs that declare library level tasks.
28345
28346 Generally the model of execution of an Ada program is that all units are
28347 elaborated, and then execution of the program starts. However, the
28348 declaration of library tasks definitely does not fit this model. The
28349 reason for this is that library tasks start as soon as they are declared
28350 (more precisely, as soon as the statement part of the enclosing package
28351 body is reached), that is to say before elaboration
28352 of the program is complete. This means that if such a task calls a
28353 subprogram, or an entry in another task, the callee may or may not be
28354 elaborated yet, and in the standard
28355 Reference Manual model of dynamic elaboration checks, you can even
28356 get timing dependent Program_Error exceptions, since there can be
28357 a race between the elaboration code and the task code.
28358
28359 The static model of elaboration in GNAT seeks to avoid all such
28360 dynamic behavior, by being conservative, and the conservative
28361 approach in this particular case is to assume that all the code
28362 in a task body is potentially executed at elaboration time if
28363 a task is declared at the library level.
28364
28365 This can definitely result in unexpected circularities. Consider
28366 the following example
28367
28368 @smallexample @c ada
28369 package Decls is
28370 task Lib_Task is
28371 entry Start;
28372 end Lib_Task;
28373
28374 type My_Int is new Integer;
28375
28376 function Ident (M : My_Int) return My_Int;
28377 end Decls;
28378
28379 with Utils;
28380 package body Decls is
28381 task body Lib_Task is
28382 begin
28383 accept Start;
28384 Utils.Put_Val (2);
28385 end Lib_Task;
28386
28387 function Ident (M : My_Int) return My_Int is
28388 begin
28389 return M;
28390 end Ident;
28391 end Decls;
28392
28393 with Decls;
28394 package Utils is
28395 procedure Put_Val (Arg : Decls.My_Int);
28396 end Utils;
28397
28398 with Text_IO;
28399 package body Utils is
28400 procedure Put_Val (Arg : Decls.My_Int) is
28401 begin
28402 Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
28403 end Put_Val;
28404 end Utils;
28405
28406 with Decls;
28407 procedure Main is
28408 begin
28409 Decls.Lib_Task.Start;
28410 end;
28411 @end smallexample
28412
28413 @noindent
28414 If the above example is compiled in the default static elaboration
28415 mode, then a circularity occurs. The circularity comes from the call
28416 @code{Utils.Put_Val} in the task body of @code{Decls.Lib_Task}. Since
28417 this call occurs in elaboration code, we need an implicit pragma
28418 @code{Elaborate_All} for @code{Utils}. This means that not only must
28419 the spec and body of @code{Utils} be elaborated before the body
28420 of @code{Decls}, but also the spec and body of any unit that is
28421 @code{with'ed} by the body of @code{Utils} must also be elaborated before
28422 the body of @code{Decls}. This is the transitive implication of
28423 pragma @code{Elaborate_All} and it makes sense, because in general
28424 the body of @code{Put_Val} might have a call to something in a
28425 @code{with'ed} unit.
28426
28427 In this case, the body of Utils (actually its spec) @code{with's}
28428 @code{Decls}. Unfortunately this means that the body of @code{Decls}
28429 must be elaborated before itself, in case there is a call from the
28430 body of @code{Utils}.
28431
28432 Here is the exact chain of events we are worrying about:
28433
28434 @enumerate
28435 @item
28436 In the body of @code{Decls} a call is made from within the body of a library
28437 task to a subprogram in the package @code{Utils}. Since this call may
28438 occur at elaboration time (given that the task is activated at elaboration
28439 time), we have to assume the worst, i.e., that the
28440 call does happen at elaboration time.
28441
28442 @item
28443 This means that the body and spec of @code{Util} must be elaborated before
28444 the body of @code{Decls} so that this call does not cause an access before
28445 elaboration.
28446
28447 @item
28448 Within the body of @code{Util}, specifically within the body of
28449 @code{Util.Put_Val} there may be calls to any unit @code{with}'ed
28450 by this package.
28451
28452 @item
28453 One such @code{with}'ed package is package @code{Decls}, so there
28454 might be a call to a subprogram in @code{Decls} in @code{Put_Val}.
28455 In fact there is such a call in this example, but we would have to
28456 assume that there was such a call even if it were not there, since
28457 we are not supposed to write the body of @code{Decls} knowing what
28458 is in the body of @code{Utils}; certainly in the case of the
28459 static elaboration model, the compiler does not know what is in
28460 other bodies and must assume the worst.
28461
28462 @item
28463 This means that the spec and body of @code{Decls} must also be
28464 elaborated before we elaborate the unit containing the call, but
28465 that unit is @code{Decls}! This means that the body of @code{Decls}
28466 must be elaborated before itself, and that's a circularity.
28467 @end enumerate
28468
28469 @noindent
28470 Indeed, if you add an explicit pragma @code{Elaborate_All} for @code{Utils} in
28471 the body of @code{Decls} you will get a true Ada Reference Manual
28472 circularity that makes the program illegal.
28473
28474 In practice, we have found that problems with the static model of
28475 elaboration in existing code often arise from library tasks, so
28476 we must address this particular situation.
28477
28478 Note that if we compile and run the program above, using the dynamic model of
28479 elaboration (that is to say use the @option{-gnatE} switch),
28480 then it compiles, binds,
28481 links, and runs, printing the expected result of 2. Therefore in some sense
28482 the circularity here is only apparent, and we need to capture
28483 the properties of this program that distinguish it from other library-level
28484 tasks that have real elaboration problems.
28485
28486 We have four possible answers to this question:
28487
28488 @itemize @bullet
28489
28490 @item
28491 Use the dynamic model of elaboration.
28492
28493 If we use the @option{-gnatE} switch, then as noted above, the program works.
28494 Why is this? If we examine the task body, it is apparent that the task cannot
28495 proceed past the
28496 @code{accept} statement until after elaboration has been completed, because
28497 the corresponding entry call comes from the main program, not earlier.
28498 This is why the dynamic model works here. But that's really giving
28499 up on a precise analysis, and we prefer to take this approach only if we cannot
28500 solve the
28501 problem in any other manner. So let us examine two ways to reorganize
28502 the program to avoid the potential elaboration problem.
28503
28504 @item
28505 Split library tasks into separate packages.
28506
28507 Write separate packages, so that library tasks are isolated from
28508 other declarations as much as possible. Let us look at a variation on
28509 the above program.
28510
28511 @smallexample @c ada
28512 package Decls1 is
28513 task Lib_Task is
28514 entry Start;
28515 end Lib_Task;
28516 end Decls1;
28517
28518 with Utils;
28519 package body Decls1 is
28520 task body Lib_Task is
28521 begin
28522 accept Start;
28523 Utils.Put_Val (2);
28524 end Lib_Task;
28525 end Decls1;
28526
28527 package Decls2 is
28528 type My_Int is new Integer;
28529 function Ident (M : My_Int) return My_Int;
28530 end Decls2;
28531
28532 with Utils;
28533 package body Decls2 is
28534 function Ident (M : My_Int) return My_Int is
28535 begin
28536 return M;
28537 end Ident;
28538 end Decls2;
28539
28540 with Decls2;
28541 package Utils is
28542 procedure Put_Val (Arg : Decls2.My_Int);
28543 end Utils;
28544
28545 with Text_IO;
28546 package body Utils is
28547 procedure Put_Val (Arg : Decls2.My_Int) is
28548 begin
28549 Text_IO.Put_Line (Decls2.My_Int'Image (Decls2.Ident (Arg)));
28550 end Put_Val;
28551 end Utils;
28552
28553 with Decls1;
28554 procedure Main is
28555 begin
28556 Decls1.Lib_Task.Start;
28557 end;
28558 @end smallexample
28559
28560 @noindent
28561 All we have done is to split @code{Decls} into two packages, one
28562 containing the library task, and one containing everything else. Now
28563 there is no cycle, and the program compiles, binds, links and executes
28564 using the default static model of elaboration.
28565
28566 @item
28567 Declare separate task types.
28568
28569 A significant part of the problem arises because of the use of the
28570 single task declaration form. This means that the elaboration of
28571 the task type, and the elaboration of the task itself (i.e.@: the
28572 creation of the task) happen at the same time. A good rule
28573 of style in Ada is to always create explicit task types. By
28574 following the additional step of placing task objects in separate
28575 packages from the task type declaration, many elaboration problems
28576 are avoided. Here is another modified example of the example program:
28577
28578 @smallexample @c ada
28579 package Decls is
28580 task type Lib_Task_Type is
28581 entry Start;
28582 end Lib_Task_Type;
28583
28584 type My_Int is new Integer;
28585
28586 function Ident (M : My_Int) return My_Int;
28587 end Decls;
28588
28589 with Utils;
28590 package body Decls is
28591 task body Lib_Task_Type is
28592 begin
28593 accept Start;
28594 Utils.Put_Val (2);
28595 end Lib_Task_Type;
28596
28597 function Ident (M : My_Int) return My_Int is
28598 begin
28599 return M;
28600 end Ident;
28601 end Decls;
28602
28603 with Decls;
28604 package Utils is
28605 procedure Put_Val (Arg : Decls.My_Int);
28606 end Utils;
28607
28608 with Text_IO;
28609 package body Utils is
28610 procedure Put_Val (Arg : Decls.My_Int) is
28611 begin
28612 Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
28613 end Put_Val;
28614 end Utils;
28615
28616 with Decls;
28617 package Declst is
28618 Lib_Task : Decls.Lib_Task_Type;
28619 end Declst;
28620
28621 with Declst;
28622 procedure Main is
28623 begin
28624 Declst.Lib_Task.Start;
28625 end;
28626 @end smallexample
28627
28628 @noindent
28629 What we have done here is to replace the @code{task} declaration in
28630 package @code{Decls} with a @code{task type} declaration. Then we
28631 introduce a separate package @code{Declst} to contain the actual
28632 task object. This separates the elaboration issues for
28633 the @code{task type}
28634 declaration, which causes no trouble, from the elaboration issues
28635 of the task object, which is also unproblematic, since it is now independent
28636 of the elaboration of @code{Utils}.
28637 This separation of concerns also corresponds to
28638 a generally sound engineering principle of separating declarations
28639 from instances. This version of the program also compiles, binds, links,
28640 and executes, generating the expected output.
28641
28642 @item
28643 Use No_Entry_Calls_In_Elaboration_Code restriction.
28644 @cindex No_Entry_Calls_In_Elaboration_Code
28645
28646 The previous two approaches described how a program can be restructured
28647 to avoid the special problems caused by library task bodies. in practice,
28648 however, such restructuring may be difficult to apply to existing legacy code,
28649 so we must consider solutions that do not require massive rewriting.
28650
28651 Let us consider more carefully why our original sample program works
28652 under the dynamic model of elaboration. The reason is that the code
28653 in the task body blocks immediately on the @code{accept}
28654 statement. Now of course there is nothing to prohibit elaboration
28655 code from making entry calls (for example from another library level task),
28656 so we cannot tell in isolation that
28657 the task will not execute the accept statement during elaboration.
28658
28659 However, in practice it is very unusual to see elaboration code
28660 make any entry calls, and the pattern of tasks starting
28661 at elaboration time and then immediately blocking on @code{accept} or
28662 @code{select} statements is very common. What this means is that
28663 the compiler is being too pessimistic when it analyzes the
28664 whole package body as though it might be executed at elaboration
28665 time.
28666
28667 If we know that the elaboration code contains no entry calls, (a very safe
28668 assumption most of the time, that could almost be made the default
28669 behavior), then we can compile all units of the program under control
28670 of the following configuration pragma:
28671
28672 @smallexample
28673 pragma Restrictions (No_Entry_Calls_In_Elaboration_Code);
28674 @end smallexample
28675
28676 @noindent
28677 This pragma can be placed in the @file{gnat.adc} file in the usual
28678 manner. If we take our original unmodified program and compile it
28679 in the presence of a @file{gnat.adc} containing the above pragma,
28680 then once again, we can compile, bind, link, and execute, obtaining
28681 the expected result. In the presence of this pragma, the compiler does
28682 not trace calls in a task body, that appear after the first @code{accept}
28683 or @code{select} statement, and therefore does not report a potential
28684 circularity in the original program.
28685
28686 The compiler will check to the extent it can that the above
28687 restriction is not violated, but it is not always possible to do a
28688 complete check at compile time, so it is important to use this
28689 pragma only if the stated restriction is in fact met, that is to say
28690 no task receives an entry call before elaboration of all units is completed.
28691
28692 @end itemize
28693
28694 @node Mixing Elaboration Models
28695 @section Mixing Elaboration Models
28696 @noindent
28697 So far, we have assumed that the entire program is either compiled
28698 using the dynamic model or static model, ensuring consistency. It
28699 is possible to mix the two models, but rules have to be followed
28700 if this mixing is done to ensure that elaboration checks are not
28701 omitted.
28702
28703 The basic rule is that @emph{a unit compiled with the static model cannot
28704 be @code{with'ed} by a unit compiled with the dynamic model}. The
28705 reason for this is that in the static model, a unit assumes that
28706 its clients guarantee to use (the equivalent of) pragma
28707 @code{Elaborate_All} so that no elaboration checks are required
28708 in inner subprograms, and this assumption is violated if the
28709 client is compiled with dynamic checks.
28710
28711 The precise rule is as follows. A unit that is compiled with dynamic
28712 checks can only @code{with} a unit that meets at least one of the
28713 following criteria:
28714
28715 @itemize @bullet
28716
28717 @item
28718 The @code{with'ed} unit is itself compiled with dynamic elaboration
28719 checks (that is with the @option{-gnatE} switch.
28720
28721 @item
28722 The @code{with'ed} unit is an internal GNAT implementation unit from
28723 the System, Interfaces, Ada, or GNAT hierarchies.
28724
28725 @item
28726 The @code{with'ed} unit has pragma Preelaborate or pragma Pure.
28727
28728 @item
28729 The @code{with'ing} unit (that is the client) has an explicit pragma
28730 @code{Elaborate_All} for the @code{with'ed} unit.
28731
28732 @end itemize
28733
28734 @noindent
28735 If this rule is violated, that is if a unit with dynamic elaboration
28736 checks @code{with's} a unit that does not meet one of the above four
28737 criteria, then the binder (@code{gnatbind}) will issue a warning
28738 similar to that in the following example:
28739
28740 @smallexample
28741 warning: "x.ads" has dynamic elaboration checks and with's
28742 warning: "y.ads" which has static elaboration checks
28743 @end smallexample
28744
28745 @noindent
28746 These warnings indicate that the rule has been violated, and that as a result
28747 elaboration checks may be missed in the resulting executable file.
28748 This warning may be suppressed using the @option{-ws} binder switch
28749 in the usual manner.
28750
28751 One useful application of this mixing rule is in the case of a subsystem
28752 which does not itself @code{with} units from the remainder of the
28753 application. In this case, the entire subsystem can be compiled with
28754 dynamic checks to resolve a circularity in the subsystem, while
28755 allowing the main application that uses this subsystem to be compiled
28756 using the more reliable default static model.
28757
28758 @node What to Do If the Default Elaboration Behavior Fails
28759 @section What to Do If the Default Elaboration Behavior Fails
28760
28761 @noindent
28762 If the binder cannot find an acceptable order, it outputs detailed
28763 diagnostics. For example:
28764 @smallexample
28765 @group
28766 @iftex
28767 @leftskip=0cm
28768 @end iftex
28769 error: elaboration circularity detected
28770 info: "proc (body)" must be elaborated before "pack (body)"
28771 info: reason: Elaborate_All probably needed in unit "pack (body)"
28772 info: recompile "pack (body)" with -gnatwl
28773 info: for full details
28774 info: "proc (body)"
28775 info: is needed by its spec:
28776 info: "proc (spec)"
28777 info: which is withed by:
28778 info: "pack (body)"
28779 info: "pack (body)" must be elaborated before "proc (body)"
28780 info: reason: pragma Elaborate in unit "proc (body)"
28781 @end group
28782
28783 @end smallexample
28784
28785 @noindent
28786 In this case we have a cycle that the binder cannot break. On the one
28787 hand, there is an explicit pragma Elaborate in @code{proc} for
28788 @code{pack}. This means that the body of @code{pack} must be elaborated
28789 before the body of @code{proc}. On the other hand, there is elaboration
28790 code in @code{pack} that calls a subprogram in @code{proc}. This means
28791 that for maximum safety, there should really be a pragma
28792 Elaborate_All in @code{pack} for @code{proc} which would require that
28793 the body of @code{proc} be elaborated before the body of
28794 @code{pack}. Clearly both requirements cannot be satisfied.
28795 Faced with a circularity of this kind, you have three different options.
28796
28797 @table @asis
28798 @item Fix the program
28799 The most desirable option from the point of view of long-term maintenance
28800 is to rearrange the program so that the elaboration problems are avoided.
28801 One useful technique is to place the elaboration code into separate
28802 child packages. Another is to move some of the initialization code to
28803 explicitly called subprograms, where the program controls the order
28804 of initialization explicitly. Although this is the most desirable option,
28805 it may be impractical and involve too much modification, especially in
28806 the case of complex legacy code.
28807
28808 @item Perform dynamic checks
28809 If the compilations are done using the
28810 @option{-gnatE}
28811 (dynamic elaboration check) switch, then GNAT behaves in a quite different
28812 manner. Dynamic checks are generated for all calls that could possibly result
28813 in raising an exception. With this switch, the compiler does not generate
28814 implicit @code{Elaborate} or @code{Elaborate_All} pragmas. The behavior then is
28815 exactly as specified in the @cite{Ada Reference Manual}.
28816 The binder will generate
28817 an executable program that may or may not raise @code{Program_Error}, and then
28818 it is the programmer's job to ensure that it does not raise an exception. Note
28819 that it is important to compile all units with the switch, it cannot be used
28820 selectively.
28821
28822 @item Suppress checks
28823 The drawback of dynamic checks is that they generate a
28824 significant overhead at run time, both in space and time. If you
28825 are absolutely sure that your program cannot raise any elaboration
28826 exceptions, and you still want to use the dynamic elaboration model,
28827 then you can use the configuration pragma
28828 @code{Suppress (Elaboration_Check)} to suppress all such checks. For
28829 example this pragma could be placed in the @file{gnat.adc} file.
28830
28831 @item Suppress checks selectively
28832 When you know that certain calls or instantiations in elaboration code cannot
28833 possibly lead to an elaboration error, and the binder nevertheless complains
28834 about implicit @code{Elaborate} and @code{Elaborate_All} pragmas that lead to
28835 elaboration circularities, it is possible to remove those warnings locally and
28836 obtain a program that will bind. Clearly this can be unsafe, and it is the
28837 responsibility of the programmer to make sure that the resulting program has no
28838 elaboration anomalies. The pragma @code{Suppress (Elaboration_Check)} can be
28839 used with different granularity to suppress warnings and break elaboration
28840 circularities:
28841
28842 @itemize @bullet
28843 @item
28844 Place the pragma that names the called subprogram in the declarative part
28845 that contains the call.
28846
28847 @item
28848 Place the pragma in the declarative part, without naming an entity. This
28849 disables warnings on all calls in the corresponding declarative region.
28850
28851 @item
28852 Place the pragma in the package spec that declares the called subprogram,
28853 and name the subprogram. This disables warnings on all elaboration calls to
28854 that subprogram.
28855
28856 @item
28857 Place the pragma in the package spec that declares the called subprogram,
28858 without naming any entity. This disables warnings on all elaboration calls to
28859 all subprograms declared in this spec.
28860
28861 @item Use Pragma Elaborate
28862 As previously described in section @xref{Treatment of Pragma Elaborate},
28863 GNAT in static mode assumes that a @code{pragma} Elaborate indicates correctly
28864 that no elaboration checks are required on calls to the designated unit.
28865 There may be cases in which the caller knows that no transitive calls
28866 can occur, so that a @code{pragma Elaborate} will be sufficient in a
28867 case where @code{pragma Elaborate_All} would cause a circularity.
28868 @end itemize
28869
28870 @noindent
28871 These five cases are listed in order of decreasing safety, and therefore
28872 require increasing programmer care in their application. Consider the
28873 following program:
28874
28875 @smallexample @c adanocomment
28876 package Pack1 is
28877 function F1 return Integer;
28878 X1 : Integer;
28879 end Pack1;
28880
28881 package Pack2 is
28882 function F2 return Integer;
28883 function Pure (x : integer) return integer;
28884 -- pragma Suppress (Elaboration_Check, On => Pure); -- (3)
28885 -- pragma Suppress (Elaboration_Check); -- (4)
28886 end Pack2;
28887
28888 with Pack2;
28889 package body Pack1 is
28890 function F1 return Integer is
28891 begin
28892 return 100;
28893 end F1;
28894 Val : integer := Pack2.Pure (11); -- Elab. call (1)
28895 begin
28896 declare
28897 -- pragma Suppress(Elaboration_Check, Pack2.F2); -- (1)
28898 -- pragma Suppress(Elaboration_Check); -- (2)
28899 begin
28900 X1 := Pack2.F2 + 1; -- Elab. call (2)
28901 end;
28902 end Pack1;
28903
28904 with Pack1;
28905 package body Pack2 is
28906 function F2 return Integer is
28907 begin
28908 return Pack1.F1;
28909 end F2;
28910 function Pure (x : integer) return integer is
28911 begin
28912 return x ** 3 - 3 * x;
28913 end;
28914 end Pack2;
28915
28916 with Pack1, Ada.Text_IO;
28917 procedure Proc3 is
28918 begin
28919 Ada.Text_IO.Put_Line(Pack1.X1'Img); -- 101
28920 end Proc3;
28921 @end smallexample
28922 In the absence of any pragmas, an attempt to bind this program produces
28923 the following diagnostics:
28924 @smallexample
28925 @group
28926 @iftex
28927 @leftskip=.5cm
28928 @end iftex
28929 error: elaboration circularity detected
28930 info: "pack1 (body)" must be elaborated before "pack1 (body)"
28931 info: reason: Elaborate_All probably needed in unit "pack1 (body)"
28932 info: recompile "pack1 (body)" with -gnatwl for full details
28933 info: "pack1 (body)"
28934 info: must be elaborated along with its spec:
28935 info: "pack1 (spec)"
28936 info: which is withed by:
28937 info: "pack2 (body)"
28938 info: which must be elaborated along with its spec:
28939 info: "pack2 (spec)"
28940 info: which is withed by:
28941 info: "pack1 (body)"
28942 @end group
28943 @end smallexample
28944 The sources of the circularity are the two calls to @code{Pack2.Pure} and
28945 @code{Pack2.F2} in the body of @code{Pack1}. We can see that the call to
28946 F2 is safe, even though F2 calls F1, because the call appears after the
28947 elaboration of the body of F1. Therefore the pragma (1) is safe, and will
28948 remove the warning on the call. It is also possible to use pragma (2)
28949 because there are no other potentially unsafe calls in the block.
28950
28951 @noindent
28952 The call to @code{Pure} is safe because this function does not depend on the
28953 state of @code{Pack2}. Therefore any call to this function is safe, and it
28954 is correct to place pragma (3) in the corresponding package spec.
28955
28956 @noindent
28957 Finally, we could place pragma (4) in the spec of @code{Pack2} to disable
28958 warnings on all calls to functions declared therein. Note that this is not
28959 necessarily safe, and requires more detailed examination of the subprogram
28960 bodies involved. In particular, a call to @code{F2} requires that @code{F1}
28961 be already elaborated.
28962 @end table
28963
28964 @noindent
28965 It is hard to generalize on which of these four approaches should be
28966 taken. Obviously if it is possible to fix the program so that the default
28967 treatment works, this is preferable, but this may not always be practical.
28968 It is certainly simple enough to use
28969 @option{-gnatE}
28970 but the danger in this case is that, even if the GNAT binder
28971 finds a correct elaboration order, it may not always do so,
28972 and certainly a binder from another Ada compiler might not. A
28973 combination of testing and analysis (for which the warnings generated
28974 with the
28975 @option{-gnatwl}
28976 switch can be useful) must be used to ensure that the program is free
28977 of errors. One switch that is useful in this testing is the
28978 @option{^-p (pessimistic elaboration order)^/PESSIMISTIC_ELABORATION_ORDER^}
28979 switch for
28980 @code{gnatbind}.
28981 Normally the binder tries to find an order that has the best chance
28982 of avoiding elaboration problems. However, if this switch is used, the binder
28983 plays a devil's advocate role, and tries to choose the order that
28984 has the best chance of failing. If your program works even with this
28985 switch, then it has a better chance of being error free, but this is still
28986 not a guarantee.
28987
28988 For an example of this approach in action, consider the C-tests (executable
28989 tests) from the ACVC suite. If these are compiled and run with the default
28990 treatment, then all but one of them succeed without generating any error
28991 diagnostics from the binder. However, there is one test that fails, and
28992 this is not surprising, because the whole point of this test is to ensure
28993 that the compiler can handle cases where it is impossible to determine
28994 a correct order statically, and it checks that an exception is indeed
28995 raised at run time.
28996
28997 This one test must be compiled and run using the
28998 @option{-gnatE}
28999 switch, and then it passes. Alternatively, the entire suite can
29000 be run using this switch. It is never wrong to run with the dynamic
29001 elaboration switch if your code is correct, and we assume that the
29002 C-tests are indeed correct (it is less efficient, but efficiency is
29003 not a factor in running the ACVC tests.)
29004
29005 @node Elaboration for Access-to-Subprogram Values
29006 @section Elaboration for Access-to-Subprogram Values
29007 @cindex Access-to-subprogram
29008
29009 @noindent
29010 Access-to-subprogram types (introduced in Ada 95) complicate
29011 the handling of elaboration. The trouble is that it becomes
29012 impossible to tell at compile time which procedure
29013 is being called. This means that it is not possible for the binder
29014 to analyze the elaboration requirements in this case.
29015
29016 If at the point at which the access value is created
29017 (i.e., the evaluation of @code{P'Access} for a subprogram @code{P}),
29018 the body of the subprogram is
29019 known to have been elaborated, then the access value is safe, and its use
29020 does not require a check. This may be achieved by appropriate arrangement
29021 of the order of declarations if the subprogram is in the current unit,
29022 or, if the subprogram is in another unit, by using pragma
29023 @code{Pure}, @code{Preelaborate}, or @code{Elaborate_Body}
29024 on the referenced unit.
29025
29026 If the referenced body is not known to have been elaborated at the point
29027 the access value is created, then any use of the access value must do a
29028 dynamic check, and this dynamic check will fail and raise a
29029 @code{Program_Error} exception if the body has not been elaborated yet.
29030 GNAT will generate the necessary checks, and in addition, if the
29031 @option{-gnatwl}
29032 switch is set, will generate warnings that such checks are required.
29033
29034 The use of dynamic dispatching for tagged types similarly generates
29035 a requirement for dynamic checks, and premature calls to any primitive
29036 operation of a tagged type before the body of the operation has been
29037 elaborated, will result in the raising of @code{Program_Error}.
29038
29039 @node Summary of Procedures for Elaboration Control
29040 @section Summary of Procedures for Elaboration Control
29041 @cindex Elaboration control
29042
29043 @noindent
29044 First, compile your program with the default options, using none of
29045 the special elaboration control switches. If the binder successfully
29046 binds your program, then you can be confident that, apart from issues
29047 raised by the use of access-to-subprogram types and dynamic dispatching,
29048 the program is free of elaboration errors. If it is important that the
29049 program be portable, then use the
29050 @option{-gnatwl}
29051 switch to generate warnings about missing @code{Elaborate} or
29052 @code{Elaborate_All} pragmas, and supply the missing pragmas.
29053
29054 If the program fails to bind using the default static elaboration
29055 handling, then you can fix the program to eliminate the binder
29056 message, or recompile the entire program with the
29057 @option{-gnatE} switch to generate dynamic elaboration checks,
29058 and, if you are sure there really are no elaboration problems,
29059 use a global pragma @code{Suppress (Elaboration_Check)}.
29060
29061 @node Other Elaboration Order Considerations
29062 @section Other Elaboration Order Considerations
29063 @noindent
29064 This section has been entirely concerned with the issue of finding a valid
29065 elaboration order, as defined by the Ada Reference Manual. In a case
29066 where several elaboration orders are valid, the task is to find one
29067 of the possible valid elaboration orders (and the static model in GNAT
29068 will ensure that this is achieved).
29069
29070 The purpose of the elaboration rules in the Ada Reference Manual is to
29071 make sure that no entity is accessed before it has been elaborated. For
29072 a subprogram, this means that the spec and body must have been elaborated
29073 before the subprogram is called. For an object, this means that the object
29074 must have been elaborated before its value is read or written. A violation
29075 of either of these two requirements is an access before elaboration order,
29076 and this section has been all about avoiding such errors.
29077
29078 In the case where more than one order of elaboration is possible, in the
29079 sense that access before elaboration errors are avoided, then any one of
29080 the orders is ``correct'' in the sense that it meets the requirements of
29081 the Ada Reference Manual, and no such error occurs.
29082
29083 However, it may be the case for a given program, that there are
29084 constraints on the order of elaboration that come not from consideration
29085 of avoiding elaboration errors, but rather from extra-lingual logic
29086 requirements. Consider this example:
29087
29088 @smallexample @c ada
29089 with Init_Constants;
29090 package Constants is
29091 X : Integer := 0;
29092 Y : Integer := 0;
29093 end Constants;
29094
29095 package Init_Constants is
29096 procedure P; -- require a body
29097 end Init_Constants;
29098
29099 with Constants;
29100 package body Init_Constants is
29101 procedure P is begin null; end;
29102 begin
29103 Constants.X := 3;
29104 Constants.Y := 4;
29105 end Init_Constants;
29106
29107 with Constants;
29108 package Calc is
29109 Z : Integer := Constants.X + Constants.Y;
29110 end Calc;
29111
29112 with Calc;
29113 with Text_IO; use Text_IO;
29114 procedure Main is
29115 begin
29116 Put_Line (Calc.Z'Img);
29117 end Main;
29118 @end smallexample
29119
29120 @noindent
29121 In this example, there is more than one valid order of elaboration. For
29122 example both the following are correct orders:
29123
29124 @smallexample
29125 Init_Constants spec
29126 Constants spec
29127 Calc spec
29128 Init_Constants body
29129 Main body
29130
29131 and
29132
29133 Init_Constants spec
29134 Init_Constants body
29135 Constants spec
29136 Calc spec
29137 Main body
29138 @end smallexample
29139
29140 @noindent
29141 There is no language rule to prefer one or the other, both are correct
29142 from an order of elaboration point of view. But the programmatic effects
29143 of the two orders are very different. In the first, the elaboration routine
29144 of @code{Calc} initializes @code{Z} to zero, and then the main program
29145 runs with this value of zero. But in the second order, the elaboration
29146 routine of @code{Calc} runs after the body of Init_Constants has set
29147 @code{X} and @code{Y} and thus @code{Z} is set to 7 before @code{Main}
29148 runs.
29149
29150 One could perhaps by applying pretty clever non-artificial intelligence
29151 to the situation guess that it is more likely that the second order of
29152 elaboration is the one desired, but there is no formal linguistic reason
29153 to prefer one over the other. In fact in this particular case, GNAT will
29154 prefer the second order, because of the rule that bodies are elaborated
29155 as soon as possible, but it's just luck that this is what was wanted
29156 (if indeed the second order was preferred).
29157
29158 If the program cares about the order of elaboration routines in a case like
29159 this, it is important to specify the order required. In this particular
29160 case, that could have been achieved by adding to the spec of Calc:
29161
29162 @smallexample @c ada
29163 pragma Elaborate_All (Constants);
29164 @end smallexample
29165
29166 @noindent
29167 which requires that the body (if any) and spec of @code{Constants},
29168 as well as the body and spec of any unit @code{with}'ed by
29169 @code{Constants} be elaborated before @code{Calc} is elaborated.
29170
29171 Clearly no automatic method can always guess which alternative you require,
29172 and if you are working with legacy code that had constraints of this kind
29173 which were not properly specified by adding @code{Elaborate} or
29174 @code{Elaborate_All} pragmas, then indeed it is possible that two different
29175 compilers can choose different orders.
29176
29177 However, GNAT does attempt to diagnose the common situation where there
29178 are uninitialized variables in the visible part of a package spec, and the
29179 corresponding package body has an elaboration block that directly or
29180 indirectly initialized one or more of these variables. This is the situation
29181 in which a pragma Elaborate_Body is usually desirable, and GNAT will generate
29182 a warning that suggests this addition if it detects this situation.
29183
29184 The @code{gnatbind}
29185 @option{^-p^/PESSIMISTIC_ELABORATION^} switch may be useful in smoking
29186 out problems. This switch causes bodies to be elaborated as late as possible
29187 instead of as early as possible. In the example above, it would have forced
29188 the choice of the first elaboration order. If you get different results
29189 when using this switch, and particularly if one set of results is right,
29190 and one is wrong as far as you are concerned, it shows that you have some
29191 missing @code{Elaborate} pragmas. For the example above, we have the
29192 following output:
29193
29194 @smallexample
29195 gnatmake -f -q main
29196 main
29197 7
29198 gnatmake -f -q main -bargs -p
29199 main
29200 0
29201 @end smallexample
29202
29203 @noindent
29204 It is of course quite unlikely that both these results are correct, so
29205 it is up to you in a case like this to investigate the source of the
29206 difference, by looking at the two elaboration orders that are chosen,
29207 and figuring out which is correct, and then adding the necessary
29208 @code{Elaborate} or @code{Elaborate_All} pragmas to ensure the desired order.
29209
29210
29211
29212 @c *******************************
29213 @node Conditional Compilation
29214 @appendix Conditional Compilation
29215 @c *******************************
29216 @cindex Conditional compilation
29217
29218 @noindent
29219 It is often necessary to arrange for a single source program
29220 to serve multiple purposes, where it is compiled in different
29221 ways to achieve these different goals. Some examples of the
29222 need for this feature are
29223
29224 @itemize @bullet
29225 @item Adapting a program to a different hardware environment
29226 @item Adapting a program to a different target architecture
29227 @item Turning debugging features on and off
29228 @item Arranging for a program to compile with different compilers
29229 @end itemize
29230
29231 @noindent
29232 In C, or C++, the typical approach would be to use the preprocessor
29233 that is defined as part of the language. The Ada language does not
29234 contain such a feature. This is not an oversight, but rather a very
29235 deliberate design decision, based on the experience that overuse of
29236 the preprocessing features in C and C++ can result in programs that
29237 are extremely difficult to maintain. For example, if we have ten
29238 switches that can be on or off, this means that there are a thousand
29239 separate programs, any one of which might not even be syntactically
29240 correct, and even if syntactically correct, the resulting program
29241 might not work correctly. Testing all combinations can quickly become
29242 impossible.
29243
29244 Nevertheless, the need to tailor programs certainly exists, and in
29245 this Appendix we will discuss how this can
29246 be achieved using Ada in general, and GNAT in particular.
29247
29248 @menu
29249 * Use of Boolean Constants::
29250 * Debugging - A Special Case::
29251 * Conditionalizing Declarations::
29252 * Use of Alternative Implementations::
29253 * Preprocessing::
29254 @end menu
29255
29256 @node Use of Boolean Constants
29257 @section Use of Boolean Constants
29258
29259 @noindent
29260 In the case where the difference is simply which code
29261 sequence is executed, the cleanest solution is to use Boolean
29262 constants to control which code is executed.
29263
29264 @smallexample @c ada
29265 @group
29266 FP_Initialize_Required : constant Boolean := True;
29267 @dots{}
29268 if FP_Initialize_Required then
29269 @dots{}
29270 end if;
29271 @end group
29272 @end smallexample
29273
29274 @noindent
29275 Not only will the code inside the @code{if} statement not be executed if
29276 the constant Boolean is @code{False}, but it will also be completely
29277 deleted from the program.
29278 However, the code is only deleted after the @code{if} statement
29279 has been checked for syntactic and semantic correctness.
29280 (In contrast, with preprocessors the code is deleted before the
29281 compiler ever gets to see it, so it is not checked until the switch
29282 is turned on.)
29283 @cindex Preprocessors (contrasted with conditional compilation)
29284
29285 Typically the Boolean constants will be in a separate package,
29286 something like:
29287
29288 @smallexample @c ada
29289 @group
29290 package Config is
29291 FP_Initialize_Required : constant Boolean := True;
29292 Reset_Available : constant Boolean := False;
29293 @dots{}
29294 end Config;
29295 @end group
29296 @end smallexample
29297
29298 @noindent
29299 The @code{Config} package exists in multiple forms for the various targets,
29300 with an appropriate script selecting the version of @code{Config} needed.
29301 Then any other unit requiring conditional compilation can do a @code{with}
29302 of @code{Config} to make the constants visible.
29303
29304
29305 @node Debugging - A Special Case
29306 @section Debugging - A Special Case
29307
29308 @noindent
29309 A common use of conditional code is to execute statements (for example
29310 dynamic checks, or output of intermediate results) under control of a
29311 debug switch, so that the debugging behavior can be turned on and off.
29312 This can be done using a Boolean constant to control whether the code
29313 is active:
29314
29315 @smallexample @c ada
29316 @group
29317 if Debugging then
29318 Put_Line ("got to the first stage!");
29319 end if;
29320 @end group
29321 @end smallexample
29322
29323 @noindent
29324 or
29325
29326 @smallexample @c ada
29327 @group
29328 if Debugging and then Temperature > 999.0 then
29329 raise Temperature_Crazy;
29330 end if;
29331 @end group
29332 @end smallexample
29333
29334 @noindent
29335 Since this is a common case, there are special features to deal with
29336 this in a convenient manner. For the case of tests, Ada 2005 has added
29337 a pragma @code{Assert} that can be used for such tests. This pragma is modeled
29338 @cindex pragma @code{Assert}
29339 on the @code{Assert} pragma that has always been available in GNAT, so this
29340 feature may be used with GNAT even if you are not using Ada 2005 features.
29341 The use of pragma @code{Assert} is described in
29342 @ref{Pragma Assert,,, gnat_rm, GNAT Reference Manual}, but as an
29343 example, the last test could be written:
29344
29345 @smallexample @c ada
29346 pragma Assert (Temperature <= 999.0, "Temperature Crazy");
29347 @end smallexample
29348
29349 @noindent
29350 or simply
29351
29352 @smallexample @c ada
29353 pragma Assert (Temperature <= 999.0);
29354 @end smallexample
29355
29356 @noindent
29357 In both cases, if assertions are active and the temperature is excessive,
29358 the exception @code{Assert_Failure} will be raised, with the given string in
29359 the first case or a string indicating the location of the pragma in the second
29360 case used as the exception message.
29361
29362 You can turn assertions on and off by using the @code{Assertion_Policy}
29363 pragma.
29364 @cindex pragma @code{Assertion_Policy}
29365 This is an Ada 2005 pragma which is implemented in all modes by
29366 GNAT, but only in the latest versions of GNAT which include Ada 2005
29367 capability. Alternatively, you can use the @option{-gnata} switch
29368 @cindex @option{-gnata} switch
29369 to enable assertions from the command line (this is recognized by all versions
29370 of GNAT).
29371
29372 For the example above with the @code{Put_Line}, the GNAT-specific pragma
29373 @code{Debug} can be used:
29374 @cindex pragma @code{Debug}
29375
29376 @smallexample @c ada
29377 pragma Debug (Put_Line ("got to the first stage!"));
29378 @end smallexample
29379
29380 @noindent
29381 If debug pragmas are enabled, the argument, which must be of the form of
29382 a procedure call, is executed (in this case, @code{Put_Line} will be called).
29383 Only one call can be present, but of course a special debugging procedure
29384 containing any code you like can be included in the program and then
29385 called in a pragma @code{Debug} argument as needed.
29386
29387 One advantage of pragma @code{Debug} over the @code{if Debugging then}
29388 construct is that pragma @code{Debug} can appear in declarative contexts,
29389 such as at the very beginning of a procedure, before local declarations have
29390 been elaborated.
29391
29392 Debug pragmas are enabled using either the @option{-gnata} switch that also
29393 controls assertions, or with a separate Debug_Policy pragma.
29394 @cindex pragma @code{Debug_Policy}
29395 The latter pragma is new in the Ada 2005 versions of GNAT (but it can be used
29396 in Ada 95 and Ada 83 programs as well), and is analogous to
29397 pragma @code{Assertion_Policy} to control assertions.
29398
29399 @code{Assertion_Policy} and @code{Debug_Policy} are configuration pragmas,
29400 and thus they can appear in @file{gnat.adc} if you are not using a
29401 project file, or in the file designated to contain configuration pragmas
29402 in a project file.
29403 They then apply to all subsequent compilations. In practice the use of
29404 the @option{-gnata} switch is often the most convenient method of controlling
29405 the status of these pragmas.
29406
29407 Note that a pragma is not a statement, so in contexts where a statement
29408 sequence is required, you can't just write a pragma on its own. You have
29409 to add a @code{null} statement.
29410
29411 @smallexample @c ada
29412 @group
29413 if @dots{} then
29414 @dots{} -- some statements
29415 else
29416 pragma Assert (Num_Cases < 10);
29417 null;
29418 end if;
29419 @end group
29420 @end smallexample
29421
29422
29423 @node Conditionalizing Declarations
29424 @section Conditionalizing Declarations
29425
29426 @noindent
29427 In some cases, it may be necessary to conditionalize declarations to meet
29428 different requirements. For example we might want a bit string whose length
29429 is set to meet some hardware message requirement.
29430
29431 In some cases, it may be possible to do this using declare blocks controlled
29432 by conditional constants:
29433
29434 @smallexample @c ada
29435 @group
29436 if Small_Machine then
29437 declare
29438 X : Bit_String (1 .. 10);
29439 begin
29440 @dots{}
29441 end;
29442 else
29443 declare
29444 X : Large_Bit_String (1 .. 1000);
29445 begin
29446 @dots{}
29447 end;
29448 end if;
29449 @end group
29450 @end smallexample
29451
29452 @noindent
29453 Note that in this approach, both declarations are analyzed by the
29454 compiler so this can only be used where both declarations are legal,
29455 even though one of them will not be used.
29456
29457 Another approach is to define integer constants, e.g.@: @code{Bits_Per_Word}, or
29458 Boolean constants, e.g.@: @code{Little_Endian}, and then write declarations
29459 that are parameterized by these constants. For example
29460
29461 @smallexample @c ada
29462 @group
29463 for Rec use
29464 Field1 at 0 range Boolean'Pos (Little_Endian) * 10 .. Bits_Per_Word;
29465 end record;
29466 @end group
29467 @end smallexample
29468
29469 @noindent
29470 If @code{Bits_Per_Word} is set to 32, this generates either
29471
29472 @smallexample @c ada
29473 @group
29474 for Rec use
29475 Field1 at 0 range 0 .. 32;
29476 end record;
29477 @end group
29478 @end smallexample
29479
29480 @noindent
29481 for the big endian case, or
29482
29483 @smallexample @c ada
29484 @group
29485 for Rec use record
29486 Field1 at 0 range 10 .. 32;
29487 end record;
29488 @end group
29489 @end smallexample
29490
29491 @noindent
29492 for the little endian case. Since a powerful subset of Ada expression
29493 notation is usable for creating static constants, clever use of this
29494 feature can often solve quite difficult problems in conditionalizing
29495 compilation (note incidentally that in Ada 95, the little endian
29496 constant was introduced as @code{System.Default_Bit_Order}, so you do not
29497 need to define this one yourself).
29498
29499
29500 @node Use of Alternative Implementations
29501 @section Use of Alternative Implementations
29502
29503 @noindent
29504 In some cases, none of the approaches described above are adequate. This
29505 can occur for example if the set of declarations required is radically
29506 different for two different configurations.
29507
29508 In this situation, the official Ada way of dealing with conditionalizing
29509 such code is to write separate units for the different cases. As long as
29510 this does not result in excessive duplication of code, this can be done
29511 without creating maintenance problems. The approach is to share common
29512 code as far as possible, and then isolate the code and declarations
29513 that are different. Subunits are often a convenient method for breaking
29514 out a piece of a unit that is to be conditionalized, with separate files
29515 for different versions of the subunit for different targets, where the
29516 build script selects the right one to give to the compiler.
29517 @cindex Subunits (and conditional compilation)
29518
29519 As an example, consider a situation where a new feature in Ada 2005
29520 allows something to be done in a really nice way. But your code must be able
29521 to compile with an Ada 95 compiler. Conceptually you want to say:
29522
29523 @smallexample @c ada
29524 @group
29525 if Ada_2005 then
29526 @dots{} neat Ada 2005 code
29527 else
29528 @dots{} not quite as neat Ada 95 code
29529 end if;
29530 @end group
29531 @end smallexample
29532
29533 @noindent
29534 where @code{Ada_2005} is a Boolean constant.
29535
29536 But this won't work when @code{Ada_2005} is set to @code{False},
29537 since the @code{then} clause will be illegal for an Ada 95 compiler.
29538 (Recall that although such unreachable code would eventually be deleted
29539 by the compiler, it still needs to be legal. If it uses features
29540 introduced in Ada 2005, it will be illegal in Ada 95.)
29541
29542 So instead we write
29543
29544 @smallexample @c ada
29545 procedure Insert is separate;
29546 @end smallexample
29547
29548 @noindent
29549 Then we have two files for the subunit @code{Insert}, with the two sets of
29550 code.
29551 If the package containing this is called @code{File_Queries}, then we might
29552 have two files
29553
29554 @itemize @bullet
29555 @item @file{file_queries-insert-2005.adb}
29556 @item @file{file_queries-insert-95.adb}
29557 @end itemize
29558
29559 @noindent
29560 and the build script renames the appropriate file to
29561
29562 @smallexample
29563 file_queries-insert.adb
29564 @end smallexample
29565
29566 @noindent
29567 and then carries out the compilation.
29568
29569 This can also be done with project files' naming schemes. For example:
29570
29571 @smallexample @c project
29572 For Body ("File_Queries.Insert") use "file_queries-insert-2005.ada";
29573 @end smallexample
29574
29575 @noindent
29576 Note also that with project files it is desirable to use a different extension
29577 than @file{ads} / @file{adb} for alternative versions. Otherwise a naming
29578 conflict may arise through another commonly used feature: to declare as part
29579 of the project a set of directories containing all the sources obeying the
29580 default naming scheme.
29581
29582 The use of alternative units is certainly feasible in all situations,
29583 and for example the Ada part of the GNAT run-time is conditionalized
29584 based on the target architecture using this approach. As a specific example,
29585 consider the implementation of the AST feature in VMS. There is one
29586 spec:
29587
29588 @smallexample
29589 s-asthan.ads
29590 @end smallexample
29591
29592 @noindent
29593 which is the same for all architectures, and three bodies:
29594
29595 @table @file
29596 @item s-asthan.adb
29597 used for all non-VMS operating systems
29598 @item s-asthan-vms-alpha.adb
29599 used for VMS on the Alpha
29600 @item s-asthan-vms-ia64.adb
29601 used for VMS on the ia64
29602 @end table
29603
29604 @noindent
29605 The dummy version @file{s-asthan.adb} simply raises exceptions noting that
29606 this operating system feature is not available, and the two remaining
29607 versions interface with the corresponding versions of VMS to provide
29608 VMS-compatible AST handling. The GNAT build script knows the architecture
29609 and operating system, and automatically selects the right version,
29610 renaming it if necessary to @file{s-asthan.adb} before the run-time build.
29611
29612 Another style for arranging alternative implementations is through Ada's
29613 access-to-subprogram facility.
29614 In case some functionality is to be conditionally included,
29615 you can declare an access-to-procedure variable @code{Ref} that is initialized
29616 to designate a ``do nothing'' procedure, and then invoke @code{Ref.all}
29617 when appropriate.
29618 In some library package, set @code{Ref} to @code{Proc'Access} for some
29619 procedure @code{Proc} that performs the relevant processing.
29620 The initialization only occurs if the library package is included in the
29621 program.
29622 The same idea can also be implemented using tagged types and dispatching
29623 calls.
29624
29625
29626 @node Preprocessing
29627 @section Preprocessing
29628 @cindex Preprocessing
29629
29630 @noindent
29631 Although it is quite possible to conditionalize code without the use of
29632 C-style preprocessing, as described earlier in this section, it is
29633 nevertheless convenient in some cases to use the C approach. Moreover,
29634 older Ada compilers have often provided some preprocessing capability,
29635 so legacy code may depend on this approach, even though it is not
29636 standard.
29637
29638 To accommodate such use, GNAT provides a preprocessor (modeled to a large
29639 extent on the various preprocessors that have been used
29640 with legacy code on other compilers, to enable easier transition).
29641
29642 The preprocessor may be used in two separate modes. It can be used quite
29643 separately from the compiler, to generate a separate output source file
29644 that is then fed to the compiler as a separate step. This is the
29645 @code{gnatprep} utility, whose use is fully described in
29646 @ref{Preprocessing Using gnatprep}.
29647 @cindex @code{gnatprep}
29648
29649 The preprocessing language allows such constructs as
29650
29651 @smallexample
29652 @group
29653 #if DEBUG or PRIORITY > 4 then
29654 bunch of declarations
29655 #else
29656 completely different bunch of declarations
29657 #end if;
29658 @end group
29659 @end smallexample
29660
29661 @noindent
29662 The values of the symbols @code{DEBUG} and @code{PRIORITY} can be
29663 defined either on the command line or in a separate file.
29664
29665 The other way of running the preprocessor is even closer to the C style and
29666 often more convenient. In this approach the preprocessing is integrated into
29667 the compilation process. The compiler is fed the preprocessor input which
29668 includes @code{#if} lines etc, and then the compiler carries out the
29669 preprocessing internally and processes the resulting output.
29670 For more details on this approach, see @ref{Integrated Preprocessing}.
29671
29672
29673 @c *******************************
29674 @node Inline Assembler
29675 @appendix Inline Assembler
29676 @c *******************************
29677
29678 @noindent
29679 If you need to write low-level software that interacts directly
29680 with the hardware, Ada provides two ways to incorporate assembly
29681 language code into your program. First, you can import and invoke
29682 external routines written in assembly language, an Ada feature fully
29683 supported by GNAT@. However, for small sections of code it may be simpler
29684 or more efficient to include assembly language statements directly
29685 in your Ada source program, using the facilities of the implementation-defined
29686 package @code{System.Machine_Code}, which incorporates the gcc
29687 Inline Assembler. The Inline Assembler approach offers a number of advantages,
29688 including the following:
29689
29690 @itemize @bullet
29691 @item No need to use non-Ada tools
29692 @item Consistent interface over different targets
29693 @item Automatic usage of the proper calling conventions
29694 @item Access to Ada constants and variables
29695 @item Definition of intrinsic routines
29696 @item Possibility of inlining a subprogram comprising assembler code
29697 @item Code optimizer can take Inline Assembler code into account
29698 @end itemize
29699
29700 This chapter presents a series of examples to show you how to use
29701 the Inline Assembler. Although it focuses on the Intel x86,
29702 the general approach applies also to other processors.
29703 It is assumed that you are familiar with Ada
29704 and with assembly language programming.
29705
29706 @menu
29707 * Basic Assembler Syntax::
29708 * A Simple Example of Inline Assembler::
29709 * Output Variables in Inline Assembler::
29710 * Input Variables in Inline Assembler::
29711 * Inlining Inline Assembler Code::
29712 * Other Asm Functionality::
29713 @end menu
29714
29715 @c ---------------------------------------------------------------------------
29716 @node Basic Assembler Syntax
29717 @section Basic Assembler Syntax
29718
29719 @noindent
29720 The assembler used by GNAT and gcc is based not on the Intel assembly
29721 language, but rather on a language that descends from the AT&T Unix
29722 assembler @emph{as} (and which is often referred to as ``AT&T syntax'').
29723 The following table summarizes the main features of @emph{as} syntax
29724 and points out the differences from the Intel conventions.
29725 See the gcc @emph{as} and @emph{gas} (an @emph{as} macro
29726 pre-processor) documentation for further information.
29727
29728 @table @asis
29729 @item Register names
29730 gcc / @emph{as}: Prefix with ``%''; for example @code{%eax}
29731 @*
29732 Intel: No extra punctuation; for example @code{eax}
29733
29734 @item Immediate operand
29735 gcc / @emph{as}: Prefix with ``$''; for example @code{$4}
29736 @*
29737 Intel: No extra punctuation; for example @code{4}
29738
29739 @item Address
29740 gcc / @emph{as}: Prefix with ``$''; for example @code{$loc}
29741 @*
29742 Intel: No extra punctuation; for example @code{loc}
29743
29744 @item Memory contents
29745 gcc / @emph{as}: No extra punctuation; for example @code{loc}
29746 @*
29747 Intel: Square brackets; for example @code{[loc]}
29748
29749 @item Register contents
29750 gcc / @emph{as}: Parentheses; for example @code{(%eax)}
29751 @*
29752 Intel: Square brackets; for example @code{[eax]}
29753
29754 @item Hexadecimal numbers
29755 gcc / @emph{as}: Leading ``0x'' (C language syntax); for example @code{0xA0}
29756 @*
29757 Intel: Trailing ``h''; for example @code{A0h}
29758
29759 @item Operand size
29760 gcc / @emph{as}: Explicit in op code; for example @code{movw} to move
29761 a 16-bit word
29762 @*
29763 Intel: Implicit, deduced by assembler; for example @code{mov}
29764
29765 @item Instruction repetition
29766 gcc / @emph{as}: Split into two lines; for example
29767 @*
29768 @code{rep}
29769 @*
29770 @code{stosl}
29771 @*
29772 Intel: Keep on one line; for example @code{rep stosl}
29773
29774 @item Order of operands
29775 gcc / @emph{as}: Source first; for example @code{movw $4, %eax}
29776 @*
29777 Intel: Destination first; for example @code{mov eax, 4}
29778 @end table
29779
29780 @c ---------------------------------------------------------------------------
29781 @node A Simple Example of Inline Assembler
29782 @section A Simple Example of Inline Assembler
29783
29784 @noindent
29785 The following example will generate a single assembly language statement,
29786 @code{nop}, which does nothing. Despite its lack of run-time effect,
29787 the example will be useful in illustrating the basics of
29788 the Inline Assembler facility.
29789
29790 @smallexample @c ada
29791 @group
29792 with System.Machine_Code; use System.Machine_Code;
29793 procedure Nothing is
29794 begin
29795 Asm ("nop");
29796 end Nothing;
29797 @end group
29798 @end smallexample
29799
29800 @code{Asm} is a procedure declared in package @code{System.Machine_Code};
29801 here it takes one parameter, a @emph{template string} that must be a static
29802 expression and that will form the generated instruction.
29803 @code{Asm} may be regarded as a compile-time procedure that parses
29804 the template string and additional parameters (none here),
29805 from which it generates a sequence of assembly language instructions.
29806
29807 The examples in this chapter will illustrate several of the forms
29808 for invoking @code{Asm}; a complete specification of the syntax
29809 is found in @ref{Machine Code Insertions,,, gnat_rm, GNAT Reference
29810 Manual}.
29811
29812 Under the standard GNAT conventions, the @code{Nothing} procedure
29813 should be in a file named @file{nothing.adb}.
29814 You can build the executable in the usual way:
29815 @smallexample
29816 gnatmake nothing
29817 @end smallexample
29818 However, the interesting aspect of this example is not its run-time behavior
29819 but rather the generated assembly code.
29820 To see this output, invoke the compiler as follows:
29821 @smallexample
29822 gcc -c -S -fomit-frame-pointer -gnatp @file{nothing.adb}
29823 @end smallexample
29824 where the options are:
29825
29826 @table @code
29827 @item -c
29828 compile only (no bind or link)
29829 @item -S
29830 generate assembler listing
29831 @item -fomit-frame-pointer
29832 do not set up separate stack frames
29833 @item -gnatp
29834 do not add runtime checks
29835 @end table
29836
29837 This gives a human-readable assembler version of the code. The resulting
29838 file will have the same name as the Ada source file, but with a @code{.s}
29839 extension. In our example, the file @file{nothing.s} has the following
29840 contents:
29841
29842 @smallexample
29843 @group
29844 .file "nothing.adb"
29845 gcc2_compiled.:
29846 ___gnu_compiled_ada:
29847 .text
29848 .align 4
29849 .globl __ada_nothing
29850 __ada_nothing:
29851 #APP
29852 nop
29853 #NO_APP
29854 jmp L1
29855 .align 2,0x90
29856 L1:
29857 ret
29858 @end group
29859 @end smallexample
29860
29861 The assembly code you included is clearly indicated by
29862 the compiler, between the @code{#APP} and @code{#NO_APP}
29863 delimiters. The character before the 'APP' and 'NOAPP'
29864 can differ on different targets. For example, GNU/Linux uses '#APP' while
29865 on NT you will see '/APP'.
29866
29867 If you make a mistake in your assembler code (such as using the
29868 wrong size modifier, or using a wrong operand for the instruction) GNAT
29869 will report this error in a temporary file, which will be deleted when
29870 the compilation is finished. Generating an assembler file will help
29871 in such cases, since you can assemble this file separately using the
29872 @emph{as} assembler that comes with gcc.
29873
29874 Assembling the file using the command
29875
29876 @smallexample
29877 as @file{nothing.s}
29878 @end smallexample
29879 @noindent
29880 will give you error messages whose lines correspond to the assembler
29881 input file, so you can easily find and correct any mistakes you made.
29882 If there are no errors, @emph{as} will generate an object file
29883 @file{nothing.out}.
29884
29885 @c ---------------------------------------------------------------------------
29886 @node Output Variables in Inline Assembler
29887 @section Output Variables in Inline Assembler
29888
29889 @noindent
29890 The examples in this section, showing how to access the processor flags,
29891 illustrate how to specify the destination operands for assembly language
29892 statements.
29893
29894 @smallexample @c ada
29895 @group
29896 with Interfaces; use Interfaces;
29897 with Ada.Text_IO; use Ada.Text_IO;
29898 with System.Machine_Code; use System.Machine_Code;
29899 procedure Get_Flags is
29900 Flags : Unsigned_32;
29901 use ASCII;
29902 begin
29903 Asm ("pushfl" & LF & HT & -- push flags on stack
29904 "popl %%eax" & LF & HT & -- load eax with flags
29905 "movl %%eax, %0", -- store flags in variable
29906 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29907 Put_Line ("Flags register:" & Flags'Img);
29908 end Get_Flags;
29909 @end group
29910 @end smallexample
29911
29912 In order to have a nicely aligned assembly listing, we have separated
29913 multiple assembler statements in the Asm template string with linefeed
29914 (ASCII.LF) and horizontal tab (ASCII.HT) characters.
29915 The resulting section of the assembly output file is:
29916
29917 @smallexample
29918 @group
29919 #APP
29920 pushfl
29921 popl %eax
29922 movl %eax, -40(%ebp)
29923 #NO_APP
29924 @end group
29925 @end smallexample
29926
29927 It would have been legal to write the Asm invocation as:
29928
29929 @smallexample
29930 Asm ("pushfl popl %%eax movl %%eax, %0")
29931 @end smallexample
29932
29933 but in the generated assembler file, this would come out as:
29934
29935 @smallexample
29936 #APP
29937 pushfl popl %eax movl %eax, -40(%ebp)
29938 #NO_APP
29939 @end smallexample
29940
29941 which is not so convenient for the human reader.
29942
29943 We use Ada comments
29944 at the end of each line to explain what the assembler instructions
29945 actually do. This is a useful convention.
29946
29947 When writing Inline Assembler instructions, you need to precede each register
29948 and variable name with a percent sign. Since the assembler already requires
29949 a percent sign at the beginning of a register name, you need two consecutive
29950 percent signs for such names in the Asm template string, thus @code{%%eax}.
29951 In the generated assembly code, one of the percent signs will be stripped off.
29952
29953 Names such as @code{%0}, @code{%1}, @code{%2}, etc., denote input or output
29954 variables: operands you later define using @code{Input} or @code{Output}
29955 parameters to @code{Asm}.
29956 An output variable is illustrated in
29957 the third statement in the Asm template string:
29958 @smallexample
29959 movl %%eax, %0
29960 @end smallexample
29961 The intent is to store the contents of the eax register in a variable that can
29962 be accessed in Ada. Simply writing @code{movl %%eax, Flags} would not
29963 necessarily work, since the compiler might optimize by using a register
29964 to hold Flags, and the expansion of the @code{movl} instruction would not be
29965 aware of this optimization. The solution is not to store the result directly
29966 but rather to advise the compiler to choose the correct operand form;
29967 that is the purpose of the @code{%0} output variable.
29968
29969 Information about the output variable is supplied in the @code{Outputs}
29970 parameter to @code{Asm}:
29971 @smallexample
29972 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29973 @end smallexample
29974
29975 The output is defined by the @code{Asm_Output} attribute of the target type;
29976 the general format is
29977 @smallexample
29978 Type'Asm_Output (constraint_string, variable_name)
29979 @end smallexample
29980
29981 The constraint string directs the compiler how
29982 to store/access the associated variable. In the example
29983 @smallexample
29984 Unsigned_32'Asm_Output ("=m", Flags);
29985 @end smallexample
29986 the @code{"m"} (memory) constraint tells the compiler that the variable
29987 @code{Flags} should be stored in a memory variable, thus preventing
29988 the optimizer from keeping it in a register. In contrast,
29989 @smallexample
29990 Unsigned_32'Asm_Output ("=r", Flags);
29991 @end smallexample
29992 uses the @code{"r"} (register) constraint, telling the compiler to
29993 store the variable in a register.
29994
29995 If the constraint is preceded by the equal character (@strong{=}), it tells
29996 the compiler that the variable will be used to store data into it.
29997
29998 In the @code{Get_Flags} example, we used the @code{"g"} (global) constraint,
29999 allowing the optimizer to choose whatever it deems best.
30000
30001 There are a fairly large number of constraints, but the ones that are
30002 most useful (for the Intel x86 processor) are the following:
30003
30004 @table @code
30005 @item =
30006 output constraint
30007 @item g
30008 global (i.e.@: can be stored anywhere)
30009 @item m
30010 in memory
30011 @item I
30012 a constant
30013 @item a
30014 use eax
30015 @item b
30016 use ebx
30017 @item c
30018 use ecx
30019 @item d
30020 use edx
30021 @item S
30022 use esi
30023 @item D
30024 use edi
30025 @item r
30026 use one of eax, ebx, ecx or edx
30027 @item q
30028 use one of eax, ebx, ecx, edx, esi or edi
30029 @end table
30030
30031 The full set of constraints is described in the gcc and @emph{as}
30032 documentation; note that it is possible to combine certain constraints
30033 in one constraint string.
30034
30035 You specify the association of an output variable with an assembler operand
30036 through the @code{%}@emph{n} notation, where @emph{n} is a non-negative
30037 integer. Thus in
30038 @smallexample @c ada
30039 @group
30040 Asm ("pushfl" & LF & HT & -- push flags on stack
30041 "popl %%eax" & LF & HT & -- load eax with flags
30042 "movl %%eax, %0", -- store flags in variable
30043 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
30044 @end group
30045 @end smallexample
30046 @noindent
30047 @code{%0} will be replaced in the expanded code by the appropriate operand,
30048 whatever
30049 the compiler decided for the @code{Flags} variable.
30050
30051 In general, you may have any number of output variables:
30052 @itemize @bullet
30053 @item
30054 Count the operands starting at 0; thus @code{%0}, @code{%1}, etc.
30055 @item
30056 Specify the @code{Outputs} parameter as a parenthesized comma-separated list
30057 of @code{Asm_Output} attributes
30058 @end itemize
30059
30060 For example:
30061 @smallexample @c ada
30062 @group
30063 Asm ("movl %%eax, %0" & LF & HT &
30064 "movl %%ebx, %1" & LF & HT &
30065 "movl %%ecx, %2",
30066 Outputs => (Unsigned_32'Asm_Output ("=g", Var_A), -- %0 = Var_A
30067 Unsigned_32'Asm_Output ("=g", Var_B), -- %1 = Var_B
30068 Unsigned_32'Asm_Output ("=g", Var_C))); -- %2 = Var_C
30069 @end group
30070 @end smallexample
30071 @noindent
30072 where @code{Var_A}, @code{Var_B}, and @code{Var_C} are variables
30073 in the Ada program.
30074
30075 As a variation on the @code{Get_Flags} example, we can use the constraints
30076 string to direct the compiler to store the eax register into the @code{Flags}
30077 variable, instead of including the store instruction explicitly in the
30078 @code{Asm} template string:
30079
30080 @smallexample @c ada
30081 @group
30082 with Interfaces; use Interfaces;
30083 with Ada.Text_IO; use Ada.Text_IO;
30084 with System.Machine_Code; use System.Machine_Code;
30085 procedure Get_Flags_2 is
30086 Flags : Unsigned_32;
30087 use ASCII;
30088 begin
30089 Asm ("pushfl" & LF & HT & -- push flags on stack
30090 "popl %%eax", -- save flags in eax
30091 Outputs => Unsigned_32'Asm_Output ("=a", Flags));
30092 Put_Line ("Flags register:" & Flags'Img);
30093 end Get_Flags_2;
30094 @end group
30095 @end smallexample
30096
30097 @noindent
30098 The @code{"a"} constraint tells the compiler that the @code{Flags}
30099 variable will come from the eax register. Here is the resulting code:
30100
30101 @smallexample
30102 @group
30103 #APP
30104 pushfl
30105 popl %eax
30106 #NO_APP
30107 movl %eax,-40(%ebp)
30108 @end group
30109 @end smallexample
30110
30111 @noindent
30112 The compiler generated the store of eax into Flags after
30113 expanding the assembler code.
30114
30115 Actually, there was no need to pop the flags into the eax register;
30116 more simply, we could just pop the flags directly into the program variable:
30117
30118 @smallexample @c ada
30119 @group
30120 with Interfaces; use Interfaces;
30121 with Ada.Text_IO; use Ada.Text_IO;
30122 with System.Machine_Code; use System.Machine_Code;
30123 procedure Get_Flags_3 is
30124 Flags : Unsigned_32;
30125 use ASCII;
30126 begin
30127 Asm ("pushfl" & LF & HT & -- push flags on stack
30128 "pop %0", -- save flags in Flags
30129 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
30130 Put_Line ("Flags register:" & Flags'Img);
30131 end Get_Flags_3;
30132 @end group
30133 @end smallexample
30134
30135 @c ---------------------------------------------------------------------------
30136 @node Input Variables in Inline Assembler
30137 @section Input Variables in Inline Assembler
30138
30139 @noindent
30140 The example in this section illustrates how to specify the source operands
30141 for assembly language statements.
30142 The program simply increments its input value by 1:
30143
30144 @smallexample @c ada
30145 @group
30146 with Interfaces; use Interfaces;
30147 with Ada.Text_IO; use Ada.Text_IO;
30148 with System.Machine_Code; use System.Machine_Code;
30149 procedure Increment is
30150
30151 function Incr (Value : Unsigned_32) return Unsigned_32 is
30152 Result : Unsigned_32;
30153 begin
30154 Asm ("incl %0",
30155 Inputs => Unsigned_32'Asm_Input ("a", Value),
30156 Outputs => Unsigned_32'Asm_Output ("=a", Result));
30157 return Result;
30158 end Incr;
30159
30160 Value : Unsigned_32;
30161
30162 begin
30163 Value := 5;
30164 Put_Line ("Value before is" & Value'Img);
30165 Value := Incr (Value);
30166 Put_Line ("Value after is" & Value'Img);
30167 end Increment;
30168 @end group
30169 @end smallexample
30170
30171 The @code{Outputs} parameter to @code{Asm} specifies
30172 that the result will be in the eax register and that it is to be stored
30173 in the @code{Result} variable.
30174
30175 The @code{Inputs} parameter looks much like the @code{Outputs} parameter,
30176 but with an @code{Asm_Input} attribute.
30177 The @code{"="} constraint, indicating an output value, is not present.
30178
30179 You can have multiple input variables, in the same way that you can have more
30180 than one output variable.
30181
30182 The parameter count (%0, %1) etc, now starts at the first input
30183 statement, and continues with the output statements.
30184 When both parameters use the same variable, the
30185 compiler will treat them as the same %n operand, which is the case here.
30186
30187 Just as the @code{Outputs} parameter causes the register to be stored into the
30188 target variable after execution of the assembler statements, so does the
30189 @code{Inputs} parameter cause its variable to be loaded into the register
30190 before execution of the assembler statements.
30191
30192 Thus the effect of the @code{Asm} invocation is:
30193 @enumerate
30194 @item load the 32-bit value of @code{Value} into eax
30195 @item execute the @code{incl %eax} instruction
30196 @item store the contents of eax into the @code{Result} variable
30197 @end enumerate
30198
30199 The resulting assembler file (with @option{-O2} optimization) contains:
30200 @smallexample
30201 @group
30202 _increment__incr.1:
30203 subl $4,%esp
30204 movl 8(%esp),%eax
30205 #APP
30206 incl %eax
30207 #NO_APP
30208 movl %eax,%edx
30209 movl %ecx,(%esp)
30210 addl $4,%esp
30211 ret
30212 @end group
30213 @end smallexample
30214
30215 @c ---------------------------------------------------------------------------
30216 @node Inlining Inline Assembler Code
30217 @section Inlining Inline Assembler Code
30218
30219 @noindent
30220 For a short subprogram such as the @code{Incr} function in the previous
30221 section, the overhead of the call and return (creating / deleting the stack
30222 frame) can be significant, compared to the amount of code in the subprogram
30223 body. A solution is to apply Ada's @code{Inline} pragma to the subprogram,
30224 which directs the compiler to expand invocations of the subprogram at the
30225 point(s) of call, instead of setting up a stack frame for out-of-line calls.
30226 Here is the resulting program:
30227
30228 @smallexample @c ada
30229 @group
30230 with Interfaces; use Interfaces;
30231 with Ada.Text_IO; use Ada.Text_IO;
30232 with System.Machine_Code; use System.Machine_Code;
30233 procedure Increment_2 is
30234
30235 function Incr (Value : Unsigned_32) return Unsigned_32 is
30236 Result : Unsigned_32;
30237 begin
30238 Asm ("incl %0",
30239 Inputs => Unsigned_32'Asm_Input ("a", Value),
30240 Outputs => Unsigned_32'Asm_Output ("=a", Result));
30241 return Result;
30242 end Incr;
30243 pragma Inline (Increment);
30244
30245 Value : Unsigned_32;
30246
30247 begin
30248 Value := 5;
30249 Put_Line ("Value before is" & Value'Img);
30250 Value := Increment (Value);
30251 Put_Line ("Value after is" & Value'Img);
30252 end Increment_2;
30253 @end group
30254 @end smallexample
30255
30256 Compile the program with both optimization (@option{-O2}) and inlining
30257 (@option{-gnatn}) enabled.
30258
30259 The @code{Incr} function is still compiled as usual, but at the
30260 point in @code{Increment} where our function used to be called:
30261
30262 @smallexample
30263 @group
30264 pushl %edi
30265 call _increment__incr.1
30266 @end group
30267 @end smallexample
30268
30269 @noindent
30270 the code for the function body directly appears:
30271
30272 @smallexample
30273 @group
30274 movl %esi,%eax
30275 #APP
30276 incl %eax
30277 #NO_APP
30278 movl %eax,%edx
30279 @end group
30280 @end smallexample
30281
30282 @noindent
30283 thus saving the overhead of stack frame setup and an out-of-line call.
30284
30285 @c ---------------------------------------------------------------------------
30286 @node Other Asm Functionality
30287 @section Other @code{Asm} Functionality
30288
30289 @noindent
30290 This section describes two important parameters to the @code{Asm}
30291 procedure: @code{Clobber}, which identifies register usage;
30292 and @code{Volatile}, which inhibits unwanted optimizations.
30293
30294 @menu
30295 * The Clobber Parameter::
30296 * The Volatile Parameter::
30297 @end menu
30298
30299 @c ---------------------------------------------------------------------------
30300 @node The Clobber Parameter
30301 @subsection The @code{Clobber} Parameter
30302
30303 @noindent
30304 One of the dangers of intermixing assembly language and a compiled language
30305 such as Ada is that the compiler needs to be aware of which registers are
30306 being used by the assembly code. In some cases, such as the earlier examples,
30307 the constraint string is sufficient to indicate register usage (e.g.,
30308 @code{"a"} for
30309 the eax register). But more generally, the compiler needs an explicit
30310 identification of the registers that are used by the Inline Assembly
30311 statements.
30312
30313 Using a register that the compiler doesn't know about
30314 could be a side effect of an instruction (like @code{mull}
30315 storing its result in both eax and edx).
30316 It can also arise from explicit register usage in your
30317 assembly code; for example:
30318 @smallexample
30319 @group
30320 Asm ("movl %0, %%ebx" & LF & HT &
30321 "movl %%ebx, %1",
30322 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
30323 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out));
30324 @end group
30325 @end smallexample
30326 @noindent
30327 where the compiler (since it does not analyze the @code{Asm} template string)
30328 does not know you are using the ebx register.
30329
30330 In such cases you need to supply the @code{Clobber} parameter to @code{Asm},
30331 to identify the registers that will be used by your assembly code:
30332
30333 @smallexample
30334 @group
30335 Asm ("movl %0, %%ebx" & LF & HT &
30336 "movl %%ebx, %1",
30337 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
30338 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
30339 Clobber => "ebx");
30340 @end group
30341 @end smallexample
30342
30343 The Clobber parameter is a static string expression specifying the
30344 register(s) you are using. Note that register names are @emph{not} prefixed
30345 by a percent sign. Also, if more than one register is used then their names
30346 are separated by commas; e.g., @code{"eax, ebx"}
30347
30348 The @code{Clobber} parameter has several additional uses:
30349 @enumerate
30350 @item Use ``register'' name @code{cc} to indicate that flags might have changed
30351 @item Use ``register'' name @code{memory} if you changed a memory location
30352 @end enumerate
30353
30354 @c ---------------------------------------------------------------------------
30355 @node The Volatile Parameter
30356 @subsection The @code{Volatile} Parameter
30357 @cindex Volatile parameter
30358
30359 @noindent
30360 Compiler optimizations in the presence of Inline Assembler may sometimes have
30361 unwanted effects. For example, when an @code{Asm} invocation with an input
30362 variable is inside a loop, the compiler might move the loading of the input
30363 variable outside the loop, regarding it as a one-time initialization.
30364
30365 If this effect is not desired, you can disable such optimizations by setting
30366 the @code{Volatile} parameter to @code{True}; for example:
30367
30368 @smallexample @c ada
30369 @group
30370 Asm ("movl %0, %%ebx" & LF & HT &
30371 "movl %%ebx, %1",
30372 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
30373 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
30374 Clobber => "ebx",
30375 Volatile => True);
30376 @end group
30377 @end smallexample
30378
30379 By default, @code{Volatile} is set to @code{False} unless there is no
30380 @code{Outputs} parameter.
30381
30382 Although setting @code{Volatile} to @code{True} prevents unwanted
30383 optimizations, it will also disable other optimizations that might be
30384 important for efficiency. In general, you should set @code{Volatile}
30385 to @code{True} only if the compiler's optimizations have created
30386 problems.
30387 @c END OF INLINE ASSEMBLER CHAPTER
30388 @c ===============================
30389
30390 @c ***********************************
30391 @c * Compatibility and Porting Guide *
30392 @c ***********************************
30393 @node Compatibility and Porting Guide
30394 @appendix Compatibility and Porting Guide
30395
30396 @noindent
30397 This chapter describes the compatibility issues that may arise between
30398 GNAT and other Ada compilation systems (including those for Ada 83),
30399 and shows how GNAT can expedite porting
30400 applications developed in other Ada environments.
30401
30402 @menu
30403 * Compatibility with Ada 83::
30404 * Compatibility between Ada 95 and Ada 2005::
30405 * Implementation-dependent characteristics::
30406 * Compatibility with Other Ada Systems::
30407 * Representation Clauses::
30408 @ifclear vms
30409 @c Brief section is only in non-VMS version
30410 @c Full chapter is in VMS version
30411 * Compatibility with HP Ada 83::
30412 @end ifclear
30413 @ifset vms
30414 * Transitioning to 64-Bit GNAT for OpenVMS::
30415 @end ifset
30416 @end menu
30417
30418 @node Compatibility with Ada 83
30419 @section Compatibility with Ada 83
30420 @cindex Compatibility (between Ada 83 and Ada 95 / Ada 2005)
30421
30422 @noindent
30423 Ada 95 and Ada 2005 are highly upwards compatible with Ada 83. In
30424 particular, the design intention was that the difficulties associated
30425 with moving from Ada 83 to Ada 95 or Ada 2005 should be no greater than those
30426 that occur when moving from one Ada 83 system to another.
30427
30428 However, there are a number of points at which there are minor
30429 incompatibilities. The @cite{Ada 95 Annotated Reference Manual} contains
30430 full details of these issues,
30431 and should be consulted for a complete treatment.
30432 In practice the
30433 following subsections treat the most likely issues to be encountered.
30434
30435 @menu
30436 * Legal Ada 83 programs that are illegal in Ada 95::
30437 * More deterministic semantics::
30438 * Changed semantics::
30439 * Other language compatibility issues::
30440 @end menu
30441
30442 @node Legal Ada 83 programs that are illegal in Ada 95
30443 @subsection Legal Ada 83 programs that are illegal in Ada 95
30444
30445 Some legal Ada 83 programs are illegal (i.e., they will fail to compile) in
30446 Ada 95 and thus also in Ada 2005:
30447
30448 @table @emph
30449 @item Character literals
30450 Some uses of character literals are ambiguous. Since Ada 95 has introduced
30451 @code{Wide_Character} as a new predefined character type, some uses of
30452 character literals that were legal in Ada 83 are illegal in Ada 95.
30453 For example:
30454 @smallexample @c ada
30455 for Char in 'A' .. 'Z' loop @dots{} end loop;
30456 @end smallexample
30457
30458 @noindent
30459 The problem is that @code{'A'} and @code{'Z'} could be from either
30460 @code{Character} or @code{Wide_Character}. The simplest correction
30461 is to make the type explicit; e.g.:
30462 @smallexample @c ada
30463 for Char in Character range 'A' .. 'Z' loop @dots{} end loop;
30464 @end smallexample
30465
30466 @item New reserved words
30467 The identifiers @code{abstract}, @code{aliased}, @code{protected},
30468 @code{requeue}, @code{tagged}, and @code{until} are reserved in Ada 95.
30469 Existing Ada 83 code using any of these identifiers must be edited to
30470 use some alternative name.
30471
30472 @item Freezing rules
30473 The rules in Ada 95 are slightly different with regard to the point at
30474 which entities are frozen, and representation pragmas and clauses are
30475 not permitted past the freeze point. This shows up most typically in
30476 the form of an error message complaining that a representation item
30477 appears too late, and the appropriate corrective action is to move
30478 the item nearer to the declaration of the entity to which it refers.
30479
30480 A particular case is that representation pragmas
30481 @ifset vms
30482 (including the
30483 extended HP Ada 83 compatibility pragmas such as @code{Export_Procedure})
30484 @end ifset
30485 cannot be applied to a subprogram body. If necessary, a separate subprogram
30486 declaration must be introduced to which the pragma can be applied.
30487
30488 @item Optional bodies for library packages
30489 In Ada 83, a package that did not require a package body was nevertheless
30490 allowed to have one. This lead to certain surprises in compiling large
30491 systems (situations in which the body could be unexpectedly ignored by the
30492 binder). In Ada 95, if a package does not require a body then it is not
30493 permitted to have a body. To fix this problem, simply remove a redundant
30494 body if it is empty, or, if it is non-empty, introduce a dummy declaration
30495 into the spec that makes the body required. One approach is to add a private
30496 part to the package declaration (if necessary), and define a parameterless
30497 procedure called @code{Requires_Body}, which must then be given a dummy
30498 procedure body in the package body, which then becomes required.
30499 Another approach (assuming that this does not introduce elaboration
30500 circularities) is to add an @code{Elaborate_Body} pragma to the package spec,
30501 since one effect of this pragma is to require the presence of a package body.
30502
30503 @item @code{Numeric_Error} is now the same as @code{Constraint_Error}
30504 In Ada 95, the exception @code{Numeric_Error} is a renaming of
30505 @code{Constraint_Error}.
30506 This means that it is illegal to have separate exception handlers for
30507 the two exceptions. The fix is simply to remove the handler for the
30508 @code{Numeric_Error} case (since even in Ada 83, a compiler was free to raise
30509 @code{Constraint_Error} in place of @code{Numeric_Error} in all cases).
30510
30511 @item Indefinite subtypes in generics
30512 In Ada 83, it was permissible to pass an indefinite type (e.g.@: @code{String})
30513 as the actual for a generic formal private type, but then the instantiation
30514 would be illegal if there were any instances of declarations of variables
30515 of this type in the generic body. In Ada 95, to avoid this clear violation
30516 of the methodological principle known as the ``contract model'',
30517 the generic declaration explicitly indicates whether
30518 or not such instantiations are permitted. If a generic formal parameter
30519 has explicit unknown discriminants, indicated by using @code{(<>)} after the
30520 type name, then it can be instantiated with indefinite types, but no
30521 stand-alone variables can be declared of this type. Any attempt to declare
30522 such a variable will result in an illegality at the time the generic is
30523 declared. If the @code{(<>)} notation is not used, then it is illegal
30524 to instantiate the generic with an indefinite type.
30525 This is the potential incompatibility issue when porting Ada 83 code to Ada 95.
30526 It will show up as a compile time error, and
30527 the fix is usually simply to add the @code{(<>)} to the generic declaration.
30528 @end table
30529
30530 @node More deterministic semantics
30531 @subsection More deterministic semantics
30532
30533 @table @emph
30534 @item Conversions
30535 Conversions from real types to integer types round away from 0. In Ada 83
30536 the conversion Integer(2.5) could deliver either 2 or 3 as its value. This
30537 implementation freedom was intended to support unbiased rounding in
30538 statistical applications, but in practice it interfered with portability.
30539 In Ada 95 the conversion semantics are unambiguous, and rounding away from 0
30540 is required. Numeric code may be affected by this change in semantics.
30541 Note, though, that this issue is no worse than already existed in Ada 83
30542 when porting code from one vendor to another.
30543
30544 @item Tasking
30545 The Real-Time Annex introduces a set of policies that define the behavior of
30546 features that were implementation dependent in Ada 83, such as the order in
30547 which open select branches are executed.
30548 @end table
30549
30550 @node Changed semantics
30551 @subsection Changed semantics
30552
30553 @noindent
30554 The worst kind of incompatibility is one where a program that is legal in
30555 Ada 83 is also legal in Ada 95 but can have an effect in Ada 95 that was not
30556 possible in Ada 83. Fortunately this is extremely rare, but the one
30557 situation that you should be alert to is the change in the predefined type
30558 @code{Character} from 7-bit ASCII to 8-bit Latin-1.
30559
30560 @table @emph
30561 @item Range of type @code{Character}
30562 The range of @code{Standard.Character} is now the full 256 characters
30563 of Latin-1, whereas in most Ada 83 implementations it was restricted
30564 to 128 characters. Although some of the effects of
30565 this change will be manifest in compile-time rejection of legal
30566 Ada 83 programs it is possible for a working Ada 83 program to have
30567 a different effect in Ada 95, one that was not permitted in Ada 83.
30568 As an example, the expression
30569 @code{Character'Pos(Character'Last)} returned @code{127} in Ada 83 and now
30570 delivers @code{255} as its value.
30571 In general, you should look at the logic of any
30572 character-processing Ada 83 program and see whether it needs to be adapted
30573 to work correctly with Latin-1. Note that the predefined Ada 95 API has a
30574 character handling package that may be relevant if code needs to be adapted
30575 to account for the additional Latin-1 elements.
30576 The desirable fix is to
30577 modify the program to accommodate the full character set, but in some cases
30578 it may be convenient to define a subtype or derived type of Character that
30579 covers only the restricted range.
30580 @cindex Latin-1
30581 @end table
30582
30583 @node Other language compatibility issues
30584 @subsection Other language compatibility issues
30585
30586 @table @emph
30587 @item @option{-gnat83} switch
30588 All implementations of GNAT provide a switch that causes GNAT to operate
30589 in Ada 83 mode. In this mode, some but not all compatibility problems
30590 of the type described above are handled automatically. For example, the
30591 new reserved words introduced in Ada 95 and Ada 2005 are treated simply
30592 as identifiers as in Ada 83.
30593 However,
30594 in practice, it is usually advisable to make the necessary modifications
30595 to the program to remove the need for using this switch.
30596 See @ref{Compiling Different Versions of Ada}.
30597
30598 @item Support for removed Ada 83 pragmas and attributes
30599 A number of pragmas and attributes from Ada 83 were removed from Ada 95,
30600 generally because they were replaced by other mechanisms. Ada 95 and Ada 2005
30601 compilers are allowed, but not required, to implement these missing
30602 elements. In contrast with some other compilers, GNAT implements all
30603 such pragmas and attributes, eliminating this compatibility concern. These
30604 include @code{pragma Interface} and the floating point type attributes
30605 (@code{Emax}, @code{Mantissa}, etc.), among other items.
30606 @end table
30607
30608
30609 @node Compatibility between Ada 95 and Ada 2005
30610 @section Compatibility between Ada 95 and Ada 2005
30611 @cindex Compatibility between Ada 95 and Ada 2005
30612
30613 @noindent
30614 Although Ada 2005 was designed to be upwards compatible with Ada 95, there are
30615 a number of incompatibilities. Several are enumerated below;
30616 for a complete description please see the
30617 Annotated Ada 2005 Reference Manual, or section 9.1.1 in
30618 @cite{Rationale for Ada 2005}.
30619
30620 @table @emph
30621 @item New reserved words.
30622 The words @code{interface}, @code{overriding} and @code{synchronized} are
30623 reserved in Ada 2005.
30624 A pre-Ada 2005 program that uses any of these as an identifier will be
30625 illegal.
30626
30627 @item New declarations in predefined packages.
30628 A number of packages in the predefined environment contain new declarations:
30629 @code{Ada.Exceptions}, @code{Ada.Real_Time}, @code{Ada.Strings},
30630 @code{Ada.Strings.Fixed}, @code{Ada.Strings.Bounded},
30631 @code{Ada.Strings.Unbounded}, @code{Ada.Strings.Wide_Fixed},
30632 @code{Ada.Strings.Wide_Bounded}, @code{Ada.Strings.Wide_Unbounded},
30633 @code{Ada.Tags}, @code{Ada.Text_IO}, and @code{Interfaces.C}.
30634 If an Ada 95 program does a @code{with} and @code{use} of any of these
30635 packages, the new declarations may cause name clashes.
30636
30637 @item Access parameters.
30638 A nondispatching subprogram with an access parameter cannot be renamed
30639 as a dispatching operation. This was permitted in Ada 95.
30640
30641 @item Access types, discriminants, and constraints.
30642 Rule changes in this area have led to some incompatibilities; for example,
30643 constrained subtypes of some access types are not permitted in Ada 2005.
30644
30645 @item Aggregates for limited types.
30646 The allowance of aggregates for limited types in Ada 2005 raises the
30647 possibility of ambiguities in legal Ada 95 programs, since additional types
30648 now need to be considered in expression resolution.
30649
30650 @item Fixed-point multiplication and division.
30651 Certain expressions involving ``*'' or ``/'' for a fixed-point type, which
30652 were legal in Ada 95 and invoked the predefined versions of these operations,
30653 are now ambiguous.
30654 The ambiguity may be resolved either by applying a type conversion to the
30655 expression, or by explicitly invoking the operation from package
30656 @code{Standard}.
30657
30658 @item Return-by-reference types.
30659 The Ada 95 return-by-reference mechanism has been removed. Instead, the user
30660 can declare a function returning a value from an anonymous access type.
30661 @end table
30662
30663
30664 @node Implementation-dependent characteristics
30665 @section Implementation-dependent characteristics
30666 @noindent
30667 Although the Ada language defines the semantics of each construct as
30668 precisely as practical, in some situations (for example for reasons of
30669 efficiency, or where the effect is heavily dependent on the host or target
30670 platform) the implementation is allowed some freedom. In porting Ada 83
30671 code to GNAT, you need to be aware of whether / how the existing code
30672 exercised such implementation dependencies. Such characteristics fall into
30673 several categories, and GNAT offers specific support in assisting the
30674 transition from certain Ada 83 compilers.
30675
30676 @menu
30677 * Implementation-defined pragmas::
30678 * Implementation-defined attributes::
30679 * Libraries::
30680 * Elaboration order::
30681 * Target-specific aspects::
30682 @end menu
30683
30684 @node Implementation-defined pragmas
30685 @subsection Implementation-defined pragmas
30686
30687 @noindent
30688 Ada compilers are allowed to supplement the language-defined pragmas, and
30689 these are a potential source of non-portability. All GNAT-defined pragmas
30690 are described in @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT
30691 Reference Manual}, and these include several that are specifically
30692 intended to correspond to other vendors' Ada 83 pragmas.
30693 For migrating from VADS, the pragma @code{Use_VADS_Size} may be useful.
30694 For compatibility with HP Ada 83, GNAT supplies the pragmas
30695 @code{Extend_System}, @code{Ident}, @code{Inline_Generic},
30696 @code{Interface_Name}, @code{Passive}, @code{Suppress_All},
30697 and @code{Volatile}.
30698 Other relevant pragmas include @code{External} and @code{Link_With}.
30699 Some vendor-specific
30700 Ada 83 pragmas (@code{Share_Generic}, @code{Subtitle}, and @code{Title}) are
30701 recognized, thus
30702 avoiding compiler rejection of units that contain such pragmas; they are not
30703 relevant in a GNAT context and hence are not otherwise implemented.
30704
30705 @node Implementation-defined attributes
30706 @subsection Implementation-defined attributes
30707
30708 Analogous to pragmas, the set of attributes may be extended by an
30709 implementation. All GNAT-defined attributes are described in
30710 @ref{Implementation Defined Attributes,,, gnat_rm, GNAT Reference
30711 Manual}, and these include several that are specifically intended
30712 to correspond to other vendors' Ada 83 attributes. For migrating from VADS,
30713 the attribute @code{VADS_Size} may be useful. For compatibility with HP
30714 Ada 83, GNAT supplies the attributes @code{Bit}, @code{Machine_Size} and
30715 @code{Type_Class}.
30716
30717 @node Libraries
30718 @subsection Libraries
30719 @noindent
30720 Vendors may supply libraries to supplement the standard Ada API. If Ada 83
30721 code uses vendor-specific libraries then there are several ways to manage
30722 this in Ada 95 or Ada 2005:
30723 @enumerate
30724 @item
30725 If the source code for the libraries (specs and bodies) are
30726 available, then the libraries can be migrated in the same way as the
30727 application.
30728 @item
30729 If the source code for the specs but not the bodies are
30730 available, then you can reimplement the bodies.
30731 @item
30732 Some features introduced by Ada 95 obviate the need for library support. For
30733 example most Ada 83 vendors supplied a package for unsigned integers. The
30734 Ada 95 modular type feature is the preferred way to handle this need, so
30735 instead of migrating or reimplementing the unsigned integer package it may
30736 be preferable to retrofit the application using modular types.
30737 @end enumerate
30738
30739 @node Elaboration order
30740 @subsection Elaboration order
30741 @noindent
30742 The implementation can choose any elaboration order consistent with the unit
30743 dependency relationship. This freedom means that some orders can result in
30744 Program_Error being raised due to an ``Access Before Elaboration'': an attempt
30745 to invoke a subprogram its body has been elaborated, or to instantiate a
30746 generic before the generic body has been elaborated. By default GNAT
30747 attempts to choose a safe order (one that will not encounter access before
30748 elaboration problems) by implicitly inserting @code{Elaborate} or
30749 @code{Elaborate_All} pragmas where
30750 needed. However, this can lead to the creation of elaboration circularities
30751 and a resulting rejection of the program by gnatbind. This issue is
30752 thoroughly described in @ref{Elaboration Order Handling in GNAT}.
30753 In brief, there are several
30754 ways to deal with this situation:
30755
30756 @itemize @bullet
30757 @item
30758 Modify the program to eliminate the circularities, e.g.@: by moving
30759 elaboration-time code into explicitly-invoked procedures
30760 @item
30761 Constrain the elaboration order by including explicit @code{Elaborate_Body} or
30762 @code{Elaborate} pragmas, and then inhibit the generation of implicit
30763 @code{Elaborate_All}
30764 pragmas either globally (as an effect of the @option{-gnatE} switch) or locally
30765 (by selectively suppressing elaboration checks via pragma
30766 @code{Suppress(Elaboration_Check)} when it is safe to do so).
30767 @end itemize
30768
30769 @node Target-specific aspects
30770 @subsection Target-specific aspects
30771 @noindent
30772 Low-level applications need to deal with machine addresses, data
30773 representations, interfacing with assembler code, and similar issues. If
30774 such an Ada 83 application is being ported to different target hardware (for
30775 example where the byte endianness has changed) then you will need to
30776 carefully examine the program logic; the porting effort will heavily depend
30777 on the robustness of the original design. Moreover, Ada 95 (and thus
30778 Ada 2005) are sometimes
30779 incompatible with typical Ada 83 compiler practices regarding implicit
30780 packing, the meaning of the Size attribute, and the size of access values.
30781 GNAT's approach to these issues is described in @ref{Representation Clauses}.
30782
30783 @node Compatibility with Other Ada Systems
30784 @section Compatibility with Other Ada Systems
30785
30786 @noindent
30787 If programs avoid the use of implementation dependent and
30788 implementation defined features, as documented in the @cite{Ada
30789 Reference Manual}, there should be a high degree of portability between
30790 GNAT and other Ada systems. The following are specific items which
30791 have proved troublesome in moving Ada 95 programs from GNAT to other Ada 95
30792 compilers, but do not affect porting code to GNAT@.
30793 (As of @value{NOW}, GNAT is the only compiler available for Ada 2005;
30794 the following issues may or may not arise for Ada 2005 programs
30795 when other compilers appear.)
30796
30797 @table @emph
30798 @item Ada 83 Pragmas and Attributes
30799 Ada 95 compilers are allowed, but not required, to implement the missing
30800 Ada 83 pragmas and attributes that are no longer defined in Ada 95.
30801 GNAT implements all such pragmas and attributes, eliminating this as
30802 a compatibility concern, but some other Ada 95 compilers reject these
30803 pragmas and attributes.
30804
30805 @item Specialized Needs Annexes
30806 GNAT implements the full set of special needs annexes. At the
30807 current time, it is the only Ada 95 compiler to do so. This means that
30808 programs making use of these features may not be portable to other Ada
30809 95 compilation systems.
30810
30811 @item Representation Clauses
30812 Some other Ada 95 compilers implement only the minimal set of
30813 representation clauses required by the Ada 95 reference manual. GNAT goes
30814 far beyond this minimal set, as described in the next section.
30815 @end table
30816
30817 @node Representation Clauses
30818 @section Representation Clauses
30819
30820 @noindent
30821 The Ada 83 reference manual was quite vague in describing both the minimal
30822 required implementation of representation clauses, and also their precise
30823 effects. Ada 95 (and thus also Ada 2005) are much more explicit, but the
30824 minimal set of capabilities required is still quite limited.
30825
30826 GNAT implements the full required set of capabilities in
30827 Ada 95 and Ada 2005, but also goes much further, and in particular
30828 an effort has been made to be compatible with existing Ada 83 usage to the
30829 greatest extent possible.
30830
30831 A few cases exist in which Ada 83 compiler behavior is incompatible with
30832 the requirements in Ada 95 (and thus also Ada 2005). These are instances of
30833 intentional or accidental dependence on specific implementation dependent
30834 characteristics of these Ada 83 compilers. The following is a list of
30835 the cases most likely to arise in existing Ada 83 code.
30836
30837 @table @emph
30838 @item Implicit Packing
30839 Some Ada 83 compilers allowed a Size specification to cause implicit
30840 packing of an array or record. This could cause expensive implicit
30841 conversions for change of representation in the presence of derived
30842 types, and the Ada design intends to avoid this possibility.
30843 Subsequent AI's were issued to make it clear that such implicit
30844 change of representation in response to a Size clause is inadvisable,
30845 and this recommendation is represented explicitly in the Ada 95 (and Ada 2005)
30846 Reference Manuals as implementation advice that is followed by GNAT@.
30847 The problem will show up as an error
30848 message rejecting the size clause. The fix is simply to provide
30849 the explicit pragma @code{Pack}, or for more fine tuned control, provide
30850 a Component_Size clause.
30851
30852 @item Meaning of Size Attribute
30853 The Size attribute in Ada 95 (and Ada 2005) for discrete types is defined as
30854 the minimal number of bits required to hold values of the type. For example,
30855 on a 32-bit machine, the size of @code{Natural} will typically be 31 and not
30856 32 (since no sign bit is required). Some Ada 83 compilers gave 31, and
30857 some 32 in this situation. This problem will usually show up as a compile
30858 time error, but not always. It is a good idea to check all uses of the
30859 'Size attribute when porting Ada 83 code. The GNAT specific attribute
30860 Object_Size can provide a useful way of duplicating the behavior of
30861 some Ada 83 compiler systems.
30862
30863 @item Size of Access Types
30864 A common assumption in Ada 83 code is that an access type is in fact a pointer,
30865 and that therefore it will be the same size as a System.Address value. This
30866 assumption is true for GNAT in most cases with one exception. For the case of
30867 a pointer to an unconstrained array type (where the bounds may vary from one
30868 value of the access type to another), the default is to use a ``fat pointer'',
30869 which is represented as two separate pointers, one to the bounds, and one to
30870 the array. This representation has a number of advantages, including improved
30871 efficiency. However, it may cause some difficulties in porting existing Ada 83
30872 code which makes the assumption that, for example, pointers fit in 32 bits on
30873 a machine with 32-bit addressing.
30874
30875 To get around this problem, GNAT also permits the use of ``thin pointers'' for
30876 access types in this case (where the designated type is an unconstrained array
30877 type). These thin pointers are indeed the same size as a System.Address value.
30878 To specify a thin pointer, use a size clause for the type, for example:
30879
30880 @smallexample @c ada
30881 type X is access all String;
30882 for X'Size use Standard'Address_Size;
30883 @end smallexample
30884
30885 @noindent
30886 which will cause the type X to be represented using a single pointer.
30887 When using this representation, the bounds are right behind the array.
30888 This representation is slightly less efficient, and does not allow quite
30889 such flexibility in the use of foreign pointers or in using the
30890 Unrestricted_Access attribute to create pointers to non-aliased objects.
30891 But for any standard portable use of the access type it will work in
30892 a functionally correct manner and allow porting of existing code.
30893 Note that another way of forcing a thin pointer representation
30894 is to use a component size clause for the element size in an array,
30895 or a record representation clause for an access field in a record.
30896 @end table
30897
30898 @ifclear vms
30899 @c This brief section is only in the non-VMS version
30900 @c The complete chapter on HP Ada is in the VMS version
30901 @node Compatibility with HP Ada 83
30902 @section Compatibility with HP Ada 83
30903
30904 @noindent
30905 The VMS version of GNAT fully implements all the pragmas and attributes
30906 provided by HP Ada 83, as well as providing the standard HP Ada 83
30907 libraries, including Starlet. In addition, data layouts and parameter
30908 passing conventions are highly compatible. This means that porting
30909 existing HP Ada 83 code to GNAT in VMS systems should be easier than
30910 most other porting efforts. The following are some of the most
30911 significant differences between GNAT and HP Ada 83.
30912
30913 @table @emph
30914 @item Default floating-point representation
30915 In GNAT, the default floating-point format is IEEE, whereas in HP Ada 83,
30916 it is VMS format. GNAT does implement the necessary pragmas
30917 (Long_Float, Float_Representation) for changing this default.
30918
30919 @item System
30920 The package System in GNAT exactly corresponds to the definition in the
30921 Ada 95 reference manual, which means that it excludes many of the
30922 HP Ada 83 extensions. However, a separate package Aux_DEC is provided
30923 that contains the additional definitions, and a special pragma,
30924 Extend_System allows this package to be treated transparently as an
30925 extension of package System.
30926
30927 @item To_Address
30928 The definitions provided by Aux_DEC are exactly compatible with those
30929 in the HP Ada 83 version of System, with one exception.
30930 HP Ada provides the following declarations:
30931
30932 @smallexample @c ada
30933 TO_ADDRESS (INTEGER)
30934 TO_ADDRESS (UNSIGNED_LONGWORD)
30935 TO_ADDRESS (@i{universal_integer})
30936 @end smallexample
30937
30938 @noindent
30939 The version of TO_ADDRESS taking a @i{universal integer} argument is in fact
30940 an extension to Ada 83 not strictly compatible with the reference manual.
30941 In GNAT, we are constrained to be exactly compatible with the standard,
30942 and this means we cannot provide this capability. In HP Ada 83, the
30943 point of this definition is to deal with a call like:
30944
30945 @smallexample @c ada
30946 TO_ADDRESS (16#12777#);
30947 @end smallexample
30948
30949 @noindent
30950 Normally, according to the Ada 83 standard, one would expect this to be
30951 ambiguous, since it matches both the INTEGER and UNSIGNED_LONGWORD forms
30952 of TO_ADDRESS@. However, in HP Ada 83, there is no ambiguity, since the
30953 definition using @i{universal_integer} takes precedence.
30954
30955 In GNAT, since the version with @i{universal_integer} cannot be supplied, it
30956 is not possible to be 100% compatible. Since there are many programs using
30957 numeric constants for the argument to TO_ADDRESS, the decision in GNAT was
30958 to change the name of the function in the UNSIGNED_LONGWORD case, so the
30959 declarations provided in the GNAT version of AUX_Dec are:
30960
30961 @smallexample @c ada
30962 function To_Address (X : Integer) return Address;
30963 pragma Pure_Function (To_Address);
30964
30965 function To_Address_Long (X : Unsigned_Longword)
30966 return Address;
30967 pragma Pure_Function (To_Address_Long);
30968 @end smallexample
30969
30970 @noindent
30971 This means that programs using TO_ADDRESS for UNSIGNED_LONGWORD must
30972 change the name to TO_ADDRESS_LONG@.
30973
30974 @item Task_Id values
30975 The Task_Id values assigned will be different in the two systems, and GNAT
30976 does not provide a specified value for the Task_Id of the environment task,
30977 which in GNAT is treated like any other declared task.
30978 @end table
30979
30980 @noindent
30981 For full details on these and other less significant compatibility issues,
30982 see appendix E of the HP publication entitled @cite{HP Ada, Technical
30983 Overview and Comparison on HP Platforms}.
30984
30985 For GNAT running on other than VMS systems, all the HP Ada 83 pragmas and
30986 attributes are recognized, although only a subset of them can sensibly
30987 be implemented. The description of pragmas in @ref{Implementation
30988 Defined Pragmas,,, gnat_rm, GNAT Reference Manual}
30989 indicates whether or not they are applicable to non-VMS systems.
30990 @end ifclear
30991
30992 @ifset vms
30993 @node Transitioning to 64-Bit GNAT for OpenVMS
30994 @section Transitioning to 64-Bit @value{EDITION} for OpenVMS
30995
30996 @noindent
30997 This section is meant to assist users of pre-2006 @value{EDITION}
30998 for Alpha OpenVMS who are transitioning to 64-bit @value{EDITION},
30999 the version of the GNAT technology supplied in 2006 and later for
31000 OpenVMS on both Alpha and I64.
31001
31002 @menu
31003 * Introduction to transitioning::
31004 * Migration of 32 bit code::
31005 * Taking advantage of 64 bit addressing::
31006 * Technical details::
31007 @end menu
31008
31009 @node Introduction to transitioning
31010 @subsection Introduction
31011
31012 @noindent
31013 64-bit @value{EDITION} for Open VMS has been designed to meet
31014 three main goals:
31015
31016 @enumerate
31017 @item
31018 Providing a full conforming implementation of Ada 95 and Ada 2005
31019
31020 @item
31021 Allowing maximum backward compatibility, thus easing migration of existing
31022 Ada source code
31023
31024 @item
31025 Supplying a path for exploiting the full 64-bit address range
31026 @end enumerate
31027
31028 @noindent
31029 Ada's strong typing semantics has made it
31030 impractical to have different 32-bit and 64-bit modes. As soon as
31031 one object could possibly be outside the 32-bit address space, this
31032 would make it necessary for the @code{System.Address} type to be 64 bits.
31033 In particular, this would cause inconsistencies if 32-bit code is
31034 called from 64-bit code that raises an exception.
31035
31036 This issue has been resolved by always using 64-bit addressing
31037 at the system level, but allowing for automatic conversions between
31038 32-bit and 64-bit addresses where required. Thus users who
31039 do not currently require 64-bit addressing capabilities, can
31040 recompile their code with only minimal changes (and indeed
31041 if the code is written in portable Ada, with no assumptions about
31042 the size of the @code{Address} type, then no changes at all are necessary).
31043 At the same time,
31044 this approach provides a simple, gradual upgrade path to future
31045 use of larger memories than available for 32-bit systems.
31046 Also, newly written applications or libraries will by default
31047 be fully compatible with future systems exploiting 64-bit
31048 addressing capabilities.
31049
31050 @ref{Migration of 32 bit code}, will focus on porting applications
31051 that do not require more than 2 GB of
31052 addressable memory. This code will be referred to as
31053 @emph{32-bit code}.
31054 For applications intending to exploit the full 64-bit address space,
31055 @ref{Taking advantage of 64 bit addressing},
31056 will consider further changes that may be required.
31057 Such code will be referred to below as @emph{64-bit code}.
31058
31059 @node Migration of 32 bit code
31060 @subsection Migration of 32-bit code
31061
31062 @menu
31063 * Address types::
31064 * Access types::
31065 * Unchecked conversions::
31066 * Predefined constants::
31067 * Interfacing with C::
31068 * Experience with source compatibility::
31069 @end menu
31070
31071 @node Address types
31072 @subsubsection Address types
31073
31074 @noindent
31075 To solve the problem of mixing 64-bit and 32-bit addressing,
31076 while maintaining maximum backward compatibility, the following
31077 approach has been taken:
31078
31079 @itemize @bullet
31080 @item
31081 @code{System.Address} always has a size of 64 bits
31082
31083 @item
31084 @code{System.Short_Address} is a 32-bit subtype of @code{System.Address}
31085 @end itemize
31086
31087 @noindent
31088 Since @code{System.Short_Address} is a subtype of @code{System.Address},
31089 a @code{Short_Address}
31090 may be used where an @code{Address} is required, and vice versa, without
31091 needing explicit type conversions.
31092 By virtue of the Open VMS parameter passing conventions,
31093 even imported
31094 and exported subprograms that have 32-bit address parameters are
31095 compatible with those that have 64-bit address parameters.
31096 (See @ref{Making code 64 bit clean} for details.)
31097
31098 The areas that may need attention are those where record types have
31099 been defined that contain components of the type @code{System.Address}, and
31100 where objects of this type are passed to code expecting a record layout with
31101 32-bit addresses.
31102
31103 Different compilers on different platforms cannot be
31104 expected to represent the same type in the same way,
31105 since alignment constraints
31106 and other system-dependent properties affect the compiler's decision.
31107 For that reason, Ada code
31108 generally uses representation clauses to specify the expected
31109 layout where required.
31110
31111 If such a representation clause uses 32 bits for a component having
31112 the type @code{System.Address}, 64-bit @value{EDITION} for OpenVMS
31113 will detect that error and produce a specific diagnostic message.
31114 The developer should then determine whether the representation
31115 should be 64 bits or not and make either of two changes:
31116 change the size to 64 bits and leave the type as @code{System.Address}, or
31117 leave the size as 32 bits and change the type to @code{System.Short_Address}.
31118 Since @code{Short_Address} is a subtype of @code{Address}, no changes are
31119 required in any code setting or accessing the field; the compiler will
31120 automatically perform any needed conversions between address
31121 formats.
31122
31123 @node Access types
31124 @subsubsection Access types
31125
31126 @noindent
31127 By default, objects designated by access values are always
31128 allocated in the 32-bit
31129 address space. Thus legacy code will never contain
31130 any objects that are not addressable with 32-bit addresses, and
31131 the compiler will never raise exceptions as result of mixing
31132 32-bit and 64-bit addresses.
31133
31134 However, the access values themselves are represented in 64 bits, for optimum
31135 performance and future compatibility with 64-bit code. As was
31136 the case with @code{System.Address}, the compiler will give an error message
31137 if an object or record component has a representation clause that
31138 requires the access value to fit in 32 bits. In such a situation,
31139 an explicit size clause for the access type, specifying 32 bits,
31140 will have the desired effect.
31141
31142 General access types (declared with @code{access all}) can never be
31143 32 bits, as values of such types must be able to refer to any object
31144 of the designated type,
31145 including objects residing outside the 32-bit address range.
31146 Existing Ada 83 code will not contain such type definitions,
31147 however, since general access types were introduced in Ada 95.
31148
31149 @node Unchecked conversions
31150 @subsubsection Unchecked conversions
31151
31152 @noindent
31153 In the case of an @code{Unchecked_Conversion} where the source type is a
31154 64-bit access type or the type @code{System.Address}, and the target
31155 type is a 32-bit type, the compiler will generate a warning.
31156 Even though the generated code will still perform the required
31157 conversions, it is highly recommended in these cases to use
31158 respectively a 32-bit access type or @code{System.Short_Address}
31159 as the source type.
31160
31161 @node Predefined constants
31162 @subsubsection Predefined constants
31163
31164 @noindent
31165 The following table shows the correspondence between pre-2006 versions of
31166 @value{EDITION} on Alpha OpenVMS (``Old'') and 64-bit @value{EDITION}
31167 (``New''):
31168
31169 @multitable {@code{System.Short_Memory_Size}} {2**32} {2**64}
31170 @item @b{Constant} @tab @b{Old} @tab @b{New}
31171 @item @code{System.Word_Size} @tab 32 @tab 64
31172 @item @code{System.Memory_Size} @tab 2**32 @tab 2**64
31173 @item @code{System.Short_Memory_Size} @tab 2**32 @tab 2**32
31174 @item @code{System.Address_Size} @tab 32 @tab 64
31175 @end multitable
31176
31177 @noindent
31178 If you need to refer to the specific
31179 memory size of a 32-bit implementation, instead of the
31180 actual memory size, use @code{System.Short_Memory_Size}
31181 rather than @code{System.Memory_Size}.
31182 Similarly, references to @code{System.Address_Size} may need
31183 to be replaced by @code{System.Short_Address'Size}.
31184 The program @command{gnatfind} may be useful for locating
31185 references to the above constants, so that you can verify that they
31186 are still correct.
31187
31188 @node Interfacing with C
31189 @subsubsection Interfacing with C
31190
31191 @noindent
31192 In order to minimize the impact of the transition to 64-bit addresses on
31193 legacy programs, some fundamental types in the @code{Interfaces.C}
31194 package hierarchy continue to be represented in 32 bits.
31195 These types are: @code{ptrdiff_t}, @code{size_t}, and @code{chars_ptr}.
31196 This eases integration with the default HP C layout choices, for example
31197 as found in the system routines in @code{DECC$SHR.EXE}.
31198 Because of this implementation choice, the type fully compatible with
31199 @code{chars_ptr} is now @code{Short_Address} and not @code{Address}.
31200 Depending on the context the compiler will issue a
31201 warning or an error when type @code{Address} is used, alerting the user to a
31202 potential problem. Otherwise 32-bit programs that use
31203 @code{Interfaces.C} should normally not require code modifications
31204
31205 The other issue arising with C interfacing concerns pragma @code{Convention}.
31206 For VMS 64-bit systems, there is an issue of the appropriate default size
31207 of C convention pointers in the absence of an explicit size clause. The HP
31208 C compiler can choose either 32 or 64 bits depending on compiler options.
31209 GNAT chooses 32-bits rather than 64-bits in the default case where no size
31210 clause is given. This proves a better choice for porting 32-bit legacy
31211 applications. In order to have a 64-bit representation, it is necessary to
31212 specify a size representation clause. For example:
31213
31214 @smallexample @c ada
31215 type int_star is access Interfaces.C.int;
31216 pragma Convention(C, int_star);
31217 for int_star'Size use 64; -- Necessary to get 64 and not 32 bits
31218 @end smallexample
31219
31220 @node Experience with source compatibility
31221 @subsubsection Experience with source compatibility
31222
31223 @noindent
31224 The Security Server and STARLET on I64 provide an interesting ``test case''
31225 for source compatibility issues, since it is in such system code
31226 where assumptions about @code{Address} size might be expected to occur.
31227 Indeed, there were a small number of occasions in the Security Server
31228 file @file{jibdef.ads}
31229 where a representation clause for a record type specified
31230 32 bits for a component of type @code{Address}.
31231 All of these errors were detected by the compiler.
31232 The repair was obvious and immediate; to simply replace @code{Address} by
31233 @code{Short_Address}.
31234
31235 In the case of STARLET, there were several record types that should
31236 have had representation clauses but did not. In these record types
31237 there was an implicit assumption that an @code{Address} value occupied
31238 32 bits.
31239 These compiled without error, but their usage resulted in run-time error
31240 returns from STARLET system calls.
31241 Future GNAT technology enhancements may include a tool that detects and flags
31242 these sorts of potential source code porting problems.
31243
31244 @c ****************************************
31245 @node Taking advantage of 64 bit addressing
31246 @subsection Taking advantage of 64-bit addressing
31247
31248 @menu
31249 * Making code 64 bit clean::
31250 * Allocating memory from the 64 bit storage pool::
31251 * Restrictions on use of 64 bit objects::
31252 * Using 64 bit storage pools by default::
31253 * General access types::
31254 * STARLET and other predefined libraries::
31255 @end menu
31256
31257 @node Making code 64 bit clean
31258 @subsubsection Making code 64-bit clean
31259
31260 @noindent
31261 In order to prevent problems that may occur when (parts of) a
31262 system start using memory outside the 32-bit address range,
31263 we recommend some additional guidelines:
31264
31265 @itemize @bullet
31266 @item
31267 For imported subprograms that take parameters of the
31268 type @code{System.Address}, ensure that these subprograms can
31269 indeed handle 64-bit addresses. If not, or when in doubt,
31270 change the subprogram declaration to specify
31271 @code{System.Short_Address} instead.
31272
31273 @item
31274 Resolve all warnings related to size mismatches in
31275 unchecked conversions. Failing to do so causes
31276 erroneous execution if the source object is outside
31277 the 32-bit address space.
31278
31279 @item
31280 (optional) Explicitly use the 32-bit storage pool
31281 for access types used in a 32-bit context, or use
31282 generic access types where possible
31283 (@pxref{Restrictions on use of 64 bit objects}).
31284 @end itemize
31285
31286 @noindent
31287 If these rules are followed, the compiler will automatically insert
31288 any necessary checks to ensure that no addresses or access values
31289 passed to 32-bit code ever refer to objects outside the 32-bit
31290 address range.
31291 Any attempt to do this will raise @code{Constraint_Error}.
31292
31293 @node Allocating memory from the 64 bit storage pool
31294 @subsubsection Allocating memory from the 64-bit storage pool
31295
31296 @noindent
31297 For any access type @code{T} that potentially requires memory allocations
31298 beyond the 32-bit address space,
31299 use the following representation clause:
31300
31301 @smallexample @c ada
31302 for T'Storage_Pool use System.Pool_64;
31303 @end smallexample
31304
31305 @node Restrictions on use of 64 bit objects
31306 @subsubsection Restrictions on use of 64-bit objects
31307
31308 @noindent
31309 Taking the address of an object allocated from a 64-bit storage pool,
31310 and then passing this address to a subprogram expecting
31311 @code{System.Short_Address},
31312 or assigning it to a variable of type @code{Short_Address}, will cause
31313 @code{Constraint_Error} to be raised. In case the code is not 64-bit clean
31314 (@pxref{Making code 64 bit clean}), or checks are suppressed,
31315 no exception is raised and execution
31316 will become erroneous.
31317
31318 @node Using 64 bit storage pools by default
31319 @subsubsection Using 64-bit storage pools by default
31320
31321 @noindent
31322 In some cases it may be desirable to have the compiler allocate
31323 from 64-bit storage pools by default. This may be the case for
31324 libraries that are 64-bit clean, but may be used in both 32-bit
31325 and 64-bit contexts. For these cases the following configuration
31326 pragma may be specified:
31327
31328 @smallexample @c ada
31329 pragma Pool_64_Default;
31330 @end smallexample
31331
31332 @noindent
31333 Any code compiled in the context of this pragma will by default
31334 use the @code{System.Pool_64} storage pool. This default may be overridden
31335 for a specific access type @code{T} by the representation clause:
31336
31337 @smallexample @c ada
31338 for T'Storage_Pool use System.Pool_32;
31339 @end smallexample
31340
31341 @noindent
31342 Any object whose address may be passed to a subprogram with a
31343 @code{Short_Address} argument, or assigned to a variable of type
31344 @code{Short_Address}, needs to be allocated from this pool.
31345
31346 @node General access types
31347 @subsubsection General access types
31348
31349 @noindent
31350 Objects designated by access values from a
31351 general access type (declared with @code{access all}) are never allocated
31352 from a 64-bit storage pool. Code that uses general access types will
31353 accept objects allocated in either 32-bit or 64-bit address spaces,
31354 but never allocate objects outside the 32-bit address space.
31355 Using general access types ensures maximum compatibility with both
31356 32-bit and 64-bit code.
31357
31358 @node STARLET and other predefined libraries
31359 @subsubsection STARLET and other predefined libraries
31360
31361 @noindent
31362 All code that comes as part of GNAT is 64-bit clean, but the
31363 restrictions given in @ref{Restrictions on use of 64 bit objects},
31364 still apply. Look at the package
31365 specs to see in which contexts objects allocated
31366 in 64-bit address space are acceptable.
31367
31368 @node Technical details
31369 @subsection Technical details
31370
31371 @noindent
31372 64-bit @value{EDITION} for Open VMS takes advantage of the freedom given in the
31373 Ada standard with respect to the type of @code{System.Address}. Previous
31374 versions of GNAT Pro have defined this type as private and implemented it as a
31375 modular type.
31376
31377 In order to allow defining @code{System.Short_Address} as a proper subtype,
31378 and to match the implicit sign extension in parameter passing,
31379 in 64-bit @value{EDITION} for Open VMS, @code{System.Address} is defined as a
31380 visible (i.e., non-private) integer type.
31381 Standard operations on the type, such as the binary operators ``+'', ``-'',
31382 etc., that take @code{Address} operands and return an @code{Address} result,
31383 have been hidden by declaring these
31384 @code{abstract}, a feature introduced in Ada 95 that helps avoid the potential
31385 ambiguities that would otherwise result from overloading.
31386 (Note that, although @code{Address} is a visible integer type,
31387 good programming practice dictates against exploiting the type's
31388 integer properties such as literals, since this will compromise
31389 code portability.)
31390
31391 Defining @code{Address} as a visible integer type helps achieve
31392 maximum compatibility for existing Ada code,
31393 without sacrificing the capabilities of the 64-bit architecture.
31394 @end ifset
31395
31396 @c ************************************************
31397 @ifset unw
31398 @node Microsoft Windows Topics
31399 @appendix Microsoft Windows Topics
31400 @cindex Windows NT
31401 @cindex Windows 95
31402 @cindex Windows 98
31403
31404 @noindent
31405 This chapter describes topics that are specific to the Microsoft Windows
31406 platforms (NT, 2000, and XP Professional).
31407
31408 @menu
31409 * Using GNAT on Windows::
31410 * Using a network installation of GNAT::
31411 * CONSOLE and WINDOWS subsystems::
31412 * Temporary Files::
31413 * Mixed-Language Programming on Windows::
31414 * Windows Calling Conventions::
31415 * Introduction to Dynamic Link Libraries (DLLs)::
31416 * Using DLLs with GNAT::
31417 * Building DLLs with GNAT::
31418 * Building DLLs with GNAT Project files::
31419 * Building DLLs with gnatdll::
31420 * GNAT and Windows Resources::
31421 * Debugging a DLL::
31422 * Setting Stack Size from gnatlink::
31423 * Setting Heap Size from gnatlink::
31424 @end menu
31425
31426 @node Using GNAT on Windows
31427 @section Using GNAT on Windows
31428
31429 @noindent
31430 One of the strengths of the GNAT technology is that its tool set
31431 (@command{gcc}, @command{gnatbind}, @command{gnatlink}, @command{gnatmake}, the
31432 @code{gdb} debugger, etc.) is used in the same way regardless of the
31433 platform.
31434
31435 On Windows this tool set is complemented by a number of Microsoft-specific
31436 tools that have been provided to facilitate interoperability with Windows
31437 when this is required. With these tools:
31438
31439 @itemize @bullet
31440
31441 @item
31442 You can build applications using the @code{CONSOLE} or @code{WINDOWS}
31443 subsystems.
31444
31445 @item
31446 You can use any Dynamically Linked Library (DLL) in your Ada code (both
31447 relocatable and non-relocatable DLLs are supported).
31448
31449 @item
31450 You can build Ada DLLs for use in other applications. These applications
31451 can be written in a language other than Ada (e.g., C, C++, etc). Again both
31452 relocatable and non-relocatable Ada DLLs are supported.
31453
31454 @item
31455 You can include Windows resources in your Ada application.
31456
31457 @item
31458 You can use or create COM/DCOM objects.
31459 @end itemize
31460
31461 @noindent
31462 Immediately below are listed all known general GNAT-for-Windows restrictions.
31463 Other restrictions about specific features like Windows Resources and DLLs
31464 are listed in separate sections below.
31465
31466 @itemize @bullet
31467
31468 @item
31469 It is not possible to use @code{GetLastError} and @code{SetLastError}
31470 when tasking, protected records, or exceptions are used. In these
31471 cases, in order to implement Ada semantics, the GNAT run-time system
31472 calls certain Win32 routines that set the last error variable to 0 upon
31473 success. It should be possible to use @code{GetLastError} and
31474 @code{SetLastError} when tasking, protected record, and exception
31475 features are not used, but it is not guaranteed to work.
31476
31477 @item
31478 It is not possible to link against Microsoft libraries except for
31479 import libraries. The library must be built to be compatible with
31480 @file{MSVCRT.LIB} (/MD Microsoft compiler option), @file{LIBC.LIB} and
31481 @file{LIBCMT.LIB} (/ML or /MT Microsoft compiler options) are known to
31482 not be compatible with the GNAT runtime. Even if the library is
31483 compatible with @file{MSVCRT.LIB} it is not guaranteed to work.
31484
31485 @item
31486 When the compilation environment is located on FAT32 drives, users may
31487 experience recompilations of the source files that have not changed if
31488 Daylight Saving Time (DST) state has changed since the last time files
31489 were compiled. NTFS drives do not have this problem.
31490
31491 @item
31492 No components of the GNAT toolset use any entries in the Windows
31493 registry. The only entries that can be created are file associations and
31494 PATH settings, provided the user has chosen to create them at installation
31495 time, as well as some minimal book-keeping information needed to correctly
31496 uninstall or integrate different GNAT products.
31497 @end itemize
31498
31499 @node Using a network installation of GNAT
31500 @section Using a network installation of GNAT
31501
31502 @noindent
31503 Make sure the system on which GNAT is installed is accessible from the
31504 current machine, i.e., the install location is shared over the network.
31505 Shared resources are accessed on Windows by means of UNC paths, which
31506 have the format @code{\\server\sharename\path}
31507
31508 In order to use such a network installation, simply add the UNC path of the
31509 @file{bin} directory of your GNAT installation in front of your PATH. For
31510 example, if GNAT is installed in @file{\GNAT} directory of a share location
31511 called @file{c-drive} on a machine @file{LOKI}, the following command will
31512 make it available:
31513
31514 @code{@ @ @ path \\loki\c-drive\gnat\bin;%path%}
31515
31516 Be aware that every compilation using the network installation results in the
31517 transfer of large amounts of data across the network and will likely cause
31518 serious performance penalty.
31519
31520 @node CONSOLE and WINDOWS subsystems
31521 @section CONSOLE and WINDOWS subsystems
31522 @cindex CONSOLE Subsystem
31523 @cindex WINDOWS Subsystem
31524 @cindex -mwindows
31525
31526 @noindent
31527 There are two main subsystems under Windows. The @code{CONSOLE} subsystem
31528 (which is the default subsystem) will always create a console when
31529 launching the application. This is not something desirable when the
31530 application has a Windows GUI. To get rid of this console the
31531 application must be using the @code{WINDOWS} subsystem. To do so
31532 the @option{-mwindows} linker option must be specified.
31533
31534 @smallexample
31535 $ gnatmake winprog -largs -mwindows
31536 @end smallexample
31537
31538 @node Temporary Files
31539 @section Temporary Files
31540 @cindex Temporary files
31541
31542 @noindent
31543 It is possible to control where temporary files gets created by setting
31544 the @env{TMP} environment variable. The file will be created:
31545
31546 @itemize
31547 @item Under the directory pointed to by the @env{TMP} environment variable if
31548 this directory exists.
31549
31550 @item Under @file{c:\temp}, if the @env{TMP} environment variable is not
31551 set (or not pointing to a directory) and if this directory exists.
31552
31553 @item Under the current working directory otherwise.
31554 @end itemize
31555
31556 @noindent
31557 This allows you to determine exactly where the temporary
31558 file will be created. This is particularly useful in networked
31559 environments where you may not have write access to some
31560 directories.
31561
31562 @node Mixed-Language Programming on Windows
31563 @section Mixed-Language Programming on Windows
31564
31565 @noindent
31566 Developing pure Ada applications on Windows is no different than on
31567 other GNAT-supported platforms. However, when developing or porting an
31568 application that contains a mix of Ada and C/C++, the choice of your
31569 Windows C/C++ development environment conditions your overall
31570 interoperability strategy.
31571
31572 If you use @command{gcc} to compile the non-Ada part of your application,
31573 there are no Windows-specific restrictions that affect the overall
31574 interoperability with your Ada code. If you plan to use
31575 Microsoft tools (e.g.@: Microsoft Visual C/C++), you should be aware of
31576 the following limitations:
31577
31578 @itemize @bullet
31579 @item
31580 You cannot link your Ada code with an object or library generated with
31581 Microsoft tools if these use the @code{.tls} section (Thread Local
31582 Storage section) since the GNAT linker does not yet support this section.
31583
31584 @item
31585 You cannot link your Ada code with an object or library generated with
31586 Microsoft tools if these use I/O routines other than those provided in
31587 the Microsoft DLL: @code{msvcrt.dll}. This is because the GNAT run time
31588 uses the services of @code{msvcrt.dll} for its I/Os. Use of other I/O
31589 libraries can cause a conflict with @code{msvcrt.dll} services. For
31590 instance Visual C++ I/O stream routines conflict with those in
31591 @code{msvcrt.dll}.
31592 @end itemize
31593
31594 @noindent
31595 If you do want to use the Microsoft tools for your non-Ada code and hit one
31596 of the above limitations, you have two choices:
31597
31598 @enumerate
31599 @item
31600 Encapsulate your non-Ada code in a DLL to be linked with your Ada
31601 application. In this case, use the Microsoft or whatever environment to
31602 build the DLL and use GNAT to build your executable
31603 (@pxref{Using DLLs with GNAT}).
31604
31605 @item
31606 Or you can encapsulate your Ada code in a DLL to be linked with the
31607 other part of your application. In this case, use GNAT to build the DLL
31608 (@pxref{Building DLLs with GNAT}) and use the Microsoft or whatever
31609 environment to build your executable.
31610 @end enumerate
31611
31612 @node Windows Calling Conventions
31613 @section Windows Calling Conventions
31614 @findex Stdcall
31615 @findex APIENTRY
31616
31617 @menu
31618 * C Calling Convention::
31619 * Stdcall Calling Convention::
31620 * Win32 Calling Convention::
31621 * DLL Calling Convention::
31622 @end menu
31623
31624 @noindent
31625 When a subprogram @code{F} (caller) calls a subprogram @code{G}
31626 (callee), there are several ways to push @code{G}'s parameters on the
31627 stack and there are several possible scenarios to clean up the stack
31628 upon @code{G}'s return. A calling convention is an agreed upon software
31629 protocol whereby the responsibilities between the caller (@code{F}) and
31630 the callee (@code{G}) are clearly defined. Several calling conventions
31631 are available for Windows:
31632
31633 @itemize @bullet
31634 @item
31635 @code{C} (Microsoft defined)
31636
31637 @item
31638 @code{Stdcall} (Microsoft defined)
31639
31640 @item
31641 @code{Win32} (GNAT specific)
31642
31643 @item
31644 @code{DLL} (GNAT specific)
31645 @end itemize
31646
31647 @node C Calling Convention
31648 @subsection @code{C} Calling Convention
31649
31650 @noindent
31651 This is the default calling convention used when interfacing to C/C++
31652 routines compiled with either @command{gcc} or Microsoft Visual C++.
31653
31654 In the @code{C} calling convention subprogram parameters are pushed on the
31655 stack by the caller from right to left. The caller itself is in charge of
31656 cleaning up the stack after the call. In addition, the name of a routine
31657 with @code{C} calling convention is mangled by adding a leading underscore.
31658
31659 The name to use on the Ada side when importing (or exporting) a routine
31660 with @code{C} calling convention is the name of the routine. For
31661 instance the C function:
31662
31663 @smallexample
31664 int get_val (long);
31665 @end smallexample
31666
31667 @noindent
31668 should be imported from Ada as follows:
31669
31670 @smallexample @c ada
31671 @group
31672 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
31673 pragma Import (C, Get_Val, External_Name => "get_val");
31674 @end group
31675 @end smallexample
31676
31677 @noindent
31678 Note that in this particular case the @code{External_Name} parameter could
31679 have been omitted since, when missing, this parameter is taken to be the
31680 name of the Ada entity in lower case. When the @code{Link_Name} parameter
31681 is missing, as in the above example, this parameter is set to be the
31682 @code{External_Name} with a leading underscore.
31683
31684 When importing a variable defined in C, you should always use the @code{C}
31685 calling convention unless the object containing the variable is part of a
31686 DLL (in which case you should use the @code{Stdcall} calling
31687 convention, @pxref{Stdcall Calling Convention}).
31688
31689 @node Stdcall Calling Convention
31690 @subsection @code{Stdcall} Calling Convention
31691
31692 @noindent
31693 This convention, which was the calling convention used for Pascal
31694 programs, is used by Microsoft for all the routines in the Win32 API for
31695 efficiency reasons. It must be used to import any routine for which this
31696 convention was specified.
31697
31698 In the @code{Stdcall} calling convention subprogram parameters are pushed
31699 on the stack by the caller from right to left. The callee (and not the
31700 caller) is in charge of cleaning the stack on routine exit. In addition,
31701 the name of a routine with @code{Stdcall} calling convention is mangled by
31702 adding a leading underscore (as for the @code{C} calling convention) and a
31703 trailing @code{@@}@code{@var{nn}}, where @var{nn} is the overall size (in
31704 bytes) of the parameters passed to the routine.
31705
31706 The name to use on the Ada side when importing a C routine with a
31707 @code{Stdcall} calling convention is the name of the C routine. The leading
31708 underscore and trailing @code{@@}@code{@var{nn}} are added automatically by
31709 the compiler. For instance the Win32 function:
31710
31711 @smallexample
31712 @b{APIENTRY} int get_val (long);
31713 @end smallexample
31714
31715 @noindent
31716 should be imported from Ada as follows:
31717
31718 @smallexample @c ada
31719 @group
31720 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
31721 pragma Import (Stdcall, Get_Val);
31722 -- On the x86 a long is 4 bytes, so the Link_Name is "_get_val@@4"
31723 @end group
31724 @end smallexample
31725
31726 @noindent
31727 As for the @code{C} calling convention, when the @code{External_Name}
31728 parameter is missing, it is taken to be the name of the Ada entity in lower
31729 case. If instead of writing the above import pragma you write:
31730
31731 @smallexample @c ada
31732 @group
31733 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
31734 pragma Import (Stdcall, Get_Val, External_Name => "retrieve_val");
31735 @end group
31736 @end smallexample
31737
31738 @noindent
31739 then the imported routine is @code{_retrieve_val@@4}. However, if instead
31740 of specifying the @code{External_Name} parameter you specify the
31741 @code{Link_Name} as in the following example:
31742
31743 @smallexample @c ada
31744 @group
31745 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
31746 pragma Import (Stdcall, Get_Val, Link_Name => "retrieve_val");
31747 @end group
31748 @end smallexample
31749
31750 @noindent
31751 then the imported routine is @code{retrieve_val}, that is, there is no
31752 decoration at all. No leading underscore and no Stdcall suffix
31753 @code{@@}@code{@var{nn}}.
31754
31755 @noindent
31756 This is especially important as in some special cases a DLL's entry
31757 point name lacks a trailing @code{@@}@code{@var{nn}} while the exported
31758 name generated for a call has it.
31759
31760 @noindent
31761 It is also possible to import variables defined in a DLL by using an
31762 import pragma for a variable. As an example, if a DLL contains a
31763 variable defined as:
31764
31765 @smallexample
31766 int my_var;
31767 @end smallexample
31768
31769 @noindent
31770 then, to access this variable from Ada you should write:
31771
31772 @smallexample @c ada
31773 @group
31774 My_Var : Interfaces.C.int;
31775 pragma Import (Stdcall, My_Var);
31776 @end group
31777 @end smallexample
31778
31779 @noindent
31780 Note that to ease building cross-platform bindings this convention
31781 will be handled as a @code{C} calling convention on non-Windows platforms.
31782
31783 @node Win32 Calling Convention
31784 @subsection @code{Win32} Calling Convention
31785
31786 @noindent
31787 This convention, which is GNAT-specific is fully equivalent to the
31788 @code{Stdcall} calling convention described above.
31789
31790 @node DLL Calling Convention
31791 @subsection @code{DLL} Calling Convention
31792
31793 @noindent
31794 This convention, which is GNAT-specific is fully equivalent to the
31795 @code{Stdcall} calling convention described above.
31796
31797 @node Introduction to Dynamic Link Libraries (DLLs)
31798 @section Introduction to Dynamic Link Libraries (DLLs)
31799 @findex DLL
31800
31801 @noindent
31802 A Dynamically Linked Library (DLL) is a library that can be shared by
31803 several applications running under Windows. A DLL can contain any number of
31804 routines and variables.
31805
31806 One advantage of DLLs is that you can change and enhance them without
31807 forcing all the applications that depend on them to be relinked or
31808 recompiled. However, you should be aware than all calls to DLL routines are
31809 slower since, as you will understand below, such calls are indirect.
31810
31811 To illustrate the remainder of this section, suppose that an application
31812 wants to use the services of a DLL @file{API.dll}. To use the services
31813 provided by @file{API.dll} you must statically link against the DLL or
31814 an import library which contains a jump table with an entry for each
31815 routine and variable exported by the DLL. In the Microsoft world this
31816 import library is called @file{API.lib}. When using GNAT this import
31817 library is called either @file{libAPI.dll.a}, @file{libapi.dll.a},
31818 @file{libAPI.a} or @file{libapi.a} (names are case insensitive).
31819
31820 After you have linked your application with the DLL or the import library
31821 and you run your application, here is what happens:
31822
31823 @enumerate
31824 @item
31825 Your application is loaded into memory.
31826
31827 @item
31828 The DLL @file{API.dll} is mapped into the address space of your
31829 application. This means that:
31830
31831 @itemize @bullet
31832 @item
31833 The DLL will use the stack of the calling thread.
31834
31835 @item
31836 The DLL will use the virtual address space of the calling process.
31837
31838 @item
31839 The DLL will allocate memory from the virtual address space of the calling
31840 process.
31841
31842 @item
31843 Handles (pointers) can be safely exchanged between routines in the DLL
31844 routines and routines in the application using the DLL.
31845 @end itemize
31846
31847 @item
31848 The entries in the jump table (from the import library @file{libAPI.dll.a}
31849 or @file{API.lib} or automatically created when linking against a DLL)
31850 which is part of your application are initialized with the addresses
31851 of the routines and variables in @file{API.dll}.
31852
31853 @item
31854 If present in @file{API.dll}, routines @code{DllMain} or
31855 @code{DllMainCRTStartup} are invoked. These routines typically contain
31856 the initialization code needed for the well-being of the routines and
31857 variables exported by the DLL.
31858 @end enumerate
31859
31860 @noindent
31861 There is an additional point which is worth mentioning. In the Windows
31862 world there are two kind of DLLs: relocatable and non-relocatable
31863 DLLs. Non-relocatable DLLs can only be loaded at a very specific address
31864 in the target application address space. If the addresses of two
31865 non-relocatable DLLs overlap and these happen to be used by the same
31866 application, a conflict will occur and the application will run
31867 incorrectly. Hence, when possible, it is always preferable to use and
31868 build relocatable DLLs. Both relocatable and non-relocatable DLLs are
31869 supported by GNAT. Note that the @option{-s} linker option (see GNU Linker
31870 User's Guide) removes the debugging symbols from the DLL but the DLL can
31871 still be relocated.
31872
31873 As a side note, an interesting difference between Microsoft DLLs and
31874 Unix shared libraries, is the fact that on most Unix systems all public
31875 routines are exported by default in a Unix shared library, while under
31876 Windows it is possible (but not required) to list exported routines in
31877 a definition file (@pxref{The Definition File}).
31878
31879 @node Using DLLs with GNAT
31880 @section Using DLLs with GNAT
31881
31882 @menu
31883 * Creating an Ada Spec for the DLL Services::
31884 * Creating an Import Library::
31885 @end menu
31886
31887 @noindent
31888 To use the services of a DLL, say @file{API.dll}, in your Ada application
31889 you must have:
31890
31891 @enumerate
31892 @item
31893 The Ada spec for the routines and/or variables you want to access in
31894 @file{API.dll}. If not available this Ada spec must be built from the C/C++
31895 header files provided with the DLL.
31896
31897 @item
31898 The import library (@file{libAPI.dll.a} or @file{API.lib}). As previously
31899 mentioned an import library is a statically linked library containing the
31900 import table which will be filled at load time to point to the actual
31901 @file{API.dll} routines. Sometimes you don't have an import library for the
31902 DLL you want to use. The following sections will explain how to build
31903 one. Note that this is optional.
31904
31905 @item
31906 The actual DLL, @file{API.dll}.
31907 @end enumerate
31908
31909 @noindent
31910 Once you have all the above, to compile an Ada application that uses the
31911 services of @file{API.dll} and whose main subprogram is @code{My_Ada_App},
31912 you simply issue the command
31913
31914 @smallexample
31915 $ gnatmake my_ada_app -largs -lAPI
31916 @end smallexample
31917
31918 @noindent
31919 The argument @option{-largs -lAPI} at the end of the @command{gnatmake} command
31920 tells the GNAT linker to look first for a library named @file{API.lib}
31921 (Microsoft-style name) and if not found for a libraries named
31922 @file{libAPI.dll.a}, @file{API.dll.a} or @file{libAPI.a}.
31923 (GNAT-style name). Note that if the Ada package spec for @file{API.dll}
31924 contains the following pragma
31925
31926 @smallexample @c ada
31927 pragma Linker_Options ("-lAPI");
31928 @end smallexample
31929
31930 @noindent
31931 you do not have to add @option{-largs -lAPI} at the end of the
31932 @command{gnatmake} command.
31933
31934 If any one of the items above is missing you will have to create it
31935 yourself. The following sections explain how to do so using as an
31936 example a fictitious DLL called @file{API.dll}.
31937
31938 @node Creating an Ada Spec for the DLL Services
31939 @subsection Creating an Ada Spec for the DLL Services
31940
31941 @noindent
31942 A DLL typically comes with a C/C++ header file which provides the
31943 definitions of the routines and variables exported by the DLL. The Ada
31944 equivalent of this header file is a package spec that contains definitions
31945 for the imported entities. If the DLL you intend to use does not come with
31946 an Ada spec you have to generate one such spec yourself. For example if
31947 the header file of @file{API.dll} is a file @file{api.h} containing the
31948 following two definitions:
31949
31950 @smallexample
31951 @group
31952 @cartouche
31953 int some_var;
31954 int get (char *);
31955 @end cartouche
31956 @end group
31957 @end smallexample
31958
31959 @noindent
31960 then the equivalent Ada spec could be:
31961
31962 @smallexample @c ada
31963 @group
31964 @cartouche
31965 with Interfaces.C.Strings;
31966 package API is
31967 use Interfaces;
31968
31969 Some_Var : C.int;
31970 function Get (Str : C.Strings.Chars_Ptr) return C.int;
31971
31972 private
31973 pragma Import (C, Get);
31974 pragma Import (DLL, Some_Var);
31975 end API;
31976 @end cartouche
31977 @end group
31978 @end smallexample
31979
31980 @noindent
31981 Note that a variable is
31982 @strong{always imported with a Stdcall convention}. A function
31983 can have @code{C} or @code{Stdcall} convention.
31984 (@pxref{Windows Calling Conventions}).
31985
31986 @node Creating an Import Library
31987 @subsection Creating an Import Library
31988 @cindex Import library
31989
31990 @menu
31991 * The Definition File::
31992 * GNAT-Style Import Library::
31993 * Microsoft-Style Import Library::
31994 @end menu
31995
31996 @noindent
31997 If a Microsoft-style import library @file{API.lib} or a GNAT-style
31998 import library @file{libAPI.dll.a} or @file{libAPI.a} is available
31999 with @file{API.dll} you can skip this section. You can also skip this
32000 section if @file{API.dll} or @file{libAPI.dll} is built with GNU tools
32001 as in this case it is possible to link directly against the
32002 DLL. Otherwise read on.
32003
32004 @node The Definition File
32005 @subsubsection The Definition File
32006 @cindex Definition file
32007 @findex .def
32008
32009 @noindent
32010 As previously mentioned, and unlike Unix systems, the list of symbols
32011 that are exported from a DLL must be provided explicitly in Windows.
32012 The main goal of a definition file is precisely that: list the symbols
32013 exported by a DLL. A definition file (usually a file with a @code{.def}
32014 suffix) has the following structure:
32015
32016 @smallexample
32017 @group
32018 @cartouche
32019 @r{[}LIBRARY @var{name}@r{]}
32020 @r{[}DESCRIPTION @var{string}@r{]}
32021 EXPORTS
32022 @var{symbol1}
32023 @var{symbol2}
32024 @dots{}
32025 @end cartouche
32026 @end group
32027 @end smallexample
32028
32029 @table @code
32030 @item LIBRARY @var{name}
32031 This section, which is optional, gives the name of the DLL.
32032
32033 @item DESCRIPTION @var{string}
32034 This section, which is optional, gives a description string that will be
32035 embedded in the import library.
32036
32037 @item EXPORTS
32038 This section gives the list of exported symbols (procedures, functions or
32039 variables). For instance in the case of @file{API.dll} the @code{EXPORTS}
32040 section of @file{API.def} looks like:
32041
32042 @smallexample
32043 @group
32044 @cartouche
32045 EXPORTS
32046 some_var
32047 get
32048 @end cartouche
32049 @end group
32050 @end smallexample
32051 @end table
32052
32053 @noindent
32054 Note that you must specify the correct suffix (@code{@@}@code{@var{nn}})
32055 (@pxref{Windows Calling Conventions}) for a Stdcall
32056 calling convention function in the exported symbols list.
32057
32058 @noindent
32059 There can actually be other sections in a definition file, but these
32060 sections are not relevant to the discussion at hand.
32061
32062 @node GNAT-Style Import Library
32063 @subsubsection GNAT-Style Import Library
32064
32065 @noindent
32066 To create a static import library from @file{API.dll} with the GNAT tools
32067 you should proceed as follows:
32068
32069 @enumerate
32070 @item
32071 Create the definition file @file{API.def} (@pxref{The Definition File}).
32072 For that use the @code{dll2def} tool as follows:
32073
32074 @smallexample
32075 $ dll2def API.dll > API.def
32076 @end smallexample
32077
32078 @noindent
32079 @code{dll2def} is a very simple tool: it takes as input a DLL and prints
32080 to standard output the list of entry points in the DLL. Note that if
32081 some routines in the DLL have the @code{Stdcall} convention
32082 (@pxref{Windows Calling Conventions}) with stripped @code{@@}@var{nn}
32083 suffix then you'll have to edit @file{api.def} to add it, and specify
32084 @option{-k} to @command{gnatdll} when creating the import library.
32085
32086 @noindent
32087 Here are some hints to find the right @code{@@}@var{nn} suffix.
32088
32089 @enumerate
32090 @item
32091 If you have the Microsoft import library (.lib), it is possible to get
32092 the right symbols by using Microsoft @code{dumpbin} tool (see the
32093 corresponding Microsoft documentation for further details).
32094
32095 @smallexample
32096 $ dumpbin /exports api.lib
32097 @end smallexample
32098
32099 @item
32100 If you have a message about a missing symbol at link time the compiler
32101 tells you what symbol is expected. You just have to go back to the
32102 definition file and add the right suffix.
32103 @end enumerate
32104
32105 @item
32106 Build the import library @code{libAPI.dll.a}, using @code{gnatdll}
32107 (@pxref{Using gnatdll}) as follows:
32108
32109 @smallexample
32110 $ gnatdll -e API.def -d API.dll
32111 @end smallexample
32112
32113 @noindent
32114 @code{gnatdll} takes as input a definition file @file{API.def} and the
32115 name of the DLL containing the services listed in the definition file
32116 @file{API.dll}. The name of the static import library generated is
32117 computed from the name of the definition file as follows: if the
32118 definition file name is @var{xyz}@code{.def}, the import library name will
32119 be @code{lib}@var{xyz}@code{.a}. Note that in the previous example option
32120 @option{-e} could have been removed because the name of the definition
32121 file (before the ``@code{.def}'' suffix) is the same as the name of the
32122 DLL (@pxref{Using gnatdll} for more information about @code{gnatdll}).
32123 @end enumerate
32124
32125 @node Microsoft-Style Import Library
32126 @subsubsection Microsoft-Style Import Library
32127
32128 @noindent
32129 With GNAT you can either use a GNAT-style or Microsoft-style import
32130 library. A Microsoft import library is needed only if you plan to make an
32131 Ada DLL available to applications developed with Microsoft
32132 tools (@pxref{Mixed-Language Programming on Windows}).
32133
32134 To create a Microsoft-style import library for @file{API.dll} you
32135 should proceed as follows:
32136
32137 @enumerate
32138 @item
32139 Create the definition file @file{API.def} from the DLL. For this use either
32140 the @code{dll2def} tool as described above or the Microsoft @code{dumpbin}
32141 tool (see the corresponding Microsoft documentation for further details).
32142
32143 @item
32144 Build the actual import library using Microsoft's @code{lib} utility:
32145
32146 @smallexample
32147 $ lib -machine:IX86 -def:API.def -out:API.lib
32148 @end smallexample
32149
32150 @noindent
32151 If you use the above command the definition file @file{API.def} must
32152 contain a line giving the name of the DLL:
32153
32154 @smallexample
32155 LIBRARY "API"
32156 @end smallexample
32157
32158 @noindent
32159 See the Microsoft documentation for further details about the usage of
32160 @code{lib}.
32161 @end enumerate
32162
32163 @node Building DLLs with GNAT
32164 @section Building DLLs with GNAT
32165 @cindex DLLs, building
32166
32167 @noindent
32168 This section explain how to build DLLs using the GNAT built-in DLL
32169 support. With the following procedure it is straight forward to build
32170 and use DLLs with GNAT.
32171
32172 @enumerate
32173
32174 @item building object files
32175
32176 The first step is to build all objects files that are to be included
32177 into the DLL. This is done by using the standard @command{gnatmake} tool.
32178
32179 @item building the DLL
32180
32181 To build the DLL you must use @command{gcc}'s @option{-shared}
32182 option. It is quite simple to use this method:
32183
32184 @smallexample
32185 $ gcc -shared -o api.dll obj1.o obj2.o @dots{}
32186 @end smallexample
32187
32188 It is important to note that in this case all symbols found in the
32189 object files are automatically exported. It is possible to restrict
32190 the set of symbols to export by passing to @command{gcc} a definition
32191 file, @pxref{The Definition File}. For example:
32192
32193 @smallexample
32194 $ gcc -shared -o api.dll api.def obj1.o obj2.o @dots{}
32195 @end smallexample
32196
32197 If you use a definition file you must export the elaboration procedures
32198 for every package that required one. Elaboration procedures are named
32199 using the package name followed by "_E".
32200
32201 @item preparing DLL to be used
32202
32203 For the DLL to be used by client programs the bodies must be hidden
32204 from it and the .ali set with read-only attribute. This is very important
32205 otherwise GNAT will recompile all packages and will not actually use
32206 the code in the DLL. For example:
32207
32208 @smallexample
32209 $ mkdir apilib
32210 $ copy *.ads *.ali api.dll apilib
32211 $ attrib +R apilib\*.ali
32212 @end smallexample
32213
32214 @end enumerate
32215
32216 At this point it is possible to use the DLL by directly linking
32217 against it. Note that you must use the GNAT shared runtime when using
32218 GNAT shared libraries. This is achieved by using @option{-shared} binder's
32219 option.
32220
32221 @smallexample
32222 $ gnatmake main -Iapilib -bargs -shared -largs -Lapilib -lAPI
32223 @end smallexample
32224
32225 @node Building DLLs with GNAT Project files
32226 @section Building DLLs with GNAT Project files
32227 @cindex DLLs, building
32228
32229 @noindent
32230 There is nothing specific to Windows in the build process.
32231 @pxref{Library Projects}.
32232
32233 @noindent
32234 Due to a system limitation, it is not possible under Windows to create threads
32235 when inside the @code{DllMain} routine which is used for auto-initialization
32236 of shared libraries, so it is not possible to have library level tasks in SALs.
32237
32238 @node Building DLLs with gnatdll
32239 @section Building DLLs with gnatdll
32240 @cindex DLLs, building
32241
32242 @menu
32243 * Limitations When Using Ada DLLs from Ada::
32244 * Exporting Ada Entities::
32245 * Ada DLLs and Elaboration::
32246 * Ada DLLs and Finalization::
32247 * Creating a Spec for Ada DLLs::
32248 * Creating the Definition File::
32249 * Using gnatdll::
32250 @end menu
32251
32252 @noindent
32253 Note that it is preferred to use the built-in GNAT DLL support
32254 (@pxref{Building DLLs with GNAT}) or GNAT Project files
32255 (@pxref{Building DLLs with GNAT Project files}) to build DLLs.
32256
32257 This section explains how to build DLLs containing Ada code using
32258 @code{gnatdll}. These DLLs will be referred to as Ada DLLs in the
32259 remainder of this section.
32260
32261 The steps required to build an Ada DLL that is to be used by Ada as well as
32262 non-Ada applications are as follows:
32263
32264 @enumerate
32265 @item
32266 You need to mark each Ada @i{entity} exported by the DLL with a @code{C} or
32267 @code{Stdcall} calling convention to avoid any Ada name mangling for the
32268 entities exported by the DLL (@pxref{Exporting Ada Entities}). You can
32269 skip this step if you plan to use the Ada DLL only from Ada applications.
32270
32271 @item
32272 Your Ada code must export an initialization routine which calls the routine
32273 @code{adainit} generated by @command{gnatbind} to perform the elaboration of
32274 the Ada code in the DLL (@pxref{Ada DLLs and Elaboration}). The initialization
32275 routine exported by the Ada DLL must be invoked by the clients of the DLL
32276 to initialize the DLL.
32277
32278 @item
32279 When useful, the DLL should also export a finalization routine which calls
32280 routine @code{adafinal} generated by @command{gnatbind} to perform the
32281 finalization of the Ada code in the DLL (@pxref{Ada DLLs and Finalization}).
32282 The finalization routine exported by the Ada DLL must be invoked by the
32283 clients of the DLL when the DLL services are no further needed.
32284
32285 @item
32286 You must provide a spec for the services exported by the Ada DLL in each
32287 of the programming languages to which you plan to make the DLL available.
32288
32289 @item
32290 You must provide a definition file listing the exported entities
32291 (@pxref{The Definition File}).
32292
32293 @item
32294 Finally you must use @code{gnatdll} to produce the DLL and the import
32295 library (@pxref{Using gnatdll}).
32296 @end enumerate
32297
32298 @noindent
32299 Note that a relocatable DLL stripped using the @code{strip}
32300 binutils tool will not be relocatable anymore. To build a DLL without
32301 debug information pass @code{-largs -s} to @code{gnatdll}. This
32302 restriction does not apply to a DLL built using a Library Project.
32303 @pxref{Library Projects}.
32304
32305 @node Limitations When Using Ada DLLs from Ada
32306 @subsection Limitations When Using Ada DLLs from Ada
32307
32308 @noindent
32309 When using Ada DLLs from Ada applications there is a limitation users
32310 should be aware of. Because on Windows the GNAT run time is not in a DLL of
32311 its own, each Ada DLL includes a part of the GNAT run time. Specifically,
32312 each Ada DLL includes the services of the GNAT run time that are necessary
32313 to the Ada code inside the DLL. As a result, when an Ada program uses an
32314 Ada DLL there are two independent GNAT run times: one in the Ada DLL and
32315 one in the main program.
32316
32317 It is therefore not possible to exchange GNAT run-time objects between the
32318 Ada DLL and the main Ada program. Example of GNAT run-time objects are file
32319 handles (e.g.@: @code{Text_IO.File_Type}), tasks types, protected objects
32320 types, etc.
32321
32322 It is completely safe to exchange plain elementary, array or record types,
32323 Windows object handles, etc.
32324
32325 @node Exporting Ada Entities
32326 @subsection Exporting Ada Entities
32327 @cindex Export table
32328
32329 @noindent
32330 Building a DLL is a way to encapsulate a set of services usable from any
32331 application. As a result, the Ada entities exported by a DLL should be
32332 exported with the @code{C} or @code{Stdcall} calling conventions to avoid
32333 any Ada name mangling. As an example here is an Ada package
32334 @code{API}, spec and body, exporting two procedures, a function, and a
32335 variable:
32336
32337 @smallexample @c ada
32338 @group
32339 @cartouche
32340 with Interfaces.C; use Interfaces;
32341 package API is
32342 Count : C.int := 0;
32343 function Factorial (Val : C.int) return C.int;
32344
32345 procedure Initialize_API;
32346 procedure Finalize_API;
32347 -- Initialization & Finalization routines. More in the next section.
32348 private
32349 pragma Export (C, Initialize_API);
32350 pragma Export (C, Finalize_API);
32351 pragma Export (C, Count);
32352 pragma Export (C, Factorial);
32353 end API;
32354 @end cartouche
32355 @end group
32356 @end smallexample
32357
32358 @smallexample @c ada
32359 @group
32360 @cartouche
32361 package body API is
32362 function Factorial (Val : C.int) return C.int is
32363 Fact : C.int := 1;
32364 begin
32365 Count := Count + 1;
32366 for K in 1 .. Val loop
32367 Fact := Fact * K;
32368 end loop;
32369 return Fact;
32370 end Factorial;
32371
32372 procedure Initialize_API is
32373 procedure Adainit;
32374 pragma Import (C, Adainit);
32375 begin
32376 Adainit;
32377 end Initialize_API;
32378
32379 procedure Finalize_API is
32380 procedure Adafinal;
32381 pragma Import (C, Adafinal);
32382 begin
32383 Adafinal;
32384 end Finalize_API;
32385 end API;
32386 @end cartouche
32387 @end group
32388 @end smallexample
32389
32390 @noindent
32391 If the Ada DLL you are building will only be used by Ada applications
32392 you do not have to export Ada entities with a @code{C} or @code{Stdcall}
32393 convention. As an example, the previous package could be written as
32394 follows:
32395
32396 @smallexample @c ada
32397 @group
32398 @cartouche
32399 package API is
32400 Count : Integer := 0;
32401 function Factorial (Val : Integer) return Integer;
32402
32403 procedure Initialize_API;
32404 procedure Finalize_API;
32405 -- Initialization and Finalization routines.
32406 end API;
32407 @end cartouche
32408 @end group
32409 @end smallexample
32410
32411 @smallexample @c ada
32412 @group
32413 @cartouche
32414 package body API is
32415 function Factorial (Val : Integer) return Integer is
32416 Fact : Integer := 1;
32417 begin
32418 Count := Count + 1;
32419 for K in 1 .. Val loop
32420 Fact := Fact * K;
32421 end loop;
32422 return Fact;
32423 end Factorial;
32424
32425 @dots{}
32426 -- The remainder of this package body is unchanged.
32427 end API;
32428 @end cartouche
32429 @end group
32430 @end smallexample
32431
32432 @noindent
32433 Note that if you do not export the Ada entities with a @code{C} or
32434 @code{Stdcall} convention you will have to provide the mangled Ada names
32435 in the definition file of the Ada DLL
32436 (@pxref{Creating the Definition File}).
32437
32438 @node Ada DLLs and Elaboration
32439 @subsection Ada DLLs and Elaboration
32440 @cindex DLLs and elaboration
32441
32442 @noindent
32443 The DLL that you are building contains your Ada code as well as all the
32444 routines in the Ada library that are needed by it. The first thing a
32445 user of your DLL must do is elaborate the Ada code
32446 (@pxref{Elaboration Order Handling in GNAT}).
32447
32448 To achieve this you must export an initialization routine
32449 (@code{Initialize_API} in the previous example), which must be invoked
32450 before using any of the DLL services. This elaboration routine must call
32451 the Ada elaboration routine @code{adainit} generated by the GNAT binder
32452 (@pxref{Binding with Non-Ada Main Programs}). See the body of
32453 @code{Initialize_Api} for an example. Note that the GNAT binder is
32454 automatically invoked during the DLL build process by the @code{gnatdll}
32455 tool (@pxref{Using gnatdll}).
32456
32457 When a DLL is loaded, Windows systematically invokes a routine called
32458 @code{DllMain}. It would therefore be possible to call @code{adainit}
32459 directly from @code{DllMain} without having to provide an explicit
32460 initialization routine. Unfortunately, it is not possible to call
32461 @code{adainit} from the @code{DllMain} if your program has library level
32462 tasks because access to the @code{DllMain} entry point is serialized by
32463 the system (that is, only a single thread can execute ``through'' it at a
32464 time), which means that the GNAT run time will deadlock waiting for the
32465 newly created task to complete its initialization.
32466
32467 @node Ada DLLs and Finalization
32468 @subsection Ada DLLs and Finalization
32469 @cindex DLLs and finalization
32470
32471 @noindent
32472 When the services of an Ada DLL are no longer needed, the client code should
32473 invoke the DLL finalization routine, if available. The DLL finalization
32474 routine is in charge of releasing all resources acquired by the DLL. In the
32475 case of the Ada code contained in the DLL, this is achieved by calling
32476 routine @code{adafinal} generated by the GNAT binder
32477 (@pxref{Binding with Non-Ada Main Programs}).
32478 See the body of @code{Finalize_Api} for an
32479 example. As already pointed out the GNAT binder is automatically invoked
32480 during the DLL build process by the @code{gnatdll} tool
32481 (@pxref{Using gnatdll}).
32482
32483 @node Creating a Spec for Ada DLLs
32484 @subsection Creating a Spec for Ada DLLs
32485
32486 @noindent
32487 To use the services exported by the Ada DLL from another programming
32488 language (e.g.@: C), you have to translate the specs of the exported Ada
32489 entities in that language. For instance in the case of @code{API.dll},
32490 the corresponding C header file could look like:
32491
32492 @smallexample
32493 @group
32494 @cartouche
32495 extern int *_imp__count;
32496 #define count (*_imp__count)
32497 int factorial (int);
32498 @end cartouche
32499 @end group
32500 @end smallexample
32501
32502 @noindent
32503 It is important to understand that when building an Ada DLL to be used by
32504 other Ada applications, you need two different specs for the packages
32505 contained in the DLL: one for building the DLL and the other for using
32506 the DLL. This is because the @code{DLL} calling convention is needed to
32507 use a variable defined in a DLL, but when building the DLL, the variable
32508 must have either the @code{Ada} or @code{C} calling convention. As an
32509 example consider a DLL comprising the following package @code{API}:
32510
32511 @smallexample @c ada
32512 @group
32513 @cartouche
32514 package API is
32515 Count : Integer := 0;
32516 @dots{}
32517 -- Remainder of the package omitted.
32518 end API;
32519 @end cartouche
32520 @end group
32521 @end smallexample
32522
32523 @noindent
32524 After producing a DLL containing package @code{API}, the spec that
32525 must be used to import @code{API.Count} from Ada code outside of the
32526 DLL is:
32527
32528 @smallexample @c ada
32529 @group
32530 @cartouche
32531 package API is
32532 Count : Integer;
32533 pragma Import (DLL, Count);
32534 end API;
32535 @end cartouche
32536 @end group
32537 @end smallexample
32538
32539 @node Creating the Definition File
32540 @subsection Creating the Definition File
32541
32542 @noindent
32543 The definition file is the last file needed to build the DLL. It lists
32544 the exported symbols. As an example, the definition file for a DLL
32545 containing only package @code{API} (where all the entities are exported
32546 with a @code{C} calling convention) is:
32547
32548 @smallexample
32549 @group
32550 @cartouche
32551 EXPORTS
32552 count
32553 factorial
32554 finalize_api
32555 initialize_api
32556 @end cartouche
32557 @end group
32558 @end smallexample
32559
32560 @noindent
32561 If the @code{C} calling convention is missing from package @code{API},
32562 then the definition file contains the mangled Ada names of the above
32563 entities, which in this case are:
32564
32565 @smallexample
32566 @group
32567 @cartouche
32568 EXPORTS
32569 api__count
32570 api__factorial
32571 api__finalize_api
32572 api__initialize_api
32573 @end cartouche
32574 @end group
32575 @end smallexample
32576
32577 @node Using gnatdll
32578 @subsection Using @code{gnatdll}
32579 @findex gnatdll
32580
32581 @menu
32582 * gnatdll Example::
32583 * gnatdll behind the Scenes::
32584 * Using dlltool::
32585 @end menu
32586
32587 @noindent
32588 @code{gnatdll} is a tool to automate the DLL build process once all the Ada
32589 and non-Ada sources that make up your DLL have been compiled.
32590 @code{gnatdll} is actually in charge of two distinct tasks: build the
32591 static import library for the DLL and the actual DLL. The form of the
32592 @code{gnatdll} command is
32593
32594 @smallexample
32595 @cartouche
32596 $ gnatdll @ovar{switches} @var{list-of-files} @r{[}-largs @var{opts}@r{]}
32597 @end cartouche
32598 @end smallexample
32599
32600 @noindent
32601 where @var{list-of-files} is a list of ALI and object files. The object
32602 file list must be the exact list of objects corresponding to the non-Ada
32603 sources whose services are to be included in the DLL. The ALI file list
32604 must be the exact list of ALI files for the corresponding Ada sources
32605 whose services are to be included in the DLL. If @var{list-of-files} is
32606 missing, only the static import library is generated.
32607
32608 @noindent
32609 You may specify any of the following switches to @code{gnatdll}:
32610
32611 @table @code
32612 @item -a@ovar{address}
32613 @cindex @option{-a} (@code{gnatdll})
32614 Build a non-relocatable DLL at @var{address}. If @var{address} is not
32615 specified the default address @var{0x11000000} will be used. By default,
32616 when this switch is missing, @code{gnatdll} builds relocatable DLL. We
32617 advise the reader to build relocatable DLL.
32618
32619 @item -b @var{address}
32620 @cindex @option{-b} (@code{gnatdll})
32621 Set the relocatable DLL base address. By default the address is
32622 @code{0x11000000}.
32623
32624 @item -bargs @var{opts}
32625 @cindex @option{-bargs} (@code{gnatdll})
32626 Binder options. Pass @var{opts} to the binder.
32627
32628 @item -d @var{dllfile}
32629 @cindex @option{-d} (@code{gnatdll})
32630 @var{dllfile} is the name of the DLL. This switch must be present for
32631 @code{gnatdll} to do anything. The name of the generated import library is
32632 obtained algorithmically from @var{dllfile} as shown in the following
32633 example: if @var{dllfile} is @code{xyz.dll}, the import library name is
32634 @code{libxyz.dll.a}. The name of the definition file to use (if not specified
32635 by option @option{-e}) is obtained algorithmically from @var{dllfile}
32636 as shown in the following example:
32637 if @var{dllfile} is @code{xyz.dll}, the definition
32638 file used is @code{xyz.def}.
32639
32640 @item -e @var{deffile}
32641 @cindex @option{-e} (@code{gnatdll})
32642 @var{deffile} is the name of the definition file.
32643
32644 @item -g
32645 @cindex @option{-g} (@code{gnatdll})
32646 Generate debugging information. This information is stored in the object
32647 file and copied from there to the final DLL file by the linker,
32648 where it can be read by the debugger. You must use the
32649 @option{-g} switch if you plan on using the debugger or the symbolic
32650 stack traceback.
32651
32652 @item -h
32653 @cindex @option{-h} (@code{gnatdll})
32654 Help mode. Displays @code{gnatdll} switch usage information.
32655
32656 @item -Idir
32657 @cindex @option{-I} (@code{gnatdll})
32658 Direct @code{gnatdll} to search the @var{dir} directory for source and
32659 object files needed to build the DLL.
32660 (@pxref{Search Paths and the Run-Time Library (RTL)}).
32661
32662 @item -k
32663 @cindex @option{-k} (@code{gnatdll})
32664 Removes the @code{@@}@var{nn} suffix from the import library's exported
32665 names, but keeps them for the link names. You must specify this
32666 option if you want to use a @code{Stdcall} function in a DLL for which
32667 the @code{@@}@var{nn} suffix has been removed. This is the case for most
32668 of the Windows NT DLL for example. This option has no effect when
32669 @option{-n} option is specified.
32670
32671 @item -l @var{file}
32672 @cindex @option{-l} (@code{gnatdll})
32673 The list of ALI and object files used to build the DLL are listed in
32674 @var{file}, instead of being given in the command line. Each line in
32675 @var{file} contains the name of an ALI or object file.
32676
32677 @item -n
32678 @cindex @option{-n} (@code{gnatdll})
32679 No Import. Do not create the import library.
32680
32681 @item -q
32682 @cindex @option{-q} (@code{gnatdll})
32683 Quiet mode. Do not display unnecessary messages.
32684
32685 @item -v
32686 @cindex @option{-v} (@code{gnatdll})
32687 Verbose mode. Display extra information.
32688
32689 @item -largs @var{opts}
32690 @cindex @option{-largs} (@code{gnatdll})
32691 Linker options. Pass @var{opts} to the linker.
32692 @end table
32693
32694 @node gnatdll Example
32695 @subsubsection @code{gnatdll} Example
32696
32697 @noindent
32698 As an example the command to build a relocatable DLL from @file{api.adb}
32699 once @file{api.adb} has been compiled and @file{api.def} created is
32700
32701 @smallexample
32702 $ gnatdll -d api.dll api.ali
32703 @end smallexample
32704
32705 @noindent
32706 The above command creates two files: @file{libapi.dll.a} (the import
32707 library) and @file{api.dll} (the actual DLL). If you want to create
32708 only the DLL, just type:
32709
32710 @smallexample
32711 $ gnatdll -d api.dll -n api.ali
32712 @end smallexample
32713
32714 @noindent
32715 Alternatively if you want to create just the import library, type:
32716
32717 @smallexample
32718 $ gnatdll -d api.dll
32719 @end smallexample
32720
32721 @node gnatdll behind the Scenes
32722 @subsubsection @code{gnatdll} behind the Scenes
32723
32724 @noindent
32725 This section details the steps involved in creating a DLL. @code{gnatdll}
32726 does these steps for you. Unless you are interested in understanding what
32727 goes on behind the scenes, you should skip this section.
32728
32729 We use the previous example of a DLL containing the Ada package @code{API},
32730 to illustrate the steps necessary to build a DLL. The starting point is a
32731 set of objects that will make up the DLL and the corresponding ALI
32732 files. In the case of this example this means that @file{api.o} and
32733 @file{api.ali} are available. To build a relocatable DLL, @code{gnatdll} does
32734 the following:
32735
32736 @enumerate
32737 @item
32738 @code{gnatdll} builds the base file (@file{api.base}). A base file gives
32739 the information necessary to generate relocation information for the
32740 DLL.
32741
32742 @smallexample
32743 @group
32744 $ gnatbind -n api
32745 $ gnatlink api -o api.jnk -mdll -Wl,--base-file,api.base
32746 @end group
32747 @end smallexample
32748
32749 @noindent
32750 In addition to the base file, the @command{gnatlink} command generates an
32751 output file @file{api.jnk} which can be discarded. The @option{-mdll} switch
32752 asks @command{gnatlink} to generate the routines @code{DllMain} and
32753 @code{DllMainCRTStartup} that are called by the Windows loader when the DLL
32754 is loaded into memory.
32755
32756 @item
32757 @code{gnatdll} uses @code{dlltool} (@pxref{Using dlltool}) to build the
32758 export table (@file{api.exp}). The export table contains the relocation
32759 information in a form which can be used during the final link to ensure
32760 that the Windows loader is able to place the DLL anywhere in memory.
32761
32762 @smallexample
32763 @group
32764 $ dlltool --dllname api.dll --def api.def --base-file api.base \
32765 --output-exp api.exp
32766 @end group
32767 @end smallexample
32768
32769 @item
32770 @code{gnatdll} builds the base file using the new export table. Note that
32771 @command{gnatbind} must be called once again since the binder generated file
32772 has been deleted during the previous call to @command{gnatlink}.
32773
32774 @smallexample
32775 @group
32776 $ gnatbind -n api
32777 $ gnatlink api -o api.jnk api.exp -mdll
32778 -Wl,--base-file,api.base
32779 @end group
32780 @end smallexample
32781
32782 @item
32783 @code{gnatdll} builds the new export table using the new base file and
32784 generates the DLL import library @file{libAPI.dll.a}.
32785
32786 @smallexample
32787 @group
32788 $ dlltool --dllname api.dll --def api.def --base-file api.base \
32789 --output-exp api.exp --output-lib libAPI.a
32790 @end group
32791 @end smallexample
32792
32793 @item
32794 Finally @code{gnatdll} builds the relocatable DLL using the final export
32795 table.
32796
32797 @smallexample
32798 @group
32799 $ gnatbind -n api
32800 $ gnatlink api api.exp -o api.dll -mdll
32801 @end group
32802 @end smallexample
32803 @end enumerate
32804
32805 @node Using dlltool
32806 @subsubsection Using @code{dlltool}
32807
32808 @noindent
32809 @code{dlltool} is the low-level tool used by @code{gnatdll} to build
32810 DLLs and static import libraries. This section summarizes the most
32811 common @code{dlltool} switches. The form of the @code{dlltool} command
32812 is
32813
32814 @smallexample
32815 $ dlltool @ovar{switches}
32816 @end smallexample
32817
32818 @noindent
32819 @code{dlltool} switches include:
32820
32821 @table @option
32822 @item --base-file @var{basefile}
32823 @cindex @option{--base-file} (@command{dlltool})
32824 Read the base file @var{basefile} generated by the linker. This switch
32825 is used to create a relocatable DLL.
32826
32827 @item --def @var{deffile}
32828 @cindex @option{--def} (@command{dlltool})
32829 Read the definition file.
32830
32831 @item --dllname @var{name}
32832 @cindex @option{--dllname} (@command{dlltool})
32833 Gives the name of the DLL. This switch is used to embed the name of the
32834 DLL in the static import library generated by @code{dlltool} with switch
32835 @option{--output-lib}.
32836
32837 @item -k
32838 @cindex @option{-k} (@command{dlltool})
32839 Kill @code{@@}@var{nn} from exported names
32840 (@pxref{Windows Calling Conventions}
32841 for a discussion about @code{Stdcall}-style symbols.
32842
32843 @item --help
32844 @cindex @option{--help} (@command{dlltool})
32845 Prints the @code{dlltool} switches with a concise description.
32846
32847 @item --output-exp @var{exportfile}
32848 @cindex @option{--output-exp} (@command{dlltool})
32849 Generate an export file @var{exportfile}. The export file contains the
32850 export table (list of symbols in the DLL) and is used to create the DLL.
32851
32852 @item --output-lib @var{libfile}
32853 @cindex @option{--output-lib} (@command{dlltool})
32854 Generate a static import library @var{libfile}.
32855
32856 @item -v
32857 @cindex @option{-v} (@command{dlltool})
32858 Verbose mode.
32859
32860 @item --as @var{assembler-name}
32861 @cindex @option{--as} (@command{dlltool})
32862 Use @var{assembler-name} as the assembler. The default is @code{as}.
32863 @end table
32864
32865 @node GNAT and Windows Resources
32866 @section GNAT and Windows Resources
32867 @cindex Resources, windows
32868
32869 @menu
32870 * Building Resources::
32871 * Compiling Resources::
32872 * Using Resources::
32873 @end menu
32874
32875 @noindent
32876 Resources are an easy way to add Windows specific objects to your
32877 application. The objects that can be added as resources include:
32878
32879 @itemize @bullet
32880 @item
32881 menus
32882
32883 @item
32884 accelerators
32885
32886 @item
32887 dialog boxes
32888
32889 @item
32890 string tables
32891
32892 @item
32893 bitmaps
32894
32895 @item
32896 cursors
32897
32898 @item
32899 icons
32900
32901 @item
32902 fonts
32903 @end itemize
32904
32905 @noindent
32906 This section explains how to build, compile and use resources.
32907
32908 @node Building Resources
32909 @subsection Building Resources
32910 @cindex Resources, building
32911
32912 @noindent
32913 A resource file is an ASCII file. By convention resource files have an
32914 @file{.rc} extension.
32915 The easiest way to build a resource file is to use Microsoft tools
32916 such as @code{imagedit.exe} to build bitmaps, icons and cursors and
32917 @code{dlgedit.exe} to build dialogs.
32918 It is always possible to build an @file{.rc} file yourself by writing a
32919 resource script.
32920
32921 It is not our objective to explain how to write a resource file. A
32922 complete description of the resource script language can be found in the
32923 Microsoft documentation.
32924
32925 @node Compiling Resources
32926 @subsection Compiling Resources
32927 @findex rc
32928 @findex windres
32929 @cindex Resources, compiling
32930
32931 @noindent
32932 This section describes how to build a GNAT-compatible (COFF) object file
32933 containing the resources. This is done using the Resource Compiler
32934 @code{windres} as follows:
32935
32936 @smallexample
32937 $ windres -i myres.rc -o myres.o
32938 @end smallexample
32939
32940 @noindent
32941 By default @code{windres} will run @command{gcc} to preprocess the @file{.rc}
32942 file. You can specify an alternate preprocessor (usually named
32943 @file{cpp.exe}) using the @code{windres} @option{--preprocessor}
32944 parameter. A list of all possible options may be obtained by entering
32945 the command @code{windres} @option{--help}.
32946
32947 It is also possible to use the Microsoft resource compiler @code{rc.exe}
32948 to produce a @file{.res} file (binary resource file). See the
32949 corresponding Microsoft documentation for further details. In this case
32950 you need to use @code{windres} to translate the @file{.res} file to a
32951 GNAT-compatible object file as follows:
32952
32953 @smallexample
32954 $ windres -i myres.res -o myres.o
32955 @end smallexample
32956
32957 @node Using Resources
32958 @subsection Using Resources
32959 @cindex Resources, using
32960
32961 @noindent
32962 To include the resource file in your program just add the
32963 GNAT-compatible object file for the resource(s) to the linker
32964 arguments. With @command{gnatmake} this is done by using the @option{-largs}
32965 option:
32966
32967 @smallexample
32968 $ gnatmake myprog -largs myres.o
32969 @end smallexample
32970
32971 @node Debugging a DLL
32972 @section Debugging a DLL
32973 @cindex DLL debugging
32974
32975 @menu
32976 * Program and DLL Both Built with GCC/GNAT::
32977 * Program Built with Foreign Tools and DLL Built with GCC/GNAT::
32978 @end menu
32979
32980 @noindent
32981 Debugging a DLL is similar to debugging a standard program. But
32982 we have to deal with two different executable parts: the DLL and the
32983 program that uses it. We have the following four possibilities:
32984
32985 @enumerate 1
32986 @item
32987 The program and the DLL are built with @code{GCC/GNAT}.
32988 @item
32989 The program is built with foreign tools and the DLL is built with
32990 @code{GCC/GNAT}.
32991 @item
32992 The program is built with @code{GCC/GNAT} and the DLL is built with
32993 foreign tools.
32994 @item
32995 @end enumerate
32996
32997 @noindent
32998 In this section we address only cases one and two above.
32999 There is no point in trying to debug
33000 a DLL with @code{GNU/GDB}, if there is no GDB-compatible debugging
33001 information in it. To do so you must use a debugger compatible with the
33002 tools suite used to build the DLL.
33003
33004 @node Program and DLL Both Built with GCC/GNAT
33005 @subsection Program and DLL Both Built with GCC/GNAT
33006
33007 @noindent
33008 This is the simplest case. Both the DLL and the program have @code{GDB}
33009 compatible debugging information. It is then possible to break anywhere in
33010 the process. Let's suppose here that the main procedure is named
33011 @code{ada_main} and that in the DLL there is an entry point named
33012 @code{ada_dll}.
33013
33014 @noindent
33015 The DLL (@pxref{Introduction to Dynamic Link Libraries (DLLs)}) and
33016 program must have been built with the debugging information (see GNAT -g
33017 switch). Here are the step-by-step instructions for debugging it:
33018
33019 @enumerate 1
33020 @item Launch @code{GDB} on the main program.
33021
33022 @smallexample
33023 $ gdb -nw ada_main
33024 @end smallexample
33025
33026 @item Start the program and stop at the beginning of the main procedure
33027
33028 @smallexample
33029 (gdb) start
33030 @end smallexample
33031
33032 @noindent
33033 This step is required to be able to set a breakpoint inside the DLL. As long
33034 as the program is not run, the DLL is not loaded. This has the
33035 consequence that the DLL debugging information is also not loaded, so it is not
33036 possible to set a breakpoint in the DLL.
33037
33038 @item Set a breakpoint inside the DLL
33039
33040 @smallexample
33041 (gdb) break ada_dll
33042 (gdb) cont
33043 @end smallexample
33044
33045 @end enumerate
33046
33047 @noindent
33048 At this stage a breakpoint is set inside the DLL. From there on
33049 you can use the standard approach to debug the whole program
33050 (@pxref{Running and Debugging Ada Programs}).
33051
33052 @ignore
33053 @c This used to work, probably because the DLLs were non-relocatable
33054 @c keep this section around until the problem is sorted out.
33055
33056 To break on the @code{DllMain} routine it is not possible to follow
33057 the procedure above. At the time the program stop on @code{ada_main}
33058 the @code{DllMain} routine as already been called. Either you can use
33059 the procedure below @pxref{Debugging the DLL Directly} or this procedure:
33060
33061 @enumerate 1
33062 @item Launch @code{GDB} on the main program.
33063
33064 @smallexample
33065 $ gdb ada_main
33066 @end smallexample
33067
33068 @item Load DLL symbols
33069
33070 @smallexample
33071 (gdb) add-sym api.dll
33072 @end smallexample
33073
33074 @item Set a breakpoint inside the DLL
33075
33076 @smallexample
33077 (gdb) break ada_dll.adb:45
33078 @end smallexample
33079
33080 Note that at this point it is not possible to break using the routine symbol
33081 directly as the program is not yet running. The solution is to break
33082 on the proper line (break in @file{ada_dll.adb} line 45).
33083
33084 @item Start the program
33085
33086 @smallexample
33087 (gdb) run
33088 @end smallexample
33089
33090 @end enumerate
33091 @end ignore
33092
33093 @node Program Built with Foreign Tools and DLL Built with GCC/GNAT
33094 @subsection Program Built with Foreign Tools and DLL Built with GCC/GNAT
33095
33096 @menu
33097 * Debugging the DLL Directly::
33098 * Attaching to a Running Process::
33099 @end menu
33100
33101 @noindent
33102 In this case things are slightly more complex because it is not possible to
33103 start the main program and then break at the beginning to load the DLL and the
33104 associated DLL debugging information. It is not possible to break at the
33105 beginning of the program because there is no @code{GDB} debugging information,
33106 and therefore there is no direct way of getting initial control. This
33107 section addresses this issue by describing some methods that can be used
33108 to break somewhere in the DLL to debug it.
33109
33110 @noindent
33111 First suppose that the main procedure is named @code{main} (this is for
33112 example some C code built with Microsoft Visual C) and that there is a
33113 DLL named @code{test.dll} containing an Ada entry point named
33114 @code{ada_dll}.
33115
33116 @noindent
33117 The DLL (@pxref{Introduction to Dynamic Link Libraries (DLLs)}) must have
33118 been built with debugging information (see GNAT -g option).
33119
33120 @node Debugging the DLL Directly
33121 @subsubsection Debugging the DLL Directly
33122
33123 @enumerate 1
33124 @item
33125 Find out the executable starting address
33126
33127 @smallexample
33128 $ objdump --file-header main.exe
33129 @end smallexample
33130
33131 The starting address is reported on the last line. For example:
33132
33133 @smallexample
33134 main.exe: file format pei-i386
33135 architecture: i386, flags 0x0000010a:
33136 EXEC_P, HAS_DEBUG, D_PAGED
33137 start address 0x00401010
33138 @end smallexample
33139
33140 @item
33141 Launch the debugger on the executable.
33142
33143 @smallexample
33144 $ gdb main.exe
33145 @end smallexample
33146
33147 @item
33148 Set a breakpoint at the starting address, and launch the program.
33149
33150 @smallexample
33151 $ (gdb) break *0x00401010
33152 $ (gdb) run
33153 @end smallexample
33154
33155 The program will stop at the given address.
33156
33157 @item
33158 Set a breakpoint on a DLL subroutine.
33159
33160 @smallexample
33161 (gdb) break ada_dll.adb:45
33162 @end smallexample
33163
33164 Or if you want to break using a symbol on the DLL, you need first to
33165 select the Ada language (language used by the DLL).
33166
33167 @smallexample
33168 (gdb) set language ada
33169 (gdb) break ada_dll
33170 @end smallexample
33171
33172 @item
33173 Continue the program.
33174
33175 @smallexample
33176 (gdb) cont
33177 @end smallexample
33178
33179 @noindent
33180 This will run the program until it reaches the breakpoint that has been
33181 set. From that point you can use the standard way to debug a program
33182 as described in (@pxref{Running and Debugging Ada Programs}).
33183
33184 @end enumerate
33185
33186 @noindent
33187 It is also possible to debug the DLL by attaching to a running process.
33188
33189 @node Attaching to a Running Process
33190 @subsubsection Attaching to a Running Process
33191 @cindex DLL debugging, attach to process
33192
33193 @noindent
33194 With @code{GDB} it is always possible to debug a running process by
33195 attaching to it. It is possible to debug a DLL this way. The limitation
33196 of this approach is that the DLL must run long enough to perform the
33197 attach operation. It may be useful for instance to insert a time wasting
33198 loop in the code of the DLL to meet this criterion.
33199
33200 @enumerate 1
33201
33202 @item Launch the main program @file{main.exe}.
33203
33204 @smallexample
33205 $ main
33206 @end smallexample
33207
33208 @item Use the Windows @i{Task Manager} to find the process ID. Let's say
33209 that the process PID for @file{main.exe} is 208.
33210
33211 @item Launch gdb.
33212
33213 @smallexample
33214 $ gdb
33215 @end smallexample
33216
33217 @item Attach to the running process to be debugged.
33218
33219 @smallexample
33220 (gdb) attach 208
33221 @end smallexample
33222
33223 @item Load the process debugging information.
33224
33225 @smallexample
33226 (gdb) symbol-file main.exe
33227 @end smallexample
33228
33229 @item Break somewhere in the DLL.
33230
33231 @smallexample
33232 (gdb) break ada_dll
33233 @end smallexample
33234
33235 @item Continue process execution.
33236
33237 @smallexample
33238 (gdb) cont
33239 @end smallexample
33240
33241 @end enumerate
33242
33243 @noindent
33244 This last step will resume the process execution, and stop at
33245 the breakpoint we have set. From there you can use the standard
33246 approach to debug a program as described in
33247 (@pxref{Running and Debugging Ada Programs}).
33248
33249 @node Setting Stack Size from gnatlink
33250 @section Setting Stack Size from @command{gnatlink}
33251
33252 @noindent
33253 It is possible to specify the program stack size at link time. On modern
33254 versions of Windows, starting with XP, this is mostly useful to set the size of
33255 the main stack (environment task). The other task stacks are set with pragma
33256 Storage_Size or with the @command{gnatbind -d} command.
33257
33258 Since older versions of Windows (2000, NT4, etc.) do not allow setting the
33259 reserve size of individual tasks, the link-time stack size applies to all
33260 tasks, and pragma Storage_Size has no effect.
33261 In particular, Stack Overflow checks are made against this
33262 link-time specified size.
33263
33264 This setting can be done with
33265 @command{gnatlink} using either:
33266
33267 @itemize @bullet
33268
33269 @item using @option{-Xlinker} linker option
33270
33271 @smallexample
33272 $ gnatlink hello -Xlinker --stack=0x10000,0x1000
33273 @end smallexample
33274
33275 This sets the stack reserve size to 0x10000 bytes and the stack commit
33276 size to 0x1000 bytes.
33277
33278 @item using @option{-Wl} linker option
33279
33280 @smallexample
33281 $ gnatlink hello -Wl,--stack=0x1000000
33282 @end smallexample
33283
33284 This sets the stack reserve size to 0x1000000 bytes. Note that with
33285 @option{-Wl} option it is not possible to set the stack commit size
33286 because the coma is a separator for this option.
33287
33288 @end itemize
33289
33290 @node Setting Heap Size from gnatlink
33291 @section Setting Heap Size from @command{gnatlink}
33292
33293 @noindent
33294 Under Windows systems, it is possible to specify the program heap size from
33295 @command{gnatlink} using either:
33296
33297 @itemize @bullet
33298
33299 @item using @option{-Xlinker} linker option
33300
33301 @smallexample
33302 $ gnatlink hello -Xlinker --heap=0x10000,0x1000
33303 @end smallexample
33304
33305 This sets the heap reserve size to 0x10000 bytes and the heap commit
33306 size to 0x1000 bytes.
33307
33308 @item using @option{-Wl} linker option
33309
33310 @smallexample
33311 $ gnatlink hello -Wl,--heap=0x1000000
33312 @end smallexample
33313
33314 This sets the heap reserve size to 0x1000000 bytes. Note that with
33315 @option{-Wl} option it is not possible to set the heap commit size
33316 because the coma is a separator for this option.
33317
33318 @end itemize
33319
33320 @end ifset
33321
33322 @c **********************************
33323 @c * GNU Free Documentation License *
33324 @c **********************************
33325 @include fdl.texi
33326 @c GNU Free Documentation License
33327
33328 @node Index,,GNU Free Documentation License, Top
33329 @unnumbered Index
33330
33331 @printindex cp
33332
33333 @contents
33334 @c Put table of contents at end, otherwise it precedes the "title page" in
33335 @c the .txt version
33336 @c Edit the pdf file to move the contents to the beginning, after the title
33337 @c page
33338
33339 @bye