]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/gnat_ugn.texi
2008-07-31 Hristian Kirtchev <kirtchev@adacore.com>
[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 -gnatem=@var{path}
3908 @cindex @option{-gnatem} (@command{gcc})
3909 Specify a mapping file
3910 @ifclear vms
3911 (the equal sign is optional)
3912 @end ifclear
3913 (@pxref{Units to Sources Mapping Files}).
3914
3915 @item -gnatep=@var{file}
3916 @cindex @option{-gnatep} (@command{gcc})
3917 Specify a preprocessing data file
3918 @ifclear vms
3919 (the equal sign is optional)
3920 @end ifclear
3921 (@pxref{Integrated Preprocessing}).
3922
3923 @item -gnatE
3924 @cindex @option{-gnatE} (@command{gcc})
3925 Full dynamic elaboration checks.
3926
3927 @item -gnatf
3928 @cindex @option{-gnatf} (@command{gcc})
3929 Full errors. Multiple errors per line, all undefined references, do not
3930 attempt to suppress cascaded errors.
3931
3932 @item -gnatF
3933 @cindex @option{-gnatF} (@command{gcc})
3934 Externals names are folded to all uppercase.
3935
3936 @item ^-gnatg^/GNAT_INTERNAL^
3937 @cindex @option{^-gnatg^/GNAT_INTERNAL^} (@command{gcc})
3938 Internal GNAT implementation mode. This should not be used for
3939 applications programs, it is intended only for use by the compiler
3940 and its run-time library. For documentation, see the GNAT sources.
3941 Note that @option{^-gnatg^/GNAT_INTERNAL^} implies
3942 @option{^-gnatwae^/WARNINGS=ALL,ERRORS^} and
3943 @option{^-gnatyg^/STYLE_CHECKS=GNAT^}
3944 so that all standard warnings and all standard style options are turned on.
3945 All warnings and style error messages are treated as errors.
3946
3947 @item -gnatG
3948 @cindex @option{-gnatG} (@command{gcc})
3949 List generated expanded code in source form.
3950
3951 @item ^-gnath^/HELP^
3952 @cindex @option{^-gnath^/HELP^} (@command{gcc})
3953 Output usage information. The output is written to @file{stdout}.
3954
3955 @item ^-gnati^/IDENTIFIER_CHARACTER_SET=^@var{c}
3956 @cindex @option{^-gnati^/IDENTIFIER_CHARACTER_SET^} (@command{gcc})
3957 Identifier character set
3958 @ifclear vms
3959 (@var{c}=1/2/3/4/8/9/p/f/n/w).
3960 @end ifclear
3961 For details of the possible selections for @var{c},
3962 see @ref{Character Set Control}.
3963
3964 @item ^-gnatI^/IGNORE_REP_CLAUSES^
3965 @cindex @option{^-gnatI^IGNORE_REP_CLAUSES^} (@command{gcc})
3966 Ignore representation clauses. When this switch is used, all
3967 representation clauses are treated as comments. This is useful
3968 when initially porting code where you want to ignore rep clause
3969 problems, and also for compiling foreign code (particularly
3970 for use with ASIS).
3971
3972 @item -gnatjnn
3973 @cindex @option{-gnatjnn} (@command{gcc})
3974 Reformat error messages to fit on nn character lines
3975
3976 @item -gnatk=@var{n}
3977 @cindex @option{-gnatk} (@command{gcc})
3978 Limit file names to @var{n} (1-999) characters ^(@code{k} = krunch)^^.
3979
3980 @item -gnatl
3981 @cindex @option{-gnatl} (@command{gcc})
3982 Output full source listing with embedded error messages.
3983
3984 @item -gnatL
3985 @cindex @option{-gnatL} (@command{gcc})
3986 Used in conjunction with -gnatG or -gnatD to intersperse original
3987 source lines (as comment lines with line numbers) in the expanded
3988 source output.
3989
3990 @item -gnatm=@var{n}
3991 @cindex @option{-gnatm} (@command{gcc})
3992 Limit number of detected error or warning messages to @var{n}
3993 where @var{n} is in the range 1..999_999. The default setting if
3994 no switch is given is 9999. Compilation is terminated if this
3995 limit is exceeded. The equal sign here is optional.
3996
3997 @item -gnatn
3998 @cindex @option{-gnatn} (@command{gcc})
3999 Activate inlining for subprograms for which
4000 pragma @code{inline} is specified. This inlining is performed
4001 by the GCC back-end.
4002
4003 @item -gnatN
4004 @cindex @option{-gnatN} (@command{gcc})
4005 Activate front end inlining for subprograms for which
4006 pragma @code{Inline} is specified. This inlining is performed
4007 by the front end and will be visible in the
4008 @option{-gnatG} output.
4009 In some cases, this has proved more effective than the back end
4010 inlining resulting from the use of
4011 @option{-gnatn}.
4012 Note that
4013 @option{-gnatN} automatically implies
4014 @option{-gnatn} so it is not necessary
4015 to specify both options. There are a few cases that the back-end inlining
4016 catches that cannot be dealt with in the front-end.
4017
4018 @item -gnato
4019 @cindex @option{-gnato} (@command{gcc})
4020 Enable numeric overflow checking (which is not normally enabled by
4021 default). Not that division by zero is a separate check that is not
4022 controlled by this switch (division by zero checking is on by default).
4023
4024 @item -gnatp
4025 @cindex @option{-gnatp} (@command{gcc})
4026 Suppress all checks.
4027
4028 @item -gnatP
4029 @cindex @option{-gnatP} (@command{gcc})
4030 Enable polling. This is required on some systems (notably Windows NT) to
4031 obtain asynchronous abort and asynchronous transfer of control capability.
4032 @xref{Pragma Polling,,, gnat_rm, GNAT Reference Manual}, for full
4033 details.
4034
4035 @item -gnatq
4036 @cindex @option{-gnatq} (@command{gcc})
4037 Don't quit; try semantics, even if parse errors.
4038
4039 @item -gnatQ
4040 @cindex @option{-gnatQ} (@command{gcc})
4041 Don't quit; generate @file{ALI} and tree files even if illegalities.
4042
4043 @item -gnatr
4044 @cindex @option{-gnatr} (@command{gcc})
4045 Treat pragma Restrictions as Restriction_Warnings.
4046
4047 @item ^-gnatR@r{[}0@r{/}1@r{/}2@r{/}3@r{[}s@r{]]}^/REPRESENTATION_INFO^
4048 @cindex @option{-gnatR} (@command{gcc})
4049 Output representation information for declared types and objects.
4050
4051 @item -gnats
4052 @cindex @option{-gnats} (@command{gcc})
4053 Syntax check only.
4054
4055 @item -gnatS
4056 @cindex @option{-gnatS} (@command{gcc})
4057 Print package Standard.
4058
4059 @item -gnatt
4060 @cindex @option{-gnatt} (@command{gcc})
4061 Generate tree output file.
4062
4063 @item ^-gnatT^/TABLE_MULTIPLIER=^@var{nnn}
4064 @cindex @option{^-gnatT^/TABLE_MULTIPLIER^} (@command{gcc})
4065 All compiler tables start at @var{nnn} times usual starting size.
4066
4067 @item -gnatu
4068 @cindex @option{-gnatu} (@command{gcc})
4069 List units for this compilation.
4070
4071 @item -gnatU
4072 @cindex @option{-gnatU} (@command{gcc})
4073 Tag all error messages with the unique string ``error:''
4074
4075 @item -gnatv
4076 @cindex @option{-gnatv} (@command{gcc})
4077 Verbose mode. Full error output with source lines to @file{stdout}.
4078
4079 @item -gnatV
4080 @cindex @option{-gnatV} (@command{gcc})
4081 Control level of validity checking. See separate section describing
4082 this feature.
4083
4084 @item ^-gnatw@var{xxx}^/WARNINGS=(@var{option}@r{[},@dots{}@r{]})^
4085 @cindex @option{^-gnatw^/WARNINGS^} (@command{gcc})
4086 Warning mode where
4087 ^@var{xxx} is a string of option letters that^the list of options^ denotes
4088 the exact warnings that
4089 are enabled or disabled (@pxref{Warning Message Control}).
4090
4091 @item ^-gnatW^/WIDE_CHARACTER_ENCODING=^@var{e}
4092 @cindex @option{^-gnatW^/WIDE_CHARACTER_ENCODING^} (@command{gcc})
4093 Wide character encoding method
4094 @ifclear vms
4095 (@var{e}=n/h/u/s/e/8).
4096 @end ifclear
4097 @ifset vms
4098 (@var{e}=@code{BRACKETS, NONE, HEX, UPPER, SHIFT_JIS, EUC, UTF8})
4099 @end ifset
4100
4101 @item -gnatx
4102 @cindex @option{-gnatx} (@command{gcc})
4103 Suppress generation of cross-reference information.
4104
4105 @item ^-gnaty^/STYLE_CHECKS=(option,option@dots{})^
4106 @cindex @option{^-gnaty^/STYLE_CHECKS^} (@command{gcc})
4107 Enable built-in style checks (@pxref{Style Checking}).
4108
4109 @item ^-gnatz^/DISTRIBUTION_STUBS=^@var{m}
4110 @cindex @option{^-gnatz^/DISTRIBUTION_STUBS^} (@command{gcc})
4111 Distribution stub generation and compilation
4112 @ifclear vms
4113 (@var{m}=r/c for receiver/caller stubs).
4114 @end ifclear
4115 @ifset vms
4116 (@var{m}=@code{RECEIVER} or @code{CALLER} to specify the type of stubs
4117 to be generated and compiled).
4118 @end ifset
4119
4120 @item ^-I^/SEARCH=^@var{dir}
4121 @cindex @option{^-I^/SEARCH^} (@command{gcc})
4122 @cindex RTL
4123 Direct GNAT to search the @var{dir} directory for source files needed by
4124 the current compilation
4125 (@pxref{Search Paths and the Run-Time Library (RTL)}).
4126
4127 @item ^-I-^/NOCURRENT_DIRECTORY^
4128 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gcc})
4129 @cindex RTL
4130 Except for the source file named in the command line, do not look for source
4131 files in the directory containing the source file named in the command line
4132 (@pxref{Search Paths and the Run-Time Library (RTL)}).
4133
4134 @ifclear vms
4135 @item -mbig-switch
4136 @cindex @option{-mbig-switch} (@command{gcc})
4137 @cindex @code{case} statement (effect of @option{-mbig-switch} option)
4138 This standard gcc switch causes the compiler to use larger offsets in its
4139 jump table representation for @code{case} statements.
4140 This may result in less efficient code, but is sometimes necessary
4141 (for example on HP-UX targets)
4142 @cindex HP-UX and @option{-mbig-switch} option
4143 in order to compile large and/or nested @code{case} statements.
4144
4145 @item -o @var{file}
4146 @cindex @option{-o} (@command{gcc})
4147 This switch is used in @command{gcc} to redirect the generated object file
4148 and its associated ALI file. Beware of this switch with GNAT, because it may
4149 cause the object file and ALI file to have different names which in turn
4150 may confuse the binder and the linker.
4151 @end ifclear
4152
4153 @item -nostdinc
4154 @cindex @option{-nostdinc} (@command{gcc})
4155 Inhibit the search of the default location for the GNAT Run Time
4156 Library (RTL) source files.
4157
4158 @item -nostdlib
4159 @cindex @option{-nostdlib} (@command{gcc})
4160 Inhibit the search of the default location for the GNAT Run Time
4161 Library (RTL) ALI files.
4162
4163 @ifclear vms
4164 @item -O@ovar{n}
4165 @cindex @option{-O} (@command{gcc})
4166 @var{n} controls the optimization level.
4167
4168 @table @asis
4169 @item n = 0
4170 No optimization, the default setting if no @option{-O} appears
4171
4172 @item n = 1
4173 Normal optimization, the default if you specify @option{-O} without
4174 an operand. A good compromise between code quality and compilation
4175 time.
4176
4177 @item n = 2
4178 Extensive optimization, may improve execution time, possibly at the cost of
4179 substantially increased compilation time.
4180
4181 @item n = 3
4182 Same as @option{-O2}, and also includes inline expansion for small subprograms
4183 in the same unit.
4184
4185 @item n = s
4186 Optimize space usage
4187 @end table
4188
4189 @noindent
4190 See also @ref{Optimization Levels}.
4191 @end ifclear
4192
4193 @ifset vms
4194 @item /NOOPTIMIZE
4195 @cindex @option{/NOOPTIMIZE} (@code{GNAT COMPILE})
4196 Equivalent to @option{/OPTIMIZE=NONE}.
4197 This is the default behavior in the absence of an @option{/OPTIMIZE}
4198 qualifier.
4199
4200 @item /OPTIMIZE@r{[}=(keyword@r{[},@dots{}@r{]})@r{]}
4201 @cindex @option{/OPTIMIZE} (@code{GNAT COMPILE})
4202 Selects the level of optimization for your program. The supported
4203 keywords are as follows:
4204 @table @code
4205 @item ALL
4206 Perform most optimizations, including those that
4207 are expensive.
4208 This is the default if the @option{/OPTIMIZE} qualifier is supplied
4209 without keyword options.
4210
4211 @item NONE
4212 Do not do any optimizations. Same as @code{/NOOPTIMIZE}.
4213
4214 @item SOME
4215 Perform some optimizations, but omit ones that are costly.
4216
4217 @item DEVELOPMENT
4218 Same as @code{SOME}.
4219
4220 @item INLINING
4221 Full optimization as in @option{/OPTIMIZE=ALL}, and also attempts
4222 automatic inlining of small subprograms within a unit
4223
4224 @item UNROLL_LOOPS
4225 Try to unroll loops. This keyword may be specified together with
4226 any keyword above other than @code{NONE}. Loop unrolling
4227 usually, but not always, improves the performance of programs.
4228
4229 @item SPACE
4230 Optimize space usage
4231 @end table
4232
4233 @noindent
4234 See also @ref{Optimization Levels}.
4235 @end ifset
4236
4237 @ifclear vms
4238 @item -pass-exit-codes
4239 @cindex @option{-pass-exit-codes} (@command{gcc})
4240 Catch exit codes from the compiler and use the most meaningful as
4241 exit status.
4242 @end ifclear
4243
4244 @item --RTS=@var{rts-path}
4245 @cindex @option{--RTS} (@command{gcc})
4246 Specifies the default location of the runtime library. Same meaning as the
4247 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
4248
4249 @item ^-S^/ASM^
4250 @cindex @option{^-S^/ASM^} (@command{gcc})
4251 ^Used in place of @option{-c} to^Used to^
4252 cause the assembler source file to be
4253 generated, using @file{^.s^.S^} as the extension,
4254 instead of the object file.
4255 This may be useful if you need to examine the generated assembly code.
4256
4257 @item ^-fverbose-asm^/VERBOSE_ASM^
4258 @cindex @option{^-fverbose-asm^/VERBOSE_ASM^} (@command{gcc})
4259 ^Used in conjunction with @option{-S}^Used in place of @option{/ASM}^
4260 to cause the generated assembly code file to be annotated with variable
4261 names, making it significantly easier to follow.
4262
4263 @item ^-v^/VERBOSE^
4264 @cindex @option{^-v^/VERBOSE^} (@command{gcc})
4265 Show commands generated by the @command{gcc} driver. Normally used only for
4266 debugging purposes or if you need to be sure what version of the
4267 compiler you are executing.
4268
4269 @ifclear vms
4270 @item -V @var{ver}
4271 @cindex @option{-V} (@command{gcc})
4272 Execute @var{ver} version of the compiler. This is the @command{gcc}
4273 version, not the GNAT version.
4274 @end ifclear
4275
4276 @item ^-w^/NO_BACK_END_WARNINGS^
4277 @cindex @option{-w} (@command{gcc})
4278 Turn off warnings generated by the back end of the compiler. Use of
4279 this switch also causes the default for front end warnings to be set
4280 to suppress (as though @option{-gnatws} had appeared at the start of
4281 the options).
4282
4283 @end table
4284
4285 @ifclear vms
4286 @c Combining qualifiers does not work on VMS
4287 You may combine a sequence of GNAT switches into a single switch. For
4288 example, the combined switch
4289
4290 @cindex Combining GNAT switches
4291 @smallexample
4292 -gnatofi3
4293 @end smallexample
4294
4295 @noindent
4296 is equivalent to specifying the following sequence of switches:
4297
4298 @smallexample
4299 -gnato -gnatf -gnati3
4300 @end smallexample
4301 @end ifclear
4302
4303 @noindent
4304 The following restrictions apply to the combination of switches
4305 in this manner:
4306
4307 @itemize @bullet
4308 @item
4309 The switch @option{-gnatc} if combined with other switches must come
4310 first in the string.
4311
4312 @item
4313 The switch @option{-gnats} if combined with other switches must come
4314 first in the string.
4315
4316 @item
4317 The switches
4318 @option{^-gnatz^/DISTRIBUTION_STUBS^}, @option{-gnatzc}, and @option{-gnatzr}
4319 may not be combined with any other switches.
4320
4321 @ifclear vms
4322 @item
4323 Once a ``y'' appears in the string (that is a use of the @option{-gnaty}
4324 switch), then all further characters in the switch are interpreted
4325 as style modifiers (see description of @option{-gnaty}).
4326
4327 @item
4328 Once a ``d'' appears in the string (that is a use of the @option{-gnatd}
4329 switch), then all further characters in the switch are interpreted
4330 as debug flags (see description of @option{-gnatd}).
4331
4332 @item
4333 Once a ``w'' appears in the string (that is a use of the @option{-gnatw}
4334 switch), then all further characters in the switch are interpreted
4335 as warning mode modifiers (see description of @option{-gnatw}).
4336
4337 @item
4338 Once a ``V'' appears in the string (that is a use of the @option{-gnatV}
4339 switch), then all further characters in the switch are interpreted
4340 as validity checking options (see description of @option{-gnatV}).
4341 @end ifclear
4342 @end itemize
4343
4344 @node Output and Error Message Control
4345 @subsection Output and Error Message Control
4346 @findex stderr
4347
4348 @noindent
4349 The standard default format for error messages is called ``brief format''.
4350 Brief format messages are written to @file{stderr} (the standard error
4351 file) and have the following form:
4352
4353 @smallexample
4354 e.adb:3:04: Incorrect spelling of keyword "function"
4355 e.adb:4:20: ";" should be "is"
4356 @end smallexample
4357
4358 @noindent
4359 The first integer after the file name is the line number in the file,
4360 and the second integer is the column number within the line.
4361 @ifclear vms
4362 @code{GPS} can parse the error messages
4363 and point to the referenced character.
4364 @end ifclear
4365 The following switches provide control over the error message
4366 format:
4367
4368 @table @option
4369 @c !sort!
4370 @item -gnatv
4371 @cindex @option{-gnatv} (@command{gcc})
4372 @findex stdout
4373 @ifclear vms
4374 The v stands for verbose.
4375 @end ifclear
4376 The effect of this setting is to write long-format error
4377 messages to @file{stdout} (the standard output file.
4378 The same program compiled with the
4379 @option{-gnatv} switch would generate:
4380
4381 @smallexample
4382 @cartouche
4383 3. funcion X (Q : Integer)
4384 |
4385 >>> Incorrect spelling of keyword "function"
4386 4. return Integer;
4387 |
4388 >>> ";" should be "is"
4389 @end cartouche
4390 @end smallexample
4391
4392 @noindent
4393 The vertical bar indicates the location of the error, and the @samp{>>>}
4394 prefix can be used to search for error messages. When this switch is
4395 used the only source lines output are those with errors.
4396
4397 @item -gnatl
4398 @cindex @option{-gnatl} (@command{gcc})
4399 @ifclear vms
4400 The @code{l} stands for list.
4401 @end ifclear
4402 This switch causes a full listing of
4403 the file to be generated. In the case where a body is
4404 compiled, the corresponding spec is also listed, along
4405 with any subunits. Typical output from compiling a package
4406 body @file{p.adb} might look like:
4407
4408 @smallexample @c ada
4409 @cartouche
4410 Compiling: p.adb
4411
4412 1. package body p is
4413 2. procedure a;
4414 3. procedure a is separate;
4415 4. begin
4416 5. null
4417 |
4418 >>> missing ";"
4419
4420 6. end;
4421
4422 Compiling: p.ads
4423
4424 1. package p is
4425 2. pragma Elaborate_Body
4426 |
4427 >>> missing ";"
4428
4429 3. end p;
4430
4431 Compiling: p-a.adb
4432
4433 1. separate p
4434 |
4435 >>> missing "("
4436
4437 2. procedure a is
4438 3. begin
4439 4. null
4440 |
4441 >>> missing ";"
4442
4443 5. end;
4444 @end cartouche
4445 @end smallexample
4446
4447 @noindent
4448 @findex stderr
4449 When you specify the @option{-gnatv} or @option{-gnatl} switches and
4450 standard output is redirected, a brief summary is written to
4451 @file{stderr} (standard error) giving the number of error messages and
4452 warning messages generated.
4453
4454 @item -^gnatl^OUTPUT_FILE^=file
4455 @cindex @option{^-gnatl^OUTPUT_FILE^=fname} (@command{gcc})
4456 This has the same effect as @option{-gnatl} except that the output is
4457 written to a file instead of to standard output. If the given name
4458 @file{fname} does not start with a period, then it is the full name
4459 of the file to be written. If @file{fname} is an extension, it is
4460 appended to the name of the file being compiled. For example, if
4461 file @file{xyz.adb} is compiled with @option{^-gnatl^OUTPUT_FILE^=.lst},
4462 then the output is written to file ^xyz.adb.lst^xyz.adb_lst^.
4463
4464 @item -gnatU
4465 @cindex @option{-gnatU} (@command{gcc})
4466 This switch forces all error messages to be preceded by the unique
4467 string ``error:''. This means that error messages take a few more
4468 characters in space, but allows easy searching for and identification
4469 of error messages.
4470
4471 @item -gnatb
4472 @cindex @option{-gnatb} (@command{gcc})
4473 @ifclear vms
4474 The @code{b} stands for brief.
4475 @end ifclear
4476 This switch causes GNAT to generate the
4477 brief format error messages to @file{stderr} (the standard error
4478 file) as well as the verbose
4479 format message or full listing (which as usual is written to
4480 @file{stdout} (the standard output file).
4481
4482 @item -gnatm=@var{n}
4483 @cindex @option{-gnatm} (@command{gcc})
4484 @ifclear vms
4485 The @code{m} stands for maximum.
4486 @end ifclear
4487 @var{n} is a decimal integer in the
4488 range of 1 to 999 and limits the number of error messages to be
4489 generated. For example, using @option{-gnatm2} might yield
4490
4491 @smallexample
4492 e.adb:3:04: Incorrect spelling of keyword "function"
4493 e.adb:5:35: missing ".."
4494 fatal error: maximum errors reached
4495 compilation abandoned
4496 @end smallexample
4497
4498 @noindent
4499 Note that the equal sign is optional, so the switches
4500 @option{-gnatm2} and @option{-gnatm=2} are equivalent.
4501
4502 @item -gnatf
4503 @cindex @option{-gnatf} (@command{gcc})
4504 @cindex Error messages, suppressing
4505 @ifclear vms
4506 The @code{f} stands for full.
4507 @end ifclear
4508 Normally, the compiler suppresses error messages that are likely to be
4509 redundant. This switch causes all error
4510 messages to be generated. In particular, in the case of
4511 references to undefined variables. If a given variable is referenced
4512 several times, the normal format of messages is
4513 @smallexample
4514 e.adb:7:07: "V" is undefined (more references follow)
4515 @end smallexample
4516
4517 @noindent
4518 where the parenthetical comment warns that there are additional
4519 references to the variable @code{V}. Compiling the same program with the
4520 @option{-gnatf} switch yields
4521
4522 @smallexample
4523 e.adb:7:07: "V" is undefined
4524 e.adb:8:07: "V" is undefined
4525 e.adb:8:12: "V" is undefined
4526 e.adb:8:16: "V" is undefined
4527 e.adb:9:07: "V" is undefined
4528 e.adb:9:12: "V" is undefined
4529 @end smallexample
4530
4531 @noindent
4532 The @option{-gnatf} switch also generates additional information for
4533 some error messages. Some examples are:
4534
4535 @itemize @bullet
4536 @item
4537 Full details on entities not available in high integrity mode
4538 @item
4539 Details on possibly non-portable unchecked conversion
4540 @item
4541 List possible interpretations for ambiguous calls
4542 @item
4543 Additional details on incorrect parameters
4544 @end itemize
4545
4546 @item -gnatjnn
4547 @cindex @option{-gnatjnn} (@command{gcc})
4548 In normal operation mode (or if @option{-gnatj0} is used, then error messages
4549 with continuation lines are treated as though the continuation lines were
4550 separate messages (and so a warning with two continuation lines counts as
4551 three warnings, and is listed as three separate messages).
4552
4553 If the @option{-gnatjnn} switch is used with a positive value for nn, then
4554 messages are output in a different manner. A message and all its continuation
4555 lines are treated as a unit, and count as only one warning or message in the
4556 statistics totals. Furthermore, the message is reformatted so that no line
4557 is longer than nn characters.
4558
4559 @item -gnatq
4560 @cindex @option{-gnatq} (@command{gcc})
4561 @ifclear vms
4562 The @code{q} stands for quit (really ``don't quit'').
4563 @end ifclear
4564 In normal operation mode, the compiler first parses the program and
4565 determines if there are any syntax errors. If there are, appropriate
4566 error messages are generated and compilation is immediately terminated.
4567 This switch tells
4568 GNAT to continue with semantic analysis even if syntax errors have been
4569 found. This may enable the detection of more errors in a single run. On
4570 the other hand, the semantic analyzer is more likely to encounter some
4571 internal fatal error when given a syntactically invalid tree.
4572
4573 @item -gnatQ
4574 @cindex @option{-gnatQ} (@command{gcc})
4575 In normal operation mode, the @file{ALI} file is not generated if any
4576 illegalities are detected in the program. The use of @option{-gnatQ} forces
4577 generation of the @file{ALI} file. This file is marked as being in
4578 error, so it cannot be used for binding purposes, but it does contain
4579 reasonably complete cross-reference information, and thus may be useful
4580 for use by tools (e.g., semantic browsing tools or integrated development
4581 environments) that are driven from the @file{ALI} file. This switch
4582 implies @option{-gnatq}, since the semantic phase must be run to get a
4583 meaningful ALI file.
4584
4585 In addition, if @option{-gnatt} is also specified, then the tree file is
4586 generated even if there are illegalities. It may be useful in this case
4587 to also specify @option{-gnatq} to ensure that full semantic processing
4588 occurs. The resulting tree file can be processed by ASIS, for the purpose
4589 of providing partial information about illegal units, but if the error
4590 causes the tree to be badly malformed, then ASIS may crash during the
4591 analysis.
4592
4593 When @option{-gnatQ} is used and the generated @file{ALI} file is marked as
4594 being in error, @command{gnatmake} will attempt to recompile the source when it
4595 finds such an @file{ALI} file, including with switch @option{-gnatc}.
4596
4597 Note that @option{-gnatQ} has no effect if @option{-gnats} is specified,
4598 since ALI files are never generated if @option{-gnats} is set.
4599
4600 @end table
4601
4602 @node Warning Message Control
4603 @subsection Warning Message Control
4604 @cindex Warning messages
4605 @noindent
4606 In addition to error messages, which correspond to illegalities as defined
4607 in the Ada Reference Manual, the compiler detects two kinds of warning
4608 situations.
4609
4610 First, the compiler considers some constructs suspicious and generates a
4611 warning message to alert you to a possible error. Second, if the
4612 compiler detects a situation that is sure to raise an exception at
4613 run time, it generates a warning message. The following shows an example
4614 of warning messages:
4615 @smallexample
4616 e.adb:4:24: warning: creation of object may raise Storage_Error
4617 e.adb:10:17: warning: static value out of range
4618 e.adb:10:17: warning: "Constraint_Error" will be raised at run time
4619 @end smallexample
4620
4621 @noindent
4622 GNAT considers a large number of situations as appropriate
4623 for the generation of warning messages. As always, warnings are not
4624 definite indications of errors. For example, if you do an out-of-range
4625 assignment with the deliberate intention of raising a
4626 @code{Constraint_Error} exception, then the warning that may be
4627 issued does not indicate an error. Some of the situations for which GNAT
4628 issues warnings (at least some of the time) are given in the following
4629 list. This list is not complete, and new warnings are often added to
4630 subsequent versions of GNAT. The list is intended to give a general idea
4631 of the kinds of warnings that are generated.
4632
4633 @itemize @bullet
4634 @item
4635 Possible infinitely recursive calls
4636
4637 @item
4638 Out-of-range values being assigned
4639
4640 @item
4641 Possible order of elaboration problems
4642
4643 @item
4644 Assertions (pragma Assert) that are sure to fail
4645
4646 @item
4647 Unreachable code
4648
4649 @item
4650 Address clauses with possibly unaligned values, or where an attempt is
4651 made to overlay a smaller variable with a larger one.
4652
4653 @item
4654 Fixed-point type declarations with a null range
4655
4656 @item
4657 Direct_IO or Sequential_IO instantiated with a type that has access values
4658
4659 @item
4660 Variables that are never assigned a value
4661
4662 @item
4663 Variables that are referenced before being initialized
4664
4665 @item
4666 Task entries with no corresponding @code{accept} statement
4667
4668 @item
4669 Duplicate accepts for the same task entry in a @code{select}
4670
4671 @item
4672 Objects that take too much storage
4673
4674 @item
4675 Unchecked conversion between types of differing sizes
4676
4677 @item
4678 Missing @code{return} statement along some execution path in a function
4679
4680 @item
4681 Incorrect (unrecognized) pragmas
4682
4683 @item
4684 Incorrect external names
4685
4686 @item
4687 Allocation from empty storage pool
4688
4689 @item
4690 Potentially blocking operation in protected type
4691
4692 @item
4693 Suspicious parenthesization of expressions
4694
4695 @item
4696 Mismatching bounds in an aggregate
4697
4698 @item
4699 Attempt to return local value by reference
4700
4701 @item
4702 Premature instantiation of a generic body
4703
4704 @item
4705 Attempt to pack aliased components
4706
4707 @item
4708 Out of bounds array subscripts
4709
4710 @item
4711 Wrong length on string assignment
4712
4713 @item
4714 Violations of style rules if style checking is enabled
4715
4716 @item
4717 Unused @code{with} clauses
4718
4719 @item
4720 @code{Bit_Order} usage that does not have any effect
4721
4722 @item
4723 @code{Standard.Duration} used to resolve universal fixed expression
4724
4725 @item
4726 Dereference of possibly null value
4727
4728 @item
4729 Declaration that is likely to cause storage error
4730
4731 @item
4732 Internal GNAT unit @code{with}'ed by application unit
4733
4734 @item
4735 Values known to be out of range at compile time
4736
4737 @item
4738 Unreferenced labels and variables
4739
4740 @item
4741 Address overlays that could clobber memory
4742
4743 @item
4744 Unexpected initialization when address clause present
4745
4746 @item
4747 Bad alignment for address clause
4748
4749 @item
4750 Useless type conversions
4751
4752 @item
4753 Redundant assignment statements and other redundant constructs
4754
4755 @item
4756 Useless exception handlers
4757
4758 @item
4759 Accidental hiding of name by child unit
4760
4761 @item
4762 Access before elaboration detected at compile time
4763
4764 @item
4765 A range in a @code{for} loop that is known to be null or might be null
4766
4767 @end itemize
4768
4769 @noindent
4770 The following section lists compiler switches that are available
4771 to control the handling of warning messages. It is also possible
4772 to exercise much finer control over what warnings are issued and
4773 suppressed using the GNAT pragma Warnings, @xref{Pragma Warnings,,,
4774 gnat_rm, GNAT Reference manual}.
4775
4776 @table @option
4777 @c !sort!
4778 @item -gnatwa
4779 @emph{Activate all optional errors.}
4780 @cindex @option{-gnatwa} (@command{gcc})
4781 This switch activates most optional warning messages, see remaining list
4782 in this section for details on optional warning messages that can be
4783 individually controlled. The warnings that are not turned on by this
4784 switch are
4785 @option{-gnatwd} (implicit dereferencing),
4786 @option{-gnatwh} (hiding),
4787 @option{-gnatwl} (elaboration warnings),
4788 @option{-gnatw.o} (warn on values set by out parameters ignored)
4789 and @option{-gnatwt} (tracking of deleted conditional code).
4790 All other optional warnings are turned on.
4791
4792 @item -gnatwA
4793 @emph{Suppress all optional errors.}
4794 @cindex @option{-gnatwA} (@command{gcc})
4795 This switch suppresses all optional warning messages, see remaining list
4796 in this section for details on optional warning messages that can be
4797 individually controlled.
4798
4799 @item -gnatw.a
4800 @emph{Activate warnings on failing assertions.}
4801 @cindex @option{-gnatw.a} (@command{gcc})
4802 @cindex Assert failures
4803 This switch activates warnings for assertions where the compiler can tell at
4804 compile time that the assertion will fail. Note that this warning is given
4805 even if assertions are disabled. The default is that such warnings are
4806 generated.
4807
4808 @item -gnatw.A
4809 @emph{Suppress warnings on failing assertions.}
4810 @cindex @option{-gnatw.A} (@command{gcc})
4811 @cindex Assert failures
4812 This switch suppresses warnings for assertions where the compiler can tell at
4813 compile time that the assertion will fail.
4814
4815 @item -gnatwb
4816 @emph{Activate warnings on bad fixed values.}
4817 @cindex @option{-gnatwb} (@command{gcc})
4818 @cindex Bad fixed values
4819 @cindex Fixed-point Small value
4820 @cindex Small value
4821 This switch activates warnings for static fixed-point expressions whose
4822 value is not an exact multiple of Small. Such values are implementation
4823 dependent, since an implementation is free to choose either of the multiples
4824 that surround the value. GNAT always chooses the closer one, but this is not
4825 required behavior, and it is better to specify a value that is an exact
4826 multiple, ensuring predictable execution. The default is that such warnings
4827 are not generated.
4828
4829 @item -gnatwB
4830 @emph{Suppress warnings on bad fixed values.}
4831 @cindex @option{-gnatwB} (@command{gcc})
4832 This switch suppresses warnings for static fixed-point expressions whose
4833 value is not an exact multiple of Small.
4834
4835 @item -gnatwc
4836 @emph{Activate warnings on conditionals.}
4837 @cindex @option{-gnatwc} (@command{gcc})
4838 @cindex Conditionals, constant
4839 This switch activates warnings for conditional expressions used in
4840 tests that are known to be True or False at compile time. The default
4841 is that such warnings are not generated.
4842 Note that this warning does
4843 not get issued for the use of boolean variables or constants whose
4844 values are known at compile time, since this is a standard technique
4845 for conditional compilation in Ada, and this would generate too many
4846 false positive warnings.
4847
4848 This warning option also activates a special test for comparisons using
4849 the operators ``>='' and`` <=''.
4850 If the compiler can tell that only the equality condition is possible,
4851 then it will warn that the ``>'' or ``<'' part of the test
4852 is useless and that the operator could be replaced by ``=''.
4853 An example would be comparing a @code{Natural} variable <= 0.
4854
4855 This warning option also generates warnings if
4856 one or both tests is optimized away in a membership test for integer
4857 values if the result can be determined at compile time. Range tests on
4858 enumeration types are not included, since it is common for such tests
4859 to include an end point.
4860
4861 This warning can also be turned on using @option{-gnatwa}.
4862
4863 @item -gnatwC
4864 @emph{Suppress warnings on conditionals.}
4865 @cindex @option{-gnatwC} (@command{gcc})
4866 This switch suppresses warnings for conditional expressions used in
4867 tests that are known to be True or False at compile time.
4868
4869 @item -gnatw.c
4870 @emph{Activate warnings on missing component clauses.}
4871 @cindex @option{-gnatw.c} (@command{gcc})
4872 @cindex Component clause, missing
4873 This switch activates warnings for record components where a record
4874 representation clause is present and has component clauses for the
4875 majority, but not all, of the components. A warning is given for each
4876 component for which no component clause is present.
4877
4878 This warning can also be turned on using @option{-gnatwa}.
4879
4880 @item -gnatw.C
4881 @emph{Suppress warnings on missing component clauses.}
4882 @cindex @option{-gnatwC} (@command{gcc})
4883 This switch suppresses warnings for record components that are
4884 missing a component clause in the situation described above.
4885
4886 @item -gnatwd
4887 @emph{Activate warnings on implicit dereferencing.}
4888 @cindex @option{-gnatwd} (@command{gcc})
4889 If this switch is set, then the use of a prefix of an access type
4890 in an indexed component, slice, or selected component without an
4891 explicit @code{.all} will generate a warning. With this warning
4892 enabled, access checks occur only at points where an explicit
4893 @code{.all} appears in the source code (assuming no warnings are
4894 generated as a result of this switch). The default is that such
4895 warnings are not generated.
4896 Note that @option{-gnatwa} does not affect the setting of
4897 this warning option.
4898
4899 @item -gnatwD
4900 @emph{Suppress warnings on implicit dereferencing.}
4901 @cindex @option{-gnatwD} (@command{gcc})
4902 @cindex Implicit dereferencing
4903 @cindex Dereferencing, implicit
4904 This switch suppresses warnings for implicit dereferences in
4905 indexed components, slices, and selected components.
4906
4907 @item -gnatwe
4908 @emph{Treat warnings as errors.}
4909 @cindex @option{-gnatwe} (@command{gcc})
4910 @cindex Warnings, treat as error
4911 This switch causes warning messages to be treated as errors.
4912 The warning string still appears, but the warning messages are counted
4913 as errors, and prevent the generation of an object file.
4914
4915 @item -gnatw.e
4916 @emph{Activate every optional warning}
4917 @cindex @option{-gnatw.e} (@command{gcc})
4918 @cindex Warnings, activate every optional warning
4919 This switch activates all optional warnings, including those which
4920 are not activated by @code{-gnatwa}.
4921
4922 @item -gnatwf
4923 @emph{Activate warnings on unreferenced formals.}
4924 @cindex @option{-gnatwf} (@command{gcc})
4925 @cindex Formals, unreferenced
4926 This switch causes a warning to be generated if a formal parameter
4927 is not referenced in the body of the subprogram. This warning can
4928 also be turned on using @option{-gnatwa} or @option{-gnatwu}. The
4929 default is that these warnings are not generated.
4930
4931 @item -gnatwF
4932 @emph{Suppress warnings on unreferenced formals.}
4933 @cindex @option{-gnatwF} (@command{gcc})
4934 This switch suppresses warnings for unreferenced formal
4935 parameters. Note that the
4936 combination @option{-gnatwu} followed by @option{-gnatwF} has the
4937 effect of warning on unreferenced entities other than subprogram
4938 formals.
4939
4940 @item -gnatwg
4941 @emph{Activate warnings on unrecognized pragmas.}
4942 @cindex @option{-gnatwg} (@command{gcc})
4943 @cindex Pragmas, unrecognized
4944 This switch causes a warning to be generated if an unrecognized
4945 pragma is encountered. Apart from issuing this warning, the
4946 pragma is ignored and has no effect. This warning can
4947 also be turned on using @option{-gnatwa}. The default
4948 is that such warnings are issued (satisfying the Ada Reference
4949 Manual requirement that such warnings appear).
4950
4951 @item -gnatwG
4952 @emph{Suppress warnings on unrecognized pragmas.}
4953 @cindex @option{-gnatwG} (@command{gcc})
4954 This switch suppresses warnings for unrecognized pragmas.
4955
4956 @item -gnatwh
4957 @emph{Activate warnings on hiding.}
4958 @cindex @option{-gnatwh} (@command{gcc})
4959 @cindex Hiding of Declarations
4960 This switch activates warnings on hiding declarations.
4961 A declaration is considered hiding
4962 if it is for a non-overloadable entity, and it declares an entity with the
4963 same name as some other entity that is directly or use-visible. The default
4964 is that such warnings are not generated.
4965 Note that @option{-gnatwa} does not affect the setting of this warning option.
4966
4967 @item -gnatwH
4968 @emph{Suppress warnings on hiding.}
4969 @cindex @option{-gnatwH} (@command{gcc})
4970 This switch suppresses warnings on hiding declarations.
4971
4972 @item -gnatwi
4973 @emph{Activate warnings on implementation units.}
4974 @cindex @option{-gnatwi} (@command{gcc})
4975 This switch activates warnings for a @code{with} of an internal GNAT
4976 implementation unit, defined as any unit from the @code{Ada},
4977 @code{Interfaces}, @code{GNAT},
4978 ^^@code{DEC},^ or @code{System}
4979 hierarchies that is not
4980 documented in either the Ada Reference Manual or the GNAT
4981 Programmer's Reference Manual. Such units are intended only
4982 for internal implementation purposes and should not be @code{with}'ed
4983 by user programs. The default is that such warnings are generated
4984 This warning can also be turned on using @option{-gnatwa}.
4985
4986 @item -gnatwI
4987 @emph{Disable warnings on implementation units.}
4988 @cindex @option{-gnatwI} (@command{gcc})
4989 This switch disables warnings for a @code{with} of an internal GNAT
4990 implementation unit.
4991
4992 @item -gnatwj
4993 @emph{Activate warnings on obsolescent features (Annex J).}
4994 @cindex @option{-gnatwj} (@command{gcc})
4995 @cindex Features, obsolescent
4996 @cindex Obsolescent features
4997 If this warning option is activated, then warnings are generated for
4998 calls to subprograms marked with @code{pragma Obsolescent} and
4999 for use of features in Annex J of the Ada Reference Manual. In the
5000 case of Annex J, not all features are flagged. In particular use
5001 of the renamed packages (like @code{Text_IO}) and use of package
5002 @code{ASCII} are not flagged, since these are very common and
5003 would generate many annoying positive warnings. The default is that
5004 such warnings are not generated. This warning is also turned on by
5005 the use of @option{-gnatwa}.
5006
5007 In addition to the above cases, warnings are also generated for
5008 GNAT features that have been provided in past versions but which
5009 have been superseded (typically by features in the new Ada standard).
5010 For example, @code{pragma Ravenscar} will be flagged since its
5011 function is replaced by @code{pragma Profile(Ravenscar)}.
5012
5013 Note that this warning option functions differently from the
5014 restriction @code{No_Obsolescent_Features} in two respects.
5015 First, the restriction applies only to annex J features.
5016 Second, the restriction does flag uses of package @code{ASCII}.
5017
5018 @item -gnatwJ
5019 @emph{Suppress warnings on obsolescent features (Annex J).}
5020 @cindex @option{-gnatwJ} (@command{gcc})
5021 This switch disables warnings on use of obsolescent features.
5022
5023 @item -gnatwk
5024 @emph{Activate warnings on variables that could be constants.}
5025 @cindex @option{-gnatwk} (@command{gcc})
5026 This switch activates warnings for variables that are initialized but
5027 never modified, and then could be declared constants. The default is that
5028 such warnings are not given.
5029 This warning can also be turned on using @option{-gnatwa}.
5030
5031 @item -gnatwK
5032 @emph{Suppress warnings on variables that could be constants.}
5033 @cindex @option{-gnatwK} (@command{gcc})
5034 This switch disables warnings on variables that could be declared constants.
5035
5036 @item -gnatwl
5037 @emph{Activate warnings for elaboration pragmas.}
5038 @cindex @option{-gnatwl} (@command{gcc})
5039 @cindex Elaboration, warnings
5040 This switch activates warnings on missing
5041 @code{Elaborate_All} and @code{Elaborate} pragmas.
5042 See the section in this guide on elaboration checking for details on
5043 when such pragmas should be used. In dynamic elaboration mode, this switch
5044 generations warnings about the need to add elaboration pragmas. Note however,
5045 that if you blindly follow these warnings, and add @code{Elaborate_All}
5046 warnings wherever they are recommended, you basically end up with the
5047 equivalent of the static elaboration model, which may not be what you want for
5048 legacy code for which the static model does not work.
5049
5050 For the static model, the messages generated are labeled "info:" (for
5051 information messages). They are not warnings to add elaboration pragmas,
5052 merely informational messages showing what implicit elaboration pragmas
5053 have been added, for use in analyzing elaboration circularity problems.
5054
5055 Warnings are also generated if you
5056 are using the static mode of elaboration, and a @code{pragma Elaborate}
5057 is encountered. The default is that such warnings
5058 are not generated.
5059 This warning is not automatically turned on by the use of @option{-gnatwa}.
5060
5061 @item -gnatwL
5062 @emph{Suppress warnings for elaboration pragmas.}
5063 @cindex @option{-gnatwL} (@command{gcc})
5064 This switch suppresses warnings on missing Elaborate and Elaborate_All pragmas.
5065 See the section in this guide on elaboration checking for details on
5066 when such pragmas should be used.
5067
5068 @item -gnatwm
5069 @emph{Activate warnings on modified but unreferenced variables.}
5070 @cindex @option{-gnatwm} (@command{gcc})
5071 This switch activates warnings for variables that are assigned (using
5072 an initialization value or with one or more assignment statements) but
5073 whose value is never read. The warning is suppressed for volatile
5074 variables and also for variables that are renamings of other variables
5075 or for which an address clause is given.
5076 This warning can also be turned on using @option{-gnatwa}.
5077 The default is that these warnings are not given.
5078
5079 @item -gnatwM
5080 @emph{Disable warnings on modified but unreferenced variables.}
5081 @cindex @option{-gnatwM} (@command{gcc})
5082 This switch disables warnings for variables that are assigned or
5083 initialized, but never read.
5084
5085 @item -gnatwn
5086 @emph{Set normal warnings mode.}
5087 @cindex @option{-gnatwn} (@command{gcc})
5088 This switch sets normal warning mode, in which enabled warnings are
5089 issued and treated as warnings rather than errors. This is the default
5090 mode. the switch @option{-gnatwn} can be used to cancel the effect of
5091 an explicit @option{-gnatws} or
5092 @option{-gnatwe}. It also cancels the effect of the
5093 implicit @option{-gnatwe} that is activated by the
5094 use of @option{-gnatg}.
5095
5096 @item -gnatwo
5097 @emph{Activate warnings on address clause overlays.}
5098 @cindex @option{-gnatwo} (@command{gcc})
5099 @cindex Address Clauses, warnings
5100 This switch activates warnings for possibly unintended initialization
5101 effects of defining address clauses that cause one variable to overlap
5102 another. The default is that such warnings are generated.
5103 This warning can also be turned on using @option{-gnatwa}.
5104
5105 @item -gnatwO
5106 @emph{Suppress warnings on address clause overlays.}
5107 @cindex @option{-gnatwO} (@command{gcc})
5108 This switch suppresses warnings on possibly unintended initialization
5109 effects of defining address clauses that cause one variable to overlap
5110 another.
5111
5112 @item -gnatw.o
5113 @emph{Activate warnings on modified but unreferenced out parameters.}
5114 @cindex @option{-gnatw.o} (@command{gcc})
5115 This switch activates warnings for variables that are modified by using
5116 them as actuals for a call to a procedure with an out mode formal, where
5117 the resulting assigned value is never read. It is applicable in the case
5118 where there is more than one out mode formal. If there is only one out
5119 mode formal, the warning is issued by default (controlled by -gnatwu).
5120 The warning is suppressed for volatile
5121 variables and also for variables that are renamings of other variables
5122 or for which an address clause is given.
5123 The default is that these warnings are not given. Note that this warning
5124 is not included in -gnatwa, it must be activated explicitly.
5125
5126 @item -gnatw.O
5127 @emph{Disable warnings on modified but unreferenced out parameters.}
5128 @cindex @option{-gnatw.O} (@command{gcc})
5129 This switch suppresses warnings for variables that are modified by using
5130 them as actuals for a call to a procedure with an out mode formal, where
5131 the resulting assigned value is never read.
5132
5133 @item -gnatwp
5134 @emph{Activate warnings on ineffective pragma Inlines.}
5135 @cindex @option{-gnatwp} (@command{gcc})
5136 @cindex Inlining, warnings
5137 This switch activates warnings for failure of front end inlining
5138 (activated by @option{-gnatN}) to inline a particular call. There are
5139 many reasons for not being able to inline a call, including most
5140 commonly that the call is too complex to inline. The default is
5141 that such warnings are not given.
5142 This warning can also be turned on using @option{-gnatwa}.
5143 Warnings on ineffective inlining by the gcc back-end can be activated
5144 separately, using the gcc switch -Winline.
5145
5146 @item -gnatwP
5147 @emph{Suppress warnings on ineffective pragma Inlines.}
5148 @cindex @option{-gnatwP} (@command{gcc})
5149 This switch suppresses warnings on ineffective pragma Inlines. If the
5150 inlining mechanism cannot inline a call, it will simply ignore the
5151 request silently.
5152
5153 @item -gnatw.p
5154 @emph{Activate warnings on parameter ordering.}
5155 @cindex @option{-gnatw.p} (@command{gcc})
5156 @cindex Parameter order, warnings
5157 This switch activates warnings for cases of suspicious parameter
5158 ordering when the list of arguments are all simple identifiers that
5159 match the names of the formals, but are in a different order. The
5160 warning is suppressed if any use of named parameter notation is used,
5161 so this is the appropriate way to suppress a false positive (and
5162 serves to emphasize that the "misordering" is deliberate). The
5163 default is
5164 that such warnings are not given.
5165 This warning can also be turned on using @option{-gnatwa}.
5166
5167 @item -gnatw.P
5168 @emph{Suppress warnings on parameter ordering.}
5169 @cindex @option{-gnatw.P} (@command{gcc})
5170 This switch suppresses warnings on cases of suspicious parameter
5171 ordering.
5172
5173 @item -gnatwq
5174 @emph{Activate warnings on questionable missing parentheses.}
5175 @cindex @option{-gnatwq} (@command{gcc})
5176 @cindex Parentheses, warnings
5177 This switch activates warnings for cases where parentheses are not used and
5178 the result is potential ambiguity from a readers point of view. For example
5179 (not a > b) when a and b are modular means ((not a) > b) and very likely the
5180 programmer intended (not (a > b)). Similarly (-x mod 5) means (-(x mod 5)) and
5181 quite likely ((-x) mod 5) was intended. In such situations it seems best to
5182 follow the rule of always parenthesizing to make the association clear, and
5183 this warning switch warns if such parentheses are not present. The default
5184 is that these warnings are given.
5185 This warning can also be turned on using @option{-gnatwa}.
5186
5187 @item -gnatwQ
5188 @emph{Suppress warnings on questionable missing parentheses.}
5189 @cindex @option{-gnatwQ} (@command{gcc})
5190 This switch suppresses warnings for cases where the association is not
5191 clear and the use of parentheses is preferred.
5192
5193 @item -gnatwr
5194 @emph{Activate warnings on redundant constructs.}
5195 @cindex @option{-gnatwr} (@command{gcc})
5196 This switch activates warnings for redundant constructs. The following
5197 is the current list of constructs regarded as redundant:
5198
5199 @itemize @bullet
5200 @item
5201 Assignment of an item to itself.
5202 @item
5203 Type conversion that converts an expression to its own type.
5204 @item
5205 Use of the attribute @code{Base} where @code{typ'Base} is the same
5206 as @code{typ}.
5207 @item
5208 Use of pragma @code{Pack} when all components are placed by a record
5209 representation clause.
5210 @item
5211 Exception handler containing only a reraise statement (raise with no
5212 operand) which has no effect.
5213 @item
5214 Use of the operator abs on an operand that is known at compile time
5215 to be non-negative
5216 @item
5217 Comparison of boolean expressions to an explicit True value.
5218 @end itemize
5219
5220 This warning can also be turned on using @option{-gnatwa}.
5221 The default is that warnings for redundant constructs are not given.
5222
5223 @item -gnatwR
5224 @emph{Suppress warnings on redundant constructs.}
5225 @cindex @option{-gnatwR} (@command{gcc})
5226 This switch suppresses warnings for redundant constructs.
5227
5228 @item -gnatws
5229 @emph{Suppress all warnings.}
5230 @cindex @option{-gnatws} (@command{gcc})
5231 This switch completely suppresses the
5232 output of all warning messages from the GNAT front end.
5233 Note that it does not suppress warnings from the @command{gcc} back end.
5234 To suppress these back end warnings as well, use the switch @option{-w}
5235 in addition to @option{-gnatws}.
5236
5237 @item -gnatwt
5238 @emph{Activate warnings for tracking of deleted conditional code.}
5239 @cindex @option{-gnatwt} (@command{gcc})
5240 @cindex Deactivated code, warnings
5241 @cindex Deleted code, warnings
5242 This switch activates warnings for tracking of code in conditionals (IF and
5243 CASE statements) that is detected to be dead code which cannot be executed, and
5244 which is removed by the front end. This warning is off by default, and is not
5245 turned on by @option{-gnatwa}, it has to be turned on explicitly. This may be
5246 useful for detecting deactivated code in certified applications.
5247
5248 @item -gnatwT
5249 @emph{Suppress warnings for tracking of deleted conditional code.}
5250 @cindex @option{-gnatwT} (@command{gcc})
5251 This switch suppresses warnings for tracking of deleted conditional code.
5252
5253 @item -gnatwu
5254 @emph{Activate warnings on unused entities.}
5255 @cindex @option{-gnatwu} (@command{gcc})
5256 This switch activates warnings to be generated for entities that
5257 are declared but not referenced, and for units that are @code{with}'ed
5258 and not
5259 referenced. In the case of packages, a warning is also generated if
5260 no entities in the package are referenced. This means that if the package
5261 is referenced but the only references are in @code{use}
5262 clauses or @code{renames}
5263 declarations, a warning is still generated. A warning is also generated
5264 for a generic package that is @code{with}'ed but never instantiated.
5265 In the case where a package or subprogram body is compiled, and there
5266 is a @code{with} on the corresponding spec
5267 that is only referenced in the body,
5268 a warning is also generated, noting that the
5269 @code{with} can be moved to the body. The default is that
5270 such warnings are not generated.
5271 This switch also activates warnings on unreferenced formals
5272 (it includes the effect of @option{-gnatwf}).
5273 This warning can also be turned on using @option{-gnatwa}.
5274
5275 @item -gnatwU
5276 @emph{Suppress warnings on unused entities.}
5277 @cindex @option{-gnatwU} (@command{gcc})
5278 This switch suppresses warnings for unused entities and packages.
5279 It also turns off warnings on unreferenced formals (and thus includes
5280 the effect of @option{-gnatwF}).
5281
5282 @item -gnatwv
5283 @emph{Activate warnings on unassigned variables.}
5284 @cindex @option{-gnatwv} (@command{gcc})
5285 @cindex Unassigned variable warnings
5286 This switch activates warnings for access to variables which
5287 may not be properly initialized. The default is that
5288 such warnings are generated.
5289 This warning can also be turned on using @option{-gnatwa}.
5290
5291 @item -gnatwV
5292 @emph{Suppress warnings on unassigned variables.}
5293 @cindex @option{-gnatwV} (@command{gcc})
5294 This switch suppresses warnings for access to variables which
5295 may not be properly initialized.
5296 For variables of a composite type, the warning can also be suppressed in
5297 Ada 2005 by using a default initialization with a box. For example, if
5298 Table is an array of records whose components are only partially uninitialized,
5299 then the following code:
5300
5301 @smallexample @c ada
5302 Tab : Table := (others => <>);
5303 @end smallexample
5304
5305 will suppress warnings on subsequent statements that access components
5306 of variable Tab.
5307
5308 @item -gnatww
5309 @emph{Activate warnings on wrong low bound assumption.}
5310 @cindex @option{-gnatww} (@command{gcc})
5311 @cindex String indexing warnings
5312 This switch activates warnings for indexing an unconstrained string parameter
5313 with a literal or S'Length. This is a case where the code is assuming that the
5314 low bound is one, which is in general not true (for example when a slice is
5315 passed). The default is that such warnings are generated.
5316 This warning can also be turned on using @option{-gnatwa}.
5317
5318 @item -gnatwW
5319 @emph{Suppress warnings on wrong low bound assumption.}
5320 @cindex @option{-gnatwW} (@command{gcc})
5321 This switch suppresses warnings for indexing an unconstrained string parameter
5322 with a literal or S'Length. Note that this warning can also be suppressed
5323 in a particular case by adding an
5324 assertion that the lower bound is 1,
5325 as shown in the following example.
5326
5327 @smallexample @c ada
5328 procedure K (S : String) is
5329 pragma Assert (S'First = 1);
5330 @dots{}
5331 @end smallexample
5332
5333 @item -gnatw.w
5334 @emph{Activate warnings on unnecessary Warnings Off pragmas}
5335 @cindex @option{-gnatw.w} (@command{gcc})
5336 @cindex Warnings Off control
5337 This switch activates warnings for use of @code{pragma Warnings (Off, entity}
5338 where either the pragma is entirely useless (because it suppresses no
5339 warnings), or it could be replaced by @code{pragma Unreferenced} or
5340 @code{pragma Unmodified}.The default is that these warnings are not given.
5341 Note that this warning is not included in -gnatwa, it must be
5342 activated explicitly.
5343
5344 @item -gnatw.W
5345 @emph{Suppress warnings on unnecessary Warnings Off pragmas}
5346 @cindex @option{-gnatw.W} (@command{gcc})
5347 This switch suppresses warnings for use of @code{pragma Warnings (Off, entity}.
5348
5349 @item -gnatwx
5350 @emph{Activate warnings on Export/Import pragmas.}
5351 @cindex @option{-gnatwx} (@command{gcc})
5352 @cindex Export/Import pragma warnings
5353 This switch activates warnings on Export/Import pragmas when
5354 the compiler detects a possible conflict between the Ada and
5355 foreign language calling sequences. For example, the use of
5356 default parameters in a convention C procedure is dubious
5357 because the C compiler cannot supply the proper default, so
5358 a warning is issued. The default is that such warnings are
5359 generated.
5360 This warning can also be turned on using @option{-gnatwa}.
5361
5362 @item -gnatwX
5363 @emph{Suppress warnings on Export/Import pragmas.}
5364 @cindex @option{-gnatwX} (@command{gcc})
5365 This switch suppresses warnings on Export/Import pragmas.
5366 The sense of this is that you are telling the compiler that
5367 you know what you are doing in writing the pragma, and it
5368 should not complain at you.
5369
5370 @item -gnatw.x
5371 @emph{Activate warnings for No_Exception_Propagation mode.}
5372 @cindex @option{-gnatwm} (@command{gcc})
5373 This switch activates warnings for exception usage when pragma Restrictions
5374 (No_Exception_Propagation) is in effect. Warnings are given for implicit or
5375 explicit exception raises which are not covered by a local handler, and for
5376 exception handlers which do not cover a local raise. The default is that these
5377 warnings are not given.
5378
5379 @item -gnatw.X
5380 @emph{Disable warnings for No_Exception_Propagation mode.}
5381 This switch disables warnings for exception usage when pragma Restrictions
5382 (No_Exception_Propagation) is in effect.
5383
5384 @item -gnatwy
5385 @emph{Activate warnings for Ada 2005 compatibility issues.}
5386 @cindex @option{-gnatwy} (@command{gcc})
5387 @cindex Ada 2005 compatibility issues warnings
5388 For the most part Ada 2005 is upwards compatible with Ada 95,
5389 but there are some exceptions (for example the fact that
5390 @code{interface} is now a reserved word in Ada 2005). This
5391 switch activates several warnings to help in identifying
5392 and correcting such incompatibilities. The default is that
5393 these warnings are generated. Note that at one point Ada 2005
5394 was called Ada 0Y, hence the choice of character.
5395 This warning can also be turned on using @option{-gnatwa}.
5396
5397 @item -gnatwY
5398 @emph{Disable warnings for Ada 2005 compatibility issues.}
5399 @cindex @option{-gnatwY} (@command{gcc})
5400 @cindex Ada 2005 compatibility issues warnings
5401 This switch suppresses several warnings intended to help in identifying
5402 incompatibilities between Ada 95 and Ada 2005.
5403
5404 @item -gnatwz
5405 @emph{Activate warnings on unchecked conversions.}
5406 @cindex @option{-gnatwz} (@command{gcc})
5407 @cindex Unchecked_Conversion warnings
5408 This switch activates warnings for unchecked conversions
5409 where the types are known at compile time to have different
5410 sizes. The default
5411 is that such warnings are generated. Warnings are also
5412 generated for subprogram pointers with different conventions,
5413 and, on VMS only, for data pointers with different conventions.
5414 This warning can also be turned on using @option{-gnatwa}.
5415
5416 @item -gnatwZ
5417 @emph{Suppress warnings on unchecked conversions.}
5418 @cindex @option{-gnatwZ} (@command{gcc})
5419 This switch suppresses warnings for unchecked conversions
5420 where the types are known at compile time to have different
5421 sizes or conventions.
5422
5423 @item ^-Wunused^WARNINGS=UNUSED^
5424 @cindex @option{-Wunused}
5425 The warnings controlled by the @option{-gnatw} switch are generated by
5426 the front end of the compiler. The @option{GCC} back end can provide
5427 additional warnings and they are controlled by the @option{-W} switch.
5428 For example, @option{^-Wunused^WARNINGS=UNUSED^} activates back end
5429 warnings for entities that are declared but not referenced.
5430
5431 @item ^-Wuninitialized^WARNINGS=UNINITIALIZED^
5432 @cindex @option{-Wuninitialized}
5433 Similarly, @option{^-Wuninitialized^WARNINGS=UNINITIALIZED^} activates
5434 the back end warning for uninitialized variables. This switch must be
5435 used in conjunction with an optimization level greater than zero.
5436
5437 @item ^-Wall^/ALL_BACK_END_WARNINGS^
5438 @cindex @option{-Wall}
5439 This switch enables all the above warnings from the @option{GCC} back end.
5440 The code generator detects a number of warning situations that are missed
5441 by the @option{GNAT} front end, and this switch can be used to activate them.
5442 The use of this switch also sets the default front end warning mode to
5443 @option{-gnatwa}, that is, most front end warnings activated as well.
5444
5445 @item ^-w^/NO_BACK_END_WARNINGS^
5446 @cindex @option{-w}
5447 Conversely, this switch suppresses warnings from the @option{GCC} back end.
5448 The use of this switch also sets the default front end warning mode to
5449 @option{-gnatws}, that is, front end warnings suppressed as well.
5450
5451 @end table
5452
5453 @noindent
5454 @ifclear vms
5455 A string of warning parameters can be used in the same parameter. For example:
5456
5457 @smallexample
5458 -gnatwaLe
5459 @end smallexample
5460
5461 @noindent
5462 will turn on all optional warnings except for elaboration pragma warnings,
5463 and also specify that warnings should be treated as errors.
5464 @end ifclear
5465 When no switch @option{^-gnatw^/WARNINGS^} is used, this is equivalent to:
5466
5467 @table @option
5468 @c !sort!
5469 @item -gnatwC
5470 @item -gnatwD
5471 @item -gnatwF
5472 @item -gnatwg
5473 @item -gnatwH
5474 @item -gnatwi
5475 @item -gnatwJ
5476 @item -gnatwK
5477 @item -gnatwL
5478 @item -gnatwM
5479 @item -gnatwn
5480 @item -gnatwo
5481 @item -gnatwP
5482 @item -gnatwR
5483 @item -gnatwU
5484 @item -gnatwv
5485 @item -gnatwz
5486 @item -gnatwx
5487
5488 @end table
5489
5490 @node Debugging and Assertion Control
5491 @subsection Debugging and Assertion Control
5492
5493 @table @option
5494 @item -gnata
5495 @cindex @option{-gnata} (@command{gcc})
5496 @findex Assert
5497 @findex Debug
5498 @cindex Assertions
5499
5500 @noindent
5501 The pragmas @code{Assert} and @code{Debug} normally have no effect and
5502 are ignored. This switch, where @samp{a} stands for assert, causes
5503 @code{Assert} and @code{Debug} pragmas to be activated.
5504
5505 The pragmas have the form:
5506
5507 @smallexample
5508 @cartouche
5509 @b{pragma} Assert (@var{Boolean-expression} @r{[},
5510 @var{static-string-expression}@r{]})
5511 @b{pragma} Debug (@var{procedure call})
5512 @end cartouche
5513 @end smallexample
5514
5515 @noindent
5516 The @code{Assert} pragma causes @var{Boolean-expression} to be tested.
5517 If the result is @code{True}, the pragma has no effect (other than
5518 possible side effects from evaluating the expression). If the result is
5519 @code{False}, the exception @code{Assert_Failure} declared in the package
5520 @code{System.Assertions} is
5521 raised (passing @var{static-string-expression}, if present, as the
5522 message associated with the exception). If no string expression is
5523 given the default is a string giving the file name and line number
5524 of the pragma.
5525
5526 The @code{Debug} pragma causes @var{procedure} to be called. Note that
5527 @code{pragma Debug} may appear within a declaration sequence, allowing
5528 debugging procedures to be called between declarations.
5529
5530 @ifset vms
5531 @item /DEBUG@r{[}=debug-level@r{]}
5532 @itemx /NODEBUG
5533 Specifies how much debugging information is to be included in
5534 the resulting object file where 'debug-level' is one of the following:
5535 @table @code
5536 @item TRACEBACK
5537 Include both debugger symbol records and traceback
5538 the object file.
5539 This is the default setting.
5540 @item ALL
5541 Include both debugger symbol records and traceback in
5542 object file.
5543 @item NONE
5544 Excludes both debugger symbol records and traceback
5545 the object file. Same as /NODEBUG.
5546 @item SYMBOLS
5547 Includes only debugger symbol records in the object
5548 file. Note that this doesn't include traceback information.
5549 @end table
5550 @end ifset
5551 @end table
5552
5553 @node Validity Checking
5554 @subsection Validity Checking
5555 @findex Validity Checking
5556
5557 @noindent
5558 The Ada Reference Manual has specific requirements for checking
5559 for invalid values. In particular, RM 13.9.1 requires that the
5560 evaluation of invalid values (for example from unchecked conversions),
5561 not result in erroneous execution. In GNAT, the result of such an
5562 evaluation in normal default mode is to either use the value
5563 unmodified, or to raise Constraint_Error in those cases where use
5564 of the unmodified value would cause erroneous execution. The cases
5565 where unmodified values might lead to erroneous execution are case
5566 statements (where a wild jump might result from an invalid value),
5567 and subscripts on the left hand side (where memory corruption could
5568 occur as a result of an invalid value).
5569
5570 The @option{-gnatV^@var{x}^^} switch allows more control over the validity
5571 checking mode.
5572 @ifclear vms
5573 The @code{x} argument is a string of letters that
5574 indicate validity checks that are performed or not performed in addition
5575 to the default checks described above.
5576 @end ifclear
5577 @ifset vms
5578 The options allowed for this qualifier
5579 indicate validity checks that are performed or not performed in addition
5580 to the default checks described above.
5581 @end ifset
5582
5583 @table @option
5584 @c !sort!
5585 @item -gnatVa
5586 @emph{All validity checks.}
5587 @cindex @option{-gnatVa} (@command{gcc})
5588 All validity checks are turned on.
5589 @ifclear vms
5590 That is, @option{-gnatVa} is
5591 equivalent to @option{gnatVcdfimorst}.
5592 @end ifclear
5593
5594 @item -gnatVc
5595 @emph{Validity checks for copies.}
5596 @cindex @option{-gnatVc} (@command{gcc})
5597 The right hand side of assignments, and the initializing values of
5598 object declarations are validity checked.
5599
5600 @item -gnatVd
5601 @emph{Default (RM) validity checks.}
5602 @cindex @option{-gnatVd} (@command{gcc})
5603 Some validity checks are done by default following normal Ada semantics
5604 (RM 13.9.1 (9-11)).
5605 A check is done in case statements that the expression is within the range
5606 of the subtype. If it is not, Constraint_Error is raised.
5607 For assignments to array components, a check is done that the expression used
5608 as index is within the range. If it is not, Constraint_Error is raised.
5609 Both these validity checks may be turned off using switch @option{-gnatVD}.
5610 They are turned on by default. If @option{-gnatVD} is specified, a subsequent
5611 switch @option{-gnatVd} will leave the checks turned on.
5612 Switch @option{-gnatVD} should be used only if you are sure that all such
5613 expressions have valid values. If you use this switch and invalid values
5614 are present, then the program is erroneous, and wild jumps or memory
5615 overwriting may occur.
5616
5617 @item -gnatVe
5618 @emph{Validity checks for elementary components.}
5619 @cindex @option{-gnatVe} (@command{gcc})
5620 In the absence of this switch, assignments to record or array components are
5621 not validity checked, even if validity checks for assignments generally
5622 (@option{-gnatVc}) are turned on. In Ada, assignment of composite values do not
5623 require valid data, but assignment of individual components does. So for
5624 example, there is a difference between copying the elements of an array with a
5625 slice assignment, compared to assigning element by element in a loop. This
5626 switch allows you to turn off validity checking for components, even when they
5627 are assigned component by component.
5628
5629 @item -gnatVf
5630 @emph{Validity checks for floating-point values.}
5631 @cindex @option{-gnatVf} (@command{gcc})
5632 In the absence of this switch, validity checking occurs only for discrete
5633 values. If @option{-gnatVf} is specified, then validity checking also applies
5634 for floating-point values, and NaNs and infinities are considered invalid,
5635 as well as out of range values for constrained types. Note that this means
5636 that standard IEEE infinity mode is not allowed. The exact contexts
5637 in which floating-point values are checked depends on the setting of other
5638 options. For example,
5639 @option{^-gnatVif^VALIDITY_CHECKING=(IN_PARAMS,FLOATS)^} or
5640 @option{^-gnatVfi^VALIDITY_CHECKING=(FLOATS,IN_PARAMS)^}
5641 (the order does not matter) specifies that floating-point parameters of mode
5642 @code{in} should be validity checked.
5643
5644 @item -gnatVi
5645 @emph{Validity checks for @code{in} mode parameters}
5646 @cindex @option{-gnatVi} (@command{gcc})
5647 Arguments for parameters of mode @code{in} are validity checked in function
5648 and procedure calls at the point of call.
5649
5650 @item -gnatVm
5651 @emph{Validity checks for @code{in out} mode parameters.}
5652 @cindex @option{-gnatVm} (@command{gcc})
5653 Arguments for parameters of mode @code{in out} are validity checked in
5654 procedure calls at the point of call. The @code{'m'} here stands for
5655 modify, since this concerns parameters that can be modified by the call.
5656 Note that there is no specific option to test @code{out} parameters,
5657 but any reference within the subprogram will be tested in the usual
5658 manner, and if an invalid value is copied back, any reference to it
5659 will be subject to validity checking.
5660
5661 @item -gnatVn
5662 @emph{No validity checks.}
5663 @cindex @option{-gnatVn} (@command{gcc})
5664 This switch turns off all validity checking, including the default checking
5665 for case statements and left hand side subscripts. Note that the use of
5666 the switch @option{-gnatp} suppresses all run-time checks, including
5667 validity checks, and thus implies @option{-gnatVn}. When this switch
5668 is used, it cancels any other @option{-gnatV} previously issued.
5669
5670 @item -gnatVo
5671 @emph{Validity checks for operator and attribute operands.}
5672 @cindex @option{-gnatVo} (@command{gcc})
5673 Arguments for predefined operators and attributes are validity checked.
5674 This includes all operators in package @code{Standard},
5675 the shift operators defined as intrinsic in package @code{Interfaces}
5676 and operands for attributes such as @code{Pos}. Checks are also made
5677 on individual component values for composite comparisons, and on the
5678 expressions in type conversions and qualified expressions. Checks are
5679 also made on explicit ranges using @samp{..} (e.g.@: slices, loops etc).
5680
5681 @item -gnatVp
5682 @emph{Validity checks for parameters.}
5683 @cindex @option{-gnatVp} (@command{gcc})
5684 This controls the treatment of parameters within a subprogram (as opposed
5685 to @option{-gnatVi} and @option{-gnatVm} which control validity testing
5686 of parameters on a call. If either of these call options is used, then
5687 normally an assumption is made within a subprogram that the input arguments
5688 have been validity checking at the point of call, and do not need checking
5689 again within a subprogram). If @option{-gnatVp} is set, then this assumption
5690 is not made, and parameters are not assumed to be valid, so their validity
5691 will be checked (or rechecked) within the subprogram.
5692
5693 @item -gnatVr
5694 @emph{Validity checks for function returns.}
5695 @cindex @option{-gnatVr} (@command{gcc})
5696 The expression in @code{return} statements in functions is validity
5697 checked.
5698
5699 @item -gnatVs
5700 @emph{Validity checks for subscripts.}
5701 @cindex @option{-gnatVs} (@command{gcc})
5702 All subscripts expressions are checked for validity, whether they appear
5703 on the right side or left side (in default mode only left side subscripts
5704 are validity checked).
5705
5706 @item -gnatVt
5707 @emph{Validity checks for tests.}
5708 @cindex @option{-gnatVt} (@command{gcc})
5709 Expressions used as conditions in @code{if}, @code{while} or @code{exit}
5710 statements are checked, as well as guard expressions in entry calls.
5711
5712 @end table
5713
5714 @noindent
5715 The @option{-gnatV} switch may be followed by
5716 ^a string of letters^a list of options^
5717 to turn on a series of validity checking options.
5718 For example,
5719 @option{^-gnatVcr^/VALIDITY_CHECKING=(COPIES, RETURNS)^}
5720 specifies that in addition to the default validity checking, copies and
5721 function return expressions are to be validity checked.
5722 In order to make it easier
5723 to specify the desired combination of effects,
5724 @ifclear vms
5725 the upper case letters @code{CDFIMORST} may
5726 be used to turn off the corresponding lower case option.
5727 @end ifclear
5728 @ifset vms
5729 the prefix @code{NO} on an option turns off the corresponding validity
5730 checking:
5731 @itemize @bullet
5732 @item @code{NOCOPIES}
5733 @item @code{NODEFAULT}
5734 @item @code{NOFLOATS}
5735 @item @code{NOIN_PARAMS}
5736 @item @code{NOMOD_PARAMS}
5737 @item @code{NOOPERANDS}
5738 @item @code{NORETURNS}
5739 @item @code{NOSUBSCRIPTS}
5740 @item @code{NOTESTS}
5741 @end itemize
5742 @end ifset
5743 Thus
5744 @option{^-gnatVaM^/VALIDITY_CHECKING=(ALL, NOMOD_PARAMS)^}
5745 turns on all validity checking options except for
5746 checking of @code{@b{in out}} procedure arguments.
5747
5748 The specification of additional validity checking generates extra code (and
5749 in the case of @option{-gnatVa} the code expansion can be substantial).
5750 However, these additional checks can be very useful in detecting
5751 uninitialized variables, incorrect use of unchecked conversion, and other
5752 errors leading to invalid values. The use of pragma @code{Initialize_Scalars}
5753 is useful in conjunction with the extra validity checking, since this
5754 ensures that wherever possible uninitialized variables have invalid values.
5755
5756 See also the pragma @code{Validity_Checks} which allows modification of
5757 the validity checking mode at the program source level, and also allows for
5758 temporary disabling of validity checks.
5759
5760 @node Style Checking
5761 @subsection Style Checking
5762 @findex Style checking
5763
5764 @noindent
5765 The @option{-gnaty^x^(option,option,@dots{})^} switch
5766 @cindex @option{-gnaty} (@command{gcc})
5767 causes the compiler to
5768 enforce specified style rules. A limited set of style rules has been used
5769 in writing the GNAT sources themselves. This switch allows user programs
5770 to activate all or some of these checks. If the source program fails a
5771 specified style check, an appropriate warning message is given, preceded by
5772 the character sequence ``(style)''.
5773 @ifset vms
5774 @code{(option,option,@dots{})} is a sequence of keywords
5775 @end ifset
5776 @ifclear vms
5777 The string @var{x} is a sequence of letters or digits
5778 @end ifclear
5779 indicating the particular style
5780 checks to be performed. The following checks are defined:
5781
5782 @table @option
5783 @c !sort!
5784 @item 0-9
5785 @emph{Specify indentation level.}
5786 If a digit from 1-9 appears
5787 ^in the string after @option{-gnaty}^as an option for /STYLE_CHECKS^
5788 then proper indentation is checked, with the digit indicating the
5789 indentation level required. A value of zero turns off this style check.
5790 The general style of required indentation is as specified by
5791 the examples in the Ada Reference Manual. Full line comments must be
5792 aligned with the @code{--} starting on a column that is a multiple of
5793 the alignment level, or they may be aligned the same way as the following
5794 non-blank line (this is useful when full line comments appear in the middle
5795 of a statement.
5796
5797 @item ^a^ATTRIBUTE^
5798 @emph{Check attribute casing.}
5799 Attribute names, including the case of keywords such as @code{digits}
5800 used as attributes names, must be written in mixed case, that is, the
5801 initial letter and any letter following an underscore must be uppercase.
5802 All other letters must be lowercase.
5803
5804 @item ^A^ARRAY_INDEXES^
5805 @emph{Use of array index numbers in array attributes.}
5806 When using the array attributes First, Last, Range,
5807 or Length, the index number must be omitted for one-dimensional arrays
5808 and is required for multi-dimensional arrays.
5809
5810 @item ^b^BLANKS^
5811 @emph{Blanks not allowed at statement end.}
5812 Trailing blanks are not allowed at the end of statements. The purpose of this
5813 rule, together with h (no horizontal tabs), is to enforce a canonical format
5814 for the use of blanks to separate source tokens.
5815
5816 @item ^c^COMMENTS^
5817 @emph{Check comments.}
5818 Comments must meet the following set of rules:
5819
5820 @itemize @bullet
5821
5822 @item
5823 The ``@code{--}'' that starts the column must either start in column one,
5824 or else at least one blank must precede this sequence.
5825
5826 @item
5827 Comments that follow other tokens on a line must have at least one blank
5828 following the ``@code{--}'' at the start of the comment.
5829
5830 @item
5831 Full line comments must have two blanks following the ``@code{--}'' that
5832 starts the comment, with the following exceptions.
5833
5834 @item
5835 A line consisting only of the ``@code{--}'' characters, possibly preceded
5836 by blanks is permitted.
5837
5838 @item
5839 A comment starting with ``@code{--x}'' where @code{x} is a special character
5840 is permitted.
5841 This allows proper processing of the output generated by specialized tools
5842 including @command{gnatprep} (where ``@code{--!}'' is used) and the SPARK
5843 annotation
5844 language (where ``@code{--#}'' is used). For the purposes of this rule, a
5845 special character is defined as being in one of the ASCII ranges
5846 @code{16#21#@dots{}16#2F#} or @code{16#3A#@dots{}16#3F#}.
5847 Note that this usage is not permitted
5848 in GNAT implementation units (i.e., when @option{-gnatg} is used).
5849
5850 @item
5851 A line consisting entirely of minus signs, possibly preceded by blanks, is
5852 permitted. This allows the construction of box comments where lines of minus
5853 signs are used to form the top and bottom of the box.
5854
5855 @item
5856 A comment that starts and ends with ``@code{--}'' is permitted as long as at
5857 least one blank follows the initial ``@code{--}''. Together with the preceding
5858 rule, this allows the construction of box comments, as shown in the following
5859 example:
5860 @smallexample
5861 ---------------------------
5862 -- This is a box comment --
5863 -- with two text lines. --
5864 ---------------------------
5865 @end smallexample
5866 @end itemize
5867
5868 @item ^d^DOS_LINE_ENDINGS^
5869 @emph{Check no DOS line terminators present.}
5870 All lines must be terminated by a single ASCII.LF
5871 character (in particular the DOS line terminator sequence CR/LF is not
5872 allowed).
5873
5874 @item ^e^END^
5875 @emph{Check end/exit labels.}
5876 Optional labels on @code{end} statements ending subprograms and on
5877 @code{exit} statements exiting named loops, are required to be present.
5878
5879 @item ^f^VTABS^
5880 @emph{No form feeds or vertical tabs.}
5881 Neither form feeds nor vertical tab characters are permitted
5882 in the source text.
5883
5884 @item ^g^GNAT^
5885 @emph{GNAT style mode}
5886 The set of style check switches is set to match that used by the GNAT sources.
5887 This may be useful when developing code that is eventually intended to be
5888 incorporated into GNAT. For further details, see GNAT sources.
5889
5890 @item ^h^HTABS^
5891 @emph{No horizontal tabs.}
5892 Horizontal tab characters are not permitted in the source text.
5893 Together with the b (no blanks at end of line) check, this
5894 enforces a canonical form for the use of blanks to separate
5895 source tokens.
5896
5897 @item ^i^IF_THEN^
5898 @emph{Check if-then layout.}
5899 The keyword @code{then} must appear either on the same
5900 line as corresponding @code{if}, or on a line on its own, lined
5901 up under the @code{if} with at least one non-blank line in between
5902 containing all or part of the condition to be tested.
5903
5904 @item ^I^IN_MODE^
5905 @emph{check mode IN keywords}
5906 Mode @code{in} (the default mode) is not
5907 allowed to be given explicitly. @code{in out} is fine,
5908 but not @code{in} on its own.
5909
5910 @item ^k^KEYWORD^
5911 @emph{Check keyword casing.}
5912 All keywords must be in lower case (with the exception of keywords
5913 such as @code{digits} used as attribute names to which this check
5914 does not apply).
5915
5916 @item ^l^LAYOUT^
5917 @emph{Check layout.}
5918 Layout of statement and declaration constructs must follow the
5919 recommendations in the Ada Reference Manual, as indicated by the
5920 form of the syntax rules. For example an @code{else} keyword must
5921 be lined up with the corresponding @code{if} keyword.
5922
5923 There are two respects in which the style rule enforced by this check
5924 option are more liberal than those in the Ada Reference Manual. First
5925 in the case of record declarations, it is permissible to put the
5926 @code{record} keyword on the same line as the @code{type} keyword, and
5927 then the @code{end} in @code{end record} must line up under @code{type}.
5928 This is also permitted when the type declaration is split on two lines.
5929 For example, any of the following three layouts is acceptable:
5930
5931 @smallexample @c ada
5932 @cartouche
5933 type q is record
5934 a : integer;
5935 b : integer;
5936 end record;
5937
5938 type q is
5939 record
5940 a : integer;
5941 b : integer;
5942 end record;
5943
5944 type q is
5945 record
5946 a : integer;
5947 b : integer;
5948 end record;
5949
5950 @end cartouche
5951 @end smallexample
5952
5953 @noindent
5954 Second, in the case of a block statement, a permitted alternative
5955 is to put the block label on the same line as the @code{declare} or
5956 @code{begin} keyword, and then line the @code{end} keyword up under
5957 the block label. For example both the following are permitted:
5958
5959 @smallexample @c ada
5960 @cartouche
5961 Block : declare
5962 A : Integer := 3;
5963 begin
5964 Proc (A, A);
5965 end Block;
5966
5967 Block :
5968 declare
5969 A : Integer := 3;
5970 begin
5971 Proc (A, A);
5972 end Block;
5973 @end cartouche
5974 @end smallexample
5975
5976 @noindent
5977 The same alternative format is allowed for loops. For example, both of
5978 the following are permitted:
5979
5980 @smallexample @c ada
5981 @cartouche
5982 Clear : while J < 10 loop
5983 A (J) := 0;
5984 end loop Clear;
5985
5986 Clear :
5987 while J < 10 loop
5988 A (J) := 0;
5989 end loop Clear;
5990 @end cartouche
5991 @end smallexample
5992
5993 @item ^Lnnn^MAX_NESTING=nnn^
5994 @emph{Set maximum nesting level}
5995 The maximum level of nesting of constructs (including subprograms, loops,
5996 blocks, packages, and conditionals) may not exceed the given value
5997 @option{nnn}. A value of zero disconnects this style check.
5998
5999 @item ^m^LINE_LENGTH^
6000 @emph{Check maximum line length.}
6001 The length of source lines must not exceed 79 characters, including
6002 any trailing blanks. The value of 79 allows convenient display on an
6003 80 character wide device or window, allowing for possible special
6004 treatment of 80 character lines. Note that this count is of
6005 characters in the source text. This means that a tab character counts
6006 as one character in this count but a wide character sequence counts as
6007 a single character (however many bytes are needed in the encoding).
6008
6009 @item ^Mnnn^MAX_LENGTH=nnn^
6010 @emph{Set maximum line length.}
6011 The length of lines must not exceed the
6012 given value @option{nnn}. The maximum value that can be specified is 32767.
6013
6014 @item ^n^STANDARD_CASING^
6015 @emph{Check casing of entities in Standard.}
6016 Any identifier from Standard must be cased
6017 to match the presentation in the Ada Reference Manual (for example,
6018 @code{Integer} and @code{ASCII.NUL}).
6019
6020 @item ^N^NONE^
6021 @emph{Turn off all style checks}
6022 All style check options are turned off.
6023
6024 @item ^o^ORDERED_SUBPROGRAMS^
6025 @emph{Check order of subprogram bodies.}
6026 All subprogram bodies in a given scope
6027 (e.g.@: a package body) must be in alphabetical order. The ordering
6028 rule uses normal Ada rules for comparing strings, ignoring casing
6029 of letters, except that if there is a trailing numeric suffix, then
6030 the value of this suffix is used in the ordering (e.g.@: Junk2 comes
6031 before Junk10).
6032
6033 @item ^p^PRAGMA^
6034 @emph{Check pragma casing.}
6035 Pragma names must be written in mixed case, that is, the
6036 initial letter and any letter following an underscore must be uppercase.
6037 All other letters must be lowercase.
6038
6039 @item ^r^REFERENCES^
6040 @emph{Check references.}
6041 All identifier references must be cased in the same way as the
6042 corresponding declaration. No specific casing style is imposed on
6043 identifiers. The only requirement is for consistency of references
6044 with declarations.
6045
6046 @item ^S^STATEMENTS_AFTER_THEN_ELSE^
6047 @emph{Check no statements after THEN/ELSE.}
6048 No statements are allowed
6049 on the same line as a THEN or ELSE keyword following the
6050 keyword in an IF statement. OR ELSE and AND THEN are not affected,
6051 and a special exception allows a pragma to appear after ELSE.
6052
6053 @item ^s^SPECS^
6054 @emph{Check separate specs.}
6055 Separate declarations (``specs'') are required for subprograms (a
6056 body is not allowed to serve as its own declaration). The only
6057 exception is that parameterless library level procedures are
6058 not required to have a separate declaration. This exception covers
6059 the most frequent form of main program procedures.
6060
6061 @item ^t^TOKEN^
6062 @emph{Check token spacing.}
6063 The following token spacing rules are enforced:
6064
6065 @itemize @bullet
6066
6067 @item
6068 The keywords @code{@b{abs}} and @code{@b{not}} must be followed by a space.
6069
6070 @item
6071 The token @code{=>} must be surrounded by spaces.
6072
6073 @item
6074 The token @code{<>} must be preceded by a space or a left parenthesis.
6075
6076 @item
6077 Binary operators other than @code{**} must be surrounded by spaces.
6078 There is no restriction on the layout of the @code{**} binary operator.
6079
6080 @item
6081 Colon must be surrounded by spaces.
6082
6083 @item
6084 Colon-equal (assignment, initialization) must be surrounded by spaces.
6085
6086 @item
6087 Comma must be the first non-blank character on the line, or be
6088 immediately preceded by a non-blank character, and must be followed
6089 by a space.
6090
6091 @item
6092 If the token preceding a left parenthesis ends with a letter or digit, then
6093 a space must separate the two tokens.
6094
6095 @item
6096 A right parenthesis must either be the first non-blank character on
6097 a line, or it must be preceded by a non-blank character.
6098
6099 @item
6100 A semicolon must not be preceded by a space, and must not be followed by
6101 a non-blank character.
6102
6103 @item
6104 A unary plus or minus may not be followed by a space.
6105
6106 @item
6107 A vertical bar must be surrounded by spaces.
6108 @end itemize
6109
6110 @item ^u^UNNECESSARY_BLANK_LINES^
6111 @emph{Check unnecessary blank lines.}
6112 Unnecessary blank lines are not allowed. A blank line is considered
6113 unnecessary if it appears at the end of the file, or if more than
6114 one blank line occurs in sequence.
6115
6116 @item ^x^XTRA_PARENS^
6117 @emph{Check extra parentheses.}
6118 Unnecessary extra level of parentheses (C-style) are not allowed
6119 around conditions in @code{if} statements, @code{while} statements and
6120 @code{exit} statements.
6121
6122 @item ^y^ALL_BUILTIN^
6123 @emph{Set all standard style check options}
6124 This is equivalent to @code{gnaty3aAbcefhiklmnprst}, that is all checking
6125 options enabled with the exception of @option{-gnatyo}, @option{-gnatyI},
6126 @option{-gnatyS}, @option{-gnatyLnnn},
6127 @option{-gnatyd}, @option{-gnatyu}, and @option{-gnatyx}.
6128
6129 @ifclear vms
6130 @item -
6131 @emph{Remove style check options}
6132 This causes any subsequent options in the string to act as canceling the
6133 corresponding style check option. To cancel maximum nesting level control,
6134 use @option{L} parameter witout any integer value after that, because any
6135 digit following @option{-} in the parameter string of the @option{-gnaty}
6136 option will be threated as canceling indentation check. The same is true
6137 for @option{M} parameter. @option{y} and @option{N} parameters are not
6138 allowed after @option{-}.
6139
6140 @item +
6141 This causes any subsequent options in the string to enable the corresponding
6142 style check option. That is, it cancels the effect of a previous ^-^REMOVE^,
6143 if any.
6144 @end ifclear
6145
6146 @ifset vms
6147 @item NOxxx
6148 @emph{Removing style check options}
6149 If the name of a style check is preceded by @option{NO} then the corresponding
6150 style check is turned off. For example @option{NOCOMMENTS} turns off style
6151 checking for comments.
6152 @end ifset
6153 @end table
6154
6155 @noindent
6156 In the above rules, appearing in column one is always permitted, that is,
6157 counts as meeting either a requirement for a required preceding space,
6158 or as meeting a requirement for no preceding space.
6159
6160 Appearing at the end of a line is also always permitted, that is, counts
6161 as meeting either a requirement for a following space, or as meeting
6162 a requirement for no following space.
6163
6164 @noindent
6165 If any of these style rules is violated, a message is generated giving
6166 details on the violation. The initial characters of such messages are
6167 always ``@code{(style)}''. Note that these messages are treated as warning
6168 messages, so they normally do not prevent the generation of an object
6169 file. The @option{-gnatwe} switch can be used to treat warning messages,
6170 including style messages, as fatal errors.
6171
6172 The switch
6173 @ifclear vms
6174 @option{-gnaty} on its own (that is not
6175 followed by any letters or digits), then the effect is equivalent
6176 to the use of @option{-gnatyy}, as described above, that is all
6177 built-in standard style check options are enabled.
6178
6179 @end ifclear
6180 @ifset vms
6181 /STYLE_CHECKS=ALL_BUILTIN enables all checking options with
6182 the exception of ORDERED_SUBPROGRAMS, UNNECESSARY_BLANK_LINES,
6183 XTRA_PARENS, and DOS_LINE_ENDINGS. In addition
6184 @end ifset
6185
6186
6187
6188 The switch
6189 @ifclear vms
6190 @option{-gnatyN}
6191 @end ifclear
6192 @ifset vms
6193 /STYLE_CHECKS=NONE
6194 @end ifset
6195 clears any previously set style checks.
6196
6197 @node Run-Time Checks
6198 @subsection Run-Time Checks
6199 @cindex Division by zero
6200 @cindex Access before elaboration
6201 @cindex Checks, division by zero
6202 @cindex Checks, access before elaboration
6203 @cindex Checks, stack overflow checking
6204
6205 @noindent
6206 If you compile with the default options, GNAT will insert many run-time
6207 checks into the compiled code, including code that performs range
6208 checking against constraints, but not arithmetic overflow checking for
6209 integer operations (including division by zero), checks for access
6210 before elaboration on subprogram calls, or stack overflow checking. All
6211 other run-time checks, as required by the Ada Reference Manual, are
6212 generated by default. The following @command{gcc} switches refine this
6213 default behavior:
6214
6215 @table @option
6216 @c !sort!
6217 @item -gnatp
6218 @cindex @option{-gnatp} (@command{gcc})
6219 @cindex Suppressing checks
6220 @cindex Checks, suppressing
6221 @findex Suppress
6222 Suppress all run-time checks as though @code{pragma Suppress (all_checks})
6223 had been present in the source. Validity checks are also suppressed (in
6224 other words @option{-gnatp} also implies @option{-gnatVn}.
6225 Use this switch to improve the performance
6226 of the code at the expense of safety in the presence of invalid data or
6227 program bugs.
6228
6229 @item -gnato
6230 @cindex @option{-gnato} (@command{gcc})
6231 @cindex Overflow checks
6232 @cindex Check, overflow
6233 Enables overflow checking for integer operations.
6234 This causes GNAT to generate slower and larger executable
6235 programs by adding code to check for overflow (resulting in raising
6236 @code{Constraint_Error} as required by standard Ada
6237 semantics). These overflow checks correspond to situations in which
6238 the true value of the result of an operation may be outside the base
6239 range of the result type. The following example shows the distinction:
6240
6241 @smallexample @c ada
6242 X1 : Integer := Integer'Last;
6243 X2 : Integer range 1 .. 5 := 5;
6244 X3 : Integer := Integer'Last;
6245 X4 : Integer range 1 .. 5 := 5;
6246 F : Float := 2.0E+20;
6247 @dots{}
6248 X1 := X1 + 1;
6249 X2 := X2 + 1;
6250 X3 := Integer (F);
6251 X4 := Integer (F);
6252 @end smallexample
6253
6254 @noindent
6255 Here the first addition results in a value that is outside the base range
6256 of Integer, and hence requires an overflow check for detection of the
6257 constraint error. Thus the first assignment to @code{X1} raises a
6258 @code{Constraint_Error} exception only if @option{-gnato} is set.
6259
6260 The second increment operation results in a violation
6261 of the explicit range constraint, and such range checks are always
6262 performed (unless specifically suppressed with a pragma @code{suppress}
6263 or the use of @option{-gnatp}).
6264
6265 The two conversions of @code{F} both result in values that are outside
6266 the base range of type @code{Integer} and thus will raise
6267 @code{Constraint_Error} exceptions only if @option{-gnato} is used.
6268 The fact that the result of the second conversion is assigned to
6269 variable @code{X4} with a restricted range is irrelevant, since the problem
6270 is in the conversion, not the assignment.
6271
6272 Basically the rule is that in the default mode (@option{-gnato} not
6273 used), the generated code assures that all integer variables stay
6274 within their declared ranges, or within the base range if there is
6275 no declared range. This prevents any serious problems like indexes
6276 out of range for array operations.
6277
6278 What is not checked in default mode is an overflow that results in
6279 an in-range, but incorrect value. In the above example, the assignments
6280 to @code{X1}, @code{X2}, @code{X3} all give results that are within the
6281 range of the target variable, but the result is wrong in the sense that
6282 it is too large to be represented correctly. Typically the assignment
6283 to @code{X1} will result in wrap around to the largest negative number.
6284 The conversions of @code{F} will result in some @code{Integer} value
6285 and if that integer value is out of the @code{X4} range then the
6286 subsequent assignment would generate an exception.
6287
6288 @findex Machine_Overflows
6289 Note that the @option{-gnato} switch does not affect the code generated
6290 for any floating-point operations; it applies only to integer
6291 semantics).
6292 For floating-point, GNAT has the @code{Machine_Overflows}
6293 attribute set to @code{False} and the normal mode of operation is to
6294 generate IEEE NaN and infinite values on overflow or invalid operations
6295 (such as dividing 0.0 by 0.0).
6296
6297 The reason that we distinguish overflow checking from other kinds of
6298 range constraint checking is that a failure of an overflow check, unlike
6299 for example the failure of a range check, can result in an incorrect
6300 value, but cannot cause random memory destruction (like an out of range
6301 subscript), or a wild jump (from an out of range case value). Overflow
6302 checking is also quite expensive in time and space, since in general it
6303 requires the use of double length arithmetic.
6304
6305 Note again that @option{-gnato} is off by default, so overflow checking is
6306 not performed in default mode. This means that out of the box, with the
6307 default settings, GNAT does not do all the checks expected from the
6308 language description in the Ada Reference Manual. If you want all constraint
6309 checks to be performed, as described in this Manual, then you must
6310 explicitly use the -gnato switch either on the @command{gnatmake} or
6311 @command{gcc} command.
6312
6313 @item -gnatE
6314 @cindex @option{-gnatE} (@command{gcc})
6315 @cindex Elaboration checks
6316 @cindex Check, elaboration
6317 Enables dynamic checks for access-before-elaboration
6318 on subprogram calls and generic instantiations.
6319 For full details of the effect and use of this switch,
6320 @xref{Compiling Using gcc}.
6321
6322 @item -fstack-check
6323 @cindex @option{-fstack-check} (@command{gcc})
6324 @cindex Stack Overflow Checking
6325 @cindex Checks, stack overflow checking
6326 Activates stack overflow checking. For full details of the effect and use of
6327 this switch see @ref{Stack Overflow Checking}.
6328 @end table
6329
6330 @findex Unsuppress
6331 @noindent
6332 The setting of these switches only controls the default setting of the
6333 checks. You may modify them using either @code{Suppress} (to remove
6334 checks) or @code{Unsuppress} (to add back suppressed checks) pragmas in
6335 the program source.
6336
6337 @node Using gcc for Syntax Checking
6338 @subsection Using @command{gcc} for Syntax Checking
6339 @table @option
6340 @item -gnats
6341 @cindex @option{-gnats} (@command{gcc})
6342 @ifclear vms
6343
6344 @noindent
6345 The @code{s} stands for ``syntax''.
6346 @end ifclear
6347
6348 Run GNAT in syntax checking only mode. For
6349 example, the command
6350
6351 @smallexample
6352 $ gcc -c -gnats x.adb
6353 @end smallexample
6354
6355 @noindent
6356 compiles file @file{x.adb} in syntax-check-only mode. You can check a
6357 series of files in a single command
6358 @ifclear vms
6359 , and can use wild cards to specify such a group of files.
6360 Note that you must specify the @option{-c} (compile
6361 only) flag in addition to the @option{-gnats} flag.
6362 @end ifclear
6363 .
6364 You may use other switches in conjunction with @option{-gnats}. In
6365 particular, @option{-gnatl} and @option{-gnatv} are useful to control the
6366 format of any generated error messages.
6367
6368 When the source file is empty or contains only empty lines and/or comments,
6369 the output is a warning:
6370
6371 @smallexample
6372 $ gcc -c -gnats -x ada toto.txt
6373 toto.txt:1:01: warning: empty file, contains no compilation units
6374 $
6375 @end smallexample
6376
6377 Otherwise, the output is simply the error messages, if any. No object file or
6378 ALI file is generated by a syntax-only compilation. Also, no units other
6379 than the one specified are accessed. For example, if a unit @code{X}
6380 @code{with}'s a unit @code{Y}, compiling unit @code{X} in syntax
6381 check only mode does not access the source file containing unit
6382 @code{Y}.
6383
6384 @cindex Multiple units, syntax checking
6385 Normally, GNAT allows only a single unit in a source file. However, this
6386 restriction does not apply in syntax-check-only mode, and it is possible
6387 to check a file containing multiple compilation units concatenated
6388 together. This is primarily used by the @code{gnatchop} utility
6389 (@pxref{Renaming Files Using gnatchop}).
6390 @end table
6391
6392 @node Using gcc for Semantic Checking
6393 @subsection Using @command{gcc} for Semantic Checking
6394 @table @option
6395 @item -gnatc
6396 @cindex @option{-gnatc} (@command{gcc})
6397
6398 @ifclear vms
6399 @noindent
6400 The @code{c} stands for ``check''.
6401 @end ifclear
6402 Causes the compiler to operate in semantic check mode,
6403 with full checking for all illegalities specified in the
6404 Ada Reference Manual, but without generation of any object code
6405 (no object file is generated).
6406
6407 Because dependent files must be accessed, you must follow the GNAT
6408 semantic restrictions on file structuring to operate in this mode:
6409
6410 @itemize @bullet
6411 @item
6412 The needed source files must be accessible
6413 (@pxref{Search Paths and the Run-Time Library (RTL)}).
6414
6415 @item
6416 Each file must contain only one compilation unit.
6417
6418 @item
6419 The file name and unit name must match (@pxref{File Naming Rules}).
6420 @end itemize
6421
6422 The output consists of error messages as appropriate. No object file is
6423 generated. An @file{ALI} file is generated for use in the context of
6424 cross-reference tools, but this file is marked as not being suitable
6425 for binding (since no object file is generated).
6426 The checking corresponds exactly to the notion of
6427 legality in the Ada Reference Manual.
6428
6429 Any unit can be compiled in semantics-checking-only mode, including
6430 units that would not normally be compiled (subunits,
6431 and specifications where a separate body is present).
6432 @end table
6433
6434 @node Compiling Different Versions of Ada
6435 @subsection Compiling Different Versions of Ada
6436
6437 @noindent
6438 The switches described in this section allow you to explicitly specify
6439 the version of the Ada language that your programs are written in.
6440 By default @value{EDITION} assumes @value{DEFAULTLANGUAGEVERSION},
6441 but you can also specify @value{NONDEFAULTLANGUAGEVERSION} or
6442 indicate Ada 83 compatibility mode.
6443
6444 @table @option
6445 @cindex Compatibility with Ada 83
6446
6447 @item -gnat83 (Ada 83 Compatibility Mode)
6448 @cindex @option{-gnat83} (@command{gcc})
6449 @cindex ACVC, Ada 83 tests
6450 @cindex Ada 83 mode
6451
6452 @noindent
6453 Although GNAT is primarily an Ada 95 / Ada 2005 compiler, this switch
6454 specifies that the program is to be compiled in Ada 83 mode. With
6455 @option{-gnat83}, GNAT rejects most post-Ada 83 extensions and applies Ada 83
6456 semantics where this can be done easily.
6457 It is not possible to guarantee this switch does a perfect
6458 job; some subtle tests, such as are
6459 found in earlier ACVC tests (and that have been removed from the ACATS suite
6460 for Ada 95), might not compile correctly.
6461 Nevertheless, this switch may be useful in some circumstances, for example
6462 where, due to contractual reasons, existing code needs to be maintained
6463 using only Ada 83 features.
6464
6465 With few exceptions (most notably the need to use @code{<>} on
6466 @cindex Generic formal parameters
6467 unconstrained generic formal parameters, the use of the new Ada 95 / Ada 2005
6468 reserved words, and the use of packages
6469 with optional bodies), it is not necessary to specify the
6470 @option{-gnat83} switch when compiling Ada 83 programs, because, with rare
6471 exceptions, Ada 95 and Ada 2005 are upwardly compatible with Ada 83. Thus
6472 a correct Ada 83 program is usually also a correct program
6473 in these later versions of the language standard.
6474 For further information, please refer to @ref{Compatibility and Porting Guide}.
6475
6476 @item -gnat95 (Ada 95 mode)
6477 @cindex @option{-gnat95} (@command{gcc})
6478 @cindex Ada 95 mode
6479
6480 @noindent
6481 This switch directs the compiler to implement the Ada 95 version of the
6482 language.
6483 Since Ada 95 is almost completely upwards
6484 compatible with Ada 83, Ada 83 programs may generally be compiled using
6485 this switch (see the description of the @option{-gnat83} switch for further
6486 information about Ada 83 mode).
6487 If an Ada 2005 program is compiled in Ada 95 mode,
6488 uses of the new Ada 2005 features will cause error
6489 messages or warnings.
6490
6491 This switch also can be used to cancel the effect of a previous
6492 @option{-gnat83} or @option{-gnat05} switch earlier in the command line.
6493
6494 @item -gnat05 (Ada 2005 mode)
6495 @cindex @option{-gnat05} (@command{gcc})
6496 @cindex Ada 2005 mode
6497
6498 @noindent
6499 This switch directs the compiler to implement the Ada 2005 version of the
6500 language.
6501 Since Ada 2005 is almost completely upwards
6502 compatible with Ada 95 (and thus also with Ada 83), Ada 83 and Ada 95 programs
6503 may generally be compiled using this switch (see the description of the
6504 @option{-gnat83} and @option{-gnat95} switches for further
6505 information).
6506
6507 For information about the approved ``Ada Issues'' that have been incorporated
6508 into Ada 2005, see @url{http://www.ada-auth.org/cgi-bin/cvsweb.cgi/AIs}.
6509 Included with GNAT releases is a file @file{features-ada0y} that describes
6510 the set of implemented Ada 2005 features.
6511 @end table
6512
6513
6514 @node Character Set Control
6515 @subsection Character Set Control
6516 @table @option
6517 @item ^-gnati^/IDENTIFIER_CHARACTER_SET=^@var{c}
6518 @cindex @option{^-gnati^/IDENTIFIER_CHARACTER_SET^} (@command{gcc})
6519
6520 @noindent
6521 Normally GNAT recognizes the Latin-1 character set in source program
6522 identifiers, as described in the Ada Reference Manual.
6523 This switch causes
6524 GNAT to recognize alternate character sets in identifiers. @var{c} is a
6525 single character ^^or word^ indicating the character set, as follows:
6526
6527 @table @code
6528 @item 1
6529 ISO 8859-1 (Latin-1) identifiers
6530
6531 @item 2
6532 ISO 8859-2 (Latin-2) letters allowed in identifiers
6533
6534 @item 3
6535 ISO 8859-3 (Latin-3) letters allowed in identifiers
6536
6537 @item 4
6538 ISO 8859-4 (Latin-4) letters allowed in identifiers
6539
6540 @item 5
6541 ISO 8859-5 (Cyrillic) letters allowed in identifiers
6542
6543 @item 9
6544 ISO 8859-15 (Latin-9) letters allowed in identifiers
6545
6546 @item ^p^PC^
6547 IBM PC letters (code page 437) allowed in identifiers
6548
6549 @item ^8^PC850^
6550 IBM PC letters (code page 850) allowed in identifiers
6551
6552 @item ^f^FULL_UPPER^
6553 Full upper-half codes allowed in identifiers
6554
6555 @item ^n^NO_UPPER^
6556 No upper-half codes allowed in identifiers
6557
6558 @item ^w^WIDE^
6559 Wide-character codes (that is, codes greater than 255)
6560 allowed in identifiers
6561 @end table
6562
6563 @xref{Foreign Language Representation}, for full details on the
6564 implementation of these character sets.
6565
6566 @item ^-gnatW^/WIDE_CHARACTER_ENCODING=^@var{e}
6567 @cindex @option{^-gnatW^/WIDE_CHARACTER_ENCODING^} (@command{gcc})
6568 Specify the method of encoding for wide characters.
6569 @var{e} is one of the following:
6570
6571 @table @code
6572
6573 @item ^h^HEX^
6574 Hex encoding (brackets coding also recognized)
6575
6576 @item ^u^UPPER^
6577 Upper half encoding (brackets encoding also recognized)
6578
6579 @item ^s^SHIFT_JIS^
6580 Shift/JIS encoding (brackets encoding also recognized)
6581
6582 @item ^e^EUC^
6583 EUC encoding (brackets encoding also recognized)
6584
6585 @item ^8^UTF8^
6586 UTF-8 encoding (brackets encoding also recognized)
6587
6588 @item ^b^BRACKETS^
6589 Brackets encoding only (default value)
6590 @end table
6591 For full details on these encoding
6592 methods see @ref{Wide Character Encodings}.
6593 Note that brackets coding is always accepted, even if one of the other
6594 options is specified, so for example @option{-gnatW8} specifies that both
6595 brackets and UTF-8 encodings will be recognized. The units that are
6596 with'ed directly or indirectly will be scanned using the specified
6597 representation scheme, and so if one of the non-brackets scheme is
6598 used, it must be used consistently throughout the program. However,
6599 since brackets encoding is always recognized, it may be conveniently
6600 used in standard libraries, allowing these libraries to be used with
6601 any of the available coding schemes.
6602 scheme.
6603
6604 If no @option{-gnatW?} parameter is present, then the default
6605 representation is normally Brackets encoding only. However, if the
6606 first three characters of the file are 16#EF# 16#BB# 16#BF# (the standard
6607 byte order mark or BOM for UTF-8), then these three characters are
6608 skipped and the default representation for the file is set to UTF-8.
6609
6610 Note that the wide character representation that is specified (explicitly
6611 or by default) for the main program also acts as the default encoding used
6612 for Wide_Text_IO files if not specifically overridden by a WCEM form
6613 parameter.
6614
6615 @end table
6616 @node File Naming Control
6617 @subsection File Naming Control
6618
6619 @table @option
6620 @item ^-gnatk^/FILE_NAME_MAX_LENGTH=^@var{n}
6621 @cindex @option{-gnatk} (@command{gcc})
6622 Activates file name ``krunching''. @var{n}, a decimal integer in the range
6623 1-999, indicates the maximum allowable length of a file name (not
6624 including the @file{.ads} or @file{.adb} extension). The default is not
6625 to enable file name krunching.
6626
6627 For the source file naming rules, @xref{File Naming Rules}.
6628 @end table
6629
6630 @node Subprogram Inlining Control
6631 @subsection Subprogram Inlining Control
6632
6633 @table @option
6634 @c !sort!
6635 @item -gnatn
6636 @cindex @option{-gnatn} (@command{gcc})
6637 @ifclear vms
6638 The @code{n} here is intended to suggest the first syllable of the
6639 word ``inline''.
6640 @end ifclear
6641 GNAT recognizes and processes @code{Inline} pragmas. However, for the
6642 inlining to actually occur, optimization must be enabled. To enable
6643 inlining of subprograms specified by pragma @code{Inline},
6644 you must also specify this switch.
6645 In the absence of this switch, GNAT does not attempt
6646 inlining and does not need to access the bodies of
6647 subprograms for which @code{pragma Inline} is specified if they are not
6648 in the current unit.
6649
6650 If you specify this switch the compiler will access these bodies,
6651 creating an extra source dependency for the resulting object file, and
6652 where possible, the call will be inlined.
6653 For further details on when inlining is possible
6654 see @ref{Inlining of Subprograms}.
6655
6656 @item -gnatN
6657 @cindex @option{-gnatN} (@command{gcc})
6658 The front end inlining activated by this switch is generally more extensive,
6659 and quite often more effective than the standard @option{-gnatn} inlining mode.
6660 It will also generate additional dependencies.
6661 Note that
6662 @option{-gnatN} automatically implies @option{-gnatn} so it is not necessary
6663 to specify both options.
6664 @end table
6665
6666 @node Auxiliary Output Control
6667 @subsection Auxiliary Output Control
6668
6669 @table @option
6670 @item -gnatt
6671 @cindex @option{-gnatt} (@command{gcc})
6672 @cindex Writing internal trees
6673 @cindex Internal trees, writing to file
6674 Causes GNAT to write the internal tree for a unit to a file (with the
6675 extension @file{.adt}.
6676 This not normally required, but is used by separate analysis tools.
6677 Typically
6678 these tools do the necessary compilations automatically, so you should
6679 not have to specify this switch in normal operation.
6680
6681 @item -gnatu
6682 @cindex @option{-gnatu} (@command{gcc})
6683 Print a list of units required by this compilation on @file{stdout}.
6684 The listing includes all units on which the unit being compiled depends
6685 either directly or indirectly.
6686
6687 @ifclear vms
6688 @item -pass-exit-codes
6689 @cindex @option{-pass-exit-codes} (@command{gcc})
6690 If this switch is not used, the exit code returned by @command{gcc} when
6691 compiling multiple files indicates whether all source files have
6692 been successfully used to generate object files or not.
6693
6694 When @option{-pass-exit-codes} is used, @command{gcc} exits with an extended
6695 exit status and allows an integrated development environment to better
6696 react to a compilation failure. Those exit status are:
6697
6698 @table @asis
6699 @item 5
6700 There was an error in at least one source file.
6701 @item 3
6702 At least one source file did not generate an object file.
6703 @item 2
6704 The compiler died unexpectedly (internal error for example).
6705 @item 0
6706 An object file has been generated for every source file.
6707 @end table
6708 @end ifclear
6709 @end table
6710
6711 @node Debugging Control
6712 @subsection Debugging Control
6713
6714 @table @option
6715 @c !sort!
6716 @cindex Debugging options
6717 @ifclear vms
6718 @item -gnatd@var{x}
6719 @cindex @option{-gnatd} (@command{gcc})
6720 Activate internal debugging switches. @var{x} is a letter or digit, or
6721 string of letters or digits, which specifies the type of debugging
6722 outputs desired. Normally these are used only for internal development
6723 or system debugging purposes. You can find full documentation for these
6724 switches in the body of the @code{Debug} unit in the compiler source
6725 file @file{debug.adb}.
6726 @end ifclear
6727
6728 @item -gnatG
6729 @cindex @option{-gnatG} (@command{gcc})
6730 This switch causes the compiler to generate auxiliary output containing
6731 a pseudo-source listing of the generated expanded code. Like most Ada
6732 compilers, GNAT works by first transforming the high level Ada code into
6733 lower level constructs. For example, tasking operations are transformed
6734 into calls to the tasking run-time routines. A unique capability of GNAT
6735 is to list this expanded code in a form very close to normal Ada source.
6736 This is very useful in understanding the implications of various Ada
6737 usage on the efficiency of the generated code. There are many cases in
6738 Ada (e.g.@: the use of controlled types), where simple Ada statements can
6739 generate a lot of run-time code. By using @option{-gnatG} you can identify
6740 these cases, and consider whether it may be desirable to modify the coding
6741 approach to improve efficiency.
6742
6743 The format of the output is very similar to standard Ada source, and is
6744 easily understood by an Ada programmer. The following special syntactic
6745 additions correspond to low level features used in the generated code that
6746 do not have any exact analogies in pure Ada source form. The following
6747 is a partial list of these special constructions. See the spec
6748 of package @code{Sprint} in file @file{sprint.ads} for a full list.
6749
6750 If the switch @option{-gnatL} is used in conjunction with
6751 @cindex @option{-gnatL} (@command{gcc})
6752 @option{-gnatG}, then the original source lines are interspersed
6753 in the expanded source (as comment lines with the original line number).
6754
6755 @table @code
6756 @item new @var{xxx} @r{[}storage_pool = @var{yyy}@r{]}
6757 Shows the storage pool being used for an allocator.
6758
6759 @item at end @var{procedure-name};
6760 Shows the finalization (cleanup) procedure for a scope.
6761
6762 @item (if @var{expr} then @var{expr} else @var{expr})
6763 Conditional expression equivalent to the @code{x?y:z} construction in C.
6764
6765 @item @var{target}^^^(@var{source})
6766 A conversion with floating-point truncation instead of rounding.
6767
6768 @item @var{target}?(@var{source})
6769 A conversion that bypasses normal Ada semantic checking. In particular
6770 enumeration types and fixed-point types are treated simply as integers.
6771
6772 @item @var{target}?^^^(@var{source})
6773 Combines the above two cases.
6774
6775 @item @var{x} #/ @var{y}
6776 @itemx @var{x} #mod @var{y}
6777 @itemx @var{x} #* @var{y}
6778 @itemx @var{x} #rem @var{y}
6779 A division or multiplication of fixed-point values which are treated as
6780 integers without any kind of scaling.
6781
6782 @item free @var{expr} @r{[}storage_pool = @var{xxx}@r{]}
6783 Shows the storage pool associated with a @code{free} statement.
6784
6785 @item [subtype or type declaration]
6786 Used to list an equivalent declaration for an internally generated
6787 type that is referenced elsewhere in the listing.
6788
6789 @item freeze @var{type-name} @ovar{actions}
6790 Shows the point at which @var{type-name} is frozen, with possible
6791 associated actions to be performed at the freeze point.
6792
6793 @item reference @var{itype}
6794 Reference (and hence definition) to internal type @var{itype}.
6795
6796 @item @var{function-name}! (@var{arg}, @var{arg}, @var{arg})
6797 Intrinsic function call.
6798
6799 @item @var{label-name} : label
6800 Declaration of label @var{labelname}.
6801
6802 @item #$ @var{subprogram-name}
6803 An implicit call to a run-time support routine
6804 (to meet the requirement of H.3.1(9) in a
6805 convenient manner).
6806
6807 @item @var{expr} && @var{expr} && @var{expr} @dots{} && @var{expr}
6808 A multiple concatenation (same effect as @var{expr} & @var{expr} &
6809 @var{expr}, but handled more efficiently).
6810
6811 @item [constraint_error]
6812 Raise the @code{Constraint_Error} exception.
6813
6814 @item @var{expression}'reference
6815 A pointer to the result of evaluating @var{expression}.
6816
6817 @item @var{target-type}!(@var{source-expression})
6818 An unchecked conversion of @var{source-expression} to @var{target-type}.
6819
6820 @item [@var{numerator}/@var{denominator}]
6821 Used to represent internal real literals (that) have no exact
6822 representation in base 2-16 (for example, the result of compile time
6823 evaluation of the expression 1.0/27.0).
6824 @end table
6825
6826 @item -gnatD
6827 @cindex @option{-gnatD} (@command{gcc})
6828 When used in conjunction with @option{-gnatG}, this switch causes
6829 the expanded source, as described above for
6830 @option{-gnatG} to be written to files with names
6831 @file{^xxx.dg^XXX_DG^}, where @file{xxx} is the normal file name,
6832 instead of to the standard output file. For
6833 example, if the source file name is @file{hello.adb}, then a file
6834 @file{^hello.adb.dg^HELLO.ADB_DG^} will be written. The debugging
6835 information generated by the @command{gcc} @option{^-g^/DEBUG^} switch
6836 will refer to the generated @file{^xxx.dg^XXX_DG^} file. This allows
6837 you to do source level debugging using the generated code which is
6838 sometimes useful for complex code, for example to find out exactly
6839 which part of a complex construction raised an exception. This switch
6840 also suppress generation of cross-reference information (see
6841 @option{-gnatx}) since otherwise the cross-reference information
6842 would refer to the @file{^.dg^.DG^} file, which would cause
6843 confusion since this is not the original source file.
6844
6845 Note that @option{-gnatD} actually implies @option{-gnatG}
6846 automatically, so it is not necessary to give both options.
6847 In other words @option{-gnatD} is equivalent to @option{-gnatDG}).
6848
6849 If the switch @option{-gnatL} is used in conjunction with
6850 @cindex @option{-gnatL} (@command{gcc})
6851 @option{-gnatDG}, then the original source lines are interspersed
6852 in the expanded source (as comment lines with the original line number).
6853
6854 @item -gnatr
6855 @cindex @option{-gnatr} (@command{gcc})
6856 @cindex pragma Restrictions
6857 This switch causes pragma Restrictions to be treated as Restriction_Warnings
6858 so that violation of restrictions causes warnings rather than illegalities.
6859 This is useful during the development process when new restrictions are added
6860 or investigated. The switch also causes pragma Profile to be treated as
6861 Profile_Warnings, and pragma Restricted_Run_Time and pragma Ravenscar set
6862 restriction warnings rather than restrictions.
6863
6864 @ifclear vms
6865 @item -gnatR@r{[}0@r{|}1@r{|}2@r{|}3@r{[}s@r{]]}
6866 @cindex @option{-gnatR} (@command{gcc})
6867 This switch controls output from the compiler of a listing showing
6868 representation information for declared types and objects. For
6869 @option{-gnatR0}, no information is output (equivalent to omitting
6870 the @option{-gnatR} switch). For @option{-gnatR1} (which is the default,
6871 so @option{-gnatR} with no parameter has the same effect), size and alignment
6872 information is listed for declared array and record types. For
6873 @option{-gnatR2}, size and alignment information is listed for all
6874 declared types and objects. Finally @option{-gnatR3} includes symbolic
6875 expressions for values that are computed at run time for
6876 variant records. These symbolic expressions have a mostly obvious
6877 format with #n being used to represent the value of the n'th
6878 discriminant. See source files @file{repinfo.ads/adb} in the
6879 @code{GNAT} sources for full details on the format of @option{-gnatR3}
6880 output. If the switch is followed by an s (e.g.@: @option{-gnatR2s}), then
6881 the output is to a file with the name @file{^file.rep^file_REP^} where
6882 file is the name of the corresponding source file.
6883 @end ifclear
6884 @ifset vms
6885 @item /REPRESENTATION_INFO
6886 @cindex @option{/REPRESENTATION_INFO} (@command{gcc})
6887 This qualifier controls output from the compiler of a listing showing
6888 representation information for declared types and objects. For
6889 @option{/REPRESENTATION_INFO=NONE}, no information is output
6890 (equivalent to omitting the @option{/REPRESENTATION_INFO} qualifier).
6891 @option{/REPRESENTATION_INFO} without option is equivalent to
6892 @option{/REPRESENTATION_INFO=ARRAYS}.
6893 For @option{/REPRESENTATION_INFO=ARRAYS}, size and alignment
6894 information is listed for declared array and record types. For
6895 @option{/REPRESENTATION_INFO=OBJECTS}, size and alignment information
6896 is listed for all expression information for values that are computed
6897 at run time for variant records. These symbolic expressions have a mostly
6898 obvious format with #n being used to represent the value of the n'th
6899 discriminant. See source files @file{REPINFO.ADS/ADB} in the
6900 @code{GNAT} sources for full details on the format of
6901 @option{/REPRESENTATION_INFO=SYMBOLIC} output.
6902 If _FILE is added at the end of an option
6903 (e.g.@: @option{/REPRESENTATION_INFO=ARRAYS_FILE}),
6904 then the output is to a file with the name @file{file_REP} where
6905 file is the name of the corresponding source file.
6906 @end ifset
6907 Note that it is possible for record components to have zero size. In
6908 this case, the component clause uses an obvious extension of permitted
6909 Ada syntax, for example @code{at 0 range 0 .. -1}.
6910
6911 Representation information requires that code be generated (since it is the
6912 code generator that lays out complex data structures). If an attempt is made
6913 to output representation information when no code is generated, for example
6914 when a subunit is compiled on its own, then no information can be generated
6915 and the compiler outputs a message to this effect.
6916
6917 @item -gnatS
6918 @cindex @option{-gnatS} (@command{gcc})
6919 The use of the switch @option{-gnatS} for an
6920 Ada compilation will cause the compiler to output a
6921 representation of package Standard in a form very
6922 close to standard Ada. It is not quite possible to
6923 do this entirely in standard Ada (since new
6924 numeric base types cannot be created in standard
6925 Ada), but the output is easily
6926 readable to any Ada programmer, and is useful to
6927 determine the characteristics of target dependent
6928 types in package Standard.
6929
6930 @item -gnatx
6931 @cindex @option{-gnatx} (@command{gcc})
6932 Normally the compiler generates full cross-referencing information in
6933 the @file{ALI} file. This information is used by a number of tools,
6934 including @code{gnatfind} and @code{gnatxref}. The @option{-gnatx} switch
6935 suppresses this information. This saves some space and may slightly
6936 speed up compilation, but means that these tools cannot be used.
6937 @end table
6938
6939 @node Exception Handling Control
6940 @subsection Exception Handling Control
6941
6942 @noindent
6943 GNAT uses two methods for handling exceptions at run-time. The
6944 @code{setjmp/longjmp} method saves the context when entering
6945 a frame with an exception handler. Then when an exception is
6946 raised, the context can be restored immediately, without the
6947 need for tracing stack frames. This method provides very fast
6948 exception propagation, but introduces significant overhead for
6949 the use of exception handlers, even if no exception is raised.
6950
6951 The other approach is called ``zero cost'' exception handling.
6952 With this method, the compiler builds static tables to describe
6953 the exception ranges. No dynamic code is required when entering
6954 a frame containing an exception handler. When an exception is
6955 raised, the tables are used to control a back trace of the
6956 subprogram invocation stack to locate the required exception
6957 handler. This method has considerably poorer performance for
6958 the propagation of exceptions, but there is no overhead for
6959 exception handlers if no exception is raised. Note that in this
6960 mode and in the context of mixed Ada and C/C++ programming,
6961 to propagate an exception through a C/C++ code, the C/C++ code
6962 must be compiled with the @option{-funwind-tables} GCC's
6963 option.
6964
6965 The following switches may be used to control which of the
6966 two exception handling methods is used.
6967
6968 @table @option
6969 @c !sort!
6970
6971 @item --RTS=sjlj
6972 @cindex @option{--RTS=sjlj} (@command{gnatmake})
6973 This switch causes the setjmp/longjmp run-time (when available) to be used
6974 for exception handling. If the default
6975 mechanism for the target is zero cost exceptions, then
6976 this switch can be used to modify this default, and must be
6977 used for all units in the partition.
6978 This option is rarely used. One case in which it may be
6979 advantageous is if you have an application where exception
6980 raising is common and the overall performance of the
6981 application is improved by favoring exception propagation.
6982
6983 @item --RTS=zcx
6984 @cindex @option{--RTS=zcx} (@command{gnatmake})
6985 @cindex Zero Cost Exceptions
6986 This switch causes the zero cost approach to be used
6987 for exception handling. If this is the default mechanism for the
6988 target (see below), then this switch is unneeded. If the default
6989 mechanism for the target is setjmp/longjmp exceptions, then
6990 this switch can be used to modify this default, and must be
6991 used for all units in the partition.
6992 This option can only be used if the zero cost approach
6993 is available for the target in use, otherwise it will generate an error.
6994 @end table
6995
6996 @noindent
6997 The same option @option{--RTS} must be used both for @command{gcc}
6998 and @command{gnatbind}. Passing this option to @command{gnatmake}
6999 (@pxref{Switches for gnatmake}) will ensure the required consistency
7000 through the compilation and binding steps.
7001
7002 @node Units to Sources Mapping Files
7003 @subsection Units to Sources Mapping Files
7004
7005 @table @option
7006
7007 @item -gnatem^^=^@var{path}
7008 @cindex @option{-gnatem} (@command{gcc})
7009 A mapping file is a way to communicate to the compiler two mappings:
7010 from unit names to file names (without any directory information) and from
7011 file names to path names (with full directory information). These mappings
7012 are used by the compiler to short-circuit the path search.
7013
7014 The use of mapping files is not required for correct operation of the
7015 compiler, but mapping files can improve efficiency, particularly when
7016 sources are read over a slow network connection. In normal operation,
7017 you need not be concerned with the format or use of mapping files,
7018 and the @option{-gnatem} switch is not a switch that you would use
7019 explicitly. it is intended only for use by automatic tools such as
7020 @command{gnatmake} running under the project file facility. The
7021 description here of the format of mapping files is provided
7022 for completeness and for possible use by other tools.
7023
7024 A mapping file is a sequence of sets of three lines. In each set,
7025 the first line is the unit name, in lower case, with ``@code{%s}''
7026 appended for
7027 specs and ``@code{%b}'' appended for bodies; the second line is the
7028 file name; and the third line is the path name.
7029
7030 Example:
7031 @smallexample
7032 main%b
7033 main.2.ada
7034 /gnat/project1/sources/main.2.ada
7035 @end smallexample
7036
7037 When the switch @option{-gnatem} is specified, the compiler will create
7038 in memory the two mappings from the specified file. If there is any problem
7039 (nonexistent file, truncated file or duplicate entries), no mapping will
7040 be created.
7041
7042 Several @option{-gnatem} switches may be specified; however, only the last
7043 one on the command line will be taken into account.
7044
7045 When using a project file, @command{gnatmake} create a temporary mapping file
7046 and communicates it to the compiler using this switch.
7047
7048 @end table
7049
7050 @node Integrated Preprocessing
7051 @subsection Integrated Preprocessing
7052
7053 @noindent
7054 GNAT sources may be preprocessed immediately before compilation.
7055 In this case, the actual
7056 text of the source is not the text of the source file, but is derived from it
7057 through a process called preprocessing. Integrated preprocessing is specified
7058 through switches @option{-gnatep} and/or @option{-gnateD}. @option{-gnatep}
7059 indicates, through a text file, the preprocessing data to be used.
7060 @option{-gnateD} specifies or modifies the values of preprocessing symbol.
7061
7062 @noindent
7063 Note that when integrated preprocessing is used, the output from the
7064 preprocessor is not written to any external file. Instead it is passed
7065 internally to the compiler. If you need to preserve the result of
7066 preprocessing in a file, then you should use @command{gnatprep}
7067 to perform the desired preprocessing in stand-alone mode.
7068
7069 @noindent
7070 It is recommended that @command{gnatmake} switch ^-s^/SWITCH_CHECK^ should be
7071 used when Integrated Preprocessing is used. The reason is that preprocessing
7072 with another Preprocessing Data file without changing the sources will
7073 not trigger recompilation without this switch.
7074
7075 @noindent
7076 Note that @command{gnatmake} switch ^-m^/MINIMAL_RECOMPILATION^ will almost
7077 always trigger recompilation for sources that are preprocessed,
7078 because @command{gnatmake} cannot compute the checksum of the source after
7079 preprocessing.
7080
7081 @noindent
7082 The actual preprocessing function is described in details in section
7083 @ref{Preprocessing Using gnatprep}. This section only describes how integrated
7084 preprocessing is triggered and parameterized.
7085
7086 @table @code
7087
7088 @item -gnatep=@var{file}
7089 @cindex @option{-gnatep} (@command{gcc})
7090 This switch indicates to the compiler the file name (without directory
7091 information) of the preprocessor data file to use. The preprocessor data file
7092 should be found in the source directories.
7093
7094 @noindent
7095 A preprocessing data file is a text file with significant lines indicating
7096 how should be preprocessed either a specific source or all sources not
7097 mentioned in other lines. A significant line is a nonempty, non-comment line.
7098 Comments are similar to Ada comments.
7099
7100 @noindent
7101 Each significant line starts with either a literal string or the character '*'.
7102 A literal string is the file name (without directory information) of the source
7103 to preprocess. A character '*' indicates the preprocessing for all the sources
7104 that are not specified explicitly on other lines (order of the lines is not
7105 significant). It is an error to have two lines with the same file name or two
7106 lines starting with the character '*'.
7107
7108 @noindent
7109 After the file name or the character '*', another optional literal string
7110 indicating the file name of the definition file to be used for preprocessing
7111 (@pxref{Form of Definitions File}). The definition files are found by the
7112 compiler in one of the source directories. In some cases, when compiling
7113 a source in a directory other than the current directory, if the definition
7114 file is in the current directory, it may be necessary to add the current
7115 directory as a source directory through switch ^-I.^/SEARCH=[]^, otherwise
7116 the compiler would not find the definition file.
7117
7118 @noindent
7119 Then, optionally, ^switches^switches^ similar to those of @code{gnatprep} may
7120 be found. Those ^switches^switches^ are:
7121
7122 @table @code
7123
7124 @item -b
7125 Causes both preprocessor lines and the lines deleted by
7126 preprocessing to be replaced by blank lines, preserving the line number.
7127 This ^switch^switch^ is always implied; however, if specified after @option{-c}
7128 it cancels the effect of @option{-c}.
7129
7130 @item -c
7131 Causes both preprocessor lines and the lines deleted
7132 by preprocessing to be retained as comments marked
7133 with the special string ``@code{--! }''.
7134
7135 @item -Dsymbol=value
7136 Define or redefine a symbol, associated with value. A symbol is an Ada
7137 identifier, or an Ada reserved word, with the exception of @code{if},
7138 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
7139 @code{value} is either a literal string, an Ada identifier or any Ada reserved
7140 word. A symbol declared with this ^switch^switch^ replaces a symbol with the
7141 same name defined in a definition file.
7142
7143 @item -s
7144 Causes a sorted list of symbol names and values to be
7145 listed on the standard output file.
7146
7147 @item -u
7148 Causes undefined symbols to be treated as having the value @code{FALSE}
7149 in the context
7150 of a preprocessor test. In the absence of this option, an undefined symbol in
7151 a @code{#if} or @code{#elsif} test will be treated as an error.
7152
7153 @end table
7154
7155 @noindent
7156 Examples of valid lines in a preprocessor data file:
7157
7158 @smallexample
7159 "toto.adb" "prep.def" -u
7160 -- preprocess "toto.adb", using definition file "prep.def",
7161 -- undefined symbol are False.
7162
7163 * -c -DVERSION=V101
7164 -- preprocess all other sources without a definition file;
7165 -- suppressed lined are commented; symbol VERSION has the value V101.
7166
7167 "titi.adb" "prep2.def" -s
7168 -- preprocess "titi.adb", using definition file "prep2.def";
7169 -- list all symbols with their values.
7170 @end smallexample
7171
7172 @item ^-gnateD^/DATA_PREPROCESSING=^symbol@r{[}=value@r{]}
7173 @cindex @option{-gnateD} (@command{gcc})
7174 Define or redefine a preprocessing symbol, associated with value. If no value
7175 is given on the command line, then the value of the symbol is @code{True}.
7176 A symbol is an identifier, following normal Ada (case-insensitive)
7177 rules for its syntax, and value is any sequence (including an empty sequence)
7178 of characters from the set (letters, digits, period, underline).
7179 Ada reserved words may be used as symbols, with the exceptions of @code{if},
7180 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
7181
7182 @noindent
7183 A symbol declared with this ^switch^switch^ on the command line replaces a
7184 symbol with the same name either in a definition file or specified with a
7185 ^switch^switch^ -D in the preprocessor data file.
7186
7187 @noindent
7188 This switch is similar to switch @option{^-D^/ASSOCIATE^} of @code{gnatprep}.
7189
7190 @end table
7191
7192 @node Code Generation Control
7193 @subsection Code Generation Control
7194
7195 @noindent
7196
7197 The GCC technology provides a wide range of target dependent
7198 @option{-m} switches for controlling
7199 details of code generation with respect to different versions of
7200 architectures. This includes variations in instruction sets (e.g.@:
7201 different members of the power pc family), and different requirements
7202 for optimal arrangement of instructions (e.g.@: different members of
7203 the x86 family). The list of available @option{-m} switches may be
7204 found in the GCC documentation.
7205
7206 Use of these @option{-m} switches may in some cases result in improved
7207 code performance.
7208
7209 The GNAT Pro technology is tested and qualified without any
7210 @option{-m} switches,
7211 so generally the most reliable approach is to avoid the use of these
7212 switches. However, we generally expect most of these switches to work
7213 successfully with GNAT Pro, and many customers have reported successful
7214 use of these options.
7215
7216 Our general advice is to avoid the use of @option{-m} switches unless
7217 special needs lead to requirements in this area. In particular,
7218 there is no point in using @option{-m} switches to improve performance
7219 unless you actually see a performance improvement.
7220
7221 @ifset vms
7222 @node Return Codes
7223 @subsection Return Codes
7224 @cindex Return Codes
7225 @cindex @option{/RETURN_CODES=VMS}
7226
7227 @noindent
7228 On VMS, GNAT compiled programs return POSIX-style codes by default,
7229 e.g.@: @option{/RETURN_CODES=POSIX}.
7230
7231 To enable VMS style return codes, use GNAT BIND and LINK with the option
7232 @option{/RETURN_CODES=VMS}. For example:
7233
7234 @smallexample
7235 GNAT BIND MYMAIN.ALI /RETURN_CODES=VMS
7236 GNAT LINK MYMAIN.ALI /RETURN_CODES=VMS
7237 @end smallexample
7238
7239 @noindent
7240 Programs built with /RETURN_CODES=VMS are suitable to be called in
7241 VMS DCL scripts. Programs compiled with the default /RETURN_CODES=POSIX
7242 are suitable for spawning with appropriate GNAT RTL routines.
7243
7244 @end ifset
7245
7246 @node Search Paths and the Run-Time Library (RTL)
7247 @section Search Paths and the Run-Time Library (RTL)
7248
7249 @noindent
7250 With the GNAT source-based library system, the compiler must be able to
7251 find source files for units that are needed by the unit being compiled.
7252 Search paths are used to guide this process.
7253
7254 The compiler compiles one source file whose name must be given
7255 explicitly on the command line. In other words, no searching is done
7256 for this file. To find all other source files that are needed (the most
7257 common being the specs of units), the compiler examines the following
7258 directories, in the following order:
7259
7260 @enumerate
7261 @item
7262 The directory containing the source file of the main unit being compiled
7263 (the file name on the command line).
7264
7265 @item
7266 Each directory named by an @option{^-I^/SOURCE_SEARCH^} switch given on the
7267 @command{gcc} command line, in the order given.
7268
7269 @item
7270 @findex ADA_PRJ_INCLUDE_FILE
7271 Each of the directories listed in the text file whose name is given
7272 by the @env{ADA_PRJ_INCLUDE_FILE} ^environment variable^logical name^.
7273
7274 @noindent
7275 @env{ADA_PRJ_INCLUDE_FILE} is normally set by gnatmake or by the ^gnat^GNAT^
7276 driver when project files are used. It should not normally be set
7277 by other means.
7278
7279 @item
7280 @findex ADA_INCLUDE_PATH
7281 Each of the directories listed in the value of the
7282 @env{ADA_INCLUDE_PATH} ^environment variable^logical name^.
7283 @ifclear vms
7284 Construct this value
7285 exactly as the @env{PATH} environment variable: a list of directory
7286 names separated by colons (semicolons when working with the NT version).
7287 @end ifclear
7288 @ifset vms
7289 Normally, define this value as a logical name containing a comma separated
7290 list of directory names.
7291
7292 This variable can also be defined by means of an environment string
7293 (an argument to the HP C exec* set of functions).
7294
7295 Logical Name:
7296 @smallexample
7297 DEFINE ANOTHER_PATH FOO:[BAG]
7298 DEFINE ADA_INCLUDE_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
7299 @end smallexample
7300
7301 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
7302 first, followed by the standard Ada
7303 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADAINCLUDE].
7304 If this is not redefined, the user will obtain the HP Ada 83 IO packages
7305 (Text_IO, Sequential_IO, etc)
7306 instead of the standard Ada packages. Thus, in order to get the standard Ada
7307 packages by default, ADA_INCLUDE_PATH must be redefined.
7308 @end ifset
7309
7310 @item
7311 The content of the @file{ada_source_path} file which is part of the GNAT
7312 installation tree and is used to store standard libraries such as the
7313 GNAT Run Time Library (RTL) source files.
7314 @ifclear vms
7315 @ref{Installing a library}
7316 @end ifclear
7317 @end enumerate
7318
7319 @noindent
7320 Specifying the switch @option{^-I-^/NOCURRENT_DIRECTORY^}
7321 inhibits the use of the directory
7322 containing the source file named in the command line. You can still
7323 have this directory on your search path, but in this case it must be
7324 explicitly requested with a @option{^-I^/SOURCE_SEARCH^} switch.
7325
7326 Specifying the switch @option{-nostdinc}
7327 inhibits the search of the default location for the GNAT Run Time
7328 Library (RTL) source files.
7329
7330 The compiler outputs its object files and ALI files in the current
7331 working directory.
7332 @ifclear vms
7333 Caution: The object file can be redirected with the @option{-o} switch;
7334 however, @command{gcc} and @code{gnat1} have not been coordinated on this
7335 so the @file{ALI} file will not go to the right place. Therefore, you should
7336 avoid using the @option{-o} switch.
7337 @end ifclear
7338
7339 @findex System.IO
7340 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
7341 children make up the GNAT RTL, together with the simple @code{System.IO}
7342 package used in the @code{"Hello World"} example. The sources for these units
7343 are needed by the compiler and are kept together in one directory. Not
7344 all of the bodies are needed, but all of the sources are kept together
7345 anyway. In a normal installation, you need not specify these directory
7346 names when compiling or binding. Either the environment variables or
7347 the built-in defaults cause these files to be found.
7348
7349 In addition to the language-defined hierarchies (@code{System}, @code{Ada} and
7350 @code{Interfaces}), the GNAT distribution provides a fourth hierarchy,
7351 consisting of child units of @code{GNAT}. This is a collection of generally
7352 useful types, subprograms, etc. @xref{Top, GNAT Reference Manual, About
7353 This Guid, gnat_rm, GNAT Reference Manual}, for further details.
7354
7355 Besides simplifying access to the RTL, a major use of search paths is
7356 in compiling sources from multiple directories. This can make
7357 development environments much more flexible.
7358
7359 @node Order of Compilation Issues
7360 @section Order of Compilation Issues
7361
7362 @noindent
7363 If, in our earlier example, there was a spec for the @code{hello}
7364 procedure, it would be contained in the file @file{hello.ads}; yet this
7365 file would not have to be explicitly compiled. This is the result of the
7366 model we chose to implement library management. Some of the consequences
7367 of this model are as follows:
7368
7369 @itemize @bullet
7370 @item
7371 There is no point in compiling specs (except for package
7372 specs with no bodies) because these are compiled as needed by clients. If
7373 you attempt a useless compilation, you will receive an error message.
7374 It is also useless to compile subunits because they are compiled as needed
7375 by the parent.
7376
7377 @item
7378 There are no order of compilation requirements: performing a
7379 compilation never obsoletes anything. The only way you can obsolete
7380 something and require recompilations is to modify one of the
7381 source files on which it depends.
7382
7383 @item
7384 There is no library as such, apart from the ALI files
7385 (@pxref{The Ada Library Information Files}, for information on the format
7386 of these files). For now we find it convenient to create separate ALI files,
7387 but eventually the information therein may be incorporated into the object
7388 file directly.
7389
7390 @item
7391 When you compile a unit, the source files for the specs of all units
7392 that it @code{with}'s, all its subunits, and the bodies of any generics it
7393 instantiates must be available (reachable by the search-paths mechanism
7394 described above), or you will receive a fatal error message.
7395 @end itemize
7396
7397 @node Examples
7398 @section Examples
7399
7400 @noindent
7401 The following are some typical Ada compilation command line examples:
7402
7403 @table @code
7404 @item $ gcc -c xyz.adb
7405 Compile body in file @file{xyz.adb} with all default options.
7406
7407 @ifclear vms
7408 @item $ gcc -c -O2 -gnata xyz-def.adb
7409 @end ifclear
7410 @ifset vms
7411 @item $ GNAT COMPILE /OPTIMIZE=ALL -gnata xyz-def.adb
7412 @end ifset
7413
7414 Compile the child unit package in file @file{xyz-def.adb} with extensive
7415 optimizations, and pragma @code{Assert}/@code{Debug} statements
7416 enabled.
7417
7418 @item $ gcc -c -gnatc abc-def.adb
7419 Compile the subunit in file @file{abc-def.adb} in semantic-checking-only
7420 mode.
7421 @end table
7422
7423 @node Binding Using gnatbind
7424 @chapter Binding Using @code{gnatbind}
7425 @findex gnatbind
7426
7427 @menu
7428 * Running gnatbind::
7429 * Switches for gnatbind::
7430 * Command-Line Access::
7431 * Search Paths for gnatbind::
7432 * Examples of gnatbind Usage::
7433 @end menu
7434
7435 @noindent
7436 This chapter describes the GNAT binder, @code{gnatbind}, which is used
7437 to bind compiled GNAT objects.
7438
7439 Note: to invoke @code{gnatbind} with a project file, use the @code{gnat}
7440 driver (see @ref{The GNAT Driver and Project Files}).
7441
7442 The @code{gnatbind} program performs four separate functions:
7443
7444 @enumerate
7445 @item
7446 Checks that a program is consistent, in accordance with the rules in
7447 Chapter 10 of the Ada Reference Manual. In particular, error
7448 messages are generated if a program uses inconsistent versions of a
7449 given unit.
7450
7451 @item
7452 Checks that an acceptable order of elaboration exists for the program
7453 and issues an error message if it cannot find an order of elaboration
7454 that satisfies the rules in Chapter 10 of the Ada Language Manual.
7455
7456 @item
7457 Generates a main program incorporating the given elaboration order.
7458 This program is a small Ada package (body and spec) that
7459 must be subsequently compiled
7460 using the GNAT compiler. The necessary compilation step is usually
7461 performed automatically by @command{gnatlink}. The two most important
7462 functions of this program
7463 are to call the elaboration routines of units in an appropriate order
7464 and to call the main program.
7465
7466 @item
7467 Determines the set of object files required by the given main program.
7468 This information is output in the forms of comments in the generated program,
7469 to be read by the @command{gnatlink} utility used to link the Ada application.
7470 @end enumerate
7471
7472 @node Running gnatbind
7473 @section Running @code{gnatbind}
7474
7475 @noindent
7476 The form of the @code{gnatbind} command is
7477
7478 @smallexample
7479 $ gnatbind @ovar{switches} @var{mainprog}@r{[}.ali@r{]} @ovar{switches}
7480 @end smallexample
7481
7482 @noindent
7483 where @file{@var{mainprog}.adb} is the Ada file containing the main program
7484 unit body. If no switches are specified, @code{gnatbind} constructs an Ada
7485 package in two files whose names are
7486 @file{b~@var{mainprog}.ads}, and @file{b~@var{mainprog}.adb}.
7487 For example, if given the
7488 parameter @file{hello.ali}, for a main program contained in file
7489 @file{hello.adb}, the binder output files would be @file{b~hello.ads}
7490 and @file{b~hello.adb}.
7491
7492 When doing consistency checking, the binder takes into consideration
7493 any source files it can locate. For example, if the binder determines
7494 that the given main program requires the package @code{Pack}, whose
7495 @file{.ALI}
7496 file is @file{pack.ali} and whose corresponding source spec file is
7497 @file{pack.ads}, it attempts to locate the source file @file{pack.ads}
7498 (using the same search path conventions as previously described for the
7499 @command{gcc} command). If it can locate this source file, it checks that
7500 the time stamps
7501 or source checksums of the source and its references to in @file{ALI} files
7502 match. In other words, any @file{ALI} files that mentions this spec must have
7503 resulted from compiling this version of the source file (or in the case
7504 where the source checksums match, a version close enough that the
7505 difference does not matter).
7506
7507 @cindex Source files, use by binder
7508 The effect of this consistency checking, which includes source files, is
7509 that the binder ensures that the program is consistent with the latest
7510 version of the source files that can be located at bind time. Editing a
7511 source file without compiling files that depend on the source file cause
7512 error messages to be generated by the binder.
7513
7514 For example, suppose you have a main program @file{hello.adb} and a
7515 package @code{P}, from file @file{p.ads} and you perform the following
7516 steps:
7517
7518 @enumerate
7519 @item
7520 Enter @code{gcc -c hello.adb} to compile the main program.
7521
7522 @item
7523 Enter @code{gcc -c p.ads} to compile package @code{P}.
7524
7525 @item
7526 Edit file @file{p.ads}.
7527
7528 @item
7529 Enter @code{gnatbind hello}.
7530 @end enumerate
7531
7532 @noindent
7533 At this point, the file @file{p.ali} contains an out-of-date time stamp
7534 because the file @file{p.ads} has been edited. The attempt at binding
7535 fails, and the binder generates the following error messages:
7536
7537 @smallexample
7538 error: "hello.adb" must be recompiled ("p.ads" has been modified)
7539 error: "p.ads" has been modified and must be recompiled
7540 @end smallexample
7541
7542 @noindent
7543 Now both files must be recompiled as indicated, and then the bind can
7544 succeed, generating a main program. You need not normally be concerned
7545 with the contents of this file, but for reference purposes a sample
7546 binder output file is given in @ref{Example of Binder Output File}.
7547
7548 In most normal usage, the default mode of @command{gnatbind} which is to
7549 generate the main package in Ada, as described in the previous section.
7550 In particular, this means that any Ada programmer can read and understand
7551 the generated main program. It can also be debugged just like any other
7552 Ada code provided the @option{^-g^/DEBUG^} switch is used for
7553 @command{gnatbind} and @command{gnatlink}.
7554
7555 However for some purposes it may be convenient to generate the main
7556 program in C rather than Ada. This may for example be helpful when you
7557 are generating a mixed language program with the main program in C. The
7558 GNAT compiler itself is an example.
7559 The use of the @option{^-C^/BIND_FILE=C^} switch
7560 for both @code{gnatbind} and @command{gnatlink} will cause the program to
7561 be generated in C (and compiled using the gnu C compiler).
7562
7563 @node Switches for gnatbind
7564 @section Switches for @command{gnatbind}
7565
7566 @noindent
7567 The following switches are available with @code{gnatbind}; details will
7568 be presented in subsequent sections.
7569
7570 @menu
7571 * Consistency-Checking Modes::
7572 * Binder Error Message Control::
7573 * Elaboration Control::
7574 * Output Control::
7575 * Binding with Non-Ada Main Programs::
7576 * Binding Programs with No Main Subprogram::
7577 @end menu
7578
7579 @table @option
7580 @c !sort!
7581
7582 @item --version
7583 @cindex @option{--version} @command{gnatbind}
7584 Display Copyright and version, then exit disregarding all other options.
7585
7586 @item --help
7587 @cindex @option{--help} @command{gnatbind}
7588 If @option{--version} was not used, display usage, then exit disregarding
7589 all other options.
7590
7591 @item -a
7592 @cindex @option{-a} @command{gnatbind}
7593 Indicates that, if supported by the platform, the adainit procedure should
7594 be treated as an initialisation routine by the linker (a constructor). This
7595 is intended to be used by the Project Manager to automatically initialize
7596 shared Stand-Alone Libraries.
7597
7598 @item ^-aO^/OBJECT_SEARCH^
7599 @cindex @option{^-aO^/OBJECT_SEARCH^} (@command{gnatbind})
7600 Specify directory to be searched for ALI files.
7601
7602 @item ^-aI^/SOURCE_SEARCH^
7603 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatbind})
7604 Specify directory to be searched for source file.
7605
7606 @item ^-A^/BIND_FILE=ADA^
7607 @cindex @option{^-A^/BIND_FILE=ADA^} (@command{gnatbind})
7608 Generate binder program in Ada (default)
7609
7610 @item ^-b^/REPORT_ERRORS=BRIEF^
7611 @cindex @option{^-b^/REPORT_ERRORS=BRIEF^} (@command{gnatbind})
7612 Generate brief messages to @file{stderr} even if verbose mode set.
7613
7614 @item ^-c^/NOOUTPUT^
7615 @cindex @option{^-c^/NOOUTPUT^} (@command{gnatbind})
7616 Check only, no generation of binder output file.
7617
7618 @item ^-C^/BIND_FILE=C^
7619 @cindex @option{^-C^/BIND_FILE=C^} (@command{gnatbind})
7620 Generate binder program in C
7621
7622 @item ^-d^/DEFAULT_STACK_SIZE=^@var{nn}@r{[}k@r{|}m@r{]}
7623 @cindex @option{^-d^/DEFAULT_STACK_SIZE=^@var{nn}@r{[}k@r{|}m@r{]}} (@command{gnatbind})
7624 This switch can be used to change the default task stack size value
7625 to a specified size @var{nn}, which is expressed in bytes by default, or
7626 in kilobytes when suffixed with @var{k} or in megabytes when suffixed
7627 with @var{m}.
7628 In the absence of a @samp{@r{[}k@r{|}m@r{]}} suffix, this switch is equivalent,
7629 in effect, to completing all task specs with
7630 @smallexample @c ada
7631 pragma Storage_Size (nn);
7632 @end smallexample
7633 When they do not already have such a pragma.
7634
7635 @item ^-D^/DEFAULT_SECONDARY_STACK_SIZE=^@var{nn}@r{[}k@r{|}m@r{]}
7636 @cindex @option{^-D^/DEFAULT_SECONDARY_STACK_SIZE=nnnnn^} (@command{gnatbind})
7637 This switch can be used to change the default secondary stack size value
7638 to a specified size @var{nn}, which is expressed in bytes by default, or
7639 in kilobytes when suffixed with @var{k} or in megabytes when suffixed
7640 with @var{m}.
7641
7642 The secondary stack is used to deal with functions that return a variable
7643 sized result, for example a function returning an unconstrained
7644 String. There are two ways in which this secondary stack is allocated.
7645
7646 For most targets, the secondary stack is growing on demand and is allocated
7647 as a chain of blocks in the heap. The -D option is not very
7648 relevant. It only give some control over the size of the allocated
7649 blocks (whose size is the minimum of the default secondary stack size value,
7650 and the actual size needed for the current allocation request).
7651
7652 For certain targets, notably VxWorks 653,
7653 the secondary stack is allocated by carving off a fixed ratio chunk of the
7654 primary task stack. The -D option is used to define the
7655 size of the environment task's secondary stack.
7656
7657 @item ^-e^/ELABORATION_DEPENDENCIES^
7658 @cindex @option{^-e^/ELABORATION_DEPENDENCIES^} (@command{gnatbind})
7659 Output complete list of elaboration-order dependencies.
7660
7661 @item ^-E^/STORE_TRACEBACKS^
7662 @cindex @option{^-E^/STORE_TRACEBACKS^} (@command{gnatbind})
7663 Store tracebacks in exception occurrences when the target supports it.
7664 This is the default with the zero cost exception mechanism.
7665 @ignore
7666 @c The following may get moved to an appendix
7667 This option is currently supported on the following targets:
7668 all x86 ports, Solaris, Windows, HP-UX, AIX, PowerPC VxWorks and Alpha VxWorks.
7669 @end ignore
7670 See also the packages @code{GNAT.Traceback} and
7671 @code{GNAT.Traceback.Symbolic} for more information.
7672 @ifclear vms
7673 Note that on x86 ports, you must not use @option{-fomit-frame-pointer}
7674 @command{gcc} option.
7675 @end ifclear
7676
7677 @item ^-F^/FORCE_ELABS_FLAGS^
7678 @cindex @option{^-F^/FORCE_ELABS_FLAGS^} (@command{gnatbind})
7679 Force the checks of elaboration flags. @command{gnatbind} does not normally
7680 generate checks of elaboration flags for the main executable, except when
7681 a Stand-Alone Library is used. However, there are cases when this cannot be
7682 detected by gnatbind. An example is importing an interface of a Stand-Alone
7683 Library through a pragma Import and only specifying through a linker switch
7684 this Stand-Alone Library. This switch is used to guarantee that elaboration
7685 flag checks are generated.
7686
7687 @item ^-h^/HELP^
7688 @cindex @option{^-h^/HELP^} (@command{gnatbind})
7689 Output usage (help) information
7690
7691 @item ^-I^/SEARCH^
7692 @cindex @option{^-I^/SEARCH^} (@command{gnatbind})
7693 Specify directory to be searched for source and ALI files.
7694
7695 @item ^-I-^/NOCURRENT_DIRECTORY^
7696 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gnatbind})
7697 Do not look for sources in the current directory where @code{gnatbind} was
7698 invoked, and do not look for ALI files in the directory containing the
7699 ALI file named in the @code{gnatbind} command line.
7700
7701 @item ^-l^/ORDER_OF_ELABORATION^
7702 @cindex @option{^-l^/ORDER_OF_ELABORATION^} (@command{gnatbind})
7703 Output chosen elaboration order.
7704
7705 @item ^-L@var{xxx}^/BUILD_LIBRARY=@var{xxx}^
7706 @cindex @option{^-L^/BUILD_LIBRARY^} (@command{gnatbind})
7707 Bind the units for library building. In this case the adainit and
7708 adafinal procedures (@pxref{Binding with Non-Ada Main Programs})
7709 are renamed to ^@var{xxx}init^@var{XXX}INIT^ and
7710 ^@var{xxx}final^@var{XXX}FINAL^.
7711 Implies ^-n^/NOCOMPILE^.
7712 @ifclear vms
7713 (@xref{GNAT and Libraries}, for more details.)
7714 @end ifclear
7715 @ifset vms
7716 On OpenVMS, these init and final procedures are exported in uppercase
7717 letters. For example if /BUILD_LIBRARY=toto is used, the exported name of
7718 the init procedure will be "TOTOINIT" and the exported name of the final
7719 procedure will be "TOTOFINAL".
7720 @end ifset
7721
7722 @item ^-Mxyz^/RENAME_MAIN=xyz^
7723 @cindex @option{^-M^/RENAME_MAIN^} (@command{gnatbind})
7724 Rename generated main program from main to xyz. This option is
7725 supported on cross environments only.
7726
7727 @item ^-m^/ERROR_LIMIT=^@var{n}
7728 @cindex @option{^-m^/ERROR_LIMIT^} (@command{gnatbind})
7729 Limit number of detected errors to @var{n}, where @var{n} is
7730 in the range 1..999_999. The default value if no switch is
7731 given is 9999. Binding is terminated if the limit is exceeded.
7732 @ifset unw
7733 Furthermore, under Windows, the sources pointed to by the libraries path
7734 set in the registry are not searched for.
7735 @end ifset
7736
7737 @item ^-n^/NOMAIN^
7738 @cindex @option{^-n^/NOMAIN^} (@command{gnatbind})
7739 No main program.
7740
7741 @item -nostdinc
7742 @cindex @option{-nostdinc} (@command{gnatbind})
7743 Do not look for sources in the system default directory.
7744
7745 @item -nostdlib
7746 @cindex @option{-nostdlib} (@command{gnatbind})
7747 Do not look for library files in the system default directory.
7748
7749 @item --RTS=@var{rts-path}
7750 @cindex @option{--RTS} (@code{gnatbind})
7751 Specifies the default location of the runtime library. Same meaning as the
7752 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
7753
7754 @item ^-o ^/OUTPUT=^@var{file}
7755 @cindex @option{^-o ^/OUTPUT^} (@command{gnatbind})
7756 Name the output file @var{file} (default is @file{b~@var{xxx}.adb}).
7757 Note that if this option is used, then linking must be done manually,
7758 gnatlink cannot be used.
7759
7760 @item ^-O^/OBJECT_LIST^
7761 @cindex @option{^-O^/OBJECT_LIST^} (@command{gnatbind})
7762 Output object list.
7763
7764 @item ^-p^/PESSIMISTIC_ELABORATION^
7765 @cindex @option{^-p^/PESSIMISTIC_ELABORATION^} (@command{gnatbind})
7766 Pessimistic (worst-case) elaboration order
7767
7768 @item ^-R^-R^
7769 @cindex @option{^-R^-R^} (@command{gnatbind})
7770 Output closure source list.
7771
7772 @item ^-s^/READ_SOURCES=ALL^
7773 @cindex @option{^-s^/READ_SOURCES=ALL^} (@command{gnatbind})
7774 Require all source files to be present.
7775
7776 @item ^-S@var{xxx}^/INITIALIZE_SCALARS=@var{xxx}^
7777 @cindex @option{^-S^/INITIALIZE_SCALARS^} (@command{gnatbind})
7778 Specifies the value to be used when detecting uninitialized scalar
7779 objects with pragma Initialize_Scalars.
7780 The @var{xxx} ^string specified with the switch^option^ may be either
7781 @itemize @bullet
7782 @item ``@option{^in^INVALID^}'' requesting an invalid value where possible
7783 @item ``@option{^lo^LOW^}'' for the lowest possible value
7784 @item ``@option{^hi^HIGH^}'' for the highest possible value
7785 @item ``@option{@var{xx}}'' for a value consisting of repeated bytes with the
7786 value @code{16#@var{xx}#} (i.e., @var{xx} is a string of two hexadecimal digits).
7787 @end itemize
7788
7789 In addition, you can specify @option{-Sev} to indicate that the value is
7790 to be set at run time. In this case, the program will look for an environment
7791 @cindex GNAT_INIT_SCALARS
7792 variable of the form @env{GNAT_INIT_SCALARS=@var{xx}}, where @var{xx} is one
7793 of @option{in/lo/hi/@var{xx}} with the same meanings as above.
7794 If no environment variable is found, or if it does not have a valid value,
7795 then the default is @option{in} (invalid values).
7796
7797 @ifclear vms
7798 @item -static
7799 @cindex @option{-static} (@code{gnatbind})
7800 Link against a static GNAT run time.
7801
7802 @item -shared
7803 @cindex @option{-shared} (@code{gnatbind})
7804 Link against a shared GNAT run time when available.
7805 @end ifclear
7806
7807 @item ^-t^/NOTIME_STAMP_CHECK^
7808 @cindex @option{^-t^/NOTIME_STAMP_CHECK^} (@code{gnatbind})
7809 Tolerate time stamp and other consistency errors
7810
7811 @item ^-T@var{n}^/TIME_SLICE=@var{n}^
7812 @cindex @option{^-T^/TIME_SLICE^} (@code{gnatbind})
7813 Set the time slice value to @var{n} milliseconds. If the system supports
7814 the specification of a specific time slice value, then the indicated value
7815 is used. If the system does not support specific time slice values, but
7816 does support some general notion of round-robin scheduling, then any
7817 nonzero value will activate round-robin scheduling.
7818
7819 A value of zero is treated specially. It turns off time
7820 slicing, and in addition, indicates to the tasking run time that the
7821 semantics should match as closely as possible the Annex D
7822 requirements of the Ada RM, and in particular sets the default
7823 scheduling policy to @code{FIFO_Within_Priorities}.
7824
7825 @item ^-u@var{n}^/DYNAMIC_STACK_USAGE=@var{n}^
7826 @cindex @option{^-u^/DYNAMIC_STACK_USAGE^} (@code{gnatbind})
7827 Enable dynamic stack usage, with @var{n} results stored and displayed
7828 at program termination. A result is generated when a task
7829 terminates. Results that can't be stored are displayed on the fly, at
7830 task termination. This option is currently not supported on Itanium
7831 platforms. (See @ref{Dynamic Stack Usage Analysis} for details.)
7832
7833 @item ^-v^/REPORT_ERRORS=VERBOSE^
7834 @cindex @option{^-v^/REPORT_ERRORS=VERBOSE^} (@code{gnatbind})
7835 Verbose mode. Write error messages, header, summary output to
7836 @file{stdout}.
7837
7838 @ifclear vms
7839 @item -w@var{x}
7840 @cindex @option{-w} (@code{gnatbind})
7841 Warning mode (@var{x}=s/e for suppress/treat as error)
7842 @end ifclear
7843
7844 @ifset vms
7845 @item /WARNINGS=NORMAL
7846 @cindex @option{/WARNINGS} (@code{gnatbind})
7847 Normal warnings mode. Warnings are issued but ignored
7848
7849 @item /WARNINGS=SUPPRESS
7850 @cindex @option{/WARNINGS} (@code{gnatbind})
7851 All warning messages are suppressed
7852
7853 @item /WARNINGS=ERROR
7854 @cindex @option{/WARNINGS} (@code{gnatbind})
7855 Warning messages are treated as fatal errors
7856 @end ifset
7857
7858 @item ^-Wx^/WIDE_CHARACTER_ENCODING=^@var{e}
7859 @cindex @option{^-Wx^/WIDE_CHARACTER_ENCODING^} (@code{gnatbind})
7860 Override default wide character encoding for standard Text_IO files.
7861
7862 @item ^-x^/READ_SOURCES=NONE^
7863 @cindex @option{^-x^/READ_SOURCES^} (@code{gnatbind})
7864 Exclude source files (check object consistency only).
7865
7866 @ifset vms
7867 @item /READ_SOURCES=AVAILABLE
7868 @cindex @option{/READ_SOURCES} (@code{gnatbind})
7869 Default mode, in which sources are checked for consistency only if
7870 they are available.
7871 @end ifset
7872
7873 @item ^-y^/ENABLE_LEAP_SECONDS^
7874 @cindex @option{^-y^/ENABLE_LEAP_SECONDS^} (@code{gnatbind})
7875 Enable leap seconds support in @code{Ada.Calendar} and its children.
7876
7877 @item ^-z^/ZERO_MAIN^
7878 @cindex @option{^-z^/ZERO_MAIN^} (@code{gnatbind})
7879 No main subprogram.
7880 @end table
7881
7882 @ifclear vms
7883 @noindent
7884 You may obtain this listing of switches by running @code{gnatbind} with
7885 no arguments.
7886 @end ifclear
7887
7888 @node Consistency-Checking Modes
7889 @subsection Consistency-Checking Modes
7890
7891 @noindent
7892 As described earlier, by default @code{gnatbind} checks
7893 that object files are consistent with one another and are consistent
7894 with any source files it can locate. The following switches control binder
7895 access to sources.
7896
7897 @table @option
7898 @c !sort!
7899 @item ^-s^/READ_SOURCES=ALL^
7900 @cindex @option{^-s^/READ_SOURCES=ALL^} (@code{gnatbind})
7901 Require source files to be present. In this mode, the binder must be
7902 able to locate all source files that are referenced, in order to check
7903 their consistency. In normal mode, if a source file cannot be located it
7904 is simply ignored. If you specify this switch, a missing source
7905 file is an error.
7906
7907 @item ^-Wx^/WIDE_CHARACTER_ENCODING=^@var{e}
7908 @cindex @option{^-Wx^/WIDE_CHARACTER_ENCODING^} (@code{gnatbind})
7909 Override default wide character encoding for standard Text_IO files.
7910 Normally the default wide character encoding method used for standard
7911 [Wide_[Wide_]]Text_IO files is taken from the encoding specified for
7912 the main source input (see description of switch
7913 @option{^-gnatWx^/WIDE_CHARACTER_ENCODING^} for the compiler). The
7914 use of this switch for the binder (which has the same set of
7915 possible arguments) overrides this default as specified.
7916
7917 @item ^-x^/READ_SOURCES=NONE^
7918 @cindex @option{^-x^/READ_SOURCES=NONE^} (@code{gnatbind})
7919 Exclude source files. In this mode, the binder only checks that ALI
7920 files are consistent with one another. Source files are not accessed.
7921 The binder runs faster in this mode, and there is still a guarantee that
7922 the resulting program is self-consistent.
7923 If a source file has been edited since it was last compiled, and you
7924 specify this switch, the binder will not detect that the object
7925 file is out of date with respect to the source file. Note that this is the
7926 mode that is automatically used by @command{gnatmake} because in this
7927 case the checking against sources has already been performed by
7928 @command{gnatmake} in the course of compilation (i.e.@: before binding).
7929
7930 @ifset vms
7931 @item /READ_SOURCES=AVAILABLE
7932 @cindex @code{/READ_SOURCES=AVAILABLE} (@code{gnatbind})
7933 This is the default mode in which source files are checked if they are
7934 available, and ignored if they are not available.
7935 @end ifset
7936 @end table
7937
7938 @node Binder Error Message Control
7939 @subsection Binder Error Message Control
7940
7941 @noindent
7942 The following switches provide control over the generation of error
7943 messages from the binder:
7944
7945 @table @option
7946 @c !sort!
7947 @item ^-v^/REPORT_ERRORS=VERBOSE^
7948 @cindex @option{^-v^/REPORT_ERRORS=VERBOSE^} (@code{gnatbind})
7949 Verbose mode. In the normal mode, brief error messages are generated to
7950 @file{stderr}. If this switch is present, a header is written
7951 to @file{stdout} and any error messages are directed to @file{stdout}.
7952 All that is written to @file{stderr} is a brief summary message.
7953
7954 @item ^-b^/REPORT_ERRORS=BRIEF^
7955 @cindex @option{^-b^/REPORT_ERRORS=BRIEF^} (@code{gnatbind})
7956 Generate brief error messages to @file{stderr} even if verbose mode is
7957 specified. This is relevant only when used with the
7958 @option{^-v^/REPORT_ERRORS=VERBOSE^} switch.
7959
7960 @ifclear vms
7961 @item -m@var{n}
7962 @cindex @option{-m} (@code{gnatbind})
7963 Limits the number of error messages to @var{n}, a decimal integer in the
7964 range 1-999. The binder terminates immediately if this limit is reached.
7965
7966 @item -M@var{xxx}
7967 @cindex @option{-M} (@code{gnatbind})
7968 Renames the generated main program from @code{main} to @code{xxx}.
7969 This is useful in the case of some cross-building environments, where
7970 the actual main program is separate from the one generated
7971 by @code{gnatbind}.
7972 @end ifclear
7973
7974 @item ^-ws^/WARNINGS=SUPPRESS^
7975 @cindex @option{^-ws^/WARNINGS=SUPPRESS^} (@code{gnatbind})
7976 @cindex Warnings
7977 Suppress all warning messages.
7978
7979 @item ^-we^/WARNINGS=ERROR^
7980 @cindex @option{^-we^/WARNINGS=ERROR^} (@code{gnatbind})
7981 Treat any warning messages as fatal errors.
7982
7983 @ifset vms
7984 @item /WARNINGS=NORMAL
7985 Standard mode with warnings generated, but warnings do not get treated
7986 as errors.
7987 @end ifset
7988
7989 @item ^-t^/NOTIME_STAMP_CHECK^
7990 @cindex @option{^-t^/NOTIME_STAMP_CHECK^} (@code{gnatbind})
7991 @cindex Time stamp checks, in binder
7992 @cindex Binder consistency checks
7993 @cindex Consistency checks, in binder
7994 The binder performs a number of consistency checks including:
7995
7996 @itemize @bullet
7997 @item
7998 Check that time stamps of a given source unit are consistent
7999 @item
8000 Check that checksums of a given source unit are consistent
8001 @item
8002 Check that consistent versions of @code{GNAT} were used for compilation
8003 @item
8004 Check consistency of configuration pragmas as required
8005 @end itemize
8006
8007 @noindent
8008 Normally failure of such checks, in accordance with the consistency
8009 requirements of the Ada Reference Manual, causes error messages to be
8010 generated which abort the binder and prevent the output of a binder
8011 file and subsequent link to obtain an executable.
8012
8013 The @option{^-t^/NOTIME_STAMP_CHECK^} switch converts these error messages
8014 into warnings, so that
8015 binding and linking can continue to completion even in the presence of such
8016 errors. The result may be a failed link (due to missing symbols), or a
8017 non-functional executable which has undefined semantics.
8018 @emph{This means that
8019 @option{^-t^/NOTIME_STAMP_CHECK^} should be used only in unusual situations,
8020 with extreme care.}
8021 @end table
8022
8023 @node Elaboration Control
8024 @subsection Elaboration Control
8025
8026 @noindent
8027 The following switches provide additional control over the elaboration
8028 order. For full details see @ref{Elaboration Order Handling in GNAT}.
8029
8030 @table @option
8031 @item ^-p^/PESSIMISTIC_ELABORATION^
8032 @cindex @option{^-p^/PESSIMISTIC_ELABORATION^} (@code{gnatbind})
8033 Normally the binder attempts to choose an elaboration order that is
8034 likely to minimize the likelihood of an elaboration order error resulting
8035 in raising a @code{Program_Error} exception. This switch reverses the
8036 action of the binder, and requests that it deliberately choose an order
8037 that is likely to maximize the likelihood of an elaboration error.
8038 This is useful in ensuring portability and avoiding dependence on
8039 accidental fortuitous elaboration ordering.
8040
8041 Normally it only makes sense to use the @option{^-p^/PESSIMISTIC_ELABORATION^}
8042 switch if dynamic
8043 elaboration checking is used (@option{-gnatE} switch used for compilation).
8044 This is because in the default static elaboration mode, all necessary
8045 @code{Elaborate} and @code{Elaborate_All} pragmas are implicitly inserted.
8046 These implicit pragmas are still respected by the binder in
8047 @option{^-p^/PESSIMISTIC_ELABORATION^} mode, so a
8048 safe elaboration order is assured.
8049 @end table
8050
8051 @node Output Control
8052 @subsection Output Control
8053
8054 @noindent
8055 The following switches allow additional control over the output
8056 generated by the binder.
8057
8058 @table @option
8059 @c !sort!
8060
8061 @item ^-A^/BIND_FILE=ADA^
8062 @cindex @option{^-A^/BIND_FILE=ADA^} (@code{gnatbind})
8063 Generate binder program in Ada (default). The binder program is named
8064 @file{b~@var{mainprog}.adb} by default. This can be changed with
8065 @option{^-o^/OUTPUT^} @code{gnatbind} option.
8066
8067 @item ^-c^/NOOUTPUT^
8068 @cindex @option{^-c^/NOOUTPUT^} (@code{gnatbind})
8069 Check only. Do not generate the binder output file. In this mode the
8070 binder performs all error checks but does not generate an output file.
8071
8072 @item ^-C^/BIND_FILE=C^
8073 @cindex @option{^-C^/BIND_FILE=C^} (@code{gnatbind})
8074 Generate binder program in C. The binder program is named
8075 @file{b_@var{mainprog}.c}.
8076 This can be changed with @option{^-o^/OUTPUT^} @code{gnatbind}
8077 option.
8078
8079 @item ^-e^/ELABORATION_DEPENDENCIES^
8080 @cindex @option{^-e^/ELABORATION_DEPENDENCIES^} (@code{gnatbind})
8081 Output complete list of elaboration-order dependencies, showing the
8082 reason for each dependency. This output can be rather extensive but may
8083 be useful in diagnosing problems with elaboration order. The output is
8084 written to @file{stdout}.
8085
8086 @item ^-h^/HELP^
8087 @cindex @option{^-h^/HELP^} (@code{gnatbind})
8088 Output usage information. The output is written to @file{stdout}.
8089
8090 @item ^-K^/LINKER_OPTION_LIST^
8091 @cindex @option{^-K^/LINKER_OPTION_LIST^} (@code{gnatbind})
8092 Output linker options to @file{stdout}. Includes library search paths,
8093 contents of pragmas Ident and Linker_Options, and libraries added
8094 by @code{gnatbind}.
8095
8096 @item ^-l^/ORDER_OF_ELABORATION^
8097 @cindex @option{^-l^/ORDER_OF_ELABORATION^} (@code{gnatbind})
8098 Output chosen elaboration order. The output is written to @file{stdout}.
8099
8100 @item ^-O^/OBJECT_LIST^
8101 @cindex @option{^-O^/OBJECT_LIST^} (@code{gnatbind})
8102 Output full names of all the object files that must be linked to provide
8103 the Ada component of the program. The output is written to @file{stdout}.
8104 This list includes the files explicitly supplied and referenced by the user
8105 as well as implicitly referenced run-time unit files. The latter are
8106 omitted if the corresponding units reside in shared libraries. The
8107 directory names for the run-time units depend on the system configuration.
8108
8109 @item ^-o ^/OUTPUT=^@var{file}
8110 @cindex @option{^-o^/OUTPUT^} (@code{gnatbind})
8111 Set name of output file to @var{file} instead of the normal
8112 @file{b~@var{mainprog}.adb} default. Note that @var{file} denote the Ada
8113 binder generated body filename. In C mode you would normally give
8114 @var{file} an extension of @file{.c} because it will be a C source program.
8115 Note that if this option is used, then linking must be done manually.
8116 It is not possible to use gnatlink in this case, since it cannot locate
8117 the binder file.
8118
8119 @item ^-r^/RESTRICTION_LIST^
8120 @cindex @option{^-r^/RESTRICTION_LIST^} (@code{gnatbind})
8121 Generate list of @code{pragma Restrictions} that could be applied to
8122 the current unit. This is useful for code audit purposes, and also may
8123 be used to improve code generation in some cases.
8124
8125 @end table
8126
8127 @node Binding with Non-Ada Main Programs
8128 @subsection Binding with Non-Ada Main Programs
8129
8130 @noindent
8131 In our description so far we have assumed that the main
8132 program is in Ada, and that the task of the binder is to generate a
8133 corresponding function @code{main} that invokes this Ada main
8134 program. GNAT also supports the building of executable programs where
8135 the main program is not in Ada, but some of the called routines are
8136 written in Ada and compiled using GNAT (@pxref{Mixed Language Programming}).
8137 The following switch is used in this situation:
8138
8139 @table @option
8140 @item ^-n^/NOMAIN^
8141 @cindex @option{^-n^/NOMAIN^} (@code{gnatbind})
8142 No main program. The main program is not in Ada.
8143 @end table
8144
8145 @noindent
8146 In this case, most of the functions of the binder are still required,
8147 but instead of generating a main program, the binder generates a file
8148 containing the following callable routines:
8149
8150 @table @code
8151 @item adainit
8152 @findex adainit
8153 You must call this routine to initialize the Ada part of the program by
8154 calling the necessary elaboration routines. A call to @code{adainit} is
8155 required before the first call to an Ada subprogram.
8156
8157 Note that it is assumed that the basic execution environment must be setup
8158 to be appropriate for Ada execution at the point where the first Ada
8159 subprogram is called. In particular, if the Ada code will do any
8160 floating-point operations, then the FPU must be setup in an appropriate
8161 manner. For the case of the x86, for example, full precision mode is
8162 required. The procedure GNAT.Float_Control.Reset may be used to ensure
8163 that the FPU is in the right state.
8164
8165 @item adafinal
8166 @findex adafinal
8167 You must call this routine to perform any library-level finalization
8168 required by the Ada subprograms. A call to @code{adafinal} is required
8169 after the last call to an Ada subprogram, and before the program
8170 terminates.
8171 @end table
8172
8173 @noindent
8174 If the @option{^-n^/NOMAIN^} switch
8175 @cindex @option{^-n^/NOMAIN^} (@command{gnatbind})
8176 @cindex Binder, multiple input files
8177 is given, more than one ALI file may appear on
8178 the command line for @code{gnatbind}. The normal @dfn{closure}
8179 calculation is performed for each of the specified units. Calculating
8180 the closure means finding out the set of units involved by tracing
8181 @code{with} references. The reason it is necessary to be able to
8182 specify more than one ALI file is that a given program may invoke two or
8183 more quite separate groups of Ada units.
8184
8185 The binder takes the name of its output file from the last specified ALI
8186 file, unless overridden by the use of the @option{^-o file^/OUTPUT=file^}.
8187 @cindex @option{^-o^/OUTPUT^} (@command{gnatbind})
8188 The output is an Ada unit in source form that can
8189 be compiled with GNAT unless the -C switch is used in which case the
8190 output is a C source file, which must be compiled using the C compiler.
8191 This compilation occurs automatically as part of the @command{gnatlink}
8192 processing.
8193
8194 Currently the GNAT run time requires a FPU using 80 bits mode
8195 precision. Under targets where this is not the default it is required to
8196 call GNAT.Float_Control.Reset before using floating point numbers (this
8197 include float computation, float input and output) in the Ada code. A
8198 side effect is that this could be the wrong mode for the foreign code
8199 where floating point computation could be broken after this call.
8200
8201 @node Binding Programs with No Main Subprogram
8202 @subsection Binding Programs with No Main Subprogram
8203
8204 @noindent
8205 It is possible to have an Ada program which does not have a main
8206 subprogram. This program will call the elaboration routines of all the
8207 packages, then the finalization routines.
8208
8209 The following switch is used to bind programs organized in this manner:
8210
8211 @table @option
8212 @item ^-z^/ZERO_MAIN^
8213 @cindex @option{^-z^/ZERO_MAIN^} (@code{gnatbind})
8214 Normally the binder checks that the unit name given on the command line
8215 corresponds to a suitable main subprogram. When this switch is used,
8216 a list of ALI files can be given, and the execution of the program
8217 consists of elaboration of these units in an appropriate order. Note
8218 that the default wide character encoding method for standard Text_IO
8219 files is always set to Brackets if this switch is set (you can use
8220 the binder switch
8221 @option{^-Wx^WIDE_CHARACTER_ENCODING^} to override this default).
8222 @end table
8223
8224 @node Command-Line Access
8225 @section Command-Line Access
8226
8227 @noindent
8228 The package @code{Ada.Command_Line} provides access to the command-line
8229 arguments and program name. In order for this interface to operate
8230 correctly, the two variables
8231
8232 @smallexample
8233 @group
8234 int gnat_argc;
8235 char **gnat_argv;
8236 @end group
8237 @end smallexample
8238
8239 @noindent
8240 @findex gnat_argv
8241 @findex gnat_argc
8242 are declared in one of the GNAT library routines. These variables must
8243 be set from the actual @code{argc} and @code{argv} values passed to the
8244 main program. With no @option{^n^/NOMAIN^} present, @code{gnatbind}
8245 generates the C main program to automatically set these variables.
8246 If the @option{^n^/NOMAIN^} switch is used, there is no automatic way to
8247 set these variables. If they are not set, the procedures in
8248 @code{Ada.Command_Line} will not be available, and any attempt to use
8249 them will raise @code{Constraint_Error}. If command line access is
8250 required, your main program must set @code{gnat_argc} and
8251 @code{gnat_argv} from the @code{argc} and @code{argv} values passed to
8252 it.
8253
8254 @node Search Paths for gnatbind
8255 @section Search Paths for @code{gnatbind}
8256
8257 @noindent
8258 The binder takes the name of an ALI file as its argument and needs to
8259 locate source files as well as other ALI files to verify object consistency.
8260
8261 For source files, it follows exactly the same search rules as @command{gcc}
8262 (@pxref{Search Paths and the Run-Time Library (RTL)}). For ALI files the
8263 directories searched are:
8264
8265 @enumerate
8266 @item
8267 The directory containing the ALI file named in the command line, unless
8268 the switch @option{^-I-^/NOCURRENT_DIRECTORY^} is specified.
8269
8270 @item
8271 All directories specified by @option{^-I^/SEARCH^}
8272 switches on the @code{gnatbind}
8273 command line, in the order given.
8274
8275 @item
8276 @findex ADA_PRJ_OBJECTS_FILE
8277 Each of the directories listed in the text file whose name is given
8278 by the @env{ADA_PRJ_OBJECTS_FILE} ^environment variable^logical name^.
8279
8280 @noindent
8281 @env{ADA_PRJ_OBJECTS_FILE} is normally set by gnatmake or by the ^gnat^GNAT^
8282 driver when project files are used. It should not normally be set
8283 by other means.
8284
8285 @item
8286 @findex ADA_OBJECTS_PATH
8287 Each of the directories listed in the value of the
8288 @env{ADA_OBJECTS_PATH} ^environment variable^logical name^.
8289 @ifset unw
8290 Construct this value
8291 exactly as the @env{PATH} environment variable: a list of directory
8292 names separated by colons (semicolons when working with the NT version
8293 of GNAT).
8294 @end ifset
8295 @ifset vms
8296 Normally, define this value as a logical name containing a comma separated
8297 list of directory names.
8298
8299 This variable can also be defined by means of an environment string
8300 (an argument to the HP C exec* set of functions).
8301
8302 Logical Name:
8303 @smallexample
8304 DEFINE ANOTHER_PATH FOO:[BAG]
8305 DEFINE ADA_OBJECTS_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
8306 @end smallexample
8307
8308 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
8309 first, followed by the standard Ada
8310 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADALIB].
8311 If this is not redefined, the user will obtain the HP Ada 83 IO packages
8312 (Text_IO, Sequential_IO, etc)
8313 instead of the standard Ada packages. Thus, in order to get the standard Ada
8314 packages by default, ADA_OBJECTS_PATH must be redefined.
8315 @end ifset
8316
8317 @item
8318 The content of the @file{ada_object_path} file which is part of the GNAT
8319 installation tree and is used to store standard libraries such as the
8320 GNAT Run Time Library (RTL) unless the switch @option{-nostdlib} is
8321 specified.
8322 @ifclear vms
8323 @ref{Installing a library}
8324 @end ifclear
8325 @end enumerate
8326
8327 @noindent
8328 In the binder the switch @option{^-I^/SEARCH^}
8329 @cindex @option{^-I^/SEARCH^} (@command{gnatbind})
8330 is used to specify both source and
8331 library file paths. Use @option{^-aI^/SOURCE_SEARCH^}
8332 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatbind})
8333 instead if you want to specify
8334 source paths only, and @option{^-aO^/LIBRARY_SEARCH^}
8335 @cindex @option{^-aO^/LIBRARY_SEARCH^} (@command{gnatbind})
8336 if you want to specify library paths
8337 only. This means that for the binder
8338 @option{^-I^/SEARCH=^}@var{dir} is equivalent to
8339 @option{^-aI^/SOURCE_SEARCH=^}@var{dir}
8340 @option{^-aO^/OBJECT_SEARCH=^}@var{dir}.
8341 The binder generates the bind file (a C language source file) in the
8342 current working directory.
8343
8344 @findex Ada
8345 @findex System
8346 @findex Interfaces
8347 @findex GNAT
8348 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
8349 children make up the GNAT Run-Time Library, together with the package
8350 GNAT and its children, which contain a set of useful additional
8351 library functions provided by GNAT. The sources for these units are
8352 needed by the compiler and are kept together in one directory. The ALI
8353 files and object files generated by compiling the RTL are needed by the
8354 binder and the linker and are kept together in one directory, typically
8355 different from the directory containing the sources. In a normal
8356 installation, you need not specify these directory names when compiling
8357 or binding. Either the environment variables or the built-in defaults
8358 cause these files to be found.
8359
8360 Besides simplifying access to the RTL, a major use of search paths is
8361 in compiling sources from multiple directories. This can make
8362 development environments much more flexible.
8363
8364 @node Examples of gnatbind Usage
8365 @section Examples of @code{gnatbind} Usage
8366
8367 @noindent
8368 This section contains a number of examples of using the GNAT binding
8369 utility @code{gnatbind}.
8370
8371 @table @code
8372 @item gnatbind hello
8373 The main program @code{Hello} (source program in @file{hello.adb}) is
8374 bound using the standard switch settings. The generated main program is
8375 @file{b~hello.adb}. This is the normal, default use of the binder.
8376
8377 @ifclear vms
8378 @item gnatbind hello -o mainprog.adb
8379 @end ifclear
8380 @ifset vms
8381 @item gnatbind HELLO.ALI /OUTPUT=Mainprog.ADB
8382 @end ifset
8383 The main program @code{Hello} (source program in @file{hello.adb}) is
8384 bound using the standard switch settings. The generated main program is
8385 @file{mainprog.adb} with the associated spec in
8386 @file{mainprog.ads}. Note that you must specify the body here not the
8387 spec, in the case where the output is in Ada. Note that if this option
8388 is used, then linking must be done manually, since gnatlink will not
8389 be able to find the generated file.
8390
8391 @ifclear vms
8392 @item gnatbind main -C -o mainprog.c -x
8393 @end ifclear
8394 @ifset vms
8395 @item gnatbind MAIN.ALI /BIND_FILE=C /OUTPUT=Mainprog.C /READ_SOURCES=NONE
8396 @end ifset
8397 The main program @code{Main} (source program in
8398 @file{main.adb}) is bound, excluding source files from the
8399 consistency checking, generating
8400 the file @file{mainprog.c}.
8401
8402 @ifclear vms
8403 @item gnatbind -x main_program -C -o mainprog.c
8404 This command is exactly the same as the previous example. Switches may
8405 appear anywhere in the command line, and single letter switches may be
8406 combined into a single switch.
8407 @end ifclear
8408
8409 @ifclear vms
8410 @item gnatbind -n math dbase -C -o ada-control.c
8411 @end ifclear
8412 @ifset vms
8413 @item gnatbind /NOMAIN math dbase /BIND_FILE=C /OUTPUT=ada-control.c
8414 @end ifset
8415 The main program is in a language other than Ada, but calls to
8416 subprograms in packages @code{Math} and @code{Dbase} appear. This call
8417 to @code{gnatbind} generates the file @file{ada-control.c} containing
8418 the @code{adainit} and @code{adafinal} routines to be called before and
8419 after accessing the Ada units.
8420 @end table
8421
8422 @c ------------------------------------
8423 @node Linking Using gnatlink
8424 @chapter Linking Using @command{gnatlink}
8425 @c ------------------------------------
8426 @findex gnatlink
8427
8428 @noindent
8429 This chapter discusses @command{gnatlink}, a tool that links
8430 an Ada program and builds an executable file. This utility
8431 invokes the system linker ^(via the @command{gcc} command)^^
8432 with a correct list of object files and library references.
8433 @command{gnatlink} automatically determines the list of files and
8434 references for the Ada part of a program. It uses the binder file
8435 generated by the @command{gnatbind} to determine this list.
8436
8437 Note: to invoke @code{gnatlink} with a project file, use the @code{gnat}
8438 driver (see @ref{The GNAT Driver and Project Files}).
8439
8440 @menu
8441 * Running gnatlink::
8442 * Switches for gnatlink::
8443 @end menu
8444
8445 @node Running gnatlink
8446 @section Running @command{gnatlink}
8447
8448 @noindent
8449 The form of the @command{gnatlink} command is
8450
8451 @smallexample
8452 $ gnatlink @ovar{switches} @var{mainprog}@r{[}.ali@r{]}
8453 @ovar{non-Ada objects} @ovar{linker options}
8454 @end smallexample
8455
8456 @noindent
8457 The arguments of @command{gnatlink} (switches, main @file{ALI} file,
8458 non-Ada objects
8459 or linker options) may be in any order, provided that no non-Ada object may
8460 be mistaken for a main @file{ALI} file.
8461 Any file name @file{F} without the @file{.ali}
8462 extension will be taken as the main @file{ALI} file if a file exists
8463 whose name is the concatenation of @file{F} and @file{.ali}.
8464
8465 @noindent
8466 @file{@var{mainprog}.ali} references the ALI file of the main program.
8467 The @file{.ali} extension of this file can be omitted. From this
8468 reference, @command{gnatlink} locates the corresponding binder file
8469 @file{b~@var{mainprog}.adb} and, using the information in this file along
8470 with the list of non-Ada objects and linker options, constructs a
8471 linker command file to create the executable.
8472
8473 The arguments other than the @command{gnatlink} switches and the main
8474 @file{ALI} file are passed to the linker uninterpreted.
8475 They typically include the names of
8476 object files for units written in other languages than Ada and any library
8477 references required to resolve references in any of these foreign language
8478 units, or in @code{Import} pragmas in any Ada units.
8479
8480 @var{linker options} is an optional list of linker specific
8481 switches.
8482 The default linker called by gnatlink is @command{gcc} which in
8483 turn calls the appropriate system linker.
8484 Standard options for the linker such as @option{-lmy_lib} or
8485 @option{-Ldir} can be added as is.
8486 For options that are not recognized by
8487 @command{gcc} as linker options, use the @command{gcc} switches
8488 @option{-Xlinker} or @option{-Wl,}.
8489 Refer to the GCC documentation for
8490 details. Here is an example showing how to generate a linker map:
8491
8492 @smallexample
8493 $ ^gnatlink my_prog -Wl,-Map,MAPFILE^GNAT LINK my_prog.ali /MAP^
8494 @end smallexample
8495
8496 Using @var{linker options} it is possible to set the program stack and
8497 heap size.
8498 @ifset unw
8499 See @ref{Setting Stack Size from gnatlink} and
8500 @ref{Setting Heap Size from gnatlink}.
8501 @end ifset
8502
8503 @command{gnatlink} determines the list of objects required by the Ada
8504 program and prepends them to the list of objects passed to the linker.
8505 @command{gnatlink} also gathers any arguments set by the use of
8506 @code{pragma Linker_Options} and adds them to the list of arguments
8507 presented to the linker.
8508
8509 @ifset vms
8510 @command{gnatlink} accepts the following types of extra files on the command
8511 line: objects (@file{.OBJ}), libraries (@file{.OLB}), sharable images
8512 (@file{.EXE}), and options files (@file{.OPT}). These are recognized and
8513 handled according to their extension.
8514 @end ifset
8515
8516 @node Switches for gnatlink
8517 @section Switches for @command{gnatlink}
8518
8519 @noindent
8520 The following switches are available with the @command{gnatlink} utility:
8521
8522 @table @option
8523 @c !sort!
8524
8525 @item --version
8526 @cindex @option{--version} @command{gnatlink}
8527 Display Copyright and version, then exit disregarding all other options.
8528
8529 @item --help
8530 @cindex @option{--help} @command{gnatlink}
8531 If @option{--version} was not used, display usage, then exit disregarding
8532 all other options.
8533
8534 @item ^-A^/BIND_FILE=ADA^
8535 @cindex @option{^-A^/BIND_FILE=ADA^} (@command{gnatlink})
8536 The binder has generated code in Ada. This is the default.
8537
8538 @item ^-C^/BIND_FILE=C^
8539 @cindex @option{^-C^/BIND_FILE=C^} (@command{gnatlink})
8540 If instead of generating a file in Ada, the binder has generated one in
8541 C, then the linker needs to know about it. Use this switch to signal
8542 to @command{gnatlink} that the binder has generated C code rather than
8543 Ada code.
8544
8545 @item ^-f^/FORCE_OBJECT_FILE_LIST^
8546 @cindex Command line length
8547 @cindex @option{^-f^/FORCE_OBJECT_FILE_LIST^} (@command{gnatlink})
8548 On some targets, the command line length is limited, and @command{gnatlink}
8549 will generate a separate file for the linker if the list of object files
8550 is too long.
8551 The @option{^-f^/FORCE_OBJECT_FILE_LIST^} switch forces this file
8552 to be generated even if
8553 the limit is not exceeded. This is useful in some cases to deal with
8554 special situations where the command line length is exceeded.
8555
8556 @item ^-g^/DEBUG^
8557 @cindex Debugging information, including
8558 @cindex @option{^-g^/DEBUG^} (@command{gnatlink})
8559 The option to include debugging information causes the Ada bind file (in
8560 other words, @file{b~@var{mainprog}.adb}) to be compiled with
8561 @option{^-g^/DEBUG^}.
8562 In addition, the binder does not delete the @file{b~@var{mainprog}.adb},
8563 @file{b~@var{mainprog}.o} and @file{b~@var{mainprog}.ali} files.
8564 Without @option{^-g^/DEBUG^}, the binder removes these files by
8565 default. The same procedure apply if a C bind file was generated using
8566 @option{^-C^/BIND_FILE=C^} @code{gnatbind} option, in this case the filenames
8567 are @file{b_@var{mainprog}.c} and @file{b_@var{mainprog}.o}.
8568
8569 @item ^-n^/NOCOMPILE^
8570 @cindex @option{^-n^/NOCOMPILE^} (@command{gnatlink})
8571 Do not compile the file generated by the binder. This may be used when
8572 a link is rerun with different options, but there is no need to recompile
8573 the binder file.
8574
8575 @item ^-v^/VERBOSE^
8576 @cindex @option{^-v^/VERBOSE^} (@command{gnatlink})
8577 Causes additional information to be output, including a full list of the
8578 included object files. This switch option is most useful when you want
8579 to see what set of object files are being used in the link step.
8580
8581 @item ^-v -v^/VERBOSE/VERBOSE^
8582 @cindex @option{^-v -v^/VERBOSE/VERBOSE^} (@command{gnatlink})
8583 Very verbose mode. Requests that the compiler operate in verbose mode when
8584 it compiles the binder file, and that the system linker run in verbose mode.
8585
8586 @item ^-o ^/EXECUTABLE=^@var{exec-name}
8587 @cindex @option{^-o^/EXECUTABLE^} (@command{gnatlink})
8588 @var{exec-name} specifies an alternate name for the generated
8589 executable program. If this switch is omitted, the executable has the same
8590 name as the main unit. For example, @code{gnatlink try.ali} creates
8591 an executable called @file{^try^TRY.EXE^}.
8592
8593 @ifclear vms
8594 @item -b @var{target}
8595 @cindex @option{-b} (@command{gnatlink})
8596 Compile your program to run on @var{target}, which is the name of a
8597 system configuration. You must have a GNAT cross-compiler built if
8598 @var{target} is not the same as your host system.
8599
8600 @item -B@var{dir}
8601 @cindex @option{-B} (@command{gnatlink})
8602 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
8603 from @var{dir} instead of the default location. Only use this switch
8604 when multiple versions of the GNAT compiler are available.
8605 @xref{Directory Options,,, gcc, The GNU Compiler Collection},
8606 for further details. You would normally use the @option{-b} or
8607 @option{-V} switch instead.
8608
8609 @item --GCC=@var{compiler_name}
8610 @cindex @option{--GCC=compiler_name} (@command{gnatlink})
8611 Program used for compiling the binder file. The default is
8612 @command{gcc}. You need to use quotes around @var{compiler_name} if
8613 @code{compiler_name} contains spaces or other separator characters.
8614 As an example @option{--GCC="foo -x -y"} will instruct @command{gnatlink} to
8615 use @code{foo -x -y} as your compiler. Note that switch @option{-c} is always
8616 inserted after your command name. Thus in the above example the compiler
8617 command that will be used by @command{gnatlink} will be @code{foo -c -x -y}.
8618 A limitation of this syntax is that the name and path name of the executable
8619 itself must not include any embedded spaces. If the compiler executable is
8620 different from the default one (gcc or <prefix>-gcc), then the back-end
8621 switches in the ALI file are not used to compile the binder generated source.
8622 For example, this is the case with @option{--GCC="foo -x -y"}. But the back end
8623 switches will be used for @option{--GCC="gcc -gnatv"}. If several
8624 @option{--GCC=compiler_name} are used, only the last @var{compiler_name}
8625 is taken into account. However, all the additional switches are also taken
8626 into account. Thus,
8627 @option{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
8628 @option{--GCC="bar -x -y -z -t"}.
8629
8630 @item --LINK=@var{name}
8631 @cindex @option{--LINK=} (@command{gnatlink})
8632 @var{name} is the name of the linker to be invoked. This is especially
8633 useful in mixed language programs since languages such as C++ require
8634 their own linker to be used. When this switch is omitted, the default
8635 name for the linker is @command{gcc}. When this switch is used, the
8636 specified linker is called instead of @command{gcc} with exactly the same
8637 parameters that would have been passed to @command{gcc} so if the desired
8638 linker requires different parameters it is necessary to use a wrapper
8639 script that massages the parameters before invoking the real linker. It
8640 may be useful to control the exact invocation by using the verbose
8641 switch.
8642
8643 @end ifclear
8644
8645 @ifset vms
8646 @item /DEBUG=TRACEBACK
8647 @cindex @code{/DEBUG=TRACEBACK} (@command{gnatlink})
8648 This qualifier causes sufficient information to be included in the
8649 executable file to allow a traceback, but does not include the full
8650 symbol information needed by the debugger.
8651
8652 @item /IDENTIFICATION="<string>"
8653 @code{"<string>"} specifies the string to be stored in the image file
8654 identification field in the image header.
8655 It overrides any pragma @code{Ident} specified string.
8656
8657 @item /NOINHIBIT-EXEC
8658 Generate the executable file even if there are linker warnings.
8659
8660 @item /NOSTART_FILES
8661 Don't link in the object file containing the ``main'' transfer address.
8662 Used when linking with a foreign language main program compiled with an
8663 HP compiler.
8664
8665 @item /STATIC
8666 Prefer linking with object libraries over sharable images, even without
8667 /DEBUG.
8668 @end ifset
8669
8670 @end table
8671
8672 @node The GNAT Make Program gnatmake
8673 @chapter The GNAT Make Program @command{gnatmake}
8674 @findex gnatmake
8675
8676 @menu
8677 * Running gnatmake::
8678 * Switches for gnatmake::
8679 * Mode Switches for gnatmake::
8680 * Notes on the Command Line::
8681 * How gnatmake Works::
8682 * Examples of gnatmake Usage::
8683 @end menu
8684 @noindent
8685 A typical development cycle when working on an Ada program consists of
8686 the following steps:
8687
8688 @enumerate
8689 @item
8690 Edit some sources to fix bugs.
8691
8692 @item
8693 Add enhancements.
8694
8695 @item
8696 Compile all sources affected.
8697
8698 @item
8699 Rebind and relink.
8700
8701 @item
8702 Test.
8703 @end enumerate
8704
8705 @noindent
8706 The third step can be tricky, because not only do the modified files
8707 @cindex Dependency rules
8708 have to be compiled, but any files depending on these files must also be
8709 recompiled. The dependency rules in Ada can be quite complex, especially
8710 in the presence of overloading, @code{use} clauses, generics and inlined
8711 subprograms.
8712
8713 @command{gnatmake} automatically takes care of the third and fourth steps
8714 of this process. It determines which sources need to be compiled,
8715 compiles them, and binds and links the resulting object files.
8716
8717 Unlike some other Ada make programs, the dependencies are always
8718 accurately recomputed from the new sources. The source based approach of
8719 the GNAT compilation model makes this possible. This means that if
8720 changes to the source program cause corresponding changes in
8721 dependencies, they will always be tracked exactly correctly by
8722 @command{gnatmake}.
8723
8724 @node Running gnatmake
8725 @section Running @command{gnatmake}
8726
8727 @noindent
8728 The usual form of the @command{gnatmake} command is
8729
8730 @smallexample
8731 $ gnatmake @ovar{switches} @var{file_name}
8732 @ovar{file_names} @ovar{mode_switches}
8733 @end smallexample
8734
8735 @noindent
8736 The only required argument is one @var{file_name}, which specifies
8737 a compilation unit that is a main program. Several @var{file_names} can be
8738 specified: this will result in several executables being built.
8739 If @code{switches} are present, they can be placed before the first
8740 @var{file_name}, between @var{file_names} or after the last @var{file_name}.
8741 If @var{mode_switches} are present, they must always be placed after
8742 the last @var{file_name} and all @code{switches}.
8743
8744 If you are using standard file extensions (@file{.adb} and @file{.ads}), then the
8745 extension may be omitted from the @var{file_name} arguments. However, if
8746 you are using non-standard extensions, then it is required that the
8747 extension be given. A relative or absolute directory path can be
8748 specified in a @var{file_name}, in which case, the input source file will
8749 be searched for in the specified directory only. Otherwise, the input
8750 source file will first be searched in the directory where
8751 @command{gnatmake} was invoked and if it is not found, it will be search on
8752 the source path of the compiler as described in
8753 @ref{Search Paths and the Run-Time Library (RTL)}.
8754
8755 All @command{gnatmake} output (except when you specify
8756 @option{^-M^/DEPENDENCIES_LIST^}) is to
8757 @file{stderr}. The output produced by the
8758 @option{^-M^/DEPENDENCIES_LIST^} switch is send to
8759 @file{stdout}.
8760
8761 @node Switches for gnatmake
8762 @section Switches for @command{gnatmake}
8763
8764 @noindent
8765 You may specify any of the following switches to @command{gnatmake}:
8766
8767 @table @option
8768 @c !sort!
8769
8770 @item --version
8771 @cindex @option{--version} @command{gnatmake}
8772 Display Copyright and version, then exit disregarding all other options.
8773
8774 @item --help
8775 @cindex @option{--help} @command{gnatmake}
8776 If @option{--version} was not used, display usage, then exit disregarding
8777 all other options.
8778
8779 @ifclear vms
8780 @item --GCC=@var{compiler_name}
8781 @cindex @option{--GCC=compiler_name} (@command{gnatmake})
8782 Program used for compiling. The default is `@command{gcc}'. You need to use
8783 quotes around @var{compiler_name} if @code{compiler_name} contains
8784 spaces or other separator characters. As an example @option{--GCC="foo -x
8785 -y"} will instruct @command{gnatmake} to use @code{foo -x -y} as your
8786 compiler. A limitation of this syntax is that the name and path name of
8787 the executable itself must not include any embedded spaces. Note that
8788 switch @option{-c} is always inserted after your command name. Thus in the
8789 above example the compiler command that will be used by @command{gnatmake}
8790 will be @code{foo -c -x -y}. If several @option{--GCC=compiler_name} are
8791 used, only the last @var{compiler_name} is taken into account. However,
8792 all the additional switches are also taken into account. Thus,
8793 @option{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
8794 @option{--GCC="bar -x -y -z -t"}.
8795
8796 @item --GNATBIND=@var{binder_name}
8797 @cindex @option{--GNATBIND=binder_name} (@command{gnatmake})
8798 Program used for binding. The default is `@code{gnatbind}'. You need to
8799 use quotes around @var{binder_name} if @var{binder_name} contains spaces
8800 or other separator characters. As an example @option{--GNATBIND="bar -x
8801 -y"} will instruct @command{gnatmake} to use @code{bar -x -y} as your
8802 binder. Binder switches that are normally appended by @command{gnatmake}
8803 to `@code{gnatbind}' are now appended to the end of @code{bar -x -y}.
8804 A limitation of this syntax is that the name and path name of the executable
8805 itself must not include any embedded spaces.
8806
8807 @item --GNATLINK=@var{linker_name}
8808 @cindex @option{--GNATLINK=linker_name} (@command{gnatmake})
8809 Program used for linking. The default is `@command{gnatlink}'. You need to
8810 use quotes around @var{linker_name} if @var{linker_name} contains spaces
8811 or other separator characters. As an example @option{--GNATLINK="lan -x
8812 -y"} will instruct @command{gnatmake} to use @code{lan -x -y} as your
8813 linker. Linker switches that are normally appended by @command{gnatmake} to
8814 `@command{gnatlink}' are now appended to the end of @code{lan -x -y}.
8815 A limitation of this syntax is that the name and path name of the executable
8816 itself must not include any embedded spaces.
8817
8818 @end ifclear
8819
8820 @item ^-a^/ALL_FILES^
8821 @cindex @option{^-a^/ALL_FILES^} (@command{gnatmake})
8822 Consider all files in the make process, even the GNAT internal system
8823 files (for example, the predefined Ada library files), as well as any
8824 locked files. Locked files are files whose ALI file is write-protected.
8825 By default,
8826 @command{gnatmake} does not check these files,
8827 because the assumption is that the GNAT internal files are properly up
8828 to date, and also that any write protected ALI files have been properly
8829 installed. Note that if there is an installation problem, such that one
8830 of these files is not up to date, it will be properly caught by the
8831 binder.
8832 You may have to specify this switch if you are working on GNAT
8833 itself. The switch @option{^-a^/ALL_FILES^} is also useful
8834 in conjunction with @option{^-f^/FORCE_COMPILE^}
8835 if you need to recompile an entire application,
8836 including run-time files, using special configuration pragmas,
8837 such as a @code{Normalize_Scalars} pragma.
8838
8839 By default
8840 @code{gnatmake ^-a^/ALL_FILES^} compiles all GNAT
8841 internal files with
8842 @ifclear vms
8843 @code{gcc -c -gnatpg} rather than @code{gcc -c}.
8844 @end ifclear
8845 @ifset vms
8846 the @code{/CHECKS=SUPPRESS_ALL /STYLE_CHECKS=GNAT} switch.
8847 @end ifset
8848
8849 @item ^-b^/ACTIONS=BIND^
8850 @cindex @option{^-b^/ACTIONS=BIND^} (@command{gnatmake})
8851 Bind only. Can be combined with @option{^-c^/ACTIONS=COMPILE^} to do
8852 compilation and binding, but no link.
8853 Can be combined with @option{^-l^/ACTIONS=LINK^}
8854 to do binding and linking. When not combined with
8855 @option{^-c^/ACTIONS=COMPILE^}
8856 all the units in the closure of the main program must have been previously
8857 compiled and must be up to date. The root unit specified by @var{file_name}
8858 may be given without extension, with the source extension or, if no GNAT
8859 Project File is specified, with the ALI file extension.
8860
8861 @item ^-c^/ACTIONS=COMPILE^
8862 @cindex @option{^-c^/ACTIONS=COMPILE^} (@command{gnatmake})
8863 Compile only. Do not perform binding, except when @option{^-b^/ACTIONS=BIND^}
8864 is also specified. Do not perform linking, except if both
8865 @option{^-b^/ACTIONS=BIND^} and
8866 @option{^-l^/ACTIONS=LINK^} are also specified.
8867 If the root unit specified by @var{file_name} is not a main unit, this is the
8868 default. Otherwise @command{gnatmake} will attempt binding and linking
8869 unless all objects are up to date and the executable is more recent than
8870 the objects.
8871
8872 @item ^-C^/MAPPING^
8873 @cindex @option{^-C^/MAPPING^} (@command{gnatmake})
8874 Use a temporary mapping file. A mapping file is a way to communicate to the
8875 compiler two mappings: from unit names to file names (without any directory
8876 information) and from file names to path names (with full directory
8877 information). These mappings are used by the compiler to short-circuit the path
8878 search. When @command{gnatmake} is invoked with this switch, it will create
8879 a temporary mapping file, initially populated by the project manager,
8880 if @option{^-P^/PROJECT_FILE^} is used, otherwise initially empty.
8881 Each invocation of the compiler will add the newly accessed sources to the
8882 mapping file. This will improve the source search during the next invocation
8883 of the compiler.
8884
8885 @item ^-C=^/USE_MAPPING_FILE=^@var{file}
8886 @cindex @option{^-C=^/USE_MAPPING^} (@command{gnatmake})
8887 Use a specific mapping file. The file, specified as a path name (absolute or
8888 relative) by this switch, should already exist, otherwise the switch is
8889 ineffective. The specified mapping file will be communicated to the compiler.
8890 This switch is not compatible with a project file
8891 (^-P^/PROJECT_FILE=^@var{file}) or with multiple compiling processes
8892 (^-j^/PROCESSES=^nnn, when nnn is greater than 1).
8893
8894 @item ^-d^/DISPLAY_PROGRESS^
8895 @cindex @option{^-d^/DISPLAY_PROGRESS^} (@command{gnatmake})
8896 Display progress for each source, up to date or not, as a single line
8897
8898 @smallexample
8899 completed x out of y (zz%)
8900 @end smallexample
8901
8902 If the file needs to be compiled this is displayed after the invocation of
8903 the compiler. These lines are displayed even in quiet output mode.
8904
8905 @item ^-D ^/DIRECTORY_OBJECTS=^@var{dir}
8906 @cindex @option{^-D^/DIRECTORY_OBJECTS^} (@command{gnatmake})
8907 Put all object files and ALI file in directory @var{dir}.
8908 If the @option{^-D^/DIRECTORY_OBJECTS^} switch is not used, all object files
8909 and ALI files go in the current working directory.
8910
8911 This switch cannot be used when using a project file.
8912
8913 @ifclear vms
8914 @item -eL
8915 @cindex @option{-eL} (@command{gnatmake})
8916 Follow all symbolic links when processing project files.
8917 @end ifclear
8918
8919 @item ^-eS^/STANDARD_OUTPUT_FOR_COMMANDS^
8920 @cindex @option{^-eS^/STANDARD_OUTPUT_FOR_COMMANDS^} (@command{gnatmake})
8921 Output the commands for the compiler, the binder and the linker
8922 on ^standard output^SYS$OUTPUT^,
8923 instead of ^standard error^SYS$ERROR^.
8924
8925 @item ^-f^/FORCE_COMPILE^
8926 @cindex @option{^-f^/FORCE_COMPILE^} (@command{gnatmake})
8927 Force recompilations. Recompile all sources, even though some object
8928 files may be up to date, but don't recompile predefined or GNAT internal
8929 files or locked files (files with a write-protected ALI file),
8930 unless the @option{^-a^/ALL_FILES^} switch is also specified.
8931
8932 @item ^-F^/FULL_PATH_IN_BRIEF_MESSAGES^
8933 @cindex @option{^-F^/FULL_PATH_IN_BRIEF_MESSAGES^} (@command{gnatmake})
8934 When using project files, if some errors or warnings are detected during
8935 parsing and verbose mode is not in effect (no use of switch
8936 ^-v^/VERBOSE^), then error lines start with the full path name of the project
8937 file, rather than its simple file name.
8938
8939 @item ^-g^/DEBUG^
8940 @cindex @option{^-g^/DEBUG^} (@command{gnatmake})
8941 Enable debugging. This switch is simply passed to the compiler and to the
8942 linker.
8943
8944 @item ^-i^/IN_PLACE^
8945 @cindex @option{^-i^/IN_PLACE^} (@command{gnatmake})
8946 In normal mode, @command{gnatmake} compiles all object files and ALI files
8947 into the current directory. If the @option{^-i^/IN_PLACE^} switch is used,
8948 then instead object files and ALI files that already exist are overwritten
8949 in place. This means that once a large project is organized into separate
8950 directories in the desired manner, then @command{gnatmake} will automatically
8951 maintain and update this organization. If no ALI files are found on the
8952 Ada object path (@ref{Search Paths and the Run-Time Library (RTL)}),
8953 the new object and ALI files are created in the
8954 directory containing the source being compiled. If another organization
8955 is desired, where objects and sources are kept in different directories,
8956 a useful technique is to create dummy ALI files in the desired directories.
8957 When detecting such a dummy file, @command{gnatmake} will be forced to
8958 recompile the corresponding source file, and it will be put the resulting
8959 object and ALI files in the directory where it found the dummy file.
8960
8961 @item ^-j^/PROCESSES=^@var{n}
8962 @cindex @option{^-j^/PROCESSES^} (@command{gnatmake})
8963 @cindex Parallel make
8964 Use @var{n} processes to carry out the (re)compilations. On a
8965 multiprocessor machine compilations will occur in parallel. In the
8966 event of compilation errors, messages from various compilations might
8967 get interspersed (but @command{gnatmake} will give you the full ordered
8968 list of failing compiles at the end). If this is problematic, rerun
8969 the make process with n set to 1 to get a clean list of messages.
8970
8971 @item ^-k^/CONTINUE_ON_ERROR^
8972 @cindex @option{^-k^/CONTINUE_ON_ERROR^} (@command{gnatmake})
8973 Keep going. Continue as much as possible after a compilation error. To
8974 ease the programmer's task in case of compilation errors, the list of
8975 sources for which the compile fails is given when @command{gnatmake}
8976 terminates.
8977
8978 If @command{gnatmake} is invoked with several @file{file_names} and with this
8979 switch, if there are compilation errors when building an executable,
8980 @command{gnatmake} will not attempt to build the following executables.
8981
8982 @item ^-l^/ACTIONS=LINK^
8983 @cindex @option{^-l^/ACTIONS=LINK^} (@command{gnatmake})
8984 Link only. Can be combined with @option{^-b^/ACTIONS=BIND^} to binding
8985 and linking. Linking will not be performed if combined with
8986 @option{^-c^/ACTIONS=COMPILE^}
8987 but not with @option{^-b^/ACTIONS=BIND^}.
8988 When not combined with @option{^-b^/ACTIONS=BIND^}
8989 all the units in the closure of the main program must have been previously
8990 compiled and must be up to date, and the main program needs to have been bound.
8991 The root unit specified by @var{file_name}
8992 may be given without extension, with the source extension or, if no GNAT
8993 Project File is specified, with the ALI file extension.
8994
8995 @item ^-m^/MINIMAL_RECOMPILATION^
8996 @cindex @option{^-m^/MINIMAL_RECOMPILATION^} (@command{gnatmake})
8997 Specify that the minimum necessary amount of recompilations
8998 be performed. In this mode @command{gnatmake} ignores time
8999 stamp differences when the only
9000 modifications to a source file consist in adding/removing comments,
9001 empty lines, spaces or tabs. This means that if you have changed the
9002 comments in a source file or have simply reformatted it, using this
9003 switch will tell @command{gnatmake} not to recompile files that depend on it
9004 (provided other sources on which these files depend have undergone no
9005 semantic modifications). Note that the debugging information may be
9006 out of date with respect to the sources if the @option{-m} switch causes
9007 a compilation to be switched, so the use of this switch represents a
9008 trade-off between compilation time and accurate debugging information.
9009
9010 @item ^-M^/DEPENDENCIES_LIST^
9011 @cindex Dependencies, producing list
9012 @cindex @option{^-M^/DEPENDENCIES_LIST^} (@command{gnatmake})
9013 Check if all objects are up to date. If they are, output the object
9014 dependences to @file{stdout} in a form that can be directly exploited in
9015 a @file{Makefile}. By default, each source file is prefixed with its
9016 (relative or absolute) directory name. This name is whatever you
9017 specified in the various @option{^-aI^/SOURCE_SEARCH^}
9018 and @option{^-I^/SEARCH^} switches. If you use
9019 @code{gnatmake ^-M^/DEPENDENCIES_LIST^}
9020 @option{^-q^/QUIET^}
9021 (see below), only the source file names,
9022 without relative paths, are output. If you just specify the
9023 @option{^-M^/DEPENDENCIES_LIST^}
9024 switch, dependencies of the GNAT internal system files are omitted. This
9025 is typically what you want. If you also specify
9026 the @option{^-a^/ALL_FILES^} switch,
9027 dependencies of the GNAT internal files are also listed. Note that
9028 dependencies of the objects in external Ada libraries (see switch
9029 @option{^-aL^/SKIP_MISSING=^}@var{dir} in the following list)
9030 are never reported.
9031
9032 @item ^-n^/DO_OBJECT_CHECK^
9033 @cindex @option{^-n^/DO_OBJECT_CHECK^} (@command{gnatmake})
9034 Don't compile, bind, or link. Checks if all objects are up to date.
9035 If they are not, the full name of the first file that needs to be
9036 recompiled is printed.
9037 Repeated use of this option, followed by compiling the indicated source
9038 file, will eventually result in recompiling all required units.
9039
9040 @item ^-o ^/EXECUTABLE=^@var{exec_name}
9041 @cindex @option{^-o^/EXECUTABLE^} (@command{gnatmake})
9042 Output executable name. The name of the final executable program will be
9043 @var{exec_name}. If the @option{^-o^/EXECUTABLE^} switch is omitted the default
9044 name for the executable will be the name of the input file in appropriate form
9045 for an executable file on the host system.
9046
9047 This switch cannot be used when invoking @command{gnatmake} with several
9048 @file{file_names}.
9049
9050 @item ^-p or --create-missing-dirs^/CREATE_MISSING_DIRS^
9051 @cindex @option{^-p^/CREATE_MISSING_DIRS^} (@command{gnatmake})
9052 When using project files (^-P^/PROJECT_FILE=^@var{project}), create
9053 automatically missing object directories, library directories and exec
9054 directories.
9055
9056 @item ^-P^/PROJECT_FILE=^@var{project}
9057 @cindex @option{^-P^/PROJECT_FILE^} (@command{gnatmake})
9058 Use project file @var{project}. Only one such switch can be used.
9059 @xref{gnatmake and Project Files}.
9060
9061 @item ^-q^/QUIET^
9062 @cindex @option{^-q^/QUIET^} (@command{gnatmake})
9063 Quiet. When this flag is not set, the commands carried out by
9064 @command{gnatmake} are displayed.
9065
9066 @item ^-s^/SWITCH_CHECK/^
9067 @cindex @option{^-s^/SWITCH_CHECK^} (@command{gnatmake})
9068 Recompile if compiler switches have changed since last compilation.
9069 All compiler switches but -I and -o are taken into account in the
9070 following way:
9071 orders between different ``first letter'' switches are ignored, but
9072 orders between same switches are taken into account. For example,
9073 @option{-O -O2} is different than @option{-O2 -O}, but @option{-g -O}
9074 is equivalent to @option{-O -g}.
9075
9076 This switch is recommended when Integrated Preprocessing is used.
9077
9078 @item ^-u^/UNIQUE^
9079 @cindex @option{^-u^/UNIQUE^} (@command{gnatmake})
9080 Unique. Recompile at most the main files. It implies -c. Combined with
9081 -f, it is equivalent to calling the compiler directly. Note that using
9082 ^-u^/UNIQUE^ with a project file and no main has a special meaning
9083 (@pxref{Project Files and Main Subprograms}).
9084
9085 @item ^-U^/ALL_PROJECTS^
9086 @cindex @option{^-U^/ALL_PROJECTS^} (@command{gnatmake})
9087 When used without a project file or with one or several mains on the command
9088 line, is equivalent to ^-u^/UNIQUE^. When used with a project file and no main
9089 on the command line, all sources of all project files are checked and compiled
9090 if not up to date, and libraries are rebuilt, if necessary.
9091
9092 @item ^-v^/REASONS^
9093 @cindex @option{^-v^/REASONS^} (@command{gnatmake})
9094 Verbose. Display the reason for all recompilations @command{gnatmake}
9095 decides are necessary, with the highest verbosity level.
9096
9097 @item ^-vl^/LOW_VERBOSITY^
9098 @cindex @option{^-vl^/LOW_VERBOSITY^} (@command{gnatmake})
9099 Verbosity level Low. Display fewer lines than in verbosity Medium.
9100
9101 @item ^-vm^/MEDIUM_VERBOSITY^
9102 @cindex @option{^-vm^/MEDIUM_VERBOSITY^} (@command{gnatmake})
9103 Verbosity level Medium. Potentially display fewer lines than in verbosity High.
9104
9105 @item ^-vh^/HIGH_VERBOSITY^
9106 @cindex @option{^-vm^/HIGH_VERBOSITY^} (@command{gnatmake})
9107 Verbosity level High. Equivalent to ^-v^/REASONS^.
9108
9109 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
9110 Indicate the verbosity of the parsing of GNAT project files.
9111 @xref{Switches Related to Project Files}.
9112
9113 @item ^-x^/NON_PROJECT_UNIT_COMPILATION^
9114 @cindex @option{^-x^/NON_PROJECT_UNIT_COMPILATION^} (@command{gnatmake})
9115 Indicate that sources that are not part of any Project File may be compiled.
9116 Normally, when using Project Files, only sources that are part of a Project
9117 File may be compile. When this switch is used, a source outside of all Project
9118 Files may be compiled. The ALI file and the object file will be put in the
9119 object directory of the main Project. The compilation switches used will only
9120 be those specified on the command line. Even when
9121 @option{^-x^/NON_PROJECT_UNIT_COMPILATION^} is used, mains specified on the
9122 command line need to be sources of a project file.
9123
9124 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
9125 Indicate that external variable @var{name} has the value @var{value}.
9126 The Project Manager will use this value for occurrences of
9127 @code{external(name)} when parsing the project file.
9128 @xref{Switches Related to Project Files}.
9129
9130 @item ^-z^/NOMAIN^
9131 @cindex @option{^-z^/NOMAIN^} (@command{gnatmake})
9132 No main subprogram. Bind and link the program even if the unit name
9133 given on the command line is a package name. The resulting executable
9134 will execute the elaboration routines of the package and its closure,
9135 then the finalization routines.
9136
9137 @end table
9138
9139 @table @asis
9140 @item @command{gcc} @asis{switches}
9141 @ifclear vms
9142 Any uppercase or multi-character switch that is not a @command{gnatmake} switch
9143 is passed to @command{gcc} (e.g.@: @option{-O}, @option{-gnato,} etc.)
9144 @end ifclear
9145 @ifset vms
9146 Any qualifier that cannot be recognized as a qualifier for @code{GNAT MAKE}
9147 but is recognizable as a valid qualifier for @code{GNAT COMPILE} is
9148 automatically treated as a compiler switch, and passed on to all
9149 compilations that are carried out.
9150 @end ifset
9151 @end table
9152
9153 @noindent
9154 Source and library search path switches:
9155
9156 @table @option
9157 @c !sort!
9158 @item ^-aI^/SOURCE_SEARCH=^@var{dir}
9159 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatmake})
9160 When looking for source files also look in directory @var{dir}.
9161 The order in which source files search is undertaken is
9162 described in @ref{Search Paths and the Run-Time Library (RTL)}.
9163
9164 @item ^-aL^/SKIP_MISSING=^@var{dir}
9165 @cindex @option{^-aL^/SKIP_MISSING^} (@command{gnatmake})
9166 Consider @var{dir} as being an externally provided Ada library.
9167 Instructs @command{gnatmake} to skip compilation units whose @file{.ALI}
9168 files have been located in directory @var{dir}. This allows you to have
9169 missing bodies for the units in @var{dir} and to ignore out of date bodies
9170 for the same units. You still need to specify
9171 the location of the specs for these units by using the switches
9172 @option{^-aI^/SOURCE_SEARCH=^@var{dir}}
9173 or @option{^-I^/SEARCH=^@var{dir}}.
9174 Note: this switch is provided for compatibility with previous versions
9175 of @command{gnatmake}. The easier method of causing standard libraries
9176 to be excluded from consideration is to write-protect the corresponding
9177 ALI files.
9178
9179 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
9180 @cindex @option{^-aO^/OBJECT_SEARCH^} (@command{gnatmake})
9181 When searching for library and object files, look in directory
9182 @var{dir}. The order in which library files are searched is described in
9183 @ref{Search Paths for gnatbind}.
9184
9185 @item ^-A^/CONDITIONAL_SOURCE_SEARCH=^@var{dir}
9186 @cindex Search paths, for @command{gnatmake}
9187 @cindex @option{^-A^/CONDITIONAL_SOURCE_SEARCH^} (@command{gnatmake})
9188 Equivalent to @option{^-aL^/SKIP_MISSING=^@var{dir}
9189 ^-aI^/SOURCE_SEARCH=^@var{dir}}.
9190
9191 @item ^-I^/SEARCH=^@var{dir}
9192 @cindex @option{^-I^/SEARCH^} (@command{gnatmake})
9193 Equivalent to @option{^-aO^/OBJECT_SEARCH=^@var{dir}
9194 ^-aI^/SOURCE_SEARCH=^@var{dir}}.
9195
9196 @item ^-I-^/NOCURRENT_DIRECTORY^
9197 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gnatmake})
9198 @cindex Source files, suppressing search
9199 Do not look for source files in the directory containing the source
9200 file named in the command line.
9201 Do not look for ALI or object files in the directory
9202 where @command{gnatmake} was invoked.
9203
9204 @item ^-L^/LIBRARY_SEARCH=^@var{dir}
9205 @cindex @option{^-L^/LIBRARY_SEARCH^} (@command{gnatmake})
9206 @cindex Linker libraries
9207 Add directory @var{dir} to the list of directories in which the linker
9208 will search for libraries. This is equivalent to
9209 @option{-largs ^-L^/LIBRARY_SEARCH=^}@var{dir}.
9210 @ifclear vms
9211 Furthermore, under Windows, the sources pointed to by the libraries path
9212 set in the registry are not searched for.
9213 @end ifclear
9214
9215 @item -nostdinc
9216 @cindex @option{-nostdinc} (@command{gnatmake})
9217 Do not look for source files in the system default directory.
9218
9219 @item -nostdlib
9220 @cindex @option{-nostdlib} (@command{gnatmake})
9221 Do not look for library files in the system default directory.
9222
9223 @item --RTS=@var{rts-path}
9224 @cindex @option{--RTS} (@command{gnatmake})
9225 Specifies the default location of the runtime library. GNAT looks for the
9226 runtime
9227 in the following directories, and stops as soon as a valid runtime is found
9228 (@file{adainclude} or @file{ada_source_path}, and @file{adalib} or
9229 @file{ada_object_path} present):
9230
9231 @itemize @bullet
9232 @item <current directory>/$rts_path
9233
9234 @item <default-search-dir>/$rts_path
9235
9236 @item <default-search-dir>/rts-$rts_path
9237 @end itemize
9238
9239 @noindent
9240 The selected path is handled like a normal RTS path.
9241
9242 @end table
9243
9244 @node Mode Switches for gnatmake
9245 @section Mode Switches for @command{gnatmake}
9246
9247 @noindent
9248 The mode switches (referred to as @code{mode_switches}) allow the
9249 inclusion of switches that are to be passed to the compiler itself, the
9250 binder or the linker. The effect of a mode switch is to cause all
9251 subsequent switches up to the end of the switch list, or up to the next
9252 mode switch, to be interpreted as switches to be passed on to the
9253 designated component of GNAT.
9254
9255 @table @option
9256 @c !sort!
9257 @item -cargs @var{switches}
9258 @cindex @option{-cargs} (@command{gnatmake})
9259 Compiler switches. Here @var{switches} is a list of switches
9260 that are valid switches for @command{gcc}. They will be passed on to
9261 all compile steps performed by @command{gnatmake}.
9262
9263 @item -bargs @var{switches}
9264 @cindex @option{-bargs} (@command{gnatmake})
9265 Binder switches. Here @var{switches} is a list of switches
9266 that are valid switches for @code{gnatbind}. They will be passed on to
9267 all bind steps performed by @command{gnatmake}.
9268
9269 @item -largs @var{switches}
9270 @cindex @option{-largs} (@command{gnatmake})
9271 Linker switches. Here @var{switches} is a list of switches
9272 that are valid switches for @command{gnatlink}. They will be passed on to
9273 all link steps performed by @command{gnatmake}.
9274
9275 @item -margs @var{switches}
9276 @cindex @option{-margs} (@command{gnatmake})
9277 Make switches. The switches are directly interpreted by @command{gnatmake},
9278 regardless of any previous occurrence of @option{-cargs}, @option{-bargs}
9279 or @option{-largs}.
9280 @end table
9281
9282 @node Notes on the Command Line
9283 @section Notes on the Command Line
9284
9285 @noindent
9286 This section contains some additional useful notes on the operation
9287 of the @command{gnatmake} command.
9288
9289 @itemize @bullet
9290 @item
9291 @cindex Recompilation, by @command{gnatmake}
9292 If @command{gnatmake} finds no ALI files, it recompiles the main program
9293 and all other units required by the main program.
9294 This means that @command{gnatmake}
9295 can be used for the initial compile, as well as during subsequent steps of
9296 the development cycle.
9297
9298 @item
9299 If you enter @code{gnatmake @var{file}.adb}, where @file{@var{file}.adb}
9300 is a subunit or body of a generic unit, @command{gnatmake} recompiles
9301 @file{@var{file}.adb} (because it finds no ALI) and stops, issuing a
9302 warning.
9303
9304 @item
9305 In @command{gnatmake} the switch @option{^-I^/SEARCH^}
9306 is used to specify both source and
9307 library file paths. Use @option{^-aI^/SOURCE_SEARCH^}
9308 instead if you just want to specify
9309 source paths only and @option{^-aO^/OBJECT_SEARCH^}
9310 if you want to specify library paths
9311 only.
9312
9313 @item
9314 @command{gnatmake} will ignore any files whose ALI file is write-protected.
9315 This may conveniently be used to exclude standard libraries from
9316 consideration and in particular it means that the use of the
9317 @option{^-f^/FORCE_COMPILE^} switch will not recompile these files
9318 unless @option{^-a^/ALL_FILES^} is also specified.
9319
9320 @item
9321 @command{gnatmake} has been designed to make the use of Ada libraries
9322 particularly convenient. Assume you have an Ada library organized
9323 as follows: @i{^obj-dir^[OBJ_DIR]^} contains the objects and ALI files for
9324 of your Ada compilation units,
9325 whereas @i{^include-dir^[INCLUDE_DIR]^} contains the
9326 specs of these units, but no bodies. Then to compile a unit
9327 stored in @code{main.adb}, which uses this Ada library you would just type
9328
9329 @smallexample
9330 @ifclear vms
9331 $ gnatmake -aI@var{include-dir} -aL@var{obj-dir} main
9332 @end ifclear
9333 @ifset vms
9334 $ gnatmake /SOURCE_SEARCH=@i{[INCLUDE_DIR]}
9335 /SKIP_MISSING=@i{[OBJ_DIR]} main
9336 @end ifset
9337 @end smallexample
9338
9339 @item
9340 Using @command{gnatmake} along with the
9341 @option{^-m (minimal recompilation)^/MINIMAL_RECOMPILATION^}
9342 switch provides a mechanism for avoiding unnecessary recompilations. Using
9343 this switch,
9344 you can update the comments/format of your
9345 source files without having to recompile everything. Note, however, that
9346 adding or deleting lines in a source files may render its debugging
9347 info obsolete. If the file in question is a spec, the impact is rather
9348 limited, as that debugging info will only be useful during the
9349 elaboration phase of your program. For bodies the impact can be more
9350 significant. In all events, your debugger will warn you if a source file
9351 is more recent than the corresponding object, and alert you to the fact
9352 that the debugging information may be out of date.
9353 @end itemize
9354
9355 @node How gnatmake Works
9356 @section How @command{gnatmake} Works
9357
9358 @noindent
9359 Generally @command{gnatmake} automatically performs all necessary
9360 recompilations and you don't need to worry about how it works. However,
9361 it may be useful to have some basic understanding of the @command{gnatmake}
9362 approach and in particular to understand how it uses the results of
9363 previous compilations without incorrectly depending on them.
9364
9365 First a definition: an object file is considered @dfn{up to date} if the
9366 corresponding ALI file exists and if all the source files listed in the
9367 dependency section of this ALI file have time stamps matching those in
9368 the ALI file. This means that neither the source file itself nor any
9369 files that it depends on have been modified, and hence there is no need
9370 to recompile this file.
9371
9372 @command{gnatmake} works by first checking if the specified main unit is up
9373 to date. If so, no compilations are required for the main unit. If not,
9374 @command{gnatmake} compiles the main program to build a new ALI file that
9375 reflects the latest sources. Then the ALI file of the main unit is
9376 examined to find all the source files on which the main program depends,
9377 and @command{gnatmake} recursively applies the above procedure on all these
9378 files.
9379
9380 This process ensures that @command{gnatmake} only trusts the dependencies
9381 in an existing ALI file if they are known to be correct. Otherwise it
9382 always recompiles to determine a new, guaranteed accurate set of
9383 dependencies. As a result the program is compiled ``upside down'' from what may
9384 be more familiar as the required order of compilation in some other Ada
9385 systems. In particular, clients are compiled before the units on which
9386 they depend. The ability of GNAT to compile in any order is critical in
9387 allowing an order of compilation to be chosen that guarantees that
9388 @command{gnatmake} will recompute a correct set of new dependencies if
9389 necessary.
9390
9391 When invoking @command{gnatmake} with several @var{file_names}, if a unit is
9392 imported by several of the executables, it will be recompiled at most once.
9393
9394 Note: when using non-standard naming conventions
9395 (@pxref{Using Other File Names}), changing through a configuration pragmas
9396 file the version of a source and invoking @command{gnatmake} to recompile may
9397 have no effect, if the previous version of the source is still accessible
9398 by @command{gnatmake}. It may be necessary to use the switch
9399 ^-f^/FORCE_COMPILE^.
9400
9401 @node Examples of gnatmake Usage
9402 @section Examples of @command{gnatmake} Usage
9403
9404 @table @code
9405 @item gnatmake hello.adb
9406 Compile all files necessary to bind and link the main program
9407 @file{hello.adb} (containing unit @code{Hello}) and bind and link the
9408 resulting object files to generate an executable file @file{^hello^HELLO.EXE^}.
9409
9410 @item gnatmake main1 main2 main3
9411 Compile all files necessary to bind and link the main programs
9412 @file{main1.adb} (containing unit @code{Main1}), @file{main2.adb}
9413 (containing unit @code{Main2}) and @file{main3.adb}
9414 (containing unit @code{Main3}) and bind and link the resulting object files
9415 to generate three executable files @file{^main1^MAIN1.EXE^},
9416 @file{^main2^MAIN2.EXE^}
9417 and @file{^main3^MAIN3.EXE^}.
9418
9419 @ifclear vms
9420 @item gnatmake -q Main_Unit -cargs -O2 -bargs -l
9421 @end ifclear
9422
9423 @ifset vms
9424 @item gnatmake Main_Unit /QUIET
9425 /COMPILER_QUALIFIERS /OPTIMIZE=ALL
9426 /BINDER_QUALIFIERS /ORDER_OF_ELABORATION
9427 @end ifset
9428 Compile all files necessary to bind and link the main program unit
9429 @code{Main_Unit} (from file @file{main_unit.adb}). All compilations will
9430 be done with optimization level 2 and the order of elaboration will be
9431 listed by the binder. @command{gnatmake} will operate in quiet mode, not
9432 displaying commands it is executing.
9433 @end table
9434
9435 @c *************************
9436 @node Improving Performance
9437 @chapter Improving Performance
9438 @cindex Improving performance
9439
9440 @noindent
9441 This chapter presents several topics related to program performance.
9442 It first describes some of the tradeoffs that need to be considered
9443 and some of the techniques for making your program run faster.
9444 It then documents the @command{gnatelim} tool and unused subprogram/data
9445 elimination feature, which can reduce the size of program executables.
9446
9447 Note: to invoke @command{gnatelim} with a project file, use the @code{gnat}
9448 driver (see @ref{The GNAT Driver and Project Files}).
9449
9450 @ifnottex
9451 @menu
9452 * Performance Considerations::
9453 * Text_IO Suggestions::
9454 * Reducing Size of Ada Executables with gnatelim::
9455 * Reducing Size of Executables with unused subprogram/data elimination::
9456 @end menu
9457 @end ifnottex
9458
9459 @c *****************************
9460 @node Performance Considerations
9461 @section Performance Considerations
9462
9463 @noindent
9464 The GNAT system provides a number of options that allow a trade-off
9465 between
9466
9467 @itemize @bullet
9468 @item
9469 performance of the generated code
9470
9471 @item
9472 speed of compilation
9473
9474 @item
9475 minimization of dependences and recompilation
9476
9477 @item
9478 the degree of run-time checking.
9479 @end itemize
9480
9481 @noindent
9482 The defaults (if no options are selected) aim at improving the speed
9483 of compilation and minimizing dependences, at the expense of performance
9484 of the generated code:
9485
9486 @itemize @bullet
9487 @item
9488 no optimization
9489
9490 @item
9491 no inlining of subprogram calls
9492
9493 @item
9494 all run-time checks enabled except overflow and elaboration checks
9495 @end itemize
9496
9497 @noindent
9498 These options are suitable for most program development purposes. This
9499 chapter describes how you can modify these choices, and also provides
9500 some guidelines on debugging optimized code.
9501
9502 @menu
9503 * Controlling Run-Time Checks::
9504 * Use of Restrictions::
9505 * Optimization Levels::
9506 * Debugging Optimized Code::
9507 * Inlining of Subprograms::
9508 * Other Optimization Switches::
9509 * Optimization and Strict Aliasing::
9510
9511 @ifset vms
9512 * Coverage Analysis::
9513 @end ifset
9514 @end menu
9515
9516 @node Controlling Run-Time Checks
9517 @subsection Controlling Run-Time Checks
9518
9519 @noindent
9520 By default, GNAT generates all run-time checks, except arithmetic overflow
9521 checking for integer operations and checks for access before elaboration on
9522 subprogram calls. The latter are not required in default mode, because all
9523 necessary checking is done at compile time.
9524 @cindex @option{-gnatp} (@command{gcc})
9525 @cindex @option{-gnato} (@command{gcc})
9526 Two gnat switches, @option{-gnatp} and @option{-gnato} allow this default to
9527 be modified. @xref{Run-Time Checks}.
9528
9529 Our experience is that the default is suitable for most development
9530 purposes.
9531
9532 We treat integer overflow specially because these
9533 are quite expensive and in our experience are not as important as other
9534 run-time checks in the development process. Note that division by zero
9535 is not considered an overflow check, and divide by zero checks are
9536 generated where required by default.
9537
9538 Elaboration checks are off by default, and also not needed by default, since
9539 GNAT uses a static elaboration analysis approach that avoids the need for
9540 run-time checking. This manual contains a full chapter discussing the issue
9541 of elaboration checks, and if the default is not satisfactory for your use,
9542 you should read this chapter.
9543
9544 For validity checks, the minimal checks required by the Ada Reference
9545 Manual (for case statements and assignments to array elements) are on
9546 by default. These can be suppressed by use of the @option{-gnatVn} switch.
9547 Note that in Ada 83, there were no validity checks, so if the Ada 83 mode
9548 is acceptable (or when comparing GNAT performance with an Ada 83 compiler),
9549 it may be reasonable to routinely use @option{-gnatVn}. Validity checks
9550 are also suppressed entirely if @option{-gnatp} is used.
9551
9552 @cindex Overflow checks
9553 @cindex Checks, overflow
9554 @findex Suppress
9555 @findex Unsuppress
9556 @cindex pragma Suppress
9557 @cindex pragma Unsuppress
9558 Note that the setting of the switches controls the default setting of
9559 the checks. They may be modified using either @code{pragma Suppress} (to
9560 remove checks) or @code{pragma Unsuppress} (to add back suppressed
9561 checks) in the program source.
9562
9563 @node Use of Restrictions
9564 @subsection Use of Restrictions
9565
9566 @noindent
9567 The use of pragma Restrictions allows you to control which features are
9568 permitted in your program. Apart from the obvious point that if you avoid
9569 relatively expensive features like finalization (enforceable by the use
9570 of pragma Restrictions (No_Finalization), the use of this pragma does not
9571 affect the generated code in most cases.
9572
9573 One notable exception to this rule is that the possibility of task abort
9574 results in some distributed overhead, particularly if finalization or
9575 exception handlers are used. The reason is that certain sections of code
9576 have to be marked as non-abortable.
9577
9578 If you use neither the @code{abort} statement, nor asynchronous transfer
9579 of control (@code{select @dots{} then abort}), then this distributed overhead
9580 is removed, which may have a general positive effect in improving
9581 overall performance. Especially code involving frequent use of tasking
9582 constructs and controlled types will show much improved performance.
9583 The relevant restrictions pragmas are
9584
9585 @smallexample @c ada
9586 pragma Restrictions (No_Abort_Statements);
9587 pragma Restrictions (Max_Asynchronous_Select_Nesting => 0);
9588 @end smallexample
9589
9590 @noindent
9591 It is recommended that these restriction pragmas be used if possible. Note
9592 that this also means that you can write code without worrying about the
9593 possibility of an immediate abort at any point.
9594
9595 @node Optimization Levels
9596 @subsection Optimization Levels
9597 @cindex @option{^-O^/OPTIMIZE^} (@command{gcc})
9598
9599 @noindent
9600 Without any optimization ^option,^qualifier,^
9601 the compiler's goal is to reduce the cost of
9602 compilation and to make debugging produce the expected results.
9603 Statements are independent: if you stop the program with a breakpoint between
9604 statements, you can then assign a new value to any variable or change
9605 the program counter to any other statement in the subprogram and get exactly
9606 the results you would expect from the source code.
9607
9608 Turning on optimization makes the compiler attempt to improve the
9609 performance and/or code size at the expense of compilation time and
9610 possibly the ability to debug the program.
9611
9612 If you use multiple
9613 ^-O options, with or without level numbers,^/OPTIMIZE qualifiers,^
9614 the last such option is the one that is effective.
9615
9616 @noindent
9617 The default is optimization off. This results in the fastest compile
9618 times, but GNAT makes absolutely no attempt to optimize, and the
9619 generated programs are considerably larger and slower than when
9620 optimization is enabled. You can use the
9621 @ifclear vms
9622 @option{-O} switch (the permitted forms are @option{-O0}, @option{-O1}
9623 @option{-O2}, @option{-O3}, and @option{-Os})
9624 @end ifclear
9625 @ifset vms
9626 @code{OPTIMIZE} qualifier
9627 @end ifset
9628 to @command{gcc} to control the optimization level:
9629
9630 @table @option
9631 @item ^-O0^/OPTIMIZE=NONE^
9632 No optimization (the default);
9633 generates unoptimized code but has
9634 the fastest compilation time.
9635
9636 Note that many other compilers do fairly extensive optimization
9637 even if ``no optimization'' is specified. With gcc, it is
9638 very unusual to use ^-O0^/OPTIMIZE=NONE^ for production if
9639 execution time is of any concern, since ^-O0^/OPTIMIZE=NONE^
9640 really does mean no optimization at all. This difference between
9641 gcc and other compilers should be kept in mind when doing
9642 performance comparisons.
9643
9644 @item ^-O1^/OPTIMIZE=SOME^
9645 Moderate optimization;
9646 optimizes reasonably well but does not
9647 degrade compilation time significantly.
9648
9649 @item ^-O2^/OPTIMIZE=ALL^
9650 @ifset vms
9651 @itemx /OPTIMIZE=DEVELOPMENT
9652 @end ifset
9653 Full optimization;
9654 generates highly optimized code and has
9655 the slowest compilation time.
9656
9657 @item ^-O3^/OPTIMIZE=INLINING^
9658 Full optimization as in @option{-O2},
9659 and also attempts automatic inlining of small
9660 subprograms within a unit (@pxref{Inlining of Subprograms}).
9661
9662 @item ^-Os^/OPTIMIZE=SPACE^
9663 Optimize space usage of resulting program.
9664 @end table
9665
9666 @noindent
9667 Higher optimization levels perform more global transformations on the
9668 program and apply more expensive analysis algorithms in order to generate
9669 faster and more compact code. The price in compilation time, and the
9670 resulting improvement in execution time,
9671 both depend on the particular application and the hardware environment.
9672 You should experiment to find the best level for your application.
9673
9674 Since the precise set of optimizations done at each level will vary from
9675 release to release (and sometime from target to target), it is best to think
9676 of the optimization settings in general terms.
9677 @xref{Optimize Options,, Options That Control Optimization, gcc, Using
9678 the GNU Compiler Collection (GCC)}, for details about
9679 ^the @option{-O} settings and a number of @option{-f} options that^how to^
9680 individually enable or disable specific optimizations.
9681
9682 Unlike some other compilation systems, ^@command{gcc}^GNAT^ has
9683 been tested extensively at all optimization levels. There are some bugs
9684 which appear only with optimization turned on, but there have also been
9685 bugs which show up only in @emph{unoptimized} code. Selecting a lower
9686 level of optimization does not improve the reliability of the code
9687 generator, which in practice is highly reliable at all optimization
9688 levels.
9689
9690 Note regarding the use of @option{-O3}: The use of this optimization level
9691 is generally discouraged with GNAT, since it often results in larger
9692 executables which run more slowly. See further discussion of this point
9693 in @ref{Inlining of Subprograms}.
9694
9695 @node Debugging Optimized Code
9696 @subsection Debugging Optimized Code
9697 @cindex Debugging optimized code
9698 @cindex Optimization and debugging
9699
9700 @noindent
9701 Although it is possible to do a reasonable amount of debugging at
9702 @ifclear vms
9703 nonzero optimization levels,
9704 the higher the level the more likely that
9705 @end ifclear
9706 @ifset vms
9707 @option{/OPTIMIZE} settings other than @code{NONE},
9708 such settings will make it more likely that
9709 @end ifset
9710 source-level constructs will have been eliminated by optimization.
9711 For example, if a loop is strength-reduced, the loop
9712 control variable may be completely eliminated and thus cannot be
9713 displayed in the debugger.
9714 This can only happen at @option{-O2} or @option{-O3}.
9715 Explicit temporary variables that you code might be eliminated at
9716 ^level^setting^ @option{-O1} or higher.
9717
9718 The use of the @option{^-g^/DEBUG^} switch,
9719 @cindex @option{^-g^/DEBUG^} (@command{gcc})
9720 which is needed for source-level debugging,
9721 affects the size of the program executable on disk,
9722 and indeed the debugging information can be quite large.
9723 However, it has no effect on the generated code (and thus does not
9724 degrade performance)
9725
9726 Since the compiler generates debugging tables for a compilation unit before
9727 it performs optimizations, the optimizing transformations may invalidate some
9728 of the debugging data. You therefore need to anticipate certain
9729 anomalous situations that may arise while debugging optimized code.
9730 These are the most common cases:
9731
9732 @enumerate
9733 @item
9734 @i{The ``hopping Program Counter'':} Repeated @code{step} or @code{next}
9735 commands show
9736 the PC bouncing back and forth in the code. This may result from any of
9737 the following optimizations:
9738
9739 @itemize @bullet
9740 @item
9741 @i{Common subexpression elimination:} using a single instance of code for a
9742 quantity that the source computes several times. As a result you
9743 may not be able to stop on what looks like a statement.
9744
9745 @item
9746 @i{Invariant code motion:} moving an expression that does not change within a
9747 loop, to the beginning of the loop.
9748
9749 @item
9750 @i{Instruction scheduling:} moving instructions so as to
9751 overlap loads and stores (typically) with other code, or in
9752 general to move computations of values closer to their uses. Often
9753 this causes you to pass an assignment statement without the assignment
9754 happening and then later bounce back to the statement when the
9755 value is actually needed. Placing a breakpoint on a line of code
9756 and then stepping over it may, therefore, not always cause all the
9757 expected side-effects.
9758 @end itemize
9759
9760 @item
9761 @i{The ``big leap'':} More commonly known as @emph{cross-jumping}, in which
9762 two identical pieces of code are merged and the program counter suddenly
9763 jumps to a statement that is not supposed to be executed, simply because
9764 it (and the code following) translates to the same thing as the code
9765 that @emph{was} supposed to be executed. This effect is typically seen in
9766 sequences that end in a jump, such as a @code{goto}, a @code{return}, or
9767 a @code{break} in a C @code{^switch^switch^} statement.
9768
9769 @item
9770 @i{The ``roving variable'':} The symptom is an unexpected value in a variable.
9771 There are various reasons for this effect:
9772
9773 @itemize @bullet
9774 @item
9775 In a subprogram prologue, a parameter may not yet have been moved to its
9776 ``home''.
9777
9778 @item
9779 A variable may be dead, and its register re-used. This is
9780 probably the most common cause.
9781
9782 @item
9783 As mentioned above, the assignment of a value to a variable may
9784 have been moved.
9785
9786 @item
9787 A variable may be eliminated entirely by value propagation or
9788 other means. In this case, GCC may incorrectly generate debugging
9789 information for the variable
9790 @end itemize
9791
9792 @noindent
9793 In general, when an unexpected value appears for a local variable or parameter
9794 you should first ascertain if that value was actually computed by
9795 your program, as opposed to being incorrectly reported by the debugger.
9796 Record fields or
9797 array elements in an object designated by an access value
9798 are generally less of a problem, once you have ascertained that the access
9799 value is sensible.
9800 Typically, this means checking variables in the preceding code and in the
9801 calling subprogram to verify that the value observed is explainable from other
9802 values (one must apply the procedure recursively to those
9803 other values); or re-running the code and stopping a little earlier
9804 (perhaps before the call) and stepping to better see how the variable obtained
9805 the value in question; or continuing to step @emph{from} the point of the
9806 strange value to see if code motion had simply moved the variable's
9807 assignments later.
9808 @end enumerate
9809
9810 @noindent
9811 In light of such anomalies, a recommended technique is to use @option{-O0}
9812 early in the software development cycle, when extensive debugging capabilities
9813 are most needed, and then move to @option{-O1} and later @option{-O2} as
9814 the debugger becomes less critical.
9815 Whether to use the @option{^-g^/DEBUG^} switch in the release version is
9816 a release management issue.
9817 @ifclear vms
9818 Note that if you use @option{-g} you can then use the @command{strip} program
9819 on the resulting executable,
9820 which removes both debugging information and global symbols.
9821 @end ifclear
9822
9823 @node Inlining of Subprograms
9824 @subsection Inlining of Subprograms
9825
9826 @noindent
9827 A call to a subprogram in the current unit is inlined if all the
9828 following conditions are met:
9829
9830 @itemize @bullet
9831 @item
9832 The optimization level is at least @option{-O1}.
9833
9834 @item
9835 The called subprogram is suitable for inlining: It must be small enough
9836 and not contain something that @command{gcc} cannot support in inlined
9837 subprograms.
9838
9839 @item
9840 @cindex pragma Inline
9841 @findex Inline
9842 Either @code{pragma Inline} applies to the subprogram, or it is local
9843 to the unit and called once from within it, or it is small and automatic
9844 inlining (optimization level @option{-O3}) is specified.
9845 @end itemize
9846
9847 @noindent
9848 Calls to subprograms in @code{with}'ed units are normally not inlined.
9849 To achieve actual inlining (that is, replacement of the call by the code
9850 in the body of the subprogram), the following conditions must all be true.
9851
9852 @itemize @bullet
9853 @item
9854 The optimization level is at least @option{-O1}.
9855
9856 @item
9857 The called subprogram is suitable for inlining: It must be small enough
9858 and not contain something that @command{gcc} cannot support in inlined
9859 subprograms.
9860
9861 @item
9862 The call appears in a body (not in a package spec).
9863
9864 @item
9865 There is a @code{pragma Inline} for the subprogram.
9866
9867 @item
9868 @cindex @option{-gnatn} (@command{gcc})
9869 The @option{^-gnatn^/INLINE^} switch
9870 is used in the @command{gcc} command line
9871 @end itemize
9872
9873 Even if all these conditions are met, it may not be possible for
9874 the compiler to inline the call, due to the length of the body,
9875 or features in the body that make it impossible for the compiler
9876 to do the inlining.
9877
9878 Note that specifying the @option{-gnatn} switch causes additional
9879 compilation dependencies. Consider the following:
9880
9881 @smallexample @c ada
9882 @cartouche
9883 package R is
9884 procedure Q;
9885 pragma Inline (Q);
9886 end R;
9887 package body R is
9888 @dots{}
9889 end R;
9890
9891 with R;
9892 procedure Main is
9893 begin
9894 @dots{}
9895 R.Q;
9896 end Main;
9897 @end cartouche
9898 @end smallexample
9899
9900 @noindent
9901 With the default behavior (no @option{-gnatn} switch specified), the
9902 compilation of the @code{Main} procedure depends only on its own source,
9903 @file{main.adb}, and the spec of the package in file @file{r.ads}. This
9904 means that editing the body of @code{R} does not require recompiling
9905 @code{Main}.
9906
9907 On the other hand, the call @code{R.Q} is not inlined under these
9908 circumstances. If the @option{-gnatn} switch is present when @code{Main}
9909 is compiled, the call will be inlined if the body of @code{Q} is small
9910 enough, but now @code{Main} depends on the body of @code{R} in
9911 @file{r.adb} as well as on the spec. This means that if this body is edited,
9912 the main program must be recompiled. Note that this extra dependency
9913 occurs whether or not the call is in fact inlined by @command{gcc}.
9914
9915 The use of front end inlining with @option{-gnatN} generates similar
9916 additional dependencies.
9917
9918 @cindex @option{^-fno-inline^/INLINE=SUPPRESS^} (@command{gcc})
9919 Note: The @option{^-fno-inline^/INLINE=SUPPRESS^} switch
9920 can be used to prevent
9921 all inlining. This switch overrides all other conditions and ensures
9922 that no inlining occurs. The extra dependences resulting from
9923 @option{-gnatn} will still be active, even if
9924 this switch is used to suppress the resulting inlining actions.
9925
9926 @cindex @option{-fno-inline-functions} (@command{gcc})
9927 Note: The @option{-fno-inline-functions} switch can be used to prevent
9928 automatic inlining of small subprograms if @option{-O3} is used.
9929
9930 @cindex @option{-fno-inline-functions-called-once} (@command{gcc})
9931 Note: The @option{-fno-inline-functions-called-once} switch
9932 can be used to prevent inlining of subprograms local to the unit
9933 and called once from within it if @option{-O1} is used.
9934
9935 Note regarding the use of @option{-O3}: There is no difference in inlining
9936 behavior between @option{-O2} and @option{-O3} for subprograms with an explicit
9937 pragma @code{Inline} assuming the use of @option{-gnatn}
9938 or @option{-gnatN} (the switches that activate inlining). If you have used
9939 pragma @code{Inline} in appropriate cases, then it is usually much better
9940 to use @option{-O2} and @option{-gnatn} and avoid the use of @option{-O3} which
9941 in this case only has the effect of inlining subprograms you did not
9942 think should be inlined. We often find that the use of @option{-O3} slows
9943 down code by performing excessive inlining, leading to increased instruction
9944 cache pressure from the increased code size. So the bottom line here is
9945 that you should not automatically assume that @option{-O3} is better than
9946 @option{-O2}, and indeed you should use @option{-O3} only if tests show that
9947 it actually improves performance.
9948
9949 @node Other Optimization Switches
9950 @subsection Other Optimization Switches
9951 @cindex Optimization Switches
9952
9953 Since @code{GNAT} uses the @command{gcc} back end, all the specialized
9954 @command{gcc} optimization switches are potentially usable. These switches
9955 have not been extensively tested with GNAT but can generally be expected
9956 to work. Examples of switches in this category are
9957 @option{-funroll-loops} and
9958 the various target-specific @option{-m} options (in particular, it has been
9959 observed that @option{-march=pentium4} can significantly improve performance
9960 on appropriate machines). For full details of these switches, see
9961 @ref{Submodel Options,, Hardware Models and Configurations, gcc, Using
9962 the GNU Compiler Collection (GCC)}.
9963
9964 @node Optimization and Strict Aliasing
9965 @subsection Optimization and Strict Aliasing
9966 @cindex Aliasing
9967 @cindex Strict Aliasing
9968 @cindex No_Strict_Aliasing
9969
9970 @noindent
9971 The strong typing capabilities of Ada allow an optimizer to generate
9972 efficient code in situations where other languages would be forced to
9973 make worst case assumptions preventing such optimizations. Consider
9974 the following example:
9975
9976 @smallexample @c ada
9977 @cartouche
9978 procedure R is
9979 type Int1 is new Integer;
9980 type Int2 is new Integer;
9981 type Int1A is access Int1;
9982 type Int2A is access Int2;
9983 Int1V : Int1A;
9984 Int2V : Int2A;
9985 @dots{}
9986
9987 begin
9988 @dots{}
9989 for J in Data'Range loop
9990 if Data (J) = Int1V.all then
9991 Int2V.all := Int2V.all + 1;
9992 end if;
9993 end loop;
9994 @dots{}
9995 end R;
9996 @end cartouche
9997 @end smallexample
9998
9999 @noindent
10000 In this example, since the variable @code{Int1V} can only access objects
10001 of type @code{Int1}, and @code{Int2V} can only access objects of type
10002 @code{Int2}, there is no possibility that the assignment to
10003 @code{Int2V.all} affects the value of @code{Int1V.all}. This means that
10004 the compiler optimizer can "know" that the value @code{Int1V.all} is constant
10005 for all iterations of the loop and avoid the extra memory reference
10006 required to dereference it each time through the loop.
10007
10008 This kind of optimization, called strict aliasing analysis, is
10009 triggered by specifying an optimization level of @option{-O2} or
10010 higher and allows @code{GNAT} to generate more efficient code
10011 when access values are involved.
10012
10013 However, although this optimization is always correct in terms of
10014 the formal semantics of the Ada Reference Manual, difficulties can
10015 arise if features like @code{Unchecked_Conversion} are used to break
10016 the typing system. Consider the following complete program example:
10017
10018 @smallexample @c ada
10019 @cartouche
10020 package p1 is
10021 type int1 is new integer;
10022 type int2 is new integer;
10023 type a1 is access int1;
10024 type a2 is access int2;
10025 end p1;
10026
10027 with p1; use p1;
10028 package p2 is
10029 function to_a2 (Input : a1) return a2;
10030 end p2;
10031
10032 with Unchecked_Conversion;
10033 package body p2 is
10034 function to_a2 (Input : a1) return a2 is
10035 function to_a2u is
10036 new Unchecked_Conversion (a1, a2);
10037 begin
10038 return to_a2u (Input);
10039 end to_a2;
10040 end p2;
10041
10042 with p2; use p2;
10043 with p1; use p1;
10044 with Text_IO; use Text_IO;
10045 procedure m is
10046 v1 : a1 := new int1;
10047 v2 : a2 := to_a2 (v1);
10048 begin
10049 v1.all := 1;
10050 v2.all := 0;
10051 put_line (int1'image (v1.all));
10052 end;
10053 @end cartouche
10054 @end smallexample
10055
10056 @noindent
10057 This program prints out 0 in @option{-O0} or @option{-O1}
10058 mode, but it prints out 1 in @option{-O2} mode. That's
10059 because in strict aliasing mode, the compiler can and
10060 does assume that the assignment to @code{v2.all} could not
10061 affect the value of @code{v1.all}, since different types
10062 are involved.
10063
10064 This behavior is not a case of non-conformance with the standard, since
10065 the Ada RM specifies that an unchecked conversion where the resulting
10066 bit pattern is not a correct value of the target type can result in an
10067 abnormal value and attempting to reference an abnormal value makes the
10068 execution of a program erroneous. That's the case here since the result
10069 does not point to an object of type @code{int2}. This means that the
10070 effect is entirely unpredictable.
10071
10072 However, although that explanation may satisfy a language
10073 lawyer, in practice an applications programmer expects an
10074 unchecked conversion involving pointers to create true
10075 aliases and the behavior of printing 1 seems plain wrong.
10076 In this case, the strict aliasing optimization is unwelcome.
10077
10078 Indeed the compiler recognizes this possibility, and the
10079 unchecked conversion generates a warning:
10080
10081 @smallexample
10082 p2.adb:5:07: warning: possible aliasing problem with type "a2"
10083 p2.adb:5:07: warning: use -fno-strict-aliasing switch for references
10084 p2.adb:5:07: warning: or use "pragma No_Strict_Aliasing (a2);"
10085 @end smallexample
10086
10087 @noindent
10088 Unfortunately the problem is recognized when compiling the body of
10089 package @code{p2}, but the actual "bad" code is generated while
10090 compiling the body of @code{m} and this latter compilation does not see
10091 the suspicious @code{Unchecked_Conversion}.
10092
10093 As implied by the warning message, there are approaches you can use to
10094 avoid the unwanted strict aliasing optimization in a case like this.
10095
10096 One possibility is to simply avoid the use of @option{-O2}, but
10097 that is a bit drastic, since it throws away a number of useful
10098 optimizations that do not involve strict aliasing assumptions.
10099
10100 A less drastic approach is to compile the program using the
10101 option @option{-fno-strict-aliasing}. Actually it is only the
10102 unit containing the dereferencing of the suspicious pointer
10103 that needs to be compiled. So in this case, if we compile
10104 unit @code{m} with this switch, then we get the expected
10105 value of zero printed. Analyzing which units might need
10106 the switch can be painful, so a more reasonable approach
10107 is to compile the entire program with options @option{-O2}
10108 and @option{-fno-strict-aliasing}. If the performance is
10109 satisfactory with this combination of options, then the
10110 advantage is that the entire issue of possible "wrong"
10111 optimization due to strict aliasing is avoided.
10112
10113 To avoid the use of compiler switches, the configuration
10114 pragma @code{No_Strict_Aliasing} with no parameters may be
10115 used to specify that for all access types, the strict
10116 aliasing optimization should be suppressed.
10117
10118 However, these approaches are still overkill, in that they causes
10119 all manipulations of all access values to be deoptimized. A more
10120 refined approach is to concentrate attention on the specific
10121 access type identified as problematic.
10122
10123 First, if a careful analysis of uses of the pointer shows
10124 that there are no possible problematic references, then
10125 the warning can be suppressed by bracketing the
10126 instantiation of @code{Unchecked_Conversion} to turn
10127 the warning off:
10128
10129 @smallexample @c ada
10130 pragma Warnings (Off);
10131 function to_a2u is
10132 new Unchecked_Conversion (a1, a2);
10133 pragma Warnings (On);
10134 @end smallexample
10135
10136 @noindent
10137 Of course that approach is not appropriate for this particular
10138 example, since indeed there is a problematic reference. In this
10139 case we can take one of two other approaches.
10140
10141 The first possibility is to move the instantiation of unchecked
10142 conversion to the unit in which the type is declared. In
10143 this example, we would move the instantiation of
10144 @code{Unchecked_Conversion} from the body of package
10145 @code{p2} to the spec of package @code{p1}. Now the
10146 warning disappears. That's because any use of the
10147 access type knows there is a suspicious unchecked
10148 conversion, and the strict aliasing optimization
10149 is automatically suppressed for the type.
10150
10151 If it is not practical to move the unchecked conversion to the same unit
10152 in which the destination access type is declared (perhaps because the
10153 source type is not visible in that unit), you may use pragma
10154 @code{No_Strict_Aliasing} for the type. This pragma must occur in the
10155 same declarative sequence as the declaration of the access type:
10156
10157 @smallexample @c ada
10158 type a2 is access int2;
10159 pragma No_Strict_Aliasing (a2);
10160 @end smallexample
10161
10162 @noindent
10163 Here again, the compiler now knows that the strict aliasing optimization
10164 should be suppressed for any reference to type @code{a2} and the
10165 expected behavior is obtained.
10166
10167 Finally, note that although the compiler can generate warnings for
10168 simple cases of unchecked conversions, there are tricker and more
10169 indirect ways of creating type incorrect aliases which the compiler
10170 cannot detect. Examples are the use of address overlays and unchecked
10171 conversions involving composite types containing access types as
10172 components. In such cases, no warnings are generated, but there can
10173 still be aliasing problems. One safe coding practice is to forbid the
10174 use of address clauses for type overlaying, and to allow unchecked
10175 conversion only for primitive types. This is not really a significant
10176 restriction since any possible desired effect can be achieved by
10177 unchecked conversion of access values.
10178
10179 @ifset vms
10180 @node Coverage Analysis
10181 @subsection Coverage Analysis
10182
10183 @noindent
10184 GNAT supports the HP Performance Coverage Analyzer (PCA), which allows
10185 the user to determine the distribution of execution time across a program,
10186 @pxref{Profiling} for details of usage.
10187 @end ifset
10188
10189
10190 @node Text_IO Suggestions
10191 @section @code{Text_IO} Suggestions
10192 @cindex @code{Text_IO} and performance
10193
10194 @noindent
10195 The @code{Ada.Text_IO} package has fairly high overheads due in part to
10196 the requirement of maintaining page and line counts. If performance
10197 is critical, a recommendation is to use @code{Stream_IO} instead of
10198 @code{Text_IO} for volume output, since this package has less overhead.
10199
10200 If @code{Text_IO} must be used, note that by default output to the standard
10201 output and standard error files is unbuffered (this provides better
10202 behavior when output statements are used for debugging, or if the
10203 progress of a program is observed by tracking the output, e.g. by
10204 using the Unix @command{tail -f} command to watch redirected output.
10205
10206 If you are generating large volumes of output with @code{Text_IO} and
10207 performance is an important factor, use a designated file instead
10208 of the standard output file, or change the standard output file to
10209 be buffered using @code{Interfaces.C_Streams.setvbuf}.
10210
10211
10212
10213 @node Reducing Size of Ada Executables with gnatelim
10214 @section Reducing Size of Ada Executables with @code{gnatelim}
10215 @findex gnatelim
10216
10217 @noindent
10218 This section describes @command{gnatelim}, a tool which detects unused
10219 subprograms and helps the compiler to create a smaller executable for your
10220 program.
10221
10222 @menu
10223 * About gnatelim::
10224 * Running gnatelim::
10225 * Correcting the List of Eliminate Pragmas::
10226 * Making Your Executables Smaller::
10227 * Summary of the gnatelim Usage Cycle::
10228 @end menu
10229
10230 @node About gnatelim
10231 @subsection About @code{gnatelim}
10232
10233 @noindent
10234 When a program shares a set of Ada
10235 packages with other programs, it may happen that this program uses
10236 only a fraction of the subprograms defined in these packages. The code
10237 created for these unused subprograms increases the size of the executable.
10238
10239 @code{gnatelim} tracks unused subprograms in an Ada program and
10240 outputs a list of GNAT-specific pragmas @code{Eliminate} marking all the
10241 subprograms that are declared but never called. By placing the list of
10242 @code{Eliminate} pragmas in the GNAT configuration file @file{gnat.adc} and
10243 recompiling your program, you may decrease the size of its executable,
10244 because the compiler will not generate the code for 'eliminated' subprograms.
10245 @xref{Pragma Eliminate,,, gnat_rm, GNAT Reference Manual}, for more
10246 information about this pragma.
10247
10248 @code{gnatelim} needs as its input data the name of the main subprogram
10249 and a bind file for a main subprogram.
10250
10251 To create a bind file for @code{gnatelim}, run @code{gnatbind} for
10252 the main subprogram. @code{gnatelim} can work with both Ada and C
10253 bind files; when both are present, it uses the Ada bind file.
10254 The following commands will build the program and create the bind file:
10255
10256 @smallexample
10257 $ gnatmake ^-c Main_Prog^/ACTIONS=COMPILE MAIN_PROG^
10258 $ gnatbind main_prog
10259 @end smallexample
10260
10261 Note that @code{gnatelim} needs neither object nor ALI files.
10262
10263 @node Running gnatelim
10264 @subsection Running @code{gnatelim}
10265
10266 @noindent
10267 @code{gnatelim} has the following command-line interface:
10268
10269 @smallexample
10270 $ gnatelim @ovar{options} name
10271 @end smallexample
10272
10273 @noindent
10274 @code{name} should be a name of a source file that contains the main subprogram
10275 of a program (partition).
10276
10277 @code{gnatelim} has the following switches:
10278
10279 @table @option
10280 @c !sort!
10281 @item ^-q^/QUIET^
10282 @cindex @option{^-q^/QUIET^} (@command{gnatelim})
10283 Quiet mode: by default @code{gnatelim} outputs to the standard error
10284 stream the number of program units left to be processed. This option turns
10285 this trace off.
10286
10287 @item ^-v^/VERBOSE^
10288 @cindex @option{^-v^/VERBOSE^} (@command{gnatelim})
10289 Verbose mode: @code{gnatelim} version information is printed as Ada
10290 comments to the standard output stream. Also, in addition to the number of
10291 program units left @code{gnatelim} will output the name of the current unit
10292 being processed.
10293
10294 @item ^-a^/ALL^
10295 @cindex @option{^-a^/ALL^} (@command{gnatelim})
10296 Also look for subprograms from the GNAT run time that can be eliminated. Note
10297 that when @file{gnat.adc} is produced using this switch, the entire program
10298 must be recompiled with switch @option{^-a^/ALL_FILES^} to @command{gnatmake}.
10299
10300 @item ^-I^/INCLUDE_DIRS=^@var{dir}
10301 @cindex @option{^-I^/INCLUDE_DIRS^} (@command{gnatelim})
10302 When looking for source files also look in directory @var{dir}. Specifying
10303 @option{^-I-^/INCLUDE_DIRS=-^} instructs @code{gnatelim} not to look for
10304 sources in the current directory.
10305
10306 @item ^-b^/BIND_FILE=^@var{bind_file}
10307 @cindex @option{^-b^/BIND_FILE^} (@command{gnatelim})
10308 Specifies @var{bind_file} as the bind file to process. If not set, the name
10309 of the bind file is computed from the full expanded Ada name
10310 of a main subprogram.
10311
10312 @item ^-C^/CONFIG_FILE=^@var{config_file}
10313 @cindex @option{^-C^/CONFIG_FILE^} (@command{gnatelim})
10314 Specifies a file @var{config_file} that contains configuration pragmas. The
10315 file must be specified with full path.
10316
10317 @item ^--GCC^/COMPILER^=@var{compiler_name}
10318 @cindex @option{^-GCC^/COMPILER^} (@command{gnatelim})
10319 Instructs @code{gnatelim} to use specific @command{gcc} compiler instead of one
10320 available on the path.
10321
10322 @item ^--GNATMAKE^/GNATMAKE^=@var{gnatmake_name}
10323 @cindex @option{^--GNATMAKE^/GNATMAKE^} (@command{gnatelim})
10324 Instructs @code{gnatelim} to use specific @command{gnatmake} instead of one
10325 available on the path.
10326 @end table
10327
10328 @noindent
10329 @code{gnatelim} sends its output to the standard output stream, and all the
10330 tracing and debug information is sent to the standard error stream.
10331 In order to produce a proper GNAT configuration file
10332 @file{gnat.adc}, redirection must be used:
10333
10334 @smallexample
10335 @ifset vms
10336 $ PIPE GNAT ELIM MAIN_PROG.ADB > GNAT.ADC
10337 @end ifset
10338 @ifclear vms
10339 $ gnatelim main_prog.adb > gnat.adc
10340 @end ifclear
10341 @end smallexample
10342
10343 @ifclear vms
10344 @noindent
10345 or
10346
10347 @smallexample
10348 $ gnatelim main_prog.adb >> gnat.adc
10349 @end smallexample
10350
10351 @noindent
10352 in order to append the @code{gnatelim} output to the existing contents of
10353 @file{gnat.adc}.
10354 @end ifclear
10355
10356 @node Correcting the List of Eliminate Pragmas
10357 @subsection Correcting the List of Eliminate Pragmas
10358
10359 @noindent
10360 In some rare cases @code{gnatelim} may try to eliminate
10361 subprograms that are actually called in the program. In this case, the
10362 compiler will generate an error message of the form:
10363
10364 @smallexample
10365 file.adb:106:07: cannot call eliminated subprogram "My_Prog"
10366 @end smallexample
10367
10368 @noindent
10369 You will need to manually remove the wrong @code{Eliminate} pragmas from
10370 the @file{gnat.adc} file. You should recompile your program
10371 from scratch after that, because you need a consistent @file{gnat.adc} file
10372 during the entire compilation.
10373
10374 @node Making Your Executables Smaller
10375 @subsection Making Your Executables Smaller
10376
10377 @noindent
10378 In order to get a smaller executable for your program you now have to
10379 recompile the program completely with the new @file{gnat.adc} file
10380 created by @code{gnatelim} in your current directory:
10381
10382 @smallexample
10383 $ gnatmake ^-f main_prog^/FORCE_COMPILE MAIN_PROG^
10384 @end smallexample
10385
10386 @noindent
10387 (Use the @option{^-f^/FORCE_COMPILE^} option for @command{gnatmake} to
10388 recompile everything
10389 with the set of pragmas @code{Eliminate} that you have obtained with
10390 @command{gnatelim}).
10391
10392 Be aware that the set of @code{Eliminate} pragmas is specific to each
10393 program. It is not recommended to merge sets of @code{Eliminate}
10394 pragmas created for different programs in one @file{gnat.adc} file.
10395
10396 @node Summary of the gnatelim Usage Cycle
10397 @subsection Summary of the gnatelim Usage Cycle
10398
10399 @noindent
10400 Here is a quick summary of the steps to be taken in order to reduce
10401 the size of your executables with @code{gnatelim}. You may use
10402 other GNAT options to control the optimization level,
10403 to produce the debugging information, to set search path, etc.
10404
10405 @enumerate
10406 @item
10407 Produce a bind file
10408
10409 @smallexample
10410 $ gnatmake ^-c main_prog^/ACTIONS=COMPILE MAIN_PROG^
10411 $ gnatbind main_prog
10412 @end smallexample
10413
10414 @item
10415 Generate a list of @code{Eliminate} pragmas
10416 @smallexample
10417 @ifset vms
10418 $ PIPE GNAT ELIM MAIN_PROG > GNAT.ADC
10419 @end ifset
10420 @ifclear vms
10421 $ gnatelim main_prog >@r{[}>@r{]} gnat.adc
10422 @end ifclear
10423 @end smallexample
10424
10425 @item
10426 Recompile the application
10427
10428 @smallexample
10429 $ gnatmake ^-f main_prog^/FORCE_COMPILE MAIN_PROG^
10430 @end smallexample
10431
10432 @end enumerate
10433
10434 @node Reducing Size of Executables with unused subprogram/data elimination
10435 @section Reducing Size of Executables with Unused Subprogram/Data Elimination
10436 @findex unused subprogram/data elimination
10437
10438 @noindent
10439 This section describes how you can eliminate unused subprograms and data from
10440 your executable just by setting options at compilation time.
10441
10442 @menu
10443 * About unused subprogram/data elimination::
10444 * Compilation options::
10445 * Example of unused subprogram/data elimination::
10446 @end menu
10447
10448 @node About unused subprogram/data elimination
10449 @subsection About unused subprogram/data elimination
10450
10451 @noindent
10452 By default, an executable contains all code and data of its composing objects
10453 (directly linked or coming from statically linked libraries), even data or code
10454 never used by this executable.
10455
10456 This feature will allow you to eliminate such unused code from your
10457 executable, making it smaller (in disk and in memory).
10458
10459 This functionality is available on all Linux platforms except for the IA-64
10460 architecture and on all cross platforms using the ELF binary file format.
10461 In both cases GNU binutils version 2.16 or later are required to enable it.
10462
10463 @node Compilation options
10464 @subsection Compilation options
10465
10466 @noindent
10467 The operation of eliminating the unused code and data from the final executable
10468 is directly performed by the linker.
10469
10470 In order to do this, it has to work with objects compiled with the
10471 following options:
10472 @option{-ffunction-sections} @option{-fdata-sections}.
10473 @cindex @option{-ffunction-sections} (@command{gcc})
10474 @cindex @option{-fdata-sections} (@command{gcc})
10475 These options are usable with C and Ada files.
10476 They will place respectively each
10477 function or data in a separate section in the resulting object file.
10478
10479 Once the objects and static libraries are created with these options, the
10480 linker can perform the dead code elimination. You can do this by setting
10481 the @option{-Wl,--gc-sections} option to gcc command or in the
10482 @option{-largs} section of @command{gnatmake}. This will perform a
10483 garbage collection of code and data never referenced.
10484
10485 If the linker performs a partial link (@option{-r} ld linker option), then you
10486 will need to provide one or several entry point using the
10487 @option{-e} / @option{--entry} ld option.
10488
10489 Note that objects compiled without the @option{-ffunction-sections} and
10490 @option{-fdata-sections} options can still be linked with the executable.
10491 However, no dead code elimination will be performed on those objects (they will
10492 be linked as is).
10493
10494 The GNAT static library is now compiled with -ffunction-sections and
10495 -fdata-sections on some platforms. This allows you to eliminate the unused code
10496 and data of the GNAT library from your executable.
10497
10498 @node Example of unused subprogram/data elimination
10499 @subsection Example of unused subprogram/data elimination
10500
10501 @noindent
10502 Here is a simple example:
10503
10504 @smallexample @c ada
10505 with Aux;
10506
10507 procedure Test is
10508 begin
10509 Aux.Used (10);
10510 end Test;
10511
10512 package Aux is
10513 Used_Data : Integer;
10514 Unused_Data : Integer;
10515
10516 procedure Used (Data : Integer);
10517 procedure Unused (Data : Integer);
10518 end Aux;
10519
10520 package body Aux is
10521 procedure Used (Data : Integer) is
10522 begin
10523 Used_Data := Data;
10524 end Used;
10525
10526 procedure Unused (Data : Integer) is
10527 begin
10528 Unused_Data := Data;
10529 end Unused;
10530 end Aux;
10531 @end smallexample
10532
10533 @noindent
10534 @code{Unused} and @code{Unused_Data} are never referenced in this code
10535 excerpt, and hence they may be safely removed from the final executable.
10536
10537 @smallexample
10538 $ gnatmake test
10539
10540 $ nm test | grep used
10541 020015f0 T aux__unused
10542 02005d88 B aux__unused_data
10543 020015cc T aux__used
10544 02005d84 B aux__used_data
10545
10546 $ gnatmake test -cargs -fdata-sections -ffunction-sections \
10547 -largs -Wl,--gc-sections
10548
10549 $ nm test | grep used
10550 02005350 T aux__used
10551 0201ffe0 B aux__used_data
10552 @end smallexample
10553
10554 @noindent
10555 It can be observed that the procedure @code{Unused} and the object
10556 @code{Unused_Data} are removed by the linker when using the
10557 appropriate options.
10558
10559 @c ********************************
10560 @node Renaming Files Using gnatchop
10561 @chapter Renaming Files Using @code{gnatchop}
10562 @findex gnatchop
10563
10564 @noindent
10565 This chapter discusses how to handle files with multiple units by using
10566 the @code{gnatchop} utility. This utility is also useful in renaming
10567 files to meet the standard GNAT default file naming conventions.
10568
10569 @menu
10570 * Handling Files with Multiple Units::
10571 * Operating gnatchop in Compilation Mode::
10572 * Command Line for gnatchop::
10573 * Switches for gnatchop::
10574 * Examples of gnatchop Usage::
10575 @end menu
10576
10577 @node Handling Files with Multiple Units
10578 @section Handling Files with Multiple Units
10579
10580 @noindent
10581 The basic compilation model of GNAT requires that a file submitted to the
10582 compiler have only one unit and there be a strict correspondence
10583 between the file name and the unit name.
10584
10585 The @code{gnatchop} utility allows both of these rules to be relaxed,
10586 allowing GNAT to process files which contain multiple compilation units
10587 and files with arbitrary file names. @code{gnatchop}
10588 reads the specified file and generates one or more output files,
10589 containing one unit per file. The unit and the file name correspond,
10590 as required by GNAT.
10591
10592 If you want to permanently restructure a set of ``foreign'' files so that
10593 they match the GNAT rules, and do the remaining development using the
10594 GNAT structure, you can simply use @command{gnatchop} once, generate the
10595 new set of files and work with them from that point on.
10596
10597 Alternatively, if you want to keep your files in the ``foreign'' format,
10598 perhaps to maintain compatibility with some other Ada compilation
10599 system, you can set up a procedure where you use @command{gnatchop} each
10600 time you compile, regarding the source files that it writes as temporary
10601 files that you throw away.
10602
10603 @node Operating gnatchop in Compilation Mode
10604 @section Operating gnatchop in Compilation Mode
10605
10606 @noindent
10607 The basic function of @code{gnatchop} is to take a file with multiple units
10608 and split it into separate files. The boundary between files is reasonably
10609 clear, except for the issue of comments and pragmas. In default mode, the
10610 rule is that any pragmas between units belong to the previous unit, except
10611 that configuration pragmas always belong to the following unit. Any comments
10612 belong to the following unit. These rules
10613 almost always result in the right choice of
10614 the split point without needing to mark it explicitly and most users will
10615 find this default to be what they want. In this default mode it is incorrect to
10616 submit a file containing only configuration pragmas, or one that ends in
10617 configuration pragmas, to @code{gnatchop}.
10618
10619 However, using a special option to activate ``compilation mode'',
10620 @code{gnatchop}
10621 can perform another function, which is to provide exactly the semantics
10622 required by the RM for handling of configuration pragmas in a compilation.
10623 In the absence of configuration pragmas (at the main file level), this
10624 option has no effect, but it causes such configuration pragmas to be handled
10625 in a quite different manner.
10626
10627 First, in compilation mode, if @code{gnatchop} is given a file that consists of
10628 only configuration pragmas, then this file is appended to the
10629 @file{gnat.adc} file in the current directory. This behavior provides
10630 the required behavior described in the RM for the actions to be taken
10631 on submitting such a file to the compiler, namely that these pragmas
10632 should apply to all subsequent compilations in the same compilation
10633 environment. Using GNAT, the current directory, possibly containing a
10634 @file{gnat.adc} file is the representation
10635 of a compilation environment. For more information on the
10636 @file{gnat.adc} file, see @ref{Handling of Configuration Pragmas}.
10637
10638 Second, in compilation mode, if @code{gnatchop}
10639 is given a file that starts with
10640 configuration pragmas, and contains one or more units, then these
10641 configuration pragmas are prepended to each of the chopped files. This
10642 behavior provides the required behavior described in the RM for the
10643 actions to be taken on compiling such a file, namely that the pragmas
10644 apply to all units in the compilation, but not to subsequently compiled
10645 units.
10646
10647 Finally, if configuration pragmas appear between units, they are appended
10648 to the previous unit. This results in the previous unit being illegal,
10649 since the compiler does not accept configuration pragmas that follow
10650 a unit. This provides the required RM behavior that forbids configuration
10651 pragmas other than those preceding the first compilation unit of a
10652 compilation.
10653
10654 For most purposes, @code{gnatchop} will be used in default mode. The
10655 compilation mode described above is used only if you need exactly
10656 accurate behavior with respect to compilations, and you have files
10657 that contain multiple units and configuration pragmas. In this
10658 circumstance the use of @code{gnatchop} with the compilation mode
10659 switch provides the required behavior, and is for example the mode
10660 in which GNAT processes the ACVC tests.
10661
10662 @node Command Line for gnatchop
10663 @section Command Line for @code{gnatchop}
10664
10665 @noindent
10666 The @code{gnatchop} command has the form:
10667
10668 @smallexample
10669 $ gnatchop switches @var{file name} @r{[}@var{file name} @dots{}@r{]}
10670 @ovar{directory}
10671 @end smallexample
10672
10673 @noindent
10674 The only required argument is the file name of the file to be chopped.
10675 There are no restrictions on the form of this file name. The file itself
10676 contains one or more Ada units, in normal GNAT format, concatenated
10677 together. As shown, more than one file may be presented to be chopped.
10678
10679 When run in default mode, @code{gnatchop} generates one output file in
10680 the current directory for each unit in each of the files.
10681
10682 @var{directory}, if specified, gives the name of the directory to which
10683 the output files will be written. If it is not specified, all files are
10684 written to the current directory.
10685
10686 For example, given a
10687 file called @file{hellofiles} containing
10688
10689 @smallexample @c ada
10690 @group
10691 @cartouche
10692 procedure hello;
10693
10694 with Text_IO; use Text_IO;
10695 procedure hello is
10696 begin
10697 Put_Line ("Hello");
10698 end hello;
10699 @end cartouche
10700 @end group
10701 @end smallexample
10702
10703 @noindent
10704 the command
10705
10706 @smallexample
10707 $ gnatchop ^hellofiles^HELLOFILES.^
10708 @end smallexample
10709
10710 @noindent
10711 generates two files in the current directory, one called
10712 @file{hello.ads} containing the single line that is the procedure spec,
10713 and the other called @file{hello.adb} containing the remaining text. The
10714 original file is not affected. The generated files can be compiled in
10715 the normal manner.
10716
10717 @noindent
10718 When gnatchop is invoked on a file that is empty or that contains only empty
10719 lines and/or comments, gnatchop will not fail, but will not produce any
10720 new sources.
10721
10722 For example, given a
10723 file called @file{toto.txt} containing
10724
10725 @smallexample @c ada
10726 @group
10727 @cartouche
10728 -- Just a comment
10729 @end cartouche
10730 @end group
10731 @end smallexample
10732
10733 @noindent
10734 the command
10735
10736 @smallexample
10737 $ gnatchop ^toto.txt^TOT.TXT^
10738 @end smallexample
10739
10740 @noindent
10741 will not produce any new file and will result in the following warnings:
10742
10743 @smallexample
10744 toto.txt:1:01: warning: empty file, contains no compilation units
10745 no compilation units found
10746 no source files written
10747 @end smallexample
10748
10749 @node Switches for gnatchop
10750 @section Switches for @code{gnatchop}
10751
10752 @noindent
10753 @command{gnatchop} recognizes the following switches:
10754
10755 @table @option
10756 @c !sort!
10757
10758 @item --version
10759 @cindex @option{--version} @command{gnatchop}
10760 Display Copyright and version, then exit disregarding all other options.
10761
10762 @item --help
10763 @cindex @option{--help} @command{gnatchop}
10764 If @option{--version} was not used, display usage, then exit disregarding
10765 all other options.
10766
10767 @item ^-c^/COMPILATION^
10768 @cindex @option{^-c^/COMPILATION^} (@code{gnatchop})
10769 Causes @code{gnatchop} to operate in compilation mode, in which
10770 configuration pragmas are handled according to strict RM rules. See
10771 previous section for a full description of this mode.
10772
10773 @ifclear vms
10774 @item -gnat@var{xxx}
10775 This passes the given @option{-gnat@var{xxx}} switch to @code{gnat} which is
10776 used to parse the given file. Not all @var{xxx} options make sense,
10777 but for example, the use of @option{-gnati2} allows @code{gnatchop} to
10778 process a source file that uses Latin-2 coding for identifiers.
10779 @end ifclear
10780
10781 @item ^-h^/HELP^
10782 Causes @code{gnatchop} to generate a brief help summary to the standard
10783 output file showing usage information.
10784
10785 @item ^-k@var{mm}^/FILE_NAME_MAX_LENGTH=@var{mm}^
10786 @cindex @option{^-k^/FILE_NAME_MAX_LENGTH^} (@code{gnatchop})
10787 Limit generated file names to the specified number @code{mm}
10788 of characters.
10789 This is useful if the
10790 resulting set of files is required to be interoperable with systems
10791 which limit the length of file names.
10792 @ifset vms
10793 If no value is given, or
10794 if no @code{/FILE_NAME_MAX_LENGTH} qualifier is given,
10795 a default of 39, suitable for OpenVMS Alpha
10796 Systems, is assumed
10797 @end ifset
10798 @ifclear vms
10799 No space is allowed between the @option{-k} and the numeric value. The numeric
10800 value may be omitted in which case a default of @option{-k8},
10801 suitable for use
10802 with DOS-like file systems, is used. If no @option{-k} switch
10803 is present then
10804 there is no limit on the length of file names.
10805 @end ifclear
10806
10807 @item ^-p^/PRESERVE^
10808 @cindex @option{^-p^/PRESERVE^} (@code{gnatchop})
10809 Causes the file ^modification^creation^ time stamp of the input file to be
10810 preserved and used for the time stamp of the output file(s). This may be
10811 useful for preserving coherency of time stamps in an environment where
10812 @code{gnatchop} is used as part of a standard build process.
10813
10814 @item ^-q^/QUIET^
10815 @cindex @option{^-q^/QUIET^} (@code{gnatchop})
10816 Causes output of informational messages indicating the set of generated
10817 files to be suppressed. Warnings and error messages are unaffected.
10818
10819 @item ^-r^/REFERENCE^
10820 @cindex @option{^-r^/REFERENCE^} (@code{gnatchop})
10821 @findex Source_Reference
10822 Generate @code{Source_Reference} pragmas. Use this switch if the output
10823 files are regarded as temporary and development is to be done in terms
10824 of the original unchopped file. This switch causes
10825 @code{Source_Reference} pragmas to be inserted into each of the
10826 generated files to refers back to the original file name and line number.
10827 The result is that all error messages refer back to the original
10828 unchopped file.
10829 In addition, the debugging information placed into the object file (when
10830 the @option{^-g^/DEBUG^} switch of @command{gcc} or @command{gnatmake} is
10831 specified)
10832 also refers back to this original file so that tools like profilers and
10833 debuggers will give information in terms of the original unchopped file.
10834
10835 If the original file to be chopped itself contains
10836 a @code{Source_Reference}
10837 pragma referencing a third file, then gnatchop respects
10838 this pragma, and the generated @code{Source_Reference} pragmas
10839 in the chopped file refer to the original file, with appropriate
10840 line numbers. This is particularly useful when @code{gnatchop}
10841 is used in conjunction with @code{gnatprep} to compile files that
10842 contain preprocessing statements and multiple units.
10843
10844 @item ^-v^/VERBOSE^
10845 @cindex @option{^-v^/VERBOSE^} (@code{gnatchop})
10846 Causes @code{gnatchop} to operate in verbose mode. The version
10847 number and copyright notice are output, as well as exact copies of
10848 the gnat1 commands spawned to obtain the chop control information.
10849
10850 @item ^-w^/OVERWRITE^
10851 @cindex @option{^-w^/OVERWRITE^} (@code{gnatchop})
10852 Overwrite existing file names. Normally @code{gnatchop} regards it as a
10853 fatal error if there is already a file with the same name as a
10854 file it would otherwise output, in other words if the files to be
10855 chopped contain duplicated units. This switch bypasses this
10856 check, and causes all but the last instance of such duplicated
10857 units to be skipped.
10858
10859 @ifclear vms
10860 @item --GCC=@var{xxxx}
10861 @cindex @option{--GCC=} (@code{gnatchop})
10862 Specify the path of the GNAT parser to be used. When this switch is used,
10863 no attempt is made to add the prefix to the GNAT parser executable.
10864 @end ifclear
10865 @end table
10866
10867 @node Examples of gnatchop Usage
10868 @section Examples of @code{gnatchop} Usage
10869
10870 @table @code
10871 @ifset vms
10872 @item gnatchop /OVERWRITE HELLO_S.ADA [PRERELEASE.FILES]
10873 @end ifset
10874 @ifclear vms
10875 @item gnatchop -w hello_s.ada prerelease/files
10876 @end ifclear
10877
10878 Chops the source file @file{hello_s.ada}. The output files will be
10879 placed in the directory @file{^prerelease/files^[PRERELEASE.FILES]^},
10880 overwriting any
10881 files with matching names in that directory (no files in the current
10882 directory are modified).
10883
10884 @item gnatchop ^archive^ARCHIVE.^
10885 Chops the source file @file{^archive^ARCHIVE.^}
10886 into the current directory. One
10887 useful application of @code{gnatchop} is in sending sets of sources
10888 around, for example in email messages. The required sources are simply
10889 concatenated (for example, using a ^Unix @code{cat}^VMS @code{APPEND/NEW}^
10890 command), and then
10891 @command{gnatchop} is used at the other end to reconstitute the original
10892 file names.
10893
10894 @item gnatchop file1 file2 file3 direc
10895 Chops all units in files @file{file1}, @file{file2}, @file{file3}, placing
10896 the resulting files in the directory @file{direc}. Note that if any units
10897 occur more than once anywhere within this set of files, an error message
10898 is generated, and no files are written. To override this check, use the
10899 @option{^-w^/OVERWRITE^} switch,
10900 in which case the last occurrence in the last file will
10901 be the one that is output, and earlier duplicate occurrences for a given
10902 unit will be skipped.
10903 @end table
10904
10905 @node Configuration Pragmas
10906 @chapter Configuration Pragmas
10907 @cindex Configuration pragmas
10908 @cindex Pragmas, configuration
10909
10910 @noindent
10911 Configuration pragmas include those pragmas described as
10912 such in the Ada Reference Manual, as well as
10913 implementation-dependent pragmas that are configuration pragmas.
10914 @xref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference Manual},
10915 for details on these additional GNAT-specific configuration pragmas.
10916 Most notably, the pragma @code{Source_File_Name}, which allows
10917 specifying non-default names for source files, is a configuration
10918 pragma. The following is a complete list of configuration pragmas
10919 recognized by GNAT:
10920
10921 @smallexample
10922 Ada_83
10923 Ada_95
10924 Ada_05
10925 Ada_2005
10926 Assertion_Policy
10927 C_Pass_By_Copy
10928 Canonical_Streams
10929 Check_Name
10930 Check_Policy
10931 Compile_Time_Error
10932 Compile_Time_Warning
10933 Compiler_Unit
10934 Component_Alignment
10935 Debug_Policy
10936 Detect_Blocking
10937 Discard_Names
10938 Elaboration_Checks
10939 Eliminate
10940 Extend_System
10941 External_Name_Casing
10942 Fast_Math
10943 Favor_Top_Level
10944 Float_Representation
10945 Implicit_Packing
10946 Initialize_Scalars
10947 Interrupt_State
10948 License
10949 Locking_Policy
10950 Long_Float
10951 No_Run_Time
10952 No_Strict_Aliasing
10953 Normalize_Scalars
10954 Optimize_Alignment
10955 Persistent_BSS
10956 Polling
10957 Priority_Specific_Dispatching
10958 Profile
10959 Profile_Warnings
10960 Propagate_Exceptions
10961 Queuing_Policy
10962 Ravenscar
10963 Restricted_Run_Time
10964 Restrictions
10965 Restrictions_Warnings
10966 Reviewable
10967 Source_File_Name
10968 Source_File_Name_Project
10969 Style_Checks
10970 Suppress
10971 Suppress_Exception_Locations
10972 Task_Dispatching_Policy
10973 Universal_Data
10974 Unsuppress
10975 Use_VADS_Size
10976 Validity_Checks
10977 Warnings
10978 Wide_Character_Encoding
10979
10980 @end smallexample
10981
10982 @menu
10983 * Handling of Configuration Pragmas::
10984 * The Configuration Pragmas Files::
10985 @end menu
10986
10987 @node Handling of Configuration Pragmas
10988 @section Handling of Configuration Pragmas
10989
10990 Configuration pragmas may either appear at the start of a compilation
10991 unit, in which case they apply only to that unit, or they may apply to
10992 all compilations performed in a given compilation environment.
10993
10994 GNAT also provides the @code{gnatchop} utility to provide an automatic
10995 way to handle configuration pragmas following the semantics for
10996 compilations (that is, files with multiple units), described in the RM.
10997 See @ref{Operating gnatchop in Compilation Mode} for details.
10998 However, for most purposes, it will be more convenient to edit the
10999 @file{gnat.adc} file that contains configuration pragmas directly,
11000 as described in the following section.
11001
11002 @node The Configuration Pragmas Files
11003 @section The Configuration Pragmas Files
11004 @cindex @file{gnat.adc}
11005
11006 @noindent
11007 In GNAT a compilation environment is defined by the current
11008 directory at the time that a compile command is given. This current
11009 directory is searched for a file whose name is @file{gnat.adc}. If
11010 this file is present, it is expected to contain one or more
11011 configuration pragmas that will be applied to the current compilation.
11012 However, if the switch @option{-gnatA} is used, @file{gnat.adc} is not
11013 considered.
11014
11015 Configuration pragmas may be entered into the @file{gnat.adc} file
11016 either by running @code{gnatchop} on a source file that consists only of
11017 configuration pragmas, or more conveniently by
11018 direct editing of the @file{gnat.adc} file, which is a standard format
11019 source file.
11020
11021 In addition to @file{gnat.adc}, additional files containing configuration
11022 pragmas may be applied to the current compilation using the switch
11023 @option{-gnatec}@var{path}. @var{path} must designate an existing file that
11024 contains only configuration pragmas. These configuration pragmas are
11025 in addition to those found in @file{gnat.adc} (provided @file{gnat.adc}
11026 is present and switch @option{-gnatA} is not used).
11027
11028 It is allowed to specify several switches @option{-gnatec}, all of which
11029 will be taken into account.
11030
11031 If you are using project file, a separate mechanism is provided using
11032 project attributes, see @ref{Specifying Configuration Pragmas} for more
11033 details.
11034
11035 @ifset vms
11036 Of special interest to GNAT OpenVMS Alpha is the following
11037 configuration pragma:
11038
11039 @smallexample @c ada
11040 @cartouche
11041 pragma Extend_System (Aux_DEC);
11042 @end cartouche
11043 @end smallexample
11044
11045 @noindent
11046 In the presence of this pragma, GNAT adds to the definition of the
11047 predefined package SYSTEM all the additional types and subprograms that are
11048 defined in HP Ada. See @ref{Compatibility with HP Ada} for details.
11049 @end ifset
11050
11051 @node Handling Arbitrary File Naming Conventions Using gnatname
11052 @chapter Handling Arbitrary File Naming Conventions Using @code{gnatname}
11053 @cindex Arbitrary File Naming Conventions
11054
11055 @menu
11056 * Arbitrary File Naming Conventions::
11057 * Running gnatname::
11058 * Switches for gnatname::
11059 * Examples of gnatname Usage::
11060 @end menu
11061
11062 @node Arbitrary File Naming Conventions
11063 @section Arbitrary File Naming Conventions
11064
11065 @noindent
11066 The GNAT compiler must be able to know the source file name of a compilation
11067 unit. When using the standard GNAT default file naming conventions
11068 (@code{.ads} for specs, @code{.adb} for bodies), the GNAT compiler
11069 does not need additional information.
11070
11071 @noindent
11072 When the source file names do not follow the standard GNAT default file naming
11073 conventions, the GNAT compiler must be given additional information through
11074 a configuration pragmas file (@pxref{Configuration Pragmas})
11075 or a project file.
11076 When the non-standard file naming conventions are well-defined,
11077 a small number of pragmas @code{Source_File_Name} specifying a naming pattern
11078 (@pxref{Alternative File Naming Schemes}) may be sufficient. However,
11079 if the file naming conventions are irregular or arbitrary, a number
11080 of pragma @code{Source_File_Name} for individual compilation units
11081 must be defined.
11082 To help maintain the correspondence between compilation unit names and
11083 source file names within the compiler,
11084 GNAT provides a tool @code{gnatname} to generate the required pragmas for a
11085 set of files.
11086
11087 @node Running gnatname
11088 @section Running @code{gnatname}
11089
11090 @noindent
11091 The usual form of the @code{gnatname} command is
11092
11093 @smallexample
11094 $ gnatname @ovar{switches} @var{naming_pattern} @ovar{naming_patterns}
11095 @r{[}--and @ovar{switches} @var{naming_pattern} @ovar{naming_patterns}@r{]}
11096 @end smallexample
11097
11098 @noindent
11099 All of the arguments are optional. If invoked without any argument,
11100 @code{gnatname} will display its usage.
11101
11102 @noindent
11103 When used with at least one naming pattern, @code{gnatname} will attempt to
11104 find all the compilation units in files that follow at least one of the
11105 naming patterns. To find these compilation units,
11106 @code{gnatname} will use the GNAT compiler in syntax-check-only mode on all
11107 regular files.
11108
11109 @noindent
11110 One or several Naming Patterns may be given as arguments to @code{gnatname}.
11111 Each Naming Pattern is enclosed between double quotes.
11112 A Naming Pattern is a regular expression similar to the wildcard patterns
11113 used in file names by the Unix shells or the DOS prompt.
11114
11115 @noindent
11116 @code{gnatname} may be called with several sections of directories/patterns.
11117 Sections are separated by switch @code{--and}. In each section, there must be
11118 at least one pattern. If no directory is specified in a section, the current
11119 directory (or the project directory is @code{-P} is used) is implied.
11120 The options other that the directory switches and the patterns apply globally
11121 even if they are in different sections.
11122
11123 @noindent
11124 Examples of Naming Patterns are
11125
11126 @smallexample
11127 "*.[12].ada"
11128 "*.ad[sb]*"
11129 "body_*" "spec_*"
11130 @end smallexample
11131
11132 @noindent
11133 For a more complete description of the syntax of Naming Patterns,
11134 see the second kind of regular expressions described in @file{g-regexp.ads}
11135 (the ``Glob'' regular expressions).
11136
11137 @noindent
11138 When invoked with no switch @code{-P}, @code{gnatname} will create a
11139 configuration pragmas file @file{gnat.adc} in the current working directory,
11140 with pragmas @code{Source_File_Name} for each file that contains a valid Ada
11141 unit.
11142
11143 @node Switches for gnatname
11144 @section Switches for @code{gnatname}
11145
11146 @noindent
11147 Switches for @code{gnatname} must precede any specified Naming Pattern.
11148
11149 @noindent
11150 You may specify any of the following switches to @code{gnatname}:
11151
11152 @table @option
11153 @c !sort!
11154
11155 @item --version
11156 @cindex @option{--version} @command{gnatname}
11157 Display Copyright and version, then exit disregarding all other options.
11158
11159 @item --help
11160 @cindex @option{--help} @command{gnatname}
11161 If @option{--version} was not used, display usage, then exit disregarding
11162 all other options.
11163
11164 @item --and
11165 Start another section of directories/patterns.
11166
11167 @item ^-c^/CONFIG_FILE=^@file{file}
11168 @cindex @option{^-c^/CONFIG_FILE^} (@code{gnatname})
11169 Create a configuration pragmas file @file{file} (instead of the default
11170 @file{gnat.adc}).
11171 @ifclear vms
11172 There may be zero, one or more space between @option{-c} and
11173 @file{file}.
11174 @end ifclear
11175 @file{file} may include directory information. @file{file} must be
11176 writable. There may be only one switch @option{^-c^/CONFIG_FILE^}.
11177 When a switch @option{^-c^/CONFIG_FILE^} is
11178 specified, no switch @option{^-P^/PROJECT_FILE^} may be specified (see below).
11179
11180 @item ^-d^/SOURCE_DIRS=^@file{dir}
11181 @cindex @option{^-d^/SOURCE_DIRS^} (@code{gnatname})
11182 Look for source files in directory @file{dir}. There may be zero, one or more
11183 spaces between @option{^-d^/SOURCE_DIRS=^} and @file{dir}.
11184 When a switch @option{^-d^/SOURCE_DIRS^}
11185 is specified, the current working directory will not be searched for source
11186 files, unless it is explicitly specified with a @option{^-d^/SOURCE_DIRS^}
11187 or @option{^-D^/DIR_FILES^} switch.
11188 Several switches @option{^-d^/SOURCE_DIRS^} may be specified.
11189 If @file{dir} is a relative path, it is relative to the directory of
11190 the configuration pragmas file specified with switch
11191 @option{^-c^/CONFIG_FILE^},
11192 or to the directory of the project file specified with switch
11193 @option{^-P^/PROJECT_FILE^} or,
11194 if neither switch @option{^-c^/CONFIG_FILE^}
11195 nor switch @option{^-P^/PROJECT_FILE^} are specified, it is relative to the
11196 current working directory. The directory
11197 specified with switch @option{^-d^/SOURCE_DIRS^} must exist and be readable.
11198
11199 @item ^-D^/DIRS_FILE=^@file{file}
11200 @cindex @option{^-D^/DIRS_FILE^} (@code{gnatname})
11201 Look for source files in all directories listed in text file @file{file}.
11202 There may be zero, one or more spaces between @option{^-D^/DIRS_FILE=^}
11203 and @file{file}.
11204 @file{file} must be an existing, readable text file.
11205 Each nonempty line in @file{file} must be a directory.
11206 Specifying switch @option{^-D^/DIRS_FILE^} is equivalent to specifying as many
11207 switches @option{^-d^/SOURCE_DIRS^} as there are nonempty lines in
11208 @file{file}.
11209
11210 @item ^-f^/FOREIGN_PATTERN=^@file{pattern}
11211 @cindex @option{^-f^/FOREIGN_PATTERN^} (@code{gnatname})
11212 Foreign patterns. Using this switch, it is possible to add sources of languages
11213 other than Ada to the list of sources of a project file.
11214 It is only useful if a ^-P^/PROJECT_FILE^ switch is used.
11215 For example,
11216 @smallexample
11217 gnatname ^-Pprj -f"*.c"^/PROJECT_FILE=PRJ /FOREIGN_PATTERN=*.C^ "*.ada"
11218 @end smallexample
11219 @noindent
11220 will look for Ada units in all files with the @file{.ada} extension,
11221 and will add to the list of file for project @file{prj.gpr} the C files
11222 with extension @file{.^c^C^}.
11223
11224 @item ^-h^/HELP^
11225 @cindex @option{^-h^/HELP^} (@code{gnatname})
11226 Output usage (help) information. The output is written to @file{stdout}.
11227
11228 @item ^-P^/PROJECT_FILE=^@file{proj}
11229 @cindex @option{^-P^/PROJECT_FILE^} (@code{gnatname})
11230 Create or update project file @file{proj}. There may be zero, one or more space
11231 between @option{-P} and @file{proj}. @file{proj} may include directory
11232 information. @file{proj} must be writable.
11233 There may be only one switch @option{^-P^/PROJECT_FILE^}.
11234 When a switch @option{^-P^/PROJECT_FILE^} is specified,
11235 no switch @option{^-c^/CONFIG_FILE^} may be specified.
11236
11237 @item ^-v^/VERBOSE^
11238 @cindex @option{^-v^/VERBOSE^} (@code{gnatname})
11239 Verbose mode. Output detailed explanation of behavior to @file{stdout}.
11240 This includes name of the file written, the name of the directories to search
11241 and, for each file in those directories whose name matches at least one of
11242 the Naming Patterns, an indication of whether the file contains a unit,
11243 and if so the name of the unit.
11244
11245 @item ^-v -v^/VERBOSE /VERBOSE^
11246 @cindex @option{^-v -v^/VERBOSE /VERBOSE^} (@code{gnatname})
11247 Very Verbose mode. In addition to the output produced in verbose mode,
11248 for each file in the searched directories whose name matches none of
11249 the Naming Patterns, an indication is given that there is no match.
11250
11251 @item ^-x^/EXCLUDED_PATTERN=^@file{pattern}
11252 @cindex @option{^-x^/EXCLUDED_PATTERN^} (@code{gnatname})
11253 Excluded patterns. Using this switch, it is possible to exclude some files
11254 that would match the name patterns. For example,
11255 @smallexample
11256 gnatname ^-x "*_nt.ada"^/EXCLUDED_PATTERN=*_nt.ada^ "*.ada"
11257 @end smallexample
11258 @noindent
11259 will look for Ada units in all files with the @file{.ada} extension,
11260 except those whose names end with @file{_nt.ada}.
11261
11262 @end table
11263
11264 @node Examples of gnatname Usage
11265 @section Examples of @code{gnatname} Usage
11266
11267 @ifset vms
11268 @smallexample
11269 $ gnatname /CONFIG_FILE=[HOME.ME]NAMES.ADC /SOURCE_DIRS=SOURCES "[a-z]*.ada*"
11270 @end smallexample
11271 @end ifset
11272
11273 @ifclear vms
11274 @smallexample
11275 $ gnatname -c /home/me/names.adc -d sources "[a-z]*.ada*"
11276 @end smallexample
11277 @end ifclear
11278
11279 @noindent
11280 In this example, the directory @file{^/home/me^[HOME.ME]^} must already exist
11281 and be writable. In addition, the directory
11282 @file{^/home/me/sources^[HOME.ME.SOURCES]^} (specified by
11283 @option{^-d sources^/SOURCE_DIRS=SOURCES^}) must exist and be readable.
11284
11285 @ifclear vms
11286 Note the optional spaces after @option{-c} and @option{-d}.
11287 @end ifclear
11288
11289 @smallexample
11290 @ifclear vms
11291 $ gnatname -P/home/me/proj -x "*_nt_body.ada"
11292 -dsources -dsources/plus -Dcommon_dirs.txt "body_*" "spec_*"
11293 @end ifclear
11294 @ifset vms
11295 $ gnatname /PROJECT_FILE=[HOME.ME]PROJ
11296 /EXCLUDED_PATTERN=*_nt_body.ada
11297 /SOURCE_DIRS=(SOURCES,[SOURCES.PLUS])
11298 /DIRS_FILE=COMMON_DIRS.TXT "body_*" "spec_*"
11299 @end ifset
11300 @end smallexample
11301
11302 Note that several switches @option{^-d^/SOURCE_DIRS^} may be used,
11303 even in conjunction with one or several switches
11304 @option{^-D^/DIRS_FILE^}. Several Naming Patterns and one excluded pattern
11305 are used in this example.
11306
11307 @c *****************************************
11308 @c * G N A T P r o j e c t M a n a g e r *
11309 @c *****************************************
11310 @node GNAT Project Manager
11311 @chapter GNAT Project Manager
11312
11313 @menu
11314 * Introduction::
11315 * Examples of Project Files::
11316 * Project File Syntax::
11317 * Objects and Sources in Project Files::
11318 * Importing Projects::
11319 * Project Extension::
11320 * Project Hierarchy Extension::
11321 * External References in Project Files::
11322 * Packages in Project Files::
11323 * Variables from Imported Projects::
11324 * Naming Schemes::
11325 * Library Projects::
11326 * Stand-alone Library Projects::
11327 * Switches Related to Project Files::
11328 * Tools Supporting Project Files::
11329 * An Extended Example::
11330 * Project File Complete Syntax::
11331 @end menu
11332
11333 @c ****************
11334 @c * Introduction *
11335 @c ****************
11336
11337 @node Introduction
11338 @section Introduction
11339
11340 @noindent
11341 This chapter describes GNAT's @emph{Project Manager}, a facility that allows
11342 you to manage complex builds involving a number of source files, directories,
11343 and compilation options for different system configurations. In particular,
11344 project files allow you to specify:
11345 @itemize @bullet
11346 @item
11347 The directory or set of directories containing the source files, and/or the
11348 names of the specific source files themselves
11349 @item
11350 The directory in which the compiler's output
11351 (@file{ALI} files, object files, tree files) is to be placed
11352 @item
11353 The directory in which the executable programs is to be placed
11354 @item
11355 ^Switch^Switch^ settings for any of the project-enabled tools
11356 (@command{gnatmake}, compiler, binder, linker, @code{gnatls}, @code{gnatxref},
11357 @code{gnatfind}); you can apply these settings either globally or to individual
11358 compilation units.
11359 @item
11360 The source files containing the main subprogram(s) to be built
11361 @item
11362 The source programming language(s) (currently Ada and/or C)
11363 @item
11364 Source file naming conventions; you can specify these either globally or for
11365 individual compilation units
11366 @end itemize
11367
11368 @menu
11369 * Project Files::
11370 @end menu
11371
11372 @node Project Files
11373 @subsection Project Files
11374
11375 @noindent
11376 Project files are written in a syntax close to that of Ada, using familiar
11377 notions such as packages, context clauses, declarations, default values,
11378 assignments, and inheritance. Finally, project files can be built
11379 hierarchically from other project files, simplifying complex system
11380 integration and project reuse.
11381
11382 A @dfn{project} is a specific set of values for various compilation properties.
11383 The settings for a given project are described by means of
11384 a @dfn{project file}, which is a text file written in an Ada-like syntax.
11385 Property values in project files are either strings or lists of strings.
11386 Properties that are not explicitly set receive default values. A project
11387 file may interrogate the values of @dfn{external variables} (user-defined
11388 command-line switches or environment variables), and it may specify property
11389 settings conditionally, based on the value of such variables.
11390
11391 In simple cases, a project's source files depend only on other source files
11392 in the same project, or on the predefined libraries. (@emph{Dependence} is
11393 used in
11394 the Ada technical sense; as in one Ada unit @code{with}ing another.) However,
11395 the Project Manager also allows more sophisticated arrangements,
11396 where the source files in one project depend on source files in other
11397 projects:
11398 @itemize @bullet
11399 @item
11400 One project can @emph{import} other projects containing needed source files.
11401 @item
11402 You can organize GNAT projects in a hierarchy: a @emph{child} project
11403 can extend a @emph{parent} project, inheriting the parent's source files and
11404 optionally overriding any of them with alternative versions
11405 @end itemize
11406
11407 @noindent
11408 More generally, the Project Manager lets you structure large development
11409 efforts into hierarchical subsystems, where build decisions are delegated
11410 to the subsystem level, and thus different compilation environments
11411 (^switch^switch^ settings) used for different subsystems.
11412
11413 The Project Manager is invoked through the
11414 @option{^-P^/PROJECT_FILE=^@emph{projectfile}}
11415 switch to @command{gnatmake} or to the @command{^gnat^GNAT^} front driver.
11416 @ifclear vms
11417 There may be zero, one or more spaces between @option{-P} and
11418 @option{@emph{projectfile}}.
11419 @end ifclear
11420 If you want to define (on the command line) an external variable that is
11421 queried by the project file, you must use the
11422 @option{^-X^/EXTERNAL_REFERENCE=^@emph{vbl}=@emph{value}} switch.
11423 The Project Manager parses and interprets the project file, and drives the
11424 invoked tool based on the project settings.
11425
11426 The Project Manager supports a wide range of development strategies,
11427 for systems of all sizes. Here are some typical practices that are
11428 easily handled:
11429 @itemize @bullet
11430 @item
11431 Using a common set of source files, but generating object files in different
11432 directories via different ^switch^switch^ settings
11433 @item
11434 Using a mostly-shared set of source files, but with different versions of
11435 some unit or units
11436 @end itemize
11437
11438 @noindent
11439 The destination of an executable can be controlled inside a project file
11440 using the @option{^-o^-o^}
11441 ^switch^switch^.
11442 In the absence of such a ^switch^switch^ either inside
11443 the project file or on the command line, any executable files generated by
11444 @command{gnatmake} are placed in the directory @code{Exec_Dir} specified
11445 in the project file. If no @code{Exec_Dir} is specified, they will be placed
11446 in the object directory of the project.
11447
11448 You can use project files to achieve some of the effects of a source
11449 versioning system (for example, defining separate projects for
11450 the different sets of sources that comprise different releases) but the
11451 Project Manager is independent of any source configuration management tools
11452 that might be used by the developers.
11453
11454 The next section introduces the main features of GNAT's project facility
11455 through a sequence of examples; subsequent sections will present the syntax
11456 and semantics in more detail. A more formal description of the project
11457 facility appears in @ref{Project File Reference,,, gnat_rm, GNAT
11458 Reference Manual}.
11459
11460 @c *****************************
11461 @c * Examples of Project Files *
11462 @c *****************************
11463
11464 @node Examples of Project Files
11465 @section Examples of Project Files
11466 @noindent
11467 This section illustrates some of the typical uses of project files and
11468 explains their basic structure and behavior.
11469
11470 @menu
11471 * Common Sources with Different ^Switches^Switches^ and Directories::
11472 * Using External Variables::
11473 * Importing Other Projects::
11474 * Extending a Project::
11475 @end menu
11476
11477 @node Common Sources with Different ^Switches^Switches^ and Directories
11478 @subsection Common Sources with Different ^Switches^Switches^ and Directories
11479
11480 @menu
11481 * Source Files::
11482 * Specifying the Object Directory::
11483 * Specifying the Exec Directory::
11484 * Project File Packages::
11485 * Specifying ^Switch^Switch^ Settings::
11486 * Main Subprograms::
11487 * Executable File Names::
11488 * Source File Naming Conventions::
11489 * Source Language(s)::
11490 @end menu
11491
11492 @noindent
11493 Suppose that the Ada source files @file{pack.ads}, @file{pack.adb}, and
11494 @file{proc.adb} are in the @file{/common} directory. The file
11495 @file{proc.adb} contains an Ada main subprogram @code{Proc} that @code{with}s
11496 package @code{Pack}. We want to compile these source files under two sets
11497 of ^switches^switches^:
11498 @itemize @bullet
11499 @item
11500 When debugging, we want to pass the @option{-g} switch to @command{gnatmake},
11501 and the @option{^-gnata^-gnata^},
11502 @option{^-gnato^-gnato^},
11503 and @option{^-gnatE^-gnatE^} switches to the
11504 compiler; the compiler's output is to appear in @file{/common/debug}
11505 @item
11506 When preparing a release version, we want to pass the @option{^-O2^O2^} switch
11507 to the compiler; the compiler's output is to appear in @file{/common/release}
11508 @end itemize
11509
11510 @noindent
11511 The GNAT project files shown below, respectively @file{debug.gpr} and
11512 @file{release.gpr} in the @file{/common} directory, achieve these effects.
11513
11514 Schematically:
11515 @smallexample
11516 @group
11517 ^/common^[COMMON]^
11518 debug.gpr
11519 release.gpr
11520 pack.ads
11521 pack.adb
11522 proc.adb
11523 @end group
11524 @group
11525 ^/common/debug^[COMMON.DEBUG]^
11526 proc.ali, proc.o
11527 pack.ali, pack.o
11528 @end group
11529 @group
11530 ^/common/release^[COMMON.RELEASE]^
11531 proc.ali, proc.o
11532 pack.ali, pack.o
11533 @end group
11534 @end smallexample
11535 Here are the corresponding project files:
11536
11537 @smallexample @c projectfile
11538 @group
11539 project Debug is
11540 for Object_Dir use "debug";
11541 for Main use ("proc");
11542
11543 package Builder is
11544 for ^Default_Switches^Default_Switches^ ("Ada")
11545 use ("^-g^-g^");
11546 for Executable ("proc.adb") use "proc1";
11547 end Builder;
11548 @end group
11549
11550 @group
11551 package Compiler is
11552 for ^Default_Switches^Default_Switches^ ("Ada")
11553 use ("-fstack-check",
11554 "^-gnata^-gnata^",
11555 "^-gnato^-gnato^",
11556 "^-gnatE^-gnatE^");
11557 end Compiler;
11558 end Debug;
11559 @end group
11560 @end smallexample
11561
11562 @smallexample @c projectfile
11563 @group
11564 project Release is
11565 for Object_Dir use "release";
11566 for Exec_Dir use ".";
11567 for Main use ("proc");
11568
11569 package Compiler is
11570 for ^Default_Switches^Default_Switches^ ("Ada")
11571 use ("^-O2^-O2^");
11572 end Compiler;
11573 end Release;
11574 @end group
11575 @end smallexample
11576
11577 @noindent
11578 The name of the project defined by @file{debug.gpr} is @code{"Debug"} (case
11579 insensitive), and analogously the project defined by @file{release.gpr} is
11580 @code{"Release"}. For consistency the file should have the same name as the
11581 project, and the project file's extension should be @code{"gpr"}. These
11582 conventions are not required, but a warning is issued if they are not followed.
11583
11584 If the current directory is @file{^/temp^[TEMP]^}, then the command
11585 @smallexample
11586 gnatmake ^-P/common/debug.gpr^/PROJECT_FILE=[COMMON]DEBUG^
11587 @end smallexample
11588
11589 @noindent
11590 generates object and ALI files in @file{^/common/debug^[COMMON.DEBUG]^},
11591 as well as the @code{^proc1^PROC1.EXE^} executable,
11592 using the ^switch^switch^ settings defined in the project file.
11593
11594 Likewise, the command
11595 @smallexample
11596 gnatmake ^-P/common/release.gpr^/PROJECT_FILE=[COMMON]RELEASE^
11597 @end smallexample
11598
11599 @noindent
11600 generates object and ALI files in @file{^/common/release^[COMMON.RELEASE]^},
11601 and the @code{^proc^PROC.EXE^}
11602 executable in @file{^/common^[COMMON]^},
11603 using the ^switch^switch^ settings from the project file.
11604
11605 @node Source Files
11606 @unnumberedsubsubsec Source Files
11607
11608 @noindent
11609 If a project file does not explicitly specify a set of source directories or
11610 a set of source files, then by default the project's source files are the
11611 Ada source files in the project file directory. Thus @file{pack.ads},
11612 @file{pack.adb}, and @file{proc.adb} are the source files for both projects.
11613
11614 @node Specifying the Object Directory
11615 @unnumberedsubsubsec Specifying the Object Directory
11616
11617 @noindent
11618 Several project properties are modeled by Ada-style @emph{attributes};
11619 a property is defined by supplying the equivalent of an Ada attribute
11620 definition clause in the project file.
11621 A project's object directory is another such a property; the corresponding
11622 attribute is @code{Object_Dir}, and its value is also a string expression,
11623 specified either as absolute or relative. In the later case,
11624 it is relative to the project file directory. Thus the compiler's
11625 output is directed to @file{^/common/debug^[COMMON.DEBUG]^}
11626 (for the @code{Debug} project)
11627 and to @file{^/common/release^[COMMON.RELEASE]^}
11628 (for the @code{Release} project).
11629 If @code{Object_Dir} is not specified, then the default is the project file
11630 directory itself.
11631
11632 @node Specifying the Exec Directory
11633 @unnumberedsubsubsec Specifying the Exec Directory
11634
11635 @noindent
11636 A project's exec directory is another property; the corresponding
11637 attribute is @code{Exec_Dir}, and its value is also a string expression,
11638 either specified as relative or absolute. If @code{Exec_Dir} is not specified,
11639 then the default is the object directory (which may also be the project file
11640 directory if attribute @code{Object_Dir} is not specified). Thus the executable
11641 is placed in @file{^/common/debug^[COMMON.DEBUG]^}
11642 for the @code{Debug} project (attribute @code{Exec_Dir} not specified)
11643 and in @file{^/common^[COMMON]^} for the @code{Release} project.
11644
11645 @node Project File Packages
11646 @unnumberedsubsubsec Project File Packages
11647
11648 @noindent
11649 A GNAT tool that is integrated with the Project Manager is modeled by a
11650 corresponding package in the project file. In the example above,
11651 The @code{Debug} project defines the packages @code{Builder}
11652 (for @command{gnatmake}) and @code{Compiler};
11653 the @code{Release} project defines only the @code{Compiler} package.
11654
11655 The Ada-like package syntax is not to be taken literally. Although packages in
11656 project files bear a surface resemblance to packages in Ada source code, the
11657 notation is simply a way to convey a grouping of properties for a named
11658 entity. Indeed, the package names permitted in project files are restricted
11659 to a predefined set, corresponding to the project-aware tools, and the contents
11660 of packages are limited to a small set of constructs.
11661 The packages in the example above contain attribute definitions.
11662
11663 @node Specifying ^Switch^Switch^ Settings
11664 @unnumberedsubsubsec Specifying ^Switch^Switch^ Settings
11665
11666 @noindent
11667 ^Switch^Switch^ settings for a project-aware tool can be specified through
11668 attributes in the package that corresponds to the tool.
11669 The example above illustrates one of the relevant attributes,
11670 @code{^Default_Switches^Default_Switches^}, which is defined in packages
11671 in both project files.
11672 Unlike simple attributes like @code{Source_Dirs},
11673 @code{^Default_Switches^Default_Switches^} is
11674 known as an @emph{associative array}. When you define this attribute, you must
11675 supply an ``index'' (a literal string), and the effect of the attribute
11676 definition is to set the value of the array at the specified index.
11677 For the @code{^Default_Switches^Default_Switches^} attribute,
11678 the index is a programming language (in our case, Ada),
11679 and the value specified (after @code{use}) must be a list
11680 of string expressions.
11681
11682 The attributes permitted in project files are restricted to a predefined set.
11683 Some may appear at project level, others in packages.
11684 For any attribute that is an associative array, the index must always be a
11685 literal string, but the restrictions on this string (e.g., a file name or a
11686 language name) depend on the individual attribute.
11687 Also depending on the attribute, its specified value will need to be either a
11688 string or a string list.
11689
11690 In the @code{Debug} project, we set the switches for two tools,
11691 @command{gnatmake} and the compiler, and thus we include the two corresponding
11692 packages; each package defines the @code{^Default_Switches^Default_Switches^}
11693 attribute with index @code{"Ada"}.
11694 Note that the package corresponding to
11695 @command{gnatmake} is named @code{Builder}. The @code{Release} project is
11696 similar, but only includes the @code{Compiler} package.
11697
11698 In project @code{Debug} above, the ^switches^switches^ starting with
11699 @option{-gnat} that are specified in package @code{Compiler}
11700 could have been placed in package @code{Builder}, since @command{gnatmake}
11701 transmits all such ^switches^switches^ to the compiler.
11702
11703 @node Main Subprograms
11704 @unnumberedsubsubsec Main Subprograms
11705
11706 @noindent
11707 One of the specifiable properties of a project is a list of files that contain
11708 main subprograms. This property is captured in the @code{Main} attribute,
11709 whose value is a list of strings. If a project defines the @code{Main}
11710 attribute, it is not necessary to identify the main subprogram(s) when
11711 invoking @command{gnatmake} (@pxref{gnatmake and Project Files}).
11712
11713 @node Executable File Names
11714 @unnumberedsubsubsec Executable File Names
11715
11716 @noindent
11717 By default, the executable file name corresponding to a main source is
11718 deduced from the main source file name. Through the attributes
11719 @code{Executable} and @code{Executable_Suffix} of package @code{Builder},
11720 it is possible to change this default.
11721 In project @code{Debug} above, the executable file name
11722 for main source @file{^proc.adb^PROC.ADB^} is
11723 @file{^proc1^PROC1.EXE^}.
11724 Attribute @code{Executable_Suffix}, when specified, may change the suffix
11725 of the executable files, when no attribute @code{Executable} applies:
11726 its value replace the platform-specific executable suffix.
11727 Attributes @code{Executable} and @code{Executable_Suffix} are the only ways to
11728 specify a non-default executable file name when several mains are built at once
11729 in a single @command{gnatmake} command.
11730
11731 @node Source File Naming Conventions
11732 @unnumberedsubsubsec Source File Naming Conventions
11733
11734 @noindent
11735 Since the project files above do not specify any source file naming
11736 conventions, the GNAT defaults are used. The mechanism for defining source
11737 file naming conventions -- a package named @code{Naming} --
11738 is described below (@pxref{Naming Schemes}).
11739
11740 @node Source Language(s)
11741 @unnumberedsubsubsec Source Language(s)
11742
11743 @noindent
11744 Since the project files do not specify a @code{Languages} attribute, by
11745 default the GNAT tools assume that the language of the project file is Ada.
11746 More generally, a project can comprise source files
11747 in Ada, C, and/or other languages.
11748
11749 @node Using External Variables
11750 @subsection Using External Variables
11751
11752 @noindent
11753 Instead of supplying different project files for debug and release, we can
11754 define a single project file that queries an external variable (set either
11755 on the command line or via an ^environment variable^logical name^) in order to
11756 conditionally define the appropriate settings. Again, assume that the
11757 source files @file{pack.ads}, @file{pack.adb}, and @file{proc.adb} are
11758 located in directory @file{^/common^[COMMON]^}. The following project file,
11759 @file{build.gpr}, queries the external variable named @code{STYLE} and
11760 defines an object directory and ^switch^switch^ settings based on whether
11761 the value is @code{"deb"} (debug) or @code{"rel"} (release), and where
11762 the default is @code{"deb"}.
11763
11764 @smallexample @c projectfile
11765 @group
11766 project Build is
11767 for Main use ("proc");
11768
11769 type Style_Type is ("deb", "rel");
11770 Style : Style_Type := external ("STYLE", "deb");
11771
11772 case Style is
11773 when "deb" =>
11774 for Object_Dir use "debug";
11775
11776 when "rel" =>
11777 for Object_Dir use "release";
11778 for Exec_Dir use ".";
11779 end case;
11780 @end group
11781
11782 @group
11783 package Builder is
11784
11785 case Style is
11786 when "deb" =>
11787 for ^Default_Switches^Default_Switches^ ("Ada")
11788 use ("^-g^-g^");
11789 for Executable ("proc") use "proc1";
11790 when others =>
11791 null;
11792 end case;
11793
11794 end Builder;
11795 @end group
11796
11797 @group
11798 package Compiler is
11799
11800 case Style is
11801 when "deb" =>
11802 for ^Default_Switches^Default_Switches^ ("Ada")
11803 use ("^-gnata^-gnata^",
11804 "^-gnato^-gnato^",
11805 "^-gnatE^-gnatE^");
11806
11807 when "rel" =>
11808 for ^Default_Switches^Default_Switches^ ("Ada")
11809 use ("^-O2^-O2^");
11810 end case;
11811
11812 end Compiler;
11813
11814 end Build;
11815 @end group
11816 @end smallexample
11817
11818 @noindent
11819 @code{Style_Type} is an example of a @emph{string type}, which is the project
11820 file analog of an Ada enumeration type but whose components are string literals
11821 rather than identifiers. @code{Style} is declared as a variable of this type.
11822
11823 The form @code{external("STYLE", "deb")} is known as an
11824 @emph{external reference}; its first argument is the name of an
11825 @emph{external variable}, and the second argument is a default value to be
11826 used if the external variable doesn't exist. You can define an external
11827 variable on the command line via the @option{^-X^/EXTERNAL_REFERENCE^} switch,
11828 or you can use ^an environment variable^a logical name^
11829 as an external variable.
11830
11831 Each @code{case} construct is expanded by the Project Manager based on the
11832 value of @code{Style}. Thus the command
11833 @ifclear vms
11834 @smallexample
11835 gnatmake -P/common/build.gpr -XSTYLE=deb
11836 @end smallexample
11837 @end ifclear
11838
11839 @ifset vms
11840 @smallexample
11841 gnatmake /PROJECT_FILE=[COMMON]BUILD.GPR /EXTERNAL_REFERENCE=STYLE=deb
11842 @end smallexample
11843 @end ifset
11844
11845 @noindent
11846 is equivalent to the @command{gnatmake} invocation using the project file
11847 @file{debug.gpr} in the earlier example. So is the command
11848 @smallexample
11849 gnatmake ^-P/common/build.gpr^/PROJECT_FILE=[COMMON]BUILD.GPR^
11850 @end smallexample
11851
11852 @noindent
11853 since @code{"deb"} is the default for @code{STYLE}.
11854
11855 Analogously,
11856
11857 @ifclear vms
11858 @smallexample
11859 gnatmake -P/common/build.gpr -XSTYLE=rel
11860 @end smallexample
11861 @end ifclear
11862
11863 @ifset vms
11864 @smallexample
11865 GNAT MAKE /PROJECT_FILE=[COMMON]BUILD.GPR /EXTERNAL_REFERENCE=STYLE=rel
11866 @end smallexample
11867 @end ifset
11868
11869 @noindent
11870 is equivalent to the @command{gnatmake} invocation using the project file
11871 @file{release.gpr} in the earlier example.
11872
11873 @node Importing Other Projects
11874 @subsection Importing Other Projects
11875 @cindex @code{ADA_PROJECT_PATH}
11876
11877 @noindent
11878 A compilation unit in a source file in one project may depend on compilation
11879 units in source files in other projects. To compile this unit under
11880 control of a project file, the
11881 dependent project must @emph{import} the projects containing the needed source
11882 files.
11883 This effect is obtained using syntax similar to an Ada @code{with} clause,
11884 but where @code{with}ed entities are strings that denote project files.
11885
11886 As an example, suppose that the two projects @code{GUI_Proj} and
11887 @code{Comm_Proj} are defined in the project files @file{gui_proj.gpr} and
11888 @file{comm_proj.gpr} in directories @file{^/gui^[GUI]^}
11889 and @file{^/comm^[COMM]^}, respectively.
11890 Suppose that the source files for @code{GUI_Proj} are
11891 @file{gui.ads} and @file{gui.adb}, and that the source files for
11892 @code{Comm_Proj} are @file{comm.ads} and @file{comm.adb}, where each set of
11893 files is located in its respective project file directory. Schematically:
11894
11895 @smallexample
11896 @group
11897 ^/gui^[GUI]^
11898 gui_proj.gpr
11899 gui.ads
11900 gui.adb
11901 @end group
11902
11903 @group
11904 ^/comm^[COMM]^
11905 comm_proj.gpr
11906 comm.ads
11907 comm.adb
11908 @end group
11909 @end smallexample
11910
11911 @noindent
11912 We want to develop an application in directory @file{^/app^[APP]^} that
11913 @code{with} the packages @code{GUI} and @code{Comm}, using the properties of
11914 the corresponding project files (e.g.@: the ^switch^switch^ settings
11915 and object directory).
11916 Skeletal code for a main procedure might be something like the following:
11917
11918 @smallexample @c ada
11919 @group
11920 with GUI, Comm;
11921 procedure App_Main is
11922 @dots{}
11923 begin
11924 @dots{}
11925 end App_Main;
11926 @end group
11927 @end smallexample
11928
11929 @noindent
11930 Here is a project file, @file{app_proj.gpr}, that achieves the desired
11931 effect:
11932
11933 @smallexample @c projectfile
11934 @group
11935 with "/gui/gui_proj", "/comm/comm_proj";
11936 project App_Proj is
11937 for Main use ("app_main");
11938 end App_Proj;
11939 @end group
11940 @end smallexample
11941
11942 @noindent
11943 Building an executable is achieved through the command:
11944 @smallexample
11945 gnatmake ^-P/app/app_proj^/PROJECT_FILE=[APP]APP_PROJ^
11946 @end smallexample
11947 @noindent
11948 which will generate the @code{^app_main^APP_MAIN.EXE^} executable
11949 in the directory where @file{app_proj.gpr} resides.
11950
11951 If an imported project file uses the standard extension (@code{^gpr^GPR^}) then
11952 (as illustrated above) the @code{with} clause can omit the extension.
11953
11954 Our example specified an absolute path for each imported project file.
11955 Alternatively, the directory name of an imported object can be omitted
11956 if either
11957 @itemize @bullet
11958 @item
11959 The imported project file is in the same directory as the importing project
11960 file, or
11961 @item
11962 You have defined ^an environment variable^a logical name^
11963 that includes the directory containing
11964 the needed project file. The syntax of @code{ADA_PROJECT_PATH} is the same as
11965 the syntax of @code{ADA_INCLUDE_PATH} and @code{ADA_OBJECTS_PATH}: a list of
11966 directory names separated by colons (semicolons on Windows).
11967 @end itemize
11968
11969 @noindent
11970 Thus, if we define @code{ADA_PROJECT_PATH} to include @file{^/gui^[GUI]^} and
11971 @file{^/comm^[COMM]^}, then our project file @file{app_proj.gpr} can be written
11972 as follows:
11973
11974 @smallexample @c projectfile
11975 @group
11976 with "gui_proj", "comm_proj";
11977 project App_Proj is
11978 for Main use ("app_main");
11979 end App_Proj;
11980 @end group
11981 @end smallexample
11982
11983 @noindent
11984 Importing other projects can create ambiguities.
11985 For example, the same unit might be present in different imported projects, or
11986 it might be present in both the importing project and in an imported project.
11987 Both of these conditions are errors. Note that in the current version of
11988 the Project Manager, it is illegal to have an ambiguous unit even if the
11989 unit is never referenced by the importing project. This restriction may be
11990 relaxed in a future release.
11991
11992 @node Extending a Project
11993 @subsection Extending a Project
11994
11995 @noindent
11996 In large software systems it is common to have multiple
11997 implementations of a common interface; in Ada terms, multiple versions of a
11998 package body for the same spec. For example, one implementation
11999 might be safe for use in tasking programs, while another might only be used
12000 in sequential applications. This can be modeled in GNAT using the concept
12001 of @emph{project extension}. If one project (the ``child'') @emph{extends}
12002 another project (the ``parent'') then by default all source files of the
12003 parent project are inherited by the child, but the child project can
12004 override any of the parent's source files with new versions, and can also
12005 add new files. This facility is the project analog of a type extension in
12006 Object-Oriented Programming. Project hierarchies are permitted (a child
12007 project may be the parent of yet another project), and a project that
12008 inherits one project can also import other projects.
12009
12010 As an example, suppose that directory @file{^/seq^[SEQ]^} contains the project
12011 file @file{seq_proj.gpr} as well as the source files @file{pack.ads},
12012 @file{pack.adb}, and @file{proc.adb}:
12013
12014 @smallexample
12015 @group
12016 ^/seq^[SEQ]^
12017 pack.ads
12018 pack.adb
12019 proc.adb
12020 seq_proj.gpr
12021 @end group
12022 @end smallexample
12023
12024 @noindent
12025 Note that the project file can simply be empty (that is, no attribute or
12026 package is defined):
12027
12028 @smallexample @c projectfile
12029 @group
12030 project Seq_Proj is
12031 end Seq_Proj;
12032 @end group
12033 @end smallexample
12034
12035 @noindent
12036 implying that its source files are all the Ada source files in the project
12037 directory.
12038
12039 Suppose we want to supply an alternate version of @file{pack.adb}, in
12040 directory @file{^/tasking^[TASKING]^}, but use the existing versions of
12041 @file{pack.ads} and @file{proc.adb}. We can define a project
12042 @code{Tasking_Proj} that inherits @code{Seq_Proj}:
12043
12044 @smallexample
12045 @group
12046 ^/tasking^[TASKING]^
12047 pack.adb
12048 tasking_proj.gpr
12049 @end group
12050
12051 @group
12052 project Tasking_Proj extends "/seq/seq_proj" is
12053 end Tasking_Proj;
12054 @end group
12055 @end smallexample
12056
12057 @noindent
12058 The version of @file{pack.adb} used in a build depends on which project file
12059 is specified.
12060
12061 Note that we could have obtained the desired behavior using project import
12062 rather than project inheritance; a @code{base} project would contain the
12063 sources for @file{pack.ads} and @file{proc.adb}, a sequential project would
12064 import @code{base} and add @file{pack.adb}, and likewise a tasking project
12065 would import @code{base} and add a different version of @file{pack.adb}. The
12066 choice depends on whether other sources in the original project need to be
12067 overridden. If they do, then project extension is necessary, otherwise,
12068 importing is sufficient.
12069
12070 @noindent
12071 In a project file that extends another project file, it is possible to
12072 indicate that an inherited source is not part of the sources of the extending
12073 project. This is necessary sometimes when a package spec has been overloaded
12074 and no longer requires a body: in this case, it is necessary to indicate that
12075 the inherited body is not part of the sources of the project, otherwise there
12076 will be a compilation error when compiling the spec.
12077
12078 For that purpose, the attribute @code{Excluded_Source_Files} is used.
12079 Its value is a string list: a list of file names. It is also possible to use
12080 attribute @code{Excluded_Source_List_File}. Its value is a single string:
12081 the file name of a text file containing a list of file names, one per line.
12082
12083 @smallexample @c @projectfile
12084 project B extends "a" is
12085 for Source_Files use ("pkg.ads");
12086 -- New spec of Pkg does not need a completion
12087 for Excluded_Source_Files use ("pkg.adb");
12088 end B;
12089 @end smallexample
12090
12091 Attribute @code{Excluded_Source_Files} may also be used to check if a source
12092 is still needed: if it is possible to build using @command{gnatmake} when such
12093 a source is put in attribute @code{Excluded_Source_Files} of a project P, then
12094 it is possible to remove the source completely from a system that includes
12095 project P.
12096
12097 @c ***********************
12098 @c * Project File Syntax *
12099 @c ***********************
12100
12101 @node Project File Syntax
12102 @section Project File Syntax
12103
12104 @menu
12105 * Basic Syntax::
12106 * Qualified Projects::
12107 * Packages::
12108 * Expressions::
12109 * String Types::
12110 * Variables::
12111 * Attributes::
12112 * Associative Array Attributes::
12113 * case Constructions::
12114 @end menu
12115
12116 @noindent
12117 This section describes the structure of project files.
12118
12119 A project may be an @emph{independent project}, entirely defined by a single
12120 project file. Any Ada source file in an independent project depends only
12121 on the predefined library and other Ada source files in the same project.
12122
12123 @noindent
12124 A project may also @dfn{depend on} other projects, in either or both of
12125 the following ways:
12126 @itemize @bullet
12127 @item It may import any number of projects
12128 @item It may extend at most one other project
12129 @end itemize
12130
12131 @noindent
12132 The dependence relation is a directed acyclic graph (the subgraph reflecting
12133 the ``extends'' relation is a tree).
12134
12135 A project's @dfn{immediate sources} are the source files directly defined by
12136 that project, either implicitly by residing in the project file's directory,
12137 or explicitly through any of the source-related attributes described below.
12138 More generally, a project @var{proj}'s @dfn{sources} are the immediate sources
12139 of @var{proj} together with the immediate sources (unless overridden) of any
12140 project on which @var{proj} depends (either directly or indirectly).
12141
12142 @node Basic Syntax
12143 @subsection Basic Syntax
12144
12145 @noindent
12146 As seen in the earlier examples, project files have an Ada-like syntax.
12147 The minimal project file is:
12148 @smallexample @c projectfile
12149 @group
12150 project Empty is
12151
12152 end Empty;
12153 @end group
12154 @end smallexample
12155
12156 @noindent
12157 The identifier @code{Empty} is the name of the project.
12158 This project name must be present after the reserved
12159 word @code{end} at the end of the project file, followed by a semi-colon.
12160
12161 Any name in a project file, such as the project name or a variable name,
12162 has the same syntax as an Ada identifier.
12163
12164 The reserved words of project files are the Ada 95 reserved words plus
12165 @code{extends}, @code{external}, and @code{project}. Note that the only Ada
12166 reserved words currently used in project file syntax are:
12167
12168 @itemize @bullet
12169 @item
12170 @code{all}
12171 @item
12172 @code{at}
12173 @item
12174 @code{case}
12175 @item
12176 @code{end}
12177 @item
12178 @code{for}
12179 @item
12180 @code{is}
12181 @item
12182 @code{limited}
12183 @item
12184 @code{null}
12185 @item
12186 @code{others}
12187 @item
12188 @code{package}
12189 @item
12190 @code{renames}
12191 @item
12192 @code{type}
12193 @item
12194 @code{use}
12195 @item
12196 @code{when}
12197 @item
12198 @code{with}
12199 @end itemize
12200
12201 @noindent
12202 Comments in project files have the same syntax as in Ada, two consecutive
12203 hyphens through the end of the line.
12204
12205 @node Qualified Projects
12206 @subsection Qualified Projects
12207
12208 @noindent
12209 Before the reserved @code{project}, there may be one or two "qualifiers", that
12210 is identifiers or other reserved words, to qualify the project.
12211
12212 The current list of qualifiers is:
12213
12214 @itemize @bullet
12215 @item
12216 @code{abstract}: qualify a project with no sources. An abstract project must
12217 have a declaration specifying that there are no sources in the project, and,
12218 if it extends another project, the project it extends must also be a qualified
12219 abstract project.
12220
12221 @item
12222 @code{standard}: a standard project is a non library project with sources.
12223
12224 @item
12225 @code{aggregate}: for future extension
12226
12227 @item
12228 @code{aggregate library}: for future extension
12229
12230 @item
12231 @code{library}: a library project must declare both attributes
12232 @code{Library_Name} and @code{Library_Dir}.
12233
12234 @item
12235 @code{configuration}: a configuration project cannot be in a project tree.
12236 @end itemize
12237
12238 @node Packages
12239 @subsection Packages
12240
12241 @noindent
12242 A project file may contain @emph{packages}. The name of a package must be one
12243 of the identifiers from the following list. A package
12244 with a given name may only appear once in a project file. Package names are
12245 case insensitive. The following package names are legal:
12246
12247 @itemize @bullet
12248 @item
12249 @code{Naming}
12250 @item
12251 @code{Builder}
12252 @item
12253 @code{Compiler}
12254 @item
12255 @code{Binder}
12256 @item
12257 @code{Linker}
12258 @item
12259 @code{Finder}
12260 @item
12261 @code{Cross_Reference}
12262 @item
12263 @code{Eliminate}
12264 @item
12265 @code{Pretty_Printer}
12266 @item
12267 @code{Metrics}
12268 @item
12269 @code{gnatls}
12270 @item
12271 @code{gnatstub}
12272 @item
12273 @code{IDE}
12274 @item
12275 @code{Language_Processing}
12276 @end itemize
12277
12278 @noindent
12279 In its simplest form, a package may be empty:
12280
12281 @smallexample @c projectfile
12282 @group
12283 project Simple is
12284 package Builder is
12285 end Builder;
12286 end Simple;
12287 @end group
12288 @end smallexample
12289
12290 @noindent
12291 A package may contain @emph{attribute declarations},
12292 @emph{variable declarations} and @emph{case constructions}, as will be
12293 described below.
12294
12295 When there is ambiguity between a project name and a package name,
12296 the name always designates the project. To avoid possible confusion, it is
12297 always a good idea to avoid naming a project with one of the
12298 names allowed for packages or any name that starts with @code{gnat}.
12299
12300 @node Expressions
12301 @subsection Expressions
12302
12303 @noindent
12304 An @emph{expression} is either a @emph{string expression} or a
12305 @emph{string list expression}.
12306
12307 A @emph{string expression} is either a @emph{simple string expression} or a
12308 @emph{compound string expression}.
12309
12310 A @emph{simple string expression} is one of the following:
12311 @itemize @bullet
12312 @item A literal string; e.g.@: @code{"comm/my_proj.gpr"}
12313 @item A string-valued variable reference (@pxref{Variables})
12314 @item A string-valued attribute reference (@pxref{Attributes})
12315 @item An external reference (@pxref{External References in Project Files})
12316 @end itemize
12317
12318 @noindent
12319 A @emph{compound string expression} is a concatenation of string expressions,
12320 using the operator @code{"&"}
12321 @smallexample
12322 Path & "/" & File_Name & ".ads"
12323 @end smallexample
12324
12325 @noindent
12326 A @emph{string list expression} is either a
12327 @emph{simple string list expression} or a
12328 @emph{compound string list expression}.
12329
12330 A @emph{simple string list expression} is one of the following:
12331 @itemize @bullet
12332 @item A parenthesized list of zero or more string expressions,
12333 separated by commas
12334 @smallexample
12335 File_Names := (File_Name, "gnat.adc", File_Name & ".orig");
12336 Empty_List := ();
12337 @end smallexample
12338 @item A string list-valued variable reference
12339 @item A string list-valued attribute reference
12340 @end itemize
12341
12342 @noindent
12343 A @emph{compound string list expression} is the concatenation (using
12344 @code{"&"}) of a simple string list expression and an expression. Note that
12345 each term in a compound string list expression, except the first, may be
12346 either a string expression or a string list expression.
12347
12348 @smallexample @c projectfile
12349 @group
12350 File_Name_List := () & File_Name; -- One string in this list
12351 Extended_File_Name_List := File_Name_List & (File_Name & ".orig");
12352 -- Two strings
12353 Big_List := File_Name_List & Extended_File_Name_List;
12354 -- Concatenation of two string lists: three strings
12355 Illegal_List := "gnat.adc" & Extended_File_Name_List;
12356 -- Illegal: must start with a string list
12357 @end group
12358 @end smallexample
12359
12360 @node String Types
12361 @subsection String Types
12362
12363 @noindent
12364 A @emph{string type declaration} introduces a discrete set of string literals.
12365 If a string variable is declared to have this type, its value
12366 is restricted to the given set of literals.
12367
12368 Here is an example of a string type declaration:
12369
12370 @smallexample @c projectfile
12371 type OS is ("NT", "nt", "Unix", "GNU/Linux", "other OS");
12372 @end smallexample
12373
12374 @noindent
12375 Variables of a string type are called @emph{typed variables}; all other
12376 variables are called @emph{untyped variables}. Typed variables are
12377 particularly useful in @code{case} constructions, to support conditional
12378 attribute declarations.
12379 (@pxref{case Constructions}).
12380
12381 The string literals in the list are case sensitive and must all be different.
12382 They may include any graphic characters allowed in Ada, including spaces.
12383
12384 A string type may only be declared at the project level, not inside a package.
12385
12386 A string type may be referenced by its name if it has been declared in the same
12387 project file, or by an expanded name whose prefix is the name of the project
12388 in which it is declared.
12389
12390 @node Variables
12391 @subsection Variables
12392
12393 @noindent
12394 A variable may be declared at the project file level, or within a package.
12395 Here are some examples of variable declarations:
12396
12397 @smallexample @c projectfile
12398 @group
12399 This_OS : OS := external ("OS"); -- a typed variable declaration
12400 That_OS := "GNU/Linux"; -- an untyped variable declaration
12401 @end group
12402 @end smallexample
12403
12404 @noindent
12405 The syntax of a @emph{typed variable declaration} is identical to the Ada
12406 syntax for an object declaration. By contrast, the syntax of an untyped
12407 variable declaration is identical to an Ada assignment statement. In fact,
12408 variable declarations in project files have some of the characteristics of
12409 an assignment, in that successive declarations for the same variable are
12410 allowed. Untyped variable declarations do establish the expected kind of the
12411 variable (string or string list), and successive declarations for it must
12412 respect the initial kind.
12413
12414 @noindent
12415 A string variable declaration (typed or untyped) declares a variable
12416 whose value is a string. This variable may be used as a string expression.
12417 @smallexample @c projectfile
12418 File_Name := "readme.txt";
12419 Saved_File_Name := File_Name & ".saved";
12420 @end smallexample
12421
12422 @noindent
12423 A string list variable declaration declares a variable whose value is a list
12424 of strings. The list may contain any number (zero or more) of strings.
12425
12426 @smallexample @c projectfile
12427 Empty_List := ();
12428 List_With_One_Element := ("^-gnaty^-gnaty^");
12429 List_With_Two_Elements := List_With_One_Element & "^-gnatg^-gnatg^";
12430 Long_List := ("main.ada", "pack1_.ada", "pack1.ada", "pack2_.ada"
12431 "pack2.ada", "util_.ada", "util.ada");
12432 @end smallexample
12433
12434 @noindent
12435 The same typed variable may not be declared more than once at project level,
12436 and it may not be declared more than once in any package; it is in effect
12437 a constant.
12438
12439 The same untyped variable may be declared several times. Declarations are
12440 elaborated in the order in which they appear, so the new value replaces
12441 the old one, and any subsequent reference to the variable uses the new value.
12442 However, as noted above, if a variable has been declared as a string, all
12443 subsequent
12444 declarations must give it a string value. Similarly, if a variable has
12445 been declared as a string list, all subsequent declarations
12446 must give it a string list value.
12447
12448 A @emph{variable reference} may take several forms:
12449
12450 @itemize @bullet
12451 @item The simple variable name, for a variable in the current package (if any)
12452 or in the current project
12453 @item An expanded name, whose prefix is a context name.
12454 @end itemize
12455
12456 @noindent
12457 A @emph{context} may be one of the following:
12458
12459 @itemize @bullet
12460 @item The name of an existing package in the current project
12461 @item The name of an imported project of the current project
12462 @item The name of an ancestor project (i.e., a project extended by the current
12463 project, either directly or indirectly)
12464 @item An expanded name whose prefix is an imported/parent project name, and
12465 whose selector is a package name in that project.
12466 @end itemize
12467
12468 @noindent
12469 A variable reference may be used in an expression.
12470
12471 @node Attributes
12472 @subsection Attributes
12473
12474 @noindent
12475 A project (and its packages) may have @emph{attributes} that define
12476 the project's properties. Some attributes have values that are strings;
12477 others have values that are string lists.
12478
12479 There are two categories of attributes: @emph{simple attributes}
12480 and @emph{associative arrays} (@pxref{Associative Array Attributes}).
12481
12482 Legal project attribute names, and attribute names for each legal package are
12483 listed below. Attributes names are case-insensitive.
12484
12485 The following attributes are defined on projects (all are simple attributes):
12486
12487 @multitable @columnfractions .4 .3
12488 @item @emph{Attribute Name}
12489 @tab @emph{Value}
12490 @item @code{Source_Files}
12491 @tab string list
12492 @item @code{Source_Dirs}
12493 @tab string list
12494 @item @code{Source_List_File}
12495 @tab string
12496 @item @code{Object_Dir}
12497 @tab string
12498 @item @code{Exec_Dir}
12499 @tab string
12500 @item @code{Excluded_Source_Dirs}
12501 @tab string list
12502 @item @code{Excluded_Source_Files}
12503 @tab string list
12504 @item @code{Excluded_Source_List_File}
12505 @tab string
12506 @item @code{Languages}
12507 @tab string list
12508 @item @code{Main}
12509 @tab string list
12510 @item @code{Library_Dir}
12511 @tab string
12512 @item @code{Library_Name}
12513 @tab string
12514 @item @code{Library_Kind}
12515 @tab string
12516 @item @code{Library_Version}
12517 @tab string
12518 @item @code{Library_Interface}
12519 @tab string
12520 @item @code{Library_Auto_Init}
12521 @tab string
12522 @item @code{Library_Options}
12523 @tab string list
12524 @item @code{Library_Src_Dir}
12525 @tab string
12526 @item @code{Library_ALI_Dir}
12527 @tab string
12528 @item @code{Library_GCC}
12529 @tab string
12530 @item @code{Library_Symbol_File}
12531 @tab string
12532 @item @code{Library_Symbol_Policy}
12533 @tab string
12534 @item @code{Library_Reference_Symbol_File}
12535 @tab string
12536 @item @code{Externally_Built}
12537 @tab string
12538 @end multitable
12539
12540 @noindent
12541 The following attributes are defined for package @code{Naming}
12542 (@pxref{Naming Schemes}):
12543
12544 @multitable @columnfractions .4 .2 .2 .2
12545 @item Attribute Name @tab Category @tab Index @tab Value
12546 @item @code{Spec_Suffix}
12547 @tab associative array
12548 @tab language name
12549 @tab string
12550 @item @code{Body_Suffix}
12551 @tab associative array
12552 @tab language name
12553 @tab string
12554 @item @code{Separate_Suffix}
12555 @tab simple attribute
12556 @tab n/a
12557 @tab string
12558 @item @code{Casing}
12559 @tab simple attribute
12560 @tab n/a
12561 @tab string
12562 @item @code{Dot_Replacement}
12563 @tab simple attribute
12564 @tab n/a
12565 @tab string
12566 @item @code{Spec}
12567 @tab associative array
12568 @tab Ada unit name
12569 @tab string
12570 @item @code{Body}
12571 @tab associative array
12572 @tab Ada unit name
12573 @tab string
12574 @item @code{Specification_Exceptions}
12575 @tab associative array
12576 @tab language name
12577 @tab string list
12578 @item @code{Implementation_Exceptions}
12579 @tab associative array
12580 @tab language name
12581 @tab string list
12582 @end multitable
12583
12584 @noindent
12585 The following attributes are defined for packages @code{Builder},
12586 @code{Compiler}, @code{Binder},
12587 @code{Linker}, @code{Cross_Reference}, and @code{Finder}
12588 (@pxref{^Switches^Switches^ and Project Files}).
12589
12590 @multitable @columnfractions .4 .2 .2 .2
12591 @item Attribute Name @tab Category @tab Index @tab Value
12592 @item @code{^Default_Switches^Default_Switches^}
12593 @tab associative array
12594 @tab language name
12595 @tab string list
12596 @item @code{^Switches^Switches^}
12597 @tab associative array
12598 @tab file name
12599 @tab string list
12600 @end multitable
12601
12602 @noindent
12603 In addition, package @code{Compiler} has a single string attribute
12604 @code{Local_Configuration_Pragmas} and package @code{Builder} has a single
12605 string attribute @code{Global_Configuration_Pragmas}.
12606
12607 @noindent
12608 Each simple attribute has a default value: the empty string (for string-valued
12609 attributes) and the empty list (for string list-valued attributes).
12610
12611 An attribute declaration defines a new value for an attribute.
12612
12613 Examples of simple attribute declarations:
12614
12615 @smallexample @c projectfile
12616 for Object_Dir use "objects";
12617 for Source_Dirs use ("units", "test/drivers");
12618 @end smallexample
12619
12620 @noindent
12621 The syntax of a @dfn{simple attribute declaration} is similar to that of an
12622 attribute definition clause in Ada.
12623
12624 Attributes references may be appear in expressions.
12625 The general form for such a reference is @code{<entity>'<attribute>}:
12626 Associative array attributes are functions. Associative
12627 array attribute references must have an argument that is a string literal.
12628
12629 Examples are:
12630
12631 @smallexample @c projectfile
12632 project'Object_Dir
12633 Naming'Dot_Replacement
12634 Imported_Project'Source_Dirs
12635 Imported_Project.Naming'Casing
12636 Builder'^Default_Switches^Default_Switches^("Ada")
12637 @end smallexample
12638
12639 @noindent
12640 The prefix of an attribute may be:
12641 @itemize @bullet
12642 @item @code{project} for an attribute of the current project
12643 @item The name of an existing package of the current project
12644 @item The name of an imported project
12645 @item The name of a parent project that is extended by the current project
12646 @item An expanded name whose prefix is imported/parent project name,
12647 and whose selector is a package name
12648 @end itemize
12649
12650 @noindent
12651 Example:
12652 @smallexample @c projectfile
12653 @group
12654 project Prj is
12655 for Source_Dirs use project'Source_Dirs & "units";
12656 for Source_Dirs use project'Source_Dirs & "test/drivers"
12657 end Prj;
12658 @end group
12659 @end smallexample
12660
12661 @noindent
12662 In the first attribute declaration, initially the attribute @code{Source_Dirs}
12663 has the default value: an empty string list. After this declaration,
12664 @code{Source_Dirs} is a string list of one element: @code{"units"}.
12665 After the second attribute declaration @code{Source_Dirs} is a string list of
12666 two elements: @code{"units"} and @code{"test/drivers"}.
12667
12668 Note: this example is for illustration only. In practice,
12669 the project file would contain only one attribute declaration:
12670
12671 @smallexample @c projectfile
12672 for Source_Dirs use ("units", "test/drivers");
12673 @end smallexample
12674
12675 @node Associative Array Attributes
12676 @subsection Associative Array Attributes
12677
12678 @noindent
12679 Some attributes are defined as @emph{associative arrays}. An associative
12680 array may be regarded as a function that takes a string as a parameter
12681 and delivers a string or string list value as its result.
12682
12683 Here are some examples of single associative array attribute associations:
12684
12685 @smallexample @c projectfile
12686 for Body ("main") use "Main.ada";
12687 for ^Switches^Switches^ ("main.ada")
12688 use ("^-v^-v^",
12689 "^-gnatv^-gnatv^");
12690 for ^Switches^Switches^ ("main.ada")
12691 use Builder'^Switches^Switches^ ("main.ada")
12692 & "^-g^-g^";
12693 @end smallexample
12694
12695 @noindent
12696 Like untyped variables and simple attributes, associative array attributes
12697 may be declared several times. Each declaration supplies a new value for the
12698 attribute, and replaces the previous setting.
12699
12700 @noindent
12701 An associative array attribute may be declared as a full associative array
12702 declaration, with the value of the same attribute in an imported or extended
12703 project.
12704
12705 @smallexample @c projectfile
12706 package Builder is
12707 for Default_Switches use Default.Builder'Default_Switches;
12708 end Builder;
12709 @end smallexample
12710
12711 @noindent
12712 In this example, @code{Default} must be either a project imported by the
12713 current project, or the project that the current project extends. If the
12714 attribute is in a package (in this case, in package @code{Builder}), the same
12715 package needs to be specified.
12716
12717 @noindent
12718 A full associative array declaration replaces any other declaration for the
12719 attribute, including other full associative array declaration. Single
12720 associative array associations may be declare after a full associative
12721 declaration, modifying the value for a single association of the attribute.
12722
12723 @node case Constructions
12724 @subsection @code{case} Constructions
12725
12726 @noindent
12727 A @code{case} construction is used in a project file to effect conditional
12728 behavior.
12729 Here is a typical example:
12730
12731 @smallexample @c projectfile
12732 @group
12733 project MyProj is
12734 type OS_Type is ("GNU/Linux", "Unix", "NT", "VMS");
12735
12736 OS : OS_Type := external ("OS", "GNU/Linux");
12737 @end group
12738
12739 @group
12740 package Compiler is
12741 case OS is
12742 when "GNU/Linux" | "Unix" =>
12743 for ^Default_Switches^Default_Switches^ ("Ada")
12744 use ("^-gnath^-gnath^");
12745 when "NT" =>
12746 for ^Default_Switches^Default_Switches^ ("Ada")
12747 use ("^-gnatP^-gnatP^");
12748 when others =>
12749 end case;
12750 end Compiler;
12751 end MyProj;
12752 @end group
12753 @end smallexample
12754
12755 @noindent
12756 The syntax of a @code{case} construction is based on the Ada case statement
12757 (although there is no @code{null} construction for empty alternatives).
12758
12759 The case expression must be a typed string variable.
12760 Each alternative comprises the reserved word @code{when}, either a list of
12761 literal strings separated by the @code{"|"} character or the reserved word
12762 @code{others}, and the @code{"=>"} token.
12763 Each literal string must belong to the string type that is the type of the
12764 case variable.
12765 An @code{others} alternative, if present, must occur last.
12766
12767 After each @code{=>}, there are zero or more constructions. The only
12768 constructions allowed in a case construction are other case constructions,
12769 attribute declarations and variable declarations. String type declarations and
12770 package declarations are not allowed. Variable declarations are restricted to
12771 variables that have already been declared before the case construction.
12772
12773 The value of the case variable is often given by an external reference
12774 (@pxref{External References in Project Files}).
12775
12776 @c ****************************************
12777 @c * Objects and Sources in Project Files *
12778 @c ****************************************
12779
12780 @node Objects and Sources in Project Files
12781 @section Objects and Sources in Project Files
12782
12783 @menu
12784 * Object Directory::
12785 * Exec Directory::
12786 * Source Directories::
12787 * Source File Names::
12788 @end menu
12789
12790 @noindent
12791 Each project has exactly one object directory and one or more source
12792 directories. The source directories must contain at least one source file,
12793 unless the project file explicitly specifies that no source files are present
12794 (@pxref{Source File Names}).
12795
12796 @node Object Directory
12797 @subsection Object Directory
12798
12799 @noindent
12800 The object directory for a project is the directory containing the compiler's
12801 output (such as @file{ALI} files and object files) for the project's immediate
12802 sources.
12803
12804 The object directory is given by the value of the attribute @code{Object_Dir}
12805 in the project file.
12806
12807 @smallexample @c projectfile
12808 for Object_Dir use "objects";
12809 @end smallexample
12810
12811 @noindent
12812 The attribute @code{Object_Dir} has a string value, the path name of the object
12813 directory. The path name may be absolute or relative to the directory of the
12814 project file. This directory must already exist, and be readable and writable.
12815
12816 By default, when the attribute @code{Object_Dir} is not given an explicit value
12817 or when its value is the empty string, the object directory is the same as the
12818 directory containing the project file.
12819
12820 @node Exec Directory
12821 @subsection Exec Directory
12822
12823 @noindent
12824 The exec directory for a project is the directory containing the executables
12825 for the project's main subprograms.
12826
12827 The exec directory is given by the value of the attribute @code{Exec_Dir}
12828 in the project file.
12829
12830 @smallexample @c projectfile
12831 for Exec_Dir use "executables";
12832 @end smallexample
12833
12834 @noindent
12835 The attribute @code{Exec_Dir} has a string value, the path name of the exec
12836 directory. The path name may be absolute or relative to the directory of the
12837 project file. This directory must already exist, and be writable.
12838
12839 By default, when the attribute @code{Exec_Dir} is not given an explicit value
12840 or when its value is the empty string, the exec directory is the same as the
12841 object directory of the project file.
12842
12843 @node Source Directories
12844 @subsection Source Directories
12845
12846 @noindent
12847 The source directories of a project are specified by the project file
12848 attribute @code{Source_Dirs}.
12849
12850 This attribute's value is a string list. If the attribute is not given an
12851 explicit value, then there is only one source directory, the one where the
12852 project file resides.
12853
12854 A @code{Source_Dirs} attribute that is explicitly defined to be the empty list,
12855 as in
12856
12857 @smallexample @c projectfile
12858 for Source_Dirs use ();
12859 @end smallexample
12860
12861 @noindent
12862 indicates that the project contains no source files.
12863
12864 Otherwise, each string in the string list designates one or more
12865 source directories.
12866
12867 @smallexample @c projectfile
12868 for Source_Dirs use ("sources", "test/drivers");
12869 @end smallexample
12870
12871 @noindent
12872 If a string in the list ends with @code{"/**"}, then the directory whose path
12873 name precedes the two asterisks, as well as all its subdirectories
12874 (recursively), are source directories.
12875
12876 @smallexample @c projectfile
12877 for Source_Dirs use ("/system/sources/**");
12878 @end smallexample
12879
12880 @noindent
12881 Here the directory @code{/system/sources} and all of its subdirectories
12882 (recursively) are source directories.
12883
12884 To specify that the source directories are the directory of the project file
12885 and all of its subdirectories, you can declare @code{Source_Dirs} as follows:
12886 @smallexample @c projectfile
12887 for Source_Dirs use ("./**");
12888 @end smallexample
12889
12890 @noindent
12891 Each of the source directories must exist and be readable.
12892
12893 @node Source File Names
12894 @subsection Source File Names
12895
12896 @noindent
12897 In a project that contains source files, their names may be specified by the
12898 attributes @code{Source_Files} (a string list) or @code{Source_List_File}
12899 (a string). Source file names never include any directory information.
12900
12901 If the attribute @code{Source_Files} is given an explicit value, then each
12902 element of the list is a source file name.
12903
12904 @smallexample @c projectfile
12905 for Source_Files use ("main.adb");
12906 for Source_Files use ("main.adb", "pack1.ads", "pack2.adb");
12907 @end smallexample
12908
12909 @noindent
12910 If the attribute @code{Source_Files} is not given an explicit value,
12911 but the attribute @code{Source_List_File} is given a string value,
12912 then the source file names are contained in the text file whose path name
12913 (absolute or relative to the directory of the project file) is the
12914 value of the attribute @code{Source_List_File}.
12915
12916 Each line in the file that is not empty or is not a comment
12917 contains a source file name.
12918
12919 @smallexample @c projectfile
12920 for Source_List_File use "source_list.txt";
12921 @end smallexample
12922
12923 @noindent
12924 By default, if neither the attribute @code{Source_Files} nor the attribute
12925 @code{Source_List_File} is given an explicit value, then each file in the
12926 source directories that conforms to the project's naming scheme
12927 (@pxref{Naming Schemes}) is an immediate source of the project.
12928
12929 A warning is issued if both attributes @code{Source_Files} and
12930 @code{Source_List_File} are given explicit values. In this case, the attribute
12931 @code{Source_Files} prevails.
12932
12933 Each source file name must be the name of one existing source file
12934 in one of the source directories.
12935
12936 A @code{Source_Files} attribute whose value is an empty list
12937 indicates that there are no source files in the project.
12938
12939 If the order of the source directories is known statically, that is if
12940 @code{"/**"} is not used in the string list @code{Source_Dirs}, then there may
12941 be several files with the same source file name. In this case, only the file
12942 in the first directory is considered as an immediate source of the project
12943 file. If the order of the source directories is not known statically, it is
12944 an error to have several files with the same source file name.
12945
12946 Projects can be specified to have no Ada source
12947 files: the value of (@code{Source_Dirs} or @code{Source_Files} may be an empty
12948 list, or the @code{"Ada"} may be absent from @code{Languages}:
12949
12950 @smallexample @c projectfile
12951 for Source_Dirs use ();
12952 for Source_Files use ();
12953 for Languages use ("C", "C++");
12954 @end smallexample
12955
12956 @noindent
12957 Otherwise, a project must contain at least one immediate source.
12958
12959 Projects with no source files are useful as template packages
12960 (@pxref{Packages in Project Files}) for other projects; in particular to
12961 define a package @code{Naming} (@pxref{Naming Schemes}).
12962
12963 @c ****************************
12964 @c * Importing Projects *
12965 @c ****************************
12966
12967 @node Importing Projects
12968 @section Importing Projects
12969 @cindex @code{ADA_PROJECT_PATH}
12970
12971 @noindent
12972 An immediate source of a project P may depend on source files that
12973 are neither immediate sources of P nor in the predefined library.
12974 To get this effect, P must @emph{import} the projects that contain the needed
12975 source files.
12976
12977 @smallexample @c projectfile
12978 @group
12979 with "project1", "utilities.gpr";
12980 with "/namings/apex.gpr";
12981 project Main is
12982 @dots{}
12983 @end group
12984 @end smallexample
12985
12986 @noindent
12987 As can be seen in this example, the syntax for importing projects is similar
12988 to the syntax for importing compilation units in Ada. However, project files
12989 use literal strings instead of names, and the @code{with} clause identifies
12990 project files rather than packages.
12991
12992 Each literal string is the file name or path name (absolute or relative) of a
12993 project file. If a string corresponds to a file name, with no path or a
12994 relative path, then its location is determined by the @emph{project path}. The
12995 latter can be queried using @code{gnatls -v}. It contains:
12996
12997 @itemize @bullet
12998 @item
12999 In first position, the directory containing the current project file.
13000 @item
13001 In last position, the default project directory. This default project directory
13002 is part of the GNAT installation and is the standard place to install project
13003 files giving access to standard support libraries.
13004 @ifclear vms
13005 @ref{Installing a library}
13006 @end ifclear
13007
13008 @item
13009 In between, all the directories referenced in the
13010 ^environment variable^logical name^ @env{ADA_PROJECT_PATH} if it exists.
13011 @end itemize
13012
13013 @noindent
13014 If a relative pathname is used, as in
13015
13016 @smallexample @c projectfile
13017 with "tests/proj";
13018 @end smallexample
13019
13020 @noindent
13021 then the full path for the project is constructed by concatenating this
13022 relative path to those in the project path, in order, until a matching file is
13023 found. Any symbolic link will be fully resolved in the directory of the
13024 importing project file before the imported project file is examined.
13025
13026 If the @code{with}'ed project file name does not have an extension,
13027 the default is @file{^.gpr^.GPR^}. If a file with this extension is not found,
13028 then the file name as specified in the @code{with} clause (no extension) will
13029 be used. In the above example, if a file @code{project1.gpr} is found, then it
13030 will be used; otherwise, if a file @code{^project1^PROJECT1^} exists
13031 then it will be used; if neither file exists, this is an error.
13032
13033 A warning is issued if the name of the project file does not match the
13034 name of the project; this check is case insensitive.
13035
13036 Any source file that is an immediate source of the imported project can be
13037 used by the immediate sources of the importing project, transitively. Thus
13038 if @code{A} imports @code{B}, and @code{B} imports @code{C}, the immediate
13039 sources of @code{A} may depend on the immediate sources of @code{C}, even if
13040 @code{A} does not import @code{C} explicitly. However, this is not recommended,
13041 because if and when @code{B} ceases to import @code{C}, some sources in
13042 @code{A} will no longer compile.
13043
13044 A side effect of this capability is that normally cyclic dependencies are not
13045 permitted: if @code{A} imports @code{B} (directly or indirectly) then @code{B}
13046 is not allowed to import @code{A}. However, there are cases when cyclic
13047 dependencies would be beneficial. For these cases, another form of import
13048 between projects exists, the @code{limited with}: a project @code{A} that
13049 imports a project @code{B} with a straight @code{with} may also be imported,
13050 directly or indirectly, by @code{B} on the condition that imports from @code{B}
13051 to @code{A} include at least one @code{limited with}.
13052
13053 @smallexample @c 0projectfile
13054 with "../b/b.gpr";
13055 with "../c/c.gpr";
13056 project A is
13057 end A;
13058
13059 limited with "../a/a.gpr";
13060 project B is
13061 end B;
13062
13063 with "../d/d.gpr";
13064 project C is
13065 end C;
13066
13067 limited with "../a/a.gpr";
13068 project D is
13069 end D;
13070 @end smallexample
13071
13072 @noindent
13073 In the above legal example, there are two project cycles:
13074 @itemize @bullet
13075 @item A-> B-> A
13076 @item A -> C -> D -> A
13077 @end itemize
13078
13079 @noindent
13080 In each of these cycle there is one @code{limited with}: import of @code{A}
13081 from @code{B} and import of @code{A} from @code{D}.
13082
13083 The difference between straight @code{with} and @code{limited with} is that
13084 the name of a project imported with a @code{limited with} cannot be used in the
13085 project that imports it. In particular, its packages cannot be renamed and
13086 its variables cannot be referred to.
13087
13088 An exception to the above rules for @code{limited with} is that for the main
13089 project specified to @command{gnatmake} or to the @command{GNAT} driver a
13090 @code{limited with} is equivalent to a straight @code{with}. For example,
13091 in the example above, projects @code{B} and @code{D} could not be main
13092 projects for @command{gnatmake} or to the @command{GNAT} driver, because they
13093 each have a @code{limited with} that is the only one in a cycle of importing
13094 projects.
13095
13096 @c *********************
13097 @c * Project Extension *
13098 @c *********************
13099
13100 @node Project Extension
13101 @section Project Extension
13102
13103 @noindent
13104 During development of a large system, it is sometimes necessary to use
13105 modified versions of some of the source files, without changing the original
13106 sources. This can be achieved through the @emph{project extension} facility.
13107
13108 @smallexample @c projectfile
13109 project Modified_Utilities extends "/baseline/utilities.gpr" is @dots{}
13110 @end smallexample
13111
13112 @noindent
13113 A project extension declaration introduces an extending project
13114 (the @emph{child}) and a project being extended (the @emph{parent}).
13115
13116 By default, a child project inherits all the sources of its parent.
13117 However, inherited sources can be overridden: a unit in a parent is hidden
13118 by a unit of the same name in the child.
13119
13120 Inherited sources are considered to be sources (but not immediate sources)
13121 of the child project; see @ref{Project File Syntax}.
13122
13123 An inherited source file retains any switches specified in the parent project.
13124
13125 For example if the project @code{Utilities} contains the spec and the
13126 body of an Ada package @code{Util_IO}, then the project
13127 @code{Modified_Utilities} can contain a new body for package @code{Util_IO}.
13128 The original body of @code{Util_IO} will not be considered in program builds.
13129 However, the package spec will still be found in the project
13130 @code{Utilities}.
13131
13132 A child project can have only one parent, except when it is qualified as
13133 abstract. But it may import any number of other projects.
13134
13135 A project is not allowed to import directly or indirectly at the same time a
13136 child project and any of its ancestors.
13137
13138 @c *******************************
13139 @c * Project Hierarchy Extension *
13140 @c *******************************
13141
13142 @node Project Hierarchy Extension
13143 @section Project Hierarchy Extension
13144
13145 @noindent
13146 When extending a large system spanning multiple projects, it is often
13147 inconvenient to extend every project in the hierarchy that is impacted by a
13148 small change introduced. In such cases, it is possible to create a virtual
13149 extension of entire hierarchy using @code{extends all} relationship.
13150
13151 When the project is extended using @code{extends all} inheritance, all projects
13152 that are imported by it, both directly and indirectly, are considered virtually
13153 extended. That is, the Project Manager creates "virtual projects"
13154 that extend every project in the hierarchy; all these virtual projects have
13155 no sources of their own and have as object directory the object directory of
13156 the root of "extending all" project.
13157
13158 It is possible to explicitly extend one or more projects in the hierarchy
13159 in order to modify the sources. These extending projects must be imported by
13160 the "extending all" project, which will replace the corresponding virtual
13161 projects with the explicit ones.
13162
13163 When building such a project hierarchy extension, the Project Manager will
13164 ensure that both modified sources and sources in virtual extending projects
13165 that depend on them, are recompiled.
13166
13167 By means of example, consider the following hierarchy of projects.
13168
13169 @enumerate
13170 @item
13171 project A, containing package P1
13172 @item
13173 project B importing A and containing package P2 which depends on P1
13174 @item
13175 project C importing B and containing package P3 which depends on P2
13176 @end enumerate
13177
13178 @noindent
13179 We want to modify packages P1 and P3.
13180
13181 This project hierarchy will need to be extended as follows:
13182
13183 @enumerate
13184 @item
13185 Create project A1 that extends A, placing modified P1 there:
13186
13187 @smallexample @c 0projectfile
13188 project A1 extends "(@dots{})/A" is
13189 end A1;
13190 @end smallexample
13191
13192 @item
13193 Create project C1 that "extends all" C and imports A1, placing modified
13194 P3 there:
13195
13196 @smallexample @c 0projectfile
13197 with "(@dots{})/A1";
13198 project C1 extends all "(@dots{})/C" is
13199 end C1;
13200 @end smallexample
13201 @end enumerate
13202
13203 When you build project C1, your entire modified project space will be
13204 recompiled, including the virtual project B1 that has been impacted by the
13205 "extending all" inheritance of project C.
13206
13207 Note that if a Library Project in the hierarchy is virtually extended,
13208 the virtual project that extends the Library Project is not a Library Project.
13209
13210 @c ****************************************
13211 @c * External References in Project Files *
13212 @c ****************************************
13213
13214 @node External References in Project Files
13215 @section External References in Project Files
13216
13217 @noindent
13218 A project file may contain references to external variables; such references
13219 are called @emph{external references}.
13220
13221 An external variable is either defined as part of the environment (an
13222 environment variable in Unix, for example) or else specified on the command
13223 line via the @option{^-X^/EXTERNAL_REFERENCE=^@emph{vbl}=@emph{value}} switch.
13224 If both, then the command line value is used.
13225
13226 The value of an external reference is obtained by means of the built-in
13227 function @code{external}, which returns a string value.
13228 This function has two forms:
13229 @itemize @bullet
13230 @item @code{external (external_variable_name)}
13231 @item @code{external (external_variable_name, default_value)}
13232 @end itemize
13233
13234 @noindent
13235 Each parameter must be a string literal. For example:
13236
13237 @smallexample @c projectfile
13238 external ("USER")
13239 external ("OS", "GNU/Linux")
13240 @end smallexample
13241
13242 @noindent
13243 In the form with one parameter, the function returns the value of
13244 the external variable given as parameter. If this name is not present in the
13245 environment, the function returns an empty string.
13246
13247 In the form with two string parameters, the second argument is
13248 the value returned when the variable given as the first argument is not
13249 present in the environment. In the example above, if @code{"OS"} is not
13250 the name of ^an environment variable^a logical name^ and is not passed on
13251 the command line, then the returned value is @code{"GNU/Linux"}.
13252
13253 An external reference may be part of a string expression or of a string
13254 list expression, and can therefore appear in a variable declaration or
13255 an attribute declaration.
13256
13257 @smallexample @c projectfile
13258 @group
13259 type Mode_Type is ("Debug", "Release");
13260 Mode : Mode_Type := external ("MODE");
13261 case Mode is
13262 when "Debug" =>
13263 @dots{}
13264 @end group
13265 @end smallexample
13266
13267 @c *****************************
13268 @c * Packages in Project Files *
13269 @c *****************************
13270
13271 @node Packages in Project Files
13272 @section Packages in Project Files
13273
13274 @noindent
13275 A @emph{package} defines the settings for project-aware tools within a
13276 project.
13277 For each such tool one can declare a package; the names for these
13278 packages are preset (@pxref{Packages}).
13279 A package may contain variable declarations, attribute declarations, and case
13280 constructions.
13281
13282 @smallexample @c projectfile
13283 @group
13284 project Proj is
13285 package Builder is -- used by gnatmake
13286 for ^Default_Switches^Default_Switches^ ("Ada")
13287 use ("^-v^-v^",
13288 "^-g^-g^");
13289 end Builder;
13290 end Proj;
13291 @end group
13292 @end smallexample
13293
13294 @noindent
13295 The syntax of package declarations mimics that of package in Ada.
13296
13297 Most of the packages have an attribute
13298 @code{^Default_Switches^Default_Switches^}.
13299 This attribute is an associative array, and its value is a string list.
13300 The index of the associative array is the name of a programming language (case
13301 insensitive). This attribute indicates the ^switch^switch^
13302 or ^switches^switches^ to be used
13303 with the corresponding tool.
13304
13305 Some packages also have another attribute, @code{^Switches^Switches^},
13306 an associative array whose value is a string list.
13307 The index is the name of a source file.
13308 This attribute indicates the ^switch^switch^
13309 or ^switches^switches^ to be used by the corresponding
13310 tool when dealing with this specific file.
13311
13312 Further information on these ^switch^switch^-related attributes is found in
13313 @ref{^Switches^Switches^ and Project Files}.
13314
13315 A package may be declared as a @emph{renaming} of another package; e.g., from
13316 the project file for an imported project.
13317
13318 @smallexample @c projectfile
13319 @group
13320 with "/global/apex.gpr";
13321 project Example is
13322 package Naming renames Apex.Naming;
13323 @dots{}
13324 end Example;
13325 @end group
13326 @end smallexample
13327
13328 @noindent
13329 Packages that are renamed in other project files often come from project files
13330 that have no sources: they are just used as templates. Any modification in the
13331 template will be reflected automatically in all the project files that rename
13332 a package from the template.
13333
13334 In addition to the tool-oriented packages, you can also declare a package
13335 named @code{Naming} to establish specialized source file naming conventions
13336 (@pxref{Naming Schemes}).
13337
13338 @c ************************************
13339 @c * Variables from Imported Projects *
13340 @c ************************************
13341
13342 @node Variables from Imported Projects
13343 @section Variables from Imported Projects
13344
13345 @noindent
13346 An attribute or variable defined in an imported or parent project can
13347 be used in expressions in the importing / extending project.
13348 Such an attribute or variable is denoted by an expanded name whose prefix
13349 is either the name of the project or the expanded name of a package within
13350 a project.
13351
13352 @smallexample @c projectfile
13353 @group
13354 with "imported";
13355 project Main extends "base" is
13356 Var1 := Imported.Var;
13357 Var2 := Base.Var & ".new";
13358 @end group
13359
13360 @group
13361 package Builder is
13362 for ^Default_Switches^Default_Switches^ ("Ada")
13363 use Imported.Builder'Ada_^Switches^Switches^ &
13364 "^-gnatg^-gnatg^" &
13365 "^-v^-v^";
13366 end Builder;
13367 @end group
13368
13369 @group
13370 package Compiler is
13371 for ^Default_Switches^Default_Switches^ ("Ada")
13372 use Base.Compiler'Ada_^Switches^Switches^;
13373 end Compiler;
13374 end Main;
13375 @end group
13376 @end smallexample
13377
13378 @noindent
13379 In this example:
13380
13381 @itemize @bullet
13382 @item
13383 The value of @code{Var1} is a copy of the variable @code{Var} defined
13384 in the project file @file{"imported.gpr"}
13385 @item
13386 the value of @code{Var2} is a copy of the value of variable @code{Var}
13387 defined in the project file @file{base.gpr}, concatenated with @code{".new"}
13388 @item
13389 attribute @code{^Default_Switches^Default_Switches^ ("Ada")} in package
13390 @code{Builder} is a string list that includes in its value a copy of the value
13391 of @code{Ada_^Switches^Switches^} defined in the @code{Builder} package
13392 in project file @file{imported.gpr} plus two new elements:
13393 @option{"^-gnatg^-gnatg^"}
13394 and @option{"^-v^-v^"};
13395 @item
13396 attribute @code{^Default_Switches^Default_Switches^ ("Ada")} in package
13397 @code{Compiler} is a copy of the variable @code{Ada_^Switches^Switches^}
13398 defined in the @code{Compiler} package in project file @file{base.gpr},
13399 the project being extended.
13400 @end itemize
13401
13402 @c ******************
13403 @c * Naming Schemes *
13404 @c ******************
13405
13406 @node Naming Schemes
13407 @section Naming Schemes
13408
13409 @noindent
13410 Sometimes an Ada software system is ported from a foreign compilation
13411 environment to GNAT, and the file names do not use the default GNAT
13412 conventions. Instead of changing all the file names (which for a variety
13413 of reasons might not be possible), you can define the relevant file
13414 naming scheme in the @code{Naming} package in your project file.
13415
13416 @noindent
13417 Note that the use of pragmas described in
13418 @ref{Alternative File Naming Schemes} by mean of a configuration
13419 pragmas file is not supported when using project files. You must use
13420 the features described in this paragraph. You can however use specify
13421 other configuration pragmas (@pxref{Specifying Configuration Pragmas}).
13422
13423 @ifclear vms
13424 For example, the following
13425 package models the Apex file naming rules:
13426
13427 @smallexample @c projectfile
13428 @group
13429 package Naming is
13430 for Casing use "lowercase";
13431 for Dot_Replacement use ".";
13432 for Spec_Suffix ("Ada") use ".1.ada";
13433 for Body_Suffix ("Ada") use ".2.ada";
13434 end Naming;
13435 @end group
13436 @end smallexample
13437 @end ifclear
13438
13439 @ifset vms
13440 For example, the following package models the HP Ada file naming rules:
13441
13442 @smallexample @c projectfile
13443 @group
13444 package Naming is
13445 for Casing use "lowercase";
13446 for Dot_Replacement use "__";
13447 for Spec_Suffix ("Ada") use "_.^ada^ada^";
13448 for Body_Suffix ("Ada") use ".^ada^ada^";
13449 end Naming;
13450 @end group
13451 @end smallexample
13452
13453 @noindent
13454 (Note that @code{Casing} is @code{"lowercase"} because GNAT gets the file
13455 names in lower case)
13456 @end ifset
13457
13458 @noindent
13459 You can define the following attributes in package @code{Naming}:
13460
13461 @table @code
13462
13463 @item @code{Casing}
13464 This must be a string with one of the three values @code{"lowercase"},
13465 @code{"uppercase"} or @code{"mixedcase"}; these strings are case insensitive.
13466
13467 @noindent
13468 If @code{Casing} is not specified, then the default is @code{"lowercase"}.
13469
13470 @item @code{Dot_Replacement}
13471 This must be a string whose value satisfies the following conditions:
13472
13473 @itemize @bullet
13474 @item It must not be empty
13475 @item It cannot start or end with an alphanumeric character
13476 @item It cannot be a single underscore
13477 @item It cannot start with an underscore followed by an alphanumeric
13478 @item It cannot contain a dot @code{'.'} except if the entire string
13479 is @code{"."}
13480 @end itemize
13481
13482 @noindent
13483 If @code{Dot_Replacement} is not specified, then the default is @code{"-"}.
13484
13485 @item @code{Spec_Suffix}
13486 This is an associative array (indexed by the programming language name, case
13487 insensitive) whose value is a string that must satisfy the following
13488 conditions:
13489
13490 @itemize @bullet
13491 @item It must not be empty
13492 @item It must include at least one dot
13493 @end itemize
13494 @noindent
13495 If @code{Spec_Suffix ("Ada")} is not specified, then the default is
13496 @code{"^.ads^.ADS^"}.
13497
13498 @item @code{Body_Suffix}
13499 This is an associative array (indexed by the programming language name, case
13500 insensitive) whose value is a string that must satisfy the following
13501 conditions:
13502
13503 @itemize @bullet
13504 @item It must not be empty
13505 @item It must include at least one dot
13506 @item It cannot be the same as @code{Spec_Suffix ("Ada")}
13507 @end itemize
13508 @noindent
13509 If @code{Body_Suffix ("Ada")} and @code{Spec_Suffix ("Ada")} end with the
13510 same string, then a file name that ends with the longest of these two suffixes
13511 will be a body if the longest suffix is @code{Body_Suffix ("Ada")} or a spec
13512 if the longest suffix is @code{Spec_Suffix ("Ada")}.
13513
13514 If @code{Body_Suffix ("Ada")} is not specified, then the default is
13515 @code{"^.adb^.ADB^"}.
13516
13517 @item @code{Separate_Suffix}
13518 This must be a string whose value satisfies the same conditions as
13519 @code{Body_Suffix}. The same "longest suffix" rules apply.
13520
13521 @noindent
13522 If @code{Separate_Suffix ("Ada")} is not specified, then it defaults to same
13523 value as @code{Body_Suffix ("Ada")}.
13524
13525 @item @code{Spec}
13526 @noindent
13527 You can use the associative array attribute @code{Spec} to define
13528 the source file name for an individual Ada compilation unit's spec. The array
13529 index must be a string literal that identifies the Ada unit (case insensitive).
13530 The value of this attribute must be a string that identifies the file that
13531 contains this unit's spec (case sensitive or insensitive depending on the
13532 operating system).
13533
13534 @smallexample @c projectfile
13535 for Spec ("MyPack.MyChild") use "mypack.mychild.spec";
13536 @end smallexample
13537
13538 @item @code{Body}
13539
13540 You can use the associative array attribute @code{Body} to
13541 define the source file name for an individual Ada compilation unit's body
13542 (possibly a subunit). The array index must be a string literal that identifies
13543 the Ada unit (case insensitive). The value of this attribute must be a string
13544 that identifies the file that contains this unit's body or subunit (case
13545 sensitive or insensitive depending on the operating system).
13546
13547 @smallexample @c projectfile
13548 for Body ("MyPack.MyChild") use "mypack.mychild.body";
13549 @end smallexample
13550 @end table
13551
13552 @c ********************
13553 @c * Library Projects *
13554 @c ********************
13555
13556 @node Library Projects
13557 @section Library Projects
13558
13559 @noindent
13560 @emph{Library projects} are projects whose object code is placed in a library.
13561 (Note that this facility is not yet supported on all platforms)
13562
13563 To create a library project, you need to define in its project file
13564 two project-level attributes: @code{Library_Name} and @code{Library_Dir}.
13565 Additionally, you may define other library-related attributes such as
13566 @code{Library_Kind}, @code{Library_Version}, @code{Library_Interface},
13567 @code{Library_Auto_Init}, @code{Library_Options} and @code{Library_GCC}.
13568
13569 The @code{Library_Name} attribute has a string value. There is no restriction
13570 on the name of a library. It is the responsibility of the developer to
13571 choose a name that will be accepted by the platform. It is recommended to
13572 choose names that could be Ada identifiers; such names are almost guaranteed
13573 to be acceptable on all platforms.
13574
13575 The @code{Library_Dir} attribute has a string value that designates the path
13576 (absolute or relative) of the directory where the library will reside.
13577 It must designate an existing directory, and this directory must be writable,
13578 different from the project's object directory and from any source directory
13579 in the project tree.
13580
13581 If both @code{Library_Name} and @code{Library_Dir} are specified and
13582 are legal, then the project file defines a library project. The optional
13583 library-related attributes are checked only for such project files.
13584
13585 The @code{Library_Kind} attribute has a string value that must be one of the
13586 following (case insensitive): @code{"static"}, @code{"dynamic"} or
13587 @code{"relocatable"} (which is a synonym for @code{"dynamic"}). If this
13588 attribute is not specified, the library is a static library, that is
13589 an archive of object files that can be potentially linked into a
13590 static executable. Otherwise, the library may be dynamic or
13591 relocatable, that is a library that is loaded only at the start of execution.
13592
13593 If you need to build both a static and a dynamic library, you should use two
13594 different object directories, since in some cases some extra code needs to
13595 be generated for the latter. For such cases, it is recommended to either use
13596 two different project files, or a single one which uses external variables
13597 to indicate what kind of library should be build.
13598
13599 The @code{Library_ALI_Dir} attribute may be specified to indicate the
13600 directory where the ALI files of the library will be copied. When it is
13601 not specified, the ALI files are copied to the directory specified in
13602 attribute @code{Library_Dir}. The directory specified by @code{Library_ALI_Dir}
13603 must be writable and different from the project's object directory and from
13604 any source directory in the project tree.
13605
13606 The @code{Library_Version} attribute has a string value whose interpretation
13607 is platform dependent. It has no effect on VMS and Windows. On Unix, it is
13608 used only for dynamic/relocatable libraries as the internal name of the
13609 library (the @code{"soname"}). If the library file name (built from the
13610 @code{Library_Name}) is different from the @code{Library_Version}, then the
13611 library file will be a symbolic link to the actual file whose name will be
13612 @code{Library_Version}.
13613
13614 Example (on Unix):
13615
13616 @smallexample @c projectfile
13617 @group
13618 project Plib is
13619
13620 Version := "1";
13621
13622 for Library_Dir use "lib_dir";
13623 for Library_Name use "dummy";
13624 for Library_Kind use "relocatable";
13625 for Library_Version use "libdummy.so." & Version;
13626
13627 end Plib;
13628 @end group
13629 @end smallexample
13630
13631 @noindent
13632 Directory @file{lib_dir} will contain the internal library file whose name
13633 will be @file{libdummy.so.1}, and @file{libdummy.so} will be a symbolic link to
13634 @file{libdummy.so.1}.
13635
13636 When @command{gnatmake} detects that a project file
13637 is a library project file, it will check all immediate sources of the project
13638 and rebuild the library if any of the sources have been recompiled.
13639
13640 Standard project files can import library project files. In such cases,
13641 the libraries will only be rebuilt if some of its sources are recompiled
13642 because they are in the closure of some other source in an importing project.
13643 Sources of the library project files that are not in such a closure will
13644 not be checked, unless the full library is checked, because one of its sources
13645 needs to be recompiled.
13646
13647 For instance, assume the project file @code{A} imports the library project file
13648 @code{L}. The immediate sources of A are @file{a1.adb}, @file{a2.ads} and
13649 @file{a2.adb}. The immediate sources of L are @file{l1.ads}, @file{l1.adb},
13650 @file{l2.ads}, @file{l2.adb}.
13651
13652 If @file{l1.adb} has been modified, then the library associated with @code{L}
13653 will be rebuilt when compiling all the immediate sources of @code{A} only
13654 if @file{a1.ads}, @file{a2.ads} or @file{a2.adb} includes a statement
13655 @code{"with L1;"}.
13656
13657 To be sure that all the sources in the library associated with @code{L} are
13658 up to date, and that all the sources of project @code{A} are also up to date,
13659 the following two commands needs to be used:
13660
13661 @smallexample
13662 gnatmake -Pl.gpr
13663 gnatmake -Pa.gpr
13664 @end smallexample
13665
13666 When a library is built or rebuilt, an attempt is made first to delete all
13667 files in the library directory.
13668 All @file{ALI} files will also be copied from the object directory to the
13669 library directory. To build executables, @command{gnatmake} will use the
13670 library rather than the individual object files.
13671
13672 @ifclear vms
13673 It is also possible to create library project files for third-party libraries
13674 that are precompiled and cannot be compiled locally thanks to the
13675 @code{externally_built} attribute. (See @ref{Installing a library}).
13676 @end ifclear
13677
13678 @c *******************************
13679 @c * Stand-alone Library Projects *
13680 @c *******************************
13681
13682 @node Stand-alone Library Projects
13683 @section Stand-alone Library Projects
13684
13685 @noindent
13686 A Stand-alone Library is a library that contains the necessary code to
13687 elaborate the Ada units that are included in the library. A Stand-alone
13688 Library is suitable to be used in an executable when the main is not
13689 in Ada. However, Stand-alone Libraries may also be used with an Ada main
13690 subprogram.
13691
13692 A Stand-alone Library Project is a Library Project where the library is
13693 a Stand-alone Library.
13694
13695 To be a Stand-alone Library Project, in addition to the two attributes
13696 that make a project a Library Project (@code{Library_Name} and
13697 @code{Library_Dir}, see @ref{Library Projects}), the attribute
13698 @code{Library_Interface} must be defined.
13699
13700 @smallexample @c projectfile
13701 @group
13702 for Library_Dir use "lib_dir";
13703 for Library_Name use "dummy";
13704 for Library_Interface use ("int1", "int1.child");
13705 @end group
13706 @end smallexample
13707
13708 Attribute @code{Library_Interface} has a nonempty string list value,
13709 each string in the list designating a unit contained in an immediate source
13710 of the project file.
13711
13712 When a Stand-alone Library is built, first the binder is invoked to build
13713 a package whose name depends on the library name
13714 (^b~dummy.ads/b^B$DUMMY.ADS/B^ in the example above).
13715 This binder-generated package includes initialization and
13716 finalization procedures whose
13717 names depend on the library name (dummyinit and dummyfinal in the example
13718 above). The object corresponding to this package is included in the library.
13719
13720 A dynamic or relocatable Stand-alone Library is automatically initialized
13721 if automatic initialization of Stand-alone Libraries is supported on the
13722 platform and if attribute @code{Library_Auto_Init} is not specified or
13723 is specified with the value "true". A static Stand-alone Library is never
13724 automatically initialized.
13725
13726 Single string attribute @code{Library_Auto_Init} may be specified with only
13727 two possible values: "false" or "true" (case-insensitive). Specifying
13728 "false" for attribute @code{Library_Auto_Init} will prevent automatic
13729 initialization of dynamic or relocatable libraries.
13730
13731 When a non-automatically initialized Stand-alone Library is used
13732 in an executable, its initialization procedure must be called before
13733 any service of the library is used.
13734 When the main subprogram is in Ada, it may mean that the initialization
13735 procedure has to be called during elaboration of another package.
13736
13737 For a Stand-Alone Library, only the @file{ALI} files of the Interface Units
13738 (those that are listed in attribute @code{Library_Interface}) are copied to
13739 the Library Directory. As a consequence, only the Interface Units may be
13740 imported from Ada units outside of the library. If other units are imported,
13741 the binding phase will fail.
13742
13743 When a Stand-Alone Library is bound, the switches that are specified in
13744 the attribute @code{Default_Switches ("Ada")} in package @code{Binder} are
13745 used in the call to @command{gnatbind}.
13746
13747 The string list attribute @code{Library_Options} may be used to specified
13748 additional switches to the call to @command{gcc} to link the library.
13749
13750 The attribute @code{Library_Src_Dir}, may be specified for a
13751 Stand-Alone Library. @code{Library_Src_Dir} is a simple attribute that has a
13752 single string value. Its value must be the path (absolute or relative to the
13753 project directory) of an existing directory. This directory cannot be the
13754 object directory or one of the source directories, but it can be the same as
13755 the library directory. The sources of the Interface
13756 Units of the library, necessary to an Ada client of the library, will be
13757 copied to the designated directory, called Interface Copy directory.
13758 These sources includes the specs of the Interface Units, but they may also
13759 include bodies and subunits, when pragmas @code{Inline} or @code{Inline_Always}
13760 are used, or when there is a generic units in the spec. Before the sources
13761 are copied to the Interface Copy directory, an attempt is made to delete all
13762 files in the Interface Copy directory.
13763
13764 @c *************************************
13765 @c * Switches Related to Project Files *
13766 @c *************************************
13767 @node Switches Related to Project Files
13768 @section Switches Related to Project Files
13769
13770 @noindent
13771 The following switches are used by GNAT tools that support project files:
13772
13773 @table @option
13774
13775 @item ^-P^/PROJECT_FILE=^@var{project}
13776 @cindex @option{^-P^/PROJECT_FILE^} (any project-aware tool)
13777 Indicates the name of a project file. This project file will be parsed with
13778 the verbosity indicated by @option{^-vP^MESSAGE_PROJECT_FILES=^@emph{x}},
13779 if any, and using the external references indicated
13780 by @option{^-X^/EXTERNAL_REFERENCE^} switches, if any.
13781 @ifclear vms
13782 There may zero, one or more spaces between @option{-P} and @var{project}.
13783 @end ifclear
13784
13785 @noindent
13786 There must be only one @option{^-P^/PROJECT_FILE^} switch on the command line.
13787
13788 @noindent
13789 Since the Project Manager parses the project file only after all the switches
13790 on the command line are checked, the order of the switches
13791 @option{^-P^/PROJECT_FILE^},
13792 @option{^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}}
13793 or @option{^-X^/EXTERNAL_REFERENCE^} is not significant.
13794
13795 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
13796 @cindex @option{^-X^/EXTERNAL_REFERENCE^} (any project-aware tool)
13797 Indicates that external variable @var{name} has the value @var{value}.
13798 The Project Manager will use this value for occurrences of
13799 @code{external(name)} when parsing the project file.
13800
13801 @ifclear vms
13802 @noindent
13803 If @var{name} or @var{value} includes a space, then @var{name=value} should be
13804 put between quotes.
13805 @smallexample
13806 -XOS=NT
13807 -X"user=John Doe"
13808 @end smallexample
13809 @end ifclear
13810
13811 @noindent
13812 Several @option{^-X^/EXTERNAL_REFERENCE^} switches can be used simultaneously.
13813 If several @option{^-X^/EXTERNAL_REFERENCE^} switches specify the same
13814 @var{name}, only the last one is used.
13815
13816 @noindent
13817 An external variable specified with a @option{^-X^/EXTERNAL_REFERENCE^} switch
13818 takes precedence over the value of the same name in the environment.
13819
13820 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
13821 @cindex @option{^-vP^/MESSAGES_PROJECT_FILE^} (any project-aware tool)
13822 Indicates the verbosity of the parsing of GNAT project files.
13823
13824 @ifclear vms
13825 @option{-vP0} means Default;
13826 @option{-vP1} means Medium;
13827 @option{-vP2} means High.
13828 @end ifclear
13829
13830 @ifset vms
13831 There are three possible options for this qualifier: DEFAULT, MEDIUM and
13832 HIGH.
13833 @end ifset
13834
13835 @noindent
13836 The default is ^Default^DEFAULT^: no output for syntactically correct
13837 project files.
13838 @noindent
13839 If several @option{^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}} switches are present,
13840 only the last one is used.
13841
13842 @item ^-aP^/ADD_PROJECT_SEARCH_DIR=^<dir>
13843 @cindex @option{^-aP^/ADD_PROJECT_SEARCH_DIR=^} (any project-aware tool)
13844 Add directory <dir> at the beginning of the project search path, in order,
13845 after the current working directory.
13846
13847 @ifclear vms
13848 @item -eL
13849 @cindex @option{-eL} (any project-aware tool)
13850 Follow all symbolic links when processing project files.
13851 @end ifclear
13852
13853 @item ^--subdirs^/SUBDIRS^=<subdir>
13854 @cindex @option{^--subdirs^/SUBDIRS^=} (gnatmake and gnatclean)
13855 This switch is recognized by gnatmake and gnatclean. It indicate that the real
13856 directories (except the source directories) are the subdirectories <subdir>
13857 of the directories specified in the project files. This applies in particular
13858 to object directories, library directories and exec directories. If the
13859 subdirectories do not exist, they are created automatically.
13860
13861 @end table
13862
13863 @c **********************************
13864 @c * Tools Supporting Project Files *
13865 @c **********************************
13866
13867 @node Tools Supporting Project Files
13868 @section Tools Supporting Project Files
13869
13870 @menu
13871 * gnatmake and Project Files::
13872 * The GNAT Driver and Project Files::
13873 @end menu
13874
13875 @node gnatmake and Project Files
13876 @subsection gnatmake and Project Files
13877
13878 @noindent
13879 This section covers several topics related to @command{gnatmake} and
13880 project files: defining ^switches^switches^ for @command{gnatmake}
13881 and for the tools that it invokes; specifying configuration pragmas;
13882 the use of the @code{Main} attribute; building and rebuilding library project
13883 files.
13884
13885 @menu
13886 * ^Switches^Switches^ and Project Files::
13887 * Specifying Configuration Pragmas::
13888 * Project Files and Main Subprograms::
13889 * Library Project Files::
13890 @end menu
13891
13892 @node ^Switches^Switches^ and Project Files
13893 @subsubsection ^Switches^Switches^ and Project Files
13894
13895 @ifset vms
13896 It is not currently possible to specify VMS style qualifiers in the project
13897 files; only Unix style ^switches^switches^ may be specified.
13898 @end ifset
13899
13900 @noindent
13901 For each of the packages @code{Builder}, @code{Compiler}, @code{Binder}, and
13902 @code{Linker}, you can specify a @code{^Default_Switches^Default_Switches^}
13903 attribute, a @code{^Switches^Switches^} attribute, or both;
13904 as their names imply, these ^switch^switch^-related
13905 attributes affect the ^switches^switches^ that are used for each of these GNAT
13906 components when
13907 @command{gnatmake} is invoked. As will be explained below, these
13908 component-specific ^switches^switches^ precede
13909 the ^switches^switches^ provided on the @command{gnatmake} command line.
13910
13911 The @code{^Default_Switches^Default_Switches^} attribute is an associative
13912 array indexed by language name (case insensitive) whose value is a string list.
13913 For example:
13914
13915 @smallexample @c projectfile
13916 @group
13917 package Compiler is
13918 for ^Default_Switches^Default_Switches^ ("Ada")
13919 use ("^-gnaty^-gnaty^",
13920 "^-v^-v^");
13921 end Compiler;
13922 @end group
13923 @end smallexample
13924
13925 @noindent
13926 The @code{^Switches^Switches^} attribute is also an associative array,
13927 indexed by a file name (which may or may not be case sensitive, depending
13928 on the operating system) whose value is a string list. For example:
13929
13930 @smallexample @c projectfile
13931 @group
13932 package Builder is
13933 for ^Switches^Switches^ ("main1.adb")
13934 use ("^-O2^-O2^");
13935 for ^Switches^Switches^ ("main2.adb")
13936 use ("^-g^-g^");
13937 end Builder;
13938 @end group
13939 @end smallexample
13940
13941 @noindent
13942 For the @code{Builder} package, the file names must designate source files
13943 for main subprograms. For the @code{Binder} and @code{Linker} packages, the
13944 file names must designate @file{ALI} or source files for main subprograms.
13945 In each case just the file name without an explicit extension is acceptable.
13946
13947 For each tool used in a program build (@command{gnatmake}, the compiler, the
13948 binder, and the linker), the corresponding package @dfn{contributes} a set of
13949 ^switches^switches^ for each file on which the tool is invoked, based on the
13950 ^switch^switch^-related attributes defined in the package.
13951 In particular, the ^switches^switches^
13952 that each of these packages contributes for a given file @var{f} comprise:
13953
13954 @itemize @bullet
13955 @item
13956 the value of attribute @code{^Switches^Switches^ (@var{f})},
13957 if it is specified in the package for the given file,
13958 @item
13959 otherwise, the value of @code{^Default_Switches^Default_Switches^ ("Ada")},
13960 if it is specified in the package.
13961 @end itemize
13962
13963 @noindent
13964 If neither of these attributes is defined in the package, then the package does
13965 not contribute any ^switches^switches^ for the given file.
13966
13967 When @command{gnatmake} is invoked on a file, the ^switches^switches^ comprise
13968 two sets, in the following order: those contributed for the file
13969 by the @code{Builder} package;
13970 and the switches passed on the command line.
13971
13972 When @command{gnatmake} invokes a tool (compiler, binder, linker) on a file,
13973 the ^switches^switches^ passed to the tool comprise three sets,
13974 in the following order:
13975
13976 @enumerate
13977 @item
13978 the applicable ^switches^switches^ contributed for the file
13979 by the @code{Builder} package in the project file supplied on the command line;
13980
13981 @item
13982 those contributed for the file by the package (in the relevant project file --
13983 see below) corresponding to the tool; and
13984
13985 @item
13986 the applicable switches passed on the command line.
13987 @end enumerate
13988
13989 @noindent
13990 The term @emph{applicable ^switches^switches^} reflects the fact that
13991 @command{gnatmake} ^switches^switches^ may or may not be passed to individual
13992 tools, depending on the individual ^switch^switch^.
13993
13994 @command{gnatmake} may invoke the compiler on source files from different
13995 projects. The Project Manager will use the appropriate project file to
13996 determine the @code{Compiler} package for each source file being compiled.
13997 Likewise for the @code{Binder} and @code{Linker} packages.
13998
13999 As an example, consider the following package in a project file:
14000
14001 @smallexample @c projectfile
14002 @group
14003 project Proj1 is
14004 package Compiler is
14005 for ^Default_Switches^Default_Switches^ ("Ada")
14006 use ("^-g^-g^");
14007 for ^Switches^Switches^ ("a.adb")
14008 use ("^-O1^-O1^");
14009 for ^Switches^Switches^ ("b.adb")
14010 use ("^-O2^-O2^",
14011 "^-gnaty^-gnaty^");
14012 end Compiler;
14013 end Proj1;
14014 @end group
14015 @end smallexample
14016
14017 @noindent
14018 If @command{gnatmake} is invoked with this project file, and it needs to
14019 compile, say, the files @file{a.adb}, @file{b.adb}, and @file{c.adb}, then
14020 @file{a.adb} will be compiled with the ^switch^switch^
14021 @option{^-O1^-O1^},
14022 @file{b.adb} with ^switches^switches^
14023 @option{^-O2^-O2^}
14024 and @option{^-gnaty^-gnaty^},
14025 and @file{c.adb} with @option{^-g^-g^}.
14026
14027 The following example illustrates the ordering of the ^switches^switches^
14028 contributed by different packages:
14029
14030 @smallexample @c projectfile
14031 @group
14032 project Proj2 is
14033 package Builder is
14034 for ^Switches^Switches^ ("main.adb")
14035 use ("^-g^-g^",
14036 "^-O1^-)1^",
14037 "^-f^-f^");
14038 end Builder;
14039 @end group
14040
14041 @group
14042 package Compiler is
14043 for ^Switches^Switches^ ("main.adb")
14044 use ("^-O2^-O2^");
14045 end Compiler;
14046 end Proj2;
14047 @end group
14048 @end smallexample
14049
14050 @noindent
14051 If you issue the command:
14052
14053 @smallexample
14054 gnatmake ^-Pproj2^/PROJECT_FILE=PROJ2^ -O0 main
14055 @end smallexample
14056
14057 @noindent
14058 then the compiler will be invoked on @file{main.adb} with the following
14059 sequence of ^switches^switches^
14060
14061 @smallexample
14062 ^-g -O1 -O2 -O0^-g -O1 -O2 -O0^
14063 @end smallexample
14064
14065 with the last @option{^-O^-O^}
14066 ^switch^switch^ having precedence over the earlier ones;
14067 several other ^switches^switches^
14068 (such as @option{^-c^-c^}) are added implicitly.
14069
14070 The ^switches^switches^
14071 @option{^-g^-g^}
14072 and @option{^-O1^-O1^} are contributed by package
14073 @code{Builder}, @option{^-O2^-O2^} is contributed
14074 by the package @code{Compiler}
14075 and @option{^-O0^-O0^} comes from the command line.
14076
14077 The @option{^-g^-g^}
14078 ^switch^switch^ will also be passed in the invocation of
14079 @command{Gnatlink.}
14080
14081 A final example illustrates switch contributions from packages in different
14082 project files:
14083
14084 @smallexample @c projectfile
14085 @group
14086 project Proj3 is
14087 for Source_Files use ("pack.ads", "pack.adb");
14088 package Compiler is
14089 for ^Default_Switches^Default_Switches^ ("Ada")
14090 use ("^-gnata^-gnata^");
14091 end Compiler;
14092 end Proj3;
14093 @end group
14094
14095 @group
14096 with "Proj3";
14097 project Proj4 is
14098 for Source_Files use ("foo_main.adb", "bar_main.adb");
14099 package Builder is
14100 for ^Switches^Switches^ ("foo_main.adb")
14101 use ("^-s^-s^",
14102 "^-g^-g^");
14103 end Builder;
14104 end Proj4;
14105 @end group
14106
14107 @group
14108 -- Ada source file:
14109 with Pack;
14110 procedure Foo_Main is
14111 @dots{}
14112 end Foo_Main;
14113 @end group
14114 @end smallexample
14115
14116 If the command is
14117 @smallexample
14118 gnatmake ^-PProj4^/PROJECT_FILE=PROJ4^ foo_main.adb -cargs -gnato
14119 @end smallexample
14120
14121 @noindent
14122 then the ^switches^switches^ passed to the compiler for @file{foo_main.adb} are
14123 @option{^-g^-g^} (contributed by the package @code{Proj4.Builder}) and
14124 @option{^-gnato^-gnato^} (passed on the command line).
14125 When the imported package @code{Pack} is compiled, the ^switches^switches^ used
14126 are @option{^-g^-g^} from @code{Proj4.Builder},
14127 @option{^-gnata^-gnata^} (contributed from package @code{Proj3.Compiler},
14128 and @option{^-gnato^-gnato^} from the command line.
14129
14130 @noindent
14131 When using @command{gnatmake} with project files, some ^switches^switches^ or
14132 arguments may be expressed as relative paths. As the working directory where
14133 compilation occurs may change, these relative paths are converted to absolute
14134 paths. For the ^switches^switches^ found in a project file, the relative paths
14135 are relative to the project file directory, for the switches on the command
14136 line, they are relative to the directory where @command{gnatmake} is invoked.
14137 The ^switches^switches^ for which this occurs are:
14138 ^-I^-I^,
14139 ^-A^-A^,
14140 ^-L^-L^,
14141 ^-aO^-aO^,
14142 ^-aL^-aL^,
14143 ^-aI^-aI^, as well as all arguments that are not switches (arguments to
14144 ^switch^switch^
14145 ^-o^-o^, object files specified in package @code{Linker} or after
14146 -largs on the command line). The exception to this rule is the ^switch^switch^
14147 ^--RTS=^--RTS=^ for which a relative path argument is never converted.
14148
14149 @node Specifying Configuration Pragmas
14150 @subsubsection Specifying Configuration Pragmas
14151
14152 When using @command{gnatmake} with project files, if there exists a file
14153 @file{gnat.adc} that contains configuration pragmas, this file will be
14154 ignored.
14155
14156 Configuration pragmas can be defined by means of the following attributes in
14157 project files: @code{Global_Configuration_Pragmas} in package @code{Builder}
14158 and @code{Local_Configuration_Pragmas} in package @code{Compiler}.
14159
14160 Both these attributes are single string attributes. Their values is the path
14161 name of a file containing configuration pragmas. If a path name is relative,
14162 then it is relative to the project directory of the project file where the
14163 attribute is defined.
14164
14165 When compiling a source, the configuration pragmas used are, in order,
14166 those listed in the file designated by attribute
14167 @code{Global_Configuration_Pragmas} in package @code{Builder} of the main
14168 project file, if it is specified, and those listed in the file designated by
14169 attribute @code{Local_Configuration_Pragmas} in package @code{Compiler} of
14170 the project file of the source, if it exists.
14171
14172 @node Project Files and Main Subprograms
14173 @subsubsection Project Files and Main Subprograms
14174
14175 @noindent
14176 When using a project file, you can invoke @command{gnatmake}
14177 with one or several main subprograms, by specifying their source files on the
14178 command line.
14179
14180 @smallexample
14181 gnatmake ^-P^/PROJECT_FILE=^prj main1 main2 main3
14182 @end smallexample
14183
14184 @noindent
14185 Each of these needs to be a source file of the same project, except
14186 when the switch ^-u^/UNIQUE^ is used.
14187
14188 @noindent
14189 When ^-u^/UNIQUE^ is not used, all the mains need to be sources of the
14190 same project, one of the project in the tree rooted at the project specified
14191 on the command line. The package @code{Builder} of this common project, the
14192 "main project" is the one that is considered by @command{gnatmake}.
14193
14194 @noindent
14195 When ^-u^/UNIQUE^ is used, the specified source files may be in projects
14196 imported directly or indirectly by the project specified on the command line.
14197 Note that if such a source file is not part of the project specified on the
14198 command line, the ^switches^switches^ found in package @code{Builder} of the
14199 project specified on the command line, if any, that are transmitted
14200 to the compiler will still be used, not those found in the project file of
14201 the source file.
14202
14203 @noindent
14204 When using a project file, you can also invoke @command{gnatmake} without
14205 explicitly specifying any main, and the effect depends on whether you have
14206 defined the @code{Main} attribute. This attribute has a string list value,
14207 where each element in the list is the name of a source file (the file
14208 extension is optional) that contains a unit that can be a main subprogram.
14209
14210 If the @code{Main} attribute is defined in a project file as a non-empty
14211 string list and the switch @option{^-u^/UNIQUE^} is not used on the command
14212 line, then invoking @command{gnatmake} with this project file but without any
14213 main on the command line is equivalent to invoking @command{gnatmake} with all
14214 the file names in the @code{Main} attribute on the command line.
14215
14216 Example:
14217 @smallexample @c projectfile
14218 @group
14219 project Prj is
14220 for Main use ("main1", "main2", "main3");
14221 end Prj;
14222 @end group
14223 @end smallexample
14224
14225 @noindent
14226 With this project file, @code{"gnatmake ^-Pprj^/PROJECT_FILE=PRJ^"}
14227 is equivalent to
14228 @code{"gnatmake ^-Pprj^/PROJECT_FILE=PRJ^ main1 main2 main3"}.
14229
14230 When the project attribute @code{Main} is not specified, or is specified
14231 as an empty string list, or when the switch @option{-u} is used on the command
14232 line, then invoking @command{gnatmake} with no main on the command line will
14233 result in all immediate sources of the project file being checked, and
14234 potentially recompiled. Depending on the presence of the switch @option{-u},
14235 sources from other project files on which the immediate sources of the main
14236 project file depend are also checked and potentially recompiled. In other
14237 words, the @option{-u} switch is applied to all of the immediate sources of the
14238 main project file.
14239
14240 When no main is specified on the command line and attribute @code{Main} exists
14241 and includes several mains, or when several mains are specified on the
14242 command line, the default ^switches^switches^ in package @code{Builder} will
14243 be used for all mains, even if there are specific ^switches^switches^
14244 specified for one or several mains.
14245
14246 But the ^switches^switches^ from package @code{Binder} or @code{Linker} will be
14247 the specific ^switches^switches^ for each main, if they are specified.
14248
14249 @node Library Project Files
14250 @subsubsection Library Project Files
14251
14252 @noindent
14253 When @command{gnatmake} is invoked with a main project file that is a library
14254 project file, it is not allowed to specify one or more mains on the command
14255 line.
14256
14257 @noindent
14258 When a library project file is specified, switches ^-b^/ACTION=BIND^ and
14259 ^-l^/ACTION=LINK^ have special meanings.
14260
14261 @itemize @bullet
14262 @item ^-b^/ACTION=BIND^ is only allowed for stand-alone libraries. It indicates
14263 to @command{gnatmake} that @command{gnatbind} should be invoked for the
14264 library.
14265
14266 @item ^-l^/ACTION=LINK^ may be used for all library projects. It indicates
14267 to @command{gnatmake} that the binder generated file should be compiled
14268 (in the case of a stand-alone library) and that the library should be built.
14269
14270 @end itemize
14271
14272 @node The GNAT Driver and Project Files
14273 @subsection The GNAT Driver and Project Files
14274
14275 @noindent
14276 A number of GNAT tools, other than @command{^gnatmake^gnatmake^}
14277 can benefit from project files:
14278 @command{^gnatbind^gnatbind^},
14279 @command{^gnatcheck^gnatcheck^}),
14280 @command{^gnatclean^gnatclean^}),
14281 @command{^gnatelim^gnatelim^},
14282 @command{^gnatfind^gnatfind^},
14283 @command{^gnatlink^gnatlink^},
14284 @command{^gnatls^gnatls^},
14285 @command{^gnatmetric^gnatmetric^},
14286 @command{^gnatpp^gnatpp^},
14287 @command{^gnatstub^gnatstub^},
14288 and @command{^gnatxref^gnatxref^}. However, none of these tools can be invoked
14289 directly with a project file switch (@option{^-P^/PROJECT_FILE=^}).
14290 They must be invoked through the @command{gnat} driver.
14291
14292 The @command{gnat} driver is a wrapper that accepts a number of commands and
14293 calls the corresponding tool. It was designed initially for VMS platforms (to
14294 convert VMS qualifiers to Unix-style switches), but it is now available on all
14295 GNAT platforms.
14296
14297 On non-VMS platforms, the @command{gnat} driver accepts the following commands
14298 (case insensitive):
14299
14300 @itemize @bullet
14301 @item
14302 BIND to invoke @command{^gnatbind^gnatbind^}
14303 @item
14304 CHOP to invoke @command{^gnatchop^gnatchop^}
14305 @item
14306 CLEAN to invoke @command{^gnatclean^gnatclean^}
14307 @item
14308 COMP or COMPILE to invoke the compiler
14309 @item
14310 ELIM to invoke @command{^gnatelim^gnatelim^}
14311 @item
14312 FIND to invoke @command{^gnatfind^gnatfind^}
14313 @item
14314 KR or KRUNCH to invoke @command{^gnatkr^gnatkr^}
14315 @item
14316 LINK to invoke @command{^gnatlink^gnatlink^}
14317 @item
14318 LS or LIST to invoke @command{^gnatls^gnatls^}
14319 @item
14320 MAKE to invoke @command{^gnatmake^gnatmake^}
14321 @item
14322 NAME to invoke @command{^gnatname^gnatname^}
14323 @item
14324 PREP or PREPROCESS to invoke @command{^gnatprep^gnatprep^}
14325 @item
14326 PP or PRETTY to invoke @command{^gnatpp^gnatpp^}
14327 @item
14328 METRIC to invoke @command{^gnatmetric^gnatmetric^}
14329 @item
14330 STUB to invoke @command{^gnatstub^gnatstub^}
14331 @item
14332 XREF to invoke @command{^gnatxref^gnatxref^}
14333 @end itemize
14334
14335 @noindent
14336 (note that the compiler is invoked using the command
14337 @command{^gnatmake -f -u -c^gnatmake -f -u -c^}).
14338
14339 @noindent
14340 On non-VMS platforms, between @command{gnat} and the command, two
14341 special switches may be used:
14342
14343 @itemize @bullet
14344 @item
14345 @command{-v} to display the invocation of the tool.
14346 @item
14347 @command{-dn} to prevent the @command{gnat} driver from removing
14348 the temporary files it has created. These temporary files are
14349 configuration files and temporary file list files.
14350 @end itemize
14351
14352 @noindent
14353 The command may be followed by switches and arguments for the invoked
14354 tool.
14355
14356 @smallexample
14357 gnat bind -C main.ali
14358 gnat ls -a main
14359 gnat chop foo.txt
14360 @end smallexample
14361
14362 @noindent
14363 Switches may also be put in text files, one switch per line, and the text
14364 files may be specified with their path name preceded by '@@'.
14365
14366 @smallexample
14367 gnat bind @@args.txt main.ali
14368 @end smallexample
14369
14370 @noindent
14371 In addition, for commands BIND, COMP or COMPILE, FIND, ELIM, LS or LIST, LINK,
14372 METRIC, PP or PRETTY, STUB and XREF, the project file related switches
14373 (@option{^-P^/PROJECT_FILE^},
14374 @option{^-X^/EXTERNAL_REFERENCE^} and
14375 @option{^-vP^/MESSAGES_PROJECT_FILE=^x}) may be used in addition to
14376 the switches of the invoking tool.
14377
14378 @noindent
14379 When GNAT PP or GNAT PRETTY is used with a project file, but with no source
14380 specified on the command line, it invokes @command{^gnatpp^gnatpp^} with all
14381 the immediate sources of the specified project file.
14382
14383 @noindent
14384 When GNAT METRIC is used with a project file, but with no source
14385 specified on the command line, it invokes @command{^gnatmetric^gnatmetric^}
14386 with all the immediate sources of the specified project file and with
14387 @option{^-d^/DIRECTORY^} with the parameter pointing to the object directory
14388 of the project.
14389
14390 @noindent
14391 In addition, when GNAT PP, GNAT PRETTY or GNAT METRIC is used with
14392 a project file, no source is specified on the command line and
14393 switch ^-U^/ALL_PROJECTS^ is specified on the command line, then
14394 the underlying tool (^gnatpp^gnatpp^ or
14395 ^gnatmetric^gnatmetric^) is invoked for all sources of all projects,
14396 not only for the immediate sources of the main project.
14397 @ifclear vms
14398 (-U stands for Universal or Union of the project files of the project tree)
14399 @end ifclear
14400
14401 @noindent
14402 For each of the following commands, there is optionally a corresponding
14403 package in the main project.
14404
14405 @itemize @bullet
14406 @item
14407 package @code{Binder} for command BIND (invoking @code{^gnatbind^gnatbind^})
14408
14409 @item
14410 package @code{Check} for command CHECK (invoking
14411 @code{^gnatcheck^gnatcheck^})
14412
14413 @item
14414 package @code{Compiler} for command COMP or COMPILE (invoking the compiler)
14415
14416 @item
14417 package @code{Cross_Reference} for command XREF (invoking
14418 @code{^gnatxref^gnatxref^})
14419
14420 @item
14421 package @code{Eliminate} for command ELIM (invoking
14422 @code{^gnatelim^gnatelim^})
14423
14424 @item
14425 package @code{Finder} for command FIND (invoking @code{^gnatfind^gnatfind^})
14426
14427 @item
14428 package @code{Gnatls} for command LS or LIST (invoking @code{^gnatls^gnatls^})
14429
14430 @item
14431 package @code{Gnatstub} for command STUB
14432 (invoking @code{^gnatstub^gnatstub^})
14433
14434 @item
14435 package @code{Linker} for command LINK (invoking @code{^gnatlink^gnatlink^})
14436
14437 @item
14438 package @code{Metrics} for command METRIC
14439 (invoking @code{^gnatmetric^gnatmetric^})
14440
14441 @item
14442 package @code{Pretty_Printer} for command PP or PRETTY
14443 (invoking @code{^gnatpp^gnatpp^})
14444
14445 @end itemize
14446
14447 @noindent
14448 Package @code{Gnatls} has a unique attribute @code{^Switches^Switches^},
14449 a simple variable with a string list value. It contains ^switches^switches^
14450 for the invocation of @code{^gnatls^gnatls^}.
14451
14452 @smallexample @c projectfile
14453 @group
14454 project Proj1 is
14455 package gnatls is
14456 for ^Switches^Switches^
14457 use ("^-a^-a^",
14458 "^-v^-v^");
14459 end gnatls;
14460 end Proj1;
14461 @end group
14462 @end smallexample
14463
14464 @noindent
14465 All other packages have two attribute @code{^Switches^Switches^} and
14466 @code{^Default_Switches^Default_Switches^}.
14467
14468 @noindent
14469 @code{^Switches^Switches^} is an associative array attribute, indexed by the
14470 source file name, that has a string list value: the ^switches^switches^ to be
14471 used when the tool corresponding to the package is invoked for the specific
14472 source file.
14473
14474 @noindent
14475 @code{^Default_Switches^Default_Switches^} is an associative array attribute,
14476 indexed by the programming language that has a string list value.
14477 @code{^Default_Switches^Default_Switches^ ("Ada")} contains the
14478 ^switches^switches^ for the invocation of the tool corresponding
14479 to the package, except if a specific @code{^Switches^Switches^} attribute
14480 is specified for the source file.
14481
14482 @smallexample @c projectfile
14483 @group
14484 project Proj is
14485
14486 for Source_Dirs use ("./**");
14487
14488 package gnatls is
14489 for ^Switches^Switches^ use
14490 ("^-a^-a^",
14491 "^-v^-v^");
14492 end gnatls;
14493 @end group
14494 @group
14495
14496 package Compiler is
14497 for ^Default_Switches^Default_Switches^ ("Ada")
14498 use ("^-gnatv^-gnatv^",
14499 "^-gnatwa^-gnatwa^");
14500 end Binder;
14501 @end group
14502 @group
14503
14504 package Binder is
14505 for ^Default_Switches^Default_Switches^ ("Ada")
14506 use ("^-C^-C^",
14507 "^-e^-e^");
14508 end Binder;
14509 @end group
14510 @group
14511
14512 package Linker is
14513 for ^Default_Switches^Default_Switches^ ("Ada")
14514 use ("^-C^-C^");
14515 for ^Switches^Switches^ ("main.adb")
14516 use ("^-C^-C^",
14517 "^-v^-v^",
14518 "^-v^-v^");
14519 end Linker;
14520 @end group
14521 @group
14522
14523 package Finder is
14524 for ^Default_Switches^Default_Switches^ ("Ada")
14525 use ("^-a^-a^",
14526 "^-f^-f^");
14527 end Finder;
14528 @end group
14529 @group
14530
14531 package Cross_Reference is
14532 for ^Default_Switches^Default_Switches^ ("Ada")
14533 use ("^-a^-a^",
14534 "^-f^-f^",
14535 "^-d^-d^",
14536 "^-u^-u^");
14537 end Cross_Reference;
14538 end Proj;
14539 @end group
14540 @end smallexample
14541
14542 @noindent
14543 With the above project file, commands such as
14544
14545 @smallexample
14546 ^gnat comp -Pproj main^GNAT COMP /PROJECT_FILE=PROJ MAIN^
14547 ^gnat ls -Pproj main^GNAT LIST /PROJECT_FILE=PROJ MAIN^
14548 ^gnat xref -Pproj main^GNAT XREF /PROJECT_FILE=PROJ MAIN^
14549 ^gnat bind -Pproj main.ali^GNAT BIND /PROJECT_FILE=PROJ MAIN.ALI^
14550 ^gnat link -Pproj main.ali^GNAT LINK /PROJECT_FILE=PROJ MAIN.ALI^
14551 @end smallexample
14552
14553 @noindent
14554 will set up the environment properly and invoke the tool with the switches
14555 found in the package corresponding to the tool:
14556 @code{^Default_Switches^Default_Switches^ ("Ada")} for all tools,
14557 except @code{^Switches^Switches^ ("main.adb")}
14558 for @code{^gnatlink^gnatlink^}.
14559 It is also possible to invoke some of the tools,
14560 @code{^gnatcheck^gnatcheck^}),
14561 @code{^gnatmetric^gnatmetric^}),
14562 and @code{^gnatpp^gnatpp^})
14563 on a set of project units thanks to the combination of the switches
14564 @option{-P}, @option{-U} and possibly the main unit when one is interested
14565 in its closure. For instance,
14566 @smallexample
14567 gnat metric -Pproj
14568 @end smallexample
14569 will compute the metrics for all the immediate units of project
14570 @code{proj}.
14571 @smallexample
14572 gnat metric -Pproj -U
14573 @end smallexample
14574 will compute the metrics for all the units of the closure of projects
14575 rooted at @code{proj}.
14576 @smallexample
14577 gnat metric -Pproj -U main_unit
14578 @end smallexample
14579 will compute the metrics for the closure of units rooted at
14580 @code{main_unit}. This last possibility relies implicitly
14581 on @command{gnatbind}'s option @option{-R}.
14582
14583 @c **********************
14584 @node An Extended Example
14585 @section An Extended Example
14586
14587 @noindent
14588 Suppose that we have two programs, @var{prog1} and @var{prog2},
14589 whose sources are in corresponding directories. We would like
14590 to build them with a single @command{gnatmake} command, and we want to place
14591 their object files into @file{build} subdirectories of the source directories.
14592 Furthermore, we want to have to have two separate subdirectories
14593 in @file{build} -- @file{release} and @file{debug} -- which will contain
14594 the object files compiled with different set of compilation flags.
14595
14596 In other words, we have the following structure:
14597
14598 @smallexample
14599 @group
14600 main
14601 |- prog1
14602 | |- build
14603 | | debug
14604 | | release
14605 |- prog2
14606 |- build
14607 | debug
14608 | release
14609 @end group
14610 @end smallexample
14611
14612 @noindent
14613 Here are the project files that we must place in a directory @file{main}
14614 to maintain this structure:
14615
14616 @enumerate
14617
14618 @item We create a @code{Common} project with a package @code{Compiler} that
14619 specifies the compilation ^switches^switches^:
14620
14621 @smallexample
14622 File "common.gpr":
14623 @group
14624 @b{project} Common @b{is}
14625
14626 @b{for} Source_Dirs @b{use} (); -- No source files
14627 @end group
14628
14629 @group
14630 @b{type} Build_Type @b{is} ("release", "debug");
14631 Build : Build_Type := External ("BUILD", "debug");
14632 @end group
14633 @group
14634 @b{package} Compiler @b{is}
14635 @b{case} Build @b{is}
14636 @b{when} "release" =>
14637 @b{for} ^Default_Switches^Default_Switches^ ("Ada")
14638 @b{use} ("^-O2^-O2^");
14639 @b{when} "debug" =>
14640 @b{for} ^Default_Switches^Default_Switches^ ("Ada")
14641 @b{use} ("^-g^-g^");
14642 @b{end case};
14643 @b{end} Compiler;
14644
14645 @b{end} Common;
14646 @end group
14647 @end smallexample
14648
14649 @item We create separate projects for the two programs:
14650
14651 @smallexample
14652 @group
14653 File "prog1.gpr":
14654
14655 @b{with} "common";
14656 @b{project} Prog1 @b{is}
14657
14658 @b{for} Source_Dirs @b{use} ("prog1");
14659 @b{for} Object_Dir @b{use} "prog1/build/" & Common.Build;
14660
14661 @b{package} Compiler @b{renames} Common.Compiler;
14662
14663 @b{end} Prog1;
14664 @end group
14665 @end smallexample
14666
14667 @smallexample
14668 @group
14669 File "prog2.gpr":
14670
14671 @b{with} "common";
14672 @b{project} Prog2 @b{is}
14673
14674 @b{for} Source_Dirs @b{use} ("prog2");
14675 @b{for} Object_Dir @b{use} "prog2/build/" & Common.Build;
14676
14677 @b{package} Compiler @b{renames} Common.Compiler;
14678
14679 @end group
14680 @b{end} Prog2;
14681 @end smallexample
14682
14683 @item We create a wrapping project @code{Main}:
14684
14685 @smallexample
14686 @group
14687 File "main.gpr":
14688
14689 @b{with} "common";
14690 @b{with} "prog1";
14691 @b{with} "prog2";
14692 @b{project} Main @b{is}
14693
14694 @b{package} Compiler @b{renames} Common.Compiler;
14695
14696 @b{end} Main;
14697 @end group
14698 @end smallexample
14699
14700 @item Finally we need to create a dummy procedure that @code{with}s (either
14701 explicitly or implicitly) all the sources of our two programs.
14702
14703 @end enumerate
14704
14705 @noindent
14706 Now we can build the programs using the command
14707
14708 @smallexample
14709 gnatmake ^-P^/PROJECT_FILE=^main dummy
14710 @end smallexample
14711
14712 @noindent
14713 for the Debug mode, or
14714
14715 @ifclear vms
14716 @smallexample
14717 gnatmake -Pmain -XBUILD=release
14718 @end smallexample
14719 @end ifclear
14720
14721 @ifset vms
14722 @smallexample
14723 GNAT MAKE /PROJECT_FILE=main /EXTERNAL_REFERENCE=BUILD=release
14724 @end smallexample
14725 @end ifset
14726
14727 @noindent
14728 for the Release mode.
14729
14730 @c ********************************
14731 @c * Project File Complete Syntax *
14732 @c ********************************
14733
14734 @node Project File Complete Syntax
14735 @section Project File Complete Syntax
14736
14737 @smallexample
14738 project ::=
14739 context_clause project_declaration
14740
14741 context_clause ::=
14742 @{with_clause@}
14743
14744 with_clause ::=
14745 @b{with} path_name @{ , path_name @} ;
14746
14747 path_name ::=
14748 string_literal
14749
14750 project_declaration ::=
14751 simple_project_declaration | project_extension
14752
14753 simple_project_declaration ::=
14754 @b{project} <project_>simple_name @b{is}
14755 @{declarative_item@}
14756 @b{end} <project_>simple_name;
14757
14758 project_extension ::=
14759 @b{project} <project_>simple_name @b{extends} path_name @b{is}
14760 @{declarative_item@}
14761 @b{end} <project_>simple_name;
14762
14763 declarative_item ::=
14764 package_declaration |
14765 typed_string_declaration |
14766 other_declarative_item
14767
14768 package_declaration ::=
14769 package_spec | package_renaming
14770
14771 package_spec ::=
14772 @b{package} package_identifier @b{is}
14773 @{simple_declarative_item@}
14774 @b{end} package_identifier ;
14775
14776 package_identifier ::=
14777 @code{Naming} | @code{Builder} | @code{Compiler} | @code{Binder} |
14778 @code{Linker} | @code{Finder} | @code{Cross_Reference} |
14779 @code{^gnatls^gnatls^} | @code{IDE} | @code{Pretty_Printer}
14780
14781 package_renaming ::==
14782 @b{package} package_identifier @b{renames}
14783 <project_>simple_name.package_identifier ;
14784
14785 typed_string_declaration ::=
14786 @b{type} <typed_string_>_simple_name @b{is}
14787 ( string_literal @{, string_literal@} );
14788
14789 other_declarative_item ::=
14790 attribute_declaration |
14791 typed_variable_declaration |
14792 variable_declaration |
14793 case_construction
14794
14795 attribute_declaration ::=
14796 full_associative_array_declaration |
14797 @b{for} attribute_designator @b{use} expression ;
14798
14799 full_associative_array_declaration ::=
14800 @b{for} <associative_array_attribute_>simple_name @b{use}
14801 <project_>simple_name [ . <package_>simple_Name ] ' <attribute_>simple_name ;
14802
14803 attribute_designator ::=
14804 <simple_attribute_>simple_name |
14805 <associative_array_attribute_>simple_name ( string_literal )
14806
14807 typed_variable_declaration ::=
14808 <typed_variable_>simple_name : <typed_string_>name := string_expression ;
14809
14810 variable_declaration ::=
14811 <variable_>simple_name := expression;
14812
14813 expression ::=
14814 term @{& term@}
14815
14816 term ::=
14817 literal_string |
14818 string_list |
14819 <variable_>name |
14820 external_value |
14821 attribute_reference
14822
14823 string_literal ::=
14824 (same as Ada)
14825
14826 string_list ::=
14827 ( <string_>expression @{ , <string_>expression @} )
14828
14829 external_value ::=
14830 @b{external} ( string_literal [, string_literal] )
14831
14832 attribute_reference ::=
14833 attribute_prefix ' <simple_attribute_>simple_name [ ( literal_string ) ]
14834
14835 attribute_prefix ::=
14836 @b{project} |
14837 <project_>simple_name | package_identifier |
14838 <project_>simple_name . package_identifier
14839
14840 case_construction ::=
14841 @b{case} <typed_variable_>name @b{is}
14842 @{case_item@}
14843 @b{end case} ;
14844
14845 case_item ::=
14846 @b{when} discrete_choice_list =>
14847 @{case_construction | attribute_declaration@}
14848
14849 discrete_choice_list ::=
14850 string_literal @{| string_literal@} |
14851 @b{others}
14852
14853 name ::=
14854 simple_name @{. simple_name@}
14855
14856 simple_name ::=
14857 identifier (same as Ada)
14858
14859 @end smallexample
14860
14861 @node The Cross-Referencing Tools gnatxref and gnatfind
14862 @chapter The Cross-Referencing Tools @code{gnatxref} and @code{gnatfind}
14863 @findex gnatxref
14864 @findex gnatfind
14865
14866 @noindent
14867 The compiler generates cross-referencing information (unless
14868 you set the @samp{-gnatx} switch), which are saved in the @file{.ali} files.
14869 This information indicates where in the source each entity is declared and
14870 referenced. Note that entities in package Standard are not included, but
14871 entities in all other predefined units are included in the output.
14872
14873 Before using any of these two tools, you need to compile successfully your
14874 application, so that GNAT gets a chance to generate the cross-referencing
14875 information.
14876
14877 The two tools @code{gnatxref} and @code{gnatfind} take advantage of this
14878 information to provide the user with the capability to easily locate the
14879 declaration and references to an entity. These tools are quite similar,
14880 the difference being that @code{gnatfind} is intended for locating
14881 definitions and/or references to a specified entity or entities, whereas
14882 @code{gnatxref} is oriented to generating a full report of all
14883 cross-references.
14884
14885 To use these tools, you must not compile your application using the
14886 @option{-gnatx} switch on the @command{gnatmake} command line
14887 (@pxref{The GNAT Make Program gnatmake}). Otherwise, cross-referencing
14888 information will not be generated.
14889
14890 Note: to invoke @code{gnatxref} or @code{gnatfind} with a project file,
14891 use the @code{gnat} driver (see @ref{The GNAT Driver and Project Files}).
14892
14893 @menu
14894 * gnatxref Switches::
14895 * gnatfind Switches::
14896 * Project Files for gnatxref and gnatfind::
14897 * Regular Expressions in gnatfind and gnatxref::
14898 * Examples of gnatxref Usage::
14899 * Examples of gnatfind Usage::
14900 @end menu
14901
14902 @node gnatxref Switches
14903 @section @code{gnatxref} Switches
14904
14905 @noindent
14906 The command invocation for @code{gnatxref} is:
14907 @smallexample
14908 $ gnatxref @ovar{switches} @var{sourcefile1} @r{[}@var{sourcefile2} @dots{}@r{]}
14909 @end smallexample
14910
14911 @noindent
14912 where
14913
14914 @table @var
14915 @item sourcefile1
14916 @itemx sourcefile2
14917 identifies the source files for which a report is to be generated. The
14918 ``with''ed units will be processed too. You must provide at least one file.
14919
14920 These file names are considered to be regular expressions, so for instance
14921 specifying @file{source*.adb} is the same as giving every file in the current
14922 directory whose name starts with @file{source} and whose extension is
14923 @file{adb}.
14924
14925 You shouldn't specify any directory name, just base names. @command{gnatxref}
14926 and @command{gnatfind} will be able to locate these files by themselves using
14927 the source path. If you specify directories, no result is produced.
14928
14929 @end table
14930
14931 @noindent
14932 The switches can be:
14933 @table @option
14934 @c !sort!
14935 @item --version
14936 @cindex @option{--version} @command{gnatxref}
14937 Display Copyright and version, then exit disregarding all other options.
14938
14939 @item --help
14940 @cindex @option{--help} @command{gnatxref}
14941 If @option{--version} was not used, display usage, then exit disregarding
14942 all other options.
14943
14944 @item ^-a^/ALL_FILES^
14945 @cindex @option{^-a^/ALL_FILES^} (@command{gnatxref})
14946 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
14947 the read-only files found in the library search path. Otherwise, these files
14948 will be ignored. This option can be used to protect Gnat sources or your own
14949 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
14950 much faster, and their output much smaller. Read-only here refers to access
14951 or permissions status in the file system for the current user.
14952
14953 @item -aIDIR
14954 @cindex @option{-aIDIR} (@command{gnatxref})
14955 When looking for source files also look in directory DIR. The order in which
14956 source file search is undertaken is the same as for @command{gnatmake}.
14957
14958 @item -aODIR
14959 @cindex @option{-aODIR} (@command{gnatxref})
14960 When searching for library and object files, look in directory
14961 DIR. The order in which library files are searched is the same as for
14962 @command{gnatmake}.
14963
14964 @item -nostdinc
14965 @cindex @option{-nostdinc} (@command{gnatxref})
14966 Do not look for sources in the system default directory.
14967
14968 @item -nostdlib
14969 @cindex @option{-nostdlib} (@command{gnatxref})
14970 Do not look for library files in the system default directory.
14971
14972 @item --RTS=@var{rts-path}
14973 @cindex @option{--RTS} (@command{gnatxref})
14974 Specifies the default location of the runtime library. Same meaning as the
14975 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
14976
14977 @item ^-d^/DERIVED_TYPES^
14978 @cindex @option{^-d^/DERIVED_TYPES^} (@command{gnatxref})
14979 If this switch is set @code{gnatxref} will output the parent type
14980 reference for each matching derived types.
14981
14982 @item ^-f^/FULL_PATHNAME^
14983 @cindex @option{^-f^/FULL_PATHNAME^} (@command{gnatxref})
14984 If this switch is set, the output file names will be preceded by their
14985 directory (if the file was found in the search path). If this switch is
14986 not set, the directory will not be printed.
14987
14988 @item ^-g^/IGNORE_LOCALS^
14989 @cindex @option{^-g^/IGNORE_LOCALS^} (@command{gnatxref})
14990 If this switch is set, information is output only for library-level
14991 entities, ignoring local entities. The use of this switch may accelerate
14992 @code{gnatfind} and @code{gnatxref}.
14993
14994 @item -IDIR
14995 @cindex @option{-IDIR} (@command{gnatxref})
14996 Equivalent to @samp{-aODIR -aIDIR}.
14997
14998 @item -pFILE
14999 @cindex @option{-pFILE} (@command{gnatxref})
15000 Specify a project file to use @xref{Project Files}.
15001 If you need to use the @file{.gpr}
15002 project files, you should use gnatxref through the GNAT driver
15003 (@command{gnat xref -Pproject}).
15004
15005 By default, @code{gnatxref} and @code{gnatfind} will try to locate a
15006 project file in the current directory.
15007
15008 If a project file is either specified or found by the tools, then the content
15009 of the source directory and object directory lines are added as if they
15010 had been specified respectively by @samp{^-aI^/SOURCE_SEARCH^}
15011 and @samp{^-aO^OBJECT_SEARCH^}.
15012 @item ^-u^/UNUSED^
15013 Output only unused symbols. This may be really useful if you give your
15014 main compilation unit on the command line, as @code{gnatxref} will then
15015 display every unused entity and 'with'ed package.
15016
15017 @ifclear vms
15018 @item -v
15019 Instead of producing the default output, @code{gnatxref} will generate a
15020 @file{tags} file that can be used by vi. For examples how to use this
15021 feature, see @ref{Examples of gnatxref Usage}. The tags file is output
15022 to the standard output, thus you will have to redirect it to a file.
15023 @end ifclear
15024
15025 @end table
15026
15027 @noindent
15028 All these switches may be in any order on the command line, and may even
15029 appear after the file names. They need not be separated by spaces, thus
15030 you can say @samp{gnatxref ^-ag^/ALL_FILES/IGNORE_LOCALS^} instead of
15031 @samp{gnatxref ^-a -g^/ALL_FILES /IGNORE_LOCALS^}.
15032
15033 @node gnatfind Switches
15034 @section @code{gnatfind} Switches
15035
15036 @noindent
15037 The command line for @code{gnatfind} is:
15038
15039 @smallexample
15040 $ gnatfind @ovar{switches} @var{pattern}@r{[}:@var{sourcefile}@r{[}:@var{line}@r{[}:@var{column}@r{]]]}
15041 @r{[}@var{file1} @var{file2} @dots{}]
15042 @end smallexample
15043
15044 @noindent
15045 where
15046
15047 @table @var
15048 @item pattern
15049 An entity will be output only if it matches the regular expression found
15050 in @var{pattern}, see @ref{Regular Expressions in gnatfind and gnatxref}.
15051
15052 Omitting the pattern is equivalent to specifying @samp{*}, which
15053 will match any entity. Note that if you do not provide a pattern, you
15054 have to provide both a sourcefile and a line.
15055
15056 Entity names are given in Latin-1, with uppercase/lowercase equivalence
15057 for matching purposes. At the current time there is no support for
15058 8-bit codes other than Latin-1, or for wide characters in identifiers.
15059
15060 @item sourcefile
15061 @code{gnatfind} will look for references, bodies or declarations
15062 of symbols referenced in @file{@var{sourcefile}}, at line @var{line}
15063 and column @var{column}. See @ref{Examples of gnatfind Usage}
15064 for syntax examples.
15065
15066 @item line
15067 is a decimal integer identifying the line number containing
15068 the reference to the entity (or entities) to be located.
15069
15070 @item column
15071 is a decimal integer identifying the exact location on the
15072 line of the first character of the identifier for the
15073 entity reference. Columns are numbered from 1.
15074
15075 @item file1 file2 @dots{}
15076 The search will be restricted to these source files. If none are given, then
15077 the search will be done for every library file in the search path.
15078 These file must appear only after the pattern or sourcefile.
15079
15080 These file names are considered to be regular expressions, so for instance
15081 specifying @file{source*.adb} is the same as giving every file in the current
15082 directory whose name starts with @file{source} and whose extension is
15083 @file{adb}.
15084
15085 The location of the spec of the entity will always be displayed, even if it
15086 isn't in one of @file{@var{file1}}, @file{@var{file2}},@enddots{} The
15087 occurrences of the entity in the separate units of the ones given on the
15088 command line will also be displayed.
15089
15090 Note that if you specify at least one file in this part, @code{gnatfind} may
15091 sometimes not be able to find the body of the subprograms.
15092
15093 @end table
15094
15095 @noindent
15096 At least one of 'sourcefile' or 'pattern' has to be present on
15097 the command line.
15098
15099 The following switches are available:
15100 @table @option
15101 @c !sort!
15102
15103 @cindex @option{--version} @command{gnatfind}
15104 Display Copyright and version, then exit disregarding all other options.
15105
15106 @item --help
15107 @cindex @option{--help} @command{gnatfind}
15108 If @option{--version} was not used, display usage, then exit disregarding
15109 all other options.
15110
15111 @item ^-a^/ALL_FILES^
15112 @cindex @option{^-a^/ALL_FILES^} (@command{gnatfind})
15113 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
15114 the read-only files found in the library search path. Otherwise, these files
15115 will be ignored. This option can be used to protect Gnat sources or your own
15116 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
15117 much faster, and their output much smaller. Read-only here refers to access
15118 or permission status in the file system for the current user.
15119
15120 @item -aIDIR
15121 @cindex @option{-aIDIR} (@command{gnatfind})
15122 When looking for source files also look in directory DIR. The order in which
15123 source file search is undertaken is the same as for @command{gnatmake}.
15124
15125 @item -aODIR
15126 @cindex @option{-aODIR} (@command{gnatfind})
15127 When searching for library and object files, look in directory
15128 DIR. The order in which library files are searched is the same as for
15129 @command{gnatmake}.
15130
15131 @item -nostdinc
15132 @cindex @option{-nostdinc} (@command{gnatfind})
15133 Do not look for sources in the system default directory.
15134
15135 @item -nostdlib
15136 @cindex @option{-nostdlib} (@command{gnatfind})
15137 Do not look for library files in the system default directory.
15138
15139 @item --RTS=@var{rts-path}
15140 @cindex @option{--RTS} (@command{gnatfind})
15141 Specifies the default location of the runtime library. Same meaning as the
15142 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
15143
15144 @item ^-d^/DERIVED_TYPE_INFORMATION^
15145 @cindex @option{^-d^/DERIVED_TYPE_INFORMATION^} (@code{gnatfind})
15146 If this switch is set, then @code{gnatfind} will output the parent type
15147 reference for each matching derived types.
15148
15149 @item ^-e^/EXPRESSIONS^
15150 @cindex @option{^-e^/EXPRESSIONS^} (@command{gnatfind})
15151 By default, @code{gnatfind} accept the simple regular expression set for
15152 @samp{pattern}. If this switch is set, then the pattern will be
15153 considered as full Unix-style regular expression.
15154
15155 @item ^-f^/FULL_PATHNAME^
15156 @cindex @option{^-f^/FULL_PATHNAME^} (@command{gnatfind})
15157 If this switch is set, the output file names will be preceded by their
15158 directory (if the file was found in the search path). If this switch is
15159 not set, the directory will not be printed.
15160
15161 @item ^-g^/IGNORE_LOCALS^
15162 @cindex @option{^-g^/IGNORE_LOCALS^} (@command{gnatfind})
15163 If this switch is set, information is output only for library-level
15164 entities, ignoring local entities. The use of this switch may accelerate
15165 @code{gnatfind} and @code{gnatxref}.
15166
15167 @item -IDIR
15168 @cindex @option{-IDIR} (@command{gnatfind})
15169 Equivalent to @samp{-aODIR -aIDIR}.
15170
15171 @item -pFILE
15172 @cindex @option{-pFILE} (@command{gnatfind})
15173 Specify a project file (@pxref{Project Files}) to use.
15174 By default, @code{gnatxref} and @code{gnatfind} will try to locate a
15175 project file in the current directory.
15176
15177 If a project file is either specified or found by the tools, then the content
15178 of the source directory and object directory lines are added as if they
15179 had been specified respectively by @samp{^-aI^/SOURCE_SEARCH^} and
15180 @samp{^-aO^/OBJECT_SEARCH^}.
15181
15182 @item ^-r^/REFERENCES^
15183 @cindex @option{^-r^/REFERENCES^} (@command{gnatfind})
15184 By default, @code{gnatfind} will output only the information about the
15185 declaration, body or type completion of the entities. If this switch is
15186 set, the @code{gnatfind} will locate every reference to the entities in
15187 the files specified on the command line (or in every file in the search
15188 path if no file is given on the command line).
15189
15190 @item ^-s^/PRINT_LINES^
15191 @cindex @option{^-s^/PRINT_LINES^} (@command{gnatfind})
15192 If this switch is set, then @code{gnatfind} will output the content
15193 of the Ada source file lines were the entity was found.
15194
15195 @item ^-t^/TYPE_HIERARCHY^
15196 @cindex @option{^-t^/TYPE_HIERARCHY^} (@command{gnatfind})
15197 If this switch is set, then @code{gnatfind} will output the type hierarchy for
15198 the specified type. It act like -d option but recursively from parent
15199 type to parent type. When this switch is set it is not possible to
15200 specify more than one file.
15201
15202 @end table
15203
15204 @noindent
15205 All these switches may be in any order on the command line, and may even
15206 appear after the file names. They need not be separated by spaces, thus
15207 you can say @samp{gnatxref ^-ag^/ALL_FILES/IGNORE_LOCALS^} instead of
15208 @samp{gnatxref ^-a -g^/ALL_FILES /IGNORE_LOCALS^}.
15209
15210 As stated previously, gnatfind will search in every directory in the
15211 search path. You can force it to look only in the current directory if
15212 you specify @code{*} at the end of the command line.
15213
15214 @node Project Files for gnatxref and gnatfind
15215 @section Project Files for @command{gnatxref} and @command{gnatfind}
15216
15217 @noindent
15218 Project files allow a programmer to specify how to compile its
15219 application, where to find sources, etc. These files are used
15220 @ifclear vms
15221 primarily by GPS, but they can also be used
15222 @end ifclear
15223 by the two tools
15224 @code{gnatxref} and @code{gnatfind}.
15225
15226 A project file name must end with @file{.gpr}. If a single one is
15227 present in the current directory, then @code{gnatxref} and @code{gnatfind} will
15228 extract the information from it. If multiple project files are found, none of
15229 them is read, and you have to use the @samp{-p} switch to specify the one
15230 you want to use.
15231
15232 The following lines can be included, even though most of them have default
15233 values which can be used in most cases.
15234 The lines can be entered in any order in the file.
15235 Except for @file{src_dir} and @file{obj_dir}, you can only have one instance of
15236 each line. If you have multiple instances, only the last one is taken into
15237 account.
15238
15239 @table @code
15240 @item src_dir=DIR
15241 [default: @code{"^./^[]^"}]
15242 specifies a directory where to look for source files. Multiple @code{src_dir}
15243 lines can be specified and they will be searched in the order they
15244 are specified.
15245
15246 @item obj_dir=DIR
15247 [default: @code{"^./^[]^"}]
15248 specifies a directory where to look for object and library files. Multiple
15249 @code{obj_dir} lines can be specified, and they will be searched in the order
15250 they are specified
15251
15252 @item comp_opt=SWITCHES
15253 [default: @code{""}]
15254 creates a variable which can be referred to subsequently by using
15255 the @code{$@{comp_opt@}} notation. This is intended to store the default
15256 switches given to @command{gnatmake} and @command{gcc}.
15257
15258 @item bind_opt=SWITCHES
15259 [default: @code{""}]
15260 creates a variable which can be referred to subsequently by using
15261 the @samp{$@{bind_opt@}} notation. This is intended to store the default
15262 switches given to @command{gnatbind}.
15263
15264 @item link_opt=SWITCHES
15265 [default: @code{""}]
15266 creates a variable which can be referred to subsequently by using
15267 the @samp{$@{link_opt@}} notation. This is intended to store the default
15268 switches given to @command{gnatlink}.
15269
15270 @item main=EXECUTABLE
15271 [default: @code{""}]
15272 specifies the name of the executable for the application. This variable can
15273 be referred to in the following lines by using the @samp{$@{main@}} notation.
15274
15275 @ifset vms
15276 @item comp_cmd=COMMAND
15277 [default: @code{"GNAT COMPILE /SEARCH=$@{src_dir@} /DEBUG /TRY_SEMANTICS"}]
15278 @end ifset
15279 @ifclear vms
15280 @item comp_cmd=COMMAND
15281 [default: @code{"gcc -c -I$@{src_dir@} -g -gnatq"}]
15282 @end ifclear
15283 specifies the command used to compile a single file in the application.
15284
15285 @ifset vms
15286 @item make_cmd=COMMAND
15287 [default: @code{"GNAT MAKE $@{main@}
15288 /SOURCE_SEARCH=$@{src_dir@} /OBJECT_SEARCH=$@{obj_dir@}
15289 /DEBUG /TRY_SEMANTICS /COMPILER_QUALIFIERS $@{comp_opt@}
15290 /BINDER_QUALIFIERS $@{bind_opt@} /LINKER_QUALIFIERS $@{link_opt@}"}]
15291 @end ifset
15292 @ifclear vms
15293 @item make_cmd=COMMAND
15294 [default: @code{"gnatmake $@{main@} -aI$@{src_dir@}
15295 -aO$@{obj_dir@} -g -gnatq -cargs $@{comp_opt@}
15296 -bargs $@{bind_opt@} -largs $@{link_opt@}"}]
15297 @end ifclear
15298 specifies the command used to recompile the whole application.
15299
15300 @item run_cmd=COMMAND
15301 [default: @code{"$@{main@}"}]
15302 specifies the command used to run the application.
15303
15304 @item debug_cmd=COMMAND
15305 [default: @code{"gdb $@{main@}"}]
15306 specifies the command used to debug the application
15307
15308 @end table
15309
15310 @noindent
15311 @command{gnatxref} and @command{gnatfind} only take into account the
15312 @code{src_dir} and @code{obj_dir} lines, and ignore the others.
15313
15314 @node Regular Expressions in gnatfind and gnatxref
15315 @section Regular Expressions in @code{gnatfind} and @code{gnatxref}
15316
15317 @noindent
15318 As specified in the section about @command{gnatfind}, the pattern can be a
15319 regular expression. Actually, there are to set of regular expressions
15320 which are recognized by the program:
15321
15322 @table @code
15323 @item globbing patterns
15324 These are the most usual regular expression. They are the same that you
15325 generally used in a Unix shell command line, or in a DOS session.
15326
15327 Here is a more formal grammar:
15328 @smallexample
15329 @group
15330 @iftex
15331 @leftskip=.5cm
15332 @end iftex
15333 regexp ::= term
15334 term ::= elmt -- matches elmt
15335 term ::= elmt elmt -- concatenation (elmt then elmt)
15336 term ::= * -- any string of 0 or more characters
15337 term ::= ? -- matches any character
15338 term ::= [char @{char@}] -- matches any character listed
15339 term ::= [char - char] -- matches any character in range
15340 @end group
15341 @end smallexample
15342
15343 @item full regular expression
15344 The second set of regular expressions is much more powerful. This is the
15345 type of regular expressions recognized by utilities such a @file{grep}.
15346
15347 The following is the form of a regular expression, expressed in Ada
15348 reference manual style BNF is as follows
15349
15350 @smallexample
15351 @iftex
15352 @leftskip=.5cm
15353 @end iftex
15354 @group
15355 regexp ::= term @{| term@} -- alternation (term or term @dots{})
15356
15357 term ::= item @{item@} -- concatenation (item then item)
15358
15359 item ::= elmt -- match elmt
15360 item ::= elmt * -- zero or more elmt's
15361 item ::= elmt + -- one or more elmt's
15362 item ::= elmt ? -- matches elmt or nothing
15363 @end group
15364 @group
15365 elmt ::= nschar -- matches given character
15366 elmt ::= [nschar @{nschar@}] -- matches any character listed
15367 elmt ::= [^^^ nschar @{nschar@}] -- matches any character not listed
15368 elmt ::= [char - char] -- matches chars in given range
15369 elmt ::= \ char -- matches given character
15370 elmt ::= . -- matches any single character
15371 elmt ::= ( regexp ) -- parens used for grouping
15372
15373 char ::= any character, including special characters
15374 nschar ::= any character except ()[].*+?^^^
15375 @end group
15376 @end smallexample
15377
15378 Following are a few examples:
15379
15380 @table @samp
15381 @item abcde|fghi
15382 will match any of the two strings @samp{abcde} and @samp{fghi},
15383
15384 @item abc*d
15385 will match any string like @samp{abd}, @samp{abcd}, @samp{abccd},
15386 @samp{abcccd}, and so on,
15387
15388 @item [a-z]+
15389 will match any string which has only lowercase characters in it (and at
15390 least one character.
15391
15392 @end table
15393 @end table
15394
15395 @node Examples of gnatxref Usage
15396 @section Examples of @code{gnatxref} Usage
15397
15398 @subsection General Usage
15399
15400 @noindent
15401 For the following examples, we will consider the following units:
15402
15403 @smallexample @c ada
15404 @group
15405 @cartouche
15406 main.ads:
15407 1: with Bar;
15408 2: package Main is
15409 3: procedure Foo (B : in Integer);
15410 4: C : Integer;
15411 5: private
15412 6: D : Integer;
15413 7: end Main;
15414
15415 main.adb:
15416 1: package body Main is
15417 2: procedure Foo (B : in Integer) is
15418 3: begin
15419 4: C := B;
15420 5: D := B;
15421 6: Bar.Print (B);
15422 7: Bar.Print (C);
15423 8: end Foo;
15424 9: end Main;
15425
15426 bar.ads:
15427 1: package Bar is
15428 2: procedure Print (B : Integer);
15429 3: end bar;
15430 @end cartouche
15431 @end group
15432 @end smallexample
15433
15434 @table @code
15435
15436 @noindent
15437 The first thing to do is to recompile your application (for instance, in
15438 that case just by doing a @samp{gnatmake main}, so that GNAT generates
15439 the cross-referencing information.
15440 You can then issue any of the following commands:
15441
15442 @item gnatxref main.adb
15443 @code{gnatxref} generates cross-reference information for main.adb
15444 and every unit 'with'ed by main.adb.
15445
15446 The output would be:
15447 @smallexample
15448 @iftex
15449 @leftskip=0cm
15450 @end iftex
15451 B Type: Integer
15452 Decl: bar.ads 2:22
15453 B Type: Integer
15454 Decl: main.ads 3:20
15455 Body: main.adb 2:20
15456 Ref: main.adb 4:13 5:13 6:19
15457 Bar Type: Unit
15458 Decl: bar.ads 1:9
15459 Ref: main.adb 6:8 7:8
15460 main.ads 1:6
15461 C Type: Integer
15462 Decl: main.ads 4:5
15463 Modi: main.adb 4:8
15464 Ref: main.adb 7:19
15465 D Type: Integer
15466 Decl: main.ads 6:5
15467 Modi: main.adb 5:8
15468 Foo Type: Unit
15469 Decl: main.ads 3:15
15470 Body: main.adb 2:15
15471 Main Type: Unit
15472 Decl: main.ads 2:9
15473 Body: main.adb 1:14
15474 Print Type: Unit
15475 Decl: bar.ads 2:15
15476 Ref: main.adb 6:12 7:12
15477 @end smallexample
15478
15479 @noindent
15480 that is the entity @code{Main} is declared in main.ads, line 2, column 9,
15481 its body is in main.adb, line 1, column 14 and is not referenced any where.
15482
15483 The entity @code{Print} is declared in bar.ads, line 2, column 15 and it
15484 it referenced in main.adb, line 6 column 12 and line 7 column 12.
15485
15486 @item gnatxref package1.adb package2.ads
15487 @code{gnatxref} will generates cross-reference information for
15488 package1.adb, package2.ads and any other package 'with'ed by any
15489 of these.
15490
15491 @end table
15492
15493 @ifclear vms
15494 @subsection Using gnatxref with vi
15495
15496 @code{gnatxref} can generate a tags file output, which can be used
15497 directly from @command{vi}. Note that the standard version of @command{vi}
15498 will not work properly with overloaded symbols. Consider using another
15499 free implementation of @command{vi}, such as @command{vim}.
15500
15501 @smallexample
15502 $ gnatxref -v gnatfind.adb > tags
15503 @end smallexample
15504
15505 @noindent
15506 will generate the tags file for @code{gnatfind} itself (if the sources
15507 are in the search path!).
15508
15509 From @command{vi}, you can then use the command @samp{:tag @var{entity}}
15510 (replacing @var{entity} by whatever you are looking for), and vi will
15511 display a new file with the corresponding declaration of entity.
15512 @end ifclear
15513
15514 @node Examples of gnatfind Usage
15515 @section Examples of @code{gnatfind} Usage
15516
15517 @table @code
15518
15519 @item gnatfind ^-f^/FULL_PATHNAME^ xyz:main.adb
15520 Find declarations for all entities xyz referenced at least once in
15521 main.adb. The references are search in every library file in the search
15522 path.
15523
15524 The directories will be printed as well (as the @samp{^-f^/FULL_PATHNAME^}
15525 switch is set)
15526
15527 The output will look like:
15528 @smallexample
15529 ^directory/^[directory]^main.ads:106:14: xyz <= declaration
15530 ^directory/^[directory]^main.adb:24:10: xyz <= body
15531 ^directory/^[directory]^foo.ads:45:23: xyz <= declaration
15532 @end smallexample
15533
15534 @noindent
15535 that is to say, one of the entities xyz found in main.adb is declared at
15536 line 12 of main.ads (and its body is in main.adb), and another one is
15537 declared at line 45 of foo.ads
15538
15539 @item gnatfind ^-fs^/FULL_PATHNAME/SOURCE_LINE^ xyz:main.adb
15540 This is the same command as the previous one, instead @code{gnatfind} will
15541 display the content of the Ada source file lines.
15542
15543 The output will look like:
15544
15545 @smallexample
15546 ^directory/^[directory]^main.ads:106:14: xyz <= declaration
15547 procedure xyz;
15548 ^directory/^[directory]^main.adb:24:10: xyz <= body
15549 procedure xyz is
15550 ^directory/^[directory]^foo.ads:45:23: xyz <= declaration
15551 xyz : Integer;
15552 @end smallexample
15553
15554 @noindent
15555 This can make it easier to find exactly the location your are looking
15556 for.
15557
15558 @item gnatfind ^-r^/REFERENCES^ "*x*":main.ads:123 foo.adb
15559 Find references to all entities containing an x that are
15560 referenced on line 123 of main.ads.
15561 The references will be searched only in main.ads and foo.adb.
15562
15563 @item gnatfind main.ads:123
15564 Find declarations and bodies for all entities that are referenced on
15565 line 123 of main.ads.
15566
15567 This is the same as @code{gnatfind "*":main.adb:123}.
15568
15569 @item gnatfind ^mydir/^[mydir]^main.adb:123:45
15570 Find the declaration for the entity referenced at column 45 in
15571 line 123 of file main.adb in directory mydir. Note that it
15572 is usual to omit the identifier name when the column is given,
15573 since the column position identifies a unique reference.
15574
15575 The column has to be the beginning of the identifier, and should not
15576 point to any character in the middle of the identifier.
15577
15578 @end table
15579
15580 @c *********************************
15581 @node The GNAT Pretty-Printer gnatpp
15582 @chapter The GNAT Pretty-Printer @command{gnatpp}
15583 @findex gnatpp
15584 @cindex Pretty-Printer
15585
15586 @noindent
15587 ^The @command{gnatpp} tool^GNAT PRETTY^ is an ASIS-based utility
15588 for source reformatting / pretty-printing.
15589 It takes an Ada source file as input and generates a reformatted
15590 version as output.
15591 You can specify various style directives via switches; e.g.,
15592 identifier case conventions, rules of indentation, and comment layout.
15593
15594 To produce a reformatted file, @command{gnatpp} generates and uses the ASIS
15595 tree for the input source and thus requires the input to be syntactically and
15596 semantically legal.
15597 If this condition is not met, @command{gnatpp} will terminate with an
15598 error message; no output file will be generated.
15599
15600 If the source files presented to @command{gnatpp} contain
15601 preprocessing directives, then the output file will
15602 correspond to the generated source after all
15603 preprocessing is carried out. There is no way
15604 using @command{gnatpp} to obtain pretty printed files that
15605 include the preprocessing directives.
15606
15607 If the compilation unit
15608 contained in the input source depends semantically upon units located
15609 outside the current directory, you have to provide the source search path
15610 when invoking @command{gnatpp}, if these units are contained in files with
15611 names that do not follow the GNAT file naming rules, you have to provide
15612 the configuration file describing the corresponding naming scheme;
15613 see the description of the @command{gnatpp}
15614 switches below. Another possibility is to use a project file and to
15615 call @command{gnatpp} through the @command{gnat} driver
15616
15617 The @command{gnatpp} command has the form
15618
15619 @smallexample
15620 $ gnatpp @ovar{switches} @var{filename}
15621 @end smallexample
15622
15623 @noindent
15624 where
15625 @itemize @bullet
15626 @item
15627 @var{switches} is an optional sequence of switches defining such properties as
15628 the formatting rules, the source search path, and the destination for the
15629 output source file
15630
15631 @item
15632 @var{filename} is the name (including the extension) of the source file to
15633 reformat; ``wildcards'' or several file names on the same gnatpp command are
15634 allowed. The file name may contain path information; it does not have to
15635 follow the GNAT file naming rules
15636 @end itemize
15637
15638 @menu
15639 * Switches for gnatpp::
15640 * Formatting Rules::
15641 @end menu
15642
15643 @node Switches for gnatpp
15644 @section Switches for @command{gnatpp}
15645
15646 @noindent
15647 The following subsections describe the various switches accepted by
15648 @command{gnatpp}, organized by category.
15649
15650 @ifclear vms
15651 You specify a switch by supplying a name and generally also a value.
15652 In many cases the values for a switch with a given name are incompatible with
15653 each other
15654 (for example the switch that controls the casing of a reserved word may have
15655 exactly one value: upper case, lower case, or
15656 mixed case) and thus exactly one such switch can be in effect for an
15657 invocation of @command{gnatpp}.
15658 If more than one is supplied, the last one is used.
15659 However, some values for the same switch are mutually compatible.
15660 You may supply several such switches to @command{gnatpp}, but then
15661 each must be specified in full, with both the name and the value.
15662 Abbreviated forms (the name appearing once, followed by each value) are
15663 not permitted.
15664 For example, to set
15665 the alignment of the assignment delimiter both in declarations and in
15666 assignment statements, you must write @option{-A2A3}
15667 (or @option{-A2 -A3}), but not @option{-A23}.
15668 @end ifclear
15669
15670 @ifset vms
15671 In many cases the set of options for a given qualifier are incompatible with
15672 each other (for example the qualifier that controls the casing of a reserved
15673 word may have exactly one option, which specifies either upper case, lower
15674 case, or mixed case), and thus exactly one such option can be in effect for
15675 an invocation of @command{gnatpp}.
15676 If more than one is supplied, the last one is used.
15677 However, some qualifiers have options that are mutually compatible,
15678 and then you may then supply several such options when invoking
15679 @command{gnatpp}.
15680 @end ifset
15681
15682 In most cases, it is obvious whether or not the
15683 ^values for a switch with a given name^options for a given qualifier^
15684 are compatible with each other.
15685 When the semantics might not be evident, the summaries below explicitly
15686 indicate the effect.
15687
15688 @menu
15689 * Alignment Control::
15690 * Casing Control::
15691 * Construct Layout Control::
15692 * General Text Layout Control::
15693 * Other Formatting Options::
15694 * Setting the Source Search Path::
15695 * Output File Control::
15696 * Other gnatpp Switches::
15697 @end menu
15698
15699 @node Alignment Control
15700 @subsection Alignment Control
15701 @cindex Alignment control in @command{gnatpp}
15702
15703 @noindent
15704 Programs can be easier to read if certain constructs are vertically aligned.
15705 By default all alignments are set ON.
15706 Through the @option{^-A0^/ALIGN=OFF^} switch you may reset the default to
15707 OFF, and then use one or more of the other
15708 ^@option{-A@var{n}} switches^@option{/ALIGN} options^
15709 to activate alignment for specific constructs.
15710
15711 @table @option
15712 @cindex @option{^-A@var{n}^/ALIGN^} (@command{gnatpp})
15713
15714 @ifset vms
15715 @item /ALIGN=ON
15716 Set all alignments to ON
15717 @end ifset
15718
15719 @item ^-A0^/ALIGN=OFF^
15720 Set all alignments to OFF
15721
15722 @item ^-A1^/ALIGN=COLONS^
15723 Align @code{:} in declarations
15724
15725 @item ^-A2^/ALIGN=DECLARATIONS^
15726 Align @code{:=} in initializations in declarations
15727
15728 @item ^-A3^/ALIGN=STATEMENTS^
15729 Align @code{:=} in assignment statements
15730
15731 @item ^-A4^/ALIGN=ARROWS^
15732 Align @code{=>} in associations
15733
15734 @item ^-A5^/ALIGN=COMPONENT_CLAUSES^
15735 Align @code{at} keywords in the component clauses in record
15736 representation clauses
15737 @end table
15738
15739 @noindent
15740 The @option{^-A^/ALIGN^} switches are mutually compatible; any combination
15741 is allowed.
15742
15743 @node Casing Control
15744 @subsection Casing Control
15745 @cindex Casing control in @command{gnatpp}
15746
15747 @noindent
15748 @command{gnatpp} allows you to specify the casing for reserved words,
15749 pragma names, attribute designators and identifiers.
15750 For identifiers you may define a
15751 general rule for name casing but also override this rule
15752 via a set of dictionary files.
15753
15754 Three types of casing are supported: lower case, upper case, and mixed case.
15755 Lower and upper case are self-explanatory (but since some letters in
15756 Latin1 and other GNAT-supported character sets
15757 exist only in lower-case form, an upper case conversion will have no
15758 effect on them.)
15759 ``Mixed case'' means that the first letter, and also each letter immediately
15760 following an underscore, are converted to their uppercase forms;
15761 all the other letters are converted to their lowercase forms.
15762
15763 @table @option
15764 @cindex @option{^-a@var{x}^/ATTRIBUTE^} (@command{gnatpp})
15765 @item ^-aL^/ATTRIBUTE_CASING=LOWER_CASE^
15766 Attribute designators are lower case
15767
15768 @item ^-aU^/ATTRIBUTE_CASING=UPPER_CASE^
15769 Attribute designators are upper case
15770
15771 @item ^-aM^/ATTRIBUTE_CASING=MIXED_CASE^
15772 Attribute designators are mixed case (this is the default)
15773
15774 @cindex @option{^-k@var{x}^/KEYWORD_CASING^} (@command{gnatpp})
15775 @item ^-kL^/KEYWORD_CASING=LOWER_CASE^
15776 Keywords (technically, these are known in Ada as @emph{reserved words}) are
15777 lower case (this is the default)
15778
15779 @item ^-kU^/KEYWORD_CASING=UPPER_CASE^
15780 Keywords are upper case
15781
15782 @cindex @option{^-n@var{x}^/NAME_CASING^} (@command{gnatpp})
15783 @item ^-nD^/NAME_CASING=AS_DECLARED^
15784 Name casing for defining occurrences are as they appear in the source file
15785 (this is the default)
15786
15787 @item ^-nU^/NAME_CASING=UPPER_CASE^
15788 Names are in upper case
15789
15790 @item ^-nL^/NAME_CASING=LOWER_CASE^
15791 Names are in lower case
15792
15793 @item ^-nM^/NAME_CASING=MIXED_CASE^
15794 Names are in mixed case
15795
15796 @cindex @option{^-p@var{x}^/PRAGMA_CASING^} (@command{gnatpp})
15797 @item ^-pL^/PRAGMA_CASING=LOWER_CASE^
15798 Pragma names are lower case
15799
15800 @item ^-pU^/PRAGMA_CASING=UPPER_CASE^
15801 Pragma names are upper case
15802
15803 @item ^-pM^/PRAGMA_CASING=MIXED_CASE^
15804 Pragma names are mixed case (this is the default)
15805
15806 @item ^-D@var{file}^/DICTIONARY=@var{file}^
15807 @cindex @option{^-D^/DICTIONARY^} (@command{gnatpp})
15808 Use @var{file} as a @emph{dictionary file} that defines
15809 the casing for a set of specified names,
15810 thereby overriding the effect on these names by
15811 any explicit or implicit
15812 ^-n^/NAME_CASING^ switch.
15813 To supply more than one dictionary file,
15814 use ^several @option{-D} switches^a list of files as options^.
15815
15816 @noindent
15817 @option{gnatpp} implicitly uses a @emph{default dictionary file}
15818 to define the casing for the Ada predefined names and
15819 the names declared in the GNAT libraries.
15820
15821 @item ^-D-^/SPECIFIC_CASING^
15822 @cindex @option{^-D-^/SPECIFIC_CASING^} (@command{gnatpp})
15823 Do not use the default dictionary file;
15824 instead, use the casing
15825 defined by a @option{^-n^/NAME_CASING^} switch and any explicit
15826 dictionary file(s)
15827 @end table
15828
15829 @noindent
15830 The structure of a dictionary file, and details on the conventions
15831 used in the default dictionary file, are defined in @ref{Name Casing}.
15832
15833 The @option{^-D-^/SPECIFIC_CASING^} and
15834 @option{^-D@var{file}^/DICTIONARY=@var{file}^} switches are mutually
15835 compatible.
15836
15837 @node Construct Layout Control
15838 @subsection Construct Layout Control
15839 @cindex Layout control in @command{gnatpp}
15840
15841 @noindent
15842 This group of @command{gnatpp} switches controls the layout of comments and
15843 complex syntactic constructs. See @ref{Formatting Comments} for details
15844 on their effect.
15845
15846 @table @option
15847 @cindex @option{^-c@var{n}^/COMMENTS_LAYOUT^} (@command{gnatpp})
15848 @item ^-c0^/COMMENTS_LAYOUT=UNTOUCHED^
15849 All the comments remain unchanged
15850
15851 @item ^-c1^/COMMENTS_LAYOUT=DEFAULT^
15852 GNAT-style comment line indentation (this is the default).
15853
15854 @item ^-c2^/COMMENTS_LAYOUT=STANDARD_INDENT^
15855 Reference-manual comment line indentation.
15856
15857 @item ^-c3^/COMMENTS_LAYOUT=GNAT_BEGINNING^
15858 GNAT-style comment beginning
15859
15860 @item ^-c4^/COMMENTS_LAYOUT=REFORMAT^
15861 Reformat comment blocks
15862
15863 @item ^-c5^/COMMENTS_LAYOUT=KEEP_SPECIAL^
15864 Keep unchanged special form comments
15865
15866 Reformat comment blocks
15867
15868 @cindex @option{^-l@var{n}^/CONSTRUCT_LAYOUT^} (@command{gnatpp})
15869 @item ^-l1^/CONSTRUCT_LAYOUT=GNAT^
15870 GNAT-style layout (this is the default)
15871
15872 @item ^-l2^/CONSTRUCT_LAYOUT=COMPACT^
15873 Compact layout
15874
15875 @item ^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^
15876 Uncompact layout
15877
15878 @cindex @option{^-N^/NOTABS^} (@command{gnatpp})
15879 @item ^-N^/NOTABS^
15880 All the VT characters are removed from the comment text. All the HT characters
15881 are expanded with the sequences of space characters to get to the next tab
15882 stops.
15883
15884 @cindex @option{^--no-separate-is^/NO_SEPARATE_IS^} (@command{gnatpp})
15885 @item ^--no-separate-is^/NO_SEPARATE_IS^
15886 Do not place the keyword @code{is} on a separate line in a subprogram body in
15887 case if the spec occupies more then one line.
15888
15889 @cindex @option{^--separate-loop-then^/SEPARATE_LOOP_THEN^} (@command{gnatpp})
15890 @item ^--separate-loop-then^/SEPARATE_LOOP_THEN^
15891 Place the keyword @code{loop} in FOR and WHILE loop statements and the
15892 keyword @code{then} in IF statements on a separate line.
15893
15894 @cindex @option{^--no-separate-loop-then^/NO_SEPARATE_LOOP_THEN^} (@command{gnatpp})
15895 @item ^--no-separate-loop-then^/NO_SEPARATE_LOOP_THEN^
15896 Do not place the keyword @code{loop} in FOR and WHILE loop statements and the
15897 keyword @code{then} in IF statements on a separate line. This option is
15898 incompatible with @option{^--separate-loop-then^/SEPARATE_LOOP_THEN^} option.
15899
15900 @cindex @option{^--use-on-new-line^/USE_ON_NEW_LINE^} (@command{gnatpp})
15901 @item ^--use-on-new-line^/USE_ON_NEW_LINE^
15902 Start each USE clause in a context clause from a separate line.
15903
15904 @cindex @option{^--separate-stmt-name^/STMT_NAME_ON_NEW_LINE^} (@command{gnatpp})
15905 @item ^--separate-stmt-name^/STMT_NAME_ON_NEW_LINE^
15906 Use a separate line for a loop or block statement name, but do not use an extra
15907 indentation level for the statement itself.
15908
15909 @end table
15910
15911 @ifclear vms
15912 @noindent
15913 The @option{-c1} and @option{-c2} switches are incompatible.
15914 The @option{-c3} and @option{-c4} switches are compatible with each other and
15915 also with @option{-c1} and @option{-c2}. The @option{-c0} switch disables all
15916 the other comment formatting switches.
15917
15918 The @option{-l1}, @option{-l2}, and @option{-l3} switches are incompatible.
15919 @end ifclear
15920
15921 @ifset vms
15922 @noindent
15923 For the @option{/COMMENTS_LAYOUT} qualifier:
15924 @itemize @bullet
15925 @item
15926 The @option{DEFAULT} and @option{STANDARD_INDENT} options are incompatible.
15927 @item
15928 The @option{GNAT_BEGINNING} and @option{REFORMAT} options are compatible with
15929 each other and also with @option{DEFAULT} and @option{STANDARD_INDENT}.
15930 @end itemize
15931
15932 @noindent
15933 The @option{GNAT}, @option{COMPACT}, and @option{UNCOMPACT} options for the
15934 @option{/CONSTRUCT_LAYOUT} qualifier are incompatible.
15935 @end ifset
15936
15937 @node General Text Layout Control
15938 @subsection General Text Layout Control
15939
15940 @noindent
15941 These switches allow control over line length and indentation.
15942
15943 @table @option
15944 @item ^-M@var{nnn}^/LINE_LENGTH_MAX=@var{nnn}^
15945 @cindex @option{^-M^/LINE_LENGTH^} (@command{gnatpp})
15946 Maximum line length, @var{nnn} from 32@dots{}256, the default value is 79
15947
15948 @item ^-i@var{nnn}^/INDENTATION_LEVEL=@var{nnn}^
15949 @cindex @option{^-i^/INDENTATION_LEVEL^} (@command{gnatpp})
15950 Indentation level, @var{nnn} from 1@dots{}9, the default value is 3
15951
15952 @item ^-cl@var{nnn}^/CONTINUATION_INDENT=@var{nnn}^
15953 @cindex @option{^-cl^/CONTINUATION_INDENT^} (@command{gnatpp})
15954 Indentation level for continuation lines (relative to the line being
15955 continued), @var{nnn} from 1@dots{}9.
15956 The default
15957 value is one less then the (normal) indentation level, unless the
15958 indentation is set to 1 (in which case the default value for continuation
15959 line indentation is also 1)
15960 @end table
15961
15962 @node Other Formatting Options
15963 @subsection Other Formatting Options
15964
15965 @noindent
15966 These switches control the inclusion of missing end/exit labels, and
15967 the indentation level in @b{case} statements.
15968
15969 @table @option
15970 @item ^-e^/NO_MISSED_LABELS^
15971 @cindex @option{^-e^/NO_MISSED_LABELS^} (@command{gnatpp})
15972 Do not insert missing end/exit labels. An end label is the name of
15973 a construct that may optionally be repeated at the end of the
15974 construct's declaration;
15975 e.g., the names of packages, subprograms, and tasks.
15976 An exit label is the name of a loop that may appear as target
15977 of an exit statement within the loop.
15978 By default, @command{gnatpp} inserts these end/exit labels when
15979 they are absent from the original source. This option suppresses such
15980 insertion, so that the formatted source reflects the original.
15981
15982 @item ^-ff^/FORM_FEED_AFTER_PRAGMA_PAGE^
15983 @cindex @option{^-ff^/FORM_FEED_AFTER_PRAGMA_PAGE^} (@command{gnatpp})
15984 Insert a Form Feed character after a pragma Page.
15985
15986 @item ^-T@var{nnn}^/MAX_INDENT=@var{nnn}^
15987 @cindex @option{^-T^/MAX_INDENT^} (@command{gnatpp})
15988 Do not use an additional indentation level for @b{case} alternatives
15989 and variants if there are @var{nnn} or more (the default
15990 value is 10).
15991 If @var{nnn} is 0, an additional indentation level is
15992 used for @b{case} alternatives and variants regardless of their number.
15993 @end table
15994
15995 @node Setting the Source Search Path
15996 @subsection Setting the Source Search Path
15997
15998 @noindent
15999 To define the search path for the input source file, @command{gnatpp}
16000 uses the same switches as the GNAT compiler, with the same effects.
16001
16002 @table @option
16003 @item ^-I^/SEARCH=^@var{dir}
16004 @cindex @option{^-I^/SEARCH^} (@code{gnatpp})
16005 The same as the corresponding gcc switch
16006
16007 @item ^-I-^/NOCURRENT_DIRECTORY^
16008 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatpp})
16009 The same as the corresponding gcc switch
16010
16011 @item ^-gnatec^/CONFIGURATION_PRAGMAS_FILE^=@var{path}
16012 @cindex @option{^-gnatec^/CONFIGURATION_PRAGMAS_FILE^} (@code{gnatpp})
16013 The same as the corresponding gcc switch
16014
16015 @item ^--RTS^/RUNTIME_SYSTEM^=@var{path}
16016 @cindex @option{^--RTS^/RUNTIME_SYSTEM^} (@code{gnatpp})
16017 The same as the corresponding gcc switch
16018
16019 @end table
16020
16021 @node Output File Control
16022 @subsection Output File Control
16023
16024 @noindent
16025 By default the output is sent to the file whose name is obtained by appending
16026 the ^@file{.pp}^@file{$PP}^ suffix to the name of the input file
16027 (if the file with this name already exists, it is unconditionally overwritten).
16028 Thus if the input file is @file{^my_ada_proc.adb^MY_ADA_PROC.ADB^} then
16029 @command{gnatpp} will produce @file{^my_ada_proc.adb.pp^MY_ADA_PROC.ADB$PP^}
16030 as output file.
16031 The output may be redirected by the following switches:
16032
16033 @table @option
16034 @item ^-pipe^/STANDARD_OUTPUT^
16035 @cindex @option{^-pipe^/STANDARD_OUTPUT^} (@code{gnatpp})
16036 Send the output to @code{Standard_Output}
16037
16038 @item ^-o @var{output_file}^/OUTPUT=@var{output_file}^
16039 @cindex @option{^-o^/OUTPUT^} (@code{gnatpp})
16040 Write the output into @var{output_file}.
16041 If @var{output_file} already exists, @command{gnatpp} terminates without
16042 reading or processing the input file.
16043
16044 @item ^-of ^/FORCED_OUTPUT=^@var{output_file}
16045 @cindex @option{^-of^/FORCED_OUTPUT^} (@code{gnatpp})
16046 Write the output into @var{output_file}, overwriting the existing file
16047 (if one is present).
16048
16049 @item ^-r^/REPLACE^
16050 @cindex @option{^-r^/REPLACE^} (@code{gnatpp})
16051 Replace the input source file with the reformatted output, and copy the
16052 original input source into the file whose name is obtained by appending the
16053 ^@file{.npp}^@file{$NPP}^ suffix to the name of the input file.
16054 If a file with this name already exists, @command{gnatpp} terminates without
16055 reading or processing the input file.
16056
16057 @item ^-rf^/OVERRIDING_REPLACE^
16058 @cindex @option{^-rf^/OVERRIDING_REPLACE^} (@code{gnatpp})
16059 Like @option{^-r^/REPLACE^} except that if the file with the specified name
16060 already exists, it is overwritten.
16061
16062 @item ^-rnb^/REPLACE_NO_BACKUP^
16063 @cindex @option{^-rnb^/REPLACE_NO_BACKUP^} (@code{gnatpp})
16064 Replace the input source file with the reformatted output without
16065 creating any backup copy of the input source.
16066
16067 @item ^--eol=@var{xxx}^/END_OF_LINE=@var{xxx}^
16068 @cindex @option{^--eol^/END_OF_LINE^} (@code{gnatpp})
16069 Specifies the format of the reformatted output file. The @var{xxx}
16070 ^string specified with the switch^option^ may be either
16071 @itemize @bullet
16072 @item ``@option{^dos^DOS^}'' MS DOS style, lines end with CR LF characters
16073 @item ``@option{^crlf^CRLF^}''
16074 the same as @option{^crlf^CRLF^}
16075 @item ``@option{^unix^UNIX^}'' UNIX style, lines end with LF character
16076 @item ``@option{^lf^LF^}''
16077 the same as @option{^unix^UNIX^}
16078 @end itemize
16079
16080 @item ^-W^/RESULT_ENCODING=^@var{e}
16081 @cindex @option{^-W^/RESULT_ENCODING=^} (@command{gnatpp})
16082 Specify the wide character encoding method used to write the code in the
16083 result file
16084 @var{e} is one of the following:
16085
16086 @itemize @bullet
16087
16088 @item ^h^HEX^
16089 Hex encoding
16090
16091 @item ^u^UPPER^
16092 Upper half encoding
16093
16094 @item ^s^SHIFT_JIS^
16095 Shift/JIS encoding
16096
16097 @item ^e^EUC^
16098 EUC encoding
16099
16100 @item ^8^UTF8^
16101 UTF-8 encoding
16102
16103 @item ^b^BRACKETS^
16104 Brackets encoding (default value)
16105 @end itemize
16106
16107 @end table
16108
16109 @noindent
16110 Options @option{^-pipe^/STANDARD_OUTPUT^},
16111 @option{^-o^/OUTPUT^} and
16112 @option{^-of^/FORCED_OUTPUT^} are allowed only if the call to gnatpp
16113 contains only one file to reformat.
16114 Option
16115 @option{^--eol^/END_OF_LINE^}
16116 and
16117 @option{^-W^/RESULT_ENCODING^}
16118 cannot be used together
16119 with @option{^-pipe^/STANDARD_OUTPUT^} option.
16120
16121 @node Other gnatpp Switches
16122 @subsection Other @code{gnatpp} Switches
16123
16124 @noindent
16125 The additional @command{gnatpp} switches are defined in this subsection.
16126
16127 @table @option
16128 @item ^-files @var{filename}^/FILES=@var{output_file}^
16129 @cindex @option{^-files^/FILES^} (@code{gnatpp})
16130 Take the argument source files from the specified file. This file should be an
16131 ordinary textual file containing file names separated by spaces or
16132 line breaks. You can use this switch more then once in the same call to
16133 @command{gnatpp}. You also can combine this switch with explicit list of
16134 files.
16135
16136 @item ^-v^/VERBOSE^
16137 @cindex @option{^-v^/VERBOSE^} (@code{gnatpp})
16138 Verbose mode;
16139 @command{gnatpp} generates version information and then
16140 a trace of the actions it takes to produce or obtain the ASIS tree.
16141
16142 @item ^-w^/WARNINGS^
16143 @cindex @option{^-w^/WARNINGS^} (@code{gnatpp})
16144 Warning mode;
16145 @command{gnatpp} generates a warning whenever it cannot provide
16146 a required layout in the result source.
16147 @end table
16148
16149 @node Formatting Rules
16150 @section Formatting Rules
16151
16152 @noindent
16153 The following subsections show how @command{gnatpp} treats ``white space'',
16154 comments, program layout, and name casing.
16155 They provide the detailed descriptions of the switches shown above.
16156
16157 @menu
16158 * White Space and Empty Lines::
16159 * Formatting Comments::
16160 * Construct Layout::
16161 * Name Casing::
16162 @end menu
16163
16164 @node White Space and Empty Lines
16165 @subsection White Space and Empty Lines
16166
16167 @noindent
16168 @command{gnatpp} does not have an option to control space characters.
16169 It will add or remove spaces according to the style illustrated by the
16170 examples in the @cite{Ada Reference Manual}.
16171
16172 The only format effectors
16173 (see @cite{Ada Reference Manual}, paragraph 2.1(13))
16174 that will appear in the output file are platform-specific line breaks,
16175 and also format effectors within (but not at the end of) comments.
16176 In particular, each horizontal tab character that is not inside
16177 a comment will be treated as a space and thus will appear in the
16178 output file as zero or more spaces depending on
16179 the reformatting of the line in which it appears.
16180 The only exception is a Form Feed character, which is inserted after a
16181 pragma @code{Page} when @option{-ff} is set.
16182
16183 The output file will contain no lines with trailing ``white space'' (spaces,
16184 format effectors).
16185
16186 Empty lines in the original source are preserved
16187 only if they separate declarations or statements.
16188 In such contexts, a
16189 sequence of two or more empty lines is replaced by exactly one empty line.
16190 Note that a blank line will be removed if it separates two ``comment blocks''
16191 (a comment block is a sequence of whole-line comments).
16192 In order to preserve a visual separation between comment blocks, use an
16193 ``empty comment'' (a line comprising only hyphens) rather than an empty line.
16194 Likewise, if for some reason you wish to have a sequence of empty lines,
16195 use a sequence of empty comments instead.
16196
16197 @node Formatting Comments
16198 @subsection Formatting Comments
16199
16200 @noindent
16201 Comments in Ada code are of two kinds:
16202 @itemize @bullet
16203 @item
16204 a @emph{whole-line comment}, which appears by itself (possibly preceded by
16205 ``white space'') on a line
16206
16207 @item
16208 an @emph{end-of-line comment}, which follows some other Ada lexical element
16209 on the same line.
16210 @end itemize
16211
16212 @noindent
16213 The indentation of a whole-line comment is that of either
16214 the preceding or following line in
16215 the formatted source, depending on switch settings as will be described below.
16216
16217 For an end-of-line comment, @command{gnatpp} leaves the same number of spaces
16218 between the end of the preceding Ada lexical element and the beginning
16219 of the comment as appear in the original source,
16220 unless either the comment has to be split to
16221 satisfy the line length limitation, or else the next line contains a
16222 whole line comment that is considered a continuation of this end-of-line
16223 comment (because it starts at the same position).
16224 In the latter two
16225 cases, the start of the end-of-line comment is moved right to the nearest
16226 multiple of the indentation level.
16227 This may result in a ``line overflow'' (the right-shifted comment extending
16228 beyond the maximum line length), in which case the comment is split as
16229 described below.
16230
16231 There is a difference between @option{^-c1^/COMMENTS_LAYOUT=DEFAULT^}
16232 (GNAT-style comment line indentation)
16233 and @option{^-c2^/COMMENTS_LAYOUT=STANDARD_INDENT^}
16234 (reference-manual comment line indentation).
16235 With reference-manual style, a whole-line comment is indented as if it
16236 were a declaration or statement at the same place
16237 (i.e., according to the indentation of the preceding line(s)).
16238 With GNAT style, a whole-line comment that is immediately followed by an
16239 @b{if} or @b{case} statement alternative, a record variant, or the reserved
16240 word @b{begin}, is indented based on the construct that follows it.
16241
16242 For example:
16243 @smallexample @c ada
16244 @cartouche
16245 if A then
16246 null;
16247 -- some comment
16248 else
16249 null;
16250 end if;
16251 @end cartouche
16252 @end smallexample
16253
16254 @noindent
16255 Reference-manual indentation produces:
16256
16257 @smallexample @c ada
16258 @cartouche
16259 if A then
16260 null;
16261 -- some comment
16262 else
16263 null;
16264 end if;
16265 @end cartouche
16266 @end smallexample
16267
16268 @noindent
16269 while GNAT-style indentation produces:
16270
16271 @smallexample @c ada
16272 @cartouche
16273 if A then
16274 null;
16275 -- some comment
16276 else
16277 null;
16278 end if;
16279 @end cartouche
16280 @end smallexample
16281
16282 @noindent
16283 The @option{^-c3^/COMMENTS_LAYOUT=GNAT_BEGINNING^} switch
16284 (GNAT style comment beginning) has the following
16285 effect:
16286
16287 @itemize @bullet
16288 @item
16289 For each whole-line comment that does not end with two hyphens,
16290 @command{gnatpp} inserts spaces if necessary after the starting two hyphens
16291 to ensure that there are at least two spaces between these hyphens and the
16292 first non-blank character of the comment.
16293 @end itemize
16294
16295 @noindent
16296 For an end-of-line comment, if in the original source the next line is a
16297 whole-line comment that starts at the same position
16298 as the end-of-line comment,
16299 then the whole-line comment (and all whole-line comments
16300 that follow it and that start at the same position)
16301 will start at this position in the output file.
16302
16303 @noindent
16304 That is, if in the original source we have:
16305
16306 @smallexample @c ada
16307 @cartouche
16308 begin
16309 A := B + C; -- B must be in the range Low1..High1
16310 -- C must be in the range Low2..High2
16311 --B+C will be in the range Low1+Low2..High1+High2
16312 X := X + 1;
16313 @end cartouche
16314 @end smallexample
16315
16316 @noindent
16317 Then in the formatted source we get
16318
16319 @smallexample @c ada
16320 @cartouche
16321 begin
16322 A := B + C; -- B must be in the range Low1..High1
16323 -- C must be in the range Low2..High2
16324 -- B+C will be in the range Low1+Low2..High1+High2
16325 X := X + 1;
16326 @end cartouche
16327 @end smallexample
16328
16329 @noindent
16330 A comment that exceeds the line length limit will be split.
16331 Unless switch
16332 @option{^-c4^/COMMENTS_LAYOUT=REFORMAT^} (reformat comment blocks) is set and
16333 the line belongs to a reformattable block, splitting the line generates a
16334 @command{gnatpp} warning.
16335 The @option{^-c4^/COMMENTS_LAYOUT=REFORMAT^} switch specifies that whole-line
16336 comments may be reformatted in typical
16337 word processor style (that is, moving words between lines and putting as
16338 many words in a line as possible).
16339
16340 @noindent
16341 The @option{^-c5^/COMMENTS_LAYOUT=KEEP_SPECIAL^} switch specifies, that comments
16342 that has a special format (that is, a character that is neither a letter nor digit
16343 not white space nor line break immediately following the leading @code{--} of
16344 the comment) should be without any change moved from the argument source
16345 into reformatted source. This switch allows to preserve comments that are used
16346 as a special marks in the code (e.g.@: SPARK annotation).
16347
16348 @node Construct Layout
16349 @subsection Construct Layout
16350
16351 @noindent
16352 In several cases the suggested layout in the Ada Reference Manual includes
16353 an extra level of indentation that many programmers prefer to avoid. The
16354 affected cases include:
16355
16356 @itemize @bullet
16357
16358 @item Record type declaration (RM 3.8)
16359
16360 @item Record representation clause (RM 13.5.1)
16361
16362 @item Loop statement in case if a loop has a statement identifier (RM 5.6)
16363
16364 @item Block statement in case if a block has a statement identifier (RM 5.6)
16365 @end itemize
16366
16367 @noindent
16368 In compact mode (when GNAT style layout or compact layout is set),
16369 the pretty printer uses one level of indentation instead
16370 of two. This is achieved in the record definition and record representation
16371 clause cases by putting the @code{record} keyword on the same line as the
16372 start of the declaration or representation clause, and in the block and loop
16373 case by putting the block or loop header on the same line as the statement
16374 identifier.
16375
16376 @noindent
16377 The difference between GNAT style @option{^-l1^/CONSTRUCT_LAYOUT=GNAT^}
16378 and compact @option{^-l2^/CONSTRUCT_LAYOUT=COMPACT^}
16379 layout on the one hand, and uncompact layout
16380 @option{^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^} on the other hand,
16381 can be illustrated by the following examples:
16382
16383 @iftex
16384 @cartouche
16385 @multitable @columnfractions .5 .5
16386 @item @i{GNAT style, compact layout} @tab @i{Uncompact layout}
16387
16388 @item
16389 @smallexample @c ada
16390 type q is record
16391 a : integer;
16392 b : integer;
16393 end record;
16394 @end smallexample
16395 @tab
16396 @smallexample @c ada
16397 type q is
16398 record
16399 a : integer;
16400 b : integer;
16401 end record;
16402 @end smallexample
16403
16404 @item
16405 @smallexample @c ada
16406 for q use record
16407 a at 0 range 0 .. 31;
16408 b at 4 range 0 .. 31;
16409 end record;
16410 @end smallexample
16411 @tab
16412 @smallexample @c ada
16413 for q use
16414 record
16415 a at 0 range 0 .. 31;
16416 b at 4 range 0 .. 31;
16417 end record;
16418 @end smallexample
16419
16420 @item
16421 @smallexample @c ada
16422 Block : declare
16423 A : Integer := 3;
16424 begin
16425 Proc (A, A);
16426 end Block;
16427 @end smallexample
16428 @tab
16429 @smallexample @c ada
16430 Block :
16431 declare
16432 A : Integer := 3;
16433 begin
16434 Proc (A, A);
16435 end Block;
16436 @end smallexample
16437
16438 @item
16439 @smallexample @c ada
16440 Clear : for J in 1 .. 10 loop
16441 A (J) := 0;
16442 end loop Clear;
16443 @end smallexample
16444 @tab
16445 @smallexample @c ada
16446 Clear :
16447 for J in 1 .. 10 loop
16448 A (J) := 0;
16449 end loop Clear;
16450 @end smallexample
16451 @end multitable
16452 @end cartouche
16453 @end iftex
16454
16455 @ifnottex
16456 @smallexample
16457 @cartouche
16458 GNAT style, compact layout Uncompact layout
16459
16460 type q is record type q is
16461 a : integer; record
16462 b : integer; a : integer;
16463 end record; b : integer;
16464 end record;
16465
16466 for q use record for q use
16467 a at 0 range 0 .. 31; record
16468 b at 4 range 0 .. 31; a at 0 range 0 .. 31;
16469 end record; b at 4 range 0 .. 31;
16470 end record;
16471
16472 Block : declare Block :
16473 A : Integer := 3; declare
16474 begin A : Integer := 3;
16475 Proc (A, A); begin
16476 end Block; Proc (A, A);
16477 end Block;
16478
16479 Clear : for J in 1 .. 10 loop Clear :
16480 A (J) := 0; for J in 1 .. 10 loop
16481 end loop Clear; A (J) := 0;
16482 end loop Clear;
16483 @end cartouche
16484 @end smallexample
16485 @end ifnottex
16486
16487 @noindent
16488 A further difference between GNAT style layout and compact layout is that
16489 GNAT style layout inserts empty lines as separation for
16490 compound statements, return statements and bodies.
16491
16492 Note that the layout specified by
16493 @option{^--separate-stmt-name^/STMT_NAME_ON_NEW_LINE^}
16494 for named block and loop statements overrides the layout defined by these
16495 constructs by @option{^-l1^/CONSTRUCT_LAYOUT=GNAT^},
16496 @option{^-l2^/CONSTRUCT_LAYOUT=COMPACT^} or
16497 @option{^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^} option.
16498
16499 @node Name Casing
16500 @subsection Name Casing
16501
16502 @noindent
16503 @command{gnatpp} always converts the usage occurrence of a (simple) name to
16504 the same casing as the corresponding defining identifier.
16505
16506 You control the casing for defining occurrences via the
16507 @option{^-n^/NAME_CASING^} switch.
16508 @ifclear vms
16509 With @option{-nD} (``as declared'', which is the default),
16510 @end ifclear
16511 @ifset vms
16512 With @option{/NAME_CASING=AS_DECLARED}, which is the default,
16513 @end ifset
16514 defining occurrences appear exactly as in the source file
16515 where they are declared.
16516 The other ^values for this switch^options for this qualifier^ ---
16517 @option{^-nU^UPPER_CASE^},
16518 @option{^-nL^LOWER_CASE^},
16519 @option{^-nM^MIXED_CASE^} ---
16520 result in
16521 ^upper, lower, or mixed case, respectively^the corresponding casing^.
16522 If @command{gnatpp} changes the casing of a defining
16523 occurrence, it analogously changes the casing of all the
16524 usage occurrences of this name.
16525
16526 If the defining occurrence of a name is not in the source compilation unit
16527 currently being processed by @command{gnatpp}, the casing of each reference to
16528 this name is changed according to the value of the @option{^-n^/NAME_CASING^}
16529 switch (subject to the dictionary file mechanism described below).
16530 Thus @command{gnatpp} acts as though the @option{^-n^/NAME_CASING^} switch
16531 had affected the
16532 casing for the defining occurrence of the name.
16533
16534 Some names may need to be spelled with casing conventions that are not
16535 covered by the upper-, lower-, and mixed-case transformations.
16536 You can arrange correct casing by placing such names in a
16537 @emph{dictionary file},
16538 and then supplying a @option{^-D^/DICTIONARY^} switch.
16539 The casing of names from dictionary files overrides
16540 any @option{^-n^/NAME_CASING^} switch.
16541
16542 To handle the casing of Ada predefined names and the names from GNAT libraries,
16543 @command{gnatpp} assumes a default dictionary file.
16544 The name of each predefined entity is spelled with the same casing as is used
16545 for the entity in the @cite{Ada Reference Manual}.
16546 The name of each entity in the GNAT libraries is spelled with the same casing
16547 as is used in the declaration of that entity.
16548
16549 The @w{@option{^-D-^/SPECIFIC_CASING^}} switch suppresses the use of the
16550 default dictionary file.
16551 Instead, the casing for predefined and GNAT-defined names will be established
16552 by the @option{^-n^/NAME_CASING^} switch or explicit dictionary files.
16553 For example, by default the names @code{Ada.Text_IO} and @code{GNAT.OS_Lib}
16554 will appear as just shown,
16555 even in the presence of a @option{^-nU^/NAME_CASING=UPPER_CASE^} switch.
16556 To ensure that even such names are rendered in uppercase,
16557 additionally supply the @w{@option{^-D-^/SPECIFIC_CASING^}} switch
16558 (or else, less conveniently, place these names in upper case in a dictionary
16559 file).
16560
16561 A dictionary file is
16562 a plain text file; each line in this file can be either a blank line
16563 (containing only space characters and ASCII.HT characters), an Ada comment
16564 line, or the specification of exactly one @emph{casing schema}.
16565
16566 A casing schema is a string that has the following syntax:
16567
16568 @smallexample
16569 @cartouche
16570 @var{casing_schema} ::= @var{identifier} | *@var{simple_identifier}*
16571
16572 @var{simple_identifier} ::= @var{letter}@{@var{letter_or_digit}@}
16573 @end cartouche
16574 @end smallexample
16575
16576 @noindent
16577 (See @cite{Ada Reference Manual}, Section 2.3) for the definition of the
16578 @var{identifier} lexical element and the @var{letter_or_digit} category.)
16579
16580 The casing schema string can be followed by white space and/or an Ada-style
16581 comment; any amount of white space is allowed before the string.
16582
16583 If a dictionary file is passed as
16584 @ifclear vms
16585 the value of a @option{-D@var{file}} switch
16586 @end ifclear
16587 @ifset vms
16588 an option to the @option{/DICTIONARY} qualifier
16589 @end ifset
16590 then for every
16591 simple name and every identifier, @command{gnatpp} checks if the dictionary
16592 defines the casing for the name or for some of its parts (the term ``subword''
16593 is used below to denote the part of a name which is delimited by ``_'' or by
16594 the beginning or end of the word and which does not contain any ``_'' inside):
16595
16596 @itemize @bullet
16597 @item
16598 if the whole name is in the dictionary, @command{gnatpp} uses for this name
16599 the casing defined by the dictionary; no subwords are checked for this word
16600
16601 @item
16602 for every subword @command{gnatpp} checks if the dictionary contains the
16603 corresponding string of the form @code{*@var{simple_identifier}*},
16604 and if it does, the casing of this @var{simple_identifier} is used
16605 for this subword
16606
16607 @item
16608 if the whole name does not contain any ``_'' inside, and if for this name
16609 the dictionary contains two entries - one of the form @var{identifier},
16610 and another - of the form *@var{simple_identifier}*, then the first one
16611 is applied to define the casing of this name
16612
16613 @item
16614 if more than one dictionary file is passed as @command{gnatpp} switches, each
16615 dictionary adds new casing exceptions and overrides all the existing casing
16616 exceptions set by the previous dictionaries
16617
16618 @item
16619 when @command{gnatpp} checks if the word or subword is in the dictionary,
16620 this check is not case sensitive
16621 @end itemize
16622
16623 @noindent
16624 For example, suppose we have the following source to reformat:
16625
16626 @smallexample @c ada
16627 @cartouche
16628 procedure test is
16629 name1 : integer := 1;
16630 name4_name3_name2 : integer := 2;
16631 name2_name3_name4 : Boolean;
16632 name1_var : Float;
16633 begin
16634 name2_name3_name4 := name4_name3_name2 > name1;
16635 end;
16636 @end cartouche
16637 @end smallexample
16638
16639 @noindent
16640 And suppose we have two dictionaries:
16641
16642 @smallexample
16643 @cartouche
16644 @i{dict1:}
16645 NAME1
16646 *NaMe3*
16647 *Name1*
16648 @end cartouche
16649
16650 @cartouche
16651 @i{dict2:}
16652 *NAME3*
16653 @end cartouche
16654 @end smallexample
16655
16656 @noindent
16657 If @command{gnatpp} is called with the following switches:
16658
16659 @smallexample
16660 @ifclear vms
16661 @command{gnatpp -nM -D dict1 -D dict2 test.adb}
16662 @end ifclear
16663 @ifset vms
16664 @command{gnatpp test.adb /NAME_CASING=MIXED_CASE /DICTIONARY=(dict1, dict2)}
16665 @end ifset
16666 @end smallexample
16667
16668 @noindent
16669 then we will get the following name casing in the @command{gnatpp} output:
16670
16671 @smallexample @c ada
16672 @cartouche
16673 procedure Test is
16674 NAME1 : Integer := 1;
16675 Name4_NAME3_Name2 : Integer := 2;
16676 Name2_NAME3_Name4 : Boolean;
16677 Name1_Var : Float;
16678 begin
16679 Name2_NAME3_Name4 := Name4_NAME3_Name2 > NAME1;
16680 end Test;
16681 @end cartouche
16682 @end smallexample
16683
16684 @c *********************************
16685 @node The GNAT Metric Tool gnatmetric
16686 @chapter The GNAT Metric Tool @command{gnatmetric}
16687 @findex gnatmetric
16688 @cindex Metric tool
16689
16690 @noindent
16691 ^The @command{gnatmetric} tool^@command{GNAT METRIC}^ is an ASIS-based utility
16692 for computing various program metrics.
16693 It takes an Ada source file as input and generates a file containing the
16694 metrics data as output. Various switches control which
16695 metrics are computed and output.
16696
16697 @command{gnatmetric} generates and uses the ASIS
16698 tree for the input source and thus requires the input to be syntactically and
16699 semantically legal.
16700 If this condition is not met, @command{gnatmetric} will generate
16701 an error message; no metric information for this file will be
16702 computed and reported.
16703
16704 If the compilation unit contained in the input source depends semantically
16705 upon units in files located outside the current directory, you have to provide
16706 the source search path when invoking @command{gnatmetric}.
16707 If it depends semantically upon units that are contained
16708 in files with names that do not follow the GNAT file naming rules, you have to
16709 provide the configuration file describing the corresponding naming scheme (see
16710 the description of the @command{gnatmetric} switches below.)
16711 Alternatively, you may use a project file and invoke @command{gnatmetric}
16712 through the @command{gnat} driver.
16713
16714 The @command{gnatmetric} command has the form
16715
16716 @smallexample
16717 $ gnatmetric @ovar{switches} @{@var{filename}@} @r{[}-cargs @var{gcc_switches}@r{]}
16718 @end smallexample
16719
16720 @noindent
16721 where
16722 @itemize @bullet
16723 @item
16724 @var{switches} specify the metrics to compute and define the destination for
16725 the output
16726
16727 @item
16728 Each @var{filename} is the name (including the extension) of a source
16729 file to process. ``Wildcards'' are allowed, and
16730 the file name may contain path information.
16731 If no @var{filename} is supplied, then the @var{switches} list must contain
16732 at least one
16733 @option{-files} switch (@pxref{Other gnatmetric Switches}).
16734 Including both a @option{-files} switch and one or more
16735 @var{filename} arguments is permitted.
16736
16737 @item
16738 @samp{-cargs @var{gcc_switches}} is a list of switches for
16739 @command{gcc}. They will be passed on to all compiler invocations made by
16740 @command{gnatmetric} to generate the ASIS trees. Here you can provide
16741 @option{^-I^/INCLUDE_DIRS=^} switches to form the source search path,
16742 and use the @option{-gnatec} switch to set the configuration file.
16743 @end itemize
16744
16745 @menu
16746 * Switches for gnatmetric::
16747 @end menu
16748
16749 @node Switches for gnatmetric
16750 @section Switches for @command{gnatmetric}
16751
16752 @noindent
16753 The following subsections describe the various switches accepted by
16754 @command{gnatmetric}, organized by category.
16755
16756 @menu
16757 * Output Files Control::
16758 * Disable Metrics For Local Units::
16759 * Specifying a set of metrics to compute::
16760 * Other gnatmetric Switches::
16761 * Generate project-wide metrics::
16762 @end menu
16763
16764 @node Output Files Control
16765 @subsection Output File Control
16766 @cindex Output file control in @command{gnatmetric}
16767
16768 @noindent
16769 @command{gnatmetric} has two output formats. It can generate a
16770 textual (human-readable) form, and also XML. By default only textual
16771 output is generated.
16772
16773 When generating the output in textual form, @command{gnatmetric} creates
16774 for each Ada source file a corresponding text file
16775 containing the computed metrics, except for the case when the set of metrics
16776 specified by gnatmetric parameters consists only of metrics that are computed
16777 for the whole set of analyzed sources, but not for each Ada source.
16778 By default, this file is placed in the same directory as where the source
16779 file is located, and its name is obtained
16780 by appending the ^@file{.metrix}^@file{$METRIX}^ suffix to the name of the
16781 input file.
16782
16783 All the output information generated in XML format is placed in a single
16784 file. By default this file is placed in the current directory and has the
16785 name ^@file{metrix.xml}^@file{METRIX$XML}^.
16786
16787 Some of the computed metrics are summed over the units passed to
16788 @command{gnatmetric}; for example, the total number of lines of code.
16789 By default this information is sent to @file{stdout}, but a file
16790 can be specified with the @option{-og} switch.
16791
16792 The following switches control the @command{gnatmetric} output:
16793
16794 @table @option
16795 @cindex @option{^-x^/XML^} (@command{gnatmetric})
16796 @item ^-x^/XML^
16797 Generate the XML output
16798
16799 @cindex @option{^-nt^/NO_TEXT^} (@command{gnatmetric})
16800 @item ^-nt^/NO_TEXT^
16801 Do not generate the output in text form (implies @option{^-x^/XML^})
16802
16803 @cindex @option{^-d^/DIRECTORY^} (@command{gnatmetric})
16804 @item ^-d @var{output_dir}^/DIRECTORY=@var{output_dir}^
16805 Put textual files with detailed metrics into @var{output_dir}
16806
16807 @cindex @option{^-o^/SUFFIX_DETAILS^} (@command{gnatmetric})
16808 @item ^-o @var{file_suffix}^/SUFFIX_DETAILS=@var{file_suffix}^
16809 Use @var{file_suffix}, instead of ^@file{.metrix}^@file{$METRIX}^
16810 in the name of the output file.
16811
16812 @cindex @option{^-og^/GLOBAL_OUTPUT^} (@command{gnatmetric})
16813 @item ^-og @var{file_name}^/GLOBAL_OUTPUT=@var{file_name}^
16814 Put global metrics into @var{file_name}
16815
16816 @cindex @option{^-ox^/XML_OUTPUT^} (@command{gnatmetric})
16817 @item ^-ox @var{file_name}^/XML_OUTPUT=@var{file_name}^
16818 Put the XML output into @var{file_name} (also implies @option{^-x^/XML^})
16819
16820 @cindex @option{^-sfn^/SHORT_SOURCE_FILE_NAME^} (@command{gnatmetric})
16821 @item ^-sfn^/SHORT_SOURCE_FILE_NAME^
16822 Use ``short'' source file names in the output. (The @command{gnatmetric}
16823 output includes the name(s) of the Ada source file(s) from which the metrics
16824 are computed. By default each name includes the absolute path. The
16825 @option{^-sfn^/SHORT_SOURCE_FILE_NAME^} switch causes @command{gnatmetric}
16826 to exclude all directory information from the file names that are output.)
16827
16828 @end table
16829
16830 @node Disable Metrics For Local Units
16831 @subsection Disable Metrics For Local Units
16832 @cindex Disable Metrics For Local Units in @command{gnatmetric}
16833
16834 @noindent
16835 @command{gnatmetric} relies on the GNAT compilation model @minus{}
16836 one compilation
16837 unit per one source file. It computes line metrics for the whole source
16838 file, and it also computes syntax
16839 and complexity metrics for the file's outermost unit.
16840
16841 By default, @command{gnatmetric} will also compute all metrics for certain
16842 kinds of locally declared program units:
16843
16844 @itemize @bullet
16845 @item
16846 subprogram (and generic subprogram) bodies;
16847
16848 @item
16849 package (and generic package) specs and bodies;
16850
16851 @item
16852 task object and type specifications and bodies;
16853
16854 @item
16855 protected object and type specifications and bodies.
16856 @end itemize
16857
16858 @noindent
16859 These kinds of entities will be referred to as
16860 @emph{eligible local program units}, or simply @emph{eligible local units},
16861 @cindex Eligible local unit (for @command{gnatmetric})
16862 in the discussion below.
16863
16864 Note that a subprogram declaration, generic instantiation,
16865 or renaming declaration only receives metrics
16866 computation when it appear as the outermost entity
16867 in a source file.
16868
16869 Suppression of metrics computation for eligible local units can be
16870 obtained via the following switch:
16871
16872 @table @option
16873 @cindex @option{^-n@var{x}^/SUPPRESS^} (@command{gnatmetric})
16874 @item ^-nolocal^/SUPPRESS=LOCAL_DETAILS^
16875 Do not compute detailed metrics for eligible local program units
16876
16877 @end table
16878
16879 @node Specifying a set of metrics to compute
16880 @subsection Specifying a set of metrics to compute
16881
16882 @noindent
16883 By default all the metrics are computed and reported. The switches
16884 described in this subsection allow you to control, on an individual
16885 basis, whether metrics are computed and
16886 reported. If at least one positive metric
16887 switch is specified (that is, a switch that defines that a given
16888 metric or set of metrics is to be computed), then only
16889 explicitly specified metrics are reported.
16890
16891 @menu
16892 * Line Metrics Control::
16893 * Syntax Metrics Control::
16894 * Complexity Metrics Control::
16895 * Object-Oriented Metrics Control::
16896 @end menu
16897
16898 @node Line Metrics Control
16899 @subsubsection Line Metrics Control
16900 @cindex Line metrics control in @command{gnatmetric}
16901
16902 @noindent
16903 For any (legal) source file, and for each of its
16904 eligible local program units, @command{gnatmetric} computes the following
16905 metrics:
16906
16907 @itemize @bullet
16908 @item
16909 the total number of lines;
16910
16911 @item
16912 the total number of code lines (i.e., non-blank lines that are not comments)
16913
16914 @item
16915 the number of comment lines
16916
16917 @item
16918 the number of code lines containing end-of-line comments;
16919
16920 @item
16921 the comment percentage: the ratio between the number of lines that contain
16922 comments and the number of all non-blank lines, expressed as a percentage;
16923
16924 @item
16925 the number of empty lines and lines containing only space characters and/or
16926 format effectors (blank lines)
16927
16928 @item
16929 the average number of code lines in subprogram bodies, task bodies, entry
16930 bodies and statement sequences in package bodies (this metric is only computed
16931 across the whole set of the analyzed units)
16932
16933 @end itemize
16934
16935 @noindent
16936 @command{gnatmetric} sums the values of the line metrics for all the
16937 files being processed and then generates the cumulative results. The tool
16938 also computes for all the files being processed the average number of code
16939 lines in bodies.
16940
16941 You can use the following switches to select the specific line metrics
16942 to be computed and reported.
16943
16944 @table @option
16945 @cindex @option{^--lines@var{x}^/LINE_COUNT_METRICS^} (@command{gnatmetric})
16946
16947 @ifclear vms
16948 @cindex @option{--no-lines@var{x}}
16949 @end ifclear
16950
16951 @item ^--lines-all^/LINE_COUNT_METRICS=ALL_ON^
16952 Report all the line metrics
16953
16954 @item ^--no-lines-all^/LINE_COUNT_METRICS=ALL_OFF^
16955 Do not report any of line metrics
16956
16957 @item ^--lines^/LINE_COUNT_METRICS=ALL_LINES_ON^
16958 Report the number of all lines
16959
16960 @item ^--no-lines^/LINE_COUNT_METRICS=ALL_LINES_OFF^
16961 Do not report the number of all lines
16962
16963 @item ^--lines-code^/LINE_COUNT_METRICS=CODE_LINES_ON^
16964 Report the number of code lines
16965
16966 @item ^--no-lines-code^/LINE_COUNT_METRICS=CODE_LINES_OFF^
16967 Do not report the number of code lines
16968
16969 @item ^--lines-comment^/LINE_COUNT_METRICS=COMMENT_LINES_ON^
16970 Report the number of comment lines
16971
16972 @item ^--no-lines-comment^/LINE_COUNT_METRICS=COMMENT_LINES_OFF^
16973 Do not report the number of comment lines
16974
16975 @item ^--lines-eol-comment^/LINE_COUNT_METRICS=CODE_COMMENT_LINES_ON^
16976 Report the number of code lines containing
16977 end-of-line comments
16978
16979 @item ^--no-lines-eol-comment^/LINE_COUNT_METRICS=CODE_COMMENT_LINES_OFF^
16980 Do not report the number of code lines containing
16981 end-of-line comments
16982
16983 @item ^--lines-ratio^/LINE_COUNT_METRICS=COMMENT_PERCENTAGE_ON^
16984 Report the comment percentage in the program text
16985
16986 @item ^--no-lines-ratio^/LINE_COUNT_METRICS=COMMENT_PERCENTAGE_OFF^
16987 Do not report the comment percentage in the program text
16988
16989 @item ^--lines-blank^/LINE_COUNT_METRICS=BLANK_LINES_ON^
16990 Report the number of blank lines
16991
16992 @item ^--no-lines-blank^/LINE_COUNT_METRICS=BLANK_LINES_OFF^
16993 Do not report the number of blank lines
16994
16995 @item ^--lines-average^/LINE_COUNT_METRICS=AVERAGE_BODY_LINES_ON^
16996 Report the average number of code lines in subprogram bodies, task bodies,
16997 entry bodies and statement sequences in package bodies. The metric is computed
16998 and reported for the whole set of processed Ada sources only.
16999
17000 @item ^--no-lines-average^/LINE_COUNT_METRICS=AVERAGE_BODY_LINES_OFF^
17001 Do not report the average number of code lines in subprogram bodies,
17002 task bodies, entry bodies and statement sequences in package bodies.
17003
17004 @end table
17005
17006 @node Syntax Metrics Control
17007 @subsubsection Syntax Metrics Control
17008 @cindex Syntax metrics control in @command{gnatmetric}
17009
17010 @noindent
17011 @command{gnatmetric} computes various syntactic metrics for the
17012 outermost unit and for each eligible local unit:
17013
17014 @table @emph
17015 @item LSLOC (``Logical Source Lines Of Code'')
17016 The total number of declarations and the total number of statements
17017
17018 @item Maximal static nesting level of inner program units
17019 According to
17020 @cite{Ada Reference Manual}, 10.1(1), ``A program unit is either a
17021 package, a task unit, a protected unit, a
17022 protected entry, a generic unit, or an explicitly declared subprogram other
17023 than an enumeration literal.''
17024
17025 @item Maximal nesting level of composite syntactic constructs
17026 This corresponds to the notion of the
17027 maximum nesting level in the GNAT built-in style checks
17028 (@pxref{Style Checking})
17029 @end table
17030
17031 @noindent
17032 For the outermost unit in the file, @command{gnatmetric} additionally computes
17033 the following metrics:
17034
17035 @table @emph
17036 @item Public subprograms
17037 This metric is computed for package specs. It is the
17038 number of subprograms and generic subprograms declared in the visible
17039 part (including the visible part of nested packages, protected objects, and
17040 protected types).
17041
17042 @item All subprograms
17043 This metric is computed for bodies and subunits. The
17044 metric is equal to a total number of subprogram bodies in the compilation
17045 unit.
17046 Neither generic instantiations nor renamings-as-a-body nor body stubs
17047 are counted. Any subprogram body is counted, independently of its nesting
17048 level and enclosing constructs. Generic bodies and bodies of protected
17049 subprograms are counted in the same way as ``usual'' subprogram bodies.
17050
17051 @item Public types
17052 This metric is computed for package specs and
17053 generic package declarations. It is the total number of types
17054 that can be referenced from outside this compilation unit, plus the
17055 number of types from all the visible parts of all the visible generic
17056 packages. Generic formal types are not counted. Only types, not subtypes,
17057 are included.
17058
17059 @noindent
17060 Along with the total number of public types, the following
17061 types are counted and reported separately:
17062
17063 @itemize @bullet
17064 @item
17065 Abstract types
17066
17067 @item
17068 Root tagged types (abstract, non-abstract, private, non-private). Type
17069 extensions are @emph{not} counted
17070
17071 @item
17072 Private types (including private extensions)
17073
17074 @item
17075 Task types
17076
17077 @item
17078 Protected types
17079
17080 @end itemize
17081
17082 @item All types
17083 This metric is computed for any compilation unit. It is equal to the total
17084 number of the declarations of different types given in the compilation unit.
17085 The private and the corresponding full type declaration are counted as one
17086 type declaration. Incomplete type declarations and generic formal types
17087 are not counted.
17088 No distinction is made among different kinds of types (abstract,
17089 private etc.); the total number of types is computed and reported.
17090
17091 @end table
17092
17093 @noindent
17094 By default, all the syntax metrics are computed and reported. You can use the
17095 following switches to select specific syntax metrics.
17096
17097 @table @option
17098
17099 @cindex @option{^--syntax@var{x}^/SYNTAX_METRICS^} (@command{gnatmetric})
17100
17101 @ifclear vms
17102 @cindex @option{--no-syntax@var{x}} (@command{gnatmetric})
17103 @end ifclear
17104
17105 @item ^--syntax-all^/SYNTAX_METRICS=ALL_ON^
17106 Report all the syntax metrics
17107
17108 @item ^--no-syntax-all^/ALL_OFF^
17109 Do not report any of syntax metrics
17110
17111 @item ^--declarations^/SYNTAX_METRICS=DECLARATIONS_ON^
17112 Report the total number of declarations
17113
17114 @item ^--no-declarations^/SYNTAX_METRICS=DECLARATIONS_OFF^
17115 Do not report the total number of declarations
17116
17117 @item ^--statements^/SYNTAX_METRICS=STATEMENTS_ON^
17118 Report the total number of statements
17119
17120 @item ^--no-statements^/SYNTAX_METRICS=STATEMENTS_OFF^
17121 Do not report the total number of statements
17122
17123 @item ^--public-subprograms^/SYNTAX_METRICS=PUBLIC_SUBPROGRAMS_ON^
17124 Report the number of public subprograms in a compilation unit
17125
17126 @item ^--no-public-subprograms^/SYNTAX_METRICS=PUBLIC_SUBPROGRAMS_OFF^
17127 Do not report the number of public subprograms in a compilation unit
17128
17129 @item ^--all-subprograms^/SYNTAX_METRICS=ALL_SUBPROGRAMS_ON^
17130 Report the number of all the subprograms in a compilation unit
17131
17132 @item ^--no-all-subprograms^/SYNTAX_METRICS=ALL_SUBPROGRAMS_OFF^
17133 Do not report the number of all the subprograms in a compilation unit
17134
17135 @item ^--public-types^/SYNTAX_METRICS=PUBLIC_TYPES_ON^
17136 Report the number of public types in a compilation unit
17137
17138 @item ^--no-public-types^/SYNTAX_METRICS=PUBLIC_TYPES_OFF^
17139 Do not report the number of public types in a compilation unit
17140
17141 @item ^--all-types^/SYNTAX_METRICS=ALL_TYPES_ON^
17142 Report the number of all the types in a compilation unit
17143
17144 @item ^--no-all-types^/SYNTAX_METRICS=ALL_TYPES_OFF^
17145 Do not report the number of all the types in a compilation unit
17146
17147 @item ^--unit-nesting^/SYNTAX_METRICS=UNIT_NESTING_ON^
17148 Report the maximal program unit nesting level
17149
17150 @item ^--no-unit-nesting^/SYNTAX_METRICS=UNIT_NESTING_OFF^
17151 Do not report the maximal program unit nesting level
17152
17153 @item ^--construct-nesting^/SYNTAX_METRICS=CONSTRUCT_NESTING_ON^
17154 Report the maximal construct nesting level
17155
17156 @item ^--no-construct-nesting^/SYNTAX_METRICS=CONSTRUCT_NESTING_OFF^
17157 Do not report the maximal construct nesting level
17158
17159 @end table
17160
17161 @node Complexity Metrics Control
17162 @subsubsection Complexity Metrics Control
17163 @cindex Complexity metrics control in @command{gnatmetric}
17164
17165 @noindent
17166 For a program unit that is an executable body (a subprogram body (including
17167 generic bodies), task body, entry body or a package body containing
17168 its own statement sequence) @command{gnatmetric} computes the following
17169 complexity metrics:
17170
17171 @itemize @bullet
17172 @item
17173 McCabe cyclomatic complexity;
17174
17175 @item
17176 McCabe essential complexity;
17177
17178 @item
17179 maximal loop nesting level
17180
17181 @end itemize
17182
17183 @noindent
17184 The McCabe complexity metrics are defined
17185 in @url{http://www.mccabe.com/pdf/nist235r.pdf}
17186
17187 According to McCabe, both control statements and short-circuit control forms
17188 should be taken into account when computing cyclomatic complexity. For each
17189 body, we compute three metric values:
17190
17191 @itemize @bullet
17192 @item
17193 the complexity introduced by control
17194 statements only, without taking into account short-circuit forms,
17195
17196 @item
17197 the complexity introduced by short-circuit control forms only, and
17198
17199 @item
17200 the total
17201 cyclomatic complexity, which is the sum of these two values.
17202 @end itemize
17203
17204 @noindent
17205 When computing cyclomatic and essential complexity, @command{gnatmetric} skips
17206 the code in the exception handlers and in all the nested program units.
17207
17208 By default, all the complexity metrics are computed and reported.
17209 For more fine-grained control you can use
17210 the following switches:
17211
17212 @table @option
17213 @cindex @option{^-complexity@var{x}^/COMPLEXITY_METRICS^} (@command{gnatmetric})
17214
17215 @ifclear vms
17216 @cindex @option{--no-complexity@var{x}}
17217 @end ifclear
17218
17219 @item ^--complexity-all^/COMPLEXITY_METRICS=ALL_ON^
17220 Report all the complexity metrics
17221
17222 @item ^--no-complexity-all^/COMPLEXITY_METRICS=ALL_OFF^
17223 Do not report any of complexity metrics
17224
17225 @item ^--complexity-cyclomatic^/COMPLEXITY_METRICS=CYCLOMATIC_ON^
17226 Report the McCabe Cyclomatic Complexity
17227
17228 @item ^--no-complexity-cyclomatic^/COMPLEXITY_METRICS=CYCLOMATIC_OFF^
17229 Do not report the McCabe Cyclomatic Complexity
17230
17231 @item ^--complexity-essential^/COMPLEXITY_METRICS=ESSENTIAL_ON^
17232 Report the Essential Complexity
17233
17234 @item ^--no-complexity-essential^/COMPLEXITY_METRICS=ESSENTIAL_OFF^
17235 Do not report the Essential Complexity
17236
17237 @item ^--loop-nesting^/COMPLEXITY_METRICS=LOOP_NESTING_ON^
17238 Report maximal loop nesting level
17239
17240 @item ^--no-loop-nesting^/COMPLEXITY_METRICS=LOOP_NESTING_OFF^
17241 Do not report maximal loop nesting level
17242
17243 @item ^--complexity-average^/COMPLEXITY_METRICS=AVERAGE_COMPLEXITY_ON^
17244 Report the average McCabe Cyclomatic Complexity for all the subprogram bodies,
17245 task bodies, entry bodies and statement sequences in package bodies.
17246 The metric is computed and reported for whole set of processed Ada sources
17247 only.
17248
17249 @item ^--no-complexity-average^/COMPLEXITY_METRICS=AVERAGE_COMPLEXITY_OFF^
17250 Do not report the average McCabe Cyclomatic Complexity for all the subprogram
17251 bodies, task bodies, entry bodies and statement sequences in package bodies
17252
17253 @cindex @option{^-ne^/NO_EXITS_AS_GOTOS^} (@command{gnatmetric})
17254 @item ^-ne^/NO_EXITS_AS_GOTOS^
17255 Do not consider @code{exit} statements as @code{goto}s when
17256 computing Essential Complexity
17257
17258 @end table
17259
17260
17261 @node Object-Oriented Metrics Control
17262 @subsubsection Object-Oriented Metrics Control
17263 @cindex Object-Oriented metrics control in @command{gnatmetric}
17264
17265 @noindent
17266 @cindex Coupling metrics (in in @command{gnatmetric})
17267 Coupling metrics are object-oriented metrics that measure the
17268 dependencies between a given class (or a group of classes) and the
17269 ``external world'' (that is, the other classes in the program). In this
17270 subsection the term ``class'' is used in its
17271 traditional object-oriented programming sense
17272 (an instantiable module that contains data and/or method members).
17273 A @emph{category} (of classes)
17274 is a group of closely related classes that are reused and/or
17275 modified together.
17276
17277 A class @code{K}'s @emph{efferent coupling} is the number of classes
17278 that @code{K} depends upon.
17279 A category's efferent coupling is the number of classes outside the
17280 category that the classes inside the category depend upon.
17281
17282 A class @code{K}'s @emph{afferent coupling} is the number of classes
17283 that depend upon @code{K}.
17284 A category's afferent coupling is the number of classes outside the
17285 category that depend on classes belonging to the category.
17286
17287 Ada's implementation of the object-oriented paradigm does not use the
17288 traditional class notion, so the definition of the coupling
17289 metrics for Ada maps the class and class category notions
17290 onto Ada constructs.
17291
17292 For the coupling metrics, several kinds of modules -- a library package,
17293 a library generic package, and a library generic package instantiation --
17294 that define a tagged type or an interface type are
17295 considered to be a class. A category consists of a library package (or
17296 a library generic package) that defines a tagged or an interface type,
17297 together with all its descendant (generic) packages that define tagged
17298 or interface types. For any package counted as a class,
17299 its body (if any) is considered
17300 together with its spec when counting the dependencies. For dependencies
17301 between classes, the Ada semantic dependencies are considered.
17302 For coupling metrics, only dependencies on units that are considered as
17303 classes, are considered.
17304
17305 When computing coupling metrics, @command{gnatmetric} counts only
17306 dependencies between units that are arguments of the gnatmetric call.
17307 Coupling metrics are program-wide (or project-wide) metrics, so to
17308 get a valid result, you should call @command{gnatmetric} for
17309 the whole set of sources that make up your program. It can be done
17310 by calling @command{gnatmetric} from the GNAT driver with @option{-U}
17311 option (see See @ref{The GNAT Driver and Project Files} for details.
17312
17313 By default, all the coupling metrics are disabled. You can use the following
17314 switches to specify the coupling metrics to be computed and reported:
17315
17316 @table @option
17317
17318 @ifclear vms
17319 @cindex @option{--package@var{x}} (@command{gnatmetric})
17320 @cindex @option{--no-package@var{x}} (@command{gnatmetric})
17321 @cindex @option{--category@var{x}} (@command{gnatmetric})
17322 @cindex @option{--no-category@var{x}} (@command{gnatmetric})
17323 @end ifclear
17324
17325 @ifset vms
17326 @cindex @option{/COUPLING_METRICS} (@command{gnatmetric})
17327 @end ifset
17328
17329 @item ^--coupling-all^/COUPLING_METRICS=ALL_ON^
17330 Report all the coupling metrics
17331
17332 @item ^--no-coupling-all^/COUPLING_METRICS=ALL_OFF^
17333 Do not report any of metrics
17334
17335 @item ^--package-efferent-coupling^/COUPLING_METRICS=PACKAGE_EFFERENT_ON^
17336 Report package efferent coupling
17337
17338 @item ^--no-package-efferent-coupling^/COUPLING_METRICS=PACKAGE_EFFERENT_OFF^
17339 Do not report package efferent coupling
17340
17341 @item ^--package-afferent-coupling^/COUPLING_METRICS=PACKAGE_AFFERENT_ON^
17342 Report package afferent coupling
17343
17344 @item ^--no-package-afferent-coupling^/COUPLING_METRICS=PACKAGE_AFFERENT_OFF^
17345 Do not report package afferent coupling
17346
17347 @item ^--category-efferent-coupling^/COUPLING_METRICS=CATEGORY_EFFERENT_ON^
17348 Report category efferent coupling
17349
17350 @item ^--no-category-efferent-coupling^/COUPLING_METRICS=CATEGORY_EFFERENT_OFF^
17351 Do not report category efferent coupling
17352
17353 @item ^--category-afferent-coupling^/COUPLING_METRICS=CATEGORY_AFFERENT_ON^
17354 Report category afferent coupling
17355
17356 @item ^--no-category-afferent-coupling^/COUPLING_METRICS=CATEGORY_AFFERENT_OFF^
17357 Do not report category afferent coupling
17358
17359 @end table
17360
17361 @node Other gnatmetric Switches
17362 @subsection Other @code{gnatmetric} Switches
17363
17364 @noindent
17365 Additional @command{gnatmetric} switches are as follows:
17366
17367 @table @option
17368 @item ^-files @var{filename}^/FILES=@var{filename}^
17369 @cindex @option{^-files^/FILES^} (@code{gnatmetric})
17370 Take the argument source files from the specified file. This file should be an
17371 ordinary text file containing file names separated by spaces or
17372 line breaks. You can use this switch more then once in the same call to
17373 @command{gnatmetric}. You also can combine this switch with
17374 an explicit list of files.
17375
17376 @item ^-v^/VERBOSE^
17377 @cindex @option{^-v^/VERBOSE^} (@code{gnatmetric})
17378 Verbose mode;
17379 @command{gnatmetric} generates version information and then
17380 a trace of sources being processed.
17381
17382 @item ^-dv^/DEBUG_OUTPUT^
17383 @cindex @option{^-dv^/DEBUG_OUTPUT^} (@code{gnatmetric})
17384 Debug mode;
17385 @command{gnatmetric} generates various messages useful to understand what
17386 happens during the metrics computation
17387
17388 @item ^-q^/QUIET^
17389 @cindex @option{^-q^/QUIET^} (@code{gnatmetric})
17390 Quiet mode.
17391 @end table
17392
17393 @node Generate project-wide metrics
17394 @subsection Generate project-wide metrics
17395
17396 In order to compute metrics on all units of a given project, you can use
17397 the @command{gnat} driver along with the @option{-P} option:
17398 @smallexample
17399 gnat metric -Pproj
17400 @end smallexample
17401
17402 @noindent
17403 If the project @code{proj} depends upon other projects, you can compute
17404 the metrics on the project closure using the @option{-U} option:
17405 @smallexample
17406 gnat metric -Pproj -U
17407 @end smallexample
17408
17409 @noindent
17410 Finally, if not all the units are relevant to a particular main
17411 program in the project closure, you can generate metrics for the set
17412 of units needed to create a given main program (unit closure) using
17413 the @option{-U} option followed by the name of the main unit:
17414 @smallexample
17415 gnat metric -Pproj -U main
17416 @end smallexample
17417
17418
17419 @c ***********************************
17420 @node File Name Krunching Using gnatkr
17421 @chapter File Name Krunching Using @code{gnatkr}
17422 @findex gnatkr
17423
17424 @noindent
17425 This chapter discusses the method used by the compiler to shorten
17426 the default file names chosen for Ada units so that they do not
17427 exceed the maximum length permitted. It also describes the
17428 @code{gnatkr} utility that can be used to determine the result of
17429 applying this shortening.
17430 @menu
17431 * About gnatkr::
17432 * Using gnatkr::
17433 * Krunching Method::
17434 * Examples of gnatkr Usage::
17435 @end menu
17436
17437 @node About gnatkr
17438 @section About @code{gnatkr}
17439
17440 @noindent
17441 The default file naming rule in GNAT
17442 is that the file name must be derived from
17443 the unit name. The exact default rule is as follows:
17444 @itemize @bullet
17445 @item
17446 Take the unit name and replace all dots by hyphens.
17447 @item
17448 If such a replacement occurs in the
17449 second character position of a name, and the first character is
17450 ^@samp{a}, @samp{g}, @samp{s}, or @samp{i}, ^@samp{A}, @samp{G}, @samp{S}, or @samp{I},^
17451 then replace the dot by the character
17452 ^@samp{~} (tilde)^@samp{$} (dollar sign)^
17453 instead of a minus.
17454 @end itemize
17455 The reason for this exception is to avoid clashes
17456 with the standard names for children of System, Ada, Interfaces,
17457 and GNAT, which use the prefixes
17458 ^@samp{s-}, @samp{a-}, @samp{i-}, and @samp{g-},^@samp{S-}, @samp{A-}, @samp{I-}, and @samp{G-},^
17459 respectively.
17460
17461 The @option{^-gnatk^/FILE_NAME_MAX_LENGTH=^@var{nn}}
17462 switch of the compiler activates a ``krunching''
17463 circuit that limits file names to nn characters (where nn is a decimal
17464 integer). For example, using OpenVMS,
17465 where the maximum file name length is
17466 39, the value of nn is usually set to 39, but if you want to generate
17467 a set of files that would be usable if ported to a system with some
17468 different maximum file length, then a different value can be specified.
17469 The default value of 39 for OpenVMS need not be specified.
17470
17471 The @code{gnatkr} utility can be used to determine the krunched name for
17472 a given file, when krunched to a specified maximum length.
17473
17474 @node Using gnatkr
17475 @section Using @code{gnatkr}
17476
17477 @noindent
17478 The @code{gnatkr} command has the form
17479
17480 @ifclear vms
17481 @smallexample
17482 $ gnatkr @var{name} @ovar{length}
17483 @end smallexample
17484 @end ifclear
17485
17486 @ifset vms
17487 @smallexample
17488 $ gnatkr @var{name} /COUNT=nn
17489 @end smallexample
17490 @end ifset
17491
17492 @noindent
17493 @var{name} is the uncrunched file name, derived from the name of the unit
17494 in the standard manner described in the previous section (i.e., in particular
17495 all dots are replaced by hyphens). The file name may or may not have an
17496 extension (defined as a suffix of the form period followed by arbitrary
17497 characters other than period). If an extension is present then it will
17498 be preserved in the output. For example, when krunching @file{hellofile.ads}
17499 to eight characters, the result will be hellofil.ads.
17500
17501 Note: for compatibility with previous versions of @code{gnatkr} dots may
17502 appear in the name instead of hyphens, but the last dot will always be
17503 taken as the start of an extension. So if @code{gnatkr} is given an argument
17504 such as @file{Hello.World.adb} it will be treated exactly as if the first
17505 period had been a hyphen, and for example krunching to eight characters
17506 gives the result @file{hellworl.adb}.
17507
17508 Note that the result is always all lower case (except on OpenVMS where it is
17509 all upper case). Characters of the other case are folded as required.
17510
17511 @var{length} represents the length of the krunched name. The default
17512 when no argument is given is ^8^39^ characters. A length of zero stands for
17513 unlimited, in other words do not chop except for system files where the
17514 implied crunching length is always eight characters.
17515
17516 @noindent
17517 The output is the krunched name. The output has an extension only if the
17518 original argument was a file name with an extension.
17519
17520 @node Krunching Method
17521 @section Krunching Method
17522
17523 @noindent
17524 The initial file name is determined by the name of the unit that the file
17525 contains. The name is formed by taking the full expanded name of the
17526 unit and replacing the separating dots with hyphens and
17527 using ^lowercase^uppercase^
17528 for all letters, except that a hyphen in the second character position is
17529 replaced by a ^tilde^dollar sign^ if the first character is
17530 ^@samp{a}, @samp{i}, @samp{g}, or @samp{s}^@samp{A}, @samp{I}, @samp{G}, or @samp{S}^.
17531 The extension is @code{.ads} for a
17532 spec and @code{.adb} for a body.
17533 Krunching does not affect the extension, but the file name is shortened to
17534 the specified length by following these rules:
17535
17536 @itemize @bullet
17537 @item
17538 The name is divided into segments separated by hyphens, tildes or
17539 underscores and all hyphens, tildes, and underscores are
17540 eliminated. If this leaves the name short enough, we are done.
17541
17542 @item
17543 If the name is too long, the longest segment is located (left-most
17544 if there are two of equal length), and shortened by dropping
17545 its last character. This is repeated until the name is short enough.
17546
17547 As an example, consider the krunching of @*@file{our-strings-wide_fixed.adb}
17548 to fit the name into 8 characters as required by some operating systems.
17549
17550 @smallexample
17551 our-strings-wide_fixed 22
17552 our strings wide fixed 19
17553 our string wide fixed 18
17554 our strin wide fixed 17
17555 our stri wide fixed 16
17556 our stri wide fixe 15
17557 our str wide fixe 14
17558 our str wid fixe 13
17559 our str wid fix 12
17560 ou str wid fix 11
17561 ou st wid fix 10
17562 ou st wi fix 9
17563 ou st wi fi 8
17564 Final file name: oustwifi.adb
17565 @end smallexample
17566
17567 @item
17568 The file names for all predefined units are always krunched to eight
17569 characters. The krunching of these predefined units uses the following
17570 special prefix replacements:
17571
17572 @table @file
17573 @item ada-
17574 replaced by @file{^a^A^-}
17575
17576 @item gnat-
17577 replaced by @file{^g^G^-}
17578
17579 @item interfaces-
17580 replaced by @file{^i^I^-}
17581
17582 @item system-
17583 replaced by @file{^s^S^-}
17584 @end table
17585
17586 These system files have a hyphen in the second character position. That
17587 is why normal user files replace such a character with a
17588 ^tilde^dollar sign^, to
17589 avoid confusion with system file names.
17590
17591 As an example of this special rule, consider
17592 @*@file{ada-strings-wide_fixed.adb}, which gets krunched as follows:
17593
17594 @smallexample
17595 ada-strings-wide_fixed 22
17596 a- strings wide fixed 18
17597 a- string wide fixed 17
17598 a- strin wide fixed 16
17599 a- stri wide fixed 15
17600 a- stri wide fixe 14
17601 a- str wide fixe 13
17602 a- str wid fixe 12
17603 a- str wid fix 11
17604 a- st wid fix 10
17605 a- st wi fix 9
17606 a- st wi fi 8
17607 Final file name: a-stwifi.adb
17608 @end smallexample
17609 @end itemize
17610
17611 Of course no file shortening algorithm can guarantee uniqueness over all
17612 possible unit names, and if file name krunching is used then it is your
17613 responsibility to ensure that no name clashes occur. The utility
17614 program @code{gnatkr} is supplied for conveniently determining the
17615 krunched name of a file.
17616
17617 @node Examples of gnatkr Usage
17618 @section Examples of @code{gnatkr} Usage
17619
17620 @smallexample
17621 @iftex
17622 @leftskip=0cm
17623 @end iftex
17624 @ifclear vms
17625 $ gnatkr very_long_unit_name.ads --> velounna.ads
17626 $ gnatkr grandparent-parent-child.ads --> grparchi.ads
17627 $ gnatkr Grandparent.Parent.Child.ads --> grparchi.ads
17628 $ gnatkr grandparent-parent-child --> grparchi
17629 @end ifclear
17630 $ gnatkr very_long_unit_name.ads/count=6 --> vlunna.ads
17631 $ gnatkr very_long_unit_name.ads/count=0 --> very_long_unit_name.ads
17632 @end smallexample
17633
17634 @node Preprocessing Using gnatprep
17635 @chapter Preprocessing Using @code{gnatprep}
17636 @findex gnatprep
17637
17638 @noindent
17639 This chapter discusses how to use GNAT's @code{gnatprep} utility for simple
17640 preprocessing.
17641 Although designed for use with GNAT, @code{gnatprep} does not depend on any
17642 special GNAT features.
17643 For further discussion of conditional compilation in general, see
17644 @ref{Conditional Compilation}.
17645
17646 @menu
17647 * Preprocessing Symbols::
17648 * Using gnatprep::
17649 * Switches for gnatprep::
17650 * Form of Definitions File::
17651 * Form of Input Text for gnatprep::
17652 @end menu
17653
17654 @node Preprocessing Symbols
17655 @section Preprocessing Symbols
17656
17657 @noindent
17658 Preprocessing symbols are defined in definition files and referred to in
17659 sources to be preprocessed. A Preprocessing symbol is an identifier, following
17660 normal Ada (case-insensitive) rules for its syntax, with the restriction that
17661 all characters need to be in the ASCII set (no accented letters).
17662
17663 @node Using gnatprep
17664 @section Using @code{gnatprep}
17665
17666 @noindent
17667 To call @code{gnatprep} use
17668
17669 @smallexample
17670 $ gnatprep @ovar{switches} @var{infile} @var{outfile} @ovar{deffile}
17671 @end smallexample
17672
17673 @noindent
17674 where
17675 @table @var
17676 @item switches
17677 is an optional sequence of switches as described in the next section.
17678
17679 @item infile
17680 is the full name of the input file, which is an Ada source
17681 file containing preprocessor directives.
17682
17683 @item outfile
17684 is the full name of the output file, which is an Ada source
17685 in standard Ada form. When used with GNAT, this file name will
17686 normally have an ads or adb suffix.
17687
17688 @item deffile
17689 is the full name of a text file containing definitions of
17690 preprocessing symbols to be referenced by the preprocessor. This argument is
17691 optional, and can be replaced by the use of the @option{-D} switch.
17692
17693 @end table
17694
17695 @node Switches for gnatprep
17696 @section Switches for @code{gnatprep}
17697
17698 @table @option
17699 @c !sort!
17700
17701 @item ^-b^/BLANK_LINES^
17702 @cindex @option{^-b^/BLANK_LINES^} (@command{gnatprep})
17703 Causes both preprocessor lines and the lines deleted by
17704 preprocessing to be replaced by blank lines in the output source file,
17705 preserving line numbers in the output file.
17706
17707 @item ^-c^/COMMENTS^
17708 @cindex @option{^-c^/COMMENTS^} (@command{gnatprep})
17709 Causes both preprocessor lines and the lines deleted
17710 by preprocessing to be retained in the output source as comments marked
17711 with the special string @code{"--! "}. This option will result in line numbers
17712 being preserved in the output file.
17713
17714 @item ^-C^/REPLACE_IN_COMMENTS^
17715 @cindex @option{^-C^/REPLACE_IN_COMMENTS^} (@command{gnatprep})
17716 Causes comments to be scanned. Normally comments are ignored by gnatprep.
17717 If this option is specified, then comments are scanned and any $symbol
17718 substitutions performed as in program text. This is particularly useful
17719 when structured comments are used (e.g., when writing programs in the
17720 SPARK dialect of Ada). Note that this switch is not available when
17721 doing integrated preprocessing (it would be useless in this context
17722 since comments are ignored by the compiler in any case).
17723
17724 @item ^-Dsymbol=value^/ASSOCIATE="symbol=value"^
17725 @cindex @option{^-D^/ASSOCIATE^} (@command{gnatprep})
17726 Defines a new preprocessing symbol, associated with value. If no value is given
17727 on the command line, then symbol is considered to be @code{True}. This switch
17728 can be used in place of a definition file.
17729
17730 @ifset vms
17731 @item /REMOVE
17732 @cindex @option{/REMOVE} (@command{gnatprep})
17733 This is the default setting which causes lines deleted by preprocessing
17734 to be entirely removed from the output file.
17735 @end ifset
17736
17737 @item ^-r^/REFERENCE^
17738 @cindex @option{^-r^/REFERENCE^} (@command{gnatprep})
17739 Causes a @code{Source_Reference} pragma to be generated that
17740 references the original input file, so that error messages will use
17741 the file name of this original file. The use of this switch implies
17742 that preprocessor lines are not to be removed from the file, so its
17743 use will force @option{^-b^/BLANK_LINES^} mode if
17744 @option{^-c^/COMMENTS^}
17745 has not been specified explicitly.
17746
17747 Note that if the file to be preprocessed contains multiple units, then
17748 it will be necessary to @code{gnatchop} the output file from
17749 @code{gnatprep}. If a @code{Source_Reference} pragma is present
17750 in the preprocessed file, it will be respected by
17751 @code{gnatchop ^-r^/REFERENCE^}
17752 so that the final chopped files will correctly refer to the original
17753 input source file for @code{gnatprep}.
17754
17755 @item ^-s^/SYMBOLS^
17756 @cindex @option{^-s^/SYMBOLS^} (@command{gnatprep})
17757 Causes a sorted list of symbol names and values to be
17758 listed on the standard output file.
17759
17760 @item ^-u^/UNDEFINED^
17761 @cindex @option{^-u^/UNDEFINED^} (@command{gnatprep})
17762 Causes undefined symbols to be treated as having the value FALSE in the context
17763 of a preprocessor test. In the absence of this option, an undefined symbol in
17764 a @code{#if} or @code{#elsif} test will be treated as an error.
17765
17766 @end table
17767
17768 @ifclear vms
17769 @noindent
17770 Note: if neither @option{-b} nor @option{-c} is present,
17771 then preprocessor lines and
17772 deleted lines are completely removed from the output, unless -r is
17773 specified, in which case -b is assumed.
17774 @end ifclear
17775
17776 @node Form of Definitions File
17777 @section Form of Definitions File
17778
17779 @noindent
17780 The definitions file contains lines of the form
17781
17782 @smallexample
17783 symbol := value
17784 @end smallexample
17785
17786 @noindent
17787 where symbol is a preprocessing symbol, and value is one of the following:
17788
17789 @itemize @bullet
17790 @item
17791 Empty, corresponding to a null substitution
17792 @item
17793 A string literal using normal Ada syntax
17794 @item
17795 Any sequence of characters from the set
17796 (letters, digits, period, underline).
17797 @end itemize
17798
17799 @noindent
17800 Comment lines may also appear in the definitions file, starting with
17801 the usual @code{--},
17802 and comments may be added to the definitions lines.
17803
17804 @node Form of Input Text for gnatprep
17805 @section Form of Input Text for @code{gnatprep}
17806
17807 @noindent
17808 The input text may contain preprocessor conditional inclusion lines,
17809 as well as general symbol substitution sequences.
17810
17811 The preprocessor conditional inclusion commands have the form
17812
17813 @smallexample
17814 @group
17815 @cartouche
17816 #if @i{expression} @r{[}then@r{]}
17817 lines
17818 #elsif @i{expression} @r{[}then@r{]}
17819 lines
17820 #elsif @i{expression} @r{[}then@r{]}
17821 lines
17822 @dots{}
17823 #else
17824 lines
17825 #end if;
17826 @end cartouche
17827 @end group
17828 @end smallexample
17829
17830 @noindent
17831 In this example, @i{expression} is defined by the following grammar:
17832 @smallexample
17833 @i{expression} ::= <symbol>
17834 @i{expression} ::= <symbol> = "<value>"
17835 @i{expression} ::= <symbol> = <symbol>
17836 @i{expression} ::= <symbol> 'Defined
17837 @i{expression} ::= not @i{expression}
17838 @i{expression} ::= @i{expression} and @i{expression}
17839 @i{expression} ::= @i{expression} or @i{expression}
17840 @i{expression} ::= @i{expression} and then @i{expression}
17841 @i{expression} ::= @i{expression} or else @i{expression}
17842 @i{expression} ::= ( @i{expression} )
17843 @end smallexample
17844
17845 The following restriction exists: it is not allowed to have "and" or "or"
17846 following "not" in the same expression without parentheses. For example, this
17847 is not allowed:
17848
17849 @smallexample
17850 not X or Y
17851 @end smallexample
17852
17853 This should be one of the following:
17854
17855 @smallexample
17856 (not X) or Y
17857 not (X or Y)
17858 @end smallexample
17859
17860 @noindent
17861 For the first test (@i{expression} ::= <symbol>) the symbol must have
17862 either the value true or false, that is to say the right-hand of the
17863 symbol definition must be one of the (case-insensitive) literals
17864 @code{True} or @code{False}. If the value is true, then the
17865 corresponding lines are included, and if the value is false, they are
17866 excluded.
17867
17868 The test (@i{expression} ::= <symbol> @code{'Defined}) is true only if
17869 the symbol has been defined in the definition file or by a @option{-D}
17870 switch on the command line. Otherwise, the test is false.
17871
17872 The equality tests are case insensitive, as are all the preprocessor lines.
17873
17874 If the symbol referenced is not defined in the symbol definitions file,
17875 then the effect depends on whether or not switch @option{-u}
17876 is specified. If so, then the symbol is treated as if it had the value
17877 false and the test fails. If this switch is not specified, then
17878 it is an error to reference an undefined symbol. It is also an error to
17879 reference a symbol that is defined with a value other than @code{True}
17880 or @code{False}.
17881
17882 The use of the @code{not} operator inverts the sense of this logical test.
17883 The @code{not} operator cannot be combined with the @code{or} or @code{and}
17884 operators, without parentheses. For example, "if not X or Y then" is not
17885 allowed, but "if (not X) or Y then" and "if not (X or Y) then" are.
17886
17887 The @code{then} keyword is optional as shown
17888
17889 The @code{#} must be the first non-blank character on a line, but
17890 otherwise the format is free form. Spaces or tabs may appear between
17891 the @code{#} and the keyword. The keywords and the symbols are case
17892 insensitive as in normal Ada code. Comments may be used on a
17893 preprocessor line, but other than that, no other tokens may appear on a
17894 preprocessor line. Any number of @code{elsif} clauses can be present,
17895 including none at all. The @code{else} is optional, as in Ada.
17896
17897 The @code{#} marking the start of a preprocessor line must be the first
17898 non-blank character on the line, i.e., it must be preceded only by
17899 spaces or horizontal tabs.
17900
17901 Symbol substitution outside of preprocessor lines is obtained by using
17902 the sequence
17903
17904 @smallexample
17905 $symbol
17906 @end smallexample
17907
17908 @noindent
17909 anywhere within a source line, except in a comment or within a
17910 string literal. The identifier
17911 following the @code{$} must match one of the symbols defined in the symbol
17912 definition file, and the result is to substitute the value of the
17913 symbol in place of @code{$symbol} in the output file.
17914
17915 Note that although the substitution of strings within a string literal
17916 is not possible, it is possible to have a symbol whose defined value is
17917 a string literal. So instead of setting XYZ to @code{hello} and writing:
17918
17919 @smallexample
17920 Header : String := "$XYZ";
17921 @end smallexample
17922
17923 @noindent
17924 you should set XYZ to @code{"hello"} and write:
17925
17926 @smallexample
17927 Header : String := $XYZ;
17928 @end smallexample
17929
17930 @noindent
17931 and then the substitution will occur as desired.
17932
17933 @ifset vms
17934 @node The GNAT Run-Time Library Builder gnatlbr
17935 @chapter The GNAT Run-Time Library Builder @code{gnatlbr}
17936 @findex gnatlbr
17937 @cindex Library builder
17938
17939 @noindent
17940 @code{gnatlbr} is a tool for rebuilding the GNAT run time with user
17941 supplied configuration pragmas.
17942
17943 @menu
17944 * Running gnatlbr::
17945 * Switches for gnatlbr::
17946 * Examples of gnatlbr Usage::
17947 @end menu
17948
17949 @node Running gnatlbr
17950 @section Running @code{gnatlbr}
17951
17952 @noindent
17953 The @code{gnatlbr} command has the form
17954
17955 @smallexample
17956 $ GNAT LIBRARY /@r{[}CREATE@r{|}SET@r{|}DELETE@r{]}=directory @r{[}/CONFIG=file@r{]}
17957 @end smallexample
17958
17959 @node Switches for gnatlbr
17960 @section Switches for @code{gnatlbr}
17961
17962 @noindent
17963 @code{gnatlbr} recognizes the following switches:
17964
17965 @table @option
17966 @c !sort!
17967 @item /CREATE=directory
17968 @cindex @code{/CREATE} (@code{gnatlbr})
17969 Create the new run-time library in the specified directory.
17970
17971 @item /SET=directory
17972 @cindex @code{/SET} (@code{gnatlbr})
17973 Make the library in the specified directory the current run-time library.
17974
17975 @item /DELETE=directory
17976 @cindex @code{/DELETE} (@code{gnatlbr})
17977 Delete the run-time library in the specified directory.
17978
17979 @item /CONFIG=file
17980 @cindex @code{/CONFIG} (@code{gnatlbr})
17981 With /CREATE: Use the configuration pragmas in the specified file when
17982 building the library.
17983
17984 With /SET: Use the configuration pragmas in the specified file when
17985 compiling.
17986
17987 @end table
17988
17989 @node Examples of gnatlbr Usage
17990 @section Example of @code{gnatlbr} Usage
17991
17992 @smallexample
17993 Contents of VAXFLOAT.ADC:
17994 pragma Float_Representation (VAX_Float);
17995
17996 $ GNAT LIBRARY /CREATE=[.VAXFLOAT] /CONFIG=VAXFLOAT.ADC
17997
17998 GNAT LIBRARY rebuilds the run-time library in directory [.VAXFLOAT]
17999
18000 @end smallexample
18001 @end ifset
18002
18003 @node The GNAT Library Browser gnatls
18004 @chapter The GNAT Library Browser @code{gnatls}
18005 @findex gnatls
18006 @cindex Library browser
18007
18008 @noindent
18009 @code{gnatls} is a tool that outputs information about compiled
18010 units. It gives the relationship between objects, unit names and source
18011 files. It can also be used to check the source dependencies of a unit
18012 as well as various characteristics.
18013
18014 Note: to invoke @code{gnatls} with a project file, use the @code{gnat}
18015 driver (see @ref{The GNAT Driver and Project Files}).
18016
18017 @menu
18018 * Running gnatls::
18019 * Switches for gnatls::
18020 * Examples of gnatls Usage::
18021 @end menu
18022
18023 @node Running gnatls
18024 @section Running @code{gnatls}
18025
18026 @noindent
18027 The @code{gnatls} command has the form
18028
18029 @smallexample
18030 $ gnatls switches @var{object_or_ali_file}
18031 @end smallexample
18032
18033 @noindent
18034 The main argument is the list of object or @file{ali} files
18035 (@pxref{The Ada Library Information Files})
18036 for which information is requested.
18037
18038 In normal mode, without additional option, @code{gnatls} produces a
18039 four-column listing. Each line represents information for a specific
18040 object. The first column gives the full path of the object, the second
18041 column gives the name of the principal unit in this object, the third
18042 column gives the status of the source and the fourth column gives the
18043 full path of the source representing this unit.
18044 Here is a simple example of use:
18045
18046 @smallexample
18047 $ gnatls *.o
18048 ^./^[]^demo1.o demo1 DIF demo1.adb
18049 ^./^[]^demo2.o demo2 OK demo2.adb
18050 ^./^[]^hello.o h1 OK hello.adb
18051 ^./^[]^instr-child.o instr.child MOK instr-child.adb
18052 ^./^[]^instr.o instr OK instr.adb
18053 ^./^[]^tef.o tef DIF tef.adb
18054 ^./^[]^text_io_example.o text_io_example OK text_io_example.adb
18055 ^./^[]^tgef.o tgef DIF tgef.adb
18056 @end smallexample
18057
18058 @noindent
18059 The first line can be interpreted as follows: the main unit which is
18060 contained in
18061 object file @file{demo1.o} is demo1, whose main source is in
18062 @file{demo1.adb}. Furthermore, the version of the source used for the
18063 compilation of demo1 has been modified (DIF). Each source file has a status
18064 qualifier which can be:
18065
18066 @table @code
18067 @item OK (unchanged)
18068 The version of the source file used for the compilation of the
18069 specified unit corresponds exactly to the actual source file.
18070
18071 @item MOK (slightly modified)
18072 The version of the source file used for the compilation of the
18073 specified unit differs from the actual source file but not enough to
18074 require recompilation. If you use gnatmake with the qualifier
18075 @option{^-m (minimal recompilation)^/MINIMAL_RECOMPILATION^}, a file marked
18076 MOK will not be recompiled.
18077
18078 @item DIF (modified)
18079 No version of the source found on the path corresponds to the source
18080 used to build this object.
18081
18082 @item ??? (file not found)
18083 No source file was found for this unit.
18084
18085 @item HID (hidden, unchanged version not first on PATH)
18086 The version of the source that corresponds exactly to the source used
18087 for compilation has been found on the path but it is hidden by another
18088 version of the same source that has been modified.
18089
18090 @end table
18091
18092 @node Switches for gnatls
18093 @section Switches for @code{gnatls}
18094
18095 @noindent
18096 @code{gnatls} recognizes the following switches:
18097
18098 @table @option
18099 @c !sort!
18100 @cindex @option{--version} @command{gnatls}
18101 Display Copyright and version, then exit disregarding all other options.
18102
18103 @item --help
18104 @cindex @option{--help} @command{gnatls}
18105 If @option{--version} was not used, display usage, then exit disregarding
18106 all other options.
18107
18108 @item ^-a^/ALL_UNITS^
18109 @cindex @option{^-a^/ALL_UNITS^} (@code{gnatls})
18110 Consider all units, including those of the predefined Ada library.
18111 Especially useful with @option{^-d^/DEPENDENCIES^}.
18112
18113 @item ^-d^/DEPENDENCIES^
18114 @cindex @option{^-d^/DEPENDENCIES^} (@code{gnatls})
18115 List sources from which specified units depend on.
18116
18117 @item ^-h^/OUTPUT=OPTIONS^
18118 @cindex @option{^-h^/OUTPUT=OPTIONS^} (@code{gnatls})
18119 Output the list of options.
18120
18121 @item ^-o^/OUTPUT=OBJECTS^
18122 @cindex @option{^-o^/OUTPUT=OBJECTS^} (@code{gnatls})
18123 Only output information about object files.
18124
18125 @item ^-s^/OUTPUT=SOURCES^
18126 @cindex @option{^-s^/OUTPUT=SOURCES^} (@code{gnatls})
18127 Only output information about source files.
18128
18129 @item ^-u^/OUTPUT=UNITS^
18130 @cindex @option{^-u^/OUTPUT=UNITS^} (@code{gnatls})
18131 Only output information about compilation units.
18132
18133 @item ^-files^/FILES^=@var{file}
18134 @cindex @option{^-files^/FILES^} (@code{gnatls})
18135 Take as arguments the files listed in text file @var{file}.
18136 Text file @var{file} may contain empty lines that are ignored.
18137 Each nonempty line should contain the name of an existing file.
18138 Several such switches may be specified simultaneously.
18139
18140 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
18141 @itemx ^-aI^/SOURCE_SEARCH=^@var{dir}
18142 @itemx ^-I^/SEARCH=^@var{dir}
18143 @itemx ^-I-^/NOCURRENT_DIRECTORY^
18144 @itemx -nostdinc
18145 @cindex @option{^-aO^/OBJECT_SEARCH^} (@code{gnatls})
18146 @cindex @option{^-aI^/SOURCE_SEARCH^} (@code{gnatls})
18147 @cindex @option{^-I^/SEARCH^} (@code{gnatls})
18148 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatls})
18149 Source path manipulation. Same meaning as the equivalent @command{gnatmake}
18150 flags (@pxref{Switches for gnatmake}).
18151
18152 @item --RTS=@var{rts-path}
18153 @cindex @option{--RTS} (@code{gnatls})
18154 Specifies the default location of the runtime library. Same meaning as the
18155 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
18156
18157 @item ^-v^/OUTPUT=VERBOSE^
18158 @cindex @option{^-v^/OUTPUT=VERBOSE^} (@code{gnatls})
18159 Verbose mode. Output the complete source, object and project paths. Do not use
18160 the default column layout but instead use long format giving as much as
18161 information possible on each requested units, including special
18162 characteristics such as:
18163
18164 @table @code
18165 @item Preelaborable
18166 The unit is preelaborable in the Ada sense.
18167
18168 @item No_Elab_Code
18169 No elaboration code has been produced by the compiler for this unit.
18170
18171 @item Pure
18172 The unit is pure in the Ada sense.
18173
18174 @item Elaborate_Body
18175 The unit contains a pragma Elaborate_Body.
18176
18177 @item Remote_Types
18178 The unit contains a pragma Remote_Types.
18179
18180 @item Shared_Passive
18181 The unit contains a pragma Shared_Passive.
18182
18183 @item Predefined
18184 This unit is part of the predefined environment and cannot be modified
18185 by the user.
18186
18187 @item Remote_Call_Interface
18188 The unit contains a pragma Remote_Call_Interface.
18189
18190 @end table
18191
18192 @end table
18193
18194 @node Examples of gnatls Usage
18195 @section Example of @code{gnatls} Usage
18196 @ifclear vms
18197
18198 @noindent
18199 Example of using the verbose switch. Note how the source and
18200 object paths are affected by the -I switch.
18201
18202 @smallexample
18203 $ gnatls -v -I.. demo1.o
18204
18205 GNATLS 5.03w (20041123-34)
18206 Copyright 1997-2004 Free Software Foundation, Inc.
18207
18208 Source Search Path:
18209 <Current_Directory>
18210 ../
18211 /home/comar/local/adainclude/
18212
18213 Object Search Path:
18214 <Current_Directory>
18215 ../
18216 /home/comar/local/lib/gcc-lib/x86-linux/3.4.3/adalib/
18217
18218 Project Search Path:
18219 <Current_Directory>
18220 /home/comar/local/lib/gnat/
18221
18222 ./demo1.o
18223 Unit =>
18224 Name => demo1
18225 Kind => subprogram body
18226 Flags => No_Elab_Code
18227 Source => demo1.adb modified
18228 @end smallexample
18229
18230 @noindent
18231 The following is an example of use of the dependency list.
18232 Note the use of the -s switch
18233 which gives a straight list of source files. This can be useful for
18234 building specialized scripts.
18235
18236 @smallexample
18237 $ gnatls -d demo2.o
18238 ./demo2.o demo2 OK demo2.adb
18239 OK gen_list.ads
18240 OK gen_list.adb
18241 OK instr.ads
18242 OK instr-child.ads
18243
18244 $ gnatls -d -s -a demo1.o
18245 demo1.adb
18246 /home/comar/local/adainclude/ada.ads
18247 /home/comar/local/adainclude/a-finali.ads
18248 /home/comar/local/adainclude/a-filico.ads
18249 /home/comar/local/adainclude/a-stream.ads
18250 /home/comar/local/adainclude/a-tags.ads
18251 gen_list.ads
18252 gen_list.adb
18253 /home/comar/local/adainclude/gnat.ads
18254 /home/comar/local/adainclude/g-io.ads
18255 instr.ads
18256 /home/comar/local/adainclude/system.ads
18257 /home/comar/local/adainclude/s-exctab.ads
18258 /home/comar/local/adainclude/s-finimp.ads
18259 /home/comar/local/adainclude/s-finroo.ads
18260 /home/comar/local/adainclude/s-secsta.ads
18261 /home/comar/local/adainclude/s-stalib.ads
18262 /home/comar/local/adainclude/s-stoele.ads
18263 /home/comar/local/adainclude/s-stratt.ads
18264 /home/comar/local/adainclude/s-tasoli.ads
18265 /home/comar/local/adainclude/s-unstyp.ads
18266 /home/comar/local/adainclude/unchconv.ads
18267 @end smallexample
18268 @end ifclear
18269
18270 @ifset vms
18271 @smallexample
18272 GNAT LIST /DEPENDENCIES /OUTPUT=SOURCES /ALL_UNITS DEMO1.ADB
18273
18274 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]ada.ads
18275 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-finali.ads
18276 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-filico.ads
18277 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-stream.ads
18278 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-tags.ads
18279 demo1.adb
18280 gen_list.ads
18281 gen_list.adb
18282 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]gnat.ads
18283 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]g-io.ads
18284 instr.ads
18285 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]system.ads
18286 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-exctab.ads
18287 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-finimp.ads
18288 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-finroo.ads
18289 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-secsta.ads
18290 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stalib.ads
18291 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stoele.ads
18292 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stratt.ads
18293 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-tasoli.ads
18294 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-unstyp.ads
18295 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]unchconv.ads
18296 @end smallexample
18297 @end ifset
18298
18299 @node Cleaning Up Using gnatclean
18300 @chapter Cleaning Up Using @code{gnatclean}
18301 @findex gnatclean
18302 @cindex Cleaning tool
18303
18304 @noindent
18305 @code{gnatclean} is a tool that allows the deletion of files produced by the
18306 compiler, binder and linker, including ALI files, object files, tree files,
18307 expanded source files, library files, interface copy source files, binder
18308 generated files and executable files.
18309
18310 @menu
18311 * Running gnatclean::
18312 * Switches for gnatclean::
18313 @c * Examples of gnatclean Usage::
18314 @end menu
18315
18316 @node Running gnatclean
18317 @section Running @code{gnatclean}
18318
18319 @noindent
18320 The @code{gnatclean} command has the form:
18321
18322 @smallexample
18323 $ gnatclean switches @var{names}
18324 @end smallexample
18325
18326 @noindent
18327 @var{names} is a list of source file names. Suffixes @code{.^ads^ADS^} and
18328 @code{^adb^ADB^} may be omitted. If a project file is specified using switch
18329 @code{^-P^/PROJECT_FILE=^}, then @var{names} may be completely omitted.
18330
18331 @noindent
18332 In normal mode, @code{gnatclean} delete the files produced by the compiler and,
18333 if switch @code{^-c^/COMPILER_FILES_ONLY^} is not specified, by the binder and
18334 the linker. In informative-only mode, specified by switch
18335 @code{^-n^/NODELETE^}, the list of files that would have been deleted in
18336 normal mode is listed, but no file is actually deleted.
18337
18338 @node Switches for gnatclean
18339 @section Switches for @code{gnatclean}
18340
18341 @noindent
18342 @code{gnatclean} recognizes the following switches:
18343
18344 @table @option
18345 @c !sort!
18346 @cindex @option{--version} @command{gnatclean}
18347 Display Copyright and version, then exit disregarding all other options.
18348
18349 @item --help
18350 @cindex @option{--help} @command{gnatclean}
18351 If @option{--version} was not used, display usage, then exit disregarding
18352 all other options.
18353
18354 @item ^-c^/COMPILER_FILES_ONLY^
18355 @cindex @option{^-c^/COMPILER_FILES_ONLY^} (@code{gnatclean})
18356 Only attempt to delete the files produced by the compiler, not those produced
18357 by the binder or the linker. The files that are not to be deleted are library
18358 files, interface copy files, binder generated files and executable files.
18359
18360 @item ^-D ^/DIRECTORY_OBJECTS=^@var{dir}
18361 @cindex @option{^-D^/DIRECTORY_OBJECTS^} (@code{gnatclean})
18362 Indicate that ALI and object files should normally be found in directory
18363 @var{dir}.
18364
18365 @item ^-F^/FULL_PATH_IN_BRIEF_MESSAGES^
18366 @cindex @option{^-F^/FULL_PATH_IN_BRIEF_MESSAGES^} (@code{gnatclean})
18367 When using project files, if some errors or warnings are detected during
18368 parsing and verbose mode is not in effect (no use of switch
18369 ^-v^/VERBOSE^), then error lines start with the full path name of the project
18370 file, rather than its simple file name.
18371
18372 @item ^-h^/HELP^
18373 @cindex @option{^-h^/HELP^} (@code{gnatclean})
18374 Output a message explaining the usage of @code{^gnatclean^gnatclean^}.
18375
18376 @item ^-n^/NODELETE^
18377 @cindex @option{^-n^/NODELETE^} (@code{gnatclean})
18378 Informative-only mode. Do not delete any files. Output the list of the files
18379 that would have been deleted if this switch was not specified.
18380
18381 @item ^-P^/PROJECT_FILE=^@var{project}
18382 @cindex @option{^-P^/PROJECT_FILE^} (@code{gnatclean})
18383 Use project file @var{project}. Only one such switch can be used.
18384 When cleaning a project file, the files produced by the compilation of the
18385 immediate sources or inherited sources of the project files are to be
18386 deleted. This is not depending on the presence or not of executable names
18387 on the command line.
18388
18389 @item ^-q^/QUIET^
18390 @cindex @option{^-q^/QUIET^} (@code{gnatclean})
18391 Quiet output. If there are no errors, do not output anything, except in
18392 verbose mode (switch ^-v^/VERBOSE^) or in informative-only mode
18393 (switch ^-n^/NODELETE^).
18394
18395 @item ^-r^/RECURSIVE^
18396 @cindex @option{^-r^/RECURSIVE^} (@code{gnatclean})
18397 When a project file is specified (using switch ^-P^/PROJECT_FILE=^),
18398 clean all imported and extended project files, recursively. If this switch
18399 is not specified, only the files related to the main project file are to be
18400 deleted. This switch has no effect if no project file is specified.
18401
18402 @item ^-v^/VERBOSE^
18403 @cindex @option{^-v^/VERBOSE^} (@code{gnatclean})
18404 Verbose mode.
18405
18406 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
18407 @cindex @option{^-vP^/MESSAGES_PROJECT_FILE^} (@code{gnatclean})
18408 Indicates the verbosity of the parsing of GNAT project files.
18409 @xref{Switches Related to Project Files}.
18410
18411 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
18412 @cindex @option{^-X^/EXTERNAL_REFERENCE^} (@code{gnatclean})
18413 Indicates that external variable @var{name} has the value @var{value}.
18414 The Project Manager will use this value for occurrences of
18415 @code{external(name)} when parsing the project file.
18416 @xref{Switches Related to Project Files}.
18417
18418 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
18419 @cindex @option{^-aO^/OBJECT_SEARCH^} (@code{gnatclean})
18420 When searching for ALI and object files, look in directory
18421 @var{dir}.
18422
18423 @item ^-I^/SEARCH=^@var{dir}
18424 @cindex @option{^-I^/SEARCH^} (@code{gnatclean})
18425 Equivalent to @option{^-aO^/OBJECT_SEARCH=^@var{dir}}.
18426
18427 @item ^-I-^/NOCURRENT_DIRECTORY^
18428 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatclean})
18429 @cindex Source files, suppressing search
18430 Do not look for ALI or object files in the directory
18431 where @code{gnatclean} was invoked.
18432
18433 @end table
18434
18435 @c @node Examples of gnatclean Usage
18436 @c @section Examples of @code{gnatclean} Usage
18437
18438 @ifclear vms
18439 @node GNAT and Libraries
18440 @chapter GNAT and Libraries
18441 @cindex Library, building, installing, using
18442
18443 @noindent
18444 This chapter describes how to build and use libraries with GNAT, and also shows
18445 how to recompile the GNAT run-time library. You should be familiar with the
18446 Project Manager facility (@pxref{GNAT Project Manager}) before reading this
18447 chapter.
18448
18449 @menu
18450 * Introduction to Libraries in GNAT::
18451 * General Ada Libraries::
18452 * Stand-alone Ada Libraries::
18453 * Rebuilding the GNAT Run-Time Library::
18454 @end menu
18455
18456 @node Introduction to Libraries in GNAT
18457 @section Introduction to Libraries in GNAT
18458
18459 @noindent
18460 A library is, conceptually, a collection of objects which does not have its
18461 own main thread of execution, but rather provides certain services to the
18462 applications that use it. A library can be either statically linked with the
18463 application, in which case its code is directly included in the application,
18464 or, on platforms that support it, be dynamically linked, in which case
18465 its code is shared by all applications making use of this library.
18466
18467 GNAT supports both types of libraries.
18468 In the static case, the compiled code can be provided in different ways. The
18469 simplest approach is to provide directly the set of objects resulting from
18470 compilation of the library source files. Alternatively, you can group the
18471 objects into an archive using whatever commands are provided by the operating
18472 system. For the latter case, the objects are grouped into a shared library.
18473
18474 In the GNAT environment, a library has three types of components:
18475 @itemize @bullet
18476 @item
18477 Source files.
18478 @item
18479 @file{ALI} files.
18480 @xref{The Ada Library Information Files}.
18481 @item
18482 Object files, an archive or a shared library.
18483 @end itemize
18484
18485 @noindent
18486 A GNAT library may expose all its source files, which is useful for
18487 documentation purposes. Alternatively, it may expose only the units needed by
18488 an external user to make use of the library. That is to say, the specs
18489 reflecting the library services along with all the units needed to compile
18490 those specs, which can include generic bodies or any body implementing an
18491 inlined routine. In the case of @emph{stand-alone libraries} those exposed
18492 units are called @emph{interface units} (@pxref{Stand-alone Ada Libraries}).
18493
18494 All compilation units comprising an application, including those in a library,
18495 need to be elaborated in an order partially defined by Ada's semantics. GNAT
18496 computes the elaboration order from the @file{ALI} files and this is why they
18497 constitute a mandatory part of GNAT libraries. Except in the case of
18498 @emph{stand-alone libraries}, where a specific library elaboration routine is
18499 produced independently of the application(s) using the library.
18500
18501 @node General Ada Libraries
18502 @section General Ada Libraries
18503
18504 @menu
18505 * Building a library::
18506 * Installing a library::
18507 * Using a library::
18508 @end menu
18509
18510 @node Building a library
18511 @subsection Building a library
18512
18513 @noindent
18514 The easiest way to build a library is to use the Project Manager,
18515 which supports a special type of project called a @emph{Library Project}
18516 (@pxref{Library Projects}).
18517
18518 A project is considered a library project, when two project-level attributes
18519 are defined in it: @code{Library_Name} and @code{Library_Dir}. In order to
18520 control different aspects of library configuration, additional optional
18521 project-level attributes can be specified:
18522 @table @code
18523 @item Library_Kind
18524 This attribute controls whether the library is to be static or dynamic
18525
18526 @item Library_Version
18527 This attribute specifies the library version; this value is used
18528 during dynamic linking of shared libraries to determine if the currently
18529 installed versions of the binaries are compatible.
18530
18531 @item Library_Options
18532 @item Library_GCC
18533 These attributes specify additional low-level options to be used during
18534 library generation, and redefine the actual application used to generate
18535 library.
18536 @end table
18537
18538 @noindent
18539 The GNAT Project Manager takes full care of the library maintenance task,
18540 including recompilation of the source files for which objects do not exist
18541 or are not up to date, assembly of the library archive, and installation of
18542 the library (i.e., copying associated source, object and @file{ALI} files
18543 to the specified location).
18544
18545 Here is a simple library project file:
18546 @smallexample @c ada
18547 project My_Lib is
18548 for Source_Dirs use ("src1", "src2");
18549 for Object_Dir use "obj";
18550 for Library_Name use "mylib";
18551 for Library_Dir use "lib";
18552 for Library_Kind use "dynamic";
18553 end My_lib;
18554 @end smallexample
18555
18556 @noindent
18557 and the compilation command to build and install the library:
18558
18559 @smallexample @c ada
18560 $ gnatmake -Pmy_lib
18561 @end smallexample
18562
18563 @noindent
18564 It is not entirely trivial to perform manually all the steps required to
18565 produce a library. We recommend that you use the GNAT Project Manager
18566 for this task. In special cases where this is not desired, the necessary
18567 steps are discussed below.
18568
18569 There are various possibilities for compiling the units that make up the
18570 library: for example with a Makefile (@pxref{Using the GNU make Utility}) or
18571 with a conventional script. For simple libraries, it is also possible to create
18572 a dummy main program which depends upon all the packages that comprise the
18573 interface of the library. This dummy main program can then be given to
18574 @command{gnatmake}, which will ensure that all necessary objects are built.
18575
18576 After this task is accomplished, you should follow the standard procedure
18577 of the underlying operating system to produce the static or shared library.
18578
18579 Here is an example of such a dummy program:
18580 @smallexample @c ada
18581 @group
18582 with My_Lib.Service1;
18583 with My_Lib.Service2;
18584 with My_Lib.Service3;
18585 procedure My_Lib_Dummy is
18586 begin
18587 null;
18588 end;
18589 @end group
18590 @end smallexample
18591
18592 @noindent
18593 Here are the generic commands that will build an archive or a shared library.
18594
18595 @smallexample
18596 # compiling the library
18597 $ gnatmake -c my_lib_dummy.adb
18598
18599 # we don't need the dummy object itself
18600 $ rm my_lib_dummy.o my_lib_dummy.ali
18601
18602 # create an archive with the remaining objects
18603 $ ar rc libmy_lib.a *.o
18604 # some systems may require "ranlib" to be run as well
18605
18606 # or create a shared library
18607 $ gcc -shared -o libmy_lib.so *.o
18608 # some systems may require the code to have been compiled with -fPIC
18609
18610 # remove the object files that are now in the library
18611 $ rm *.o
18612
18613 # Make the ALI files read-only so that gnatmake will not try to
18614 # regenerate the objects that are in the library
18615 $ chmod -w *.ali
18616 @end smallexample
18617
18618 @noindent
18619 Please note that the library must have a name of the form @file{lib@var{xxx}.a}
18620 or @file{lib@var{xxx}.so} (or @file{lib@var{xxx}.dll} on Windows) in order to
18621 be accessed by the directive @option{-l@var{xxx}} at link time.
18622
18623 @node Installing a library
18624 @subsection Installing a library
18625 @cindex @code{ADA_PROJECT_PATH}
18626
18627 @noindent
18628 If you use project files, library installation is part of the library build
18629 process. Thus no further action is needed in order to make use of the
18630 libraries that are built as part of the general application build. A usable
18631 version of the library is installed in the directory specified by the
18632 @code{Library_Dir} attribute of the library project file.
18633
18634 You may want to install a library in a context different from where the library
18635 is built. This situation arises with third party suppliers, who may want
18636 to distribute a library in binary form where the user is not expected to be
18637 able to recompile the library. The simplest option in this case is to provide
18638 a project file slightly different from the one used to build the library, by
18639 using the @code{externally_built} attribute. For instance, the project
18640 file used to build the library in the previous section can be changed into the
18641 following one when the library is installed:
18642
18643 @smallexample @c projectfile
18644 project My_Lib is
18645 for Source_Dirs use ("src1", "src2");
18646 for Library_Name use "mylib";
18647 for Library_Dir use "lib";
18648 for Library_Kind use "dynamic";
18649 for Externally_Built use "true";
18650 end My_lib;
18651 @end smallexample
18652
18653 @noindent
18654 This project file assumes that the directories @file{src1},
18655 @file{src2}, and @file{lib} exist in
18656 the directory containing the project file. The @code{externally_built}
18657 attribute makes it clear to the GNAT builder that it should not attempt to
18658 recompile any of the units from this library. It allows the library provider to
18659 restrict the source set to the minimum necessary for clients to make use of the
18660 library as described in the first section of this chapter. It is the
18661 responsibility of the library provider to install the necessary sources, ALI
18662 files and libraries in the directories mentioned in the project file. For
18663 convenience, the user's library project file should be installed in a location
18664 that will be searched automatically by the GNAT
18665 builder. These are the directories referenced in the @env{ADA_PROJECT_PATH}
18666 environment variable (@pxref{Importing Projects}), and also the default GNAT
18667 library location that can be queried with @command{gnatls -v} and is usually of
18668 the form $gnat_install_root/lib/gnat.
18669
18670 When project files are not an option, it is also possible, but not recommended,
18671 to install the library so that the sources needed to use the library are on the
18672 Ada source path and the ALI files & libraries be on the Ada Object path (see
18673 @ref{Search Paths and the Run-Time Library (RTL)}. Alternatively, the system
18674 administrator can place general-purpose libraries in the default compiler
18675 paths, by specifying the libraries' location in the configuration files
18676 @file{ada_source_path} and @file{ada_object_path}. These configuration files
18677 must be located in the GNAT installation tree at the same place as the gcc spec
18678 file. The location of the gcc spec file can be determined as follows:
18679 @smallexample
18680 $ gcc -v
18681 @end smallexample
18682
18683 @noindent
18684 The configuration files mentioned above have a simple format: each line
18685 must contain one unique directory name.
18686 Those names are added to the corresponding path
18687 in their order of appearance in the file. The names can be either absolute
18688 or relative; in the latter case, they are relative to where theses files
18689 are located.
18690
18691 The files @file{ada_source_path} and @file{ada_object_path} might not be
18692 present in a
18693 GNAT installation, in which case, GNAT will look for its run-time library in
18694 the directories @file{adainclude} (for the sources) and @file{adalib} (for the
18695 objects and @file{ALI} files). When the files exist, the compiler does not
18696 look in @file{adainclude} and @file{adalib}, and thus the
18697 @file{ada_source_path} file
18698 must contain the location for the GNAT run-time sources (which can simply
18699 be @file{adainclude}). In the same way, the @file{ada_object_path} file must
18700 contain the location for the GNAT run-time objects (which can simply
18701 be @file{adalib}).
18702
18703 You can also specify a new default path to the run-time library at compilation
18704 time with the switch @option{--RTS=rts-path}. You can thus choose / change
18705 the run-time library you want your program to be compiled with. This switch is
18706 recognized by @command{gcc}, @command{gnatmake}, @command{gnatbind},
18707 @command{gnatls}, @command{gnatfind} and @command{gnatxref}.
18708
18709 It is possible to install a library before or after the standard GNAT
18710 library, by reordering the lines in the configuration files. In general, a
18711 library must be installed before the GNAT library if it redefines
18712 any part of it.
18713
18714 @node Using a library
18715 @subsection Using a library
18716
18717 @noindent Once again, the project facility greatly simplifies the use of
18718 libraries. In this context, using a library is just a matter of adding a
18719 @code{with} clause in the user project. For instance, to make use of the
18720 library @code{My_Lib} shown in examples in earlier sections, you can
18721 write:
18722
18723 @smallexample @c projectfile
18724 with "my_lib";
18725 project My_Proj is
18726 @dots{}
18727 end My_Proj;
18728 @end smallexample
18729
18730 Even if you have a third-party, non-Ada library, you can still use GNAT's
18731 Project Manager facility to provide a wrapper for it. For example, the
18732 following project, when @code{with}ed by your main project, will link with the
18733 third-party library @file{liba.a}:
18734
18735 @smallexample @c projectfile
18736 @group
18737 project Liba is
18738 for Externally_Built use "true";
18739 for Source_Files use ();
18740 for Library_Dir use "lib";
18741 for Library_Name use "a";
18742 for Library_Kind use "static";
18743 end Liba;
18744 @end group
18745 @end smallexample
18746 This is an alternative to the use of @code{pragma Linker_Options}. It is
18747 especially interesting in the context of systems with several interdependent
18748 static libraries where finding a proper linker order is not easy and best be
18749 left to the tools having visibility over project dependence information.
18750
18751 @noindent
18752 In order to use an Ada library manually, you need to make sure that this
18753 library is on both your source and object path
18754 (see @ref{Search Paths and the Run-Time Library (RTL)}
18755 and @ref{Search Paths for gnatbind}). Furthermore, when the objects are grouped
18756 in an archive or a shared library, you need to specify the desired
18757 library at link time.
18758
18759 For example, you can use the library @file{mylib} installed in
18760 @file{/dir/my_lib_src} and @file{/dir/my_lib_obj} with the following commands:
18761
18762 @smallexample
18763 $ gnatmake -aI/dir/my_lib_src -aO/dir/my_lib_obj my_appl \
18764 -largs -lmy_lib
18765 @end smallexample
18766
18767 @noindent
18768 This can be expressed more simply:
18769 @smallexample
18770 $ gnatmake my_appl
18771 @end smallexample
18772 @noindent
18773 when the following conditions are met:
18774 @itemize @bullet
18775 @item
18776 @file{/dir/my_lib_src} has been added by the user to the environment
18777 variable @env{ADA_INCLUDE_PATH}, or by the administrator to the file
18778 @file{ada_source_path}
18779 @item
18780 @file{/dir/my_lib_obj} has been added by the user to the environment
18781 variable @env{ADA_OBJECTS_PATH}, or by the administrator to the file
18782 @file{ada_object_path}
18783 @item
18784 a pragma @code{Linker_Options} has been added to one of the sources.
18785 For example:
18786
18787 @smallexample @c ada
18788 pragma Linker_Options ("-lmy_lib");
18789 @end smallexample
18790 @end itemize
18791
18792 @node Stand-alone Ada Libraries
18793 @section Stand-alone Ada Libraries
18794 @cindex Stand-alone library, building, using
18795
18796 @menu
18797 * Introduction to Stand-alone Libraries::
18798 * Building a Stand-alone Library::
18799 * Creating a Stand-alone Library to be used in a non-Ada context::
18800 * Restrictions in Stand-alone Libraries::
18801 @end menu
18802
18803 @node Introduction to Stand-alone Libraries
18804 @subsection Introduction to Stand-alone Libraries
18805
18806 @noindent
18807 A Stand-alone Library (abbreviated ``SAL'') is a library that contains the
18808 necessary code to
18809 elaborate the Ada units that are included in the library. In contrast with
18810 an ordinary library, which consists of all sources, objects and @file{ALI}
18811 files of the
18812 library, a SAL may specify a restricted subset of compilation units
18813 to serve as a library interface. In this case, the fully
18814 self-sufficient set of files will normally consist of an objects
18815 archive, the sources of interface units' specs, and the @file{ALI}
18816 files of interface units.
18817 If an interface spec contains a generic unit or an inlined subprogram,
18818 the body's
18819 source must also be provided; if the units that must be provided in the source
18820 form depend on other units, the source and @file{ALI} files of those must
18821 also be provided.
18822
18823 The main purpose of a SAL is to minimize the recompilation overhead of client
18824 applications when a new version of the library is installed. Specifically,
18825 if the interface sources have not changed, client applications do not need to
18826 be recompiled. If, furthermore, a SAL is provided in the shared form and its
18827 version, controlled by @code{Library_Version} attribute, is not changed,
18828 then the clients do not need to be relinked.
18829
18830 SALs also allow the library providers to minimize the amount of library source
18831 text exposed to the clients. Such ``information hiding'' might be useful or
18832 necessary for various reasons.
18833
18834 Stand-alone libraries are also well suited to be used in an executable whose
18835 main routine is not written in Ada.
18836
18837 @node Building a Stand-alone Library
18838 @subsection Building a Stand-alone Library
18839
18840 @noindent
18841 GNAT's Project facility provides a simple way of building and installing
18842 stand-alone libraries; see @ref{Stand-alone Library Projects}.
18843 To be a Stand-alone Library Project, in addition to the two attributes
18844 that make a project a Library Project (@code{Library_Name} and
18845 @code{Library_Dir}; see @ref{Library Projects}), the attribute
18846 @code{Library_Interface} must be defined. For example:
18847
18848 @smallexample @c projectfile
18849 @group
18850 for Library_Dir use "lib_dir";
18851 for Library_Name use "dummy";
18852 for Library_Interface use ("int1", "int1.child");
18853 @end group
18854 @end smallexample
18855
18856 @noindent
18857 Attribute @code{Library_Interface} has a non-empty string list value,
18858 each string in the list designating a unit contained in an immediate source
18859 of the project file.
18860
18861 When a Stand-alone Library is built, first the binder is invoked to build
18862 a package whose name depends on the library name
18863 (@file{^b~dummy.ads/b^B$DUMMY.ADS/B^} in the example above).
18864 This binder-generated package includes initialization and
18865 finalization procedures whose
18866 names depend on the library name (@code{dummyinit} and @code{dummyfinal}
18867 in the example
18868 above). The object corresponding to this package is included in the library.
18869
18870 You must ensure timely (e.g., prior to any use of interfaces in the SAL)
18871 calling of these procedures if a static SAL is built, or if a shared SAL
18872 is built
18873 with the project-level attribute @code{Library_Auto_Init} set to
18874 @code{"false"}.
18875
18876 For a Stand-Alone Library, only the @file{ALI} files of the Interface Units
18877 (those that are listed in attribute @code{Library_Interface}) are copied to
18878 the Library Directory. As a consequence, only the Interface Units may be
18879 imported from Ada units outside of the library. If other units are imported,
18880 the binding phase will fail.
18881
18882 The attribute @code{Library_Src_Dir} may be specified for a
18883 Stand-Alone Library. @code{Library_Src_Dir} is a simple attribute that has a
18884 single string value. Its value must be the path (absolute or relative to the
18885 project directory) of an existing directory. This directory cannot be the
18886 object directory or one of the source directories, but it can be the same as
18887 the library directory. The sources of the Interface
18888 Units of the library that are needed by an Ada client of the library will be
18889 copied to the designated directory, called the Interface Copy directory.
18890 These sources include the specs of the Interface Units, but they may also
18891 include bodies and subunits, when pragmas @code{Inline} or @code{Inline_Always}
18892 are used, or when there is a generic unit in the spec. Before the sources
18893 are copied to the Interface Copy directory, an attempt is made to delete all
18894 files in the Interface Copy directory.
18895
18896 Building stand-alone libraries by hand is somewhat tedious, but for those
18897 occasions when it is necessary here are the steps that you need to perform:
18898 @itemize @bullet
18899 @item
18900 Compile all library sources.
18901
18902 @item
18903 Invoke the binder with the switch @option{-n} (No Ada main program),
18904 with all the @file{ALI} files of the interfaces, and
18905 with the switch @option{-L} to give specific names to the @code{init}
18906 and @code{final} procedures. For example:
18907 @smallexample
18908 gnatbind -n int1.ali int2.ali -Lsal1
18909 @end smallexample
18910
18911 @item
18912 Compile the binder generated file:
18913 @smallexample
18914 gcc -c b~int2.adb
18915 @end smallexample
18916
18917 @item
18918 Link the dynamic library with all the necessary object files,
18919 indicating to the linker the names of the @code{init} (and possibly
18920 @code{final}) procedures for automatic initialization (and finalization).
18921 The built library should be placed in a directory different from
18922 the object directory.
18923
18924 @item
18925 Copy the @code{ALI} files of the interface to the library directory,
18926 add in this copy an indication that it is an interface to a SAL
18927 (i.e., add a word @option{SL} on the line in the @file{ALI} file that starts
18928 with letter ``P'') and make the modified copy of the @file{ALI} file
18929 read-only.
18930 @end itemize
18931
18932 @noindent
18933 Using SALs is not different from using other libraries
18934 (see @ref{Using a library}).
18935
18936 @node Creating a Stand-alone Library to be used in a non-Ada context
18937 @subsection Creating a Stand-alone Library to be used in a non-Ada context
18938
18939 @noindent
18940 It is easy to adapt the SAL build procedure discussed above for use of a SAL in
18941 a non-Ada context.
18942
18943 The only extra step required is to ensure that library interface subprograms
18944 are compatible with the main program, by means of @code{pragma Export}
18945 or @code{pragma Convention}.
18946
18947 Here is an example of simple library interface for use with C main program:
18948
18949 @smallexample @c ada
18950 package Interface is
18951
18952 procedure Do_Something;
18953 pragma Export (C, Do_Something, "do_something");
18954
18955 procedure Do_Something_Else;
18956 pragma Export (C, Do_Something_Else, "do_something_else");
18957
18958 end Interface;
18959 @end smallexample
18960
18961 @noindent
18962 On the foreign language side, you must provide a ``foreign'' view of the
18963 library interface; remember that it should contain elaboration routines in
18964 addition to interface subprograms.
18965
18966 The example below shows the content of @code{mylib_interface.h} (note
18967 that there is no rule for the naming of this file, any name can be used)
18968 @smallexample
18969 /* the library elaboration procedure */
18970 extern void mylibinit (void);
18971
18972 /* the library finalization procedure */
18973 extern void mylibfinal (void);
18974
18975 /* the interface exported by the library */
18976 extern void do_something (void);
18977 extern void do_something_else (void);
18978 @end smallexample
18979
18980 @noindent
18981 Libraries built as explained above can be used from any program, provided
18982 that the elaboration procedures (named @code{mylibinit} in the previous
18983 example) are called before the library services are used. Any number of
18984 libraries can be used simultaneously, as long as the elaboration
18985 procedure of each library is called.
18986
18987 Below is an example of a C program that uses the @code{mylib} library.
18988
18989 @smallexample
18990 #include "mylib_interface.h"
18991
18992 int
18993 main (void)
18994 @{
18995 /* First, elaborate the library before using it */
18996 mylibinit ();
18997
18998 /* Main program, using the library exported entities */
18999 do_something ();
19000 do_something_else ();
19001
19002 /* Library finalization at the end of the program */
19003 mylibfinal ();
19004 return 0;
19005 @}
19006 @end smallexample
19007
19008 @noindent
19009 Note that invoking any library finalization procedure generated by
19010 @code{gnatbind} shuts down the Ada run-time environment.
19011 Consequently, the
19012 finalization of all Ada libraries must be performed at the end of the program.
19013 No call to these libraries or to the Ada run-time library should be made
19014 after the finalization phase.
19015
19016 @node Restrictions in Stand-alone Libraries
19017 @subsection Restrictions in Stand-alone Libraries
19018
19019 @noindent
19020 The pragmas listed below should be used with caution inside libraries,
19021 as they can create incompatibilities with other Ada libraries:
19022 @itemize @bullet
19023 @item pragma @code{Locking_Policy}
19024 @item pragma @code{Queuing_Policy}
19025 @item pragma @code{Task_Dispatching_Policy}
19026 @item pragma @code{Unreserve_All_Interrupts}
19027 @end itemize
19028
19029 @noindent
19030 When using a library that contains such pragmas, the user must make sure
19031 that all libraries use the same pragmas with the same values. Otherwise,
19032 @code{Program_Error} will
19033 be raised during the elaboration of the conflicting
19034 libraries. The usage of these pragmas and its consequences for the user
19035 should therefore be well documented.
19036
19037 Similarly, the traceback in the exception occurrence mechanism should be
19038 enabled or disabled in a consistent manner across all libraries.
19039 Otherwise, Program_Error will be raised during the elaboration of the
19040 conflicting libraries.
19041
19042 If the @code{Version} or @code{Body_Version}
19043 attributes are used inside a library, then you need to
19044 perform a @code{gnatbind} step that specifies all @file{ALI} files in all
19045 libraries, so that version identifiers can be properly computed.
19046 In practice these attributes are rarely used, so this is unlikely
19047 to be a consideration.
19048
19049 @node Rebuilding the GNAT Run-Time Library
19050 @section Rebuilding the GNAT Run-Time Library
19051 @cindex GNAT Run-Time Library, rebuilding
19052 @cindex Building the GNAT Run-Time Library
19053 @cindex Rebuilding the GNAT Run-Time Library
19054 @cindex Run-Time Library, rebuilding
19055
19056 @noindent
19057 It may be useful to recompile the GNAT library in various contexts, the
19058 most important one being the use of partition-wide configuration pragmas
19059 such as @code{Normalize_Scalars}. A special Makefile called
19060 @code{Makefile.adalib} is provided to that effect and can be found in
19061 the directory containing the GNAT library. The location of this
19062 directory depends on the way the GNAT environment has been installed and can
19063 be determined by means of the command:
19064
19065 @smallexample
19066 $ gnatls -v
19067 @end smallexample
19068
19069 @noindent
19070 The last entry in the object search path usually contains the
19071 gnat library. This Makefile contains its own documentation and in
19072 particular the set of instructions needed to rebuild a new library and
19073 to use it.
19074
19075 @node Using the GNU make Utility
19076 @chapter Using the GNU @code{make} Utility
19077 @findex make
19078
19079 @noindent
19080 This chapter offers some examples of makefiles that solve specific
19081 problems. It does not explain how to write a makefile (@pxref{Top,, GNU
19082 make, make, GNU @code{make}}), nor does it try to replace the
19083 @command{gnatmake} utility (@pxref{The GNAT Make Program gnatmake}).
19084
19085 All the examples in this section are specific to the GNU version of
19086 make. Although @command{make} is a standard utility, and the basic language
19087 is the same, these examples use some advanced features found only in
19088 @code{GNU make}.
19089
19090 @menu
19091 * Using gnatmake in a Makefile::
19092 * Automatically Creating a List of Directories::
19093 * Generating the Command Line Switches::
19094 * Overcoming Command Line Length Limits::
19095 @end menu
19096
19097 @node Using gnatmake in a Makefile
19098 @section Using gnatmake in a Makefile
19099 @findex makefile
19100 @cindex GNU make
19101
19102 @noindent
19103 Complex project organizations can be handled in a very powerful way by
19104 using GNU make combined with gnatmake. For instance, here is a Makefile
19105 which allows you to build each subsystem of a big project into a separate
19106 shared library. Such a makefile allows you to significantly reduce the link
19107 time of very big applications while maintaining full coherence at
19108 each step of the build process.
19109
19110 The list of dependencies are handled automatically by
19111 @command{gnatmake}. The Makefile is simply used to call gnatmake in each of
19112 the appropriate directories.
19113
19114 Note that you should also read the example on how to automatically
19115 create the list of directories
19116 (@pxref{Automatically Creating a List of Directories})
19117 which might help you in case your project has a lot of subdirectories.
19118
19119 @smallexample
19120 @iftex
19121 @leftskip=0cm
19122 @font@heightrm=cmr8
19123 @heightrm
19124 @end iftex
19125 ## This Makefile is intended to be used with the following directory
19126 ## configuration:
19127 ## - The sources are split into a series of csc (computer software components)
19128 ## Each of these csc is put in its own directory.
19129 ## Their name are referenced by the directory names.
19130 ## They will be compiled into shared library (although this would also work
19131 ## with static libraries
19132 ## - The main program (and possibly other packages that do not belong to any
19133 ## csc is put in the top level directory (where the Makefile is).
19134 ## toplevel_dir __ first_csc (sources) __ lib (will contain the library)
19135 ## \_ second_csc (sources) __ lib (will contain the library)
19136 ## \_ @dots{}
19137 ## Although this Makefile is build for shared library, it is easy to modify
19138 ## to build partial link objects instead (modify the lines with -shared and
19139 ## gnatlink below)
19140 ##
19141 ## With this makefile, you can change any file in the system or add any new
19142 ## file, and everything will be recompiled correctly (only the relevant shared
19143 ## objects will be recompiled, and the main program will be re-linked).
19144
19145 # The list of computer software component for your project. This might be
19146 # generated automatically.
19147 CSC_LIST=aa bb cc
19148
19149 # Name of the main program (no extension)
19150 MAIN=main
19151
19152 # If we need to build objects with -fPIC, uncomment the following line
19153 #NEED_FPIC=-fPIC
19154
19155 # The following variable should give the directory containing libgnat.so
19156 # You can get this directory through 'gnatls -v'. This is usually the last
19157 # directory in the Object_Path.
19158 GLIB=@dots{}
19159
19160 # The directories for the libraries
19161 # (This macro expands the list of CSC to the list of shared libraries, you
19162 # could simply use the expanded form:
19163 # LIB_DIR=aa/lib/libaa.so bb/lib/libbb.so cc/lib/libcc.so
19164 LIB_DIR=$@{foreach dir,$@{CSC_LIST@},$@{dir@}/lib/lib$@{dir@}.so@}
19165
19166 $@{MAIN@}: objects $@{LIB_DIR@}
19167 gnatbind $@{MAIN@} $@{CSC_LIST:%=-aO%/lib@} -shared
19168 gnatlink $@{MAIN@} $@{CSC_LIST:%=-l%@}
19169
19170 objects::
19171 # recompile the sources
19172 gnatmake -c -i $@{MAIN@}.adb $@{NEED_FPIC@} $@{CSC_LIST:%=-I%@}
19173
19174 # Note: In a future version of GNAT, the following commands will be simplified
19175 # by a new tool, gnatmlib
19176 $@{LIB_DIR@}:
19177 mkdir -p $@{dir $@@ @}
19178 cd $@{dir $@@ @} && gcc -shared -o $@{notdir $@@ @} ../*.o -L$@{GLIB@} -lgnat
19179 cd $@{dir $@@ @} && cp -f ../*.ali .
19180
19181 # The dependencies for the modules
19182 # Note that we have to force the expansion of *.o, since in some cases
19183 # make won't be able to do it itself.
19184 aa/lib/libaa.so: $@{wildcard aa/*.o@}
19185 bb/lib/libbb.so: $@{wildcard bb/*.o@}
19186 cc/lib/libcc.so: $@{wildcard cc/*.o@}
19187
19188 # Make sure all of the shared libraries are in the path before starting the
19189 # program
19190 run::
19191 LD_LIBRARY_PATH=`pwd`/aa/lib:`pwd`/bb/lib:`pwd`/cc/lib ./$@{MAIN@}
19192
19193 clean::
19194 $@{RM@} -rf $@{CSC_LIST:%=%/lib@}
19195 $@{RM@} $@{CSC_LIST:%=%/*.ali@}
19196 $@{RM@} $@{CSC_LIST:%=%/*.o@}
19197 $@{RM@} *.o *.ali $@{MAIN@}
19198 @end smallexample
19199
19200 @node Automatically Creating a List of Directories
19201 @section Automatically Creating a List of Directories
19202
19203 @noindent
19204 In most makefiles, you will have to specify a list of directories, and
19205 store it in a variable. For small projects, it is often easier to
19206 specify each of them by hand, since you then have full control over what
19207 is the proper order for these directories, which ones should be
19208 included.
19209
19210 However, in larger projects, which might involve hundreds of
19211 subdirectories, it might be more convenient to generate this list
19212 automatically.
19213
19214 The example below presents two methods. The first one, although less
19215 general, gives you more control over the list. It involves wildcard
19216 characters, that are automatically expanded by @command{make}. Its
19217 shortcoming is that you need to explicitly specify some of the
19218 organization of your project, such as for instance the directory tree
19219 depth, whether some directories are found in a separate tree, @enddots{}
19220
19221 The second method is the most general one. It requires an external
19222 program, called @command{find}, which is standard on all Unix systems. All
19223 the directories found under a given root directory will be added to the
19224 list.
19225
19226 @smallexample
19227 @iftex
19228 @leftskip=0cm
19229 @font@heightrm=cmr8
19230 @heightrm
19231 @end iftex
19232 # The examples below are based on the following directory hierarchy:
19233 # All the directories can contain any number of files
19234 # ROOT_DIRECTORY -> a -> aa -> aaa
19235 # -> ab
19236 # -> ac
19237 # -> b -> ba -> baa
19238 # -> bb
19239 # -> bc
19240 # This Makefile creates a variable called DIRS, that can be reused any time
19241 # you need this list (see the other examples in this section)
19242
19243 # The root of your project's directory hierarchy
19244 ROOT_DIRECTORY=.
19245
19246 ####
19247 # First method: specify explicitly the list of directories
19248 # This allows you to specify any subset of all the directories you need.
19249 ####
19250
19251 DIRS := a/aa/ a/ab/ b/ba/
19252
19253 ####
19254 # Second method: use wildcards
19255 # Note that the argument(s) to wildcard below should end with a '/'.
19256 # Since wildcards also return file names, we have to filter them out
19257 # to avoid duplicate directory names.
19258 # We thus use make's @code{dir} and @code{sort} functions.
19259 # It sets DIRs to the following value (note that the directories aaa and baa
19260 # are not given, unless you change the arguments to wildcard).
19261 # DIRS= ./a/a/ ./b/ ./a/aa/ ./a/ab/ ./a/ac/ ./b/ba/ ./b/bb/ ./b/bc/
19262 ####
19263
19264 DIRS := $@{sort $@{dir $@{wildcard $@{ROOT_DIRECTORY@}/*/
19265 $@{ROOT_DIRECTORY@}/*/*/@}@}@}
19266
19267 ####
19268 # Third method: use an external program
19269 # This command is much faster if run on local disks, avoiding NFS slowdowns.
19270 # This is the most complete command: it sets DIRs to the following value:
19271 # DIRS= ./a ./a/aa ./a/aa/aaa ./a/ab ./a/ac ./b ./b/ba ./b/ba/baa ./b/bb ./b/bc
19272 ####
19273
19274 DIRS := $@{shell find $@{ROOT_DIRECTORY@} -type d -print@}
19275
19276 @end smallexample
19277
19278 @node Generating the Command Line Switches
19279 @section Generating the Command Line Switches
19280
19281 @noindent
19282 Once you have created the list of directories as explained in the
19283 previous section (@pxref{Automatically Creating a List of Directories}),
19284 you can easily generate the command line arguments to pass to gnatmake.
19285
19286 For the sake of completeness, this example assumes that the source path
19287 is not the same as the object path, and that you have two separate lists
19288 of directories.
19289
19290 @smallexample
19291 # see "Automatically creating a list of directories" to create
19292 # these variables
19293 SOURCE_DIRS=
19294 OBJECT_DIRS=
19295
19296 GNATMAKE_SWITCHES := $@{patsubst %,-aI%,$@{SOURCE_DIRS@}@}
19297 GNATMAKE_SWITCHES += $@{patsubst %,-aO%,$@{OBJECT_DIRS@}@}
19298
19299 all:
19300 gnatmake $@{GNATMAKE_SWITCHES@} main_unit
19301 @end smallexample
19302
19303 @node Overcoming Command Line Length Limits
19304 @section Overcoming Command Line Length Limits
19305
19306 @noindent
19307 One problem that might be encountered on big projects is that many
19308 operating systems limit the length of the command line. It is thus hard to give
19309 gnatmake the list of source and object directories.
19310
19311 This example shows how you can set up environment variables, which will
19312 make @command{gnatmake} behave exactly as if the directories had been
19313 specified on the command line, but have a much higher length limit (or
19314 even none on most systems).
19315
19316 It assumes that you have created a list of directories in your Makefile,
19317 using one of the methods presented in
19318 @ref{Automatically Creating a List of Directories}.
19319 For the sake of completeness, we assume that the object
19320 path (where the ALI files are found) is different from the sources patch.
19321
19322 Note a small trick in the Makefile below: for efficiency reasons, we
19323 create two temporary variables (SOURCE_LIST and OBJECT_LIST), that are
19324 expanded immediately by @code{make}. This way we overcome the standard
19325 make behavior which is to expand the variables only when they are
19326 actually used.
19327
19328 On Windows, if you are using the standard Windows command shell, you must
19329 replace colons with semicolons in the assignments to these variables.
19330
19331 @smallexample
19332 @iftex
19333 @leftskip=0cm
19334 @font@heightrm=cmr8
19335 @heightrm
19336 @end iftex
19337 # In this example, we create both ADA_INCLUDE_PATH and ADA_OBJECT_PATH.
19338 # This is the same thing as putting the -I arguments on the command line.
19339 # (the equivalent of using -aI on the command line would be to define
19340 # only ADA_INCLUDE_PATH, the equivalent of -aO is ADA_OBJECT_PATH).
19341 # You can of course have different values for these variables.
19342 #
19343 # Note also that we need to keep the previous values of these variables, since
19344 # they might have been set before running 'make' to specify where the GNAT
19345 # library is installed.
19346
19347 # see "Automatically creating a list of directories" to create these
19348 # variables
19349 SOURCE_DIRS=
19350 OBJECT_DIRS=
19351
19352 empty:=
19353 space:=$@{empty@} $@{empty@}
19354 SOURCE_LIST := $@{subst $@{space@},:,$@{SOURCE_DIRS@}@}
19355 OBJECT_LIST := $@{subst $@{space@},:,$@{OBJECT_DIRS@}@}
19356 ADA_INCLUDE_PATH += $@{SOURCE_LIST@}
19357 ADA_OBJECT_PATH += $@{OBJECT_LIST@}
19358 export ADA_INCLUDE_PATH
19359 export ADA_OBJECT_PATH
19360
19361 all:
19362 gnatmake main_unit
19363 @end smallexample
19364 @end ifclear
19365
19366 @node Memory Management Issues
19367 @chapter Memory Management Issues
19368
19369 @noindent
19370 This chapter describes some useful memory pools provided in the GNAT library
19371 and in particular the GNAT Debug Pool facility, which can be used to detect
19372 incorrect uses of access values (including ``dangling references'').
19373 @ifclear vms
19374 It also describes the @command{gnatmem} tool, which can be used to track down
19375 ``memory leaks''.
19376 @end ifclear
19377
19378 @menu
19379 * Some Useful Memory Pools::
19380 * The GNAT Debug Pool Facility::
19381 @ifclear vms
19382 * The gnatmem Tool::
19383 @end ifclear
19384 @end menu
19385
19386 @node Some Useful Memory Pools
19387 @section Some Useful Memory Pools
19388 @findex Memory Pool
19389 @cindex storage, pool
19390
19391 @noindent
19392 The @code{System.Pool_Global} package offers the Unbounded_No_Reclaim_Pool
19393 storage pool. Allocations use the standard system call @code{malloc} while
19394 deallocations use the standard system call @code{free}. No reclamation is
19395 performed when the pool goes out of scope. For performance reasons, the
19396 standard default Ada allocators/deallocators do not use any explicit storage
19397 pools but if they did, they could use this storage pool without any change in
19398 behavior. That is why this storage pool is used when the user
19399 manages to make the default implicit allocator explicit as in this example:
19400 @smallexample @c ada
19401 type T1 is access Something;
19402 -- no Storage pool is defined for T2
19403 type T2 is access Something_Else;
19404 for T2'Storage_Pool use T1'Storage_Pool;
19405 -- the above is equivalent to
19406 for T2'Storage_Pool use System.Pool_Global.Global_Pool_Object;
19407 @end smallexample
19408
19409 @noindent
19410 The @code{System.Pool_Local} package offers the Unbounded_Reclaim_Pool storage
19411 pool. The allocation strategy is similar to @code{Pool_Local}'s
19412 except that the all
19413 storage allocated with this pool is reclaimed when the pool object goes out of
19414 scope. This pool provides a explicit mechanism similar to the implicit one
19415 provided by several Ada 83 compilers for allocations performed through a local
19416 access type and whose purpose was to reclaim memory when exiting the
19417 scope of a given local access. As an example, the following program does not
19418 leak memory even though it does not perform explicit deallocation:
19419
19420 @smallexample @c ada
19421 with System.Pool_Local;
19422 procedure Pooloc1 is
19423 procedure Internal is
19424 type A is access Integer;
19425 X : System.Pool_Local.Unbounded_Reclaim_Pool;
19426 for A'Storage_Pool use X;
19427 v : A;
19428 begin
19429 for I in 1 .. 50 loop
19430 v := new Integer;
19431 end loop;
19432 end Internal;
19433 begin
19434 for I in 1 .. 100 loop
19435 Internal;
19436 end loop;
19437 end Pooloc1;
19438 @end smallexample
19439
19440 @noindent
19441 The @code{System.Pool_Size} package implements the Stack_Bounded_Pool used when
19442 @code{Storage_Size} is specified for an access type.
19443 The whole storage for the pool is
19444 allocated at once, usually on the stack at the point where the access type is
19445 elaborated. It is automatically reclaimed when exiting the scope where the
19446 access type is defined. This package is not intended to be used directly by the
19447 user and it is implicitly used for each such declaration:
19448
19449 @smallexample @c ada
19450 type T1 is access Something;
19451 for T1'Storage_Size use 10_000;
19452 @end smallexample
19453
19454 @node The GNAT Debug Pool Facility
19455 @section The GNAT Debug Pool Facility
19456 @findex Debug Pool
19457 @cindex storage, pool, memory corruption
19458
19459 @noindent
19460 The use of unchecked deallocation and unchecked conversion can easily
19461 lead to incorrect memory references. The problems generated by such
19462 references are usually difficult to tackle because the symptoms can be
19463 very remote from the origin of the problem. In such cases, it is
19464 very helpful to detect the problem as early as possible. This is the
19465 purpose of the Storage Pool provided by @code{GNAT.Debug_Pools}.
19466
19467 In order to use the GNAT specific debugging pool, the user must
19468 associate a debug pool object with each of the access types that may be
19469 related to suspected memory problems. See Ada Reference Manual 13.11.
19470 @smallexample @c ada
19471 type Ptr is access Some_Type;
19472 Pool : GNAT.Debug_Pools.Debug_Pool;
19473 for Ptr'Storage_Pool use Pool;
19474 @end smallexample
19475
19476 @noindent
19477 @code{GNAT.Debug_Pools} is derived from a GNAT-specific kind of
19478 pool: the @code{Checked_Pool}. Such pools, like standard Ada storage pools,
19479 allow the user to redefine allocation and deallocation strategies. They
19480 also provide a checkpoint for each dereference, through the use of
19481 the primitive operation @code{Dereference} which is implicitly called at
19482 each dereference of an access value.
19483
19484 Once an access type has been associated with a debug pool, operations on
19485 values of the type may raise four distinct exceptions,
19486 which correspond to four potential kinds of memory corruption:
19487 @itemize @bullet
19488 @item
19489 @code{GNAT.Debug_Pools.Accessing_Not_Allocated_Storage}
19490 @item
19491 @code{GNAT.Debug_Pools.Accessing_Deallocated_Storage}
19492 @item
19493 @code{GNAT.Debug_Pools.Freeing_Not_Allocated_Storage}
19494 @item
19495 @code{GNAT.Debug_Pools.Freeing_Deallocated_Storage }
19496 @end itemize
19497
19498 @noindent
19499 For types associated with a Debug_Pool, dynamic allocation is performed using
19500 the standard GNAT allocation routine. References to all allocated chunks of
19501 memory are kept in an internal dictionary. Several deallocation strategies are
19502 provided, whereupon the user can choose to release the memory to the system,
19503 keep it allocated for further invalid access checks, or fill it with an easily
19504 recognizable pattern for debug sessions. The memory pattern is the old IBM
19505 hexadecimal convention: @code{16#DEADBEEF#}.
19506
19507 See the documentation in the file g-debpoo.ads for more information on the
19508 various strategies.
19509
19510 Upon each dereference, a check is made that the access value denotes a
19511 properly allocated memory location. Here is a complete example of use of
19512 @code{Debug_Pools}, that includes typical instances of memory corruption:
19513 @smallexample @c ada
19514 @iftex
19515 @leftskip=0cm
19516 @end iftex
19517 with Gnat.Io; use Gnat.Io;
19518 with Unchecked_Deallocation;
19519 with Unchecked_Conversion;
19520 with GNAT.Debug_Pools;
19521 with System.Storage_Elements;
19522 with Ada.Exceptions; use Ada.Exceptions;
19523 procedure Debug_Pool_Test is
19524
19525 type T is access Integer;
19526 type U is access all T;
19527
19528 P : GNAT.Debug_Pools.Debug_Pool;
19529 for T'Storage_Pool use P;
19530
19531 procedure Free is new Unchecked_Deallocation (Integer, T);
19532 function UC is new Unchecked_Conversion (U, T);
19533 A, B : aliased T;
19534
19535 procedure Info is new GNAT.Debug_Pools.Print_Info(Put_Line);
19536
19537 begin
19538 Info (P);
19539 A := new Integer;
19540 B := new Integer;
19541 B := A;
19542 Info (P);
19543 Free (A);
19544 begin
19545 Put_Line (Integer'Image(B.all));
19546 exception
19547 when E : others => Put_Line ("raised: " & Exception_Name (E));
19548 end;
19549 begin
19550 Free (B);
19551 exception
19552 when E : others => Put_Line ("raised: " & Exception_Name (E));
19553 end;
19554 B := UC(A'Access);
19555 begin
19556 Put_Line (Integer'Image(B.all));
19557 exception
19558 when E : others => Put_Line ("raised: " & Exception_Name (E));
19559 end;
19560 begin
19561 Free (B);
19562 exception
19563 when E : others => Put_Line ("raised: " & Exception_Name (E));
19564 end;
19565 Info (P);
19566 end Debug_Pool_Test;
19567 @end smallexample
19568
19569 @noindent
19570 The debug pool mechanism provides the following precise diagnostics on the
19571 execution of this erroneous program:
19572 @smallexample
19573 Debug Pool info:
19574 Total allocated bytes : 0
19575 Total deallocated bytes : 0
19576 Current Water Mark: 0
19577 High Water Mark: 0
19578
19579 Debug Pool info:
19580 Total allocated bytes : 8
19581 Total deallocated bytes : 0
19582 Current Water Mark: 8
19583 High Water Mark: 8
19584
19585 raised: GNAT.DEBUG_POOLS.ACCESSING_DEALLOCATED_STORAGE
19586 raised: GNAT.DEBUG_POOLS.FREEING_DEALLOCATED_STORAGE
19587 raised: GNAT.DEBUG_POOLS.ACCESSING_NOT_ALLOCATED_STORAGE
19588 raised: GNAT.DEBUG_POOLS.FREEING_NOT_ALLOCATED_STORAGE
19589 Debug Pool info:
19590 Total allocated bytes : 8
19591 Total deallocated bytes : 4
19592 Current Water Mark: 4
19593 High Water Mark: 8
19594 @end smallexample
19595
19596 @ifclear vms
19597 @node The gnatmem Tool
19598 @section The @command{gnatmem} Tool
19599 @findex gnatmem
19600
19601 @noindent
19602 The @code{gnatmem} utility monitors dynamic allocation and
19603 deallocation activity in a program, and displays information about
19604 incorrect deallocations and possible sources of memory leaks.
19605 It provides three type of information:
19606 @itemize @bullet
19607 @item
19608 General information concerning memory management, such as the total
19609 number of allocations and deallocations, the amount of allocated
19610 memory and the high water mark, i.e.@: the largest amount of allocated
19611 memory in the course of program execution.
19612
19613 @item
19614 Backtraces for all incorrect deallocations, that is to say deallocations
19615 which do not correspond to a valid allocation.
19616
19617 @item
19618 Information on each allocation that is potentially the origin of a memory
19619 leak.
19620 @end itemize
19621
19622 @menu
19623 * Running gnatmem::
19624 * Switches for gnatmem::
19625 * Example of gnatmem Usage::
19626 @end menu
19627
19628 @node Running gnatmem
19629 @subsection Running @code{gnatmem}
19630
19631 @noindent
19632 @code{gnatmem} makes use of the output created by the special version of
19633 allocation and deallocation routines that record call information. This
19634 allows to obtain accurate dynamic memory usage history at a minimal cost to
19635 the execution speed. Note however, that @code{gnatmem} is not supported on
19636 all platforms (currently, it is supported on AIX, HP-UX, GNU/Linux,
19637 Solaris and Windows NT/2000/XP (x86).
19638
19639 @noindent
19640 The @code{gnatmem} command has the form
19641
19642 @smallexample
19643 $ gnatmem @ovar{switches} user_program
19644 @end smallexample
19645
19646 @noindent
19647 The program must have been linked with the instrumented version of the
19648 allocation and deallocation routines. This is done by linking with the
19649 @file{libgmem.a} library. For correct symbolic backtrace information,
19650 the user program should be compiled with debugging options
19651 (see @ref{Switches for gcc}). For example to build @file{my_program}:
19652
19653 @smallexample
19654 $ gnatmake -g my_program -largs -lgmem
19655 @end smallexample
19656
19657 @noindent
19658 As library @file{libgmem.a} contains an alternate body for package
19659 @code{System.Memory}, @file{s-memory.adb} should not be compiled and linked
19660 when an executable is linked with library @file{libgmem.a}. It is then not
19661 recommended to use @command{gnatmake} with switch @option{^-a^/ALL_FILES^}.
19662
19663 @noindent
19664 When @file{my_program} is executed, the file @file{gmem.out} is produced.
19665 This file contains information about all allocations and deallocations
19666 performed by the program. It is produced by the instrumented allocations and
19667 deallocations routines and will be used by @code{gnatmem}.
19668
19669 In order to produce symbolic backtrace information for allocations and
19670 deallocations performed by the GNAT run-time library, you need to use a
19671 version of that library that has been compiled with the @option{-g} switch
19672 (see @ref{Rebuilding the GNAT Run-Time Library}).
19673
19674 Gnatmem must be supplied with the @file{gmem.out} file and the executable to
19675 examine. If the location of @file{gmem.out} file was not explicitly supplied by
19676 @option{-i} switch, gnatmem will assume that this file can be found in the
19677 current directory. For example, after you have executed @file{my_program},
19678 @file{gmem.out} can be analyzed by @code{gnatmem} using the command:
19679
19680 @smallexample
19681 $ gnatmem my_program
19682 @end smallexample
19683
19684 @noindent
19685 This will produce the output with the following format:
19686
19687 *************** debut cc
19688 @smallexample
19689 $ gnatmem my_program
19690
19691 Global information
19692 ------------------
19693 Total number of allocations : 45
19694 Total number of deallocations : 6
19695 Final Water Mark (non freed mem) : 11.29 Kilobytes
19696 High Water Mark : 11.40 Kilobytes
19697
19698 .
19699 .
19700 .
19701 Allocation Root # 2
19702 -------------------
19703 Number of non freed allocations : 11
19704 Final Water Mark (non freed mem) : 1.16 Kilobytes
19705 High Water Mark : 1.27 Kilobytes
19706 Backtrace :
19707 my_program.adb:23 my_program.alloc
19708 .
19709 .
19710 .
19711 @end smallexample
19712
19713 The first block of output gives general information. In this case, the
19714 Ada construct ``@code{@b{new}}'' was executed 45 times, and only 6 calls to an
19715 Unchecked_Deallocation routine occurred.
19716
19717 @noindent
19718 Subsequent paragraphs display information on all allocation roots.
19719 An allocation root is a specific point in the execution of the program
19720 that generates some dynamic allocation, such as a ``@code{@b{new}}''
19721 construct. This root is represented by an execution backtrace (or subprogram
19722 call stack). By default the backtrace depth for allocations roots is 1, so
19723 that a root corresponds exactly to a source location. The backtrace can
19724 be made deeper, to make the root more specific.
19725
19726 @node Switches for gnatmem
19727 @subsection Switches for @code{gnatmem}
19728
19729 @noindent
19730 @code{gnatmem} recognizes the following switches:
19731
19732 @table @option
19733
19734 @item -q
19735 @cindex @option{-q} (@code{gnatmem})
19736 Quiet. Gives the minimum output needed to identify the origin of the
19737 memory leaks. Omits statistical information.
19738
19739 @item @var{N}
19740 @cindex @var{N} (@code{gnatmem})
19741 N is an integer literal (usually between 1 and 10) which controls the
19742 depth of the backtraces defining allocation root. The default value for
19743 N is 1. The deeper the backtrace, the more precise the localization of
19744 the root. Note that the total number of roots can depend on this
19745 parameter. This parameter must be specified @emph{before} the name of the
19746 executable to be analyzed, to avoid ambiguity.
19747
19748 @item -b n
19749 @cindex @option{-b} (@code{gnatmem})
19750 This switch has the same effect as just depth parameter.
19751
19752 @item -i @var{file}
19753 @cindex @option{-i} (@code{gnatmem})
19754 Do the @code{gnatmem} processing starting from @file{file}, rather than
19755 @file{gmem.out} in the current directory.
19756
19757 @item -m n
19758 @cindex @option{-m} (@code{gnatmem})
19759 This switch causes @code{gnatmem} to mask the allocation roots that have less
19760 than n leaks. The default value is 1. Specifying the value of 0 will allow to
19761 examine even the roots that didn't result in leaks.
19762
19763 @item -s order
19764 @cindex @option{-s} (@code{gnatmem})
19765 This switch causes @code{gnatmem} to sort the allocation roots according to the
19766 specified order of sort criteria, each identified by a single letter. The
19767 currently supported criteria are @code{n, h, w} standing respectively for
19768 number of unfreed allocations, high watermark, and final watermark
19769 corresponding to a specific root. The default order is @code{nwh}.
19770
19771 @end table
19772
19773 @node Example of gnatmem Usage
19774 @subsection Example of @code{gnatmem} Usage
19775
19776 @noindent
19777 The following example shows the use of @code{gnatmem}
19778 on a simple memory-leaking program.
19779 Suppose that we have the following Ada program:
19780
19781 @smallexample @c ada
19782 @group
19783 @cartouche
19784 with Unchecked_Deallocation;
19785 procedure Test_Gm is
19786
19787 type T is array (1..1000) of Integer;
19788 type Ptr is access T;
19789 procedure Free is new Unchecked_Deallocation (T, Ptr);
19790 A : Ptr;
19791
19792 procedure My_Alloc is
19793 begin
19794 A := new T;
19795 end My_Alloc;
19796
19797 procedure My_DeAlloc is
19798 B : Ptr := A;
19799 begin
19800 Free (B);
19801 end My_DeAlloc;
19802
19803 begin
19804 My_Alloc;
19805 for I in 1 .. 5 loop
19806 for J in I .. 5 loop
19807 My_Alloc;
19808 end loop;
19809 My_Dealloc;
19810 end loop;
19811 end;
19812 @end cartouche
19813 @end group
19814 @end smallexample
19815
19816 @noindent
19817 The program needs to be compiled with debugging option and linked with
19818 @code{gmem} library:
19819
19820 @smallexample
19821 $ gnatmake -g test_gm -largs -lgmem
19822 @end smallexample
19823
19824 @noindent
19825 Then we execute the program as usual:
19826
19827 @smallexample
19828 $ test_gm
19829 @end smallexample
19830
19831 @noindent
19832 Then @code{gnatmem} is invoked simply with
19833 @smallexample
19834 $ gnatmem test_gm
19835 @end smallexample
19836
19837 @noindent
19838 which produces the following output (result may vary on different platforms):
19839
19840 @smallexample
19841 Global information
19842 ------------------
19843 Total number of allocations : 18
19844 Total number of deallocations : 5
19845 Final Water Mark (non freed mem) : 53.00 Kilobytes
19846 High Water Mark : 56.90 Kilobytes
19847
19848 Allocation Root # 1
19849 -------------------
19850 Number of non freed allocations : 11
19851 Final Water Mark (non freed mem) : 42.97 Kilobytes
19852 High Water Mark : 46.88 Kilobytes
19853 Backtrace :
19854 test_gm.adb:11 test_gm.my_alloc
19855
19856 Allocation Root # 2
19857 -------------------
19858 Number of non freed allocations : 1
19859 Final Water Mark (non freed mem) : 10.02 Kilobytes
19860 High Water Mark : 10.02 Kilobytes
19861 Backtrace :
19862 s-secsta.adb:81 system.secondary_stack.ss_init
19863
19864 Allocation Root # 3
19865 -------------------
19866 Number of non freed allocations : 1
19867 Final Water Mark (non freed mem) : 12 Bytes
19868 High Water Mark : 12 Bytes
19869 Backtrace :
19870 s-secsta.adb:181 system.secondary_stack.ss_init
19871 @end smallexample
19872
19873 @noindent
19874 Note that the GNAT run time contains itself a certain number of
19875 allocations that have no corresponding deallocation,
19876 as shown here for root #2 and root
19877 #3. This is a normal behavior when the number of non-freed allocations
19878 is one, it allocates dynamic data structures that the run time needs for
19879 the complete lifetime of the program. Note also that there is only one
19880 allocation root in the user program with a single line back trace:
19881 test_gm.adb:11 test_gm.my_alloc, whereas a careful analysis of the
19882 program shows that 'My_Alloc' is called at 2 different points in the
19883 source (line 21 and line 24). If those two allocation roots need to be
19884 distinguished, the backtrace depth parameter can be used:
19885
19886 @smallexample
19887 $ gnatmem 3 test_gm
19888 @end smallexample
19889
19890 @noindent
19891 which will give the following output:
19892
19893 @smallexample
19894 Global information
19895 ------------------
19896 Total number of allocations : 18
19897 Total number of deallocations : 5
19898 Final Water Mark (non freed mem) : 53.00 Kilobytes
19899 High Water Mark : 56.90 Kilobytes
19900
19901 Allocation Root # 1
19902 -------------------
19903 Number of non freed allocations : 10
19904 Final Water Mark (non freed mem) : 39.06 Kilobytes
19905 High Water Mark : 42.97 Kilobytes
19906 Backtrace :
19907 test_gm.adb:11 test_gm.my_alloc
19908 test_gm.adb:24 test_gm
19909 b_test_gm.c:52 main
19910
19911 Allocation Root # 2
19912 -------------------
19913 Number of non freed allocations : 1
19914 Final Water Mark (non freed mem) : 10.02 Kilobytes
19915 High Water Mark : 10.02 Kilobytes
19916 Backtrace :
19917 s-secsta.adb:81 system.secondary_stack.ss_init
19918 s-secsta.adb:283 <system__secondary_stack___elabb>
19919 b_test_gm.c:33 adainit
19920
19921 Allocation Root # 3
19922 -------------------
19923 Number of non freed allocations : 1
19924 Final Water Mark (non freed mem) : 3.91 Kilobytes
19925 High Water Mark : 3.91 Kilobytes
19926 Backtrace :
19927 test_gm.adb:11 test_gm.my_alloc
19928 test_gm.adb:21 test_gm
19929 b_test_gm.c:52 main
19930
19931 Allocation Root # 4
19932 -------------------
19933 Number of non freed allocations : 1
19934 Final Water Mark (non freed mem) : 12 Bytes
19935 High Water Mark : 12 Bytes
19936 Backtrace :
19937 s-secsta.adb:181 system.secondary_stack.ss_init
19938 s-secsta.adb:283 <system__secondary_stack___elabb>
19939 b_test_gm.c:33 adainit
19940 @end smallexample
19941
19942 @noindent
19943 The allocation root #1 of the first example has been split in 2 roots #1
19944 and #3 thanks to the more precise associated backtrace.
19945
19946 @end ifclear
19947
19948 @node Stack Related Facilities
19949 @chapter Stack Related Facilities
19950
19951 @noindent
19952 This chapter describes some useful tools associated with stack
19953 checking and analysis. In
19954 particular, it deals with dynamic and static stack usage measurements.
19955
19956 @menu
19957 * Stack Overflow Checking::
19958 * Static Stack Usage Analysis::
19959 * Dynamic Stack Usage Analysis::
19960 @end menu
19961
19962 @node Stack Overflow Checking
19963 @section Stack Overflow Checking
19964 @cindex Stack Overflow Checking
19965 @cindex -fstack-check
19966
19967 @noindent
19968 For most operating systems, @command{gcc} does not perform stack overflow
19969 checking by default. This means that if the main environment task or
19970 some other task exceeds the available stack space, then unpredictable
19971 behavior will occur. Most native systems offer some level of protection by
19972 adding a guard page at the end of each task stack. This mechanism is usually
19973 not enough for dealing properly with stack overflow situations because
19974 a large local variable could ``jump'' above the guard page.
19975 Furthermore, when the
19976 guard page is hit, there may not be any space left on the stack for executing
19977 the exception propagation code. Enabling stack checking avoids
19978 such situations.
19979
19980 To activate stack checking, compile all units with the gcc option
19981 @option{-fstack-check}. For example:
19982
19983 @smallexample
19984 gcc -c -fstack-check package1.adb
19985 @end smallexample
19986
19987 @noindent
19988 Units compiled with this option will generate extra instructions to check
19989 that any use of the stack (for procedure calls or for declaring local
19990 variables in declare blocks) does not exceed the available stack space.
19991 If the space is exceeded, then a @code{Storage_Error} exception is raised.
19992
19993 For declared tasks, the stack size is controlled by the size
19994 given in an applicable @code{Storage_Size} pragma or by the value specified
19995 at bind time with @option{-d} (@pxref{Switches for gnatbind}) or is set to
19996 the default size as defined in the GNAT runtime otherwise.
19997
19998 For the environment task, the stack size depends on
19999 system defaults and is unknown to the compiler. Stack checking
20000 may still work correctly if a fixed
20001 size stack is allocated, but this cannot be guaranteed.
20002 @ifclear vms
20003 To ensure that a clean exception is signalled for stack
20004 overflow, set the environment variable
20005 @env{GNAT_STACK_LIMIT} to indicate the maximum
20006 stack area that can be used, as in:
20007 @cindex GNAT_STACK_LIMIT
20008
20009 @smallexample
20010 SET GNAT_STACK_LIMIT 1600
20011 @end smallexample
20012
20013 @noindent
20014 The limit is given in kilobytes, so the above declaration would
20015 set the stack limit of the environment task to 1.6 megabytes.
20016 Note that the only purpose of this usage is to limit the amount
20017 of stack used by the environment task. If it is necessary to
20018 increase the amount of stack for the environment task, then this
20019 is an operating systems issue, and must be addressed with the
20020 appropriate operating systems commands.
20021 @end ifclear
20022 @ifset vms
20023 To have a fixed size stack in the environment task, the stack must be put
20024 in the P0 address space and its size specified. Use these switches to
20025 create a p0 image:
20026
20027 @smallexample
20028 gnatmake my_progs -largs "-Wl,--opt=STACK=4000,/p0image"
20029 @end smallexample
20030
20031 @noindent
20032 The quotes are required to keep case. The number after @samp{STACK=} is the
20033 size of the environmental task stack in pagelets (512 bytes). In this example
20034 the stack size is about 2 megabytes.
20035
20036 @noindent
20037 A consequence of the @option{/p0image} qualifier is also to makes RMS buffers
20038 be placed in P0 space. Refer to @cite{HP OpenVMS Linker Utility Manual} for
20039 more details about the @option{/p0image} qualifier and the @option{stack}
20040 option.
20041 @end ifset
20042
20043 @node Static Stack Usage Analysis
20044 @section Static Stack Usage Analysis
20045 @cindex Static Stack Usage Analysis
20046 @cindex -fstack-usage
20047
20048 @noindent
20049 A unit compiled with @option{-fstack-usage} will generate an extra file
20050 that specifies
20051 the maximum amount of stack used, on a per-function basis.
20052 The file has the same
20053 basename as the target object file with a @file{.su} extension.
20054 Each line of this file is made up of three fields:
20055
20056 @itemize
20057 @item
20058 The name of the function.
20059 @item
20060 A number of bytes.
20061 @item
20062 One or more qualifiers: @code{static}, @code{dynamic}, @code{bounded}.
20063 @end itemize
20064
20065 The second field corresponds to the size of the known part of the function
20066 frame.
20067
20068 The qualifier @code{static} means that the function frame size
20069 is purely static.
20070 It usually means that all local variables have a static size.
20071 In this case, the second field is a reliable measure of the function stack
20072 utilization.
20073
20074 The qualifier @code{dynamic} means that the function frame size is not static.
20075 It happens mainly when some local variables have a dynamic size. When this
20076 qualifier appears alone, the second field is not a reliable measure
20077 of the function stack analysis. When it is qualified with @code{bounded}, it
20078 means that the second field is a reliable maximum of the function stack
20079 utilization.
20080
20081 @node Dynamic Stack Usage Analysis
20082 @section Dynamic Stack Usage Analysis
20083
20084 @noindent
20085 It is possible to measure the maximum amount of stack used by a task, by
20086 adding a switch to @command{gnatbind}, as:
20087
20088 @smallexample
20089 $ gnatbind -u0 file
20090 @end smallexample
20091
20092 @noindent
20093 With this option, at each task termination, its stack usage is output on
20094 @file{stderr}.
20095 It is not always convenient to output the stack usage when the program
20096 is still running. Hence, it is possible to delay this output until program
20097 termination. for a given number of tasks specified as the argument of the
20098 @option{-u} option. For instance:
20099
20100 @smallexample
20101 $ gnatbind -u100 file
20102 @end smallexample
20103
20104 @noindent
20105 will buffer the stack usage information of the first 100 tasks to terminate and
20106 output this info at program termination. Results are displayed in four
20107 columns:
20108
20109 @noindent
20110 Index | Task Name | Stack Size | Actual Use [min - max]
20111
20112 @noindent
20113 where:
20114
20115 @table @emph
20116 @item Index
20117 is a number associated with each task.
20118
20119 @item Task Name
20120 is the name of the task analyzed.
20121
20122 @item Stack Size
20123 is the maximum size for the stack.
20124
20125 @item Actual Use
20126 is the measure done by the stack analyzer. In order to prevent overflow,
20127 the stack is not entirely analyzed, and it's not possible to know exactly how
20128 much has actually been used. The real amount of stack used is between the min
20129 and max values.
20130
20131 @end table
20132
20133 @noindent
20134 The environment task stack, e.g., the stack that contains the main unit, is
20135 only processed when the environment variable GNAT_STACK_LIMIT is set.
20136
20137
20138 @c *********************************
20139 @c * GNATCHECK *
20140 @c *********************************
20141 @node Verifying Properties Using gnatcheck
20142 @chapter Verifying Properties Using @command{gnatcheck}
20143 @findex gnatcheck
20144 @cindex @command{gnatcheck}
20145
20146 @noindent
20147 The @command{gnatcheck} tool is an ASIS-based utility that checks properties
20148 of Ada source files according to a given set of semantic rules.
20149 @cindex ASIS
20150
20151 In order to check compliance with a given rule, @command{gnatcheck} has to
20152 semantically analyze the Ada sources.
20153 Therefore, checks can only be performed on
20154 legal Ada units. Moreover, when a unit depends semantically upon units located
20155 outside the current directory, the source search path has to be provided when
20156 calling @command{gnatcheck}, either through a specified project file or
20157 through @command{gnatcheck} switches as described below.
20158
20159 A number of rules are predefined in @command{gnatcheck} and are described
20160 later in this chapter.
20161 You can also add new rules, by modifying the @command{gnatcheck} code and
20162 rebuilding the tool. In order to add a simple rule making some local checks,
20163 a small amount of straightforward ASIS-based programming is usually needed.
20164
20165 Project support for @command{gnatcheck} is provided by the GNAT
20166 driver (see @ref{The GNAT Driver and Project Files}).
20167
20168 Invoking @command{gnatcheck} on the command line has the form:
20169
20170 @smallexample
20171 $ gnatcheck @ovar{switches} @{@var{filename}@}
20172 @r{[}^-files^/FILES^=@{@var{arg_list_filename}@}@r{]}
20173 @r{[}-cargs @var{gcc_switches}@r{]} @r{[}-rules @var{rule_options}@r{]}
20174 @end smallexample
20175
20176 @noindent
20177 where
20178 @itemize @bullet
20179 @item
20180 @var{switches} specify the general tool options
20181
20182 @item
20183 Each @var{filename} is the name (including the extension) of a source
20184 file to process. ``Wildcards'' are allowed, and
20185 the file name may contain path information.
20186
20187 @item
20188 Each @var{arg_list_filename} is the name (including the extension) of a text
20189 file containing the names of the source files to process, separated by spaces
20190 or line breaks.
20191
20192 @item
20193 @var{gcc_switches} is a list of switches for
20194 @command{gcc}. They will be passed on to all compiler invocations made by
20195 @command{gnatcheck} to generate the ASIS trees. Here you can provide
20196 @option{^-I^/INCLUDE_DIRS=^} switches to form the source search path,
20197 and use the @option{-gnatec} switch to set the configuration file.
20198
20199 @item
20200 @var{rule_options} is a list of options for controlling a set of
20201 rules to be checked by @command{gnatcheck} (@pxref{gnatcheck Rule Options}).
20202 @end itemize
20203
20204 @noindent
20205 Either a @file{@var{filename}} or an @file{@var{arg_list_filename}} must be supplied.
20206
20207 @menu
20208 * Format of the Report File::
20209 * General gnatcheck Switches::
20210 * gnatcheck Rule Options::
20211 * Adding the Results of Compiler Checks to gnatcheck Output::
20212 * Project-Wide Checks::
20213 * Predefined Rules::
20214 @end menu
20215
20216 @node Format of the Report File
20217 @section Format of the Report File
20218 @cindex Report file (for @code{gnatcheck})
20219
20220 @noindent
20221 The @command{gnatcheck} tool outputs on @file{stdout} all messages concerning
20222 rule violations.
20223 It also creates, in the current
20224 directory, a text file named @file{^gnatcheck.out^GNATCHECK.OUT^} that
20225 contains the complete report of the last gnatcheck run. This report contains:
20226 @itemize @bullet
20227 @item a list of the Ada source files being checked,
20228 @item a list of enabled and disabled rules,
20229 @item a list of the diagnostic messages, ordered in three different ways
20230 and collected in three separate
20231 sections. Section 1 contains the raw list of diagnostic messages. It
20232 corresponds to the output going to @file{stdout}. Section 2 contains
20233 messages ordered by rules.
20234 Section 3 contains messages ordered by source files.
20235 @end itemize
20236
20237 @node General gnatcheck Switches
20238 @section General @command{gnatcheck} Switches
20239
20240 @noindent
20241 The following switches control the general @command{gnatcheck} behavior
20242
20243 @table @option
20244 @c !sort!
20245 @cindex @option{^-a^/ALL^} (@command{gnatcheck})
20246 @item ^-a^/ALL^
20247 Process all units including those with read-only ALI files such as
20248 those from GNAT Run-Time library.
20249
20250 @ifclear vms
20251 @ignore
20252 @cindex @option{-d} (@command{gnatcheck})
20253 @item -d
20254 Debug mode
20255 @end ignore
20256
20257 @cindex @option{-dd} (@command{gnatcheck})
20258 @item -dd
20259 Progress indicator mode (for use in GPS)
20260 @end ifclear
20261
20262 @cindex @option{^-h^/HELP^} (@command{gnatcheck})
20263 @item ^-h^/HELP^
20264 List the predefined and user-defined rules. For more details see
20265 @ref{Predefined Rules}.
20266
20267 @cindex @option{^-l^/LOCS^} (@command{gnatcheck})
20268 @item ^-l^/LOCS^
20269 Use full source locations references in the report file. For a construct from
20270 a generic instantiation a full source location is a chain from the location
20271 of this construct in the generic unit to the place where this unit is
20272 instantiated.
20273
20274 @cindex @option{^-q^/QUIET^} (@command{gnatcheck})
20275 @item ^-q^/QUIET^
20276 Quiet mode. All the diagnoses about rule violations are placed in the
20277 @command{gnatcheck} report file only, without duplicating in @file{stdout}.
20278
20279 @cindex @option{^-s^/SHORT^} (@command{gnatcheck})
20280 @item ^-s^/SHORT^
20281 Short format of the report file (no version information, no list of applied
20282 rules, no list of checked sources is included)
20283
20284 @cindex @option{^-s1^/COMPILER_STYLE^} (@command{gnatcheck})
20285 @item ^-s1^/COMPILER_STYLE^
20286 Include the compiler-style section in the report file
20287
20288 @cindex @option{^-s2^/BY_RULES^} (@command{gnatcheck})
20289 @item ^-s2^/BY_RULES^
20290 Include the section containing diagnoses ordered by rules in the report file
20291
20292 @cindex @option{^-s3^/BY_FILES_BY_RULES^} (@command{gnatcheck})
20293 @item ^-s3^/BY_FILES_BY_RULES^
20294 Include the section containing diagnoses ordered by files and then by rules
20295 in the report file
20296
20297 @cindex @option{^-v^/VERBOSE^} (@command{gnatcheck})
20298 @item ^-v^/VERBOSE^
20299 Verbose mode; @command{gnatcheck} generates version information and then
20300 a trace of sources being processed.
20301
20302 @end table
20303
20304 @noindent
20305 Note that if any of the options @option{^-s1^/COMPILER_STYLE^},
20306 @option{^-s2^/BY_RULES^} or
20307 @option{^-s3^/BY_FILES_BY_RULES^} is specified,
20308 then the @command{gnatcheck} report file will only contain sections
20309 explicitly denoted by these options.
20310
20311 @node gnatcheck Rule Options
20312 @section @command{gnatcheck} Rule Options
20313
20314 @noindent
20315 The following options control the processing performed by
20316 @command{gnatcheck}.
20317
20318 @table @option
20319 @cindex @option{+ALL} (@command{gnatcheck})
20320 @item +ALL
20321 Turn all the rule checks ON.
20322
20323 @cindex @option{-ALL} (@command{gnatcheck})
20324 @item -ALL
20325 Turn all the rule checks OFF.
20326
20327 @cindex @option{+R} (@command{gnatcheck})
20328 @item +R@var{rule_id}@r{[}:@var{param}@r{]}
20329 Turn on the check for a specified rule with the specified parameter, if any.
20330 @var{rule_id} must be the identifier of one of the currently implemented rules
20331 (use @option{^-h^/HELP^} for the list of implemented rules). Rule identifiers
20332 are not case-sensitive. The @var{param} item must
20333 be a string representing a valid parameter(s) for the specified rule.
20334 If it contains any space characters then this string must be enclosed in
20335 quotation marks.
20336
20337 @cindex @option{-R} (@command{gnatcheck})
20338 @item -R@var{rule_id}@r{[}:@var{param}@r{]}
20339 Turn off the check for a specified rule with the specified parameter, if any.
20340
20341 @cindex @option{-from} (@command{gnatcheck})
20342 @item -from=@var{rule_option_filename}
20343 Read the rule options from the text file @var{rule_option_filename}, referred as
20344 ``rule file'' below.
20345
20346 @end table
20347
20348 @noindent
20349 The default behavior is that all the rule checks are enabled, except for
20350 the checks performed by the compiler.
20351 @ignore
20352 and the checks associated with the
20353 global rules.
20354 @end ignore
20355
20356 A rule file is a text file containing a set of rule options.
20357 @cindex Rule file (for @code{gnatcheck})
20358 The file may contain empty lines and Ada-style comments (comment
20359 lines and end-of-line comments). The rule file has free format; that is,
20360 you do not have to start a new rule option on a new line.
20361
20362 A rule file may contain other @option{-from=@var{rule_option_filename}}
20363 options, each such option being replaced with the content of the
20364 corresponding rule file during the rule files processing. In case a
20365 cycle is detected (that is, @file{@var{rule_file_1}} reads rule options
20366 from @file{@var{rule_file_2}}, and @file{@var{rule_file_2}} reads
20367 (directly or indirectly) rule options from @file{@var{rule_file_1}}),
20368 the processing of rule files is interrupted and a part of their content
20369 is ignored.
20370
20371
20372 @node Adding the Results of Compiler Checks to gnatcheck Output
20373 @section Adding the Results of Compiler Checks to @command{gnatcheck} Output
20374
20375 @noindent
20376 The @command{gnatcheck} tool can include in the generated diagnostic messages
20377 and in
20378 the report file the results of the checks performed by the compiler. Though
20379 disabled by default, this effect may be obtained by using @option{+R} with
20380 the following rule identifiers and parameters:
20381
20382 @table @option
20383 @item Restrictions
20384 To record restrictions violations (that are performed by the compiler if the
20385 pragma @code{Restrictions} or @code{Restriction_Warnings} are given),
20386 use the rule named
20387 @code{Restrictions} with the same parameters as pragma
20388 @code{Restrictions} or @code{Restriction_Warnings}.
20389
20390 @item Style_Checks
20391 To record compiler style checks(@pxref{Style Checking}), use the rule named
20392 @code{Style_Checks}. A parameter of this rule can be either @code{All_Checks},
20393 which enables all the standard style checks that corresponds to @option{-gnatyy}
20394 GNAT style check option, or a string that has exactly the same
20395 structure and semantics as the @code{string_LITERAL} parameter of GNAT pragma
20396 @code{Style_Checks} (for further information about this pragma,
20397 @pxref{Pragma Style_Checks,,, gnat_rm, GNAT Reference Manual}).
20398
20399 @item Warnings
20400 To record compiler warnings (@pxref{Warning Message Control}), use the rule
20401 named @code{Warnings} with a parameter that is a valid
20402 @i{static_string_expression} argument of GNAT pragma @code{Warnings}
20403 (for further information about this pragma, @pxref{Pragma Warnings,,,
20404 gnat_rm, GNAT Reference Manual}). Note, that in case of gnatcheck
20405 's' parameter, that corresponds to the GNAT @option{-gnatws} option, disables
20406 all the specific warnings, but not suppresses the warning mode,
20407 and 'e' parameter, corresponding to @option{-gnatwe} that means
20408 "treat warnings as errors", does not have any effect.
20409
20410 @end table
20411
20412 To disable a specific restriction check, use @code{-RStyle_Checks} gnatcheck
20413 option with the corresponding restriction name as a parameter. @code{-R} is
20414 not available for @code{Style_Checks} and @code{Warnings} options, to disable
20415 warnings and style checks, use the corresponding warning and style options.
20416
20417 @node Project-Wide Checks
20418 @section Project-Wide Checks
20419 @cindex Project-wide checks (for @command{gnatcheck})
20420
20421 @noindent
20422 In order to perform checks on all units of a given project, you can use
20423 the GNAT driver along with the @option{-P} option:
20424 @smallexample
20425 gnat check -Pproj -rules -from=my_rules
20426 @end smallexample
20427
20428 @noindent
20429 If the project @code{proj} depends upon other projects, you can perform
20430 checks on the project closure using the @option{-U} option:
20431 @smallexample
20432 gnat check -Pproj -U -rules -from=my_rules
20433 @end smallexample
20434
20435 @noindent
20436 Finally, if not all the units are relevant to a particular main
20437 program in the project closure, you can perform checks for the set
20438 of units needed to create a given main program (unit closure) using
20439 the @option{-U} option followed by the name of the main unit:
20440 @smallexample
20441 gnat check -Pproj -U main -rules -from=my_rules
20442 @end smallexample
20443
20444
20445 @node Predefined Rules
20446 @section Predefined Rules
20447 @cindex Predefined rules (for @command{gnatcheck})
20448
20449 @ignore
20450 @c (Jan 2007) Since the global rules are still under development and are not
20451 @c documented, there is no point in explaining the difference between
20452 @c global and local rules
20453 @noindent
20454 A rule in @command{gnatcheck} is either local or global.
20455 A @emph{local rule} is a rule that applies to a well-defined section
20456 of a program and that can be checked by analyzing only this section.
20457 A @emph{global rule} requires analysis of some global properties of the
20458 whole program (mostly related to the program call graph).
20459 As of @value{NOW}, the implementation of global rules should be
20460 considered to be at a preliminary stage. You can use the
20461 @option{+GLOBAL} option to enable all the global rules, and the
20462 @option{-GLOBAL} rule option to disable all the global rules.
20463
20464 All the global rules in the list below are
20465 so indicated by marking them ``GLOBAL''.
20466 This +GLOBAL and -GLOBAL options are not
20467 included in the list of gnatcheck options above, because at the moment they
20468 are considered as a temporary debug options.
20469
20470 @command{gnatcheck} performs rule checks for generic
20471 instances only for global rules. This limitation may be relaxed in a later
20472 release.
20473 @end ignore
20474
20475 @noindent
20476 The following subsections document the rules implemented in
20477 @command{gnatcheck}.
20478 The subsection title is the same as the rule identifier, which may be
20479 used as a parameter of the @option{+R} or @option{-R} options.
20480
20481
20482 @menu
20483 * Abstract_Type_Declarations::
20484 * Anonymous_Arrays::
20485 * Anonymous_Subtypes::
20486 * Blocks::
20487 * Boolean_Relational_Operators::
20488 @ignore
20489 * Ceiling_Violations::
20490 @end ignore
20491 * Controlled_Type_Declarations::
20492 * Declarations_In_Blocks::
20493 * Default_Parameters::
20494 * Discriminated_Records::
20495 * Enumeration_Ranges_In_CASE_Statements::
20496 * Exceptions_As_Control_Flow::
20497 * EXIT_Statements_With_No_Loop_Name::
20498 * Expanded_Loop_Exit_Names::
20499 * Explicit_Full_Discrete_Ranges::
20500 * Float_Equality_Checks::
20501 * Forbidden_Pragmas::
20502 * Function_Style_Procedures::
20503 * Generics_In_Subprograms::
20504 * GOTO_Statements::
20505 * Implicit_IN_Mode_Parameters::
20506 * Implicit_SMALL_For_Fixed_Point_Types::
20507 * Improperly_Located_Instantiations::
20508 * Improper_Returns::
20509 * Library_Level_Subprograms::
20510 * Local_Packages::
20511 @ignore
20512 * Improperly_Called_Protected_Entries::
20513 @end ignore
20514 * Metrics_Violation::
20515 * Misnamed_Identifiers::
20516 * Multiple_Entries_In_Protected_Definitions::
20517 * Name_Clashes::
20518 * Non_Qualified_Aggregates::
20519 * Non_Short_Circuit_Operators::
20520 * Non_SPARK_Attributes::
20521 * Non_Tagged_Derived_Types::
20522 * Non_Visible_Exceptions::
20523 * Numeric_Literals::
20524 * OTHERS_In_Aggregates::
20525 * OTHERS_In_CASE_Statements::
20526 * OTHERS_In_Exception_Handlers::
20527 * Outer_Loop_Exits::
20528 * Overloaded_Operators::
20529 * Overly_Nested_Control_Structures::
20530 * Parameters_Out_Of_Order::
20531 * Positional_Actuals_For_Defaulted_Generic_Parameters::
20532 * Positional_Actuals_For_Defaulted_Parameters::
20533 * Positional_Components::
20534 * Positional_Generic_Parameters::
20535 * Positional_Parameters::
20536 * Predefined_Numeric_Types::
20537 * Raising_External_Exceptions::
20538 * Raising_Predefined_Exceptions::
20539 * Separate_Numeric_Error_Handlers::
20540 @ignore
20541 * Recursion::
20542 * Side_Effect_Functions::
20543 @end ignore
20544 * Slices::
20545 * Unassigned_OUT_Parameters::
20546 * Uncommented_BEGIN_In_Package_Bodies::
20547 * Unconstrained_Array_Returns::
20548 * Universal_Ranges::
20549 * Unnamed_Blocks_And_Loops::
20550 @ignore
20551 * Unused_Subprograms::
20552 @end ignore
20553 * USE_PACKAGE_Clauses::
20554 * Volatile_Objects_Without_Address_Clauses::
20555 @end menu
20556
20557
20558 @node Abstract_Type_Declarations
20559 @subsection @code{Abstract_Type_Declarations}
20560 @cindex @code{Abstract_Type_Declarations} rule (for @command{gnatcheck})
20561
20562 @noindent
20563 Flag all declarations of abstract types. For an abstract private
20564 type, both the private and full type declarations are flagged.
20565
20566 This rule has no parameters.
20567
20568
20569 @node Anonymous_Arrays
20570 @subsection @code{Anonymous_Arrays}
20571 @cindex @code{Anonymous_Arrays} rule (for @command{gnatcheck})
20572
20573 @noindent
20574 Flag all anonymous array type definitions (by Ada semantics these can only
20575 occur in object declarations).
20576
20577 This rule has no parameters.
20578
20579 @node Anonymous_Subtypes
20580 @subsection @code{Anonymous_Subtypes}
20581 @cindex @code{Anonymous_Subtypes} rule (for @command{gnatcheck})
20582
20583 @noindent
20584 Flag all uses of anonymous subtypes. A use of an anonymous subtype is
20585 any instance of a subtype indication with a constraint, other than one
20586 that occurs immediately within a subtype declaration. Any use of a range
20587 other than as a constraint used immediately within a subtype declaration
20588 is considered as an anonymous subtype.
20589
20590 An effect of this rule is that @code{for} loops such as the following are
20591 flagged (since @code{1..N} is formally a ``range''):
20592
20593 @smallexample @c ada
20594 for I in 1 .. N loop
20595 @dots{}
20596 end loop;
20597 @end smallexample
20598
20599 @noindent
20600 Declaring an explicit subtype solves the problem:
20601
20602 @smallexample @c ada
20603 subtype S is Integer range 1..N;
20604 @dots{}
20605 for I in S loop
20606 @dots{}
20607 end loop;
20608 @end smallexample
20609
20610 @noindent
20611 This rule has no parameters.
20612
20613 @node Blocks
20614 @subsection @code{Blocks}
20615 @cindex @code{Blocks} rule (for @command{gnatcheck})
20616
20617 @noindent
20618 Flag each block statement.
20619
20620 This rule has no parameters.
20621
20622 @node Boolean_Relational_Operators
20623 @subsection @code{Boolean_Relational_Operators}
20624 @cindex @code{Boolean_Relational_Operators} rule (for @command{gnatcheck})
20625
20626 @noindent
20627 Flag each call to a predefined relational operator (``<'', ``>'', ``<='',
20628 ``>='', ``='' and ``/='') for the predefined Boolean type.
20629 (This rule is useful in enforcing the SPARK language restrictions.)
20630
20631 Calls to predefined relational operators of any type derived from
20632 @code{Standard.Boolean} are not detected. Calls to user-defined functions
20633 with these designators, and uses of operators that are renamings
20634 of the predefined relational operators for @code{Standard.Boolean},
20635 are likewise not detected.
20636
20637 This rule has no parameters.
20638
20639 @ignore
20640 @node Ceiling_Violations
20641 @subsection @code{Ceiling_Violations} (under construction, GLOBAL)
20642 @cindex @code{Ceiling_Violations} rule (for @command{gnatcheck})
20643
20644 @noindent
20645 Flag invocations of a protected operation by a task whose priority exceeds
20646 the protected object's ceiling.
20647
20648 As of @value{NOW}, this rule has the following limitations:
20649
20650 @itemize @bullet
20651
20652 @item
20653 We consider only pragmas Priority and Interrupt_Priority as means to define
20654 a task/protected operation priority. We do not consider the effect of using
20655 Ada.Dynamic_Priorities.Set_Priority procedure;
20656
20657 @item
20658 We consider only base task priorities, and no priority inheritance. That is,
20659 we do not make a difference between calls issued during task activation and
20660 execution of the sequence of statements from task body;
20661
20662 @item
20663 Any situation when the priority of protected operation caller is set by a
20664 dynamic expression (that is, the corresponding Priority or
20665 Interrupt_Priority pragma has a non-static expression as an argument) we
20666 treat as a priority inconsistency (and, therefore, detect this situation).
20667 @end itemize
20668
20669 @noindent
20670 At the moment the notion of the main subprogram is not implemented in
20671 gnatcheck, so any pragma Priority in a library level subprogram body (in case
20672 if this subprogram can be a main subprogram of a partition) changes the
20673 priority of an environment task. So if we have more then one such pragma in
20674 the set of processed sources, the pragma that is processed last, defines the
20675 priority of an environment task.
20676
20677 This rule has no parameters.
20678 @end ignore
20679
20680 @node Controlled_Type_Declarations
20681 @subsection @code{Controlled_Type_Declarations}
20682 @cindex @code{Controlled_Type_Declarations} rule (for @command{gnatcheck})
20683
20684 @noindent
20685 Flag all declarations of controlled types. A declaration of a private type
20686 is flagged if its full declaration declares a controlled type. A declaration
20687 of a derived type is flagged if its ancestor type is controlled. Subtype
20688 declarations are not checked. A declaration of a type that itself is not a
20689 descendant of a type declared in @code{Ada.Finalization} but has a controlled
20690 component is not checked.
20691
20692 This rule has no parameters.
20693
20694
20695
20696 @node Declarations_In_Blocks
20697 @subsection @code{Declarations_In_Blocks}
20698 @cindex @code{Declarations_In_Blocks} rule (for @command{gnatcheck})
20699
20700 @noindent
20701 Flag all block statements containing local declarations. A @code{declare}
20702 block with an empty @i{declarative_part} or with a @i{declarative part}
20703 containing only pragmas and/or @code{use} clauses is not flagged.
20704
20705 This rule has no parameters.
20706
20707
20708 @node Default_Parameters
20709 @subsection @code{Default_Parameters}
20710 @cindex @code{Default_Parameters} rule (for @command{gnatcheck})
20711
20712 @noindent
20713 Flag all default expressions for subprogram parameters. Parameter
20714 declarations of formal and generic subprograms are also checked.
20715
20716 This rule has no parameters.
20717
20718
20719 @node Discriminated_Records
20720 @subsection @code{Discriminated_Records}
20721 @cindex @code{Discriminated_Records} rule (for @command{gnatcheck})
20722
20723 @noindent
20724 Flag all declarations of record types with discriminants. Only the
20725 declarations of record and record extension types are checked. Incomplete,
20726 formal, private, derived and private extension type declarations are not
20727 checked. Task and protected type declarations also are not checked.
20728
20729 This rule has no parameters.
20730
20731
20732 @node Enumeration_Ranges_In_CASE_Statements
20733 @subsection @code{Enumeration_Ranges_In_CASE_Statements}
20734 @cindex @code{Enumeration_Ranges_In_CASE_Statements} (for @command{gnatcheck})
20735
20736 @noindent
20737 Flag each use of a range of enumeration literals as a choice in a
20738 @code{case} statement.
20739 All forms for specifying a range (explicit ranges
20740 such as @code{A .. B}, subtype marks and @code{'Range} attributes) are flagged.
20741 An enumeration range is
20742 flagged even if contains exactly one enumeration value or no values at all. A
20743 type derived from an enumeration type is considered as an enumeration type.
20744
20745 This rule helps prevent maintenance problems arising from adding an
20746 enumeration value to a type and having it implicitly handled by an existing
20747 @code{case} statement with an enumeration range that includes the new literal.
20748
20749 This rule has no parameters.
20750
20751
20752 @node Exceptions_As_Control_Flow
20753 @subsection @code{Exceptions_As_Control_Flow}
20754 @cindex @code{Exceptions_As_Control_Flow} (for @command{gnatcheck})
20755
20756 @noindent
20757 Flag each place where an exception is explicitly raised and handled in the
20758 same subprogram body. A @code{raise} statement in an exception handler,
20759 package body, task body or entry body is not flagged.
20760
20761 The rule has no parameters.
20762
20763 @node EXIT_Statements_With_No_Loop_Name
20764 @subsection @code{EXIT_Statements_With_No_Loop_Name}
20765 @cindex @code{EXIT_Statements_With_No_Loop_Name} (for @command{gnatcheck})
20766
20767 @noindent
20768 Flag each @code{exit} statement that does not specify the name of the loop
20769 being exited.
20770
20771 The rule has no parameters.
20772
20773
20774 @node Expanded_Loop_Exit_Names
20775 @subsection @code{Expanded_Loop_Exit_Names}
20776 @cindex @code{Expanded_Loop_Exit_Names} rule (for @command{gnatcheck})
20777
20778 @noindent
20779 Flag all expanded loop names in @code{exit} statements.
20780
20781 This rule has no parameters.
20782
20783 @node Explicit_Full_Discrete_Ranges
20784 @subsection @code{Explicit_Full_Discrete_Ranges}
20785 @cindex @code{Explicit_Full_Discrete_Ranges} rule (for @command{gnatcheck})
20786
20787 @noindent
20788 Flag each discrete range that has the form @code{A'First .. A'Last}.
20789
20790 This rule has no parameters.
20791
20792 @node Float_Equality_Checks
20793 @subsection @code{Float_Equality_Checks}
20794 @cindex @code{Float_Equality_Checks} rule (for @command{gnatcheck})
20795
20796 @noindent
20797 Flag all calls to the predefined equality operations for floating-point types.
20798 Both ``@code{=}'' and ``@code{/=}'' operations are checked.
20799 User-defined equality operations are not flagged, nor are ``@code{=}''
20800 and ``@code{/=}'' operations for fixed-point types.
20801
20802 This rule has no parameters.
20803
20804
20805 @node Forbidden_Pragmas
20806 @subsection @code{Forbidden_Pragmas}
20807 @cindex @code{Forbidden_Pragmas} rule (for @command{gnatcheck})
20808
20809 @noindent
20810 Flag each use of the specified pragmas. The pragmas to be detected
20811 are named in the rule's parameters.
20812
20813 This rule has the following parameters:
20814
20815 @itemize @bullet
20816 @item For the @option{+R} option
20817
20818 @table @asis
20819 @item @emph{Pragma_Name}
20820 Adds the specified pragma to the set of pragmas to be
20821 checked and sets the checks for all the specified pragmas
20822 ON. @emph{Pragma_Name} is treated as a name of a pragma. If it
20823 does not correspond to any pragma name defined in the Ada
20824 standard or to the name of a GNAT-specific pragma defined
20825 in @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference
20826 Manual}, it is treated as the name of unknown pragma.
20827
20828 @item @code{GNAT}
20829 All the GNAT-specific pragmas are detected; this sets
20830 the checks for all the specified pragmas ON.
20831
20832 @item @code{ALL}
20833 All pragmas are detected; this sets the rule ON.
20834 @end table
20835
20836 @item For the @option{-R} option
20837 @table @asis
20838 @item @emph{Pragma_Name}
20839 Removes the specified pragma from the set of pragmas to be
20840 checked without affecting checks for
20841 other pragmas. @emph{Pragma_Name} is treated as a name
20842 of a pragma. If it does not correspond to any pragma
20843 defined in the Ada standard or to any name defined in
20844 @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference Manual},
20845 this option is treated as turning OFF detection of all unknown pragmas.
20846
20847 @item GNAT
20848 Turn OFF detection of all GNAT-specific pragmas
20849
20850 @item ALL
20851 Clear the list of the pragmas to be detected and
20852 turn the rule OFF.
20853 @end table
20854 @end itemize
20855
20856 @noindent
20857 Parameters are not case sensitive. If @emph{Pragma_Name} does not have
20858 the syntax of an Ada identifier and therefore can not be considered
20859 as a pragma name, a diagnostic message is generated and the corresponding
20860 parameter is ignored.
20861
20862 When more then one parameter is given in the same rule option, the parameters
20863 must be separated by a comma.
20864
20865 If more then one option for this rule is specified for the @command{gnatcheck}
20866 call, a new option overrides the previous one(s).
20867
20868 The @option{+R} option with no parameters turns the rule ON with the set of
20869 pragmas to be detected defined by the previous rule options.
20870 (By default this set is empty, so if the only option specified for the rule is
20871 @option{+RForbidden_Pragmas} (with
20872 no parameter), then the rule is enabled, but it does not detect anything).
20873 The @option{-R} option with no parameter turns the rule OFF, but it does not
20874 affect the set of pragmas to be detected.
20875
20876
20877
20878
20879 @node Function_Style_Procedures
20880 @subsection @code{Function_Style_Procedures}
20881 @cindex @code{Function_Style_Procedures} rule (for @command{gnatcheck})
20882
20883 @noindent
20884 Flag each procedure that can be rewritten as a function. A procedure can be
20885 converted into a function if it has exactly one parameter of mode @code{out}
20886 and no parameters of mode @code{in out}. Procedure declarations,
20887 formal procedure declarations, and generic procedure declarations are always
20888 checked. Procedure
20889 bodies and body stubs are flagged only if they do not have corresponding
20890 separate declarations. Procedure renamings and procedure instantiations are
20891 not flagged.
20892
20893 If a procedure can be rewritten as a function, but its @code{out} parameter is
20894 of a limited type, it is not flagged.
20895
20896 Protected procedures are not flagged. Null procedures also are not flagged.
20897
20898 This rule has no parameters.
20899
20900
20901 @node Generics_In_Subprograms
20902 @subsection @code{Generics_In_Subprograms}
20903 @cindex @code{Generics_In_Subprograms} rule (for @command{gnatcheck})
20904
20905 @noindent
20906 Flag each declaration of a generic unit in a subprogram. Generic
20907 declarations in the bodies of generic subprograms are also flagged.
20908 A generic unit nested in another generic unit is not flagged.
20909 If a generic unit is
20910 declared in a local package that is declared in a subprogram body, the
20911 generic unit is flagged.
20912
20913 This rule has no parameters.
20914
20915
20916 @node GOTO_Statements
20917 @subsection @code{GOTO_Statements}
20918 @cindex @code{GOTO_Statements} rule (for @command{gnatcheck})
20919
20920 @noindent
20921 Flag each occurrence of a @code{goto} statement.
20922
20923 This rule has no parameters.
20924
20925
20926 @node Implicit_IN_Mode_Parameters
20927 @subsection @code{Implicit_IN_Mode_Parameters}
20928 @cindex @code{Implicit_IN_Mode_Parameters} rule (for @command{gnatcheck})
20929
20930 @noindent
20931 Flag each occurrence of a formal parameter with an implicit @code{in} mode.
20932 Note that @code{access} parameters, although they technically behave
20933 like @code{in} parameters, are not flagged.
20934
20935 This rule has no parameters.
20936
20937
20938 @node Implicit_SMALL_For_Fixed_Point_Types
20939 @subsection @code{Implicit_SMALL_For_Fixed_Point_Types}
20940 @cindex @code{Implicit_SMALL_For_Fixed_Point_Types} rule (for @command{gnatcheck})
20941
20942 @noindent
20943 Flag each fixed point type declaration that lacks an explicit
20944 representation clause to define its @code{'Small} value.
20945 Since @code{'Small} can be defined only for ordinary fixed point types,
20946 decimal fixed point type declarations are not checked.
20947
20948 This rule has no parameters.
20949
20950
20951 @node Improperly_Located_Instantiations
20952 @subsection @code{Improperly_Located_Instantiations}
20953 @cindex @code{Improperly_Located_Instantiations} rule (for @command{gnatcheck})
20954
20955 @noindent
20956 Flag all generic instantiations in library-level package specs
20957 (including library generic packages) and in all subprogram bodies.
20958
20959 Instantiations in task and entry bodies are not flagged. Instantiations in the
20960 bodies of protected subprograms are flagged.
20961
20962 This rule has no parameters.
20963
20964
20965
20966 @node Improper_Returns
20967 @subsection @code{Improper_Returns}
20968 @cindex @code{Improper_Returns} rule (for @command{gnatcheck})
20969
20970 @noindent
20971 Flag each explicit @code{return} statement in procedures, and
20972 multiple @code{return} statements in functions.
20973 Diagnostic messages are generated for all @code{return} statements
20974 in a procedure (thus each procedure must be written so that it
20975 returns implicitly at the end of its statement part),
20976 and for all @code{return} statements in a function after the first one.
20977 This rule supports the stylistic convention that each subprogram
20978 should have no more than one point of normal return.
20979
20980 This rule has no parameters.
20981
20982
20983 @node Library_Level_Subprograms
20984 @subsection @code{Library_Level_Subprograms}
20985 @cindex @code{Library_Level_Subprograms} rule (for @command{gnatcheck})
20986
20987 @noindent
20988 Flag all library-level subprograms (including generic subprogram instantiations).
20989
20990 This rule has no parameters.
20991
20992
20993 @node Local_Packages
20994 @subsection @code{Local_Packages}
20995 @cindex @code{Local_Packages} rule (for @command{gnatcheck})
20996
20997 @noindent
20998 Flag all local packages declared in package and generic package
20999 specs.
21000 Local packages in bodies are not flagged.
21001
21002 This rule has no parameters.
21003
21004 @ignore
21005 @node Improperly_Called_Protected_Entries
21006 @subsection @code{Improperly_Called_Protected_Entries} (under construction, GLOBAL)
21007 @cindex @code{Improperly_Called_Protected_Entries} rule (for @command{gnatcheck})
21008
21009 @noindent
21010 Flag each protected entry that can be called from more than one task.
21011
21012 This rule has no parameters.
21013 @end ignore
21014
21015 @node Metrics_Violation
21016 @subsection @code{Metrics_Violation}
21017 @cindex @code{Metrics} rule (for @command{gnatcheck})
21018
21019 @noindent
21020 This is an umbrella rule for a set of metrics-based checks. The parameters of
21021 the rule specify which metrics should be checked, and a bound (upper or lower,
21022 depending on the metric) for each specified metric. A construct is
21023 flagged if a specified metric can be computed for it, and the resulting value
21024 is higher then the upper bound (or less than the lower bound) specified.
21025
21026 This rule has the following parameters:
21027
21028 @itemize @bullet
21029 @item
21030 For the @option{+R} option:
21031 @table @code
21032 @item @i{Metric_Check_Name} < @i{LowerBound}
21033 Turns the check for the specified metric ON and specifies the lower bound
21034 for a given metric check
21035
21036 @item @i{Metric_Check_Name} > @i{UpperBound}
21037
21038 Turns the check for the specified metric ON and specifies the upper bound
21039 for a given metric check
21040 @end table
21041
21042 @item
21043 For the @option{-R} option:
21044 @table @code
21045 @item @i{Metric_Check_Name}
21046 Turns the check for the specified metric OFF
21047 @end table
21048 @end itemize
21049
21050 @noindent
21051 Parameters are not case-sensitive. @i{Metric_Check_Name} must be
21052 the name of a metric supported by the @code{Metrics_Violation} rule
21053 (see the table below),
21054 otherwise the parameter is ignored. Whether the upper or lower bound
21055 is specified for a given check, depends on the metric. If a
21056 parameter for the @option{+R} option specifies an invalid limit, a
21057 warning is issued and the parameter is ignored.
21058
21059 The @option{-R} option without parameters turns OFF all the previously enabled
21060 metric checks. the @option{+R} option without parameters turns ON all the
21061 metric checks that have been defined by previous @option{+R} options with
21062 valid parameters. @option{+R} option with a valid
21063 parameter also turns ON all the other metric checks that have been defined
21064 by previous @option{+R} options with valid parameters if they have been
21065 disabled by @option{-R} option without parameters.
21066
21067 By default no metrics checks are ON, so the @option{+R} option without
21068 parameters actually does not specify any check.
21069
21070 The following table shows the available metrics-based checks,
21071 including the constraint that must be satisfied by the bound that
21072 is specified for the check.
21073
21074 @multitable {@code{Cyclomatic_Complexity}}{Cyclomatic complexity}{Positive integer}
21075 @ifnothtml
21076 @headitem Check Name @tab Description @tab Bounds Value
21077 @end ifnothtml
21078 @ifhtml
21079 @item @b{Check Name} @tab @b{Description} @tab @b{Bounds Value}
21080 @end ifhtml
21081 @c Above conditional code is workaround to bug in texi2html (Feb 2008)
21082 @item @code{Essential_Complexity} @tab Essential complexity @tab Positive integer
21083 @item @code{Cyclomatic_Complexity} @tab Cyclomatic complexity @tab Positive integer
21084 @item @code{LSLOC} @tab Logical Source Lines of Code @tab Positive integer
21085 @end multitable
21086
21087 @noindent
21088 The meaning and the computed values for all these metrics are exactly
21089 the same as for the corresponding metrics in @command{gnatmetric}.
21090
21091 @emph{Example:} the rule
21092 @smallexample
21093 +RMetrics_Violation: Cyclomatic_Complexity > 7
21094 @end smallexample
21095 @noindent
21096 means that all bodies with cyclomatic complexity exceeding 7 will be flagged.
21097
21098 @node Misnamed_Identifiers
21099 @subsection @code{Misnamed_Identifiers}
21100 @cindex @code{Misnamed_Identifiers} rule (for @command{gnatcheck})
21101
21102 @noindent
21103 Flag the declaration of each identifier that does not have a suffix
21104 corresponding to the kind of entity being declared.
21105 The following declarations are checked:
21106
21107 @itemize @bullet
21108 @item
21109 type declarations
21110
21111 @item
21112 constant declarations (but not number declarations)
21113
21114 @item
21115 package renaming declarations (but not generic package renaming
21116 declarations)
21117 @end itemize
21118
21119 @noindent
21120 This rule may have parameters. When used without parameters, the rule enforces
21121 the following checks:
21122
21123 @itemize @bullet
21124 @item
21125 type-defining names end with @code{_T}, unless the type is an access type,
21126 in which case the suffix must be @code{_A}
21127 @item
21128 constant names end with @code{_C}
21129 @item
21130 names defining package renamings end with @code{_R}
21131 @end itemize
21132
21133 @noindent
21134 For a private or incomplete type declaration the following checks are
21135 made for the defining name suffix:
21136
21137 @itemize @bullet
21138 @item
21139 For an incomplete type declaration: if the corresponding full type
21140 declaration is available, the defining identifier from the full type
21141 declaration is checked, but the defining identifier from the incomplete type
21142 declaration is not; otherwise the defining identifier from the incomplete
21143 type declaration is checked against the suffix specified for type
21144 declarations.
21145
21146 @item
21147 For a private type declaration (including private extensions), the defining
21148 identifier from the private type declaration is checked against the type
21149 suffix (even if the corresponding full declaration is an access type
21150 declaration), and the defining identifier from the corresponding full type
21151 declaration is not checked.
21152 @end itemize
21153
21154 @noindent
21155 For a deferred constant, the defining name in the corresponding full constant
21156 declaration is not checked.
21157
21158 Defining names of formal types are not checked.
21159
21160 The rule may have the following parameters:
21161
21162 @itemize @bullet
21163 @item
21164 For the @option{+R} option:
21165 @table @code
21166 @item Default
21167 Sets the default listed above for all the names to be checked.
21168
21169 @item Type_Suffix=@emph{string}
21170 Specifies the suffix for a type name.
21171
21172 @item Access_Suffix=@emph{string}
21173 Specifies the suffix for an access type name. If
21174 this parameter is set, it overrides for access
21175 types the suffix set by the @code{Type_Suffix} parameter.
21176
21177 @item Constant_Suffix=@emph{string}
21178 Specifies the suffix for a constant name.
21179
21180 @item Renaming_Suffix=@emph{string}
21181 Specifies the suffix for a package renaming name.
21182 @end table
21183
21184 @item
21185 For the @option{-R} option:
21186 @table @code
21187 @item All_Suffixes
21188 Remove all the suffixes specified for the
21189 identifier suffix checks, whether by default or
21190 as specified by other rule parameters. All the
21191 checks for this rule are disabled as a result.
21192
21193 @item Type_Suffix
21194 Removes the suffix specified for types. This
21195 disables checks for types but does not disable
21196 any other checks for this rule (including the
21197 check for access type names if @code{Access_Suffix} is
21198 set).
21199
21200 @item Access_Suffix
21201 Removes the suffix specified for access types.
21202 This disables checks for access type names but
21203 does not disable any other checks for this rule.
21204 If @code{Type_Suffix} is set, access type names are
21205 checked as ordinary type names.
21206
21207 @item Constant_Suffix
21208 Removes the suffix specified for constants. This
21209 disables checks for constant names but does not
21210 disable any other checks for this rule.
21211
21212 @item Renaming_Suffix
21213 Removes the suffix specified for package
21214 renamings. This disables checks for package
21215 renamings but does not disable any other checks
21216 for this rule.
21217 @end table
21218 @end itemize
21219
21220 @noindent
21221 If more than one parameter is used, parameters must be separated by commas.
21222
21223 If more than one option is specified for the @command{gnatcheck} invocation,
21224 a new option overrides the previous one(s).
21225
21226 The @option{+RMisnamed_Identifiers} option (with no parameter) enables
21227 checks for all the
21228 name suffixes specified by previous options used for this rule.
21229
21230 The @option{-RMisnamed_Identifiers} option (with no parameter) disables
21231 all the checks but keeps
21232 all the suffixes specified by previous options used for this rule.
21233
21234 The @emph{string} value must be a valid suffix for an Ada identifier (after
21235 trimming all the leading and trailing space characters, if any).
21236 Parameters are not case sensitive, except the @emph{string} part.
21237
21238 If any error is detected in a rule parameter, the parameter is ignored.
21239 In such a case the options that are set for the rule are not
21240 specified.
21241
21242
21243
21244 @node Multiple_Entries_In_Protected_Definitions
21245 @subsection @code{Multiple_Entries_In_Protected_Definitions}
21246 @cindex @code{Multiple_Entries_In_Protected_Definitions} rule (for @command{gnatcheck})
21247
21248 @noindent
21249 Flag each protected definition (i.e., each protected object/type declaration)
21250 that defines more than one entry.
21251 Diagnostic messages are generated for all the entry declarations
21252 except the first one. An entry family is counted as one entry. Entries from
21253 the private part of the protected definition are also checked.
21254
21255 This rule has no parameters.
21256
21257 @node Name_Clashes
21258 @subsection @code{Name_Clashes}
21259 @cindex @code{Name_Clashes} rule (for @command{gnatcheck})
21260
21261 @noindent
21262 Check that certain names are not used as defining identifiers. To activate
21263 this rule, you need to supply a reference to the dictionary file(s) as a rule
21264 parameter(s) (more then one dictionary file can be specified). If no
21265 dictionary file is set, this rule will not cause anything to be flagged.
21266 Only defining occurrences, not references, are checked.
21267 The check is not case-sensitive.
21268
21269 This rule is enabled by default, but without setting any corresponding
21270 dictionary file(s); thus the default effect is to do no checks.
21271
21272 A dictionary file is a plain text file. The maximum line length for this file
21273 is 1024 characters. If the line is longer then this limit, extra characters
21274 are ignored.
21275
21276 Each line can be either an empty line, a comment line, or a line containing
21277 a list of identifiers separated by space or HT characters.
21278 A comment is an Ada-style comment (from @code{--} to end-of-line).
21279 Identifiers must follow the Ada syntax for identifiers.
21280 A line containing one or more identifiers may end with a comment.
21281
21282 @node Non_Qualified_Aggregates
21283 @subsection @code{Non_Qualified_Aggregates}
21284 @cindex @code{Non_Qualified_Aggregates} rule (for @command{gnatcheck})
21285
21286 @noindent
21287 Flag each non-qualified aggregate.
21288 A non-qualified aggregate is an
21289 aggregate that is not the expression of a qualified expression. A
21290 string literal is not considered an aggregate, but an array
21291 aggregate of a string type is considered as a normal aggregate.
21292 Aggregates of anonymous array types are not flagged.
21293
21294 This rule has no parameters.
21295
21296
21297 @node Non_Short_Circuit_Operators
21298 @subsection @code{Non_Short_Circuit_Operators}
21299 @cindex @code{Non_Short_Circuit_Operators} rule (for @command{gnatcheck})
21300
21301 @noindent
21302 Flag all calls to predefined @code{and} and @code{or} operators for
21303 any boolean type. Calls to
21304 user-defined @code{and} and @code{or} and to operators defined by renaming
21305 declarations are not flagged. Calls to predefined @code{and} and @code{or}
21306 operators for modular types or boolean array types are not flagged.
21307
21308 This rule has no parameters.
21309
21310
21311
21312 @node Non_SPARK_Attributes
21313 @subsection @code{Non_SPARK_Attributes}
21314 @cindex @code{Non_SPARK_Attributes} rule (for @command{gnatcheck})
21315
21316 @noindent
21317 The SPARK language defines the following subset of Ada 95 attribute
21318 designators as those that can be used in SPARK programs. The use of
21319 any other attribute is flagged.
21320
21321 @itemize @bullet
21322 @item @code{'Adjacent}
21323 @item @code{'Aft}
21324 @item @code{'Base}
21325 @item @code{'Ceiling}
21326 @item @code{'Component_Size}
21327 @item @code{'Compose}
21328 @item @code{'Copy_Sign}
21329 @item @code{'Delta}
21330 @item @code{'Denorm}
21331 @item @code{'Digits}
21332 @item @code{'Exponent}
21333 @item @code{'First}
21334 @item @code{'Floor}
21335 @item @code{'Fore}
21336 @item @code{'Fraction}
21337 @item @code{'Last}
21338 @item @code{'Leading_Part}
21339 @item @code{'Length}
21340 @item @code{'Machine}
21341 @item @code{'Machine_Emax}
21342 @item @code{'Machine_Emin}
21343 @item @code{'Machine_Mantissa}
21344 @item @code{'Machine_Overflows}
21345 @item @code{'Machine_Radix}
21346 @item @code{'Machine_Rounds}
21347 @item @code{'Max}
21348 @item @code{'Min}
21349 @item @code{'Model}
21350 @item @code{'Model_Emin}
21351 @item @code{'Model_Epsilon}
21352 @item @code{'Model_Mantissa}
21353 @item @code{'Model_Small}
21354 @item @code{'Modulus}
21355 @item @code{'Pos}
21356 @item @code{'Pred}
21357 @item @code{'Range}
21358 @item @code{'Remainder}
21359 @item @code{'Rounding}
21360 @item @code{'Safe_First}
21361 @item @code{'Safe_Last}
21362 @item @code{'Scaling}
21363 @item @code{'Signed_Zeros}
21364 @item @code{'Size}
21365 @item @code{'Small}
21366 @item @code{'Succ}
21367 @item @code{'Truncation}
21368 @item @code{'Unbiased_Rounding}
21369 @item @code{'Val}
21370 @item @code{'Valid}
21371 @end itemize
21372
21373 @noindent
21374 This rule has no parameters.
21375
21376
21377 @node Non_Tagged_Derived_Types
21378 @subsection @code{Non_Tagged_Derived_Types}
21379 @cindex @code{Non_Tagged_Derived_Types} rule (for @command{gnatcheck})
21380
21381 @noindent
21382 Flag all derived type declarations that do not have a record extension part.
21383
21384 This rule has no parameters.
21385
21386
21387
21388 @node Non_Visible_Exceptions
21389 @subsection @code{Non_Visible_Exceptions}
21390 @cindex @code{Non_Visible_Exceptions} rule (for @command{gnatcheck})
21391
21392 @noindent
21393 Flag constructs leading to the possibility of propagating an exception
21394 out of the scope in which the exception is declared.
21395 Two cases are detected:
21396
21397 @itemize @bullet
21398 @item
21399 An exception declaration in a subprogram body, task body or block
21400 statement is flagged if the body or statement does not contain a handler for
21401 that exception or a handler with an @code{others} choice.
21402
21403 @item
21404 A @code{raise} statement in an exception handler of a subprogram body,
21405 task body or block statement is flagged if it (re)raises a locally
21406 declared exception. This may occur under the following circumstances:
21407 @itemize @minus
21408 @item
21409 it explicitly raises a locally declared exception, or
21410 @item
21411 it does not specify an exception name (i.e., it is simply @code{raise;})
21412 and the enclosing handler contains a locally declared exception in its
21413 exception choices.
21414 @end itemize
21415 @end itemize
21416
21417 @noindent
21418 Renamings of local exceptions are not flagged.
21419
21420 This rule has no parameters.
21421
21422
21423 @node Numeric_Literals
21424 @subsection @code{Numeric_Literals}
21425 @cindex @code{Numeric_Literals} rule (for @command{gnatcheck})
21426
21427 @noindent
21428 Flag each use of a numeric literal in an index expression, and in any
21429 circumstance except for the following:
21430
21431 @itemize @bullet
21432 @item
21433 a literal occurring in the initialization expression for a constant
21434 declaration or a named number declaration, or
21435
21436 @item
21437 an integer literal that is less than or equal to a value
21438 specified by the @option{N} rule parameter.
21439 @end itemize
21440
21441 @noindent
21442 This rule may have the following parameters for the @option{+R} option:
21443
21444 @table @asis
21445 @item @emph{N}
21446 @emph{N} is an integer literal used as the maximal value that is not flagged
21447 (i.e., integer literals not exceeding this value are allowed)
21448
21449 @item @code{ALL}
21450 All integer literals are flagged
21451 @end table
21452
21453 @noindent
21454 If no parameters are set, the maximum unflagged value is 1.
21455
21456 The last specified check limit (or the fact that there is no limit at
21457 all) is used when multiple @option{+R} options appear.
21458
21459 The @option{-R} option for this rule has no parameters.
21460 It disables the rule but retains the last specified maximum unflagged value.
21461 If the @option{+R} option subsequently appears, this value is used as the
21462 threshold for the check.
21463
21464
21465 @node OTHERS_In_Aggregates
21466 @subsection @code{OTHERS_In_Aggregates}
21467 @cindex @code{OTHERS_In_Aggregates} rule (for @command{gnatcheck})
21468
21469 @noindent
21470 Flag each use of an @code{others} choice in extension aggregates.
21471 In record and array aggregates, an @code{others} choice is flagged unless
21472 it is used to refer to all components, or to all but one component.
21473
21474 If, in case of a named array aggregate, there are two associations, one
21475 with an @code{others} choice and another with a discrete range, the
21476 @code{others} choice is flagged even if the discrete range specifies
21477 exactly one component; for example, @code{(1..1 => 0, others => 1)}.
21478
21479 This rule has no parameters.
21480
21481 @node OTHERS_In_CASE_Statements
21482 @subsection @code{OTHERS_In_CASE_Statements}
21483 @cindex @code{OTHERS_In_CASE_Statements} rule (for @command{gnatcheck})
21484
21485 @noindent
21486 Flag any use of an @code{others} choice in a @code{case} statement.
21487
21488 This rule has no parameters.
21489
21490 @node OTHERS_In_Exception_Handlers
21491 @subsection @code{OTHERS_In_Exception_Handlers}
21492 @cindex @code{OTHERS_In_Exception_Handlers} rule (for @command{gnatcheck})
21493
21494 @noindent
21495 Flag any use of an @code{others} choice in an exception handler.
21496
21497 This rule has no parameters.
21498
21499
21500 @node Outer_Loop_Exits
21501 @subsection @code{Outer_Loop_Exits}
21502 @cindex @code{Outer_Loop_Exits} rule (for @command{gnatcheck})
21503
21504 @noindent
21505 Flag each @code{exit} statement containing a loop name that is not the name
21506 of the immediately enclosing @code{loop} statement.
21507
21508 This rule has no parameters.
21509
21510
21511 @node Overloaded_Operators
21512 @subsection @code{Overloaded_Operators}
21513 @cindex @code{Overloaded_Operators} rule (for @command{gnatcheck})
21514
21515 @noindent
21516 Flag each function declaration that overloads an operator symbol.
21517 A function body is checked only if the body does not have a
21518 separate spec. Formal functions are also checked. For a
21519 renaming declaration, only renaming-as-declaration is checked
21520
21521 This rule has no parameters.
21522
21523
21524 @node Overly_Nested_Control_Structures
21525 @subsection @code{Overly_Nested_Control_Structures}
21526 @cindex @code{Overly_Nested_Control_Structures} rule (for @command{gnatcheck})
21527
21528 @noindent
21529 Flag each control structure whose nesting level exceeds the value provided
21530 in the rule parameter.
21531
21532 The control structures checked are the following:
21533
21534 @itemize @bullet
21535 @item @code{if} statement
21536 @item @code{case} statement
21537 @item @code{loop} statement
21538 @item Selective accept statement
21539 @item Timed entry call statement
21540 @item Conditional entry call
21541 @item Asynchronous select statement
21542 @end itemize
21543
21544 @noindent
21545 The rule has the following parameter for the @option{+R} option:
21546
21547 @table @emph
21548 @item N
21549 Positive integer specifying the maximal control structure nesting
21550 level that is not flagged
21551 @end table
21552
21553 @noindent
21554 If the parameter for the @option{+R} option is not specified or
21555 if it is not a positive integer, @option{+R} option is ignored.
21556
21557 If more then one option is specified for the gnatcheck call, the later option and
21558 new parameter override the previous one(s).
21559
21560
21561 @node Parameters_Out_Of_Order
21562 @subsection @code{Parameters_Out_Of_Order}
21563 @cindex @code{Parameters_Out_Of_Order} rule (for @command{gnatcheck})
21564
21565 @noindent
21566 Flag each subprogram and entry declaration whose formal parameters are not
21567 ordered according to the following scheme:
21568
21569 @itemize @bullet
21570
21571 @item @code{in} and @code{access} parameters first,
21572 then @code{in out} parameters,
21573 and then @code{out} parameters;
21574
21575 @item for @code{in} mode, parameters with default initialization expressions
21576 occur last
21577 @end itemize
21578
21579 @noindent
21580 Only the first violation of the described order is flagged.
21581
21582 The following constructs are checked:
21583
21584 @itemize @bullet
21585 @item subprogram declarations (including null procedures);
21586 @item generic subprogram declarations;
21587 @item formal subprogram declarations;
21588 @item entry declarations;
21589 @item subprogram bodies and subprogram body stubs that do not
21590 have separate specifications
21591 @end itemize
21592
21593 @noindent
21594 Subprogram renamings are not checked.
21595
21596 This rule has no parameters.
21597
21598
21599 @node Positional_Actuals_For_Defaulted_Generic_Parameters
21600 @subsection @code{Positional_Actuals_For_Defaulted_Generic_Parameters}
21601 @cindex @code{Positional_Actuals_For_Defaulted_Generic_Parameters} rule (for @command{gnatcheck})
21602
21603 @noindent
21604 Flag each generic actual parameter corresponding to a generic formal
21605 parameter with a default initialization, if positional notation is used.
21606
21607 This rule has no parameters.
21608
21609 @node Positional_Actuals_For_Defaulted_Parameters
21610 @subsection @code{Positional_Actuals_For_Defaulted_Parameters}
21611 @cindex @code{Positional_Actuals_For_Defaulted_Parameters} rule (for @command{gnatcheck})
21612
21613 @noindent
21614 Flag each actual parameter to a subprogram or entry call where the
21615 corresponding formal parameter has a default expression, if positional
21616 notation is used.
21617
21618 This rule has no parameters.
21619
21620 @node Positional_Components
21621 @subsection @code{Positional_Components}
21622 @cindex @code{Positional_Components} rule (for @command{gnatcheck})
21623
21624 @noindent
21625 Flag each array, record and extension aggregate that includes positional
21626 notation.
21627
21628 This rule has no parameters.
21629
21630
21631 @node Positional_Generic_Parameters
21632 @subsection @code{Positional_Generic_Parameters}
21633 @cindex @code{Positional_Generic_Parameters} rule (for @command{gnatcheck})
21634
21635 @noindent
21636 Flag each instantiation using positional parameter notation.
21637
21638 This rule has no parameters.
21639
21640
21641 @node Positional_Parameters
21642 @subsection @code{Positional_Parameters}
21643 @cindex @code{Positional_Parameters} rule (for @command{gnatcheck})
21644
21645 @noindent
21646 Flag each subprogram or entry call using positional parameter notation,
21647 except for the following:
21648
21649 @itemize @bullet
21650 @item
21651 Invocations of prefix or infix operators are not flagged
21652 @item
21653 If the called subprogram or entry has only one formal parameter,
21654 the call is not flagged;
21655 @item
21656 If a subprogram call uses the @emph{Object.Operation} notation, then
21657 @itemize @minus
21658 @item
21659 the first parameter (that is, @emph{Object}) is not flagged;
21660 @item
21661 if the called subprogram has only two parameters, the second parameter
21662 of the call is not flagged;
21663 @end itemize
21664 @end itemize
21665
21666 @noindent
21667 This rule has no parameters.
21668
21669
21670
21671
21672 @node Predefined_Numeric_Types
21673 @subsection @code{Predefined_Numeric_Types}
21674 @cindex @code{Predefined_Numeric_Types} rule (for @command{gnatcheck})
21675
21676 @noindent
21677 Flag each explicit use of the name of any numeric type or subtype defined
21678 in package @code{Standard}.
21679
21680 The rationale for this rule is to detect when the
21681 program may depend on platform-specific characteristics of the implementation
21682 of the predefined numeric types. Note that this rule is over-pessimistic;
21683 for example, a program that uses @code{String} indexing
21684 likely needs a variable of type @code{Integer}.
21685 Another example is the flagging of predefined numeric types with explicit
21686 constraints:
21687
21688 @smallexample @c ada
21689 subtype My_Integer is Integer range Left .. Right;
21690 Vy_Var : My_Integer;
21691 @end smallexample
21692
21693 @noindent
21694 This rule detects only numeric types and subtypes defined in
21695 @code{Standard}. The use of numeric types and subtypes defined in other
21696 predefined packages (such as @code{System.Any_Priority} or
21697 @code{Ada.Text_IO.Count}) is not flagged
21698
21699 This rule has no parameters.
21700
21701
21702
21703 @node Raising_External_Exceptions
21704 @subsection @code{Raising_External_Exceptions}
21705 @cindex @code{Raising_External_Exceptions} rule (for @command{gnatcheck})
21706
21707 @noindent
21708 Flag any @code{raise} statement, in a program unit declared in a library
21709 package or in a generic library package, for an exception that is
21710 neither a predefined exception nor an exception that is also declared (or
21711 renamed) in the visible part of the package.
21712
21713 This rule has no parameters.
21714
21715
21716
21717 @node Raising_Predefined_Exceptions
21718 @subsection @code{Raising_Predefined_Exceptions}
21719 @cindex @code{Raising_Predefined_Exceptions} rule (for @command{gnatcheck})
21720
21721 @noindent
21722 Flag each @code{raise} statement that raises a predefined exception
21723 (i.e., one of the exceptions @code{Constraint_Error}, @code{Numeric_Error},
21724 @code{Program_Error}, @code{Storage_Error}, or @code{Tasking_Error}).
21725
21726 This rule has no parameters.
21727
21728 @node Separate_Numeric_Error_Handlers
21729 @subsection @code{Separate_Numeric_Error_Handlers}
21730 @cindex @code{Separate_Numeric_Error_Handlers} rule (for @command{gnatcheck})
21731
21732 @noindent
21733 Flags each exception handler that contains a choice for
21734 the predefined @code{Constraint_Error} exception, but does not contain
21735 the choice for the predefined @code{Numeric_Error} exception, or
21736 that contains the choice for @code{Numeric_Error}, but does not contain the
21737 choice for @code{Constraint_Error}.
21738
21739 This rule has no parameters.
21740
21741 @ignore
21742 @node Recursion
21743 @subsection @code{Recursion} (under construction, GLOBAL)
21744 @cindex @code{Recursion} rule (for @command{gnatcheck})
21745
21746 @noindent
21747 Flag recursive subprograms (cycles in the call graph). Declarations, and not
21748 calls, of recursive subprograms are detected.
21749
21750 This rule has no parameters.
21751 @end ignore
21752
21753 @ignore
21754 @node Side_Effect_Functions
21755 @subsection @code{Side_Effect_Functions} (under construction, GLOBAL)
21756 @cindex @code{Side_Effect_Functions} rule (for @command{gnatcheck})
21757
21758 @noindent
21759 Flag functions with side effects.
21760
21761 We define a side effect as changing any data object that is not local for the
21762 body of this function.
21763
21764 At the moment, we do NOT consider a side effect any input-output operations
21765 (changing a state or a content of any file).
21766
21767 We do not consider protected functions for this rule (???)
21768
21769 There are the following sources of side effect:
21770
21771 @enumerate
21772 @item Explicit (or direct) side-effect:
21773
21774 @itemize @bullet
21775 @item
21776 direct assignment to a non-local variable;
21777
21778 @item
21779 direct call to an entity that is known to change some data object that is
21780 not local for the body of this function (Note, that if F1 calls F2 and F2
21781 does have a side effect, this does not automatically mean that F1 also
21782 have a side effect, because it may be the case that F2 is declared in
21783 F1's body and it changes some data object that is global for F2, but
21784 local for F1);
21785 @end itemize
21786
21787 @item Indirect side-effect:
21788 @itemize @bullet
21789 @item
21790 Subprogram calls implicitly issued by:
21791 @itemize @bullet
21792 @item
21793 computing initialization expressions from type declarations as a part
21794 of object elaboration or allocator evaluation;
21795 @item
21796 computing implicit parameters of subprogram or entry calls or generic
21797 instantiations;
21798 @end itemize
21799
21800 @item
21801 activation of a task that change some non-local data object (directly or
21802 indirectly);
21803
21804 @item
21805 elaboration code of a package that is a result of a package instantiation;
21806
21807 @item
21808 controlled objects;
21809 @end itemize
21810
21811 @item Situations when we can suspect a side-effect, but the full static check
21812 is either impossible or too hard:
21813 @itemize @bullet
21814 @item
21815 assignment to access variables or to the objects pointed by access
21816 variables;
21817
21818 @item
21819 call to a subprogram pointed by access-to-subprogram value
21820
21821 @item
21822 dispatching calls;
21823 @end itemize
21824 @end enumerate
21825
21826 @noindent
21827 This rule has no parameters.
21828 @end ignore
21829
21830 @node Slices
21831 @subsection @code{Slices}
21832 @cindex @code{Slices} rule (for @command{gnatcheck})
21833
21834 @noindent
21835 Flag all uses of array slicing
21836
21837 This rule has no parameters.
21838
21839
21840 @node Unassigned_OUT_Parameters
21841 @subsection @code{Unassigned_OUT_Parameters}
21842 @cindex @code{Unassigned_OUT_Parameters} rule (for @command{gnatcheck})
21843
21844 @noindent
21845 Flags procedures' @code{out} parameters that are not assigned, and
21846 identifies the contexts in which the assignments are missing.
21847
21848 An @code{out} parameter is flagged in the statements in the procedure
21849 body's handled sequence of statements (before the procedure body's
21850 @code{exception} part, if any) if this sequence of statements contains
21851 no assignments to the parameter.
21852
21853 An @code{out} parameter is flagged in an exception handler in the exception
21854 part of the procedure body's handled sequence of statements if the handler
21855 contains no assignment to the parameter.
21856
21857 Bodies of generic procedures are also considered.
21858
21859 The following are treated as assignments to an @code{out} parameter:
21860
21861 @itemize @bullet
21862 @item
21863 an assignment statement, with the parameter or some component as the target;
21864
21865 @item
21866 passing the parameter (or one of its components) as an @code{out} or
21867 @code{in out} parameter.
21868 @end itemize
21869
21870 @noindent
21871 This rule does not have any parameters.
21872
21873
21874
21875 @node Uncommented_BEGIN_In_Package_Bodies
21876 @subsection @code{Uncommented_BEGIN_In_Package_Bodies}
21877 @cindex @code{Uncommented_BEGIN_In_Package_Bodies} rule (for @command{gnatcheck})
21878
21879 @noindent
21880 Flags each package body with declarations and a statement part that does not
21881 include a trailing comment on the line containing the @code{begin} keyword;
21882 this trailing comment needs to specify the package name and nothing else.
21883 The @code{begin} is not flagged if the package body does not
21884 contain any declarations.
21885
21886 If the @code{begin} keyword is placed on the
21887 same line as the last declaration or the first statement, it is flagged
21888 independently of whether the line contains a trailing comment. The
21889 diagnostic message is attached to the line containing the first statement.
21890
21891 This rule has no parameters.
21892
21893
21894 @node Unconstrained_Array_Returns
21895 @subsection @code{Unconstrained_Array_Returns}
21896 @cindex @code{Unconstrained_Array_Returns} rule (for @command{gnatcheck})
21897
21898 @noindent
21899 Flag each function returning an unconstrained array. Function declarations,
21900 function bodies (and body stubs) having no separate specifications,
21901 and generic function instantiations are checked.
21902 Generic function declarations, function calls and function renamings are
21903 not checked.
21904
21905 This rule has no parameters.
21906
21907 @node Universal_Ranges
21908 @subsection @code{Universal_Ranges}
21909 @cindex @code{Universal_Ranges} rule (for @command{gnatcheck})
21910
21911 @noindent
21912 Flag discrete ranges that are a part of an index constraint, constrained
21913 array definition, or @code{for}-loop parameter specification, and whose bounds
21914 are both of type @i{universal_integer}. Ranges that have at least one
21915 bound of a specific type (such as @code{1 .. N}, where @code{N} is a variable
21916 or an expression of non-universal type) are not flagged.
21917
21918 This rule has no parameters.
21919
21920
21921 @node Unnamed_Blocks_And_Loops
21922 @subsection @code{Unnamed_Blocks_And_Loops}
21923 @cindex @code{Unnamed_Blocks_And_Loops} rule (for @command{gnatcheck})
21924
21925 @noindent
21926 Flag each unnamed block statement and loop statement.
21927
21928 The rule has no parameters.
21929
21930
21931
21932 @ignore
21933 @node Unused_Subprograms
21934 @subsection @code{Unused_Subprograms} (under construction, GLOBAL)
21935 @cindex @code{Unused_Subprograms} rule (for @command{gnatcheck})
21936
21937 @noindent
21938 Flag all unused subprograms.
21939
21940 This rule has no parameters.
21941 @end ignore
21942
21943
21944
21945
21946 @node USE_PACKAGE_Clauses
21947 @subsection @code{USE_PACKAGE_Clauses}
21948 @cindex @code{USE_PACKAGE_Clauses} rule (for @command{gnatcheck})
21949
21950 @noindent
21951 Flag all @code{use} clauses for packages; @code{use type} clauses are
21952 not flagged.
21953
21954 This rule has no parameters.
21955
21956
21957
21958 @node Volatile_Objects_Without_Address_Clauses
21959 @subsection @code{Volatile_Objects_Without_Address_Clauses}
21960 @cindex @code{Volatile_Objects_Without_Address_Clauses} rule (for @command{gnatcheck})
21961
21962 @noindent
21963 Flag each volatile object that does not have an address clause.
21964
21965 The following check is made: if the pragma @code{Volatile} is applied to a
21966 data object or to its type, then an address clause must
21967 be supplied for this object.
21968
21969 This rule does not check the components of data objects,
21970 array components that are volatile as a result of the pragma
21971 @code{Volatile_Components}, or objects that are volatile because
21972 they are atomic as a result of pragmas @code{Atomic} or
21973 @code{Atomic_Components}.
21974
21975 Only variable declarations, and not constant declarations, are checked.
21976
21977 This rule has no parameters.
21978
21979
21980 @c *********************************
21981 @node Creating Sample Bodies Using gnatstub
21982 @chapter Creating Sample Bodies Using @command{gnatstub}
21983 @findex gnatstub
21984
21985 @noindent
21986 @command{gnatstub} creates body stubs, that is, empty but compilable bodies
21987 for library unit declarations.
21988
21989 Note: to invoke @code{gnatstub} with a project file, use the @code{gnat}
21990 driver (see @ref{The GNAT Driver and Project Files}).
21991
21992 To create a body stub, @command{gnatstub} has to compile the library
21993 unit declaration. Therefore, bodies can be created only for legal
21994 library units. Moreover, if a library unit depends semantically upon
21995 units located outside the current directory, you have to provide
21996 the source search path when calling @command{gnatstub}, see the description
21997 of @command{gnatstub} switches below.
21998
21999 @menu
22000 * Running gnatstub::
22001 * Switches for gnatstub::
22002 @end menu
22003
22004 @node Running gnatstub
22005 @section Running @command{gnatstub}
22006
22007 @noindent
22008 @command{gnatstub} has the command-line interface of the form
22009
22010 @smallexample
22011 $ gnatstub @ovar{switches} @var{filename} @ovar{directory}
22012 @end smallexample
22013
22014 @noindent
22015 where
22016 @table @var
22017 @item filename
22018 is the name of the source file that contains a library unit declaration
22019 for which a body must be created. The file name may contain the path
22020 information.
22021 The file name does not have to follow the GNAT file name conventions. If the
22022 name
22023 does not follow GNAT file naming conventions, the name of the body file must
22024 be provided
22025 explicitly as the value of the @option{^-o^/BODY=^@var{body-name}} option.
22026 If the file name follows the GNAT file naming
22027 conventions and the name of the body file is not provided,
22028 @command{gnatstub}
22029 creates the name
22030 of the body file from the argument file name by replacing the @file{.ads}
22031 suffix
22032 with the @file{.adb} suffix.
22033
22034 @item directory
22035 indicates the directory in which the body stub is to be placed (the default
22036 is the
22037 current directory)
22038
22039 @item switches
22040 is an optional sequence of switches as described in the next section
22041 @end table
22042
22043 @node Switches for gnatstub
22044 @section Switches for @command{gnatstub}
22045
22046 @table @option
22047 @c !sort!
22048
22049 @item ^-f^/FULL^
22050 @cindex @option{^-f^/FULL^} (@command{gnatstub})
22051 If the destination directory already contains a file with the name of the
22052 body file
22053 for the argument spec file, replace it with the generated body stub.
22054
22055 @item ^-hs^/HEADER=SPEC^
22056 @cindex @option{^-hs^/HEADER=SPEC^} (@command{gnatstub})
22057 Put the comment header (i.e., all the comments preceding the
22058 compilation unit) from the source of the library unit declaration
22059 into the body stub.
22060
22061 @item ^-hg^/HEADER=GENERAL^
22062 @cindex @option{^-hg^/HEADER=GENERAL^} (@command{gnatstub})
22063 Put a sample comment header into the body stub.
22064
22065 @item ^--header-file=@var{filename}^/FROM_HEADER_FILE=@var{filename}^
22066 @cindex @option{^--header-file^/FROM_HEADER_FILE=^} (@command{gnatstub})
22067 Use the content of the file as the comment header for a generated body stub.
22068
22069 @ifclear vms
22070 @item -IDIR
22071 @cindex @option{-IDIR} (@command{gnatstub})
22072 @itemx -I-
22073 @cindex @option{-I-} (@command{gnatstub})
22074 @end ifclear
22075 @ifset vms
22076 @item /NOCURRENT_DIRECTORY
22077 @cindex @option{/NOCURRENT_DIRECTORY} (@command{gnatstub})
22078 @end ifset
22079 ^These switches have ^This switch has^ the same meaning as in calls to
22080 @command{gcc}.
22081 ^They define ^It defines ^ the source search path in the call to
22082 @command{gcc} issued
22083 by @command{gnatstub} to compile an argument source file.
22084
22085 @item ^-gnatec^/CONFIGURATION_PRAGMAS_FILE=^@var{PATH}
22086 @cindex @option{^-gnatec^/CONFIGURATION_PRAGMAS_FILE^} (@command{gnatstub})
22087 This switch has the same meaning as in calls to @command{gcc}.
22088 It defines the additional configuration file to be passed to the call to
22089 @command{gcc} issued
22090 by @command{gnatstub} to compile an argument source file.
22091
22092 @item ^-gnatyM^/MAX_LINE_LENGTH=^@var{n}
22093 @cindex @option{^-gnatyM^/MAX_LINE_LENGTH^} (@command{gnatstub})
22094 (@var{n} is a non-negative integer). Set the maximum line length in the
22095 body stub to @var{n}; the default is 79. The maximum value that can be
22096 specified is 32767. Note that in the special case of configuration
22097 pragma files, the maximum is always 32767 regardless of whether or
22098 not this switch appears.
22099
22100 @item ^-gnaty^/STYLE_CHECKS=^@var{n}
22101 @cindex @option{^-gnaty^/STYLE_CHECKS=^} (@command{gnatstub})
22102 (@var{n} is a non-negative integer from 1 to 9). Set the indentation level in
22103 the generated body sample to @var{n}.
22104 The default indentation is 3.
22105
22106 @item ^-gnatyo^/ORDERED_SUBPROGRAMS^
22107 @cindex @option{^-gnato^/ORDERED_SUBPROGRAMS^} (@command{gnatstub})
22108 Order local bodies alphabetically. (By default local bodies are ordered
22109 in the same way as the corresponding local specs in the argument spec file.)
22110
22111 @item ^-i^/INDENTATION=^@var{n}
22112 @cindex @option{^-i^/INDENTATION^} (@command{gnatstub})
22113 Same as @option{^-gnaty^/STYLE_CHECKS=^@var{n}}
22114
22115 @item ^-k^/TREE_FILE=SAVE^
22116 @cindex @option{^-k^/TREE_FILE=SAVE^} (@command{gnatstub})
22117 Do not remove the tree file (i.e., the snapshot of the compiler internal
22118 structures used by @command{gnatstub}) after creating the body stub.
22119
22120 @item ^-l^/LINE_LENGTH=^@var{n}
22121 @cindex @option{^-l^/LINE_LENGTH^} (@command{gnatstub})
22122 Same as @option{^-gnatyM^/MAX_LINE_LENGTH=^@var{n}}
22123
22124 @item ^-o^/BODY=^@var{body-name}
22125 @cindex @option{^-o^/BODY^} (@command{gnatstub})
22126 Body file name. This should be set if the argument file name does not
22127 follow
22128 the GNAT file naming
22129 conventions. If this switch is omitted the default name for the body will be
22130 obtained
22131 from the argument file name according to the GNAT file naming conventions.
22132
22133 @item ^-q^/QUIET^
22134 @cindex @option{^-q^/QUIET^} (@command{gnatstub})
22135 Quiet mode: do not generate a confirmation when a body is
22136 successfully created, and do not generate a message when a body is not
22137 required for an
22138 argument unit.
22139
22140 @item ^-r^/TREE_FILE=REUSE^
22141 @cindex @option{^-r^/TREE_FILE=REUSE^} (@command{gnatstub})
22142 Reuse the tree file (if it exists) instead of creating it. Instead of
22143 creating the tree file for the library unit declaration, @command{gnatstub}
22144 tries to find it in the current directory and use it for creating
22145 a body. If the tree file is not found, no body is created. This option
22146 also implies @option{^-k^/SAVE^}, whether or not
22147 the latter is set explicitly.
22148
22149 @item ^-t^/TREE_FILE=OVERWRITE^
22150 @cindex @option{^-t^/TREE_FILE=OVERWRITE^} (@command{gnatstub})
22151 Overwrite the existing tree file. If the current directory already
22152 contains the file which, according to the GNAT file naming rules should
22153 be considered as a tree file for the argument source file,
22154 @command{gnatstub}
22155 will refuse to create the tree file needed to create a sample body
22156 unless this option is set.
22157
22158 @item ^-v^/VERBOSE^
22159 @cindex @option{^-v^/VERBOSE^} (@command{gnatstub})
22160 Verbose mode: generate version information.
22161
22162 @end table
22163
22164 @node Other Utility Programs
22165 @chapter Other Utility Programs
22166
22167 @noindent
22168 This chapter discusses some other utility programs available in the Ada
22169 environment.
22170
22171 @menu
22172 * Using Other Utility Programs with GNAT::
22173 * The External Symbol Naming Scheme of GNAT::
22174 * Converting Ada Files to html with gnathtml::
22175 * Installing gnathtml::
22176 @ifset vms
22177 * LSE::
22178 * Profiling::
22179 @end ifset
22180 @end menu
22181
22182 @node Using Other Utility Programs with GNAT
22183 @section Using Other Utility Programs with GNAT
22184
22185 @noindent
22186 The object files generated by GNAT are in standard system format and in
22187 particular the debugging information uses this format. This means
22188 programs generated by GNAT can be used with existing utilities that
22189 depend on these formats.
22190
22191 @ifclear vms
22192 In general, any utility program that works with C will also often work with
22193 Ada programs generated by GNAT. This includes software utilities such as
22194 gprof (a profiling program), @code{gdb} (the FSF debugger), and utilities such
22195 as Purify.
22196 @end ifclear
22197
22198 @node The External Symbol Naming Scheme of GNAT
22199 @section The External Symbol Naming Scheme of GNAT
22200
22201 @noindent
22202 In order to interpret the output from GNAT, when using tools that are
22203 originally intended for use with other languages, it is useful to
22204 understand the conventions used to generate link names from the Ada
22205 entity names.
22206
22207 All link names are in all lowercase letters. With the exception of library
22208 procedure names, the mechanism used is simply to use the full expanded
22209 Ada name with dots replaced by double underscores. For example, suppose
22210 we have the following package spec:
22211
22212 @smallexample @c ada
22213 @group
22214 @cartouche
22215 package QRS is
22216 MN : Integer;
22217 end QRS;
22218 @end cartouche
22219 @end group
22220 @end smallexample
22221
22222 @noindent
22223 The variable @code{MN} has a full expanded Ada name of @code{QRS.MN}, so
22224 the corresponding link name is @code{qrs__mn}.
22225 @findex Export
22226 Of course if a @code{pragma Export} is used this may be overridden:
22227
22228 @smallexample @c ada
22229 @group
22230 @cartouche
22231 package Exports is
22232 Var1 : Integer;
22233 pragma Export (Var1, C, External_Name => "var1_name");
22234 Var2 : Integer;
22235 pragma Export (Var2, C, Link_Name => "var2_link_name");
22236 end Exports;
22237 @end cartouche
22238 @end group
22239 @end smallexample
22240
22241 @noindent
22242 In this case, the link name for @var{Var1} is whatever link name the
22243 C compiler would assign for the C function @var{var1_name}. This typically
22244 would be either @var{var1_name} or @var{_var1_name}, depending on operating
22245 system conventions, but other possibilities exist. The link name for
22246 @var{Var2} is @var{var2_link_name}, and this is not operating system
22247 dependent.
22248
22249 @findex _main
22250 One exception occurs for library level procedures. A potential ambiguity
22251 arises between the required name @code{_main} for the C main program,
22252 and the name we would otherwise assign to an Ada library level procedure
22253 called @code{Main} (which might well not be the main program).
22254
22255 To avoid this ambiguity, we attach the prefix @code{_ada_} to such
22256 names. So if we have a library level procedure such as
22257
22258 @smallexample @c ada
22259 @group
22260 @cartouche
22261 procedure Hello (S : String);
22262 @end cartouche
22263 @end group
22264 @end smallexample
22265
22266 @noindent
22267 the external name of this procedure will be @var{_ada_hello}.
22268
22269
22270 @node Converting Ada Files to html with gnathtml
22271 @section Converting Ada Files to HTML with @code{gnathtml}
22272
22273 @noindent
22274 This @code{Perl} script allows Ada source files to be browsed using
22275 standard Web browsers. For installation procedure, see the section
22276 @xref{Installing gnathtml}.
22277
22278 Ada reserved keywords are highlighted in a bold font and Ada comments in
22279 a blue font. Unless your program was compiled with the gcc @option{-gnatx}
22280 switch to suppress the generation of cross-referencing information, user
22281 defined variables and types will appear in a different color; you will
22282 be able to click on any identifier and go to its declaration.
22283
22284 The command line is as follow:
22285 @smallexample
22286 $ perl gnathtml.pl @ovar{^switches^options^} @var{ada-files}
22287 @end smallexample
22288
22289 @noindent
22290 You can pass it as many Ada files as you want. @code{gnathtml} will generate
22291 an html file for every ada file, and a global file called @file{index.htm}.
22292 This file is an index of every identifier defined in the files.
22293
22294 The available ^switches^options^ are the following ones:
22295
22296 @table @option
22297 @item -83
22298 @cindex @option{-83} (@code{gnathtml})
22299 Only the Ada 83 subset of keywords will be highlighted.
22300
22301 @item -cc @var{color}
22302 @cindex @option{-cc} (@code{gnathtml})
22303 This option allows you to change the color used for comments. The default
22304 value is green. The color argument can be any name accepted by html.
22305
22306 @item -d
22307 @cindex @option{-d} (@code{gnathtml})
22308 If the Ada files depend on some other files (for instance through
22309 @code{with} clauses, the latter files will also be converted to html.
22310 Only the files in the user project will be converted to html, not the files
22311 in the run-time library itself.
22312
22313 @item -D
22314 @cindex @option{-D} (@code{gnathtml})
22315 This command is the same as @option{-d} above, but @command{gnathtml} will
22316 also look for files in the run-time library, and generate html files for them.
22317
22318 @item -ext @var{extension}
22319 @cindex @option{-ext} (@code{gnathtml})
22320 This option allows you to change the extension of the generated HTML files.
22321 If you do not specify an extension, it will default to @file{htm}.
22322
22323 @item -f
22324 @cindex @option{-f} (@code{gnathtml})
22325 By default, gnathtml will generate html links only for global entities
22326 ('with'ed units, global variables and types,@dots{}). If you specify
22327 @option{-f} on the command line, then links will be generated for local
22328 entities too.
22329
22330 @item -l @var{number}
22331 @cindex @option{-l} (@code{gnathtml})
22332 If this ^switch^option^ is provided and @var{number} is not 0, then
22333 @code{gnathtml} will number the html files every @var{number} line.
22334
22335 @item -I @var{dir}
22336 @cindex @option{-I} (@code{gnathtml})
22337 Specify a directory to search for library files (@file{.ALI} files) and
22338 source files. You can provide several -I switches on the command line,
22339 and the directories will be parsed in the order of the command line.
22340
22341 @item -o @var{dir}
22342 @cindex @option{-o} (@code{gnathtml})
22343 Specify the output directory for html files. By default, gnathtml will
22344 saved the generated html files in a subdirectory named @file{html/}.
22345
22346 @item -p @var{file}
22347 @cindex @option{-p} (@code{gnathtml})
22348 If you are using Emacs and the most recent Emacs Ada mode, which provides
22349 a full Integrated Development Environment for compiling, checking,
22350 running and debugging applications, you may use @file{.gpr} files
22351 to give the directories where Emacs can find sources and object files.
22352
22353 Using this ^switch^option^, you can tell gnathtml to use these files.
22354 This allows you to get an html version of your application, even if it
22355 is spread over multiple directories.
22356
22357 @item -sc @var{color}
22358 @cindex @option{-sc} (@code{gnathtml})
22359 This ^switch^option^ allows you to change the color used for symbol
22360 definitions.
22361 The default value is red. The color argument can be any name accepted by html.
22362
22363 @item -t @var{file}
22364 @cindex @option{-t} (@code{gnathtml})
22365 This ^switch^option^ provides the name of a file. This file contains a list of
22366 file names to be converted, and the effect is exactly as though they had
22367 appeared explicitly on the command line. This
22368 is the recommended way to work around the command line length limit on some
22369 systems.
22370
22371 @end table
22372
22373 @node Installing gnathtml
22374 @section Installing @code{gnathtml}
22375
22376 @noindent
22377 @code{Perl} needs to be installed on your machine to run this script.
22378 @code{Perl} is freely available for almost every architecture and
22379 Operating System via the Internet.
22380
22381 On Unix systems, you may want to modify the first line of the script
22382 @code{gnathtml}, to explicitly tell the Operating system where Perl
22383 is. The syntax of this line is:
22384 @smallexample
22385 #!full_path_name_to_perl
22386 @end smallexample
22387
22388 @noindent
22389 Alternatively, you may run the script using the following command line:
22390
22391 @smallexample
22392 $ perl gnathtml.pl @ovar{switches} @var{files}
22393 @end smallexample
22394
22395 @ifset vms
22396 @node LSE
22397 @section LSE
22398 @findex LSE
22399
22400 @noindent
22401 The GNAT distribution provides an Ada 95 template for the HP Language
22402 Sensitive Editor (LSE), a component of DECset. In order to
22403 access it, invoke LSE with the qualifier /ENVIRONMENT=GNU:[LIB]ADA95.ENV.
22404
22405 @node Profiling
22406 @section Profiling
22407 @findex PCA
22408
22409 @noindent
22410 GNAT supports The HP Performance Coverage Analyzer (PCA), a component
22411 of DECset. To use it proceed as outlined under ``HELP PCA'', except for running
22412 the collection phase with the /DEBUG qualifier.
22413
22414 @smallexample
22415 $ GNAT MAKE /DEBUG <PROGRAM_NAME>
22416 $ DEFINE LIB$DEBUG PCA$COLLECTOR
22417 $ RUN/DEBUG <PROGRAM_NAME>
22418 @end smallexample
22419 @noindent
22420 @end ifset
22421
22422 @ifclear vms
22423 @c ******************************
22424 @node Code Coverage and Profiling
22425 @chapter Code Coverage and Profiling
22426 @cindex Code Coverage
22427 @cindex Profiling
22428
22429 @noindent
22430 This chapter describes how to use @code{gcov} - coverage testing tool - and
22431 @code{gprof} - profiler tool - on your Ada programs.
22432
22433 @menu
22434 * Code Coverage of Ada Programs using gcov::
22435 * Profiling an Ada Program using gprof::
22436 @end menu
22437
22438 @node Code Coverage of Ada Programs using gcov
22439 @section Code Coverage of Ada Programs using gcov
22440 @cindex gcov
22441 @cindex -fprofile-arcs
22442 @cindex -ftest-coverage
22443 @cindex -coverage
22444 @cindex Code Coverage
22445
22446 @noindent
22447 @code{gcov} is a test coverage program: it analyzes the execution of a given
22448 program on selected tests, to help you determine the portions of the program
22449 that are still untested.
22450
22451 @code{gcov} is part of the GCC suite, and is described in detail in the GCC
22452 User's Guide. You can refer to this documentation for a more complete
22453 description.
22454
22455 This chapter provides a quick startup guide, and
22456 details some Gnat-specific features.
22457
22458 @menu
22459 * Quick startup guide::
22460 * Gnat specifics::
22461 @end menu
22462
22463 @node Quick startup guide
22464 @subsection Quick startup guide
22465
22466 In order to perform coverage analysis of a program using @code{gcov}, 3
22467 steps are needed:
22468
22469 @itemize @bullet
22470 @item
22471 Code instrumentation during the compilation process
22472 @item
22473 Execution of the instrumented program
22474 @item
22475 Execution of the @code{gcov} tool to generate the result.
22476 @end itemize
22477
22478 The code instrumentation needed by gcov is created at the object level:
22479 The source code is not modified in any way, because the instrumentation code is
22480 inserted by gcc during the compilation process. To compile your code with code
22481 coverage activated, you need to recompile your whole project using the
22482 switches
22483 @code{-fprofile-arcs} and @code{-ftest-coverage}, and link it using
22484 @code{-fprofile-arcs}.
22485
22486 @smallexample
22487 $ gnatmake -P my_project.gpr -f -cargs -fprofile-arcs -ftest-coverage \
22488 -largs -fprofile-arcs
22489 @end smallexample
22490
22491 This compilation process will create @file{.gcno} files together with
22492 the usual object files.
22493
22494 Once the program is compiled with coverage instrumentation, you can
22495 run it as many times as needed - on portions of a test suite for
22496 example. The first execution will produce @file{.gcda} files at the
22497 same location as the @file{.gcno} files. The following executions
22498 will update those files, so that a cumulative result of the covered
22499 portions of the program is generated.
22500
22501 Finally, you need to call the @code{gcov} tool. The different options of
22502 @code{gcov} are available in the GCC User's Guide, section 'Invoking gcov'.
22503
22504 This will create annotated source files with a @file{.gcov} extension:
22505 @file{my_main.adb} file will be analysed in @file{my_main.adb.gcov}.
22506
22507 @node Gnat specifics
22508 @subsection Gnat specifics
22509
22510 Because Ada semantics, portions of the source code may be shared among
22511 several object files. This is the case for example when generics are
22512 involved, when inlining is active or when declarations generate initialisation
22513 calls. In order to take
22514 into account this shared code, you need to call @code{gcov} on all
22515 source files of the tested program at once.
22516
22517 The list of source files might exceed the system's maximum command line
22518 length. In order to bypass this limitation, a new mechanism has been
22519 implemented in @code{gcov}: you can now list all your project's files into a
22520 text file, and provide this file to gcov as a parameter, preceded by a @@
22521 (e.g. @samp{gcov @@mysrclist.txt}).
22522
22523 @node Profiling an Ada Program using gprof
22524 @section Profiling an Ada Program using gprof
22525 @cindex gprof
22526 @cindex -pg
22527 @cindex Profiling
22528
22529 @noindent
22530 This section is not meant to be an exhaustive documentation of @code{gprof}.
22531 Full documentation for it can be found in the GNU Profiler User's Guide
22532 documentation that is part of this GNAT distribution.
22533
22534 Profiling a program helps determine the parts of a program that are executed
22535 most often, and are therefore the most time-consuming.
22536
22537 @code{gprof} is the standard GNU profiling tool; it has been enhanced to
22538 better handle Ada programs and multitasking.
22539 It is currently supported on the following platforms
22540 @itemize @bullet
22541 @item
22542 linux x86/x86_64
22543 @item
22544 solaris sparc/sparc64/x86
22545 @item
22546 windows x86
22547 @end itemize
22548
22549 @noindent
22550 In order to profile a program using @code{gprof}, 3 steps are needed:
22551
22552 @itemize @bullet
22553 @item
22554 Code instrumentation, requiring a full recompilation of the project with the
22555 proper switches.
22556 @item
22557 Execution of the program under the analysis conditions, i.e. with the desired
22558 input.
22559 @item
22560 Analysis of the results using the @code{gprof} tool.
22561 @end itemize
22562
22563 @noindent
22564 The following sections detail the different steps, and indicate how
22565 to interpret the results:
22566 @menu
22567 * Compilation for profiling::
22568 * Program execution::
22569 * Running gprof::
22570 * Interpretation of profiling results::
22571 @end menu
22572
22573 @node Compilation for profiling
22574 @subsection Compilation for profiling
22575 @cindex -pg
22576 @cindex Profiling
22577
22578 In order to profile a program the first step is to tell the compiler
22579 to generate the necessary profiling information. The compiler switch to be used
22580 is @code{-pg}, which must be added to other compilation switches. This
22581 switch needs to be specified both during compilation and link stages, and can
22582 be specified once when using gnatmake:
22583
22584 @smallexample
22585 gnatmake -f -pg -P my_project
22586 @end smallexample
22587
22588 @noindent
22589 Note that only the objects that were compiled with the @samp{-pg} switch will be
22590 profiled; if you need to profile your whole project, use the
22591 @samp{-f} gnatmake switch to force full recompilation.
22592
22593 @node Program execution
22594 @subsection Program execution
22595
22596 @noindent
22597 Once the program has been compiled for profiling, you can run it as usual.
22598
22599 The only constraint imposed by profiling is that the program must terminate
22600 normally. An interrupted program (via a Ctrl-C, kill, etc.) will not be
22601 properly analyzed.
22602
22603 Once the program completes execution, a data file called @file{gmon.out} is
22604 generated in the directory where the program was launched from. If this file
22605 already exists, it will be overwritten.
22606
22607 @node Running gprof
22608 @subsection Running gprof
22609
22610 @noindent
22611 The @code{gprof} tool is called as follow:
22612
22613 @smallexample
22614 gprof my_prog gmon.out
22615 @end smallexample
22616
22617 @noindent
22618 or simpler:
22619
22620 @smallexample
22621 gprof my_prog
22622 @end smallexample
22623
22624 @noindent
22625 The complete form of the gprof command line is the following:
22626
22627 @smallexample
22628 gprof [^switches^options^] [executable [data-file]]
22629 @end smallexample
22630
22631 @noindent
22632 @code{gprof} supports numerous ^switch^options^. The order of these
22633 ^switch^options^ does not matter. The full list of options can be found in
22634 the GNU Profiler User's Guide documentation that comes with this documentation.
22635
22636 The following is the subset of those switches that is most relevant:
22637
22638 @table @option
22639
22640 @item --demangle[=@var{style}]
22641 @itemx --no-demangle
22642 @cindex @option{--demangle} (@code{gprof})
22643 These options control whether symbol names should be demangled when
22644 printing output. The default is to demangle C++ symbols. The
22645 @code{--no-demangle} option may be used to turn off demangling. Different
22646 compilers have different mangling styles. The optional demangling style
22647 argument can be used to choose an appropriate demangling style for your
22648 compiler, in particular Ada symbols generated by GNAT can be demangled using
22649 @code{--demangle=gnat}.
22650
22651 @item -e @var{function_name}
22652 @cindex @option{-e} (@code{gprof})
22653 The @samp{-e @var{function}} option tells @code{gprof} not to print
22654 information about the function @var{function_name} (and its
22655 children@dots{}) in the call graph. The function will still be listed
22656 as a child of any functions that call it, but its index number will be
22657 shown as @samp{[not printed]}. More than one @samp{-e} option may be
22658 given; only one @var{function_name} may be indicated with each @samp{-e}
22659 option.
22660
22661 @item -E @var{function_name}
22662 @cindex @option{-E} (@code{gprof})
22663 The @code{-E @var{function}} option works like the @code{-e} option, but
22664 execution time spent in the function (and children who were not called from
22665 anywhere else), will not be used to compute the percentages-of-time for
22666 the call graph. More than one @samp{-E} option may be given; only one
22667 @var{function_name} may be indicated with each @samp{-E} option.
22668
22669 @item -f @var{function_name}
22670 @cindex @option{-f} (@code{gprof})
22671 The @samp{-f @var{function}} option causes @code{gprof} to limit the
22672 call graph to the function @var{function_name} and its children (and
22673 their children@dots{}). More than one @samp{-f} option may be given;
22674 only one @var{function_name} may be indicated with each @samp{-f}
22675 option.
22676
22677 @item -F @var{function_name}
22678 @cindex @option{-F} (@code{gprof})
22679 The @samp{-F @var{function}} option works like the @code{-f} option, but
22680 only time spent in the function and its children (and their
22681 children@dots{}) will be used to determine total-time and
22682 percentages-of-time for the call graph. More than one @samp{-F} option
22683 may be given; only one @var{function_name} may be indicated with each
22684 @samp{-F} option. The @samp{-F} option overrides the @samp{-E} option.
22685
22686 @end table
22687
22688 @node Interpretation of profiling results
22689 @subsection Interpretation of profiling results
22690
22691 @noindent
22692
22693 The results of the profiling analysis are represented by two arrays: the
22694 'flat profile' and the 'call graph'. Full documentation of those outputs
22695 can be found in the GNU Profiler User's Guide.
22696
22697 The flat profile shows the time spent in each function of the program, and how
22698 many time it has been called. This allows you to locate easily the most
22699 time-consuming functions.
22700
22701 The call graph shows, for each subprogram, the subprograms that call it,
22702 and the subprograms that it calls. It also provides an estimate of the time
22703 spent in each of those callers/called subprograms.
22704 @end ifclear
22705
22706 @c ******************************
22707 @node Running and Debugging Ada Programs
22708 @chapter Running and Debugging Ada Programs
22709 @cindex Debugging
22710
22711 @noindent
22712 This chapter discusses how to debug Ada programs.
22713 @ifset vms
22714 It applies to GNAT on the Alpha OpenVMS platform;
22715 for I64 OpenVMS please refer to the @cite{OpenVMS Debugger Manual},
22716 since HP has implemented Ada support in the OpenVMS debugger on I64.
22717 @end ifset
22718
22719 An incorrect Ada program may be handled in three ways by the GNAT compiler:
22720
22721 @enumerate
22722 @item
22723 The illegality may be a violation of the static semantics of Ada. In
22724 that case GNAT diagnoses the constructs in the program that are illegal.
22725 It is then a straightforward matter for the user to modify those parts of
22726 the program.
22727
22728 @item
22729 The illegality may be a violation of the dynamic semantics of Ada. In
22730 that case the program compiles and executes, but may generate incorrect
22731 results, or may terminate abnormally with some exception.
22732
22733 @item
22734 When presented with a program that contains convoluted errors, GNAT
22735 itself may terminate abnormally without providing full diagnostics on
22736 the incorrect user program.
22737 @end enumerate
22738
22739 @menu
22740 * The GNAT Debugger GDB::
22741 * Running GDB::
22742 * Introduction to GDB Commands::
22743 * Using Ada Expressions::
22744 * Calling User-Defined Subprograms::
22745 * Using the Next Command in a Function::
22746 * Ada Exceptions::
22747 * Ada Tasks::
22748 * Debugging Generic Units::
22749 * GNAT Abnormal Termination or Failure to Terminate::
22750 * Naming Conventions for GNAT Source Files::
22751 * Getting Internal Debugging Information::
22752 * Stack Traceback::
22753 @end menu
22754
22755 @cindex Debugger
22756 @findex gdb
22757
22758 @node The GNAT Debugger GDB
22759 @section The GNAT Debugger GDB
22760
22761 @noindent
22762 @code{GDB} is a general purpose, platform-independent debugger that
22763 can be used to debug mixed-language programs compiled with @command{gcc},
22764 and in particular is capable of debugging Ada programs compiled with
22765 GNAT. The latest versions of @code{GDB} are Ada-aware and can handle
22766 complex Ada data structures.
22767
22768 @xref{Top,, Debugging with GDB, gdb, Debugging with GDB},
22769 @ifset vms
22770 located in the GNU:[DOCS] directory,
22771 @end ifset
22772 for full details on the usage of @code{GDB}, including a section on
22773 its usage on programs. This manual should be consulted for full
22774 details. The section that follows is a brief introduction to the
22775 philosophy and use of @code{GDB}.
22776
22777 When GNAT programs are compiled, the compiler optionally writes debugging
22778 information into the generated object file, including information on
22779 line numbers, and on declared types and variables. This information is
22780 separate from the generated code. It makes the object files considerably
22781 larger, but it does not add to the size of the actual executable that
22782 will be loaded into memory, and has no impact on run-time performance. The
22783 generation of debug information is triggered by the use of the
22784 ^-g^/DEBUG^ switch in the @command{gcc} or @command{gnatmake} command
22785 used to carry out the compilations. It is important to emphasize that
22786 the use of these options does not change the generated code.
22787
22788 The debugging information is written in standard system formats that
22789 are used by many tools, including debuggers and profilers. The format
22790 of the information is typically designed to describe C types and
22791 semantics, but GNAT implements a translation scheme which allows full
22792 details about Ada types and variables to be encoded into these
22793 standard C formats. Details of this encoding scheme may be found in
22794 the file exp_dbug.ads in the GNAT source distribution. However, the
22795 details of this encoding are, in general, of no interest to a user,
22796 since @code{GDB} automatically performs the necessary decoding.
22797
22798 When a program is bound and linked, the debugging information is
22799 collected from the object files, and stored in the executable image of
22800 the program. Again, this process significantly increases the size of
22801 the generated executable file, but it does not increase the size of
22802 the executable program itself. Furthermore, if this program is run in
22803 the normal manner, it runs exactly as if the debug information were
22804 not present, and takes no more actual memory.
22805
22806 However, if the program is run under control of @code{GDB}, the
22807 debugger is activated. The image of the program is loaded, at which
22808 point it is ready to run. If a run command is given, then the program
22809 will run exactly as it would have if @code{GDB} were not present. This
22810 is a crucial part of the @code{GDB} design philosophy. @code{GDB} is
22811 entirely non-intrusive until a breakpoint is encountered. If no
22812 breakpoint is ever hit, the program will run exactly as it would if no
22813 debugger were present. When a breakpoint is hit, @code{GDB} accesses
22814 the debugging information and can respond to user commands to inspect
22815 variables, and more generally to report on the state of execution.
22816
22817 @c **************
22818 @node Running GDB
22819 @section Running GDB
22820
22821 @noindent
22822 This section describes how to initiate the debugger.
22823 @c The above sentence is really just filler, but it was otherwise
22824 @c clumsy to get the first paragraph nonindented given the conditional
22825 @c nature of the description
22826
22827 @ifclear vms
22828 The debugger can be launched from a @code{GPS} menu or
22829 directly from the command line. The description below covers the latter use.
22830 All the commands shown can be used in the @code{GPS} debug console window,
22831 but there are usually more GUI-based ways to achieve the same effect.
22832 @end ifclear
22833
22834 The command to run @code{GDB} is
22835
22836 @smallexample
22837 $ ^gdb program^GDB PROGRAM^
22838 @end smallexample
22839
22840 @noindent
22841 where @code{^program^PROGRAM^} is the name of the executable file. This
22842 activates the debugger and results in a prompt for debugger commands.
22843 The simplest command is simply @code{run}, which causes the program to run
22844 exactly as if the debugger were not present. The following section
22845 describes some of the additional commands that can be given to @code{GDB}.
22846
22847 @c *******************************
22848 @node Introduction to GDB Commands
22849 @section Introduction to GDB Commands
22850
22851 @noindent
22852 @code{GDB} contains a large repertoire of commands. @xref{Top,,
22853 Debugging with GDB, gdb, Debugging with GDB},
22854 @ifset vms
22855 located in the GNU:[DOCS] directory,
22856 @end ifset
22857 for extensive documentation on the use
22858 of these commands, together with examples of their use. Furthermore,
22859 the command @command{help} invoked from within GDB activates a simple help
22860 facility which summarizes the available commands and their options.
22861 In this section we summarize a few of the most commonly
22862 used commands to give an idea of what @code{GDB} is about. You should create
22863 a simple program with debugging information and experiment with the use of
22864 these @code{GDB} commands on the program as you read through the
22865 following section.
22866
22867 @table @code
22868 @item set args @var{arguments}
22869 The @var{arguments} list above is a list of arguments to be passed to
22870 the program on a subsequent run command, just as though the arguments
22871 had been entered on a normal invocation of the program. The @code{set args}
22872 command is not needed if the program does not require arguments.
22873
22874 @item run
22875 The @code{run} command causes execution of the program to start from
22876 the beginning. If the program is already running, that is to say if
22877 you are currently positioned at a breakpoint, then a prompt will ask
22878 for confirmation that you want to abandon the current execution and
22879 restart.
22880
22881 @item breakpoint @var{location}
22882 The breakpoint command sets a breakpoint, that is to say a point at which
22883 execution will halt and @code{GDB} will await further
22884 commands. @var{location} is
22885 either a line number within a file, given in the format @code{file:linenumber},
22886 or it is the name of a subprogram. If you request that a breakpoint be set on
22887 a subprogram that is overloaded, a prompt will ask you to specify on which of
22888 those subprograms you want to breakpoint. You can also
22889 specify that all of them should be breakpointed. If the program is run
22890 and execution encounters the breakpoint, then the program
22891 stops and @code{GDB} signals that the breakpoint was encountered by
22892 printing the line of code before which the program is halted.
22893
22894 @item breakpoint exception @var{name}
22895 A special form of the breakpoint command which breakpoints whenever
22896 exception @var{name} is raised.
22897 If @var{name} is omitted,
22898 then a breakpoint will occur when any exception is raised.
22899
22900 @item print @var{expression}
22901 This will print the value of the given expression. Most simple
22902 Ada expression formats are properly handled by @code{GDB}, so the expression
22903 can contain function calls, variables, operators, and attribute references.
22904
22905 @item continue
22906 Continues execution following a breakpoint, until the next breakpoint or the
22907 termination of the program.
22908
22909 @item step
22910 Executes a single line after a breakpoint. If the next statement
22911 is a subprogram call, execution continues into (the first statement of)
22912 the called subprogram.
22913
22914 @item next
22915 Executes a single line. If this line is a subprogram call, executes and
22916 returns from the call.
22917
22918 @item list
22919 Lists a few lines around the current source location. In practice, it
22920 is usually more convenient to have a separate edit window open with the
22921 relevant source file displayed. Successive applications of this command
22922 print subsequent lines. The command can be given an argument which is a
22923 line number, in which case it displays a few lines around the specified one.
22924
22925 @item backtrace
22926 Displays a backtrace of the call chain. This command is typically
22927 used after a breakpoint has occurred, to examine the sequence of calls that
22928 leads to the current breakpoint. The display includes one line for each
22929 activation record (frame) corresponding to an active subprogram.
22930
22931 @item up
22932 At a breakpoint, @code{GDB} can display the values of variables local
22933 to the current frame. The command @code{up} can be used to
22934 examine the contents of other active frames, by moving the focus up
22935 the stack, that is to say from callee to caller, one frame at a time.
22936
22937 @item down
22938 Moves the focus of @code{GDB} down from the frame currently being
22939 examined to the frame of its callee (the reverse of the previous command),
22940
22941 @item frame @var{n}
22942 Inspect the frame with the given number. The value 0 denotes the frame
22943 of the current breakpoint, that is to say the top of the call stack.
22944
22945 @end table
22946
22947 @noindent
22948 The above list is a very short introduction to the commands that
22949 @code{GDB} provides. Important additional capabilities, including conditional
22950 breakpoints, the ability to execute command sequences on a breakpoint,
22951 the ability to debug at the machine instruction level and many other
22952 features are described in detail in @ref{Top,, Debugging with GDB, gdb,
22953 Debugging with GDB}. Note that most commands can be abbreviated
22954 (for example, c for continue, bt for backtrace).
22955
22956 @node Using Ada Expressions
22957 @section Using Ada Expressions
22958 @cindex Ada expressions
22959
22960 @noindent
22961 @code{GDB} supports a fairly large subset of Ada expression syntax, with some
22962 extensions. The philosophy behind the design of this subset is
22963
22964 @itemize @bullet
22965 @item
22966 That @code{GDB} should provide basic literals and access to operations for
22967 arithmetic, dereferencing, field selection, indexing, and subprogram calls,
22968 leaving more sophisticated computations to subprograms written into the
22969 program (which therefore may be called from @code{GDB}).
22970
22971 @item
22972 That type safety and strict adherence to Ada language restrictions
22973 are not particularly important to the @code{GDB} user.
22974
22975 @item
22976 That brevity is important to the @code{GDB} user.
22977 @end itemize
22978
22979 @noindent
22980 Thus, for brevity, the debugger acts as if there were
22981 implicit @code{with} and @code{use} clauses in effect for all user-written
22982 packages, thus making it unnecessary to fully qualify most names with
22983 their packages, regardless of context. Where this causes ambiguity,
22984 @code{GDB} asks the user's intent.
22985
22986 For details on the supported Ada syntax, see @ref{Top,, Debugging with
22987 GDB, gdb, Debugging with GDB}.
22988
22989 @node Calling User-Defined Subprograms
22990 @section Calling User-Defined Subprograms
22991
22992 @noindent
22993 An important capability of @code{GDB} is the ability to call user-defined
22994 subprograms while debugging. This is achieved simply by entering
22995 a subprogram call statement in the form:
22996
22997 @smallexample
22998 call subprogram-name (parameters)
22999 @end smallexample
23000
23001 @noindent
23002 The keyword @code{call} can be omitted in the normal case where the
23003 @code{subprogram-name} does not coincide with any of the predefined
23004 @code{GDB} commands.
23005
23006 The effect is to invoke the given subprogram, passing it the
23007 list of parameters that is supplied. The parameters can be expressions and
23008 can include variables from the program being debugged. The
23009 subprogram must be defined
23010 at the library level within your program, and @code{GDB} will call the
23011 subprogram within the environment of your program execution (which
23012 means that the subprogram is free to access or even modify variables
23013 within your program).
23014
23015 The most important use of this facility is in allowing the inclusion of
23016 debugging routines that are tailored to particular data structures
23017 in your program. Such debugging routines can be written to provide a suitably
23018 high-level description of an abstract type, rather than a low-level dump
23019 of its physical layout. After all, the standard
23020 @code{GDB print} command only knows the physical layout of your
23021 types, not their abstract meaning. Debugging routines can provide information
23022 at the desired semantic level and are thus enormously useful.
23023
23024 For example, when debugging GNAT itself, it is crucial to have access to
23025 the contents of the tree nodes used to represent the program internally.
23026 But tree nodes are represented simply by an integer value (which in turn
23027 is an index into a table of nodes).
23028 Using the @code{print} command on a tree node would simply print this integer
23029 value, which is not very useful. But the PN routine (defined in file
23030 treepr.adb in the GNAT sources) takes a tree node as input, and displays
23031 a useful high level representation of the tree node, which includes the
23032 syntactic category of the node, its position in the source, the integers
23033 that denote descendant nodes and parent node, as well as varied
23034 semantic information. To study this example in more detail, you might want to
23035 look at the body of the PN procedure in the stated file.
23036
23037 @node Using the Next Command in a Function
23038 @section Using the Next Command in a Function
23039
23040 @noindent
23041 When you use the @code{next} command in a function, the current source
23042 location will advance to the next statement as usual. A special case
23043 arises in the case of a @code{return} statement.
23044
23045 Part of the code for a return statement is the ``epilog'' of the function.
23046 This is the code that returns to the caller. There is only one copy of
23047 this epilog code, and it is typically associated with the last return
23048 statement in the function if there is more than one return. In some
23049 implementations, this epilog is associated with the first statement
23050 of the function.
23051
23052 The result is that if you use the @code{next} command from a return
23053 statement that is not the last return statement of the function you
23054 may see a strange apparent jump to the last return statement or to
23055 the start of the function. You should simply ignore this odd jump.
23056 The value returned is always that from the first return statement
23057 that was stepped through.
23058
23059 @node Ada Exceptions
23060 @section Breaking on Ada Exceptions
23061 @cindex Exceptions
23062
23063 @noindent
23064 You can set breakpoints that trip when your program raises
23065 selected exceptions.
23066
23067 @table @code
23068 @item break exception
23069 Set a breakpoint that trips whenever (any task in the) program raises
23070 any exception.
23071
23072 @item break exception @var{name}
23073 Set a breakpoint that trips whenever (any task in the) program raises
23074 the exception @var{name}.
23075
23076 @item break exception unhandled
23077 Set a breakpoint that trips whenever (any task in the) program raises an
23078 exception for which there is no handler.
23079
23080 @item info exceptions
23081 @itemx info exceptions @var{regexp}
23082 The @code{info exceptions} command permits the user to examine all defined
23083 exceptions within Ada programs. With a regular expression, @var{regexp}, as
23084 argument, prints out only those exceptions whose name matches @var{regexp}.
23085 @end table
23086
23087 @node Ada Tasks
23088 @section Ada Tasks
23089 @cindex Tasks
23090
23091 @noindent
23092 @code{GDB} allows the following task-related commands:
23093
23094 @table @code
23095 @item info tasks
23096 This command shows a list of current Ada tasks, as in the following example:
23097
23098 @smallexample
23099 @iftex
23100 @leftskip=0cm
23101 @end iftex
23102 (gdb) info tasks
23103 ID TID P-ID Thread Pri State Name
23104 1 8088000 0 807e000 15 Child Activation Wait main_task
23105 2 80a4000 1 80ae000 15 Accept/Select Wait b
23106 3 809a800 1 80a4800 15 Child Activation Wait a
23107 * 4 80ae800 3 80b8000 15 Running c
23108 @end smallexample
23109
23110 @noindent
23111 In this listing, the asterisk before the first task indicates it to be the
23112 currently running task. The first column lists the task ID that is used
23113 to refer to tasks in the following commands.
23114
23115 @item break @var{linespec} task @var{taskid}
23116 @itemx break @var{linespec} task @var{taskid} if @dots{}
23117 @cindex Breakpoints and tasks
23118 These commands are like the @code{break @dots{} thread @dots{}}.
23119 @var{linespec} specifies source lines.
23120
23121 Use the qualifier @samp{task @var{taskid}} with a breakpoint command
23122 to specify that you only want @code{GDB} to stop the program when a
23123 particular Ada task reaches this breakpoint. @var{taskid} is one of the
23124 numeric task identifiers assigned by @code{GDB}, shown in the first
23125 column of the @samp{info tasks} display.
23126
23127 If you do not specify @samp{task @var{taskid}} when you set a
23128 breakpoint, the breakpoint applies to @emph{all} tasks of your
23129 program.
23130
23131 You can use the @code{task} qualifier on conditional breakpoints as
23132 well; in this case, place @samp{task @var{taskid}} before the
23133 breakpoint condition (before the @code{if}).
23134
23135 @item task @var{taskno}
23136 @cindex Task switching
23137
23138 This command allows to switch to the task referred by @var{taskno}. In
23139 particular, This allows to browse the backtrace of the specified
23140 task. It is advised to switch back to the original task before
23141 continuing execution otherwise the scheduling of the program may be
23142 perturbed.
23143 @end table
23144
23145 @noindent
23146 For more detailed information on the tasking support,
23147 see @ref{Top,, Debugging with GDB, gdb, Debugging with GDB}.
23148
23149 @node Debugging Generic Units
23150 @section Debugging Generic Units
23151 @cindex Debugging Generic Units
23152 @cindex Generics
23153
23154 @noindent
23155 GNAT always uses code expansion for generic instantiation. This means that
23156 each time an instantiation occurs, a complete copy of the original code is
23157 made, with appropriate substitutions of formals by actuals.
23158
23159 It is not possible to refer to the original generic entities in
23160 @code{GDB}, but it is always possible to debug a particular instance of
23161 a generic, by using the appropriate expanded names. For example, if we have
23162
23163 @smallexample @c ada
23164 @group
23165 @cartouche
23166 procedure g is
23167
23168 generic package k is
23169 procedure kp (v1 : in out integer);
23170 end k;
23171
23172 package body k is
23173 procedure kp (v1 : in out integer) is
23174 begin
23175 v1 := v1 + 1;
23176 end kp;
23177 end k;
23178
23179 package k1 is new k;
23180 package k2 is new k;
23181
23182 var : integer := 1;
23183
23184 begin
23185 k1.kp (var);
23186 k2.kp (var);
23187 k1.kp (var);
23188 k2.kp (var);
23189 end;
23190 @end cartouche
23191 @end group
23192 @end smallexample
23193
23194 @noindent
23195 Then to break on a call to procedure kp in the k2 instance, simply
23196 use the command:
23197
23198 @smallexample
23199 (gdb) break g.k2.kp
23200 @end smallexample
23201
23202 @noindent
23203 When the breakpoint occurs, you can step through the code of the
23204 instance in the normal manner and examine the values of local variables, as for
23205 other units.
23206
23207 @node GNAT Abnormal Termination or Failure to Terminate
23208 @section GNAT Abnormal Termination or Failure to Terminate
23209 @cindex GNAT Abnormal Termination or Failure to Terminate
23210
23211 @noindent
23212 When presented with programs that contain serious errors in syntax
23213 or semantics,
23214 GNAT may on rare occasions experience problems in operation, such
23215 as aborting with a
23216 segmentation fault or illegal memory access, raising an internal
23217 exception, terminating abnormally, or failing to terminate at all.
23218 In such cases, you can activate
23219 various features of GNAT that can help you pinpoint the construct in your
23220 program that is the likely source of the problem.
23221
23222 The following strategies are presented in increasing order of
23223 difficulty, corresponding to your experience in using GNAT and your
23224 familiarity with compiler internals.
23225
23226 @enumerate
23227 @item
23228 Run @command{gcc} with the @option{-gnatf}. This first
23229 switch causes all errors on a given line to be reported. In its absence,
23230 only the first error on a line is displayed.
23231
23232 The @option{-gnatdO} switch causes errors to be displayed as soon as they
23233 are encountered, rather than after compilation is terminated. If GNAT
23234 terminates prematurely or goes into an infinite loop, the last error
23235 message displayed may help to pinpoint the culprit.
23236
23237 @item
23238 Run @command{gcc} with the @option{^-v (verbose)^/VERBOSE^} switch. In this
23239 mode, @command{gcc} produces ongoing information about the progress of the
23240 compilation and provides the name of each procedure as code is
23241 generated. This switch allows you to find which Ada procedure was being
23242 compiled when it encountered a code generation problem.
23243
23244 @item
23245 @cindex @option{-gnatdc} switch
23246 Run @command{gcc} with the @option{-gnatdc} switch. This is a GNAT specific
23247 switch that does for the front-end what @option{^-v^VERBOSE^} does
23248 for the back end. The system prints the name of each unit,
23249 either a compilation unit or nested unit, as it is being analyzed.
23250 @item
23251 Finally, you can start
23252 @code{gdb} directly on the @code{gnat1} executable. @code{gnat1} is the
23253 front-end of GNAT, and can be run independently (normally it is just
23254 called from @command{gcc}). You can use @code{gdb} on @code{gnat1} as you
23255 would on a C program (but @pxref{The GNAT Debugger GDB} for caveats). The
23256 @code{where} command is the first line of attack; the variable
23257 @code{lineno} (seen by @code{print lineno}), used by the second phase of
23258 @code{gnat1} and by the @command{gcc} backend, indicates the source line at
23259 which the execution stopped, and @code{input_file name} indicates the name of
23260 the source file.
23261 @end enumerate
23262
23263 @node Naming Conventions for GNAT Source Files
23264 @section Naming Conventions for GNAT Source Files
23265
23266 @noindent
23267 In order to examine the workings of the GNAT system, the following
23268 brief description of its organization may be helpful:
23269
23270 @itemize @bullet
23271 @item
23272 Files with prefix @file{^sc^SC^} contain the lexical scanner.
23273
23274 @item
23275 All files prefixed with @file{^par^PAR^} are components of the parser. The
23276 numbers correspond to chapters of the Ada Reference Manual. For example,
23277 parsing of select statements can be found in @file{par-ch9.adb}.
23278
23279 @item
23280 All files prefixed with @file{^sem^SEM^} perform semantic analysis. The
23281 numbers correspond to chapters of the Ada standard. For example, all
23282 issues involving context clauses can be found in @file{sem_ch10.adb}. In
23283 addition, some features of the language require sufficient special processing
23284 to justify their own semantic files: sem_aggr for aggregates, sem_disp for
23285 dynamic dispatching, etc.
23286
23287 @item
23288 All files prefixed with @file{^exp^EXP^} perform normalization and
23289 expansion of the intermediate representation (abstract syntax tree, or AST).
23290 these files use the same numbering scheme as the parser and semantics files.
23291 For example, the construction of record initialization procedures is done in
23292 @file{exp_ch3.adb}.
23293
23294 @item
23295 The files prefixed with @file{^bind^BIND^} implement the binder, which
23296 verifies the consistency of the compilation, determines an order of
23297 elaboration, and generates the bind file.
23298
23299 @item
23300 The files @file{atree.ads} and @file{atree.adb} detail the low-level
23301 data structures used by the front-end.
23302
23303 @item
23304 The files @file{sinfo.ads} and @file{sinfo.adb} detail the structure of
23305 the abstract syntax tree as produced by the parser.
23306
23307 @item
23308 The files @file{einfo.ads} and @file{einfo.adb} detail the attributes of
23309 all entities, computed during semantic analysis.
23310
23311 @item
23312 Library management issues are dealt with in files with prefix
23313 @file{^lib^LIB^}.
23314
23315 @item
23316 @findex Ada
23317 @cindex Annex A
23318 Ada files with the prefix @file{^a-^A-^} are children of @code{Ada}, as
23319 defined in Annex A.
23320
23321 @item
23322 @findex Interfaces
23323 @cindex Annex B
23324 Files with prefix @file{^i-^I-^} are children of @code{Interfaces}, as
23325 defined in Annex B.
23326
23327 @item
23328 @findex System
23329 Files with prefix @file{^s-^S-^} are children of @code{System}. This includes
23330 both language-defined children and GNAT run-time routines.
23331
23332 @item
23333 @findex GNAT
23334 Files with prefix @file{^g-^G-^} are children of @code{GNAT}. These are useful
23335 general-purpose packages, fully documented in their specs. All
23336 the other @file{.c} files are modifications of common @command{gcc} files.
23337 @end itemize
23338
23339 @node Getting Internal Debugging Information
23340 @section Getting Internal Debugging Information
23341
23342 @noindent
23343 Most compilers have internal debugging switches and modes. GNAT
23344 does also, except GNAT internal debugging switches and modes are not
23345 secret. A summary and full description of all the compiler and binder
23346 debug flags are in the file @file{debug.adb}. You must obtain the
23347 sources of the compiler to see the full detailed effects of these flags.
23348
23349 The switches that print the source of the program (reconstructed from
23350 the internal tree) are of general interest for user programs, as are the
23351 options to print
23352 the full internal tree, and the entity table (the symbol table
23353 information). The reconstructed source provides a readable version of the
23354 program after the front-end has completed analysis and expansion,
23355 and is useful when studying the performance of specific constructs.
23356 For example, constraint checks are indicated, complex aggregates
23357 are replaced with loops and assignments, and tasking primitives
23358 are replaced with run-time calls.
23359
23360 @node Stack Traceback
23361 @section Stack Traceback
23362 @cindex traceback
23363 @cindex stack traceback
23364 @cindex stack unwinding
23365
23366 @noindent
23367 Traceback is a mechanism to display the sequence of subprogram calls that
23368 leads to a specified execution point in a program. Often (but not always)
23369 the execution point is an instruction at which an exception has been raised.
23370 This mechanism is also known as @i{stack unwinding} because it obtains
23371 its information by scanning the run-time stack and recovering the activation
23372 records of all active subprograms. Stack unwinding is one of the most
23373 important tools for program debugging.
23374
23375 The first entry stored in traceback corresponds to the deepest calling level,
23376 that is to say the subprogram currently executing the instruction
23377 from which we want to obtain the traceback.
23378
23379 Note that there is no runtime performance penalty when stack traceback
23380 is enabled, and no exception is raised during program execution.
23381
23382 @menu
23383 * Non-Symbolic Traceback::
23384 * Symbolic Traceback::
23385 @end menu
23386
23387 @node Non-Symbolic Traceback
23388 @subsection Non-Symbolic Traceback
23389 @cindex traceback, non-symbolic
23390
23391 @noindent
23392 Note: this feature is not supported on all platforms. See
23393 @file{GNAT.Traceback spec in g-traceb.ads} for a complete list of supported
23394 platforms.
23395
23396 @menu
23397 * Tracebacks From an Unhandled Exception::
23398 * Tracebacks From Exception Occurrences (non-symbolic)::
23399 * Tracebacks From Anywhere in a Program (non-symbolic)::
23400 @end menu
23401
23402 @node Tracebacks From an Unhandled Exception
23403 @subsubsection Tracebacks From an Unhandled Exception
23404
23405 @noindent
23406 A runtime non-symbolic traceback is a list of addresses of call instructions.
23407 To enable this feature you must use the @option{-E}
23408 @code{gnatbind}'s option. With this option a stack traceback is stored as part
23409 of exception information. You can retrieve this information using the
23410 @code{addr2line} tool.
23411
23412 Here is a simple example:
23413
23414 @smallexample @c ada
23415 @cartouche
23416 procedure STB is
23417
23418 procedure P1 is
23419 begin
23420 raise Constraint_Error;
23421 end P1;
23422
23423 procedure P2 is
23424 begin
23425 P1;
23426 end P2;
23427
23428 begin
23429 P2;
23430 end STB;
23431 @end cartouche
23432 @end smallexample
23433
23434 @smallexample
23435 $ gnatmake stb -bargs -E
23436 $ stb
23437
23438 Execution terminated by unhandled exception
23439 Exception name: CONSTRAINT_ERROR
23440 Message: stb.adb:5
23441 Call stack traceback locations:
23442 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
23443 @end smallexample
23444
23445 @noindent
23446 As we see the traceback lists a sequence of addresses for the unhandled
23447 exception @code{CONSTRAINT_ERROR} raised in procedure P1. It is easy to
23448 guess that this exception come from procedure P1. To translate these
23449 addresses into the source lines where the calls appear, the
23450 @code{addr2line} tool, described below, is invaluable. The use of this tool
23451 requires the program to be compiled with debug information.
23452
23453 @smallexample
23454 $ gnatmake -g stb -bargs -E
23455 $ stb
23456
23457 Execution terminated by unhandled exception
23458 Exception name: CONSTRAINT_ERROR
23459 Message: stb.adb:5
23460 Call stack traceback locations:
23461 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
23462
23463 $ addr2line --exe=stb 0x401373 0x40138b 0x40139c 0x401335 0x4011c4
23464 0x4011f1 0x77e892a4
23465
23466 00401373 at d:/stb/stb.adb:5
23467 0040138B at d:/stb/stb.adb:10
23468 0040139C at d:/stb/stb.adb:14
23469 00401335 at d:/stb/b~stb.adb:104
23470 004011C4 at /build/@dots{}/crt1.c:200
23471 004011F1 at /build/@dots{}/crt1.c:222
23472 77E892A4 in ?? at ??:0
23473 @end smallexample
23474
23475 @noindent
23476 The @code{addr2line} tool has several other useful options:
23477
23478 @table @code
23479 @item --functions
23480 to get the function name corresponding to any location
23481
23482 @item --demangle=gnat
23483 to use the gnat decoding mode for the function names. Note that
23484 for binutils version 2.9.x the option is simply @option{--demangle}.
23485 @end table
23486
23487 @smallexample
23488 $ addr2line --exe=stb --functions --demangle=gnat 0x401373 0x40138b
23489 0x40139c 0x401335 0x4011c4 0x4011f1
23490
23491 00401373 in stb.p1 at d:/stb/stb.adb:5
23492 0040138B in stb.p2 at d:/stb/stb.adb:10
23493 0040139C in stb at d:/stb/stb.adb:14
23494 00401335 in main at d:/stb/b~stb.adb:104
23495 004011C4 in <__mingw_CRTStartup> at /build/@dots{}/crt1.c:200
23496 004011F1 in <mainCRTStartup> at /build/@dots{}/crt1.c:222
23497 @end smallexample
23498
23499 @noindent
23500 From this traceback we can see that the exception was raised in
23501 @file{stb.adb} at line 5, which was reached from a procedure call in
23502 @file{stb.adb} at line 10, and so on. The @file{b~std.adb} is the binder file,
23503 which contains the call to the main program.
23504 @xref{Running gnatbind}. The remaining entries are assorted runtime routines,
23505 and the output will vary from platform to platform.
23506
23507 It is also possible to use @code{GDB} with these traceback addresses to debug
23508 the program. For example, we can break at a given code location, as reported
23509 in the stack traceback:
23510
23511 @smallexample
23512 $ gdb -nw stb
23513 @ifclear vms
23514 @noindent
23515 Furthermore, this feature is not implemented inside Windows DLL. Only
23516 the non-symbolic traceback is reported in this case.
23517 @end ifclear
23518
23519 (gdb) break *0x401373
23520 Breakpoint 1 at 0x401373: file stb.adb, line 5.
23521 @end smallexample
23522
23523 @noindent
23524 It is important to note that the stack traceback addresses
23525 do not change when debug information is included. This is particularly useful
23526 because it makes it possible to release software without debug information (to
23527 minimize object size), get a field report that includes a stack traceback
23528 whenever an internal bug occurs, and then be able to retrieve the sequence
23529 of calls with the same program compiled with debug information.
23530
23531 @node Tracebacks From Exception Occurrences (non-symbolic)
23532 @subsubsection Tracebacks From Exception Occurrences
23533
23534 @noindent
23535 Non-symbolic tracebacks are obtained by using the @option{-E} binder argument.
23536 The stack traceback is attached to the exception information string, and can
23537 be retrieved in an exception handler within the Ada program, by means of the
23538 Ada facilities defined in @code{Ada.Exceptions}. Here is a simple example:
23539
23540 @smallexample @c ada
23541 with Ada.Text_IO;
23542 with Ada.Exceptions;
23543
23544 procedure STB is
23545
23546 use Ada;
23547 use Ada.Exceptions;
23548
23549 procedure P1 is
23550 K : Positive := 1;
23551 begin
23552 K := K - 1;
23553 exception
23554 when E : others =>
23555 Text_IO.Put_Line (Exception_Information (E));
23556 end P1;
23557
23558 procedure P2 is
23559 begin
23560 P1;
23561 end P2;
23562
23563 begin
23564 P2;
23565 end STB;
23566 @end smallexample
23567
23568 @noindent
23569 This program will output:
23570
23571 @smallexample
23572 $ stb
23573
23574 Exception name: CONSTRAINT_ERROR
23575 Message: stb.adb:12
23576 Call stack traceback locations:
23577 0x4015e4 0x401633 0x401644 0x401461 0x4011c4 0x4011f1 0x77e892a4
23578 @end smallexample
23579
23580 @node Tracebacks From Anywhere in a Program (non-symbolic)
23581 @subsubsection Tracebacks From Anywhere in a Program
23582
23583 @noindent
23584 It is also possible to retrieve a stack traceback from anywhere in a
23585 program. For this you need to
23586 use the @code{GNAT.Traceback} API. This package includes a procedure called
23587 @code{Call_Chain} that computes a complete stack traceback, as well as useful
23588 display procedures described below. It is not necessary to use the
23589 @option{-E gnatbind} option in this case, because the stack traceback mechanism
23590 is invoked explicitly.
23591
23592 @noindent
23593 In the following example we compute a traceback at a specific location in
23594 the program, and we display it using @code{GNAT.Debug_Utilities.Image} to
23595 convert addresses to strings:
23596
23597 @smallexample @c ada
23598 with Ada.Text_IO;
23599 with GNAT.Traceback;
23600 with GNAT.Debug_Utilities;
23601
23602 procedure STB is
23603
23604 use Ada;
23605 use GNAT;
23606 use GNAT.Traceback;
23607
23608 procedure P1 is
23609 TB : Tracebacks_Array (1 .. 10);
23610 -- We are asking for a maximum of 10 stack frames.
23611 Len : Natural;
23612 -- Len will receive the actual number of stack frames returned.
23613 begin
23614 Call_Chain (TB, Len);
23615
23616 Text_IO.Put ("In STB.P1 : ");
23617
23618 for K in 1 .. Len loop
23619 Text_IO.Put (Debug_Utilities.Image (TB (K)));
23620 Text_IO.Put (' ');
23621 end loop;
23622
23623 Text_IO.New_Line;
23624 end P1;
23625
23626 procedure P2 is
23627 begin
23628 P1;
23629 end P2;
23630
23631 begin
23632 P2;
23633 end STB;
23634 @end smallexample
23635
23636 @smallexample
23637 $ gnatmake -g stb
23638 $ stb
23639
23640 In STB.P1 : 16#0040_F1E4# 16#0040_14F2# 16#0040_170B# 16#0040_171C#
23641 16#0040_1461# 16#0040_11C4# 16#0040_11F1# 16#77E8_92A4#
23642 @end smallexample
23643
23644 @noindent
23645 You can then get further information by invoking the @code{addr2line}
23646 tool as described earlier (note that the hexadecimal addresses
23647 need to be specified in C format, with a leading ``0x'').
23648
23649 @node Symbolic Traceback
23650 @subsection Symbolic Traceback
23651 @cindex traceback, symbolic
23652
23653 @noindent
23654 A symbolic traceback is a stack traceback in which procedure names are
23655 associated with each code location.
23656
23657 @noindent
23658 Note that this feature is not supported on all platforms. See
23659 @file{GNAT.Traceback.Symbolic spec in g-trasym.ads} for a complete
23660 list of currently supported platforms.
23661
23662 @noindent
23663 Note that the symbolic traceback requires that the program be compiled
23664 with debug information. If it is not compiled with debug information
23665 only the non-symbolic information will be valid.
23666
23667 @menu
23668 * Tracebacks From Exception Occurrences (symbolic)::
23669 * Tracebacks From Anywhere in a Program (symbolic)::
23670 @end menu
23671
23672 @node Tracebacks From Exception Occurrences (symbolic)
23673 @subsubsection Tracebacks From Exception Occurrences
23674
23675 @smallexample @c ada
23676 with Ada.Text_IO;
23677 with GNAT.Traceback.Symbolic;
23678
23679 procedure STB is
23680
23681 procedure P1 is
23682 begin
23683 raise Constraint_Error;
23684 end P1;
23685
23686 procedure P2 is
23687 begin
23688 P1;
23689 end P2;
23690
23691 procedure P3 is
23692 begin
23693 P2;
23694 end P3;
23695
23696 begin
23697 P3;
23698 exception
23699 when E : others =>
23700 Ada.Text_IO.Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
23701 end STB;
23702 @end smallexample
23703
23704 @smallexample
23705 $ gnatmake -g .\stb -bargs -E -largs -lgnat -laddr2line -lintl
23706 $ stb
23707
23708 0040149F in stb.p1 at stb.adb:8
23709 004014B7 in stb.p2 at stb.adb:13
23710 004014CF in stb.p3 at stb.adb:18
23711 004015DD in ada.stb at stb.adb:22
23712 00401461 in main at b~stb.adb:168
23713 004011C4 in __mingw_CRTStartup at crt1.c:200
23714 004011F1 in mainCRTStartup at crt1.c:222
23715 77E892A4 in ?? at ??:0
23716 @end smallexample
23717
23718 @noindent
23719 In the above example the ``.\'' syntax in the @command{gnatmake} command
23720 is currently required by @command{addr2line} for files that are in
23721 the current working directory.
23722 Moreover, the exact sequence of linker options may vary from platform
23723 to platform.
23724 The above @option{-largs} section is for Windows platforms. By contrast,
23725 under Unix there is no need for the @option{-largs} section.
23726 Differences across platforms are due to details of linker implementation.
23727
23728 @node Tracebacks From Anywhere in a Program (symbolic)
23729 @subsubsection Tracebacks From Anywhere in a Program
23730
23731 @noindent
23732 It is possible to get a symbolic stack traceback
23733 from anywhere in a program, just as for non-symbolic tracebacks.
23734 The first step is to obtain a non-symbolic
23735 traceback, and then call @code{Symbolic_Traceback} to compute the symbolic
23736 information. Here is an example:
23737
23738 @smallexample @c ada
23739 with Ada.Text_IO;
23740 with GNAT.Traceback;
23741 with GNAT.Traceback.Symbolic;
23742
23743 procedure STB is
23744
23745 use Ada;
23746 use GNAT.Traceback;
23747 use GNAT.Traceback.Symbolic;
23748
23749 procedure P1 is
23750 TB : Tracebacks_Array (1 .. 10);
23751 -- We are asking for a maximum of 10 stack frames.
23752 Len : Natural;
23753 -- Len will receive the actual number of stack frames returned.
23754 begin
23755 Call_Chain (TB, Len);
23756 Text_IO.Put_Line (Symbolic_Traceback (TB (1 .. Len)));
23757 end P1;
23758
23759 procedure P2 is
23760 begin
23761 P1;
23762 end P2;
23763
23764 begin
23765 P2;
23766 end STB;
23767 @end smallexample
23768
23769 @c ******************************
23770 @ifset vms
23771 @node Compatibility with HP Ada
23772 @chapter Compatibility with HP Ada
23773 @cindex Compatibility
23774
23775 @noindent
23776 @cindex DEC Ada
23777 @cindex HP Ada
23778 @cindex Compatibility between GNAT and HP Ada
23779 This chapter compares HP Ada (formerly known as ``DEC Ada'')
23780 for OpenVMS Alpha and GNAT for OpenVMS for Alpha and for I64.
23781 GNAT is highly compatible
23782 with HP Ada, and it should generally be straightforward to port code
23783 from the HP Ada environment to GNAT. However, there are a few language
23784 and implementation differences of which the user must be aware. These
23785 differences are discussed in this chapter. In
23786 addition, the operating environment and command structure for the
23787 compiler are different, and these differences are also discussed.
23788
23789 For further details on these and other compatibility issues,
23790 see Appendix E of the HP publication
23791 @cite{HP Ada, Technical Overview and Comparison on HP Platforms}.
23792
23793 Except where otherwise indicated, the description of GNAT for OpenVMS
23794 applies to both the Alpha and I64 platforms.
23795
23796 For information on porting Ada code from GNAT on Alpha OpenVMS to GNAT on
23797 I64 OpenVMS, see @ref{Transitioning to 64-Bit GNAT for OpenVMS}.
23798
23799 The discussion in this chapter addresses specifically the implementation
23800 of Ada 83 for HP OpenVMS Alpha Systems. In cases where the implementation
23801 of HP Ada differs between OpenVMS Alpha Systems and OpenVMS VAX Systems,
23802 GNAT always follows the Alpha implementation.
23803
23804 For GNAT running on other than VMS systems, all the HP Ada 83 pragmas and
23805 attributes are recognized, although only a subset of them can sensibly
23806 be implemented. The description of pragmas in
23807 @xref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference Manual}
23808 indicates whether or not they are applicable to non-VMS systems.
23809
23810 @menu
23811 * Ada Language Compatibility::
23812 * Differences in the Definition of Package System::
23813 * Language-Related Features::
23814 * The Package STANDARD::
23815 * The Package SYSTEM::
23816 * Tasking and Task-Related Features::
23817 * Pragmas and Pragma-Related Features::
23818 * Library of Predefined Units::
23819 * Bindings::
23820 * Main Program Definition::
23821 * Implementation-Defined Attributes::
23822 * Compiler and Run-Time Interfacing::
23823 * Program Compilation and Library Management::
23824 * Input-Output::
23825 * Implementation Limits::
23826 * Tools and Utilities::
23827 @end menu
23828
23829 @node Ada Language Compatibility
23830 @section Ada Language Compatibility
23831
23832 @noindent
23833 GNAT handles Ada 95 and Ada 2005 as well as Ada 83, whereas HP Ada is only
23834 for Ada 83. Ada 95 and Ada 2005 are almost completely upwards compatible
23835 with Ada 83, and therefore Ada 83 programs will compile
23836 and run under GNAT with
23837 no changes or only minor changes. The @cite{Annotated Ada Reference Manual}
23838 provides details on specific incompatibilities.
23839
23840 GNAT provides the switch @option{/83} on the @command{GNAT COMPILE} command,
23841 as well as the pragma @code{ADA_83}, to force the compiler to
23842 operate in Ada 83 mode. This mode does not guarantee complete
23843 conformance to Ada 83, but in practice is sufficient to
23844 eliminate most sources of incompatibilities.
23845 In particular, it eliminates the recognition of the
23846 additional Ada 95 and Ada 2005 keywords, so that their use as identifiers
23847 in Ada 83 programs is legal, and handles the cases of packages
23848 with optional bodies, and generics that instantiate unconstrained
23849 types without the use of @code{(<>)}.
23850
23851 @node Differences in the Definition of Package System
23852 @section Differences in the Definition of Package @code{System}
23853
23854 @noindent
23855 An Ada compiler is allowed to add
23856 implementation-dependent declarations to package @code{System}.
23857 In normal mode,
23858 GNAT does not take advantage of this permission, and the version of
23859 @code{System} provided by GNAT exactly matches that defined in the Ada
23860 Reference Manual.
23861
23862 However, HP Ada adds an extensive set of declarations to package
23863 @code{System},
23864 as fully documented in the HP Ada manuals. To minimize changes required
23865 for programs that make use of these extensions, GNAT provides the pragma
23866 @code{Extend_System} for extending the definition of package System. By using:
23867 @cindex pragma @code{Extend_System}
23868 @cindex @code{Extend_System} pragma
23869
23870 @smallexample @c ada
23871 @group
23872 @cartouche
23873 pragma Extend_System (Aux_DEC);
23874 @end cartouche
23875 @end group
23876 @end smallexample
23877
23878 @noindent
23879 the set of definitions in @code{System} is extended to include those in
23880 package @code{System.Aux_DEC}.
23881 @cindex @code{System.Aux_DEC} package
23882 @cindex @code{Aux_DEC} package (child of @code{System})
23883 These definitions are incorporated directly into package @code{System},
23884 as though they had been declared there. For a
23885 list of the declarations added, see the spec of this package,
23886 which can be found in the file @file{s-auxdec.ads} in the GNAT library.
23887 @cindex @file{s-auxdec.ads} file
23888 The pragma @code{Extend_System} is a configuration pragma, which means that
23889 it can be placed in the file @file{gnat.adc}, so that it will automatically
23890 apply to all subsequent compilations. See @ref{Configuration Pragmas},
23891 for further details.
23892
23893 An alternative approach that avoids the use of the non-standard
23894 @code{Extend_System} pragma is to add a context clause to the unit that
23895 references these facilities:
23896
23897 @smallexample @c ada
23898 @cartouche
23899 with System.Aux_DEC;
23900 use System.Aux_DEC;
23901 @end cartouche
23902 @end smallexample
23903
23904 @noindent
23905 The effect is not quite semantically identical to incorporating
23906 the declarations directly into package @code{System},
23907 but most programs will not notice a difference
23908 unless they use prefix notation (e.g.@: @code{System.Integer_8})
23909 to reference the entities directly in package @code{System}.
23910 For units containing such references,
23911 the prefixes must either be removed, or the pragma @code{Extend_System}
23912 must be used.
23913
23914 @node Language-Related Features
23915 @section Language-Related Features
23916
23917 @noindent
23918 The following sections highlight differences in types,
23919 representations of types, operations, alignment, and
23920 related topics.
23921
23922 @menu
23923 * Integer Types and Representations::
23924 * Floating-Point Types and Representations::
23925 * Pragmas Float_Representation and Long_Float::
23926 * Fixed-Point Types and Representations::
23927 * Record and Array Component Alignment::
23928 * Address Clauses::
23929 * Other Representation Clauses::
23930 @end menu
23931
23932 @node Integer Types and Representations
23933 @subsection Integer Types and Representations
23934
23935 @noindent
23936 The set of predefined integer types is identical in HP Ada and GNAT.
23937 Furthermore the representation of these integer types is also identical,
23938 including the capability of size clauses forcing biased representation.
23939
23940 In addition,
23941 HP Ada for OpenVMS Alpha systems has defined the
23942 following additional integer types in package @code{System}:
23943
23944 @itemize @bullet
23945
23946 @item
23947 @code{INTEGER_8}
23948
23949 @item
23950 @code{INTEGER_16}
23951
23952 @item
23953 @code{INTEGER_32}
23954
23955 @item
23956 @code{INTEGER_64}
23957
23958 @item
23959 @code{LARGEST_INTEGER}
23960 @end itemize
23961
23962 @noindent
23963 In GNAT, the first four of these types may be obtained from the
23964 standard Ada package @code{Interfaces}.
23965 Alternatively, by use of the pragma @code{Extend_System}, identical
23966 declarations can be referenced directly in package @code{System}.
23967 On both GNAT and HP Ada, the maximum integer size is 64 bits.
23968
23969 @node Floating-Point Types and Representations
23970 @subsection Floating-Point Types and Representations
23971 @cindex Floating-Point types
23972
23973 @noindent
23974 The set of predefined floating-point types is identical in HP Ada and GNAT.
23975 Furthermore the representation of these floating-point
23976 types is also identical. One important difference is that the default
23977 representation for HP Ada is @code{VAX_Float}, but the default representation
23978 for GNAT is IEEE.
23979
23980 Specific types may be declared to be @code{VAX_Float} or IEEE, using the
23981 pragma @code{Float_Representation} as described in the HP Ada
23982 documentation.
23983 For example, the declarations:
23984
23985 @smallexample @c ada
23986 @cartouche
23987 type F_Float is digits 6;
23988 pragma Float_Representation (VAX_Float, F_Float);
23989 @end cartouche
23990 @end smallexample
23991
23992 @noindent
23993 declares a type @code{F_Float} that will be represented in @code{VAX_Float}
23994 format.
23995 This set of declarations actually appears in @code{System.Aux_DEC},
23996 which contains
23997 the full set of additional floating-point declarations provided in
23998 the HP Ada version of package @code{System}.
23999 This and similar declarations may be accessed in a user program
24000 by using pragma @code{Extend_System}. The use of this
24001 pragma, and the related pragma @code{Long_Float} is described in further
24002 detail in the following section.
24003
24004 @node Pragmas Float_Representation and Long_Float
24005 @subsection Pragmas @code{Float_Representation} and @code{Long_Float}
24006
24007 @noindent
24008 HP Ada provides the pragma @code{Float_Representation}, which
24009 acts as a program library switch to allow control over
24010 the internal representation chosen for the predefined
24011 floating-point types declared in the package @code{Standard}.
24012 The format of this pragma is as follows:
24013
24014 @smallexample @c ada
24015 @cartouche
24016 pragma Float_Representation(VAX_Float | IEEE_Float);
24017 @end cartouche
24018 @end smallexample
24019
24020 @noindent
24021 This pragma controls the representation of floating-point
24022 types as follows:
24023
24024 @itemize @bullet
24025 @item
24026 @code{VAX_Float} specifies that floating-point
24027 types are represented by default with the VAX system hardware types
24028 @code{F-floating}, @code{D-floating}, @code{G-floating}.
24029 Note that the @code{H-floating}
24030 type was available only on VAX systems, and is not available
24031 in either HP Ada or GNAT.
24032
24033 @item
24034 @code{IEEE_Float} specifies that floating-point
24035 types are represented by default with the IEEE single and
24036 double floating-point types.
24037 @end itemize
24038
24039 @noindent
24040 GNAT provides an identical implementation of the pragma
24041 @code{Float_Representation}, except that it functions as a
24042 configuration pragma. Note that the
24043 notion of configuration pragma corresponds closely to the
24044 HP Ada notion of a program library switch.
24045
24046 When no pragma is used in GNAT, the default is @code{IEEE_Float},
24047 which is different
24048 from HP Ada 83, where the default is @code{VAX_Float}. In addition, the
24049 predefined libraries in GNAT are built using @code{IEEE_Float}, so it is not
24050 advisable to change the format of numbers passed to standard library
24051 routines, and if necessary explicit type conversions may be needed.
24052
24053 The use of @code{IEEE_Float} is recommended in GNAT since it is more
24054 efficient, and (given that it conforms to an international standard)
24055 potentially more portable.
24056 The situation in which @code{VAX_Float} may be useful is in interfacing
24057 to existing code and data that expect the use of @code{VAX_Float}.
24058 In such a situation use the predefined @code{VAX_Float}
24059 types in package @code{System}, as extended by
24060 @code{Extend_System}. For example, use @code{System.F_Float}
24061 to specify the 32-bit @code{F-Float} format.
24062
24063 @noindent
24064 On OpenVMS systems, HP Ada provides the pragma @code{Long_Float}
24065 to allow control over the internal representation chosen
24066 for the predefined type @code{Long_Float} and for floating-point
24067 type declarations with digits specified in the range 7 .. 15.
24068 The format of this pragma is as follows:
24069
24070 @smallexample @c ada
24071 @cartouche
24072 pragma Long_Float (D_FLOAT | G_FLOAT);
24073 @end cartouche
24074 @end smallexample
24075
24076 @node Fixed-Point Types and Representations
24077 @subsection Fixed-Point Types and Representations
24078
24079 @noindent
24080 On HP Ada for OpenVMS Alpha systems, rounding is
24081 away from zero for both positive and negative numbers.
24082 Therefore, @code{+0.5} rounds to @code{1},
24083 and @code{-0.5} rounds to @code{-1}.
24084
24085 On GNAT the results of operations
24086 on fixed-point types are in accordance with the Ada
24087 rules. In particular, results of operations on decimal
24088 fixed-point types are truncated.
24089
24090 @node Record and Array Component Alignment
24091 @subsection Record and Array Component Alignment
24092
24093 @noindent
24094 On HP Ada for OpenVMS Alpha, all non-composite components
24095 are aligned on natural boundaries. For example, 1-byte
24096 components are aligned on byte boundaries, 2-byte
24097 components on 2-byte boundaries, 4-byte components on 4-byte
24098 byte boundaries, and so on. The OpenVMS Alpha hardware
24099 runs more efficiently with naturally aligned data.
24100
24101 On GNAT, alignment rules are compatible
24102 with HP Ada for OpenVMS Alpha.
24103
24104 @node Address Clauses
24105 @subsection Address Clauses
24106
24107 @noindent
24108 In HP Ada and GNAT, address clauses are supported for
24109 objects and imported subprograms.
24110 The predefined type @code{System.Address} is a private type
24111 in both compilers on Alpha OpenVMS, with the same representation
24112 (it is simply a machine pointer). Addition, subtraction, and comparison
24113 operations are available in the standard Ada package
24114 @code{System.Storage_Elements}, or in package @code{System}
24115 if it is extended to include @code{System.Aux_DEC} using a
24116 pragma @code{Extend_System} as previously described.
24117
24118 Note that code that @code{with}'s both this extended package @code{System}
24119 and the package @code{System.Storage_Elements} should not @code{use}
24120 both packages, or ambiguities will result. In general it is better
24121 not to mix these two sets of facilities. The Ada package was
24122 designed specifically to provide the kind of features that HP Ada
24123 adds directly to package @code{System}.
24124
24125 The type @code{System.Address} is a 64-bit integer type in GNAT for
24126 I64 OpenVMS. For more information,
24127 see @ref{Transitioning to 64-Bit GNAT for OpenVMS}.
24128
24129 GNAT is compatible with HP Ada in its handling of address
24130 clauses, except for some limitations in
24131 the form of address clauses for composite objects with
24132 initialization. Such address clauses are easily replaced
24133 by the use of an explicitly-defined constant as described
24134 in the Ada Reference Manual (13.1(22)). For example, the sequence
24135 of declarations:
24136
24137 @smallexample @c ada
24138 @cartouche
24139 X, Y : Integer := Init_Func;
24140 Q : String (X .. Y) := "abc";
24141 @dots{}
24142 for Q'Address use Compute_Address;
24143 @end cartouche
24144 @end smallexample
24145
24146 @noindent
24147 will be rejected by GNAT, since the address cannot be computed at the time
24148 that @code{Q} is declared. To achieve the intended effect, write instead:
24149
24150 @smallexample @c ada
24151 @group
24152 @cartouche
24153 X, Y : Integer := Init_Func;
24154 Q_Address : constant Address := Compute_Address;
24155 Q : String (X .. Y) := "abc";
24156 @dots{}
24157 for Q'Address use Q_Address;
24158 @end cartouche
24159 @end group
24160 @end smallexample
24161
24162 @noindent
24163 which will be accepted by GNAT (and other Ada compilers), and is also
24164 compatible with Ada 83. A fuller description of the restrictions
24165 on address specifications is found in @ref{Top, GNAT Reference Manual,
24166 About This Guide, gnat_rm, GNAT Reference Manual}.
24167
24168 @node Other Representation Clauses
24169 @subsection Other Representation Clauses
24170
24171 @noindent
24172 GNAT implements in a compatible manner all the representation
24173 clauses supported by HP Ada. In addition, GNAT
24174 implements the representation clause forms that were introduced in Ada 95,
24175 including @code{COMPONENT_SIZE} and @code{SIZE} clauses for objects.
24176
24177 @node The Package STANDARD
24178 @section The Package @code{STANDARD}
24179
24180 @noindent
24181 The package @code{STANDARD}, as implemented by HP Ada, is fully
24182 described in the @cite{Ada Reference Manual} and in the
24183 @cite{HP Ada Language Reference Manual}. As implemented by GNAT, the
24184 package @code{STANDARD} is described in the @cite{Ada Reference Manual}.
24185
24186 In addition, HP Ada supports the Latin-1 character set in
24187 the type @code{CHARACTER}. GNAT supports the Latin-1 character set
24188 in the type @code{CHARACTER} and also Unicode (ISO 10646 BMP) in
24189 the type @code{WIDE_CHARACTER}.
24190
24191 The floating-point types supported by GNAT are those
24192 supported by HP Ada, but the defaults are different, and are controlled by
24193 pragmas. See @ref{Floating-Point Types and Representations}, for details.
24194
24195 @node The Package SYSTEM
24196 @section The Package @code{SYSTEM}
24197
24198 @noindent
24199 HP Ada provides a specific version of the package
24200 @code{SYSTEM} for each platform on which the language is implemented.
24201 For the complete spec of the package @code{SYSTEM}, see
24202 Appendix F of the @cite{HP Ada Language Reference Manual}.
24203
24204 On HP Ada, the package @code{SYSTEM} includes the following conversion
24205 functions:
24206 @itemize @bullet
24207 @item @code{TO_ADDRESS(INTEGER)}
24208
24209 @item @code{TO_ADDRESS(UNSIGNED_LONGWORD)}
24210
24211 @item @code{TO_ADDRESS(}@i{universal_integer}@code{)}
24212
24213 @item @code{TO_INTEGER(ADDRESS)}
24214
24215 @item @code{TO_UNSIGNED_LONGWORD(ADDRESS)}
24216
24217 @item Function @code{IMPORT_VALUE return UNSIGNED_LONGWORD} and the
24218 functions @code{IMPORT_ADDRESS} and @code{IMPORT_LARGEST_VALUE}
24219 @end itemize
24220
24221 @noindent
24222 By default, GNAT supplies a version of @code{SYSTEM} that matches
24223 the definition given in the @cite{Ada Reference Manual}.
24224 This
24225 is a subset of the HP system definitions, which is as
24226 close as possible to the original definitions. The only difference
24227 is that the definition of @code{SYSTEM_NAME} is different:
24228
24229 @smallexample @c ada
24230 @cartouche
24231 type Name is (SYSTEM_NAME_GNAT);
24232 System_Name : constant Name := SYSTEM_NAME_GNAT;
24233 @end cartouche
24234 @end smallexample
24235
24236 @noindent
24237 Also, GNAT adds the Ada declarations for
24238 @code{BIT_ORDER} and @code{DEFAULT_BIT_ORDER}.
24239
24240 However, the use of the following pragma causes GNAT
24241 to extend the definition of package @code{SYSTEM} so that it
24242 encompasses the full set of HP-specific extensions,
24243 including the functions listed above:
24244
24245 @smallexample @c ada
24246 @cartouche
24247 pragma Extend_System (Aux_DEC);
24248 @end cartouche
24249 @end smallexample
24250
24251 @noindent
24252 The pragma @code{Extend_System} is a configuration pragma that
24253 is most conveniently placed in the @file{gnat.adc} file. @xref{Pragma
24254 Extend_System,,, gnat_rm, GNAT Reference Manual} for further details.
24255
24256 HP Ada does not allow the recompilation of the package
24257 @code{SYSTEM}. Instead HP Ada provides several pragmas
24258 (@code{SYSTEM_NAME}, @code{STORAGE_UNIT}, and @code{MEMORY_SIZE})
24259 to modify values in the package @code{SYSTEM}.
24260 On OpenVMS Alpha systems, the pragma
24261 @code{SYSTEM_NAME} takes the enumeration literal @code{OPENVMS_AXP} as
24262 its single argument.
24263
24264 GNAT does permit the recompilation of package @code{SYSTEM} using
24265 the special switch @option{-gnatg}, and this switch can be used if
24266 it is necessary to modify the definitions in @code{SYSTEM}. GNAT does
24267 not permit the specification of @code{SYSTEM_NAME}, @code{STORAGE_UNIT}
24268 or @code{MEMORY_SIZE} by any other means.
24269
24270 On GNAT systems, the pragma @code{SYSTEM_NAME} takes the
24271 enumeration literal @code{SYSTEM_NAME_GNAT}.
24272
24273 The definitions provided by the use of
24274
24275 @smallexample @c ada
24276 pragma Extend_System (AUX_Dec);
24277 @end smallexample
24278
24279 @noindent
24280 are virtually identical to those provided by the HP Ada 83 package
24281 @code{SYSTEM}. One important difference is that the name of the
24282 @code{TO_ADDRESS}
24283 function for type @code{UNSIGNED_LONGWORD} is changed to
24284 @code{TO_ADDRESS_LONG}.
24285 @xref{Address Clauses,,, gnat_rm, GNAT Reference Manual} for a
24286 discussion of why this change was necessary.
24287
24288 @noindent
24289 The version of @code{TO_ADDRESS} taking a @i{universal_integer} argument
24290 is in fact
24291 an extension to Ada 83 not strictly compatible with the reference manual.
24292 GNAT, in order to be exactly compatible with the standard,
24293 does not provide this capability. In HP Ada 83, the
24294 point of this definition is to deal with a call like:
24295
24296 @smallexample @c ada
24297 TO_ADDRESS (16#12777#);
24298 @end smallexample
24299
24300 @noindent
24301 Normally, according to Ada 83 semantics, one would expect this to be
24302 ambiguous, since it matches both the @code{INTEGER} and
24303 @code{UNSIGNED_LONGWORD} forms of @code{TO_ADDRESS}.
24304 However, in HP Ada 83, there is no ambiguity, since the
24305 definition using @i{universal_integer} takes precedence.
24306
24307 In GNAT, since the version with @i{universal_integer} cannot be supplied,
24308 it is
24309 not possible to be 100% compatible. Since there are many programs using
24310 numeric constants for the argument to @code{TO_ADDRESS}, the decision in
24311 GNAT was
24312 to change the name of the function in the @code{UNSIGNED_LONGWORD} case,
24313 so the declarations provided in the GNAT version of @code{AUX_Dec} are:
24314
24315 @smallexample @c ada
24316 function To_Address (X : Integer) return Address;
24317 pragma Pure_Function (To_Address);
24318
24319 function To_Address_Long (X : Unsigned_Longword) return Address;
24320 pragma Pure_Function (To_Address_Long);
24321 @end smallexample
24322
24323 @noindent
24324 This means that programs using @code{TO_ADDRESS} for
24325 @code{UNSIGNED_LONGWORD} must change the name to @code{TO_ADDRESS_LONG}.
24326
24327 @node Tasking and Task-Related Features
24328 @section Tasking and Task-Related Features
24329
24330 @noindent
24331 This section compares the treatment of tasking in GNAT
24332 and in HP Ada for OpenVMS Alpha.
24333 The GNAT description applies to both Alpha and I64 OpenVMS.
24334 For detailed information on tasking in
24335 HP Ada, see the @cite{HP Ada Language Reference Manual} and the
24336 relevant run-time reference manual.
24337
24338 @menu
24339 * Implementation of Tasks in HP Ada for OpenVMS Alpha Systems::
24340 * Assigning Task IDs::
24341 * Task IDs and Delays::
24342 * Task-Related Pragmas::
24343 * Scheduling and Task Priority::
24344 * The Task Stack::
24345 * External Interrupts::
24346 @end menu
24347
24348 @node Implementation of Tasks in HP Ada for OpenVMS Alpha Systems
24349 @subsection Implementation of Tasks in HP Ada for OpenVMS Alpha Systems
24350
24351 @noindent
24352 On OpenVMS Alpha systems, each Ada task (except a passive
24353 task) is implemented as a single stream of execution
24354 that is created and managed by the kernel. On these
24355 systems, HP Ada tasking support is based on DECthreads,
24356 an implementation of the POSIX standard for threads.
24357
24358 Also, on OpenVMS Alpha systems, HP Ada tasks and foreign
24359 code that calls DECthreads routines can be used together.
24360 The interaction between Ada tasks and DECthreads routines
24361 can have some benefits. For example when on OpenVMS Alpha,
24362 HP Ada can call C code that is already threaded.
24363
24364 GNAT uses the facilities of DECthreads,
24365 and Ada tasks are mapped to threads.
24366
24367 @node Assigning Task IDs
24368 @subsection Assigning Task IDs
24369
24370 @noindent
24371 The HP Ada Run-Time Library always assigns @code{%TASK 1} to
24372 the environment task that executes the main program. On
24373 OpenVMS Alpha systems, @code{%TASK 0} is often used for tasks
24374 that have been created but are not yet activated.
24375
24376 On OpenVMS Alpha systems, task IDs are assigned at
24377 activation. On GNAT systems, task IDs are also assigned at
24378 task creation but do not have the same form or values as
24379 task ID values in HP Ada. There is no null task, and the
24380 environment task does not have a specific task ID value.
24381
24382 @node Task IDs and Delays
24383 @subsection Task IDs and Delays
24384
24385 @noindent
24386 On OpenVMS Alpha systems, tasking delays are implemented
24387 using Timer System Services. The Task ID is used for the
24388 identification of the timer request (the @code{REQIDT} parameter).
24389 If Timers are used in the application take care not to use
24390 @code{0} for the identification, because cancelling such a timer
24391 will cancel all timers and may lead to unpredictable results.
24392
24393 @node Task-Related Pragmas
24394 @subsection Task-Related Pragmas
24395
24396 @noindent
24397 Ada supplies the pragma @code{TASK_STORAGE}, which allows
24398 specification of the size of the guard area for a task
24399 stack. (The guard area forms an area of memory that has no
24400 read or write access and thus helps in the detection of
24401 stack overflow.) On OpenVMS Alpha systems, if the pragma
24402 @code{TASK_STORAGE} specifies a value of zero, a minimal guard
24403 area is created. In the absence of a pragma @code{TASK_STORAGE},
24404 a default guard area is created.
24405
24406 GNAT supplies the following task-related pragmas:
24407
24408 @itemize @bullet
24409 @item @code{TASK_INFO}
24410
24411 This pragma appears within a task definition and
24412 applies to the task in which it appears. The argument
24413 must be of type @code{SYSTEM.TASK_INFO.TASK_INFO_TYPE}.
24414
24415 @item @code{TASK_STORAGE}
24416
24417 GNAT implements pragma @code{TASK_STORAGE} in the same way as HP Ada.
24418 Both HP Ada and GNAT supply the pragmas @code{PASSIVE},
24419 @code{SUPPRESS}, and @code{VOLATILE}.
24420 @end itemize
24421 @node Scheduling and Task Priority
24422 @subsection Scheduling and Task Priority
24423
24424 @noindent
24425 HP Ada implements the Ada language requirement that
24426 when two tasks are eligible for execution and they have
24427 different priorities, the lower priority task does not
24428 execute while the higher priority task is waiting. The HP
24429 Ada Run-Time Library keeps a task running until either the
24430 task is suspended or a higher priority task becomes ready.
24431
24432 On OpenVMS Alpha systems, the default strategy is round-
24433 robin with preemption. Tasks of equal priority take turns
24434 at the processor. A task is run for a certain period of
24435 time and then placed at the tail of the ready queue for
24436 its priority level.
24437
24438 HP Ada provides the implementation-defined pragma @code{TIME_SLICE},
24439 which can be used to enable or disable round-robin
24440 scheduling of tasks with the same priority.
24441 See the relevant HP Ada run-time reference manual for
24442 information on using the pragmas to control HP Ada task
24443 scheduling.
24444
24445 GNAT follows the scheduling rules of Annex D (Real-Time
24446 Annex) of the @cite{Ada Reference Manual}. In general, this
24447 scheduling strategy is fully compatible with HP Ada
24448 although it provides some additional constraints (as
24449 fully documented in Annex D).
24450 GNAT implements time slicing control in a manner compatible with
24451 HP Ada 83, by means of the pragma @code{Time_Slice}, whose semantics
24452 are identical to the HP Ada 83 pragma of the same name.
24453 Note that it is not possible to mix GNAT tasking and
24454 HP Ada 83 tasking in the same program, since the two run-time
24455 libraries are not compatible.
24456
24457 @node The Task Stack
24458 @subsection The Task Stack
24459
24460 @noindent
24461 In HP Ada, a task stack is allocated each time a
24462 non-passive task is activated. As soon as the task is
24463 terminated, the storage for the task stack is deallocated.
24464 If you specify a size of zero (bytes) with @code{T'STORAGE_SIZE},
24465 a default stack size is used. Also, regardless of the size
24466 specified, some additional space is allocated for task
24467 management purposes. On OpenVMS Alpha systems, at least
24468 one page is allocated.
24469
24470 GNAT handles task stacks in a similar manner. In accordance with
24471 the Ada rules, it provides the pragma @code{STORAGE_SIZE} as
24472 an alternative method for controlling the task stack size.
24473 The specification of the attribute @code{T'STORAGE_SIZE} is also
24474 supported in a manner compatible with HP Ada.
24475
24476 @node External Interrupts
24477 @subsection External Interrupts
24478
24479 @noindent
24480 On HP Ada, external interrupts can be associated with task entries.
24481 GNAT is compatible with HP Ada in its handling of external interrupts.
24482
24483 @node Pragmas and Pragma-Related Features
24484 @section Pragmas and Pragma-Related Features
24485
24486 @noindent
24487 Both HP Ada and GNAT supply all language-defined pragmas
24488 as specified by the Ada 83 standard. GNAT also supplies all
24489 language-defined pragmas introduced by Ada 95 and Ada 2005.
24490 In addition, GNAT implements the implementation-defined pragmas
24491 from HP Ada 83.
24492
24493 @itemize @bullet
24494 @item @code{AST_ENTRY}
24495
24496 @item @code{COMMON_OBJECT}
24497
24498 @item @code{COMPONENT_ALIGNMENT}
24499
24500 @item @code{EXPORT_EXCEPTION}
24501
24502 @item @code{EXPORT_FUNCTION}
24503
24504 @item @code{EXPORT_OBJECT}
24505
24506 @item @code{EXPORT_PROCEDURE}
24507
24508 @item @code{EXPORT_VALUED_PROCEDURE}
24509
24510 @item @code{FLOAT_REPRESENTATION}
24511
24512 @item @code{IDENT}
24513
24514 @item @code{IMPORT_EXCEPTION}
24515
24516 @item @code{IMPORT_FUNCTION}
24517
24518 @item @code{IMPORT_OBJECT}
24519
24520 @item @code{IMPORT_PROCEDURE}
24521
24522 @item @code{IMPORT_VALUED_PROCEDURE}
24523
24524 @item @code{INLINE_GENERIC}
24525
24526 @item @code{INTERFACE_NAME}
24527
24528 @item @code{LONG_FLOAT}
24529
24530 @item @code{MAIN_STORAGE}
24531
24532 @item @code{PASSIVE}
24533
24534 @item @code{PSECT_OBJECT}
24535
24536 @item @code{SHARE_GENERIC}
24537
24538 @item @code{SUPPRESS_ALL}
24539
24540 @item @code{TASK_STORAGE}
24541
24542 @item @code{TIME_SLICE}
24543
24544 @item @code{TITLE}
24545 @end itemize
24546
24547 @noindent
24548 These pragmas are all fully implemented, with the exception of @code{TITLE},
24549 @code{PASSIVE}, and @code{SHARE_GENERIC}, which are
24550 recognized, but which have no
24551 effect in GNAT. The effect of @code{PASSIVE} may be obtained by the
24552 use of Ada protected objects. In GNAT, all generics are inlined.
24553
24554 Unlike HP Ada, the GNAT ``@code{EXPORT_}@i{subprogram}'' pragmas require
24555 a separate subprogram specification which must appear before the
24556 subprogram body.
24557
24558 GNAT also supplies a number of implementation-defined pragmas as follows:
24559 @itemize @bullet
24560 @item @code{ABORT_DEFER}
24561
24562 @item @code{ADA_83}
24563
24564 @item @code{ADA_95}
24565
24566 @item @code{ADA_05}
24567
24568 @item @code{ANNOTATE}
24569
24570 @item @code{ASSERT}
24571
24572 @item @code{C_PASS_BY_COPY}
24573
24574 @item @code{CPP_CLASS}
24575
24576 @item @code{CPP_CONSTRUCTOR}
24577
24578 @item @code{CPP_DESTRUCTOR}
24579
24580 @item @code{DEBUG}
24581
24582 @item @code{EXTEND_SYSTEM}
24583
24584 @item @code{LINKER_ALIAS}
24585
24586 @item @code{LINKER_SECTION}
24587
24588 @item @code{MACHINE_ATTRIBUTE}
24589
24590 @item @code{NO_RETURN}
24591
24592 @item @code{PURE_FUNCTION}
24593
24594 @item @code{SOURCE_FILE_NAME}
24595
24596 @item @code{SOURCE_REFERENCE}
24597
24598 @item @code{TASK_INFO}
24599
24600 @item @code{UNCHECKED_UNION}
24601
24602 @item @code{UNIMPLEMENTED_UNIT}
24603
24604 @item @code{UNIVERSAL_DATA}
24605
24606 @item @code{UNSUPPRESS}
24607
24608 @item @code{WARNINGS}
24609
24610 @item @code{WEAK_EXTERNAL}
24611 @end itemize
24612
24613 @noindent
24614 For full details on these GNAT implementation-defined pragmas,
24615 see @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference
24616 Manual}.
24617
24618 @menu
24619 * Restrictions on the Pragma INLINE::
24620 * Restrictions on the Pragma INTERFACE::
24621 * Restrictions on the Pragma SYSTEM_NAME::
24622 @end menu
24623
24624 @node Restrictions on the Pragma INLINE
24625 @subsection Restrictions on Pragma @code{INLINE}
24626
24627 @noindent
24628 HP Ada enforces the following restrictions on the pragma @code{INLINE}:
24629 @itemize @bullet
24630 @item Parameters cannot have a task type.
24631
24632 @item Function results cannot be task types, unconstrained
24633 array types, or unconstrained types with discriminants.
24634
24635 @item Bodies cannot declare the following:
24636 @itemize @bullet
24637 @item Subprogram body or stub (imported subprogram is allowed)
24638
24639 @item Tasks
24640
24641 @item Generic declarations
24642
24643 @item Instantiations
24644
24645 @item Exceptions
24646
24647 @item Access types (types derived from access types allowed)
24648
24649 @item Array or record types
24650
24651 @item Dependent tasks
24652
24653 @item Direct recursive calls of subprogram or containing
24654 subprogram, directly or via a renaming
24655
24656 @end itemize
24657 @end itemize
24658
24659 @noindent
24660 In GNAT, the only restriction on pragma @code{INLINE} is that the
24661 body must occur before the call if both are in the same
24662 unit, and the size must be appropriately small. There are
24663 no other specific restrictions which cause subprograms to
24664 be incapable of being inlined.
24665
24666 @node Restrictions on the Pragma INTERFACE
24667 @subsection Restrictions on Pragma @code{INTERFACE}
24668
24669 @noindent
24670 The following restrictions on pragma @code{INTERFACE}
24671 are enforced by both HP Ada and GNAT:
24672 @itemize @bullet
24673 @item Languages accepted: Ada, Bliss, C, Fortran, Default.
24674 Default is the default on OpenVMS Alpha systems.
24675
24676 @item Parameter passing: Language specifies default
24677 mechanisms but can be overridden with an @code{EXPORT} pragma.
24678
24679 @itemize @bullet
24680 @item Ada: Use internal Ada rules.
24681
24682 @item Bliss, C: Parameters must be mode @code{in}; cannot be
24683 record or task type. Result cannot be a string, an
24684 array, or a record.
24685
24686 @item Fortran: Parameters cannot have a task type. Result cannot
24687 be a string, an array, or a record.
24688 @end itemize
24689 @end itemize
24690
24691 @noindent
24692 GNAT is entirely upwards compatible with HP Ada, and in addition allows
24693 record parameters for all languages.
24694
24695 @node Restrictions on the Pragma SYSTEM_NAME
24696 @subsection Restrictions on Pragma @code{SYSTEM_NAME}
24697
24698 @noindent
24699 For HP Ada for OpenVMS Alpha, the enumeration literal
24700 for the type @code{NAME} is @code{OPENVMS_AXP}.
24701 In GNAT, the enumeration
24702 literal for the type @code{NAME} is @code{SYSTEM_NAME_GNAT}.
24703
24704 @node Library of Predefined Units
24705 @section Library of Predefined Units
24706
24707 @noindent
24708 A library of predefined units is provided as part of the
24709 HP Ada and GNAT implementations. HP Ada does not provide
24710 the package @code{MACHINE_CODE} but instead recommends importing
24711 assembler code.
24712
24713 The GNAT versions of the HP Ada Run-Time Library (@code{ADA$PREDEFINED:})
24714 units are taken from the OpenVMS Alpha version, not the OpenVMS VAX
24715 version.
24716 The HP Ada Predefined Library units are modified to remove post-Ada 83
24717 incompatibilities and to make them interoperable with GNAT
24718 (@pxref{Changes to DECLIB}, for details).
24719 The units are located in the @file{DECLIB} directory.
24720
24721 The GNAT RTL is contained in
24722 the @file{ADALIB} directory, and
24723 the default search path is set up to find @code{DECLIB} units in preference
24724 to @code{ADALIB} units with the same name (@code{TEXT_IO},
24725 @code{SEQUENTIAL_IO}, and @code{DIRECT_IO}, for example).
24726
24727 @menu
24728 * Changes to DECLIB::
24729 @end menu
24730
24731 @node Changes to DECLIB
24732 @subsection Changes to @code{DECLIB}
24733
24734 @noindent
24735 The changes made to the HP Ada predefined library for GNAT and post-Ada 83
24736 compatibility are minor and include the following:
24737
24738 @itemize @bullet
24739 @item Adjusting the location of pragmas and record representation
24740 clauses to obey Ada 95 (and thus Ada 2005) rules
24741
24742 @item Adding the proper notation to generic formal parameters
24743 that take unconstrained types in instantiation
24744
24745 @item Adding pragma @code{ELABORATE_BODY} to package specs
24746 that have package bodies not otherwise allowed
24747
24748 @item Replacing occurrences of the identifier ``@code{PROTECTED}'' by
24749 ``@code{PROTECTD}''.
24750 Currently these are found only in the @code{STARLET} package spec.
24751
24752 @item Changing @code{SYSTEM.ADDRESS} to @code{SYSTEM.SHORT_ADDRESS}
24753 where the address size is constrained to 32 bits.
24754 @end itemize
24755
24756 @noindent
24757 None of the above changes is visible to users.
24758
24759 @node Bindings
24760 @section Bindings
24761
24762 @noindent
24763 On OpenVMS Alpha, HP Ada provides the following strongly-typed bindings:
24764 @itemize @bullet
24765
24766 @item Command Language Interpreter (CLI interface)
24767
24768 @item DECtalk Run-Time Library (DTK interface)
24769
24770 @item Librarian utility routines (LBR interface)
24771
24772 @item General Purpose Run-Time Library (LIB interface)
24773
24774 @item Math Run-Time Library (MTH interface)
24775
24776 @item National Character Set Run-Time Library (NCS interface)
24777
24778 @item Compiled Code Support Run-Time Library (OTS interface)
24779
24780 @item Parallel Processing Run-Time Library (PPL interface)
24781
24782 @item Screen Management Run-Time Library (SMG interface)
24783
24784 @item Sort Run-Time Library (SOR interface)
24785
24786 @item String Run-Time Library (STR interface)
24787
24788 @item STARLET System Library
24789 @findex Starlet
24790
24791 @item X Window System Version 11R4 and 11R5 (X, XLIB interface)
24792
24793 @item X Windows Toolkit (XT interface)
24794
24795 @item X/Motif Version 1.1.3 and 1.2 (XM interface)
24796 @end itemize
24797
24798 @noindent
24799 GNAT provides implementations of these HP bindings in the @code{DECLIB}
24800 directory, on both the Alpha and I64 OpenVMS platforms.
24801
24802 The X/Motif bindings used to build @code{DECLIB} are whatever versions are
24803 in the
24804 HP Ada @file{ADA$PREDEFINED} directory with extension @file{.ADC}.
24805 A pragma @code{Linker_Options} has been added to packages @code{Xm},
24806 @code{Xt}, and @code{X_Lib}
24807 causing the default X/Motif sharable image libraries to be linked in. This
24808 is done via options files named @file{xm.opt}, @file{xt.opt}, and
24809 @file{x_lib.opt} (also located in the @file{DECLIB} directory).
24810
24811 It may be necessary to edit these options files to update or correct the
24812 library names if, for example, the newer X/Motif bindings from
24813 @file{ADA$EXAMPLES}
24814 had been (previous to installing GNAT) copied and renamed to supersede the
24815 default @file{ADA$PREDEFINED} versions.
24816
24817 @menu
24818 * Shared Libraries and Options Files::
24819 * Interfaces to C::
24820 @end menu
24821
24822 @node Shared Libraries and Options Files
24823 @subsection Shared Libraries and Options Files
24824
24825 @noindent
24826 When using the HP Ada
24827 predefined X and Motif bindings, the linking with their sharable images is
24828 done automatically by @command{GNAT LINK}.
24829 When using other X and Motif bindings, you need
24830 to add the corresponding sharable images to the command line for
24831 @code{GNAT LINK}. When linking with shared libraries, or with
24832 @file{.OPT} files, you must
24833 also add them to the command line for @command{GNAT LINK}.
24834
24835 A shared library to be used with GNAT is built in the same way as other
24836 libraries under VMS. The VMS Link command can be used in standard fashion.
24837
24838 @node Interfaces to C
24839 @subsection Interfaces to C
24840
24841 @noindent
24842 HP Ada
24843 provides the following Ada types and operations:
24844
24845 @itemize @bullet
24846 @item C types package (@code{C_TYPES})
24847
24848 @item C strings (@code{C_TYPES.NULL_TERMINATED})
24849
24850 @item Other_types (@code{SHORT_INT})
24851 @end itemize
24852
24853 @noindent
24854 Interfacing to C with GNAT, you can use the above approach
24855 described for HP Ada or the facilities of Annex B of
24856 the @cite{Ada Reference Manual} (packages @code{INTERFACES.C},
24857 @code{INTERFACES.C.STRINGS} and @code{INTERFACES.C.POINTERS}). For more
24858 information, see @ref{Interfacing to C,,, gnat_rm, GNAT Reference Manual}.
24859
24860 The @option{-gnatF} qualifier forces default and explicit
24861 @code{External_Name} parameters in pragmas @code{Import} and @code{Export}
24862 to be uppercased for compatibility with the default behavior
24863 of HP C. The qualifier has no effect on @code{Link_Name} parameters.
24864
24865 @node Main Program Definition
24866 @section Main Program Definition
24867
24868 @noindent
24869 The following section discusses differences in the
24870 definition of main programs on HP Ada and GNAT.
24871 On HP Ada, main programs are defined to meet the
24872 following conditions:
24873 @itemize @bullet
24874 @item Procedure with no formal parameters (returns @code{0} upon
24875 normal completion)
24876
24877 @item Procedure with no formal parameters (returns @code{42} when
24878 an unhandled exception is raised)
24879
24880 @item Function with no formal parameters whose returned value
24881 is of a discrete type
24882
24883 @item Procedure with one @code{out} formal of a discrete type for
24884 which a specification of pragma @code{EXPORT_VALUED_PROCEDURE} is given.
24885
24886 @end itemize
24887
24888 @noindent
24889 When declared with the pragma @code{EXPORT_VALUED_PROCEDURE},
24890 a main function or main procedure returns a discrete
24891 value whose size is less than 64 bits (32 on VAX systems),
24892 the value is zero- or sign-extended as appropriate.
24893 On GNAT, main programs are defined as follows:
24894 @itemize @bullet
24895 @item Must be a non-generic, parameterless subprogram that
24896 is either a procedure or function returning an Ada
24897 @code{STANDARD.INTEGER} (the predefined type)
24898
24899 @item Cannot be a generic subprogram or an instantiation of a
24900 generic subprogram
24901 @end itemize
24902
24903 @node Implementation-Defined Attributes
24904 @section Implementation-Defined Attributes
24905
24906 @noindent
24907 GNAT provides all HP Ada implementation-defined
24908 attributes.
24909
24910 @node Compiler and Run-Time Interfacing
24911 @section Compiler and Run-Time Interfacing
24912
24913 @noindent
24914 HP Ada provides the following qualifiers to pass options to the linker
24915 (ACS LINK):
24916 @itemize @bullet
24917 @item @option{/WAIT} and @option{/SUBMIT}
24918
24919 @item @option{/COMMAND}
24920
24921 @item @option{/@r{[}NO@r{]}MAP}
24922
24923 @item @option{/OUTPUT=@var{file-spec}}
24924
24925 @item @option{/@r{[}NO@r{]}DEBUG} and @option{/@r{[}NO@r{]}TRACEBACK}
24926 @end itemize
24927
24928 @noindent
24929 To pass options to the linker, GNAT provides the following
24930 switches:
24931
24932 @itemize @bullet
24933 @item @option{/EXECUTABLE=@var{exec-name}}
24934
24935 @item @option{/VERBOSE}
24936
24937 @item @option{/@r{[}NO@r{]}DEBUG} and @option{/@r{[}NO@r{]}TRACEBACK}
24938 @end itemize
24939
24940 @noindent
24941 For more information on these switches, see
24942 @ref{Switches for gnatlink}.
24943 In HP Ada, the command-line switch @option{/OPTIMIZE} is available
24944 to control optimization. HP Ada also supplies the
24945 following pragmas:
24946 @itemize @bullet
24947 @item @code{OPTIMIZE}
24948
24949 @item @code{INLINE}
24950
24951 @item @code{INLINE_GENERIC}
24952
24953 @item @code{SUPPRESS_ALL}
24954
24955 @item @code{PASSIVE}
24956 @end itemize
24957
24958 @noindent
24959 In GNAT, optimization is controlled strictly by command
24960 line parameters, as described in the corresponding section of this guide.
24961 The HP pragmas for control of optimization are
24962 recognized but ignored.
24963
24964 Note that in GNAT, the default is optimization off, whereas in HP Ada
24965 the default is that optimization is turned on.
24966
24967 @node Program Compilation and Library Management
24968 @section Program Compilation and Library Management
24969
24970 @noindent
24971 HP Ada and GNAT provide a comparable set of commands to
24972 build programs. HP Ada also provides a program library,
24973 which is a concept that does not exist on GNAT. Instead,
24974 GNAT provides directories of sources that are compiled as
24975 needed.
24976
24977 The following table summarizes
24978 the HP Ada commands and provides
24979 equivalent GNAT commands. In this table, some GNAT
24980 equivalents reflect the fact that GNAT does not use the
24981 concept of a program library. Instead, it uses a model
24982 in which collections of source and object files are used
24983 in a manner consistent with other languages like C and
24984 Fortran. Therefore, standard system file commands are used
24985 to manipulate these elements. Those GNAT commands are marked with
24986 an asterisk.
24987 Note that, unlike HP Ada, none of the GNAT commands accepts wild cards.
24988
24989 @need 1500
24990 @multitable @columnfractions .35 .65
24991
24992 @item @emph{HP Ada Command}
24993 @tab @emph{GNAT Equivalent / Description}
24994
24995 @item @command{ADA}
24996 @tab @command{GNAT COMPILE}@*
24997 Invokes the compiler to compile one or more Ada source files.
24998
24999 @item @command{ACS ATTACH}@*
25000 @tab [No equivalent]@*
25001 Switches control of terminal from current process running the program
25002 library manager.
25003
25004 @item @command{ACS CHECK}
25005 @tab @command{GNAT MAKE /DEPENDENCY_LIST}@*
25006 Forms the execution closure of one
25007 or more compiled units and checks completeness and currency.
25008
25009 @item @command{ACS COMPILE}
25010 @tab @command{GNAT MAKE /ACTIONS=COMPILE}@*
25011 Forms the execution closure of one or
25012 more specified units, checks completeness and currency,
25013 identifies units that have revised source files, compiles same,
25014 and recompiles units that are or will become obsolete.
25015 Also completes incomplete generic instantiations.
25016
25017 @item @command{ACS COPY FOREIGN}
25018 @tab Copy (*)@*
25019 Copies a foreign object file into the program library as a
25020 library unit body.
25021
25022 @item @command{ACS COPY UNIT}
25023 @tab Copy (*)@*
25024 Copies a compiled unit from one program library to another.
25025
25026 @item @command{ACS CREATE LIBRARY}
25027 @tab Create /directory (*)@*
25028 Creates a program library.
25029
25030 @item @command{ACS CREATE SUBLIBRARY}
25031 @tab Create /directory (*)@*
25032 Creates a program sublibrary.
25033
25034 @item @command{ACS DELETE LIBRARY}
25035 @tab @*
25036 Deletes a program library and its contents.
25037
25038 @item @command{ACS DELETE SUBLIBRARY}
25039 @tab @*
25040 Deletes a program sublibrary and its contents.
25041
25042 @item @command{ACS DELETE UNIT}
25043 @tab Delete file (*)@*
25044 On OpenVMS systems, deletes one or more compiled units from
25045 the current program library.
25046
25047 @item @command{ACS DIRECTORY}
25048 @tab Directory (*)@*
25049 On OpenVMS systems, lists units contained in the current
25050 program library.
25051
25052 @item @command{ACS ENTER FOREIGN}
25053 @tab Copy (*)@*
25054 Allows the import of a foreign body as an Ada library
25055 spec and enters a reference to a pointer.
25056
25057 @item @command{ACS ENTER UNIT}
25058 @tab Copy (*)@*
25059 Enters a reference (pointer) from the current program library to
25060 a unit compiled into another program library.
25061
25062 @item @command{ACS EXIT}
25063 @tab [No equivalent]@*
25064 Exits from the program library manager.
25065
25066 @item @command{ACS EXPORT}
25067 @tab Copy (*)@*
25068 Creates an object file that contains system-specific object code
25069 for one or more units. With GNAT, object files can simply be copied
25070 into the desired directory.
25071
25072 @item @command{ACS EXTRACT SOURCE}
25073 @tab Copy (*)@*
25074 Allows access to the copied source file for each Ada compilation unit
25075
25076 @item @command{ACS HELP}
25077 @tab @command{HELP GNAT}@*
25078 Provides online help.
25079
25080 @item @command{ACS LINK}
25081 @tab @command{GNAT LINK}@*
25082 Links an object file containing Ada units into an executable file.
25083
25084 @item @command{ACS LOAD}
25085 @tab Copy (*)@*
25086 Loads (partially compiles) Ada units into the program library.
25087 Allows loading a program from a collection of files into a library
25088 without knowing the relationship among units.
25089
25090 @item @command{ACS MERGE}
25091 @tab Copy (*)@*
25092 Merges into the current program library, one or more units from
25093 another library where they were modified.
25094
25095 @item @command{ACS RECOMPILE}
25096 @tab @command{GNAT MAKE /ACTIONS=COMPILE}@*
25097 Recompiles from external or copied source files any obsolete
25098 unit in the closure. Also, completes any incomplete generic
25099 instantiations.
25100
25101 @item @command{ACS REENTER}
25102 @tab @command{GNAT MAKE}@*
25103 Reenters current references to units compiled after last entered
25104 with the @command{ACS ENTER UNIT} command.
25105
25106 @item @command{ACS SET LIBRARY}
25107 @tab Set default (*)@*
25108 Defines a program library to be the compilation context as well
25109 as the target library for compiler output and commands in general.
25110
25111 @item @command{ACS SET PRAGMA}
25112 @tab Edit @file{gnat.adc} (*)@*
25113 Redefines specified values of the library characteristics
25114 @code{LONG_ FLOAT}, @code{MEMORY_SIZE}, @code{SYSTEM_NAME},
25115 and @code{Float_Representation}.
25116
25117 @item @command{ACS SET SOURCE}
25118 @tab Define @code{ADA_INCLUDE_PATH} path (*)@*
25119 Defines the source file search list for the @command{ACS COMPILE} command.
25120
25121 @item @command{ACS SHOW LIBRARY}
25122 @tab Directory (*)@*
25123 Lists information about one or more program libraries.
25124
25125 @item @command{ACS SHOW PROGRAM}
25126 @tab [No equivalent]@*
25127 Lists information about the execution closure of one or
25128 more units in the program library.
25129
25130 @item @command{ACS SHOW SOURCE}
25131 @tab Show logical @code{ADA_INCLUDE_PATH}@*
25132 Shows the source file search used when compiling units.
25133
25134 @item @command{ACS SHOW VERSION}
25135 @tab Compile with @option{VERBOSE} option
25136 Displays the version number of the compiler and program library
25137 manager used.
25138
25139 @item @command{ACS SPAWN}
25140 @tab [No equivalent]@*
25141 Creates a subprocess of the current process (same as @command{DCL SPAWN}
25142 command).
25143
25144 @item @command{ACS VERIFY}
25145 @tab [No equivalent]@*
25146 Performs a series of consistency checks on a program library to
25147 determine whether the library structure and library files are in
25148 valid form.
25149 @end multitable
25150
25151 @noindent
25152
25153 @node Input-Output
25154 @section Input-Output
25155
25156 @noindent
25157 On OpenVMS Alpha systems, HP Ada uses OpenVMS Record
25158 Management Services (RMS) to perform operations on
25159 external files.
25160
25161 @noindent
25162 HP Ada and GNAT predefine an identical set of input-
25163 output packages. To make the use of the
25164 generic @code{TEXT_IO} operations more convenient, HP Ada
25165 provides predefined library packages that instantiate the
25166 integer and floating-point operations for the predefined
25167 integer and floating-point types as shown in the following table.
25168
25169 @multitable @columnfractions .45 .55
25170 @item @emph{Package Name} @tab Instantiation
25171
25172 @item @code{INTEGER_TEXT_IO}
25173 @tab @code{INTEGER_IO(INTEGER)}
25174
25175 @item @code{SHORT_INTEGER_TEXT_IO}
25176 @tab @code{INTEGER_IO(SHORT_INTEGER)}
25177
25178 @item @code{SHORT_SHORT_INTEGER_TEXT_IO}
25179 @tab @code{INTEGER_IO(SHORT_SHORT_INTEGER)}
25180
25181 @item @code{FLOAT_TEXT_IO}
25182 @tab @code{FLOAT_IO(FLOAT)}
25183
25184 @item @code{LONG_FLOAT_TEXT_IO}
25185 @tab @code{FLOAT_IO(LONG_FLOAT)}
25186 @end multitable
25187
25188 @noindent
25189 The HP Ada predefined packages and their operations
25190 are implemented using OpenVMS Alpha files and input-output
25191 facilities. HP Ada supports asynchronous input-output on OpenVMS Alpha.
25192 Familiarity with the following is recommended:
25193 @itemize @bullet
25194 @item RMS file organizations and access methods
25195
25196 @item OpenVMS file specifications and directories
25197
25198 @item OpenVMS File Definition Language (FDL)
25199 @end itemize
25200
25201 @noindent
25202 GNAT provides I/O facilities that are completely
25203 compatible with HP Ada. The distribution includes the
25204 standard HP Ada versions of all I/O packages, operating
25205 in a manner compatible with HP Ada. In particular, the
25206 following packages are by default the HP Ada (Ada 83)
25207 versions of these packages rather than the renamings
25208 suggested in Annex J of the Ada Reference Manual:
25209 @itemize @bullet
25210 @item @code{TEXT_IO}
25211
25212 @item @code{SEQUENTIAL_IO}
25213
25214 @item @code{DIRECT_IO}
25215 @end itemize
25216
25217 @noindent
25218 The use of the standard child package syntax (for
25219 example, @code{ADA.TEXT_IO}) retrieves the post-Ada 83 versions of these
25220 packages.
25221 GNAT provides HP-compatible predefined instantiations
25222 of the @code{TEXT_IO} packages, and also
25223 provides the standard predefined instantiations required
25224 by the @cite{Ada Reference Manual}.
25225
25226 For further information on how GNAT interfaces to the file
25227 system or how I/O is implemented in programs written in
25228 mixed languages, see @ref{Implementation of the Standard I/O,,,
25229 gnat_rm, GNAT Reference Manual}.
25230 This chapter covers the following:
25231 @itemize @bullet
25232 @item Standard I/O packages
25233
25234 @item @code{FORM} strings
25235
25236 @item @code{ADA.DIRECT_IO}
25237
25238 @item @code{ADA.SEQUENTIAL_IO}
25239
25240 @item @code{ADA.TEXT_IO}
25241
25242 @item Stream pointer positioning
25243
25244 @item Reading and writing non-regular files
25245
25246 @item @code{GET_IMMEDIATE}
25247
25248 @item Treating @code{TEXT_IO} files as streams
25249
25250 @item Shared files
25251
25252 @item Open modes
25253 @end itemize
25254
25255 @node Implementation Limits
25256 @section Implementation Limits
25257
25258 @noindent
25259 The following table lists implementation limits for HP Ada
25260 and GNAT systems.
25261 @multitable @columnfractions .60 .20 .20
25262 @sp 1
25263 @item @emph{Compilation Parameter}
25264 @tab @emph{HP Ada}
25265 @tab @emph{GNAT}
25266 @sp 1
25267
25268 @item In a subprogram or entry declaration, maximum number of
25269 formal parameters that are of an unconstrained record type
25270 @tab 32
25271 @tab No set limit
25272 @sp 1
25273
25274 @item Maximum identifier length (number of characters)
25275 @tab 255
25276 @tab 32766
25277 @sp 1
25278
25279 @item Maximum number of characters in a source line
25280 @tab 255
25281 @tab 32766
25282 @sp 1
25283
25284 @item Maximum collection size (number of bytes)
25285 @tab 2**31-1
25286 @tab 2**31-1
25287 @sp 1
25288
25289 @item Maximum number of discriminants for a record type
25290 @tab 245
25291 @tab No set limit
25292 @sp 1
25293
25294 @item Maximum number of formal parameters in an entry or
25295 subprogram declaration
25296 @tab 246
25297 @tab No set limit
25298 @sp 1
25299
25300 @item Maximum number of dimensions in an array type
25301 @tab 255
25302 @tab No set limit
25303 @sp 1
25304
25305 @item Maximum number of library units and subunits in a compilation.
25306 @tab 4095
25307 @tab No set limit
25308 @sp 1
25309
25310 @item Maximum number of library units and subunits in an execution.
25311 @tab 16383
25312 @tab No set limit
25313 @sp 1
25314
25315 @item Maximum number of objects declared with the pragma @code{COMMON_OBJECT}
25316 or @code{PSECT_OBJECT}
25317 @tab 32757
25318 @tab No set limit
25319 @sp 1
25320
25321 @item Maximum number of enumeration literals in an enumeration type
25322 definition
25323 @tab 65535
25324 @tab No set limit
25325 @sp 1
25326
25327 @item Maximum number of lines in a source file
25328 @tab 65534
25329 @tab No set limit
25330 @sp 1
25331
25332 @item Maximum number of bits in any object
25333 @tab 2**31-1
25334 @tab 2**31-1
25335 @sp 1
25336
25337 @item Maximum size of the static portion of a stack frame (approximate)
25338 @tab 2**31-1
25339 @tab 2**31-1
25340 @end multitable
25341
25342 @node Tools and Utilities
25343 @section Tools and Utilities
25344
25345 @noindent
25346 The following table lists some of the OpenVMS development tools
25347 available for HP Ada, and the corresponding tools for
25348 use with @value{EDITION} on Alpha and I64 platforms.
25349 Aside from the debugger, all the OpenVMS tools identified are part
25350 of the DECset package.
25351
25352 @iftex
25353 @c Specify table in TeX since Texinfo does a poor job
25354 @tex
25355 \smallskip
25356 \smallskip
25357 \settabs\+Language-Sensitive Editor\quad
25358 &Product with HP Ada\quad
25359 &\cr
25360 \+\it Tool
25361 &\it Product with HP Ada
25362 & \it Product with GNAT Pro\cr
25363 \smallskip
25364 \+Code Management System
25365 &HP CMS
25366 & HP CMS\cr
25367 \smallskip
25368 \+Language-Sensitive Editor
25369 &HP LSE
25370 & emacs or HP LSE (Alpha)\cr
25371 \+
25372 &
25373 & HP LSE (I64)\cr
25374 \smallskip
25375 \+Debugger
25376 &OpenVMS Debug
25377 & gdb (Alpha),\cr
25378 \+
25379 &
25380 & OpenVMS Debug (I64)\cr
25381 \smallskip
25382 \+Source Code Analyzer /
25383 &HP SCA
25384 & GNAT XREF\cr
25385 \+Cross Referencer
25386 &
25387 &\cr
25388 \smallskip
25389 \+Test Manager
25390 &HP Digital Test
25391 & HP DTM\cr
25392 \+
25393 &Manager (DTM)
25394 &\cr
25395 \smallskip
25396 \+Performance and
25397 & HP PCA
25398 & HP PCA\cr
25399 \+Coverage Analyzer
25400 &
25401 &\cr
25402 \smallskip
25403 \+Module Management
25404 & HP MMS
25405 & Not applicable\cr
25406 \+ System
25407 &
25408 &\cr
25409 \smallskip
25410 \smallskip
25411 @end tex
25412 @end iftex
25413
25414 @ifnottex
25415 @c This is the Texinfo version of the table. It renders poorly in pdf, hence
25416 @c the TeX version above for the printed version
25417 @flushleft
25418 @c @multitable @columnfractions .3 .4 .4
25419 @multitable {Source Code Analyzer /}{Tool with HP Ada}{Tool with GNAT Pro}
25420 @item @i{Tool}
25421 @tab @i{Tool with HP Ada}
25422 @tab @i{Tool with @value{EDITION}}
25423 @item Code Management@*System
25424 @tab HP CMS
25425 @tab HP CMS
25426 @item Language-Sensitive@*Editor
25427 @tab HP LSE
25428 @tab emacs or HP LSE (Alpha)
25429 @item
25430 @tab
25431 @tab HP LSE (I64)
25432 @item Debugger
25433 @tab OpenVMS Debug
25434 @tab gdb (Alpha),
25435 @item
25436 @tab
25437 @tab OpenVMS Debug (I64)
25438 @item Source Code Analyzer /@*Cross Referencer
25439 @tab HP SCA
25440 @tab GNAT XREF
25441 @item Test Manager
25442 @tab HP Digital Test@*Manager (DTM)
25443 @tab HP DTM
25444 @item Performance and@*Coverage Analyzer
25445 @tab HP PCA
25446 @tab HP PCA
25447 @item Module Management@*System
25448 @tab HP MMS
25449 @tab Not applicable
25450 @end multitable
25451 @end flushleft
25452 @end ifnottex
25453
25454 @end ifset
25455
25456 @c **************************************
25457 @node Platform-Specific Information for the Run-Time Libraries
25458 @appendix Platform-Specific Information for the Run-Time Libraries
25459 @cindex Tasking and threads libraries
25460 @cindex Threads libraries and tasking
25461 @cindex Run-time libraries (platform-specific information)
25462
25463 @noindent
25464 The GNAT run-time implementation may vary with respect to both the
25465 underlying threads library and the exception handling scheme.
25466 For threads support, one or more of the following are supplied:
25467 @itemize @bullet
25468 @item @b{native threads library}, a binding to the thread package from
25469 the underlying operating system
25470
25471 @item @b{pthreads library} (Sparc Solaris only), a binding to the Solaris
25472 POSIX thread package
25473 @end itemize
25474
25475 @noindent
25476 For exception handling, either or both of two models are supplied:
25477 @itemize @bullet
25478 @item @b{Zero-Cost Exceptions} (``ZCX''),@footnote{
25479 Most programs should experience a substantial speed improvement by
25480 being compiled with a ZCX run-time.
25481 This is especially true for
25482 tasking applications or applications with many exception handlers.}
25483 @cindex Zero-Cost Exceptions
25484 @cindex ZCX (Zero-Cost Exceptions)
25485 which uses binder-generated tables that
25486 are interrogated at run time to locate a handler
25487
25488 @item @b{setjmp / longjmp} (``SJLJ''),
25489 @cindex setjmp/longjmp Exception Model
25490 @cindex SJLJ (setjmp/longjmp Exception Model)
25491 which uses dynamically-set data to establish
25492 the set of handlers
25493 @end itemize
25494
25495 @noindent
25496 This appendix summarizes which combinations of threads and exception support
25497 are supplied on various GNAT platforms.
25498 It then shows how to select a particular library either
25499 permanently or temporarily,
25500 explains the properties of (and tradeoffs among) the various threads
25501 libraries, and provides some additional
25502 information about several specific platforms.
25503
25504 @menu
25505 * Summary of Run-Time Configurations::
25506 * Specifying a Run-Time Library::
25507 * Choosing the Scheduling Policy::
25508 * Solaris-Specific Considerations::
25509 * Linux-Specific Considerations::
25510 * AIX-Specific Considerations::
25511 * Irix-Specific Considerations::
25512 @end menu
25513
25514 @node Summary of Run-Time Configurations
25515 @section Summary of Run-Time Configurations
25516
25517 @multitable @columnfractions .30 .70
25518 @item @b{alpha-openvms}
25519 @item @code{@ @ }@i{rts-native (default)}
25520 @item @code{@ @ @ @ }Tasking @tab native VMS threads
25521 @item @code{@ @ @ @ }Exceptions @tab ZCX
25522 @*
25523 @item @b{alpha-tru64}
25524 @item @code{@ @ }@i{rts-native (default)}
25525 @item @code{@ @ @ @ }Tasking @tab native TRU64 threads
25526 @item @code{@ @ @ @ }Exceptions @tab ZCX
25527 @*
25528 @item @code{@ @ }@i{rts-sjlj}
25529 @item @code{@ @ @ @ }Tasking @tab native TRU64 threads
25530 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25531 @*
25532 @item @b{ia64-hp_linux}
25533 @item @code{@ @ }@i{rts-native (default)}
25534 @item @code{@ @ @ @ }Tasking @tab pthread library
25535 @item @code{@ @ @ @ }Exceptions @tab ZCX
25536 @*
25537 @item @b{ia64-hpux}
25538 @item @code{@ @ }@i{rts-native (default)}
25539 @item @code{@ @ @ @ }Tasking @tab native HP-UX threads
25540 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25541 @*
25542 @item @b{ia64-openvms}
25543 @item @code{@ @ }@i{rts-native (default)}
25544 @item @code{@ @ @ @ }Tasking @tab native VMS threads
25545 @item @code{@ @ @ @ }Exceptions @tab ZCX
25546 @*
25547 @item @b{ia64-sgi_linux}
25548 @item @code{@ @ }@i{rts-native (default)}
25549 @item @code{@ @ @ @ }Tasking @tab pthread library
25550 @item @code{@ @ @ @ }Exceptions @tab ZCX
25551 @*
25552 @item @b{mips-irix}
25553 @item @code{@ @ }@i{rts-native (default)}
25554 @item @code{@ @ @ @ }Tasking @tab native IRIX threads
25555 @item @code{@ @ @ @ }Exceptions @tab ZCX
25556 @*
25557 @item @b{pa-hpux}
25558 @item @code{@ @ }@i{rts-native (default)}
25559 @item @code{@ @ @ @ }Tasking @tab native HP-UX threads
25560 @item @code{@ @ @ @ }Exceptions @tab ZCX
25561 @*
25562 @item @code{@ @ }@i{rts-sjlj}
25563 @item @code{@ @ @ @ }Tasking @tab native HP-UX threads
25564 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25565 @*
25566 @item @b{ppc-aix}
25567 @item @code{@ @ }@i{rts-native (default)}
25568 @item @code{@ @ @ @ }Tasking @tab native AIX threads
25569 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25570 @*
25571 @item @b{ppc-darwin}
25572 @item @code{@ @ }@i{rts-native (default)}
25573 @item @code{@ @ @ @ }Tasking @tab native MacOS threads
25574 @item @code{@ @ @ @ }Exceptions @tab ZCX
25575 @*
25576 @item @b{sparc-solaris} @tab
25577 @item @code{@ @ }@i{rts-native (default)}
25578 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
25579 @item @code{@ @ @ @ }Exceptions @tab ZCX
25580 @*
25581 @item @code{@ @ }@i{rts-pthread}
25582 @item @code{@ @ @ @ }Tasking @tab pthread library
25583 @item @code{@ @ @ @ }Exceptions @tab ZCX
25584 @*
25585 @item @code{@ @ }@i{rts-sjlj}
25586 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
25587 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25588 @*
25589 @item @b{sparc64-solaris} @tab
25590 @item @code{@ @ }@i{rts-native (default)}
25591 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
25592 @item @code{@ @ @ @ }Exceptions @tab ZCX
25593 @*
25594 @item @b{x86-linux}
25595 @item @code{@ @ }@i{rts-native (default)}
25596 @item @code{@ @ @ @ }Tasking @tab pthread library
25597 @item @code{@ @ @ @ }Exceptions @tab ZCX
25598 @*
25599 @item @code{@ @ }@i{rts-sjlj}
25600 @item @code{@ @ @ @ }Tasking @tab pthread library
25601 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25602 @*
25603 @item @b{x86-lynx}
25604 @item @code{@ @ }@i{rts-native (default)}
25605 @item @code{@ @ @ @ }Tasking @tab native LynxOS threads
25606 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25607 @*
25608 @item @b{x86-solaris}
25609 @item @code{@ @ }@i{rts-native (default)}
25610 @item @code{@ @ @ @ }Tasking @tab native Solaris threads
25611 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25612 @*
25613 @item @b{x86-windows}
25614 @item @code{@ @ }@i{rts-native (default)}
25615 @item @code{@ @ @ @ }Tasking @tab native Win32 threads
25616 @item @code{@ @ @ @ }Exceptions @tab ZCX
25617 @*
25618 @item @code{@ @ }@i{rts-sjlj (default)}
25619 @item @code{@ @ @ @ }Tasking @tab native Win32 threads
25620 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25621 @*
25622 @item @b{x86_64-linux}
25623 @item @code{@ @ }@i{rts-native (default)}
25624 @item @code{@ @ @ @ }Tasking @tab pthread library
25625 @item @code{@ @ @ @ }Exceptions @tab ZCX
25626 @*
25627 @item @code{@ @ }@i{rts-sjlj}
25628 @item @code{@ @ @ @ }Tasking @tab pthread library
25629 @item @code{@ @ @ @ }Exceptions @tab SJLJ
25630 @*
25631 @end multitable
25632
25633 @node Specifying a Run-Time Library
25634 @section Specifying a Run-Time Library
25635
25636 @noindent
25637 The @file{adainclude} subdirectory containing the sources of the GNAT
25638 run-time library, and the @file{adalib} subdirectory containing the
25639 @file{ALI} files and the static and/or shared GNAT library, are located
25640 in the gcc target-dependent area:
25641
25642 @smallexample
25643 target=$prefix/lib/gcc/gcc-@i{dumpmachine}/gcc-@i{dumpversion}/
25644 @end smallexample
25645
25646 @noindent
25647 As indicated above, on some platforms several run-time libraries are supplied.
25648 These libraries are installed in the target dependent area and
25649 contain a complete source and binary subdirectory. The detailed description
25650 below explains the differences between the different libraries in terms of
25651 their thread support.
25652
25653 The default run-time library (when GNAT is installed) is @emph{rts-native}.
25654 This default run time is selected by the means of soft links.
25655 For example on x86-linux:
25656
25657 @smallexample
25658 @group
25659 $(target-dir)
25660 |
25661 +--- adainclude----------+
25662 | |
25663 +--- adalib-----------+ |
25664 | | |
25665 +--- rts-native | |
25666 | | | |
25667 | +--- adainclude <---+
25668 | | |
25669 | +--- adalib <----+
25670 |
25671 +--- rts-sjlj
25672 |
25673 +--- adainclude
25674 |
25675 +--- adalib
25676 @end group
25677 @end smallexample
25678
25679 @noindent
25680 If the @i{rts-sjlj} library is to be selected on a permanent basis,
25681 these soft links can be modified with the following commands:
25682
25683 @smallexample
25684 $ cd $target
25685 $ rm -f adainclude adalib
25686 $ ln -s rts-sjlj/adainclude adainclude
25687 $ ln -s rts-sjlj/adalib adalib
25688 @end smallexample
25689
25690 @noindent
25691 Alternatively, you can specify @file{rts-sjlj/adainclude} in the file
25692 @file{$target/ada_source_path} and @file{rts-sjlj/adalib} in
25693 @file{$target/ada_object_path}.
25694
25695 Selecting another run-time library temporarily can be
25696 achieved by using the @option{--RTS} switch, e.g., @option{--RTS=sjlj}
25697 @cindex @option{--RTS} option
25698
25699 @node Choosing the Scheduling Policy
25700 @section Choosing the Scheduling Policy
25701
25702 @noindent
25703 When using a POSIX threads implementation, you have a choice of several
25704 scheduling policies: @code{SCHED_FIFO},
25705 @cindex @code{SCHED_FIFO} scheduling policy
25706 @code{SCHED_RR}
25707 @cindex @code{SCHED_RR} scheduling policy
25708 and @code{SCHED_OTHER}.
25709 @cindex @code{SCHED_OTHER} scheduling policy
25710 Typically, the default is @code{SCHED_OTHER}, while using @code{SCHED_FIFO}
25711 or @code{SCHED_RR} requires special (e.g., root) privileges.
25712
25713 By default, GNAT uses the @code{SCHED_OTHER} policy. To specify
25714 @code{SCHED_FIFO},
25715 @cindex @code{SCHED_FIFO} scheduling policy
25716 you can use one of the following:
25717
25718 @itemize @bullet
25719 @item
25720 @code{pragma Time_Slice (0.0)}
25721 @cindex pragma Time_Slice
25722 @item
25723 the corresponding binder option @option{-T0}
25724 @cindex @option{-T0} option
25725 @item
25726 @code{pragma Task_Dispatching_Policy (FIFO_Within_Priorities)}
25727 @cindex pragma Task_Dispatching_Policy
25728 @end itemize
25729
25730 @noindent
25731 To specify @code{SCHED_RR},
25732 @cindex @code{SCHED_RR} scheduling policy
25733 you should use @code{pragma Time_Slice} with a
25734 value greater than @code{0.0}, or else use the corresponding @option{-T}
25735 binder option.
25736
25737 @node Solaris-Specific Considerations
25738 @section Solaris-Specific Considerations
25739 @cindex Solaris Sparc threads libraries
25740
25741 @noindent
25742 This section addresses some topics related to the various threads libraries
25743 on Sparc Solaris.
25744
25745 @menu
25746 * Solaris Threads Issues::
25747 @end menu
25748
25749 @node Solaris Threads Issues
25750 @subsection Solaris Threads Issues
25751
25752 @noindent
25753 GNAT under Solaris/Sparc 32 bits comes with an alternate tasking run-time
25754 library based on POSIX threads --- @emph{rts-pthread}.
25755 @cindex rts-pthread threads library
25756 This run-time library has the advantage of being mostly shared across all
25757 POSIX-compliant thread implementations, and it also provides under
25758 @w{Solaris 8} the @code{PTHREAD_PRIO_INHERIT}
25759 @cindex @code{PTHREAD_PRIO_INHERIT} policy (under rts-pthread)
25760 and @code{PTHREAD_PRIO_PROTECT}
25761 @cindex @code{PTHREAD_PRIO_PROTECT} policy (under rts-pthread)
25762 semantics that can be selected using the predefined pragma
25763 @code{Locking_Policy}
25764 @cindex pragma Locking_Policy (under rts-pthread)
25765 with respectively
25766 @code{Inheritance_Locking} and @code{Ceiling_Locking} as the policy.
25767 @cindex @code{Inheritance_Locking} (under rts-pthread)
25768 @cindex @code{Ceiling_Locking} (under rts-pthread)
25769
25770 As explained above, the native run-time library is based on the Solaris thread
25771 library (@code{libthread}) and is the default library.
25772
25773 When the Solaris threads library is used (this is the default), programs
25774 compiled with GNAT can automatically take advantage of
25775 and can thus execute on multiple processors.
25776 The user can alternatively specify a processor on which the program should run
25777 to emulate a single-processor system. The multiprocessor / uniprocessor choice
25778 is made by
25779 setting the environment variable @env{GNAT_PROCESSOR}
25780 @cindex @env{GNAT_PROCESSOR} environment variable (on Sparc Solaris)
25781 to one of the following:
25782
25783 @table @code
25784 @item -2
25785 Use the default configuration (run the program on all
25786 available processors) - this is the same as having @code{GNAT_PROCESSOR}
25787 unset
25788
25789 @item -1
25790 Let the run-time implementation choose one processor and run the program on
25791 that processor
25792
25793 @item 0 .. Last_Proc
25794 Run the program on the specified processor.
25795 @code{Last_Proc} is equal to @code{_SC_NPROCESSORS_CONF - 1}
25796 (where @code{_SC_NPROCESSORS_CONF} is a system variable).
25797 @end table
25798
25799 @node Linux-Specific Considerations
25800 @section Linux-Specific Considerations
25801 @cindex Linux threads libraries
25802
25803 @noindent
25804 On GNU/Linux without NPTL support (usually system with GNU C Library
25805 older than 2.3), the signal model is not POSIX compliant, which means
25806 that to send a signal to the process, you need to send the signal to all
25807 threads, e.g.@: by using @code{killpg()}.
25808
25809 @node AIX-Specific Considerations
25810 @section AIX-Specific Considerations
25811 @cindex AIX resolver library
25812
25813 @noindent
25814 On AIX, the resolver library initializes some internal structure on
25815 the first call to @code{get*by*} functions, which are used to implement
25816 @code{GNAT.Sockets.Get_Host_By_Name} and
25817 @code{GNAT.Sockets.Get_Host_By_Address}.
25818 If such initialization occurs within an Ada task, and the stack size for
25819 the task is the default size, a stack overflow may occur.
25820
25821 To avoid this overflow, the user should either ensure that the first call
25822 to @code{GNAT.Sockets.Get_Host_By_Name} or
25823 @code{GNAT.Sockets.Get_Host_By_Addrss}
25824 occurs in the environment task, or use @code{pragma Storage_Size} to
25825 specify a sufficiently large size for the stack of the task that contains
25826 this call.
25827
25828 @node Irix-Specific Considerations
25829 @section Irix-Specific Considerations
25830 @cindex Irix libraries
25831
25832 @noindent
25833 The GCC support libraries coming with the Irix compiler have moved to
25834 their canonical place with respect to the general Irix ABI related
25835 conventions. Running applications built with the default shared GNAT
25836 run-time now requires the LD_LIBRARY_PATH environment variable to
25837 include this location. A possible way to achieve this is to issue the
25838 following command line on a bash prompt:
25839
25840 @smallexample
25841 @group
25842 $ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`dirname \`gcc --print-file-name=libgcc_s.so\``
25843 @end group
25844 @end smallexample
25845
25846 @c *******************************
25847 @node Example of Binder Output File
25848 @appendix Example of Binder Output File
25849
25850 @noindent
25851 This Appendix displays the source code for @command{gnatbind}'s output
25852 file generated for a simple ``Hello World'' program.
25853 Comments have been added for clarification purposes.
25854
25855 @smallexample @c adanocomment
25856 @iftex
25857 @leftskip=0cm
25858 @end iftex
25859 -- The package is called Ada_Main unless this name is actually used
25860 -- as a unit name in the partition, in which case some other unique
25861 -- name is used.
25862
25863 with System;
25864 package ada_main is
25865
25866 Elab_Final_Code : Integer;
25867 pragma Import (C, Elab_Final_Code, "__gnat_inside_elab_final_code");
25868
25869 -- The main program saves the parameters (argument count,
25870 -- argument values, environment pointer) in global variables
25871 -- for later access by other units including
25872 -- Ada.Command_Line.
25873
25874 gnat_argc : Integer;
25875 gnat_argv : System.Address;
25876 gnat_envp : System.Address;
25877
25878 -- The actual variables are stored in a library routine. This
25879 -- is useful for some shared library situations, where there
25880 -- are problems if variables are not in the library.
25881
25882 pragma Import (C, gnat_argc);
25883 pragma Import (C, gnat_argv);
25884 pragma Import (C, gnat_envp);
25885
25886 -- The exit status is similarly an external location
25887
25888 gnat_exit_status : Integer;
25889 pragma Import (C, gnat_exit_status);
25890
25891 GNAT_Version : constant String :=
25892 "GNAT Version: 6.0.0w (20061115)";
25893 pragma Export (C, GNAT_Version, "__gnat_version");
25894
25895 -- This is the generated adafinal routine that performs
25896 -- finalization at the end of execution. In the case where
25897 -- Ada is the main program, this main program makes a call
25898 -- to adafinal at program termination.
25899
25900 procedure adafinal;
25901 pragma Export (C, adafinal, "adafinal");
25902
25903 -- This is the generated adainit routine that performs
25904 -- initialization at the start of execution. In the case
25905 -- where Ada is the main program, this main program makes
25906 -- a call to adainit at program startup.
25907
25908 procedure adainit;
25909 pragma Export (C, adainit, "adainit");
25910
25911 -- This routine is called at the start of execution. It is
25912 -- a dummy routine that is used by the debugger to breakpoint
25913 -- at the start of execution.
25914
25915 procedure Break_Start;
25916 pragma Import (C, Break_Start, "__gnat_break_start");
25917
25918 -- This is the actual generated main program (it would be
25919 -- suppressed if the no main program switch were used). As
25920 -- required by standard system conventions, this program has
25921 -- the external name main.
25922
25923 function main
25924 (argc : Integer;
25925 argv : System.Address;
25926 envp : System.Address)
25927 return Integer;
25928 pragma Export (C, main, "main");
25929
25930 -- The following set of constants give the version
25931 -- identification values for every unit in the bound
25932 -- partition. This identification is computed from all
25933 -- dependent semantic units, and corresponds to the
25934 -- string that would be returned by use of the
25935 -- Body_Version or Version attributes.
25936
25937 type Version_32 is mod 2 ** 32;
25938 u00001 : constant Version_32 := 16#7880BEB3#;
25939 u00002 : constant Version_32 := 16#0D24CBD0#;
25940 u00003 : constant Version_32 := 16#3283DBEB#;
25941 u00004 : constant Version_32 := 16#2359F9ED#;
25942 u00005 : constant Version_32 := 16#664FB847#;
25943 u00006 : constant Version_32 := 16#68E803DF#;
25944 u00007 : constant Version_32 := 16#5572E604#;
25945 u00008 : constant Version_32 := 16#46B173D8#;
25946 u00009 : constant Version_32 := 16#156A40CF#;
25947 u00010 : constant Version_32 := 16#033DABE0#;
25948 u00011 : constant Version_32 := 16#6AB38FEA#;
25949 u00012 : constant Version_32 := 16#22B6217D#;
25950 u00013 : constant Version_32 := 16#68A22947#;
25951 u00014 : constant Version_32 := 16#18CC4A56#;
25952 u00015 : constant Version_32 := 16#08258E1B#;
25953 u00016 : constant Version_32 := 16#367D5222#;
25954 u00017 : constant Version_32 := 16#20C9ECA4#;
25955 u00018 : constant Version_32 := 16#50D32CB6#;
25956 u00019 : constant Version_32 := 16#39A8BB77#;
25957 u00020 : constant Version_32 := 16#5CF8FA2B#;
25958 u00021 : constant Version_32 := 16#2F1EB794#;
25959 u00022 : constant Version_32 := 16#31AB6444#;
25960 u00023 : constant Version_32 := 16#1574B6E9#;
25961 u00024 : constant Version_32 := 16#5109C189#;
25962 u00025 : constant Version_32 := 16#56D770CD#;
25963 u00026 : constant Version_32 := 16#02F9DE3D#;
25964 u00027 : constant Version_32 := 16#08AB6B2C#;
25965 u00028 : constant Version_32 := 16#3FA37670#;
25966 u00029 : constant Version_32 := 16#476457A0#;
25967 u00030 : constant Version_32 := 16#731E1B6E#;
25968 u00031 : constant Version_32 := 16#23C2E789#;
25969 u00032 : constant Version_32 := 16#0F1BD6A1#;
25970 u00033 : constant Version_32 := 16#7C25DE96#;
25971 u00034 : constant Version_32 := 16#39ADFFA2#;
25972 u00035 : constant Version_32 := 16#571DE3E7#;
25973 u00036 : constant Version_32 := 16#5EB646AB#;
25974 u00037 : constant Version_32 := 16#4249379B#;
25975 u00038 : constant Version_32 := 16#0357E00A#;
25976 u00039 : constant Version_32 := 16#3784FB72#;
25977 u00040 : constant Version_32 := 16#2E723019#;
25978 u00041 : constant Version_32 := 16#623358EA#;
25979 u00042 : constant Version_32 := 16#107F9465#;
25980 u00043 : constant Version_32 := 16#6843F68A#;
25981 u00044 : constant Version_32 := 16#63305874#;
25982 u00045 : constant Version_32 := 16#31E56CE1#;
25983 u00046 : constant Version_32 := 16#02917970#;
25984 u00047 : constant Version_32 := 16#6CCBA70E#;
25985 u00048 : constant Version_32 := 16#41CD4204#;
25986 u00049 : constant Version_32 := 16#572E3F58#;
25987 u00050 : constant Version_32 := 16#20729FF5#;
25988 u00051 : constant Version_32 := 16#1D4F93E8#;
25989 u00052 : constant Version_32 := 16#30B2EC3D#;
25990 u00053 : constant Version_32 := 16#34054F96#;
25991 u00054 : constant Version_32 := 16#5A199860#;
25992 u00055 : constant Version_32 := 16#0E7F912B#;
25993 u00056 : constant Version_32 := 16#5760634A#;
25994 u00057 : constant Version_32 := 16#5D851835#;
25995
25996 -- The following Export pragmas export the version numbers
25997 -- with symbolic names ending in B (for body) or S
25998 -- (for spec) so that they can be located in a link. The
25999 -- information provided here is sufficient to track down
26000 -- the exact versions of units used in a given build.
26001
26002 pragma Export (C, u00001, "helloB");
26003 pragma Export (C, u00002, "system__standard_libraryB");
26004 pragma Export (C, u00003, "system__standard_libraryS");
26005 pragma Export (C, u00004, "adaS");
26006 pragma Export (C, u00005, "ada__text_ioB");
26007 pragma Export (C, u00006, "ada__text_ioS");
26008 pragma Export (C, u00007, "ada__exceptionsB");
26009 pragma Export (C, u00008, "ada__exceptionsS");
26010 pragma Export (C, u00009, "gnatS");
26011 pragma Export (C, u00010, "gnat__heap_sort_aB");
26012 pragma Export (C, u00011, "gnat__heap_sort_aS");
26013 pragma Export (C, u00012, "systemS");
26014 pragma Export (C, u00013, "system__exception_tableB");
26015 pragma Export (C, u00014, "system__exception_tableS");
26016 pragma Export (C, u00015, "gnat__htableB");
26017 pragma Export (C, u00016, "gnat__htableS");
26018 pragma Export (C, u00017, "system__exceptionsS");
26019 pragma Export (C, u00018, "system__machine_state_operationsB");
26020 pragma Export (C, u00019, "system__machine_state_operationsS");
26021 pragma Export (C, u00020, "system__machine_codeS");
26022 pragma Export (C, u00021, "system__storage_elementsB");
26023 pragma Export (C, u00022, "system__storage_elementsS");
26024 pragma Export (C, u00023, "system__secondary_stackB");
26025 pragma Export (C, u00024, "system__secondary_stackS");
26026 pragma Export (C, u00025, "system__parametersB");
26027 pragma Export (C, u00026, "system__parametersS");
26028 pragma Export (C, u00027, "system__soft_linksB");
26029 pragma Export (C, u00028, "system__soft_linksS");
26030 pragma Export (C, u00029, "system__stack_checkingB");
26031 pragma Export (C, u00030, "system__stack_checkingS");
26032 pragma Export (C, u00031, "system__tracebackB");
26033 pragma Export (C, u00032, "system__tracebackS");
26034 pragma Export (C, u00033, "ada__streamsS");
26035 pragma Export (C, u00034, "ada__tagsB");
26036 pragma Export (C, u00035, "ada__tagsS");
26037 pragma Export (C, u00036, "system__string_opsB");
26038 pragma Export (C, u00037, "system__string_opsS");
26039 pragma Export (C, u00038, "interfacesS");
26040 pragma Export (C, u00039, "interfaces__c_streamsB");
26041 pragma Export (C, u00040, "interfaces__c_streamsS");
26042 pragma Export (C, u00041, "system__file_ioB");
26043 pragma Export (C, u00042, "system__file_ioS");
26044 pragma Export (C, u00043, "ada__finalizationB");
26045 pragma Export (C, u00044, "ada__finalizationS");
26046 pragma Export (C, u00045, "system__finalization_rootB");
26047 pragma Export (C, u00046, "system__finalization_rootS");
26048 pragma Export (C, u00047, "system__finalization_implementationB");
26049 pragma Export (C, u00048, "system__finalization_implementationS");
26050 pragma Export (C, u00049, "system__string_ops_concat_3B");
26051 pragma Export (C, u00050, "system__string_ops_concat_3S");
26052 pragma Export (C, u00051, "system__stream_attributesB");
26053 pragma Export (C, u00052, "system__stream_attributesS");
26054 pragma Export (C, u00053, "ada__io_exceptionsS");
26055 pragma Export (C, u00054, "system__unsigned_typesS");
26056 pragma Export (C, u00055, "system__file_control_blockS");
26057 pragma Export (C, u00056, "ada__finalization__list_controllerB");
26058 pragma Export (C, u00057, "ada__finalization__list_controllerS");
26059
26060 -- BEGIN ELABORATION ORDER
26061 -- ada (spec)
26062 -- gnat (spec)
26063 -- gnat.heap_sort_a (spec)
26064 -- gnat.heap_sort_a (body)
26065 -- gnat.htable (spec)
26066 -- gnat.htable (body)
26067 -- interfaces (spec)
26068 -- system (spec)
26069 -- system.machine_code (spec)
26070 -- system.parameters (spec)
26071 -- system.parameters (body)
26072 -- interfaces.c_streams (spec)
26073 -- interfaces.c_streams (body)
26074 -- system.standard_library (spec)
26075 -- ada.exceptions (spec)
26076 -- system.exception_table (spec)
26077 -- system.exception_table (body)
26078 -- ada.io_exceptions (spec)
26079 -- system.exceptions (spec)
26080 -- system.storage_elements (spec)
26081 -- system.storage_elements (body)
26082 -- system.machine_state_operations (spec)
26083 -- system.machine_state_operations (body)
26084 -- system.secondary_stack (spec)
26085 -- system.stack_checking (spec)
26086 -- system.soft_links (spec)
26087 -- system.soft_links (body)
26088 -- system.stack_checking (body)
26089 -- system.secondary_stack (body)
26090 -- system.standard_library (body)
26091 -- system.string_ops (spec)
26092 -- system.string_ops (body)
26093 -- ada.tags (spec)
26094 -- ada.tags (body)
26095 -- ada.streams (spec)
26096 -- system.finalization_root (spec)
26097 -- system.finalization_root (body)
26098 -- system.string_ops_concat_3 (spec)
26099 -- system.string_ops_concat_3 (body)
26100 -- system.traceback (spec)
26101 -- system.traceback (body)
26102 -- ada.exceptions (body)
26103 -- system.unsigned_types (spec)
26104 -- system.stream_attributes (spec)
26105 -- system.stream_attributes (body)
26106 -- system.finalization_implementation (spec)
26107 -- system.finalization_implementation (body)
26108 -- ada.finalization (spec)
26109 -- ada.finalization (body)
26110 -- ada.finalization.list_controller (spec)
26111 -- ada.finalization.list_controller (body)
26112 -- system.file_control_block (spec)
26113 -- system.file_io (spec)
26114 -- system.file_io (body)
26115 -- ada.text_io (spec)
26116 -- ada.text_io (body)
26117 -- hello (body)
26118 -- END ELABORATION ORDER
26119
26120 end ada_main;
26121
26122 -- The following source file name pragmas allow the generated file
26123 -- names to be unique for different main programs. They are needed
26124 -- since the package name will always be Ada_Main.
26125
26126 pragma Source_File_Name (ada_main, Spec_File_Name => "b~hello.ads");
26127 pragma Source_File_Name (ada_main, Body_File_Name => "b~hello.adb");
26128
26129 -- Generated package body for Ada_Main starts here
26130
26131 package body ada_main is
26132
26133 -- The actual finalization is performed by calling the
26134 -- library routine in System.Standard_Library.Adafinal
26135
26136 procedure Do_Finalize;
26137 pragma Import (C, Do_Finalize, "system__standard_library__adafinal");
26138
26139 -------------
26140 -- adainit --
26141 -------------
26142
26143 @findex adainit
26144 procedure adainit is
26145
26146 -- These booleans are set to True once the associated unit has
26147 -- been elaborated. It is also used to avoid elaborating the
26148 -- same unit twice.
26149
26150 E040 : Boolean;
26151 pragma Import (Ada, E040, "interfaces__c_streams_E");
26152
26153 E008 : Boolean;
26154 pragma Import (Ada, E008, "ada__exceptions_E");
26155
26156 E014 : Boolean;
26157 pragma Import (Ada, E014, "system__exception_table_E");
26158
26159 E053 : Boolean;
26160 pragma Import (Ada, E053, "ada__io_exceptions_E");
26161
26162 E017 : Boolean;
26163 pragma Import (Ada, E017, "system__exceptions_E");
26164
26165 E024 : Boolean;
26166 pragma Import (Ada, E024, "system__secondary_stack_E");
26167
26168 E030 : Boolean;
26169 pragma Import (Ada, E030, "system__stack_checking_E");
26170
26171 E028 : Boolean;
26172 pragma Import (Ada, E028, "system__soft_links_E");
26173
26174 E035 : Boolean;
26175 pragma Import (Ada, E035, "ada__tags_E");
26176
26177 E033 : Boolean;
26178 pragma Import (Ada, E033, "ada__streams_E");
26179
26180 E046 : Boolean;
26181 pragma Import (Ada, E046, "system__finalization_root_E");
26182
26183 E048 : Boolean;
26184 pragma Import (Ada, E048, "system__finalization_implementation_E");
26185
26186 E044 : Boolean;
26187 pragma Import (Ada, E044, "ada__finalization_E");
26188
26189 E057 : Boolean;
26190 pragma Import (Ada, E057, "ada__finalization__list_controller_E");
26191
26192 E055 : Boolean;
26193 pragma Import (Ada, E055, "system__file_control_block_E");
26194
26195 E042 : Boolean;
26196 pragma Import (Ada, E042, "system__file_io_E");
26197
26198 E006 : Boolean;
26199 pragma Import (Ada, E006, "ada__text_io_E");
26200
26201 -- Set_Globals is a library routine that stores away the
26202 -- value of the indicated set of global values in global
26203 -- variables within the library.
26204
26205 procedure Set_Globals
26206 (Main_Priority : Integer;
26207 Time_Slice_Value : Integer;
26208 WC_Encoding : Character;
26209 Locking_Policy : Character;
26210 Queuing_Policy : Character;
26211 Task_Dispatching_Policy : Character;
26212 Adafinal : System.Address;
26213 Unreserve_All_Interrupts : Integer;
26214 Exception_Tracebacks : Integer);
26215 @findex __gnat_set_globals
26216 pragma Import (C, Set_Globals, "__gnat_set_globals");
26217
26218 -- SDP_Table_Build is a library routine used to build the
26219 -- exception tables. See unit Ada.Exceptions in files
26220 -- a-except.ads/adb for full details of how zero cost
26221 -- exception handling works. This procedure, the call to
26222 -- it, and the two following tables are all omitted if the
26223 -- build is in longjmp/setjmp exception mode.
26224
26225 @findex SDP_Table_Build
26226 @findex Zero Cost Exceptions
26227 procedure SDP_Table_Build
26228 (SDP_Addresses : System.Address;
26229 SDP_Count : Natural;
26230 Elab_Addresses : System.Address;
26231 Elab_Addr_Count : Natural);
26232 pragma Import (C, SDP_Table_Build, "__gnat_SDP_Table_Build");
26233
26234 -- Table of Unit_Exception_Table addresses. Used for zero
26235 -- cost exception handling to build the top level table.
26236
26237 ST : aliased constant array (1 .. 23) of System.Address := (
26238 Hello'UET_Address,
26239 Ada.Text_Io'UET_Address,
26240 Ada.Exceptions'UET_Address,
26241 Gnat.Heap_Sort_A'UET_Address,
26242 System.Exception_Table'UET_Address,
26243 System.Machine_State_Operations'UET_Address,
26244 System.Secondary_Stack'UET_Address,
26245 System.Parameters'UET_Address,
26246 System.Soft_Links'UET_Address,
26247 System.Stack_Checking'UET_Address,
26248 System.Traceback'UET_Address,
26249 Ada.Streams'UET_Address,
26250 Ada.Tags'UET_Address,
26251 System.String_Ops'UET_Address,
26252 Interfaces.C_Streams'UET_Address,
26253 System.File_Io'UET_Address,
26254 Ada.Finalization'UET_Address,
26255 System.Finalization_Root'UET_Address,
26256 System.Finalization_Implementation'UET_Address,
26257 System.String_Ops_Concat_3'UET_Address,
26258 System.Stream_Attributes'UET_Address,
26259 System.File_Control_Block'UET_Address,
26260 Ada.Finalization.List_Controller'UET_Address);
26261
26262 -- Table of addresses of elaboration routines. Used for
26263 -- zero cost exception handling to make sure these
26264 -- addresses are included in the top level procedure
26265 -- address table.
26266
26267 EA : aliased constant array (1 .. 23) of System.Address := (
26268 adainit'Code_Address,
26269 Do_Finalize'Code_Address,
26270 Ada.Exceptions'Elab_Spec'Address,
26271 System.Exceptions'Elab_Spec'Address,
26272 Interfaces.C_Streams'Elab_Spec'Address,
26273 System.Exception_Table'Elab_Body'Address,
26274 Ada.Io_Exceptions'Elab_Spec'Address,
26275 System.Stack_Checking'Elab_Spec'Address,
26276 System.Soft_Links'Elab_Body'Address,
26277 System.Secondary_Stack'Elab_Body'Address,
26278 Ada.Tags'Elab_Spec'Address,
26279 Ada.Tags'Elab_Body'Address,
26280 Ada.Streams'Elab_Spec'Address,
26281 System.Finalization_Root'Elab_Spec'Address,
26282 Ada.Exceptions'Elab_Body'Address,
26283 System.Finalization_Implementation'Elab_Spec'Address,
26284 System.Finalization_Implementation'Elab_Body'Address,
26285 Ada.Finalization'Elab_Spec'Address,
26286 Ada.Finalization.List_Controller'Elab_Spec'Address,
26287 System.File_Control_Block'Elab_Spec'Address,
26288 System.File_Io'Elab_Body'Address,
26289 Ada.Text_Io'Elab_Spec'Address,
26290 Ada.Text_Io'Elab_Body'Address);
26291
26292 -- Start of processing for adainit
26293
26294 begin
26295
26296 -- Call SDP_Table_Build to build the top level procedure
26297 -- table for zero cost exception handling (omitted in
26298 -- longjmp/setjmp mode).
26299
26300 SDP_Table_Build (ST'Address, 23, EA'Address, 23);
26301
26302 -- Call Set_Globals to record various information for
26303 -- this partition. The values are derived by the binder
26304 -- from information stored in the ali files by the compiler.
26305
26306 @findex __gnat_set_globals
26307 Set_Globals
26308 (Main_Priority => -1,
26309 -- Priority of main program, -1 if no pragma Priority used
26310
26311 Time_Slice_Value => -1,
26312 -- Time slice from Time_Slice pragma, -1 if none used
26313
26314 WC_Encoding => 'b',
26315 -- Wide_Character encoding used, default is brackets
26316
26317 Locking_Policy => ' ',
26318 -- Locking_Policy used, default of space means not
26319 -- specified, otherwise it is the first character of
26320 -- the policy name.
26321
26322 Queuing_Policy => ' ',
26323 -- Queuing_Policy used, default of space means not
26324 -- specified, otherwise it is the first character of
26325 -- the policy name.
26326
26327 Task_Dispatching_Policy => ' ',
26328 -- Task_Dispatching_Policy used, default of space means
26329 -- not specified, otherwise first character of the
26330 -- policy name.
26331
26332 Adafinal => System.Null_Address,
26333 -- Address of Adafinal routine, not used anymore
26334
26335 Unreserve_All_Interrupts => 0,
26336 -- Set true if pragma Unreserve_All_Interrupts was used
26337
26338 Exception_Tracebacks => 0);
26339 -- Indicates if exception tracebacks are enabled
26340
26341 Elab_Final_Code := 1;
26342
26343 -- Now we have the elaboration calls for all units in the partition.
26344 -- The Elab_Spec and Elab_Body attributes generate references to the
26345 -- implicit elaboration procedures generated by the compiler for
26346 -- each unit that requires elaboration.
26347
26348 if not E040 then
26349 Interfaces.C_Streams'Elab_Spec;
26350 end if;
26351 E040 := True;
26352 if not E008 then
26353 Ada.Exceptions'Elab_Spec;
26354 end if;
26355 if not E014 then
26356 System.Exception_Table'Elab_Body;
26357 E014 := True;
26358 end if;
26359 if not E053 then
26360 Ada.Io_Exceptions'Elab_Spec;
26361 E053 := True;
26362 end if;
26363 if not E017 then
26364 System.Exceptions'Elab_Spec;
26365 E017 := True;
26366 end if;
26367 if not E030 then
26368 System.Stack_Checking'Elab_Spec;
26369 end if;
26370 if not E028 then
26371 System.Soft_Links'Elab_Body;
26372 E028 := True;
26373 end if;
26374 E030 := True;
26375 if not E024 then
26376 System.Secondary_Stack'Elab_Body;
26377 E024 := True;
26378 end if;
26379 if not E035 then
26380 Ada.Tags'Elab_Spec;
26381 end if;
26382 if not E035 then
26383 Ada.Tags'Elab_Body;
26384 E035 := True;
26385 end if;
26386 if not E033 then
26387 Ada.Streams'Elab_Spec;
26388 E033 := True;
26389 end if;
26390 if not E046 then
26391 System.Finalization_Root'Elab_Spec;
26392 end if;
26393 E046 := True;
26394 if not E008 then
26395 Ada.Exceptions'Elab_Body;
26396 E008 := True;
26397 end if;
26398 if not E048 then
26399 System.Finalization_Implementation'Elab_Spec;
26400 end if;
26401 if not E048 then
26402 System.Finalization_Implementation'Elab_Body;
26403 E048 := True;
26404 end if;
26405 if not E044 then
26406 Ada.Finalization'Elab_Spec;
26407 end if;
26408 E044 := True;
26409 if not E057 then
26410 Ada.Finalization.List_Controller'Elab_Spec;
26411 end if;
26412 E057 := True;
26413 if not E055 then
26414 System.File_Control_Block'Elab_Spec;
26415 E055 := True;
26416 end if;
26417 if not E042 then
26418 System.File_Io'Elab_Body;
26419 E042 := True;
26420 end if;
26421 if not E006 then
26422 Ada.Text_Io'Elab_Spec;
26423 end if;
26424 if not E006 then
26425 Ada.Text_Io'Elab_Body;
26426 E006 := True;
26427 end if;
26428
26429 Elab_Final_Code := 0;
26430 end adainit;
26431
26432 --------------
26433 -- adafinal --
26434 --------------
26435
26436 @findex adafinal
26437 procedure adafinal is
26438 begin
26439 Do_Finalize;
26440 end adafinal;
26441
26442 ----------
26443 -- main --
26444 ----------
26445
26446 -- main is actually a function, as in the ANSI C standard,
26447 -- defined to return the exit status. The three parameters
26448 -- are the argument count, argument values and environment
26449 -- pointer.
26450
26451 @findex Main Program
26452 function main
26453 (argc : Integer;
26454 argv : System.Address;
26455 envp : System.Address)
26456 return Integer
26457 is
26458 -- The initialize routine performs low level system
26459 -- initialization using a standard library routine which
26460 -- sets up signal handling and performs any other
26461 -- required setup. The routine can be found in file
26462 -- a-init.c.
26463
26464 @findex __gnat_initialize
26465 procedure initialize;
26466 pragma Import (C, initialize, "__gnat_initialize");
26467
26468 -- The finalize routine performs low level system
26469 -- finalization using a standard library routine. The
26470 -- routine is found in file a-final.c and in the standard
26471 -- distribution is a dummy routine that does nothing, so
26472 -- really this is a hook for special user finalization.
26473
26474 @findex __gnat_finalize
26475 procedure finalize;
26476 pragma Import (C, finalize, "__gnat_finalize");
26477
26478 -- We get to the main program of the partition by using
26479 -- pragma Import because if we try to with the unit and
26480 -- call it Ada style, then not only do we waste time
26481 -- recompiling it, but also, we don't really know the right
26482 -- switches (e.g.@: identifier character set) to be used
26483 -- to compile it.
26484
26485 procedure Ada_Main_Program;
26486 pragma Import (Ada, Ada_Main_Program, "_ada_hello");
26487
26488 -- Start of processing for main
26489
26490 begin
26491 -- Save global variables
26492
26493 gnat_argc := argc;
26494 gnat_argv := argv;
26495 gnat_envp := envp;
26496
26497 -- Call low level system initialization
26498
26499 Initialize;
26500
26501 -- Call our generated Ada initialization routine
26502
26503 adainit;
26504
26505 -- This is the point at which we want the debugger to get
26506 -- control
26507
26508 Break_Start;
26509
26510 -- Now we call the main program of the partition
26511
26512 Ada_Main_Program;
26513
26514 -- Perform Ada finalization
26515
26516 adafinal;
26517
26518 -- Perform low level system finalization
26519
26520 Finalize;
26521
26522 -- Return the proper exit status
26523 return (gnat_exit_status);
26524 end;
26525
26526 -- This section is entirely comments, so it has no effect on the
26527 -- compilation of the Ada_Main package. It provides the list of
26528 -- object files and linker options, as well as some standard
26529 -- libraries needed for the link. The gnatlink utility parses
26530 -- this b~hello.adb file to read these comment lines to generate
26531 -- the appropriate command line arguments for the call to the
26532 -- system linker. The BEGIN/END lines are used for sentinels for
26533 -- this parsing operation.
26534
26535 -- The exact file names will of course depend on the environment,
26536 -- host/target and location of files on the host system.
26537
26538 @findex Object file list
26539 -- BEGIN Object file/option list
26540 -- ./hello.o
26541 -- -L./
26542 -- -L/usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/
26543 -- /usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a
26544 -- END Object file/option list
26545
26546 end ada_main;
26547 @end smallexample
26548
26549 @noindent
26550 The Ada code in the above example is exactly what is generated by the
26551 binder. We have added comments to more clearly indicate the function
26552 of each part of the generated @code{Ada_Main} package.
26553
26554 The code is standard Ada in all respects, and can be processed by any
26555 tools that handle Ada. In particular, it is possible to use the debugger
26556 in Ada mode to debug the generated @code{Ada_Main} package. For example,
26557 suppose that for reasons that you do not understand, your program is crashing
26558 during elaboration of the body of @code{Ada.Text_IO}. To locate this bug,
26559 you can place a breakpoint on the call:
26560
26561 @smallexample @c ada
26562 Ada.Text_Io'Elab_Body;
26563 @end smallexample
26564
26565 @noindent
26566 and trace the elaboration routine for this package to find out where
26567 the problem might be (more usually of course you would be debugging
26568 elaboration code in your own application).
26569
26570 @node Elaboration Order Handling in GNAT
26571 @appendix Elaboration Order Handling in GNAT
26572 @cindex Order of elaboration
26573 @cindex Elaboration control
26574
26575 @menu
26576 * Elaboration Code::
26577 * Checking the Elaboration Order::
26578 * Controlling the Elaboration Order::
26579 * Controlling Elaboration in GNAT - Internal Calls::
26580 * Controlling Elaboration in GNAT - External Calls::
26581 * Default Behavior in GNAT - Ensuring Safety::
26582 * Treatment of Pragma Elaborate::
26583 * Elaboration Issues for Library Tasks::
26584 * Mixing Elaboration Models::
26585 * What to Do If the Default Elaboration Behavior Fails::
26586 * Elaboration for Access-to-Subprogram Values::
26587 * Summary of Procedures for Elaboration Control::
26588 * Other Elaboration Order Considerations::
26589 @end menu
26590
26591 @noindent
26592 This chapter describes the handling of elaboration code in Ada and
26593 in GNAT, and discusses how the order of elaboration of program units can
26594 be controlled in GNAT, either automatically or with explicit programming
26595 features.
26596
26597 @node Elaboration Code
26598 @section Elaboration Code
26599
26600 @noindent
26601 Ada provides rather general mechanisms for executing code at elaboration
26602 time, that is to say before the main program starts executing. Such code arises
26603 in three contexts:
26604
26605 @table @asis
26606 @item Initializers for variables.
26607 Variables declared at the library level, in package specs or bodies, can
26608 require initialization that is performed at elaboration time, as in:
26609 @smallexample @c ada
26610 @cartouche
26611 Sqrt_Half : Float := Sqrt (0.5);
26612 @end cartouche
26613 @end smallexample
26614
26615 @item Package initialization code
26616 Code in a @code{BEGIN-END} section at the outer level of a package body is
26617 executed as part of the package body elaboration code.
26618
26619 @item Library level task allocators
26620 Tasks that are declared using task allocators at the library level
26621 start executing immediately and hence can execute at elaboration time.
26622 @end table
26623
26624 @noindent
26625 Subprogram calls are possible in any of these contexts, which means that
26626 any arbitrary part of the program may be executed as part of the elaboration
26627 code. It is even possible to write a program which does all its work at
26628 elaboration time, with a null main program, although stylistically this
26629 would usually be considered an inappropriate way to structure
26630 a program.
26631
26632 An important concern arises in the context of elaboration code:
26633 we have to be sure that it is executed in an appropriate order. What we
26634 have is a series of elaboration code sections, potentially one section
26635 for each unit in the program. It is important that these execute
26636 in the correct order. Correctness here means that, taking the above
26637 example of the declaration of @code{Sqrt_Half},
26638 if some other piece of
26639 elaboration code references @code{Sqrt_Half},
26640 then it must run after the
26641 section of elaboration code that contains the declaration of
26642 @code{Sqrt_Half}.
26643
26644 There would never be any order of elaboration problem if we made a rule
26645 that whenever you @code{with} a unit, you must elaborate both the spec and body
26646 of that unit before elaborating the unit doing the @code{with}'ing:
26647
26648 @smallexample @c ada
26649 @group
26650 @cartouche
26651 with Unit_1;
26652 package Unit_2 is @dots{}
26653 @end cartouche
26654 @end group
26655 @end smallexample
26656
26657 @noindent
26658 would require that both the body and spec of @code{Unit_1} be elaborated
26659 before the spec of @code{Unit_2}. However, a rule like that would be far too
26660 restrictive. In particular, it would make it impossible to have routines
26661 in separate packages that were mutually recursive.
26662
26663 You might think that a clever enough compiler could look at the actual
26664 elaboration code and determine an appropriate correct order of elaboration,
26665 but in the general case, this is not possible. Consider the following
26666 example.
26667
26668 In the body of @code{Unit_1}, we have a procedure @code{Func_1}
26669 that references
26670 the variable @code{Sqrt_1}, which is declared in the elaboration code
26671 of the body of @code{Unit_1}:
26672
26673 @smallexample @c ada
26674 @cartouche
26675 Sqrt_1 : Float := Sqrt (0.1);
26676 @end cartouche
26677 @end smallexample
26678
26679 @noindent
26680 The elaboration code of the body of @code{Unit_1} also contains:
26681
26682 @smallexample @c ada
26683 @group
26684 @cartouche
26685 if expression_1 = 1 then
26686 Q := Unit_2.Func_2;
26687 end if;
26688 @end cartouche
26689 @end group
26690 @end smallexample
26691
26692 @noindent
26693 @code{Unit_2} is exactly parallel,
26694 it has a procedure @code{Func_2} that references
26695 the variable @code{Sqrt_2}, which is declared in the elaboration code of
26696 the body @code{Unit_2}:
26697
26698 @smallexample @c ada
26699 @cartouche
26700 Sqrt_2 : Float := Sqrt (0.1);
26701 @end cartouche
26702 @end smallexample
26703
26704 @noindent
26705 The elaboration code of the body of @code{Unit_2} also contains:
26706
26707 @smallexample @c ada
26708 @group
26709 @cartouche
26710 if expression_2 = 2 then
26711 Q := Unit_1.Func_1;
26712 end if;
26713 @end cartouche
26714 @end group
26715 @end smallexample
26716
26717 @noindent
26718 Now the question is, which of the following orders of elaboration is
26719 acceptable:
26720
26721 @smallexample
26722 @group
26723 Spec of Unit_1
26724 Spec of Unit_2
26725 Body of Unit_1
26726 Body of Unit_2
26727 @end group
26728 @end smallexample
26729
26730 @noindent
26731 or
26732
26733 @smallexample
26734 @group
26735 Spec of Unit_2
26736 Spec of Unit_1
26737 Body of Unit_2
26738 Body of Unit_1
26739 @end group
26740 @end smallexample
26741
26742 @noindent
26743 If you carefully analyze the flow here, you will see that you cannot tell
26744 at compile time the answer to this question.
26745 If @code{expression_1} is not equal to 1,
26746 and @code{expression_2} is not equal to 2,
26747 then either order is acceptable, because neither of the function calls is
26748 executed. If both tests evaluate to true, then neither order is acceptable
26749 and in fact there is no correct order.
26750
26751 If one of the two expressions is true, and the other is false, then one
26752 of the above orders is correct, and the other is incorrect. For example,
26753 if @code{expression_1} /= 1 and @code{expression_2} = 2,
26754 then the call to @code{Func_1}
26755 will occur, but not the call to @code{Func_2.}
26756 This means that it is essential
26757 to elaborate the body of @code{Unit_1} before
26758 the body of @code{Unit_2}, so the first
26759 order of elaboration is correct and the second is wrong.
26760
26761 By making @code{expression_1} and @code{expression_2}
26762 depend on input data, or perhaps
26763 the time of day, we can make it impossible for the compiler or binder
26764 to figure out which of these expressions will be true, and hence it
26765 is impossible to guarantee a safe order of elaboration at run time.
26766
26767 @node Checking the Elaboration Order
26768 @section Checking the Elaboration Order
26769
26770 @noindent
26771 In some languages that involve the same kind of elaboration problems,
26772 e.g.@: Java and C++, the programmer is expected to worry about these
26773 ordering problems himself, and it is common to
26774 write a program in which an incorrect elaboration order gives
26775 surprising results, because it references variables before they
26776 are initialized.
26777 Ada is designed to be a safe language, and a programmer-beware approach is
26778 clearly not sufficient. Consequently, the language provides three lines
26779 of defense:
26780
26781 @table @asis
26782 @item Standard rules
26783 Some standard rules restrict the possible choice of elaboration
26784 order. In particular, if you @code{with} a unit, then its spec is always
26785 elaborated before the unit doing the @code{with}. Similarly, a parent
26786 spec is always elaborated before the child spec, and finally
26787 a spec is always elaborated before its corresponding body.
26788
26789 @item Dynamic elaboration checks
26790 @cindex Elaboration checks
26791 @cindex Checks, elaboration
26792 Dynamic checks are made at run time, so that if some entity is accessed
26793 before it is elaborated (typically by means of a subprogram call)
26794 then the exception (@code{Program_Error}) is raised.
26795
26796 @item Elaboration control
26797 Facilities are provided for the programmer to specify the desired order
26798 of elaboration.
26799 @end table
26800
26801 Let's look at these facilities in more detail. First, the rules for
26802 dynamic checking. One possible rule would be simply to say that the
26803 exception is raised if you access a variable which has not yet been
26804 elaborated. The trouble with this approach is that it could require
26805 expensive checks on every variable reference. Instead Ada has two
26806 rules which are a little more restrictive, but easier to check, and
26807 easier to state:
26808
26809 @table @asis
26810 @item Restrictions on calls
26811 A subprogram can only be called at elaboration time if its body
26812 has been elaborated. The rules for elaboration given above guarantee
26813 that the spec of the subprogram has been elaborated before the
26814 call, but not the body. If this rule is violated, then the
26815 exception @code{Program_Error} is raised.
26816
26817 @item Restrictions on instantiations
26818 A generic unit can only be instantiated if the body of the generic
26819 unit has been elaborated. Again, the rules for elaboration given above
26820 guarantee that the spec of the generic unit has been elaborated
26821 before the instantiation, but not the body. If this rule is
26822 violated, then the exception @code{Program_Error} is raised.
26823 @end table
26824
26825 @noindent
26826 The idea is that if the body has been elaborated, then any variables
26827 it references must have been elaborated; by checking for the body being
26828 elaborated we guarantee that none of its references causes any
26829 trouble. As we noted above, this is a little too restrictive, because a
26830 subprogram that has no non-local references in its body may in fact be safe
26831 to call. However, it really would be unsafe to rely on this, because
26832 it would mean that the caller was aware of details of the implementation
26833 in the body. This goes against the basic tenets of Ada.
26834
26835 A plausible implementation can be described as follows.
26836 A Boolean variable is associated with each subprogram
26837 and each generic unit. This variable is initialized to False, and is set to
26838 True at the point body is elaborated. Every call or instantiation checks the
26839 variable, and raises @code{Program_Error} if the variable is False.
26840
26841 Note that one might think that it would be good enough to have one Boolean
26842 variable for each package, but that would not deal with cases of trying
26843 to call a body in the same package as the call
26844 that has not been elaborated yet.
26845 Of course a compiler may be able to do enough analysis to optimize away
26846 some of the Boolean variables as unnecessary, and @code{GNAT} indeed
26847 does such optimizations, but still the easiest conceptual model is to
26848 think of there being one variable per subprogram.
26849
26850 @node Controlling the Elaboration Order
26851 @section Controlling the Elaboration Order
26852
26853 @noindent
26854 In the previous section we discussed the rules in Ada which ensure
26855 that @code{Program_Error} is raised if an incorrect elaboration order is
26856 chosen. This prevents erroneous executions, but we need mechanisms to
26857 specify a correct execution and avoid the exception altogether.
26858 To achieve this, Ada provides a number of features for controlling
26859 the order of elaboration. We discuss these features in this section.
26860
26861 First, there are several ways of indicating to the compiler that a given
26862 unit has no elaboration problems:
26863
26864 @table @asis
26865 @item packages that do not require a body
26866 A library package that does not require a body does not permit
26867 a body (this rule was introduced in Ada 95).
26868 Thus if we have a such a package, as in:
26869
26870 @smallexample @c ada
26871 @group
26872 @cartouche
26873 package Definitions is
26874 generic
26875 type m is new integer;
26876 package Subp is
26877 type a is array (1 .. 10) of m;
26878 type b is array (1 .. 20) of m;
26879 end Subp;
26880 end Definitions;
26881 @end cartouche
26882 @end group
26883 @end smallexample
26884
26885 @noindent
26886 A package that @code{with}'s @code{Definitions} may safely instantiate
26887 @code{Definitions.Subp} because the compiler can determine that there
26888 definitely is no package body to worry about in this case
26889
26890 @item pragma Pure
26891 @cindex pragma Pure
26892 @findex Pure
26893 Places sufficient restrictions on a unit to guarantee that
26894 no call to any subprogram in the unit can result in an
26895 elaboration problem. This means that the compiler does not need
26896 to worry about the point of elaboration of such units, and in
26897 particular, does not need to check any calls to any subprograms
26898 in this unit.
26899
26900 @item pragma Preelaborate
26901 @findex Preelaborate
26902 @cindex pragma Preelaborate
26903 This pragma places slightly less stringent restrictions on a unit than
26904 does pragma Pure,
26905 but these restrictions are still sufficient to ensure that there
26906 are no elaboration problems with any calls to the unit.
26907
26908 @item pragma Elaborate_Body
26909 @findex Elaborate_Body
26910 @cindex pragma Elaborate_Body
26911 This pragma requires that the body of a unit be elaborated immediately
26912 after its spec. Suppose a unit @code{A} has such a pragma,
26913 and unit @code{B} does
26914 a @code{with} of unit @code{A}. Recall that the standard rules require
26915 the spec of unit @code{A}
26916 to be elaborated before the @code{with}'ing unit; given the pragma in
26917 @code{A}, we also know that the body of @code{A}
26918 will be elaborated before @code{B}, so
26919 that calls to @code{A} are safe and do not need a check.
26920 @end table
26921
26922 @noindent
26923 Note that,
26924 unlike pragma @code{Pure} and pragma @code{Preelaborate},
26925 the use of
26926 @code{Elaborate_Body} does not guarantee that the program is
26927 free of elaboration problems, because it may not be possible
26928 to satisfy the requested elaboration order.
26929 Let's go back to the example with @code{Unit_1} and @code{Unit_2}.
26930 If a programmer
26931 marks @code{Unit_1} as @code{Elaborate_Body},
26932 and not @code{Unit_2,} then the order of
26933 elaboration will be:
26934
26935 @smallexample
26936 @group
26937 Spec of Unit_2
26938 Spec of Unit_1
26939 Body of Unit_1
26940 Body of Unit_2
26941 @end group
26942 @end smallexample
26943
26944 @noindent
26945 Now that means that the call to @code{Func_1} in @code{Unit_2}
26946 need not be checked,
26947 it must be safe. But the call to @code{Func_2} in
26948 @code{Unit_1} may still fail if
26949 @code{Expression_1} is equal to 1,
26950 and the programmer must still take
26951 responsibility for this not being the case.
26952
26953 If all units carry a pragma @code{Elaborate_Body}, then all problems are
26954 eliminated, except for calls entirely within a body, which are
26955 in any case fully under programmer control. However, using the pragma
26956 everywhere is not always possible.
26957 In particular, for our @code{Unit_1}/@code{Unit_2} example, if
26958 we marked both of them as having pragma @code{Elaborate_Body}, then
26959 clearly there would be no possible elaboration order.
26960
26961 The above pragmas allow a server to guarantee safe use by clients, and
26962 clearly this is the preferable approach. Consequently a good rule
26963 is to mark units as @code{Pure} or @code{Preelaborate} if possible,
26964 and if this is not possible,
26965 mark them as @code{Elaborate_Body} if possible.
26966 As we have seen, there are situations where neither of these
26967 three pragmas can be used.
26968 So we also provide methods for clients to control the
26969 order of elaboration of the servers on which they depend:
26970
26971 @table @asis
26972 @item pragma Elaborate (unit)
26973 @findex Elaborate
26974 @cindex pragma Elaborate
26975 This pragma is placed in the context clause, after a @code{with} clause,
26976 and it requires that the body of the named unit be elaborated before
26977 the unit in which the pragma occurs. The idea is to use this pragma
26978 if the current unit calls at elaboration time, directly or indirectly,
26979 some subprogram in the named unit.
26980
26981 @item pragma Elaborate_All (unit)
26982 @findex Elaborate_All
26983 @cindex pragma Elaborate_All
26984 This is a stronger version of the Elaborate pragma. Consider the
26985 following example:
26986
26987 @smallexample
26988 Unit A @code{with}'s unit B and calls B.Func in elab code
26989 Unit B @code{with}'s unit C, and B.Func calls C.Func
26990 @end smallexample
26991
26992 @noindent
26993 Now if we put a pragma @code{Elaborate (B)}
26994 in unit @code{A}, this ensures that the
26995 body of @code{B} is elaborated before the call, but not the
26996 body of @code{C}, so
26997 the call to @code{C.Func} could still cause @code{Program_Error} to
26998 be raised.
26999
27000 The effect of a pragma @code{Elaborate_All} is stronger, it requires
27001 not only that the body of the named unit be elaborated before the
27002 unit doing the @code{with}, but also the bodies of all units that the
27003 named unit uses, following @code{with} links transitively. For example,
27004 if we put a pragma @code{Elaborate_All (B)} in unit @code{A},
27005 then it requires
27006 not only that the body of @code{B} be elaborated before @code{A},
27007 but also the
27008 body of @code{C}, because @code{B} @code{with}'s @code{C}.
27009 @end table
27010
27011 @noindent
27012 We are now in a position to give a usage rule in Ada for avoiding
27013 elaboration problems, at least if dynamic dispatching and access to
27014 subprogram values are not used. We will handle these cases separately
27015 later.
27016
27017 The rule is simple. If a unit has elaboration code that can directly or
27018 indirectly make a call to a subprogram in a @code{with}'ed unit, or instantiate
27019 a generic package in a @code{with}'ed unit,
27020 then if the @code{with}'ed unit does not have
27021 pragma @code{Pure} or @code{Preelaborate}, then the client should have
27022 a pragma @code{Elaborate_All}
27023 for the @code{with}'ed unit. By following this rule a client is
27024 assured that calls can be made without risk of an exception.
27025
27026 For generic subprogram instantiations, the rule can be relaxed to
27027 require only a pragma @code{Elaborate} since elaborating the body
27028 of a subprogram cannot cause any transitive elaboration (we are
27029 not calling the subprogram in this case, just elaborating its
27030 declaration).
27031
27032 If this rule is not followed, then a program may be in one of four
27033 states:
27034
27035 @table @asis
27036 @item No order exists
27037 No order of elaboration exists which follows the rules, taking into
27038 account any @code{Elaborate}, @code{Elaborate_All},
27039 or @code{Elaborate_Body} pragmas. In
27040 this case, an Ada compiler must diagnose the situation at bind
27041 time, and refuse to build an executable program.
27042
27043 @item One or more orders exist, all incorrect
27044 One or more acceptable elaboration orders exist, and all of them
27045 generate an elaboration order problem. In this case, the binder
27046 can build an executable program, but @code{Program_Error} will be raised
27047 when the program is run.
27048
27049 @item Several orders exist, some right, some incorrect
27050 One or more acceptable elaboration orders exists, and some of them
27051 work, and some do not. The programmer has not controlled
27052 the order of elaboration, so the binder may or may not pick one of
27053 the correct orders, and the program may or may not raise an
27054 exception when it is run. This is the worst case, because it means
27055 that the program may fail when moved to another compiler, or even
27056 another version of the same compiler.
27057
27058 @item One or more orders exists, all correct
27059 One ore more acceptable elaboration orders exist, and all of them
27060 work. In this case the program runs successfully. This state of
27061 affairs can be guaranteed by following the rule we gave above, but
27062 may be true even if the rule is not followed.
27063 @end table
27064
27065 @noindent
27066 Note that one additional advantage of following our rules on the use
27067 of @code{Elaborate} and @code{Elaborate_All}
27068 is that the program continues to stay in the ideal (all orders OK) state
27069 even if maintenance
27070 changes some bodies of some units. Conversely, if a program that does
27071 not follow this rule happens to be safe at some point, this state of affairs
27072 may deteriorate silently as a result of maintenance changes.
27073
27074 You may have noticed that the above discussion did not mention
27075 the use of @code{Elaborate_Body}. This was a deliberate omission. If you
27076 @code{with} an @code{Elaborate_Body} unit, it still may be the case that
27077 code in the body makes calls to some other unit, so it is still necessary
27078 to use @code{Elaborate_All} on such units.
27079
27080 @node Controlling Elaboration in GNAT - Internal Calls
27081 @section Controlling Elaboration in GNAT - Internal Calls
27082
27083 @noindent
27084 In the case of internal calls, i.e., calls within a single package, the
27085 programmer has full control over the order of elaboration, and it is up
27086 to the programmer to elaborate declarations in an appropriate order. For
27087 example writing:
27088
27089 @smallexample @c ada
27090 @group
27091 @cartouche
27092 function One return Float;
27093
27094 Q : Float := One;
27095
27096 function One return Float is
27097 begin
27098 return 1.0;
27099 end One;
27100 @end cartouche
27101 @end group
27102 @end smallexample
27103
27104 @noindent
27105 will obviously raise @code{Program_Error} at run time, because function
27106 One will be called before its body is elaborated. In this case GNAT will
27107 generate a warning that the call will raise @code{Program_Error}:
27108
27109 @smallexample
27110 @group
27111 @cartouche
27112 1. procedure y is
27113 2. function One return Float;
27114 3.
27115 4. Q : Float := One;
27116 |
27117 >>> warning: cannot call "One" before body is elaborated
27118 >>> warning: Program_Error will be raised at run time
27119
27120 5.
27121 6. function One return Float is
27122 7. begin
27123 8. return 1.0;
27124 9. end One;
27125 10.
27126 11. begin
27127 12. null;
27128 13. end;
27129 @end cartouche
27130 @end group
27131 @end smallexample
27132
27133 @noindent
27134 Note that in this particular case, it is likely that the call is safe, because
27135 the function @code{One} does not access any global variables.
27136 Nevertheless in Ada, we do not want the validity of the check to depend on
27137 the contents of the body (think about the separate compilation case), so this
27138 is still wrong, as we discussed in the previous sections.
27139
27140 The error is easily corrected by rearranging the declarations so that the
27141 body of @code{One} appears before the declaration containing the call
27142 (note that in Ada 95 and Ada 2005,
27143 declarations can appear in any order, so there is no restriction that
27144 would prevent this reordering, and if we write:
27145
27146 @smallexample @c ada
27147 @group
27148 @cartouche
27149 function One return Float;
27150
27151 function One return Float is
27152 begin
27153 return 1.0;
27154 end One;
27155
27156 Q : Float := One;
27157 @end cartouche
27158 @end group
27159 @end smallexample
27160
27161 @noindent
27162 then all is well, no warning is generated, and no
27163 @code{Program_Error} exception
27164 will be raised.
27165 Things are more complicated when a chain of subprograms is executed:
27166
27167 @smallexample @c ada
27168 @group
27169 @cartouche
27170 function A return Integer;
27171 function B return Integer;
27172 function C return Integer;
27173
27174 function B return Integer is begin return A; end;
27175 function C return Integer is begin return B; end;
27176
27177 X : Integer := C;
27178
27179 function A return Integer is begin return 1; end;
27180 @end cartouche
27181 @end group
27182 @end smallexample
27183
27184 @noindent
27185 Now the call to @code{C}
27186 at elaboration time in the declaration of @code{X} is correct, because
27187 the body of @code{C} is already elaborated,
27188 and the call to @code{B} within the body of
27189 @code{C} is correct, but the call
27190 to @code{A} within the body of @code{B} is incorrect, because the body
27191 of @code{A} has not been elaborated, so @code{Program_Error}
27192 will be raised on the call to @code{A}.
27193 In this case GNAT will generate a
27194 warning that @code{Program_Error} may be
27195 raised at the point of the call. Let's look at the warning:
27196
27197 @smallexample
27198 @group
27199 @cartouche
27200 1. procedure x is
27201 2. function A return Integer;
27202 3. function B return Integer;
27203 4. function C return Integer;
27204 5.
27205 6. function B return Integer is begin return A; end;
27206 |
27207 >>> warning: call to "A" before body is elaborated may
27208 raise Program_Error
27209 >>> warning: "B" called at line 7
27210 >>> warning: "C" called at line 9
27211
27212 7. function C return Integer is begin return B; end;
27213 8.
27214 9. X : Integer := C;
27215 10.
27216 11. function A return Integer is begin return 1; end;
27217 12.
27218 13. begin
27219 14. null;
27220 15. end;
27221 @end cartouche
27222 @end group
27223 @end smallexample
27224
27225 @noindent
27226 Note that the message here says ``may raise'', instead of the direct case,
27227 where the message says ``will be raised''. That's because whether
27228 @code{A} is
27229 actually called depends in general on run-time flow of control.
27230 For example, if the body of @code{B} said
27231
27232 @smallexample @c ada
27233 @group
27234 @cartouche
27235 function B return Integer is
27236 begin
27237 if some-condition-depending-on-input-data then
27238 return A;
27239 else
27240 return 1;
27241 end if;
27242 end B;
27243 @end cartouche
27244 @end group
27245 @end smallexample
27246
27247 @noindent
27248 then we could not know until run time whether the incorrect call to A would
27249 actually occur, so @code{Program_Error} might
27250 or might not be raised. It is possible for a compiler to
27251 do a better job of analyzing bodies, to
27252 determine whether or not @code{Program_Error}
27253 might be raised, but it certainly
27254 couldn't do a perfect job (that would require solving the halting problem
27255 and is provably impossible), and because this is a warning anyway, it does
27256 not seem worth the effort to do the analysis. Cases in which it
27257 would be relevant are rare.
27258
27259 In practice, warnings of either of the forms given
27260 above will usually correspond to
27261 real errors, and should be examined carefully and eliminated.
27262 In the rare case where a warning is bogus, it can be suppressed by any of
27263 the following methods:
27264
27265 @itemize @bullet
27266 @item
27267 Compile with the @option{-gnatws} switch set
27268
27269 @item
27270 Suppress @code{Elaboration_Check} for the called subprogram
27271
27272 @item
27273 Use pragma @code{Warnings_Off} to turn warnings off for the call
27274 @end itemize
27275
27276 @noindent
27277 For the internal elaboration check case,
27278 GNAT by default generates the
27279 necessary run-time checks to ensure
27280 that @code{Program_Error} is raised if any
27281 call fails an elaboration check. Of course this can only happen if a
27282 warning has been issued as described above. The use of pragma
27283 @code{Suppress (Elaboration_Check)} may (but is not guaranteed to) suppress
27284 some of these checks, meaning that it may be possible (but is not
27285 guaranteed) for a program to be able to call a subprogram whose body
27286 is not yet elaborated, without raising a @code{Program_Error} exception.
27287
27288 @node Controlling Elaboration in GNAT - External Calls
27289 @section Controlling Elaboration in GNAT - External Calls
27290
27291 @noindent
27292 The previous section discussed the case in which the execution of a
27293 particular thread of elaboration code occurred entirely within a
27294 single unit. This is the easy case to handle, because a programmer
27295 has direct and total control over the order of elaboration, and
27296 furthermore, checks need only be generated in cases which are rare
27297 and which the compiler can easily detect.
27298 The situation is more complex when separate compilation is taken into account.
27299 Consider the following:
27300
27301 @smallexample @c ada
27302 @cartouche
27303 @group
27304 package Math is
27305 function Sqrt (Arg : Float) return Float;
27306 end Math;
27307
27308 package body Math is
27309 function Sqrt (Arg : Float) return Float is
27310 begin
27311 @dots{}
27312 end Sqrt;
27313 end Math;
27314 @end group
27315 @group
27316 with Math;
27317 package Stuff is
27318 X : Float := Math.Sqrt (0.5);
27319 end Stuff;
27320
27321 with Stuff;
27322 procedure Main is
27323 begin
27324 @dots{}
27325 end Main;
27326 @end group
27327 @end cartouche
27328 @end smallexample
27329
27330 @noindent
27331 where @code{Main} is the main program. When this program is executed, the
27332 elaboration code must first be executed, and one of the jobs of the
27333 binder is to determine the order in which the units of a program are
27334 to be elaborated. In this case we have four units: the spec and body
27335 of @code{Math},
27336 the spec of @code{Stuff} and the body of @code{Main}).
27337 In what order should the four separate sections of elaboration code
27338 be executed?
27339
27340 There are some restrictions in the order of elaboration that the binder
27341 can choose. In particular, if unit U has a @code{with}
27342 for a package @code{X}, then you
27343 are assured that the spec of @code{X}
27344 is elaborated before U , but you are
27345 not assured that the body of @code{X}
27346 is elaborated before U.
27347 This means that in the above case, the binder is allowed to choose the
27348 order:
27349
27350 @smallexample
27351 spec of Math
27352 spec of Stuff
27353 body of Math
27354 body of Main
27355 @end smallexample
27356
27357 @noindent
27358 but that's not good, because now the call to @code{Math.Sqrt}
27359 that happens during
27360 the elaboration of the @code{Stuff}
27361 spec happens before the body of @code{Math.Sqrt} is
27362 elaborated, and hence causes @code{Program_Error} exception to be raised.
27363 At first glance, one might say that the binder is misbehaving, because
27364 obviously you want to elaborate the body of something you @code{with}
27365 first, but
27366 that is not a general rule that can be followed in all cases. Consider
27367
27368 @smallexample @c ada
27369 @group
27370 @cartouche
27371 package X is @dots{}
27372
27373 package Y is @dots{}
27374
27375 with X;
27376 package body Y is @dots{}
27377
27378 with Y;
27379 package body X is @dots{}
27380 @end cartouche
27381 @end group
27382 @end smallexample
27383
27384 @noindent
27385 This is a common arrangement, and, apart from the order of elaboration
27386 problems that might arise in connection with elaboration code, this works fine.
27387 A rule that says that you must first elaborate the body of anything you
27388 @code{with} cannot work in this case:
27389 the body of @code{X} @code{with}'s @code{Y},
27390 which means you would have to
27391 elaborate the body of @code{Y} first, but that @code{with}'s @code{X},
27392 which means
27393 you have to elaborate the body of @code{X} first, but @dots{} and we have a
27394 loop that cannot be broken.
27395
27396 It is true that the binder can in many cases guess an order of elaboration
27397 that is unlikely to cause a @code{Program_Error}
27398 exception to be raised, and it tries to do so (in the
27399 above example of @code{Math/Stuff/Spec}, the GNAT binder will
27400 by default
27401 elaborate the body of @code{Math} right after its spec, so all will be well).
27402
27403 However, a program that blindly relies on the binder to be helpful can
27404 get into trouble, as we discussed in the previous sections, so
27405 GNAT
27406 provides a number of facilities for assisting the programmer in
27407 developing programs that are robust with respect to elaboration order.
27408
27409 @node Default Behavior in GNAT - Ensuring Safety
27410 @section Default Behavior in GNAT - Ensuring Safety
27411
27412 @noindent
27413 The default behavior in GNAT ensures elaboration safety. In its
27414 default mode GNAT implements the
27415 rule we previously described as the right approach. Let's restate it:
27416
27417 @itemize
27418 @item
27419 @emph{If a unit has elaboration code that can directly or indirectly make a
27420 call to a subprogram in a @code{with}'ed unit, or instantiate a generic
27421 package in a @code{with}'ed unit, then if the @code{with}'ed unit
27422 does not have pragma @code{Pure} or
27423 @code{Preelaborate}, then the client should have an
27424 @code{Elaborate_All} pragma for the @code{with}'ed unit.}
27425
27426 @emph{In the case of instantiating a generic subprogram, it is always
27427 sufficient to have only an @code{Elaborate} pragma for the
27428 @code{with}'ed unit.}
27429 @end itemize
27430
27431 @noindent
27432 By following this rule a client is assured that calls and instantiations
27433 can be made without risk of an exception.
27434
27435 In this mode GNAT traces all calls that are potentially made from
27436 elaboration code, and puts in any missing implicit @code{Elaborate}
27437 and @code{Elaborate_All} pragmas.
27438 The advantage of this approach is that no elaboration problems
27439 are possible if the binder can find an elaboration order that is
27440 consistent with these implicit @code{Elaborate} and
27441 @code{Elaborate_All} pragmas. The
27442 disadvantage of this approach is that no such order may exist.
27443
27444 If the binder does not generate any diagnostics, then it means that it has
27445 found an elaboration order that is guaranteed to be safe. However, the binder
27446 may still be relying on implicitly generated @code{Elaborate} and
27447 @code{Elaborate_All} pragmas so portability to other compilers than GNAT is not
27448 guaranteed.
27449
27450 If it is important to guarantee portability, then the compilations should
27451 use the
27452 @option{-gnatwl}
27453 (warn on elaboration problems) switch. This will cause warning messages
27454 to be generated indicating the missing @code{Elaborate} and
27455 @code{Elaborate_All} pragmas.
27456 Consider the following source program:
27457
27458 @smallexample @c ada
27459 @group
27460 @cartouche
27461 with k;
27462 package j is
27463 m : integer := k.r;
27464 end;
27465 @end cartouche
27466 @end group
27467 @end smallexample
27468
27469 @noindent
27470 where it is clear that there
27471 should be a pragma @code{Elaborate_All}
27472 for unit @code{k}. An implicit pragma will be generated, and it is
27473 likely that the binder will be able to honor it. However, if you want
27474 to port this program to some other Ada compiler than GNAT.
27475 it is safer to include the pragma explicitly in the source. If this
27476 unit is compiled with the
27477 @option{-gnatwl}
27478 switch, then the compiler outputs a warning:
27479
27480 @smallexample
27481 @group
27482 @cartouche
27483 1. with k;
27484 2. package j is
27485 3. m : integer := k.r;
27486 |
27487 >>> warning: call to "r" may raise Program_Error
27488 >>> warning: missing pragma Elaborate_All for "k"
27489
27490 4. end;
27491 @end cartouche
27492 @end group
27493 @end smallexample
27494
27495 @noindent
27496 and these warnings can be used as a guide for supplying manually
27497 the missing pragmas. It is usually a bad idea to use this warning
27498 option during development. That's because it will warn you when
27499 you need to put in a pragma, but cannot warn you when it is time
27500 to take it out. So the use of pragma @code{Elaborate_All} may lead to
27501 unnecessary dependencies and even false circularities.
27502
27503 This default mode is more restrictive than the Ada Reference
27504 Manual, and it is possible to construct programs which will compile
27505 using the dynamic model described there, but will run into a
27506 circularity using the safer static model we have described.
27507
27508 Of course any Ada compiler must be able to operate in a mode
27509 consistent with the requirements of the Ada Reference Manual,
27510 and in particular must have the capability of implementing the
27511 standard dynamic model of elaboration with run-time checks.
27512
27513 In GNAT, this standard mode can be achieved either by the use of
27514 the @option{-gnatE} switch on the compiler (@command{gcc} or
27515 @command{gnatmake}) command, or by the use of the configuration pragma:
27516
27517 @smallexample @c ada
27518 pragma Elaboration_Checks (RM);
27519 @end smallexample
27520
27521 @noindent
27522 Either approach will cause the unit affected to be compiled using the
27523 standard dynamic run-time elaboration checks described in the Ada
27524 Reference Manual. The static model is generally preferable, since it
27525 is clearly safer to rely on compile and link time checks rather than
27526 run-time checks. However, in the case of legacy code, it may be
27527 difficult to meet the requirements of the static model. This
27528 issue is further discussed in
27529 @ref{What to Do If the Default Elaboration Behavior Fails}.
27530
27531 Note that the static model provides a strict subset of the allowed
27532 behavior and programs of the Ada Reference Manual, so if you do
27533 adhere to the static model and no circularities exist,
27534 then you are assured that your program will
27535 work using the dynamic model, providing that you remove any
27536 pragma Elaborate statements from the source.
27537
27538 @node Treatment of Pragma Elaborate
27539 @section Treatment of Pragma Elaborate
27540 @cindex Pragma Elaborate
27541
27542 @noindent
27543 The use of @code{pragma Elaborate}
27544 should generally be avoided in Ada 95 and Ada 2005 programs,
27545 since there is no guarantee that transitive calls
27546 will be properly handled. Indeed at one point, this pragma was placed
27547 in Annex J (Obsolescent Features), on the grounds that it is never useful.
27548
27549 Now that's a bit restrictive. In practice, the case in which
27550 @code{pragma Elaborate} is useful is when the caller knows that there
27551 are no transitive calls, or that the called unit contains all necessary
27552 transitive @code{pragma Elaborate} statements, and legacy code often
27553 contains such uses.
27554
27555 Strictly speaking the static mode in GNAT should ignore such pragmas,
27556 since there is no assurance at compile time that the necessary safety
27557 conditions are met. In practice, this would cause GNAT to be incompatible
27558 with correctly written Ada 83 code that had all necessary
27559 @code{pragma Elaborate} statements in place. Consequently, we made the
27560 decision that GNAT in its default mode will believe that if it encounters
27561 a @code{pragma Elaborate} then the programmer knows what they are doing,
27562 and it will trust that no elaboration errors can occur.
27563
27564 The result of this decision is two-fold. First to be safe using the
27565 static mode, you should remove all @code{pragma Elaborate} statements.
27566 Second, when fixing circularities in existing code, you can selectively
27567 use @code{pragma Elaborate} statements to convince the static mode of
27568 GNAT that it need not generate an implicit @code{pragma Elaborate_All}
27569 statement.
27570
27571 When using the static mode with @option{-gnatwl}, any use of
27572 @code{pragma Elaborate} will generate a warning about possible
27573 problems.
27574
27575 @node Elaboration Issues for Library Tasks
27576 @section Elaboration Issues for Library Tasks
27577 @cindex Library tasks, elaboration issues
27578 @cindex Elaboration of library tasks
27579
27580 @noindent
27581 In this section we examine special elaboration issues that arise for
27582 programs that declare library level tasks.
27583
27584 Generally the model of execution of an Ada program is that all units are
27585 elaborated, and then execution of the program starts. However, the
27586 declaration of library tasks definitely does not fit this model. The
27587 reason for this is that library tasks start as soon as they are declared
27588 (more precisely, as soon as the statement part of the enclosing package
27589 body is reached), that is to say before elaboration
27590 of the program is complete. This means that if such a task calls a
27591 subprogram, or an entry in another task, the callee may or may not be
27592 elaborated yet, and in the standard
27593 Reference Manual model of dynamic elaboration checks, you can even
27594 get timing dependent Program_Error exceptions, since there can be
27595 a race between the elaboration code and the task code.
27596
27597 The static model of elaboration in GNAT seeks to avoid all such
27598 dynamic behavior, by being conservative, and the conservative
27599 approach in this particular case is to assume that all the code
27600 in a task body is potentially executed at elaboration time if
27601 a task is declared at the library level.
27602
27603 This can definitely result in unexpected circularities. Consider
27604 the following example
27605
27606 @smallexample @c ada
27607 package Decls is
27608 task Lib_Task is
27609 entry Start;
27610 end Lib_Task;
27611
27612 type My_Int is new Integer;
27613
27614 function Ident (M : My_Int) return My_Int;
27615 end Decls;
27616
27617 with Utils;
27618 package body Decls is
27619 task body Lib_Task is
27620 begin
27621 accept Start;
27622 Utils.Put_Val (2);
27623 end Lib_Task;
27624
27625 function Ident (M : My_Int) return My_Int is
27626 begin
27627 return M;
27628 end Ident;
27629 end Decls;
27630
27631 with Decls;
27632 package Utils is
27633 procedure Put_Val (Arg : Decls.My_Int);
27634 end Utils;
27635
27636 with Text_IO;
27637 package body Utils is
27638 procedure Put_Val (Arg : Decls.My_Int) is
27639 begin
27640 Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
27641 end Put_Val;
27642 end Utils;
27643
27644 with Decls;
27645 procedure Main is
27646 begin
27647 Decls.Lib_Task.Start;
27648 end;
27649 @end smallexample
27650
27651 @noindent
27652 If the above example is compiled in the default static elaboration
27653 mode, then a circularity occurs. The circularity comes from the call
27654 @code{Utils.Put_Val} in the task body of @code{Decls.Lib_Task}. Since
27655 this call occurs in elaboration code, we need an implicit pragma
27656 @code{Elaborate_All} for @code{Utils}. This means that not only must
27657 the spec and body of @code{Utils} be elaborated before the body
27658 of @code{Decls}, but also the spec and body of any unit that is
27659 @code{with'ed} by the body of @code{Utils} must also be elaborated before
27660 the body of @code{Decls}. This is the transitive implication of
27661 pragma @code{Elaborate_All} and it makes sense, because in general
27662 the body of @code{Put_Val} might have a call to something in a
27663 @code{with'ed} unit.
27664
27665 In this case, the body of Utils (actually its spec) @code{with's}
27666 @code{Decls}. Unfortunately this means that the body of @code{Decls}
27667 must be elaborated before itself, in case there is a call from the
27668 body of @code{Utils}.
27669
27670 Here is the exact chain of events we are worrying about:
27671
27672 @enumerate
27673 @item
27674 In the body of @code{Decls} a call is made from within the body of a library
27675 task to a subprogram in the package @code{Utils}. Since this call may
27676 occur at elaboration time (given that the task is activated at elaboration
27677 time), we have to assume the worst, i.e., that the
27678 call does happen at elaboration time.
27679
27680 @item
27681 This means that the body and spec of @code{Util} must be elaborated before
27682 the body of @code{Decls} so that this call does not cause an access before
27683 elaboration.
27684
27685 @item
27686 Within the body of @code{Util}, specifically within the body of
27687 @code{Util.Put_Val} there may be calls to any unit @code{with}'ed
27688 by this package.
27689
27690 @item
27691 One such @code{with}'ed package is package @code{Decls}, so there
27692 might be a call to a subprogram in @code{Decls} in @code{Put_Val}.
27693 In fact there is such a call in this example, but we would have to
27694 assume that there was such a call even if it were not there, since
27695 we are not supposed to write the body of @code{Decls} knowing what
27696 is in the body of @code{Utils}; certainly in the case of the
27697 static elaboration model, the compiler does not know what is in
27698 other bodies and must assume the worst.
27699
27700 @item
27701 This means that the spec and body of @code{Decls} must also be
27702 elaborated before we elaborate the unit containing the call, but
27703 that unit is @code{Decls}! This means that the body of @code{Decls}
27704 must be elaborated before itself, and that's a circularity.
27705 @end enumerate
27706
27707 @noindent
27708 Indeed, if you add an explicit pragma @code{Elaborate_All} for @code{Utils} in
27709 the body of @code{Decls} you will get a true Ada Reference Manual
27710 circularity that makes the program illegal.
27711
27712 In practice, we have found that problems with the static model of
27713 elaboration in existing code often arise from library tasks, so
27714 we must address this particular situation.
27715
27716 Note that if we compile and run the program above, using the dynamic model of
27717 elaboration (that is to say use the @option{-gnatE} switch),
27718 then it compiles, binds,
27719 links, and runs, printing the expected result of 2. Therefore in some sense
27720 the circularity here is only apparent, and we need to capture
27721 the properties of this program that distinguish it from other library-level
27722 tasks that have real elaboration problems.
27723
27724 We have four possible answers to this question:
27725
27726 @itemize @bullet
27727
27728 @item
27729 Use the dynamic model of elaboration.
27730
27731 If we use the @option{-gnatE} switch, then as noted above, the program works.
27732 Why is this? If we examine the task body, it is apparent that the task cannot
27733 proceed past the
27734 @code{accept} statement until after elaboration has been completed, because
27735 the corresponding entry call comes from the main program, not earlier.
27736 This is why the dynamic model works here. But that's really giving
27737 up on a precise analysis, and we prefer to take this approach only if we cannot
27738 solve the
27739 problem in any other manner. So let us examine two ways to reorganize
27740 the program to avoid the potential elaboration problem.
27741
27742 @item
27743 Split library tasks into separate packages.
27744
27745 Write separate packages, so that library tasks are isolated from
27746 other declarations as much as possible. Let us look at a variation on
27747 the above program.
27748
27749 @smallexample @c ada
27750 package Decls1 is
27751 task Lib_Task is
27752 entry Start;
27753 end Lib_Task;
27754 end Decls1;
27755
27756 with Utils;
27757 package body Decls1 is
27758 task body Lib_Task is
27759 begin
27760 accept Start;
27761 Utils.Put_Val (2);
27762 end Lib_Task;
27763 end Decls1;
27764
27765 package Decls2 is
27766 type My_Int is new Integer;
27767 function Ident (M : My_Int) return My_Int;
27768 end Decls2;
27769
27770 with Utils;
27771 package body Decls2 is
27772 function Ident (M : My_Int) return My_Int is
27773 begin
27774 return M;
27775 end Ident;
27776 end Decls2;
27777
27778 with Decls2;
27779 package Utils is
27780 procedure Put_Val (Arg : Decls2.My_Int);
27781 end Utils;
27782
27783 with Text_IO;
27784 package body Utils is
27785 procedure Put_Val (Arg : Decls2.My_Int) is
27786 begin
27787 Text_IO.Put_Line (Decls2.My_Int'Image (Decls2.Ident (Arg)));
27788 end Put_Val;
27789 end Utils;
27790
27791 with Decls1;
27792 procedure Main is
27793 begin
27794 Decls1.Lib_Task.Start;
27795 end;
27796 @end smallexample
27797
27798 @noindent
27799 All we have done is to split @code{Decls} into two packages, one
27800 containing the library task, and one containing everything else. Now
27801 there is no cycle, and the program compiles, binds, links and executes
27802 using the default static model of elaboration.
27803
27804 @item
27805 Declare separate task types.
27806
27807 A significant part of the problem arises because of the use of the
27808 single task declaration form. This means that the elaboration of
27809 the task type, and the elaboration of the task itself (i.e.@: the
27810 creation of the task) happen at the same time. A good rule
27811 of style in Ada is to always create explicit task types. By
27812 following the additional step of placing task objects in separate
27813 packages from the task type declaration, many elaboration problems
27814 are avoided. Here is another modified example of the example program:
27815
27816 @smallexample @c ada
27817 package Decls is
27818 task type Lib_Task_Type is
27819 entry Start;
27820 end Lib_Task_Type;
27821
27822 type My_Int is new Integer;
27823
27824 function Ident (M : My_Int) return My_Int;
27825 end Decls;
27826
27827 with Utils;
27828 package body Decls is
27829 task body Lib_Task_Type is
27830 begin
27831 accept Start;
27832 Utils.Put_Val (2);
27833 end Lib_Task_Type;
27834
27835 function Ident (M : My_Int) return My_Int is
27836 begin
27837 return M;
27838 end Ident;
27839 end Decls;
27840
27841 with Decls;
27842 package Utils is
27843 procedure Put_Val (Arg : Decls.My_Int);
27844 end Utils;
27845
27846 with Text_IO;
27847 package body Utils is
27848 procedure Put_Val (Arg : Decls.My_Int) is
27849 begin
27850 Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
27851 end Put_Val;
27852 end Utils;
27853
27854 with Decls;
27855 package Declst is
27856 Lib_Task : Decls.Lib_Task_Type;
27857 end Declst;
27858
27859 with Declst;
27860 procedure Main is
27861 begin
27862 Declst.Lib_Task.Start;
27863 end;
27864 @end smallexample
27865
27866 @noindent
27867 What we have done here is to replace the @code{task} declaration in
27868 package @code{Decls} with a @code{task type} declaration. Then we
27869 introduce a separate package @code{Declst} to contain the actual
27870 task object. This separates the elaboration issues for
27871 the @code{task type}
27872 declaration, which causes no trouble, from the elaboration issues
27873 of the task object, which is also unproblematic, since it is now independent
27874 of the elaboration of @code{Utils}.
27875 This separation of concerns also corresponds to
27876 a generally sound engineering principle of separating declarations
27877 from instances. This version of the program also compiles, binds, links,
27878 and executes, generating the expected output.
27879
27880 @item
27881 Use No_Entry_Calls_In_Elaboration_Code restriction.
27882 @cindex No_Entry_Calls_In_Elaboration_Code
27883
27884 The previous two approaches described how a program can be restructured
27885 to avoid the special problems caused by library task bodies. in practice,
27886 however, such restructuring may be difficult to apply to existing legacy code,
27887 so we must consider solutions that do not require massive rewriting.
27888
27889 Let us consider more carefully why our original sample program works
27890 under the dynamic model of elaboration. The reason is that the code
27891 in the task body blocks immediately on the @code{accept}
27892 statement. Now of course there is nothing to prohibit elaboration
27893 code from making entry calls (for example from another library level task),
27894 so we cannot tell in isolation that
27895 the task will not execute the accept statement during elaboration.
27896
27897 However, in practice it is very unusual to see elaboration code
27898 make any entry calls, and the pattern of tasks starting
27899 at elaboration time and then immediately blocking on @code{accept} or
27900 @code{select} statements is very common. What this means is that
27901 the compiler is being too pessimistic when it analyzes the
27902 whole package body as though it might be executed at elaboration
27903 time.
27904
27905 If we know that the elaboration code contains no entry calls, (a very safe
27906 assumption most of the time, that could almost be made the default
27907 behavior), then we can compile all units of the program under control
27908 of the following configuration pragma:
27909
27910 @smallexample
27911 pragma Restrictions (No_Entry_Calls_In_Elaboration_Code);
27912 @end smallexample
27913
27914 @noindent
27915 This pragma can be placed in the @file{gnat.adc} file in the usual
27916 manner. If we take our original unmodified program and compile it
27917 in the presence of a @file{gnat.adc} containing the above pragma,
27918 then once again, we can compile, bind, link, and execute, obtaining
27919 the expected result. In the presence of this pragma, the compiler does
27920 not trace calls in a task body, that appear after the first @code{accept}
27921 or @code{select} statement, and therefore does not report a potential
27922 circularity in the original program.
27923
27924 The compiler will check to the extent it can that the above
27925 restriction is not violated, but it is not always possible to do a
27926 complete check at compile time, so it is important to use this
27927 pragma only if the stated restriction is in fact met, that is to say
27928 no task receives an entry call before elaboration of all units is completed.
27929
27930 @end itemize
27931
27932 @node Mixing Elaboration Models
27933 @section Mixing Elaboration Models
27934 @noindent
27935 So far, we have assumed that the entire program is either compiled
27936 using the dynamic model or static model, ensuring consistency. It
27937 is possible to mix the two models, but rules have to be followed
27938 if this mixing is done to ensure that elaboration checks are not
27939 omitted.
27940
27941 The basic rule is that @emph{a unit compiled with the static model cannot
27942 be @code{with'ed} by a unit compiled with the dynamic model}. The
27943 reason for this is that in the static model, a unit assumes that
27944 its clients guarantee to use (the equivalent of) pragma
27945 @code{Elaborate_All} so that no elaboration checks are required
27946 in inner subprograms, and this assumption is violated if the
27947 client is compiled with dynamic checks.
27948
27949 The precise rule is as follows. A unit that is compiled with dynamic
27950 checks can only @code{with} a unit that meets at least one of the
27951 following criteria:
27952
27953 @itemize @bullet
27954
27955 @item
27956 The @code{with'ed} unit is itself compiled with dynamic elaboration
27957 checks (that is with the @option{-gnatE} switch.
27958
27959 @item
27960 The @code{with'ed} unit is an internal GNAT implementation unit from
27961 the System, Interfaces, Ada, or GNAT hierarchies.
27962
27963 @item
27964 The @code{with'ed} unit has pragma Preelaborate or pragma Pure.
27965
27966 @item
27967 The @code{with'ing} unit (that is the client) has an explicit pragma
27968 @code{Elaborate_All} for the @code{with'ed} unit.
27969
27970 @end itemize
27971
27972 @noindent
27973 If this rule is violated, that is if a unit with dynamic elaboration
27974 checks @code{with's} a unit that does not meet one of the above four
27975 criteria, then the binder (@code{gnatbind}) will issue a warning
27976 similar to that in the following example:
27977
27978 @smallexample
27979 warning: "x.ads" has dynamic elaboration checks and with's
27980 warning: "y.ads" which has static elaboration checks
27981 @end smallexample
27982
27983 @noindent
27984 These warnings indicate that the rule has been violated, and that as a result
27985 elaboration checks may be missed in the resulting executable file.
27986 This warning may be suppressed using the @option{-ws} binder switch
27987 in the usual manner.
27988
27989 One useful application of this mixing rule is in the case of a subsystem
27990 which does not itself @code{with} units from the remainder of the
27991 application. In this case, the entire subsystem can be compiled with
27992 dynamic checks to resolve a circularity in the subsystem, while
27993 allowing the main application that uses this subsystem to be compiled
27994 using the more reliable default static model.
27995
27996 @node What to Do If the Default Elaboration Behavior Fails
27997 @section What to Do If the Default Elaboration Behavior Fails
27998
27999 @noindent
28000 If the binder cannot find an acceptable order, it outputs detailed
28001 diagnostics. For example:
28002 @smallexample
28003 @group
28004 @iftex
28005 @leftskip=0cm
28006 @end iftex
28007 error: elaboration circularity detected
28008 info: "proc (body)" must be elaborated before "pack (body)"
28009 info: reason: Elaborate_All probably needed in unit "pack (body)"
28010 info: recompile "pack (body)" with -gnatwl
28011 info: for full details
28012 info: "proc (body)"
28013 info: is needed by its spec:
28014 info: "proc (spec)"
28015 info: which is withed by:
28016 info: "pack (body)"
28017 info: "pack (body)" must be elaborated before "proc (body)"
28018 info: reason: pragma Elaborate in unit "proc (body)"
28019 @end group
28020
28021 @end smallexample
28022
28023 @noindent
28024 In this case we have a cycle that the binder cannot break. On the one
28025 hand, there is an explicit pragma Elaborate in @code{proc} for
28026 @code{pack}. This means that the body of @code{pack} must be elaborated
28027 before the body of @code{proc}. On the other hand, there is elaboration
28028 code in @code{pack} that calls a subprogram in @code{proc}. This means
28029 that for maximum safety, there should really be a pragma
28030 Elaborate_All in @code{pack} for @code{proc} which would require that
28031 the body of @code{proc} be elaborated before the body of
28032 @code{pack}. Clearly both requirements cannot be satisfied.
28033 Faced with a circularity of this kind, you have three different options.
28034
28035 @table @asis
28036 @item Fix the program
28037 The most desirable option from the point of view of long-term maintenance
28038 is to rearrange the program so that the elaboration problems are avoided.
28039 One useful technique is to place the elaboration code into separate
28040 child packages. Another is to move some of the initialization code to
28041 explicitly called subprograms, where the program controls the order
28042 of initialization explicitly. Although this is the most desirable option,
28043 it may be impractical and involve too much modification, especially in
28044 the case of complex legacy code.
28045
28046 @item Perform dynamic checks
28047 If the compilations are done using the
28048 @option{-gnatE}
28049 (dynamic elaboration check) switch, then GNAT behaves in a quite different
28050 manner. Dynamic checks are generated for all calls that could possibly result
28051 in raising an exception. With this switch, the compiler does not generate
28052 implicit @code{Elaborate} or @code{Elaborate_All} pragmas. The behavior then is
28053 exactly as specified in the @cite{Ada Reference Manual}.
28054 The binder will generate
28055 an executable program that may or may not raise @code{Program_Error}, and then
28056 it is the programmer's job to ensure that it does not raise an exception. Note
28057 that it is important to compile all units with the switch, it cannot be used
28058 selectively.
28059
28060 @item Suppress checks
28061 The drawback of dynamic checks is that they generate a
28062 significant overhead at run time, both in space and time. If you
28063 are absolutely sure that your program cannot raise any elaboration
28064 exceptions, and you still want to use the dynamic elaboration model,
28065 then you can use the configuration pragma
28066 @code{Suppress (Elaboration_Check)} to suppress all such checks. For
28067 example this pragma could be placed in the @file{gnat.adc} file.
28068
28069 @item Suppress checks selectively
28070 When you know that certain calls or instantiations in elaboration code cannot
28071 possibly lead to an elaboration error, and the binder nevertheless complains
28072 about implicit @code{Elaborate} and @code{Elaborate_All} pragmas that lead to
28073 elaboration circularities, it is possible to remove those warnings locally and
28074 obtain a program that will bind. Clearly this can be unsafe, and it is the
28075 responsibility of the programmer to make sure that the resulting program has no
28076 elaboration anomalies. The pragma @code{Suppress (Elaboration_Check)} can be
28077 used with different granularity to suppress warnings and break elaboration
28078 circularities:
28079
28080 @itemize @bullet
28081 @item
28082 Place the pragma that names the called subprogram in the declarative part
28083 that contains the call.
28084
28085 @item
28086 Place the pragma in the declarative part, without naming an entity. This
28087 disables warnings on all calls in the corresponding declarative region.
28088
28089 @item
28090 Place the pragma in the package spec that declares the called subprogram,
28091 and name the subprogram. This disables warnings on all elaboration calls to
28092 that subprogram.
28093
28094 @item
28095 Place the pragma in the package spec that declares the called subprogram,
28096 without naming any entity. This disables warnings on all elaboration calls to
28097 all subprograms declared in this spec.
28098
28099 @item Use Pragma Elaborate
28100 As previously described in section @xref{Treatment of Pragma Elaborate},
28101 GNAT in static mode assumes that a @code{pragma} Elaborate indicates correctly
28102 that no elaboration checks are required on calls to the designated unit.
28103 There may be cases in which the caller knows that no transitive calls
28104 can occur, so that a @code{pragma Elaborate} will be sufficient in a
28105 case where @code{pragma Elaborate_All} would cause a circularity.
28106 @end itemize
28107
28108 @noindent
28109 These five cases are listed in order of decreasing safety, and therefore
28110 require increasing programmer care in their application. Consider the
28111 following program:
28112
28113 @smallexample @c adanocomment
28114 package Pack1 is
28115 function F1 return Integer;
28116 X1 : Integer;
28117 end Pack1;
28118
28119 package Pack2 is
28120 function F2 return Integer;
28121 function Pure (x : integer) return integer;
28122 -- pragma Suppress (Elaboration_Check, On => Pure); -- (3)
28123 -- pragma Suppress (Elaboration_Check); -- (4)
28124 end Pack2;
28125
28126 with Pack2;
28127 package body Pack1 is
28128 function F1 return Integer is
28129 begin
28130 return 100;
28131 end F1;
28132 Val : integer := Pack2.Pure (11); -- Elab. call (1)
28133 begin
28134 declare
28135 -- pragma Suppress(Elaboration_Check, Pack2.F2); -- (1)
28136 -- pragma Suppress(Elaboration_Check); -- (2)
28137 begin
28138 X1 := Pack2.F2 + 1; -- Elab. call (2)
28139 end;
28140 end Pack1;
28141
28142 with Pack1;
28143 package body Pack2 is
28144 function F2 return Integer is
28145 begin
28146 return Pack1.F1;
28147 end F2;
28148 function Pure (x : integer) return integer is
28149 begin
28150 return x ** 3 - 3 * x;
28151 end;
28152 end Pack2;
28153
28154 with Pack1, Ada.Text_IO;
28155 procedure Proc3 is
28156 begin
28157 Ada.Text_IO.Put_Line(Pack1.X1'Img); -- 101
28158 end Proc3;
28159 @end smallexample
28160 In the absence of any pragmas, an attempt to bind this program produces
28161 the following diagnostics:
28162 @smallexample
28163 @group
28164 @iftex
28165 @leftskip=.5cm
28166 @end iftex
28167 error: elaboration circularity detected
28168 info: "pack1 (body)" must be elaborated before "pack1 (body)"
28169 info: reason: Elaborate_All probably needed in unit "pack1 (body)"
28170 info: recompile "pack1 (body)" with -gnatwl for full details
28171 info: "pack1 (body)"
28172 info: must be elaborated along with its spec:
28173 info: "pack1 (spec)"
28174 info: which is withed by:
28175 info: "pack2 (body)"
28176 info: which must be elaborated along with its spec:
28177 info: "pack2 (spec)"
28178 info: which is withed by:
28179 info: "pack1 (body)"
28180 @end group
28181 @end smallexample
28182 The sources of the circularity are the two calls to @code{Pack2.Pure} and
28183 @code{Pack2.F2} in the body of @code{Pack1}. We can see that the call to
28184 F2 is safe, even though F2 calls F1, because the call appears after the
28185 elaboration of the body of F1. Therefore the pragma (1) is safe, and will
28186 remove the warning on the call. It is also possible to use pragma (2)
28187 because there are no other potentially unsafe calls in the block.
28188
28189 @noindent
28190 The call to @code{Pure} is safe because this function does not depend on the
28191 state of @code{Pack2}. Therefore any call to this function is safe, and it
28192 is correct to place pragma (3) in the corresponding package spec.
28193
28194 @noindent
28195 Finally, we could place pragma (4) in the spec of @code{Pack2} to disable
28196 warnings on all calls to functions declared therein. Note that this is not
28197 necessarily safe, and requires more detailed examination of the subprogram
28198 bodies involved. In particular, a call to @code{F2} requires that @code{F1}
28199 be already elaborated.
28200 @end table
28201
28202 @noindent
28203 It is hard to generalize on which of these four approaches should be
28204 taken. Obviously if it is possible to fix the program so that the default
28205 treatment works, this is preferable, but this may not always be practical.
28206 It is certainly simple enough to use
28207 @option{-gnatE}
28208 but the danger in this case is that, even if the GNAT binder
28209 finds a correct elaboration order, it may not always do so,
28210 and certainly a binder from another Ada compiler might not. A
28211 combination of testing and analysis (for which the warnings generated
28212 with the
28213 @option{-gnatwl}
28214 switch can be useful) must be used to ensure that the program is free
28215 of errors. One switch that is useful in this testing is the
28216 @option{^-p (pessimistic elaboration order)^/PESSIMISTIC_ELABORATION_ORDER^}
28217 switch for
28218 @code{gnatbind}.
28219 Normally the binder tries to find an order that has the best chance
28220 of avoiding elaboration problems. However, if this switch is used, the binder
28221 plays a devil's advocate role, and tries to choose the order that
28222 has the best chance of failing. If your program works even with this
28223 switch, then it has a better chance of being error free, but this is still
28224 not a guarantee.
28225
28226 For an example of this approach in action, consider the C-tests (executable
28227 tests) from the ACVC suite. If these are compiled and run with the default
28228 treatment, then all but one of them succeed without generating any error
28229 diagnostics from the binder. However, there is one test that fails, and
28230 this is not surprising, because the whole point of this test is to ensure
28231 that the compiler can handle cases where it is impossible to determine
28232 a correct order statically, and it checks that an exception is indeed
28233 raised at run time.
28234
28235 This one test must be compiled and run using the
28236 @option{-gnatE}
28237 switch, and then it passes. Alternatively, the entire suite can
28238 be run using this switch. It is never wrong to run with the dynamic
28239 elaboration switch if your code is correct, and we assume that the
28240 C-tests are indeed correct (it is less efficient, but efficiency is
28241 not a factor in running the ACVC tests.)
28242
28243 @node Elaboration for Access-to-Subprogram Values
28244 @section Elaboration for Access-to-Subprogram Values
28245 @cindex Access-to-subprogram
28246
28247 @noindent
28248 Access-to-subprogram types (introduced in Ada 95) complicate
28249 the handling of elaboration. The trouble is that it becomes
28250 impossible to tell at compile time which procedure
28251 is being called. This means that it is not possible for the binder
28252 to analyze the elaboration requirements in this case.
28253
28254 If at the point at which the access value is created
28255 (i.e., the evaluation of @code{P'Access} for a subprogram @code{P}),
28256 the body of the subprogram is
28257 known to have been elaborated, then the access value is safe, and its use
28258 does not require a check. This may be achieved by appropriate arrangement
28259 of the order of declarations if the subprogram is in the current unit,
28260 or, if the subprogram is in another unit, by using pragma
28261 @code{Pure}, @code{Preelaborate}, or @code{Elaborate_Body}
28262 on the referenced unit.
28263
28264 If the referenced body is not known to have been elaborated at the point
28265 the access value is created, then any use of the access value must do a
28266 dynamic check, and this dynamic check will fail and raise a
28267 @code{Program_Error} exception if the body has not been elaborated yet.
28268 GNAT will generate the necessary checks, and in addition, if the
28269 @option{-gnatwl}
28270 switch is set, will generate warnings that such checks are required.
28271
28272 The use of dynamic dispatching for tagged types similarly generates
28273 a requirement for dynamic checks, and premature calls to any primitive
28274 operation of a tagged type before the body of the operation has been
28275 elaborated, will result in the raising of @code{Program_Error}.
28276
28277 @node Summary of Procedures for Elaboration Control
28278 @section Summary of Procedures for Elaboration Control
28279 @cindex Elaboration control
28280
28281 @noindent
28282 First, compile your program with the default options, using none of
28283 the special elaboration control switches. If the binder successfully
28284 binds your program, then you can be confident that, apart from issues
28285 raised by the use of access-to-subprogram types and dynamic dispatching,
28286 the program is free of elaboration errors. If it is important that the
28287 program be portable, then use the
28288 @option{-gnatwl}
28289 switch to generate warnings about missing @code{Elaborate} or
28290 @code{Elaborate_All} pragmas, and supply the missing pragmas.
28291
28292 If the program fails to bind using the default static elaboration
28293 handling, then you can fix the program to eliminate the binder
28294 message, or recompile the entire program with the
28295 @option{-gnatE} switch to generate dynamic elaboration checks,
28296 and, if you are sure there really are no elaboration problems,
28297 use a global pragma @code{Suppress (Elaboration_Check)}.
28298
28299 @node Other Elaboration Order Considerations
28300 @section Other Elaboration Order Considerations
28301 @noindent
28302 This section has been entirely concerned with the issue of finding a valid
28303 elaboration order, as defined by the Ada Reference Manual. In a case
28304 where several elaboration orders are valid, the task is to find one
28305 of the possible valid elaboration orders (and the static model in GNAT
28306 will ensure that this is achieved).
28307
28308 The purpose of the elaboration rules in the Ada Reference Manual is to
28309 make sure that no entity is accessed before it has been elaborated. For
28310 a subprogram, this means that the spec and body must have been elaborated
28311 before the subprogram is called. For an object, this means that the object
28312 must have been elaborated before its value is read or written. A violation
28313 of either of these two requirements is an access before elaboration order,
28314 and this section has been all about avoiding such errors.
28315
28316 In the case where more than one order of elaboration is possible, in the
28317 sense that access before elaboration errors are avoided, then any one of
28318 the orders is ``correct'' in the sense that it meets the requirements of
28319 the Ada Reference Manual, and no such error occurs.
28320
28321 However, it may be the case for a given program, that there are
28322 constraints on the order of elaboration that come not from consideration
28323 of avoiding elaboration errors, but rather from extra-lingual logic
28324 requirements. Consider this example:
28325
28326 @smallexample @c ada
28327 with Init_Constants;
28328 package Constants is
28329 X : Integer := 0;
28330 Y : Integer := 0;
28331 end Constants;
28332
28333 package Init_Constants is
28334 procedure P; -- require a body
28335 end Init_Constants;
28336
28337 with Constants;
28338 package body Init_Constants is
28339 procedure P is begin null; end;
28340 begin
28341 Constants.X := 3;
28342 Constants.Y := 4;
28343 end Init_Constants;
28344
28345 with Constants;
28346 package Calc is
28347 Z : Integer := Constants.X + Constants.Y;
28348 end Calc;
28349
28350 with Calc;
28351 with Text_IO; use Text_IO;
28352 procedure Main is
28353 begin
28354 Put_Line (Calc.Z'Img);
28355 end Main;
28356 @end smallexample
28357
28358 @noindent
28359 In this example, there is more than one valid order of elaboration. For
28360 example both the following are correct orders:
28361
28362 @smallexample
28363 Init_Constants spec
28364 Constants spec
28365 Calc spec
28366 Init_Constants body
28367 Main body
28368
28369 and
28370
28371 Init_Constants spec
28372 Init_Constants body
28373 Constants spec
28374 Calc spec
28375 Main body
28376 @end smallexample
28377
28378 @noindent
28379 There is no language rule to prefer one or the other, both are correct
28380 from an order of elaboration point of view. But the programmatic effects
28381 of the two orders are very different. In the first, the elaboration routine
28382 of @code{Calc} initializes @code{Z} to zero, and then the main program
28383 runs with this value of zero. But in the second order, the elaboration
28384 routine of @code{Calc} runs after the body of Init_Constants has set
28385 @code{X} and @code{Y} and thus @code{Z} is set to 7 before @code{Main}
28386 runs.
28387
28388 One could perhaps by applying pretty clever non-artificial intelligence
28389 to the situation guess that it is more likely that the second order of
28390 elaboration is the one desired, but there is no formal linguistic reason
28391 to prefer one over the other. In fact in this particular case, GNAT will
28392 prefer the second order, because of the rule that bodies are elaborated
28393 as soon as possible, but it's just luck that this is what was wanted
28394 (if indeed the second order was preferred).
28395
28396 If the program cares about the order of elaboration routines in a case like
28397 this, it is important to specify the order required. In this particular
28398 case, that could have been achieved by adding to the spec of Calc:
28399
28400 @smallexample @c ada
28401 pragma Elaborate_All (Constants);
28402 @end smallexample
28403
28404 @noindent
28405 which requires that the body (if any) and spec of @code{Constants},
28406 as well as the body and spec of any unit @code{with}'ed by
28407 @code{Constants} be elaborated before @code{Calc} is elaborated.
28408
28409 Clearly no automatic method can always guess which alternative you require,
28410 and if you are working with legacy code that had constraints of this kind
28411 which were not properly specified by adding @code{Elaborate} or
28412 @code{Elaborate_All} pragmas, then indeed it is possible that two different
28413 compilers can choose different orders.
28414
28415 However, GNAT does attempt to diagnose the common situation where there
28416 are uninitialized variables in the visible part of a package spec, and the
28417 corresponding package body has an elaboration block that directly or
28418 indirectly initialized one or more of these variables. This is the situation
28419 in which a pragma Elaborate_Body is usually desirable, and GNAT will generate
28420 a warning that suggests this addition if it detects this situation.
28421
28422 The @code{gnatbind}
28423 @option{^-p^/PESSIMISTIC_ELABORATION^} switch may be useful in smoking
28424 out problems. This switch causes bodies to be elaborated as late as possible
28425 instead of as early as possible. In the example above, it would have forced
28426 the choice of the first elaboration order. If you get different results
28427 when using this switch, and particularly if one set of results is right,
28428 and one is wrong as far as you are concerned, it shows that you have some
28429 missing @code{Elaborate} pragmas. For the example above, we have the
28430 following output:
28431
28432 @smallexample
28433 gnatmake -f -q main
28434 main
28435 7
28436 gnatmake -f -q main -bargs -p
28437 main
28438 0
28439 @end smallexample
28440
28441 @noindent
28442 It is of course quite unlikely that both these results are correct, so
28443 it is up to you in a case like this to investigate the source of the
28444 difference, by looking at the two elaboration orders that are chosen,
28445 and figuring out which is correct, and then adding the necessary
28446 @code{Elaborate} or @code{Elaborate_All} pragmas to ensure the desired order.
28447
28448
28449
28450 @c *******************************
28451 @node Conditional Compilation
28452 @appendix Conditional Compilation
28453 @c *******************************
28454 @cindex Conditional compilation
28455
28456 @noindent
28457 It is often necessary to arrange for a single source program
28458 to serve multiple purposes, where it is compiled in different
28459 ways to achieve these different goals. Some examples of the
28460 need for this feature are
28461
28462 @itemize @bullet
28463 @item Adapting a program to a different hardware environment
28464 @item Adapting a program to a different target architecture
28465 @item Turning debugging features on and off
28466 @item Arranging for a program to compile with different compilers
28467 @end itemize
28468
28469 @noindent
28470 In C, or C++, the typical approach would be to use the preprocessor
28471 that is defined as part of the language. The Ada language does not
28472 contain such a feature. This is not an oversight, but rather a very
28473 deliberate design decision, based on the experience that overuse of
28474 the preprocessing features in C and C++ can result in programs that
28475 are extremely difficult to maintain. For example, if we have ten
28476 switches that can be on or off, this means that there are a thousand
28477 separate programs, any one of which might not even be syntactically
28478 correct, and even if syntactically correct, the resulting program
28479 might not work correctly. Testing all combinations can quickly become
28480 impossible.
28481
28482 Nevertheless, the need to tailor programs certainly exists, and in
28483 this Appendix we will discuss how this can
28484 be achieved using Ada in general, and GNAT in particular.
28485
28486 @menu
28487 * Use of Boolean Constants::
28488 * Debugging - A Special Case::
28489 * Conditionalizing Declarations::
28490 * Use of Alternative Implementations::
28491 * Preprocessing::
28492 @end menu
28493
28494 @node Use of Boolean Constants
28495 @section Use of Boolean Constants
28496
28497 @noindent
28498 In the case where the difference is simply which code
28499 sequence is executed, the cleanest solution is to use Boolean
28500 constants to control which code is executed.
28501
28502 @smallexample @c ada
28503 @group
28504 FP_Initialize_Required : constant Boolean := True;
28505 @dots{}
28506 if FP_Initialize_Required then
28507 @dots{}
28508 end if;
28509 @end group
28510 @end smallexample
28511
28512 @noindent
28513 Not only will the code inside the @code{if} statement not be executed if
28514 the constant Boolean is @code{False}, but it will also be completely
28515 deleted from the program.
28516 However, the code is only deleted after the @code{if} statement
28517 has been checked for syntactic and semantic correctness.
28518 (In contrast, with preprocessors the code is deleted before the
28519 compiler ever gets to see it, so it is not checked until the switch
28520 is turned on.)
28521 @cindex Preprocessors (contrasted with conditional compilation)
28522
28523 Typically the Boolean constants will be in a separate package,
28524 something like:
28525
28526 @smallexample @c ada
28527 @group
28528 package Config is
28529 FP_Initialize_Required : constant Boolean := True;
28530 Reset_Available : constant Boolean := False;
28531 @dots{}
28532 end Config;
28533 @end group
28534 @end smallexample
28535
28536 @noindent
28537 The @code{Config} package exists in multiple forms for the various targets,
28538 with an appropriate script selecting the version of @code{Config} needed.
28539 Then any other unit requiring conditional compilation can do a @code{with}
28540 of @code{Config} to make the constants visible.
28541
28542
28543 @node Debugging - A Special Case
28544 @section Debugging - A Special Case
28545
28546 @noindent
28547 A common use of conditional code is to execute statements (for example
28548 dynamic checks, or output of intermediate results) under control of a
28549 debug switch, so that the debugging behavior can be turned on and off.
28550 This can be done using a Boolean constant to control whether the code
28551 is active:
28552
28553 @smallexample @c ada
28554 @group
28555 if Debugging then
28556 Put_Line ("got to the first stage!");
28557 end if;
28558 @end group
28559 @end smallexample
28560
28561 @noindent
28562 or
28563
28564 @smallexample @c ada
28565 @group
28566 if Debugging and then Temperature > 999.0 then
28567 raise Temperature_Crazy;
28568 end if;
28569 @end group
28570 @end smallexample
28571
28572 @noindent
28573 Since this is a common case, there are special features to deal with
28574 this in a convenient manner. For the case of tests, Ada 2005 has added
28575 a pragma @code{Assert} that can be used for such tests. This pragma is modeled
28576 @cindex pragma @code{Assert}
28577 on the @code{Assert} pragma that has always been available in GNAT, so this
28578 feature may be used with GNAT even if you are not using Ada 2005 features.
28579 The use of pragma @code{Assert} is described in
28580 @ref{Pragma Assert,,, gnat_rm, GNAT Reference Manual}, but as an
28581 example, the last test could be written:
28582
28583 @smallexample @c ada
28584 pragma Assert (Temperature <= 999.0, "Temperature Crazy");
28585 @end smallexample
28586
28587 @noindent
28588 or simply
28589
28590 @smallexample @c ada
28591 pragma Assert (Temperature <= 999.0);
28592 @end smallexample
28593
28594 @noindent
28595 In both cases, if assertions are active and the temperature is excessive,
28596 the exception @code{Assert_Failure} will be raised, with the given string in
28597 the first case or a string indicating the location of the pragma in the second
28598 case used as the exception message.
28599
28600 You can turn assertions on and off by using the @code{Assertion_Policy}
28601 pragma.
28602 @cindex pragma @code{Assertion_Policy}
28603 This is an Ada 2005 pragma which is implemented in all modes by
28604 GNAT, but only in the latest versions of GNAT which include Ada 2005
28605 capability. Alternatively, you can use the @option{-gnata} switch
28606 @cindex @option{-gnata} switch
28607 to enable assertions from the command line (this is recognized by all versions
28608 of GNAT).
28609
28610 For the example above with the @code{Put_Line}, the GNAT-specific pragma
28611 @code{Debug} can be used:
28612 @cindex pragma @code{Debug}
28613
28614 @smallexample @c ada
28615 pragma Debug (Put_Line ("got to the first stage!"));
28616 @end smallexample
28617
28618 @noindent
28619 If debug pragmas are enabled, the argument, which must be of the form of
28620 a procedure call, is executed (in this case, @code{Put_Line} will be called).
28621 Only one call can be present, but of course a special debugging procedure
28622 containing any code you like can be included in the program and then
28623 called in a pragma @code{Debug} argument as needed.
28624
28625 One advantage of pragma @code{Debug} over the @code{if Debugging then}
28626 construct is that pragma @code{Debug} can appear in declarative contexts,
28627 such as at the very beginning of a procedure, before local declarations have
28628 been elaborated.
28629
28630 Debug pragmas are enabled using either the @option{-gnata} switch that also
28631 controls assertions, or with a separate Debug_Policy pragma.
28632 @cindex pragma @code{Debug_Policy}
28633 The latter pragma is new in the Ada 2005 versions of GNAT (but it can be used
28634 in Ada 95 and Ada 83 programs as well), and is analogous to
28635 pragma @code{Assertion_Policy} to control assertions.
28636
28637 @code{Assertion_Policy} and @code{Debug_Policy} are configuration pragmas,
28638 and thus they can appear in @file{gnat.adc} if you are not using a
28639 project file, or in the file designated to contain configuration pragmas
28640 in a project file.
28641 They then apply to all subsequent compilations. In practice the use of
28642 the @option{-gnata} switch is often the most convenient method of controlling
28643 the status of these pragmas.
28644
28645 Note that a pragma is not a statement, so in contexts where a statement
28646 sequence is required, you can't just write a pragma on its own. You have
28647 to add a @code{null} statement.
28648
28649 @smallexample @c ada
28650 @group
28651 if @dots{} then
28652 @dots{} -- some statements
28653 else
28654 pragma Assert (Num_Cases < 10);
28655 null;
28656 end if;
28657 @end group
28658 @end smallexample
28659
28660
28661 @node Conditionalizing Declarations
28662 @section Conditionalizing Declarations
28663
28664 @noindent
28665 In some cases, it may be necessary to conditionalize declarations to meet
28666 different requirements. For example we might want a bit string whose length
28667 is set to meet some hardware message requirement.
28668
28669 In some cases, it may be possible to do this using declare blocks controlled
28670 by conditional constants:
28671
28672 @smallexample @c ada
28673 @group
28674 if Small_Machine then
28675 declare
28676 X : Bit_String (1 .. 10);
28677 begin
28678 @dots{}
28679 end;
28680 else
28681 declare
28682 X : Large_Bit_String (1 .. 1000);
28683 begin
28684 @dots{}
28685 end;
28686 end if;
28687 @end group
28688 @end smallexample
28689
28690 @noindent
28691 Note that in this approach, both declarations are analyzed by the
28692 compiler so this can only be used where both declarations are legal,
28693 even though one of them will not be used.
28694
28695 Another approach is to define integer constants, e.g.@: @code{Bits_Per_Word}, or
28696 Boolean constants, e.g.@: @code{Little_Endian}, and then write declarations
28697 that are parameterized by these constants. For example
28698
28699 @smallexample @c ada
28700 @group
28701 for Rec use
28702 Field1 at 0 range Boolean'Pos (Little_Endian) * 10 .. Bits_Per_Word;
28703 end record;
28704 @end group
28705 @end smallexample
28706
28707 @noindent
28708 If @code{Bits_Per_Word} is set to 32, this generates either
28709
28710 @smallexample @c ada
28711 @group
28712 for Rec use
28713 Field1 at 0 range 0 .. 32;
28714 end record;
28715 @end group
28716 @end smallexample
28717
28718 @noindent
28719 for the big endian case, or
28720
28721 @smallexample @c ada
28722 @group
28723 for Rec use record
28724 Field1 at 0 range 10 .. 32;
28725 end record;
28726 @end group
28727 @end smallexample
28728
28729 @noindent
28730 for the little endian case. Since a powerful subset of Ada expression
28731 notation is usable for creating static constants, clever use of this
28732 feature can often solve quite difficult problems in conditionalizing
28733 compilation (note incidentally that in Ada 95, the little endian
28734 constant was introduced as @code{System.Default_Bit_Order}, so you do not
28735 need to define this one yourself).
28736
28737
28738 @node Use of Alternative Implementations
28739 @section Use of Alternative Implementations
28740
28741 @noindent
28742 In some cases, none of the approaches described above are adequate. This
28743 can occur for example if the set of declarations required is radically
28744 different for two different configurations.
28745
28746 In this situation, the official Ada way of dealing with conditionalizing
28747 such code is to write separate units for the different cases. As long as
28748 this does not result in excessive duplication of code, this can be done
28749 without creating maintenance problems. The approach is to share common
28750 code as far as possible, and then isolate the code and declarations
28751 that are different. Subunits are often a convenient method for breaking
28752 out a piece of a unit that is to be conditionalized, with separate files
28753 for different versions of the subunit for different targets, where the
28754 build script selects the right one to give to the compiler.
28755 @cindex Subunits (and conditional compilation)
28756
28757 As an example, consider a situation where a new feature in Ada 2005
28758 allows something to be done in a really nice way. But your code must be able
28759 to compile with an Ada 95 compiler. Conceptually you want to say:
28760
28761 @smallexample @c ada
28762 @group
28763 if Ada_2005 then
28764 @dots{} neat Ada 2005 code
28765 else
28766 @dots{} not quite as neat Ada 95 code
28767 end if;
28768 @end group
28769 @end smallexample
28770
28771 @noindent
28772 where @code{Ada_2005} is a Boolean constant.
28773
28774 But this won't work when @code{Ada_2005} is set to @code{False},
28775 since the @code{then} clause will be illegal for an Ada 95 compiler.
28776 (Recall that although such unreachable code would eventually be deleted
28777 by the compiler, it still needs to be legal. If it uses features
28778 introduced in Ada 2005, it will be illegal in Ada 95.)
28779
28780 So instead we write
28781
28782 @smallexample @c ada
28783 procedure Insert is separate;
28784 @end smallexample
28785
28786 @noindent
28787 Then we have two files for the subunit @code{Insert}, with the two sets of
28788 code.
28789 If the package containing this is called @code{File_Queries}, then we might
28790 have two files
28791
28792 @itemize @bullet
28793 @item @file{file_queries-insert-2005.adb}
28794 @item @file{file_queries-insert-95.adb}
28795 @end itemize
28796
28797 @noindent
28798 and the build script renames the appropriate file to
28799
28800 @smallexample
28801 file_queries-insert.adb
28802 @end smallexample
28803
28804 @noindent
28805 and then carries out the compilation.
28806
28807 This can also be done with project files' naming schemes. For example:
28808
28809 @smallexample @c project
28810 For Body ("File_Queries.Insert") use "file_queries-insert-2005.ada";
28811 @end smallexample
28812
28813 @noindent
28814 Note also that with project files it is desirable to use a different extension
28815 than @file{ads} / @file{adb} for alternative versions. Otherwise a naming
28816 conflict may arise through another commonly used feature: to declare as part
28817 of the project a set of directories containing all the sources obeying the
28818 default naming scheme.
28819
28820 The use of alternative units is certainly feasible in all situations,
28821 and for example the Ada part of the GNAT run-time is conditionalized
28822 based on the target architecture using this approach. As a specific example,
28823 consider the implementation of the AST feature in VMS. There is one
28824 spec:
28825
28826 @smallexample
28827 s-asthan.ads
28828 @end smallexample
28829
28830 @noindent
28831 which is the same for all architectures, and three bodies:
28832
28833 @table @file
28834 @item s-asthan.adb
28835 used for all non-VMS operating systems
28836 @item s-asthan-vms-alpha.adb
28837 used for VMS on the Alpha
28838 @item s-asthan-vms-ia64.adb
28839 used for VMS on the ia64
28840 @end table
28841
28842 @noindent
28843 The dummy version @file{s-asthan.adb} simply raises exceptions noting that
28844 this operating system feature is not available, and the two remaining
28845 versions interface with the corresponding versions of VMS to provide
28846 VMS-compatible AST handling. The GNAT build script knows the architecture
28847 and operating system, and automatically selects the right version,
28848 renaming it if necessary to @file{s-asthan.adb} before the run-time build.
28849
28850 Another style for arranging alternative implementations is through Ada's
28851 access-to-subprogram facility.
28852 In case some functionality is to be conditionally included,
28853 you can declare an access-to-procedure variable @code{Ref} that is initialized
28854 to designate a ``do nothing'' procedure, and then invoke @code{Ref.all}
28855 when appropriate.
28856 In some library package, set @code{Ref} to @code{Proc'Access} for some
28857 procedure @code{Proc} that performs the relevant processing.
28858 The initialization only occurs if the library package is included in the
28859 program.
28860 The same idea can also be implemented using tagged types and dispatching
28861 calls.
28862
28863
28864 @node Preprocessing
28865 @section Preprocessing
28866 @cindex Preprocessing
28867
28868 @noindent
28869 Although it is quite possible to conditionalize code without the use of
28870 C-style preprocessing, as described earlier in this section, it is
28871 nevertheless convenient in some cases to use the C approach. Moreover,
28872 older Ada compilers have often provided some preprocessing capability,
28873 so legacy code may depend on this approach, even though it is not
28874 standard.
28875
28876 To accommodate such use, GNAT provides a preprocessor (modeled to a large
28877 extent on the various preprocessors that have been used
28878 with legacy code on other compilers, to enable easier transition).
28879
28880 The preprocessor may be used in two separate modes. It can be used quite
28881 separately from the compiler, to generate a separate output source file
28882 that is then fed to the compiler as a separate step. This is the
28883 @code{gnatprep} utility, whose use is fully described in
28884 @ref{Preprocessing Using gnatprep}.
28885 @cindex @code{gnatprep}
28886
28887 The preprocessing language allows such constructs as
28888
28889 @smallexample
28890 @group
28891 #if DEBUG or PRIORITY > 4 then
28892 bunch of declarations
28893 #else
28894 completely different bunch of declarations
28895 #end if;
28896 @end group
28897 @end smallexample
28898
28899 @noindent
28900 The values of the symbols @code{DEBUG} and @code{PRIORITY} can be
28901 defined either on the command line or in a separate file.
28902
28903 The other way of running the preprocessor is even closer to the C style and
28904 often more convenient. In this approach the preprocessing is integrated into
28905 the compilation process. The compiler is fed the preprocessor input which
28906 includes @code{#if} lines etc, and then the compiler carries out the
28907 preprocessing internally and processes the resulting output.
28908 For more details on this approach, see @ref{Integrated Preprocessing}.
28909
28910
28911 @c *******************************
28912 @node Inline Assembler
28913 @appendix Inline Assembler
28914 @c *******************************
28915
28916 @noindent
28917 If you need to write low-level software that interacts directly
28918 with the hardware, Ada provides two ways to incorporate assembly
28919 language code into your program. First, you can import and invoke
28920 external routines written in assembly language, an Ada feature fully
28921 supported by GNAT@. However, for small sections of code it may be simpler
28922 or more efficient to include assembly language statements directly
28923 in your Ada source program, using the facilities of the implementation-defined
28924 package @code{System.Machine_Code}, which incorporates the gcc
28925 Inline Assembler. The Inline Assembler approach offers a number of advantages,
28926 including the following:
28927
28928 @itemize @bullet
28929 @item No need to use non-Ada tools
28930 @item Consistent interface over different targets
28931 @item Automatic usage of the proper calling conventions
28932 @item Access to Ada constants and variables
28933 @item Definition of intrinsic routines
28934 @item Possibility of inlining a subprogram comprising assembler code
28935 @item Code optimizer can take Inline Assembler code into account
28936 @end itemize
28937
28938 This chapter presents a series of examples to show you how to use
28939 the Inline Assembler. Although it focuses on the Intel x86,
28940 the general approach applies also to other processors.
28941 It is assumed that you are familiar with Ada
28942 and with assembly language programming.
28943
28944 @menu
28945 * Basic Assembler Syntax::
28946 * A Simple Example of Inline Assembler::
28947 * Output Variables in Inline Assembler::
28948 * Input Variables in Inline Assembler::
28949 * Inlining Inline Assembler Code::
28950 * Other Asm Functionality::
28951 @end menu
28952
28953 @c ---------------------------------------------------------------------------
28954 @node Basic Assembler Syntax
28955 @section Basic Assembler Syntax
28956
28957 @noindent
28958 The assembler used by GNAT and gcc is based not on the Intel assembly
28959 language, but rather on a language that descends from the AT&T Unix
28960 assembler @emph{as} (and which is often referred to as ``AT&T syntax'').
28961 The following table summarizes the main features of @emph{as} syntax
28962 and points out the differences from the Intel conventions.
28963 See the gcc @emph{as} and @emph{gas} (an @emph{as} macro
28964 pre-processor) documentation for further information.
28965
28966 @table @asis
28967 @item Register names
28968 gcc / @emph{as}: Prefix with ``%''; for example @code{%eax}
28969 @*
28970 Intel: No extra punctuation; for example @code{eax}
28971
28972 @item Immediate operand
28973 gcc / @emph{as}: Prefix with ``$''; for example @code{$4}
28974 @*
28975 Intel: No extra punctuation; for example @code{4}
28976
28977 @item Address
28978 gcc / @emph{as}: Prefix with ``$''; for example @code{$loc}
28979 @*
28980 Intel: No extra punctuation; for example @code{loc}
28981
28982 @item Memory contents
28983 gcc / @emph{as}: No extra punctuation; for example @code{loc}
28984 @*
28985 Intel: Square brackets; for example @code{[loc]}
28986
28987 @item Register contents
28988 gcc / @emph{as}: Parentheses; for example @code{(%eax)}
28989 @*
28990 Intel: Square brackets; for example @code{[eax]}
28991
28992 @item Hexadecimal numbers
28993 gcc / @emph{as}: Leading ``0x'' (C language syntax); for example @code{0xA0}
28994 @*
28995 Intel: Trailing ``h''; for example @code{A0h}
28996
28997 @item Operand size
28998 gcc / @emph{as}: Explicit in op code; for example @code{movw} to move
28999 a 16-bit word
29000 @*
29001 Intel: Implicit, deduced by assembler; for example @code{mov}
29002
29003 @item Instruction repetition
29004 gcc / @emph{as}: Split into two lines; for example
29005 @*
29006 @code{rep}
29007 @*
29008 @code{stosl}
29009 @*
29010 Intel: Keep on one line; for example @code{rep stosl}
29011
29012 @item Order of operands
29013 gcc / @emph{as}: Source first; for example @code{movw $4, %eax}
29014 @*
29015 Intel: Destination first; for example @code{mov eax, 4}
29016 @end table
29017
29018 @c ---------------------------------------------------------------------------
29019 @node A Simple Example of Inline Assembler
29020 @section A Simple Example of Inline Assembler
29021
29022 @noindent
29023 The following example will generate a single assembly language statement,
29024 @code{nop}, which does nothing. Despite its lack of run-time effect,
29025 the example will be useful in illustrating the basics of
29026 the Inline Assembler facility.
29027
29028 @smallexample @c ada
29029 @group
29030 with System.Machine_Code; use System.Machine_Code;
29031 procedure Nothing is
29032 begin
29033 Asm ("nop");
29034 end Nothing;
29035 @end group
29036 @end smallexample
29037
29038 @code{Asm} is a procedure declared in package @code{System.Machine_Code};
29039 here it takes one parameter, a @emph{template string} that must be a static
29040 expression and that will form the generated instruction.
29041 @code{Asm} may be regarded as a compile-time procedure that parses
29042 the template string and additional parameters (none here),
29043 from which it generates a sequence of assembly language instructions.
29044
29045 The examples in this chapter will illustrate several of the forms
29046 for invoking @code{Asm}; a complete specification of the syntax
29047 is found in @ref{Machine Code Insertions,,, gnat_rm, GNAT Reference
29048 Manual}.
29049
29050 Under the standard GNAT conventions, the @code{Nothing} procedure
29051 should be in a file named @file{nothing.adb}.
29052 You can build the executable in the usual way:
29053 @smallexample
29054 gnatmake nothing
29055 @end smallexample
29056 However, the interesting aspect of this example is not its run-time behavior
29057 but rather the generated assembly code.
29058 To see this output, invoke the compiler as follows:
29059 @smallexample
29060 gcc -c -S -fomit-frame-pointer -gnatp @file{nothing.adb}
29061 @end smallexample
29062 where the options are:
29063
29064 @table @code
29065 @item -c
29066 compile only (no bind or link)
29067 @item -S
29068 generate assembler listing
29069 @item -fomit-frame-pointer
29070 do not set up separate stack frames
29071 @item -gnatp
29072 do not add runtime checks
29073 @end table
29074
29075 This gives a human-readable assembler version of the code. The resulting
29076 file will have the same name as the Ada source file, but with a @code{.s}
29077 extension. In our example, the file @file{nothing.s} has the following
29078 contents:
29079
29080 @smallexample
29081 @group
29082 .file "nothing.adb"
29083 gcc2_compiled.:
29084 ___gnu_compiled_ada:
29085 .text
29086 .align 4
29087 .globl __ada_nothing
29088 __ada_nothing:
29089 #APP
29090 nop
29091 #NO_APP
29092 jmp L1
29093 .align 2,0x90
29094 L1:
29095 ret
29096 @end group
29097 @end smallexample
29098
29099 The assembly code you included is clearly indicated by
29100 the compiler, between the @code{#APP} and @code{#NO_APP}
29101 delimiters. The character before the 'APP' and 'NOAPP'
29102 can differ on different targets. For example, GNU/Linux uses '#APP' while
29103 on NT you will see '/APP'.
29104
29105 If you make a mistake in your assembler code (such as using the
29106 wrong size modifier, or using a wrong operand for the instruction) GNAT
29107 will report this error in a temporary file, which will be deleted when
29108 the compilation is finished. Generating an assembler file will help
29109 in such cases, since you can assemble this file separately using the
29110 @emph{as} assembler that comes with gcc.
29111
29112 Assembling the file using the command
29113
29114 @smallexample
29115 as @file{nothing.s}
29116 @end smallexample
29117 @noindent
29118 will give you error messages whose lines correspond to the assembler
29119 input file, so you can easily find and correct any mistakes you made.
29120 If there are no errors, @emph{as} will generate an object file
29121 @file{nothing.out}.
29122
29123 @c ---------------------------------------------------------------------------
29124 @node Output Variables in Inline Assembler
29125 @section Output Variables in Inline Assembler
29126
29127 @noindent
29128 The examples in this section, showing how to access the processor flags,
29129 illustrate how to specify the destination operands for assembly language
29130 statements.
29131
29132 @smallexample @c ada
29133 @group
29134 with Interfaces; use Interfaces;
29135 with Ada.Text_IO; use Ada.Text_IO;
29136 with System.Machine_Code; use System.Machine_Code;
29137 procedure Get_Flags is
29138 Flags : Unsigned_32;
29139 use ASCII;
29140 begin
29141 Asm ("pushfl" & LF & HT & -- push flags on stack
29142 "popl %%eax" & LF & HT & -- load eax with flags
29143 "movl %%eax, %0", -- store flags in variable
29144 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29145 Put_Line ("Flags register:" & Flags'Img);
29146 end Get_Flags;
29147 @end group
29148 @end smallexample
29149
29150 In order to have a nicely aligned assembly listing, we have separated
29151 multiple assembler statements in the Asm template string with linefeed
29152 (ASCII.LF) and horizontal tab (ASCII.HT) characters.
29153 The resulting section of the assembly output file is:
29154
29155 @smallexample
29156 @group
29157 #APP
29158 pushfl
29159 popl %eax
29160 movl %eax, -40(%ebp)
29161 #NO_APP
29162 @end group
29163 @end smallexample
29164
29165 It would have been legal to write the Asm invocation as:
29166
29167 @smallexample
29168 Asm ("pushfl popl %%eax movl %%eax, %0")
29169 @end smallexample
29170
29171 but in the generated assembler file, this would come out as:
29172
29173 @smallexample
29174 #APP
29175 pushfl popl %eax movl %eax, -40(%ebp)
29176 #NO_APP
29177 @end smallexample
29178
29179 which is not so convenient for the human reader.
29180
29181 We use Ada comments
29182 at the end of each line to explain what the assembler instructions
29183 actually do. This is a useful convention.
29184
29185 When writing Inline Assembler instructions, you need to precede each register
29186 and variable name with a percent sign. Since the assembler already requires
29187 a percent sign at the beginning of a register name, you need two consecutive
29188 percent signs for such names in the Asm template string, thus @code{%%eax}.
29189 In the generated assembly code, one of the percent signs will be stripped off.
29190
29191 Names such as @code{%0}, @code{%1}, @code{%2}, etc., denote input or output
29192 variables: operands you later define using @code{Input} or @code{Output}
29193 parameters to @code{Asm}.
29194 An output variable is illustrated in
29195 the third statement in the Asm template string:
29196 @smallexample
29197 movl %%eax, %0
29198 @end smallexample
29199 The intent is to store the contents of the eax register in a variable that can
29200 be accessed in Ada. Simply writing @code{movl %%eax, Flags} would not
29201 necessarily work, since the compiler might optimize by using a register
29202 to hold Flags, and the expansion of the @code{movl} instruction would not be
29203 aware of this optimization. The solution is not to store the result directly
29204 but rather to advise the compiler to choose the correct operand form;
29205 that is the purpose of the @code{%0} output variable.
29206
29207 Information about the output variable is supplied in the @code{Outputs}
29208 parameter to @code{Asm}:
29209 @smallexample
29210 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29211 @end smallexample
29212
29213 The output is defined by the @code{Asm_Output} attribute of the target type;
29214 the general format is
29215 @smallexample
29216 Type'Asm_Output (constraint_string, variable_name)
29217 @end smallexample
29218
29219 The constraint string directs the compiler how
29220 to store/access the associated variable. In the example
29221 @smallexample
29222 Unsigned_32'Asm_Output ("=m", Flags);
29223 @end smallexample
29224 the @code{"m"} (memory) constraint tells the compiler that the variable
29225 @code{Flags} should be stored in a memory variable, thus preventing
29226 the optimizer from keeping it in a register. In contrast,
29227 @smallexample
29228 Unsigned_32'Asm_Output ("=r", Flags);
29229 @end smallexample
29230 uses the @code{"r"} (register) constraint, telling the compiler to
29231 store the variable in a register.
29232
29233 If the constraint is preceded by the equal character (@strong{=}), it tells
29234 the compiler that the variable will be used to store data into it.
29235
29236 In the @code{Get_Flags} example, we used the @code{"g"} (global) constraint,
29237 allowing the optimizer to choose whatever it deems best.
29238
29239 There are a fairly large number of constraints, but the ones that are
29240 most useful (for the Intel x86 processor) are the following:
29241
29242 @table @code
29243 @item =
29244 output constraint
29245 @item g
29246 global (i.e.@: can be stored anywhere)
29247 @item m
29248 in memory
29249 @item I
29250 a constant
29251 @item a
29252 use eax
29253 @item b
29254 use ebx
29255 @item c
29256 use ecx
29257 @item d
29258 use edx
29259 @item S
29260 use esi
29261 @item D
29262 use edi
29263 @item r
29264 use one of eax, ebx, ecx or edx
29265 @item q
29266 use one of eax, ebx, ecx, edx, esi or edi
29267 @end table
29268
29269 The full set of constraints is described in the gcc and @emph{as}
29270 documentation; note that it is possible to combine certain constraints
29271 in one constraint string.
29272
29273 You specify the association of an output variable with an assembler operand
29274 through the @code{%}@emph{n} notation, where @emph{n} is a non-negative
29275 integer. Thus in
29276 @smallexample @c ada
29277 @group
29278 Asm ("pushfl" & LF & HT & -- push flags on stack
29279 "popl %%eax" & LF & HT & -- load eax with flags
29280 "movl %%eax, %0", -- store flags in variable
29281 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29282 @end group
29283 @end smallexample
29284 @noindent
29285 @code{%0} will be replaced in the expanded code by the appropriate operand,
29286 whatever
29287 the compiler decided for the @code{Flags} variable.
29288
29289 In general, you may have any number of output variables:
29290 @itemize @bullet
29291 @item
29292 Count the operands starting at 0; thus @code{%0}, @code{%1}, etc.
29293 @item
29294 Specify the @code{Outputs} parameter as a parenthesized comma-separated list
29295 of @code{Asm_Output} attributes
29296 @end itemize
29297
29298 For example:
29299 @smallexample @c ada
29300 @group
29301 Asm ("movl %%eax, %0" & LF & HT &
29302 "movl %%ebx, %1" & LF & HT &
29303 "movl %%ecx, %2",
29304 Outputs => (Unsigned_32'Asm_Output ("=g", Var_A), -- %0 = Var_A
29305 Unsigned_32'Asm_Output ("=g", Var_B), -- %1 = Var_B
29306 Unsigned_32'Asm_Output ("=g", Var_C))); -- %2 = Var_C
29307 @end group
29308 @end smallexample
29309 @noindent
29310 where @code{Var_A}, @code{Var_B}, and @code{Var_C} are variables
29311 in the Ada program.
29312
29313 As a variation on the @code{Get_Flags} example, we can use the constraints
29314 string to direct the compiler to store the eax register into the @code{Flags}
29315 variable, instead of including the store instruction explicitly in the
29316 @code{Asm} template string:
29317
29318 @smallexample @c ada
29319 @group
29320 with Interfaces; use Interfaces;
29321 with Ada.Text_IO; use Ada.Text_IO;
29322 with System.Machine_Code; use System.Machine_Code;
29323 procedure Get_Flags_2 is
29324 Flags : Unsigned_32;
29325 use ASCII;
29326 begin
29327 Asm ("pushfl" & LF & HT & -- push flags on stack
29328 "popl %%eax", -- save flags in eax
29329 Outputs => Unsigned_32'Asm_Output ("=a", Flags));
29330 Put_Line ("Flags register:" & Flags'Img);
29331 end Get_Flags_2;
29332 @end group
29333 @end smallexample
29334
29335 @noindent
29336 The @code{"a"} constraint tells the compiler that the @code{Flags}
29337 variable will come from the eax register. Here is the resulting code:
29338
29339 @smallexample
29340 @group
29341 #APP
29342 pushfl
29343 popl %eax
29344 #NO_APP
29345 movl %eax,-40(%ebp)
29346 @end group
29347 @end smallexample
29348
29349 @noindent
29350 The compiler generated the store of eax into Flags after
29351 expanding the assembler code.
29352
29353 Actually, there was no need to pop the flags into the eax register;
29354 more simply, we could just pop the flags directly into the program variable:
29355
29356 @smallexample @c ada
29357 @group
29358 with Interfaces; use Interfaces;
29359 with Ada.Text_IO; use Ada.Text_IO;
29360 with System.Machine_Code; use System.Machine_Code;
29361 procedure Get_Flags_3 is
29362 Flags : Unsigned_32;
29363 use ASCII;
29364 begin
29365 Asm ("pushfl" & LF & HT & -- push flags on stack
29366 "pop %0", -- save flags in Flags
29367 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29368 Put_Line ("Flags register:" & Flags'Img);
29369 end Get_Flags_3;
29370 @end group
29371 @end smallexample
29372
29373 @c ---------------------------------------------------------------------------
29374 @node Input Variables in Inline Assembler
29375 @section Input Variables in Inline Assembler
29376
29377 @noindent
29378 The example in this section illustrates how to specify the source operands
29379 for assembly language statements.
29380 The program simply increments its input value by 1:
29381
29382 @smallexample @c ada
29383 @group
29384 with Interfaces; use Interfaces;
29385 with Ada.Text_IO; use Ada.Text_IO;
29386 with System.Machine_Code; use System.Machine_Code;
29387 procedure Increment is
29388
29389 function Incr (Value : Unsigned_32) return Unsigned_32 is
29390 Result : Unsigned_32;
29391 begin
29392 Asm ("incl %0",
29393 Inputs => Unsigned_32'Asm_Input ("a", Value),
29394 Outputs => Unsigned_32'Asm_Output ("=a", Result));
29395 return Result;
29396 end Incr;
29397
29398 Value : Unsigned_32;
29399
29400 begin
29401 Value := 5;
29402 Put_Line ("Value before is" & Value'Img);
29403 Value := Incr (Value);
29404 Put_Line ("Value after is" & Value'Img);
29405 end Increment;
29406 @end group
29407 @end smallexample
29408
29409 The @code{Outputs} parameter to @code{Asm} specifies
29410 that the result will be in the eax register and that it is to be stored
29411 in the @code{Result} variable.
29412
29413 The @code{Inputs} parameter looks much like the @code{Outputs} parameter,
29414 but with an @code{Asm_Input} attribute.
29415 The @code{"="} constraint, indicating an output value, is not present.
29416
29417 You can have multiple input variables, in the same way that you can have more
29418 than one output variable.
29419
29420 The parameter count (%0, %1) etc, now starts at the first input
29421 statement, and continues with the output statements.
29422 When both parameters use the same variable, the
29423 compiler will treat them as the same %n operand, which is the case here.
29424
29425 Just as the @code{Outputs} parameter causes the register to be stored into the
29426 target variable after execution of the assembler statements, so does the
29427 @code{Inputs} parameter cause its variable to be loaded into the register
29428 before execution of the assembler statements.
29429
29430 Thus the effect of the @code{Asm} invocation is:
29431 @enumerate
29432 @item load the 32-bit value of @code{Value} into eax
29433 @item execute the @code{incl %eax} instruction
29434 @item store the contents of eax into the @code{Result} variable
29435 @end enumerate
29436
29437 The resulting assembler file (with @option{-O2} optimization) contains:
29438 @smallexample
29439 @group
29440 _increment__incr.1:
29441 subl $4,%esp
29442 movl 8(%esp),%eax
29443 #APP
29444 incl %eax
29445 #NO_APP
29446 movl %eax,%edx
29447 movl %ecx,(%esp)
29448 addl $4,%esp
29449 ret
29450 @end group
29451 @end smallexample
29452
29453 @c ---------------------------------------------------------------------------
29454 @node Inlining Inline Assembler Code
29455 @section Inlining Inline Assembler Code
29456
29457 @noindent
29458 For a short subprogram such as the @code{Incr} function in the previous
29459 section, the overhead of the call and return (creating / deleting the stack
29460 frame) can be significant, compared to the amount of code in the subprogram
29461 body. A solution is to apply Ada's @code{Inline} pragma to the subprogram,
29462 which directs the compiler to expand invocations of the subprogram at the
29463 point(s) of call, instead of setting up a stack frame for out-of-line calls.
29464 Here is the resulting program:
29465
29466 @smallexample @c ada
29467 @group
29468 with Interfaces; use Interfaces;
29469 with Ada.Text_IO; use Ada.Text_IO;
29470 with System.Machine_Code; use System.Machine_Code;
29471 procedure Increment_2 is
29472
29473 function Incr (Value : Unsigned_32) return Unsigned_32 is
29474 Result : Unsigned_32;
29475 begin
29476 Asm ("incl %0",
29477 Inputs => Unsigned_32'Asm_Input ("a", Value),
29478 Outputs => Unsigned_32'Asm_Output ("=a", Result));
29479 return Result;
29480 end Incr;
29481 pragma Inline (Increment);
29482
29483 Value : Unsigned_32;
29484
29485 begin
29486 Value := 5;
29487 Put_Line ("Value before is" & Value'Img);
29488 Value := Increment (Value);
29489 Put_Line ("Value after is" & Value'Img);
29490 end Increment_2;
29491 @end group
29492 @end smallexample
29493
29494 Compile the program with both optimization (@option{-O2}) and inlining
29495 (@option{-gnatn}) enabled.
29496
29497 The @code{Incr} function is still compiled as usual, but at the
29498 point in @code{Increment} where our function used to be called:
29499
29500 @smallexample
29501 @group
29502 pushl %edi
29503 call _increment__incr.1
29504 @end group
29505 @end smallexample
29506
29507 @noindent
29508 the code for the function body directly appears:
29509
29510 @smallexample
29511 @group
29512 movl %esi,%eax
29513 #APP
29514 incl %eax
29515 #NO_APP
29516 movl %eax,%edx
29517 @end group
29518 @end smallexample
29519
29520 @noindent
29521 thus saving the overhead of stack frame setup and an out-of-line call.
29522
29523 @c ---------------------------------------------------------------------------
29524 @node Other Asm Functionality
29525 @section Other @code{Asm} Functionality
29526
29527 @noindent
29528 This section describes two important parameters to the @code{Asm}
29529 procedure: @code{Clobber}, which identifies register usage;
29530 and @code{Volatile}, which inhibits unwanted optimizations.
29531
29532 @menu
29533 * The Clobber Parameter::
29534 * The Volatile Parameter::
29535 @end menu
29536
29537 @c ---------------------------------------------------------------------------
29538 @node The Clobber Parameter
29539 @subsection The @code{Clobber} Parameter
29540
29541 @noindent
29542 One of the dangers of intermixing assembly language and a compiled language
29543 such as Ada is that the compiler needs to be aware of which registers are
29544 being used by the assembly code. In some cases, such as the earlier examples,
29545 the constraint string is sufficient to indicate register usage (e.g.,
29546 @code{"a"} for
29547 the eax register). But more generally, the compiler needs an explicit
29548 identification of the registers that are used by the Inline Assembly
29549 statements.
29550
29551 Using a register that the compiler doesn't know about
29552 could be a side effect of an instruction (like @code{mull}
29553 storing its result in both eax and edx).
29554 It can also arise from explicit register usage in your
29555 assembly code; for example:
29556 @smallexample
29557 @group
29558 Asm ("movl %0, %%ebx" & LF & HT &
29559 "movl %%ebx, %1",
29560 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
29561 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out));
29562 @end group
29563 @end smallexample
29564 @noindent
29565 where the compiler (since it does not analyze the @code{Asm} template string)
29566 does not know you are using the ebx register.
29567
29568 In such cases you need to supply the @code{Clobber} parameter to @code{Asm},
29569 to identify the registers that will be used by your assembly code:
29570
29571 @smallexample
29572 @group
29573 Asm ("movl %0, %%ebx" & LF & HT &
29574 "movl %%ebx, %1",
29575 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
29576 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
29577 Clobber => "ebx");
29578 @end group
29579 @end smallexample
29580
29581 The Clobber parameter is a static string expression specifying the
29582 register(s) you are using. Note that register names are @emph{not} prefixed
29583 by a percent sign. Also, if more than one register is used then their names
29584 are separated by commas; e.g., @code{"eax, ebx"}
29585
29586 The @code{Clobber} parameter has several additional uses:
29587 @enumerate
29588 @item Use ``register'' name @code{cc} to indicate that flags might have changed
29589 @item Use ``register'' name @code{memory} if you changed a memory location
29590 @end enumerate
29591
29592 @c ---------------------------------------------------------------------------
29593 @node The Volatile Parameter
29594 @subsection The @code{Volatile} Parameter
29595 @cindex Volatile parameter
29596
29597 @noindent
29598 Compiler optimizations in the presence of Inline Assembler may sometimes have
29599 unwanted effects. For example, when an @code{Asm} invocation with an input
29600 variable is inside a loop, the compiler might move the loading of the input
29601 variable outside the loop, regarding it as a one-time initialization.
29602
29603 If this effect is not desired, you can disable such optimizations by setting
29604 the @code{Volatile} parameter to @code{True}; for example:
29605
29606 @smallexample @c ada
29607 @group
29608 Asm ("movl %0, %%ebx" & LF & HT &
29609 "movl %%ebx, %1",
29610 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
29611 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
29612 Clobber => "ebx",
29613 Volatile => True);
29614 @end group
29615 @end smallexample
29616
29617 By default, @code{Volatile} is set to @code{False} unless there is no
29618 @code{Outputs} parameter.
29619
29620 Although setting @code{Volatile} to @code{True} prevents unwanted
29621 optimizations, it will also disable other optimizations that might be
29622 important for efficiency. In general, you should set @code{Volatile}
29623 to @code{True} only if the compiler's optimizations have created
29624 problems.
29625 @c END OF INLINE ASSEMBLER CHAPTER
29626 @c ===============================
29627
29628 @c ***********************************
29629 @c * Compatibility and Porting Guide *
29630 @c ***********************************
29631 @node Compatibility and Porting Guide
29632 @appendix Compatibility and Porting Guide
29633
29634 @noindent
29635 This chapter describes the compatibility issues that may arise between
29636 GNAT and other Ada compilation systems (including those for Ada 83),
29637 and shows how GNAT can expedite porting
29638 applications developed in other Ada environments.
29639
29640 @menu
29641 * Compatibility with Ada 83::
29642 * Compatibility between Ada 95 and Ada 2005::
29643 * Implementation-dependent characteristics::
29644 * Compatibility with Other Ada Systems::
29645 * Representation Clauses::
29646 @ifclear vms
29647 @c Brief section is only in non-VMS version
29648 @c Full chapter is in VMS version
29649 * Compatibility with HP Ada 83::
29650 @end ifclear
29651 @ifset vms
29652 * Transitioning to 64-Bit GNAT for OpenVMS::
29653 @end ifset
29654 @end menu
29655
29656 @node Compatibility with Ada 83
29657 @section Compatibility with Ada 83
29658 @cindex Compatibility (between Ada 83 and Ada 95 / Ada 2005)
29659
29660 @noindent
29661 Ada 95 and Ada 2005 are highly upwards compatible with Ada 83. In
29662 particular, the design intention was that the difficulties associated
29663 with moving from Ada 83 to Ada 95 or Ada 2005 should be no greater than those
29664 that occur when moving from one Ada 83 system to another.
29665
29666 However, there are a number of points at which there are minor
29667 incompatibilities. The @cite{Ada 95 Annotated Reference Manual} contains
29668 full details of these issues,
29669 and should be consulted for a complete treatment.
29670 In practice the
29671 following subsections treat the most likely issues to be encountered.
29672
29673 @menu
29674 * Legal Ada 83 programs that are illegal in Ada 95::
29675 * More deterministic semantics::
29676 * Changed semantics::
29677 * Other language compatibility issues::
29678 @end menu
29679
29680 @node Legal Ada 83 programs that are illegal in Ada 95
29681 @subsection Legal Ada 83 programs that are illegal in Ada 95
29682
29683 Some legal Ada 83 programs are illegal (i.e., they will fail to compile) in
29684 Ada 95 and thus also in Ada 2005:
29685
29686 @table @emph
29687 @item Character literals
29688 Some uses of character literals are ambiguous. Since Ada 95 has introduced
29689 @code{Wide_Character} as a new predefined character type, some uses of
29690 character literals that were legal in Ada 83 are illegal in Ada 95.
29691 For example:
29692 @smallexample @c ada
29693 for Char in 'A' .. 'Z' loop @dots{} end loop;
29694 @end smallexample
29695
29696 @noindent
29697 The problem is that @code{'A'} and @code{'Z'} could be from either
29698 @code{Character} or @code{Wide_Character}. The simplest correction
29699 is to make the type explicit; e.g.:
29700 @smallexample @c ada
29701 for Char in Character range 'A' .. 'Z' loop @dots{} end loop;
29702 @end smallexample
29703
29704 @item New reserved words
29705 The identifiers @code{abstract}, @code{aliased}, @code{protected},
29706 @code{requeue}, @code{tagged}, and @code{until} are reserved in Ada 95.
29707 Existing Ada 83 code using any of these identifiers must be edited to
29708 use some alternative name.
29709
29710 @item Freezing rules
29711 The rules in Ada 95 are slightly different with regard to the point at
29712 which entities are frozen, and representation pragmas and clauses are
29713 not permitted past the freeze point. This shows up most typically in
29714 the form of an error message complaining that a representation item
29715 appears too late, and the appropriate corrective action is to move
29716 the item nearer to the declaration of the entity to which it refers.
29717
29718 A particular case is that representation pragmas
29719 @ifset vms
29720 (including the
29721 extended HP Ada 83 compatibility pragmas such as @code{Export_Procedure})
29722 @end ifset
29723 cannot be applied to a subprogram body. If necessary, a separate subprogram
29724 declaration must be introduced to which the pragma can be applied.
29725
29726 @item Optional bodies for library packages
29727 In Ada 83, a package that did not require a package body was nevertheless
29728 allowed to have one. This lead to certain surprises in compiling large
29729 systems (situations in which the body could be unexpectedly ignored by the
29730 binder). In Ada 95, if a package does not require a body then it is not
29731 permitted to have a body. To fix this problem, simply remove a redundant
29732 body if it is empty, or, if it is non-empty, introduce a dummy declaration
29733 into the spec that makes the body required. One approach is to add a private
29734 part to the package declaration (if necessary), and define a parameterless
29735 procedure called @code{Requires_Body}, which must then be given a dummy
29736 procedure body in the package body, which then becomes required.
29737 Another approach (assuming that this does not introduce elaboration
29738 circularities) is to add an @code{Elaborate_Body} pragma to the package spec,
29739 since one effect of this pragma is to require the presence of a package body.
29740
29741 @item @code{Numeric_Error} is now the same as @code{Constraint_Error}
29742 In Ada 95, the exception @code{Numeric_Error} is a renaming of
29743 @code{Constraint_Error}.
29744 This means that it is illegal to have separate exception handlers for
29745 the two exceptions. The fix is simply to remove the handler for the
29746 @code{Numeric_Error} case (since even in Ada 83, a compiler was free to raise
29747 @code{Constraint_Error} in place of @code{Numeric_Error} in all cases).
29748
29749 @item Indefinite subtypes in generics
29750 In Ada 83, it was permissible to pass an indefinite type (e.g.@: @code{String})
29751 as the actual for a generic formal private type, but then the instantiation
29752 would be illegal if there were any instances of declarations of variables
29753 of this type in the generic body. In Ada 95, to avoid this clear violation
29754 of the methodological principle known as the ``contract model'',
29755 the generic declaration explicitly indicates whether
29756 or not such instantiations are permitted. If a generic formal parameter
29757 has explicit unknown discriminants, indicated by using @code{(<>)} after the
29758 type name, then it can be instantiated with indefinite types, but no
29759 stand-alone variables can be declared of this type. Any attempt to declare
29760 such a variable will result in an illegality at the time the generic is
29761 declared. If the @code{(<>)} notation is not used, then it is illegal
29762 to instantiate the generic with an indefinite type.
29763 This is the potential incompatibility issue when porting Ada 83 code to Ada 95.
29764 It will show up as a compile time error, and
29765 the fix is usually simply to add the @code{(<>)} to the generic declaration.
29766 @end table
29767
29768 @node More deterministic semantics
29769 @subsection More deterministic semantics
29770
29771 @table @emph
29772 @item Conversions
29773 Conversions from real types to integer types round away from 0. In Ada 83
29774 the conversion Integer(2.5) could deliver either 2 or 3 as its value. This
29775 implementation freedom was intended to support unbiased rounding in
29776 statistical applications, but in practice it interfered with portability.
29777 In Ada 95 the conversion semantics are unambiguous, and rounding away from 0
29778 is required. Numeric code may be affected by this change in semantics.
29779 Note, though, that this issue is no worse than already existed in Ada 83
29780 when porting code from one vendor to another.
29781
29782 @item Tasking
29783 The Real-Time Annex introduces a set of policies that define the behavior of
29784 features that were implementation dependent in Ada 83, such as the order in
29785 which open select branches are executed.
29786 @end table
29787
29788 @node Changed semantics
29789 @subsection Changed semantics
29790
29791 @noindent
29792 The worst kind of incompatibility is one where a program that is legal in
29793 Ada 83 is also legal in Ada 95 but can have an effect in Ada 95 that was not
29794 possible in Ada 83. Fortunately this is extremely rare, but the one
29795 situation that you should be alert to is the change in the predefined type
29796 @code{Character} from 7-bit ASCII to 8-bit Latin-1.
29797
29798 @table @emph
29799 @item Range of type @code{Character}
29800 The range of @code{Standard.Character} is now the full 256 characters
29801 of Latin-1, whereas in most Ada 83 implementations it was restricted
29802 to 128 characters. Although some of the effects of
29803 this change will be manifest in compile-time rejection of legal
29804 Ada 83 programs it is possible for a working Ada 83 program to have
29805 a different effect in Ada 95, one that was not permitted in Ada 83.
29806 As an example, the expression
29807 @code{Character'Pos(Character'Last)} returned @code{127} in Ada 83 and now
29808 delivers @code{255} as its value.
29809 In general, you should look at the logic of any
29810 character-processing Ada 83 program and see whether it needs to be adapted
29811 to work correctly with Latin-1. Note that the predefined Ada 95 API has a
29812 character handling package that may be relevant if code needs to be adapted
29813 to account for the additional Latin-1 elements.
29814 The desirable fix is to
29815 modify the program to accommodate the full character set, but in some cases
29816 it may be convenient to define a subtype or derived type of Character that
29817 covers only the restricted range.
29818 @cindex Latin-1
29819 @end table
29820
29821 @node Other language compatibility issues
29822 @subsection Other language compatibility issues
29823
29824 @table @emph
29825 @item @option{-gnat83} switch
29826 All implementations of GNAT provide a switch that causes GNAT to operate
29827 in Ada 83 mode. In this mode, some but not all compatibility problems
29828 of the type described above are handled automatically. For example, the
29829 new reserved words introduced in Ada 95 and Ada 2005 are treated simply
29830 as identifiers as in Ada 83.
29831 However,
29832 in practice, it is usually advisable to make the necessary modifications
29833 to the program to remove the need for using this switch.
29834 See @ref{Compiling Different Versions of Ada}.
29835
29836 @item Support for removed Ada 83 pragmas and attributes
29837 A number of pragmas and attributes from Ada 83 were removed from Ada 95,
29838 generally because they were replaced by other mechanisms. Ada 95 and Ada 2005
29839 compilers are allowed, but not required, to implement these missing
29840 elements. In contrast with some other compilers, GNAT implements all
29841 such pragmas and attributes, eliminating this compatibility concern. These
29842 include @code{pragma Interface} and the floating point type attributes
29843 (@code{Emax}, @code{Mantissa}, etc.), among other items.
29844 @end table
29845
29846
29847 @node Compatibility between Ada 95 and Ada 2005
29848 @section Compatibility between Ada 95 and Ada 2005
29849 @cindex Compatibility between Ada 95 and Ada 2005
29850
29851 @noindent
29852 Although Ada 2005 was designed to be upwards compatible with Ada 95, there are
29853 a number of incompatibilities. Several are enumerated below;
29854 for a complete description please see the
29855 Annotated Ada 2005 Reference Manual, or section 9.1.1 in
29856 @cite{Rationale for Ada 2005}.
29857
29858 @table @emph
29859 @item New reserved words.
29860 The words @code{interface}, @code{overriding} and @code{synchronized} are
29861 reserved in Ada 2005.
29862 A pre-Ada 2005 program that uses any of these as an identifier will be
29863 illegal.
29864
29865 @item New declarations in predefined packages.
29866 A number of packages in the predefined environment contain new declarations:
29867 @code{Ada.Exceptions}, @code{Ada.Real_Time}, @code{Ada.Strings},
29868 @code{Ada.Strings.Fixed}, @code{Ada.Strings.Bounded},
29869 @code{Ada.Strings.Unbounded}, @code{Ada.Strings.Wide_Fixed},
29870 @code{Ada.Strings.Wide_Bounded}, @code{Ada.Strings.Wide_Unbounded},
29871 @code{Ada.Tags}, @code{Ada.Text_IO}, and @code{Interfaces.C}.
29872 If an Ada 95 program does a @code{with} and @code{use} of any of these
29873 packages, the new declarations may cause name clashes.
29874
29875 @item Access parameters.
29876 A nondispatching subprogram with an access parameter cannot be renamed
29877 as a dispatching operation. This was permitted in Ada 95.
29878
29879 @item Access types, discriminants, and constraints.
29880 Rule changes in this area have led to some incompatibilities; for example,
29881 constrained subtypes of some access types are not permitted in Ada 2005.
29882
29883 @item Aggregates for limited types.
29884 The allowance of aggregates for limited types in Ada 2005 raises the
29885 possibility of ambiguities in legal Ada 95 programs, since additional types
29886 now need to be considered in expression resolution.
29887
29888 @item Fixed-point multiplication and division.
29889 Certain expressions involving ``*'' or ``/'' for a fixed-point type, which
29890 were legal in Ada 95 and invoked the predefined versions of these operations,
29891 are now ambiguous.
29892 The ambiguity may be resolved either by applying a type conversion to the
29893 expression, or by explicitly invoking the operation from package
29894 @code{Standard}.
29895
29896 @item Return-by-reference types.
29897 The Ada 95 return-by-reference mechanism has been removed. Instead, the user
29898 can declare a function returning a value from an anonymous access type.
29899 @end table
29900
29901
29902 @node Implementation-dependent characteristics
29903 @section Implementation-dependent characteristics
29904 @noindent
29905 Although the Ada language defines the semantics of each construct as
29906 precisely as practical, in some situations (for example for reasons of
29907 efficiency, or where the effect is heavily dependent on the host or target
29908 platform) the implementation is allowed some freedom. In porting Ada 83
29909 code to GNAT, you need to be aware of whether / how the existing code
29910 exercised such implementation dependencies. Such characteristics fall into
29911 several categories, and GNAT offers specific support in assisting the
29912 transition from certain Ada 83 compilers.
29913
29914 @menu
29915 * Implementation-defined pragmas::
29916 * Implementation-defined attributes::
29917 * Libraries::
29918 * Elaboration order::
29919 * Target-specific aspects::
29920 @end menu
29921
29922 @node Implementation-defined pragmas
29923 @subsection Implementation-defined pragmas
29924
29925 @noindent
29926 Ada compilers are allowed to supplement the language-defined pragmas, and
29927 these are a potential source of non-portability. All GNAT-defined pragmas
29928 are described in @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT
29929 Reference Manual}, and these include several that are specifically
29930 intended to correspond to other vendors' Ada 83 pragmas.
29931 For migrating from VADS, the pragma @code{Use_VADS_Size} may be useful.
29932 For compatibility with HP Ada 83, GNAT supplies the pragmas
29933 @code{Extend_System}, @code{Ident}, @code{Inline_Generic},
29934 @code{Interface_Name}, @code{Passive}, @code{Suppress_All},
29935 and @code{Volatile}.
29936 Other relevant pragmas include @code{External} and @code{Link_With}.
29937 Some vendor-specific
29938 Ada 83 pragmas (@code{Share_Generic}, @code{Subtitle}, and @code{Title}) are
29939 recognized, thus
29940 avoiding compiler rejection of units that contain such pragmas; they are not
29941 relevant in a GNAT context and hence are not otherwise implemented.
29942
29943 @node Implementation-defined attributes
29944 @subsection Implementation-defined attributes
29945
29946 Analogous to pragmas, the set of attributes may be extended by an
29947 implementation. All GNAT-defined attributes are described in
29948 @ref{Implementation Defined Attributes,,, gnat_rm, GNAT Reference
29949 Manual}, and these include several that are specifically intended
29950 to correspond to other vendors' Ada 83 attributes. For migrating from VADS,
29951 the attribute @code{VADS_Size} may be useful. For compatibility with HP
29952 Ada 83, GNAT supplies the attributes @code{Bit}, @code{Machine_Size} and
29953 @code{Type_Class}.
29954
29955 @node Libraries
29956 @subsection Libraries
29957 @noindent
29958 Vendors may supply libraries to supplement the standard Ada API. If Ada 83
29959 code uses vendor-specific libraries then there are several ways to manage
29960 this in Ada 95 or Ada 2005:
29961 @enumerate
29962 @item
29963 If the source code for the libraries (specs and bodies) are
29964 available, then the libraries can be migrated in the same way as the
29965 application.
29966 @item
29967 If the source code for the specs but not the bodies are
29968 available, then you can reimplement the bodies.
29969 @item
29970 Some features introduced by Ada 95 obviate the need for library support. For
29971 example most Ada 83 vendors supplied a package for unsigned integers. The
29972 Ada 95 modular type feature is the preferred way to handle this need, so
29973 instead of migrating or reimplementing the unsigned integer package it may
29974 be preferable to retrofit the application using modular types.
29975 @end enumerate
29976
29977 @node Elaboration order
29978 @subsection Elaboration order
29979 @noindent
29980 The implementation can choose any elaboration order consistent with the unit
29981 dependency relationship. This freedom means that some orders can result in
29982 Program_Error being raised due to an ``Access Before Elaboration'': an attempt
29983 to invoke a subprogram its body has been elaborated, or to instantiate a
29984 generic before the generic body has been elaborated. By default GNAT
29985 attempts to choose a safe order (one that will not encounter access before
29986 elaboration problems) by implicitly inserting @code{Elaborate} or
29987 @code{Elaborate_All} pragmas where
29988 needed. However, this can lead to the creation of elaboration circularities
29989 and a resulting rejection of the program by gnatbind. This issue is
29990 thoroughly described in @ref{Elaboration Order Handling in GNAT}.
29991 In brief, there are several
29992 ways to deal with this situation:
29993
29994 @itemize @bullet
29995 @item
29996 Modify the program to eliminate the circularities, e.g.@: by moving
29997 elaboration-time code into explicitly-invoked procedures
29998 @item
29999 Constrain the elaboration order by including explicit @code{Elaborate_Body} or
30000 @code{Elaborate} pragmas, and then inhibit the generation of implicit
30001 @code{Elaborate_All}
30002 pragmas either globally (as an effect of the @option{-gnatE} switch) or locally
30003 (by selectively suppressing elaboration checks via pragma
30004 @code{Suppress(Elaboration_Check)} when it is safe to do so).
30005 @end itemize
30006
30007 @node Target-specific aspects
30008 @subsection Target-specific aspects
30009 @noindent
30010 Low-level applications need to deal with machine addresses, data
30011 representations, interfacing with assembler code, and similar issues. If
30012 such an Ada 83 application is being ported to different target hardware (for
30013 example where the byte endianness has changed) then you will need to
30014 carefully examine the program logic; the porting effort will heavily depend
30015 on the robustness of the original design. Moreover, Ada 95 (and thus
30016 Ada 2005) are sometimes
30017 incompatible with typical Ada 83 compiler practices regarding implicit
30018 packing, the meaning of the Size attribute, and the size of access values.
30019 GNAT's approach to these issues is described in @ref{Representation Clauses}.
30020
30021 @node Compatibility with Other Ada Systems
30022 @section Compatibility with Other Ada Systems
30023
30024 @noindent
30025 If programs avoid the use of implementation dependent and
30026 implementation defined features, as documented in the @cite{Ada
30027 Reference Manual}, there should be a high degree of portability between
30028 GNAT and other Ada systems. The following are specific items which
30029 have proved troublesome in moving Ada 95 programs from GNAT to other Ada 95
30030 compilers, but do not affect porting code to GNAT@.
30031 (As of @value{NOW}, GNAT is the only compiler available for Ada 2005;
30032 the following issues may or may not arise for Ada 2005 programs
30033 when other compilers appear.)
30034
30035 @table @emph
30036 @item Ada 83 Pragmas and Attributes
30037 Ada 95 compilers are allowed, but not required, to implement the missing
30038 Ada 83 pragmas and attributes that are no longer defined in Ada 95.
30039 GNAT implements all such pragmas and attributes, eliminating this as
30040 a compatibility concern, but some other Ada 95 compilers reject these
30041 pragmas and attributes.
30042
30043 @item Specialized Needs Annexes
30044 GNAT implements the full set of special needs annexes. At the
30045 current time, it is the only Ada 95 compiler to do so. This means that
30046 programs making use of these features may not be portable to other Ada
30047 95 compilation systems.
30048
30049 @item Representation Clauses
30050 Some other Ada 95 compilers implement only the minimal set of
30051 representation clauses required by the Ada 95 reference manual. GNAT goes
30052 far beyond this minimal set, as described in the next section.
30053 @end table
30054
30055 @node Representation Clauses
30056 @section Representation Clauses
30057
30058 @noindent
30059 The Ada 83 reference manual was quite vague in describing both the minimal
30060 required implementation of representation clauses, and also their precise
30061 effects. Ada 95 (and thus also Ada 2005) are much more explicit, but the
30062 minimal set of capabilities required is still quite limited.
30063
30064 GNAT implements the full required set of capabilities in
30065 Ada 95 and Ada 2005, but also goes much further, and in particular
30066 an effort has been made to be compatible with existing Ada 83 usage to the
30067 greatest extent possible.
30068
30069 A few cases exist in which Ada 83 compiler behavior is incompatible with
30070 the requirements in Ada 95 (and thus also Ada 2005). These are instances of
30071 intentional or accidental dependence on specific implementation dependent
30072 characteristics of these Ada 83 compilers. The following is a list of
30073 the cases most likely to arise in existing Ada 83 code.
30074
30075 @table @emph
30076 @item Implicit Packing
30077 Some Ada 83 compilers allowed a Size specification to cause implicit
30078 packing of an array or record. This could cause expensive implicit
30079 conversions for change of representation in the presence of derived
30080 types, and the Ada design intends to avoid this possibility.
30081 Subsequent AI's were issued to make it clear that such implicit
30082 change of representation in response to a Size clause is inadvisable,
30083 and this recommendation is represented explicitly in the Ada 95 (and Ada 2005)
30084 Reference Manuals as implementation advice that is followed by GNAT@.
30085 The problem will show up as an error
30086 message rejecting the size clause. The fix is simply to provide
30087 the explicit pragma @code{Pack}, or for more fine tuned control, provide
30088 a Component_Size clause.
30089
30090 @item Meaning of Size Attribute
30091 The Size attribute in Ada 95 (and Ada 2005) for discrete types is defined as
30092 the minimal number of bits required to hold values of the type. For example,
30093 on a 32-bit machine, the size of @code{Natural} will typically be 31 and not
30094 32 (since no sign bit is required). Some Ada 83 compilers gave 31, and
30095 some 32 in this situation. This problem will usually show up as a compile
30096 time error, but not always. It is a good idea to check all uses of the
30097 'Size attribute when porting Ada 83 code. The GNAT specific attribute
30098 Object_Size can provide a useful way of duplicating the behavior of
30099 some Ada 83 compiler systems.
30100
30101 @item Size of Access Types
30102 A common assumption in Ada 83 code is that an access type is in fact a pointer,
30103 and that therefore it will be the same size as a System.Address value. This
30104 assumption is true for GNAT in most cases with one exception. For the case of
30105 a pointer to an unconstrained array type (where the bounds may vary from one
30106 value of the access type to another), the default is to use a ``fat pointer'',
30107 which is represented as two separate pointers, one to the bounds, and one to
30108 the array. This representation has a number of advantages, including improved
30109 efficiency. However, it may cause some difficulties in porting existing Ada 83
30110 code which makes the assumption that, for example, pointers fit in 32 bits on
30111 a machine with 32-bit addressing.
30112
30113 To get around this problem, GNAT also permits the use of ``thin pointers'' for
30114 access types in this case (where the designated type is an unconstrained array
30115 type). These thin pointers are indeed the same size as a System.Address value.
30116 To specify a thin pointer, use a size clause for the type, for example:
30117
30118 @smallexample @c ada
30119 type X is access all String;
30120 for X'Size use Standard'Address_Size;
30121 @end smallexample
30122
30123 @noindent
30124 which will cause the type X to be represented using a single pointer.
30125 When using this representation, the bounds are right behind the array.
30126 This representation is slightly less efficient, and does not allow quite
30127 such flexibility in the use of foreign pointers or in using the
30128 Unrestricted_Access attribute to create pointers to non-aliased objects.
30129 But for any standard portable use of the access type it will work in
30130 a functionally correct manner and allow porting of existing code.
30131 Note that another way of forcing a thin pointer representation
30132 is to use a component size clause for the element size in an array,
30133 or a record representation clause for an access field in a record.
30134 @end table
30135
30136 @ifclear vms
30137 @c This brief section is only in the non-VMS version
30138 @c The complete chapter on HP Ada is in the VMS version
30139 @node Compatibility with HP Ada 83
30140 @section Compatibility with HP Ada 83
30141
30142 @noindent
30143 The VMS version of GNAT fully implements all the pragmas and attributes
30144 provided by HP Ada 83, as well as providing the standard HP Ada 83
30145 libraries, including Starlet. In addition, data layouts and parameter
30146 passing conventions are highly compatible. This means that porting
30147 existing HP Ada 83 code to GNAT in VMS systems should be easier than
30148 most other porting efforts. The following are some of the most
30149 significant differences between GNAT and HP Ada 83.
30150
30151 @table @emph
30152 @item Default floating-point representation
30153 In GNAT, the default floating-point format is IEEE, whereas in HP Ada 83,
30154 it is VMS format. GNAT does implement the necessary pragmas
30155 (Long_Float, Float_Representation) for changing this default.
30156
30157 @item System
30158 The package System in GNAT exactly corresponds to the definition in the
30159 Ada 95 reference manual, which means that it excludes many of the
30160 HP Ada 83 extensions. However, a separate package Aux_DEC is provided
30161 that contains the additional definitions, and a special pragma,
30162 Extend_System allows this package to be treated transparently as an
30163 extension of package System.
30164
30165 @item To_Address
30166 The definitions provided by Aux_DEC are exactly compatible with those
30167 in the HP Ada 83 version of System, with one exception.
30168 HP Ada provides the following declarations:
30169
30170 @smallexample @c ada
30171 TO_ADDRESS (INTEGER)
30172 TO_ADDRESS (UNSIGNED_LONGWORD)
30173 TO_ADDRESS (@i{universal_integer})
30174 @end smallexample
30175
30176 @noindent
30177 The version of TO_ADDRESS taking a @i{universal integer} argument is in fact
30178 an extension to Ada 83 not strictly compatible with the reference manual.
30179 In GNAT, we are constrained to be exactly compatible with the standard,
30180 and this means we cannot provide this capability. In HP Ada 83, the
30181 point of this definition is to deal with a call like:
30182
30183 @smallexample @c ada
30184 TO_ADDRESS (16#12777#);
30185 @end smallexample
30186
30187 @noindent
30188 Normally, according to the Ada 83 standard, one would expect this to be
30189 ambiguous, since it matches both the INTEGER and UNSIGNED_LONGWORD forms
30190 of TO_ADDRESS@. However, in HP Ada 83, there is no ambiguity, since the
30191 definition using @i{universal_integer} takes precedence.
30192
30193 In GNAT, since the version with @i{universal_integer} cannot be supplied, it
30194 is not possible to be 100% compatible. Since there are many programs using
30195 numeric constants for the argument to TO_ADDRESS, the decision in GNAT was
30196 to change the name of the function in the UNSIGNED_LONGWORD case, so the
30197 declarations provided in the GNAT version of AUX_Dec are:
30198
30199 @smallexample @c ada
30200 function To_Address (X : Integer) return Address;
30201 pragma Pure_Function (To_Address);
30202
30203 function To_Address_Long (X : Unsigned_Longword)
30204 return Address;
30205 pragma Pure_Function (To_Address_Long);
30206 @end smallexample
30207
30208 @noindent
30209 This means that programs using TO_ADDRESS for UNSIGNED_LONGWORD must
30210 change the name to TO_ADDRESS_LONG@.
30211
30212 @item Task_Id values
30213 The Task_Id values assigned will be different in the two systems, and GNAT
30214 does not provide a specified value for the Task_Id of the environment task,
30215 which in GNAT is treated like any other declared task.
30216 @end table
30217
30218 @noindent
30219 For full details on these and other less significant compatibility issues,
30220 see appendix E of the HP publication entitled @cite{HP Ada, Technical
30221 Overview and Comparison on HP Platforms}.
30222
30223 For GNAT running on other than VMS systems, all the HP Ada 83 pragmas and
30224 attributes are recognized, although only a subset of them can sensibly
30225 be implemented. The description of pragmas in @ref{Implementation
30226 Defined Pragmas,,, gnat_rm, GNAT Reference Manual}
30227 indicates whether or not they are applicable to non-VMS systems.
30228 @end ifclear
30229
30230 @ifset vms
30231 @node Transitioning to 64-Bit GNAT for OpenVMS
30232 @section Transitioning to 64-Bit @value{EDITION} for OpenVMS
30233
30234 @noindent
30235 This section is meant to assist users of pre-2006 @value{EDITION}
30236 for Alpha OpenVMS who are transitioning to 64-bit @value{EDITION},
30237 the version of the GNAT technology supplied in 2006 and later for
30238 OpenVMS on both Alpha and I64.
30239
30240 @menu
30241 * Introduction to transitioning::
30242 * Migration of 32 bit code::
30243 * Taking advantage of 64 bit addressing::
30244 * Technical details::
30245 @end menu
30246
30247 @node Introduction to transitioning
30248 @subsection Introduction
30249
30250 @noindent
30251 64-bit @value{EDITION} for Open VMS has been designed to meet
30252 three main goals:
30253
30254 @enumerate
30255 @item
30256 Providing a full conforming implementation of Ada 95 and Ada 2005
30257
30258 @item
30259 Allowing maximum backward compatibility, thus easing migration of existing
30260 Ada source code
30261
30262 @item
30263 Supplying a path for exploiting the full 64-bit address range
30264 @end enumerate
30265
30266 @noindent
30267 Ada's strong typing semantics has made it
30268 impractical to have different 32-bit and 64-bit modes. As soon as
30269 one object could possibly be outside the 32-bit address space, this
30270 would make it necessary for the @code{System.Address} type to be 64 bits.
30271 In particular, this would cause inconsistencies if 32-bit code is
30272 called from 64-bit code that raises an exception.
30273
30274 This issue has been resolved by always using 64-bit addressing
30275 at the system level, but allowing for automatic conversions between
30276 32-bit and 64-bit addresses where required. Thus users who
30277 do not currently require 64-bit addressing capabilities, can
30278 recompile their code with only minimal changes (and indeed
30279 if the code is written in portable Ada, with no assumptions about
30280 the size of the @code{Address} type, then no changes at all are necessary).
30281 At the same time,
30282 this approach provides a simple, gradual upgrade path to future
30283 use of larger memories than available for 32-bit systems.
30284 Also, newly written applications or libraries will by default
30285 be fully compatible with future systems exploiting 64-bit
30286 addressing capabilities.
30287
30288 @ref{Migration of 32 bit code}, will focus on porting applications
30289 that do not require more than 2 GB of
30290 addressable memory. This code will be referred to as
30291 @emph{32-bit code}.
30292 For applications intending to exploit the full 64-bit address space,
30293 @ref{Taking advantage of 64 bit addressing},
30294 will consider further changes that may be required.
30295 Such code will be referred to below as @emph{64-bit code}.
30296
30297 @node Migration of 32 bit code
30298 @subsection Migration of 32-bit code
30299
30300 @menu
30301 * Address types::
30302 * Access types::
30303 * Unchecked conversions::
30304 * Predefined constants::
30305 * Interfacing with C::
30306 * Experience with source compatibility::
30307 @end menu
30308
30309 @node Address types
30310 @subsubsection Address types
30311
30312 @noindent
30313 To solve the problem of mixing 64-bit and 32-bit addressing,
30314 while maintaining maximum backward compatibility, the following
30315 approach has been taken:
30316
30317 @itemize @bullet
30318 @item
30319 @code{System.Address} always has a size of 64 bits
30320
30321 @item
30322 @code{System.Short_Address} is a 32-bit subtype of @code{System.Address}
30323 @end itemize
30324
30325 @noindent
30326 Since @code{System.Short_Address} is a subtype of @code{System.Address},
30327 a @code{Short_Address}
30328 may be used where an @code{Address} is required, and vice versa, without
30329 needing explicit type conversions.
30330 By virtue of the Open VMS parameter passing conventions,
30331 even imported
30332 and exported subprograms that have 32-bit address parameters are
30333 compatible with those that have 64-bit address parameters.
30334 (See @ref{Making code 64 bit clean} for details.)
30335
30336 The areas that may need attention are those where record types have
30337 been defined that contain components of the type @code{System.Address}, and
30338 where objects of this type are passed to code expecting a record layout with
30339 32-bit addresses.
30340
30341 Different compilers on different platforms cannot be
30342 expected to represent the same type in the same way,
30343 since alignment constraints
30344 and other system-dependent properties affect the compiler's decision.
30345 For that reason, Ada code
30346 generally uses representation clauses to specify the expected
30347 layout where required.
30348
30349 If such a representation clause uses 32 bits for a component having
30350 the type @code{System.Address}, 64-bit @value{EDITION} for OpenVMS
30351 will detect that error and produce a specific diagnostic message.
30352 The developer should then determine whether the representation
30353 should be 64 bits or not and make either of two changes:
30354 change the size to 64 bits and leave the type as @code{System.Address}, or
30355 leave the size as 32 bits and change the type to @code{System.Short_Address}.
30356 Since @code{Short_Address} is a subtype of @code{Address}, no changes are
30357 required in any code setting or accessing the field; the compiler will
30358 automatically perform any needed conversions between address
30359 formats.
30360
30361 @node Access types
30362 @subsubsection Access types
30363
30364 @noindent
30365 By default, objects designated by access values are always
30366 allocated in the 32-bit
30367 address space. Thus legacy code will never contain
30368 any objects that are not addressable with 32-bit addresses, and
30369 the compiler will never raise exceptions as result of mixing
30370 32-bit and 64-bit addresses.
30371
30372 However, the access values themselves are represented in 64 bits, for optimum
30373 performance and future compatibility with 64-bit code. As was
30374 the case with @code{System.Address}, the compiler will give an error message
30375 if an object or record component has a representation clause that
30376 requires the access value to fit in 32 bits. In such a situation,
30377 an explicit size clause for the access type, specifying 32 bits,
30378 will have the desired effect.
30379
30380 General access types (declared with @code{access all}) can never be
30381 32 bits, as values of such types must be able to refer to any object
30382 of the designated type,
30383 including objects residing outside the 32-bit address range.
30384 Existing Ada 83 code will not contain such type definitions,
30385 however, since general access types were introduced in Ada 95.
30386
30387 @node Unchecked conversions
30388 @subsubsection Unchecked conversions
30389
30390 @noindent
30391 In the case of an @code{Unchecked_Conversion} where the source type is a
30392 64-bit access type or the type @code{System.Address}, and the target
30393 type is a 32-bit type, the compiler will generate a warning.
30394 Even though the generated code will still perform the required
30395 conversions, it is highly recommended in these cases to use
30396 respectively a 32-bit access type or @code{System.Short_Address}
30397 as the source type.
30398
30399 @node Predefined constants
30400 @subsubsection Predefined constants
30401
30402 @noindent
30403 The following table shows the correspondence between pre-2006 versions of
30404 @value{EDITION} on Alpha OpenVMS (``Old'') and 64-bit @value{EDITION}
30405 (``New''):
30406
30407 @multitable {@code{System.Short_Memory_Size}} {2**32} {2**64}
30408 @item @b{Constant} @tab @b{Old} @tab @b{New}
30409 @item @code{System.Word_Size} @tab 32 @tab 64
30410 @item @code{System.Memory_Size} @tab 2**32 @tab 2**64
30411 @item @code{System.Short_Memory_Size} @tab 2**32 @tab 2**32
30412 @item @code{System.Address_Size} @tab 32 @tab 64
30413 @end multitable
30414
30415 @noindent
30416 If you need to refer to the specific
30417 memory size of a 32-bit implementation, instead of the
30418 actual memory size, use @code{System.Short_Memory_Size}
30419 rather than @code{System.Memory_Size}.
30420 Similarly, references to @code{System.Address_Size} may need
30421 to be replaced by @code{System.Short_Address'Size}.
30422 The program @command{gnatfind} may be useful for locating
30423 references to the above constants, so that you can verify that they
30424 are still correct.
30425
30426 @node Interfacing with C
30427 @subsubsection Interfacing with C
30428
30429 @noindent
30430 In order to minimize the impact of the transition to 64-bit addresses on
30431 legacy programs, some fundamental types in the @code{Interfaces.C}
30432 package hierarchy continue to be represented in 32 bits.
30433 These types are: @code{ptrdiff_t}, @code{size_t}, and @code{chars_ptr}.
30434 This eases integration with the default HP C layout choices, for example
30435 as found in the system routines in @code{DECC$SHR.EXE}.
30436 Because of this implementation choice, the type fully compatible with
30437 @code{chars_ptr} is now @code{Short_Address} and not @code{Address}.
30438 Depending on the context the compiler will issue a
30439 warning or an error when type @code{Address} is used, alerting the user to a
30440 potential problem. Otherwise 32-bit programs that use
30441 @code{Interfaces.C} should normally not require code modifications
30442
30443 The other issue arising with C interfacing concerns pragma @code{Convention}.
30444 For VMS 64-bit systems, there is an issue of the appropriate default size
30445 of C convention pointers in the absence of an explicit size clause. The HP
30446 C compiler can choose either 32 or 64 bits depending on compiler options.
30447 GNAT chooses 32-bits rather than 64-bits in the default case where no size
30448 clause is given. This proves a better choice for porting 32-bit legacy
30449 applications. In order to have a 64-bit representation, it is necessary to
30450 specify a size representation clause. For example:
30451
30452 @smallexample @c ada
30453 type int_star is access Interfaces.C.int;
30454 pragma Convention(C, int_star);
30455 for int_star'Size use 64; -- Necessary to get 64 and not 32 bits
30456 @end smallexample
30457
30458 @node Experience with source compatibility
30459 @subsubsection Experience with source compatibility
30460
30461 @noindent
30462 The Security Server and STARLET on I64 provide an interesting ``test case''
30463 for source compatibility issues, since it is in such system code
30464 where assumptions about @code{Address} size might be expected to occur.
30465 Indeed, there were a small number of occasions in the Security Server
30466 file @file{jibdef.ads}
30467 where a representation clause for a record type specified
30468 32 bits for a component of type @code{Address}.
30469 All of these errors were detected by the compiler.
30470 The repair was obvious and immediate; to simply replace @code{Address} by
30471 @code{Short_Address}.
30472
30473 In the case of STARLET, there were several record types that should
30474 have had representation clauses but did not. In these record types
30475 there was an implicit assumption that an @code{Address} value occupied
30476 32 bits.
30477 These compiled without error, but their usage resulted in run-time error
30478 returns from STARLET system calls.
30479 Future GNAT technology enhancements may include a tool that detects and flags
30480 these sorts of potential source code porting problems.
30481
30482 @c ****************************************
30483 @node Taking advantage of 64 bit addressing
30484 @subsection Taking advantage of 64-bit addressing
30485
30486 @menu
30487 * Making code 64 bit clean::
30488 * Allocating memory from the 64 bit storage pool::
30489 * Restrictions on use of 64 bit objects::
30490 * Using 64 bit storage pools by default::
30491 * General access types::
30492 * STARLET and other predefined libraries::
30493 @end menu
30494
30495 @node Making code 64 bit clean
30496 @subsubsection Making code 64-bit clean
30497
30498 @noindent
30499 In order to prevent problems that may occur when (parts of) a
30500 system start using memory outside the 32-bit address range,
30501 we recommend some additional guidelines:
30502
30503 @itemize @bullet
30504 @item
30505 For imported subprograms that take parameters of the
30506 type @code{System.Address}, ensure that these subprograms can
30507 indeed handle 64-bit addresses. If not, or when in doubt,
30508 change the subprogram declaration to specify
30509 @code{System.Short_Address} instead.
30510
30511 @item
30512 Resolve all warnings related to size mismatches in
30513 unchecked conversions. Failing to do so causes
30514 erroneous execution if the source object is outside
30515 the 32-bit address space.
30516
30517 @item
30518 (optional) Explicitly use the 32-bit storage pool
30519 for access types used in a 32-bit context, or use
30520 generic access types where possible
30521 (@pxref{Restrictions on use of 64 bit objects}).
30522 @end itemize
30523
30524 @noindent
30525 If these rules are followed, the compiler will automatically insert
30526 any necessary checks to ensure that no addresses or access values
30527 passed to 32-bit code ever refer to objects outside the 32-bit
30528 address range.
30529 Any attempt to do this will raise @code{Constraint_Error}.
30530
30531 @node Allocating memory from the 64 bit storage pool
30532 @subsubsection Allocating memory from the 64-bit storage pool
30533
30534 @noindent
30535 For any access type @code{T} that potentially requires memory allocations
30536 beyond the 32-bit address space,
30537 use the following representation clause:
30538
30539 @smallexample @c ada
30540 for T'Storage_Pool use System.Pool_64;
30541 @end smallexample
30542
30543 @node Restrictions on use of 64 bit objects
30544 @subsubsection Restrictions on use of 64-bit objects
30545
30546 @noindent
30547 Taking the address of an object allocated from a 64-bit storage pool,
30548 and then passing this address to a subprogram expecting
30549 @code{System.Short_Address},
30550 or assigning it to a variable of type @code{Short_Address}, will cause
30551 @code{Constraint_Error} to be raised. In case the code is not 64-bit clean
30552 (@pxref{Making code 64 bit clean}), or checks are suppressed,
30553 no exception is raised and execution
30554 will become erroneous.
30555
30556 @node Using 64 bit storage pools by default
30557 @subsubsection Using 64-bit storage pools by default
30558
30559 @noindent
30560 In some cases it may be desirable to have the compiler allocate
30561 from 64-bit storage pools by default. This may be the case for
30562 libraries that are 64-bit clean, but may be used in both 32-bit
30563 and 64-bit contexts. For these cases the following configuration
30564 pragma may be specified:
30565
30566 @smallexample @c ada
30567 pragma Pool_64_Default;
30568 @end smallexample
30569
30570 @noindent
30571 Any code compiled in the context of this pragma will by default
30572 use the @code{System.Pool_64} storage pool. This default may be overridden
30573 for a specific access type @code{T} by the representation clause:
30574
30575 @smallexample @c ada
30576 for T'Storage_Pool use System.Pool_32;
30577 @end smallexample
30578
30579 @noindent
30580 Any object whose address may be passed to a subprogram with a
30581 @code{Short_Address} argument, or assigned to a variable of type
30582 @code{Short_Address}, needs to be allocated from this pool.
30583
30584 @node General access types
30585 @subsubsection General access types
30586
30587 @noindent
30588 Objects designated by access values from a
30589 general access type (declared with @code{access all}) are never allocated
30590 from a 64-bit storage pool. Code that uses general access types will
30591 accept objects allocated in either 32-bit or 64-bit address spaces,
30592 but never allocate objects outside the 32-bit address space.
30593 Using general access types ensures maximum compatibility with both
30594 32-bit and 64-bit code.
30595
30596 @node STARLET and other predefined libraries
30597 @subsubsection STARLET and other predefined libraries
30598
30599 @noindent
30600 All code that comes as part of GNAT is 64-bit clean, but the
30601 restrictions given in @ref{Restrictions on use of 64 bit objects},
30602 still apply. Look at the package
30603 specs to see in which contexts objects allocated
30604 in 64-bit address space are acceptable.
30605
30606 @node Technical details
30607 @subsection Technical details
30608
30609 @noindent
30610 64-bit @value{EDITION} for Open VMS takes advantage of the freedom given in the
30611 Ada standard with respect to the type of @code{System.Address}. Previous
30612 versions of GNAT Pro have defined this type as private and implemented it as a
30613 modular type.
30614
30615 In order to allow defining @code{System.Short_Address} as a proper subtype,
30616 and to match the implicit sign extension in parameter passing,
30617 in 64-bit @value{EDITION} for Open VMS, @code{System.Address} is defined as a
30618 visible (i.e., non-private) integer type.
30619 Standard operations on the type, such as the binary operators ``+'', ``-'',
30620 etc., that take @code{Address} operands and return an @code{Address} result,
30621 have been hidden by declaring these
30622 @code{abstract}, a feature introduced in Ada 95 that helps avoid the potential
30623 ambiguities that would otherwise result from overloading.
30624 (Note that, although @code{Address} is a visible integer type,
30625 good programming practice dictates against exploiting the type's
30626 integer properties such as literals, since this will compromise
30627 code portability.)
30628
30629 Defining @code{Address} as a visible integer type helps achieve
30630 maximum compatibility for existing Ada code,
30631 without sacrificing the capabilities of the 64-bit architecture.
30632 @end ifset
30633
30634 @c ************************************************
30635 @ifset unw
30636 @node Microsoft Windows Topics
30637 @appendix Microsoft Windows Topics
30638 @cindex Windows NT
30639 @cindex Windows 95
30640 @cindex Windows 98
30641
30642 @noindent
30643 This chapter describes topics that are specific to the Microsoft Windows
30644 platforms (NT, 2000, and XP Professional).
30645
30646 @menu
30647 * Using GNAT on Windows::
30648 * Using a network installation of GNAT::
30649 * CONSOLE and WINDOWS subsystems::
30650 * Temporary Files::
30651 * Mixed-Language Programming on Windows::
30652 * Windows Calling Conventions::
30653 * Introduction to Dynamic Link Libraries (DLLs)::
30654 * Using DLLs with GNAT::
30655 * Building DLLs with GNAT::
30656 * Building DLLs with GNAT Project files::
30657 * Building DLLs with gnatdll::
30658 * GNAT and Windows Resources::
30659 * Debugging a DLL::
30660 * Setting Stack Size from gnatlink::
30661 * Setting Heap Size from gnatlink::
30662 @end menu
30663
30664 @node Using GNAT on Windows
30665 @section Using GNAT on Windows
30666
30667 @noindent
30668 One of the strengths of the GNAT technology is that its tool set
30669 (@command{gcc}, @command{gnatbind}, @command{gnatlink}, @command{gnatmake}, the
30670 @code{gdb} debugger, etc.) is used in the same way regardless of the
30671 platform.
30672
30673 On Windows this tool set is complemented by a number of Microsoft-specific
30674 tools that have been provided to facilitate interoperability with Windows
30675 when this is required. With these tools:
30676
30677 @itemize @bullet
30678
30679 @item
30680 You can build applications using the @code{CONSOLE} or @code{WINDOWS}
30681 subsystems.
30682
30683 @item
30684 You can use any Dynamically Linked Library (DLL) in your Ada code (both
30685 relocatable and non-relocatable DLLs are supported).
30686
30687 @item
30688 You can build Ada DLLs for use in other applications. These applications
30689 can be written in a language other than Ada (e.g., C, C++, etc). Again both
30690 relocatable and non-relocatable Ada DLLs are supported.
30691
30692 @item
30693 You can include Windows resources in your Ada application.
30694
30695 @item
30696 You can use or create COM/DCOM objects.
30697 @end itemize
30698
30699 @noindent
30700 Immediately below are listed all known general GNAT-for-Windows restrictions.
30701 Other restrictions about specific features like Windows Resources and DLLs
30702 are listed in separate sections below.
30703
30704 @itemize @bullet
30705
30706 @item
30707 It is not possible to use @code{GetLastError} and @code{SetLastError}
30708 when tasking, protected records, or exceptions are used. In these
30709 cases, in order to implement Ada semantics, the GNAT run-time system
30710 calls certain Win32 routines that set the last error variable to 0 upon
30711 success. It should be possible to use @code{GetLastError} and
30712 @code{SetLastError} when tasking, protected record, and exception
30713 features are not used, but it is not guaranteed to work.
30714
30715 @item
30716 It is not possible to link against Microsoft libraries except for
30717 import libraries. The library must be built to be compatible with
30718 @file{MSVCRT.LIB} (/MD Microsoft compiler option), @file{LIBC.LIB} and
30719 @file{LIBCMT.LIB} (/ML or /MT Microsoft compiler options) are known to
30720 not be compatible with the GNAT runtime. Even if the library is
30721 compatible with @file{MSVCRT.LIB} it is not guaranteed to work.
30722
30723 @item
30724 When the compilation environment is located on FAT32 drives, users may
30725 experience recompilations of the source files that have not changed if
30726 Daylight Saving Time (DST) state has changed since the last time files
30727 were compiled. NTFS drives do not have this problem.
30728
30729 @item
30730 No components of the GNAT toolset use any entries in the Windows
30731 registry. The only entries that can be created are file associations and
30732 PATH settings, provided the user has chosen to create them at installation
30733 time, as well as some minimal book-keeping information needed to correctly
30734 uninstall or integrate different GNAT products.
30735 @end itemize
30736
30737 @node Using a network installation of GNAT
30738 @section Using a network installation of GNAT
30739
30740 @noindent
30741 Make sure the system on which GNAT is installed is accessible from the
30742 current machine, i.e., the install location is shared over the network.
30743 Shared resources are accessed on Windows by means of UNC paths, which
30744 have the format @code{\\server\sharename\path}
30745
30746 In order to use such a network installation, simply add the UNC path of the
30747 @file{bin} directory of your GNAT installation in front of your PATH. For
30748 example, if GNAT is installed in @file{\GNAT} directory of a share location
30749 called @file{c-drive} on a machine @file{LOKI}, the following command will
30750 make it available:
30751
30752 @code{@ @ @ path \\loki\c-drive\gnat\bin;%path%}
30753
30754 Be aware that every compilation using the network installation results in the
30755 transfer of large amounts of data across the network and will likely cause
30756 serious performance penalty.
30757
30758 @node CONSOLE and WINDOWS subsystems
30759 @section CONSOLE and WINDOWS subsystems
30760 @cindex CONSOLE Subsystem
30761 @cindex WINDOWS Subsystem
30762 @cindex -mwindows
30763
30764 @noindent
30765 There are two main subsystems under Windows. The @code{CONSOLE} subsystem
30766 (which is the default subsystem) will always create a console when
30767 launching the application. This is not something desirable when the
30768 application has a Windows GUI. To get rid of this console the
30769 application must be using the @code{WINDOWS} subsystem. To do so
30770 the @option{-mwindows} linker option must be specified.
30771
30772 @smallexample
30773 $ gnatmake winprog -largs -mwindows
30774 @end smallexample
30775
30776 @node Temporary Files
30777 @section Temporary Files
30778 @cindex Temporary files
30779
30780 @noindent
30781 It is possible to control where temporary files gets created by setting
30782 the @env{TMP} environment variable. The file will be created:
30783
30784 @itemize
30785 @item Under the directory pointed to by the @env{TMP} environment variable if
30786 this directory exists.
30787
30788 @item Under @file{c:\temp}, if the @env{TMP} environment variable is not
30789 set (or not pointing to a directory) and if this directory exists.
30790
30791 @item Under the current working directory otherwise.
30792 @end itemize
30793
30794 @noindent
30795 This allows you to determine exactly where the temporary
30796 file will be created. This is particularly useful in networked
30797 environments where you may not have write access to some
30798 directories.
30799
30800 @node Mixed-Language Programming on Windows
30801 @section Mixed-Language Programming on Windows
30802
30803 @noindent
30804 Developing pure Ada applications on Windows is no different than on
30805 other GNAT-supported platforms. However, when developing or porting an
30806 application that contains a mix of Ada and C/C++, the choice of your
30807 Windows C/C++ development environment conditions your overall
30808 interoperability strategy.
30809
30810 If you use @command{gcc} to compile the non-Ada part of your application,
30811 there are no Windows-specific restrictions that affect the overall
30812 interoperability with your Ada code. If you plan to use
30813 Microsoft tools (e.g.@: Microsoft Visual C/C++), you should be aware of
30814 the following limitations:
30815
30816 @itemize @bullet
30817 @item
30818 You cannot link your Ada code with an object or library generated with
30819 Microsoft tools if these use the @code{.tls} section (Thread Local
30820 Storage section) since the GNAT linker does not yet support this section.
30821
30822 @item
30823 You cannot link your Ada code with an object or library generated with
30824 Microsoft tools if these use I/O routines other than those provided in
30825 the Microsoft DLL: @code{msvcrt.dll}. This is because the GNAT run time
30826 uses the services of @code{msvcrt.dll} for its I/Os. Use of other I/O
30827 libraries can cause a conflict with @code{msvcrt.dll} services. For
30828 instance Visual C++ I/O stream routines conflict with those in
30829 @code{msvcrt.dll}.
30830 @end itemize
30831
30832 @noindent
30833 If you do want to use the Microsoft tools for your non-Ada code and hit one
30834 of the above limitations, you have two choices:
30835
30836 @enumerate
30837 @item
30838 Encapsulate your non-Ada code in a DLL to be linked with your Ada
30839 application. In this case, use the Microsoft or whatever environment to
30840 build the DLL and use GNAT to build your executable
30841 (@pxref{Using DLLs with GNAT}).
30842
30843 @item
30844 Or you can encapsulate your Ada code in a DLL to be linked with the
30845 other part of your application. In this case, use GNAT to build the DLL
30846 (@pxref{Building DLLs with GNAT}) and use the Microsoft or whatever
30847 environment to build your executable.
30848 @end enumerate
30849
30850 @node Windows Calling Conventions
30851 @section Windows Calling Conventions
30852 @findex Stdcall
30853 @findex APIENTRY
30854
30855 @menu
30856 * C Calling Convention::
30857 * Stdcall Calling Convention::
30858 * Win32 Calling Convention::
30859 * DLL Calling Convention::
30860 @end menu
30861
30862 @noindent
30863 When a subprogram @code{F} (caller) calls a subprogram @code{G}
30864 (callee), there are several ways to push @code{G}'s parameters on the
30865 stack and there are several possible scenarios to clean up the stack
30866 upon @code{G}'s return. A calling convention is an agreed upon software
30867 protocol whereby the responsibilities between the caller (@code{F}) and
30868 the callee (@code{G}) are clearly defined. Several calling conventions
30869 are available for Windows:
30870
30871 @itemize @bullet
30872 @item
30873 @code{C} (Microsoft defined)
30874
30875 @item
30876 @code{Stdcall} (Microsoft defined)
30877
30878 @item
30879 @code{Win32} (GNAT specific)
30880
30881 @item
30882 @code{DLL} (GNAT specific)
30883 @end itemize
30884
30885 @node C Calling Convention
30886 @subsection @code{C} Calling Convention
30887
30888 @noindent
30889 This is the default calling convention used when interfacing to C/C++
30890 routines compiled with either @command{gcc} or Microsoft Visual C++.
30891
30892 In the @code{C} calling convention subprogram parameters are pushed on the
30893 stack by the caller from right to left. The caller itself is in charge of
30894 cleaning up the stack after the call. In addition, the name of a routine
30895 with @code{C} calling convention is mangled by adding a leading underscore.
30896
30897 The name to use on the Ada side when importing (or exporting) a routine
30898 with @code{C} calling convention is the name of the routine. For
30899 instance the C function:
30900
30901 @smallexample
30902 int get_val (long);
30903 @end smallexample
30904
30905 @noindent
30906 should be imported from Ada as follows:
30907
30908 @smallexample @c ada
30909 @group
30910 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
30911 pragma Import (C, Get_Val, External_Name => "get_val");
30912 @end group
30913 @end smallexample
30914
30915 @noindent
30916 Note that in this particular case the @code{External_Name} parameter could
30917 have been omitted since, when missing, this parameter is taken to be the
30918 name of the Ada entity in lower case. When the @code{Link_Name} parameter
30919 is missing, as in the above example, this parameter is set to be the
30920 @code{External_Name} with a leading underscore.
30921
30922 When importing a variable defined in C, you should always use the @code{C}
30923 calling convention unless the object containing the variable is part of a
30924 DLL (in which case you should use the @code{Stdcall} calling
30925 convention, @pxref{Stdcall Calling Convention}).
30926
30927 @node Stdcall Calling Convention
30928 @subsection @code{Stdcall} Calling Convention
30929
30930 @noindent
30931 This convention, which was the calling convention used for Pascal
30932 programs, is used by Microsoft for all the routines in the Win32 API for
30933 efficiency reasons. It must be used to import any routine for which this
30934 convention was specified.
30935
30936 In the @code{Stdcall} calling convention subprogram parameters are pushed
30937 on the stack by the caller from right to left. The callee (and not the
30938 caller) is in charge of cleaning the stack on routine exit. In addition,
30939 the name of a routine with @code{Stdcall} calling convention is mangled by
30940 adding a leading underscore (as for the @code{C} calling convention) and a
30941 trailing @code{@@}@code{@var{nn}}, where @var{nn} is the overall size (in
30942 bytes) of the parameters passed to the routine.
30943
30944 The name to use on the Ada side when importing a C routine with a
30945 @code{Stdcall} calling convention is the name of the C routine. The leading
30946 underscore and trailing @code{@@}@code{@var{nn}} are added automatically by
30947 the compiler. For instance the Win32 function:
30948
30949 @smallexample
30950 @b{APIENTRY} int get_val (long);
30951 @end smallexample
30952
30953 @noindent
30954 should be imported from Ada as follows:
30955
30956 @smallexample @c ada
30957 @group
30958 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
30959 pragma Import (Stdcall, Get_Val);
30960 -- On the x86 a long is 4 bytes, so the Link_Name is "_get_val@@4"
30961 @end group
30962 @end smallexample
30963
30964 @noindent
30965 As for the @code{C} calling convention, when the @code{External_Name}
30966 parameter is missing, it is taken to be the name of the Ada entity in lower
30967 case. If instead of writing the above import pragma you write:
30968
30969 @smallexample @c ada
30970 @group
30971 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
30972 pragma Import (Stdcall, Get_Val, External_Name => "retrieve_val");
30973 @end group
30974 @end smallexample
30975
30976 @noindent
30977 then the imported routine is @code{_retrieve_val@@4}. However, if instead
30978 of specifying the @code{External_Name} parameter you specify the
30979 @code{Link_Name} as in the following example:
30980
30981 @smallexample @c ada
30982 @group
30983 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
30984 pragma Import (Stdcall, Get_Val, Link_Name => "retrieve_val");
30985 @end group
30986 @end smallexample
30987
30988 @noindent
30989 then the imported routine is @code{retrieve_val}, that is, there is no
30990 decoration at all. No leading underscore and no Stdcall suffix
30991 @code{@@}@code{@var{nn}}.
30992
30993 @noindent
30994 This is especially important as in some special cases a DLL's entry
30995 point name lacks a trailing @code{@@}@code{@var{nn}} while the exported
30996 name generated for a call has it.
30997
30998 @noindent
30999 It is also possible to import variables defined in a DLL by using an
31000 import pragma for a variable. As an example, if a DLL contains a
31001 variable defined as:
31002
31003 @smallexample
31004 int my_var;
31005 @end smallexample
31006
31007 @noindent
31008 then, to access this variable from Ada you should write:
31009
31010 @smallexample @c ada
31011 @group
31012 My_Var : Interfaces.C.int;
31013 pragma Import (Stdcall, My_Var);
31014 @end group
31015 @end smallexample
31016
31017 @noindent
31018 Note that to ease building cross-platform bindings this convention
31019 will be handled as a @code{C} calling convention on non-Windows platforms.
31020
31021 @node Win32 Calling Convention
31022 @subsection @code{Win32} Calling Convention
31023
31024 @noindent
31025 This convention, which is GNAT-specific is fully equivalent to the
31026 @code{Stdcall} calling convention described above.
31027
31028 @node DLL Calling Convention
31029 @subsection @code{DLL} Calling Convention
31030
31031 @noindent
31032 This convention, which is GNAT-specific is fully equivalent to the
31033 @code{Stdcall} calling convention described above.
31034
31035 @node Introduction to Dynamic Link Libraries (DLLs)
31036 @section Introduction to Dynamic Link Libraries (DLLs)
31037 @findex DLL
31038
31039 @noindent
31040 A Dynamically Linked Library (DLL) is a library that can be shared by
31041 several applications running under Windows. A DLL can contain any number of
31042 routines and variables.
31043
31044 One advantage of DLLs is that you can change and enhance them without
31045 forcing all the applications that depend on them to be relinked or
31046 recompiled. However, you should be aware than all calls to DLL routines are
31047 slower since, as you will understand below, such calls are indirect.
31048
31049 To illustrate the remainder of this section, suppose that an application
31050 wants to use the services of a DLL @file{API.dll}. To use the services
31051 provided by @file{API.dll} you must statically link against the DLL or
31052 an import library which contains a jump table with an entry for each
31053 routine and variable exported by the DLL. In the Microsoft world this
31054 import library is called @file{API.lib}. When using GNAT this import
31055 library is called either @file{libAPI.dll.a}, @file{libapi.dll.a},
31056 @file{libAPI.a} or @file{libapi.a} (names are case insensitive).
31057
31058 After you have linked your application with the DLL or the import library
31059 and you run your application, here is what happens:
31060
31061 @enumerate
31062 @item
31063 Your application is loaded into memory.
31064
31065 @item
31066 The DLL @file{API.dll} is mapped into the address space of your
31067 application. This means that:
31068
31069 @itemize @bullet
31070 @item
31071 The DLL will use the stack of the calling thread.
31072
31073 @item
31074 The DLL will use the virtual address space of the calling process.
31075
31076 @item
31077 The DLL will allocate memory from the virtual address space of the calling
31078 process.
31079
31080 @item
31081 Handles (pointers) can be safely exchanged between routines in the DLL
31082 routines and routines in the application using the DLL.
31083 @end itemize
31084
31085 @item
31086 The entries in the jump table (from the import library @file{libAPI.dll.a}
31087 or @file{API.lib} or automatically created when linking against a DLL)
31088 which is part of your application are initialized with the addresses
31089 of the routines and variables in @file{API.dll}.
31090
31091 @item
31092 If present in @file{API.dll}, routines @code{DllMain} or
31093 @code{DllMainCRTStartup} are invoked. These routines typically contain
31094 the initialization code needed for the well-being of the routines and
31095 variables exported by the DLL.
31096 @end enumerate
31097
31098 @noindent
31099 There is an additional point which is worth mentioning. In the Windows
31100 world there are two kind of DLLs: relocatable and non-relocatable
31101 DLLs. Non-relocatable DLLs can only be loaded at a very specific address
31102 in the target application address space. If the addresses of two
31103 non-relocatable DLLs overlap and these happen to be used by the same
31104 application, a conflict will occur and the application will run
31105 incorrectly. Hence, when possible, it is always preferable to use and
31106 build relocatable DLLs. Both relocatable and non-relocatable DLLs are
31107 supported by GNAT. Note that the @option{-s} linker option (see GNU Linker
31108 User's Guide) removes the debugging symbols from the DLL but the DLL can
31109 still be relocated.
31110
31111 As a side note, an interesting difference between Microsoft DLLs and
31112 Unix shared libraries, is the fact that on most Unix systems all public
31113 routines are exported by default in a Unix shared library, while under
31114 Windows it is possible (but not required) to list exported routines in
31115 a definition file (@pxref{The Definition File}).
31116
31117 @node Using DLLs with GNAT
31118 @section Using DLLs with GNAT
31119
31120 @menu
31121 * Creating an Ada Spec for the DLL Services::
31122 * Creating an Import Library::
31123 @end menu
31124
31125 @noindent
31126 To use the services of a DLL, say @file{API.dll}, in your Ada application
31127 you must have:
31128
31129 @enumerate
31130 @item
31131 The Ada spec for the routines and/or variables you want to access in
31132 @file{API.dll}. If not available this Ada spec must be built from the C/C++
31133 header files provided with the DLL.
31134
31135 @item
31136 The import library (@file{libAPI.dll.a} or @file{API.lib}). As previously
31137 mentioned an import library is a statically linked library containing the
31138 import table which will be filled at load time to point to the actual
31139 @file{API.dll} routines. Sometimes you don't have an import library for the
31140 DLL you want to use. The following sections will explain how to build
31141 one. Note that this is optional.
31142
31143 @item
31144 The actual DLL, @file{API.dll}.
31145 @end enumerate
31146
31147 @noindent
31148 Once you have all the above, to compile an Ada application that uses the
31149 services of @file{API.dll} and whose main subprogram is @code{My_Ada_App},
31150 you simply issue the command
31151
31152 @smallexample
31153 $ gnatmake my_ada_app -largs -lAPI
31154 @end smallexample
31155
31156 @noindent
31157 The argument @option{-largs -lAPI} at the end of the @command{gnatmake} command
31158 tells the GNAT linker to look first for a library named @file{API.lib}
31159 (Microsoft-style name) and if not found for a libraries named
31160 @file{libAPI.dll.a}, @file{API.dll.a} or @file{libAPI.a}.
31161 (GNAT-style name). Note that if the Ada package spec for @file{API.dll}
31162 contains the following pragma
31163
31164 @smallexample @c ada
31165 pragma Linker_Options ("-lAPI");
31166 @end smallexample
31167
31168 @noindent
31169 you do not have to add @option{-largs -lAPI} at the end of the
31170 @command{gnatmake} command.
31171
31172 If any one of the items above is missing you will have to create it
31173 yourself. The following sections explain how to do so using as an
31174 example a fictitious DLL called @file{API.dll}.
31175
31176 @node Creating an Ada Spec for the DLL Services
31177 @subsection Creating an Ada Spec for the DLL Services
31178
31179 @noindent
31180 A DLL typically comes with a C/C++ header file which provides the
31181 definitions of the routines and variables exported by the DLL. The Ada
31182 equivalent of this header file is a package spec that contains definitions
31183 for the imported entities. If the DLL you intend to use does not come with
31184 an Ada spec you have to generate one such spec yourself. For example if
31185 the header file of @file{API.dll} is a file @file{api.h} containing the
31186 following two definitions:
31187
31188 @smallexample
31189 @group
31190 @cartouche
31191 int some_var;
31192 int get (char *);
31193 @end cartouche
31194 @end group
31195 @end smallexample
31196
31197 @noindent
31198 then the equivalent Ada spec could be:
31199
31200 @smallexample @c ada
31201 @group
31202 @cartouche
31203 with Interfaces.C.Strings;
31204 package API is
31205 use Interfaces;
31206
31207 Some_Var : C.int;
31208 function Get (Str : C.Strings.Chars_Ptr) return C.int;
31209
31210 private
31211 pragma Import (C, Get);
31212 pragma Import (DLL, Some_Var);
31213 end API;
31214 @end cartouche
31215 @end group
31216 @end smallexample
31217
31218 @noindent
31219 Note that a variable is
31220 @strong{always imported with a Stdcall convention}. A function
31221 can have @code{C} or @code{Stdcall} convention.
31222 (@pxref{Windows Calling Conventions}).
31223
31224 @node Creating an Import Library
31225 @subsection Creating an Import Library
31226 @cindex Import library
31227
31228 @menu
31229 * The Definition File::
31230 * GNAT-Style Import Library::
31231 * Microsoft-Style Import Library::
31232 @end menu
31233
31234 @noindent
31235 If a Microsoft-style import library @file{API.lib} or a GNAT-style
31236 import library @file{libAPI.dll.a} or @file{libAPI.a} is available
31237 with @file{API.dll} you can skip this section. You can also skip this
31238 section if @file{API.dll} or @file{libAPI.dll} is built with GNU tools
31239 as in this case it is possible to link directly against the
31240 DLL. Otherwise read on.
31241
31242 @node The Definition File
31243 @subsubsection The Definition File
31244 @cindex Definition file
31245 @findex .def
31246
31247 @noindent
31248 As previously mentioned, and unlike Unix systems, the list of symbols
31249 that are exported from a DLL must be provided explicitly in Windows.
31250 The main goal of a definition file is precisely that: list the symbols
31251 exported by a DLL. A definition file (usually a file with a @code{.def}
31252 suffix) has the following structure:
31253
31254 @smallexample
31255 @group
31256 @cartouche
31257 @r{[}LIBRARY @var{name}@r{]}
31258 @r{[}DESCRIPTION @var{string}@r{]}
31259 EXPORTS
31260 @var{symbol1}
31261 @var{symbol2}
31262 @dots{}
31263 @end cartouche
31264 @end group
31265 @end smallexample
31266
31267 @table @code
31268 @item LIBRARY @var{name}
31269 This section, which is optional, gives the name of the DLL.
31270
31271 @item DESCRIPTION @var{string}
31272 This section, which is optional, gives a description string that will be
31273 embedded in the import library.
31274
31275 @item EXPORTS
31276 This section gives the list of exported symbols (procedures, functions or
31277 variables). For instance in the case of @file{API.dll} the @code{EXPORTS}
31278 section of @file{API.def} looks like:
31279
31280 @smallexample
31281 @group
31282 @cartouche
31283 EXPORTS
31284 some_var
31285 get
31286 @end cartouche
31287 @end group
31288 @end smallexample
31289 @end table
31290
31291 @noindent
31292 Note that you must specify the correct suffix (@code{@@}@code{@var{nn}})
31293 (@pxref{Windows Calling Conventions}) for a Stdcall
31294 calling convention function in the exported symbols list.
31295
31296 @noindent
31297 There can actually be other sections in a definition file, but these
31298 sections are not relevant to the discussion at hand.
31299
31300 @node GNAT-Style Import Library
31301 @subsubsection GNAT-Style Import Library
31302
31303 @noindent
31304 To create a static import library from @file{API.dll} with the GNAT tools
31305 you should proceed as follows:
31306
31307 @enumerate
31308 @item
31309 Create the definition file @file{API.def} (@pxref{The Definition File}).
31310 For that use the @code{dll2def} tool as follows:
31311
31312 @smallexample
31313 $ dll2def API.dll > API.def
31314 @end smallexample
31315
31316 @noindent
31317 @code{dll2def} is a very simple tool: it takes as input a DLL and prints
31318 to standard output the list of entry points in the DLL. Note that if
31319 some routines in the DLL have the @code{Stdcall} convention
31320 (@pxref{Windows Calling Conventions}) with stripped @code{@@}@var{nn}
31321 suffix then you'll have to edit @file{api.def} to add it, and specify
31322 @option{-k} to @command{gnatdll} when creating the import library.
31323
31324 @noindent
31325 Here are some hints to find the right @code{@@}@var{nn} suffix.
31326
31327 @enumerate
31328 @item
31329 If you have the Microsoft import library (.lib), it is possible to get
31330 the right symbols by using Microsoft @code{dumpbin} tool (see the
31331 corresponding Microsoft documentation for further details).
31332
31333 @smallexample
31334 $ dumpbin /exports api.lib
31335 @end smallexample
31336
31337 @item
31338 If you have a message about a missing symbol at link time the compiler
31339 tells you what symbol is expected. You just have to go back to the
31340 definition file and add the right suffix.
31341 @end enumerate
31342
31343 @item
31344 Build the import library @code{libAPI.dll.a}, using @code{gnatdll}
31345 (@pxref{Using gnatdll}) as follows:
31346
31347 @smallexample
31348 $ gnatdll -e API.def -d API.dll
31349 @end smallexample
31350
31351 @noindent
31352 @code{gnatdll} takes as input a definition file @file{API.def} and the
31353 name of the DLL containing the services listed in the definition file
31354 @file{API.dll}. The name of the static import library generated is
31355 computed from the name of the definition file as follows: if the
31356 definition file name is @var{xyz}@code{.def}, the import library name will
31357 be @code{lib}@var{xyz}@code{.a}. Note that in the previous example option
31358 @option{-e} could have been removed because the name of the definition
31359 file (before the ``@code{.def}'' suffix) is the same as the name of the
31360 DLL (@pxref{Using gnatdll} for more information about @code{gnatdll}).
31361 @end enumerate
31362
31363 @node Microsoft-Style Import Library
31364 @subsubsection Microsoft-Style Import Library
31365
31366 @noindent
31367 With GNAT you can either use a GNAT-style or Microsoft-style import
31368 library. A Microsoft import library is needed only if you plan to make an
31369 Ada DLL available to applications developed with Microsoft
31370 tools (@pxref{Mixed-Language Programming on Windows}).
31371
31372 To create a Microsoft-style import library for @file{API.dll} you
31373 should proceed as follows:
31374
31375 @enumerate
31376 @item
31377 Create the definition file @file{API.def} from the DLL. For this use either
31378 the @code{dll2def} tool as described above or the Microsoft @code{dumpbin}
31379 tool (see the corresponding Microsoft documentation for further details).
31380
31381 @item
31382 Build the actual import library using Microsoft's @code{lib} utility:
31383
31384 @smallexample
31385 $ lib -machine:IX86 -def:API.def -out:API.lib
31386 @end smallexample
31387
31388 @noindent
31389 If you use the above command the definition file @file{API.def} must
31390 contain a line giving the name of the DLL:
31391
31392 @smallexample
31393 LIBRARY "API"
31394 @end smallexample
31395
31396 @noindent
31397 See the Microsoft documentation for further details about the usage of
31398 @code{lib}.
31399 @end enumerate
31400
31401 @node Building DLLs with GNAT
31402 @section Building DLLs with GNAT
31403 @cindex DLLs, building
31404
31405 @noindent
31406 This section explain how to build DLLs using the GNAT built-in DLL
31407 support. With the following procedure it is straight forward to build
31408 and use DLLs with GNAT.
31409
31410 @enumerate
31411
31412 @item building object files
31413
31414 The first step is to build all objects files that are to be included
31415 into the DLL. This is done by using the standard @command{gnatmake} tool.
31416
31417 @item building the DLL
31418
31419 To build the DLL you must use @command{gcc}'s @option{-shared}
31420 option. It is quite simple to use this method:
31421
31422 @smallexample
31423 $ gcc -shared -o api.dll obj1.o obj2.o @dots{}
31424 @end smallexample
31425
31426 It is important to note that in this case all symbols found in the
31427 object files are automatically exported. It is possible to restrict
31428 the set of symbols to export by passing to @command{gcc} a definition
31429 file, @pxref{The Definition File}. For example:
31430
31431 @smallexample
31432 $ gcc -shared -o api.dll api.def obj1.o obj2.o @dots{}
31433 @end smallexample
31434
31435 If you use a definition file you must export the elaboration procedures
31436 for every package that required one. Elaboration procedures are named
31437 using the package name followed by "_E".
31438
31439 @item preparing DLL to be used
31440
31441 For the DLL to be used by client programs the bodies must be hidden
31442 from it and the .ali set with read-only attribute. This is very important
31443 otherwise GNAT will recompile all packages and will not actually use
31444 the code in the DLL. For example:
31445
31446 @smallexample
31447 $ mkdir apilib
31448 $ copy *.ads *.ali api.dll apilib
31449 $ attrib +R apilib\*.ali
31450 @end smallexample
31451
31452 @end enumerate
31453
31454 At this point it is possible to use the DLL by directly linking
31455 against it. Note that you must use the GNAT shared runtime when using
31456 GNAT shared libraries. This is achieved by using @option{-shared} binder's
31457 option.
31458
31459 @smallexample
31460 $ gnatmake main -Iapilib -bargs -shared -largs -Lapilib -lAPI
31461 @end smallexample
31462
31463 @node Building DLLs with GNAT Project files
31464 @section Building DLLs with GNAT Project files
31465 @cindex DLLs, building
31466
31467 @noindent
31468 There is nothing specific to Windows in the build process.
31469 @pxref{Library Projects}.
31470
31471 @noindent
31472 Due to a system limitation, it is not possible under Windows to create threads
31473 when inside the @code{DllMain} routine which is used for auto-initialization
31474 of shared libraries, so it is not possible to have library level tasks in SALs.
31475
31476 @node Building DLLs with gnatdll
31477 @section Building DLLs with gnatdll
31478 @cindex DLLs, building
31479
31480 @menu
31481 * Limitations When Using Ada DLLs from Ada::
31482 * Exporting Ada Entities::
31483 * Ada DLLs and Elaboration::
31484 * Ada DLLs and Finalization::
31485 * Creating a Spec for Ada DLLs::
31486 * Creating the Definition File::
31487 * Using gnatdll::
31488 @end menu
31489
31490 @noindent
31491 Note that it is preferred to use the built-in GNAT DLL support
31492 (@pxref{Building DLLs with GNAT}) or GNAT Project files
31493 (@pxref{Building DLLs with GNAT Project files}) to build DLLs.
31494
31495 This section explains how to build DLLs containing Ada code using
31496 @code{gnatdll}. These DLLs will be referred to as Ada DLLs in the
31497 remainder of this section.
31498
31499 The steps required to build an Ada DLL that is to be used by Ada as well as
31500 non-Ada applications are as follows:
31501
31502 @enumerate
31503 @item
31504 You need to mark each Ada @i{entity} exported by the DLL with a @code{C} or
31505 @code{Stdcall} calling convention to avoid any Ada name mangling for the
31506 entities exported by the DLL (@pxref{Exporting Ada Entities}). You can
31507 skip this step if you plan to use the Ada DLL only from Ada applications.
31508
31509 @item
31510 Your Ada code must export an initialization routine which calls the routine
31511 @code{adainit} generated by @command{gnatbind} to perform the elaboration of
31512 the Ada code in the DLL (@pxref{Ada DLLs and Elaboration}). The initialization
31513 routine exported by the Ada DLL must be invoked by the clients of the DLL
31514 to initialize the DLL.
31515
31516 @item
31517 When useful, the DLL should also export a finalization routine which calls
31518 routine @code{adafinal} generated by @command{gnatbind} to perform the
31519 finalization of the Ada code in the DLL (@pxref{Ada DLLs and Finalization}).
31520 The finalization routine exported by the Ada DLL must be invoked by the
31521 clients of the DLL when the DLL services are no further needed.
31522
31523 @item
31524 You must provide a spec for the services exported by the Ada DLL in each
31525 of the programming languages to which you plan to make the DLL available.
31526
31527 @item
31528 You must provide a definition file listing the exported entities
31529 (@pxref{The Definition File}).
31530
31531 @item
31532 Finally you must use @code{gnatdll} to produce the DLL and the import
31533 library (@pxref{Using gnatdll}).
31534 @end enumerate
31535
31536 @noindent
31537 Note that a relocatable DLL stripped using the @code{strip}
31538 binutils tool will not be relocatable anymore. To build a DLL without
31539 debug information pass @code{-largs -s} to @code{gnatdll}. This
31540 restriction does not apply to a DLL built using a Library Project.
31541 @pxref{Library Projects}.
31542
31543 @node Limitations When Using Ada DLLs from Ada
31544 @subsection Limitations When Using Ada DLLs from Ada
31545
31546 @noindent
31547 When using Ada DLLs from Ada applications there is a limitation users
31548 should be aware of. Because on Windows the GNAT run time is not in a DLL of
31549 its own, each Ada DLL includes a part of the GNAT run time. Specifically,
31550 each Ada DLL includes the services of the GNAT run time that are necessary
31551 to the Ada code inside the DLL. As a result, when an Ada program uses an
31552 Ada DLL there are two independent GNAT run times: one in the Ada DLL and
31553 one in the main program.
31554
31555 It is therefore not possible to exchange GNAT run-time objects between the
31556 Ada DLL and the main Ada program. Example of GNAT run-time objects are file
31557 handles (e.g.@: @code{Text_IO.File_Type}), tasks types, protected objects
31558 types, etc.
31559
31560 It is completely safe to exchange plain elementary, array or record types,
31561 Windows object handles, etc.
31562
31563 @node Exporting Ada Entities
31564 @subsection Exporting Ada Entities
31565 @cindex Export table
31566
31567 @noindent
31568 Building a DLL is a way to encapsulate a set of services usable from any
31569 application. As a result, the Ada entities exported by a DLL should be
31570 exported with the @code{C} or @code{Stdcall} calling conventions to avoid
31571 any Ada name mangling. As an example here is an Ada package
31572 @code{API}, spec and body, exporting two procedures, a function, and a
31573 variable:
31574
31575 @smallexample @c ada
31576 @group
31577 @cartouche
31578 with Interfaces.C; use Interfaces;
31579 package API is
31580 Count : C.int := 0;
31581 function Factorial (Val : C.int) return C.int;
31582
31583 procedure Initialize_API;
31584 procedure Finalize_API;
31585 -- Initialization & Finalization routines. More in the next section.
31586 private
31587 pragma Export (C, Initialize_API);
31588 pragma Export (C, Finalize_API);
31589 pragma Export (C, Count);
31590 pragma Export (C, Factorial);
31591 end API;
31592 @end cartouche
31593 @end group
31594 @end smallexample
31595
31596 @smallexample @c ada
31597 @group
31598 @cartouche
31599 package body API is
31600 function Factorial (Val : C.int) return C.int is
31601 Fact : C.int := 1;
31602 begin
31603 Count := Count + 1;
31604 for K in 1 .. Val loop
31605 Fact := Fact * K;
31606 end loop;
31607 return Fact;
31608 end Factorial;
31609
31610 procedure Initialize_API is
31611 procedure Adainit;
31612 pragma Import (C, Adainit);
31613 begin
31614 Adainit;
31615 end Initialize_API;
31616
31617 procedure Finalize_API is
31618 procedure Adafinal;
31619 pragma Import (C, Adafinal);
31620 begin
31621 Adafinal;
31622 end Finalize_API;
31623 end API;
31624 @end cartouche
31625 @end group
31626 @end smallexample
31627
31628 @noindent
31629 If the Ada DLL you are building will only be used by Ada applications
31630 you do not have to export Ada entities with a @code{C} or @code{Stdcall}
31631 convention. As an example, the previous package could be written as
31632 follows:
31633
31634 @smallexample @c ada
31635 @group
31636 @cartouche
31637 package API is
31638 Count : Integer := 0;
31639 function Factorial (Val : Integer) return Integer;
31640
31641 procedure Initialize_API;
31642 procedure Finalize_API;
31643 -- Initialization and Finalization routines.
31644 end API;
31645 @end cartouche
31646 @end group
31647 @end smallexample
31648
31649 @smallexample @c ada
31650 @group
31651 @cartouche
31652 package body API is
31653 function Factorial (Val : Integer) return Integer is
31654 Fact : Integer := 1;
31655 begin
31656 Count := Count + 1;
31657 for K in 1 .. Val loop
31658 Fact := Fact * K;
31659 end loop;
31660 return Fact;
31661 end Factorial;
31662
31663 @dots{}
31664 -- The remainder of this package body is unchanged.
31665 end API;
31666 @end cartouche
31667 @end group
31668 @end smallexample
31669
31670 @noindent
31671 Note that if you do not export the Ada entities with a @code{C} or
31672 @code{Stdcall} convention you will have to provide the mangled Ada names
31673 in the definition file of the Ada DLL
31674 (@pxref{Creating the Definition File}).
31675
31676 @node Ada DLLs and Elaboration
31677 @subsection Ada DLLs and Elaboration
31678 @cindex DLLs and elaboration
31679
31680 @noindent
31681 The DLL that you are building contains your Ada code as well as all the
31682 routines in the Ada library that are needed by it. The first thing a
31683 user of your DLL must do is elaborate the Ada code
31684 (@pxref{Elaboration Order Handling in GNAT}).
31685
31686 To achieve this you must export an initialization routine
31687 (@code{Initialize_API} in the previous example), which must be invoked
31688 before using any of the DLL services. This elaboration routine must call
31689 the Ada elaboration routine @code{adainit} generated by the GNAT binder
31690 (@pxref{Binding with Non-Ada Main Programs}). See the body of
31691 @code{Initialize_Api} for an example. Note that the GNAT binder is
31692 automatically invoked during the DLL build process by the @code{gnatdll}
31693 tool (@pxref{Using gnatdll}).
31694
31695 When a DLL is loaded, Windows systematically invokes a routine called
31696 @code{DllMain}. It would therefore be possible to call @code{adainit}
31697 directly from @code{DllMain} without having to provide an explicit
31698 initialization routine. Unfortunately, it is not possible to call
31699 @code{adainit} from the @code{DllMain} if your program has library level
31700 tasks because access to the @code{DllMain} entry point is serialized by
31701 the system (that is, only a single thread can execute ``through'' it at a
31702 time), which means that the GNAT run time will deadlock waiting for the
31703 newly created task to complete its initialization.
31704
31705 @node Ada DLLs and Finalization
31706 @subsection Ada DLLs and Finalization
31707 @cindex DLLs and finalization
31708
31709 @noindent
31710 When the services of an Ada DLL are no longer needed, the client code should
31711 invoke the DLL finalization routine, if available. The DLL finalization
31712 routine is in charge of releasing all resources acquired by the DLL. In the
31713 case of the Ada code contained in the DLL, this is achieved by calling
31714 routine @code{adafinal} generated by the GNAT binder
31715 (@pxref{Binding with Non-Ada Main Programs}).
31716 See the body of @code{Finalize_Api} for an
31717 example. As already pointed out the GNAT binder is automatically invoked
31718 during the DLL build process by the @code{gnatdll} tool
31719 (@pxref{Using gnatdll}).
31720
31721 @node Creating a Spec for Ada DLLs
31722 @subsection Creating a Spec for Ada DLLs
31723
31724 @noindent
31725 To use the services exported by the Ada DLL from another programming
31726 language (e.g.@: C), you have to translate the specs of the exported Ada
31727 entities in that language. For instance in the case of @code{API.dll},
31728 the corresponding C header file could look like:
31729
31730 @smallexample
31731 @group
31732 @cartouche
31733 extern int *_imp__count;
31734 #define count (*_imp__count)
31735 int factorial (int);
31736 @end cartouche
31737 @end group
31738 @end smallexample
31739
31740 @noindent
31741 It is important to understand that when building an Ada DLL to be used by
31742 other Ada applications, you need two different specs for the packages
31743 contained in the DLL: one for building the DLL and the other for using
31744 the DLL. This is because the @code{DLL} calling convention is needed to
31745 use a variable defined in a DLL, but when building the DLL, the variable
31746 must have either the @code{Ada} or @code{C} calling convention. As an
31747 example consider a DLL comprising the following package @code{API}:
31748
31749 @smallexample @c ada
31750 @group
31751 @cartouche
31752 package API is
31753 Count : Integer := 0;
31754 @dots{}
31755 -- Remainder of the package omitted.
31756 end API;
31757 @end cartouche
31758 @end group
31759 @end smallexample
31760
31761 @noindent
31762 After producing a DLL containing package @code{API}, the spec that
31763 must be used to import @code{API.Count} from Ada code outside of the
31764 DLL is:
31765
31766 @smallexample @c ada
31767 @group
31768 @cartouche
31769 package API is
31770 Count : Integer;
31771 pragma Import (DLL, Count);
31772 end API;
31773 @end cartouche
31774 @end group
31775 @end smallexample
31776
31777 @node Creating the Definition File
31778 @subsection Creating the Definition File
31779
31780 @noindent
31781 The definition file is the last file needed to build the DLL. It lists
31782 the exported symbols. As an example, the definition file for a DLL
31783 containing only package @code{API} (where all the entities are exported
31784 with a @code{C} calling convention) is:
31785
31786 @smallexample
31787 @group
31788 @cartouche
31789 EXPORTS
31790 count
31791 factorial
31792 finalize_api
31793 initialize_api
31794 @end cartouche
31795 @end group
31796 @end smallexample
31797
31798 @noindent
31799 If the @code{C} calling convention is missing from package @code{API},
31800 then the definition file contains the mangled Ada names of the above
31801 entities, which in this case are:
31802
31803 @smallexample
31804 @group
31805 @cartouche
31806 EXPORTS
31807 api__count
31808 api__factorial
31809 api__finalize_api
31810 api__initialize_api
31811 @end cartouche
31812 @end group
31813 @end smallexample
31814
31815 @node Using gnatdll
31816 @subsection Using @code{gnatdll}
31817 @findex gnatdll
31818
31819 @menu
31820 * gnatdll Example::
31821 * gnatdll behind the Scenes::
31822 * Using dlltool::
31823 @end menu
31824
31825 @noindent
31826 @code{gnatdll} is a tool to automate the DLL build process once all the Ada
31827 and non-Ada sources that make up your DLL have been compiled.
31828 @code{gnatdll} is actually in charge of two distinct tasks: build the
31829 static import library for the DLL and the actual DLL. The form of the
31830 @code{gnatdll} command is
31831
31832 @smallexample
31833 @cartouche
31834 $ gnatdll @ovar{switches} @var{list-of-files} @r{[}-largs @var{opts}@r{]}
31835 @end cartouche
31836 @end smallexample
31837
31838 @noindent
31839 where @var{list-of-files} is a list of ALI and object files. The object
31840 file list must be the exact list of objects corresponding to the non-Ada
31841 sources whose services are to be included in the DLL. The ALI file list
31842 must be the exact list of ALI files for the corresponding Ada sources
31843 whose services are to be included in the DLL. If @var{list-of-files} is
31844 missing, only the static import library is generated.
31845
31846 @noindent
31847 You may specify any of the following switches to @code{gnatdll}:
31848
31849 @table @code
31850 @item -a@ovar{address}
31851 @cindex @option{-a} (@code{gnatdll})
31852 Build a non-relocatable DLL at @var{address}. If @var{address} is not
31853 specified the default address @var{0x11000000} will be used. By default,
31854 when this switch is missing, @code{gnatdll} builds relocatable DLL. We
31855 advise the reader to build relocatable DLL.
31856
31857 @item -b @var{address}
31858 @cindex @option{-b} (@code{gnatdll})
31859 Set the relocatable DLL base address. By default the address is
31860 @code{0x11000000}.
31861
31862 @item -bargs @var{opts}
31863 @cindex @option{-bargs} (@code{gnatdll})
31864 Binder options. Pass @var{opts} to the binder.
31865
31866 @item -d @var{dllfile}
31867 @cindex @option{-d} (@code{gnatdll})
31868 @var{dllfile} is the name of the DLL. This switch must be present for
31869 @code{gnatdll} to do anything. The name of the generated import library is
31870 obtained algorithmically from @var{dllfile} as shown in the following
31871 example: if @var{dllfile} is @code{xyz.dll}, the import library name is
31872 @code{libxyz.dll.a}. The name of the definition file to use (if not specified
31873 by option @option{-e}) is obtained algorithmically from @var{dllfile}
31874 as shown in the following example:
31875 if @var{dllfile} is @code{xyz.dll}, the definition
31876 file used is @code{xyz.def}.
31877
31878 @item -e @var{deffile}
31879 @cindex @option{-e} (@code{gnatdll})
31880 @var{deffile} is the name of the definition file.
31881
31882 @item -g
31883 @cindex @option{-g} (@code{gnatdll})
31884 Generate debugging information. This information is stored in the object
31885 file and copied from there to the final DLL file by the linker,
31886 where it can be read by the debugger. You must use the
31887 @option{-g} switch if you plan on using the debugger or the symbolic
31888 stack traceback.
31889
31890 @item -h
31891 @cindex @option{-h} (@code{gnatdll})
31892 Help mode. Displays @code{gnatdll} switch usage information.
31893
31894 @item -Idir
31895 @cindex @option{-I} (@code{gnatdll})
31896 Direct @code{gnatdll} to search the @var{dir} directory for source and
31897 object files needed to build the DLL.
31898 (@pxref{Search Paths and the Run-Time Library (RTL)}).
31899
31900 @item -k
31901 @cindex @option{-k} (@code{gnatdll})
31902 Removes the @code{@@}@var{nn} suffix from the import library's exported
31903 names, but keeps them for the link names. You must specify this
31904 option if you want to use a @code{Stdcall} function in a DLL for which
31905 the @code{@@}@var{nn} suffix has been removed. This is the case for most
31906 of the Windows NT DLL for example. This option has no effect when
31907 @option{-n} option is specified.
31908
31909 @item -l @var{file}
31910 @cindex @option{-l} (@code{gnatdll})
31911 The list of ALI and object files used to build the DLL are listed in
31912 @var{file}, instead of being given in the command line. Each line in
31913 @var{file} contains the name of an ALI or object file.
31914
31915 @item -n
31916 @cindex @option{-n} (@code{gnatdll})
31917 No Import. Do not create the import library.
31918
31919 @item -q
31920 @cindex @option{-q} (@code{gnatdll})
31921 Quiet mode. Do not display unnecessary messages.
31922
31923 @item -v
31924 @cindex @option{-v} (@code{gnatdll})
31925 Verbose mode. Display extra information.
31926
31927 @item -largs @var{opts}
31928 @cindex @option{-largs} (@code{gnatdll})
31929 Linker options. Pass @var{opts} to the linker.
31930 @end table
31931
31932 @node gnatdll Example
31933 @subsubsection @code{gnatdll} Example
31934
31935 @noindent
31936 As an example the command to build a relocatable DLL from @file{api.adb}
31937 once @file{api.adb} has been compiled and @file{api.def} created is
31938
31939 @smallexample
31940 $ gnatdll -d api.dll api.ali
31941 @end smallexample
31942
31943 @noindent
31944 The above command creates two files: @file{libapi.dll.a} (the import
31945 library) and @file{api.dll} (the actual DLL). If you want to create
31946 only the DLL, just type:
31947
31948 @smallexample
31949 $ gnatdll -d api.dll -n api.ali
31950 @end smallexample
31951
31952 @noindent
31953 Alternatively if you want to create just the import library, type:
31954
31955 @smallexample
31956 $ gnatdll -d api.dll
31957 @end smallexample
31958
31959 @node gnatdll behind the Scenes
31960 @subsubsection @code{gnatdll} behind the Scenes
31961
31962 @noindent
31963 This section details the steps involved in creating a DLL. @code{gnatdll}
31964 does these steps for you. Unless you are interested in understanding what
31965 goes on behind the scenes, you should skip this section.
31966
31967 We use the previous example of a DLL containing the Ada package @code{API},
31968 to illustrate the steps necessary to build a DLL. The starting point is a
31969 set of objects that will make up the DLL and the corresponding ALI
31970 files. In the case of this example this means that @file{api.o} and
31971 @file{api.ali} are available. To build a relocatable DLL, @code{gnatdll} does
31972 the following:
31973
31974 @enumerate
31975 @item
31976 @code{gnatdll} builds the base file (@file{api.base}). A base file gives
31977 the information necessary to generate relocation information for the
31978 DLL.
31979
31980 @smallexample
31981 @group
31982 $ gnatbind -n api
31983 $ gnatlink api -o api.jnk -mdll -Wl,--base-file,api.base
31984 @end group
31985 @end smallexample
31986
31987 @noindent
31988 In addition to the base file, the @command{gnatlink} command generates an
31989 output file @file{api.jnk} which can be discarded. The @option{-mdll} switch
31990 asks @command{gnatlink} to generate the routines @code{DllMain} and
31991 @code{DllMainCRTStartup} that are called by the Windows loader when the DLL
31992 is loaded into memory.
31993
31994 @item
31995 @code{gnatdll} uses @code{dlltool} (@pxref{Using dlltool}) to build the
31996 export table (@file{api.exp}). The export table contains the relocation
31997 information in a form which can be used during the final link to ensure
31998 that the Windows loader is able to place the DLL anywhere in memory.
31999
32000 @smallexample
32001 @group
32002 $ dlltool --dllname api.dll --def api.def --base-file api.base \
32003 --output-exp api.exp
32004 @end group
32005 @end smallexample
32006
32007 @item
32008 @code{gnatdll} builds the base file using the new export table. Note that
32009 @command{gnatbind} must be called once again since the binder generated file
32010 has been deleted during the previous call to @command{gnatlink}.
32011
32012 @smallexample
32013 @group
32014 $ gnatbind -n api
32015 $ gnatlink api -o api.jnk api.exp -mdll
32016 -Wl,--base-file,api.base
32017 @end group
32018 @end smallexample
32019
32020 @item
32021 @code{gnatdll} builds the new export table using the new base file and
32022 generates the DLL import library @file{libAPI.dll.a}.
32023
32024 @smallexample
32025 @group
32026 $ dlltool --dllname api.dll --def api.def --base-file api.base \
32027 --output-exp api.exp --output-lib libAPI.a
32028 @end group
32029 @end smallexample
32030
32031 @item
32032 Finally @code{gnatdll} builds the relocatable DLL using the final export
32033 table.
32034
32035 @smallexample
32036 @group
32037 $ gnatbind -n api
32038 $ gnatlink api api.exp -o api.dll -mdll
32039 @end group
32040 @end smallexample
32041 @end enumerate
32042
32043 @node Using dlltool
32044 @subsubsection Using @code{dlltool}
32045
32046 @noindent
32047 @code{dlltool} is the low-level tool used by @code{gnatdll} to build
32048 DLLs and static import libraries. This section summarizes the most
32049 common @code{dlltool} switches. The form of the @code{dlltool} command
32050 is
32051
32052 @smallexample
32053 $ dlltool @ovar{switches}
32054 @end smallexample
32055
32056 @noindent
32057 @code{dlltool} switches include:
32058
32059 @table @option
32060 @item --base-file @var{basefile}
32061 @cindex @option{--base-file} (@command{dlltool})
32062 Read the base file @var{basefile} generated by the linker. This switch
32063 is used to create a relocatable DLL.
32064
32065 @item --def @var{deffile}
32066 @cindex @option{--def} (@command{dlltool})
32067 Read the definition file.
32068
32069 @item --dllname @var{name}
32070 @cindex @option{--dllname} (@command{dlltool})
32071 Gives the name of the DLL. This switch is used to embed the name of the
32072 DLL in the static import library generated by @code{dlltool} with switch
32073 @option{--output-lib}.
32074
32075 @item -k
32076 @cindex @option{-k} (@command{dlltool})
32077 Kill @code{@@}@var{nn} from exported names
32078 (@pxref{Windows Calling Conventions}
32079 for a discussion about @code{Stdcall}-style symbols.
32080
32081 @item --help
32082 @cindex @option{--help} (@command{dlltool})
32083 Prints the @code{dlltool} switches with a concise description.
32084
32085 @item --output-exp @var{exportfile}
32086 @cindex @option{--output-exp} (@command{dlltool})
32087 Generate an export file @var{exportfile}. The export file contains the
32088 export table (list of symbols in the DLL) and is used to create the DLL.
32089
32090 @item --output-lib @var{libfile}
32091 @cindex @option{--output-lib} (@command{dlltool})
32092 Generate a static import library @var{libfile}.
32093
32094 @item -v
32095 @cindex @option{-v} (@command{dlltool})
32096 Verbose mode.
32097
32098 @item --as @var{assembler-name}
32099 @cindex @option{--as} (@command{dlltool})
32100 Use @var{assembler-name} as the assembler. The default is @code{as}.
32101 @end table
32102
32103 @node GNAT and Windows Resources
32104 @section GNAT and Windows Resources
32105 @cindex Resources, windows
32106
32107 @menu
32108 * Building Resources::
32109 * Compiling Resources::
32110 * Using Resources::
32111 @end menu
32112
32113 @noindent
32114 Resources are an easy way to add Windows specific objects to your
32115 application. The objects that can be added as resources include:
32116
32117 @itemize @bullet
32118 @item
32119 menus
32120
32121 @item
32122 accelerators
32123
32124 @item
32125 dialog boxes
32126
32127 @item
32128 string tables
32129
32130 @item
32131 bitmaps
32132
32133 @item
32134 cursors
32135
32136 @item
32137 icons
32138
32139 @item
32140 fonts
32141 @end itemize
32142
32143 @noindent
32144 This section explains how to build, compile and use resources.
32145
32146 @node Building Resources
32147 @subsection Building Resources
32148 @cindex Resources, building
32149
32150 @noindent
32151 A resource file is an ASCII file. By convention resource files have an
32152 @file{.rc} extension.
32153 The easiest way to build a resource file is to use Microsoft tools
32154 such as @code{imagedit.exe} to build bitmaps, icons and cursors and
32155 @code{dlgedit.exe} to build dialogs.
32156 It is always possible to build an @file{.rc} file yourself by writing a
32157 resource script.
32158
32159 It is not our objective to explain how to write a resource file. A
32160 complete description of the resource script language can be found in the
32161 Microsoft documentation.
32162
32163 @node Compiling Resources
32164 @subsection Compiling Resources
32165 @findex rc
32166 @findex windres
32167 @cindex Resources, compiling
32168
32169 @noindent
32170 This section describes how to build a GNAT-compatible (COFF) object file
32171 containing the resources. This is done using the Resource Compiler
32172 @code{windres} as follows:
32173
32174 @smallexample
32175 $ windres -i myres.rc -o myres.o
32176 @end smallexample
32177
32178 @noindent
32179 By default @code{windres} will run @command{gcc} to preprocess the @file{.rc}
32180 file. You can specify an alternate preprocessor (usually named
32181 @file{cpp.exe}) using the @code{windres} @option{--preprocessor}
32182 parameter. A list of all possible options may be obtained by entering
32183 the command @code{windres} @option{--help}.
32184
32185 It is also possible to use the Microsoft resource compiler @code{rc.exe}
32186 to produce a @file{.res} file (binary resource file). See the
32187 corresponding Microsoft documentation for further details. In this case
32188 you need to use @code{windres} to translate the @file{.res} file to a
32189 GNAT-compatible object file as follows:
32190
32191 @smallexample
32192 $ windres -i myres.res -o myres.o
32193 @end smallexample
32194
32195 @node Using Resources
32196 @subsection Using Resources
32197 @cindex Resources, using
32198
32199 @noindent
32200 To include the resource file in your program just add the
32201 GNAT-compatible object file for the resource(s) to the linker
32202 arguments. With @command{gnatmake} this is done by using the @option{-largs}
32203 option:
32204
32205 @smallexample
32206 $ gnatmake myprog -largs myres.o
32207 @end smallexample
32208
32209 @node Debugging a DLL
32210 @section Debugging a DLL
32211 @cindex DLL debugging
32212
32213 @menu
32214 * Program and DLL Both Built with GCC/GNAT::
32215 * Program Built with Foreign Tools and DLL Built with GCC/GNAT::
32216 @end menu
32217
32218 @noindent
32219 Debugging a DLL is similar to debugging a standard program. But
32220 we have to deal with two different executable parts: the DLL and the
32221 program that uses it. We have the following four possibilities:
32222
32223 @enumerate 1
32224 @item
32225 The program and the DLL are built with @code{GCC/GNAT}.
32226 @item
32227 The program is built with foreign tools and the DLL is built with
32228 @code{GCC/GNAT}.
32229 @item
32230 The program is built with @code{GCC/GNAT} and the DLL is built with
32231 foreign tools.
32232 @item
32233 @end enumerate
32234
32235 @noindent
32236 In this section we address only cases one and two above.
32237 There is no point in trying to debug
32238 a DLL with @code{GNU/GDB}, if there is no GDB-compatible debugging
32239 information in it. To do so you must use a debugger compatible with the
32240 tools suite used to build the DLL.
32241
32242 @node Program and DLL Both Built with GCC/GNAT
32243 @subsection Program and DLL Both Built with GCC/GNAT
32244
32245 @noindent
32246 This is the simplest case. Both the DLL and the program have @code{GDB}
32247 compatible debugging information. It is then possible to break anywhere in
32248 the process. Let's suppose here that the main procedure is named
32249 @code{ada_main} and that in the DLL there is an entry point named
32250 @code{ada_dll}.
32251
32252 @noindent
32253 The DLL (@pxref{Introduction to Dynamic Link Libraries (DLLs)}) and
32254 program must have been built with the debugging information (see GNAT -g
32255 switch). Here are the step-by-step instructions for debugging it:
32256
32257 @enumerate 1
32258 @item Launch @code{GDB} on the main program.
32259
32260 @smallexample
32261 $ gdb -nw ada_main
32262 @end smallexample
32263
32264 @item Start the program and stop at the beginning of the main procedure
32265
32266 @smallexample
32267 (gdb) start
32268 @end smallexample
32269
32270 @noindent
32271 This step is required to be able to set a breakpoint inside the DLL. As long
32272 as the program is not run, the DLL is not loaded. This has the
32273 consequence that the DLL debugging information is also not loaded, so it is not
32274 possible to set a breakpoint in the DLL.
32275
32276 @item Set a breakpoint inside the DLL
32277
32278 @smallexample
32279 (gdb) break ada_dll
32280 (gdb) cont
32281 @end smallexample
32282
32283 @end enumerate
32284
32285 @noindent
32286 At this stage a breakpoint is set inside the DLL. From there on
32287 you can use the standard approach to debug the whole program
32288 (@pxref{Running and Debugging Ada Programs}).
32289
32290 @ignore
32291 @c This used to work, probably because the DLLs were non-relocatable
32292 @c keep this section around until the problem is sorted out.
32293
32294 To break on the @code{DllMain} routine it is not possible to follow
32295 the procedure above. At the time the program stop on @code{ada_main}
32296 the @code{DllMain} routine as already been called. Either you can use
32297 the procedure below @pxref{Debugging the DLL Directly} or this procedure:
32298
32299 @enumerate 1
32300 @item Launch @code{GDB} on the main program.
32301
32302 @smallexample
32303 $ gdb ada_main
32304 @end smallexample
32305
32306 @item Load DLL symbols
32307
32308 @smallexample
32309 (gdb) add-sym api.dll
32310 @end smallexample
32311
32312 @item Set a breakpoint inside the DLL
32313
32314 @smallexample
32315 (gdb) break ada_dll.adb:45
32316 @end smallexample
32317
32318 Note that at this point it is not possible to break using the routine symbol
32319 directly as the program is not yet running. The solution is to break
32320 on the proper line (break in @file{ada_dll.adb} line 45).
32321
32322 @item Start the program
32323
32324 @smallexample
32325 (gdb) run
32326 @end smallexample
32327
32328 @end enumerate
32329 @end ignore
32330
32331 @node Program Built with Foreign Tools and DLL Built with GCC/GNAT
32332 @subsection Program Built with Foreign Tools and DLL Built with GCC/GNAT
32333
32334 @menu
32335 * Debugging the DLL Directly::
32336 * Attaching to a Running Process::
32337 @end menu
32338
32339 @noindent
32340 In this case things are slightly more complex because it is not possible to
32341 start the main program and then break at the beginning to load the DLL and the
32342 associated DLL debugging information. It is not possible to break at the
32343 beginning of the program because there is no @code{GDB} debugging information,
32344 and therefore there is no direct way of getting initial control. This
32345 section addresses this issue by describing some methods that can be used
32346 to break somewhere in the DLL to debug it.
32347
32348 @noindent
32349 First suppose that the main procedure is named @code{main} (this is for
32350 example some C code built with Microsoft Visual C) and that there is a
32351 DLL named @code{test.dll} containing an Ada entry point named
32352 @code{ada_dll}.
32353
32354 @noindent
32355 The DLL (@pxref{Introduction to Dynamic Link Libraries (DLLs)}) must have
32356 been built with debugging information (see GNAT -g option).
32357
32358 @node Debugging the DLL Directly
32359 @subsubsection Debugging the DLL Directly
32360
32361 @enumerate 1
32362 @item
32363 Find out the executable starting address
32364
32365 @smallexample
32366 $ objdump --file-header main.exe
32367 @end smallexample
32368
32369 The starting address is reported on the last line. For example:
32370
32371 @smallexample
32372 main.exe: file format pei-i386
32373 architecture: i386, flags 0x0000010a:
32374 EXEC_P, HAS_DEBUG, D_PAGED
32375 start address 0x00401010
32376 @end smallexample
32377
32378 @item
32379 Launch the debugger on the executable.
32380
32381 @smallexample
32382 $ gdb main.exe
32383 @end smallexample
32384
32385 @item
32386 Set a breakpoint at the starting address, and launch the program.
32387
32388 @smallexample
32389 $ (gdb) break *0x00401010
32390 $ (gdb) run
32391 @end smallexample
32392
32393 The program will stop at the given address.
32394
32395 @item
32396 Set a breakpoint on a DLL subroutine.
32397
32398 @smallexample
32399 (gdb) break ada_dll.adb:45
32400 @end smallexample
32401
32402 Or if you want to break using a symbol on the DLL, you need first to
32403 select the Ada language (language used by the DLL).
32404
32405 @smallexample
32406 (gdb) set language ada
32407 (gdb) break ada_dll
32408 @end smallexample
32409
32410 @item
32411 Continue the program.
32412
32413 @smallexample
32414 (gdb) cont
32415 @end smallexample
32416
32417 @noindent
32418 This will run the program until it reaches the breakpoint that has been
32419 set. From that point you can use the standard way to debug a program
32420 as described in (@pxref{Running and Debugging Ada Programs}).
32421
32422 @end enumerate
32423
32424 @noindent
32425 It is also possible to debug the DLL by attaching to a running process.
32426
32427 @node Attaching to a Running Process
32428 @subsubsection Attaching to a Running Process
32429 @cindex DLL debugging, attach to process
32430
32431 @noindent
32432 With @code{GDB} it is always possible to debug a running process by
32433 attaching to it. It is possible to debug a DLL this way. The limitation
32434 of this approach is that the DLL must run long enough to perform the
32435 attach operation. It may be useful for instance to insert a time wasting
32436 loop in the code of the DLL to meet this criterion.
32437
32438 @enumerate 1
32439
32440 @item Launch the main program @file{main.exe}.
32441
32442 @smallexample
32443 $ main
32444 @end smallexample
32445
32446 @item Use the Windows @i{Task Manager} to find the process ID. Let's say
32447 that the process PID for @file{main.exe} is 208.
32448
32449 @item Launch gdb.
32450
32451 @smallexample
32452 $ gdb
32453 @end smallexample
32454
32455 @item Attach to the running process to be debugged.
32456
32457 @smallexample
32458 (gdb) attach 208
32459 @end smallexample
32460
32461 @item Load the process debugging information.
32462
32463 @smallexample
32464 (gdb) symbol-file main.exe
32465 @end smallexample
32466
32467 @item Break somewhere in the DLL.
32468
32469 @smallexample
32470 (gdb) break ada_dll
32471 @end smallexample
32472
32473 @item Continue process execution.
32474
32475 @smallexample
32476 (gdb) cont
32477 @end smallexample
32478
32479 @end enumerate
32480
32481 @noindent
32482 This last step will resume the process execution, and stop at
32483 the breakpoint we have set. From there you can use the standard
32484 approach to debug a program as described in
32485 (@pxref{Running and Debugging Ada Programs}).
32486
32487 @node Setting Stack Size from gnatlink
32488 @section Setting Stack Size from @command{gnatlink}
32489
32490 @noindent
32491 It is possible to specify the program stack size at link time. On modern
32492 versions of Windows, starting with XP, this is mostly useful to set the size of
32493 the main stack (environment task). The other task stacks are set with pragma
32494 Storage_Size or with the @command{gnatbind -d} command.
32495
32496 Since older versions of Windows (2000, NT4, etc.) do not allow setting the
32497 reserve size of individual tasks, the link-time stack size applies to all
32498 tasks, and pragma Storage_Size has no effect.
32499 In particular, Stack Overflow checks are made against this
32500 link-time specified size.
32501
32502 This setting can be done with
32503 @command{gnatlink} using either:
32504
32505 @itemize @bullet
32506
32507 @item using @option{-Xlinker} linker option
32508
32509 @smallexample
32510 $ gnatlink hello -Xlinker --stack=0x10000,0x1000
32511 @end smallexample
32512
32513 This sets the stack reserve size to 0x10000 bytes and the stack commit
32514 size to 0x1000 bytes.
32515
32516 @item using @option{-Wl} linker option
32517
32518 @smallexample
32519 $ gnatlink hello -Wl,--stack=0x1000000
32520 @end smallexample
32521
32522 This sets the stack reserve size to 0x1000000 bytes. Note that with
32523 @option{-Wl} option it is not possible to set the stack commit size
32524 because the coma is a separator for this option.
32525
32526 @end itemize
32527
32528 @node Setting Heap Size from gnatlink
32529 @section Setting Heap Size from @command{gnatlink}
32530
32531 @noindent
32532 Under Windows systems, it is possible to specify the program heap size from
32533 @command{gnatlink} using either:
32534
32535 @itemize @bullet
32536
32537 @item using @option{-Xlinker} linker option
32538
32539 @smallexample
32540 $ gnatlink hello -Xlinker --heap=0x10000,0x1000
32541 @end smallexample
32542
32543 This sets the heap reserve size to 0x10000 bytes and the heap commit
32544 size to 0x1000 bytes.
32545
32546 @item using @option{-Wl} linker option
32547
32548 @smallexample
32549 $ gnatlink hello -Wl,--heap=0x1000000
32550 @end smallexample
32551
32552 This sets the heap reserve size to 0x1000000 bytes. Note that with
32553 @option{-Wl} option it is not possible to set the heap commit size
32554 because the coma is a separator for this option.
32555
32556 @end itemize
32557
32558 @end ifset
32559
32560 @c **********************************
32561 @c * GNU Free Documentation License *
32562 @c **********************************
32563 @include fdl.texi
32564 @c GNU Free Documentation License
32565
32566 @node Index,,GNU Free Documentation License, Top
32567 @unnumbered Index
32568
32569 @printindex cp
32570
32571 @contents
32572 @c Put table of contents at end, otherwise it precedes the "title page" in
32573 @c the .txt version
32574 @c Edit the pdf file to move the contents to the beginning, after the title
32575 @c page
32576
32577 @bye