]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/gnat_ugn.texi
gnat_ugn.texi: Update the gnatcheck subsection for metric rules acoording to the...
[thirdparty/gcc.git] / gcc / ada / gnat_ugn.texi
1 \input texinfo @c -*-texinfo-*-
2 @c %**start of header
3
4 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
5 @c o
6 @c GNAT DOCUMENTATION o
7 @c o
8 @c G N A T _ U G N o
9 @c o
10 @c GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). o
11 @c o
12 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
13
14 @setfilename gnat_ugn.info
15
16 @copying
17 Copyright @copyright{} 1995-2005, 2006, 2007, 2008 Free Software Foundation,
18 Inc.
19
20 Permission is granted to copy, distribute and/or modify this document
21 under the terms of the GNU Free Documentation License, Version 1.2 or
22 any later version published by the Free Software Foundation; with no
23 Invariant Sections, with no Front-Cover Texts and with no Back-Cover
24 Texts. A copy of the license is included in the section entitled
25 ``GNU Free Documentation License''.
26 @end copying
27
28 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
29 @c
30 @c GNAT_UGN Style Guide
31 @c
32 @c 1. Always put a @noindent on the line before the first paragraph
33 @c after any of these commands:
34 @c
35 @c @chapter
36 @c @section
37 @c @subsection
38 @c @subsubsection
39 @c @subsubsubsection
40 @c
41 @c @end smallexample
42 @c @end itemize
43 @c @end enumerate
44 @c
45 @c 2. DO NOT use @example. Use @smallexample instead.
46 @c a) DO NOT use highlighting commands (@b{}, @i{}) inside an @smallexample
47 @c context. These can interfere with the readability of the texi
48 @c source file. Instead, use one of the following annotated
49 @c @smallexample commands, and preprocess the texi file with the
50 @c ada2texi tool (which generates appropriate highlighting):
51 @c @smallexample @c ada
52 @c @smallexample @c adanocomment
53 @c @smallexample @c projectfile
54 @c b) The "@c ada" markup will result in boldface for reserved words
55 @c and italics for comments
56 @c c) The "@c adanocomment" markup will result only in boldface for
57 @c reserved words (comments are left alone)
58 @c d) The "@c projectfile" markup is like "@c ada" except that the set
59 @c of reserved words include the new reserved words for project files
60 @c
61 @c 3. Each @chapter, @section, @subsection, @subsubsection, etc.
62 @c command must be preceded by two empty lines
63 @c
64 @c 4. The @item command should be on a line of its own if it is in an
65 @c @itemize or @enumerate command.
66 @c
67 @c 5. When talking about ALI files use "ALI" (all uppercase), not "Ali"
68 @c or "ali".
69 @c
70 @c 6. DO NOT put trailing spaces at the end of a line. Such spaces will
71 @c cause the document build to fail.
72 @c
73 @c 7. DO NOT use @cartouche for examples that are longer than around 10 lines.
74 @c This command inhibits page breaks, so long examples in a @cartouche can
75 @c lead to large, ugly patches of empty space on a page.
76 @c
77 @c NOTE: This file should be submitted to xgnatugn with either the vms flag
78 @c or the unw flag set. The unw flag covers topics for both Unix and
79 @c Windows.
80 @c
81 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
82
83 @set NOW January 2007
84 @c This flag is used where the text refers to conditions that exist when the
85 @c text was entered into the document but which may change over time.
86 @c Update the setting for the flag, and (if necessary) the text surrounding,
87 @c the references to the flag, on future doc revisions:
88 @c search for @value{NOW}.
89
90 @set FSFEDITION
91 @set EDITION GNAT
92 @set DEFAULTLANGUAGEVERSION Ada 2005
93 @set NONDEFAULTLANGUAGEVERSION Ada 95
94
95 @ifset unw
96 @set PLATFORM
97 @end ifset
98
99 @ifset vms
100 @set PLATFORM OpenVMS
101 @end ifset
102
103 @c @ovar(ARG)
104 @c ----------
105 @c The ARG is an optional argument. To be used for macro arguments in
106 @c their documentation (@defmac).
107 @macro ovar{varname}
108 @r{[}@var{\varname\}@r{]}@c
109 @end macro
110
111 @settitle @value{EDITION} User's Guide @value{PLATFORM}
112 @dircategory GNU Ada tools
113 @direntry
114 * @value{EDITION} User's Guide (gnat_ugn) @value{PLATFORM}
115 @end direntry
116
117 @include gcc-common.texi
118
119 @setchapternewpage odd
120 @syncodeindex fn cp
121 @c %**end of header
122
123 @titlepage
124 @title @value{EDITION} User's Guide
125 @ifset vms
126 @sp 1
127 @flushright
128 @titlefont{@i{@value{PLATFORM}}}
129 @end flushright
130 @end ifset
131
132 @sp 2
133
134 @subtitle GNAT, The GNU Ada Compiler
135 @versionsubtitle
136 @author AdaCore
137
138 @page
139 @vskip 0pt plus 1filll
140
141 @insertcopying
142
143 @end titlepage
144
145 @ifnottex
146 @node Top, About This Guide, (dir), (dir)
147 @top @value{EDITION} User's Guide
148
149 @noindent
150 @value{EDITION} User's Guide @value{PLATFORM}
151
152 @noindent
153 GNAT, The GNU Ada Compiler@*
154 GCC version @value{version-GCC}@*
155
156 @noindent
157 AdaCore@*
158
159 @menu
160 * About This Guide::
161 * Getting Started with GNAT::
162 * The GNAT Compilation Model::
163 * Compiling Using gcc::
164 * Binding Using gnatbind::
165 * Linking Using gnatlink::
166 * The GNAT Make Program gnatmake::
167 * Improving Performance::
168 * Renaming Files Using gnatchop::
169 * Configuration Pragmas::
170 * Handling Arbitrary File Naming Conventions Using gnatname::
171 * GNAT Project Manager::
172 * The Cross-Referencing Tools gnatxref and gnatfind::
173 * The GNAT Pretty-Printer gnatpp::
174 * The GNAT Metric Tool gnatmetric::
175 * File Name Krunching Using gnatkr::
176 * Preprocessing Using gnatprep::
177 @ifset vms
178 * The GNAT Run-Time Library Builder gnatlbr::
179 @end ifset
180 * The GNAT Library Browser gnatls::
181 * Cleaning Up Using gnatclean::
182 @ifclear vms
183 * GNAT and Libraries::
184 * Using the GNU make Utility::
185 @end ifclear
186 * Memory Management Issues::
187 * Stack Related Facilities::
188 * Verifying Properties Using gnatcheck::
189 * Creating Sample Bodies Using gnatstub::
190 * Other Utility Programs::
191 * Running and Debugging Ada Programs::
192 @ifclear vms
193 * Code Coverage and Profiling::
194 @end ifclear
195 @ifset vms
196 * Compatibility with HP Ada::
197 @end ifset
198 * Platform-Specific Information for the Run-Time Libraries::
199 * Example of Binder Output File::
200 * Elaboration Order Handling in GNAT::
201 * Conditional Compilation::
202 * Inline Assembler::
203 * Compatibility and Porting Guide::
204 @ifset unw
205 * Microsoft Windows Topics::
206 @end ifset
207 * GNU Free Documentation License::
208 * Index::
209
210 --- The Detailed Node Listing ---
211
212 About This Guide
213
214 * What This Guide Contains::
215 * What You Should Know before Reading This Guide::
216 * Related Information::
217 * Conventions::
218
219 Getting Started with GNAT
220
221 * Running GNAT::
222 * Running a Simple Ada Program::
223 * Running a Program with Multiple Units::
224 * Using the gnatmake Utility::
225 @ifset vms
226 * Editing with Emacs::
227 @end ifset
228 @ifclear vms
229 * Introduction to GPS::
230 @end ifclear
231
232 The GNAT Compilation Model
233
234 * Source Representation::
235 * Foreign Language Representation::
236 * File Naming Rules::
237 * Using Other File Names::
238 * Alternative File Naming Schemes::
239 * Generating Object Files::
240 * Source Dependencies::
241 * The Ada Library Information Files::
242 * Binding an Ada Program::
243 * Mixed Language Programming::
244 @ifclear vms
245 * Building Mixed Ada & C++ Programs::
246 * Comparison between GNAT and C/C++ Compilation Models::
247 @end ifclear
248 * Comparison between GNAT and Conventional Ada Library Models::
249 @ifset vms
250 * Placement of temporary files::
251 @end ifset
252
253 Foreign Language Representation
254
255 * Latin-1::
256 * Other 8-Bit Codes::
257 * Wide Character Encodings::
258
259 Compiling Ada Programs With gcc
260
261 * Compiling Programs::
262 * Switches for gcc::
263 * Search Paths and the Run-Time Library (RTL)::
264 * Order of Compilation Issues::
265 * Examples::
266
267 Switches for gcc
268
269 * Output and Error Message Control::
270 * Warning Message Control::
271 * Debugging and Assertion Control::
272 * Validity Checking::
273 * Style Checking::
274 * Run-Time Checks::
275 * Using gcc for Syntax Checking::
276 * Using gcc for Semantic Checking::
277 * Compiling Different Versions of Ada::
278 * Character Set Control::
279 * File Naming Control::
280 * Subprogram Inlining Control::
281 * Auxiliary Output Control::
282 * Debugging Control::
283 * Exception Handling Control::
284 * Units to Sources Mapping Files::
285 * Integrated Preprocessing::
286 @ifset vms
287 * Return Codes::
288 @end ifset
289
290 Binding Ada Programs With gnatbind
291
292 * Running gnatbind::
293 * Switches for gnatbind::
294 * Command-Line Access::
295 * Search Paths for gnatbind::
296 * Examples of gnatbind Usage::
297
298 Switches for gnatbind
299
300 * Consistency-Checking Modes::
301 * Binder Error Message Control::
302 * Elaboration Control::
303 * Output Control::
304 * Binding with Non-Ada Main Programs::
305 * Binding Programs with No Main Subprogram::
306
307 Linking Using gnatlink
308
309 * Running gnatlink::
310 * Switches for gnatlink::
311
312 The GNAT Make Program gnatmake
313
314 * Running gnatmake::
315 * Switches for gnatmake::
316 * Mode Switches for gnatmake::
317 * Notes on the Command Line::
318 * How gnatmake Works::
319 * Examples of gnatmake Usage::
320
321 Improving Performance
322 * Performance Considerations::
323 * Text_IO Suggestions::
324 * Reducing Size of Ada Executables with gnatelim::
325 * Reducing Size of Executables with unused subprogram/data elimination::
326
327 Performance Considerations
328 * Controlling Run-Time Checks::
329 * Use of Restrictions::
330 * Optimization Levels::
331 * Debugging Optimized Code::
332 * Inlining of Subprograms::
333 * Other Optimization Switches::
334 * Optimization and Strict Aliasing::
335 @ifset vms
336 * Coverage Analysis::
337 @end ifset
338
339 Reducing Size of Ada Executables with gnatelim
340 * About gnatelim::
341 * Running gnatelim::
342 * Correcting the List of Eliminate Pragmas::
343 * Making Your Executables Smaller::
344 * Summary of the gnatelim Usage Cycle::
345
346 Reducing Size of Executables with unused subprogram/data elimination
347 * About unused subprogram/data elimination::
348 * Compilation options::
349
350 Renaming Files Using gnatchop
351
352 * Handling Files with Multiple Units::
353 * Operating gnatchop in Compilation Mode::
354 * Command Line for gnatchop::
355 * Switches for gnatchop::
356 * Examples of gnatchop Usage::
357
358 Configuration Pragmas
359
360 * Handling of Configuration Pragmas::
361 * The Configuration Pragmas Files::
362
363 Handling Arbitrary File Naming Conventions Using gnatname
364
365 * Arbitrary File Naming Conventions::
366 * Running gnatname::
367 * Switches for gnatname::
368 * Examples of gnatname Usage::
369
370 GNAT Project Manager
371
372 * Introduction::
373 * Examples of Project Files::
374 * Project File Syntax::
375 * Objects and Sources in Project Files::
376 * Importing Projects::
377 * Project Extension::
378 * Project Hierarchy Extension::
379 * External References in Project Files::
380 * Packages in Project Files::
381 * Variables from Imported Projects::
382 * Naming Schemes::
383 * Library Projects::
384 * Stand-alone Library Projects::
385 * Switches Related to Project Files::
386 * Tools Supporting Project Files::
387 * An Extended Example::
388 * Project File Complete Syntax::
389
390 The Cross-Referencing Tools gnatxref and gnatfind
391
392 * gnatxref Switches::
393 * gnatfind Switches::
394 * Project Files for gnatxref and gnatfind::
395 * Regular Expressions in gnatfind and gnatxref::
396 * Examples of gnatxref Usage::
397 * Examples of gnatfind Usage::
398
399 The GNAT Pretty-Printer gnatpp
400
401 * Switches for gnatpp::
402 * Formatting Rules::
403
404 The GNAT Metrics Tool gnatmetric
405
406 * Switches for gnatmetric::
407
408 File Name Krunching Using gnatkr
409
410 * About gnatkr::
411 * Using gnatkr::
412 * Krunching Method::
413 * Examples of gnatkr Usage::
414
415 Preprocessing Using gnatprep
416 * Preprocessing Symbols::
417 * Using gnatprep::
418 * Switches for gnatprep::
419 * Form of Definitions File::
420 * Form of Input Text for gnatprep::
421
422 @ifset vms
423 The GNAT Run-Time Library Builder gnatlbr
424
425 * Running gnatlbr::
426 * Switches for gnatlbr::
427 * Examples of gnatlbr Usage::
428 @end ifset
429
430 The GNAT Library Browser gnatls
431
432 * Running gnatls::
433 * Switches for gnatls::
434 * Examples of gnatls Usage::
435
436 Cleaning Up Using gnatclean
437
438 * Running gnatclean::
439 * Switches for gnatclean::
440 @c * Examples of gnatclean Usage::
441
442 @ifclear vms
443
444 GNAT and Libraries
445
446 * Introduction to Libraries in GNAT::
447 * General Ada Libraries::
448 * Stand-alone Ada Libraries::
449 * Rebuilding the GNAT Run-Time Library::
450
451 Using the GNU make Utility
452
453 * Using gnatmake in a Makefile::
454 * Automatically Creating a List of Directories::
455 * Generating the Command Line Switches::
456 * Overcoming Command Line Length Limits::
457 @end ifclear
458
459 Memory Management Issues
460
461 * Some Useful Memory Pools::
462 * The GNAT Debug Pool Facility::
463 @ifclear vms
464 * The gnatmem Tool::
465 @end ifclear
466
467 Stack Related Facilities
468
469 * Stack Overflow Checking::
470 * Static Stack Usage Analysis::
471 * Dynamic Stack Usage Analysis::
472
473 Some Useful Memory Pools
474
475 The GNAT Debug Pool Facility
476
477 @ifclear vms
478 The gnatmem Tool
479
480 * Running gnatmem::
481 * Switches for gnatmem::
482 * Example of gnatmem Usage::
483 @end ifclear
484
485 Verifying Properties Using gnatcheck
486
487 * Format of the Report File::
488 * General gnatcheck Switches::
489 * gnatcheck Rule Options::
490 * Adding the Results of Compiler Checks to gnatcheck Output::
491 * Project-Wide Checks::
492 * Predefined Rules::
493
494 Sample Bodies Using gnatstub
495
496 * Running gnatstub::
497 * Switches for gnatstub::
498
499 Other Utility Programs
500
501 * Using Other Utility Programs with GNAT::
502 * The External Symbol Naming Scheme of GNAT::
503 * Converting Ada Files to html with gnathtml::
504
505 @ifclear vms
506 Code Coverage and Profiling
507
508 * Code Coverage of Ada Programs using gcov::
509 * Profiling an Ada Program using gprof::
510 @end ifclear
511
512 Running and Debugging Ada Programs
513
514 * The GNAT Debugger GDB::
515 * Running GDB::
516 * Introduction to GDB Commands::
517 * Using Ada Expressions::
518 * Calling User-Defined Subprograms::
519 * Using the Next Command in a Function::
520 * Ada Exceptions::
521 * Ada Tasks::
522 * Debugging Generic Units::
523 * GNAT Abnormal Termination or Failure to Terminate::
524 * Naming Conventions for GNAT Source Files::
525 * Getting Internal Debugging Information::
526 * Stack Traceback::
527
528 @ifset vms
529 * LSE::
530 @end ifset
531
532 @ifset vms
533 Compatibility with HP Ada
534
535 * Ada Language Compatibility::
536 * Differences in the Definition of Package System::
537 * Language-Related Features::
538 * The Package STANDARD::
539 * The Package SYSTEM::
540 * Tasking and Task-Related Features::
541 * Pragmas and Pragma-Related Features::
542 * Library of Predefined Units::
543 * Bindings::
544 * Main Program Definition::
545 * Implementation-Defined Attributes::
546 * Compiler and Run-Time Interfacing::
547 * Program Compilation and Library Management::
548 * Input-Output::
549 * Implementation Limits::
550 * Tools and Utilities::
551
552 Language-Related Features
553
554 * Integer Types and Representations::
555 * Floating-Point Types and Representations::
556 * Pragmas Float_Representation and Long_Float::
557 * Fixed-Point Types and Representations::
558 * Record and Array Component Alignment::
559 * Address Clauses::
560 * Other Representation Clauses::
561
562 Tasking and Task-Related Features
563
564 * Implementation of Tasks in HP Ada for OpenVMS Alpha Systems::
565 * Assigning Task IDs::
566 * Task IDs and Delays::
567 * Task-Related Pragmas::
568 * Scheduling and Task Priority::
569 * The Task Stack::
570 * External Interrupts::
571
572 Pragmas and Pragma-Related Features
573
574 * Restrictions on the Pragma INLINE::
575 * Restrictions on the Pragma INTERFACE::
576 * Restrictions on the Pragma SYSTEM_NAME::
577
578 Library of Predefined Units
579
580 * Changes to DECLIB::
581
582 Bindings
583
584 * Shared Libraries and Options Files::
585 * Interfaces to C::
586 @end ifset
587
588 Platform-Specific Information for the Run-Time Libraries
589
590 * Summary of Run-Time Configurations::
591 * Specifying a Run-Time Library::
592 * Choosing the Scheduling Policy::
593 * Solaris-Specific Considerations::
594 * Linux-Specific Considerations::
595 * AIX-Specific Considerations::
596 * Irix-Specific Considerations::
597
598 Example of Binder Output File
599
600 Elaboration Order Handling in GNAT
601
602 * Elaboration Code::
603 * Checking the Elaboration Order::
604 * Controlling the Elaboration Order::
605 * Controlling Elaboration in GNAT - Internal Calls::
606 * Controlling Elaboration in GNAT - External Calls::
607 * Default Behavior in GNAT - Ensuring Safety::
608 * Treatment of Pragma Elaborate::
609 * Elaboration Issues for Library Tasks::
610 * Mixing Elaboration Models::
611 * What to Do If the Default Elaboration Behavior Fails::
612 * Elaboration for Access-to-Subprogram Values::
613 * Summary of Procedures for Elaboration Control::
614 * Other Elaboration Order Considerations::
615
616 Conditional Compilation
617 * Use of Boolean Constants::
618 * Debugging - A Special Case::
619 * Conditionalizing Declarations::
620 * Use of Alternative Implementations::
621 * Preprocessing::
622
623 Inline Assembler
624
625 * Basic Assembler Syntax::
626 * A Simple Example of Inline Assembler::
627 * Output Variables in Inline Assembler::
628 * Input Variables in Inline Assembler::
629 * Inlining Inline Assembler Code::
630 * Other Asm Functionality::
631
632 Compatibility and Porting Guide
633
634 * Compatibility with Ada 83::
635 * Compatibility between Ada 95 and Ada 2005::
636 * Implementation-dependent characteristics::
637 @ifclear vms
638 @c This brief section is only in the non-VMS version
639 @c The complete chapter on HP Ada issues is in the VMS version
640 * Compatibility with HP Ada 83::
641 @end ifclear
642 * Compatibility with Other Ada Systems::
643 * Representation Clauses::
644 @ifset vms
645 * Transitioning to 64-Bit GNAT for OpenVMS::
646 @end ifset
647
648 @ifset unw
649 Microsoft Windows Topics
650
651 * Using GNAT on Windows::
652 * CONSOLE and WINDOWS subsystems::
653 * Temporary Files::
654 * Mixed-Language Programming on Windows::
655 * Windows Calling Conventions::
656 * Introduction to Dynamic Link Libraries (DLLs)::
657 * Using DLLs with GNAT::
658 * Building DLLs with GNAT::
659 * GNAT and Windows Resources::
660 * Debugging a DLL::
661 * Setting Stack Size from gnatlink::
662 * Setting Heap Size from gnatlink::
663 @end ifset
664
665 * Index::
666 @end menu
667 @end ifnottex
668
669 @node About This Guide
670 @unnumbered About This Guide
671
672 @noindent
673 @ifset vms
674 This guide describes the use of @value{EDITION},
675 a compiler and software development toolset for the full Ada
676 programming language, implemented on OpenVMS for HP's Alpha and
677 Integrity server (I64) platforms.
678 @end ifset
679 @ifclear vms
680 This guide describes the use of @value{EDITION},
681 a compiler and software development
682 toolset for the full Ada programming language.
683 @end ifclear
684 It documents the features of the compiler and tools, and explains
685 how to use them to build Ada applications.
686
687 @value{EDITION} implements Ada 95 and Ada 2005, and it may also be invoked in
688 Ada 83 compatibility mode.
689 By default, @value{EDITION} assumes @value{DEFAULTLANGUAGEVERSION},
690 but you can override with a compiler switch
691 (@pxref{Compiling Different Versions of Ada})
692 to explicitly specify the language version.
693 Throughout this manual, references to ``Ada'' without a year suffix
694 apply to both the Ada 95 and Ada 2005 versions of the language.
695
696
697 @ifclear FSFEDITION
698 For ease of exposition, ``@value{EDITION}'' will be referred to simply as
699 ``GNAT'' in the remainder of this document.
700 @end ifclear
701
702
703
704
705 @menu
706 * What This Guide Contains::
707 * What You Should Know before Reading This Guide::
708 * Related Information::
709 * Conventions::
710 @end menu
711
712 @node What This Guide Contains
713 @unnumberedsec What This Guide Contains
714
715 @noindent
716 This guide contains the following chapters:
717 @itemize @bullet
718
719 @item
720 @ref{Getting Started with GNAT}, describes how to get started compiling
721 and running Ada programs with the GNAT Ada programming environment.
722 @item
723 @ref{The GNAT Compilation Model}, describes the compilation model used
724 by GNAT.
725
726 @item
727 @ref{Compiling Using gcc}, describes how to compile
728 Ada programs with @command{gcc}, the Ada compiler.
729
730 @item
731 @ref{Binding Using gnatbind}, describes how to
732 perform binding of Ada programs with @code{gnatbind}, the GNAT binding
733 utility.
734
735 @item
736 @ref{Linking Using gnatlink},
737 describes @command{gnatlink}, a
738 program that provides for linking using the GNAT run-time library to
739 construct a program. @command{gnatlink} can also incorporate foreign language
740 object units into the executable.
741
742 @item
743 @ref{The GNAT Make Program gnatmake}, describes @command{gnatmake}, a
744 utility that automatically determines the set of sources
745 needed by an Ada compilation unit, and executes the necessary compilations
746 binding and link.
747
748 @item
749 @ref{Improving Performance}, shows various techniques for making your
750 Ada program run faster or take less space.
751 It discusses the effect of the compiler's optimization switch and
752 also describes the @command{gnatelim} tool and unused subprogram/data
753 elimination.
754
755 @item
756 @ref{Renaming Files Using gnatchop}, describes
757 @code{gnatchop}, a utility that allows you to preprocess a file that
758 contains Ada source code, and split it into one or more new files, one
759 for each compilation unit.
760
761 @item
762 @ref{Configuration Pragmas}, describes the configuration pragmas
763 handled by GNAT.
764
765 @item
766 @ref{Handling Arbitrary File Naming Conventions Using gnatname},
767 shows how to override the default GNAT file naming conventions,
768 either for an individual unit or globally.
769
770 @item
771 @ref{GNAT Project Manager}, describes how to use project files
772 to organize large projects.
773
774 @item
775 @ref{The Cross-Referencing Tools gnatxref and gnatfind}, discusses
776 @code{gnatxref} and @code{gnatfind}, two tools that provide an easy
777 way to navigate through sources.
778
779 @item
780 @ref{The GNAT Pretty-Printer gnatpp}, shows how to produce a reformatted
781 version of an Ada source file with control over casing, indentation,
782 comment placement, and other elements of program presentation style.
783
784 @item
785 @ref{The GNAT Metric Tool gnatmetric}, shows how to compute various
786 metrics for an Ada source file, such as the number of types and subprograms,
787 and assorted complexity measures.
788
789 @item
790 @ref{File Name Krunching Using gnatkr}, describes the @code{gnatkr}
791 file name krunching utility, used to handle shortened
792 file names on operating systems with a limit on the length of names.
793
794 @item
795 @ref{Preprocessing Using gnatprep}, describes @code{gnatprep}, a
796 preprocessor utility that allows a single source file to be used to
797 generate multiple or parameterized source files by means of macro
798 substitution.
799
800 @ifset vms
801 @item
802 @ref{The GNAT Run-Time Library Builder gnatlbr}, describes @command{gnatlbr},
803 a tool for rebuilding the GNAT run time with user-supplied
804 configuration pragmas.
805 @end ifset
806
807 @item
808 @ref{The GNAT Library Browser gnatls}, describes @code{gnatls}, a
809 utility that displays information about compiled units, including dependences
810 on the corresponding sources files, and consistency of compilations.
811
812 @item
813 @ref{Cleaning Up Using gnatclean}, describes @code{gnatclean}, a utility
814 to delete files that are produced by the compiler, binder and linker.
815
816 @ifclear vms
817 @item
818 @ref{GNAT and Libraries}, describes the process of creating and using
819 Libraries with GNAT. It also describes how to recompile the GNAT run-time
820 library.
821
822 @item
823 @ref{Using the GNU make Utility}, describes some techniques for using
824 the GNAT toolset in Makefiles.
825 @end ifclear
826
827 @item
828 @ref{Memory Management Issues}, describes some useful predefined storage pools
829 and in particular the GNAT Debug Pool facility, which helps detect incorrect
830 memory references.
831 @ifclear vms
832 It also describes @command{gnatmem}, a utility that monitors dynamic
833 allocation and deallocation and helps detect ``memory leaks''.
834 @end ifclear
835
836 @item
837 @ref{Stack Related Facilities}, describes some useful tools associated with
838 stack checking and analysis.
839
840 @item
841 @ref{Verifying Properties Using gnatcheck}, discusses @code{gnatcheck},
842 a utility that checks Ada code against a set of rules.
843
844 @item
845 @ref{Creating Sample Bodies Using gnatstub}, discusses @code{gnatstub},
846 a utility that generates empty but compilable bodies for library units.
847
848 @item
849 @ref{Other Utility Programs}, discusses several other GNAT utilities,
850 including @code{gnathtml}.
851
852 @ifclear vms
853 @item
854 @ref{Code Coverage and Profiling}, describes how to perform a structural
855 coverage and profile the execution of Ada programs.
856 @end ifclear
857
858 @item
859 @ref{Running and Debugging Ada Programs}, describes how to run and debug
860 Ada programs.
861
862 @ifset vms
863 @item
864 @ref{Compatibility with HP Ada}, details the compatibility of GNAT with
865 HP Ada 83 @footnote{``HP Ada'' refers to the legacy product originally
866 developed by Digital Equipment Corporation and currently supported by HP.}
867 for OpenVMS Alpha. This product was formerly known as DEC Ada,
868 @cindex DEC Ada
869 and for
870 historical compatibility reasons, the relevant libraries still use the
871 DEC prefix.
872 @end ifset
873
874 @item
875 @ref{Platform-Specific Information for the Run-Time Libraries},
876 describes the various run-time
877 libraries supported by GNAT on various platforms and explains how to
878 choose a particular library.
879
880 @item
881 @ref{Example of Binder Output File}, shows the source code for the binder
882 output file for a sample program.
883
884 @item
885 @ref{Elaboration Order Handling in GNAT}, describes how GNAT helps
886 you deal with elaboration order issues.
887
888 @item
889 @ref{Conditional Compilation}, describes how to model conditional compilation,
890 both with Ada in general and with GNAT facilities in particular.
891
892 @item
893 @ref{Inline Assembler}, shows how to use the inline assembly facility
894 in an Ada program.
895
896 @item
897 @ref{Compatibility and Porting Guide}, contains sections on compatibility
898 of GNAT with other Ada development environments (including Ada 83 systems),
899 to assist in porting code from those environments.
900
901 @ifset unw
902 @item
903 @ref{Microsoft Windows Topics}, presents information relevant to the
904 Microsoft Windows platform.
905 @end ifset
906 @end itemize
907
908 @c *************************************************
909 @node What You Should Know before Reading This Guide
910 @c *************************************************
911 @unnumberedsec What You Should Know before Reading This Guide
912
913 @cindex Ada 95 Language Reference Manual
914 @cindex Ada 2005 Language Reference Manual
915 @noindent
916 This guide assumes a basic familiarity with the Ada 95 language, as
917 described in the International Standard ANSI/ISO/IEC-8652:1995, January
918 1995.
919 It does not require knowledge of the new features introduced by Ada 2005,
920 (officially known as ISO/IEC 8652:1995 with Technical Corrigendum 1
921 and Amendment 1).
922 Both reference manuals are included in the GNAT documentation
923 package.
924
925 @node Related Information
926 @unnumberedsec Related Information
927
928 @noindent
929 For further information about related tools, refer to the following
930 documents:
931
932 @itemize @bullet
933 @item
934 @xref{Top, GNAT Reference Manual, About This Guide, gnat_rm, GNAT
935 Reference Manual}, which contains all reference material for the GNAT
936 implementation of Ada.
937
938 @ifset unw
939 @item
940 @cite{Using the GNAT Programming Studio}, which describes the GPS
941 Integrated Development Environment.
942
943 @item
944 @cite{GNAT Programming Studio Tutorial}, which introduces the
945 main GPS features through examples.
946 @end ifset
947
948 @item
949 @cite{Ada 95 Reference Manual}, which contains reference
950 material for the Ada 95 programming language.
951
952 @item
953 @cite{Ada 2005 Reference Manual}, which contains reference
954 material for the Ada 2005 programming language.
955
956 @item
957 @xref{Top,, Debugging with GDB, gdb, Debugging with GDB},
958 @ifset vms
959 in the GNU:[DOCS] directory,
960 @end ifset
961 for all details on the use of the GNU source-level debugger.
962
963 @item
964 @xref{Top,, The extensible self-documenting text editor, emacs,
965 GNU Emacs Manual},
966 @ifset vms
967 located in the GNU:[DOCS] directory if the EMACS kit is installed,
968 @end ifset
969 for full information on the extensible editor and programming
970 environment Emacs.
971
972 @end itemize
973
974 @c **************
975 @node Conventions
976 @unnumberedsec Conventions
977 @cindex Conventions
978 @cindex Typographical conventions
979
980 @noindent
981 Following are examples of the typographical and graphic conventions used
982 in this guide:
983
984 @itemize @bullet
985 @item
986 @code{Functions}, @command{utility program names}, @code{standard names},
987 and @code{classes}.
988
989 @item
990 @option{Option flags}
991
992 @item
993 @file{File names}, @samp{button names}, and @samp{field names}.
994
995 @item
996 @code{Variables}, @env{environment variables}, and @var{metasyntactic
997 variables}.
998
999 @item
1000 @emph{Emphasis}.
1001
1002 @item
1003 @r{[}optional information or parameters@r{]}
1004
1005 @item
1006 Examples are described by text
1007 @smallexample
1008 and then shown this way.
1009 @end smallexample
1010 @end itemize
1011
1012 @noindent
1013 Commands that are entered by the user are preceded in this manual by the
1014 characters @w{``@code{$ }''} (dollar sign followed by space). If your system
1015 uses this sequence as a prompt, then the commands will appear exactly as
1016 you see them in the manual. If your system uses some other prompt, then
1017 the command will appear with the @code{$} replaced by whatever prompt
1018 character you are using.
1019
1020 @ifset unw
1021 Full file names are shown with the ``@code{/}'' character
1022 as the directory separator; e.g., @file{parent-dir/subdir/myfile.adb}.
1023 If you are using GNAT on a Windows platform, please note that
1024 the ``@code{\}'' character should be used instead.
1025 @end ifset
1026
1027 @c ****************************
1028 @node Getting Started with GNAT
1029 @chapter Getting Started with GNAT
1030
1031 @noindent
1032 This chapter describes some simple ways of using GNAT to build
1033 executable Ada programs.
1034 @ifset unw
1035 @ref{Running GNAT}, through @ref{Using the gnatmake Utility},
1036 show how to use the command line environment.
1037 @ref{Introduction to GPS}, provides a brief
1038 introduction to the GNAT Programming Studio, a visually-oriented
1039 Integrated Development Environment for GNAT.
1040 GPS offers a graphical ``look and feel'', support for development in
1041 other programming languages, comprehensive browsing features, and
1042 many other capabilities.
1043 For information on GPS please refer to
1044 @cite{Using the GNAT Programming Studio}.
1045 @end ifset
1046
1047 @menu
1048 * Running GNAT::
1049 * Running a Simple Ada Program::
1050 * Running a Program with Multiple Units::
1051 * Using the gnatmake Utility::
1052 @ifset vms
1053 * Editing with Emacs::
1054 @end ifset
1055 @ifclear vms
1056 * Introduction to GPS::
1057 @end ifclear
1058 @end menu
1059
1060 @node Running GNAT
1061 @section Running GNAT
1062
1063 @noindent
1064 Three steps are needed to create an executable file from an Ada source
1065 file:
1066
1067 @enumerate
1068 @item
1069 The source file(s) must be compiled.
1070 @item
1071 The file(s) must be bound using the GNAT binder.
1072 @item
1073 All appropriate object files must be linked to produce an executable.
1074 @end enumerate
1075
1076 @noindent
1077 All three steps are most commonly handled by using the @command{gnatmake}
1078 utility program that, given the name of the main program, automatically
1079 performs the necessary compilation, binding and linking steps.
1080
1081 @node Running a Simple Ada Program
1082 @section Running a Simple Ada Program
1083
1084 @noindent
1085 Any text editor may be used to prepare an Ada program.
1086 (If @code{Emacs} is
1087 used, the optional Ada mode may be helpful in laying out the program.)
1088 The
1089 program text is a normal text file. We will assume in our initial
1090 example that you have used your editor to prepare the following
1091 standard format text file:
1092
1093 @smallexample @c ada
1094 @cartouche
1095 with Ada.Text_IO; use Ada.Text_IO;
1096 procedure Hello is
1097 begin
1098 Put_Line ("Hello WORLD!");
1099 end Hello;
1100 @end cartouche
1101 @end smallexample
1102
1103 @noindent
1104 This file should be named @file{hello.adb}.
1105 With the normal default file naming conventions, GNAT requires
1106 that each file
1107 contain a single compilation unit whose file name is the
1108 unit name,
1109 with periods replaced by hyphens; the
1110 extension is @file{ads} for a
1111 spec and @file{adb} for a body.
1112 You can override this default file naming convention by use of the
1113 special pragma @code{Source_File_Name} (@pxref{Using Other File Names}).
1114 Alternatively, if you want to rename your files according to this default
1115 convention, which is probably more convenient if you will be using GNAT
1116 for all your compilations, then the @code{gnatchop} utility
1117 can be used to generate correctly-named source files
1118 (@pxref{Renaming Files Using gnatchop}).
1119
1120 You can compile the program using the following command (@code{$} is used
1121 as the command prompt in the examples in this document):
1122
1123 @smallexample
1124 $ gcc -c hello.adb
1125 @end smallexample
1126
1127 @noindent
1128 @command{gcc} is the command used to run the compiler. This compiler is
1129 capable of compiling programs in several languages, including Ada and
1130 C. It assumes that you have given it an Ada program if the file extension is
1131 either @file{.ads} or @file{.adb}, and it will then call
1132 the GNAT compiler to compile the specified file.
1133
1134 @ifclear vms
1135 The @option{-c} switch is required. It tells @command{gcc} to only do a
1136 compilation. (For C programs, @command{gcc} can also do linking, but this
1137 capability is not used directly for Ada programs, so the @option{-c}
1138 switch must always be present.)
1139 @end ifclear
1140
1141 This compile command generates a file
1142 @file{hello.o}, which is the object
1143 file corresponding to your Ada program. It also generates
1144 an ``Ada Library Information'' file @file{hello.ali},
1145 which contains additional information used to check
1146 that an Ada program is consistent.
1147 To build an executable file,
1148 use @code{gnatbind} to bind the program
1149 and @command{gnatlink} to link it. The
1150 argument to both @code{gnatbind} and @command{gnatlink} is the name of the
1151 @file{ALI} file, but the default extension of @file{.ali} can
1152 be omitted. This means that in the most common case, the argument
1153 is simply the name of the main program:
1154
1155 @smallexample
1156 $ gnatbind hello
1157 $ gnatlink hello
1158 @end smallexample
1159
1160 @noindent
1161 A simpler method of carrying out these steps is to use
1162 @command{gnatmake},
1163 a master program that invokes all the required
1164 compilation, binding and linking tools in the correct order. In particular,
1165 @command{gnatmake} automatically recompiles any sources that have been
1166 modified since they were last compiled, or sources that depend
1167 on such modified sources, so that ``version skew'' is avoided.
1168 @cindex Version skew (avoided by @command{gnatmake})
1169
1170 @smallexample
1171 $ gnatmake hello.adb
1172 @end smallexample
1173
1174 @noindent
1175 The result is an executable program called @file{hello}, which can be
1176 run by entering:
1177
1178 @smallexample
1179 $ ^hello^RUN HELLO^
1180 @end smallexample
1181
1182 @noindent
1183 assuming that the current directory is on the search path
1184 for executable programs.
1185
1186 @noindent
1187 and, if all has gone well, you will see
1188
1189 @smallexample
1190 Hello WORLD!
1191 @end smallexample
1192
1193 @noindent
1194 appear in response to this command.
1195
1196 @c ****************************************
1197 @node Running a Program with Multiple Units
1198 @section Running a Program with Multiple Units
1199
1200 @noindent
1201 Consider a slightly more complicated example that has three files: a
1202 main program, and the spec and body of a package:
1203
1204 @smallexample @c ada
1205 @cartouche
1206 @group
1207 package Greetings is
1208 procedure Hello;
1209 procedure Goodbye;
1210 end Greetings;
1211
1212 with Ada.Text_IO; use Ada.Text_IO;
1213 package body Greetings is
1214 procedure Hello is
1215 begin
1216 Put_Line ("Hello WORLD!");
1217 end Hello;
1218
1219 procedure Goodbye is
1220 begin
1221 Put_Line ("Goodbye WORLD!");
1222 end Goodbye;
1223 end Greetings;
1224 @end group
1225
1226 @group
1227 with Greetings;
1228 procedure Gmain is
1229 begin
1230 Greetings.Hello;
1231 Greetings.Goodbye;
1232 end Gmain;
1233 @end group
1234 @end cartouche
1235 @end smallexample
1236
1237 @noindent
1238 Following the one-unit-per-file rule, place this program in the
1239 following three separate files:
1240
1241 @table @file
1242 @item greetings.ads
1243 spec of package @code{Greetings}
1244
1245 @item greetings.adb
1246 body of package @code{Greetings}
1247
1248 @item gmain.adb
1249 body of main program
1250 @end table
1251
1252 @noindent
1253 To build an executable version of
1254 this program, we could use four separate steps to compile, bind, and link
1255 the program, as follows:
1256
1257 @smallexample
1258 $ gcc -c gmain.adb
1259 $ gcc -c greetings.adb
1260 $ gnatbind gmain
1261 $ gnatlink gmain
1262 @end smallexample
1263
1264 @noindent
1265 Note that there is no required order of compilation when using GNAT.
1266 In particular it is perfectly fine to compile the main program first.
1267 Also, it is not necessary to compile package specs in the case where
1268 there is an accompanying body; you only need to compile the body. If you want
1269 to submit these files to the compiler for semantic checking and not code
1270 generation, then use the
1271 @option{-gnatc} switch:
1272
1273 @smallexample
1274 $ gcc -c greetings.ads -gnatc
1275 @end smallexample
1276
1277 @noindent
1278 Although the compilation can be done in separate steps as in the
1279 above example, in practice it is almost always more convenient
1280 to use the @command{gnatmake} tool. All you need to know in this case
1281 is the name of the main program's source file. The effect of the above four
1282 commands can be achieved with a single one:
1283
1284 @smallexample
1285 $ gnatmake gmain.adb
1286 @end smallexample
1287
1288 @noindent
1289 In the next section we discuss the advantages of using @command{gnatmake} in
1290 more detail.
1291
1292 @c *****************************
1293 @node Using the gnatmake Utility
1294 @section Using the @command{gnatmake} Utility
1295
1296 @noindent
1297 If you work on a program by compiling single components at a time using
1298 @command{gcc}, you typically keep track of the units you modify. In order to
1299 build a consistent system, you compile not only these units, but also any
1300 units that depend on the units you have modified.
1301 For example, in the preceding case,
1302 if you edit @file{gmain.adb}, you only need to recompile that file. But if
1303 you edit @file{greetings.ads}, you must recompile both
1304 @file{greetings.adb} and @file{gmain.adb}, because both files contain
1305 units that depend on @file{greetings.ads}.
1306
1307 @code{gnatbind} will warn you if you forget one of these compilation
1308 steps, so that it is impossible to generate an inconsistent program as a
1309 result of forgetting to do a compilation. Nevertheless it is tedious and
1310 error-prone to keep track of dependencies among units.
1311 One approach to handle the dependency-bookkeeping is to use a
1312 makefile. However, makefiles present maintenance problems of their own:
1313 if the dependencies change as you change the program, you must make
1314 sure that the makefile is kept up-to-date manually, which is also an
1315 error-prone process.
1316
1317 The @command{gnatmake} utility takes care of these details automatically.
1318 Invoke it using either one of the following forms:
1319
1320 @smallexample
1321 $ gnatmake gmain.adb
1322 $ gnatmake ^gmain^GMAIN^
1323 @end smallexample
1324
1325 @noindent
1326 The argument is the name of the file containing the main program;
1327 you may omit the extension. @command{gnatmake}
1328 examines the environment, automatically recompiles any files that need
1329 recompiling, and binds and links the resulting set of object files,
1330 generating the executable file, @file{^gmain^GMAIN.EXE^}.
1331 In a large program, it
1332 can be extremely helpful to use @command{gnatmake}, because working out by hand
1333 what needs to be recompiled can be difficult.
1334
1335 Note that @command{gnatmake}
1336 takes into account all the Ada rules that
1337 establish dependencies among units. These include dependencies that result
1338 from inlining subprogram bodies, and from
1339 generic instantiation. Unlike some other
1340 Ada make tools, @command{gnatmake} does not rely on the dependencies that were
1341 found by the compiler on a previous compilation, which may possibly
1342 be wrong when sources change. @command{gnatmake} determines the exact set of
1343 dependencies from scratch each time it is run.
1344
1345 @ifset vms
1346 @node Editing with Emacs
1347 @section Editing with Emacs
1348 @cindex Emacs
1349
1350 @noindent
1351 Emacs is an extensible self-documenting text editor that is available in a
1352 separate VMSINSTAL kit.
1353
1354 Invoke Emacs by typing @kbd{Emacs} at the command prompt. To get started,
1355 click on the Emacs Help menu and run the Emacs Tutorial.
1356 In a character cell terminal, Emacs help is invoked with @kbd{Ctrl-h} (also
1357 written as @kbd{C-h}), and the tutorial by @kbd{C-h t}.
1358
1359 Documentation on Emacs and other tools is available in Emacs under the
1360 pull-down menu button: @code{Help - Info}. After selecting @code{Info},
1361 use the middle mouse button to select a topic (e.g.@: Emacs).
1362
1363 In a character cell terminal, do @kbd{C-h i} to invoke info, and then @kbd{m}
1364 (stands for menu) followed by the menu item desired, as in @kbd{m Emacs}, to
1365 get to the Emacs manual.
1366 Help on Emacs is also available by typing @kbd{HELP EMACS} at the DCL command
1367 prompt.
1368
1369 The tutorial is highly recommended in order to learn the intricacies of Emacs,
1370 which is sufficiently extensible to provide for a complete programming
1371 environment and shell for the sophisticated user.
1372 @end ifset
1373
1374 @ifclear vms
1375 @node Introduction to GPS
1376 @section Introduction to GPS
1377 @cindex GPS (GNAT Programming Studio)
1378 @cindex GNAT Programming Studio (GPS)
1379 @noindent
1380 Although the command line interface (@command{gnatmake}, etc.) alone
1381 is sufficient, a graphical Interactive Development
1382 Environment can make it easier for you to compose, navigate, and debug
1383 programs. This section describes the main features of GPS
1384 (``GNAT Programming Studio''), the GNAT graphical IDE.
1385 You will see how to use GPS to build and debug an executable, and
1386 you will also learn some of the basics of the GNAT ``project'' facility.
1387
1388 GPS enables you to do much more than is presented here;
1389 e.g., you can produce a call graph, interface to a third-party
1390 Version Control System, and inspect the generated assembly language
1391 for a program.
1392 Indeed, GPS also supports languages other than Ada.
1393 Such additional information, and an explanation of all of the GPS menu
1394 items. may be found in the on-line help, which includes
1395 a user's guide and a tutorial (these are also accessible from the GNAT
1396 startup menu).
1397
1398 @menu
1399 * Building a New Program with GPS::
1400 * Simple Debugging with GPS::
1401 @end menu
1402
1403 @node Building a New Program with GPS
1404 @subsection Building a New Program with GPS
1405 @noindent
1406 GPS invokes the GNAT compilation tools using information
1407 contained in a @emph{project} (also known as a @emph{project file}):
1408 a collection of properties such
1409 as source directories, identities of main subprograms, tool switches, etc.,
1410 and their associated values.
1411 See @ref{GNAT Project Manager} for details.
1412 In order to run GPS, you will need to either create a new project
1413 or else open an existing one.
1414
1415 This section will explain how you can use GPS to create a project,
1416 to associate Ada source files with a project, and to build and run
1417 programs.
1418
1419 @enumerate
1420 @item @emph{Creating a project}
1421
1422 Invoke GPS, either from the command line or the platform's IDE.
1423 After it starts, GPS will display a ``Welcome'' screen with three
1424 radio buttons:
1425
1426 @itemize @bullet
1427 @item
1428 @code{Start with default project in directory}
1429
1430 @item
1431 @code{Create new project with wizard}
1432
1433 @item
1434 @code{Open existing project}
1435 @end itemize
1436
1437 @noindent
1438 Select @code{Create new project with wizard} and press @code{OK}.
1439 A new window will appear. In the text box labeled with
1440 @code{Enter the name of the project to create}, type @file{sample}
1441 as the project name.
1442 In the next box, browse to choose the directory in which you
1443 would like to create the project file.
1444 After selecting an appropriate directory, press @code{Forward}.
1445
1446 A window will appear with the title
1447 @code{Version Control System Configuration}.
1448 Simply press @code{Forward}.
1449
1450 A window will appear with the title
1451 @code{Please select the source directories for this project}.
1452 The directory that you specified for the project file will be selected
1453 by default as the one to use for sources; simply press @code{Forward}.
1454
1455 A window will appear with the title
1456 @code{Please select the build directory for this project}.
1457 The directory that you specified for the project file will be selected
1458 by default for object files and executables;
1459 simply press @code{Forward}.
1460
1461 A window will appear with the title
1462 @code{Please select the main units for this project}.
1463 You will supply this information later, after creating the source file.
1464 Simply press @code{Forward} for now.
1465
1466 A window will appear with the title
1467 @code{Please select the switches to build the project}.
1468 Press @code{Apply}. This will create a project file named
1469 @file{sample.prj} in the directory that you had specified.
1470
1471 @item @emph{Creating and saving the source file}
1472
1473 After you create the new project, a GPS window will appear, which is
1474 partitioned into two main sections:
1475
1476 @itemize @bullet
1477 @item
1478 A @emph{Workspace area}, initially greyed out, which you will use for
1479 creating and editing source files
1480
1481 @item
1482 Directly below, a @emph{Messages area}, which initially displays a
1483 ``Welcome'' message.
1484 (If the Messages area is not visible, drag its border upward to expand it.)
1485 @end itemize
1486
1487 @noindent
1488 Select @code{File} on the menu bar, and then the @code{New} command.
1489 The Workspace area will become white, and you can now
1490 enter the source program explicitly.
1491 Type the following text
1492
1493 @smallexample @c ada
1494 @group
1495 with Ada.Text_IO; use Ada.Text_IO;
1496 procedure Hello is
1497 begin
1498 Put_Line("Hello from GPS!");
1499 end Hello;
1500 @end group
1501 @end smallexample
1502
1503 @noindent
1504 Select @code{File}, then @code{Save As}, and enter the source file name
1505 @file{hello.adb}.
1506 The file will be saved in the same directory you specified as the
1507 location of the default project file.
1508
1509 @item @emph{Updating the project file}
1510
1511 You need to add the new source file to the project.
1512 To do this, select
1513 the @code{Project} menu and then @code{Edit project properties}.
1514 Click the @code{Main files} tab on the left, and then the
1515 @code{Add} button.
1516 Choose @file{hello.adb} from the list, and press @code{Open}.
1517 The project settings window will reflect this action.
1518 Click @code{OK}.
1519
1520 @item @emph{Building and running the program}
1521
1522 In the main GPS window, now choose the @code{Build} menu, then @code{Make},
1523 and select @file{hello.adb}.
1524 The Messages window will display the resulting invocations of @command{gcc},
1525 @command{gnatbind}, and @command{gnatlink}
1526 (reflecting the default switch settings from the
1527 project file that you created) and then a ``successful compilation/build''
1528 message.
1529
1530 To run the program, choose the @code{Build} menu, then @code{Run}, and
1531 select @command{hello}.
1532 An @emph{Arguments Selection} window will appear.
1533 There are no command line arguments, so just click @code{OK}.
1534
1535 The Messages window will now display the program's output (the string
1536 @code{Hello from GPS}), and at the bottom of the GPS window a status
1537 update is displayed (@code{Run: hello}).
1538 Close the GPS window (or select @code{File}, then @code{Exit}) to
1539 terminate this GPS session.
1540 @end enumerate
1541
1542 @node Simple Debugging with GPS
1543 @subsection Simple Debugging with GPS
1544 @noindent
1545 This section illustrates basic debugging techniques (setting breakpoints,
1546 examining/modifying variables, single stepping).
1547
1548 @enumerate
1549 @item @emph{Opening a project}
1550
1551 Start GPS and select @code{Open existing project}; browse to
1552 specify the project file @file{sample.prj} that you had created in the
1553 earlier example.
1554
1555 @item @emph{Creating a source file}
1556
1557 Select @code{File}, then @code{New}, and type in the following program:
1558
1559 @smallexample @c ada
1560 @group
1561 with Ada.Text_IO; use Ada.Text_IO;
1562 procedure Example is
1563 Line : String (1..80);
1564 N : Natural;
1565 begin
1566 Put_Line("Type a line of text at each prompt; an empty line to exit");
1567 loop
1568 Put(": ");
1569 Get_Line (Line, N);
1570 Put_Line (Line (1..N) );
1571 exit when N=0;
1572 end loop;
1573 end Example;
1574 @end group
1575 @end smallexample
1576
1577 @noindent
1578 Select @code{File}, then @code{Save as}, and enter the file name
1579 @file{example.adb}.
1580
1581 @item @emph{Updating the project file}
1582
1583 Add @code{Example} as a new main unit for the project:
1584 @enumerate a
1585 @item
1586 Select @code{Project}, then @code{Edit Project Properties}.
1587
1588 @item
1589 Select the @code{Main files} tab, click @code{Add}, then
1590 select the file @file{example.adb} from the list, and
1591 click @code{Open}.
1592 You will see the file name appear in the list of main units
1593
1594 @item
1595 Click @code{OK}
1596 @end enumerate
1597
1598 @item @emph{Building/running the executable}
1599
1600 To build the executable
1601 select @code{Build}, then @code{Make}, and then choose @file{example.adb}.
1602
1603 Run the program to see its effect (in the Messages area).
1604 Each line that you enter is displayed; an empty line will
1605 cause the loop to exit and the program to terminate.
1606
1607 @item @emph{Debugging the program}
1608
1609 Note that the @option{-g} switches to @command{gcc} and @command{gnatlink},
1610 which are required for debugging, are on by default when you create
1611 a new project.
1612 Thus unless you intentionally remove these settings, you will be able
1613 to debug any program that you develop using GPS.
1614
1615 @enumerate a
1616 @item @emph{Initializing}
1617
1618 Select @code{Debug}, then @code{Initialize}, then @file{example}
1619
1620 @item @emph{Setting a breakpoint}
1621
1622 After performing the initialization step, you will observe a small
1623 icon to the right of each line number.
1624 This serves as a toggle for breakpoints; clicking the icon will
1625 set a breakpoint at the corresponding line (the icon will change to
1626 a red circle with an ``x''), and clicking it again
1627 will remove the breakpoint / reset the icon.
1628
1629 For purposes of this example, set a breakpoint at line 10 (the
1630 statement @code{Put_Line@ (Line@ (1..N));}
1631
1632 @item @emph{Starting program execution}
1633
1634 Select @code{Debug}, then @code{Run}. When the
1635 @code{Program Arguments} window appears, click @code{OK}.
1636 A console window will appear; enter some line of text,
1637 e.g.@: @code{abcde}, at the prompt.
1638 The program will pause execution when it gets to the
1639 breakpoint, and the corresponding line is highlighted.
1640
1641 @item @emph{Examining a variable}
1642
1643 Move the mouse over one of the occurrences of the variable @code{N}.
1644 You will see the value (5) displayed, in ``tool tip'' fashion.
1645 Right click on @code{N}, select @code{Debug}, then select @code{Display N}.
1646 You will see information about @code{N} appear in the @code{Debugger Data}
1647 pane, showing the value as 5.
1648
1649 @item @emph{Assigning a new value to a variable}
1650
1651 Right click on the @code{N} in the @code{Debugger Data} pane, and
1652 select @code{Set value of N}.
1653 When the input window appears, enter the value @code{4} and click
1654 @code{OK}.
1655 This value does not automatically appear in the @code{Debugger Data}
1656 pane; to see it, right click again on the @code{N} in the
1657 @code{Debugger Data} pane and select @code{Update value}.
1658 The new value, 4, will appear in red.
1659
1660 @item @emph{Single stepping}
1661
1662 Select @code{Debug}, then @code{Next}.
1663 This will cause the next statement to be executed, in this case the
1664 call of @code{Put_Line} with the string slice.
1665 Notice in the console window that the displayed string is simply
1666 @code{abcd} and not @code{abcde} which you had entered.
1667 This is because the upper bound of the slice is now 4 rather than 5.
1668
1669 @item @emph{Removing a breakpoint}
1670
1671 Toggle the breakpoint icon at line 10.
1672
1673 @item @emph{Resuming execution from a breakpoint}
1674
1675 Select @code{Debug}, then @code{Continue}.
1676 The program will reach the next iteration of the loop, and
1677 wait for input after displaying the prompt.
1678 This time, just hit the @kbd{Enter} key.
1679 The value of @code{N} will be 0, and the program will terminate.
1680 The console window will disappear.
1681 @end enumerate
1682 @end enumerate
1683 @end ifclear
1684
1685 @node The GNAT Compilation Model
1686 @chapter The GNAT Compilation Model
1687 @cindex GNAT compilation model
1688 @cindex Compilation model
1689
1690 @menu
1691 * Source Representation::
1692 * Foreign Language Representation::
1693 * File Naming Rules::
1694 * Using Other File Names::
1695 * Alternative File Naming Schemes::
1696 * Generating Object Files::
1697 * Source Dependencies::
1698 * The Ada Library Information Files::
1699 * Binding an Ada Program::
1700 * Mixed Language Programming::
1701 @ifclear vms
1702 * Building Mixed Ada & C++ Programs::
1703 * Comparison between GNAT and C/C++ Compilation Models::
1704 @end ifclear
1705 * Comparison between GNAT and Conventional Ada Library Models::
1706 @ifset vms
1707 * Placement of temporary files::
1708 @end ifset
1709 @end menu
1710
1711 @noindent
1712 This chapter describes the compilation model used by GNAT. Although
1713 similar to that used by other languages, such as C and C++, this model
1714 is substantially different from the traditional Ada compilation models,
1715 which are based on a library. The model is initially described without
1716 reference to the library-based model. If you have not previously used an
1717 Ada compiler, you need only read the first part of this chapter. The
1718 last section describes and discusses the differences between the GNAT
1719 model and the traditional Ada compiler models. If you have used other
1720 Ada compilers, this section will help you to understand those
1721 differences, and the advantages of the GNAT model.
1722
1723 @node Source Representation
1724 @section Source Representation
1725 @cindex Latin-1
1726
1727 @noindent
1728 Ada source programs are represented in standard text files, using
1729 Latin-1 coding. Latin-1 is an 8-bit code that includes the familiar
1730 7-bit ASCII set, plus additional characters used for
1731 representing foreign languages (@pxref{Foreign Language Representation}
1732 for support of non-USA character sets). The format effector characters
1733 are represented using their standard ASCII encodings, as follows:
1734
1735 @table @code
1736 @item VT
1737 @findex VT
1738 Vertical tab, @code{16#0B#}
1739
1740 @item HT
1741 @findex HT
1742 Horizontal tab, @code{16#09#}
1743
1744 @item CR
1745 @findex CR
1746 Carriage return, @code{16#0D#}
1747
1748 @item LF
1749 @findex LF
1750 Line feed, @code{16#0A#}
1751
1752 @item FF
1753 @findex FF
1754 Form feed, @code{16#0C#}
1755 @end table
1756
1757 @noindent
1758 Source files are in standard text file format. In addition, GNAT will
1759 recognize a wide variety of stream formats, in which the end of
1760 physical lines is marked by any of the following sequences:
1761 @code{LF}, @code{CR}, @code{CR-LF}, or @code{LF-CR}. This is useful
1762 in accommodating files that are imported from other operating systems.
1763
1764 @cindex End of source file
1765 @cindex Source file, end
1766 @findex SUB
1767 The end of a source file is normally represented by the physical end of
1768 file. However, the control character @code{16#1A#} (@code{SUB}) is also
1769 recognized as signalling the end of the source file. Again, this is
1770 provided for compatibility with other operating systems where this
1771 code is used to represent the end of file.
1772
1773 Each file contains a single Ada compilation unit, including any pragmas
1774 associated with the unit. For example, this means you must place a
1775 package declaration (a package @dfn{spec}) and the corresponding body in
1776 separate files. An Ada @dfn{compilation} (which is a sequence of
1777 compilation units) is represented using a sequence of files. Similarly,
1778 you will place each subunit or child unit in a separate file.
1779
1780 @node Foreign Language Representation
1781 @section Foreign Language Representation
1782
1783 @noindent
1784 GNAT supports the standard character sets defined in Ada as well as
1785 several other non-standard character sets for use in localized versions
1786 of the compiler (@pxref{Character Set Control}).
1787 @menu
1788 * Latin-1::
1789 * Other 8-Bit Codes::
1790 * Wide Character Encodings::
1791 @end menu
1792
1793 @node Latin-1
1794 @subsection Latin-1
1795 @cindex Latin-1
1796
1797 @noindent
1798 The basic character set is Latin-1. This character set is defined by ISO
1799 standard 8859, part 1. The lower half (character codes @code{16#00#}
1800 @dots{} @code{16#7F#)} is identical to standard ASCII coding, but the upper half
1801 is used to represent additional characters. These include extended letters
1802 used by European languages, such as French accents, the vowels with umlauts
1803 used in German, and the extra letter A-ring used in Swedish.
1804
1805 @findex Ada.Characters.Latin_1
1806 For a complete list of Latin-1 codes and their encodings, see the source
1807 file of library unit @code{Ada.Characters.Latin_1} in file
1808 @file{a-chlat1.ads}.
1809 You may use any of these extended characters freely in character or
1810 string literals. In addition, the extended characters that represent
1811 letters can be used in identifiers.
1812
1813 @node Other 8-Bit Codes
1814 @subsection Other 8-Bit Codes
1815
1816 @noindent
1817 GNAT also supports several other 8-bit coding schemes:
1818
1819 @table @asis
1820 @item ISO 8859-2 (Latin-2)
1821 @cindex Latin-2
1822 @cindex ISO 8859-2
1823 Latin-2 letters allowed in identifiers, with uppercase and lowercase
1824 equivalence.
1825
1826 @item ISO 8859-3 (Latin-3)
1827 @cindex Latin-3
1828 @cindex ISO 8859-3
1829 Latin-3 letters allowed in identifiers, with uppercase and lowercase
1830 equivalence.
1831
1832 @item ISO 8859-4 (Latin-4)
1833 @cindex Latin-4
1834 @cindex ISO 8859-4
1835 Latin-4 letters allowed in identifiers, with uppercase and lowercase
1836 equivalence.
1837
1838 @item ISO 8859-5 (Cyrillic)
1839 @cindex ISO 8859-5
1840 @cindex Cyrillic
1841 ISO 8859-5 letters (Cyrillic) allowed in identifiers, with uppercase and
1842 lowercase equivalence.
1843
1844 @item ISO 8859-15 (Latin-9)
1845 @cindex ISO 8859-15
1846 @cindex Latin-9
1847 ISO 8859-15 (Latin-9) letters allowed in identifiers, with uppercase and
1848 lowercase equivalence
1849
1850 @item IBM PC (code page 437)
1851 @cindex code page 437
1852 This code page is the normal default for PCs in the U.S. It corresponds
1853 to the original IBM PC character set. This set has some, but not all, of
1854 the extended Latin-1 letters, but these letters do not have the same
1855 encoding as Latin-1. In this mode, these letters are allowed in
1856 identifiers with uppercase and lowercase equivalence.
1857
1858 @item IBM PC (code page 850)
1859 @cindex code page 850
1860 This code page is a modification of 437 extended to include all the
1861 Latin-1 letters, but still not with the usual Latin-1 encoding. In this
1862 mode, all these letters are allowed in identifiers with uppercase and
1863 lowercase equivalence.
1864
1865 @item Full Upper 8-bit
1866 Any character in the range 80-FF allowed in identifiers, and all are
1867 considered distinct. In other words, there are no uppercase and lowercase
1868 equivalences in this range. This is useful in conjunction with
1869 certain encoding schemes used for some foreign character sets (e.g.,
1870 the typical method of representing Chinese characters on the PC).
1871
1872 @item No Upper-Half
1873 No upper-half characters in the range 80-FF are allowed in identifiers.
1874 This gives Ada 83 compatibility for identifier names.
1875 @end table
1876
1877 @noindent
1878 For precise data on the encodings permitted, and the uppercase and lowercase
1879 equivalences that are recognized, see the file @file{csets.adb} in
1880 the GNAT compiler sources. You will need to obtain a full source release
1881 of GNAT to obtain this file.
1882
1883 @node Wide Character Encodings
1884 @subsection Wide Character Encodings
1885
1886 @noindent
1887 GNAT allows wide character codes to appear in character and string
1888 literals, and also optionally in identifiers, by means of the following
1889 possible encoding schemes:
1890
1891 @table @asis
1892
1893 @item Hex Coding
1894 In this encoding, a wide character is represented by the following five
1895 character sequence:
1896
1897 @smallexample
1898 ESC a b c d
1899 @end smallexample
1900
1901 @noindent
1902 Where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
1903 characters (using uppercase letters) of the wide character code. For
1904 example, ESC A345 is used to represent the wide character with code
1905 @code{16#A345#}.
1906 This scheme is compatible with use of the full Wide_Character set.
1907
1908 @item Upper-Half Coding
1909 @cindex Upper-Half Coding
1910 The wide character with encoding @code{16#abcd#} where the upper bit is on
1911 (in other words, ``a'' is in the range 8-F) is represented as two bytes,
1912 @code{16#ab#} and @code{16#cd#}. The second byte cannot be a format control
1913 character, but is not required to be in the upper half. This method can
1914 be also used for shift-JIS or EUC, where the internal coding matches the
1915 external coding.
1916
1917 @item Shift JIS Coding
1918 @cindex Shift JIS Coding
1919 A wide character is represented by a two-character sequence,
1920 @code{16#ab#} and
1921 @code{16#cd#}, with the restrictions described for upper-half encoding as
1922 described above. The internal character code is the corresponding JIS
1923 character according to the standard algorithm for Shift-JIS
1924 conversion. Only characters defined in the JIS code set table can be
1925 used with this encoding method.
1926
1927 @item EUC Coding
1928 @cindex EUC Coding
1929 A wide character is represented by a two-character sequence
1930 @code{16#ab#} and
1931 @code{16#cd#}, with both characters being in the upper half. The internal
1932 character code is the corresponding JIS character according to the EUC
1933 encoding algorithm. Only characters defined in the JIS code set table
1934 can be used with this encoding method.
1935
1936 @item UTF-8 Coding
1937 A wide character is represented using
1938 UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO
1939 10646-1/Am.2. Depending on the character value, the representation
1940 is a one, two, or three byte sequence:
1941 @smallexample
1942 @iftex
1943 @leftskip=.7cm
1944 @end iftex
1945 16#0000#-16#007f#: 2#0@var{xxxxxxx}#
1946 16#0080#-16#07ff#: 2#110@var{xxxxx}# 2#10@var{xxxxxx}#
1947 16#0800#-16#ffff#: 2#1110@var{xxxx}# 2#10@var{xxxxxx}# 2#10@var{xxxxxx}#
1948
1949 @end smallexample
1950
1951 @noindent
1952 where the @var{xxx} bits correspond to the left-padded bits of the
1953 16-bit character value. Note that all lower half ASCII characters
1954 are represented as ASCII bytes and all upper half characters and
1955 other wide characters are represented as sequences of upper-half
1956 (The full UTF-8 scheme allows for encoding 31-bit characters as
1957 6-byte sequences, but in this implementation, all UTF-8 sequences
1958 of four or more bytes length will be treated as illegal).
1959 @item Brackets Coding
1960 In this encoding, a wide character is represented by the following eight
1961 character sequence:
1962
1963 @smallexample
1964 [ " a b c d " ]
1965 @end smallexample
1966
1967 @noindent
1968 Where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
1969 characters (using uppercase letters) of the wide character code. For
1970 example, [``A345''] is used to represent the wide character with code
1971 @code{16#A345#}. It is also possible (though not required) to use the
1972 Brackets coding for upper half characters. For example, the code
1973 @code{16#A3#} can be represented as @code{[``A3'']}.
1974
1975 This scheme is compatible with use of the full Wide_Character set,
1976 and is also the method used for wide character encoding in the standard
1977 ACVC (Ada Compiler Validation Capability) test suite distributions.
1978
1979 @end table
1980
1981 @noindent
1982 Note: Some of these coding schemes do not permit the full use of the
1983 Ada character set. For example, neither Shift JIS, nor EUC allow the
1984 use of the upper half of the Latin-1 set.
1985
1986 @node File Naming Rules
1987 @section File Naming Rules
1988
1989 @noindent
1990 The default file name is determined by the name of the unit that the
1991 file contains. The name is formed by taking the full expanded name of
1992 the unit and replacing the separating dots with hyphens and using
1993 ^lowercase^uppercase^ for all letters.
1994
1995 An exception arises if the file name generated by the above rules starts
1996 with one of the characters
1997 @ifset vms
1998 @samp{A}, @samp{G}, @samp{I}, or @samp{S},
1999 @end ifset
2000 @ifclear vms
2001 @samp{a}, @samp{g}, @samp{i}, or @samp{s},
2002 @end ifclear
2003 and the second character is a
2004 minus. In this case, the character ^tilde^dollar sign^ is used in place
2005 of the minus. The reason for this special rule is to avoid clashes with
2006 the standard names for child units of the packages System, Ada,
2007 Interfaces, and GNAT, which use the prefixes
2008 @ifset vms
2009 @samp{S-}, @samp{A-}, @samp{I-}, and @samp{G-},
2010 @end ifset
2011 @ifclear vms
2012 @samp{s-}, @samp{a-}, @samp{i-}, and @samp{g-},
2013 @end ifclear
2014 respectively.
2015
2016 The file extension is @file{.ads} for a spec and
2017 @file{.adb} for a body. The following list shows some
2018 examples of these rules.
2019
2020 @table @file
2021 @item main.ads
2022 Main (spec)
2023 @item main.adb
2024 Main (body)
2025 @item arith_functions.ads
2026 Arith_Functions (package spec)
2027 @item arith_functions.adb
2028 Arith_Functions (package body)
2029 @item func-spec.ads
2030 Func.Spec (child package spec)
2031 @item func-spec.adb
2032 Func.Spec (child package body)
2033 @item main-sub.adb
2034 Sub (subunit of Main)
2035 @item ^a~bad.adb^A$BAD.ADB^
2036 A.Bad (child package body)
2037 @end table
2038
2039 @noindent
2040 Following these rules can result in excessively long
2041 file names if corresponding
2042 unit names are long (for example, if child units or subunits are
2043 heavily nested). An option is available to shorten such long file names
2044 (called file name ``krunching''). This may be particularly useful when
2045 programs being developed with GNAT are to be used on operating systems
2046 with limited file name lengths. @xref{Using gnatkr}.
2047
2048 Of course, no file shortening algorithm can guarantee uniqueness over
2049 all possible unit names; if file name krunching is used, it is your
2050 responsibility to ensure no name clashes occur. Alternatively you
2051 can specify the exact file names that you want used, as described
2052 in the next section. Finally, if your Ada programs are migrating from a
2053 compiler with a different naming convention, you can use the gnatchop
2054 utility to produce source files that follow the GNAT naming conventions.
2055 (For details @pxref{Renaming Files Using gnatchop}.)
2056
2057 Note: in the case of @code{Windows NT/XP} or @code{OpenVMS} operating
2058 systems, case is not significant. So for example on @code{Windows XP}
2059 if the canonical name is @code{main-sub.adb}, you can use the file name
2060 @code{Main-Sub.adb} instead. However, case is significant for other
2061 operating systems, so for example, if you want to use other than
2062 canonically cased file names on a Unix system, you need to follow
2063 the procedures described in the next section.
2064
2065 @node Using Other File Names
2066 @section Using Other File Names
2067 @cindex File names
2068
2069 @noindent
2070 In the previous section, we have described the default rules used by
2071 GNAT to determine the file name in which a given unit resides. It is
2072 often convenient to follow these default rules, and if you follow them,
2073 the compiler knows without being explicitly told where to find all
2074 the files it needs.
2075
2076 However, in some cases, particularly when a program is imported from
2077 another Ada compiler environment, it may be more convenient for the
2078 programmer to specify which file names contain which units. GNAT allows
2079 arbitrary file names to be used by means of the Source_File_Name pragma.
2080 The form of this pragma is as shown in the following examples:
2081 @cindex Source_File_Name pragma
2082
2083 @smallexample @c ada
2084 @cartouche
2085 pragma Source_File_Name (My_Utilities.Stacks,
2086 Spec_File_Name => "myutilst_a.ada");
2087 pragma Source_File_name (My_Utilities.Stacks,
2088 Body_File_Name => "myutilst.ada");
2089 @end cartouche
2090 @end smallexample
2091
2092 @noindent
2093 As shown in this example, the first argument for the pragma is the unit
2094 name (in this example a child unit). The second argument has the form
2095 of a named association. The identifier
2096 indicates whether the file name is for a spec or a body;
2097 the file name itself is given by a string literal.
2098
2099 The source file name pragma is a configuration pragma, which means that
2100 normally it will be placed in the @file{gnat.adc}
2101 file used to hold configuration
2102 pragmas that apply to a complete compilation environment.
2103 For more details on how the @file{gnat.adc} file is created and used
2104 see @ref{Handling of Configuration Pragmas}.
2105 @cindex @file{gnat.adc}
2106
2107 @ifclear vms
2108 GNAT allows completely arbitrary file names to be specified using the
2109 source file name pragma. However, if the file name specified has an
2110 extension other than @file{.ads} or @file{.adb} it is necessary to use
2111 a special syntax when compiling the file. The name in this case must be
2112 preceded by the special sequence @option{-x} followed by a space and the name
2113 of the language, here @code{ada}, as in:
2114
2115 @smallexample
2116 $ gcc -c -x ada peculiar_file_name.sim
2117 @end smallexample
2118 @end ifclear
2119
2120 @noindent
2121 @command{gnatmake} handles non-standard file names in the usual manner (the
2122 non-standard file name for the main program is simply used as the
2123 argument to gnatmake). Note that if the extension is also non-standard,
2124 then it must be included in the @command{gnatmake} command, it may not
2125 be omitted.
2126
2127 @node Alternative File Naming Schemes
2128 @section Alternative File Naming Schemes
2129 @cindex File naming schemes, alternative
2130 @cindex File names
2131
2132 In the previous section, we described the use of the @code{Source_File_Name}
2133 pragma to allow arbitrary names to be assigned to individual source files.
2134 However, this approach requires one pragma for each file, and especially in
2135 large systems can result in very long @file{gnat.adc} files, and also create
2136 a maintenance problem.
2137
2138 GNAT also provides a facility for specifying systematic file naming schemes
2139 other than the standard default naming scheme previously described. An
2140 alternative scheme for naming is specified by the use of
2141 @code{Source_File_Name} pragmas having the following format:
2142 @cindex Source_File_Name pragma
2143
2144 @smallexample @c ada
2145 pragma Source_File_Name (
2146 Spec_File_Name => FILE_NAME_PATTERN
2147 @r{[},Casing => CASING_SPEC@r{]}
2148 @r{[},Dot_Replacement => STRING_LITERAL@r{]});
2149
2150 pragma Source_File_Name (
2151 Body_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 Subunit_File_Name => FILE_NAME_PATTERN
2157 @r{[},Casing => CASING_SPEC@r{]}
2158 @r{[},Dot_Replacement => STRING_LITERAL@r{]});
2159
2160 FILE_NAME_PATTERN ::= STRING_LITERAL
2161 CASING_SPEC ::= Lowercase | Uppercase | Mixedcase
2162 @end smallexample
2163
2164 @noindent
2165 The @code{FILE_NAME_PATTERN} string shows how the file name is constructed.
2166 It contains a single asterisk character, and the unit name is substituted
2167 systematically for this asterisk. The optional parameter
2168 @code{Casing} indicates
2169 whether the unit name is to be all upper-case letters, all lower-case letters,
2170 or mixed-case. If no
2171 @code{Casing} parameter is used, then the default is all
2172 ^lower-case^upper-case^.
2173
2174 The optional @code{Dot_Replacement} string is used to replace any periods
2175 that occur in subunit or child unit names. If no @code{Dot_Replacement}
2176 argument is used then separating dots appear unchanged in the resulting
2177 file name.
2178 Although the above syntax indicates that the
2179 @code{Casing} argument must appear
2180 before the @code{Dot_Replacement} argument, but it
2181 is also permissible to write these arguments in the opposite order.
2182
2183 As indicated, it is possible to specify different naming schemes for
2184 bodies, specs, and subunits. Quite often the rule for subunits is the
2185 same as the rule for bodies, in which case, there is no need to give
2186 a separate @code{Subunit_File_Name} rule, and in this case the
2187 @code{Body_File_name} rule is used for subunits as well.
2188
2189 The separate rule for subunits can also be used to implement the rather
2190 unusual case of a compilation environment (e.g.@: a single directory) which
2191 contains a subunit and a child unit with the same unit name. Although
2192 both units cannot appear in the same partition, the Ada Reference Manual
2193 allows (but does not require) the possibility of the two units coexisting
2194 in the same environment.
2195
2196 The file name translation works in the following steps:
2197
2198 @itemize @bullet
2199
2200 @item
2201 If there is a specific @code{Source_File_Name} pragma for the given unit,
2202 then this is always used, and any general pattern rules are ignored.
2203
2204 @item
2205 If there is a pattern type @code{Source_File_Name} pragma that applies to
2206 the unit, then the resulting file name will be used if the file exists. If
2207 more than one pattern matches, the latest one will be tried first, and the
2208 first attempt resulting in a reference to a file that exists will be used.
2209
2210 @item
2211 If no pattern type @code{Source_File_Name} pragma that applies to the unit
2212 for which the corresponding file exists, then the standard GNAT default
2213 naming rules are used.
2214
2215 @end itemize
2216
2217 @noindent
2218 As an example of the use of this mechanism, consider a commonly used scheme
2219 in which file names are all lower case, with separating periods copied
2220 unchanged to the resulting file name, and specs end with @file{.1.ada}, and
2221 bodies end with @file{.2.ada}. GNAT will follow this scheme if the following
2222 two pragmas appear:
2223
2224 @smallexample @c ada
2225 pragma Source_File_Name
2226 (Spec_File_Name => "*.1.ada");
2227 pragma Source_File_Name
2228 (Body_File_Name => "*.2.ada");
2229 @end smallexample
2230
2231 @noindent
2232 The default GNAT scheme is actually implemented by providing the following
2233 default pragmas internally:
2234
2235 @smallexample @c ada
2236 pragma Source_File_Name
2237 (Spec_File_Name => "*.ads", Dot_Replacement => "-");
2238 pragma Source_File_Name
2239 (Body_File_Name => "*.adb", Dot_Replacement => "-");
2240 @end smallexample
2241
2242 @noindent
2243 Our final example implements a scheme typically used with one of the
2244 Ada 83 compilers, where the separator character for subunits was ``__''
2245 (two underscores), specs were identified by adding @file{_.ADA}, bodies
2246 by adding @file{.ADA}, and subunits by
2247 adding @file{.SEP}. All file names were
2248 upper case. Child units were not present of course since this was an
2249 Ada 83 compiler, but it seems reasonable to extend this scheme to use
2250 the same double underscore separator for child units.
2251
2252 @smallexample @c ada
2253 pragma Source_File_Name
2254 (Spec_File_Name => "*_.ADA",
2255 Dot_Replacement => "__",
2256 Casing = Uppercase);
2257 pragma Source_File_Name
2258 (Body_File_Name => "*.ADA",
2259 Dot_Replacement => "__",
2260 Casing = Uppercase);
2261 pragma Source_File_Name
2262 (Subunit_File_Name => "*.SEP",
2263 Dot_Replacement => "__",
2264 Casing = Uppercase);
2265 @end smallexample
2266
2267 @node Generating Object Files
2268 @section Generating Object Files
2269
2270 @noindent
2271 An Ada program consists of a set of source files, and the first step in
2272 compiling the program is to generate the corresponding object files.
2273 These are generated by compiling a subset of these source files.
2274 The files you need to compile are the following:
2275
2276 @itemize @bullet
2277 @item
2278 If a package spec has no body, compile the package spec to produce the
2279 object file for the package.
2280
2281 @item
2282 If a package has both a spec and a body, compile the body to produce the
2283 object file for the package. The source file for the package spec need
2284 not be compiled in this case because there is only one object file, which
2285 contains the code for both the spec and body of the package.
2286
2287 @item
2288 For a subprogram, compile the subprogram body to produce the object file
2289 for the subprogram. The spec, if one is present, is as usual in a
2290 separate file, and need not be compiled.
2291
2292 @item
2293 @cindex Subunits
2294 In the case of subunits, only compile the parent unit. A single object
2295 file is generated for the entire subunit tree, which includes all the
2296 subunits.
2297
2298 @item
2299 Compile child units independently of their parent units
2300 (though, of course, the spec of all the ancestor unit must be present in order
2301 to compile a child unit).
2302
2303 @item
2304 @cindex Generics
2305 Compile generic units in the same manner as any other units. The object
2306 files in this case are small dummy files that contain at most the
2307 flag used for elaboration checking. This is because GNAT always handles generic
2308 instantiation by means of macro expansion. However, it is still necessary to
2309 compile generic units, for dependency checking and elaboration purposes.
2310 @end itemize
2311
2312 @noindent
2313 The preceding rules describe the set of files that must be compiled to
2314 generate the object files for a program. Each object file has the same
2315 name as the corresponding source file, except that the extension is
2316 @file{.o} as usual.
2317
2318 You may wish to compile other files for the purpose of checking their
2319 syntactic and semantic correctness. For example, in the case where a
2320 package has a separate spec and body, you would not normally compile the
2321 spec. However, it is convenient in practice to compile the spec to make
2322 sure it is error-free before compiling clients of this spec, because such
2323 compilations will fail if there is an error in the spec.
2324
2325 GNAT provides an option for compiling such files purely for the
2326 purposes of checking correctness; such compilations are not required as
2327 part of the process of building a program. To compile a file in this
2328 checking mode, use the @option{-gnatc} switch.
2329
2330 @node Source Dependencies
2331 @section Source Dependencies
2332
2333 @noindent
2334 A given object file clearly depends on the source file which is compiled
2335 to produce it. Here we are using @dfn{depends} in the sense of a typical
2336 @code{make} utility; in other words, an object file depends on a source
2337 file if changes to the source file require the object file to be
2338 recompiled.
2339 In addition to this basic dependency, a given object may depend on
2340 additional source files as follows:
2341
2342 @itemize @bullet
2343 @item
2344 If a file being compiled @code{with}'s a unit @var{X}, the object file
2345 depends on the file containing the spec of unit @var{X}. This includes
2346 files that are @code{with}'ed implicitly either because they are parents
2347 of @code{with}'ed child units or they are run-time units required by the
2348 language constructs used in a particular unit.
2349
2350 @item
2351 If a file being compiled instantiates a library level generic unit, the
2352 object file depends on both the spec and body files for this generic
2353 unit.
2354
2355 @item
2356 If a file being compiled instantiates a generic unit defined within a
2357 package, the object file depends on the body file for the package as
2358 well as the spec file.
2359
2360 @item
2361 @findex Inline
2362 @cindex @option{-gnatn} switch
2363 If a file being compiled contains a call to a subprogram for which
2364 pragma @code{Inline} applies and inlining is activated with the
2365 @option{-gnatn} switch, the object file depends on the file containing the
2366 body of this subprogram as well as on the file containing the spec. Note
2367 that for inlining to actually occur as a result of the use of this switch,
2368 it is necessary to compile in optimizing mode.
2369
2370 @cindex @option{-gnatN} switch
2371 The use of @option{-gnatN} activates inlining optimization
2372 that is performed by the front end of the compiler. This inlining does
2373 not require that the code generation be optimized. Like @option{-gnatn},
2374 the use of this switch generates additional dependencies.
2375 Note that
2376 @option{-gnatN} automatically implies @option{-gnatn} so it is not necessary
2377 to specify both options.
2378
2379 When using a gcc-based back end (in practice this means using any version
2380 of GNAT other than the JGNAT, .NET or GNAAMP versions), then the use of
2381 @option{-gnatN} is deprecated, and the use of @option{-gnatn} is preferred.
2382 Historically front end inlining was more extensive than the gcc back end
2383 inlining, but that is no longer the case.
2384
2385 @item
2386 If an object file @file{O} depends on the proper body of a subunit through
2387 inlining or instantiation, it depends on the parent unit of the subunit.
2388 This means that any modification of the parent unit or one of its subunits
2389 affects the compilation of @file{O}.
2390
2391 @item
2392 The object file for a parent unit depends on all its subunit body files.
2393
2394 @item
2395 The previous two rules meant that for purposes of computing dependencies and
2396 recompilation, a body and all its subunits are treated as an indivisible whole.
2397
2398 @noindent
2399 These rules are applied transitively: if unit @code{A} @code{with}'s
2400 unit @code{B}, whose elaboration calls an inlined procedure in package
2401 @code{C}, the object file for unit @code{A} will depend on the body of
2402 @code{C}, in file @file{c.adb}.
2403
2404 The set of dependent files described by these rules includes all the
2405 files on which the unit is semantically dependent, as dictated by the
2406 Ada language standard. However, it is a superset of what the
2407 standard describes, because it includes generic, inline, and subunit
2408 dependencies.
2409
2410 An object file must be recreated by recompiling the corresponding source
2411 file if any of the source files on which it depends are modified. For
2412 example, if the @code{make} utility is used to control compilation,
2413 the rule for an Ada object file must mention all the source files on
2414 which the object file depends, according to the above definition.
2415 The determination of the necessary
2416 recompilations is done automatically when one uses @command{gnatmake}.
2417 @end itemize
2418
2419 @node The Ada Library Information Files
2420 @section The Ada Library Information Files
2421 @cindex Ada Library Information files
2422 @cindex @file{ALI} files
2423
2424 @noindent
2425 Each compilation actually generates two output files. The first of these
2426 is the normal object file that has a @file{.o} extension. The second is a
2427 text file containing full dependency information. It has the same
2428 name as the source file, but an @file{.ali} extension.
2429 This file is known as the Ada Library Information (@file{ALI}) file.
2430 The following information is contained in the @file{ALI} file.
2431
2432 @itemize @bullet
2433 @item
2434 Version information (indicates which version of GNAT was used to compile
2435 the unit(s) in question)
2436
2437 @item
2438 Main program information (including priority and time slice settings,
2439 as well as the wide character encoding used during compilation).
2440
2441 @item
2442 List of arguments used in the @command{gcc} command for the compilation
2443
2444 @item
2445 Attributes of the unit, including configuration pragmas used, an indication
2446 of whether the compilation was successful, exception model used etc.
2447
2448 @item
2449 A list of relevant restrictions applying to the unit (used for consistency)
2450 checking.
2451
2452 @item
2453 Categorization information (e.g.@: use of pragma @code{Pure}).
2454
2455 @item
2456 Information on all @code{with}'ed units, including presence of
2457 @code{Elaborate} or @code{Elaborate_All} pragmas.
2458
2459 @item
2460 Information from any @code{Linker_Options} pragmas used in the unit
2461
2462 @item
2463 Information on the use of @code{Body_Version} or @code{Version}
2464 attributes in the unit.
2465
2466 @item
2467 Dependency information. This is a list of files, together with
2468 time stamp and checksum information. These are files on which
2469 the unit depends in the sense that recompilation is required
2470 if any of these units are modified.
2471
2472 @item
2473 Cross-reference data. Contains information on all entities referenced
2474 in the unit. Used by tools like @code{gnatxref} and @code{gnatfind} to
2475 provide cross-reference information.
2476
2477 @end itemize
2478
2479 @noindent
2480 For a full detailed description of the format of the @file{ALI} file,
2481 see the source of the body of unit @code{Lib.Writ}, contained in file
2482 @file{lib-writ.adb} in the GNAT compiler sources.
2483
2484 @node Binding an Ada Program
2485 @section Binding an Ada Program
2486
2487 @noindent
2488 When using languages such as C and C++, once the source files have been
2489 compiled the only remaining step in building an executable program
2490 is linking the object modules together. This means that it is possible to
2491 link an inconsistent version of a program, in which two units have
2492 included different versions of the same header.
2493
2494 The rules of Ada do not permit such an inconsistent program to be built.
2495 For example, if two clients have different versions of the same package,
2496 it is illegal to build a program containing these two clients.
2497 These rules are enforced by the GNAT binder, which also determines an
2498 elaboration order consistent with the Ada rules.
2499
2500 The GNAT binder is run after all the object files for a program have
2501 been created. It is given the name of the main program unit, and from
2502 this it determines the set of units required by the program, by reading the
2503 corresponding ALI files. It generates error messages if the program is
2504 inconsistent or if no valid order of elaboration exists.
2505
2506 If no errors are detected, the binder produces a main program, in Ada by
2507 default, that contains calls to the elaboration procedures of those
2508 compilation unit that require them, followed by
2509 a call to the main program. This Ada program is compiled to generate the
2510 object file for the main program. The name of
2511 the Ada file is @file{b~@var{xxx}.adb} (with the corresponding spec
2512 @file{b~@var{xxx}.ads}) where @var{xxx} is the name of the
2513 main program unit.
2514
2515 Finally, the linker is used to build the resulting executable program,
2516 using the object from the main program from the bind step as well as the
2517 object files for the Ada units of the program.
2518
2519 @node Mixed Language Programming
2520 @section Mixed Language Programming
2521 @cindex Mixed Language Programming
2522
2523 @noindent
2524 This section describes how to develop a mixed-language program,
2525 specifically one that comprises units in both Ada and C.
2526
2527 @menu
2528 * Interfacing to C::
2529 * Calling Conventions::
2530 @end menu
2531
2532 @node Interfacing to C
2533 @subsection Interfacing to C
2534 @noindent
2535 Interfacing Ada with a foreign language such as C involves using
2536 compiler directives to import and/or export entity definitions in each
2537 language---using @code{extern} statements in C, for instance, and the
2538 @code{Import}, @code{Export}, and @code{Convention} pragmas in Ada.
2539 A full treatment of these topics is provided in Appendix B, section 1
2540 of the Ada Reference Manual.
2541
2542 There are two ways to build a program using GNAT that contains some Ada
2543 sources and some foreign language sources, depending on whether or not
2544 the main subprogram is written in Ada. Here is a source example with
2545 the main subprogram in Ada:
2546
2547 @smallexample
2548 /* file1.c */
2549 #include <stdio.h>
2550
2551 void print_num (int num)
2552 @{
2553 printf ("num is %d.\n", num);
2554 return;
2555 @}
2556
2557 /* file2.c */
2558
2559 /* num_from_Ada is declared in my_main.adb */
2560 extern int num_from_Ada;
2561
2562 int get_num (void)
2563 @{
2564 return num_from_Ada;
2565 @}
2566 @end smallexample
2567
2568 @smallexample @c ada
2569 -- my_main.adb
2570 procedure My_Main is
2571
2572 -- Declare then export an Integer entity called num_from_Ada
2573 My_Num : Integer := 10;
2574 pragma Export (C, My_Num, "num_from_Ada");
2575
2576 -- Declare an Ada function spec for Get_Num, then use
2577 -- C function get_num for the implementation.
2578 function Get_Num return Integer;
2579 pragma Import (C, Get_Num, "get_num");
2580
2581 -- Declare an Ada procedure spec for Print_Num, then use
2582 -- C function print_num for the implementation.
2583 procedure Print_Num (Num : Integer);
2584 pragma Import (C, Print_Num, "print_num");
2585
2586 begin
2587 Print_Num (Get_Num);
2588 end My_Main;
2589 @end smallexample
2590
2591 @enumerate
2592 @item
2593 To build this example, first compile the foreign language files to
2594 generate object files:
2595 @smallexample
2596 ^gcc -c file1.c^gcc -c FILE1.C^
2597 ^gcc -c file2.c^gcc -c FILE2.C^
2598 @end smallexample
2599
2600 @item
2601 Then, compile the Ada units to produce a set of object files and ALI
2602 files:
2603 @smallexample
2604 gnatmake ^-c^/ACTIONS=COMPILE^ my_main.adb
2605 @end smallexample
2606
2607 @item
2608 Run the Ada binder on the Ada main program:
2609 @smallexample
2610 gnatbind my_main.ali
2611 @end smallexample
2612
2613 @item
2614 Link the Ada main program, the Ada objects and the other language
2615 objects:
2616 @smallexample
2617 gnatlink my_main.ali file1.o file2.o
2618 @end smallexample
2619 @end enumerate
2620
2621 The last three steps can be grouped in a single command:
2622 @smallexample
2623 gnatmake my_main.adb -largs file1.o file2.o
2624 @end smallexample
2625
2626 @cindex Binder output file
2627 @noindent
2628 If the main program is in a language other than Ada, then you may have
2629 more than one entry point into the Ada subsystem. You must use a special
2630 binder option to generate callable routines that initialize and
2631 finalize the Ada units (@pxref{Binding with Non-Ada Main Programs}).
2632 Calls to the initialization and finalization routines must be inserted
2633 in the main program, or some other appropriate point in the code. The
2634 call to initialize the Ada units must occur before the first Ada
2635 subprogram is called, and the call to finalize the Ada units must occur
2636 after the last Ada subprogram returns. The binder will place the
2637 initialization and finalization subprograms into the
2638 @file{b~@var{xxx}.adb} file where they can be accessed by your C
2639 sources. To illustrate, we have the following example:
2640
2641 @smallexample
2642 /* main.c */
2643 extern void adainit (void);
2644 extern void adafinal (void);
2645 extern int add (int, int);
2646 extern int sub (int, int);
2647
2648 int main (int argc, char *argv[])
2649 @{
2650 int a = 21, b = 7;
2651
2652 adainit();
2653
2654 /* Should print "21 + 7 = 28" */
2655 printf ("%d + %d = %d\n", a, b, add (a, b));
2656 /* Should print "21 - 7 = 14" */
2657 printf ("%d - %d = %d\n", a, b, sub (a, b));
2658
2659 adafinal();
2660 @}
2661 @end smallexample
2662
2663 @smallexample @c ada
2664 -- unit1.ads
2665 package Unit1 is
2666 function Add (A, B : Integer) return Integer;
2667 pragma Export (C, Add, "add");
2668 end Unit1;
2669
2670 -- unit1.adb
2671 package body Unit1 is
2672 function Add (A, B : Integer) return Integer is
2673 begin
2674 return A + B;
2675 end Add;
2676 end Unit1;
2677
2678 -- unit2.ads
2679 package Unit2 is
2680 function Sub (A, B : Integer) return Integer;
2681 pragma Export (C, Sub, "sub");
2682 end Unit2;
2683
2684 -- unit2.adb
2685 package body Unit2 is
2686 function Sub (A, B : Integer) return Integer is
2687 begin
2688 return A - B;
2689 end Sub;
2690 end Unit2;
2691 @end smallexample
2692
2693 @enumerate
2694 @item
2695 The build procedure for this application is similar to the last
2696 example's. First, compile the foreign language files to generate object
2697 files:
2698 @smallexample
2699 ^gcc -c main.c^gcc -c main.c^
2700 @end smallexample
2701
2702 @item
2703 Next, compile the Ada units to produce a set of object files and ALI
2704 files:
2705 @smallexample
2706 gnatmake ^-c^/ACTIONS=COMPILE^ unit1.adb
2707 gnatmake ^-c^/ACTIONS=COMPILE^ unit2.adb
2708 @end smallexample
2709
2710 @item
2711 Run the Ada binder on every generated ALI file. Make sure to use the
2712 @option{-n} option to specify a foreign main program:
2713 @smallexample
2714 gnatbind ^-n^/NOMAIN^ unit1.ali unit2.ali
2715 @end smallexample
2716
2717 @item
2718 Link the Ada main program, the Ada objects and the foreign language
2719 objects. You need only list the last ALI file here:
2720 @smallexample
2721 gnatlink unit2.ali main.o -o exec_file
2722 @end smallexample
2723
2724 This procedure yields a binary executable called @file{exec_file}.
2725 @end enumerate
2726
2727 @noindent
2728 Depending on the circumstances (for example when your non-Ada main object
2729 does not provide symbol @code{main}), you may also need to instruct the
2730 GNAT linker not to include the standard startup objects by passing the
2731 @option{^-nostartfiles^/NOSTART_FILES^} switch to @command{gnatlink}.
2732
2733 @node Calling Conventions
2734 @subsection Calling Conventions
2735 @cindex Foreign Languages
2736 @cindex Calling Conventions
2737 GNAT follows standard calling sequence conventions and will thus interface
2738 to any other language that also follows these conventions. The following
2739 Convention identifiers are recognized by GNAT:
2740
2741 @table @code
2742 @cindex Interfacing to Ada
2743 @cindex Other Ada compilers
2744 @cindex Convention Ada
2745 @item Ada
2746 This indicates that the standard Ada calling sequence will be
2747 used and all Ada data items may be passed without any limitations in the
2748 case where GNAT is used to generate both the caller and callee. It is also
2749 possible to mix GNAT generated code and code generated by another Ada
2750 compiler. In this case, the data types should be restricted to simple
2751 cases, including primitive types. Whether complex data types can be passed
2752 depends on the situation. Probably it is safe to pass simple arrays, such
2753 as arrays of integers or floats. Records may or may not work, depending
2754 on whether both compilers lay them out identically. Complex structures
2755 involving variant records, access parameters, tasks, or protected types,
2756 are unlikely to be able to be passed.
2757
2758 Note that in the case of GNAT running
2759 on a platform that supports HP Ada 83, a higher degree of compatibility
2760 can be guaranteed, and in particular records are layed out in an identical
2761 manner in the two compilers. Note also that if output from two different
2762 compilers is mixed, the program is responsible for dealing with elaboration
2763 issues. Probably the safest approach is to write the main program in the
2764 version of Ada other than GNAT, so that it takes care of its own elaboration
2765 requirements, and then call the GNAT-generated adainit procedure to ensure
2766 elaboration of the GNAT components. Consult the documentation of the other
2767 Ada compiler for further details on elaboration.
2768
2769 However, it is not possible to mix the tasking run time of GNAT and
2770 HP Ada 83, All the tasking operations must either be entirely within
2771 GNAT compiled sections of the program, or entirely within HP Ada 83
2772 compiled sections of the program.
2773
2774 @cindex Interfacing to Assembly
2775 @cindex Convention Assembler
2776 @item Assembler
2777 Specifies assembler as the convention. In practice this has the
2778 same effect as convention Ada (but is not equivalent in the sense of being
2779 considered the same convention).
2780
2781 @cindex Convention Asm
2782 @findex Asm
2783 @item Asm
2784 Equivalent to Assembler.
2785
2786 @cindex Interfacing to COBOL
2787 @cindex Convention COBOL
2788 @findex COBOL
2789 @item COBOL
2790 Data will be passed according to the conventions described
2791 in section B.4 of the Ada Reference Manual.
2792
2793 @findex C
2794 @cindex Interfacing to C
2795 @cindex Convention C
2796 @item C
2797 Data will be passed according to the conventions described
2798 in section B.3 of the Ada Reference Manual.
2799
2800 A note on interfacing to a C ``varargs'' function:
2801 @findex C varargs function
2802 @cindex Interfacing to C varargs function
2803 @cindex varargs function interfaces
2804
2805 @itemize @bullet
2806 @item
2807 In C, @code{varargs} allows a function to take a variable number of
2808 arguments. There is no direct equivalent in this to Ada. One
2809 approach that can be used is to create a C wrapper for each
2810 different profile and then interface to this C wrapper. For
2811 example, to print an @code{int} value using @code{printf},
2812 create a C function @code{printfi} that takes two arguments, a
2813 pointer to a string and an int, and calls @code{printf}.
2814 Then in the Ada program, use pragma @code{Import} to
2815 interface to @code{printfi}.
2816
2817 @item
2818 It may work on some platforms to directly interface to
2819 a @code{varargs} function by providing a specific Ada profile
2820 for a particular call. However, this does not work on
2821 all platforms, since there is no guarantee that the
2822 calling sequence for a two argument normal C function
2823 is the same as for calling a @code{varargs} C function with
2824 the same two arguments.
2825 @end itemize
2826
2827 @cindex Convention Default
2828 @findex Default
2829 @item Default
2830 Equivalent to C.
2831
2832 @cindex Convention External
2833 @findex External
2834 @item External
2835 Equivalent to C.
2836
2837 @ifclear vms
2838 @findex C++
2839 @cindex Interfacing to C++
2840 @cindex Convention C++
2841 @item C_Plus_Plus (or CPP)
2842 This stands for C++. For most purposes this is identical to C.
2843 See the separate description of the specialized GNAT pragmas relating to
2844 C++ interfacing for further details.
2845 @end ifclear
2846
2847 @findex Fortran
2848 @cindex Interfacing to Fortran
2849 @cindex Convention Fortran
2850 @item Fortran
2851 Data will be passed according to the conventions described
2852 in section B.5 of the Ada Reference Manual.
2853
2854 @item Intrinsic
2855 This applies to an intrinsic operation, as defined in the Ada
2856 Reference Manual. If a pragma Import (Intrinsic) applies to a subprogram,
2857 this means that the body of the subprogram is provided by the compiler itself,
2858 usually by means of an efficient code sequence, and that the user does not
2859 supply an explicit body for it. In an application program, the pragma may
2860 be applied to the following sets of names:
2861
2862 @itemize @bullet
2863 @item
2864 Rotate_Left, Rotate_Right, Shift_Left, Shift_Right,
2865 Shift_Right_Arithmetic. The corresponding subprogram declaration must have
2866 two formal parameters. The
2867 first one must be a signed integer type or a modular type with a binary
2868 modulus, and the second parameter must be of type Natural.
2869 The return type must be the same as the type of the first argument. The size
2870 of this type can only be 8, 16, 32, or 64.
2871
2872 @item
2873 Binary arithmetic operators: ``+'', ``-'', ``*'', ``/''
2874 The corresponding operator declaration must have parameters and result type
2875 that have the same root numeric type (for example, all three are long_float
2876 types). This simplifies the definition of operations that use type checking
2877 to perform dimensional checks:
2878
2879 @smallexample @c ada
2880 type Distance is new Long_Float;
2881 type Time is new Long_Float;
2882 type Velocity is new Long_Float;
2883 function "/" (D : Distance; T : Time)
2884 return Velocity;
2885 pragma Import (Intrinsic, "/");
2886 @end smallexample
2887
2888 @noindent
2889 This common idiom is often programmed with a generic definition and an
2890 explicit body. The pragma makes it simpler to introduce such declarations.
2891 It incurs no overhead in compilation time or code size, because it is
2892 implemented as a single machine instruction.
2893
2894 @item
2895 General subprogram entities, to bind an Ada subprogram declaration to
2896 a compiler builtin by name with back-ends where such interfaces are
2897 available. A typical example is the set of ``__builtin'' functions
2898 exposed by the GCC back-end, as in the following example:
2899
2900 @smallexample @c ada
2901 function builtin_sqrt (F : Float) return Float;
2902 pragma Import (Intrinsic, builtin_sqrt, "__builtin_sqrtf");
2903 @end smallexample
2904
2905 Most of the GCC builtins are accessible this way, and as for other
2906 import conventions (e.g. C), it is the user's responsibility to ensure
2907 that the Ada subprogram profile matches the underlying builtin
2908 expectations.
2909 @end itemize
2910
2911 @noindent
2912
2913 @ifset unw
2914 @findex Stdcall
2915 @cindex Convention Stdcall
2916 @item Stdcall
2917 This is relevant only to Windows XP/2000/NT implementations of GNAT,
2918 and specifies that the @code{Stdcall} calling sequence will be used,
2919 as defined by the NT API. Nevertheless, to ease building
2920 cross-platform bindings this convention will be handled as a @code{C} calling
2921 convention on non-Windows platforms.
2922
2923 @findex DLL
2924 @cindex Convention DLL
2925 @item DLL
2926 This is equivalent to @code{Stdcall}.
2927
2928 @findex Win32
2929 @cindex Convention Win32
2930 @item Win32
2931 This is equivalent to @code{Stdcall}.
2932 @end ifset
2933
2934 @findex Stubbed
2935 @cindex Convention Stubbed
2936 @item Stubbed
2937 This is a special convention that indicates that the compiler
2938 should provide a stub body that raises @code{Program_Error}.
2939 @end table
2940
2941 @noindent
2942 GNAT additionally provides a useful pragma @code{Convention_Identifier}
2943 that can be used to parametrize conventions and allow additional synonyms
2944 to be specified. For example if you have legacy code in which the convention
2945 identifier Fortran77 was used for Fortran, you can use the configuration
2946 pragma:
2947
2948 @smallexample @c ada
2949 pragma Convention_Identifier (Fortran77, Fortran);
2950 @end smallexample
2951
2952 @noindent
2953 And from now on the identifier Fortran77 may be used as a convention
2954 identifier (for example in an @code{Import} pragma) with the same
2955 meaning as Fortran.
2956
2957 @ifclear vms
2958 @node Building Mixed Ada & C++ Programs
2959 @section Building Mixed Ada and C++ Programs
2960
2961 @noindent
2962 A programmer inexperienced with mixed-language development may find that
2963 building an application containing both Ada and C++ code can be a
2964 challenge. This section gives a few
2965 hints that should make this task easier. The first section addresses
2966 the differences between interfacing with C and interfacing with C++.
2967 The second section
2968 looks into the delicate problem of linking the complete application from
2969 its Ada and C++ parts. The last section gives some hints on how the GNAT
2970 run-time library can be adapted in order to allow inter-language dispatching
2971 with a new C++ compiler.
2972
2973 @menu
2974 * Interfacing to C++::
2975 * Linking a Mixed C++ & Ada Program::
2976 * A Simple Example::
2977 * Interfacing with C++ at the Class Level::
2978 @end menu
2979
2980 @node Interfacing to C++
2981 @subsection Interfacing to C++
2982
2983 @noindent
2984 GNAT supports interfacing with the G++ compiler (or any C++ compiler
2985 generating code that is compatible with the G++ Application Binary
2986 Interface ---see http://www.codesourcery.com/archives/cxx-abi).
2987
2988 @noindent
2989 Interfacing can be done at 3 levels: simple data, subprograms, and
2990 classes. In the first two cases, GNAT offers a specific @code{Convention
2991 C_Plus_Plus} (or @code{CPP}) that behaves exactly like @code{Convention C}.
2992 Usually, C++ mangles the names of subprograms, and currently, GNAT does
2993 not provide any help to solve the demangling problem. This problem can be
2994 addressed in two ways:
2995 @itemize @bullet
2996 @item
2997 by modifying the C++ code in order to force a C convention using
2998 the @code{extern "C"} syntax.
2999
3000 @item
3001 by figuring out the mangled name and use it as the Link_Name argument of
3002 the pragma import.
3003 @end itemize
3004
3005 @noindent
3006 Interfacing at the class level can be achieved by using the GNAT specific
3007 pragmas such as @code{CPP_Constructor}. @xref{Interfacing to C++,,,
3008 gnat_rm, GNAT Reference Manual}, for additional information.
3009
3010 @node Linking a Mixed C++ & Ada Program
3011 @subsection Linking a Mixed C++ & Ada Program
3012
3013 @noindent
3014 Usually the linker of the C++ development system must be used to link
3015 mixed applications because most C++ systems will resolve elaboration
3016 issues (such as calling constructors on global class instances)
3017 transparently during the link phase. GNAT has been adapted to ease the
3018 use of a foreign linker for the last phase. Three cases can be
3019 considered:
3020 @enumerate
3021
3022 @item
3023 Using GNAT and G++ (GNU C++ compiler) from the same GCC installation:
3024 The C++ linker can simply be called by using the C++ specific driver
3025 called @code{c++}. Note that this setup is not very common because it
3026 may involve recompiling the whole GCC tree from sources, which makes it
3027 harder to upgrade the compilation system for one language without
3028 destabilizing the other.
3029
3030 @smallexample
3031 $ c++ -c file1.C
3032 $ c++ -c file2.C
3033 $ gnatmake ada_unit -largs file1.o file2.o --LINK=c++
3034 @end smallexample
3035
3036 @item
3037 Using GNAT and G++ from two different GCC installations: If both
3038 compilers are on the @env{PATH}, the previous method may be used. It is
3039 important to note that environment variables such as
3040 @env{C_INCLUDE_PATH}, @env{GCC_EXEC_PREFIX}, @env{BINUTILS_ROOT}, and
3041 @env{GCC_ROOT} will affect both compilers
3042 at the same time and may make one of the two compilers operate
3043 improperly if set during invocation of the wrong compiler. It is also
3044 very important that the linker uses the proper @file{libgcc.a} GCC
3045 library -- that is, the one from the C++ compiler installation. The
3046 implicit link command as suggested in the @command{gnatmake} command
3047 from the former example can be replaced by an explicit link command with
3048 the full-verbosity option in order to verify which library is used:
3049 @smallexample
3050 $ gnatbind ada_unit
3051 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=c++
3052 @end smallexample
3053 If there is a problem due to interfering environment variables, it can
3054 be worked around by using an intermediate script. The following example
3055 shows the proper script to use when GNAT has not been installed at its
3056 default location and g++ has been installed at its default location:
3057
3058 @smallexample
3059 $ cat ./my_script
3060 #!/bin/sh
3061 unset BINUTILS_ROOT
3062 unset GCC_ROOT
3063 c++ $*
3064 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=./my_script
3065 @end smallexample
3066
3067 @item
3068 Using a non-GNU C++ compiler: The commands previously described can be
3069 used to insure that the C++ linker is used. Nonetheless, you need to add
3070 a few more parameters to the link command line, depending on the exception
3071 mechanism used.
3072
3073 If the @code{setjmp/longjmp} exception mechanism is used, only the paths
3074 to the libgcc libraries are required:
3075
3076 @smallexample
3077 $ cat ./my_script
3078 #!/bin/sh
3079 CC $* `gcc -print-file-name=libgcc.a` `gcc -print-file-name=libgcc_eh.a`
3080 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
3081 @end smallexample
3082
3083 Where CC is the name of the non-GNU C++ compiler.
3084
3085 If the @code{zero cost} exception mechanism is used, and the platform
3086 supports automatic registration of exception tables (e.g.@: Solaris or IRIX),
3087 paths to more objects are required:
3088
3089 @smallexample
3090 $ cat ./my_script
3091 #!/bin/sh
3092 CC `gcc -print-file-name=crtbegin.o` $* \
3093 `gcc -print-file-name=libgcc.a` `gcc -print-file-name=libgcc_eh.a` \
3094 `gcc -print-file-name=crtend.o`
3095 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
3096 @end smallexample
3097
3098 If the @code{zero cost} exception mechanism is used, and the platform
3099 doesn't support automatic registration of exception tables (e.g.@: HP-UX,
3100 Tru64 or AIX), the simple approach described above will not work and
3101 a pre-linking phase using GNAT will be necessary.
3102
3103 @end enumerate
3104
3105 @node A Simple Example
3106 @subsection A Simple Example
3107 @noindent
3108 The following example, provided as part of the GNAT examples, shows how
3109 to achieve procedural interfacing between Ada and C++ in both
3110 directions. The C++ class A has two methods. The first method is exported
3111 to Ada by the means of an extern C wrapper function. The second method
3112 calls an Ada subprogram. On the Ada side, The C++ calls are modelled by
3113 a limited record with a layout comparable to the C++ class. The Ada
3114 subprogram, in turn, calls the C++ method. So, starting from the C++
3115 main program, the process passes back and forth between the two
3116 languages.
3117
3118 @noindent
3119 Here are the compilation commands:
3120 @smallexample
3121 $ gnatmake -c simple_cpp_interface
3122 $ c++ -c cpp_main.C
3123 $ c++ -c ex7.C
3124 $ gnatbind -n simple_cpp_interface
3125 $ gnatlink simple_cpp_interface -o cpp_main --LINK=$(CPLUSPLUS)
3126 -lstdc++ ex7.o cpp_main.o
3127 @end smallexample
3128
3129 @noindent
3130 Here are the corresponding sources:
3131 @smallexample
3132
3133 //cpp_main.C
3134
3135 #include "ex7.h"
3136
3137 extern "C" @{
3138 void adainit (void);
3139 void adafinal (void);
3140 void method1 (A *t);
3141 @}
3142
3143 void method1 (A *t)
3144 @{
3145 t->method1 ();
3146 @}
3147
3148 int main ()
3149 @{
3150 A obj;
3151 adainit ();
3152 obj.method2 (3030);
3153 adafinal ();
3154 @}
3155
3156 //ex7.h
3157
3158 class Origin @{
3159 public:
3160 int o_value;
3161 @};
3162 class A : public Origin @{
3163 public:
3164 void method1 (void);
3165 void method2 (int v);
3166 A();
3167 int a_value;
3168 @};
3169
3170 //ex7.C
3171
3172 #include "ex7.h"
3173 #include <stdio.h>
3174
3175 extern "C" @{ void ada_method2 (A *t, int v);@}
3176
3177 void A::method1 (void)
3178 @{
3179 a_value = 2020;
3180 printf ("in A::method1, a_value = %d \n",a_value);
3181
3182 @}
3183
3184 void A::method2 (int v)
3185 @{
3186 ada_method2 (this, v);
3187 printf ("in A::method2, a_value = %d \n",a_value);
3188
3189 @}
3190
3191 A::A(void)
3192 @{
3193 a_value = 1010;
3194 printf ("in A::A, a_value = %d \n",a_value);
3195 @}
3196 @end smallexample
3197
3198 @smallexample @c ada
3199 -- Ada sources
3200 package body Simple_Cpp_Interface is
3201
3202 procedure Ada_Method2 (This : in out A; V : Integer) is
3203 begin
3204 Method1 (This);
3205 This.A_Value := V;
3206 end Ada_Method2;
3207
3208 end Simple_Cpp_Interface;
3209
3210 with System;
3211 package Simple_Cpp_Interface is
3212 type A is limited
3213 record
3214 Vptr : System.Address;
3215 O_Value : Integer;
3216 A_Value : Integer;
3217 end record;
3218 pragma Convention (C, A);
3219
3220 procedure Method1 (This : in out A);
3221 pragma Import (C, Method1);
3222
3223 procedure Ada_Method2 (This : in out A; V : Integer);
3224 pragma Export (C, Ada_Method2);
3225
3226 end Simple_Cpp_Interface;
3227 @end smallexample
3228
3229 @node Interfacing with C++ at the Class Level
3230 @subsection Interfacing with C++ at the Class Level
3231 @noindent
3232 In this section we demonstrate the GNAT features for interfacing with
3233 C++ by means of an example making use of Ada 2005 abstract interface
3234 types. This example consists of a classification of animals; classes
3235 have been used to model our main classification of animals, and
3236 interfaces provide support for the management of secondary
3237 classifications. We first demonstrate a case in which the types and
3238 constructors are defined on the C++ side and imported from the Ada
3239 side, and latter the reverse case.
3240
3241 The root of our derivation will be the @code{Animal} class, with a
3242 single private attribute (the @code{Age} of the animal) and two public
3243 primitives to set and get the value of this attribute.
3244
3245 @smallexample
3246 @b{class} Animal @{
3247 @b{public}:
3248 @b{virtual} void Set_Age (int New_Age);
3249 @b{virtual} int Age ();
3250 @b{private}:
3251 int Age_Count;
3252 @};
3253 @end smallexample
3254
3255 Abstract interface types are defined in C++ by means of classes with pure
3256 virtual functions and no data members. In our example we will use two
3257 interfaces that provide support for the common management of @code{Carnivore}
3258 and @code{Domestic} animals:
3259
3260 @smallexample
3261 @b{class} Carnivore @{
3262 @b{public}:
3263 @b{virtual} int Number_Of_Teeth () = 0;
3264 @};
3265
3266 @b{class} Domestic @{
3267 @b{public}:
3268 @b{virtual void} Set_Owner (char* Name) = 0;
3269 @};
3270 @end smallexample
3271
3272 Using these declarations, we can now say that a @code{Dog} is an animal that is
3273 both Carnivore and Domestic, that is:
3274
3275 @smallexample
3276 @b{class} Dog : Animal, Carnivore, Domestic @{
3277 @b{public}:
3278 @b{virtual} int Number_Of_Teeth ();
3279 @b{virtual} void Set_Owner (char* Name);
3280
3281 Dog(); // Constructor
3282 @b{private}:
3283 int Tooth_Count;
3284 char *Owner;
3285 @};
3286 @end smallexample
3287
3288 In the following examples we will assume that the previous declarations are
3289 located in a file named @code{animals.h}. The following package demonstrates
3290 how to import these C++ declarations from the Ada side:
3291
3292 @smallexample @c ada
3293 with Interfaces.C.Strings; use Interfaces.C.Strings;
3294 package Animals is
3295 type Carnivore is interface;
3296 pragma Convention (C_Plus_Plus, Carnivore);
3297 function Number_Of_Teeth (X : Carnivore)
3298 return Natural is abstract;
3299
3300 type Domestic is interface;
3301 pragma Convention (C_Plus_Plus, Set_Owner);
3302 procedure Set_Owner
3303 (X : in out Domestic;
3304 Name : Chars_Ptr) is abstract;
3305
3306 type Animal is tagged record
3307 Age : Natural := 0;
3308 end record;
3309 pragma Import (C_Plus_Plus, Animal);
3310
3311 procedure Set_Age (X : in out Animal; Age : Integer);
3312 pragma Import (C_Plus_Plus, Set_Age);
3313
3314 function Age (X : Animal) return Integer;
3315 pragma Import (C_Plus_Plus, Age);
3316
3317 type Dog is new Animal and Carnivore and Domestic with record
3318 Tooth_Count : Natural;
3319 Owner : String (1 .. 30);
3320 end record;
3321 pragma Import (C_Plus_Plus, Dog);
3322
3323 function Number_Of_Teeth (A : Dog) return Integer;
3324 pragma Import (C_Plus_Plus, Number_Of_Teeth);
3325
3326 procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
3327 pragma Import (C_Plus_Plus, Set_Owner);
3328
3329 function New_Dog return Dog'Class;
3330 pragma CPP_Constructor (New_Dog);
3331 pragma Import (CPP, New_Dog, "_ZN3DogC2Ev");
3332 end Animals;
3333 @end smallexample
3334
3335 Thanks to the compatibility between GNAT run-time structures and the C++ ABI,
3336 interfacing with these C++ classes is easy. The only requirement is that all
3337 the primitives and components must be declared exactly in the same order in
3338 the two languages.
3339
3340 Regarding the abstract interfaces, we must indicate to the GNAT compiler by
3341 means of a @code{pragma Convention (C_Plus_Plus)}, the convention used to pass
3342 the arguments to the called primitives will be the same as for C++. For the
3343 imported classes we use @code{pragma Import} with convention @code{C_Plus_Plus}
3344 to indicate that they have been defined on the C++ side; this is required
3345 because the dispatch table associated with these tagged types will be built
3346 in the C++ side and therefore will not contain the predefined Ada primitives
3347 which Ada would otherwise expect.
3348
3349 As the reader can see there is no need to indicate the C++ mangled names
3350 associated with each subprogram because it is assumed that all the calls to
3351 these primitives will be dispatching calls. The only exception is the
3352 constructor, which must be registered with the compiler by means of
3353 @code{pragma CPP_Constructor} and needs to provide its associated C++
3354 mangled name because the Ada compiler generates direct calls to it.
3355
3356 With the above packages we can now declare objects of type Dog on the Ada side
3357 and dispatch calls to the corresponding subprograms on the C++ side. We can
3358 also extend the tagged type Dog with further fields and primitives, and
3359 override some of its C++ primitives on the Ada side. For example, here we have
3360 a type derivation defined on the Ada side that inherits all the dispatching
3361 primitives of the ancestor from the C++ side.
3362
3363 @smallexample
3364 @b{with} Animals; @b{use} Animals;
3365 @b{package} Vaccinated_Animals @b{is}
3366 @b{type} Vaccinated_Dog @b{is new} Dog @b{with null record};
3367 @b{function} Vaccination_Expired (A : Vaccinated_Dog) @b{return} Boolean;
3368 @b{end} Vaccinated_Animals;
3369 @end smallexample
3370
3371 It is important to note that, because of the ABI compatibility, the programmer
3372 does not need to add any further information to indicate either the object
3373 layout or the dispatch table entry associated with each dispatching operation.
3374
3375 Now let us define all the types and constructors on the Ada side and export
3376 them to C++, using the same hierarchy of our previous example:
3377
3378 @smallexample @c ada
3379 with Interfaces.C.Strings;
3380 use Interfaces.C.Strings;
3381 package Animals is
3382 type Carnivore is interface;
3383 pragma Convention (C_Plus_Plus, Carnivore);
3384 function Number_Of_Teeth (X : Carnivore)
3385 return Natural is abstract;
3386
3387 type Domestic is interface;
3388 pragma Convention (C_Plus_Plus, Set_Owner);
3389 procedure Set_Owner
3390 (X : in out Domestic;
3391 Name : Chars_Ptr) is abstract;
3392
3393 type Animal is tagged record
3394 Age : Natural := 0;
3395 end record;
3396 pragma Convention (C_Plus_Plus, Animal);
3397
3398 procedure Set_Age (X : in out Animal; Age : Integer);
3399 pragma Export (C_Plus_Plus, Set_Age);
3400
3401 function Age (X : Animal) return Integer;
3402 pragma Export (C_Plus_Plus, Age);
3403
3404 type Dog is new Animal and Carnivore and Domestic with record
3405 Tooth_Count : Natural;
3406 Owner : String (1 .. 30);
3407 end record;
3408 pragma Convention (C_Plus_Plus, Dog);
3409
3410 function Number_Of_Teeth (A : Dog) return Integer;
3411 pragma Export (C_Plus_Plus, Number_Of_Teeth);
3412
3413 procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
3414 pragma Export (C_Plus_Plus, Set_Owner);
3415
3416 function New_Dog return Dog'Class;
3417 pragma Export (C_Plus_Plus, New_Dog);
3418 end Animals;
3419 @end smallexample
3420
3421 Compared with our previous example the only difference is the use of
3422 @code{pragma Export} to indicate to the GNAT compiler that the primitives will
3423 be available to C++. Thanks to the ABI compatibility, on the C++ side there is
3424 nothing else to be done; as explained above, the only requirement is that all
3425 the primitives and components are declared in exactly the same order.
3426
3427 For completeness, let us see a brief C++ main program that uses the
3428 declarations available in @code{animals.h} (presented in our first example) to
3429 import and use the declarations from the Ada side, properly initializing and
3430 finalizing the Ada run-time system along the way:
3431
3432 @smallexample
3433 @b{#include} "animals.h"
3434 @b{#include} <iostream>
3435 @b{using namespace} std;
3436
3437 void Check_Carnivore (Carnivore *obj) @{@dots{}@}
3438 void Check_Domestic (Domestic *obj) @{@dots{}@}
3439 void Check_Animal (Animal *obj) @{@dots{}@}
3440 void Check_Dog (Dog *obj) @{@dots{}@}
3441
3442 @b{extern} "C" @{
3443 void adainit (void);
3444 void adafinal (void);
3445 Dog* new_dog ();
3446 @}
3447
3448 void test ()
3449 @{
3450 Dog *obj = new_dog(); // Ada constructor
3451 Check_Carnivore (obj); // Check secondary DT
3452 Check_Domestic (obj); // Check secondary DT
3453 Check_Animal (obj); // Check primary DT
3454 Check_Dog (obj); // Check primary DT
3455 @}
3456
3457 int main ()
3458 @{
3459 adainit (); test(); adafinal ();
3460 return 0;
3461 @}
3462 @end smallexample
3463
3464 @node Comparison between GNAT and C/C++ Compilation Models
3465 @section Comparison between GNAT and C/C++ Compilation Models
3466
3467 @noindent
3468 The GNAT model of compilation is close to the C and C++ models. You can
3469 think of Ada specs as corresponding to header files in C. As in C, you
3470 don't need to compile specs; they are compiled when they are used. The
3471 Ada @code{with} is similar in effect to the @code{#include} of a C
3472 header.
3473
3474 One notable difference is that, in Ada, you may compile specs separately
3475 to check them for semantic and syntactic accuracy. This is not always
3476 possible with C headers because they are fragments of programs that have
3477 less specific syntactic or semantic rules.
3478
3479 The other major difference is the requirement for running the binder,
3480 which performs two important functions. First, it checks for
3481 consistency. In C or C++, the only defense against assembling
3482 inconsistent programs lies outside the compiler, in a makefile, for
3483 example. The binder satisfies the Ada requirement that it be impossible
3484 to construct an inconsistent program when the compiler is used in normal
3485 mode.
3486
3487 @cindex Elaboration order control
3488 The other important function of the binder is to deal with elaboration
3489 issues. There are also elaboration issues in C++ that are handled
3490 automatically. This automatic handling has the advantage of being
3491 simpler to use, but the C++ programmer has no control over elaboration.
3492 Where @code{gnatbind} might complain there was no valid order of
3493 elaboration, a C++ compiler would simply construct a program that
3494 malfunctioned at run time.
3495 @end ifclear
3496
3497 @node Comparison between GNAT and Conventional Ada Library Models
3498 @section Comparison between GNAT and Conventional Ada Library Models
3499
3500 @noindent
3501 This section is intended for Ada programmers who have
3502 used an Ada compiler implementing the traditional Ada library
3503 model, as described in the Ada Reference Manual.
3504
3505 @cindex GNAT library
3506 In GNAT, there is no ``library'' in the normal sense. Instead, the set of
3507 source files themselves acts as the library. Compiling Ada programs does
3508 not generate any centralized information, but rather an object file and
3509 a ALI file, which are of interest only to the binder and linker.
3510 In a traditional system, the compiler reads information not only from
3511 the source file being compiled, but also from the centralized library.
3512 This means that the effect of a compilation depends on what has been
3513 previously compiled. In particular:
3514
3515 @itemize @bullet
3516 @item
3517 When a unit is @code{with}'ed, the unit seen by the compiler corresponds
3518 to the version of the unit most recently compiled into the library.
3519
3520 @item
3521 Inlining is effective only if the necessary body has already been
3522 compiled into the library.
3523
3524 @item
3525 Compiling a unit may obsolete other units in the library.
3526 @end itemize
3527
3528 @noindent
3529 In GNAT, compiling one unit never affects the compilation of any other
3530 units because the compiler reads only source files. Only changes to source
3531 files can affect the results of a compilation. In particular:
3532
3533 @itemize @bullet
3534 @item
3535 When a unit is @code{with}'ed, the unit seen by the compiler corresponds
3536 to the source version of the unit that is currently accessible to the
3537 compiler.
3538
3539 @item
3540 @cindex Inlining
3541 Inlining requires the appropriate source files for the package or
3542 subprogram bodies to be available to the compiler. Inlining is always
3543 effective, independent of the order in which units are complied.
3544
3545 @item
3546 Compiling a unit never affects any other compilations. The editing of
3547 sources may cause previous compilations to be out of date if they
3548 depended on the source file being modified.
3549 @end itemize
3550
3551 @noindent
3552 The most important result of these differences is that order of compilation
3553 is never significant in GNAT. There is no situation in which one is
3554 required to do one compilation before another. What shows up as order of
3555 compilation requirements in the traditional Ada library becomes, in
3556 GNAT, simple source dependencies; in other words, there is only a set
3557 of rules saying what source files must be present when a file is
3558 compiled.
3559
3560 @ifset vms
3561 @node Placement of temporary files
3562 @section Placement of temporary files
3563 @cindex Temporary files (user control over placement)
3564
3565 @noindent
3566 GNAT creates temporary files in the directory designated by the environment
3567 variable @env{TMPDIR}.
3568 (See the HP @emph{C RTL Reference Manual} on the function @code{getenv()}
3569 for detailed information on how environment variables are resolved.
3570 For most users the easiest way to make use of this feature is to simply
3571 define @env{TMPDIR} as a job level logical name).
3572 For example, if you wish to use a Ramdisk (assuming DECRAM is installed)
3573 for compiler temporary files, then you can include something like the
3574 following command in your @file{LOGIN.COM} file:
3575
3576 @smallexample
3577 $ define/job TMPDIR "/disk$scratchram/000000/temp/"
3578 @end smallexample
3579
3580 @noindent
3581 If @env{TMPDIR} is not defined, then GNAT uses the directory designated by
3582 @env{TMP}; if @env{TMP} is not defined, then GNAT uses the directory
3583 designated by @env{TEMP}.
3584 If none of these environment variables are defined then GNAT uses the
3585 directory designated by the logical name @code{SYS$SCRATCH:}
3586 (by default the user's home directory). If all else fails
3587 GNAT uses the current directory for temporary files.
3588 @end ifset
3589
3590 @c *************************
3591 @node Compiling Using gcc
3592 @chapter Compiling Using @command{gcc}
3593
3594 @noindent
3595 This chapter discusses how to compile Ada programs using the @command{gcc}
3596 command. It also describes the set of switches
3597 that can be used to control the behavior of the compiler.
3598 @menu
3599 * Compiling Programs::
3600 * Switches for gcc::
3601 * Search Paths and the Run-Time Library (RTL)::
3602 * Order of Compilation Issues::
3603 * Examples::
3604 @end menu
3605
3606 @node Compiling Programs
3607 @section Compiling Programs
3608
3609 @noindent
3610 The first step in creating an executable program is to compile the units
3611 of the program using the @command{gcc} command. You must compile the
3612 following files:
3613
3614 @itemize @bullet
3615 @item
3616 the body file (@file{.adb}) for a library level subprogram or generic
3617 subprogram
3618
3619 @item
3620 the spec file (@file{.ads}) for a library level package or generic
3621 package that has no body
3622
3623 @item
3624 the body file (@file{.adb}) for a library level package
3625 or generic package that has a body
3626
3627 @end itemize
3628
3629 @noindent
3630 You need @emph{not} compile the following files
3631
3632 @itemize @bullet
3633
3634 @item
3635 the spec of a library unit which has a body
3636
3637 @item
3638 subunits
3639 @end itemize
3640
3641 @noindent
3642 because they are compiled as part of compiling related units. GNAT
3643 package specs
3644 when the corresponding body is compiled, and subunits when the parent is
3645 compiled.
3646
3647 @cindex cannot generate code
3648 If you attempt to compile any of these files, you will get one of the
3649 following error messages (where @var{fff} is the name of the file you compiled):
3650
3651 @smallexample
3652 cannot generate code for file @var{fff} (package spec)
3653 to check package spec, use -gnatc
3654
3655 cannot generate code for file @var{fff} (missing subunits)
3656 to check parent unit, use -gnatc
3657
3658 cannot generate code for file @var{fff} (subprogram spec)
3659 to check subprogram spec, use -gnatc
3660
3661 cannot generate code for file @var{fff} (subunit)
3662 to check subunit, use -gnatc
3663 @end smallexample
3664
3665 @noindent
3666 As indicated by the above error messages, if you want to submit
3667 one of these files to the compiler to check for correct semantics
3668 without generating code, then use the @option{-gnatc} switch.
3669
3670 The basic command for compiling a file containing an Ada unit is
3671
3672 @smallexample
3673 $ gcc -c @ovar{switches} @file{file name}
3674 @end smallexample
3675
3676 @noindent
3677 where @var{file name} is the name of the Ada file (usually
3678 having an extension
3679 @file{.ads} for a spec or @file{.adb} for a body).
3680 @ifclear vms
3681 You specify the
3682 @option{-c} switch to tell @command{gcc} to compile, but not link, the file.
3683 @end ifclear
3684 The result of a successful compilation is an object file, which has the
3685 same name as the source file but an extension of @file{.o} and an Ada
3686 Library Information (ALI) file, which also has the same name as the
3687 source file, but with @file{.ali} as the extension. GNAT creates these
3688 two output files in the current directory, but you may specify a source
3689 file in any directory using an absolute or relative path specification
3690 containing the directory information.
3691
3692 @findex gnat1
3693 @command{gcc} is actually a driver program that looks at the extensions of
3694 the file arguments and loads the appropriate compiler. For example, the
3695 GNU C compiler is @file{cc1}, and the Ada compiler is @file{gnat1}.
3696 These programs are in directories known to the driver program (in some
3697 configurations via environment variables you set), but need not be in
3698 your path. The @command{gcc} driver also calls the assembler and any other
3699 utilities needed to complete the generation of the required object
3700 files.
3701
3702 It is possible to supply several file names on the same @command{gcc}
3703 command. This causes @command{gcc} to call the appropriate compiler for
3704 each file. For example, the following command lists three separate
3705 files to be compiled:
3706
3707 @smallexample
3708 $ gcc -c x.adb y.adb z.c
3709 @end smallexample
3710
3711 @noindent
3712 calls @code{gnat1} (the Ada compiler) twice to compile @file{x.adb} and
3713 @file{y.adb}, and @code{cc1} (the C compiler) once to compile @file{z.c}.
3714 The compiler generates three object files @file{x.o}, @file{y.o} and
3715 @file{z.o} and the two ALI files @file{x.ali} and @file{y.ali} from the
3716 Ada compilations. Any switches apply to all the files ^listed,^listed.^
3717 @ifclear vms
3718 except for
3719 @option{-gnat@var{x}} switches, which apply only to Ada compilations.
3720 @end ifclear
3721
3722 @node Switches for gcc
3723 @section Switches for @command{gcc}
3724
3725 @noindent
3726 The @command{gcc} command accepts switches that control the
3727 compilation process. These switches are fully described in this section.
3728 First we briefly list all the switches, in alphabetical order, then we
3729 describe the switches in more detail in functionally grouped sections.
3730
3731 More switches exist for GCC than those documented here, especially
3732 for specific targets. However, their use is not recommended as
3733 they may change code generation in ways that are incompatible with
3734 the Ada run-time library, or can cause inconsistencies between
3735 compilation units.
3736
3737 @menu
3738 * Output and Error Message Control::
3739 * Warning Message Control::
3740 * Debugging and Assertion Control::
3741 * Validity Checking::
3742 * Style Checking::
3743 * Run-Time Checks::
3744 * Using gcc for Syntax Checking::
3745 * Using gcc for Semantic Checking::
3746 * Compiling Different Versions of Ada::
3747 * Character Set Control::
3748 * File Naming Control::
3749 * Subprogram Inlining Control::
3750 * Auxiliary Output Control::
3751 * Debugging Control::
3752 * Exception Handling Control::
3753 * Units to Sources Mapping Files::
3754 * Integrated Preprocessing::
3755 * Code Generation Control::
3756 @ifset vms
3757 * Return Codes::
3758 @end ifset
3759 @end menu
3760
3761 @table @option
3762 @c !sort!
3763 @ifclear vms
3764 @cindex @option{-b} (@command{gcc})
3765 @item -b @var{target}
3766 Compile your program to run on @var{target}, which is the name of a
3767 system configuration. You must have a GNAT cross-compiler built if
3768 @var{target} is not the same as your host system.
3769
3770 @item -B@var{dir}
3771 @cindex @option{-B} (@command{gcc})
3772 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
3773 from @var{dir} instead of the default location. Only use this switch
3774 when multiple versions of the GNAT compiler are available.
3775 @xref{Directory Options,, Options for Directory Search, gcc, Using the
3776 GNU Compiler Collection (GCC)}, for further details. You would normally
3777 use the @option{-b} or @option{-V} switch instead.
3778
3779 @item -c
3780 @cindex @option{-c} (@command{gcc})
3781 Compile. Always use this switch when compiling Ada programs.
3782
3783 Note: for some other languages when using @command{gcc}, notably in
3784 the case of C and C++, it is possible to use
3785 use @command{gcc} without a @option{-c} switch to
3786 compile and link in one step. In the case of GNAT, you
3787 cannot use this approach, because the binder must be run
3788 and @command{gcc} cannot be used to run the GNAT binder.
3789 @end ifclear
3790
3791 @item -fno-inline
3792 @cindex @option{-fno-inline} (@command{gcc})
3793 Suppresses all back-end inlining, even if other optimization or inlining
3794 switches are set.
3795 This includes suppression of inlining that results
3796 from the use of the pragma @code{Inline_Always}.
3797 Any occurrences of pragma @code{Inline} or @code{Inline_Always}
3798 are ignored, and @option{-gnatn} and @option{-gnatN} have no
3799 effect if this switch is present.
3800
3801 @item -fno-inline-functions
3802 @cindex @option{-fno-inline-functions} (@command{gcc})
3803 Suppresses automatic inlining of small subprograms, which is enabled
3804 if @option{-O3} is used.
3805
3806 @item -fno-inline-functions-called-once
3807 @cindex @option{-fno-inline-functions-called-once} (@command{gcc})
3808 Suppresses inlining of subprograms local to the unit and called once
3809 from within it, which is enabled if @option{-O1} is used.
3810
3811 @item -fno-strict-aliasing
3812 @cindex @option{-fno-strict-aliasing} (@command{gcc})
3813 Causes the compiler to avoid assumptions regarding non-aliasing
3814 of objects of different types. See
3815 @ref{Optimization and Strict Aliasing} for details.
3816
3817 @item -fstack-check
3818 @cindex @option{-fstack-check} (@command{gcc})
3819 Activates stack checking.
3820 See @ref{Stack Overflow Checking} for details.
3821
3822 @item -fstack-usage
3823 @cindex @option{-fstack-usage} (@command{gcc})
3824 Makes the compiler output stack usage information for the program, on a
3825 per-function basis. See @ref{Static Stack Usage Analysis} for details.
3826
3827 @item -fcallgraph-info@r{[}=su@r{]}
3828 @cindex @option{-fcallgraph-info} (@command{gcc})
3829 Makes the compiler output callgraph information for the program, on a
3830 per-file basis. The information is generated in the VCG format. It can
3831 be decorated with stack-usage per-node information.
3832
3833 @item ^-g^/DEBUG^
3834 @cindex @option{^-g^/DEBUG^} (@command{gcc})
3835 Generate debugging information. This information is stored in the object
3836 file and copied from there to the final executable file by the linker,
3837 where it can be read by the debugger. You must use the
3838 @option{^-g^/DEBUG^} switch if you plan on using the debugger.
3839
3840 @item -gnat83
3841 @cindex @option{-gnat83} (@command{gcc})
3842 Enforce Ada 83 restrictions.
3843
3844 @item -gnat95
3845 @cindex @option{-gnat95} (@command{gcc})
3846 Enforce Ada 95 restrictions.
3847
3848 @item -gnat05
3849 @cindex @option{-gnat05} (@command{gcc})
3850 Allow full Ada 2005 features.
3851
3852 @item -gnata
3853 @cindex @option{-gnata} (@command{gcc})
3854 Assertions enabled. @code{Pragma Assert} and @code{pragma Debug} to be
3855 activated. Note that these pragmas can also be controlled using the
3856 configuration pragmas @code{Assertion_Policy} and @code{Debug_Policy}.
3857 It also activates pragmas @code{Check}, @code{Precondition}, and
3858 @code{Postcondition}. Note that these pragmas can also be controlled
3859 using the configuration pragma @code{Check_Policy}.
3860
3861 @item -gnatA
3862 @cindex @option{-gnatA} (@command{gcc})
3863 Avoid processing @file{gnat.adc}. If a @file{gnat.adc} file is present,
3864 it will be ignored.
3865
3866 @item -gnatb
3867 @cindex @option{-gnatb} (@command{gcc})
3868 Generate brief messages to @file{stderr} even if verbose mode set.
3869
3870 @item -gnatc
3871 @cindex @option{-gnatc} (@command{gcc})
3872 Check syntax and semantics only (no code generation attempted).
3873
3874 @item -gnatd
3875 @cindex @option{-gnatd} (@command{gcc})
3876 Specify debug options for the compiler. The string of characters after
3877 the @option{-gnatd} specify the specific debug options. The possible
3878 characters are 0-9, a-z, A-Z, optionally preceded by a dot. See
3879 compiler source file @file{debug.adb} for details of the implemented
3880 debug options. Certain debug options are relevant to applications
3881 programmers, and these are documented at appropriate points in this
3882 users guide.
3883
3884 @item -gnatD
3885 @cindex @option{-gnatD} (@command{gcc})
3886 Create expanded source files for source level debugging. This switch
3887 also suppress generation of cross-reference information
3888 (see @option{-gnatx}).
3889
3890 @item -gnatec=@var{path}
3891 @cindex @option{-gnatec} (@command{gcc})
3892 Specify a configuration pragma file
3893 @ifclear vms
3894 (the equal sign is optional)
3895 @end ifclear
3896 (@pxref{The Configuration Pragmas Files}).
3897
3898 @item ^-gnateD^/DATA_PREPROCESSING=^symbol@r{[}=@var{value}@r{]}
3899 @cindex @option{-gnateD} (@command{gcc})
3900 Defines a symbol, associated with @var{value}, for preprocessing.
3901 (@pxref{Integrated Preprocessing}).
3902
3903 @item -gnatef
3904 @cindex @option{-gnatef} (@command{gcc})
3905 Display full source path name in brief error messages.
3906
3907 @item -gnateG
3908 @cindex @option{-gnateG} (@command{gcc})
3909 Save result of preprocessing in a text file.
3910
3911 @item -gnatem=@var{path}
3912 @cindex @option{-gnatem} (@command{gcc})
3913 Specify a mapping file
3914 @ifclear vms
3915 (the equal sign is optional)
3916 @end ifclear
3917 (@pxref{Units to Sources Mapping Files}).
3918
3919 @item -gnatep=@var{file}
3920 @cindex @option{-gnatep} (@command{gcc})
3921 Specify a preprocessing data file
3922 @ifclear vms
3923 (the equal sign is optional)
3924 @end ifclear
3925 (@pxref{Integrated Preprocessing}).
3926
3927 @item -gnatE
3928 @cindex @option{-gnatE} (@command{gcc})
3929 Full dynamic elaboration checks.
3930
3931 @item -gnatf
3932 @cindex @option{-gnatf} (@command{gcc})
3933 Full errors. Multiple errors per line, all undefined references, do not
3934 attempt to suppress cascaded errors.
3935
3936 @item -gnatF
3937 @cindex @option{-gnatF} (@command{gcc})
3938 Externals names are folded to all uppercase.
3939
3940 @item ^-gnatg^/GNAT_INTERNAL^
3941 @cindex @option{^-gnatg^/GNAT_INTERNAL^} (@command{gcc})
3942 Internal GNAT implementation mode. This should not be used for
3943 applications programs, it is intended only for use by the compiler
3944 and its run-time library. For documentation, see the GNAT sources.
3945 Note that @option{^-gnatg^/GNAT_INTERNAL^} implies
3946 @option{^-gnatwae^/WARNINGS=ALL,ERRORS^} and
3947 @option{^-gnatyg^/STYLE_CHECKS=GNAT^}
3948 so that all standard warnings and all standard style options are turned on.
3949 All warnings and style error messages are treated as errors.
3950
3951 @item -gnatG
3952 @cindex @option{-gnatG} (@command{gcc})
3953 List generated expanded code in source form.
3954
3955 @item ^-gnath^/HELP^
3956 @cindex @option{^-gnath^/HELP^} (@command{gcc})
3957 Output usage information. The output is written to @file{stdout}.
3958
3959 @item ^-gnati^/IDENTIFIER_CHARACTER_SET=^@var{c}
3960 @cindex @option{^-gnati^/IDENTIFIER_CHARACTER_SET^} (@command{gcc})
3961 Identifier character set
3962 @ifclear vms
3963 (@var{c}=1/2/3/4/8/9/p/f/n/w).
3964 @end ifclear
3965 For details of the possible selections for @var{c},
3966 see @ref{Character Set Control}.
3967
3968 @item ^-gnatI^/IGNORE_REP_CLAUSES^
3969 @cindex @option{^-gnatI^IGNORE_REP_CLAUSES^} (@command{gcc})
3970 Ignore representation clauses. When this switch is used, all
3971 representation clauses are treated as comments. This is useful
3972 when initially porting code where you want to ignore rep clause
3973 problems, and also for compiling foreign code (particularly
3974 for use with ASIS).
3975
3976 @item -gnatjnn
3977 @cindex @option{-gnatjnn} (@command{gcc})
3978 Reformat error messages to fit on nn character lines
3979
3980 @item -gnatk=@var{n}
3981 @cindex @option{-gnatk} (@command{gcc})
3982 Limit file names to @var{n} (1-999) characters ^(@code{k} = krunch)^^.
3983
3984 @item -gnatl
3985 @cindex @option{-gnatl} (@command{gcc})
3986 Output full source listing with embedded error messages.
3987
3988 @item -gnatL
3989 @cindex @option{-gnatL} (@command{gcc})
3990 Used in conjunction with -gnatG or -gnatD to intersperse original
3991 source lines (as comment lines with line numbers) in the expanded
3992 source output.
3993
3994 @item -gnatm=@var{n}
3995 @cindex @option{-gnatm} (@command{gcc})
3996 Limit number of detected error or warning messages to @var{n}
3997 where @var{n} is in the range 1..999_999. The default setting if
3998 no switch is given is 9999. Compilation is terminated if this
3999 limit is exceeded. The equal sign here is optional.
4000
4001 @item -gnatn
4002 @cindex @option{-gnatn} (@command{gcc})
4003 Activate inlining for subprograms for which
4004 pragma @code{inline} is specified. This inlining is performed
4005 by the GCC back-end.
4006
4007 @item -gnatN
4008 @cindex @option{-gnatN} (@command{gcc})
4009 Activate front end inlining for subprograms for which
4010 pragma @code{Inline} is specified. This inlining is performed
4011 by the front end and will be visible in the
4012 @option{-gnatG} output.
4013 In some cases, this has proved more effective than the back end
4014 inlining resulting from the use of
4015 @option{-gnatn}.
4016 Note that
4017 @option{-gnatN} automatically implies
4018 @option{-gnatn} so it is not necessary
4019 to specify both options. There are a few cases that the back-end inlining
4020 catches that cannot be dealt with in the front-end.
4021
4022 @item -gnato
4023 @cindex @option{-gnato} (@command{gcc})
4024 Enable numeric overflow checking (which is not normally enabled by
4025 default). Note that division by zero is a separate check that is not
4026 controlled by this switch (division by zero checking is on by default).
4027
4028 @item -gnatp
4029 @cindex @option{-gnatp} (@command{gcc})
4030 Suppress all checks. See @ref{Run-Time Checks} for details.
4031
4032 @item -gnatP
4033 @cindex @option{-gnatP} (@command{gcc})
4034 Enable polling. This is required on some systems (notably Windows NT) to
4035 obtain asynchronous abort and asynchronous transfer of control capability.
4036 @xref{Pragma Polling,,, gnat_rm, GNAT Reference Manual}, for full
4037 details.
4038
4039 @item -gnatq
4040 @cindex @option{-gnatq} (@command{gcc})
4041 Don't quit. Try semantics, even if parse errors.
4042
4043 @item -gnatQ
4044 @cindex @option{-gnatQ} (@command{gcc})
4045 Don't quit. Generate @file{ALI} and tree files even if illegalities.
4046
4047 @item -gnatr
4048 @cindex @option{-gnatr} (@command{gcc})
4049 Treat pragma Restrictions as Restriction_Warnings.
4050
4051 @item ^-gnatR@r{[}0@r{/}1@r{/}2@r{/}3@r{[}s@r{]]}^/REPRESENTATION_INFO^
4052 @cindex @option{-gnatR} (@command{gcc})
4053 Output representation information for declared types and objects.
4054
4055 @item -gnats
4056 @cindex @option{-gnats} (@command{gcc})
4057 Syntax check only.
4058
4059 @item -gnatS
4060 @cindex @option{-gnatS} (@command{gcc})
4061 Print package Standard.
4062
4063 @item -gnatt
4064 @cindex @option{-gnatt} (@command{gcc})
4065 Generate tree output file.
4066
4067 @item ^-gnatT^/TABLE_MULTIPLIER=^@var{nnn}
4068 @cindex @option{^-gnatT^/TABLE_MULTIPLIER^} (@command{gcc})
4069 All compiler tables start at @var{nnn} times usual starting size.
4070
4071 @item -gnatu
4072 @cindex @option{-gnatu} (@command{gcc})
4073 List units for this compilation.
4074
4075 @item -gnatU
4076 @cindex @option{-gnatU} (@command{gcc})
4077 Tag all error messages with the unique string ``error:''
4078
4079 @item -gnatv
4080 @cindex @option{-gnatv} (@command{gcc})
4081 Verbose mode. Full error output with source lines to @file{stdout}.
4082
4083 @item -gnatV
4084 @cindex @option{-gnatV} (@command{gcc})
4085 Control level of validity checking. See separate section describing
4086 this feature.
4087
4088 @item ^-gnatw@var{xxx}^/WARNINGS=(@var{option}@r{[},@dots{}@r{]})^
4089 @cindex @option{^-gnatw^/WARNINGS^} (@command{gcc})
4090 Warning mode where
4091 ^@var{xxx} is a string of option letters that^the list of options^ denotes
4092 the exact warnings that
4093 are enabled or disabled (@pxref{Warning Message Control}).
4094
4095 @item ^-gnatW^/WIDE_CHARACTER_ENCODING=^@var{e}
4096 @cindex @option{^-gnatW^/WIDE_CHARACTER_ENCODING^} (@command{gcc})
4097 Wide character encoding method
4098 @ifclear vms
4099 (@var{e}=n/h/u/s/e/8).
4100 @end ifclear
4101 @ifset vms
4102 (@var{e}=@code{BRACKETS, NONE, HEX, UPPER, SHIFT_JIS, EUC, UTF8})
4103 @end ifset
4104
4105 @item -gnatx
4106 @cindex @option{-gnatx} (@command{gcc})
4107 Suppress generation of cross-reference information.
4108
4109 @item ^-gnaty^/STYLE_CHECKS=(option,option@dots{})^
4110 @cindex @option{^-gnaty^/STYLE_CHECKS^} (@command{gcc})
4111 Enable built-in style checks (@pxref{Style Checking}).
4112
4113 @item ^-gnatz^/DISTRIBUTION_STUBS=^@var{m}
4114 @cindex @option{^-gnatz^/DISTRIBUTION_STUBS^} (@command{gcc})
4115 Distribution stub generation and compilation
4116 @ifclear vms
4117 (@var{m}=r/c for receiver/caller stubs).
4118 @end ifclear
4119 @ifset vms
4120 (@var{m}=@code{RECEIVER} or @code{CALLER} to specify the type of stubs
4121 to be generated and compiled).
4122 @end ifset
4123
4124 @item ^-I^/SEARCH=^@var{dir}
4125 @cindex @option{^-I^/SEARCH^} (@command{gcc})
4126 @cindex RTL
4127 Direct GNAT to search the @var{dir} directory for source files needed by
4128 the current compilation
4129 (@pxref{Search Paths and the Run-Time Library (RTL)}).
4130
4131 @item ^-I-^/NOCURRENT_DIRECTORY^
4132 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gcc})
4133 @cindex RTL
4134 Except for the source file named in the command line, do not look for source
4135 files in the directory containing the source file named in the command line
4136 (@pxref{Search Paths and the Run-Time Library (RTL)}).
4137
4138 @ifclear vms
4139 @item -mbig-switch
4140 @cindex @option{-mbig-switch} (@command{gcc})
4141 @cindex @code{case} statement (effect of @option{-mbig-switch} option)
4142 This standard gcc switch causes the compiler to use larger offsets in its
4143 jump table representation for @code{case} statements.
4144 This may result in less efficient code, but is sometimes necessary
4145 (for example on HP-UX targets)
4146 @cindex HP-UX and @option{-mbig-switch} option
4147 in order to compile large and/or nested @code{case} statements.
4148
4149 @item -o @var{file}
4150 @cindex @option{-o} (@command{gcc})
4151 This switch is used in @command{gcc} to redirect the generated object file
4152 and its associated ALI file. Beware of this switch with GNAT, because it may
4153 cause the object file and ALI file to have different names which in turn
4154 may confuse the binder and the linker.
4155 @end ifclear
4156
4157 @item -nostdinc
4158 @cindex @option{-nostdinc} (@command{gcc})
4159 Inhibit the search of the default location for the GNAT Run Time
4160 Library (RTL) source files.
4161
4162 @item -nostdlib
4163 @cindex @option{-nostdlib} (@command{gcc})
4164 Inhibit the search of the default location for the GNAT Run Time
4165 Library (RTL) ALI files.
4166
4167 @ifclear vms
4168 @item -O@ovar{n}
4169 @cindex @option{-O} (@command{gcc})
4170 @var{n} controls the optimization level.
4171
4172 @table @asis
4173 @item n = 0
4174 No optimization, the default setting if no @option{-O} appears
4175
4176 @item n = 1
4177 Normal optimization, the default if you specify @option{-O} without
4178 an operand. A good compromise between code quality and compilation
4179 time.
4180
4181 @item n = 2
4182 Extensive optimization, may improve execution time, possibly at the cost of
4183 substantially increased compilation time.
4184
4185 @item n = 3
4186 Same as @option{-O2}, and also includes inline expansion for small subprograms
4187 in the same unit.
4188
4189 @item n = s
4190 Optimize space usage
4191 @end table
4192
4193 @noindent
4194 See also @ref{Optimization Levels}.
4195 @end ifclear
4196
4197 @ifset vms
4198 @item /NOOPTIMIZE
4199 @cindex @option{/NOOPTIMIZE} (@code{GNAT COMPILE})
4200 Equivalent to @option{/OPTIMIZE=NONE}.
4201 This is the default behavior in the absence of an @option{/OPTIMIZE}
4202 qualifier.
4203
4204 @item /OPTIMIZE@r{[}=(keyword@r{[},@dots{}@r{]})@r{]}
4205 @cindex @option{/OPTIMIZE} (@code{GNAT COMPILE})
4206 Selects the level of optimization for your program. The supported
4207 keywords are as follows:
4208 @table @code
4209 @item ALL
4210 Perform most optimizations, including those that
4211 are expensive.
4212 This is the default if the @option{/OPTIMIZE} qualifier is supplied
4213 without keyword options.
4214
4215 @item NONE
4216 Do not do any optimizations. Same as @code{/NOOPTIMIZE}.
4217
4218 @item SOME
4219 Perform some optimizations, but omit ones that are costly.
4220
4221 @item DEVELOPMENT
4222 Same as @code{SOME}.
4223
4224 @item INLINING
4225 Full optimization as in @option{/OPTIMIZE=ALL}, and also attempts
4226 automatic inlining of small subprograms within a unit
4227
4228 @item UNROLL_LOOPS
4229 Try to unroll loops. This keyword may be specified together with
4230 any keyword above other than @code{NONE}. Loop unrolling
4231 usually, but not always, improves the performance of programs.
4232
4233 @item SPACE
4234 Optimize space usage
4235 @end table
4236
4237 @noindent
4238 See also @ref{Optimization Levels}.
4239 @end ifset
4240
4241 @ifclear vms
4242 @item -pass-exit-codes
4243 @cindex @option{-pass-exit-codes} (@command{gcc})
4244 Catch exit codes from the compiler and use the most meaningful as
4245 exit status.
4246 @end ifclear
4247
4248 @item --RTS=@var{rts-path}
4249 @cindex @option{--RTS} (@command{gcc})
4250 Specifies the default location of the runtime library. Same meaning as the
4251 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
4252
4253 @item ^-S^/ASM^
4254 @cindex @option{^-S^/ASM^} (@command{gcc})
4255 ^Used in place of @option{-c} to^Used to^
4256 cause the assembler source file to be
4257 generated, using @file{^.s^.S^} as the extension,
4258 instead of the object file.
4259 This may be useful if you need to examine the generated assembly code.
4260
4261 @item ^-fverbose-asm^/VERBOSE_ASM^
4262 @cindex @option{^-fverbose-asm^/VERBOSE_ASM^} (@command{gcc})
4263 ^Used in conjunction with @option{-S}^Used in place of @option{/ASM}^
4264 to cause the generated assembly code file to be annotated with variable
4265 names, making it significantly easier to follow.
4266
4267 @item ^-v^/VERBOSE^
4268 @cindex @option{^-v^/VERBOSE^} (@command{gcc})
4269 Show commands generated by the @command{gcc} driver. Normally used only for
4270 debugging purposes or if you need to be sure what version of the
4271 compiler you are executing.
4272
4273 @ifclear vms
4274 @item -V @var{ver}
4275 @cindex @option{-V} (@command{gcc})
4276 Execute @var{ver} version of the compiler. This is the @command{gcc}
4277 version, not the GNAT version.
4278 @end ifclear
4279
4280 @item ^-w^/NO_BACK_END_WARNINGS^
4281 @cindex @option{-w} (@command{gcc})
4282 Turn off warnings generated by the back end of the compiler. Use of
4283 this switch also causes the default for front end warnings to be set
4284 to suppress (as though @option{-gnatws} had appeared at the start of
4285 the options).
4286
4287 @end table
4288
4289 @ifclear vms
4290 @c Combining qualifiers does not work on VMS
4291 You may combine a sequence of GNAT switches into a single switch. For
4292 example, the combined switch
4293
4294 @cindex Combining GNAT switches
4295 @smallexample
4296 -gnatofi3
4297 @end smallexample
4298
4299 @noindent
4300 is equivalent to specifying the following sequence of switches:
4301
4302 @smallexample
4303 -gnato -gnatf -gnati3
4304 @end smallexample
4305 @end ifclear
4306
4307 @noindent
4308 The following restrictions apply to the combination of switches
4309 in this manner:
4310
4311 @itemize @bullet
4312 @item
4313 The switch @option{-gnatc} if combined with other switches must come
4314 first in the string.
4315
4316 @item
4317 The switch @option{-gnats} if combined with other switches must come
4318 first in the string.
4319
4320 @item
4321 The switches
4322 @option{^-gnatz^/DISTRIBUTION_STUBS^}, @option{-gnatzc}, and @option{-gnatzr}
4323 may not be combined with any other switches.
4324
4325 @ifclear vms
4326 @item
4327 Once a ``y'' appears in the string (that is a use of the @option{-gnaty}
4328 switch), then all further characters in the switch are interpreted
4329 as style modifiers (see description of @option{-gnaty}).
4330
4331 @item
4332 Once a ``d'' appears in the string (that is a use of the @option{-gnatd}
4333 switch), then all further characters in the switch are interpreted
4334 as debug flags (see description of @option{-gnatd}).
4335
4336 @item
4337 Once a ``w'' appears in the string (that is a use of the @option{-gnatw}
4338 switch), then all further characters in the switch are interpreted
4339 as warning mode modifiers (see description of @option{-gnatw}).
4340
4341 @item
4342 Once a ``V'' appears in the string (that is a use of the @option{-gnatV}
4343 switch), then all further characters in the switch are interpreted
4344 as validity checking options (see description of @option{-gnatV}).
4345 @end ifclear
4346 @end itemize
4347
4348 @node Output and Error Message Control
4349 @subsection Output and Error Message Control
4350 @findex stderr
4351
4352 @noindent
4353 The standard default format for error messages is called ``brief format''.
4354 Brief format messages are written to @file{stderr} (the standard error
4355 file) and have the following form:
4356
4357 @smallexample
4358 e.adb:3:04: Incorrect spelling of keyword "function"
4359 e.adb:4:20: ";" should be "is"
4360 @end smallexample
4361
4362 @noindent
4363 The first integer after the file name is the line number in the file,
4364 and the second integer is the column number within the line.
4365 @ifclear vms
4366 @code{GPS} can parse the error messages
4367 and point to the referenced character.
4368 @end ifclear
4369 The following switches provide control over the error message
4370 format:
4371
4372 @table @option
4373 @c !sort!
4374 @item -gnatv
4375 @cindex @option{-gnatv} (@command{gcc})
4376 @findex stdout
4377 @ifclear vms
4378 The v stands for verbose.
4379 @end ifclear
4380 The effect of this setting is to write long-format error
4381 messages to @file{stdout} (the standard output file.
4382 The same program compiled with the
4383 @option{-gnatv} switch would generate:
4384
4385 @smallexample
4386 @cartouche
4387 3. funcion X (Q : Integer)
4388 |
4389 >>> Incorrect spelling of keyword "function"
4390 4. return Integer;
4391 |
4392 >>> ";" should be "is"
4393 @end cartouche
4394 @end smallexample
4395
4396 @noindent
4397 The vertical bar indicates the location of the error, and the @samp{>>>}
4398 prefix can be used to search for error messages. When this switch is
4399 used the only source lines output are those with errors.
4400
4401 @item -gnatl
4402 @cindex @option{-gnatl} (@command{gcc})
4403 @ifclear vms
4404 The @code{l} stands for list.
4405 @end ifclear
4406 This switch causes a full listing of
4407 the file to be generated. In the case where a body is
4408 compiled, the corresponding spec is also listed, along
4409 with any subunits. Typical output from compiling a package
4410 body @file{p.adb} might look like:
4411
4412 @smallexample @c ada
4413 @cartouche
4414 Compiling: p.adb
4415
4416 1. package body p is
4417 2. procedure a;
4418 3. procedure a is separate;
4419 4. begin
4420 5. null
4421 |
4422 >>> missing ";"
4423
4424 6. end;
4425
4426 Compiling: p.ads
4427
4428 1. package p is
4429 2. pragma Elaborate_Body
4430 |
4431 >>> missing ";"
4432
4433 3. end p;
4434
4435 Compiling: p-a.adb
4436
4437 1. separate p
4438 |
4439 >>> missing "("
4440
4441 2. procedure a is
4442 3. begin
4443 4. null
4444 |
4445 >>> missing ";"
4446
4447 5. end;
4448 @end cartouche
4449 @end smallexample
4450
4451 @noindent
4452 @findex stderr
4453 When you specify the @option{-gnatv} or @option{-gnatl} switches and
4454 standard output is redirected, a brief summary is written to
4455 @file{stderr} (standard error) giving the number of error messages and
4456 warning messages generated.
4457
4458 @item -^gnatl^OUTPUT_FILE^=file
4459 @cindex @option{^-gnatl^OUTPUT_FILE^=fname} (@command{gcc})
4460 This has the same effect as @option{-gnatl} except that the output is
4461 written to a file instead of to standard output. If the given name
4462 @file{fname} does not start with a period, then it is the full name
4463 of the file to be written. If @file{fname} is an extension, it is
4464 appended to the name of the file being compiled. For example, if
4465 file @file{xyz.adb} is compiled with @option{^-gnatl^OUTPUT_FILE^=.lst},
4466 then the output is written to file ^xyz.adb.lst^xyz.adb_lst^.
4467
4468 @item -gnatU
4469 @cindex @option{-gnatU} (@command{gcc})
4470 This switch forces all error messages to be preceded by the unique
4471 string ``error:''. This means that error messages take a few more
4472 characters in space, but allows easy searching for and identification
4473 of error messages.
4474
4475 @item -gnatb
4476 @cindex @option{-gnatb} (@command{gcc})
4477 @ifclear vms
4478 The @code{b} stands for brief.
4479 @end ifclear
4480 This switch causes GNAT to generate the
4481 brief format error messages to @file{stderr} (the standard error
4482 file) as well as the verbose
4483 format message or full listing (which as usual is written to
4484 @file{stdout} (the standard output file).
4485
4486 @item -gnatm=@var{n}
4487 @cindex @option{-gnatm} (@command{gcc})
4488 @ifclear vms
4489 The @code{m} stands for maximum.
4490 @end ifclear
4491 @var{n} is a decimal integer in the
4492 range of 1 to 999 and limits the number of error messages to be
4493 generated. For example, using @option{-gnatm2} might yield
4494
4495 @smallexample
4496 e.adb:3:04: Incorrect spelling of keyword "function"
4497 e.adb:5:35: missing ".."
4498 fatal error: maximum errors reached
4499 compilation abandoned
4500 @end smallexample
4501
4502 @noindent
4503 Note that the equal sign is optional, so the switches
4504 @option{-gnatm2} and @option{-gnatm=2} are equivalent.
4505
4506 @item -gnatf
4507 @cindex @option{-gnatf} (@command{gcc})
4508 @cindex Error messages, suppressing
4509 @ifclear vms
4510 The @code{f} stands for full.
4511 @end ifclear
4512 Normally, the compiler suppresses error messages that are likely to be
4513 redundant. This switch causes all error
4514 messages to be generated. In particular, in the case of
4515 references to undefined variables. If a given variable is referenced
4516 several times, the normal format of messages is
4517 @smallexample
4518 e.adb:7:07: "V" is undefined (more references follow)
4519 @end smallexample
4520
4521 @noindent
4522 where the parenthetical comment warns that there are additional
4523 references to the variable @code{V}. Compiling the same program with the
4524 @option{-gnatf} switch yields
4525
4526 @smallexample
4527 e.adb:7:07: "V" is undefined
4528 e.adb:8:07: "V" is undefined
4529 e.adb:8:12: "V" is undefined
4530 e.adb:8:16: "V" is undefined
4531 e.adb:9:07: "V" is undefined
4532 e.adb:9:12: "V" is undefined
4533 @end smallexample
4534
4535 @noindent
4536 The @option{-gnatf} switch also generates additional information for
4537 some error messages. Some examples are:
4538
4539 @itemize @bullet
4540 @item
4541 Full details on entities not available in high integrity mode
4542 @item
4543 Details on possibly non-portable unchecked conversion
4544 @item
4545 List possible interpretations for ambiguous calls
4546 @item
4547 Additional details on incorrect parameters
4548 @end itemize
4549
4550 @item -gnatjnn
4551 @cindex @option{-gnatjnn} (@command{gcc})
4552 In normal operation mode (or if @option{-gnatj0} is used, then error messages
4553 with continuation lines are treated as though the continuation lines were
4554 separate messages (and so a warning with two continuation lines counts as
4555 three warnings, and is listed as three separate messages).
4556
4557 If the @option{-gnatjnn} switch is used with a positive value for nn, then
4558 messages are output in a different manner. A message and all its continuation
4559 lines are treated as a unit, and count as only one warning or message in the
4560 statistics totals. Furthermore, the message is reformatted so that no line
4561 is longer than nn characters.
4562
4563 @item -gnatq
4564 @cindex @option{-gnatq} (@command{gcc})
4565 @ifclear vms
4566 The @code{q} stands for quit (really ``don't quit'').
4567 @end ifclear
4568 In normal operation mode, the compiler first parses the program and
4569 determines if there are any syntax errors. If there are, appropriate
4570 error messages are generated and compilation is immediately terminated.
4571 This switch tells
4572 GNAT to continue with semantic analysis even if syntax errors have been
4573 found. This may enable the detection of more errors in a single run. On
4574 the other hand, the semantic analyzer is more likely to encounter some
4575 internal fatal error when given a syntactically invalid tree.
4576
4577 @item -gnatQ
4578 @cindex @option{-gnatQ} (@command{gcc})
4579 In normal operation mode, the @file{ALI} file is not generated if any
4580 illegalities are detected in the program. The use of @option{-gnatQ} forces
4581 generation of the @file{ALI} file. This file is marked as being in
4582 error, so it cannot be used for binding purposes, but it does contain
4583 reasonably complete cross-reference information, and thus may be useful
4584 for use by tools (e.g., semantic browsing tools or integrated development
4585 environments) that are driven from the @file{ALI} file. This switch
4586 implies @option{-gnatq}, since the semantic phase must be run to get a
4587 meaningful ALI file.
4588
4589 In addition, if @option{-gnatt} is also specified, then the tree file is
4590 generated even if there are illegalities. It may be useful in this case
4591 to also specify @option{-gnatq} to ensure that full semantic processing
4592 occurs. The resulting tree file can be processed by ASIS, for the purpose
4593 of providing partial information about illegal units, but if the error
4594 causes the tree to be badly malformed, then ASIS may crash during the
4595 analysis.
4596
4597 When @option{-gnatQ} is used and the generated @file{ALI} file is marked as
4598 being in error, @command{gnatmake} will attempt to recompile the source when it
4599 finds such an @file{ALI} file, including with switch @option{-gnatc}.
4600
4601 Note that @option{-gnatQ} has no effect if @option{-gnats} is specified,
4602 since ALI files are never generated if @option{-gnats} is set.
4603
4604 @end table
4605
4606 @node Warning Message Control
4607 @subsection Warning Message Control
4608 @cindex Warning messages
4609 @noindent
4610 In addition to error messages, which correspond to illegalities as defined
4611 in the Ada Reference Manual, the compiler detects two kinds of warning
4612 situations.
4613
4614 First, the compiler considers some constructs suspicious and generates a
4615 warning message to alert you to a possible error. Second, if the
4616 compiler detects a situation that is sure to raise an exception at
4617 run time, it generates a warning message. The following shows an example
4618 of warning messages:
4619 @smallexample
4620 e.adb:4:24: warning: creation of object may raise Storage_Error
4621 e.adb:10:17: warning: static value out of range
4622 e.adb:10:17: warning: "Constraint_Error" will be raised at run time
4623 @end smallexample
4624
4625 @noindent
4626 GNAT considers a large number of situations as appropriate
4627 for the generation of warning messages. As always, warnings are not
4628 definite indications of errors. For example, if you do an out-of-range
4629 assignment with the deliberate intention of raising a
4630 @code{Constraint_Error} exception, then the warning that may be
4631 issued does not indicate an error. Some of the situations for which GNAT
4632 issues warnings (at least some of the time) are given in the following
4633 list. This list is not complete, and new warnings are often added to
4634 subsequent versions of GNAT. The list is intended to give a general idea
4635 of the kinds of warnings that are generated.
4636
4637 @itemize @bullet
4638 @item
4639 Possible infinitely recursive calls
4640
4641 @item
4642 Out-of-range values being assigned
4643
4644 @item
4645 Possible order of elaboration problems
4646
4647 @item
4648 Assertions (pragma Assert) that are sure to fail
4649
4650 @item
4651 Unreachable code
4652
4653 @item
4654 Address clauses with possibly unaligned values, or where an attempt is
4655 made to overlay a smaller variable with a larger one.
4656
4657 @item
4658 Fixed-point type declarations with a null range
4659
4660 @item
4661 Direct_IO or Sequential_IO instantiated with a type that has access values
4662
4663 @item
4664 Variables that are never assigned a value
4665
4666 @item
4667 Variables that are referenced before being initialized
4668
4669 @item
4670 Task entries with no corresponding @code{accept} statement
4671
4672 @item
4673 Duplicate accepts for the same task entry in a @code{select}
4674
4675 @item
4676 Objects that take too much storage
4677
4678 @item
4679 Unchecked conversion between types of differing sizes
4680
4681 @item
4682 Missing @code{return} statement along some execution path in a function
4683
4684 @item
4685 Incorrect (unrecognized) pragmas
4686
4687 @item
4688 Incorrect external names
4689
4690 @item
4691 Allocation from empty storage pool
4692
4693 @item
4694 Potentially blocking operation in protected type
4695
4696 @item
4697 Suspicious parenthesization of expressions
4698
4699 @item
4700 Mismatching bounds in an aggregate
4701
4702 @item
4703 Attempt to return local value by reference
4704
4705 @item
4706 Premature instantiation of a generic body
4707
4708 @item
4709 Attempt to pack aliased components
4710
4711 @item
4712 Out of bounds array subscripts
4713
4714 @item
4715 Wrong length on string assignment
4716
4717 @item
4718 Violations of style rules if style checking is enabled
4719
4720 @item
4721 Unused @code{with} clauses
4722
4723 @item
4724 @code{Bit_Order} usage that does not have any effect
4725
4726 @item
4727 @code{Standard.Duration} used to resolve universal fixed expression
4728
4729 @item
4730 Dereference of possibly null value
4731
4732 @item
4733 Declaration that is likely to cause storage error
4734
4735 @item
4736 Internal GNAT unit @code{with}'ed by application unit
4737
4738 @item
4739 Values known to be out of range at compile time
4740
4741 @item
4742 Unreferenced labels and variables
4743
4744 @item
4745 Address overlays that could clobber memory
4746
4747 @item
4748 Unexpected initialization when address clause present
4749
4750 @item
4751 Bad alignment for address clause
4752
4753 @item
4754 Useless type conversions
4755
4756 @item
4757 Redundant assignment statements and other redundant constructs
4758
4759 @item
4760 Useless exception handlers
4761
4762 @item
4763 Accidental hiding of name by child unit
4764
4765 @item
4766 Access before elaboration detected at compile time
4767
4768 @item
4769 A range in a @code{for} loop that is known to be null or might be null
4770
4771 @end itemize
4772
4773 @noindent
4774 The following section lists compiler switches that are available
4775 to control the handling of warning messages. It is also possible
4776 to exercise much finer control over what warnings are issued and
4777 suppressed using the GNAT pragma Warnings, @xref{Pragma Warnings,,,
4778 gnat_rm, GNAT Reference manual}.
4779
4780 @table @option
4781 @c !sort!
4782 @item -gnatwa
4783 @emph{Activate all optional errors.}
4784 @cindex @option{-gnatwa} (@command{gcc})
4785 This switch activates most optional warning messages, see remaining list
4786 in this section for details on optional warning messages that can be
4787 individually controlled. The warnings that are not turned on by this
4788 switch are
4789 @option{-gnatwd} (implicit dereferencing),
4790 @option{-gnatwh} (hiding),
4791 @option{-gnatwl} (elaboration warnings),
4792 @option{-gnatw.o} (warn on values set by out parameters ignored)
4793 and @option{-gnatwt} (tracking of deleted conditional code).
4794 All other optional warnings are turned on.
4795
4796 @item -gnatwA
4797 @emph{Suppress all optional errors.}
4798 @cindex @option{-gnatwA} (@command{gcc})
4799 This switch suppresses all optional warning messages, see remaining list
4800 in this section for details on optional warning messages that can be
4801 individually controlled.
4802
4803 @item -gnatw.a
4804 @emph{Activate warnings on failing assertions.}
4805 @cindex @option{-gnatw.a} (@command{gcc})
4806 @cindex Assert failures
4807 This switch activates warnings for assertions where the compiler can tell at
4808 compile time that the assertion will fail. Note that this warning is given
4809 even if assertions are disabled. The default is that such warnings are
4810 generated.
4811
4812 @item -gnatw.A
4813 @emph{Suppress warnings on failing assertions.}
4814 @cindex @option{-gnatw.A} (@command{gcc})
4815 @cindex Assert failures
4816 This switch suppresses warnings for assertions where the compiler can tell at
4817 compile time that the assertion will fail.
4818
4819 @item -gnatwb
4820 @emph{Activate warnings on bad fixed values.}
4821 @cindex @option{-gnatwb} (@command{gcc})
4822 @cindex Bad fixed values
4823 @cindex Fixed-point Small value
4824 @cindex Small value
4825 This switch activates warnings for static fixed-point expressions whose
4826 value is not an exact multiple of Small. Such values are implementation
4827 dependent, since an implementation is free to choose either of the multiples
4828 that surround the value. GNAT always chooses the closer one, but this is not
4829 required behavior, and it is better to specify a value that is an exact
4830 multiple, ensuring predictable execution. The default is that such warnings
4831 are not generated.
4832
4833 @item -gnatwB
4834 @emph{Suppress warnings on bad fixed values.}
4835 @cindex @option{-gnatwB} (@command{gcc})
4836 This switch suppresses warnings for static fixed-point expressions whose
4837 value is not an exact multiple of Small.
4838
4839 @item -gnatw.b
4840 @emph{Activate warnings on biased representation.}
4841 @cindex @option{-gnatw.b} (@command{gcc})
4842 @cindex Biased representation
4843 This switch activates warnings when a size clause, value size clause, component
4844 clause, or component size clause forces the use of biased representation for an
4845 integer type (e.g. representing a range of 10..11 in a single bit by using 0/1
4846 to represent 10/11). The default is that such warnings are generated.
4847
4848 @item -gnatw.B
4849 @emph{Suppress warnings on biased representation.}
4850 @cindex @option{-gnatwB} (@command{gcc})
4851 This switch suppresses warnings for representation clauses that force the use
4852 of biased representation.
4853
4854 @item -gnatwc
4855 @emph{Activate warnings on conditionals.}
4856 @cindex @option{-gnatwc} (@command{gcc})
4857 @cindex Conditionals, constant
4858 This switch activates warnings for conditional expressions used in
4859 tests that are known to be True or False at compile time. The default
4860 is that such warnings are not generated.
4861 Note that this warning does
4862 not get issued for the use of boolean variables or constants whose
4863 values are known at compile time, since this is a standard technique
4864 for conditional compilation in Ada, and this would generate too many
4865 false positive warnings.
4866
4867 This warning option also activates a special test for comparisons using
4868 the operators ``>='' and`` <=''.
4869 If the compiler can tell that only the equality condition is possible,
4870 then it will warn that the ``>'' or ``<'' part of the test
4871 is useless and that the operator could be replaced by ``=''.
4872 An example would be comparing a @code{Natural} variable <= 0.
4873
4874 This warning option also generates warnings if
4875 one or both tests is optimized away in a membership test for integer
4876 values if the result can be determined at compile time. Range tests on
4877 enumeration types are not included, since it is common for such tests
4878 to include an end point.
4879
4880 This warning can also be turned on using @option{-gnatwa}.
4881
4882 @item -gnatwC
4883 @emph{Suppress warnings on conditionals.}
4884 @cindex @option{-gnatwC} (@command{gcc})
4885 This switch suppresses warnings for conditional expressions used in
4886 tests that are known to be True or False at compile time.
4887
4888 @item -gnatw.c
4889 @emph{Activate warnings on missing component clauses.}
4890 @cindex @option{-gnatw.c} (@command{gcc})
4891 @cindex Component clause, missing
4892 This switch activates warnings for record components where a record
4893 representation clause is present and has component clauses for the
4894 majority, but not all, of the components. A warning is given for each
4895 component for which no component clause is present.
4896
4897 This warning can also be turned on using @option{-gnatwa}.
4898
4899 @item -gnatw.C
4900 @emph{Suppress warnings on missing component clauses.}
4901 @cindex @option{-gnatwC} (@command{gcc})
4902 This switch suppresses warnings for record components that are
4903 missing a component clause in the situation described above.
4904
4905 @item -gnatwd
4906 @emph{Activate warnings on implicit dereferencing.}
4907 @cindex @option{-gnatwd} (@command{gcc})
4908 If this switch is set, then the use of a prefix of an access type
4909 in an indexed component, slice, or selected component without an
4910 explicit @code{.all} will generate a warning. With this warning
4911 enabled, access checks occur only at points where an explicit
4912 @code{.all} appears in the source code (assuming no warnings are
4913 generated as a result of this switch). The default is that such
4914 warnings are not generated.
4915 Note that @option{-gnatwa} does not affect the setting of
4916 this warning option.
4917
4918 @item -gnatwD
4919 @emph{Suppress warnings on implicit dereferencing.}
4920 @cindex @option{-gnatwD} (@command{gcc})
4921 @cindex Implicit dereferencing
4922 @cindex Dereferencing, implicit
4923 This switch suppresses warnings for implicit dereferences in
4924 indexed components, slices, and selected components.
4925
4926 @item -gnatwe
4927 @emph{Treat warnings as errors.}
4928 @cindex @option{-gnatwe} (@command{gcc})
4929 @cindex Warnings, treat as error
4930 This switch causes warning messages to be treated as errors.
4931 The warning string still appears, but the warning messages are counted
4932 as errors, and prevent the generation of an object file.
4933
4934 @item -gnatw.e
4935 @emph{Activate every optional warning}
4936 @cindex @option{-gnatw.e} (@command{gcc})
4937 @cindex Warnings, activate every optional warning
4938 This switch activates all optional warnings, including those which
4939 are not activated by @code{-gnatwa}.
4940
4941 @item -gnatwf
4942 @emph{Activate warnings on unreferenced formals.}
4943 @cindex @option{-gnatwf} (@command{gcc})
4944 @cindex Formals, unreferenced
4945 This switch causes a warning to be generated if a formal parameter
4946 is not referenced in the body of the subprogram. This warning can
4947 also be turned on using @option{-gnatwa} or @option{-gnatwu}. The
4948 default is that these warnings are not generated.
4949
4950 @item -gnatwF
4951 @emph{Suppress warnings on unreferenced formals.}
4952 @cindex @option{-gnatwF} (@command{gcc})
4953 This switch suppresses warnings for unreferenced formal
4954 parameters. Note that the
4955 combination @option{-gnatwu} followed by @option{-gnatwF} has the
4956 effect of warning on unreferenced entities other than subprogram
4957 formals.
4958
4959 @item -gnatwg
4960 @emph{Activate warnings on unrecognized pragmas.}
4961 @cindex @option{-gnatwg} (@command{gcc})
4962 @cindex Pragmas, unrecognized
4963 This switch causes a warning to be generated if an unrecognized
4964 pragma is encountered. Apart from issuing this warning, the
4965 pragma is ignored and has no effect. This warning can
4966 also be turned on using @option{-gnatwa}. The default
4967 is that such warnings are issued (satisfying the Ada Reference
4968 Manual requirement that such warnings appear).
4969
4970 @item -gnatwG
4971 @emph{Suppress warnings on unrecognized pragmas.}
4972 @cindex @option{-gnatwG} (@command{gcc})
4973 This switch suppresses warnings for unrecognized pragmas.
4974
4975 @item -gnatwh
4976 @emph{Activate warnings on hiding.}
4977 @cindex @option{-gnatwh} (@command{gcc})
4978 @cindex Hiding of Declarations
4979 This switch activates warnings on hiding declarations.
4980 A declaration is considered hiding
4981 if it is for a non-overloadable entity, and it declares an entity with the
4982 same name as some other entity that is directly or use-visible. The default
4983 is that such warnings are not generated.
4984 Note that @option{-gnatwa} does not affect the setting of this warning option.
4985
4986 @item -gnatwH
4987 @emph{Suppress warnings on hiding.}
4988 @cindex @option{-gnatwH} (@command{gcc})
4989 This switch suppresses warnings on hiding declarations.
4990
4991 @item -gnatwi
4992 @emph{Activate warnings on implementation units.}
4993 @cindex @option{-gnatwi} (@command{gcc})
4994 This switch activates warnings for a @code{with} of an internal GNAT
4995 implementation unit, defined as any unit from the @code{Ada},
4996 @code{Interfaces}, @code{GNAT},
4997 ^^@code{DEC},^ or @code{System}
4998 hierarchies that is not
4999 documented in either the Ada Reference Manual or the GNAT
5000 Programmer's Reference Manual. Such units are intended only
5001 for internal implementation purposes and should not be @code{with}'ed
5002 by user programs. The default is that such warnings are generated
5003 This warning can also be turned on using @option{-gnatwa}.
5004
5005 @item -gnatwI
5006 @emph{Disable warnings on implementation units.}
5007 @cindex @option{-gnatwI} (@command{gcc})
5008 This switch disables warnings for a @code{with} of an internal GNAT
5009 implementation unit.
5010
5011 @item -gnatwj
5012 @emph{Activate warnings on obsolescent features (Annex J).}
5013 @cindex @option{-gnatwj} (@command{gcc})
5014 @cindex Features, obsolescent
5015 @cindex Obsolescent features
5016 If this warning option is activated, then warnings are generated for
5017 calls to subprograms marked with @code{pragma Obsolescent} and
5018 for use of features in Annex J of the Ada Reference Manual. In the
5019 case of Annex J, not all features are flagged. In particular use
5020 of the renamed packages (like @code{Text_IO}) and use of package
5021 @code{ASCII} are not flagged, since these are very common and
5022 would generate many annoying positive warnings. The default is that
5023 such warnings are not generated. This warning is also turned on by
5024 the use of @option{-gnatwa}.
5025
5026 In addition to the above cases, warnings are also generated for
5027 GNAT features that have been provided in past versions but which
5028 have been superseded (typically by features in the new Ada standard).
5029 For example, @code{pragma Ravenscar} will be flagged since its
5030 function is replaced by @code{pragma Profile(Ravenscar)}.
5031
5032 Note that this warning option functions differently from the
5033 restriction @code{No_Obsolescent_Features} in two respects.
5034 First, the restriction applies only to annex J features.
5035 Second, the restriction does flag uses of package @code{ASCII}.
5036
5037 @item -gnatwJ
5038 @emph{Suppress warnings on obsolescent features (Annex J).}
5039 @cindex @option{-gnatwJ} (@command{gcc})
5040 This switch disables warnings on use of obsolescent features.
5041
5042 @item -gnatwk
5043 @emph{Activate warnings on variables that could be constants.}
5044 @cindex @option{-gnatwk} (@command{gcc})
5045 This switch activates warnings for variables that are initialized but
5046 never modified, and then could be declared constants. The default is that
5047 such warnings are not given.
5048 This warning can also be turned on using @option{-gnatwa}.
5049
5050 @item -gnatwK
5051 @emph{Suppress warnings on variables that could be constants.}
5052 @cindex @option{-gnatwK} (@command{gcc})
5053 This switch disables warnings on variables that could be declared constants.
5054
5055 @item -gnatwl
5056 @emph{Activate warnings for elaboration pragmas.}
5057 @cindex @option{-gnatwl} (@command{gcc})
5058 @cindex Elaboration, warnings
5059 This switch activates warnings on missing
5060 @code{Elaborate_All} and @code{Elaborate} pragmas.
5061 See the section in this guide on elaboration checking for details on
5062 when such pragmas should be used. In dynamic elaboration mode, this switch
5063 generations warnings about the need to add elaboration pragmas. Note however,
5064 that if you blindly follow these warnings, and add @code{Elaborate_All}
5065 warnings wherever they are recommended, you basically end up with the
5066 equivalent of the static elaboration model, which may not be what you want for
5067 legacy code for which the static model does not work.
5068
5069 For the static model, the messages generated are labeled "info:" (for
5070 information messages). They are not warnings to add elaboration pragmas,
5071 merely informational messages showing what implicit elaboration pragmas
5072 have been added, for use in analyzing elaboration circularity problems.
5073
5074 Warnings are also generated if you
5075 are using the static mode of elaboration, and a @code{pragma Elaborate}
5076 is encountered. The default is that such warnings
5077 are not generated.
5078 This warning is not automatically turned on by the use of @option{-gnatwa}.
5079
5080 @item -gnatwL
5081 @emph{Suppress warnings for elaboration pragmas.}
5082 @cindex @option{-gnatwL} (@command{gcc})
5083 This switch suppresses warnings on missing Elaborate and Elaborate_All pragmas.
5084 See the section in this guide on elaboration checking for details on
5085 when such pragmas should be used.
5086
5087 @item -gnatwm
5088 @emph{Activate warnings on modified but unreferenced variables.}
5089 @cindex @option{-gnatwm} (@command{gcc})
5090 This switch activates warnings for variables that are assigned (using
5091 an initialization value or with one or more assignment statements) but
5092 whose value is never read. The warning is suppressed for volatile
5093 variables and also for variables that are renamings of other variables
5094 or for which an address clause is given.
5095 This warning can also be turned on using @option{-gnatwa}.
5096 The default is that these warnings are not given.
5097
5098 @item -gnatwM
5099 @emph{Disable warnings on modified but unreferenced variables.}
5100 @cindex @option{-gnatwM} (@command{gcc})
5101 This switch disables warnings for variables that are assigned or
5102 initialized, but never read.
5103
5104 @item -gnatwn
5105 @emph{Set normal warnings mode.}
5106 @cindex @option{-gnatwn} (@command{gcc})
5107 This switch sets normal warning mode, in which enabled warnings are
5108 issued and treated as warnings rather than errors. This is the default
5109 mode. the switch @option{-gnatwn} can be used to cancel the effect of
5110 an explicit @option{-gnatws} or
5111 @option{-gnatwe}. It also cancels the effect of the
5112 implicit @option{-gnatwe} that is activated by the
5113 use of @option{-gnatg}.
5114
5115 @item -gnatwo
5116 @emph{Activate warnings on address clause overlays.}
5117 @cindex @option{-gnatwo} (@command{gcc})
5118 @cindex Address Clauses, warnings
5119 This switch activates warnings for possibly unintended initialization
5120 effects of defining address clauses that cause one variable to overlap
5121 another. The default is that such warnings are generated.
5122 This warning can also be turned on using @option{-gnatwa}.
5123
5124 @item -gnatwO
5125 @emph{Suppress warnings on address clause overlays.}
5126 @cindex @option{-gnatwO} (@command{gcc})
5127 This switch suppresses warnings on possibly unintended initialization
5128 effects of defining address clauses that cause one variable to overlap
5129 another.
5130
5131 @item -gnatw.o
5132 @emph{Activate warnings on modified but unreferenced out parameters.}
5133 @cindex @option{-gnatw.o} (@command{gcc})
5134 This switch activates warnings for variables that are modified by using
5135 them as actuals for a call to a procedure with an out mode formal, where
5136 the resulting assigned value is never read. It is applicable in the case
5137 where there is more than one out mode formal. If there is only one out
5138 mode formal, the warning is issued by default (controlled by -gnatwu).
5139 The warning is suppressed for volatile
5140 variables and also for variables that are renamings of other variables
5141 or for which an address clause is given.
5142 The default is that these warnings are not given. Note that this warning
5143 is not included in -gnatwa, it must be activated explicitly.
5144
5145 @item -gnatw.O
5146 @emph{Disable warnings on modified but unreferenced out parameters.}
5147 @cindex @option{-gnatw.O} (@command{gcc})
5148 This switch suppresses warnings for variables that are modified by using
5149 them as actuals for a call to a procedure with an out mode formal, where
5150 the resulting assigned value is never read.
5151
5152 @item -gnatwp
5153 @emph{Activate warnings on ineffective pragma Inlines.}
5154 @cindex @option{-gnatwp} (@command{gcc})
5155 @cindex Inlining, warnings
5156 This switch activates warnings for failure of front end inlining
5157 (activated by @option{-gnatN}) to inline a particular call. There are
5158 many reasons for not being able to inline a call, including most
5159 commonly that the call is too complex to inline. The default is
5160 that such warnings are not given.
5161 This warning can also be turned on using @option{-gnatwa}.
5162 Warnings on ineffective inlining by the gcc back-end can be activated
5163 separately, using the gcc switch -Winline.
5164
5165 @item -gnatwP
5166 @emph{Suppress warnings on ineffective pragma Inlines.}
5167 @cindex @option{-gnatwP} (@command{gcc})
5168 This switch suppresses warnings on ineffective pragma Inlines. If the
5169 inlining mechanism cannot inline a call, it will simply ignore the
5170 request silently.
5171
5172 @item -gnatw.p
5173 @emph{Activate warnings on parameter ordering.}
5174 @cindex @option{-gnatw.p} (@command{gcc})
5175 @cindex Parameter order, warnings
5176 This switch activates warnings for cases of suspicious parameter
5177 ordering when the list of arguments are all simple identifiers that
5178 match the names of the formals, but are in a different order. The
5179 warning is suppressed if any use of named parameter notation is used,
5180 so this is the appropriate way to suppress a false positive (and
5181 serves to emphasize that the "misordering" is deliberate). The
5182 default is
5183 that such warnings are not given.
5184 This warning can also be turned on using @option{-gnatwa}.
5185
5186 @item -gnatw.P
5187 @emph{Suppress warnings on parameter ordering.}
5188 @cindex @option{-gnatw.P} (@command{gcc})
5189 This switch suppresses warnings on cases of suspicious parameter
5190 ordering.
5191
5192 @item -gnatwq
5193 @emph{Activate warnings on questionable missing parentheses.}
5194 @cindex @option{-gnatwq} (@command{gcc})
5195 @cindex Parentheses, warnings
5196 This switch activates warnings for cases where parentheses are not used and
5197 the result is potential ambiguity from a readers point of view. For example
5198 (not a > b) when a and b are modular means ((not a) > b) and very likely the
5199 programmer intended (not (a > b)). Similarly (-x mod 5) means (-(x mod 5)) and
5200 quite likely ((-x) mod 5) was intended. In such situations it seems best to
5201 follow the rule of always parenthesizing to make the association clear, and
5202 this warning switch warns if such parentheses are not present. The default
5203 is that these warnings are given.
5204 This warning can also be turned on using @option{-gnatwa}.
5205
5206 @item -gnatwQ
5207 @emph{Suppress warnings on questionable missing parentheses.}
5208 @cindex @option{-gnatwQ} (@command{gcc})
5209 This switch suppresses warnings for cases where the association is not
5210 clear and the use of parentheses is preferred.
5211
5212 @item -gnatwr
5213 @emph{Activate warnings on redundant constructs.}
5214 @cindex @option{-gnatwr} (@command{gcc})
5215 This switch activates warnings for redundant constructs. The following
5216 is the current list of constructs regarded as redundant:
5217
5218 @itemize @bullet
5219 @item
5220 Assignment of an item to itself.
5221 @item
5222 Type conversion that converts an expression to its own type.
5223 @item
5224 Use of the attribute @code{Base} where @code{typ'Base} is the same
5225 as @code{typ}.
5226 @item
5227 Use of pragma @code{Pack} when all components are placed by a record
5228 representation clause.
5229 @item
5230 Exception handler containing only a reraise statement (raise with no
5231 operand) which has no effect.
5232 @item
5233 Use of the operator abs on an operand that is known at compile time
5234 to be non-negative
5235 @item
5236 Comparison of boolean expressions to an explicit True value.
5237 @end itemize
5238
5239 This warning can also be turned on using @option{-gnatwa}.
5240 The default is that warnings for redundant constructs are not given.
5241
5242 @item -gnatwR
5243 @emph{Suppress warnings on redundant constructs.}
5244 @cindex @option{-gnatwR} (@command{gcc})
5245 This switch suppresses warnings for redundant constructs.
5246
5247 @item -gnatws
5248 @emph{Suppress all warnings.}
5249 @cindex @option{-gnatws} (@command{gcc})
5250 This switch completely suppresses the
5251 output of all warning messages from the GNAT front end.
5252 Note that it does not suppress warnings from the @command{gcc} back end.
5253 To suppress these back end warnings as well, use the switch @option{-w}
5254 in addition to @option{-gnatws}.
5255
5256 @item -gnatwt
5257 @emph{Activate warnings for tracking of deleted conditional code.}
5258 @cindex @option{-gnatwt} (@command{gcc})
5259 @cindex Deactivated code, warnings
5260 @cindex Deleted code, warnings
5261 This switch activates warnings for tracking of code in conditionals (IF and
5262 CASE statements) that is detected to be dead code which cannot be executed, and
5263 which is removed by the front end. This warning is off by default, and is not
5264 turned on by @option{-gnatwa}, it has to be turned on explicitly. This may be
5265 useful for detecting deactivated code in certified applications.
5266
5267 @item -gnatwT
5268 @emph{Suppress warnings for tracking of deleted conditional code.}
5269 @cindex @option{-gnatwT} (@command{gcc})
5270 This switch suppresses warnings for tracking of deleted conditional code.
5271
5272 @item -gnatwu
5273 @emph{Activate warnings on unused entities.}
5274 @cindex @option{-gnatwu} (@command{gcc})
5275 This switch activates warnings to be generated for entities that
5276 are declared but not referenced, and for units that are @code{with}'ed
5277 and not
5278 referenced. In the case of packages, a warning is also generated if
5279 no entities in the package are referenced. This means that if the package
5280 is referenced but the only references are in @code{use}
5281 clauses or @code{renames}
5282 declarations, a warning is still generated. A warning is also generated
5283 for a generic package that is @code{with}'ed but never instantiated.
5284 In the case where a package or subprogram body is compiled, and there
5285 is a @code{with} on the corresponding spec
5286 that is only referenced in the body,
5287 a warning is also generated, noting that the
5288 @code{with} can be moved to the body. The default is that
5289 such warnings are not generated.
5290 This switch also activates warnings on unreferenced formals
5291 (it includes the effect of @option{-gnatwf}).
5292 This warning can also be turned on using @option{-gnatwa}.
5293
5294 @item -gnatwU
5295 @emph{Suppress warnings on unused entities.}
5296 @cindex @option{-gnatwU} (@command{gcc})
5297 This switch suppresses warnings for unused entities and packages.
5298 It also turns off warnings on unreferenced formals (and thus includes
5299 the effect of @option{-gnatwF}).
5300
5301 @item -gnatwv
5302 @emph{Activate warnings on unassigned variables.}
5303 @cindex @option{-gnatwv} (@command{gcc})
5304 @cindex Unassigned variable warnings
5305 This switch activates warnings for access to variables which
5306 may not be properly initialized. The default is that
5307 such warnings are generated.
5308 This warning can also be turned on using @option{-gnatwa}.
5309
5310 @item -gnatwV
5311 @emph{Suppress warnings on unassigned variables.}
5312 @cindex @option{-gnatwV} (@command{gcc})
5313 This switch suppresses warnings for access to variables which
5314 may not be properly initialized.
5315 For variables of a composite type, the warning can also be suppressed in
5316 Ada 2005 by using a default initialization with a box. For example, if
5317 Table is an array of records whose components are only partially uninitialized,
5318 then the following code:
5319
5320 @smallexample @c ada
5321 Tab : Table := (others => <>);
5322 @end smallexample
5323
5324 will suppress warnings on subsequent statements that access components
5325 of variable Tab.
5326
5327 @item -gnatww
5328 @emph{Activate warnings on wrong low bound assumption.}
5329 @cindex @option{-gnatww} (@command{gcc})
5330 @cindex String indexing warnings
5331 This switch activates warnings for indexing an unconstrained string parameter
5332 with a literal or S'Length. This is a case where the code is assuming that the
5333 low bound is one, which is in general not true (for example when a slice is
5334 passed). The default is that such warnings are generated.
5335 This warning can also be turned on using @option{-gnatwa}.
5336
5337 @item -gnatwW
5338 @emph{Suppress warnings on wrong low bound assumption.}
5339 @cindex @option{-gnatwW} (@command{gcc})
5340 This switch suppresses warnings for indexing an unconstrained string parameter
5341 with a literal or S'Length. Note that this warning can also be suppressed
5342 in a particular case by adding an
5343 assertion that the lower bound is 1,
5344 as shown in the following example.
5345
5346 @smallexample @c ada
5347 procedure K (S : String) is
5348 pragma Assert (S'First = 1);
5349 @dots{}
5350 @end smallexample
5351
5352 @item -gnatw.w
5353 @emph{Activate warnings on unnecessary Warnings Off pragmas}
5354 @cindex @option{-gnatw.w} (@command{gcc})
5355 @cindex Warnings Off control
5356 This switch activates warnings for use of @code{pragma Warnings (Off, entity}
5357 where either the pragma is entirely useless (because it suppresses no
5358 warnings), or it could be replaced by @code{pragma Unreferenced} or
5359 @code{pragma Unmodified}.The default is that these warnings are not given.
5360 Note that this warning is not included in -gnatwa, it must be
5361 activated explicitly.
5362
5363 @item -gnatw.W
5364 @emph{Suppress warnings on unnecessary Warnings Off pragmas}
5365 @cindex @option{-gnatw.W} (@command{gcc})
5366 This switch suppresses warnings for use of @code{pragma Warnings (Off, entity}.
5367
5368 @item -gnatwx
5369 @emph{Activate warnings on Export/Import pragmas.}
5370 @cindex @option{-gnatwx} (@command{gcc})
5371 @cindex Export/Import pragma warnings
5372 This switch activates warnings on Export/Import pragmas when
5373 the compiler detects a possible conflict between the Ada and
5374 foreign language calling sequences. For example, the use of
5375 default parameters in a convention C procedure is dubious
5376 because the C compiler cannot supply the proper default, so
5377 a warning is issued. The default is that such warnings are
5378 generated.
5379 This warning can also be turned on using @option{-gnatwa}.
5380
5381 @item -gnatwX
5382 @emph{Suppress warnings on Export/Import pragmas.}
5383 @cindex @option{-gnatwX} (@command{gcc})
5384 This switch suppresses warnings on Export/Import pragmas.
5385 The sense of this is that you are telling the compiler that
5386 you know what you are doing in writing the pragma, and it
5387 should not complain at you.
5388
5389 @item -gnatw.x
5390 @emph{Activate warnings for No_Exception_Propagation mode.}
5391 @cindex @option{-gnatwm} (@command{gcc})
5392 This switch activates warnings for exception usage when pragma Restrictions
5393 (No_Exception_Propagation) is in effect. Warnings are given for implicit or
5394 explicit exception raises which are not covered by a local handler, and for
5395 exception handlers which do not cover a local raise. The default is that these
5396 warnings are not given.
5397
5398 @item -gnatw.X
5399 @emph{Disable warnings for No_Exception_Propagation mode.}
5400 This switch disables warnings for exception usage when pragma Restrictions
5401 (No_Exception_Propagation) is in effect.
5402
5403 @item -gnatwy
5404 @emph{Activate warnings for Ada 2005 compatibility issues.}
5405 @cindex @option{-gnatwy} (@command{gcc})
5406 @cindex Ada 2005 compatibility issues warnings
5407 For the most part Ada 2005 is upwards compatible with Ada 95,
5408 but there are some exceptions (for example the fact that
5409 @code{interface} is now a reserved word in Ada 2005). This
5410 switch activates several warnings to help in identifying
5411 and correcting such incompatibilities. The default is that
5412 these warnings are generated. Note that at one point Ada 2005
5413 was called Ada 0Y, hence the choice of character.
5414 This warning can also be turned on using @option{-gnatwa}.
5415
5416 @item -gnatwY
5417 @emph{Disable warnings for Ada 2005 compatibility issues.}
5418 @cindex @option{-gnatwY} (@command{gcc})
5419 @cindex Ada 2005 compatibility issues warnings
5420 This switch suppresses several warnings intended to help in identifying
5421 incompatibilities between Ada 95 and Ada 2005.
5422
5423 @item -gnatwz
5424 @emph{Activate warnings on unchecked conversions.}
5425 @cindex @option{-gnatwz} (@command{gcc})
5426 @cindex Unchecked_Conversion warnings
5427 This switch activates warnings for unchecked conversions
5428 where the types are known at compile time to have different
5429 sizes. The default
5430 is that such warnings are generated. Warnings are also
5431 generated for subprogram pointers with different conventions,
5432 and, on VMS only, for data pointers with different conventions.
5433 This warning can also be turned on using @option{-gnatwa}.
5434
5435 @item -gnatwZ
5436 @emph{Suppress warnings on unchecked conversions.}
5437 @cindex @option{-gnatwZ} (@command{gcc})
5438 This switch suppresses warnings for unchecked conversions
5439 where the types are known at compile time to have different
5440 sizes or conventions.
5441
5442 @item ^-Wunused^WARNINGS=UNUSED^
5443 @cindex @option{-Wunused}
5444 The warnings controlled by the @option{-gnatw} switch are generated by
5445 the front end of the compiler. The @option{GCC} back end can provide
5446 additional warnings and they are controlled by the @option{-W} switch.
5447 For example, @option{^-Wunused^WARNINGS=UNUSED^} activates back end
5448 warnings for entities that are declared but not referenced.
5449
5450 @item ^-Wuninitialized^WARNINGS=UNINITIALIZED^
5451 @cindex @option{-Wuninitialized}
5452 Similarly, @option{^-Wuninitialized^WARNINGS=UNINITIALIZED^} activates
5453 the back end warning for uninitialized variables. This switch must be
5454 used in conjunction with an optimization level greater than zero.
5455
5456 @item ^-Wall^/ALL_BACK_END_WARNINGS^
5457 @cindex @option{-Wall}
5458 This switch enables all the above warnings from the @option{GCC} back end.
5459 The code generator detects a number of warning situations that are missed
5460 by the @option{GNAT} front end, and this switch can be used to activate them.
5461 The use of this switch also sets the default front end warning mode to
5462 @option{-gnatwa}, that is, most front end warnings activated as well.
5463
5464 @item ^-w^/NO_BACK_END_WARNINGS^
5465 @cindex @option{-w}
5466 Conversely, this switch suppresses warnings from the @option{GCC} back end.
5467 The use of this switch also sets the default front end warning mode to
5468 @option{-gnatws}, that is, front end warnings suppressed as well.
5469
5470 @end table
5471
5472 @noindent
5473 @ifclear vms
5474 A string of warning parameters can be used in the same parameter. For example:
5475
5476 @smallexample
5477 -gnatwaLe
5478 @end smallexample
5479
5480 @noindent
5481 will turn on all optional warnings except for elaboration pragma warnings,
5482 and also specify that warnings should be treated as errors.
5483 @end ifclear
5484 When no switch @option{^-gnatw^/WARNINGS^} is used, this is equivalent to:
5485
5486 @table @option
5487 @c !sort!
5488 @item -gnatwC
5489 @item -gnatwD
5490 @item -gnatwF
5491 @item -gnatwg
5492 @item -gnatwH
5493 @item -gnatwi
5494 @item -gnatwJ
5495 @item -gnatwK
5496 @item -gnatwL
5497 @item -gnatwM
5498 @item -gnatwn
5499 @item -gnatwo
5500 @item -gnatwP
5501 @item -gnatwR
5502 @item -gnatwU
5503 @item -gnatwv
5504 @item -gnatwz
5505 @item -gnatwx
5506
5507 @end table
5508
5509 @node Debugging and Assertion Control
5510 @subsection Debugging and Assertion Control
5511
5512 @table @option
5513 @item -gnata
5514 @cindex @option{-gnata} (@command{gcc})
5515 @findex Assert
5516 @findex Debug
5517 @cindex Assertions
5518
5519 @noindent
5520 The pragmas @code{Assert} and @code{Debug} normally have no effect and
5521 are ignored. This switch, where @samp{a} stands for assert, causes
5522 @code{Assert} and @code{Debug} pragmas to be activated.
5523
5524 The pragmas have the form:
5525
5526 @smallexample
5527 @cartouche
5528 @b{pragma} Assert (@var{Boolean-expression} @r{[},
5529 @var{static-string-expression}@r{]})
5530 @b{pragma} Debug (@var{procedure call})
5531 @end cartouche
5532 @end smallexample
5533
5534 @noindent
5535 The @code{Assert} pragma causes @var{Boolean-expression} to be tested.
5536 If the result is @code{True}, the pragma has no effect (other than
5537 possible side effects from evaluating the expression). If the result is
5538 @code{False}, the exception @code{Assert_Failure} declared in the package
5539 @code{System.Assertions} is
5540 raised (passing @var{static-string-expression}, if present, as the
5541 message associated with the exception). If no string expression is
5542 given the default is a string giving the file name and line number
5543 of the pragma.
5544
5545 The @code{Debug} pragma causes @var{procedure} to be called. Note that
5546 @code{pragma Debug} may appear within a declaration sequence, allowing
5547 debugging procedures to be called between declarations.
5548
5549 @ifset vms
5550 @item /DEBUG@r{[}=debug-level@r{]}
5551 @itemx /NODEBUG
5552 Specifies how much debugging information is to be included in
5553 the resulting object file where 'debug-level' is one of the following:
5554 @table @code
5555 @item TRACEBACK
5556 Include both debugger symbol records and traceback
5557 the object file.
5558 This is the default setting.
5559 @item ALL
5560 Include both debugger symbol records and traceback in
5561 object file.
5562 @item NONE
5563 Excludes both debugger symbol records and traceback
5564 the object file. Same as /NODEBUG.
5565 @item SYMBOLS
5566 Includes only debugger symbol records in the object
5567 file. Note that this doesn't include traceback information.
5568 @end table
5569 @end ifset
5570 @end table
5571
5572 @node Validity Checking
5573 @subsection Validity Checking
5574 @findex Validity Checking
5575
5576 @noindent
5577 The Ada Reference Manual has specific requirements for checking
5578 for invalid values. In particular, RM 13.9.1 requires that the
5579 evaluation of invalid values (for example from unchecked conversions),
5580 not result in erroneous execution. In GNAT, the result of such an
5581 evaluation in normal default mode is to either use the value
5582 unmodified, or to raise Constraint_Error in those cases where use
5583 of the unmodified value would cause erroneous execution. The cases
5584 where unmodified values might lead to erroneous execution are case
5585 statements (where a wild jump might result from an invalid value),
5586 and subscripts on the left hand side (where memory corruption could
5587 occur as a result of an invalid value).
5588
5589 The @option{-gnatV^@var{x}^^} switch allows more control over the validity
5590 checking mode.
5591 @ifclear vms
5592 The @code{x} argument is a string of letters that
5593 indicate validity checks that are performed or not performed in addition
5594 to the default checks described above.
5595 @end ifclear
5596 @ifset vms
5597 The options allowed for this qualifier
5598 indicate validity checks that are performed or not performed in addition
5599 to the default checks described above.
5600 @end ifset
5601
5602 @table @option
5603 @c !sort!
5604 @item -gnatVa
5605 @emph{All validity checks.}
5606 @cindex @option{-gnatVa} (@command{gcc})
5607 All validity checks are turned on.
5608 @ifclear vms
5609 That is, @option{-gnatVa} is
5610 equivalent to @option{gnatVcdfimorst}.
5611 @end ifclear
5612
5613 @item -gnatVc
5614 @emph{Validity checks for copies.}
5615 @cindex @option{-gnatVc} (@command{gcc})
5616 The right hand side of assignments, and the initializing values of
5617 object declarations are validity checked.
5618
5619 @item -gnatVd
5620 @emph{Default (RM) validity checks.}
5621 @cindex @option{-gnatVd} (@command{gcc})
5622 Some validity checks are done by default following normal Ada semantics
5623 (RM 13.9.1 (9-11)).
5624 A check is done in case statements that the expression is within the range
5625 of the subtype. If it is not, Constraint_Error is raised.
5626 For assignments to array components, a check is done that the expression used
5627 as index is within the range. If it is not, Constraint_Error is raised.
5628 Both these validity checks may be turned off using switch @option{-gnatVD}.
5629 They are turned on by default. If @option{-gnatVD} is specified, a subsequent
5630 switch @option{-gnatVd} will leave the checks turned on.
5631 Switch @option{-gnatVD} should be used only if you are sure that all such
5632 expressions have valid values. If you use this switch and invalid values
5633 are present, then the program is erroneous, and wild jumps or memory
5634 overwriting may occur.
5635
5636 @item -gnatVe
5637 @emph{Validity checks for elementary components.}
5638 @cindex @option{-gnatVe} (@command{gcc})
5639 In the absence of this switch, assignments to record or array components are
5640 not validity checked, even if validity checks for assignments generally
5641 (@option{-gnatVc}) are turned on. In Ada, assignment of composite values do not
5642 require valid data, but assignment of individual components does. So for
5643 example, there is a difference between copying the elements of an array with a
5644 slice assignment, compared to assigning element by element in a loop. This
5645 switch allows you to turn off validity checking for components, even when they
5646 are assigned component by component.
5647
5648 @item -gnatVf
5649 @emph{Validity checks for floating-point values.}
5650 @cindex @option{-gnatVf} (@command{gcc})
5651 In the absence of this switch, validity checking occurs only for discrete
5652 values. If @option{-gnatVf} is specified, then validity checking also applies
5653 for floating-point values, and NaNs and infinities are considered invalid,
5654 as well as out of range values for constrained types. Note that this means
5655 that standard IEEE infinity mode is not allowed. The exact contexts
5656 in which floating-point values are checked depends on the setting of other
5657 options. For example,
5658 @option{^-gnatVif^VALIDITY_CHECKING=(IN_PARAMS,FLOATS)^} or
5659 @option{^-gnatVfi^VALIDITY_CHECKING=(FLOATS,IN_PARAMS)^}
5660 (the order does not matter) specifies that floating-point parameters of mode
5661 @code{in} should be validity checked.
5662
5663 @item -gnatVi
5664 @emph{Validity checks for @code{in} mode parameters}
5665 @cindex @option{-gnatVi} (@command{gcc})
5666 Arguments for parameters of mode @code{in} are validity checked in function
5667 and procedure calls at the point of call.
5668
5669 @item -gnatVm
5670 @emph{Validity checks for @code{in out} mode parameters.}
5671 @cindex @option{-gnatVm} (@command{gcc})
5672 Arguments for parameters of mode @code{in out} are validity checked in
5673 procedure calls at the point of call. The @code{'m'} here stands for
5674 modify, since this concerns parameters that can be modified by the call.
5675 Note that there is no specific option to test @code{out} parameters,
5676 but any reference within the subprogram will be tested in the usual
5677 manner, and if an invalid value is copied back, any reference to it
5678 will be subject to validity checking.
5679
5680 @item -gnatVn
5681 @emph{No validity checks.}
5682 @cindex @option{-gnatVn} (@command{gcc})
5683 This switch turns off all validity checking, including the default checking
5684 for case statements and left hand side subscripts. Note that the use of
5685 the switch @option{-gnatp} suppresses all run-time checks, including
5686 validity checks, and thus implies @option{-gnatVn}. When this switch
5687 is used, it cancels any other @option{-gnatV} previously issued.
5688
5689 @item -gnatVo
5690 @emph{Validity checks for operator and attribute operands.}
5691 @cindex @option{-gnatVo} (@command{gcc})
5692 Arguments for predefined operators and attributes are validity checked.
5693 This includes all operators in package @code{Standard},
5694 the shift operators defined as intrinsic in package @code{Interfaces}
5695 and operands for attributes such as @code{Pos}. Checks are also made
5696 on individual component values for composite comparisons, and on the
5697 expressions in type conversions and qualified expressions. Checks are
5698 also made on explicit ranges using @samp{..} (e.g.@: slices, loops etc).
5699
5700 @item -gnatVp
5701 @emph{Validity checks for parameters.}
5702 @cindex @option{-gnatVp} (@command{gcc})
5703 This controls the treatment of parameters within a subprogram (as opposed
5704 to @option{-gnatVi} and @option{-gnatVm} which control validity testing
5705 of parameters on a call. If either of these call options is used, then
5706 normally an assumption is made within a subprogram that the input arguments
5707 have been validity checking at the point of call, and do not need checking
5708 again within a subprogram). If @option{-gnatVp} is set, then this assumption
5709 is not made, and parameters are not assumed to be valid, so their validity
5710 will be checked (or rechecked) within the subprogram.
5711
5712 @item -gnatVr
5713 @emph{Validity checks for function returns.}
5714 @cindex @option{-gnatVr} (@command{gcc})
5715 The expression in @code{return} statements in functions is validity
5716 checked.
5717
5718 @item -gnatVs
5719 @emph{Validity checks for subscripts.}
5720 @cindex @option{-gnatVs} (@command{gcc})
5721 All subscripts expressions are checked for validity, whether they appear
5722 on the right side or left side (in default mode only left side subscripts
5723 are validity checked).
5724
5725 @item -gnatVt
5726 @emph{Validity checks for tests.}
5727 @cindex @option{-gnatVt} (@command{gcc})
5728 Expressions used as conditions in @code{if}, @code{while} or @code{exit}
5729 statements are checked, as well as guard expressions in entry calls.
5730
5731 @end table
5732
5733 @noindent
5734 The @option{-gnatV} switch may be followed by
5735 ^a string of letters^a list of options^
5736 to turn on a series of validity checking options.
5737 For example,
5738 @option{^-gnatVcr^/VALIDITY_CHECKING=(COPIES, RETURNS)^}
5739 specifies that in addition to the default validity checking, copies and
5740 function return expressions are to be validity checked.
5741 In order to make it easier
5742 to specify the desired combination of effects,
5743 @ifclear vms
5744 the upper case letters @code{CDFIMORST} may
5745 be used to turn off the corresponding lower case option.
5746 @end ifclear
5747 @ifset vms
5748 the prefix @code{NO} on an option turns off the corresponding validity
5749 checking:
5750 @itemize @bullet
5751 @item @code{NOCOPIES}
5752 @item @code{NODEFAULT}
5753 @item @code{NOFLOATS}
5754 @item @code{NOIN_PARAMS}
5755 @item @code{NOMOD_PARAMS}
5756 @item @code{NOOPERANDS}
5757 @item @code{NORETURNS}
5758 @item @code{NOSUBSCRIPTS}
5759 @item @code{NOTESTS}
5760 @end itemize
5761 @end ifset
5762 Thus
5763 @option{^-gnatVaM^/VALIDITY_CHECKING=(ALL, NOMOD_PARAMS)^}
5764 turns on all validity checking options except for
5765 checking of @code{@b{in out}} procedure arguments.
5766
5767 The specification of additional validity checking generates extra code (and
5768 in the case of @option{-gnatVa} the code expansion can be substantial).
5769 However, these additional checks can be very useful in detecting
5770 uninitialized variables, incorrect use of unchecked conversion, and other
5771 errors leading to invalid values. The use of pragma @code{Initialize_Scalars}
5772 is useful in conjunction with the extra validity checking, since this
5773 ensures that wherever possible uninitialized variables have invalid values.
5774
5775 See also the pragma @code{Validity_Checks} which allows modification of
5776 the validity checking mode at the program source level, and also allows for
5777 temporary disabling of validity checks.
5778
5779 @node Style Checking
5780 @subsection Style Checking
5781 @findex Style checking
5782
5783 @noindent
5784 The @option{-gnaty^x^(option,option,@dots{})^} switch
5785 @cindex @option{-gnaty} (@command{gcc})
5786 causes the compiler to
5787 enforce specified style rules. A limited set of style rules has been used
5788 in writing the GNAT sources themselves. This switch allows user programs
5789 to activate all or some of these checks. If the source program fails a
5790 specified style check, an appropriate warning message is given, preceded by
5791 the character sequence ``(style)''.
5792 @ifset vms
5793 @code{(option,option,@dots{})} is a sequence of keywords
5794 @end ifset
5795 @ifclear vms
5796 The string @var{x} is a sequence of letters or digits
5797 @end ifclear
5798 indicating the particular style
5799 checks to be performed. The following checks are defined:
5800
5801 @table @option
5802 @c !sort!
5803 @item 0-9
5804 @emph{Specify indentation level.}
5805 If a digit from 1-9 appears
5806 ^in the string after @option{-gnaty}^as an option for /STYLE_CHECKS^
5807 then proper indentation is checked, with the digit indicating the
5808 indentation level required. A value of zero turns off this style check.
5809 The general style of required indentation is as specified by
5810 the examples in the Ada Reference Manual. Full line comments must be
5811 aligned with the @code{--} starting on a column that is a multiple of
5812 the alignment level, or they may be aligned the same way as the following
5813 non-blank line (this is useful when full line comments appear in the middle
5814 of a statement.
5815
5816 @item ^a^ATTRIBUTE^
5817 @emph{Check attribute casing.}
5818 Attribute names, including the case of keywords such as @code{digits}
5819 used as attributes names, must be written in mixed case, that is, the
5820 initial letter and any letter following an underscore must be uppercase.
5821 All other letters must be lowercase.
5822
5823 @item ^A^ARRAY_INDEXES^
5824 @emph{Use of array index numbers in array attributes.}
5825 When using the array attributes First, Last, Range,
5826 or Length, the index number must be omitted for one-dimensional arrays
5827 and is required for multi-dimensional arrays.
5828
5829 @item ^b^BLANKS^
5830 @emph{Blanks not allowed at statement end.}
5831 Trailing blanks are not allowed at the end of statements. The purpose of this
5832 rule, together with h (no horizontal tabs), is to enforce a canonical format
5833 for the use of blanks to separate source tokens.
5834
5835 @item ^c^COMMENTS^
5836 @emph{Check comments.}
5837 Comments must meet the following set of rules:
5838
5839 @itemize @bullet
5840
5841 @item
5842 The ``@code{--}'' that starts the column must either start in column one,
5843 or else at least one blank must precede this sequence.
5844
5845 @item
5846 Comments that follow other tokens on a line must have at least one blank
5847 following the ``@code{--}'' at the start of the comment.
5848
5849 @item
5850 Full line comments must have two blanks following the ``@code{--}'' that
5851 starts the comment, with the following exceptions.
5852
5853 @item
5854 A line consisting only of the ``@code{--}'' characters, possibly preceded
5855 by blanks is permitted.
5856
5857 @item
5858 A comment starting with ``@code{--x}'' where @code{x} is a special character
5859 is permitted.
5860 This allows proper processing of the output generated by specialized tools
5861 including @command{gnatprep} (where ``@code{--!}'' is used) and the SPARK
5862 annotation
5863 language (where ``@code{--#}'' is used). For the purposes of this rule, a
5864 special character is defined as being in one of the ASCII ranges
5865 @code{16#21#@dots{}16#2F#} or @code{16#3A#@dots{}16#3F#}.
5866 Note that this usage is not permitted
5867 in GNAT implementation units (i.e., when @option{-gnatg} is used).
5868
5869 @item
5870 A line consisting entirely of minus signs, possibly preceded by blanks, is
5871 permitted. This allows the construction of box comments where lines of minus
5872 signs are used to form the top and bottom of the box.
5873
5874 @item
5875 A comment that starts and ends with ``@code{--}'' is permitted as long as at
5876 least one blank follows the initial ``@code{--}''. Together with the preceding
5877 rule, this allows the construction of box comments, as shown in the following
5878 example:
5879 @smallexample
5880 ---------------------------
5881 -- This is a box comment --
5882 -- with two text lines. --
5883 ---------------------------
5884 @end smallexample
5885 @end itemize
5886
5887 @item ^d^DOS_LINE_ENDINGS^
5888 @emph{Check no DOS line terminators present.}
5889 All lines must be terminated by a single ASCII.LF
5890 character (in particular the DOS line terminator sequence CR/LF is not
5891 allowed).
5892
5893 @item ^e^END^
5894 @emph{Check end/exit labels.}
5895 Optional labels on @code{end} statements ending subprograms and on
5896 @code{exit} statements exiting named loops, are required to be present.
5897
5898 @item ^f^VTABS^
5899 @emph{No form feeds or vertical tabs.}
5900 Neither form feeds nor vertical tab characters are permitted
5901 in the source text.
5902
5903 @item ^g^GNAT^
5904 @emph{GNAT style mode}
5905 The set of style check switches is set to match that used by the GNAT sources.
5906 This may be useful when developing code that is eventually intended to be
5907 incorporated into GNAT. For further details, see GNAT sources.
5908
5909 @item ^h^HTABS^
5910 @emph{No horizontal tabs.}
5911 Horizontal tab characters are not permitted in the source text.
5912 Together with the b (no blanks at end of line) check, this
5913 enforces a canonical form for the use of blanks to separate
5914 source tokens.
5915
5916 @item ^i^IF_THEN^
5917 @emph{Check if-then layout.}
5918 The keyword @code{then} must appear either on the same
5919 line as corresponding @code{if}, or on a line on its own, lined
5920 up under the @code{if} with at least one non-blank line in between
5921 containing all or part of the condition to be tested.
5922
5923 @item ^I^IN_MODE^
5924 @emph{check mode IN keywords}
5925 Mode @code{in} (the default mode) is not
5926 allowed to be given explicitly. @code{in out} is fine,
5927 but not @code{in} on its own.
5928
5929 @item ^k^KEYWORD^
5930 @emph{Check keyword casing.}
5931 All keywords must be in lower case (with the exception of keywords
5932 such as @code{digits} used as attribute names to which this check
5933 does not apply).
5934
5935 @item ^l^LAYOUT^
5936 @emph{Check layout.}
5937 Layout of statement and declaration constructs must follow the
5938 recommendations in the Ada Reference Manual, as indicated by the
5939 form of the syntax rules. For example an @code{else} keyword must
5940 be lined up with the corresponding @code{if} keyword.
5941
5942 There are two respects in which the style rule enforced by this check
5943 option are more liberal than those in the Ada Reference Manual. First
5944 in the case of record declarations, it is permissible to put the
5945 @code{record} keyword on the same line as the @code{type} keyword, and
5946 then the @code{end} in @code{end record} must line up under @code{type}.
5947 This is also permitted when the type declaration is split on two lines.
5948 For example, any of the following three layouts is acceptable:
5949
5950 @smallexample @c ada
5951 @cartouche
5952 type q is record
5953 a : integer;
5954 b : integer;
5955 end record;
5956
5957 type q is
5958 record
5959 a : integer;
5960 b : integer;
5961 end record;
5962
5963 type q is
5964 record
5965 a : integer;
5966 b : integer;
5967 end record;
5968
5969 @end cartouche
5970 @end smallexample
5971
5972 @noindent
5973 Second, in the case of a block statement, a permitted alternative
5974 is to put the block label on the same line as the @code{declare} or
5975 @code{begin} keyword, and then line the @code{end} keyword up under
5976 the block label. For example both the following are permitted:
5977
5978 @smallexample @c ada
5979 @cartouche
5980 Block : declare
5981 A : Integer := 3;
5982 begin
5983 Proc (A, A);
5984 end Block;
5985
5986 Block :
5987 declare
5988 A : Integer := 3;
5989 begin
5990 Proc (A, A);
5991 end Block;
5992 @end cartouche
5993 @end smallexample
5994
5995 @noindent
5996 The same alternative format is allowed for loops. For example, both of
5997 the following are permitted:
5998
5999 @smallexample @c ada
6000 @cartouche
6001 Clear : while J < 10 loop
6002 A (J) := 0;
6003 end loop Clear;
6004
6005 Clear :
6006 while J < 10 loop
6007 A (J) := 0;
6008 end loop Clear;
6009 @end cartouche
6010 @end smallexample
6011
6012 @item ^Lnnn^MAX_NESTING=nnn^
6013 @emph{Set maximum nesting level}
6014 The maximum level of nesting of constructs (including subprograms, loops,
6015 blocks, packages, and conditionals) may not exceed the given value
6016 @option{nnn}. A value of zero disconnects this style check.
6017
6018 @item ^m^LINE_LENGTH^
6019 @emph{Check maximum line length.}
6020 The length of source lines must not exceed 79 characters, including
6021 any trailing blanks. The value of 79 allows convenient display on an
6022 80 character wide device or window, allowing for possible special
6023 treatment of 80 character lines. Note that this count is of
6024 characters in the source text. This means that a tab character counts
6025 as one character in this count but a wide character sequence counts as
6026 a single character (however many bytes are needed in the encoding).
6027
6028 @item ^Mnnn^MAX_LENGTH=nnn^
6029 @emph{Set maximum line length.}
6030 The length of lines must not exceed the
6031 given value @option{nnn}. The maximum value that can be specified is 32767.
6032
6033 @item ^n^STANDARD_CASING^
6034 @emph{Check casing of entities in Standard.}
6035 Any identifier from Standard must be cased
6036 to match the presentation in the Ada Reference Manual (for example,
6037 @code{Integer} and @code{ASCII.NUL}).
6038
6039 @item ^N^NONE^
6040 @emph{Turn off all style checks}
6041 All style check options are turned off.
6042
6043 @item ^o^ORDERED_SUBPROGRAMS^
6044 @emph{Check order of subprogram bodies.}
6045 All subprogram bodies in a given scope
6046 (e.g.@: a package body) must be in alphabetical order. The ordering
6047 rule uses normal Ada rules for comparing strings, ignoring casing
6048 of letters, except that if there is a trailing numeric suffix, then
6049 the value of this suffix is used in the ordering (e.g.@: Junk2 comes
6050 before Junk10).
6051
6052 @item ^p^PRAGMA^
6053 @emph{Check pragma casing.}
6054 Pragma names must be written in mixed case, that is, the
6055 initial letter and any letter following an underscore must be uppercase.
6056 All other letters must be lowercase.
6057
6058 @item ^r^REFERENCES^
6059 @emph{Check references.}
6060 All identifier references must be cased in the same way as the
6061 corresponding declaration. No specific casing style is imposed on
6062 identifiers. The only requirement is for consistency of references
6063 with declarations.
6064
6065 @item ^S^STATEMENTS_AFTER_THEN_ELSE^
6066 @emph{Check no statements after THEN/ELSE.}
6067 No statements are allowed
6068 on the same line as a THEN or ELSE keyword following the
6069 keyword in an IF statement. OR ELSE and AND THEN are not affected,
6070 and a special exception allows a pragma to appear after ELSE.
6071
6072 @item ^s^SPECS^
6073 @emph{Check separate specs.}
6074 Separate declarations (``specs'') are required for subprograms (a
6075 body is not allowed to serve as its own declaration). The only
6076 exception is that parameterless library level procedures are
6077 not required to have a separate declaration. This exception covers
6078 the most frequent form of main program procedures.
6079
6080 @item ^t^TOKEN^
6081 @emph{Check token spacing.}
6082 The following token spacing rules are enforced:
6083
6084 @itemize @bullet
6085
6086 @item
6087 The keywords @code{@b{abs}} and @code{@b{not}} must be followed by a space.
6088
6089 @item
6090 The token @code{=>} must be surrounded by spaces.
6091
6092 @item
6093 The token @code{<>} must be preceded by a space or a left parenthesis.
6094
6095 @item
6096 Binary operators other than @code{**} must be surrounded by spaces.
6097 There is no restriction on the layout of the @code{**} binary operator.
6098
6099 @item
6100 Colon must be surrounded by spaces.
6101
6102 @item
6103 Colon-equal (assignment, initialization) must be surrounded by spaces.
6104
6105 @item
6106 Comma must be the first non-blank character on the line, or be
6107 immediately preceded by a non-blank character, and must be followed
6108 by a space.
6109
6110 @item
6111 If the token preceding a left parenthesis ends with a letter or digit, then
6112 a space must separate the two tokens.
6113
6114 @item
6115 A right parenthesis must either be the first non-blank character on
6116 a line, or it must be preceded by a non-blank character.
6117
6118 @item
6119 A semicolon must not be preceded by a space, and must not be followed by
6120 a non-blank character.
6121
6122 @item
6123 A unary plus or minus may not be followed by a space.
6124
6125 @item
6126 A vertical bar must be surrounded by spaces.
6127 @end itemize
6128
6129 @item ^u^UNNECESSARY_BLANK_LINES^
6130 @emph{Check unnecessary blank lines.}
6131 Unnecessary blank lines are not allowed. A blank line is considered
6132 unnecessary if it appears at the end of the file, or if more than
6133 one blank line occurs in sequence.
6134
6135 @item ^x^XTRA_PARENS^
6136 @emph{Check extra parentheses.}
6137 Unnecessary extra level of parentheses (C-style) are not allowed
6138 around conditions in @code{if} statements, @code{while} statements and
6139 @code{exit} statements.
6140
6141 @item ^y^ALL_BUILTIN^
6142 @emph{Set all standard style check options}
6143 This is equivalent to @code{gnaty3aAbcefhiklmnprst}, that is all checking
6144 options enabled with the exception of @option{-gnatyo}, @option{-gnatyI},
6145 @option{-gnatyS}, @option{-gnatyLnnn},
6146 @option{-gnatyd}, @option{-gnatyu}, and @option{-gnatyx}.
6147
6148 @ifclear vms
6149 @item -
6150 @emph{Remove style check options}
6151 This causes any subsequent options in the string to act as canceling the
6152 corresponding style check option. To cancel maximum nesting level control,
6153 use @option{L} parameter witout any integer value after that, because any
6154 digit following @option{-} in the parameter string of the @option{-gnaty}
6155 option will be threated as canceling indentation check. The same is true
6156 for @option{M} parameter. @option{y} and @option{N} parameters are not
6157 allowed after @option{-}.
6158
6159 @item +
6160 This causes any subsequent options in the string to enable the corresponding
6161 style check option. That is, it cancels the effect of a previous ^-^REMOVE^,
6162 if any.
6163 @end ifclear
6164
6165 @ifset vms
6166 @item NOxxx
6167 @emph{Removing style check options}
6168 If the name of a style check is preceded by @option{NO} then the corresponding
6169 style check is turned off. For example @option{NOCOMMENTS} turns off style
6170 checking for comments.
6171 @end ifset
6172 @end table
6173
6174 @noindent
6175 In the above rules, appearing in column one is always permitted, that is,
6176 counts as meeting either a requirement for a required preceding space,
6177 or as meeting a requirement for no preceding space.
6178
6179 Appearing at the end of a line is also always permitted, that is, counts
6180 as meeting either a requirement for a following space, or as meeting
6181 a requirement for no following space.
6182
6183 @noindent
6184 If any of these style rules is violated, a message is generated giving
6185 details on the violation. The initial characters of such messages are
6186 always ``@code{(style)}''. Note that these messages are treated as warning
6187 messages, so they normally do not prevent the generation of an object
6188 file. The @option{-gnatwe} switch can be used to treat warning messages,
6189 including style messages, as fatal errors.
6190
6191 The switch
6192 @ifclear vms
6193 @option{-gnaty} on its own (that is not
6194 followed by any letters or digits), then the effect is equivalent
6195 to the use of @option{-gnatyy}, as described above, that is all
6196 built-in standard style check options are enabled.
6197
6198 @end ifclear
6199 @ifset vms
6200 /STYLE_CHECKS=ALL_BUILTIN enables all checking options with
6201 the exception of ORDERED_SUBPROGRAMS, UNNECESSARY_BLANK_LINES,
6202 XTRA_PARENS, and DOS_LINE_ENDINGS. In addition
6203 @end ifset
6204
6205
6206
6207 The switch
6208 @ifclear vms
6209 @option{-gnatyN}
6210 @end ifclear
6211 @ifset vms
6212 /STYLE_CHECKS=NONE
6213 @end ifset
6214 clears any previously set style checks.
6215
6216 @node Run-Time Checks
6217 @subsection Run-Time Checks
6218 @cindex Division by zero
6219 @cindex Access before elaboration
6220 @cindex Checks, division by zero
6221 @cindex Checks, access before elaboration
6222 @cindex Checks, stack overflow checking
6223
6224 @noindent
6225 By default, the following checks are suppressed: integer overflow
6226 checks, stack overflow checks, and checks for access before
6227 elaboration on subprogram calls. All other checks, including range
6228 checks and array bounds checks, are turned on by default. The
6229 following @command{gcc} switches refine this default behavior.
6230
6231 @table @option
6232 @c !sort!
6233 @item -gnatp
6234 @cindex @option{-gnatp} (@command{gcc})
6235 @cindex Suppressing checks
6236 @cindex Checks, suppressing
6237 @findex Suppress
6238 Suppress all run-time checks as though @code{pragma Suppress (All_checks)}
6239 had been present in the source. Validity checks are also suppressed (in
6240 other words @option{-gnatp} also implies @option{-gnatVn}.
6241 Use this switch to improve the performance
6242 of the code at the expense of safety in the presence of invalid data or
6243 program bugs.
6244
6245 Note that when checks are suppressed, the compiler is allowed, but not
6246 required, to omit the checking code. If the run-time cost of the
6247 checking code is zero or near-zero, the compiler will generate it even
6248 if checks are suppressed. In particular, if the compiler can prove
6249 that a certain check will necessarily fail, it will generate code to
6250 do an unconditional ``raise'', even if checks are suppressed. The
6251 compiler warns in this case.
6252
6253 Of course, run-time checks are omitted whenever the compiler can prove
6254 that they will not fail, whether or not checks are suppressed.
6255
6256 Note that if you suppress a check that would have failed, program
6257 execution is erroneous, which means the behavior is totally
6258 unpredictable. The program might crash, or print wrong answers, or
6259 do anything else. It might even do exactly what you wanted it to do
6260 (and then it might start failing mysteriously next week or next
6261 year). The compiler will generate code based on the assumption that
6262 the condition being checked is true, which can result in disaster if
6263 that assumption is wrong.
6264
6265 @item -gnato
6266 @cindex @option{-gnato} (@command{gcc})
6267 @cindex Overflow checks
6268 @cindex Check, overflow
6269 Enables overflow checking for integer operations.
6270 This causes GNAT to generate slower and larger executable
6271 programs by adding code to check for overflow (resulting in raising
6272 @code{Constraint_Error} as required by standard Ada
6273 semantics). These overflow checks correspond to situations in which
6274 the true value of the result of an operation may be outside the base
6275 range of the result type. The following example shows the distinction:
6276
6277 @smallexample @c ada
6278 X1 : Integer := "Integer'Last";
6279 X2 : Integer range 1 .. 5 := "5";
6280 X3 : Integer := "Integer'Last";
6281 X4 : Integer range 1 .. 5 := "5";
6282 F : Float := "2.0E+20";
6283 @dots{}
6284 X1 := X1 + 1;
6285 X2 := X2 + 1;
6286 X3 := Integer (F);
6287 X4 := Integer (F);
6288 @end smallexample
6289
6290 @noindent
6291 Note that if explicit values are assigned at compile time, the
6292 compiler may be able to detect overflow at compile time, in which case
6293 no actual run-time checking code is required, and Constraint_Error
6294 will be raised unconditionally, with or without
6295 @option{-gnato}. That's why the assigned values in the above fragment
6296 are in quotes, the meaning is "assign a value not known to the
6297 compiler that happens to be equal to ...". The remaining discussion
6298 assumes that the compiler cannot detect the values at compile time.
6299
6300 Here the first addition results in a value that is outside the base range
6301 of Integer, and hence requires an overflow check for detection of the
6302 constraint error. Thus the first assignment to @code{X1} raises a
6303 @code{Constraint_Error} exception only if @option{-gnato} is set.
6304
6305 The second increment operation results in a violation of the explicit
6306 range constraint; such range checks are performed by default, and are
6307 unaffected by @option{-gnato}.
6308
6309 The two conversions of @code{F} both result in values that are outside
6310 the base range of type @code{Integer} and thus will raise
6311 @code{Constraint_Error} exceptions only if @option{-gnato} is used.
6312 The fact that the result of the second conversion is assigned to
6313 variable @code{X4} with a restricted range is irrelevant, since the problem
6314 is in the conversion, not the assignment.
6315
6316 Basically the rule is that in the default mode (@option{-gnato} not
6317 used), the generated code assures that all integer variables stay
6318 within their declared ranges, or within the base range if there is
6319 no declared range. This prevents any serious problems like indexes
6320 out of range for array operations.
6321
6322 What is not checked in default mode is an overflow that results in
6323 an in-range, but incorrect value. In the above example, the assignments
6324 to @code{X1}, @code{X2}, @code{X3} all give results that are within the
6325 range of the target variable, but the result is wrong in the sense that
6326 it is too large to be represented correctly. Typically the assignment
6327 to @code{X1} will result in wrap around to the largest negative number.
6328 The conversions of @code{F} will result in some @code{Integer} value
6329 and if that integer value is out of the @code{X4} range then the
6330 subsequent assignment would generate an exception.
6331
6332 @findex Machine_Overflows
6333 Note that the @option{-gnato} switch does not affect the code generated
6334 for any floating-point operations; it applies only to integer
6335 semantics).
6336 For floating-point, GNAT has the @code{Machine_Overflows}
6337 attribute set to @code{False} and the normal mode of operation is to
6338 generate IEEE NaN and infinite values on overflow or invalid operations
6339 (such as dividing 0.0 by 0.0).
6340
6341 The reason that we distinguish overflow checking from other kinds of
6342 range constraint checking is that a failure of an overflow check, unlike
6343 for example the failure of a range check, can result in an incorrect
6344 value, but cannot cause random memory destruction (like an out of range
6345 subscript), or a wild jump (from an out of range case value). Overflow
6346 checking is also quite expensive in time and space, since in general it
6347 requires the use of double length arithmetic.
6348
6349 Note again that @option{-gnato} is off by default, so overflow checking is
6350 not performed in default mode. This means that out of the box, with the
6351 default settings, GNAT does not do all the checks expected from the
6352 language description in the Ada Reference Manual. If you want all constraint
6353 checks to be performed, as described in this Manual, then you must
6354 explicitly use the -gnato switch either on the @command{gnatmake} or
6355 @command{gcc} command.
6356
6357 @item -gnatE
6358 @cindex @option{-gnatE} (@command{gcc})
6359 @cindex Elaboration checks
6360 @cindex Check, elaboration
6361 Enables dynamic checks for access-before-elaboration
6362 on subprogram calls and generic instantiations.
6363 Note that @option{-gnatE} is not necessary for safety, because in the
6364 default mode, GNAT ensures statically that the checks would not fail.
6365 For full details of the effect and use of this switch,
6366 @xref{Compiling Using gcc}.
6367
6368 @item -fstack-check
6369 @cindex @option{-fstack-check} (@command{gcc})
6370 @cindex Stack Overflow Checking
6371 @cindex Checks, stack overflow checking
6372 Activates stack overflow checking. For full details of the effect and use of
6373 this switch see @ref{Stack Overflow Checking}.
6374 @end table
6375
6376 @findex Unsuppress
6377 @noindent
6378 The setting of these switches only controls the default setting of the
6379 checks. You may modify them using either @code{Suppress} (to remove
6380 checks) or @code{Unsuppress} (to add back suppressed checks) pragmas in
6381 the program source.
6382
6383 @node Using gcc for Syntax Checking
6384 @subsection Using @command{gcc} for Syntax Checking
6385 @table @option
6386 @item -gnats
6387 @cindex @option{-gnats} (@command{gcc})
6388 @ifclear vms
6389
6390 @noindent
6391 The @code{s} stands for ``syntax''.
6392 @end ifclear
6393
6394 Run GNAT in syntax checking only mode. For
6395 example, the command
6396
6397 @smallexample
6398 $ gcc -c -gnats x.adb
6399 @end smallexample
6400
6401 @noindent
6402 compiles file @file{x.adb} in syntax-check-only mode. You can check a
6403 series of files in a single command
6404 @ifclear vms
6405 , and can use wild cards to specify such a group of files.
6406 Note that you must specify the @option{-c} (compile
6407 only) flag in addition to the @option{-gnats} flag.
6408 @end ifclear
6409 .
6410 You may use other switches in conjunction with @option{-gnats}. In
6411 particular, @option{-gnatl} and @option{-gnatv} are useful to control the
6412 format of any generated error messages.
6413
6414 When the source file is empty or contains only empty lines and/or comments,
6415 the output is a warning:
6416
6417 @smallexample
6418 $ gcc -c -gnats -x ada toto.txt
6419 toto.txt:1:01: warning: empty file, contains no compilation units
6420 $
6421 @end smallexample
6422
6423 Otherwise, the output is simply the error messages, if any. No object file or
6424 ALI file is generated by a syntax-only compilation. Also, no units other
6425 than the one specified are accessed. For example, if a unit @code{X}
6426 @code{with}'s a unit @code{Y}, compiling unit @code{X} in syntax
6427 check only mode does not access the source file containing unit
6428 @code{Y}.
6429
6430 @cindex Multiple units, syntax checking
6431 Normally, GNAT allows only a single unit in a source file. However, this
6432 restriction does not apply in syntax-check-only mode, and it is possible
6433 to check a file containing multiple compilation units concatenated
6434 together. This is primarily used by the @code{gnatchop} utility
6435 (@pxref{Renaming Files Using gnatchop}).
6436 @end table
6437
6438 @node Using gcc for Semantic Checking
6439 @subsection Using @command{gcc} for Semantic Checking
6440 @table @option
6441 @item -gnatc
6442 @cindex @option{-gnatc} (@command{gcc})
6443
6444 @ifclear vms
6445 @noindent
6446 The @code{c} stands for ``check''.
6447 @end ifclear
6448 Causes the compiler to operate in semantic check mode,
6449 with full checking for all illegalities specified in the
6450 Ada Reference Manual, but without generation of any object code
6451 (no object file is generated).
6452
6453 Because dependent files must be accessed, you must follow the GNAT
6454 semantic restrictions on file structuring to operate in this mode:
6455
6456 @itemize @bullet
6457 @item
6458 The needed source files must be accessible
6459 (@pxref{Search Paths and the Run-Time Library (RTL)}).
6460
6461 @item
6462 Each file must contain only one compilation unit.
6463
6464 @item
6465 The file name and unit name must match (@pxref{File Naming Rules}).
6466 @end itemize
6467
6468 The output consists of error messages as appropriate. No object file is
6469 generated. An @file{ALI} file is generated for use in the context of
6470 cross-reference tools, but this file is marked as not being suitable
6471 for binding (since no object file is generated).
6472 The checking corresponds exactly to the notion of
6473 legality in the Ada Reference Manual.
6474
6475 Any unit can be compiled in semantics-checking-only mode, including
6476 units that would not normally be compiled (subunits,
6477 and specifications where a separate body is present).
6478 @end table
6479
6480 @node Compiling Different Versions of Ada
6481 @subsection Compiling Different Versions of Ada
6482
6483 @noindent
6484 The switches described in this section allow you to explicitly specify
6485 the version of the Ada language that your programs are written in.
6486 By default @value{EDITION} assumes @value{DEFAULTLANGUAGEVERSION},
6487 but you can also specify @value{NONDEFAULTLANGUAGEVERSION} or
6488 indicate Ada 83 compatibility mode.
6489
6490 @table @option
6491 @cindex Compatibility with Ada 83
6492
6493 @item -gnat83 (Ada 83 Compatibility Mode)
6494 @cindex @option{-gnat83} (@command{gcc})
6495 @cindex ACVC, Ada 83 tests
6496 @cindex Ada 83 mode
6497
6498 @noindent
6499 Although GNAT is primarily an Ada 95 / Ada 2005 compiler, this switch
6500 specifies that the program is to be compiled in Ada 83 mode. With
6501 @option{-gnat83}, GNAT rejects most post-Ada 83 extensions and applies Ada 83
6502 semantics where this can be done easily.
6503 It is not possible to guarantee this switch does a perfect
6504 job; some subtle tests, such as are
6505 found in earlier ACVC tests (and that have been removed from the ACATS suite
6506 for Ada 95), might not compile correctly.
6507 Nevertheless, this switch may be useful in some circumstances, for example
6508 where, due to contractual reasons, existing code needs to be maintained
6509 using only Ada 83 features.
6510
6511 With few exceptions (most notably the need to use @code{<>} on
6512 @cindex Generic formal parameters
6513 unconstrained generic formal parameters, the use of the new Ada 95 / Ada 2005
6514 reserved words, and the use of packages
6515 with optional bodies), it is not necessary to specify the
6516 @option{-gnat83} switch when compiling Ada 83 programs, because, with rare
6517 exceptions, Ada 95 and Ada 2005 are upwardly compatible with Ada 83. Thus
6518 a correct Ada 83 program is usually also a correct program
6519 in these later versions of the language standard.
6520 For further information, please refer to @ref{Compatibility and Porting Guide}.
6521
6522 @item -gnat95 (Ada 95 mode)
6523 @cindex @option{-gnat95} (@command{gcc})
6524 @cindex Ada 95 mode
6525
6526 @noindent
6527 This switch directs the compiler to implement the Ada 95 version of the
6528 language.
6529 Since Ada 95 is almost completely upwards
6530 compatible with Ada 83, Ada 83 programs may generally be compiled using
6531 this switch (see the description of the @option{-gnat83} switch for further
6532 information about Ada 83 mode).
6533 If an Ada 2005 program is compiled in Ada 95 mode,
6534 uses of the new Ada 2005 features will cause error
6535 messages or warnings.
6536
6537 This switch also can be used to cancel the effect of a previous
6538 @option{-gnat83} or @option{-gnat05} switch earlier in the command line.
6539
6540 @item -gnat05 (Ada 2005 mode)
6541 @cindex @option{-gnat05} (@command{gcc})
6542 @cindex Ada 2005 mode
6543
6544 @noindent
6545 This switch directs the compiler to implement the Ada 2005 version of the
6546 language.
6547 Since Ada 2005 is almost completely upwards
6548 compatible with Ada 95 (and thus also with Ada 83), Ada 83 and Ada 95 programs
6549 may generally be compiled using this switch (see the description of the
6550 @option{-gnat83} and @option{-gnat95} switches for further
6551 information).
6552
6553 For information about the approved ``Ada Issues'' that have been incorporated
6554 into Ada 2005, see @url{http://www.ada-auth.org/cgi-bin/cvsweb.cgi/AIs}.
6555 Included with GNAT releases is a file @file{features-ada0y} that describes
6556 the set of implemented Ada 2005 features.
6557 @end table
6558
6559
6560 @node Character Set Control
6561 @subsection Character Set Control
6562 @table @option
6563 @item ^-gnati^/IDENTIFIER_CHARACTER_SET=^@var{c}
6564 @cindex @option{^-gnati^/IDENTIFIER_CHARACTER_SET^} (@command{gcc})
6565
6566 @noindent
6567 Normally GNAT recognizes the Latin-1 character set in source program
6568 identifiers, as described in the Ada Reference Manual.
6569 This switch causes
6570 GNAT to recognize alternate character sets in identifiers. @var{c} is a
6571 single character ^^or word^ indicating the character set, as follows:
6572
6573 @table @code
6574 @item 1
6575 ISO 8859-1 (Latin-1) identifiers
6576
6577 @item 2
6578 ISO 8859-2 (Latin-2) letters allowed in identifiers
6579
6580 @item 3
6581 ISO 8859-3 (Latin-3) letters allowed in identifiers
6582
6583 @item 4
6584 ISO 8859-4 (Latin-4) letters allowed in identifiers
6585
6586 @item 5
6587 ISO 8859-5 (Cyrillic) letters allowed in identifiers
6588
6589 @item 9
6590 ISO 8859-15 (Latin-9) letters allowed in identifiers
6591
6592 @item ^p^PC^
6593 IBM PC letters (code page 437) allowed in identifiers
6594
6595 @item ^8^PC850^
6596 IBM PC letters (code page 850) allowed in identifiers
6597
6598 @item ^f^FULL_UPPER^
6599 Full upper-half codes allowed in identifiers
6600
6601 @item ^n^NO_UPPER^
6602 No upper-half codes allowed in identifiers
6603
6604 @item ^w^WIDE^
6605 Wide-character codes (that is, codes greater than 255)
6606 allowed in identifiers
6607 @end table
6608
6609 @xref{Foreign Language Representation}, for full details on the
6610 implementation of these character sets.
6611
6612 @item ^-gnatW^/WIDE_CHARACTER_ENCODING=^@var{e}
6613 @cindex @option{^-gnatW^/WIDE_CHARACTER_ENCODING^} (@command{gcc})
6614 Specify the method of encoding for wide characters.
6615 @var{e} is one of the following:
6616
6617 @table @code
6618
6619 @item ^h^HEX^
6620 Hex encoding (brackets coding also recognized)
6621
6622 @item ^u^UPPER^
6623 Upper half encoding (brackets encoding also recognized)
6624
6625 @item ^s^SHIFT_JIS^
6626 Shift/JIS encoding (brackets encoding also recognized)
6627
6628 @item ^e^EUC^
6629 EUC encoding (brackets encoding also recognized)
6630
6631 @item ^8^UTF8^
6632 UTF-8 encoding (brackets encoding also recognized)
6633
6634 @item ^b^BRACKETS^
6635 Brackets encoding only (default value)
6636 @end table
6637 For full details on these encoding
6638 methods see @ref{Wide Character Encodings}.
6639 Note that brackets coding is always accepted, even if one of the other
6640 options is specified, so for example @option{-gnatW8} specifies that both
6641 brackets and UTF-8 encodings will be recognized. The units that are
6642 with'ed directly or indirectly will be scanned using the specified
6643 representation scheme, and so if one of the non-brackets scheme is
6644 used, it must be used consistently throughout the program. However,
6645 since brackets encoding is always recognized, it may be conveniently
6646 used in standard libraries, allowing these libraries to be used with
6647 any of the available coding schemes.
6648 scheme.
6649
6650 If no @option{-gnatW?} parameter is present, then the default
6651 representation is normally Brackets encoding only. However, if the
6652 first three characters of the file are 16#EF# 16#BB# 16#BF# (the standard
6653 byte order mark or BOM for UTF-8), then these three characters are
6654 skipped and the default representation for the file is set to UTF-8.
6655
6656 Note that the wide character representation that is specified (explicitly
6657 or by default) for the main program also acts as the default encoding used
6658 for Wide_Text_IO files if not specifically overridden by a WCEM form
6659 parameter.
6660
6661 @end table
6662 @node File Naming Control
6663 @subsection File Naming Control
6664
6665 @table @option
6666 @item ^-gnatk^/FILE_NAME_MAX_LENGTH=^@var{n}
6667 @cindex @option{-gnatk} (@command{gcc})
6668 Activates file name ``krunching''. @var{n}, a decimal integer in the range
6669 1-999, indicates the maximum allowable length of a file name (not
6670 including the @file{.ads} or @file{.adb} extension). The default is not
6671 to enable file name krunching.
6672
6673 For the source file naming rules, @xref{File Naming Rules}.
6674 @end table
6675
6676 @node Subprogram Inlining Control
6677 @subsection Subprogram Inlining Control
6678
6679 @table @option
6680 @c !sort!
6681 @item -gnatn
6682 @cindex @option{-gnatn} (@command{gcc})
6683 @ifclear vms
6684 The @code{n} here is intended to suggest the first syllable of the
6685 word ``inline''.
6686 @end ifclear
6687 GNAT recognizes and processes @code{Inline} pragmas. However, for the
6688 inlining to actually occur, optimization must be enabled. To enable
6689 inlining of subprograms specified by pragma @code{Inline},
6690 you must also specify this switch.
6691 In the absence of this switch, GNAT does not attempt
6692 inlining and does not need to access the bodies of
6693 subprograms for which @code{pragma Inline} is specified if they are not
6694 in the current unit.
6695
6696 If you specify this switch the compiler will access these bodies,
6697 creating an extra source dependency for the resulting object file, and
6698 where possible, the call will be inlined.
6699 For further details on when inlining is possible
6700 see @ref{Inlining of Subprograms}.
6701
6702 @item -gnatN
6703 @cindex @option{-gnatN} (@command{gcc})
6704 The front end inlining activated by this switch is generally more extensive,
6705 and quite often more effective than the standard @option{-gnatn} inlining mode.
6706 It will also generate additional dependencies.
6707 Note that
6708 @option{-gnatN} automatically implies @option{-gnatn} so it is not necessary
6709 to specify both options.
6710 @end table
6711
6712 @node Auxiliary Output Control
6713 @subsection Auxiliary Output Control
6714
6715 @table @option
6716 @item -gnatt
6717 @cindex @option{-gnatt} (@command{gcc})
6718 @cindex Writing internal trees
6719 @cindex Internal trees, writing to file
6720 Causes GNAT to write the internal tree for a unit to a file (with the
6721 extension @file{.adt}.
6722 This not normally required, but is used by separate analysis tools.
6723 Typically
6724 these tools do the necessary compilations automatically, so you should
6725 not have to specify this switch in normal operation.
6726
6727 @item -gnatu
6728 @cindex @option{-gnatu} (@command{gcc})
6729 Print a list of units required by this compilation on @file{stdout}.
6730 The listing includes all units on which the unit being compiled depends
6731 either directly or indirectly.
6732
6733 @ifclear vms
6734 @item -pass-exit-codes
6735 @cindex @option{-pass-exit-codes} (@command{gcc})
6736 If this switch is not used, the exit code returned by @command{gcc} when
6737 compiling multiple files indicates whether all source files have
6738 been successfully used to generate object files or not.
6739
6740 When @option{-pass-exit-codes} is used, @command{gcc} exits with an extended
6741 exit status and allows an integrated development environment to better
6742 react to a compilation failure. Those exit status are:
6743
6744 @table @asis
6745 @item 5
6746 There was an error in at least one source file.
6747 @item 3
6748 At least one source file did not generate an object file.
6749 @item 2
6750 The compiler died unexpectedly (internal error for example).
6751 @item 0
6752 An object file has been generated for every source file.
6753 @end table
6754 @end ifclear
6755 @end table
6756
6757 @node Debugging Control
6758 @subsection Debugging Control
6759
6760 @table @option
6761 @c !sort!
6762 @cindex Debugging options
6763 @ifclear vms
6764 @item -gnatd@var{x}
6765 @cindex @option{-gnatd} (@command{gcc})
6766 Activate internal debugging switches. @var{x} is a letter or digit, or
6767 string of letters or digits, which specifies the type of debugging
6768 outputs desired. Normally these are used only for internal development
6769 or system debugging purposes. You can find full documentation for these
6770 switches in the body of the @code{Debug} unit in the compiler source
6771 file @file{debug.adb}.
6772 @end ifclear
6773
6774 @item -gnatG
6775 @cindex @option{-gnatG} (@command{gcc})
6776 This switch causes the compiler to generate auxiliary output containing
6777 a pseudo-source listing of the generated expanded code. Like most Ada
6778 compilers, GNAT works by first transforming the high level Ada code into
6779 lower level constructs. For example, tasking operations are transformed
6780 into calls to the tasking run-time routines. A unique capability of GNAT
6781 is to list this expanded code in a form very close to normal Ada source.
6782 This is very useful in understanding the implications of various Ada
6783 usage on the efficiency of the generated code. There are many cases in
6784 Ada (e.g.@: the use of controlled types), where simple Ada statements can
6785 generate a lot of run-time code. By using @option{-gnatG} you can identify
6786 these cases, and consider whether it may be desirable to modify the coding
6787 approach to improve efficiency.
6788
6789 The format of the output is very similar to standard Ada source, and is
6790 easily understood by an Ada programmer. The following special syntactic
6791 additions correspond to low level features used in the generated code that
6792 do not have any exact analogies in pure Ada source form. The following
6793 is a partial list of these special constructions. See the spec
6794 of package @code{Sprint} in file @file{sprint.ads} for a full list.
6795
6796 If the switch @option{-gnatL} is used in conjunction with
6797 @cindex @option{-gnatL} (@command{gcc})
6798 @option{-gnatG}, then the original source lines are interspersed
6799 in the expanded source (as comment lines with the original line number).
6800
6801 @table @code
6802 @item new @var{xxx} @r{[}storage_pool = @var{yyy}@r{]}
6803 Shows the storage pool being used for an allocator.
6804
6805 @item at end @var{procedure-name};
6806 Shows the finalization (cleanup) procedure for a scope.
6807
6808 @item (if @var{expr} then @var{expr} else @var{expr})
6809 Conditional expression equivalent to the @code{x?y:z} construction in C.
6810
6811 @item @var{target}^^^(@var{source})
6812 A conversion with floating-point truncation instead of rounding.
6813
6814 @item @var{target}?(@var{source})
6815 A conversion that bypasses normal Ada semantic checking. In particular
6816 enumeration types and fixed-point types are treated simply as integers.
6817
6818 @item @var{target}?^^^(@var{source})
6819 Combines the above two cases.
6820
6821 @item @var{x} #/ @var{y}
6822 @itemx @var{x} #mod @var{y}
6823 @itemx @var{x} #* @var{y}
6824 @itemx @var{x} #rem @var{y}
6825 A division or multiplication of fixed-point values which are treated as
6826 integers without any kind of scaling.
6827
6828 @item free @var{expr} @r{[}storage_pool = @var{xxx}@r{]}
6829 Shows the storage pool associated with a @code{free} statement.
6830
6831 @item [subtype or type declaration]
6832 Used to list an equivalent declaration for an internally generated
6833 type that is referenced elsewhere in the listing.
6834
6835 @item freeze @var{type-name} @ovar{actions}
6836 Shows the point at which @var{type-name} is frozen, with possible
6837 associated actions to be performed at the freeze point.
6838
6839 @item reference @var{itype}
6840 Reference (and hence definition) to internal type @var{itype}.
6841
6842 @item @var{function-name}! (@var{arg}, @var{arg}, @var{arg})
6843 Intrinsic function call.
6844
6845 @item @var{label-name} : label
6846 Declaration of label @var{labelname}.
6847
6848 @item #$ @var{subprogram-name}
6849 An implicit call to a run-time support routine
6850 (to meet the requirement of H.3.1(9) in a
6851 convenient manner).
6852
6853 @item @var{expr} && @var{expr} && @var{expr} @dots{} && @var{expr}
6854 A multiple concatenation (same effect as @var{expr} & @var{expr} &
6855 @var{expr}, but handled more efficiently).
6856
6857 @item [constraint_error]
6858 Raise the @code{Constraint_Error} exception.
6859
6860 @item @var{expression}'reference
6861 A pointer to the result of evaluating @var{expression}.
6862
6863 @item @var{target-type}!(@var{source-expression})
6864 An unchecked conversion of @var{source-expression} to @var{target-type}.
6865
6866 @item [@var{numerator}/@var{denominator}]
6867 Used to represent internal real literals (that) have no exact
6868 representation in base 2-16 (for example, the result of compile time
6869 evaluation of the expression 1.0/27.0).
6870 @end table
6871
6872 @item -gnatD
6873 @cindex @option{-gnatD} (@command{gcc})
6874 When used in conjunction with @option{-gnatG}, this switch causes
6875 the expanded source, as described above for
6876 @option{-gnatG} to be written to files with names
6877 @file{^xxx.dg^XXX_DG^}, where @file{xxx} is the normal file name,
6878 instead of to the standard output file. For
6879 example, if the source file name is @file{hello.adb}, then a file
6880 @file{^hello.adb.dg^HELLO.ADB_DG^} will be written. The debugging
6881 information generated by the @command{gcc} @option{^-g^/DEBUG^} switch
6882 will refer to the generated @file{^xxx.dg^XXX_DG^} file. This allows
6883 you to do source level debugging using the generated code which is
6884 sometimes useful for complex code, for example to find out exactly
6885 which part of a complex construction raised an exception. This switch
6886 also suppress generation of cross-reference information (see
6887 @option{-gnatx}) since otherwise the cross-reference information
6888 would refer to the @file{^.dg^.DG^} file, which would cause
6889 confusion since this is not the original source file.
6890
6891 Note that @option{-gnatD} actually implies @option{-gnatG}
6892 automatically, so it is not necessary to give both options.
6893 In other words @option{-gnatD} is equivalent to @option{-gnatDG}).
6894
6895 If the switch @option{-gnatL} is used in conjunction with
6896 @cindex @option{-gnatL} (@command{gcc})
6897 @option{-gnatDG}, then the original source lines are interspersed
6898 in the expanded source (as comment lines with the original line number).
6899
6900 @item -gnatr
6901 @cindex @option{-gnatr} (@command{gcc})
6902 @cindex pragma Restrictions
6903 This switch causes pragma Restrictions to be treated as Restriction_Warnings
6904 so that violation of restrictions causes warnings rather than illegalities.
6905 This is useful during the development process when new restrictions are added
6906 or investigated. The switch also causes pragma Profile to be treated as
6907 Profile_Warnings, and pragma Restricted_Run_Time and pragma Ravenscar set
6908 restriction warnings rather than restrictions.
6909
6910 @ifclear vms
6911 @item -gnatR@r{[}0@r{|}1@r{|}2@r{|}3@r{[}s@r{]]}
6912 @cindex @option{-gnatR} (@command{gcc})
6913 This switch controls output from the compiler of a listing showing
6914 representation information for declared types and objects. For
6915 @option{-gnatR0}, no information is output (equivalent to omitting
6916 the @option{-gnatR} switch). For @option{-gnatR1} (which is the default,
6917 so @option{-gnatR} with no parameter has the same effect), size and alignment
6918 information is listed for declared array and record types. For
6919 @option{-gnatR2}, size and alignment information is listed for all
6920 declared types and objects. Finally @option{-gnatR3} includes symbolic
6921 expressions for values that are computed at run time for
6922 variant records. These symbolic expressions have a mostly obvious
6923 format with #n being used to represent the value of the n'th
6924 discriminant. See source files @file{repinfo.ads/adb} in the
6925 @code{GNAT} sources for full details on the format of @option{-gnatR3}
6926 output. If the switch is followed by an s (e.g.@: @option{-gnatR2s}), then
6927 the output is to a file with the name @file{^file.rep^file_REP^} where
6928 file is the name of the corresponding source file.
6929 @end ifclear
6930 @ifset vms
6931 @item /REPRESENTATION_INFO
6932 @cindex @option{/REPRESENTATION_INFO} (@command{gcc})
6933 This qualifier controls output from the compiler of a listing showing
6934 representation information for declared types and objects. For
6935 @option{/REPRESENTATION_INFO=NONE}, no information is output
6936 (equivalent to omitting the @option{/REPRESENTATION_INFO} qualifier).
6937 @option{/REPRESENTATION_INFO} without option is equivalent to
6938 @option{/REPRESENTATION_INFO=ARRAYS}.
6939 For @option{/REPRESENTATION_INFO=ARRAYS}, size and alignment
6940 information is listed for declared array and record types. For
6941 @option{/REPRESENTATION_INFO=OBJECTS}, size and alignment information
6942 is listed for all expression information for values that are computed
6943 at run time for variant records. These symbolic expressions have a mostly
6944 obvious format with #n being used to represent the value of the n'th
6945 discriminant. See source files @file{REPINFO.ADS/ADB} in the
6946 @code{GNAT} sources for full details on the format of
6947 @option{/REPRESENTATION_INFO=SYMBOLIC} output.
6948 If _FILE is added at the end of an option
6949 (e.g.@: @option{/REPRESENTATION_INFO=ARRAYS_FILE}),
6950 then the output is to a file with the name @file{file_REP} where
6951 file is the name of the corresponding source file.
6952 @end ifset
6953 Note that it is possible for record components to have zero size. In
6954 this case, the component clause uses an obvious extension of permitted
6955 Ada syntax, for example @code{at 0 range 0 .. -1}.
6956
6957 Representation information requires that code be generated (since it is the
6958 code generator that lays out complex data structures). If an attempt is made
6959 to output representation information when no code is generated, for example
6960 when a subunit is compiled on its own, then no information can be generated
6961 and the compiler outputs a message to this effect.
6962
6963 @item -gnatS
6964 @cindex @option{-gnatS} (@command{gcc})
6965 The use of the switch @option{-gnatS} for an
6966 Ada compilation will cause the compiler to output a
6967 representation of package Standard in a form very
6968 close to standard Ada. It is not quite possible to
6969 do this entirely in standard Ada (since new
6970 numeric base types cannot be created in standard
6971 Ada), but the output is easily
6972 readable to any Ada programmer, and is useful to
6973 determine the characteristics of target dependent
6974 types in package Standard.
6975
6976 @item -gnatx
6977 @cindex @option{-gnatx} (@command{gcc})
6978 Normally the compiler generates full cross-referencing information in
6979 the @file{ALI} file. This information is used by a number of tools,
6980 including @code{gnatfind} and @code{gnatxref}. The @option{-gnatx} switch
6981 suppresses this information. This saves some space and may slightly
6982 speed up compilation, but means that these tools cannot be used.
6983 @end table
6984
6985 @node Exception Handling Control
6986 @subsection Exception Handling Control
6987
6988 @noindent
6989 GNAT uses two methods for handling exceptions at run-time. The
6990 @code{setjmp/longjmp} method saves the context when entering
6991 a frame with an exception handler. Then when an exception is
6992 raised, the context can be restored immediately, without the
6993 need for tracing stack frames. This method provides very fast
6994 exception propagation, but introduces significant overhead for
6995 the use of exception handlers, even if no exception is raised.
6996
6997 The other approach is called ``zero cost'' exception handling.
6998 With this method, the compiler builds static tables to describe
6999 the exception ranges. No dynamic code is required when entering
7000 a frame containing an exception handler. When an exception is
7001 raised, the tables are used to control a back trace of the
7002 subprogram invocation stack to locate the required exception
7003 handler. This method has considerably poorer performance for
7004 the propagation of exceptions, but there is no overhead for
7005 exception handlers if no exception is raised. Note that in this
7006 mode and in the context of mixed Ada and C/C++ programming,
7007 to propagate an exception through a C/C++ code, the C/C++ code
7008 must be compiled with the @option{-funwind-tables} GCC's
7009 option.
7010
7011 The following switches may be used to control which of the
7012 two exception handling methods is used.
7013
7014 @table @option
7015 @c !sort!
7016
7017 @item --RTS=sjlj
7018 @cindex @option{--RTS=sjlj} (@command{gnatmake})
7019 This switch causes the setjmp/longjmp run-time (when available) to be used
7020 for exception handling. If the default
7021 mechanism for the target is zero cost exceptions, then
7022 this switch can be used to modify this default, and must be
7023 used for all units in the partition.
7024 This option is rarely used. One case in which it may be
7025 advantageous is if you have an application where exception
7026 raising is common and the overall performance of the
7027 application is improved by favoring exception propagation.
7028
7029 @item --RTS=zcx
7030 @cindex @option{--RTS=zcx} (@command{gnatmake})
7031 @cindex Zero Cost Exceptions
7032 This switch causes the zero cost approach to be used
7033 for exception handling. If this is the default mechanism for the
7034 target (see below), then this switch is unneeded. If the default
7035 mechanism for the target is setjmp/longjmp exceptions, then
7036 this switch can be used to modify this default, and must be
7037 used for all units in the partition.
7038 This option can only be used if the zero cost approach
7039 is available for the target in use, otherwise it will generate an error.
7040 @end table
7041
7042 @noindent
7043 The same option @option{--RTS} must be used both for @command{gcc}
7044 and @command{gnatbind}. Passing this option to @command{gnatmake}
7045 (@pxref{Switches for gnatmake}) will ensure the required consistency
7046 through the compilation and binding steps.
7047
7048 @node Units to Sources Mapping Files
7049 @subsection Units to Sources Mapping Files
7050
7051 @table @option
7052
7053 @item -gnatem^^=^@var{path}
7054 @cindex @option{-gnatem} (@command{gcc})
7055 A mapping file is a way to communicate to the compiler two mappings:
7056 from unit names to file names (without any directory information) and from
7057 file names to path names (with full directory information). These mappings
7058 are used by the compiler to short-circuit the path search.
7059
7060 The use of mapping files is not required for correct operation of the
7061 compiler, but mapping files can improve efficiency, particularly when
7062 sources are read over a slow network connection. In normal operation,
7063 you need not be concerned with the format or use of mapping files,
7064 and the @option{-gnatem} switch is not a switch that you would use
7065 explicitly. it is intended only for use by automatic tools such as
7066 @command{gnatmake} running under the project file facility. The
7067 description here of the format of mapping files is provided
7068 for completeness and for possible use by other tools.
7069
7070 A mapping file is a sequence of sets of three lines. In each set,
7071 the first line is the unit name, in lower case, with ``@code{%s}''
7072 appended for
7073 specs and ``@code{%b}'' appended for bodies; the second line is the
7074 file name; and the third line is the path name.
7075
7076 Example:
7077 @smallexample
7078 main%b
7079 main.2.ada
7080 /gnat/project1/sources/main.2.ada
7081 @end smallexample
7082
7083 When the switch @option{-gnatem} is specified, the compiler will create
7084 in memory the two mappings from the specified file. If there is any problem
7085 (nonexistent file, truncated file or duplicate entries), no mapping will
7086 be created.
7087
7088 Several @option{-gnatem} switches may be specified; however, only the last
7089 one on the command line will be taken into account.
7090
7091 When using a project file, @command{gnatmake} create a temporary mapping file
7092 and communicates it to the compiler using this switch.
7093
7094 @end table
7095
7096 @node Integrated Preprocessing
7097 @subsection Integrated Preprocessing
7098
7099 @noindent
7100 GNAT sources may be preprocessed immediately before compilation.
7101 In this case, the actual
7102 text of the source is not the text of the source file, but is derived from it
7103 through a process called preprocessing. Integrated preprocessing is specified
7104 through switches @option{-gnatep} and/or @option{-gnateD}. @option{-gnatep}
7105 indicates, through a text file, the preprocessing data to be used.
7106 @option{-gnateD} specifies or modifies the values of preprocessing symbol.
7107
7108 @noindent
7109 Note that when integrated preprocessing is used, the output from the
7110 preprocessor is not written to any external file. Instead it is passed
7111 internally to the compiler. If you need to preserve the result of
7112 preprocessing in a file, then you should use @command{gnatprep}
7113 to perform the desired preprocessing in stand-alone mode.
7114
7115 @noindent
7116 It is recommended that @command{gnatmake} switch ^-s^/SWITCH_CHECK^ should be
7117 used when Integrated Preprocessing is used. The reason is that preprocessing
7118 with another Preprocessing Data file without changing the sources will
7119 not trigger recompilation without this switch.
7120
7121 @noindent
7122 Note that @command{gnatmake} switch ^-m^/MINIMAL_RECOMPILATION^ will almost
7123 always trigger recompilation for sources that are preprocessed,
7124 because @command{gnatmake} cannot compute the checksum of the source after
7125 preprocessing.
7126
7127 @noindent
7128 The actual preprocessing function is described in details in section
7129 @ref{Preprocessing Using gnatprep}. This section only describes how integrated
7130 preprocessing is triggered and parameterized.
7131
7132 @table @code
7133
7134 @item -gnatep=@var{file}
7135 @cindex @option{-gnatep} (@command{gcc})
7136 This switch indicates to the compiler the file name (without directory
7137 information) of the preprocessor data file to use. The preprocessor data file
7138 should be found in the source directories.
7139
7140 @noindent
7141 A preprocessing data file is a text file with significant lines indicating
7142 how should be preprocessed either a specific source or all sources not
7143 mentioned in other lines. A significant line is a nonempty, non-comment line.
7144 Comments are similar to Ada comments.
7145
7146 @noindent
7147 Each significant line starts with either a literal string or the character '*'.
7148 A literal string is the file name (without directory information) of the source
7149 to preprocess. A character '*' indicates the preprocessing for all the sources
7150 that are not specified explicitly on other lines (order of the lines is not
7151 significant). It is an error to have two lines with the same file name or two
7152 lines starting with the character '*'.
7153
7154 @noindent
7155 After the file name or the character '*', another optional literal string
7156 indicating the file name of the definition file to be used for preprocessing
7157 (@pxref{Form of Definitions File}). The definition files are found by the
7158 compiler in one of the source directories. In some cases, when compiling
7159 a source in a directory other than the current directory, if the definition
7160 file is in the current directory, it may be necessary to add the current
7161 directory as a source directory through switch ^-I.^/SEARCH=[]^, otherwise
7162 the compiler would not find the definition file.
7163
7164 @noindent
7165 Then, optionally, ^switches^switches^ similar to those of @code{gnatprep} may
7166 be found. Those ^switches^switches^ are:
7167
7168 @table @code
7169
7170 @item -b
7171 Causes both preprocessor lines and the lines deleted by
7172 preprocessing to be replaced by blank lines, preserving the line number.
7173 This ^switch^switch^ is always implied; however, if specified after @option{-c}
7174 it cancels the effect of @option{-c}.
7175
7176 @item -c
7177 Causes both preprocessor lines and the lines deleted
7178 by preprocessing to be retained as comments marked
7179 with the special string ``@code{--! }''.
7180
7181 @item -Dsymbol=value
7182 Define or redefine a symbol, associated with value. A symbol is an Ada
7183 identifier, or an Ada reserved word, with the exception of @code{if},
7184 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
7185 @code{value} is either a literal string, an Ada identifier or any Ada reserved
7186 word. A symbol declared with this ^switch^switch^ replaces a symbol with the
7187 same name defined in a definition file.
7188
7189 @item -s
7190 Causes a sorted list of symbol names and values to be
7191 listed on the standard output file.
7192
7193 @item -u
7194 Causes undefined symbols to be treated as having the value @code{FALSE}
7195 in the context
7196 of a preprocessor test. In the absence of this option, an undefined symbol in
7197 a @code{#if} or @code{#elsif} test will be treated as an error.
7198
7199 @end table
7200
7201 @noindent
7202 Examples of valid lines in a preprocessor data file:
7203
7204 @smallexample
7205 "toto.adb" "prep.def" -u
7206 -- preprocess "toto.adb", using definition file "prep.def",
7207 -- undefined symbol are False.
7208
7209 * -c -DVERSION=V101
7210 -- preprocess all other sources without a definition file;
7211 -- suppressed lined are commented; symbol VERSION has the value V101.
7212
7213 "titi.adb" "prep2.def" -s
7214 -- preprocess "titi.adb", using definition file "prep2.def";
7215 -- list all symbols with their values.
7216 @end smallexample
7217
7218 @item ^-gnateD^/DATA_PREPROCESSING=^symbol@r{[}=value@r{]}
7219 @cindex @option{-gnateD} (@command{gcc})
7220 Define or redefine a preprocessing symbol, associated with value. If no value
7221 is given on the command line, then the value of the symbol is @code{True}.
7222 A symbol is an identifier, following normal Ada (case-insensitive)
7223 rules for its syntax, and value is any sequence (including an empty sequence)
7224 of characters from the set (letters, digits, period, underline).
7225 Ada reserved words may be used as symbols, with the exceptions of @code{if},
7226 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
7227
7228 @noindent
7229 A symbol declared with this ^switch^switch^ on the command line replaces a
7230 symbol with the same name either in a definition file or specified with a
7231 ^switch^switch^ -D in the preprocessor data file.
7232
7233 @noindent
7234 This switch is similar to switch @option{^-D^/ASSOCIATE^} of @code{gnatprep}.
7235
7236 @item -gnateG
7237 When integrated preprocessing is performed and the preprocessor modifies
7238 the source text, write the result of this preprocessing into a file
7239 <source>^.prep^_prep^.
7240
7241 @end table
7242
7243 @node Code Generation Control
7244 @subsection Code Generation Control
7245
7246 @noindent
7247
7248 The GCC technology provides a wide range of target dependent
7249 @option{-m} switches for controlling
7250 details of code generation with respect to different versions of
7251 architectures. This includes variations in instruction sets (e.g.@:
7252 different members of the power pc family), and different requirements
7253 for optimal arrangement of instructions (e.g.@: different members of
7254 the x86 family). The list of available @option{-m} switches may be
7255 found in the GCC documentation.
7256
7257 Use of these @option{-m} switches may in some cases result in improved
7258 code performance.
7259
7260 The GNAT Pro technology is tested and qualified without any
7261 @option{-m} switches,
7262 so generally the most reliable approach is to avoid the use of these
7263 switches. However, we generally expect most of these switches to work
7264 successfully with GNAT Pro, and many customers have reported successful
7265 use of these options.
7266
7267 Our general advice is to avoid the use of @option{-m} switches unless
7268 special needs lead to requirements in this area. In particular,
7269 there is no point in using @option{-m} switches to improve performance
7270 unless you actually see a performance improvement.
7271
7272 @ifset vms
7273 @node Return Codes
7274 @subsection Return Codes
7275 @cindex Return Codes
7276 @cindex @option{/RETURN_CODES=VMS}
7277
7278 @noindent
7279 On VMS, GNAT compiled programs return POSIX-style codes by default,
7280 e.g.@: @option{/RETURN_CODES=POSIX}.
7281
7282 To enable VMS style return codes, use GNAT BIND and LINK with the option
7283 @option{/RETURN_CODES=VMS}. For example:
7284
7285 @smallexample
7286 GNAT BIND MYMAIN.ALI /RETURN_CODES=VMS
7287 GNAT LINK MYMAIN.ALI /RETURN_CODES=VMS
7288 @end smallexample
7289
7290 @noindent
7291 Programs built with /RETURN_CODES=VMS are suitable to be called in
7292 VMS DCL scripts. Programs compiled with the default /RETURN_CODES=POSIX
7293 are suitable for spawning with appropriate GNAT RTL routines.
7294
7295 @end ifset
7296
7297 @node Search Paths and the Run-Time Library (RTL)
7298 @section Search Paths and the Run-Time Library (RTL)
7299
7300 @noindent
7301 With the GNAT source-based library system, the compiler must be able to
7302 find source files for units that are needed by the unit being compiled.
7303 Search paths are used to guide this process.
7304
7305 The compiler compiles one source file whose name must be given
7306 explicitly on the command line. In other words, no searching is done
7307 for this file. To find all other source files that are needed (the most
7308 common being the specs of units), the compiler examines the following
7309 directories, in the following order:
7310
7311 @enumerate
7312 @item
7313 The directory containing the source file of the main unit being compiled
7314 (the file name on the command line).
7315
7316 @item
7317 Each directory named by an @option{^-I^/SOURCE_SEARCH^} switch given on the
7318 @command{gcc} command line, in the order given.
7319
7320 @item
7321 @findex ADA_PRJ_INCLUDE_FILE
7322 Each of the directories listed in the text file whose name is given
7323 by the @env{ADA_PRJ_INCLUDE_FILE} ^environment variable^logical name^.
7324
7325 @noindent
7326 @env{ADA_PRJ_INCLUDE_FILE} is normally set by gnatmake or by the ^gnat^GNAT^
7327 driver when project files are used. It should not normally be set
7328 by other means.
7329
7330 @item
7331 @findex ADA_INCLUDE_PATH
7332 Each of the directories listed in the value of the
7333 @env{ADA_INCLUDE_PATH} ^environment variable^logical name^.
7334 @ifclear vms
7335 Construct this value
7336 exactly as the @env{PATH} environment variable: a list of directory
7337 names separated by colons (semicolons when working with the NT version).
7338 @end ifclear
7339 @ifset vms
7340 Normally, define this value as a logical name containing a comma separated
7341 list of directory names.
7342
7343 This variable can also be defined by means of an environment string
7344 (an argument to the HP C exec* set of functions).
7345
7346 Logical Name:
7347 @smallexample
7348 DEFINE ANOTHER_PATH FOO:[BAG]
7349 DEFINE ADA_INCLUDE_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
7350 @end smallexample
7351
7352 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
7353 first, followed by the standard Ada
7354 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADAINCLUDE].
7355 If this is not redefined, the user will obtain the HP Ada 83 IO packages
7356 (Text_IO, Sequential_IO, etc)
7357 instead of the standard Ada packages. Thus, in order to get the standard Ada
7358 packages by default, ADA_INCLUDE_PATH must be redefined.
7359 @end ifset
7360
7361 @item
7362 The content of the @file{ada_source_path} file which is part of the GNAT
7363 installation tree and is used to store standard libraries such as the
7364 GNAT Run Time Library (RTL) source files.
7365 @ifclear vms
7366 @ref{Installing a library}
7367 @end ifclear
7368 @end enumerate
7369
7370 @noindent
7371 Specifying the switch @option{^-I-^/NOCURRENT_DIRECTORY^}
7372 inhibits the use of the directory
7373 containing the source file named in the command line. You can still
7374 have this directory on your search path, but in this case it must be
7375 explicitly requested with a @option{^-I^/SOURCE_SEARCH^} switch.
7376
7377 Specifying the switch @option{-nostdinc}
7378 inhibits the search of the default location for the GNAT Run Time
7379 Library (RTL) source files.
7380
7381 The compiler outputs its object files and ALI files in the current
7382 working directory.
7383 @ifclear vms
7384 Caution: The object file can be redirected with the @option{-o} switch;
7385 however, @command{gcc} and @code{gnat1} have not been coordinated on this
7386 so the @file{ALI} file will not go to the right place. Therefore, you should
7387 avoid using the @option{-o} switch.
7388 @end ifclear
7389
7390 @findex System.IO
7391 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
7392 children make up the GNAT RTL, together with the simple @code{System.IO}
7393 package used in the @code{"Hello World"} example. The sources for these units
7394 are needed by the compiler and are kept together in one directory. Not
7395 all of the bodies are needed, but all of the sources are kept together
7396 anyway. In a normal installation, you need not specify these directory
7397 names when compiling or binding. Either the environment variables or
7398 the built-in defaults cause these files to be found.
7399
7400 In addition to the language-defined hierarchies (@code{System}, @code{Ada} and
7401 @code{Interfaces}), the GNAT distribution provides a fourth hierarchy,
7402 consisting of child units of @code{GNAT}. This is a collection of generally
7403 useful types, subprograms, etc. @xref{Top, GNAT Reference Manual, About
7404 This Guid, gnat_rm, GNAT Reference Manual}, for further details.
7405
7406 Besides simplifying access to the RTL, a major use of search paths is
7407 in compiling sources from multiple directories. This can make
7408 development environments much more flexible.
7409
7410 @node Order of Compilation Issues
7411 @section Order of Compilation Issues
7412
7413 @noindent
7414 If, in our earlier example, there was a spec for the @code{hello}
7415 procedure, it would be contained in the file @file{hello.ads}; yet this
7416 file would not have to be explicitly compiled. This is the result of the
7417 model we chose to implement library management. Some of the consequences
7418 of this model are as follows:
7419
7420 @itemize @bullet
7421 @item
7422 There is no point in compiling specs (except for package
7423 specs with no bodies) because these are compiled as needed by clients. If
7424 you attempt a useless compilation, you will receive an error message.
7425 It is also useless to compile subunits because they are compiled as needed
7426 by the parent.
7427
7428 @item
7429 There are no order of compilation requirements: performing a
7430 compilation never obsoletes anything. The only way you can obsolete
7431 something and require recompilations is to modify one of the
7432 source files on which it depends.
7433
7434 @item
7435 There is no library as such, apart from the ALI files
7436 (@pxref{The Ada Library Information Files}, for information on the format
7437 of these files). For now we find it convenient to create separate ALI files,
7438 but eventually the information therein may be incorporated into the object
7439 file directly.
7440
7441 @item
7442 When you compile a unit, the source files for the specs of all units
7443 that it @code{with}'s, all its subunits, and the bodies of any generics it
7444 instantiates must be available (reachable by the search-paths mechanism
7445 described above), or you will receive a fatal error message.
7446 @end itemize
7447
7448 @node Examples
7449 @section Examples
7450
7451 @noindent
7452 The following are some typical Ada compilation command line examples:
7453
7454 @table @code
7455 @item $ gcc -c xyz.adb
7456 Compile body in file @file{xyz.adb} with all default options.
7457
7458 @ifclear vms
7459 @item $ gcc -c -O2 -gnata xyz-def.adb
7460 @end ifclear
7461 @ifset vms
7462 @item $ GNAT COMPILE /OPTIMIZE=ALL -gnata xyz-def.adb
7463 @end ifset
7464
7465 Compile the child unit package in file @file{xyz-def.adb} with extensive
7466 optimizations, and pragma @code{Assert}/@code{Debug} statements
7467 enabled.
7468
7469 @item $ gcc -c -gnatc abc-def.adb
7470 Compile the subunit in file @file{abc-def.adb} in semantic-checking-only
7471 mode.
7472 @end table
7473
7474 @node Binding Using gnatbind
7475 @chapter Binding Using @code{gnatbind}
7476 @findex gnatbind
7477
7478 @menu
7479 * Running gnatbind::
7480 * Switches for gnatbind::
7481 * Command-Line Access::
7482 * Search Paths for gnatbind::
7483 * Examples of gnatbind Usage::
7484 @end menu
7485
7486 @noindent
7487 This chapter describes the GNAT binder, @code{gnatbind}, which is used
7488 to bind compiled GNAT objects.
7489
7490 Note: to invoke @code{gnatbind} with a project file, use the @code{gnat}
7491 driver (see @ref{The GNAT Driver and Project Files}).
7492
7493 The @code{gnatbind} program performs four separate functions:
7494
7495 @enumerate
7496 @item
7497 Checks that a program is consistent, in accordance with the rules in
7498 Chapter 10 of the Ada Reference Manual. In particular, error
7499 messages are generated if a program uses inconsistent versions of a
7500 given unit.
7501
7502 @item
7503 Checks that an acceptable order of elaboration exists for the program
7504 and issues an error message if it cannot find an order of elaboration
7505 that satisfies the rules in Chapter 10 of the Ada Language Manual.
7506
7507 @item
7508 Generates a main program incorporating the given elaboration order.
7509 This program is a small Ada package (body and spec) that
7510 must be subsequently compiled
7511 using the GNAT compiler. The necessary compilation step is usually
7512 performed automatically by @command{gnatlink}. The two most important
7513 functions of this program
7514 are to call the elaboration routines of units in an appropriate order
7515 and to call the main program.
7516
7517 @item
7518 Determines the set of object files required by the given main program.
7519 This information is output in the forms of comments in the generated program,
7520 to be read by the @command{gnatlink} utility used to link the Ada application.
7521 @end enumerate
7522
7523 @node Running gnatbind
7524 @section Running @code{gnatbind}
7525
7526 @noindent
7527 The form of the @code{gnatbind} command is
7528
7529 @smallexample
7530 $ gnatbind @ovar{switches} @var{mainprog}@r{[}.ali@r{]} @ovar{switches}
7531 @end smallexample
7532
7533 @noindent
7534 where @file{@var{mainprog}.adb} is the Ada file containing the main program
7535 unit body. If no switches are specified, @code{gnatbind} constructs an Ada
7536 package in two files whose names are
7537 @file{b~@var{mainprog}.ads}, and @file{b~@var{mainprog}.adb}.
7538 For example, if given the
7539 parameter @file{hello.ali}, for a main program contained in file
7540 @file{hello.adb}, the binder output files would be @file{b~hello.ads}
7541 and @file{b~hello.adb}.
7542
7543 When doing consistency checking, the binder takes into consideration
7544 any source files it can locate. For example, if the binder determines
7545 that the given main program requires the package @code{Pack}, whose
7546 @file{.ALI}
7547 file is @file{pack.ali} and whose corresponding source spec file is
7548 @file{pack.ads}, it attempts to locate the source file @file{pack.ads}
7549 (using the same search path conventions as previously described for the
7550 @command{gcc} command). If it can locate this source file, it checks that
7551 the time stamps
7552 or source checksums of the source and its references to in @file{ALI} files
7553 match. In other words, any @file{ALI} files that mentions this spec must have
7554 resulted from compiling this version of the source file (or in the case
7555 where the source checksums match, a version close enough that the
7556 difference does not matter).
7557
7558 @cindex Source files, use by binder
7559 The effect of this consistency checking, which includes source files, is
7560 that the binder ensures that the program is consistent with the latest
7561 version of the source files that can be located at bind time. Editing a
7562 source file without compiling files that depend on the source file cause
7563 error messages to be generated by the binder.
7564
7565 For example, suppose you have a main program @file{hello.adb} and a
7566 package @code{P}, from file @file{p.ads} and you perform the following
7567 steps:
7568
7569 @enumerate
7570 @item
7571 Enter @code{gcc -c hello.adb} to compile the main program.
7572
7573 @item
7574 Enter @code{gcc -c p.ads} to compile package @code{P}.
7575
7576 @item
7577 Edit file @file{p.ads}.
7578
7579 @item
7580 Enter @code{gnatbind hello}.
7581 @end enumerate
7582
7583 @noindent
7584 At this point, the file @file{p.ali} contains an out-of-date time stamp
7585 because the file @file{p.ads} has been edited. The attempt at binding
7586 fails, and the binder generates the following error messages:
7587
7588 @smallexample
7589 error: "hello.adb" must be recompiled ("p.ads" has been modified)
7590 error: "p.ads" has been modified and must be recompiled
7591 @end smallexample
7592
7593 @noindent
7594 Now both files must be recompiled as indicated, and then the bind can
7595 succeed, generating a main program. You need not normally be concerned
7596 with the contents of this file, but for reference purposes a sample
7597 binder output file is given in @ref{Example of Binder Output File}.
7598
7599 In most normal usage, the default mode of @command{gnatbind} which is to
7600 generate the main package in Ada, as described in the previous section.
7601 In particular, this means that any Ada programmer can read and understand
7602 the generated main program. It can also be debugged just like any other
7603 Ada code provided the @option{^-g^/DEBUG^} switch is used for
7604 @command{gnatbind} and @command{gnatlink}.
7605
7606 However for some purposes it may be convenient to generate the main
7607 program in C rather than Ada. This may for example be helpful when you
7608 are generating a mixed language program with the main program in C. The
7609 GNAT compiler itself is an example.
7610 The use of the @option{^-C^/BIND_FILE=C^} switch
7611 for both @code{gnatbind} and @command{gnatlink} will cause the program to
7612 be generated in C (and compiled using the gnu C compiler).
7613
7614 @node Switches for gnatbind
7615 @section Switches for @command{gnatbind}
7616
7617 @noindent
7618 The following switches are available with @code{gnatbind}; details will
7619 be presented in subsequent sections.
7620
7621 @menu
7622 * Consistency-Checking Modes::
7623 * Binder Error Message Control::
7624 * Elaboration Control::
7625 * Output Control::
7626 * Binding with Non-Ada Main Programs::
7627 * Binding Programs with No Main Subprogram::
7628 @end menu
7629
7630 @table @option
7631 @c !sort!
7632
7633 @item --version
7634 @cindex @option{--version} @command{gnatbind}
7635 Display Copyright and version, then exit disregarding all other options.
7636
7637 @item --help
7638 @cindex @option{--help} @command{gnatbind}
7639 If @option{--version} was not used, display usage, then exit disregarding
7640 all other options.
7641
7642 @item -a
7643 @cindex @option{-a} @command{gnatbind}
7644 Indicates that, if supported by the platform, the adainit procedure should
7645 be treated as an initialisation routine by the linker (a constructor). This
7646 is intended to be used by the Project Manager to automatically initialize
7647 shared Stand-Alone Libraries.
7648
7649 @item ^-aO^/OBJECT_SEARCH^
7650 @cindex @option{^-aO^/OBJECT_SEARCH^} (@command{gnatbind})
7651 Specify directory to be searched for ALI files.
7652
7653 @item ^-aI^/SOURCE_SEARCH^
7654 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatbind})
7655 Specify directory to be searched for source file.
7656
7657 @item ^-A^/BIND_FILE=ADA^
7658 @cindex @option{^-A^/BIND_FILE=ADA^} (@command{gnatbind})
7659 Generate binder program in Ada (default)
7660
7661 @item ^-b^/REPORT_ERRORS=BRIEF^
7662 @cindex @option{^-b^/REPORT_ERRORS=BRIEF^} (@command{gnatbind})
7663 Generate brief messages to @file{stderr} even if verbose mode set.
7664
7665 @item ^-c^/NOOUTPUT^
7666 @cindex @option{^-c^/NOOUTPUT^} (@command{gnatbind})
7667 Check only, no generation of binder output file.
7668
7669 @item ^-C^/BIND_FILE=C^
7670 @cindex @option{^-C^/BIND_FILE=C^} (@command{gnatbind})
7671 Generate binder program in C
7672
7673 @item ^-d^/DEFAULT_STACK_SIZE=^@var{nn}@r{[}k@r{|}m@r{]}
7674 @cindex @option{^-d^/DEFAULT_STACK_SIZE=^@var{nn}@r{[}k@r{|}m@r{]}} (@command{gnatbind})
7675 This switch can be used to change the default task stack size value
7676 to a specified size @var{nn}, which is expressed in bytes by default, or
7677 in kilobytes when suffixed with @var{k} or in megabytes when suffixed
7678 with @var{m}.
7679 In the absence of a @samp{@r{[}k@r{|}m@r{]}} suffix, this switch is equivalent,
7680 in effect, to completing all task specs with
7681 @smallexample @c ada
7682 pragma Storage_Size (nn);
7683 @end smallexample
7684 When they do not already have such a pragma.
7685
7686 @item ^-D^/DEFAULT_SECONDARY_STACK_SIZE=^@var{nn}@r{[}k@r{|}m@r{]}
7687 @cindex @option{^-D^/DEFAULT_SECONDARY_STACK_SIZE=nnnnn^} (@command{gnatbind})
7688 This switch can be used to change the default secondary stack size value
7689 to a specified size @var{nn}, which is expressed in bytes by default, or
7690 in kilobytes when suffixed with @var{k} or in megabytes when suffixed
7691 with @var{m}.
7692
7693 The secondary stack is used to deal with functions that return a variable
7694 sized result, for example a function returning an unconstrained
7695 String. There are two ways in which this secondary stack is allocated.
7696
7697 For most targets, the secondary stack is growing on demand and is allocated
7698 as a chain of blocks in the heap. The -D option is not very
7699 relevant. It only give some control over the size of the allocated
7700 blocks (whose size is the minimum of the default secondary stack size value,
7701 and the actual size needed for the current allocation request).
7702
7703 For certain targets, notably VxWorks 653,
7704 the secondary stack is allocated by carving off a fixed ratio chunk of the
7705 primary task stack. The -D option is used to define the
7706 size of the environment task's secondary stack.
7707
7708 @item ^-e^/ELABORATION_DEPENDENCIES^
7709 @cindex @option{^-e^/ELABORATION_DEPENDENCIES^} (@command{gnatbind})
7710 Output complete list of elaboration-order dependencies.
7711
7712 @item ^-E^/STORE_TRACEBACKS^
7713 @cindex @option{^-E^/STORE_TRACEBACKS^} (@command{gnatbind})
7714 Store tracebacks in exception occurrences when the target supports it.
7715 This is the default with the zero cost exception mechanism.
7716 @ignore
7717 @c The following may get moved to an appendix
7718 This option is currently supported on the following targets:
7719 all x86 ports, Solaris, Windows, HP-UX, AIX, PowerPC VxWorks and Alpha VxWorks.
7720 @end ignore
7721 See also the packages @code{GNAT.Traceback} and
7722 @code{GNAT.Traceback.Symbolic} for more information.
7723 @ifclear vms
7724 Note that on x86 ports, you must not use @option{-fomit-frame-pointer}
7725 @command{gcc} option.
7726 @end ifclear
7727
7728 @item ^-F^/FORCE_ELABS_FLAGS^
7729 @cindex @option{^-F^/FORCE_ELABS_FLAGS^} (@command{gnatbind})
7730 Force the checks of elaboration flags. @command{gnatbind} does not normally
7731 generate checks of elaboration flags for the main executable, except when
7732 a Stand-Alone Library is used. However, there are cases when this cannot be
7733 detected by gnatbind. An example is importing an interface of a Stand-Alone
7734 Library through a pragma Import and only specifying through a linker switch
7735 this Stand-Alone Library. This switch is used to guarantee that elaboration
7736 flag checks are generated.
7737
7738 @item ^-h^/HELP^
7739 @cindex @option{^-h^/HELP^} (@command{gnatbind})
7740 Output usage (help) information
7741
7742 @item ^-I^/SEARCH^
7743 @cindex @option{^-I^/SEARCH^} (@command{gnatbind})
7744 Specify directory to be searched for source and ALI files.
7745
7746 @item ^-I-^/NOCURRENT_DIRECTORY^
7747 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gnatbind})
7748 Do not look for sources in the current directory where @code{gnatbind} was
7749 invoked, and do not look for ALI files in the directory containing the
7750 ALI file named in the @code{gnatbind} command line.
7751
7752 @item ^-l^/ORDER_OF_ELABORATION^
7753 @cindex @option{^-l^/ORDER_OF_ELABORATION^} (@command{gnatbind})
7754 Output chosen elaboration order.
7755
7756 @item ^-L@var{xxx}^/BUILD_LIBRARY=@var{xxx}^
7757 @cindex @option{^-L^/BUILD_LIBRARY^} (@command{gnatbind})
7758 Bind the units for library building. In this case the adainit and
7759 adafinal procedures (@pxref{Binding with Non-Ada Main Programs})
7760 are renamed to ^@var{xxx}init^@var{XXX}INIT^ and
7761 ^@var{xxx}final^@var{XXX}FINAL^.
7762 Implies ^-n^/NOCOMPILE^.
7763 @ifclear vms
7764 (@xref{GNAT and Libraries}, for more details.)
7765 @end ifclear
7766 @ifset vms
7767 On OpenVMS, these init and final procedures are exported in uppercase
7768 letters. For example if /BUILD_LIBRARY=toto is used, the exported name of
7769 the init procedure will be "TOTOINIT" and the exported name of the final
7770 procedure will be "TOTOFINAL".
7771 @end ifset
7772
7773 @item ^-Mxyz^/RENAME_MAIN=xyz^
7774 @cindex @option{^-M^/RENAME_MAIN^} (@command{gnatbind})
7775 Rename generated main program from main to xyz. This option is
7776 supported on cross environments only.
7777
7778 @item ^-m^/ERROR_LIMIT=^@var{n}
7779 @cindex @option{^-m^/ERROR_LIMIT^} (@command{gnatbind})
7780 Limit number of detected errors to @var{n}, where @var{n} is
7781 in the range 1..999_999. The default value if no switch is
7782 given is 9999. Binding is terminated if the limit is exceeded.
7783 @ifset unw
7784 Furthermore, under Windows, the sources pointed to by the libraries path
7785 set in the registry are not searched for.
7786 @end ifset
7787
7788 @item ^-n^/NOMAIN^
7789 @cindex @option{^-n^/NOMAIN^} (@command{gnatbind})
7790 No main program.
7791
7792 @item -nostdinc
7793 @cindex @option{-nostdinc} (@command{gnatbind})
7794 Do not look for sources in the system default directory.
7795
7796 @item -nostdlib
7797 @cindex @option{-nostdlib} (@command{gnatbind})
7798 Do not look for library files in the system default directory.
7799
7800 @item --RTS=@var{rts-path}
7801 @cindex @option{--RTS} (@code{gnatbind})
7802 Specifies the default location of the runtime library. Same meaning as the
7803 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
7804
7805 @item ^-o ^/OUTPUT=^@var{file}
7806 @cindex @option{^-o ^/OUTPUT^} (@command{gnatbind})
7807 Name the output file @var{file} (default is @file{b~@var{xxx}.adb}).
7808 Note that if this option is used, then linking must be done manually,
7809 gnatlink cannot be used.
7810
7811 @item ^-O^/OBJECT_LIST^
7812 @cindex @option{^-O^/OBJECT_LIST^} (@command{gnatbind})
7813 Output object list.
7814
7815 @item ^-p^/PESSIMISTIC_ELABORATION^
7816 @cindex @option{^-p^/PESSIMISTIC_ELABORATION^} (@command{gnatbind})
7817 Pessimistic (worst-case) elaboration order
7818
7819 @item ^-R^-R^
7820 @cindex @option{^-R^-R^} (@command{gnatbind})
7821 Output closure source list.
7822
7823 @item ^-s^/READ_SOURCES=ALL^
7824 @cindex @option{^-s^/READ_SOURCES=ALL^} (@command{gnatbind})
7825 Require all source files to be present.
7826
7827 @item ^-S@var{xxx}^/INITIALIZE_SCALARS=@var{xxx}^
7828 @cindex @option{^-S^/INITIALIZE_SCALARS^} (@command{gnatbind})
7829 Specifies the value to be used when detecting uninitialized scalar
7830 objects with pragma Initialize_Scalars.
7831 The @var{xxx} ^string specified with the switch^option^ may be either
7832 @itemize @bullet
7833 @item ``@option{^in^INVALID^}'' requesting an invalid value where possible
7834 @item ``@option{^lo^LOW^}'' for the lowest possible value
7835 @item ``@option{^hi^HIGH^}'' for the highest possible value
7836 @item ``@option{@var{xx}}'' for a value consisting of repeated bytes with the
7837 value @code{16#@var{xx}#} (i.e., @var{xx} is a string of two hexadecimal digits).
7838 @end itemize
7839
7840 In addition, you can specify @option{-Sev} to indicate that the value is
7841 to be set at run time. In this case, the program will look for an environment
7842 @cindex GNAT_INIT_SCALARS
7843 variable of the form @env{GNAT_INIT_SCALARS=@var{xx}}, where @var{xx} is one
7844 of @option{in/lo/hi/@var{xx}} with the same meanings as above.
7845 If no environment variable is found, or if it does not have a valid value,
7846 then the default is @option{in} (invalid values).
7847
7848 @ifclear vms
7849 @item -static
7850 @cindex @option{-static} (@code{gnatbind})
7851 Link against a static GNAT run time.
7852
7853 @item -shared
7854 @cindex @option{-shared} (@code{gnatbind})
7855 Link against a shared GNAT run time when available.
7856 @end ifclear
7857
7858 @item ^-t^/NOTIME_STAMP_CHECK^
7859 @cindex @option{^-t^/NOTIME_STAMP_CHECK^} (@code{gnatbind})
7860 Tolerate time stamp and other consistency errors
7861
7862 @item ^-T@var{n}^/TIME_SLICE=@var{n}^
7863 @cindex @option{^-T^/TIME_SLICE^} (@code{gnatbind})
7864 Set the time slice value to @var{n} milliseconds. If the system supports
7865 the specification of a specific time slice value, then the indicated value
7866 is used. If the system does not support specific time slice values, but
7867 does support some general notion of round-robin scheduling, then any
7868 nonzero value will activate round-robin scheduling.
7869
7870 A value of zero is treated specially. It turns off time
7871 slicing, and in addition, indicates to the tasking run time that the
7872 semantics should match as closely as possible the Annex D
7873 requirements of the Ada RM, and in particular sets the default
7874 scheduling policy to @code{FIFO_Within_Priorities}.
7875
7876 @item ^-u@var{n}^/DYNAMIC_STACK_USAGE=@var{n}^
7877 @cindex @option{^-u^/DYNAMIC_STACK_USAGE^} (@code{gnatbind})
7878 Enable dynamic stack usage, with @var{n} results stored and displayed
7879 at program termination. A result is generated when a task
7880 terminates. Results that can't be stored are displayed on the fly, at
7881 task termination. This option is currently not supported on Itanium
7882 platforms. (See @ref{Dynamic Stack Usage Analysis} for details.)
7883
7884 @item ^-v^/REPORT_ERRORS=VERBOSE^
7885 @cindex @option{^-v^/REPORT_ERRORS=VERBOSE^} (@code{gnatbind})
7886 Verbose mode. Write error messages, header, summary output to
7887 @file{stdout}.
7888
7889 @ifclear vms
7890 @item -w@var{x}
7891 @cindex @option{-w} (@code{gnatbind})
7892 Warning mode (@var{x}=s/e for suppress/treat as error)
7893 @end ifclear
7894
7895 @ifset vms
7896 @item /WARNINGS=NORMAL
7897 @cindex @option{/WARNINGS} (@code{gnatbind})
7898 Normal warnings mode. Warnings are issued but ignored
7899
7900 @item /WARNINGS=SUPPRESS
7901 @cindex @option{/WARNINGS} (@code{gnatbind})
7902 All warning messages are suppressed
7903
7904 @item /WARNINGS=ERROR
7905 @cindex @option{/WARNINGS} (@code{gnatbind})
7906 Warning messages are treated as fatal errors
7907 @end ifset
7908
7909 @item ^-Wx^/WIDE_CHARACTER_ENCODING=^@var{e}
7910 @cindex @option{^-Wx^/WIDE_CHARACTER_ENCODING^} (@code{gnatbind})
7911 Override default wide character encoding for standard Text_IO files.
7912
7913 @item ^-x^/READ_SOURCES=NONE^
7914 @cindex @option{^-x^/READ_SOURCES^} (@code{gnatbind})
7915 Exclude source files (check object consistency only).
7916
7917 @ifset vms
7918 @item /READ_SOURCES=AVAILABLE
7919 @cindex @option{/READ_SOURCES} (@code{gnatbind})
7920 Default mode, in which sources are checked for consistency only if
7921 they are available.
7922 @end ifset
7923
7924 @item ^-y^/ENABLE_LEAP_SECONDS^
7925 @cindex @option{^-y^/ENABLE_LEAP_SECONDS^} (@code{gnatbind})
7926 Enable leap seconds support in @code{Ada.Calendar} and its children.
7927
7928 @item ^-z^/ZERO_MAIN^
7929 @cindex @option{^-z^/ZERO_MAIN^} (@code{gnatbind})
7930 No main subprogram.
7931 @end table
7932
7933 @ifclear vms
7934 @noindent
7935 You may obtain this listing of switches by running @code{gnatbind} with
7936 no arguments.
7937 @end ifclear
7938
7939 @node Consistency-Checking Modes
7940 @subsection Consistency-Checking Modes
7941
7942 @noindent
7943 As described earlier, by default @code{gnatbind} checks
7944 that object files are consistent with one another and are consistent
7945 with any source files it can locate. The following switches control binder
7946 access to sources.
7947
7948 @table @option
7949 @c !sort!
7950 @item ^-s^/READ_SOURCES=ALL^
7951 @cindex @option{^-s^/READ_SOURCES=ALL^} (@code{gnatbind})
7952 Require source files to be present. In this mode, the binder must be
7953 able to locate all source files that are referenced, in order to check
7954 their consistency. In normal mode, if a source file cannot be located it
7955 is simply ignored. If you specify this switch, a missing source
7956 file is an error.
7957
7958 @item ^-Wx^/WIDE_CHARACTER_ENCODING=^@var{e}
7959 @cindex @option{^-Wx^/WIDE_CHARACTER_ENCODING^} (@code{gnatbind})
7960 Override default wide character encoding for standard Text_IO files.
7961 Normally the default wide character encoding method used for standard
7962 [Wide_[Wide_]]Text_IO files is taken from the encoding specified for
7963 the main source input (see description of switch
7964 @option{^-gnatWx^/WIDE_CHARACTER_ENCODING^} for the compiler). The
7965 use of this switch for the binder (which has the same set of
7966 possible arguments) overrides this default as specified.
7967
7968 @item ^-x^/READ_SOURCES=NONE^
7969 @cindex @option{^-x^/READ_SOURCES=NONE^} (@code{gnatbind})
7970 Exclude source files. In this mode, the binder only checks that ALI
7971 files are consistent with one another. Source files are not accessed.
7972 The binder runs faster in this mode, and there is still a guarantee that
7973 the resulting program is self-consistent.
7974 If a source file has been edited since it was last compiled, and you
7975 specify this switch, the binder will not detect that the object
7976 file is out of date with respect to the source file. Note that this is the
7977 mode that is automatically used by @command{gnatmake} because in this
7978 case the checking against sources has already been performed by
7979 @command{gnatmake} in the course of compilation (i.e.@: before binding).
7980
7981 @ifset vms
7982 @item /READ_SOURCES=AVAILABLE
7983 @cindex @code{/READ_SOURCES=AVAILABLE} (@code{gnatbind})
7984 This is the default mode in which source files are checked if they are
7985 available, and ignored if they are not available.
7986 @end ifset
7987 @end table
7988
7989 @node Binder Error Message Control
7990 @subsection Binder Error Message Control
7991
7992 @noindent
7993 The following switches provide control over the generation of error
7994 messages from the binder:
7995
7996 @table @option
7997 @c !sort!
7998 @item ^-v^/REPORT_ERRORS=VERBOSE^
7999 @cindex @option{^-v^/REPORT_ERRORS=VERBOSE^} (@code{gnatbind})
8000 Verbose mode. In the normal mode, brief error messages are generated to
8001 @file{stderr}. If this switch is present, a header is written
8002 to @file{stdout} and any error messages are directed to @file{stdout}.
8003 All that is written to @file{stderr} is a brief summary message.
8004
8005 @item ^-b^/REPORT_ERRORS=BRIEF^
8006 @cindex @option{^-b^/REPORT_ERRORS=BRIEF^} (@code{gnatbind})
8007 Generate brief error messages to @file{stderr} even if verbose mode is
8008 specified. This is relevant only when used with the
8009 @option{^-v^/REPORT_ERRORS=VERBOSE^} switch.
8010
8011 @ifclear vms
8012 @item -m@var{n}
8013 @cindex @option{-m} (@code{gnatbind})
8014 Limits the number of error messages to @var{n}, a decimal integer in the
8015 range 1-999. The binder terminates immediately if this limit is reached.
8016
8017 @item -M@var{xxx}
8018 @cindex @option{-M} (@code{gnatbind})
8019 Renames the generated main program from @code{main} to @code{xxx}.
8020 This is useful in the case of some cross-building environments, where
8021 the actual main program is separate from the one generated
8022 by @code{gnatbind}.
8023 @end ifclear
8024
8025 @item ^-ws^/WARNINGS=SUPPRESS^
8026 @cindex @option{^-ws^/WARNINGS=SUPPRESS^} (@code{gnatbind})
8027 @cindex Warnings
8028 Suppress all warning messages.
8029
8030 @item ^-we^/WARNINGS=ERROR^
8031 @cindex @option{^-we^/WARNINGS=ERROR^} (@code{gnatbind})
8032 Treat any warning messages as fatal errors.
8033
8034 @ifset vms
8035 @item /WARNINGS=NORMAL
8036 Standard mode with warnings generated, but warnings do not get treated
8037 as errors.
8038 @end ifset
8039
8040 @item ^-t^/NOTIME_STAMP_CHECK^
8041 @cindex @option{^-t^/NOTIME_STAMP_CHECK^} (@code{gnatbind})
8042 @cindex Time stamp checks, in binder
8043 @cindex Binder consistency checks
8044 @cindex Consistency checks, in binder
8045 The binder performs a number of consistency checks including:
8046
8047 @itemize @bullet
8048 @item
8049 Check that time stamps of a given source unit are consistent
8050 @item
8051 Check that checksums of a given source unit are consistent
8052 @item
8053 Check that consistent versions of @code{GNAT} were used for compilation
8054 @item
8055 Check consistency of configuration pragmas as required
8056 @end itemize
8057
8058 @noindent
8059 Normally failure of such checks, in accordance with the consistency
8060 requirements of the Ada Reference Manual, causes error messages to be
8061 generated which abort the binder and prevent the output of a binder
8062 file and subsequent link to obtain an executable.
8063
8064 The @option{^-t^/NOTIME_STAMP_CHECK^} switch converts these error messages
8065 into warnings, so that
8066 binding and linking can continue to completion even in the presence of such
8067 errors. The result may be a failed link (due to missing symbols), or a
8068 non-functional executable which has undefined semantics.
8069 @emph{This means that
8070 @option{^-t^/NOTIME_STAMP_CHECK^} should be used only in unusual situations,
8071 with extreme care.}
8072 @end table
8073
8074 @node Elaboration Control
8075 @subsection Elaboration Control
8076
8077 @noindent
8078 The following switches provide additional control over the elaboration
8079 order. For full details see @ref{Elaboration Order Handling in GNAT}.
8080
8081 @table @option
8082 @item ^-p^/PESSIMISTIC_ELABORATION^
8083 @cindex @option{^-p^/PESSIMISTIC_ELABORATION^} (@code{gnatbind})
8084 Normally the binder attempts to choose an elaboration order that is
8085 likely to minimize the likelihood of an elaboration order error resulting
8086 in raising a @code{Program_Error} exception. This switch reverses the
8087 action of the binder, and requests that it deliberately choose an order
8088 that is likely to maximize the likelihood of an elaboration error.
8089 This is useful in ensuring portability and avoiding dependence on
8090 accidental fortuitous elaboration ordering.
8091
8092 Normally it only makes sense to use the @option{^-p^/PESSIMISTIC_ELABORATION^}
8093 switch if dynamic
8094 elaboration checking is used (@option{-gnatE} switch used for compilation).
8095 This is because in the default static elaboration mode, all necessary
8096 @code{Elaborate} and @code{Elaborate_All} pragmas are implicitly inserted.
8097 These implicit pragmas are still respected by the binder in
8098 @option{^-p^/PESSIMISTIC_ELABORATION^} mode, so a
8099 safe elaboration order is assured.
8100 @end table
8101
8102 @node Output Control
8103 @subsection Output Control
8104
8105 @noindent
8106 The following switches allow additional control over the output
8107 generated by the binder.
8108
8109 @table @option
8110 @c !sort!
8111
8112 @item ^-A^/BIND_FILE=ADA^
8113 @cindex @option{^-A^/BIND_FILE=ADA^} (@code{gnatbind})
8114 Generate binder program in Ada (default). The binder program is named
8115 @file{b~@var{mainprog}.adb} by default. This can be changed with
8116 @option{^-o^/OUTPUT^} @code{gnatbind} option.
8117
8118 @item ^-c^/NOOUTPUT^
8119 @cindex @option{^-c^/NOOUTPUT^} (@code{gnatbind})
8120 Check only. Do not generate the binder output file. In this mode the
8121 binder performs all error checks but does not generate an output file.
8122
8123 @item ^-C^/BIND_FILE=C^
8124 @cindex @option{^-C^/BIND_FILE=C^} (@code{gnatbind})
8125 Generate binder program in C. The binder program is named
8126 @file{b_@var{mainprog}.c}.
8127 This can be changed with @option{^-o^/OUTPUT^} @code{gnatbind}
8128 option.
8129
8130 @item ^-e^/ELABORATION_DEPENDENCIES^
8131 @cindex @option{^-e^/ELABORATION_DEPENDENCIES^} (@code{gnatbind})
8132 Output complete list of elaboration-order dependencies, showing the
8133 reason for each dependency. This output can be rather extensive but may
8134 be useful in diagnosing problems with elaboration order. The output is
8135 written to @file{stdout}.
8136
8137 @item ^-h^/HELP^
8138 @cindex @option{^-h^/HELP^} (@code{gnatbind})
8139 Output usage information. The output is written to @file{stdout}.
8140
8141 @item ^-K^/LINKER_OPTION_LIST^
8142 @cindex @option{^-K^/LINKER_OPTION_LIST^} (@code{gnatbind})
8143 Output linker options to @file{stdout}. Includes library search paths,
8144 contents of pragmas Ident and Linker_Options, and libraries added
8145 by @code{gnatbind}.
8146
8147 @item ^-l^/ORDER_OF_ELABORATION^
8148 @cindex @option{^-l^/ORDER_OF_ELABORATION^} (@code{gnatbind})
8149 Output chosen elaboration order. The output is written to @file{stdout}.
8150
8151 @item ^-O^/OBJECT_LIST^
8152 @cindex @option{^-O^/OBJECT_LIST^} (@code{gnatbind})
8153 Output full names of all the object files that must be linked to provide
8154 the Ada component of the program. The output is written to @file{stdout}.
8155 This list includes the files explicitly supplied and referenced by the user
8156 as well as implicitly referenced run-time unit files. The latter are
8157 omitted if the corresponding units reside in shared libraries. The
8158 directory names for the run-time units depend on the system configuration.
8159
8160 @item ^-o ^/OUTPUT=^@var{file}
8161 @cindex @option{^-o^/OUTPUT^} (@code{gnatbind})
8162 Set name of output file to @var{file} instead of the normal
8163 @file{b~@var{mainprog}.adb} default. Note that @var{file} denote the Ada
8164 binder generated body filename. In C mode you would normally give
8165 @var{file} an extension of @file{.c} because it will be a C source program.
8166 Note that if this option is used, then linking must be done manually.
8167 It is not possible to use gnatlink in this case, since it cannot locate
8168 the binder file.
8169
8170 @item ^-r^/RESTRICTION_LIST^
8171 @cindex @option{^-r^/RESTRICTION_LIST^} (@code{gnatbind})
8172 Generate list of @code{pragma Restrictions} that could be applied to
8173 the current unit. This is useful for code audit purposes, and also may
8174 be used to improve code generation in some cases.
8175
8176 @end table
8177
8178 @node Binding with Non-Ada Main Programs
8179 @subsection Binding with Non-Ada Main Programs
8180
8181 @noindent
8182 In our description so far we have assumed that the main
8183 program is in Ada, and that the task of the binder is to generate a
8184 corresponding function @code{main} that invokes this Ada main
8185 program. GNAT also supports the building of executable programs where
8186 the main program is not in Ada, but some of the called routines are
8187 written in Ada and compiled using GNAT (@pxref{Mixed Language Programming}).
8188 The following switch is used in this situation:
8189
8190 @table @option
8191 @item ^-n^/NOMAIN^
8192 @cindex @option{^-n^/NOMAIN^} (@code{gnatbind})
8193 No main program. The main program is not in Ada.
8194 @end table
8195
8196 @noindent
8197 In this case, most of the functions of the binder are still required,
8198 but instead of generating a main program, the binder generates a file
8199 containing the following callable routines:
8200
8201 @table @code
8202 @item adainit
8203 @findex adainit
8204 You must call this routine to initialize the Ada part of the program by
8205 calling the necessary elaboration routines. A call to @code{adainit} is
8206 required before the first call to an Ada subprogram.
8207
8208 Note that it is assumed that the basic execution environment must be setup
8209 to be appropriate for Ada execution at the point where the first Ada
8210 subprogram is called. In particular, if the Ada code will do any
8211 floating-point operations, then the FPU must be setup in an appropriate
8212 manner. For the case of the x86, for example, full precision mode is
8213 required. The procedure GNAT.Float_Control.Reset may be used to ensure
8214 that the FPU is in the right state.
8215
8216 @item adafinal
8217 @findex adafinal
8218 You must call this routine to perform any library-level finalization
8219 required by the Ada subprograms. A call to @code{adafinal} is required
8220 after the last call to an Ada subprogram, and before the program
8221 terminates.
8222 @end table
8223
8224 @noindent
8225 If the @option{^-n^/NOMAIN^} switch
8226 @cindex @option{^-n^/NOMAIN^} (@command{gnatbind})
8227 @cindex Binder, multiple input files
8228 is given, more than one ALI file may appear on
8229 the command line for @code{gnatbind}. The normal @dfn{closure}
8230 calculation is performed for each of the specified units. Calculating
8231 the closure means finding out the set of units involved by tracing
8232 @code{with} references. The reason it is necessary to be able to
8233 specify more than one ALI file is that a given program may invoke two or
8234 more quite separate groups of Ada units.
8235
8236 The binder takes the name of its output file from the last specified ALI
8237 file, unless overridden by the use of the @option{^-o file^/OUTPUT=file^}.
8238 @cindex @option{^-o^/OUTPUT^} (@command{gnatbind})
8239 The output is an Ada unit in source form that can
8240 be compiled with GNAT unless the -C switch is used in which case the
8241 output is a C source file, which must be compiled using the C compiler.
8242 This compilation occurs automatically as part of the @command{gnatlink}
8243 processing.
8244
8245 Currently the GNAT run time requires a FPU using 80 bits mode
8246 precision. Under targets where this is not the default it is required to
8247 call GNAT.Float_Control.Reset before using floating point numbers (this
8248 include float computation, float input and output) in the Ada code. A
8249 side effect is that this could be the wrong mode for the foreign code
8250 where floating point computation could be broken after this call.
8251
8252 @node Binding Programs with No Main Subprogram
8253 @subsection Binding Programs with No Main Subprogram
8254
8255 @noindent
8256 It is possible to have an Ada program which does not have a main
8257 subprogram. This program will call the elaboration routines of all the
8258 packages, then the finalization routines.
8259
8260 The following switch is used to bind programs organized in this manner:
8261
8262 @table @option
8263 @item ^-z^/ZERO_MAIN^
8264 @cindex @option{^-z^/ZERO_MAIN^} (@code{gnatbind})
8265 Normally the binder checks that the unit name given on the command line
8266 corresponds to a suitable main subprogram. When this switch is used,
8267 a list of ALI files can be given, and the execution of the program
8268 consists of elaboration of these units in an appropriate order. Note
8269 that the default wide character encoding method for standard Text_IO
8270 files is always set to Brackets if this switch is set (you can use
8271 the binder switch
8272 @option{^-Wx^WIDE_CHARACTER_ENCODING^} to override this default).
8273 @end table
8274
8275 @node Command-Line Access
8276 @section Command-Line Access
8277
8278 @noindent
8279 The package @code{Ada.Command_Line} provides access to the command-line
8280 arguments and program name. In order for this interface to operate
8281 correctly, the two variables
8282
8283 @smallexample
8284 @group
8285 int gnat_argc;
8286 char **gnat_argv;
8287 @end group
8288 @end smallexample
8289
8290 @noindent
8291 @findex gnat_argv
8292 @findex gnat_argc
8293 are declared in one of the GNAT library routines. These variables must
8294 be set from the actual @code{argc} and @code{argv} values passed to the
8295 main program. With no @option{^n^/NOMAIN^} present, @code{gnatbind}
8296 generates the C main program to automatically set these variables.
8297 If the @option{^n^/NOMAIN^} switch is used, there is no automatic way to
8298 set these variables. If they are not set, the procedures in
8299 @code{Ada.Command_Line} will not be available, and any attempt to use
8300 them will raise @code{Constraint_Error}. If command line access is
8301 required, your main program must set @code{gnat_argc} and
8302 @code{gnat_argv} from the @code{argc} and @code{argv} values passed to
8303 it.
8304
8305 @node Search Paths for gnatbind
8306 @section Search Paths for @code{gnatbind}
8307
8308 @noindent
8309 The binder takes the name of an ALI file as its argument and needs to
8310 locate source files as well as other ALI files to verify object consistency.
8311
8312 For source files, it follows exactly the same search rules as @command{gcc}
8313 (@pxref{Search Paths and the Run-Time Library (RTL)}). For ALI files the
8314 directories searched are:
8315
8316 @enumerate
8317 @item
8318 The directory containing the ALI file named in the command line, unless
8319 the switch @option{^-I-^/NOCURRENT_DIRECTORY^} is specified.
8320
8321 @item
8322 All directories specified by @option{^-I^/SEARCH^}
8323 switches on the @code{gnatbind}
8324 command line, in the order given.
8325
8326 @item
8327 @findex ADA_PRJ_OBJECTS_FILE
8328 Each of the directories listed in the text file whose name is given
8329 by the @env{ADA_PRJ_OBJECTS_FILE} ^environment variable^logical name^.
8330
8331 @noindent
8332 @env{ADA_PRJ_OBJECTS_FILE} is normally set by gnatmake or by the ^gnat^GNAT^
8333 driver when project files are used. It should not normally be set
8334 by other means.
8335
8336 @item
8337 @findex ADA_OBJECTS_PATH
8338 Each of the directories listed in the value of the
8339 @env{ADA_OBJECTS_PATH} ^environment variable^logical name^.
8340 @ifset unw
8341 Construct this value
8342 exactly as the @env{PATH} environment variable: a list of directory
8343 names separated by colons (semicolons when working with the NT version
8344 of GNAT).
8345 @end ifset
8346 @ifset vms
8347 Normally, define this value as a logical name containing a comma separated
8348 list of directory names.
8349
8350 This variable can also be defined by means of an environment string
8351 (an argument to the HP C exec* set of functions).
8352
8353 Logical Name:
8354 @smallexample
8355 DEFINE ANOTHER_PATH FOO:[BAG]
8356 DEFINE ADA_OBJECTS_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
8357 @end smallexample
8358
8359 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
8360 first, followed by the standard Ada
8361 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADALIB].
8362 If this is not redefined, the user will obtain the HP Ada 83 IO packages
8363 (Text_IO, Sequential_IO, etc)
8364 instead of the standard Ada packages. Thus, in order to get the standard Ada
8365 packages by default, ADA_OBJECTS_PATH must be redefined.
8366 @end ifset
8367
8368 @item
8369 The content of the @file{ada_object_path} file which is part of the GNAT
8370 installation tree and is used to store standard libraries such as the
8371 GNAT Run Time Library (RTL) unless the switch @option{-nostdlib} is
8372 specified.
8373 @ifclear vms
8374 @ref{Installing a library}
8375 @end ifclear
8376 @end enumerate
8377
8378 @noindent
8379 In the binder the switch @option{^-I^/SEARCH^}
8380 @cindex @option{^-I^/SEARCH^} (@command{gnatbind})
8381 is used to specify both source and
8382 library file paths. Use @option{^-aI^/SOURCE_SEARCH^}
8383 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatbind})
8384 instead if you want to specify
8385 source paths only, and @option{^-aO^/LIBRARY_SEARCH^}
8386 @cindex @option{^-aO^/LIBRARY_SEARCH^} (@command{gnatbind})
8387 if you want to specify library paths
8388 only. This means that for the binder
8389 @option{^-I^/SEARCH=^}@var{dir} is equivalent to
8390 @option{^-aI^/SOURCE_SEARCH=^}@var{dir}
8391 @option{^-aO^/OBJECT_SEARCH=^}@var{dir}.
8392 The binder generates the bind file (a C language source file) in the
8393 current working directory.
8394
8395 @findex Ada
8396 @findex System
8397 @findex Interfaces
8398 @findex GNAT
8399 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
8400 children make up the GNAT Run-Time Library, together with the package
8401 GNAT and its children, which contain a set of useful additional
8402 library functions provided by GNAT. The sources for these units are
8403 needed by the compiler and are kept together in one directory. The ALI
8404 files and object files generated by compiling the RTL are needed by the
8405 binder and the linker and are kept together in one directory, typically
8406 different from the directory containing the sources. In a normal
8407 installation, you need not specify these directory names when compiling
8408 or binding. Either the environment variables or the built-in defaults
8409 cause these files to be found.
8410
8411 Besides simplifying access to the RTL, a major use of search paths is
8412 in compiling sources from multiple directories. This can make
8413 development environments much more flexible.
8414
8415 @node Examples of gnatbind Usage
8416 @section Examples of @code{gnatbind} Usage
8417
8418 @noindent
8419 This section contains a number of examples of using the GNAT binding
8420 utility @code{gnatbind}.
8421
8422 @table @code
8423 @item gnatbind hello
8424 The main program @code{Hello} (source program in @file{hello.adb}) is
8425 bound using the standard switch settings. The generated main program is
8426 @file{b~hello.adb}. This is the normal, default use of the binder.
8427
8428 @ifclear vms
8429 @item gnatbind hello -o mainprog.adb
8430 @end ifclear
8431 @ifset vms
8432 @item gnatbind HELLO.ALI /OUTPUT=Mainprog.ADB
8433 @end ifset
8434 The main program @code{Hello} (source program in @file{hello.adb}) is
8435 bound using the standard switch settings. The generated main program is
8436 @file{mainprog.adb} with the associated spec in
8437 @file{mainprog.ads}. Note that you must specify the body here not the
8438 spec, in the case where the output is in Ada. Note that if this option
8439 is used, then linking must be done manually, since gnatlink will not
8440 be able to find the generated file.
8441
8442 @ifclear vms
8443 @item gnatbind main -C -o mainprog.c -x
8444 @end ifclear
8445 @ifset vms
8446 @item gnatbind MAIN.ALI /BIND_FILE=C /OUTPUT=Mainprog.C /READ_SOURCES=NONE
8447 @end ifset
8448 The main program @code{Main} (source program in
8449 @file{main.adb}) is bound, excluding source files from the
8450 consistency checking, generating
8451 the file @file{mainprog.c}.
8452
8453 @ifclear vms
8454 @item gnatbind -x main_program -C -o mainprog.c
8455 This command is exactly the same as the previous example. Switches may
8456 appear anywhere in the command line, and single letter switches may be
8457 combined into a single switch.
8458 @end ifclear
8459
8460 @ifclear vms
8461 @item gnatbind -n math dbase -C -o ada-control.c
8462 @end ifclear
8463 @ifset vms
8464 @item gnatbind /NOMAIN math dbase /BIND_FILE=C /OUTPUT=ada-control.c
8465 @end ifset
8466 The main program is in a language other than Ada, but calls to
8467 subprograms in packages @code{Math} and @code{Dbase} appear. This call
8468 to @code{gnatbind} generates the file @file{ada-control.c} containing
8469 the @code{adainit} and @code{adafinal} routines to be called before and
8470 after accessing the Ada units.
8471 @end table
8472
8473 @c ------------------------------------
8474 @node Linking Using gnatlink
8475 @chapter Linking Using @command{gnatlink}
8476 @c ------------------------------------
8477 @findex gnatlink
8478
8479 @noindent
8480 This chapter discusses @command{gnatlink}, a tool that links
8481 an Ada program and builds an executable file. This utility
8482 invokes the system linker ^(via the @command{gcc} command)^^
8483 with a correct list of object files and library references.
8484 @command{gnatlink} automatically determines the list of files and
8485 references for the Ada part of a program. It uses the binder file
8486 generated by the @command{gnatbind} to determine this list.
8487
8488 Note: to invoke @code{gnatlink} with a project file, use the @code{gnat}
8489 driver (see @ref{The GNAT Driver and Project Files}).
8490
8491 @menu
8492 * Running gnatlink::
8493 * Switches for gnatlink::
8494 @end menu
8495
8496 @node Running gnatlink
8497 @section Running @command{gnatlink}
8498
8499 @noindent
8500 The form of the @command{gnatlink} command is
8501
8502 @smallexample
8503 $ gnatlink @ovar{switches} @var{mainprog}@r{[}.ali@r{]}
8504 @ovar{non-Ada objects} @ovar{linker options}
8505 @end smallexample
8506
8507 @noindent
8508 The arguments of @command{gnatlink} (switches, main @file{ALI} file,
8509 non-Ada objects
8510 or linker options) may be in any order, provided that no non-Ada object may
8511 be mistaken for a main @file{ALI} file.
8512 Any file name @file{F} without the @file{.ali}
8513 extension will be taken as the main @file{ALI} file if a file exists
8514 whose name is the concatenation of @file{F} and @file{.ali}.
8515
8516 @noindent
8517 @file{@var{mainprog}.ali} references the ALI file of the main program.
8518 The @file{.ali} extension of this file can be omitted. From this
8519 reference, @command{gnatlink} locates the corresponding binder file
8520 @file{b~@var{mainprog}.adb} and, using the information in this file along
8521 with the list of non-Ada objects and linker options, constructs a
8522 linker command file to create the executable.
8523
8524 The arguments other than the @command{gnatlink} switches and the main
8525 @file{ALI} file are passed to the linker uninterpreted.
8526 They typically include the names of
8527 object files for units written in other languages than Ada and any library
8528 references required to resolve references in any of these foreign language
8529 units, or in @code{Import} pragmas in any Ada units.
8530
8531 @var{linker options} is an optional list of linker specific
8532 switches.
8533 The default linker called by gnatlink is @command{gcc} which in
8534 turn calls the appropriate system linker.
8535 Standard options for the linker such as @option{-lmy_lib} or
8536 @option{-Ldir} can be added as is.
8537 For options that are not recognized by
8538 @command{gcc} as linker options, use the @command{gcc} switches
8539 @option{-Xlinker} or @option{-Wl,}.
8540 Refer to the GCC documentation for
8541 details. Here is an example showing how to generate a linker map:
8542
8543 @smallexample
8544 $ ^gnatlink my_prog -Wl,-Map,MAPFILE^GNAT LINK my_prog.ali /MAP^
8545 @end smallexample
8546
8547 Using @var{linker options} it is possible to set the program stack and
8548 heap size.
8549 @ifset unw
8550 See @ref{Setting Stack Size from gnatlink} and
8551 @ref{Setting Heap Size from gnatlink}.
8552 @end ifset
8553
8554 @command{gnatlink} determines the list of objects required by the Ada
8555 program and prepends them to the list of objects passed to the linker.
8556 @command{gnatlink} also gathers any arguments set by the use of
8557 @code{pragma Linker_Options} and adds them to the list of arguments
8558 presented to the linker.
8559
8560 @ifset vms
8561 @command{gnatlink} accepts the following types of extra files on the command
8562 line: objects (@file{.OBJ}), libraries (@file{.OLB}), sharable images
8563 (@file{.EXE}), and options files (@file{.OPT}). These are recognized and
8564 handled according to their extension.
8565 @end ifset
8566
8567 @node Switches for gnatlink
8568 @section Switches for @command{gnatlink}
8569
8570 @noindent
8571 The following switches are available with the @command{gnatlink} utility:
8572
8573 @table @option
8574 @c !sort!
8575
8576 @item --version
8577 @cindex @option{--version} @command{gnatlink}
8578 Display Copyright and version, then exit disregarding all other options.
8579
8580 @item --help
8581 @cindex @option{--help} @command{gnatlink}
8582 If @option{--version} was not used, display usage, then exit disregarding
8583 all other options.
8584
8585 @item ^-A^/BIND_FILE=ADA^
8586 @cindex @option{^-A^/BIND_FILE=ADA^} (@command{gnatlink})
8587 The binder has generated code in Ada. This is the default.
8588
8589 @item ^-C^/BIND_FILE=C^
8590 @cindex @option{^-C^/BIND_FILE=C^} (@command{gnatlink})
8591 If instead of generating a file in Ada, the binder has generated one in
8592 C, then the linker needs to know about it. Use this switch to signal
8593 to @command{gnatlink} that the binder has generated C code rather than
8594 Ada code.
8595
8596 @item ^-f^/FORCE_OBJECT_FILE_LIST^
8597 @cindex Command line length
8598 @cindex @option{^-f^/FORCE_OBJECT_FILE_LIST^} (@command{gnatlink})
8599 On some targets, the command line length is limited, and @command{gnatlink}
8600 will generate a separate file for the linker if the list of object files
8601 is too long.
8602 The @option{^-f^/FORCE_OBJECT_FILE_LIST^} switch forces this file
8603 to be generated even if
8604 the limit is not exceeded. This is useful in some cases to deal with
8605 special situations where the command line length is exceeded.
8606
8607 @item ^-g^/DEBUG^
8608 @cindex Debugging information, including
8609 @cindex @option{^-g^/DEBUG^} (@command{gnatlink})
8610 The option to include debugging information causes the Ada bind file (in
8611 other words, @file{b~@var{mainprog}.adb}) to be compiled with
8612 @option{^-g^/DEBUG^}.
8613 In addition, the binder does not delete the @file{b~@var{mainprog}.adb},
8614 @file{b~@var{mainprog}.o} and @file{b~@var{mainprog}.ali} files.
8615 Without @option{^-g^/DEBUG^}, the binder removes these files by
8616 default. The same procedure apply if a C bind file was generated using
8617 @option{^-C^/BIND_FILE=C^} @code{gnatbind} option, in this case the filenames
8618 are @file{b_@var{mainprog}.c} and @file{b_@var{mainprog}.o}.
8619
8620 @item ^-n^/NOCOMPILE^
8621 @cindex @option{^-n^/NOCOMPILE^} (@command{gnatlink})
8622 Do not compile the file generated by the binder. This may be used when
8623 a link is rerun with different options, but there is no need to recompile
8624 the binder file.
8625
8626 @item ^-v^/VERBOSE^
8627 @cindex @option{^-v^/VERBOSE^} (@command{gnatlink})
8628 Causes additional information to be output, including a full list of the
8629 included object files. This switch option is most useful when you want
8630 to see what set of object files are being used in the link step.
8631
8632 @item ^-v -v^/VERBOSE/VERBOSE^
8633 @cindex @option{^-v -v^/VERBOSE/VERBOSE^} (@command{gnatlink})
8634 Very verbose mode. Requests that the compiler operate in verbose mode when
8635 it compiles the binder file, and that the system linker run in verbose mode.
8636
8637 @item ^-o ^/EXECUTABLE=^@var{exec-name}
8638 @cindex @option{^-o^/EXECUTABLE^} (@command{gnatlink})
8639 @var{exec-name} specifies an alternate name for the generated
8640 executable program. If this switch is omitted, the executable has the same
8641 name as the main unit. For example, @code{gnatlink try.ali} creates
8642 an executable called @file{^try^TRY.EXE^}.
8643
8644 @ifclear vms
8645 @item -b @var{target}
8646 @cindex @option{-b} (@command{gnatlink})
8647 Compile your program to run on @var{target}, which is the name of a
8648 system configuration. You must have a GNAT cross-compiler built if
8649 @var{target} is not the same as your host system.
8650
8651 @item -B@var{dir}
8652 @cindex @option{-B} (@command{gnatlink})
8653 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
8654 from @var{dir} instead of the default location. Only use this switch
8655 when multiple versions of the GNAT compiler are available.
8656 @xref{Directory Options,,, gcc, The GNU Compiler Collection},
8657 for further details. You would normally use the @option{-b} or
8658 @option{-V} switch instead.
8659
8660 @item --GCC=@var{compiler_name}
8661 @cindex @option{--GCC=compiler_name} (@command{gnatlink})
8662 Program used for compiling the binder file. The default is
8663 @command{gcc}. You need to use quotes around @var{compiler_name} if
8664 @code{compiler_name} contains spaces or other separator characters.
8665 As an example @option{--GCC="foo -x -y"} will instruct @command{gnatlink} to
8666 use @code{foo -x -y} as your compiler. Note that switch @option{-c} is always
8667 inserted after your command name. Thus in the above example the compiler
8668 command that will be used by @command{gnatlink} will be @code{foo -c -x -y}.
8669 A limitation of this syntax is that the name and path name of the executable
8670 itself must not include any embedded spaces. If the compiler executable is
8671 different from the default one (gcc or <prefix>-gcc), then the back-end
8672 switches in the ALI file are not used to compile the binder generated source.
8673 For example, this is the case with @option{--GCC="foo -x -y"}. But the back end
8674 switches will be used for @option{--GCC="gcc -gnatv"}. If several
8675 @option{--GCC=compiler_name} are used, only the last @var{compiler_name}
8676 is taken into account. However, all the additional switches are also taken
8677 into account. Thus,
8678 @option{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
8679 @option{--GCC="bar -x -y -z -t"}.
8680
8681 @item --LINK=@var{name}
8682 @cindex @option{--LINK=} (@command{gnatlink})
8683 @var{name} is the name of the linker to be invoked. This is especially
8684 useful in mixed language programs since languages such as C++ require
8685 their own linker to be used. When this switch is omitted, the default
8686 name for the linker is @command{gcc}. When this switch is used, the
8687 specified linker is called instead of @command{gcc} with exactly the same
8688 parameters that would have been passed to @command{gcc} so if the desired
8689 linker requires different parameters it is necessary to use a wrapper
8690 script that massages the parameters before invoking the real linker. It
8691 may be useful to control the exact invocation by using the verbose
8692 switch.
8693
8694 @end ifclear
8695
8696 @ifset vms
8697 @item /DEBUG=TRACEBACK
8698 @cindex @code{/DEBUG=TRACEBACK} (@command{gnatlink})
8699 This qualifier causes sufficient information to be included in the
8700 executable file to allow a traceback, but does not include the full
8701 symbol information needed by the debugger.
8702
8703 @item /IDENTIFICATION="<string>"
8704 @code{"<string>"} specifies the string to be stored in the image file
8705 identification field in the image header.
8706 It overrides any pragma @code{Ident} specified string.
8707
8708 @item /NOINHIBIT-EXEC
8709 Generate the executable file even if there are linker warnings.
8710
8711 @item /NOSTART_FILES
8712 Don't link in the object file containing the ``main'' transfer address.
8713 Used when linking with a foreign language main program compiled with an
8714 HP compiler.
8715
8716 @item /STATIC
8717 Prefer linking with object libraries over sharable images, even without
8718 /DEBUG.
8719 @end ifset
8720
8721 @end table
8722
8723 @node The GNAT Make Program gnatmake
8724 @chapter The GNAT Make Program @command{gnatmake}
8725 @findex gnatmake
8726
8727 @menu
8728 * Running gnatmake::
8729 * Switches for gnatmake::
8730 * Mode Switches for gnatmake::
8731 * Notes on the Command Line::
8732 * How gnatmake Works::
8733 * Examples of gnatmake Usage::
8734 @end menu
8735 @noindent
8736 A typical development cycle when working on an Ada program consists of
8737 the following steps:
8738
8739 @enumerate
8740 @item
8741 Edit some sources to fix bugs.
8742
8743 @item
8744 Add enhancements.
8745
8746 @item
8747 Compile all sources affected.
8748
8749 @item
8750 Rebind and relink.
8751
8752 @item
8753 Test.
8754 @end enumerate
8755
8756 @noindent
8757 The third step can be tricky, because not only do the modified files
8758 @cindex Dependency rules
8759 have to be compiled, but any files depending on these files must also be
8760 recompiled. The dependency rules in Ada can be quite complex, especially
8761 in the presence of overloading, @code{use} clauses, generics and inlined
8762 subprograms.
8763
8764 @command{gnatmake} automatically takes care of the third and fourth steps
8765 of this process. It determines which sources need to be compiled,
8766 compiles them, and binds and links the resulting object files.
8767
8768 Unlike some other Ada make programs, the dependencies are always
8769 accurately recomputed from the new sources. The source based approach of
8770 the GNAT compilation model makes this possible. This means that if
8771 changes to the source program cause corresponding changes in
8772 dependencies, they will always be tracked exactly correctly by
8773 @command{gnatmake}.
8774
8775 @node Running gnatmake
8776 @section Running @command{gnatmake}
8777
8778 @noindent
8779 The usual form of the @command{gnatmake} command is
8780
8781 @smallexample
8782 $ gnatmake @ovar{switches} @var{file_name}
8783 @ovar{file_names} @ovar{mode_switches}
8784 @end smallexample
8785
8786 @noindent
8787 The only required argument is one @var{file_name}, which specifies
8788 a compilation unit that is a main program. Several @var{file_names} can be
8789 specified: this will result in several executables being built.
8790 If @code{switches} are present, they can be placed before the first
8791 @var{file_name}, between @var{file_names} or after the last @var{file_name}.
8792 If @var{mode_switches} are present, they must always be placed after
8793 the last @var{file_name} and all @code{switches}.
8794
8795 If you are using standard file extensions (@file{.adb} and @file{.ads}), then the
8796 extension may be omitted from the @var{file_name} arguments. However, if
8797 you are using non-standard extensions, then it is required that the
8798 extension be given. A relative or absolute directory path can be
8799 specified in a @var{file_name}, in which case, the input source file will
8800 be searched for in the specified directory only. Otherwise, the input
8801 source file will first be searched in the directory where
8802 @command{gnatmake} was invoked and if it is not found, it will be search on
8803 the source path of the compiler as described in
8804 @ref{Search Paths and the Run-Time Library (RTL)}.
8805
8806 All @command{gnatmake} output (except when you specify
8807 @option{^-M^/DEPENDENCIES_LIST^}) is to
8808 @file{stderr}. The output produced by the
8809 @option{^-M^/DEPENDENCIES_LIST^} switch is send to
8810 @file{stdout}.
8811
8812 @node Switches for gnatmake
8813 @section Switches for @command{gnatmake}
8814
8815 @noindent
8816 You may specify any of the following switches to @command{gnatmake}:
8817
8818 @table @option
8819 @c !sort!
8820
8821 @item --version
8822 @cindex @option{--version} @command{gnatmake}
8823 Display Copyright and version, then exit disregarding all other options.
8824
8825 @item --help
8826 @cindex @option{--help} @command{gnatmake}
8827 If @option{--version} was not used, display usage, then exit disregarding
8828 all other options.
8829
8830 @ifclear vms
8831 @item --GCC=@var{compiler_name}
8832 @cindex @option{--GCC=compiler_name} (@command{gnatmake})
8833 Program used for compiling. The default is `@command{gcc}'. You need to use
8834 quotes around @var{compiler_name} if @code{compiler_name} contains
8835 spaces or other separator characters. As an example @option{--GCC="foo -x
8836 -y"} will instruct @command{gnatmake} to use @code{foo -x -y} as your
8837 compiler. A limitation of this syntax is that the name and path name of
8838 the executable itself must not include any embedded spaces. Note that
8839 switch @option{-c} is always inserted after your command name. Thus in the
8840 above example the compiler command that will be used by @command{gnatmake}
8841 will be @code{foo -c -x -y}. If several @option{--GCC=compiler_name} are
8842 used, only the last @var{compiler_name} is taken into account. However,
8843 all the additional switches are also taken into account. Thus,
8844 @option{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
8845 @option{--GCC="bar -x -y -z -t"}.
8846
8847 @item --GNATBIND=@var{binder_name}
8848 @cindex @option{--GNATBIND=binder_name} (@command{gnatmake})
8849 Program used for binding. The default is `@code{gnatbind}'. You need to
8850 use quotes around @var{binder_name} if @var{binder_name} contains spaces
8851 or other separator characters. As an example @option{--GNATBIND="bar -x
8852 -y"} will instruct @command{gnatmake} to use @code{bar -x -y} as your
8853 binder. Binder switches that are normally appended by @command{gnatmake}
8854 to `@code{gnatbind}' are now appended to the end of @code{bar -x -y}.
8855 A limitation of this syntax is that the name and path name of the executable
8856 itself must not include any embedded spaces.
8857
8858 @item --GNATLINK=@var{linker_name}
8859 @cindex @option{--GNATLINK=linker_name} (@command{gnatmake})
8860 Program used for linking. The default is `@command{gnatlink}'. You need to
8861 use quotes around @var{linker_name} if @var{linker_name} contains spaces
8862 or other separator characters. As an example @option{--GNATLINK="lan -x
8863 -y"} will instruct @command{gnatmake} to use @code{lan -x -y} as your
8864 linker. Linker switches that are normally appended by @command{gnatmake} to
8865 `@command{gnatlink}' are now appended to the end of @code{lan -x -y}.
8866 A limitation of this syntax is that the name and path name of the executable
8867 itself must not include any embedded spaces.
8868
8869 @end ifclear
8870
8871 @item ^-a^/ALL_FILES^
8872 @cindex @option{^-a^/ALL_FILES^} (@command{gnatmake})
8873 Consider all files in the make process, even the GNAT internal system
8874 files (for example, the predefined Ada library files), as well as any
8875 locked files. Locked files are files whose ALI file is write-protected.
8876 By default,
8877 @command{gnatmake} does not check these files,
8878 because the assumption is that the GNAT internal files are properly up
8879 to date, and also that any write protected ALI files have been properly
8880 installed. Note that if there is an installation problem, such that one
8881 of these files is not up to date, it will be properly caught by the
8882 binder.
8883 You may have to specify this switch if you are working on GNAT
8884 itself. The switch @option{^-a^/ALL_FILES^} is also useful
8885 in conjunction with @option{^-f^/FORCE_COMPILE^}
8886 if you need to recompile an entire application,
8887 including run-time files, using special configuration pragmas,
8888 such as a @code{Normalize_Scalars} pragma.
8889
8890 By default
8891 @code{gnatmake ^-a^/ALL_FILES^} compiles all GNAT
8892 internal files with
8893 @ifclear vms
8894 @code{gcc -c -gnatpg} rather than @code{gcc -c}.
8895 @end ifclear
8896 @ifset vms
8897 the @code{/CHECKS=SUPPRESS_ALL /STYLE_CHECKS=GNAT} switch.
8898 @end ifset
8899
8900 @item ^-b^/ACTIONS=BIND^
8901 @cindex @option{^-b^/ACTIONS=BIND^} (@command{gnatmake})
8902 Bind only. Can be combined with @option{^-c^/ACTIONS=COMPILE^} to do
8903 compilation and binding, but no link.
8904 Can be combined with @option{^-l^/ACTIONS=LINK^}
8905 to do binding and linking. When not combined with
8906 @option{^-c^/ACTIONS=COMPILE^}
8907 all the units in the closure of the main program must have been previously
8908 compiled and must be up to date. The root unit specified by @var{file_name}
8909 may be given without extension, with the source extension or, if no GNAT
8910 Project File is specified, with the ALI file extension.
8911
8912 @item ^-c^/ACTIONS=COMPILE^
8913 @cindex @option{^-c^/ACTIONS=COMPILE^} (@command{gnatmake})
8914 Compile only. Do not perform binding, except when @option{^-b^/ACTIONS=BIND^}
8915 is also specified. Do not perform linking, except if both
8916 @option{^-b^/ACTIONS=BIND^} and
8917 @option{^-l^/ACTIONS=LINK^} are also specified.
8918 If the root unit specified by @var{file_name} is not a main unit, this is the
8919 default. Otherwise @command{gnatmake} will attempt binding and linking
8920 unless all objects are up to date and the executable is more recent than
8921 the objects.
8922
8923 @item ^-C^/MAPPING^
8924 @cindex @option{^-C^/MAPPING^} (@command{gnatmake})
8925 Use a temporary mapping file. A mapping file is a way to communicate to the
8926 compiler two mappings: from unit names to file names (without any directory
8927 information) and from file names to path names (with full directory
8928 information). These mappings are used by the compiler to short-circuit the path
8929 search. When @command{gnatmake} is invoked with this switch, it will create
8930 a temporary mapping file, initially populated by the project manager,
8931 if @option{^-P^/PROJECT_FILE^} is used, otherwise initially empty.
8932 Each invocation of the compiler will add the newly accessed sources to the
8933 mapping file. This will improve the source search during the next invocation
8934 of the compiler.
8935
8936 @item ^-C=^/USE_MAPPING_FILE=^@var{file}
8937 @cindex @option{^-C=^/USE_MAPPING^} (@command{gnatmake})
8938 Use a specific mapping file. The file, specified as a path name (absolute or
8939 relative) by this switch, should already exist, otherwise the switch is
8940 ineffective. The specified mapping file will be communicated to the compiler.
8941 This switch is not compatible with a project file
8942 (^-P^/PROJECT_FILE=^@var{file}) or with multiple compiling processes
8943 (^-j^/PROCESSES=^nnn, when nnn is greater than 1).
8944
8945 @item ^-d^/DISPLAY_PROGRESS^
8946 @cindex @option{^-d^/DISPLAY_PROGRESS^} (@command{gnatmake})
8947 Display progress for each source, up to date or not, as a single line
8948
8949 @smallexample
8950 completed x out of y (zz%)
8951 @end smallexample
8952
8953 If the file needs to be compiled this is displayed after the invocation of
8954 the compiler. These lines are displayed even in quiet output mode.
8955
8956 @item ^-D ^/DIRECTORY_OBJECTS=^@var{dir}
8957 @cindex @option{^-D^/DIRECTORY_OBJECTS^} (@command{gnatmake})
8958 Put all object files and ALI file in directory @var{dir}.
8959 If the @option{^-D^/DIRECTORY_OBJECTS^} switch is not used, all object files
8960 and ALI files go in the current working directory.
8961
8962 This switch cannot be used when using a project file.
8963
8964 @ifclear vms
8965 @item -eL
8966 @cindex @option{-eL} (@command{gnatmake})
8967 Follow all symbolic links when processing project files.
8968 @end ifclear
8969
8970 @item ^-eS^/STANDARD_OUTPUT_FOR_COMMANDS^
8971 @cindex @option{^-eS^/STANDARD_OUTPUT_FOR_COMMANDS^} (@command{gnatmake})
8972 Output the commands for the compiler, the binder and the linker
8973 on ^standard output^SYS$OUTPUT^,
8974 instead of ^standard error^SYS$ERROR^.
8975
8976 @item ^-f^/FORCE_COMPILE^
8977 @cindex @option{^-f^/FORCE_COMPILE^} (@command{gnatmake})
8978 Force recompilations. Recompile all sources, even though some object
8979 files may be up to date, but don't recompile predefined or GNAT internal
8980 files or locked files (files with a write-protected ALI file),
8981 unless the @option{^-a^/ALL_FILES^} switch is also specified.
8982
8983 @item ^-F^/FULL_PATH_IN_BRIEF_MESSAGES^
8984 @cindex @option{^-F^/FULL_PATH_IN_BRIEF_MESSAGES^} (@command{gnatmake})
8985 When using project files, if some errors or warnings are detected during
8986 parsing and verbose mode is not in effect (no use of switch
8987 ^-v^/VERBOSE^), then error lines start with the full path name of the project
8988 file, rather than its simple file name.
8989
8990 @item ^-g^/DEBUG^
8991 @cindex @option{^-g^/DEBUG^} (@command{gnatmake})
8992 Enable debugging. This switch is simply passed to the compiler and to the
8993 linker.
8994
8995 @item ^-i^/IN_PLACE^
8996 @cindex @option{^-i^/IN_PLACE^} (@command{gnatmake})
8997 In normal mode, @command{gnatmake} compiles all object files and ALI files
8998 into the current directory. If the @option{^-i^/IN_PLACE^} switch is used,
8999 then instead object files and ALI files that already exist are overwritten
9000 in place. This means that once a large project is organized into separate
9001 directories in the desired manner, then @command{gnatmake} will automatically
9002 maintain and update this organization. If no ALI files are found on the
9003 Ada object path (@ref{Search Paths and the Run-Time Library (RTL)}),
9004 the new object and ALI files are created in the
9005 directory containing the source being compiled. If another organization
9006 is desired, where objects and sources are kept in different directories,
9007 a useful technique is to create dummy ALI files in the desired directories.
9008 When detecting such a dummy file, @command{gnatmake} will be forced to
9009 recompile the corresponding source file, and it will be put the resulting
9010 object and ALI files in the directory where it found the dummy file.
9011
9012 @item ^-j^/PROCESSES=^@var{n}
9013 @cindex @option{^-j^/PROCESSES^} (@command{gnatmake})
9014 @cindex Parallel make
9015 Use @var{n} processes to carry out the (re)compilations. On a
9016 multiprocessor machine compilations will occur in parallel. In the
9017 event of compilation errors, messages from various compilations might
9018 get interspersed (but @command{gnatmake} will give you the full ordered
9019 list of failing compiles at the end). If this is problematic, rerun
9020 the make process with n set to 1 to get a clean list of messages.
9021
9022 @item ^-k^/CONTINUE_ON_ERROR^
9023 @cindex @option{^-k^/CONTINUE_ON_ERROR^} (@command{gnatmake})
9024 Keep going. Continue as much as possible after a compilation error. To
9025 ease the programmer's task in case of compilation errors, the list of
9026 sources for which the compile fails is given when @command{gnatmake}
9027 terminates.
9028
9029 If @command{gnatmake} is invoked with several @file{file_names} and with this
9030 switch, if there are compilation errors when building an executable,
9031 @command{gnatmake} will not attempt to build the following executables.
9032
9033 @item ^-l^/ACTIONS=LINK^
9034 @cindex @option{^-l^/ACTIONS=LINK^} (@command{gnatmake})
9035 Link only. Can be combined with @option{^-b^/ACTIONS=BIND^} to binding
9036 and linking. Linking will not be performed if combined with
9037 @option{^-c^/ACTIONS=COMPILE^}
9038 but not with @option{^-b^/ACTIONS=BIND^}.
9039 When not combined with @option{^-b^/ACTIONS=BIND^}
9040 all the units in the closure of the main program must have been previously
9041 compiled and must be up to date, and the main program needs to have been bound.
9042 The root unit specified by @var{file_name}
9043 may be given without extension, with the source extension or, if no GNAT
9044 Project File is specified, with the ALI file extension.
9045
9046 @item ^-m^/MINIMAL_RECOMPILATION^
9047 @cindex @option{^-m^/MINIMAL_RECOMPILATION^} (@command{gnatmake})
9048 Specify that the minimum necessary amount of recompilations
9049 be performed. In this mode @command{gnatmake} ignores time
9050 stamp differences when the only
9051 modifications to a source file consist in adding/removing comments,
9052 empty lines, spaces or tabs. This means that if you have changed the
9053 comments in a source file or have simply reformatted it, using this
9054 switch will tell @command{gnatmake} not to recompile files that depend on it
9055 (provided other sources on which these files depend have undergone no
9056 semantic modifications). Note that the debugging information may be
9057 out of date with respect to the sources if the @option{-m} switch causes
9058 a compilation to be switched, so the use of this switch represents a
9059 trade-off between compilation time and accurate debugging information.
9060
9061 @item ^-M^/DEPENDENCIES_LIST^
9062 @cindex Dependencies, producing list
9063 @cindex @option{^-M^/DEPENDENCIES_LIST^} (@command{gnatmake})
9064 Check if all objects are up to date. If they are, output the object
9065 dependences to @file{stdout} in a form that can be directly exploited in
9066 a @file{Makefile}. By default, each source file is prefixed with its
9067 (relative or absolute) directory name. This name is whatever you
9068 specified in the various @option{^-aI^/SOURCE_SEARCH^}
9069 and @option{^-I^/SEARCH^} switches. If you use
9070 @code{gnatmake ^-M^/DEPENDENCIES_LIST^}
9071 @option{^-q^/QUIET^}
9072 (see below), only the source file names,
9073 without relative paths, are output. If you just specify the
9074 @option{^-M^/DEPENDENCIES_LIST^}
9075 switch, dependencies of the GNAT internal system files are omitted. This
9076 is typically what you want. If you also specify
9077 the @option{^-a^/ALL_FILES^} switch,
9078 dependencies of the GNAT internal files are also listed. Note that
9079 dependencies of the objects in external Ada libraries (see switch
9080 @option{^-aL^/SKIP_MISSING=^}@var{dir} in the following list)
9081 are never reported.
9082
9083 @item ^-n^/DO_OBJECT_CHECK^
9084 @cindex @option{^-n^/DO_OBJECT_CHECK^} (@command{gnatmake})
9085 Don't compile, bind, or link. Checks if all objects are up to date.
9086 If they are not, the full name of the first file that needs to be
9087 recompiled is printed.
9088 Repeated use of this option, followed by compiling the indicated source
9089 file, will eventually result in recompiling all required units.
9090
9091 @item ^-o ^/EXECUTABLE=^@var{exec_name}
9092 @cindex @option{^-o^/EXECUTABLE^} (@command{gnatmake})
9093 Output executable name. The name of the final executable program will be
9094 @var{exec_name}. If the @option{^-o^/EXECUTABLE^} switch is omitted the default
9095 name for the executable will be the name of the input file in appropriate form
9096 for an executable file on the host system.
9097
9098 This switch cannot be used when invoking @command{gnatmake} with several
9099 @file{file_names}.
9100
9101 @item ^-p or --create-missing-dirs^/CREATE_MISSING_DIRS^
9102 @cindex @option{^-p^/CREATE_MISSING_DIRS^} (@command{gnatmake})
9103 When using project files (^-P^/PROJECT_FILE=^@var{project}), create
9104 automatically missing object directories, library directories and exec
9105 directories.
9106
9107 @item ^-P^/PROJECT_FILE=^@var{project}
9108 @cindex @option{^-P^/PROJECT_FILE^} (@command{gnatmake})
9109 Use project file @var{project}. Only one such switch can be used.
9110 @xref{gnatmake and Project Files}.
9111
9112 @item ^-q^/QUIET^
9113 @cindex @option{^-q^/QUIET^} (@command{gnatmake})
9114 Quiet. When this flag is not set, the commands carried out by
9115 @command{gnatmake} are displayed.
9116
9117 @item ^-s^/SWITCH_CHECK/^
9118 @cindex @option{^-s^/SWITCH_CHECK^} (@command{gnatmake})
9119 Recompile if compiler switches have changed since last compilation.
9120 All compiler switches but -I and -o are taken into account in the
9121 following way:
9122 orders between different ``first letter'' switches are ignored, but
9123 orders between same switches are taken into account. For example,
9124 @option{-O -O2} is different than @option{-O2 -O}, but @option{-g -O}
9125 is equivalent to @option{-O -g}.
9126
9127 This switch is recommended when Integrated Preprocessing is used.
9128
9129 @item ^-u^/UNIQUE^
9130 @cindex @option{^-u^/UNIQUE^} (@command{gnatmake})
9131 Unique. Recompile at most the main files. It implies -c. Combined with
9132 -f, it is equivalent to calling the compiler directly. Note that using
9133 ^-u^/UNIQUE^ with a project file and no main has a special meaning
9134 (@pxref{Project Files and Main Subprograms}).
9135
9136 @item ^-U^/ALL_PROJECTS^
9137 @cindex @option{^-U^/ALL_PROJECTS^} (@command{gnatmake})
9138 When used without a project file or with one or several mains on the command
9139 line, is equivalent to ^-u^/UNIQUE^. When used with a project file and no main
9140 on the command line, all sources of all project files are checked and compiled
9141 if not up to date, and libraries are rebuilt, if necessary.
9142
9143 @item ^-v^/REASONS^
9144 @cindex @option{^-v^/REASONS^} (@command{gnatmake})
9145 Verbose. Display the reason for all recompilations @command{gnatmake}
9146 decides are necessary, with the highest verbosity level.
9147
9148 @item ^-vl^/LOW_VERBOSITY^
9149 @cindex @option{^-vl^/LOW_VERBOSITY^} (@command{gnatmake})
9150 Verbosity level Low. Display fewer lines than in verbosity Medium.
9151
9152 @item ^-vm^/MEDIUM_VERBOSITY^
9153 @cindex @option{^-vm^/MEDIUM_VERBOSITY^} (@command{gnatmake})
9154 Verbosity level Medium. Potentially display fewer lines than in verbosity High.
9155
9156 @item ^-vh^/HIGH_VERBOSITY^
9157 @cindex @option{^-vm^/HIGH_VERBOSITY^} (@command{gnatmake})
9158 Verbosity level High. Equivalent to ^-v^/REASONS^.
9159
9160 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
9161 Indicate the verbosity of the parsing of GNAT project files.
9162 @xref{Switches Related to Project Files}.
9163
9164 @item ^-x^/NON_PROJECT_UNIT_COMPILATION^
9165 @cindex @option{^-x^/NON_PROJECT_UNIT_COMPILATION^} (@command{gnatmake})
9166 Indicate that sources that are not part of any Project File may be compiled.
9167 Normally, when using Project Files, only sources that are part of a Project
9168 File may be compile. When this switch is used, a source outside of all Project
9169 Files may be compiled. The ALI file and the object file will be put in the
9170 object directory of the main Project. The compilation switches used will only
9171 be those specified on the command line. Even when
9172 @option{^-x^/NON_PROJECT_UNIT_COMPILATION^} is used, mains specified on the
9173 command line need to be sources of a project file.
9174
9175 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
9176 Indicate that external variable @var{name} has the value @var{value}.
9177 The Project Manager will use this value for occurrences of
9178 @code{external(name)} when parsing the project file.
9179 @xref{Switches Related to Project Files}.
9180
9181 @item ^-z^/NOMAIN^
9182 @cindex @option{^-z^/NOMAIN^} (@command{gnatmake})
9183 No main subprogram. Bind and link the program even if the unit name
9184 given on the command line is a package name. The resulting executable
9185 will execute the elaboration routines of the package and its closure,
9186 then the finalization routines.
9187
9188 @end table
9189
9190 @table @asis
9191 @item @command{gcc} @asis{switches}
9192 @ifclear vms
9193 Any uppercase or multi-character switch that is not a @command{gnatmake} switch
9194 is passed to @command{gcc} (e.g.@: @option{-O}, @option{-gnato,} etc.)
9195 @end ifclear
9196 @ifset vms
9197 Any qualifier that cannot be recognized as a qualifier for @code{GNAT MAKE}
9198 but is recognizable as a valid qualifier for @code{GNAT COMPILE} is
9199 automatically treated as a compiler switch, and passed on to all
9200 compilations that are carried out.
9201 @end ifset
9202 @end table
9203
9204 @noindent
9205 Source and library search path switches:
9206
9207 @table @option
9208 @c !sort!
9209 @item ^-aI^/SOURCE_SEARCH=^@var{dir}
9210 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatmake})
9211 When looking for source files also look in directory @var{dir}.
9212 The order in which source files search is undertaken is
9213 described in @ref{Search Paths and the Run-Time Library (RTL)}.
9214
9215 @item ^-aL^/SKIP_MISSING=^@var{dir}
9216 @cindex @option{^-aL^/SKIP_MISSING^} (@command{gnatmake})
9217 Consider @var{dir} as being an externally provided Ada library.
9218 Instructs @command{gnatmake} to skip compilation units whose @file{.ALI}
9219 files have been located in directory @var{dir}. This allows you to have
9220 missing bodies for the units in @var{dir} and to ignore out of date bodies
9221 for the same units. You still need to specify
9222 the location of the specs for these units by using the switches
9223 @option{^-aI^/SOURCE_SEARCH=^@var{dir}}
9224 or @option{^-I^/SEARCH=^@var{dir}}.
9225 Note: this switch is provided for compatibility with previous versions
9226 of @command{gnatmake}. The easier method of causing standard libraries
9227 to be excluded from consideration is to write-protect the corresponding
9228 ALI files.
9229
9230 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
9231 @cindex @option{^-aO^/OBJECT_SEARCH^} (@command{gnatmake})
9232 When searching for library and object files, look in directory
9233 @var{dir}. The order in which library files are searched is described in
9234 @ref{Search Paths for gnatbind}.
9235
9236 @item ^-A^/CONDITIONAL_SOURCE_SEARCH=^@var{dir}
9237 @cindex Search paths, for @command{gnatmake}
9238 @cindex @option{^-A^/CONDITIONAL_SOURCE_SEARCH^} (@command{gnatmake})
9239 Equivalent to @option{^-aL^/SKIP_MISSING=^@var{dir}
9240 ^-aI^/SOURCE_SEARCH=^@var{dir}}.
9241
9242 @item ^-I^/SEARCH=^@var{dir}
9243 @cindex @option{^-I^/SEARCH^} (@command{gnatmake})
9244 Equivalent to @option{^-aO^/OBJECT_SEARCH=^@var{dir}
9245 ^-aI^/SOURCE_SEARCH=^@var{dir}}.
9246
9247 @item ^-I-^/NOCURRENT_DIRECTORY^
9248 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gnatmake})
9249 @cindex Source files, suppressing search
9250 Do not look for source files in the directory containing the source
9251 file named in the command line.
9252 Do not look for ALI or object files in the directory
9253 where @command{gnatmake} was invoked.
9254
9255 @item ^-L^/LIBRARY_SEARCH=^@var{dir}
9256 @cindex @option{^-L^/LIBRARY_SEARCH^} (@command{gnatmake})
9257 @cindex Linker libraries
9258 Add directory @var{dir} to the list of directories in which the linker
9259 will search for libraries. This is equivalent to
9260 @option{-largs ^-L^/LIBRARY_SEARCH=^}@var{dir}.
9261 @ifclear vms
9262 Furthermore, under Windows, the sources pointed to by the libraries path
9263 set in the registry are not searched for.
9264 @end ifclear
9265
9266 @item -nostdinc
9267 @cindex @option{-nostdinc} (@command{gnatmake})
9268 Do not look for source files in the system default directory.
9269
9270 @item -nostdlib
9271 @cindex @option{-nostdlib} (@command{gnatmake})
9272 Do not look for library files in the system default directory.
9273
9274 @item --RTS=@var{rts-path}
9275 @cindex @option{--RTS} (@command{gnatmake})
9276 Specifies the default location of the runtime library. GNAT looks for the
9277 runtime
9278 in the following directories, and stops as soon as a valid runtime is found
9279 (@file{adainclude} or @file{ada_source_path}, and @file{adalib} or
9280 @file{ada_object_path} present):
9281
9282 @itemize @bullet
9283 @item <current directory>/$rts_path
9284
9285 @item <default-search-dir>/$rts_path
9286
9287 @item <default-search-dir>/rts-$rts_path
9288 @end itemize
9289
9290 @noindent
9291 The selected path is handled like a normal RTS path.
9292
9293 @end table
9294
9295 @node Mode Switches for gnatmake
9296 @section Mode Switches for @command{gnatmake}
9297
9298 @noindent
9299 The mode switches (referred to as @code{mode_switches}) allow the
9300 inclusion of switches that are to be passed to the compiler itself, the
9301 binder or the linker. The effect of a mode switch is to cause all
9302 subsequent switches up to the end of the switch list, or up to the next
9303 mode switch, to be interpreted as switches to be passed on to the
9304 designated component of GNAT.
9305
9306 @table @option
9307 @c !sort!
9308 @item -cargs @var{switches}
9309 @cindex @option{-cargs} (@command{gnatmake})
9310 Compiler switches. Here @var{switches} is a list of switches
9311 that are valid switches for @command{gcc}. They will be passed on to
9312 all compile steps performed by @command{gnatmake}.
9313
9314 @item -bargs @var{switches}
9315 @cindex @option{-bargs} (@command{gnatmake})
9316 Binder switches. Here @var{switches} is a list of switches
9317 that are valid switches for @code{gnatbind}. They will be passed on to
9318 all bind steps performed by @command{gnatmake}.
9319
9320 @item -largs @var{switches}
9321 @cindex @option{-largs} (@command{gnatmake})
9322 Linker switches. Here @var{switches} is a list of switches
9323 that are valid switches for @command{gnatlink}. They will be passed on to
9324 all link steps performed by @command{gnatmake}.
9325
9326 @item -margs @var{switches}
9327 @cindex @option{-margs} (@command{gnatmake})
9328 Make switches. The switches are directly interpreted by @command{gnatmake},
9329 regardless of any previous occurrence of @option{-cargs}, @option{-bargs}
9330 or @option{-largs}.
9331 @end table
9332
9333 @node Notes on the Command Line
9334 @section Notes on the Command Line
9335
9336 @noindent
9337 This section contains some additional useful notes on the operation
9338 of the @command{gnatmake} command.
9339
9340 @itemize @bullet
9341 @item
9342 @cindex Recompilation, by @command{gnatmake}
9343 If @command{gnatmake} finds no ALI files, it recompiles the main program
9344 and all other units required by the main program.
9345 This means that @command{gnatmake}
9346 can be used for the initial compile, as well as during subsequent steps of
9347 the development cycle.
9348
9349 @item
9350 If you enter @code{gnatmake @var{file}.adb}, where @file{@var{file}.adb}
9351 is a subunit or body of a generic unit, @command{gnatmake} recompiles
9352 @file{@var{file}.adb} (because it finds no ALI) and stops, issuing a
9353 warning.
9354
9355 @item
9356 In @command{gnatmake} the switch @option{^-I^/SEARCH^}
9357 is used to specify both source and
9358 library file paths. Use @option{^-aI^/SOURCE_SEARCH^}
9359 instead if you just want to specify
9360 source paths only and @option{^-aO^/OBJECT_SEARCH^}
9361 if you want to specify library paths
9362 only.
9363
9364 @item
9365 @command{gnatmake} will ignore any files whose ALI file is write-protected.
9366 This may conveniently be used to exclude standard libraries from
9367 consideration and in particular it means that the use of the
9368 @option{^-f^/FORCE_COMPILE^} switch will not recompile these files
9369 unless @option{^-a^/ALL_FILES^} is also specified.
9370
9371 @item
9372 @command{gnatmake} has been designed to make the use of Ada libraries
9373 particularly convenient. Assume you have an Ada library organized
9374 as follows: @i{^obj-dir^[OBJ_DIR]^} contains the objects and ALI files for
9375 of your Ada compilation units,
9376 whereas @i{^include-dir^[INCLUDE_DIR]^} contains the
9377 specs of these units, but no bodies. Then to compile a unit
9378 stored in @code{main.adb}, which uses this Ada library you would just type
9379
9380 @smallexample
9381 @ifclear vms
9382 $ gnatmake -aI@var{include-dir} -aL@var{obj-dir} main
9383 @end ifclear
9384 @ifset vms
9385 $ gnatmake /SOURCE_SEARCH=@i{[INCLUDE_DIR]}
9386 /SKIP_MISSING=@i{[OBJ_DIR]} main
9387 @end ifset
9388 @end smallexample
9389
9390 @item
9391 Using @command{gnatmake} along with the
9392 @option{^-m (minimal recompilation)^/MINIMAL_RECOMPILATION^}
9393 switch provides a mechanism for avoiding unnecessary recompilations. Using
9394 this switch,
9395 you can update the comments/format of your
9396 source files without having to recompile everything. Note, however, that
9397 adding or deleting lines in a source files may render its debugging
9398 info obsolete. If the file in question is a spec, the impact is rather
9399 limited, as that debugging info will only be useful during the
9400 elaboration phase of your program. For bodies the impact can be more
9401 significant. In all events, your debugger will warn you if a source file
9402 is more recent than the corresponding object, and alert you to the fact
9403 that the debugging information may be out of date.
9404 @end itemize
9405
9406 @node How gnatmake Works
9407 @section How @command{gnatmake} Works
9408
9409 @noindent
9410 Generally @command{gnatmake} automatically performs all necessary
9411 recompilations and you don't need to worry about how it works. However,
9412 it may be useful to have some basic understanding of the @command{gnatmake}
9413 approach and in particular to understand how it uses the results of
9414 previous compilations without incorrectly depending on them.
9415
9416 First a definition: an object file is considered @dfn{up to date} if the
9417 corresponding ALI file exists and if all the source files listed in the
9418 dependency section of this ALI file have time stamps matching those in
9419 the ALI file. This means that neither the source file itself nor any
9420 files that it depends on have been modified, and hence there is no need
9421 to recompile this file.
9422
9423 @command{gnatmake} works by first checking if the specified main unit is up
9424 to date. If so, no compilations are required for the main unit. If not,
9425 @command{gnatmake} compiles the main program to build a new ALI file that
9426 reflects the latest sources. Then the ALI file of the main unit is
9427 examined to find all the source files on which the main program depends,
9428 and @command{gnatmake} recursively applies the above procedure on all these
9429 files.
9430
9431 This process ensures that @command{gnatmake} only trusts the dependencies
9432 in an existing ALI file if they are known to be correct. Otherwise it
9433 always recompiles to determine a new, guaranteed accurate set of
9434 dependencies. As a result the program is compiled ``upside down'' from what may
9435 be more familiar as the required order of compilation in some other Ada
9436 systems. In particular, clients are compiled before the units on which
9437 they depend. The ability of GNAT to compile in any order is critical in
9438 allowing an order of compilation to be chosen that guarantees that
9439 @command{gnatmake} will recompute a correct set of new dependencies if
9440 necessary.
9441
9442 When invoking @command{gnatmake} with several @var{file_names}, if a unit is
9443 imported by several of the executables, it will be recompiled at most once.
9444
9445 Note: when using non-standard naming conventions
9446 (@pxref{Using Other File Names}), changing through a configuration pragmas
9447 file the version of a source and invoking @command{gnatmake} to recompile may
9448 have no effect, if the previous version of the source is still accessible
9449 by @command{gnatmake}. It may be necessary to use the switch
9450 ^-f^/FORCE_COMPILE^.
9451
9452 @node Examples of gnatmake Usage
9453 @section Examples of @command{gnatmake} Usage
9454
9455 @table @code
9456 @item gnatmake hello.adb
9457 Compile all files necessary to bind and link the main program
9458 @file{hello.adb} (containing unit @code{Hello}) and bind and link the
9459 resulting object files to generate an executable file @file{^hello^HELLO.EXE^}.
9460
9461 @item gnatmake main1 main2 main3
9462 Compile all files necessary to bind and link the main programs
9463 @file{main1.adb} (containing unit @code{Main1}), @file{main2.adb}
9464 (containing unit @code{Main2}) and @file{main3.adb}
9465 (containing unit @code{Main3}) and bind and link the resulting object files
9466 to generate three executable files @file{^main1^MAIN1.EXE^},
9467 @file{^main2^MAIN2.EXE^}
9468 and @file{^main3^MAIN3.EXE^}.
9469
9470 @ifclear vms
9471 @item gnatmake -q Main_Unit -cargs -O2 -bargs -l
9472 @end ifclear
9473
9474 @ifset vms
9475 @item gnatmake Main_Unit /QUIET
9476 /COMPILER_QUALIFIERS /OPTIMIZE=ALL
9477 /BINDER_QUALIFIERS /ORDER_OF_ELABORATION
9478 @end ifset
9479 Compile all files necessary to bind and link the main program unit
9480 @code{Main_Unit} (from file @file{main_unit.adb}). All compilations will
9481 be done with optimization level 2 and the order of elaboration will be
9482 listed by the binder. @command{gnatmake} will operate in quiet mode, not
9483 displaying commands it is executing.
9484 @end table
9485
9486 @c *************************
9487 @node Improving Performance
9488 @chapter Improving Performance
9489 @cindex Improving performance
9490
9491 @noindent
9492 This chapter presents several topics related to program performance.
9493 It first describes some of the tradeoffs that need to be considered
9494 and some of the techniques for making your program run faster.
9495 It then documents the @command{gnatelim} tool and unused subprogram/data
9496 elimination feature, which can reduce the size of program executables.
9497
9498 Note: to invoke @command{gnatelim} with a project file, use the @code{gnat}
9499 driver (see @ref{The GNAT Driver and Project Files}).
9500
9501 @ifnottex
9502 @menu
9503 * Performance Considerations::
9504 * Text_IO Suggestions::
9505 * Reducing Size of Ada Executables with gnatelim::
9506 * Reducing Size of Executables with unused subprogram/data elimination::
9507 @end menu
9508 @end ifnottex
9509
9510 @c *****************************
9511 @node Performance Considerations
9512 @section Performance Considerations
9513
9514 @noindent
9515 The GNAT system provides a number of options that allow a trade-off
9516 between
9517
9518 @itemize @bullet
9519 @item
9520 performance of the generated code
9521
9522 @item
9523 speed of compilation
9524
9525 @item
9526 minimization of dependences and recompilation
9527
9528 @item
9529 the degree of run-time checking.
9530 @end itemize
9531
9532 @noindent
9533 The defaults (if no options are selected) aim at improving the speed
9534 of compilation and minimizing dependences, at the expense of performance
9535 of the generated code:
9536
9537 @itemize @bullet
9538 @item
9539 no optimization
9540
9541 @item
9542 no inlining of subprogram calls
9543
9544 @item
9545 all run-time checks enabled except overflow and elaboration checks
9546 @end itemize
9547
9548 @noindent
9549 These options are suitable for most program development purposes. This
9550 chapter describes how you can modify these choices, and also provides
9551 some guidelines on debugging optimized code.
9552
9553 @menu
9554 * Controlling Run-Time Checks::
9555 * Use of Restrictions::
9556 * Optimization Levels::
9557 * Debugging Optimized Code::
9558 * Inlining of Subprograms::
9559 * Other Optimization Switches::
9560 * Optimization and Strict Aliasing::
9561
9562 @ifset vms
9563 * Coverage Analysis::
9564 @end ifset
9565 @end menu
9566
9567 @node Controlling Run-Time Checks
9568 @subsection Controlling Run-Time Checks
9569
9570 @noindent
9571 By default, GNAT generates all run-time checks, except integer overflow
9572 checks, stack overflow checks, and checks for access before elaboration on
9573 subprogram calls. The latter are not required in default mode, because all
9574 necessary checking is done at compile time.
9575 @cindex @option{-gnatp} (@command{gcc})
9576 @cindex @option{-gnato} (@command{gcc})
9577 Two gnat switches, @option{-gnatp} and @option{-gnato} allow this default to
9578 be modified. @xref{Run-Time Checks}.
9579
9580 Our experience is that the default is suitable for most development
9581 purposes.
9582
9583 We treat integer overflow specially because these
9584 are quite expensive and in our experience are not as important as other
9585 run-time checks in the development process. Note that division by zero
9586 is not considered an overflow check, and divide by zero checks are
9587 generated where required by default.
9588
9589 Elaboration checks are off by default, and also not needed by default, since
9590 GNAT uses a static elaboration analysis approach that avoids the need for
9591 run-time checking. This manual contains a full chapter discussing the issue
9592 of elaboration checks, and if the default is not satisfactory for your use,
9593 you should read this chapter.
9594
9595 For validity checks, the minimal checks required by the Ada Reference
9596 Manual (for case statements and assignments to array elements) are on
9597 by default. These can be suppressed by use of the @option{-gnatVn} switch.
9598 Note that in Ada 83, there were no validity checks, so if the Ada 83 mode
9599 is acceptable (or when comparing GNAT performance with an Ada 83 compiler),
9600 it may be reasonable to routinely use @option{-gnatVn}. Validity checks
9601 are also suppressed entirely if @option{-gnatp} is used.
9602
9603 @cindex Overflow checks
9604 @cindex Checks, overflow
9605 @findex Suppress
9606 @findex Unsuppress
9607 @cindex pragma Suppress
9608 @cindex pragma Unsuppress
9609 Note that the setting of the switches controls the default setting of
9610 the checks. They may be modified using either @code{pragma Suppress} (to
9611 remove checks) or @code{pragma Unsuppress} (to add back suppressed
9612 checks) in the program source.
9613
9614 @node Use of Restrictions
9615 @subsection Use of Restrictions
9616
9617 @noindent
9618 The use of pragma Restrictions allows you to control which features are
9619 permitted in your program. Apart from the obvious point that if you avoid
9620 relatively expensive features like finalization (enforceable by the use
9621 of pragma Restrictions (No_Finalization), the use of this pragma does not
9622 affect the generated code in most cases.
9623
9624 One notable exception to this rule is that the possibility of task abort
9625 results in some distributed overhead, particularly if finalization or
9626 exception handlers are used. The reason is that certain sections of code
9627 have to be marked as non-abortable.
9628
9629 If you use neither the @code{abort} statement, nor asynchronous transfer
9630 of control (@code{select @dots{} then abort}), then this distributed overhead
9631 is removed, which may have a general positive effect in improving
9632 overall performance. Especially code involving frequent use of tasking
9633 constructs and controlled types will show much improved performance.
9634 The relevant restrictions pragmas are
9635
9636 @smallexample @c ada
9637 pragma Restrictions (No_Abort_Statements);
9638 pragma Restrictions (Max_Asynchronous_Select_Nesting => 0);
9639 @end smallexample
9640
9641 @noindent
9642 It is recommended that these restriction pragmas be used if possible. Note
9643 that this also means that you can write code without worrying about the
9644 possibility of an immediate abort at any point.
9645
9646 @node Optimization Levels
9647 @subsection Optimization Levels
9648 @cindex @option{^-O^/OPTIMIZE^} (@command{gcc})
9649
9650 @noindent
9651 Without any optimization ^option,^qualifier,^
9652 the compiler's goal is to reduce the cost of
9653 compilation and to make debugging produce the expected results.
9654 Statements are independent: if you stop the program with a breakpoint between
9655 statements, you can then assign a new value to any variable or change
9656 the program counter to any other statement in the subprogram and get exactly
9657 the results you would expect from the source code.
9658
9659 Turning on optimization makes the compiler attempt to improve the
9660 performance and/or code size at the expense of compilation time and
9661 possibly the ability to debug the program.
9662
9663 If you use multiple
9664 ^-O options, with or without level numbers,^/OPTIMIZE qualifiers,^
9665 the last such option is the one that is effective.
9666
9667 @noindent
9668 The default is optimization off. This results in the fastest compile
9669 times, but GNAT makes absolutely no attempt to optimize, and the
9670 generated programs are considerably larger and slower than when
9671 optimization is enabled. You can use the
9672 @ifclear vms
9673 @option{-O} switch (the permitted forms are @option{-O0}, @option{-O1}
9674 @option{-O2}, @option{-O3}, and @option{-Os})
9675 @end ifclear
9676 @ifset vms
9677 @code{OPTIMIZE} qualifier
9678 @end ifset
9679 to @command{gcc} to control the optimization level:
9680
9681 @table @option
9682 @item ^-O0^/OPTIMIZE=NONE^
9683 No optimization (the default);
9684 generates unoptimized code but has
9685 the fastest compilation time.
9686
9687 Note that many other compilers do fairly extensive optimization
9688 even if ``no optimization'' is specified. With gcc, it is
9689 very unusual to use ^-O0^/OPTIMIZE=NONE^ for production if
9690 execution time is of any concern, since ^-O0^/OPTIMIZE=NONE^
9691 really does mean no optimization at all. This difference between
9692 gcc and other compilers should be kept in mind when doing
9693 performance comparisons.
9694
9695 @item ^-O1^/OPTIMIZE=SOME^
9696 Moderate optimization;
9697 optimizes reasonably well but does not
9698 degrade compilation time significantly.
9699
9700 @item ^-O2^/OPTIMIZE=ALL^
9701 @ifset vms
9702 @itemx /OPTIMIZE=DEVELOPMENT
9703 @end ifset
9704 Full optimization;
9705 generates highly optimized code and has
9706 the slowest compilation time.
9707
9708 @item ^-O3^/OPTIMIZE=INLINING^
9709 Full optimization as in @option{-O2},
9710 and also attempts automatic inlining of small
9711 subprograms within a unit (@pxref{Inlining of Subprograms}).
9712
9713 @item ^-Os^/OPTIMIZE=SPACE^
9714 Optimize space usage of resulting program.
9715 @end table
9716
9717 @noindent
9718 Higher optimization levels perform more global transformations on the
9719 program and apply more expensive analysis algorithms in order to generate
9720 faster and more compact code. The price in compilation time, and the
9721 resulting improvement in execution time,
9722 both depend on the particular application and the hardware environment.
9723 You should experiment to find the best level for your application.
9724
9725 Since the precise set of optimizations done at each level will vary from
9726 release to release (and sometime from target to target), it is best to think
9727 of the optimization settings in general terms.
9728 @xref{Optimize Options,, Options That Control Optimization, gcc, Using
9729 the GNU Compiler Collection (GCC)}, for details about
9730 ^the @option{-O} settings and a number of @option{-f} options that^how to^
9731 individually enable or disable specific optimizations.
9732
9733 Unlike some other compilation systems, ^@command{gcc}^GNAT^ has
9734 been tested extensively at all optimization levels. There are some bugs
9735 which appear only with optimization turned on, but there have also been
9736 bugs which show up only in @emph{unoptimized} code. Selecting a lower
9737 level of optimization does not improve the reliability of the code
9738 generator, which in practice is highly reliable at all optimization
9739 levels.
9740
9741 Note regarding the use of @option{-O3}: The use of this optimization level
9742 is generally discouraged with GNAT, since it often results in larger
9743 executables which run more slowly. See further discussion of this point
9744 in @ref{Inlining of Subprograms}.
9745
9746 @node Debugging Optimized Code
9747 @subsection Debugging Optimized Code
9748 @cindex Debugging optimized code
9749 @cindex Optimization and debugging
9750
9751 @noindent
9752 Although it is possible to do a reasonable amount of debugging at
9753 @ifclear vms
9754 nonzero optimization levels,
9755 the higher the level the more likely that
9756 @end ifclear
9757 @ifset vms
9758 @option{/OPTIMIZE} settings other than @code{NONE},
9759 such settings will make it more likely that
9760 @end ifset
9761 source-level constructs will have been eliminated by optimization.
9762 For example, if a loop is strength-reduced, the loop
9763 control variable may be completely eliminated and thus cannot be
9764 displayed in the debugger.
9765 This can only happen at @option{-O2} or @option{-O3}.
9766 Explicit temporary variables that you code might be eliminated at
9767 ^level^setting^ @option{-O1} or higher.
9768
9769 The use of the @option{^-g^/DEBUG^} switch,
9770 @cindex @option{^-g^/DEBUG^} (@command{gcc})
9771 which is needed for source-level debugging,
9772 affects the size of the program executable on disk,
9773 and indeed the debugging information can be quite large.
9774 However, it has no effect on the generated code (and thus does not
9775 degrade performance)
9776
9777 Since the compiler generates debugging tables for a compilation unit before
9778 it performs optimizations, the optimizing transformations may invalidate some
9779 of the debugging data. You therefore need to anticipate certain
9780 anomalous situations that may arise while debugging optimized code.
9781 These are the most common cases:
9782
9783 @enumerate
9784 @item
9785 @i{The ``hopping Program Counter'':} Repeated @code{step} or @code{next}
9786 commands show
9787 the PC bouncing back and forth in the code. This may result from any of
9788 the following optimizations:
9789
9790 @itemize @bullet
9791 @item
9792 @i{Common subexpression elimination:} using a single instance of code for a
9793 quantity that the source computes several times. As a result you
9794 may not be able to stop on what looks like a statement.
9795
9796 @item
9797 @i{Invariant code motion:} moving an expression that does not change within a
9798 loop, to the beginning of the loop.
9799
9800 @item
9801 @i{Instruction scheduling:} moving instructions so as to
9802 overlap loads and stores (typically) with other code, or in
9803 general to move computations of values closer to their uses. Often
9804 this causes you to pass an assignment statement without the assignment
9805 happening and then later bounce back to the statement when the
9806 value is actually needed. Placing a breakpoint on a line of code
9807 and then stepping over it may, therefore, not always cause all the
9808 expected side-effects.
9809 @end itemize
9810
9811 @item
9812 @i{The ``big leap'':} More commonly known as @emph{cross-jumping}, in which
9813 two identical pieces of code are merged and the program counter suddenly
9814 jumps to a statement that is not supposed to be executed, simply because
9815 it (and the code following) translates to the same thing as the code
9816 that @emph{was} supposed to be executed. This effect is typically seen in
9817 sequences that end in a jump, such as a @code{goto}, a @code{return}, or
9818 a @code{break} in a C @code{^switch^switch^} statement.
9819
9820 @item
9821 @i{The ``roving variable'':} The symptom is an unexpected value in a variable.
9822 There are various reasons for this effect:
9823
9824 @itemize @bullet
9825 @item
9826 In a subprogram prologue, a parameter may not yet have been moved to its
9827 ``home''.
9828
9829 @item
9830 A variable may be dead, and its register re-used. This is
9831 probably the most common cause.
9832
9833 @item
9834 As mentioned above, the assignment of a value to a variable may
9835 have been moved.
9836
9837 @item
9838 A variable may be eliminated entirely by value propagation or
9839 other means. In this case, GCC may incorrectly generate debugging
9840 information for the variable
9841 @end itemize
9842
9843 @noindent
9844 In general, when an unexpected value appears for a local variable or parameter
9845 you should first ascertain if that value was actually computed by
9846 your program, as opposed to being incorrectly reported by the debugger.
9847 Record fields or
9848 array elements in an object designated by an access value
9849 are generally less of a problem, once you have ascertained that the access
9850 value is sensible.
9851 Typically, this means checking variables in the preceding code and in the
9852 calling subprogram to verify that the value observed is explainable from other
9853 values (one must apply the procedure recursively to those
9854 other values); or re-running the code and stopping a little earlier
9855 (perhaps before the call) and stepping to better see how the variable obtained
9856 the value in question; or continuing to step @emph{from} the point of the
9857 strange value to see if code motion had simply moved the variable's
9858 assignments later.
9859 @end enumerate
9860
9861 @noindent
9862 In light of such anomalies, a recommended technique is to use @option{-O0}
9863 early in the software development cycle, when extensive debugging capabilities
9864 are most needed, and then move to @option{-O1} and later @option{-O2} as
9865 the debugger becomes less critical.
9866 Whether to use the @option{^-g^/DEBUG^} switch in the release version is
9867 a release management issue.
9868 @ifclear vms
9869 Note that if you use @option{-g} you can then use the @command{strip} program
9870 on the resulting executable,
9871 which removes both debugging information and global symbols.
9872 @end ifclear
9873
9874 @node Inlining of Subprograms
9875 @subsection Inlining of Subprograms
9876
9877 @noindent
9878 A call to a subprogram in the current unit is inlined if all the
9879 following conditions are met:
9880
9881 @itemize @bullet
9882 @item
9883 The optimization level is at least @option{-O1}.
9884
9885 @item
9886 The called subprogram is suitable for inlining: It must be small enough
9887 and not contain something that @command{gcc} cannot support in inlined
9888 subprograms.
9889
9890 @item
9891 @cindex pragma Inline
9892 @findex Inline
9893 Either @code{pragma Inline} applies to the subprogram, or it is local
9894 to the unit and called once from within it, or it is small and automatic
9895 inlining (optimization level @option{-O3}) is specified.
9896 @end itemize
9897
9898 @noindent
9899 Calls to subprograms in @code{with}'ed units are normally not inlined.
9900 To achieve actual inlining (that is, replacement of the call by the code
9901 in the body of the subprogram), the following conditions must all be true.
9902
9903 @itemize @bullet
9904 @item
9905 The optimization level is at least @option{-O1}.
9906
9907 @item
9908 The called subprogram is suitable for inlining: It must be small enough
9909 and not contain something that @command{gcc} cannot support in inlined
9910 subprograms.
9911
9912 @item
9913 The call appears in a body (not in a package spec).
9914
9915 @item
9916 There is a @code{pragma Inline} for the subprogram.
9917
9918 @item
9919 @cindex @option{-gnatn} (@command{gcc})
9920 The @option{^-gnatn^/INLINE^} switch
9921 is used in the @command{gcc} command line
9922 @end itemize
9923
9924 Even if all these conditions are met, it may not be possible for
9925 the compiler to inline the call, due to the length of the body,
9926 or features in the body that make it impossible for the compiler
9927 to do the inlining.
9928
9929 Note that specifying the @option{-gnatn} switch causes additional
9930 compilation dependencies. Consider the following:
9931
9932 @smallexample @c ada
9933 @cartouche
9934 package R is
9935 procedure Q;
9936 pragma Inline (Q);
9937 end R;
9938 package body R is
9939 @dots{}
9940 end R;
9941
9942 with R;
9943 procedure Main is
9944 begin
9945 @dots{}
9946 R.Q;
9947 end Main;
9948 @end cartouche
9949 @end smallexample
9950
9951 @noindent
9952 With the default behavior (no @option{-gnatn} switch specified), the
9953 compilation of the @code{Main} procedure depends only on its own source,
9954 @file{main.adb}, and the spec of the package in file @file{r.ads}. This
9955 means that editing the body of @code{R} does not require recompiling
9956 @code{Main}.
9957
9958 On the other hand, the call @code{R.Q} is not inlined under these
9959 circumstances. If the @option{-gnatn} switch is present when @code{Main}
9960 is compiled, the call will be inlined if the body of @code{Q} is small
9961 enough, but now @code{Main} depends on the body of @code{R} in
9962 @file{r.adb} as well as on the spec. This means that if this body is edited,
9963 the main program must be recompiled. Note that this extra dependency
9964 occurs whether or not the call is in fact inlined by @command{gcc}.
9965
9966 The use of front end inlining with @option{-gnatN} generates similar
9967 additional dependencies.
9968
9969 @cindex @option{^-fno-inline^/INLINE=SUPPRESS^} (@command{gcc})
9970 Note: The @option{^-fno-inline^/INLINE=SUPPRESS^} switch
9971 can be used to prevent
9972 all inlining. This switch overrides all other conditions and ensures
9973 that no inlining occurs. The extra dependences resulting from
9974 @option{-gnatn} will still be active, even if
9975 this switch is used to suppress the resulting inlining actions.
9976
9977 @cindex @option{-fno-inline-functions} (@command{gcc})
9978 Note: The @option{-fno-inline-functions} switch can be used to prevent
9979 automatic inlining of small subprograms if @option{-O3} is used.
9980
9981 @cindex @option{-fno-inline-functions-called-once} (@command{gcc})
9982 Note: The @option{-fno-inline-functions-called-once} switch
9983 can be used to prevent inlining of subprograms local to the unit
9984 and called once from within it if @option{-O1} is used.
9985
9986 Note regarding the use of @option{-O3}: There is no difference in inlining
9987 behavior between @option{-O2} and @option{-O3} for subprograms with an explicit
9988 pragma @code{Inline} assuming the use of @option{-gnatn}
9989 or @option{-gnatN} (the switches that activate inlining). If you have used
9990 pragma @code{Inline} in appropriate cases, then it is usually much better
9991 to use @option{-O2} and @option{-gnatn} and avoid the use of @option{-O3} which
9992 in this case only has the effect of inlining subprograms you did not
9993 think should be inlined. We often find that the use of @option{-O3} slows
9994 down code by performing excessive inlining, leading to increased instruction
9995 cache pressure from the increased code size. So the bottom line here is
9996 that you should not automatically assume that @option{-O3} is better than
9997 @option{-O2}, and indeed you should use @option{-O3} only if tests show that
9998 it actually improves performance.
9999
10000 @node Other Optimization Switches
10001 @subsection Other Optimization Switches
10002 @cindex Optimization Switches
10003
10004 Since @code{GNAT} uses the @command{gcc} back end, all the specialized
10005 @command{gcc} optimization switches are potentially usable. These switches
10006 have not been extensively tested with GNAT but can generally be expected
10007 to work. Examples of switches in this category are
10008 @option{-funroll-loops} and
10009 the various target-specific @option{-m} options (in particular, it has been
10010 observed that @option{-march=pentium4} can significantly improve performance
10011 on appropriate machines). For full details of these switches, see
10012 @ref{Submodel Options,, Hardware Models and Configurations, gcc, Using
10013 the GNU Compiler Collection (GCC)}.
10014
10015 @node Optimization and Strict Aliasing
10016 @subsection Optimization and Strict Aliasing
10017 @cindex Aliasing
10018 @cindex Strict Aliasing
10019 @cindex No_Strict_Aliasing
10020
10021 @noindent
10022 The strong typing capabilities of Ada allow an optimizer to generate
10023 efficient code in situations where other languages would be forced to
10024 make worst case assumptions preventing such optimizations. Consider
10025 the following example:
10026
10027 @smallexample @c ada
10028 @cartouche
10029 procedure R is
10030 type Int1 is new Integer;
10031 type Int2 is new Integer;
10032 type Int1A is access Int1;
10033 type Int2A is access Int2;
10034 Int1V : Int1A;
10035 Int2V : Int2A;
10036 @dots{}
10037
10038 begin
10039 @dots{}
10040 for J in Data'Range loop
10041 if Data (J) = Int1V.all then
10042 Int2V.all := Int2V.all + 1;
10043 end if;
10044 end loop;
10045 @dots{}
10046 end R;
10047 @end cartouche
10048 @end smallexample
10049
10050 @noindent
10051 In this example, since the variable @code{Int1V} can only access objects
10052 of type @code{Int1}, and @code{Int2V} can only access objects of type
10053 @code{Int2}, there is no possibility that the assignment to
10054 @code{Int2V.all} affects the value of @code{Int1V.all}. This means that
10055 the compiler optimizer can "know" that the value @code{Int1V.all} is constant
10056 for all iterations of the loop and avoid the extra memory reference
10057 required to dereference it each time through the loop.
10058
10059 This kind of optimization, called strict aliasing analysis, is
10060 triggered by specifying an optimization level of @option{-O2} or
10061 higher and allows @code{GNAT} to generate more efficient code
10062 when access values are involved.
10063
10064 However, although this optimization is always correct in terms of
10065 the formal semantics of the Ada Reference Manual, difficulties can
10066 arise if features like @code{Unchecked_Conversion} are used to break
10067 the typing system. Consider the following complete program example:
10068
10069 @smallexample @c ada
10070 @cartouche
10071 package p1 is
10072 type int1 is new integer;
10073 type int2 is new integer;
10074 type a1 is access int1;
10075 type a2 is access int2;
10076 end p1;
10077
10078 with p1; use p1;
10079 package p2 is
10080 function to_a2 (Input : a1) return a2;
10081 end p2;
10082
10083 with Unchecked_Conversion;
10084 package body p2 is
10085 function to_a2 (Input : a1) return a2 is
10086 function to_a2u is
10087 new Unchecked_Conversion (a1, a2);
10088 begin
10089 return to_a2u (Input);
10090 end to_a2;
10091 end p2;
10092
10093 with p2; use p2;
10094 with p1; use p1;
10095 with Text_IO; use Text_IO;
10096 procedure m is
10097 v1 : a1 := new int1;
10098 v2 : a2 := to_a2 (v1);
10099 begin
10100 v1.all := 1;
10101 v2.all := 0;
10102 put_line (int1'image (v1.all));
10103 end;
10104 @end cartouche
10105 @end smallexample
10106
10107 @noindent
10108 This program prints out 0 in @option{-O0} or @option{-O1}
10109 mode, but it prints out 1 in @option{-O2} mode. That's
10110 because in strict aliasing mode, the compiler can and
10111 does assume that the assignment to @code{v2.all} could not
10112 affect the value of @code{v1.all}, since different types
10113 are involved.
10114
10115 This behavior is not a case of non-conformance with the standard, since
10116 the Ada RM specifies that an unchecked conversion where the resulting
10117 bit pattern is not a correct value of the target type can result in an
10118 abnormal value and attempting to reference an abnormal value makes the
10119 execution of a program erroneous. That's the case here since the result
10120 does not point to an object of type @code{int2}. This means that the
10121 effect is entirely unpredictable.
10122
10123 However, although that explanation may satisfy a language
10124 lawyer, in practice an applications programmer expects an
10125 unchecked conversion involving pointers to create true
10126 aliases and the behavior of printing 1 seems plain wrong.
10127 In this case, the strict aliasing optimization is unwelcome.
10128
10129 Indeed the compiler recognizes this possibility, and the
10130 unchecked conversion generates a warning:
10131
10132 @smallexample
10133 p2.adb:5:07: warning: possible aliasing problem with type "a2"
10134 p2.adb:5:07: warning: use -fno-strict-aliasing switch for references
10135 p2.adb:5:07: warning: or use "pragma No_Strict_Aliasing (a2);"
10136 @end smallexample
10137
10138 @noindent
10139 Unfortunately the problem is recognized when compiling the body of
10140 package @code{p2}, but the actual "bad" code is generated while
10141 compiling the body of @code{m} and this latter compilation does not see
10142 the suspicious @code{Unchecked_Conversion}.
10143
10144 As implied by the warning message, there are approaches you can use to
10145 avoid the unwanted strict aliasing optimization in a case like this.
10146
10147 One possibility is to simply avoid the use of @option{-O2}, but
10148 that is a bit drastic, since it throws away a number of useful
10149 optimizations that do not involve strict aliasing assumptions.
10150
10151 A less drastic approach is to compile the program using the
10152 option @option{-fno-strict-aliasing}. Actually it is only the
10153 unit containing the dereferencing of the suspicious pointer
10154 that needs to be compiled. So in this case, if we compile
10155 unit @code{m} with this switch, then we get the expected
10156 value of zero printed. Analyzing which units might need
10157 the switch can be painful, so a more reasonable approach
10158 is to compile the entire program with options @option{-O2}
10159 and @option{-fno-strict-aliasing}. If the performance is
10160 satisfactory with this combination of options, then the
10161 advantage is that the entire issue of possible "wrong"
10162 optimization due to strict aliasing is avoided.
10163
10164 To avoid the use of compiler switches, the configuration
10165 pragma @code{No_Strict_Aliasing} with no parameters may be
10166 used to specify that for all access types, the strict
10167 aliasing optimization should be suppressed.
10168
10169 However, these approaches are still overkill, in that they causes
10170 all manipulations of all access values to be deoptimized. A more
10171 refined approach is to concentrate attention on the specific
10172 access type identified as problematic.
10173
10174 First, if a careful analysis of uses of the pointer shows
10175 that there are no possible problematic references, then
10176 the warning can be suppressed by bracketing the
10177 instantiation of @code{Unchecked_Conversion} to turn
10178 the warning off:
10179
10180 @smallexample @c ada
10181 pragma Warnings (Off);
10182 function to_a2u is
10183 new Unchecked_Conversion (a1, a2);
10184 pragma Warnings (On);
10185 @end smallexample
10186
10187 @noindent
10188 Of course that approach is not appropriate for this particular
10189 example, since indeed there is a problematic reference. In this
10190 case we can take one of two other approaches.
10191
10192 The first possibility is to move the instantiation of unchecked
10193 conversion to the unit in which the type is declared. In
10194 this example, we would move the instantiation of
10195 @code{Unchecked_Conversion} from the body of package
10196 @code{p2} to the spec of package @code{p1}. Now the
10197 warning disappears. That's because any use of the
10198 access type knows there is a suspicious unchecked
10199 conversion, and the strict aliasing optimization
10200 is automatically suppressed for the type.
10201
10202 If it is not practical to move the unchecked conversion to the same unit
10203 in which the destination access type is declared (perhaps because the
10204 source type is not visible in that unit), you may use pragma
10205 @code{No_Strict_Aliasing} for the type. This pragma must occur in the
10206 same declarative sequence as the declaration of the access type:
10207
10208 @smallexample @c ada
10209 type a2 is access int2;
10210 pragma No_Strict_Aliasing (a2);
10211 @end smallexample
10212
10213 @noindent
10214 Here again, the compiler now knows that the strict aliasing optimization
10215 should be suppressed for any reference to type @code{a2} and the
10216 expected behavior is obtained.
10217
10218 Finally, note that although the compiler can generate warnings for
10219 simple cases of unchecked conversions, there are tricker and more
10220 indirect ways of creating type incorrect aliases which the compiler
10221 cannot detect. Examples are the use of address overlays and unchecked
10222 conversions involving composite types containing access types as
10223 components. In such cases, no warnings are generated, but there can
10224 still be aliasing problems. One safe coding practice is to forbid the
10225 use of address clauses for type overlaying, and to allow unchecked
10226 conversion only for primitive types. This is not really a significant
10227 restriction since any possible desired effect can be achieved by
10228 unchecked conversion of access values.
10229
10230 @ifset vms
10231 @node Coverage Analysis
10232 @subsection Coverage Analysis
10233
10234 @noindent
10235 GNAT supports the HP Performance Coverage Analyzer (PCA), which allows
10236 the user to determine the distribution of execution time across a program,
10237 @pxref{Profiling} for details of usage.
10238 @end ifset
10239
10240
10241 @node Text_IO Suggestions
10242 @section @code{Text_IO} Suggestions
10243 @cindex @code{Text_IO} and performance
10244
10245 @noindent
10246 The @code{Ada.Text_IO} package has fairly high overheads due in part to
10247 the requirement of maintaining page and line counts. If performance
10248 is critical, a recommendation is to use @code{Stream_IO} instead of
10249 @code{Text_IO} for volume output, since this package has less overhead.
10250
10251 If @code{Text_IO} must be used, note that by default output to the standard
10252 output and standard error files is unbuffered (this provides better
10253 behavior when output statements are used for debugging, or if the
10254 progress of a program is observed by tracking the output, e.g. by
10255 using the Unix @command{tail -f} command to watch redirected output.
10256
10257 If you are generating large volumes of output with @code{Text_IO} and
10258 performance is an important factor, use a designated file instead
10259 of the standard output file, or change the standard output file to
10260 be buffered using @code{Interfaces.C_Streams.setvbuf}.
10261
10262
10263
10264 @node Reducing Size of Ada Executables with gnatelim
10265 @section Reducing Size of Ada Executables with @code{gnatelim}
10266 @findex gnatelim
10267
10268 @noindent
10269 This section describes @command{gnatelim}, a tool which detects unused
10270 subprograms and helps the compiler to create a smaller executable for your
10271 program.
10272
10273 @menu
10274 * About gnatelim::
10275 * Running gnatelim::
10276 * Correcting the List of Eliminate Pragmas::
10277 * Making Your Executables Smaller::
10278 * Summary of the gnatelim Usage Cycle::
10279 @end menu
10280
10281 @node About gnatelim
10282 @subsection About @code{gnatelim}
10283
10284 @noindent
10285 When a program shares a set of Ada
10286 packages with other programs, it may happen that this program uses
10287 only a fraction of the subprograms defined in these packages. The code
10288 created for these unused subprograms increases the size of the executable.
10289
10290 @code{gnatelim} tracks unused subprograms in an Ada program and
10291 outputs a list of GNAT-specific pragmas @code{Eliminate} marking all the
10292 subprograms that are declared but never called. By placing the list of
10293 @code{Eliminate} pragmas in the GNAT configuration file @file{gnat.adc} and
10294 recompiling your program, you may decrease the size of its executable,
10295 because the compiler will not generate the code for 'eliminated' subprograms.
10296 @xref{Pragma Eliminate,,, gnat_rm, GNAT Reference Manual}, for more
10297 information about this pragma.
10298
10299 @code{gnatelim} needs as its input data the name of the main subprogram
10300 and a bind file for a main subprogram.
10301
10302 To create a bind file for @code{gnatelim}, run @code{gnatbind} for
10303 the main subprogram. @code{gnatelim} can work with both Ada and C
10304 bind files; when both are present, it uses the Ada bind file.
10305 The following commands will build the program and create the bind file:
10306
10307 @smallexample
10308 $ gnatmake ^-c Main_Prog^/ACTIONS=COMPILE MAIN_PROG^
10309 $ gnatbind main_prog
10310 @end smallexample
10311
10312 Note that @code{gnatelim} needs neither object nor ALI files.
10313
10314 @node Running gnatelim
10315 @subsection Running @code{gnatelim}
10316
10317 @noindent
10318 @code{gnatelim} has the following command-line interface:
10319
10320 @smallexample
10321 $ gnatelim @ovar{options} name
10322 @end smallexample
10323
10324 @noindent
10325 @code{name} should be a name of a source file that contains the main subprogram
10326 of a program (partition).
10327
10328 @code{gnatelim} has the following switches:
10329
10330 @table @option
10331 @c !sort!
10332 @item ^-q^/QUIET^
10333 @cindex @option{^-q^/QUIET^} (@command{gnatelim})
10334 Quiet mode: by default @code{gnatelim} outputs to the standard error
10335 stream the number of program units left to be processed. This option turns
10336 this trace off.
10337
10338 @item ^-v^/VERBOSE^
10339 @cindex @option{^-v^/VERBOSE^} (@command{gnatelim})
10340 Verbose mode: @code{gnatelim} version information is printed as Ada
10341 comments to the standard output stream. Also, in addition to the number of
10342 program units left @code{gnatelim} will output the name of the current unit
10343 being processed.
10344
10345 @item ^-a^/ALL^
10346 @cindex @option{^-a^/ALL^} (@command{gnatelim})
10347 Also look for subprograms from the GNAT run time that can be eliminated. Note
10348 that when @file{gnat.adc} is produced using this switch, the entire program
10349 must be recompiled with switch @option{^-a^/ALL_FILES^} to @command{gnatmake}.
10350
10351 @item ^-I^/INCLUDE_DIRS=^@var{dir}
10352 @cindex @option{^-I^/INCLUDE_DIRS^} (@command{gnatelim})
10353 When looking for source files also look in directory @var{dir}. Specifying
10354 @option{^-I-^/INCLUDE_DIRS=-^} instructs @code{gnatelim} not to look for
10355 sources in the current directory.
10356
10357 @item ^-b^/BIND_FILE=^@var{bind_file}
10358 @cindex @option{^-b^/BIND_FILE^} (@command{gnatelim})
10359 Specifies @var{bind_file} as the bind file to process. If not set, the name
10360 of the bind file is computed from the full expanded Ada name
10361 of a main subprogram.
10362
10363 @item ^-C^/CONFIG_FILE=^@var{config_file}
10364 @cindex @option{^-C^/CONFIG_FILE^} (@command{gnatelim})
10365 Specifies a file @var{config_file} that contains configuration pragmas. The
10366 file must be specified with full path.
10367
10368 @item ^--GCC^/COMPILER^=@var{compiler_name}
10369 @cindex @option{^-GCC^/COMPILER^} (@command{gnatelim})
10370 Instructs @code{gnatelim} to use specific @command{gcc} compiler instead of one
10371 available on the path.
10372
10373 @item ^--GNATMAKE^/GNATMAKE^=@var{gnatmake_name}
10374 @cindex @option{^--GNATMAKE^/GNATMAKE^} (@command{gnatelim})
10375 Instructs @code{gnatelim} to use specific @command{gnatmake} instead of one
10376 available on the path.
10377 @end table
10378
10379 @noindent
10380 @code{gnatelim} sends its output to the standard output stream, and all the
10381 tracing and debug information is sent to the standard error stream.
10382 In order to produce a proper GNAT configuration file
10383 @file{gnat.adc}, redirection must be used:
10384
10385 @smallexample
10386 @ifset vms
10387 $ PIPE GNAT ELIM MAIN_PROG.ADB > GNAT.ADC
10388 @end ifset
10389 @ifclear vms
10390 $ gnatelim main_prog.adb > gnat.adc
10391 @end ifclear
10392 @end smallexample
10393
10394 @ifclear vms
10395 @noindent
10396 or
10397
10398 @smallexample
10399 $ gnatelim main_prog.adb >> gnat.adc
10400 @end smallexample
10401
10402 @noindent
10403 in order to append the @code{gnatelim} output to the existing contents of
10404 @file{gnat.adc}.
10405 @end ifclear
10406
10407 @node Correcting the List of Eliminate Pragmas
10408 @subsection Correcting the List of Eliminate Pragmas
10409
10410 @noindent
10411 In some rare cases @code{gnatelim} may try to eliminate
10412 subprograms that are actually called in the program. In this case, the
10413 compiler will generate an error message of the form:
10414
10415 @smallexample
10416 file.adb:106:07: cannot call eliminated subprogram "My_Prog"
10417 @end smallexample
10418
10419 @noindent
10420 You will need to manually remove the wrong @code{Eliminate} pragmas from
10421 the @file{gnat.adc} file. You should recompile your program
10422 from scratch after that, because you need a consistent @file{gnat.adc} file
10423 during the entire compilation.
10424
10425 @node Making Your Executables Smaller
10426 @subsection Making Your Executables Smaller
10427
10428 @noindent
10429 In order to get a smaller executable for your program you now have to
10430 recompile the program completely with the new @file{gnat.adc} file
10431 created by @code{gnatelim} in your current directory:
10432
10433 @smallexample
10434 $ gnatmake ^-f main_prog^/FORCE_COMPILE MAIN_PROG^
10435 @end smallexample
10436
10437 @noindent
10438 (Use the @option{^-f^/FORCE_COMPILE^} option for @command{gnatmake} to
10439 recompile everything
10440 with the set of pragmas @code{Eliminate} that you have obtained with
10441 @command{gnatelim}).
10442
10443 Be aware that the set of @code{Eliminate} pragmas is specific to each
10444 program. It is not recommended to merge sets of @code{Eliminate}
10445 pragmas created for different programs in one @file{gnat.adc} file.
10446
10447 @node Summary of the gnatelim Usage Cycle
10448 @subsection Summary of the gnatelim Usage Cycle
10449
10450 @noindent
10451 Here is a quick summary of the steps to be taken in order to reduce
10452 the size of your executables with @code{gnatelim}. You may use
10453 other GNAT options to control the optimization level,
10454 to produce the debugging information, to set search path, etc.
10455
10456 @enumerate
10457 @item
10458 Produce a bind file
10459
10460 @smallexample
10461 $ gnatmake ^-c main_prog^/ACTIONS=COMPILE MAIN_PROG^
10462 $ gnatbind main_prog
10463 @end smallexample
10464
10465 @item
10466 Generate a list of @code{Eliminate} pragmas
10467 @smallexample
10468 @ifset vms
10469 $ PIPE GNAT ELIM MAIN_PROG > GNAT.ADC
10470 @end ifset
10471 @ifclear vms
10472 $ gnatelim main_prog >@r{[}>@r{]} gnat.adc
10473 @end ifclear
10474 @end smallexample
10475
10476 @item
10477 Recompile the application
10478
10479 @smallexample
10480 $ gnatmake ^-f main_prog^/FORCE_COMPILE MAIN_PROG^
10481 @end smallexample
10482
10483 @end enumerate
10484
10485 @node Reducing Size of Executables with unused subprogram/data elimination
10486 @section Reducing Size of Executables with Unused Subprogram/Data Elimination
10487 @findex unused subprogram/data elimination
10488
10489 @noindent
10490 This section describes how you can eliminate unused subprograms and data from
10491 your executable just by setting options at compilation time.
10492
10493 @menu
10494 * About unused subprogram/data elimination::
10495 * Compilation options::
10496 * Example of unused subprogram/data elimination::
10497 @end menu
10498
10499 @node About unused subprogram/data elimination
10500 @subsection About unused subprogram/data elimination
10501
10502 @noindent
10503 By default, an executable contains all code and data of its composing objects
10504 (directly linked or coming from statically linked libraries), even data or code
10505 never used by this executable.
10506
10507 This feature will allow you to eliminate such unused code from your
10508 executable, making it smaller (in disk and in memory).
10509
10510 This functionality is available on all Linux platforms except for the IA-64
10511 architecture and on all cross platforms using the ELF binary file format.
10512 In both cases GNU binutils version 2.16 or later are required to enable it.
10513
10514 @node Compilation options
10515 @subsection Compilation options
10516
10517 @noindent
10518 The operation of eliminating the unused code and data from the final executable
10519 is directly performed by the linker.
10520
10521 In order to do this, it has to work with objects compiled with the
10522 following options:
10523 @option{-ffunction-sections} @option{-fdata-sections}.
10524 @cindex @option{-ffunction-sections} (@command{gcc})
10525 @cindex @option{-fdata-sections} (@command{gcc})
10526 These options are usable with C and Ada files.
10527 They will place respectively each
10528 function or data in a separate section in the resulting object file.
10529
10530 Once the objects and static libraries are created with these options, the
10531 linker can perform the dead code elimination. You can do this by setting
10532 the @option{-Wl,--gc-sections} option to gcc command or in the
10533 @option{-largs} section of @command{gnatmake}. This will perform a
10534 garbage collection of code and data never referenced.
10535
10536 If the linker performs a partial link (@option{-r} ld linker option), then you
10537 will need to provide one or several entry point using the
10538 @option{-e} / @option{--entry} ld option.
10539
10540 Note that objects compiled without the @option{-ffunction-sections} and
10541 @option{-fdata-sections} options can still be linked with the executable.
10542 However, no dead code elimination will be performed on those objects (they will
10543 be linked as is).
10544
10545 The GNAT static library is now compiled with -ffunction-sections and
10546 -fdata-sections on some platforms. This allows you to eliminate the unused code
10547 and data of the GNAT library from your executable.
10548
10549 @node Example of unused subprogram/data elimination
10550 @subsection Example of unused subprogram/data elimination
10551
10552 @noindent
10553 Here is a simple example:
10554
10555 @smallexample @c ada
10556 with Aux;
10557
10558 procedure Test is
10559 begin
10560 Aux.Used (10);
10561 end Test;
10562
10563 package Aux is
10564 Used_Data : Integer;
10565 Unused_Data : Integer;
10566
10567 procedure Used (Data : Integer);
10568 procedure Unused (Data : Integer);
10569 end Aux;
10570
10571 package body Aux is
10572 procedure Used (Data : Integer) is
10573 begin
10574 Used_Data := Data;
10575 end Used;
10576
10577 procedure Unused (Data : Integer) is
10578 begin
10579 Unused_Data := Data;
10580 end Unused;
10581 end Aux;
10582 @end smallexample
10583
10584 @noindent
10585 @code{Unused} and @code{Unused_Data} are never referenced in this code
10586 excerpt, and hence they may be safely removed from the final executable.
10587
10588 @smallexample
10589 $ gnatmake test
10590
10591 $ nm test | grep used
10592 020015f0 T aux__unused
10593 02005d88 B aux__unused_data
10594 020015cc T aux__used
10595 02005d84 B aux__used_data
10596
10597 $ gnatmake test -cargs -fdata-sections -ffunction-sections \
10598 -largs -Wl,--gc-sections
10599
10600 $ nm test | grep used
10601 02005350 T aux__used
10602 0201ffe0 B aux__used_data
10603 @end smallexample
10604
10605 @noindent
10606 It can be observed that the procedure @code{Unused} and the object
10607 @code{Unused_Data} are removed by the linker when using the
10608 appropriate options.
10609
10610 @c ********************************
10611 @node Renaming Files Using gnatchop
10612 @chapter Renaming Files Using @code{gnatchop}
10613 @findex gnatchop
10614
10615 @noindent
10616 This chapter discusses how to handle files with multiple units by using
10617 the @code{gnatchop} utility. This utility is also useful in renaming
10618 files to meet the standard GNAT default file naming conventions.
10619
10620 @menu
10621 * Handling Files with Multiple Units::
10622 * Operating gnatchop in Compilation Mode::
10623 * Command Line for gnatchop::
10624 * Switches for gnatchop::
10625 * Examples of gnatchop Usage::
10626 @end menu
10627
10628 @node Handling Files with Multiple Units
10629 @section Handling Files with Multiple Units
10630
10631 @noindent
10632 The basic compilation model of GNAT requires that a file submitted to the
10633 compiler have only one unit and there be a strict correspondence
10634 between the file name and the unit name.
10635
10636 The @code{gnatchop} utility allows both of these rules to be relaxed,
10637 allowing GNAT to process files which contain multiple compilation units
10638 and files with arbitrary file names. @code{gnatchop}
10639 reads the specified file and generates one or more output files,
10640 containing one unit per file. The unit and the file name correspond,
10641 as required by GNAT.
10642
10643 If you want to permanently restructure a set of ``foreign'' files so that
10644 they match the GNAT rules, and do the remaining development using the
10645 GNAT structure, you can simply use @command{gnatchop} once, generate the
10646 new set of files and work with them from that point on.
10647
10648 Alternatively, if you want to keep your files in the ``foreign'' format,
10649 perhaps to maintain compatibility with some other Ada compilation
10650 system, you can set up a procedure where you use @command{gnatchop} each
10651 time you compile, regarding the source files that it writes as temporary
10652 files that you throw away.
10653
10654 @node Operating gnatchop in Compilation Mode
10655 @section Operating gnatchop in Compilation Mode
10656
10657 @noindent
10658 The basic function of @code{gnatchop} is to take a file with multiple units
10659 and split it into separate files. The boundary between files is reasonably
10660 clear, except for the issue of comments and pragmas. In default mode, the
10661 rule is that any pragmas between units belong to the previous unit, except
10662 that configuration pragmas always belong to the following unit. Any comments
10663 belong to the following unit. These rules
10664 almost always result in the right choice of
10665 the split point without needing to mark it explicitly and most users will
10666 find this default to be what they want. In this default mode it is incorrect to
10667 submit a file containing only configuration pragmas, or one that ends in
10668 configuration pragmas, to @code{gnatchop}.
10669
10670 However, using a special option to activate ``compilation mode'',
10671 @code{gnatchop}
10672 can perform another function, which is to provide exactly the semantics
10673 required by the RM for handling of configuration pragmas in a compilation.
10674 In the absence of configuration pragmas (at the main file level), this
10675 option has no effect, but it causes such configuration pragmas to be handled
10676 in a quite different manner.
10677
10678 First, in compilation mode, if @code{gnatchop} is given a file that consists of
10679 only configuration pragmas, then this file is appended to the
10680 @file{gnat.adc} file in the current directory. This behavior provides
10681 the required behavior described in the RM for the actions to be taken
10682 on submitting such a file to the compiler, namely that these pragmas
10683 should apply to all subsequent compilations in the same compilation
10684 environment. Using GNAT, the current directory, possibly containing a
10685 @file{gnat.adc} file is the representation
10686 of a compilation environment. For more information on the
10687 @file{gnat.adc} file, see @ref{Handling of Configuration Pragmas}.
10688
10689 Second, in compilation mode, if @code{gnatchop}
10690 is given a file that starts with
10691 configuration pragmas, and contains one or more units, then these
10692 configuration pragmas are prepended to each of the chopped files. This
10693 behavior provides the required behavior described in the RM for the
10694 actions to be taken on compiling such a file, namely that the pragmas
10695 apply to all units in the compilation, but not to subsequently compiled
10696 units.
10697
10698 Finally, if configuration pragmas appear between units, they are appended
10699 to the previous unit. This results in the previous unit being illegal,
10700 since the compiler does not accept configuration pragmas that follow
10701 a unit. This provides the required RM behavior that forbids configuration
10702 pragmas other than those preceding the first compilation unit of a
10703 compilation.
10704
10705 For most purposes, @code{gnatchop} will be used in default mode. The
10706 compilation mode described above is used only if you need exactly
10707 accurate behavior with respect to compilations, and you have files
10708 that contain multiple units and configuration pragmas. In this
10709 circumstance the use of @code{gnatchop} with the compilation mode
10710 switch provides the required behavior, and is for example the mode
10711 in which GNAT processes the ACVC tests.
10712
10713 @node Command Line for gnatchop
10714 @section Command Line for @code{gnatchop}
10715
10716 @noindent
10717 The @code{gnatchop} command has the form:
10718
10719 @smallexample
10720 $ gnatchop switches @var{file name} @r{[}@var{file name} @dots{}@r{]}
10721 @ovar{directory}
10722 @end smallexample
10723
10724 @noindent
10725 The only required argument is the file name of the file to be chopped.
10726 There are no restrictions on the form of this file name. The file itself
10727 contains one or more Ada units, in normal GNAT format, concatenated
10728 together. As shown, more than one file may be presented to be chopped.
10729
10730 When run in default mode, @code{gnatchop} generates one output file in
10731 the current directory for each unit in each of the files.
10732
10733 @var{directory}, if specified, gives the name of the directory to which
10734 the output files will be written. If it is not specified, all files are
10735 written to the current directory.
10736
10737 For example, given a
10738 file called @file{hellofiles} containing
10739
10740 @smallexample @c ada
10741 @group
10742 @cartouche
10743 procedure hello;
10744
10745 with Text_IO; use Text_IO;
10746 procedure hello is
10747 begin
10748 Put_Line ("Hello");
10749 end hello;
10750 @end cartouche
10751 @end group
10752 @end smallexample
10753
10754 @noindent
10755 the command
10756
10757 @smallexample
10758 $ gnatchop ^hellofiles^HELLOFILES.^
10759 @end smallexample
10760
10761 @noindent
10762 generates two files in the current directory, one called
10763 @file{hello.ads} containing the single line that is the procedure spec,
10764 and the other called @file{hello.adb} containing the remaining text. The
10765 original file is not affected. The generated files can be compiled in
10766 the normal manner.
10767
10768 @noindent
10769 When gnatchop is invoked on a file that is empty or that contains only empty
10770 lines and/or comments, gnatchop will not fail, but will not produce any
10771 new sources.
10772
10773 For example, given a
10774 file called @file{toto.txt} containing
10775
10776 @smallexample @c ada
10777 @group
10778 @cartouche
10779 -- Just a comment
10780 @end cartouche
10781 @end group
10782 @end smallexample
10783
10784 @noindent
10785 the command
10786
10787 @smallexample
10788 $ gnatchop ^toto.txt^TOT.TXT^
10789 @end smallexample
10790
10791 @noindent
10792 will not produce any new file and will result in the following warnings:
10793
10794 @smallexample
10795 toto.txt:1:01: warning: empty file, contains no compilation units
10796 no compilation units found
10797 no source files written
10798 @end smallexample
10799
10800 @node Switches for gnatchop
10801 @section Switches for @code{gnatchop}
10802
10803 @noindent
10804 @command{gnatchop} recognizes the following switches:
10805
10806 @table @option
10807 @c !sort!
10808
10809 @item --version
10810 @cindex @option{--version} @command{gnatchop}
10811 Display Copyright and version, then exit disregarding all other options.
10812
10813 @item --help
10814 @cindex @option{--help} @command{gnatchop}
10815 If @option{--version} was not used, display usage, then exit disregarding
10816 all other options.
10817
10818 @item ^-c^/COMPILATION^
10819 @cindex @option{^-c^/COMPILATION^} (@code{gnatchop})
10820 Causes @code{gnatchop} to operate in compilation mode, in which
10821 configuration pragmas are handled according to strict RM rules. See
10822 previous section for a full description of this mode.
10823
10824 @ifclear vms
10825 @item -gnat@var{xxx}
10826 This passes the given @option{-gnat@var{xxx}} switch to @code{gnat} which is
10827 used to parse the given file. Not all @var{xxx} options make sense,
10828 but for example, the use of @option{-gnati2} allows @code{gnatchop} to
10829 process a source file that uses Latin-2 coding for identifiers.
10830 @end ifclear
10831
10832 @item ^-h^/HELP^
10833 Causes @code{gnatchop} to generate a brief help summary to the standard
10834 output file showing usage information.
10835
10836 @item ^-k@var{mm}^/FILE_NAME_MAX_LENGTH=@var{mm}^
10837 @cindex @option{^-k^/FILE_NAME_MAX_LENGTH^} (@code{gnatchop})
10838 Limit generated file names to the specified number @code{mm}
10839 of characters.
10840 This is useful if the
10841 resulting set of files is required to be interoperable with systems
10842 which limit the length of file names.
10843 @ifset vms
10844 If no value is given, or
10845 if no @code{/FILE_NAME_MAX_LENGTH} qualifier is given,
10846 a default of 39, suitable for OpenVMS Alpha
10847 Systems, is assumed
10848 @end ifset
10849 @ifclear vms
10850 No space is allowed between the @option{-k} and the numeric value. The numeric
10851 value may be omitted in which case a default of @option{-k8},
10852 suitable for use
10853 with DOS-like file systems, is used. If no @option{-k} switch
10854 is present then
10855 there is no limit on the length of file names.
10856 @end ifclear
10857
10858 @item ^-p^/PRESERVE^
10859 @cindex @option{^-p^/PRESERVE^} (@code{gnatchop})
10860 Causes the file ^modification^creation^ time stamp of the input file to be
10861 preserved and used for the time stamp of the output file(s). This may be
10862 useful for preserving coherency of time stamps in an environment where
10863 @code{gnatchop} is used as part of a standard build process.
10864
10865 @item ^-q^/QUIET^
10866 @cindex @option{^-q^/QUIET^} (@code{gnatchop})
10867 Causes output of informational messages indicating the set of generated
10868 files to be suppressed. Warnings and error messages are unaffected.
10869
10870 @item ^-r^/REFERENCE^
10871 @cindex @option{^-r^/REFERENCE^} (@code{gnatchop})
10872 @findex Source_Reference
10873 Generate @code{Source_Reference} pragmas. Use this switch if the output
10874 files are regarded as temporary and development is to be done in terms
10875 of the original unchopped file. This switch causes
10876 @code{Source_Reference} pragmas to be inserted into each of the
10877 generated files to refers back to the original file name and line number.
10878 The result is that all error messages refer back to the original
10879 unchopped file.
10880 In addition, the debugging information placed into the object file (when
10881 the @option{^-g^/DEBUG^} switch of @command{gcc} or @command{gnatmake} is
10882 specified)
10883 also refers back to this original file so that tools like profilers and
10884 debuggers will give information in terms of the original unchopped file.
10885
10886 If the original file to be chopped itself contains
10887 a @code{Source_Reference}
10888 pragma referencing a third file, then gnatchop respects
10889 this pragma, and the generated @code{Source_Reference} pragmas
10890 in the chopped file refer to the original file, with appropriate
10891 line numbers. This is particularly useful when @code{gnatchop}
10892 is used in conjunction with @code{gnatprep} to compile files that
10893 contain preprocessing statements and multiple units.
10894
10895 @item ^-v^/VERBOSE^
10896 @cindex @option{^-v^/VERBOSE^} (@code{gnatchop})
10897 Causes @code{gnatchop} to operate in verbose mode. The version
10898 number and copyright notice are output, as well as exact copies of
10899 the gnat1 commands spawned to obtain the chop control information.
10900
10901 @item ^-w^/OVERWRITE^
10902 @cindex @option{^-w^/OVERWRITE^} (@code{gnatchop})
10903 Overwrite existing file names. Normally @code{gnatchop} regards it as a
10904 fatal error if there is already a file with the same name as a
10905 file it would otherwise output, in other words if the files to be
10906 chopped contain duplicated units. This switch bypasses this
10907 check, and causes all but the last instance of such duplicated
10908 units to be skipped.
10909
10910 @ifclear vms
10911 @item --GCC=@var{xxxx}
10912 @cindex @option{--GCC=} (@code{gnatchop})
10913 Specify the path of the GNAT parser to be used. When this switch is used,
10914 no attempt is made to add the prefix to the GNAT parser executable.
10915 @end ifclear
10916 @end table
10917
10918 @node Examples of gnatchop Usage
10919 @section Examples of @code{gnatchop} Usage
10920
10921 @table @code
10922 @ifset vms
10923 @item gnatchop /OVERWRITE HELLO_S.ADA [PRERELEASE.FILES]
10924 @end ifset
10925 @ifclear vms
10926 @item gnatchop -w hello_s.ada prerelease/files
10927 @end ifclear
10928
10929 Chops the source file @file{hello_s.ada}. The output files will be
10930 placed in the directory @file{^prerelease/files^[PRERELEASE.FILES]^},
10931 overwriting any
10932 files with matching names in that directory (no files in the current
10933 directory are modified).
10934
10935 @item gnatchop ^archive^ARCHIVE.^
10936 Chops the source file @file{^archive^ARCHIVE.^}
10937 into the current directory. One
10938 useful application of @code{gnatchop} is in sending sets of sources
10939 around, for example in email messages. The required sources are simply
10940 concatenated (for example, using a ^Unix @code{cat}^VMS @code{APPEND/NEW}^
10941 command), and then
10942 @command{gnatchop} is used at the other end to reconstitute the original
10943 file names.
10944
10945 @item gnatchop file1 file2 file3 direc
10946 Chops all units in files @file{file1}, @file{file2}, @file{file3}, placing
10947 the resulting files in the directory @file{direc}. Note that if any units
10948 occur more than once anywhere within this set of files, an error message
10949 is generated, and no files are written. To override this check, use the
10950 @option{^-w^/OVERWRITE^} switch,
10951 in which case the last occurrence in the last file will
10952 be the one that is output, and earlier duplicate occurrences for a given
10953 unit will be skipped.
10954 @end table
10955
10956 @node Configuration Pragmas
10957 @chapter Configuration Pragmas
10958 @cindex Configuration pragmas
10959 @cindex Pragmas, configuration
10960
10961 @noindent
10962 Configuration pragmas include those pragmas described as
10963 such in the Ada Reference Manual, as well as
10964 implementation-dependent pragmas that are configuration pragmas.
10965 @xref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference Manual},
10966 for details on these additional GNAT-specific configuration pragmas.
10967 Most notably, the pragma @code{Source_File_Name}, which allows
10968 specifying non-default names for source files, is a configuration
10969 pragma. The following is a complete list of configuration pragmas
10970 recognized by GNAT:
10971
10972 @smallexample
10973 Ada_83
10974 Ada_95
10975 Ada_05
10976 Ada_2005
10977 Assertion_Policy
10978 C_Pass_By_Copy
10979 Check_Name
10980 Check_Policy
10981 Compile_Time_Error
10982 Compile_Time_Warning
10983 Compiler_Unit
10984 Component_Alignment
10985 Debug_Policy
10986 Detect_Blocking
10987 Discard_Names
10988 Elaboration_Checks
10989 Eliminate
10990 Extend_System
10991 External_Name_Casing
10992 Fast_Math
10993 Favor_Top_Level
10994 Float_Representation
10995 Implicit_Packing
10996 Initialize_Scalars
10997 Interrupt_State
10998 License
10999 Locking_Policy
11000 Long_Float
11001 No_Run_Time
11002 No_Strict_Aliasing
11003 Normalize_Scalars
11004 Optimize_Alignment
11005 Persistent_BSS
11006 Polling
11007 Priority_Specific_Dispatching
11008 Profile
11009 Profile_Warnings
11010 Propagate_Exceptions
11011 Queuing_Policy
11012 Ravenscar
11013 Restricted_Run_Time
11014 Restrictions
11015 Restrictions_Warnings
11016 Reviewable
11017 Source_File_Name
11018 Source_File_Name_Project
11019 Style_Checks
11020 Suppress
11021 Suppress_Exception_Locations
11022 Task_Dispatching_Policy
11023 Universal_Data
11024 Unsuppress
11025 Use_VADS_Size
11026 Validity_Checks
11027 Warnings
11028 Wide_Character_Encoding
11029
11030 @end smallexample
11031
11032 @menu
11033 * Handling of Configuration Pragmas::
11034 * The Configuration Pragmas Files::
11035 @end menu
11036
11037 @node Handling of Configuration Pragmas
11038 @section Handling of Configuration Pragmas
11039
11040 Configuration pragmas may either appear at the start of a compilation
11041 unit, in which case they apply only to that unit, or they may apply to
11042 all compilations performed in a given compilation environment.
11043
11044 GNAT also provides the @code{gnatchop} utility to provide an automatic
11045 way to handle configuration pragmas following the semantics for
11046 compilations (that is, files with multiple units), described in the RM.
11047 See @ref{Operating gnatchop in Compilation Mode} for details.
11048 However, for most purposes, it will be more convenient to edit the
11049 @file{gnat.adc} file that contains configuration pragmas directly,
11050 as described in the following section.
11051
11052 @node The Configuration Pragmas Files
11053 @section The Configuration Pragmas Files
11054 @cindex @file{gnat.adc}
11055
11056 @noindent
11057 In GNAT a compilation environment is defined by the current
11058 directory at the time that a compile command is given. This current
11059 directory is searched for a file whose name is @file{gnat.adc}. If
11060 this file is present, it is expected to contain one or more
11061 configuration pragmas that will be applied to the current compilation.
11062 However, if the switch @option{-gnatA} is used, @file{gnat.adc} is not
11063 considered.
11064
11065 Configuration pragmas may be entered into the @file{gnat.adc} file
11066 either by running @code{gnatchop} on a source file that consists only of
11067 configuration pragmas, or more conveniently by
11068 direct editing of the @file{gnat.adc} file, which is a standard format
11069 source file.
11070
11071 In addition to @file{gnat.adc}, additional files containing configuration
11072 pragmas may be applied to the current compilation using the switch
11073 @option{-gnatec}@var{path}. @var{path} must designate an existing file that
11074 contains only configuration pragmas. These configuration pragmas are
11075 in addition to those found in @file{gnat.adc} (provided @file{gnat.adc}
11076 is present and switch @option{-gnatA} is not used).
11077
11078 It is allowed to specify several switches @option{-gnatec}, all of which
11079 will be taken into account.
11080
11081 If you are using project file, a separate mechanism is provided using
11082 project attributes, see @ref{Specifying Configuration Pragmas} for more
11083 details.
11084
11085 @ifset vms
11086 Of special interest to GNAT OpenVMS Alpha is the following
11087 configuration pragma:
11088
11089 @smallexample @c ada
11090 @cartouche
11091 pragma Extend_System (Aux_DEC);
11092 @end cartouche
11093 @end smallexample
11094
11095 @noindent
11096 In the presence of this pragma, GNAT adds to the definition of the
11097 predefined package SYSTEM all the additional types and subprograms that are
11098 defined in HP Ada. See @ref{Compatibility with HP Ada} for details.
11099 @end ifset
11100
11101 @node Handling Arbitrary File Naming Conventions Using gnatname
11102 @chapter Handling Arbitrary File Naming Conventions Using @code{gnatname}
11103 @cindex Arbitrary File Naming Conventions
11104
11105 @menu
11106 * Arbitrary File Naming Conventions::
11107 * Running gnatname::
11108 * Switches for gnatname::
11109 * Examples of gnatname Usage::
11110 @end menu
11111
11112 @node Arbitrary File Naming Conventions
11113 @section Arbitrary File Naming Conventions
11114
11115 @noindent
11116 The GNAT compiler must be able to know the source file name of a compilation
11117 unit. When using the standard GNAT default file naming conventions
11118 (@code{.ads} for specs, @code{.adb} for bodies), the GNAT compiler
11119 does not need additional information.
11120
11121 @noindent
11122 When the source file names do not follow the standard GNAT default file naming
11123 conventions, the GNAT compiler must be given additional information through
11124 a configuration pragmas file (@pxref{Configuration Pragmas})
11125 or a project file.
11126 When the non-standard file naming conventions are well-defined,
11127 a small number of pragmas @code{Source_File_Name} specifying a naming pattern
11128 (@pxref{Alternative File Naming Schemes}) may be sufficient. However,
11129 if the file naming conventions are irregular or arbitrary, a number
11130 of pragma @code{Source_File_Name} for individual compilation units
11131 must be defined.
11132 To help maintain the correspondence between compilation unit names and
11133 source file names within the compiler,
11134 GNAT provides a tool @code{gnatname} to generate the required pragmas for a
11135 set of files.
11136
11137 @node Running gnatname
11138 @section Running @code{gnatname}
11139
11140 @noindent
11141 The usual form of the @code{gnatname} command is
11142
11143 @smallexample
11144 $ gnatname @ovar{switches} @var{naming_pattern} @ovar{naming_patterns}
11145 @r{[}--and @ovar{switches} @var{naming_pattern} @ovar{naming_patterns}@r{]}
11146 @end smallexample
11147
11148 @noindent
11149 All of the arguments are optional. If invoked without any argument,
11150 @code{gnatname} will display its usage.
11151
11152 @noindent
11153 When used with at least one naming pattern, @code{gnatname} will attempt to
11154 find all the compilation units in files that follow at least one of the
11155 naming patterns. To find these compilation units,
11156 @code{gnatname} will use the GNAT compiler in syntax-check-only mode on all
11157 regular files.
11158
11159 @noindent
11160 One or several Naming Patterns may be given as arguments to @code{gnatname}.
11161 Each Naming Pattern is enclosed between double quotes.
11162 A Naming Pattern is a regular expression similar to the wildcard patterns
11163 used in file names by the Unix shells or the DOS prompt.
11164
11165 @noindent
11166 @code{gnatname} may be called with several sections of directories/patterns.
11167 Sections are separated by switch @code{--and}. In each section, there must be
11168 at least one pattern. If no directory is specified in a section, the current
11169 directory (or the project directory is @code{-P} is used) is implied.
11170 The options other that the directory switches and the patterns apply globally
11171 even if they are in different sections.
11172
11173 @noindent
11174 Examples of Naming Patterns are
11175
11176 @smallexample
11177 "*.[12].ada"
11178 "*.ad[sb]*"
11179 "body_*" "spec_*"
11180 @end smallexample
11181
11182 @noindent
11183 For a more complete description of the syntax of Naming Patterns,
11184 see the second kind of regular expressions described in @file{g-regexp.ads}
11185 (the ``Glob'' regular expressions).
11186
11187 @noindent
11188 When invoked with no switch @code{-P}, @code{gnatname} will create a
11189 configuration pragmas file @file{gnat.adc} in the current working directory,
11190 with pragmas @code{Source_File_Name} for each file that contains a valid Ada
11191 unit.
11192
11193 @node Switches for gnatname
11194 @section Switches for @code{gnatname}
11195
11196 @noindent
11197 Switches for @code{gnatname} must precede any specified Naming Pattern.
11198
11199 @noindent
11200 You may specify any of the following switches to @code{gnatname}:
11201
11202 @table @option
11203 @c !sort!
11204
11205 @item --version
11206 @cindex @option{--version} @command{gnatname}
11207 Display Copyright and version, then exit disregarding all other options.
11208
11209 @item --help
11210 @cindex @option{--help} @command{gnatname}
11211 If @option{--version} was not used, display usage, then exit disregarding
11212 all other options.
11213
11214 @item --and
11215 Start another section of directories/patterns.
11216
11217 @item ^-c^/CONFIG_FILE=^@file{file}
11218 @cindex @option{^-c^/CONFIG_FILE^} (@code{gnatname})
11219 Create a configuration pragmas file @file{file} (instead of the default
11220 @file{gnat.adc}).
11221 @ifclear vms
11222 There may be zero, one or more space between @option{-c} and
11223 @file{file}.
11224 @end ifclear
11225 @file{file} may include directory information. @file{file} must be
11226 writable. There may be only one switch @option{^-c^/CONFIG_FILE^}.
11227 When a switch @option{^-c^/CONFIG_FILE^} is
11228 specified, no switch @option{^-P^/PROJECT_FILE^} may be specified (see below).
11229
11230 @item ^-d^/SOURCE_DIRS=^@file{dir}
11231 @cindex @option{^-d^/SOURCE_DIRS^} (@code{gnatname})
11232 Look for source files in directory @file{dir}. There may be zero, one or more
11233 spaces between @option{^-d^/SOURCE_DIRS=^} and @file{dir}.
11234 When a switch @option{^-d^/SOURCE_DIRS^}
11235 is specified, the current working directory will not be searched for source
11236 files, unless it is explicitly specified with a @option{^-d^/SOURCE_DIRS^}
11237 or @option{^-D^/DIR_FILES^} switch.
11238 Several switches @option{^-d^/SOURCE_DIRS^} may be specified.
11239 If @file{dir} is a relative path, it is relative to the directory of
11240 the configuration pragmas file specified with switch
11241 @option{^-c^/CONFIG_FILE^},
11242 or to the directory of the project file specified with switch
11243 @option{^-P^/PROJECT_FILE^} or,
11244 if neither switch @option{^-c^/CONFIG_FILE^}
11245 nor switch @option{^-P^/PROJECT_FILE^} are specified, it is relative to the
11246 current working directory. The directory
11247 specified with switch @option{^-d^/SOURCE_DIRS^} must exist and be readable.
11248
11249 @item ^-D^/DIRS_FILE=^@file{file}
11250 @cindex @option{^-D^/DIRS_FILE^} (@code{gnatname})
11251 Look for source files in all directories listed in text file @file{file}.
11252 There may be zero, one or more spaces between @option{^-D^/DIRS_FILE=^}
11253 and @file{file}.
11254 @file{file} must be an existing, readable text file.
11255 Each nonempty line in @file{file} must be a directory.
11256 Specifying switch @option{^-D^/DIRS_FILE^} is equivalent to specifying as many
11257 switches @option{^-d^/SOURCE_DIRS^} as there are nonempty lines in
11258 @file{file}.
11259
11260 @item ^-f^/FOREIGN_PATTERN=^@file{pattern}
11261 @cindex @option{^-f^/FOREIGN_PATTERN^} (@code{gnatname})
11262 Foreign patterns. Using this switch, it is possible to add sources of languages
11263 other than Ada to the list of sources of a project file.
11264 It is only useful if a ^-P^/PROJECT_FILE^ switch is used.
11265 For example,
11266 @smallexample
11267 gnatname ^-Pprj -f"*.c"^/PROJECT_FILE=PRJ /FOREIGN_PATTERN=*.C^ "*.ada"
11268 @end smallexample
11269 @noindent
11270 will look for Ada units in all files with the @file{.ada} extension,
11271 and will add to the list of file for project @file{prj.gpr} the C files
11272 with extension @file{.^c^C^}.
11273
11274 @item ^-h^/HELP^
11275 @cindex @option{^-h^/HELP^} (@code{gnatname})
11276 Output usage (help) information. The output is written to @file{stdout}.
11277
11278 @item ^-P^/PROJECT_FILE=^@file{proj}
11279 @cindex @option{^-P^/PROJECT_FILE^} (@code{gnatname})
11280 Create or update project file @file{proj}. There may be zero, one or more space
11281 between @option{-P} and @file{proj}. @file{proj} may include directory
11282 information. @file{proj} must be writable.
11283 There may be only one switch @option{^-P^/PROJECT_FILE^}.
11284 When a switch @option{^-P^/PROJECT_FILE^} is specified,
11285 no switch @option{^-c^/CONFIG_FILE^} may be specified.
11286
11287 @item ^-v^/VERBOSE^
11288 @cindex @option{^-v^/VERBOSE^} (@code{gnatname})
11289 Verbose mode. Output detailed explanation of behavior to @file{stdout}.
11290 This includes name of the file written, the name of the directories to search
11291 and, for each file in those directories whose name matches at least one of
11292 the Naming Patterns, an indication of whether the file contains a unit,
11293 and if so the name of the unit.
11294
11295 @item ^-v -v^/VERBOSE /VERBOSE^
11296 @cindex @option{^-v -v^/VERBOSE /VERBOSE^} (@code{gnatname})
11297 Very Verbose mode. In addition to the output produced in verbose mode,
11298 for each file in the searched directories whose name matches none of
11299 the Naming Patterns, an indication is given that there is no match.
11300
11301 @item ^-x^/EXCLUDED_PATTERN=^@file{pattern}
11302 @cindex @option{^-x^/EXCLUDED_PATTERN^} (@code{gnatname})
11303 Excluded patterns. Using this switch, it is possible to exclude some files
11304 that would match the name patterns. For example,
11305 @smallexample
11306 gnatname ^-x "*_nt.ada"^/EXCLUDED_PATTERN=*_nt.ada^ "*.ada"
11307 @end smallexample
11308 @noindent
11309 will look for Ada units in all files with the @file{.ada} extension,
11310 except those whose names end with @file{_nt.ada}.
11311
11312 @end table
11313
11314 @node Examples of gnatname Usage
11315 @section Examples of @code{gnatname} Usage
11316
11317 @ifset vms
11318 @smallexample
11319 $ gnatname /CONFIG_FILE=[HOME.ME]NAMES.ADC /SOURCE_DIRS=SOURCES "[a-z]*.ada*"
11320 @end smallexample
11321 @end ifset
11322
11323 @ifclear vms
11324 @smallexample
11325 $ gnatname -c /home/me/names.adc -d sources "[a-z]*.ada*"
11326 @end smallexample
11327 @end ifclear
11328
11329 @noindent
11330 In this example, the directory @file{^/home/me^[HOME.ME]^} must already exist
11331 and be writable. In addition, the directory
11332 @file{^/home/me/sources^[HOME.ME.SOURCES]^} (specified by
11333 @option{^-d sources^/SOURCE_DIRS=SOURCES^}) must exist and be readable.
11334
11335 @ifclear vms
11336 Note the optional spaces after @option{-c} and @option{-d}.
11337 @end ifclear
11338
11339 @smallexample
11340 @ifclear vms
11341 $ gnatname -P/home/me/proj -x "*_nt_body.ada"
11342 -dsources -dsources/plus -Dcommon_dirs.txt "body_*" "spec_*"
11343 @end ifclear
11344 @ifset vms
11345 $ gnatname /PROJECT_FILE=[HOME.ME]PROJ
11346 /EXCLUDED_PATTERN=*_nt_body.ada
11347 /SOURCE_DIRS=(SOURCES,[SOURCES.PLUS])
11348 /DIRS_FILE=COMMON_DIRS.TXT "body_*" "spec_*"
11349 @end ifset
11350 @end smallexample
11351
11352 Note that several switches @option{^-d^/SOURCE_DIRS^} may be used,
11353 even in conjunction with one or several switches
11354 @option{^-D^/DIRS_FILE^}. Several Naming Patterns and one excluded pattern
11355 are used in this example.
11356
11357 @c *****************************************
11358 @c * G N A T P r o j e c t M a n a g e r *
11359 @c *****************************************
11360 @node GNAT Project Manager
11361 @chapter GNAT Project Manager
11362
11363 @menu
11364 * Introduction::
11365 * Examples of Project Files::
11366 * Project File Syntax::
11367 * Objects and Sources in Project Files::
11368 * Importing Projects::
11369 * Project Extension::
11370 * Project Hierarchy Extension::
11371 * External References in Project Files::
11372 * Packages in Project Files::
11373 * Variables from Imported Projects::
11374 * Naming Schemes::
11375 * Library Projects::
11376 * Stand-alone Library Projects::
11377 * Switches Related to Project Files::
11378 * Tools Supporting Project Files::
11379 * An Extended Example::
11380 * Project File Complete Syntax::
11381 @end menu
11382
11383 @c ****************
11384 @c * Introduction *
11385 @c ****************
11386
11387 @node Introduction
11388 @section Introduction
11389
11390 @noindent
11391 This chapter describes GNAT's @emph{Project Manager}, a facility that allows
11392 you to manage complex builds involving a number of source files, directories,
11393 and compilation options for different system configurations. In particular,
11394 project files allow you to specify:
11395 @itemize @bullet
11396 @item
11397 The directory or set of directories containing the source files, and/or the
11398 names of the specific source files themselves
11399 @item
11400 The directory in which the compiler's output
11401 (@file{ALI} files, object files, tree files) is to be placed
11402 @item
11403 The directory in which the executable programs is to be placed
11404 @item
11405 ^Switch^Switch^ settings for any of the project-enabled tools
11406 (@command{gnatmake}, compiler, binder, linker, @code{gnatls}, @code{gnatxref},
11407 @code{gnatfind}); you can apply these settings either globally or to individual
11408 compilation units.
11409 @item
11410 The source files containing the main subprogram(s) to be built
11411 @item
11412 The source programming language(s) (currently Ada and/or C)
11413 @item
11414 Source file naming conventions; you can specify these either globally or for
11415 individual compilation units
11416 @end itemize
11417
11418 @menu
11419 * Project Files::
11420 @end menu
11421
11422 @node Project Files
11423 @subsection Project Files
11424
11425 @noindent
11426 Project files are written in a syntax close to that of Ada, using familiar
11427 notions such as packages, context clauses, declarations, default values,
11428 assignments, and inheritance. Finally, project files can be built
11429 hierarchically from other project files, simplifying complex system
11430 integration and project reuse.
11431
11432 A @dfn{project} is a specific set of values for various compilation properties.
11433 The settings for a given project are described by means of
11434 a @dfn{project file}, which is a text file written in an Ada-like syntax.
11435 Property values in project files are either strings or lists of strings.
11436 Properties that are not explicitly set receive default values. A project
11437 file may interrogate the values of @dfn{external variables} (user-defined
11438 command-line switches or environment variables), and it may specify property
11439 settings conditionally, based on the value of such variables.
11440
11441 In simple cases, a project's source files depend only on other source files
11442 in the same project, or on the predefined libraries. (@emph{Dependence} is
11443 used in
11444 the Ada technical sense; as in one Ada unit @code{with}ing another.) However,
11445 the Project Manager also allows more sophisticated arrangements,
11446 where the source files in one project depend on source files in other
11447 projects:
11448 @itemize @bullet
11449 @item
11450 One project can @emph{import} other projects containing needed source files.
11451 @item
11452 You can organize GNAT projects in a hierarchy: a @emph{child} project
11453 can extend a @emph{parent} project, inheriting the parent's source files and
11454 optionally overriding any of them with alternative versions
11455 @end itemize
11456
11457 @noindent
11458 More generally, the Project Manager lets you structure large development
11459 efforts into hierarchical subsystems, where build decisions are delegated
11460 to the subsystem level, and thus different compilation environments
11461 (^switch^switch^ settings) used for different subsystems.
11462
11463 The Project Manager is invoked through the
11464 @option{^-P^/PROJECT_FILE=^@emph{projectfile}}
11465 switch to @command{gnatmake} or to the @command{^gnat^GNAT^} front driver.
11466 @ifclear vms
11467 There may be zero, one or more spaces between @option{-P} and
11468 @option{@emph{projectfile}}.
11469 @end ifclear
11470 If you want to define (on the command line) an external variable that is
11471 queried by the project file, you must use the
11472 @option{^-X^/EXTERNAL_REFERENCE=^@emph{vbl}=@emph{value}} switch.
11473 The Project Manager parses and interprets the project file, and drives the
11474 invoked tool based on the project settings.
11475
11476 The Project Manager supports a wide range of development strategies,
11477 for systems of all sizes. Here are some typical practices that are
11478 easily handled:
11479 @itemize @bullet
11480 @item
11481 Using a common set of source files, but generating object files in different
11482 directories via different ^switch^switch^ settings
11483 @item
11484 Using a mostly-shared set of source files, but with different versions of
11485 some unit or units
11486 @end itemize
11487
11488 @noindent
11489 The destination of an executable can be controlled inside a project file
11490 using the @option{^-o^-o^}
11491 ^switch^switch^.
11492 In the absence of such a ^switch^switch^ either inside
11493 the project file or on the command line, any executable files generated by
11494 @command{gnatmake} are placed in the directory @code{Exec_Dir} specified
11495 in the project file. If no @code{Exec_Dir} is specified, they will be placed
11496 in the object directory of the project.
11497
11498 You can use project files to achieve some of the effects of a source
11499 versioning system (for example, defining separate projects for
11500 the different sets of sources that comprise different releases) but the
11501 Project Manager is independent of any source configuration management tools
11502 that might be used by the developers.
11503
11504 The next section introduces the main features of GNAT's project facility
11505 through a sequence of examples; subsequent sections will present the syntax
11506 and semantics in more detail. A more formal description of the project
11507 facility appears in @ref{Project File Reference,,, gnat_rm, GNAT
11508 Reference Manual}.
11509
11510 @c *****************************
11511 @c * Examples of Project Files *
11512 @c *****************************
11513
11514 @node Examples of Project Files
11515 @section Examples of Project Files
11516 @noindent
11517 This section illustrates some of the typical uses of project files and
11518 explains their basic structure and behavior.
11519
11520 @menu
11521 * Common Sources with Different ^Switches^Switches^ and Directories::
11522 * Using External Variables::
11523 * Importing Other Projects::
11524 * Extending a Project::
11525 @end menu
11526
11527 @node Common Sources with Different ^Switches^Switches^ and Directories
11528 @subsection Common Sources with Different ^Switches^Switches^ and Directories
11529
11530 @menu
11531 * Source Files::
11532 * Specifying the Object Directory::
11533 * Specifying the Exec Directory::
11534 * Project File Packages::
11535 * Specifying ^Switch^Switch^ Settings::
11536 * Main Subprograms::
11537 * Executable File Names::
11538 * Source File Naming Conventions::
11539 * Source Language(s)::
11540 @end menu
11541
11542 @noindent
11543 Suppose that the Ada source files @file{pack.ads}, @file{pack.adb}, and
11544 @file{proc.adb} are in the @file{/common} directory. The file
11545 @file{proc.adb} contains an Ada main subprogram @code{Proc} that @code{with}s
11546 package @code{Pack}. We want to compile these source files under two sets
11547 of ^switches^switches^:
11548 @itemize @bullet
11549 @item
11550 When debugging, we want to pass the @option{-g} switch to @command{gnatmake},
11551 and the @option{^-gnata^-gnata^},
11552 @option{^-gnato^-gnato^},
11553 and @option{^-gnatE^-gnatE^} switches to the
11554 compiler; the compiler's output is to appear in @file{/common/debug}
11555 @item
11556 When preparing a release version, we want to pass the @option{^-O2^O2^} switch
11557 to the compiler; the compiler's output is to appear in @file{/common/release}
11558 @end itemize
11559
11560 @noindent
11561 The GNAT project files shown below, respectively @file{debug.gpr} and
11562 @file{release.gpr} in the @file{/common} directory, achieve these effects.
11563
11564 Schematically:
11565 @smallexample
11566 @group
11567 ^/common^[COMMON]^
11568 debug.gpr
11569 release.gpr
11570 pack.ads
11571 pack.adb
11572 proc.adb
11573 @end group
11574 @group
11575 ^/common/debug^[COMMON.DEBUG]^
11576 proc.ali, proc.o
11577 pack.ali, pack.o
11578 @end group
11579 @group
11580 ^/common/release^[COMMON.RELEASE]^
11581 proc.ali, proc.o
11582 pack.ali, pack.o
11583 @end group
11584 @end smallexample
11585 Here are the corresponding project files:
11586
11587 @smallexample @c projectfile
11588 @group
11589 project Debug is
11590 for Object_Dir use "debug";
11591 for Main use ("proc");
11592
11593 package Builder is
11594 for ^Default_Switches^Default_Switches^ ("Ada")
11595 use ("^-g^-g^");
11596 for Executable ("proc.adb") use "proc1";
11597 end Builder;
11598 @end group
11599
11600 @group
11601 package Compiler is
11602 for ^Default_Switches^Default_Switches^ ("Ada")
11603 use ("-fstack-check",
11604 "^-gnata^-gnata^",
11605 "^-gnato^-gnato^",
11606 "^-gnatE^-gnatE^");
11607 end Compiler;
11608 end Debug;
11609 @end group
11610 @end smallexample
11611
11612 @smallexample @c projectfile
11613 @group
11614 project Release is
11615 for Object_Dir use "release";
11616 for Exec_Dir use ".";
11617 for Main use ("proc");
11618
11619 package Compiler is
11620 for ^Default_Switches^Default_Switches^ ("Ada")
11621 use ("^-O2^-O2^");
11622 end Compiler;
11623 end Release;
11624 @end group
11625 @end smallexample
11626
11627 @noindent
11628 The name of the project defined by @file{debug.gpr} is @code{"Debug"} (case
11629 insensitive), and analogously the project defined by @file{release.gpr} is
11630 @code{"Release"}. For consistency the file should have the same name as the
11631 project, and the project file's extension should be @code{"gpr"}. These
11632 conventions are not required, but a warning is issued if they are not followed.
11633
11634 If the current directory is @file{^/temp^[TEMP]^}, then the command
11635 @smallexample
11636 gnatmake ^-P/common/debug.gpr^/PROJECT_FILE=[COMMON]DEBUG^
11637 @end smallexample
11638
11639 @noindent
11640 generates object and ALI files in @file{^/common/debug^[COMMON.DEBUG]^},
11641 as well as the @code{^proc1^PROC1.EXE^} executable,
11642 using the ^switch^switch^ settings defined in the project file.
11643
11644 Likewise, the command
11645 @smallexample
11646 gnatmake ^-P/common/release.gpr^/PROJECT_FILE=[COMMON]RELEASE^
11647 @end smallexample
11648
11649 @noindent
11650 generates object and ALI files in @file{^/common/release^[COMMON.RELEASE]^},
11651 and the @code{^proc^PROC.EXE^}
11652 executable in @file{^/common^[COMMON]^},
11653 using the ^switch^switch^ settings from the project file.
11654
11655 @node Source Files
11656 @unnumberedsubsubsec Source Files
11657
11658 @noindent
11659 If a project file does not explicitly specify a set of source directories or
11660 a set of source files, then by default the project's source files are the
11661 Ada source files in the project file directory. Thus @file{pack.ads},
11662 @file{pack.adb}, and @file{proc.adb} are the source files for both projects.
11663
11664 @node Specifying the Object Directory
11665 @unnumberedsubsubsec Specifying the Object Directory
11666
11667 @noindent
11668 Several project properties are modeled by Ada-style @emph{attributes};
11669 a property is defined by supplying the equivalent of an Ada attribute
11670 definition clause in the project file.
11671 A project's object directory is another such a property; the corresponding
11672 attribute is @code{Object_Dir}, and its value is also a string expression,
11673 specified either as absolute or relative. In the later case,
11674 it is relative to the project file directory. Thus the compiler's
11675 output is directed to @file{^/common/debug^[COMMON.DEBUG]^}
11676 (for the @code{Debug} project)
11677 and to @file{^/common/release^[COMMON.RELEASE]^}
11678 (for the @code{Release} project).
11679 If @code{Object_Dir} is not specified, then the default is the project file
11680 directory itself.
11681
11682 @node Specifying the Exec Directory
11683 @unnumberedsubsubsec Specifying the Exec Directory
11684
11685 @noindent
11686 A project's exec directory is another property; the corresponding
11687 attribute is @code{Exec_Dir}, and its value is also a string expression,
11688 either specified as relative or absolute. If @code{Exec_Dir} is not specified,
11689 then the default is the object directory (which may also be the project file
11690 directory if attribute @code{Object_Dir} is not specified). Thus the executable
11691 is placed in @file{^/common/debug^[COMMON.DEBUG]^}
11692 for the @code{Debug} project (attribute @code{Exec_Dir} not specified)
11693 and in @file{^/common^[COMMON]^} for the @code{Release} project.
11694
11695 @node Project File Packages
11696 @unnumberedsubsubsec Project File Packages
11697
11698 @noindent
11699 A GNAT tool that is integrated with the Project Manager is modeled by a
11700 corresponding package in the project file. In the example above,
11701 The @code{Debug} project defines the packages @code{Builder}
11702 (for @command{gnatmake}) and @code{Compiler};
11703 the @code{Release} project defines only the @code{Compiler} package.
11704
11705 The Ada-like package syntax is not to be taken literally. Although packages in
11706 project files bear a surface resemblance to packages in Ada source code, the
11707 notation is simply a way to convey a grouping of properties for a named
11708 entity. Indeed, the package names permitted in project files are restricted
11709 to a predefined set, corresponding to the project-aware tools, and the contents
11710 of packages are limited to a small set of constructs.
11711 The packages in the example above contain attribute definitions.
11712
11713 @node Specifying ^Switch^Switch^ Settings
11714 @unnumberedsubsubsec Specifying ^Switch^Switch^ Settings
11715
11716 @noindent
11717 ^Switch^Switch^ settings for a project-aware tool can be specified through
11718 attributes in the package that corresponds to the tool.
11719 The example above illustrates one of the relevant attributes,
11720 @code{^Default_Switches^Default_Switches^}, which is defined in packages
11721 in both project files.
11722 Unlike simple attributes like @code{Source_Dirs},
11723 @code{^Default_Switches^Default_Switches^} is
11724 known as an @emph{associative array}. When you define this attribute, you must
11725 supply an ``index'' (a literal string), and the effect of the attribute
11726 definition is to set the value of the array at the specified index.
11727 For the @code{^Default_Switches^Default_Switches^} attribute,
11728 the index is a programming language (in our case, Ada),
11729 and the value specified (after @code{use}) must be a list
11730 of string expressions.
11731
11732 The attributes permitted in project files are restricted to a predefined set.
11733 Some may appear at project level, others in packages.
11734 For any attribute that is an associative array, the index must always be a
11735 literal string, but the restrictions on this string (e.g., a file name or a
11736 language name) depend on the individual attribute.
11737 Also depending on the attribute, its specified value will need to be either a
11738 string or a string list.
11739
11740 In the @code{Debug} project, we set the switches for two tools,
11741 @command{gnatmake} and the compiler, and thus we include the two corresponding
11742 packages; each package defines the @code{^Default_Switches^Default_Switches^}
11743 attribute with index @code{"Ada"}.
11744 Note that the package corresponding to
11745 @command{gnatmake} is named @code{Builder}. The @code{Release} project is
11746 similar, but only includes the @code{Compiler} package.
11747
11748 In project @code{Debug} above, the ^switches^switches^ starting with
11749 @option{-gnat} that are specified in package @code{Compiler}
11750 could have been placed in package @code{Builder}, since @command{gnatmake}
11751 transmits all such ^switches^switches^ to the compiler.
11752
11753 @node Main Subprograms
11754 @unnumberedsubsubsec Main Subprograms
11755
11756 @noindent
11757 One of the specifiable properties of a project is a list of files that contain
11758 main subprograms. This property is captured in the @code{Main} attribute,
11759 whose value is a list of strings. If a project defines the @code{Main}
11760 attribute, it is not necessary to identify the main subprogram(s) when
11761 invoking @command{gnatmake} (@pxref{gnatmake and Project Files}).
11762
11763 @node Executable File Names
11764 @unnumberedsubsubsec Executable File Names
11765
11766 @noindent
11767 By default, the executable file name corresponding to a main source is
11768 deduced from the main source file name. Through the attributes
11769 @code{Executable} and @code{Executable_Suffix} of package @code{Builder},
11770 it is possible to change this default.
11771 In project @code{Debug} above, the executable file name
11772 for main source @file{^proc.adb^PROC.ADB^} is
11773 @file{^proc1^PROC1.EXE^}.
11774 Attribute @code{Executable_Suffix}, when specified, may change the suffix
11775 of the executable files, when no attribute @code{Executable} applies:
11776 its value replace the platform-specific executable suffix.
11777 Attributes @code{Executable} and @code{Executable_Suffix} are the only ways to
11778 specify a non-default executable file name when several mains are built at once
11779 in a single @command{gnatmake} command.
11780
11781 @node Source File Naming Conventions
11782 @unnumberedsubsubsec Source File Naming Conventions
11783
11784 @noindent
11785 Since the project files above do not specify any source file naming
11786 conventions, the GNAT defaults are used. The mechanism for defining source
11787 file naming conventions -- a package named @code{Naming} --
11788 is described below (@pxref{Naming Schemes}).
11789
11790 @node Source Language(s)
11791 @unnumberedsubsubsec Source Language(s)
11792
11793 @noindent
11794 Since the project files do not specify a @code{Languages} attribute, by
11795 default the GNAT tools assume that the language of the project file is Ada.
11796 More generally, a project can comprise source files
11797 in Ada, C, and/or other languages.
11798
11799 @node Using External Variables
11800 @subsection Using External Variables
11801
11802 @noindent
11803 Instead of supplying different project files for debug and release, we can
11804 define a single project file that queries an external variable (set either
11805 on the command line or via an ^environment variable^logical name^) in order to
11806 conditionally define the appropriate settings. Again, assume that the
11807 source files @file{pack.ads}, @file{pack.adb}, and @file{proc.adb} are
11808 located in directory @file{^/common^[COMMON]^}. The following project file,
11809 @file{build.gpr}, queries the external variable named @code{STYLE} and
11810 defines an object directory and ^switch^switch^ settings based on whether
11811 the value is @code{"deb"} (debug) or @code{"rel"} (release), and where
11812 the default is @code{"deb"}.
11813
11814 @smallexample @c projectfile
11815 @group
11816 project Build is
11817 for Main use ("proc");
11818
11819 type Style_Type is ("deb", "rel");
11820 Style : Style_Type := external ("STYLE", "deb");
11821
11822 case Style is
11823 when "deb" =>
11824 for Object_Dir use "debug";
11825
11826 when "rel" =>
11827 for Object_Dir use "release";
11828 for Exec_Dir use ".";
11829 end case;
11830 @end group
11831
11832 @group
11833 package Builder is
11834
11835 case Style is
11836 when "deb" =>
11837 for ^Default_Switches^Default_Switches^ ("Ada")
11838 use ("^-g^-g^");
11839 for Executable ("proc") use "proc1";
11840 when others =>
11841 null;
11842 end case;
11843
11844 end Builder;
11845 @end group
11846
11847 @group
11848 package Compiler is
11849
11850 case Style is
11851 when "deb" =>
11852 for ^Default_Switches^Default_Switches^ ("Ada")
11853 use ("^-gnata^-gnata^",
11854 "^-gnato^-gnato^",
11855 "^-gnatE^-gnatE^");
11856
11857 when "rel" =>
11858 for ^Default_Switches^Default_Switches^ ("Ada")
11859 use ("^-O2^-O2^");
11860 end case;
11861
11862 end Compiler;
11863
11864 end Build;
11865 @end group
11866 @end smallexample
11867
11868 @noindent
11869 @code{Style_Type} is an example of a @emph{string type}, which is the project
11870 file analog of an Ada enumeration type but whose components are string literals
11871 rather than identifiers. @code{Style} is declared as a variable of this type.
11872
11873 The form @code{external("STYLE", "deb")} is known as an
11874 @emph{external reference}; its first argument is the name of an
11875 @emph{external variable}, and the second argument is a default value to be
11876 used if the external variable doesn't exist. You can define an external
11877 variable on the command line via the @option{^-X^/EXTERNAL_REFERENCE^} switch,
11878 or you can use ^an environment variable^a logical name^
11879 as an external variable.
11880
11881 Each @code{case} construct is expanded by the Project Manager based on the
11882 value of @code{Style}. Thus the command
11883 @ifclear vms
11884 @smallexample
11885 gnatmake -P/common/build.gpr -XSTYLE=deb
11886 @end smallexample
11887 @end ifclear
11888
11889 @ifset vms
11890 @smallexample
11891 gnatmake /PROJECT_FILE=[COMMON]BUILD.GPR /EXTERNAL_REFERENCE=STYLE=deb
11892 @end smallexample
11893 @end ifset
11894
11895 @noindent
11896 is equivalent to the @command{gnatmake} invocation using the project file
11897 @file{debug.gpr} in the earlier example. So is the command
11898 @smallexample
11899 gnatmake ^-P/common/build.gpr^/PROJECT_FILE=[COMMON]BUILD.GPR^
11900 @end smallexample
11901
11902 @noindent
11903 since @code{"deb"} is the default for @code{STYLE}.
11904
11905 Analogously,
11906
11907 @ifclear vms
11908 @smallexample
11909 gnatmake -P/common/build.gpr -XSTYLE=rel
11910 @end smallexample
11911 @end ifclear
11912
11913 @ifset vms
11914 @smallexample
11915 GNAT MAKE /PROJECT_FILE=[COMMON]BUILD.GPR /EXTERNAL_REFERENCE=STYLE=rel
11916 @end smallexample
11917 @end ifset
11918
11919 @noindent
11920 is equivalent to the @command{gnatmake} invocation using the project file
11921 @file{release.gpr} in the earlier example.
11922
11923 @node Importing Other Projects
11924 @subsection Importing Other Projects
11925 @cindex @code{ADA_PROJECT_PATH}
11926
11927 @noindent
11928 A compilation unit in a source file in one project may depend on compilation
11929 units in source files in other projects. To compile this unit under
11930 control of a project file, the
11931 dependent project must @emph{import} the projects containing the needed source
11932 files.
11933 This effect is obtained using syntax similar to an Ada @code{with} clause,
11934 but where @code{with}ed entities are strings that denote project files.
11935
11936 As an example, suppose that the two projects @code{GUI_Proj} and
11937 @code{Comm_Proj} are defined in the project files @file{gui_proj.gpr} and
11938 @file{comm_proj.gpr} in directories @file{^/gui^[GUI]^}
11939 and @file{^/comm^[COMM]^}, respectively.
11940 Suppose that the source files for @code{GUI_Proj} are
11941 @file{gui.ads} and @file{gui.adb}, and that the source files for
11942 @code{Comm_Proj} are @file{comm.ads} and @file{comm.adb}, where each set of
11943 files is located in its respective project file directory. Schematically:
11944
11945 @smallexample
11946 @group
11947 ^/gui^[GUI]^
11948 gui_proj.gpr
11949 gui.ads
11950 gui.adb
11951 @end group
11952
11953 @group
11954 ^/comm^[COMM]^
11955 comm_proj.gpr
11956 comm.ads
11957 comm.adb
11958 @end group
11959 @end smallexample
11960
11961 @noindent
11962 We want to develop an application in directory @file{^/app^[APP]^} that
11963 @code{with} the packages @code{GUI} and @code{Comm}, using the properties of
11964 the corresponding project files (e.g.@: the ^switch^switch^ settings
11965 and object directory).
11966 Skeletal code for a main procedure might be something like the following:
11967
11968 @smallexample @c ada
11969 @group
11970 with GUI, Comm;
11971 procedure App_Main is
11972 @dots{}
11973 begin
11974 @dots{}
11975 end App_Main;
11976 @end group
11977 @end smallexample
11978
11979 @noindent
11980 Here is a project file, @file{app_proj.gpr}, that achieves the desired
11981 effect:
11982
11983 @smallexample @c projectfile
11984 @group
11985 with "/gui/gui_proj", "/comm/comm_proj";
11986 project App_Proj is
11987 for Main use ("app_main");
11988 end App_Proj;
11989 @end group
11990 @end smallexample
11991
11992 @noindent
11993 Building an executable is achieved through the command:
11994 @smallexample
11995 gnatmake ^-P/app/app_proj^/PROJECT_FILE=[APP]APP_PROJ^
11996 @end smallexample
11997 @noindent
11998 which will generate the @code{^app_main^APP_MAIN.EXE^} executable
11999 in the directory where @file{app_proj.gpr} resides.
12000
12001 If an imported project file uses the standard extension (@code{^gpr^GPR^}) then
12002 (as illustrated above) the @code{with} clause can omit the extension.
12003
12004 Our example specified an absolute path for each imported project file.
12005 Alternatively, the directory name of an imported object can be omitted
12006 if either
12007 @itemize @bullet
12008 @item
12009 The imported project file is in the same directory as the importing project
12010 file, or
12011 @item
12012 You have defined ^an environment variable^a logical name^
12013 that includes the directory containing
12014 the needed project file. The syntax of @code{ADA_PROJECT_PATH} is the same as
12015 the syntax of @code{ADA_INCLUDE_PATH} and @code{ADA_OBJECTS_PATH}: a list of
12016 directory names separated by colons (semicolons on Windows).
12017 @end itemize
12018
12019 @noindent
12020 Thus, if we define @code{ADA_PROJECT_PATH} to include @file{^/gui^[GUI]^} and
12021 @file{^/comm^[COMM]^}, then our project file @file{app_proj.gpr} can be written
12022 as follows:
12023
12024 @smallexample @c projectfile
12025 @group
12026 with "gui_proj", "comm_proj";
12027 project App_Proj is
12028 for Main use ("app_main");
12029 end App_Proj;
12030 @end group
12031 @end smallexample
12032
12033 @noindent
12034 Importing other projects can create ambiguities.
12035 For example, the same unit might be present in different imported projects, or
12036 it might be present in both the importing project and in an imported project.
12037 Both of these conditions are errors. Note that in the current version of
12038 the Project Manager, it is illegal to have an ambiguous unit even if the
12039 unit is never referenced by the importing project. This restriction may be
12040 relaxed in a future release.
12041
12042 @node Extending a Project
12043 @subsection Extending a Project
12044
12045 @noindent
12046 In large software systems it is common to have multiple
12047 implementations of a common interface; in Ada terms, multiple versions of a
12048 package body for the same spec. For example, one implementation
12049 might be safe for use in tasking programs, while another might only be used
12050 in sequential applications. This can be modeled in GNAT using the concept
12051 of @emph{project extension}. If one project (the ``child'') @emph{extends}
12052 another project (the ``parent'') then by default all source files of the
12053 parent project are inherited by the child, but the child project can
12054 override any of the parent's source files with new versions, and can also
12055 add new files. This facility is the project analog of a type extension in
12056 Object-Oriented Programming. Project hierarchies are permitted (a child
12057 project may be the parent of yet another project), and a project that
12058 inherits one project can also import other projects.
12059
12060 As an example, suppose that directory @file{^/seq^[SEQ]^} contains the project
12061 file @file{seq_proj.gpr} as well as the source files @file{pack.ads},
12062 @file{pack.adb}, and @file{proc.adb}:
12063
12064 @smallexample
12065 @group
12066 ^/seq^[SEQ]^
12067 pack.ads
12068 pack.adb
12069 proc.adb
12070 seq_proj.gpr
12071 @end group
12072 @end smallexample
12073
12074 @noindent
12075 Note that the project file can simply be empty (that is, no attribute or
12076 package is defined):
12077
12078 @smallexample @c projectfile
12079 @group
12080 project Seq_Proj is
12081 end Seq_Proj;
12082 @end group
12083 @end smallexample
12084
12085 @noindent
12086 implying that its source files are all the Ada source files in the project
12087 directory.
12088
12089 Suppose we want to supply an alternate version of @file{pack.adb}, in
12090 directory @file{^/tasking^[TASKING]^}, but use the existing versions of
12091 @file{pack.ads} and @file{proc.adb}. We can define a project
12092 @code{Tasking_Proj} that inherits @code{Seq_Proj}:
12093
12094 @smallexample
12095 @group
12096 ^/tasking^[TASKING]^
12097 pack.adb
12098 tasking_proj.gpr
12099 @end group
12100
12101 @group
12102 project Tasking_Proj extends "/seq/seq_proj" is
12103 end Tasking_Proj;
12104 @end group
12105 @end smallexample
12106
12107 @noindent
12108 The version of @file{pack.adb} used in a build depends on which project file
12109 is specified.
12110
12111 Note that we could have obtained the desired behavior using project import
12112 rather than project inheritance; a @code{base} project would contain the
12113 sources for @file{pack.ads} and @file{proc.adb}, a sequential project would
12114 import @code{base} and add @file{pack.adb}, and likewise a tasking project
12115 would import @code{base} and add a different version of @file{pack.adb}. The
12116 choice depends on whether other sources in the original project need to be
12117 overridden. If they do, then project extension is necessary, otherwise,
12118 importing is sufficient.
12119
12120 @noindent
12121 In a project file that extends another project file, it is possible to
12122 indicate that an inherited source is not part of the sources of the extending
12123 project. This is necessary sometimes when a package spec has been overloaded
12124 and no longer requires a body: in this case, it is necessary to indicate that
12125 the inherited body is not part of the sources of the project, otherwise there
12126 will be a compilation error when compiling the spec.
12127
12128 For that purpose, the attribute @code{Excluded_Source_Files} is used.
12129 Its value is a string list: a list of file names. It is also possible to use
12130 attribute @code{Excluded_Source_List_File}. Its value is a single string:
12131 the file name of a text file containing a list of file names, one per line.
12132
12133 @smallexample @c @projectfile
12134 project B extends "a" is
12135 for Source_Files use ("pkg.ads");
12136 -- New spec of Pkg does not need a completion
12137 for Excluded_Source_Files use ("pkg.adb");
12138 end B;
12139 @end smallexample
12140
12141 Attribute @code{Excluded_Source_Files} may also be used to check if a source
12142 is still needed: if it is possible to build using @command{gnatmake} when such
12143 a source is put in attribute @code{Excluded_Source_Files} of a project P, then
12144 it is possible to remove the source completely from a system that includes
12145 project P.
12146
12147 @c ***********************
12148 @c * Project File Syntax *
12149 @c ***********************
12150
12151 @node Project File Syntax
12152 @section Project File Syntax
12153
12154 @menu
12155 * Basic Syntax::
12156 * Qualified Projects::
12157 * Packages::
12158 * Expressions::
12159 * String Types::
12160 * Variables::
12161 * Attributes::
12162 * Associative Array Attributes::
12163 * case Constructions::
12164 @end menu
12165
12166 @noindent
12167 This section describes the structure of project files.
12168
12169 A project may be an @emph{independent project}, entirely defined by a single
12170 project file. Any Ada source file in an independent project depends only
12171 on the predefined library and other Ada source files in the same project.
12172
12173 @noindent
12174 A project may also @dfn{depend on} other projects, in either or both of
12175 the following ways:
12176 @itemize @bullet
12177 @item It may import any number of projects
12178 @item It may extend at most one other project
12179 @end itemize
12180
12181 @noindent
12182 The dependence relation is a directed acyclic graph (the subgraph reflecting
12183 the ``extends'' relation is a tree).
12184
12185 A project's @dfn{immediate sources} are the source files directly defined by
12186 that project, either implicitly by residing in the project file's directory,
12187 or explicitly through any of the source-related attributes described below.
12188 More generally, a project @var{proj}'s @dfn{sources} are the immediate sources
12189 of @var{proj} together with the immediate sources (unless overridden) of any
12190 project on which @var{proj} depends (either directly or indirectly).
12191
12192 @node Basic Syntax
12193 @subsection Basic Syntax
12194
12195 @noindent
12196 As seen in the earlier examples, project files have an Ada-like syntax.
12197 The minimal project file is:
12198 @smallexample @c projectfile
12199 @group
12200 project Empty is
12201
12202 end Empty;
12203 @end group
12204 @end smallexample
12205
12206 @noindent
12207 The identifier @code{Empty} is the name of the project.
12208 This project name must be present after the reserved
12209 word @code{end} at the end of the project file, followed by a semi-colon.
12210
12211 Any name in a project file, such as the project name or a variable name,
12212 has the same syntax as an Ada identifier.
12213
12214 The reserved words of project files are the Ada 95 reserved words plus
12215 @code{extends}, @code{external}, and @code{project}. Note that the only Ada
12216 reserved words currently used in project file syntax are:
12217
12218 @itemize @bullet
12219 @item
12220 @code{all}
12221 @item
12222 @code{at}
12223 @item
12224 @code{case}
12225 @item
12226 @code{end}
12227 @item
12228 @code{for}
12229 @item
12230 @code{is}
12231 @item
12232 @code{limited}
12233 @item
12234 @code{null}
12235 @item
12236 @code{others}
12237 @item
12238 @code{package}
12239 @item
12240 @code{renames}
12241 @item
12242 @code{type}
12243 @item
12244 @code{use}
12245 @item
12246 @code{when}
12247 @item
12248 @code{with}
12249 @end itemize
12250
12251 @noindent
12252 Comments in project files have the same syntax as in Ada, two consecutive
12253 hyphens through the end of the line.
12254
12255 @node Qualified Projects
12256 @subsection Qualified Projects
12257
12258 @noindent
12259 Before the reserved @code{project}, there may be one or two "qualifiers", that
12260 is identifiers or other reserved words, to qualify the project.
12261
12262 The current list of qualifiers is:
12263
12264 @itemize @bullet
12265 @item
12266 @code{abstract}: qualify a project with no sources. An abstract project must
12267 have a declaration specifying that there are no sources in the project, and,
12268 if it extends another project, the project it extends must also be a qualified
12269 abstract project.
12270
12271 @item
12272 @code{standard}: a standard project is a non library project with sources.
12273
12274 @item
12275 @code{aggregate}: for future extension
12276
12277 @item
12278 @code{aggregate library}: for future extension
12279
12280 @item
12281 @code{library}: a library project must declare both attributes
12282 @code{Library_Name} and @code{Library_Dir}.
12283
12284 @item
12285 @code{configuration}: a configuration project cannot be in a project tree.
12286 @end itemize
12287
12288 @node Packages
12289 @subsection Packages
12290
12291 @noindent
12292 A project file may contain @emph{packages}. The name of a package must be one
12293 of the identifiers from the following list. A package
12294 with a given name may only appear once in a project file. Package names are
12295 case insensitive. The following package names are legal:
12296
12297 @itemize @bullet
12298 @item
12299 @code{Naming}
12300 @item
12301 @code{Builder}
12302 @item
12303 @code{Compiler}
12304 @item
12305 @code{Binder}
12306 @item
12307 @code{Linker}
12308 @item
12309 @code{Finder}
12310 @item
12311 @code{Cross_Reference}
12312 @item
12313 @code{Eliminate}
12314 @item
12315 @code{Pretty_Printer}
12316 @item
12317 @code{Metrics}
12318 @item
12319 @code{gnatls}
12320 @item
12321 @code{gnatstub}
12322 @item
12323 @code{IDE}
12324 @item
12325 @code{Language_Processing}
12326 @end itemize
12327
12328 @noindent
12329 In its simplest form, a package may be empty:
12330
12331 @smallexample @c projectfile
12332 @group
12333 project Simple is
12334 package Builder is
12335 end Builder;
12336 end Simple;
12337 @end group
12338 @end smallexample
12339
12340 @noindent
12341 A package may contain @emph{attribute declarations},
12342 @emph{variable declarations} and @emph{case constructions}, as will be
12343 described below.
12344
12345 When there is ambiguity between a project name and a package name,
12346 the name always designates the project. To avoid possible confusion, it is
12347 always a good idea to avoid naming a project with one of the
12348 names allowed for packages or any name that starts with @code{gnat}.
12349
12350 @node Expressions
12351 @subsection Expressions
12352
12353 @noindent
12354 An @emph{expression} is either a @emph{string expression} or a
12355 @emph{string list expression}.
12356
12357 A @emph{string expression} is either a @emph{simple string expression} or a
12358 @emph{compound string expression}.
12359
12360 A @emph{simple string expression} is one of the following:
12361 @itemize @bullet
12362 @item A literal string; e.g.@: @code{"comm/my_proj.gpr"}
12363 @item A string-valued variable reference (@pxref{Variables})
12364 @item A string-valued attribute reference (@pxref{Attributes})
12365 @item An external reference (@pxref{External References in Project Files})
12366 @end itemize
12367
12368 @noindent
12369 A @emph{compound string expression} is a concatenation of string expressions,
12370 using the operator @code{"&"}
12371 @smallexample
12372 Path & "/" & File_Name & ".ads"
12373 @end smallexample
12374
12375 @noindent
12376 A @emph{string list expression} is either a
12377 @emph{simple string list expression} or a
12378 @emph{compound string list expression}.
12379
12380 A @emph{simple string list expression} is one of the following:
12381 @itemize @bullet
12382 @item A parenthesized list of zero or more string expressions,
12383 separated by commas
12384 @smallexample
12385 File_Names := (File_Name, "gnat.adc", File_Name & ".orig");
12386 Empty_List := ();
12387 @end smallexample
12388 @item A string list-valued variable reference
12389 @item A string list-valued attribute reference
12390 @end itemize
12391
12392 @noindent
12393 A @emph{compound string list expression} is the concatenation (using
12394 @code{"&"}) of a simple string list expression and an expression. Note that
12395 each term in a compound string list expression, except the first, may be
12396 either a string expression or a string list expression.
12397
12398 @smallexample @c projectfile
12399 @group
12400 File_Name_List := () & File_Name; -- One string in this list
12401 Extended_File_Name_List := File_Name_List & (File_Name & ".orig");
12402 -- Two strings
12403 Big_List := File_Name_List & Extended_File_Name_List;
12404 -- Concatenation of two string lists: three strings
12405 Illegal_List := "gnat.adc" & Extended_File_Name_List;
12406 -- Illegal: must start with a string list
12407 @end group
12408 @end smallexample
12409
12410 @node String Types
12411 @subsection String Types
12412
12413 @noindent
12414 A @emph{string type declaration} introduces a discrete set of string literals.
12415 If a string variable is declared to have this type, its value
12416 is restricted to the given set of literals.
12417
12418 Here is an example of a string type declaration:
12419
12420 @smallexample @c projectfile
12421 type OS is ("NT", "nt", "Unix", "GNU/Linux", "other OS");
12422 @end smallexample
12423
12424 @noindent
12425 Variables of a string type are called @emph{typed variables}; all other
12426 variables are called @emph{untyped variables}. Typed variables are
12427 particularly useful in @code{case} constructions, to support conditional
12428 attribute declarations.
12429 (@pxref{case Constructions}).
12430
12431 The string literals in the list are case sensitive and must all be different.
12432 They may include any graphic characters allowed in Ada, including spaces.
12433
12434 A string type may only be declared at the project level, not inside a package.
12435
12436 A string type may be referenced by its name if it has been declared in the same
12437 project file, or by an expanded name whose prefix is the name of the project
12438 in which it is declared.
12439
12440 @node Variables
12441 @subsection Variables
12442
12443 @noindent
12444 A variable may be declared at the project file level, or within a package.
12445 Here are some examples of variable declarations:
12446
12447 @smallexample @c projectfile
12448 @group
12449 This_OS : OS := external ("OS"); -- a typed variable declaration
12450 That_OS := "GNU/Linux"; -- an untyped variable declaration
12451 @end group
12452 @end smallexample
12453
12454 @noindent
12455 The syntax of a @emph{typed variable declaration} is identical to the Ada
12456 syntax for an object declaration. By contrast, the syntax of an untyped
12457 variable declaration is identical to an Ada assignment statement. In fact,
12458 variable declarations in project files have some of the characteristics of
12459 an assignment, in that successive declarations for the same variable are
12460 allowed. Untyped variable declarations do establish the expected kind of the
12461 variable (string or string list), and successive declarations for it must
12462 respect the initial kind.
12463
12464 @noindent
12465 A string variable declaration (typed or untyped) declares a variable
12466 whose value is a string. This variable may be used as a string expression.
12467 @smallexample @c projectfile
12468 File_Name := "readme.txt";
12469 Saved_File_Name := File_Name & ".saved";
12470 @end smallexample
12471
12472 @noindent
12473 A string list variable declaration declares a variable whose value is a list
12474 of strings. The list may contain any number (zero or more) of strings.
12475
12476 @smallexample @c projectfile
12477 Empty_List := ();
12478 List_With_One_Element := ("^-gnaty^-gnaty^");
12479 List_With_Two_Elements := List_With_One_Element & "^-gnatg^-gnatg^";
12480 Long_List := ("main.ada", "pack1_.ada", "pack1.ada", "pack2_.ada"
12481 "pack2.ada", "util_.ada", "util.ada");
12482 @end smallexample
12483
12484 @noindent
12485 The same typed variable may not be declared more than once at project level,
12486 and it may not be declared more than once in any package; it is in effect
12487 a constant.
12488
12489 The same untyped variable may be declared several times. Declarations are
12490 elaborated in the order in which they appear, so the new value replaces
12491 the old one, and any subsequent reference to the variable uses the new value.
12492 However, as noted above, if a variable has been declared as a string, all
12493 subsequent
12494 declarations must give it a string value. Similarly, if a variable has
12495 been declared as a string list, all subsequent declarations
12496 must give it a string list value.
12497
12498 A @emph{variable reference} may take several forms:
12499
12500 @itemize @bullet
12501 @item The simple variable name, for a variable in the current package (if any)
12502 or in the current project
12503 @item An expanded name, whose prefix is a context name.
12504 @end itemize
12505
12506 @noindent
12507 A @emph{context} may be one of the following:
12508
12509 @itemize @bullet
12510 @item The name of an existing package in the current project
12511 @item The name of an imported project of the current project
12512 @item The name of an ancestor project (i.e., a project extended by the current
12513 project, either directly or indirectly)
12514 @item An expanded name whose prefix is an imported/parent project name, and
12515 whose selector is a package name in that project.
12516 @end itemize
12517
12518 @noindent
12519 A variable reference may be used in an expression.
12520
12521 @node Attributes
12522 @subsection Attributes
12523
12524 @noindent
12525 A project (and its packages) may have @emph{attributes} that define
12526 the project's properties. Some attributes have values that are strings;
12527 others have values that are string lists.
12528
12529 There are two categories of attributes: @emph{simple attributes}
12530 and @emph{associative arrays} (@pxref{Associative Array Attributes}).
12531
12532 Legal project attribute names, and attribute names for each legal package are
12533 listed below. Attributes names are case-insensitive.
12534
12535 The following attributes are defined on projects (all are simple attributes):
12536
12537 @multitable @columnfractions .4 .3
12538 @item @emph{Attribute Name}
12539 @tab @emph{Value}
12540 @item @code{Source_Files}
12541 @tab string list
12542 @item @code{Source_Dirs}
12543 @tab string list
12544 @item @code{Source_List_File}
12545 @tab string
12546 @item @code{Object_Dir}
12547 @tab string
12548 @item @code{Exec_Dir}
12549 @tab string
12550 @item @code{Excluded_Source_Dirs}
12551 @tab string list
12552 @item @code{Excluded_Source_Files}
12553 @tab string list
12554 @item @code{Excluded_Source_List_File}
12555 @tab string
12556 @item @code{Languages}
12557 @tab string list
12558 @item @code{Main}
12559 @tab string list
12560 @item @code{Library_Dir}
12561 @tab string
12562 @item @code{Library_Name}
12563 @tab string
12564 @item @code{Library_Kind}
12565 @tab string
12566 @item @code{Library_Version}
12567 @tab string
12568 @item @code{Library_Interface}
12569 @tab string
12570 @item @code{Library_Auto_Init}
12571 @tab string
12572 @item @code{Library_Options}
12573 @tab string list
12574 @item @code{Library_Src_Dir}
12575 @tab string
12576 @item @code{Library_ALI_Dir}
12577 @tab string
12578 @item @code{Library_GCC}
12579 @tab string
12580 @item @code{Library_Symbol_File}
12581 @tab string
12582 @item @code{Library_Symbol_Policy}
12583 @tab string
12584 @item @code{Library_Reference_Symbol_File}
12585 @tab string
12586 @item @code{Externally_Built}
12587 @tab string
12588 @end multitable
12589
12590 @noindent
12591 The following attributes are defined for package @code{Naming}
12592 (@pxref{Naming Schemes}):
12593
12594 @multitable @columnfractions .4 .2 .2 .2
12595 @item Attribute Name @tab Category @tab Index @tab Value
12596 @item @code{Spec_Suffix}
12597 @tab associative array
12598 @tab language name
12599 @tab string
12600 @item @code{Body_Suffix}
12601 @tab associative array
12602 @tab language name
12603 @tab string
12604 @item @code{Separate_Suffix}
12605 @tab simple attribute
12606 @tab n/a
12607 @tab string
12608 @item @code{Casing}
12609 @tab simple attribute
12610 @tab n/a
12611 @tab string
12612 @item @code{Dot_Replacement}
12613 @tab simple attribute
12614 @tab n/a
12615 @tab string
12616 @item @code{Spec}
12617 @tab associative array
12618 @tab Ada unit name
12619 @tab string
12620 @item @code{Body}
12621 @tab associative array
12622 @tab Ada unit name
12623 @tab string
12624 @item @code{Specification_Exceptions}
12625 @tab associative array
12626 @tab language name
12627 @tab string list
12628 @item @code{Implementation_Exceptions}
12629 @tab associative array
12630 @tab language name
12631 @tab string list
12632 @end multitable
12633
12634 @noindent
12635 The following attributes are defined for packages @code{Builder},
12636 @code{Compiler}, @code{Binder},
12637 @code{Linker}, @code{Cross_Reference}, and @code{Finder}
12638 (@pxref{^Switches^Switches^ and Project Files}).
12639
12640 @multitable @columnfractions .4 .2 .2 .2
12641 @item Attribute Name @tab Category @tab Index @tab Value
12642 @item @code{^Default_Switches^Default_Switches^}
12643 @tab associative array
12644 @tab language name
12645 @tab string list
12646 @item @code{^Switches^Switches^}
12647 @tab associative array
12648 @tab file name
12649 @tab string list
12650 @end multitable
12651
12652 @noindent
12653 In addition, package @code{Compiler} has a single string attribute
12654 @code{Local_Configuration_Pragmas} and package @code{Builder} has a single
12655 string attribute @code{Global_Configuration_Pragmas}.
12656
12657 @noindent
12658 Each simple attribute has a default value: the empty string (for string-valued
12659 attributes) and the empty list (for string list-valued attributes).
12660
12661 An attribute declaration defines a new value for an attribute.
12662
12663 Examples of simple attribute declarations:
12664
12665 @smallexample @c projectfile
12666 for Object_Dir use "objects";
12667 for Source_Dirs use ("units", "test/drivers");
12668 @end smallexample
12669
12670 @noindent
12671 The syntax of a @dfn{simple attribute declaration} is similar to that of an
12672 attribute definition clause in Ada.
12673
12674 Attributes references may be appear in expressions.
12675 The general form for such a reference is @code{<entity>'<attribute>}:
12676 Associative array attributes are functions. Associative
12677 array attribute references must have an argument that is a string literal.
12678
12679 Examples are:
12680
12681 @smallexample @c projectfile
12682 project'Object_Dir
12683 Naming'Dot_Replacement
12684 Imported_Project'Source_Dirs
12685 Imported_Project.Naming'Casing
12686 Builder'^Default_Switches^Default_Switches^("Ada")
12687 @end smallexample
12688
12689 @noindent
12690 The prefix of an attribute may be:
12691 @itemize @bullet
12692 @item @code{project} for an attribute of the current project
12693 @item The name of an existing package of the current project
12694 @item The name of an imported project
12695 @item The name of a parent project that is extended by the current project
12696 @item An expanded name whose prefix is imported/parent project name,
12697 and whose selector is a package name
12698 @end itemize
12699
12700 @noindent
12701 Example:
12702 @smallexample @c projectfile
12703 @group
12704 project Prj is
12705 for Source_Dirs use project'Source_Dirs & "units";
12706 for Source_Dirs use project'Source_Dirs & "test/drivers"
12707 end Prj;
12708 @end group
12709 @end smallexample
12710
12711 @noindent
12712 In the first attribute declaration, initially the attribute @code{Source_Dirs}
12713 has the default value: an empty string list. After this declaration,
12714 @code{Source_Dirs} is a string list of one element: @code{"units"}.
12715 After the second attribute declaration @code{Source_Dirs} is a string list of
12716 two elements: @code{"units"} and @code{"test/drivers"}.
12717
12718 Note: this example is for illustration only. In practice,
12719 the project file would contain only one attribute declaration:
12720
12721 @smallexample @c projectfile
12722 for Source_Dirs use ("units", "test/drivers");
12723 @end smallexample
12724
12725 @node Associative Array Attributes
12726 @subsection Associative Array Attributes
12727
12728 @noindent
12729 Some attributes are defined as @emph{associative arrays}. An associative
12730 array may be regarded as a function that takes a string as a parameter
12731 and delivers a string or string list value as its result.
12732
12733 Here are some examples of single associative array attribute associations:
12734
12735 @smallexample @c projectfile
12736 for Body ("main") use "Main.ada";
12737 for ^Switches^Switches^ ("main.ada")
12738 use ("^-v^-v^",
12739 "^-gnatv^-gnatv^");
12740 for ^Switches^Switches^ ("main.ada")
12741 use Builder'^Switches^Switches^ ("main.ada")
12742 & "^-g^-g^";
12743 @end smallexample
12744
12745 @noindent
12746 Like untyped variables and simple attributes, associative array attributes
12747 may be declared several times. Each declaration supplies a new value for the
12748 attribute, and replaces the previous setting.
12749
12750 @noindent
12751 An associative array attribute may be declared as a full associative array
12752 declaration, with the value of the same attribute in an imported or extended
12753 project.
12754
12755 @smallexample @c projectfile
12756 package Builder is
12757 for Default_Switches use Default.Builder'Default_Switches;
12758 end Builder;
12759 @end smallexample
12760
12761 @noindent
12762 In this example, @code{Default} must be either a project imported by the
12763 current project, or the project that the current project extends. If the
12764 attribute is in a package (in this case, in package @code{Builder}), the same
12765 package needs to be specified.
12766
12767 @noindent
12768 A full associative array declaration replaces any other declaration for the
12769 attribute, including other full associative array declaration. Single
12770 associative array associations may be declare after a full associative
12771 declaration, modifying the value for a single association of the attribute.
12772
12773 @node case Constructions
12774 @subsection @code{case} Constructions
12775
12776 @noindent
12777 A @code{case} construction is used in a project file to effect conditional
12778 behavior.
12779 Here is a typical example:
12780
12781 @smallexample @c projectfile
12782 @group
12783 project MyProj is
12784 type OS_Type is ("GNU/Linux", "Unix", "NT", "VMS");
12785
12786 OS : OS_Type := external ("OS", "GNU/Linux");
12787 @end group
12788
12789 @group
12790 package Compiler is
12791 case OS is
12792 when "GNU/Linux" | "Unix" =>
12793 for ^Default_Switches^Default_Switches^ ("Ada")
12794 use ("^-gnath^-gnath^");
12795 when "NT" =>
12796 for ^Default_Switches^Default_Switches^ ("Ada")
12797 use ("^-gnatP^-gnatP^");
12798 when others =>
12799 end case;
12800 end Compiler;
12801 end MyProj;
12802 @end group
12803 @end smallexample
12804
12805 @noindent
12806 The syntax of a @code{case} construction is based on the Ada case statement
12807 (although there is no @code{null} construction for empty alternatives).
12808
12809 The case expression must be a typed string variable.
12810 Each alternative comprises the reserved word @code{when}, either a list of
12811 literal strings separated by the @code{"|"} character or the reserved word
12812 @code{others}, and the @code{"=>"} token.
12813 Each literal string must belong to the string type that is the type of the
12814 case variable.
12815 An @code{others} alternative, if present, must occur last.
12816
12817 After each @code{=>}, there are zero or more constructions. The only
12818 constructions allowed in a case construction are other case constructions,
12819 attribute declarations and variable declarations. String type declarations and
12820 package declarations are not allowed. Variable declarations are restricted to
12821 variables that have already been declared before the case construction.
12822
12823 The value of the case variable is often given by an external reference
12824 (@pxref{External References in Project Files}).
12825
12826 @c ****************************************
12827 @c * Objects and Sources in Project Files *
12828 @c ****************************************
12829
12830 @node Objects and Sources in Project Files
12831 @section Objects and Sources in Project Files
12832
12833 @menu
12834 * Object Directory::
12835 * Exec Directory::
12836 * Source Directories::
12837 * Source File Names::
12838 @end menu
12839
12840 @noindent
12841 Each project has exactly one object directory and one or more source
12842 directories. The source directories must contain at least one source file,
12843 unless the project file explicitly specifies that no source files are present
12844 (@pxref{Source File Names}).
12845
12846 @node Object Directory
12847 @subsection Object Directory
12848
12849 @noindent
12850 The object directory for a project is the directory containing the compiler's
12851 output (such as @file{ALI} files and object files) for the project's immediate
12852 sources.
12853
12854 The object directory is given by the value of the attribute @code{Object_Dir}
12855 in the project file.
12856
12857 @smallexample @c projectfile
12858 for Object_Dir use "objects";
12859 @end smallexample
12860
12861 @noindent
12862 The attribute @code{Object_Dir} has a string value, the path name of the object
12863 directory. The path name may be absolute or relative to the directory of the
12864 project file. This directory must already exist, and be readable and writable.
12865
12866 By default, when the attribute @code{Object_Dir} is not given an explicit value
12867 or when its value is the empty string, the object directory is the same as the
12868 directory containing the project file.
12869
12870 @node Exec Directory
12871 @subsection Exec Directory
12872
12873 @noindent
12874 The exec directory for a project is the directory containing the executables
12875 for the project's main subprograms.
12876
12877 The exec directory is given by the value of the attribute @code{Exec_Dir}
12878 in the project file.
12879
12880 @smallexample @c projectfile
12881 for Exec_Dir use "executables";
12882 @end smallexample
12883
12884 @noindent
12885 The attribute @code{Exec_Dir} has a string value, the path name of the exec
12886 directory. The path name may be absolute or relative to the directory of the
12887 project file. This directory must already exist, and be writable.
12888
12889 By default, when the attribute @code{Exec_Dir} is not given an explicit value
12890 or when its value is the empty string, the exec directory is the same as the
12891 object directory of the project file.
12892
12893 @node Source Directories
12894 @subsection Source Directories
12895
12896 @noindent
12897 The source directories of a project are specified by the project file
12898 attribute @code{Source_Dirs}.
12899
12900 This attribute's value is a string list. If the attribute is not given an
12901 explicit value, then there is only one source directory, the one where the
12902 project file resides.
12903
12904 A @code{Source_Dirs} attribute that is explicitly defined to be the empty list,
12905 as in
12906
12907 @smallexample @c projectfile
12908 for Source_Dirs use ();
12909 @end smallexample
12910
12911 @noindent
12912 indicates that the project contains no source files.
12913
12914 Otherwise, each string in the string list designates one or more
12915 source directories.
12916
12917 @smallexample @c projectfile
12918 for Source_Dirs use ("sources", "test/drivers");
12919 @end smallexample
12920
12921 @noindent
12922 If a string in the list ends with @code{"/**"}, then the directory whose path
12923 name precedes the two asterisks, as well as all its subdirectories
12924 (recursively), are source directories.
12925
12926 @smallexample @c projectfile
12927 for Source_Dirs use ("/system/sources/**");
12928 @end smallexample
12929
12930 @noindent
12931 Here the directory @code{/system/sources} and all of its subdirectories
12932 (recursively) are source directories.
12933
12934 To specify that the source directories are the directory of the project file
12935 and all of its subdirectories, you can declare @code{Source_Dirs} as follows:
12936 @smallexample @c projectfile
12937 for Source_Dirs use ("./**");
12938 @end smallexample
12939
12940 @noindent
12941 Each of the source directories must exist and be readable.
12942
12943 @node Source File Names
12944 @subsection Source File Names
12945
12946 @noindent
12947 In a project that contains source files, their names may be specified by the
12948 attributes @code{Source_Files} (a string list) or @code{Source_List_File}
12949 (a string). Source file names never include any directory information.
12950
12951 If the attribute @code{Source_Files} is given an explicit value, then each
12952 element of the list is a source file name.
12953
12954 @smallexample @c projectfile
12955 for Source_Files use ("main.adb");
12956 for Source_Files use ("main.adb", "pack1.ads", "pack2.adb");
12957 @end smallexample
12958
12959 @noindent
12960 If the attribute @code{Source_Files} is not given an explicit value,
12961 but the attribute @code{Source_List_File} is given a string value,
12962 then the source file names are contained in the text file whose path name
12963 (absolute or relative to the directory of the project file) is the
12964 value of the attribute @code{Source_List_File}.
12965
12966 Each line in the file that is not empty or is not a comment
12967 contains a source file name.
12968
12969 @smallexample @c projectfile
12970 for Source_List_File use "source_list.txt";
12971 @end smallexample
12972
12973 @noindent
12974 By default, if neither the attribute @code{Source_Files} nor the attribute
12975 @code{Source_List_File} is given an explicit value, then each file in the
12976 source directories that conforms to the project's naming scheme
12977 (@pxref{Naming Schemes}) is an immediate source of the project.
12978
12979 A warning is issued if both attributes @code{Source_Files} and
12980 @code{Source_List_File} are given explicit values. In this case, the attribute
12981 @code{Source_Files} prevails.
12982
12983 Each source file name must be the name of one existing source file
12984 in one of the source directories.
12985
12986 A @code{Source_Files} attribute whose value is an empty list
12987 indicates that there are no source files in the project.
12988
12989 If the order of the source directories is known statically, that is if
12990 @code{"/**"} is not used in the string list @code{Source_Dirs}, then there may
12991 be several files with the same source file name. In this case, only the file
12992 in the first directory is considered as an immediate source of the project
12993 file. If the order of the source directories is not known statically, it is
12994 an error to have several files with the same source file name.
12995
12996 Projects can be specified to have no Ada source
12997 files: the value of (@code{Source_Dirs} or @code{Source_Files} may be an empty
12998 list, or the @code{"Ada"} may be absent from @code{Languages}:
12999
13000 @smallexample @c projectfile
13001 for Source_Dirs use ();
13002 for Source_Files use ();
13003 for Languages use ("C", "C++");
13004 @end smallexample
13005
13006 @noindent
13007 Otherwise, a project must contain at least one immediate source.
13008
13009 Projects with no source files are useful as template packages
13010 (@pxref{Packages in Project Files}) for other projects; in particular to
13011 define a package @code{Naming} (@pxref{Naming Schemes}).
13012
13013 @c ****************************
13014 @c * Importing Projects *
13015 @c ****************************
13016
13017 @node Importing Projects
13018 @section Importing Projects
13019 @cindex @code{ADA_PROJECT_PATH}
13020
13021 @noindent
13022 An immediate source of a project P may depend on source files that
13023 are neither immediate sources of P nor in the predefined library.
13024 To get this effect, P must @emph{import} the projects that contain the needed
13025 source files.
13026
13027 @smallexample @c projectfile
13028 @group
13029 with "project1", "utilities.gpr";
13030 with "/namings/apex.gpr";
13031 project Main is
13032 @dots{}
13033 @end group
13034 @end smallexample
13035
13036 @noindent
13037 As can be seen in this example, the syntax for importing projects is similar
13038 to the syntax for importing compilation units in Ada. However, project files
13039 use literal strings instead of names, and the @code{with} clause identifies
13040 project files rather than packages.
13041
13042 Each literal string is the file name or path name (absolute or relative) of a
13043 project file. If a string corresponds to a file name, with no path or a
13044 relative path, then its location is determined by the @emph{project path}. The
13045 latter can be queried using @code{gnatls -v}. It contains:
13046
13047 @itemize @bullet
13048 @item
13049 In first position, the directory containing the current project file.
13050 @item
13051 In last position, the default project directory. This default project directory
13052 is part of the GNAT installation and is the standard place to install project
13053 files giving access to standard support libraries.
13054 @ifclear vms
13055 @ref{Installing a library}
13056 @end ifclear
13057
13058 @item
13059 In between, all the directories referenced in the
13060 ^environment variable^logical name^ @env{ADA_PROJECT_PATH} if it exists.
13061 @end itemize
13062
13063 @noindent
13064 If a relative pathname is used, as in
13065
13066 @smallexample @c projectfile
13067 with "tests/proj";
13068 @end smallexample
13069
13070 @noindent
13071 then the full path for the project is constructed by concatenating this
13072 relative path to those in the project path, in order, until a matching file is
13073 found. Any symbolic link will be fully resolved in the directory of the
13074 importing project file before the imported project file is examined.
13075
13076 If the @code{with}'ed project file name does not have an extension,
13077 the default is @file{^.gpr^.GPR^}. If a file with this extension is not found,
13078 then the file name as specified in the @code{with} clause (no extension) will
13079 be used. In the above example, if a file @code{project1.gpr} is found, then it
13080 will be used; otherwise, if a file @code{^project1^PROJECT1^} exists
13081 then it will be used; if neither file exists, this is an error.
13082
13083 A warning is issued if the name of the project file does not match the
13084 name of the project; this check is case insensitive.
13085
13086 Any source file that is an immediate source of the imported project can be
13087 used by the immediate sources of the importing project, transitively. Thus
13088 if @code{A} imports @code{B}, and @code{B} imports @code{C}, the immediate
13089 sources of @code{A} may depend on the immediate sources of @code{C}, even if
13090 @code{A} does not import @code{C} explicitly. However, this is not recommended,
13091 because if and when @code{B} ceases to import @code{C}, some sources in
13092 @code{A} will no longer compile.
13093
13094 A side effect of this capability is that normally cyclic dependencies are not
13095 permitted: if @code{A} imports @code{B} (directly or indirectly) then @code{B}
13096 is not allowed to import @code{A}. However, there are cases when cyclic
13097 dependencies would be beneficial. For these cases, another form of import
13098 between projects exists, the @code{limited with}: a project @code{A} that
13099 imports a project @code{B} with a straight @code{with} may also be imported,
13100 directly or indirectly, by @code{B} on the condition that imports from @code{B}
13101 to @code{A} include at least one @code{limited with}.
13102
13103 @smallexample @c 0projectfile
13104 with "../b/b.gpr";
13105 with "../c/c.gpr";
13106 project A is
13107 end A;
13108
13109 limited with "../a/a.gpr";
13110 project B is
13111 end B;
13112
13113 with "../d/d.gpr";
13114 project C is
13115 end C;
13116
13117 limited with "../a/a.gpr";
13118 project D is
13119 end D;
13120 @end smallexample
13121
13122 @noindent
13123 In the above legal example, there are two project cycles:
13124 @itemize @bullet
13125 @item A-> B-> A
13126 @item A -> C -> D -> A
13127 @end itemize
13128
13129 @noindent
13130 In each of these cycle there is one @code{limited with}: import of @code{A}
13131 from @code{B} and import of @code{A} from @code{D}.
13132
13133 The difference between straight @code{with} and @code{limited with} is that
13134 the name of a project imported with a @code{limited with} cannot be used in the
13135 project that imports it. In particular, its packages cannot be renamed and
13136 its variables cannot be referred to.
13137
13138 An exception to the above rules for @code{limited with} is that for the main
13139 project specified to @command{gnatmake} or to the @command{GNAT} driver a
13140 @code{limited with} is equivalent to a straight @code{with}. For example,
13141 in the example above, projects @code{B} and @code{D} could not be main
13142 projects for @command{gnatmake} or to the @command{GNAT} driver, because they
13143 each have a @code{limited with} that is the only one in a cycle of importing
13144 projects.
13145
13146 @c *********************
13147 @c * Project Extension *
13148 @c *********************
13149
13150 @node Project Extension
13151 @section Project Extension
13152
13153 @noindent
13154 During development of a large system, it is sometimes necessary to use
13155 modified versions of some of the source files, without changing the original
13156 sources. This can be achieved through the @emph{project extension} facility.
13157
13158 @smallexample @c projectfile
13159 project Modified_Utilities extends "/baseline/utilities.gpr" is @dots{}
13160 @end smallexample
13161
13162 @noindent
13163 A project extension declaration introduces an extending project
13164 (the @emph{child}) and a project being extended (the @emph{parent}).
13165
13166 By default, a child project inherits all the sources of its parent.
13167 However, inherited sources can be overridden: a unit in a parent is hidden
13168 by a unit of the same name in the child.
13169
13170 Inherited sources are considered to be sources (but not immediate sources)
13171 of the child project; see @ref{Project File Syntax}.
13172
13173 An inherited source file retains any switches specified in the parent project.
13174
13175 For example if the project @code{Utilities} contains the spec and the
13176 body of an Ada package @code{Util_IO}, then the project
13177 @code{Modified_Utilities} can contain a new body for package @code{Util_IO}.
13178 The original body of @code{Util_IO} will not be considered in program builds.
13179 However, the package spec will still be found in the project
13180 @code{Utilities}.
13181
13182 A child project can have only one parent, except when it is qualified as
13183 abstract. But it may import any number of other projects.
13184
13185 A project is not allowed to import directly or indirectly at the same time a
13186 child project and any of its ancestors.
13187
13188 @c *******************************
13189 @c * Project Hierarchy Extension *
13190 @c *******************************
13191
13192 @node Project Hierarchy Extension
13193 @section Project Hierarchy Extension
13194
13195 @noindent
13196 When extending a large system spanning multiple projects, it is often
13197 inconvenient to extend every project in the hierarchy that is impacted by a
13198 small change introduced. In such cases, it is possible to create a virtual
13199 extension of entire hierarchy using @code{extends all} relationship.
13200
13201 When the project is extended using @code{extends all} inheritance, all projects
13202 that are imported by it, both directly and indirectly, are considered virtually
13203 extended. That is, the Project Manager creates "virtual projects"
13204 that extend every project in the hierarchy; all these virtual projects have
13205 no sources of their own and have as object directory the object directory of
13206 the root of "extending all" project.
13207
13208 It is possible to explicitly extend one or more projects in the hierarchy
13209 in order to modify the sources. These extending projects must be imported by
13210 the "extending all" project, which will replace the corresponding virtual
13211 projects with the explicit ones.
13212
13213 When building such a project hierarchy extension, the Project Manager will
13214 ensure that both modified sources and sources in virtual extending projects
13215 that depend on them, are recompiled.
13216
13217 By means of example, consider the following hierarchy of projects.
13218
13219 @enumerate
13220 @item
13221 project A, containing package P1
13222 @item
13223 project B importing A and containing package P2 which depends on P1
13224 @item
13225 project C importing B and containing package P3 which depends on P2
13226 @end enumerate
13227
13228 @noindent
13229 We want to modify packages P1 and P3.
13230
13231 This project hierarchy will need to be extended as follows:
13232
13233 @enumerate
13234 @item
13235 Create project A1 that extends A, placing modified P1 there:
13236
13237 @smallexample @c 0projectfile
13238 project A1 extends "(@dots{})/A" is
13239 end A1;
13240 @end smallexample
13241
13242 @item
13243 Create project C1 that "extends all" C and imports A1, placing modified
13244 P3 there:
13245
13246 @smallexample @c 0projectfile
13247 with "(@dots{})/A1";
13248 project C1 extends all "(@dots{})/C" is
13249 end C1;
13250 @end smallexample
13251 @end enumerate
13252
13253 When you build project C1, your entire modified project space will be
13254 recompiled, including the virtual project B1 that has been impacted by the
13255 "extending all" inheritance of project C.
13256
13257 Note that if a Library Project in the hierarchy is virtually extended,
13258 the virtual project that extends the Library Project is not a Library Project.
13259
13260 @c ****************************************
13261 @c * External References in Project Files *
13262 @c ****************************************
13263
13264 @node External References in Project Files
13265 @section External References in Project Files
13266
13267 @noindent
13268 A project file may contain references to external variables; such references
13269 are called @emph{external references}.
13270
13271 An external variable is either defined as part of the environment (an
13272 environment variable in Unix, for example) or else specified on the command
13273 line via the @option{^-X^/EXTERNAL_REFERENCE=^@emph{vbl}=@emph{value}} switch.
13274 If both, then the command line value is used.
13275
13276 The value of an external reference is obtained by means of the built-in
13277 function @code{external}, which returns a string value.
13278 This function has two forms:
13279 @itemize @bullet
13280 @item @code{external (external_variable_name)}
13281 @item @code{external (external_variable_name, default_value)}
13282 @end itemize
13283
13284 @noindent
13285 Each parameter must be a string literal. For example:
13286
13287 @smallexample @c projectfile
13288 external ("USER")
13289 external ("OS", "GNU/Linux")
13290 @end smallexample
13291
13292 @noindent
13293 In the form with one parameter, the function returns the value of
13294 the external variable given as parameter. If this name is not present in the
13295 environment, the function returns an empty string.
13296
13297 In the form with two string parameters, the second argument is
13298 the value returned when the variable given as the first argument is not
13299 present in the environment. In the example above, if @code{"OS"} is not
13300 the name of ^an environment variable^a logical name^ and is not passed on
13301 the command line, then the returned value is @code{"GNU/Linux"}.
13302
13303 An external reference may be part of a string expression or of a string
13304 list expression, and can therefore appear in a variable declaration or
13305 an attribute declaration.
13306
13307 @smallexample @c projectfile
13308 @group
13309 type Mode_Type is ("Debug", "Release");
13310 Mode : Mode_Type := external ("MODE");
13311 case Mode is
13312 when "Debug" =>
13313 @dots{}
13314 @end group
13315 @end smallexample
13316
13317 @c *****************************
13318 @c * Packages in Project Files *
13319 @c *****************************
13320
13321 @node Packages in Project Files
13322 @section Packages in Project Files
13323
13324 @noindent
13325 A @emph{package} defines the settings for project-aware tools within a
13326 project.
13327 For each such tool one can declare a package; the names for these
13328 packages are preset (@pxref{Packages}).
13329 A package may contain variable declarations, attribute declarations, and case
13330 constructions.
13331
13332 @smallexample @c projectfile
13333 @group
13334 project Proj is
13335 package Builder is -- used by gnatmake
13336 for ^Default_Switches^Default_Switches^ ("Ada")
13337 use ("^-v^-v^",
13338 "^-g^-g^");
13339 end Builder;
13340 end Proj;
13341 @end group
13342 @end smallexample
13343
13344 @noindent
13345 The syntax of package declarations mimics that of package in Ada.
13346
13347 Most of the packages have an attribute
13348 @code{^Default_Switches^Default_Switches^}.
13349 This attribute is an associative array, and its value is a string list.
13350 The index of the associative array is the name of a programming language (case
13351 insensitive). This attribute indicates the ^switch^switch^
13352 or ^switches^switches^ to be used
13353 with the corresponding tool.
13354
13355 Some packages also have another attribute, @code{^Switches^Switches^},
13356 an associative array whose value is a string list.
13357 The index is the name of a source file.
13358 This attribute indicates the ^switch^switch^
13359 or ^switches^switches^ to be used by the corresponding
13360 tool when dealing with this specific file.
13361
13362 Further information on these ^switch^switch^-related attributes is found in
13363 @ref{^Switches^Switches^ and Project Files}.
13364
13365 A package may be declared as a @emph{renaming} of another package; e.g., from
13366 the project file for an imported project.
13367
13368 @smallexample @c projectfile
13369 @group
13370 with "/global/apex.gpr";
13371 project Example is
13372 package Naming renames Apex.Naming;
13373 @dots{}
13374 end Example;
13375 @end group
13376 @end smallexample
13377
13378 @noindent
13379 Packages that are renamed in other project files often come from project files
13380 that have no sources: they are just used as templates. Any modification in the
13381 template will be reflected automatically in all the project files that rename
13382 a package from the template.
13383
13384 In addition to the tool-oriented packages, you can also declare a package
13385 named @code{Naming} to establish specialized source file naming conventions
13386 (@pxref{Naming Schemes}).
13387
13388 @c ************************************
13389 @c * Variables from Imported Projects *
13390 @c ************************************
13391
13392 @node Variables from Imported Projects
13393 @section Variables from Imported Projects
13394
13395 @noindent
13396 An attribute or variable defined in an imported or parent project can
13397 be used in expressions in the importing / extending project.
13398 Such an attribute or variable is denoted by an expanded name whose prefix
13399 is either the name of the project or the expanded name of a package within
13400 a project.
13401
13402 @smallexample @c projectfile
13403 @group
13404 with "imported";
13405 project Main extends "base" is
13406 Var1 := Imported.Var;
13407 Var2 := Base.Var & ".new";
13408 @end group
13409
13410 @group
13411 package Builder is
13412 for ^Default_Switches^Default_Switches^ ("Ada")
13413 use Imported.Builder'Ada_^Switches^Switches^ &
13414 "^-gnatg^-gnatg^" &
13415 "^-v^-v^";
13416 end Builder;
13417 @end group
13418
13419 @group
13420 package Compiler is
13421 for ^Default_Switches^Default_Switches^ ("Ada")
13422 use Base.Compiler'Ada_^Switches^Switches^;
13423 end Compiler;
13424 end Main;
13425 @end group
13426 @end smallexample
13427
13428 @noindent
13429 In this example:
13430
13431 @itemize @bullet
13432 @item
13433 The value of @code{Var1} is a copy of the variable @code{Var} defined
13434 in the project file @file{"imported.gpr"}
13435 @item
13436 the value of @code{Var2} is a copy of the value of variable @code{Var}
13437 defined in the project file @file{base.gpr}, concatenated with @code{".new"}
13438 @item
13439 attribute @code{^Default_Switches^Default_Switches^ ("Ada")} in package
13440 @code{Builder} is a string list that includes in its value a copy of the value
13441 of @code{Ada_^Switches^Switches^} defined in the @code{Builder} package
13442 in project file @file{imported.gpr} plus two new elements:
13443 @option{"^-gnatg^-gnatg^"}
13444 and @option{"^-v^-v^"};
13445 @item
13446 attribute @code{^Default_Switches^Default_Switches^ ("Ada")} in package
13447 @code{Compiler} is a copy of the variable @code{Ada_^Switches^Switches^}
13448 defined in the @code{Compiler} package in project file @file{base.gpr},
13449 the project being extended.
13450 @end itemize
13451
13452 @c ******************
13453 @c * Naming Schemes *
13454 @c ******************
13455
13456 @node Naming Schemes
13457 @section Naming Schemes
13458
13459 @noindent
13460 Sometimes an Ada software system is ported from a foreign compilation
13461 environment to GNAT, and the file names do not use the default GNAT
13462 conventions. Instead of changing all the file names (which for a variety
13463 of reasons might not be possible), you can define the relevant file
13464 naming scheme in the @code{Naming} package in your project file.
13465
13466 @noindent
13467 Note that the use of pragmas described in
13468 @ref{Alternative File Naming Schemes} by mean of a configuration
13469 pragmas file is not supported when using project files. You must use
13470 the features described in this paragraph. You can however use specify
13471 other configuration pragmas (@pxref{Specifying Configuration Pragmas}).
13472
13473 @ifclear vms
13474 For example, the following
13475 package models the Apex file naming rules:
13476
13477 @smallexample @c projectfile
13478 @group
13479 package Naming is
13480 for Casing use "lowercase";
13481 for Dot_Replacement use ".";
13482 for Spec_Suffix ("Ada") use ".1.ada";
13483 for Body_Suffix ("Ada") use ".2.ada";
13484 end Naming;
13485 @end group
13486 @end smallexample
13487 @end ifclear
13488
13489 @ifset vms
13490 For example, the following package models the HP Ada file naming rules:
13491
13492 @smallexample @c projectfile
13493 @group
13494 package Naming is
13495 for Casing use "lowercase";
13496 for Dot_Replacement use "__";
13497 for Spec_Suffix ("Ada") use "_.^ada^ada^";
13498 for Body_Suffix ("Ada") use ".^ada^ada^";
13499 end Naming;
13500 @end group
13501 @end smallexample
13502
13503 @noindent
13504 (Note that @code{Casing} is @code{"lowercase"} because GNAT gets the file
13505 names in lower case)
13506 @end ifset
13507
13508 @noindent
13509 You can define the following attributes in package @code{Naming}:
13510
13511 @table @code
13512
13513 @item @code{Casing}
13514 This must be a string with one of the three values @code{"lowercase"},
13515 @code{"uppercase"} or @code{"mixedcase"}; these strings are case insensitive.
13516
13517 @noindent
13518 If @code{Casing} is not specified, then the default is @code{"lowercase"}.
13519
13520 @item @code{Dot_Replacement}
13521 This must be a string whose value satisfies the following conditions:
13522
13523 @itemize @bullet
13524 @item It must not be empty
13525 @item It cannot start or end with an alphanumeric character
13526 @item It cannot be a single underscore
13527 @item It cannot start with an underscore followed by an alphanumeric
13528 @item It cannot contain a dot @code{'.'} except if the entire string
13529 is @code{"."}
13530 @end itemize
13531
13532 @noindent
13533 If @code{Dot_Replacement} is not specified, then the default is @code{"-"}.
13534
13535 @item @code{Spec_Suffix}
13536 This is an associative array (indexed by the programming language name, case
13537 insensitive) whose value is a string that must satisfy the following
13538 conditions:
13539
13540 @itemize @bullet
13541 @item It must not be empty
13542 @item It must include at least one dot
13543 @end itemize
13544 @noindent
13545 If @code{Spec_Suffix ("Ada")} is not specified, then the default is
13546 @code{"^.ads^.ADS^"}.
13547
13548 @item @code{Body_Suffix}
13549 This is an associative array (indexed by the programming language name, case
13550 insensitive) whose value is a string that must satisfy the following
13551 conditions:
13552
13553 @itemize @bullet
13554 @item It must not be empty
13555 @item It must include at least one dot
13556 @item It cannot be the same as @code{Spec_Suffix ("Ada")}
13557 @end itemize
13558 @noindent
13559 If @code{Body_Suffix ("Ada")} and @code{Spec_Suffix ("Ada")} end with the
13560 same string, then a file name that ends with the longest of these two suffixes
13561 will be a body if the longest suffix is @code{Body_Suffix ("Ada")} or a spec
13562 if the longest suffix is @code{Spec_Suffix ("Ada")}.
13563
13564 If @code{Body_Suffix ("Ada")} is not specified, then the default is
13565 @code{"^.adb^.ADB^"}.
13566
13567 @item @code{Separate_Suffix}
13568 This must be a string whose value satisfies the same conditions as
13569 @code{Body_Suffix}. The same "longest suffix" rules apply.
13570
13571 @noindent
13572 If @code{Separate_Suffix ("Ada")} is not specified, then it defaults to same
13573 value as @code{Body_Suffix ("Ada")}.
13574
13575 @item @code{Spec}
13576 @noindent
13577 You can use the associative array attribute @code{Spec} to define
13578 the source file name for an individual Ada compilation unit's spec. The array
13579 index must be a string literal that identifies the Ada unit (case insensitive).
13580 The value of this attribute must be a string that identifies the file that
13581 contains this unit's spec (case sensitive or insensitive depending on the
13582 operating system).
13583
13584 @smallexample @c projectfile
13585 for Spec ("MyPack.MyChild") use "mypack.mychild.spec";
13586 @end smallexample
13587
13588 @item @code{Body}
13589
13590 You can use the associative array attribute @code{Body} to
13591 define the source file name for an individual Ada compilation unit's body
13592 (possibly a subunit). The array index must be a string literal that identifies
13593 the Ada unit (case insensitive). The value of this attribute must be a string
13594 that identifies the file that contains this unit's body or subunit (case
13595 sensitive or insensitive depending on the operating system).
13596
13597 @smallexample @c projectfile
13598 for Body ("MyPack.MyChild") use "mypack.mychild.body";
13599 @end smallexample
13600 @end table
13601
13602 @c ********************
13603 @c * Library Projects *
13604 @c ********************
13605
13606 @node Library Projects
13607 @section Library Projects
13608
13609 @noindent
13610 @emph{Library projects} are projects whose object code is placed in a library.
13611 (Note that this facility is not yet supported on all platforms)
13612
13613 To create a library project, you need to define in its project file
13614 two project-level attributes: @code{Library_Name} and @code{Library_Dir}.
13615 Additionally, you may define other library-related attributes such as
13616 @code{Library_Kind}, @code{Library_Version}, @code{Library_Interface},
13617 @code{Library_Auto_Init}, @code{Library_Options} and @code{Library_GCC}.
13618
13619 The @code{Library_Name} attribute has a string value. There is no restriction
13620 on the name of a library. It is the responsibility of the developer to
13621 choose a name that will be accepted by the platform. It is recommended to
13622 choose names that could be Ada identifiers; such names are almost guaranteed
13623 to be acceptable on all platforms.
13624
13625 The @code{Library_Dir} attribute has a string value that designates the path
13626 (absolute or relative) of the directory where the library will reside.
13627 It must designate an existing directory, and this directory must be writable,
13628 different from the project's object directory and from any source directory
13629 in the project tree.
13630
13631 If both @code{Library_Name} and @code{Library_Dir} are specified and
13632 are legal, then the project file defines a library project. The optional
13633 library-related attributes are checked only for such project files.
13634
13635 The @code{Library_Kind} attribute has a string value that must be one of the
13636 following (case insensitive): @code{"static"}, @code{"dynamic"} or
13637 @code{"relocatable"} (which is a synonym for @code{"dynamic"}). If this
13638 attribute is not specified, the library is a static library, that is
13639 an archive of object files that can be potentially linked into a
13640 static executable. Otherwise, the library may be dynamic or
13641 relocatable, that is a library that is loaded only at the start of execution.
13642
13643 If you need to build both a static and a dynamic library, you should use two
13644 different object directories, since in some cases some extra code needs to
13645 be generated for the latter. For such cases, it is recommended to either use
13646 two different project files, or a single one which uses external variables
13647 to indicate what kind of library should be build.
13648
13649 The @code{Library_ALI_Dir} attribute may be specified to indicate the
13650 directory where the ALI files of the library will be copied. When it is
13651 not specified, the ALI files are copied to the directory specified in
13652 attribute @code{Library_Dir}. The directory specified by @code{Library_ALI_Dir}
13653 must be writable and different from the project's object directory and from
13654 any source directory in the project tree.
13655
13656 The @code{Library_Version} attribute has a string value whose interpretation
13657 is platform dependent. It has no effect on VMS and Windows. On Unix, it is
13658 used only for dynamic/relocatable libraries as the internal name of the
13659 library (the @code{"soname"}). If the library file name (built from the
13660 @code{Library_Name}) is different from the @code{Library_Version}, then the
13661 library file will be a symbolic link to the actual file whose name will be
13662 @code{Library_Version}.
13663
13664 Example (on Unix):
13665
13666 @smallexample @c projectfile
13667 @group
13668 project Plib is
13669
13670 Version := "1";
13671
13672 for Library_Dir use "lib_dir";
13673 for Library_Name use "dummy";
13674 for Library_Kind use "relocatable";
13675 for Library_Version use "libdummy.so." & Version;
13676
13677 end Plib;
13678 @end group
13679 @end smallexample
13680
13681 @noindent
13682 Directory @file{lib_dir} will contain the internal library file whose name
13683 will be @file{libdummy.so.1}, and @file{libdummy.so} will be a symbolic link to
13684 @file{libdummy.so.1}.
13685
13686 When @command{gnatmake} detects that a project file
13687 is a library project file, it will check all immediate sources of the project
13688 and rebuild the library if any of the sources have been recompiled.
13689
13690 Standard project files can import library project files. In such cases,
13691 the libraries will only be rebuilt if some of its sources are recompiled
13692 because they are in the closure of some other source in an importing project.
13693 Sources of the library project files that are not in such a closure will
13694 not be checked, unless the full library is checked, because one of its sources
13695 needs to be recompiled.
13696
13697 For instance, assume the project file @code{A} imports the library project file
13698 @code{L}. The immediate sources of A are @file{a1.adb}, @file{a2.ads} and
13699 @file{a2.adb}. The immediate sources of L are @file{l1.ads}, @file{l1.adb},
13700 @file{l2.ads}, @file{l2.adb}.
13701
13702 If @file{l1.adb} has been modified, then the library associated with @code{L}
13703 will be rebuilt when compiling all the immediate sources of @code{A} only
13704 if @file{a1.ads}, @file{a2.ads} or @file{a2.adb} includes a statement
13705 @code{"with L1;"}.
13706
13707 To be sure that all the sources in the library associated with @code{L} are
13708 up to date, and that all the sources of project @code{A} are also up to date,
13709 the following two commands needs to be used:
13710
13711 @smallexample
13712 gnatmake -Pl.gpr
13713 gnatmake -Pa.gpr
13714 @end smallexample
13715
13716 When a library is built or rebuilt, an attempt is made first to delete all
13717 files in the library directory.
13718 All @file{ALI} files will also be copied from the object directory to the
13719 library directory. To build executables, @command{gnatmake} will use the
13720 library rather than the individual object files.
13721
13722 @ifclear vms
13723 It is also possible to create library project files for third-party libraries
13724 that are precompiled and cannot be compiled locally thanks to the
13725 @code{externally_built} attribute. (See @ref{Installing a library}).
13726 @end ifclear
13727
13728 @c *******************************
13729 @c * Stand-alone Library Projects *
13730 @c *******************************
13731
13732 @node Stand-alone Library Projects
13733 @section Stand-alone Library Projects
13734
13735 @noindent
13736 A Stand-alone Library is a library that contains the necessary code to
13737 elaborate the Ada units that are included in the library. A Stand-alone
13738 Library is suitable to be used in an executable when the main is not
13739 in Ada. However, Stand-alone Libraries may also be used with an Ada main
13740 subprogram.
13741
13742 A Stand-alone Library Project is a Library Project where the library is
13743 a Stand-alone Library.
13744
13745 To be a Stand-alone Library Project, in addition to the two attributes
13746 that make a project a Library Project (@code{Library_Name} and
13747 @code{Library_Dir}, see @ref{Library Projects}), the attribute
13748 @code{Library_Interface} must be defined.
13749
13750 @smallexample @c projectfile
13751 @group
13752 for Library_Dir use "lib_dir";
13753 for Library_Name use "dummy";
13754 for Library_Interface use ("int1", "int1.child");
13755 @end group
13756 @end smallexample
13757
13758 Attribute @code{Library_Interface} has a nonempty string list value,
13759 each string in the list designating a unit contained in an immediate source
13760 of the project file.
13761
13762 When a Stand-alone Library is built, first the binder is invoked to build
13763 a package whose name depends on the library name
13764 (^b~dummy.ads/b^B$DUMMY.ADS/B^ in the example above).
13765 This binder-generated package includes initialization and
13766 finalization procedures whose
13767 names depend on the library name (dummyinit and dummyfinal in the example
13768 above). The object corresponding to this package is included in the library.
13769
13770 A dynamic or relocatable Stand-alone Library is automatically initialized
13771 if automatic initialization of Stand-alone Libraries is supported on the
13772 platform and if attribute @code{Library_Auto_Init} is not specified or
13773 is specified with the value "true". A static Stand-alone Library is never
13774 automatically initialized.
13775
13776 Single string attribute @code{Library_Auto_Init} may be specified with only
13777 two possible values: "false" or "true" (case-insensitive). Specifying
13778 "false" for attribute @code{Library_Auto_Init} will prevent automatic
13779 initialization of dynamic or relocatable libraries.
13780
13781 When a non-automatically initialized Stand-alone Library is used
13782 in an executable, its initialization procedure must be called before
13783 any service of the library is used.
13784 When the main subprogram is in Ada, it may mean that the initialization
13785 procedure has to be called during elaboration of another package.
13786
13787 For a Stand-Alone Library, only the @file{ALI} files of the Interface Units
13788 (those that are listed in attribute @code{Library_Interface}) are copied to
13789 the Library Directory. As a consequence, only the Interface Units may be
13790 imported from Ada units outside of the library. If other units are imported,
13791 the binding phase will fail.
13792
13793 When a Stand-Alone Library is bound, the switches that are specified in
13794 the attribute @code{Default_Switches ("Ada")} in package @code{Binder} are
13795 used in the call to @command{gnatbind}.
13796
13797 The string list attribute @code{Library_Options} may be used to specified
13798 additional switches to the call to @command{gcc} to link the library.
13799
13800 The attribute @code{Library_Src_Dir}, may be specified for a
13801 Stand-Alone Library. @code{Library_Src_Dir} is a simple attribute that has a
13802 single string value. Its value must be the path (absolute or relative to the
13803 project directory) of an existing directory. This directory cannot be the
13804 object directory or one of the source directories, but it can be the same as
13805 the library directory. The sources of the Interface
13806 Units of the library, necessary to an Ada client of the library, will be
13807 copied to the designated directory, called Interface Copy directory.
13808 These sources includes the specs of the Interface Units, but they may also
13809 include bodies and subunits, when pragmas @code{Inline} or @code{Inline_Always}
13810 are used, or when there is a generic units in the spec. Before the sources
13811 are copied to the Interface Copy directory, an attempt is made to delete all
13812 files in the Interface Copy directory.
13813
13814 @c *************************************
13815 @c * Switches Related to Project Files *
13816 @c *************************************
13817 @node Switches Related to Project Files
13818 @section Switches Related to Project Files
13819
13820 @noindent
13821 The following switches are used by GNAT tools that support project files:
13822
13823 @table @option
13824
13825 @item ^-P^/PROJECT_FILE=^@var{project}
13826 @cindex @option{^-P^/PROJECT_FILE^} (any project-aware tool)
13827 Indicates the name of a project file. This project file will be parsed with
13828 the verbosity indicated by @option{^-vP^MESSAGE_PROJECT_FILES=^@emph{x}},
13829 if any, and using the external references indicated
13830 by @option{^-X^/EXTERNAL_REFERENCE^} switches, if any.
13831 @ifclear vms
13832 There may zero, one or more spaces between @option{-P} and @var{project}.
13833 @end ifclear
13834
13835 @noindent
13836 There must be only one @option{^-P^/PROJECT_FILE^} switch on the command line.
13837
13838 @noindent
13839 Since the Project Manager parses the project file only after all the switches
13840 on the command line are checked, the order of the switches
13841 @option{^-P^/PROJECT_FILE^},
13842 @option{^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}}
13843 or @option{^-X^/EXTERNAL_REFERENCE^} is not significant.
13844
13845 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
13846 @cindex @option{^-X^/EXTERNAL_REFERENCE^} (any project-aware tool)
13847 Indicates that external variable @var{name} has the value @var{value}.
13848 The Project Manager will use this value for occurrences of
13849 @code{external(name)} when parsing the project file.
13850
13851 @ifclear vms
13852 @noindent
13853 If @var{name} or @var{value} includes a space, then @var{name=value} should be
13854 put between quotes.
13855 @smallexample
13856 -XOS=NT
13857 -X"user=John Doe"
13858 @end smallexample
13859 @end ifclear
13860
13861 @noindent
13862 Several @option{^-X^/EXTERNAL_REFERENCE^} switches can be used simultaneously.
13863 If several @option{^-X^/EXTERNAL_REFERENCE^} switches specify the same
13864 @var{name}, only the last one is used.
13865
13866 @noindent
13867 An external variable specified with a @option{^-X^/EXTERNAL_REFERENCE^} switch
13868 takes precedence over the value of the same name in the environment.
13869
13870 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
13871 @cindex @option{^-vP^/MESSAGES_PROJECT_FILE^} (any project-aware tool)
13872 Indicates the verbosity of the parsing of GNAT project files.
13873
13874 @ifclear vms
13875 @option{-vP0} means Default;
13876 @option{-vP1} means Medium;
13877 @option{-vP2} means High.
13878 @end ifclear
13879
13880 @ifset vms
13881 There are three possible options for this qualifier: DEFAULT, MEDIUM and
13882 HIGH.
13883 @end ifset
13884
13885 @noindent
13886 The default is ^Default^DEFAULT^: no output for syntactically correct
13887 project files.
13888 @noindent
13889 If several @option{^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}} switches are present,
13890 only the last one is used.
13891
13892 @item ^-aP^/ADD_PROJECT_SEARCH_DIR=^<dir>
13893 @cindex @option{^-aP^/ADD_PROJECT_SEARCH_DIR=^} (any project-aware tool)
13894 Add directory <dir> at the beginning of the project search path, in order,
13895 after the current working directory.
13896
13897 @ifclear vms
13898 @item -eL
13899 @cindex @option{-eL} (any project-aware tool)
13900 Follow all symbolic links when processing project files.
13901 @end ifclear
13902
13903 @item ^--subdirs^/SUBDIRS^=<subdir>
13904 @cindex @option{^--subdirs^/SUBDIRS^=} (gnatmake and gnatclean)
13905 This switch is recognized by gnatmake and gnatclean. It indicate that the real
13906 directories (except the source directories) are the subdirectories <subdir>
13907 of the directories specified in the project files. This applies in particular
13908 to object directories, library directories and exec directories. If the
13909 subdirectories do not exist, they are created automatically.
13910
13911 @end table
13912
13913 @c **********************************
13914 @c * Tools Supporting Project Files *
13915 @c **********************************
13916
13917 @node Tools Supporting Project Files
13918 @section Tools Supporting Project Files
13919
13920 @menu
13921 * gnatmake and Project Files::
13922 * The GNAT Driver and Project Files::
13923 @end menu
13924
13925 @node gnatmake and Project Files
13926 @subsection gnatmake and Project Files
13927
13928 @noindent
13929 This section covers several topics related to @command{gnatmake} and
13930 project files: defining ^switches^switches^ for @command{gnatmake}
13931 and for the tools that it invokes; specifying configuration pragmas;
13932 the use of the @code{Main} attribute; building and rebuilding library project
13933 files.
13934
13935 @menu
13936 * ^Switches^Switches^ and Project Files::
13937 * Specifying Configuration Pragmas::
13938 * Project Files and Main Subprograms::
13939 * Library Project Files::
13940 @end menu
13941
13942 @node ^Switches^Switches^ and Project Files
13943 @subsubsection ^Switches^Switches^ and Project Files
13944
13945 @ifset vms
13946 It is not currently possible to specify VMS style qualifiers in the project
13947 files; only Unix style ^switches^switches^ may be specified.
13948 @end ifset
13949
13950 @noindent
13951 For each of the packages @code{Builder}, @code{Compiler}, @code{Binder}, and
13952 @code{Linker}, you can specify a @code{^Default_Switches^Default_Switches^}
13953 attribute, a @code{^Switches^Switches^} attribute, or both;
13954 as their names imply, these ^switch^switch^-related
13955 attributes affect the ^switches^switches^ that are used for each of these GNAT
13956 components when
13957 @command{gnatmake} is invoked. As will be explained below, these
13958 component-specific ^switches^switches^ precede
13959 the ^switches^switches^ provided on the @command{gnatmake} command line.
13960
13961 The @code{^Default_Switches^Default_Switches^} attribute is an associative
13962 array indexed by language name (case insensitive) whose value is a string list.
13963 For example:
13964
13965 @smallexample @c projectfile
13966 @group
13967 package Compiler is
13968 for ^Default_Switches^Default_Switches^ ("Ada")
13969 use ("^-gnaty^-gnaty^",
13970 "^-v^-v^");
13971 end Compiler;
13972 @end group
13973 @end smallexample
13974
13975 @noindent
13976 The @code{^Switches^Switches^} attribute is also an associative array,
13977 indexed by a file name (which may or may not be case sensitive, depending
13978 on the operating system) whose value is a string list. For example:
13979
13980 @smallexample @c projectfile
13981 @group
13982 package Builder is
13983 for ^Switches^Switches^ ("main1.adb")
13984 use ("^-O2^-O2^");
13985 for ^Switches^Switches^ ("main2.adb")
13986 use ("^-g^-g^");
13987 end Builder;
13988 @end group
13989 @end smallexample
13990
13991 @noindent
13992 For the @code{Builder} package, the file names must designate source files
13993 for main subprograms. For the @code{Binder} and @code{Linker} packages, the
13994 file names must designate @file{ALI} or source files for main subprograms.
13995 In each case just the file name without an explicit extension is acceptable.
13996
13997 For each tool used in a program build (@command{gnatmake}, the compiler, the
13998 binder, and the linker), the corresponding package @dfn{contributes} a set of
13999 ^switches^switches^ for each file on which the tool is invoked, based on the
14000 ^switch^switch^-related attributes defined in the package.
14001 In particular, the ^switches^switches^
14002 that each of these packages contributes for a given file @var{f} comprise:
14003
14004 @itemize @bullet
14005 @item
14006 the value of attribute @code{^Switches^Switches^ (@var{f})},
14007 if it is specified in the package for the given file,
14008 @item
14009 otherwise, the value of @code{^Default_Switches^Default_Switches^ ("Ada")},
14010 if it is specified in the package.
14011 @end itemize
14012
14013 @noindent
14014 If neither of these attributes is defined in the package, then the package does
14015 not contribute any ^switches^switches^ for the given file.
14016
14017 When @command{gnatmake} is invoked on a file, the ^switches^switches^ comprise
14018 two sets, in the following order: those contributed for the file
14019 by the @code{Builder} package;
14020 and the switches passed on the command line.
14021
14022 When @command{gnatmake} invokes a tool (compiler, binder, linker) on a file,
14023 the ^switches^switches^ passed to the tool comprise three sets,
14024 in the following order:
14025
14026 @enumerate
14027 @item
14028 the applicable ^switches^switches^ contributed for the file
14029 by the @code{Builder} package in the project file supplied on the command line;
14030
14031 @item
14032 those contributed for the file by the package (in the relevant project file --
14033 see below) corresponding to the tool; and
14034
14035 @item
14036 the applicable switches passed on the command line.
14037 @end enumerate
14038
14039 @noindent
14040 The term @emph{applicable ^switches^switches^} reflects the fact that
14041 @command{gnatmake} ^switches^switches^ may or may not be passed to individual
14042 tools, depending on the individual ^switch^switch^.
14043
14044 @command{gnatmake} may invoke the compiler on source files from different
14045 projects. The Project Manager will use the appropriate project file to
14046 determine the @code{Compiler} package for each source file being compiled.
14047 Likewise for the @code{Binder} and @code{Linker} packages.
14048
14049 As an example, consider the following package in a project file:
14050
14051 @smallexample @c projectfile
14052 @group
14053 project Proj1 is
14054 package Compiler is
14055 for ^Default_Switches^Default_Switches^ ("Ada")
14056 use ("^-g^-g^");
14057 for ^Switches^Switches^ ("a.adb")
14058 use ("^-O1^-O1^");
14059 for ^Switches^Switches^ ("b.adb")
14060 use ("^-O2^-O2^",
14061 "^-gnaty^-gnaty^");
14062 end Compiler;
14063 end Proj1;
14064 @end group
14065 @end smallexample
14066
14067 @noindent
14068 If @command{gnatmake} is invoked with this project file, and it needs to
14069 compile, say, the files @file{a.adb}, @file{b.adb}, and @file{c.adb}, then
14070 @file{a.adb} will be compiled with the ^switch^switch^
14071 @option{^-O1^-O1^},
14072 @file{b.adb} with ^switches^switches^
14073 @option{^-O2^-O2^}
14074 and @option{^-gnaty^-gnaty^},
14075 and @file{c.adb} with @option{^-g^-g^}.
14076
14077 The following example illustrates the ordering of the ^switches^switches^
14078 contributed by different packages:
14079
14080 @smallexample @c projectfile
14081 @group
14082 project Proj2 is
14083 package Builder is
14084 for ^Switches^Switches^ ("main.adb")
14085 use ("^-g^-g^",
14086 "^-O1^-)1^",
14087 "^-f^-f^");
14088 end Builder;
14089 @end group
14090
14091 @group
14092 package Compiler is
14093 for ^Switches^Switches^ ("main.adb")
14094 use ("^-O2^-O2^");
14095 end Compiler;
14096 end Proj2;
14097 @end group
14098 @end smallexample
14099
14100 @noindent
14101 If you issue the command:
14102
14103 @smallexample
14104 gnatmake ^-Pproj2^/PROJECT_FILE=PROJ2^ -O0 main
14105 @end smallexample
14106
14107 @noindent
14108 then the compiler will be invoked on @file{main.adb} with the following
14109 sequence of ^switches^switches^
14110
14111 @smallexample
14112 ^-g -O1 -O2 -O0^-g -O1 -O2 -O0^
14113 @end smallexample
14114
14115 with the last @option{^-O^-O^}
14116 ^switch^switch^ having precedence over the earlier ones;
14117 several other ^switches^switches^
14118 (such as @option{^-c^-c^}) are added implicitly.
14119
14120 The ^switches^switches^
14121 @option{^-g^-g^}
14122 and @option{^-O1^-O1^} are contributed by package
14123 @code{Builder}, @option{^-O2^-O2^} is contributed
14124 by the package @code{Compiler}
14125 and @option{^-O0^-O0^} comes from the command line.
14126
14127 The @option{^-g^-g^}
14128 ^switch^switch^ will also be passed in the invocation of
14129 @command{Gnatlink.}
14130
14131 A final example illustrates switch contributions from packages in different
14132 project files:
14133
14134 @smallexample @c projectfile
14135 @group
14136 project Proj3 is
14137 for Source_Files use ("pack.ads", "pack.adb");
14138 package Compiler is
14139 for ^Default_Switches^Default_Switches^ ("Ada")
14140 use ("^-gnata^-gnata^");
14141 end Compiler;
14142 end Proj3;
14143 @end group
14144
14145 @group
14146 with "Proj3";
14147 project Proj4 is
14148 for Source_Files use ("foo_main.adb", "bar_main.adb");
14149 package Builder is
14150 for ^Switches^Switches^ ("foo_main.adb")
14151 use ("^-s^-s^",
14152 "^-g^-g^");
14153 end Builder;
14154 end Proj4;
14155 @end group
14156
14157 @group
14158 -- Ada source file:
14159 with Pack;
14160 procedure Foo_Main is
14161 @dots{}
14162 end Foo_Main;
14163 @end group
14164 @end smallexample
14165
14166 If the command is
14167 @smallexample
14168 gnatmake ^-PProj4^/PROJECT_FILE=PROJ4^ foo_main.adb -cargs -gnato
14169 @end smallexample
14170
14171 @noindent
14172 then the ^switches^switches^ passed to the compiler for @file{foo_main.adb} are
14173 @option{^-g^-g^} (contributed by the package @code{Proj4.Builder}) and
14174 @option{^-gnato^-gnato^} (passed on the command line).
14175 When the imported package @code{Pack} is compiled, the ^switches^switches^ used
14176 are @option{^-g^-g^} from @code{Proj4.Builder},
14177 @option{^-gnata^-gnata^} (contributed from package @code{Proj3.Compiler},
14178 and @option{^-gnato^-gnato^} from the command line.
14179
14180 @noindent
14181 When using @command{gnatmake} with project files, some ^switches^switches^ or
14182 arguments may be expressed as relative paths. As the working directory where
14183 compilation occurs may change, these relative paths are converted to absolute
14184 paths. For the ^switches^switches^ found in a project file, the relative paths
14185 are relative to the project file directory, for the switches on the command
14186 line, they are relative to the directory where @command{gnatmake} is invoked.
14187 The ^switches^switches^ for which this occurs are:
14188 ^-I^-I^,
14189 ^-A^-A^,
14190 ^-L^-L^,
14191 ^-aO^-aO^,
14192 ^-aL^-aL^,
14193 ^-aI^-aI^, as well as all arguments that are not switches (arguments to
14194 ^switch^switch^
14195 ^-o^-o^, object files specified in package @code{Linker} or after
14196 -largs on the command line). The exception to this rule is the ^switch^switch^
14197 ^--RTS=^--RTS=^ for which a relative path argument is never converted.
14198
14199 @node Specifying Configuration Pragmas
14200 @subsubsection Specifying Configuration Pragmas
14201
14202 When using @command{gnatmake} with project files, if there exists a file
14203 @file{gnat.adc} that contains configuration pragmas, this file will be
14204 ignored.
14205
14206 Configuration pragmas can be defined by means of the following attributes in
14207 project files: @code{Global_Configuration_Pragmas} in package @code{Builder}
14208 and @code{Local_Configuration_Pragmas} in package @code{Compiler}.
14209
14210 Both these attributes are single string attributes. Their values is the path
14211 name of a file containing configuration pragmas. If a path name is relative,
14212 then it is relative to the project directory of the project file where the
14213 attribute is defined.
14214
14215 When compiling a source, the configuration pragmas used are, in order,
14216 those listed in the file designated by attribute
14217 @code{Global_Configuration_Pragmas} in package @code{Builder} of the main
14218 project file, if it is specified, and those listed in the file designated by
14219 attribute @code{Local_Configuration_Pragmas} in package @code{Compiler} of
14220 the project file of the source, if it exists.
14221
14222 @node Project Files and Main Subprograms
14223 @subsubsection Project Files and Main Subprograms
14224
14225 @noindent
14226 When using a project file, you can invoke @command{gnatmake}
14227 with one or several main subprograms, by specifying their source files on the
14228 command line.
14229
14230 @smallexample
14231 gnatmake ^-P^/PROJECT_FILE=^prj main1 main2 main3
14232 @end smallexample
14233
14234 @noindent
14235 Each of these needs to be a source file of the same project, except
14236 when the switch ^-u^/UNIQUE^ is used.
14237
14238 @noindent
14239 When ^-u^/UNIQUE^ is not used, all the mains need to be sources of the
14240 same project, one of the project in the tree rooted at the project specified
14241 on the command line. The package @code{Builder} of this common project, the
14242 "main project" is the one that is considered by @command{gnatmake}.
14243
14244 @noindent
14245 When ^-u^/UNIQUE^ is used, the specified source files may be in projects
14246 imported directly or indirectly by the project specified on the command line.
14247 Note that if such a source file is not part of the project specified on the
14248 command line, the ^switches^switches^ found in package @code{Builder} of the
14249 project specified on the command line, if any, that are transmitted
14250 to the compiler will still be used, not those found in the project file of
14251 the source file.
14252
14253 @noindent
14254 When using a project file, you can also invoke @command{gnatmake} without
14255 explicitly specifying any main, and the effect depends on whether you have
14256 defined the @code{Main} attribute. This attribute has a string list value,
14257 where each element in the list is the name of a source file (the file
14258 extension is optional) that contains a unit that can be a main subprogram.
14259
14260 If the @code{Main} attribute is defined in a project file as a non-empty
14261 string list and the switch @option{^-u^/UNIQUE^} is not used on the command
14262 line, then invoking @command{gnatmake} with this project file but without any
14263 main on the command line is equivalent to invoking @command{gnatmake} with all
14264 the file names in the @code{Main} attribute on the command line.
14265
14266 Example:
14267 @smallexample @c projectfile
14268 @group
14269 project Prj is
14270 for Main use ("main1", "main2", "main3");
14271 end Prj;
14272 @end group
14273 @end smallexample
14274
14275 @noindent
14276 With this project file, @code{"gnatmake ^-Pprj^/PROJECT_FILE=PRJ^"}
14277 is equivalent to
14278 @code{"gnatmake ^-Pprj^/PROJECT_FILE=PRJ^ main1 main2 main3"}.
14279
14280 When the project attribute @code{Main} is not specified, or is specified
14281 as an empty string list, or when the switch @option{-u} is used on the command
14282 line, then invoking @command{gnatmake} with no main on the command line will
14283 result in all immediate sources of the project file being checked, and
14284 potentially recompiled. Depending on the presence of the switch @option{-u},
14285 sources from other project files on which the immediate sources of the main
14286 project file depend are also checked and potentially recompiled. In other
14287 words, the @option{-u} switch is applied to all of the immediate sources of the
14288 main project file.
14289
14290 When no main is specified on the command line and attribute @code{Main} exists
14291 and includes several mains, or when several mains are specified on the
14292 command line, the default ^switches^switches^ in package @code{Builder} will
14293 be used for all mains, even if there are specific ^switches^switches^
14294 specified for one or several mains.
14295
14296 But the ^switches^switches^ from package @code{Binder} or @code{Linker} will be
14297 the specific ^switches^switches^ for each main, if they are specified.
14298
14299 @node Library Project Files
14300 @subsubsection Library Project Files
14301
14302 @noindent
14303 When @command{gnatmake} is invoked with a main project file that is a library
14304 project file, it is not allowed to specify one or more mains on the command
14305 line.
14306
14307 @noindent
14308 When a library project file is specified, switches ^-b^/ACTION=BIND^ and
14309 ^-l^/ACTION=LINK^ have special meanings.
14310
14311 @itemize @bullet
14312 @item ^-b^/ACTION=BIND^ is only allowed for stand-alone libraries. It indicates
14313 to @command{gnatmake} that @command{gnatbind} should be invoked for the
14314 library.
14315
14316 @item ^-l^/ACTION=LINK^ may be used for all library projects. It indicates
14317 to @command{gnatmake} that the binder generated file should be compiled
14318 (in the case of a stand-alone library) and that the library should be built.
14319
14320 @end itemize
14321
14322 @node The GNAT Driver and Project Files
14323 @subsection The GNAT Driver and Project Files
14324
14325 @noindent
14326 A number of GNAT tools, other than @command{^gnatmake^gnatmake^}
14327 can benefit from project files:
14328 @command{^gnatbind^gnatbind^},
14329 @command{^gnatcheck^gnatcheck^}),
14330 @command{^gnatclean^gnatclean^}),
14331 @command{^gnatelim^gnatelim^},
14332 @command{^gnatfind^gnatfind^},
14333 @command{^gnatlink^gnatlink^},
14334 @command{^gnatls^gnatls^},
14335 @command{^gnatmetric^gnatmetric^},
14336 @command{^gnatpp^gnatpp^},
14337 @command{^gnatstub^gnatstub^},
14338 and @command{^gnatxref^gnatxref^}. However, none of these tools can be invoked
14339 directly with a project file switch (@option{^-P^/PROJECT_FILE=^}).
14340 They must be invoked through the @command{gnat} driver.
14341
14342 The @command{gnat} driver is a wrapper that accepts a number of commands and
14343 calls the corresponding tool. It was designed initially for VMS platforms (to
14344 convert VMS qualifiers to Unix-style switches), but it is now available on all
14345 GNAT platforms.
14346
14347 On non-VMS platforms, the @command{gnat} driver accepts the following commands
14348 (case insensitive):
14349
14350 @itemize @bullet
14351 @item
14352 BIND to invoke @command{^gnatbind^gnatbind^}
14353 @item
14354 CHOP to invoke @command{^gnatchop^gnatchop^}
14355 @item
14356 CLEAN to invoke @command{^gnatclean^gnatclean^}
14357 @item
14358 COMP or COMPILE to invoke the compiler
14359 @item
14360 ELIM to invoke @command{^gnatelim^gnatelim^}
14361 @item
14362 FIND to invoke @command{^gnatfind^gnatfind^}
14363 @item
14364 KR or KRUNCH to invoke @command{^gnatkr^gnatkr^}
14365 @item
14366 LINK to invoke @command{^gnatlink^gnatlink^}
14367 @item
14368 LS or LIST to invoke @command{^gnatls^gnatls^}
14369 @item
14370 MAKE to invoke @command{^gnatmake^gnatmake^}
14371 @item
14372 NAME to invoke @command{^gnatname^gnatname^}
14373 @item
14374 PREP or PREPROCESS to invoke @command{^gnatprep^gnatprep^}
14375 @item
14376 PP or PRETTY to invoke @command{^gnatpp^gnatpp^}
14377 @item
14378 METRIC to invoke @command{^gnatmetric^gnatmetric^}
14379 @item
14380 STUB to invoke @command{^gnatstub^gnatstub^}
14381 @item
14382 XREF to invoke @command{^gnatxref^gnatxref^}
14383 @end itemize
14384
14385 @noindent
14386 (note that the compiler is invoked using the command
14387 @command{^gnatmake -f -u -c^gnatmake -f -u -c^}).
14388
14389 @noindent
14390 On non-VMS platforms, between @command{gnat} and the command, two
14391 special switches may be used:
14392
14393 @itemize @bullet
14394 @item
14395 @command{-v} to display the invocation of the tool.
14396 @item
14397 @command{-dn} to prevent the @command{gnat} driver from removing
14398 the temporary files it has created. These temporary files are
14399 configuration files and temporary file list files.
14400 @end itemize
14401
14402 @noindent
14403 The command may be followed by switches and arguments for the invoked
14404 tool.
14405
14406 @smallexample
14407 gnat bind -C main.ali
14408 gnat ls -a main
14409 gnat chop foo.txt
14410 @end smallexample
14411
14412 @noindent
14413 Switches may also be put in text files, one switch per line, and the text
14414 files may be specified with their path name preceded by '@@'.
14415
14416 @smallexample
14417 gnat bind @@args.txt main.ali
14418 @end smallexample
14419
14420 @noindent
14421 In addition, for commands BIND, COMP or COMPILE, FIND, ELIM, LS or LIST, LINK,
14422 METRIC, PP or PRETTY, STUB and XREF, the project file related switches
14423 (@option{^-P^/PROJECT_FILE^},
14424 @option{^-X^/EXTERNAL_REFERENCE^} and
14425 @option{^-vP^/MESSAGES_PROJECT_FILE=^x}) may be used in addition to
14426 the switches of the invoking tool.
14427
14428 @noindent
14429 When GNAT PP or GNAT PRETTY is used with a project file, but with no source
14430 specified on the command line, it invokes @command{^gnatpp^gnatpp^} with all
14431 the immediate sources of the specified project file.
14432
14433 @noindent
14434 When GNAT METRIC is used with a project file, but with no source
14435 specified on the command line, it invokes @command{^gnatmetric^gnatmetric^}
14436 with all the immediate sources of the specified project file and with
14437 @option{^-d^/DIRECTORY^} with the parameter pointing to the object directory
14438 of the project.
14439
14440 @noindent
14441 In addition, when GNAT PP, GNAT PRETTY or GNAT METRIC is used with
14442 a project file, no source is specified on the command line and
14443 switch ^-U^/ALL_PROJECTS^ is specified on the command line, then
14444 the underlying tool (^gnatpp^gnatpp^ or
14445 ^gnatmetric^gnatmetric^) is invoked for all sources of all projects,
14446 not only for the immediate sources of the main project.
14447 @ifclear vms
14448 (-U stands for Universal or Union of the project files of the project tree)
14449 @end ifclear
14450
14451 @noindent
14452 For each of the following commands, there is optionally a corresponding
14453 package in the main project.
14454
14455 @itemize @bullet
14456 @item
14457 package @code{Binder} for command BIND (invoking @code{^gnatbind^gnatbind^})
14458
14459 @item
14460 package @code{Check} for command CHECK (invoking
14461 @code{^gnatcheck^gnatcheck^})
14462
14463 @item
14464 package @code{Compiler} for command COMP or COMPILE (invoking the compiler)
14465
14466 @item
14467 package @code{Cross_Reference} for command XREF (invoking
14468 @code{^gnatxref^gnatxref^})
14469
14470 @item
14471 package @code{Eliminate} for command ELIM (invoking
14472 @code{^gnatelim^gnatelim^})
14473
14474 @item
14475 package @code{Finder} for command FIND (invoking @code{^gnatfind^gnatfind^})
14476
14477 @item
14478 package @code{Gnatls} for command LS or LIST (invoking @code{^gnatls^gnatls^})
14479
14480 @item
14481 package @code{Gnatstub} for command STUB
14482 (invoking @code{^gnatstub^gnatstub^})
14483
14484 @item
14485 package @code{Linker} for command LINK (invoking @code{^gnatlink^gnatlink^})
14486
14487 @item
14488 package @code{Metrics} for command METRIC
14489 (invoking @code{^gnatmetric^gnatmetric^})
14490
14491 @item
14492 package @code{Pretty_Printer} for command PP or PRETTY
14493 (invoking @code{^gnatpp^gnatpp^})
14494
14495 @end itemize
14496
14497 @noindent
14498 Package @code{Gnatls} has a unique attribute @code{^Switches^Switches^},
14499 a simple variable with a string list value. It contains ^switches^switches^
14500 for the invocation of @code{^gnatls^gnatls^}.
14501
14502 @smallexample @c projectfile
14503 @group
14504 project Proj1 is
14505 package gnatls is
14506 for ^Switches^Switches^
14507 use ("^-a^-a^",
14508 "^-v^-v^");
14509 end gnatls;
14510 end Proj1;
14511 @end group
14512 @end smallexample
14513
14514 @noindent
14515 All other packages have two attribute @code{^Switches^Switches^} and
14516 @code{^Default_Switches^Default_Switches^}.
14517
14518 @noindent
14519 @code{^Switches^Switches^} is an associative array attribute, indexed by the
14520 source file name, that has a string list value: the ^switches^switches^ to be
14521 used when the tool corresponding to the package is invoked for the specific
14522 source file.
14523
14524 @noindent
14525 @code{^Default_Switches^Default_Switches^} is an associative array attribute,
14526 indexed by the programming language that has a string list value.
14527 @code{^Default_Switches^Default_Switches^ ("Ada")} contains the
14528 ^switches^switches^ for the invocation of the tool corresponding
14529 to the package, except if a specific @code{^Switches^Switches^} attribute
14530 is specified for the source file.
14531
14532 @smallexample @c projectfile
14533 @group
14534 project Proj is
14535
14536 for Source_Dirs use ("./**");
14537
14538 package gnatls is
14539 for ^Switches^Switches^ use
14540 ("^-a^-a^",
14541 "^-v^-v^");
14542 end gnatls;
14543 @end group
14544 @group
14545
14546 package Compiler is
14547 for ^Default_Switches^Default_Switches^ ("Ada")
14548 use ("^-gnatv^-gnatv^",
14549 "^-gnatwa^-gnatwa^");
14550 end Binder;
14551 @end group
14552 @group
14553
14554 package Binder is
14555 for ^Default_Switches^Default_Switches^ ("Ada")
14556 use ("^-C^-C^",
14557 "^-e^-e^");
14558 end Binder;
14559 @end group
14560 @group
14561
14562 package Linker is
14563 for ^Default_Switches^Default_Switches^ ("Ada")
14564 use ("^-C^-C^");
14565 for ^Switches^Switches^ ("main.adb")
14566 use ("^-C^-C^",
14567 "^-v^-v^",
14568 "^-v^-v^");
14569 end Linker;
14570 @end group
14571 @group
14572
14573 package Finder is
14574 for ^Default_Switches^Default_Switches^ ("Ada")
14575 use ("^-a^-a^",
14576 "^-f^-f^");
14577 end Finder;
14578 @end group
14579 @group
14580
14581 package Cross_Reference is
14582 for ^Default_Switches^Default_Switches^ ("Ada")
14583 use ("^-a^-a^",
14584 "^-f^-f^",
14585 "^-d^-d^",
14586 "^-u^-u^");
14587 end Cross_Reference;
14588 end Proj;
14589 @end group
14590 @end smallexample
14591
14592 @noindent
14593 With the above project file, commands such as
14594
14595 @smallexample
14596 ^gnat comp -Pproj main^GNAT COMP /PROJECT_FILE=PROJ MAIN^
14597 ^gnat ls -Pproj main^GNAT LIST /PROJECT_FILE=PROJ MAIN^
14598 ^gnat xref -Pproj main^GNAT XREF /PROJECT_FILE=PROJ MAIN^
14599 ^gnat bind -Pproj main.ali^GNAT BIND /PROJECT_FILE=PROJ MAIN.ALI^
14600 ^gnat link -Pproj main.ali^GNAT LINK /PROJECT_FILE=PROJ MAIN.ALI^
14601 @end smallexample
14602
14603 @noindent
14604 will set up the environment properly and invoke the tool with the switches
14605 found in the package corresponding to the tool:
14606 @code{^Default_Switches^Default_Switches^ ("Ada")} for all tools,
14607 except @code{^Switches^Switches^ ("main.adb")}
14608 for @code{^gnatlink^gnatlink^}.
14609 It is also possible to invoke some of the tools,
14610 @code{^gnatcheck^gnatcheck^}),
14611 @code{^gnatmetric^gnatmetric^}),
14612 and @code{^gnatpp^gnatpp^})
14613 on a set of project units thanks to the combination of the switches
14614 @option{-P}, @option{-U} and possibly the main unit when one is interested
14615 in its closure. For instance,
14616 @smallexample
14617 gnat metric -Pproj
14618 @end smallexample
14619 will compute the metrics for all the immediate units of project
14620 @code{proj}.
14621 @smallexample
14622 gnat metric -Pproj -U
14623 @end smallexample
14624 will compute the metrics for all the units of the closure of projects
14625 rooted at @code{proj}.
14626 @smallexample
14627 gnat metric -Pproj -U main_unit
14628 @end smallexample
14629 will compute the metrics for the closure of units rooted at
14630 @code{main_unit}. This last possibility relies implicitly
14631 on @command{gnatbind}'s option @option{-R}.
14632
14633 @c **********************
14634 @node An Extended Example
14635 @section An Extended Example
14636
14637 @noindent
14638 Suppose that we have two programs, @var{prog1} and @var{prog2},
14639 whose sources are in corresponding directories. We would like
14640 to build them with a single @command{gnatmake} command, and we want to place
14641 their object files into @file{build} subdirectories of the source directories.
14642 Furthermore, we want to have to have two separate subdirectories
14643 in @file{build} -- @file{release} and @file{debug} -- which will contain
14644 the object files compiled with different set of compilation flags.
14645
14646 In other words, we have the following structure:
14647
14648 @smallexample
14649 @group
14650 main
14651 |- prog1
14652 | |- build
14653 | | debug
14654 | | release
14655 |- prog2
14656 |- build
14657 | debug
14658 | release
14659 @end group
14660 @end smallexample
14661
14662 @noindent
14663 Here are the project files that we must place in a directory @file{main}
14664 to maintain this structure:
14665
14666 @enumerate
14667
14668 @item We create a @code{Common} project with a package @code{Compiler} that
14669 specifies the compilation ^switches^switches^:
14670
14671 @smallexample
14672 File "common.gpr":
14673 @group
14674 @b{project} Common @b{is}
14675
14676 @b{for} Source_Dirs @b{use} (); -- No source files
14677 @end group
14678
14679 @group
14680 @b{type} Build_Type @b{is} ("release", "debug");
14681 Build : Build_Type := External ("BUILD", "debug");
14682 @end group
14683 @group
14684 @b{package} Compiler @b{is}
14685 @b{case} Build @b{is}
14686 @b{when} "release" =>
14687 @b{for} ^Default_Switches^Default_Switches^ ("Ada")
14688 @b{use} ("^-O2^-O2^");
14689 @b{when} "debug" =>
14690 @b{for} ^Default_Switches^Default_Switches^ ("Ada")
14691 @b{use} ("^-g^-g^");
14692 @b{end case};
14693 @b{end} Compiler;
14694
14695 @b{end} Common;
14696 @end group
14697 @end smallexample
14698
14699 @item We create separate projects for the two programs:
14700
14701 @smallexample
14702 @group
14703 File "prog1.gpr":
14704
14705 @b{with} "common";
14706 @b{project} Prog1 @b{is}
14707
14708 @b{for} Source_Dirs @b{use} ("prog1");
14709 @b{for} Object_Dir @b{use} "prog1/build/" & Common.Build;
14710
14711 @b{package} Compiler @b{renames} Common.Compiler;
14712
14713 @b{end} Prog1;
14714 @end group
14715 @end smallexample
14716
14717 @smallexample
14718 @group
14719 File "prog2.gpr":
14720
14721 @b{with} "common";
14722 @b{project} Prog2 @b{is}
14723
14724 @b{for} Source_Dirs @b{use} ("prog2");
14725 @b{for} Object_Dir @b{use} "prog2/build/" & Common.Build;
14726
14727 @b{package} Compiler @b{renames} Common.Compiler;
14728
14729 @end group
14730 @b{end} Prog2;
14731 @end smallexample
14732
14733 @item We create a wrapping project @code{Main}:
14734
14735 @smallexample
14736 @group
14737 File "main.gpr":
14738
14739 @b{with} "common";
14740 @b{with} "prog1";
14741 @b{with} "prog2";
14742 @b{project} Main @b{is}
14743
14744 @b{package} Compiler @b{renames} Common.Compiler;
14745
14746 @b{end} Main;
14747 @end group
14748 @end smallexample
14749
14750 @item Finally we need to create a dummy procedure that @code{with}s (either
14751 explicitly or implicitly) all the sources of our two programs.
14752
14753 @end enumerate
14754
14755 @noindent
14756 Now we can build the programs using the command
14757
14758 @smallexample
14759 gnatmake ^-P^/PROJECT_FILE=^main dummy
14760 @end smallexample
14761
14762 @noindent
14763 for the Debug mode, or
14764
14765 @ifclear vms
14766 @smallexample
14767 gnatmake -Pmain -XBUILD=release
14768 @end smallexample
14769 @end ifclear
14770
14771 @ifset vms
14772 @smallexample
14773 GNAT MAKE /PROJECT_FILE=main /EXTERNAL_REFERENCE=BUILD=release
14774 @end smallexample
14775 @end ifset
14776
14777 @noindent
14778 for the Release mode.
14779
14780 @c ********************************
14781 @c * Project File Complete Syntax *
14782 @c ********************************
14783
14784 @node Project File Complete Syntax
14785 @section Project File Complete Syntax
14786
14787 @smallexample
14788 project ::=
14789 context_clause project_declaration
14790
14791 context_clause ::=
14792 @{with_clause@}
14793
14794 with_clause ::=
14795 @b{with} path_name @{ , path_name @} ;
14796
14797 path_name ::=
14798 string_literal
14799
14800 project_declaration ::=
14801 simple_project_declaration | project_extension
14802
14803 simple_project_declaration ::=
14804 @b{project} <project_>simple_name @b{is}
14805 @{declarative_item@}
14806 @b{end} <project_>simple_name;
14807
14808 project_extension ::=
14809 @b{project} <project_>simple_name @b{extends} path_name @b{is}
14810 @{declarative_item@}
14811 @b{end} <project_>simple_name;
14812
14813 declarative_item ::=
14814 package_declaration |
14815 typed_string_declaration |
14816 other_declarative_item
14817
14818 package_declaration ::=
14819 package_spec | package_renaming
14820
14821 package_spec ::=
14822 @b{package} package_identifier @b{is}
14823 @{simple_declarative_item@}
14824 @b{end} package_identifier ;
14825
14826 package_identifier ::=
14827 @code{Naming} | @code{Builder} | @code{Compiler} | @code{Binder} |
14828 @code{Linker} | @code{Finder} | @code{Cross_Reference} |
14829 @code{^gnatls^gnatls^} | @code{IDE} | @code{Pretty_Printer}
14830
14831 package_renaming ::==
14832 @b{package} package_identifier @b{renames}
14833 <project_>simple_name.package_identifier ;
14834
14835 typed_string_declaration ::=
14836 @b{type} <typed_string_>_simple_name @b{is}
14837 ( string_literal @{, string_literal@} );
14838
14839 other_declarative_item ::=
14840 attribute_declaration |
14841 typed_variable_declaration |
14842 variable_declaration |
14843 case_construction
14844
14845 attribute_declaration ::=
14846 full_associative_array_declaration |
14847 @b{for} attribute_designator @b{use} expression ;
14848
14849 full_associative_array_declaration ::=
14850 @b{for} <associative_array_attribute_>simple_name @b{use}
14851 <project_>simple_name [ . <package_>simple_Name ] ' <attribute_>simple_name ;
14852
14853 attribute_designator ::=
14854 <simple_attribute_>simple_name |
14855 <associative_array_attribute_>simple_name ( string_literal )
14856
14857 typed_variable_declaration ::=
14858 <typed_variable_>simple_name : <typed_string_>name := string_expression ;
14859
14860 variable_declaration ::=
14861 <variable_>simple_name := expression;
14862
14863 expression ::=
14864 term @{& term@}
14865
14866 term ::=
14867 literal_string |
14868 string_list |
14869 <variable_>name |
14870 external_value |
14871 attribute_reference
14872
14873 string_literal ::=
14874 (same as Ada)
14875
14876 string_list ::=
14877 ( <string_>expression @{ , <string_>expression @} )
14878
14879 external_value ::=
14880 @b{external} ( string_literal [, string_literal] )
14881
14882 attribute_reference ::=
14883 attribute_prefix ' <simple_attribute_>simple_name [ ( literal_string ) ]
14884
14885 attribute_prefix ::=
14886 @b{project} |
14887 <project_>simple_name | package_identifier |
14888 <project_>simple_name . package_identifier
14889
14890 case_construction ::=
14891 @b{case} <typed_variable_>name @b{is}
14892 @{case_item@}
14893 @b{end case} ;
14894
14895 case_item ::=
14896 @b{when} discrete_choice_list =>
14897 @{case_construction | attribute_declaration@}
14898
14899 discrete_choice_list ::=
14900 string_literal @{| string_literal@} |
14901 @b{others}
14902
14903 name ::=
14904 simple_name @{. simple_name@}
14905
14906 simple_name ::=
14907 identifier (same as Ada)
14908
14909 @end smallexample
14910
14911 @node The Cross-Referencing Tools gnatxref and gnatfind
14912 @chapter The Cross-Referencing Tools @code{gnatxref} and @code{gnatfind}
14913 @findex gnatxref
14914 @findex gnatfind
14915
14916 @noindent
14917 The compiler generates cross-referencing information (unless
14918 you set the @samp{-gnatx} switch), which are saved in the @file{.ali} files.
14919 This information indicates where in the source each entity is declared and
14920 referenced. Note that entities in package Standard are not included, but
14921 entities in all other predefined units are included in the output.
14922
14923 Before using any of these two tools, you need to compile successfully your
14924 application, so that GNAT gets a chance to generate the cross-referencing
14925 information.
14926
14927 The two tools @code{gnatxref} and @code{gnatfind} take advantage of this
14928 information to provide the user with the capability to easily locate the
14929 declaration and references to an entity. These tools are quite similar,
14930 the difference being that @code{gnatfind} is intended for locating
14931 definitions and/or references to a specified entity or entities, whereas
14932 @code{gnatxref} is oriented to generating a full report of all
14933 cross-references.
14934
14935 To use these tools, you must not compile your application using the
14936 @option{-gnatx} switch on the @command{gnatmake} command line
14937 (@pxref{The GNAT Make Program gnatmake}). Otherwise, cross-referencing
14938 information will not be generated.
14939
14940 Note: to invoke @code{gnatxref} or @code{gnatfind} with a project file,
14941 use the @code{gnat} driver (see @ref{The GNAT Driver and Project Files}).
14942
14943 @menu
14944 * gnatxref Switches::
14945 * gnatfind Switches::
14946 * Project Files for gnatxref and gnatfind::
14947 * Regular Expressions in gnatfind and gnatxref::
14948 * Examples of gnatxref Usage::
14949 * Examples of gnatfind Usage::
14950 @end menu
14951
14952 @node gnatxref Switches
14953 @section @code{gnatxref} Switches
14954
14955 @noindent
14956 The command invocation for @code{gnatxref} is:
14957 @smallexample
14958 $ gnatxref @ovar{switches} @var{sourcefile1} @r{[}@var{sourcefile2} @dots{}@r{]}
14959 @end smallexample
14960
14961 @noindent
14962 where
14963
14964 @table @var
14965 @item sourcefile1
14966 @itemx sourcefile2
14967 identifies the source files for which a report is to be generated. The
14968 ``with''ed units will be processed too. You must provide at least one file.
14969
14970 These file names are considered to be regular expressions, so for instance
14971 specifying @file{source*.adb} is the same as giving every file in the current
14972 directory whose name starts with @file{source} and whose extension is
14973 @file{adb}.
14974
14975 You shouldn't specify any directory name, just base names. @command{gnatxref}
14976 and @command{gnatfind} will be able to locate these files by themselves using
14977 the source path. If you specify directories, no result is produced.
14978
14979 @end table
14980
14981 @noindent
14982 The switches can be:
14983 @table @option
14984 @c !sort!
14985 @item --version
14986 @cindex @option{--version} @command{gnatxref}
14987 Display Copyright and version, then exit disregarding all other options.
14988
14989 @item --help
14990 @cindex @option{--help} @command{gnatxref}
14991 If @option{--version} was not used, display usage, then exit disregarding
14992 all other options.
14993
14994 @item ^-a^/ALL_FILES^
14995 @cindex @option{^-a^/ALL_FILES^} (@command{gnatxref})
14996 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
14997 the read-only files found in the library search path. Otherwise, these files
14998 will be ignored. This option can be used to protect Gnat sources or your own
14999 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
15000 much faster, and their output much smaller. Read-only here refers to access
15001 or permissions status in the file system for the current user.
15002
15003 @item -aIDIR
15004 @cindex @option{-aIDIR} (@command{gnatxref})
15005 When looking for source files also look in directory DIR. The order in which
15006 source file search is undertaken is the same as for @command{gnatmake}.
15007
15008 @item -aODIR
15009 @cindex @option{-aODIR} (@command{gnatxref})
15010 When searching for library and object files, look in directory
15011 DIR. The order in which library files are searched is the same as for
15012 @command{gnatmake}.
15013
15014 @item -nostdinc
15015 @cindex @option{-nostdinc} (@command{gnatxref})
15016 Do not look for sources in the system default directory.
15017
15018 @item -nostdlib
15019 @cindex @option{-nostdlib} (@command{gnatxref})
15020 Do not look for library files in the system default directory.
15021
15022 @item --RTS=@var{rts-path}
15023 @cindex @option{--RTS} (@command{gnatxref})
15024 Specifies the default location of the runtime library. Same meaning as the
15025 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
15026
15027 @item ^-d^/DERIVED_TYPES^
15028 @cindex @option{^-d^/DERIVED_TYPES^} (@command{gnatxref})
15029 If this switch is set @code{gnatxref} will output the parent type
15030 reference for each matching derived types.
15031
15032 @item ^-f^/FULL_PATHNAME^
15033 @cindex @option{^-f^/FULL_PATHNAME^} (@command{gnatxref})
15034 If this switch is set, the output file names will be preceded by their
15035 directory (if the file was found in the search path). If this switch is
15036 not set, the directory will not be printed.
15037
15038 @item ^-g^/IGNORE_LOCALS^
15039 @cindex @option{^-g^/IGNORE_LOCALS^} (@command{gnatxref})
15040 If this switch is set, information is output only for library-level
15041 entities, ignoring local entities. The use of this switch may accelerate
15042 @code{gnatfind} and @code{gnatxref}.
15043
15044 @item -IDIR
15045 @cindex @option{-IDIR} (@command{gnatxref})
15046 Equivalent to @samp{-aODIR -aIDIR}.
15047
15048 @item -pFILE
15049 @cindex @option{-pFILE} (@command{gnatxref})
15050 Specify a project file to use @xref{Project Files}.
15051 If you need to use the @file{.gpr}
15052 project files, you should use gnatxref through the GNAT driver
15053 (@command{gnat xref -Pproject}).
15054
15055 By default, @code{gnatxref} and @code{gnatfind} will try to locate a
15056 project file in the current directory.
15057
15058 If a project file is either specified or found by the tools, then the content
15059 of the source directory and object directory lines are added as if they
15060 had been specified respectively by @samp{^-aI^/SOURCE_SEARCH^}
15061 and @samp{^-aO^OBJECT_SEARCH^}.
15062 @item ^-u^/UNUSED^
15063 Output only unused symbols. This may be really useful if you give your
15064 main compilation unit on the command line, as @code{gnatxref} will then
15065 display every unused entity and 'with'ed package.
15066
15067 @ifclear vms
15068 @item -v
15069 Instead of producing the default output, @code{gnatxref} will generate a
15070 @file{tags} file that can be used by vi. For examples how to use this
15071 feature, see @ref{Examples of gnatxref Usage}. The tags file is output
15072 to the standard output, thus you will have to redirect it to a file.
15073 @end ifclear
15074
15075 @end table
15076
15077 @noindent
15078 All these switches may be in any order on the command line, and may even
15079 appear after the file names. They need not be separated by spaces, thus
15080 you can say @samp{gnatxref ^-ag^/ALL_FILES/IGNORE_LOCALS^} instead of
15081 @samp{gnatxref ^-a -g^/ALL_FILES /IGNORE_LOCALS^}.
15082
15083 @node gnatfind Switches
15084 @section @code{gnatfind} Switches
15085
15086 @noindent
15087 The command line for @code{gnatfind} is:
15088
15089 @smallexample
15090 $ gnatfind @ovar{switches} @var{pattern}@r{[}:@var{sourcefile}@r{[}:@var{line}@r{[}:@var{column}@r{]]]}
15091 @r{[}@var{file1} @var{file2} @dots{}]
15092 @end smallexample
15093
15094 @noindent
15095 where
15096
15097 @table @var
15098 @item pattern
15099 An entity will be output only if it matches the regular expression found
15100 in @var{pattern}, see @ref{Regular Expressions in gnatfind and gnatxref}.
15101
15102 Omitting the pattern is equivalent to specifying @samp{*}, which
15103 will match any entity. Note that if you do not provide a pattern, you
15104 have to provide both a sourcefile and a line.
15105
15106 Entity names are given in Latin-1, with uppercase/lowercase equivalence
15107 for matching purposes. At the current time there is no support for
15108 8-bit codes other than Latin-1, or for wide characters in identifiers.
15109
15110 @item sourcefile
15111 @code{gnatfind} will look for references, bodies or declarations
15112 of symbols referenced in @file{@var{sourcefile}}, at line @var{line}
15113 and column @var{column}. See @ref{Examples of gnatfind Usage}
15114 for syntax examples.
15115
15116 @item line
15117 is a decimal integer identifying the line number containing
15118 the reference to the entity (or entities) to be located.
15119
15120 @item column
15121 is a decimal integer identifying the exact location on the
15122 line of the first character of the identifier for the
15123 entity reference. Columns are numbered from 1.
15124
15125 @item file1 file2 @dots{}
15126 The search will be restricted to these source files. If none are given, then
15127 the search will be done for every library file in the search path.
15128 These file must appear only after the pattern or sourcefile.
15129
15130 These file names are considered to be regular expressions, so for instance
15131 specifying @file{source*.adb} is the same as giving every file in the current
15132 directory whose name starts with @file{source} and whose extension is
15133 @file{adb}.
15134
15135 The location of the spec of the entity will always be displayed, even if it
15136 isn't in one of @file{@var{file1}}, @file{@var{file2}},@enddots{} The
15137 occurrences of the entity in the separate units of the ones given on the
15138 command line will also be displayed.
15139
15140 Note that if you specify at least one file in this part, @code{gnatfind} may
15141 sometimes not be able to find the body of the subprograms.
15142
15143 @end table
15144
15145 @noindent
15146 At least one of 'sourcefile' or 'pattern' has to be present on
15147 the command line.
15148
15149 The following switches are available:
15150 @table @option
15151 @c !sort!
15152
15153 @cindex @option{--version} @command{gnatfind}
15154 Display Copyright and version, then exit disregarding all other options.
15155
15156 @item --help
15157 @cindex @option{--help} @command{gnatfind}
15158 If @option{--version} was not used, display usage, then exit disregarding
15159 all other options.
15160
15161 @item ^-a^/ALL_FILES^
15162 @cindex @option{^-a^/ALL_FILES^} (@command{gnatfind})
15163 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
15164 the read-only files found in the library search path. Otherwise, these files
15165 will be ignored. This option can be used to protect Gnat sources or your own
15166 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
15167 much faster, and their output much smaller. Read-only here refers to access
15168 or permission status in the file system for the current user.
15169
15170 @item -aIDIR
15171 @cindex @option{-aIDIR} (@command{gnatfind})
15172 When looking for source files also look in directory DIR. The order in which
15173 source file search is undertaken is the same as for @command{gnatmake}.
15174
15175 @item -aODIR
15176 @cindex @option{-aODIR} (@command{gnatfind})
15177 When searching for library and object files, look in directory
15178 DIR. The order in which library files are searched is the same as for
15179 @command{gnatmake}.
15180
15181 @item -nostdinc
15182 @cindex @option{-nostdinc} (@command{gnatfind})
15183 Do not look for sources in the system default directory.
15184
15185 @item -nostdlib
15186 @cindex @option{-nostdlib} (@command{gnatfind})
15187 Do not look for library files in the system default directory.
15188
15189 @item --RTS=@var{rts-path}
15190 @cindex @option{--RTS} (@command{gnatfind})
15191 Specifies the default location of the runtime library. Same meaning as the
15192 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
15193
15194 @item ^-d^/DERIVED_TYPE_INFORMATION^
15195 @cindex @option{^-d^/DERIVED_TYPE_INFORMATION^} (@code{gnatfind})
15196 If this switch is set, then @code{gnatfind} will output the parent type
15197 reference for each matching derived types.
15198
15199 @item ^-e^/EXPRESSIONS^
15200 @cindex @option{^-e^/EXPRESSIONS^} (@command{gnatfind})
15201 By default, @code{gnatfind} accept the simple regular expression set for
15202 @samp{pattern}. If this switch is set, then the pattern will be
15203 considered as full Unix-style regular expression.
15204
15205 @item ^-f^/FULL_PATHNAME^
15206 @cindex @option{^-f^/FULL_PATHNAME^} (@command{gnatfind})
15207 If this switch is set, the output file names will be preceded by their
15208 directory (if the file was found in the search path). If this switch is
15209 not set, the directory will not be printed.
15210
15211 @item ^-g^/IGNORE_LOCALS^
15212 @cindex @option{^-g^/IGNORE_LOCALS^} (@command{gnatfind})
15213 If this switch is set, information is output only for library-level
15214 entities, ignoring local entities. The use of this switch may accelerate
15215 @code{gnatfind} and @code{gnatxref}.
15216
15217 @item -IDIR
15218 @cindex @option{-IDIR} (@command{gnatfind})
15219 Equivalent to @samp{-aODIR -aIDIR}.
15220
15221 @item -pFILE
15222 @cindex @option{-pFILE} (@command{gnatfind})
15223 Specify a project file (@pxref{Project Files}) to use.
15224 By default, @code{gnatxref} and @code{gnatfind} will try to locate a
15225 project file in the current directory.
15226
15227 If a project file is either specified or found by the tools, then the content
15228 of the source directory and object directory lines are added as if they
15229 had been specified respectively by @samp{^-aI^/SOURCE_SEARCH^} and
15230 @samp{^-aO^/OBJECT_SEARCH^}.
15231
15232 @item ^-r^/REFERENCES^
15233 @cindex @option{^-r^/REFERENCES^} (@command{gnatfind})
15234 By default, @code{gnatfind} will output only the information about the
15235 declaration, body or type completion of the entities. If this switch is
15236 set, the @code{gnatfind} will locate every reference to the entities in
15237 the files specified on the command line (or in every file in the search
15238 path if no file is given on the command line).
15239
15240 @item ^-s^/PRINT_LINES^
15241 @cindex @option{^-s^/PRINT_LINES^} (@command{gnatfind})
15242 If this switch is set, then @code{gnatfind} will output the content
15243 of the Ada source file lines were the entity was found.
15244
15245 @item ^-t^/TYPE_HIERARCHY^
15246 @cindex @option{^-t^/TYPE_HIERARCHY^} (@command{gnatfind})
15247 If this switch is set, then @code{gnatfind} will output the type hierarchy for
15248 the specified type. It act like -d option but recursively from parent
15249 type to parent type. When this switch is set it is not possible to
15250 specify more than one file.
15251
15252 @end table
15253
15254 @noindent
15255 All these switches may be in any order on the command line, and may even
15256 appear after the file names. They need not be separated by spaces, thus
15257 you can say @samp{gnatxref ^-ag^/ALL_FILES/IGNORE_LOCALS^} instead of
15258 @samp{gnatxref ^-a -g^/ALL_FILES /IGNORE_LOCALS^}.
15259
15260 As stated previously, gnatfind will search in every directory in the
15261 search path. You can force it to look only in the current directory if
15262 you specify @code{*} at the end of the command line.
15263
15264 @node Project Files for gnatxref and gnatfind
15265 @section Project Files for @command{gnatxref} and @command{gnatfind}
15266
15267 @noindent
15268 Project files allow a programmer to specify how to compile its
15269 application, where to find sources, etc. These files are used
15270 @ifclear vms
15271 primarily by GPS, but they can also be used
15272 @end ifclear
15273 by the two tools
15274 @code{gnatxref} and @code{gnatfind}.
15275
15276 A project file name must end with @file{.gpr}. If a single one is
15277 present in the current directory, then @code{gnatxref} and @code{gnatfind} will
15278 extract the information from it. If multiple project files are found, none of
15279 them is read, and you have to use the @samp{-p} switch to specify the one
15280 you want to use.
15281
15282 The following lines can be included, even though most of them have default
15283 values which can be used in most cases.
15284 The lines can be entered in any order in the file.
15285 Except for @file{src_dir} and @file{obj_dir}, you can only have one instance of
15286 each line. If you have multiple instances, only the last one is taken into
15287 account.
15288
15289 @table @code
15290 @item src_dir=DIR
15291 [default: @code{"^./^[]^"}]
15292 specifies a directory where to look for source files. Multiple @code{src_dir}
15293 lines can be specified and they will be searched in the order they
15294 are specified.
15295
15296 @item obj_dir=DIR
15297 [default: @code{"^./^[]^"}]
15298 specifies a directory where to look for object and library files. Multiple
15299 @code{obj_dir} lines can be specified, and they will be searched in the order
15300 they are specified
15301
15302 @item comp_opt=SWITCHES
15303 [default: @code{""}]
15304 creates a variable which can be referred to subsequently by using
15305 the @code{$@{comp_opt@}} notation. This is intended to store the default
15306 switches given to @command{gnatmake} and @command{gcc}.
15307
15308 @item bind_opt=SWITCHES
15309 [default: @code{""}]
15310 creates a variable which can be referred to subsequently by using
15311 the @samp{$@{bind_opt@}} notation. This is intended to store the default
15312 switches given to @command{gnatbind}.
15313
15314 @item link_opt=SWITCHES
15315 [default: @code{""}]
15316 creates a variable which can be referred to subsequently by using
15317 the @samp{$@{link_opt@}} notation. This is intended to store the default
15318 switches given to @command{gnatlink}.
15319
15320 @item main=EXECUTABLE
15321 [default: @code{""}]
15322 specifies the name of the executable for the application. This variable can
15323 be referred to in the following lines by using the @samp{$@{main@}} notation.
15324
15325 @ifset vms
15326 @item comp_cmd=COMMAND
15327 [default: @code{"GNAT COMPILE /SEARCH=$@{src_dir@} /DEBUG /TRY_SEMANTICS"}]
15328 @end ifset
15329 @ifclear vms
15330 @item comp_cmd=COMMAND
15331 [default: @code{"gcc -c -I$@{src_dir@} -g -gnatq"}]
15332 @end ifclear
15333 specifies the command used to compile a single file in the application.
15334
15335 @ifset vms
15336 @item make_cmd=COMMAND
15337 [default: @code{"GNAT MAKE $@{main@}
15338 /SOURCE_SEARCH=$@{src_dir@} /OBJECT_SEARCH=$@{obj_dir@}
15339 /DEBUG /TRY_SEMANTICS /COMPILER_QUALIFIERS $@{comp_opt@}
15340 /BINDER_QUALIFIERS $@{bind_opt@} /LINKER_QUALIFIERS $@{link_opt@}"}]
15341 @end ifset
15342 @ifclear vms
15343 @item make_cmd=COMMAND
15344 [default: @code{"gnatmake $@{main@} -aI$@{src_dir@}
15345 -aO$@{obj_dir@} -g -gnatq -cargs $@{comp_opt@}
15346 -bargs $@{bind_opt@} -largs $@{link_opt@}"}]
15347 @end ifclear
15348 specifies the command used to recompile the whole application.
15349
15350 @item run_cmd=COMMAND
15351 [default: @code{"$@{main@}"}]
15352 specifies the command used to run the application.
15353
15354 @item debug_cmd=COMMAND
15355 [default: @code{"gdb $@{main@}"}]
15356 specifies the command used to debug the application
15357
15358 @end table
15359
15360 @noindent
15361 @command{gnatxref} and @command{gnatfind} only take into account the
15362 @code{src_dir} and @code{obj_dir} lines, and ignore the others.
15363
15364 @node Regular Expressions in gnatfind and gnatxref
15365 @section Regular Expressions in @code{gnatfind} and @code{gnatxref}
15366
15367 @noindent
15368 As specified in the section about @command{gnatfind}, the pattern can be a
15369 regular expression. Actually, there are to set of regular expressions
15370 which are recognized by the program:
15371
15372 @table @code
15373 @item globbing patterns
15374 These are the most usual regular expression. They are the same that you
15375 generally used in a Unix shell command line, or in a DOS session.
15376
15377 Here is a more formal grammar:
15378 @smallexample
15379 @group
15380 @iftex
15381 @leftskip=.5cm
15382 @end iftex
15383 regexp ::= term
15384 term ::= elmt -- matches elmt
15385 term ::= elmt elmt -- concatenation (elmt then elmt)
15386 term ::= * -- any string of 0 or more characters
15387 term ::= ? -- matches any character
15388 term ::= [char @{char@}] -- matches any character listed
15389 term ::= [char - char] -- matches any character in range
15390 @end group
15391 @end smallexample
15392
15393 @item full regular expression
15394 The second set of regular expressions is much more powerful. This is the
15395 type of regular expressions recognized by utilities such a @file{grep}.
15396
15397 The following is the form of a regular expression, expressed in Ada
15398 reference manual style BNF is as follows
15399
15400 @smallexample
15401 @iftex
15402 @leftskip=.5cm
15403 @end iftex
15404 @group
15405 regexp ::= term @{| term@} -- alternation (term or term @dots{})
15406
15407 term ::= item @{item@} -- concatenation (item then item)
15408
15409 item ::= elmt -- match elmt
15410 item ::= elmt * -- zero or more elmt's
15411 item ::= elmt + -- one or more elmt's
15412 item ::= elmt ? -- matches elmt or nothing
15413 @end group
15414 @group
15415 elmt ::= nschar -- matches given character
15416 elmt ::= [nschar @{nschar@}] -- matches any character listed
15417 elmt ::= [^^^ nschar @{nschar@}] -- matches any character not listed
15418 elmt ::= [char - char] -- matches chars in given range
15419 elmt ::= \ char -- matches given character
15420 elmt ::= . -- matches any single character
15421 elmt ::= ( regexp ) -- parens used for grouping
15422
15423 char ::= any character, including special characters
15424 nschar ::= any character except ()[].*+?^^^
15425 @end group
15426 @end smallexample
15427
15428 Following are a few examples:
15429
15430 @table @samp
15431 @item abcde|fghi
15432 will match any of the two strings @samp{abcde} and @samp{fghi},
15433
15434 @item abc*d
15435 will match any string like @samp{abd}, @samp{abcd}, @samp{abccd},
15436 @samp{abcccd}, and so on,
15437
15438 @item [a-z]+
15439 will match any string which has only lowercase characters in it (and at
15440 least one character.
15441
15442 @end table
15443 @end table
15444
15445 @node Examples of gnatxref Usage
15446 @section Examples of @code{gnatxref} Usage
15447
15448 @subsection General Usage
15449
15450 @noindent
15451 For the following examples, we will consider the following units:
15452
15453 @smallexample @c ada
15454 @group
15455 @cartouche
15456 main.ads:
15457 1: with Bar;
15458 2: package Main is
15459 3: procedure Foo (B : in Integer);
15460 4: C : Integer;
15461 5: private
15462 6: D : Integer;
15463 7: end Main;
15464
15465 main.adb:
15466 1: package body Main is
15467 2: procedure Foo (B : in Integer) is
15468 3: begin
15469 4: C := B;
15470 5: D := B;
15471 6: Bar.Print (B);
15472 7: Bar.Print (C);
15473 8: end Foo;
15474 9: end Main;
15475
15476 bar.ads:
15477 1: package Bar is
15478 2: procedure Print (B : Integer);
15479 3: end bar;
15480 @end cartouche
15481 @end group
15482 @end smallexample
15483
15484 @table @code
15485
15486 @noindent
15487 The first thing to do is to recompile your application (for instance, in
15488 that case just by doing a @samp{gnatmake main}, so that GNAT generates
15489 the cross-referencing information.
15490 You can then issue any of the following commands:
15491
15492 @item gnatxref main.adb
15493 @code{gnatxref} generates cross-reference information for main.adb
15494 and every unit 'with'ed by main.adb.
15495
15496 The output would be:
15497 @smallexample
15498 @iftex
15499 @leftskip=0cm
15500 @end iftex
15501 B Type: Integer
15502 Decl: bar.ads 2:22
15503 B Type: Integer
15504 Decl: main.ads 3:20
15505 Body: main.adb 2:20
15506 Ref: main.adb 4:13 5:13 6:19
15507 Bar Type: Unit
15508 Decl: bar.ads 1:9
15509 Ref: main.adb 6:8 7:8
15510 main.ads 1:6
15511 C Type: Integer
15512 Decl: main.ads 4:5
15513 Modi: main.adb 4:8
15514 Ref: main.adb 7:19
15515 D Type: Integer
15516 Decl: main.ads 6:5
15517 Modi: main.adb 5:8
15518 Foo Type: Unit
15519 Decl: main.ads 3:15
15520 Body: main.adb 2:15
15521 Main Type: Unit
15522 Decl: main.ads 2:9
15523 Body: main.adb 1:14
15524 Print Type: Unit
15525 Decl: bar.ads 2:15
15526 Ref: main.adb 6:12 7:12
15527 @end smallexample
15528
15529 @noindent
15530 that is the entity @code{Main} is declared in main.ads, line 2, column 9,
15531 its body is in main.adb, line 1, column 14 and is not referenced any where.
15532
15533 The entity @code{Print} is declared in bar.ads, line 2, column 15 and it
15534 it referenced in main.adb, line 6 column 12 and line 7 column 12.
15535
15536 @item gnatxref package1.adb package2.ads
15537 @code{gnatxref} will generates cross-reference information for
15538 package1.adb, package2.ads and any other package 'with'ed by any
15539 of these.
15540
15541 @end table
15542
15543 @ifclear vms
15544 @subsection Using gnatxref with vi
15545
15546 @code{gnatxref} can generate a tags file output, which can be used
15547 directly from @command{vi}. Note that the standard version of @command{vi}
15548 will not work properly with overloaded symbols. Consider using another
15549 free implementation of @command{vi}, such as @command{vim}.
15550
15551 @smallexample
15552 $ gnatxref -v gnatfind.adb > tags
15553 @end smallexample
15554
15555 @noindent
15556 will generate the tags file for @code{gnatfind} itself (if the sources
15557 are in the search path!).
15558
15559 From @command{vi}, you can then use the command @samp{:tag @var{entity}}
15560 (replacing @var{entity} by whatever you are looking for), and vi will
15561 display a new file with the corresponding declaration of entity.
15562 @end ifclear
15563
15564 @node Examples of gnatfind Usage
15565 @section Examples of @code{gnatfind} Usage
15566
15567 @table @code
15568
15569 @item gnatfind ^-f^/FULL_PATHNAME^ xyz:main.adb
15570 Find declarations for all entities xyz referenced at least once in
15571 main.adb. The references are search in every library file in the search
15572 path.
15573
15574 The directories will be printed as well (as the @samp{^-f^/FULL_PATHNAME^}
15575 switch is set)
15576
15577 The output will look like:
15578 @smallexample
15579 ^directory/^[directory]^main.ads:106:14: xyz <= declaration
15580 ^directory/^[directory]^main.adb:24:10: xyz <= body
15581 ^directory/^[directory]^foo.ads:45:23: xyz <= declaration
15582 @end smallexample
15583
15584 @noindent
15585 that is to say, one of the entities xyz found in main.adb is declared at
15586 line 12 of main.ads (and its body is in main.adb), and another one is
15587 declared at line 45 of foo.ads
15588
15589 @item gnatfind ^-fs^/FULL_PATHNAME/SOURCE_LINE^ xyz:main.adb
15590 This is the same command as the previous one, instead @code{gnatfind} will
15591 display the content of the Ada source file lines.
15592
15593 The output will look like:
15594
15595 @smallexample
15596 ^directory/^[directory]^main.ads:106:14: xyz <= declaration
15597 procedure xyz;
15598 ^directory/^[directory]^main.adb:24:10: xyz <= body
15599 procedure xyz is
15600 ^directory/^[directory]^foo.ads:45:23: xyz <= declaration
15601 xyz : Integer;
15602 @end smallexample
15603
15604 @noindent
15605 This can make it easier to find exactly the location your are looking
15606 for.
15607
15608 @item gnatfind ^-r^/REFERENCES^ "*x*":main.ads:123 foo.adb
15609 Find references to all entities containing an x that are
15610 referenced on line 123 of main.ads.
15611 The references will be searched only in main.ads and foo.adb.
15612
15613 @item gnatfind main.ads:123
15614 Find declarations and bodies for all entities that are referenced on
15615 line 123 of main.ads.
15616
15617 This is the same as @code{gnatfind "*":main.adb:123}.
15618
15619 @item gnatfind ^mydir/^[mydir]^main.adb:123:45
15620 Find the declaration for the entity referenced at column 45 in
15621 line 123 of file main.adb in directory mydir. Note that it
15622 is usual to omit the identifier name when the column is given,
15623 since the column position identifies a unique reference.
15624
15625 The column has to be the beginning of the identifier, and should not
15626 point to any character in the middle of the identifier.
15627
15628 @end table
15629
15630 @c *********************************
15631 @node The GNAT Pretty-Printer gnatpp
15632 @chapter The GNAT Pretty-Printer @command{gnatpp}
15633 @findex gnatpp
15634 @cindex Pretty-Printer
15635
15636 @noindent
15637 ^The @command{gnatpp} tool^GNAT PRETTY^ is an ASIS-based utility
15638 for source reformatting / pretty-printing.
15639 It takes an Ada source file as input and generates a reformatted
15640 version as output.
15641 You can specify various style directives via switches; e.g.,
15642 identifier case conventions, rules of indentation, and comment layout.
15643
15644 To produce a reformatted file, @command{gnatpp} generates and uses the ASIS
15645 tree for the input source and thus requires the input to be syntactically and
15646 semantically legal.
15647 If this condition is not met, @command{gnatpp} will terminate with an
15648 error message; no output file will be generated.
15649
15650 If the source files presented to @command{gnatpp} contain
15651 preprocessing directives, then the output file will
15652 correspond to the generated source after all
15653 preprocessing is carried out. There is no way
15654 using @command{gnatpp} to obtain pretty printed files that
15655 include the preprocessing directives.
15656
15657 If the compilation unit
15658 contained in the input source depends semantically upon units located
15659 outside the current directory, you have to provide the source search path
15660 when invoking @command{gnatpp}, if these units are contained in files with
15661 names that do not follow the GNAT file naming rules, you have to provide
15662 the configuration file describing the corresponding naming scheme;
15663 see the description of the @command{gnatpp}
15664 switches below. Another possibility is to use a project file and to
15665 call @command{gnatpp} through the @command{gnat} driver
15666
15667 The @command{gnatpp} command has the form
15668
15669 @smallexample
15670 $ gnatpp @ovar{switches} @var{filename}
15671 @end smallexample
15672
15673 @noindent
15674 where
15675 @itemize @bullet
15676 @item
15677 @var{switches} is an optional sequence of switches defining such properties as
15678 the formatting rules, the source search path, and the destination for the
15679 output source file
15680
15681 @item
15682 @var{filename} is the name (including the extension) of the source file to
15683 reformat; ``wildcards'' or several file names on the same gnatpp command are
15684 allowed. The file name may contain path information; it does not have to
15685 follow the GNAT file naming rules
15686 @end itemize
15687
15688 @menu
15689 * Switches for gnatpp::
15690 * Formatting Rules::
15691 @end menu
15692
15693 @node Switches for gnatpp
15694 @section Switches for @command{gnatpp}
15695
15696 @noindent
15697 The following subsections describe the various switches accepted by
15698 @command{gnatpp}, organized by category.
15699
15700 @ifclear vms
15701 You specify a switch by supplying a name and generally also a value.
15702 In many cases the values for a switch with a given name are incompatible with
15703 each other
15704 (for example the switch that controls the casing of a reserved word may have
15705 exactly one value: upper case, lower case, or
15706 mixed case) and thus exactly one such switch can be in effect for an
15707 invocation of @command{gnatpp}.
15708 If more than one is supplied, the last one is used.
15709 However, some values for the same switch are mutually compatible.
15710 You may supply several such switches to @command{gnatpp}, but then
15711 each must be specified in full, with both the name and the value.
15712 Abbreviated forms (the name appearing once, followed by each value) are
15713 not permitted.
15714 For example, to set
15715 the alignment of the assignment delimiter both in declarations and in
15716 assignment statements, you must write @option{-A2A3}
15717 (or @option{-A2 -A3}), but not @option{-A23}.
15718 @end ifclear
15719
15720 @ifset vms
15721 In many cases the set of options for a given qualifier are incompatible with
15722 each other (for example the qualifier that controls the casing of a reserved
15723 word may have exactly one option, which specifies either upper case, lower
15724 case, or mixed case), and thus exactly one such option can be in effect for
15725 an invocation of @command{gnatpp}.
15726 If more than one is supplied, the last one is used.
15727 However, some qualifiers have options that are mutually compatible,
15728 and then you may then supply several such options when invoking
15729 @command{gnatpp}.
15730 @end ifset
15731
15732 In most cases, it is obvious whether or not the
15733 ^values for a switch with a given name^options for a given qualifier^
15734 are compatible with each other.
15735 When the semantics might not be evident, the summaries below explicitly
15736 indicate the effect.
15737
15738 @menu
15739 * Alignment Control::
15740 * Casing Control::
15741 * Construct Layout Control::
15742 * General Text Layout Control::
15743 * Other Formatting Options::
15744 * Setting the Source Search Path::
15745 * Output File Control::
15746 * Other gnatpp Switches::
15747 @end menu
15748
15749 @node Alignment Control
15750 @subsection Alignment Control
15751 @cindex Alignment control in @command{gnatpp}
15752
15753 @noindent
15754 Programs can be easier to read if certain constructs are vertically aligned.
15755 By default all alignments are set ON.
15756 Through the @option{^-A0^/ALIGN=OFF^} switch you may reset the default to
15757 OFF, and then use one or more of the other
15758 ^@option{-A@var{n}} switches^@option{/ALIGN} options^
15759 to activate alignment for specific constructs.
15760
15761 @table @option
15762 @cindex @option{^-A@var{n}^/ALIGN^} (@command{gnatpp})
15763
15764 @ifset vms
15765 @item /ALIGN=ON
15766 Set all alignments to ON
15767 @end ifset
15768
15769 @item ^-A0^/ALIGN=OFF^
15770 Set all alignments to OFF
15771
15772 @item ^-A1^/ALIGN=COLONS^
15773 Align @code{:} in declarations
15774
15775 @item ^-A2^/ALIGN=DECLARATIONS^
15776 Align @code{:=} in initializations in declarations
15777
15778 @item ^-A3^/ALIGN=STATEMENTS^
15779 Align @code{:=} in assignment statements
15780
15781 @item ^-A4^/ALIGN=ARROWS^
15782 Align @code{=>} in associations
15783
15784 @item ^-A5^/ALIGN=COMPONENT_CLAUSES^
15785 Align @code{at} keywords in the component clauses in record
15786 representation clauses
15787 @end table
15788
15789 @noindent
15790 The @option{^-A^/ALIGN^} switches are mutually compatible; any combination
15791 is allowed.
15792
15793 @node Casing Control
15794 @subsection Casing Control
15795 @cindex Casing control in @command{gnatpp}
15796
15797 @noindent
15798 @command{gnatpp} allows you to specify the casing for reserved words,
15799 pragma names, attribute designators and identifiers.
15800 For identifiers you may define a
15801 general rule for name casing but also override this rule
15802 via a set of dictionary files.
15803
15804 Three types of casing are supported: lower case, upper case, and mixed case.
15805 Lower and upper case are self-explanatory (but since some letters in
15806 Latin1 and other GNAT-supported character sets
15807 exist only in lower-case form, an upper case conversion will have no
15808 effect on them.)
15809 ``Mixed case'' means that the first letter, and also each letter immediately
15810 following an underscore, are converted to their uppercase forms;
15811 all the other letters are converted to their lowercase forms.
15812
15813 @table @option
15814 @cindex @option{^-a@var{x}^/ATTRIBUTE^} (@command{gnatpp})
15815 @item ^-aL^/ATTRIBUTE_CASING=LOWER_CASE^
15816 Attribute designators are lower case
15817
15818 @item ^-aU^/ATTRIBUTE_CASING=UPPER_CASE^
15819 Attribute designators are upper case
15820
15821 @item ^-aM^/ATTRIBUTE_CASING=MIXED_CASE^
15822 Attribute designators are mixed case (this is the default)
15823
15824 @cindex @option{^-k@var{x}^/KEYWORD_CASING^} (@command{gnatpp})
15825 @item ^-kL^/KEYWORD_CASING=LOWER_CASE^
15826 Keywords (technically, these are known in Ada as @emph{reserved words}) are
15827 lower case (this is the default)
15828
15829 @item ^-kU^/KEYWORD_CASING=UPPER_CASE^
15830 Keywords are upper case
15831
15832 @cindex @option{^-n@var{x}^/NAME_CASING^} (@command{gnatpp})
15833 @item ^-nD^/NAME_CASING=AS_DECLARED^
15834 Name casing for defining occurrences are as they appear in the source file
15835 (this is the default)
15836
15837 @item ^-nU^/NAME_CASING=UPPER_CASE^
15838 Names are in upper case
15839
15840 @item ^-nL^/NAME_CASING=LOWER_CASE^
15841 Names are in lower case
15842
15843 @item ^-nM^/NAME_CASING=MIXED_CASE^
15844 Names are in mixed case
15845
15846 @cindex @option{^-p@var{x}^/PRAGMA_CASING^} (@command{gnatpp})
15847 @item ^-pL^/PRAGMA_CASING=LOWER_CASE^
15848 Pragma names are lower case
15849
15850 @item ^-pU^/PRAGMA_CASING=UPPER_CASE^
15851 Pragma names are upper case
15852
15853 @item ^-pM^/PRAGMA_CASING=MIXED_CASE^
15854 Pragma names are mixed case (this is the default)
15855
15856 @item ^-D@var{file}^/DICTIONARY=@var{file}^
15857 @cindex @option{^-D^/DICTIONARY^} (@command{gnatpp})
15858 Use @var{file} as a @emph{dictionary file} that defines
15859 the casing for a set of specified names,
15860 thereby overriding the effect on these names by
15861 any explicit or implicit
15862 ^-n^/NAME_CASING^ switch.
15863 To supply more than one dictionary file,
15864 use ^several @option{-D} switches^a list of files as options^.
15865
15866 @noindent
15867 @option{gnatpp} implicitly uses a @emph{default dictionary file}
15868 to define the casing for the Ada predefined names and
15869 the names declared in the GNAT libraries.
15870
15871 @item ^-D-^/SPECIFIC_CASING^
15872 @cindex @option{^-D-^/SPECIFIC_CASING^} (@command{gnatpp})
15873 Do not use the default dictionary file;
15874 instead, use the casing
15875 defined by a @option{^-n^/NAME_CASING^} switch and any explicit
15876 dictionary file(s)
15877 @end table
15878
15879 @noindent
15880 The structure of a dictionary file, and details on the conventions
15881 used in the default dictionary file, are defined in @ref{Name Casing}.
15882
15883 The @option{^-D-^/SPECIFIC_CASING^} and
15884 @option{^-D@var{file}^/DICTIONARY=@var{file}^} switches are mutually
15885 compatible.
15886
15887 @node Construct Layout Control
15888 @subsection Construct Layout Control
15889 @cindex Layout control in @command{gnatpp}
15890
15891 @noindent
15892 This group of @command{gnatpp} switches controls the layout of comments and
15893 complex syntactic constructs. See @ref{Formatting Comments} for details
15894 on their effect.
15895
15896 @table @option
15897 @cindex @option{^-c@var{n}^/COMMENTS_LAYOUT^} (@command{gnatpp})
15898 @item ^-c0^/COMMENTS_LAYOUT=UNTOUCHED^
15899 All the comments remain unchanged
15900
15901 @item ^-c1^/COMMENTS_LAYOUT=DEFAULT^
15902 GNAT-style comment line indentation (this is the default).
15903
15904 @item ^-c2^/COMMENTS_LAYOUT=STANDARD_INDENT^
15905 Reference-manual comment line indentation.
15906
15907 @item ^-c3^/COMMENTS_LAYOUT=GNAT_BEGINNING^
15908 GNAT-style comment beginning
15909
15910 @item ^-c4^/COMMENTS_LAYOUT=REFORMAT^
15911 Reformat comment blocks
15912
15913 @item ^-c5^/COMMENTS_LAYOUT=KEEP_SPECIAL^
15914 Keep unchanged special form comments
15915
15916 Reformat comment blocks
15917
15918 @cindex @option{^-l@var{n}^/CONSTRUCT_LAYOUT^} (@command{gnatpp})
15919 @item ^-l1^/CONSTRUCT_LAYOUT=GNAT^
15920 GNAT-style layout (this is the default)
15921
15922 @item ^-l2^/CONSTRUCT_LAYOUT=COMPACT^
15923 Compact layout
15924
15925 @item ^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^
15926 Uncompact layout
15927
15928 @cindex @option{^-N^/NOTABS^} (@command{gnatpp})
15929 @item ^-N^/NOTABS^
15930 All the VT characters are removed from the comment text. All the HT characters
15931 are expanded with the sequences of space characters to get to the next tab
15932 stops.
15933
15934 @cindex @option{^--no-separate-is^/NO_SEPARATE_IS^} (@command{gnatpp})
15935 @item ^--no-separate-is^/NO_SEPARATE_IS^
15936 Do not place the keyword @code{is} on a separate line in a subprogram body in
15937 case if the spec occupies more then one line.
15938
15939 @cindex @option{^--separate-loop-then^/SEPARATE_LOOP_THEN^} (@command{gnatpp})
15940 @item ^--separate-loop-then^/SEPARATE_LOOP_THEN^
15941 Place the keyword @code{loop} in FOR and WHILE loop statements and the
15942 keyword @code{then} in IF statements on a separate line.
15943
15944 @cindex @option{^--no-separate-loop-then^/NO_SEPARATE_LOOP_THEN^} (@command{gnatpp})
15945 @item ^--no-separate-loop-then^/NO_SEPARATE_LOOP_THEN^
15946 Do not place the keyword @code{loop} in FOR and WHILE loop statements and the
15947 keyword @code{then} in IF statements on a separate line. This option is
15948 incompatible with @option{^--separate-loop-then^/SEPARATE_LOOP_THEN^} option.
15949
15950 @cindex @option{^--use-on-new-line^/USE_ON_NEW_LINE^} (@command{gnatpp})
15951 @item ^--use-on-new-line^/USE_ON_NEW_LINE^
15952 Start each USE clause in a context clause from a separate line.
15953
15954 @cindex @option{^--separate-stmt-name^/STMT_NAME_ON_NEW_LINE^} (@command{gnatpp})
15955 @item ^--separate-stmt-name^/STMT_NAME_ON_NEW_LINE^
15956 Use a separate line for a loop or block statement name, but do not use an extra
15957 indentation level for the statement itself.
15958
15959 @end table
15960
15961 @ifclear vms
15962 @noindent
15963 The @option{-c1} and @option{-c2} switches are incompatible.
15964 The @option{-c3} and @option{-c4} switches are compatible with each other and
15965 also with @option{-c1} and @option{-c2}. The @option{-c0} switch disables all
15966 the other comment formatting switches.
15967
15968 The @option{-l1}, @option{-l2}, and @option{-l3} switches are incompatible.
15969 @end ifclear
15970
15971 @ifset vms
15972 @noindent
15973 For the @option{/COMMENTS_LAYOUT} qualifier:
15974 @itemize @bullet
15975 @item
15976 The @option{DEFAULT} and @option{STANDARD_INDENT} options are incompatible.
15977 @item
15978 The @option{GNAT_BEGINNING} and @option{REFORMAT} options are compatible with
15979 each other and also with @option{DEFAULT} and @option{STANDARD_INDENT}.
15980 @end itemize
15981
15982 @noindent
15983 The @option{GNAT}, @option{COMPACT}, and @option{UNCOMPACT} options for the
15984 @option{/CONSTRUCT_LAYOUT} qualifier are incompatible.
15985 @end ifset
15986
15987 @node General Text Layout Control
15988 @subsection General Text Layout Control
15989
15990 @noindent
15991 These switches allow control over line length and indentation.
15992
15993 @table @option
15994 @item ^-M@var{nnn}^/LINE_LENGTH_MAX=@var{nnn}^
15995 @cindex @option{^-M^/LINE_LENGTH^} (@command{gnatpp})
15996 Maximum line length, @var{nnn} from 32@dots{}256, the default value is 79
15997
15998 @item ^-i@var{nnn}^/INDENTATION_LEVEL=@var{nnn}^
15999 @cindex @option{^-i^/INDENTATION_LEVEL^} (@command{gnatpp})
16000 Indentation level, @var{nnn} from 1@dots{}9, the default value is 3
16001
16002 @item ^-cl@var{nnn}^/CONTINUATION_INDENT=@var{nnn}^
16003 @cindex @option{^-cl^/CONTINUATION_INDENT^} (@command{gnatpp})
16004 Indentation level for continuation lines (relative to the line being
16005 continued), @var{nnn} from 1@dots{}9.
16006 The default
16007 value is one less then the (normal) indentation level, unless the
16008 indentation is set to 1 (in which case the default value for continuation
16009 line indentation is also 1)
16010 @end table
16011
16012 @node Other Formatting Options
16013 @subsection Other Formatting Options
16014
16015 @noindent
16016 These switches control the inclusion of missing end/exit labels, and
16017 the indentation level in @b{case} statements.
16018
16019 @table @option
16020 @item ^-e^/NO_MISSED_LABELS^
16021 @cindex @option{^-e^/NO_MISSED_LABELS^} (@command{gnatpp})
16022 Do not insert missing end/exit labels. An end label is the name of
16023 a construct that may optionally be repeated at the end of the
16024 construct's declaration;
16025 e.g., the names of packages, subprograms, and tasks.
16026 An exit label is the name of a loop that may appear as target
16027 of an exit statement within the loop.
16028 By default, @command{gnatpp} inserts these end/exit labels when
16029 they are absent from the original source. This option suppresses such
16030 insertion, so that the formatted source reflects the original.
16031
16032 @item ^-ff^/FORM_FEED_AFTER_PRAGMA_PAGE^
16033 @cindex @option{^-ff^/FORM_FEED_AFTER_PRAGMA_PAGE^} (@command{gnatpp})
16034 Insert a Form Feed character after a pragma Page.
16035
16036 @item ^-T@var{nnn}^/MAX_INDENT=@var{nnn}^
16037 @cindex @option{^-T^/MAX_INDENT^} (@command{gnatpp})
16038 Do not use an additional indentation level for @b{case} alternatives
16039 and variants if there are @var{nnn} or more (the default
16040 value is 10).
16041 If @var{nnn} is 0, an additional indentation level is
16042 used for @b{case} alternatives and variants regardless of their number.
16043 @end table
16044
16045 @node Setting the Source Search Path
16046 @subsection Setting the Source Search Path
16047
16048 @noindent
16049 To define the search path for the input source file, @command{gnatpp}
16050 uses the same switches as the GNAT compiler, with the same effects.
16051
16052 @table @option
16053 @item ^-I^/SEARCH=^@var{dir}
16054 @cindex @option{^-I^/SEARCH^} (@code{gnatpp})
16055 The same as the corresponding gcc switch
16056
16057 @item ^-I-^/NOCURRENT_DIRECTORY^
16058 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatpp})
16059 The same as the corresponding gcc switch
16060
16061 @item ^-gnatec^/CONFIGURATION_PRAGMAS_FILE^=@var{path}
16062 @cindex @option{^-gnatec^/CONFIGURATION_PRAGMAS_FILE^} (@code{gnatpp})
16063 The same as the corresponding gcc switch
16064
16065 @item ^--RTS^/RUNTIME_SYSTEM^=@var{path}
16066 @cindex @option{^--RTS^/RUNTIME_SYSTEM^} (@code{gnatpp})
16067 The same as the corresponding gcc switch
16068
16069 @end table
16070
16071 @node Output File Control
16072 @subsection Output File Control
16073
16074 @noindent
16075 By default the output is sent to the file whose name is obtained by appending
16076 the ^@file{.pp}^@file{$PP}^ suffix to the name of the input file
16077 (if the file with this name already exists, it is unconditionally overwritten).
16078 Thus if the input file is @file{^my_ada_proc.adb^MY_ADA_PROC.ADB^} then
16079 @command{gnatpp} will produce @file{^my_ada_proc.adb.pp^MY_ADA_PROC.ADB$PP^}
16080 as output file.
16081 The output may be redirected by the following switches:
16082
16083 @table @option
16084 @item ^-pipe^/STANDARD_OUTPUT^
16085 @cindex @option{^-pipe^/STANDARD_OUTPUT^} (@code{gnatpp})
16086 Send the output to @code{Standard_Output}
16087
16088 @item ^-o @var{output_file}^/OUTPUT=@var{output_file}^
16089 @cindex @option{^-o^/OUTPUT^} (@code{gnatpp})
16090 Write the output into @var{output_file}.
16091 If @var{output_file} already exists, @command{gnatpp} terminates without
16092 reading or processing the input file.
16093
16094 @item ^-of ^/FORCED_OUTPUT=^@var{output_file}
16095 @cindex @option{^-of^/FORCED_OUTPUT^} (@code{gnatpp})
16096 Write the output into @var{output_file}, overwriting the existing file
16097 (if one is present).
16098
16099 @item ^-r^/REPLACE^
16100 @cindex @option{^-r^/REPLACE^} (@code{gnatpp})
16101 Replace the input source file with the reformatted output, and copy the
16102 original input source into the file whose name is obtained by appending the
16103 ^@file{.npp}^@file{$NPP}^ suffix to the name of the input file.
16104 If a file with this name already exists, @command{gnatpp} terminates without
16105 reading or processing the input file.
16106
16107 @item ^-rf^/OVERRIDING_REPLACE^
16108 @cindex @option{^-rf^/OVERRIDING_REPLACE^} (@code{gnatpp})
16109 Like @option{^-r^/REPLACE^} except that if the file with the specified name
16110 already exists, it is overwritten.
16111
16112 @item ^-rnb^/REPLACE_NO_BACKUP^
16113 @cindex @option{^-rnb^/REPLACE_NO_BACKUP^} (@code{gnatpp})
16114 Replace the input source file with the reformatted output without
16115 creating any backup copy of the input source.
16116
16117 @item ^--eol=@var{xxx}^/END_OF_LINE=@var{xxx}^
16118 @cindex @option{^--eol^/END_OF_LINE^} (@code{gnatpp})
16119 Specifies the format of the reformatted output file. The @var{xxx}
16120 ^string specified with the switch^option^ may be either
16121 @itemize @bullet
16122 @item ``@option{^dos^DOS^}'' MS DOS style, lines end with CR LF characters
16123 @item ``@option{^crlf^CRLF^}''
16124 the same as @option{^crlf^CRLF^}
16125 @item ``@option{^unix^UNIX^}'' UNIX style, lines end with LF character
16126 @item ``@option{^lf^LF^}''
16127 the same as @option{^unix^UNIX^}
16128 @end itemize
16129
16130 @item ^-W^/RESULT_ENCODING=^@var{e}
16131 @cindex @option{^-W^/RESULT_ENCODING=^} (@command{gnatpp})
16132 Specify the wide character encoding method used to write the code in the
16133 result file
16134 @var{e} is one of the following:
16135
16136 @itemize @bullet
16137
16138 @item ^h^HEX^
16139 Hex encoding
16140
16141 @item ^u^UPPER^
16142 Upper half encoding
16143
16144 @item ^s^SHIFT_JIS^
16145 Shift/JIS encoding
16146
16147 @item ^e^EUC^
16148 EUC encoding
16149
16150 @item ^8^UTF8^
16151 UTF-8 encoding
16152
16153 @item ^b^BRACKETS^
16154 Brackets encoding (default value)
16155 @end itemize
16156
16157 @end table
16158
16159 @noindent
16160 Options @option{^-pipe^/STANDARD_OUTPUT^},
16161 @option{^-o^/OUTPUT^} and
16162 @option{^-of^/FORCED_OUTPUT^} are allowed only if the call to gnatpp
16163 contains only one file to reformat.
16164 Option
16165 @option{^--eol^/END_OF_LINE^}
16166 and
16167 @option{^-W^/RESULT_ENCODING^}
16168 cannot be used together
16169 with @option{^-pipe^/STANDARD_OUTPUT^} option.
16170
16171 @node Other gnatpp Switches
16172 @subsection Other @code{gnatpp} Switches
16173
16174 @noindent
16175 The additional @command{gnatpp} switches are defined in this subsection.
16176
16177 @table @option
16178 @item ^-files @var{filename}^/FILES=@var{output_file}^
16179 @cindex @option{^-files^/FILES^} (@code{gnatpp})
16180 Take the argument source files from the specified file. This file should be an
16181 ordinary textual file containing file names separated by spaces or
16182 line breaks. You can use this switch more then once in the same call to
16183 @command{gnatpp}. You also can combine this switch with explicit list of
16184 files.
16185
16186 @item ^-v^/VERBOSE^
16187 @cindex @option{^-v^/VERBOSE^} (@code{gnatpp})
16188 Verbose mode;
16189 @command{gnatpp} generates version information and then
16190 a trace of the actions it takes to produce or obtain the ASIS tree.
16191
16192 @item ^-w^/WARNINGS^
16193 @cindex @option{^-w^/WARNINGS^} (@code{gnatpp})
16194 Warning mode;
16195 @command{gnatpp} generates a warning whenever it cannot provide
16196 a required layout in the result source.
16197 @end table
16198
16199 @node Formatting Rules
16200 @section Formatting Rules
16201
16202 @noindent
16203 The following subsections show how @command{gnatpp} treats ``white space'',
16204 comments, program layout, and name casing.
16205 They provide the detailed descriptions of the switches shown above.
16206
16207 @menu
16208 * White Space and Empty Lines::
16209 * Formatting Comments::
16210 * Construct Layout::
16211 * Name Casing::
16212 @end menu
16213
16214 @node White Space and Empty Lines
16215 @subsection White Space and Empty Lines
16216
16217 @noindent
16218 @command{gnatpp} does not have an option to control space characters.
16219 It will add or remove spaces according to the style illustrated by the
16220 examples in the @cite{Ada Reference Manual}.
16221
16222 The only format effectors
16223 (see @cite{Ada Reference Manual}, paragraph 2.1(13))
16224 that will appear in the output file are platform-specific line breaks,
16225 and also format effectors within (but not at the end of) comments.
16226 In particular, each horizontal tab character that is not inside
16227 a comment will be treated as a space and thus will appear in the
16228 output file as zero or more spaces depending on
16229 the reformatting of the line in which it appears.
16230 The only exception is a Form Feed character, which is inserted after a
16231 pragma @code{Page} when @option{-ff} is set.
16232
16233 The output file will contain no lines with trailing ``white space'' (spaces,
16234 format effectors).
16235
16236 Empty lines in the original source are preserved
16237 only if they separate declarations or statements.
16238 In such contexts, a
16239 sequence of two or more empty lines is replaced by exactly one empty line.
16240 Note that a blank line will be removed if it separates two ``comment blocks''
16241 (a comment block is a sequence of whole-line comments).
16242 In order to preserve a visual separation between comment blocks, use an
16243 ``empty comment'' (a line comprising only hyphens) rather than an empty line.
16244 Likewise, if for some reason you wish to have a sequence of empty lines,
16245 use a sequence of empty comments instead.
16246
16247 @node Formatting Comments
16248 @subsection Formatting Comments
16249
16250 @noindent
16251 Comments in Ada code are of two kinds:
16252 @itemize @bullet
16253 @item
16254 a @emph{whole-line comment}, which appears by itself (possibly preceded by
16255 ``white space'') on a line
16256
16257 @item
16258 an @emph{end-of-line comment}, which follows some other Ada lexical element
16259 on the same line.
16260 @end itemize
16261
16262 @noindent
16263 The indentation of a whole-line comment is that of either
16264 the preceding or following line in
16265 the formatted source, depending on switch settings as will be described below.
16266
16267 For an end-of-line comment, @command{gnatpp} leaves the same number of spaces
16268 between the end of the preceding Ada lexical element and the beginning
16269 of the comment as appear in the original source,
16270 unless either the comment has to be split to
16271 satisfy the line length limitation, or else the next line contains a
16272 whole line comment that is considered a continuation of this end-of-line
16273 comment (because it starts at the same position).
16274 In the latter two
16275 cases, the start of the end-of-line comment is moved right to the nearest
16276 multiple of the indentation level.
16277 This may result in a ``line overflow'' (the right-shifted comment extending
16278 beyond the maximum line length), in which case the comment is split as
16279 described below.
16280
16281 There is a difference between @option{^-c1^/COMMENTS_LAYOUT=DEFAULT^}
16282 (GNAT-style comment line indentation)
16283 and @option{^-c2^/COMMENTS_LAYOUT=STANDARD_INDENT^}
16284 (reference-manual comment line indentation).
16285 With reference-manual style, a whole-line comment is indented as if it
16286 were a declaration or statement at the same place
16287 (i.e., according to the indentation of the preceding line(s)).
16288 With GNAT style, a whole-line comment that is immediately followed by an
16289 @b{if} or @b{case} statement alternative, a record variant, or the reserved
16290 word @b{begin}, is indented based on the construct that follows it.
16291
16292 For example:
16293 @smallexample @c ada
16294 @cartouche
16295 if A then
16296 null;
16297 -- some comment
16298 else
16299 null;
16300 end if;
16301 @end cartouche
16302 @end smallexample
16303
16304 @noindent
16305 Reference-manual indentation produces:
16306
16307 @smallexample @c ada
16308 @cartouche
16309 if A then
16310 null;
16311 -- some comment
16312 else
16313 null;
16314 end if;
16315 @end cartouche
16316 @end smallexample
16317
16318 @noindent
16319 while GNAT-style indentation produces:
16320
16321 @smallexample @c ada
16322 @cartouche
16323 if A then
16324 null;
16325 -- some comment
16326 else
16327 null;
16328 end if;
16329 @end cartouche
16330 @end smallexample
16331
16332 @noindent
16333 The @option{^-c3^/COMMENTS_LAYOUT=GNAT_BEGINNING^} switch
16334 (GNAT style comment beginning) has the following
16335 effect:
16336
16337 @itemize @bullet
16338 @item
16339 For each whole-line comment that does not end with two hyphens,
16340 @command{gnatpp} inserts spaces if necessary after the starting two hyphens
16341 to ensure that there are at least two spaces between these hyphens and the
16342 first non-blank character of the comment.
16343 @end itemize
16344
16345 @noindent
16346 For an end-of-line comment, if in the original source the next line is a
16347 whole-line comment that starts at the same position
16348 as the end-of-line comment,
16349 then the whole-line comment (and all whole-line comments
16350 that follow it and that start at the same position)
16351 will start at this position in the output file.
16352
16353 @noindent
16354 That is, if in the original source we have:
16355
16356 @smallexample @c ada
16357 @cartouche
16358 begin
16359 A := B + C; -- B must be in the range Low1..High1
16360 -- C must be in the range Low2..High2
16361 --B+C will be in the range Low1+Low2..High1+High2
16362 X := X + 1;
16363 @end cartouche
16364 @end smallexample
16365
16366 @noindent
16367 Then in the formatted source we get
16368
16369 @smallexample @c ada
16370 @cartouche
16371 begin
16372 A := B + C; -- B must be in the range Low1..High1
16373 -- C must be in the range Low2..High2
16374 -- B+C will be in the range Low1+Low2..High1+High2
16375 X := X + 1;
16376 @end cartouche
16377 @end smallexample
16378
16379 @noindent
16380 A comment that exceeds the line length limit will be split.
16381 Unless switch
16382 @option{^-c4^/COMMENTS_LAYOUT=REFORMAT^} (reformat comment blocks) is set and
16383 the line belongs to a reformattable block, splitting the line generates a
16384 @command{gnatpp} warning.
16385 The @option{^-c4^/COMMENTS_LAYOUT=REFORMAT^} switch specifies that whole-line
16386 comments may be reformatted in typical
16387 word processor style (that is, moving words between lines and putting as
16388 many words in a line as possible).
16389
16390 @noindent
16391 The @option{^-c5^/COMMENTS_LAYOUT=KEEP_SPECIAL^} switch specifies, that comments
16392 that has a special format (that is, a character that is neither a letter nor digit
16393 not white space nor line break immediately following the leading @code{--} of
16394 the comment) should be without any change moved from the argument source
16395 into reformatted source. This switch allows to preserve comments that are used
16396 as a special marks in the code (e.g.@: SPARK annotation).
16397
16398 @node Construct Layout
16399 @subsection Construct Layout
16400
16401 @noindent
16402 In several cases the suggested layout in the Ada Reference Manual includes
16403 an extra level of indentation that many programmers prefer to avoid. The
16404 affected cases include:
16405
16406 @itemize @bullet
16407
16408 @item Record type declaration (RM 3.8)
16409
16410 @item Record representation clause (RM 13.5.1)
16411
16412 @item Loop statement in case if a loop has a statement identifier (RM 5.6)
16413
16414 @item Block statement in case if a block has a statement identifier (RM 5.6)
16415 @end itemize
16416
16417 @noindent
16418 In compact mode (when GNAT style layout or compact layout is set),
16419 the pretty printer uses one level of indentation instead
16420 of two. This is achieved in the record definition and record representation
16421 clause cases by putting the @code{record} keyword on the same line as the
16422 start of the declaration or representation clause, and in the block and loop
16423 case by putting the block or loop header on the same line as the statement
16424 identifier.
16425
16426 @noindent
16427 The difference between GNAT style @option{^-l1^/CONSTRUCT_LAYOUT=GNAT^}
16428 and compact @option{^-l2^/CONSTRUCT_LAYOUT=COMPACT^}
16429 layout on the one hand, and uncompact layout
16430 @option{^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^} on the other hand,
16431 can be illustrated by the following examples:
16432
16433 @iftex
16434 @cartouche
16435 @multitable @columnfractions .5 .5
16436 @item @i{GNAT style, compact layout} @tab @i{Uncompact layout}
16437
16438 @item
16439 @smallexample @c ada
16440 type q is record
16441 a : integer;
16442 b : integer;
16443 end record;
16444 @end smallexample
16445 @tab
16446 @smallexample @c ada
16447 type q is
16448 record
16449 a : integer;
16450 b : integer;
16451 end record;
16452 @end smallexample
16453
16454 @item
16455 @smallexample @c ada
16456 for q use record
16457 a at 0 range 0 .. 31;
16458 b at 4 range 0 .. 31;
16459 end record;
16460 @end smallexample
16461 @tab
16462 @smallexample @c ada
16463 for q use
16464 record
16465 a at 0 range 0 .. 31;
16466 b at 4 range 0 .. 31;
16467 end record;
16468 @end smallexample
16469
16470 @item
16471 @smallexample @c ada
16472 Block : declare
16473 A : Integer := 3;
16474 begin
16475 Proc (A, A);
16476 end Block;
16477 @end smallexample
16478 @tab
16479 @smallexample @c ada
16480 Block :
16481 declare
16482 A : Integer := 3;
16483 begin
16484 Proc (A, A);
16485 end Block;
16486 @end smallexample
16487
16488 @item
16489 @smallexample @c ada
16490 Clear : for J in 1 .. 10 loop
16491 A (J) := 0;
16492 end loop Clear;
16493 @end smallexample
16494 @tab
16495 @smallexample @c ada
16496 Clear :
16497 for J in 1 .. 10 loop
16498 A (J) := 0;
16499 end loop Clear;
16500 @end smallexample
16501 @end multitable
16502 @end cartouche
16503 @end iftex
16504
16505 @ifnottex
16506 @smallexample
16507 @cartouche
16508 GNAT style, compact layout Uncompact layout
16509
16510 type q is record type q is
16511 a : integer; record
16512 b : integer; a : integer;
16513 end record; b : integer;
16514 end record;
16515
16516 for q use record for q use
16517 a at 0 range 0 .. 31; record
16518 b at 4 range 0 .. 31; a at 0 range 0 .. 31;
16519 end record; b at 4 range 0 .. 31;
16520 end record;
16521
16522 Block : declare Block :
16523 A : Integer := 3; declare
16524 begin A : Integer := 3;
16525 Proc (A, A); begin
16526 end Block; Proc (A, A);
16527 end Block;
16528
16529 Clear : for J in 1 .. 10 loop Clear :
16530 A (J) := 0; for J in 1 .. 10 loop
16531 end loop Clear; A (J) := 0;
16532 end loop Clear;
16533 @end cartouche
16534 @end smallexample
16535 @end ifnottex
16536
16537 @noindent
16538 A further difference between GNAT style layout and compact layout is that
16539 GNAT style layout inserts empty lines as separation for
16540 compound statements, return statements and bodies.
16541
16542 Note that the layout specified by
16543 @option{^--separate-stmt-name^/STMT_NAME_ON_NEW_LINE^}
16544 for named block and loop statements overrides the layout defined by these
16545 constructs by @option{^-l1^/CONSTRUCT_LAYOUT=GNAT^},
16546 @option{^-l2^/CONSTRUCT_LAYOUT=COMPACT^} or
16547 @option{^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^} option.
16548
16549 @node Name Casing
16550 @subsection Name Casing
16551
16552 @noindent
16553 @command{gnatpp} always converts the usage occurrence of a (simple) name to
16554 the same casing as the corresponding defining identifier.
16555
16556 You control the casing for defining occurrences via the
16557 @option{^-n^/NAME_CASING^} switch.
16558 @ifclear vms
16559 With @option{-nD} (``as declared'', which is the default),
16560 @end ifclear
16561 @ifset vms
16562 With @option{/NAME_CASING=AS_DECLARED}, which is the default,
16563 @end ifset
16564 defining occurrences appear exactly as in the source file
16565 where they are declared.
16566 The other ^values for this switch^options for this qualifier^ ---
16567 @option{^-nU^UPPER_CASE^},
16568 @option{^-nL^LOWER_CASE^},
16569 @option{^-nM^MIXED_CASE^} ---
16570 result in
16571 ^upper, lower, or mixed case, respectively^the corresponding casing^.
16572 If @command{gnatpp} changes the casing of a defining
16573 occurrence, it analogously changes the casing of all the
16574 usage occurrences of this name.
16575
16576 If the defining occurrence of a name is not in the source compilation unit
16577 currently being processed by @command{gnatpp}, the casing of each reference to
16578 this name is changed according to the value of the @option{^-n^/NAME_CASING^}
16579 switch (subject to the dictionary file mechanism described below).
16580 Thus @command{gnatpp} acts as though the @option{^-n^/NAME_CASING^} switch
16581 had affected the
16582 casing for the defining occurrence of the name.
16583
16584 Some names may need to be spelled with casing conventions that are not
16585 covered by the upper-, lower-, and mixed-case transformations.
16586 You can arrange correct casing by placing such names in a
16587 @emph{dictionary file},
16588 and then supplying a @option{^-D^/DICTIONARY^} switch.
16589 The casing of names from dictionary files overrides
16590 any @option{^-n^/NAME_CASING^} switch.
16591
16592 To handle the casing of Ada predefined names and the names from GNAT libraries,
16593 @command{gnatpp} assumes a default dictionary file.
16594 The name of each predefined entity is spelled with the same casing as is used
16595 for the entity in the @cite{Ada Reference Manual}.
16596 The name of each entity in the GNAT libraries is spelled with the same casing
16597 as is used in the declaration of that entity.
16598
16599 The @w{@option{^-D-^/SPECIFIC_CASING^}} switch suppresses the use of the
16600 default dictionary file.
16601 Instead, the casing for predefined and GNAT-defined names will be established
16602 by the @option{^-n^/NAME_CASING^} switch or explicit dictionary files.
16603 For example, by default the names @code{Ada.Text_IO} and @code{GNAT.OS_Lib}
16604 will appear as just shown,
16605 even in the presence of a @option{^-nU^/NAME_CASING=UPPER_CASE^} switch.
16606 To ensure that even such names are rendered in uppercase,
16607 additionally supply the @w{@option{^-D-^/SPECIFIC_CASING^}} switch
16608 (or else, less conveniently, place these names in upper case in a dictionary
16609 file).
16610
16611 A dictionary file is
16612 a plain text file; each line in this file can be either a blank line
16613 (containing only space characters and ASCII.HT characters), an Ada comment
16614 line, or the specification of exactly one @emph{casing schema}.
16615
16616 A casing schema is a string that has the following syntax:
16617
16618 @smallexample
16619 @cartouche
16620 @var{casing_schema} ::= @var{identifier} | *@var{simple_identifier}*
16621
16622 @var{simple_identifier} ::= @var{letter}@{@var{letter_or_digit}@}
16623 @end cartouche
16624 @end smallexample
16625
16626 @noindent
16627 (See @cite{Ada Reference Manual}, Section 2.3) for the definition of the
16628 @var{identifier} lexical element and the @var{letter_or_digit} category.)
16629
16630 The casing schema string can be followed by white space and/or an Ada-style
16631 comment; any amount of white space is allowed before the string.
16632
16633 If a dictionary file is passed as
16634 @ifclear vms
16635 the value of a @option{-D@var{file}} switch
16636 @end ifclear
16637 @ifset vms
16638 an option to the @option{/DICTIONARY} qualifier
16639 @end ifset
16640 then for every
16641 simple name and every identifier, @command{gnatpp} checks if the dictionary
16642 defines the casing for the name or for some of its parts (the term ``subword''
16643 is used below to denote the part of a name which is delimited by ``_'' or by
16644 the beginning or end of the word and which does not contain any ``_'' inside):
16645
16646 @itemize @bullet
16647 @item
16648 if the whole name is in the dictionary, @command{gnatpp} uses for this name
16649 the casing defined by the dictionary; no subwords are checked for this word
16650
16651 @item
16652 for every subword @command{gnatpp} checks if the dictionary contains the
16653 corresponding string of the form @code{*@var{simple_identifier}*},
16654 and if it does, the casing of this @var{simple_identifier} is used
16655 for this subword
16656
16657 @item
16658 if the whole name does not contain any ``_'' inside, and if for this name
16659 the dictionary contains two entries - one of the form @var{identifier},
16660 and another - of the form *@var{simple_identifier}*, then the first one
16661 is applied to define the casing of this name
16662
16663 @item
16664 if more than one dictionary file is passed as @command{gnatpp} switches, each
16665 dictionary adds new casing exceptions and overrides all the existing casing
16666 exceptions set by the previous dictionaries
16667
16668 @item
16669 when @command{gnatpp} checks if the word or subword is in the dictionary,
16670 this check is not case sensitive
16671 @end itemize
16672
16673 @noindent
16674 For example, suppose we have the following source to reformat:
16675
16676 @smallexample @c ada
16677 @cartouche
16678 procedure test is
16679 name1 : integer := 1;
16680 name4_name3_name2 : integer := 2;
16681 name2_name3_name4 : Boolean;
16682 name1_var : Float;
16683 begin
16684 name2_name3_name4 := name4_name3_name2 > name1;
16685 end;
16686 @end cartouche
16687 @end smallexample
16688
16689 @noindent
16690 And suppose we have two dictionaries:
16691
16692 @smallexample
16693 @cartouche
16694 @i{dict1:}
16695 NAME1
16696 *NaMe3*
16697 *Name1*
16698 @end cartouche
16699
16700 @cartouche
16701 @i{dict2:}
16702 *NAME3*
16703 @end cartouche
16704 @end smallexample
16705
16706 @noindent
16707 If @command{gnatpp} is called with the following switches:
16708
16709 @smallexample
16710 @ifclear vms
16711 @command{gnatpp -nM -D dict1 -D dict2 test.adb}
16712 @end ifclear
16713 @ifset vms
16714 @command{gnatpp test.adb /NAME_CASING=MIXED_CASE /DICTIONARY=(dict1, dict2)}
16715 @end ifset
16716 @end smallexample
16717
16718 @noindent
16719 then we will get the following name casing in the @command{gnatpp} output:
16720
16721 @smallexample @c ada
16722 @cartouche
16723 procedure Test is
16724 NAME1 : Integer := 1;
16725 Name4_NAME3_Name2 : Integer := 2;
16726 Name2_NAME3_Name4 : Boolean;
16727 Name1_Var : Float;
16728 begin
16729 Name2_NAME3_Name4 := Name4_NAME3_Name2 > NAME1;
16730 end Test;
16731 @end cartouche
16732 @end smallexample
16733
16734 @c *********************************
16735 @node The GNAT Metric Tool gnatmetric
16736 @chapter The GNAT Metric Tool @command{gnatmetric}
16737 @findex gnatmetric
16738 @cindex Metric tool
16739
16740 @noindent
16741 ^The @command{gnatmetric} tool^@command{GNAT METRIC}^ is an ASIS-based utility
16742 for computing various program metrics.
16743 It takes an Ada source file as input and generates a file containing the
16744 metrics data as output. Various switches control which
16745 metrics are computed and output.
16746
16747 @command{gnatmetric} generates and uses the ASIS
16748 tree for the input source and thus requires the input to be syntactically and
16749 semantically legal.
16750 If this condition is not met, @command{gnatmetric} will generate
16751 an error message; no metric information for this file will be
16752 computed and reported.
16753
16754 If the compilation unit contained in the input source depends semantically
16755 upon units in files located outside the current directory, you have to provide
16756 the source search path when invoking @command{gnatmetric}.
16757 If it depends semantically upon units that are contained
16758 in files with names that do not follow the GNAT file naming rules, you have to
16759 provide the configuration file describing the corresponding naming scheme (see
16760 the description of the @command{gnatmetric} switches below.)
16761 Alternatively, you may use a project file and invoke @command{gnatmetric}
16762 through the @command{gnat} driver.
16763
16764 The @command{gnatmetric} command has the form
16765
16766 @smallexample
16767 $ gnatmetric @ovar{switches} @{@var{filename}@} @r{[}-cargs @var{gcc_switches}@r{]}
16768 @end smallexample
16769
16770 @noindent
16771 where
16772 @itemize @bullet
16773 @item
16774 @var{switches} specify the metrics to compute and define the destination for
16775 the output
16776
16777 @item
16778 Each @var{filename} is the name (including the extension) of a source
16779 file to process. ``Wildcards'' are allowed, and
16780 the file name may contain path information.
16781 If no @var{filename} is supplied, then the @var{switches} list must contain
16782 at least one
16783 @option{-files} switch (@pxref{Other gnatmetric Switches}).
16784 Including both a @option{-files} switch and one or more
16785 @var{filename} arguments is permitted.
16786
16787 @item
16788 @samp{-cargs @var{gcc_switches}} is a list of switches for
16789 @command{gcc}. They will be passed on to all compiler invocations made by
16790 @command{gnatmetric} to generate the ASIS trees. Here you can provide
16791 @option{^-I^/INCLUDE_DIRS=^} switches to form the source search path,
16792 and use the @option{-gnatec} switch to set the configuration file.
16793 @end itemize
16794
16795 @menu
16796 * Switches for gnatmetric::
16797 @end menu
16798
16799 @node Switches for gnatmetric
16800 @section Switches for @command{gnatmetric}
16801
16802 @noindent
16803 The following subsections describe the various switches accepted by
16804 @command{gnatmetric}, organized by category.
16805
16806 @menu
16807 * Output Files Control::
16808 * Disable Metrics For Local Units::
16809 * Specifying a set of metrics to compute::
16810 * Other gnatmetric Switches::
16811 * Generate project-wide metrics::
16812 @end menu
16813
16814 @node Output Files Control
16815 @subsection Output File Control
16816 @cindex Output file control in @command{gnatmetric}
16817
16818 @noindent
16819 @command{gnatmetric} has two output formats. It can generate a
16820 textual (human-readable) form, and also XML. By default only textual
16821 output is generated.
16822
16823 When generating the output in textual form, @command{gnatmetric} creates
16824 for each Ada source file a corresponding text file
16825 containing the computed metrics, except for the case when the set of metrics
16826 specified by gnatmetric parameters consists only of metrics that are computed
16827 for the whole set of analyzed sources, but not for each Ada source.
16828 By default, this file is placed in the same directory as where the source
16829 file is located, and its name is obtained
16830 by appending the ^@file{.metrix}^@file{$METRIX}^ suffix to the name of the
16831 input file.
16832
16833 All the output information generated in XML format is placed in a single
16834 file. By default this file is placed in the current directory and has the
16835 name ^@file{metrix.xml}^@file{METRIX$XML}^.
16836
16837 Some of the computed metrics are summed over the units passed to
16838 @command{gnatmetric}; for example, the total number of lines of code.
16839 By default this information is sent to @file{stdout}, but a file
16840 can be specified with the @option{-og} switch.
16841
16842 The following switches control the @command{gnatmetric} output:
16843
16844 @table @option
16845 @cindex @option{^-x^/XML^} (@command{gnatmetric})
16846 @item ^-x^/XML^
16847 Generate the XML output
16848
16849 @cindex @option{^-nt^/NO_TEXT^} (@command{gnatmetric})
16850 @item ^-nt^/NO_TEXT^
16851 Do not generate the output in text form (implies @option{^-x^/XML^})
16852
16853 @cindex @option{^-d^/DIRECTORY^} (@command{gnatmetric})
16854 @item ^-d @var{output_dir}^/DIRECTORY=@var{output_dir}^
16855 Put textual files with detailed metrics into @var{output_dir}
16856
16857 @cindex @option{^-o^/SUFFIX_DETAILS^} (@command{gnatmetric})
16858 @item ^-o @var{file_suffix}^/SUFFIX_DETAILS=@var{file_suffix}^
16859 Use @var{file_suffix}, instead of ^@file{.metrix}^@file{$METRIX}^
16860 in the name of the output file.
16861
16862 @cindex @option{^-og^/GLOBAL_OUTPUT^} (@command{gnatmetric})
16863 @item ^-og @var{file_name}^/GLOBAL_OUTPUT=@var{file_name}^
16864 Put global metrics into @var{file_name}
16865
16866 @cindex @option{^-ox^/XML_OUTPUT^} (@command{gnatmetric})
16867 @item ^-ox @var{file_name}^/XML_OUTPUT=@var{file_name}^
16868 Put the XML output into @var{file_name} (also implies @option{^-x^/XML^})
16869
16870 @cindex @option{^-sfn^/SHORT_SOURCE_FILE_NAME^} (@command{gnatmetric})
16871 @item ^-sfn^/SHORT_SOURCE_FILE_NAME^
16872 Use ``short'' source file names in the output. (The @command{gnatmetric}
16873 output includes the name(s) of the Ada source file(s) from which the metrics
16874 are computed. By default each name includes the absolute path. The
16875 @option{^-sfn^/SHORT_SOURCE_FILE_NAME^} switch causes @command{gnatmetric}
16876 to exclude all directory information from the file names that are output.)
16877
16878 @end table
16879
16880 @node Disable Metrics For Local Units
16881 @subsection Disable Metrics For Local Units
16882 @cindex Disable Metrics For Local Units in @command{gnatmetric}
16883
16884 @noindent
16885 @command{gnatmetric} relies on the GNAT compilation model @minus{}
16886 one compilation
16887 unit per one source file. It computes line metrics for the whole source
16888 file, and it also computes syntax
16889 and complexity metrics for the file's outermost unit.
16890
16891 By default, @command{gnatmetric} will also compute all metrics for certain
16892 kinds of locally declared program units:
16893
16894 @itemize @bullet
16895 @item
16896 subprogram (and generic subprogram) bodies;
16897
16898 @item
16899 package (and generic package) specs and bodies;
16900
16901 @item
16902 task object and type specifications and bodies;
16903
16904 @item
16905 protected object and type specifications and bodies.
16906 @end itemize
16907
16908 @noindent
16909 These kinds of entities will be referred to as
16910 @emph{eligible local program units}, or simply @emph{eligible local units},
16911 @cindex Eligible local unit (for @command{gnatmetric})
16912 in the discussion below.
16913
16914 Note that a subprogram declaration, generic instantiation,
16915 or renaming declaration only receives metrics
16916 computation when it appear as the outermost entity
16917 in a source file.
16918
16919 Suppression of metrics computation for eligible local units can be
16920 obtained via the following switch:
16921
16922 @table @option
16923 @cindex @option{^-n@var{x}^/SUPPRESS^} (@command{gnatmetric})
16924 @item ^-nolocal^/SUPPRESS=LOCAL_DETAILS^
16925 Do not compute detailed metrics for eligible local program units
16926
16927 @end table
16928
16929 @node Specifying a set of metrics to compute
16930 @subsection Specifying a set of metrics to compute
16931
16932 @noindent
16933 By default all the metrics are computed and reported. The switches
16934 described in this subsection allow you to control, on an individual
16935 basis, whether metrics are computed and
16936 reported. If at least one positive metric
16937 switch is specified (that is, a switch that defines that a given
16938 metric or set of metrics is to be computed), then only
16939 explicitly specified metrics are reported.
16940
16941 @menu
16942 * Line Metrics Control::
16943 * Syntax Metrics Control::
16944 * Complexity Metrics Control::
16945 * Object-Oriented Metrics Control::
16946 @end menu
16947
16948 @node Line Metrics Control
16949 @subsubsection Line Metrics Control
16950 @cindex Line metrics control in @command{gnatmetric}
16951
16952 @noindent
16953 For any (legal) source file, and for each of its
16954 eligible local program units, @command{gnatmetric} computes the following
16955 metrics:
16956
16957 @itemize @bullet
16958 @item
16959 the total number of lines;
16960
16961 @item
16962 the total number of code lines (i.e., non-blank lines that are not comments)
16963
16964 @item
16965 the number of comment lines
16966
16967 @item
16968 the number of code lines containing end-of-line comments;
16969
16970 @item
16971 the comment percentage: the ratio between the number of lines that contain
16972 comments and the number of all non-blank lines, expressed as a percentage;
16973
16974 @item
16975 the number of empty lines and lines containing only space characters and/or
16976 format effectors (blank lines)
16977
16978 @item
16979 the average number of code lines in subprogram bodies, task bodies, entry
16980 bodies and statement sequences in package bodies (this metric is only computed
16981 across the whole set of the analyzed units)
16982
16983 @end itemize
16984
16985 @noindent
16986 @command{gnatmetric} sums the values of the line metrics for all the
16987 files being processed and then generates the cumulative results. The tool
16988 also computes for all the files being processed the average number of code
16989 lines in bodies.
16990
16991 You can use the following switches to select the specific line metrics
16992 to be computed and reported.
16993
16994 @table @option
16995 @cindex @option{^--lines@var{x}^/LINE_COUNT_METRICS^} (@command{gnatmetric})
16996
16997 @ifclear vms
16998 @cindex @option{--no-lines@var{x}}
16999 @end ifclear
17000
17001 @item ^--lines-all^/LINE_COUNT_METRICS=ALL_ON^
17002 Report all the line metrics
17003
17004 @item ^--no-lines-all^/LINE_COUNT_METRICS=ALL_OFF^
17005 Do not report any of line metrics
17006
17007 @item ^--lines^/LINE_COUNT_METRICS=ALL_LINES_ON^
17008 Report the number of all lines
17009
17010 @item ^--no-lines^/LINE_COUNT_METRICS=ALL_LINES_OFF^
17011 Do not report the number of all lines
17012
17013 @item ^--lines-code^/LINE_COUNT_METRICS=CODE_LINES_ON^
17014 Report the number of code lines
17015
17016 @item ^--no-lines-code^/LINE_COUNT_METRICS=CODE_LINES_OFF^
17017 Do not report the number of code lines
17018
17019 @item ^--lines-comment^/LINE_COUNT_METRICS=COMMENT_LINES_ON^
17020 Report the number of comment lines
17021
17022 @item ^--no-lines-comment^/LINE_COUNT_METRICS=COMMENT_LINES_OFF^
17023 Do not report the number of comment lines
17024
17025 @item ^--lines-eol-comment^/LINE_COUNT_METRICS=CODE_COMMENT_LINES_ON^
17026 Report the number of code lines containing
17027 end-of-line comments
17028
17029 @item ^--no-lines-eol-comment^/LINE_COUNT_METRICS=CODE_COMMENT_LINES_OFF^
17030 Do not report the number of code lines containing
17031 end-of-line comments
17032
17033 @item ^--lines-ratio^/LINE_COUNT_METRICS=COMMENT_PERCENTAGE_ON^
17034 Report the comment percentage in the program text
17035
17036 @item ^--no-lines-ratio^/LINE_COUNT_METRICS=COMMENT_PERCENTAGE_OFF^
17037 Do not report the comment percentage in the program text
17038
17039 @item ^--lines-blank^/LINE_COUNT_METRICS=BLANK_LINES_ON^
17040 Report the number of blank lines
17041
17042 @item ^--no-lines-blank^/LINE_COUNT_METRICS=BLANK_LINES_OFF^
17043 Do not report the number of blank lines
17044
17045 @item ^--lines-average^/LINE_COUNT_METRICS=AVERAGE_BODY_LINES_ON^
17046 Report the average number of code lines in subprogram bodies, task bodies,
17047 entry bodies and statement sequences in package bodies. The metric is computed
17048 and reported for the whole set of processed Ada sources only.
17049
17050 @item ^--no-lines-average^/LINE_COUNT_METRICS=AVERAGE_BODY_LINES_OFF^
17051 Do not report the average number of code lines in subprogram bodies,
17052 task bodies, entry bodies and statement sequences in package bodies.
17053
17054 @end table
17055
17056 @node Syntax Metrics Control
17057 @subsubsection Syntax Metrics Control
17058 @cindex Syntax metrics control in @command{gnatmetric}
17059
17060 @noindent
17061 @command{gnatmetric} computes various syntactic metrics for the
17062 outermost unit and for each eligible local unit:
17063
17064 @table @emph
17065 @item LSLOC (``Logical Source Lines Of Code'')
17066 The total number of declarations and the total number of statements
17067
17068 @item Maximal static nesting level of inner program units
17069 According to
17070 @cite{Ada Reference Manual}, 10.1(1), ``A program unit is either a
17071 package, a task unit, a protected unit, a
17072 protected entry, a generic unit, or an explicitly declared subprogram other
17073 than an enumeration literal.''
17074
17075 @item Maximal nesting level of composite syntactic constructs
17076 This corresponds to the notion of the
17077 maximum nesting level in the GNAT built-in style checks
17078 (@pxref{Style Checking})
17079 @end table
17080
17081 @noindent
17082 For the outermost unit in the file, @command{gnatmetric} additionally computes
17083 the following metrics:
17084
17085 @table @emph
17086 @item Public subprograms
17087 This metric is computed for package specs. It is the
17088 number of subprograms and generic subprograms declared in the visible
17089 part (including the visible part of nested packages, protected objects, and
17090 protected types).
17091
17092 @item All subprograms
17093 This metric is computed for bodies and subunits. The
17094 metric is equal to a total number of subprogram bodies in the compilation
17095 unit.
17096 Neither generic instantiations nor renamings-as-a-body nor body stubs
17097 are counted. Any subprogram body is counted, independently of its nesting
17098 level and enclosing constructs. Generic bodies and bodies of protected
17099 subprograms are counted in the same way as ``usual'' subprogram bodies.
17100
17101 @item Public types
17102 This metric is computed for package specs and
17103 generic package declarations. It is the total number of types
17104 that can be referenced from outside this compilation unit, plus the
17105 number of types from all the visible parts of all the visible generic
17106 packages. Generic formal types are not counted. Only types, not subtypes,
17107 are included.
17108
17109 @noindent
17110 Along with the total number of public types, the following
17111 types are counted and reported separately:
17112
17113 @itemize @bullet
17114 @item
17115 Abstract types
17116
17117 @item
17118 Root tagged types (abstract, non-abstract, private, non-private). Type
17119 extensions are @emph{not} counted
17120
17121 @item
17122 Private types (including private extensions)
17123
17124 @item
17125 Task types
17126
17127 @item
17128 Protected types
17129
17130 @end itemize
17131
17132 @item All types
17133 This metric is computed for any compilation unit. It is equal to the total
17134 number of the declarations of different types given in the compilation unit.
17135 The private and the corresponding full type declaration are counted as one
17136 type declaration. Incomplete type declarations and generic formal types
17137 are not counted.
17138 No distinction is made among different kinds of types (abstract,
17139 private etc.); the total number of types is computed and reported.
17140
17141 @end table
17142
17143 @noindent
17144 By default, all the syntax metrics are computed and reported. You can use the
17145 following switches to select specific syntax metrics.
17146
17147 @table @option
17148
17149 @cindex @option{^--syntax@var{x}^/SYNTAX_METRICS^} (@command{gnatmetric})
17150
17151 @ifclear vms
17152 @cindex @option{--no-syntax@var{x}} (@command{gnatmetric})
17153 @end ifclear
17154
17155 @item ^--syntax-all^/SYNTAX_METRICS=ALL_ON^
17156 Report all the syntax metrics
17157
17158 @item ^--no-syntax-all^/ALL_OFF^
17159 Do not report any of syntax metrics
17160
17161 @item ^--declarations^/SYNTAX_METRICS=DECLARATIONS_ON^
17162 Report the total number of declarations
17163
17164 @item ^--no-declarations^/SYNTAX_METRICS=DECLARATIONS_OFF^
17165 Do not report the total number of declarations
17166
17167 @item ^--statements^/SYNTAX_METRICS=STATEMENTS_ON^
17168 Report the total number of statements
17169
17170 @item ^--no-statements^/SYNTAX_METRICS=STATEMENTS_OFF^
17171 Do not report the total number of statements
17172
17173 @item ^--public-subprograms^/SYNTAX_METRICS=PUBLIC_SUBPROGRAMS_ON^
17174 Report the number of public subprograms in a compilation unit
17175
17176 @item ^--no-public-subprograms^/SYNTAX_METRICS=PUBLIC_SUBPROGRAMS_OFF^
17177 Do not report the number of public subprograms in a compilation unit
17178
17179 @item ^--all-subprograms^/SYNTAX_METRICS=ALL_SUBPROGRAMS_ON^
17180 Report the number of all the subprograms in a compilation unit
17181
17182 @item ^--no-all-subprograms^/SYNTAX_METRICS=ALL_SUBPROGRAMS_OFF^
17183 Do not report the number of all the subprograms in a compilation unit
17184
17185 @item ^--public-types^/SYNTAX_METRICS=PUBLIC_TYPES_ON^
17186 Report the number of public types in a compilation unit
17187
17188 @item ^--no-public-types^/SYNTAX_METRICS=PUBLIC_TYPES_OFF^
17189 Do not report the number of public types in a compilation unit
17190
17191 @item ^--all-types^/SYNTAX_METRICS=ALL_TYPES_ON^
17192 Report the number of all the types in a compilation unit
17193
17194 @item ^--no-all-types^/SYNTAX_METRICS=ALL_TYPES_OFF^
17195 Do not report the number of all the types in a compilation unit
17196
17197 @item ^--unit-nesting^/SYNTAX_METRICS=UNIT_NESTING_ON^
17198 Report the maximal program unit nesting level
17199
17200 @item ^--no-unit-nesting^/SYNTAX_METRICS=UNIT_NESTING_OFF^
17201 Do not report the maximal program unit nesting level
17202
17203 @item ^--construct-nesting^/SYNTAX_METRICS=CONSTRUCT_NESTING_ON^
17204 Report the maximal construct nesting level
17205
17206 @item ^--no-construct-nesting^/SYNTAX_METRICS=CONSTRUCT_NESTING_OFF^
17207 Do not report the maximal construct nesting level
17208
17209 @end table
17210
17211 @node Complexity Metrics Control
17212 @subsubsection Complexity Metrics Control
17213 @cindex Complexity metrics control in @command{gnatmetric}
17214
17215 @noindent
17216 For a program unit that is an executable body (a subprogram body (including
17217 generic bodies), task body, entry body or a package body containing
17218 its own statement sequence) @command{gnatmetric} computes the following
17219 complexity metrics:
17220
17221 @itemize @bullet
17222 @item
17223 McCabe cyclomatic complexity;
17224
17225 @item
17226 McCabe essential complexity;
17227
17228 @item
17229 maximal loop nesting level
17230
17231 @end itemize
17232
17233 @noindent
17234 The McCabe complexity metrics are defined
17235 in @url{http://www.mccabe.com/pdf/nist235r.pdf}
17236
17237 According to McCabe, both control statements and short-circuit control forms
17238 should be taken into account when computing cyclomatic complexity. For each
17239 body, we compute three metric values:
17240
17241 @itemize @bullet
17242 @item
17243 the complexity introduced by control
17244 statements only, without taking into account short-circuit forms,
17245
17246 @item
17247 the complexity introduced by short-circuit control forms only, and
17248
17249 @item
17250 the total
17251 cyclomatic complexity, which is the sum of these two values.
17252 @end itemize
17253
17254 @noindent
17255 When computing cyclomatic and essential complexity, @command{gnatmetric} skips
17256 the code in the exception handlers and in all the nested program units.
17257
17258 By default, all the complexity metrics are computed and reported.
17259 For more fine-grained control you can use
17260 the following switches:
17261
17262 @table @option
17263 @cindex @option{^-complexity@var{x}^/COMPLEXITY_METRICS^} (@command{gnatmetric})
17264
17265 @ifclear vms
17266 @cindex @option{--no-complexity@var{x}}
17267 @end ifclear
17268
17269 @item ^--complexity-all^/COMPLEXITY_METRICS=ALL_ON^
17270 Report all the complexity metrics
17271
17272 @item ^--no-complexity-all^/COMPLEXITY_METRICS=ALL_OFF^
17273 Do not report any of complexity metrics
17274
17275 @item ^--complexity-cyclomatic^/COMPLEXITY_METRICS=CYCLOMATIC_ON^
17276 Report the McCabe Cyclomatic Complexity
17277
17278 @item ^--no-complexity-cyclomatic^/COMPLEXITY_METRICS=CYCLOMATIC_OFF^
17279 Do not report the McCabe Cyclomatic Complexity
17280
17281 @item ^--complexity-essential^/COMPLEXITY_METRICS=ESSENTIAL_ON^
17282 Report the Essential Complexity
17283
17284 @item ^--no-complexity-essential^/COMPLEXITY_METRICS=ESSENTIAL_OFF^
17285 Do not report the Essential Complexity
17286
17287 @item ^--loop-nesting^/COMPLEXITY_METRICS=LOOP_NESTING_ON^
17288 Report maximal loop nesting level
17289
17290 @item ^--no-loop-nesting^/COMPLEXITY_METRICS=LOOP_NESTING_OFF^
17291 Do not report maximal loop nesting level
17292
17293 @item ^--complexity-average^/COMPLEXITY_METRICS=AVERAGE_COMPLEXITY_ON^
17294 Report the average McCabe Cyclomatic Complexity for all the subprogram bodies,
17295 task bodies, entry bodies and statement sequences in package bodies.
17296 The metric is computed and reported for whole set of processed Ada sources
17297 only.
17298
17299 @item ^--no-complexity-average^/COMPLEXITY_METRICS=AVERAGE_COMPLEXITY_OFF^
17300 Do not report the average McCabe Cyclomatic Complexity for all the subprogram
17301 bodies, task bodies, entry bodies and statement sequences in package bodies
17302
17303 @cindex @option{^-ne^/NO_EXITS_AS_GOTOS^} (@command{gnatmetric})
17304 @item ^-ne^/NO_EXITS_AS_GOTOS^
17305 Do not consider @code{exit} statements as @code{goto}s when
17306 computing Essential Complexity
17307
17308 @end table
17309
17310
17311 @node Object-Oriented Metrics Control
17312 @subsubsection Object-Oriented Metrics Control
17313 @cindex Object-Oriented metrics control in @command{gnatmetric}
17314
17315 @noindent
17316 @cindex Coupling metrics (in in @command{gnatmetric})
17317 Coupling metrics are object-oriented metrics that measure the
17318 dependencies between a given class (or a group of classes) and the
17319 ``external world'' (that is, the other classes in the program). In this
17320 subsection the term ``class'' is used in its
17321 traditional object-oriented programming sense
17322 (an instantiable module that contains data and/or method members).
17323 A @emph{category} (of classes)
17324 is a group of closely related classes that are reused and/or
17325 modified together.
17326
17327 A class @code{K}'s @emph{efferent coupling} is the number of classes
17328 that @code{K} depends upon.
17329 A category's efferent coupling is the number of classes outside the
17330 category that the classes inside the category depend upon.
17331
17332 A class @code{K}'s @emph{afferent coupling} is the number of classes
17333 that depend upon @code{K}.
17334 A category's afferent coupling is the number of classes outside the
17335 category that depend on classes belonging to the category.
17336
17337 Ada's implementation of the object-oriented paradigm does not use the
17338 traditional class notion, so the definition of the coupling
17339 metrics for Ada maps the class and class category notions
17340 onto Ada constructs.
17341
17342 For the coupling metrics, several kinds of modules -- a library package,
17343 a library generic package, and a library generic package instantiation --
17344 that define a tagged type or an interface type are
17345 considered to be a class. A category consists of a library package (or
17346 a library generic package) that defines a tagged or an interface type,
17347 together with all its descendant (generic) packages that define tagged
17348 or interface types. For any package counted as a class,
17349 its body (if any) is considered
17350 together with its spec when counting the dependencies. For dependencies
17351 between classes, the Ada semantic dependencies are considered.
17352 For coupling metrics, only dependencies on units that are considered as
17353 classes, are considered.
17354
17355 When computing coupling metrics, @command{gnatmetric} counts only
17356 dependencies between units that are arguments of the gnatmetric call.
17357 Coupling metrics are program-wide (or project-wide) metrics, so to
17358 get a valid result, you should call @command{gnatmetric} for
17359 the whole set of sources that make up your program. It can be done
17360 by calling @command{gnatmetric} from the GNAT driver with @option{-U}
17361 option (see See @ref{The GNAT Driver and Project Files} for details.
17362
17363 By default, all the coupling metrics are disabled. You can use the following
17364 switches to specify the coupling metrics to be computed and reported:
17365
17366 @table @option
17367
17368 @ifclear vms
17369 @cindex @option{--package@var{x}} (@command{gnatmetric})
17370 @cindex @option{--no-package@var{x}} (@command{gnatmetric})
17371 @cindex @option{--category@var{x}} (@command{gnatmetric})
17372 @cindex @option{--no-category@var{x}} (@command{gnatmetric})
17373 @end ifclear
17374
17375 @ifset vms
17376 @cindex @option{/COUPLING_METRICS} (@command{gnatmetric})
17377 @end ifset
17378
17379 @item ^--coupling-all^/COUPLING_METRICS=ALL_ON^
17380 Report all the coupling metrics
17381
17382 @item ^--no-coupling-all^/COUPLING_METRICS=ALL_OFF^
17383 Do not report any of metrics
17384
17385 @item ^--package-efferent-coupling^/COUPLING_METRICS=PACKAGE_EFFERENT_ON^
17386 Report package efferent coupling
17387
17388 @item ^--no-package-efferent-coupling^/COUPLING_METRICS=PACKAGE_EFFERENT_OFF^
17389 Do not report package efferent coupling
17390
17391 @item ^--package-afferent-coupling^/COUPLING_METRICS=PACKAGE_AFFERENT_ON^
17392 Report package afferent coupling
17393
17394 @item ^--no-package-afferent-coupling^/COUPLING_METRICS=PACKAGE_AFFERENT_OFF^
17395 Do not report package afferent coupling
17396
17397 @item ^--category-efferent-coupling^/COUPLING_METRICS=CATEGORY_EFFERENT_ON^
17398 Report category efferent coupling
17399
17400 @item ^--no-category-efferent-coupling^/COUPLING_METRICS=CATEGORY_EFFERENT_OFF^
17401 Do not report category efferent coupling
17402
17403 @item ^--category-afferent-coupling^/COUPLING_METRICS=CATEGORY_AFFERENT_ON^
17404 Report category afferent coupling
17405
17406 @item ^--no-category-afferent-coupling^/COUPLING_METRICS=CATEGORY_AFFERENT_OFF^
17407 Do not report category afferent coupling
17408
17409 @end table
17410
17411 @node Other gnatmetric Switches
17412 @subsection Other @code{gnatmetric} Switches
17413
17414 @noindent
17415 Additional @command{gnatmetric} switches are as follows:
17416
17417 @table @option
17418 @item ^-files @var{filename}^/FILES=@var{filename}^
17419 @cindex @option{^-files^/FILES^} (@code{gnatmetric})
17420 Take the argument source files from the specified file. This file should be an
17421 ordinary text file containing file names separated by spaces or
17422 line breaks. You can use this switch more then once in the same call to
17423 @command{gnatmetric}. You also can combine this switch with
17424 an explicit list of files.
17425
17426 @item ^-v^/VERBOSE^
17427 @cindex @option{^-v^/VERBOSE^} (@code{gnatmetric})
17428 Verbose mode;
17429 @command{gnatmetric} generates version information and then
17430 a trace of sources being processed.
17431
17432 @item ^-dv^/DEBUG_OUTPUT^
17433 @cindex @option{^-dv^/DEBUG_OUTPUT^} (@code{gnatmetric})
17434 Debug mode;
17435 @command{gnatmetric} generates various messages useful to understand what
17436 happens during the metrics computation
17437
17438 @item ^-q^/QUIET^
17439 @cindex @option{^-q^/QUIET^} (@code{gnatmetric})
17440 Quiet mode.
17441 @end table
17442
17443 @node Generate project-wide metrics
17444 @subsection Generate project-wide metrics
17445
17446 In order to compute metrics on all units of a given project, you can use
17447 the @command{gnat} driver along with the @option{-P} option:
17448 @smallexample
17449 gnat metric -Pproj
17450 @end smallexample
17451
17452 @noindent
17453 If the project @code{proj} depends upon other projects, you can compute
17454 the metrics on the project closure using the @option{-U} option:
17455 @smallexample
17456 gnat metric -Pproj -U
17457 @end smallexample
17458
17459 @noindent
17460 Finally, if not all the units are relevant to a particular main
17461 program in the project closure, you can generate metrics for the set
17462 of units needed to create a given main program (unit closure) using
17463 the @option{-U} option followed by the name of the main unit:
17464 @smallexample
17465 gnat metric -Pproj -U main
17466 @end smallexample
17467
17468
17469 @c ***********************************
17470 @node File Name Krunching Using gnatkr
17471 @chapter File Name Krunching Using @code{gnatkr}
17472 @findex gnatkr
17473
17474 @noindent
17475 This chapter discusses the method used by the compiler to shorten
17476 the default file names chosen for Ada units so that they do not
17477 exceed the maximum length permitted. It also describes the
17478 @code{gnatkr} utility that can be used to determine the result of
17479 applying this shortening.
17480 @menu
17481 * About gnatkr::
17482 * Using gnatkr::
17483 * Krunching Method::
17484 * Examples of gnatkr Usage::
17485 @end menu
17486
17487 @node About gnatkr
17488 @section About @code{gnatkr}
17489
17490 @noindent
17491 The default file naming rule in GNAT
17492 is that the file name must be derived from
17493 the unit name. The exact default rule is as follows:
17494 @itemize @bullet
17495 @item
17496 Take the unit name and replace all dots by hyphens.
17497 @item
17498 If such a replacement occurs in the
17499 second character position of a name, and the first character is
17500 ^@samp{a}, @samp{g}, @samp{s}, or @samp{i}, ^@samp{A}, @samp{G}, @samp{S}, or @samp{I},^
17501 then replace the dot by the character
17502 ^@samp{~} (tilde)^@samp{$} (dollar sign)^
17503 instead of a minus.
17504 @end itemize
17505 The reason for this exception is to avoid clashes
17506 with the standard names for children of System, Ada, Interfaces,
17507 and GNAT, which use the prefixes
17508 ^@samp{s-}, @samp{a-}, @samp{i-}, and @samp{g-},^@samp{S-}, @samp{A-}, @samp{I-}, and @samp{G-},^
17509 respectively.
17510
17511 The @option{^-gnatk^/FILE_NAME_MAX_LENGTH=^@var{nn}}
17512 switch of the compiler activates a ``krunching''
17513 circuit that limits file names to nn characters (where nn is a decimal
17514 integer). For example, using OpenVMS,
17515 where the maximum file name length is
17516 39, the value of nn is usually set to 39, but if you want to generate
17517 a set of files that would be usable if ported to a system with some
17518 different maximum file length, then a different value can be specified.
17519 The default value of 39 for OpenVMS need not be specified.
17520
17521 The @code{gnatkr} utility can be used to determine the krunched name for
17522 a given file, when krunched to a specified maximum length.
17523
17524 @node Using gnatkr
17525 @section Using @code{gnatkr}
17526
17527 @noindent
17528 The @code{gnatkr} command has the form
17529
17530 @ifclear vms
17531 @smallexample
17532 $ gnatkr @var{name} @ovar{length}
17533 @end smallexample
17534 @end ifclear
17535
17536 @ifset vms
17537 @smallexample
17538 $ gnatkr @var{name} /COUNT=nn
17539 @end smallexample
17540 @end ifset
17541
17542 @noindent
17543 @var{name} is the uncrunched file name, derived from the name of the unit
17544 in the standard manner described in the previous section (i.e., in particular
17545 all dots are replaced by hyphens). The file name may or may not have an
17546 extension (defined as a suffix of the form period followed by arbitrary
17547 characters other than period). If an extension is present then it will
17548 be preserved in the output. For example, when krunching @file{hellofile.ads}
17549 to eight characters, the result will be hellofil.ads.
17550
17551 Note: for compatibility with previous versions of @code{gnatkr} dots may
17552 appear in the name instead of hyphens, but the last dot will always be
17553 taken as the start of an extension. So if @code{gnatkr} is given an argument
17554 such as @file{Hello.World.adb} it will be treated exactly as if the first
17555 period had been a hyphen, and for example krunching to eight characters
17556 gives the result @file{hellworl.adb}.
17557
17558 Note that the result is always all lower case (except on OpenVMS where it is
17559 all upper case). Characters of the other case are folded as required.
17560
17561 @var{length} represents the length of the krunched name. The default
17562 when no argument is given is ^8^39^ characters. A length of zero stands for
17563 unlimited, in other words do not chop except for system files where the
17564 implied crunching length is always eight characters.
17565
17566 @noindent
17567 The output is the krunched name. The output has an extension only if the
17568 original argument was a file name with an extension.
17569
17570 @node Krunching Method
17571 @section Krunching Method
17572
17573 @noindent
17574 The initial file name is determined by the name of the unit that the file
17575 contains. The name is formed by taking the full expanded name of the
17576 unit and replacing the separating dots with hyphens and
17577 using ^lowercase^uppercase^
17578 for all letters, except that a hyphen in the second character position is
17579 replaced by a ^tilde^dollar sign^ if the first character is
17580 ^@samp{a}, @samp{i}, @samp{g}, or @samp{s}^@samp{A}, @samp{I}, @samp{G}, or @samp{S}^.
17581 The extension is @code{.ads} for a
17582 spec and @code{.adb} for a body.
17583 Krunching does not affect the extension, but the file name is shortened to
17584 the specified length by following these rules:
17585
17586 @itemize @bullet
17587 @item
17588 The name is divided into segments separated by hyphens, tildes or
17589 underscores and all hyphens, tildes, and underscores are
17590 eliminated. If this leaves the name short enough, we are done.
17591
17592 @item
17593 If the name is too long, the longest segment is located (left-most
17594 if there are two of equal length), and shortened by dropping
17595 its last character. This is repeated until the name is short enough.
17596
17597 As an example, consider the krunching of @*@file{our-strings-wide_fixed.adb}
17598 to fit the name into 8 characters as required by some operating systems.
17599
17600 @smallexample
17601 our-strings-wide_fixed 22
17602 our strings wide fixed 19
17603 our string wide fixed 18
17604 our strin wide fixed 17
17605 our stri wide fixed 16
17606 our stri wide fixe 15
17607 our str wide fixe 14
17608 our str wid fixe 13
17609 our str wid fix 12
17610 ou str wid fix 11
17611 ou st wid fix 10
17612 ou st wi fix 9
17613 ou st wi fi 8
17614 Final file name: oustwifi.adb
17615 @end smallexample
17616
17617 @item
17618 The file names for all predefined units are always krunched to eight
17619 characters. The krunching of these predefined units uses the following
17620 special prefix replacements:
17621
17622 @table @file
17623 @item ada-
17624 replaced by @file{^a^A^-}
17625
17626 @item gnat-
17627 replaced by @file{^g^G^-}
17628
17629 @item interfaces-
17630 replaced by @file{^i^I^-}
17631
17632 @item system-
17633 replaced by @file{^s^S^-}
17634 @end table
17635
17636 These system files have a hyphen in the second character position. That
17637 is why normal user files replace such a character with a
17638 ^tilde^dollar sign^, to
17639 avoid confusion with system file names.
17640
17641 As an example of this special rule, consider
17642 @*@file{ada-strings-wide_fixed.adb}, which gets krunched as follows:
17643
17644 @smallexample
17645 ada-strings-wide_fixed 22
17646 a- strings wide fixed 18
17647 a- string wide fixed 17
17648 a- strin wide fixed 16
17649 a- stri wide fixed 15
17650 a- stri wide fixe 14
17651 a- str wide fixe 13
17652 a- str wid fixe 12
17653 a- str wid fix 11
17654 a- st wid fix 10
17655 a- st wi fix 9
17656 a- st wi fi 8
17657 Final file name: a-stwifi.adb
17658 @end smallexample
17659 @end itemize
17660
17661 Of course no file shortening algorithm can guarantee uniqueness over all
17662 possible unit names, and if file name krunching is used then it is your
17663 responsibility to ensure that no name clashes occur. The utility
17664 program @code{gnatkr} is supplied for conveniently determining the
17665 krunched name of a file.
17666
17667 @node Examples of gnatkr Usage
17668 @section Examples of @code{gnatkr} Usage
17669
17670 @smallexample
17671 @iftex
17672 @leftskip=0cm
17673 @end iftex
17674 @ifclear vms
17675 $ gnatkr very_long_unit_name.ads --> velounna.ads
17676 $ gnatkr grandparent-parent-child.ads --> grparchi.ads
17677 $ gnatkr Grandparent.Parent.Child.ads --> grparchi.ads
17678 $ gnatkr grandparent-parent-child --> grparchi
17679 @end ifclear
17680 $ gnatkr very_long_unit_name.ads/count=6 --> vlunna.ads
17681 $ gnatkr very_long_unit_name.ads/count=0 --> very_long_unit_name.ads
17682 @end smallexample
17683
17684 @node Preprocessing Using gnatprep
17685 @chapter Preprocessing Using @code{gnatprep}
17686 @findex gnatprep
17687
17688 @noindent
17689 This chapter discusses how to use GNAT's @code{gnatprep} utility for simple
17690 preprocessing.
17691 Although designed for use with GNAT, @code{gnatprep} does not depend on any
17692 special GNAT features.
17693 For further discussion of conditional compilation in general, see
17694 @ref{Conditional Compilation}.
17695
17696 @menu
17697 * Preprocessing Symbols::
17698 * Using gnatprep::
17699 * Switches for gnatprep::
17700 * Form of Definitions File::
17701 * Form of Input Text for gnatprep::
17702 @end menu
17703
17704 @node Preprocessing Symbols
17705 @section Preprocessing Symbols
17706
17707 @noindent
17708 Preprocessing symbols are defined in definition files and referred to in
17709 sources to be preprocessed. A Preprocessing symbol is an identifier, following
17710 normal Ada (case-insensitive) rules for its syntax, with the restriction that
17711 all characters need to be in the ASCII set (no accented letters).
17712
17713 @node Using gnatprep
17714 @section Using @code{gnatprep}
17715
17716 @noindent
17717 To call @code{gnatprep} use
17718
17719 @smallexample
17720 $ gnatprep @ovar{switches} @var{infile} @var{outfile} @ovar{deffile}
17721 @end smallexample
17722
17723 @noindent
17724 where
17725 @table @var
17726 @item switches
17727 is an optional sequence of switches as described in the next section.
17728
17729 @item infile
17730 is the full name of the input file, which is an Ada source
17731 file containing preprocessor directives.
17732
17733 @item outfile
17734 is the full name of the output file, which is an Ada source
17735 in standard Ada form. When used with GNAT, this file name will
17736 normally have an ads or adb suffix.
17737
17738 @item deffile
17739 is the full name of a text file containing definitions of
17740 preprocessing symbols to be referenced by the preprocessor. This argument is
17741 optional, and can be replaced by the use of the @option{-D} switch.
17742
17743 @end table
17744
17745 @node Switches for gnatprep
17746 @section Switches for @code{gnatprep}
17747
17748 @table @option
17749 @c !sort!
17750
17751 @item ^-b^/BLANK_LINES^
17752 @cindex @option{^-b^/BLANK_LINES^} (@command{gnatprep})
17753 Causes both preprocessor lines and the lines deleted by
17754 preprocessing to be replaced by blank lines in the output source file,
17755 preserving line numbers in the output file.
17756
17757 @item ^-c^/COMMENTS^
17758 @cindex @option{^-c^/COMMENTS^} (@command{gnatprep})
17759 Causes both preprocessor lines and the lines deleted
17760 by preprocessing to be retained in the output source as comments marked
17761 with the special string @code{"--! "}. This option will result in line numbers
17762 being preserved in the output file.
17763
17764 @item ^-C^/REPLACE_IN_COMMENTS^
17765 @cindex @option{^-C^/REPLACE_IN_COMMENTS^} (@command{gnatprep})
17766 Causes comments to be scanned. Normally comments are ignored by gnatprep.
17767 If this option is specified, then comments are scanned and any $symbol
17768 substitutions performed as in program text. This is particularly useful
17769 when structured comments are used (e.g., when writing programs in the
17770 SPARK dialect of Ada). Note that this switch is not available when
17771 doing integrated preprocessing (it would be useless in this context
17772 since comments are ignored by the compiler in any case).
17773
17774 @item ^-Dsymbol=value^/ASSOCIATE="symbol=value"^
17775 @cindex @option{^-D^/ASSOCIATE^} (@command{gnatprep})
17776 Defines a new preprocessing symbol, associated with value. If no value is given
17777 on the command line, then symbol is considered to be @code{True}. This switch
17778 can be used in place of a definition file.
17779
17780 @ifset vms
17781 @item /REMOVE
17782 @cindex @option{/REMOVE} (@command{gnatprep})
17783 This is the default setting which causes lines deleted by preprocessing
17784 to be entirely removed from the output file.
17785 @end ifset
17786
17787 @item ^-r^/REFERENCE^
17788 @cindex @option{^-r^/REFERENCE^} (@command{gnatprep})
17789 Causes a @code{Source_Reference} pragma to be generated that
17790 references the original input file, so that error messages will use
17791 the file name of this original file. The use of this switch implies
17792 that preprocessor lines are not to be removed from the file, so its
17793 use will force @option{^-b^/BLANK_LINES^} mode if
17794 @option{^-c^/COMMENTS^}
17795 has not been specified explicitly.
17796
17797 Note that if the file to be preprocessed contains multiple units, then
17798 it will be necessary to @code{gnatchop} the output file from
17799 @code{gnatprep}. If a @code{Source_Reference} pragma is present
17800 in the preprocessed file, it will be respected by
17801 @code{gnatchop ^-r^/REFERENCE^}
17802 so that the final chopped files will correctly refer to the original
17803 input source file for @code{gnatprep}.
17804
17805 @item ^-s^/SYMBOLS^
17806 @cindex @option{^-s^/SYMBOLS^} (@command{gnatprep})
17807 Causes a sorted list of symbol names and values to be
17808 listed on the standard output file.
17809
17810 @item ^-u^/UNDEFINED^
17811 @cindex @option{^-u^/UNDEFINED^} (@command{gnatprep})
17812 Causes undefined symbols to be treated as having the value FALSE in the context
17813 of a preprocessor test. In the absence of this option, an undefined symbol in
17814 a @code{#if} or @code{#elsif} test will be treated as an error.
17815
17816 @end table
17817
17818 @ifclear vms
17819 @noindent
17820 Note: if neither @option{-b} nor @option{-c} is present,
17821 then preprocessor lines and
17822 deleted lines are completely removed from the output, unless -r is
17823 specified, in which case -b is assumed.
17824 @end ifclear
17825
17826 @node Form of Definitions File
17827 @section Form of Definitions File
17828
17829 @noindent
17830 The definitions file contains lines of the form
17831
17832 @smallexample
17833 symbol := value
17834 @end smallexample
17835
17836 @noindent
17837 where symbol is a preprocessing symbol, and value is one of the following:
17838
17839 @itemize @bullet
17840 @item
17841 Empty, corresponding to a null substitution
17842 @item
17843 A string literal using normal Ada syntax
17844 @item
17845 Any sequence of characters from the set
17846 (letters, digits, period, underline).
17847 @end itemize
17848
17849 @noindent
17850 Comment lines may also appear in the definitions file, starting with
17851 the usual @code{--},
17852 and comments may be added to the definitions lines.
17853
17854 @node Form of Input Text for gnatprep
17855 @section Form of Input Text for @code{gnatprep}
17856
17857 @noindent
17858 The input text may contain preprocessor conditional inclusion lines,
17859 as well as general symbol substitution sequences.
17860
17861 The preprocessor conditional inclusion commands have the form
17862
17863 @smallexample
17864 @group
17865 @cartouche
17866 #if @i{expression} @r{[}then@r{]}
17867 lines
17868 #elsif @i{expression} @r{[}then@r{]}
17869 lines
17870 #elsif @i{expression} @r{[}then@r{]}
17871 lines
17872 @dots{}
17873 #else
17874 lines
17875 #end if;
17876 @end cartouche
17877 @end group
17878 @end smallexample
17879
17880 @noindent
17881 In this example, @i{expression} is defined by the following grammar:
17882 @smallexample
17883 @i{expression} ::= <symbol>
17884 @i{expression} ::= <symbol> = "<value>"
17885 @i{expression} ::= <symbol> = <symbol>
17886 @i{expression} ::= <symbol> 'Defined
17887 @i{expression} ::= not @i{expression}
17888 @i{expression} ::= @i{expression} and @i{expression}
17889 @i{expression} ::= @i{expression} or @i{expression}
17890 @i{expression} ::= @i{expression} and then @i{expression}
17891 @i{expression} ::= @i{expression} or else @i{expression}
17892 @i{expression} ::= ( @i{expression} )
17893 @end smallexample
17894
17895 The following restriction exists: it is not allowed to have "and" or "or"
17896 following "not" in the same expression without parentheses. For example, this
17897 is not allowed:
17898
17899 @smallexample
17900 not X or Y
17901 @end smallexample
17902
17903 This should be one of the following:
17904
17905 @smallexample
17906 (not X) or Y
17907 not (X or Y)
17908 @end smallexample
17909
17910 @noindent
17911 For the first test (@i{expression} ::= <symbol>) the symbol must have
17912 either the value true or false, that is to say the right-hand of the
17913 symbol definition must be one of the (case-insensitive) literals
17914 @code{True} or @code{False}. If the value is true, then the
17915 corresponding lines are included, and if the value is false, they are
17916 excluded.
17917
17918 The test (@i{expression} ::= <symbol> @code{'Defined}) is true only if
17919 the symbol has been defined in the definition file or by a @option{-D}
17920 switch on the command line. Otherwise, the test is false.
17921
17922 The equality tests are case insensitive, as are all the preprocessor lines.
17923
17924 If the symbol referenced is not defined in the symbol definitions file,
17925 then the effect depends on whether or not switch @option{-u}
17926 is specified. If so, then the symbol is treated as if it had the value
17927 false and the test fails. If this switch is not specified, then
17928 it is an error to reference an undefined symbol. It is also an error to
17929 reference a symbol that is defined with a value other than @code{True}
17930 or @code{False}.
17931
17932 The use of the @code{not} operator inverts the sense of this logical test.
17933 The @code{not} operator cannot be combined with the @code{or} or @code{and}
17934 operators, without parentheses. For example, "if not X or Y then" is not
17935 allowed, but "if (not X) or Y then" and "if not (X or Y) then" are.
17936
17937 The @code{then} keyword is optional as shown
17938
17939 The @code{#} must be the first non-blank character on a line, but
17940 otherwise the format is free form. Spaces or tabs may appear between
17941 the @code{#} and the keyword. The keywords and the symbols are case
17942 insensitive as in normal Ada code. Comments may be used on a
17943 preprocessor line, but other than that, no other tokens may appear on a
17944 preprocessor line. Any number of @code{elsif} clauses can be present,
17945 including none at all. The @code{else} is optional, as in Ada.
17946
17947 The @code{#} marking the start of a preprocessor line must be the first
17948 non-blank character on the line, i.e., it must be preceded only by
17949 spaces or horizontal tabs.
17950
17951 Symbol substitution outside of preprocessor lines is obtained by using
17952 the sequence
17953
17954 @smallexample
17955 $symbol
17956 @end smallexample
17957
17958 @noindent
17959 anywhere within a source line, except in a comment or within a
17960 string literal. The identifier
17961 following the @code{$} must match one of the symbols defined in the symbol
17962 definition file, and the result is to substitute the value of the
17963 symbol in place of @code{$symbol} in the output file.
17964
17965 Note that although the substitution of strings within a string literal
17966 is not possible, it is possible to have a symbol whose defined value is
17967 a string literal. So instead of setting XYZ to @code{hello} and writing:
17968
17969 @smallexample
17970 Header : String := "$XYZ";
17971 @end smallexample
17972
17973 @noindent
17974 you should set XYZ to @code{"hello"} and write:
17975
17976 @smallexample
17977 Header : String := $XYZ;
17978 @end smallexample
17979
17980 @noindent
17981 and then the substitution will occur as desired.
17982
17983 @ifset vms
17984 @node The GNAT Run-Time Library Builder gnatlbr
17985 @chapter The GNAT Run-Time Library Builder @code{gnatlbr}
17986 @findex gnatlbr
17987 @cindex Library builder
17988
17989 @noindent
17990 @code{gnatlbr} is a tool for rebuilding the GNAT run time with user
17991 supplied configuration pragmas.
17992
17993 @menu
17994 * Running gnatlbr::
17995 * Switches for gnatlbr::
17996 * Examples of gnatlbr Usage::
17997 @end menu
17998
17999 @node Running gnatlbr
18000 @section Running @code{gnatlbr}
18001
18002 @noindent
18003 The @code{gnatlbr} command has the form
18004
18005 @smallexample
18006 $ GNAT LIBRARY /@r{[}CREATE@r{|}SET@r{|}DELETE@r{]}=directory @r{[}/CONFIG=file@r{]}
18007 @end smallexample
18008
18009 @node Switches for gnatlbr
18010 @section Switches for @code{gnatlbr}
18011
18012 @noindent
18013 @code{gnatlbr} recognizes the following switches:
18014
18015 @table @option
18016 @c !sort!
18017 @item /CREATE=directory
18018 @cindex @code{/CREATE} (@code{gnatlbr})
18019 Create the new run-time library in the specified directory.
18020
18021 @item /SET=directory
18022 @cindex @code{/SET} (@code{gnatlbr})
18023 Make the library in the specified directory the current run-time library.
18024
18025 @item /DELETE=directory
18026 @cindex @code{/DELETE} (@code{gnatlbr})
18027 Delete the run-time library in the specified directory.
18028
18029 @item /CONFIG=file
18030 @cindex @code{/CONFIG} (@code{gnatlbr})
18031 With /CREATE: Use the configuration pragmas in the specified file when
18032 building the library.
18033
18034 With /SET: Use the configuration pragmas in the specified file when
18035 compiling.
18036
18037 @end table
18038
18039 @node Examples of gnatlbr Usage
18040 @section Example of @code{gnatlbr} Usage
18041
18042 @smallexample
18043 Contents of VAXFLOAT.ADC:
18044 pragma Float_Representation (VAX_Float);
18045
18046 $ GNAT LIBRARY /CREATE=[.VAXFLOAT] /CONFIG=VAXFLOAT.ADC
18047
18048 GNAT LIBRARY rebuilds the run-time library in directory [.VAXFLOAT]
18049
18050 @end smallexample
18051 @end ifset
18052
18053 @node The GNAT Library Browser gnatls
18054 @chapter The GNAT Library Browser @code{gnatls}
18055 @findex gnatls
18056 @cindex Library browser
18057
18058 @noindent
18059 @code{gnatls} is a tool that outputs information about compiled
18060 units. It gives the relationship between objects, unit names and source
18061 files. It can also be used to check the source dependencies of a unit
18062 as well as various characteristics.
18063
18064 Note: to invoke @code{gnatls} with a project file, use the @code{gnat}
18065 driver (see @ref{The GNAT Driver and Project Files}).
18066
18067 @menu
18068 * Running gnatls::
18069 * Switches for gnatls::
18070 * Examples of gnatls Usage::
18071 @end menu
18072
18073 @node Running gnatls
18074 @section Running @code{gnatls}
18075
18076 @noindent
18077 The @code{gnatls} command has the form
18078
18079 @smallexample
18080 $ gnatls switches @var{object_or_ali_file}
18081 @end smallexample
18082
18083 @noindent
18084 The main argument is the list of object or @file{ali} files
18085 (@pxref{The Ada Library Information Files})
18086 for which information is requested.
18087
18088 In normal mode, without additional option, @code{gnatls} produces a
18089 four-column listing. Each line represents information for a specific
18090 object. The first column gives the full path of the object, the second
18091 column gives the name of the principal unit in this object, the third
18092 column gives the status of the source and the fourth column gives the
18093 full path of the source representing this unit.
18094 Here is a simple example of use:
18095
18096 @smallexample
18097 $ gnatls *.o
18098 ^./^[]^demo1.o demo1 DIF demo1.adb
18099 ^./^[]^demo2.o demo2 OK demo2.adb
18100 ^./^[]^hello.o h1 OK hello.adb
18101 ^./^[]^instr-child.o instr.child MOK instr-child.adb
18102 ^./^[]^instr.o instr OK instr.adb
18103 ^./^[]^tef.o tef DIF tef.adb
18104 ^./^[]^text_io_example.o text_io_example OK text_io_example.adb
18105 ^./^[]^tgef.o tgef DIF tgef.adb
18106 @end smallexample
18107
18108 @noindent
18109 The first line can be interpreted as follows: the main unit which is
18110 contained in
18111 object file @file{demo1.o} is demo1, whose main source is in
18112 @file{demo1.adb}. Furthermore, the version of the source used for the
18113 compilation of demo1 has been modified (DIF). Each source file has a status
18114 qualifier which can be:
18115
18116 @table @code
18117 @item OK (unchanged)
18118 The version of the source file used for the compilation of the
18119 specified unit corresponds exactly to the actual source file.
18120
18121 @item MOK (slightly modified)
18122 The version of the source file used for the compilation of the
18123 specified unit differs from the actual source file but not enough to
18124 require recompilation. If you use gnatmake with the qualifier
18125 @option{^-m (minimal recompilation)^/MINIMAL_RECOMPILATION^}, a file marked
18126 MOK will not be recompiled.
18127
18128 @item DIF (modified)
18129 No version of the source found on the path corresponds to the source
18130 used to build this object.
18131
18132 @item ??? (file not found)
18133 No source file was found for this unit.
18134
18135 @item HID (hidden, unchanged version not first on PATH)
18136 The version of the source that corresponds exactly to the source used
18137 for compilation has been found on the path but it is hidden by another
18138 version of the same source that has been modified.
18139
18140 @end table
18141
18142 @node Switches for gnatls
18143 @section Switches for @code{gnatls}
18144
18145 @noindent
18146 @code{gnatls} recognizes the following switches:
18147
18148 @table @option
18149 @c !sort!
18150 @cindex @option{--version} @command{gnatls}
18151 Display Copyright and version, then exit disregarding all other options.
18152
18153 @item --help
18154 @cindex @option{--help} @command{gnatls}
18155 If @option{--version} was not used, display usage, then exit disregarding
18156 all other options.
18157
18158 @item ^-a^/ALL_UNITS^
18159 @cindex @option{^-a^/ALL_UNITS^} (@code{gnatls})
18160 Consider all units, including those of the predefined Ada library.
18161 Especially useful with @option{^-d^/DEPENDENCIES^}.
18162
18163 @item ^-d^/DEPENDENCIES^
18164 @cindex @option{^-d^/DEPENDENCIES^} (@code{gnatls})
18165 List sources from which specified units depend on.
18166
18167 @item ^-h^/OUTPUT=OPTIONS^
18168 @cindex @option{^-h^/OUTPUT=OPTIONS^} (@code{gnatls})
18169 Output the list of options.
18170
18171 @item ^-o^/OUTPUT=OBJECTS^
18172 @cindex @option{^-o^/OUTPUT=OBJECTS^} (@code{gnatls})
18173 Only output information about object files.
18174
18175 @item ^-s^/OUTPUT=SOURCES^
18176 @cindex @option{^-s^/OUTPUT=SOURCES^} (@code{gnatls})
18177 Only output information about source files.
18178
18179 @item ^-u^/OUTPUT=UNITS^
18180 @cindex @option{^-u^/OUTPUT=UNITS^} (@code{gnatls})
18181 Only output information about compilation units.
18182
18183 @item ^-files^/FILES^=@var{file}
18184 @cindex @option{^-files^/FILES^} (@code{gnatls})
18185 Take as arguments the files listed in text file @var{file}.
18186 Text file @var{file} may contain empty lines that are ignored.
18187 Each nonempty line should contain the name of an existing file.
18188 Several such switches may be specified simultaneously.
18189
18190 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
18191 @itemx ^-aI^/SOURCE_SEARCH=^@var{dir}
18192 @itemx ^-I^/SEARCH=^@var{dir}
18193 @itemx ^-I-^/NOCURRENT_DIRECTORY^
18194 @itemx -nostdinc
18195 @cindex @option{^-aO^/OBJECT_SEARCH^} (@code{gnatls})
18196 @cindex @option{^-aI^/SOURCE_SEARCH^} (@code{gnatls})
18197 @cindex @option{^-I^/SEARCH^} (@code{gnatls})
18198 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatls})
18199 Source path manipulation. Same meaning as the equivalent @command{gnatmake}
18200 flags (@pxref{Switches for gnatmake}).
18201
18202 @item --RTS=@var{rts-path}
18203 @cindex @option{--RTS} (@code{gnatls})
18204 Specifies the default location of the runtime library. Same meaning as the
18205 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
18206
18207 @item ^-v^/OUTPUT=VERBOSE^
18208 @cindex @option{^-v^/OUTPUT=VERBOSE^} (@code{gnatls})
18209 Verbose mode. Output the complete source, object and project paths. Do not use
18210 the default column layout but instead use long format giving as much as
18211 information possible on each requested units, including special
18212 characteristics such as:
18213
18214 @table @code
18215 @item Preelaborable
18216 The unit is preelaborable in the Ada sense.
18217
18218 @item No_Elab_Code
18219 No elaboration code has been produced by the compiler for this unit.
18220
18221 @item Pure
18222 The unit is pure in the Ada sense.
18223
18224 @item Elaborate_Body
18225 The unit contains a pragma Elaborate_Body.
18226
18227 @item Remote_Types
18228 The unit contains a pragma Remote_Types.
18229
18230 @item Shared_Passive
18231 The unit contains a pragma Shared_Passive.
18232
18233 @item Predefined
18234 This unit is part of the predefined environment and cannot be modified
18235 by the user.
18236
18237 @item Remote_Call_Interface
18238 The unit contains a pragma Remote_Call_Interface.
18239
18240 @end table
18241
18242 @end table
18243
18244 @node Examples of gnatls Usage
18245 @section Example of @code{gnatls} Usage
18246 @ifclear vms
18247
18248 @noindent
18249 Example of using the verbose switch. Note how the source and
18250 object paths are affected by the -I switch.
18251
18252 @smallexample
18253 $ gnatls -v -I.. demo1.o
18254
18255 GNATLS 5.03w (20041123-34)
18256 Copyright 1997-2004 Free Software Foundation, Inc.
18257
18258 Source Search Path:
18259 <Current_Directory>
18260 ../
18261 /home/comar/local/adainclude/
18262
18263 Object Search Path:
18264 <Current_Directory>
18265 ../
18266 /home/comar/local/lib/gcc-lib/x86-linux/3.4.3/adalib/
18267
18268 Project Search Path:
18269 <Current_Directory>
18270 /home/comar/local/lib/gnat/
18271
18272 ./demo1.o
18273 Unit =>
18274 Name => demo1
18275 Kind => subprogram body
18276 Flags => No_Elab_Code
18277 Source => demo1.adb modified
18278 @end smallexample
18279
18280 @noindent
18281 The following is an example of use of the dependency list.
18282 Note the use of the -s switch
18283 which gives a straight list of source files. This can be useful for
18284 building specialized scripts.
18285
18286 @smallexample
18287 $ gnatls -d demo2.o
18288 ./demo2.o demo2 OK demo2.adb
18289 OK gen_list.ads
18290 OK gen_list.adb
18291 OK instr.ads
18292 OK instr-child.ads
18293
18294 $ gnatls -d -s -a demo1.o
18295 demo1.adb
18296 /home/comar/local/adainclude/ada.ads
18297 /home/comar/local/adainclude/a-finali.ads
18298 /home/comar/local/adainclude/a-filico.ads
18299 /home/comar/local/adainclude/a-stream.ads
18300 /home/comar/local/adainclude/a-tags.ads
18301 gen_list.ads
18302 gen_list.adb
18303 /home/comar/local/adainclude/gnat.ads
18304 /home/comar/local/adainclude/g-io.ads
18305 instr.ads
18306 /home/comar/local/adainclude/system.ads
18307 /home/comar/local/adainclude/s-exctab.ads
18308 /home/comar/local/adainclude/s-finimp.ads
18309 /home/comar/local/adainclude/s-finroo.ads
18310 /home/comar/local/adainclude/s-secsta.ads
18311 /home/comar/local/adainclude/s-stalib.ads
18312 /home/comar/local/adainclude/s-stoele.ads
18313 /home/comar/local/adainclude/s-stratt.ads
18314 /home/comar/local/adainclude/s-tasoli.ads
18315 /home/comar/local/adainclude/s-unstyp.ads
18316 /home/comar/local/adainclude/unchconv.ads
18317 @end smallexample
18318 @end ifclear
18319
18320 @ifset vms
18321 @smallexample
18322 GNAT LIST /DEPENDENCIES /OUTPUT=SOURCES /ALL_UNITS DEMO1.ADB
18323
18324 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]ada.ads
18325 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-finali.ads
18326 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-filico.ads
18327 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-stream.ads
18328 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-tags.ads
18329 demo1.adb
18330 gen_list.ads
18331 gen_list.adb
18332 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]gnat.ads
18333 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]g-io.ads
18334 instr.ads
18335 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]system.ads
18336 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-exctab.ads
18337 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-finimp.ads
18338 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-finroo.ads
18339 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-secsta.ads
18340 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stalib.ads
18341 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stoele.ads
18342 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stratt.ads
18343 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-tasoli.ads
18344 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-unstyp.ads
18345 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]unchconv.ads
18346 @end smallexample
18347 @end ifset
18348
18349 @node Cleaning Up Using gnatclean
18350 @chapter Cleaning Up Using @code{gnatclean}
18351 @findex gnatclean
18352 @cindex Cleaning tool
18353
18354 @noindent
18355 @code{gnatclean} is a tool that allows the deletion of files produced by the
18356 compiler, binder and linker, including ALI files, object files, tree files,
18357 expanded source files, library files, interface copy source files, binder
18358 generated files and executable files.
18359
18360 @menu
18361 * Running gnatclean::
18362 * Switches for gnatclean::
18363 @c * Examples of gnatclean Usage::
18364 @end menu
18365
18366 @node Running gnatclean
18367 @section Running @code{gnatclean}
18368
18369 @noindent
18370 The @code{gnatclean} command has the form:
18371
18372 @smallexample
18373 $ gnatclean switches @var{names}
18374 @end smallexample
18375
18376 @noindent
18377 @var{names} is a list of source file names. Suffixes @code{.^ads^ADS^} and
18378 @code{^adb^ADB^} may be omitted. If a project file is specified using switch
18379 @code{^-P^/PROJECT_FILE=^}, then @var{names} may be completely omitted.
18380
18381 @noindent
18382 In normal mode, @code{gnatclean} delete the files produced by the compiler and,
18383 if switch @code{^-c^/COMPILER_FILES_ONLY^} is not specified, by the binder and
18384 the linker. In informative-only mode, specified by switch
18385 @code{^-n^/NODELETE^}, the list of files that would have been deleted in
18386 normal mode is listed, but no file is actually deleted.
18387
18388 @node Switches for gnatclean
18389 @section Switches for @code{gnatclean}
18390
18391 @noindent
18392 @code{gnatclean} recognizes the following switches:
18393
18394 @table @option
18395 @c !sort!
18396 @cindex @option{--version} @command{gnatclean}
18397 Display Copyright and version, then exit disregarding all other options.
18398
18399 @item --help
18400 @cindex @option{--help} @command{gnatclean}
18401 If @option{--version} was not used, display usage, then exit disregarding
18402 all other options.
18403
18404 @item ^-c^/COMPILER_FILES_ONLY^
18405 @cindex @option{^-c^/COMPILER_FILES_ONLY^} (@code{gnatclean})
18406 Only attempt to delete the files produced by the compiler, not those produced
18407 by the binder or the linker. The files that are not to be deleted are library
18408 files, interface copy files, binder generated files and executable files.
18409
18410 @item ^-D ^/DIRECTORY_OBJECTS=^@var{dir}
18411 @cindex @option{^-D^/DIRECTORY_OBJECTS^} (@code{gnatclean})
18412 Indicate that ALI and object files should normally be found in directory
18413 @var{dir}.
18414
18415 @item ^-F^/FULL_PATH_IN_BRIEF_MESSAGES^
18416 @cindex @option{^-F^/FULL_PATH_IN_BRIEF_MESSAGES^} (@code{gnatclean})
18417 When using project files, if some errors or warnings are detected during
18418 parsing and verbose mode is not in effect (no use of switch
18419 ^-v^/VERBOSE^), then error lines start with the full path name of the project
18420 file, rather than its simple file name.
18421
18422 @item ^-h^/HELP^
18423 @cindex @option{^-h^/HELP^} (@code{gnatclean})
18424 Output a message explaining the usage of @code{^gnatclean^gnatclean^}.
18425
18426 @item ^-n^/NODELETE^
18427 @cindex @option{^-n^/NODELETE^} (@code{gnatclean})
18428 Informative-only mode. Do not delete any files. Output the list of the files
18429 that would have been deleted if this switch was not specified.
18430
18431 @item ^-P^/PROJECT_FILE=^@var{project}
18432 @cindex @option{^-P^/PROJECT_FILE^} (@code{gnatclean})
18433 Use project file @var{project}. Only one such switch can be used.
18434 When cleaning a project file, the files produced by the compilation of the
18435 immediate sources or inherited sources of the project files are to be
18436 deleted. This is not depending on the presence or not of executable names
18437 on the command line.
18438
18439 @item ^-q^/QUIET^
18440 @cindex @option{^-q^/QUIET^} (@code{gnatclean})
18441 Quiet output. If there are no errors, do not output anything, except in
18442 verbose mode (switch ^-v^/VERBOSE^) or in informative-only mode
18443 (switch ^-n^/NODELETE^).
18444
18445 @item ^-r^/RECURSIVE^
18446 @cindex @option{^-r^/RECURSIVE^} (@code{gnatclean})
18447 When a project file is specified (using switch ^-P^/PROJECT_FILE=^),
18448 clean all imported and extended project files, recursively. If this switch
18449 is not specified, only the files related to the main project file are to be
18450 deleted. This switch has no effect if no project file is specified.
18451
18452 @item ^-v^/VERBOSE^
18453 @cindex @option{^-v^/VERBOSE^} (@code{gnatclean})
18454 Verbose mode.
18455
18456 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
18457 @cindex @option{^-vP^/MESSAGES_PROJECT_FILE^} (@code{gnatclean})
18458 Indicates the verbosity of the parsing of GNAT project files.
18459 @xref{Switches Related to Project Files}.
18460
18461 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
18462 @cindex @option{^-X^/EXTERNAL_REFERENCE^} (@code{gnatclean})
18463 Indicates that external variable @var{name} has the value @var{value}.
18464 The Project Manager will use this value for occurrences of
18465 @code{external(name)} when parsing the project file.
18466 @xref{Switches Related to Project Files}.
18467
18468 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
18469 @cindex @option{^-aO^/OBJECT_SEARCH^} (@code{gnatclean})
18470 When searching for ALI and object files, look in directory
18471 @var{dir}.
18472
18473 @item ^-I^/SEARCH=^@var{dir}
18474 @cindex @option{^-I^/SEARCH^} (@code{gnatclean})
18475 Equivalent to @option{^-aO^/OBJECT_SEARCH=^@var{dir}}.
18476
18477 @item ^-I-^/NOCURRENT_DIRECTORY^
18478 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatclean})
18479 @cindex Source files, suppressing search
18480 Do not look for ALI or object files in the directory
18481 where @code{gnatclean} was invoked.
18482
18483 @end table
18484
18485 @c @node Examples of gnatclean Usage
18486 @c @section Examples of @code{gnatclean} Usage
18487
18488 @ifclear vms
18489 @node GNAT and Libraries
18490 @chapter GNAT and Libraries
18491 @cindex Library, building, installing, using
18492
18493 @noindent
18494 This chapter describes how to build and use libraries with GNAT, and also shows
18495 how to recompile the GNAT run-time library. You should be familiar with the
18496 Project Manager facility (@pxref{GNAT Project Manager}) before reading this
18497 chapter.
18498
18499 @menu
18500 * Introduction to Libraries in GNAT::
18501 * General Ada Libraries::
18502 * Stand-alone Ada Libraries::
18503 * Rebuilding the GNAT Run-Time Library::
18504 @end menu
18505
18506 @node Introduction to Libraries in GNAT
18507 @section Introduction to Libraries in GNAT
18508
18509 @noindent
18510 A library is, conceptually, a collection of objects which does not have its
18511 own main thread of execution, but rather provides certain services to the
18512 applications that use it. A library can be either statically linked with the
18513 application, in which case its code is directly included in the application,
18514 or, on platforms that support it, be dynamically linked, in which case
18515 its code is shared by all applications making use of this library.
18516
18517 GNAT supports both types of libraries.
18518 In the static case, the compiled code can be provided in different ways. The
18519 simplest approach is to provide directly the set of objects resulting from
18520 compilation of the library source files. Alternatively, you can group the
18521 objects into an archive using whatever commands are provided by the operating
18522 system. For the latter case, the objects are grouped into a shared library.
18523
18524 In the GNAT environment, a library has three types of components:
18525 @itemize @bullet
18526 @item
18527 Source files.
18528 @item
18529 @file{ALI} files.
18530 @xref{The Ada Library Information Files}.
18531 @item
18532 Object files, an archive or a shared library.
18533 @end itemize
18534
18535 @noindent
18536 A GNAT library may expose all its source files, which is useful for
18537 documentation purposes. Alternatively, it may expose only the units needed by
18538 an external user to make use of the library. That is to say, the specs
18539 reflecting the library services along with all the units needed to compile
18540 those specs, which can include generic bodies or any body implementing an
18541 inlined routine. In the case of @emph{stand-alone libraries} those exposed
18542 units are called @emph{interface units} (@pxref{Stand-alone Ada Libraries}).
18543
18544 All compilation units comprising an application, including those in a library,
18545 need to be elaborated in an order partially defined by Ada's semantics. GNAT
18546 computes the elaboration order from the @file{ALI} files and this is why they
18547 constitute a mandatory part of GNAT libraries. Except in the case of
18548 @emph{stand-alone libraries}, where a specific library elaboration routine is
18549 produced independently of the application(s) using the library.
18550
18551 @node General Ada Libraries
18552 @section General Ada Libraries
18553
18554 @menu
18555 * Building a library::
18556 * Installing a library::
18557 * Using a library::
18558 @end menu
18559
18560 @node Building a library
18561 @subsection Building a library
18562
18563 @noindent
18564 The easiest way to build a library is to use the Project Manager,
18565 which supports a special type of project called a @emph{Library Project}
18566 (@pxref{Library Projects}).
18567
18568 A project is considered a library project, when two project-level attributes
18569 are defined in it: @code{Library_Name} and @code{Library_Dir}. In order to
18570 control different aspects of library configuration, additional optional
18571 project-level attributes can be specified:
18572 @table @code
18573 @item Library_Kind
18574 This attribute controls whether the library is to be static or dynamic
18575
18576 @item Library_Version
18577 This attribute specifies the library version; this value is used
18578 during dynamic linking of shared libraries to determine if the currently
18579 installed versions of the binaries are compatible.
18580
18581 @item Library_Options
18582 @item Library_GCC
18583 These attributes specify additional low-level options to be used during
18584 library generation, and redefine the actual application used to generate
18585 library.
18586 @end table
18587
18588 @noindent
18589 The GNAT Project Manager takes full care of the library maintenance task,
18590 including recompilation of the source files for which objects do not exist
18591 or are not up to date, assembly of the library archive, and installation of
18592 the library (i.e., copying associated source, object and @file{ALI} files
18593 to the specified location).
18594
18595 Here is a simple library project file:
18596 @smallexample @c ada
18597 project My_Lib is
18598 for Source_Dirs use ("src1", "src2");
18599 for Object_Dir use "obj";
18600 for Library_Name use "mylib";
18601 for Library_Dir use "lib";
18602 for Library_Kind use "dynamic";
18603 end My_lib;
18604 @end smallexample
18605
18606 @noindent
18607 and the compilation command to build and install the library:
18608
18609 @smallexample @c ada
18610 $ gnatmake -Pmy_lib
18611 @end smallexample
18612
18613 @noindent
18614 It is not entirely trivial to perform manually all the steps required to
18615 produce a library. We recommend that you use the GNAT Project Manager
18616 for this task. In special cases where this is not desired, the necessary
18617 steps are discussed below.
18618
18619 There are various possibilities for compiling the units that make up the
18620 library: for example with a Makefile (@pxref{Using the GNU make Utility}) or
18621 with a conventional script. For simple libraries, it is also possible to create
18622 a dummy main program which depends upon all the packages that comprise the
18623 interface of the library. This dummy main program can then be given to
18624 @command{gnatmake}, which will ensure that all necessary objects are built.
18625
18626 After this task is accomplished, you should follow the standard procedure
18627 of the underlying operating system to produce the static or shared library.
18628
18629 Here is an example of such a dummy program:
18630 @smallexample @c ada
18631 @group
18632 with My_Lib.Service1;
18633 with My_Lib.Service2;
18634 with My_Lib.Service3;
18635 procedure My_Lib_Dummy is
18636 begin
18637 null;
18638 end;
18639 @end group
18640 @end smallexample
18641
18642 @noindent
18643 Here are the generic commands that will build an archive or a shared library.
18644
18645 @smallexample
18646 # compiling the library
18647 $ gnatmake -c my_lib_dummy.adb
18648
18649 # we don't need the dummy object itself
18650 $ rm my_lib_dummy.o my_lib_dummy.ali
18651
18652 # create an archive with the remaining objects
18653 $ ar rc libmy_lib.a *.o
18654 # some systems may require "ranlib" to be run as well
18655
18656 # or create a shared library
18657 $ gcc -shared -o libmy_lib.so *.o
18658 # some systems may require the code to have been compiled with -fPIC
18659
18660 # remove the object files that are now in the library
18661 $ rm *.o
18662
18663 # Make the ALI files read-only so that gnatmake will not try to
18664 # regenerate the objects that are in the library
18665 $ chmod -w *.ali
18666 @end smallexample
18667
18668 @noindent
18669 Please note that the library must have a name of the form @file{lib@var{xxx}.a}
18670 or @file{lib@var{xxx}.so} (or @file{lib@var{xxx}.dll} on Windows) in order to
18671 be accessed by the directive @option{-l@var{xxx}} at link time.
18672
18673 @node Installing a library
18674 @subsection Installing a library
18675 @cindex @code{ADA_PROJECT_PATH}
18676
18677 @noindent
18678 If you use project files, library installation is part of the library build
18679 process. Thus no further action is needed in order to make use of the
18680 libraries that are built as part of the general application build. A usable
18681 version of the library is installed in the directory specified by the
18682 @code{Library_Dir} attribute of the library project file.
18683
18684 You may want to install a library in a context different from where the library
18685 is built. This situation arises with third party suppliers, who may want
18686 to distribute a library in binary form where the user is not expected to be
18687 able to recompile the library. The simplest option in this case is to provide
18688 a project file slightly different from the one used to build the library, by
18689 using the @code{externally_built} attribute. For instance, the project
18690 file used to build the library in the previous section can be changed into the
18691 following one when the library is installed:
18692
18693 @smallexample @c projectfile
18694 project My_Lib is
18695 for Source_Dirs use ("src1", "src2");
18696 for Library_Name use "mylib";
18697 for Library_Dir use "lib";
18698 for Library_Kind use "dynamic";
18699 for Externally_Built use "true";
18700 end My_lib;
18701 @end smallexample
18702
18703 @noindent
18704 This project file assumes that the directories @file{src1},
18705 @file{src2}, and @file{lib} exist in
18706 the directory containing the project file. The @code{externally_built}
18707 attribute makes it clear to the GNAT builder that it should not attempt to
18708 recompile any of the units from this library. It allows the library provider to
18709 restrict the source set to the minimum necessary for clients to make use of the
18710 library as described in the first section of this chapter. It is the
18711 responsibility of the library provider to install the necessary sources, ALI
18712 files and libraries in the directories mentioned in the project file. For
18713 convenience, the user's library project file should be installed in a location
18714 that will be searched automatically by the GNAT
18715 builder. These are the directories referenced in the @env{ADA_PROJECT_PATH}
18716 environment variable (@pxref{Importing Projects}), and also the default GNAT
18717 library location that can be queried with @command{gnatls -v} and is usually of
18718 the form $gnat_install_root/lib/gnat.
18719
18720 When project files are not an option, it is also possible, but not recommended,
18721 to install the library so that the sources needed to use the library are on the
18722 Ada source path and the ALI files & libraries be on the Ada Object path (see
18723 @ref{Search Paths and the Run-Time Library (RTL)}. Alternatively, the system
18724 administrator can place general-purpose libraries in the default compiler
18725 paths, by specifying the libraries' location in the configuration files
18726 @file{ada_source_path} and @file{ada_object_path}. These configuration files
18727 must be located in the GNAT installation tree at the same place as the gcc spec
18728 file. The location of the gcc spec file can be determined as follows:
18729 @smallexample
18730 $ gcc -v
18731 @end smallexample
18732
18733 @noindent
18734 The configuration files mentioned above have a simple format: each line
18735 must contain one unique directory name.
18736 Those names are added to the corresponding path
18737 in their order of appearance in the file. The names can be either absolute
18738 or relative; in the latter case, they are relative to where theses files
18739 are located.
18740
18741 The files @file{ada_source_path} and @file{ada_object_path} might not be
18742 present in a
18743 GNAT installation, in which case, GNAT will look for its run-time library in
18744 the directories @file{adainclude} (for the sources) and @file{adalib} (for the
18745 objects and @file{ALI} files). When the files exist, the compiler does not
18746 look in @file{adainclude} and @file{adalib}, and thus the
18747 @file{ada_source_path} file
18748 must contain the location for the GNAT run-time sources (which can simply
18749 be @file{adainclude}). In the same way, the @file{ada_object_path} file must
18750 contain the location for the GNAT run-time objects (which can simply
18751 be @file{adalib}).
18752
18753 You can also specify a new default path to the run-time library at compilation
18754 time with the switch @option{--RTS=rts-path}. You can thus choose / change
18755 the run-time library you want your program to be compiled with. This switch is
18756 recognized by @command{gcc}, @command{gnatmake}, @command{gnatbind},
18757 @command{gnatls}, @command{gnatfind} and @command{gnatxref}.
18758
18759 It is possible to install a library before or after the standard GNAT
18760 library, by reordering the lines in the configuration files. In general, a
18761 library must be installed before the GNAT library if it redefines
18762 any part of it.
18763
18764 @node Using a library
18765 @subsection Using a library
18766
18767 @noindent Once again, the project facility greatly simplifies the use of
18768 libraries. In this context, using a library is just a matter of adding a
18769 @code{with} clause in the user project. For instance, to make use of the
18770 library @code{My_Lib} shown in examples in earlier sections, you can
18771 write:
18772
18773 @smallexample @c projectfile
18774 with "my_lib";
18775 project My_Proj is
18776 @dots{}
18777 end My_Proj;
18778 @end smallexample
18779
18780 Even if you have a third-party, non-Ada library, you can still use GNAT's
18781 Project Manager facility to provide a wrapper for it. For example, the
18782 following project, when @code{with}ed by your main project, will link with the
18783 third-party library @file{liba.a}:
18784
18785 @smallexample @c projectfile
18786 @group
18787 project Liba is
18788 for Externally_Built use "true";
18789 for Source_Files use ();
18790 for Library_Dir use "lib";
18791 for Library_Name use "a";
18792 for Library_Kind use "static";
18793 end Liba;
18794 @end group
18795 @end smallexample
18796 This is an alternative to the use of @code{pragma Linker_Options}. It is
18797 especially interesting in the context of systems with several interdependent
18798 static libraries where finding a proper linker order is not easy and best be
18799 left to the tools having visibility over project dependence information.
18800
18801 @noindent
18802 In order to use an Ada library manually, you need to make sure that this
18803 library is on both your source and object path
18804 (see @ref{Search Paths and the Run-Time Library (RTL)}
18805 and @ref{Search Paths for gnatbind}). Furthermore, when the objects are grouped
18806 in an archive or a shared library, you need to specify the desired
18807 library at link time.
18808
18809 For example, you can use the library @file{mylib} installed in
18810 @file{/dir/my_lib_src} and @file{/dir/my_lib_obj} with the following commands:
18811
18812 @smallexample
18813 $ gnatmake -aI/dir/my_lib_src -aO/dir/my_lib_obj my_appl \
18814 -largs -lmy_lib
18815 @end smallexample
18816
18817 @noindent
18818 This can be expressed more simply:
18819 @smallexample
18820 $ gnatmake my_appl
18821 @end smallexample
18822 @noindent
18823 when the following conditions are met:
18824 @itemize @bullet
18825 @item
18826 @file{/dir/my_lib_src} has been added by the user to the environment
18827 variable @env{ADA_INCLUDE_PATH}, or by the administrator to the file
18828 @file{ada_source_path}
18829 @item
18830 @file{/dir/my_lib_obj} has been added by the user to the environment
18831 variable @env{ADA_OBJECTS_PATH}, or by the administrator to the file
18832 @file{ada_object_path}
18833 @item
18834 a pragma @code{Linker_Options} has been added to one of the sources.
18835 For example:
18836
18837 @smallexample @c ada
18838 pragma Linker_Options ("-lmy_lib");
18839 @end smallexample
18840 @end itemize
18841
18842 @node Stand-alone Ada Libraries
18843 @section Stand-alone Ada Libraries
18844 @cindex Stand-alone library, building, using
18845
18846 @menu
18847 * Introduction to Stand-alone Libraries::
18848 * Building a Stand-alone Library::
18849 * Creating a Stand-alone Library to be used in a non-Ada context::
18850 * Restrictions in Stand-alone Libraries::
18851 @end menu
18852
18853 @node Introduction to Stand-alone Libraries
18854 @subsection Introduction to Stand-alone Libraries
18855
18856 @noindent
18857 A Stand-alone Library (abbreviated ``SAL'') is a library that contains the
18858 necessary code to
18859 elaborate the Ada units that are included in the library. In contrast with
18860 an ordinary library, which consists of all sources, objects and @file{ALI}
18861 files of the
18862 library, a SAL may specify a restricted subset of compilation units
18863 to serve as a library interface. In this case, the fully
18864 self-sufficient set of files will normally consist of an objects
18865 archive, the sources of interface units' specs, and the @file{ALI}
18866 files of interface units.
18867 If an interface spec contains a generic unit or an inlined subprogram,
18868 the body's
18869 source must also be provided; if the units that must be provided in the source
18870 form depend on other units, the source and @file{ALI} files of those must
18871 also be provided.
18872
18873 The main purpose of a SAL is to minimize the recompilation overhead of client
18874 applications when a new version of the library is installed. Specifically,
18875 if the interface sources have not changed, client applications do not need to
18876 be recompiled. If, furthermore, a SAL is provided in the shared form and its
18877 version, controlled by @code{Library_Version} attribute, is not changed,
18878 then the clients do not need to be relinked.
18879
18880 SALs also allow the library providers to minimize the amount of library source
18881 text exposed to the clients. Such ``information hiding'' might be useful or
18882 necessary for various reasons.
18883
18884 Stand-alone libraries are also well suited to be used in an executable whose
18885 main routine is not written in Ada.
18886
18887 @node Building a Stand-alone Library
18888 @subsection Building a Stand-alone Library
18889
18890 @noindent
18891 GNAT's Project facility provides a simple way of building and installing
18892 stand-alone libraries; see @ref{Stand-alone Library Projects}.
18893 To be a Stand-alone Library Project, in addition to the two attributes
18894 that make a project a Library Project (@code{Library_Name} and
18895 @code{Library_Dir}; see @ref{Library Projects}), the attribute
18896 @code{Library_Interface} must be defined. For example:
18897
18898 @smallexample @c projectfile
18899 @group
18900 for Library_Dir use "lib_dir";
18901 for Library_Name use "dummy";
18902 for Library_Interface use ("int1", "int1.child");
18903 @end group
18904 @end smallexample
18905
18906 @noindent
18907 Attribute @code{Library_Interface} has a non-empty string list value,
18908 each string in the list designating a unit contained in an immediate source
18909 of the project file.
18910
18911 When a Stand-alone Library is built, first the binder is invoked to build
18912 a package whose name depends on the library name
18913 (@file{^b~dummy.ads/b^B$DUMMY.ADS/B^} in the example above).
18914 This binder-generated package includes initialization and
18915 finalization procedures whose
18916 names depend on the library name (@code{dummyinit} and @code{dummyfinal}
18917 in the example
18918 above). The object corresponding to this package is included in the library.
18919
18920 You must ensure timely (e.g., prior to any use of interfaces in the SAL)
18921 calling of these procedures if a static SAL is built, or if a shared SAL
18922 is built
18923 with the project-level attribute @code{Library_Auto_Init} set to
18924 @code{"false"}.
18925
18926 For a Stand-Alone Library, only the @file{ALI} files of the Interface Units
18927 (those that are listed in attribute @code{Library_Interface}) are copied to
18928 the Library Directory. As a consequence, only the Interface Units may be
18929 imported from Ada units outside of the library. If other units are imported,
18930 the binding phase will fail.
18931
18932 The attribute @code{Library_Src_Dir} may be specified for a
18933 Stand-Alone Library. @code{Library_Src_Dir} is a simple attribute that has a
18934 single string value. Its value must be the path (absolute or relative to the
18935 project directory) of an existing directory. This directory cannot be the
18936 object directory or one of the source directories, but it can be the same as
18937 the library directory. The sources of the Interface
18938 Units of the library that are needed by an Ada client of the library will be
18939 copied to the designated directory, called the Interface Copy directory.
18940 These sources include the specs of the Interface Units, but they may also
18941 include bodies and subunits, when pragmas @code{Inline} or @code{Inline_Always}
18942 are used, or when there is a generic unit in the spec. Before the sources
18943 are copied to the Interface Copy directory, an attempt is made to delete all
18944 files in the Interface Copy directory.
18945
18946 Building stand-alone libraries by hand is somewhat tedious, but for those
18947 occasions when it is necessary here are the steps that you need to perform:
18948 @itemize @bullet
18949 @item
18950 Compile all library sources.
18951
18952 @item
18953 Invoke the binder with the switch @option{-n} (No Ada main program),
18954 with all the @file{ALI} files of the interfaces, and
18955 with the switch @option{-L} to give specific names to the @code{init}
18956 and @code{final} procedures. For example:
18957 @smallexample
18958 gnatbind -n int1.ali int2.ali -Lsal1
18959 @end smallexample
18960
18961 @item
18962 Compile the binder generated file:
18963 @smallexample
18964 gcc -c b~int2.adb
18965 @end smallexample
18966
18967 @item
18968 Link the dynamic library with all the necessary object files,
18969 indicating to the linker the names of the @code{init} (and possibly
18970 @code{final}) procedures for automatic initialization (and finalization).
18971 The built library should be placed in a directory different from
18972 the object directory.
18973
18974 @item
18975 Copy the @code{ALI} files of the interface to the library directory,
18976 add in this copy an indication that it is an interface to a SAL
18977 (i.e., add a word @option{SL} on the line in the @file{ALI} file that starts
18978 with letter ``P'') and make the modified copy of the @file{ALI} file
18979 read-only.
18980 @end itemize
18981
18982 @noindent
18983 Using SALs is not different from using other libraries
18984 (see @ref{Using a library}).
18985
18986 @node Creating a Stand-alone Library to be used in a non-Ada context
18987 @subsection Creating a Stand-alone Library to be used in a non-Ada context
18988
18989 @noindent
18990 It is easy to adapt the SAL build procedure discussed above for use of a SAL in
18991 a non-Ada context.
18992
18993 The only extra step required is to ensure that library interface subprograms
18994 are compatible with the main program, by means of @code{pragma Export}
18995 or @code{pragma Convention}.
18996
18997 Here is an example of simple library interface for use with C main program:
18998
18999 @smallexample @c ada
19000 package Interface is
19001
19002 procedure Do_Something;
19003 pragma Export (C, Do_Something, "do_something");
19004
19005 procedure Do_Something_Else;
19006 pragma Export (C, Do_Something_Else, "do_something_else");
19007
19008 end Interface;
19009 @end smallexample
19010
19011 @noindent
19012 On the foreign language side, you must provide a ``foreign'' view of the
19013 library interface; remember that it should contain elaboration routines in
19014 addition to interface subprograms.
19015
19016 The example below shows the content of @code{mylib_interface.h} (note
19017 that there is no rule for the naming of this file, any name can be used)
19018 @smallexample
19019 /* the library elaboration procedure */
19020 extern void mylibinit (void);
19021
19022 /* the library finalization procedure */
19023 extern void mylibfinal (void);
19024
19025 /* the interface exported by the library */
19026 extern void do_something (void);
19027 extern void do_something_else (void);
19028 @end smallexample
19029
19030 @noindent
19031 Libraries built as explained above can be used from any program, provided
19032 that the elaboration procedures (named @code{mylibinit} in the previous
19033 example) are called before the library services are used. Any number of
19034 libraries can be used simultaneously, as long as the elaboration
19035 procedure of each library is called.
19036
19037 Below is an example of a C program that uses the @code{mylib} library.
19038
19039 @smallexample
19040 #include "mylib_interface.h"
19041
19042 int
19043 main (void)
19044 @{
19045 /* First, elaborate the library before using it */
19046 mylibinit ();
19047
19048 /* Main program, using the library exported entities */
19049 do_something ();
19050 do_something_else ();
19051
19052 /* Library finalization at the end of the program */
19053 mylibfinal ();
19054 return 0;
19055 @}
19056 @end smallexample
19057
19058 @noindent
19059 Note that invoking any library finalization procedure generated by
19060 @code{gnatbind} shuts down the Ada run-time environment.
19061 Consequently, the
19062 finalization of all Ada libraries must be performed at the end of the program.
19063 No call to these libraries or to the Ada run-time library should be made
19064 after the finalization phase.
19065
19066 @node Restrictions in Stand-alone Libraries
19067 @subsection Restrictions in Stand-alone Libraries
19068
19069 @noindent
19070 The pragmas listed below should be used with caution inside libraries,
19071 as they can create incompatibilities with other Ada libraries:
19072 @itemize @bullet
19073 @item pragma @code{Locking_Policy}
19074 @item pragma @code{Queuing_Policy}
19075 @item pragma @code{Task_Dispatching_Policy}
19076 @item pragma @code{Unreserve_All_Interrupts}
19077 @end itemize
19078
19079 @noindent
19080 When using a library that contains such pragmas, the user must make sure
19081 that all libraries use the same pragmas with the same values. Otherwise,
19082 @code{Program_Error} will
19083 be raised during the elaboration of the conflicting
19084 libraries. The usage of these pragmas and its consequences for the user
19085 should therefore be well documented.
19086
19087 Similarly, the traceback in the exception occurrence mechanism should be
19088 enabled or disabled in a consistent manner across all libraries.
19089 Otherwise, Program_Error will be raised during the elaboration of the
19090 conflicting libraries.
19091
19092 If the @code{Version} or @code{Body_Version}
19093 attributes are used inside a library, then you need to
19094 perform a @code{gnatbind} step that specifies all @file{ALI} files in all
19095 libraries, so that version identifiers can be properly computed.
19096 In practice these attributes are rarely used, so this is unlikely
19097 to be a consideration.
19098
19099 @node Rebuilding the GNAT Run-Time Library
19100 @section Rebuilding the GNAT Run-Time Library
19101 @cindex GNAT Run-Time Library, rebuilding
19102 @cindex Building the GNAT Run-Time Library
19103 @cindex Rebuilding the GNAT Run-Time Library
19104 @cindex Run-Time Library, rebuilding
19105
19106 @noindent
19107 It may be useful to recompile the GNAT library in various contexts, the
19108 most important one being the use of partition-wide configuration pragmas
19109 such as @code{Normalize_Scalars}. A special Makefile called
19110 @code{Makefile.adalib} is provided to that effect and can be found in
19111 the directory containing the GNAT library. The location of this
19112 directory depends on the way the GNAT environment has been installed and can
19113 be determined by means of the command:
19114
19115 @smallexample
19116 $ gnatls -v
19117 @end smallexample
19118
19119 @noindent
19120 The last entry in the object search path usually contains the
19121 gnat library. This Makefile contains its own documentation and in
19122 particular the set of instructions needed to rebuild a new library and
19123 to use it.
19124
19125 @node Using the GNU make Utility
19126 @chapter Using the GNU @code{make} Utility
19127 @findex make
19128
19129 @noindent
19130 This chapter offers some examples of makefiles that solve specific
19131 problems. It does not explain how to write a makefile (@pxref{Top,, GNU
19132 make, make, GNU @code{make}}), nor does it try to replace the
19133 @command{gnatmake} utility (@pxref{The GNAT Make Program gnatmake}).
19134
19135 All the examples in this section are specific to the GNU version of
19136 make. Although @command{make} is a standard utility, and the basic language
19137 is the same, these examples use some advanced features found only in
19138 @code{GNU make}.
19139
19140 @menu
19141 * Using gnatmake in a Makefile::
19142 * Automatically Creating a List of Directories::
19143 * Generating the Command Line Switches::
19144 * Overcoming Command Line Length Limits::
19145 @end menu
19146
19147 @node Using gnatmake in a Makefile
19148 @section Using gnatmake in a Makefile
19149 @findex makefile
19150 @cindex GNU make
19151
19152 @noindent
19153 Complex project organizations can be handled in a very powerful way by
19154 using GNU make combined with gnatmake. For instance, here is a Makefile
19155 which allows you to build each subsystem of a big project into a separate
19156 shared library. Such a makefile allows you to significantly reduce the link
19157 time of very big applications while maintaining full coherence at
19158 each step of the build process.
19159
19160 The list of dependencies are handled automatically by
19161 @command{gnatmake}. The Makefile is simply used to call gnatmake in each of
19162 the appropriate directories.
19163
19164 Note that you should also read the example on how to automatically
19165 create the list of directories
19166 (@pxref{Automatically Creating a List of Directories})
19167 which might help you in case your project has a lot of subdirectories.
19168
19169 @smallexample
19170 @iftex
19171 @leftskip=0cm
19172 @font@heightrm=cmr8
19173 @heightrm
19174 @end iftex
19175 ## This Makefile is intended to be used with the following directory
19176 ## configuration:
19177 ## - The sources are split into a series of csc (computer software components)
19178 ## Each of these csc is put in its own directory.
19179 ## Their name are referenced by the directory names.
19180 ## They will be compiled into shared library (although this would also work
19181 ## with static libraries
19182 ## - The main program (and possibly other packages that do not belong to any
19183 ## csc is put in the top level directory (where the Makefile is).
19184 ## toplevel_dir __ first_csc (sources) __ lib (will contain the library)
19185 ## \_ second_csc (sources) __ lib (will contain the library)
19186 ## \_ @dots{}
19187 ## Although this Makefile is build for shared library, it is easy to modify
19188 ## to build partial link objects instead (modify the lines with -shared and
19189 ## gnatlink below)
19190 ##
19191 ## With this makefile, you can change any file in the system or add any new
19192 ## file, and everything will be recompiled correctly (only the relevant shared
19193 ## objects will be recompiled, and the main program will be re-linked).
19194
19195 # The list of computer software component for your project. This might be
19196 # generated automatically.
19197 CSC_LIST=aa bb cc
19198
19199 # Name of the main program (no extension)
19200 MAIN=main
19201
19202 # If we need to build objects with -fPIC, uncomment the following line
19203 #NEED_FPIC=-fPIC
19204
19205 # The following variable should give the directory containing libgnat.so
19206 # You can get this directory through 'gnatls -v'. This is usually the last
19207 # directory in the Object_Path.
19208 GLIB=@dots{}
19209
19210 # The directories for the libraries
19211 # (This macro expands the list of CSC to the list of shared libraries, you
19212 # could simply use the expanded form:
19213 # LIB_DIR=aa/lib/libaa.so bb/lib/libbb.so cc/lib/libcc.so
19214 LIB_DIR=$@{foreach dir,$@{CSC_LIST@},$@{dir@}/lib/lib$@{dir@}.so@}
19215
19216 $@{MAIN@}: objects $@{LIB_DIR@}
19217 gnatbind $@{MAIN@} $@{CSC_LIST:%=-aO%/lib@} -shared
19218 gnatlink $@{MAIN@} $@{CSC_LIST:%=-l%@}
19219
19220 objects::
19221 # recompile the sources
19222 gnatmake -c -i $@{MAIN@}.adb $@{NEED_FPIC@} $@{CSC_LIST:%=-I%@}
19223
19224 # Note: In a future version of GNAT, the following commands will be simplified
19225 # by a new tool, gnatmlib
19226 $@{LIB_DIR@}:
19227 mkdir -p $@{dir $@@ @}
19228 cd $@{dir $@@ @} && gcc -shared -o $@{notdir $@@ @} ../*.o -L$@{GLIB@} -lgnat
19229 cd $@{dir $@@ @} && cp -f ../*.ali .
19230
19231 # The dependencies for the modules
19232 # Note that we have to force the expansion of *.o, since in some cases
19233 # make won't be able to do it itself.
19234 aa/lib/libaa.so: $@{wildcard aa/*.o@}
19235 bb/lib/libbb.so: $@{wildcard bb/*.o@}
19236 cc/lib/libcc.so: $@{wildcard cc/*.o@}
19237
19238 # Make sure all of the shared libraries are in the path before starting the
19239 # program
19240 run::
19241 LD_LIBRARY_PATH=`pwd`/aa/lib:`pwd`/bb/lib:`pwd`/cc/lib ./$@{MAIN@}
19242
19243 clean::
19244 $@{RM@} -rf $@{CSC_LIST:%=%/lib@}
19245 $@{RM@} $@{CSC_LIST:%=%/*.ali@}
19246 $@{RM@} $@{CSC_LIST:%=%/*.o@}
19247 $@{RM@} *.o *.ali $@{MAIN@}
19248 @end smallexample
19249
19250 @node Automatically Creating a List of Directories
19251 @section Automatically Creating a List of Directories
19252
19253 @noindent
19254 In most makefiles, you will have to specify a list of directories, and
19255 store it in a variable. For small projects, it is often easier to
19256 specify each of them by hand, since you then have full control over what
19257 is the proper order for these directories, which ones should be
19258 included.
19259
19260 However, in larger projects, which might involve hundreds of
19261 subdirectories, it might be more convenient to generate this list
19262 automatically.
19263
19264 The example below presents two methods. The first one, although less
19265 general, gives you more control over the list. It involves wildcard
19266 characters, that are automatically expanded by @command{make}. Its
19267 shortcoming is that you need to explicitly specify some of the
19268 organization of your project, such as for instance the directory tree
19269 depth, whether some directories are found in a separate tree, @enddots{}
19270
19271 The second method is the most general one. It requires an external
19272 program, called @command{find}, which is standard on all Unix systems. All
19273 the directories found under a given root directory will be added to the
19274 list.
19275
19276 @smallexample
19277 @iftex
19278 @leftskip=0cm
19279 @font@heightrm=cmr8
19280 @heightrm
19281 @end iftex
19282 # The examples below are based on the following directory hierarchy:
19283 # All the directories can contain any number of files
19284 # ROOT_DIRECTORY -> a -> aa -> aaa
19285 # -> ab
19286 # -> ac
19287 # -> b -> ba -> baa
19288 # -> bb
19289 # -> bc
19290 # This Makefile creates a variable called DIRS, that can be reused any time
19291 # you need this list (see the other examples in this section)
19292
19293 # The root of your project's directory hierarchy
19294 ROOT_DIRECTORY=.
19295
19296 ####
19297 # First method: specify explicitly the list of directories
19298 # This allows you to specify any subset of all the directories you need.
19299 ####
19300
19301 DIRS := a/aa/ a/ab/ b/ba/
19302
19303 ####
19304 # Second method: use wildcards
19305 # Note that the argument(s) to wildcard below should end with a '/'.
19306 # Since wildcards also return file names, we have to filter them out
19307 # to avoid duplicate directory names.
19308 # We thus use make's @code{dir} and @code{sort} functions.
19309 # It sets DIRs to the following value (note that the directories aaa and baa
19310 # are not given, unless you change the arguments to wildcard).
19311 # DIRS= ./a/a/ ./b/ ./a/aa/ ./a/ab/ ./a/ac/ ./b/ba/ ./b/bb/ ./b/bc/
19312 ####
19313
19314 DIRS := $@{sort $@{dir $@{wildcard $@{ROOT_DIRECTORY@}/*/
19315 $@{ROOT_DIRECTORY@}/*/*/@}@}@}
19316
19317 ####
19318 # Third method: use an external program
19319 # This command is much faster if run on local disks, avoiding NFS slowdowns.
19320 # This is the most complete command: it sets DIRs to the following value:
19321 # DIRS= ./a ./a/aa ./a/aa/aaa ./a/ab ./a/ac ./b ./b/ba ./b/ba/baa ./b/bb ./b/bc
19322 ####
19323
19324 DIRS := $@{shell find $@{ROOT_DIRECTORY@} -type d -print@}
19325
19326 @end smallexample
19327
19328 @node Generating the Command Line Switches
19329 @section Generating the Command Line Switches
19330
19331 @noindent
19332 Once you have created the list of directories as explained in the
19333 previous section (@pxref{Automatically Creating a List of Directories}),
19334 you can easily generate the command line arguments to pass to gnatmake.
19335
19336 For the sake of completeness, this example assumes that the source path
19337 is not the same as the object path, and that you have two separate lists
19338 of directories.
19339
19340 @smallexample
19341 # see "Automatically creating a list of directories" to create
19342 # these variables
19343 SOURCE_DIRS=
19344 OBJECT_DIRS=
19345
19346 GNATMAKE_SWITCHES := $@{patsubst %,-aI%,$@{SOURCE_DIRS@}@}
19347 GNATMAKE_SWITCHES += $@{patsubst %,-aO%,$@{OBJECT_DIRS@}@}
19348
19349 all:
19350 gnatmake $@{GNATMAKE_SWITCHES@} main_unit
19351 @end smallexample
19352
19353 @node Overcoming Command Line Length Limits
19354 @section Overcoming Command Line Length Limits
19355
19356 @noindent
19357 One problem that might be encountered on big projects is that many
19358 operating systems limit the length of the command line. It is thus hard to give
19359 gnatmake the list of source and object directories.
19360
19361 This example shows how you can set up environment variables, which will
19362 make @command{gnatmake} behave exactly as if the directories had been
19363 specified on the command line, but have a much higher length limit (or
19364 even none on most systems).
19365
19366 It assumes that you have created a list of directories in your Makefile,
19367 using one of the methods presented in
19368 @ref{Automatically Creating a List of Directories}.
19369 For the sake of completeness, we assume that the object
19370 path (where the ALI files are found) is different from the sources patch.
19371
19372 Note a small trick in the Makefile below: for efficiency reasons, we
19373 create two temporary variables (SOURCE_LIST and OBJECT_LIST), that are
19374 expanded immediately by @code{make}. This way we overcome the standard
19375 make behavior which is to expand the variables only when they are
19376 actually used.
19377
19378 On Windows, if you are using the standard Windows command shell, you must
19379 replace colons with semicolons in the assignments to these variables.
19380
19381 @smallexample
19382 @iftex
19383 @leftskip=0cm
19384 @font@heightrm=cmr8
19385 @heightrm
19386 @end iftex
19387 # In this example, we create both ADA_INCLUDE_PATH and ADA_OBJECT_PATH.
19388 # This is the same thing as putting the -I arguments on the command line.
19389 # (the equivalent of using -aI on the command line would be to define
19390 # only ADA_INCLUDE_PATH, the equivalent of -aO is ADA_OBJECT_PATH).
19391 # You can of course have different values for these variables.
19392 #
19393 # Note also that we need to keep the previous values of these variables, since
19394 # they might have been set before running 'make' to specify where the GNAT
19395 # library is installed.
19396
19397 # see "Automatically creating a list of directories" to create these
19398 # variables
19399 SOURCE_DIRS=
19400 OBJECT_DIRS=
19401
19402 empty:=
19403 space:=$@{empty@} $@{empty@}
19404 SOURCE_LIST := $@{subst $@{space@},:,$@{SOURCE_DIRS@}@}
19405 OBJECT_LIST := $@{subst $@{space@},:,$@{OBJECT_DIRS@}@}
19406 ADA_INCLUDE_PATH += $@{SOURCE_LIST@}
19407 ADA_OBJECT_PATH += $@{OBJECT_LIST@}
19408 export ADA_INCLUDE_PATH
19409 export ADA_OBJECT_PATH
19410
19411 all:
19412 gnatmake main_unit
19413 @end smallexample
19414 @end ifclear
19415
19416 @node Memory Management Issues
19417 @chapter Memory Management Issues
19418
19419 @noindent
19420 This chapter describes some useful memory pools provided in the GNAT library
19421 and in particular the GNAT Debug Pool facility, which can be used to detect
19422 incorrect uses of access values (including ``dangling references'').
19423 @ifclear vms
19424 It also describes the @command{gnatmem} tool, which can be used to track down
19425 ``memory leaks''.
19426 @end ifclear
19427
19428 @menu
19429 * Some Useful Memory Pools::
19430 * The GNAT Debug Pool Facility::
19431 @ifclear vms
19432 * The gnatmem Tool::
19433 @end ifclear
19434 @end menu
19435
19436 @node Some Useful Memory Pools
19437 @section Some Useful Memory Pools
19438 @findex Memory Pool
19439 @cindex storage, pool
19440
19441 @noindent
19442 The @code{System.Pool_Global} package offers the Unbounded_No_Reclaim_Pool
19443 storage pool. Allocations use the standard system call @code{malloc} while
19444 deallocations use the standard system call @code{free}. No reclamation is
19445 performed when the pool goes out of scope. For performance reasons, the
19446 standard default Ada allocators/deallocators do not use any explicit storage
19447 pools but if they did, they could use this storage pool without any change in
19448 behavior. That is why this storage pool is used when the user
19449 manages to make the default implicit allocator explicit as in this example:
19450 @smallexample @c ada
19451 type T1 is access Something;
19452 -- no Storage pool is defined for T2
19453 type T2 is access Something_Else;
19454 for T2'Storage_Pool use T1'Storage_Pool;
19455 -- the above is equivalent to
19456 for T2'Storage_Pool use System.Pool_Global.Global_Pool_Object;
19457 @end smallexample
19458
19459 @noindent
19460 The @code{System.Pool_Local} package offers the Unbounded_Reclaim_Pool storage
19461 pool. The allocation strategy is similar to @code{Pool_Local}'s
19462 except that the all
19463 storage allocated with this pool is reclaimed when the pool object goes out of
19464 scope. This pool provides a explicit mechanism similar to the implicit one
19465 provided by several Ada 83 compilers for allocations performed through a local
19466 access type and whose purpose was to reclaim memory when exiting the
19467 scope of a given local access. As an example, the following program does not
19468 leak memory even though it does not perform explicit deallocation:
19469
19470 @smallexample @c ada
19471 with System.Pool_Local;
19472 procedure Pooloc1 is
19473 procedure Internal is
19474 type A is access Integer;
19475 X : System.Pool_Local.Unbounded_Reclaim_Pool;
19476 for A'Storage_Pool use X;
19477 v : A;
19478 begin
19479 for I in 1 .. 50 loop
19480 v := new Integer;
19481 end loop;
19482 end Internal;
19483 begin
19484 for I in 1 .. 100 loop
19485 Internal;
19486 end loop;
19487 end Pooloc1;
19488 @end smallexample
19489
19490 @noindent
19491 The @code{System.Pool_Size} package implements the Stack_Bounded_Pool used when
19492 @code{Storage_Size} is specified for an access type.
19493 The whole storage for the pool is
19494 allocated at once, usually on the stack at the point where the access type is
19495 elaborated. It is automatically reclaimed when exiting the scope where the
19496 access type is defined. This package is not intended to be used directly by the
19497 user and it is implicitly used for each such declaration:
19498
19499 @smallexample @c ada
19500 type T1 is access Something;
19501 for T1'Storage_Size use 10_000;
19502 @end smallexample
19503
19504 @node The GNAT Debug Pool Facility
19505 @section The GNAT Debug Pool Facility
19506 @findex Debug Pool
19507 @cindex storage, pool, memory corruption
19508
19509 @noindent
19510 The use of unchecked deallocation and unchecked conversion can easily
19511 lead to incorrect memory references. The problems generated by such
19512 references are usually difficult to tackle because the symptoms can be
19513 very remote from the origin of the problem. In such cases, it is
19514 very helpful to detect the problem as early as possible. This is the
19515 purpose of the Storage Pool provided by @code{GNAT.Debug_Pools}.
19516
19517 In order to use the GNAT specific debugging pool, the user must
19518 associate a debug pool object with each of the access types that may be
19519 related to suspected memory problems. See Ada Reference Manual 13.11.
19520 @smallexample @c ada
19521 type Ptr is access Some_Type;
19522 Pool : GNAT.Debug_Pools.Debug_Pool;
19523 for Ptr'Storage_Pool use Pool;
19524 @end smallexample
19525
19526 @noindent
19527 @code{GNAT.Debug_Pools} is derived from a GNAT-specific kind of
19528 pool: the @code{Checked_Pool}. Such pools, like standard Ada storage pools,
19529 allow the user to redefine allocation and deallocation strategies. They
19530 also provide a checkpoint for each dereference, through the use of
19531 the primitive operation @code{Dereference} which is implicitly called at
19532 each dereference of an access value.
19533
19534 Once an access type has been associated with a debug pool, operations on
19535 values of the type may raise four distinct exceptions,
19536 which correspond to four potential kinds of memory corruption:
19537 @itemize @bullet
19538 @item
19539 @code{GNAT.Debug_Pools.Accessing_Not_Allocated_Storage}
19540 @item
19541 @code{GNAT.Debug_Pools.Accessing_Deallocated_Storage}
19542 @item
19543 @code{GNAT.Debug_Pools.Freeing_Not_Allocated_Storage}
19544 @item
19545 @code{GNAT.Debug_Pools.Freeing_Deallocated_Storage }
19546 @end itemize
19547
19548 @noindent
19549 For types associated with a Debug_Pool, dynamic allocation is performed using
19550 the standard GNAT allocation routine. References to all allocated chunks of
19551 memory are kept in an internal dictionary. Several deallocation strategies are
19552 provided, whereupon the user can choose to release the memory to the system,
19553 keep it allocated for further invalid access checks, or fill it with an easily
19554 recognizable pattern for debug sessions. The memory pattern is the old IBM
19555 hexadecimal convention: @code{16#DEADBEEF#}.
19556
19557 See the documentation in the file g-debpoo.ads for more information on the
19558 various strategies.
19559
19560 Upon each dereference, a check is made that the access value denotes a
19561 properly allocated memory location. Here is a complete example of use of
19562 @code{Debug_Pools}, that includes typical instances of memory corruption:
19563 @smallexample @c ada
19564 @iftex
19565 @leftskip=0cm
19566 @end iftex
19567 with Gnat.Io; use Gnat.Io;
19568 with Unchecked_Deallocation;
19569 with Unchecked_Conversion;
19570 with GNAT.Debug_Pools;
19571 with System.Storage_Elements;
19572 with Ada.Exceptions; use Ada.Exceptions;
19573 procedure Debug_Pool_Test is
19574
19575 type T is access Integer;
19576 type U is access all T;
19577
19578 P : GNAT.Debug_Pools.Debug_Pool;
19579 for T'Storage_Pool use P;
19580
19581 procedure Free is new Unchecked_Deallocation (Integer, T);
19582 function UC is new Unchecked_Conversion (U, T);
19583 A, B : aliased T;
19584
19585 procedure Info is new GNAT.Debug_Pools.Print_Info(Put_Line);
19586
19587 begin
19588 Info (P);
19589 A := new Integer;
19590 B := new Integer;
19591 B := A;
19592 Info (P);
19593 Free (A);
19594 begin
19595 Put_Line (Integer'Image(B.all));
19596 exception
19597 when E : others => Put_Line ("raised: " & Exception_Name (E));
19598 end;
19599 begin
19600 Free (B);
19601 exception
19602 when E : others => Put_Line ("raised: " & Exception_Name (E));
19603 end;
19604 B := UC(A'Access);
19605 begin
19606 Put_Line (Integer'Image(B.all));
19607 exception
19608 when E : others => Put_Line ("raised: " & Exception_Name (E));
19609 end;
19610 begin
19611 Free (B);
19612 exception
19613 when E : others => Put_Line ("raised: " & Exception_Name (E));
19614 end;
19615 Info (P);
19616 end Debug_Pool_Test;
19617 @end smallexample
19618
19619 @noindent
19620 The debug pool mechanism provides the following precise diagnostics on the
19621 execution of this erroneous program:
19622 @smallexample
19623 Debug Pool info:
19624 Total allocated bytes : 0
19625 Total deallocated bytes : 0
19626 Current Water Mark: 0
19627 High Water Mark: 0
19628
19629 Debug Pool info:
19630 Total allocated bytes : 8
19631 Total deallocated bytes : 0
19632 Current Water Mark: 8
19633 High Water Mark: 8
19634
19635 raised: GNAT.DEBUG_POOLS.ACCESSING_DEALLOCATED_STORAGE
19636 raised: GNAT.DEBUG_POOLS.FREEING_DEALLOCATED_STORAGE
19637 raised: GNAT.DEBUG_POOLS.ACCESSING_NOT_ALLOCATED_STORAGE
19638 raised: GNAT.DEBUG_POOLS.FREEING_NOT_ALLOCATED_STORAGE
19639 Debug Pool info:
19640 Total allocated bytes : 8
19641 Total deallocated bytes : 4
19642 Current Water Mark: 4
19643 High Water Mark: 8
19644 @end smallexample
19645
19646 @ifclear vms
19647 @node The gnatmem Tool
19648 @section The @command{gnatmem} Tool
19649 @findex gnatmem
19650
19651 @noindent
19652 The @code{gnatmem} utility monitors dynamic allocation and
19653 deallocation activity in a program, and displays information about
19654 incorrect deallocations and possible sources of memory leaks.
19655 It provides three type of information:
19656 @itemize @bullet
19657 @item
19658 General information concerning memory management, such as the total
19659 number of allocations and deallocations, the amount of allocated
19660 memory and the high water mark, i.e.@: the largest amount of allocated
19661 memory in the course of program execution.
19662
19663 @item
19664 Backtraces for all incorrect deallocations, that is to say deallocations
19665 which do not correspond to a valid allocation.
19666
19667 @item
19668 Information on each allocation that is potentially the origin of a memory
19669 leak.
19670 @end itemize
19671
19672 @menu
19673 * Running gnatmem::
19674 * Switches for gnatmem::
19675 * Example of gnatmem Usage::
19676 @end menu
19677
19678 @node Running gnatmem
19679 @subsection Running @code{gnatmem}
19680
19681 @noindent
19682 @code{gnatmem} makes use of the output created by the special version of
19683 allocation and deallocation routines that record call information. This
19684 allows to obtain accurate dynamic memory usage history at a minimal cost to
19685 the execution speed. Note however, that @code{gnatmem} is not supported on
19686 all platforms (currently, it is supported on AIX, HP-UX, GNU/Linux,
19687 Solaris and Windows NT/2000/XP (x86).
19688
19689 @noindent
19690 The @code{gnatmem} command has the form
19691
19692 @smallexample
19693 $ gnatmem @ovar{switches} user_program
19694 @end smallexample
19695
19696 @noindent
19697 The program must have been linked with the instrumented version of the
19698 allocation and deallocation routines. This is done by linking with the
19699 @file{libgmem.a} library. For correct symbolic backtrace information,
19700 the user program should be compiled with debugging options
19701 (see @ref{Switches for gcc}). For example to build @file{my_program}:
19702
19703 @smallexample
19704 $ gnatmake -g my_program -largs -lgmem
19705 @end smallexample
19706
19707 @noindent
19708 As library @file{libgmem.a} contains an alternate body for package
19709 @code{System.Memory}, @file{s-memory.adb} should not be compiled and linked
19710 when an executable is linked with library @file{libgmem.a}. It is then not
19711 recommended to use @command{gnatmake} with switch @option{^-a^/ALL_FILES^}.
19712
19713 @noindent
19714 When @file{my_program} is executed, the file @file{gmem.out} is produced.
19715 This file contains information about all allocations and deallocations
19716 performed by the program. It is produced by the instrumented allocations and
19717 deallocations routines and will be used by @code{gnatmem}.
19718
19719 In order to produce symbolic backtrace information for allocations and
19720 deallocations performed by the GNAT run-time library, you need to use a
19721 version of that library that has been compiled with the @option{-g} switch
19722 (see @ref{Rebuilding the GNAT Run-Time Library}).
19723
19724 Gnatmem must be supplied with the @file{gmem.out} file and the executable to
19725 examine. If the location of @file{gmem.out} file was not explicitly supplied by
19726 @option{-i} switch, gnatmem will assume that this file can be found in the
19727 current directory. For example, after you have executed @file{my_program},
19728 @file{gmem.out} can be analyzed by @code{gnatmem} using the command:
19729
19730 @smallexample
19731 $ gnatmem my_program
19732 @end smallexample
19733
19734 @noindent
19735 This will produce the output with the following format:
19736
19737 *************** debut cc
19738 @smallexample
19739 $ gnatmem my_program
19740
19741 Global information
19742 ------------------
19743 Total number of allocations : 45
19744 Total number of deallocations : 6
19745 Final Water Mark (non freed mem) : 11.29 Kilobytes
19746 High Water Mark : 11.40 Kilobytes
19747
19748 .
19749 .
19750 .
19751 Allocation Root # 2
19752 -------------------
19753 Number of non freed allocations : 11
19754 Final Water Mark (non freed mem) : 1.16 Kilobytes
19755 High Water Mark : 1.27 Kilobytes
19756 Backtrace :
19757 my_program.adb:23 my_program.alloc
19758 .
19759 .
19760 .
19761 @end smallexample
19762
19763 The first block of output gives general information. In this case, the
19764 Ada construct ``@code{@b{new}}'' was executed 45 times, and only 6 calls to an
19765 Unchecked_Deallocation routine occurred.
19766
19767 @noindent
19768 Subsequent paragraphs display information on all allocation roots.
19769 An allocation root is a specific point in the execution of the program
19770 that generates some dynamic allocation, such as a ``@code{@b{new}}''
19771 construct. This root is represented by an execution backtrace (or subprogram
19772 call stack). By default the backtrace depth for allocations roots is 1, so
19773 that a root corresponds exactly to a source location. The backtrace can
19774 be made deeper, to make the root more specific.
19775
19776 @node Switches for gnatmem
19777 @subsection Switches for @code{gnatmem}
19778
19779 @noindent
19780 @code{gnatmem} recognizes the following switches:
19781
19782 @table @option
19783
19784 @item -q
19785 @cindex @option{-q} (@code{gnatmem})
19786 Quiet. Gives the minimum output needed to identify the origin of the
19787 memory leaks. Omits statistical information.
19788
19789 @item @var{N}
19790 @cindex @var{N} (@code{gnatmem})
19791 N is an integer literal (usually between 1 and 10) which controls the
19792 depth of the backtraces defining allocation root. The default value for
19793 N is 1. The deeper the backtrace, the more precise the localization of
19794 the root. Note that the total number of roots can depend on this
19795 parameter. This parameter must be specified @emph{before} the name of the
19796 executable to be analyzed, to avoid ambiguity.
19797
19798 @item -b n
19799 @cindex @option{-b} (@code{gnatmem})
19800 This switch has the same effect as just depth parameter.
19801
19802 @item -i @var{file}
19803 @cindex @option{-i} (@code{gnatmem})
19804 Do the @code{gnatmem} processing starting from @file{file}, rather than
19805 @file{gmem.out} in the current directory.
19806
19807 @item -m n
19808 @cindex @option{-m} (@code{gnatmem})
19809 This switch causes @code{gnatmem} to mask the allocation roots that have less
19810 than n leaks. The default value is 1. Specifying the value of 0 will allow to
19811 examine even the roots that didn't result in leaks.
19812
19813 @item -s order
19814 @cindex @option{-s} (@code{gnatmem})
19815 This switch causes @code{gnatmem} to sort the allocation roots according to the
19816 specified order of sort criteria, each identified by a single letter. The
19817 currently supported criteria are @code{n, h, w} standing respectively for
19818 number of unfreed allocations, high watermark, and final watermark
19819 corresponding to a specific root. The default order is @code{nwh}.
19820
19821 @end table
19822
19823 @node Example of gnatmem Usage
19824 @subsection Example of @code{gnatmem} Usage
19825
19826 @noindent
19827 The following example shows the use of @code{gnatmem}
19828 on a simple memory-leaking program.
19829 Suppose that we have the following Ada program:
19830
19831 @smallexample @c ada
19832 @group
19833 @cartouche
19834 with Unchecked_Deallocation;
19835 procedure Test_Gm is
19836
19837 type T is array (1..1000) of Integer;
19838 type Ptr is access T;
19839 procedure Free is new Unchecked_Deallocation (T, Ptr);
19840 A : Ptr;
19841
19842 procedure My_Alloc is
19843 begin
19844 A := new T;
19845 end My_Alloc;
19846
19847 procedure My_DeAlloc is
19848 B : Ptr := A;
19849 begin
19850 Free (B);
19851 end My_DeAlloc;
19852
19853 begin
19854 My_Alloc;
19855 for I in 1 .. 5 loop
19856 for J in I .. 5 loop
19857 My_Alloc;
19858 end loop;
19859 My_Dealloc;
19860 end loop;
19861 end;
19862 @end cartouche
19863 @end group
19864 @end smallexample
19865
19866 @noindent
19867 The program needs to be compiled with debugging option and linked with
19868 @code{gmem} library:
19869
19870 @smallexample
19871 $ gnatmake -g test_gm -largs -lgmem
19872 @end smallexample
19873
19874 @noindent
19875 Then we execute the program as usual:
19876
19877 @smallexample
19878 $ test_gm
19879 @end smallexample
19880
19881 @noindent
19882 Then @code{gnatmem} is invoked simply with
19883 @smallexample
19884 $ gnatmem test_gm
19885 @end smallexample
19886
19887 @noindent
19888 which produces the following output (result may vary on different platforms):
19889
19890 @smallexample
19891 Global information
19892 ------------------
19893 Total number of allocations : 18
19894 Total number of deallocations : 5
19895 Final Water Mark (non freed mem) : 53.00 Kilobytes
19896 High Water Mark : 56.90 Kilobytes
19897
19898 Allocation Root # 1
19899 -------------------
19900 Number of non freed allocations : 11
19901 Final Water Mark (non freed mem) : 42.97 Kilobytes
19902 High Water Mark : 46.88 Kilobytes
19903 Backtrace :
19904 test_gm.adb:11 test_gm.my_alloc
19905
19906 Allocation Root # 2
19907 -------------------
19908 Number of non freed allocations : 1
19909 Final Water Mark (non freed mem) : 10.02 Kilobytes
19910 High Water Mark : 10.02 Kilobytes
19911 Backtrace :
19912 s-secsta.adb:81 system.secondary_stack.ss_init
19913
19914 Allocation Root # 3
19915 -------------------
19916 Number of non freed allocations : 1
19917 Final Water Mark (non freed mem) : 12 Bytes
19918 High Water Mark : 12 Bytes
19919 Backtrace :
19920 s-secsta.adb:181 system.secondary_stack.ss_init
19921 @end smallexample
19922
19923 @noindent
19924 Note that the GNAT run time contains itself a certain number of
19925 allocations that have no corresponding deallocation,
19926 as shown here for root #2 and root
19927 #3. This is a normal behavior when the number of non-freed allocations
19928 is one, it allocates dynamic data structures that the run time needs for
19929 the complete lifetime of the program. Note also that there is only one
19930 allocation root in the user program with a single line back trace:
19931 test_gm.adb:11 test_gm.my_alloc, whereas a careful analysis of the
19932 program shows that 'My_Alloc' is called at 2 different points in the
19933 source (line 21 and line 24). If those two allocation roots need to be
19934 distinguished, the backtrace depth parameter can be used:
19935
19936 @smallexample
19937 $ gnatmem 3 test_gm
19938 @end smallexample
19939
19940 @noindent
19941 which will give the following output:
19942
19943 @smallexample
19944 Global information
19945 ------------------
19946 Total number of allocations : 18
19947 Total number of deallocations : 5
19948 Final Water Mark (non freed mem) : 53.00 Kilobytes
19949 High Water Mark : 56.90 Kilobytes
19950
19951 Allocation Root # 1
19952 -------------------
19953 Number of non freed allocations : 10
19954 Final Water Mark (non freed mem) : 39.06 Kilobytes
19955 High Water Mark : 42.97 Kilobytes
19956 Backtrace :
19957 test_gm.adb:11 test_gm.my_alloc
19958 test_gm.adb:24 test_gm
19959 b_test_gm.c:52 main
19960
19961 Allocation Root # 2
19962 -------------------
19963 Number of non freed allocations : 1
19964 Final Water Mark (non freed mem) : 10.02 Kilobytes
19965 High Water Mark : 10.02 Kilobytes
19966 Backtrace :
19967 s-secsta.adb:81 system.secondary_stack.ss_init
19968 s-secsta.adb:283 <system__secondary_stack___elabb>
19969 b_test_gm.c:33 adainit
19970
19971 Allocation Root # 3
19972 -------------------
19973 Number of non freed allocations : 1
19974 Final Water Mark (non freed mem) : 3.91 Kilobytes
19975 High Water Mark : 3.91 Kilobytes
19976 Backtrace :
19977 test_gm.adb:11 test_gm.my_alloc
19978 test_gm.adb:21 test_gm
19979 b_test_gm.c:52 main
19980
19981 Allocation Root # 4
19982 -------------------
19983 Number of non freed allocations : 1
19984 Final Water Mark (non freed mem) : 12 Bytes
19985 High Water Mark : 12 Bytes
19986 Backtrace :
19987 s-secsta.adb:181 system.secondary_stack.ss_init
19988 s-secsta.adb:283 <system__secondary_stack___elabb>
19989 b_test_gm.c:33 adainit
19990 @end smallexample
19991
19992 @noindent
19993 The allocation root #1 of the first example has been split in 2 roots #1
19994 and #3 thanks to the more precise associated backtrace.
19995
19996 @end ifclear
19997
19998 @node Stack Related Facilities
19999 @chapter Stack Related Facilities
20000
20001 @noindent
20002 This chapter describes some useful tools associated with stack
20003 checking and analysis. In
20004 particular, it deals with dynamic and static stack usage measurements.
20005
20006 @menu
20007 * Stack Overflow Checking::
20008 * Static Stack Usage Analysis::
20009 * Dynamic Stack Usage Analysis::
20010 @end menu
20011
20012 @node Stack Overflow Checking
20013 @section Stack Overflow Checking
20014 @cindex Stack Overflow Checking
20015 @cindex -fstack-check
20016
20017 @noindent
20018 For most operating systems, @command{gcc} does not perform stack overflow
20019 checking by default. This means that if the main environment task or
20020 some other task exceeds the available stack space, then unpredictable
20021 behavior will occur. Most native systems offer some level of protection by
20022 adding a guard page at the end of each task stack. This mechanism is usually
20023 not enough for dealing properly with stack overflow situations because
20024 a large local variable could ``jump'' above the guard page.
20025 Furthermore, when the
20026 guard page is hit, there may not be any space left on the stack for executing
20027 the exception propagation code. Enabling stack checking avoids
20028 such situations.
20029
20030 To activate stack checking, compile all units with the gcc option
20031 @option{-fstack-check}. For example:
20032
20033 @smallexample
20034 gcc -c -fstack-check package1.adb
20035 @end smallexample
20036
20037 @noindent
20038 Units compiled with this option will generate extra instructions to check
20039 that any use of the stack (for procedure calls or for declaring local
20040 variables in declare blocks) does not exceed the available stack space.
20041 If the space is exceeded, then a @code{Storage_Error} exception is raised.
20042
20043 For declared tasks, the stack size is controlled by the size
20044 given in an applicable @code{Storage_Size} pragma or by the value specified
20045 at bind time with @option{-d} (@pxref{Switches for gnatbind}) or is set to
20046 the default size as defined in the GNAT runtime otherwise.
20047
20048 For the environment task, the stack size depends on
20049 system defaults and is unknown to the compiler. Stack checking
20050 may still work correctly if a fixed
20051 size stack is allocated, but this cannot be guaranteed.
20052 @ifclear vms
20053 To ensure that a clean exception is signalled for stack
20054 overflow, set the environment variable
20055 @env{GNAT_STACK_LIMIT} to indicate the maximum
20056 stack area that can be used, as in:
20057 @cindex GNAT_STACK_LIMIT
20058
20059 @smallexample
20060 SET GNAT_STACK_LIMIT 1600
20061 @end smallexample
20062
20063 @noindent
20064 The limit is given in kilobytes, so the above declaration would
20065 set the stack limit of the environment task to 1.6 megabytes.
20066 Note that the only purpose of this usage is to limit the amount
20067 of stack used by the environment task. If it is necessary to
20068 increase the amount of stack for the environment task, then this
20069 is an operating systems issue, and must be addressed with the
20070 appropriate operating systems commands.
20071 @end ifclear
20072 @ifset vms
20073 To have a fixed size stack in the environment task, the stack must be put
20074 in the P0 address space and its size specified. Use these switches to
20075 create a p0 image:
20076
20077 @smallexample
20078 gnatmake my_progs -largs "-Wl,--opt=STACK=4000,/p0image"
20079 @end smallexample
20080
20081 @noindent
20082 The quotes are required to keep case. The number after @samp{STACK=} is the
20083 size of the environmental task stack in pagelets (512 bytes). In this example
20084 the stack size is about 2 megabytes.
20085
20086 @noindent
20087 A consequence of the @option{/p0image} qualifier is also to makes RMS buffers
20088 be placed in P0 space. Refer to @cite{HP OpenVMS Linker Utility Manual} for
20089 more details about the @option{/p0image} qualifier and the @option{stack}
20090 option.
20091 @end ifset
20092
20093 @node Static Stack Usage Analysis
20094 @section Static Stack Usage Analysis
20095 @cindex Static Stack Usage Analysis
20096 @cindex -fstack-usage
20097
20098 @noindent
20099 A unit compiled with @option{-fstack-usage} will generate an extra file
20100 that specifies
20101 the maximum amount of stack used, on a per-function basis.
20102 The file has the same
20103 basename as the target object file with a @file{.su} extension.
20104 Each line of this file is made up of three fields:
20105
20106 @itemize
20107 @item
20108 The name of the function.
20109 @item
20110 A number of bytes.
20111 @item
20112 One or more qualifiers: @code{static}, @code{dynamic}, @code{bounded}.
20113 @end itemize
20114
20115 The second field corresponds to the size of the known part of the function
20116 frame.
20117
20118 The qualifier @code{static} means that the function frame size
20119 is purely static.
20120 It usually means that all local variables have a static size.
20121 In this case, the second field is a reliable measure of the function stack
20122 utilization.
20123
20124 The qualifier @code{dynamic} means that the function frame size is not static.
20125 It happens mainly when some local variables have a dynamic size. When this
20126 qualifier appears alone, the second field is not a reliable measure
20127 of the function stack analysis. When it is qualified with @code{bounded}, it
20128 means that the second field is a reliable maximum of the function stack
20129 utilization.
20130
20131 @node Dynamic Stack Usage Analysis
20132 @section Dynamic Stack Usage Analysis
20133
20134 @noindent
20135 It is possible to measure the maximum amount of stack used by a task, by
20136 adding a switch to @command{gnatbind}, as:
20137
20138 @smallexample
20139 $ gnatbind -u0 file
20140 @end smallexample
20141
20142 @noindent
20143 With this option, at each task termination, its stack usage is output on
20144 @file{stderr}.
20145 It is not always convenient to output the stack usage when the program
20146 is still running. Hence, it is possible to delay this output until program
20147 termination. for a given number of tasks specified as the argument of the
20148 @option{-u} option. For instance:
20149
20150 @smallexample
20151 $ gnatbind -u100 file
20152 @end smallexample
20153
20154 @noindent
20155 will buffer the stack usage information of the first 100 tasks to terminate and
20156 output this info at program termination. Results are displayed in four
20157 columns:
20158
20159 @noindent
20160 Index | Task Name | Stack Size | Actual Use [min - max]
20161
20162 @noindent
20163 where:
20164
20165 @table @emph
20166 @item Index
20167 is a number associated with each task.
20168
20169 @item Task Name
20170 is the name of the task analyzed.
20171
20172 @item Stack Size
20173 is the maximum size for the stack.
20174
20175 @item Actual Use
20176 is the measure done by the stack analyzer. In order to prevent overflow,
20177 the stack is not entirely analyzed, and it's not possible to know exactly how
20178 much has actually been used. The real amount of stack used is between the min
20179 and max values.
20180
20181 @end table
20182
20183 @noindent
20184 The environment task stack, e.g., the stack that contains the main unit, is
20185 only processed when the environment variable GNAT_STACK_LIMIT is set.
20186
20187
20188 @c *********************************
20189 @c * GNATCHECK *
20190 @c *********************************
20191 @node Verifying Properties Using gnatcheck
20192 @chapter Verifying Properties Using @command{gnatcheck}
20193 @findex gnatcheck
20194 @cindex @command{gnatcheck}
20195
20196 @noindent
20197 The @command{gnatcheck} tool is an ASIS-based utility that checks properties
20198 of Ada source files according to a given set of semantic rules.
20199 @cindex ASIS
20200
20201 In order to check compliance with a given rule, @command{gnatcheck} has to
20202 semantically analyze the Ada sources.
20203 Therefore, checks can only be performed on
20204 legal Ada units. Moreover, when a unit depends semantically upon units located
20205 outside the current directory, the source search path has to be provided when
20206 calling @command{gnatcheck}, either through a specified project file or
20207 through @command{gnatcheck} switches as described below.
20208
20209 A number of rules are predefined in @command{gnatcheck} and are described
20210 later in this chapter.
20211 You can also add new rules, by modifying the @command{gnatcheck} code and
20212 rebuilding the tool. In order to add a simple rule making some local checks,
20213 a small amount of straightforward ASIS-based programming is usually needed.
20214
20215 Project support for @command{gnatcheck} is provided by the GNAT
20216 driver (see @ref{The GNAT Driver and Project Files}).
20217
20218 Invoking @command{gnatcheck} on the command line has the form:
20219
20220 @smallexample
20221 $ gnatcheck @ovar{switches} @{@var{filename}@}
20222 @r{[}^-files^/FILES^=@{@var{arg_list_filename}@}@r{]}
20223 @r{[}-cargs @var{gcc_switches}@r{]} @r{[}-rules @var{rule_options}@r{]}
20224 @end smallexample
20225
20226 @noindent
20227 where
20228 @itemize @bullet
20229 @item
20230 @var{switches} specify the general tool options
20231
20232 @item
20233 Each @var{filename} is the name (including the extension) of a source
20234 file to process. ``Wildcards'' are allowed, and
20235 the file name may contain path information.
20236
20237 @item
20238 Each @var{arg_list_filename} is the name (including the extension) of a text
20239 file containing the names of the source files to process, separated by spaces
20240 or line breaks.
20241
20242 @item
20243 @var{gcc_switches} is a list of switches for
20244 @command{gcc}. They will be passed on to all compiler invocations made by
20245 @command{gnatcheck} to generate the ASIS trees. Here you can provide
20246 @option{^-I^/INCLUDE_DIRS=^} switches to form the source search path,
20247 and use the @option{-gnatec} switch to set the configuration file.
20248
20249 @item
20250 @var{rule_options} is a list of options for controlling a set of
20251 rules to be checked by @command{gnatcheck} (@pxref{gnatcheck Rule Options}).
20252 @end itemize
20253
20254 @noindent
20255 Either a @file{@var{filename}} or an @file{@var{arg_list_filename}} must be supplied.
20256
20257 @menu
20258 * Format of the Report File::
20259 * General gnatcheck Switches::
20260 * gnatcheck Rule Options::
20261 * Adding the Results of Compiler Checks to gnatcheck Output::
20262 * Project-Wide Checks::
20263 * Predefined Rules::
20264 @end menu
20265
20266 @node Format of the Report File
20267 @section Format of the Report File
20268 @cindex Report file (for @code{gnatcheck})
20269
20270 @noindent
20271 The @command{gnatcheck} tool outputs on @file{stdout} all messages concerning
20272 rule violations.
20273 It also creates, in the current
20274 directory, a text file named @file{^gnatcheck.out^GNATCHECK.OUT^} that
20275 contains the complete report of the last gnatcheck run. This report contains:
20276 @itemize @bullet
20277 @item a list of the Ada source files being checked,
20278 @item a list of enabled and disabled rules,
20279 @item a list of the diagnostic messages, ordered in three different ways
20280 and collected in three separate
20281 sections. Section 1 contains the raw list of diagnostic messages. It
20282 corresponds to the output going to @file{stdout}. Section 2 contains
20283 messages ordered by rules.
20284 Section 3 contains messages ordered by source files.
20285 @end itemize
20286
20287 @node General gnatcheck Switches
20288 @section General @command{gnatcheck} Switches
20289
20290 @noindent
20291 The following switches control the general @command{gnatcheck} behavior
20292
20293 @table @option
20294 @c !sort!
20295 @cindex @option{^-a^/ALL^} (@command{gnatcheck})
20296 @item ^-a^/ALL^
20297 Process all units including those with read-only ALI files such as
20298 those from GNAT Run-Time library.
20299
20300 @ifclear vms
20301 @ignore
20302 @cindex @option{-d} (@command{gnatcheck})
20303 @item -d
20304 Debug mode
20305 @end ignore
20306
20307 @cindex @option{-dd} (@command{gnatcheck})
20308 @item -dd
20309 Progress indicator mode (for use in GPS)
20310 @end ifclear
20311
20312 @cindex @option{^-h^/HELP^} (@command{gnatcheck})
20313 @item ^-h^/HELP^
20314 List the predefined and user-defined rules. For more details see
20315 @ref{Predefined Rules}.
20316
20317 @cindex @option{^-l^/LOCS^} (@command{gnatcheck})
20318 @item ^-l^/LOCS^
20319 Use full source locations references in the report file. For a construct from
20320 a generic instantiation a full source location is a chain from the location
20321 of this construct in the generic unit to the place where this unit is
20322 instantiated.
20323
20324 @cindex @option{^-q^/QUIET^} (@command{gnatcheck})
20325 @item ^-q^/QUIET^
20326 Quiet mode. All the diagnoses about rule violations are placed in the
20327 @command{gnatcheck} report file only, without duplicating in @file{stdout}.
20328
20329 @cindex @option{^-s^/SHORT^} (@command{gnatcheck})
20330 @item ^-s^/SHORT^
20331 Short format of the report file (no version information, no list of applied
20332 rules, no list of checked sources is included)
20333
20334 @cindex @option{^-s1^/COMPILER_STYLE^} (@command{gnatcheck})
20335 @item ^-s1^/COMPILER_STYLE^
20336 Include the compiler-style section in the report file
20337
20338 @cindex @option{^-s2^/BY_RULES^} (@command{gnatcheck})
20339 @item ^-s2^/BY_RULES^
20340 Include the section containing diagnoses ordered by rules in the report file
20341
20342 @cindex @option{^-s3^/BY_FILES_BY_RULES^} (@command{gnatcheck})
20343 @item ^-s3^/BY_FILES_BY_RULES^
20344 Include the section containing diagnoses ordered by files and then by rules
20345 in the report file
20346
20347 @cindex @option{^-v^/VERBOSE^} (@command{gnatcheck})
20348 @item ^-v^/VERBOSE^
20349 Verbose mode; @command{gnatcheck} generates version information and then
20350 a trace of sources being processed.
20351
20352 @end table
20353
20354 @noindent
20355 Note that if any of the options @option{^-s1^/COMPILER_STYLE^},
20356 @option{^-s2^/BY_RULES^} or
20357 @option{^-s3^/BY_FILES_BY_RULES^} is specified,
20358 then the @command{gnatcheck} report file will only contain sections
20359 explicitly denoted by these options.
20360
20361 @node gnatcheck Rule Options
20362 @section @command{gnatcheck} Rule Options
20363
20364 @noindent
20365 The following options control the processing performed by
20366 @command{gnatcheck}.
20367
20368 @table @option
20369 @cindex @option{+ALL} (@command{gnatcheck})
20370 @item +ALL
20371 Turn all the rule checks ON.
20372
20373 @cindex @option{-ALL} (@command{gnatcheck})
20374 @item -ALL
20375 Turn all the rule checks OFF.
20376
20377 @cindex @option{+R} (@command{gnatcheck})
20378 @item +R@var{rule_id}@r{[}:@var{param}@r{]}
20379 Turn on the check for a specified rule with the specified parameter, if any.
20380 @var{rule_id} must be the identifier of one of the currently implemented rules
20381 (use @option{^-h^/HELP^} for the list of implemented rules). Rule identifiers
20382 are not case-sensitive. The @var{param} item must
20383 be a string representing a valid parameter(s) for the specified rule.
20384 If it contains any space characters then this string must be enclosed in
20385 quotation marks.
20386
20387 @cindex @option{-R} (@command{gnatcheck})
20388 @item -R@var{rule_id}@r{[}:@var{param}@r{]}
20389 Turn off the check for a specified rule with the specified parameter, if any.
20390
20391 @cindex @option{-from} (@command{gnatcheck})
20392 @item -from=@var{rule_option_filename}
20393 Read the rule options from the text file @var{rule_option_filename}, referred as
20394 ``rule file'' below.
20395
20396 @end table
20397
20398 @noindent
20399 The default behavior is that all the rule checks are enabled, except for
20400 the checks performed by the compiler.
20401 @ignore
20402 and the checks associated with the
20403 global rules.
20404 @end ignore
20405
20406 A rule file is a text file containing a set of rule options.
20407 @cindex Rule file (for @code{gnatcheck})
20408 The file may contain empty lines and Ada-style comments (comment
20409 lines and end-of-line comments). The rule file has free format; that is,
20410 you do not have to start a new rule option on a new line.
20411
20412 A rule file may contain other @option{-from=@var{rule_option_filename}}
20413 options, each such option being replaced with the content of the
20414 corresponding rule file during the rule files processing. In case a
20415 cycle is detected (that is, @file{@var{rule_file_1}} reads rule options
20416 from @file{@var{rule_file_2}}, and @file{@var{rule_file_2}} reads
20417 (directly or indirectly) rule options from @file{@var{rule_file_1}}),
20418 the processing of rule files is interrupted and a part of their content
20419 is ignored.
20420
20421
20422 @node Adding the Results of Compiler Checks to gnatcheck Output
20423 @section Adding the Results of Compiler Checks to @command{gnatcheck} Output
20424
20425 @noindent
20426 The @command{gnatcheck} tool can include in the generated diagnostic messages
20427 and in
20428 the report file the results of the checks performed by the compiler. Though
20429 disabled by default, this effect may be obtained by using @option{+R} with
20430 the following rule identifiers and parameters:
20431
20432 @table @option
20433 @item Restrictions
20434 To record restrictions violations (that are performed by the compiler if the
20435 pragma @code{Restrictions} or @code{Restriction_Warnings} are given),
20436 use the rule named
20437 @code{Restrictions} with the same parameters as pragma
20438 @code{Restrictions} or @code{Restriction_Warnings}.
20439
20440 @item Style_Checks
20441 To record compiler style checks(@pxref{Style Checking}), use the rule named
20442 @code{Style_Checks}. A parameter of this rule can be either @code{All_Checks},
20443 which enables all the standard style checks that corresponds to @option{-gnatyy}
20444 GNAT style check option, or a string that has exactly the same
20445 structure and semantics as the @code{string_LITERAL} parameter of GNAT pragma
20446 @code{Style_Checks} (for further information about this pragma,
20447 @pxref{Pragma Style_Checks,,, gnat_rm, GNAT Reference Manual}).
20448
20449 @item Warnings
20450 To record compiler warnings (@pxref{Warning Message Control}), use the rule
20451 named @code{Warnings} with a parameter that is a valid
20452 @i{static_string_expression} argument of GNAT pragma @code{Warnings}
20453 (for further information about this pragma, @pxref{Pragma Warnings,,,
20454 gnat_rm, GNAT Reference Manual}). Note, that in case of gnatcheck
20455 's' parameter, that corresponds to the GNAT @option{-gnatws} option, disables
20456 all the specific warnings, but not suppresses the warning mode,
20457 and 'e' parameter, corresponding to @option{-gnatwe} that means
20458 "treat warnings as errors", does not have any effect.
20459
20460 @end table
20461
20462 To disable a specific restriction check, use @code{-RStyle_Checks} gnatcheck
20463 option with the corresponding restriction name as a parameter. @code{-R} is
20464 not available for @code{Style_Checks} and @code{Warnings} options, to disable
20465 warnings and style checks, use the corresponding warning and style options.
20466
20467 @node Project-Wide Checks
20468 @section Project-Wide Checks
20469 @cindex Project-wide checks (for @command{gnatcheck})
20470
20471 @noindent
20472 In order to perform checks on all units of a given project, you can use
20473 the GNAT driver along with the @option{-P} option:
20474 @smallexample
20475 gnat check -Pproj -rules -from=my_rules
20476 @end smallexample
20477
20478 @noindent
20479 If the project @code{proj} depends upon other projects, you can perform
20480 checks on the project closure using the @option{-U} option:
20481 @smallexample
20482 gnat check -Pproj -U -rules -from=my_rules
20483 @end smallexample
20484
20485 @noindent
20486 Finally, if not all the units are relevant to a particular main
20487 program in the project closure, you can perform checks for the set
20488 of units needed to create a given main program (unit closure) using
20489 the @option{-U} option followed by the name of the main unit:
20490 @smallexample
20491 gnat check -Pproj -U main -rules -from=my_rules
20492 @end smallexample
20493
20494
20495 @node Predefined Rules
20496 @section Predefined Rules
20497 @cindex Predefined rules (for @command{gnatcheck})
20498
20499 @ignore
20500 @c (Jan 2007) Since the global rules are still under development and are not
20501 @c documented, there is no point in explaining the difference between
20502 @c global and local rules
20503 @noindent
20504 A rule in @command{gnatcheck} is either local or global.
20505 A @emph{local rule} is a rule that applies to a well-defined section
20506 of a program and that can be checked by analyzing only this section.
20507 A @emph{global rule} requires analysis of some global properties of the
20508 whole program (mostly related to the program call graph).
20509 As of @value{NOW}, the implementation of global rules should be
20510 considered to be at a preliminary stage. You can use the
20511 @option{+GLOBAL} option to enable all the global rules, and the
20512 @option{-GLOBAL} rule option to disable all the global rules.
20513
20514 All the global rules in the list below are
20515 so indicated by marking them ``GLOBAL''.
20516 This +GLOBAL and -GLOBAL options are not
20517 included in the list of gnatcheck options above, because at the moment they
20518 are considered as a temporary debug options.
20519
20520 @command{gnatcheck} performs rule checks for generic
20521 instances only for global rules. This limitation may be relaxed in a later
20522 release.
20523 @end ignore
20524
20525 @noindent
20526 The following subsections document the rules implemented in
20527 @command{gnatcheck}.
20528 The subsection title is the same as the rule identifier, which may be
20529 used as a parameter of the @option{+R} or @option{-R} options.
20530
20531
20532 @menu
20533 * Abstract_Type_Declarations::
20534 * Anonymous_Arrays::
20535 * Anonymous_Subtypes::
20536 * Blocks::
20537 * Boolean_Relational_Operators::
20538 @ignore
20539 * Ceiling_Violations::
20540 @end ignore
20541 * Controlled_Type_Declarations::
20542 * Declarations_In_Blocks::
20543 * Default_Parameters::
20544 * Discriminated_Records::
20545 * Enumeration_Ranges_In_CASE_Statements::
20546 * Exceptions_As_Control_Flow::
20547 * EXIT_Statements_With_No_Loop_Name::
20548 * Expanded_Loop_Exit_Names::
20549 * Explicit_Full_Discrete_Ranges::
20550 * Float_Equality_Checks::
20551 * Forbidden_Pragmas::
20552 * Function_Style_Procedures::
20553 * Generics_In_Subprograms::
20554 * GOTO_Statements::
20555 * Implicit_IN_Mode_Parameters::
20556 * Implicit_SMALL_For_Fixed_Point_Types::
20557 * Improperly_Located_Instantiations::
20558 * Improper_Returns::
20559 * Library_Level_Subprograms::
20560 * Local_Packages::
20561 @ignore
20562 * Improperly_Called_Protected_Entries::
20563 @end ignore
20564 * Metrics::
20565 * Misnamed_Identifiers::
20566 * Multiple_Entries_In_Protected_Definitions::
20567 * Name_Clashes::
20568 * Non_Qualified_Aggregates::
20569 * Non_Short_Circuit_Operators::
20570 * Non_SPARK_Attributes::
20571 * Non_Tagged_Derived_Types::
20572 * Non_Visible_Exceptions::
20573 * Numeric_Literals::
20574 * OTHERS_In_Aggregates::
20575 * OTHERS_In_CASE_Statements::
20576 * OTHERS_In_Exception_Handlers::
20577 * Outer_Loop_Exits::
20578 * Overloaded_Operators::
20579 * Overly_Nested_Control_Structures::
20580 * Parameters_Out_Of_Order::
20581 * Positional_Actuals_For_Defaulted_Generic_Parameters::
20582 * Positional_Actuals_For_Defaulted_Parameters::
20583 * Positional_Components::
20584 * Positional_Generic_Parameters::
20585 * Positional_Parameters::
20586 * Predefined_Numeric_Types::
20587 * Raising_External_Exceptions::
20588 * Raising_Predefined_Exceptions::
20589 * Separate_Numeric_Error_Handlers::
20590 @ignore
20591 * Recursion::
20592 * Side_Effect_Functions::
20593 @end ignore
20594 * Slices::
20595 * Unassigned_OUT_Parameters::
20596 * Uncommented_BEGIN_In_Package_Bodies::
20597 * Unconstrained_Array_Returns::
20598 * Universal_Ranges::
20599 * Unnamed_Blocks_And_Loops::
20600 @ignore
20601 * Unused_Subprograms::
20602 @end ignore
20603 * USE_PACKAGE_Clauses::
20604 * Volatile_Objects_Without_Address_Clauses::
20605 @end menu
20606
20607
20608 @node Abstract_Type_Declarations
20609 @subsection @code{Abstract_Type_Declarations}
20610 @cindex @code{Abstract_Type_Declarations} rule (for @command{gnatcheck})
20611
20612 @noindent
20613 Flag all declarations of abstract types. For an abstract private
20614 type, both the private and full type declarations are flagged.
20615
20616 This rule has no parameters.
20617
20618
20619 @node Anonymous_Arrays
20620 @subsection @code{Anonymous_Arrays}
20621 @cindex @code{Anonymous_Arrays} rule (for @command{gnatcheck})
20622
20623 @noindent
20624 Flag all anonymous array type definitions (by Ada semantics these can only
20625 occur in object declarations).
20626
20627 This rule has no parameters.
20628
20629 @node Anonymous_Subtypes
20630 @subsection @code{Anonymous_Subtypes}
20631 @cindex @code{Anonymous_Subtypes} rule (for @command{gnatcheck})
20632
20633 @noindent
20634 Flag all uses of anonymous subtypes. A use of an anonymous subtype is
20635 any instance of a subtype indication with a constraint, other than one
20636 that occurs immediately within a subtype declaration. Any use of a range
20637 other than as a constraint used immediately within a subtype declaration
20638 is considered as an anonymous subtype.
20639
20640 An effect of this rule is that @code{for} loops such as the following are
20641 flagged (since @code{1..N} is formally a ``range''):
20642
20643 @smallexample @c ada
20644 for I in 1 .. N loop
20645 @dots{}
20646 end loop;
20647 @end smallexample
20648
20649 @noindent
20650 Declaring an explicit subtype solves the problem:
20651
20652 @smallexample @c ada
20653 subtype S is Integer range 1..N;
20654 @dots{}
20655 for I in S loop
20656 @dots{}
20657 end loop;
20658 @end smallexample
20659
20660 @noindent
20661 This rule has no parameters.
20662
20663 @node Blocks
20664 @subsection @code{Blocks}
20665 @cindex @code{Blocks} rule (for @command{gnatcheck})
20666
20667 @noindent
20668 Flag each block statement.
20669
20670 This rule has no parameters.
20671
20672 @node Boolean_Relational_Operators
20673 @subsection @code{Boolean_Relational_Operators}
20674 @cindex @code{Boolean_Relational_Operators} rule (for @command{gnatcheck})
20675
20676 @noindent
20677 Flag each call to a predefined relational operator (``<'', ``>'', ``<='',
20678 ``>='', ``='' and ``/='') for the predefined Boolean type.
20679 (This rule is useful in enforcing the SPARK language restrictions.)
20680
20681 Calls to predefined relational operators of any type derived from
20682 @code{Standard.Boolean} are not detected. Calls to user-defined functions
20683 with these designators, and uses of operators that are renamings
20684 of the predefined relational operators for @code{Standard.Boolean},
20685 are likewise not detected.
20686
20687 This rule has no parameters.
20688
20689 @ignore
20690 @node Ceiling_Violations
20691 @subsection @code{Ceiling_Violations} (under construction, GLOBAL)
20692 @cindex @code{Ceiling_Violations} rule (for @command{gnatcheck})
20693
20694 @noindent
20695 Flag invocations of a protected operation by a task whose priority exceeds
20696 the protected object's ceiling.
20697
20698 As of @value{NOW}, this rule has the following limitations:
20699
20700 @itemize @bullet
20701
20702 @item
20703 We consider only pragmas Priority and Interrupt_Priority as means to define
20704 a task/protected operation priority. We do not consider the effect of using
20705 Ada.Dynamic_Priorities.Set_Priority procedure;
20706
20707 @item
20708 We consider only base task priorities, and no priority inheritance. That is,
20709 we do not make a difference between calls issued during task activation and
20710 execution of the sequence of statements from task body;
20711
20712 @item
20713 Any situation when the priority of protected operation caller is set by a
20714 dynamic expression (that is, the corresponding Priority or
20715 Interrupt_Priority pragma has a non-static expression as an argument) we
20716 treat as a priority inconsistency (and, therefore, detect this situation).
20717 @end itemize
20718
20719 @noindent
20720 At the moment the notion of the main subprogram is not implemented in
20721 gnatcheck, so any pragma Priority in a library level subprogram body (in case
20722 if this subprogram can be a main subprogram of a partition) changes the
20723 priority of an environment task. So if we have more then one such pragma in
20724 the set of processed sources, the pragma that is processed last, defines the
20725 priority of an environment task.
20726
20727 This rule has no parameters.
20728 @end ignore
20729
20730 @node Controlled_Type_Declarations
20731 @subsection @code{Controlled_Type_Declarations}
20732 @cindex @code{Controlled_Type_Declarations} rule (for @command{gnatcheck})
20733
20734 @noindent
20735 Flag all declarations of controlled types. A declaration of a private type
20736 is flagged if its full declaration declares a controlled type. A declaration
20737 of a derived type is flagged if its ancestor type is controlled. Subtype
20738 declarations are not checked. A declaration of a type that itself is not a
20739 descendant of a type declared in @code{Ada.Finalization} but has a controlled
20740 component is not checked.
20741
20742 This rule has no parameters.
20743
20744
20745
20746 @node Declarations_In_Blocks
20747 @subsection @code{Declarations_In_Blocks}
20748 @cindex @code{Declarations_In_Blocks} rule (for @command{gnatcheck})
20749
20750 @noindent
20751 Flag all block statements containing local declarations. A @code{declare}
20752 block with an empty @i{declarative_part} or with a @i{declarative part}
20753 containing only pragmas and/or @code{use} clauses is not flagged.
20754
20755 This rule has no parameters.
20756
20757
20758 @node Default_Parameters
20759 @subsection @code{Default_Parameters}
20760 @cindex @code{Default_Parameters} rule (for @command{gnatcheck})
20761
20762 @noindent
20763 Flag all default expressions for subprogram parameters. Parameter
20764 declarations of formal and generic subprograms are also checked.
20765
20766 This rule has no parameters.
20767
20768
20769 @node Discriminated_Records
20770 @subsection @code{Discriminated_Records}
20771 @cindex @code{Discriminated_Records} rule (for @command{gnatcheck})
20772
20773 @noindent
20774 Flag all declarations of record types with discriminants. Only the
20775 declarations of record and record extension types are checked. Incomplete,
20776 formal, private, derived and private extension type declarations are not
20777 checked. Task and protected type declarations also are not checked.
20778
20779 This rule has no parameters.
20780
20781
20782 @node Enumeration_Ranges_In_CASE_Statements
20783 @subsection @code{Enumeration_Ranges_In_CASE_Statements}
20784 @cindex @code{Enumeration_Ranges_In_CASE_Statements} (for @command{gnatcheck})
20785
20786 @noindent
20787 Flag each use of a range of enumeration literals as a choice in a
20788 @code{case} statement.
20789 All forms for specifying a range (explicit ranges
20790 such as @code{A .. B}, subtype marks and @code{'Range} attributes) are flagged.
20791 An enumeration range is
20792 flagged even if contains exactly one enumeration value or no values at all. A
20793 type derived from an enumeration type is considered as an enumeration type.
20794
20795 This rule helps prevent maintenance problems arising from adding an
20796 enumeration value to a type and having it implicitly handled by an existing
20797 @code{case} statement with an enumeration range that includes the new literal.
20798
20799 This rule has no parameters.
20800
20801
20802 @node Exceptions_As_Control_Flow
20803 @subsection @code{Exceptions_As_Control_Flow}
20804 @cindex @code{Exceptions_As_Control_Flow} (for @command{gnatcheck})
20805
20806 @noindent
20807 Flag each place where an exception is explicitly raised and handled in the
20808 same subprogram body. A @code{raise} statement in an exception handler,
20809 package body, task body or entry body is not flagged.
20810
20811 The rule has no parameters.
20812
20813 @node EXIT_Statements_With_No_Loop_Name
20814 @subsection @code{EXIT_Statements_With_No_Loop_Name}
20815 @cindex @code{EXIT_Statements_With_No_Loop_Name} (for @command{gnatcheck})
20816
20817 @noindent
20818 Flag each @code{exit} statement that does not specify the name of the loop
20819 being exited.
20820
20821 The rule has no parameters.
20822
20823
20824 @node Expanded_Loop_Exit_Names
20825 @subsection @code{Expanded_Loop_Exit_Names}
20826 @cindex @code{Expanded_Loop_Exit_Names} rule (for @command{gnatcheck})
20827
20828 @noindent
20829 Flag all expanded loop names in @code{exit} statements.
20830
20831 This rule has no parameters.
20832
20833 @node Explicit_Full_Discrete_Ranges
20834 @subsection @code{Explicit_Full_Discrete_Ranges}
20835 @cindex @code{Explicit_Full_Discrete_Ranges} rule (for @command{gnatcheck})
20836
20837 @noindent
20838 Flag each discrete range that has the form @code{A'First .. A'Last}.
20839
20840 This rule has no parameters.
20841
20842 @node Float_Equality_Checks
20843 @subsection @code{Float_Equality_Checks}
20844 @cindex @code{Float_Equality_Checks} rule (for @command{gnatcheck})
20845
20846 @noindent
20847 Flag all calls to the predefined equality operations for floating-point types.
20848 Both ``@code{=}'' and ``@code{/=}'' operations are checked.
20849 User-defined equality operations are not flagged, nor are ``@code{=}''
20850 and ``@code{/=}'' operations for fixed-point types.
20851
20852 This rule has no parameters.
20853
20854
20855 @node Forbidden_Pragmas
20856 @subsection @code{Forbidden_Pragmas}
20857 @cindex @code{Forbidden_Pragmas} rule (for @command{gnatcheck})
20858
20859 @noindent
20860 Flag each use of the specified pragmas. The pragmas to be detected
20861 are named in the rule's parameters.
20862
20863 This rule has the following parameters:
20864
20865 @itemize @bullet
20866 @item For the @option{+R} option
20867
20868 @table @asis
20869 @item @emph{Pragma_Name}
20870 Adds the specified pragma to the set of pragmas to be
20871 checked and sets the checks for all the specified pragmas
20872 ON. @emph{Pragma_Name} is treated as a name of a pragma. If it
20873 does not correspond to any pragma name defined in the Ada
20874 standard or to the name of a GNAT-specific pragma defined
20875 in @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference
20876 Manual}, it is treated as the name of unknown pragma.
20877
20878 @item @code{GNAT}
20879 All the GNAT-specific pragmas are detected; this sets
20880 the checks for all the specified pragmas ON.
20881
20882 @item @code{ALL}
20883 All pragmas are detected; this sets the rule ON.
20884 @end table
20885
20886 @item For the @option{-R} option
20887 @table @asis
20888 @item @emph{Pragma_Name}
20889 Removes the specified pragma from the set of pragmas to be
20890 checked without affecting checks for
20891 other pragmas. @emph{Pragma_Name} is treated as a name
20892 of a pragma. If it does not correspond to any pragma
20893 defined in the Ada standard or to any name defined in
20894 @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference Manual},
20895 this option is treated as turning OFF detection of all unknown pragmas.
20896
20897 @item GNAT
20898 Turn OFF detection of all GNAT-specific pragmas
20899
20900 @item ALL
20901 Clear the list of the pragmas to be detected and
20902 turn the rule OFF.
20903 @end table
20904 @end itemize
20905
20906 @noindent
20907 Parameters are not case sensitive. If @emph{Pragma_Name} does not have
20908 the syntax of an Ada identifier and therefore can not be considered
20909 as a pragma name, a diagnostic message is generated and the corresponding
20910 parameter is ignored.
20911
20912 When more then one parameter is given in the same rule option, the parameters
20913 must be separated by a comma.
20914
20915 If more then one option for this rule is specified for the @command{gnatcheck}
20916 call, a new option overrides the previous one(s).
20917
20918 The @option{+R} option with no parameters turns the rule ON with the set of
20919 pragmas to be detected defined by the previous rule options.
20920 (By default this set is empty, so if the only option specified for the rule is
20921 @option{+RForbidden_Pragmas} (with
20922 no parameter), then the rule is enabled, but it does not detect anything).
20923 The @option{-R} option with no parameter turns the rule OFF, but it does not
20924 affect the set of pragmas to be detected.
20925
20926
20927
20928
20929 @node Function_Style_Procedures
20930 @subsection @code{Function_Style_Procedures}
20931 @cindex @code{Function_Style_Procedures} rule (for @command{gnatcheck})
20932
20933 @noindent
20934 Flag each procedure that can be rewritten as a function. A procedure can be
20935 converted into a function if it has exactly one parameter of mode @code{out}
20936 and no parameters of mode @code{in out}. Procedure declarations,
20937 formal procedure declarations, and generic procedure declarations are always
20938 checked. Procedure
20939 bodies and body stubs are flagged only if they do not have corresponding
20940 separate declarations. Procedure renamings and procedure instantiations are
20941 not flagged.
20942
20943 If a procedure can be rewritten as a function, but its @code{out} parameter is
20944 of a limited type, it is not flagged.
20945
20946 Protected procedures are not flagged. Null procedures also are not flagged.
20947
20948 This rule has no parameters.
20949
20950
20951 @node Generics_In_Subprograms
20952 @subsection @code{Generics_In_Subprograms}
20953 @cindex @code{Generics_In_Subprograms} rule (for @command{gnatcheck})
20954
20955 @noindent
20956 Flag each declaration of a generic unit in a subprogram. Generic
20957 declarations in the bodies of generic subprograms are also flagged.
20958 A generic unit nested in another generic unit is not flagged.
20959 If a generic unit is
20960 declared in a local package that is declared in a subprogram body, the
20961 generic unit is flagged.
20962
20963 This rule has no parameters.
20964
20965
20966 @node GOTO_Statements
20967 @subsection @code{GOTO_Statements}
20968 @cindex @code{GOTO_Statements} rule (for @command{gnatcheck})
20969
20970 @noindent
20971 Flag each occurrence of a @code{goto} statement.
20972
20973 This rule has no parameters.
20974
20975
20976 @node Implicit_IN_Mode_Parameters
20977 @subsection @code{Implicit_IN_Mode_Parameters}
20978 @cindex @code{Implicit_IN_Mode_Parameters} rule (for @command{gnatcheck})
20979
20980 @noindent
20981 Flag each occurrence of a formal parameter with an implicit @code{in} mode.
20982 Note that @code{access} parameters, although they technically behave
20983 like @code{in} parameters, are not flagged.
20984
20985 This rule has no parameters.
20986
20987
20988 @node Implicit_SMALL_For_Fixed_Point_Types
20989 @subsection @code{Implicit_SMALL_For_Fixed_Point_Types}
20990 @cindex @code{Implicit_SMALL_For_Fixed_Point_Types} rule (for @command{gnatcheck})
20991
20992 @noindent
20993 Flag each fixed point type declaration that lacks an explicit
20994 representation clause to define its @code{'Small} value.
20995 Since @code{'Small} can be defined only for ordinary fixed point types,
20996 decimal fixed point type declarations are not checked.
20997
20998 This rule has no parameters.
20999
21000
21001 @node Improperly_Located_Instantiations
21002 @subsection @code{Improperly_Located_Instantiations}
21003 @cindex @code{Improperly_Located_Instantiations} rule (for @command{gnatcheck})
21004
21005 @noindent
21006 Flag all generic instantiations in library-level package specs
21007 (including library generic packages) and in all subprogram bodies.
21008
21009 Instantiations in task and entry bodies are not flagged. Instantiations in the
21010 bodies of protected subprograms are flagged.
21011
21012 This rule has no parameters.
21013
21014
21015
21016 @node Improper_Returns
21017 @subsection @code{Improper_Returns}
21018 @cindex @code{Improper_Returns} rule (for @command{gnatcheck})
21019
21020 @noindent
21021 Flag each explicit @code{return} statement in procedures, and
21022 multiple @code{return} statements in functions.
21023 Diagnostic messages are generated for all @code{return} statements
21024 in a procedure (thus each procedure must be written so that it
21025 returns implicitly at the end of its statement part),
21026 and for all @code{return} statements in a function after the first one.
21027 This rule supports the stylistic convention that each subprogram
21028 should have no more than one point of normal return.
21029
21030 This rule has no parameters.
21031
21032
21033 @node Library_Level_Subprograms
21034 @subsection @code{Library_Level_Subprograms}
21035 @cindex @code{Library_Level_Subprograms} rule (for @command{gnatcheck})
21036
21037 @noindent
21038 Flag all library-level subprograms (including generic subprogram instantiations).
21039
21040 This rule has no parameters.
21041
21042
21043 @node Local_Packages
21044 @subsection @code{Local_Packages}
21045 @cindex @code{Local_Packages} rule (for @command{gnatcheck})
21046
21047 @noindent
21048 Flag all local packages declared in package and generic package
21049 specs.
21050 Local packages in bodies are not flagged.
21051
21052 This rule has no parameters.
21053
21054 @ignore
21055 @node Improperly_Called_Protected_Entries
21056 @subsection @code{Improperly_Called_Protected_Entries} (under construction, GLOBAL)
21057 @cindex @code{Improperly_Called_Protected_Entries} rule (for @command{gnatcheck})
21058
21059 @noindent
21060 Flag each protected entry that can be called from more than one task.
21061
21062 This rule has no parameters.
21063 @end ignore
21064
21065 @node Metrics
21066 @subsection @code{Metrics}
21067 @cindex @code{Metrics} rule (for @command{gnatcheck})
21068
21069 @noindent
21070 There is a set of checks based on computing a metric value and comparing the
21071 result with the specified upper (or lower, depending on a specific metric)
21072 value specified for a given metric. A construct is flagged if a given metric
21073 is applicable (can be computed) for it and the computed value is greater
21074 then (lover then) the specified upper (lower) bound.
21075
21076 The name of any metric-based rule consists of the prefix @code{Metrics_}
21077 followed by the name of the corresponding metric (see the table below).
21078 For @option{+R} option, each metric-based rule has a numeric parameter
21079 specifying the bound (integer or real, depending on a metric), @option{-R}
21080 option for metric rules does not have a parameter.
21081
21082 The following table shows the metric names for that the corresponding
21083 metrics-based checks are supported by gnatcheck, including the
21084 constraint that must be satisfied by the bound that is specified for the check
21085 and what bound - upper (U) or lower (L) - should be specified.
21086
21087 @multitable {@code{Cyclomatic_Complexity}}{Cyclomatic complexity}{Positive integer}
21088 @ifnothtml
21089 @headitem Check Name @tab Description @tab Bounds Value
21090 @end ifnothtml
21091 @ifhtml
21092 @item @b{Check Name} @tab @b{Description} @tab @b{Bounds Value}
21093 @end ifhtml
21094 @c Above conditional code is workaround to bug in texi2html (Feb 2008)
21095 @item @code{Essential_Complexity} @tab Essential complexity @tab Positive integer (U)
21096 @item @code{Cyclomatic_Complexity} @tab Cyclomatic complexity @tab Positive integer (U)
21097 @item @code{LSLOC} @tab Logical Source Lines of Code @tab Positive integer (U)
21098 @end multitable
21099
21100 @noindent
21101 The meaning and the computed values for all these metrics are exactly
21102 the same as for the corresponding metrics in @command{gnatmetric}.
21103
21104 @emph{Example:} the rule
21105 @smallexample
21106 +RMetrics_Cyclomatic_Complexity : 7
21107 @end smallexample
21108 @noindent
21109 means that all bodies with cyclomatic complexity exceeding 7 will be flagged.
21110
21111 To turn OFF the check for cyclomatic complexity metric, use the following option:
21112 @smallexample
21113 -RMetrics_Cyclomatic_Complexity
21114 @end smallexample
21115
21116 @node Misnamed_Identifiers
21117 @subsection @code{Misnamed_Identifiers}
21118 @cindex @code{Misnamed_Identifiers} rule (for @command{gnatcheck})
21119
21120 @noindent
21121 Flag the declaration of each identifier that does not have a suffix
21122 corresponding to the kind of entity being declared.
21123 The following declarations are checked:
21124
21125 @itemize @bullet
21126 @item
21127 type declarations
21128
21129 @item
21130 constant declarations (but not number declarations)
21131
21132 @item
21133 package renaming declarations (but not generic package renaming
21134 declarations)
21135 @end itemize
21136
21137 @noindent
21138 This rule may have parameters. When used without parameters, the rule enforces
21139 the following checks:
21140
21141 @itemize @bullet
21142 @item
21143 type-defining names end with @code{_T}, unless the type is an access type,
21144 in which case the suffix must be @code{_A}
21145 @item
21146 constant names end with @code{_C}
21147 @item
21148 names defining package renamings end with @code{_R}
21149 @end itemize
21150
21151 @noindent
21152 For a private or incomplete type declaration the following checks are
21153 made for the defining name suffix:
21154
21155 @itemize @bullet
21156 @item
21157 For an incomplete type declaration: if the corresponding full type
21158 declaration is available, the defining identifier from the full type
21159 declaration is checked, but the defining identifier from the incomplete type
21160 declaration is not; otherwise the defining identifier from the incomplete
21161 type declaration is checked against the suffix specified for type
21162 declarations.
21163
21164 @item
21165 For a private type declaration (including private extensions), the defining
21166 identifier from the private type declaration is checked against the type
21167 suffix (even if the corresponding full declaration is an access type
21168 declaration), and the defining identifier from the corresponding full type
21169 declaration is not checked.
21170 @end itemize
21171
21172 @noindent
21173 For a deferred constant, the defining name in the corresponding full constant
21174 declaration is not checked.
21175
21176 Defining names of formal types are not checked.
21177
21178 The rule may have the following parameters:
21179
21180 @itemize @bullet
21181 @item
21182 For the @option{+R} option:
21183 @table @code
21184 @item Default
21185 Sets the default listed above for all the names to be checked.
21186
21187 @item Type_Suffix=@emph{string}
21188 Specifies the suffix for a type name.
21189
21190 @item Access_Suffix=@emph{string}
21191 Specifies the suffix for an access type name. If
21192 this parameter is set, it overrides for access
21193 types the suffix set by the @code{Type_Suffix} parameter.
21194
21195 @item Constant_Suffix=@emph{string}
21196 Specifies the suffix for a constant name.
21197
21198 @item Renaming_Suffix=@emph{string}
21199 Specifies the suffix for a package renaming name.
21200 @end table
21201
21202 @item
21203 For the @option{-R} option:
21204 @table @code
21205 @item All_Suffixes
21206 Remove all the suffixes specified for the
21207 identifier suffix checks, whether by default or
21208 as specified by other rule parameters. All the
21209 checks for this rule are disabled as a result.
21210
21211 @item Type_Suffix
21212 Removes the suffix specified for types. This
21213 disables checks for types but does not disable
21214 any other checks for this rule (including the
21215 check for access type names if @code{Access_Suffix} is
21216 set).
21217
21218 @item Access_Suffix
21219 Removes the suffix specified for access types.
21220 This disables checks for access type names but
21221 does not disable any other checks for this rule.
21222 If @code{Type_Suffix} is set, access type names are
21223 checked as ordinary type names.
21224
21225 @item Constant_Suffix
21226 Removes the suffix specified for constants. This
21227 disables checks for constant names but does not
21228 disable any other checks for this rule.
21229
21230 @item Renaming_Suffix
21231 Removes the suffix specified for package
21232 renamings. This disables checks for package
21233 renamings but does not disable any other checks
21234 for this rule.
21235 @end table
21236 @end itemize
21237
21238 @noindent
21239 If more than one parameter is used, parameters must be separated by commas.
21240
21241 If more than one option is specified for the @command{gnatcheck} invocation,
21242 a new option overrides the previous one(s).
21243
21244 The @option{+RMisnamed_Identifiers} option (with no parameter) enables
21245 checks for all the
21246 name suffixes specified by previous options used for this rule.
21247
21248 The @option{-RMisnamed_Identifiers} option (with no parameter) disables
21249 all the checks but keeps
21250 all the suffixes specified by previous options used for this rule.
21251
21252 The @emph{string} value must be a valid suffix for an Ada identifier (after
21253 trimming all the leading and trailing space characters, if any).
21254 Parameters are not case sensitive, except the @emph{string} part.
21255
21256 If any error is detected in a rule parameter, the parameter is ignored.
21257 In such a case the options that are set for the rule are not
21258 specified.
21259
21260
21261
21262 @node Multiple_Entries_In_Protected_Definitions
21263 @subsection @code{Multiple_Entries_In_Protected_Definitions}
21264 @cindex @code{Multiple_Entries_In_Protected_Definitions} rule (for @command{gnatcheck})
21265
21266 @noindent
21267 Flag each protected definition (i.e., each protected object/type declaration)
21268 that defines more than one entry.
21269 Diagnostic messages are generated for all the entry declarations
21270 except the first one. An entry family is counted as one entry. Entries from
21271 the private part of the protected definition are also checked.
21272
21273 This rule has no parameters.
21274
21275 @node Name_Clashes
21276 @subsection @code{Name_Clashes}
21277 @cindex @code{Name_Clashes} rule (for @command{gnatcheck})
21278
21279 @noindent
21280 Check that certain names are not used as defining identifiers. To activate
21281 this rule, you need to supply a reference to the dictionary file(s) as a rule
21282 parameter(s) (more then one dictionary file can be specified). If no
21283 dictionary file is set, this rule will not cause anything to be flagged.
21284 Only defining occurrences, not references, are checked.
21285 The check is not case-sensitive.
21286
21287 This rule is enabled by default, but without setting any corresponding
21288 dictionary file(s); thus the default effect is to do no checks.
21289
21290 A dictionary file is a plain text file. The maximum line length for this file
21291 is 1024 characters. If the line is longer then this limit, extra characters
21292 are ignored.
21293
21294 Each line can be either an empty line, a comment line, or a line containing
21295 a list of identifiers separated by space or HT characters.
21296 A comment is an Ada-style comment (from @code{--} to end-of-line).
21297 Identifiers must follow the Ada syntax for identifiers.
21298 A line containing one or more identifiers may end with a comment.
21299
21300 @node Non_Qualified_Aggregates
21301 @subsection @code{Non_Qualified_Aggregates}
21302 @cindex @code{Non_Qualified_Aggregates} rule (for @command{gnatcheck})
21303
21304 @noindent
21305 Flag each non-qualified aggregate.
21306 A non-qualified aggregate is an
21307 aggregate that is not the expression of a qualified expression. A
21308 string literal is not considered an aggregate, but an array
21309 aggregate of a string type is considered as a normal aggregate.
21310 Aggregates of anonymous array types are not flagged.
21311
21312 This rule has no parameters.
21313
21314
21315 @node Non_Short_Circuit_Operators
21316 @subsection @code{Non_Short_Circuit_Operators}
21317 @cindex @code{Non_Short_Circuit_Operators} rule (for @command{gnatcheck})
21318
21319 @noindent
21320 Flag all calls to predefined @code{and} and @code{or} operators for
21321 any boolean type. Calls to
21322 user-defined @code{and} and @code{or} and to operators defined by renaming
21323 declarations are not flagged. Calls to predefined @code{and} and @code{or}
21324 operators for modular types or boolean array types are not flagged.
21325
21326 This rule has no parameters.
21327
21328
21329
21330 @node Non_SPARK_Attributes
21331 @subsection @code{Non_SPARK_Attributes}
21332 @cindex @code{Non_SPARK_Attributes} rule (for @command{gnatcheck})
21333
21334 @noindent
21335 The SPARK language defines the following subset of Ada 95 attribute
21336 designators as those that can be used in SPARK programs. The use of
21337 any other attribute is flagged.
21338
21339 @itemize @bullet
21340 @item @code{'Adjacent}
21341 @item @code{'Aft}
21342 @item @code{'Base}
21343 @item @code{'Ceiling}
21344 @item @code{'Component_Size}
21345 @item @code{'Compose}
21346 @item @code{'Copy_Sign}
21347 @item @code{'Delta}
21348 @item @code{'Denorm}
21349 @item @code{'Digits}
21350 @item @code{'Exponent}
21351 @item @code{'First}
21352 @item @code{'Floor}
21353 @item @code{'Fore}
21354 @item @code{'Fraction}
21355 @item @code{'Last}
21356 @item @code{'Leading_Part}
21357 @item @code{'Length}
21358 @item @code{'Machine}
21359 @item @code{'Machine_Emax}
21360 @item @code{'Machine_Emin}
21361 @item @code{'Machine_Mantissa}
21362 @item @code{'Machine_Overflows}
21363 @item @code{'Machine_Radix}
21364 @item @code{'Machine_Rounds}
21365 @item @code{'Max}
21366 @item @code{'Min}
21367 @item @code{'Model}
21368 @item @code{'Model_Emin}
21369 @item @code{'Model_Epsilon}
21370 @item @code{'Model_Mantissa}
21371 @item @code{'Model_Small}
21372 @item @code{'Modulus}
21373 @item @code{'Pos}
21374 @item @code{'Pred}
21375 @item @code{'Range}
21376 @item @code{'Remainder}
21377 @item @code{'Rounding}
21378 @item @code{'Safe_First}
21379 @item @code{'Safe_Last}
21380 @item @code{'Scaling}
21381 @item @code{'Signed_Zeros}
21382 @item @code{'Size}
21383 @item @code{'Small}
21384 @item @code{'Succ}
21385 @item @code{'Truncation}
21386 @item @code{'Unbiased_Rounding}
21387 @item @code{'Val}
21388 @item @code{'Valid}
21389 @end itemize
21390
21391 @noindent
21392 This rule has no parameters.
21393
21394
21395 @node Non_Tagged_Derived_Types
21396 @subsection @code{Non_Tagged_Derived_Types}
21397 @cindex @code{Non_Tagged_Derived_Types} rule (for @command{gnatcheck})
21398
21399 @noindent
21400 Flag all derived type declarations that do not have a record extension part.
21401
21402 This rule has no parameters.
21403
21404
21405
21406 @node Non_Visible_Exceptions
21407 @subsection @code{Non_Visible_Exceptions}
21408 @cindex @code{Non_Visible_Exceptions} rule (for @command{gnatcheck})
21409
21410 @noindent
21411 Flag constructs leading to the possibility of propagating an exception
21412 out of the scope in which the exception is declared.
21413 Two cases are detected:
21414
21415 @itemize @bullet
21416 @item
21417 An exception declaration in a subprogram body, task body or block
21418 statement is flagged if the body or statement does not contain a handler for
21419 that exception or a handler with an @code{others} choice.
21420
21421 @item
21422 A @code{raise} statement in an exception handler of a subprogram body,
21423 task body or block statement is flagged if it (re)raises a locally
21424 declared exception. This may occur under the following circumstances:
21425 @itemize @minus
21426 @item
21427 it explicitly raises a locally declared exception, or
21428 @item
21429 it does not specify an exception name (i.e., it is simply @code{raise;})
21430 and the enclosing handler contains a locally declared exception in its
21431 exception choices.
21432 @end itemize
21433 @end itemize
21434
21435 @noindent
21436 Renamings of local exceptions are not flagged.
21437
21438 This rule has no parameters.
21439
21440
21441 @node Numeric_Literals
21442 @subsection @code{Numeric_Literals}
21443 @cindex @code{Numeric_Literals} rule (for @command{gnatcheck})
21444
21445 @noindent
21446 Flag each use of a numeric literal in an index expression, and in any
21447 circumstance except for the following:
21448
21449 @itemize @bullet
21450 @item
21451 a literal occurring in the initialization expression for a constant
21452 declaration or a named number declaration, or
21453
21454 @item
21455 an integer literal that is less than or equal to a value
21456 specified by the @option{N} rule parameter.
21457 @end itemize
21458
21459 @noindent
21460 This rule may have the following parameters for the @option{+R} option:
21461
21462 @table @asis
21463 @item @emph{N}
21464 @emph{N} is an integer literal used as the maximal value that is not flagged
21465 (i.e., integer literals not exceeding this value are allowed)
21466
21467 @item @code{ALL}
21468 All integer literals are flagged
21469 @end table
21470
21471 @noindent
21472 If no parameters are set, the maximum unflagged value is 1.
21473
21474 The last specified check limit (or the fact that there is no limit at
21475 all) is used when multiple @option{+R} options appear.
21476
21477 The @option{-R} option for this rule has no parameters.
21478 It disables the rule but retains the last specified maximum unflagged value.
21479 If the @option{+R} option subsequently appears, this value is used as the
21480 threshold for the check.
21481
21482
21483 @node OTHERS_In_Aggregates
21484 @subsection @code{OTHERS_In_Aggregates}
21485 @cindex @code{OTHERS_In_Aggregates} rule (for @command{gnatcheck})
21486
21487 @noindent
21488 Flag each use of an @code{others} choice in extension aggregates.
21489 In record and array aggregates, an @code{others} choice is flagged unless
21490 it is used to refer to all components, or to all but one component.
21491
21492 If, in case of a named array aggregate, there are two associations, one
21493 with an @code{others} choice and another with a discrete range, the
21494 @code{others} choice is flagged even if the discrete range specifies
21495 exactly one component; for example, @code{(1..1 => 0, others => 1)}.
21496
21497 This rule has no parameters.
21498
21499 @node OTHERS_In_CASE_Statements
21500 @subsection @code{OTHERS_In_CASE_Statements}
21501 @cindex @code{OTHERS_In_CASE_Statements} rule (for @command{gnatcheck})
21502
21503 @noindent
21504 Flag any use of an @code{others} choice in a @code{case} statement.
21505
21506 This rule has no parameters.
21507
21508 @node OTHERS_In_Exception_Handlers
21509 @subsection @code{OTHERS_In_Exception_Handlers}
21510 @cindex @code{OTHERS_In_Exception_Handlers} rule (for @command{gnatcheck})
21511
21512 @noindent
21513 Flag any use of an @code{others} choice in an exception handler.
21514
21515 This rule has no parameters.
21516
21517
21518 @node Outer_Loop_Exits
21519 @subsection @code{Outer_Loop_Exits}
21520 @cindex @code{Outer_Loop_Exits} rule (for @command{gnatcheck})
21521
21522 @noindent
21523 Flag each @code{exit} statement containing a loop name that is not the name
21524 of the immediately enclosing @code{loop} statement.
21525
21526 This rule has no parameters.
21527
21528
21529 @node Overloaded_Operators
21530 @subsection @code{Overloaded_Operators}
21531 @cindex @code{Overloaded_Operators} rule (for @command{gnatcheck})
21532
21533 @noindent
21534 Flag each function declaration that overloads an operator symbol.
21535 A function body is checked only if the body does not have a
21536 separate spec. Formal functions are also checked. For a
21537 renaming declaration, only renaming-as-declaration is checked
21538
21539 This rule has no parameters.
21540
21541
21542 @node Overly_Nested_Control_Structures
21543 @subsection @code{Overly_Nested_Control_Structures}
21544 @cindex @code{Overly_Nested_Control_Structures} rule (for @command{gnatcheck})
21545
21546 @noindent
21547 Flag each control structure whose nesting level exceeds the value provided
21548 in the rule parameter.
21549
21550 The control structures checked are the following:
21551
21552 @itemize @bullet
21553 @item @code{if} statement
21554 @item @code{case} statement
21555 @item @code{loop} statement
21556 @item Selective accept statement
21557 @item Timed entry call statement
21558 @item Conditional entry call
21559 @item Asynchronous select statement
21560 @end itemize
21561
21562 @noindent
21563 The rule has the following parameter for the @option{+R} option:
21564
21565 @table @emph
21566 @item N
21567 Positive integer specifying the maximal control structure nesting
21568 level that is not flagged
21569 @end table
21570
21571 @noindent
21572 If the parameter for the @option{+R} option is not specified or
21573 if it is not a positive integer, @option{+R} option is ignored.
21574
21575 If more then one option is specified for the gnatcheck call, the later option and
21576 new parameter override the previous one(s).
21577
21578
21579 @node Parameters_Out_Of_Order
21580 @subsection @code{Parameters_Out_Of_Order}
21581 @cindex @code{Parameters_Out_Of_Order} rule (for @command{gnatcheck})
21582
21583 @noindent
21584 Flag each subprogram and entry declaration whose formal parameters are not
21585 ordered according to the following scheme:
21586
21587 @itemize @bullet
21588
21589 @item @code{in} and @code{access} parameters first,
21590 then @code{in out} parameters,
21591 and then @code{out} parameters;
21592
21593 @item for @code{in} mode, parameters with default initialization expressions
21594 occur last
21595 @end itemize
21596
21597 @noindent
21598 Only the first violation of the described order is flagged.
21599
21600 The following constructs are checked:
21601
21602 @itemize @bullet
21603 @item subprogram declarations (including null procedures);
21604 @item generic subprogram declarations;
21605 @item formal subprogram declarations;
21606 @item entry declarations;
21607 @item subprogram bodies and subprogram body stubs that do not
21608 have separate specifications
21609 @end itemize
21610
21611 @noindent
21612 Subprogram renamings are not checked.
21613
21614 This rule has no parameters.
21615
21616
21617 @node Positional_Actuals_For_Defaulted_Generic_Parameters
21618 @subsection @code{Positional_Actuals_For_Defaulted_Generic_Parameters}
21619 @cindex @code{Positional_Actuals_For_Defaulted_Generic_Parameters} rule (for @command{gnatcheck})
21620
21621 @noindent
21622 Flag each generic actual parameter corresponding to a generic formal
21623 parameter with a default initialization, if positional notation is used.
21624
21625 This rule has no parameters.
21626
21627 @node Positional_Actuals_For_Defaulted_Parameters
21628 @subsection @code{Positional_Actuals_For_Defaulted_Parameters}
21629 @cindex @code{Positional_Actuals_For_Defaulted_Parameters} rule (for @command{gnatcheck})
21630
21631 @noindent
21632 Flag each actual parameter to a subprogram or entry call where the
21633 corresponding formal parameter has a default expression, if positional
21634 notation is used.
21635
21636 This rule has no parameters.
21637
21638 @node Positional_Components
21639 @subsection @code{Positional_Components}
21640 @cindex @code{Positional_Components} rule (for @command{gnatcheck})
21641
21642 @noindent
21643 Flag each array, record and extension aggregate that includes positional
21644 notation.
21645
21646 This rule has no parameters.
21647
21648
21649 @node Positional_Generic_Parameters
21650 @subsection @code{Positional_Generic_Parameters}
21651 @cindex @code{Positional_Generic_Parameters} rule (for @command{gnatcheck})
21652
21653 @noindent
21654 Flag each instantiation using positional parameter notation.
21655
21656 This rule has no parameters.
21657
21658
21659 @node Positional_Parameters
21660 @subsection @code{Positional_Parameters}
21661 @cindex @code{Positional_Parameters} rule (for @command{gnatcheck})
21662
21663 @noindent
21664 Flag each subprogram or entry call using positional parameter notation,
21665 except for the following:
21666
21667 @itemize @bullet
21668 @item
21669 Invocations of prefix or infix operators are not flagged
21670 @item
21671 If the called subprogram or entry has only one formal parameter,
21672 the call is not flagged;
21673 @item
21674 If a subprogram call uses the @emph{Object.Operation} notation, then
21675 @itemize @minus
21676 @item
21677 the first parameter (that is, @emph{Object}) is not flagged;
21678 @item
21679 if the called subprogram has only two parameters, the second parameter
21680 of the call is not flagged;
21681 @end itemize
21682 @end itemize
21683
21684 @noindent
21685 This rule has no parameters.
21686
21687
21688
21689
21690 @node Predefined_Numeric_Types
21691 @subsection @code{Predefined_Numeric_Types}
21692 @cindex @code{Predefined_Numeric_Types} rule (for @command{gnatcheck})
21693
21694 @noindent
21695 Flag each explicit use of the name of any numeric type or subtype defined
21696 in package @code{Standard}.
21697
21698 The rationale for this rule is to detect when the
21699 program may depend on platform-specific characteristics of the implementation
21700 of the predefined numeric types. Note that this rule is over-pessimistic;
21701 for example, a program that uses @code{String} indexing
21702 likely needs a variable of type @code{Integer}.
21703 Another example is the flagging of predefined numeric types with explicit
21704 constraints:
21705
21706 @smallexample @c ada
21707 subtype My_Integer is Integer range Left .. Right;
21708 Vy_Var : My_Integer;
21709 @end smallexample
21710
21711 @noindent
21712 This rule detects only numeric types and subtypes defined in
21713 @code{Standard}. The use of numeric types and subtypes defined in other
21714 predefined packages (such as @code{System.Any_Priority} or
21715 @code{Ada.Text_IO.Count}) is not flagged
21716
21717 This rule has no parameters.
21718
21719
21720
21721 @node Raising_External_Exceptions
21722 @subsection @code{Raising_External_Exceptions}
21723 @cindex @code{Raising_External_Exceptions} rule (for @command{gnatcheck})
21724
21725 @noindent
21726 Flag any @code{raise} statement, in a program unit declared in a library
21727 package or in a generic library package, for an exception that is
21728 neither a predefined exception nor an exception that is also declared (or
21729 renamed) in the visible part of the package.
21730
21731 This rule has no parameters.
21732
21733
21734
21735 @node Raising_Predefined_Exceptions
21736 @subsection @code{Raising_Predefined_Exceptions}
21737 @cindex @code{Raising_Predefined_Exceptions} rule (for @command{gnatcheck})
21738
21739 @noindent
21740 Flag each @code{raise} statement that raises a predefined exception
21741 (i.e., one of the exceptions @code{Constraint_Error}, @code{Numeric_Error},
21742 @code{Program_Error}, @code{Storage_Error}, or @code{Tasking_Error}).
21743
21744 This rule has no parameters.
21745
21746 @node Separate_Numeric_Error_Handlers
21747 @subsection @code{Separate_Numeric_Error_Handlers}
21748 @cindex @code{Separate_Numeric_Error_Handlers} rule (for @command{gnatcheck})
21749
21750 @noindent
21751 Flags each exception handler that contains a choice for
21752 the predefined @code{Constraint_Error} exception, but does not contain
21753 the choice for the predefined @code{Numeric_Error} exception, or
21754 that contains the choice for @code{Numeric_Error}, but does not contain the
21755 choice for @code{Constraint_Error}.
21756
21757 This rule has no parameters.
21758
21759 @ignore
21760 @node Recursion
21761 @subsection @code{Recursion} (under construction, GLOBAL)
21762 @cindex @code{Recursion} rule (for @command{gnatcheck})
21763
21764 @noindent
21765 Flag recursive subprograms (cycles in the call graph). Declarations, and not
21766 calls, of recursive subprograms are detected.
21767
21768 This rule has no parameters.
21769 @end ignore
21770
21771 @ignore
21772 @node Side_Effect_Functions
21773 @subsection @code{Side_Effect_Functions} (under construction, GLOBAL)
21774 @cindex @code{Side_Effect_Functions} rule (for @command{gnatcheck})
21775
21776 @noindent
21777 Flag functions with side effects.
21778
21779 We define a side effect as changing any data object that is not local for the
21780 body of this function.
21781
21782 At the moment, we do NOT consider a side effect any input-output operations
21783 (changing a state or a content of any file).
21784
21785 We do not consider protected functions for this rule (???)
21786
21787 There are the following sources of side effect:
21788
21789 @enumerate
21790 @item Explicit (or direct) side-effect:
21791
21792 @itemize @bullet
21793 @item
21794 direct assignment to a non-local variable;
21795
21796 @item
21797 direct call to an entity that is known to change some data object that is
21798 not local for the body of this function (Note, that if F1 calls F2 and F2
21799 does have a side effect, this does not automatically mean that F1 also
21800 have a side effect, because it may be the case that F2 is declared in
21801 F1's body and it changes some data object that is global for F2, but
21802 local for F1);
21803 @end itemize
21804
21805 @item Indirect side-effect:
21806 @itemize @bullet
21807 @item
21808 Subprogram calls implicitly issued by:
21809 @itemize @bullet
21810 @item
21811 computing initialization expressions from type declarations as a part
21812 of object elaboration or allocator evaluation;
21813 @item
21814 computing implicit parameters of subprogram or entry calls or generic
21815 instantiations;
21816 @end itemize
21817
21818 @item
21819 activation of a task that change some non-local data object (directly or
21820 indirectly);
21821
21822 @item
21823 elaboration code of a package that is a result of a package instantiation;
21824
21825 @item
21826 controlled objects;
21827 @end itemize
21828
21829 @item Situations when we can suspect a side-effect, but the full static check
21830 is either impossible or too hard:
21831 @itemize @bullet
21832 @item
21833 assignment to access variables or to the objects pointed by access
21834 variables;
21835
21836 @item
21837 call to a subprogram pointed by access-to-subprogram value
21838
21839 @item
21840 dispatching calls;
21841 @end itemize
21842 @end enumerate
21843
21844 @noindent
21845 This rule has no parameters.
21846 @end ignore
21847
21848 @node Slices
21849 @subsection @code{Slices}
21850 @cindex @code{Slices} rule (for @command{gnatcheck})
21851
21852 @noindent
21853 Flag all uses of array slicing
21854
21855 This rule has no parameters.
21856
21857
21858 @node Unassigned_OUT_Parameters
21859 @subsection @code{Unassigned_OUT_Parameters}
21860 @cindex @code{Unassigned_OUT_Parameters} rule (for @command{gnatcheck})
21861
21862 @noindent
21863 Flags procedures' @code{out} parameters that are not assigned, and
21864 identifies the contexts in which the assignments are missing.
21865
21866 An @code{out} parameter is flagged in the statements in the procedure
21867 body's handled sequence of statements (before the procedure body's
21868 @code{exception} part, if any) if this sequence of statements contains
21869 no assignments to the parameter.
21870
21871 An @code{out} parameter is flagged in an exception handler in the exception
21872 part of the procedure body's handled sequence of statements if the handler
21873 contains no assignment to the parameter.
21874
21875 Bodies of generic procedures are also considered.
21876
21877 The following are treated as assignments to an @code{out} parameter:
21878
21879 @itemize @bullet
21880 @item
21881 an assignment statement, with the parameter or some component as the target;
21882
21883 @item
21884 passing the parameter (or one of its components) as an @code{out} or
21885 @code{in out} parameter.
21886 @end itemize
21887
21888 @noindent
21889 This rule does not have any parameters.
21890
21891
21892
21893 @node Uncommented_BEGIN_In_Package_Bodies
21894 @subsection @code{Uncommented_BEGIN_In_Package_Bodies}
21895 @cindex @code{Uncommented_BEGIN_In_Package_Bodies} rule (for @command{gnatcheck})
21896
21897 @noindent
21898 Flags each package body with declarations and a statement part that does not
21899 include a trailing comment on the line containing the @code{begin} keyword;
21900 this trailing comment needs to specify the package name and nothing else.
21901 The @code{begin} is not flagged if the package body does not
21902 contain any declarations.
21903
21904 If the @code{begin} keyword is placed on the
21905 same line as the last declaration or the first statement, it is flagged
21906 independently of whether the line contains a trailing comment. The
21907 diagnostic message is attached to the line containing the first statement.
21908
21909 This rule has no parameters.
21910
21911
21912 @node Unconstrained_Array_Returns
21913 @subsection @code{Unconstrained_Array_Returns}
21914 @cindex @code{Unconstrained_Array_Returns} rule (for @command{gnatcheck})
21915
21916 @noindent
21917 Flag each function returning an unconstrained array. Function declarations,
21918 function bodies (and body stubs) having no separate specifications,
21919 and generic function instantiations are checked.
21920 Generic function declarations, function calls and function renamings are
21921 not checked.
21922
21923 This rule has no parameters.
21924
21925 @node Universal_Ranges
21926 @subsection @code{Universal_Ranges}
21927 @cindex @code{Universal_Ranges} rule (for @command{gnatcheck})
21928
21929 @noindent
21930 Flag discrete ranges that are a part of an index constraint, constrained
21931 array definition, or @code{for}-loop parameter specification, and whose bounds
21932 are both of type @i{universal_integer}. Ranges that have at least one
21933 bound of a specific type (such as @code{1 .. N}, where @code{N} is a variable
21934 or an expression of non-universal type) are not flagged.
21935
21936 This rule has no parameters.
21937
21938
21939 @node Unnamed_Blocks_And_Loops
21940 @subsection @code{Unnamed_Blocks_And_Loops}
21941 @cindex @code{Unnamed_Blocks_And_Loops} rule (for @command{gnatcheck})
21942
21943 @noindent
21944 Flag each unnamed block statement and loop statement.
21945
21946 The rule has no parameters.
21947
21948
21949
21950 @ignore
21951 @node Unused_Subprograms
21952 @subsection @code{Unused_Subprograms} (under construction, GLOBAL)
21953 @cindex @code{Unused_Subprograms} rule (for @command{gnatcheck})
21954
21955 @noindent
21956 Flag all unused subprograms.
21957
21958 This rule has no parameters.
21959 @end ignore
21960
21961
21962
21963
21964 @node USE_PACKAGE_Clauses
21965 @subsection @code{USE_PACKAGE_Clauses}
21966 @cindex @code{USE_PACKAGE_Clauses} rule (for @command{gnatcheck})
21967
21968 @noindent
21969 Flag all @code{use} clauses for packages; @code{use type} clauses are
21970 not flagged.
21971
21972 This rule has no parameters.
21973
21974
21975
21976 @node Volatile_Objects_Without_Address_Clauses
21977 @subsection @code{Volatile_Objects_Without_Address_Clauses}
21978 @cindex @code{Volatile_Objects_Without_Address_Clauses} rule (for @command{gnatcheck})
21979
21980 @noindent
21981 Flag each volatile object that does not have an address clause.
21982
21983 The following check is made: if the pragma @code{Volatile} is applied to a
21984 data object or to its type, then an address clause must
21985 be supplied for this object.
21986
21987 This rule does not check the components of data objects,
21988 array components that are volatile as a result of the pragma
21989 @code{Volatile_Components}, or objects that are volatile because
21990 they are atomic as a result of pragmas @code{Atomic} or
21991 @code{Atomic_Components}.
21992
21993 Only variable declarations, and not constant declarations, are checked.
21994
21995 This rule has no parameters.
21996
21997
21998 @c *********************************
21999 @node Creating Sample Bodies Using gnatstub
22000 @chapter Creating Sample Bodies Using @command{gnatstub}
22001 @findex gnatstub
22002
22003 @noindent
22004 @command{gnatstub} creates body stubs, that is, empty but compilable bodies
22005 for library unit declarations.
22006
22007 Note: to invoke @code{gnatstub} with a project file, use the @code{gnat}
22008 driver (see @ref{The GNAT Driver and Project Files}).
22009
22010 To create a body stub, @command{gnatstub} has to compile the library
22011 unit declaration. Therefore, bodies can be created only for legal
22012 library units. Moreover, if a library unit depends semantically upon
22013 units located outside the current directory, you have to provide
22014 the source search path when calling @command{gnatstub}, see the description
22015 of @command{gnatstub} switches below.
22016
22017 @menu
22018 * Running gnatstub::
22019 * Switches for gnatstub::
22020 @end menu
22021
22022 @node Running gnatstub
22023 @section Running @command{gnatstub}
22024
22025 @noindent
22026 @command{gnatstub} has the command-line interface of the form
22027
22028 @smallexample
22029 $ gnatstub @ovar{switches} @var{filename} @ovar{directory}
22030 @end smallexample
22031
22032 @noindent
22033 where
22034 @table @var
22035 @item filename
22036 is the name of the source file that contains a library unit declaration
22037 for which a body must be created. The file name may contain the path
22038 information.
22039 The file name does not have to follow the GNAT file name conventions. If the
22040 name
22041 does not follow GNAT file naming conventions, the name of the body file must
22042 be provided
22043 explicitly as the value of the @option{^-o^/BODY=^@var{body-name}} option.
22044 If the file name follows the GNAT file naming
22045 conventions and the name of the body file is not provided,
22046 @command{gnatstub}
22047 creates the name
22048 of the body file from the argument file name by replacing the @file{.ads}
22049 suffix
22050 with the @file{.adb} suffix.
22051
22052 @item directory
22053 indicates the directory in which the body stub is to be placed (the default
22054 is the
22055 current directory)
22056
22057 @item switches
22058 is an optional sequence of switches as described in the next section
22059 @end table
22060
22061 @node Switches for gnatstub
22062 @section Switches for @command{gnatstub}
22063
22064 @table @option
22065 @c !sort!
22066
22067 @item ^-f^/FULL^
22068 @cindex @option{^-f^/FULL^} (@command{gnatstub})
22069 If the destination directory already contains a file with the name of the
22070 body file
22071 for the argument spec file, replace it with the generated body stub.
22072
22073 @item ^-hs^/HEADER=SPEC^
22074 @cindex @option{^-hs^/HEADER=SPEC^} (@command{gnatstub})
22075 Put the comment header (i.e., all the comments preceding the
22076 compilation unit) from the source of the library unit declaration
22077 into the body stub.
22078
22079 @item ^-hg^/HEADER=GENERAL^
22080 @cindex @option{^-hg^/HEADER=GENERAL^} (@command{gnatstub})
22081 Put a sample comment header into the body stub.
22082
22083 @item ^--header-file=@var{filename}^/FROM_HEADER_FILE=@var{filename}^
22084 @cindex @option{^--header-file^/FROM_HEADER_FILE=^} (@command{gnatstub})
22085 Use the content of the file as the comment header for a generated body stub.
22086
22087 @ifclear vms
22088 @item -IDIR
22089 @cindex @option{-IDIR} (@command{gnatstub})
22090 @itemx -I-
22091 @cindex @option{-I-} (@command{gnatstub})
22092 @end ifclear
22093 @ifset vms
22094 @item /NOCURRENT_DIRECTORY
22095 @cindex @option{/NOCURRENT_DIRECTORY} (@command{gnatstub})
22096 @end ifset
22097 ^These switches have ^This switch has^ the same meaning as in calls to
22098 @command{gcc}.
22099 ^They define ^It defines ^ the source search path in the call to
22100 @command{gcc} issued
22101 by @command{gnatstub} to compile an argument source file.
22102
22103 @item ^-gnatec^/CONFIGURATION_PRAGMAS_FILE=^@var{PATH}
22104 @cindex @option{^-gnatec^/CONFIGURATION_PRAGMAS_FILE^} (@command{gnatstub})
22105 This switch has the same meaning as in calls to @command{gcc}.
22106 It defines the additional configuration file to be passed to the call to
22107 @command{gcc} issued
22108 by @command{gnatstub} to compile an argument source file.
22109
22110 @item ^-gnatyM^/MAX_LINE_LENGTH=^@var{n}
22111 @cindex @option{^-gnatyM^/MAX_LINE_LENGTH^} (@command{gnatstub})
22112 (@var{n} is a non-negative integer). Set the maximum line length in the
22113 body stub to @var{n}; the default is 79. The maximum value that can be
22114 specified is 32767. Note that in the special case of configuration
22115 pragma files, the maximum is always 32767 regardless of whether or
22116 not this switch appears.
22117
22118 @item ^-gnaty^/STYLE_CHECKS=^@var{n}
22119 @cindex @option{^-gnaty^/STYLE_CHECKS=^} (@command{gnatstub})
22120 (@var{n} is a non-negative integer from 1 to 9). Set the indentation level in
22121 the generated body sample to @var{n}.
22122 The default indentation is 3.
22123
22124 @item ^-gnatyo^/ORDERED_SUBPROGRAMS^
22125 @cindex @option{^-gnato^/ORDERED_SUBPROGRAMS^} (@command{gnatstub})
22126 Order local bodies alphabetically. (By default local bodies are ordered
22127 in the same way as the corresponding local specs in the argument spec file.)
22128
22129 @item ^-i^/INDENTATION=^@var{n}
22130 @cindex @option{^-i^/INDENTATION^} (@command{gnatstub})
22131 Same as @option{^-gnaty^/STYLE_CHECKS=^@var{n}}
22132
22133 @item ^-k^/TREE_FILE=SAVE^
22134 @cindex @option{^-k^/TREE_FILE=SAVE^} (@command{gnatstub})
22135 Do not remove the tree file (i.e., the snapshot of the compiler internal
22136 structures used by @command{gnatstub}) after creating the body stub.
22137
22138 @item ^-l^/LINE_LENGTH=^@var{n}
22139 @cindex @option{^-l^/LINE_LENGTH^} (@command{gnatstub})
22140 Same as @option{^-gnatyM^/MAX_LINE_LENGTH=^@var{n}}
22141
22142 @item ^-o^/BODY=^@var{body-name}
22143 @cindex @option{^-o^/BODY^} (@command{gnatstub})
22144 Body file name. This should be set if the argument file name does not
22145 follow
22146 the GNAT file naming
22147 conventions. If this switch is omitted the default name for the body will be
22148 obtained
22149 from the argument file name according to the GNAT file naming conventions.
22150
22151 @item ^-q^/QUIET^
22152 @cindex @option{^-q^/QUIET^} (@command{gnatstub})
22153 Quiet mode: do not generate a confirmation when a body is
22154 successfully created, and do not generate a message when a body is not
22155 required for an
22156 argument unit.
22157
22158 @item ^-r^/TREE_FILE=REUSE^
22159 @cindex @option{^-r^/TREE_FILE=REUSE^} (@command{gnatstub})
22160 Reuse the tree file (if it exists) instead of creating it. Instead of
22161 creating the tree file for the library unit declaration, @command{gnatstub}
22162 tries to find it in the current directory and use it for creating
22163 a body. If the tree file is not found, no body is created. This option
22164 also implies @option{^-k^/SAVE^}, whether or not
22165 the latter is set explicitly.
22166
22167 @item ^-t^/TREE_FILE=OVERWRITE^
22168 @cindex @option{^-t^/TREE_FILE=OVERWRITE^} (@command{gnatstub})
22169 Overwrite the existing tree file. If the current directory already
22170 contains the file which, according to the GNAT file naming rules should
22171 be considered as a tree file for the argument source file,
22172 @command{gnatstub}
22173 will refuse to create the tree file needed to create a sample body
22174 unless this option is set.
22175
22176 @item ^-v^/VERBOSE^
22177 @cindex @option{^-v^/VERBOSE^} (@command{gnatstub})
22178 Verbose mode: generate version information.
22179
22180 @end table
22181
22182 @node Other Utility Programs
22183 @chapter Other Utility Programs
22184
22185 @noindent
22186 This chapter discusses some other utility programs available in the Ada
22187 environment.
22188
22189 @menu
22190 * Using Other Utility Programs with GNAT::
22191 * The External Symbol Naming Scheme of GNAT::
22192 * Converting Ada Files to html with gnathtml::
22193 * Installing gnathtml::
22194 @ifset vms
22195 * LSE::
22196 * Profiling::
22197 @end ifset
22198 @end menu
22199
22200 @node Using Other Utility Programs with GNAT
22201 @section Using Other Utility Programs with GNAT
22202
22203 @noindent
22204 The object files generated by GNAT are in standard system format and in
22205 particular the debugging information uses this format. This means
22206 programs generated by GNAT can be used with existing utilities that
22207 depend on these formats.
22208
22209 @ifclear vms
22210 In general, any utility program that works with C will also often work with
22211 Ada programs generated by GNAT. This includes software utilities such as
22212 gprof (a profiling program), @code{gdb} (the FSF debugger), and utilities such
22213 as Purify.
22214 @end ifclear
22215
22216 @node The External Symbol Naming Scheme of GNAT
22217 @section The External Symbol Naming Scheme of GNAT
22218
22219 @noindent
22220 In order to interpret the output from GNAT, when using tools that are
22221 originally intended for use with other languages, it is useful to
22222 understand the conventions used to generate link names from the Ada
22223 entity names.
22224
22225 All link names are in all lowercase letters. With the exception of library
22226 procedure names, the mechanism used is simply to use the full expanded
22227 Ada name with dots replaced by double underscores. For example, suppose
22228 we have the following package spec:
22229
22230 @smallexample @c ada
22231 @group
22232 @cartouche
22233 package QRS is
22234 MN : Integer;
22235 end QRS;
22236 @end cartouche
22237 @end group
22238 @end smallexample
22239
22240 @noindent
22241 The variable @code{MN} has a full expanded Ada name of @code{QRS.MN}, so
22242 the corresponding link name is @code{qrs__mn}.
22243 @findex Export
22244 Of course if a @code{pragma Export} is used this may be overridden:
22245
22246 @smallexample @c ada
22247 @group
22248 @cartouche
22249 package Exports is
22250 Var1 : Integer;
22251 pragma Export (Var1, C, External_Name => "var1_name");
22252 Var2 : Integer;
22253 pragma Export (Var2, C, Link_Name => "var2_link_name");
22254 end Exports;
22255 @end cartouche
22256 @end group
22257 @end smallexample
22258
22259 @noindent
22260 In this case, the link name for @var{Var1} is whatever link name the
22261 C compiler would assign for the C function @var{var1_name}. This typically
22262 would be either @var{var1_name} or @var{_var1_name}, depending on operating
22263 system conventions, but other possibilities exist. The link name for
22264 @var{Var2} is @var{var2_link_name}, and this is not operating system
22265 dependent.
22266
22267 @findex _main
22268 One exception occurs for library level procedures. A potential ambiguity
22269 arises between the required name @code{_main} for the C main program,
22270 and the name we would otherwise assign to an Ada library level procedure
22271 called @code{Main} (which might well not be the main program).
22272
22273 To avoid this ambiguity, we attach the prefix @code{_ada_} to such
22274 names. So if we have a library level procedure such as
22275
22276 @smallexample @c ada
22277 @group
22278 @cartouche
22279 procedure Hello (S : String);
22280 @end cartouche
22281 @end group
22282 @end smallexample
22283
22284 @noindent
22285 the external name of this procedure will be @var{_ada_hello}.
22286
22287
22288 @node Converting Ada Files to html with gnathtml
22289 @section Converting Ada Files to HTML with @code{gnathtml}
22290
22291 @noindent
22292 This @code{Perl} script allows Ada source files to be browsed using
22293 standard Web browsers. For installation procedure, see the section
22294 @xref{Installing gnathtml}.
22295
22296 Ada reserved keywords are highlighted in a bold font and Ada comments in
22297 a blue font. Unless your program was compiled with the gcc @option{-gnatx}
22298 switch to suppress the generation of cross-referencing information, user
22299 defined variables and types will appear in a different color; you will
22300 be able to click on any identifier and go to its declaration.
22301
22302 The command line is as follow:
22303 @smallexample
22304 $ perl gnathtml.pl @ovar{^switches^options^} @var{ada-files}
22305 @end smallexample
22306
22307 @noindent
22308 You can pass it as many Ada files as you want. @code{gnathtml} will generate
22309 an html file for every ada file, and a global file called @file{index.htm}.
22310 This file is an index of every identifier defined in the files.
22311
22312 The available ^switches^options^ are the following ones:
22313
22314 @table @option
22315 @item -83
22316 @cindex @option{-83} (@code{gnathtml})
22317 Only the Ada 83 subset of keywords will be highlighted.
22318
22319 @item -cc @var{color}
22320 @cindex @option{-cc} (@code{gnathtml})
22321 This option allows you to change the color used for comments. The default
22322 value is green. The color argument can be any name accepted by html.
22323
22324 @item -d
22325 @cindex @option{-d} (@code{gnathtml})
22326 If the Ada files depend on some other files (for instance through
22327 @code{with} clauses, the latter files will also be converted to html.
22328 Only the files in the user project will be converted to html, not the files
22329 in the run-time library itself.
22330
22331 @item -D
22332 @cindex @option{-D} (@code{gnathtml})
22333 This command is the same as @option{-d} above, but @command{gnathtml} will
22334 also look for files in the run-time library, and generate html files for them.
22335
22336 @item -ext @var{extension}
22337 @cindex @option{-ext} (@code{gnathtml})
22338 This option allows you to change the extension of the generated HTML files.
22339 If you do not specify an extension, it will default to @file{htm}.
22340
22341 @item -f
22342 @cindex @option{-f} (@code{gnathtml})
22343 By default, gnathtml will generate html links only for global entities
22344 ('with'ed units, global variables and types,@dots{}). If you specify
22345 @option{-f} on the command line, then links will be generated for local
22346 entities too.
22347
22348 @item -l @var{number}
22349 @cindex @option{-l} (@code{gnathtml})
22350 If this ^switch^option^ is provided and @var{number} is not 0, then
22351 @code{gnathtml} will number the html files every @var{number} line.
22352
22353 @item -I @var{dir}
22354 @cindex @option{-I} (@code{gnathtml})
22355 Specify a directory to search for library files (@file{.ALI} files) and
22356 source files. You can provide several -I switches on the command line,
22357 and the directories will be parsed in the order of the command line.
22358
22359 @item -o @var{dir}
22360 @cindex @option{-o} (@code{gnathtml})
22361 Specify the output directory for html files. By default, gnathtml will
22362 saved the generated html files in a subdirectory named @file{html/}.
22363
22364 @item -p @var{file}
22365 @cindex @option{-p} (@code{gnathtml})
22366 If you are using Emacs and the most recent Emacs Ada mode, which provides
22367 a full Integrated Development Environment for compiling, checking,
22368 running and debugging applications, you may use @file{.gpr} files
22369 to give the directories where Emacs can find sources and object files.
22370
22371 Using this ^switch^option^, you can tell gnathtml to use these files.
22372 This allows you to get an html version of your application, even if it
22373 is spread over multiple directories.
22374
22375 @item -sc @var{color}
22376 @cindex @option{-sc} (@code{gnathtml})
22377 This ^switch^option^ allows you to change the color used for symbol
22378 definitions.
22379 The default value is red. The color argument can be any name accepted by html.
22380
22381 @item -t @var{file}
22382 @cindex @option{-t} (@code{gnathtml})
22383 This ^switch^option^ provides the name of a file. This file contains a list of
22384 file names to be converted, and the effect is exactly as though they had
22385 appeared explicitly on the command line. This
22386 is the recommended way to work around the command line length limit on some
22387 systems.
22388
22389 @end table
22390
22391 @node Installing gnathtml
22392 @section Installing @code{gnathtml}
22393
22394 @noindent
22395 @code{Perl} needs to be installed on your machine to run this script.
22396 @code{Perl} is freely available for almost every architecture and
22397 Operating System via the Internet.
22398
22399 On Unix systems, you may want to modify the first line of the script
22400 @code{gnathtml}, to explicitly tell the Operating system where Perl
22401 is. The syntax of this line is:
22402 @smallexample
22403 #!full_path_name_to_perl
22404 @end smallexample
22405
22406 @noindent
22407 Alternatively, you may run the script using the following command line:
22408
22409 @smallexample
22410 $ perl gnathtml.pl @ovar{switches} @var{files}
22411 @end smallexample
22412
22413 @ifset vms
22414 @node LSE
22415 @section LSE
22416 @findex LSE
22417
22418 @noindent
22419 The GNAT distribution provides an Ada 95 template for the HP Language
22420 Sensitive Editor (LSE), a component of DECset. In order to
22421 access it, invoke LSE with the qualifier /ENVIRONMENT=GNU:[LIB]ADA95.ENV.
22422
22423 @node Profiling
22424 @section Profiling
22425 @findex PCA
22426
22427 @noindent
22428 GNAT supports The HP Performance Coverage Analyzer (PCA), a component
22429 of DECset. To use it proceed as outlined under ``HELP PCA'', except for running
22430 the collection phase with the /DEBUG qualifier.
22431
22432 @smallexample
22433 $ GNAT MAKE /DEBUG <PROGRAM_NAME>
22434 $ DEFINE LIB$DEBUG PCA$COLLECTOR
22435 $ RUN/DEBUG <PROGRAM_NAME>
22436 @end smallexample
22437 @noindent
22438 @end ifset
22439
22440 @ifclear vms
22441 @c ******************************
22442 @node Code Coverage and Profiling
22443 @chapter Code Coverage and Profiling
22444 @cindex Code Coverage
22445 @cindex Profiling
22446
22447 @noindent
22448 This chapter describes how to use @code{gcov} - coverage testing tool - and
22449 @code{gprof} - profiler tool - on your Ada programs.
22450
22451 @menu
22452 * Code Coverage of Ada Programs using gcov::
22453 * Profiling an Ada Program using gprof::
22454 @end menu
22455
22456 @node Code Coverage of Ada Programs using gcov
22457 @section Code Coverage of Ada Programs using gcov
22458 @cindex gcov
22459 @cindex -fprofile-arcs
22460 @cindex -ftest-coverage
22461 @cindex -coverage
22462 @cindex Code Coverage
22463
22464 @noindent
22465 @code{gcov} is a test coverage program: it analyzes the execution of a given
22466 program on selected tests, to help you determine the portions of the program
22467 that are still untested.
22468
22469 @code{gcov} is part of the GCC suite, and is described in detail in the GCC
22470 User's Guide. You can refer to this documentation for a more complete
22471 description.
22472
22473 This chapter provides a quick startup guide, and
22474 details some Gnat-specific features.
22475
22476 @menu
22477 * Quick startup guide::
22478 * Gnat specifics::
22479 @end menu
22480
22481 @node Quick startup guide
22482 @subsection Quick startup guide
22483
22484 In order to perform coverage analysis of a program using @code{gcov}, 3
22485 steps are needed:
22486
22487 @itemize @bullet
22488 @item
22489 Code instrumentation during the compilation process
22490 @item
22491 Execution of the instrumented program
22492 @item
22493 Execution of the @code{gcov} tool to generate the result.
22494 @end itemize
22495
22496 The code instrumentation needed by gcov is created at the object level:
22497 The source code is not modified in any way, because the instrumentation code is
22498 inserted by gcc during the compilation process. To compile your code with code
22499 coverage activated, you need to recompile your whole project using the
22500 switches
22501 @code{-fprofile-arcs} and @code{-ftest-coverage}, and link it using
22502 @code{-fprofile-arcs}.
22503
22504 @smallexample
22505 $ gnatmake -P my_project.gpr -f -cargs -fprofile-arcs -ftest-coverage \
22506 -largs -fprofile-arcs
22507 @end smallexample
22508
22509 This compilation process will create @file{.gcno} files together with
22510 the usual object files.
22511
22512 Once the program is compiled with coverage instrumentation, you can
22513 run it as many times as needed - on portions of a test suite for
22514 example. The first execution will produce @file{.gcda} files at the
22515 same location as the @file{.gcno} files. The following executions
22516 will update those files, so that a cumulative result of the covered
22517 portions of the program is generated.
22518
22519 Finally, you need to call the @code{gcov} tool. The different options of
22520 @code{gcov} are available in the GCC User's Guide, section 'Invoking gcov'.
22521
22522 This will create annotated source files with a @file{.gcov} extension:
22523 @file{my_main.adb} file will be analysed in @file{my_main.adb.gcov}.
22524
22525 @node Gnat specifics
22526 @subsection Gnat specifics
22527
22528 Because Ada semantics, portions of the source code may be shared among
22529 several object files. This is the case for example when generics are
22530 involved, when inlining is active or when declarations generate initialisation
22531 calls. In order to take
22532 into account this shared code, you need to call @code{gcov} on all
22533 source files of the tested program at once.
22534
22535 The list of source files might exceed the system's maximum command line
22536 length. In order to bypass this limitation, a new mechanism has been
22537 implemented in @code{gcov}: you can now list all your project's files into a
22538 text file, and provide this file to gcov as a parameter, preceded by a @@
22539 (e.g. @samp{gcov @@mysrclist.txt}).
22540
22541 Note that on AIX compiling a static library with @code{-fprofile-arcs} is
22542 not supported as there can be unresolved symbols during the final link.
22543
22544 @node Profiling an Ada Program using gprof
22545 @section Profiling an Ada Program using gprof
22546 @cindex gprof
22547 @cindex -pg
22548 @cindex Profiling
22549
22550 @noindent
22551 This section is not meant to be an exhaustive documentation of @code{gprof}.
22552 Full documentation for it can be found in the GNU Profiler User's Guide
22553 documentation that is part of this GNAT distribution.
22554
22555 Profiling a program helps determine the parts of a program that are executed
22556 most often, and are therefore the most time-consuming.
22557
22558 @code{gprof} is the standard GNU profiling tool; it has been enhanced to
22559 better handle Ada programs and multitasking.
22560 It is currently supported on the following platforms
22561 @itemize @bullet
22562 @item
22563 linux x86/x86_64
22564 @item
22565 solaris sparc/sparc64/x86
22566 @item
22567 windows x86
22568 @end itemize
22569
22570 @noindent
22571 In order to profile a program using @code{gprof}, 3 steps are needed:
22572
22573 @itemize @bullet
22574 @item
22575 Code instrumentation, requiring a full recompilation of the project with the
22576 proper switches.
22577 @item
22578 Execution of the program under the analysis conditions, i.e. with the desired
22579 input.
22580 @item
22581 Analysis of the results using the @code{gprof} tool.
22582 @end itemize
22583
22584 @noindent
22585 The following sections detail the different steps, and indicate how
22586 to interpret the results:
22587 @menu
22588 * Compilation for profiling::
22589 * Program execution::
22590 * Running gprof::
22591 * Interpretation of profiling results::
22592 @end menu
22593
22594 @node Compilation for profiling
22595 @subsection Compilation for profiling
22596 @cindex -pg
22597 @cindex Profiling
22598
22599 In order to profile a program the first step is to tell the compiler
22600 to generate the necessary profiling information. The compiler switch to be used
22601 is @code{-pg}, which must be added to other compilation switches. This
22602 switch needs to be specified both during compilation and link stages, and can
22603 be specified once when using gnatmake:
22604
22605 @smallexample
22606 gnatmake -f -pg -P my_project
22607 @end smallexample
22608
22609 @noindent
22610 Note that only the objects that were compiled with the @samp{-pg} switch will be
22611 profiled; if you need to profile your whole project, use the
22612 @samp{-f} gnatmake switch to force full recompilation.
22613
22614 @node Program execution
22615 @subsection Program execution
22616
22617 @noindent
22618 Once the program has been compiled for profiling, you can run it as usual.
22619
22620 The only constraint imposed by profiling is that the program must terminate
22621 normally. An interrupted program (via a Ctrl-C, kill, etc.) will not be
22622 properly analyzed.
22623
22624 Once the program completes execution, a data file called @file{gmon.out} is
22625 generated in the directory where the program was launched from. If this file
22626 already exists, it will be overwritten.
22627
22628 @node Running gprof
22629 @subsection Running gprof
22630
22631 @noindent
22632 The @code{gprof} tool is called as follow:
22633
22634 @smallexample
22635 gprof my_prog gmon.out
22636 @end smallexample
22637
22638 @noindent
22639 or simpler:
22640
22641 @smallexample
22642 gprof my_prog
22643 @end smallexample
22644
22645 @noindent
22646 The complete form of the gprof command line is the following:
22647
22648 @smallexample
22649 gprof [^switches^options^] [executable [data-file]]
22650 @end smallexample
22651
22652 @noindent
22653 @code{gprof} supports numerous ^switch^options^. The order of these
22654 ^switch^options^ does not matter. The full list of options can be found in
22655 the GNU Profiler User's Guide documentation that comes with this documentation.
22656
22657 The following is the subset of those switches that is most relevant:
22658
22659 @table @option
22660
22661 @item --demangle[=@var{style}]
22662 @itemx --no-demangle
22663 @cindex @option{--demangle} (@code{gprof})
22664 These options control whether symbol names should be demangled when
22665 printing output. The default is to demangle C++ symbols. The
22666 @code{--no-demangle} option may be used to turn off demangling. Different
22667 compilers have different mangling styles. The optional demangling style
22668 argument can be used to choose an appropriate demangling style for your
22669 compiler, in particular Ada symbols generated by GNAT can be demangled using
22670 @code{--demangle=gnat}.
22671
22672 @item -e @var{function_name}
22673 @cindex @option{-e} (@code{gprof})
22674 The @samp{-e @var{function}} option tells @code{gprof} not to print
22675 information about the function @var{function_name} (and its
22676 children@dots{}) in the call graph. The function will still be listed
22677 as a child of any functions that call it, but its index number will be
22678 shown as @samp{[not printed]}. More than one @samp{-e} option may be
22679 given; only one @var{function_name} may be indicated with each @samp{-e}
22680 option.
22681
22682 @item -E @var{function_name}
22683 @cindex @option{-E} (@code{gprof})
22684 The @code{-E @var{function}} option works like the @code{-e} option, but
22685 execution time spent in the function (and children who were not called from
22686 anywhere else), will not be used to compute the percentages-of-time for
22687 the call graph. More than one @samp{-E} option may be given; only one
22688 @var{function_name} may be indicated with each @samp{-E} option.
22689
22690 @item -f @var{function_name}
22691 @cindex @option{-f} (@code{gprof})
22692 The @samp{-f @var{function}} option causes @code{gprof} to limit the
22693 call graph to the function @var{function_name} and its children (and
22694 their children@dots{}). More than one @samp{-f} option may be given;
22695 only one @var{function_name} may be indicated with each @samp{-f}
22696 option.
22697
22698 @item -F @var{function_name}
22699 @cindex @option{-F} (@code{gprof})
22700 The @samp{-F @var{function}} option works like the @code{-f} option, but
22701 only time spent in the function and its children (and their
22702 children@dots{}) will be used to determine total-time and
22703 percentages-of-time for the call graph. More than one @samp{-F} option
22704 may be given; only one @var{function_name} may be indicated with each
22705 @samp{-F} option. The @samp{-F} option overrides the @samp{-E} option.
22706
22707 @end table
22708
22709 @node Interpretation of profiling results
22710 @subsection Interpretation of profiling results
22711
22712 @noindent
22713
22714 The results of the profiling analysis are represented by two arrays: the
22715 'flat profile' and the 'call graph'. Full documentation of those outputs
22716 can be found in the GNU Profiler User's Guide.
22717
22718 The flat profile shows the time spent in each function of the program, and how
22719 many time it has been called. This allows you to locate easily the most
22720 time-consuming functions.
22721
22722 The call graph shows, for each subprogram, the subprograms that call it,
22723 and the subprograms that it calls. It also provides an estimate of the time
22724 spent in each of those callers/called subprograms.
22725 @end ifclear
22726
22727 @c ******************************
22728 @node Running and Debugging Ada Programs
22729 @chapter Running and Debugging Ada Programs
22730 @cindex Debugging
22731
22732 @noindent
22733 This chapter discusses how to debug Ada programs.
22734 @ifset vms
22735 It applies to GNAT on the Alpha OpenVMS platform;
22736 for I64 OpenVMS please refer to the @cite{OpenVMS Debugger Manual},
22737 since HP has implemented Ada support in the OpenVMS debugger on I64.
22738 @end ifset
22739
22740 An incorrect Ada program may be handled in three ways by the GNAT compiler:
22741
22742 @enumerate
22743 @item
22744 The illegality may be a violation of the static semantics of Ada. In
22745 that case GNAT diagnoses the constructs in the program that are illegal.
22746 It is then a straightforward matter for the user to modify those parts of
22747 the program.
22748
22749 @item
22750 The illegality may be a violation of the dynamic semantics of Ada. In
22751 that case the program compiles and executes, but may generate incorrect
22752 results, or may terminate abnormally with some exception.
22753
22754 @item
22755 When presented with a program that contains convoluted errors, GNAT
22756 itself may terminate abnormally without providing full diagnostics on
22757 the incorrect user program.
22758 @end enumerate
22759
22760 @menu
22761 * The GNAT Debugger GDB::
22762 * Running GDB::
22763 * Introduction to GDB Commands::
22764 * Using Ada Expressions::
22765 * Calling User-Defined Subprograms::
22766 * Using the Next Command in a Function::
22767 * Ada Exceptions::
22768 * Ada Tasks::
22769 * Debugging Generic Units::
22770 * GNAT Abnormal Termination or Failure to Terminate::
22771 * Naming Conventions for GNAT Source Files::
22772 * Getting Internal Debugging Information::
22773 * Stack Traceback::
22774 @end menu
22775
22776 @cindex Debugger
22777 @findex gdb
22778
22779 @node The GNAT Debugger GDB
22780 @section The GNAT Debugger GDB
22781
22782 @noindent
22783 @code{GDB} is a general purpose, platform-independent debugger that
22784 can be used to debug mixed-language programs compiled with @command{gcc},
22785 and in particular is capable of debugging Ada programs compiled with
22786 GNAT. The latest versions of @code{GDB} are Ada-aware and can handle
22787 complex Ada data structures.
22788
22789 @xref{Top,, Debugging with GDB, gdb, Debugging with GDB},
22790 @ifset vms
22791 located in the GNU:[DOCS] directory,
22792 @end ifset
22793 for full details on the usage of @code{GDB}, including a section on
22794 its usage on programs. This manual should be consulted for full
22795 details. The section that follows is a brief introduction to the
22796 philosophy and use of @code{GDB}.
22797
22798 When GNAT programs are compiled, the compiler optionally writes debugging
22799 information into the generated object file, including information on
22800 line numbers, and on declared types and variables. This information is
22801 separate from the generated code. It makes the object files considerably
22802 larger, but it does not add to the size of the actual executable that
22803 will be loaded into memory, and has no impact on run-time performance. The
22804 generation of debug information is triggered by the use of the
22805 ^-g^/DEBUG^ switch in the @command{gcc} or @command{gnatmake} command
22806 used to carry out the compilations. It is important to emphasize that
22807 the use of these options does not change the generated code.
22808
22809 The debugging information is written in standard system formats that
22810 are used by many tools, including debuggers and profilers. The format
22811 of the information is typically designed to describe C types and
22812 semantics, but GNAT implements a translation scheme which allows full
22813 details about Ada types and variables to be encoded into these
22814 standard C formats. Details of this encoding scheme may be found in
22815 the file exp_dbug.ads in the GNAT source distribution. However, the
22816 details of this encoding are, in general, of no interest to a user,
22817 since @code{GDB} automatically performs the necessary decoding.
22818
22819 When a program is bound and linked, the debugging information is
22820 collected from the object files, and stored in the executable image of
22821 the program. Again, this process significantly increases the size of
22822 the generated executable file, but it does not increase the size of
22823 the executable program itself. Furthermore, if this program is run in
22824 the normal manner, it runs exactly as if the debug information were
22825 not present, and takes no more actual memory.
22826
22827 However, if the program is run under control of @code{GDB}, the
22828 debugger is activated. The image of the program is loaded, at which
22829 point it is ready to run. If a run command is given, then the program
22830 will run exactly as it would have if @code{GDB} were not present. This
22831 is a crucial part of the @code{GDB} design philosophy. @code{GDB} is
22832 entirely non-intrusive until a breakpoint is encountered. If no
22833 breakpoint is ever hit, the program will run exactly as it would if no
22834 debugger were present. When a breakpoint is hit, @code{GDB} accesses
22835 the debugging information and can respond to user commands to inspect
22836 variables, and more generally to report on the state of execution.
22837
22838 @c **************
22839 @node Running GDB
22840 @section Running GDB
22841
22842 @noindent
22843 This section describes how to initiate the debugger.
22844 @c The above sentence is really just filler, but it was otherwise
22845 @c clumsy to get the first paragraph nonindented given the conditional
22846 @c nature of the description
22847
22848 @ifclear vms
22849 The debugger can be launched from a @code{GPS} menu or
22850 directly from the command line. The description below covers the latter use.
22851 All the commands shown can be used in the @code{GPS} debug console window,
22852 but there are usually more GUI-based ways to achieve the same effect.
22853 @end ifclear
22854
22855 The command to run @code{GDB} is
22856
22857 @smallexample
22858 $ ^gdb program^GDB PROGRAM^
22859 @end smallexample
22860
22861 @noindent
22862 where @code{^program^PROGRAM^} is the name of the executable file. This
22863 activates the debugger and results in a prompt for debugger commands.
22864 The simplest command is simply @code{run}, which causes the program to run
22865 exactly as if the debugger were not present. The following section
22866 describes some of the additional commands that can be given to @code{GDB}.
22867
22868 @c *******************************
22869 @node Introduction to GDB Commands
22870 @section Introduction to GDB Commands
22871
22872 @noindent
22873 @code{GDB} contains a large repertoire of commands. @xref{Top,,
22874 Debugging with GDB, gdb, Debugging with GDB},
22875 @ifset vms
22876 located in the GNU:[DOCS] directory,
22877 @end ifset
22878 for extensive documentation on the use
22879 of these commands, together with examples of their use. Furthermore,
22880 the command @command{help} invoked from within GDB activates a simple help
22881 facility which summarizes the available commands and their options.
22882 In this section we summarize a few of the most commonly
22883 used commands to give an idea of what @code{GDB} is about. You should create
22884 a simple program with debugging information and experiment with the use of
22885 these @code{GDB} commands on the program as you read through the
22886 following section.
22887
22888 @table @code
22889 @item set args @var{arguments}
22890 The @var{arguments} list above is a list of arguments to be passed to
22891 the program on a subsequent run command, just as though the arguments
22892 had been entered on a normal invocation of the program. The @code{set args}
22893 command is not needed if the program does not require arguments.
22894
22895 @item run
22896 The @code{run} command causes execution of the program to start from
22897 the beginning. If the program is already running, that is to say if
22898 you are currently positioned at a breakpoint, then a prompt will ask
22899 for confirmation that you want to abandon the current execution and
22900 restart.
22901
22902 @item breakpoint @var{location}
22903 The breakpoint command sets a breakpoint, that is to say a point at which
22904 execution will halt and @code{GDB} will await further
22905 commands. @var{location} is
22906 either a line number within a file, given in the format @code{file:linenumber},
22907 or it is the name of a subprogram. If you request that a breakpoint be set on
22908 a subprogram that is overloaded, a prompt will ask you to specify on which of
22909 those subprograms you want to breakpoint. You can also
22910 specify that all of them should be breakpointed. If the program is run
22911 and execution encounters the breakpoint, then the program
22912 stops and @code{GDB} signals that the breakpoint was encountered by
22913 printing the line of code before which the program is halted.
22914
22915 @item breakpoint exception @var{name}
22916 A special form of the breakpoint command which breakpoints whenever
22917 exception @var{name} is raised.
22918 If @var{name} is omitted,
22919 then a breakpoint will occur when any exception is raised.
22920
22921 @item print @var{expression}
22922 This will print the value of the given expression. Most simple
22923 Ada expression formats are properly handled by @code{GDB}, so the expression
22924 can contain function calls, variables, operators, and attribute references.
22925
22926 @item continue
22927 Continues execution following a breakpoint, until the next breakpoint or the
22928 termination of the program.
22929
22930 @item step
22931 Executes a single line after a breakpoint. If the next statement
22932 is a subprogram call, execution continues into (the first statement of)
22933 the called subprogram.
22934
22935 @item next
22936 Executes a single line. If this line is a subprogram call, executes and
22937 returns from the call.
22938
22939 @item list
22940 Lists a few lines around the current source location. In practice, it
22941 is usually more convenient to have a separate edit window open with the
22942 relevant source file displayed. Successive applications of this command
22943 print subsequent lines. The command can be given an argument which is a
22944 line number, in which case it displays a few lines around the specified one.
22945
22946 @item backtrace
22947 Displays a backtrace of the call chain. This command is typically
22948 used after a breakpoint has occurred, to examine the sequence of calls that
22949 leads to the current breakpoint. The display includes one line for each
22950 activation record (frame) corresponding to an active subprogram.
22951
22952 @item up
22953 At a breakpoint, @code{GDB} can display the values of variables local
22954 to the current frame. The command @code{up} can be used to
22955 examine the contents of other active frames, by moving the focus up
22956 the stack, that is to say from callee to caller, one frame at a time.
22957
22958 @item down
22959 Moves the focus of @code{GDB} down from the frame currently being
22960 examined to the frame of its callee (the reverse of the previous command),
22961
22962 @item frame @var{n}
22963 Inspect the frame with the given number. The value 0 denotes the frame
22964 of the current breakpoint, that is to say the top of the call stack.
22965
22966 @end table
22967
22968 @noindent
22969 The above list is a very short introduction to the commands that
22970 @code{GDB} provides. Important additional capabilities, including conditional
22971 breakpoints, the ability to execute command sequences on a breakpoint,
22972 the ability to debug at the machine instruction level and many other
22973 features are described in detail in @ref{Top,, Debugging with GDB, gdb,
22974 Debugging with GDB}. Note that most commands can be abbreviated
22975 (for example, c for continue, bt for backtrace).
22976
22977 @node Using Ada Expressions
22978 @section Using Ada Expressions
22979 @cindex Ada expressions
22980
22981 @noindent
22982 @code{GDB} supports a fairly large subset of Ada expression syntax, with some
22983 extensions. The philosophy behind the design of this subset is
22984
22985 @itemize @bullet
22986 @item
22987 That @code{GDB} should provide basic literals and access to operations for
22988 arithmetic, dereferencing, field selection, indexing, and subprogram calls,
22989 leaving more sophisticated computations to subprograms written into the
22990 program (which therefore may be called from @code{GDB}).
22991
22992 @item
22993 That type safety and strict adherence to Ada language restrictions
22994 are not particularly important to the @code{GDB} user.
22995
22996 @item
22997 That brevity is important to the @code{GDB} user.
22998 @end itemize
22999
23000 @noindent
23001 Thus, for brevity, the debugger acts as if there were
23002 implicit @code{with} and @code{use} clauses in effect for all user-written
23003 packages, thus making it unnecessary to fully qualify most names with
23004 their packages, regardless of context. Where this causes ambiguity,
23005 @code{GDB} asks the user's intent.
23006
23007 For details on the supported Ada syntax, see @ref{Top,, Debugging with
23008 GDB, gdb, Debugging with GDB}.
23009
23010 @node Calling User-Defined Subprograms
23011 @section Calling User-Defined Subprograms
23012
23013 @noindent
23014 An important capability of @code{GDB} is the ability to call user-defined
23015 subprograms while debugging. This is achieved simply by entering
23016 a subprogram call statement in the form:
23017
23018 @smallexample
23019 call subprogram-name (parameters)
23020 @end smallexample
23021
23022 @noindent
23023 The keyword @code{call} can be omitted in the normal case where the
23024 @code{subprogram-name} does not coincide with any of the predefined
23025 @code{GDB} commands.
23026
23027 The effect is to invoke the given subprogram, passing it the
23028 list of parameters that is supplied. The parameters can be expressions and
23029 can include variables from the program being debugged. The
23030 subprogram must be defined
23031 at the library level within your program, and @code{GDB} will call the
23032 subprogram within the environment of your program execution (which
23033 means that the subprogram is free to access or even modify variables
23034 within your program).
23035
23036 The most important use of this facility is in allowing the inclusion of
23037 debugging routines that are tailored to particular data structures
23038 in your program. Such debugging routines can be written to provide a suitably
23039 high-level description of an abstract type, rather than a low-level dump
23040 of its physical layout. After all, the standard
23041 @code{GDB print} command only knows the physical layout of your
23042 types, not their abstract meaning. Debugging routines can provide information
23043 at the desired semantic level and are thus enormously useful.
23044
23045 For example, when debugging GNAT itself, it is crucial to have access to
23046 the contents of the tree nodes used to represent the program internally.
23047 But tree nodes are represented simply by an integer value (which in turn
23048 is an index into a table of nodes).
23049 Using the @code{print} command on a tree node would simply print this integer
23050 value, which is not very useful. But the PN routine (defined in file
23051 treepr.adb in the GNAT sources) takes a tree node as input, and displays
23052 a useful high level representation of the tree node, which includes the
23053 syntactic category of the node, its position in the source, the integers
23054 that denote descendant nodes and parent node, as well as varied
23055 semantic information. To study this example in more detail, you might want to
23056 look at the body of the PN procedure in the stated file.
23057
23058 @node Using the Next Command in a Function
23059 @section Using the Next Command in a Function
23060
23061 @noindent
23062 When you use the @code{next} command in a function, the current source
23063 location will advance to the next statement as usual. A special case
23064 arises in the case of a @code{return} statement.
23065
23066 Part of the code for a return statement is the ``epilog'' of the function.
23067 This is the code that returns to the caller. There is only one copy of
23068 this epilog code, and it is typically associated with the last return
23069 statement in the function if there is more than one return. In some
23070 implementations, this epilog is associated with the first statement
23071 of the function.
23072
23073 The result is that if you use the @code{next} command from a return
23074 statement that is not the last return statement of the function you
23075 may see a strange apparent jump to the last return statement or to
23076 the start of the function. You should simply ignore this odd jump.
23077 The value returned is always that from the first return statement
23078 that was stepped through.
23079
23080 @node Ada Exceptions
23081 @section Breaking on Ada Exceptions
23082 @cindex Exceptions
23083
23084 @noindent
23085 You can set breakpoints that trip when your program raises
23086 selected exceptions.
23087
23088 @table @code
23089 @item break exception
23090 Set a breakpoint that trips whenever (any task in the) program raises
23091 any exception.
23092
23093 @item break exception @var{name}
23094 Set a breakpoint that trips whenever (any task in the) program raises
23095 the exception @var{name}.
23096
23097 @item break exception unhandled
23098 Set a breakpoint that trips whenever (any task in the) program raises an
23099 exception for which there is no handler.
23100
23101 @item info exceptions
23102 @itemx info exceptions @var{regexp}
23103 The @code{info exceptions} command permits the user to examine all defined
23104 exceptions within Ada programs. With a regular expression, @var{regexp}, as
23105 argument, prints out only those exceptions whose name matches @var{regexp}.
23106 @end table
23107
23108 @node Ada Tasks
23109 @section Ada Tasks
23110 @cindex Tasks
23111
23112 @noindent
23113 @code{GDB} allows the following task-related commands:
23114
23115 @table @code
23116 @item info tasks
23117 This command shows a list of current Ada tasks, as in the following example:
23118
23119 @smallexample
23120 @iftex
23121 @leftskip=0cm
23122 @end iftex
23123 (gdb) info tasks
23124 ID TID P-ID Thread Pri State Name
23125 1 8088000 0 807e000 15 Child Activation Wait main_task
23126 2 80a4000 1 80ae000 15 Accept/Select Wait b
23127 3 809a800 1 80a4800 15 Child Activation Wait a
23128 * 4 80ae800 3 80b8000 15 Running c
23129 @end smallexample
23130
23131 @noindent
23132 In this listing, the asterisk before the first task indicates it to be the
23133 currently running task. The first column lists the task ID that is used
23134 to refer to tasks in the following commands.
23135
23136 @item break @var{linespec} task @var{taskid}
23137 @itemx break @var{linespec} task @var{taskid} if @dots{}
23138 @cindex Breakpoints and tasks
23139 These commands are like the @code{break @dots{} thread @dots{}}.
23140 @var{linespec} specifies source lines.
23141
23142 Use the qualifier @samp{task @var{taskid}} with a breakpoint command
23143 to specify that you only want @code{GDB} to stop the program when a
23144 particular Ada task reaches this breakpoint. @var{taskid} is one of the
23145 numeric task identifiers assigned by @code{GDB}, shown in the first
23146 column of the @samp{info tasks} display.
23147
23148 If you do not specify @samp{task @var{taskid}} when you set a
23149 breakpoint, the breakpoint applies to @emph{all} tasks of your
23150 program.
23151
23152 You can use the @code{task} qualifier on conditional breakpoints as
23153 well; in this case, place @samp{task @var{taskid}} before the
23154 breakpoint condition (before the @code{if}).
23155
23156 @item task @var{taskno}
23157 @cindex Task switching
23158
23159 This command allows to switch to the task referred by @var{taskno}. In
23160 particular, This allows to browse the backtrace of the specified
23161 task. It is advised to switch back to the original task before
23162 continuing execution otherwise the scheduling of the program may be
23163 perturbed.
23164 @end table
23165
23166 @noindent
23167 For more detailed information on the tasking support,
23168 see @ref{Top,, Debugging with GDB, gdb, Debugging with GDB}.
23169
23170 @node Debugging Generic Units
23171 @section Debugging Generic Units
23172 @cindex Debugging Generic Units
23173 @cindex Generics
23174
23175 @noindent
23176 GNAT always uses code expansion for generic instantiation. This means that
23177 each time an instantiation occurs, a complete copy of the original code is
23178 made, with appropriate substitutions of formals by actuals.
23179
23180 It is not possible to refer to the original generic entities in
23181 @code{GDB}, but it is always possible to debug a particular instance of
23182 a generic, by using the appropriate expanded names. For example, if we have
23183
23184 @smallexample @c ada
23185 @group
23186 @cartouche
23187 procedure g is
23188
23189 generic package k is
23190 procedure kp (v1 : in out integer);
23191 end k;
23192
23193 package body k is
23194 procedure kp (v1 : in out integer) is
23195 begin
23196 v1 := v1 + 1;
23197 end kp;
23198 end k;
23199
23200 package k1 is new k;
23201 package k2 is new k;
23202
23203 var : integer := 1;
23204
23205 begin
23206 k1.kp (var);
23207 k2.kp (var);
23208 k1.kp (var);
23209 k2.kp (var);
23210 end;
23211 @end cartouche
23212 @end group
23213 @end smallexample
23214
23215 @noindent
23216 Then to break on a call to procedure kp in the k2 instance, simply
23217 use the command:
23218
23219 @smallexample
23220 (gdb) break g.k2.kp
23221 @end smallexample
23222
23223 @noindent
23224 When the breakpoint occurs, you can step through the code of the
23225 instance in the normal manner and examine the values of local variables, as for
23226 other units.
23227
23228 @node GNAT Abnormal Termination or Failure to Terminate
23229 @section GNAT Abnormal Termination or Failure to Terminate
23230 @cindex GNAT Abnormal Termination or Failure to Terminate
23231
23232 @noindent
23233 When presented with programs that contain serious errors in syntax
23234 or semantics,
23235 GNAT may on rare occasions experience problems in operation, such
23236 as aborting with a
23237 segmentation fault or illegal memory access, raising an internal
23238 exception, terminating abnormally, or failing to terminate at all.
23239 In such cases, you can activate
23240 various features of GNAT that can help you pinpoint the construct in your
23241 program that is the likely source of the problem.
23242
23243 The following strategies are presented in increasing order of
23244 difficulty, corresponding to your experience in using GNAT and your
23245 familiarity with compiler internals.
23246
23247 @enumerate
23248 @item
23249 Run @command{gcc} with the @option{-gnatf}. This first
23250 switch causes all errors on a given line to be reported. In its absence,
23251 only the first error on a line is displayed.
23252
23253 The @option{-gnatdO} switch causes errors to be displayed as soon as they
23254 are encountered, rather than after compilation is terminated. If GNAT
23255 terminates prematurely or goes into an infinite loop, the last error
23256 message displayed may help to pinpoint the culprit.
23257
23258 @item
23259 Run @command{gcc} with the @option{^-v (verbose)^/VERBOSE^} switch. In this
23260 mode, @command{gcc} produces ongoing information about the progress of the
23261 compilation and provides the name of each procedure as code is
23262 generated. This switch allows you to find which Ada procedure was being
23263 compiled when it encountered a code generation problem.
23264
23265 @item
23266 @cindex @option{-gnatdc} switch
23267 Run @command{gcc} with the @option{-gnatdc} switch. This is a GNAT specific
23268 switch that does for the front-end what @option{^-v^VERBOSE^} does
23269 for the back end. The system prints the name of each unit,
23270 either a compilation unit or nested unit, as it is being analyzed.
23271 @item
23272 Finally, you can start
23273 @code{gdb} directly on the @code{gnat1} executable. @code{gnat1} is the
23274 front-end of GNAT, and can be run independently (normally it is just
23275 called from @command{gcc}). You can use @code{gdb} on @code{gnat1} as you
23276 would on a C program (but @pxref{The GNAT Debugger GDB} for caveats). The
23277 @code{where} command is the first line of attack; the variable
23278 @code{lineno} (seen by @code{print lineno}), used by the second phase of
23279 @code{gnat1} and by the @command{gcc} backend, indicates the source line at
23280 which the execution stopped, and @code{input_file name} indicates the name of
23281 the source file.
23282 @end enumerate
23283
23284 @node Naming Conventions for GNAT Source Files
23285 @section Naming Conventions for GNAT Source Files
23286
23287 @noindent
23288 In order to examine the workings of the GNAT system, the following
23289 brief description of its organization may be helpful:
23290
23291 @itemize @bullet
23292 @item
23293 Files with prefix @file{^sc^SC^} contain the lexical scanner.
23294
23295 @item
23296 All files prefixed with @file{^par^PAR^} are components of the parser. The
23297 numbers correspond to chapters of the Ada Reference Manual. For example,
23298 parsing of select statements can be found in @file{par-ch9.adb}.
23299
23300 @item
23301 All files prefixed with @file{^sem^SEM^} perform semantic analysis. The
23302 numbers correspond to chapters of the Ada standard. For example, all
23303 issues involving context clauses can be found in @file{sem_ch10.adb}. In
23304 addition, some features of the language require sufficient special processing
23305 to justify their own semantic files: sem_aggr for aggregates, sem_disp for
23306 dynamic dispatching, etc.
23307
23308 @item
23309 All files prefixed with @file{^exp^EXP^} perform normalization and
23310 expansion of the intermediate representation (abstract syntax tree, or AST).
23311 these files use the same numbering scheme as the parser and semantics files.
23312 For example, the construction of record initialization procedures is done in
23313 @file{exp_ch3.adb}.
23314
23315 @item
23316 The files prefixed with @file{^bind^BIND^} implement the binder, which
23317 verifies the consistency of the compilation, determines an order of
23318 elaboration, and generates the bind file.
23319
23320 @item
23321 The files @file{atree.ads} and @file{atree.adb} detail the low-level
23322 data structures used by the front-end.
23323
23324 @item
23325 The files @file{sinfo.ads} and @file{sinfo.adb} detail the structure of
23326 the abstract syntax tree as produced by the parser.
23327
23328 @item
23329 The files @file{einfo.ads} and @file{einfo.adb} detail the attributes of
23330 all entities, computed during semantic analysis.
23331
23332 @item
23333 Library management issues are dealt with in files with prefix
23334 @file{^lib^LIB^}.
23335
23336 @item
23337 @findex Ada
23338 @cindex Annex A
23339 Ada files with the prefix @file{^a-^A-^} are children of @code{Ada}, as
23340 defined in Annex A.
23341
23342 @item
23343 @findex Interfaces
23344 @cindex Annex B
23345 Files with prefix @file{^i-^I-^} are children of @code{Interfaces}, as
23346 defined in Annex B.
23347
23348 @item
23349 @findex System
23350 Files with prefix @file{^s-^S-^} are children of @code{System}. This includes
23351 both language-defined children and GNAT run-time routines.
23352
23353 @item
23354 @findex GNAT
23355 Files with prefix @file{^g-^G-^} are children of @code{GNAT}. These are useful
23356 general-purpose packages, fully documented in their specs. All
23357 the other @file{.c} files are modifications of common @command{gcc} files.
23358 @end itemize
23359
23360 @node Getting Internal Debugging Information
23361 @section Getting Internal Debugging Information
23362
23363 @noindent
23364 Most compilers have internal debugging switches and modes. GNAT
23365 does also, except GNAT internal debugging switches and modes are not
23366 secret. A summary and full description of all the compiler and binder
23367 debug flags are in the file @file{debug.adb}. You must obtain the
23368 sources of the compiler to see the full detailed effects of these flags.
23369
23370 The switches that print the source of the program (reconstructed from
23371 the internal tree) are of general interest for user programs, as are the
23372 options to print
23373 the full internal tree, and the entity table (the symbol table
23374 information). The reconstructed source provides a readable version of the
23375 program after the front-end has completed analysis and expansion,
23376 and is useful when studying the performance of specific constructs.
23377 For example, constraint checks are indicated, complex aggregates
23378 are replaced with loops and assignments, and tasking primitives
23379 are replaced with run-time calls.
23380
23381 @node Stack Traceback
23382 @section Stack Traceback
23383 @cindex traceback
23384 @cindex stack traceback
23385 @cindex stack unwinding
23386
23387 @noindent
23388 Traceback is a mechanism to display the sequence of subprogram calls that
23389 leads to a specified execution point in a program. Often (but not always)
23390 the execution point is an instruction at which an exception has been raised.
23391 This mechanism is also known as @i{stack unwinding} because it obtains
23392 its information by scanning the run-time stack and recovering the activation
23393 records of all active subprograms. Stack unwinding is one of the most
23394 important tools for program debugging.
23395
23396 The first entry stored in traceback corresponds to the deepest calling level,
23397 that is to say the subprogram currently executing the instruction
23398 from which we want to obtain the traceback.
23399
23400 Note that there is no runtime performance penalty when stack traceback
23401 is enabled, and no exception is raised during program execution.
23402
23403 @menu
23404 * Non-Symbolic Traceback::
23405 * Symbolic Traceback::
23406 @end menu
23407
23408 @node Non-Symbolic Traceback
23409 @subsection Non-Symbolic Traceback
23410 @cindex traceback, non-symbolic
23411
23412 @noindent
23413 Note: this feature is not supported on all platforms. See
23414 @file{GNAT.Traceback spec in g-traceb.ads} for a complete list of supported
23415 platforms.
23416
23417 @menu
23418 * Tracebacks From an Unhandled Exception::
23419 * Tracebacks From Exception Occurrences (non-symbolic)::
23420 * Tracebacks From Anywhere in a Program (non-symbolic)::
23421 @end menu
23422
23423 @node Tracebacks From an Unhandled Exception
23424 @subsubsection Tracebacks From an Unhandled Exception
23425
23426 @noindent
23427 A runtime non-symbolic traceback is a list of addresses of call instructions.
23428 To enable this feature you must use the @option{-E}
23429 @code{gnatbind}'s option. With this option a stack traceback is stored as part
23430 of exception information. You can retrieve this information using the
23431 @code{addr2line} tool.
23432
23433 Here is a simple example:
23434
23435 @smallexample @c ada
23436 @cartouche
23437 procedure STB is
23438
23439 procedure P1 is
23440 begin
23441 raise Constraint_Error;
23442 end P1;
23443
23444 procedure P2 is
23445 begin
23446 P1;
23447 end P2;
23448
23449 begin
23450 P2;
23451 end STB;
23452 @end cartouche
23453 @end smallexample
23454
23455 @smallexample
23456 $ gnatmake stb -bargs -E
23457 $ stb
23458
23459 Execution terminated by unhandled exception
23460 Exception name: CONSTRAINT_ERROR
23461 Message: stb.adb:5
23462 Call stack traceback locations:
23463 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
23464 @end smallexample
23465
23466 @noindent
23467 As we see the traceback lists a sequence of addresses for the unhandled
23468 exception @code{CONSTRAINT_ERROR} raised in procedure P1. It is easy to
23469 guess that this exception come from procedure P1. To translate these
23470 addresses into the source lines where the calls appear, the
23471 @code{addr2line} tool, described below, is invaluable. The use of this tool
23472 requires the program to be compiled with debug information.
23473
23474 @smallexample
23475 $ gnatmake -g stb -bargs -E
23476 $ stb
23477
23478 Execution terminated by unhandled exception
23479 Exception name: CONSTRAINT_ERROR
23480 Message: stb.adb:5
23481 Call stack traceback locations:
23482 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
23483
23484 $ addr2line --exe=stb 0x401373 0x40138b 0x40139c 0x401335 0x4011c4
23485 0x4011f1 0x77e892a4
23486
23487 00401373 at d:/stb/stb.adb:5
23488 0040138B at d:/stb/stb.adb:10
23489 0040139C at d:/stb/stb.adb:14
23490 00401335 at d:/stb/b~stb.adb:104
23491 004011C4 at /build/@dots{}/crt1.c:200
23492 004011F1 at /build/@dots{}/crt1.c:222
23493 77E892A4 in ?? at ??:0
23494 @end smallexample
23495
23496 @noindent
23497 The @code{addr2line} tool has several other useful options:
23498
23499 @table @code
23500 @item --functions
23501 to get the function name corresponding to any location
23502
23503 @item --demangle=gnat
23504 to use the gnat decoding mode for the function names. Note that
23505 for binutils version 2.9.x the option is simply @option{--demangle}.
23506 @end table
23507
23508 @smallexample
23509 $ addr2line --exe=stb --functions --demangle=gnat 0x401373 0x40138b
23510 0x40139c 0x401335 0x4011c4 0x4011f1
23511
23512 00401373 in stb.p1 at d:/stb/stb.adb:5
23513 0040138B in stb.p2 at d:/stb/stb.adb:10
23514 0040139C in stb at d:/stb/stb.adb:14
23515 00401335 in main at d:/stb/b~stb.adb:104
23516 004011C4 in <__mingw_CRTStartup> at /build/@dots{}/crt1.c:200
23517 004011F1 in <mainCRTStartup> at /build/@dots{}/crt1.c:222
23518 @end smallexample
23519
23520 @noindent
23521 From this traceback we can see that the exception was raised in
23522 @file{stb.adb} at line 5, which was reached from a procedure call in
23523 @file{stb.adb} at line 10, and so on. The @file{b~std.adb} is the binder file,
23524 which contains the call to the main program.
23525 @xref{Running gnatbind}. The remaining entries are assorted runtime routines,
23526 and the output will vary from platform to platform.
23527
23528 It is also possible to use @code{GDB} with these traceback addresses to debug
23529 the program. For example, we can break at a given code location, as reported
23530 in the stack traceback:
23531
23532 @smallexample
23533 $ gdb -nw stb
23534 @ifclear vms
23535 @noindent
23536 Furthermore, this feature is not implemented inside Windows DLL. Only
23537 the non-symbolic traceback is reported in this case.
23538 @end ifclear
23539
23540 (gdb) break *0x401373
23541 Breakpoint 1 at 0x401373: file stb.adb, line 5.
23542 @end smallexample
23543
23544 @noindent
23545 It is important to note that the stack traceback addresses
23546 do not change when debug information is included. This is particularly useful
23547 because it makes it possible to release software without debug information (to
23548 minimize object size), get a field report that includes a stack traceback
23549 whenever an internal bug occurs, and then be able to retrieve the sequence
23550 of calls with the same program compiled with debug information.
23551
23552 @node Tracebacks From Exception Occurrences (non-symbolic)
23553 @subsubsection Tracebacks From Exception Occurrences
23554
23555 @noindent
23556 Non-symbolic tracebacks are obtained by using the @option{-E} binder argument.
23557 The stack traceback is attached to the exception information string, and can
23558 be retrieved in an exception handler within the Ada program, by means of the
23559 Ada facilities defined in @code{Ada.Exceptions}. Here is a simple example:
23560
23561 @smallexample @c ada
23562 with Ada.Text_IO;
23563 with Ada.Exceptions;
23564
23565 procedure STB is
23566
23567 use Ada;
23568 use Ada.Exceptions;
23569
23570 procedure P1 is
23571 K : Positive := 1;
23572 begin
23573 K := K - 1;
23574 exception
23575 when E : others =>
23576 Text_IO.Put_Line (Exception_Information (E));
23577 end P1;
23578
23579 procedure P2 is
23580 begin
23581 P1;
23582 end P2;
23583
23584 begin
23585 P2;
23586 end STB;
23587 @end smallexample
23588
23589 @noindent
23590 This program will output:
23591
23592 @smallexample
23593 $ stb
23594
23595 Exception name: CONSTRAINT_ERROR
23596 Message: stb.adb:12
23597 Call stack traceback locations:
23598 0x4015e4 0x401633 0x401644 0x401461 0x4011c4 0x4011f1 0x77e892a4
23599 @end smallexample
23600
23601 @node Tracebacks From Anywhere in a Program (non-symbolic)
23602 @subsubsection Tracebacks From Anywhere in a Program
23603
23604 @noindent
23605 It is also possible to retrieve a stack traceback from anywhere in a
23606 program. For this you need to
23607 use the @code{GNAT.Traceback} API. This package includes a procedure called
23608 @code{Call_Chain} that computes a complete stack traceback, as well as useful
23609 display procedures described below. It is not necessary to use the
23610 @option{-E gnatbind} option in this case, because the stack traceback mechanism
23611 is invoked explicitly.
23612
23613 @noindent
23614 In the following example we compute a traceback at a specific location in
23615 the program, and we display it using @code{GNAT.Debug_Utilities.Image} to
23616 convert addresses to strings:
23617
23618 @smallexample @c ada
23619 with Ada.Text_IO;
23620 with GNAT.Traceback;
23621 with GNAT.Debug_Utilities;
23622
23623 procedure STB is
23624
23625 use Ada;
23626 use GNAT;
23627 use GNAT.Traceback;
23628
23629 procedure P1 is
23630 TB : Tracebacks_Array (1 .. 10);
23631 -- We are asking for a maximum of 10 stack frames.
23632 Len : Natural;
23633 -- Len will receive the actual number of stack frames returned.
23634 begin
23635 Call_Chain (TB, Len);
23636
23637 Text_IO.Put ("In STB.P1 : ");
23638
23639 for K in 1 .. Len loop
23640 Text_IO.Put (Debug_Utilities.Image (TB (K)));
23641 Text_IO.Put (' ');
23642 end loop;
23643
23644 Text_IO.New_Line;
23645 end P1;
23646
23647 procedure P2 is
23648 begin
23649 P1;
23650 end P2;
23651
23652 begin
23653 P2;
23654 end STB;
23655 @end smallexample
23656
23657 @smallexample
23658 $ gnatmake -g stb
23659 $ stb
23660
23661 In STB.P1 : 16#0040_F1E4# 16#0040_14F2# 16#0040_170B# 16#0040_171C#
23662 16#0040_1461# 16#0040_11C4# 16#0040_11F1# 16#77E8_92A4#
23663 @end smallexample
23664
23665 @noindent
23666 You can then get further information by invoking the @code{addr2line}
23667 tool as described earlier (note that the hexadecimal addresses
23668 need to be specified in C format, with a leading ``0x'').
23669
23670 @node Symbolic Traceback
23671 @subsection Symbolic Traceback
23672 @cindex traceback, symbolic
23673
23674 @noindent
23675 A symbolic traceback is a stack traceback in which procedure names are
23676 associated with each code location.
23677
23678 @noindent
23679 Note that this feature is not supported on all platforms. See
23680 @file{GNAT.Traceback.Symbolic spec in g-trasym.ads} for a complete
23681 list of currently supported platforms.
23682
23683 @noindent
23684 Note that the symbolic traceback requires that the program be compiled
23685 with debug information. If it is not compiled with debug information
23686 only the non-symbolic information will be valid.
23687
23688 @menu
23689 * Tracebacks From Exception Occurrences (symbolic)::
23690 * Tracebacks From Anywhere in a Program (symbolic)::
23691 @end menu
23692
23693 @node Tracebacks From Exception Occurrences (symbolic)
23694 @subsubsection Tracebacks From Exception Occurrences
23695
23696 @smallexample @c ada
23697 with Ada.Text_IO;
23698 with GNAT.Traceback.Symbolic;
23699
23700 procedure STB is
23701
23702 procedure P1 is
23703 begin
23704 raise Constraint_Error;
23705 end P1;
23706
23707 procedure P2 is
23708 begin
23709 P1;
23710 end P2;
23711
23712 procedure P3 is
23713 begin
23714 P2;
23715 end P3;
23716
23717 begin
23718 P3;
23719 exception
23720 when E : others =>
23721 Ada.Text_IO.Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
23722 end STB;
23723 @end smallexample
23724
23725 @smallexample
23726 $ gnatmake -g .\stb -bargs -E -largs -lgnat -laddr2line -lintl
23727 $ stb
23728
23729 0040149F in stb.p1 at stb.adb:8
23730 004014B7 in stb.p2 at stb.adb:13
23731 004014CF in stb.p3 at stb.adb:18
23732 004015DD in ada.stb at stb.adb:22
23733 00401461 in main at b~stb.adb:168
23734 004011C4 in __mingw_CRTStartup at crt1.c:200
23735 004011F1 in mainCRTStartup at crt1.c:222
23736 77E892A4 in ?? at ??:0
23737 @end smallexample
23738
23739 @noindent
23740 In the above example the ``.\'' syntax in the @command{gnatmake} command
23741 is currently required by @command{addr2line} for files that are in
23742 the current working directory.
23743 Moreover, the exact sequence of linker options may vary from platform
23744 to platform.
23745 The above @option{-largs} section is for Windows platforms. By contrast,
23746 under Unix there is no need for the @option{-largs} section.
23747 Differences across platforms are due to details of linker implementation.
23748
23749 @node Tracebacks From Anywhere in a Program (symbolic)
23750 @subsubsection Tracebacks From Anywhere in a Program
23751
23752 @noindent
23753 It is possible to get a symbolic stack traceback
23754 from anywhere in a program, just as for non-symbolic tracebacks.
23755 The first step is to obtain a non-symbolic
23756 traceback, and then call @code{Symbolic_Traceback} to compute the symbolic
23757 information. Here is an example:
23758
23759 @smallexample @c ada
23760 with Ada.Text_IO;
23761 with GNAT.Traceback;
23762 with GNAT.Traceback.Symbolic;
23763
23764 procedure STB is
23765
23766 use Ada;
23767 use GNAT.Traceback;
23768 use GNAT.Traceback.Symbolic;
23769
23770 procedure P1 is
23771 TB : Tracebacks_Array (1 .. 10);
23772 -- We are asking for a maximum of 10 stack frames.
23773 Len : Natural;
23774 -- Len will receive the actual number of stack frames returned.
23775 begin
23776 Call_Chain (TB, Len);
23777 Text_IO.Put_Line (Symbolic_Traceback (TB (1 .. Len)));
23778 end P1;
23779
23780 procedure P2 is
23781 begin
23782 P1;
23783 end P2;
23784
23785 begin
23786 P2;
23787 end STB;
23788 @end smallexample
23789
23790 @c ******************************
23791 @ifset vms
23792 @node Compatibility with HP Ada
23793 @chapter Compatibility with HP Ada
23794 @cindex Compatibility
23795
23796 @noindent
23797 @cindex DEC Ada
23798 @cindex HP Ada
23799 @cindex Compatibility between GNAT and HP Ada
23800 This chapter compares HP Ada (formerly known as ``DEC Ada'')
23801 for OpenVMS Alpha and GNAT for OpenVMS for Alpha and for I64.
23802 GNAT is highly compatible
23803 with HP Ada, and it should generally be straightforward to port code
23804 from the HP Ada environment to GNAT. However, there are a few language
23805 and implementation differences of which the user must be aware. These
23806 differences are discussed in this chapter. In
23807 addition, the operating environment and command structure for the
23808 compiler are different, and these differences are also discussed.
23809
23810 For further details on these and other compatibility issues,
23811 see Appendix E of the HP publication
23812 @cite{HP Ada, Technical Overview and Comparison on HP Platforms}.
23813
23814 Except where otherwise indicated, the description of GNAT for OpenVMS
23815 applies to both the Alpha and I64 platforms.
23816
23817 For information on porting Ada code from GNAT on Alpha OpenVMS to GNAT on
23818 I64 OpenVMS, see @ref{Transitioning to 64-Bit GNAT for OpenVMS}.
23819
23820 The discussion in this chapter addresses specifically the implementation
23821 of Ada 83 for HP OpenVMS Alpha Systems. In cases where the implementation
23822 of HP Ada differs between OpenVMS Alpha Systems and OpenVMS VAX Systems,
23823 GNAT always follows the Alpha implementation.
23824
23825 For GNAT running on other than VMS systems, all the HP Ada 83 pragmas and
23826 attributes are recognized, although only a subset of them can sensibly
23827 be implemented. The description of pragmas in
23828 @xref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference Manual}
23829 indicates whether or not they are applicable to non-VMS systems.
23830
23831 @menu
23832 * Ada Language Compatibility::
23833 * Differences in the Definition of Package System::
23834 * Language-Related Features::
23835 * The Package STANDARD::
23836 * The Package SYSTEM::
23837 * Tasking and Task-Related Features::
23838 * Pragmas and Pragma-Related Features::
23839 * Library of Predefined Units::
23840 * Bindings::
23841 * Main Program Definition::
23842 * Implementation-Defined Attributes::
23843 * Compiler and Run-Time Interfacing::
23844 * Program Compilation and Library Management::
23845 * Input-Output::
23846 * Implementation Limits::
23847 * Tools and Utilities::
23848 @end menu
23849
23850 @node Ada Language Compatibility
23851 @section Ada Language Compatibility
23852
23853 @noindent
23854 GNAT handles Ada 95 and Ada 2005 as well as Ada 83, whereas HP Ada is only
23855 for Ada 83. Ada 95 and Ada 2005 are almost completely upwards compatible
23856 with Ada 83, and therefore Ada 83 programs will compile
23857 and run under GNAT with
23858 no changes or only minor changes. The @cite{Annotated Ada Reference Manual}
23859 provides details on specific incompatibilities.
23860
23861 GNAT provides the switch @option{/83} on the @command{GNAT COMPILE} command,
23862 as well as the pragma @code{ADA_83}, to force the compiler to
23863 operate in Ada 83 mode. This mode does not guarantee complete
23864 conformance to Ada 83, but in practice is sufficient to
23865 eliminate most sources of incompatibilities.
23866 In particular, it eliminates the recognition of the
23867 additional Ada 95 and Ada 2005 keywords, so that their use as identifiers
23868 in Ada 83 programs is legal, and handles the cases of packages
23869 with optional bodies, and generics that instantiate unconstrained
23870 types without the use of @code{(<>)}.
23871
23872 @node Differences in the Definition of Package System
23873 @section Differences in the Definition of Package @code{System}
23874
23875 @noindent
23876 An Ada compiler is allowed to add
23877 implementation-dependent declarations to package @code{System}.
23878 In normal mode,
23879 GNAT does not take advantage of this permission, and the version of
23880 @code{System} provided by GNAT exactly matches that defined in the Ada
23881 Reference Manual.
23882
23883 However, HP Ada adds an extensive set of declarations to package
23884 @code{System},
23885 as fully documented in the HP Ada manuals. To minimize changes required
23886 for programs that make use of these extensions, GNAT provides the pragma
23887 @code{Extend_System} for extending the definition of package System. By using:
23888 @cindex pragma @code{Extend_System}
23889 @cindex @code{Extend_System} pragma
23890
23891 @smallexample @c ada
23892 @group
23893 @cartouche
23894 pragma Extend_System (Aux_DEC);
23895 @end cartouche
23896 @end group
23897 @end smallexample
23898
23899 @noindent
23900 the set of definitions in @code{System} is extended to include those in
23901 package @code{System.Aux_DEC}.
23902 @cindex @code{System.Aux_DEC} package
23903 @cindex @code{Aux_DEC} package (child of @code{System})
23904 These definitions are incorporated directly into package @code{System},
23905 as though they had been declared there. For a
23906 list of the declarations added, see the spec of this package,
23907 which can be found in the file @file{s-auxdec.ads} in the GNAT library.
23908 @cindex @file{s-auxdec.ads} file
23909 The pragma @code{Extend_System} is a configuration pragma, which means that
23910 it can be placed in the file @file{gnat.adc}, so that it will automatically
23911 apply to all subsequent compilations. See @ref{Configuration Pragmas},
23912 for further details.
23913
23914 An alternative approach that avoids the use of the non-standard
23915 @code{Extend_System} pragma is to add a context clause to the unit that
23916 references these facilities:
23917
23918 @smallexample @c ada
23919 @cartouche
23920 with System.Aux_DEC;
23921 use System.Aux_DEC;
23922 @end cartouche
23923 @end smallexample
23924
23925 @noindent
23926 The effect is not quite semantically identical to incorporating
23927 the declarations directly into package @code{System},
23928 but most programs will not notice a difference
23929 unless they use prefix notation (e.g.@: @code{System.Integer_8})
23930 to reference the entities directly in package @code{System}.
23931 For units containing such references,
23932 the prefixes must either be removed, or the pragma @code{Extend_System}
23933 must be used.
23934
23935 @node Language-Related Features
23936 @section Language-Related Features
23937
23938 @noindent
23939 The following sections highlight differences in types,
23940 representations of types, operations, alignment, and
23941 related topics.
23942
23943 @menu
23944 * Integer Types and Representations::
23945 * Floating-Point Types and Representations::
23946 * Pragmas Float_Representation and Long_Float::
23947 * Fixed-Point Types and Representations::
23948 * Record and Array Component Alignment::
23949 * Address Clauses::
23950 * Other Representation Clauses::
23951 @end menu
23952
23953 @node Integer Types and Representations
23954 @subsection Integer Types and Representations
23955
23956 @noindent
23957 The set of predefined integer types is identical in HP Ada and GNAT.
23958 Furthermore the representation of these integer types is also identical,
23959 including the capability of size clauses forcing biased representation.
23960
23961 In addition,
23962 HP Ada for OpenVMS Alpha systems has defined the
23963 following additional integer types in package @code{System}:
23964
23965 @itemize @bullet
23966
23967 @item
23968 @code{INTEGER_8}
23969
23970 @item
23971 @code{INTEGER_16}
23972
23973 @item
23974 @code{INTEGER_32}
23975
23976 @item
23977 @code{INTEGER_64}
23978
23979 @item
23980 @code{LARGEST_INTEGER}
23981 @end itemize
23982
23983 @noindent
23984 In GNAT, the first four of these types may be obtained from the
23985 standard Ada package @code{Interfaces}.
23986 Alternatively, by use of the pragma @code{Extend_System}, identical
23987 declarations can be referenced directly in package @code{System}.
23988 On both GNAT and HP Ada, the maximum integer size is 64 bits.
23989
23990 @node Floating-Point Types and Representations
23991 @subsection Floating-Point Types and Representations
23992 @cindex Floating-Point types
23993
23994 @noindent
23995 The set of predefined floating-point types is identical in HP Ada and GNAT.
23996 Furthermore the representation of these floating-point
23997 types is also identical. One important difference is that the default
23998 representation for HP Ada is @code{VAX_Float}, but the default representation
23999 for GNAT is IEEE.
24000
24001 Specific types may be declared to be @code{VAX_Float} or IEEE, using the
24002 pragma @code{Float_Representation} as described in the HP Ada
24003 documentation.
24004 For example, the declarations:
24005
24006 @smallexample @c ada
24007 @cartouche
24008 type F_Float is digits 6;
24009 pragma Float_Representation (VAX_Float, F_Float);
24010 @end cartouche
24011 @end smallexample
24012
24013 @noindent
24014 declares a type @code{F_Float} that will be represented in @code{VAX_Float}
24015 format.
24016 This set of declarations actually appears in @code{System.Aux_DEC},
24017 which contains
24018 the full set of additional floating-point declarations provided in
24019 the HP Ada version of package @code{System}.
24020 This and similar declarations may be accessed in a user program
24021 by using pragma @code{Extend_System}. The use of this
24022 pragma, and the related pragma @code{Long_Float} is described in further
24023 detail in the following section.
24024
24025 @node Pragmas Float_Representation and Long_Float
24026 @subsection Pragmas @code{Float_Representation} and @code{Long_Float}
24027
24028 @noindent
24029 HP Ada provides the pragma @code{Float_Representation}, which
24030 acts as a program library switch to allow control over
24031 the internal representation chosen for the predefined
24032 floating-point types declared in the package @code{Standard}.
24033 The format of this pragma is as follows:
24034
24035 @smallexample @c ada
24036 @cartouche
24037 pragma Float_Representation(VAX_Float | IEEE_Float);
24038 @end cartouche
24039 @end smallexample
24040
24041 @noindent
24042 This pragma controls the representation of floating-point
24043 types as follows:
24044
24045 @itemize @bullet
24046 @item
24047 @code{VAX_Float} specifies that floating-point
24048 types are represented by default with the VAX system hardware types
24049 @code{F-floating}, @code{D-floating}, @code{G-floating}.
24050 Note that the @code{H-floating}
24051 type was available only on VAX systems, and is not available
24052 in either HP Ada or GNAT.
24053
24054 @item
24055 @code{IEEE_Float} specifies that floating-point
24056 types are represented by default with the IEEE single and
24057 double floating-point types.
24058 @end itemize
24059
24060 @noindent
24061 GNAT provides an identical implementation of the pragma
24062 @code{Float_Representation}, except that it functions as a
24063 configuration pragma. Note that the
24064 notion of configuration pragma corresponds closely to the
24065 HP Ada notion of a program library switch.
24066
24067 When no pragma is used in GNAT, the default is @code{IEEE_Float},
24068 which is different
24069 from HP Ada 83, where the default is @code{VAX_Float}. In addition, the
24070 predefined libraries in GNAT are built using @code{IEEE_Float}, so it is not
24071 advisable to change the format of numbers passed to standard library
24072 routines, and if necessary explicit type conversions may be needed.
24073
24074 The use of @code{IEEE_Float} is recommended in GNAT since it is more
24075 efficient, and (given that it conforms to an international standard)
24076 potentially more portable.
24077 The situation in which @code{VAX_Float} may be useful is in interfacing
24078 to existing code and data that expect the use of @code{VAX_Float}.
24079 In such a situation use the predefined @code{VAX_Float}
24080 types in package @code{System}, as extended by
24081 @code{Extend_System}. For example, use @code{System.F_Float}
24082 to specify the 32-bit @code{F-Float} format.
24083
24084 @noindent
24085 On OpenVMS systems, HP Ada provides the pragma @code{Long_Float}
24086 to allow control over the internal representation chosen
24087 for the predefined type @code{Long_Float} and for floating-point
24088 type declarations with digits specified in the range 7 .. 15.
24089 The format of this pragma is as follows:
24090
24091 @smallexample @c ada
24092 @cartouche
24093 pragma Long_Float (D_FLOAT | G_FLOAT);
24094 @end cartouche
24095 @end smallexample
24096
24097 @node Fixed-Point Types and Representations
24098 @subsection Fixed-Point Types and Representations
24099
24100 @noindent
24101 On HP Ada for OpenVMS Alpha systems, rounding is
24102 away from zero for both positive and negative numbers.
24103 Therefore, @code{+0.5} rounds to @code{1},
24104 and @code{-0.5} rounds to @code{-1}.
24105
24106 On GNAT the results of operations
24107 on fixed-point types are in accordance with the Ada
24108 rules. In particular, results of operations on decimal
24109 fixed-point types are truncated.
24110
24111 @node Record and Array Component Alignment
24112 @subsection Record and Array Component Alignment
24113
24114 @noindent
24115 On HP Ada for OpenVMS Alpha, all non-composite components
24116 are aligned on natural boundaries. For example, 1-byte
24117 components are aligned on byte boundaries, 2-byte
24118 components on 2-byte boundaries, 4-byte components on 4-byte
24119 byte boundaries, and so on. The OpenVMS Alpha hardware
24120 runs more efficiently with naturally aligned data.
24121
24122 On GNAT, alignment rules are compatible
24123 with HP Ada for OpenVMS Alpha.
24124
24125 @node Address Clauses
24126 @subsection Address Clauses
24127
24128 @noindent
24129 In HP Ada and GNAT, address clauses are supported for
24130 objects and imported subprograms.
24131 The predefined type @code{System.Address} is a private type
24132 in both compilers on Alpha OpenVMS, with the same representation
24133 (it is simply a machine pointer). Addition, subtraction, and comparison
24134 operations are available in the standard Ada package
24135 @code{System.Storage_Elements}, or in package @code{System}
24136 if it is extended to include @code{System.Aux_DEC} using a
24137 pragma @code{Extend_System} as previously described.
24138
24139 Note that code that @code{with}'s both this extended package @code{System}
24140 and the package @code{System.Storage_Elements} should not @code{use}
24141 both packages, or ambiguities will result. In general it is better
24142 not to mix these two sets of facilities. The Ada package was
24143 designed specifically to provide the kind of features that HP Ada
24144 adds directly to package @code{System}.
24145
24146 The type @code{System.Address} is a 64-bit integer type in GNAT for
24147 I64 OpenVMS. For more information,
24148 see @ref{Transitioning to 64-Bit GNAT for OpenVMS}.
24149
24150 GNAT is compatible with HP Ada in its handling of address
24151 clauses, except for some limitations in
24152 the form of address clauses for composite objects with
24153 initialization. Such address clauses are easily replaced
24154 by the use of an explicitly-defined constant as described
24155 in the Ada Reference Manual (13.1(22)). For example, the sequence
24156 of declarations:
24157
24158 @smallexample @c ada
24159 @cartouche
24160 X, Y : Integer := Init_Func;
24161 Q : String (X .. Y) := "abc";
24162 @dots{}
24163 for Q'Address use Compute_Address;
24164 @end cartouche
24165 @end smallexample
24166
24167 @noindent
24168 will be rejected by GNAT, since the address cannot be computed at the time
24169 that @code{Q} is declared. To achieve the intended effect, write instead:
24170
24171 @smallexample @c ada
24172 @group
24173 @cartouche
24174 X, Y : Integer := Init_Func;
24175 Q_Address : constant Address := Compute_Address;
24176 Q : String (X .. Y) := "abc";
24177 @dots{}
24178 for Q'Address use Q_Address;
24179 @end cartouche
24180 @end group
24181 @end smallexample
24182
24183 @noindent
24184 which will be accepted by GNAT (and other Ada compilers), and is also
24185 compatible with Ada 83. A fuller description of the restrictions
24186 on address specifications is found in @ref{Top, GNAT Reference Manual,
24187 About This Guide, gnat_rm, GNAT Reference Manual}.
24188
24189 @node Other Representation Clauses
24190 @subsection Other Representation Clauses
24191
24192 @noindent
24193 GNAT implements in a compatible manner all the representation
24194 clauses supported by HP Ada. In addition, GNAT
24195 implements the representation clause forms that were introduced in Ada 95,
24196 including @code{COMPONENT_SIZE} and @code{SIZE} clauses for objects.
24197
24198 @node The Package STANDARD
24199 @section The Package @code{STANDARD}
24200
24201 @noindent
24202 The package @code{STANDARD}, as implemented by HP Ada, is fully
24203 described in the @cite{Ada Reference Manual} and in the
24204 @cite{HP Ada Language Reference Manual}. As implemented by GNAT, the
24205 package @code{STANDARD} is described in the @cite{Ada Reference Manual}.
24206
24207 In addition, HP Ada supports the Latin-1 character set in
24208 the type @code{CHARACTER}. GNAT supports the Latin-1 character set
24209 in the type @code{CHARACTER} and also Unicode (ISO 10646 BMP) in
24210 the type @code{WIDE_CHARACTER}.
24211
24212 The floating-point types supported by GNAT are those
24213 supported by HP Ada, but the defaults are different, and are controlled by
24214 pragmas. See @ref{Floating-Point Types and Representations}, for details.
24215
24216 @node The Package SYSTEM
24217 @section The Package @code{SYSTEM}
24218
24219 @noindent
24220 HP Ada provides a specific version of the package
24221 @code{SYSTEM} for each platform on which the language is implemented.
24222 For the complete spec of the package @code{SYSTEM}, see
24223 Appendix F of the @cite{HP Ada Language Reference Manual}.
24224
24225 On HP Ada, the package @code{SYSTEM} includes the following conversion
24226 functions:
24227 @itemize @bullet
24228 @item @code{TO_ADDRESS(INTEGER)}
24229
24230 @item @code{TO_ADDRESS(UNSIGNED_LONGWORD)}
24231
24232 @item @code{TO_ADDRESS(}@i{universal_integer}@code{)}
24233
24234 @item @code{TO_INTEGER(ADDRESS)}
24235
24236 @item @code{TO_UNSIGNED_LONGWORD(ADDRESS)}
24237
24238 @item Function @code{IMPORT_VALUE return UNSIGNED_LONGWORD} and the
24239 functions @code{IMPORT_ADDRESS} and @code{IMPORT_LARGEST_VALUE}
24240 @end itemize
24241
24242 @noindent
24243 By default, GNAT supplies a version of @code{SYSTEM} that matches
24244 the definition given in the @cite{Ada Reference Manual}.
24245 This
24246 is a subset of the HP system definitions, which is as
24247 close as possible to the original definitions. The only difference
24248 is that the definition of @code{SYSTEM_NAME} is different:
24249
24250 @smallexample @c ada
24251 @cartouche
24252 type Name is (SYSTEM_NAME_GNAT);
24253 System_Name : constant Name := SYSTEM_NAME_GNAT;
24254 @end cartouche
24255 @end smallexample
24256
24257 @noindent
24258 Also, GNAT adds the Ada declarations for
24259 @code{BIT_ORDER} and @code{DEFAULT_BIT_ORDER}.
24260
24261 However, the use of the following pragma causes GNAT
24262 to extend the definition of package @code{SYSTEM} so that it
24263 encompasses the full set of HP-specific extensions,
24264 including the functions listed above:
24265
24266 @smallexample @c ada
24267 @cartouche
24268 pragma Extend_System (Aux_DEC);
24269 @end cartouche
24270 @end smallexample
24271
24272 @noindent
24273 The pragma @code{Extend_System} is a configuration pragma that
24274 is most conveniently placed in the @file{gnat.adc} file. @xref{Pragma
24275 Extend_System,,, gnat_rm, GNAT Reference Manual} for further details.
24276
24277 HP Ada does not allow the recompilation of the package
24278 @code{SYSTEM}. Instead HP Ada provides several pragmas
24279 (@code{SYSTEM_NAME}, @code{STORAGE_UNIT}, and @code{MEMORY_SIZE})
24280 to modify values in the package @code{SYSTEM}.
24281 On OpenVMS Alpha systems, the pragma
24282 @code{SYSTEM_NAME} takes the enumeration literal @code{OPENVMS_AXP} as
24283 its single argument.
24284
24285 GNAT does permit the recompilation of package @code{SYSTEM} using
24286 the special switch @option{-gnatg}, and this switch can be used if
24287 it is necessary to modify the definitions in @code{SYSTEM}. GNAT does
24288 not permit the specification of @code{SYSTEM_NAME}, @code{STORAGE_UNIT}
24289 or @code{MEMORY_SIZE} by any other means.
24290
24291 On GNAT systems, the pragma @code{SYSTEM_NAME} takes the
24292 enumeration literal @code{SYSTEM_NAME_GNAT}.
24293
24294 The definitions provided by the use of
24295
24296 @smallexample @c ada
24297 pragma Extend_System (AUX_Dec);
24298 @end smallexample
24299
24300 @noindent
24301 are virtually identical to those provided by the HP Ada 83 package
24302 @code{SYSTEM}. One important difference is that the name of the
24303 @code{TO_ADDRESS}
24304 function for type @code{UNSIGNED_LONGWORD} is changed to
24305 @code{TO_ADDRESS_LONG}.
24306 @xref{Address Clauses,,, gnat_rm, GNAT Reference Manual} for a
24307 discussion of why this change was necessary.
24308
24309 @noindent
24310 The version of @code{TO_ADDRESS} taking a @i{universal_integer} argument
24311 is in fact
24312 an extension to Ada 83 not strictly compatible with the reference manual.
24313 GNAT, in order to be exactly compatible with the standard,
24314 does not provide this capability. In HP Ada 83, the
24315 point of this definition is to deal with a call like:
24316
24317 @smallexample @c ada
24318 TO_ADDRESS (16#12777#);
24319 @end smallexample
24320
24321 @noindent
24322 Normally, according to Ada 83 semantics, one would expect this to be
24323 ambiguous, since it matches both the @code{INTEGER} and
24324 @code{UNSIGNED_LONGWORD} forms of @code{TO_ADDRESS}.
24325 However, in HP Ada 83, there is no ambiguity, since the
24326 definition using @i{universal_integer} takes precedence.
24327
24328 In GNAT, since the version with @i{universal_integer} cannot be supplied,
24329 it is
24330 not possible to be 100% compatible. Since there are many programs using
24331 numeric constants for the argument to @code{TO_ADDRESS}, the decision in
24332 GNAT was
24333 to change the name of the function in the @code{UNSIGNED_LONGWORD} case,
24334 so the declarations provided in the GNAT version of @code{AUX_Dec} are:
24335
24336 @smallexample @c ada
24337 function To_Address (X : Integer) return Address;
24338 pragma Pure_Function (To_Address);
24339
24340 function To_Address_Long (X : Unsigned_Longword) return Address;
24341 pragma Pure_Function (To_Address_Long);
24342 @end smallexample
24343
24344 @noindent
24345 This means that programs using @code{TO_ADDRESS} for
24346 @code{UNSIGNED_LONGWORD} must change the name to @code{TO_ADDRESS_LONG}.
24347
24348 @node Tasking and Task-Related Features
24349 @section Tasking and Task-Related Features
24350
24351 @noindent
24352 This section compares the treatment of tasking in GNAT
24353 and in HP Ada for OpenVMS Alpha.
24354 The GNAT description applies to both Alpha and I64 OpenVMS.
24355 For detailed information on tasking in
24356 HP Ada, see the @cite{HP Ada Language Reference Manual} and the
24357 relevant run-time reference manual.
24358
24359 @menu
24360 * Implementation of Tasks in HP Ada for OpenVMS Alpha Systems::
24361 * Assigning Task IDs::
24362 * Task IDs and Delays::
24363 * Task-Related Pragmas::
24364 * Scheduling and Task Priority::
24365 * The Task Stack::
24366 * External Interrupts::
24367 @end menu
24368
24369 @node Implementation of Tasks in HP Ada for OpenVMS Alpha Systems
24370 @subsection Implementation of Tasks in HP Ada for OpenVMS Alpha Systems
24371
24372 @noindent
24373 On OpenVMS Alpha systems, each Ada task (except a passive
24374 task) is implemented as a single stream of execution
24375 that is created and managed by the kernel. On these
24376 systems, HP Ada tasking support is based on DECthreads,
24377 an implementation of the POSIX standard for threads.
24378
24379 Also, on OpenVMS Alpha systems, HP Ada tasks and foreign
24380 code that calls DECthreads routines can be used together.
24381 The interaction between Ada tasks and DECthreads routines
24382 can have some benefits. For example when on OpenVMS Alpha,
24383 HP Ada can call C code that is already threaded.
24384
24385 GNAT uses the facilities of DECthreads,
24386 and Ada tasks are mapped to threads.
24387
24388 @node Assigning Task IDs
24389 @subsection Assigning Task IDs
24390
24391 @noindent
24392 The HP Ada Run-Time Library always assigns @code{%TASK 1} to
24393 the environment task that executes the main program. On
24394 OpenVMS Alpha systems, @code{%TASK 0} is often used for tasks
24395 that have been created but are not yet activated.
24396
24397 On OpenVMS Alpha systems, task IDs are assigned at
24398 activation. On GNAT systems, task IDs are also assigned at
24399 task creation but do not have the same form or values as
24400 task ID values in HP Ada. There is no null task, and the
24401 environment task does not have a specific task ID value.
24402
24403 @node Task IDs and Delays
24404 @subsection Task IDs and Delays
24405
24406 @noindent
24407 On OpenVMS Alpha systems, tasking delays are implemented
24408 using Timer System Services. The Task ID is used for the
24409 identification of the timer request (the @code{REQIDT} parameter).
24410 If Timers are used in the application take care not to use
24411 @code{0} for the identification, because cancelling such a timer
24412 will cancel all timers and may lead to unpredictable results.
24413
24414 @node Task-Related Pragmas
24415 @subsection Task-Related Pragmas
24416
24417 @noindent
24418 Ada supplies the pragma @code{TASK_STORAGE}, which allows
24419 specification of the size of the guard area for a task
24420 stack. (The guard area forms an area of memory that has no
24421 read or write access and thus helps in the detection of
24422 stack overflow.) On OpenVMS Alpha systems, if the pragma
24423 @code{TASK_STORAGE} specifies a value of zero, a minimal guard
24424 area is created. In the absence of a pragma @code{TASK_STORAGE},
24425 a default guard area is created.
24426
24427 GNAT supplies the following task-related pragmas:
24428
24429 @itemize @bullet
24430 @item @code{TASK_INFO}
24431
24432 This pragma appears within a task definition and
24433 applies to the task in which it appears. The argument
24434 must be of type @code{SYSTEM.TASK_INFO.TASK_INFO_TYPE}.
24435
24436 @item @code{TASK_STORAGE}
24437
24438 GNAT implements pragma @code{TASK_STORAGE} in the same way as HP Ada.
24439 Both HP Ada and GNAT supply the pragmas @code{PASSIVE},
24440 @code{SUPPRESS}, and @code{VOLATILE}.
24441 @end itemize
24442 @node Scheduling and Task Priority
24443 @subsection Scheduling and Task Priority
24444
24445 @noindent
24446 HP Ada implements the Ada language requirement that
24447 when two tasks are eligible for execution and they have
24448 different priorities, the lower priority task does not
24449 execute while the higher priority task is waiting. The HP
24450 Ada Run-Time Library keeps a task running until either the
24451 task is suspended or a higher priority task becomes ready.
24452
24453 On OpenVMS Alpha systems, the default strategy is round-
24454 robin with preemption. Tasks of equal priority take turns
24455 at the processor. A task is run for a certain period of
24456 time and then placed at the tail of the ready queue for
24457 its priority level.
24458
24459 HP Ada provides the implementation-defined pragma @code{TIME_SLICE},
24460 which can be used to enable or disable round-robin
24461 scheduling of tasks with the same priority.
24462 See the relevant HP Ada run-time reference manual for
24463 information on using the pragmas to control HP Ada task
24464 scheduling.
24465
24466 GNAT follows the scheduling rules of Annex D (Real-Time
24467 Annex) of the @cite{Ada Reference Manual}. In general, this
24468 scheduling strategy is fully compatible with HP Ada
24469 although it provides some additional constraints (as
24470 fully documented in Annex D).
24471 GNAT implements time slicing control in a manner compatible with
24472 HP Ada 83, by means of the pragma @code{Time_Slice}, whose semantics
24473 are identical to the HP Ada 83 pragma of the same name.
24474 Note that it is not possible to mix GNAT tasking and
24475 HP Ada 83 tasking in the same program, since the two run-time
24476 libraries are not compatible.
24477
24478 @node The Task Stack
24479 @subsection The Task Stack
24480
24481 @noindent
24482 In HP Ada, a task stack is allocated each time a
24483 non-passive task is activated. As soon as the task is
24484 terminated, the storage for the task stack is deallocated.
24485 If you specify a size of zero (bytes) with @code{T'STORAGE_SIZE},
24486 a default stack size is used. Also, regardless of the size
24487 specified, some additional space is allocated for task
24488 management purposes. On OpenVMS Alpha systems, at least
24489 one page is allocated.
24490
24491 GNAT handles task stacks in a similar manner. In accordance with
24492 the Ada rules, it provides the pragma @code{STORAGE_SIZE} as
24493 an alternative method for controlling the task stack size.
24494 The specification of the attribute @code{T'STORAGE_SIZE} is also
24495 supported in a manner compatible with HP Ada.
24496
24497 @node External Interrupts
24498 @subsection External Interrupts
24499
24500 @noindent
24501 On HP Ada, external interrupts can be associated with task entries.
24502 GNAT is compatible with HP Ada in its handling of external interrupts.
24503
24504 @node Pragmas and Pragma-Related Features
24505 @section Pragmas and Pragma-Related Features
24506
24507 @noindent
24508 Both HP Ada and GNAT supply all language-defined pragmas
24509 as specified by the Ada 83 standard. GNAT also supplies all
24510 language-defined pragmas introduced by Ada 95 and Ada 2005.
24511 In addition, GNAT implements the implementation-defined pragmas
24512 from HP Ada 83.
24513
24514 @itemize @bullet
24515 @item @code{AST_ENTRY}
24516
24517 @item @code{COMMON_OBJECT}
24518
24519 @item @code{COMPONENT_ALIGNMENT}
24520
24521 @item @code{EXPORT_EXCEPTION}
24522
24523 @item @code{EXPORT_FUNCTION}
24524
24525 @item @code{EXPORT_OBJECT}
24526
24527 @item @code{EXPORT_PROCEDURE}
24528
24529 @item @code{EXPORT_VALUED_PROCEDURE}
24530
24531 @item @code{FLOAT_REPRESENTATION}
24532
24533 @item @code{IDENT}
24534
24535 @item @code{IMPORT_EXCEPTION}
24536
24537 @item @code{IMPORT_FUNCTION}
24538
24539 @item @code{IMPORT_OBJECT}
24540
24541 @item @code{IMPORT_PROCEDURE}
24542
24543 @item @code{IMPORT_VALUED_PROCEDURE}
24544
24545 @item @code{INLINE_GENERIC}
24546
24547 @item @code{INTERFACE_NAME}
24548
24549 @item @code{LONG_FLOAT}
24550
24551 @item @code{MAIN_STORAGE}
24552
24553 @item @code{PASSIVE}
24554
24555 @item @code{PSECT_OBJECT}
24556
24557 @item @code{SHARE_GENERIC}
24558
24559 @item @code{SUPPRESS_ALL}
24560
24561 @item @code{TASK_STORAGE}
24562
24563 @item @code{TIME_SLICE}
24564
24565 @item @code{TITLE}
24566 @end itemize
24567
24568 @noindent
24569 These pragmas are all fully implemented, with the exception of @code{TITLE},
24570 @code{PASSIVE}, and @code{SHARE_GENERIC}, which are
24571 recognized, but which have no
24572 effect in GNAT. The effect of @code{PASSIVE} may be obtained by the
24573 use of Ada protected objects. In GNAT, all generics are inlined.
24574
24575 Unlike HP Ada, the GNAT ``@code{EXPORT_}@i{subprogram}'' pragmas require
24576 a separate subprogram specification which must appear before the
24577 subprogram body.
24578
24579 GNAT also supplies a number of implementation-defined pragmas as follows:
24580 @itemize @bullet
24581 @item @code{ABORT_DEFER}
24582
24583 @item @code{ADA_83}
24584
24585 @item @code{ADA_95}
24586
24587 @item @code{ADA_05}
24588
24589 @item @code{ANNOTATE}
24590
24591 @item @code{ASSERT}
24592
24593 @item @code{C_PASS_BY_COPY}
24594
24595 @item @code{CPP_CLASS}
24596
24597 @item @code{CPP_CONSTRUCTOR}
24598
24599 @item @code{CPP_DESTRUCTOR}
24600
24601 @item @code{DEBUG}
24602
24603 @item @code{EXTEND_SYSTEM}
24604
24605 @item @code{LINKER_ALIAS}
24606
24607 @item @code{LINKER_SECTION}
24608
24609 @item @code{MACHINE_ATTRIBUTE}
24610
24611 @item @code{NO_RETURN}
24612
24613 @item @code{PURE_FUNCTION}
24614
24615 @item @code{SOURCE_FILE_NAME}
24616
24617 @item @code{SOURCE_REFERENCE}
24618
24619 @item @code{TASK_INFO}
24620
24621 @item @code{UNCHECKED_UNION}
24622
24623 @item @code{UNIMPLEMENTED_UNIT}
24624
24625 @item @code{UNIVERSAL_DATA}
24626
24627 @item @code{UNSUPPRESS}
24628
24629 @item @code{WARNINGS}
24630
24631 @item @code{WEAK_EXTERNAL}
24632 @end itemize
24633
24634 @noindent
24635 For full details on these GNAT implementation-defined pragmas,
24636 see @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference
24637 Manual}.
24638
24639 @menu
24640 * Restrictions on the Pragma INLINE::
24641 * Restrictions on the Pragma INTERFACE::
24642 * Restrictions on the Pragma SYSTEM_NAME::
24643 @end menu
24644
24645 @node Restrictions on the Pragma INLINE
24646 @subsection Restrictions on Pragma @code{INLINE}
24647
24648 @noindent
24649 HP Ada enforces the following restrictions on the pragma @code{INLINE}:
24650 @itemize @bullet
24651 @item Parameters cannot have a task type.
24652
24653 @item Function results cannot be task types, unconstrained
24654 array types, or unconstrained types with discriminants.
24655
24656 @item Bodies cannot declare the following:
24657 @itemize @bullet
24658 @item Subprogram body or stub (imported subprogram is allowed)
24659
24660 @item Tasks
24661
24662 @item Generic declarations
24663
24664 @item Instantiations
24665
24666 @item Exceptions
24667
24668 @item Access types (types derived from access types allowed)
24669
24670 @item Array or record types
24671
24672 @item Dependent tasks
24673
24674 @item Direct recursive calls of subprogram or containing
24675 subprogram, directly or via a renaming
24676
24677 @end itemize
24678 @end itemize
24679
24680 @noindent
24681 In GNAT, the only restriction on pragma @code{INLINE} is that the
24682 body must occur before the call if both are in the same
24683 unit, and the size must be appropriately small. There are
24684 no other specific restrictions which cause subprograms to
24685 be incapable of being inlined.
24686
24687 @node Restrictions on the Pragma INTERFACE
24688 @subsection Restrictions on Pragma @code{INTERFACE}
24689
24690 @noindent
24691 The following restrictions on pragma @code{INTERFACE}
24692 are enforced by both HP Ada and GNAT:
24693 @itemize @bullet
24694 @item Languages accepted: Ada, Bliss, C, Fortran, Default.
24695 Default is the default on OpenVMS Alpha systems.
24696
24697 @item Parameter passing: Language specifies default
24698 mechanisms but can be overridden with an @code{EXPORT} pragma.
24699
24700 @itemize @bullet
24701 @item Ada: Use internal Ada rules.
24702
24703 @item Bliss, C: Parameters must be mode @code{in}; cannot be
24704 record or task type. Result cannot be a string, an
24705 array, or a record.
24706
24707 @item Fortran: Parameters cannot have a task type. Result cannot
24708 be a string, an array, or a record.
24709 @end itemize
24710 @end itemize
24711
24712 @noindent
24713 GNAT is entirely upwards compatible with HP Ada, and in addition allows
24714 record parameters for all languages.
24715
24716 @node Restrictions on the Pragma SYSTEM_NAME
24717 @subsection Restrictions on Pragma @code{SYSTEM_NAME}
24718
24719 @noindent
24720 For HP Ada for OpenVMS Alpha, the enumeration literal
24721 for the type @code{NAME} is @code{OPENVMS_AXP}.
24722 In GNAT, the enumeration
24723 literal for the type @code{NAME} is @code{SYSTEM_NAME_GNAT}.
24724
24725 @node Library of Predefined Units
24726 @section Library of Predefined Units
24727
24728 @noindent
24729 A library of predefined units is provided as part of the
24730 HP Ada and GNAT implementations. HP Ada does not provide
24731 the package @code{MACHINE_CODE} but instead recommends importing
24732 assembler code.
24733
24734 The GNAT versions of the HP Ada Run-Time Library (@code{ADA$PREDEFINED:})
24735 units are taken from the OpenVMS Alpha version, not the OpenVMS VAX
24736 version.
24737 The HP Ada Predefined Library units are modified to remove post-Ada 83
24738 incompatibilities and to make them interoperable with GNAT
24739 (@pxref{Changes to DECLIB}, for details).
24740 The units are located in the @file{DECLIB} directory.
24741
24742 The GNAT RTL is contained in
24743 the @file{ADALIB} directory, and
24744 the default search path is set up to find @code{DECLIB} units in preference
24745 to @code{ADALIB} units with the same name (@code{TEXT_IO},
24746 @code{SEQUENTIAL_IO}, and @code{DIRECT_IO}, for example).
24747
24748 @menu
24749 * Changes to DECLIB::
24750 @end menu
24751
24752 @node Changes to DECLIB
24753 @subsection Changes to @code{DECLIB}
24754
24755 @noindent
24756 The changes made to the HP Ada predefined library for GNAT and post-Ada 83
24757 compatibility are minor and include the following:
24758
24759 @itemize @bullet
24760 @item Adjusting the location of pragmas and record representation
24761 clauses to obey Ada 95 (and thus Ada 2005) rules
24762
24763 @item Adding the proper notation to generic formal parameters
24764 that take unconstrained types in instantiation
24765
24766 @item Adding pragma @code{ELABORATE_BODY} to package specs
24767 that have package bodies not otherwise allowed
24768
24769 @item Replacing occurrences of the identifier ``@code{PROTECTED}'' by
24770 ``@code{PROTECTD}''.
24771 Currently these are found only in the @code{STARLET} package spec.
24772
24773 @item Changing @code{SYSTEM.ADDRESS} to @code{SYSTEM.SHORT_ADDRESS}
24774 where the address size is constrained to 32 bits.
24775 @end itemize
24776
24777 @noindent
24778 None of the above changes is visible to users.
24779
24780 @node Bindings
24781 @section Bindings
24782
24783 @noindent
24784 On OpenVMS Alpha, HP Ada provides the following strongly-typed bindings:
24785 @itemize @bullet
24786
24787 @item Command Language Interpreter (CLI interface)
24788
24789 @item DECtalk Run-Time Library (DTK interface)
24790
24791 @item Librarian utility routines (LBR interface)
24792
24793 @item General Purpose Run-Time Library (LIB interface)
24794
24795 @item Math Run-Time Library (MTH interface)
24796
24797 @item National Character Set Run-Time Library (NCS interface)
24798
24799 @item Compiled Code Support Run-Time Library (OTS interface)
24800
24801 @item Parallel Processing Run-Time Library (PPL interface)
24802
24803 @item Screen Management Run-Time Library (SMG interface)
24804
24805 @item Sort Run-Time Library (SOR interface)
24806
24807 @item String Run-Time Library (STR interface)
24808
24809 @item STARLET System Library
24810 @findex Starlet
24811
24812 @item X Window System Version 11R4 and 11R5 (X, XLIB interface)
24813
24814 @item X Windows Toolkit (XT interface)
24815
24816 @item X/Motif Version 1.1.3 and 1.2 (XM interface)
24817 @end itemize
24818
24819 @noindent
24820 GNAT provides implementations of these HP bindings in the @code{DECLIB}
24821 directory, on both the Alpha and I64 OpenVMS platforms.
24822
24823 The X/Motif bindings used to build @code{DECLIB} are whatever versions are
24824 in the
24825 HP Ada @file{ADA$PREDEFINED} directory with extension @file{.ADC}.
24826 A pragma @code{Linker_Options} has been added to packages @code{Xm},
24827 @code{Xt}, and @code{X_Lib}
24828 causing the default X/Motif sharable image libraries to be linked in. This
24829 is done via options files named @file{xm.opt}, @file{xt.opt}, and
24830 @file{x_lib.opt} (also located in the @file{DECLIB} directory).
24831
24832 It may be necessary to edit these options files to update or correct the
24833 library names if, for example, the newer X/Motif bindings from
24834 @file{ADA$EXAMPLES}
24835 had been (previous to installing GNAT) copied and renamed to supersede the
24836 default @file{ADA$PREDEFINED} versions.
24837
24838 @menu
24839 * Shared Libraries and Options Files::
24840 * Interfaces to C::
24841 @end menu
24842
24843 @node Shared Libraries and Options Files
24844 @subsection Shared Libraries and Options Files
24845
24846 @noindent
24847 When using the HP Ada
24848 predefined X and Motif bindings, the linking with their sharable images is
24849 done automatically by @command{GNAT LINK}.
24850 When using other X and Motif bindings, you need
24851 to add the corresponding sharable images to the command line for
24852 @code{GNAT LINK}. When linking with shared libraries, or with
24853 @file{.OPT} files, you must
24854 also add them to the command line for @command{GNAT LINK}.
24855
24856 A shared library to be used with GNAT is built in the same way as other
24857 libraries under VMS. The VMS Link command can be used in standard fashion.
24858
24859 @node Interfaces to C
24860 @subsection Interfaces to C
24861
24862 @noindent
24863 HP Ada
24864 provides the following Ada types and operations:
24865
24866 @itemize @bullet
24867 @item C types package (@code{C_TYPES})
24868
24869 @item C strings (@code{C_TYPES.NULL_TERMINATED})
24870
24871 @item Other_types (@code{SHORT_INT})
24872 @end itemize
24873
24874 @noindent
24875 Interfacing to C with GNAT, you can use the above approach
24876 described for HP Ada or the facilities of Annex B of
24877 the @cite{Ada Reference Manual} (packages @code{INTERFACES.C},
24878 @code{INTERFACES.C.STRINGS} and @code{INTERFACES.C.POINTERS}). For more
24879 information, see @ref{Interfacing to C,,, gnat_rm, GNAT Reference Manual}.
24880
24881 The @option{-gnatF} qualifier forces default and explicit
24882 @code{External_Name} parameters in pragmas @code{Import} and @code{Export}
24883 to be uppercased for compatibility with the default behavior
24884 of HP C. The qualifier has no effect on @code{Link_Name} parameters.
24885
24886 @node Main Program Definition
24887 @section Main Program Definition
24888
24889 @noindent
24890 The following section discusses differences in the
24891 definition of main programs on HP Ada and GNAT.
24892 On HP Ada, main programs are defined to meet the
24893 following conditions:
24894 @itemize @bullet
24895 @item Procedure with no formal parameters (returns @code{0} upon
24896 normal completion)
24897
24898 @item Procedure with no formal parameters (returns @code{42} when
24899 an unhandled exception is raised)
24900
24901 @item Function with no formal parameters whose returned value
24902 is of a discrete type
24903
24904 @item Procedure with one @code{out} formal of a discrete type for
24905 which a specification of pragma @code{EXPORT_VALUED_PROCEDURE} is given.
24906
24907 @end itemize
24908
24909 @noindent
24910 When declared with the pragma @code{EXPORT_VALUED_PROCEDURE},
24911 a main function or main procedure returns a discrete
24912 value whose size is less than 64 bits (32 on VAX systems),
24913 the value is zero- or sign-extended as appropriate.
24914 On GNAT, main programs are defined as follows:
24915 @itemize @bullet
24916 @item Must be a non-generic, parameterless subprogram that
24917 is either a procedure or function returning an Ada
24918 @code{STANDARD.INTEGER} (the predefined type)
24919
24920 @item Cannot be a generic subprogram or an instantiation of a
24921 generic subprogram
24922 @end itemize
24923
24924 @node Implementation-Defined Attributes
24925 @section Implementation-Defined Attributes
24926
24927 @noindent
24928 GNAT provides all HP Ada implementation-defined
24929 attributes.
24930
24931 @node Compiler and Run-Time Interfacing
24932 @section Compiler and Run-Time Interfacing
24933
24934 @noindent
24935 HP Ada provides the following qualifiers to pass options to the linker
24936 (ACS LINK):
24937 @itemize @bullet
24938 @item @option{/WAIT} and @option{/SUBMIT}
24939
24940 @item @option{/COMMAND}
24941
24942 @item @option{/@r{[}NO@r{]}MAP}
24943
24944 @item @option{/OUTPUT=@var{file-spec}}
24945
24946 @item @option{/@r{[}NO@r{]}DEBUG} and @option{/@r{[}NO@r{]}TRACEBACK}
24947 @end itemize
24948
24949 @noindent
24950 To pass options to the linker, GNAT provides the following
24951 switches:
24952
24953 @itemize @bullet
24954 @item @option{/EXECUTABLE=@var{exec-name}}
24955
24956 @item @option{/VERBOSE}
24957
24958 @item @option{/@r{[}NO@r{]}DEBUG} and @option{/@r{[}NO@r{]}TRACEBACK}
24959 @end itemize
24960
24961 @noindent
24962 For more information on these switches, see
24963 @ref{Switches for gnatlink}.
24964 In HP Ada, the command-line switch @option{/OPTIMIZE} is available
24965 to control optimization. HP Ada also supplies the
24966 following pragmas:
24967 @itemize @bullet
24968 @item @code{OPTIMIZE}
24969
24970 @item @code{INLINE}
24971
24972 @item @code{INLINE_GENERIC}
24973
24974 @item @code{SUPPRESS_ALL}
24975
24976 @item @code{PASSIVE}
24977 @end itemize
24978
24979 @noindent
24980 In GNAT, optimization is controlled strictly by command
24981 line parameters, as described in the corresponding section of this guide.
24982 The HP pragmas for control of optimization are
24983 recognized but ignored.
24984
24985 Note that in GNAT, the default is optimization off, whereas in HP Ada
24986 the default is that optimization is turned on.
24987
24988 @node Program Compilation and Library Management
24989 @section Program Compilation and Library Management
24990
24991 @noindent
24992 HP Ada and GNAT provide a comparable set of commands to
24993 build programs. HP Ada also provides a program library,
24994 which is a concept that does not exist on GNAT. Instead,
24995 GNAT provides directories of sources that are compiled as
24996 needed.
24997
24998 The following table summarizes
24999 the HP Ada commands and provides
25000 equivalent GNAT commands. In this table, some GNAT
25001 equivalents reflect the fact that GNAT does not use the
25002 concept of a program library. Instead, it uses a model
25003 in which collections of source and object files are used
25004 in a manner consistent with other languages like C and
25005 Fortran. Therefore, standard system file commands are used
25006 to manipulate these elements. Those GNAT commands are marked with
25007 an asterisk.
25008 Note that, unlike HP Ada, none of the GNAT commands accepts wild cards.
25009
25010 @need 1500
25011 @multitable @columnfractions .35 .65
25012
25013 @item @emph{HP Ada Command}
25014 @tab @emph{GNAT Equivalent / Description}
25015
25016 @item @command{ADA}
25017 @tab @command{GNAT COMPILE}@*
25018 Invokes the compiler to compile one or more Ada source files.
25019
25020 @item @command{ACS ATTACH}@*
25021 @tab [No equivalent]@*
25022 Switches control of terminal from current process running the program
25023 library manager.
25024
25025 @item @command{ACS CHECK}
25026 @tab @command{GNAT MAKE /DEPENDENCY_LIST}@*
25027 Forms the execution closure of one
25028 or more compiled units and checks completeness and currency.
25029
25030 @item @command{ACS COMPILE}
25031 @tab @command{GNAT MAKE /ACTIONS=COMPILE}@*
25032 Forms the execution closure of one or
25033 more specified units, checks completeness and currency,
25034 identifies units that have revised source files, compiles same,
25035 and recompiles units that are or will become obsolete.
25036 Also completes incomplete generic instantiations.
25037
25038 @item @command{ACS COPY FOREIGN}
25039 @tab Copy (*)@*
25040 Copies a foreign object file into the program library as a
25041 library unit body.
25042
25043 @item @command{ACS COPY UNIT}
25044 @tab Copy (*)@*
25045 Copies a compiled unit from one program library to another.
25046
25047 @item @command{ACS CREATE LIBRARY}
25048 @tab Create /directory (*)@*
25049 Creates a program library.
25050
25051 @item @command{ACS CREATE SUBLIBRARY}
25052 @tab Create /directory (*)@*
25053 Creates a program sublibrary.
25054
25055 @item @command{ACS DELETE LIBRARY}
25056 @tab @*
25057 Deletes a program library and its contents.
25058
25059 @item @command{ACS DELETE SUBLIBRARY}
25060 @tab @*
25061 Deletes a program sublibrary and its contents.
25062
25063 @item @command{ACS DELETE UNIT}
25064 @tab Delete file (*)@*
25065 On OpenVMS systems, deletes one or more compiled units from
25066 the current program library.
25067
25068 @item @command{ACS DIRECTORY}
25069 @tab Directory (*)@*
25070 On OpenVMS systems, lists units contained in the current
25071 program library.
25072
25073 @item @command{ACS ENTER FOREIGN}
25074 @tab Copy (*)@*
25075 Allows the import of a foreign body as an Ada library
25076 spec and enters a reference to a pointer.
25077
25078 @item @command{ACS ENTER UNIT}
25079 @tab Copy (*)@*
25080 Enters a reference (pointer) from the current program library to
25081 a unit compiled into another program library.
25082
25083 @item @command{ACS EXIT}
25084 @tab [No equivalent]@*
25085 Exits from the program library manager.
25086
25087 @item @command{ACS EXPORT}
25088 @tab Copy (*)@*
25089 Creates an object file that contains system-specific object code
25090 for one or more units. With GNAT, object files can simply be copied
25091 into the desired directory.
25092
25093 @item @command{ACS EXTRACT SOURCE}
25094 @tab Copy (*)@*
25095 Allows access to the copied source file for each Ada compilation unit
25096
25097 @item @command{ACS HELP}
25098 @tab @command{HELP GNAT}@*
25099 Provides online help.
25100
25101 @item @command{ACS LINK}
25102 @tab @command{GNAT LINK}@*
25103 Links an object file containing Ada units into an executable file.
25104
25105 @item @command{ACS LOAD}
25106 @tab Copy (*)@*
25107 Loads (partially compiles) Ada units into the program library.
25108 Allows loading a program from a collection of files into a library
25109 without knowing the relationship among units.
25110
25111 @item @command{ACS MERGE}
25112 @tab Copy (*)@*
25113 Merges into the current program library, one or more units from
25114 another library where they were modified.
25115
25116 @item @command{ACS RECOMPILE}
25117 @tab @command{GNAT MAKE /ACTIONS=COMPILE}@*
25118 Recompiles from external or copied source files any obsolete
25119 unit in the closure. Also, completes any incomplete generic
25120 instantiations.
25121
25122 @item @command{ACS REENTER}
25123 @tab @command{GNAT MAKE}@*
25124 Reenters current references to units compiled after last entered
25125 with the @command{ACS ENTER UNIT} command.
25126
25127 @item @command{ACS SET LIBRARY}
25128 @tab Set default (*)@*
25129 Defines a program library to be the compilation context as well
25130 as the target library for compiler output and commands in general.
25131
25132 @item @command{ACS SET PRAGMA}
25133 @tab Edit @file{gnat.adc} (*)@*
25134 Redefines specified values of the library characteristics
25135 @code{LONG_ FLOAT}, @code{MEMORY_SIZE}, @code{SYSTEM_NAME},
25136 and @code{Float_Representation}.
25137
25138 @item @command{ACS SET SOURCE}
25139 @tab Define @code{ADA_INCLUDE_PATH} path (*)@*
25140 Defines the source file search list for the @command{ACS COMPILE} command.
25141
25142 @item @command{ACS SHOW LIBRARY}
25143 @tab Directory (*)@*
25144 Lists information about one or more program libraries.
25145
25146 @item @command{ACS SHOW PROGRAM}
25147 @tab [No equivalent]@*
25148 Lists information about the execution closure of one or
25149 more units in the program library.
25150
25151 @item @command{ACS SHOW SOURCE}
25152 @tab Show logical @code{ADA_INCLUDE_PATH}@*
25153 Shows the source file search used when compiling units.
25154
25155 @item @command{ACS SHOW VERSION}
25156 @tab Compile with @option{VERBOSE} option
25157 Displays the version number of the compiler and program library
25158 manager used.
25159
25160 @item @command{ACS SPAWN}
25161 @tab [No equivalent]@*
25162 Creates a subprocess of the current process (same as @command{DCL SPAWN}
25163 command).
25164
25165 @item @command{ACS VERIFY}
25166 @tab [No equivalent]@*
25167 Performs a series of consistency checks on a program library to
25168 determine whether the library structure and library files are in
25169 valid form.
25170 @end multitable
25171
25172 @noindent
25173
25174 @node Input-Output
25175 @section Input-Output
25176
25177 @noindent
25178 On OpenVMS Alpha systems, HP Ada uses OpenVMS Record
25179 Management Services (RMS) to perform operations on
25180 external files.
25181
25182 @noindent
25183 HP Ada and GNAT predefine an identical set of input-
25184 output packages. To make the use of the
25185 generic @code{TEXT_IO} operations more convenient, HP Ada
25186 provides predefined library packages that instantiate the
25187 integer and floating-point operations for the predefined
25188 integer and floating-point types as shown in the following table.
25189
25190 @multitable @columnfractions .45 .55
25191 @item @emph{Package Name} @tab Instantiation
25192
25193 @item @code{INTEGER_TEXT_IO}
25194 @tab @code{INTEGER_IO(INTEGER)}
25195
25196 @item @code{SHORT_INTEGER_TEXT_IO}
25197 @tab @code{INTEGER_IO(SHORT_INTEGER)}
25198
25199 @item @code{SHORT_SHORT_INTEGER_TEXT_IO}
25200 @tab @code{INTEGER_IO(SHORT_SHORT_INTEGER)}
25201
25202 @item @code{FLOAT_TEXT_IO}
25203 @tab @code{FLOAT_IO(FLOAT)}
25204
25205 @item @code{LONG_FLOAT_TEXT_IO}
25206 @tab @code{FLOAT_IO(LONG_FLOAT)}
25207 @end multitable
25208
25209 @noindent
25210 The HP Ada predefined packages and their operations
25211 are implemented using OpenVMS Alpha files and input-output
25212 facilities. HP Ada supports asynchronous input-output on OpenVMS Alpha.
25213 Familiarity with the following is recommended:
25214 @itemize @bullet
25215 @item RMS file organizations and access methods
25216
25217 @item OpenVMS file specifications and directories
25218
25219 @item OpenVMS File Definition Language (FDL)
25220 @end itemize
25221
25222 @noindent
25223 GNAT provides I/O facilities that are completely
25224 compatible with HP Ada. The distribution includes the
25225 standard HP Ada versions of all I/O packages, operating
25226 in a manner compatible with HP Ada. In particular, the
25227 following packages are by default the HP Ada (Ada 83)
25228 versions of these packages rather than the renamings
25229 suggested in Annex J of the Ada Reference Manual:
25230 @itemize @bullet
25231 @item @code{TEXT_IO}
25232
25233 @item @code{SEQUENTIAL_IO}
25234
25235 @item @code{DIRECT_IO}
25236 @end itemize
25237
25238 @noindent
25239 The use of the standard child package syntax (for
25240 example, @code{ADA.TEXT_IO}) retrieves the post-Ada 83 versions of these
25241 packages.
25242 GNAT provides HP-compatible predefined instantiations
25243 of the @code{TEXT_IO} packages, and also
25244 provides the standard predefined instantiations required
25245 by the @cite{Ada Reference Manual}.
25246
25247 For further information on how GNAT interfaces to the file
25248 system or how I/O is implemented in programs written in
25249 mixed languages, see @ref{Implementation of the Standard I/O,,,
25250 gnat_rm, GNAT Reference Manual}.
25251 This chapter covers the following:
25252 @itemize @bullet
25253 @item Standard I/O packages
25254
25255 @item @code{FORM} strings
25256
25257 @item @code{ADA.DIRECT_IO}
25258
25259 @item @code{ADA.SEQUENTIAL_IO}
25260
25261 @item @code{ADA.TEXT_IO}
25262
25263 @item Stream pointer positioning
25264
25265 @item Reading and writing non-regular files
25266
25267 @item @code{GET_IMMEDIATE}
25268
25269 @item Treating @code{TEXT_IO} files as streams
25270
25271 @item Shared files
25272
25273 @item Open modes
25274 @end itemize
25275
25276 @node Implementation Limits
25277 @section Implementation Limits
25278
25279 @noindent
25280 The following table lists implementation limits for HP Ada
25281 and GNAT systems.
25282 @multitable @columnfractions .60 .20 .20
25283 @sp 1
25284 @item @emph{Compilation Parameter}
25285 @tab @emph{HP Ada}
25286 @tab @emph{GNAT}
25287 @sp 1
25288
25289 @item In a subprogram or entry declaration, maximum number of
25290 formal parameters that are of an unconstrained record type
25291 @tab 32
25292 @tab No set limit
25293 @sp 1
25294
25295 @item Maximum identifier length (number of characters)
25296 @tab 255
25297 @tab 32766
25298 @sp 1
25299
25300 @item Maximum number of characters in a source line
25301 @tab 255
25302 @tab 32766
25303 @sp 1
25304
25305 @item Maximum collection size (number of bytes)
25306 @tab 2**31-1
25307 @tab 2**31-1
25308 @sp 1
25309
25310 @item Maximum number of discriminants for a record type
25311 @tab 245
25312 @tab No set limit
25313 @sp 1
25314
25315 @item Maximum number of formal parameters in an entry or
25316 subprogram declaration
25317 @tab 246
25318 @tab No set limit
25319 @sp 1
25320
25321 @item Maximum number of dimensions in an array type
25322 @tab 255
25323 @tab No set limit
25324 @sp 1
25325
25326 @item Maximum number of library units and subunits in a compilation.
25327 @tab 4095
25328 @tab No set limit
25329 @sp 1
25330
25331 @item Maximum number of library units and subunits in an execution.
25332 @tab 16383
25333 @tab No set limit
25334 @sp 1
25335
25336 @item Maximum number of objects declared with the pragma @code{COMMON_OBJECT}
25337 or @code{PSECT_OBJECT}
25338 @tab 32757
25339 @tab No set limit
25340 @sp 1
25341
25342 @item Maximum number of enumeration literals in an enumeration type
25343 definition
25344 @tab 65535
25345 @tab No set limit
25346 @sp 1
25347
25348 @item Maximum number of lines in a source file
25349 @tab 65534
25350 @tab No set limit
25351 @sp 1
25352
25353 @item Maximum number of bits in any object
25354 @tab 2**31-1
25355 @tab 2**31-1
25356 @sp 1
25357
25358 @item Maximum size of the static portion of a stack frame (approximate)
25359 @tab 2**31-1
25360 @tab 2**31-1
25361 @end multitable
25362
25363 @node Tools and Utilities
25364 @section Tools and Utilities
25365
25366 @noindent
25367 The following table lists some of the OpenVMS development tools
25368 available for HP Ada, and the corresponding tools for
25369 use with @value{EDITION} on Alpha and I64 platforms.
25370 Aside from the debugger, all the OpenVMS tools identified are part
25371 of the DECset package.
25372
25373 @iftex
25374 @c Specify table in TeX since Texinfo does a poor job
25375 @tex
25376 \smallskip
25377 \smallskip
25378 \settabs\+Language-Sensitive Editor\quad
25379 &Product with HP Ada\quad
25380 &\cr
25381 \+\it Tool
25382 &\it Product with HP Ada
25383 & \it Product with GNAT Pro\cr
25384 \smallskip
25385 \+Code Management System
25386 &HP CMS
25387 & HP CMS\cr
25388 \smallskip
25389 \+Language-Sensitive Editor
25390 &HP LSE
25391 & emacs or HP LSE (Alpha)\cr
25392 \+
25393 &
25394 & HP LSE (I64)\cr
25395 \smallskip
25396 \+Debugger
25397 &OpenVMS Debug
25398 & gdb (Alpha),\cr
25399 \+
25400 &
25401 & OpenVMS Debug (I64)\cr
25402 \smallskip
25403 \+Source Code Analyzer /
25404 &HP SCA
25405 & GNAT XREF\cr
25406 \+Cross Referencer
25407 &
25408 &\cr
25409 \smallskip
25410 \+Test Manager
25411 &HP Digital Test
25412 & HP DTM\cr
25413 \+
25414 &Manager (DTM)
25415 &\cr
25416 \smallskip
25417 \+Performance and
25418 & HP PCA
25419 & HP PCA\cr
25420 \+Coverage Analyzer
25421 &
25422 &\cr
25423 \smallskip
25424 \+Module Management
25425 & HP MMS
25426 & Not applicable\cr
25427 \+ System
25428 &
25429 &\cr
25430 \smallskip
25431 \smallskip
25432 @end tex
25433 @end iftex
25434
25435 @ifnottex
25436 @c This is the Texinfo version of the table. It renders poorly in pdf, hence
25437 @c the TeX version above for the printed version
25438 @flushleft
25439 @c @multitable @columnfractions .3 .4 .4
25440 @multitable {Source Code Analyzer /}{Tool with HP Ada}{Tool with GNAT Pro}
25441 @item @i{Tool}
25442 @tab @i{Tool with HP Ada}
25443 @tab @i{Tool with @value{EDITION}}
25444 @item Code Management@*System
25445 @tab HP CMS
25446 @tab HP CMS
25447 @item Language-Sensitive@*Editor
25448 @tab HP LSE
25449 @tab emacs or HP LSE (Alpha)
25450 @item
25451 @tab
25452 @tab HP LSE (I64)
25453 @item Debugger
25454 @tab OpenVMS Debug
25455 @tab gdb (Alpha),
25456 @item
25457 @tab
25458 @tab OpenVMS Debug (I64)
25459 @item Source Code Analyzer /@*Cross Referencer
25460 @tab HP SCA
25461 @tab GNAT XREF
25462 @item Test Manager
25463 @tab HP Digital Test@*Manager (DTM)
25464 @tab HP DTM
25465 @item Performance and@*Coverage Analyzer
25466 @tab HP PCA
25467 @tab HP PCA
25468 @item Module Management@*System
25469 @tab HP MMS
25470 @tab Not applicable
25471 @end multitable
25472 @end flushleft
25473 @end ifnottex
25474
25475 @end ifset
25476
25477 @c **************************************
25478 @node Platform-Specific Information for the Run-Time Libraries
25479 @appendix Platform-Specific Information for the Run-Time Libraries
25480 @cindex Tasking and threads libraries
25481 @cindex Threads libraries and tasking
25482 @cindex Run-time libraries (platform-specific information)
25483
25484 @noindent
25485 The GNAT run-time implementation may vary with respect to both the
25486 underlying threads library and the exception handling scheme.
25487 For threads support, one or more of the following are supplied:
25488 @itemize @bullet
25489 @item @b{native threads library}, a binding to the thread package from
25490 the underlying operating system
25491
25492 @item @b{pthreads library} (Sparc Solaris only), a binding to the Solaris
25493 POSIX thread package
25494 @end itemize
25495
25496 @noindent
25497 For exception handling, either or both of two models are supplied:
25498 @itemize @bullet
25499 @item @b{Zero-Cost Exceptions} (``ZCX''),@footnote{
25500 Most programs should experience a substantial speed improvement by
25501 being compiled with a ZCX run-time.
25502 This is especially true for
25503 tasking applications or applications with many exception handlers.}
25504 @cindex Zero-Cost Exceptions
25505 @cindex ZCX (Zero-Cost Exceptions)
25506 which uses binder-generated tables that
25507 are interrogated at run time to locate a handler
25508
25509 @item @b{setjmp / longjmp} (``SJLJ''),
25510 @cindex setjmp/longjmp Exception Model
25511 @cindex SJLJ (setjmp/longjmp Exception Model)
25512 which uses dynamically-set data to establish
25513 the set of handlers
25514 @end itemize
25515
25516 @noindent
25517 This appendix summarizes which combinations of threads and exception support
25518 are supplied on various GNAT platforms.
25519 It then shows how to select a particular library either
25520 permanently or temporarily,
25521 explains the properties of (and tradeoffs among) the various threads
25522 libraries, and provides some additional
25523 information about several specific platforms.
25524
25525 @menu
25526 * Summary of Run-Time Configurations::
25527 * Specifying a Run-Time Library::
25528 * Choosing the Scheduling Policy::
25529 * Solaris-Specific Considerations::
25530 * Linux-Specific Considerations::
25531 * AIX-Specific Considerations::
25532 * Irix-Specific Considerations::
25533 * RTX-Specific Considerations::
25534 @end menu
25535
25536 @node Summary of Run-Time Configurations
25537 @section Summary of Run-Time Configurations
25538
25539 @multitable @columnfractions .30 .70
25540 @item @b{alpha-openvms}
25541 @item @code{@ @ }@i{rts-native (default)}
25542 @item @code{@ @ @ @ }Tasking @tab native VMS threads
25543 @item @code{@ @ @ @ }Exceptions @tab ZCX
25544 @*
25545 @item @b{alpha-tru64}
25546 @item @code{@ @ }@i{rts-native (default)}
25547 @item @code{@ @ @ @ }Tasking @tab native TRU64 threads
25548 @item @code{@ @ @ @ }Exceptions @tab ZCX
25549 @*
25550 @item @code{@ @ }@i{rts-sjlj}
25551 @item @code{@ @ @ @ }Tasking @tab native TRU64 threads
25552 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25553 @*
25554 @item @b{ia64-hp_linux}
25555 @item @code{@ @ }@i{rts-native (default)}
25556 @item @code{@ @ @ @ }Tasking @tab pthread library
25557 @item @code{@ @ @ @ }Exceptions @tab ZCX
25558 @*
25559 @item @b{ia64-hpux}
25560 @item @code{@ @ }@i{rts-native (default)}
25561 @item @code{@ @ @ @ }Tasking @tab native HP-UX threads
25562 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25563 @*
25564 @item @b{ia64-openvms}
25565 @item @code{@ @ }@i{rts-native (default)}
25566 @item @code{@ @ @ @ }Tasking @tab native VMS threads
25567 @item @code{@ @ @ @ }Exceptions @tab ZCX
25568 @*
25569 @item @b{ia64-sgi_linux}
25570 @item @code{@ @ }@i{rts-native (default)}
25571 @item @code{@ @ @ @ }Tasking @tab pthread library
25572 @item @code{@ @ @ @ }Exceptions @tab ZCX
25573 @*
25574 @item @b{mips-irix}
25575 @item @code{@ @ }@i{rts-native (default)}
25576 @item @code{@ @ @ @ }Tasking @tab native IRIX threads
25577 @item @code{@ @ @ @ }Exceptions @tab ZCX
25578 @*
25579 @item @b{pa-hpux}
25580 @item @code{@ @ }@i{rts-native (default)}
25581 @item @code{@ @ @ @ }Tasking @tab native HP-UX threads
25582 @item @code{@ @ @ @ }Exceptions @tab ZCX
25583 @*
25584 @item @code{@ @ }@i{rts-sjlj}
25585 @item @code{@ @ @ @ }Tasking @tab native HP-UX threads
25586 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25587 @*
25588 @item @b{ppc-aix}
25589 @item @code{@ @ }@i{rts-native (default)}
25590 @item @code{@ @ @ @ }Tasking @tab native AIX threads
25591 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25592 @*
25593 @item @b{ppc-darwin}
25594 @item @code{@ @ }@i{rts-native (default)}
25595 @item @code{@ @ @ @ }Tasking @tab native MacOS threads
25596 @item @code{@ @ @ @ }Exceptions @tab ZCX
25597 @*
25598 @item @b{sparc-solaris} @tab
25599 @item @code{@ @ }@i{rts-native (default)}
25600 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
25601 @item @code{@ @ @ @ }Exceptions @tab ZCX
25602 @*
25603 @item @code{@ @ }@i{rts-pthread}
25604 @item @code{@ @ @ @ }Tasking @tab pthread library
25605 @item @code{@ @ @ @ }Exceptions @tab ZCX
25606 @*
25607 @item @code{@ @ }@i{rts-sjlj}
25608 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
25609 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25610 @*
25611 @item @b{sparc64-solaris} @tab
25612 @item @code{@ @ }@i{rts-native (default)}
25613 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
25614 @item @code{@ @ @ @ }Exceptions @tab ZCX
25615 @*
25616 @item @b{x86-linux}
25617 @item @code{@ @ }@i{rts-native (default)}
25618 @item @code{@ @ @ @ }Tasking @tab pthread library
25619 @item @code{@ @ @ @ }Exceptions @tab ZCX
25620 @*
25621 @item @code{@ @ }@i{rts-sjlj}
25622 @item @code{@ @ @ @ }Tasking @tab pthread library
25623 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25624 @*
25625 @item @b{x86-lynx}
25626 @item @code{@ @ }@i{rts-native (default)}
25627 @item @code{@ @ @ @ }Tasking @tab native LynxOS threads
25628 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25629 @*
25630 @item @b{x86-solaris}
25631 @item @code{@ @ }@i{rts-native (default)}
25632 @item @code{@ @ @ @ }Tasking @tab native Solaris threads
25633 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25634 @*
25635 @item @b{x86-windows}
25636 @item @code{@ @ }@i{rts-native (default)}
25637 @item @code{@ @ @ @ }Tasking @tab native Win32 threads
25638 @item @code{@ @ @ @ }Exceptions @tab ZCX
25639 @*
25640 @item @code{@ @ }@i{rts-sjlj (default)}
25641 @item @code{@ @ @ @ }Tasking @tab native Win32 threads
25642 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25643 @*
25644 @item @b{x86-windows-rtx}
25645 @item @code{@ @ }@i{rts-rtx-rtss (default)}
25646 @item @code{@ @ @ @ }Tasking @tab RTX real-time subsystem RTSS threads (kernel mode)
25647 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25648 @*
25649 @item @code{@ @ }@i{rts-rtx-w32}
25650 @item @code{@ @ @ @ }Tasking @tab RTX Win32 threads (user mode)
25651 @item @code{@ @ @ @ }Exceptions @tab ZCX
25652 @*
25653 @item @b{x86_64-linux}
25654 @item @code{@ @ }@i{rts-native (default)}
25655 @item @code{@ @ @ @ }Tasking @tab pthread library
25656 @item @code{@ @ @ @ }Exceptions @tab ZCX
25657 @*
25658 @item @code{@ @ }@i{rts-sjlj}
25659 @item @code{@ @ @ @ }Tasking @tab pthread library
25660 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25661 @*
25662 @end multitable
25663
25664 @node Specifying a Run-Time Library
25665 @section Specifying a Run-Time Library
25666
25667 @noindent
25668 The @file{adainclude} subdirectory containing the sources of the GNAT
25669 run-time library, and the @file{adalib} subdirectory containing the
25670 @file{ALI} files and the static and/or shared GNAT library, are located
25671 in the gcc target-dependent area:
25672
25673 @smallexample
25674 target=$prefix/lib/gcc/gcc-@i{dumpmachine}/gcc-@i{dumpversion}/
25675 @end smallexample
25676
25677 @noindent
25678 As indicated above, on some platforms several run-time libraries are supplied.
25679 These libraries are installed in the target dependent area and
25680 contain a complete source and binary subdirectory. The detailed description
25681 below explains the differences between the different libraries in terms of
25682 their thread support.
25683
25684 The default run-time library (when GNAT is installed) is @emph{rts-native}.
25685 This default run time is selected by the means of soft links.
25686 For example on x86-linux:
25687
25688 @smallexample
25689 @group
25690 $(target-dir)
25691 |
25692 +--- adainclude----------+
25693 | |
25694 +--- adalib-----------+ |
25695 | | |
25696 +--- rts-native | |
25697 | | | |
25698 | +--- adainclude <---+
25699 | | |
25700 | +--- adalib <----+
25701 |
25702 +--- rts-sjlj
25703 |
25704 +--- adainclude
25705 |
25706 +--- adalib
25707 @end group
25708 @end smallexample
25709
25710 @noindent
25711 If the @i{rts-sjlj} library is to be selected on a permanent basis,
25712 these soft links can be modified with the following commands:
25713
25714 @smallexample
25715 $ cd $target
25716 $ rm -f adainclude adalib
25717 $ ln -s rts-sjlj/adainclude adainclude
25718 $ ln -s rts-sjlj/adalib adalib
25719 @end smallexample
25720
25721 @noindent
25722 Alternatively, you can specify @file{rts-sjlj/adainclude} in the file
25723 @file{$target/ada_source_path} and @file{rts-sjlj/adalib} in
25724 @file{$target/ada_object_path}.
25725
25726 Selecting another run-time library temporarily can be
25727 achieved by using the @option{--RTS} switch, e.g., @option{--RTS=sjlj}
25728 @cindex @option{--RTS} option
25729
25730 @node Choosing the Scheduling Policy
25731 @section Choosing the Scheduling Policy
25732
25733 @noindent
25734 When using a POSIX threads implementation, you have a choice of several
25735 scheduling policies: @code{SCHED_FIFO},
25736 @cindex @code{SCHED_FIFO} scheduling policy
25737 @code{SCHED_RR}
25738 @cindex @code{SCHED_RR} scheduling policy
25739 and @code{SCHED_OTHER}.
25740 @cindex @code{SCHED_OTHER} scheduling policy
25741 Typically, the default is @code{SCHED_OTHER}, while using @code{SCHED_FIFO}
25742 or @code{SCHED_RR} requires special (e.g., root) privileges.
25743
25744 By default, GNAT uses the @code{SCHED_OTHER} policy. To specify
25745 @code{SCHED_FIFO},
25746 @cindex @code{SCHED_FIFO} scheduling policy
25747 you can use one of the following:
25748
25749 @itemize @bullet
25750 @item
25751 @code{pragma Time_Slice (0.0)}
25752 @cindex pragma Time_Slice
25753 @item
25754 the corresponding binder option @option{-T0}
25755 @cindex @option{-T0} option
25756 @item
25757 @code{pragma Task_Dispatching_Policy (FIFO_Within_Priorities)}
25758 @cindex pragma Task_Dispatching_Policy
25759 @end itemize
25760
25761 @noindent
25762 To specify @code{SCHED_RR},
25763 @cindex @code{SCHED_RR} scheduling policy
25764 you should use @code{pragma Time_Slice} with a
25765 value greater than @code{0.0}, or else use the corresponding @option{-T}
25766 binder option.
25767
25768 @node Solaris-Specific Considerations
25769 @section Solaris-Specific Considerations
25770 @cindex Solaris Sparc threads libraries
25771
25772 @noindent
25773 This section addresses some topics related to the various threads libraries
25774 on Sparc Solaris.
25775
25776 @menu
25777 * Solaris Threads Issues::
25778 @end menu
25779
25780 @node Solaris Threads Issues
25781 @subsection Solaris Threads Issues
25782
25783 @noindent
25784 GNAT under Solaris/Sparc 32 bits comes with an alternate tasking run-time
25785 library based on POSIX threads --- @emph{rts-pthread}.
25786 @cindex rts-pthread threads library
25787 This run-time library has the advantage of being mostly shared across all
25788 POSIX-compliant thread implementations, and it also provides under
25789 @w{Solaris 8} the @code{PTHREAD_PRIO_INHERIT}
25790 @cindex @code{PTHREAD_PRIO_INHERIT} policy (under rts-pthread)
25791 and @code{PTHREAD_PRIO_PROTECT}
25792 @cindex @code{PTHREAD_PRIO_PROTECT} policy (under rts-pthread)
25793 semantics that can be selected using the predefined pragma
25794 @code{Locking_Policy}
25795 @cindex pragma Locking_Policy (under rts-pthread)
25796 with respectively
25797 @code{Inheritance_Locking} and @code{Ceiling_Locking} as the policy.
25798 @cindex @code{Inheritance_Locking} (under rts-pthread)
25799 @cindex @code{Ceiling_Locking} (under rts-pthread)
25800
25801 As explained above, the native run-time library is based on the Solaris thread
25802 library (@code{libthread}) and is the default library.
25803
25804 When the Solaris threads library is used (this is the default), programs
25805 compiled with GNAT can automatically take advantage of
25806 and can thus execute on multiple processors.
25807 The user can alternatively specify a processor on which the program should run
25808 to emulate a single-processor system. The multiprocessor / uniprocessor choice
25809 is made by
25810 setting the environment variable @env{GNAT_PROCESSOR}
25811 @cindex @env{GNAT_PROCESSOR} environment variable (on Sparc Solaris)
25812 to one of the following:
25813
25814 @table @code
25815 @item -2
25816 Use the default configuration (run the program on all
25817 available processors) - this is the same as having @code{GNAT_PROCESSOR}
25818 unset
25819
25820 @item -1
25821 Let the run-time implementation choose one processor and run the program on
25822 that processor
25823
25824 @item 0 .. Last_Proc
25825 Run the program on the specified processor.
25826 @code{Last_Proc} is equal to @code{_SC_NPROCESSORS_CONF - 1}
25827 (where @code{_SC_NPROCESSORS_CONF} is a system variable).
25828 @end table
25829
25830 @node Linux-Specific Considerations
25831 @section Linux-Specific Considerations
25832 @cindex Linux threads libraries
25833
25834 @noindent
25835 On GNU/Linux without NPTL support (usually system with GNU C Library
25836 older than 2.3), the signal model is not POSIX compliant, which means
25837 that to send a signal to the process, you need to send the signal to all
25838 threads, e.g.@: by using @code{killpg()}.
25839
25840 @node AIX-Specific Considerations
25841 @section AIX-Specific Considerations
25842 @cindex AIX resolver library
25843
25844 @noindent
25845 On AIX, the resolver library initializes some internal structure on
25846 the first call to @code{get*by*} functions, which are used to implement
25847 @code{GNAT.Sockets.Get_Host_By_Name} and
25848 @code{GNAT.Sockets.Get_Host_By_Address}.
25849 If such initialization occurs within an Ada task, and the stack size for
25850 the task is the default size, a stack overflow may occur.
25851
25852 To avoid this overflow, the user should either ensure that the first call
25853 to @code{GNAT.Sockets.Get_Host_By_Name} or
25854 @code{GNAT.Sockets.Get_Host_By_Addrss}
25855 occurs in the environment task, or use @code{pragma Storage_Size} to
25856 specify a sufficiently large size for the stack of the task that contains
25857 this call.
25858
25859 @node Irix-Specific Considerations
25860 @section Irix-Specific Considerations
25861 @cindex Irix libraries
25862
25863 @noindent
25864 The GCC support libraries coming with the Irix compiler have moved to
25865 their canonical place with respect to the general Irix ABI related
25866 conventions. Running applications built with the default shared GNAT
25867 run-time now requires the LD_LIBRARY_PATH environment variable to
25868 include this location. A possible way to achieve this is to issue the
25869 following command line on a bash prompt:
25870
25871 @smallexample
25872 @group
25873 $ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`dirname \`gcc --print-file-name=libgcc_s.so\``
25874 @end group
25875 @end smallexample
25876
25877 @node RTX-Specific Considerations
25878 @section RTX-Specific Considerations
25879 @cindex RTX libraries
25880
25881 @noindent
25882 The Real-time Extension (RTX) to Windows is based on the Windows Win32
25883 API. Applications can be built to work in two different modes:
25884
25885 @itemize @bullet
25886 @item
25887 Windows executables that run in Ring 3 to utilize memory protection
25888 (@emph{rts-rtx-w32}).
25889
25890 @item
25891 Real-time subsystem (RTSS) executables that run in Ring 0, where
25892 performance can be optimized with RTSS applications taking precedent
25893 over all Windows applications (@emph{rts-rtx-rtss}).
25894
25895 @end itemize
25896
25897 @c *******************************
25898 @node Example of Binder Output File
25899 @appendix Example of Binder Output File
25900
25901 @noindent
25902 This Appendix displays the source code for @command{gnatbind}'s output
25903 file generated for a simple ``Hello World'' program.
25904 Comments have been added for clarification purposes.
25905
25906 @smallexample @c adanocomment
25907 @iftex
25908 @leftskip=0cm
25909 @end iftex
25910 -- The package is called Ada_Main unless this name is actually used
25911 -- as a unit name in the partition, in which case some other unique
25912 -- name is used.
25913
25914 with System;
25915 package ada_main is
25916
25917 Elab_Final_Code : Integer;
25918 pragma Import (C, Elab_Final_Code, "__gnat_inside_elab_final_code");
25919
25920 -- The main program saves the parameters (argument count,
25921 -- argument values, environment pointer) in global variables
25922 -- for later access by other units including
25923 -- Ada.Command_Line.
25924
25925 gnat_argc : Integer;
25926 gnat_argv : System.Address;
25927 gnat_envp : System.Address;
25928
25929 -- The actual variables are stored in a library routine. This
25930 -- is useful for some shared library situations, where there
25931 -- are problems if variables are not in the library.
25932
25933 pragma Import (C, gnat_argc);
25934 pragma Import (C, gnat_argv);
25935 pragma Import (C, gnat_envp);
25936
25937 -- The exit status is similarly an external location
25938
25939 gnat_exit_status : Integer;
25940 pragma Import (C, gnat_exit_status);
25941
25942 GNAT_Version : constant String :=
25943 "GNAT Version: 6.0.0w (20061115)";
25944 pragma Export (C, GNAT_Version, "__gnat_version");
25945
25946 -- This is the generated adafinal routine that performs
25947 -- finalization at the end of execution. In the case where
25948 -- Ada is the main program, this main program makes a call
25949 -- to adafinal at program termination.
25950
25951 procedure adafinal;
25952 pragma Export (C, adafinal, "adafinal");
25953
25954 -- This is the generated adainit routine that performs
25955 -- initialization at the start of execution. In the case
25956 -- where Ada is the main program, this main program makes
25957 -- a call to adainit at program startup.
25958
25959 procedure adainit;
25960 pragma Export (C, adainit, "adainit");
25961
25962 -- This routine is called at the start of execution. It is
25963 -- a dummy routine that is used by the debugger to breakpoint
25964 -- at the start of execution.
25965
25966 procedure Break_Start;
25967 pragma Import (C, Break_Start, "__gnat_break_start");
25968
25969 -- This is the actual generated main program (it would be
25970 -- suppressed if the no main program switch were used). As
25971 -- required by standard system conventions, this program has
25972 -- the external name main.
25973
25974 function main
25975 (argc : Integer;
25976 argv : System.Address;
25977 envp : System.Address)
25978 return Integer;
25979 pragma Export (C, main, "main");
25980
25981 -- The following set of constants give the version
25982 -- identification values for every unit in the bound
25983 -- partition. This identification is computed from all
25984 -- dependent semantic units, and corresponds to the
25985 -- string that would be returned by use of the
25986 -- Body_Version or Version attributes.
25987
25988 type Version_32 is mod 2 ** 32;
25989 u00001 : constant Version_32 := 16#7880BEB3#;
25990 u00002 : constant Version_32 := 16#0D24CBD0#;
25991 u00003 : constant Version_32 := 16#3283DBEB#;
25992 u00004 : constant Version_32 := 16#2359F9ED#;
25993 u00005 : constant Version_32 := 16#664FB847#;
25994 u00006 : constant Version_32 := 16#68E803DF#;
25995 u00007 : constant Version_32 := 16#5572E604#;
25996 u00008 : constant Version_32 := 16#46B173D8#;
25997 u00009 : constant Version_32 := 16#156A40CF#;
25998 u00010 : constant Version_32 := 16#033DABE0#;
25999 u00011 : constant Version_32 := 16#6AB38FEA#;
26000 u00012 : constant Version_32 := 16#22B6217D#;
26001 u00013 : constant Version_32 := 16#68A22947#;
26002 u00014 : constant Version_32 := 16#18CC4A56#;
26003 u00015 : constant Version_32 := 16#08258E1B#;
26004 u00016 : constant Version_32 := 16#367D5222#;
26005 u00017 : constant Version_32 := 16#20C9ECA4#;
26006 u00018 : constant Version_32 := 16#50D32CB6#;
26007 u00019 : constant Version_32 := 16#39A8BB77#;
26008 u00020 : constant Version_32 := 16#5CF8FA2B#;
26009 u00021 : constant Version_32 := 16#2F1EB794#;
26010 u00022 : constant Version_32 := 16#31AB6444#;
26011 u00023 : constant Version_32 := 16#1574B6E9#;
26012 u00024 : constant Version_32 := 16#5109C189#;
26013 u00025 : constant Version_32 := 16#56D770CD#;
26014 u00026 : constant Version_32 := 16#02F9DE3D#;
26015 u00027 : constant Version_32 := 16#08AB6B2C#;
26016 u00028 : constant Version_32 := 16#3FA37670#;
26017 u00029 : constant Version_32 := 16#476457A0#;
26018 u00030 : constant Version_32 := 16#731E1B6E#;
26019 u00031 : constant Version_32 := 16#23C2E789#;
26020 u00032 : constant Version_32 := 16#0F1BD6A1#;
26021 u00033 : constant Version_32 := 16#7C25DE96#;
26022 u00034 : constant Version_32 := 16#39ADFFA2#;
26023 u00035 : constant Version_32 := 16#571DE3E7#;
26024 u00036 : constant Version_32 := 16#5EB646AB#;
26025 u00037 : constant Version_32 := 16#4249379B#;
26026 u00038 : constant Version_32 := 16#0357E00A#;
26027 u00039 : constant Version_32 := 16#3784FB72#;
26028 u00040 : constant Version_32 := 16#2E723019#;
26029 u00041 : constant Version_32 := 16#623358EA#;
26030 u00042 : constant Version_32 := 16#107F9465#;
26031 u00043 : constant Version_32 := 16#6843F68A#;
26032 u00044 : constant Version_32 := 16#63305874#;
26033 u00045 : constant Version_32 := 16#31E56CE1#;
26034 u00046 : constant Version_32 := 16#02917970#;
26035 u00047 : constant Version_32 := 16#6CCBA70E#;
26036 u00048 : constant Version_32 := 16#41CD4204#;
26037 u00049 : constant Version_32 := 16#572E3F58#;
26038 u00050 : constant Version_32 := 16#20729FF5#;
26039 u00051 : constant Version_32 := 16#1D4F93E8#;
26040 u00052 : constant Version_32 := 16#30B2EC3D#;
26041 u00053 : constant Version_32 := 16#34054F96#;
26042 u00054 : constant Version_32 := 16#5A199860#;
26043 u00055 : constant Version_32 := 16#0E7F912B#;
26044 u00056 : constant Version_32 := 16#5760634A#;
26045 u00057 : constant Version_32 := 16#5D851835#;
26046
26047 -- The following Export pragmas export the version numbers
26048 -- with symbolic names ending in B (for body) or S
26049 -- (for spec) so that they can be located in a link. The
26050 -- information provided here is sufficient to track down
26051 -- the exact versions of units used in a given build.
26052
26053 pragma Export (C, u00001, "helloB");
26054 pragma Export (C, u00002, "system__standard_libraryB");
26055 pragma Export (C, u00003, "system__standard_libraryS");
26056 pragma Export (C, u00004, "adaS");
26057 pragma Export (C, u00005, "ada__text_ioB");
26058 pragma Export (C, u00006, "ada__text_ioS");
26059 pragma Export (C, u00007, "ada__exceptionsB");
26060 pragma Export (C, u00008, "ada__exceptionsS");
26061 pragma Export (C, u00009, "gnatS");
26062 pragma Export (C, u00010, "gnat__heap_sort_aB");
26063 pragma Export (C, u00011, "gnat__heap_sort_aS");
26064 pragma Export (C, u00012, "systemS");
26065 pragma Export (C, u00013, "system__exception_tableB");
26066 pragma Export (C, u00014, "system__exception_tableS");
26067 pragma Export (C, u00015, "gnat__htableB");
26068 pragma Export (C, u00016, "gnat__htableS");
26069 pragma Export (C, u00017, "system__exceptionsS");
26070 pragma Export (C, u00018, "system__machine_state_operationsB");
26071 pragma Export (C, u00019, "system__machine_state_operationsS");
26072 pragma Export (C, u00020, "system__machine_codeS");
26073 pragma Export (C, u00021, "system__storage_elementsB");
26074 pragma Export (C, u00022, "system__storage_elementsS");
26075 pragma Export (C, u00023, "system__secondary_stackB");
26076 pragma Export (C, u00024, "system__secondary_stackS");
26077 pragma Export (C, u00025, "system__parametersB");
26078 pragma Export (C, u00026, "system__parametersS");
26079 pragma Export (C, u00027, "system__soft_linksB");
26080 pragma Export (C, u00028, "system__soft_linksS");
26081 pragma Export (C, u00029, "system__stack_checkingB");
26082 pragma Export (C, u00030, "system__stack_checkingS");
26083 pragma Export (C, u00031, "system__tracebackB");
26084 pragma Export (C, u00032, "system__tracebackS");
26085 pragma Export (C, u00033, "ada__streamsS");
26086 pragma Export (C, u00034, "ada__tagsB");
26087 pragma Export (C, u00035, "ada__tagsS");
26088 pragma Export (C, u00036, "system__string_opsB");
26089 pragma Export (C, u00037, "system__string_opsS");
26090 pragma Export (C, u00038, "interfacesS");
26091 pragma Export (C, u00039, "interfaces__c_streamsB");
26092 pragma Export (C, u00040, "interfaces__c_streamsS");
26093 pragma Export (C, u00041, "system__file_ioB");
26094 pragma Export (C, u00042, "system__file_ioS");
26095 pragma Export (C, u00043, "ada__finalizationB");
26096 pragma Export (C, u00044, "ada__finalizationS");
26097 pragma Export (C, u00045, "system__finalization_rootB");
26098 pragma Export (C, u00046, "system__finalization_rootS");
26099 pragma Export (C, u00047, "system__finalization_implementationB");
26100 pragma Export (C, u00048, "system__finalization_implementationS");
26101 pragma Export (C, u00049, "system__string_ops_concat_3B");
26102 pragma Export (C, u00050, "system__string_ops_concat_3S");
26103 pragma Export (C, u00051, "system__stream_attributesB");
26104 pragma Export (C, u00052, "system__stream_attributesS");
26105 pragma Export (C, u00053, "ada__io_exceptionsS");
26106 pragma Export (C, u00054, "system__unsigned_typesS");
26107 pragma Export (C, u00055, "system__file_control_blockS");
26108 pragma Export (C, u00056, "ada__finalization__list_controllerB");
26109 pragma Export (C, u00057, "ada__finalization__list_controllerS");
26110
26111 -- BEGIN ELABORATION ORDER
26112 -- ada (spec)
26113 -- gnat (spec)
26114 -- gnat.heap_sort_a (spec)
26115 -- gnat.heap_sort_a (body)
26116 -- gnat.htable (spec)
26117 -- gnat.htable (body)
26118 -- interfaces (spec)
26119 -- system (spec)
26120 -- system.machine_code (spec)
26121 -- system.parameters (spec)
26122 -- system.parameters (body)
26123 -- interfaces.c_streams (spec)
26124 -- interfaces.c_streams (body)
26125 -- system.standard_library (spec)
26126 -- ada.exceptions (spec)
26127 -- system.exception_table (spec)
26128 -- system.exception_table (body)
26129 -- ada.io_exceptions (spec)
26130 -- system.exceptions (spec)
26131 -- system.storage_elements (spec)
26132 -- system.storage_elements (body)
26133 -- system.machine_state_operations (spec)
26134 -- system.machine_state_operations (body)
26135 -- system.secondary_stack (spec)
26136 -- system.stack_checking (spec)
26137 -- system.soft_links (spec)
26138 -- system.soft_links (body)
26139 -- system.stack_checking (body)
26140 -- system.secondary_stack (body)
26141 -- system.standard_library (body)
26142 -- system.string_ops (spec)
26143 -- system.string_ops (body)
26144 -- ada.tags (spec)
26145 -- ada.tags (body)
26146 -- ada.streams (spec)
26147 -- system.finalization_root (spec)
26148 -- system.finalization_root (body)
26149 -- system.string_ops_concat_3 (spec)
26150 -- system.string_ops_concat_3 (body)
26151 -- system.traceback (spec)
26152 -- system.traceback (body)
26153 -- ada.exceptions (body)
26154 -- system.unsigned_types (spec)
26155 -- system.stream_attributes (spec)
26156 -- system.stream_attributes (body)
26157 -- system.finalization_implementation (spec)
26158 -- system.finalization_implementation (body)
26159 -- ada.finalization (spec)
26160 -- ada.finalization (body)
26161 -- ada.finalization.list_controller (spec)
26162 -- ada.finalization.list_controller (body)
26163 -- system.file_control_block (spec)
26164 -- system.file_io (spec)
26165 -- system.file_io (body)
26166 -- ada.text_io (spec)
26167 -- ada.text_io (body)
26168 -- hello (body)
26169 -- END ELABORATION ORDER
26170
26171 end ada_main;
26172
26173 -- The following source file name pragmas allow the generated file
26174 -- names to be unique for different main programs. They are needed
26175 -- since the package name will always be Ada_Main.
26176
26177 pragma Source_File_Name (ada_main, Spec_File_Name => "b~hello.ads");
26178 pragma Source_File_Name (ada_main, Body_File_Name => "b~hello.adb");
26179
26180 -- Generated package body for Ada_Main starts here
26181
26182 package body ada_main is
26183
26184 -- The actual finalization is performed by calling the
26185 -- library routine in System.Standard_Library.Adafinal
26186
26187 procedure Do_Finalize;
26188 pragma Import (C, Do_Finalize, "system__standard_library__adafinal");
26189
26190 -------------
26191 -- adainit --
26192 -------------
26193
26194 @findex adainit
26195 procedure adainit is
26196
26197 -- These booleans are set to True once the associated unit has
26198 -- been elaborated. It is also used to avoid elaborating the
26199 -- same unit twice.
26200
26201 E040 : Boolean;
26202 pragma Import (Ada, E040, "interfaces__c_streams_E");
26203
26204 E008 : Boolean;
26205 pragma Import (Ada, E008, "ada__exceptions_E");
26206
26207 E014 : Boolean;
26208 pragma Import (Ada, E014, "system__exception_table_E");
26209
26210 E053 : Boolean;
26211 pragma Import (Ada, E053, "ada__io_exceptions_E");
26212
26213 E017 : Boolean;
26214 pragma Import (Ada, E017, "system__exceptions_E");
26215
26216 E024 : Boolean;
26217 pragma Import (Ada, E024, "system__secondary_stack_E");
26218
26219 E030 : Boolean;
26220 pragma Import (Ada, E030, "system__stack_checking_E");
26221
26222 E028 : Boolean;
26223 pragma Import (Ada, E028, "system__soft_links_E");
26224
26225 E035 : Boolean;
26226 pragma Import (Ada, E035, "ada__tags_E");
26227
26228 E033 : Boolean;
26229 pragma Import (Ada, E033, "ada__streams_E");
26230
26231 E046 : Boolean;
26232 pragma Import (Ada, E046, "system__finalization_root_E");
26233
26234 E048 : Boolean;
26235 pragma Import (Ada, E048, "system__finalization_implementation_E");
26236
26237 E044 : Boolean;
26238 pragma Import (Ada, E044, "ada__finalization_E");
26239
26240 E057 : Boolean;
26241 pragma Import (Ada, E057, "ada__finalization__list_controller_E");
26242
26243 E055 : Boolean;
26244 pragma Import (Ada, E055, "system__file_control_block_E");
26245
26246 E042 : Boolean;
26247 pragma Import (Ada, E042, "system__file_io_E");
26248
26249 E006 : Boolean;
26250 pragma Import (Ada, E006, "ada__text_io_E");
26251
26252 -- Set_Globals is a library routine that stores away the
26253 -- value of the indicated set of global values in global
26254 -- variables within the library.
26255
26256 procedure Set_Globals
26257 (Main_Priority : Integer;
26258 Time_Slice_Value : Integer;
26259 WC_Encoding : Character;
26260 Locking_Policy : Character;
26261 Queuing_Policy : Character;
26262 Task_Dispatching_Policy : Character;
26263 Adafinal : System.Address;
26264 Unreserve_All_Interrupts : Integer;
26265 Exception_Tracebacks : Integer);
26266 @findex __gnat_set_globals
26267 pragma Import (C, Set_Globals, "__gnat_set_globals");
26268
26269 -- SDP_Table_Build is a library routine used to build the
26270 -- exception tables. See unit Ada.Exceptions in files
26271 -- a-except.ads/adb for full details of how zero cost
26272 -- exception handling works. This procedure, the call to
26273 -- it, and the two following tables are all omitted if the
26274 -- build is in longjmp/setjmp exception mode.
26275
26276 @findex SDP_Table_Build
26277 @findex Zero Cost Exceptions
26278 procedure SDP_Table_Build
26279 (SDP_Addresses : System.Address;
26280 SDP_Count : Natural;
26281 Elab_Addresses : System.Address;
26282 Elab_Addr_Count : Natural);
26283 pragma Import (C, SDP_Table_Build, "__gnat_SDP_Table_Build");
26284
26285 -- Table of Unit_Exception_Table addresses. Used for zero
26286 -- cost exception handling to build the top level table.
26287
26288 ST : aliased constant array (1 .. 23) of System.Address := (
26289 Hello'UET_Address,
26290 Ada.Text_Io'UET_Address,
26291 Ada.Exceptions'UET_Address,
26292 Gnat.Heap_Sort_A'UET_Address,
26293 System.Exception_Table'UET_Address,
26294 System.Machine_State_Operations'UET_Address,
26295 System.Secondary_Stack'UET_Address,
26296 System.Parameters'UET_Address,
26297 System.Soft_Links'UET_Address,
26298 System.Stack_Checking'UET_Address,
26299 System.Traceback'UET_Address,
26300 Ada.Streams'UET_Address,
26301 Ada.Tags'UET_Address,
26302 System.String_Ops'UET_Address,
26303 Interfaces.C_Streams'UET_Address,
26304 System.File_Io'UET_Address,
26305 Ada.Finalization'UET_Address,
26306 System.Finalization_Root'UET_Address,
26307 System.Finalization_Implementation'UET_Address,
26308 System.String_Ops_Concat_3'UET_Address,
26309 System.Stream_Attributes'UET_Address,
26310 System.File_Control_Block'UET_Address,
26311 Ada.Finalization.List_Controller'UET_Address);
26312
26313 -- Table of addresses of elaboration routines. Used for
26314 -- zero cost exception handling to make sure these
26315 -- addresses are included in the top level procedure
26316 -- address table.
26317
26318 EA : aliased constant array (1 .. 23) of System.Address := (
26319 adainit'Code_Address,
26320 Do_Finalize'Code_Address,
26321 Ada.Exceptions'Elab_Spec'Address,
26322 System.Exceptions'Elab_Spec'Address,
26323 Interfaces.C_Streams'Elab_Spec'Address,
26324 System.Exception_Table'Elab_Body'Address,
26325 Ada.Io_Exceptions'Elab_Spec'Address,
26326 System.Stack_Checking'Elab_Spec'Address,
26327 System.Soft_Links'Elab_Body'Address,
26328 System.Secondary_Stack'Elab_Body'Address,
26329 Ada.Tags'Elab_Spec'Address,
26330 Ada.Tags'Elab_Body'Address,
26331 Ada.Streams'Elab_Spec'Address,
26332 System.Finalization_Root'Elab_Spec'Address,
26333 Ada.Exceptions'Elab_Body'Address,
26334 System.Finalization_Implementation'Elab_Spec'Address,
26335 System.Finalization_Implementation'Elab_Body'Address,
26336 Ada.Finalization'Elab_Spec'Address,
26337 Ada.Finalization.List_Controller'Elab_Spec'Address,
26338 System.File_Control_Block'Elab_Spec'Address,
26339 System.File_Io'Elab_Body'Address,
26340 Ada.Text_Io'Elab_Spec'Address,
26341 Ada.Text_Io'Elab_Body'Address);
26342
26343 -- Start of processing for adainit
26344
26345 begin
26346
26347 -- Call SDP_Table_Build to build the top level procedure
26348 -- table for zero cost exception handling (omitted in
26349 -- longjmp/setjmp mode).
26350
26351 SDP_Table_Build (ST'Address, 23, EA'Address, 23);
26352
26353 -- Call Set_Globals to record various information for
26354 -- this partition. The values are derived by the binder
26355 -- from information stored in the ali files by the compiler.
26356
26357 @findex __gnat_set_globals
26358 Set_Globals
26359 (Main_Priority => -1,
26360 -- Priority of main program, -1 if no pragma Priority used
26361
26362 Time_Slice_Value => -1,
26363 -- Time slice from Time_Slice pragma, -1 if none used
26364
26365 WC_Encoding => 'b',
26366 -- Wide_Character encoding used, default is brackets
26367
26368 Locking_Policy => ' ',
26369 -- Locking_Policy used, default of space means not
26370 -- specified, otherwise it is the first character of
26371 -- the policy name.
26372
26373 Queuing_Policy => ' ',
26374 -- Queuing_Policy used, default of space means not
26375 -- specified, otherwise it is the first character of
26376 -- the policy name.
26377
26378 Task_Dispatching_Policy => ' ',
26379 -- Task_Dispatching_Policy used, default of space means
26380 -- not specified, otherwise first character of the
26381 -- policy name.
26382
26383 Adafinal => System.Null_Address,
26384 -- Address of Adafinal routine, not used anymore
26385
26386 Unreserve_All_Interrupts => 0,
26387 -- Set true if pragma Unreserve_All_Interrupts was used
26388
26389 Exception_Tracebacks => 0);
26390 -- Indicates if exception tracebacks are enabled
26391
26392 Elab_Final_Code := 1;
26393
26394 -- Now we have the elaboration calls for all units in the partition.
26395 -- The Elab_Spec and Elab_Body attributes generate references to the
26396 -- implicit elaboration procedures generated by the compiler for
26397 -- each unit that requires elaboration.
26398
26399 if not E040 then
26400 Interfaces.C_Streams'Elab_Spec;
26401 end if;
26402 E040 := True;
26403 if not E008 then
26404 Ada.Exceptions'Elab_Spec;
26405 end if;
26406 if not E014 then
26407 System.Exception_Table'Elab_Body;
26408 E014 := True;
26409 end if;
26410 if not E053 then
26411 Ada.Io_Exceptions'Elab_Spec;
26412 E053 := True;
26413 end if;
26414 if not E017 then
26415 System.Exceptions'Elab_Spec;
26416 E017 := True;
26417 end if;
26418 if not E030 then
26419 System.Stack_Checking'Elab_Spec;
26420 end if;
26421 if not E028 then
26422 System.Soft_Links'Elab_Body;
26423 E028 := True;
26424 end if;
26425 E030 := True;
26426 if not E024 then
26427 System.Secondary_Stack'Elab_Body;
26428 E024 := True;
26429 end if;
26430 if not E035 then
26431 Ada.Tags'Elab_Spec;
26432 end if;
26433 if not E035 then
26434 Ada.Tags'Elab_Body;
26435 E035 := True;
26436 end if;
26437 if not E033 then
26438 Ada.Streams'Elab_Spec;
26439 E033 := True;
26440 end if;
26441 if not E046 then
26442 System.Finalization_Root'Elab_Spec;
26443 end if;
26444 E046 := True;
26445 if not E008 then
26446 Ada.Exceptions'Elab_Body;
26447 E008 := True;
26448 end if;
26449 if not E048 then
26450 System.Finalization_Implementation'Elab_Spec;
26451 end if;
26452 if not E048 then
26453 System.Finalization_Implementation'Elab_Body;
26454 E048 := True;
26455 end if;
26456 if not E044 then
26457 Ada.Finalization'Elab_Spec;
26458 end if;
26459 E044 := True;
26460 if not E057 then
26461 Ada.Finalization.List_Controller'Elab_Spec;
26462 end if;
26463 E057 := True;
26464 if not E055 then
26465 System.File_Control_Block'Elab_Spec;
26466 E055 := True;
26467 end if;
26468 if not E042 then
26469 System.File_Io'Elab_Body;
26470 E042 := True;
26471 end if;
26472 if not E006 then
26473 Ada.Text_Io'Elab_Spec;
26474 end if;
26475 if not E006 then
26476 Ada.Text_Io'Elab_Body;
26477 E006 := True;
26478 end if;
26479
26480 Elab_Final_Code := 0;
26481 end adainit;
26482
26483 --------------
26484 -- adafinal --
26485 --------------
26486
26487 @findex adafinal
26488 procedure adafinal is
26489 begin
26490 Do_Finalize;
26491 end adafinal;
26492
26493 ----------
26494 -- main --
26495 ----------
26496
26497 -- main is actually a function, as in the ANSI C standard,
26498 -- defined to return the exit status. The three parameters
26499 -- are the argument count, argument values and environment
26500 -- pointer.
26501
26502 @findex Main Program
26503 function main
26504 (argc : Integer;
26505 argv : System.Address;
26506 envp : System.Address)
26507 return Integer
26508 is
26509 -- The initialize routine performs low level system
26510 -- initialization using a standard library routine which
26511 -- sets up signal handling and performs any other
26512 -- required setup. The routine can be found in file
26513 -- a-init.c.
26514
26515 @findex __gnat_initialize
26516 procedure initialize;
26517 pragma Import (C, initialize, "__gnat_initialize");
26518
26519 -- The finalize routine performs low level system
26520 -- finalization using a standard library routine. The
26521 -- routine is found in file a-final.c and in the standard
26522 -- distribution is a dummy routine that does nothing, so
26523 -- really this is a hook for special user finalization.
26524
26525 @findex __gnat_finalize
26526 procedure finalize;
26527 pragma Import (C, finalize, "__gnat_finalize");
26528
26529 -- We get to the main program of the partition by using
26530 -- pragma Import because if we try to with the unit and
26531 -- call it Ada style, then not only do we waste time
26532 -- recompiling it, but also, we don't really know the right
26533 -- switches (e.g.@: identifier character set) to be used
26534 -- to compile it.
26535
26536 procedure Ada_Main_Program;
26537 pragma Import (Ada, Ada_Main_Program, "_ada_hello");
26538
26539 -- Start of processing for main
26540
26541 begin
26542 -- Save global variables
26543
26544 gnat_argc := argc;
26545 gnat_argv := argv;
26546 gnat_envp := envp;
26547
26548 -- Call low level system initialization
26549
26550 Initialize;
26551
26552 -- Call our generated Ada initialization routine
26553
26554 adainit;
26555
26556 -- This is the point at which we want the debugger to get
26557 -- control
26558
26559 Break_Start;
26560
26561 -- Now we call the main program of the partition
26562
26563 Ada_Main_Program;
26564
26565 -- Perform Ada finalization
26566
26567 adafinal;
26568
26569 -- Perform low level system finalization
26570
26571 Finalize;
26572
26573 -- Return the proper exit status
26574 return (gnat_exit_status);
26575 end;
26576
26577 -- This section is entirely comments, so it has no effect on the
26578 -- compilation of the Ada_Main package. It provides the list of
26579 -- object files and linker options, as well as some standard
26580 -- libraries needed for the link. The gnatlink utility parses
26581 -- this b~hello.adb file to read these comment lines to generate
26582 -- the appropriate command line arguments for the call to the
26583 -- system linker. The BEGIN/END lines are used for sentinels for
26584 -- this parsing operation.
26585
26586 -- The exact file names will of course depend on the environment,
26587 -- host/target and location of files on the host system.
26588
26589 @findex Object file list
26590 -- BEGIN Object file/option list
26591 -- ./hello.o
26592 -- -L./
26593 -- -L/usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/
26594 -- /usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a
26595 -- END Object file/option list
26596
26597 end ada_main;
26598 @end smallexample
26599
26600 @noindent
26601 The Ada code in the above example is exactly what is generated by the
26602 binder. We have added comments to more clearly indicate the function
26603 of each part of the generated @code{Ada_Main} package.
26604
26605 The code is standard Ada in all respects, and can be processed by any
26606 tools that handle Ada. In particular, it is possible to use the debugger
26607 in Ada mode to debug the generated @code{Ada_Main} package. For example,
26608 suppose that for reasons that you do not understand, your program is crashing
26609 during elaboration of the body of @code{Ada.Text_IO}. To locate this bug,
26610 you can place a breakpoint on the call:
26611
26612 @smallexample @c ada
26613 Ada.Text_Io'Elab_Body;
26614 @end smallexample
26615
26616 @noindent
26617 and trace the elaboration routine for this package to find out where
26618 the problem might be (more usually of course you would be debugging
26619 elaboration code in your own application).
26620
26621 @node Elaboration Order Handling in GNAT
26622 @appendix Elaboration Order Handling in GNAT
26623 @cindex Order of elaboration
26624 @cindex Elaboration control
26625
26626 @menu
26627 * Elaboration Code::
26628 * Checking the Elaboration Order::
26629 * Controlling the Elaboration Order::
26630 * Controlling Elaboration in GNAT - Internal Calls::
26631 * Controlling Elaboration in GNAT - External Calls::
26632 * Default Behavior in GNAT - Ensuring Safety::
26633 * Treatment of Pragma Elaborate::
26634 * Elaboration Issues for Library Tasks::
26635 * Mixing Elaboration Models::
26636 * What to Do If the Default Elaboration Behavior Fails::
26637 * Elaboration for Access-to-Subprogram Values::
26638 * Summary of Procedures for Elaboration Control::
26639 * Other Elaboration Order Considerations::
26640 @end menu
26641
26642 @noindent
26643 This chapter describes the handling of elaboration code in Ada and
26644 in GNAT, and discusses how the order of elaboration of program units can
26645 be controlled in GNAT, either automatically or with explicit programming
26646 features.
26647
26648 @node Elaboration Code
26649 @section Elaboration Code
26650
26651 @noindent
26652 Ada provides rather general mechanisms for executing code at elaboration
26653 time, that is to say before the main program starts executing. Such code arises
26654 in three contexts:
26655
26656 @table @asis
26657 @item Initializers for variables.
26658 Variables declared at the library level, in package specs or bodies, can
26659 require initialization that is performed at elaboration time, as in:
26660 @smallexample @c ada
26661 @cartouche
26662 Sqrt_Half : Float := Sqrt (0.5);
26663 @end cartouche
26664 @end smallexample
26665
26666 @item Package initialization code
26667 Code in a @code{BEGIN-END} section at the outer level of a package body is
26668 executed as part of the package body elaboration code.
26669
26670 @item Library level task allocators
26671 Tasks that are declared using task allocators at the library level
26672 start executing immediately and hence can execute at elaboration time.
26673 @end table
26674
26675 @noindent
26676 Subprogram calls are possible in any of these contexts, which means that
26677 any arbitrary part of the program may be executed as part of the elaboration
26678 code. It is even possible to write a program which does all its work at
26679 elaboration time, with a null main program, although stylistically this
26680 would usually be considered an inappropriate way to structure
26681 a program.
26682
26683 An important concern arises in the context of elaboration code:
26684 we have to be sure that it is executed in an appropriate order. What we
26685 have is a series of elaboration code sections, potentially one section
26686 for each unit in the program. It is important that these execute
26687 in the correct order. Correctness here means that, taking the above
26688 example of the declaration of @code{Sqrt_Half},
26689 if some other piece of
26690 elaboration code references @code{Sqrt_Half},
26691 then it must run after the
26692 section of elaboration code that contains the declaration of
26693 @code{Sqrt_Half}.
26694
26695 There would never be any order of elaboration problem if we made a rule
26696 that whenever you @code{with} a unit, you must elaborate both the spec and body
26697 of that unit before elaborating the unit doing the @code{with}'ing:
26698
26699 @smallexample @c ada
26700 @group
26701 @cartouche
26702 with Unit_1;
26703 package Unit_2 is @dots{}
26704 @end cartouche
26705 @end group
26706 @end smallexample
26707
26708 @noindent
26709 would require that both the body and spec of @code{Unit_1} be elaborated
26710 before the spec of @code{Unit_2}. However, a rule like that would be far too
26711 restrictive. In particular, it would make it impossible to have routines
26712 in separate packages that were mutually recursive.
26713
26714 You might think that a clever enough compiler could look at the actual
26715 elaboration code and determine an appropriate correct order of elaboration,
26716 but in the general case, this is not possible. Consider the following
26717 example.
26718
26719 In the body of @code{Unit_1}, we have a procedure @code{Func_1}
26720 that references
26721 the variable @code{Sqrt_1}, which is declared in the elaboration code
26722 of the body of @code{Unit_1}:
26723
26724 @smallexample @c ada
26725 @cartouche
26726 Sqrt_1 : Float := Sqrt (0.1);
26727 @end cartouche
26728 @end smallexample
26729
26730 @noindent
26731 The elaboration code of the body of @code{Unit_1} also contains:
26732
26733 @smallexample @c ada
26734 @group
26735 @cartouche
26736 if expression_1 = 1 then
26737 Q := Unit_2.Func_2;
26738 end if;
26739 @end cartouche
26740 @end group
26741 @end smallexample
26742
26743 @noindent
26744 @code{Unit_2} is exactly parallel,
26745 it has a procedure @code{Func_2} that references
26746 the variable @code{Sqrt_2}, which is declared in the elaboration code of
26747 the body @code{Unit_2}:
26748
26749 @smallexample @c ada
26750 @cartouche
26751 Sqrt_2 : Float := Sqrt (0.1);
26752 @end cartouche
26753 @end smallexample
26754
26755 @noindent
26756 The elaboration code of the body of @code{Unit_2} also contains:
26757
26758 @smallexample @c ada
26759 @group
26760 @cartouche
26761 if expression_2 = 2 then
26762 Q := Unit_1.Func_1;
26763 end if;
26764 @end cartouche
26765 @end group
26766 @end smallexample
26767
26768 @noindent
26769 Now the question is, which of the following orders of elaboration is
26770 acceptable:
26771
26772 @smallexample
26773 @group
26774 Spec of Unit_1
26775 Spec of Unit_2
26776 Body of Unit_1
26777 Body of Unit_2
26778 @end group
26779 @end smallexample
26780
26781 @noindent
26782 or
26783
26784 @smallexample
26785 @group
26786 Spec of Unit_2
26787 Spec of Unit_1
26788 Body of Unit_2
26789 Body of Unit_1
26790 @end group
26791 @end smallexample
26792
26793 @noindent
26794 If you carefully analyze the flow here, you will see that you cannot tell
26795 at compile time the answer to this question.
26796 If @code{expression_1} is not equal to 1,
26797 and @code{expression_2} is not equal to 2,
26798 then either order is acceptable, because neither of the function calls is
26799 executed. If both tests evaluate to true, then neither order is acceptable
26800 and in fact there is no correct order.
26801
26802 If one of the two expressions is true, and the other is false, then one
26803 of the above orders is correct, and the other is incorrect. For example,
26804 if @code{expression_1} /= 1 and @code{expression_2} = 2,
26805 then the call to @code{Func_1}
26806 will occur, but not the call to @code{Func_2.}
26807 This means that it is essential
26808 to elaborate the body of @code{Unit_1} before
26809 the body of @code{Unit_2}, so the first
26810 order of elaboration is correct and the second is wrong.
26811
26812 By making @code{expression_1} and @code{expression_2}
26813 depend on input data, or perhaps
26814 the time of day, we can make it impossible for the compiler or binder
26815 to figure out which of these expressions will be true, and hence it
26816 is impossible to guarantee a safe order of elaboration at run time.
26817
26818 @node Checking the Elaboration Order
26819 @section Checking the Elaboration Order
26820
26821 @noindent
26822 In some languages that involve the same kind of elaboration problems,
26823 e.g.@: Java and C++, the programmer is expected to worry about these
26824 ordering problems himself, and it is common to
26825 write a program in which an incorrect elaboration order gives
26826 surprising results, because it references variables before they
26827 are initialized.
26828 Ada is designed to be a safe language, and a programmer-beware approach is
26829 clearly not sufficient. Consequently, the language provides three lines
26830 of defense:
26831
26832 @table @asis
26833 @item Standard rules
26834 Some standard rules restrict the possible choice of elaboration
26835 order. In particular, if you @code{with} a unit, then its spec is always
26836 elaborated before the unit doing the @code{with}. Similarly, a parent
26837 spec is always elaborated before the child spec, and finally
26838 a spec is always elaborated before its corresponding body.
26839
26840 @item Dynamic elaboration checks
26841 @cindex Elaboration checks
26842 @cindex Checks, elaboration
26843 Dynamic checks are made at run time, so that if some entity is accessed
26844 before it is elaborated (typically by means of a subprogram call)
26845 then the exception (@code{Program_Error}) is raised.
26846
26847 @item Elaboration control
26848 Facilities are provided for the programmer to specify the desired order
26849 of elaboration.
26850 @end table
26851
26852 Let's look at these facilities in more detail. First, the rules for
26853 dynamic checking. One possible rule would be simply to say that the
26854 exception is raised if you access a variable which has not yet been
26855 elaborated. The trouble with this approach is that it could require
26856 expensive checks on every variable reference. Instead Ada has two
26857 rules which are a little more restrictive, but easier to check, and
26858 easier to state:
26859
26860 @table @asis
26861 @item Restrictions on calls
26862 A subprogram can only be called at elaboration time if its body
26863 has been elaborated. The rules for elaboration given above guarantee
26864 that the spec of the subprogram has been elaborated before the
26865 call, but not the body. If this rule is violated, then the
26866 exception @code{Program_Error} is raised.
26867
26868 @item Restrictions on instantiations
26869 A generic unit can only be instantiated if the body of the generic
26870 unit has been elaborated. Again, the rules for elaboration given above
26871 guarantee that the spec of the generic unit has been elaborated
26872 before the instantiation, but not the body. If this rule is
26873 violated, then the exception @code{Program_Error} is raised.
26874 @end table
26875
26876 @noindent
26877 The idea is that if the body has been elaborated, then any variables
26878 it references must have been elaborated; by checking for the body being
26879 elaborated we guarantee that none of its references causes any
26880 trouble. As we noted above, this is a little too restrictive, because a
26881 subprogram that has no non-local references in its body may in fact be safe
26882 to call. However, it really would be unsafe to rely on this, because
26883 it would mean that the caller was aware of details of the implementation
26884 in the body. This goes against the basic tenets of Ada.
26885
26886 A plausible implementation can be described as follows.
26887 A Boolean variable is associated with each subprogram
26888 and each generic unit. This variable is initialized to False, and is set to
26889 True at the point body is elaborated. Every call or instantiation checks the
26890 variable, and raises @code{Program_Error} if the variable is False.
26891
26892 Note that one might think that it would be good enough to have one Boolean
26893 variable for each package, but that would not deal with cases of trying
26894 to call a body in the same package as the call
26895 that has not been elaborated yet.
26896 Of course a compiler may be able to do enough analysis to optimize away
26897 some of the Boolean variables as unnecessary, and @code{GNAT} indeed
26898 does such optimizations, but still the easiest conceptual model is to
26899 think of there being one variable per subprogram.
26900
26901 @node Controlling the Elaboration Order
26902 @section Controlling the Elaboration Order
26903
26904 @noindent
26905 In the previous section we discussed the rules in Ada which ensure
26906 that @code{Program_Error} is raised if an incorrect elaboration order is
26907 chosen. This prevents erroneous executions, but we need mechanisms to
26908 specify a correct execution and avoid the exception altogether.
26909 To achieve this, Ada provides a number of features for controlling
26910 the order of elaboration. We discuss these features in this section.
26911
26912 First, there are several ways of indicating to the compiler that a given
26913 unit has no elaboration problems:
26914
26915 @table @asis
26916 @item packages that do not require a body
26917 A library package that does not require a body does not permit
26918 a body (this rule was introduced in Ada 95).
26919 Thus if we have a such a package, as in:
26920
26921 @smallexample @c ada
26922 @group
26923 @cartouche
26924 package Definitions is
26925 generic
26926 type m is new integer;
26927 package Subp is
26928 type a is array (1 .. 10) of m;
26929 type b is array (1 .. 20) of m;
26930 end Subp;
26931 end Definitions;
26932 @end cartouche
26933 @end group
26934 @end smallexample
26935
26936 @noindent
26937 A package that @code{with}'s @code{Definitions} may safely instantiate
26938 @code{Definitions.Subp} because the compiler can determine that there
26939 definitely is no package body to worry about in this case
26940
26941 @item pragma Pure
26942 @cindex pragma Pure
26943 @findex Pure
26944 Places sufficient restrictions on a unit to guarantee that
26945 no call to any subprogram in the unit can result in an
26946 elaboration problem. This means that the compiler does not need
26947 to worry about the point of elaboration of such units, and in
26948 particular, does not need to check any calls to any subprograms
26949 in this unit.
26950
26951 @item pragma Preelaborate
26952 @findex Preelaborate
26953 @cindex pragma Preelaborate
26954 This pragma places slightly less stringent restrictions on a unit than
26955 does pragma Pure,
26956 but these restrictions are still sufficient to ensure that there
26957 are no elaboration problems with any calls to the unit.
26958
26959 @item pragma Elaborate_Body
26960 @findex Elaborate_Body
26961 @cindex pragma Elaborate_Body
26962 This pragma requires that the body of a unit be elaborated immediately
26963 after its spec. Suppose a unit @code{A} has such a pragma,
26964 and unit @code{B} does
26965 a @code{with} of unit @code{A}. Recall that the standard rules require
26966 the spec of unit @code{A}
26967 to be elaborated before the @code{with}'ing unit; given the pragma in
26968 @code{A}, we also know that the body of @code{A}
26969 will be elaborated before @code{B}, so
26970 that calls to @code{A} are safe and do not need a check.
26971 @end table
26972
26973 @noindent
26974 Note that,
26975 unlike pragma @code{Pure} and pragma @code{Preelaborate},
26976 the use of
26977 @code{Elaborate_Body} does not guarantee that the program is
26978 free of elaboration problems, because it may not be possible
26979 to satisfy the requested elaboration order.
26980 Let's go back to the example with @code{Unit_1} and @code{Unit_2}.
26981 If a programmer
26982 marks @code{Unit_1} as @code{Elaborate_Body},
26983 and not @code{Unit_2,} then the order of
26984 elaboration will be:
26985
26986 @smallexample
26987 @group
26988 Spec of Unit_2
26989 Spec of Unit_1
26990 Body of Unit_1
26991 Body of Unit_2
26992 @end group
26993 @end smallexample
26994
26995 @noindent
26996 Now that means that the call to @code{Func_1} in @code{Unit_2}
26997 need not be checked,
26998 it must be safe. But the call to @code{Func_2} in
26999 @code{Unit_1} may still fail if
27000 @code{Expression_1} is equal to 1,
27001 and the programmer must still take
27002 responsibility for this not being the case.
27003
27004 If all units carry a pragma @code{Elaborate_Body}, then all problems are
27005 eliminated, except for calls entirely within a body, which are
27006 in any case fully under programmer control. However, using the pragma
27007 everywhere is not always possible.
27008 In particular, for our @code{Unit_1}/@code{Unit_2} example, if
27009 we marked both of them as having pragma @code{Elaborate_Body}, then
27010 clearly there would be no possible elaboration order.
27011
27012 The above pragmas allow a server to guarantee safe use by clients, and
27013 clearly this is the preferable approach. Consequently a good rule
27014 is to mark units as @code{Pure} or @code{Preelaborate} if possible,
27015 and if this is not possible,
27016 mark them as @code{Elaborate_Body} if possible.
27017 As we have seen, there are situations where neither of these
27018 three pragmas can be used.
27019 So we also provide methods for clients to control the
27020 order of elaboration of the servers on which they depend:
27021
27022 @table @asis
27023 @item pragma Elaborate (unit)
27024 @findex Elaborate
27025 @cindex pragma Elaborate
27026 This pragma is placed in the context clause, after a @code{with} clause,
27027 and it requires that the body of the named unit be elaborated before
27028 the unit in which the pragma occurs. The idea is to use this pragma
27029 if the current unit calls at elaboration time, directly or indirectly,
27030 some subprogram in the named unit.
27031
27032 @item pragma Elaborate_All (unit)
27033 @findex Elaborate_All
27034 @cindex pragma Elaborate_All
27035 This is a stronger version of the Elaborate pragma. Consider the
27036 following example:
27037
27038 @smallexample
27039 Unit A @code{with}'s unit B and calls B.Func in elab code
27040 Unit B @code{with}'s unit C, and B.Func calls C.Func
27041 @end smallexample
27042
27043 @noindent
27044 Now if we put a pragma @code{Elaborate (B)}
27045 in unit @code{A}, this ensures that the
27046 body of @code{B} is elaborated before the call, but not the
27047 body of @code{C}, so
27048 the call to @code{C.Func} could still cause @code{Program_Error} to
27049 be raised.
27050
27051 The effect of a pragma @code{Elaborate_All} is stronger, it requires
27052 not only that the body of the named unit be elaborated before the
27053 unit doing the @code{with}, but also the bodies of all units that the
27054 named unit uses, following @code{with} links transitively. For example,
27055 if we put a pragma @code{Elaborate_All (B)} in unit @code{A},
27056 then it requires
27057 not only that the body of @code{B} be elaborated before @code{A},
27058 but also the
27059 body of @code{C}, because @code{B} @code{with}'s @code{C}.
27060 @end table
27061
27062 @noindent
27063 We are now in a position to give a usage rule in Ada for avoiding
27064 elaboration problems, at least if dynamic dispatching and access to
27065 subprogram values are not used. We will handle these cases separately
27066 later.
27067
27068 The rule is simple. If a unit has elaboration code that can directly or
27069 indirectly make a call to a subprogram in a @code{with}'ed unit, or instantiate
27070 a generic package in a @code{with}'ed unit,
27071 then if the @code{with}'ed unit does not have
27072 pragma @code{Pure} or @code{Preelaborate}, then the client should have
27073 a pragma @code{Elaborate_All}
27074 for the @code{with}'ed unit. By following this rule a client is
27075 assured that calls can be made without risk of an exception.
27076
27077 For generic subprogram instantiations, the rule can be relaxed to
27078 require only a pragma @code{Elaborate} since elaborating the body
27079 of a subprogram cannot cause any transitive elaboration (we are
27080 not calling the subprogram in this case, just elaborating its
27081 declaration).
27082
27083 If this rule is not followed, then a program may be in one of four
27084 states:
27085
27086 @table @asis
27087 @item No order exists
27088 No order of elaboration exists which follows the rules, taking into
27089 account any @code{Elaborate}, @code{Elaborate_All},
27090 or @code{Elaborate_Body} pragmas. In
27091 this case, an Ada compiler must diagnose the situation at bind
27092 time, and refuse to build an executable program.
27093
27094 @item One or more orders exist, all incorrect
27095 One or more acceptable elaboration orders exist, and all of them
27096 generate an elaboration order problem. In this case, the binder
27097 can build an executable program, but @code{Program_Error} will be raised
27098 when the program is run.
27099
27100 @item Several orders exist, some right, some incorrect
27101 One or more acceptable elaboration orders exists, and some of them
27102 work, and some do not. The programmer has not controlled
27103 the order of elaboration, so the binder may or may not pick one of
27104 the correct orders, and the program may or may not raise an
27105 exception when it is run. This is the worst case, because it means
27106 that the program may fail when moved to another compiler, or even
27107 another version of the same compiler.
27108
27109 @item One or more orders exists, all correct
27110 One ore more acceptable elaboration orders exist, and all of them
27111 work. In this case the program runs successfully. This state of
27112 affairs can be guaranteed by following the rule we gave above, but
27113 may be true even if the rule is not followed.
27114 @end table
27115
27116 @noindent
27117 Note that one additional advantage of following our rules on the use
27118 of @code{Elaborate} and @code{Elaborate_All}
27119 is that the program continues to stay in the ideal (all orders OK) state
27120 even if maintenance
27121 changes some bodies of some units. Conversely, if a program that does
27122 not follow this rule happens to be safe at some point, this state of affairs
27123 may deteriorate silently as a result of maintenance changes.
27124
27125 You may have noticed that the above discussion did not mention
27126 the use of @code{Elaborate_Body}. This was a deliberate omission. If you
27127 @code{with} an @code{Elaborate_Body} unit, it still may be the case that
27128 code in the body makes calls to some other unit, so it is still necessary
27129 to use @code{Elaborate_All} on such units.
27130
27131 @node Controlling Elaboration in GNAT - Internal Calls
27132 @section Controlling Elaboration in GNAT - Internal Calls
27133
27134 @noindent
27135 In the case of internal calls, i.e., calls within a single package, the
27136 programmer has full control over the order of elaboration, and it is up
27137 to the programmer to elaborate declarations in an appropriate order. For
27138 example writing:
27139
27140 @smallexample @c ada
27141 @group
27142 @cartouche
27143 function One return Float;
27144
27145 Q : Float := One;
27146
27147 function One return Float is
27148 begin
27149 return 1.0;
27150 end One;
27151 @end cartouche
27152 @end group
27153 @end smallexample
27154
27155 @noindent
27156 will obviously raise @code{Program_Error} at run time, because function
27157 One will be called before its body is elaborated. In this case GNAT will
27158 generate a warning that the call will raise @code{Program_Error}:
27159
27160 @smallexample
27161 @group
27162 @cartouche
27163 1. procedure y is
27164 2. function One return Float;
27165 3.
27166 4. Q : Float := One;
27167 |
27168 >>> warning: cannot call "One" before body is elaborated
27169 >>> warning: Program_Error will be raised at run time
27170
27171 5.
27172 6. function One return Float is
27173 7. begin
27174 8. return 1.0;
27175 9. end One;
27176 10.
27177 11. begin
27178 12. null;
27179 13. end;
27180 @end cartouche
27181 @end group
27182 @end smallexample
27183
27184 @noindent
27185 Note that in this particular case, it is likely that the call is safe, because
27186 the function @code{One} does not access any global variables.
27187 Nevertheless in Ada, we do not want the validity of the check to depend on
27188 the contents of the body (think about the separate compilation case), so this
27189 is still wrong, as we discussed in the previous sections.
27190
27191 The error is easily corrected by rearranging the declarations so that the
27192 body of @code{One} appears before the declaration containing the call
27193 (note that in Ada 95 and Ada 2005,
27194 declarations can appear in any order, so there is no restriction that
27195 would prevent this reordering, and if we write:
27196
27197 @smallexample @c ada
27198 @group
27199 @cartouche
27200 function One return Float;
27201
27202 function One return Float is
27203 begin
27204 return 1.0;
27205 end One;
27206
27207 Q : Float := One;
27208 @end cartouche
27209 @end group
27210 @end smallexample
27211
27212 @noindent
27213 then all is well, no warning is generated, and no
27214 @code{Program_Error} exception
27215 will be raised.
27216 Things are more complicated when a chain of subprograms is executed:
27217
27218 @smallexample @c ada
27219 @group
27220 @cartouche
27221 function A return Integer;
27222 function B return Integer;
27223 function C return Integer;
27224
27225 function B return Integer is begin return A; end;
27226 function C return Integer is begin return B; end;
27227
27228 X : Integer := C;
27229
27230 function A return Integer is begin return 1; end;
27231 @end cartouche
27232 @end group
27233 @end smallexample
27234
27235 @noindent
27236 Now the call to @code{C}
27237 at elaboration time in the declaration of @code{X} is correct, because
27238 the body of @code{C} is already elaborated,
27239 and the call to @code{B} within the body of
27240 @code{C} is correct, but the call
27241 to @code{A} within the body of @code{B} is incorrect, because the body
27242 of @code{A} has not been elaborated, so @code{Program_Error}
27243 will be raised on the call to @code{A}.
27244 In this case GNAT will generate a
27245 warning that @code{Program_Error} may be
27246 raised at the point of the call. Let's look at the warning:
27247
27248 @smallexample
27249 @group
27250 @cartouche
27251 1. procedure x is
27252 2. function A return Integer;
27253 3. function B return Integer;
27254 4. function C return Integer;
27255 5.
27256 6. function B return Integer is begin return A; end;
27257 |
27258 >>> warning: call to "A" before body is elaborated may
27259 raise Program_Error
27260 >>> warning: "B" called at line 7
27261 >>> warning: "C" called at line 9
27262
27263 7. function C return Integer is begin return B; end;
27264 8.
27265 9. X : Integer := C;
27266 10.
27267 11. function A return Integer is begin return 1; end;
27268 12.
27269 13. begin
27270 14. null;
27271 15. end;
27272 @end cartouche
27273 @end group
27274 @end smallexample
27275
27276 @noindent
27277 Note that the message here says ``may raise'', instead of the direct case,
27278 where the message says ``will be raised''. That's because whether
27279 @code{A} is
27280 actually called depends in general on run-time flow of control.
27281 For example, if the body of @code{B} said
27282
27283 @smallexample @c ada
27284 @group
27285 @cartouche
27286 function B return Integer is
27287 begin
27288 if some-condition-depending-on-input-data then
27289 return A;
27290 else
27291 return 1;
27292 end if;
27293 end B;
27294 @end cartouche
27295 @end group
27296 @end smallexample
27297
27298 @noindent
27299 then we could not know until run time whether the incorrect call to A would
27300 actually occur, so @code{Program_Error} might
27301 or might not be raised. It is possible for a compiler to
27302 do a better job of analyzing bodies, to
27303 determine whether or not @code{Program_Error}
27304 might be raised, but it certainly
27305 couldn't do a perfect job (that would require solving the halting problem
27306 and is provably impossible), and because this is a warning anyway, it does
27307 not seem worth the effort to do the analysis. Cases in which it
27308 would be relevant are rare.
27309
27310 In practice, warnings of either of the forms given
27311 above will usually correspond to
27312 real errors, and should be examined carefully and eliminated.
27313 In the rare case where a warning is bogus, it can be suppressed by any of
27314 the following methods:
27315
27316 @itemize @bullet
27317 @item
27318 Compile with the @option{-gnatws} switch set
27319
27320 @item
27321 Suppress @code{Elaboration_Check} for the called subprogram
27322
27323 @item
27324 Use pragma @code{Warnings_Off} to turn warnings off for the call
27325 @end itemize
27326
27327 @noindent
27328 For the internal elaboration check case,
27329 GNAT by default generates the
27330 necessary run-time checks to ensure
27331 that @code{Program_Error} is raised if any
27332 call fails an elaboration check. Of course this can only happen if a
27333 warning has been issued as described above. The use of pragma
27334 @code{Suppress (Elaboration_Check)} may (but is not guaranteed to) suppress
27335 some of these checks, meaning that it may be possible (but is not
27336 guaranteed) for a program to be able to call a subprogram whose body
27337 is not yet elaborated, without raising a @code{Program_Error} exception.
27338
27339 @node Controlling Elaboration in GNAT - External Calls
27340 @section Controlling Elaboration in GNAT - External Calls
27341
27342 @noindent
27343 The previous section discussed the case in which the execution of a
27344 particular thread of elaboration code occurred entirely within a
27345 single unit. This is the easy case to handle, because a programmer
27346 has direct and total control over the order of elaboration, and
27347 furthermore, checks need only be generated in cases which are rare
27348 and which the compiler can easily detect.
27349 The situation is more complex when separate compilation is taken into account.
27350 Consider the following:
27351
27352 @smallexample @c ada
27353 @cartouche
27354 @group
27355 package Math is
27356 function Sqrt (Arg : Float) return Float;
27357 end Math;
27358
27359 package body Math is
27360 function Sqrt (Arg : Float) return Float is
27361 begin
27362 @dots{}
27363 end Sqrt;
27364 end Math;
27365 @end group
27366 @group
27367 with Math;
27368 package Stuff is
27369 X : Float := Math.Sqrt (0.5);
27370 end Stuff;
27371
27372 with Stuff;
27373 procedure Main is
27374 begin
27375 @dots{}
27376 end Main;
27377 @end group
27378 @end cartouche
27379 @end smallexample
27380
27381 @noindent
27382 where @code{Main} is the main program. When this program is executed, the
27383 elaboration code must first be executed, and one of the jobs of the
27384 binder is to determine the order in which the units of a program are
27385 to be elaborated. In this case we have four units: the spec and body
27386 of @code{Math},
27387 the spec of @code{Stuff} and the body of @code{Main}).
27388 In what order should the four separate sections of elaboration code
27389 be executed?
27390
27391 There are some restrictions in the order of elaboration that the binder
27392 can choose. In particular, if unit U has a @code{with}
27393 for a package @code{X}, then you
27394 are assured that the spec of @code{X}
27395 is elaborated before U , but you are
27396 not assured that the body of @code{X}
27397 is elaborated before U.
27398 This means that in the above case, the binder is allowed to choose the
27399 order:
27400
27401 @smallexample
27402 spec of Math
27403 spec of Stuff
27404 body of Math
27405 body of Main
27406 @end smallexample
27407
27408 @noindent
27409 but that's not good, because now the call to @code{Math.Sqrt}
27410 that happens during
27411 the elaboration of the @code{Stuff}
27412 spec happens before the body of @code{Math.Sqrt} is
27413 elaborated, and hence causes @code{Program_Error} exception to be raised.
27414 At first glance, one might say that the binder is misbehaving, because
27415 obviously you want to elaborate the body of something you @code{with}
27416 first, but
27417 that is not a general rule that can be followed in all cases. Consider
27418
27419 @smallexample @c ada
27420 @group
27421 @cartouche
27422 package X is @dots{}
27423
27424 package Y is @dots{}
27425
27426 with X;
27427 package body Y is @dots{}
27428
27429 with Y;
27430 package body X is @dots{}
27431 @end cartouche
27432 @end group
27433 @end smallexample
27434
27435 @noindent
27436 This is a common arrangement, and, apart from the order of elaboration
27437 problems that might arise in connection with elaboration code, this works fine.
27438 A rule that says that you must first elaborate the body of anything you
27439 @code{with} cannot work in this case:
27440 the body of @code{X} @code{with}'s @code{Y},
27441 which means you would have to
27442 elaborate the body of @code{Y} first, but that @code{with}'s @code{X},
27443 which means
27444 you have to elaborate the body of @code{X} first, but @dots{} and we have a
27445 loop that cannot be broken.
27446
27447 It is true that the binder can in many cases guess an order of elaboration
27448 that is unlikely to cause a @code{Program_Error}
27449 exception to be raised, and it tries to do so (in the
27450 above example of @code{Math/Stuff/Spec}, the GNAT binder will
27451 by default
27452 elaborate the body of @code{Math} right after its spec, so all will be well).
27453
27454 However, a program that blindly relies on the binder to be helpful can
27455 get into trouble, as we discussed in the previous sections, so
27456 GNAT
27457 provides a number of facilities for assisting the programmer in
27458 developing programs that are robust with respect to elaboration order.
27459
27460 @node Default Behavior in GNAT - Ensuring Safety
27461 @section Default Behavior in GNAT - Ensuring Safety
27462
27463 @noindent
27464 The default behavior in GNAT ensures elaboration safety. In its
27465 default mode GNAT implements the
27466 rule we previously described as the right approach. Let's restate it:
27467
27468 @itemize
27469 @item
27470 @emph{If a unit has elaboration code that can directly or indirectly make a
27471 call to a subprogram in a @code{with}'ed unit, or instantiate a generic
27472 package in a @code{with}'ed unit, then if the @code{with}'ed unit
27473 does not have pragma @code{Pure} or
27474 @code{Preelaborate}, then the client should have an
27475 @code{Elaborate_All} pragma for the @code{with}'ed unit.}
27476
27477 @emph{In the case of instantiating a generic subprogram, it is always
27478 sufficient to have only an @code{Elaborate} pragma for the
27479 @code{with}'ed unit.}
27480 @end itemize
27481
27482 @noindent
27483 By following this rule a client is assured that calls and instantiations
27484 can be made without risk of an exception.
27485
27486 In this mode GNAT traces all calls that are potentially made from
27487 elaboration code, and puts in any missing implicit @code{Elaborate}
27488 and @code{Elaborate_All} pragmas.
27489 The advantage of this approach is that no elaboration problems
27490 are possible if the binder can find an elaboration order that is
27491 consistent with these implicit @code{Elaborate} and
27492 @code{Elaborate_All} pragmas. The
27493 disadvantage of this approach is that no such order may exist.
27494
27495 If the binder does not generate any diagnostics, then it means that it has
27496 found an elaboration order that is guaranteed to be safe. However, the binder
27497 may still be relying on implicitly generated @code{Elaborate} and
27498 @code{Elaborate_All} pragmas so portability to other compilers than GNAT is not
27499 guaranteed.
27500
27501 If it is important to guarantee portability, then the compilations should
27502 use the
27503 @option{-gnatwl}
27504 (warn on elaboration problems) switch. This will cause warning messages
27505 to be generated indicating the missing @code{Elaborate} and
27506 @code{Elaborate_All} pragmas.
27507 Consider the following source program:
27508
27509 @smallexample @c ada
27510 @group
27511 @cartouche
27512 with k;
27513 package j is
27514 m : integer := k.r;
27515 end;
27516 @end cartouche
27517 @end group
27518 @end smallexample
27519
27520 @noindent
27521 where it is clear that there
27522 should be a pragma @code{Elaborate_All}
27523 for unit @code{k}. An implicit pragma will be generated, and it is
27524 likely that the binder will be able to honor it. However, if you want
27525 to port this program to some other Ada compiler than GNAT.
27526 it is safer to include the pragma explicitly in the source. If this
27527 unit is compiled with the
27528 @option{-gnatwl}
27529 switch, then the compiler outputs a warning:
27530
27531 @smallexample
27532 @group
27533 @cartouche
27534 1. with k;
27535 2. package j is
27536 3. m : integer := k.r;
27537 |
27538 >>> warning: call to "r" may raise Program_Error
27539 >>> warning: missing pragma Elaborate_All for "k"
27540
27541 4. end;
27542 @end cartouche
27543 @end group
27544 @end smallexample
27545
27546 @noindent
27547 and these warnings can be used as a guide for supplying manually
27548 the missing pragmas. It is usually a bad idea to use this warning
27549 option during development. That's because it will warn you when
27550 you need to put in a pragma, but cannot warn you when it is time
27551 to take it out. So the use of pragma @code{Elaborate_All} may lead to
27552 unnecessary dependencies and even false circularities.
27553
27554 This default mode is more restrictive than the Ada Reference
27555 Manual, and it is possible to construct programs which will compile
27556 using the dynamic model described there, but will run into a
27557 circularity using the safer static model we have described.
27558
27559 Of course any Ada compiler must be able to operate in a mode
27560 consistent with the requirements of the Ada Reference Manual,
27561 and in particular must have the capability of implementing the
27562 standard dynamic model of elaboration with run-time checks.
27563
27564 In GNAT, this standard mode can be achieved either by the use of
27565 the @option{-gnatE} switch on the compiler (@command{gcc} or
27566 @command{gnatmake}) command, or by the use of the configuration pragma:
27567
27568 @smallexample @c ada
27569 pragma Elaboration_Checks (RM);
27570 @end smallexample
27571
27572 @noindent
27573 Either approach will cause the unit affected to be compiled using the
27574 standard dynamic run-time elaboration checks described in the Ada
27575 Reference Manual. The static model is generally preferable, since it
27576 is clearly safer to rely on compile and link time checks rather than
27577 run-time checks. However, in the case of legacy code, it may be
27578 difficult to meet the requirements of the static model. This
27579 issue is further discussed in
27580 @ref{What to Do If the Default Elaboration Behavior Fails}.
27581
27582 Note that the static model provides a strict subset of the allowed
27583 behavior and programs of the Ada Reference Manual, so if you do
27584 adhere to the static model and no circularities exist,
27585 then you are assured that your program will
27586 work using the dynamic model, providing that you remove any
27587 pragma Elaborate statements from the source.
27588
27589 @node Treatment of Pragma Elaborate
27590 @section Treatment of Pragma Elaborate
27591 @cindex Pragma Elaborate
27592
27593 @noindent
27594 The use of @code{pragma Elaborate}
27595 should generally be avoided in Ada 95 and Ada 2005 programs,
27596 since there is no guarantee that transitive calls
27597 will be properly handled. Indeed at one point, this pragma was placed
27598 in Annex J (Obsolescent Features), on the grounds that it is never useful.
27599
27600 Now that's a bit restrictive. In practice, the case in which
27601 @code{pragma Elaborate} is useful is when the caller knows that there
27602 are no transitive calls, or that the called unit contains all necessary
27603 transitive @code{pragma Elaborate} statements, and legacy code often
27604 contains such uses.
27605
27606 Strictly speaking the static mode in GNAT should ignore such pragmas,
27607 since there is no assurance at compile time that the necessary safety
27608 conditions are met. In practice, this would cause GNAT to be incompatible
27609 with correctly written Ada 83 code that had all necessary
27610 @code{pragma Elaborate} statements in place. Consequently, we made the
27611 decision that GNAT in its default mode will believe that if it encounters
27612 a @code{pragma Elaborate} then the programmer knows what they are doing,
27613 and it will trust that no elaboration errors can occur.
27614
27615 The result of this decision is two-fold. First to be safe using the
27616 static mode, you should remove all @code{pragma Elaborate} statements.
27617 Second, when fixing circularities in existing code, you can selectively
27618 use @code{pragma Elaborate} statements to convince the static mode of
27619 GNAT that it need not generate an implicit @code{pragma Elaborate_All}
27620 statement.
27621
27622 When using the static mode with @option{-gnatwl}, any use of
27623 @code{pragma Elaborate} will generate a warning about possible
27624 problems.
27625
27626 @node Elaboration Issues for Library Tasks
27627 @section Elaboration Issues for Library Tasks
27628 @cindex Library tasks, elaboration issues
27629 @cindex Elaboration of library tasks
27630
27631 @noindent
27632 In this section we examine special elaboration issues that arise for
27633 programs that declare library level tasks.
27634
27635 Generally the model of execution of an Ada program is that all units are
27636 elaborated, and then execution of the program starts. However, the
27637 declaration of library tasks definitely does not fit this model. The
27638 reason for this is that library tasks start as soon as they are declared
27639 (more precisely, as soon as the statement part of the enclosing package
27640 body is reached), that is to say before elaboration
27641 of the program is complete. This means that if such a task calls a
27642 subprogram, or an entry in another task, the callee may or may not be
27643 elaborated yet, and in the standard
27644 Reference Manual model of dynamic elaboration checks, you can even
27645 get timing dependent Program_Error exceptions, since there can be
27646 a race between the elaboration code and the task code.
27647
27648 The static model of elaboration in GNAT seeks to avoid all such
27649 dynamic behavior, by being conservative, and the conservative
27650 approach in this particular case is to assume that all the code
27651 in a task body is potentially executed at elaboration time if
27652 a task is declared at the library level.
27653
27654 This can definitely result in unexpected circularities. Consider
27655 the following example
27656
27657 @smallexample @c ada
27658 package Decls is
27659 task Lib_Task is
27660 entry Start;
27661 end Lib_Task;
27662
27663 type My_Int is new Integer;
27664
27665 function Ident (M : My_Int) return My_Int;
27666 end Decls;
27667
27668 with Utils;
27669 package body Decls is
27670 task body Lib_Task is
27671 begin
27672 accept Start;
27673 Utils.Put_Val (2);
27674 end Lib_Task;
27675
27676 function Ident (M : My_Int) return My_Int is
27677 begin
27678 return M;
27679 end Ident;
27680 end Decls;
27681
27682 with Decls;
27683 package Utils is
27684 procedure Put_Val (Arg : Decls.My_Int);
27685 end Utils;
27686
27687 with Text_IO;
27688 package body Utils is
27689 procedure Put_Val (Arg : Decls.My_Int) is
27690 begin
27691 Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
27692 end Put_Val;
27693 end Utils;
27694
27695 with Decls;
27696 procedure Main is
27697 begin
27698 Decls.Lib_Task.Start;
27699 end;
27700 @end smallexample
27701
27702 @noindent
27703 If the above example is compiled in the default static elaboration
27704 mode, then a circularity occurs. The circularity comes from the call
27705 @code{Utils.Put_Val} in the task body of @code{Decls.Lib_Task}. Since
27706 this call occurs in elaboration code, we need an implicit pragma
27707 @code{Elaborate_All} for @code{Utils}. This means that not only must
27708 the spec and body of @code{Utils} be elaborated before the body
27709 of @code{Decls}, but also the spec and body of any unit that is
27710 @code{with'ed} by the body of @code{Utils} must also be elaborated before
27711 the body of @code{Decls}. This is the transitive implication of
27712 pragma @code{Elaborate_All} and it makes sense, because in general
27713 the body of @code{Put_Val} might have a call to something in a
27714 @code{with'ed} unit.
27715
27716 In this case, the body of Utils (actually its spec) @code{with's}
27717 @code{Decls}. Unfortunately this means that the body of @code{Decls}
27718 must be elaborated before itself, in case there is a call from the
27719 body of @code{Utils}.
27720
27721 Here is the exact chain of events we are worrying about:
27722
27723 @enumerate
27724 @item
27725 In the body of @code{Decls} a call is made from within the body of a library
27726 task to a subprogram in the package @code{Utils}. Since this call may
27727 occur at elaboration time (given that the task is activated at elaboration
27728 time), we have to assume the worst, i.e., that the
27729 call does happen at elaboration time.
27730
27731 @item
27732 This means that the body and spec of @code{Util} must be elaborated before
27733 the body of @code{Decls} so that this call does not cause an access before
27734 elaboration.
27735
27736 @item
27737 Within the body of @code{Util}, specifically within the body of
27738 @code{Util.Put_Val} there may be calls to any unit @code{with}'ed
27739 by this package.
27740
27741 @item
27742 One such @code{with}'ed package is package @code{Decls}, so there
27743 might be a call to a subprogram in @code{Decls} in @code{Put_Val}.
27744 In fact there is such a call in this example, but we would have to
27745 assume that there was such a call even if it were not there, since
27746 we are not supposed to write the body of @code{Decls} knowing what
27747 is in the body of @code{Utils}; certainly in the case of the
27748 static elaboration model, the compiler does not know what is in
27749 other bodies and must assume the worst.
27750
27751 @item
27752 This means that the spec and body of @code{Decls} must also be
27753 elaborated before we elaborate the unit containing the call, but
27754 that unit is @code{Decls}! This means that the body of @code{Decls}
27755 must be elaborated before itself, and that's a circularity.
27756 @end enumerate
27757
27758 @noindent
27759 Indeed, if you add an explicit pragma @code{Elaborate_All} for @code{Utils} in
27760 the body of @code{Decls} you will get a true Ada Reference Manual
27761 circularity that makes the program illegal.
27762
27763 In practice, we have found that problems with the static model of
27764 elaboration in existing code often arise from library tasks, so
27765 we must address this particular situation.
27766
27767 Note that if we compile and run the program above, using the dynamic model of
27768 elaboration (that is to say use the @option{-gnatE} switch),
27769 then it compiles, binds,
27770 links, and runs, printing the expected result of 2. Therefore in some sense
27771 the circularity here is only apparent, and we need to capture
27772 the properties of this program that distinguish it from other library-level
27773 tasks that have real elaboration problems.
27774
27775 We have four possible answers to this question:
27776
27777 @itemize @bullet
27778
27779 @item
27780 Use the dynamic model of elaboration.
27781
27782 If we use the @option{-gnatE} switch, then as noted above, the program works.
27783 Why is this? If we examine the task body, it is apparent that the task cannot
27784 proceed past the
27785 @code{accept} statement until after elaboration has been completed, because
27786 the corresponding entry call comes from the main program, not earlier.
27787 This is why the dynamic model works here. But that's really giving
27788 up on a precise analysis, and we prefer to take this approach only if we cannot
27789 solve the
27790 problem in any other manner. So let us examine two ways to reorganize
27791 the program to avoid the potential elaboration problem.
27792
27793 @item
27794 Split library tasks into separate packages.
27795
27796 Write separate packages, so that library tasks are isolated from
27797 other declarations as much as possible. Let us look at a variation on
27798 the above program.
27799
27800 @smallexample @c ada
27801 package Decls1 is
27802 task Lib_Task is
27803 entry Start;
27804 end Lib_Task;
27805 end Decls1;
27806
27807 with Utils;
27808 package body Decls1 is
27809 task body Lib_Task is
27810 begin
27811 accept Start;
27812 Utils.Put_Val (2);
27813 end Lib_Task;
27814 end Decls1;
27815
27816 package Decls2 is
27817 type My_Int is new Integer;
27818 function Ident (M : My_Int) return My_Int;
27819 end Decls2;
27820
27821 with Utils;
27822 package body Decls2 is
27823 function Ident (M : My_Int) return My_Int is
27824 begin
27825 return M;
27826 end Ident;
27827 end Decls2;
27828
27829 with Decls2;
27830 package Utils is
27831 procedure Put_Val (Arg : Decls2.My_Int);
27832 end Utils;
27833
27834 with Text_IO;
27835 package body Utils is
27836 procedure Put_Val (Arg : Decls2.My_Int) is
27837 begin
27838 Text_IO.Put_Line (Decls2.My_Int'Image (Decls2.Ident (Arg)));
27839 end Put_Val;
27840 end Utils;
27841
27842 with Decls1;
27843 procedure Main is
27844 begin
27845 Decls1.Lib_Task.Start;
27846 end;
27847 @end smallexample
27848
27849 @noindent
27850 All we have done is to split @code{Decls} into two packages, one
27851 containing the library task, and one containing everything else. Now
27852 there is no cycle, and the program compiles, binds, links and executes
27853 using the default static model of elaboration.
27854
27855 @item
27856 Declare separate task types.
27857
27858 A significant part of the problem arises because of the use of the
27859 single task declaration form. This means that the elaboration of
27860 the task type, and the elaboration of the task itself (i.e.@: the
27861 creation of the task) happen at the same time. A good rule
27862 of style in Ada is to always create explicit task types. By
27863 following the additional step of placing task objects in separate
27864 packages from the task type declaration, many elaboration problems
27865 are avoided. Here is another modified example of the example program:
27866
27867 @smallexample @c ada
27868 package Decls is
27869 task type Lib_Task_Type is
27870 entry Start;
27871 end Lib_Task_Type;
27872
27873 type My_Int is new Integer;
27874
27875 function Ident (M : My_Int) return My_Int;
27876 end Decls;
27877
27878 with Utils;
27879 package body Decls is
27880 task body Lib_Task_Type is
27881 begin
27882 accept Start;
27883 Utils.Put_Val (2);
27884 end Lib_Task_Type;
27885
27886 function Ident (M : My_Int) return My_Int is
27887 begin
27888 return M;
27889 end Ident;
27890 end Decls;
27891
27892 with Decls;
27893 package Utils is
27894 procedure Put_Val (Arg : Decls.My_Int);
27895 end Utils;
27896
27897 with Text_IO;
27898 package body Utils is
27899 procedure Put_Val (Arg : Decls.My_Int) is
27900 begin
27901 Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
27902 end Put_Val;
27903 end Utils;
27904
27905 with Decls;
27906 package Declst is
27907 Lib_Task : Decls.Lib_Task_Type;
27908 end Declst;
27909
27910 with Declst;
27911 procedure Main is
27912 begin
27913 Declst.Lib_Task.Start;
27914 end;
27915 @end smallexample
27916
27917 @noindent
27918 What we have done here is to replace the @code{task} declaration in
27919 package @code{Decls} with a @code{task type} declaration. Then we
27920 introduce a separate package @code{Declst} to contain the actual
27921 task object. This separates the elaboration issues for
27922 the @code{task type}
27923 declaration, which causes no trouble, from the elaboration issues
27924 of the task object, which is also unproblematic, since it is now independent
27925 of the elaboration of @code{Utils}.
27926 This separation of concerns also corresponds to
27927 a generally sound engineering principle of separating declarations
27928 from instances. This version of the program also compiles, binds, links,
27929 and executes, generating the expected output.
27930
27931 @item
27932 Use No_Entry_Calls_In_Elaboration_Code restriction.
27933 @cindex No_Entry_Calls_In_Elaboration_Code
27934
27935 The previous two approaches described how a program can be restructured
27936 to avoid the special problems caused by library task bodies. in practice,
27937 however, such restructuring may be difficult to apply to existing legacy code,
27938 so we must consider solutions that do not require massive rewriting.
27939
27940 Let us consider more carefully why our original sample program works
27941 under the dynamic model of elaboration. The reason is that the code
27942 in the task body blocks immediately on the @code{accept}
27943 statement. Now of course there is nothing to prohibit elaboration
27944 code from making entry calls (for example from another library level task),
27945 so we cannot tell in isolation that
27946 the task will not execute the accept statement during elaboration.
27947
27948 However, in practice it is very unusual to see elaboration code
27949 make any entry calls, and the pattern of tasks starting
27950 at elaboration time and then immediately blocking on @code{accept} or
27951 @code{select} statements is very common. What this means is that
27952 the compiler is being too pessimistic when it analyzes the
27953 whole package body as though it might be executed at elaboration
27954 time.
27955
27956 If we know that the elaboration code contains no entry calls, (a very safe
27957 assumption most of the time, that could almost be made the default
27958 behavior), then we can compile all units of the program under control
27959 of the following configuration pragma:
27960
27961 @smallexample
27962 pragma Restrictions (No_Entry_Calls_In_Elaboration_Code);
27963 @end smallexample
27964
27965 @noindent
27966 This pragma can be placed in the @file{gnat.adc} file in the usual
27967 manner. If we take our original unmodified program and compile it
27968 in the presence of a @file{gnat.adc} containing the above pragma,
27969 then once again, we can compile, bind, link, and execute, obtaining
27970 the expected result. In the presence of this pragma, the compiler does
27971 not trace calls in a task body, that appear after the first @code{accept}
27972 or @code{select} statement, and therefore does not report a potential
27973 circularity in the original program.
27974
27975 The compiler will check to the extent it can that the above
27976 restriction is not violated, but it is not always possible to do a
27977 complete check at compile time, so it is important to use this
27978 pragma only if the stated restriction is in fact met, that is to say
27979 no task receives an entry call before elaboration of all units is completed.
27980
27981 @end itemize
27982
27983 @node Mixing Elaboration Models
27984 @section Mixing Elaboration Models
27985 @noindent
27986 So far, we have assumed that the entire program is either compiled
27987 using the dynamic model or static model, ensuring consistency. It
27988 is possible to mix the two models, but rules have to be followed
27989 if this mixing is done to ensure that elaboration checks are not
27990 omitted.
27991
27992 The basic rule is that @emph{a unit compiled with the static model cannot
27993 be @code{with'ed} by a unit compiled with the dynamic model}. The
27994 reason for this is that in the static model, a unit assumes that
27995 its clients guarantee to use (the equivalent of) pragma
27996 @code{Elaborate_All} so that no elaboration checks are required
27997 in inner subprograms, and this assumption is violated if the
27998 client is compiled with dynamic checks.
27999
28000 The precise rule is as follows. A unit that is compiled with dynamic
28001 checks can only @code{with} a unit that meets at least one of the
28002 following criteria:
28003
28004 @itemize @bullet
28005
28006 @item
28007 The @code{with'ed} unit is itself compiled with dynamic elaboration
28008 checks (that is with the @option{-gnatE} switch.
28009
28010 @item
28011 The @code{with'ed} unit is an internal GNAT implementation unit from
28012 the System, Interfaces, Ada, or GNAT hierarchies.
28013
28014 @item
28015 The @code{with'ed} unit has pragma Preelaborate or pragma Pure.
28016
28017 @item
28018 The @code{with'ing} unit (that is the client) has an explicit pragma
28019 @code{Elaborate_All} for the @code{with'ed} unit.
28020
28021 @end itemize
28022
28023 @noindent
28024 If this rule is violated, that is if a unit with dynamic elaboration
28025 checks @code{with's} a unit that does not meet one of the above four
28026 criteria, then the binder (@code{gnatbind}) will issue a warning
28027 similar to that in the following example:
28028
28029 @smallexample
28030 warning: "x.ads" has dynamic elaboration checks and with's
28031 warning: "y.ads" which has static elaboration checks
28032 @end smallexample
28033
28034 @noindent
28035 These warnings indicate that the rule has been violated, and that as a result
28036 elaboration checks may be missed in the resulting executable file.
28037 This warning may be suppressed using the @option{-ws} binder switch
28038 in the usual manner.
28039
28040 One useful application of this mixing rule is in the case of a subsystem
28041 which does not itself @code{with} units from the remainder of the
28042 application. In this case, the entire subsystem can be compiled with
28043 dynamic checks to resolve a circularity in the subsystem, while
28044 allowing the main application that uses this subsystem to be compiled
28045 using the more reliable default static model.
28046
28047 @node What to Do If the Default Elaboration Behavior Fails
28048 @section What to Do If the Default Elaboration Behavior Fails
28049
28050 @noindent
28051 If the binder cannot find an acceptable order, it outputs detailed
28052 diagnostics. For example:
28053 @smallexample
28054 @group
28055 @iftex
28056 @leftskip=0cm
28057 @end iftex
28058 error: elaboration circularity detected
28059 info: "proc (body)" must be elaborated before "pack (body)"
28060 info: reason: Elaborate_All probably needed in unit "pack (body)"
28061 info: recompile "pack (body)" with -gnatwl
28062 info: for full details
28063 info: "proc (body)"
28064 info: is needed by its spec:
28065 info: "proc (spec)"
28066 info: which is withed by:
28067 info: "pack (body)"
28068 info: "pack (body)" must be elaborated before "proc (body)"
28069 info: reason: pragma Elaborate in unit "proc (body)"
28070 @end group
28071
28072 @end smallexample
28073
28074 @noindent
28075 In this case we have a cycle that the binder cannot break. On the one
28076 hand, there is an explicit pragma Elaborate in @code{proc} for
28077 @code{pack}. This means that the body of @code{pack} must be elaborated
28078 before the body of @code{proc}. On the other hand, there is elaboration
28079 code in @code{pack} that calls a subprogram in @code{proc}. This means
28080 that for maximum safety, there should really be a pragma
28081 Elaborate_All in @code{pack} for @code{proc} which would require that
28082 the body of @code{proc} be elaborated before the body of
28083 @code{pack}. Clearly both requirements cannot be satisfied.
28084 Faced with a circularity of this kind, you have three different options.
28085
28086 @table @asis
28087 @item Fix the program
28088 The most desirable option from the point of view of long-term maintenance
28089 is to rearrange the program so that the elaboration problems are avoided.
28090 One useful technique is to place the elaboration code into separate
28091 child packages. Another is to move some of the initialization code to
28092 explicitly called subprograms, where the program controls the order
28093 of initialization explicitly. Although this is the most desirable option,
28094 it may be impractical and involve too much modification, especially in
28095 the case of complex legacy code.
28096
28097 @item Perform dynamic checks
28098 If the compilations are done using the
28099 @option{-gnatE}
28100 (dynamic elaboration check) switch, then GNAT behaves in a quite different
28101 manner. Dynamic checks are generated for all calls that could possibly result
28102 in raising an exception. With this switch, the compiler does not generate
28103 implicit @code{Elaborate} or @code{Elaborate_All} pragmas. The behavior then is
28104 exactly as specified in the @cite{Ada Reference Manual}.
28105 The binder will generate
28106 an executable program that may or may not raise @code{Program_Error}, and then
28107 it is the programmer's job to ensure that it does not raise an exception. Note
28108 that it is important to compile all units with the switch, it cannot be used
28109 selectively.
28110
28111 @item Suppress checks
28112 The drawback of dynamic checks is that they generate a
28113 significant overhead at run time, both in space and time. If you
28114 are absolutely sure that your program cannot raise any elaboration
28115 exceptions, and you still want to use the dynamic elaboration model,
28116 then you can use the configuration pragma
28117 @code{Suppress (Elaboration_Check)} to suppress all such checks. For
28118 example this pragma could be placed in the @file{gnat.adc} file.
28119
28120 @item Suppress checks selectively
28121 When you know that certain calls or instantiations in elaboration code cannot
28122 possibly lead to an elaboration error, and the binder nevertheless complains
28123 about implicit @code{Elaborate} and @code{Elaborate_All} pragmas that lead to
28124 elaboration circularities, it is possible to remove those warnings locally and
28125 obtain a program that will bind. Clearly this can be unsafe, and it is the
28126 responsibility of the programmer to make sure that the resulting program has no
28127 elaboration anomalies. The pragma @code{Suppress (Elaboration_Check)} can be
28128 used with different granularity to suppress warnings and break elaboration
28129 circularities:
28130
28131 @itemize @bullet
28132 @item
28133 Place the pragma that names the called subprogram in the declarative part
28134 that contains the call.
28135
28136 @item
28137 Place the pragma in the declarative part, without naming an entity. This
28138 disables warnings on all calls in the corresponding declarative region.
28139
28140 @item
28141 Place the pragma in the package spec that declares the called subprogram,
28142 and name the subprogram. This disables warnings on all elaboration calls to
28143 that subprogram.
28144
28145 @item
28146 Place the pragma in the package spec that declares the called subprogram,
28147 without naming any entity. This disables warnings on all elaboration calls to
28148 all subprograms declared in this spec.
28149
28150 @item Use Pragma Elaborate
28151 As previously described in section @xref{Treatment of Pragma Elaborate},
28152 GNAT in static mode assumes that a @code{pragma} Elaborate indicates correctly
28153 that no elaboration checks are required on calls to the designated unit.
28154 There may be cases in which the caller knows that no transitive calls
28155 can occur, so that a @code{pragma Elaborate} will be sufficient in a
28156 case where @code{pragma Elaborate_All} would cause a circularity.
28157 @end itemize
28158
28159 @noindent
28160 These five cases are listed in order of decreasing safety, and therefore
28161 require increasing programmer care in their application. Consider the
28162 following program:
28163
28164 @smallexample @c adanocomment
28165 package Pack1 is
28166 function F1 return Integer;
28167 X1 : Integer;
28168 end Pack1;
28169
28170 package Pack2 is
28171 function F2 return Integer;
28172 function Pure (x : integer) return integer;
28173 -- pragma Suppress (Elaboration_Check, On => Pure); -- (3)
28174 -- pragma Suppress (Elaboration_Check); -- (4)
28175 end Pack2;
28176
28177 with Pack2;
28178 package body Pack1 is
28179 function F1 return Integer is
28180 begin
28181 return 100;
28182 end F1;
28183 Val : integer := Pack2.Pure (11); -- Elab. call (1)
28184 begin
28185 declare
28186 -- pragma Suppress(Elaboration_Check, Pack2.F2); -- (1)
28187 -- pragma Suppress(Elaboration_Check); -- (2)
28188 begin
28189 X1 := Pack2.F2 + 1; -- Elab. call (2)
28190 end;
28191 end Pack1;
28192
28193 with Pack1;
28194 package body Pack2 is
28195 function F2 return Integer is
28196 begin
28197 return Pack1.F1;
28198 end F2;
28199 function Pure (x : integer) return integer is
28200 begin
28201 return x ** 3 - 3 * x;
28202 end;
28203 end Pack2;
28204
28205 with Pack1, Ada.Text_IO;
28206 procedure Proc3 is
28207 begin
28208 Ada.Text_IO.Put_Line(Pack1.X1'Img); -- 101
28209 end Proc3;
28210 @end smallexample
28211 In the absence of any pragmas, an attempt to bind this program produces
28212 the following diagnostics:
28213 @smallexample
28214 @group
28215 @iftex
28216 @leftskip=.5cm
28217 @end iftex
28218 error: elaboration circularity detected
28219 info: "pack1 (body)" must be elaborated before "pack1 (body)"
28220 info: reason: Elaborate_All probably needed in unit "pack1 (body)"
28221 info: recompile "pack1 (body)" with -gnatwl for full details
28222 info: "pack1 (body)"
28223 info: must be elaborated along with its spec:
28224 info: "pack1 (spec)"
28225 info: which is withed by:
28226 info: "pack2 (body)"
28227 info: which must be elaborated along with its spec:
28228 info: "pack2 (spec)"
28229 info: which is withed by:
28230 info: "pack1 (body)"
28231 @end group
28232 @end smallexample
28233 The sources of the circularity are the two calls to @code{Pack2.Pure} and
28234 @code{Pack2.F2} in the body of @code{Pack1}. We can see that the call to
28235 F2 is safe, even though F2 calls F1, because the call appears after the
28236 elaboration of the body of F1. Therefore the pragma (1) is safe, and will
28237 remove the warning on the call. It is also possible to use pragma (2)
28238 because there are no other potentially unsafe calls in the block.
28239
28240 @noindent
28241 The call to @code{Pure} is safe because this function does not depend on the
28242 state of @code{Pack2}. Therefore any call to this function is safe, and it
28243 is correct to place pragma (3) in the corresponding package spec.
28244
28245 @noindent
28246 Finally, we could place pragma (4) in the spec of @code{Pack2} to disable
28247 warnings on all calls to functions declared therein. Note that this is not
28248 necessarily safe, and requires more detailed examination of the subprogram
28249 bodies involved. In particular, a call to @code{F2} requires that @code{F1}
28250 be already elaborated.
28251 @end table
28252
28253 @noindent
28254 It is hard to generalize on which of these four approaches should be
28255 taken. Obviously if it is possible to fix the program so that the default
28256 treatment works, this is preferable, but this may not always be practical.
28257 It is certainly simple enough to use
28258 @option{-gnatE}
28259 but the danger in this case is that, even if the GNAT binder
28260 finds a correct elaboration order, it may not always do so,
28261 and certainly a binder from another Ada compiler might not. A
28262 combination of testing and analysis (for which the warnings generated
28263 with the
28264 @option{-gnatwl}
28265 switch can be useful) must be used to ensure that the program is free
28266 of errors. One switch that is useful in this testing is the
28267 @option{^-p (pessimistic elaboration order)^/PESSIMISTIC_ELABORATION_ORDER^}
28268 switch for
28269 @code{gnatbind}.
28270 Normally the binder tries to find an order that has the best chance
28271 of avoiding elaboration problems. However, if this switch is used, the binder
28272 plays a devil's advocate role, and tries to choose the order that
28273 has the best chance of failing. If your program works even with this
28274 switch, then it has a better chance of being error free, but this is still
28275 not a guarantee.
28276
28277 For an example of this approach in action, consider the C-tests (executable
28278 tests) from the ACVC suite. If these are compiled and run with the default
28279 treatment, then all but one of them succeed without generating any error
28280 diagnostics from the binder. However, there is one test that fails, and
28281 this is not surprising, because the whole point of this test is to ensure
28282 that the compiler can handle cases where it is impossible to determine
28283 a correct order statically, and it checks that an exception is indeed
28284 raised at run time.
28285
28286 This one test must be compiled and run using the
28287 @option{-gnatE}
28288 switch, and then it passes. Alternatively, the entire suite can
28289 be run using this switch. It is never wrong to run with the dynamic
28290 elaboration switch if your code is correct, and we assume that the
28291 C-tests are indeed correct (it is less efficient, but efficiency is
28292 not a factor in running the ACVC tests.)
28293
28294 @node Elaboration for Access-to-Subprogram Values
28295 @section Elaboration for Access-to-Subprogram Values
28296 @cindex Access-to-subprogram
28297
28298 @noindent
28299 Access-to-subprogram types (introduced in Ada 95) complicate
28300 the handling of elaboration. The trouble is that it becomes
28301 impossible to tell at compile time which procedure
28302 is being called. This means that it is not possible for the binder
28303 to analyze the elaboration requirements in this case.
28304
28305 If at the point at which the access value is created
28306 (i.e., the evaluation of @code{P'Access} for a subprogram @code{P}),
28307 the body of the subprogram is
28308 known to have been elaborated, then the access value is safe, and its use
28309 does not require a check. This may be achieved by appropriate arrangement
28310 of the order of declarations if the subprogram is in the current unit,
28311 or, if the subprogram is in another unit, by using pragma
28312 @code{Pure}, @code{Preelaborate}, or @code{Elaborate_Body}
28313 on the referenced unit.
28314
28315 If the referenced body is not known to have been elaborated at the point
28316 the access value is created, then any use of the access value must do a
28317 dynamic check, and this dynamic check will fail and raise a
28318 @code{Program_Error} exception if the body has not been elaborated yet.
28319 GNAT will generate the necessary checks, and in addition, if the
28320 @option{-gnatwl}
28321 switch is set, will generate warnings that such checks are required.
28322
28323 The use of dynamic dispatching for tagged types similarly generates
28324 a requirement for dynamic checks, and premature calls to any primitive
28325 operation of a tagged type before the body of the operation has been
28326 elaborated, will result in the raising of @code{Program_Error}.
28327
28328 @node Summary of Procedures for Elaboration Control
28329 @section Summary of Procedures for Elaboration Control
28330 @cindex Elaboration control
28331
28332 @noindent
28333 First, compile your program with the default options, using none of
28334 the special elaboration control switches. If the binder successfully
28335 binds your program, then you can be confident that, apart from issues
28336 raised by the use of access-to-subprogram types and dynamic dispatching,
28337 the program is free of elaboration errors. If it is important that the
28338 program be portable, then use the
28339 @option{-gnatwl}
28340 switch to generate warnings about missing @code{Elaborate} or
28341 @code{Elaborate_All} pragmas, and supply the missing pragmas.
28342
28343 If the program fails to bind using the default static elaboration
28344 handling, then you can fix the program to eliminate the binder
28345 message, or recompile the entire program with the
28346 @option{-gnatE} switch to generate dynamic elaboration checks,
28347 and, if you are sure there really are no elaboration problems,
28348 use a global pragma @code{Suppress (Elaboration_Check)}.
28349
28350 @node Other Elaboration Order Considerations
28351 @section Other Elaboration Order Considerations
28352 @noindent
28353 This section has been entirely concerned with the issue of finding a valid
28354 elaboration order, as defined by the Ada Reference Manual. In a case
28355 where several elaboration orders are valid, the task is to find one
28356 of the possible valid elaboration orders (and the static model in GNAT
28357 will ensure that this is achieved).
28358
28359 The purpose of the elaboration rules in the Ada Reference Manual is to
28360 make sure that no entity is accessed before it has been elaborated. For
28361 a subprogram, this means that the spec and body must have been elaborated
28362 before the subprogram is called. For an object, this means that the object
28363 must have been elaborated before its value is read or written. A violation
28364 of either of these two requirements is an access before elaboration order,
28365 and this section has been all about avoiding such errors.
28366
28367 In the case where more than one order of elaboration is possible, in the
28368 sense that access before elaboration errors are avoided, then any one of
28369 the orders is ``correct'' in the sense that it meets the requirements of
28370 the Ada Reference Manual, and no such error occurs.
28371
28372 However, it may be the case for a given program, that there are
28373 constraints on the order of elaboration that come not from consideration
28374 of avoiding elaboration errors, but rather from extra-lingual logic
28375 requirements. Consider this example:
28376
28377 @smallexample @c ada
28378 with Init_Constants;
28379 package Constants is
28380 X : Integer := 0;
28381 Y : Integer := 0;
28382 end Constants;
28383
28384 package Init_Constants is
28385 procedure P; -- require a body
28386 end Init_Constants;
28387
28388 with Constants;
28389 package body Init_Constants is
28390 procedure P is begin null; end;
28391 begin
28392 Constants.X := 3;
28393 Constants.Y := 4;
28394 end Init_Constants;
28395
28396 with Constants;
28397 package Calc is
28398 Z : Integer := Constants.X + Constants.Y;
28399 end Calc;
28400
28401 with Calc;
28402 with Text_IO; use Text_IO;
28403 procedure Main is
28404 begin
28405 Put_Line (Calc.Z'Img);
28406 end Main;
28407 @end smallexample
28408
28409 @noindent
28410 In this example, there is more than one valid order of elaboration. For
28411 example both the following are correct orders:
28412
28413 @smallexample
28414 Init_Constants spec
28415 Constants spec
28416 Calc spec
28417 Init_Constants body
28418 Main body
28419
28420 and
28421
28422 Init_Constants spec
28423 Init_Constants body
28424 Constants spec
28425 Calc spec
28426 Main body
28427 @end smallexample
28428
28429 @noindent
28430 There is no language rule to prefer one or the other, both are correct
28431 from an order of elaboration point of view. But the programmatic effects
28432 of the two orders are very different. In the first, the elaboration routine
28433 of @code{Calc} initializes @code{Z} to zero, and then the main program
28434 runs with this value of zero. But in the second order, the elaboration
28435 routine of @code{Calc} runs after the body of Init_Constants has set
28436 @code{X} and @code{Y} and thus @code{Z} is set to 7 before @code{Main}
28437 runs.
28438
28439 One could perhaps by applying pretty clever non-artificial intelligence
28440 to the situation guess that it is more likely that the second order of
28441 elaboration is the one desired, but there is no formal linguistic reason
28442 to prefer one over the other. In fact in this particular case, GNAT will
28443 prefer the second order, because of the rule that bodies are elaborated
28444 as soon as possible, but it's just luck that this is what was wanted
28445 (if indeed the second order was preferred).
28446
28447 If the program cares about the order of elaboration routines in a case like
28448 this, it is important to specify the order required. In this particular
28449 case, that could have been achieved by adding to the spec of Calc:
28450
28451 @smallexample @c ada
28452 pragma Elaborate_All (Constants);
28453 @end smallexample
28454
28455 @noindent
28456 which requires that the body (if any) and spec of @code{Constants},
28457 as well as the body and spec of any unit @code{with}'ed by
28458 @code{Constants} be elaborated before @code{Calc} is elaborated.
28459
28460 Clearly no automatic method can always guess which alternative you require,
28461 and if you are working with legacy code that had constraints of this kind
28462 which were not properly specified by adding @code{Elaborate} or
28463 @code{Elaborate_All} pragmas, then indeed it is possible that two different
28464 compilers can choose different orders.
28465
28466 However, GNAT does attempt to diagnose the common situation where there
28467 are uninitialized variables in the visible part of a package spec, and the
28468 corresponding package body has an elaboration block that directly or
28469 indirectly initialized one or more of these variables. This is the situation
28470 in which a pragma Elaborate_Body is usually desirable, and GNAT will generate
28471 a warning that suggests this addition if it detects this situation.
28472
28473 The @code{gnatbind}
28474 @option{^-p^/PESSIMISTIC_ELABORATION^} switch may be useful in smoking
28475 out problems. This switch causes bodies to be elaborated as late as possible
28476 instead of as early as possible. In the example above, it would have forced
28477 the choice of the first elaboration order. If you get different results
28478 when using this switch, and particularly if one set of results is right,
28479 and one is wrong as far as you are concerned, it shows that you have some
28480 missing @code{Elaborate} pragmas. For the example above, we have the
28481 following output:
28482
28483 @smallexample
28484 gnatmake -f -q main
28485 main
28486 7
28487 gnatmake -f -q main -bargs -p
28488 main
28489 0
28490 @end smallexample
28491
28492 @noindent
28493 It is of course quite unlikely that both these results are correct, so
28494 it is up to you in a case like this to investigate the source of the
28495 difference, by looking at the two elaboration orders that are chosen,
28496 and figuring out which is correct, and then adding the necessary
28497 @code{Elaborate} or @code{Elaborate_All} pragmas to ensure the desired order.
28498
28499
28500
28501 @c *******************************
28502 @node Conditional Compilation
28503 @appendix Conditional Compilation
28504 @c *******************************
28505 @cindex Conditional compilation
28506
28507 @noindent
28508 It is often necessary to arrange for a single source program
28509 to serve multiple purposes, where it is compiled in different
28510 ways to achieve these different goals. Some examples of the
28511 need for this feature are
28512
28513 @itemize @bullet
28514 @item Adapting a program to a different hardware environment
28515 @item Adapting a program to a different target architecture
28516 @item Turning debugging features on and off
28517 @item Arranging for a program to compile with different compilers
28518 @end itemize
28519
28520 @noindent
28521 In C, or C++, the typical approach would be to use the preprocessor
28522 that is defined as part of the language. The Ada language does not
28523 contain such a feature. This is not an oversight, but rather a very
28524 deliberate design decision, based on the experience that overuse of
28525 the preprocessing features in C and C++ can result in programs that
28526 are extremely difficult to maintain. For example, if we have ten
28527 switches that can be on or off, this means that there are a thousand
28528 separate programs, any one of which might not even be syntactically
28529 correct, and even if syntactically correct, the resulting program
28530 might not work correctly. Testing all combinations can quickly become
28531 impossible.
28532
28533 Nevertheless, the need to tailor programs certainly exists, and in
28534 this Appendix we will discuss how this can
28535 be achieved using Ada in general, and GNAT in particular.
28536
28537 @menu
28538 * Use of Boolean Constants::
28539 * Debugging - A Special Case::
28540 * Conditionalizing Declarations::
28541 * Use of Alternative Implementations::
28542 * Preprocessing::
28543 @end menu
28544
28545 @node Use of Boolean Constants
28546 @section Use of Boolean Constants
28547
28548 @noindent
28549 In the case where the difference is simply which code
28550 sequence is executed, the cleanest solution is to use Boolean
28551 constants to control which code is executed.
28552
28553 @smallexample @c ada
28554 @group
28555 FP_Initialize_Required : constant Boolean := True;
28556 @dots{}
28557 if FP_Initialize_Required then
28558 @dots{}
28559 end if;
28560 @end group
28561 @end smallexample
28562
28563 @noindent
28564 Not only will the code inside the @code{if} statement not be executed if
28565 the constant Boolean is @code{False}, but it will also be completely
28566 deleted from the program.
28567 However, the code is only deleted after the @code{if} statement
28568 has been checked for syntactic and semantic correctness.
28569 (In contrast, with preprocessors the code is deleted before the
28570 compiler ever gets to see it, so it is not checked until the switch
28571 is turned on.)
28572 @cindex Preprocessors (contrasted with conditional compilation)
28573
28574 Typically the Boolean constants will be in a separate package,
28575 something like:
28576
28577 @smallexample @c ada
28578 @group
28579 package Config is
28580 FP_Initialize_Required : constant Boolean := True;
28581 Reset_Available : constant Boolean := False;
28582 @dots{}
28583 end Config;
28584 @end group
28585 @end smallexample
28586
28587 @noindent
28588 The @code{Config} package exists in multiple forms for the various targets,
28589 with an appropriate script selecting the version of @code{Config} needed.
28590 Then any other unit requiring conditional compilation can do a @code{with}
28591 of @code{Config} to make the constants visible.
28592
28593
28594 @node Debugging - A Special Case
28595 @section Debugging - A Special Case
28596
28597 @noindent
28598 A common use of conditional code is to execute statements (for example
28599 dynamic checks, or output of intermediate results) under control of a
28600 debug switch, so that the debugging behavior can be turned on and off.
28601 This can be done using a Boolean constant to control whether the code
28602 is active:
28603
28604 @smallexample @c ada
28605 @group
28606 if Debugging then
28607 Put_Line ("got to the first stage!");
28608 end if;
28609 @end group
28610 @end smallexample
28611
28612 @noindent
28613 or
28614
28615 @smallexample @c ada
28616 @group
28617 if Debugging and then Temperature > 999.0 then
28618 raise Temperature_Crazy;
28619 end if;
28620 @end group
28621 @end smallexample
28622
28623 @noindent
28624 Since this is a common case, there are special features to deal with
28625 this in a convenient manner. For the case of tests, Ada 2005 has added
28626 a pragma @code{Assert} that can be used for such tests. This pragma is modeled
28627 @cindex pragma @code{Assert}
28628 on the @code{Assert} pragma that has always been available in GNAT, so this
28629 feature may be used with GNAT even if you are not using Ada 2005 features.
28630 The use of pragma @code{Assert} is described in
28631 @ref{Pragma Assert,,, gnat_rm, GNAT Reference Manual}, but as an
28632 example, the last test could be written:
28633
28634 @smallexample @c ada
28635 pragma Assert (Temperature <= 999.0, "Temperature Crazy");
28636 @end smallexample
28637
28638 @noindent
28639 or simply
28640
28641 @smallexample @c ada
28642 pragma Assert (Temperature <= 999.0);
28643 @end smallexample
28644
28645 @noindent
28646 In both cases, if assertions are active and the temperature is excessive,
28647 the exception @code{Assert_Failure} will be raised, with the given string in
28648 the first case or a string indicating the location of the pragma in the second
28649 case used as the exception message.
28650
28651 You can turn assertions on and off by using the @code{Assertion_Policy}
28652 pragma.
28653 @cindex pragma @code{Assertion_Policy}
28654 This is an Ada 2005 pragma which is implemented in all modes by
28655 GNAT, but only in the latest versions of GNAT which include Ada 2005
28656 capability. Alternatively, you can use the @option{-gnata} switch
28657 @cindex @option{-gnata} switch
28658 to enable assertions from the command line (this is recognized by all versions
28659 of GNAT).
28660
28661 For the example above with the @code{Put_Line}, the GNAT-specific pragma
28662 @code{Debug} can be used:
28663 @cindex pragma @code{Debug}
28664
28665 @smallexample @c ada
28666 pragma Debug (Put_Line ("got to the first stage!"));
28667 @end smallexample
28668
28669 @noindent
28670 If debug pragmas are enabled, the argument, which must be of the form of
28671 a procedure call, is executed (in this case, @code{Put_Line} will be called).
28672 Only one call can be present, but of course a special debugging procedure
28673 containing any code you like can be included in the program and then
28674 called in a pragma @code{Debug} argument as needed.
28675
28676 One advantage of pragma @code{Debug} over the @code{if Debugging then}
28677 construct is that pragma @code{Debug} can appear in declarative contexts,
28678 such as at the very beginning of a procedure, before local declarations have
28679 been elaborated.
28680
28681 Debug pragmas are enabled using either the @option{-gnata} switch that also
28682 controls assertions, or with a separate Debug_Policy pragma.
28683 @cindex pragma @code{Debug_Policy}
28684 The latter pragma is new in the Ada 2005 versions of GNAT (but it can be used
28685 in Ada 95 and Ada 83 programs as well), and is analogous to
28686 pragma @code{Assertion_Policy} to control assertions.
28687
28688 @code{Assertion_Policy} and @code{Debug_Policy} are configuration pragmas,
28689 and thus they can appear in @file{gnat.adc} if you are not using a
28690 project file, or in the file designated to contain configuration pragmas
28691 in a project file.
28692 They then apply to all subsequent compilations. In practice the use of
28693 the @option{-gnata} switch is often the most convenient method of controlling
28694 the status of these pragmas.
28695
28696 Note that a pragma is not a statement, so in contexts where a statement
28697 sequence is required, you can't just write a pragma on its own. You have
28698 to add a @code{null} statement.
28699
28700 @smallexample @c ada
28701 @group
28702 if @dots{} then
28703 @dots{} -- some statements
28704 else
28705 pragma Assert (Num_Cases < 10);
28706 null;
28707 end if;
28708 @end group
28709 @end smallexample
28710
28711
28712 @node Conditionalizing Declarations
28713 @section Conditionalizing Declarations
28714
28715 @noindent
28716 In some cases, it may be necessary to conditionalize declarations to meet
28717 different requirements. For example we might want a bit string whose length
28718 is set to meet some hardware message requirement.
28719
28720 In some cases, it may be possible to do this using declare blocks controlled
28721 by conditional constants:
28722
28723 @smallexample @c ada
28724 @group
28725 if Small_Machine then
28726 declare
28727 X : Bit_String (1 .. 10);
28728 begin
28729 @dots{}
28730 end;
28731 else
28732 declare
28733 X : Large_Bit_String (1 .. 1000);
28734 begin
28735 @dots{}
28736 end;
28737 end if;
28738 @end group
28739 @end smallexample
28740
28741 @noindent
28742 Note that in this approach, both declarations are analyzed by the
28743 compiler so this can only be used where both declarations are legal,
28744 even though one of them will not be used.
28745
28746 Another approach is to define integer constants, e.g.@: @code{Bits_Per_Word}, or
28747 Boolean constants, e.g.@: @code{Little_Endian}, and then write declarations
28748 that are parameterized by these constants. For example
28749
28750 @smallexample @c ada
28751 @group
28752 for Rec use
28753 Field1 at 0 range Boolean'Pos (Little_Endian) * 10 .. Bits_Per_Word;
28754 end record;
28755 @end group
28756 @end smallexample
28757
28758 @noindent
28759 If @code{Bits_Per_Word} is set to 32, this generates either
28760
28761 @smallexample @c ada
28762 @group
28763 for Rec use
28764 Field1 at 0 range 0 .. 32;
28765 end record;
28766 @end group
28767 @end smallexample
28768
28769 @noindent
28770 for the big endian case, or
28771
28772 @smallexample @c ada
28773 @group
28774 for Rec use record
28775 Field1 at 0 range 10 .. 32;
28776 end record;
28777 @end group
28778 @end smallexample
28779
28780 @noindent
28781 for the little endian case. Since a powerful subset of Ada expression
28782 notation is usable for creating static constants, clever use of this
28783 feature can often solve quite difficult problems in conditionalizing
28784 compilation (note incidentally that in Ada 95, the little endian
28785 constant was introduced as @code{System.Default_Bit_Order}, so you do not
28786 need to define this one yourself).
28787
28788
28789 @node Use of Alternative Implementations
28790 @section Use of Alternative Implementations
28791
28792 @noindent
28793 In some cases, none of the approaches described above are adequate. This
28794 can occur for example if the set of declarations required is radically
28795 different for two different configurations.
28796
28797 In this situation, the official Ada way of dealing with conditionalizing
28798 such code is to write separate units for the different cases. As long as
28799 this does not result in excessive duplication of code, this can be done
28800 without creating maintenance problems. The approach is to share common
28801 code as far as possible, and then isolate the code and declarations
28802 that are different. Subunits are often a convenient method for breaking
28803 out a piece of a unit that is to be conditionalized, with separate files
28804 for different versions of the subunit for different targets, where the
28805 build script selects the right one to give to the compiler.
28806 @cindex Subunits (and conditional compilation)
28807
28808 As an example, consider a situation where a new feature in Ada 2005
28809 allows something to be done in a really nice way. But your code must be able
28810 to compile with an Ada 95 compiler. Conceptually you want to say:
28811
28812 @smallexample @c ada
28813 @group
28814 if Ada_2005 then
28815 @dots{} neat Ada 2005 code
28816 else
28817 @dots{} not quite as neat Ada 95 code
28818 end if;
28819 @end group
28820 @end smallexample
28821
28822 @noindent
28823 where @code{Ada_2005} is a Boolean constant.
28824
28825 But this won't work when @code{Ada_2005} is set to @code{False},
28826 since the @code{then} clause will be illegal for an Ada 95 compiler.
28827 (Recall that although such unreachable code would eventually be deleted
28828 by the compiler, it still needs to be legal. If it uses features
28829 introduced in Ada 2005, it will be illegal in Ada 95.)
28830
28831 So instead we write
28832
28833 @smallexample @c ada
28834 procedure Insert is separate;
28835 @end smallexample
28836
28837 @noindent
28838 Then we have two files for the subunit @code{Insert}, with the two sets of
28839 code.
28840 If the package containing this is called @code{File_Queries}, then we might
28841 have two files
28842
28843 @itemize @bullet
28844 @item @file{file_queries-insert-2005.adb}
28845 @item @file{file_queries-insert-95.adb}
28846 @end itemize
28847
28848 @noindent
28849 and the build script renames the appropriate file to
28850
28851 @smallexample
28852 file_queries-insert.adb
28853 @end smallexample
28854
28855 @noindent
28856 and then carries out the compilation.
28857
28858 This can also be done with project files' naming schemes. For example:
28859
28860 @smallexample @c project
28861 For Body ("File_Queries.Insert") use "file_queries-insert-2005.ada";
28862 @end smallexample
28863
28864 @noindent
28865 Note also that with project files it is desirable to use a different extension
28866 than @file{ads} / @file{adb} for alternative versions. Otherwise a naming
28867 conflict may arise through another commonly used feature: to declare as part
28868 of the project a set of directories containing all the sources obeying the
28869 default naming scheme.
28870
28871 The use of alternative units is certainly feasible in all situations,
28872 and for example the Ada part of the GNAT run-time is conditionalized
28873 based on the target architecture using this approach. As a specific example,
28874 consider the implementation of the AST feature in VMS. There is one
28875 spec:
28876
28877 @smallexample
28878 s-asthan.ads
28879 @end smallexample
28880
28881 @noindent
28882 which is the same for all architectures, and three bodies:
28883
28884 @table @file
28885 @item s-asthan.adb
28886 used for all non-VMS operating systems
28887 @item s-asthan-vms-alpha.adb
28888 used for VMS on the Alpha
28889 @item s-asthan-vms-ia64.adb
28890 used for VMS on the ia64
28891 @end table
28892
28893 @noindent
28894 The dummy version @file{s-asthan.adb} simply raises exceptions noting that
28895 this operating system feature is not available, and the two remaining
28896 versions interface with the corresponding versions of VMS to provide
28897 VMS-compatible AST handling. The GNAT build script knows the architecture
28898 and operating system, and automatically selects the right version,
28899 renaming it if necessary to @file{s-asthan.adb} before the run-time build.
28900
28901 Another style for arranging alternative implementations is through Ada's
28902 access-to-subprogram facility.
28903 In case some functionality is to be conditionally included,
28904 you can declare an access-to-procedure variable @code{Ref} that is initialized
28905 to designate a ``do nothing'' procedure, and then invoke @code{Ref.all}
28906 when appropriate.
28907 In some library package, set @code{Ref} to @code{Proc'Access} for some
28908 procedure @code{Proc} that performs the relevant processing.
28909 The initialization only occurs if the library package is included in the
28910 program.
28911 The same idea can also be implemented using tagged types and dispatching
28912 calls.
28913
28914
28915 @node Preprocessing
28916 @section Preprocessing
28917 @cindex Preprocessing
28918
28919 @noindent
28920 Although it is quite possible to conditionalize code without the use of
28921 C-style preprocessing, as described earlier in this section, it is
28922 nevertheless convenient in some cases to use the C approach. Moreover,
28923 older Ada compilers have often provided some preprocessing capability,
28924 so legacy code may depend on this approach, even though it is not
28925 standard.
28926
28927 To accommodate such use, GNAT provides a preprocessor (modeled to a large
28928 extent on the various preprocessors that have been used
28929 with legacy code on other compilers, to enable easier transition).
28930
28931 The preprocessor may be used in two separate modes. It can be used quite
28932 separately from the compiler, to generate a separate output source file
28933 that is then fed to the compiler as a separate step. This is the
28934 @code{gnatprep} utility, whose use is fully described in
28935 @ref{Preprocessing Using gnatprep}.
28936 @cindex @code{gnatprep}
28937
28938 The preprocessing language allows such constructs as
28939
28940 @smallexample
28941 @group
28942 #if DEBUG or PRIORITY > 4 then
28943 bunch of declarations
28944 #else
28945 completely different bunch of declarations
28946 #end if;
28947 @end group
28948 @end smallexample
28949
28950 @noindent
28951 The values of the symbols @code{DEBUG} and @code{PRIORITY} can be
28952 defined either on the command line or in a separate file.
28953
28954 The other way of running the preprocessor is even closer to the C style and
28955 often more convenient. In this approach the preprocessing is integrated into
28956 the compilation process. The compiler is fed the preprocessor input which
28957 includes @code{#if} lines etc, and then the compiler carries out the
28958 preprocessing internally and processes the resulting output.
28959 For more details on this approach, see @ref{Integrated Preprocessing}.
28960
28961
28962 @c *******************************
28963 @node Inline Assembler
28964 @appendix Inline Assembler
28965 @c *******************************
28966
28967 @noindent
28968 If you need to write low-level software that interacts directly
28969 with the hardware, Ada provides two ways to incorporate assembly
28970 language code into your program. First, you can import and invoke
28971 external routines written in assembly language, an Ada feature fully
28972 supported by GNAT@. However, for small sections of code it may be simpler
28973 or more efficient to include assembly language statements directly
28974 in your Ada source program, using the facilities of the implementation-defined
28975 package @code{System.Machine_Code}, which incorporates the gcc
28976 Inline Assembler. The Inline Assembler approach offers a number of advantages,
28977 including the following:
28978
28979 @itemize @bullet
28980 @item No need to use non-Ada tools
28981 @item Consistent interface over different targets
28982 @item Automatic usage of the proper calling conventions
28983 @item Access to Ada constants and variables
28984 @item Definition of intrinsic routines
28985 @item Possibility of inlining a subprogram comprising assembler code
28986 @item Code optimizer can take Inline Assembler code into account
28987 @end itemize
28988
28989 This chapter presents a series of examples to show you how to use
28990 the Inline Assembler. Although it focuses on the Intel x86,
28991 the general approach applies also to other processors.
28992 It is assumed that you are familiar with Ada
28993 and with assembly language programming.
28994
28995 @menu
28996 * Basic Assembler Syntax::
28997 * A Simple Example of Inline Assembler::
28998 * Output Variables in Inline Assembler::
28999 * Input Variables in Inline Assembler::
29000 * Inlining Inline Assembler Code::
29001 * Other Asm Functionality::
29002 @end menu
29003
29004 @c ---------------------------------------------------------------------------
29005 @node Basic Assembler Syntax
29006 @section Basic Assembler Syntax
29007
29008 @noindent
29009 The assembler used by GNAT and gcc is based not on the Intel assembly
29010 language, but rather on a language that descends from the AT&T Unix
29011 assembler @emph{as} (and which is often referred to as ``AT&T syntax'').
29012 The following table summarizes the main features of @emph{as} syntax
29013 and points out the differences from the Intel conventions.
29014 See the gcc @emph{as} and @emph{gas} (an @emph{as} macro
29015 pre-processor) documentation for further information.
29016
29017 @table @asis
29018 @item Register names
29019 gcc / @emph{as}: Prefix with ``%''; for example @code{%eax}
29020 @*
29021 Intel: No extra punctuation; for example @code{eax}
29022
29023 @item Immediate operand
29024 gcc / @emph{as}: Prefix with ``$''; for example @code{$4}
29025 @*
29026 Intel: No extra punctuation; for example @code{4}
29027
29028 @item Address
29029 gcc / @emph{as}: Prefix with ``$''; for example @code{$loc}
29030 @*
29031 Intel: No extra punctuation; for example @code{loc}
29032
29033 @item Memory contents
29034 gcc / @emph{as}: No extra punctuation; for example @code{loc}
29035 @*
29036 Intel: Square brackets; for example @code{[loc]}
29037
29038 @item Register contents
29039 gcc / @emph{as}: Parentheses; for example @code{(%eax)}
29040 @*
29041 Intel: Square brackets; for example @code{[eax]}
29042
29043 @item Hexadecimal numbers
29044 gcc / @emph{as}: Leading ``0x'' (C language syntax); for example @code{0xA0}
29045 @*
29046 Intel: Trailing ``h''; for example @code{A0h}
29047
29048 @item Operand size
29049 gcc / @emph{as}: Explicit in op code; for example @code{movw} to move
29050 a 16-bit word
29051 @*
29052 Intel: Implicit, deduced by assembler; for example @code{mov}
29053
29054 @item Instruction repetition
29055 gcc / @emph{as}: Split into two lines; for example
29056 @*
29057 @code{rep}
29058 @*
29059 @code{stosl}
29060 @*
29061 Intel: Keep on one line; for example @code{rep stosl}
29062
29063 @item Order of operands
29064 gcc / @emph{as}: Source first; for example @code{movw $4, %eax}
29065 @*
29066 Intel: Destination first; for example @code{mov eax, 4}
29067 @end table
29068
29069 @c ---------------------------------------------------------------------------
29070 @node A Simple Example of Inline Assembler
29071 @section A Simple Example of Inline Assembler
29072
29073 @noindent
29074 The following example will generate a single assembly language statement,
29075 @code{nop}, which does nothing. Despite its lack of run-time effect,
29076 the example will be useful in illustrating the basics of
29077 the Inline Assembler facility.
29078
29079 @smallexample @c ada
29080 @group
29081 with System.Machine_Code; use System.Machine_Code;
29082 procedure Nothing is
29083 begin
29084 Asm ("nop");
29085 end Nothing;
29086 @end group
29087 @end smallexample
29088
29089 @code{Asm} is a procedure declared in package @code{System.Machine_Code};
29090 here it takes one parameter, a @emph{template string} that must be a static
29091 expression and that will form the generated instruction.
29092 @code{Asm} may be regarded as a compile-time procedure that parses
29093 the template string and additional parameters (none here),
29094 from which it generates a sequence of assembly language instructions.
29095
29096 The examples in this chapter will illustrate several of the forms
29097 for invoking @code{Asm}; a complete specification of the syntax
29098 is found in @ref{Machine Code Insertions,,, gnat_rm, GNAT Reference
29099 Manual}.
29100
29101 Under the standard GNAT conventions, the @code{Nothing} procedure
29102 should be in a file named @file{nothing.adb}.
29103 You can build the executable in the usual way:
29104 @smallexample
29105 gnatmake nothing
29106 @end smallexample
29107 However, the interesting aspect of this example is not its run-time behavior
29108 but rather the generated assembly code.
29109 To see this output, invoke the compiler as follows:
29110 @smallexample
29111 gcc -c -S -fomit-frame-pointer -gnatp @file{nothing.adb}
29112 @end smallexample
29113 where the options are:
29114
29115 @table @code
29116 @item -c
29117 compile only (no bind or link)
29118 @item -S
29119 generate assembler listing
29120 @item -fomit-frame-pointer
29121 do not set up separate stack frames
29122 @item -gnatp
29123 do not add runtime checks
29124 @end table
29125
29126 This gives a human-readable assembler version of the code. The resulting
29127 file will have the same name as the Ada source file, but with a @code{.s}
29128 extension. In our example, the file @file{nothing.s} has the following
29129 contents:
29130
29131 @smallexample
29132 @group
29133 .file "nothing.adb"
29134 gcc2_compiled.:
29135 ___gnu_compiled_ada:
29136 .text
29137 .align 4
29138 .globl __ada_nothing
29139 __ada_nothing:
29140 #APP
29141 nop
29142 #NO_APP
29143 jmp L1
29144 .align 2,0x90
29145 L1:
29146 ret
29147 @end group
29148 @end smallexample
29149
29150 The assembly code you included is clearly indicated by
29151 the compiler, between the @code{#APP} and @code{#NO_APP}
29152 delimiters. The character before the 'APP' and 'NOAPP'
29153 can differ on different targets. For example, GNU/Linux uses '#APP' while
29154 on NT you will see '/APP'.
29155
29156 If you make a mistake in your assembler code (such as using the
29157 wrong size modifier, or using a wrong operand for the instruction) GNAT
29158 will report this error in a temporary file, which will be deleted when
29159 the compilation is finished. Generating an assembler file will help
29160 in such cases, since you can assemble this file separately using the
29161 @emph{as} assembler that comes with gcc.
29162
29163 Assembling the file using the command
29164
29165 @smallexample
29166 as @file{nothing.s}
29167 @end smallexample
29168 @noindent
29169 will give you error messages whose lines correspond to the assembler
29170 input file, so you can easily find and correct any mistakes you made.
29171 If there are no errors, @emph{as} will generate an object file
29172 @file{nothing.out}.
29173
29174 @c ---------------------------------------------------------------------------
29175 @node Output Variables in Inline Assembler
29176 @section Output Variables in Inline Assembler
29177
29178 @noindent
29179 The examples in this section, showing how to access the processor flags,
29180 illustrate how to specify the destination operands for assembly language
29181 statements.
29182
29183 @smallexample @c ada
29184 @group
29185 with Interfaces; use Interfaces;
29186 with Ada.Text_IO; use Ada.Text_IO;
29187 with System.Machine_Code; use System.Machine_Code;
29188 procedure Get_Flags is
29189 Flags : Unsigned_32;
29190 use ASCII;
29191 begin
29192 Asm ("pushfl" & LF & HT & -- push flags on stack
29193 "popl %%eax" & LF & HT & -- load eax with flags
29194 "movl %%eax, %0", -- store flags in variable
29195 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29196 Put_Line ("Flags register:" & Flags'Img);
29197 end Get_Flags;
29198 @end group
29199 @end smallexample
29200
29201 In order to have a nicely aligned assembly listing, we have separated
29202 multiple assembler statements in the Asm template string with linefeed
29203 (ASCII.LF) and horizontal tab (ASCII.HT) characters.
29204 The resulting section of the assembly output file is:
29205
29206 @smallexample
29207 @group
29208 #APP
29209 pushfl
29210 popl %eax
29211 movl %eax, -40(%ebp)
29212 #NO_APP
29213 @end group
29214 @end smallexample
29215
29216 It would have been legal to write the Asm invocation as:
29217
29218 @smallexample
29219 Asm ("pushfl popl %%eax movl %%eax, %0")
29220 @end smallexample
29221
29222 but in the generated assembler file, this would come out as:
29223
29224 @smallexample
29225 #APP
29226 pushfl popl %eax movl %eax, -40(%ebp)
29227 #NO_APP
29228 @end smallexample
29229
29230 which is not so convenient for the human reader.
29231
29232 We use Ada comments
29233 at the end of each line to explain what the assembler instructions
29234 actually do. This is a useful convention.
29235
29236 When writing Inline Assembler instructions, you need to precede each register
29237 and variable name with a percent sign. Since the assembler already requires
29238 a percent sign at the beginning of a register name, you need two consecutive
29239 percent signs for such names in the Asm template string, thus @code{%%eax}.
29240 In the generated assembly code, one of the percent signs will be stripped off.
29241
29242 Names such as @code{%0}, @code{%1}, @code{%2}, etc., denote input or output
29243 variables: operands you later define using @code{Input} or @code{Output}
29244 parameters to @code{Asm}.
29245 An output variable is illustrated in
29246 the third statement in the Asm template string:
29247 @smallexample
29248 movl %%eax, %0
29249 @end smallexample
29250 The intent is to store the contents of the eax register in a variable that can
29251 be accessed in Ada. Simply writing @code{movl %%eax, Flags} would not
29252 necessarily work, since the compiler might optimize by using a register
29253 to hold Flags, and the expansion of the @code{movl} instruction would not be
29254 aware of this optimization. The solution is not to store the result directly
29255 but rather to advise the compiler to choose the correct operand form;
29256 that is the purpose of the @code{%0} output variable.
29257
29258 Information about the output variable is supplied in the @code{Outputs}
29259 parameter to @code{Asm}:
29260 @smallexample
29261 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29262 @end smallexample
29263
29264 The output is defined by the @code{Asm_Output} attribute of the target type;
29265 the general format is
29266 @smallexample
29267 Type'Asm_Output (constraint_string, variable_name)
29268 @end smallexample
29269
29270 The constraint string directs the compiler how
29271 to store/access the associated variable. In the example
29272 @smallexample
29273 Unsigned_32'Asm_Output ("=m", Flags);
29274 @end smallexample
29275 the @code{"m"} (memory) constraint tells the compiler that the variable
29276 @code{Flags} should be stored in a memory variable, thus preventing
29277 the optimizer from keeping it in a register. In contrast,
29278 @smallexample
29279 Unsigned_32'Asm_Output ("=r", Flags);
29280 @end smallexample
29281 uses the @code{"r"} (register) constraint, telling the compiler to
29282 store the variable in a register.
29283
29284 If the constraint is preceded by the equal character (@strong{=}), it tells
29285 the compiler that the variable will be used to store data into it.
29286
29287 In the @code{Get_Flags} example, we used the @code{"g"} (global) constraint,
29288 allowing the optimizer to choose whatever it deems best.
29289
29290 There are a fairly large number of constraints, but the ones that are
29291 most useful (for the Intel x86 processor) are the following:
29292
29293 @table @code
29294 @item =
29295 output constraint
29296 @item g
29297 global (i.e.@: can be stored anywhere)
29298 @item m
29299 in memory
29300 @item I
29301 a constant
29302 @item a
29303 use eax
29304 @item b
29305 use ebx
29306 @item c
29307 use ecx
29308 @item d
29309 use edx
29310 @item S
29311 use esi
29312 @item D
29313 use edi
29314 @item r
29315 use one of eax, ebx, ecx or edx
29316 @item q
29317 use one of eax, ebx, ecx, edx, esi or edi
29318 @end table
29319
29320 The full set of constraints is described in the gcc and @emph{as}
29321 documentation; note that it is possible to combine certain constraints
29322 in one constraint string.
29323
29324 You specify the association of an output variable with an assembler operand
29325 through the @code{%}@emph{n} notation, where @emph{n} is a non-negative
29326 integer. Thus in
29327 @smallexample @c ada
29328 @group
29329 Asm ("pushfl" & LF & HT & -- push flags on stack
29330 "popl %%eax" & LF & HT & -- load eax with flags
29331 "movl %%eax, %0", -- store flags in variable
29332 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29333 @end group
29334 @end smallexample
29335 @noindent
29336 @code{%0} will be replaced in the expanded code by the appropriate operand,
29337 whatever
29338 the compiler decided for the @code{Flags} variable.
29339
29340 In general, you may have any number of output variables:
29341 @itemize @bullet
29342 @item
29343 Count the operands starting at 0; thus @code{%0}, @code{%1}, etc.
29344 @item
29345 Specify the @code{Outputs} parameter as a parenthesized comma-separated list
29346 of @code{Asm_Output} attributes
29347 @end itemize
29348
29349 For example:
29350 @smallexample @c ada
29351 @group
29352 Asm ("movl %%eax, %0" & LF & HT &
29353 "movl %%ebx, %1" & LF & HT &
29354 "movl %%ecx, %2",
29355 Outputs => (Unsigned_32'Asm_Output ("=g", Var_A), -- %0 = Var_A
29356 Unsigned_32'Asm_Output ("=g", Var_B), -- %1 = Var_B
29357 Unsigned_32'Asm_Output ("=g", Var_C))); -- %2 = Var_C
29358 @end group
29359 @end smallexample
29360 @noindent
29361 where @code{Var_A}, @code{Var_B}, and @code{Var_C} are variables
29362 in the Ada program.
29363
29364 As a variation on the @code{Get_Flags} example, we can use the constraints
29365 string to direct the compiler to store the eax register into the @code{Flags}
29366 variable, instead of including the store instruction explicitly in the
29367 @code{Asm} template string:
29368
29369 @smallexample @c ada
29370 @group
29371 with Interfaces; use Interfaces;
29372 with Ada.Text_IO; use Ada.Text_IO;
29373 with System.Machine_Code; use System.Machine_Code;
29374 procedure Get_Flags_2 is
29375 Flags : Unsigned_32;
29376 use ASCII;
29377 begin
29378 Asm ("pushfl" & LF & HT & -- push flags on stack
29379 "popl %%eax", -- save flags in eax
29380 Outputs => Unsigned_32'Asm_Output ("=a", Flags));
29381 Put_Line ("Flags register:" & Flags'Img);
29382 end Get_Flags_2;
29383 @end group
29384 @end smallexample
29385
29386 @noindent
29387 The @code{"a"} constraint tells the compiler that the @code{Flags}
29388 variable will come from the eax register. Here is the resulting code:
29389
29390 @smallexample
29391 @group
29392 #APP
29393 pushfl
29394 popl %eax
29395 #NO_APP
29396 movl %eax,-40(%ebp)
29397 @end group
29398 @end smallexample
29399
29400 @noindent
29401 The compiler generated the store of eax into Flags after
29402 expanding the assembler code.
29403
29404 Actually, there was no need to pop the flags into the eax register;
29405 more simply, we could just pop the flags directly into the program variable:
29406
29407 @smallexample @c ada
29408 @group
29409 with Interfaces; use Interfaces;
29410 with Ada.Text_IO; use Ada.Text_IO;
29411 with System.Machine_Code; use System.Machine_Code;
29412 procedure Get_Flags_3 is
29413 Flags : Unsigned_32;
29414 use ASCII;
29415 begin
29416 Asm ("pushfl" & LF & HT & -- push flags on stack
29417 "pop %0", -- save flags in Flags
29418 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29419 Put_Line ("Flags register:" & Flags'Img);
29420 end Get_Flags_3;
29421 @end group
29422 @end smallexample
29423
29424 @c ---------------------------------------------------------------------------
29425 @node Input Variables in Inline Assembler
29426 @section Input Variables in Inline Assembler
29427
29428 @noindent
29429 The example in this section illustrates how to specify the source operands
29430 for assembly language statements.
29431 The program simply increments its input value by 1:
29432
29433 @smallexample @c ada
29434 @group
29435 with Interfaces; use Interfaces;
29436 with Ada.Text_IO; use Ada.Text_IO;
29437 with System.Machine_Code; use System.Machine_Code;
29438 procedure Increment is
29439
29440 function Incr (Value : Unsigned_32) return Unsigned_32 is
29441 Result : Unsigned_32;
29442 begin
29443 Asm ("incl %0",
29444 Inputs => Unsigned_32'Asm_Input ("a", Value),
29445 Outputs => Unsigned_32'Asm_Output ("=a", Result));
29446 return Result;
29447 end Incr;
29448
29449 Value : Unsigned_32;
29450
29451 begin
29452 Value := 5;
29453 Put_Line ("Value before is" & Value'Img);
29454 Value := Incr (Value);
29455 Put_Line ("Value after is" & Value'Img);
29456 end Increment;
29457 @end group
29458 @end smallexample
29459
29460 The @code{Outputs} parameter to @code{Asm} specifies
29461 that the result will be in the eax register and that it is to be stored
29462 in the @code{Result} variable.
29463
29464 The @code{Inputs} parameter looks much like the @code{Outputs} parameter,
29465 but with an @code{Asm_Input} attribute.
29466 The @code{"="} constraint, indicating an output value, is not present.
29467
29468 You can have multiple input variables, in the same way that you can have more
29469 than one output variable.
29470
29471 The parameter count (%0, %1) etc, now starts at the first input
29472 statement, and continues with the output statements.
29473 When both parameters use the same variable, the
29474 compiler will treat them as the same %n operand, which is the case here.
29475
29476 Just as the @code{Outputs} parameter causes the register to be stored into the
29477 target variable after execution of the assembler statements, so does the
29478 @code{Inputs} parameter cause its variable to be loaded into the register
29479 before execution of the assembler statements.
29480
29481 Thus the effect of the @code{Asm} invocation is:
29482 @enumerate
29483 @item load the 32-bit value of @code{Value} into eax
29484 @item execute the @code{incl %eax} instruction
29485 @item store the contents of eax into the @code{Result} variable
29486 @end enumerate
29487
29488 The resulting assembler file (with @option{-O2} optimization) contains:
29489 @smallexample
29490 @group
29491 _increment__incr.1:
29492 subl $4,%esp
29493 movl 8(%esp),%eax
29494 #APP
29495 incl %eax
29496 #NO_APP
29497 movl %eax,%edx
29498 movl %ecx,(%esp)
29499 addl $4,%esp
29500 ret
29501 @end group
29502 @end smallexample
29503
29504 @c ---------------------------------------------------------------------------
29505 @node Inlining Inline Assembler Code
29506 @section Inlining Inline Assembler Code
29507
29508 @noindent
29509 For a short subprogram such as the @code{Incr} function in the previous
29510 section, the overhead of the call and return (creating / deleting the stack
29511 frame) can be significant, compared to the amount of code in the subprogram
29512 body. A solution is to apply Ada's @code{Inline} pragma to the subprogram,
29513 which directs the compiler to expand invocations of the subprogram at the
29514 point(s) of call, instead of setting up a stack frame for out-of-line calls.
29515 Here is the resulting program:
29516
29517 @smallexample @c ada
29518 @group
29519 with Interfaces; use Interfaces;
29520 with Ada.Text_IO; use Ada.Text_IO;
29521 with System.Machine_Code; use System.Machine_Code;
29522 procedure Increment_2 is
29523
29524 function Incr (Value : Unsigned_32) return Unsigned_32 is
29525 Result : Unsigned_32;
29526 begin
29527 Asm ("incl %0",
29528 Inputs => Unsigned_32'Asm_Input ("a", Value),
29529 Outputs => Unsigned_32'Asm_Output ("=a", Result));
29530 return Result;
29531 end Incr;
29532 pragma Inline (Increment);
29533
29534 Value : Unsigned_32;
29535
29536 begin
29537 Value := 5;
29538 Put_Line ("Value before is" & Value'Img);
29539 Value := Increment (Value);
29540 Put_Line ("Value after is" & Value'Img);
29541 end Increment_2;
29542 @end group
29543 @end smallexample
29544
29545 Compile the program with both optimization (@option{-O2}) and inlining
29546 (@option{-gnatn}) enabled.
29547
29548 The @code{Incr} function is still compiled as usual, but at the
29549 point in @code{Increment} where our function used to be called:
29550
29551 @smallexample
29552 @group
29553 pushl %edi
29554 call _increment__incr.1
29555 @end group
29556 @end smallexample
29557
29558 @noindent
29559 the code for the function body directly appears:
29560
29561 @smallexample
29562 @group
29563 movl %esi,%eax
29564 #APP
29565 incl %eax
29566 #NO_APP
29567 movl %eax,%edx
29568 @end group
29569 @end smallexample
29570
29571 @noindent
29572 thus saving the overhead of stack frame setup and an out-of-line call.
29573
29574 @c ---------------------------------------------------------------------------
29575 @node Other Asm Functionality
29576 @section Other @code{Asm} Functionality
29577
29578 @noindent
29579 This section describes two important parameters to the @code{Asm}
29580 procedure: @code{Clobber}, which identifies register usage;
29581 and @code{Volatile}, which inhibits unwanted optimizations.
29582
29583 @menu
29584 * The Clobber Parameter::
29585 * The Volatile Parameter::
29586 @end menu
29587
29588 @c ---------------------------------------------------------------------------
29589 @node The Clobber Parameter
29590 @subsection The @code{Clobber} Parameter
29591
29592 @noindent
29593 One of the dangers of intermixing assembly language and a compiled language
29594 such as Ada is that the compiler needs to be aware of which registers are
29595 being used by the assembly code. In some cases, such as the earlier examples,
29596 the constraint string is sufficient to indicate register usage (e.g.,
29597 @code{"a"} for
29598 the eax register). But more generally, the compiler needs an explicit
29599 identification of the registers that are used by the Inline Assembly
29600 statements.
29601
29602 Using a register that the compiler doesn't know about
29603 could be a side effect of an instruction (like @code{mull}
29604 storing its result in both eax and edx).
29605 It can also arise from explicit register usage in your
29606 assembly code; for example:
29607 @smallexample
29608 @group
29609 Asm ("movl %0, %%ebx" & LF & HT &
29610 "movl %%ebx, %1",
29611 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
29612 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out));
29613 @end group
29614 @end smallexample
29615 @noindent
29616 where the compiler (since it does not analyze the @code{Asm} template string)
29617 does not know you are using the ebx register.
29618
29619 In such cases you need to supply the @code{Clobber} parameter to @code{Asm},
29620 to identify the registers that will be used by your assembly code:
29621
29622 @smallexample
29623 @group
29624 Asm ("movl %0, %%ebx" & LF & HT &
29625 "movl %%ebx, %1",
29626 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
29627 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
29628 Clobber => "ebx");
29629 @end group
29630 @end smallexample
29631
29632 The Clobber parameter is a static string expression specifying the
29633 register(s) you are using. Note that register names are @emph{not} prefixed
29634 by a percent sign. Also, if more than one register is used then their names
29635 are separated by commas; e.g., @code{"eax, ebx"}
29636
29637 The @code{Clobber} parameter has several additional uses:
29638 @enumerate
29639 @item Use ``register'' name @code{cc} to indicate that flags might have changed
29640 @item Use ``register'' name @code{memory} if you changed a memory location
29641 @end enumerate
29642
29643 @c ---------------------------------------------------------------------------
29644 @node The Volatile Parameter
29645 @subsection The @code{Volatile} Parameter
29646 @cindex Volatile parameter
29647
29648 @noindent
29649 Compiler optimizations in the presence of Inline Assembler may sometimes have
29650 unwanted effects. For example, when an @code{Asm} invocation with an input
29651 variable is inside a loop, the compiler might move the loading of the input
29652 variable outside the loop, regarding it as a one-time initialization.
29653
29654 If this effect is not desired, you can disable such optimizations by setting
29655 the @code{Volatile} parameter to @code{True}; for example:
29656
29657 @smallexample @c ada
29658 @group
29659 Asm ("movl %0, %%ebx" & LF & HT &
29660 "movl %%ebx, %1",
29661 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
29662 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
29663 Clobber => "ebx",
29664 Volatile => True);
29665 @end group
29666 @end smallexample
29667
29668 By default, @code{Volatile} is set to @code{False} unless there is no
29669 @code{Outputs} parameter.
29670
29671 Although setting @code{Volatile} to @code{True} prevents unwanted
29672 optimizations, it will also disable other optimizations that might be
29673 important for efficiency. In general, you should set @code{Volatile}
29674 to @code{True} only if the compiler's optimizations have created
29675 problems.
29676 @c END OF INLINE ASSEMBLER CHAPTER
29677 @c ===============================
29678
29679 @c ***********************************
29680 @c * Compatibility and Porting Guide *
29681 @c ***********************************
29682 @node Compatibility and Porting Guide
29683 @appendix Compatibility and Porting Guide
29684
29685 @noindent
29686 This chapter describes the compatibility issues that may arise between
29687 GNAT and other Ada compilation systems (including those for Ada 83),
29688 and shows how GNAT can expedite porting
29689 applications developed in other Ada environments.
29690
29691 @menu
29692 * Compatibility with Ada 83::
29693 * Compatibility between Ada 95 and Ada 2005::
29694 * Implementation-dependent characteristics::
29695 * Compatibility with Other Ada Systems::
29696 * Representation Clauses::
29697 @ifclear vms
29698 @c Brief section is only in non-VMS version
29699 @c Full chapter is in VMS version
29700 * Compatibility with HP Ada 83::
29701 @end ifclear
29702 @ifset vms
29703 * Transitioning to 64-Bit GNAT for OpenVMS::
29704 @end ifset
29705 @end menu
29706
29707 @node Compatibility with Ada 83
29708 @section Compatibility with Ada 83
29709 @cindex Compatibility (between Ada 83 and Ada 95 / Ada 2005)
29710
29711 @noindent
29712 Ada 95 and Ada 2005 are highly upwards compatible with Ada 83. In
29713 particular, the design intention was that the difficulties associated
29714 with moving from Ada 83 to Ada 95 or Ada 2005 should be no greater than those
29715 that occur when moving from one Ada 83 system to another.
29716
29717 However, there are a number of points at which there are minor
29718 incompatibilities. The @cite{Ada 95 Annotated Reference Manual} contains
29719 full details of these issues,
29720 and should be consulted for a complete treatment.
29721 In practice the
29722 following subsections treat the most likely issues to be encountered.
29723
29724 @menu
29725 * Legal Ada 83 programs that are illegal in Ada 95::
29726 * More deterministic semantics::
29727 * Changed semantics::
29728 * Other language compatibility issues::
29729 @end menu
29730
29731 @node Legal Ada 83 programs that are illegal in Ada 95
29732 @subsection Legal Ada 83 programs that are illegal in Ada 95
29733
29734 Some legal Ada 83 programs are illegal (i.e., they will fail to compile) in
29735 Ada 95 and thus also in Ada 2005:
29736
29737 @table @emph
29738 @item Character literals
29739 Some uses of character literals are ambiguous. Since Ada 95 has introduced
29740 @code{Wide_Character} as a new predefined character type, some uses of
29741 character literals that were legal in Ada 83 are illegal in Ada 95.
29742 For example:
29743 @smallexample @c ada
29744 for Char in 'A' .. 'Z' loop @dots{} end loop;
29745 @end smallexample
29746
29747 @noindent
29748 The problem is that @code{'A'} and @code{'Z'} could be from either
29749 @code{Character} or @code{Wide_Character}. The simplest correction
29750 is to make the type explicit; e.g.:
29751 @smallexample @c ada
29752 for Char in Character range 'A' .. 'Z' loop @dots{} end loop;
29753 @end smallexample
29754
29755 @item New reserved words
29756 The identifiers @code{abstract}, @code{aliased}, @code{protected},
29757 @code{requeue}, @code{tagged}, and @code{until} are reserved in Ada 95.
29758 Existing Ada 83 code using any of these identifiers must be edited to
29759 use some alternative name.
29760
29761 @item Freezing rules
29762 The rules in Ada 95 are slightly different with regard to the point at
29763 which entities are frozen, and representation pragmas and clauses are
29764 not permitted past the freeze point. This shows up most typically in
29765 the form of an error message complaining that a representation item
29766 appears too late, and the appropriate corrective action is to move
29767 the item nearer to the declaration of the entity to which it refers.
29768
29769 A particular case is that representation pragmas
29770 @ifset vms
29771 (including the
29772 extended HP Ada 83 compatibility pragmas such as @code{Export_Procedure})
29773 @end ifset
29774 cannot be applied to a subprogram body. If necessary, a separate subprogram
29775 declaration must be introduced to which the pragma can be applied.
29776
29777 @item Optional bodies for library packages
29778 In Ada 83, a package that did not require a package body was nevertheless
29779 allowed to have one. This lead to certain surprises in compiling large
29780 systems (situations in which the body could be unexpectedly ignored by the
29781 binder). In Ada 95, if a package does not require a body then it is not
29782 permitted to have a body. To fix this problem, simply remove a redundant
29783 body if it is empty, or, if it is non-empty, introduce a dummy declaration
29784 into the spec that makes the body required. One approach is to add a private
29785 part to the package declaration (if necessary), and define a parameterless
29786 procedure called @code{Requires_Body}, which must then be given a dummy
29787 procedure body in the package body, which then becomes required.
29788 Another approach (assuming that this does not introduce elaboration
29789 circularities) is to add an @code{Elaborate_Body} pragma to the package spec,
29790 since one effect of this pragma is to require the presence of a package body.
29791
29792 @item @code{Numeric_Error} is now the same as @code{Constraint_Error}
29793 In Ada 95, the exception @code{Numeric_Error} is a renaming of
29794 @code{Constraint_Error}.
29795 This means that it is illegal to have separate exception handlers for
29796 the two exceptions. The fix is simply to remove the handler for the
29797 @code{Numeric_Error} case (since even in Ada 83, a compiler was free to raise
29798 @code{Constraint_Error} in place of @code{Numeric_Error} in all cases).
29799
29800 @item Indefinite subtypes in generics
29801 In Ada 83, it was permissible to pass an indefinite type (e.g.@: @code{String})
29802 as the actual for a generic formal private type, but then the instantiation
29803 would be illegal if there were any instances of declarations of variables
29804 of this type in the generic body. In Ada 95, to avoid this clear violation
29805 of the methodological principle known as the ``contract model'',
29806 the generic declaration explicitly indicates whether
29807 or not such instantiations are permitted. If a generic formal parameter
29808 has explicit unknown discriminants, indicated by using @code{(<>)} after the
29809 type name, then it can be instantiated with indefinite types, but no
29810 stand-alone variables can be declared of this type. Any attempt to declare
29811 such a variable will result in an illegality at the time the generic is
29812 declared. If the @code{(<>)} notation is not used, then it is illegal
29813 to instantiate the generic with an indefinite type.
29814 This is the potential incompatibility issue when porting Ada 83 code to Ada 95.
29815 It will show up as a compile time error, and
29816 the fix is usually simply to add the @code{(<>)} to the generic declaration.
29817 @end table
29818
29819 @node More deterministic semantics
29820 @subsection More deterministic semantics
29821
29822 @table @emph
29823 @item Conversions
29824 Conversions from real types to integer types round away from 0. In Ada 83
29825 the conversion Integer(2.5) could deliver either 2 or 3 as its value. This
29826 implementation freedom was intended to support unbiased rounding in
29827 statistical applications, but in practice it interfered with portability.
29828 In Ada 95 the conversion semantics are unambiguous, and rounding away from 0
29829 is required. Numeric code may be affected by this change in semantics.
29830 Note, though, that this issue is no worse than already existed in Ada 83
29831 when porting code from one vendor to another.
29832
29833 @item Tasking
29834 The Real-Time Annex introduces a set of policies that define the behavior of
29835 features that were implementation dependent in Ada 83, such as the order in
29836 which open select branches are executed.
29837 @end table
29838
29839 @node Changed semantics
29840 @subsection Changed semantics
29841
29842 @noindent
29843 The worst kind of incompatibility is one where a program that is legal in
29844 Ada 83 is also legal in Ada 95 but can have an effect in Ada 95 that was not
29845 possible in Ada 83. Fortunately this is extremely rare, but the one
29846 situation that you should be alert to is the change in the predefined type
29847 @code{Character} from 7-bit ASCII to 8-bit Latin-1.
29848
29849 @table @emph
29850 @item Range of type @code{Character}
29851 The range of @code{Standard.Character} is now the full 256 characters
29852 of Latin-1, whereas in most Ada 83 implementations it was restricted
29853 to 128 characters. Although some of the effects of
29854 this change will be manifest in compile-time rejection of legal
29855 Ada 83 programs it is possible for a working Ada 83 program to have
29856 a different effect in Ada 95, one that was not permitted in Ada 83.
29857 As an example, the expression
29858 @code{Character'Pos(Character'Last)} returned @code{127} in Ada 83 and now
29859 delivers @code{255} as its value.
29860 In general, you should look at the logic of any
29861 character-processing Ada 83 program and see whether it needs to be adapted
29862 to work correctly with Latin-1. Note that the predefined Ada 95 API has a
29863 character handling package that may be relevant if code needs to be adapted
29864 to account for the additional Latin-1 elements.
29865 The desirable fix is to
29866 modify the program to accommodate the full character set, but in some cases
29867 it may be convenient to define a subtype or derived type of Character that
29868 covers only the restricted range.
29869 @cindex Latin-1
29870 @end table
29871
29872 @node Other language compatibility issues
29873 @subsection Other language compatibility issues
29874
29875 @table @emph
29876 @item @option{-gnat83} switch
29877 All implementations of GNAT provide a switch that causes GNAT to operate
29878 in Ada 83 mode. In this mode, some but not all compatibility problems
29879 of the type described above are handled automatically. For example, the
29880 new reserved words introduced in Ada 95 and Ada 2005 are treated simply
29881 as identifiers as in Ada 83.
29882 However,
29883 in practice, it is usually advisable to make the necessary modifications
29884 to the program to remove the need for using this switch.
29885 See @ref{Compiling Different Versions of Ada}.
29886
29887 @item Support for removed Ada 83 pragmas and attributes
29888 A number of pragmas and attributes from Ada 83 were removed from Ada 95,
29889 generally because they were replaced by other mechanisms. Ada 95 and Ada 2005
29890 compilers are allowed, but not required, to implement these missing
29891 elements. In contrast with some other compilers, GNAT implements all
29892 such pragmas and attributes, eliminating this compatibility concern. These
29893 include @code{pragma Interface} and the floating point type attributes
29894 (@code{Emax}, @code{Mantissa}, etc.), among other items.
29895 @end table
29896
29897
29898 @node Compatibility between Ada 95 and Ada 2005
29899 @section Compatibility between Ada 95 and Ada 2005
29900 @cindex Compatibility between Ada 95 and Ada 2005
29901
29902 @noindent
29903 Although Ada 2005 was designed to be upwards compatible with Ada 95, there are
29904 a number of incompatibilities. Several are enumerated below;
29905 for a complete description please see the
29906 Annotated Ada 2005 Reference Manual, or section 9.1.1 in
29907 @cite{Rationale for Ada 2005}.
29908
29909 @table @emph
29910 @item New reserved words.
29911 The words @code{interface}, @code{overriding} and @code{synchronized} are
29912 reserved in Ada 2005.
29913 A pre-Ada 2005 program that uses any of these as an identifier will be
29914 illegal.
29915
29916 @item New declarations in predefined packages.
29917 A number of packages in the predefined environment contain new declarations:
29918 @code{Ada.Exceptions}, @code{Ada.Real_Time}, @code{Ada.Strings},
29919 @code{Ada.Strings.Fixed}, @code{Ada.Strings.Bounded},
29920 @code{Ada.Strings.Unbounded}, @code{Ada.Strings.Wide_Fixed},
29921 @code{Ada.Strings.Wide_Bounded}, @code{Ada.Strings.Wide_Unbounded},
29922 @code{Ada.Tags}, @code{Ada.Text_IO}, and @code{Interfaces.C}.
29923 If an Ada 95 program does a @code{with} and @code{use} of any of these
29924 packages, the new declarations may cause name clashes.
29925
29926 @item Access parameters.
29927 A nondispatching subprogram with an access parameter cannot be renamed
29928 as a dispatching operation. This was permitted in Ada 95.
29929
29930 @item Access types, discriminants, and constraints.
29931 Rule changes in this area have led to some incompatibilities; for example,
29932 constrained subtypes of some access types are not permitted in Ada 2005.
29933
29934 @item Aggregates for limited types.
29935 The allowance of aggregates for limited types in Ada 2005 raises the
29936 possibility of ambiguities in legal Ada 95 programs, since additional types
29937 now need to be considered in expression resolution.
29938
29939 @item Fixed-point multiplication and division.
29940 Certain expressions involving ``*'' or ``/'' for a fixed-point type, which
29941 were legal in Ada 95 and invoked the predefined versions of these operations,
29942 are now ambiguous.
29943 The ambiguity may be resolved either by applying a type conversion to the
29944 expression, or by explicitly invoking the operation from package
29945 @code{Standard}.
29946
29947 @item Return-by-reference types.
29948 The Ada 95 return-by-reference mechanism has been removed. Instead, the user
29949 can declare a function returning a value from an anonymous access type.
29950 @end table
29951
29952
29953 @node Implementation-dependent characteristics
29954 @section Implementation-dependent characteristics
29955 @noindent
29956 Although the Ada language defines the semantics of each construct as
29957 precisely as practical, in some situations (for example for reasons of
29958 efficiency, or where the effect is heavily dependent on the host or target
29959 platform) the implementation is allowed some freedom. In porting Ada 83
29960 code to GNAT, you need to be aware of whether / how the existing code
29961 exercised such implementation dependencies. Such characteristics fall into
29962 several categories, and GNAT offers specific support in assisting the
29963 transition from certain Ada 83 compilers.
29964
29965 @menu
29966 * Implementation-defined pragmas::
29967 * Implementation-defined attributes::
29968 * Libraries::
29969 * Elaboration order::
29970 * Target-specific aspects::
29971 @end menu
29972
29973 @node Implementation-defined pragmas
29974 @subsection Implementation-defined pragmas
29975
29976 @noindent
29977 Ada compilers are allowed to supplement the language-defined pragmas, and
29978 these are a potential source of non-portability. All GNAT-defined pragmas
29979 are described in @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT
29980 Reference Manual}, and these include several that are specifically
29981 intended to correspond to other vendors' Ada 83 pragmas.
29982 For migrating from VADS, the pragma @code{Use_VADS_Size} may be useful.
29983 For compatibility with HP Ada 83, GNAT supplies the pragmas
29984 @code{Extend_System}, @code{Ident}, @code{Inline_Generic},
29985 @code{Interface_Name}, @code{Passive}, @code{Suppress_All},
29986 and @code{Volatile}.
29987 Other relevant pragmas include @code{External} and @code{Link_With}.
29988 Some vendor-specific
29989 Ada 83 pragmas (@code{Share_Generic}, @code{Subtitle}, and @code{Title}) are
29990 recognized, thus
29991 avoiding compiler rejection of units that contain such pragmas; they are not
29992 relevant in a GNAT context and hence are not otherwise implemented.
29993
29994 @node Implementation-defined attributes
29995 @subsection Implementation-defined attributes
29996
29997 Analogous to pragmas, the set of attributes may be extended by an
29998 implementation. All GNAT-defined attributes are described in
29999 @ref{Implementation Defined Attributes,,, gnat_rm, GNAT Reference
30000 Manual}, and these include several that are specifically intended
30001 to correspond to other vendors' Ada 83 attributes. For migrating from VADS,
30002 the attribute @code{VADS_Size} may be useful. For compatibility with HP
30003 Ada 83, GNAT supplies the attributes @code{Bit}, @code{Machine_Size} and
30004 @code{Type_Class}.
30005
30006 @node Libraries
30007 @subsection Libraries
30008 @noindent
30009 Vendors may supply libraries to supplement the standard Ada API. If Ada 83
30010 code uses vendor-specific libraries then there are several ways to manage
30011 this in Ada 95 or Ada 2005:
30012 @enumerate
30013 @item
30014 If the source code for the libraries (specs and bodies) are
30015 available, then the libraries can be migrated in the same way as the
30016 application.
30017 @item
30018 If the source code for the specs but not the bodies are
30019 available, then you can reimplement the bodies.
30020 @item
30021 Some features introduced by Ada 95 obviate the need for library support. For
30022 example most Ada 83 vendors supplied a package for unsigned integers. The
30023 Ada 95 modular type feature is the preferred way to handle this need, so
30024 instead of migrating or reimplementing the unsigned integer package it may
30025 be preferable to retrofit the application using modular types.
30026 @end enumerate
30027
30028 @node Elaboration order
30029 @subsection Elaboration order
30030 @noindent
30031 The implementation can choose any elaboration order consistent with the unit
30032 dependency relationship. This freedom means that some orders can result in
30033 Program_Error being raised due to an ``Access Before Elaboration'': an attempt
30034 to invoke a subprogram its body has been elaborated, or to instantiate a
30035 generic before the generic body has been elaborated. By default GNAT
30036 attempts to choose a safe order (one that will not encounter access before
30037 elaboration problems) by implicitly inserting @code{Elaborate} or
30038 @code{Elaborate_All} pragmas where
30039 needed. However, this can lead to the creation of elaboration circularities
30040 and a resulting rejection of the program by gnatbind. This issue is
30041 thoroughly described in @ref{Elaboration Order Handling in GNAT}.
30042 In brief, there are several
30043 ways to deal with this situation:
30044
30045 @itemize @bullet
30046 @item
30047 Modify the program to eliminate the circularities, e.g.@: by moving
30048 elaboration-time code into explicitly-invoked procedures
30049 @item
30050 Constrain the elaboration order by including explicit @code{Elaborate_Body} or
30051 @code{Elaborate} pragmas, and then inhibit the generation of implicit
30052 @code{Elaborate_All}
30053 pragmas either globally (as an effect of the @option{-gnatE} switch) or locally
30054 (by selectively suppressing elaboration checks via pragma
30055 @code{Suppress(Elaboration_Check)} when it is safe to do so).
30056 @end itemize
30057
30058 @node Target-specific aspects
30059 @subsection Target-specific aspects
30060 @noindent
30061 Low-level applications need to deal with machine addresses, data
30062 representations, interfacing with assembler code, and similar issues. If
30063 such an Ada 83 application is being ported to different target hardware (for
30064 example where the byte endianness has changed) then you will need to
30065 carefully examine the program logic; the porting effort will heavily depend
30066 on the robustness of the original design. Moreover, Ada 95 (and thus
30067 Ada 2005) are sometimes
30068 incompatible with typical Ada 83 compiler practices regarding implicit
30069 packing, the meaning of the Size attribute, and the size of access values.
30070 GNAT's approach to these issues is described in @ref{Representation Clauses}.
30071
30072 @node Compatibility with Other Ada Systems
30073 @section Compatibility with Other Ada Systems
30074
30075 @noindent
30076 If programs avoid the use of implementation dependent and
30077 implementation defined features, as documented in the @cite{Ada
30078 Reference Manual}, there should be a high degree of portability between
30079 GNAT and other Ada systems. The following are specific items which
30080 have proved troublesome in moving Ada 95 programs from GNAT to other Ada 95
30081 compilers, but do not affect porting code to GNAT@.
30082 (As of @value{NOW}, GNAT is the only compiler available for Ada 2005;
30083 the following issues may or may not arise for Ada 2005 programs
30084 when other compilers appear.)
30085
30086 @table @emph
30087 @item Ada 83 Pragmas and Attributes
30088 Ada 95 compilers are allowed, but not required, to implement the missing
30089 Ada 83 pragmas and attributes that are no longer defined in Ada 95.
30090 GNAT implements all such pragmas and attributes, eliminating this as
30091 a compatibility concern, but some other Ada 95 compilers reject these
30092 pragmas and attributes.
30093
30094 @item Specialized Needs Annexes
30095 GNAT implements the full set of special needs annexes. At the
30096 current time, it is the only Ada 95 compiler to do so. This means that
30097 programs making use of these features may not be portable to other Ada
30098 95 compilation systems.
30099
30100 @item Representation Clauses
30101 Some other Ada 95 compilers implement only the minimal set of
30102 representation clauses required by the Ada 95 reference manual. GNAT goes
30103 far beyond this minimal set, as described in the next section.
30104 @end table
30105
30106 @node Representation Clauses
30107 @section Representation Clauses
30108
30109 @noindent
30110 The Ada 83 reference manual was quite vague in describing both the minimal
30111 required implementation of representation clauses, and also their precise
30112 effects. Ada 95 (and thus also Ada 2005) are much more explicit, but the
30113 minimal set of capabilities required is still quite limited.
30114
30115 GNAT implements the full required set of capabilities in
30116 Ada 95 and Ada 2005, but also goes much further, and in particular
30117 an effort has been made to be compatible with existing Ada 83 usage to the
30118 greatest extent possible.
30119
30120 A few cases exist in which Ada 83 compiler behavior is incompatible with
30121 the requirements in Ada 95 (and thus also Ada 2005). These are instances of
30122 intentional or accidental dependence on specific implementation dependent
30123 characteristics of these Ada 83 compilers. The following is a list of
30124 the cases most likely to arise in existing Ada 83 code.
30125
30126 @table @emph
30127 @item Implicit Packing
30128 Some Ada 83 compilers allowed a Size specification to cause implicit
30129 packing of an array or record. This could cause expensive implicit
30130 conversions for change of representation in the presence of derived
30131 types, and the Ada design intends to avoid this possibility.
30132 Subsequent AI's were issued to make it clear that such implicit
30133 change of representation in response to a Size clause is inadvisable,
30134 and this recommendation is represented explicitly in the Ada 95 (and Ada 2005)
30135 Reference Manuals as implementation advice that is followed by GNAT@.
30136 The problem will show up as an error
30137 message rejecting the size clause. The fix is simply to provide
30138 the explicit pragma @code{Pack}, or for more fine tuned control, provide
30139 a Component_Size clause.
30140
30141 @item Meaning of Size Attribute
30142 The Size attribute in Ada 95 (and Ada 2005) for discrete types is defined as
30143 the minimal number of bits required to hold values of the type. For example,
30144 on a 32-bit machine, the size of @code{Natural} will typically be 31 and not
30145 32 (since no sign bit is required). Some Ada 83 compilers gave 31, and
30146 some 32 in this situation. This problem will usually show up as a compile
30147 time error, but not always. It is a good idea to check all uses of the
30148 'Size attribute when porting Ada 83 code. The GNAT specific attribute
30149 Object_Size can provide a useful way of duplicating the behavior of
30150 some Ada 83 compiler systems.
30151
30152 @item Size of Access Types
30153 A common assumption in Ada 83 code is that an access type is in fact a pointer,
30154 and that therefore it will be the same size as a System.Address value. This
30155 assumption is true for GNAT in most cases with one exception. For the case of
30156 a pointer to an unconstrained array type (where the bounds may vary from one
30157 value of the access type to another), the default is to use a ``fat pointer'',
30158 which is represented as two separate pointers, one to the bounds, and one to
30159 the array. This representation has a number of advantages, including improved
30160 efficiency. However, it may cause some difficulties in porting existing Ada 83
30161 code which makes the assumption that, for example, pointers fit in 32 bits on
30162 a machine with 32-bit addressing.
30163
30164 To get around this problem, GNAT also permits the use of ``thin pointers'' for
30165 access types in this case (where the designated type is an unconstrained array
30166 type). These thin pointers are indeed the same size as a System.Address value.
30167 To specify a thin pointer, use a size clause for the type, for example:
30168
30169 @smallexample @c ada
30170 type X is access all String;
30171 for X'Size use Standard'Address_Size;
30172 @end smallexample
30173
30174 @noindent
30175 which will cause the type X to be represented using a single pointer.
30176 When using this representation, the bounds are right behind the array.
30177 This representation is slightly less efficient, and does not allow quite
30178 such flexibility in the use of foreign pointers or in using the
30179 Unrestricted_Access attribute to create pointers to non-aliased objects.
30180 But for any standard portable use of the access type it will work in
30181 a functionally correct manner and allow porting of existing code.
30182 Note that another way of forcing a thin pointer representation
30183 is to use a component size clause for the element size in an array,
30184 or a record representation clause for an access field in a record.
30185 @end table
30186
30187 @ifclear vms
30188 @c This brief section is only in the non-VMS version
30189 @c The complete chapter on HP Ada is in the VMS version
30190 @node Compatibility with HP Ada 83
30191 @section Compatibility with HP Ada 83
30192
30193 @noindent
30194 The VMS version of GNAT fully implements all the pragmas and attributes
30195 provided by HP Ada 83, as well as providing the standard HP Ada 83
30196 libraries, including Starlet. In addition, data layouts and parameter
30197 passing conventions are highly compatible. This means that porting
30198 existing HP Ada 83 code to GNAT in VMS systems should be easier than
30199 most other porting efforts. The following are some of the most
30200 significant differences between GNAT and HP Ada 83.
30201
30202 @table @emph
30203 @item Default floating-point representation
30204 In GNAT, the default floating-point format is IEEE, whereas in HP Ada 83,
30205 it is VMS format. GNAT does implement the necessary pragmas
30206 (Long_Float, Float_Representation) for changing this default.
30207
30208 @item System
30209 The package System in GNAT exactly corresponds to the definition in the
30210 Ada 95 reference manual, which means that it excludes many of the
30211 HP Ada 83 extensions. However, a separate package Aux_DEC is provided
30212 that contains the additional definitions, and a special pragma,
30213 Extend_System allows this package to be treated transparently as an
30214 extension of package System.
30215
30216 @item To_Address
30217 The definitions provided by Aux_DEC are exactly compatible with those
30218 in the HP Ada 83 version of System, with one exception.
30219 HP Ada provides the following declarations:
30220
30221 @smallexample @c ada
30222 TO_ADDRESS (INTEGER)
30223 TO_ADDRESS (UNSIGNED_LONGWORD)
30224 TO_ADDRESS (@i{universal_integer})
30225 @end smallexample
30226
30227 @noindent
30228 The version of TO_ADDRESS taking a @i{universal integer} argument is in fact
30229 an extension to Ada 83 not strictly compatible with the reference manual.
30230 In GNAT, we are constrained to be exactly compatible with the standard,
30231 and this means we cannot provide this capability. In HP Ada 83, the
30232 point of this definition is to deal with a call like:
30233
30234 @smallexample @c ada
30235 TO_ADDRESS (16#12777#);
30236 @end smallexample
30237
30238 @noindent
30239 Normally, according to the Ada 83 standard, one would expect this to be
30240 ambiguous, since it matches both the INTEGER and UNSIGNED_LONGWORD forms
30241 of TO_ADDRESS@. However, in HP Ada 83, there is no ambiguity, since the
30242 definition using @i{universal_integer} takes precedence.
30243
30244 In GNAT, since the version with @i{universal_integer} cannot be supplied, it
30245 is not possible to be 100% compatible. Since there are many programs using
30246 numeric constants for the argument to TO_ADDRESS, the decision in GNAT was
30247 to change the name of the function in the UNSIGNED_LONGWORD case, so the
30248 declarations provided in the GNAT version of AUX_Dec are:
30249
30250 @smallexample @c ada
30251 function To_Address (X : Integer) return Address;
30252 pragma Pure_Function (To_Address);
30253
30254 function To_Address_Long (X : Unsigned_Longword)
30255 return Address;
30256 pragma Pure_Function (To_Address_Long);
30257 @end smallexample
30258
30259 @noindent
30260 This means that programs using TO_ADDRESS for UNSIGNED_LONGWORD must
30261 change the name to TO_ADDRESS_LONG@.
30262
30263 @item Task_Id values
30264 The Task_Id values assigned will be different in the two systems, and GNAT
30265 does not provide a specified value for the Task_Id of the environment task,
30266 which in GNAT is treated like any other declared task.
30267 @end table
30268
30269 @noindent
30270 For full details on these and other less significant compatibility issues,
30271 see appendix E of the HP publication entitled @cite{HP Ada, Technical
30272 Overview and Comparison on HP Platforms}.
30273
30274 For GNAT running on other than VMS systems, all the HP Ada 83 pragmas and
30275 attributes are recognized, although only a subset of them can sensibly
30276 be implemented. The description of pragmas in @ref{Implementation
30277 Defined Pragmas,,, gnat_rm, GNAT Reference Manual}
30278 indicates whether or not they are applicable to non-VMS systems.
30279 @end ifclear
30280
30281 @ifset vms
30282 @node Transitioning to 64-Bit GNAT for OpenVMS
30283 @section Transitioning to 64-Bit @value{EDITION} for OpenVMS
30284
30285 @noindent
30286 This section is meant to assist users of pre-2006 @value{EDITION}
30287 for Alpha OpenVMS who are transitioning to 64-bit @value{EDITION},
30288 the version of the GNAT technology supplied in 2006 and later for
30289 OpenVMS on both Alpha and I64.
30290
30291 @menu
30292 * Introduction to transitioning::
30293 * Migration of 32 bit code::
30294 * Taking advantage of 64 bit addressing::
30295 * Technical details::
30296 @end menu
30297
30298 @node Introduction to transitioning
30299 @subsection Introduction
30300
30301 @noindent
30302 64-bit @value{EDITION} for Open VMS has been designed to meet
30303 three main goals:
30304
30305 @enumerate
30306 @item
30307 Providing a full conforming implementation of Ada 95 and Ada 2005
30308
30309 @item
30310 Allowing maximum backward compatibility, thus easing migration of existing
30311 Ada source code
30312
30313 @item
30314 Supplying a path for exploiting the full 64-bit address range
30315 @end enumerate
30316
30317 @noindent
30318 Ada's strong typing semantics has made it
30319 impractical to have different 32-bit and 64-bit modes. As soon as
30320 one object could possibly be outside the 32-bit address space, this
30321 would make it necessary for the @code{System.Address} type to be 64 bits.
30322 In particular, this would cause inconsistencies if 32-bit code is
30323 called from 64-bit code that raises an exception.
30324
30325 This issue has been resolved by always using 64-bit addressing
30326 at the system level, but allowing for automatic conversions between
30327 32-bit and 64-bit addresses where required. Thus users who
30328 do not currently require 64-bit addressing capabilities, can
30329 recompile their code with only minimal changes (and indeed
30330 if the code is written in portable Ada, with no assumptions about
30331 the size of the @code{Address} type, then no changes at all are necessary).
30332 At the same time,
30333 this approach provides a simple, gradual upgrade path to future
30334 use of larger memories than available for 32-bit systems.
30335 Also, newly written applications or libraries will by default
30336 be fully compatible with future systems exploiting 64-bit
30337 addressing capabilities.
30338
30339 @ref{Migration of 32 bit code}, will focus on porting applications
30340 that do not require more than 2 GB of
30341 addressable memory. This code will be referred to as
30342 @emph{32-bit code}.
30343 For applications intending to exploit the full 64-bit address space,
30344 @ref{Taking advantage of 64 bit addressing},
30345 will consider further changes that may be required.
30346 Such code will be referred to below as @emph{64-bit code}.
30347
30348 @node Migration of 32 bit code
30349 @subsection Migration of 32-bit code
30350
30351 @menu
30352 * Address types::
30353 * Access types::
30354 * Unchecked conversions::
30355 * Predefined constants::
30356 * Interfacing with C::
30357 * Experience with source compatibility::
30358 @end menu
30359
30360 @node Address types
30361 @subsubsection Address types
30362
30363 @noindent
30364 To solve the problem of mixing 64-bit and 32-bit addressing,
30365 while maintaining maximum backward compatibility, the following
30366 approach has been taken:
30367
30368 @itemize @bullet
30369 @item
30370 @code{System.Address} always has a size of 64 bits
30371
30372 @item
30373 @code{System.Short_Address} is a 32-bit subtype of @code{System.Address}
30374 @end itemize
30375
30376 @noindent
30377 Since @code{System.Short_Address} is a subtype of @code{System.Address},
30378 a @code{Short_Address}
30379 may be used where an @code{Address} is required, and vice versa, without
30380 needing explicit type conversions.
30381 By virtue of the Open VMS parameter passing conventions,
30382 even imported
30383 and exported subprograms that have 32-bit address parameters are
30384 compatible with those that have 64-bit address parameters.
30385 (See @ref{Making code 64 bit clean} for details.)
30386
30387 The areas that may need attention are those where record types have
30388 been defined that contain components of the type @code{System.Address}, and
30389 where objects of this type are passed to code expecting a record layout with
30390 32-bit addresses.
30391
30392 Different compilers on different platforms cannot be
30393 expected to represent the same type in the same way,
30394 since alignment constraints
30395 and other system-dependent properties affect the compiler's decision.
30396 For that reason, Ada code
30397 generally uses representation clauses to specify the expected
30398 layout where required.
30399
30400 If such a representation clause uses 32 bits for a component having
30401 the type @code{System.Address}, 64-bit @value{EDITION} for OpenVMS
30402 will detect that error and produce a specific diagnostic message.
30403 The developer should then determine whether the representation
30404 should be 64 bits or not and make either of two changes:
30405 change the size to 64 bits and leave the type as @code{System.Address}, or
30406 leave the size as 32 bits and change the type to @code{System.Short_Address}.
30407 Since @code{Short_Address} is a subtype of @code{Address}, no changes are
30408 required in any code setting or accessing the field; the compiler will
30409 automatically perform any needed conversions between address
30410 formats.
30411
30412 @node Access types
30413 @subsubsection Access types
30414
30415 @noindent
30416 By default, objects designated by access values are always
30417 allocated in the 32-bit
30418 address space. Thus legacy code will never contain
30419 any objects that are not addressable with 32-bit addresses, and
30420 the compiler will never raise exceptions as result of mixing
30421 32-bit and 64-bit addresses.
30422
30423 However, the access values themselves are represented in 64 bits, for optimum
30424 performance and future compatibility with 64-bit code. As was
30425 the case with @code{System.Address}, the compiler will give an error message
30426 if an object or record component has a representation clause that
30427 requires the access value to fit in 32 bits. In such a situation,
30428 an explicit size clause for the access type, specifying 32 bits,
30429 will have the desired effect.
30430
30431 General access types (declared with @code{access all}) can never be
30432 32 bits, as values of such types must be able to refer to any object
30433 of the designated type,
30434 including objects residing outside the 32-bit address range.
30435 Existing Ada 83 code will not contain such type definitions,
30436 however, since general access types were introduced in Ada 95.
30437
30438 @node Unchecked conversions
30439 @subsubsection Unchecked conversions
30440
30441 @noindent
30442 In the case of an @code{Unchecked_Conversion} where the source type is a
30443 64-bit access type or the type @code{System.Address}, and the target
30444 type is a 32-bit type, the compiler will generate a warning.
30445 Even though the generated code will still perform the required
30446 conversions, it is highly recommended in these cases to use
30447 respectively a 32-bit access type or @code{System.Short_Address}
30448 as the source type.
30449
30450 @node Predefined constants
30451 @subsubsection Predefined constants
30452
30453 @noindent
30454 The following table shows the correspondence between pre-2006 versions of
30455 @value{EDITION} on Alpha OpenVMS (``Old'') and 64-bit @value{EDITION}
30456 (``New''):
30457
30458 @multitable {@code{System.Short_Memory_Size}} {2**32} {2**64}
30459 @item @b{Constant} @tab @b{Old} @tab @b{New}
30460 @item @code{System.Word_Size} @tab 32 @tab 64
30461 @item @code{System.Memory_Size} @tab 2**32 @tab 2**64
30462 @item @code{System.Short_Memory_Size} @tab 2**32 @tab 2**32
30463 @item @code{System.Address_Size} @tab 32 @tab 64
30464 @end multitable
30465
30466 @noindent
30467 If you need to refer to the specific
30468 memory size of a 32-bit implementation, instead of the
30469 actual memory size, use @code{System.Short_Memory_Size}
30470 rather than @code{System.Memory_Size}.
30471 Similarly, references to @code{System.Address_Size} may need
30472 to be replaced by @code{System.Short_Address'Size}.
30473 The program @command{gnatfind} may be useful for locating
30474 references to the above constants, so that you can verify that they
30475 are still correct.
30476
30477 @node Interfacing with C
30478 @subsubsection Interfacing with C
30479
30480 @noindent
30481 In order to minimize the impact of the transition to 64-bit addresses on
30482 legacy programs, some fundamental types in the @code{Interfaces.C}
30483 package hierarchy continue to be represented in 32 bits.
30484 These types are: @code{ptrdiff_t}, @code{size_t}, and @code{chars_ptr}.
30485 This eases integration with the default HP C layout choices, for example
30486 as found in the system routines in @code{DECC$SHR.EXE}.
30487 Because of this implementation choice, the type fully compatible with
30488 @code{chars_ptr} is now @code{Short_Address} and not @code{Address}.
30489 Depending on the context the compiler will issue a
30490 warning or an error when type @code{Address} is used, alerting the user to a
30491 potential problem. Otherwise 32-bit programs that use
30492 @code{Interfaces.C} should normally not require code modifications
30493
30494 The other issue arising with C interfacing concerns pragma @code{Convention}.
30495 For VMS 64-bit systems, there is an issue of the appropriate default size
30496 of C convention pointers in the absence of an explicit size clause. The HP
30497 C compiler can choose either 32 or 64 bits depending on compiler options.
30498 GNAT chooses 32-bits rather than 64-bits in the default case where no size
30499 clause is given. This proves a better choice for porting 32-bit legacy
30500 applications. In order to have a 64-bit representation, it is necessary to
30501 specify a size representation clause. For example:
30502
30503 @smallexample @c ada
30504 type int_star is access Interfaces.C.int;
30505 pragma Convention(C, int_star);
30506 for int_star'Size use 64; -- Necessary to get 64 and not 32 bits
30507 @end smallexample
30508
30509 @node Experience with source compatibility
30510 @subsubsection Experience with source compatibility
30511
30512 @noindent
30513 The Security Server and STARLET on I64 provide an interesting ``test case''
30514 for source compatibility issues, since it is in such system code
30515 where assumptions about @code{Address} size might be expected to occur.
30516 Indeed, there were a small number of occasions in the Security Server
30517 file @file{jibdef.ads}
30518 where a representation clause for a record type specified
30519 32 bits for a component of type @code{Address}.
30520 All of these errors were detected by the compiler.
30521 The repair was obvious and immediate; to simply replace @code{Address} by
30522 @code{Short_Address}.
30523
30524 In the case of STARLET, there were several record types that should
30525 have had representation clauses but did not. In these record types
30526 there was an implicit assumption that an @code{Address} value occupied
30527 32 bits.
30528 These compiled without error, but their usage resulted in run-time error
30529 returns from STARLET system calls.
30530 Future GNAT technology enhancements may include a tool that detects and flags
30531 these sorts of potential source code porting problems.
30532
30533 @c ****************************************
30534 @node Taking advantage of 64 bit addressing
30535 @subsection Taking advantage of 64-bit addressing
30536
30537 @menu
30538 * Making code 64 bit clean::
30539 * Allocating memory from the 64 bit storage pool::
30540 * Restrictions on use of 64 bit objects::
30541 * Using 64 bit storage pools by default::
30542 * General access types::
30543 * STARLET and other predefined libraries::
30544 @end menu
30545
30546 @node Making code 64 bit clean
30547 @subsubsection Making code 64-bit clean
30548
30549 @noindent
30550 In order to prevent problems that may occur when (parts of) a
30551 system start using memory outside the 32-bit address range,
30552 we recommend some additional guidelines:
30553
30554 @itemize @bullet
30555 @item
30556 For imported subprograms that take parameters of the
30557 type @code{System.Address}, ensure that these subprograms can
30558 indeed handle 64-bit addresses. If not, or when in doubt,
30559 change the subprogram declaration to specify
30560 @code{System.Short_Address} instead.
30561
30562 @item
30563 Resolve all warnings related to size mismatches in
30564 unchecked conversions. Failing to do so causes
30565 erroneous execution if the source object is outside
30566 the 32-bit address space.
30567
30568 @item
30569 (optional) Explicitly use the 32-bit storage pool
30570 for access types used in a 32-bit context, or use
30571 generic access types where possible
30572 (@pxref{Restrictions on use of 64 bit objects}).
30573 @end itemize
30574
30575 @noindent
30576 If these rules are followed, the compiler will automatically insert
30577 any necessary checks to ensure that no addresses or access values
30578 passed to 32-bit code ever refer to objects outside the 32-bit
30579 address range.
30580 Any attempt to do this will raise @code{Constraint_Error}.
30581
30582 @node Allocating memory from the 64 bit storage pool
30583 @subsubsection Allocating memory from the 64-bit storage pool
30584
30585 @noindent
30586 For any access type @code{T} that potentially requires memory allocations
30587 beyond the 32-bit address space,
30588 use the following representation clause:
30589
30590 @smallexample @c ada
30591 for T'Storage_Pool use System.Pool_64;
30592 @end smallexample
30593
30594 @node Restrictions on use of 64 bit objects
30595 @subsubsection Restrictions on use of 64-bit objects
30596
30597 @noindent
30598 Taking the address of an object allocated from a 64-bit storage pool,
30599 and then passing this address to a subprogram expecting
30600 @code{System.Short_Address},
30601 or assigning it to a variable of type @code{Short_Address}, will cause
30602 @code{Constraint_Error} to be raised. In case the code is not 64-bit clean
30603 (@pxref{Making code 64 bit clean}), or checks are suppressed,
30604 no exception is raised and execution
30605 will become erroneous.
30606
30607 @node Using 64 bit storage pools by default
30608 @subsubsection Using 64-bit storage pools by default
30609
30610 @noindent
30611 In some cases it may be desirable to have the compiler allocate
30612 from 64-bit storage pools by default. This may be the case for
30613 libraries that are 64-bit clean, but may be used in both 32-bit
30614 and 64-bit contexts. For these cases the following configuration
30615 pragma may be specified:
30616
30617 @smallexample @c ada
30618 pragma Pool_64_Default;
30619 @end smallexample
30620
30621 @noindent
30622 Any code compiled in the context of this pragma will by default
30623 use the @code{System.Pool_64} storage pool. This default may be overridden
30624 for a specific access type @code{T} by the representation clause:
30625
30626 @smallexample @c ada
30627 for T'Storage_Pool use System.Pool_32;
30628 @end smallexample
30629
30630 @noindent
30631 Any object whose address may be passed to a subprogram with a
30632 @code{Short_Address} argument, or assigned to a variable of type
30633 @code{Short_Address}, needs to be allocated from this pool.
30634
30635 @node General access types
30636 @subsubsection General access types
30637
30638 @noindent
30639 Objects designated by access values from a
30640 general access type (declared with @code{access all}) are never allocated
30641 from a 64-bit storage pool. Code that uses general access types will
30642 accept objects allocated in either 32-bit or 64-bit address spaces,
30643 but never allocate objects outside the 32-bit address space.
30644 Using general access types ensures maximum compatibility with both
30645 32-bit and 64-bit code.
30646
30647 @node STARLET and other predefined libraries
30648 @subsubsection STARLET and other predefined libraries
30649
30650 @noindent
30651 All code that comes as part of GNAT is 64-bit clean, but the
30652 restrictions given in @ref{Restrictions on use of 64 bit objects},
30653 still apply. Look at the package
30654 specs to see in which contexts objects allocated
30655 in 64-bit address space are acceptable.
30656
30657 @node Technical details
30658 @subsection Technical details
30659
30660 @noindent
30661 64-bit @value{EDITION} for Open VMS takes advantage of the freedom given in the
30662 Ada standard with respect to the type of @code{System.Address}. Previous
30663 versions of GNAT Pro have defined this type as private and implemented it as a
30664 modular type.
30665
30666 In order to allow defining @code{System.Short_Address} as a proper subtype,
30667 and to match the implicit sign extension in parameter passing,
30668 in 64-bit @value{EDITION} for Open VMS, @code{System.Address} is defined as a
30669 visible (i.e., non-private) integer type.
30670 Standard operations on the type, such as the binary operators ``+'', ``-'',
30671 etc., that take @code{Address} operands and return an @code{Address} result,
30672 have been hidden by declaring these
30673 @code{abstract}, a feature introduced in Ada 95 that helps avoid the potential
30674 ambiguities that would otherwise result from overloading.
30675 (Note that, although @code{Address} is a visible integer type,
30676 good programming practice dictates against exploiting the type's
30677 integer properties such as literals, since this will compromise
30678 code portability.)
30679
30680 Defining @code{Address} as a visible integer type helps achieve
30681 maximum compatibility for existing Ada code,
30682 without sacrificing the capabilities of the 64-bit architecture.
30683 @end ifset
30684
30685 @c ************************************************
30686 @ifset unw
30687 @node Microsoft Windows Topics
30688 @appendix Microsoft Windows Topics
30689 @cindex Windows NT
30690 @cindex Windows 95
30691 @cindex Windows 98
30692
30693 @noindent
30694 This chapter describes topics that are specific to the Microsoft Windows
30695 platforms (NT, 2000, and XP Professional).
30696
30697 @menu
30698 * Using GNAT on Windows::
30699 * Using a network installation of GNAT::
30700 * CONSOLE and WINDOWS subsystems::
30701 * Temporary Files::
30702 * Mixed-Language Programming on Windows::
30703 * Windows Calling Conventions::
30704 * Introduction to Dynamic Link Libraries (DLLs)::
30705 * Using DLLs with GNAT::
30706 * Building DLLs with GNAT::
30707 * Building DLLs with GNAT Project files::
30708 * Building DLLs with gnatdll::
30709 * GNAT and Windows Resources::
30710 * Debugging a DLL::
30711 * Setting Stack Size from gnatlink::
30712 * Setting Heap Size from gnatlink::
30713 @end menu
30714
30715 @node Using GNAT on Windows
30716 @section Using GNAT on Windows
30717
30718 @noindent
30719 One of the strengths of the GNAT technology is that its tool set
30720 (@command{gcc}, @command{gnatbind}, @command{gnatlink}, @command{gnatmake}, the
30721 @code{gdb} debugger, etc.) is used in the same way regardless of the
30722 platform.
30723
30724 On Windows this tool set is complemented by a number of Microsoft-specific
30725 tools that have been provided to facilitate interoperability with Windows
30726 when this is required. With these tools:
30727
30728 @itemize @bullet
30729
30730 @item
30731 You can build applications using the @code{CONSOLE} or @code{WINDOWS}
30732 subsystems.
30733
30734 @item
30735 You can use any Dynamically Linked Library (DLL) in your Ada code (both
30736 relocatable and non-relocatable DLLs are supported).
30737
30738 @item
30739 You can build Ada DLLs for use in other applications. These applications
30740 can be written in a language other than Ada (e.g., C, C++, etc). Again both
30741 relocatable and non-relocatable Ada DLLs are supported.
30742
30743 @item
30744 You can include Windows resources in your Ada application.
30745
30746 @item
30747 You can use or create COM/DCOM objects.
30748 @end itemize
30749
30750 @noindent
30751 Immediately below are listed all known general GNAT-for-Windows restrictions.
30752 Other restrictions about specific features like Windows Resources and DLLs
30753 are listed in separate sections below.
30754
30755 @itemize @bullet
30756
30757 @item
30758 It is not possible to use @code{GetLastError} and @code{SetLastError}
30759 when tasking, protected records, or exceptions are used. In these
30760 cases, in order to implement Ada semantics, the GNAT run-time system
30761 calls certain Win32 routines that set the last error variable to 0 upon
30762 success. It should be possible to use @code{GetLastError} and
30763 @code{SetLastError} when tasking, protected record, and exception
30764 features are not used, but it is not guaranteed to work.
30765
30766 @item
30767 It is not possible to link against Microsoft libraries except for
30768 import libraries. The library must be built to be compatible with
30769 @file{MSVCRT.LIB} (/MD Microsoft compiler option), @file{LIBC.LIB} and
30770 @file{LIBCMT.LIB} (/ML or /MT Microsoft compiler options) are known to
30771 not be compatible with the GNAT runtime. Even if the library is
30772 compatible with @file{MSVCRT.LIB} it is not guaranteed to work.
30773
30774 @item
30775 When the compilation environment is located on FAT32 drives, users may
30776 experience recompilations of the source files that have not changed if
30777 Daylight Saving Time (DST) state has changed since the last time files
30778 were compiled. NTFS drives do not have this problem.
30779
30780 @item
30781 No components of the GNAT toolset use any entries in the Windows
30782 registry. The only entries that can be created are file associations and
30783 PATH settings, provided the user has chosen to create them at installation
30784 time, as well as some minimal book-keeping information needed to correctly
30785 uninstall or integrate different GNAT products.
30786 @end itemize
30787
30788 @node Using a network installation of GNAT
30789 @section Using a network installation of GNAT
30790
30791 @noindent
30792 Make sure the system on which GNAT is installed is accessible from the
30793 current machine, i.e., the install location is shared over the network.
30794 Shared resources are accessed on Windows by means of UNC paths, which
30795 have the format @code{\\server\sharename\path}
30796
30797 In order to use such a network installation, simply add the UNC path of the
30798 @file{bin} directory of your GNAT installation in front of your PATH. For
30799 example, if GNAT is installed in @file{\GNAT} directory of a share location
30800 called @file{c-drive} on a machine @file{LOKI}, the following command will
30801 make it available:
30802
30803 @code{@ @ @ path \\loki\c-drive\gnat\bin;%path%}
30804
30805 Be aware that every compilation using the network installation results in the
30806 transfer of large amounts of data across the network and will likely cause
30807 serious performance penalty.
30808
30809 @node CONSOLE and WINDOWS subsystems
30810 @section CONSOLE and WINDOWS subsystems
30811 @cindex CONSOLE Subsystem
30812 @cindex WINDOWS Subsystem
30813 @cindex -mwindows
30814
30815 @noindent
30816 There are two main subsystems under Windows. The @code{CONSOLE} subsystem
30817 (which is the default subsystem) will always create a console when
30818 launching the application. This is not something desirable when the
30819 application has a Windows GUI. To get rid of this console the
30820 application must be using the @code{WINDOWS} subsystem. To do so
30821 the @option{-mwindows} linker option must be specified.
30822
30823 @smallexample
30824 $ gnatmake winprog -largs -mwindows
30825 @end smallexample
30826
30827 @node Temporary Files
30828 @section Temporary Files
30829 @cindex Temporary files
30830
30831 @noindent
30832 It is possible to control where temporary files gets created by setting
30833 the @env{TMP} environment variable. The file will be created:
30834
30835 @itemize
30836 @item Under the directory pointed to by the @env{TMP} environment variable if
30837 this directory exists.
30838
30839 @item Under @file{c:\temp}, if the @env{TMP} environment variable is not
30840 set (or not pointing to a directory) and if this directory exists.
30841
30842 @item Under the current working directory otherwise.
30843 @end itemize
30844
30845 @noindent
30846 This allows you to determine exactly where the temporary
30847 file will be created. This is particularly useful in networked
30848 environments where you may not have write access to some
30849 directories.
30850
30851 @node Mixed-Language Programming on Windows
30852 @section Mixed-Language Programming on Windows
30853
30854 @noindent
30855 Developing pure Ada applications on Windows is no different than on
30856 other GNAT-supported platforms. However, when developing or porting an
30857 application that contains a mix of Ada and C/C++, the choice of your
30858 Windows C/C++ development environment conditions your overall
30859 interoperability strategy.
30860
30861 If you use @command{gcc} to compile the non-Ada part of your application,
30862 there are no Windows-specific restrictions that affect the overall
30863 interoperability with your Ada code. If you plan to use
30864 Microsoft tools (e.g.@: Microsoft Visual C/C++), you should be aware of
30865 the following limitations:
30866
30867 @itemize @bullet
30868 @item
30869 You cannot link your Ada code with an object or library generated with
30870 Microsoft tools if these use the @code{.tls} section (Thread Local
30871 Storage section) since the GNAT linker does not yet support this section.
30872
30873 @item
30874 You cannot link your Ada code with an object or library generated with
30875 Microsoft tools if these use I/O routines other than those provided in
30876 the Microsoft DLL: @code{msvcrt.dll}. This is because the GNAT run time
30877 uses the services of @code{msvcrt.dll} for its I/Os. Use of other I/O
30878 libraries can cause a conflict with @code{msvcrt.dll} services. For
30879 instance Visual C++ I/O stream routines conflict with those in
30880 @code{msvcrt.dll}.
30881 @end itemize
30882
30883 @noindent
30884 If you do want to use the Microsoft tools for your non-Ada code and hit one
30885 of the above limitations, you have two choices:
30886
30887 @enumerate
30888 @item
30889 Encapsulate your non-Ada code in a DLL to be linked with your Ada
30890 application. In this case, use the Microsoft or whatever environment to
30891 build the DLL and use GNAT to build your executable
30892 (@pxref{Using DLLs with GNAT}).
30893
30894 @item
30895 Or you can encapsulate your Ada code in a DLL to be linked with the
30896 other part of your application. In this case, use GNAT to build the DLL
30897 (@pxref{Building DLLs with GNAT}) and use the Microsoft or whatever
30898 environment to build your executable.
30899 @end enumerate
30900
30901 @node Windows Calling Conventions
30902 @section Windows Calling Conventions
30903 @findex Stdcall
30904 @findex APIENTRY
30905
30906 @menu
30907 * C Calling Convention::
30908 * Stdcall Calling Convention::
30909 * Win32 Calling Convention::
30910 * DLL Calling Convention::
30911 @end menu
30912
30913 @noindent
30914 When a subprogram @code{F} (caller) calls a subprogram @code{G}
30915 (callee), there are several ways to push @code{G}'s parameters on the
30916 stack and there are several possible scenarios to clean up the stack
30917 upon @code{G}'s return. A calling convention is an agreed upon software
30918 protocol whereby the responsibilities between the caller (@code{F}) and
30919 the callee (@code{G}) are clearly defined. Several calling conventions
30920 are available for Windows:
30921
30922 @itemize @bullet
30923 @item
30924 @code{C} (Microsoft defined)
30925
30926 @item
30927 @code{Stdcall} (Microsoft defined)
30928
30929 @item
30930 @code{Win32} (GNAT specific)
30931
30932 @item
30933 @code{DLL} (GNAT specific)
30934 @end itemize
30935
30936 @node C Calling Convention
30937 @subsection @code{C} Calling Convention
30938
30939 @noindent
30940 This is the default calling convention used when interfacing to C/C++
30941 routines compiled with either @command{gcc} or Microsoft Visual C++.
30942
30943 In the @code{C} calling convention subprogram parameters are pushed on the
30944 stack by the caller from right to left. The caller itself is in charge of
30945 cleaning up the stack after the call. In addition, the name of a routine
30946 with @code{C} calling convention is mangled by adding a leading underscore.
30947
30948 The name to use on the Ada side when importing (or exporting) a routine
30949 with @code{C} calling convention is the name of the routine. For
30950 instance the C function:
30951
30952 @smallexample
30953 int get_val (long);
30954 @end smallexample
30955
30956 @noindent
30957 should be imported from Ada as follows:
30958
30959 @smallexample @c ada
30960 @group
30961 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
30962 pragma Import (C, Get_Val, External_Name => "get_val");
30963 @end group
30964 @end smallexample
30965
30966 @noindent
30967 Note that in this particular case the @code{External_Name} parameter could
30968 have been omitted since, when missing, this parameter is taken to be the
30969 name of the Ada entity in lower case. When the @code{Link_Name} parameter
30970 is missing, as in the above example, this parameter is set to be the
30971 @code{External_Name} with a leading underscore.
30972
30973 When importing a variable defined in C, you should always use the @code{C}
30974 calling convention unless the object containing the variable is part of a
30975 DLL (in which case you should use the @code{Stdcall} calling
30976 convention, @pxref{Stdcall Calling Convention}).
30977
30978 @node Stdcall Calling Convention
30979 @subsection @code{Stdcall} Calling Convention
30980
30981 @noindent
30982 This convention, which was the calling convention used for Pascal
30983 programs, is used by Microsoft for all the routines in the Win32 API for
30984 efficiency reasons. It must be used to import any routine for which this
30985 convention was specified.
30986
30987 In the @code{Stdcall} calling convention subprogram parameters are pushed
30988 on the stack by the caller from right to left. The callee (and not the
30989 caller) is in charge of cleaning the stack on routine exit. In addition,
30990 the name of a routine with @code{Stdcall} calling convention is mangled by
30991 adding a leading underscore (as for the @code{C} calling convention) and a
30992 trailing @code{@@}@code{@var{nn}}, where @var{nn} is the overall size (in
30993 bytes) of the parameters passed to the routine.
30994
30995 The name to use on the Ada side when importing a C routine with a
30996 @code{Stdcall} calling convention is the name of the C routine. The leading
30997 underscore and trailing @code{@@}@code{@var{nn}} are added automatically by
30998 the compiler. For instance the Win32 function:
30999
31000 @smallexample
31001 @b{APIENTRY} int get_val (long);
31002 @end smallexample
31003
31004 @noindent
31005 should be imported from Ada as follows:
31006
31007 @smallexample @c ada
31008 @group
31009 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
31010 pragma Import (Stdcall, Get_Val);
31011 -- On the x86 a long is 4 bytes, so the Link_Name is "_get_val@@4"
31012 @end group
31013 @end smallexample
31014
31015 @noindent
31016 As for the @code{C} calling convention, when the @code{External_Name}
31017 parameter is missing, it is taken to be the name of the Ada entity in lower
31018 case. If instead of writing the above import pragma you write:
31019
31020 @smallexample @c ada
31021 @group
31022 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
31023 pragma Import (Stdcall, Get_Val, External_Name => "retrieve_val");
31024 @end group
31025 @end smallexample
31026
31027 @noindent
31028 then the imported routine is @code{_retrieve_val@@4}. However, if instead
31029 of specifying the @code{External_Name} parameter you specify the
31030 @code{Link_Name} as in the following example:
31031
31032 @smallexample @c ada
31033 @group
31034 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
31035 pragma Import (Stdcall, Get_Val, Link_Name => "retrieve_val");
31036 @end group
31037 @end smallexample
31038
31039 @noindent
31040 then the imported routine is @code{retrieve_val}, that is, there is no
31041 decoration at all. No leading underscore and no Stdcall suffix
31042 @code{@@}@code{@var{nn}}.
31043
31044 @noindent
31045 This is especially important as in some special cases a DLL's entry
31046 point name lacks a trailing @code{@@}@code{@var{nn}} while the exported
31047 name generated for a call has it.
31048
31049 @noindent
31050 It is also possible to import variables defined in a DLL by using an
31051 import pragma for a variable. As an example, if a DLL contains a
31052 variable defined as:
31053
31054 @smallexample
31055 int my_var;
31056 @end smallexample
31057
31058 @noindent
31059 then, to access this variable from Ada you should write:
31060
31061 @smallexample @c ada
31062 @group
31063 My_Var : Interfaces.C.int;
31064 pragma Import (Stdcall, My_Var);
31065 @end group
31066 @end smallexample
31067
31068 @noindent
31069 Note that to ease building cross-platform bindings this convention
31070 will be handled as a @code{C} calling convention on non-Windows platforms.
31071
31072 @node Win32 Calling Convention
31073 @subsection @code{Win32} Calling Convention
31074
31075 @noindent
31076 This convention, which is GNAT-specific is fully equivalent to the
31077 @code{Stdcall} calling convention described above.
31078
31079 @node DLL Calling Convention
31080 @subsection @code{DLL} Calling Convention
31081
31082 @noindent
31083 This convention, which is GNAT-specific is fully equivalent to the
31084 @code{Stdcall} calling convention described above.
31085
31086 @node Introduction to Dynamic Link Libraries (DLLs)
31087 @section Introduction to Dynamic Link Libraries (DLLs)
31088 @findex DLL
31089
31090 @noindent
31091 A Dynamically Linked Library (DLL) is a library that can be shared by
31092 several applications running under Windows. A DLL can contain any number of
31093 routines and variables.
31094
31095 One advantage of DLLs is that you can change and enhance them without
31096 forcing all the applications that depend on them to be relinked or
31097 recompiled. However, you should be aware than all calls to DLL routines are
31098 slower since, as you will understand below, such calls are indirect.
31099
31100 To illustrate the remainder of this section, suppose that an application
31101 wants to use the services of a DLL @file{API.dll}. To use the services
31102 provided by @file{API.dll} you must statically link against the DLL or
31103 an import library which contains a jump table with an entry for each
31104 routine and variable exported by the DLL. In the Microsoft world this
31105 import library is called @file{API.lib}. When using GNAT this import
31106 library is called either @file{libAPI.dll.a}, @file{libapi.dll.a},
31107 @file{libAPI.a} or @file{libapi.a} (names are case insensitive).
31108
31109 After you have linked your application with the DLL or the import library
31110 and you run your application, here is what happens:
31111
31112 @enumerate
31113 @item
31114 Your application is loaded into memory.
31115
31116 @item
31117 The DLL @file{API.dll} is mapped into the address space of your
31118 application. This means that:
31119
31120 @itemize @bullet
31121 @item
31122 The DLL will use the stack of the calling thread.
31123
31124 @item
31125 The DLL will use the virtual address space of the calling process.
31126
31127 @item
31128 The DLL will allocate memory from the virtual address space of the calling
31129 process.
31130
31131 @item
31132 Handles (pointers) can be safely exchanged between routines in the DLL
31133 routines and routines in the application using the DLL.
31134 @end itemize
31135
31136 @item
31137 The entries in the jump table (from the import library @file{libAPI.dll.a}
31138 or @file{API.lib} or automatically created when linking against a DLL)
31139 which is part of your application are initialized with the addresses
31140 of the routines and variables in @file{API.dll}.
31141
31142 @item
31143 If present in @file{API.dll}, routines @code{DllMain} or
31144 @code{DllMainCRTStartup} are invoked. These routines typically contain
31145 the initialization code needed for the well-being of the routines and
31146 variables exported by the DLL.
31147 @end enumerate
31148
31149 @noindent
31150 There is an additional point which is worth mentioning. In the Windows
31151 world there are two kind of DLLs: relocatable and non-relocatable
31152 DLLs. Non-relocatable DLLs can only be loaded at a very specific address
31153 in the target application address space. If the addresses of two
31154 non-relocatable DLLs overlap and these happen to be used by the same
31155 application, a conflict will occur and the application will run
31156 incorrectly. Hence, when possible, it is always preferable to use and
31157 build relocatable DLLs. Both relocatable and non-relocatable DLLs are
31158 supported by GNAT. Note that the @option{-s} linker option (see GNU Linker
31159 User's Guide) removes the debugging symbols from the DLL but the DLL can
31160 still be relocated.
31161
31162 As a side note, an interesting difference between Microsoft DLLs and
31163 Unix shared libraries, is the fact that on most Unix systems all public
31164 routines are exported by default in a Unix shared library, while under
31165 Windows it is possible (but not required) to list exported routines in
31166 a definition file (@pxref{The Definition File}).
31167
31168 @node Using DLLs with GNAT
31169 @section Using DLLs with GNAT
31170
31171 @menu
31172 * Creating an Ada Spec for the DLL Services::
31173 * Creating an Import Library::
31174 @end menu
31175
31176 @noindent
31177 To use the services of a DLL, say @file{API.dll}, in your Ada application
31178 you must have:
31179
31180 @enumerate
31181 @item
31182 The Ada spec for the routines and/or variables you want to access in
31183 @file{API.dll}. If not available this Ada spec must be built from the C/C++
31184 header files provided with the DLL.
31185
31186 @item
31187 The import library (@file{libAPI.dll.a} or @file{API.lib}). As previously
31188 mentioned an import library is a statically linked library containing the
31189 import table which will be filled at load time to point to the actual
31190 @file{API.dll} routines. Sometimes you don't have an import library for the
31191 DLL you want to use. The following sections will explain how to build
31192 one. Note that this is optional.
31193
31194 @item
31195 The actual DLL, @file{API.dll}.
31196 @end enumerate
31197
31198 @noindent
31199 Once you have all the above, to compile an Ada application that uses the
31200 services of @file{API.dll} and whose main subprogram is @code{My_Ada_App},
31201 you simply issue the command
31202
31203 @smallexample
31204 $ gnatmake my_ada_app -largs -lAPI
31205 @end smallexample
31206
31207 @noindent
31208 The argument @option{-largs -lAPI} at the end of the @command{gnatmake} command
31209 tells the GNAT linker to look first for a library named @file{API.lib}
31210 (Microsoft-style name) and if not found for a libraries named
31211 @file{libAPI.dll.a}, @file{API.dll.a} or @file{libAPI.a}.
31212 (GNAT-style name). Note that if the Ada package spec for @file{API.dll}
31213 contains the following pragma
31214
31215 @smallexample @c ada
31216 pragma Linker_Options ("-lAPI");
31217 @end smallexample
31218
31219 @noindent
31220 you do not have to add @option{-largs -lAPI} at the end of the
31221 @command{gnatmake} command.
31222
31223 If any one of the items above is missing you will have to create it
31224 yourself. The following sections explain how to do so using as an
31225 example a fictitious DLL called @file{API.dll}.
31226
31227 @node Creating an Ada Spec for the DLL Services
31228 @subsection Creating an Ada Spec for the DLL Services
31229
31230 @noindent
31231 A DLL typically comes with a C/C++ header file which provides the
31232 definitions of the routines and variables exported by the DLL. The Ada
31233 equivalent of this header file is a package spec that contains definitions
31234 for the imported entities. If the DLL you intend to use does not come with
31235 an Ada spec you have to generate one such spec yourself. For example if
31236 the header file of @file{API.dll} is a file @file{api.h} containing the
31237 following two definitions:
31238
31239 @smallexample
31240 @group
31241 @cartouche
31242 int some_var;
31243 int get (char *);
31244 @end cartouche
31245 @end group
31246 @end smallexample
31247
31248 @noindent
31249 then the equivalent Ada spec could be:
31250
31251 @smallexample @c ada
31252 @group
31253 @cartouche
31254 with Interfaces.C.Strings;
31255 package API is
31256 use Interfaces;
31257
31258 Some_Var : C.int;
31259 function Get (Str : C.Strings.Chars_Ptr) return C.int;
31260
31261 private
31262 pragma Import (C, Get);
31263 pragma Import (DLL, Some_Var);
31264 end API;
31265 @end cartouche
31266 @end group
31267 @end smallexample
31268
31269 @noindent
31270 Note that a variable is
31271 @strong{always imported with a Stdcall convention}. A function
31272 can have @code{C} or @code{Stdcall} convention.
31273 (@pxref{Windows Calling Conventions}).
31274
31275 @node Creating an Import Library
31276 @subsection Creating an Import Library
31277 @cindex Import library
31278
31279 @menu
31280 * The Definition File::
31281 * GNAT-Style Import Library::
31282 * Microsoft-Style Import Library::
31283 @end menu
31284
31285 @noindent
31286 If a Microsoft-style import library @file{API.lib} or a GNAT-style
31287 import library @file{libAPI.dll.a} or @file{libAPI.a} is available
31288 with @file{API.dll} you can skip this section. You can also skip this
31289 section if @file{API.dll} or @file{libAPI.dll} is built with GNU tools
31290 as in this case it is possible to link directly against the
31291 DLL. Otherwise read on.
31292
31293 @node The Definition File
31294 @subsubsection The Definition File
31295 @cindex Definition file
31296 @findex .def
31297
31298 @noindent
31299 As previously mentioned, and unlike Unix systems, the list of symbols
31300 that are exported from a DLL must be provided explicitly in Windows.
31301 The main goal of a definition file is precisely that: list the symbols
31302 exported by a DLL. A definition file (usually a file with a @code{.def}
31303 suffix) has the following structure:
31304
31305 @smallexample
31306 @group
31307 @cartouche
31308 @r{[}LIBRARY @var{name}@r{]}
31309 @r{[}DESCRIPTION @var{string}@r{]}
31310 EXPORTS
31311 @var{symbol1}
31312 @var{symbol2}
31313 @dots{}
31314 @end cartouche
31315 @end group
31316 @end smallexample
31317
31318 @table @code
31319 @item LIBRARY @var{name}
31320 This section, which is optional, gives the name of the DLL.
31321
31322 @item DESCRIPTION @var{string}
31323 This section, which is optional, gives a description string that will be
31324 embedded in the import library.
31325
31326 @item EXPORTS
31327 This section gives the list of exported symbols (procedures, functions or
31328 variables). For instance in the case of @file{API.dll} the @code{EXPORTS}
31329 section of @file{API.def} looks like:
31330
31331 @smallexample
31332 @group
31333 @cartouche
31334 EXPORTS
31335 some_var
31336 get
31337 @end cartouche
31338 @end group
31339 @end smallexample
31340 @end table
31341
31342 @noindent
31343 Note that you must specify the correct suffix (@code{@@}@code{@var{nn}})
31344 (@pxref{Windows Calling Conventions}) for a Stdcall
31345 calling convention function in the exported symbols list.
31346
31347 @noindent
31348 There can actually be other sections in a definition file, but these
31349 sections are not relevant to the discussion at hand.
31350
31351 @node GNAT-Style Import Library
31352 @subsubsection GNAT-Style Import Library
31353
31354 @noindent
31355 To create a static import library from @file{API.dll} with the GNAT tools
31356 you should proceed as follows:
31357
31358 @enumerate
31359 @item
31360 Create the definition file @file{API.def} (@pxref{The Definition File}).
31361 For that use the @code{dll2def} tool as follows:
31362
31363 @smallexample
31364 $ dll2def API.dll > API.def
31365 @end smallexample
31366
31367 @noindent
31368 @code{dll2def} is a very simple tool: it takes as input a DLL and prints
31369 to standard output the list of entry points in the DLL. Note that if
31370 some routines in the DLL have the @code{Stdcall} convention
31371 (@pxref{Windows Calling Conventions}) with stripped @code{@@}@var{nn}
31372 suffix then you'll have to edit @file{api.def} to add it, and specify
31373 @option{-k} to @command{gnatdll} when creating the import library.
31374
31375 @noindent
31376 Here are some hints to find the right @code{@@}@var{nn} suffix.
31377
31378 @enumerate
31379 @item
31380 If you have the Microsoft import library (.lib), it is possible to get
31381 the right symbols by using Microsoft @code{dumpbin} tool (see the
31382 corresponding Microsoft documentation for further details).
31383
31384 @smallexample
31385 $ dumpbin /exports api.lib
31386 @end smallexample
31387
31388 @item
31389 If you have a message about a missing symbol at link time the compiler
31390 tells you what symbol is expected. You just have to go back to the
31391 definition file and add the right suffix.
31392 @end enumerate
31393
31394 @item
31395 Build the import library @code{libAPI.dll.a}, using @code{gnatdll}
31396 (@pxref{Using gnatdll}) as follows:
31397
31398 @smallexample
31399 $ gnatdll -e API.def -d API.dll
31400 @end smallexample
31401
31402 @noindent
31403 @code{gnatdll} takes as input a definition file @file{API.def} and the
31404 name of the DLL containing the services listed in the definition file
31405 @file{API.dll}. The name of the static import library generated is
31406 computed from the name of the definition file as follows: if the
31407 definition file name is @var{xyz}@code{.def}, the import library name will
31408 be @code{lib}@var{xyz}@code{.a}. Note that in the previous example option
31409 @option{-e} could have been removed because the name of the definition
31410 file (before the ``@code{.def}'' suffix) is the same as the name of the
31411 DLL (@pxref{Using gnatdll} for more information about @code{gnatdll}).
31412 @end enumerate
31413
31414 @node Microsoft-Style Import Library
31415 @subsubsection Microsoft-Style Import Library
31416
31417 @noindent
31418 With GNAT you can either use a GNAT-style or Microsoft-style import
31419 library. A Microsoft import library is needed only if you plan to make an
31420 Ada DLL available to applications developed with Microsoft
31421 tools (@pxref{Mixed-Language Programming on Windows}).
31422
31423 To create a Microsoft-style import library for @file{API.dll} you
31424 should proceed as follows:
31425
31426 @enumerate
31427 @item
31428 Create the definition file @file{API.def} from the DLL. For this use either
31429 the @code{dll2def} tool as described above or the Microsoft @code{dumpbin}
31430 tool (see the corresponding Microsoft documentation for further details).
31431
31432 @item
31433 Build the actual import library using Microsoft's @code{lib} utility:
31434
31435 @smallexample
31436 $ lib -machine:IX86 -def:API.def -out:API.lib
31437 @end smallexample
31438
31439 @noindent
31440 If you use the above command the definition file @file{API.def} must
31441 contain a line giving the name of the DLL:
31442
31443 @smallexample
31444 LIBRARY "API"
31445 @end smallexample
31446
31447 @noindent
31448 See the Microsoft documentation for further details about the usage of
31449 @code{lib}.
31450 @end enumerate
31451
31452 @node Building DLLs with GNAT
31453 @section Building DLLs with GNAT
31454 @cindex DLLs, building
31455
31456 @noindent
31457 This section explain how to build DLLs using the GNAT built-in DLL
31458 support. With the following procedure it is straight forward to build
31459 and use DLLs with GNAT.
31460
31461 @enumerate
31462
31463 @item building object files
31464
31465 The first step is to build all objects files that are to be included
31466 into the DLL. This is done by using the standard @command{gnatmake} tool.
31467
31468 @item building the DLL
31469
31470 To build the DLL you must use @command{gcc}'s @option{-shared}
31471 option. It is quite simple to use this method:
31472
31473 @smallexample
31474 $ gcc -shared -o api.dll obj1.o obj2.o @dots{}
31475 @end smallexample
31476
31477 It is important to note that in this case all symbols found in the
31478 object files are automatically exported. It is possible to restrict
31479 the set of symbols to export by passing to @command{gcc} a definition
31480 file, @pxref{The Definition File}. For example:
31481
31482 @smallexample
31483 $ gcc -shared -o api.dll api.def obj1.o obj2.o @dots{}
31484 @end smallexample
31485
31486 If you use a definition file you must export the elaboration procedures
31487 for every package that required one. Elaboration procedures are named
31488 using the package name followed by "_E".
31489
31490 @item preparing DLL to be used
31491
31492 For the DLL to be used by client programs the bodies must be hidden
31493 from it and the .ali set with read-only attribute. This is very important
31494 otherwise GNAT will recompile all packages and will not actually use
31495 the code in the DLL. For example:
31496
31497 @smallexample
31498 $ mkdir apilib
31499 $ copy *.ads *.ali api.dll apilib
31500 $ attrib +R apilib\*.ali
31501 @end smallexample
31502
31503 @end enumerate
31504
31505 At this point it is possible to use the DLL by directly linking
31506 against it. Note that you must use the GNAT shared runtime when using
31507 GNAT shared libraries. This is achieved by using @option{-shared} binder's
31508 option.
31509
31510 @smallexample
31511 $ gnatmake main -Iapilib -bargs -shared -largs -Lapilib -lAPI
31512 @end smallexample
31513
31514 @node Building DLLs with GNAT Project files
31515 @section Building DLLs with GNAT Project files
31516 @cindex DLLs, building
31517
31518 @noindent
31519 There is nothing specific to Windows in the build process.
31520 @pxref{Library Projects}.
31521
31522 @noindent
31523 Due to a system limitation, it is not possible under Windows to create threads
31524 when inside the @code{DllMain} routine which is used for auto-initialization
31525 of shared libraries, so it is not possible to have library level tasks in SALs.
31526
31527 @node Building DLLs with gnatdll
31528 @section Building DLLs with gnatdll
31529 @cindex DLLs, building
31530
31531 @menu
31532 * Limitations When Using Ada DLLs from Ada::
31533 * Exporting Ada Entities::
31534 * Ada DLLs and Elaboration::
31535 * Ada DLLs and Finalization::
31536 * Creating a Spec for Ada DLLs::
31537 * Creating the Definition File::
31538 * Using gnatdll::
31539 @end menu
31540
31541 @noindent
31542 Note that it is preferred to use the built-in GNAT DLL support
31543 (@pxref{Building DLLs with GNAT}) or GNAT Project files
31544 (@pxref{Building DLLs with GNAT Project files}) to build DLLs.
31545
31546 This section explains how to build DLLs containing Ada code using
31547 @code{gnatdll}. These DLLs will be referred to as Ada DLLs in the
31548 remainder of this section.
31549
31550 The steps required to build an Ada DLL that is to be used by Ada as well as
31551 non-Ada applications are as follows:
31552
31553 @enumerate
31554 @item
31555 You need to mark each Ada @i{entity} exported by the DLL with a @code{C} or
31556 @code{Stdcall} calling convention to avoid any Ada name mangling for the
31557 entities exported by the DLL (@pxref{Exporting Ada Entities}). You can
31558 skip this step if you plan to use the Ada DLL only from Ada applications.
31559
31560 @item
31561 Your Ada code must export an initialization routine which calls the routine
31562 @code{adainit} generated by @command{gnatbind} to perform the elaboration of
31563 the Ada code in the DLL (@pxref{Ada DLLs and Elaboration}). The initialization
31564 routine exported by the Ada DLL must be invoked by the clients of the DLL
31565 to initialize the DLL.
31566
31567 @item
31568 When useful, the DLL should also export a finalization routine which calls
31569 routine @code{adafinal} generated by @command{gnatbind} to perform the
31570 finalization of the Ada code in the DLL (@pxref{Ada DLLs and Finalization}).
31571 The finalization routine exported by the Ada DLL must be invoked by the
31572 clients of the DLL when the DLL services are no further needed.
31573
31574 @item
31575 You must provide a spec for the services exported by the Ada DLL in each
31576 of the programming languages to which you plan to make the DLL available.
31577
31578 @item
31579 You must provide a definition file listing the exported entities
31580 (@pxref{The Definition File}).
31581
31582 @item
31583 Finally you must use @code{gnatdll} to produce the DLL and the import
31584 library (@pxref{Using gnatdll}).
31585 @end enumerate
31586
31587 @noindent
31588 Note that a relocatable DLL stripped using the @code{strip}
31589 binutils tool will not be relocatable anymore. To build a DLL without
31590 debug information pass @code{-largs -s} to @code{gnatdll}. This
31591 restriction does not apply to a DLL built using a Library Project.
31592 @pxref{Library Projects}.
31593
31594 @node Limitations When Using Ada DLLs from Ada
31595 @subsection Limitations When Using Ada DLLs from Ada
31596
31597 @noindent
31598 When using Ada DLLs from Ada applications there is a limitation users
31599 should be aware of. Because on Windows the GNAT run time is not in a DLL of
31600 its own, each Ada DLL includes a part of the GNAT run time. Specifically,
31601 each Ada DLL includes the services of the GNAT run time that are necessary
31602 to the Ada code inside the DLL. As a result, when an Ada program uses an
31603 Ada DLL there are two independent GNAT run times: one in the Ada DLL and
31604 one in the main program.
31605
31606 It is therefore not possible to exchange GNAT run-time objects between the
31607 Ada DLL and the main Ada program. Example of GNAT run-time objects are file
31608 handles (e.g.@: @code{Text_IO.File_Type}), tasks types, protected objects
31609 types, etc.
31610
31611 It is completely safe to exchange plain elementary, array or record types,
31612 Windows object handles, etc.
31613
31614 @node Exporting Ada Entities
31615 @subsection Exporting Ada Entities
31616 @cindex Export table
31617
31618 @noindent
31619 Building a DLL is a way to encapsulate a set of services usable from any
31620 application. As a result, the Ada entities exported by a DLL should be
31621 exported with the @code{C} or @code{Stdcall} calling conventions to avoid
31622 any Ada name mangling. As an example here is an Ada package
31623 @code{API}, spec and body, exporting two procedures, a function, and a
31624 variable:
31625
31626 @smallexample @c ada
31627 @group
31628 @cartouche
31629 with Interfaces.C; use Interfaces;
31630 package API is
31631 Count : C.int := 0;
31632 function Factorial (Val : C.int) return C.int;
31633
31634 procedure Initialize_API;
31635 procedure Finalize_API;
31636 -- Initialization & Finalization routines. More in the next section.
31637 private
31638 pragma Export (C, Initialize_API);
31639 pragma Export (C, Finalize_API);
31640 pragma Export (C, Count);
31641 pragma Export (C, Factorial);
31642 end API;
31643 @end cartouche
31644 @end group
31645 @end smallexample
31646
31647 @smallexample @c ada
31648 @group
31649 @cartouche
31650 package body API is
31651 function Factorial (Val : C.int) return C.int is
31652 Fact : C.int := 1;
31653 begin
31654 Count := Count + 1;
31655 for K in 1 .. Val loop
31656 Fact := Fact * K;
31657 end loop;
31658 return Fact;
31659 end Factorial;
31660
31661 procedure Initialize_API is
31662 procedure Adainit;
31663 pragma Import (C, Adainit);
31664 begin
31665 Adainit;
31666 end Initialize_API;
31667
31668 procedure Finalize_API is
31669 procedure Adafinal;
31670 pragma Import (C, Adafinal);
31671 begin
31672 Adafinal;
31673 end Finalize_API;
31674 end API;
31675 @end cartouche
31676 @end group
31677 @end smallexample
31678
31679 @noindent
31680 If the Ada DLL you are building will only be used by Ada applications
31681 you do not have to export Ada entities with a @code{C} or @code{Stdcall}
31682 convention. As an example, the previous package could be written as
31683 follows:
31684
31685 @smallexample @c ada
31686 @group
31687 @cartouche
31688 package API is
31689 Count : Integer := 0;
31690 function Factorial (Val : Integer) return Integer;
31691
31692 procedure Initialize_API;
31693 procedure Finalize_API;
31694 -- Initialization and Finalization routines.
31695 end API;
31696 @end cartouche
31697 @end group
31698 @end smallexample
31699
31700 @smallexample @c ada
31701 @group
31702 @cartouche
31703 package body API is
31704 function Factorial (Val : Integer) return Integer is
31705 Fact : Integer := 1;
31706 begin
31707 Count := Count + 1;
31708 for K in 1 .. Val loop
31709 Fact := Fact * K;
31710 end loop;
31711 return Fact;
31712 end Factorial;
31713
31714 @dots{}
31715 -- The remainder of this package body is unchanged.
31716 end API;
31717 @end cartouche
31718 @end group
31719 @end smallexample
31720
31721 @noindent
31722 Note that if you do not export the Ada entities with a @code{C} or
31723 @code{Stdcall} convention you will have to provide the mangled Ada names
31724 in the definition file of the Ada DLL
31725 (@pxref{Creating the Definition File}).
31726
31727 @node Ada DLLs and Elaboration
31728 @subsection Ada DLLs and Elaboration
31729 @cindex DLLs and elaboration
31730
31731 @noindent
31732 The DLL that you are building contains your Ada code as well as all the
31733 routines in the Ada library that are needed by it. The first thing a
31734 user of your DLL must do is elaborate the Ada code
31735 (@pxref{Elaboration Order Handling in GNAT}).
31736
31737 To achieve this you must export an initialization routine
31738 (@code{Initialize_API} in the previous example), which must be invoked
31739 before using any of the DLL services. This elaboration routine must call
31740 the Ada elaboration routine @code{adainit} generated by the GNAT binder
31741 (@pxref{Binding with Non-Ada Main Programs}). See the body of
31742 @code{Initialize_Api} for an example. Note that the GNAT binder is
31743 automatically invoked during the DLL build process by the @code{gnatdll}
31744 tool (@pxref{Using gnatdll}).
31745
31746 When a DLL is loaded, Windows systematically invokes a routine called
31747 @code{DllMain}. It would therefore be possible to call @code{adainit}
31748 directly from @code{DllMain} without having to provide an explicit
31749 initialization routine. Unfortunately, it is not possible to call
31750 @code{adainit} from the @code{DllMain} if your program has library level
31751 tasks because access to the @code{DllMain} entry point is serialized by
31752 the system (that is, only a single thread can execute ``through'' it at a
31753 time), which means that the GNAT run time will deadlock waiting for the
31754 newly created task to complete its initialization.
31755
31756 @node Ada DLLs and Finalization
31757 @subsection Ada DLLs and Finalization
31758 @cindex DLLs and finalization
31759
31760 @noindent
31761 When the services of an Ada DLL are no longer needed, the client code should
31762 invoke the DLL finalization routine, if available. The DLL finalization
31763 routine is in charge of releasing all resources acquired by the DLL. In the
31764 case of the Ada code contained in the DLL, this is achieved by calling
31765 routine @code{adafinal} generated by the GNAT binder
31766 (@pxref{Binding with Non-Ada Main Programs}).
31767 See the body of @code{Finalize_Api} for an
31768 example. As already pointed out the GNAT binder is automatically invoked
31769 during the DLL build process by the @code{gnatdll} tool
31770 (@pxref{Using gnatdll}).
31771
31772 @node Creating a Spec for Ada DLLs
31773 @subsection Creating a Spec for Ada DLLs
31774
31775 @noindent
31776 To use the services exported by the Ada DLL from another programming
31777 language (e.g.@: C), you have to translate the specs of the exported Ada
31778 entities in that language. For instance in the case of @code{API.dll},
31779 the corresponding C header file could look like:
31780
31781 @smallexample
31782 @group
31783 @cartouche
31784 extern int *_imp__count;
31785 #define count (*_imp__count)
31786 int factorial (int);
31787 @end cartouche
31788 @end group
31789 @end smallexample
31790
31791 @noindent
31792 It is important to understand that when building an Ada DLL to be used by
31793 other Ada applications, you need two different specs for the packages
31794 contained in the DLL: one for building the DLL and the other for using
31795 the DLL. This is because the @code{DLL} calling convention is needed to
31796 use a variable defined in a DLL, but when building the DLL, the variable
31797 must have either the @code{Ada} or @code{C} calling convention. As an
31798 example consider a DLL comprising the following package @code{API}:
31799
31800 @smallexample @c ada
31801 @group
31802 @cartouche
31803 package API is
31804 Count : Integer := 0;
31805 @dots{}
31806 -- Remainder of the package omitted.
31807 end API;
31808 @end cartouche
31809 @end group
31810 @end smallexample
31811
31812 @noindent
31813 After producing a DLL containing package @code{API}, the spec that
31814 must be used to import @code{API.Count} from Ada code outside of the
31815 DLL is:
31816
31817 @smallexample @c ada
31818 @group
31819 @cartouche
31820 package API is
31821 Count : Integer;
31822 pragma Import (DLL, Count);
31823 end API;
31824 @end cartouche
31825 @end group
31826 @end smallexample
31827
31828 @node Creating the Definition File
31829 @subsection Creating the Definition File
31830
31831 @noindent
31832 The definition file is the last file needed to build the DLL. It lists
31833 the exported symbols. As an example, the definition file for a DLL
31834 containing only package @code{API} (where all the entities are exported
31835 with a @code{C} calling convention) is:
31836
31837 @smallexample
31838 @group
31839 @cartouche
31840 EXPORTS
31841 count
31842 factorial
31843 finalize_api
31844 initialize_api
31845 @end cartouche
31846 @end group
31847 @end smallexample
31848
31849 @noindent
31850 If the @code{C} calling convention is missing from package @code{API},
31851 then the definition file contains the mangled Ada names of the above
31852 entities, which in this case are:
31853
31854 @smallexample
31855 @group
31856 @cartouche
31857 EXPORTS
31858 api__count
31859 api__factorial
31860 api__finalize_api
31861 api__initialize_api
31862 @end cartouche
31863 @end group
31864 @end smallexample
31865
31866 @node Using gnatdll
31867 @subsection Using @code{gnatdll}
31868 @findex gnatdll
31869
31870 @menu
31871 * gnatdll Example::
31872 * gnatdll behind the Scenes::
31873 * Using dlltool::
31874 @end menu
31875
31876 @noindent
31877 @code{gnatdll} is a tool to automate the DLL build process once all the Ada
31878 and non-Ada sources that make up your DLL have been compiled.
31879 @code{gnatdll} is actually in charge of two distinct tasks: build the
31880 static import library for the DLL and the actual DLL. The form of the
31881 @code{gnatdll} command is
31882
31883 @smallexample
31884 @cartouche
31885 $ gnatdll @ovar{switches} @var{list-of-files} @r{[}-largs @var{opts}@r{]}
31886 @end cartouche
31887 @end smallexample
31888
31889 @noindent
31890 where @var{list-of-files} is a list of ALI and object files. The object
31891 file list must be the exact list of objects corresponding to the non-Ada
31892 sources whose services are to be included in the DLL. The ALI file list
31893 must be the exact list of ALI files for the corresponding Ada sources
31894 whose services are to be included in the DLL. If @var{list-of-files} is
31895 missing, only the static import library is generated.
31896
31897 @noindent
31898 You may specify any of the following switches to @code{gnatdll}:
31899
31900 @table @code
31901 @item -a@ovar{address}
31902 @cindex @option{-a} (@code{gnatdll})
31903 Build a non-relocatable DLL at @var{address}. If @var{address} is not
31904 specified the default address @var{0x11000000} will be used. By default,
31905 when this switch is missing, @code{gnatdll} builds relocatable DLL. We
31906 advise the reader to build relocatable DLL.
31907
31908 @item -b @var{address}
31909 @cindex @option{-b} (@code{gnatdll})
31910 Set the relocatable DLL base address. By default the address is
31911 @code{0x11000000}.
31912
31913 @item -bargs @var{opts}
31914 @cindex @option{-bargs} (@code{gnatdll})
31915 Binder options. Pass @var{opts} to the binder.
31916
31917 @item -d @var{dllfile}
31918 @cindex @option{-d} (@code{gnatdll})
31919 @var{dllfile} is the name of the DLL. This switch must be present for
31920 @code{gnatdll} to do anything. The name of the generated import library is
31921 obtained algorithmically from @var{dllfile} as shown in the following
31922 example: if @var{dllfile} is @code{xyz.dll}, the import library name is
31923 @code{libxyz.dll.a}. The name of the definition file to use (if not specified
31924 by option @option{-e}) is obtained algorithmically from @var{dllfile}
31925 as shown in the following example:
31926 if @var{dllfile} is @code{xyz.dll}, the definition
31927 file used is @code{xyz.def}.
31928
31929 @item -e @var{deffile}
31930 @cindex @option{-e} (@code{gnatdll})
31931 @var{deffile} is the name of the definition file.
31932
31933 @item -g
31934 @cindex @option{-g} (@code{gnatdll})
31935 Generate debugging information. This information is stored in the object
31936 file and copied from there to the final DLL file by the linker,
31937 where it can be read by the debugger. You must use the
31938 @option{-g} switch if you plan on using the debugger or the symbolic
31939 stack traceback.
31940
31941 @item -h
31942 @cindex @option{-h} (@code{gnatdll})
31943 Help mode. Displays @code{gnatdll} switch usage information.
31944
31945 @item -Idir
31946 @cindex @option{-I} (@code{gnatdll})
31947 Direct @code{gnatdll} to search the @var{dir} directory for source and
31948 object files needed to build the DLL.
31949 (@pxref{Search Paths and the Run-Time Library (RTL)}).
31950
31951 @item -k
31952 @cindex @option{-k} (@code{gnatdll})
31953 Removes the @code{@@}@var{nn} suffix from the import library's exported
31954 names, but keeps them for the link names. You must specify this
31955 option if you want to use a @code{Stdcall} function in a DLL for which
31956 the @code{@@}@var{nn} suffix has been removed. This is the case for most
31957 of the Windows NT DLL for example. This option has no effect when
31958 @option{-n} option is specified.
31959
31960 @item -l @var{file}
31961 @cindex @option{-l} (@code{gnatdll})
31962 The list of ALI and object files used to build the DLL are listed in
31963 @var{file}, instead of being given in the command line. Each line in
31964 @var{file} contains the name of an ALI or object file.
31965
31966 @item -n
31967 @cindex @option{-n} (@code{gnatdll})
31968 No Import. Do not create the import library.
31969
31970 @item -q
31971 @cindex @option{-q} (@code{gnatdll})
31972 Quiet mode. Do not display unnecessary messages.
31973
31974 @item -v
31975 @cindex @option{-v} (@code{gnatdll})
31976 Verbose mode. Display extra information.
31977
31978 @item -largs @var{opts}
31979 @cindex @option{-largs} (@code{gnatdll})
31980 Linker options. Pass @var{opts} to the linker.
31981 @end table
31982
31983 @node gnatdll Example
31984 @subsubsection @code{gnatdll} Example
31985
31986 @noindent
31987 As an example the command to build a relocatable DLL from @file{api.adb}
31988 once @file{api.adb} has been compiled and @file{api.def} created is
31989
31990 @smallexample
31991 $ gnatdll -d api.dll api.ali
31992 @end smallexample
31993
31994 @noindent
31995 The above command creates two files: @file{libapi.dll.a} (the import
31996 library) and @file{api.dll} (the actual DLL). If you want to create
31997 only the DLL, just type:
31998
31999 @smallexample
32000 $ gnatdll -d api.dll -n api.ali
32001 @end smallexample
32002
32003 @noindent
32004 Alternatively if you want to create just the import library, type:
32005
32006 @smallexample
32007 $ gnatdll -d api.dll
32008 @end smallexample
32009
32010 @node gnatdll behind the Scenes
32011 @subsubsection @code{gnatdll} behind the Scenes
32012
32013 @noindent
32014 This section details the steps involved in creating a DLL. @code{gnatdll}
32015 does these steps for you. Unless you are interested in understanding what
32016 goes on behind the scenes, you should skip this section.
32017
32018 We use the previous example of a DLL containing the Ada package @code{API},
32019 to illustrate the steps necessary to build a DLL. The starting point is a
32020 set of objects that will make up the DLL and the corresponding ALI
32021 files. In the case of this example this means that @file{api.o} and
32022 @file{api.ali} are available. To build a relocatable DLL, @code{gnatdll} does
32023 the following:
32024
32025 @enumerate
32026 @item
32027 @code{gnatdll} builds the base file (@file{api.base}). A base file gives
32028 the information necessary to generate relocation information for the
32029 DLL.
32030
32031 @smallexample
32032 @group
32033 $ gnatbind -n api
32034 $ gnatlink api -o api.jnk -mdll -Wl,--base-file,api.base
32035 @end group
32036 @end smallexample
32037
32038 @noindent
32039 In addition to the base file, the @command{gnatlink} command generates an
32040 output file @file{api.jnk} which can be discarded. The @option{-mdll} switch
32041 asks @command{gnatlink} to generate the routines @code{DllMain} and
32042 @code{DllMainCRTStartup} that are called by the Windows loader when the DLL
32043 is loaded into memory.
32044
32045 @item
32046 @code{gnatdll} uses @code{dlltool} (@pxref{Using dlltool}) to build the
32047 export table (@file{api.exp}). The export table contains the relocation
32048 information in a form which can be used during the final link to ensure
32049 that the Windows loader is able to place the DLL anywhere in memory.
32050
32051 @smallexample
32052 @group
32053 $ dlltool --dllname api.dll --def api.def --base-file api.base \
32054 --output-exp api.exp
32055 @end group
32056 @end smallexample
32057
32058 @item
32059 @code{gnatdll} builds the base file using the new export table. Note that
32060 @command{gnatbind} must be called once again since the binder generated file
32061 has been deleted during the previous call to @command{gnatlink}.
32062
32063 @smallexample
32064 @group
32065 $ gnatbind -n api
32066 $ gnatlink api -o api.jnk api.exp -mdll
32067 -Wl,--base-file,api.base
32068 @end group
32069 @end smallexample
32070
32071 @item
32072 @code{gnatdll} builds the new export table using the new base file and
32073 generates the DLL import library @file{libAPI.dll.a}.
32074
32075 @smallexample
32076 @group
32077 $ dlltool --dllname api.dll --def api.def --base-file api.base \
32078 --output-exp api.exp --output-lib libAPI.a
32079 @end group
32080 @end smallexample
32081
32082 @item
32083 Finally @code{gnatdll} builds the relocatable DLL using the final export
32084 table.
32085
32086 @smallexample
32087 @group
32088 $ gnatbind -n api
32089 $ gnatlink api api.exp -o api.dll -mdll
32090 @end group
32091 @end smallexample
32092 @end enumerate
32093
32094 @node Using dlltool
32095 @subsubsection Using @code{dlltool}
32096
32097 @noindent
32098 @code{dlltool} is the low-level tool used by @code{gnatdll} to build
32099 DLLs and static import libraries. This section summarizes the most
32100 common @code{dlltool} switches. The form of the @code{dlltool} command
32101 is
32102
32103 @smallexample
32104 $ dlltool @ovar{switches}
32105 @end smallexample
32106
32107 @noindent
32108 @code{dlltool} switches include:
32109
32110 @table @option
32111 @item --base-file @var{basefile}
32112 @cindex @option{--base-file} (@command{dlltool})
32113 Read the base file @var{basefile} generated by the linker. This switch
32114 is used to create a relocatable DLL.
32115
32116 @item --def @var{deffile}
32117 @cindex @option{--def} (@command{dlltool})
32118 Read the definition file.
32119
32120 @item --dllname @var{name}
32121 @cindex @option{--dllname} (@command{dlltool})
32122 Gives the name of the DLL. This switch is used to embed the name of the
32123 DLL in the static import library generated by @code{dlltool} with switch
32124 @option{--output-lib}.
32125
32126 @item -k
32127 @cindex @option{-k} (@command{dlltool})
32128 Kill @code{@@}@var{nn} from exported names
32129 (@pxref{Windows Calling Conventions}
32130 for a discussion about @code{Stdcall}-style symbols.
32131
32132 @item --help
32133 @cindex @option{--help} (@command{dlltool})
32134 Prints the @code{dlltool} switches with a concise description.
32135
32136 @item --output-exp @var{exportfile}
32137 @cindex @option{--output-exp} (@command{dlltool})
32138 Generate an export file @var{exportfile}. The export file contains the
32139 export table (list of symbols in the DLL) and is used to create the DLL.
32140
32141 @item --output-lib @var{libfile}
32142 @cindex @option{--output-lib} (@command{dlltool})
32143 Generate a static import library @var{libfile}.
32144
32145 @item -v
32146 @cindex @option{-v} (@command{dlltool})
32147 Verbose mode.
32148
32149 @item --as @var{assembler-name}
32150 @cindex @option{--as} (@command{dlltool})
32151 Use @var{assembler-name} as the assembler. The default is @code{as}.
32152 @end table
32153
32154 @node GNAT and Windows Resources
32155 @section GNAT and Windows Resources
32156 @cindex Resources, windows
32157
32158 @menu
32159 * Building Resources::
32160 * Compiling Resources::
32161 * Using Resources::
32162 @end menu
32163
32164 @noindent
32165 Resources are an easy way to add Windows specific objects to your
32166 application. The objects that can be added as resources include:
32167
32168 @itemize @bullet
32169 @item
32170 menus
32171
32172 @item
32173 accelerators
32174
32175 @item
32176 dialog boxes
32177
32178 @item
32179 string tables
32180
32181 @item
32182 bitmaps
32183
32184 @item
32185 cursors
32186
32187 @item
32188 icons
32189
32190 @item
32191 fonts
32192 @end itemize
32193
32194 @noindent
32195 This section explains how to build, compile and use resources.
32196
32197 @node Building Resources
32198 @subsection Building Resources
32199 @cindex Resources, building
32200
32201 @noindent
32202 A resource file is an ASCII file. By convention resource files have an
32203 @file{.rc} extension.
32204 The easiest way to build a resource file is to use Microsoft tools
32205 such as @code{imagedit.exe} to build bitmaps, icons and cursors and
32206 @code{dlgedit.exe} to build dialogs.
32207 It is always possible to build an @file{.rc} file yourself by writing a
32208 resource script.
32209
32210 It is not our objective to explain how to write a resource file. A
32211 complete description of the resource script language can be found in the
32212 Microsoft documentation.
32213
32214 @node Compiling Resources
32215 @subsection Compiling Resources
32216 @findex rc
32217 @findex windres
32218 @cindex Resources, compiling
32219
32220 @noindent
32221 This section describes how to build a GNAT-compatible (COFF) object file
32222 containing the resources. This is done using the Resource Compiler
32223 @code{windres} as follows:
32224
32225 @smallexample
32226 $ windres -i myres.rc -o myres.o
32227 @end smallexample
32228
32229 @noindent
32230 By default @code{windres} will run @command{gcc} to preprocess the @file{.rc}
32231 file. You can specify an alternate preprocessor (usually named
32232 @file{cpp.exe}) using the @code{windres} @option{--preprocessor}
32233 parameter. A list of all possible options may be obtained by entering
32234 the command @code{windres} @option{--help}.
32235
32236 It is also possible to use the Microsoft resource compiler @code{rc.exe}
32237 to produce a @file{.res} file (binary resource file). See the
32238 corresponding Microsoft documentation for further details. In this case
32239 you need to use @code{windres} to translate the @file{.res} file to a
32240 GNAT-compatible object file as follows:
32241
32242 @smallexample
32243 $ windres -i myres.res -o myres.o
32244 @end smallexample
32245
32246 @node Using Resources
32247 @subsection Using Resources
32248 @cindex Resources, using
32249
32250 @noindent
32251 To include the resource file in your program just add the
32252 GNAT-compatible object file for the resource(s) to the linker
32253 arguments. With @command{gnatmake} this is done by using the @option{-largs}
32254 option:
32255
32256 @smallexample
32257 $ gnatmake myprog -largs myres.o
32258 @end smallexample
32259
32260 @node Debugging a DLL
32261 @section Debugging a DLL
32262 @cindex DLL debugging
32263
32264 @menu
32265 * Program and DLL Both Built with GCC/GNAT::
32266 * Program Built with Foreign Tools and DLL Built with GCC/GNAT::
32267 @end menu
32268
32269 @noindent
32270 Debugging a DLL is similar to debugging a standard program. But
32271 we have to deal with two different executable parts: the DLL and the
32272 program that uses it. We have the following four possibilities:
32273
32274 @enumerate 1
32275 @item
32276 The program and the DLL are built with @code{GCC/GNAT}.
32277 @item
32278 The program is built with foreign tools and the DLL is built with
32279 @code{GCC/GNAT}.
32280 @item
32281 The program is built with @code{GCC/GNAT} and the DLL is built with
32282 foreign tools.
32283 @item
32284 @end enumerate
32285
32286 @noindent
32287 In this section we address only cases one and two above.
32288 There is no point in trying to debug
32289 a DLL with @code{GNU/GDB}, if there is no GDB-compatible debugging
32290 information in it. To do so you must use a debugger compatible with the
32291 tools suite used to build the DLL.
32292
32293 @node Program and DLL Both Built with GCC/GNAT
32294 @subsection Program and DLL Both Built with GCC/GNAT
32295
32296 @noindent
32297 This is the simplest case. Both the DLL and the program have @code{GDB}
32298 compatible debugging information. It is then possible to break anywhere in
32299 the process. Let's suppose here that the main procedure is named
32300 @code{ada_main} and that in the DLL there is an entry point named
32301 @code{ada_dll}.
32302
32303 @noindent
32304 The DLL (@pxref{Introduction to Dynamic Link Libraries (DLLs)}) and
32305 program must have been built with the debugging information (see GNAT -g
32306 switch). Here are the step-by-step instructions for debugging it:
32307
32308 @enumerate 1
32309 @item Launch @code{GDB} on the main program.
32310
32311 @smallexample
32312 $ gdb -nw ada_main
32313 @end smallexample
32314
32315 @item Start the program and stop at the beginning of the main procedure
32316
32317 @smallexample
32318 (gdb) start
32319 @end smallexample
32320
32321 @noindent
32322 This step is required to be able to set a breakpoint inside the DLL. As long
32323 as the program is not run, the DLL is not loaded. This has the
32324 consequence that the DLL debugging information is also not loaded, so it is not
32325 possible to set a breakpoint in the DLL.
32326
32327 @item Set a breakpoint inside the DLL
32328
32329 @smallexample
32330 (gdb) break ada_dll
32331 (gdb) cont
32332 @end smallexample
32333
32334 @end enumerate
32335
32336 @noindent
32337 At this stage a breakpoint is set inside the DLL. From there on
32338 you can use the standard approach to debug the whole program
32339 (@pxref{Running and Debugging Ada Programs}).
32340
32341 @ignore
32342 @c This used to work, probably because the DLLs were non-relocatable
32343 @c keep this section around until the problem is sorted out.
32344
32345 To break on the @code{DllMain} routine it is not possible to follow
32346 the procedure above. At the time the program stop on @code{ada_main}
32347 the @code{DllMain} routine as already been called. Either you can use
32348 the procedure below @pxref{Debugging the DLL Directly} or this procedure:
32349
32350 @enumerate 1
32351 @item Launch @code{GDB} on the main program.
32352
32353 @smallexample
32354 $ gdb ada_main
32355 @end smallexample
32356
32357 @item Load DLL symbols
32358
32359 @smallexample
32360 (gdb) add-sym api.dll
32361 @end smallexample
32362
32363 @item Set a breakpoint inside the DLL
32364
32365 @smallexample
32366 (gdb) break ada_dll.adb:45
32367 @end smallexample
32368
32369 Note that at this point it is not possible to break using the routine symbol
32370 directly as the program is not yet running. The solution is to break
32371 on the proper line (break in @file{ada_dll.adb} line 45).
32372
32373 @item Start the program
32374
32375 @smallexample
32376 (gdb) run
32377 @end smallexample
32378
32379 @end enumerate
32380 @end ignore
32381
32382 @node Program Built with Foreign Tools and DLL Built with GCC/GNAT
32383 @subsection Program Built with Foreign Tools and DLL Built with GCC/GNAT
32384
32385 @menu
32386 * Debugging the DLL Directly::
32387 * Attaching to a Running Process::
32388 @end menu
32389
32390 @noindent
32391 In this case things are slightly more complex because it is not possible to
32392 start the main program and then break at the beginning to load the DLL and the
32393 associated DLL debugging information. It is not possible to break at the
32394 beginning of the program because there is no @code{GDB} debugging information,
32395 and therefore there is no direct way of getting initial control. This
32396 section addresses this issue by describing some methods that can be used
32397 to break somewhere in the DLL to debug it.
32398
32399 @noindent
32400 First suppose that the main procedure is named @code{main} (this is for
32401 example some C code built with Microsoft Visual C) and that there is a
32402 DLL named @code{test.dll} containing an Ada entry point named
32403 @code{ada_dll}.
32404
32405 @noindent
32406 The DLL (@pxref{Introduction to Dynamic Link Libraries (DLLs)}) must have
32407 been built with debugging information (see GNAT -g option).
32408
32409 @node Debugging the DLL Directly
32410 @subsubsection Debugging the DLL Directly
32411
32412 @enumerate 1
32413 @item
32414 Find out the executable starting address
32415
32416 @smallexample
32417 $ objdump --file-header main.exe
32418 @end smallexample
32419
32420 The starting address is reported on the last line. For example:
32421
32422 @smallexample
32423 main.exe: file format pei-i386
32424 architecture: i386, flags 0x0000010a:
32425 EXEC_P, HAS_DEBUG, D_PAGED
32426 start address 0x00401010
32427 @end smallexample
32428
32429 @item
32430 Launch the debugger on the executable.
32431
32432 @smallexample
32433 $ gdb main.exe
32434 @end smallexample
32435
32436 @item
32437 Set a breakpoint at the starting address, and launch the program.
32438
32439 @smallexample
32440 $ (gdb) break *0x00401010
32441 $ (gdb) run
32442 @end smallexample
32443
32444 The program will stop at the given address.
32445
32446 @item
32447 Set a breakpoint on a DLL subroutine.
32448
32449 @smallexample
32450 (gdb) break ada_dll.adb:45
32451 @end smallexample
32452
32453 Or if you want to break using a symbol on the DLL, you need first to
32454 select the Ada language (language used by the DLL).
32455
32456 @smallexample
32457 (gdb) set language ada
32458 (gdb) break ada_dll
32459 @end smallexample
32460
32461 @item
32462 Continue the program.
32463
32464 @smallexample
32465 (gdb) cont
32466 @end smallexample
32467
32468 @noindent
32469 This will run the program until it reaches the breakpoint that has been
32470 set. From that point you can use the standard way to debug a program
32471 as described in (@pxref{Running and Debugging Ada Programs}).
32472
32473 @end enumerate
32474
32475 @noindent
32476 It is also possible to debug the DLL by attaching to a running process.
32477
32478 @node Attaching to a Running Process
32479 @subsubsection Attaching to a Running Process
32480 @cindex DLL debugging, attach to process
32481
32482 @noindent
32483 With @code{GDB} it is always possible to debug a running process by
32484 attaching to it. It is possible to debug a DLL this way. The limitation
32485 of this approach is that the DLL must run long enough to perform the
32486 attach operation. It may be useful for instance to insert a time wasting
32487 loop in the code of the DLL to meet this criterion.
32488
32489 @enumerate 1
32490
32491 @item Launch the main program @file{main.exe}.
32492
32493 @smallexample
32494 $ main
32495 @end smallexample
32496
32497 @item Use the Windows @i{Task Manager} to find the process ID. Let's say
32498 that the process PID for @file{main.exe} is 208.
32499
32500 @item Launch gdb.
32501
32502 @smallexample
32503 $ gdb
32504 @end smallexample
32505
32506 @item Attach to the running process to be debugged.
32507
32508 @smallexample
32509 (gdb) attach 208
32510 @end smallexample
32511
32512 @item Load the process debugging information.
32513
32514 @smallexample
32515 (gdb) symbol-file main.exe
32516 @end smallexample
32517
32518 @item Break somewhere in the DLL.
32519
32520 @smallexample
32521 (gdb) break ada_dll
32522 @end smallexample
32523
32524 @item Continue process execution.
32525
32526 @smallexample
32527 (gdb) cont
32528 @end smallexample
32529
32530 @end enumerate
32531
32532 @noindent
32533 This last step will resume the process execution, and stop at
32534 the breakpoint we have set. From there you can use the standard
32535 approach to debug a program as described in
32536 (@pxref{Running and Debugging Ada Programs}).
32537
32538 @node Setting Stack Size from gnatlink
32539 @section Setting Stack Size from @command{gnatlink}
32540
32541 @noindent
32542 It is possible to specify the program stack size at link time. On modern
32543 versions of Windows, starting with XP, this is mostly useful to set the size of
32544 the main stack (environment task). The other task stacks are set with pragma
32545 Storage_Size or with the @command{gnatbind -d} command.
32546
32547 Since older versions of Windows (2000, NT4, etc.) do not allow setting the
32548 reserve size of individual tasks, the link-time stack size applies to all
32549 tasks, and pragma Storage_Size has no effect.
32550 In particular, Stack Overflow checks are made against this
32551 link-time specified size.
32552
32553 This setting can be done with
32554 @command{gnatlink} using either:
32555
32556 @itemize @bullet
32557
32558 @item using @option{-Xlinker} linker option
32559
32560 @smallexample
32561 $ gnatlink hello -Xlinker --stack=0x10000,0x1000
32562 @end smallexample
32563
32564 This sets the stack reserve size to 0x10000 bytes and the stack commit
32565 size to 0x1000 bytes.
32566
32567 @item using @option{-Wl} linker option
32568
32569 @smallexample
32570 $ gnatlink hello -Wl,--stack=0x1000000
32571 @end smallexample
32572
32573 This sets the stack reserve size to 0x1000000 bytes. Note that with
32574 @option{-Wl} option it is not possible to set the stack commit size
32575 because the coma is a separator for this option.
32576
32577 @end itemize
32578
32579 @node Setting Heap Size from gnatlink
32580 @section Setting Heap Size from @command{gnatlink}
32581
32582 @noindent
32583 Under Windows systems, it is possible to specify the program heap size from
32584 @command{gnatlink} using either:
32585
32586 @itemize @bullet
32587
32588 @item using @option{-Xlinker} linker option
32589
32590 @smallexample
32591 $ gnatlink hello -Xlinker --heap=0x10000,0x1000
32592 @end smallexample
32593
32594 This sets the heap reserve size to 0x10000 bytes and the heap commit
32595 size to 0x1000 bytes.
32596
32597 @item using @option{-Wl} linker option
32598
32599 @smallexample
32600 $ gnatlink hello -Wl,--heap=0x1000000
32601 @end smallexample
32602
32603 This sets the heap reserve size to 0x1000000 bytes. Note that with
32604 @option{-Wl} option it is not possible to set the heap commit size
32605 because the coma is a separator for this option.
32606
32607 @end itemize
32608
32609 @end ifset
32610
32611 @c **********************************
32612 @c * GNU Free Documentation License *
32613 @c **********************************
32614 @include fdl.texi
32615 @c GNU Free Documentation License
32616
32617 @node Index,,GNU Free Documentation License, Top
32618 @unnumbered Index
32619
32620 @printindex cp
32621
32622 @contents
32623 @c Put table of contents at end, otherwise it precedes the "title page" in
32624 @c the .txt version
32625 @c Edit the pdf file to move the contents to the beginning, after the title
32626 @c page
32627
32628 @bye