OSDN Git Service

2005-11-21 Joel Sherrill <joel.sherrill@oarcorp.com>
[pf3gnuchains/gcc-fork.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                     Copyright (C) 1992-2005, AdaCore                       o
11 @c                                                                            o
12 @c  GNAT is free software;  you can  redistribute it  and/or modify it under  o
13 @c  terms of the  GNU General Public License as published  by the Free Soft-  o
14 @c  ware  Foundation;  either version 2,  or (at your option) any later ver-  o
15 @c  sion.  GNAT is distributed in the hope that it will be useful, but WITH-  o
16 @c  OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY  o
17 @c  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License  o
18 @c  for  more details.  You should have  received  a copy of the GNU General  o
19 @c  Public License  distributed with GNAT;  see file COPYING.  If not, write  o
20 @c  to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor,  o
21 @c  Boston, MA 02110-1301, USA.                                               o
22 @c                                                                            o
23 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
24
25 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
26 @c
27 @c                           GNAT_UGN Style Guide
28 @c
29 @c  1. Always put a @noindent on the line before the first paragraph
30 @c     after any of these commands:
31 @c
32 @c          @chapter
33 @c          @section
34 @c          @subsection
35 @c          @subsubsection
36 @c          @subsubsubsection
37 @c
38 @c          @end smallexample
39 @c          @end itemize
40 @c          @end enumerate
41 @c
42 @c  2. DO NOT use @example. Use @smallexample instead.
43 @c     a) DO NOT use highlighting commands (@b{}, @i{}) inside an @smallexample
44 @c        context.  These can interfere with the readability of the texi
45 @c        source file.  Instead, use one of the following annotated
46 @c        @smallexample commands, and preprocess the texi file with the
47 @c        ada2texi tool (which generates appropriate highlighting):
48 @c        @smallexample @c ada
49 @c        @smallexample @c adanocomment
50 @c        @smallexample @c projectfile
51 @c     b) The "@c ada" markup will result in boldface for reserved words
52 @c        and italics for comments
53 @c     c) The "@c adanocomment" markup will result only in boldface for
54 @c        reserved words (comments are left alone)
55 @c     d) The "@c projectfile" markup is like "@c ada" except that the set
56 @c        of reserved words include the new reserved words for project files
57 @c
58 @c  3. Each @chapter, @section, @subsection, @subsubsection, etc.
59 @c     command must be preceded by two empty lines
60 @c
61 @c  4. The @item command should be on a line of its own if it is in an
62 @c     @itemize or @enumerate command.
63 @c
64 @c  5. When talking about ALI files use "ALI" (all uppercase), not "Ali"
65 @c     or "ali".
66 @c
67 @c  6. DO NOT put trailing spaces at the end of a line.  Such spaces will
68 @c     cause the document build to fail.
69 @c
70 @c  7. DO NOT use @cartouche for examples that are longer than around 10 lines.
71 @c     This command inhibits page breaks, so long examples in a @cartouche can
72 @c     lead to large, ugly patches of empty space on a page.
73 @c
74 @c  NOTE: This file should be submitted to xgnatugn with either the vms flag
75 @c        or the unw flag set.  The unw flag covers topics for both Unix and
76 @c        Windows.
77 @c
78 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
79
80 @ifset vms
81 @setfilename gnat_ugn_vms.info
82 @end ifset
83
84 @ifset unw
85 @setfilename gnat_ugn_unw.info
86 @end ifset
87
88 @set FSFEDITION
89 @set EDITION GNAT
90
91 @ifset unw
92 @set PLATFORM
93 @set FILE gnat_ugn_unw
94 @end ifset
95
96 @ifset vms
97 @set PLATFORM OpenVMS
98 @set FILE gnat_ugn_vms
99 @end ifset
100
101 @settitle @value{EDITION} User's Guide @value{PLATFORM}
102 @dircategory GNU Ada tools
103 @direntry
104 * @value{EDITION} User's Guide (@value{FILE}) @value{PLATFORM}
105 @end direntry
106
107 @include gcc-common.texi
108
109 @setchapternewpage odd
110 @syncodeindex fn cp
111 @c %**end of header
112
113 @copying
114 Copyright @copyright{} 1995-2005, Free Software Foundation
115
116 Permission is granted to copy, distribute and/or modify this document
117 under the terms of the GNU Free Documentation License, Version 1.2
118 or any later version published by the Free Software Foundation;
119 with the Invariant Sections being ``GNU Free Documentation License'', with the
120 Front-Cover Texts being
121 ``@value{EDITION} User's Guide'',
122 and with no Back-Cover Texts.
123 A copy of the license is included in the section entitled
124 ``GNU Free Documentation License''.
125 @end copying
126
127 @titlepage
128
129 @title @value{EDITION} User's Guide
130
131 @ifset vms
132 @sp 1
133 @flushright
134 @titlefont{@i{@value{PLATFORM}}}
135 @end flushright
136 @end ifset
137
138 @sp 2
139
140 @subtitle GNAT, The GNU Ada 95 Compiler
141 @subtitle GCC version @value{version-GCC}
142
143 @author AdaCore
144
145 @page
146 @vskip 0pt plus 1filll
147
148 @insertcopying
149
150 @end titlepage
151
152 @ifnottex
153 @node Top, About This Guide, (dir), (dir)
154 @top @value{EDITION} User's Guide
155
156 @noindent
157 @value{EDITION} User's Guide @value{PLATFORM}
158
159 @noindent
160 GNAT, The GNU Ada 95 Compiler@*
161 GCC version @value{version-GCC}@*
162
163 @noindent
164 AdaCore@*
165
166 @menu
167 * About This Guide::
168 * Getting Started with GNAT::
169 * The GNAT Compilation Model::
170 * Compiling Using gcc::
171 * Binding Using gnatbind::
172 * Linking Using gnatlink::
173 * The GNAT Make Program gnatmake::
174 * Improving Performance::
175 * Renaming Files Using gnatchop::
176 * Configuration Pragmas::
177 * Handling Arbitrary File Naming Conventions Using gnatname::
178 * GNAT Project Manager::
179 * The Cross-Referencing Tools gnatxref and gnatfind::
180 * The GNAT Pretty-Printer gnatpp::
181 * The GNAT Metric Tool gnatmetric::
182 * File Name Krunching Using gnatkr::
183 * Preprocessing Using gnatprep::
184 @ifset vms
185 * The GNAT Run-Time Library Builder gnatlbr::
186 @end ifset
187 * The GNAT Library Browser gnatls::
188 * Cleaning Up Using gnatclean::
189 @ifclear vms
190 * GNAT and Libraries::
191 * Using the GNU make Utility::
192 @end ifclear
193 * Memory Management Issues::
194 * Creating Sample Bodies Using gnatstub::
195 * Other Utility Programs::
196 * Running and Debugging Ada Programs::
197 @ifset vms
198 * Compatibility with HP Ada::
199 @end ifset
200 * Platform-Specific Information for the Run-Time Libraries::
201 * Example of Binder Output File::
202 * Elaboration Order Handling in GNAT::
203 * Inline Assembler::
204 * Compatibility and Porting Guide::
205 @ifset unw
206 * Microsoft Windows Topics::
207 @end ifset
208 * GNU Free Documentation License::
209 * Index::
210
211  --- The Detailed Node Listing ---
212
213 About This Guide
214
215 * What This Guide Contains::
216 * What You Should Know before Reading This Guide::
217 * Related Information::
218 * Conventions::
219
220 Getting Started with GNAT
221
222 * Running GNAT::
223 * Running a Simple Ada Program::
224 * Running a Program with Multiple Units::
225 * Using the gnatmake Utility::
226 @ifset vms
227 * Editing with Emacs::
228 @end ifset
229 @ifclear vms
230 * Introduction to GPS::
231 * Introduction to Glide and GVD::
232 @end ifclear
233
234 The GNAT Compilation Model
235
236 * Source Representation::
237 * Foreign Language Representation::
238 * File Naming Rules::
239 * Using Other File Names::
240 * Alternative File Naming Schemes::
241 * Generating Object Files::
242 * Source Dependencies::
243 * The Ada Library Information Files::
244 * Binding an Ada Program::
245 * Mixed Language Programming::
246 * Building Mixed Ada & C++ Programs::
247 * Comparison between GNAT and C/C++ Compilation Models::
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 * Stack Overflow Checking::
276 * Using gcc for Syntax Checking::
277 * Using gcc for Semantic Checking::
278 * Compiling Different Versions of Ada::
279 * Character Set Control::
280 * File Naming Control::
281 * Subprogram Inlining Control::
282 * Auxiliary Output Control::
283 * Debugging Control::
284 * Exception Handling Control::
285 * Units to Sources Mapping Files::
286 * Integrated Preprocessing::
287 @ifset vms
288 * Return Codes::
289 @end ifset
290
291 Binding Ada Programs With gnatbind
292
293 * Running gnatbind::
294 * Switches for gnatbind::
295 * Command-Line Access::
296 * Search Paths for gnatbind::
297 * Examples of gnatbind Usage::
298
299 Switches for gnatbind
300
301 * Consistency-Checking Modes::
302 * Binder Error Message Control::
303 * Elaboration Control::
304 * Output Control::
305 * Binding with Non-Ada Main Programs::
306 * Binding Programs with No Main Subprogram::
307
308 Linking Using gnatlink
309
310 * Running gnatlink::
311 * Switches for gnatlink::
312 * Setting Stack Size from gnatlink::
313 * Setting Heap Size from gnatlink::
314
315 The GNAT Make Program gnatmake
316
317 * Running gnatmake::
318 * Switches for gnatmake::
319 * Mode Switches for gnatmake::
320 * Notes on the Command Line::
321 * How gnatmake Works::
322 * Examples of gnatmake Usage::
323
324 Improving Performance
325 * Performance Considerations::
326 * Reducing the Size of Ada Executables with gnatelim::
327
328 Performance Considerations
329 * Controlling Run-Time Checks::
330 * Use of Restrictions::
331 * Optimization Levels::
332 * Debugging Optimized Code::
333 * Inlining of Subprograms::
334 * Other Optimization Switches::
335 * Optimization and Strict Aliasing::
336 @ifset vms
337 * Coverage Analysis::
338 @end ifset
339
340 Reducing the Size of Ada Executables with gnatelim
341 * About gnatelim::
342 * Running gnatelim::
343 * Correcting the List of Eliminate Pragmas::
344 * Making Your Executables Smaller::
345 * Summary of the gnatelim Usage Cycle::
346
347 Renaming Files Using gnatchop
348
349 * Handling Files with Multiple Units::
350 * Operating gnatchop in Compilation Mode::
351 * Command Line for gnatchop::
352 * Switches for gnatchop::
353 * Examples of gnatchop Usage::
354
355 Configuration Pragmas
356
357 * Handling of Configuration Pragmas::
358 * The Configuration Pragmas Files::
359
360 Handling Arbitrary File Naming Conventions Using gnatname
361
362 * Arbitrary File Naming Conventions::
363 * Running gnatname::
364 * Switches for gnatname::
365 * Examples of gnatname Usage::
366
367 GNAT Project Manager
368
369 * Introduction::
370 * Examples of Project Files::
371 * Project File Syntax::
372 * Objects and Sources in Project Files::
373 * Importing Projects::
374 * Project Extension::
375 * Project Hierarchy Extension::
376 * External References in Project Files::
377 * Packages in Project Files::
378 * Variables from Imported Projects::
379 * Naming Schemes::
380 * Library Projects::
381 * Stand-alone Library Projects::
382 * Switches Related to Project Files::
383 * Tools Supporting Project Files::
384 * An Extended Example::
385 * Project File Complete Syntax::
386
387 The Cross-Referencing Tools gnatxref and gnatfind
388
389 * gnatxref Switches::
390 * gnatfind Switches::
391 * Project Files for gnatxref and gnatfind::
392 * Regular Expressions in gnatfind and gnatxref::
393 * Examples of gnatxref Usage::
394 * Examples of gnatfind Usage::
395
396 The GNAT Pretty-Printer gnatpp
397
398 * Switches for gnatpp::
399 * Formatting Rules::
400
401 The GNAT Metrics Tool gnatmetric
402
403 * Switches for gnatmetric::
404
405 File Name Krunching Using gnatkr
406
407 * About gnatkr::
408 * Using gnatkr::
409 * Krunching Method::
410 * Examples of gnatkr Usage::
411
412 Preprocessing Using gnatprep
413
414 * Using gnatprep::
415 * Switches for gnatprep::
416 * Form of Definitions File::
417 * Form of Input Text for gnatprep::
418
419 @ifset vms
420 The GNAT Run-Time Library Builder gnatlbr
421
422 * Running gnatlbr::
423 * Switches for gnatlbr::
424 * Examples of gnatlbr Usage::
425 @end ifset
426
427 The GNAT Library Browser gnatls
428
429 * Running gnatls::
430 * Switches for gnatls::
431 * Examples of gnatls Usage::
432
433 Cleaning Up Using gnatclean
434
435 * Running gnatclean::
436 * Switches for gnatclean::
437 @c * Examples of gnatclean Usage::
438
439 @ifclear vms
440
441 GNAT and Libraries
442
443 * Introduction to Libraries in GNAT::
444 * General Ada Libraries::
445 * Stand-alone Ada Libraries::
446 * Rebuilding the GNAT Run-Time Library::
447
448 Using the GNU make Utility
449
450 * Using gnatmake in a Makefile::
451 * Automatically Creating a List of Directories::
452 * Generating the Command Line Switches::
453 * Overcoming Command Line Length Limits::
454 @end ifclear
455
456 Memory Management Issues
457
458 * Some Useful Memory Pools::
459 * The GNAT Debug Pool Facility::
460 @ifclear vms
461 * The gnatmem Tool::
462 @end ifclear
463
464 Some Useful Memory Pools
465
466 The GNAT Debug Pool Facility
467
468 @ifclear vms
469 The gnatmem Tool
470
471 * Running gnatmem::
472 * Switches for gnatmem::
473 * Example of gnatmem Usage::
474 @end ifclear
475
476  Sample Bodies Using gnatstub
477
478 * Running gnatstub::
479 * Switches for gnatstub::
480
481 Other Utility Programs
482
483 * Using Other Utility Programs with GNAT::
484 * The External Symbol Naming Scheme of GNAT::
485 @ifclear vms
486 * Ada Mode for Glide::
487 @end ifclear
488 * Converting Ada Files to html with gnathtml::
489
490 Running and Debugging Ada Programs
491
492 * The GNAT Debugger GDB::
493 * Running GDB::
494 * Introduction to GDB Commands::
495 * Using Ada Expressions::
496 * Calling User-Defined Subprograms::
497 * Using the Next Command in a Function::
498 * Ada Exceptions::
499 * Ada Tasks::
500 * Debugging Generic Units::
501 * GNAT Abnormal Termination or Failure to Terminate::
502 * Naming Conventions for GNAT Source Files::
503 * Getting Internal Debugging Information::
504 * Stack Traceback::
505
506 @ifset vms
507 * LSE::
508 @end ifset
509
510 @ifset vms
511 Compatibility with HP Ada
512
513 * Ada 95 Compatibility::
514 * Differences in the Definition of Package System::
515 * Language-Related Features::
516 * The Package STANDARD::
517 * The Package SYSTEM::
518 * Tasking and Task-Related Features::
519 * Implementation of Tasks in HP Ada for OpenVMS Alpha Systems::
520 * Pragmas and Pragma-Related Features::
521 * Library of Predefined Units::
522 * Bindings::
523 * Main Program Definition::
524 * Implementation-Defined Attributes::
525 * Compiler and Run-Time Interfacing::
526 * Program Compilation and Library Management::
527 * Input-Output::
528 * Implementation Limits::
529
530 Language-Related Features
531
532 * Integer Types and Representations::
533 * Floating-Point Types and Representations::
534 * Pragmas Float_Representation and Long_Float::
535 * Fixed-Point Types and Representations::
536 * Record and Array Component Alignment::
537 * Address Clauses::
538 * Other Representation Clauses::
539
540 Implementation of Tasks in HP Ada for OpenVMS Alpha Systems
541
542 * Assigning Task IDs::
543 * Task IDs and Delays::
544 * Task-Related Pragmas::
545 * Scheduling and Task Priority::
546 * The Task Stack::
547 * External Interrupts::
548
549 Pragmas and Pragma-Related Features
550
551 * Restrictions on the Pragma INLINE::
552 * Restrictions on the Pragma INTERFACE::
553 * Restrictions on the Pragma SYSTEM_NAME::
554
555 Library of Predefined Units
556
557 * Changes to DECLIB::
558
559 Bindings
560
561 * Shared Libraries and Options Files::
562 * Interfaces to C::
563 @end ifset
564
565 Platform-Specific Information for the Run-Time Libraries
566
567 * Summary of Run-Time Configurations::
568 * Specifying a Run-Time Library::
569 * Choosing the Scheduling Policy::
570 * Solaris-Specific Considerations::
571 * IRIX-Specific Considerations::
572 * Linux-Specific Considerations::
573 * AIX-Specific Considerations::
574
575 Example of Binder Output File
576
577 Elaboration Order Handling in GNAT
578
579 * Elaboration Code in Ada 95::
580 * Checking the Elaboration Order in Ada 95::
581 * Controlling the Elaboration Order in Ada 95::
582 * Controlling Elaboration in GNAT - Internal Calls::
583 * Controlling Elaboration in GNAT - External Calls::
584 * Default Behavior in GNAT - Ensuring Safety::
585 * Treatment of Pragma Elaborate::
586 * Elaboration Issues for Library Tasks::
587 * Mixing Elaboration Models::
588 * What to Do If the Default Elaboration Behavior Fails::
589 * Elaboration for Access-to-Subprogram Values::
590 * Summary of Procedures for Elaboration Control::
591 * Other Elaboration Order Considerations::
592
593 Inline Assembler
594
595 * Basic Assembler Syntax::
596 * A Simple Example of Inline Assembler::
597 * Output Variables in Inline Assembler::
598 * Input Variables in Inline Assembler::
599 * Inlining Inline Assembler Code::
600 * Other Asm Functionality::
601
602 Compatibility and Porting Guide
603
604 * Compatibility with Ada 83::
605 * Implementation-dependent characteristics::
606 * Compatibility with HP Ada 83::
607 * Compatibility with Other Ada 95 Systems::
608 * Representation Clauses::
609 @ifset vms
610 * Transitioning from Alpha to Integrity OpenVMS::
611 @end ifset
612
613 @ifset unw
614 Microsoft Windows Topics
615
616 * Using GNAT on Windows::
617 * CONSOLE and WINDOWS subsystems::
618 * Temporary Files::
619 * Mixed-Language Programming on Windows::
620 * Windows Calling Conventions::
621 * Introduction to Dynamic Link Libraries (DLLs)::
622 * Using DLLs with GNAT::
623 * Building DLLs with GNAT::
624 * GNAT and Windows Resources::
625 * Debugging a DLL::
626 * GNAT and COM/DCOM Objects::
627 @end ifset
628
629 * Index::
630 @end menu
631 @end ifnottex
632
633 @node About This Guide
634 @unnumbered About This Guide
635
636 @noindent
637 @ifset vms
638 This guide describes the use of @value{EDITION},
639 a full language compiler for the Ada
640 95 programming language, implemented on HP's Alpha and
641 Integrity (ia64) OpenVMS platforms.
642 @end ifset
643 @ifclear vms
644 This guide describes the use of @value{EDITION},
645 a compiler and software development
646 toolset for the full Ada 95 programming language.
647 @end ifclear
648 It describes the features of the compiler and tools, and details
649 how to use them to build Ada 95 applications.
650
651 @ifset PROEDITION
652 For ease of exposition, ``GNAT Pro'' will be referred to simply as
653 ``GNAT'' in the remainder of this document.
654 @end ifset
655
656 @menu
657 * What This Guide Contains::
658 * What You Should Know before Reading This Guide::
659 * Related Information::
660 * Conventions::
661 @end menu
662
663 @node What This Guide Contains
664 @unnumberedsec What This Guide Contains
665
666 @noindent
667 This guide contains the following chapters:
668 @itemize @bullet
669
670 @item
671 @ref{Getting Started with GNAT}, describes how to get started compiling
672 and running Ada programs with the GNAT Ada programming environment.
673 @item
674 @ref{The GNAT Compilation Model}, describes the compilation model used
675 by GNAT.
676
677 @item
678 @ref{Compiling Using gcc}, describes how to compile
679 Ada programs with @command{gcc}, the Ada compiler.
680
681 @item
682 @ref{Binding Using gnatbind}, describes how to
683 perform binding of Ada programs with @code{gnatbind}, the GNAT binding
684 utility.
685
686 @item
687 @ref{Linking Using gnatlink},
688 describes @command{gnatlink}, a
689 program that provides for linking using the GNAT run-time library to
690 construct a program. @command{gnatlink} can also incorporate foreign language
691 object units into the executable.
692
693 @item
694 @ref{The GNAT Make Program gnatmake}, describes @command{gnatmake}, a
695 utility that automatically determines the set of sources
696 needed by an Ada compilation unit, and executes the necessary compilations
697 binding and link.
698
699 @item
700 @ref{Improving Performance}, shows various techniques for making your
701 Ada program run faster or take less space.
702 It discusses the effect of the compiler's optimization switch and
703 also describes the @command{gnatelim} tool.
704
705 @item
706 @ref{Renaming Files Using gnatchop}, describes
707 @code{gnatchop}, a utility that allows you to preprocess a file that
708 contains Ada source code, and split it into one or more new files, one
709 for each compilation unit.
710
711 @item
712 @ref{Configuration Pragmas}, describes the configuration pragmas
713 handled by GNAT.
714
715 @item
716 @ref{Handling Arbitrary File Naming Conventions Using gnatname},
717 shows how to override the default GNAT file naming conventions,
718 either for an individual unit or globally.
719
720 @item
721 @ref{GNAT Project Manager}, describes how to use project files
722 to organize large projects.
723
724 @item
725 @ref{The Cross-Referencing Tools gnatxref and gnatfind}, discusses
726 @code{gnatxref} and @code{gnatfind}, two tools that provide an easy
727 way to navigate through sources.
728
729 @item
730 @ref{The GNAT Pretty-Printer gnatpp}, shows how to produce a reformatted
731 version of an Ada source file with control over casing, indentation,
732 comment placement, and other elements of program presentation style.
733
734 @item
735 @ref{The GNAT Metric Tool gnatmetric}, shows how to compute various
736 metrics for an Ada source file, such as the number of types and subprograms,
737 and assorted complexity measures.
738
739 @item
740 @ref{File Name Krunching Using gnatkr}, describes the @code{gnatkr}
741 file name krunching utility, used to handle shortened
742 file names on operating systems with a limit on the length of names.
743
744 @item
745 @ref{Preprocessing Using gnatprep}, describes @code{gnatprep}, a
746 preprocessor utility that allows a single source file to be used to
747 generate multiple or parameterized source files, by means of macro
748 substitution.
749
750 @ifset vms
751 @item
752 @ref{The GNAT Run-Time Library Builder gnatlbr}, describes @command{gnatlbr},
753 a tool for rebuilding the GNAT run time with user-supplied
754 configuration pragmas.
755 @end ifset
756
757 @item
758 @ref{The GNAT Library Browser gnatls}, describes @code{gnatls}, a
759 utility that displays information about compiled units, including dependences
760 on the corresponding sources files, and consistency of compilations.
761
762 @item
763 @ref{Cleaning Up Using gnatclean}, describes @code{gnatclean}, a utility
764 to delete files that are produced by the compiler, binder and linker.
765
766 @ifclear vms
767 @item
768 @ref{GNAT and Libraries}, describes the process of creating and using
769 Libraries with GNAT. It also describes how to recompile the GNAT run-time
770 library.
771
772 @item
773 @ref{Using the GNU make Utility}, describes some techniques for using
774 the GNAT toolset in Makefiles.
775 @end ifclear
776
777 @item
778 @ref{Memory Management Issues}, describes some useful predefined storage pools
779 and in particular the GNAT Debug Pool facility, which helps detect incorrect
780 memory references.
781 @ifclear vms
782 It also describes @command{gnatmem}, a utility that monitors dynamic
783 allocation and deallocation and helps detect ``memory leaks''.
784 @end ifclear
785
786 @item
787 @ref{Creating Sample Bodies Using gnatstub}, discusses @code{gnatstub},
788 a utility that generates empty but compilable bodies for library units.
789
790 @item
791 @ref{Other Utility Programs}, discusses several other GNAT utilities,
792 including @code{gnathtml}.
793
794 @item
795 @ref{Running and Debugging Ada Programs}, describes how to run and debug
796 Ada programs.
797
798 @ifset vms
799 @item
800 @ref{Compatibility with HP Ada}, details the compatibility of GNAT with
801 HP Ada 83 @footnote{``HP Ada'' refers to the legacy product originally
802 developed by Digital Equipment Corporation and currently supported by HP.}
803 for OpenVMS Alpha. This product was formerly known as DEC Ada, and for
804 historical compatibility reasons, the relevant libraries still use the
805 DEC prefix.
806 @end ifset
807
808 @item
809 @ref{Platform-Specific Information for the Run-Time Libraries},
810 describes the various run-time
811 libraries supported by GNAT on various platforms and explains how to
812 choose a particular library.
813
814 @item
815 @ref{Example of Binder Output File}, shows the source code for the binder
816 output file for a sample program.
817
818 @item
819 @ref{Elaboration Order Handling in GNAT}, describes how GNAT helps
820 you deal with elaboration order issues.
821
822 @item
823 @ref{Inline Assembler}, shows how to use the inline assembly facility
824 in an Ada program.
825
826 @item
827 @ref{Compatibility and Porting Guide}, includes sections on compatibility
828 of GNAT with other Ada 83 and Ada 95 compilation systems, to assist
829 in porting code from other environments.
830
831 @ifset unw
832 @item
833 @ref{Microsoft Windows Topics}, presents information relevant to the
834 Microsoft Windows platform.
835 @end ifset
836 @end itemize
837
838 @c *************************************************
839 @node What You Should Know before Reading This Guide
840 @c *************************************************
841 @unnumberedsec What You Should Know before Reading This Guide
842
843 @cindex Ada 95 Language Reference Manual
844 @noindent
845 This user's guide assumes that you are familiar with Ada 95 language, as
846 described in the International Standard ANSI/ISO/IEC-8652:1995, January
847 1995.
848
849 @node Related Information
850 @unnumberedsec Related Information
851
852 @noindent
853 For further information about related tools, refer to the following
854 documents:
855
856 @itemize @bullet
857 @item
858 @cite{GNAT Reference Manual}, which contains all reference
859 material for the GNAT implementation of Ada 95.
860
861 @ifset unw
862 @item
863 @cite{Using the GNAT Programming System}, which describes the GPS
864 integrated development environment.
865
866 @item
867 @cite{GNAT Programming System Tutorial}, which introduces the
868 main GPS features through examples.
869 @end ifset
870
871 @item
872 @cite{Ada 95 Language Reference Manual}, which contains all reference
873 material for the Ada 95 programming language.
874
875 @item
876 @cite{Debugging with GDB}
877 @ifset vms
878 , located in the GNU:[DOCS] directory,
879 @end ifset
880 contains all details on the use of the GNU source-level debugger.
881
882 @item
883 @cite{GNU Emacs Manual}
884 @ifset vms
885 , located in the GNU:[DOCS] directory if the EMACS kit is installed,
886 @end ifset
887 contains full information on the extensible editor and programming
888 environment Emacs.
889
890 @end itemize
891
892 @c **************
893 @node Conventions
894 @unnumberedsec Conventions
895 @cindex Conventions
896 @cindex Typographical conventions
897
898 @noindent
899 Following are examples of the typographical and graphic conventions used
900 in this guide:
901
902 @itemize @bullet
903 @item
904 @code{Functions}, @code{utility program names}, @code{standard names},
905 and @code{classes}.
906
907 @item
908 @samp{Option flags}
909
910 @item
911 @file{File Names}, @file{button names}, and @file{field names}.
912
913 @item
914 @var{Variables}.
915
916 @item
917 @emph{Emphasis}.
918
919 @item
920 [optional information or parameters]
921
922 @item
923 Examples are described by text
924 @smallexample
925 and then shown this way.
926 @end smallexample
927 @end itemize
928
929 @noindent
930 Commands that are entered by the user are preceded in this manual by the
931 characters @w{``@code{$ }''} (dollar sign followed by space). If your system
932 uses this sequence as a prompt, then the commands will appear exactly as
933 you see them in the manual. If your system uses some other prompt, then
934 the command will appear with the @code{$} replaced by whatever prompt
935 character you are using.
936
937 @ifset unw
938 Full file names are shown with the ``@code{/}'' character
939 as the directory separator; e.g., @file{parent-dir/subdir/myfile.adb}.
940 If you are using GNAT on a Windows platform, please note that
941 the ``@code{\}'' character should be used instead.
942 @end ifset
943
944 @c ****************************
945 @node Getting Started with GNAT
946 @chapter Getting Started with GNAT
947
948 @noindent
949 This chapter describes some simple ways of using GNAT to build
950 executable Ada programs.
951 @ifset unw
952 @ref{Running GNAT}, through @ref{Using the gnatmake Utility},
953 show how to use the command line environment.
954 @ref{Introduction to Glide and GVD}, provides a brief
955 introduction to the visually-oriented IDE for GNAT.
956 Supplementing Glide on some platforms is GPS, the
957 GNAT Programming System, which offers a richer graphical
958 ``look and feel'', enhanced configurability, support for
959 development in other programming language, comprehensive
960 browsing features, and many other capabilities.
961 For information on GPS please refer to
962 @cite{Using the GNAT Programming System}.
963 @end ifset
964
965 @menu
966 * Running GNAT::
967 * Running a Simple Ada Program::
968 * Running a Program with Multiple Units::
969 * Using the gnatmake Utility::
970 @ifset vms
971 * Editing with Emacs::
972 @end ifset
973 @ifclear vms
974 * Introduction to GPS::
975 * Introduction to Glide and GVD::
976 @end ifclear
977 @end menu
978
979 @node Running GNAT
980 @section Running GNAT
981
982 @noindent
983 Three steps are needed to create an executable file from an Ada source
984 file:
985
986 @enumerate
987 @item
988 The source file(s) must be compiled.
989 @item
990 The file(s) must be bound using the GNAT binder.
991 @item
992 All appropriate object files must be linked to produce an executable.
993 @end enumerate
994
995 @noindent
996 All three steps are most commonly handled by using the @command{gnatmake}
997 utility program that, given the name of the main program, automatically
998 performs the necessary compilation, binding and linking steps.
999
1000 @node Running a Simple Ada Program
1001 @section Running a Simple Ada Program
1002
1003 @noindent
1004 Any text editor may be used to prepare an Ada program.
1005 @ifclear vms
1006 If @code{Glide} is
1007 used, the optional Ada mode may be helpful in laying out the program.
1008 @end ifclear
1009 The
1010 program text is a normal text file. We will suppose in our initial
1011 example that you have used your editor to prepare the following
1012 standard format text file:
1013
1014 @smallexample @c ada
1015 @cartouche
1016 with Ada.Text_IO; use Ada.Text_IO;
1017 procedure Hello is
1018 begin
1019    Put_Line ("Hello WORLD!");
1020 end Hello;
1021 @end cartouche
1022 @end smallexample
1023
1024 @noindent
1025 This file should be named @file{hello.adb}.
1026 With the normal default file naming conventions, GNAT requires
1027 that each file
1028 contain a single compilation unit whose file name is the
1029 unit name,
1030 with periods replaced by hyphens; the
1031 extension is @file{ads} for a
1032 spec and @file{adb} for a body.
1033 You can override this default file naming convention by use of the
1034 special pragma @code{Source_File_Name} (@pxref{Using Other File Names}).
1035 Alternatively, if you want to rename your files according to this default
1036 convention, which is probably more convenient if you will be using GNAT
1037 for all your compilations, then the @code{gnatchop} utility
1038 can be used to generate correctly-named source files
1039 (@pxref{Renaming Files Using gnatchop}).
1040
1041 You can compile the program using the following command (@code{$} is used
1042 as the command prompt in the examples in this document):
1043
1044 @smallexample
1045 $ gcc -c hello.adb
1046 @end smallexample
1047
1048 @noindent
1049 @command{gcc} is the command used to run the compiler. This compiler is
1050 capable of compiling programs in several languages, including Ada 95 and
1051 C. It assumes that you have given it an Ada program if the file extension is
1052 either @file{.ads} or @file{.adb}, and it will then call
1053 the GNAT compiler to compile the specified file.
1054
1055 @ifclear vms
1056 The @option{-c} switch is required. It tells @command{gcc} to only do a
1057 compilation. (For C programs, @command{gcc} can also do linking, but this
1058 capability is not used directly for Ada programs, so the @option{-c}
1059 switch must always be present.)
1060 @end ifclear
1061
1062 This compile command generates a file
1063 @file{hello.o}, which is the object
1064 file corresponding to your Ada program. It also generates
1065 an ``Ada Library Information'' file @file{hello.ali},
1066 which contains additional information used to check
1067 that an Ada program is consistent.
1068 To build an executable file,
1069 use @code{gnatbind} to bind the program
1070 and @command{gnatlink} to link it. The
1071 argument to both @code{gnatbind} and @command{gnatlink} is the name of the
1072 @file{ALI} file, but the default extension of @file{.ali} can
1073 be omitted. This means that in the most common case, the argument
1074 is simply the name of the main program:
1075
1076 @smallexample
1077 $ gnatbind hello
1078 $ gnatlink hello
1079 @end smallexample
1080
1081 @noindent
1082 A simpler method of carrying out these steps is to use
1083 @command{gnatmake},
1084 a master program that invokes all the required
1085 compilation, binding and linking tools in the correct order. In particular,
1086 @command{gnatmake} automatically recompiles any sources that have been
1087 modified since they were last compiled, or sources that depend
1088 on such modified sources, so that ``version skew'' is avoided.
1089 @cindex Version skew (avoided by @command{gnatmake})
1090
1091 @smallexample
1092 $ gnatmake hello.adb
1093 @end smallexample
1094
1095 @noindent
1096 The result is an executable program called @file{hello}, which can be
1097 run by entering:
1098
1099 @smallexample
1100 $ ^hello^RUN HELLO^
1101 @end smallexample
1102
1103 @noindent
1104 assuming that the current directory is on the search path
1105 for executable programs.
1106
1107 @noindent
1108 and, if all has gone well, you will see
1109
1110 @smallexample
1111 Hello WORLD!
1112 @end smallexample
1113
1114 @noindent
1115 appear in response to this command.
1116
1117 @c ****************************************
1118 @node Running a Program with Multiple Units
1119 @section Running a Program with Multiple Units
1120
1121 @noindent
1122 Consider a slightly more complicated example that has three files: a
1123 main program, and the spec and body of a package:
1124
1125 @smallexample @c ada
1126 @cartouche
1127 @group
1128 package Greetings is
1129    procedure Hello;
1130    procedure Goodbye;
1131 end Greetings;
1132
1133 with Ada.Text_IO; use Ada.Text_IO;
1134 package body Greetings is
1135    procedure Hello is
1136    begin
1137       Put_Line ("Hello WORLD!");
1138    end Hello;
1139
1140    procedure Goodbye is
1141    begin
1142       Put_Line ("Goodbye WORLD!");
1143    end Goodbye;
1144 end Greetings;
1145 @end group
1146
1147 @group
1148 with Greetings;
1149 procedure Gmain is
1150 begin
1151    Greetings.Hello;
1152    Greetings.Goodbye;
1153 end Gmain;
1154 @end group
1155 @end cartouche
1156 @end smallexample
1157
1158 @noindent
1159 Following the one-unit-per-file rule, place this program in the
1160 following three separate files:
1161
1162 @table @file
1163 @item greetings.ads
1164 spec of package @code{Greetings}
1165
1166 @item greetings.adb
1167 body of package @code{Greetings}
1168
1169 @item gmain.adb
1170 body of main program
1171 @end table
1172
1173 @noindent
1174 To build an executable version of
1175 this program, we could use four separate steps to compile, bind, and link
1176 the program, as follows:
1177
1178 @smallexample
1179 $ gcc -c gmain.adb
1180 $ gcc -c greetings.adb
1181 $ gnatbind gmain
1182 $ gnatlink gmain
1183 @end smallexample
1184
1185 @noindent
1186 Note that there is no required order of compilation when using GNAT.
1187 In particular it is perfectly fine to compile the main program first.
1188 Also, it is not necessary to compile package specs in the case where
1189 there is an accompanying body; you only need to compile the body. If you want
1190 to submit these files to the compiler for semantic checking and not code
1191 generation, then use the
1192 @option{-gnatc} switch:
1193
1194 @smallexample
1195 $ gcc -c greetings.ads -gnatc
1196 @end smallexample
1197
1198 @noindent
1199 Although the compilation can be done in separate steps as in the
1200 above example, in practice it is almost always more convenient
1201 to use the @command{gnatmake} tool. All you need to know in this case
1202 is the name of the main program's source file. The effect of the above four
1203 commands can be achieved with a single one:
1204
1205 @smallexample
1206 $ gnatmake gmain.adb
1207 @end smallexample
1208
1209 @noindent
1210 In the next section we discuss the advantages of using @command{gnatmake} in
1211 more detail.
1212
1213 @c *****************************
1214 @node Using the gnatmake Utility
1215 @section Using the @command{gnatmake} Utility
1216
1217 @noindent
1218 If you work on a program by compiling single components at a time using
1219 @command{gcc}, you typically keep track of the units you modify. In order to
1220 build a consistent system, you compile not only these units, but also any
1221 units that depend on the units you have modified.
1222 For example, in the preceding case,
1223 if you edit @file{gmain.adb}, you only need to recompile that file. But if
1224 you edit @file{greetings.ads}, you must recompile both
1225 @file{greetings.adb} and @file{gmain.adb}, because both files contain
1226 units that depend on @file{greetings.ads}.
1227
1228 @code{gnatbind} will warn you if you forget one of these compilation
1229 steps, so that it is impossible to generate an inconsistent program as a
1230 result of forgetting to do a compilation. Nevertheless it is tedious and
1231 error-prone to keep track of dependencies among units.
1232 One approach to handle the dependency-bookkeeping is to use a
1233 makefile. However, makefiles present maintenance problems of their own:
1234 if the dependencies change as you change the program, you must make
1235 sure that the makefile is kept up-to-date manually, which is also an
1236 error-prone process.
1237
1238 The @command{gnatmake} utility takes care of these details automatically.
1239 Invoke it using either one of the following forms:
1240
1241 @smallexample
1242 $ gnatmake gmain.adb
1243 $ gnatmake ^gmain^GMAIN^
1244 @end smallexample
1245
1246 @noindent
1247 The argument is the name of the file containing the main program;
1248 you may omit the extension. @command{gnatmake}
1249 examines the environment, automatically recompiles any files that need
1250 recompiling, and binds and links the resulting set of object files,
1251 generating the executable file, @file{^gmain^GMAIN.EXE^}.
1252 In a large program, it
1253 can be extremely helpful to use @command{gnatmake}, because working out by hand
1254 what needs to be recompiled can be difficult.
1255
1256 Note that @command{gnatmake}
1257 takes into account all the Ada 95 rules that
1258 establish dependencies among units. These include dependencies that result
1259 from inlining subprogram bodies, and from
1260 generic instantiation. Unlike some other
1261 Ada make tools, @command{gnatmake} does not rely on the dependencies that were
1262 found by the compiler on a previous compilation, which may possibly
1263 be wrong when sources change. @command{gnatmake} determines the exact set of
1264 dependencies from scratch each time it is run.
1265
1266 @ifset vms
1267 @node Editing with Emacs
1268 @section Editing with Emacs
1269 @cindex Emacs
1270
1271 @noindent
1272 Emacs is an extensible self-documenting text editor that is available in a
1273 separate VMSINSTAL kit.
1274
1275 Invoke Emacs by typing @kbd{Emacs} at the command prompt. To get started,
1276 click on the Emacs Help menu and run the Emacs Tutorial.
1277 In a character cell terminal, Emacs help is invoked with @kbd{Ctrl-h} (also
1278 written as @kbd{C-h}), and the tutorial by @kbd{C-h t}.
1279
1280 Documentation on Emacs and other tools is available in Emacs under the
1281 pull-down menu button: @code{Help - Info}. After selecting @code{Info},
1282 use the middle mouse button to select a topic (e.g. Emacs).
1283
1284 In a character cell terminal, do @kbd{C-h i} to invoke info, and then @kbd{m}
1285 (stands for menu) followed by the menu item desired, as in @kbd{m Emacs}, to
1286 get to the Emacs manual.
1287 Help on Emacs is also available by typing @kbd{HELP EMACS} at the DCL command
1288 prompt.
1289
1290 The tutorial is highly recommended in order to learn the intricacies of Emacs,
1291 which is sufficiently extensible to provide for a complete programming
1292 environment and shell for the sophisticated user.
1293 @end ifset
1294
1295 @ifclear vms
1296 @node Introduction to GPS
1297 @section Introduction to GPS
1298 @cindex GPS (GNAT Programming System)
1299 @cindex GNAT Programming System (GPS)
1300 @noindent
1301 Although the command line interface (@command{gnatmake}, etc.) alone
1302 is sufficient, a graphical Interactive Development
1303 Environment can make it easier for you to compose, navigate, and debug
1304 programs.  This section describes the main features of GPS
1305 (``GNAT Programming System''), the GNAT graphical IDE.
1306 You will see how to use GPS to build and debug an executable, and
1307 you will also learn some of the basics of the GNAT ``project'' facility.
1308
1309 GPS enables you to do much more than is presented here;
1310 e.g., you can produce a call graph, interface to a third-party
1311 Version Control System, and inspect the generated assembly language
1312 for a program.
1313 Indeed, GPS also supports languages other than Ada.
1314 Such additional information, and an explanation of all of the GPS menu
1315 items. may be found in the on-line help, which includes
1316 a user's guide and a tutorial (these are also accessible from the GNAT
1317 startup menu).
1318
1319 @menu
1320 * Building a New Program with GPS::
1321 * Simple Debugging with GPS::
1322 @end menu
1323
1324 @node Building a New Program with GPS
1325 @subsection Building a New Program with GPS
1326 @noindent
1327 GPS invokes the GNAT compilation tools using information
1328 contained in a @emph{project} (also known as a @emph{project file}):
1329 a collection of properties such
1330 as source directories, identities of main subprograms, tool switches, etc.,
1331 and their associated values.
1332 See @ref{GNAT Project Manager} for details.
1333 In order to run GPS, you will need to either create a new project
1334 or else open an existing one.
1335
1336 This section will explain how you can use GPS to create a project,
1337 to associate Ada source files with a project, and to build and run
1338 programs.
1339
1340 @enumerate
1341 @item @emph{Creating a project}
1342
1343 Invoke GPS, either from the command line or the platform's IDE.
1344 After it starts, GPS will display a ``Welcome'' screen with three
1345 radio buttons:
1346
1347 @itemize @bullet
1348 @item
1349 @code{Start with default project in directory}
1350
1351 @item
1352 @code{Create new project with wizard}
1353
1354 @item
1355 @code{Open existing project}
1356 @end itemize
1357
1358 @noindent
1359 Select @code{Create new project with wizard} and press @code{OK}.
1360 A new window will appear.  In the text box labeled with
1361 @code{Enter the name of the project to create}, type @file{sample}
1362 as the project name.
1363 In the next box, browse to choose the directory in which you
1364 would like to create the project file.
1365 After selecting an appropriate directory, press @code{Forward}.
1366
1367 A window will appear with the title
1368 @code{Version Control System Configuration}.
1369 Simply press @code{Forward}.
1370
1371 A window will appear with the title
1372 @code{Please select the source directories for this project}.
1373 The directory that you specified for the project file will be selected
1374 by default as the one to use for sources; simply press @code{Forward}.
1375
1376 A window will appear with the title
1377 @code{Please select the build directory for this project}.
1378 The directory that you specified for the project file will be selected
1379 by default for object files and executables;
1380 simply press @code{Forward}.
1381
1382 A window will appear with the title
1383 @code{Please select the main units for this project}.
1384 You will supply this information later, after creating the source file.
1385 Simply press @code{Forward} for now.
1386
1387 A window will appear with the title
1388 @code{Please select the switches to build the project}.
1389 Press @code{Apply}.  This will create a project file named
1390 @file{sample.prj} in the directory that you had specified.
1391
1392 @item @emph{Creating and saving the source file}
1393
1394 After you create the new project, a GPS window will appear, which is
1395 partitioned into two main sections:
1396
1397 @itemize @bullet
1398 @item
1399 A @emph{Workspace area}, initially greyed out, which you will use for
1400 creating and editing source files
1401
1402 @item
1403 Directly below, a @emph{Messages area}, which initially displays a
1404 ``Welcome'' message.
1405 (If the Messages area is not visible, drag its border upward to expand it.)
1406 @end itemize
1407
1408 @noindent
1409 Select @code{File} on the menu bar, and then the @code{New} command.
1410 The Workspace area will become white, and you can now
1411 enter the source program explicitly.
1412 Type the following text
1413
1414 @smallexample @c ada
1415 @group
1416 with Ada.Text_IO; use Ada.Text_IO;
1417 procedure Hello is
1418 begin
1419   Put_Line("Hello from GPS!");
1420 end Hello;
1421 @end group
1422 @end smallexample
1423
1424 @noindent
1425 Select @code{File}, then @code{Save As}, and enter the source file name
1426 @file{hello.adb}.
1427 The file will be saved in the same directory you specified as the
1428 location of the default project file.
1429
1430 @item @emph{Updating the project file}
1431
1432 You need to add the new source file to the project.
1433 To do this, select
1434 the @code{Project} menu and then @code{Edit project properties}.
1435 Click the @code{Main files} tab on the left, and then the
1436 @code{Add} button.
1437 Choose @file{hello.adb} from the list, and press @code{Open}.
1438 The project settings window will reflect this action.
1439 Click @code{OK}.
1440
1441 @item @emph{Building and running the program}
1442
1443 In the main GPS window, now choose the @code{Build} menu, then @code{Make},
1444 and select @file{hello.adb}.
1445 The Messages window will display the resulting invocations of @command{gcc},
1446 @command{gnatbind}, and @command{gnatlink}
1447 (reflecting the default switch settings from the
1448 project file that you created) and then a ``successful compilation/build''
1449 message.
1450
1451 To run the program, choose the @code{Build} menu, then @code{Run}, and
1452 select @command{hello}.
1453 An @emph{Arguments Selection} window will appear.
1454 There are no command line arguments, so just click @code{OK}.
1455
1456 The Messages window will now display the program's output (the string
1457 @code{Hello from GPS}), and at the bottom of the GPS window a status
1458 update is displayed (@code{Run: hello}).
1459 Close the GPS window (or select @code{File}, then @code{Exit}) to
1460 terminate this GPS session.
1461 @end enumerate
1462
1463 @node Simple Debugging with GPS
1464 @subsection Simple Debugging with GPS
1465 @noindent
1466 This section illustrates basic debugging techniques (setting breakpoints,
1467 examining/modifying variables, single stepping).
1468
1469 @enumerate
1470 @item @emph{Opening a project}
1471
1472 Start GPS and select @code{Open existing project}; browse to
1473 specify the project file @file{sample.prj} that you had created in the
1474 earlier example.
1475
1476 @item @emph{Creating a source file}
1477
1478 Select @code{File}, then @code{New}, and type in the following program:
1479
1480 @smallexample @c ada
1481 @group
1482 with Ada.Text_IO; use Ada.Text_IO;
1483 procedure Example is
1484    Line : String (1..80);
1485    N    : Natural;
1486 begin
1487    Put_Line("Type a line of text at each prompt; an empty line to exit");
1488    loop
1489       Put(": ");
1490       Get_Line (Line, N);
1491       Put_Line (Line (1..N) );
1492       exit when N=0;
1493    end loop;
1494 end Example;
1495 @end group
1496 @end smallexample
1497
1498 @noindent
1499 Select @code{File}, then @code{Save as}, and enter the file name
1500 @file{example.adb}.
1501
1502 @item @emph{Updating the project file}
1503
1504 Add @code{Example} as a new main unit for the project:
1505 @enumerate a
1506 @item
1507 Select @code{Project}, then @code{Edit Project Properties}.
1508
1509 @item
1510 Select the @code{Main files} tab, click @code{Add}, then
1511 select the file @file{example.adb} from the list, and
1512 click @code{Open}.
1513 You will see the file name appear in the list of main units
1514
1515 @item
1516 Click @code{OK}
1517 @end enumerate
1518
1519 @item @emph{Building/running the executable}
1520
1521 To build the executable
1522 select @code{Build}, then @code{Make}, and then choose @file{example.adb}.
1523
1524 Run the program to see its effect (in the Messages area).
1525 Each line that you enter is displayed; an empty line will
1526 cause the loop to exit and the program to terminate.
1527
1528 @item @emph{Debugging the program}
1529
1530 Note that the @option{-g} switches to @command{gcc} and @command{gnatlink},
1531 which are required for debugging, are on by default when you create
1532 a new project.
1533 Thus unless you intentionally remove these settings, you will be able
1534 to debug any program that you develop using GPS.
1535
1536 @enumerate a
1537 @item @emph{Initializing}
1538
1539 Select @code{Debug}, then @code{Initialize}, then @file{example}
1540
1541 @item @emph{Setting a breakpoint}
1542
1543 After performing the initialization step, you will observe a small
1544 icon to the right of each line number.
1545 This serves as a toggle for breakpoints; clicking the icon will
1546 set a breakpoint at the corresponding line (the icon will change to
1547 a red circle with an ``x''), and clicking it again
1548 will remove the breakpoint / reset the icon.
1549
1550 For purposes of this example, set a breakpoint at line 10 (the
1551 statement @code{Put_Line@ (Line@ (1..N));}
1552
1553 @item @emph{Starting program execution}
1554
1555 Select @code{Debug}, then @code{Run}.  When the
1556 @code{Program Arguments} window appears, click @code{OK}.
1557 A console window will appear; enter some line of text,
1558 e.g. @code{abcde}, at the prompt.
1559 The program will pause execution when it gets to the
1560 breakpoint, and the corresponding line is highlighted.
1561
1562 @item @emph{Examining a variable}
1563
1564 Move the mouse over one of the occurrences of the variable @code{N}.
1565 You will see the value (5) displayed, in ``tool tip'' fashion.
1566 Right click on @code{N}, select @code{Debug}, then select @code{Display N}.
1567 You will see information about @code{N} appear in the @code{Debugger Data}
1568 pane, showing the value as 5.
1569
1570 @item @emph{Assigning a new value to a variable}
1571
1572 Right click on the @code{N} in the @code{Debugger Data} pane, and
1573 select @code{Set value of N}.
1574 When the input window appears, enter the value @code{4} and click
1575 @code{OK}.
1576 This value does not automatically appear in the @code{Debugger Data}
1577 pane; to see it, right click again on the @code{N} in the
1578 @code{Debugger Data} pane and select @code{Update value}.
1579 The new value, 4, will appear in red.
1580
1581 @item @emph{Single stepping}
1582
1583 Select @code{Debug}, then @code{Next}.
1584 This will cause the next statement to be executed, in this case the
1585 call of @code{Put_Line} with the string slice.
1586 Notice in the console window that the displayed string is simply
1587 @code{abcd} and not @code{abcde} which you had entered.
1588 This is because the upper bound of the slice is now 4 rather than 5.
1589
1590 @item @emph{Removing a breakpoint}
1591
1592 Toggle the breakpoint icon at line 10.
1593
1594 @item @emph{Resuming execution from a breakpoint}
1595
1596 Select @code{Debug}, then @code{Continue}.
1597 The program will reach the next iteration of the loop, and
1598 wait for input after displaying the prompt.
1599 This time, just hit the @kbd{Enter} key.
1600 The value of @code{N} will be 0, and the program will terminate.
1601 The console window will disappear.
1602 @end enumerate
1603 @end enumerate
1604
1605 @node Introduction to Glide and GVD
1606 @section Introduction to Glide and GVD
1607 @cindex Glide
1608 @cindex GVD
1609 @noindent
1610 This section describes the main features of Glide,
1611 a GNAT graphical IDE, and also shows how to use the basic commands in GVD,
1612 the GNU Visual Debugger.
1613 These tools may be present in addition to, or in place of, GPS on some
1614 platforms.
1615 Additional information on Glide and GVD may be found
1616 in the on-line help for these tools.
1617
1618 @menu
1619 * Building a New Program with Glide::
1620 * Simple Debugging with GVD::
1621 * Other Glide Features::
1622 @end menu
1623
1624 @node Building a New Program with Glide
1625 @subsection Building a New Program with Glide
1626 @noindent
1627 The simplest way to invoke Glide is to enter @command{glide}
1628 at the command prompt.  It will generally be useful to issue this
1629 as a background command, thus allowing you to continue using
1630 your command window for other purposes while Glide is running:
1631
1632 @smallexample
1633 $ glide&
1634 @end smallexample
1635
1636 @noindent
1637 Glide will start up with an initial screen displaying the top-level menu items
1638 as well as some other information.  The menu selections are as follows
1639 @itemize @bullet
1640 @item @code{Buffers}
1641 @item @code{Files}
1642 @item @code{Tools}
1643 @item @code{Edit}
1644 @item @code{Search}
1645 @item @code{Mule}
1646 @item @code{Glide}
1647 @item @code{Help}
1648 @end itemize
1649
1650 @noindent
1651 For this introductory example, you will need to create a new Ada source file.
1652 First, select the @code{Files} menu.  This will pop open a menu with around
1653 a dozen or so items.  To create a file, select the @code{Open file...} choice.
1654 Depending on the platform, you may see a pop-up window where you can browse
1655 to an appropriate directory and then enter the file name, or else simply
1656 see a line at the bottom of the Glide window where you can likewise enter
1657 the file name.  Note that in Glide, when you attempt to open a non-existent
1658 file, the effect is to create a file with that name.  For this example enter
1659 @file{hello.adb} as the name of the file.
1660
1661 A new buffer will now appear, occupying the entire Glide window,
1662 with the file name at the top.  The menu selections are slightly different
1663 from the ones you saw on the opening screen; there is an @code{Entities} item,
1664 and in place of @code{Glide} there is now an @code{Ada} item.  Glide uses
1665 the file extension to identify the source language, so @file{adb} indicates
1666 an Ada source file.
1667
1668 You will enter some of the source program lines explicitly,
1669 and use the syntax-oriented template mechanism to enter other lines.
1670 First, type the following text:
1671 @smallexample
1672 with Ada.Text_IO; use Ada.Text_IO;
1673 procedure Hello is
1674 begin
1675 @end smallexample
1676
1677 @noindent
1678 Observe that Glide uses different colors to distinguish reserved words from
1679 identifiers.  Also, after the @code{procedure Hello is} line, the cursor is
1680 automatically indented in anticipation of declarations.  When you enter
1681 @code{begin}, Glide recognizes that there are no declarations and thus places
1682 @code{begin} flush left.  But after the @code{begin} line the cursor is again
1683 indented, where the statement(s) will be placed.
1684
1685 The main part of the program will be a @code{for} loop.  Instead of entering
1686 the text explicitly, however, use a statement template.  Select the @code{Ada}
1687 item on the top menu bar, move the mouse to the @code{Statements} item,
1688 and you will see a large selection of alternatives.  Choose @code{for loop}.
1689 You will be prompted (at the bottom of the buffer) for a loop name;
1690 simply press the @key{Enter} key since a loop name is not needed.
1691 You should see the beginning of a @code{for} loop appear in the source
1692 program window.  You will now be prompted for the name of the loop variable;
1693 enter a line with the identifier @code{ind} (lower case).  Note that,
1694 by default, Glide capitalizes the name (you can override such behavior
1695 if you wish, although this is outside the scope of this introduction).
1696 Next, Glide prompts you for the loop range; enter a line containing
1697 @code{1..5} and you will see this also appear in the source program,
1698 together with the remaining elements of the @code{for} loop syntax.
1699
1700 Next enter the statement (with an intentional error, a missing semicolon)
1701 that will form the body of the loop:
1702 @smallexample
1703 Put_Line("Hello, World" & Integer'Image(I))
1704 @end smallexample
1705
1706 @noindent
1707 Finally, type @code{end Hello;} as the last line in the program.
1708 Now save the file: choose the @code{File} menu item, and then the
1709 @code{Save buffer} selection.  You will see a message at the bottom
1710 of the buffer confirming that the file has been saved.
1711
1712 You are now ready to attempt to build the program.  Select the @code{Ada}
1713 item from the top menu bar.  Although we could choose simply to compile
1714 the file, we will instead attempt to do a build (which invokes
1715 @command{gnatmake}) since, if the compile is successful, we want to build
1716 an executable.  Thus select @code{Ada build}.  This will fail because of the
1717 compilation error, and you will notice that the Glide window has been split:
1718 the top window contains the source file, and the bottom window contains the
1719 output from the GNAT tools. Glide allows you to navigate from a compilation
1720 error to the source file position corresponding to the error: click the
1721 middle mouse button (or simultaneously press the left and right buttons,
1722 on a two-button mouse) on the diagnostic line in the tool window.  The
1723 focus will shift to the source window, and the cursor will be positioned
1724 on the character at which the error was detected.
1725
1726 Correct the error: type in a semicolon to terminate the statement.
1727 Although you can again save the file explicitly, you can also simply invoke
1728 @code{Ada} @result{} @code{Build} and you will be prompted to save the file.
1729 This time the build will succeed; the tool output window shows you the
1730 options that are supplied by default.  The GNAT tools' output (e.g.
1731 object and ALI files, executable) will go in the directory from which
1732 Glide was launched.
1733
1734 To execute the program, choose @code{Ada} and then @code{Run}.
1735 You should see the program's output displayed in the bottom window:
1736
1737 @smallexample
1738 Hello, world 1
1739 Hello, world 2
1740 Hello, world 3
1741 Hello, world 4
1742 Hello, world 5
1743 @end smallexample
1744
1745 @node Simple Debugging with GVD
1746 @subsection Simple Debugging with GVD
1747
1748 @noindent
1749 This section describes how to set breakpoints, examine/modify variables,
1750 and step through execution.
1751
1752 In order to enable debugging, you need to pass the @option{-g} switch
1753 to both the compiler and to @command{gnatlink}.  If you are using
1754 the command line, passing @option{-g} to @command{gnatmake} will have
1755 this effect.  You can then launch GVD, e.g. on the @code{hello} program,
1756 by issuing the command:
1757
1758 @smallexample
1759 $ gvd hello
1760 @end smallexample
1761
1762 @noindent
1763 If you are using Glide, then @option{-g} is passed to the relevant tools
1764 by default when you do a build.  Start the debugger by selecting the
1765 @code{Ada} menu item, and then @code{Debug}.
1766
1767 GVD comes up in a multi-part window.  One pane shows the names of files
1768 comprising your executable; another pane shows the source code of the current
1769 unit (initially your main subprogram), another pane shows the debugger output
1770 and user interactions, and the fourth pane (the data canvas at the top
1771 of the window) displays data objects that you have selected.
1772
1773 To the left of the source file pane, you will notice green dots adjacent
1774 to some lines.  These are lines for which object code exists and where
1775 breakpoints can thus be set.  You set/reset a breakpoint by clicking
1776 the green dot.  When a breakpoint is set, the dot is replaced by an @code{X}
1777 in a red circle.  Clicking the circle toggles the breakpoint off,
1778 and the red circle is replaced by the green dot.
1779
1780 For this example, set a breakpoint at the statement where @code{Put_Line}
1781 is invoked.
1782
1783 Start program execution by selecting the @code{Run} button on the top menu bar.
1784 (The @code{Start} button will also start your program, but it will
1785 cause program execution to break at the entry to your main subprogram.)
1786 Evidence of reaching the breakpoint will appear: the source file line will be
1787 highlighted, and the debugger interactions pane will display
1788 a relevant message.
1789
1790 You can examine the values of variables in several ways.  Move the mouse
1791 over an occurrence of @code{Ind} in the @code{for} loop, and you will see
1792 the value (now @code{1}) displayed.  Alternatively, right-click on @code{Ind}
1793 and select @code{Display Ind}; a box showing the variable's name and value
1794 will appear in the data canvas.
1795
1796 Although a loop index is a constant with respect to Ada semantics,
1797 you can change its value in the debugger.  Right-click in the box
1798 for @code{Ind}, and select the @code{Set Value of Ind} item.
1799 Enter @code{2} as the new value, and press @command{OK}.
1800 The box for @code{Ind} shows the update.
1801
1802 Press the @code{Step} button on the top menu bar; this will step through
1803 one line of program text (the invocation of @code{Put_Line}), and you can
1804 observe the effect of having modified @code{Ind} since the value displayed
1805 is @code{2}.
1806
1807 Remove the breakpoint, and resume execution by selecting the @code{Cont}
1808 button.  You will see the remaining output lines displayed in the debugger
1809 interaction window, along with a message confirming normal program
1810 termination.
1811
1812 @node Other Glide Features
1813 @subsection Other Glide Features
1814
1815 @noindent
1816 You may have observed that some of the menu selections contain abbreviations;
1817 e.g., @code{(C-x C-f)} for @code{Open file...} in the @code{Files} menu.
1818 These are @emph{shortcut keys} that you can use instead of selecting
1819 menu items.  The @key{C} stands for @key{Ctrl}; thus @code{(C-x C-f)} means
1820 @key{Ctrl-x} followed by @key{Ctrl-f}, and this sequence can be used instead
1821 of selecting @code{Files} and then @code{Open file...}.
1822
1823 To abort a Glide command, type @key{Ctrl-g}.
1824
1825 If you want Glide to start with an existing source file, you can either
1826 launch Glide as above and then open the file via @code{Files} @result{}
1827 @code{Open file...}, or else simply pass the name of the source file
1828 on the command line:
1829
1830 @smallexample
1831 $ glide hello.adb&
1832 @end smallexample
1833
1834 @noindent
1835 While you are using Glide, a number of @emph{buffers} exist.
1836 You create some explicitly; e.g., when you open/create a file.
1837 Others arise as an effect of the commands that you issue; e.g., the buffer
1838 containing the output of the tools invoked during a build.  If a buffer
1839 is hidden, you can bring it into a visible window by first opening
1840 the @code{Buffers} menu and then selecting the desired entry.
1841
1842 If a buffer occupies only part of the Glide screen and you want to expand it
1843 to fill the entire screen, then click in the buffer and then select
1844 @code{Files} @result{} @code{One Window}.
1845
1846 If a window is occupied by one buffer and you want to split the window
1847 to bring up a second buffer, perform the following steps:
1848 @itemize @bullet
1849 @item Select @code{Files} @result{} @code{Split Window};
1850 this will produce two windows each of which holds the original buffer
1851 (these are not copies, but rather different views of the same buffer contents)
1852
1853 @item With the focus in one of the windows,
1854 select the desired buffer from the @code{Buffers} menu
1855 @end itemize
1856
1857 @noindent
1858 To exit from Glide, choose @code{Files} @result{} @code{Exit}.
1859 @end ifclear
1860
1861 @node The GNAT Compilation Model
1862 @chapter The GNAT Compilation Model
1863 @cindex GNAT compilation model
1864 @cindex Compilation model
1865
1866 @menu
1867 * Source Representation::
1868 * Foreign Language Representation::
1869 * File Naming Rules::
1870 * Using Other File Names::
1871 * Alternative File Naming Schemes::
1872 * Generating Object Files::
1873 * Source Dependencies::
1874 * The Ada Library Information Files::
1875 * Binding an Ada Program::
1876 * Mixed Language Programming::
1877 * Building Mixed Ada & C++ Programs::
1878 * Comparison between GNAT and C/C++ Compilation Models::
1879 * Comparison between GNAT and Conventional Ada Library Models::
1880 @ifset vms
1881 * Placement of temporary files::
1882 @end ifset
1883 @end menu
1884
1885 @noindent
1886 This chapter describes the compilation model used by GNAT. Although
1887 similar to that used by other languages, such as C and C++, this model
1888 is substantially different from the traditional Ada compilation models,
1889 which are based on a library. The model is initially described without
1890 reference to the library-based model. If you have not previously used an
1891 Ada compiler, you need only read the first part of this chapter. The
1892 last section describes and discusses the differences between the GNAT
1893 model and the traditional Ada compiler models. If you have used other
1894 Ada compilers, this section will help you to understand those
1895 differences, and the advantages of the GNAT model.
1896
1897 @node Source Representation
1898 @section Source Representation
1899 @cindex Latin-1
1900
1901 @noindent
1902 Ada source programs are represented in standard text files, using
1903 Latin-1 coding. Latin-1 is an 8-bit code that includes the familiar
1904 7-bit ASCII set, plus additional characters used for
1905 representing foreign languages (@pxref{Foreign Language Representation}
1906 for support of non-USA character sets). The format effector characters
1907 are represented using their standard ASCII encodings, as follows:
1908
1909 @table @code
1910 @item VT
1911 @findex VT
1912 Vertical tab, @code{16#0B#}
1913
1914 @item HT
1915 @findex HT
1916 Horizontal tab, @code{16#09#}
1917
1918 @item CR
1919 @findex CR
1920 Carriage return, @code{16#0D#}
1921
1922 @item LF
1923 @findex LF
1924 Line feed, @code{16#0A#}
1925
1926 @item FF
1927 @findex FF
1928 Form feed, @code{16#0C#}
1929 @end table
1930
1931 @noindent
1932 Source files are in standard text file format. In addition, GNAT will
1933 recognize a wide variety of stream formats, in which the end of
1934 physical lines is marked by any of the following sequences:
1935 @code{LF}, @code{CR}, @code{CR-LF}, or @code{LF-CR}. This is useful
1936 in accommodating files that are imported from other operating systems.
1937
1938 @cindex End of source file
1939 @cindex Source file, end
1940 @findex SUB
1941 The end of a source file is normally represented by the physical end of
1942 file. However, the control character @code{16#1A#} (@code{SUB}) is also
1943 recognized as signalling the end of the source file. Again, this is
1944 provided for compatibility with other operating systems where this
1945 code is used to represent the end of file.
1946
1947 Each file contains a single Ada compilation unit, including any pragmas
1948 associated with the unit. For example, this means you must place a
1949 package declaration (a package @dfn{spec}) and the corresponding body in
1950 separate files. An Ada @dfn{compilation} (which is a sequence of
1951 compilation units) is represented using a sequence of files. Similarly,
1952 you will place each subunit or child unit in a separate file.
1953
1954 @node Foreign Language Representation
1955 @section Foreign Language Representation
1956
1957 @noindent
1958 GNAT supports the standard character sets defined in Ada 95 as well as
1959 several other non-standard character sets for use in localized versions
1960 of the compiler (@pxref{Character Set Control}).
1961 @menu
1962 * Latin-1::
1963 * Other 8-Bit Codes::
1964 * Wide Character Encodings::
1965 @end menu
1966
1967 @node Latin-1
1968 @subsection Latin-1
1969 @cindex Latin-1
1970
1971 @noindent
1972 The basic character set is Latin-1. This character set is defined by ISO
1973 standard 8859, part 1. The lower half (character codes @code{16#00#}
1974 ... @code{16#7F#)} is identical to standard ASCII coding, but the upper half
1975 is used to represent additional characters. These include extended letters
1976 used by European languages, such as French accents, the vowels with umlauts
1977 used in German, and the extra letter A-ring used in Swedish.
1978
1979 @findex Ada.Characters.Latin_1
1980 For a complete list of Latin-1 codes and their encodings, see the source
1981 file of library unit @code{Ada.Characters.Latin_1} in file
1982 @file{a-chlat1.ads}.
1983 You may use any of these extended characters freely in character or
1984 string literals. In addition, the extended characters that represent
1985 letters can be used in identifiers.
1986
1987 @node Other 8-Bit Codes
1988 @subsection Other 8-Bit Codes
1989
1990 @noindent
1991 GNAT also supports several other 8-bit coding schemes:
1992
1993 @table @asis
1994 @item ISO 8859-2 (Latin-2)
1995 @cindex Latin-2
1996 @cindex ISO 8859-2
1997 Latin-2 letters allowed in identifiers, with uppercase and lowercase
1998 equivalence.
1999
2000 @item ISO 8859-3 (Latin-3)
2001 @cindex Latin-3
2002 @cindex ISO 8859-3
2003 Latin-3 letters allowed in identifiers, with uppercase and lowercase
2004 equivalence.
2005
2006 @item ISO 8859-4 (Latin-4)
2007 @cindex Latin-4
2008 @cindex ISO 8859-4
2009 Latin-4 letters allowed in identifiers, with uppercase and lowercase
2010 equivalence.
2011
2012 @item ISO 8859-5 (Cyrillic)
2013 @cindex ISO 8859-5
2014 @cindex Cyrillic
2015 ISO 8859-5 letters (Cyrillic) allowed in identifiers, with uppercase and
2016 lowercase equivalence.
2017
2018 @item ISO 8859-15 (Latin-9)
2019 @cindex ISO 8859-15
2020 @cindex Latin-9
2021 ISO 8859-15 (Latin-9) letters allowed in identifiers, with uppercase and
2022 lowercase equivalence
2023
2024 @item IBM PC (code page 437)
2025 @cindex code page 437
2026 This code page is the normal default for PCs in the U.S. It corresponds
2027 to the original IBM PC character set. This set has some, but not all, of
2028 the extended Latin-1 letters, but these letters do not have the same
2029 encoding as Latin-1. In this mode, these letters are allowed in
2030 identifiers with uppercase and lowercase equivalence.
2031
2032 @item IBM PC (code page 850)
2033 @cindex code page 850
2034 This code page is a modification of 437 extended to include all the
2035 Latin-1 letters, but still not with the usual Latin-1 encoding. In this
2036 mode, all these letters are allowed in identifiers with uppercase and
2037 lowercase equivalence.
2038
2039 @item Full Upper 8-bit
2040 Any character in the range 80-FF allowed in identifiers, and all are
2041 considered distinct. In other words, there are no uppercase and lowercase
2042 equivalences in this range. This is useful in conjunction with
2043 certain encoding schemes used for some foreign character sets (e.g.
2044 the typical method of representing Chinese characters on the PC).
2045
2046 @item No Upper-Half
2047 No upper-half characters in the range 80-FF are allowed in identifiers.
2048 This gives Ada 83 compatibility for identifier names.
2049 @end table
2050
2051 @noindent
2052 For precise data on the encodings permitted, and the uppercase and lowercase
2053 equivalences that are recognized, see the file @file{csets.adb} in
2054 the GNAT compiler sources. You will need to obtain a full source release
2055 of GNAT to obtain this file.
2056
2057 @node Wide Character Encodings
2058 @subsection Wide Character Encodings
2059
2060 @noindent
2061 GNAT allows wide character codes to appear in character and string
2062 literals, and also optionally in identifiers, by means of the following
2063 possible encoding schemes:
2064
2065 @table @asis
2066
2067 @item Hex Coding
2068 In this encoding, a wide character is represented by the following five
2069 character sequence:
2070
2071 @smallexample
2072 ESC a b c d
2073 @end smallexample
2074
2075 @noindent
2076 Where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
2077 characters (using uppercase letters) of the wide character code. For
2078 example, ESC A345 is used to represent the wide character with code
2079 @code{16#A345#}.
2080 This scheme is compatible with use of the full Wide_Character set.
2081
2082 @item Upper-Half Coding
2083 @cindex Upper-Half Coding
2084 The wide character with encoding @code{16#abcd#} where the upper bit is on
2085 (in other words, ``a'' is in the range 8-F) is represented as two bytes,
2086 @code{16#ab#} and @code{16#cd#}. The second byte cannot be a format control
2087 character, but is not required to be in the upper half. This method can
2088 be also used for shift-JIS or EUC, where the internal coding matches the
2089 external coding.
2090
2091 @item Shift JIS Coding
2092 @cindex Shift JIS Coding
2093 A wide character is represented by a two-character sequence,
2094 @code{16#ab#} and
2095 @code{16#cd#}, with the restrictions described for upper-half encoding as
2096 described above. The internal character code is the corresponding JIS
2097 character according to the standard algorithm for Shift-JIS
2098 conversion. Only characters defined in the JIS code set table can be
2099 used with this encoding method.
2100
2101 @item EUC Coding
2102 @cindex EUC Coding
2103 A wide character is represented by a two-character sequence
2104 @code{16#ab#} and
2105 @code{16#cd#}, with both characters being in the upper half. The internal
2106 character code is the corresponding JIS character according to the EUC
2107 encoding algorithm. Only characters defined in the JIS code set table
2108 can be used with this encoding method.
2109
2110 @item UTF-8 Coding
2111 A wide character is represented using
2112 UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO
2113 10646-1/Am.2. Depending on the character value, the representation
2114 is a one, two, or three byte sequence:
2115 @smallexample
2116 @iftex
2117 @leftskip=.7cm
2118 @end iftex
2119 16#0000#-16#007f#: 2#0xxxxxxx#
2120 16#0080#-16#07ff#: 2#110xxxxx# 2#10xxxxxx#
2121 16#0800#-16#ffff#: 2#1110xxxx# 2#10xxxxxx# 2#10xxxxxx#
2122
2123 @end smallexample
2124
2125 @noindent
2126 where the xxx bits correspond to the left-padded bits of the
2127 16-bit character value. Note that all lower half ASCII characters
2128 are represented as ASCII bytes and all upper half characters and
2129 other wide characters are represented as sequences of upper-half
2130 (The full UTF-8 scheme allows for encoding 31-bit characters as
2131 6-byte sequences, but in this implementation, all UTF-8 sequences
2132 of four or more bytes length will be treated as illegal).
2133 @item Brackets Coding
2134 In this encoding, a wide character is represented by the following eight
2135 character sequence:
2136
2137 @smallexample
2138 [ " a b c d " ]
2139 @end smallexample
2140
2141 @noindent
2142 Where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
2143 characters (using uppercase letters) of the wide character code. For
2144 example, [``A345''] is used to represent the wide character with code
2145 @code{16#A345#}. It is also possible (though not required) to use the
2146 Brackets coding for upper half characters. For example, the code
2147 @code{16#A3#} can be represented as @code{[``A3'']}.
2148
2149 This scheme is compatible with use of the full Wide_Character set,
2150 and is also the method used for wide character encoding in the standard
2151 ACVC (Ada Compiler Validation Capability) test suite distributions.
2152
2153 @end table
2154
2155 @noindent
2156 Note: Some of these coding schemes do not permit the full use of the
2157 Ada 95 character set. For example, neither Shift JIS, nor EUC allow the
2158 use of the upper half of the Latin-1 set.
2159
2160 @node File Naming Rules
2161 @section File Naming Rules
2162
2163 @noindent
2164 The default file name is determined by the name of the unit that the
2165 file contains. The name is formed by taking the full expanded name of
2166 the unit and replacing the separating dots with hyphens and using
2167 ^lowercase^uppercase^ for all letters.
2168
2169 An exception arises if the file name generated by the above rules starts
2170 with one of the characters
2171 @ifset vms
2172 A,G,I, or S,
2173 @end ifset
2174 @ifclear vms
2175 a,g,i, or s,
2176 @end ifclear
2177 and the second character is a
2178 minus. In this case, the character ^tilde^dollar sign^ is used in place
2179 of the minus. The reason for this special rule is to avoid clashes with
2180 the standard names for child units of the packages System, Ada,
2181 Interfaces, and GNAT, which use the prefixes
2182 @ifset vms
2183 S- A- I- and G-
2184 @end ifset
2185 @ifclear vms
2186 s- a- i- and g-
2187 @end ifclear
2188 respectively.
2189
2190 The file extension is @file{.ads} for a spec and
2191 @file{.adb} for a body. The following list shows some
2192 examples of these rules.
2193
2194 @table @file
2195 @item main.ads
2196 Main (spec)
2197 @item main.adb
2198 Main (body)
2199 @item arith_functions.ads
2200 Arith_Functions (package spec)
2201 @item arith_functions.adb
2202 Arith_Functions (package body)
2203 @item func-spec.ads
2204 Func.Spec (child package spec)
2205 @item func-spec.adb
2206 Func.Spec (child package body)
2207 @item main-sub.adb
2208 Sub (subunit of Main)
2209 @item ^a~bad.adb^A$BAD.ADB^
2210 A.Bad (child package body)
2211 @end table
2212
2213 @noindent
2214 Following these rules can result in excessively long
2215 file names if corresponding
2216 unit names are long (for example, if child units or subunits are
2217 heavily nested). An option is available to shorten such long file names
2218 (called file name ``krunching''). This may be particularly useful when
2219 programs being developed with GNAT are to be used on operating systems
2220 with limited file name lengths. @xref{Using gnatkr}.
2221
2222 Of course, no file shortening algorithm can guarantee uniqueness over
2223 all possible unit names; if file name krunching is used, it is your
2224 responsibility to ensure no name clashes occur. Alternatively you
2225 can specify the exact file names that you want used, as described
2226 in the next section. Finally, if your Ada programs are migrating from a
2227 compiler with a different naming convention, you can use the gnatchop
2228 utility to produce source files that follow the GNAT naming conventions.
2229 (For details @pxref{Renaming Files Using gnatchop}.)
2230
2231 Note: in the case of @code{Windows NT/XP} or @code{OpenVMS} operating
2232 systems, case is not significant. So for example on @code{Windows XP}
2233 if the canonical name is @code{main-sub.adb}, you can use the file name
2234 @code{Main-Sub.adb} instead. However, case is significant for other
2235 operating systems, so for example, if you want to use other than
2236 canonically cased file names on a Unix system, you need to follow
2237 the procedures described in the next section.
2238
2239 @node Using Other File Names
2240 @section Using Other File Names
2241 @cindex File names
2242
2243 @noindent
2244 In the previous section, we have described the default rules used by
2245 GNAT to determine the file name in which a given unit resides. It is
2246 often convenient to follow these default rules, and if you follow them,
2247 the compiler knows without being explicitly told where to find all
2248 the files it needs.
2249
2250 However, in some cases, particularly when a program is imported from
2251 another Ada compiler environment, it may be more convenient for the
2252 programmer to specify which file names contain which units. GNAT allows
2253 arbitrary file names to be used by means of the Source_File_Name pragma.
2254 The form of this pragma is as shown in the following examples:
2255 @cindex Source_File_Name pragma
2256
2257 @smallexample @c ada
2258 @cartouche
2259 pragma Source_File_Name (My_Utilities.Stacks,
2260   Spec_File_Name => "myutilst_a.ada");
2261 pragma Source_File_name (My_Utilities.Stacks,
2262   Body_File_Name => "myutilst.ada");
2263 @end cartouche
2264 @end smallexample
2265
2266 @noindent
2267 As shown in this example, the first argument for the pragma is the unit
2268 name (in this example a child unit). The second argument has the form
2269 of a named association. The identifier
2270 indicates whether the file name is for a spec or a body;
2271 the file name itself is given by a string literal.
2272
2273 The source file name pragma is a configuration pragma, which means that
2274 normally it will be placed in the @file{gnat.adc}
2275 file used to hold configuration
2276 pragmas that apply to a complete compilation environment.
2277 For more details on how the @file{gnat.adc} file is created and used
2278 see @ref{Handling of Configuration Pragmas}.
2279 @cindex @file{gnat.adc}
2280
2281 @ifclear vms
2282 GNAT allows completely arbitrary file names to be specified using the
2283 source file name pragma. However, if the file name specified has an
2284 extension other than @file{.ads} or @file{.adb} it is necessary to use
2285 a special syntax when compiling the file. The name in this case must be
2286 preceded by the special sequence @code{-x} followed by a space and the name
2287 of the language, here @code{ada}, as in:
2288
2289 @smallexample
2290 $ gcc -c -x ada peculiar_file_name.sim
2291 @end smallexample
2292 @end ifclear
2293
2294 @noindent
2295 @command{gnatmake} handles non-standard file names in the usual manner (the
2296 non-standard file name for the main program is simply used as the
2297 argument to gnatmake). Note that if the extension is also non-standard,
2298 then it must be included in the gnatmake command, it may not be omitted.
2299
2300 @node Alternative File Naming Schemes
2301 @section Alternative File Naming Schemes
2302 @cindex File naming schemes, alternative
2303 @cindex File names
2304
2305 In the previous section, we described the use of the @code{Source_File_Name}
2306 pragma to allow arbitrary names to be assigned to individual source files.
2307 However, this approach requires one pragma for each file, and especially in
2308 large systems can result in very long @file{gnat.adc} files, and also create
2309 a maintenance problem.
2310
2311 GNAT also provides a facility for specifying systematic file naming schemes
2312 other than the standard default naming scheme previously described. An
2313 alternative scheme for naming is specified by the use of
2314 @code{Source_File_Name} pragmas having the following format:
2315 @cindex Source_File_Name pragma
2316
2317 @smallexample @c ada
2318 pragma Source_File_Name (
2319    Spec_File_Name  => FILE_NAME_PATTERN
2320  [,Casing          => CASING_SPEC]
2321  [,Dot_Replacement => STRING_LITERAL]);
2322
2323 pragma Source_File_Name (
2324    Body_File_Name  => FILE_NAME_PATTERN
2325  [,Casing          => CASING_SPEC]
2326  [,Dot_Replacement => STRING_LITERAL]);
2327
2328 pragma Source_File_Name (
2329    Subunit_File_Name  => FILE_NAME_PATTERN
2330  [,Casing             => CASING_SPEC]
2331  [,Dot_Replacement    => STRING_LITERAL]);
2332
2333 FILE_NAME_PATTERN ::= STRING_LITERAL
2334 CASING_SPEC ::= Lowercase | Uppercase | Mixedcase
2335 @end smallexample
2336
2337 @noindent
2338 The @code{FILE_NAME_PATTERN} string shows how the file name is constructed.
2339 It contains a single asterisk character, and the unit name is substituted
2340 systematically for this asterisk. The optional parameter
2341 @code{Casing} indicates
2342 whether the unit name is to be all upper-case letters, all lower-case letters,
2343 or mixed-case. If no
2344 @code{Casing} parameter is used, then the default is all
2345 ^lower-case^upper-case^.
2346
2347 The optional @code{Dot_Replacement} string is used to replace any periods
2348 that occur in subunit or child unit names. If no @code{Dot_Replacement}
2349 argument is used then separating dots appear unchanged in the resulting
2350 file name.
2351 Although the above syntax indicates that the
2352 @code{Casing} argument must appear
2353 before the @code{Dot_Replacement} argument, but it
2354 is also permissible to write these arguments in the opposite order.
2355
2356 As indicated, it is possible to specify different naming schemes for
2357 bodies, specs, and subunits. Quite often the rule for subunits is the
2358 same as the rule for bodies, in which case, there is no need to give
2359 a separate @code{Subunit_File_Name} rule, and in this case the
2360 @code{Body_File_name} rule is used for subunits as well.
2361
2362 The separate rule for subunits can also be used to implement the rather
2363 unusual case of a compilation environment (e.g. a single directory) which
2364 contains a subunit and a child unit with the same unit name. Although
2365 both units cannot appear in the same partition, the Ada Reference Manual
2366 allows (but does not require) the possibility of the two units coexisting
2367 in the same environment.
2368
2369 The file name translation works in the following steps:
2370
2371 @itemize @bullet
2372
2373 @item
2374 If there is a specific @code{Source_File_Name} pragma for the given unit,
2375 then this is always used, and any general pattern rules are ignored.
2376
2377 @item
2378 If there is a pattern type @code{Source_File_Name} pragma that applies to
2379 the unit, then the resulting file name will be used if the file exists. If
2380 more than one pattern matches, the latest one will be tried first, and the
2381 first attempt resulting in a reference to a file that exists will be used.
2382
2383 @item
2384 If no pattern type @code{Source_File_Name} pragma that applies to the unit
2385 for which the corresponding file exists, then the standard GNAT default
2386 naming rules are used.
2387
2388 @end itemize
2389
2390 @noindent
2391 As an example of the use of this mechanism, consider a commonly used scheme
2392 in which file names are all lower case, with separating periods copied
2393 unchanged to the resulting file name, and specs end with @file{.1.ada}, and
2394 bodies end with @file{.2.ada}. GNAT will follow this scheme if the following
2395 two pragmas appear:
2396
2397 @smallexample @c ada
2398 pragma Source_File_Name
2399   (Spec_File_Name => "*.1.ada");
2400 pragma Source_File_Name
2401   (Body_File_Name => "*.2.ada");
2402 @end smallexample
2403
2404 @noindent
2405 The default GNAT scheme is actually implemented by providing the following
2406 default pragmas internally:
2407
2408 @smallexample @c ada
2409 pragma Source_File_Name
2410   (Spec_File_Name => "*.ads", Dot_Replacement => "-");
2411 pragma Source_File_Name
2412   (Body_File_Name => "*.adb", Dot_Replacement => "-");
2413 @end smallexample
2414
2415 @noindent
2416 Our final example implements a scheme typically used with one of the
2417 Ada 83 compilers, where the separator character for subunits was ``__''
2418 (two underscores), specs were identified by adding @file{_.ADA}, bodies
2419 by adding @file{.ADA}, and subunits by
2420 adding @file{.SEP}. All file names were
2421 upper case. Child units were not present of course since this was an
2422 Ada 83 compiler, but it seems reasonable to extend this scheme to use
2423 the same double underscore separator for child units.
2424
2425 @smallexample @c ada
2426 pragma Source_File_Name
2427   (Spec_File_Name => "*_.ADA",
2428    Dot_Replacement => "__",
2429    Casing = Uppercase);
2430 pragma Source_File_Name
2431   (Body_File_Name => "*.ADA",
2432    Dot_Replacement => "__",
2433    Casing = Uppercase);
2434 pragma Source_File_Name
2435   (Subunit_File_Name => "*.SEP",
2436    Dot_Replacement => "__",
2437    Casing = Uppercase);
2438 @end smallexample
2439
2440 @node Generating Object Files
2441 @section Generating Object Files
2442
2443 @noindent
2444 An Ada program consists of a set of source files, and the first step in
2445 compiling the program is to generate the corresponding object files.
2446 These are generated by compiling a subset of these source files.
2447 The files you need to compile are the following:
2448
2449 @itemize @bullet
2450 @item
2451 If a package spec has no body, compile the package spec to produce the
2452 object file for the package.
2453
2454 @item
2455 If a package has both a spec and a body, compile the body to produce the
2456 object file for the package. The source file for the package spec need
2457 not be compiled in this case because there is only one object file, which
2458 contains the code for both the spec and body of the package.
2459
2460 @item
2461 For a subprogram, compile the subprogram body to produce the object file
2462 for the subprogram. The spec, if one is present, is as usual in a
2463 separate file, and need not be compiled.
2464
2465 @item
2466 @cindex Subunits
2467 In the case of subunits, only compile the parent unit. A single object
2468 file is generated for the entire subunit tree, which includes all the
2469 subunits.
2470
2471 @item
2472 Compile child units independently of their parent units
2473 (though, of course, the spec of all the ancestor unit must be present in order
2474 to compile a child unit).
2475
2476 @item
2477 @cindex Generics
2478 Compile generic units in the same manner as any other units. The object
2479 files in this case are small dummy files that contain at most the
2480 flag used for elaboration checking. This is because GNAT always handles generic
2481 instantiation by means of macro expansion. However, it is still necessary to
2482 compile generic units, for dependency checking and elaboration purposes.
2483 @end itemize
2484
2485 @noindent
2486 The preceding rules describe the set of files that must be compiled to
2487 generate the object files for a program. Each object file has the same
2488 name as the corresponding source file, except that the extension is
2489 @file{.o} as usual.
2490
2491 You may wish to compile other files for the purpose of checking their
2492 syntactic and semantic correctness. For example, in the case where a
2493 package has a separate spec and body, you would not normally compile the
2494 spec. However, it is convenient in practice to compile the spec to make
2495 sure it is error-free before compiling clients of this spec, because such
2496 compilations will fail if there is an error in the spec.
2497
2498 GNAT provides an option for compiling such files purely for the
2499 purposes of checking correctness; such compilations are not required as
2500 part of the process of building a program. To compile a file in this
2501 checking mode, use the @option{-gnatc} switch.
2502
2503 @node Source Dependencies
2504 @section Source Dependencies
2505
2506 @noindent
2507 A given object file clearly depends on the source file which is compiled
2508 to produce it. Here we are using @dfn{depends} in the sense of a typical
2509 @code{make} utility; in other words, an object file depends on a source
2510 file if changes to the source file require the object file to be
2511 recompiled.
2512 In addition to this basic dependency, a given object may depend on
2513 additional source files as follows:
2514
2515 @itemize @bullet
2516 @item
2517 If a file being compiled @code{with}'s a unit @var{X}, the object file
2518 depends on the file containing the spec of unit @var{X}. This includes
2519 files that are @code{with}'ed implicitly either because they are parents
2520 of @code{with}'ed child units or they are run-time units required by the
2521 language constructs used in a particular unit.
2522
2523 @item
2524 If a file being compiled instantiates a library level generic unit, the
2525 object file depends on both the spec and body files for this generic
2526 unit.
2527
2528 @item
2529 If a file being compiled instantiates a generic unit defined within a
2530 package, the object file depends on the body file for the package as
2531 well as the spec file.
2532
2533 @item
2534 @findex Inline
2535 @cindex @option{-gnatn} switch
2536 If a file being compiled contains a call to a subprogram for which
2537 pragma @code{Inline} applies and inlining is activated with the
2538 @option{-gnatn} switch, the object file depends on the file containing the
2539 body of this subprogram as well as on the file containing the spec. Note
2540 that for inlining to actually occur as a result of the use of this switch,
2541 it is necessary to compile in optimizing mode.
2542
2543 @cindex @option{-gnatN} switch
2544 The use of @option{-gnatN} activates a more extensive inlining optimization
2545 that is performed by the front end of the compiler. This inlining does
2546 not require that the code generation be optimized. Like @option{-gnatn},
2547 the use of this switch generates additional dependencies.
2548 Note that
2549 @option{-gnatN} automatically implies @option{-gnatn} so it is not necessary
2550 to specify both options.
2551
2552 @item
2553 If an object file O  depends on the proper body of a subunit through inlining
2554 or instantiation, it depends on the parent unit of the subunit. This means that
2555 any modification of the parent unit or one of its subunits affects the
2556 compilation of O.
2557
2558 @item
2559 The object file for a parent unit depends on all its subunit body files.
2560
2561 @item
2562 The previous two rules meant that for purposes of computing dependencies and
2563 recompilation, a body and all its subunits are treated as an indivisible whole.
2564
2565 @noindent
2566 These rules are applied transitively: if unit @code{A} @code{with}'s
2567 unit @code{B}, whose elaboration calls an inlined procedure in package
2568 @code{C}, the object file for unit @code{A} will depend on the body of
2569 @code{C}, in file @file{c.adb}.
2570
2571 The set of dependent files described by these rules includes all the
2572 files on which the unit is semantically dependent, as described in the
2573 Ada 95 Language Reference Manual. However, it is a superset of what the
2574 ARM describes, because it includes generic, inline, and subunit dependencies.
2575
2576 An object file must be recreated by recompiling the corresponding source
2577 file if any of the source files on which it depends are modified. For
2578 example, if the @code{make} utility is used to control compilation,
2579 the rule for an Ada object file must mention all the source files on
2580 which the object file depends, according to the above definition.
2581 The determination of the necessary
2582 recompilations is done automatically when one uses @command{gnatmake}.
2583 @end itemize
2584
2585 @node The Ada Library Information Files
2586 @section The Ada Library Information Files
2587 @cindex Ada Library Information files
2588 @cindex @file{ALI} files
2589
2590 @noindent
2591 Each compilation actually generates two output files. The first of these
2592 is the normal object file that has a @file{.o} extension. The second is a
2593 text file containing full dependency information. It has the same
2594 name as the source file, but an @file{.ali} extension.
2595 This file is known as the Ada Library Information (@file{ALI}) file.
2596 The following information is contained in the @file{ALI} file.
2597
2598 @itemize @bullet
2599 @item
2600 Version information (indicates which version of GNAT was used to compile
2601 the unit(s) in question)
2602
2603 @item
2604 Main program information (including priority and time slice settings,
2605 as well as the wide character encoding used during compilation).
2606
2607 @item
2608 List of arguments used in the @command{gcc} command for the compilation
2609
2610 @item
2611 Attributes of the unit, including configuration pragmas used, an indication
2612 of whether the compilation was successful, exception model used etc.
2613
2614 @item
2615 A list of relevant restrictions applying to the unit (used for consistency)
2616 checking.
2617
2618 @item
2619 Categorization information (e.g. use of pragma @code{Pure}).
2620
2621 @item
2622 Information on all @code{with}'ed units, including presence of
2623 @code{Elaborate} or @code{Elaborate_All} pragmas.
2624
2625 @item
2626 Information from any @code{Linker_Options} pragmas used in the unit
2627
2628 @item
2629 Information on the use of @code{Body_Version} or @code{Version}
2630 attributes in the unit.
2631
2632 @item
2633 Dependency information. This is a list of files, together with
2634 time stamp and checksum information. These are files on which
2635 the unit depends in the sense that recompilation is required
2636 if any of these units are modified.
2637
2638 @item
2639 Cross-reference data. Contains information on all entities referenced
2640 in the unit. Used by tools like @code{gnatxref} and @code{gnatfind} to
2641 provide cross-reference information.
2642
2643 @end itemize
2644
2645 @noindent
2646 For a full detailed description of the format of the @file{ALI} file,
2647 see the source of the body of unit @code{Lib.Writ}, contained in file
2648 @file{lib-writ.adb} in the GNAT compiler sources.
2649
2650 @node Binding an Ada Program
2651 @section Binding an Ada Program
2652
2653 @noindent
2654 When using languages such as C and C++, once the source files have been
2655 compiled the only remaining step in building an executable program
2656 is linking the object modules together. This means that it is possible to
2657 link an inconsistent version of a program, in which two units have
2658 included different versions of the same header.
2659
2660 The rules of Ada do not permit such an inconsistent program to be built.
2661 For example, if two clients have different versions of the same package,
2662 it is illegal to build a program containing these two clients.
2663 These rules are enforced by the GNAT binder, which also determines an
2664 elaboration order consistent with the Ada rules.
2665
2666 The GNAT binder is run after all the object files for a program have
2667 been created. It is given the name of the main program unit, and from
2668 this it determines the set of units required by the program, by reading the
2669 corresponding ALI files. It generates error messages if the program is
2670 inconsistent or if no valid order of elaboration exists.
2671
2672 If no errors are detected, the binder produces a main program, in Ada by
2673 default, that contains calls to the elaboration procedures of those
2674 compilation unit that require them, followed by
2675 a call to the main program. This Ada program is compiled to generate the
2676 object file for the main program. The name of
2677 the Ada file is @file{b~@var{xxx}.adb} (with the corresponding spec
2678 @file{b~@var{xxx}.ads}) where @var{xxx} is the name of the
2679 main program unit.
2680
2681 Finally, the linker is used to build the resulting executable program,
2682 using the object from the main program from the bind step as well as the
2683 object files for the Ada units of the program.
2684
2685 @node Mixed Language Programming
2686 @section Mixed Language Programming
2687 @cindex Mixed Language Programming
2688
2689 @noindent
2690 This section describes how to develop a mixed-language program,
2691 specifically one that comprises units in both Ada and C.
2692
2693 @menu
2694 * Interfacing to C::
2695 * Calling Conventions::
2696 @end menu
2697
2698 @node Interfacing to C
2699 @subsection Interfacing to C
2700 @noindent
2701 Interfacing Ada with a foreign language such as C involves using
2702 compiler directives to import and/or export entity definitions in each
2703 language---using @code{extern} statements in C, for instance, and the
2704 @code{Import}, @code{Export}, and @code{Convention} pragmas in Ada.  For
2705 a full treatment of these topics, read Appendix B, section 1 of the Ada
2706 95 Language Reference Manual.
2707
2708 There are two ways to build a program using GNAT that contains some Ada
2709 sources and some foreign language sources, depending on whether or not
2710 the main subprogram is written in Ada.  Here is a source example with
2711 the main subprogram in Ada:
2712
2713 @smallexample
2714 /* file1.c */
2715 #include <stdio.h>
2716
2717 void print_num (int num)
2718 @{
2719   printf ("num is %d.\n", num);
2720   return;
2721 @}
2722
2723 /* file2.c */
2724
2725 /* num_from_Ada is declared in my_main.adb */
2726 extern int num_from_Ada;
2727
2728 int get_num (void)
2729 @{
2730   return num_from_Ada;
2731 @}
2732 @end smallexample
2733
2734 @smallexample @c ada
2735 --  my_main.adb
2736 procedure My_Main is
2737
2738    --  Declare then export an Integer entity called num_from_Ada
2739    My_Num : Integer := 10;
2740    pragma Export (C, My_Num, "num_from_Ada");
2741
2742    --  Declare an Ada function spec for Get_Num, then use
2743    --  C function get_num for the implementation.
2744    function Get_Num return Integer;
2745    pragma Import (C, Get_Num, "get_num");
2746
2747    --  Declare an Ada procedure spec for Print_Num, then use
2748    --  C function print_num for the implementation.
2749    procedure Print_Num (Num : Integer);
2750    pragma Import (C, Print_Num, "print_num");
2751
2752 begin
2753    Print_Num (Get_Num);
2754 end My_Main;
2755 @end smallexample
2756
2757 @enumerate
2758 @item
2759 To build this example, first compile the foreign language files to
2760 generate object files:
2761 @smallexample
2762 gcc -c file1.c
2763 gcc -c file2.c
2764 @end smallexample
2765
2766 @item
2767 Then, compile the Ada units to produce a set of object files and ALI
2768 files:
2769 @smallexample
2770 gnatmake ^-c^/ACTIONS=COMPILE^ my_main.adb
2771 @end smallexample
2772
2773 @item
2774 Run the Ada binder on the Ada main program:
2775 @smallexample
2776 gnatbind my_main.ali
2777 @end smallexample
2778
2779 @item
2780 Link the Ada main program, the Ada objects and the other language
2781 objects:
2782 @smallexample
2783 gnatlink my_main.ali file1.o file2.o
2784 @end smallexample
2785 @end enumerate
2786
2787 The last three steps can be grouped in a single command:
2788 @smallexample
2789 gnatmake my_main.adb -largs file1.o file2.o
2790 @end smallexample
2791
2792 @cindex Binder output file
2793 @noindent
2794 If the main program is in a language other than Ada, then you may have
2795 more than one entry point into the Ada subsystem. You must use a special
2796 binder option to generate callable routines that initialize and
2797 finalize the Ada units (@pxref{Binding with Non-Ada Main Programs}).
2798 Calls to the initialization and finalization routines must be inserted
2799 in the main program, or some other appropriate point in the code. The
2800 call to initialize the Ada units must occur before the first Ada
2801 subprogram is called, and the call to finalize the Ada units must occur
2802 after the last Ada subprogram returns. The binder will place the
2803 initialization and finalization subprograms into the
2804 @file{b~@var{xxx}.adb} file where they can be accessed by your C
2805 sources.  To illustrate, we have the following example:
2806
2807 @smallexample
2808 /* main.c */
2809 extern void adainit (void);
2810 extern void adafinal (void);
2811 extern int add (int, int);
2812 extern int sub (int, int);
2813
2814 int main (int argc, char *argv[])
2815 @{
2816   int a = 21, b = 7;
2817
2818   adainit();
2819
2820   /* Should print "21 + 7 = 28" */
2821   printf ("%d + %d = %d\n", a, b, add (a, b));
2822   /* Should print "21 - 7 = 14" */
2823   printf ("%d - %d = %d\n", a, b, sub (a, b));
2824
2825   adafinal();
2826 @}
2827 @end smallexample
2828
2829 @smallexample @c ada
2830 --  unit1.ads
2831 package Unit1 is
2832    function Add (A, B : Integer) return Integer;
2833    pragma Export (C, Add, "add");
2834 end Unit1;
2835
2836 --  unit1.adb
2837 package body Unit1 is
2838    function Add (A, B : Integer) return Integer is
2839    begin
2840       return A + B;
2841    end Add;
2842 end Unit1;
2843
2844 --  unit2.ads
2845 package Unit2 is
2846    function Sub (A, B : Integer) return Integer;
2847    pragma Export (C, Sub, "sub");
2848 end Unit2;
2849
2850 --  unit2.adb
2851 package body Unit2 is
2852    function Sub (A, B : Integer) return Integer is
2853    begin
2854       return A - B;
2855    end Sub;
2856 end Unit2;
2857 @end smallexample
2858
2859 @enumerate
2860 @item
2861 The build procedure for this application is similar to the last
2862 example's.  First, compile the foreign language files to generate object
2863 files:
2864 @smallexample
2865 ^gcc -c main.c^gcc -c main.c^
2866 @end smallexample
2867
2868 @item
2869 Next, compile the Ada units to produce a set of object files and ALI
2870 files:
2871 @smallexample
2872 gnatmake ^-c^/ACTIONS=COMPILE^ unit1.adb
2873 gnatmake ^-c^/ACTIONS=COMPILE^ unit2.adb
2874 @end smallexample
2875
2876 @item
2877 Run the Ada binder on every generated ALI file.  Make sure to use the
2878 @option{-n} option to specify a foreign main program:
2879 @smallexample
2880 gnatbind ^-n^/NOMAIN^ unit1.ali unit2.ali
2881 @end smallexample
2882
2883 @item
2884 Link the Ada main program, the Ada objects and the foreign language
2885 objects. You need only list the last ALI file here:
2886 @smallexample
2887 gnatlink unit2.ali main.o -o exec_file
2888 @end smallexample
2889
2890 This procedure yields a binary executable called @file{exec_file}.
2891 @end enumerate
2892
2893 @node Calling Conventions
2894 @subsection Calling Conventions
2895 @cindex Foreign Languages
2896 @cindex Calling Conventions
2897 GNAT follows standard calling sequence conventions and will thus interface
2898 to any other language that also follows these conventions. The following
2899 Convention identifiers are recognized by GNAT:
2900
2901 @table @code
2902 @cindex Interfacing to Ada
2903 @cindex Other Ada compilers
2904 @cindex Convention Ada
2905 @item Ada
2906 This indicates that the standard Ada calling sequence will be
2907 used and all Ada data items may be passed without any limitations in the
2908 case where GNAT is used to generate both the caller and callee. It is also
2909 possible to mix GNAT generated code and code generated by another Ada
2910 compiler. In this case, the data types should be restricted to simple
2911 cases, including primitive types. Whether complex data types can be passed
2912 depends on the situation. Probably it is safe to pass simple arrays, such
2913 as arrays of integers or floats. Records may or may not work, depending
2914 on whether both compilers lay them out identically. Complex structures
2915 involving variant records, access parameters, tasks, or protected types,
2916 are unlikely to be able to be passed.
2917
2918 Note that in the case of GNAT running
2919 on a platform that supports HP Ada 83, a higher degree of compatibility
2920 can be guaranteed, and in particular records are layed out in an identical
2921 manner in the two compilers. Note also that if output from two different
2922 compilers is mixed, the program is responsible for dealing with elaboration
2923 issues. Probably the safest approach is to write the main program in the
2924 version of Ada other than GNAT, so that it takes care of its own elaboration
2925 requirements, and then call the GNAT-generated adainit procedure to ensure
2926 elaboration of the GNAT components. Consult the documentation of the other
2927 Ada compiler for further details on elaboration.
2928
2929 However, it is not possible to mix the tasking run time of GNAT and
2930 HP Ada 83, All the tasking operations must either be entirely within
2931 GNAT compiled sections of the program, or entirely within HP Ada 83
2932 compiled sections of the program.
2933
2934 @cindex Interfacing to Assembly
2935 @cindex Convention Assembler
2936 @item Assembler
2937 Specifies assembler as the convention. In practice this has the
2938 same effect as convention Ada (but is not equivalent in the sense of being
2939 considered the same convention).
2940
2941 @cindex Convention Asm
2942 @findex Asm
2943 @item Asm
2944 Equivalent to Assembler.
2945
2946 @cindex Interfacing to COBOL
2947 @cindex Convention COBOL
2948 @findex COBOL
2949 @item COBOL
2950 Data will be passed according to the conventions described
2951 in section B.4 of the Ada 95 Reference Manual.
2952
2953 @findex C
2954 @cindex Interfacing to C
2955 @cindex Convention C
2956 @item C
2957 Data will be passed according to the conventions described
2958 in section B.3 of the Ada 95 Reference Manual.
2959
2960 @findex C varargs function
2961 @cindex Intefacing to C varargs function
2962 @cindex varargs function interfaces
2963 @item C varargs function
2964 In C, @code{varargs} allows a function to take a variable number of
2965 arguments. There is no direct equivalent in this to Ada. One
2966 approach that can be used is to create a C wrapper for each
2967 different profile and then interface to this C wrapper. For
2968 example, to print an @code{int} value using @code{printf},
2969 create a C function @code{printfi} that takes two arguments, a
2970 pointer to a string and an int, and calls @code{printf}.
2971 Then in the Ada program, use pragma @code{Import} to
2972 interface to printfi.
2973
2974 It may work on some platforms to directly interface to
2975 a @code{varargs} function by providing a specific Ada profile
2976 for a a particular call. However, this does not work on
2977 all platforms, since there is no guarantee that the
2978 calling sequence for a two argument normal C function
2979 is the same as for calling a @code{varargs} C function with
2980 the same two arguments.
2981
2982 @cindex Convention Default
2983 @findex Default
2984 @item Default
2985 Equivalent to C.
2986
2987 @cindex Convention External
2988 @findex External
2989 @item External
2990 Equivalent to C.
2991
2992 @findex C++
2993 @cindex Interfacing to C++
2994 @cindex Convention C++
2995 @item CPP
2996 This stands for C++. For most purposes this is identical to C.
2997 See the separate description of the specialized GNAT pragmas relating to
2998 C++ interfacing for further details.
2999
3000 @findex Fortran
3001 @cindex Interfacing to Fortran
3002 @cindex Convention Fortran
3003 @item Fortran
3004 Data will be passed according to the conventions described
3005 in section B.5 of the Ada 95 Reference Manual.
3006
3007 @item Intrinsic
3008 This applies to an intrinsic operation, as defined in the Ada 95
3009 Reference Manual. If a a pragma Import (Intrinsic) applies to a subprogram,
3010 this means that the body of the subprogram is provided by the compiler itself,
3011 usually by means of an efficient code sequence, and that the user does not
3012 supply an explicit body for it. In an application program, the pragma can
3013 only be applied to the following two sets of names, which the GNAT compiler
3014 recognizes.
3015
3016 @itemize @bullet
3017 @item
3018 Rotate_Left, Rotate_Right, Shift_Left, Shift_Right, Shift_Right_-
3019 Arithmetic.  The corresponding subprogram declaration must have
3020 two formal parameters. The
3021 first one must be a signed integer type or a modular type with a binary
3022 modulus, and the second parameter must be of type Natural.
3023 The return type must be the same as the type of the first argument. The size
3024 of this type can only be 8, 16, 32, or 64.
3025 @item binary arithmetic operators: ``+'', ``-'', ``*'', ``/''
3026 The corresponding operator declaration must have parameters and result type
3027 that have the same root numeric type (for example, all three are long_float
3028 types). This simplifies the definition of operations that use type checking
3029 to perform dimensional checks:
3030
3031 @smallexample @c ada
3032 type Distance is new Long_Float;
3033 type Time     is new Long_Float;
3034 type Velocity is new Long_Float;
3035 function "/" (D : Distance; T : Time)
3036   return Velocity;
3037 pragma Import (Intrinsic, "/");
3038 @end smallexample
3039
3040 @noindent
3041 This common idiom is often programmed with a generic definition and an
3042 explicit body. The pragma makes it simpler to introduce such declarations.
3043 It incurs no overhead in compilation time or code size, because it is
3044 implemented as a single machine instruction.
3045 @end itemize
3046 @noindent
3047
3048 @ifset unw
3049 @findex Stdcall
3050 @cindex Convention Stdcall
3051 @item Stdcall
3052 This is relevant only to Windows XP/2000/NT/95 implementations of GNAT,
3053 and specifies that the @code{Stdcall} calling sequence will be used,
3054 as defined by the NT API. Nevertheless, to ease building
3055 cross-platform bindings this convention will be handled as a @code{C} calling
3056 convention on non Windows platforms.
3057
3058 @findex DLL
3059 @cindex Convention DLL
3060 @item DLL
3061 This is equivalent to @code{Stdcall}.
3062
3063 @findex Win32
3064 @cindex Convention Win32
3065 @item Win32
3066 This is equivalent to @code{Stdcall}.
3067 @end ifset
3068
3069 @findex Stubbed
3070 @cindex Convention Stubbed
3071 @item Stubbed
3072 This is a special convention that indicates that the compiler
3073 should provide a stub body that raises @code{Program_Error}.
3074 @end table
3075
3076 @noindent
3077 GNAT additionally provides a useful pragma @code{Convention_Identifier}
3078 that can be used to parametrize conventions and allow additional synonyms
3079 to be specified. For example if you have legacy code in which the convention
3080 identifier Fortran77 was used for Fortran, you can use the configuration
3081 pragma:
3082
3083 @smallexample @c ada
3084 pragma Convention_Identifier (Fortran77, Fortran);
3085 @end smallexample
3086
3087 @noindent
3088 And from now on the identifier Fortran77 may be used as a convention
3089 identifier (for example in an @code{Import} pragma) with the same
3090 meaning as Fortran.
3091
3092 @node Building Mixed Ada & C++ Programs
3093 @section Building Mixed Ada & C++ Programs
3094
3095 @noindent
3096 A programmer inexperienced with mixed-language development may find that
3097 building an application containing both Ada and C++ code can be a
3098 challenge. As a matter of fact, interfacing with C++ has not been
3099 standardized in the Ada 95 Reference Manual due to the immaturity of --
3100 and lack of standards for -- C++ at the time. This section gives a few
3101 hints that should make this task easier. The first section addresses
3102 the differences regarding interfacing with C. The second section
3103 looks into the delicate problem of linking the complete application from
3104 its Ada and C++ parts. The last section gives some hints on how the GNAT
3105 run time can be adapted in order to allow inter-language dispatching
3106 with a new C++ compiler.
3107
3108 @menu
3109 * Interfacing to C++::
3110 * Linking a Mixed C++ & Ada Program::
3111 * A Simple Example::
3112 * Adapting the Run Time to a New C++ Compiler::
3113 @end menu
3114
3115 @node Interfacing to C++
3116 @subsection Interfacing to C++
3117
3118 @noindent
3119 GNAT supports interfacing with C++ compilers generating code that is
3120 compatible with the standard Application Binary Interface of the given
3121 platform.
3122
3123 @noindent
3124 Interfacing can be done at 3 levels: simple data, subprograms, and
3125 classes. In the first two cases, GNAT offers a specific @var{Convention
3126 CPP} that behaves exactly like @var{Convention C}. Usually, C++ mangles
3127 the names of subprograms, and currently, GNAT does not provide any help
3128 to solve the demangling problem. This problem can be addressed in two
3129 ways:
3130 @itemize @bullet
3131 @item
3132 by modifying the C++ code in order to force a C convention using
3133 the @code{extern "C"} syntax.
3134
3135 @item
3136 by figuring out the mangled name and use it as the Link_Name argument of
3137 the pragma import.
3138 @end itemize
3139
3140 @noindent
3141 Interfacing at the class level can be achieved by using the GNAT specific
3142 pragmas such as @code{CPP_Class} and @code{CPP_Virtual}. See the GNAT
3143 Reference Manual for additional information.
3144
3145 @node Linking a Mixed C++ & Ada Program
3146 @subsection Linking a Mixed C++ & Ada Program
3147
3148 @noindent
3149 Usually the linker of the C++ development system must be used to link
3150 mixed applications because most C++ systems will resolve elaboration
3151 issues (such as calling constructors on global class instances)
3152 transparently during the link phase. GNAT has been adapted to ease the
3153 use of a foreign linker for the last phase. Three cases can be
3154 considered:
3155 @enumerate
3156
3157 @item
3158 Using GNAT and G++ (GNU C++ compiler) from the same GCC installation:
3159 The C++ linker can simply be called by using the C++ specific driver
3160 called @code{c++}. Note that this setup is not very common because it
3161 may involve recompiling the whole GCC tree from sources, which makes it
3162 harder to upgrade the compilation system for one language without
3163 destabilizing the other.
3164
3165 @smallexample
3166 $ c++ -c file1.C
3167 $ c++ -c file2.C
3168 $ gnatmake ada_unit -largs file1.o file2.o --LINK=c++
3169 @end smallexample
3170
3171 @item
3172 Using GNAT and G++ from two different GCC installations: If both
3173 compilers are on the PATH, the previous method may be used. It is
3174 important to note that environment variables such as C_INCLUDE_PATH,
3175 GCC_EXEC_PREFIX, BINUTILS_ROOT, and GCC_ROOT will affect both compilers
3176 at the same time and may make one of the two compilers operate
3177 improperly if set during invocation of the wrong compiler.  It is also
3178 very important that the linker uses the proper @file{libgcc.a} GCC
3179 library -- that is, the one from the C++ compiler installation. The
3180 implicit link command as suggested in the gnatmake command from the
3181 former example can be replaced by an explicit link command with the
3182 full-verbosity option in order to verify which library is used:
3183 @smallexample
3184 $ gnatbind ada_unit
3185 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=c++
3186 @end smallexample
3187 If there is a problem due to interfering environment variables, it can
3188 be worked around by using an intermediate script. The following example
3189 shows the proper script to use when GNAT has not been installed at its
3190 default location and g++ has been installed at its default location:
3191
3192 @smallexample
3193 $ cat ./my_script
3194 #!/bin/sh
3195 unset BINUTILS_ROOT
3196 unset GCC_ROOT
3197 c++ $*
3198 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=./my_script
3199 @end smallexample
3200
3201 @item
3202 Using a non-GNU C++ compiler: The commands previously described can be
3203 used to insure that the C++ linker is used. Nonetheless, you need to add
3204 a few more parameters to the link command line, depending on the exception
3205 mechanism used.
3206
3207 If the @code{setjmp/longjmp} exception mechanism is used, only the paths
3208 to the libgcc libraries are required:
3209
3210 @smallexample
3211 $ cat ./my_script
3212 #!/bin/sh
3213 CC $* `gcc -print-file-name=libgcc.a` `gcc -print-file-name=libgcc_eh.a`
3214 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
3215 @end smallexample
3216
3217 Where CC is the name of the non-GNU C++ compiler.
3218
3219 If the @code{zero cost} exception mechanism is used, and the platform
3220 supports automatic registration of exception tables (e.g. Solaris or IRIX),
3221 paths to more objects are required:
3222
3223 @smallexample
3224 $ cat ./my_script
3225 #!/bin/sh
3226 CC `gcc -print-file-name=crtbegin.o` $* \
3227 `gcc -print-file-name=libgcc.a` `gcc -print-file-name=libgcc_eh.a` \
3228 `gcc -print-file-name=crtend.o`
3229 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
3230 @end smallexample
3231
3232 If the @code{zero cost} exception mechanism is used, and the platform
3233 doesn't support automatic registration of exception tables (e.g. HP-UX,
3234 Tru64 or AIX), the simple approach described above will not work and
3235 a pre-linking phase using GNAT will be necessary.
3236
3237 @end enumerate
3238
3239 @node A Simple Example
3240 @subsection  A Simple Example
3241 @noindent
3242 The following example, provided as part of the GNAT examples, shows how
3243 to achieve procedural interfacing between Ada and C++ in both
3244 directions. The C++ class A has two methods. The first method is exported
3245 to Ada by the means of an extern C wrapper function. The second method
3246 calls an Ada subprogram. On the Ada side, The C++ calls are modelled by
3247 a limited record with a layout comparable to the C++ class. The Ada
3248 subprogram, in turn, calls the C++ method. So, starting from the C++
3249 main program, the process passes back and forth between the two
3250 languages.
3251
3252 @noindent
3253 Here are the compilation commands:
3254 @smallexample
3255 $ gnatmake -c simple_cpp_interface
3256 $ c++ -c cpp_main.C
3257 $ c++ -c ex7.C
3258 $ gnatbind -n simple_cpp_interface
3259 $ gnatlink simple_cpp_interface -o cpp_main --LINK=$(CPLUSPLUS)
3260       -lstdc++ ex7.o cpp_main.o
3261 @end smallexample
3262
3263 @noindent
3264 Here are the corresponding sources:
3265 @smallexample
3266
3267 //cpp_main.C
3268
3269 #include "ex7.h"
3270
3271 extern "C" @{
3272   void adainit (void);
3273   void adafinal (void);
3274   void method1 (A *t);
3275 @}
3276
3277 void method1 (A *t)
3278 @{
3279   t->method1 ();
3280 @}
3281
3282 int main ()
3283 @{
3284   A obj;
3285   adainit ();
3286   obj.method2 (3030);
3287   adafinal ();
3288 @}
3289
3290 //ex7.h
3291
3292 class Origin @{
3293  public:
3294   int o_value;
3295 @};
3296 class A : public Origin @{
3297  public:
3298   void method1 (void);
3299   void method2 (int v);
3300   A();
3301   int   a_value;
3302 @};
3303
3304 //ex7.C
3305
3306 #include "ex7.h"
3307 #include <stdio.h>
3308
3309 extern "C" @{ void ada_method2 (A *t, int v);@}
3310
3311 void A::method1 (void)
3312 @{
3313   a_value = 2020;
3314   printf ("in A::method1, a_value = %d \n",a_value);
3315
3316 @}
3317
3318 void A::method2 (int v)
3319 @{
3320    ada_method2 (this, v);
3321    printf ("in A::method2, a_value = %d \n",a_value);
3322
3323 @}
3324
3325 A::A(void)
3326 @{
3327    a_value = 1010;
3328   printf ("in A::A, a_value = %d \n",a_value);
3329 @}
3330
3331 -- Ada sources
3332 @b{package} @b{body} Simple_Cpp_Interface @b{is}
3333
3334    @b{procedure} Ada_Method2 (This : @b{in} @b{out} A; V : Integer) @b{is}
3335    @b{begin}
3336       Method1 (This);
3337       This.A_Value := V;
3338    @b{end} Ada_Method2;
3339
3340 @b{end} Simple_Cpp_Interface;
3341
3342 @b{package} Simple_Cpp_Interface @b{is}
3343    @b{type} A @b{is} @b{limited}
3344       @b{record}
3345          O_Value : Integer;
3346          A_Value : Integer;
3347       @b{end} @b{record};
3348    @b{pragma} Convention (C, A);
3349
3350    @b{procedure} Method1 (This : @b{in} @b{out} A);
3351    @b{pragma} Import (C, Method1);
3352
3353    @b{procedure} Ada_Method2 (This : @b{in} @b{out} A; V : Integer);
3354    @b{pragma} Export (C, Ada_Method2);
3355
3356 @b{end} Simple_Cpp_Interface;
3357 @end smallexample
3358
3359 @node Adapting the Run Time to a New C++ Compiler
3360 @subsection Adapting the Run Time to a New C++ Compiler
3361 @noindent
3362 GNAT offers the capability to derive Ada 95 tagged types directly from
3363 preexisting C++ classes and . See ``Interfacing with C++'' in the
3364 @cite{GNAT Reference Manual}. The mechanism used by GNAT for achieving
3365 such a goal
3366 has been made user configurable through a GNAT library unit
3367 @code{Interfaces.CPP}. The default version of this file is adapted to
3368 the GNU C++ compiler. Internal knowledge of the virtual
3369 table layout used by the new C++ compiler is needed to configure
3370 properly this unit. The Interface of this unit is known by the compiler
3371 and cannot be changed except for the value of the constants defining the
3372 characteristics of the virtual table: CPP_DT_Prologue_Size, CPP_DT_Entry_Size,
3373 CPP_TSD_Prologue_Size, CPP_TSD_Entry_Size. Read comments in the source
3374 of this unit for more details.
3375
3376 @node Comparison between GNAT and C/C++ Compilation Models
3377 @section Comparison between GNAT and C/C++ Compilation Models
3378
3379 @noindent
3380 The GNAT model of compilation is close to the C and C++ models. You can
3381 think of Ada specs as corresponding to header files in C. As in C, you
3382 don't need to compile specs; they are compiled when they are used. The
3383 Ada @code{with} is similar in effect to the @code{#include} of a C
3384 header.
3385
3386 One notable difference is that, in Ada, you may compile specs separately
3387 to check them for semantic and syntactic accuracy. This is not always
3388 possible with C headers because they are fragments of programs that have
3389 less specific syntactic or semantic rules.
3390
3391 The other major difference is the requirement for running the binder,
3392 which performs two important functions. First, it checks for
3393 consistency. In C or C++, the only defense against assembling
3394 inconsistent programs lies outside the compiler, in a makefile, for
3395 example. The binder satisfies the Ada requirement that it be impossible
3396 to construct an inconsistent program when the compiler is used in normal
3397 mode.
3398
3399 @cindex Elaboration order control
3400 The other important function of the binder is to deal with elaboration
3401 issues. There are also elaboration issues in C++ that are handled
3402 automatically. This automatic handling has the advantage of being
3403 simpler to use, but the C++ programmer has no control over elaboration.
3404 Where @code{gnatbind} might complain there was no valid order of
3405 elaboration, a C++ compiler would simply construct a program that
3406 malfunctioned at run time.
3407
3408 @node Comparison between GNAT and Conventional Ada Library Models
3409 @section Comparison between GNAT and Conventional Ada Library Models
3410
3411 @noindent
3412 This section is intended to be useful to Ada programmers who have
3413 previously used an Ada compiler implementing the traditional Ada library
3414 model, as described in the Ada 95 Language Reference Manual. If you
3415 have not used such a system, please go on to the next section.
3416
3417 @cindex GNAT library
3418 In GNAT, there is no @dfn{library} in the normal sense. Instead, the set of
3419 source files themselves acts as the library. Compiling Ada programs does
3420 not generate any centralized information, but rather an object file and
3421 a ALI file, which are of interest only to the binder and linker.
3422 In a traditional system, the compiler reads information not only from
3423 the source file being compiled, but also from the centralized library.
3424 This means that the effect of a compilation depends on what has been
3425 previously compiled. In particular:
3426
3427 @itemize @bullet
3428 @item
3429 When a unit is @code{with}'ed, the unit seen by the compiler corresponds
3430 to the version of the unit most recently compiled into the library.
3431
3432 @item
3433 Inlining is effective only if the necessary body has already been
3434 compiled into the library.
3435
3436 @item
3437 Compiling a unit may obsolete other units in the library.
3438 @end itemize
3439
3440 @noindent
3441 In GNAT, compiling one unit never affects the compilation of any other
3442 units because the compiler reads only source files. Only changes to source
3443 files can affect the results of a compilation. In particular:
3444
3445 @itemize @bullet
3446 @item
3447 When a unit is @code{with}'ed, the unit seen by the compiler corresponds
3448 to the source version of the unit that is currently accessible to the
3449 compiler.
3450
3451 @item
3452 @cindex Inlining
3453 Inlining requires the appropriate source files for the package or
3454 subprogram bodies to be available to the compiler. Inlining is always
3455 effective, independent of the order in which units are complied.
3456
3457 @item
3458 Compiling a unit never affects any other compilations. The editing of
3459 sources may cause previous compilations to be out of date if they
3460 depended on the source file being modified.
3461 @end itemize
3462
3463 @noindent
3464 The most important result of these differences is that order of compilation
3465 is never significant in GNAT. There is no situation in which one is
3466 required to do one compilation before another. What shows up as order of
3467 compilation requirements in the traditional Ada library becomes, in
3468 GNAT, simple source dependencies; in other words, there is only a set
3469 of rules saying what source files must be present when a file is
3470 compiled.
3471
3472 @ifset vms
3473 @node Placement of temporary files
3474 @section Placement of temporary files
3475 @cindex Temporary files (user control over placement)
3476
3477 @noindent
3478 GNAT creates temporary files in the directory designated by the environment
3479 variable @env{TMPDIR}.
3480 (See the HP @emph{C RTL Reference Manual} on the function @code{getenv()}
3481 for detailed information on how environment variables are resolved.
3482 For most users the easiest way to make use of this feature is to simply
3483 define @env{TMPDIR} as a job level logical name).
3484 For example, if you wish to use a Ramdisk (assuming DECRAM is installed)
3485 for compiler temporary files, then you can include something like the
3486 following command in your @file{LOGIN.COM} file:
3487
3488 @smallexample
3489 $ define/job TMPDIR "/disk$scratchram/000000/temp/"
3490 @end smallexample
3491
3492 @noindent
3493 If @env{TMPDIR} is not defined, then GNAT uses the directory designated by
3494 @env{TMP}; if @env{TMP} is not defined, then GNAT uses the directory
3495 designated by @env{TEMP}.
3496 If none of these environment variables are defined then GNAT uses the
3497 directory designated by the logical name @code{SYS$SCRATCH:}
3498 (by default the user's home directory). If all else fails
3499 GNAT uses the current directory for temporary files.
3500 @end ifset
3501
3502 @c *************************
3503 @node Compiling Using gcc
3504 @chapter Compiling Using @command{gcc}
3505
3506 @noindent
3507 This chapter discusses how to compile Ada programs using the @command{gcc}
3508 command. It also describes the set of switches
3509 that can be used to control the behavior of the compiler.
3510 @menu
3511 * Compiling Programs::
3512 * Switches for gcc::
3513 * Search Paths and the Run-Time Library (RTL)::
3514 * Order of Compilation Issues::
3515 * Examples::
3516 @end menu
3517
3518 @node Compiling Programs
3519 @section Compiling Programs
3520
3521 @noindent
3522 The first step in creating an executable program is to compile the units
3523 of the program using the @command{gcc} command. You must compile the
3524 following files:
3525
3526 @itemize @bullet
3527 @item
3528 the body file (@file{.adb}) for a library level subprogram or generic
3529 subprogram
3530
3531 @item
3532 the spec file (@file{.ads}) for a library level package or generic
3533 package that has no body
3534
3535 @item
3536 the body file (@file{.adb}) for a library level package
3537 or generic package that has a body
3538
3539 @end itemize
3540
3541 @noindent
3542 You need @emph{not} compile the following files
3543
3544 @itemize @bullet
3545
3546 @item
3547 the spec of a library unit which has a body
3548
3549 @item
3550 subunits
3551 @end itemize
3552
3553 @noindent
3554 because they are compiled as part of compiling related units. GNAT
3555 package specs
3556 when the corresponding body is compiled, and subunits when the parent is
3557 compiled.
3558
3559 @cindex cannot generate code
3560 If you attempt to compile any of these files, you will get one of the
3561 following error messages (where fff is the name of the file you compiled):
3562
3563 @smallexample
3564 cannot generate code for file @var{fff} (package spec)
3565 to check package spec, use -gnatc
3566
3567 cannot generate code for file @var{fff} (missing subunits)
3568 to check parent unit, use -gnatc
3569
3570 cannot generate code for file @var{fff} (subprogram spec)
3571 to check subprogram spec, use -gnatc
3572
3573 cannot generate code for file @var{fff} (subunit)
3574 to check subunit, use -gnatc
3575 @end smallexample
3576
3577 @noindent
3578 As indicated by the above error messages, if you want to submit
3579 one of these files to the compiler to check for correct semantics
3580 without generating code, then use the @option{-gnatc} switch.
3581
3582 The basic command for compiling a file containing an Ada unit is
3583
3584 @smallexample
3585 $ gcc -c [@var{switches}] @file{file name}
3586 @end smallexample
3587
3588 @noindent
3589 where @var{file name} is the name of the Ada file (usually
3590 having an extension
3591 @file{.ads} for a spec or @file{.adb} for a body).
3592 @ifclear vms
3593 You specify the
3594 @option{-c} switch to tell @command{gcc} to compile, but not link, the file.
3595 @end ifclear
3596 The result of a successful compilation is an object file, which has the
3597 same name as the source file but an extension of @file{.o} and an Ada
3598 Library Information (ALI) file, which also has the same name as the
3599 source file, but with @file{.ali} as the extension. GNAT creates these
3600 two output files in the current directory, but you may specify a source
3601 file in any directory using an absolute or relative path specification
3602 containing the directory information.
3603
3604 @findex gnat1
3605 @command{gcc} is actually a driver program that looks at the extensions of
3606 the file arguments and loads the appropriate compiler. For example, the
3607 GNU C compiler is @file{cc1}, and the Ada compiler is @file{gnat1}.
3608 These programs are in directories known to the driver program (in some
3609 configurations via environment variables you set), but need not be in
3610 your path. The @command{gcc} driver also calls the assembler and any other
3611 utilities needed to complete the generation of the required object
3612 files.
3613
3614 It is possible to supply several file names on the same @command{gcc}
3615 command. This causes @command{gcc} to call the appropriate compiler for
3616 each file. For example, the following command lists three separate
3617 files to be compiled:
3618
3619 @smallexample
3620 $ gcc -c x.adb y.adb z.c
3621 @end smallexample
3622
3623 @noindent
3624 calls @code{gnat1} (the Ada compiler) twice to compile @file{x.adb} and
3625 @file{y.adb}, and @code{cc1} (the C compiler) once to compile @file{z.c}.
3626 The compiler generates three object files @file{x.o}, @file{y.o} and
3627 @file{z.o} and the two ALI files @file{x.ali} and @file{y.ali} from the
3628 Ada compilations. Any switches apply to all the files ^listed,^listed.^
3629 @ifclear vms
3630 except for
3631 @option{-gnat@var{x}} switches, which apply only to Ada compilations.
3632 @end ifclear
3633
3634 @node Switches for gcc
3635 @section Switches for @command{gcc}
3636
3637 @noindent
3638 The @command{gcc} command accepts switches that control the
3639 compilation process. These switches are fully described in this section.
3640 First we briefly list all the switches, in alphabetical order, then we
3641 describe the switches in more detail in functionally grouped sections.
3642
3643 More switches exist for GCC than those documented here, especially
3644 for specific targets. However, their use is not recommended as
3645 they may change code generation in ways that are incompatible with
3646 the Ada run-time library, or can cause inconsistencies between
3647 compilation units.
3648
3649 @menu
3650 * Output and Error Message Control::
3651 * Warning Message Control::
3652 * Debugging and Assertion Control::
3653 * Validity Checking::
3654 * Style Checking::
3655 * Run-Time Checks::
3656 * Stack Overflow Checking::
3657 * Using gcc for Syntax Checking::
3658 * Using gcc for Semantic Checking::
3659 * Compiling Different Versions of Ada::
3660 * Character Set Control::
3661 * File Naming Control::
3662 * Subprogram Inlining Control::
3663 * Auxiliary Output Control::
3664 * Debugging Control::
3665 * Exception Handling Control::
3666 * Units to Sources Mapping Files::
3667 * Integrated Preprocessing::
3668 * Code Generation Control::
3669 @ifset vms
3670 * Return Codes::
3671 @end ifset
3672 @end menu
3673
3674 @table @option
3675 @c !sort!
3676 @ifclear vms
3677 @cindex @option{-b} (@command{gcc})
3678 @item -b @var{target}
3679 Compile your program to run on @var{target}, which is the name of a
3680 system configuration. You must have a GNAT cross-compiler built if
3681 @var{target} is not the same as your host system.
3682
3683 @item -B@var{dir}
3684 @cindex @option{-B} (@command{gcc})
3685 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
3686 from @var{dir} instead of the default location. Only use this switch
3687 when multiple versions of the GNAT compiler are available. See the
3688 @command{gcc} manual page for further details. You would normally use the
3689 @option{-b} or @option{-V} switch instead.
3690
3691 @item -c
3692 @cindex @option{-c} (@command{gcc})
3693 Compile. Always use this switch when compiling Ada programs.
3694
3695 Note: for some other languages when using @command{gcc}, notably in
3696 the case of C and C++, it is possible to use
3697 use @command{gcc} without a @option{-c} switch to
3698 compile and link in one step. In the case of GNAT, you
3699 cannot use this approach, because the binder must be run
3700 and @command{gcc} cannot be used to run the GNAT binder.
3701 @end ifclear
3702
3703 @item -fno-inline
3704 @cindex @option{-fno-inline} (@command{gcc})
3705 Suppresses all back-end inlining, even if other optimization or inlining
3706 switches are set.
3707 This includes suppression of inlining that results
3708 from the use of the pragma @code{Inline_Always}.
3709 See also @option{-gnatn} and @option{-gnatN}.
3710
3711 @item -fno-strict-aliasing
3712 @cindex @option{-fno-strict-aliasing} (@command{gcc})
3713 Causes the compiler to avoid assumptions regarding non-aliasing
3714 of objects of different types. See
3715 @ref{Optimization and Strict Aliasing} for details.
3716
3717 @item -fstack-check
3718 @cindex @option{-fstack-check} (@command{gcc})
3719 Activates stack checking.
3720 See @ref{Stack Overflow Checking} for details of the use of this option.
3721
3722 @item -fstack-usage
3723 @cindex @option{-fstack-usage} (@command{gcc})
3724 Makes the compiler output stack usage information for the program, on a
3725 per-function basis.  The description of the format is to be found in
3726 the GCC documentation.
3727
3728 @item -fcallgraph-info
3729 @cindex @option{-fcallgraph-info} (@command{gcc})
3730 Makes the compiler output callgraph information for the program, on a
3731 per-file basis.  The information is generated in the VCG format.  It can
3732 be decorated with additional, per-node information if other debugging
3733 options are enabled (only works with -fstack-usage as of this writing).
3734
3735 @item ^-g^/DEBUG^
3736 @cindex @option{^-g^/DEBUG^} (@command{gcc})
3737 Generate debugging information. This information is stored in the object
3738 file and copied from there to the final executable file by the linker,
3739 where it can be read by the debugger. You must use the
3740 @option{^-g^/DEBUG^} switch if you plan on using the debugger.
3741
3742 @item -gnat83
3743 @cindex @option{-gnat83} (@command{gcc})
3744 Enforce Ada 83 restrictions.
3745
3746 @item -gnat95
3747 @cindex @option{-gnat95} (@command{gcc})
3748 Enforce Ada 95 restrictions.
3749
3750 @item -gnat05
3751 @cindex @option{-gnat05} (@command{gcc})
3752 Allow full Ada 2005 features.
3753
3754 @item -gnata
3755 @cindex @option{-gnata} (@command{gcc})
3756 Assertions enabled. @code{Pragma Assert} and @code{pragma Debug} to be
3757 activated. Note that these pragmas can also be controlled using the
3758 configuration pragmas @code{Assertion_Policy} and @code{Debug_Policy}.
3759
3760 @item -gnatA
3761 @cindex @option{-gnatA} (@command{gcc})
3762 Avoid processing @file{gnat.adc}. If a gnat.adc file is present,
3763 it will be ignored.
3764
3765 @item -gnatb
3766 @cindex @option{-gnatb} (@command{gcc})
3767 Generate brief messages to @file{stderr} even if verbose mode set.
3768
3769 @item -gnatc
3770 @cindex @option{-gnatc} (@command{gcc})
3771 Check syntax and semantics only (no code generation attempted).
3772
3773 @item -gnatd
3774 @cindex @option{-gnatd} (@command{gcc})
3775 Specify debug options for the compiler. The string of characters after
3776 the @option{-gnatd} specify the specific debug options. The possible
3777 characters are 0-9, a-z, A-Z, optionally preceded by a dot. See
3778 compiler source file @file{debug.adb} for details of the implemented
3779 debug options. Certain debug options are relevant to applications
3780 programmers, and these are documented at appropriate points in this
3781 users guide.
3782
3783 @item -gnatD
3784 @cindex @option{-gnatD} (@command{gcc})
3785 Create expanded source files for source level debugging. This switch
3786 also suppress generation of cross-reference information
3787 (see @option{-gnatx}).
3788
3789 @item -gnatec=@var{path}
3790 @cindex @option{-gnatec} (@command{gcc})
3791 Specify a configuration pragma file
3792 @ifclear vms
3793 (the equal sign is optional)
3794 @end ifclear
3795 (@pxref{The Configuration Pragmas Files}).
3796
3797 @item ^-gnateD^/DATA_PREPROCESSING=^symbol[=value]
3798 @cindex @option{-gnateD} (@command{gcc})
3799 Defines a symbol, associated with value, for preprocessing.
3800 (@pxref{Integrated Preprocessing}).
3801
3802 @item -gnatef
3803 @cindex @option{-gnatef} (@command{gcc})
3804 Display full source path name in brief error messages.
3805
3806 @item -gnatem=@var{path}
3807 @cindex @option{-gnatem} (@command{gcc})
3808 Specify a mapping file
3809 @ifclear vms
3810 (the equal sign is optional)
3811 @end ifclear
3812 (@pxref{Units to Sources Mapping Files}).
3813
3814 @item -gnatep=@var{file}
3815 @cindex @option{-gnatep} (@command{gcc})
3816 Specify a preprocessing data file
3817 @ifclear vms
3818 (the equal sign is optional)
3819 @end ifclear
3820 (@pxref{Integrated Preprocessing}).
3821
3822 @item -gnatE
3823 @cindex @option{-gnatE} (@command{gcc})
3824 Full dynamic elaboration checks.
3825
3826 @item -gnatf
3827 @cindex @option{-gnatf} (@command{gcc})
3828 Full errors. Multiple errors per line, all undefined references, do not
3829 attempt to suppress cascaded errors.
3830
3831 @item -gnatF
3832 @cindex @option{-gnatF} (@command{gcc})
3833 Externals names are folded to all uppercase.
3834
3835 @item -gnatg
3836 @cindex @option{-gnatg} (@command{gcc})
3837 Internal GNAT implementation mode. This should not be used for
3838 applications programs, it is intended only for use by the compiler
3839 and its run-time library. For documentation, see the GNAT sources.
3840 Note that @option{-gnatg} implies @option{-gnatwu} so that warnings
3841 are generated on unreferenced entities, and all warnings are treated
3842 as errors.
3843
3844 @item -gnatG
3845 @cindex @option{-gnatG} (@command{gcc})
3846 List generated expanded code in source form.
3847
3848 @item ^-gnath^/HELP^
3849 @cindex @option{^-gnath^/HELP^} (@command{gcc})
3850 Output usage information. The output is written to @file{stdout}.
3851
3852 @item ^-gnati^/IDENTIFIER_CHARACTER_SET=^@var{c}
3853 @cindex @option{^-gnati^/IDENTIFIER_CHARACTER_SET^} (@command{gcc})
3854 Identifier character set
3855 @ifclear vms
3856 (@var{c}=1/2/3/4/8/9/p/f/n/w).
3857 @end ifclear
3858 @ifset vms
3859 For details of the possible selections for @var{c},
3860 see @ref{Character Set Control}.
3861 @end ifset
3862
3863 @item -gnatk=@var{n}
3864 @cindex @option{-gnatk} (@command{gcc})
3865 Limit file names to @var{n} (1-999) characters ^(@code{k} = krunch)^^.
3866
3867 @item -gnatl
3868 @cindex @option{-gnatl} (@command{gcc})
3869 Output full source listing with embedded error messages.
3870
3871 @item -gnatm=@var{n}
3872 @cindex @option{-gnatm} (@command{gcc})
3873 Limit number of detected error or warning messages to @var{n}
3874 where @var{n} is in the range 1..999_999. The default setting if
3875 no switch is given is 9999. Compilation is terminated if this
3876 limit is exceeded. The equal sign here is optional.
3877
3878 @item -gnatn
3879 @cindex @option{-gnatn} (@command{gcc})
3880 Activate inlining for subprograms for which
3881 pragma @code{inline} is specified. This inlining is performed
3882 by the GCC back-end.
3883
3884 @item -gnatN
3885 @cindex @option{-gnatN} (@command{gcc})
3886 Activate front end inlining for subprograms for which
3887 pragma @code{Inline} is specified. This inlining is performed
3888 by the front end and will be visible in the
3889 @option{-gnatG} output.
3890 In some cases, this has proved more effective than the back end
3891 inlining resulting from the use of
3892 @option{-gnatn}.
3893 Note that
3894 @option{-gnatN} automatically implies
3895 @option{-gnatn} so it is not necessary
3896 to specify both options. There are a few cases that the back-end inlining
3897 catches that cannot be dealt with in the front-end.
3898
3899 @item -gnato
3900 @cindex @option{-gnato} (@command{gcc})
3901 Enable numeric overflow checking (which is not normally enabled by
3902 default). Not that division by zero is a separate check that is not
3903 controlled by this switch (division by zero checking is on by default).
3904
3905 @item -gnatp
3906 @cindex @option{-gnatp} (@command{gcc})
3907 Suppress all checks.
3908
3909 @item -gnatP
3910 @cindex @option{-gnatP} (@command{gcc})
3911 Enable polling. This is required on some systems (notably Windows NT) to
3912 obtain asynchronous abort and asynchronous transfer of control capability.
3913 See the description of pragma Polling in the GNAT Reference Manual for
3914 full details.
3915
3916 @item -gnatq
3917 @cindex @option{-gnatq} (@command{gcc})
3918 Don't quit; try semantics, even if parse errors.
3919
3920 @item -gnatQ
3921 @cindex @option{-gnatQ} (@command{gcc})
3922 Don't quit; generate @file{ALI} and tree files even if illegalities.
3923
3924 @item ^-gnatR[0/1/2/3[s]]^/REPRESENTATION_INFO^
3925 @cindex @option{-gnatR} (@command{gcc})
3926 Output representation information for declared types and objects.
3927
3928 @item -gnats
3929 @cindex @option{-gnats} (@command{gcc})
3930 Syntax check only.
3931
3932 @item -gnatS
3933 @cindex @option{-gnatS} (@command{gcc})
3934 Print package Standard.
3935
3936 @item -gnatt
3937 @cindex @option{-gnatt} (@command{gcc})
3938 Generate tree output file.
3939
3940 @item ^-gnatT^/TABLE_MULTIPLIER=^@var{nnn}
3941 @cindex @option{^-gnatT^/TABLE_MULTIPLIER^} (@command{gcc})
3942 All compiler tables start at @var{nnn} times usual starting size.
3943
3944 @item -gnatu
3945 @cindex @option{-gnatu} (@command{gcc})
3946 List units for this compilation.
3947
3948 @item -gnatU
3949 @cindex @option{-gnatU} (@command{gcc})
3950 Tag all error messages with the unique string ``error:''
3951
3952 @item -gnatv
3953 @cindex @option{-gnatv} (@command{gcc})
3954 Verbose mode. Full error output with source lines to @file{stdout}.
3955
3956 @item -gnatV
3957 @cindex @option{-gnatV} (@command{gcc})
3958 Control level of validity checking. See separate section describing
3959 this feature.
3960
3961 @item ^-gnatw@var{xxx}^/WARNINGS=(@var{option}[,...])^
3962 @cindex @option{^-gnatw^/WARNINGS^} (@command{gcc})
3963 Warning mode where
3964 ^@var{xxx} is a string of option letters that^the list of options^ denotes
3965 the exact warnings that
3966 are enabled or disabled (@pxref{Warning Message Control}).
3967
3968 @item ^-gnatW^/WIDE_CHARACTER_ENCODING=^@var{e}
3969 @cindex @option{^-gnatW^/WIDE_CHARACTER_ENCODING^} (@command{gcc})
3970 Wide character encoding method
3971 @ifclear vms
3972 (@var{e}=n/h/u/s/e/8).
3973 @end ifclear
3974 @ifset vms
3975 (@var{e}=@code{BRACKETS, NONE, HEX, UPPER, SHIFT_JIS, EUC, UTF8})
3976 @end ifset
3977
3978 @item -gnatx
3979 @cindex @option{-gnatx} (@command{gcc})
3980 Suppress generation of cross-reference information.
3981
3982 @item ^-gnaty^/STYLE_CHECKS=(option,option..)^
3983 @cindex @option{^-gnaty^/STYLE_CHECKS^} (@command{gcc})
3984 Enable built-in style checks (@pxref{Style Checking}).
3985
3986 @item ^-gnatz^/DISTRIBUTION_STUBS=^@var{m}
3987 @cindex @option{^-gnatz^/DISTRIBUTION_STUBS^} (@command{gcc})
3988 Distribution stub generation and compilation
3989 @ifclear vms
3990 (@var{m}=r/c for receiver/caller stubs).
3991 @end ifclear
3992 @ifset vms
3993 (@var{m}=@code{RECEIVER} or @code{CALLER} to specify the type of stubs
3994 to be generated and compiled).
3995 @end ifset
3996
3997 @item ^-I^/SEARCH=^@var{dir}
3998 @cindex @option{^-I^/SEARCH^} (@command{gcc})
3999 @cindex RTL
4000 Direct GNAT to search the @var{dir} directory for source files needed by
4001 the current compilation
4002 (@pxref{Search Paths and the Run-Time Library (RTL)}).
4003
4004 @item ^-I-^/NOCURRENT_DIRECTORY^
4005 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gcc})
4006 @cindex RTL
4007 Except for the source file named in the command line, do not look for source
4008 files in the directory containing the source file named in the command line
4009 (@pxref{Search Paths and the Run-Time Library (RTL)}).
4010
4011 @ifclear vms
4012 @item -mbig-switch
4013 @cindex @option{-mbig-switch} (@command{gcc})
4014 @cindex @code{case} statement (effect of @option{-mbig-switch} option)
4015 This standard gcc switch causes the compiler to use larger offsets in its
4016 jump table representation for @code{case} statements.
4017 This may result in less efficient code, but is sometimes necessary
4018 (for example on HP-UX targets)
4019 @cindex HP-UX and @option{-mbig-switch} option
4020 in order to compile large and/or nested @code{case} statements.
4021
4022 @item -o @var{file}
4023 @cindex @option{-o} (@command{gcc})
4024 This switch is used in @command{gcc} to redirect the generated object file
4025 and its associated ALI file. Beware of this switch with GNAT, because it may
4026 cause the object file and ALI file to have different names which in turn
4027 may confuse the binder and the linker.
4028 @end ifclear
4029
4030 @item -nostdinc
4031 @cindex @option{-nostdinc} (@command{gcc})
4032 Inhibit the search of the default location for the GNAT Run Time
4033 Library (RTL) source files.
4034
4035 @item -nostdlib
4036 @cindex @option{-nostdlib} (@command{gcc})
4037 Inhibit the search of the default location for the GNAT Run Time
4038 Library (RTL) ALI files.
4039
4040 @ifclear vms
4041 @item -O[@var{n}]
4042 @cindex @option{-O} (@command{gcc})
4043 @var{n} controls the optimization level.
4044
4045 @table @asis
4046 @item n = 0
4047 No optimization, the default setting if no @option{-O} appears
4048
4049 @item n = 1
4050 Normal optimization, the default if you specify @option{-O} without
4051 an operand. A good compromise between code quality and compilation
4052 time.
4053
4054 @item n = 2
4055 Extensive optimization, may improve execution time, possibly at the cost of
4056 substantially increased compilation time.
4057
4058 @end table
4059 @end ifclear
4060
4061 @ifset vms
4062 @item  /NOOPTIMIZE
4063 @cindex @option{/NOOPTIMIZE} (@code{GNAT COMPILE})
4064 Equivalent to @option{/OPTIMIZE=NONE}.
4065 This is the default behavior in the absence of an @option{/OPTMIZE}
4066 qualifier.
4067
4068 @item /OPTIMIZE[=(keyword[,...])]
4069 @cindex @option{/OPTIMIZE} (@code{GNAT COMPILE})
4070 Selects the level of optimization for your program. The supported
4071 keywords are as follows:
4072 @table @code
4073 @item   ALL
4074 Perform most optimizations, including those that
4075 are expensive.
4076 This is the default if the @option{/OPTMIZE} qualifier is supplied
4077 without keyword options.
4078
4079 @item   NONE
4080 Do not do any optimizations. Same as @code{/NOOPTIMIZE}.
4081
4082 @item SOME
4083 Perform some optimizations, but omit ones that are costly.
4084
4085 @item   DEVELOPMENT
4086 Same as @code{SOME}.
4087
4088 @item   UNROLL_LOOPS
4089 Try to unroll loops. This keyword may be specified together with
4090 any keyword above other than @code{NONE}. Loop unrolling
4091 usually, but not always, improves the performance of programs.
4092 @end table
4093 @end ifset
4094
4095 @ifclear vms
4096 @item -pass-exit-codes
4097 @cindex @option{-pass-exit-codes} (@command{gcc})
4098 Catch exit codes from the compiler and use the most meaningful as
4099 exit status.
4100 @end ifclear
4101
4102 @item --RTS=@var{rts-path}
4103 @cindex @option{--RTS} (@command{gcc})
4104 Specifies the default location of the runtime library. Same meaning as the
4105 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
4106
4107 @item ^-S^/ASM^
4108 @cindex @option{^-S^/ASM^} (@command{gcc})
4109 ^Used in place of @option{-c} to^Used to^
4110 cause the assembler source file to be
4111 generated, using @file{^.s^.S^} as the extension,
4112 instead of the object file.
4113 This may be useful if you need to examine the generated assembly code.
4114
4115 @item ^-fverbose-asm^/VERBOSE_ASM^
4116 @cindex @option{^-fverbose-asm^/VERBOSE_ASM^} (@command{gcc})
4117 ^Used in conjunction with @option{-S}^Used in place of @option{/ASM}^
4118 to cause the generated assembly code file to be annotated with variable
4119 names, making it significantly easier to follow.
4120
4121 @item ^-v^/VERBOSE^
4122 @cindex @option{^-v^/VERBOSE^} (@command{gcc})
4123 Show commands generated by the @command{gcc} driver. Normally used only for
4124 debugging purposes or if you need to be sure what version of the
4125 compiler you are executing.
4126
4127 @ifclear vms
4128 @item -V @var{ver}
4129 @cindex @option{-V} (@command{gcc})
4130 Execute @var{ver} version of the compiler. This is the @command{gcc}
4131 version, not the GNAT version.
4132 @end ifclear
4133
4134 @item ^-w^NO_BACK_END_WARNINGS^
4135 @cindex @option{-w} (@command{gcc})
4136 Turn off warnings generated by the back end of the compiler. Use of
4137 this switch also causes the default for front end warnings to be set
4138 to suppress (as though @option{-gnatws} had appeared at the start of
4139 the options.
4140
4141 @end table
4142
4143 @ifclear vms
4144 You may combine a sequence of GNAT switches into a single switch. For
4145 example, the combined switch
4146
4147 @cindex Combining GNAT switches
4148 @smallexample
4149 -gnatofi3
4150 @end smallexample
4151
4152 @noindent
4153 is equivalent to specifying the following sequence of switches:
4154
4155 @smallexample
4156 -gnato -gnatf -gnati3
4157 @end smallexample
4158 @end ifclear
4159
4160 @c NEED TO CHECK THIS FOR VMS
4161
4162 @noindent
4163 The following restrictions apply to the combination of switches
4164 in this manner:
4165
4166 @itemize @bullet
4167 @item
4168 The switch @option{-gnatc} if combined with other switches must come
4169 first in the string.
4170
4171 @item
4172 The switch @option{-gnats} if combined with other switches must come
4173 first in the string.
4174
4175 @item
4176 The switches
4177 @option{^-gnatz^/DISTRIBUTION_STUBS^}, @option{-gnatzc}, and @option{-gnatzr}
4178 may not be combined with any other switches.
4179
4180 @ifclear vms
4181 @item
4182 Once a ``y'' appears in the string (that is a use of the @option{-gnaty}
4183 switch), then all further characters in the switch are interpreted
4184 as style modifiers (see description of @option{-gnaty}).
4185
4186 @item
4187 Once a ``d'' appears in the string (that is a use of the @option{-gnatd}
4188 switch), then all further characters in the switch are interpreted
4189 as debug flags (see description of @option{-gnatd}).
4190
4191 @item
4192 Once a ``w'' appears in the string (that is a use of the @option{-gnatw}
4193 switch), then all further characters in the switch are interpreted
4194 as warning mode modifiers (see description of @option{-gnatw}).
4195
4196 @item
4197 Once a ``V'' appears in the string (that is a use of the @option{-gnatV}
4198 switch), then all further characters in the switch are interpreted
4199 as validity checking options (see description of @option{-gnatV}).
4200 @end ifclear
4201 @end itemize
4202
4203 @node Output and Error Message Control
4204 @subsection Output and Error Message Control
4205 @findex stderr
4206
4207 @noindent
4208 The standard default format for error messages is called ``brief format''.
4209 Brief format messages are written to @file{stderr} (the standard error
4210 file) and have the following form:
4211
4212 @smallexample
4213 e.adb:3:04: Incorrect spelling of keyword "function"
4214 e.adb:4:20: ";" should be "is"
4215 @end smallexample
4216
4217 @noindent
4218 The first integer after the file name is the line number in the file,
4219 and the second integer is the column number within the line.
4220 @code{glide} can parse the error messages
4221 and point to the referenced character.
4222 The following switches provide control over the error message
4223 format:
4224
4225 @table @option
4226 @c !sort!
4227 @item -gnatv
4228 @cindex @option{-gnatv} (@command{gcc})
4229 @findex stdout
4230 @ifclear vms
4231 The v stands for verbose.
4232 @end ifclear
4233 The effect of this setting is to write long-format error
4234 messages to @file{stdout} (the standard output file.
4235 The same program compiled with the
4236 @option{-gnatv} switch would generate:
4237
4238 @smallexample
4239 @cartouche
4240 3. funcion X (Q : Integer)
4241    |
4242 >>> Incorrect spelling of keyword "function"
4243 4. return Integer;
4244                  |
4245 >>> ";" should be "is"
4246 @end cartouche
4247 @end smallexample
4248
4249 @noindent
4250 The vertical bar indicates the location of the error, and the @samp{>>>}
4251 prefix can be used to search for error messages. When this switch is
4252 used the only source lines output are those with errors.
4253
4254 @item -gnatl
4255 @cindex @option{-gnatl} (@command{gcc})
4256 @ifclear vms
4257 The @code{l} stands for list.
4258 @end ifclear
4259 This switch causes a full listing of
4260 the file to be generated. The output might look as follows:
4261
4262 @smallexample
4263 @cartouche
4264  1. procedure E is
4265  2.    V : Integer;
4266  3.    funcion X (Q : Integer)
4267        |
4268     >>> Incorrect spelling of keyword "function"
4269  4.     return Integer;
4270                       |
4271     >>> ";" should be "is"
4272  5.    begin
4273  6.       return Q + Q;
4274  7.    end;
4275  8. begin
4276  9.    V := X + X;
4277 10.end E;
4278 @end cartouche
4279 @end smallexample
4280
4281 @noindent
4282 @findex stderr
4283 When you specify the @option{-gnatv} or @option{-gnatl} switches and
4284 standard output is redirected, a brief summary is written to
4285 @file{stderr} (standard error) giving the number of error messages and
4286 warning messages generated.
4287
4288 @item -gnatU
4289 @cindex @option{-gnatU} (@command{gcc})
4290 This switch forces all error messages to be preceded by the unique
4291 string ``error:''. This means that error messages take a few more
4292 characters in space, but allows easy searching for and identification
4293 of error messages.
4294
4295 @item -gnatb
4296 @cindex @option{-gnatb} (@command{gcc})
4297 @ifclear vms
4298 The @code{b} stands for brief.
4299 @end ifclear
4300 This switch causes GNAT to generate the
4301 brief format error messages to @file{stderr} (the standard error
4302 file) as well as the verbose
4303 format message or full listing (which as usual is written to
4304 @file{stdout} (the standard output file).
4305
4306 @item -gnatm=@var{n}
4307 @cindex @option{-gnatm} (@command{gcc})
4308 @ifclear vms
4309 The @code{m} stands for maximum.
4310 @end ifclear
4311 @var{n} is a decimal integer in the
4312 range of 1 to 999 and limits the number of error messages to be
4313 generated. For example, using @option{-gnatm2} might yield
4314
4315 @smallexample
4316 e.adb:3:04: Incorrect spelling of keyword "function"
4317 e.adb:5:35: missing ".."
4318 fatal error: maximum errors reached
4319 compilation abandoned
4320 @end smallexample
4321
4322 @noindent
4323 Note that the equal sign is optional, so the switches
4324 @option{-gnatm2} and @option{-gnatm=2} are equivalent.
4325
4326 @item -gnatf
4327 @cindex @option{-gnatf} (@command{gcc})
4328 @cindex Error messages, suppressing
4329 @ifclear vms
4330 The @code{f} stands for full.
4331 @end ifclear
4332 Normally, the compiler suppresses error messages that are likely to be
4333 redundant. This switch causes all error
4334 messages to be generated. In particular, in the case of
4335 references to undefined variables. If a given variable is referenced
4336 several times, the normal format of messages is
4337 @smallexample
4338 e.adb:7:07: "V" is undefined (more references follow)
4339 @end smallexample
4340
4341 @noindent
4342 where the parenthetical comment warns that there are additional
4343 references to the variable @code{V}. Compiling the same program with the
4344 @option{-gnatf} switch yields
4345
4346 @smallexample
4347 e.adb:7:07: "V" is undefined
4348 e.adb:8:07: "V" is undefined
4349 e.adb:8:12: "V" is undefined
4350 e.adb:8:16: "V" is undefined
4351 e.adb:9:07: "V" is undefined
4352 e.adb:9:12: "V" is undefined
4353 @end smallexample
4354
4355 @noindent
4356 The @option{-gnatf} switch also generates additional information for
4357 some error messages.  Some examples are:
4358
4359 @itemize @bullet
4360 @item
4361 Full details on entities not available in high integrity mode
4362 @item
4363 Details on possibly non-portable unchecked conversion
4364 @item
4365 List possible interpretations for ambiguous calls
4366 @item
4367 Additional details on incorrect parameters
4368 @end itemize
4369
4370 @item -gnatq
4371 @cindex @option{-gnatq} (@command{gcc})
4372 @ifclear vms
4373 The @code{q} stands for quit (really ``don't quit'').
4374 @end ifclear
4375 In normal operation mode, the compiler first parses the program and
4376 determines if there are any syntax errors. If there are, appropriate
4377 error messages are generated and compilation is immediately terminated.
4378 This switch tells
4379 GNAT to continue with semantic analysis even if syntax errors have been
4380 found. This may enable the detection of more errors in a single run. On
4381 the other hand, the semantic analyzer is more likely to encounter some
4382 internal fatal error when given a syntactically invalid tree.
4383
4384 @item -gnatQ
4385 @cindex @option{-gnatQ} (@command{gcc})
4386 In normal operation mode, the @file{ALI} file is not generated if any
4387 illegalities are detected in the program. The use of @option{-gnatQ} forces
4388 generation of the @file{ALI} file. This file is marked as being in
4389 error, so it cannot be used for binding purposes, but it does contain
4390 reasonably complete cross-reference information, and thus may be useful
4391 for use by tools (e.g. semantic browsing tools or integrated development
4392 environments) that are driven from the @file{ALI} file. This switch
4393 implies @option{-gnatq}, since the semantic phase must be run to get a
4394 meaningful ALI file.
4395
4396 In addition, if @option{-gnatt} is also specified, then the tree file is
4397 generated even if there are illegalities. It may be useful in this case
4398 to also specify @option{-gnatq} to ensure that full semantic processing
4399 occurs. The resulting tree file can be processed by ASIS, for the purpose
4400 of providing partial information about illegal units, but if the error
4401 causes the tree to be badly malformed, then ASIS may crash during the
4402 analysis.
4403
4404 When @option{-gnatQ} is used and the generated @file{ALI} file is marked as
4405 being in error, @command{gnatmake} will attempt to recompile the source when it
4406 finds such an @file{ALI} file, including with switch @option{-gnatc}.
4407
4408 Note that @option{-gnatQ} has no effect if @option{-gnats} is specified,
4409 since ALI files are never generated if @option{-gnats} is set.
4410
4411 @end table
4412
4413 @node Warning Message Control
4414 @subsection Warning Message Control
4415 @cindex Warning messages
4416 @noindent
4417 In addition to error messages, which correspond to illegalities as defined
4418 in the Ada 95 Reference Manual, the compiler detects two kinds of warning
4419 situations.
4420
4421 First, the compiler considers some constructs suspicious and generates a
4422 warning message to alert you to a possible error. Second, if the
4423 compiler detects a situation that is sure to raise an exception at
4424 run time, it generates a warning message. The following shows an example
4425 of warning messages:
4426 @smallexample
4427 e.adb:4:24: warning: creation of object may raise Storage_Error
4428 e.adb:10:17: warning: static value out of range
4429 e.adb:10:17: warning: "Constraint_Error" will be raised at run time
4430 @end smallexample
4431
4432 @noindent
4433 GNAT considers a large number of situations as appropriate
4434 for the generation of warning messages. As always, warnings are not
4435 definite indications of errors. For example, if you do an out-of-range
4436 assignment with the deliberate intention of raising a
4437 @code{Constraint_Error} exception, then the warning that may be
4438 issued does not indicate an error. Some of the situations for which GNAT
4439 issues warnings (at least some of the time) are given in the following
4440 list. This list is not complete, and new warnings are often added to
4441 subsequent versions of GNAT. The list is intended to give a general idea
4442 of the kinds of warnings that are generated.
4443
4444 @itemize @bullet
4445 @item
4446 Possible infinitely recursive calls
4447
4448 @item
4449 Out-of-range values being assigned
4450
4451 @item
4452 Possible order of elaboration problems
4453
4454 @item
4455 Unreachable code
4456
4457 @item
4458 Fixed-point type declarations with a null range
4459
4460 @item
4461 Direct_IO or Sequential_IO instantiated with a type that has access values
4462
4463 @item
4464 Variables that are never assigned a value
4465
4466 @item
4467 Variables that are referenced before being initialized
4468
4469 @item
4470 Task entries with no corresponding @code{accept} statement
4471
4472 @item
4473 Duplicate accepts for the same task entry in a @code{select}
4474
4475 @item
4476 Objects that take too much storage
4477
4478 @item
4479 Unchecked conversion between types of differing sizes
4480
4481 @item
4482 Missing @code{return} statement along some execution path in a function
4483
4484 @item
4485 Incorrect (unrecognized) pragmas
4486
4487 @item
4488 Incorrect external names
4489
4490 @item
4491 Allocation from empty storage pool
4492
4493 @item
4494 Potentially blocking operation in protected type
4495
4496 @item
4497 Suspicious parenthesization of expressions
4498
4499 @item
4500 Mismatching bounds in an aggregate
4501
4502 @item
4503 Attempt to return local value by reference
4504
4505 @item
4506 Premature instantiation of a generic body
4507
4508 @item
4509 Attempt to pack aliased components
4510
4511 @item
4512 Out of bounds array subscripts
4513
4514 @item
4515 Wrong length on string assignment
4516
4517 @item
4518 Violations of style rules if style checking is enabled
4519
4520 @item
4521 Unused @code{with} clauses
4522
4523 @item
4524 @code{Bit_Order} usage that does not have any effect
4525
4526 @item
4527 @code{Standard.Duration} used to resolve universal fixed expression
4528
4529 @item
4530 Dereference of possibly null value
4531
4532 @item
4533 Declaration that is likely to cause storage error
4534
4535 @item
4536 Internal GNAT unit @code{with}'ed by application unit
4537
4538 @item
4539 Values known to be out of range at compile time
4540
4541 @item
4542 Unreferenced labels and variables
4543
4544 @item
4545 Address overlays that could clobber memory
4546
4547 @item
4548 Unexpected initialization when address clause present
4549
4550 @item
4551 Bad alignment for address clause
4552
4553 @item
4554 Useless type conversions
4555
4556 @item
4557 Redundant assignment statements and other redundant constructs
4558
4559 @item
4560 Useless exception handlers
4561
4562 @item
4563 Accidental hiding of name by child unit
4564
4565 @item
4566 Access before elaboration detected at compile time
4567
4568 @item
4569 A range in a @code{for} loop that is known to be null or might be null
4570
4571 @end itemize
4572
4573 @noindent
4574 The following switches are available to control the handling of
4575 warning messages:
4576
4577 @table @option
4578 @c !sort!
4579 @item -gnatwa
4580 @emph{Activate all optional errors.}
4581 @cindex @option{-gnatwa} (@command{gcc})
4582 This switch activates most optional warning messages, see remaining list
4583 in this section for details on optional warning messages that can be
4584 individually controlled.  The warnings that are not turned on by this
4585 switch are
4586 @option{-gnatwd} (implicit dereferencing),
4587 @option{-gnatwh} (hiding),
4588 and @option{-gnatwl} (elaboration warnings).
4589 All other optional warnings are turned on.
4590
4591 @item -gnatwA
4592 @emph{Suppress all optional errors.}
4593 @cindex @option{-gnatwA} (@command{gcc})
4594 This switch suppresses all optional warning messages, see remaining list
4595 in this section for details on optional warning messages that can be
4596 individually controlled.
4597
4598 @item -gnatwb
4599 @emph{Activate warnings on bad fixed values.}
4600 @cindex @option{-gnatwb} (@command{gcc})
4601 @cindex Bad fixed values
4602 @cindex Fixed-point Small value
4603 @cindex Small value
4604 This switch activates warnings for static fixed-point expressions whose
4605 value is not an exact multiple of Small. Such values are implementation
4606 dependent, since an implementation is free to choose either of the multiples
4607 that surround the value. GNAT always chooses the closer one, but this is not
4608 required behavior, and it is better to specify a value that is an exact
4609 multiple, ensuring predictable execution. The default is that such warnings
4610 are not generated.
4611
4612 @item -gnatwB
4613 @emph{Suppress warnings on bad fixed values.}
4614 @cindex @option{-gnatwB} (@command{gcc})
4615 This switch suppresses warnings for static fixed-point expressions whose
4616 value is not an exact multiple of Small.
4617
4618 @item -gnatwc
4619 @emph{Activate warnings on conditionals.}
4620 @cindex @option{-gnatwc} (@command{gcc})
4621 @cindex Conditionals, constant
4622 This switch activates warnings for conditional expressions used in
4623 tests that are known to be True or False at compile time. The default
4624 is that such warnings are not generated.
4625 Note that this warning does
4626 not get issued for the use of boolean variables or constants whose
4627 values are known at compile time, since this is a standard technique
4628 for conditional compilation in Ada, and this would generate too many
4629 ``false positive'' warnings.
4630
4631 This warning option also activates a special test for comparisons using
4632 the operators >= and <=. If the compiler can tell that only the equality
4633 condition is posible, then it will warn that the > or < part of the test
4634 is useless and that the operator could be replaced by =. An example would
4635 be comparing a Natural variable <= 0.
4636
4637 This warning can also be turned on using @option{-gnatwa}.
4638
4639 @item -gnatwC
4640 @emph{Suppress warnings on conditionals.}
4641 @cindex @option{-gnatwC} (@command{gcc})
4642 This switch suppresses warnings for conditional expressions used in
4643 tests that are known to be True or False at compile time.
4644
4645 @item -gnatwd
4646 @emph{Activate warnings on implicit dereferencing.}
4647 @cindex @option{-gnatwd} (@command{gcc})
4648 If this switch is set, then the use of a prefix of an access type
4649 in an indexed component, slice, or selected component without an
4650 explicit @code{.all} will generate a warning. With this warning
4651 enabled, access checks occur only at points where an explicit
4652 @code{.all} appears in the source code (assuming no warnings are
4653 generated as a result of this switch). The default is that such
4654 warnings are not generated.
4655 Note that @option{-gnatwa} does not affect the setting of
4656 this warning option.
4657
4658 @item -gnatwD
4659 @emph{Suppress warnings on implicit dereferencing.}
4660 @cindex @option{-gnatwD} (@command{gcc})
4661 @cindex Implicit dereferencing
4662 @cindex Dereferencing, implicit
4663 This switch suppresses warnings for implicit dereferences in
4664 indexed components, slices, and selected components.
4665
4666 @item -gnatwe
4667 @emph{Treat warnings as errors.}
4668 @cindex @option{-gnatwe} (@command{gcc})
4669 @cindex Warnings, treat as error
4670 This switch causes warning messages to be treated as errors.
4671 The warning string still appears, but the warning messages are counted
4672 as errors, and prevent the generation of an object file.
4673
4674 @item -gnatwf
4675 @emph{Activate warnings on unreferenced formals.}
4676 @cindex @option{-gnatwf} (@command{gcc})
4677 @cindex Formals, unreferenced
4678 This switch causes a warning to be generated if a formal parameter
4679 is not referenced in the body of the subprogram. This warning can
4680 also be turned on using @option{-gnatwa} or @option{-gnatwu}.
4681
4682 @item -gnatwF
4683 @emph{Suppress warnings on unreferenced formals.}
4684 @cindex @option{-gnatwF} (@command{gcc})
4685 This switch suppresses warnings for unreferenced formal
4686 parameters. Note that the
4687 combination @option{-gnatwu} followed by @option{-gnatwF} has the
4688 effect of warning on unreferenced entities other than subprogram
4689 formals.
4690
4691 @item -gnatwg
4692 @emph{Activate warnings on unrecognized pragmas.}
4693 @cindex @option{-gnatwg} (@command{gcc})
4694 @cindex Pragmas, unrecognized
4695 This switch causes a warning to be generated if an unrecognized
4696 pragma is encountered. Apart from issuing this warning, the
4697 pragma is ignored and has no effect. This warning can
4698 also be turned on using @option{-gnatwa}. The default
4699 is that such warnings are issued (satisfying the Ada Reference
4700 Manual requirement that such warnings appear).
4701
4702 @item -gnatwG
4703 @emph{Suppress warnings on unrecognized pragmas.}
4704 @cindex @option{-gnatwG} (@command{gcc})
4705 This switch suppresses warnings for unrecognized pragmas.
4706
4707 @item -gnatwh
4708 @emph{Activate warnings on hiding.}
4709 @cindex @option{-gnatwh} (@command{gcc})
4710 @cindex Hiding of Declarations
4711 This switch activates warnings on hiding declarations.
4712 A declaration is considered hiding
4713 if it is for a non-overloadable entity, and it declares an entity with the
4714 same name as some other entity that is directly or use-visible. The default
4715 is that such warnings are not generated.
4716 Note that @option{-gnatwa} does not affect the setting of this warning option.
4717
4718 @item -gnatwH
4719 @emph{Suppress warnings on hiding.}
4720 @cindex @option{-gnatwH} (@command{gcc})
4721 This switch suppresses warnings on hiding declarations.
4722
4723 @item -gnatwi
4724 @emph{Activate warnings on implementation units.}
4725 @cindex @option{-gnatwi} (@command{gcc})
4726 This switch activates warnings for a @code{with} of an internal GNAT
4727 implementation unit, defined as any unit from the @code{Ada},
4728 @code{Interfaces}, @code{GNAT},
4729 ^^@code{DEC},^ or @code{System}
4730 hierarchies that is not
4731 documented in either the Ada Reference Manual or the GNAT
4732 Programmer's Reference Manual. Such units are intended only
4733 for internal implementation purposes and should not be @code{with}'ed
4734 by user programs. The default is that such warnings are generated
4735 This warning can also be turned on using @option{-gnatwa}.
4736
4737 @item -gnatwI
4738 @emph{Disable warnings on implementation units.}
4739 @cindex @option{-gnatwI} (@command{gcc})
4740 This switch disables warnings for a @code{with} of an internal GNAT
4741 implementation unit.
4742
4743 @item -gnatwj
4744 @emph{Activate warnings on obsolescent features (Annex J).}
4745 @cindex @option{-gnatwj} (@command{gcc})
4746 @cindex Features, obsolescent
4747 @cindex Obsolescent features
4748 If this warning option is activated, then warnings are generated for
4749 calls to subprograms marked with @code{pragma Obsolescent} and
4750 for use of features in Annex J of the Ada Reference Manual. In the
4751 case of Annex J, not all features are flagged. In particular use
4752 of the renamed packages (like @code{Text_IO}) and use of package
4753 @code{ASCII} are not flagged, since these are very common and
4754 would generate many annoying positive warnings. The default is that
4755 such warnings are not generated.
4756
4757 In addition to the above cases, warnings are also generated for
4758 GNAT features that have been provided in past versions but which
4759 have been superseded (typically by features in the new Ada standard).
4760 For example, @code{pragma Ravenscar} will be flagged since its
4761 function is replaced by @code{pragma Profile(Ravenscar)}.
4762
4763 Note that this warning option functions differently from the
4764 restriction @code{No_Obsolescent_Features} in two respects.
4765 First, the restriction applies only to annex J features.
4766 Second, the restriction does flag uses of package @code{ASCII}.
4767
4768 @item -gnatwJ
4769 @emph{Suppress warnings on obsolescent features (Annex J).}
4770 @cindex @option{-gnatwJ} (@command{gcc})
4771 This switch disables warnings on use of obsolescent features.
4772
4773 @item -gnatwk
4774 @emph{Activate warnings on variables that could be constants.}
4775 @cindex @option{-gnatwk} (@command{gcc})
4776 This switch activates warnings for variables that are initialized but
4777 never modified, and then could be declared constants.
4778
4779 @item -gnatwK
4780 @emph{Suppress warnings on variables that could be constants.}
4781 @cindex @option{-gnatwK} (@command{gcc})
4782 This switch disables warnings on variables that could be declared constants.
4783
4784 @item -gnatwl
4785 @emph{Activate warnings for missing elaboration pragmas.}
4786 @cindex @option{-gnatwl} (@command{gcc})
4787 @cindex Elaboration, warnings
4788 This switch activates warnings on missing
4789 @code{Elaborate_All} and @code{Elaborate} pragmas.
4790 See the section in this guide on elaboration checking for details on
4791 when such pragmas should be used. Warnings are also generated if you
4792 are using the static mode of elaboration, and a @code{pragma Elaborate}
4793 is encountered. The default is that such warnings
4794 are not generated.
4795 This warning is not automatically turned on by the use of @option{-gnatwa}.
4796
4797 @item -gnatwL
4798 @emph{Suppress warnings for missing elaboration pragmas.}
4799 @cindex @option{-gnatwL} (@command{gcc})
4800 This switch suppresses warnings on missing Elaborate and Elaborate_All pragmas.
4801 See the section in this guide on elaboration checking for details on
4802 when such pragmas should be used.
4803
4804 @item -gnatwm
4805 @emph{Activate warnings on modified but unreferenced variables.}
4806 @cindex @option{-gnatwm} (@command{gcc})
4807 This switch activates warnings for variables that are assigned (using
4808 an initialization value or with one or more assignment statements) but
4809 whose value is never read. The warning is suppressed for volatile
4810 variables and also for variables that are renamings of other variables
4811 or for which an address clause is given.
4812 This warning can also be turned on using @option{-gnatwa}.
4813
4814 @item -gnatwM
4815 @emph{Disable warnings on modified but unreferenced variables.}
4816 @cindex @option{-gnatwM} (@command{gcc})
4817 This switch disables warnings for variables that are assigned or
4818 initialized, but never read.
4819
4820 @item -gnatwn
4821 @emph{Set normal warnings mode.}
4822 @cindex @option{-gnatwn} (@command{gcc})
4823 This switch sets normal warning mode, in which enabled warnings are
4824 issued and treated as warnings rather than errors. This is the default
4825 mode. the switch @option{-gnatwn} can be used to cancel the effect of
4826 an explicit @option{-gnatws} or
4827 @option{-gnatwe}. It also cancels the effect of the
4828 implicit @option{-gnatwe} that is activated by the
4829 use of @option{-gnatg}.
4830
4831 @item -gnatwo
4832 @emph{Activate warnings on address clause overlays.}
4833 @cindex @option{-gnatwo} (@command{gcc})
4834 @cindex Address Clauses, warnings
4835 This switch activates warnings for possibly unintended initialization
4836 effects of defining address clauses that cause one variable to overlap
4837 another. The default is that such warnings are generated.
4838 This warning can also be turned on using @option{-gnatwa}.
4839
4840 @item -gnatwO
4841 @emph{Suppress warnings on address clause overlays.}
4842 @cindex @option{-gnatwO} (@command{gcc})
4843 This switch suppresses warnings on possibly unintended initialization
4844 effects of defining address clauses that cause one variable to overlap
4845 another.
4846
4847 @item -gnatwp
4848 @emph{Activate warnings on ineffective pragma Inlines.}
4849 @cindex @option{-gnatwp} (@command{gcc})
4850 @cindex Inlining, warnings
4851 This switch activates warnings for failure of front end inlining
4852 (activated by @option{-gnatN}) to inline a particular call. There are
4853 many reasons for not being able to inline a call, including most
4854 commonly that the call is too complex to inline.
4855 This warning can also be turned on using @option{-gnatwa}.
4856
4857 @item -gnatwP
4858 @emph{Suppress warnings on ineffective pragma Inlines.}
4859 @cindex @option{-gnatwP} (@command{gcc})
4860 This switch suppresses warnings on ineffective pragma Inlines. If the
4861 inlining mechanism cannot inline a call, it will simply ignore the
4862 request silently.
4863
4864 @item -gnatwr
4865 @emph{Activate warnings on redundant constructs.}
4866 @cindex @option{-gnatwr} (@command{gcc})
4867 This switch activates warnings for redundant constructs. The following
4868 is the current list of constructs regarded as redundant:
4869 This warning can also be turned on using @option{-gnatwa}.
4870
4871 @itemize @bullet
4872 @item
4873 Assignment of an item to itself.
4874 @item
4875 Type conversion that converts an expression to its own type.
4876 @item
4877 Use of the attribute @code{Base} where @code{typ'Base} is the same
4878 as @code{typ}.
4879 @item
4880 Use of pragma @code{Pack} when all components are placed by a record
4881 representation clause.
4882 @item
4883 Exception handler containing only a reraise statement (raise with no
4884 operand) which has no effect.
4885 @item
4886 Use of the operator abs on an operand that is known at compile time
4887 to be non-negative
4888 @item
4889 Comparison of boolean expressions to an explicit True value.
4890 @end itemize
4891
4892 @item -gnatwR
4893 @emph{Suppress warnings on redundant constructs.}
4894 @cindex @option{-gnatwR} (@command{gcc})
4895 This switch suppresses warnings for redundant constructs.
4896
4897 @item -gnatws
4898 @emph{Suppress all warnings.}
4899 @cindex @option{-gnatws} (@command{gcc})
4900 This switch completely suppresses the
4901 output of all warning messages from the GNAT front end.
4902 Note that it does not suppress warnings from the @command{gcc} back end.
4903 To suppress these back end warnings as well, use the switch @option{-w}
4904 in addition to @option{-gnatws}.
4905
4906 @item -gnatwu
4907 @emph{Activate warnings on unused entities.}
4908 @cindex @option{-gnatwu} (@command{gcc})
4909 This switch activates warnings to be generated for entities that
4910 are declared but not referenced, and for units that are @code{with}'ed
4911 and not
4912 referenced. In the case of packages, a warning is also generated if
4913 no entities in the package are referenced. This means that if the package
4914 is referenced but the only references are in @code{use}
4915 clauses or @code{renames}
4916 declarations, a warning is still generated. A warning is also generated
4917 for a generic package that is @code{with}'ed but never instantiated.
4918 In the case where a package or subprogram body is compiled, and there
4919 is a @code{with} on the corresponding spec
4920 that is only referenced in the body,
4921 a warning is also generated, noting that the
4922 @code{with} can be moved to the body. The default is that
4923 such warnings are not generated.
4924 This switch also activates warnings on unreferenced formals
4925 (it includes the effect of @option{-gnatwf}).
4926 This warning can also be turned on using @option{-gnatwa}.
4927
4928 @item -gnatwU
4929 @emph{Suppress warnings on unused entities.}
4930 @cindex @option{-gnatwU} (@command{gcc})
4931 This switch suppresses warnings for unused entities and packages.
4932 It also turns off warnings on unreferenced formals (and thus includes
4933 the effect of @option{-gnatwF}).
4934
4935 @item -gnatwv
4936 @emph{Activate warnings on unassigned variables.}
4937 @cindex @option{-gnatwv} (@command{gcc})
4938 @cindex Unassigned variable warnings
4939 This switch activates warnings for access to variables which
4940 may not be properly initialized. The default is that
4941 such warnings are generated.
4942
4943 @item -gnatwV
4944 @emph{Suppress warnings on unassigned variables.}
4945 @cindex @option{-gnatwV} (@command{gcc})
4946 This switch suppresses warnings for access to variables which
4947 may not be properly initialized.
4948
4949 @item -gnatwy
4950 @emph{Activate warnings for Ada 2005 compatibility issues.}
4951 @cindex @option{-gnatwy} (@command{gcc})
4952 @cindex Ada 2005 compatibility issues warnings
4953 For the most part Ada 2005 is upwards compatible with Ada 95,
4954 but there are some exceptions (for example the fact that
4955 @code{interface} is now a reserved word in Ada 2005). This
4956 switch activates several warnings to help in identifying
4957 and correcting such incompatibilities. The default is that
4958 these warnings are generated. Note that at one point Ada 2005
4959 was called Ada 0Y, hence the choice of character.
4960
4961 @item -gnatwY
4962 @emph{Disable warnings for Ada 2005 compatibility issues.}
4963 @cindex @option{-gnatwY} (@command{gcc})
4964 @cindex Ada 2005 compatibility issues warnings
4965 This switch suppresses several warnings intended to help in identifying
4966 incompatibilities between Ada 95 and Ada 2005.
4967
4968 @item -gnatwx
4969 @emph{Activate warnings on Export/Import pragmas.}
4970 @cindex @option{-gnatwx} (@command{gcc})
4971 @cindex Export/Import pragma warnings
4972 This switch activates warnings on Export/Import pragmas when
4973 the compiler detects a possible conflict between the Ada and
4974 foreign language calling sequences. For example, the use of
4975 default parameters in a convention C procedure is dubious
4976 because the C compiler cannot supply the proper default, so
4977 a warning is issued. The default is that such warnings are
4978 generated.
4979
4980 @item -gnatwX
4981 @emph{Suppress warnings on Export/Import pragmas.}
4982 @cindex @option{-gnatwX} (@command{gcc})
4983 This switch suppresses warnings on Export/Import pragmas.
4984 The sense of this is that you are telling the compiler that
4985 you know what you are doing in writing the pragma, and it
4986 should not complain at you.
4987
4988 @item -gnatwz
4989 @emph{Activate warnings on unchecked conversions.}
4990 @cindex @option{-gnatwz} (@command{gcc})
4991 @cindex Unchecked_Conversion warnings
4992 This switch activates warnings for unchecked conversions
4993 where the types are known at compile time to have different
4994 sizes. The default
4995 is that such warnings are generated.
4996
4997 @item -gnatwZ
4998 @emph{Suppress warnings on unchecked conversions.}
4999 @cindex @option{-gnatwZ} (@command{gcc})
5000 This switch suppresses warnings for unchecked conversions
5001 where the types are known at compile time to have different
5002 sizes.
5003
5004 @item ^-Wuninitialized^WARNINGS=UNINITIALIZED^
5005 @cindex @option{-Wuninitialized}
5006 The warnings controlled by the @option{-gnatw} switch are generated by the
5007 front end of the compiler. In some cases, the @option{^gcc^GCC^} back end
5008 can provide additional warnings. One such useful warning is provided by
5009 @option{^-Wuninitialized^WARNINGS=UNINITIALIZED^}. This must be used in
5010 conjunction with tunrning on optimization mode. This causes the flow
5011 analysis circuits of the back end optimizer to output additional
5012 warnings about uninitialized variables.
5013
5014 @item ^-w^/NO_BACK_END_WARNINGS^
5015 @cindex @option{-w}
5016 This switch suppresses warnings from the @option{^gcc^GCC^} back end. The
5017 code generator detects a number of warning situations that are missed
5018 by the @option{GNAT} front end, and this switch can be used to suppress them.
5019 The use of this switch also sets the default front end warning mode to
5020 @option{-gnatws}, that is, front end warnings suppressed as well.
5021
5022 @end table
5023
5024 @noindent
5025 @ifclear vms
5026 A string of warning parameters can be used in the same parameter. For example:
5027
5028 @smallexample
5029 -gnatwaLe
5030 @end smallexample
5031
5032 @noindent
5033 will turn on all optional warnings except for elaboration pragma warnings,
5034 and also specify that warnings should be treated as errors.
5035 @end ifclear
5036 When no switch @option{^-gnatw^/WARNINGS^} is used, this is equivalent to:
5037
5038 @table @option
5039 @c !sort!
5040 @item -gnatwC
5041 @item -gnatwD
5042 @item -gnatwF
5043 @item -gnatwg
5044 @item -gnatwH
5045 @item -gnatwi
5046 @item -gnatwJ
5047 @item -gnatwK
5048 @item -gnatwL
5049 @item -gnatwM
5050 @item -gnatwn
5051 @item -gnatwo
5052 @item -gnatwP
5053 @item -gnatwR
5054 @item -gnatwU
5055 @item -gnatwv
5056 @item -gnatwz
5057 @item -gnatwx
5058
5059 @end table
5060
5061 @node Debugging and Assertion Control
5062 @subsection Debugging and Assertion Control
5063
5064 @table @option
5065 @item -gnata
5066 @cindex @option{-gnata} (@command{gcc})
5067 @findex Assert
5068 @findex Debug
5069 @cindex Assertions
5070
5071 @noindent
5072 The pragmas @code{Assert} and @code{Debug} normally have no effect and
5073 are ignored. This switch, where @samp{a} stands for assert, causes
5074 @code{Assert} and @code{Debug} pragmas to be activated.
5075
5076 The pragmas have the form:
5077
5078 @smallexample
5079 @cartouche
5080    @b{pragma} Assert (@var{Boolean-expression} [,
5081                       @var{static-string-expression}])
5082    @b{pragma} Debug (@var{procedure call})
5083 @end cartouche
5084 @end smallexample
5085
5086 @noindent
5087 The @code{Assert} pragma causes @var{Boolean-expression} to be tested.
5088 If the result is @code{True}, the pragma has no effect (other than
5089 possible side effects from evaluating the expression). If the result is
5090 @code{False}, the exception @code{Assert_Failure} declared in the package
5091 @code{System.Assertions} is
5092 raised (passing @var{static-string-expression}, if present, as the
5093 message associated with the exception). If no string expression is
5094 given the default is a string giving the file name and line number
5095 of the pragma.
5096
5097 The @code{Debug} pragma causes @var{procedure} to be called. Note that
5098 @code{pragma Debug} may appear within a declaration sequence, allowing
5099 debugging procedures to be called between declarations.
5100
5101 @ifset vms
5102 @item /DEBUG[=debug-level]
5103 @itemx  /NODEBUG
5104 Specifies how much debugging information is to be included in
5105 the resulting object file where 'debug-level' is one of the following:
5106 @table @code
5107 @item   TRACEBACK
5108 Include both debugger symbol records and traceback
5109 the object file.
5110 This is the default setting.
5111 @item   ALL
5112 Include both debugger symbol records and traceback in
5113 object file.
5114 @item   NONE
5115 Excludes both debugger symbol records and traceback
5116 the object file. Same as /NODEBUG.
5117 @item   SYMBOLS
5118 Includes only debugger symbol records in the object
5119 file. Note that this doesn't include traceback information.
5120 @end table
5121 @end ifset
5122 @end table
5123
5124 @node Validity Checking
5125 @subsection Validity Checking
5126 @findex Validity Checking
5127
5128 @noindent
5129 The Ada 95 Reference Manual has specific requirements for checking
5130 for invalid values. In particular, RM 13.9.1 requires that the
5131 evaluation of invalid values (for example from unchecked conversions),
5132 not result in erroneous execution. In GNAT, the result of such an
5133 evaluation in normal default mode is to either use the value
5134 unmodified, or to raise Constraint_Error in those cases where use
5135 of the unmodified value would cause erroneous execution. The cases
5136 where unmodified values might lead to erroneous execution are case
5137 statements (where a wild jump might result from an invalid value),
5138 and subscripts on the left hand side (where memory corruption could
5139 occur as a result of an invalid value).
5140
5141 The @option{-gnatV^@var{x}^^} switch allows more control over the validity
5142 checking mode.
5143 @ifclear vms
5144 The @code{x} argument is a string of letters that
5145 indicate validity checks that are performed or not performed in addition
5146 to the default checks described above.
5147 @end ifclear
5148 @ifset vms
5149 The options allowed for this qualifier
5150 indicate validity checks that are performed or not performed in addition
5151 to the default checks described above.
5152 @end ifset
5153
5154 @table @option
5155 @c !sort!
5156 @item -gnatVa
5157 @emph{All validity checks.}
5158 @cindex @option{-gnatVa} (@command{gcc})
5159 All validity checks are turned on.
5160 @ifclear vms
5161 That is, @option{-gnatVa} is
5162 equivalent to @option{gnatVcdfimorst}.
5163 @end ifclear
5164
5165 @item -gnatVc
5166 @emph{Validity checks for copies.}
5167 @cindex @option{-gnatVc} (@command{gcc})
5168 The right hand side of assignments, and the initializing values of
5169 object declarations are validity checked.
5170
5171 @item -gnatVd
5172 @emph{Default (RM) validity checks.}
5173 @cindex @option{-gnatVd} (@command{gcc})
5174 Some validity checks are done by default following normal Ada semantics
5175 (RM 13.9.1 (9-11)).
5176 A check is done in case statements that the expression is within the range
5177 of the subtype. If it is not, Constraint_Error is raised.
5178 For assignments to array components, a check is done that the expression used
5179 as index is within the range. If it is not, Constraint_Error is raised.
5180 Both these validity checks may be turned off using switch @option{-gnatVD}.
5181 They are turned on by default. If @option{-gnatVD} is specified, a subsequent
5182 switch @option{-gnatVd} will leave the checks turned on.
5183 Switch @option{-gnatVD} should be used only if you are sure that all such
5184 expressions have valid values. If you use this switch and invalid values
5185 are present, then the program is erroneous, and wild jumps or memory
5186 overwriting may occur.
5187
5188 @item -gnatVf
5189 @emph{Validity checks for floating-point values.}
5190 @cindex @option{-gnatVf} (@command{gcc})
5191 In the absence of this switch, validity checking occurs only for discrete
5192 values. If @option{-gnatVf} is specified, then validity checking also applies
5193 for floating-point values, and NaN's and infinities are considered invalid,
5194 as well as out of range values for constrained types. Note that this means
5195 that standard @code{IEEE} infinity mode is not allowed. The exact contexts
5196 in which floating-point values are checked depends on the setting of other
5197 options. For example,
5198 @option{^-gnatVif^VALIDITY_CHECKING=(IN_PARAMS,FLOATS)^} or
5199 @option{^-gnatVfi^VALIDITY_CHECKING=(FLOATS,IN_PARAMS)^}
5200 (the order does not matter) specifies that floating-point parameters of mode
5201 @code{in} should be validity checked.
5202
5203 @item -gnatVi
5204 @emph{Validity checks for @code{in} mode parameters}
5205 @cindex @option{-gnatVi} (@command{gcc})
5206 Arguments for parameters of mode @code{in} are validity checked in function
5207 and procedure calls at the point of call.
5208
5209 @item -gnatVm
5210 @emph{Validity checks for @code{in out} mode parameters.}
5211 @cindex @option{-gnatVm} (@command{gcc})
5212 Arguments for parameters of mode @code{in out} are validity checked in
5213 procedure calls at the point of call. The @code{'m'} here stands for
5214 modify, since this concerns parameters that can be modified by the call.
5215 Note that there is no specific option to test @code{out} parameters,
5216 but any reference within the subprogram will be tested in the usual
5217 manner, and if an invalid value is copied back, any reference to it
5218 will be subject to validity checking.
5219
5220 @item -gnatVn
5221 @emph{No validity checks.}
5222 @cindex @option{-gnatVn} (@command{gcc})
5223 This switch turns off all validity checking, including the default checking
5224 for case statements and left hand side subscripts. Note that the use of
5225 the switch @option{-gnatp} suppresses all run-time checks, including
5226 validity checks, and thus implies @option{-gnatVn}. When this switch
5227 is used, it cancels any other @option{-gnatV} previously issued.
5228
5229 @item -gnatVo
5230 @emph{Validity checks for operator and attribute operands.}
5231 @cindex @option{-gnatVo} (@command{gcc})
5232 Arguments for predefined operators and attributes are validity checked.
5233 This includes all operators in package @code{Standard},
5234 the shift operators defined as intrinsic in package @code{Interfaces}
5235 and operands for attributes such as @code{Pos}. Checks are also made
5236 on individual component values for composite comparisons.
5237
5238 @item -gnatVp
5239 @emph{Validity checks for parameters.}
5240 @cindex @option{-gnatVp} (@command{gcc})
5241 This controls the treatment of parameters within a subprogram (as opposed
5242 to @option{-gnatVi} and @option{-gnatVm} which control validity testing
5243 of parameters on a call. If either of these call options is used, then
5244 normally an assumption is made within a subprogram that the input arguments
5245 have been validity checking at the point of call, and do not need checking
5246 again within a subprogram). If @option{-gnatVp} is set, then this assumption
5247 is not made, and parameters are not assumed to be valid, so their validity
5248 will be checked (or rechecked) within the subprogram.
5249
5250 @item -gnatVr
5251 @emph{Validity checks for function returns.}
5252 @cindex @option{-gnatVr} (@command{gcc})
5253 The expression in @code{return} statements in functions is validity
5254 checked.
5255
5256 @item -gnatVs
5257 @emph{Validity checks for subscripts.}
5258 @cindex @option{-gnatVs} (@command{gcc})
5259 All subscripts expressions are checked for validity, whether they appear
5260 on the right side or left side (in default mode only left side subscripts
5261 are validity checked).
5262
5263 @item -gnatVt
5264 @emph{Validity checks for tests.}
5265 @cindex @option{-gnatVt} (@command{gcc})
5266 Expressions used as conditions in @code{if}, @code{while} or @code{exit}
5267 statements are checked, as well as guard expressions in entry calls.
5268
5269 @end table
5270
5271 @noindent
5272 The @option{-gnatV} switch may be followed by
5273 ^a string of letters^a list of options^
5274 to turn on a series of validity checking options.
5275 For example,
5276 @option{^-gnatVcr^/VALIDITY_CHECKING=(COPIES, RETURNS)^}
5277 specifies that in addition to the default validity checking, copies and
5278 function return expressions are to be validity checked.
5279 In order to make it easier
5280 to specify the desired combination of effects,
5281 @ifclear vms
5282 the upper case letters @code{CDFIMORST} may
5283 be used to turn off the corresponding lower case option.
5284 @end ifclear
5285 @ifset vms
5286 the prefix @code{NO} on an option turns off the corresponding validity
5287 checking:
5288 @itemize @bullet
5289 @item @code{NOCOPIES}
5290 @item @code{NODEFAULT}
5291 @item @code{NOFLOATS}
5292 @item @code{NOIN_PARAMS}
5293 @item @code{NOMOD_PARAMS}
5294 @item @code{NOOPERANDS}
5295 @item @code{NORETURNS}
5296 @item @code{NOSUBSCRIPTS}
5297 @item @code{NOTESTS}
5298 @end itemize
5299 @end ifset
5300 Thus
5301 @option{^-gnatVaM^/VALIDITY_CHECKING=(ALL, NOMOD_PARAMS)^}
5302 turns on all validity checking options except for
5303 checking of @code{@b{in out}} procedure arguments.
5304
5305 The specification of additional validity checking generates extra code (and
5306 in the case of @option{-gnatVa} the code expansion can be substantial.
5307 However, these additional checks can be very useful in detecting
5308 uninitialized variables, incorrect use of unchecked conversion, and other
5309 errors leading to invalid values. The use of pragma @code{Initialize_Scalars}
5310 is useful in conjunction with the extra validity checking, since this
5311 ensures that wherever possible uninitialized variables have invalid values.
5312
5313 See also the pragma @code{Validity_Checks} which allows modification of
5314 the validity checking mode at the program source level, and also allows for
5315 temporary disabling of validity checks.
5316
5317 @node Style Checking
5318 @subsection Style Checking
5319 @findex Style checking
5320
5321 @noindent
5322 The @option{-gnaty^x^(option,option,...)^} switch
5323 @cindex @option{-gnaty} (@command{gcc})
5324 causes the compiler to
5325 enforce specified style rules. A limited set of style rules has been used
5326 in writing the GNAT sources themselves. This switch allows user programs
5327 to activate all or some of these checks. If the source program fails a
5328 specified style check, an appropriate warning message is given, preceded by
5329 the character sequence ``(style)''.
5330 @ifset vms
5331 @code{(option,option,...)} is a sequence of keywords
5332 @end ifset
5333 @ifclear vms
5334 The string @var{x} is a sequence of letters or digits
5335 @end ifclear
5336 indicating the particular style
5337 checks to be performed. The following checks are defined:
5338
5339 @table @option
5340 @c !sort!
5341 @item 1-9
5342 @emph{Specify indentation level.}
5343 If a digit from 1-9 appears
5344 ^in the string after @option{-gnaty}^as an option for /STYLE_CHECKS^
5345 then proper indentation is checked, with the digit indicating the
5346 indentation level required.
5347 The general style of required indentation is as specified by
5348 the examples in the Ada Reference Manual. Full line comments must be
5349 aligned with the @code{--} starting on a column that is a multiple of
5350 the alignment level.
5351
5352 @item ^a^ATTRIBUTE^
5353 @emph{Check attribute casing.}
5354 If the ^letter a^word ATTRIBUTE^ appears in the string after @option{-gnaty}
5355 then attribute names, including the case of keywords such as @code{digits}
5356 used as attributes names, must be written in mixed case, that is, the
5357 initial letter and any letter following an underscore must be uppercase.
5358 All other letters must be lowercase.
5359
5360 @item ^b^BLANKS^
5361 @emph{Blanks not allowed at statement end.}
5362 If the ^letter b^word BLANKS^ appears in the string after @option{-gnaty} then
5363 trailing blanks are not allowed at the end of statements. The purpose of this
5364 rule, together with h (no horizontal tabs), is to enforce a canonical format
5365 for the use of blanks to separate source tokens.
5366
5367 @item ^c^COMMENTS^
5368 @emph{Check comments.}
5369 If the ^letter c^word COMMENTS^ appears in the string after @option{-gnaty}
5370 then comments must meet the following set of rules:
5371
5372 @itemize @bullet
5373
5374 @item
5375 The ``@code{--}'' that starts the column must either start in column one,
5376 or else at least one blank must precede this sequence.
5377
5378 @item
5379 Comments that follow other tokens on a line must have at least one blank
5380 following the ``@code{--}'' at the start of the comment.
5381
5382 @item
5383 Full line comments must have two blanks following the ``@code{--}'' that
5384 starts the comment, with the following exceptions.
5385
5386 @item
5387 A line consisting only of the ``@code{--}'' characters, possibly preceded
5388 by blanks is permitted.
5389
5390 @item
5391 A comment starting with ``@code{--x}'' where @code{x} is a special character
5392 is permitted.
5393 This allows proper processing of the output generated by specialized tools
5394 including @command{gnatprep} (where ``@code{--!}'' is used) and the SPARK
5395 annotation
5396 language (where ``@code{--#}'' is used). For the purposes of this rule, a
5397 special character is defined as being in one of the ASCII ranges
5398 @code{16#21#..16#2F#} or @code{16#3A#..16#3F#}.
5399 Note that this usage is not permitted
5400 in GNAT implementation units (i.e. when @option{-gnatg} is used).
5401
5402 @item
5403 A line consisting entirely of minus signs, possibly preceded by blanks, is
5404 permitted. This allows the construction of box comments where lines of minus
5405 signs are used to form the top and bottom of the box.
5406
5407 @item
5408 A comment that starts and ends with ``@code{--}'' is permitted as long as at
5409 least one blank follows the initial ``@code{--}''. Together with the preceding
5410 rule, this allows the construction of box comments, as shown in the following
5411 example:
5412 @smallexample
5413 ---------------------------
5414 -- This is a box comment --
5415 -- with two text lines.  --
5416 ---------------------------
5417 @end smallexample
5418 @end itemize
5419
5420 @item ^d^DOS_LINE_ENDINGS^
5421 @emph{Check no DOS line terminators present.}
5422 If the ^letter d^word DOS_LINE_ENDINGS^ appears in the string after
5423 @option{-gnaty} then all lines must be terminated by a single ASCII.LF
5424 character (in particular the DOS line terminator sequence CR/LF is not
5425 allowed).
5426
5427 @item ^e^END^
5428 @emph{Check end/exit labels.}
5429 If the ^letter e^word END^ appears in the string after @option{-gnaty} then
5430 optional labels on @code{end} statements ending subprograms and on
5431 @code{exit} statements exiting named loops, are required to be present.
5432
5433 @item ^f^VTABS^
5434 @emph{No form feeds or vertical tabs.}
5435 If the ^letter f^word VTABS^ appears in the string after @option{-gnaty} then
5436 neither form feeds nor vertical tab characters are permitted
5437 in the source text.
5438
5439 @item ^h^HTABS^
5440 @emph{No horizontal tabs.}
5441 If the ^letter h^word HTABS^ appears in the string after @option{-gnaty} then
5442 horizontal tab characters are not permitted in the source text.
5443 Together with the b (no blanks at end of line) check, this
5444 enforces a canonical form for the use of blanks to separate
5445 source tokens.
5446
5447 @item ^i^IF_THEN^
5448 @emph{Check if-then layout.}
5449 If the ^letter i^word IF_THEN^ appears in the string after @option{-gnaty},
5450 then the keyword @code{then} must appear either on the same
5451 line as corresponding @code{if}, or on a line on its own, lined
5452 up under the @code{if} with at least one non-blank line in between
5453 containing all or part of the condition to be tested.
5454
5455 @item ^k^KEYWORD^
5456 @emph{Check keyword casing.}
5457 If the ^letter k^word KEYWORD^ appears in the string after @option{-gnaty} then
5458 all keywords must be in lower case (with the exception of keywords
5459 such as @code{digits} used as attribute names to which this check
5460 does not apply).
5461
5462 @item ^l^LAYOUT^
5463 @emph{Check layout.}
5464 If the ^letter l^word LAYOUT^ appears in the string after @option{-gnaty} then
5465 layout of statement and declaration constructs must follow the
5466 recommendations in the Ada Reference Manual, as indicated by the
5467 form of the syntax rules. For example an @code{else} keyword must
5468 be lined up with the corresponding @code{if} keyword.
5469
5470 There are two respects in which the style rule enforced by this check
5471 option are more liberal than those in the Ada Reference Manual. First
5472 in the case of record declarations, it is permissible to put the
5473 @code{record} keyword on the same line as the @code{type} keyword, and
5474 then the @code{end} in @code{end record} must line up under @code{type}.
5475 For example, either of the following two layouts is acceptable:
5476
5477 @smallexample @c ada
5478 @cartouche
5479 type q is record
5480    a : integer;
5481    b : integer;
5482 end record;
5483
5484 type q is
5485    record
5486       a : integer;
5487       b : integer;
5488    end record;
5489 @end cartouche
5490 @end smallexample
5491
5492 @noindent
5493 Second, in the case of a block statement, a permitted alternative
5494 is to put the block label on the same line as the @code{declare} or
5495 @code{begin} keyword, and then line the @code{end} keyword up under
5496 the block label. For example both the following are permitted:
5497
5498 @smallexample @c ada
5499 @cartouche
5500 Block : declare
5501    A : Integer := 3;
5502 begin
5503    Proc (A, A);
5504 end Block;
5505
5506 Block :
5507    declare
5508       A : Integer := 3;
5509    begin
5510       Proc (A, A);
5511    end Block;
5512 @end cartouche
5513 @end smallexample
5514
5515 @noindent
5516 The same alternative format is allowed for loops. For example, both of
5517 the following are permitted:
5518
5519 @smallexample @c ada
5520 @cartouche
5521 Clear : while J < 10 loop
5522    A (J) := 0;
5523 end loop Clear;
5524
5525 Clear :
5526    while J < 10 loop
5527       A (J) := 0;
5528    end loop Clear;
5529 @end cartouche
5530 @end smallexample
5531
5532 @item ^Lnnn^MAX_NESTING=nnn^
5533 @emph{Set maximum nesting level}
5534 If the sequence ^Lnnn^MAX_NESTING=nnn^, where nnn is a decimal number in
5535 the range 0-999, appears in the string after @option{-gnaty} then the
5536 maximum level of nesting of constructs (including subprograms, loops,
5537 blocks, packages, and conditionals) may not exceed the given value. A
5538 value of zero disconnects this style check.
5539
5540 @item ^m^LINE_LENGTH^
5541 @emph{Check maximum line length.}
5542 If the ^letter m^word LINE_LENGTH^ appears in the string after @option{-gnaty}
5543 then the length of source lines must not exceed 79 characters, including
5544 any trailing blanks. The value of 79 allows convenient display on an
5545 80 character wide device or window, allowing for possible special
5546 treatment of 80 character lines. Note that this count is of
5547 characters in the source text. This means that a tab character counts
5548 as one character in this count but a wide character sequence counts as
5549 a single character (however many bytes are needed in the encoding).
5550
5551 @item ^Mnnn^MAX_LENGTH=nnn^
5552 @emph{Set maximum line length.}
5553 If the sequence ^M^MAX_LENGTH=^nnn, where nnn is a decimal number, appears in
5554 the string after @option{-gnaty} then the length of lines must not exceed the
5555 given value.
5556
5557 @item ^n^STANDARD_CASING^
5558 @emph{Check casing of entities in Standard.}
5559 If the ^letter n^word STANDARD_CASING^ appears in the string
5560 after @option{-gnaty} then any identifier from Standard must be cased
5561 to match the presentation in the Ada Reference Manual (for example,
5562 @code{Integer} and @code{ASCII.NUL}).
5563
5564 @item ^o^ORDERED_SUBPROGRAMS^
5565 @emph{Check order of subprogram bodies.}
5566 If the ^letter o^word ORDERED_SUBPROGRAMS^ appears in the string
5567 after @option{-gnaty} then all subprogram bodies in a given scope
5568 (e.g. a package body) must be in alphabetical order. The ordering
5569 rule uses normal Ada rules for comparing strings, ignoring casing
5570 of letters, except that if there is a trailing numeric suffix, then
5571 the value of this suffix is used in the ordering (e.g. Junk2 comes
5572 before Junk10).
5573
5574 @item ^p^PRAGMA^
5575 @emph{Check pragma casing.}
5576 If the ^letter p^word PRAGMA^ appears in the string after @option{-gnaty} then
5577 pragma names must be written in mixed case, that is, the
5578 initial letter and any letter following an underscore must be uppercase.
5579 All other letters must be lowercase.
5580
5581 @item ^r^REFERENCES^
5582 @emph{Check references.}
5583 If the ^letter r^word REFERENCES^ appears in the string after @option{-gnaty}
5584 then all identifier references must be cased in the same way as the
5585 corresponding declaration. No specific casing style is imposed on
5586 identifiers. The only requirement is for consistency of references
5587 with declarations.
5588
5589 @item ^s^SPECS^
5590 @emph{Check separate specs.}
5591 If the ^letter s^word SPECS^ appears in the string after @option{-gnaty} then
5592 separate declarations (``specs'') are required for subprograms (a
5593 body is not allowed to serve as its own declaration). The only
5594 exception is that parameterless library level procedures are
5595 not required to have a separate declaration. This exception covers
5596 the most frequent form of main program procedures.
5597
5598 @item ^t^TOKEN^
5599 @emph{Check token spacing.}
5600 If the ^letter t^word TOKEN^ appears in the string after @option{-gnaty} then
5601 the following token spacing rules are enforced:
5602
5603 @itemize @bullet
5604
5605 @item
5606 The keywords @code{@b{abs}} and @code{@b{not}} must be followed by a space.
5607
5608 @item
5609 The token @code{=>} must be surrounded by spaces.
5610
5611 @item
5612 The token @code{<>} must be preceded by a space or a left parenthesis.
5613
5614 @item
5615 Binary operators other than @code{**} must be surrounded by spaces.
5616 There is no restriction on the layout of the @code{**} binary operator.
5617
5618 @item
5619 Colon must be surrounded by spaces.
5620
5621 @item
5622 Colon-equal (assignment, initialization) must be surrounded by spaces.
5623
5624 @item
5625 Comma must be the first non-blank character on the line, or be
5626 immediately preceded by a non-blank character, and must be followed
5627 by a space.
5628
5629 @item
5630 If the token preceding a left parenthesis ends with a letter or digit, then
5631 a space must separate the two tokens.
5632
5633 @item
5634 A right parenthesis must either be the first non-blank character on
5635 a line, or it must be preceded by a non-blank character.
5636
5637 @item
5638 A semicolon must not be preceded by a space, and must not be followed by
5639 a non-blank character.
5640
5641 @item
5642 A unary plus or minus may not be followed by a space.
5643
5644 @item
5645 A vertical bar must be surrounded by spaces.
5646 @end itemize
5647
5648 @item ^u^UNNECESSARY_BLANK_LINES^
5649 @emph{Check unnecessary blank lines.}
5650 Check for unnecessary blank lines. A blank line is considered
5651 unnecessary if it appears at the end of the file, or if more than
5652 one blank line occurs in sequence.
5653
5654 @item ^x^XTRA_PARENS^
5655 @emph{Check extra parentheses.}
5656 Check for the use of an unnecessary extra level of parentheses (C-style)
5657 around conditions in @code{if} statements, @code{while} statements and
5658 @code{exit} statements.
5659
5660 @end table
5661
5662 @noindent
5663 In the above rules, appearing in column one is always permitted, that is,
5664 counts as meeting either a requirement for a required preceding space,
5665 or as meeting a requirement for no preceding space.
5666
5667 Appearing at the end of a line is also always permitted, that is, counts
5668 as meeting either a requirement for a following space, or as meeting
5669 a requirement for no following space.
5670
5671 @noindent
5672 If any of these style rules is violated, a message is generated giving
5673 details on the violation. The initial characters of such messages are
5674 always ``@code{(style)}''. Note that these messages are treated as warning
5675 messages, so they normally do not prevent the generation of an object
5676 file. The @option{-gnatwe} switch can be used to treat warning messages,
5677 including style messages, as fatal errors.
5678
5679 The switch
5680 @ifclear vms
5681 @option{-gnaty} on its own (that is not
5682 followed by any letters or digits),
5683 is equivalent to @code{gnaty3abcefhiklmnprst}, that is all checking
5684 options enabled with the exception of @option{-gnatyo},
5685 @option{-gnatyd}, @option{-gnatyu}, and @option{-gnatyx}.
5686 @end ifclear
5687 @ifset vms
5688 /STYLE_CHECKS=ALL_BUILTIN enables all checking options with
5689 the exception of ORDERED_SUBPROGRAMS, UNNECESSARY_BLANK_LINES,
5690 XTRA_PARENS, and DOS_LINE_ENDINGS. In addition
5691 @end ifset
5692 an indentation level of 3 is set. This is similar to the standard
5693 checking option that is used for the GNAT sources.
5694
5695 The switch
5696 @ifclear vms
5697 @option{-gnatyN}
5698 @end ifclear
5699 @ifset vms
5700 /STYLE_CHECKS=NONE
5701 @end ifset
5702 clears any previously set style checks.
5703
5704 @node Run-Time Checks
5705 @subsection Run-Time Checks
5706 @cindex Division by zero
5707 @cindex Access before elaboration
5708 @cindex Checks, division by zero
5709 @cindex Checks, access before elaboration
5710 @cindex Checks, stack overflow checking
5711
5712 @noindent
5713 If you compile with the default options, GNAT will insert many run-time
5714 checks into the compiled code, including code that performs range
5715 checking against constraints, but not arithmetic overflow checking for
5716 integer operations (including division by zero), checks for access
5717 before elaboration on subprogram calls, or stack overflow checking. All
5718 other run-time checks, as required by the Ada 95 Reference Manual, are
5719 generated by default. The following @command{gcc} switches refine this
5720 default behavior:
5721
5722 @table @option
5723 @c !sort!
5724 @item -gnatp
5725 @cindex @option{-gnatp} (@command{gcc})
5726 @cindex Suppressing checks
5727 @cindex Checks, suppressing
5728 @findex Suppress
5729 Suppress all run-time checks as though @code{pragma Suppress (all_checks})
5730 had been present in the source. Validity checks are also suppressed (in
5731 other words @option{-gnatp} also implies @option{-gnatVn}.
5732 Use this switch to improve the performance
5733 of the code at the expense of safety in the presence of invalid data or
5734 program bugs.
5735
5736 @item -gnato
5737 @cindex @option{-gnato} (@command{gcc})
5738 @cindex Overflow checks
5739 @cindex Check, overflow
5740 Enables overflow checking for integer operations.
5741 This causes GNAT to generate slower and larger executable
5742 programs by adding code to check for overflow (resulting in raising
5743 @code{Constraint_Error} as required by standard Ada
5744 semantics). These overflow checks correspond to situations in which
5745 the true value of the result of an operation may be outside the base
5746 range of the result type. The following example shows the distinction:
5747
5748 @smallexample @c ada
5749 X1 : Integer := Integer'Last;
5750 X2 : Integer range 1 .. 5 := 5;
5751 X3 : Integer := Integer'Last;
5752 X4 : Integer range 1 .. 5 := 5;
5753 F  : Float := 2.0E+20;
5754 ...
5755 X1 := X1 + 1;
5756 X2 := X2 + 1;
5757 X3 := Integer (F);
5758 X4 := Integer (F);
5759 @end smallexample
5760
5761 @noindent
5762 Here the first addition results in a value that is outside the base range
5763 of Integer, and hence requires an overflow check for detection of the
5764 constraint error. Thus the first assignment to @code{X1} raises a
5765 @code{Constraint_Error} exception only if @option{-gnato} is set.
5766
5767 The second increment operation results in a violation
5768 of the explicit range constraint, and such range checks are always
5769 performed (unless specifically suppressed with a pragma @code{suppress}
5770 or the use of @option{-gnatp}).
5771
5772 The two conversions of @code{F} both result in values that are outside
5773 the base range of type @code{Integer} and thus will raise
5774 @code{Constraint_Error} exceptions only if @option{-gnato} is used.
5775 The fact that the result of the second conversion is assigned to
5776 variable @code{X4} with a restricted range is irrelevant, since the problem
5777 is in the conversion, not the assignment.
5778
5779 Basically the rule is that in the default mode (@option{-gnato} not
5780 used), the generated code assures that all integer variables stay
5781 within their declared ranges, or within the base range if there is
5782 no declared range. This prevents any serious problems like indexes
5783 out of range for array operations.
5784
5785 What is not checked in default mode is an overflow that results in
5786 an in-range, but incorrect value. In the above example, the assignments
5787 to @code{X1}, @code{X2}, @code{X3} all give results that are within the
5788 range of the target variable, but the result is wrong in the sense that
5789 it is too large to be represented correctly. Typically the assignment
5790 to @code{X1} will result in wrap around to the largest negative number.
5791 The conversions of @code{F} will result in some @code{Integer} value
5792 and if that integer value is out of the @code{X4} range then the
5793 subsequent assignment would generate an exception.
5794
5795 @findex Machine_Overflows
5796 Note that the @option{-gnato} switch does not affect the code generated
5797 for any floating-point operations; it applies only to integer
5798 semantics).
5799 For floating-point, GNAT has the @code{Machine_Overflows}
5800 attribute set to @code{False} and the normal mode of operation is to
5801 generate IEEE NaN and infinite values on overflow or invalid operations
5802 (such as dividing 0.0 by 0.0).
5803
5804 The reason that we distinguish overflow checking from other kinds of
5805 range constraint checking is that a failure of an overflow check can
5806 generate an incorrect value, but cannot cause erroneous behavior. This
5807 is unlike the situation with a constraint check on an array subscript,
5808 where failure to perform the check can result in random memory description,
5809 or the range check on a case statement, where failure to perform the check
5810 can cause a wild jump.
5811
5812 Note again that @option{-gnato} is off by default, so overflow checking is
5813 not performed in default mode. This means that out of the box, with the
5814 default settings, GNAT does not do all the checks expected from the
5815 language description in the Ada Reference Manual. If you want all constraint
5816 checks to be performed, as described in this Manual, then you must
5817 explicitly use the -gnato switch either on the @command{gnatmake} or
5818 @command{gcc} command.
5819
5820 @item -gnatE
5821 @cindex @option{-gnatE} (@command{gcc})
5822 @cindex Elaboration checks
5823 @cindex Check, elaboration
5824 Enables dynamic checks for access-before-elaboration
5825 on subprogram calls and generic instantiations.
5826 For full details of the effect and use of this switch,
5827 @xref{Compiling Using gcc}.
5828
5829 @item -fstack-check
5830 @cindex @option{-fstack-check} (@command{gcc})
5831 @cindex Stack Overflow Checking
5832 @cindex Checks, stack overflow checking
5833 Activates stack overflow checking. For full details of the effect and use of
5834 this switch see @ref{Stack Overflow Checking}.
5835 @end table
5836
5837 @findex Unsuppress
5838 @noindent
5839 The setting of these switches only controls the default setting of the
5840 checks. You may modify them using either @code{Suppress} (to remove
5841 checks) or @code{Unsuppress} (to add back suppressed checks) pragmas in
5842 the program source.
5843
5844 @node Stack Overflow Checking
5845 @subsection Stack Overflow Checking
5846 @cindex Stack Overflow Checking
5847 @cindex -fstack-check
5848
5849 @noindent
5850 For most operating systems, @command{gcc} does not perform stack overflow
5851 checking by default. This means that if the main environment task or
5852 some other task exceeds the available stack space, then unpredictable
5853 behavior will occur.
5854
5855 To activate stack checking, compile all units with the gcc option
5856 @option{-fstack-check}. For example:
5857
5858 @smallexample
5859 gcc -c -fstack-check package1.adb
5860 @end smallexample
5861
5862 @noindent
5863 Units compiled with this option will generate extra instructions to check
5864 that any use of the stack (for procedure calls or for declaring local
5865 variables in declare blocks) do not exceed the available stack space.
5866 If the space is exceeded, then a @code{Storage_Error} exception is raised.
5867
5868 For declared tasks, the stack size is always controlled by the size
5869 given in an applicable @code{Storage_Size} pragma (or is set to
5870 the default size if no pragma is used.
5871
5872 For the environment task, the stack size depends on
5873 system defaults and is unknown to the compiler. The stack
5874 may even dynamically grow on some systems, precluding the
5875 normal Ada semantics for stack overflow. In the worst case,
5876 unbounded stack usage, causes unbounded stack expansion
5877 resulting in the system running out of virtual memory.
5878
5879 The stack checking may still work correctly if a fixed
5880 size stack is allocated, but this cannot be guaranteed.
5881 To ensure that a clean exception is signalled for stack
5882 overflow, set the environment variable
5883 @code{GNAT_STACK_LIMIT} to indicate the maximum
5884 stack area that can be used, as in:
5885 @cindex GNAT_STACK_LIMIT
5886
5887 @smallexample
5888 SET GNAT_STACK_LIMIT 1600
5889 @end smallexample
5890
5891 @noindent
5892 The limit is given in kilobytes, so the above declaration would
5893 set the stack limit of the environment task to 1.6 megabytes.
5894 Note that the only purpose of this usage is to limit the amount
5895 of stack used by the environment task. If it is necessary to
5896 increase the amount of stack for the environment task, then this
5897 is an operating systems issue, and must be addressed with the
5898 appropriate operating systems commands.
5899
5900 @node Using gcc for Syntax Checking
5901 @subsection Using @command{gcc} for Syntax Checking
5902 @table @option
5903 @item -gnats
5904 @cindex @option{-gnats} (@command{gcc})
5905 @ifclear vms
5906
5907 @noindent
5908 The @code{s} stands for ``syntax''.
5909 @end ifclear
5910
5911 Run GNAT in syntax checking only mode. For
5912 example, the command
5913
5914 @smallexample
5915 $ gcc -c -gnats x.adb
5916 @end smallexample
5917
5918 @noindent
5919 compiles file @file{x.adb} in syntax-check-only mode. You can check a
5920 series of files in a single command
5921 @ifclear vms
5922 , and can use wild cards to specify such a group of files.
5923 Note that you must specify the @option{-c} (compile
5924 only) flag in addition to the @option{-gnats} flag.
5925 @end ifclear
5926 .
5927 You may use other switches in conjunction with @option{-gnats}. In
5928 particular, @option{-gnatl} and @option{-gnatv} are useful to control the
5929 format of any generated error messages.
5930
5931 When the source file is empty or contains only empty lines and/or comments,
5932 the output is a warning:
5933
5934 @smallexample
5935 $ gcc -c -gnats -x ada toto.txt
5936 toto.txt:1:01: warning: empty file, contains no compilation units
5937 $
5938 @end smallexample
5939
5940 Otherwise, the output is simply the error messages, if any. No object file or
5941 ALI file is generated by a syntax-only compilation. Also, no units other
5942 than the one specified are accessed. For example, if a unit @code{X}
5943 @code{with}'s a unit @code{Y}, compiling unit @code{X} in syntax
5944 check only mode does not access the source file containing unit
5945 @code{Y}.
5946
5947 @cindex Multiple units, syntax checking
5948 Normally, GNAT allows only a single unit in a source file. However, this
5949 restriction does not apply in syntax-check-only mode, and it is possible
5950 to check a file containing multiple compilation units concatenated
5951 together. This is primarily used by the @code{gnatchop} utility
5952 (@pxref{Renaming Files Using gnatchop}).
5953 @end table
5954
5955 @node Using gcc for Semantic Checking
5956 @subsection Using @command{gcc} for Semantic Checking
5957 @table @option
5958 @item -gnatc
5959 @cindex @option{-gnatc} (@command{gcc})
5960
5961 @ifclear vms
5962 @noindent
5963 The @code{c} stands for ``check''.
5964 @end ifclear
5965 Causes the compiler to operate in semantic check mode,
5966 with full checking for all illegalities specified in the
5967 Ada 95 Reference Manual, but without generation of any object code
5968 (no object file is generated).
5969
5970 Because dependent files must be accessed, you must follow the GNAT
5971 semantic restrictions on file structuring to operate in this mode:
5972
5973 @itemize @bullet
5974 @item
5975 The needed source files must be accessible
5976 (@pxref{Search Paths and the Run-Time Library (RTL)}).
5977
5978 @item
5979 Each file must contain only one compilation unit.
5980
5981 @item
5982 The file name and unit name must match (@pxref{File Naming Rules}).
5983 @end itemize
5984
5985 The output consists of error messages as appropriate. No object file is
5986 generated. An @file{ALI} file is generated for use in the context of
5987 cross-reference tools, but this file is marked as not being suitable
5988 for binding (since no object file is generated).
5989 The checking corresponds exactly to the notion of
5990 legality in the Ada 95 Reference Manual.
5991
5992 Any unit can be compiled in semantics-checking-only mode, including
5993 units that would not normally be compiled (subunits,
5994 and specifications where a separate body is present).
5995 @end table
5996
5997 @node Compiling Different Versions of Ada
5998 @subsection Compiling Different Versions of Ada
5999 @table @option
6000 @cindex Compatibility with Ada 83
6001 @cindex Ada 83 mode
6002 @cindex Ada 95 mode
6003 @cindex Ada 2005 mode
6004
6005 GNAT is primarily an Ada 95 compiler, but the switches described in
6006 this section allow operation in Ada 83 compatibility mode, and also
6007 allow the use of a preliminary implementation of many of the expected
6008 new features in Ada 2005, the forthcoming new version of the standard.
6009
6010 @item -gnat83 (Ada 83 Compatibility Mode)
6011 @cindex @option{-gnat83} (@command{gcc})
6012 @cindex ACVC, Ada 83 tests
6013
6014 @noindent
6015 Although GNAT is primarily an Ada 95 compiler, it accepts this switch to
6016 specify that an Ada 83 program is to be compiled in Ada 83 mode. If you specify
6017 this switch, GNAT rejects most Ada 95 extensions and applies Ada 83 semantics
6018 where this can be done easily.
6019 It is not possible to guarantee this switch does a perfect
6020 job; for example, some subtle tests, such as are
6021 found in earlier ACVC tests (and that have been removed from the ACATS suite
6022 for Ada 95), might not compile correctly.
6023 Nevertheless, this switch may be useful in some circumstances, for example
6024 where, due to contractual reasons, legacy code needs to be maintained
6025 using only Ada 83 features.
6026
6027 With few exceptions (most notably the need to use @code{<>} on
6028 @cindex Generic formal parameters
6029 unconstrained generic formal parameters, the use of the new Ada 95
6030 reserved words, and the use of packages
6031 with optional bodies), it is not necessary to use the
6032 @option{-gnat83} switch when compiling Ada 83 programs, because, with rare
6033 exceptions, Ada 95 is upwardly compatible with Ada 83. This
6034 means that a correct Ada 83 program is usually also a correct Ada 95
6035 program.
6036 For further information, please refer to @ref{Compatibility and Porting Guide}.
6037
6038 @item -gnat95 (Ada 95 mode)
6039 @cindex @option{-gnat95} (@command{gcc})
6040
6041 @noindent
6042 GNAT is primarily an Ada 95 compiler, and all current releases of GNAT Pro
6043 compile in Ada 95 mode by default. Typically, Ada 95 is sufficiently upwards
6044 compatible with Ada 83, that legacy Ada 83 programs may be compiled using
6045 this default Ada95 mode without problems (see section above describing the
6046 use of @option{-gnat83} to run in Ada 83 mode).
6047
6048 In Ada 95 mode, the use of Ada 2005 features will in general cause error
6049 messages or warnings. Some specialized releases of GNAT (notably the GAP
6050 academic version) operate in Ada 2005 mode by default (see section below
6051 describing the use of @option{-gnat05} to run in Ada 2005 mode). For such
6052 versions the @option{-gnat95} switch may be used to enforce Ada 95 mode.
6053 This option also can be used to cancel the effect of a previous
6054 @option{-gnat83} or @option{-gnat05} switch earlier in the command line.
6055
6056
6057 @item -gnat05 (Ada 2005 mode)
6058 @cindex @option{-gnat05} (@command{gcc})
6059
6060 @noindent
6061 Although GNAT is primarily an Ada 95 compiler, it can be set to operate
6062 in Ada 2005 mode using this option. Although the new standard has not
6063 yet been issued (as of early 2005), many features have been discussed and
6064 approved in ``Ada Issues'' (AI's). For the text of these AI's, see
6065 @url{www.ada-auth.org/cgi-bin/cvsweb.cgi/AIs}. Included with GNAT
6066 releases is a file @file{features-ada0y} that describes the current set
6067 of implemented Ada 2005 features.
6068
6069 If these features are used in Ada 95 mode (which is the normal default),
6070 then error messages or warnings may be
6071 generated, reflecting the fact that these new features are otherwise
6072 unauthorized extensions to Ada 95. The use of the @option{-gnat05}
6073 switch (or an equivalent pragma) causes these messages to be suppressed.
6074
6075 Note that some specialized releases of GNAT (notably the GAP academic
6076 version) have Ada 2005 mode on by default, and in such environments,
6077 the Ada 2005 features can be used freely without the use of switches.
6078
6079 @end table
6080
6081 @node Character Set Control
6082 @subsection Character Set Control
6083 @table @option
6084 @item ^-gnati^/IDENTIFIER_CHARACTER_SET=^@var{c}
6085 @cindex @option{^-gnati^/IDENTIFIER_CHARACTER_SET^} (@command{gcc})
6086
6087 @noindent
6088 Normally GNAT recognizes the Latin-1 character set in source program
6089 identifiers, as described in the Ada 95 Reference Manual.
6090 This switch causes
6091 GNAT to recognize alternate character sets in identifiers. @var{c} is a
6092 single character ^^or word^ indicating the character set, as follows:
6093
6094 @table @code
6095 @item 1
6096 ISO 8859-1 (Latin-1) identifiers
6097
6098 @item 2
6099 ISO 8859-2 (Latin-2) letters allowed in identifiers
6100
6101 @item 3
6102 ISO 8859-3 (Latin-3) letters allowed in identifiers
6103
6104 @item 4
6105 ISO 8859-4 (Latin-4) letters allowed in identifiers
6106
6107 @item 5
6108 ISO 8859-5 (Cyrillic) letters allowed in identifiers
6109
6110 @item 9
6111 ISO 8859-15 (Latin-9) letters allowed in identifiers
6112
6113 @item ^p^PC^
6114 IBM PC letters (code page 437) allowed in identifiers
6115
6116 @item ^8^PC850^
6117 IBM PC letters (code page 850) allowed in identifiers
6118
6119 @item ^f^FULL_UPPER^
6120 Full upper-half codes allowed in identifiers
6121
6122 @item ^n^NO_UPPER^
6123 No upper-half codes allowed in identifiers
6124
6125 @item ^w^WIDE^
6126 Wide-character codes (that is, codes greater than 255)
6127 allowed in identifiers
6128 @end table
6129
6130 @xref{Foreign Language Representation}, for full details on the
6131 implementation of these character sets.
6132
6133 @item ^-gnatW^/WIDE_CHARACTER_ENCODING=^@var{e}
6134 @cindex @option{^-gnatW^/WIDE_CHARACTER_ENCODING^} (@command{gcc})
6135 Specify the method of encoding for wide characters.
6136 @var{e} is one of the following:
6137
6138 @table @code
6139
6140 @item ^h^HEX^
6141 Hex encoding (brackets coding also recognized)
6142
6143 @item ^u^UPPER^
6144 Upper half encoding (brackets encoding also recognized)
6145
6146 @item ^s^SHIFT_JIS^
6147 Shift/JIS encoding (brackets encoding also recognized)
6148
6149 @item ^e^EUC^
6150 EUC encoding (brackets encoding also recognized)
6151
6152 @item ^8^UTF8^
6153 UTF-8 encoding (brackets encoding also recognized)
6154
6155 @item ^b^BRACKETS^
6156 Brackets encoding only (default value)
6157 @end table
6158 For full details on the these encoding
6159 methods see @ref{Wide Character Encodings}.
6160 Note that brackets coding is always accepted, even if one of the other
6161 options is specified, so for example @option{-gnatW8} specifies that both
6162 brackets and @code{UTF-8} encodings will be recognized. The units that are
6163 with'ed directly or indirectly will be scanned using the specified
6164 representation scheme, and so if one of the non-brackets scheme is
6165 used, it must be used consistently throughout the program. However,
6166 since brackets encoding is always recognized, it may be conveniently
6167 used in standard libraries, allowing these libraries to be used with
6168 any of the available coding schemes.
6169 scheme. If no @option{-gnatW?} parameter is present, then the default
6170 representation is Brackets encoding only.
6171
6172 Note that the wide character representation that is specified (explicitly
6173 or by default) for the main program also acts as the default encoding used
6174 for Wide_Text_IO files if not specifically overridden by a WCEM form
6175 parameter.
6176
6177 @end table
6178 @node File Naming Control
6179 @subsection File Naming Control
6180
6181 @table @option
6182 @item ^-gnatk^/FILE_NAME_MAX_LENGTH=^@var{n}
6183 @cindex @option{-gnatk} (@command{gcc})
6184 Activates file name ``krunching''. @var{n}, a decimal integer in the range
6185 1-999, indicates the maximum allowable length of a file name (not
6186 including the @file{.ads} or @file{.adb} extension). The default is not
6187 to enable file name krunching.
6188
6189 For the source file naming rules, @xref{File Naming Rules}.
6190 @end table
6191
6192 @node Subprogram Inlining Control
6193 @subsection Subprogram Inlining Control
6194
6195 @table @option
6196 @c !sort!
6197 @item -gnatn
6198 @cindex @option{-gnatn} (@command{gcc})
6199 @ifclear vms
6200 The @code{n} here is intended to suggest the first syllable of the
6201 word ``inline''.
6202 @end ifclear
6203 GNAT recognizes and processes @code{Inline} pragmas. However, for the
6204 inlining to actually occur, optimization must be enabled. To enable
6205 inlining of subprograms specified by pragma @code{Inline},
6206 you must also specify this switch.
6207 In the absence of this switch, GNAT does not attempt
6208 inlining and does not need to access the bodies of
6209 subprograms for which @code{pragma Inline} is specified if they are not
6210 in the current unit.
6211
6212 If you specify this switch the compiler will access these bodies,
6213 creating an extra source dependency for the resulting object file, and
6214 where possible, the call will be inlined.
6215 For further details on when inlining is possible
6216 see @ref{Inlining of Subprograms}.
6217
6218 @item -gnatN
6219 @cindex @option{-gnatN} (@command{gcc})
6220 The front end inlining activated by this switch is generally more extensive,
6221 and quite often more effective than the standard @option{-gnatn} inlining mode.
6222 It will also generate additional dependencies.
6223 Note that
6224 @option{-gnatN} automatically implies @option{-gnatn} so it is not necessary
6225 to specify both options.
6226 @end table
6227
6228 @node Auxiliary Output Control
6229 @subsection Auxiliary Output Control
6230
6231 @table @option
6232 @item -gnatt
6233 @cindex @option{-gnatt} (@command{gcc})
6234 @cindex Writing internal trees
6235 @cindex Internal trees, writing to file
6236 Causes GNAT to write the internal tree for a unit to a file (with the
6237 extension @file{.adt}.
6238 This not normally required, but is used by separate analysis tools.
6239 Typically
6240 these tools do the necessary compilations automatically, so you should
6241 not have to specify this switch in normal operation.
6242
6243 @item -gnatu
6244 @cindex @option{-gnatu} (@command{gcc})
6245 Print a list of units required by this compilation on @file{stdout}.
6246 The listing includes all units on which the unit being compiled depends
6247 either directly or indirectly.
6248
6249 @ifclear vms
6250 @item -pass-exit-codes
6251 @cindex @option{-pass-exit-codes} (@command{gcc})
6252 If this switch is not used, the exit code returned by @command{gcc} when
6253 compiling multiple files indicates whether all source files have
6254 been successfully used to generate object files or not.
6255
6256 When @option{-pass-exit-codes} is used, @command{gcc} exits with an extended
6257 exit status and allows an integrated development environment to better
6258 react to a compilation failure. Those exit status are:
6259
6260 @table @asis
6261 @item 5
6262 There was an error in at least one source file.
6263 @item 3
6264 At least one source file did not generate an object file.
6265 @item 2
6266 The compiler died unexpectedly (internal error for example).
6267 @item 0
6268 An object file has been generated for every source file.
6269 @end table
6270 @end ifclear
6271 @end table
6272
6273 @node Debugging Control
6274 @subsection Debugging Control
6275
6276 @table @option
6277 @c !sort!
6278 @cindex Debugging options
6279 @ifclear vms
6280 @item -gnatd@var{x}
6281 @cindex @option{-gnatd} (@command{gcc})
6282 Activate internal debugging switches. @var{x} is a letter or digit, or
6283 string of letters or digits, which specifies the type of debugging
6284 outputs desired. Normally these are used only for internal development
6285 or system debugging purposes. You can find full documentation for these
6286 switches in the body of the @code{Debug} unit in the compiler source
6287 file @file{debug.adb}.
6288 @end ifclear
6289
6290 @item -gnatG
6291 @cindex @option{-gnatG} (@command{gcc})
6292 This switch causes the compiler to generate auxiliary output containing
6293 a pseudo-source listing of the generated expanded code. Like most Ada
6294 compilers, GNAT works by first transforming the high level Ada code into
6295 lower level constructs. For example, tasking operations are transformed
6296 into calls to the tasking run-time routines. A unique capability of GNAT
6297 is to list this expanded code in a form very close to normal Ada source.
6298 This is very useful in understanding the implications of various Ada
6299 usage on the efficiency of the generated code. There are many cases in
6300 Ada (e.g. the use of controlled types), where simple Ada statements can
6301 generate a lot of run-time code. By using @option{-gnatG} you can identify
6302 these cases, and consider whether it may be desirable to modify the coding
6303 approach to improve efficiency.
6304
6305 The format of the output is very similar to standard Ada source, and is
6306 easily understood by an Ada programmer. The following special syntactic
6307 additions correspond to low level features used in the generated code that
6308 do not have any exact analogies in pure Ada source form. The following
6309 is a partial list of these special constructions. See the specification
6310 of package @code{Sprint} in file @file{sprint.ads} for a full list.
6311
6312 @table @code
6313 @item new @var{xxx} [storage_pool = @var{yyy}]
6314 Shows the storage pool being used for an allocator.
6315
6316 @item at end @var{procedure-name};
6317 Shows the finalization (cleanup) procedure for a scope.
6318
6319 @item (if @var{expr} then @var{expr} else @var{expr})
6320 Conditional expression equivalent to the @code{x?y:z} construction in C.
6321
6322 @item @var{target}^^^(@var{source})
6323 A conversion with floating-point truncation instead of rounding.
6324
6325 @item @var{target}?(@var{source})
6326 A conversion that bypasses normal Ada semantic checking. In particular
6327 enumeration types and fixed-point types are treated simply as integers.
6328
6329 @item @var{target}?^^^(@var{source})
6330 Combines the above two cases.
6331
6332 @item @var{x} #/ @var{y}
6333 @itemx @var{x} #mod @var{y}
6334 @itemx @var{x} #* @var{y}
6335 @itemx @var{x} #rem @var{y}
6336 A division or multiplication of fixed-point values which are treated as
6337 integers without any kind of scaling.
6338
6339 @item free @var{expr} [storage_pool = @var{xxx}]
6340 Shows the storage pool associated with a @code{free} statement.
6341
6342 @item freeze @var{typename} [@var{actions}]
6343 Shows the point at which @var{typename} is frozen, with possible
6344 associated actions to be performed at the freeze point.
6345
6346 @item reference @var{itype}
6347 Reference (and hence definition) to internal type @var{itype}.
6348
6349 @item @var{function-name}! (@var{arg}, @var{arg}, @var{arg})
6350 Intrinsic function call.
6351
6352 @item @var{labelname} : label
6353 Declaration of label @var{labelname}.
6354
6355 @item @var{expr} && @var{expr} && @var{expr} ... && @var{expr}
6356 A multiple concatenation (same effect as @var{expr} & @var{expr} &
6357 @var{expr}, but handled more efficiently).
6358
6359 @item [constraint_error]
6360 Raise the @code{Constraint_Error} exception.
6361
6362 @item @var{expression}'reference
6363 A pointer to the result of evaluating @var{expression}.
6364
6365 @item @var{target-type}!(@var{source-expression})
6366 An unchecked conversion of @var{source-expression} to @var{target-type}.
6367
6368 @item [@var{numerator}/@var{denominator}]
6369 Used to represent internal real literals (that) have no exact
6370 representation in base 2-16 (for example, the result of compile time
6371 evaluation of the expression 1.0/27.0).
6372 @end table
6373
6374 @item -gnatD
6375 @cindex @option{-gnatD} (@command{gcc})
6376 When used in conjunction with @option{-gnatG}, this switch causes
6377 the expanded source, as described above for
6378 @option{-gnatG} to be written to files with names
6379 @file{^xxx.dg^XXX_DG^}, where @file{xxx} is the normal file name,
6380 instead of to the standard ooutput file. For
6381 example, if the source file name is @file{hello.adb}, then a file
6382 @file{^hello.adb.dg^HELLO.ADB_DG^} will be written.  The debugging
6383 information generated by the @command{gcc} @option{^-g^/DEBUG^} switch
6384 will refer to the generated @file{^xxx.dg^XXX_DG^} file. This allows
6385 you to do source level debugging using the generated code which is
6386 sometimes useful for complex code, for example to find out exactly
6387 which part of a complex construction raised an exception. This switch
6388 also suppress generation of cross-reference information (see
6389 @option{-gnatx}) since otherwise the cross-reference information
6390 would refer to the @file{^.dg^.DG^} file, which would cause
6391 confusion since this is not the original source file.
6392
6393 Note that @option{-gnatD} actually implies @option{-gnatG}
6394 automatically, so it is not necessary to give both options.
6395 In other words @option{-gnatD} is equivalent to @option{-gnatDG}).
6396
6397 @ifclear vms
6398 @item -gnatR[0|1|2|3[s]]
6399 @cindex @option{-gnatR} (@command{gcc})
6400 This switch controls output from the compiler of a listing showing
6401 representation information for declared types and objects. For
6402 @option{-gnatR0}, no information is output (equivalent to omitting
6403 the @option{-gnatR} switch). For @option{-gnatR1} (which is the default,
6404 so @option{-gnatR} with no parameter has the same effect), size and alignment
6405 information is listed for declared array and record types. For
6406 @option{-gnatR2}, size and alignment information is listed for all
6407 expression information for values that are computed at run time for
6408 variant records. These symbolic expressions have a mostly obvious
6409 format with #n being used to represent the value of the n'th
6410 discriminant. See source files @file{repinfo.ads/adb} in the
6411 @code{GNAT} sources for full details on the format of @option{-gnatR3}
6412 output. If the switch is followed by an s (e.g. @option{-gnatR2s}), then
6413 the output is to a file with the name @file{^file.rep^file_REP^} where
6414 file is the name of the corresponding source file.
6415 @end ifclear
6416 @ifset vms
6417 @item /REPRESENTATION_INFO
6418 @cindex @option{/REPRESENTATION_INFO} (@command{gcc})
6419 This qualifier controls output from the compiler of a listing showing
6420 representation information for declared types and objects. For
6421 @option{/REPRESENTATION_INFO=NONE}, no information is output
6422 (equivalent to omitting the @option{/REPRESENTATION_INFO} qualifier).
6423 @option{/REPRESENTATION_INFO} without option is equivalent to
6424 @option{/REPRESENTATION_INFO=ARRAYS}.
6425 For @option{/REPRESENTATION_INFO=ARRAYS}, size and alignment
6426 information is listed for declared array and record types. For
6427 @option{/REPRESENTATION_INFO=OBJECTS}, size and alignment information
6428 is listed for all expression information for values that are computed
6429 at run time for variant records. These symbolic expressions have a mostly
6430 obvious format with #n being used to represent the value of the n'th
6431 discriminant. See source files @file{REPINFO.ADS/ADB} in the
6432 @code{GNAT} sources for full details on the format of
6433 @option{/REPRESENTATION_INFO=SYMBOLIC} output.
6434 If _FILE is added at the end of an option
6435 (e.g. @option{/REPRESENTATION_INFO=ARRAYS_FILE}),
6436 then the output is to a file with the name @file{file_REP} where
6437 file is the name of the corresponding source file.
6438 @end ifset
6439 Note that it is possible for record components to have zero size. In
6440 this case, the component clause uses an obvious extension of permitted
6441 Ada syntax, for example @code{at 0 range 0 .. -1}.
6442
6443 @item -gnatS
6444 @cindex @option{-gnatS} (@command{gcc})
6445 The use of the switch @option{-gnatS} for an
6446 Ada compilation will cause the compiler to output a
6447 representation of package Standard in a form very
6448 close to standard Ada. It is not quite possible to
6449 do this entirely in standard Ada (since new
6450 numeric base types cannot be created in standard
6451 Ada), but the output is easily
6452 readable to any Ada programmer, and is useful to
6453 determine the characteristics of target dependent
6454 types in package Standard.
6455
6456 @item -gnatx
6457 @cindex @option{-gnatx} (@command{gcc})
6458 Normally the compiler generates full cross-referencing information in
6459 the @file{ALI} file. This information is used by a number of tools,
6460 including @code{gnatfind} and @code{gnatxref}. The @option{-gnatx} switch
6461 suppresses this information. This saves some space and may slightly
6462 speed up compilation, but means that these tools cannot be used.
6463 @end table
6464
6465 @node Exception Handling Control
6466 @subsection Exception Handling Control
6467
6468 @noindent
6469 GNAT uses two methods for handling exceptions at run-time. The
6470 @code{setjmp/longjmp} method saves the context when entering
6471 a frame with an exception handler. Then when an exception is
6472 raised, the context can be restored immediately, without the
6473 need for tracing stack frames. This method provides very fast
6474 exception propagation, but introduces significant overhead for
6475 the use of exception handlers, even if no exception is raised.
6476
6477 The other approach is called ``zero cost'' exception handling.
6478 With this method, the compiler builds static tables to describe
6479 the exception ranges. No dynamic code is required when entering
6480 a frame containing an exception handler. When an exception is
6481 raised, the tables are used to control a back trace of the
6482 subprogram invocation stack to locate the required exception
6483 handler. This method has considerably poorer performance for
6484 the propagation of exceptions, but there is no overhead for
6485 exception handlers if no exception is raised. Note that in this
6486 mode and in the context of mixed Ada and C/C++ programming,
6487 to propagate an exception through a C/C++ code, the C/C++ code
6488 must be compiled with the @option{-funwind-tables} GCC's
6489 option.
6490
6491 The following switches can be used to control which of the
6492 two exception handling methods is used.
6493
6494 @table @option
6495 @c !sort!
6496
6497 @item --RTS=sjlj
6498 @cindex @option{--RTS=sjlj} (@command{gnatmake})
6499 This switch causes the setjmp/longjmp run-time to be used
6500 for exception handling. If this is the default mechanism for the
6501 target (see below), then this has no effect. If the default
6502 mechanism for the target is zero cost exceptions, then
6503 this switch can be used to modify this default, and must be
6504 used for all units in the partition.
6505 This option is rarely used. One case in which it may be
6506 advantageous is if you have an application where exception
6507 raising is common and the overall performance of the
6508 application is improved by favoring exception propagation.
6509
6510 @item --RTS=zcx
6511 @cindex @option{--RTS=zcx} (@command{gnatmake})
6512 @cindex Zero Cost Exceptions
6513 This switch causes the zero cost approach to be used
6514 for exception handling. If this is the default mechanism for the
6515 target (see below), then this has no effect. If the default
6516 mechanism for the target is setjmp/longjmp exceptions, then
6517 this switch can be used to modify this default, and must be
6518 used for all units in the partition.
6519 This option can only be used if the zero cost approach
6520 is available for the target in use (see below).
6521 @end table
6522
6523 @noindent
6524 The @code{setjmp/longjmp} approach is available on all targets, while
6525 the @code{zero cost} approach is available on selected targets.
6526 To determine whether zero cost exceptions can be used for a
6527 particular target, look at the private part of the file system.ads.
6528 Either @code{GCC_ZCX_Support} or @code{Front_End_ZCX_Support} must
6529 be True to use the zero cost approach. If both of these switches
6530 are set to False, this means that zero cost exception handling
6531 is not yet available for that target. The switch
6532 @code{ZCX_By_Default} indicates the default approach. If this
6533 switch is set to True, then the @code{zero cost} approach is
6534 used by default.
6535
6536 @node Units to Sources Mapping Files
6537 @subsection Units to Sources Mapping Files
6538
6539 @table @option
6540
6541 @item -gnatem^^=^@var{path}
6542 @cindex @option{-gnatem} (@command{gcc})
6543 A mapping file is a way to communicate to the compiler two mappings:
6544 from unit names to file names (without any directory information) and from
6545 file names to path names (with full directory information). These mappings
6546 are used by the compiler to short-circuit the path search.
6547
6548 The use of mapping files is not required for correct operation of the
6549 compiler, but mapping files can improve efficiency, particularly when
6550 sources are read over a slow network connection. In normal operation,
6551 you need not be concerned with the format or use of mapping files,
6552 and the @option{-gnatem} switch is not a switch that you would use
6553 explicitly. it is intended only for use by automatic tools such as
6554 @command{gnatmake} running under the project file facility. The
6555 description here of the format of mapping files is provided
6556 for completeness and for possible use by other tools.
6557
6558 A mapping file is a sequence of sets of three lines. In each set,
6559 the first line is the unit name, in lower case, with ``@code{%s}''
6560 appended for
6561 specifications and ``@code{%b}'' appended for bodies; the second line is the
6562 file name; and the third line is the path name.
6563
6564 Example:
6565 @smallexample
6566    main%b
6567    main.2.ada
6568    /gnat/project1/sources/main.2.ada
6569 @end smallexample
6570
6571 When the switch @option{-gnatem} is specified, the compiler will create
6572 in memory the two mappings from the specified file. If there is any problem
6573 (non existent file, truncated file or duplicate entries), no mapping
6574 will be created.
6575
6576 Several @option{-gnatem} switches may be specified; however, only the last
6577 one on the command line will be taken into account.
6578
6579 When using a project file, @command{gnatmake} create a temporary mapping file
6580 and communicates it to the compiler using this switch.
6581
6582 @end table
6583
6584 @node Integrated Preprocessing
6585 @subsection Integrated Preprocessing
6586
6587 @noindent
6588 GNAT sources may be preprocessed immediately before compilation; the actual
6589 text of the source is not the text of the source file, but is derived from it
6590 through a process called preprocessing. Integrated preprocessing is specified
6591 through switches @option{-gnatep} and/or @option{-gnateD}. @option{-gnatep}
6592 indicates, through a text file, the preprocessing data to be used.
6593 @option{-gnateD} specifies or modifies the values of preprocessing symbol.
6594
6595 @noindent
6596 It is recommended that @command{gnatmake} switch ^-s^/SWITCH_CHECK^ should be
6597 used when Integrated Preprocessing is used. The reason is that preprocessing
6598 with another Preprocessing Data file without changing the sources will
6599 not trigger recompilation without this switch.
6600
6601 @noindent
6602 Note that @command{gnatmake} switch ^-m^/MINIMAL_RECOMPILATION^ will almost
6603 always trigger recompilation for sources that are preprocessed,
6604 because @command{gnatmake} cannot compute the checksum of the source after
6605 preprocessing.
6606
6607 @noindent
6608 The actual preprocessing function is described in details in section
6609 @ref{Preprocessing Using gnatprep}. This section only describes how integrated
6610 preprocessing is triggered and parameterized.
6611
6612 @table @code
6613
6614 @item -gnatep=@var{file}
6615 @cindex @option{-gnatep} (@command{gcc})
6616 This switch indicates to the compiler the file name (without directory
6617 information) of the preprocessor data file to use. The preprocessor data file
6618 should be found in the source directories.
6619
6620 @noindent
6621 A preprocessing data file is a text file with significant lines indicating
6622 how should be preprocessed either a specific source or all sources not
6623 mentioned in other lines. A significant line is a non empty, non comment line.
6624 Comments are similar to Ada comments.
6625
6626 @noindent
6627 Each significant line starts with either a literal string or the character '*'.
6628 A literal string is the file name (without directory information) of the source
6629 to preprocess. A character '*' indicates the preprocessing for all the sources
6630 that are not specified explicitly on other lines (order of the lines is not
6631 significant). It is an error to have two lines with the same file name or two
6632 lines starting with the character '*'.
6633
6634 @noindent
6635 After the file name or the character '*', another optional literal string
6636 indicating the file name of the definition file to be used for preprocessing
6637 (@pxref{Form of Definitions File}). The definition files are found by the
6638 compiler in one of the source directories. In some cases, when compiling
6639 a source in a directory other than the current directory, if the definition
6640 file is in the current directory, it may be necessary to add the current
6641 directory as a source directory through switch ^-I.^/SEARCH=[]^, otherwise
6642 the compiler would not find the definition file.
6643
6644 @noindent
6645 Then, optionally, ^switches^switches^ similar to those of @code{gnatprep} may
6646 be found. Those ^switches^switches^ are:
6647
6648 @table @code
6649
6650 @item -b
6651 Causes both preprocessor lines and the lines deleted by
6652 preprocessing to be replaced by blank lines, preserving the line number.
6653 This ^switch^switch^ is always implied; however, if specified after @option{-c}
6654 it cancels the effect of @option{-c}.
6655
6656 @item -c
6657 Causes both preprocessor lines and the lines deleted
6658 by preprocessing to be retained as comments marked
6659 with the special string ``@code{--! }''.
6660
6661 @item -Dsymbol=value
6662 Define or redefine a symbol, associated with value. A symbol is an Ada
6663 identifier, or an Ada reserved word, with the exception of @code{if},
6664 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
6665 @code{value} is either a literal string, an Ada identifier or any Ada reserved
6666 word. A symbol declared with this ^switch^switch^ replaces a symbol with the
6667 same name defined in a definition file.
6668
6669 @item -s
6670 Causes a sorted list of symbol names and values to be
6671 listed on the standard output file.
6672
6673 @item -u
6674 Causes undefined symbols to be treated as having the value @code{FALSE}
6675 in the context
6676 of a preprocessor test. In the absence of this option, an undefined symbol in
6677 a @code{#if} or @code{#elsif} test will be treated as an error.
6678
6679 @end table
6680
6681 @noindent
6682 Examples of valid lines in a preprocessor data file:
6683
6684 @smallexample
6685   "toto.adb"  "prep.def" -u
6686   --  preprocess "toto.adb", using definition file "prep.def",
6687   --  undefined symbol are False.
6688
6689   * -c -DVERSION=V101
6690   --  preprocess all other sources without a definition file;
6691   --  suppressed lined are commented; symbol VERSION has the value V101.
6692
6693   "titi.adb" "prep2.def" -s
6694   --  preprocess "titi.adb", using definition file "prep2.def";
6695   --  list all symbols with their values.
6696 @end smallexample
6697
6698 @item ^-gnateD^/DATA_PREPROCESSING=^symbol[=value]
6699 @cindex @option{-gnateD} (@command{gcc})
6700 Define or redefine a preprocessing symbol, associated with value. If no value
6701 is given on the command line, then the value of the symbol is @code{True}.
6702 A symbol is an identifier, following normal Ada (case-insensitive)
6703 rules for its syntax, and value is any sequence (including an empty sequence)
6704 of characters from the set (letters, digits, period, underline).
6705 Ada reserved words may be used as symbols, with the exceptions of @code{if},
6706 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
6707
6708 @noindent
6709 A symbol declared with this ^switch^switch^ on the command line replaces a
6710 symbol with the same name either in a definition file or specified with a
6711 ^switch^switch^ -D in the preprocessor data file.
6712
6713 @noindent
6714 This switch is similar to switch @option{^-D^/ASSOCIATE^} of @code{gnatprep}.
6715
6716 @end table
6717
6718 @node Code Generation Control
6719 @subsection Code Generation Control
6720
6721 @noindent
6722
6723 The GCC technology provides a wide range of target dependent
6724 @option{-m} switches for controlling
6725 details of code generation with respect to different versions of
6726 architectures. This includes variations in instruction sets (e.g.
6727 different members of the power pc family), and different requirements
6728 for optimal arrangement of instructions (e.g. different members of
6729 the x86 family). The list of available @option{-m} switches may be
6730 found in the GCC documentation.
6731
6732 Use of the these @option{-m} switches may in some cases result in improved
6733 code performance.
6734
6735 The GNAT Pro technology is tested and qualified without any
6736 @option{-m} switches,
6737 so generally the most reliable approach is to avoid the use of these
6738 switches. However, we generally expect most of these switches to work
6739 successfully with GNAT Pro, and many customers have reported successful
6740 use of these options.
6741
6742 Our general advice is to avoid the use of @option{-m} switches unless
6743 special needs lead to requirements in this area. In particular,
6744 there is no point in using @option{-m} switches to improve performance
6745 unless you actually see a performance improvement.
6746
6747 @ifset vms
6748 @node Return Codes
6749 @subsection Return Codes
6750 @cindex Return Codes
6751 @cindex @option{/RETURN_CODES=VMS}
6752
6753 @noindent
6754 On VMS, GNAT compiled programs return POSIX-style codes by default,
6755 e.g. @option{/RETURN_CODES=POSIX}.
6756
6757 To enable VMS style return codes, use GNAT BIND and LINK with the option
6758 @option{/RETURN_CODES=VMS}. For example:
6759
6760 @smallexample
6761 GNAT BIND MYMAIN.ALI /RETURN_CODES=VMS
6762 GNAT LINK MYMAIN.ALI /RETURN_CODES=VMS
6763 @end smallexample
6764
6765 @noindent
6766 Programs built with /RETURN_CODES=VMS are suitable to be called in
6767 VMS DCL scripts. Programs compiled with the default /RETURN_CODES=POSIX
6768 are suitable for spawning with appropriate GNAT RTL routines.
6769
6770 @end ifset
6771
6772 @node Search Paths and the Run-Time Library (RTL)
6773 @section Search Paths and the Run-Time Library (RTL)
6774
6775 @noindent
6776 With the GNAT source-based library system, the compiler must be able to
6777 find source files for units that are needed by the unit being compiled.
6778 Search paths are used to guide this process.
6779
6780 The compiler compiles one source file whose name must be given
6781 explicitly on the command line. In other words, no searching is done
6782 for this file. To find all other source files that are needed (the most
6783 common being the specs of units), the compiler examines the following
6784 directories, in the following order:
6785
6786 @enumerate
6787 @item
6788 The directory containing the source file of the main unit being compiled
6789 (the file name on the command line).
6790
6791 @item
6792 Each directory named by an @option{^-I^/SOURCE_SEARCH^} switch given on the
6793 @command{gcc} command line, in the order given.
6794
6795 @item
6796 @findex ADA_PRJ_INCLUDE_FILE
6797 Each of the directories listed in the text file whose name is given
6798 by the @code{ADA_PRJ_INCLUDE_FILE} ^environment variable^logical name^.
6799
6800 @noindent
6801 @code{ADA_PRJ_INCLUDE_FILE} is normally set by gnatmake or by the ^gnat^GNAT^
6802 driver when project files are used. It should not normally be set
6803 by other means.
6804
6805 @item
6806 @findex ADA_INCLUDE_PATH
6807 Each of the directories listed in the value of the
6808 @code{ADA_INCLUDE_PATH} ^environment variable^logical name^.
6809 @ifclear vms
6810 Construct this value
6811 exactly as the @code{PATH} environment variable: a list of directory
6812 names separated by colons (semicolons when working with the NT version).
6813 @end ifclear
6814 @ifset vms
6815 Normally, define this value as a logical name containing a comma separated
6816 list of directory names.
6817
6818 This variable can also be defined by means of an environment string
6819 (an argument to the HP C exec* set of functions).
6820
6821 Logical Name:
6822 @smallexample
6823 DEFINE ANOTHER_PATH FOO:[BAG]
6824 DEFINE ADA_INCLUDE_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
6825 @end smallexample
6826
6827 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
6828 first, followed by the standard Ada 95
6829 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADAINCLUDE].
6830 If this is not redefined, the user will obtain the HP Ada 83 IO packages
6831 (Text_IO, Sequential_IO, etc)
6832 instead of the Ada95 packages. Thus, in order to get the Ada 95
6833 packages by default, ADA_INCLUDE_PATH must be redefined.
6834 @end ifset
6835
6836 @item
6837 The content of the @file{ada_source_path} file which is part of the GNAT
6838 installation tree and is used to store standard libraries such as the
6839 GNAT Run Time Library (RTL) source files.
6840 @ifclear vms
6841 @ref{Installing a library}
6842 @end ifclear
6843 @end enumerate
6844
6845 @noindent
6846 Specifying the switch @option{^-I-^/NOCURRENT_DIRECTORY^}
6847 inhibits the use of the directory
6848 containing the source file named in the command line. You can still
6849 have this directory on your search path, but in this case it must be
6850 explicitly requested with a @option{^-I^/SOURCE_SEARCH^} switch.
6851
6852 Specifying the switch @option{-nostdinc}
6853 inhibits the search of the default location for the GNAT Run Time
6854 Library (RTL) source files.
6855
6856 The compiler outputs its object files and ALI files in the current
6857 working directory.
6858 @ifclear vms
6859 Caution: The object file can be redirected with the @option{-o} switch;
6860 however, @command{gcc} and @code{gnat1} have not been coordinated on this
6861 so the @file{ALI} file will not go to the right place. Therefore, you should
6862 avoid using the @option{-o} switch.
6863 @end ifclear
6864
6865 @findex System.IO
6866 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
6867 children make up the GNAT RTL, together with the simple @code{System.IO}
6868 package used in the @code{"Hello World"} example. The sources for these units
6869 are needed by the compiler and are kept together in one directory. Not
6870 all of the bodies are needed, but all of the sources are kept together
6871 anyway. In a normal installation, you need not specify these directory
6872 names when compiling or binding. Either the environment variables or
6873 the built-in defaults cause these files to be found.
6874
6875 In addition to the language-defined hierarchies (@code{System}, @code{Ada} and
6876 @code{Interfaces}), the GNAT distribution provides a fourth hierarchy,
6877 consisting of child units of @code{GNAT}. This is a collection of generally
6878 useful types, subprograms, etc. See the @cite{GNAT Reference Manual} for
6879 further details.
6880
6881 Besides simplifying access to the RTL, a major use of search paths is
6882 in compiling sources from multiple directories. This can make
6883 development environments much more flexible.
6884
6885 @node Order of Compilation Issues
6886 @section Order of Compilation Issues
6887
6888 @noindent
6889 If, in our earlier example, there was a spec for the @code{hello}
6890 procedure, it would be contained in the file @file{hello.ads}; yet this
6891 file would not have to be explicitly compiled. This is the result of the
6892 model we chose to implement library management. Some of the consequences
6893 of this model are as follows:
6894
6895 @itemize @bullet
6896 @item
6897 There is no point in compiling specs (except for package
6898 specs with no bodies) because these are compiled as needed by clients. If
6899 you attempt a useless compilation, you will receive an error message.
6900 It is also useless to compile subunits because they are compiled as needed
6901 by the parent.
6902
6903 @item
6904 There are no order of compilation requirements: performing a
6905 compilation never obsoletes anything. The only way you can obsolete
6906 something and require recompilations is to modify one of the
6907 source files on which it depends.
6908
6909 @item
6910 There is no library as such, apart from the ALI files
6911 (@pxref{The Ada Library Information Files}, for information on the format
6912 of these files). For now we find it convenient to create separate ALI files,
6913 but eventually the information therein may be incorporated into the object
6914 file directly.
6915
6916 @item
6917 When you compile a unit, the source files for the specs of all units
6918 that it @code{with}'s, all its subunits, and the bodies of any generics it
6919 instantiates must be available (reachable by the search-paths mechanism
6920 described above), or you will receive a fatal error message.
6921 @end itemize
6922
6923 @node Examples
6924 @section Examples
6925
6926 @noindent
6927 The following are some typical Ada compilation command line examples:
6928
6929 @table @code
6930 @item $ gcc -c xyz.adb
6931 Compile body in file @file{xyz.adb} with all default options.
6932
6933 @ifclear vms
6934 @item $ gcc -c -O2 -gnata xyz-def.adb
6935 @end ifclear
6936 @ifset vms
6937 @item $ GNAT COMPILE /OPTIMIZE=ALL -gnata xyz-def.adb
6938 @end ifset
6939
6940 Compile the child unit package in file @file{xyz-def.adb} with extensive
6941 optimizations, and pragma @code{Assert}/@code{Debug} statements
6942 enabled.
6943
6944 @item $ gcc -c -gnatc abc-def.adb
6945 Compile the subunit in file @file{abc-def.adb} in semantic-checking-only
6946 mode.
6947 @end table
6948
6949 @node Binding Using gnatbind
6950 @chapter Binding Using @code{gnatbind}
6951 @findex gnatbind
6952
6953 @menu
6954 * Running gnatbind::
6955 * Switches for gnatbind::
6956 * Command-Line Access::
6957 * Search Paths for gnatbind::
6958 * Examples of gnatbind Usage::
6959 @end menu
6960
6961 @noindent
6962 This chapter describes the GNAT binder, @code{gnatbind}, which is used
6963 to bind compiled GNAT objects. The @code{gnatbind} program performs
6964 four separate functions:
6965
6966 @enumerate
6967 @item
6968 Checks that a program is consistent, in accordance with the rules in
6969 Chapter 10 of the Ada 95 Reference Manual. In particular, error
6970 messages are generated if a program uses inconsistent versions of a
6971 given unit.
6972
6973 @item
6974 Checks that an acceptable order of elaboration exists for the program
6975 and issues an error message if it cannot find an order of elaboration
6976 that satisfies the rules in Chapter 10 of the Ada 95 Language Manual.
6977
6978 @item
6979 Generates a main program incorporating the given elaboration order.
6980 This program is a small Ada package (body and spec) that
6981 must be subsequently compiled
6982 using the GNAT compiler. The necessary compilation step is usually
6983 performed automatically by @command{gnatlink}. The two most important
6984 functions of this program
6985 are to call the elaboration routines of units in an appropriate order
6986 and to call the main program.
6987
6988 @item
6989 Determines the set of object files required by the given main program.
6990 This information is output in the forms of comments in the generated program,
6991 to be read by the @command{gnatlink} utility used to link the Ada application.
6992 @end enumerate
6993
6994 @node Running gnatbind
6995 @section Running @code{gnatbind}
6996
6997 @noindent
6998 The form of the @code{gnatbind} command is
6999
7000 @smallexample
7001 $ gnatbind [@i{switches}] @i{mainprog}[.ali] [@i{switches}]
7002 @end smallexample
7003
7004 @noindent
7005 where @file{@i{mainprog}.adb} is the Ada file containing the main program
7006 unit body. If no switches are specified, @code{gnatbind} constructs an Ada
7007 package in two files whose names are
7008 @file{b~@i{mainprog}.ads}, and @file{b~@i{mainprog}.adb}.
7009 For example, if given the
7010 parameter @file{hello.ali}, for a main program contained in file
7011 @file{hello.adb}, the binder output files would be @file{b~hello.ads}
7012 and @file{b~hello.adb}.
7013
7014 When doing consistency checking, the binder takes into consideration
7015 any source files it can locate. For example, if the binder determines
7016 that the given main program requires the package @code{Pack}, whose
7017 @file{.ALI}
7018 file is @file{pack.ali} and whose corresponding source spec file is
7019 @file{pack.ads}, it attempts to locate the source file @file{pack.ads}
7020 (using the same search path conventions as previously described for the
7021 @command{gcc} command). If it can locate this source file, it checks that
7022 the time stamps
7023 or source checksums of the source and its references to in @file{ALI} files
7024 match. In other words, any @file{ALI} files that mentions this spec must have
7025 resulted from compiling this version of the source file (or in the case
7026 where the source checksums match, a version close enough that the
7027 difference does not matter).
7028
7029 @cindex Source files, use by binder
7030 The effect of this consistency checking, which includes source files, is
7031 that the binder ensures that the program is consistent with the latest
7032 version of the source files that can be located at bind time. Editing a
7033 source file without compiling files that depend on the source file cause
7034 error messages to be generated by the binder.
7035
7036 For example, suppose you have a main program @file{hello.adb} and a
7037 package @code{P}, from file @file{p.ads} and you perform the following
7038 steps:
7039
7040 @enumerate
7041 @item
7042 Enter @code{gcc -c hello.adb} to compile the main program.
7043
7044 @item
7045 Enter @code{gcc -c p.ads} to compile package @code{P}.
7046
7047 @item
7048 Edit file @file{p.ads}.
7049
7050 @item
7051 Enter @code{gnatbind hello}.
7052 @end enumerate
7053
7054 @noindent
7055 At this point, the file @file{p.ali} contains an out-of-date time stamp
7056 because the file @file{p.ads} has been edited. The attempt at binding
7057 fails, and the binder generates the following error messages:
7058
7059 @smallexample
7060 error: "hello.adb" must be recompiled ("p.ads" has been modified)
7061 error: "p.ads" has been modified and must be recompiled
7062 @end smallexample
7063
7064 @noindent
7065 Now both files must be recompiled as indicated, and then the bind can
7066 succeed, generating a main program. You need not normally be concerned
7067 with the contents of this file, but for reference purposes a sample
7068 binder output file is given in @ref{Example of Binder Output File}.
7069
7070 In most normal usage, the default mode of @command{gnatbind} which is to
7071 generate the main package in Ada, as described in the previous section.
7072 In particular, this means that any Ada programmer can read and understand
7073 the generated main program. It can also be debugged just like any other
7074 Ada code provided the @option{^-g^/DEBUG^} switch is used for
7075 @command{gnatbind} and @command{gnatlink}.
7076
7077 However for some purposes it may be convenient to generate the main
7078 program in C rather than Ada. This may for example be helpful when you
7079 are generating a mixed language program with the main program in C. The
7080 GNAT compiler itself is an example.
7081 The use of the @option{^-C^/BIND_FILE=C^} switch
7082 for both @code{gnatbind} and @command{gnatlink} will cause the program to
7083 be generated in C (and compiled using the gnu C compiler).
7084
7085 @node Switches for gnatbind
7086 @section Switches for @command{gnatbind}
7087
7088 @noindent
7089 The following switches are available with @code{gnatbind}; details will
7090 be presented in subsequent sections.
7091
7092 @menu
7093 * Consistency-Checking Modes::
7094 * Binder Error Message Control::
7095 * Elaboration Control::
7096 * Output Control::
7097 * Binding with Non-Ada Main Programs::
7098 * Binding Programs with No Main Subprogram::
7099 @end menu
7100
7101 @table @option
7102 @c !sort!
7103 @item ^-aO^/OBJECT_SEARCH^
7104 @cindex @option{^-aO^/OBJECT_SEARCH^} (@command{gnatbind})
7105 Specify directory to be searched for ALI files.
7106
7107 @item ^-aI^/SOURCE_SEARCH^
7108 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatbind})
7109 Specify directory to be searched for source file.
7110
7111 @item ^-A^/BIND_FILE=ADA^
7112 @cindex @option{^-A^/BIND_FILE=ADA^} (@command{gnatbind})
7113 Generate binder program in Ada (default)
7114
7115 @item ^-b^/REPORT_ERRORS=BRIEF^
7116 @cindex @option{^-b^/REPORT_ERRORS=BRIEF^} (@command{gnatbind})
7117 Generate brief messages to @file{stderr} even if verbose mode set.
7118
7119 @item ^-c^/NOOUTPUT^
7120 @cindex @option{^-c^/NOOUTPUT^} (@command{gnatbind})
7121 Check only, no generation of binder output file.
7122
7123 @item ^-C^/BIND_FILE=C^
7124 @cindex @option{^-C^/BIND_FILE=C^} (@command{gnatbind})
7125 Generate binder program in C
7126
7127 @item ^-e^/ELABORATION_DEPENDENCIES^
7128 @cindex @option{^-e^/ELABORATION_DEPENDENCIES^} (@command{gnatbind})
7129 Output complete list of elaboration-order dependencies.
7130
7131 @item ^-E^/STORE_TRACEBACKS^
7132 @cindex @option{^-E^/STORE_TRACEBACKS^} (@command{gnatbind})
7133 Store tracebacks in exception occurrences when the target supports it.
7134 This is the default with the zero cost exception mechanism.
7135 @ignore
7136 @c The following may get moved to an appendix
7137 This option is currently supported on the following targets:
7138 all x86 ports, Solaris, Windows, HP-UX, AIX, PowerPC VxWorks and Alpha VxWorks.
7139 @end ignore
7140 See also the packages @code{GNAT.Traceback} and
7141 @code{GNAT.Traceback.Symbolic} for more information.
7142 @ifclear vms
7143 Note that on x86 ports, you must not use @option{-fomit-frame-pointer}
7144 @command{gcc} option.
7145 @end ifclear
7146
7147 @item ^-F^/FORCE_ELABS_FLAGS^
7148 @cindex @option{^-F^/FORCE_ELABS_FLAGS^} (@command{gnatbind})
7149 Force the checks of elaboration flags. @command{gnatbind} does not normally
7150 generate checks of elaboration flags for the main executable, except when
7151 a Stand-Alone Library is used. However, there are cases when this cannot be
7152 detected by gnatbind. An example is importing an interface of a Stand-Alone
7153 Library through a pragma Import and only specifying through a linker switch
7154 this Stand-Alone Library. This switch is used to guarantee that elaboration
7155 flag checks are generated.
7156
7157 @item ^-h^/HELP^
7158 @cindex @option{^-h^/HELP^} (@command{gnatbind})
7159 Output usage (help) information
7160
7161 @item ^-I^/SEARCH^
7162 @cindex @option{^-I^/SEARCH^} (@command{gnatbind})
7163 Specify directory to be searched for source and ALI files.
7164
7165 @item ^-I-^/NOCURRENT_DIRECTORY^
7166 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gnatbind})
7167 Do not look for sources in the current directory where @code{gnatbind} was
7168 invoked, and do not look for ALI files in the directory containing the
7169 ALI file named in the @code{gnatbind} command line.
7170
7171 @item ^-l^/ORDER_OF_ELABORATION^
7172 @cindex @option{^-l^/ORDER_OF_ELABORATION^} (@command{gnatbind})
7173 Output chosen elaboration order.
7174
7175 @item ^-Lxxx^/BUILD_LIBRARY=xxx^
7176 @cindex @option{^-L^/BUILD_LIBRARY^} (@command{gnatbind})
7177 Bind the units for library building. In this case the adainit and
7178 adafinal procedures (@pxref{Binding with Non-Ada Main Programs})
7179 are renamed to ^xxxinit^XXXINIT^ and
7180 ^xxxfinal^XXXFINAL^.
7181 Implies ^-n^/NOCOMPILE^.
7182 @ifclear vms
7183 (@xref{GNAT and Libraries}, for more details.)
7184 @end ifclear
7185 @ifset vms
7186 On OpenVMS, these init and final procedures are exported in uppercase
7187 letters. For example if /BUILD_LIBRARY=toto is used, the exported name of
7188 the init procedure will be "TOTOINIT" and the exported name of the final
7189 procedure will be "TOTOFINAL".
7190 @end ifset
7191
7192 @item ^-Mxyz^/RENAME_MAIN=xyz^
7193 @cindex @option{^-M^/RENAME_MAIN^} (@command{gnatbind})
7194 Rename generated main program from main to xyz
7195
7196 @item ^-m^/ERROR_LIMIT=^@var{n}
7197 @cindex @option{^-m^/ERROR_LIMIT^} (@command{gnatbind})
7198 Limit number of detected errors to @var{n}, where @var{n} is
7199 in the range 1..999_999. The default value if no switch is
7200 given is 9999. Binding is terminated if the limit is exceeded.
7201 @ifset unw
7202 Furthermore, under Windows, the sources pointed to by the libraries path
7203 set in the registry are not searched for.
7204 @end ifset
7205
7206 @item ^-n^/NOMAIN^
7207 @cindex @option{^-n^/NOMAIN^} (@command{gnatbind})
7208 No main program.
7209
7210 @item -nostdinc
7211 @cindex @option{-nostdinc} (@command{gnatbind})
7212 Do not look for sources in the system default directory.
7213
7214 @item -nostdlib
7215 @cindex @option{-nostdlib} (@command{gnatbind})
7216 Do not look for library files in the system default directory.
7217
7218 @item --RTS=@var{rts-path}
7219 @cindex @option{--RTS} (@code{gnatbind})
7220 Specifies the default location of the runtime library. Same meaning as the
7221 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
7222
7223 @item ^-o ^/OUTPUT=^@var{file}
7224 @cindex @option{^-o ^/OUTPUT^} (@command{gnatbind})
7225 Name the output file @var{file} (default is @file{b~@var{xxx}.adb}).
7226 Note that if this option is used, then linking must be done manually,
7227 gnatlink cannot be used.
7228
7229 @item ^-O^/OBJECT_LIST^
7230 @cindex @option{^-O^/OBJECT_LIST^} (@command{gnatbind})
7231 Output object list.
7232
7233 @item ^-p^/PESSIMISTIC_ELABORATION^
7234 @cindex @option{^-p^/PESSIMISTIC_ELABORATION^} (@command{gnatbind})
7235 Pessimistic (worst-case) elaboration order
7236
7237 @item ^-s^/READ_SOURCES=ALL^
7238 @cindex @option{^-s^/READ_SOURCES=ALL^} (@command{gnatbind})
7239 Require all source files to be present.
7240
7241 @item ^-S@var{xxx}^/INITIALIZE_SCALARS=@var{xxx}^
7242 @cindex @option{^-S^/INITIALIZE_SCALARS^} (@command{gnatbind})
7243 Specifies the value to be used when detecting uninitialized scalar
7244 objects with pragma Initialize_Scalars.
7245 The @var{xxx} ^string specified with the switch^option^ may be either
7246 @itemize @bullet
7247 @item ``@option{^in^INVALID^}'' requesting an invalid value where possible
7248 @item ``@option{^lo^LOW^}'' for the lowest possible value
7249 @item ``@option{^hi^HIGH^}'' for the highest possible value
7250 @item ``@option{xx}'' for a value consisting of repeated bytes with the
7251 value 16#xx# (i.e. xx is a string of two hexadecimal digits).
7252 @end itemize
7253
7254 In addition, you can specify @option{-Sev} to indicate that the value is
7255 to be set at run time. In this case, the program will look for an environment
7256 @cindex GNAT_INIT_SCALARS
7257 variable of the form @code{GNAT_INIT_SCALARS=xx}, where xx is one
7258 of @option{in/lo/hi/xx} with the same meanings as above.
7259 If no environment variable is found, or if it does not have a valid value,
7260 then the default is @option{in} (invalid values).
7261
7262 @ifclear vms
7263 @item -static
7264 @cindex @option{-static} (@code{gnatbind})
7265 Link against a static GNAT run time.
7266
7267 @item -shared
7268 @cindex @option{-shared} (@code{gnatbind})
7269 Link against a shared GNAT run time when available.
7270 @end ifclear
7271
7272 @item ^-t^/NOTIME_STAMP_CHECK^
7273 @cindex @option{^-t^/NOTIME_STAMP_CHECK^} (@code{gnatbind})
7274 Tolerate time stamp and other consistency errors
7275
7276 @item ^-T@var{n}^/TIME_SLICE=@var{n}^
7277 @cindex @option{^-T^/TIME_SLICE^} (@code{gnatbind})
7278 Set the time slice value to @var{n} milliseconds. If the system supports
7279 the specification of a specific time slice value, then the indicated value
7280 is used. If the system does not support specific time slice values, but
7281 does support some general notion of round-robin scheduling, then any
7282 non-zero value will activate round-robin scheduling.
7283
7284 A value of zero is treated specially. It turns off time
7285 slicing, and in addition, indicates to the tasking run time that the
7286 semantics should match as closely as possible the Annex D
7287 requirements of the Ada RM, and in particular sets the default
7288 scheduling policy to @code{FIFO_Within_Priorities}.
7289
7290 @item ^-v^/REPORT_ERRORS=VERBOSE^
7291 @cindex @option{^-v^/REPORT_ERRORS=VERBOSE^} (@code{gnatbind})
7292 Verbose mode. Write error messages, header, summary output to
7293 @file{stdout}.
7294
7295 @ifclear vms
7296 @item -w@var{x}
7297 @cindex @option{-w} (@code{gnatbind})
7298 Warning mode (@var{x}=s/e for suppress/treat as error)
7299 @end ifclear
7300
7301 @ifset vms
7302 @item /WARNINGS=NORMAL
7303 @cindex @option{/WARNINGS} (@code{gnatbind})
7304 Normal warnings mode. Warnings are issued but ignored
7305
7306 @item /WARNINGS=SUPPRESS
7307 @cindex @option{/WARNINGS} (@code{gnatbind})
7308 All warning messages are suppressed
7309
7310 @item /WARNINGS=ERROR
7311 @cindex @option{/WARNINGS} (@code{gnatbind})
7312 Warning messages are treated as fatal errors
7313 @end ifset
7314
7315 @item ^-x^/READ_SOURCES=NONE^
7316 @cindex @option{^-x^/READ_SOURCES^} (@code{gnatbind})
7317 Exclude source files (check object consistency only).
7318
7319 @ifset vms
7320 @item /READ_SOURCES=AVAILABLE
7321 @cindex @option{/READ_SOURCES} (@code{gnatbind})
7322 Default mode, in which sources are checked for consistency only if
7323 they are available.
7324 @end ifset
7325
7326 @item ^-z^/ZERO_MAIN^
7327 @cindex @option{^-z^/ZERO_MAIN^} (@code{gnatbind})
7328 No main subprogram.
7329 @end table
7330
7331 @ifclear vms
7332 @noindent
7333 You may obtain this listing of switches by running @code{gnatbind} with
7334 no arguments.
7335 @end ifclear
7336
7337 @node Consistency-Checking Modes
7338 @subsection Consistency-Checking Modes
7339
7340 @noindent
7341 As described earlier, by default @code{gnatbind} checks
7342 that object files are consistent with one another and are consistent
7343 with any source files it can locate. The following switches control binder
7344 access to sources.
7345
7346 @table @option
7347 @c !sort!
7348 @item ^-s^/READ_SOURCES=ALL^
7349 @cindex @option{^-s^/READ_SOURCES=ALL^} (@code{gnatbind})
7350 Require source files to be present. In this mode, the binder must be
7351 able to locate all source files that are referenced, in order to check
7352 their consistency. In normal mode, if a source file cannot be located it
7353 is simply ignored. If you specify this switch, a missing source
7354 file is an error.
7355
7356 @item ^-x^/READ_SOURCES=NONE^
7357 @cindex @option{^-x^/READ_SOURCES=NONE^} (@code{gnatbind})
7358 Exclude source files. In this mode, the binder only checks that ALI
7359 files are consistent with one another. Source files are not accessed.
7360 The binder runs faster in this mode, and there is still a guarantee that
7361 the resulting program is self-consistent.
7362 If a source file has been edited since it was last compiled, and you
7363 specify this switch, the binder will not detect that the object
7364 file is out of date with respect to the source file. Note that this is the
7365 mode that is automatically used by @command{gnatmake} because in this
7366 case the checking against sources has already been performed by
7367 @command{gnatmake} in the course of compilation (i.e. before binding).
7368
7369 @ifset vms
7370 @item /READ_SOURCES=AVAILABLE
7371 @cindex @code{/READ_SOURCES=AVAILABLE} (@code{gnatbind})
7372 This is the default mode in which source files are checked if they are
7373 available, and ignored if they are not available.
7374 @end ifset
7375 @end table
7376
7377 @node Binder Error Message Control
7378 @subsection Binder Error Message Control
7379
7380 @noindent
7381 The following switches provide control over the generation of error
7382 messages from the binder:
7383
7384 @table @option
7385 @c !sort!
7386 @item ^-v^/REPORT_ERRORS=VERBOSE^
7387 @cindex @option{^-v^/REPORT_ERRORS=VERBOSE^} (@code{gnatbind})
7388 Verbose mode. In the normal mode, brief error messages are generated to
7389 @file{stderr}. If this switch is present, a header is written
7390 to @file{stdout} and any error messages are directed to @file{stdout}.
7391 All that is written to @file{stderr} is a brief summary message.
7392
7393 @item ^-b^/REPORT_ERRORS=BRIEF^
7394 @cindex @option{^-b^/REPORT_ERRORS=BRIEF^} (@code{gnatbind})
7395 Generate brief error messages to @file{stderr} even if verbose mode is
7396 specified. This is relevant only when used with the
7397 @option{^-v^/REPORT_ERRORS=VERBOSE^} switch.
7398
7399 @ifclear vms
7400 @item -m@var{n}
7401 @cindex @option{-m} (@code{gnatbind})
7402 Limits the number of error messages to @var{n}, a decimal integer in the
7403 range 1-999. The binder terminates immediately if this limit is reached.
7404
7405 @item -M@var{xxx}
7406 @cindex @option{-M} (@code{gnatbind})
7407 Renames the generated main program from @code{main} to @code{xxx}.
7408 This is useful in the case of some cross-building environments, where
7409 the actual main program is separate from the one generated
7410 by @code{gnatbind}.
7411 @end ifclear
7412
7413 @item ^-ws^/WARNINGS=SUPPRESS^
7414 @cindex @option{^-ws^/WARNINGS=SUPPRESS^} (@code{gnatbind})
7415 @cindex Warnings
7416 Suppress all warning messages.
7417
7418 @item ^-we^/WARNINGS=ERROR^
7419 @cindex @option{^-we^/WARNINGS=ERROR^} (@code{gnatbind})
7420 Treat any warning messages as fatal errors.
7421
7422 @ifset vms
7423 @item /WARNINGS=NORMAL
7424 Standard mode with warnings generated, but warnings do not get treated
7425 as errors.
7426 @end ifset
7427
7428 @item ^-t^/NOTIME_STAMP_CHECK^
7429 @cindex @option{^-t^/NOTIME_STAMP_CHECK^} (@code{gnatbind})
7430 @cindex Time stamp checks, in binder
7431 @cindex Binder consistency checks
7432 @cindex Consistency checks, in binder
7433 The binder performs a number of consistency checks including:
7434
7435 @itemize @bullet
7436 @item
7437 Check that time stamps of a given source unit are consistent
7438 @item
7439 Check that checksums of a given source unit are consistent
7440 @item
7441 Check that consistent versions of @code{GNAT} were used for compilation
7442 @item
7443 Check consistency of configuration pragmas as required
7444 @end itemize
7445
7446 @noindent
7447 Normally failure of such checks, in accordance with the consistency
7448 requirements of the Ada Reference Manual, causes error messages to be
7449 generated which abort the binder and prevent the output of a binder
7450 file and subsequent link to obtain an executable.
7451
7452 The @option{^-t^/NOTIME_STAMP_CHECK^} switch converts these error messages
7453 into warnings, so that
7454 binding and linking can continue to completion even in the presence of such
7455 errors. The result may be a failed link (due to missing symbols), or a
7456 non-functional executable which has undefined semantics.
7457 @emph{This means that
7458 @option{^-t^/NOTIME_STAMP_CHECK^} should be used only in unusual situations,
7459 with extreme care.}
7460 @end table
7461
7462 @node Elaboration Control
7463 @subsection Elaboration Control
7464
7465 @noindent
7466 The following switches provide additional control over the elaboration
7467 order. For full details see @ref{Elaboration Order Handling in GNAT}.
7468
7469 @table @option
7470 @item ^-p^/PESSIMISTIC_ELABORATION^
7471 @cindex @option{^-p^/PESSIMISTIC_ELABORATION^} (@code{gnatbind})
7472 Normally the binder attempts to choose an elaboration order that is
7473 likely to minimize the likelihood of an elaboration order error resulting
7474 in raising a @code{Program_Error} exception. This switch reverses the
7475 action of the binder, and requests that it deliberately choose an order
7476 that is likely to maximize the likelihood of an elaboration error.
7477 This is useful in ensuring portability and avoiding dependence on
7478 accidental fortuitous elaboration ordering.
7479
7480 Normally it only makes sense to use the @option{^-p^/PESSIMISTIC_ELABORATION^}
7481 switch if dynamic
7482 elaboration checking is used (@option{-gnatE} switch used for compilation).
7483 This is because in the default static elaboration mode, all necessary
7484 @code{Elaborate} and @code{Elaborate_All} pragmas are implicitly inserted.
7485 These implicit pragmas are still respected by the binder in
7486 @option{^-p^/PESSIMISTIC_ELABORATION^} mode, so a
7487 safe elaboration order is assured.
7488 @end table
7489
7490 @node Output Control
7491 @subsection Output Control
7492
7493 @noindent
7494 The following switches allow additional control over the output
7495 generated by the binder.
7496
7497 @table @option
7498 @c !sort!
7499
7500 @item ^-A^/BIND_FILE=ADA^
7501 @cindex @option{^-A^/BIND_FILE=ADA^} (@code{gnatbind})
7502 Generate binder program in Ada (default). The binder program is named
7503 @file{b~@var{mainprog}.adb} by default. This can be changed with
7504 @option{^-o^/OUTPUT^} @code{gnatbind} option.
7505
7506 @item ^-c^/NOOUTPUT^
7507 @cindex @option{^-c^/NOOUTPUT^} (@code{gnatbind})
7508 Check only. Do not generate the binder output file. In this mode the
7509 binder performs all error checks but does not generate an output file.
7510
7511 @item ^-C^/BIND_FILE=C^
7512 @cindex @option{^-C^/BIND_FILE=C^} (@code{gnatbind})
7513 Generate binder program in C. The binder program is named
7514 @file{b_@var{mainprog}.c}.
7515 This can be changed with @option{^-o^/OUTPUT^} @code{gnatbind}
7516 option.
7517
7518 @item ^-e^/ELABORATION_DEPENDENCIES^
7519 @cindex @option{^-e^/ELABORATION_DEPENDENCIES^} (@code{gnatbind})
7520 Output complete list of elaboration-order dependencies, showing the
7521 reason for each dependency. This output can be rather extensive but may
7522 be useful in diagnosing problems with elaboration order. The output is
7523 written to @file{stdout}.
7524
7525 @item ^-h^/HELP^
7526 @cindex @option{^-h^/HELP^} (@code{gnatbind})
7527 Output usage information. The output is written to @file{stdout}.
7528
7529 @item ^-K^/LINKER_OPTION_LIST^
7530 @cindex @option{^-K^/LINKER_OPTION_LIST^} (@code{gnatbind})
7531 Output linker options to @file{stdout}. Includes library search paths,
7532 contents of pragmas Ident and Linker_Options, and libraries added
7533 by @code{gnatbind}.
7534
7535 @item ^-l^/ORDER_OF_ELABORATION^
7536 @cindex @option{^-l^/ORDER_OF_ELABORATION^} (@code{gnatbind})
7537 Output chosen elaboration order. The output is written to @file{stdout}.
7538
7539 @item ^-O^/OBJECT_LIST^
7540 @cindex @option{^-O^/OBJECT_LIST^} (@code{gnatbind})
7541 Output full names of all the object files that must be linked to provide
7542 the Ada component of the program. The output is written to @file{stdout}.
7543 This list includes the files explicitly supplied and referenced by the user
7544 as well as implicitly referenced run-time unit files. The latter are
7545 omitted if the corresponding units reside in shared libraries. The
7546 directory names for the run-time units depend on the system configuration.
7547
7548 @item ^-o ^/OUTPUT=^@var{file}
7549 @cindex @option{^-o^/OUTPUT^} (@code{gnatbind})
7550 Set name of output file to @var{file} instead of the normal
7551 @file{b~@var{mainprog}.adb} default. Note that @var{file} denote the Ada
7552 binder generated body filename. In C mode you would normally give
7553 @var{file} an extension of @file{.c} because it will be a C source program.
7554 Note that if this option is used, then linking must be done manually.
7555 It is not possible to use gnatlink in this case, since it cannot locate
7556 the binder file.
7557
7558 @item ^-r^/RESTRICTION_LIST^
7559 @cindex @option{^-r^/RESTRICTION_LIST^} (@code{gnatbind})
7560 Generate list of @code{pragma Restrictions} that could be applied to
7561 the current unit. This is useful for code audit purposes, and also may
7562 be used to improve code generation in some cases.
7563
7564 @end table
7565
7566 @node Binding with Non-Ada Main Programs
7567 @subsection Binding with Non-Ada Main Programs
7568
7569 @noindent
7570 In our description so far we have assumed that the main
7571 program is in Ada, and that the task of the binder is to generate a
7572 corresponding function @code{main} that invokes this Ada main
7573 program. GNAT also supports the building of executable programs where
7574 the main program is not in Ada, but some of the called routines are
7575 written in Ada and compiled using GNAT (@pxref{Mixed Language Programming}).
7576 The following switch is used in this situation:
7577
7578 @table @option
7579 @item ^-n^/NOMAIN^
7580 @cindex @option{^-n^/NOMAIN^} (@code{gnatbind})
7581 No main program. The main program is not in Ada.
7582 @end table
7583
7584 @noindent
7585 In this case, most of the functions of the binder are still required,
7586 but instead of generating a main program, the binder generates a file
7587 containing the following callable routines:
7588
7589 @table @code
7590 @item adainit
7591 @findex adainit
7592 You must call this routine to initialize the Ada part of the program by
7593 calling the necessary elaboration routines. A call to @code{adainit} is
7594 required before the first call to an Ada subprogram.
7595
7596 Note that it is assumed that the basic execution environment must be setup
7597 to be appropriate for Ada execution at the point where the first Ada
7598 subprogram is called. In particular, if the Ada code will do any
7599 floating-point operations, then the FPU must be setup in an appropriate
7600 manner. For the case of the x86, for example, full precision mode is
7601 required. The procedure GNAT.Float_Control.Reset may be used to ensure
7602 that the FPU is in the right state.
7603
7604 @item adafinal
7605 @findex adafinal
7606 You must call this routine to perform any library-level finalization
7607 required by the Ada subprograms. A call to @code{adafinal} is required
7608 after the last call to an Ada subprogram, and before the program
7609 terminates.
7610 @end table
7611
7612 @noindent
7613 If the @option{^-n^/NOMAIN^} switch
7614 @cindex @option{^-n^/NOMAIN^} (@command{gnatbind})
7615 @cindex Binder, multiple input files
7616 is given, more than one ALI file may appear on
7617 the command line for @code{gnatbind}. The normal @dfn{closure}
7618 calculation is performed for each of the specified units. Calculating
7619 the closure means finding out the set of units involved by tracing
7620 @code{with} references. The reason it is necessary to be able to
7621 specify more than one ALI file is that a given program may invoke two or
7622 more quite separate groups of Ada units.
7623
7624 The binder takes the name of its output file from the last specified ALI
7625 file, unless overridden by the use of the @option{^-o file^/OUTPUT=file^}.
7626 @cindex @option{^-o^/OUTPUT^} (@command{gnatbind})
7627 The output is an Ada unit in source form that can
7628 be compiled with GNAT unless the -C switch is used in which case the
7629 output is a C source file, which must be compiled using the C compiler.
7630 This compilation occurs automatically as part of the @command{gnatlink}
7631 processing.
7632
7633 Currently the GNAT run time requires a FPU using 80 bits mode
7634 precision. Under targets where this is not the default it is required to
7635 call GNAT.Float_Control.Reset before using floating point numbers (this
7636 include float computation, float input and output) in the Ada code. A
7637 side effect is that this could be the wrong mode for the foreign code
7638 where floating point computation could be broken after this call.
7639
7640 @node Binding Programs with No Main Subprogram
7641 @subsection Binding Programs with No Main Subprogram
7642
7643 @noindent
7644 It is possible to have an Ada program which does not have a main
7645 subprogram. This program will call the elaboration routines of all the
7646 packages, then the finalization routines.
7647
7648 The following switch is used to bind programs organized in this manner:
7649
7650 @table @option
7651 @item ^-z^/ZERO_MAIN^
7652 @cindex @option{^-z^/ZERO_MAIN^} (@code{gnatbind})
7653 Normally the binder checks that the unit name given on the command line
7654 corresponds to a suitable main subprogram. When this switch is used,
7655 a list of ALI files can be given, and the execution of the program
7656 consists of elaboration of these units in an appropriate order.
7657 @end table
7658
7659 @node Command-Line Access
7660 @section Command-Line Access
7661
7662 @noindent
7663 The package @code{Ada.Command_Line} provides access to the command-line
7664 arguments and program name. In order for this interface to operate
7665 correctly, the two variables
7666
7667 @smallexample
7668 @group
7669 int gnat_argc;
7670 char **gnat_argv;
7671 @end group
7672 @end smallexample
7673
7674 @noindent
7675 @findex gnat_argv
7676 @findex gnat_argc
7677 are declared in one of the GNAT library routines. These variables must
7678 be set from the actual @code{argc} and @code{argv} values passed to the
7679 main program. With no @option{^n^/NOMAIN^} present, @code{gnatbind}
7680 generates the C main program to automatically set these variables.
7681 If the @option{^n^/NOMAIN^} switch is used, there is no automatic way to
7682 set these variables. If they are not set, the procedures in
7683 @code{Ada.Command_Line} will not be available, and any attempt to use
7684 them will raise @code{Constraint_Error}. If command line access is
7685 required, your main program must set @code{gnat_argc} and
7686 @code{gnat_argv} from the @code{argc} and @code{argv} values passed to
7687 it.
7688
7689 @node Search Paths for gnatbind
7690 @section Search Paths for @code{gnatbind}
7691
7692 @noindent
7693 The binder takes the name of an ALI file as its argument and needs to
7694 locate source files as well as other ALI files to verify object consistency.
7695
7696 For source files, it follows exactly the same search rules as @command{gcc}
7697 (@pxref{Search Paths and the Run-Time Library (RTL)}). For ALI files the
7698 directories searched are:
7699
7700 @enumerate
7701 @item
7702 The directory containing the ALI file named in the command line, unless
7703 the switch @option{^-I-^/NOCURRENT_DIRECTORY^} is specified.
7704
7705 @item
7706 All directories specified by @option{^-I^/SEARCH^}
7707 switches on the @code{gnatbind}
7708 command line, in the order given.
7709
7710 @item
7711 @findex ADA_PRJ_OBJECTS_FILE
7712 Each of the directories listed in the text file whose name is given
7713 by the @code{ADA_PRJ_OBJECTS_FILE} ^environment variable^logical name^.
7714
7715 @noindent
7716 @code{ADA_PRJ_OBJECTS_FILE} is normally set by gnatmake or by the ^gnat^GNAT^
7717 driver when project files are used. It should not normally be set
7718 by other means.
7719
7720 @item
7721 @findex ADA_OBJECTS_PATH
7722 Each of the directories listed in the value of the
7723 @code{ADA_OBJECTS_PATH} ^environment variable^logical name^.
7724 @ifset unw
7725 Construct this value
7726 exactly as the @code{PATH} environment variable: a list of directory
7727 names separated by colons (semicolons when working with the NT version
7728 of GNAT).
7729 @end ifset
7730 @ifset vms
7731 Normally, define this value as a logical name containing a comma separated
7732 list of directory names.
7733
7734 This variable can also be defined by means of an environment string
7735 (an argument to the HP C exec* set of functions).
7736
7737 Logical Name:
7738 @smallexample
7739 DEFINE ANOTHER_PATH FOO:[BAG]
7740 DEFINE ADA_OBJECTS_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
7741 @end smallexample
7742
7743 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
7744 first, followed by the standard Ada 95
7745 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADALIB].
7746 If this is not redefined, the user will obtain the HP Ada 83 IO packages
7747 (Text_IO, Sequential_IO, etc)
7748 instead of the Ada95 packages. Thus, in order to get the Ada 95
7749 packages by default, ADA_OBJECTS_PATH must be redefined.
7750 @end ifset
7751
7752 @item
7753 The content of the @file{ada_object_path} file which is part of the GNAT
7754 installation tree and is used to store standard libraries such as the
7755 GNAT Run Time Library (RTL) unless the switch @option{-nostdlib} is
7756 specified.
7757 @ifclear vms
7758 @ref{Installing a library}
7759 @end ifclear
7760 @end enumerate
7761
7762 @noindent
7763 In the binder the switch @option{^-I^/SEARCH^}
7764 @cindex @option{^-I^/SEARCH^} (@command{gnatbind})
7765 is used to specify both source and
7766 library file paths. Use @option{^-aI^/SOURCE_SEARCH^}
7767 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatbind})
7768 instead if you want to specify
7769 source paths only, and @option{^-aO^/LIBRARY_SEARCH^}
7770 @cindex @option{^-aO^/LIBRARY_SEARCH^} (@command{gnatbind})
7771 if you want to specify library paths
7772 only. This means that for the binder
7773 @option{^-I^/SEARCH=^}@var{dir} is equivalent to
7774 @option{^-aI^/SOURCE_SEARCH=^}@var{dir}
7775 @option{^-aO^/OBJECT_SEARCH=^}@var{dir}.
7776 The binder generates the bind file (a C language source file) in the
7777 current working directory.
7778
7779 @findex Ada
7780 @findex System
7781 @findex Interfaces
7782 @findex GNAT
7783 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
7784 children make up the GNAT Run-Time Library, together with the package
7785 GNAT and its children, which contain a set of useful additional
7786 library functions provided by GNAT. The sources for these units are
7787 needed by the compiler and are kept together in one directory. The ALI
7788 files and object files generated by compiling the RTL are needed by the
7789 binder and the linker and are kept together in one directory, typically
7790 different from the directory containing the sources. In a normal
7791 installation, you need not specify these directory names when compiling
7792 or binding. Either the environment variables or the built-in defaults
7793 cause these files to be found.
7794
7795 Besides simplifying access to the RTL, a major use of search paths is
7796 in compiling sources from multiple directories. This can make
7797 development environments much more flexible.
7798
7799 @node Examples of gnatbind Usage
7800 @section Examples of @code{gnatbind} Usage
7801
7802 @noindent
7803 This section contains a number of examples of using the GNAT binding
7804 utility @code{gnatbind}.
7805
7806 @table @code
7807 @item gnatbind hello
7808 The main program @code{Hello} (source program in @file{hello.adb}) is
7809 bound using the standard switch settings. The generated main program is
7810 @file{b~hello.adb}. This is the normal, default use of the binder.
7811
7812 @ifclear vms
7813 @item gnatbind hello -o mainprog.adb
7814 @end ifclear
7815 @ifset vms
7816 @item gnatbind HELLO.ALI /OUTPUT=Mainprog.ADB
7817 @end ifset
7818 The main program @code{Hello} (source program in @file{hello.adb}) is
7819 bound using the standard switch settings. The generated main program is
7820 @file{mainprog.adb} with the associated spec in
7821 @file{mainprog.ads}. Note that you must specify the body here not the
7822 spec, in the case where the output is in Ada. Note that if this option
7823 is used, then linking must be done manually, since gnatlink will not
7824 be able to find the generated file.
7825
7826 @ifclear vms
7827 @item gnatbind main -C -o mainprog.c -x
7828 @end ifclear
7829 @ifset vms
7830 @item gnatbind MAIN.ALI /BIND_FILE=C /OUTPUT=Mainprog.C /READ_SOURCES=NONE
7831 @end ifset
7832 The main program @code{Main} (source program in
7833 @file{main.adb}) is bound, excluding source files from the
7834 consistency checking, generating
7835 the file @file{mainprog.c}.
7836
7837 @ifclear vms
7838 @item gnatbind -x main_program -C -o mainprog.c
7839 This command is exactly the same as the previous example. Switches may
7840 appear anywhere in the command line, and single letter switches may be
7841 combined into a single switch.
7842 @end ifclear
7843
7844 @ifclear vms
7845 @item gnatbind -n math dbase -C -o ada-control.c
7846 @end ifclear
7847 @ifset vms
7848 @item gnatbind /NOMAIN math dbase /BIND_FILE=C /OUTPUT=ada-control.c
7849 @end ifset
7850 The main program is in a language other than Ada, but calls to
7851 subprograms in packages @code{Math} and @code{Dbase} appear. This call
7852 to @code{gnatbind} generates the file @file{ada-control.c} containing
7853 the @code{adainit} and @code{adafinal} routines to be called before and
7854 after accessing the Ada units.
7855 @end table
7856
7857 @c ------------------------------------
7858 @node Linking Using gnatlink
7859 @chapter Linking Using @command{gnatlink}
7860 @c ------------------------------------
7861 @findex gnatlink
7862
7863 @noindent
7864 This chapter discusses @command{gnatlink}, a tool that links
7865 an Ada program and builds an executable file. This utility
7866 invokes the system linker ^(via the @command{gcc} command)^^
7867 with a correct list of object files and library references.
7868 @command{gnatlink} automatically determines the list of files and
7869 references for the Ada part of a program. It uses the binder file
7870 generated by the @command{gnatbind} to determine this list.
7871
7872 @menu
7873 * Running gnatlink::
7874 * Switches for gnatlink::
7875 * Setting Stack Size from gnatlink::
7876 * Setting Heap Size from gnatlink::
7877 @end menu
7878
7879 @node Running gnatlink
7880 @section Running @command{gnatlink}
7881
7882 @noindent
7883 The form of the @command{gnatlink} command is
7884
7885 @smallexample
7886 $ gnatlink [@var{switches}] @var{mainprog}[.ali]
7887            [@var{non-Ada objects}] [@var{linker options}]
7888 @end smallexample
7889
7890 @noindent
7891 The arguments of @command{gnatlink} (switches, main @file{ALI} file,
7892 non-Ada objects
7893 or linker options) may be in any order, provided that no non-Ada object may
7894 be mistaken for a main @file{ALI} file.
7895 Any file name @file{F} without the @file{.ali}
7896 extension will be taken as the main @file{ALI} file if a file exists
7897 whose name is the concatenation of @file{F} and @file{.ali}.
7898
7899 @noindent
7900 @file{@var{mainprog}.ali} references the ALI file of the main program.
7901 The @file{.ali} extension of this file can be omitted. From this
7902 reference, @command{gnatlink} locates the corresponding binder file
7903 @file{b~@var{mainprog}.adb} and, using the information in this file along
7904 with the list of non-Ada objects and linker options, constructs a
7905 linker command file to create the executable.
7906
7907 The arguments other than the @command{gnatlink} switches and the main
7908 @file{ALI} file are passed to the linker uninterpreted.
7909 They typically include the names of
7910 object files for units written in other languages than Ada and any library
7911 references required to resolve references in any of these foreign language
7912 units, or in @code{Import} pragmas in any Ada units.
7913
7914 @var{linker options} is an optional list of linker specific
7915 switches.
7916 The default linker called by gnatlink is @var{gcc} which in
7917 turn calls the appropriate system linker.
7918 Standard options for the linker such as @option{-lmy_lib} or
7919 @option{-Ldir} can be added as is.
7920 For options that are not recognized by
7921 @var{gcc} as linker options, use the @var{gcc} switches @option{-Xlinker} or
7922 @option{-Wl,}.
7923 Refer to the GCC documentation for
7924 details. Here is an example showing how to generate a linker map:
7925
7926 @ifclear vms
7927 @smallexample
7928 $ gnatlink my_prog -Wl,-Map,MAPFILE
7929 @end smallexample
7930 @end ifclear
7931
7932 @ifset vms
7933 <<Need example for VMS>>
7934 @end ifset
7935
7936 Using @var{linker options} it is possible to set the program stack and
7937 heap size. See @ref{Setting Stack Size from gnatlink} and
7938 @ref{Setting Heap Size from gnatlink}.
7939
7940 @command{gnatlink} determines the list of objects required by the Ada
7941 program and prepends them to the list of objects passed to the linker.
7942 @command{gnatlink} also gathers any arguments set by the use of
7943 @code{pragma Linker_Options} and adds them to the list of arguments
7944 presented to the linker.
7945
7946 @ifset vms
7947 @command{gnatlink} accepts the following types of extra files on the command
7948 line: objects (.OBJ), libraries (.OLB), sharable images (.EXE), and
7949 options files (.OPT). These are recognized and handled according to their
7950 extension.
7951 @end ifset
7952
7953 @node Switches for gnatlink
7954 @section Switches for @command{gnatlink}
7955
7956 @noindent
7957 The following switches are available with the @command{gnatlink} utility:
7958
7959 @table @option
7960 @c !sort!
7961
7962 @item ^-A^/BIND_FILE=ADA^
7963 @cindex @option{^-A^/BIND_FILE=ADA^} (@command{gnatlink})
7964 The binder has generated code in Ada. This is the default.
7965
7966 @item ^-C^/BIND_FILE=C^
7967 @cindex @option{^-C^/BIND_FILE=C^} (@command{gnatlink})
7968 If instead of generating a file in Ada, the binder has generated one in
7969 C, then the linker needs to know about it. Use this switch to signal
7970 to @command{gnatlink} that the binder has generated C code rather than
7971 Ada code.
7972
7973 @item ^-f^/FORCE_OBJECT_FILE_LIST^
7974 @cindex Command line length
7975 @cindex @option{^-f^/FORCE_OBJECT_FILE_LIST^} (@command{gnatlink})
7976 On some targets, the command line length is limited, and @command{gnatlink}
7977 will generate a separate file for the linker if the list of object files
7978 is too long.
7979 The @option{^-f^/FORCE_OBJECT_FILE_LIST^} switch forces this file
7980 to be generated even if
7981 the limit is not exceeded. This is useful in some cases to deal with
7982 special situations where the command line length is exceeded.
7983
7984 @item ^-g^/DEBUG^
7985 @cindex Debugging information, including
7986 @cindex @option{^-g^/DEBUG^} (@command{gnatlink})
7987 The option to include debugging information causes the Ada bind file (in
7988 other words, @file{b~@var{mainprog}.adb}) to be compiled with
7989 @option{^-g^/DEBUG^}.
7990 In addition, the binder does not delete the @file{b~@var{mainprog}.adb},
7991 @file{b~@var{mainprog}.o} and @file{b~@var{mainprog}.ali} files.
7992 Without @option{^-g^/DEBUG^}, the binder removes these files by
7993 default. The same procedure apply if a C bind file was generated using
7994 @option{^-C^/BIND_FILE=C^} @code{gnatbind} option, in this case the filenames
7995 are @file{b_@var{mainprog}.c} and @file{b_@var{mainprog}.o}.
7996
7997 @item ^-n^/NOCOMPILE^
7998 @cindex @option{^-n^/NOCOMPILE^} (@command{gnatlink})
7999 Do not compile the file generated by the binder. This may be used when
8000 a link is rerun with different options, but there is no need to recompile
8001 the binder file.
8002
8003 @item ^-v^/VERBOSE^
8004 @cindex @option{^-v^/VERBOSE^} (@command{gnatlink})
8005 Causes additional information to be output, including a full list of the
8006 included object files. This switch option is most useful when you want
8007 to see what set of object files are being used in the link step.
8008
8009 @item ^-v -v^/VERBOSE/VERBOSE^
8010 @cindex @option{^-v -v^/VERBOSE/VERBOSE^} (@command{gnatlink})
8011 Very verbose mode. Requests that the compiler operate in verbose mode when
8012 it compiles the binder file, and that the system linker run in verbose mode.
8013
8014 @item ^-o ^/EXECUTABLE=^@var{exec-name}
8015 @cindex @option{^-o^/EXECUTABLE^} (@command{gnatlink})
8016 @var{exec-name} specifies an alternate name for the generated
8017 executable program. If this switch is omitted, the executable has the same
8018 name as the main unit. For example, @code{gnatlink try.ali} creates
8019 an executable called @file{^try^TRY.EXE^}.
8020
8021 @ifclear vms
8022 @item -b @var{target}
8023 @cindex @option{-b} (@command{gnatlink})
8024 Compile your program to run on @var{target}, which is the name of a
8025 system configuration. You must have a GNAT cross-compiler built if
8026 @var{target} is not the same as your host system.
8027
8028 @item -B@var{dir}
8029 @cindex @option{-B} (@command{gnatlink})
8030 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
8031 from @var{dir} instead of the default location. Only use this switch
8032 when multiple versions of the GNAT compiler are available. See the
8033 @command{gcc} manual page for further details. You would normally use the
8034 @option{-b} or @option{-V} switch instead.
8035
8036 @item --GCC=@var{compiler_name}
8037 @cindex @option{--GCC=compiler_name} (@command{gnatlink})
8038 Program used for compiling the binder file. The default is
8039 @command{gcc}. You need to use quotes around @var{compiler_name} if
8040 @code{compiler_name} contains spaces or other separator characters.
8041 As an example @option{--GCC="foo -x -y"} will instruct @command{gnatlink} to
8042 use @code{foo -x -y} as your compiler. Note that switch @option{-c} is always
8043 inserted after your command name. Thus in the above example the compiler
8044 command that will be used by @command{gnatlink} will be @code{foo -c -x -y}.
8045 A limitation of this syntax is that the name and path name of the executable
8046 itself must not include any embedded spaces. If several
8047 @option{--GCC=compiler_name} are used, only the last @var{compiler_name}
8048 is taken into account. However, all the additional switches are also taken
8049 into account. Thus,
8050 @option{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
8051 @option{--GCC="bar -x -y -z -t"}.
8052
8053 @item --LINK=@var{name}
8054 @cindex @option{--LINK=} (@command{gnatlink})
8055 @var{name} is the name of the linker to be invoked. This is especially
8056 useful in mixed language programs since languages such as C++ require
8057 their own linker to be used. When this switch is omitted, the default
8058 name for the linker is @command{gcc}. When this switch is used, the
8059 specified linker is called instead of @command{gcc} with exactly the same
8060 parameters that would have been passed to @command{gcc} so if the desired
8061 linker requires different parameters it is necessary to use a wrapper
8062 script that massages the parameters before invoking the real linker. It
8063 may be useful to control the exact invocation by using the verbose
8064 switch.
8065
8066 @end ifclear
8067
8068 @ifset vms
8069 @item /DEBUG=TRACEBACK
8070 @cindex @code{/DEBUG=TRACEBACK} (@command{gnatlink})
8071 This qualifier causes sufficient information to be included in the
8072 executable file to allow a traceback, but does not include the full
8073 symbol information needed by the debugger.
8074
8075 @item /IDENTIFICATION="<string>"
8076 @code{"<string>"} specifies the string to be stored in the image file
8077 identification field in the image header.
8078 It overrides any pragma @code{Ident} specified string.
8079
8080 @item /NOINHIBIT-EXEC
8081 Generate the executable file even if there are linker warnings.
8082
8083 @item /NOSTART_FILES
8084 Don't link in the object file containing the ``main'' transfer address.
8085 Used when linking with a foreign language main program compiled with an
8086 HP compiler.
8087
8088 @item /STATIC
8089 Prefer linking with object libraries over sharable images, even without
8090 /DEBUG.
8091 @end ifset
8092
8093 @end table
8094
8095 @node Setting Stack Size from gnatlink
8096 @section Setting Stack Size from @command{gnatlink}
8097
8098 @noindent
8099 Under Windows systems, it is possible to specify the program stack size from
8100 @command{gnatlink} using either:
8101
8102 @itemize @bullet
8103
8104 @item using @option{-Xlinker} linker option
8105
8106 @smallexample
8107 $ gnatlink hello -Xlinker --stack=0x10000,0x1000
8108 @end smallexample
8109
8110 This sets the stack reserve size to 0x10000 bytes and the stack commit
8111 size to 0x1000 bytes.
8112
8113 @item using @option{-Wl} linker option
8114
8115 @smallexample
8116 $ gnatlink hello -Wl,--stack=0x1000000
8117 @end smallexample
8118
8119 This sets the stack reserve size to 0x1000000 bytes. Note that with
8120 @option{-Wl} option it is not possible to set the stack commit size
8121 because the coma is a separator for this option.
8122
8123 @end itemize
8124
8125 @node Setting Heap Size from gnatlink
8126 @section Setting Heap Size from @command{gnatlink}
8127
8128 @noindent
8129 Under Windows systems, it is possible to specify the program heap size from
8130 @command{gnatlink} using either:
8131
8132 @itemize @bullet
8133
8134 @item using @option{-Xlinker} linker option
8135
8136 @smallexample
8137 $ gnatlink hello -Xlinker --heap=0x10000,0x1000
8138 @end smallexample
8139
8140 This sets the heap reserve size to 0x10000 bytes and the heap commit
8141 size to 0x1000 bytes.
8142
8143 @item using @option{-Wl} linker option
8144
8145 @smallexample
8146 $ gnatlink hello -Wl,--heap=0x1000000
8147 @end smallexample
8148
8149 This sets the heap reserve size to 0x1000000 bytes. Note that with
8150 @option{-Wl} option it is not possible to set the heap commit size
8151 because the coma is a separator for this option.
8152
8153 @end itemize
8154
8155 @node The GNAT Make Program gnatmake
8156 @chapter The GNAT Make Program @command{gnatmake}
8157 @findex gnatmake
8158
8159 @menu
8160 * Running gnatmake::
8161 * Switches for gnatmake::
8162 * Mode Switches for gnatmake::
8163 * Notes on the Command Line::
8164 * How gnatmake Works::
8165 * Examples of gnatmake Usage::
8166 @end menu
8167 @noindent
8168 A typical development cycle when working on an Ada program consists of
8169 the following steps:
8170
8171 @enumerate
8172 @item
8173 Edit some sources to fix bugs.
8174
8175 @item
8176 Add enhancements.
8177
8178 @item
8179 Compile all sources affected.
8180
8181 @item
8182 Rebind and relink.
8183
8184 @item
8185 Test.
8186 @end enumerate
8187
8188 @noindent
8189 The third step can be tricky, because not only do the modified files
8190 @cindex Dependency rules
8191 have to be compiled, but any files depending on these files must also be
8192 recompiled. The dependency rules in Ada can be quite complex, especially
8193 in the presence of overloading, @code{use} clauses, generics and inlined
8194 subprograms.
8195
8196 @command{gnatmake} automatically takes care of the third and fourth steps
8197 of this process. It determines which sources need to be compiled,
8198 compiles them, and binds and links the resulting object files.
8199
8200 Unlike some other Ada make programs, the dependencies are always
8201 accurately recomputed from the new sources. The source based approach of
8202 the GNAT compilation model makes this possible. This means that if
8203 changes to the source program cause corresponding changes in
8204 dependencies, they will always be tracked exactly correctly by
8205 @command{gnatmake}.
8206
8207 @node Running gnatmake
8208 @section Running @command{gnatmake}
8209
8210 @noindent
8211 The usual form of the @command{gnatmake} command is
8212
8213 @smallexample
8214 $ gnatmake [@var{switches}] @var{file_name}
8215       [@var{file_names}] [@var{mode_switches}]
8216 @end smallexample
8217
8218 @noindent
8219 The only required argument is one @var{file_name}, which specifies
8220 a compilation unit that is a main program. Several @var{file_names} can be
8221 specified: this will result in several executables being built.
8222 If @code{switches} are present, they can be placed before the first
8223 @var{file_name}, between @var{file_names} or after the last @var{file_name}.
8224 If @var{mode_switches} are present, they must always be placed after
8225 the last @var{file_name} and all @code{switches}.
8226
8227 If you are using standard file extensions (.adb and .ads), then the
8228 extension may be omitted from the @var{file_name} arguments. However, if
8229 you are using non-standard extensions, then it is required that the
8230 extension be given. A relative or absolute directory path can be
8231 specified in a @var{file_name}, in which case, the input source file will
8232 be searched for in the specified directory only. Otherwise, the input
8233 source file will first be searched in the directory where
8234 @command{gnatmake} was invoked and if it is not found, it will be search on
8235 the source path of the compiler as described in
8236 @ref{Search Paths and the Run-Time Library (RTL)}.
8237
8238 All @command{gnatmake} output (except when you specify
8239 @option{^-M^/DEPENDENCIES_LIST^}) is to
8240 @file{stderr}. The output produced by the
8241 @option{^-M^/DEPENDENCIES_LIST^} switch is send to
8242 @file{stdout}.
8243
8244 @node Switches for gnatmake
8245 @section Switches for @command{gnatmake}
8246
8247 @noindent
8248 You may specify any of the following switches to @command{gnatmake}:
8249
8250 @table @option
8251 @c !sort!
8252 @ifclear vms
8253 @item --GCC=@var{compiler_name}
8254 @cindex @option{--GCC=compiler_name} (@command{gnatmake})
8255 Program used for compiling. The default is `@command{gcc}'. You need to use
8256 quotes around @var{compiler_name} if @code{compiler_name} contains
8257 spaces or other separator characters. As an example @option{--GCC="foo -x
8258 -y"} will instruct @command{gnatmake} to use @code{foo -x -y} as your
8259 compiler. A limitation of this syntax is that the name and path name of
8260 the executable itself must not include any embedded spaces. Note that
8261 switch @option{-c} is always inserted after your command name. Thus in the
8262 above example the compiler command that will be used by @command{gnatmake}
8263 will be @code{foo -c -x -y}. If several @option{--GCC=compiler_name} are
8264 used, only the last @var{compiler_name} is taken into account. However,
8265 all the additional switches are also taken into account. Thus,
8266 @option{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
8267 @option{--GCC="bar -x -y -z -t"}.
8268
8269 @item --GNATBIND=@var{binder_name}
8270 @cindex @option{--GNATBIND=binder_name} (@command{gnatmake})
8271 Program used for binding. The default is `@code{gnatbind}'. You need to
8272 use quotes around @var{binder_name} if @var{binder_name} contains spaces
8273 or other separator characters. As an example @option{--GNATBIND="bar -x
8274 -y"} will instruct @command{gnatmake} to use @code{bar -x -y} as your
8275 binder. Binder switches that are normally appended by @command{gnatmake}
8276 to `@code{gnatbind}' are now appended to the end of @code{bar -x -y}.
8277 A limitation of this syntax is that the name and path name of the executable
8278 itself must not include any embedded spaces.
8279
8280 @item --GNATLINK=@var{linker_name}
8281 @cindex @option{--GNATLINK=linker_name} (@command{gnatmake})
8282 Program used for linking. The default is `@command{gnatlink}'. You need to
8283 use quotes around @var{linker_name} if @var{linker_name} contains spaces
8284 or other separator characters. As an example @option{--GNATLINK="lan -x
8285 -y"} will instruct @command{gnatmake} to use @code{lan -x -y} as your
8286 linker. Linker switches that are normally appended by @command{gnatmake} to
8287 `@command{gnatlink}' are now appended to the end of @code{lan -x -y}.
8288 A limitation of this syntax is that the name and path name of the executable
8289 itself must not include any embedded spaces.
8290
8291 @end ifclear
8292
8293 @item ^-a^/ALL_FILES^
8294 @cindex @option{^-a^/ALL_FILES^} (@command{gnatmake})
8295 Consider all files in the make process, even the GNAT internal system
8296 files (for example, the predefined Ada library files), as well as any
8297 locked files. Locked files are files whose ALI file is write-protected.
8298 By default,
8299 @command{gnatmake} does not check these files,
8300 because the assumption is that the GNAT internal files are properly up
8301 to date, and also that any write protected ALI files have been properly
8302 installed. Note that if there is an installation problem, such that one
8303 of these files is not up to date, it will be properly caught by the
8304 binder.
8305 You may have to specify this switch if you are working on GNAT
8306 itself. The switch @option{^-a^/ALL_FILES^} is also useful
8307 in conjunction with @option{^-f^/FORCE_COMPILE^}
8308 if you need to recompile an entire application,
8309 including run-time files, using special configuration pragmas,
8310 such as a @code{Normalize_Scalars} pragma.
8311
8312 By default
8313 @code{gnatmake ^-a^/ALL_FILES^} compiles all GNAT
8314 internal files with
8315 @ifclear vms
8316 @code{gcc -c -gnatpg} rather than @code{gcc -c}.
8317 @end ifclear
8318 @ifset vms
8319 the @code{/CHECKS=SUPPRESS_ALL /STYLE_CHECKS=GNAT} switch.
8320 @end ifset
8321
8322 @item ^-b^/ACTIONS=BIND^
8323 @cindex @option{^-b^/ACTIONS=BIND^} (@command{gnatmake})
8324 Bind only. Can be combined with @option{^-c^/ACTIONS=COMPILE^} to do
8325 compilation and binding, but no link.
8326 Can be combined with @option{^-l^/ACTIONS=LINK^}
8327 to do binding and linking. When not combined with
8328 @option{^-c^/ACTIONS=COMPILE^}
8329 all the units in the closure of the main program must have been previously
8330 compiled and must be up to date. The root unit specified by @var{file_name}
8331 may be given without extension, with the source extension or, if no GNAT
8332 Project File is specified, with the ALI file extension.
8333
8334 @item ^-c^/ACTIONS=COMPILE^
8335 @cindex @option{^-c^/ACTIONS=COMPILE^} (@command{gnatmake})
8336 Compile only. Do not perform binding, except when @option{^-b^/ACTIONS=BIND^}
8337 is also specified. Do not perform linking, except if both
8338 @option{^-b^/ACTIONS=BIND^} and
8339  @option{^-l^/ACTIONS=LINK^} are also specified.
8340 If the root unit specified by @var{file_name} is not a main unit, this is the
8341 default. Otherwise @command{gnatmake} will attempt binding and linking
8342 unless all objects are up to date and the executable is more recent than
8343 the objects.
8344
8345 @item ^-C^/MAPPING^
8346 @cindex @option{^-C^/MAPPING^} (@command{gnatmake})
8347 Use a temporary mapping file. A mapping file is a way to communicate to the
8348 compiler two mappings: from unit names to file names (without any directory
8349 information) and from file names to path names (with full directory
8350 information). These mappings are used by the compiler to short-circuit the path
8351 search. When @command{gnatmake} is invoked with this switch, it will create
8352 a temporary mapping file, initially populated by the project manager,
8353 if @option{^-P^/PROJECT_FILE^} is used, otherwise initially empty.
8354 Each invocation of the compiler will add the newly accessed sources to the
8355 mapping file. This will improve the source search during the next invocation
8356 of the compiler.
8357
8358 @item ^-C=^/USE_MAPPING_FILE=^@var{file}
8359 @cindex @option{^-C=^/USE_MAPPING^} (@command{gnatmake})
8360 Use a specific mapping file. The file, specified as a path name (absolute or
8361 relative) by this switch, should already exist, otherwise the switch is
8362 ineffective. The specified mapping file will be communicated to the compiler.
8363 This switch is not compatible with a project file
8364 (^-P^/PROJECT_FILE=^@var{file}) or with multiple compiling processes
8365 (^-j^/PROCESSES=^nnn, when nnn is greater than 1).
8366
8367 @item ^-D ^/DIRECTORY_OBJECTS=^@var{dir}
8368 @cindex @option{^-D^/DIRECTORY_OBJECTS^} (@command{gnatmake})
8369 Put all object files and ALI file in directory @var{dir}.
8370 If the @option{^-D^/DIRECTORY_OBJECTS^} switch is not used, all object files
8371 and ALI files go in the current working directory.
8372
8373 This switch cannot be used when using a project file.
8374
8375 @ifclear vms
8376 @item -eL
8377 @cindex @option{-eL} (@command{gnatmake})
8378 Follow all symbolic links when processing project files.
8379 @end ifclear
8380
8381 @item ^-f^/FORCE_COMPILE^
8382 @cindex @option{^-f^/FORCE_COMPILE^} (@command{gnatmake})
8383 Force recompilations. Recompile all sources, even though some object
8384 files may be up to date, but don't recompile predefined or GNAT internal
8385 files or locked files (files with a write-protected ALI file),
8386 unless the @option{^-a^/ALL_FILES^} switch is also specified.
8387
8388 @item ^-F^/FULL_PATH_IN_BRIEF_MESSAGES^
8389 @cindex @option{^-F^/FULL_PATH_IN_BRIEF_MESSAGES^} (@command{gnatmake})
8390 When using project files, if some errors or warnings are detected during
8391 parsing and verbose mode is not in effect (no use of switch
8392 ^-v^/VERBOSE^), then error lines start with the full path name of the project
8393 file, rather than its simple file name.
8394
8395 @item ^-i^/IN_PLACE^
8396 @cindex @option{^-i^/IN_PLACE^} (@command{gnatmake})
8397 In normal mode, @command{gnatmake} compiles all object files and ALI files
8398 into the current directory. If the @option{^-i^/IN_PLACE^} switch is used,
8399 then instead object files and ALI files that already exist are overwritten
8400 in place. This means that once a large project is organized into separate
8401 directories in the desired manner, then @command{gnatmake} will automatically
8402 maintain and update this organization. If no ALI files are found on the
8403 Ada object path (@ref{Search Paths and the Run-Time Library (RTL)}),
8404 the new object and ALI files are created in the
8405 directory containing the source being compiled. If another organization
8406 is desired, where objects and sources are kept in different directories,
8407 a useful technique is to create dummy ALI files in the desired directories.
8408 When detecting such a dummy file, @command{gnatmake} will be forced to
8409 recompile the corresponding source file, and it will be put the resulting
8410 object and ALI files in the directory where it found the dummy file.
8411
8412 @item ^-j^/PROCESSES=^@var{n}
8413 @cindex @option{^-j^/PROCESSES^} (@command{gnatmake})
8414 @cindex Parallel make
8415 Use @var{n} processes to carry out the (re)compilations. On a
8416 multiprocessor machine compilations will occur in parallel. In the
8417 event of compilation errors, messages from various compilations might
8418 get interspersed (but @command{gnatmake} will give you the full ordered
8419 list of failing compiles at the end). If this is problematic, rerun
8420 the make process with n set to 1 to get a clean list of messages.
8421
8422 @item ^-k^/CONTINUE_ON_ERROR^
8423 @cindex @option{^-k^/CONTINUE_ON_ERROR^} (@command{gnatmake})
8424 Keep going. Continue as much as possible after a compilation error. To
8425 ease the programmer's task in case of compilation errors, the list of
8426 sources for which the compile fails is given when @command{gnatmake}
8427 terminates.
8428
8429 If @command{gnatmake} is invoked with several @file{file_names} and with this
8430 switch, if there are compilation errors when building an executable,
8431 @command{gnatmake} will not attempt to build the following executables.
8432
8433 @item ^-l^/ACTIONS=LINK^
8434 @cindex @option{^-l^/ACTIONS=LINK^} (@command{gnatmake})
8435 Link only. Can be combined with @option{^-b^/ACTIONS=BIND^} to binding
8436 and linking. Linking will not be performed if combined with
8437 @option{^-c^/ACTIONS=COMPILE^}
8438 but not with @option{^-b^/ACTIONS=BIND^}.
8439 When not combined with @option{^-b^/ACTIONS=BIND^}
8440 all the units in the closure of the main program must have been previously
8441 compiled and must be up to date, and the main program needs to have been bound.
8442 The root unit specified by @var{file_name}
8443 may be given without extension, with the source extension or, if no GNAT
8444 Project File is specified, with the ALI file extension.
8445
8446 @item ^-m^/MINIMAL_RECOMPILATION^
8447 @cindex @option{^-m^/MINIMAL_RECOMPILATION^} (@command{gnatmake})
8448 Specify that the minimum necessary amount of recompilations
8449 be performed. In this mode @command{gnatmake} ignores time
8450 stamp differences when the only
8451 modifications to a source file consist in adding/removing comments,
8452 empty lines, spaces or tabs. This means that if you have changed the
8453 comments in a source file or have simply reformatted it, using this
8454 switch will tell gnatmake not to recompile files that depend on it
8455 (provided other sources on which these files depend have undergone no
8456 semantic modifications). Note that the debugging information may be
8457 out of date with respect to the sources if the @option{-m} switch causes
8458 a compilation to be switched, so the use of this switch represents a
8459 trade-off between compilation time and accurate debugging information.
8460
8461 @item ^-M^/DEPENDENCIES_LIST^
8462 @cindex Dependencies, producing list
8463 @cindex @option{^-M^/DEPENDENCIES_LIST^} (@command{gnatmake})
8464 Check if all objects are up to date. If they are, output the object
8465 dependences to @file{stdout} in a form that can be directly exploited in
8466 a @file{Makefile}. By default, each source file is prefixed with its
8467 (relative or absolute) directory name. This name is whatever you
8468 specified in the various @option{^-aI^/SOURCE_SEARCH^}
8469 and @option{^-I^/SEARCH^} switches. If you use
8470 @code{gnatmake ^-M^/DEPENDENCIES_LIST^}
8471 @option{^-q^/QUIET^}
8472 (see below), only the source file names,
8473 without relative paths, are output. If you just specify the
8474 @option{^-M^/DEPENDENCIES_LIST^}
8475 switch, dependencies of the GNAT internal system files are omitted. This
8476 is typically what you want. If you also specify
8477 the @option{^-a^/ALL_FILES^} switch,
8478 dependencies of the GNAT internal files are also listed. Note that
8479 dependencies of the objects in external Ada libraries (see switch
8480 @option{^-aL^/SKIP_MISSING=^}@var{dir} in the following list)
8481 are never reported.
8482
8483 @item ^-n^/DO_OBJECT_CHECK^
8484 @cindex @option{^-n^/DO_OBJECT_CHECK^} (@command{gnatmake})
8485 Don't compile, bind, or link. Checks if all objects are up to date.
8486 If they are not, the full name of the first file that needs to be
8487 recompiled is printed.
8488 Repeated use of this option, followed by compiling the indicated source
8489 file, will eventually result in recompiling all required units.
8490
8491 @item ^-o ^/EXECUTABLE=^@var{exec_name}
8492 @cindex @option{^-o^/EXECUTABLE^} (@command{gnatmake})
8493 Output executable name. The name of the final executable program will be
8494 @var{exec_name}. If the @option{^-o^/EXECUTABLE^} switch is omitted the default
8495 name for the executable will be the name of the input file in appropriate form
8496 for an executable file on the host system.
8497
8498 This switch cannot be used when invoking @command{gnatmake} with several
8499 @file{file_names}.
8500
8501 @item ^-P^/PROJECT_FILE=^@var{project}
8502 @cindex @option{^-P^/PROJECT_FILE^} (@command{gnatmake})
8503 Use project file @var{project}. Only one such switch can be used.
8504 @xref{gnatmake and Project Files}.
8505
8506 @item ^-q^/QUIET^
8507 @cindex @option{^-q^/QUIET^} (@command{gnatmake})
8508 Quiet. When this flag is not set, the commands carried out by
8509 @command{gnatmake} are displayed.
8510
8511 @item ^-s^/SWITCH_CHECK/^
8512 @cindex @option{^-s^/SWITCH_CHECK^} (@command{gnatmake})
8513 Recompile if compiler switches have changed since last compilation.
8514 All compiler switches but -I and -o are taken into account in the
8515 following way:
8516 orders between different ``first letter'' switches are ignored, but
8517 orders between same switches are taken into account. For example,
8518 @option{-O -O2} is different than @option{-O2 -O}, but @option{-g -O}
8519 is equivalent to @option{-O -g}.
8520
8521 This switch is recommended when Integrated Preprocessing is used.
8522
8523 @item ^-u^/UNIQUE^
8524 @cindex @option{^-u^/UNIQUE^} (@command{gnatmake})
8525 Unique. Recompile at most the main files. It implies -c. Combined with
8526 -f, it is equivalent to calling the compiler directly. Note that using
8527 ^-u^/UNIQUE^ with a project file and no main has a special meaning
8528 (@pxref{Project Files and Main Subprograms}).
8529
8530 @item ^-U^/ALL_PROJECTS^
8531 @cindex @option{^-U^/ALL_PROJECTS^} (@command{gnatmake})
8532 When used without a project file or with one or several mains on the command
8533 line, is equivalent to ^-u^/UNIQUE^. When used with a project file and no main
8534 on the command line, all sources of all project files are checked and compiled
8535 if not up to date, and libraries are rebuilt, if necessary.
8536
8537 @item ^-v^/REASONS^
8538 @cindex @option{^-v^/REASONS^} (@command{gnatmake})
8539 Verbose. Display the reason for all recompilations @command{gnatmake}
8540 decides are necessary, with the highest verbosity level.
8541
8542 @item ^-vl^/LOW_VERBOSITY^
8543 @cindex @option{^-vl^/LOW_VERBOSITY^} (@command{gnatmake})
8544 Verbosity level Low. Display fewer lines than in verbosity Medium.
8545
8546 @item ^-vm^/MEDIUM_VERBOSITY^
8547 @cindex @option{^-vm^/MEDIUM_VERBOSITY^} (@command{gnatmake})
8548 Verbosity level Medium. Potentially display fewer lines than in verbosity High.
8549
8550 @item ^-vh^/HIGH_VERBOSITY^
8551 @cindex @option{^-vm^/HIGH_VERBOSITY^} (@command{gnatmake})
8552 Verbosity level High. Equivalent to ^-v^/REASONS^.
8553
8554 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
8555 Indicate the verbosity of the parsing of GNAT project files.
8556 @xref{Switches Related to Project Files}.
8557
8558 @item ^-x^/NON_PROJECT_UNIT_COMPILATION^
8559 @cindex @option{^-x^/NON_PROJECT_UNIT_COMPILATION^} (@command{gnatmake})
8560 Indicate that sources that are not part of any Project File may be compiled.
8561 Normally, when using Project Files, only sources that are part of a Project
8562 File may be compile. When this switch is used, a source outside of all Project
8563 Files may be compiled. The ALI file and the object file will be put in the
8564 object directory of the main Project. The compilation switches used will only
8565 be those specified on the command line.
8566
8567 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
8568 Indicate that external variable @var{name} has the value @var{value}.
8569 The Project Manager will use this value for occurrences of
8570 @code{external(name)} when parsing the project file.
8571 @xref{Switches Related to Project Files}.
8572
8573 @item ^-z^/NOMAIN^
8574 @cindex @option{^-z^/NOMAIN^} (@command{gnatmake})
8575 No main subprogram. Bind and link the program even if the unit name
8576 given on the command line is a package name. The resulting executable
8577 will execute the elaboration routines of the package and its closure,
8578 then the finalization routines.
8579
8580 @item ^-g^/DEBUG^
8581 @cindex @option{^-g^/DEBUG^} (@command{gnatmake})
8582 Enable debugging. This switch is simply passed to the compiler and to the
8583 linker.
8584
8585 @end table
8586
8587 @table @asis
8588 @item @command{gcc} @asis{switches}
8589 @ifclear vms
8590 Any uppercase or multi-character switch that is not a @command{gnatmake} switch
8591 is passed to @command{gcc} (e.g. @option{-O}, @option{-gnato,} etc.)
8592 @end ifclear
8593 @ifset vms
8594 Any qualifier that cannot be recognized as a qualifier for @code{GNAT MAKE}
8595 but is recognizable as a valid qualifier for @code{GNAT COMPILE} is
8596 automatically treated as a compiler switch, and passed on to all
8597 compilations that are carried out.
8598 @end ifset
8599 @end table
8600
8601 @noindent
8602 Source and library search path switches:
8603
8604 @table @option
8605 @c !sort!
8606 @item ^-aI^/SOURCE_SEARCH=^@var{dir}
8607 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatmake})
8608 When looking for source files also look in directory @var{dir}.
8609 The order in which source files search is undertaken is
8610 described in @ref{Search Paths and the Run-Time Library (RTL)}.
8611
8612 @item ^-aL^/SKIP_MISSING=^@var{dir}
8613 @cindex @option{^-aL^/SKIP_MISSING^} (@command{gnatmake})
8614 Consider @var{dir} as being an externally provided Ada library.
8615 Instructs @command{gnatmake} to skip compilation units whose @file{.ALI}
8616 files have been located in directory @var{dir}. This allows you to have
8617 missing bodies for the units in @var{dir} and to ignore out of date bodies
8618 for the same units. You still need to specify
8619 the location of the specs for these units by using the switches
8620 @option{^-aI^/SOURCE_SEARCH=^@var{dir}}
8621 or @option{^-I^/SEARCH=^@var{dir}}.
8622 Note: this switch is provided for compatibility with previous versions
8623 of @command{gnatmake}. The easier method of causing standard libraries
8624 to be excluded from consideration is to write-protect the corresponding
8625 ALI files.
8626
8627 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
8628 @cindex @option{^-aO^/OBJECT_SEARCH^} (@command{gnatmake})
8629 When searching for library and object files, look in directory
8630 @var{dir}. The order in which library files are searched is described in
8631 @ref{Search Paths for gnatbind}.
8632
8633 @item ^-A^/CONDITIONAL_SOURCE_SEARCH=^@var{dir}
8634 @cindex Search paths, for @command{gnatmake}
8635 @cindex @option{^-A^/CONDITIONAL_SOURCE_SEARCH^} (@command{gnatmake})
8636 Equivalent to @option{^-aL^/SKIP_MISSING=^@var{dir}
8637 ^-aI^/SOURCE_SEARCH=^@var{dir}}.
8638
8639 @item ^-I^/SEARCH=^@var{dir}
8640 @cindex @option{^-I^/SEARCH^} (@command{gnatmake})
8641 Equivalent to @option{^-aO^/OBJECT_SEARCH=^@var{dir}
8642 ^-aI^/SOURCE_SEARCH=^@var{dir}}.
8643
8644 @item ^-I-^/NOCURRENT_DIRECTORY^
8645 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gnatmake})
8646 @cindex Source files, suppressing search
8647 Do not look for source files in the directory containing the source
8648 file named in the command line.
8649 Do not look for ALI or object files in the directory
8650 where @command{gnatmake} was invoked.
8651
8652 @item ^-L^/LIBRARY_SEARCH=^@var{dir}
8653 @cindex @option{^-L^/LIBRARY_SEARCH^} (@command{gnatmake})
8654 @cindex Linker libraries
8655 Add directory @var{dir} to the list of directories in which the linker
8656 will search for libraries. This is equivalent to
8657 @option{-largs ^-L^/LIBRARY_SEARCH=^}@var{dir}.
8658 @ifclear vms
8659 Furthermore, under Windows, the sources pointed to by the libraries path
8660 set in the registry are not searched for.
8661 @end ifclear
8662
8663 @item -nostdinc
8664 @cindex @option{-nostdinc} (@command{gnatmake})
8665 Do not look for source files in the system default directory.
8666
8667 @item -nostdlib
8668 @cindex @option{-nostdlib} (@command{gnatmake})
8669 Do not look for library files in the system default directory.
8670
8671 @item --RTS=@var{rts-path}
8672 @cindex @option{--RTS} (@command{gnatmake})
8673 Specifies the default location of the runtime library. GNAT looks for the
8674 runtime
8675 in the following directories, and stops as soon as a valid runtime is found
8676 (@file{adainclude} or @file{ada_source_path}, and @file{adalib} or
8677 @file{ada_object_path} present):
8678
8679 @itemize @bullet
8680 @item <current directory>/$rts_path
8681
8682 @item <default-search-dir>/$rts_path
8683
8684 @item <default-search-dir>/rts-$rts_path
8685 @end itemize
8686
8687 @noindent
8688 The selected path is handled like a normal RTS path.
8689
8690 @end table
8691
8692 @node Mode Switches for gnatmake
8693 @section Mode Switches for @command{gnatmake}
8694
8695 @noindent
8696 The mode switches (referred to as @code{mode_switches}) allow the
8697 inclusion of switches that are to be passed to the compiler itself, the
8698 binder or the linker. The effect of a mode switch is to cause all
8699 subsequent switches up to the end of the switch list, or up to the next
8700 mode switch, to be interpreted as switches to be passed on to the
8701 designated component of GNAT.
8702
8703 @table @option
8704 @c !sort!
8705 @item -cargs @var{switches}
8706 @cindex @option{-cargs} (@command{gnatmake})
8707 Compiler switches. Here @var{switches} is a list of switches
8708 that are valid switches for @command{gcc}. They will be passed on to
8709 all compile steps performed by @command{gnatmake}.
8710
8711 @item -bargs @var{switches}
8712 @cindex @option{-bargs} (@command{gnatmake})
8713 Binder switches. Here @var{switches} is a list of switches
8714 that are valid switches for @code{gnatbind}. They will be passed on to
8715 all bind steps performed by @command{gnatmake}.
8716
8717 @item -largs @var{switches}
8718 @cindex @option{-largs} (@command{gnatmake})
8719 Linker switches. Here @var{switches} is a list of switches
8720 that are valid switches for @command{gnatlink}. They will be passed on to
8721 all link steps performed by @command{gnatmake}.
8722
8723 @item -margs @var{switches}
8724 @cindex @option{-margs} (@command{gnatmake})
8725 Make switches. The switches are directly interpreted by @command{gnatmake},
8726 regardless of any previous occurrence of @option{-cargs}, @option{-bargs}
8727 or @option{-largs}.
8728 @end table
8729
8730 @node Notes on the Command Line
8731 @section Notes on the Command Line
8732
8733 @noindent
8734 This section contains some additional useful notes on the operation
8735 of the @command{gnatmake} command.
8736
8737 @itemize @bullet
8738 @item
8739 @cindex Recompilation, by @command{gnatmake}
8740 If @command{gnatmake} finds no ALI files, it recompiles the main program
8741 and all other units required by the main program.
8742 This means that @command{gnatmake}
8743 can be used for the initial compile, as well as during subsequent steps of
8744 the development cycle.
8745
8746 @item
8747 If you enter @code{gnatmake @var{file}.adb}, where @file{@var{file}.adb}
8748 is a subunit or body of a generic unit, @command{gnatmake} recompiles
8749 @file{@var{file}.adb} (because it finds no ALI) and stops, issuing a
8750 warning.
8751
8752 @item
8753 In @command{gnatmake} the switch @option{^-I^/SEARCH^}
8754 is used to specify both source and
8755 library file paths. Use @option{^-aI^/SOURCE_SEARCH^}
8756 instead if you just want to specify
8757 source paths only and @option{^-aO^/OBJECT_SEARCH^}
8758 if you want to specify library paths
8759 only.
8760
8761 @item
8762 @command{gnatmake} will ignore any files whose ALI file is write-protected.
8763 This may conveniently be used to exclude standard libraries from
8764 consideration and in particular it means that the use of the
8765 @option{^-f^/FORCE_COMPILE^} switch will not recompile these files
8766 unless @option{^-a^/ALL_FILES^} is also specified.
8767
8768 @item
8769 @command{gnatmake} has been designed to make the use of Ada libraries
8770 particularly convenient. Assume you have an Ada library organized
8771 as follows: @i{^obj-dir^[OBJ_DIR]^} contains the objects and ALI files for
8772 of your Ada compilation units,
8773 whereas @i{^include-dir^[INCLUDE_DIR]^} contains the
8774 specs of these units, but no bodies. Then to compile a unit
8775 stored in @code{main.adb}, which uses this Ada library you would just type
8776
8777 @smallexample
8778 @ifclear vms
8779 $ gnatmake -aI@var{include-dir}  -aL@var{obj-dir}  main
8780 @end ifclear
8781 @ifset vms
8782 $ gnatmake /SOURCE_SEARCH=@i{[INCLUDE_DIR]}
8783            /SKIP_MISSING=@i{[OBJ_DIR]} main
8784 @end ifset
8785 @end smallexample
8786
8787 @item
8788 Using @command{gnatmake} along with the
8789 @option{^-m (minimal recompilation)^/MINIMAL_RECOMPILATION^}
8790 switch provides a mechanism for avoiding unnecessary rcompilations. Using
8791 this switch,
8792 you can update the comments/format of your
8793 source files without having to recompile everything. Note, however, that
8794 adding or deleting lines in a source files may render its debugging
8795 info obsolete. If the file in question is a spec, the impact is rather
8796 limited, as that debugging info will only be useful during the
8797 elaboration phase of your program. For bodies the impact can be more
8798 significant. In all events, your debugger will warn you if a source file
8799 is more recent than the corresponding object, and alert you to the fact
8800 that the debugging information may be out of date.
8801 @end itemize
8802
8803 @node How gnatmake Works
8804 @section How @command{gnatmake} Works
8805
8806 @noindent
8807 Generally @command{gnatmake} automatically performs all necessary
8808 recompilations and you don't need to worry about how it works. However,
8809 it may be useful to have some basic understanding of the @command{gnatmake}
8810 approach and in particular to understand how it uses the results of
8811 previous compilations without incorrectly depending on them.
8812
8813 First a definition: an object file is considered @dfn{up to date} if the
8814 corresponding ALI file exists and if all the source files listed in the
8815 dependency section of this ALI file have time stamps matching those in
8816 the ALI file. This means that neither the source file itself nor any
8817 files that it depends on have been modified, and hence there is no need
8818 to recompile this file.
8819
8820 @command{gnatmake} works by first checking if the specified main unit is up
8821 to date. If so, no compilations are required for the main unit. If not,
8822 @command{gnatmake} compiles the main program to build a new ALI file that
8823 reflects the latest sources. Then the ALI file of the main unit is
8824 examined to find all the source files on which the main program depends,
8825 and @command{gnatmake} recursively applies the above procedure on all these
8826 files.
8827
8828 This process ensures that @command{gnatmake} only trusts the dependencies
8829 in an existing ALI file if they are known to be correct. Otherwise it
8830 always recompiles to determine a new, guaranteed accurate set of
8831 dependencies. As a result the program is compiled ``upside down'' from what may
8832 be more familiar as the required order of compilation in some other Ada
8833 systems. In particular, clients are compiled before the units on which
8834 they depend. The ability of GNAT to compile in any order is critical in
8835 allowing an order of compilation to be chosen that guarantees that
8836 @command{gnatmake} will recompute a correct set of new dependencies if
8837 necessary.
8838
8839 When invoking @command{gnatmake} with several @var{file_names}, if a unit is
8840 imported by several of the executables, it will be recompiled at most once.
8841
8842 Note: when using non-standard naming conventions
8843 (@pxref{Using Other File Names}), changing through a configuration pragmas
8844 file the version of a source and invoking @command{gnatmake} to recompile may
8845 have no effect, if the previous version of the source is still accessible
8846 by @command{gnatmake}. It may be necessary to use the switch
8847 ^-f^/FORCE_COMPILE^.
8848
8849 @node Examples of gnatmake Usage
8850 @section Examples of @command{gnatmake} Usage
8851
8852 @table @code
8853 @item gnatmake hello.adb
8854 Compile all files necessary to bind and link the main program
8855 @file{hello.adb} (containing unit @code{Hello}) and bind and link the
8856 resulting object files to generate an executable file @file{^hello^HELLO.EXE^}.
8857
8858 @item gnatmake main1 main2 main3
8859 Compile all files necessary to bind and link the main programs
8860 @file{main1.adb} (containing unit @code{Main1}), @file{main2.adb}
8861 (containing unit @code{Main2}) and @file{main3.adb}
8862 (containing unit @code{Main3}) and bind and link the resulting object files
8863 to generate three executable files @file{^main1^MAIN1.EXE^},
8864 @file{^main2^MAIN2.EXE^}
8865 and @file{^main3^MAIN3.EXE^}.
8866
8867 @ifclear vms
8868 @item gnatmake -q Main_Unit -cargs -O2 -bargs -l
8869 @end ifclear
8870
8871 @ifset vms
8872 @item gnatmake Main_Unit /QUIET
8873    /COMPILER_QUALIFIERS /OPTIMIZE=ALL
8874    /BINDER_QUALIFIERS /ORDER_OF_ELABORATION
8875 @end ifset
8876 Compile all files necessary to bind and link the main program unit
8877 @code{Main_Unit} (from file @file{main_unit.adb}). All compilations will
8878 be done with optimization level 2 and the order of elaboration will be
8879 listed by the binder. @command{gnatmake} will operate in quiet mode, not
8880 displaying commands it is executing.
8881 @end table
8882
8883 @c *************************
8884 @node Improving Performance
8885 @chapter Improving Performance
8886 @cindex Improving performance
8887
8888 @noindent
8889 This chapter presents several topics related to program performance.
8890 It first describes some of the tradeoffs that need to be considered
8891 and some of the techniques for making your program run faster.
8892 It then documents the @command{gnatelim} tool, which can reduce
8893 the size of program executables.
8894
8895 @ifnottex
8896 @menu
8897 * Performance Considerations::
8898 * Reducing the Size of Ada Executables with gnatelim::
8899 @end menu
8900 @end ifnottex
8901
8902 @c *****************************
8903 @node Performance Considerations
8904 @section Performance Considerations
8905
8906 @noindent
8907 The GNAT system provides a number of options that allow a trade-off
8908 between
8909
8910 @itemize @bullet
8911 @item
8912 performance of the generated code
8913
8914 @item
8915 speed of compilation
8916
8917 @item
8918 minimization of dependences and recompilation
8919
8920 @item
8921 the degree of run-time checking.
8922 @end itemize
8923
8924 @noindent
8925 The defaults (if no options are selected) aim at improving the speed
8926 of compilation and minimizing dependences, at the expense of performance
8927 of the generated code:
8928
8929 @itemize @bullet
8930 @item
8931 no optimization
8932
8933 @item
8934 no inlining of subprogram calls
8935
8936 @item
8937 all run-time checks enabled except overflow and elaboration checks
8938 @end itemize
8939
8940 @noindent
8941 These options are suitable for most program development purposes. This
8942 chapter describes how you can modify these choices, and also provides
8943 some guidelines on debugging optimized code.
8944
8945 @menu
8946 * Controlling Run-Time Checks::
8947 * Use of Restrictions::
8948 * Optimization Levels::
8949 * Debugging Optimized Code::
8950 * Inlining of Subprograms::
8951 * Other Optimization Switches::
8952 * Optimization and Strict Aliasing::
8953
8954 @ifset vms
8955 * Coverage Analysis::
8956 @end ifset
8957 @end menu
8958
8959 @node Controlling Run-Time Checks
8960 @subsection Controlling Run-Time Checks
8961
8962 @noindent
8963 By default, GNAT generates all run-time checks, except arithmetic overflow
8964 checking for integer operations and checks for access before elaboration on
8965 subprogram calls. The latter are not required in default mode, because all
8966 necessary checking is done at compile time.
8967 @cindex @option{-gnatp} (@command{gcc})
8968 @cindex @option{-gnato} (@command{gcc})
8969 Two gnat switches, @option{-gnatp} and @option{-gnato} allow this default to
8970 be modified. @xref{Run-Time Checks}.
8971
8972 Our experience is that the default is suitable for most development
8973 purposes.
8974
8975 We treat integer overflow specially because these
8976 are quite expensive and in our experience are not as important as other
8977 run-time checks in the development process. Note that division by zero
8978 is not considered an overflow check, and divide by zero checks are
8979 generated where required by default.
8980
8981 Elaboration checks are off by default, and also not needed by default, since
8982 GNAT uses a static elaboration analysis approach that avoids the need for
8983 run-time checking. This manual contains a full chapter discussing the issue
8984 of elaboration checks, and if the default is not satisfactory for your use,
8985 you should read this chapter.
8986
8987 For validity checks, the minimal checks required by the Ada Reference
8988 Manual (for case statements and assignments to array elements) are on
8989 by default. These can be suppressed by use of the @option{-gnatVn} switch.
8990 Note that in Ada 83, there were no validity checks, so if the Ada 83 mode
8991 is acceptable (or when comparing GNAT performance with an Ada 83 compiler),
8992 it may be reasonable to routinely use @option{-gnatVn}. Validity checks
8993 are also suppressed entirely if @option{-gnatp} is used.
8994
8995 @cindex Overflow checks
8996 @cindex Checks, overflow
8997 @findex Suppress
8998 @findex Unsuppress
8999 @cindex pragma Suppress
9000 @cindex pragma Unsuppress
9001 Note that the setting of the switches controls the default setting of
9002 the checks. They may be modified using either @code{pragma Suppress} (to
9003 remove checks) or @code{pragma Unsuppress} (to add back suppressed
9004 checks) in the program source.
9005
9006 @node Use of Restrictions
9007 @subsection Use of Restrictions
9008
9009 @noindent
9010 The use of pragma Restrictions allows you to control which features are
9011 permitted in your program. Apart from the obvious point that if you avoid
9012 relatively expensive features like finalization (enforceable by the use
9013 of pragma Restrictions (No_Finalization), the use of this pragma does not
9014 affect the generated code in most cases.
9015
9016 One notable exception to this rule is that the possibility of task abort
9017 results in some distributed overhead, particularly if finalization or
9018 exception handlers are used. The reason is that certain sections of code
9019 have to be marked as non-abortable.
9020
9021 If you use neither the @code{abort} statement, nor asynchronous transfer
9022 of control (@code{select .. then abort}), then this distributed overhead
9023 is removed, which may have a general positive effect in improving
9024 overall performance.  Especially code involving frequent use of tasking
9025 constructs and controlled types will show much improved performance.
9026 The relevant restrictions pragmas are
9027
9028 @smallexample
9029    pragma Restrictions (No_Abort_Statements);
9030    pragma Restrictions (Max_Asynchronous_Select_Nesting => 0);
9031 @end smallexample
9032
9033 @noindent
9034 It is recommended that these restriction pragmas be used if possible. Note
9035 that this also means that you can write code without worrying about the
9036 possibility of an immediate abort at any point.
9037
9038 @node Optimization Levels
9039 @subsection Optimization Levels
9040 @cindex @option{^-O^/OPTIMIZE^} (@command{gcc})
9041
9042 @noindent
9043 The default is optimization off. This results in the fastest compile
9044 times, but GNAT makes absolutely no attempt to optimize, and the
9045 generated programs are considerably larger and slower than when
9046 optimization is enabled. You can use the
9047 @ifclear vms
9048 @option{-O@var{n}} switch, where @var{n} is an integer from 0 to 3,
9049 @end ifclear
9050 @ifset vms
9051 @code{OPTIMIZE} qualifier
9052 @end ifset
9053 to @command{gcc} to control the optimization level:
9054
9055 @table @option
9056 @item ^-O0^/OPTIMIZE=NONE^
9057 No optimization (the default);
9058 generates unoptimized code but has
9059 the fastest compilation time.
9060
9061 Note that many other compilers do fairly extensive optimization
9062 even if "no optimization" is specified. When using gcc, it is
9063 very unusual to use ^-O0^/OPTIMIZE=NONE^ for production if
9064 execution time is of any concern, since ^-O0^/OPTIMIZE=NONE^
9065 really does mean no optimization at all. This difference between
9066 gcc and other compilers should be kept in mind when doing
9067 performance comparisons.
9068
9069 @item ^-O1^/OPTIMIZE=SOME^
9070 Moderate optimization;
9071 optimizes reasonably well but does not
9072 degrade compilation time significantly.
9073
9074 @item ^-O2^/OPTIMIZE=ALL^
9075 @ifset vms
9076 @itemx /OPTIMIZE=DEVELOPMENT
9077 @end ifset
9078 Full optimization;
9079 generates highly optimized code and has
9080 the slowest compilation time.
9081
9082 @item ^-O3^/OPTIMIZE=INLINING^
9083 Full optimization as in @option{-O2},
9084 and also attempts automatic inlining of small
9085 subprograms within a unit (@pxref{Inlining of Subprograms}).
9086 @end table
9087
9088 @noindent
9089 Higher optimization levels perform more global transformations on the
9090 program and apply more expensive analysis algorithms in order to generate
9091 faster and more compact code. The price in compilation time, and the
9092 resulting improvement in execution time,
9093 both depend on the particular application and the hardware environment.
9094 You should experiment to find the best level for your application.
9095
9096 Since the precise set of optimizations done at each level will vary from
9097 release to release (and sometime from target to target), it is best to think
9098 of the optimization settings in general terms.
9099 The @cite{Using GNU GCC} manual contains details about
9100 ^the @option{-O} settings and a number of @option{-f} options that^how to^
9101 individually enable or disable specific optimizations.
9102
9103 Unlike some other compilation systems, ^@command{gcc}^GNAT^ has
9104 been tested extensively at all optimization levels. There are some bugs
9105 which appear only with optimization turned on, but there have also been
9106 bugs which show up only in @emph{unoptimized} code. Selecting a lower
9107 level of optimization does not improve the reliability of the code
9108 generator, which in practice is highly reliable at all optimization
9109 levels.
9110
9111 Note regarding the use of @option{-O3}: The use of this optimization level
9112 is generally discouraged with GNAT, since it often results in larger
9113 executables which run more slowly. See further discussion of this point
9114 in @ref{Inlining of Subprograms}.
9115
9116 @node Debugging Optimized Code
9117 @subsection Debugging Optimized Code
9118 @cindex Debugging optimized code
9119 @cindex Optimization and debugging
9120
9121 @noindent
9122 Although it is possible to do a reasonable amount of debugging at
9123 @ifclear vms
9124 non-zero optimization levels,
9125 the higher the level the more likely that
9126 @end ifclear
9127 @ifset vms
9128 @option{/OPTIMIZE} settings other than @code{NONE},
9129 such settings will make it more likely that
9130 @end ifset
9131 source-level constructs will have been eliminated by optimization.
9132 For example, if a loop is strength-reduced, the loop
9133 control variable may be completely eliminated and thus cannot be
9134 displayed in the debugger.
9135 This can only happen at @option{-O2} or @option{-O3}.
9136 Explicit temporary variables that you code might be eliminated at
9137 ^level^setting^ @option{-O1} or higher.
9138
9139 The use of the @option{^-g^/DEBUG^} switch,
9140 @cindex @option{^-g^/DEBUG^} (@command{gcc})
9141 which is needed for source-level debugging,
9142 affects the size of the program executable on disk,
9143 and indeed the debugging information can be quite large.
9144 However, it has no effect on the generated code (and thus does not
9145 degrade performance)
9146
9147 Since the compiler generates debugging tables for a compilation unit before
9148 it performs optimizations, the optimizing transformations may invalidate some
9149 of the debugging data.  You therefore need to anticipate certain
9150 anomalous situations that may arise while debugging optimized code.
9151 These are the most common cases:
9152
9153 @enumerate
9154 @item
9155 @i{The ``hopping Program Counter'':}  Repeated @code{step} or @code{next}
9156 commands show
9157 the PC bouncing back and forth in the code.  This may result from any of
9158 the following optimizations:
9159
9160 @itemize @bullet
9161 @item
9162 @i{Common subexpression elimination:} using a single instance of code for a
9163 quantity that the source computes several times.  As a result you
9164 may not be able to stop on what looks like a statement.
9165
9166 @item
9167 @i{Invariant code motion:} moving an expression that does not change within a
9168 loop, to the beginning of the loop.
9169
9170 @item
9171 @i{Instruction scheduling:} moving instructions so as to
9172 overlap loads and stores (typically) with other code, or in
9173 general to move computations of values closer to their uses. Often
9174 this causes you to pass an assignment statement without the assignment
9175 happening and then later bounce back to the statement when the
9176 value is actually needed.  Placing a breakpoint on a line of code
9177 and then stepping over it may, therefore, not always cause all the
9178 expected side-effects.
9179 @end itemize
9180
9181 @item
9182 @i{The ``big leap'':} More commonly known as @emph{cross-jumping}, in which
9183 two identical pieces of code are merged and the program counter suddenly
9184 jumps to a statement that is not supposed to be executed, simply because
9185 it (and the code following) translates to the same thing as the code
9186 that @emph{was} supposed to be executed.  This effect is typically seen in
9187 sequences that end in a jump, such as a @code{goto}, a @code{return}, or
9188 a @code{break} in a C @code{^switch^switch^} statement.
9189
9190 @item
9191 @i{The ``roving variable'':} The symptom is an unexpected value in a variable.
9192 There are various reasons for this effect:
9193
9194 @itemize @bullet
9195 @item
9196 In a subprogram prologue, a parameter may not yet have been moved to its
9197 ``home''.
9198
9199 @item
9200 A variable may be dead, and its register re-used.  This is
9201 probably the most common cause.
9202
9203 @item
9204 As mentioned above, the assignment of a value to a variable may
9205 have been moved.
9206
9207 @item
9208 A variable may be eliminated entirely by value propagation or
9209 other means.  In this case, GCC may incorrectly generate debugging
9210 information for the variable
9211 @end itemize
9212
9213 @noindent
9214 In general, when an unexpected value appears for a local variable or parameter
9215 you should first ascertain if that value was actually computed by
9216 your program, as opposed to being incorrectly reported by the debugger.
9217 Record fields or
9218 array elements in an object designated by an access value
9219 are generally less of a problem, once you have ascertained that the access
9220 value is sensible.
9221 Typically, this means checking variables in the preceding code and in the
9222 calling subprogram to verify that the value observed is explainable from other
9223 values (one must apply the procedure recursively to those
9224 other values); or re-running the code and stopping a little earlier
9225 (perhaps before the call) and stepping to better see how the variable obtained
9226 the value in question; or continuing to step @emph{from} the point of the
9227 strange value to see if code motion had simply moved the variable's
9228 assignments later.
9229 @end enumerate
9230
9231 @noindent
9232 In light of such anomalies, a recommended technique is to use @option{-O0}
9233 early in the software development cycle, when extensive debugging capabilities
9234 are most needed, and then move to @option{-O1} and later @option{-O2} as
9235 the debugger becomes less critical.
9236 Whether to use the @option{^-g^/DEBUG^} switch in the release version is
9237 a release management issue.
9238 @ifclear vms
9239 Note that if you use @option{-g} you can then use the @command{strip} program
9240 on the resulting executable,
9241 which removes both debugging information and global symbols.
9242 @end ifclear
9243
9244 @node Inlining of Subprograms
9245 @subsection Inlining of Subprograms
9246
9247 @noindent
9248 A call to a subprogram in the current unit is inlined if all the
9249 following conditions are met:
9250
9251 @itemize @bullet
9252 @item
9253 The optimization level is at least @option{-O1}.
9254
9255 @item
9256 The called subprogram is suitable for inlining: It must be small enough
9257 and not contain nested subprograms or anything else that @command{gcc}
9258 cannot support in inlined subprograms.
9259
9260 @item
9261 The call occurs after the definition of the body of the subprogram.
9262
9263 @item
9264 @cindex pragma Inline
9265 @findex Inline
9266 Either @code{pragma Inline} applies to the subprogram or it is
9267 small and automatic inlining (optimization level @option{-O3}) is
9268 specified.
9269 @end itemize
9270
9271 @noindent
9272 Calls to subprograms in @code{with}'ed units are normally not inlined.
9273 To achieve this level of inlining, the following conditions must all be
9274 true:
9275
9276 @itemize @bullet
9277 @item
9278 The optimization level is at least @option{-O1}.
9279
9280 @item
9281 The called subprogram is suitable for inlining: It must be small enough
9282 and not contain nested subprograms or anything else @command{gcc} cannot
9283 support in inlined subprograms.
9284
9285 @item
9286 The call appears in a body (not in a package spec).
9287
9288 @item
9289 There is a @code{pragma Inline} for the subprogram.
9290
9291 @item
9292 @cindex @option{-gnatn} (@command{gcc})
9293 The @option{^-gnatn^/INLINE^} switch
9294 is used in the @command{gcc} command line
9295 @end itemize
9296
9297 Note that specifying the @option{-gnatn} switch causes additional
9298 compilation dependencies. Consider the following:
9299
9300 @smallexample @c ada
9301 @cartouche
9302 package R is
9303    procedure Q;
9304    pragma Inline (Q);
9305 end R;
9306 package body R is
9307    ...
9308 end R;
9309
9310 with R;
9311 procedure Main is
9312 begin
9313    ...
9314    R.Q;
9315 end Main;
9316 @end cartouche
9317 @end smallexample
9318
9319 @noindent
9320 With the default behavior (no @option{-gnatn} switch specified), the
9321 compilation of the @code{Main} procedure depends only on its own source,
9322 @file{main.adb}, and the spec of the package in file @file{r.ads}. This
9323 means that editing the body of @code{R} does not require recompiling
9324 @code{Main}.
9325
9326 On the other hand, the call @code{R.Q} is not inlined under these
9327 circumstances. If the @option{-gnatn} switch is present when @code{Main}
9328 is compiled, the call will be inlined if the body of @code{Q} is small
9329 enough, but now @code{Main} depends on the body of @code{R} in
9330 @file{r.adb} as well as on the spec. This means that if this body is edited,
9331 the main program must be recompiled. Note that this extra dependency
9332 occurs whether or not the call is in fact inlined by @command{gcc}.
9333
9334 The use of front end inlining with @option{-gnatN} generates similar
9335 additional dependencies.
9336
9337 @cindex @option{^-fno-inline^/INLINE=SUPPRESS^} (@command{gcc})
9338 Note: The @option{^-fno-inline^/INLINE=SUPPRESS^} switch
9339 can be used to prevent
9340 all inlining. This switch overrides all other conditions and ensures
9341 that no inlining occurs. The extra dependences resulting from
9342 @option{-gnatn} will still be active, even if
9343 this switch is used to suppress the resulting inlining actions.
9344
9345 Note regarding the use of @option{-O3}: There is no difference in inlining
9346 behavior between @option{-O2} and @option{-O3} for subprograms with an explicit
9347 pragma @code{Inline} assuming the use of @option{-gnatn}
9348 or @option{-gnatN} (the switches that activate inlining). If you have used
9349 pragma @code{Inline} in appropriate cases, then it is usually much better
9350 to use @option{-O2} and @option{-gnatn} and avoid the use of @option{-O3} which
9351 in this case only has the effect of inlining subprograms you did not
9352 think should be inlined. We often find that the use of @option{-O3} slows
9353 down code by performing excessive inlining, leading to increased instruction
9354 cache pressure from the increased code size. So the bottom line here is
9355 that you should not automatically assume that @option{-O3} is better than
9356 @option{-O2}, and indeed you should use @option{-O3} only if tests show that
9357 it actually improves performance.
9358
9359 @node Other Optimization Switches
9360 @subsection Other Optimization Switches
9361 @cindex Optimization Switches
9362
9363 Since @code{GNAT} uses the @code{gcc} back end, all the specialized
9364 @code{gcc} optimization switches are potentially usable. These switches
9365 have not been extensively tested with GNAT but can generally be expected
9366 to work. Examples of switches in this category are
9367 @option{-funroll-loops} and
9368 the various target-specific @option{-m} options (in particular, it has been
9369 observed that @option{-march=pentium4} can significantly improve performance
9370 on appropriate machines). For full details of these switches, see the
9371 @code{gcc} manual.
9372
9373 @node Optimization and Strict Aliasing
9374 @subsection Optimization and Strict Aliasing
9375 @cindex Aliasing
9376 @cindex Strict Aliasing
9377 @cindex No_Strict_Aliasing
9378
9379 @noindent
9380 The strong typing capabilities of Ada allow an optimizer to generate
9381 efficient code in situations where other languages would be forced to
9382 make worst case assumptions preventing such optimizations. Consider
9383 the following example:
9384
9385 @smallexample @c ada
9386 @cartouche
9387 procedure R is
9388    type Int1 is new Integer;
9389    type Int2 is new Integer;
9390    type Int1A is access Int1;
9391    type Int2A is access Int2;
9392    Int1V : Int1A;
9393    Int2V : Int2A;
9394    ...
9395
9396 begin
9397    ...
9398    for J in Data'Range loop
9399       if Data (J) = Int1V.all then
9400          Int2V.all := Int2V.all + 1;
9401       end if;
9402    end loop;
9403    ...
9404 end R;
9405 @end cartouche
9406 @end smallexample
9407
9408 @noindent
9409 In this example, since the variable @code{Int1V} can only access objects
9410 of type @code{Int1}, and @code{Int2V} can only access objects of type
9411 @code{Int2}, there is no possibility that the assignment to
9412 @code{Int2V.all} affects the value of @code{Int1V.all}. This means that
9413 the compiler optimizer can "know" that the value @code{Int1V.all} is constant
9414 for all iterations of the loop and avoid the extra memory reference
9415 required to dereference it each time through the loop.
9416
9417 This kind of optimization, called strict aliasing analysis, is
9418 triggered by specifying an optimization level of @option{-O2} or
9419 higher and allows @code{GNAT} to generate more efficient code
9420 when access values are involved.
9421
9422 However, although this optimization is always correct in terms of
9423 the formal semantics of the Ada Reference Manual, difficulties can
9424 arise if features like @code{Unchecked_Conversion} are used to break
9425 the typing system. Consider the following complete program example:
9426
9427 @smallexample @c ada
9428 @cartouche
9429 package p1 is
9430    type int1 is new integer;
9431    type int2 is new integer;
9432    type a1 is access int1;
9433    type a2 is access int2;
9434 end p1;
9435
9436 with p1; use p1;
9437 package p2 is
9438    function to_a2 (Input : a1) return a2;
9439 end p2;
9440
9441 with Unchecked_Conversion;
9442 package body p2 is
9443    function to_a2 (Input : a1) return a2 is
9444       function to_a2u is
9445         new Unchecked_Conversion (a1, a2);
9446    begin
9447       return to_a2u (Input);
9448    end to_a2;
9449 end p2;
9450
9451 with p2; use p2;
9452 with p1; use p1;
9453 with Text_IO; use Text_IO;
9454 procedure m is
9455    v1 : a1 := new int1;
9456    v2 : a2 := to_a2 (v1);
9457 begin
9458    v1.all := 1;
9459    v2.all := 0;
9460    put_line (int1'image (v1.all));
9461 end;
9462 @end cartouche
9463 @end smallexample
9464
9465 @noindent
9466 This program prints out 0 in @code{-O0} or @code{-O1}
9467 mode, but it prints out 1 in @code{-O2} mode. That's
9468 because in strict aliasing mode, the compiler can and
9469 does assume that the assignment to @code{v2.all} could not
9470 affect the value of @code{v1.all}, since different types
9471 are involved.
9472
9473 This behavior is not a case of non-conformance with the standard, since
9474 the Ada RM specifies that an unchecked conversion where the resulting
9475 bit pattern is not a correct value of the target type can result in an
9476 abnormal value and attempting to reference an abnormal value makes the
9477 execution of a program erroneous.  That's the case here since the result
9478 does not point to an object of type @code{int2}.  This means that the
9479 effect is entirely unpredictable.
9480
9481 However, although that explanation may satisfy a language
9482 lawyer, in practice an applications programmer expects an
9483 unchecked conversion involving pointers to create true
9484 aliases and the behavior of printing 1 seems plain wrong.
9485 In this case, the strict aliasing optimization is unwelcome.
9486
9487 Indeed the compiler recognizes this possibility, and the
9488 unchecked conversion generates a warning:
9489
9490 @smallexample
9491 p2.adb:5:07: warning: possible aliasing problem with type "a2"
9492 p2.adb:5:07: warning: use -fno-strict-aliasing switch for references
9493 p2.adb:5:07: warning:  or use "pragma No_Strict_Aliasing (a2);"
9494 @end smallexample
9495
9496 @noindent
9497 Unfortunately the problem is recognized when compiling the body of
9498 package @code{p2}, but the actual "bad" code is generated while
9499 compiling the body of @code{m} and this latter compilation does not see
9500 the suspicious @code{Unchecked_Conversion}.
9501
9502 As implied by the warning message, there are approaches you can use to
9503 avoid the unwanted strict aliasing optimization in a case like this.
9504
9505 One possibility is to simply avoid the use of @code{-O2}, but
9506 that is a bit drastic, since it throws away a number of useful
9507 optimizations that do not involve strict aliasing assumptions.
9508
9509 A less drastic approach is to compile the program using the
9510 option @code{-fno-strict-aliasing}. Actually it is only the
9511 unit containing the dereferencing of the suspicious pointer
9512 that needs to be compiled. So in this case, if we compile
9513 unit @code{m} with this switch, then we get the expected
9514 value of zero printed. Analyzing which units might need
9515 the switch can be painful, so a more reasonable approach
9516 is to compile the entire program with options @code{-O2}
9517 and @code{-fno-strict-aliasing}. If the performance is
9518 satisfactory with this combination of options, then the
9519 advantage is that the entire issue of possible "wrong"
9520 optimization due to strict aliasing is avoided.
9521
9522 To avoid the use of compiler switches, the configuration
9523 pragma @code{No_Strict_Aliasing} with no parameters may be
9524 used to specify that for all access types, the strict
9525 aliasing optimization should be suppressed.
9526
9527 However, these approaches are still overkill, in that they causes
9528 all manipulations of all access values to be deoptimized. A more
9529 refined approach is to concentrate attention on the specific
9530 access type identified as problematic.
9531
9532 First, if a careful analysis of uses of the pointer shows
9533 that there are no possible problematic references, then
9534 the warning can be suppressed by bracketing the
9535 instantiation of @code{Unchecked_Conversion} to turn
9536 the warning off:
9537
9538 @smallexample @c ada
9539    pragma Warnings (Off);
9540    function to_a2u is
9541      new Unchecked_Conversion (a1, a2);
9542    pragma Warnings (On);
9543 @end smallexample
9544
9545 @noindent
9546 Of course that approach is not appropriate for this particular
9547 example, since indeed there is a problematic reference. In this
9548 case we can take one of two other approaches.
9549
9550 The first possibility is to move the instantiation of unchecked
9551 conversion to the unit in which the type is declared. In
9552 this example, we would move the instantiation of
9553 @code{Unchecked_Conversion} from the body of package
9554 @code{p2} to the spec of package @code{p1}. Now the
9555 warning disappears. That's because any use of the
9556 access type knows there is a suspicious unchecked
9557 conversion, and the strict aliasing optimization
9558 is automatically suppressed for the type.
9559
9560 If it is not practical to move the unchecked conversion to the same unit
9561 in which the destination access type is declared (perhaps because the
9562 source type is not visible in that unit), you may use pragma
9563 @code{No_Strict_Aliasing} for the type. This pragma must occur in the
9564 same declarative sequence as the declaration of the access type:
9565
9566 @smallexample @c ada
9567    type a2 is access int2;
9568    pragma No_Strict_Aliasing (a2);
9569 @end smallexample
9570
9571 @noindent
9572 Here again, the compiler now knows that the strict aliasing optimization
9573 should be suppressed for any reference to type @code{a2} and the
9574 expected behavior is obtained.
9575
9576 Finally, note that although the compiler can generate warnings for
9577 simple cases of unchecked conversions, there are tricker and more
9578 indirect ways of creating type incorrect aliases which the compiler
9579 cannot detect. Examples are the use of address overlays and unchecked
9580 conversions involving composite types containing access types as
9581 components. In such cases, no warnings are generated, but there can
9582 still be aliasing problems. One safe coding practice is to forbid the
9583 use of address clauses for type overlaying, and to allow unchecked
9584 conversion only for primitive types. This is not really a significant
9585 restriction since any possible desired effect can be achieved by
9586 unchecked conversion of access values.
9587
9588 @ifset vms
9589 @node Coverage Analysis
9590 @subsection Coverage Analysis
9591
9592 @noindent
9593 GNAT supports the HP Performance Coverage Analyzer (PCA), which allows
9594 the user to determine the distribution of execution time across a program,
9595 @pxref{Profiling} for details of usage.
9596 @end ifset
9597
9598 @node Reducing the Size of Ada Executables with gnatelim
9599 @section Reducing the Size of Ada Executables with @code{gnatelim}
9600 @findex gnatelim
9601
9602 @noindent
9603 This section describes @command{gnatelim}, a tool which detects unused
9604 subprograms and helps the compiler to create a smaller executable for your
9605 program.
9606
9607 @menu
9608 * About gnatelim::
9609 * Running gnatelim::
9610 * Correcting the List of Eliminate Pragmas::
9611 * Making Your Executables Smaller::
9612 * Summary of the gnatelim Usage Cycle::
9613 @end menu
9614
9615 @node About gnatelim
9616 @subsection About @code{gnatelim}
9617
9618 @noindent
9619 When a program shares a set of Ada
9620 packages with other programs, it may happen that this program uses
9621 only a fraction of the subprograms defined in these packages. The code
9622 created for these unused subprograms increases the size of the executable.
9623
9624 @code{gnatelim} tracks unused subprograms in an Ada program and
9625 outputs a list of GNAT-specific pragmas @code{Eliminate} marking all the
9626 subprograms that are declared but never called. By placing the list of
9627 @code{Eliminate} pragmas in the GNAT configuration file @file{gnat.adc} and
9628 recompiling your program, you may decrease the size of its executable,
9629 because the compiler will not generate the code for 'eliminated' subprograms.
9630 See GNAT Reference Manual for more information about this pragma.
9631
9632 @code{gnatelim} needs as its input data the name of the main subprogram
9633 and a bind file for a main subprogram.
9634
9635 To create a bind file for @code{gnatelim}, run @code{gnatbind} for
9636 the main subprogram. @code{gnatelim} can work with both Ada and C
9637 bind files; when both are present, it uses the Ada bind file.
9638 The following commands will build the program and create the bind file:
9639
9640 @smallexample
9641 $ gnatmake ^-c Main_Prog^/ACTIONS=COMPILE MAIN_PROG^
9642 $ gnatbind main_prog
9643 @end smallexample
9644
9645 Note that @code{gnatelim} needs neither object nor ALI files.
9646
9647 @node Running gnatelim
9648 @subsection Running @code{gnatelim}
9649
9650 @noindent
9651 @code{gnatelim} has the following command-line interface:
9652
9653 @smallexample
9654 $ gnatelim [options] name
9655 @end smallexample
9656
9657 @noindent
9658 @code{name} should be a name of a source file that contains the main subprogram
9659 of a program (partition).
9660
9661 @code{gnatelim} has the following switches:
9662
9663 @table @option
9664 @c !sort!
9665 @item ^-q^/QUIET^
9666 @cindex @option{^-q^/QUIET^} (@command{gnatelim})
9667 Quiet mode: by default @code{gnatelim} outputs to the standard error
9668 stream the number of program units left to be processed. This option turns
9669 this trace off.
9670
9671 @item ^-v^/VERBOSE^
9672 @cindex @option{^-v^/VERBOSE^} (@command{gnatelim})
9673 Verbose mode: @code{gnatelim} version information is printed as Ada
9674 comments to the standard output stream. Also, in addition to the number of
9675 program units left @code{gnatelim} will output the name of the current unit
9676 being processed.
9677
9678 @item ^-a^/ALL^
9679 @cindex @option{^-a^/ALL^} (@command{gnatelim})
9680 Also look for subprograms from the GNAT run time that can be eliminated. Note
9681 that when @file{gnat.adc} is produced using this switch, the entire program
9682 must be recompiled with switch @option{^-a^/ALL_FILES^} to @command{gnatmake}.
9683
9684 @item ^-I^/INCLUDE_DIRS=^@var{dir}
9685 @cindex @option{^-I^/INCLUDE_DIRS^} (@command{gnatelim})
9686 When looking for source files also look in directory @var{dir}. Specifying
9687 @option{^-I-^/INCLUDE_DIRS=-^} instructs @code{gnatelim} not to look for
9688 sources in the current directory.
9689
9690 @item ^-b^/BIND_FILE=^@var{bind_file}
9691 @cindex @option{^-b^/BIND_FILE^} (@command{gnatelim})
9692 Specifies @var{bind_file} as the bind file to process. If not set, the name
9693 of the bind file is computed from the full expanded Ada name
9694 of a main subprogram.
9695
9696 @item ^-C^/CONFIG_FILE=^@var{config_file}
9697 @cindex @option{^-C^/CONFIG_FILE^} (@command{gnatelim})
9698 Specifies a file @var{config_file} that contains configuration pragmas. The
9699 file must be specified with full path.
9700
9701 @item ^--GCC^/COMPILER^=@var{compiler_name}
9702 @cindex @option{^-GCC^/COMPILER^} (@command{gnatelim})
9703 Instructs @code{gnatelim} to use specific @command{gcc} compiler instead of one
9704 available on the path.
9705
9706 @item ^--GNATMAKE^/GNATMAKE^=@var{gnatmake_name}
9707 @cindex @option{^--GNATMAKE^/GNATMAKE^} (@command{gnatelim})
9708 Instructs @code{gnatelim} to use specific @command{gnatmake} instead of one
9709 available on the path.
9710 @end table
9711
9712 @noindent
9713 @code{gnatelim} sends its output to the standard output stream, and all the
9714 tracing and debug information is sent to the standard error stream.
9715 In order to produce a proper GNAT configuration file
9716 @file{gnat.adc}, redirection must be used:
9717
9718 @smallexample
9719 @ifset vms
9720 $ PIPE GNAT ELIM MAIN_PROG.ADB > GNAT.ADC
9721 @end ifset
9722 @ifclear vms
9723 $ gnatelim main_prog.adb > gnat.adc
9724 @end ifclear
9725 @end smallexample
9726
9727 @ifclear vms
9728 @noindent
9729 or
9730
9731 @smallexample
9732 $ gnatelim main_prog.adb >> gnat.adc
9733 @end smallexample
9734
9735 @noindent
9736 in order to append the @code{gnatelim} output to the existing contents of
9737 @file{gnat.adc}.
9738 @end ifclear
9739
9740 @node Correcting the List of Eliminate Pragmas
9741 @subsection Correcting the List of Eliminate Pragmas
9742
9743 @noindent
9744 In some rare cases @code{gnatelim} may try to eliminate
9745 subprograms that are actually called in the program. In this case, the
9746 compiler will generate an error message of the form:
9747
9748 @smallexample
9749 file.adb:106:07: cannot call eliminated subprogram "My_Prog"
9750 @end smallexample
9751
9752 @noindent
9753 You will need to manually remove the wrong @code{Eliminate} pragmas from
9754 the @file{gnat.adc} file. You should recompile your program
9755 from scratch after that, because you need a consistent @file{gnat.adc} file
9756 during the entire compilation.
9757
9758 @node Making Your Executables Smaller
9759 @subsection Making Your Executables Smaller
9760
9761 @noindent
9762 In order to get a smaller executable for your program you now have to
9763 recompile the program completely with the new @file{gnat.adc} file
9764 created by @code{gnatelim} in your current directory:
9765
9766 @smallexample
9767 $ gnatmake ^-f main_prog^/FORCE_COMPILE MAIN_PROG^
9768 @end smallexample
9769
9770 @noindent
9771 (Use the @option{^-f^/FORCE_COMPILE^} option for @command{gnatmake} to
9772 recompile everything
9773 with the set of pragmas @code{Eliminate} that you have obtained with
9774 @command{gnatelim}).
9775
9776 Be aware that the set of @code{Eliminate} pragmas is specific to each
9777 program. It is not recommended to merge sets of @code{Eliminate}
9778 pragmas created for different programs in one @file{gnat.adc} file.
9779
9780 @node Summary of the gnatelim Usage Cycle
9781 @subsection Summary of the gnatelim Usage Cycle
9782
9783 @noindent
9784 Here is a quick summary of the steps to be taken in order to reduce
9785 the size of your executables with @code{gnatelim}. You may use
9786 other GNAT options to control the optimization level,
9787 to produce the debugging information, to set search path, etc.
9788
9789 @enumerate
9790 @item
9791 Produce a bind file
9792
9793 @smallexample
9794 $ gnatmake ^-c main_prog^/ACTIONS=COMPILE MAIN_PROG^
9795 $ gnatbind main_prog
9796 @end smallexample
9797
9798 @item
9799 Generate a list of @code{Eliminate} pragmas
9800 @smallexample
9801 @ifset vms
9802 $ PIPE GNAT ELIM MAIN_PROG > GNAT.ADC
9803 @end ifset
9804 @ifclear vms
9805 $ gnatelim main_prog >[>] gnat.adc
9806 @end ifclear
9807 @end smallexample
9808
9809 @item
9810 Recompile the application
9811
9812 @smallexample
9813 $ gnatmake ^-f main_prog^/FORCE_COMPILE MAIN_PROG^
9814 @end smallexample
9815
9816 @end enumerate
9817
9818 @c ********************************
9819 @node Renaming Files Using gnatchop
9820 @chapter Renaming Files Using @code{gnatchop}
9821 @findex gnatchop
9822
9823 @noindent
9824 This chapter discusses how to handle files with multiple units by using
9825 the @code{gnatchop} utility. This utility is also useful in renaming
9826 files to meet the standard GNAT default file naming conventions.
9827
9828 @menu
9829 * Handling Files with Multiple Units::
9830 * Operating gnatchop in Compilation Mode::
9831 * Command Line for gnatchop::
9832 * Switches for gnatchop::
9833 * Examples of gnatchop Usage::
9834 @end menu
9835
9836 @node Handling Files with Multiple Units
9837 @section Handling Files with Multiple Units
9838
9839 @noindent
9840 The basic compilation model of GNAT requires that a file submitted to the
9841 compiler have only one unit and there be a strict correspondence
9842 between the file name and the unit name.
9843
9844 The @code{gnatchop} utility allows both of these rules to be relaxed,
9845 allowing GNAT to process files which contain multiple compilation units
9846 and files with arbitrary file names. @code{gnatchop}
9847 reads the specified file and generates one or more output files,
9848 containing one unit per file. The unit and the file name correspond,
9849 as required by GNAT.
9850
9851 If you want to permanently restructure a set of ``foreign'' files so that
9852 they match the GNAT rules, and do the remaining development using the
9853 GNAT structure, you can simply use @command{gnatchop} once, generate the
9854 new set of files and work with them from that point on.
9855
9856 Alternatively, if you want to keep your files in the ``foreign'' format,
9857 perhaps to maintain compatibility with some other Ada compilation
9858 system, you can set up a procedure where you use @command{gnatchop} each
9859 time you compile, regarding the source files that it writes as temporary
9860 files that you throw away.
9861
9862 @node Operating gnatchop in Compilation Mode
9863 @section Operating gnatchop in Compilation Mode
9864
9865 @noindent
9866 The basic function of @code{gnatchop} is to take a file with multiple units
9867 and split it into separate files. The boundary between files is reasonably
9868 clear, except for the issue of comments and pragmas. In default mode, the
9869 rule is that any pragmas between units belong to the previous unit, except
9870 that configuration pragmas always belong to the following unit. Any comments
9871 belong to the following unit. These rules
9872 almost always result in the right choice of
9873 the split point without needing to mark it explicitly and most users will
9874 find this default to be what they want. In this default mode it is incorrect to
9875 submit a file containing only configuration pragmas, or one that ends in
9876 configuration pragmas, to @code{gnatchop}.
9877
9878 However, using a special option to activate ``compilation mode'',
9879 @code{gnatchop}
9880 can perform another function, which is to provide exactly the semantics
9881 required by the RM for handling of configuration pragmas in a compilation.
9882 In the absence of configuration pragmas (at the main file level), this
9883 option has no effect, but it causes such configuration pragmas to be handled
9884 in a quite different manner.
9885
9886 First, in compilation mode, if @code{gnatchop} is given a file that consists of
9887 only configuration pragmas, then this file is appended to the
9888 @file{gnat.adc} file in the current directory. This behavior provides
9889 the required behavior described in the RM for the actions to be taken
9890 on submitting such a file to the compiler, namely that these pragmas
9891 should apply to all subsequent compilations in the same compilation
9892 environment. Using GNAT, the current directory, possibly containing a
9893 @file{gnat.adc} file is the representation
9894 of a compilation environment. For more information on the
9895 @file{gnat.adc} file, see @ref{Handling of Configuration Pragmas}.
9896
9897 Second, in compilation mode, if @code{gnatchop}
9898 is given a file that starts with
9899 configuration pragmas, and contains one or more units, then these
9900 configuration pragmas are prepended to each of the chopped files. This
9901 behavior provides the required behavior described in the RM for the
9902 actions to be taken on compiling such a file, namely that the pragmas
9903 apply to all units in the compilation, but not to subsequently compiled
9904 units.
9905
9906 Finally, if configuration pragmas appear between units, they are appended
9907 to the previous unit. This results in the previous unit being illegal,
9908 since the compiler does not accept configuration pragmas that follow
9909 a unit. This provides the required RM behavior that forbids configuration
9910 pragmas other than those preceding the first compilation unit of a
9911 compilation.
9912
9913 For most purposes, @code{gnatchop} will be used in default mode. The
9914 compilation mode described above is used only if you need exactly
9915 accurate behavior with respect to compilations, and you have files
9916 that contain multiple units and configuration pragmas. In this
9917 circumstance the use of @code{gnatchop} with the compilation mode
9918 switch provides the required behavior, and is for example the mode
9919 in which GNAT processes the ACVC tests.
9920
9921 @node Command Line for gnatchop
9922 @section Command Line for @code{gnatchop}
9923
9924 @noindent
9925 The @code{gnatchop} command has the form:
9926
9927 @smallexample
9928 $ gnatchop switches @var{file name} [@var{file name} @var{file name} ...]
9929       [@var{directory}]
9930 @end smallexample
9931
9932 @noindent
9933 The only required argument is the file name of the file to be chopped.
9934 There are no restrictions on the form of this file name. The file itself
9935 contains one or more Ada units, in normal GNAT format, concatenated
9936 together. As shown, more than one file may be presented to be chopped.
9937
9938 When run in default mode, @code{gnatchop} generates one output file in
9939 the current directory for each unit in each of the files.
9940
9941 @var{directory}, if specified, gives the name of the directory to which
9942 the output files will be written. If it is not specified, all files are
9943 written to the current directory.
9944
9945 For example, given a
9946 file called @file{hellofiles} containing
9947
9948 @smallexample @c ada
9949 @group
9950 @cartouche
9951 procedure hello;
9952
9953 with Text_IO; use Text_IO;
9954 procedure hello is
9955 begin
9956    Put_Line ("Hello");
9957 end hello;
9958 @end cartouche
9959 @end group
9960 @end smallexample
9961
9962 @noindent
9963 the command
9964
9965 @smallexample
9966 $ gnatchop ^hellofiles^HELLOFILES.^
9967 @end smallexample
9968
9969 @noindent
9970 generates two files in the current directory, one called
9971 @file{hello.ads} containing the single line that is the procedure spec,
9972 and the other called @file{hello.adb} containing the remaining text. The
9973 original file is not affected. The generated files can be compiled in
9974 the normal manner.
9975
9976 @noindent
9977 When gnatchop is invoked on a file that is empty or that contains only empty
9978 lines and/or comments, gnatchop will not fail, but will not produce any
9979 new sources.
9980
9981 For example, given a
9982 file called @file{toto.txt} containing
9983
9984 @smallexample @c ada
9985 @group
9986 @cartouche
9987 --  Just a comment
9988 @end cartouche
9989 @end group
9990 @end smallexample
9991
9992 @noindent
9993 the command
9994
9995 @smallexample
9996 $ gnatchop ^toto.txt^TOT.TXT^
9997 @end smallexample
9998
9999 @noindent
10000 will not produce any new file and will result in the following warnings:
10001
10002 @smallexample
10003 toto.txt:1:01: warning: empty file, contains no compilation units
10004 no compilation units found
10005 no source files written
10006 @end smallexample
10007
10008 @node Switches for gnatchop
10009 @section Switches for @code{gnatchop}
10010
10011 @noindent
10012 @command{gnatchop} recognizes the following switches:
10013
10014 @table @option
10015 @c !sort!
10016
10017 @item ^-c^/COMPILATION^
10018 @cindex @option{^-c^/COMPILATION^} (@code{gnatchop})
10019 Causes @code{gnatchop} to operate in compilation mode, in which
10020 configuration pragmas are handled according to strict RM rules. See
10021 previous section for a full description of this mode.
10022
10023 @ifclear vms
10024 @item -gnatxxx
10025 This passes the given @option{-gnatxxx} switch to @code{gnat} which is
10026 used to parse the given file. Not all @code{xxx} options make sense,
10027 but for example, the use of @option{-gnati2} allows @code{gnatchop} to
10028 process a source file that uses Latin-2 coding for identifiers.
10029 @end ifclear
10030
10031 @item ^-h^/HELP^
10032 Causes @code{gnatchop} to generate a brief help summary to the standard
10033 output file showing usage information.
10034
10035 @item ^-k@var{mm}^/FILE_NAME_MAX_LENGTH=@var{mm}^
10036 @cindex @option{^-k^/FILE_NAME_MAX_LENGTH^} (@code{gnatchop})
10037 Limit generated file names to the specified number @code{mm}
10038 of characters.
10039 This is useful if the
10040 resulting set of files is required to be interoperable with systems
10041 which limit the length of file names.
10042 @ifset vms
10043 If no value is given, or
10044 if no @code{/FILE_NAME_MAX_LENGTH} qualifier is given,
10045 a default of 39, suitable for OpenVMS Alpha
10046 Systems, is assumed
10047 @end ifset
10048 @ifclear vms
10049 No space is allowed between the @option{-k} and the numeric value. The numeric
10050 value may be omitted in which case a default of @option{-k8},
10051 suitable for use
10052 with DOS-like file systems, is used. If no @option{-k} switch
10053 is present then
10054 there is no limit on the length of file names.
10055 @end ifclear
10056
10057 @item ^-p^/PRESERVE^
10058 @cindex @option{^-p^/PRESERVE^} (@code{gnatchop})
10059 Causes the file ^modification^creation^ time stamp of the input file to be
10060 preserved and used for the time stamp of the output file(s). This may be
10061 useful for preserving coherency of time stamps in an environment where
10062 @code{gnatchop} is used as part of a standard build process.
10063
10064 @item ^-q^/QUIET^
10065 @cindex @option{^-q^/QUIET^} (@code{gnatchop})
10066 Causes output of informational messages indicating the set of generated
10067 files to be suppressed. Warnings and error messages are unaffected.
10068
10069 @item ^-r^/REFERENCE^
10070 @cindex @option{^-r^/REFERENCE^} (@code{gnatchop})
10071 @findex Source_Reference
10072 Generate @code{Source_Reference} pragmas. Use this switch if the output
10073 files are regarded as temporary and development is to be done in terms
10074 of the original unchopped file. This switch causes
10075 @code{Source_Reference} pragmas to be inserted into each of the
10076 generated files to refers back to the original file name and line number.
10077 The result is that all error messages refer back to the original
10078 unchopped file.
10079 In addition, the debugging information placed into the object file (when
10080 the @option{^-g^/DEBUG^} switch of @command{gcc} or @command{gnatmake} is
10081 specified)
10082 also refers back to this original file so that tools like profilers and
10083 debuggers will give information in terms of the original unchopped file.
10084
10085 If the original file to be chopped itself contains
10086 a @code{Source_Reference}
10087 pragma referencing a third file, then gnatchop respects
10088 this pragma, and the generated @code{Source_Reference} pragmas
10089 in the chopped file refer to the original file, with appropriate
10090 line numbers. This is particularly useful when @code{gnatchop}
10091 is used in conjunction with @code{gnatprep} to compile files that
10092 contain preprocessing statements and multiple units.
10093
10094 @item ^-v^/VERBOSE^
10095 @cindex @option{^-v^/VERBOSE^} (@code{gnatchop})
10096 Causes @code{gnatchop} to operate in verbose mode. The version
10097 number and copyright notice are output, as well as exact copies of
10098 the gnat1 commands spawned to obtain the chop control information.
10099
10100 @item ^-w^/OVERWRITE^
10101 @cindex @option{^-w^/OVERWRITE^} (@code{gnatchop})
10102 Overwrite existing file names. Normally @code{gnatchop} regards it as a
10103 fatal error if there is already a file with the same name as a
10104 file it would otherwise output, in other words if the files to be
10105 chopped contain duplicated units. This switch bypasses this
10106 check, and causes all but the last instance of such duplicated
10107 units to be skipped.
10108
10109 @ifclear vms
10110 @item --GCC=xxxx
10111 @cindex @option{--GCC=} (@code{gnatchop})
10112 Specify the path of the GNAT parser to be used. When this switch is used,
10113 no attempt is made to add the prefix to the GNAT parser executable.
10114 @end ifclear
10115 @end table
10116
10117 @node Examples of gnatchop Usage
10118 @section Examples of @code{gnatchop} Usage
10119
10120 @table @code
10121 @ifset vms
10122 @item gnatchop /OVERWRITE HELLO_S.ADA [PRERELEASE.FILES]
10123 @end ifset
10124 @ifclear vms
10125 @item gnatchop -w hello_s.ada prerelease/files
10126 @end ifclear
10127
10128 Chops the source file @file{hello_s.ada}. The output files will be
10129 placed in the directory @file{^prerelease/files^[PRERELEASE.FILES]^},
10130 overwriting any
10131 files with matching names in that directory (no files in the current
10132 directory are modified).
10133
10134 @item gnatchop ^archive^ARCHIVE.^
10135 Chops the source file @file{^archive^ARCHIVE.^}
10136 into the current directory. One
10137 useful application of @code{gnatchop} is in sending sets of sources
10138 around, for example in email messages. The required sources are simply
10139 concatenated (for example, using a ^Unix @code{cat}^VMS @code{APPEND/NEW}^
10140 command), and then
10141 @code{gnatchop} is used at the other end to reconstitute the original
10142 file names.
10143
10144 @item gnatchop file1 file2 file3 direc
10145 Chops all units in files @file{file1}, @file{file2}, @file{file3}, placing
10146 the resulting files in the directory @file{direc}. Note that if any units
10147 occur more than once anywhere within this set of files, an error message
10148 is generated, and no files are written. To override this check, use the
10149 @option{^-w^/OVERWRITE^} switch,
10150 in which case the last occurrence in the last file will
10151 be the one that is output, and earlier duplicate occurrences for a given
10152 unit will be skipped.
10153 @end table
10154
10155 @node Configuration Pragmas
10156 @chapter Configuration Pragmas
10157 @cindex Configuration pragmas
10158 @cindex Pragmas, configuration
10159
10160 @noindent
10161 In Ada 95, configuration pragmas include those pragmas described as
10162 such in the Ada 95 Reference Manual, as well as
10163 implementation-dependent pragmas that are configuration pragmas. See the
10164 individual descriptions of pragmas in the GNAT Reference Manual for
10165 details on these additional GNAT-specific configuration pragmas. Most
10166 notably, the pragma @code{Source_File_Name}, which allows
10167 specifying non-default names for source files, is a configuration
10168 pragma. The following is a complete list of configuration pragmas
10169 recognized by @code{GNAT}:
10170
10171 @smallexample
10172    Ada_83
10173    Ada_95
10174    Ada_05
10175    C_Pass_By_Copy
10176    Component_Alignment
10177    Detect_Blocking
10178    Discard_Names
10179    Elaboration_Checks
10180    Eliminate
10181    Extend_System
10182    External_Name_Casing
10183    Float_Representation
10184    Initialize_Scalars
10185    License
10186    Locking_Policy
10187    Long_Float
10188    Normalize_Scalars
10189    Polling
10190    Profile
10191    Profile_Warnings
10192    Propagate_Exceptions
10193    Queuing_Policy
10194    Ravenscar
10195    Restricted_Run_Time
10196    Restrictions
10197    Restrictions_Warnings
10198    Reviewable
10199    Source_File_Name
10200    Style_Checks
10201    Suppress
10202    Task_Dispatching_Policy
10203    Universal_Data
10204    Unsuppress
10205    Use_VADS_Size
10206    Warnings
10207    Validity_Checks
10208 @end smallexample
10209
10210 @menu
10211 * Handling of Configuration Pragmas::
10212 * The Configuration Pragmas Files::
10213 @end menu
10214
10215 @node Handling of Configuration Pragmas
10216 @section Handling of Configuration Pragmas
10217
10218 Configuration pragmas may either appear at the start of a compilation
10219 unit, in which case they apply only to that unit, or they may apply to
10220 all compilations performed in a given compilation environment.
10221
10222 GNAT also provides the @code{gnatchop} utility to provide an automatic
10223 way to handle configuration pragmas following the semantics for
10224 compilations (that is, files with multiple units), described in the RM.
10225 See @ref{Operating gnatchop in Compilation Mode} for details.
10226 However, for most purposes, it will be more convenient to edit the
10227 @file{gnat.adc} file that contains configuration pragmas directly,
10228 as described in the following section.
10229
10230 @node The Configuration Pragmas Files
10231 @section The Configuration Pragmas Files
10232 @cindex @file{gnat.adc}
10233
10234 @noindent
10235 In GNAT a compilation environment is defined by the current
10236 directory at the time that a compile command is given. This current
10237 directory is searched for a file whose name is @file{gnat.adc}. If
10238 this file is present, it is expected to contain one or more
10239 configuration pragmas that will be applied to the current compilation.
10240 However, if the switch @option{-gnatA} is used, @file{gnat.adc} is not
10241 considered.
10242
10243 Configuration pragmas may be entered into the @file{gnat.adc} file
10244 either by running @code{gnatchop} on a source file that consists only of
10245 configuration pragmas, or more conveniently  by
10246 direct editing of the @file{gnat.adc} file, which is a standard format
10247 source file.
10248
10249 In addition to @file{gnat.adc}, one additional file containing configuration
10250 pragmas may be applied to the current compilation using the switch
10251 @option{-gnatec}@var{path}. @var{path} must designate an existing file that
10252 contains only configuration pragmas. These configuration pragmas are
10253 in addition to those found in @file{gnat.adc} (provided @file{gnat.adc}
10254 is present and switch @option{-gnatA} is not used).
10255
10256 It is allowed to specify several switches @option{-gnatec}, however only
10257 the last one on the command line will be taken into account.
10258
10259 If you are using project file, a separate mechanism is provided using
10260 project attributes, see @ref{Specifying Configuration Pragmas} for more
10261 details.
10262
10263 @ifset vms
10264 Of special interest to GNAT OpenVMS Alpha is the following
10265 configuration pragma:
10266
10267 @smallexample @c ada
10268 @cartouche
10269 pragma Extend_System (Aux_DEC);
10270 @end cartouche
10271 @end smallexample
10272
10273 @noindent
10274 In the presence of this pragma, GNAT adds to the definition of the
10275 predefined package SYSTEM all the additional types and subprograms that are
10276 defined in HP Ada. See @ref{Compatibility with HP Ada} for details.
10277 @end ifset
10278
10279 @node Handling Arbitrary File Naming Conventions Using gnatname
10280 @chapter Handling Arbitrary File Naming Conventions Using @code{gnatname}
10281 @cindex Arbitrary File Naming Conventions
10282
10283 @menu
10284 * Arbitrary File Naming Conventions::
10285 * Running gnatname::
10286 * Switches for gnatname::
10287 * Examples of gnatname Usage::
10288 @end menu
10289
10290 @node Arbitrary File Naming Conventions
10291 @section Arbitrary File Naming Conventions
10292
10293 @noindent
10294 The GNAT compiler must be able to know the source file name of a compilation
10295 unit.  When using the standard GNAT default file naming conventions
10296 (@code{.ads} for specs, @code{.adb} for bodies), the GNAT compiler
10297 does not need additional information.
10298
10299 @noindent
10300 When the source file names do not follow the standard GNAT default file naming
10301 conventions, the GNAT compiler must be given additional information through
10302 a configuration pragmas file (@pxref{Configuration Pragmas})
10303 or a project file.
10304 When the non standard file naming conventions are well-defined,
10305 a small number of pragmas @code{Source_File_Name} specifying a naming pattern
10306 (@pxref{Alternative File Naming Schemes}) may be sufficient. However,
10307 if the file naming conventions are irregular or arbitrary, a number
10308 of pragma @code{Source_File_Name} for individual compilation units
10309 must be defined.
10310 To help maintain the correspondence between compilation unit names and
10311 source file names within the compiler,
10312 GNAT provides a tool @code{gnatname} to generate the required pragmas for a
10313 set of files.
10314
10315 @node Running gnatname
10316 @section Running @code{gnatname}
10317
10318 @noindent
10319 The usual form of the @code{gnatname} command is
10320
10321 @smallexample
10322 $ gnatname [@var{switches}] @var{naming_pattern} [@var{naming_patterns}]
10323 @end smallexample
10324
10325 @noindent
10326 All of the arguments are optional. If invoked without any argument,
10327 @code{gnatname} will display its usage.
10328
10329 @noindent
10330 When used with at least one naming pattern, @code{gnatname} will attempt to
10331 find all the compilation units in files that follow at least one of the
10332 naming patterns. To find these compilation units,
10333 @code{gnatname} will use the GNAT compiler in syntax-check-only mode on all
10334 regular files.
10335
10336 @noindent
10337 One or several Naming Patterns may be given as arguments to @code{gnatname}.
10338 Each Naming Pattern is enclosed between double quotes.
10339 A Naming Pattern is a regular expression similar to the wildcard patterns
10340 used in file names by the Unix shells or the DOS prompt.
10341
10342 @noindent
10343 Examples of Naming Patterns are
10344
10345 @smallexample
10346    "*.[12].ada"
10347    "*.ad[sb]*"
10348    "body_*"    "spec_*"
10349 @end smallexample
10350
10351 @noindent
10352 For a more complete description of the syntax of Naming Patterns,
10353 see the second kind of regular expressions described in @file{g-regexp.ads}
10354 (the ``Glob'' regular expressions).
10355
10356 @noindent
10357 When invoked with no switches, @code{gnatname} will create a configuration
10358 pragmas file @file{gnat.adc} in the current working directory, with pragmas
10359 @code{Source_File_Name} for each file that contains a valid Ada unit.
10360
10361 @node Switches for gnatname
10362 @section Switches for @code{gnatname}
10363
10364 @noindent
10365 Switches for @code{gnatname} must precede any specified Naming Pattern.
10366
10367 @noindent
10368 You may specify any of the following switches to @code{gnatname}:
10369
10370 @table @option
10371 @c !sort!
10372
10373 @item ^-c^/CONFIG_FILE=^@file{file}
10374 @cindex @option{^-c^/CONFIG_FILE^} (@code{gnatname})
10375 Create a configuration pragmas file @file{file} (instead of the default
10376 @file{gnat.adc}).
10377 @ifclear vms
10378 There may be zero, one or more space between @option{-c} and
10379 @file{file}.
10380 @end ifclear
10381 @file{file} may include directory information. @file{file} must be
10382 writable. There may be only one switch @option{^-c^/CONFIG_FILE^}.
10383 When a switch @option{^-c^/CONFIG_FILE^} is
10384 specified, no switch @option{^-P^/PROJECT_FILE^} may be specified (see below).
10385
10386 @item ^-d^/SOURCE_DIRS=^@file{dir}
10387 @cindex @option{^-d^/SOURCE_DIRS^} (@code{gnatname})
10388 Look for source files in directory @file{dir}. There may be zero, one or more
10389 spaces between @option{^-d^/SOURCE_DIRS=^} and @file{dir}.
10390 When a switch @option{^-d^/SOURCE_DIRS^}
10391 is specified, the current working directory will not be searched for source
10392 files, unless it is explicitly specified with a @option{^-d^/SOURCE_DIRS^}
10393 or @option{^-D^/DIR_FILES^} switch.
10394 Several switches @option{^-d^/SOURCE_DIRS^} may be specified.
10395 If @file{dir} is a relative path, it is relative to the directory of
10396 the configuration pragmas file specified with switch
10397 @option{^-c^/CONFIG_FILE^},
10398 or to the directory of the project file specified with switch
10399 @option{^-P^/PROJECT_FILE^} or,
10400 if neither switch @option{^-c^/CONFIG_FILE^}
10401 nor switch @option{^-P^/PROJECT_FILE^} are specified, it is relative to the
10402 current working directory. The directory
10403 specified with switch @option{^-d^/SOURCE_DIRS^} must exist and be readable.
10404
10405 @item ^-D^/DIRS_FILE=^@file{file}
10406 @cindex @option{^-D^/DIRS_FILE^} (@code{gnatname})
10407 Look for source files in all directories listed in text file @file{file}.
10408 There may be zero, one or more spaces between @option{^-D^/DIRS_FILE=^}
10409 and @file{file}.
10410 @file{file} must be an existing, readable text file.
10411 Each non empty line in @file{file} must be a directory.
10412 Specifying switch @option{^-D^/DIRS_FILE^} is equivalent to specifying as many
10413 switches @option{^-d^/SOURCE_DIRS^} as there are non empty lines in
10414 @file{file}.
10415
10416 @item ^-f^/FOREIGN_PATTERN=^@file{pattern}
10417 @cindex @option{^-f^/FOREIGN_PATTERN^} (@code{gnatname})
10418 Foreign patterns. Using this switch, it is possible to add sources of languages
10419 other than Ada to the list of sources of a project file.
10420 It is only useful if a ^-P^/PROJECT_FILE^ switch is used.
10421 For example,
10422 @smallexample
10423 gnatname ^-Pprj -f"*.c"^/PROJECT_FILE=PRJ /FOREIGN_PATTERN=*.C^ "*.ada"
10424 @end smallexample
10425 @noindent
10426 will look for Ada units in all files with the @file{.ada} extension,
10427 and will add to the list of file for project @file{prj.gpr} the C files
10428 with extension ".^c^C^".
10429
10430 @item ^-h^/HELP^
10431 @cindex @option{^-h^/HELP^} (@code{gnatname})
10432 Output usage (help) information. The output is written to @file{stdout}.
10433
10434 @item ^-P^/PROJECT_FILE=^@file{proj}
10435 @cindex @option{^-P^/PROJECT_FILE^} (@code{gnatname})
10436 Create or update project file @file{proj}. There may be zero, one or more space
10437 between @option{-P} and @file{proj}. @file{proj} may include directory
10438 information. @file{proj} must be writable.
10439 There may be only one switch @option{^-P^/PROJECT_FILE^}.
10440 When a switch @option{^-P^/PROJECT_FILE^} is specified,
10441 no switch @option{^-c^/CONFIG_FILE^} may be specified.
10442
10443 @item ^-v^/VERBOSE^
10444 @cindex @option{^-v^/VERBOSE^} (@code{gnatname})
10445 Verbose mode. Output detailed explanation of behavior to @file{stdout}.
10446 This includes name of the file written, the name of the directories to search
10447 and, for each file in those directories whose name matches at least one of
10448 the Naming Patterns, an indication of whether the file contains a unit,
10449 and if so the name of the unit.
10450
10451 @item ^-v -v^/VERBOSE /VERBOSE^
10452 @cindex @option{^-v -v^/VERBOSE /VERBOSE^} (@code{gnatname})
10453 Very Verbose mode. In addition to the output produced in verbose mode,
10454 for each file in the searched directories whose name matches none of
10455 the Naming Patterns, an indication is given that there is no match.
10456
10457 @item ^-x^/EXCLUDED_PATTERN=^@file{pattern}
10458 @cindex @option{^-x^/EXCLUDED_PATTERN^} (@code{gnatname})
10459 Excluded patterns. Using this switch, it is possible to exclude some files
10460 that would match the name patterns. For example,
10461 @smallexample
10462 gnatname ^-x "*_nt.ada"^/EXCLUDED_PATTERN=*_nt.ada^ "*.ada"
10463 @end smallexample
10464 @noindent
10465 will look for Ada units in all files with the @file{.ada} extension,
10466 except those whose names end with @file{_nt.ada}.
10467
10468 @end table
10469
10470 @node Examples of gnatname Usage
10471 @section Examples of @code{gnatname} Usage
10472
10473 @ifset vms
10474 @smallexample
10475 $ gnatname /CONFIG_FILE=[HOME.ME]NAMES.ADC /SOURCE_DIRS=SOURCES "[a-z]*.ada*"
10476 @end smallexample
10477 @end ifset
10478
10479 @ifclear vms
10480 @smallexample
10481 $ gnatname -c /home/me/names.adc -d sources "[a-z]*.ada*"
10482 @end smallexample
10483 @end ifclear
10484
10485 @noindent
10486 In this example, the directory @file{^/home/me^[HOME.ME]^} must already exist
10487 and be writable. In addition, the directory
10488 @file{^/home/me/sources^[HOME.ME.SOURCES]^} (specified by
10489 @option{^-d sources^/SOURCE_DIRS=SOURCES^}) must exist and be readable.
10490
10491 @ifclear vms
10492 Note the optional spaces after @option{-c} and @option{-d}.
10493 @end ifclear
10494
10495 @smallexample
10496 @ifclear vms
10497 $ gnatname -P/home/me/proj -x "*_nt_body.ada"
10498   -dsources -dsources/plus -Dcommon_dirs.txt "body_*" "spec_*"
10499 @end ifclear
10500 @ifset vms
10501 $ gnatname  /PROJECT_FILE=[HOME.ME]PROJ
10502   /EXCLUDED_PATTERN=*_nt_body.ada
10503   /SOURCE_DIRS=(SOURCES,[SOURCES.PLUS])
10504   /DIRS_FILE=COMMON_DIRS.TXT "body_*" "spec_*"
10505 @end ifset
10506 @end smallexample
10507
10508 Note that several switches @option{^-d^/SOURCE_DIRS^} may be used,
10509 even in conjunction with one or several switches
10510 @option{^-D^/DIRS_FILE^}. Several Naming Patterns and one excluded pattern
10511 are used in this example.
10512
10513 @c *****************************************
10514 @c * G N A T  P r o j e c t  M a n a g e r *
10515 @c *****************************************
10516 @node GNAT Project Manager
10517 @chapter GNAT Project Manager
10518
10519 @menu
10520 * Introduction::
10521 * Examples of Project Files::
10522 * Project File Syntax::
10523 * Objects and Sources in Project Files::
10524 * Importing Projects::
10525 * Project Extension::
10526 * Project Hierarchy Extension::
10527 * External References in Project Files::
10528 * Packages in Project Files::
10529 * Variables from Imported Projects::
10530 * Naming Schemes::
10531 * Library Projects::
10532 * Stand-alone Library Projects::
10533 * Switches Related to Project Files::
10534 * Tools Supporting Project Files::
10535 * An Extended Example::
10536 * Project File Complete Syntax::
10537 @end menu
10538
10539 @c ****************
10540 @c * Introduction *
10541 @c ****************
10542
10543 @node Introduction
10544 @section Introduction
10545
10546 @noindent
10547 This chapter describes GNAT's @emph{Project Manager}, a facility that allows
10548 you to manage complex builds involving a number of source files, directories,
10549 and compilation options for different system configurations. In particular,
10550 project files allow you to specify:
10551 @itemize @bullet
10552 @item
10553 The directory or set of directories containing the source files, and/or the
10554 names of the specific source files themselves
10555 @item
10556 The directory in which the compiler's output
10557 (@file{ALI} files, object files, tree files) is to be placed
10558 @item
10559 The directory in which the executable programs is to be placed
10560 @item
10561 ^Switch^Switch^ settings for any of the project-enabled tools
10562 (@command{gnatmake}, compiler, binder, linker, @code{gnatls}, @code{gnatxref},
10563 @code{gnatfind}); you can apply these settings either globally or to individual
10564 compilation units.
10565 @item
10566 The source files containing the main subprogram(s) to be built
10567 @item
10568 The source programming language(s) (currently Ada and/or C)
10569 @item
10570 Source file naming conventions; you can specify these either globally or for
10571 individual compilation units
10572 @end itemize
10573
10574 @menu
10575 * Project Files::
10576 @end menu
10577
10578 @node Project Files
10579 @subsection Project Files
10580
10581 @noindent
10582 Project files are written in a syntax close to that of Ada, using  familiar
10583 notions such as packages, context clauses, declarations, default values,
10584 assignments, and inheritance. Finally, project files can be built
10585 hierarchically from other project files, simplifying complex system
10586 integration and project reuse.
10587
10588 A @dfn{project} is a specific set of values for various compilation properties.
10589 The settings for a given project are described by means of
10590 a @dfn{project file}, which is a text file written in an Ada-like syntax.
10591 Property values in project files are either strings or lists of strings.
10592 Properties that are not explicitly set receive default values.  A project
10593 file may interrogate the values of @dfn{external variables} (user-defined
10594 command-line switches or environment variables), and it may specify property
10595 settings conditionally, based on the value of such variables.
10596
10597 In simple cases, a project's source files depend only on other source files
10598 in the same project, or on the predefined libraries.  (@emph{Dependence} is
10599 used in
10600 the Ada technical sense; as in one Ada unit @code{with}ing another.)  However,
10601 the Project Manager also allows more sophisticated arrangements,
10602 where the source files in one project depend on source files in other
10603 projects:
10604 @itemize @bullet
10605 @item
10606 One project can @emph{import} other projects containing needed source files.
10607 @item
10608 You can organize GNAT projects in a hierarchy: a @emph{child} project
10609 can extend a @emph{parent} project, inheriting the parent's source files and
10610 optionally overriding any of them with alternative versions
10611 @end itemize
10612
10613 @noindent
10614 More generally, the Project Manager lets you structure large development
10615 efforts into hierarchical subsystems, where build decisions are delegated
10616 to the subsystem level, and thus different compilation environments
10617 (^switch^switch^ settings) used for different subsystems.
10618
10619 The Project Manager is invoked through the
10620 @option{^-P^/PROJECT_FILE=^@emph{projectfile}}
10621 switch to @command{gnatmake} or to the @command{^gnat^GNAT^} front driver.
10622 @ifclear vms
10623 There may be zero, one or more spaces between @option{-P} and
10624 @option{@emph{projectfile}}.
10625 @end ifclear
10626 If you want to define (on the command line) an external variable that is
10627 queried by the project file, you must use the
10628 @option{^-X^/EXTERNAT_REFERENCE=^@emph{vbl}=@emph{value}} switch.
10629 The Project Manager parses and interprets the project file, and drives the
10630 invoked tool based on the project settings.
10631
10632 The Project Manager supports a wide range of development strategies,
10633 for systems of all sizes.  Here are some typical practices that are
10634 easily handled:
10635 @itemize @bullet
10636 @item
10637 Using a common set of source files, but generating object files in different
10638 directories via different ^switch^switch^ settings
10639 @item
10640 Using a mostly-shared set of source files, but with different versions of
10641 some unit or units
10642 @end itemize
10643
10644 @noindent
10645 The destination of an executable can be controlled inside a project file
10646 using the @option{^-o^-o^}
10647 ^switch^switch^.
10648 In the absence of such a ^switch^switch^ either inside
10649 the project file or on the command line, any executable files generated by
10650 @command{gnatmake} are placed in the directory @code{Exec_Dir} specified
10651 in the project file. If no @code{Exec_Dir} is specified, they will be placed
10652 in the object directory of the project.
10653
10654 You can use project files to achieve some of the effects of a source
10655 versioning system (for example, defining separate projects for
10656 the different sets of sources that comprise different releases) but the
10657 Project Manager is independent of any source configuration management tools
10658 that might be used by the developers.
10659
10660 The next section introduces the main features of GNAT's project facility
10661 through a sequence of examples; subsequent sections will present the syntax
10662 and semantics in more detail. A more formal description of the project
10663 facility appears in the GNAT Reference Manual.
10664
10665 @c *****************************
10666 @c * Examples of Project Files *
10667 @c *****************************
10668
10669 @node Examples of Project Files
10670 @section Examples of Project Files
10671 @noindent
10672 This section illustrates some of the typical uses of project files and
10673 explains their basic structure and behavior.
10674
10675 @menu
10676 * Common Sources with Different ^Switches^Switches^ and Directories::
10677 * Using External Variables::
10678 * Importing Other Projects::
10679 * Extending a Project::
10680 @end menu
10681
10682 @node Common Sources with Different ^Switches^Switches^ and Directories
10683 @subsection Common Sources with Different ^Switches^Switches^ and Directories
10684
10685 @menu
10686 * Source Files::
10687 * Specifying the Object Directory::
10688 * Specifying the Exec Directory::
10689 * Project File Packages::
10690 * Specifying ^Switch^Switch^ Settings::
10691 * Main Subprograms::
10692 * Executable File Names::
10693 * Source File Naming Conventions::
10694 * Source Language(s)::
10695 @end menu
10696
10697 @noindent
10698 Suppose that the Ada source files @file{pack.ads}, @file{pack.adb}, and
10699 @file{proc.adb} are in the @file{/common} directory.  The file
10700 @file{proc.adb} contains an Ada main subprogram @code{Proc} that @code{with}s
10701 package @code{Pack}.  We want to compile these source files under two sets
10702 of ^switches^switches^:
10703 @itemize @bullet
10704 @item
10705 When debugging, we want to pass the @option{-g} switch to @command{gnatmake},
10706 and the @option{^-gnata^-gnata^},
10707 @option{^-gnato^-gnato^},
10708 and @option{^-gnatE^-gnatE^} switches to the
10709 compiler; the compiler's output is to appear in @file{/common/debug}
10710 @item
10711 When preparing a release version, we want to pass the @option{^-O2^O2^} switch
10712 to the compiler; the compiler's output is to appear in @file{/common/release}
10713 @end itemize
10714
10715 @noindent
10716 The GNAT project files shown below, respectively @file{debug.gpr} and
10717 @file{release.gpr} in the @file{/common} directory, achieve these effects.
10718
10719 Schematically:
10720 @smallexample
10721 @group
10722 ^/common^[COMMON]^
10723   debug.gpr
10724   release.gpr
10725   pack.ads
10726   pack.adb
10727   proc.adb
10728 @end group
10729 @group
10730 ^/common/debug^[COMMON.DEBUG]^
10731   proc.ali, proc.o
10732   pack.ali, pack.o
10733 @end group
10734 @group
10735 ^/common/release^[COMMON.RELEASE]^
10736   proc.ali, proc.o
10737   pack.ali, pack.o
10738 @end group
10739 @end smallexample
10740 Here are the corresponding project files:
10741
10742 @smallexample @c projectfile
10743 @group
10744 project Debug is
10745   for Object_Dir use "debug";
10746   for Main use ("proc");
10747
10748   package Builder is
10749     for ^Default_Switches^Default_Switches^ ("Ada")
10750         use ("^-g^-g^");
10751     for Executable ("proc.adb") use "proc1";
10752   end Builder;
10753 @end group
10754
10755 @group
10756   package Compiler is
10757     for ^Default_Switches^Default_Switches^ ("Ada")
10758        use ("-fstack-check",
10759             "^-gnata^-gnata^",
10760             "^-gnato^-gnato^",
10761             "^-gnatE^-gnatE^");
10762   end Compiler;
10763 end Debug;
10764 @end group
10765 @end smallexample
10766
10767 @smallexample @c projectfile
10768 @group
10769 project Release is
10770   for Object_Dir use "release";
10771   for Exec_Dir use ".";
10772   for Main use ("proc");
10773
10774   package Compiler is
10775     for ^Default_Switches^Default_Switches^ ("Ada")
10776         use ("^-O2^-O2^");
10777   end Compiler;
10778 end Release;
10779 @end group
10780 @end smallexample
10781
10782 @noindent
10783 The name of the project defined by @file{debug.gpr} is @code{"Debug"} (case
10784 insensitive), and analogously the project defined by @file{release.gpr} is
10785 @code{"Release"}.  For consistency the file should have the same name as the
10786 project, and the project file's extension should be @code{"gpr"}. These
10787 conventions are not required, but a warning is issued if they are not followed.
10788
10789 If the current directory is @file{^/temp^[TEMP]^}, then the command
10790 @smallexample
10791 gnatmake ^-P/common/debug.gpr^/PROJECT_FILE=[COMMON]DEBUG^
10792 @end smallexample
10793
10794 @noindent
10795 generates object and ALI files in @file{^/common/debug^[COMMON.DEBUG]^},
10796 as well as the @code{^proc1^PROC1.EXE^} executable,
10797 using the ^switch^switch^ settings defined in the project file.
10798
10799 Likewise, the command
10800 @smallexample
10801 gnatmake ^-P/common/release.gpr^/PROJECT_FILE=[COMMON]RELEASE^
10802 @end smallexample
10803
10804 @noindent
10805 generates object and ALI files in @file{^/common/release^[COMMON.RELEASE]^},
10806 and the @code{^proc^PROC.EXE^}
10807 executable in @file{^/common^[COMMON]^},
10808 using the ^switch^switch^ settings from the project file.
10809
10810 @node Source Files
10811 @unnumberedsubsubsec Source Files
10812
10813 @noindent
10814 If a project file does not explicitly specify a set of source directories or
10815 a set of source files, then by default the project's source files are the
10816 Ada source files in the project file directory.  Thus @file{pack.ads},
10817 @file{pack.adb}, and @file{proc.adb} are the source files for both projects.
10818
10819 @node Specifying the Object Directory
10820 @unnumberedsubsubsec Specifying the Object Directory
10821
10822 @noindent
10823 Several project properties are modeled by Ada-style @emph{attributes};
10824 a property is defined by supplying the equivalent of an Ada attribute
10825 definition clause in the project file.
10826 A project's object directory is another such a property; the corresponding
10827 attribute is @code{Object_Dir}, and its value is also a string expression,
10828 specified either as absolute or relative. In the later case,
10829 it is relative to the project file directory. Thus the compiler's
10830 output is directed to @file{^/common/debug^[COMMON.DEBUG]^}
10831 (for the @code{Debug} project)
10832 and to @file{^/common/release^[COMMON.RELEASE]^}
10833 (for the @code{Release} project).
10834 If @code{Object_Dir} is not specified, then the default is the project file
10835 directory itself.
10836
10837 @node Specifying the Exec Directory
10838 @unnumberedsubsubsec Specifying the Exec Directory
10839
10840 @noindent
10841 A project's exec directory is another property; the corresponding
10842 attribute is @code{Exec_Dir}, and its value is also a string expression,
10843 either specified as relative or absolute. If @code{Exec_Dir} is not specified,
10844 then the default is the object directory (which may also be the project file
10845 directory if attribute @code{Object_Dir} is not specified). Thus the executable
10846 is placed in @file{^/common/debug^[COMMON.DEBUG]^}
10847 for the @code{Debug} project (attribute @code{Exec_Dir} not specified)
10848 and in @file{^/common^[COMMON]^} for the @code{Release} project.
10849
10850 @node Project File Packages
10851 @unnumberedsubsubsec Project File Packages
10852
10853 @noindent
10854 A GNAT tool that is integrated with the Project Manager is modeled by a
10855 corresponding package in the project file. In the example above,
10856 The @code{Debug} project defines the packages @code{Builder}
10857 (for @command{gnatmake}) and @code{Compiler};
10858 the @code{Release} project defines only the @code{Compiler} package.
10859
10860 The Ada-like package syntax is not to be taken literally.  Although packages in
10861 project files bear a surface resemblance to packages in Ada source code, the
10862 notation is simply a way to convey a grouping of properties for a named
10863 entity.  Indeed, the package names permitted in project files are restricted
10864 to a predefined set, corresponding to the project-aware tools, and the contents
10865 of packages are limited to a small set of constructs.
10866 The packages in the example above contain attribute definitions.
10867
10868 @node Specifying ^Switch^Switch^ Settings
10869 @unnumberedsubsubsec Specifying ^Switch^Switch^ Settings
10870
10871 @noindent
10872 ^Switch^Switch^ settings for a project-aware tool can be specified through
10873 attributes in the package that corresponds to the tool.
10874 The example above illustrates one of the relevant attributes,
10875 @code{^Default_Switches^Default_Switches^}, which is defined in packages
10876 in both project files.
10877 Unlike simple attributes like @code{Source_Dirs},
10878 @code{^Default_Switches^Default_Switches^} is
10879 known as an @emph{associative array}.  When you define this attribute, you must
10880 supply an ``index'' (a literal string), and the effect of the attribute
10881 definition is to set the value of the array at the specified index.
10882 For the @code{^Default_Switches^Default_Switches^} attribute,
10883 the index is a programming language (in our case, Ada),
10884 and the value specified (after @code{use}) must be a list
10885 of string expressions.
10886
10887 The attributes permitted in project files are restricted to a predefined set.
10888 Some may appear at project level, others in packages.
10889 For any attribute that is an associative array, the index must always be a
10890 literal string, but the restrictions on this string (e.g., a file name or a
10891 language name) depend on the individual attribute.
10892 Also depending on the attribute, its specified value will need to be either a
10893 string or a string list.
10894
10895 In the @code{Debug} project, we set the switches for two tools,
10896 @command{gnatmake} and the compiler, and thus we include the two corresponding
10897 packages; each package defines the @code{^Default_Switches^Default_Switches^}
10898 attribute with index @code{"Ada"}.
10899 Note that the package corresponding to
10900 @command{gnatmake} is named @code{Builder}.  The @code{Release} project is
10901 similar, but only includes the @code{Compiler} package.
10902
10903 In project @code{Debug} above, the ^switches^switches^ starting with
10904 @option{-gnat} that are specified in package @code{Compiler}
10905 could have been placed in package @code{Builder}, since @command{gnatmake}
10906 transmits all such ^switches^switches^ to the compiler.
10907
10908 @node Main Subprograms
10909 @unnumberedsubsubsec Main Subprograms
10910
10911 @noindent
10912 One of the specifiable properties of a project is a list of files that contain
10913 main subprograms.  This property is captured in the @code{Main} attribute,
10914 whose value is a list of strings.  If a project defines the @code{Main}
10915 attribute, it is not necessary to identify the main subprogram(s) when
10916 invoking @command{gnatmake} (@pxref{gnatmake and Project Files}).
10917
10918 @node Executable File Names
10919 @unnumberedsubsubsec Executable File Names
10920
10921 @noindent
10922 By default, the executable file name corresponding to a main source is
10923 deduced from the main source file name. Through the attributes
10924 @code{Executable} and @code{Executable_Suffix} of package @code{Builder},
10925 it is possible to change this default.
10926 In project @code{Debug} above, the executable file name
10927 for main source @file{^proc.adb^PROC.ADB^} is
10928 @file{^proc1^PROC1.EXE^}.
10929 Attribute @code{Executable_Suffix}, when specified, may change the suffix
10930 of the the executable files, when no attribute @code{Executable} applies:
10931 its value replace the platform-specific executable suffix.
10932 Attributes @code{Executable} and @code{Executable_Suffix} are the only ways to
10933 specify a non default executable file name when several mains are built at once
10934 in a single @command{gnatmake} command.
10935
10936 @node Source File Naming Conventions
10937 @unnumberedsubsubsec Source File Naming Conventions
10938
10939 @noindent
10940 Since the project files above do not specify any source file naming
10941 conventions, the GNAT defaults are used.  The mechanism for defining source
10942 file naming conventions -- a package named @code{Naming} --
10943 is described below (@pxref{Naming Schemes}).
10944
10945 @node Source Language(s)
10946 @unnumberedsubsubsec Source Language(s)
10947
10948 @noindent
10949 Since the project files do not specify a @code{Languages} attribute, by
10950 default the GNAT tools assume that the language of the project file is Ada.
10951 More generally, a project can comprise source files
10952 in Ada, C, and/or other languages.
10953
10954 @node Using External Variables
10955 @subsection Using External Variables
10956
10957 @noindent
10958 Instead of supplying different project files for debug and release, we can
10959 define a single project file that queries an external variable (set either
10960 on the command line or via an ^environment variable^logical name^) in order to
10961 conditionally define the appropriate settings.  Again, assume that the
10962 source files @file{pack.ads}, @file{pack.adb}, and @file{proc.adb} are
10963 located in directory @file{^/common^[COMMON]^}.  The following project file,
10964 @file{build.gpr}, queries the external variable named @code{STYLE} and
10965 defines an object directory and ^switch^switch^ settings based on whether
10966 the value is @code{"deb"} (debug) or @code{"rel"} (release), and where
10967 the default is @code{"deb"}.
10968
10969 @smallexample @c projectfile
10970 @group
10971 project Build is
10972   for Main use ("proc");
10973
10974   type Style_Type is ("deb", "rel");
10975   Style : Style_Type := external ("STYLE", "deb");
10976
10977   case Style is
10978     when "deb" =>
10979       for Object_Dir use "debug";
10980
10981     when "rel" =>
10982       for Object_Dir use "release";
10983       for Exec_Dir use ".";
10984   end case;
10985 @end group
10986
10987 @group
10988   package Builder is
10989
10990     case Style is
10991       when "deb" =>
10992         for ^Default_Switches^Default_Switches^ ("Ada")
10993             use ("^-g^-g^");
10994         for Executable ("proc") use "proc1";
10995       when others =>
10996         null;
10997     end case;
10998
10999   end Builder;
11000 @end group
11001
11002 @group
11003   package Compiler is
11004
11005     case Style is
11006       when "deb" =>
11007         for ^Default_Switches^Default_Switches^ ("Ada")
11008             use ("^-gnata^-gnata^",
11009                  "^-gnato^-gnato^",
11010                  "^-gnatE^-gnatE^");
11011
11012       when "rel" =>
11013         for ^Default_Switches^Default_Switches^ ("Ada")
11014             use ("^-O2^-O2^");
11015     end case;
11016
11017   end Compiler;
11018
11019 end Build;
11020 @end group
11021 @end smallexample
11022
11023 @noindent
11024 @code{Style_Type} is an example of a @emph{string type}, which is the project
11025 file analog of an Ada enumeration type but whose components are string literals
11026 rather than identifiers.  @code{Style} is declared as a variable of this type.
11027
11028 The form @code{external("STYLE", "deb")} is known as an
11029 @emph{external reference}; its first argument is the name of an
11030 @emph{external variable}, and the second argument is a default value to be
11031 used if the external variable doesn't exist.  You can define an external
11032 variable on the command line via the @option{^-X^/EXTERNAL_REFERENCE^} switch,
11033 or you can use ^an environment variable^a logical name^
11034 as an external variable.
11035
11036 Each @code{case} construct is expanded by the Project Manager based on the
11037 value of @code{Style}. Thus the command
11038 @ifclear vms
11039 @smallexample
11040 gnatmake -P/common/build.gpr -XSTYLE=deb
11041 @end smallexample
11042 @end ifclear
11043
11044 @ifset vms
11045 @smallexample
11046 gnatmake /PROJECT_FILE=[COMMON]BUILD.GPR /EXTERNAL_REFERENCE=STYLE=deb
11047 @end smallexample
11048 @end ifset
11049
11050 @noindent
11051 is equivalent to the @command{gnatmake} invocation using the project file
11052 @file{debug.gpr} in the earlier example.  So is the command
11053 @smallexample
11054 gnatmake ^-P/common/build.gpr^/PROJECT_FILE=[COMMON]BUILD.GPR^
11055 @end smallexample
11056
11057 @noindent
11058 since @code{"deb"} is the default for @code{STYLE}.
11059
11060 Analogously,
11061
11062 @ifclear vms
11063 @smallexample
11064 gnatmake -P/common/build.gpr -XSTYLE=rel
11065 @end smallexample
11066 @end ifclear
11067
11068 @ifset vms
11069 @smallexample
11070 GNAT MAKE /PROJECT_FILE=[COMMON]BUILD.GPR /EXTERNAL_REFERENCE=STYLE=rel
11071 @end smallexample
11072 @end ifset
11073
11074 @noindent
11075 is equivalent to the @command{gnatmake} invocation using the project file
11076 @file{release.gpr} in the earlier example.
11077
11078 @node Importing Other Projects
11079 @subsection Importing Other Projects
11080 @cindex @code{ADA_PROJECT_PATH}
11081
11082 @noindent
11083 A compilation unit in a source file in one project may depend on compilation
11084 units in source files in other projects.  To compile this unit under
11085 control of a project file, the
11086 dependent project must @emph{import} the projects containing the needed source
11087 files.
11088 This effect is obtained using syntax similar to an Ada @code{with} clause,
11089 but where @code{with}ed entities are strings that denote project files.
11090
11091 As an example, suppose that the two projects @code{GUI_Proj} and
11092 @code{Comm_Proj} are defined in the project files @file{gui_proj.gpr} and
11093 @file{comm_proj.gpr} in directories @file{^/gui^[GUI]^}
11094 and @file{^/comm^[COMM]^}, respectively.
11095 Suppose that the source files for @code{GUI_Proj} are
11096 @file{gui.ads} and @file{gui.adb}, and that the source files for
11097 @code{Comm_Proj} are @file{comm.ads} and @file{comm.adb}, where each set of
11098 files is located in its respective project file directory.  Schematically:
11099
11100 @smallexample
11101 @group
11102 ^/gui^[GUI]^
11103   gui_proj.gpr
11104   gui.ads
11105   gui.adb
11106 @end group
11107
11108 @group
11109 ^/comm^[COMM]^
11110   comm_proj.gpr
11111   comm.ads
11112   comm.adb
11113 @end group
11114 @end smallexample
11115
11116 @noindent
11117 We want to develop an application in directory @file{^/app^[APP]^} that
11118 @code{with} the packages @code{GUI} and @code{Comm}, using the properties of
11119 the corresponding project files (e.g. the ^switch^switch^ settings
11120 and object directory).
11121 Skeletal code for a main procedure might be something like the following:
11122
11123 @smallexample @c ada
11124 @group
11125 with GUI, Comm;
11126 procedure App_Main is
11127    ...
11128 begin
11129    ...
11130 end App_Main;
11131 @end group
11132 @end smallexample
11133
11134 @noindent
11135 Here is a project file, @file{app_proj.gpr}, that achieves the desired
11136 effect:
11137
11138 @smallexample @c projectfile
11139 @group
11140 with "/gui/gui_proj", "/comm/comm_proj";
11141 project App_Proj is
11142    for Main use ("app_main");
11143 end App_Proj;
11144 @end group
11145 @end smallexample
11146
11147 @noindent
11148 Building an executable is achieved through the command:
11149 @smallexample
11150 gnatmake ^-P/app/app_proj^/PROJECT_FILE=[APP]APP_PROJ^
11151 @end smallexample
11152 @noindent
11153 which will generate the @code{^app_main^APP_MAIN.EXE^} executable
11154 in the directory where @file{app_proj.gpr} resides.
11155
11156 If an imported project file uses the standard extension (@code{^gpr^GPR^}) then
11157 (as illustrated above) the @code{with} clause can omit the extension.
11158
11159 Our example specified an absolute path for each imported project file.
11160 Alternatively, the directory name of an imported object can be omitted
11161 if either
11162 @itemize @bullet
11163 @item
11164 The imported project file is in the same directory as the importing project
11165 file, or
11166 @item
11167 You have defined ^an environment variable^a logical name^
11168 that includes the directory containing
11169 the needed project file. The syntax of @code{ADA_PROJECT_PATH} is the same as
11170 the syntax of @code{ADA_INCLUDE_PATH} and @code{ADA_OBJECTS_PATH}: a list of
11171 directory names separated by colons (semicolons on Windows).
11172 @end itemize
11173
11174 @noindent
11175 Thus, if we define @code{ADA_PROJECT_PATH} to include @file{^/gui^[GUI]^} and
11176 @file{^/comm^[COMM]^}, then our project file @file{app_proj.gpr} can be written
11177 as follows:
11178
11179 @smallexample @c projectfile
11180 @group
11181 with "gui_proj", "comm_proj";
11182 project App_Proj is
11183    for Main use ("app_main");
11184 end App_Proj;
11185 @end group
11186 @end smallexample
11187
11188 @noindent
11189 Importing other projects can create ambiguities.
11190 For example, the same unit might be present in different imported projects, or
11191 it might be present in both the importing project and in an imported project.
11192 Both of these conditions are errors.  Note that in the current version of
11193 the Project Manager, it is illegal to have an ambiguous unit even if the
11194 unit is never referenced by the importing project.  This restriction may be
11195 relaxed in a future release.
11196
11197 @node Extending a Project
11198 @subsection Extending a Project
11199
11200 @noindent
11201 In large software systems it is common to have multiple
11202 implementations of a common interface; in Ada terms, multiple versions of a
11203 package body for the same specification.  For example, one implementation
11204 might be safe for use in tasking programs, while another might only be used
11205 in sequential applications.  This can be modeled in GNAT using the concept
11206 of @emph{project extension}.  If one project (the ``child'') @emph{extends}
11207 another project (the ``parent'') then by default all source files of the
11208 parent project are inherited by the child, but the child project can
11209 override any of the parent's source files with new versions, and can also
11210 add new files.  This facility is the project analog of a type extension in
11211 Object-Oriented Programming.  Project hierarchies are permitted (a child
11212 project may be the parent of yet another project), and a project that
11213 inherits one project can also import other projects.
11214
11215 As an example, suppose that directory @file{^/seq^[SEQ]^} contains the project
11216 file @file{seq_proj.gpr} as well as the source files @file{pack.ads},
11217 @file{pack.adb}, and @file{proc.adb}:
11218
11219 @smallexample
11220 @group
11221 ^/seq^[SEQ]^
11222   pack.ads
11223   pack.adb
11224   proc.adb
11225   seq_proj.gpr
11226 @end group
11227 @end smallexample
11228
11229 @noindent
11230 Note that the project file can simply be empty (that is, no attribute or
11231 package is defined):
11232
11233 @smallexample @c projectfile
11234 @group
11235 project Seq_Proj is
11236 end Seq_Proj;
11237 @end group
11238 @end smallexample
11239
11240 @noindent
11241 implying that its source files are all the Ada source files in the project
11242 directory.
11243
11244 Suppose we want to supply an alternate version of @file{pack.adb}, in
11245 directory @file{^/tasking^[TASKING]^}, but use the existing versions of
11246 @file{pack.ads} and @file{proc.adb}.  We can define a project
11247 @code{Tasking_Proj} that inherits @code{Seq_Proj}:
11248
11249 @smallexample
11250 @group
11251 ^/tasking^[TASKING]^
11252   pack.adb
11253   tasking_proj.gpr
11254 @end group
11255
11256 @group
11257 project Tasking_Proj extends "/seq/seq_proj" is
11258 end Tasking_Proj;
11259 @end group
11260 @end smallexample
11261
11262 @noindent
11263 The version of @file{pack.adb} used in a build depends on which project file
11264 is specified.
11265
11266 Note that we could have obtained the desired behavior using project import
11267 rather than project inheritance; a @code{base} project would contain the
11268 sources for @file{pack.ads} and @file{proc.adb}, a sequential project would
11269 import @code{base} and add @file{pack.adb}, and likewise a tasking project
11270 would import @code{base} and add a different version of @file{pack.adb}.  The
11271 choice depends on whether other sources in the original project need to be
11272 overridden.  If they do, then project extension is necessary, otherwise,
11273 importing is sufficient.
11274
11275 @noindent
11276 In a project file that extends another project file, it is possible to
11277 indicate that an inherited source is not part of the sources of the extending
11278 project. This is necessary sometimes when a package spec has been overloaded
11279 and no longer requires a body: in this case, it is necessary to indicate that
11280 the inherited body is not part of the sources of the project, otherwise there
11281 will be a compilation error when compiling the spec.
11282
11283 For that purpose, the attribute @code{Locally_Removed_Files} is used.
11284 Its value is a string list: a list of file names.
11285
11286 @smallexample @c @projectfile
11287 project B extends "a" is
11288    for Source_Files use ("pkg.ads");
11289    --  New spec of Pkg does not need a completion
11290    for Locally_Removed_Files use ("pkg.adb");
11291 end B;
11292 @end smallexample
11293
11294 Attribute @code{Locally_Removed_Files} may also be used to check if a source
11295 is still needed: if it is possible to build using @command{gnatmake} when such
11296 a source is put in attribute @code{Locally_Removed_Files} of a project P, then
11297 it is possible to remove the source completely from a system that includes
11298 project P.
11299
11300 @c ***********************
11301 @c * Project File Syntax *
11302 @c ***********************
11303
11304 @node Project File Syntax
11305 @section Project File Syntax
11306
11307 @menu
11308 * Basic Syntax::
11309 * Packages::
11310 * Expressions::
11311 * String Types::
11312 * Variables::
11313 * Attributes::
11314 * Associative Array Attributes::
11315 * case Constructions::
11316 @end menu
11317
11318 @noindent
11319 This section describes the structure of project files.
11320
11321 A project may be an @emph{independent project}, entirely defined by a single
11322 project file. Any Ada source file in an independent project depends only
11323 on the predefined library and other Ada source files in the same project.
11324
11325 @noindent
11326 A project may also @dfn{depend on} other projects, in either or both of
11327 the following ways:
11328 @itemize @bullet
11329 @item It may import any number of projects
11330 @item It may extend at most one other project
11331 @end itemize
11332
11333 @noindent
11334 The dependence relation is a directed acyclic graph (the subgraph reflecting
11335 the ``extends'' relation is a tree).
11336
11337 A project's @dfn{immediate sources} are the source files directly defined by
11338 that project, either implicitly by residing in the project file's directory,
11339 or explicitly through any of the source-related attributes described below.
11340 More generally, a project @var{proj}'s @dfn{sources} are the immediate sources
11341 of @var{proj} together with the immediate sources (unless overridden) of any
11342 project on which @var{proj} depends (either directly or indirectly).
11343
11344 @node Basic Syntax
11345 @subsection Basic Syntax
11346
11347 @noindent
11348 As seen in the earlier examples, project files have an Ada-like syntax.
11349 The minimal project file is:
11350 @smallexample @c projectfile
11351 @group
11352 project Empty is
11353
11354 end Empty;
11355 @end group
11356 @end smallexample
11357
11358 @noindent
11359 The identifier @code{Empty} is the name of the project.
11360 This project name must be present after the reserved
11361 word @code{end} at the end of the project file, followed by a semi-colon.
11362
11363 Any name in a project file, such as the project name or a variable name,
11364 has the same syntax as an Ada identifier.
11365
11366 The reserved words of project files are the Ada reserved words plus
11367 @code{extends}, @code{external}, and @code{project}.  Note that the only Ada
11368 reserved words currently used in project file syntax are:
11369
11370 @itemize @bullet
11371 @item
11372 @code{case}
11373 @item
11374 @code{end}
11375 @item
11376 @code{for}
11377 @item
11378 @code{is}
11379 @item
11380 @code{others}
11381 @item
11382 @code{package}
11383 @item
11384 @code{renames}
11385 @item
11386 @code{type}
11387 @item
11388 @code{use}
11389 @item
11390 @code{when}
11391 @item
11392 @code{with}
11393 @end itemize
11394
11395 @noindent
11396 Comments in project files have the same syntax as in Ada, two consecutives
11397 hyphens through the end of the line.
11398
11399 @node Packages
11400 @subsection Packages
11401
11402 @noindent
11403 A project file may contain @emph{packages}. The name of a package must be one
11404 of the identifiers from the following list. A package
11405 with a given name may only appear once in a project file. Package names are
11406 case insensitive. The following package names are legal:
11407
11408 @itemize @bullet
11409 @item
11410 @code{Naming}
11411 @item
11412 @code{Builder}
11413 @item
11414 @code{Compiler}
11415 @item
11416 @code{Binder}
11417 @item
11418 @code{Linker}
11419 @item
11420 @code{Finder}
11421 @item
11422 @code{Cross_Reference}
11423 @item
11424 @code{Eliminate}
11425 @item
11426 @code{Pretty_Printer}
11427 @item
11428 @code{Metrics}
11429 @item
11430 @code{gnatls}
11431 @item
11432 @code{gnatstub}
11433 @item
11434 @code{IDE}
11435 @item
11436 @code{Language_Processing}
11437 @end itemize
11438
11439 @noindent
11440 In its simplest form, a package may be empty:
11441
11442 @smallexample @c projectfile
11443 @group
11444 project Simple is
11445   package Builder is
11446   end Builder;
11447 end Simple;
11448 @end group
11449 @end smallexample
11450
11451 @noindent
11452 A package may contain @emph{attribute declarations},
11453 @emph{variable declarations} and @emph{case constructions}, as will be
11454 described below.
11455
11456 When there is ambiguity between a project name and a package name,
11457 the name always designates the project. To avoid possible confusion, it is
11458 always a good idea to avoid naming a project with one of the
11459 names allowed for packages or any name that starts with @code{gnat}.
11460
11461 @node Expressions
11462 @subsection Expressions
11463
11464 @noindent
11465 An @emph{expression} is either a @emph{string expression} or a
11466 @emph{string list expression}.
11467
11468 A @emph{string expression} is either a @emph{simple string expression} or a
11469 @emph{compound string expression}.
11470
11471 A @emph{simple string expression} is one of the following:
11472 @itemize @bullet
11473 @item A literal string; e.g.@code{"comm/my_proj.gpr"}
11474 @item A string-valued variable reference (@pxref{Variables})
11475 @item A string-valued attribute reference (@pxref{Attributes})
11476 @item An external reference (@pxref{External References in Project Files})
11477 @end itemize
11478
11479 @noindent
11480 A @emph{compound string expression} is a concatenation of string expressions,
11481 using the operator @code{"&"}
11482 @smallexample
11483        Path & "/" & File_Name & ".ads"
11484 @end smallexample
11485
11486 @noindent
11487 A @emph{string list expression} is either a
11488 @emph{simple string list expression} or a
11489 @emph{compound string list expression}.
11490
11491 A @emph{simple string list expression} is one of the following:
11492 @itemize @bullet
11493 @item A parenthesized list of zero or more string expressions,
11494 separated by commas
11495 @smallexample
11496    File_Names := (File_Name, "gnat.adc", File_Name & ".orig");
11497    Empty_List := ();
11498 @end smallexample
11499 @item A string list-valued variable reference
11500 @item A string list-valued attribute reference
11501 @end itemize
11502
11503 @noindent
11504 A @emph{compound string list expression} is the concatenation (using
11505 @code{"&"}) of a simple string list expression and an expression.  Note that
11506 each term in a compound string list expression, except the first, may be
11507 either a string expression or a string list expression.
11508
11509 @smallexample @c projectfile
11510 @group
11511    File_Name_List := () & File_Name; --  One string in this list
11512    Extended_File_Name_List := File_Name_List & (File_Name & ".orig");
11513    --  Two strings
11514    Big_List := File_Name_List & Extended_File_Name_List;
11515    --  Concatenation of two string lists: three strings
11516    Illegal_List := "gnat.adc" & Extended_File_Name_List;
11517    --  Illegal: must start with a string list
11518 @end group
11519 @end smallexample
11520
11521 @node String Types
11522 @subsection String Types
11523
11524 @noindent
11525 A @emph{string type declaration} introduces a discrete set of string literals.
11526 If a string variable is declared to have this type, its value
11527 is restricted to the given set of literals.
11528
11529 Here is an example of a string type declaration:
11530
11531 @smallexample @c projectfile
11532    type OS is ("NT", "nt", "Unix", "GNU/Linux", "other OS");
11533 @end smallexample
11534
11535 @noindent
11536 Variables of a string type are called @emph{typed variables}; all other
11537 variables are called @emph{untyped variables}. Typed variables are
11538 particularly useful in @code{case} constructions, to support conditional
11539 attribute declarations.
11540 (@pxref{case Constructions}).
11541
11542 The string literals in the list are case sensitive and must all be different.
11543 They may include any graphic characters allowed in Ada, including spaces.
11544
11545 A string type may only be declared at the project level, not inside a package.
11546
11547 A string type may be referenced by its name if it has been declared in the same
11548 project file, or by an expanded name whose prefix is the name of the project
11549 in which it is declared.
11550
11551 @node Variables
11552 @subsection Variables
11553
11554 @noindent
11555 A variable may be declared at the project file level, or within a package.
11556 Here are some examples of variable declarations:
11557
11558 @smallexample @c projectfile
11559 @group
11560    This_OS : OS := external ("OS"); --  a typed variable declaration
11561    That_OS := "GNU/Linux";          --  an untyped variable declaration
11562 @end group
11563 @end smallexample
11564
11565 @noindent
11566 The syntax of a @emph{typed variable declaration} is identical to the Ada
11567 syntax for an object declaration. By contrast, the syntax of an untyped
11568 variable declaration is identical to an Ada assignment statement. In fact,
11569 variable declarations in project files have some of the characteristics of
11570 an assignment, in that successive declarations for the same variable are
11571 allowed. Untyped variable declarations do establish the expected kind of the
11572 variable (string or string list), and successive declarations for it must
11573 respect the initial kind.
11574
11575 @noindent
11576 A string variable declaration (typed or untyped) declares a variable
11577 whose value is a string. This variable may be used as a string expression.
11578 @smallexample @c projectfile
11579    File_Name       := "readme.txt";
11580    Saved_File_Name := File_Name & ".saved";
11581 @end smallexample
11582
11583 @noindent
11584 A string list variable declaration declares a variable whose value is a list
11585 of strings. The list may contain any number (zero or more) of strings.
11586
11587 @smallexample @c projectfile
11588    Empty_List := ();
11589    List_With_One_Element := ("^-gnaty^-gnaty^");
11590    List_With_Two_Elements := List_With_One_Element & "^-gnatg^-gnatg^";
11591    Long_List := ("main.ada", "pack1_.ada", "pack1.ada", "pack2_.ada"
11592                  "pack2.ada", "util_.ada", "util.ada");
11593 @end smallexample
11594
11595 @noindent
11596 The same typed variable may not be declared more than once at project level,
11597 and it may not be declared more than once in any package; it is in effect
11598 a constant.
11599
11600 The same untyped variable may be declared several times. Declarations are
11601 elaborated in the order in which they appear, so  the new value replaces
11602 the old one, and any subsequent reference to the variable uses the new value.
11603 However, as noted above, if a variable has been declared as a string, all
11604 subsequent
11605 declarations must give it a string value. Similarly, if a variable has
11606 been declared as a string list, all subsequent declarations
11607 must give it a string list value.
11608
11609 A @emph{variable reference} may take several forms:
11610
11611 @itemize @bullet
11612 @item The simple variable name, for a variable in the current package (if any)
11613 or in the current project
11614 @item An expanded name, whose prefix is a context name.
11615 @end itemize
11616
11617 @noindent
11618 A @emph{context} may be one of the following:
11619
11620 @itemize @bullet
11621 @item The name of an existing package in the current project
11622 @item The name of an imported project of the current project
11623 @item The name of an ancestor project (i.e., a project extended by the current
11624 project, either directly or indirectly)
11625 @item An expanded name whose prefix is an imported/parent project name, and
11626 whose selector is a package name in that project.
11627 @end itemize
11628
11629 @noindent
11630 A variable reference may be used in an expression.
11631
11632 @node Attributes
11633 @subsection Attributes
11634
11635 @noindent
11636 A project (and its packages) may have @emph{attributes} that define
11637 the project's properties.  Some attributes have values that are strings;
11638 others have values that are string lists.
11639
11640 There are two categories of attributes: @emph{simple attributes}
11641 and @emph{associative arrays} (@pxref{Associative Array Attributes}).
11642
11643 Legal project attribute names, and attribute names for each legal package are
11644 listed below. Attributes names are case-insensitive.
11645
11646 The following attributes are defined on projects (all are simple attributes):
11647
11648 @multitable @columnfractions .4 .3
11649 @item @emph{Attribute Name}
11650 @tab @emph{Value}
11651 @item @code{Source_Files}
11652 @tab string list
11653 @item @code{Source_Dirs}
11654 @tab string list
11655 @item @code{Source_List_File}
11656 @tab string
11657 @item @code{Object_Dir}
11658 @tab string
11659 @item @code{Exec_Dir}
11660 @tab string
11661 @item @code{Locally_Removed_Files}
11662 @tab string list
11663 @item @code{Languages}
11664 @tab string list
11665 @item @code{Main}
11666 @tab string list
11667 @item @code{Library_Dir}
11668 @tab string
11669 @item @code{Library_Name}
11670 @tab string
11671 @item @code{Library_Kind}
11672 @tab string
11673 @item @code{Library_Version}
11674 @tab string
11675 @item @code{Library_Interface}
11676 @tab string
11677 @item @code{Library_Auto_Init}
11678 @tab string
11679 @item @code{Library_Options}
11680 @tab string list
11681 @item @code{Library_Src_Dir}
11682 @tab string
11683 @item @code{Library_ALI_Dir}
11684 @tab string
11685 @item @code{Library_GCC}
11686 @tab string
11687 @item @code{Library_Symbol_File}
11688 @tab string
11689 @item @code{Library_Symbol_Policy}
11690 @tab string
11691 @item @code{Library_Reference_Symbol_File}
11692 @tab string
11693 @item @code{Externally_Built}
11694 @tab string
11695 @end multitable
11696
11697 @noindent
11698 The following attributes are defined for package  @code{Naming}
11699 (@pxref{Naming Schemes}):
11700
11701 @multitable @columnfractions .4 .2 .2 .2
11702 @item Attribute Name @tab Category @tab Index @tab Value
11703 @item @code{Spec_Suffix}
11704 @tab associative array
11705 @tab language name
11706 @tab string
11707 @item @code{Body_Suffix}
11708 @tab associative array
11709 @tab language name
11710 @tab string
11711 @item @code{Separate_Suffix}
11712 @tab simple attribute
11713 @tab n/a
11714 @tab string
11715 @item @code{Casing}
11716 @tab simple attribute
11717 @tab n/a
11718 @tab string
11719 @item @code{Dot_Replacement}
11720 @tab simple attribute
11721 @tab n/a
11722 @tab string
11723 @item @code{Spec}
11724 @tab associative array
11725 @tab Ada unit name
11726 @tab string
11727 @item @code{Body}
11728 @tab associative array
11729 @tab Ada unit name
11730 @tab string
11731 @item @code{Specification_Exceptions}
11732 @tab associative array
11733 @tab language name
11734 @tab string list
11735 @item @code{Implementation_Exceptions}
11736 @tab associative array
11737 @tab language name
11738 @tab string list
11739 @end multitable
11740
11741 @noindent
11742 The following attributes are defined for packages @code{Builder},
11743 @code{Compiler}, @code{Binder},
11744 @code{Linker}, @code{Cross_Reference}, and @code{Finder}
11745 (@pxref{^Switches^Switches^ and Project Files}).
11746
11747 @multitable @columnfractions .4 .2 .2 .2
11748 @item Attribute Name @tab Category @tab Index @tab Value
11749 @item @code{^Default_Switches^Default_Switches^}
11750 @tab associative array
11751 @tab language name
11752 @tab string list
11753 @item @code{^Switches^Switches^}
11754 @tab associative array
11755 @tab file name
11756 @tab string list
11757 @end multitable
11758
11759 @noindent
11760 In addition, package @code{Compiler} has a single string attribute
11761 @code{Local_Configuration_Pragmas} and package @code{Builder} has a single
11762 string attribute @code{Global_Configuration_Pragmas}.
11763
11764 @noindent
11765 Each simple attribute has a default value: the empty string (for string-valued
11766 attributes) and the empty list (for string list-valued attributes).
11767
11768 An attribute declaration defines a new value for an attribute.
11769
11770 Examples of simple attribute declarations:
11771
11772 @smallexample @c projectfile
11773    for Object_Dir use "objects";
11774    for Source_Dirs use ("units", "test/drivers");
11775 @end smallexample
11776
11777 @noindent
11778 The syntax of a @dfn{simple attribute declaration} is similar to that of an
11779 attribute definition clause in Ada.
11780
11781 Attributes references may be appear in expressions.
11782 The general form for such a reference is @code{<entity>'<attribute>}:
11783 Associative array attributes are functions. Associative
11784 array attribute references must have an argument that is a string literal.
11785
11786 Examples are:
11787
11788 @smallexample @c projectfile
11789   project'Object_Dir
11790   Naming'Dot_Replacement
11791   Imported_Project'Source_Dirs
11792   Imported_Project.Naming'Casing
11793   Builder'^Default_Switches^Default_Switches^("Ada")
11794 @end smallexample
11795
11796 @noindent
11797 The prefix of an attribute may be:
11798 @itemize @bullet
11799 @item @code{project} for an attribute of the current project
11800 @item The name of an existing package of the current project
11801 @item The name of an imported project
11802 @item The name of a parent project that is extended by the current project
11803 @item An expanded name whose prefix is imported/parent project name,
11804       and whose selector is a package name
11805 @end itemize
11806
11807 @noindent
11808 Example:
11809 @smallexample @c projectfile
11810 @group
11811    project Prj is
11812      for Source_Dirs use project'Source_Dirs & "units";
11813      for Source_Dirs use project'Source_Dirs & "test/drivers"
11814    end Prj;
11815 @end group
11816 @end smallexample
11817
11818 @noindent
11819 In the first attribute declaration, initially the attribute @code{Source_Dirs}
11820 has the default value: an empty string list. After this declaration,
11821 @code{Source_Dirs} is a string list of one element: @code{"units"}.
11822 After the second attribute declaration @code{Source_Dirs} is a string list of
11823 two elements: @code{"units"} and @code{"test/drivers"}.
11824
11825 Note: this example is for illustration only. In practice,
11826 the project file would contain only one attribute declaration:
11827
11828 @smallexample @c projectfile
11829    for Source_Dirs use ("units", "test/drivers");
11830 @end smallexample
11831
11832 @node Associative Array Attributes
11833 @subsection Associative Array Attributes
11834
11835 @noindent
11836 Some attributes are defined as @emph{associative arrays}. An associative
11837 array may be regarded as a function that takes a string as a parameter
11838 and delivers a string or string list value as its result.
11839
11840 Here are some examples of single associative array attribute associations:
11841
11842 @smallexample @c projectfile
11843    for Body ("main") use "Main.ada";
11844    for ^Switches^Switches^ ("main.ada")
11845        use ("^-v^-v^",
11846             "^-gnatv^-gnatv^");
11847    for ^Switches^Switches^ ("main.ada")
11848             use Builder'^Switches^Switches^ ("main.ada")
11849               & "^-g^-g^";
11850 @end smallexample
11851
11852 @noindent
11853 Like untyped variables and simple attributes, associative array attributes
11854 may be declared several times. Each declaration supplies a new value for the
11855 attribute, and replaces the previous setting.
11856
11857 @noindent
11858 An associative array attribute may be declared as a full associative array
11859 declaration, with the value of the same attribute in an imported or extended
11860 project.
11861
11862 @smallexample @c projectfile
11863    package Builder is
11864       for Default_Switches use Default.Builder'Default_Switches;
11865    end Builder;
11866 @end smallexample
11867
11868 @noindent
11869 In this example, @code{Default} must be either an project imported by the
11870 current project, or the project that the current project extends. If the
11871 attribute is in a package (in this case, in package @code{Builder}), the same
11872 package needs to be specified.
11873
11874 @noindent
11875 A full associative array declaration replaces any other declaration for the
11876 attribute, including other full associative array declaration. Single
11877 associative array associations may be declare after a full associative
11878 declaration, modifying the value for a single association of the attribute.
11879
11880 @node case Constructions
11881 @subsection @code{case} Constructions
11882
11883 @noindent
11884 A @code{case} construction is used in a project file to effect conditional
11885 behavior.
11886 Here is a typical example:
11887
11888 @smallexample @c projectfile
11889 @group
11890 project MyProj is
11891    type OS_Type is ("GNU/Linux", "Unix", "NT", "VMS");
11892
11893    OS : OS_Type := external ("OS", "GNU/Linux");
11894 @end group
11895
11896 @group
11897    package Compiler is
11898      case OS is
11899        when "GNU/Linux" | "Unix" =>
11900          for ^Default_Switches^Default_Switches^ ("Ada")
11901              use ("^-gnath^-gnath^");
11902        when "NT" =>
11903          for ^Default_Switches^Default_Switches^ ("Ada")
11904              use ("^-gnatP^-gnatP^");
11905        when others =>
11906      end case;
11907    end Compiler;
11908 end MyProj;
11909 @end group
11910 @end smallexample
11911
11912 @noindent
11913 The syntax of a @code{case} construction is based on the Ada case statement
11914 (although there is no @code{null} construction for empty alternatives).
11915
11916 The case expression must a typed string variable.
11917 Each alternative comprises the reserved word @code{when}, either a list of
11918 literal strings separated by the @code{"|"} character or the reserved word
11919 @code{others},  and the @code{"=>"} token.
11920 Each literal string must belong to the string type that is the type of the
11921 case variable.
11922 An @code{others} alternative, if present, must occur last.
11923
11924 After each @code{=>}, there are zero or more constructions.  The only
11925 constructions allowed in a case construction are other case constructions and
11926 attribute declarations. String type declarations, variable declarations and
11927 package declarations are not allowed.
11928
11929 The value of the case variable is often given by an external reference
11930 (@pxref{External References in Project Files}).
11931
11932 @c ****************************************
11933 @c * Objects and Sources in Project Files *
11934 @c ****************************************
11935
11936 @node Objects and Sources in Project Files
11937 @section Objects and Sources in Project Files
11938
11939 @menu
11940 * Object Directory::
11941 * Exec Directory::
11942 * Source Directories::
11943 * Source File Names::
11944 @end menu
11945
11946 @noindent
11947 Each project has exactly one object directory and one or more source
11948 directories. The source directories must contain at least one source file,
11949 unless  the project file explicitly specifies that no source files are present
11950 (@pxref{Source File Names}).
11951
11952 @node Object Directory
11953 @subsection Object Directory
11954
11955 @noindent
11956 The object directory for a project is the directory containing the compiler's
11957 output (such as @file{ALI} files and object files) for the project's immediate
11958 sources.
11959
11960 The object directory is given by the value of the attribute @code{Object_Dir}
11961 in the project file.
11962
11963 @smallexample @c projectfile
11964    for Object_Dir use "objects";
11965 @end smallexample
11966
11967 @noindent
11968 The attribute @var{Object_Dir} has a string value, the path name of the object
11969 directory. The path name may be absolute or relative to the directory of the
11970 project file. This directory must already exist, and be readable and writable.
11971
11972 By default, when the attribute @code{Object_Dir} is not given an explicit value
11973 or when its value is the empty string, the object directory is the same as the
11974 directory containing the project file.
11975
11976 @node Exec Directory
11977 @subsection Exec Directory
11978
11979 @noindent
11980 The exec directory for a project is the directory containing the executables
11981 for the project's main subprograms.
11982
11983 The exec directory is given by the value of the attribute @code{Exec_Dir}
11984 in the project file.
11985
11986 @smallexample @c projectfile
11987    for Exec_Dir use "executables";
11988 @end smallexample
11989
11990 @noindent
11991 The attribute @var{Exec_Dir} has a string value, the path name of the exec
11992 directory. The path name may be absolute or relative to the directory of the
11993 project file. This directory must already exist, and be writable.
11994
11995 By default, when the attribute @code{Exec_Dir} is not given an explicit value
11996 or when its value is the empty string, the exec directory is the same as the
11997 object directory of the project file.
11998
11999 @node Source Directories
12000 @subsection Source Directories
12001
12002 @noindent
12003 The source directories of a project are specified by the project file
12004 attribute @code{Source_Dirs}.
12005
12006 This attribute's value is a string list. If the attribute is not given an
12007 explicit value, then there is only one source directory, the one where the
12008 project file resides.
12009
12010 A @code{Source_Dirs} attribute that is explicitly defined to be the empty list,
12011 as in
12012
12013 @smallexample @c projectfile
12014     for Source_Dirs use ();
12015 @end smallexample
12016
12017 @noindent
12018 indicates that the project contains no source files.
12019
12020 Otherwise, each string in the string list designates one or more
12021 source directories.
12022
12023 @smallexample @c projectfile
12024    for Source_Dirs use ("sources", "test/drivers");
12025 @end smallexample
12026
12027 @noindent
12028 If a string in the list ends with @code{"/**"},  then the directory whose path
12029 name precedes the two asterisks, as well as all its subdirectories
12030 (recursively), are source directories.
12031
12032 @smallexample @c projectfile
12033    for Source_Dirs use ("/system/sources/**");
12034 @end smallexample
12035
12036 @noindent
12037 Here the directory @code{/system/sources} and all of its subdirectories
12038 (recursively) are source directories.
12039
12040 To specify that the source directories are the directory of the project file
12041 and all of its subdirectories, you can declare @code{Source_Dirs} as follows:
12042 @smallexample @c projectfile
12043    for Source_Dirs use ("./**");
12044 @end smallexample
12045
12046 @noindent
12047 Each of the source directories must exist and be readable.
12048
12049 @node Source File Names
12050 @subsection Source File Names
12051
12052 @noindent
12053 In a project that contains source files, their names may be specified by the
12054 attributes @code{Source_Files} (a string list) or @code{Source_List_File}
12055 (a string). Source file names never include any directory information.
12056
12057 If the attribute @code{Source_Files} is given an explicit value, then each
12058 element of the list is a source file name.
12059
12060 @smallexample @c projectfile
12061    for Source_Files use ("main.adb");
12062    for Source_Files use ("main.adb", "pack1.ads", "pack2.adb");
12063 @end smallexample
12064
12065 @noindent
12066 If the attribute @code{Source_Files} is not given an explicit value,
12067 but the attribute @code{Source_List_File} is given a string value,
12068 then the source file names are contained in the text file whose path name
12069 (absolute or relative to the directory of the project file) is the
12070 value of the attribute @code{Source_List_File}.
12071
12072 Each line in the file that is not empty or is not a comment
12073 contains a source file name.
12074
12075 @smallexample @c projectfile
12076    for Source_List_File use "source_list.txt";
12077 @end smallexample
12078
12079 @noindent
12080 By default, if neither the attribute @code{Source_Files} nor the attribute
12081 @code{Source_List_File} is given an explicit value, then each file in the
12082 source directories that conforms to the project's naming scheme
12083 (@pxref{Naming Schemes}) is an immediate source of the project.
12084
12085 A warning is issued if both attributes @code{Source_Files} and
12086 @code{Source_List_File} are given explicit values. In this case, the attribute
12087 @code{Source_Files} prevails.
12088
12089 Each source file name must be the name of one existing source file
12090 in one of the source directories.
12091
12092 A @code{Source_Files} attribute whose value is an empty list
12093 indicates that there are no source files in the project.
12094
12095 If the order of the source directories is known statically, that is if
12096 @code{"/**"} is not used in the string list @code{Source_Dirs}, then there may
12097 be several files with the same source file name. In this case, only the file
12098 in the first directory is considered as an immediate source of the project
12099 file. If the order of the source directories is not known statically, it is
12100 an error to have several files with the same source file name.
12101
12102 Projects can be specified to have no Ada source
12103 files: the value of (@code{Source_Dirs} or @code{Source_Files} may be an empty
12104 list, or the @code{"Ada"} may be absent from @code{Languages}:
12105
12106 @smallexample @c projectfile
12107    for Source_Dirs use ();
12108    for Source_Files use ();
12109    for Languages use ("C", "C++");
12110 @end smallexample
12111
12112 @noindent
12113 Otherwise, a project must contain at least one immediate source.
12114
12115 Projects with no source files are useful as template packages
12116 (@pxref{Packages in Project Files}) for other projects; in particular to
12117 define a package @code{Naming} (@pxref{Naming Schemes}).
12118
12119 @c ****************************
12120 @c * Importing Projects *
12121 @c ****************************
12122
12123 @node  Importing Projects
12124 @section Importing Projects
12125 @cindex @code{ADA_PROJECT_PATH}
12126
12127 @noindent
12128 An immediate source of a project P may depend on source files that
12129 are neither immediate sources of P nor in the predefined library.
12130 To get this effect, P must @emph{import} the projects that contain the needed
12131 source files.
12132
12133 @smallexample @c projectfile
12134 @group
12135   with "project1", "utilities.gpr";
12136   with "/namings/apex.gpr";
12137   project Main is
12138     ...
12139 @end group
12140 @end smallexample
12141
12142 @noindent
12143 As can be seen in this example, the syntax for importing projects is similar
12144 to the syntax for importing compilation units in Ada. However, project files
12145 use literal strings instead of names, and the @code{with} clause identifies
12146 project files rather than packages.
12147
12148 Each literal string is the file name or path name (absolute or relative) of a
12149 project file. If a string corresponds to a file name, with no path or a
12150 relative path, then its location is determined by the @emph{project path}. The
12151 latter can be queried using @code{gnatls -v}. It contains:
12152
12153 @itemize @bullet
12154 @item
12155 In first position, the directory containing the current project file.
12156 @item
12157 In last position, the default project directory. This default project directory
12158 is part of the GNAT installation and is the standard place to install project
12159 files giving access to standard support libraries.
12160 @ifclear vms
12161 @ref{Installing a library}
12162 @end ifclear
12163
12164 @item
12165 In between, all the directories referenced in the
12166 ^environment variable^logical name^ @env{ADA_PROJECT_PATH} if it exists.
12167 @end itemize
12168
12169 @noindent
12170 If a relative pathname is used, as in
12171
12172 @smallexample @c projectfile
12173   with "tests/proj";
12174 @end smallexample
12175
12176 @noindent
12177 then the full path for the project is constructed by concatenating this
12178 relative path to those in the project path, in order, until a matching file is
12179 found. Any symbolic link will be fully resolved in the directory of the
12180 importing project file before the imported project file is examined.
12181
12182 If the @code{with}'ed project file name does not have an extension,
12183 the default is @file{^.gpr^.GPR^}. If a file with this extension is not found,
12184 then the file name as specified in the @code{with} clause (no extension) will
12185 be used. In the above example, if a file @code{project1.gpr} is found, then it
12186 will be used; otherwise, if a file @code{^project1^PROJECT1^} exists
12187 then it will be used; if neither file exists, this is an error.
12188
12189 A warning is issued if the name of the project file does not match the
12190 name of the project; this check is case insensitive.
12191
12192 Any source file that is an immediate source of the imported project can be
12193 used by the immediate sources of the importing project, transitively. Thus
12194 if @code{A} imports @code{B}, and @code{B} imports @code{C}, the immediate
12195 sources of @code{A} may depend on the immediate sources of @code{C}, even if
12196 @code{A} does not import @code{C} explicitly. However, this is not recommended,
12197 because if and when @code{B} ceases to import @code{C}, some sources in
12198 @code{A} will no longer compile.
12199
12200 A side effect of this capability is that normally cyclic dependencies are not
12201 permitted: if @code{A} imports @code{B} (directly or indirectly) then @code{B}
12202 is not allowed to import @code{A}. However, there are cases when cyclic
12203 dependencies would be beneficial. For these cases, another form of import
12204 between projects exists, the @code{limited with}: a project @code{A} that
12205 imports a project @code{B} with a straigh @code{with} may also be imported,
12206 directly or indirectly, by @code{B} on the condition that imports from @code{B}
12207 to @code{A} include at least one @code{limited with}.
12208
12209 @smallexample @c 0projectfile
12210 with "../b/b.gpr";
12211 with "../c/c.gpr";
12212 project A is
12213 end A;
12214
12215 limited with "../a/a.gpr";
12216 project B is
12217 end B;
12218
12219 with "../d/d.gpr";
12220 project C is
12221 end C;
12222
12223 limited with "../a/a.gpr";
12224 project D is
12225 end D;
12226 @end smallexample
12227
12228 @noindent
12229 In the above legal example, there are two project cycles:
12230 @itemize @bullet
12231 @item A-> B-> A
12232 @item A -> C -> D -> A
12233 @end itemize
12234
12235 @noindent
12236 In each of these cycle there is one @code{limited with}: import of @code{A}
12237 from @code{B} and import of @code{A} from @code{D}.
12238
12239 The difference between straight @code{with} and @code{limited with} is that
12240 the name of a project imported with a @code{limited with} cannot be used in the
12241 project that imports it. In particular, its packages cannot be renamed and
12242 its variables cannot be referred to.
12243
12244 An exception to the above rules for @code{limited with} is that for the main
12245 project specified to @command{gnatmake} or to the @command{GNAT} driver a
12246 @code{limited with} is equivalent to a straight @code{with}. For example,
12247 in the example above, projects @code{B} and @code{D} could not be main
12248 projects for @command{gnatmake} or to the @command{GNAT} driver, because they
12249 each have a @code{limited with} that is the only one in a cycle of importing
12250 projects.
12251
12252 @c *********************
12253 @c * Project Extension *
12254 @c *********************
12255
12256 @node Project Extension
12257 @section Project Extension
12258
12259 @noindent
12260 During development of a large system, it is sometimes necessary to use
12261 modified versions of some of the source files, without changing the original
12262 sources. This can be achieved through the @emph{project extension} facility.
12263
12264 @smallexample @c projectfile
12265    project Modified_Utilities extends "/baseline/utilities.gpr" is ...
12266 @end smallexample
12267
12268 @noindent
12269 A project extension declaration introduces an extending project
12270 (the @emph{child}) and a project being extended (the @emph{parent}).
12271
12272 By default, a child project inherits all the sources of its parent.
12273 However, inherited sources can be overridden: a unit in a parent is hidden
12274 by a unit of the same name in the child.
12275
12276 Inherited sources are considered to be sources (but not immediate sources)
12277 of the child project; see @ref{Project File Syntax}.
12278
12279 An inherited source file retains any switches specified in the parent project.
12280
12281 For example if the project @code{Utilities} contains the specification and the
12282 body of an Ada package @code{Util_IO}, then the project
12283 @code{Modified_Utilities} can contain a new body for package @code{Util_IO}.
12284 The original body of @code{Util_IO} will not be considered in program builds.
12285 However, the package specification will still be found in the project
12286 @code{Utilities}.
12287
12288 A child project can have only one parent but it may import any number of other
12289 projects.
12290
12291 A project is not allowed to import directly or indirectly at the same time a
12292 child project and any of its ancestors.
12293
12294 @c *******************************
12295 @c * Project Hierarchy Extension *
12296 @c *******************************
12297
12298 @node Project Hierarchy Extension
12299 @section Project Hierarchy Extension
12300
12301 @noindent
12302 When extending a large system spanning multiple projects, it is often
12303 inconvenient to extend every project in the hierarchy that is impacted by a
12304 small change introduced. In such cases, it is possible to create a virtual
12305 extension of entire hierarchy using @code{extends all} relationship.
12306
12307 When the project is extended using @code{extends all} inheritance, all projects
12308 that are imported by it, both directly and indirectly, are considered virtually
12309 extended. That is, the Project Manager creates "virtual projects"
12310 that extend every project in the hierarchy; all these virtual projects have
12311 no sources of their own and have as object directory the object directory of
12312 the root of "extending all" project.
12313
12314 It is possible to explicitly extend one or more projects in the hierarchy
12315 in order to modify the sources. These extending projects must be imported by
12316 the "extending all" project, which will replace the corresponding virtual
12317 projects with the explicit ones.
12318
12319 When building such a project hierarchy extension, the Project Manager will
12320 ensure that both modified sources and sources in virtual extending projects
12321 that depend on them, are recompiled.
12322
12323 By means of example, consider the following hierarchy of projects.
12324
12325 @enumerate
12326 @item
12327 project A, containing package P1
12328 @item
12329 project B importing A and containing package P2 which depends on P1
12330 @item
12331 project C importing B and containing package P3 which depends on P2
12332 @end enumerate
12333
12334 @noindent
12335 We want to modify packages P1 and P3.
12336
12337 This project hierarchy will need to be extended as follows:
12338
12339 @enumerate
12340 @item
12341 Create project A1 that extends A, placing modified P1 there:
12342
12343 @smallexample @c 0projectfile
12344 project A1 extends "(...)/A" is
12345 end A1;
12346 @end smallexample
12347
12348 @item
12349 Create project C1 that "extends all" C and imports A1, placing modified
12350 P3 there:
12351
12352 @smallexample @c 0projectfile
12353 with "(...)/A1";
12354 project C1 extends all "(...)/C" is
12355 end C1;
12356 @end smallexample
12357 @end enumerate
12358
12359 When you build project C1, your entire modified project space will be
12360 recompiled, including the virtual project B1 that has been impacted by the
12361 "extending all" inheritance of project C.
12362
12363 Note that if a Library Project in the hierarchy is virtually extended,
12364 the virtual project that extends the Library Project is not a Library Project.
12365
12366 @c ****************************************
12367 @c * External References in Project Files *
12368 @c ****************************************
12369
12370 @node  External References in Project Files
12371 @section External References in Project Files
12372
12373 @noindent
12374 A project file may contain references to external variables; such references
12375 are called @emph{external references}.
12376
12377 An external variable is either defined as part of the environment (an
12378 environment variable in Unix, for example) or else specified on the command
12379 line via the @option{^-X^/EXTERNAL_REFERENCE=^@emph{vbl}=@emph{value}} switch.
12380 If both, then the command line value is used.
12381
12382 The value of an external reference is obtained by means of the built-in
12383 function @code{external}, which returns a string value.
12384 This function has two forms:
12385 @itemize @bullet
12386 @item @code{external (external_variable_name)}
12387 @item @code{external (external_variable_name, default_value)}
12388 @end itemize
12389
12390 @noindent
12391 Each parameter must be a string literal.  For example:
12392
12393 @smallexample @c projectfile
12394    external ("USER")
12395    external ("OS", "GNU/Linux")
12396 @end smallexample
12397
12398 @noindent
12399 In the form with one parameter, the function returns the value of
12400 the external variable given as parameter. If this name is not present in the
12401 environment, the function returns an empty string.
12402
12403 In the form with two string parameters, the second argument is
12404 the value returned when the variable given as the first argument is not
12405 present in the environment. In the example above, if @code{"OS"} is not
12406 the name of ^an environment variable^a logical name^ and is not passed on
12407 the command line, then the returned value is @code{"GNU/Linux"}.
12408
12409 An external reference may be part of a string expression or of a string
12410 list expression, and can therefore appear in a variable declaration or
12411 an attribute declaration.
12412
12413 @smallexample @c projectfile
12414 @group
12415    type Mode_Type is ("Debug", "Release");
12416    Mode : Mode_Type := external ("MODE");
12417    case Mode is
12418      when "Debug" =>
12419         ...
12420 @end group
12421 @end smallexample
12422
12423 @c *****************************
12424 @c * Packages in Project Files *
12425 @c *****************************
12426
12427 @node  Packages in Project Files
12428 @section Packages in Project Files
12429
12430 @noindent
12431 A @emph{package} defines the settings for project-aware tools within a
12432 project.
12433 For each such tool one can declare a package; the names for these
12434 packages are preset (@pxref{Packages}).
12435 A package may contain variable declarations, attribute declarations, and case
12436 constructions.
12437
12438 @smallexample @c projectfile
12439 @group
12440    project Proj is
12441       package Builder is  -- used by gnatmake
12442          for ^Default_Switches^Default_Switches^ ("Ada")
12443              use ("^-v^-v^",
12444                   "^-g^-g^");
12445       end Builder;
12446    end Proj;
12447 @end group
12448 @end smallexample
12449
12450 @noindent
12451 The syntax of package declarations mimics that of package in Ada.
12452
12453 Most of the packages have an attribute
12454 @code{^Default_Switches^Default_Switches^}.
12455 This attribute is an associative array, and its value is a string list.
12456 The index of the associative array is the name of a programming language (case
12457 insensitive). This attribute indicates the ^switch^switch^
12458 or ^switches^switches^ to be used
12459 with the corresponding tool.
12460
12461 Some packages also have another attribute, @code{^Switches^Switches^},
12462 an associative array whose value is a string list.
12463 The index is the name of a source file.
12464 This attribute indicates the ^switch^switch^
12465 or ^switches^switches^ to be used by the corresponding
12466 tool when dealing with this specific file.
12467
12468 Further information on these ^switch^switch^-related attributes is found in
12469 @ref{^Switches^Switches^ and Project Files}.
12470
12471 A package may be declared as a @emph{renaming} of another package; e.g., from
12472 the project file for an imported project.
12473
12474 @smallexample @c projectfile
12475 @group
12476   with "/global/apex.gpr";
12477   project Example is
12478     package Naming renames Apex.Naming;
12479     ...
12480   end Example;
12481 @end group
12482 @end smallexample
12483
12484 @noindent
12485 Packages that are renamed in other project files often come from project files
12486 that have no sources: they are just used as templates. Any modification in the
12487 template will be reflected automatically in all the project files that rename
12488 a package from the template.
12489
12490 In addition to the tool-oriented packages, you can also declare a package
12491 named @code{Naming} to establish specialized source file naming conventions
12492 (@pxref{Naming Schemes}).
12493
12494 @c ************************************
12495 @c * Variables from Imported Projects *
12496 @c ************************************
12497
12498 @node Variables from Imported Projects
12499 @section Variables from Imported Projects
12500
12501 @noindent
12502 An attribute or variable defined in an imported or parent project can
12503 be used in expressions in the importing / extending project.
12504 Such an attribute or variable is denoted by an expanded name whose prefix
12505 is either the name of the project or the expanded name of a package within
12506 a project.
12507
12508 @smallexample @c projectfile
12509 @group
12510   with "imported";
12511   project Main extends "base" is
12512      Var1 := Imported.Var;
12513      Var2 := Base.Var & ".new";
12514 @end group
12515
12516 @group
12517      package Builder is
12518         for ^Default_Switches^Default_Switches^ ("Ada")
12519             use Imported.Builder.Ada_^Switches^Switches^ &
12520                 "^-gnatg^-gnatg^" &
12521                 "^-v^-v^";
12522      end Builder;
12523 @end group
12524
12525 @group
12526      package Compiler is
12527         for ^Default_Switches^Default_Switches^ ("Ada")
12528             use Base.Compiler.Ada_^Switches^Switches^;
12529      end Compiler;
12530   end Main;
12531 @end group
12532 @end smallexample
12533
12534 @noindent
12535 In this example:
12536
12537 @itemize @bullet
12538 @item
12539 The value of @code{Var1} is a copy of the variable @code{Var} defined
12540 in the project file @file{"imported.gpr"}
12541 @item
12542 the value of @code{Var2} is a copy of the value of variable @code{Var}
12543 defined in the project file @file{base.gpr}, concatenated with @code{".new"}
12544 @item
12545 attribute @code{^Default_Switches^Default_Switches^ ("Ada")} in package
12546 @code{Builder} is a string list that includes in its value a copy of the value
12547 of @code{Ada_^Switches^Switches^} defined in the @code{Builder} package
12548 in project file @file{imported.gpr} plus two new elements:
12549 @option{"^-gnatg^-gnatg^"}
12550 and @option{"^-v^-v^"};
12551 @item
12552 attribute @code{^Default_Switches^Default_Switches^ ("Ada")} in package
12553 @code{Compiler} is a copy of the variable @code{Ada_^Switches^Switches^}
12554 defined in the @code{Compiler} package in project file @file{base.gpr},
12555 the project being extended.
12556 @end itemize
12557
12558 @c ******************
12559 @c * Naming Schemes *
12560 @c ******************
12561
12562 @node  Naming Schemes
12563 @section Naming Schemes
12564
12565 @noindent
12566 Sometimes an Ada software system is ported from a foreign compilation
12567 environment to GNAT, and the file names do not use the default GNAT
12568 conventions. Instead of changing all the file names (which for a variety
12569 of reasons might not be possible), you can define the relevant file
12570 naming scheme in the @code{Naming} package in your project file.
12571
12572 @noindent
12573 Note that the use of pragmas described in
12574 @ref{Alternative File Naming Schemes} by mean of a configuration
12575 pragmas file is not supported when using project files. You must use
12576 the features described in this paragraph. You can however use specify
12577 other configuration pragmas (@pxref{Specifying Configuration Pragmas}).
12578
12579 @ifclear vms
12580 For example, the following
12581 package models the Apex file naming rules:
12582
12583 @smallexample @c projectfile
12584 @group
12585   package Naming is
12586     for Casing               use "lowercase";
12587     for Dot_Replacement      use ".";
12588     for Spec_Suffix ("Ada")  use ".1.ada";
12589     for Body_Suffix ("Ada")  use ".2.ada";
12590   end Naming;
12591 @end group
12592 @end smallexample
12593 @end ifclear
12594
12595 @ifset vms
12596 For example, the following package models the HP Ada file naming rules:
12597
12598 @smallexample @c projectfile
12599 @group
12600   package Naming is
12601     for Casing               use "lowercase";
12602     for Dot_Replacement      use "__";
12603     for Spec_Suffix ("Ada")  use "_.^ada^ada^";
12604     for Body_Suffix ("Ada")  use ".^ada^ada^";
12605   end Naming;
12606 @end group
12607 @end smallexample
12608
12609 @noindent
12610 (Note that @code{Casing} is @code{"lowercase"} because GNAT gets the file
12611 names in lower case)
12612 @end ifset
12613
12614 @noindent
12615 You can define the following attributes in package @code{Naming}:
12616
12617 @table @code
12618
12619 @item @var{Casing}
12620 This must be a string with one of the three values @code{"lowercase"},
12621 @code{"uppercase"} or @code{"mixedcase"}; these strings are case insensitive.
12622
12623 @noindent
12624 If @var{Casing} is not specified, then the default is @code{"lowercase"}.
12625
12626 @item @var{Dot_Replacement}
12627 This must be a string whose value satisfies the following conditions:
12628
12629 @itemize @bullet
12630 @item It must not be empty
12631 @item It cannot start or end with an alphanumeric character
12632 @item It cannot be a single underscore
12633 @item It cannot start with an underscore followed by an alphanumeric
12634 @item It cannot contain a dot @code{'.'} except if the entire string
12635 is @code{"."}
12636 @end itemize
12637
12638 @noindent
12639 If @code{Dot_Replacement} is not specified, then the default is @code{"-"}.
12640
12641 @item @var{Spec_Suffix}
12642 This is an associative array (indexed by the programming language name, case
12643 insensitive) whose value is a string that must satisfy the following
12644 conditions:
12645
12646 @itemize @bullet
12647 @item It must not be empty
12648 @item It must include at least one dot
12649 @end itemize
12650 @noindent
12651 If @code{Spec_Suffix ("Ada")} is not specified, then the default is
12652 @code{"^.ads^.ADS^"}.
12653
12654 @item @var{Body_Suffix}
12655 This is an associative array (indexed by the programming language name, case
12656 insensitive) whose value is a string that must satisfy the following
12657 conditions:
12658
12659 @itemize @bullet
12660 @item It must not be empty
12661 @item It must include at least one dot
12662 @item It cannot end with the same string as @code{Spec_Suffix ("Ada")}
12663 @end itemize
12664 @noindent
12665 If @code{Body_Suffix ("Ada")} is not specified, then the default is
12666 @code{"^.adb^.ADB^"}.
12667
12668 @item @var{Separate_Suffix}
12669 This must be a string whose value satisfies the same conditions as
12670 @code{Body_Suffix}.
12671
12672 @noindent
12673 If @code{Separate_Suffix ("Ada")} is not specified, then it defaults to same
12674 value as @code{Body_Suffix ("Ada")}.
12675
12676 @item @var{Spec}
12677 @noindent
12678 You can use the associative array attribute @code{Spec}  to define
12679 the source file name for an individual Ada compilation unit's spec. The array
12680 index must be a string literal that identifies the Ada unit (case insensitive).
12681 The value of this attribute must be a string that identifies the file that
12682 contains this unit's spec (case sensitive or insensitive depending on the
12683 operating system).
12684
12685 @smallexample @c projectfile
12686    for Spec ("MyPack.MyChild") use "mypack.mychild.spec";
12687 @end smallexample
12688
12689 @item @var{Body}
12690
12691 You can use the associative array attribute @code{Body} to
12692 define the source file name for an individual Ada compilation unit's body
12693 (possibly a subunit).  The array index must be a string literal that identifies
12694 the Ada unit (case insensitive).  The value of this attribute must be a string
12695 that identifies the file that contains this unit's body or subunit (case
12696 sensitive or insensitive depending on the operating system).
12697
12698 @smallexample @c projectfile
12699    for Body ("MyPack.MyChild") use "mypack.mychild.body";
12700 @end smallexample
12701 @end table
12702
12703 @c ********************
12704 @c * Library Projects *
12705 @c ********************
12706
12707 @node Library Projects
12708 @section Library Projects
12709
12710 @noindent
12711 @emph{Library projects} are projects whose object code is placed in a library.
12712 (Note that this facility is not yet supported on all platforms)
12713
12714 To create a library project, you need to define in its project file
12715 two project-level attributes: @code{Library_Name} and @code{Library_Dir}.
12716 Additionally, you may define other library-related attributes such as
12717 @code{Library_Kind}, @code{Library_Version}, @code{Library_Interface},
12718 @code{Library_Auto_Init}, @code{Library_Options} and @code{Library_GCC}.
12719
12720 The @code{Library_Name} attribute has a string value. There is no restriction
12721 on the name of a library. It is the responsibility of the developer to
12722 choose a name that will be accepted by the platform. It is recommended to
12723 choose names that could be Ada identifiers; such names are almost guaranteed
12724 to be acceptable on all platforms.
12725
12726 The @code{Library_Dir} attribute has a string value that designates the path
12727 (absolute or relative) of the directory where the library will reside.
12728 It must designate an existing directory, and this directory must be writable,
12729 different from the project's object directory and from any source directory
12730 in the project tree.
12731
12732 If both @code{Library_Name} and @code{Library_Dir} are specified and
12733 are legal, then the project file defines a library project.  The optional
12734 library-related attributes are checked only for such project files.
12735
12736 The @code{Library_Kind} attribute has a string value that must be one of the
12737 following (case insensitive): @code{"static"}, @code{"dynamic"} or
12738 @code{"relocatable"} (which is a synonym for @code{"dynamic"}). If this
12739 attribute is not specified, the library is a static library, that is
12740 an archive of object files that can be potentially linked into an
12741 static executable. Otherwise, the library may be dynamic or
12742 relocatable, that is a library that is loaded only at the start of execution.
12743
12744 If you need to build both a static and a dynamic library, you should use two
12745 different object directories, since in some cases some extra code needs to
12746 be generated for the latter. For such cases, it is recommended to either use
12747 two different project files, or a single one which uses external variables
12748 to indicate what kind of library should be build.
12749
12750 The @code{Library_ALI_Dir} attribute may be specified to indicate the
12751 directory where the ALI files of the libriry will be copied. When it is
12752 not specified, the ALI files are copied ti the directory specified in
12753 attribute @code{Library_Dir}. The directory specified by @code{Library_ALI_Dir}
12754 must be writable and different from the project's object directory and from
12755 any source directory in the project tree.
12756
12757 The @code{Library_Version} attribute has a string value whose interpretation
12758 is platform dependent. It has no effect on VMS and Windows. On Unix, it is
12759 used only for dynamic/relocatable libraries as the internal name of the
12760 library (the @code{"soname"}). If the library file name (built from the
12761 @code{Library_Name}) is different from the @code{Library_Version}, then the
12762 library file will be a symbolic link to the actual file whose name will be
12763 @code{Library_Version}.
12764
12765 Example (on Unix):
12766
12767 @smallexample @c projectfile
12768 @group
12769 project Plib is
12770
12771    Version := "1";
12772
12773    for Library_Dir use "lib_dir";
12774    for Library_Name use "dummy";
12775    for Library_Kind use "relocatable";
12776    for Library_Version use "libdummy.so." & Version;
12777
12778 end Plib;
12779 @end group
12780 @end smallexample
12781
12782 @noindent
12783 Directory @file{lib_dir} will contain the internal library file whose name
12784 will be @file{libdummy.so.1}, and @file{libdummy.so} will be a symbolic link to
12785 @file{libdummy.so.1}.
12786
12787 When @command{gnatmake} detects that a project file
12788 is a library project file, it will check all immediate sources of the project
12789 and rebuild the library if any of the sources have been recompiled.
12790
12791 Standard project files can import library project files. In such cases,
12792 the libraries will only be rebuild if some of its sources are recompiled
12793 because they are in the closure of some other source in an importing project.
12794 Sources of the library project files that are not in such a closure will
12795 not be checked, unless the full library is checked, because one of its sources
12796 needs to be recompiled.
12797
12798 For instance, assume the project file @code{A} imports the library project file
12799 @code{L}. The immediate sources of A are @file{a1.adb}, @file{a2.ads} and
12800 @file{a2.adb}. The immediate sources of L are @file{l1.ads}, @file{l1.adb},
12801 @file{l2.ads}, @file{l2.adb}.
12802
12803 If @file{l1.adb} has been modified, then the library associated with @code{L}
12804 will be rebuild when compiling all the immediate sources of @code{A} only
12805 if @file{a1.ads}, @file{a2.ads} or @file{a2.adb} includes a statement
12806 @code{"with L1;"}.
12807
12808 To be sure that all the sources in the library associated with @code{L} are
12809 up to date, and that all the sources of project @code{A} are also up to date,
12810 the following two commands needs to be used:
12811
12812 @smallexample
12813 gnatmake -Pl.gpr
12814 gnatmake -Pa.gpr
12815 @end smallexample
12816
12817 When a library is built or rebuilt, an attempt is made first to delete all
12818 files in the library directory.
12819 All @file{ALI} files will also be copied from the object directory to the
12820 library directory. To build executables, @command{gnatmake} will use the
12821 library rather than the individual object files.
12822
12823 @ifclear vms
12824 It is also possible to create library project files for third-party libraries
12825 that are precompiled and cannot be compiled locally thanks to the
12826 @code{externally_built} attribute. (See @ref{Installing a library}).
12827 @end ifclear
12828
12829 @c *******************************
12830 @c * Stand-alone Library Projects *
12831 @c *******************************
12832
12833 @node Stand-alone Library Projects
12834 @section Stand-alone Library Projects
12835
12836 @noindent
12837 A Stand-alone Library is a library that contains the necessary code to
12838 elaborate the Ada units that are included in the library. A Stand-alone
12839 Library is suitable to be used in an executable when the main is not
12840 in Ada. However, Stand-alone Libraries may also be used with an Ada main
12841 subprogram.
12842
12843 A Stand-alone Library Project is a Library Project where the library is
12844 a Stand-alone Library.
12845
12846 To be a Stand-alone Library Project, in addition to the two attributes
12847 that make a project a Library Project (@code{Library_Name} and
12848 @code{Library_Dir}, see @ref{Library Projects}), the attribute
12849 @code{Library_Interface} must be defined.
12850
12851 @smallexample @c projectfile
12852 @group
12853    for Library_Dir use "lib_dir";
12854    for Library_Name use "dummy";
12855    for Library_Interface use ("int1", "int1.child");
12856 @end group
12857 @end smallexample
12858
12859 Attribute @code{Library_Interface} has a non empty string list value,
12860 each string in the list designating a unit contained in an immediate source
12861 of the project file.
12862
12863 When a Stand-alone Library is built, first the binder is invoked to build
12864 a package whose name depends on the library name
12865 (^b~dummy.ads/b^B$DUMMY.ADS/B^ in the example above).
12866 This binder-generated package includes initialization and
12867 finalization procedures whose
12868 names depend on the library name (dummyinit and dummyfinal in the example
12869 above). The object corresponding to this package is included in the library.
12870
12871 A dynamic or relocatable Stand-alone Library is automatically initialized
12872 if automatic initialization of Stand-alone Libraries is supported on the
12873 platform and if attribute @code{Library_Auto_Init} is not specified or
12874 is specified with the value "true". A static Stand-alone Library is never
12875 automatically initialized.
12876
12877 Single string attribute @code{Library_Auto_Init} may be specified with only
12878 two possible values: "false" or "true" (case-insensitive). Specifying
12879 "false" for attribute @code{Library_Auto_Init} will prevent automatic
12880 initialization of dynamic or relocatable libraries.
12881
12882 When a non automatically initialized Stand-alone Library is used
12883 in an executable, its initialization procedure must be called before
12884 any service of the library is used.
12885 When the main subprogram is in Ada, it may mean that the initialization
12886 procedure has to be called during elaboration of another package.
12887
12888 For a Stand-Alone Library, only the @file{ALI} files of the Interface Units
12889 (those that are listed in attribute @code{Library_Interface}) are copied to
12890 the Library Directory. As a consequence, only the Interface Units may be
12891 imported from Ada units outside of the library. If other units are imported,
12892 the binding phase will fail.
12893
12894 When a Stand-Alone Library is bound, the switches that are specified in
12895 the attribute @code{Default_Switches ("Ada")} in package @code{Binder} are
12896 used in the call to @command{gnatbind}.
12897
12898 The string list attribute @code{Library_Options} may be used to specified
12899 additional switches to the call to @command{gcc} to link the library.
12900
12901 The attribute @code{Library_Src_Dir}, may be specified for a
12902 Stand-Alone Library. @code{Library_Src_Dir} is a simple attribute that has a
12903 single string value. Its value must be the path (absolute or relative to the
12904 project directory) of an existing directory. This directory cannot be the
12905 object directory or one of the source directories, but it can be the same as
12906 the library directory. The sources of the Interface
12907 Units of the library, necessary to an Ada client of the library, will be
12908 copied to the designated directory, called Interface Copy directory.
12909 These sources includes the specs of the Interface Units, but they may also
12910 include bodies and subunits, when pragmas @code{Inline} or @code{Inline_Always}
12911 are used, or when there is a generic units in the spec. Before the sources
12912 are copied to the Interface Copy directory, an attempt is made to delete all
12913 files in the Interface Copy directory.
12914
12915 @c *************************************
12916 @c * Switches Related to Project Files *
12917 @c *************************************
12918 @node Switches Related to Project Files
12919 @section Switches Related to Project Files
12920
12921 @noindent
12922 The following switches are used by GNAT tools that support project files:
12923
12924 @table @option
12925
12926 @item ^-P^/PROJECT_FILE=^@var{project}
12927 @cindex @option{^-P^/PROJECT_FILE^} (any tool supporting project files)
12928 Indicates the name of a project file. This project file will be parsed with
12929 the verbosity indicated by @option{^-vP^MESSAGE_PROJECT_FILES=^@emph{x}},
12930 if any, and using the external references indicated
12931 by @option{^-X^/EXTERNAL_REFERENCE^} switches, if any.
12932 @ifclear vms
12933 There may zero, one or more spaces between @option{-P} and @var{project}.
12934 @end ifclear
12935
12936 @noindent
12937 There must be only one @option{^-P^/PROJECT_FILE^} switch on the command line.
12938
12939 @noindent
12940 Since the Project Manager parses the project file only after all the switches
12941 on the command line are checked, the order of the switches
12942 @option{^-P^/PROJECT_FILE^},
12943 @option{^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}}
12944 or @option{^-X^/EXTERNAL_REFERENCE^} is not significant.
12945
12946 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
12947 @cindex @option{^-X^/EXTERNAL_REFERENCE^} (any tool supporting project files)
12948 Indicates that external variable @var{name} has the value @var{value}.
12949 The Project Manager will use this value for occurrences of
12950 @code{external(name)} when parsing the project file.
12951
12952 @ifclear vms
12953 @noindent
12954 If @var{name} or @var{value} includes a space, then @var{name=value} should be
12955 put between quotes.
12956 @smallexample
12957   -XOS=NT
12958   -X"user=John Doe"
12959 @end smallexample
12960 @end ifclear
12961
12962 @noindent
12963 Several @option{^-X^/EXTERNAL_REFERENCE^} switches can be used simultaneously.
12964 If several @option{^-X^/EXTERNAL_REFERENCE^} switches specify the same
12965 @var{name}, only the last one is used.
12966
12967 @noindent
12968 An external variable specified with a @option{^-X^/EXTERNAL_REFERENCE^} switch
12969 takes precedence over the value of the same name in the environment.
12970
12971 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
12972 @cindex @code{^-vP^/MESSAGES_PROJECT_FILE^} (any tool supporting project files)
12973 @c Previous line uses code vs option command, to stay less than 80 chars
12974 Indicates the verbosity of the parsing of GNAT project files.
12975
12976 @ifclear vms
12977 @option{-vP0} means Default;
12978 @option{-vP1} means Medium;
12979 @option{-vP2} means High.
12980 @end ifclear
12981
12982 @ifset vms
12983 There are three possible options for this qualifier: DEFAULT, MEDIUM and
12984 HIGH.
12985 @end ifset
12986
12987 @noindent
12988 The default is ^Default^DEFAULT^: no output for syntactically correct
12989 project files.
12990 @noindent
12991 If several @option{^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}} switches are present,
12992 only the last one is used.
12993
12994 @end table
12995
12996 @c **********************************
12997 @c * Tools Supporting Project Files *
12998 @c **********************************
12999
13000 @node  Tools Supporting Project Files
13001 @section Tools Supporting Project Files
13002
13003 @menu
13004 * gnatmake and Project Files::
13005 * The GNAT Driver and Project Files::
13006 @ifclear vms
13007 * Glide and Project Files::
13008 @end ifclear
13009 @end menu
13010
13011 @node gnatmake and Project Files
13012 @subsection gnatmake and Project Files
13013
13014 @noindent
13015 This section covers several topics related to @command{gnatmake} and
13016 project files: defining ^switches^switches^ for @command{gnatmake}
13017 and for the tools that it invokes; specifying configuration pragmas;
13018 the use of the @code{Main} attribute; building and rebuilding library project
13019 files.
13020
13021 @menu
13022 * ^Switches^Switches^ and Project Files::
13023 * Specifying Configuration Pragmas::
13024 * Project Files and Main Subprograms::
13025 * Library Project Files::
13026 @end menu
13027
13028 @node ^Switches^Switches^ and Project Files
13029 @subsubsection ^Switches^Switches^ and Project Files
13030
13031 @ifset vms
13032 It is not currently possible to specify VMS style qualifiers in the project
13033 files; only Unix style ^switches^switches^ may be specified.
13034 @end ifset
13035
13036 @noindent
13037 For each of the packages @code{Builder}, @code{Compiler}, @code{Binder}, and
13038 @code{Linker}, you can specify a @code{^Default_Switches^Default_Switches^}
13039 attribute, a @code{^Switches^Switches^} attribute, or both;
13040 as their names imply, these ^switch^switch^-related
13041 attributes affect the ^switches^switches^ that are used for each of these GNAT
13042 components when
13043 @command{gnatmake} is invoked.  As will be explained below, these
13044 component-specific ^switches^switches^ precede
13045 the ^switches^switches^ provided on the @command{gnatmake} command line.
13046
13047 The @code{^Default_Switches^Default_Switches^} attribute is an associative
13048 array indexed by language name (case insensitive) whose value is a string list.
13049 For example:
13050
13051 @smallexample @c projectfile
13052 @group
13053 package Compiler is
13054   for ^Default_Switches^Default_Switches^ ("Ada")
13055       use ("^-gnaty^-gnaty^",
13056            "^-v^-v^");
13057 end Compiler;
13058 @end group
13059 @end smallexample
13060
13061 @noindent
13062 The @code{^Switches^Switches^} attribute is also an associative array,
13063 indexed by a file name (which may or may not be case sensitive, depending
13064 on the operating system) whose value is a string list.  For example:
13065
13066 @smallexample @c projectfile
13067 @group
13068 package Builder is
13069    for ^Switches^Switches^ ("main1.adb")
13070        use ("^-O2^-O2^");
13071    for ^Switches^Switches^ ("main2.adb")
13072        use ("^-g^-g^");
13073 end Builder;
13074 @end group
13075 @end smallexample
13076
13077 @noindent
13078 For the @code{Builder} package, the file names must designate source files
13079 for main subprograms.  For the @code{Binder} and @code{Linker} packages, the
13080 file names must designate @file{ALI} or source files for main subprograms.
13081 In each case just the file name without an explicit extension is acceptable.
13082
13083 For each tool used in a program build (@command{gnatmake}, the compiler, the
13084 binder, and the linker), the corresponding package @dfn{contributes} a set of
13085 ^switches^switches^ for each file on which the tool is invoked, based on the
13086 ^switch^switch^-related attributes defined in the package.
13087 In particular, the ^switches^switches^
13088 that each of these packages contributes for a given file @var{f} comprise:
13089
13090 @itemize @bullet
13091 @item
13092 the value of attribute @code{^Switches^Switches^ (@var{f})},
13093 if it is specified in the package for the given file,
13094 @item
13095 otherwise, the value of @code{^Default_Switches^Default_Switches^ ("Ada")},
13096 if it is specified in the package.
13097 @end itemize
13098
13099 @noindent
13100 If neither of these attributes is defined in the package, then the package does
13101 not contribute any ^switches^switches^ for the given file.
13102
13103 When @command{gnatmake} is invoked on a file, the ^switches^switches^ comprise
13104 two sets, in the following order: those contributed for the file
13105 by the @code{Builder} package;
13106 and the switches passed on the command line.
13107
13108 When @command{gnatmake} invokes a tool (compiler, binder, linker) on a file,
13109 the ^switches^switches^ passed to the tool comprise three sets,
13110 in the following order:
13111
13112 @enumerate
13113 @item
13114 the applicable ^switches^switches^ contributed for the file
13115 by the @code{Builder} package in the project file supplied on the command line;
13116
13117 @item
13118 those contributed for the file by the package (in the relevant project file --
13119 see below) corresponding to the tool; and
13120
13121 @item
13122 the applicable switches passed on the command line.
13123 @end enumerate
13124
13125 @noindent
13126 The term @emph{applicable ^switches^switches^} reflects the fact that
13127 @command{gnatmake} ^switches^switches^ may or may not be passed to individual
13128 tools, depending on the individual ^switch^switch^.
13129
13130 @command{gnatmake} may invoke the compiler on source files from different
13131 projects. The Project Manager will use the appropriate project file to
13132 determine the @code{Compiler} package for each source file being compiled.
13133 Likewise for the @code{Binder} and @code{Linker} packages.
13134
13135 As an example, consider the following package in a project file:
13136
13137 @smallexample @c projectfile
13138 @group
13139 project Proj1 is
13140    package Compiler is
13141       for ^Default_Switches^Default_Switches^ ("Ada")
13142           use ("^-g^-g^");
13143       for ^Switches^Switches^ ("a.adb")
13144           use ("^-O1^-O1^");
13145       for ^Switches^Switches^ ("b.adb")
13146           use ("^-O2^-O2^",
13147                "^-gnaty^-gnaty^");
13148    end Compiler;
13149 end Proj1;
13150 @end group
13151 @end smallexample
13152
13153 @noindent
13154 If @command{gnatmake} is invoked with this project file, and it needs to
13155 compile, say, the files @file{a.adb}, @file{b.adb}, and @file{c.adb}, then
13156 @file{a.adb} will be compiled with the ^switch^switch^
13157 @option{^-O1^-O1^},
13158 @file{b.adb} with ^switches^switches^
13159 @option{^-O2^-O2^}
13160 and @option{^-gnaty^-gnaty^},
13161 and @file{c.adb} with @option{^-g^-g^}.
13162
13163 The following example illustrates the ordering of the ^switches^switches^
13164 contributed by different packages:
13165
13166 @smallexample @c projectfile
13167 @group
13168 project Proj2 is
13169    package Builder is
13170       for ^Switches^Switches^ ("main.adb")
13171           use ("^-g^-g^",
13172                "^-O1^-)1^",
13173                "^-f^-f^");
13174    end Builder;
13175 @end group
13176
13177 @group
13178    package Compiler is
13179       for ^Switches^Switches^ ("main.adb")
13180           use ("^-O2^-O2^");
13181    end Compiler;
13182 end Proj2;
13183 @end group
13184 @end smallexample
13185
13186 @noindent
13187 If you issue the command:
13188
13189 @smallexample
13190     gnatmake ^-Pproj2^/PROJECT_FILE=PROJ2^ -O0 main
13191 @end smallexample
13192
13193 @noindent
13194 then the compiler will be invoked on @file{main.adb} with the following
13195 sequence of ^switches^switches^
13196
13197 @smallexample
13198    ^-g -O1 -O2 -O0^-g -O1 -O2 -O0^
13199 @end smallexample
13200
13201 with the last @option{^-O^-O^}
13202 ^switch^switch^ having precedence over the earlier ones;
13203 several other ^switches^switches^
13204 (such as @option{^-c^-c^}) are added implicitly.
13205
13206 The ^switches^switches^
13207 @option{^-g^-g^}
13208 and @option{^-O1^-O1^} are contributed by package
13209 @code{Builder},  @option{^-O2^-O2^} is contributed
13210 by the package @code{Compiler}
13211 and @option{^-O0^-O0^} comes from the command line.
13212
13213 The @option{^-g^-g^}
13214 ^switch^switch^ will also be passed in the invocation of
13215 @command{Gnatlink.}
13216
13217 A final example illustrates switch contributions from packages in different
13218 project files:
13219
13220 @smallexample @c projectfile
13221 @group
13222 project Proj3 is
13223    for Source_Files use ("pack.ads", "pack.adb");
13224    package Compiler is
13225       for ^Default_Switches^Default_Switches^ ("Ada")
13226           use ("^-gnata^-gnata^");
13227    end Compiler;
13228 end Proj3;
13229 @end group
13230
13231 @group
13232 with "Proj3";
13233 project Proj4 is
13234    for Source_Files use ("foo_main.adb", "bar_main.adb");
13235    package Builder is
13236       for ^Switches^Switches^ ("foo_main.adb")
13237           use ("^-s^-s^",
13238                "^-g^-g^");
13239    end Builder;
13240 end Proj4;
13241 @end group
13242
13243 @group
13244 -- Ada source file:
13245 with Pack;
13246 procedure Foo_Main is
13247    ...
13248 end Foo_Main;
13249 @end group
13250 @end smallexample
13251
13252 If the command is
13253 @smallexample
13254 gnatmake ^-PProj4^/PROJECT_FILE=PROJ4^ foo_main.adb -cargs -gnato
13255 @end smallexample
13256
13257 @noindent
13258 then the ^switches^switches^ passed to the compiler for @file{foo_main.adb} are
13259 @option{^-g^-g^} (contributed by the package @code{Proj4.Builder}) and
13260 @option{^-gnato^-gnato^} (passed on the command line).
13261 When the imported package @code{Pack} is compiled, the ^switches^switches^ used
13262 are @option{^-g^-g^} from @code{Proj4.Builder},
13263 @option{^-gnata^-gnata^} (contributed from package @code{Proj3.Compiler},
13264 and @option{^-gnato^-gnato^} from the command line.
13265
13266 @noindent
13267 When using @command{gnatmake} with project files, some ^switches^switches^ or
13268 arguments may be expressed as relative paths. As the working directory where
13269 compilation occurs may change, these relative paths are converted to absolute
13270 paths. For the ^switches^switches^ found in a project file, the relative paths
13271 are relative to the project file directory, for the switches on the command
13272 line, they are relative to the directory where @command{gnatmake} is invoked.
13273 The ^switches^switches^ for which this occurs are:
13274 ^-I^-I^,
13275 ^-A^-A^,
13276 ^-L^-L^,
13277 ^-aO^-aO^,
13278 ^-aL^-aL^,
13279 ^-aI^-aI^, as well as all arguments that are not switches (arguments to
13280 ^switch^switch^
13281 ^-o^-o^, object files specified in package @code{Linker} or after
13282 -largs on the command line). The exception to this rule is the ^switch^switch^
13283 ^--RTS=^--RTS=^ for which a relative path argument is never converted.
13284
13285 @node Specifying Configuration Pragmas
13286 @subsubsection Specifying Configuration Pragmas
13287
13288 When using @command{gnatmake} with project files, if there exists a file
13289 @file{gnat.adc} that contains configuration pragmas, this file will be
13290 ignored.
13291
13292 Configuration pragmas can be defined by means of the following attributes in
13293 project files: @code{Global_Configuration_Pragmas} in package @code{Builder}
13294 and @code{Local_Configuration_Pragmas} in package @code{Compiler}.
13295
13296 Both these attributes are single string attributes. Their values is the path
13297 name of a file containing configuration pragmas. If a path name is relative,
13298 then it is relative to the project directory of the project file where the
13299 attribute is defined.
13300
13301 When compiling a source, the configuration pragmas used are, in order,
13302 those listed in the file designated by attribute
13303 @code{Global_Configuration_Pragmas} in package @code{Builder} of the main
13304 project file, if it is specified, and those listed in the file designated by
13305 attribute @code{Local_Configuration_Pragmas} in package @code{Compiler} of
13306 the project file of the source, if it exists.
13307
13308 @node Project Files and Main Subprograms
13309 @subsubsection Project Files and Main Subprograms
13310
13311 @noindent
13312 When using a project file, you can invoke @command{gnatmake}
13313 with one or several main subprograms, by specifying their source files on the
13314 command line.
13315
13316 @smallexample
13317     gnatmake ^-P^/PROJECT_FILE=^prj main1 main2 main3
13318 @end smallexample
13319
13320 @noindent
13321 Each of these needs to be a source file of the same project, except
13322 when the switch ^-u^/UNIQUE^ is used.
13323
13324 @noindent
13325 When ^-u^/UNIQUE^ is not used, all the mains need to be sources of the
13326 same project, one of the project in the tree rooted at the project specified
13327 on the command line. The package @code{Builder} of this common project, the
13328 "main project" is the one that is considered by @command{gnatmake}.
13329
13330 @noindent
13331 When ^-u^/UNIQUE^ is used, the specified source files may be in projects
13332 imported directly or indirectly by the project specified on the command line.
13333 Note that if such a source file is not part of the project specified on the
13334 command line, the ^switches^switches^ found in package @code{Builder} of the
13335 project specified on the command line, if any, that are transmitted
13336 to the compiler will still be used, not those found in the project file of
13337 the source file.
13338
13339 @noindent
13340 When using a project file, you can also invoke @command{gnatmake} without
13341 explicitly specifying any main, and the effect depends on whether you have
13342 defined the @code{Main} attribute.  This attribute has a string list value,
13343 where each element in the list is the name of a source file (the file
13344 extension is optional) that contains a unit that can be a main subprogram.
13345
13346 If the @code{Main} attribute is defined in a project file as a non-empty
13347 string list and the switch @option{^-u^/UNIQUE^} is not used on the command
13348 line, then invoking @command{gnatmake} with this project file but without any
13349 main on the command line is equivalent to invoking @command{gnatmake} with all
13350 the file names in the @code{Main} attribute on the command line.
13351
13352 Example:
13353 @smallexample @c projectfile
13354 @group
13355    project Prj is
13356       for Main use ("main1", "main2", "main3");
13357    end Prj;
13358 @end group
13359 @end smallexample
13360
13361 @noindent
13362 With this project file, @code{"gnatmake ^-Pprj^/PROJECT_FILE=PRJ^"}
13363 is equivalent to
13364 @code{"gnatmake ^-Pprj^/PROJECT_FILE=PRJ^ main1 main2 main3"}.
13365
13366 When the project attribute @code{Main} is not specified, or is specified
13367 as an empty string list, or when the switch @option{-u} is used on the command
13368 line, then invoking @command{gnatmake} with no main on the command line will
13369 result in all immediate sources of the project file being checked, and
13370 potentially recompiled. Depending on the presence of the switch @option{-u},
13371 sources from other project files on which the immediate sources of the main
13372 project file depend are also checked and potentially recompiled. In other
13373 words, the @option{-u} switch is applied to all of the immediate sources of the
13374 main project file.
13375
13376 When no main is specified on the command line and attribute @code{Main} exists
13377 and includes several mains, or when several mains are specified on the
13378 command line, the default ^switches^switches^ in package @code{Builder} will
13379 be used for all mains, even if there are specific ^switches^switches^
13380 specified for one or several mains.
13381
13382 But the ^switches^switches^ from package @code{Binder} or @code{Linker} will be
13383 the specific ^switches^switches^ for each main, if they are specified.
13384
13385 @node Library Project Files
13386 @subsubsection Library Project Files
13387
13388 @noindent
13389 When @command{gnatmake} is invoked with a main project file that is a library
13390 project file, it is not allowed to specify one or more mains on the command
13391 line.
13392
13393 @noindent
13394 When a library project file is specified, switches ^-b^/ACTION=BIND^ and
13395 ^-l^/ACTION=LINK^ have special meanings.
13396
13397 @itemize @bullet
13398 @item ^-b^/ACTION=BIND^ is only allowed for stand-alone libraries. It indicates
13399 to @command{gnatmake} that @command{gnatbind} should be invoked for the
13400 library.
13401
13402 @item ^-l^/ACTION=LINK^ may be used for all library projects. It indicates
13403 to @command{gnatmake} that the binder generated file should be compiled
13404 (in the case of a stand-alone library) and that the library should be built.
13405
13406 @end itemize
13407
13408 @node The GNAT Driver and Project Files
13409 @subsection The GNAT Driver and Project Files
13410
13411 @noindent
13412 A number of GNAT tools, other than @command{^gnatmake^gnatmake^}
13413 are project-aware:
13414 @command{^gnatbind^gnatbind^},
13415 @command{^gnatfind^gnatfind^},
13416 @command{^gnatlink^gnatlink^},
13417 @command{^gnatls^gnatls^},
13418 @command{^gnatelim^gnatelim^},
13419 @command{^gnatpp^gnatpp^},
13420 @command{^gnatmetric^gnatmetric^},
13421 @command{^gnatstub^gnatstub^},
13422 and @command{^gnatxref^gnatxref^}. However, none of these tools can be invoked
13423 directly with a project file switch (@option{^-P^/PROJECT_FILE=^}).
13424 They must be invoked through the @command{gnat} driver.
13425
13426 The @command{gnat} driver is a front-end that accepts a number of commands and
13427 call the corresponding tool. It has been designed initially for VMS to convert
13428 VMS style qualifiers to Unix style switches, but it is now available to all
13429 the GNAT supported platforms.
13430
13431 On non VMS platforms, the @command{gnat} driver accepts the following commands
13432 (case insensitive):
13433
13434 @itemize @bullet
13435 @item
13436 BIND to invoke @command{^gnatbind^gnatbind^}
13437 @item
13438 CHOP to invoke @command{^gnatchop^gnatchop^}
13439 @item
13440 CLEAN to invoke @command{^gnatclean^gnatclean^}
13441 @item
13442 COMP or COMPILE to invoke the compiler
13443 @item
13444 ELIM to invoke @command{^gnatelim^gnatelim^}
13445 @item
13446 FIND to invoke @command{^gnatfind^gnatfind^}
13447 @item
13448 KR or KRUNCH to invoke @command{^gnatkr^gnatkr^}
13449 @item
13450 LINK to invoke @command{^gnatlink^gnatlink^}
13451 @item
13452 LS or LIST to invoke @command{^gnatls^gnatls^}
13453 @item
13454 MAKE to invoke @command{^gnatmake^gnatmake^}
13455 @item
13456 NAME to invoke @command{^gnatname^gnatname^}
13457 @item
13458 PREP or PREPROCESS to invoke @command{^gnatprep^gnatprep^}
13459 @item
13460 PP or PRETTY to invoke @command{^gnatpp^gnatpp^}
13461 @item
13462 METRIC to invoke @command{^gnatmetric^gnatmetric^}
13463 @item
13464 STUB to invoke @command{^gnatstub^gnatstub^}
13465 @item
13466 XREF to invoke @command{^gnatxref^gnatxref^}
13467 @end itemize
13468
13469 @noindent
13470 (note that the compiler is invoked using the command
13471 @command{^gnatmake -f -u -c^gnatmake -f -u -c^}).
13472
13473 @noindent
13474 On non VMS platforms, between @command{gnat} and the command, two
13475 special switches may be used:
13476
13477 @itemize @bullet
13478 @item
13479 @command{-v} to display the invocation of the tool.
13480 @item
13481 @command{-dn} to prevent the @command{gnat} driver from removing
13482 the temporary files it has created. These temporary files are
13483 configuration files and temporary file list files.
13484 @end itemize
13485
13486 @noindent
13487 The command may be followed by switches and arguments for the invoked
13488 tool.
13489
13490 @smallexample
13491   gnat bind -C main.ali
13492   gnat ls -a main
13493   gnat chop foo.txt
13494 @end smallexample
13495
13496 @noindent
13497 Switches may also be put in text files, one switch per line, and the text
13498 files may be specified with their path name preceded by '@@'.
13499
13500 @smallexample
13501    gnat bind @@args.txt main.ali
13502 @end smallexample
13503
13504 @noindent
13505 In addition, for commands BIND, COMP or COMPILE, FIND, ELIM, LS or LIST, LINK,
13506 METRIC, PP or PRETTY, STUB and XREF, the project file related switches
13507 (@option{^-P^/PROJECT_FILE^},
13508 @option{^-X^/EXTERNAL_REFERENCE^} and
13509 @option{^-vP^/MESSAGES_PROJECT_FILE=^x}) may be used in addition to
13510 the switches of the invoking tool.
13511
13512 @noindent
13513 When GNAT PP or GNAT PRETTY is used with a project file, but with no source
13514 specified on the command line, it invokes @command{^gnatpp^gnatpp^} with all
13515 the immediate sources of the specified project file.
13516
13517 @noindent
13518 When GNAT METRIC is used with a project file, but with no source
13519 specified on the command line, it invokes @command{^gnatmetric^gnatmetric^}
13520 with all the immediate sources of the specified project file and with
13521 @option{^-d^/DIRECTORY^} with the parameter pointing to the object directory
13522 of the project.
13523
13524 @noindent
13525 In addition, when GNAT PP, GNAT PRETTY or GNAT METRIC is used with
13526 a project file, no source is specified on the command line and
13527 switch ^-U^/ALL_PROJECTS^ is specified on the command line, then
13528 the underlying tool (^gnatpp^gnatpp^ or
13529 ^gnatmetric^gnatmetric^) is invoked for all sources of all projects,
13530 not only for the immediate sources of the main project.
13531 @ifclear vms
13532 (-U stands for Universal or Union of the project files of the project tree)
13533 @end ifclear
13534
13535 @noindent
13536 For each of the following commands, there is optionally a corresponding
13537 package in the main project.
13538
13539 @itemize @bullet
13540 @item
13541 package @code{Binder} for command BIND (invoking @code{^gnatbind^gnatbind^})
13542
13543 @item
13544 package @code{Compiler} for command COMP or COMPILE (invoking the compiler)
13545
13546 @item
13547 package @code{Finder} for command FIND (invoking @code{^gnatfind^gnatfind^})
13548
13549 @item
13550 package @code{Eliminate} for command ELIM (invoking
13551 @code{^gnatelim^gnatelim^})
13552
13553 @item
13554 package @code{Gnatls} for command LS or LIST (invoking @code{^gnatls^gnatls^})
13555
13556 @item
13557 package @code{Linker} for command LINK (invoking @code{^gnatlink^gnatlink^})
13558
13559 @item
13560 package @code{Metrics} for command METRIC
13561 (invoking @code{^gnatmetric^gnatmetric^})
13562
13563 @item
13564 package @code{Pretty_Printer} for command PP or PRETTY
13565 (invoking @code{^gnatpp^gnatpp^})
13566
13567 @item
13568 package @code{Gnatstub} for command STUB
13569 (invoking @code{^gnatstub^gnatstub^})
13570
13571 @item
13572 package @code{Cross_Reference} for command XREF (invoking
13573 @code{^gnatxref^gnatxref^})
13574
13575 @end itemize
13576
13577 @noindent
13578 Package @code{Gnatls} has a unique attribute @code{^Switches^Switches^},
13579 a simple variable with a string list value. It contains ^switches^switches^
13580 for the invocation of @code{^gnatls^gnatls^}.
13581
13582 @smallexample @c projectfile
13583 @group
13584 project Proj1 is
13585    package gnatls is
13586       for ^Switches^Switches^
13587           use ("^-a^-a^",
13588                "^-v^-v^");
13589    end gnatls;
13590 end Proj1;
13591 @end group
13592 @end smallexample
13593
13594 @noindent
13595 All other packages have two attribute @code{^Switches^Switches^} and
13596 @code{^Default_Switches^Default_Switches^}.
13597
13598 @noindent
13599 @code{^Switches^Switches^} is an associated array attribute, indexed by the
13600 source file name, that has a string list value: the ^switches^switches^ to be
13601 used when the tool corresponding to the package is invoked for the specific
13602 source file.
13603
13604 @noindent
13605 @code{^Default_Switches^Default_Switches^} is an associative array attribute,
13606 indexed by  the programming language that has a string list value.
13607 @code{^Default_Switches^Default_Switches^ ("Ada")} contains the
13608 ^switches^switches^ for the invocation of the tool corresponding
13609 to the package, except if a specific @code{^Switches^Switches^} attribute
13610 is specified for the source file.
13611
13612 @smallexample @c projectfile
13613 @group
13614 project Proj is
13615
13616    for Source_Dirs use ("./**");
13617
13618    package gnatls is
13619       for ^Switches^Switches^ use
13620           ("^-a^-a^",
13621            "^-v^-v^");
13622    end gnatls;
13623 @end group
13624 @group
13625
13626    package Compiler is
13627       for ^Default_Switches^Default_Switches^ ("Ada")
13628           use ("^-gnatv^-gnatv^",
13629                "^-gnatwa^-gnatwa^");
13630    end Binder;
13631 @end group
13632 @group
13633
13634    package Binder is
13635       for ^Default_Switches^Default_Switches^ ("Ada")
13636           use ("^-C^-C^",
13637                "^-e^-e^");
13638    end Binder;
13639 @end group
13640 @group
13641
13642    package Linker is
13643       for ^Default_Switches^Default_Switches^ ("Ada")
13644           use ("^-C^-C^");
13645       for ^Switches^Switches^ ("main.adb")
13646           use ("^-C^-C^",
13647                "^-v^-v^",
13648                "^-v^-v^");
13649    end Linker;
13650 @end group
13651 @group
13652
13653    package Finder is
13654       for ^Default_Switches^Default_Switches^ ("Ada")
13655            use ("^-a^-a^",
13656                 "^-f^-f^");
13657    end Finder;
13658 @end group
13659 @group
13660
13661    package Cross_Reference is
13662       for ^Default_Switches^Default_Switches^ ("Ada")
13663           use ("^-a^-a^",
13664                "^-f^-f^",
13665                "^-d^-d^",
13666                "^-u^-u^");
13667    end Cross_Reference;
13668 end Proj;
13669 @end group
13670 @end smallexample
13671
13672 @noindent
13673 With the above project file, commands such as
13674
13675 @smallexample
13676    ^gnat comp -Pproj main^GNAT COMP /PROJECT_FILE=PROJ MAIN^
13677    ^gnat ls -Pproj main^GNAT LIST /PROJECT_FILE=PROJ MAIN^
13678    ^gnat xref -Pproj main^GNAT XREF /PROJECT_FILE=PROJ MAIN^
13679    ^gnat bind -Pproj main.ali^GNAT BIND /PROJECT_FILE=PROJ MAIN.ALI^
13680    ^gnat link -Pproj main.ali^GNAT LINK /PROJECT_FILE=PROJ MAIN.ALI^
13681 @end smallexample
13682
13683 @noindent
13684 will set up the environment properly and invoke the tool with the switches
13685 found in the package corresponding to the tool:
13686 @code{^Default_Switches^Default_Switches^ ("Ada")} for all tools,
13687 except @code{^Switches^Switches^ ("main.adb")}
13688 for @code{^gnatlink^gnatlink^}.
13689
13690 @ifclear vms
13691 @node Glide and Project Files
13692 @subsection Glide and Project Files
13693
13694 @noindent
13695 Glide will automatically recognize the @file{.gpr} extension for
13696 project files, and will
13697 convert them to its own internal format automatically. However, it
13698 doesn't provide a syntax-oriented editor for modifying these
13699 files.
13700 The project file will be loaded as text when you select the menu item
13701 @code{Ada} @result{} @code{Project} @result{} @code{Edit}.
13702 You can edit this text and save the @file{gpr} file;
13703 when you next select this project file in Glide it
13704 will be automatically reloaded.
13705 @end ifclear
13706
13707 @c **********************
13708 @node An Extended Example
13709 @section An Extended Example
13710
13711 @noindent
13712 Suppose that we have two programs, @var{prog1} and @var{prog2},
13713 whose sources are in corresponding directories. We would like
13714 to build them with a single @command{gnatmake} command, and we want to place
13715 their object files into @file{build} subdirectories of the source directories.
13716 Furthermore, we want to have to have two separate subdirectories
13717 in @file{build}  -- @file{release} and @file{debug} -- which will contain
13718 the object files compiled with different set of compilation flags.
13719
13720 In other words, we have the following structure:
13721
13722 @smallexample
13723 @group
13724    main
13725      |- prog1
13726      |    |- build
13727      |         | debug
13728      |         | release
13729      |- prog2
13730           |- build
13731                | debug
13732                | release
13733 @end group
13734 @end smallexample
13735
13736 @noindent
13737 Here are the project files that we must place in a directory @file{main}
13738 to maintain this structure:
13739
13740 @enumerate
13741
13742 @item We create a @code{Common} project with a package @code{Compiler} that
13743 specifies the compilation ^switches^switches^:
13744
13745 @smallexample
13746 File "common.gpr":
13747 @group
13748 @b{project} Common @b{is}
13749
13750    @b{for} Source_Dirs @b{use} (); -- No source files
13751 @end group
13752
13753 @group
13754    @b{type} Build_Type @b{is} ("release", "debug");
13755    Build : Build_Type := External ("BUILD", "debug");
13756 @end group
13757 @group
13758    @b{package} Compiler @b{is}
13759       @b{case} Build @b{is}
13760          @b{when} "release" =>
13761            @b{for} ^Default_Switches^Default_Switches^ ("Ada")
13762                    @b{use} ("^-O2^-O2^");
13763          @b{when} "debug"   =>
13764            @b{for} ^Default_Switches^Default_Switches^ ("Ada")
13765                    @b{use} ("^-g^-g^");
13766       @b{end case};
13767    @b{end} Compiler;
13768
13769 @b{end} Common;
13770 @end group
13771 @end smallexample
13772
13773 @item We create separate projects for the two programs:
13774
13775 @smallexample
13776 @group
13777 File "prog1.gpr":
13778
13779 @b{with} "common";
13780 @b{project} Prog1 @b{is}
13781
13782     @b{for} Source_Dirs @b{use} ("prog1");
13783     @b{for} Object_Dir  @b{use} "prog1/build/" & Common.Build;
13784
13785     @b{package} Compiler @b{renames} Common.Compiler;
13786
13787 @b{end} Prog1;
13788 @end group
13789 @end smallexample
13790
13791 @smallexample
13792 @group
13793 File "prog2.gpr":
13794
13795 @b{with} "common";
13796 @b{project} Prog2 @b{is}
13797
13798     @b{for} Source_Dirs @b{use} ("prog2");
13799     @b{for} Object_Dir  @b{use} "prog2/build/" & Common.Build;
13800
13801     @b{package} Compiler @b{renames} Common.Compiler;
13802
13803 @end group
13804 @b{end} Prog2;
13805 @end smallexample
13806
13807 @item We create a wrapping project @code{Main}:
13808
13809 @smallexample
13810 @group
13811 File "main.gpr":
13812
13813 @b{with} "common";
13814 @b{with} "prog1";
13815 @b{with} "prog2";
13816 @b{project} Main @b{is}
13817
13818    @b{package} Compiler @b{renames} Common.Compiler;
13819
13820 @b{end} Main;
13821 @end group
13822 @end smallexample
13823
13824 @item Finally we need to create a dummy procedure that @code{with}s (either
13825 explicitly or implicitly) all the sources of our two programs.
13826
13827 @end enumerate
13828
13829 @noindent
13830 Now we can build the programs using the command
13831
13832 @smallexample
13833    gnatmake ^-P^/PROJECT_FILE=^main dummy
13834 @end smallexample
13835
13836 @noindent
13837 for the Debug mode, or
13838
13839 @ifclear vms
13840 @smallexample
13841    gnatmake -Pmain -XBUILD=release
13842 @end smallexample
13843 @end ifclear
13844
13845 @ifset vms
13846 @smallexample
13847    GNAT MAKE /PROJECT_FILE=main /EXTERNAL_REFERENCE=BUILD=release
13848 @end smallexample
13849 @end ifset
13850
13851 @noindent
13852 for the Release mode.
13853
13854 @c ********************************
13855 @c * Project File Complete Syntax *
13856 @c ********************************
13857
13858 @node Project File Complete Syntax
13859 @section Project File Complete Syntax
13860
13861 @smallexample
13862 project ::=
13863   context_clause project_declaration
13864
13865 context_clause ::=
13866   @{with_clause@}
13867
13868 with_clause ::=
13869   @b{with} path_name @{ , path_name @} ;
13870
13871 path_name ::=
13872    string_literal
13873
13874 project_declaration ::=
13875   simple_project_declaration | project_extension
13876
13877 simple_project_declaration ::=
13878   @b{project} <project_>simple_name @b{is}
13879     @{declarative_item@}
13880   @b{end} <project_>simple_name;
13881
13882 project_extension ::=
13883   @b{project} <project_>simple_name  @b{extends} path_name @b{is}
13884     @{declarative_item@}
13885   @b{end} <project_>simple_name;
13886
13887 declarative_item ::=
13888   package_declaration |
13889   typed_string_declaration |
13890   other_declarative_item
13891
13892 package_declaration ::=
13893   package_specification | package_renaming
13894
13895 package_specification ::=
13896   @b{package} package_identifier @b{is}
13897     @{simple_declarative_item@}
13898   @b{end} package_identifier ;
13899
13900 package_identifier ::=
13901   @code{Naming} | @code{Builder} | @code{Compiler} | @code{Binder} |
13902   @code{Linker} | @code{Finder}  | @code{Cross_Reference} |
13903   @code{^gnatls^gnatls^} | @code{IDE}     | @code{Pretty_Printer}
13904
13905 package_renaming ::==
13906   @b{package} package_identifier @b{renames}
13907        <project_>simple_name.package_identifier ;
13908
13909 typed_string_declaration ::=
13910   @b{type} <typed_string_>_simple_name @b{is}
13911    ( string_literal @{, string_literal@} );
13912
13913 other_declarative_item ::=
13914   attribute_declaration |
13915   typed_variable_declaration |
13916   variable_declaration |
13917   case_construction
13918
13919 attribute_declaration ::=
13920   full_associative_array_declaration |
13921   @b{for} attribute_designator @b{use} expression ;
13922
13923 full_associative_array_declaration ::=
13924   @b{for} <associative_array_attribute_>simple_name @b{use}
13925   <project_>simple_name [ . <package_>simple_Name ] ' <attribute_>simple_name ;
13926
13927 attribute_designator ::=
13928   <simple_attribute_>simple_name |
13929   <associative_array_attribute_>simple_name ( string_literal )
13930
13931 typed_variable_declaration ::=
13932   <typed_variable_>simple_name : <typed_string_>name :=  string_expression ;
13933
13934 variable_declaration ::=
13935   <variable_>simple_name := expression;
13936
13937 expression ::=
13938   term @{& term@}
13939
13940 term ::=
13941   literal_string |
13942   string_list |
13943   <variable_>name |
13944   external_value |
13945   attribute_reference
13946
13947 string_literal ::=
13948   (same as Ada)
13949
13950 string_list ::=
13951   ( <string_>expression @{ , <string_>expression @} )
13952
13953 external_value ::=
13954   @b{external} ( string_literal [, string_literal] )
13955
13956 attribute_reference ::=
13957   attribute_prefix ' <simple_attribute_>simple_name [ ( literal_string ) ]
13958
13959 attribute_prefix ::=
13960   @b{project} |
13961   <project_>simple_name | package_identifier |
13962   <project_>simple_name . package_identifier
13963
13964 case_construction ::=
13965   @b{case} <typed_variable_>name @b{is}
13966     @{case_item@}
13967   @b{end case} ;
13968
13969 case_item ::=
13970   @b{when} discrete_choice_list =>
13971       @{case_construction | attribute_declaration@}
13972
13973 discrete_choice_list ::=
13974   string_literal @{| string_literal@} |
13975   @b{others}
13976
13977 name ::=
13978   simple_name @{. simple_name@}
13979
13980 simple_name ::=
13981   identifier (same as Ada)
13982
13983 @end smallexample
13984
13985 @node The Cross-Referencing Tools gnatxref and gnatfind
13986 @chapter  The Cross-Referencing Tools @code{gnatxref} and @code{gnatfind}
13987 @findex gnatxref
13988 @findex gnatfind
13989
13990 @noindent
13991 The compiler generates cross-referencing information (unless
13992 you set the @samp{-gnatx} switch), which are saved in the @file{.ali} files.
13993 This information indicates where in the source each entity is declared and
13994 referenced. Note that entities in package Standard are not included, but
13995 entities in all other predefined units are included in the output.
13996
13997 Before using any of these two tools, you need to compile successfully your
13998 application, so that GNAT gets a chance to generate the cross-referencing
13999 information.
14000
14001 The two tools @code{gnatxref} and @code{gnatfind} take advantage of this
14002 information to provide the user with the capability to easily locate the
14003 declaration and references to an entity. These tools are quite similar,
14004 the difference being that @code{gnatfind} is intended for locating
14005 definitions and/or references to a specified entity or entities, whereas
14006 @code{gnatxref} is oriented to generating a full report of all
14007 cross-references.
14008
14009 To use these tools, you must not compile your application using the
14010 @option{-gnatx} switch on the @command{gnatmake} command line
14011 (@pxref{The GNAT Make Program gnatmake}). Otherwise, cross-referencing
14012 information will not be generated.
14013
14014 @menu
14015 * gnatxref Switches::
14016 * gnatfind Switches::
14017 * Project Files for gnatxref and gnatfind::
14018 * Regular Expressions in gnatfind and gnatxref::
14019 * Examples of gnatxref Usage::
14020 * Examples of gnatfind Usage::
14021 @end menu
14022
14023 @node gnatxref Switches
14024 @section @code{gnatxref} Switches
14025
14026 @noindent
14027 The command invocation for @code{gnatxref} is:
14028 @smallexample
14029 $ gnatxref [switches] sourcefile1 [sourcefile2 ...]
14030 @end smallexample
14031
14032 @noindent
14033 where
14034
14035 @table @code
14036 @item sourcefile1, sourcefile2
14037 identifies the source files for which a report is to be generated. The
14038 ``with''ed units will be processed too. You must provide at least one file.
14039
14040 These file names are considered to be regular expressions, so for instance
14041 specifying @file{source*.adb} is the same as giving every file in the current
14042 directory whose name starts with @file{source} and whose extension is
14043 @file{adb}.
14044
14045 You shouldn't specify any directory name, just base names. @command{gnatxref}
14046 and @command{gnatfind} will be able to locate these files by themselves using
14047 the source path. If you specify directories, no result is produced.
14048
14049 @end table
14050
14051 @noindent
14052 The switches can be :
14053 @table @option
14054 @c !sort!
14055 @item ^-a^/ALL_FILES^
14056 @cindex @option{^-a^/ALL_FILES^} (@command{gnatxref})
14057 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
14058 the read-only files found in the library search path. Otherwise, these files
14059 will be ignored. This option can be used to protect Gnat sources or your own
14060 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
14061 much faster, and their output much smaller. Read-only here refers to access
14062 or permissions status in the file system for the current user.
14063
14064 @item -aIDIR
14065 @cindex @option{-aIDIR} (@command{gnatxref})
14066 When looking for source files also look in directory DIR. The order in which
14067 source file search is undertaken is the same as for @command{gnatmake}.
14068
14069 @item -aODIR
14070 @cindex @option{-aODIR} (@command{gnatxref})
14071 When searching for library and object files, look in directory
14072 DIR. The order in which library files are searched is the same as for
14073 @command{gnatmake}.
14074
14075 @item -nostdinc
14076 @cindex @option{-nostdinc} (@command{gnatxref})
14077 Do not look for sources in the system default directory.
14078
14079 @item -nostdlib
14080 @cindex @option{-nostdlib} (@command{gnatxref})
14081 Do not look for library files in the system default directory.
14082
14083 @item --RTS=@var{rts-path}
14084 @cindex @option{--RTS} (@command{gnatxref})
14085 Specifies the default location of the runtime library. Same meaning as the
14086 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
14087
14088 @item ^-d^/DERIVED_TYPES^
14089 @cindex @option{^-d^/DERIVED_TYPES^} (@command{gnatxref})
14090 If this switch is set @code{gnatxref} will output the parent type
14091 reference for each matching derived types.
14092
14093 @item ^-f^/FULL_PATHNAME^
14094 @cindex @option{^-f^/FULL_PATHNAME^} (@command{gnatxref})
14095 If this switch is set, the output file names will be preceded by their
14096 directory (if the file was found in the search path). If this switch is
14097 not set, the directory will not be printed.
14098
14099 @item ^-g^/IGNORE_LOCALS^
14100 @cindex @option{^-g^/IGNORE_LOCALS^} (@command{gnatxref})
14101 If this switch is set, information is output only for library-level
14102 entities, ignoring local entities. The use of this switch may accelerate
14103 @code{gnatfind} and @code{gnatxref}.
14104
14105 @item -IDIR
14106 @cindex @option{-IDIR} (@command{gnatxref})
14107 Equivalent to @samp{-aODIR -aIDIR}.
14108
14109 @item -pFILE
14110 @cindex @option{-pFILE} (@command{gnatxref})
14111 Specify a project file to use @xref{Project Files}. These project files are
14112 the @file{.adp} files used by Glide. If you need to use the @file{.gpr}
14113 project files, you should use gnatxref through the GNAT driver
14114 (@command{gnat xref -Pproject}).
14115
14116 By default, @code{gnatxref} and @code{gnatfind} will try to locate a
14117 project file in the current directory.
14118
14119 If a project file is either specified or found by the tools, then the content
14120 of the source directory and object directory lines are added as if they
14121 had been specified respectively by @samp{^-aI^/SOURCE_SEARCH^}
14122 and @samp{^-aO^OBJECT_SEARCH^}.
14123 @item ^-u^/UNUSED^
14124 Output only unused symbols. This may be really useful if you give your
14125 main compilation unit on the command line, as @code{gnatxref} will then
14126 display every unused entity and 'with'ed package.
14127
14128 @ifclear vms
14129 @item -v
14130 Instead of producing the default output, @code{gnatxref} will generate a
14131 @file{tags} file that can be used by vi. For examples how to use this
14132 feature, see @ref{Examples of gnatxref Usage}. The tags file is output
14133 to the standard output, thus you will have to redirect it to a file.
14134 @end ifclear
14135
14136 @end table
14137
14138 @noindent
14139 All these switches may be in any order on the command line, and may even
14140 appear after the file names. They need not be separated by spaces, thus
14141 you can say @samp{gnatxref ^-ag^/ALL_FILES/IGNORE_LOCALS^} instead of
14142 @samp{gnatxref ^-a -g^/ALL_FILES /IGNORE_LOCALS^}.
14143
14144 @node gnatfind Switches
14145 @section @code{gnatfind} Switches
14146
14147 @noindent
14148 The command line for @code{gnatfind} is:
14149
14150 @smallexample
14151 $ gnatfind [switches] pattern[:sourcefile[:line[:column]]]
14152       [file1 file2 ...]
14153 @end smallexample
14154
14155 @noindent
14156 where
14157
14158 @table @code
14159 @item pattern
14160 An entity will be output only if it matches the regular expression found
14161 in @samp{pattern}, see @ref{Regular Expressions in gnatfind and gnatxref}.
14162
14163 Omitting the pattern is equivalent to specifying @samp{*}, which
14164 will match any entity. Note that if you do not provide a pattern, you
14165 have to provide both a sourcefile and a line.
14166
14167 Entity names are given in Latin-1, with uppercase/lowercase equivalence
14168 for matching purposes. At the current time there is no support for
14169 8-bit codes other than Latin-1, or for wide characters in identifiers.
14170
14171 @item sourcefile
14172 @code{gnatfind} will look for references, bodies or declarations
14173 of symbols referenced in @file{sourcefile}, at line @samp{line}
14174 and column @samp{column}. See @ref{Examples of gnatfind Usage}
14175 for syntax examples.
14176
14177 @item line
14178 is a decimal integer identifying the line number containing
14179 the reference to the entity (or entities) to be located.
14180
14181 @item column
14182 is a decimal integer identifying the exact location on the
14183 line of the first character of the identifier for the
14184 entity reference. Columns are numbered from 1.
14185
14186 @item file1 file2 ...
14187 The search will be restricted to these source files. If none are given, then
14188 the search will be done for every library file in the search path.
14189 These file must appear only after the pattern or sourcefile.
14190
14191 These file names are considered to be regular expressions, so for instance
14192 specifying 'source*.adb' is the same as giving every file in the current
14193 directory whose name starts with 'source' and whose extension is 'adb'.
14194
14195 The location of the spec of the entity will always be displayed, even if it
14196 isn't in one of file1, file2,... The occurrences of the entity in the
14197 separate units of the ones given on the command line will also be displayed.
14198
14199 Note that if you specify at least one file in this part, @code{gnatfind} may
14200 sometimes not be able to find the body of the subprograms...
14201
14202 @end table
14203
14204 @noindent
14205 At least one of 'sourcefile' or 'pattern' has to be present on
14206 the command line.
14207
14208 The following switches are available:
14209 @table @option
14210 @c !sort!
14211
14212 @item ^-a^/ALL_FILES^
14213 @cindex @option{^-a^/ALL_FILES^} (@command{gnatfind})
14214 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
14215 the read-only files found in the library search path. Otherwise, these files
14216 will be ignored. This option can be used to protect Gnat sources or your own
14217 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
14218 much faster, and their output much smaller. Read-only here refers to access
14219 or permission status in the file system for the current user.
14220
14221 @item -aIDIR
14222 @cindex @option{-aIDIR} (@command{gnatfind})
14223 When looking for source files also look in directory DIR. The order in which
14224 source file search is undertaken is the same as for @command{gnatmake}.
14225
14226 @item -aODIR
14227 @cindex @option{-aODIR} (@command{gnatfind})
14228 When searching for library and object files, look in directory
14229 DIR. The order in which library files are searched is the same as for
14230 @command{gnatmake}.
14231
14232 @item -nostdinc
14233 @cindex @option{-nostdinc} (@command{gnatfind})
14234 Do not look for sources in the system default directory.
14235
14236 @item -nostdlib
14237 @cindex @option{-nostdlib} (@command{gnatfind})
14238 Do not look for library files in the system default directory.
14239
14240 @item --RTS=@var{rts-path}
14241 @cindex @option{--RTS} (@command{gnatfind})
14242 Specifies the default location of the runtime library. Same meaning as the
14243 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
14244
14245 @item ^-d^/DERIVED_TYPE_INFORMATION^
14246 @cindex @option{^-d^/DERIVED_TYPE_INFORMATION^} (@code{gnatfind})
14247 If this switch is set, then @code{gnatfind} will output the parent type
14248 reference for each matching derived types.
14249
14250 @item ^-e^/EXPRESSIONS^
14251 @cindex @option{^-e^/EXPRESSIONS^} (@command{gnatfind})
14252 By default, @code{gnatfind} accept the simple regular expression set for
14253 @samp{pattern}. If this switch is set, then the pattern will be
14254 considered as full Unix-style regular expression.
14255
14256 @item ^-f^/FULL_PATHNAME^
14257 @cindex @option{^-f^/FULL_PATHNAME^} (@command{gnatfind})
14258 If this switch is set, the output file names will be preceded by their
14259 directory (if the file was found in the search path). If this switch is
14260 not set, the directory will not be printed.
14261
14262 @item ^-g^/IGNORE_LOCALS^
14263 @cindex @option{^-g^/IGNORE_LOCALS^} (@command{gnatfind})
14264 If this switch is set, information is output only for library-level
14265 entities, ignoring local entities. The use of this switch may accelerate
14266 @code{gnatfind} and @code{gnatxref}.
14267
14268 @item -IDIR
14269 @cindex @option{-IDIR} (@command{gnatfind})
14270 Equivalent to @samp{-aODIR -aIDIR}.
14271
14272 @item -pFILE
14273 @cindex @option{-pFILE} (@command{gnatfind})
14274 Specify a project file (@pxref{Project Files}) to use.
14275 By default, @code{gnatxref} and @code{gnatfind} will try to locate a
14276 project file in the current directory.
14277
14278 If a project file is either specified or found by the tools, then the content
14279 of the source directory and object directory lines are added as if they
14280 had been specified respectively by @samp{^-aI^/SOURCE_SEARCH^} and
14281 @samp{^-aO^/OBJECT_SEARCH^}.
14282
14283 @item ^-r^/REFERENCES^
14284 @cindex @option{^-r^/REFERENCES^} (@command{gnatfind})
14285 By default, @code{gnatfind} will output only the information about the
14286 declaration, body or type completion of the entities. If this switch is
14287 set, the @code{gnatfind} will locate every reference to the entities in
14288 the files specified on the command line (or in every file in the search
14289 path if no file is given on the command line).
14290
14291 @item ^-s^/PRINT_LINES^
14292 @cindex @option{^-s^/PRINT_LINES^} (@command{gnatfind})
14293 If this switch is set, then @code{gnatfind} will output the content
14294 of the Ada source file lines were the entity was found.
14295
14296 @item ^-t^/TYPE_HIERARCHY^
14297 @cindex @option{^-t^/TYPE_HIERARCHY^} (@command{gnatfind})
14298 If this switch is set, then @code{gnatfind} will output the type hierarchy for
14299 the specified type. It act like -d option but recursively from parent
14300 type to parent type. When this switch is set it is not possible to
14301 specify more than one file.
14302
14303 @end table
14304
14305 @noindent
14306 All these switches may be in any order on the command line, and may even
14307 appear after the file names. They need not be separated by spaces, thus
14308 you can say @samp{gnatxref ^-ag^/ALL_FILES/IGNORE_LOCALS^} instead of
14309 @samp{gnatxref ^-a -g^/ALL_FILES /IGNORE_LOCALS^}.
14310
14311 As stated previously, gnatfind will search in every directory in the
14312 search path. You can force it to look only in the current directory if
14313 you specify @code{*} at the end of the command line.
14314
14315 @node Project Files for gnatxref and gnatfind
14316 @section Project Files for @command{gnatxref} and @command{gnatfind}
14317
14318 @noindent
14319 Project files allow a programmer to specify how to compile its
14320 application, where to find sources, etc.  These files are used
14321 @ifclear vms
14322 primarily by the Glide Ada mode, but they can also be used
14323 @end ifclear
14324 by the two tools
14325 @code{gnatxref} and @code{gnatfind}.
14326
14327 A project file name must end with @file{.gpr}. If a single one is
14328 present in the current directory, then @code{gnatxref} and @code{gnatfind} will
14329 extract the information from it. If multiple project files are found, none of
14330 them is read, and you have to use the @samp{-p} switch to specify the one
14331 you want to use.
14332
14333 The following lines can be included, even though most of them have default
14334 values which can be used in most cases.
14335 The lines can be entered in any order in the file.
14336 Except for @file{src_dir} and @file{obj_dir}, you can only have one instance of
14337 each line. If you have multiple instances, only the last one is taken into
14338 account.
14339
14340 @table @code
14341 @item src_dir=DIR
14342 [default: @code{"^./^[]^"}]
14343 specifies a directory where to look for source files. Multiple @code{src_dir}
14344 lines can be specified and they will be searched in the order they
14345 are specified.
14346
14347 @item obj_dir=DIR
14348 [default: @code{"^./^[]^"}]
14349 specifies a directory where to look for object and library files. Multiple
14350 @code{obj_dir} lines can be specified, and they will be searched in the order
14351 they are specified
14352
14353 @item comp_opt=SWITCHES
14354 [default: @code{""}]
14355 creates a variable which can be referred to subsequently by using
14356 the @code{$@{comp_opt@}} notation. This is intended to store the default
14357 switches given to @command{gnatmake} and @command{gcc}.
14358
14359 @item bind_opt=SWITCHES
14360 [default: @code{""}]
14361 creates a variable which can be referred to subsequently by using
14362 the @samp{$@{bind_opt@}} notation. This is intended to store the default
14363 switches given to @command{gnatbind}.
14364
14365 @item link_opt=SWITCHES
14366 [default: @code{""}]
14367 creates a variable which can be referred to subsequently by using
14368 the @samp{$@{link_opt@}} notation. This is intended to store the default
14369 switches given to @command{gnatlink}.
14370
14371 @item main=EXECUTABLE
14372 [default: @code{""}]
14373 specifies the name of the executable for the application. This variable can
14374 be referred to in the following lines by using the @samp{$@{main@}} notation.
14375
14376 @ifset vms
14377 @item comp_cmd=COMMAND
14378 [default: @code{"GNAT COMPILE /SEARCH=$@{src_dir@} /DEBUG /TRY_SEMANTICS"}]
14379 @end ifset
14380 @ifclear vms
14381 @item comp_cmd=COMMAND
14382 [default: @code{"gcc -c -I$@{src_dir@} -g -gnatq"}]
14383 @end ifclear
14384 specifies the command used to compile a single file in the application.
14385
14386 @ifset vms
14387 @item make_cmd=COMMAND
14388 [default: @code{"GNAT MAKE $@{main@}
14389 /SOURCE_SEARCH=$@{src_dir@} /OBJECT_SEARCH=$@{obj_dir@}
14390 /DEBUG /TRY_SEMANTICS /COMPILER_QUALIFIERS $@{comp_opt@}
14391 /BINDER_QUALIFIERS $@{bind_opt@} /LINKER_QUALIFIERS $@{link_opt@}"}]
14392 @end ifset
14393 @ifclear vms
14394 @item make_cmd=COMMAND
14395 [default: @code{"gnatmake $@{main@} -aI$@{src_dir@}
14396  -aO$@{obj_dir@} -g -gnatq -cargs $@{comp_opt@}
14397  -bargs $@{bind_opt@} -largs $@{link_opt@}"}]
14398 @end ifclear
14399 specifies the command used to recompile the whole application.
14400
14401 @item run_cmd=COMMAND
14402 [default: @code{"$@{main@}"}]
14403 specifies the command used to run the application.
14404
14405 @item debug_cmd=COMMAND
14406 [default: @code{"gdb $@{main@}"}]
14407 specifies the command used to debug the application
14408
14409 @end table
14410
14411 @noindent
14412 @command{gnatxref} and @command{gnatfind} only take into account the
14413 @code{src_dir} and @code{obj_dir} lines, and ignore the others.
14414
14415 @node Regular Expressions in gnatfind and gnatxref
14416 @section  Regular Expressions in @code{gnatfind} and @code{gnatxref}
14417
14418 @noindent
14419 As specified in the section about @command{gnatfind}, the pattern can be a
14420 regular expression. Actually, there are to set of regular expressions
14421 which are recognized by the program :
14422
14423 @table @code
14424 @item globbing patterns
14425 These are the most usual regular expression. They are the same that you
14426 generally used in a Unix shell command line, or in a DOS session.
14427
14428 Here is a more formal grammar :
14429 @smallexample
14430 @group
14431 @iftex
14432 @leftskip=.5cm
14433 @end iftex
14434 regexp ::= term
14435 term   ::= elmt            -- matches elmt
14436 term   ::= elmt elmt       -- concatenation (elmt then elmt)
14437 term   ::= *               -- any string of 0 or more characters
14438 term   ::= ?               -- matches any character
14439 term   ::= [char @{char@}] -- matches any character listed
14440 term   ::= [char - char]   -- matches any character in range
14441 @end group
14442 @end smallexample
14443
14444 @item full regular expression
14445 The second set of regular expressions is much more powerful. This is the
14446 type of regular expressions recognized by utilities such a @file{grep}.
14447
14448 The following is the form of a regular expression, expressed in Ada
14449 reference manual style BNF is as follows
14450
14451 @smallexample
14452 @iftex
14453 @leftskip=.5cm
14454 @end iftex
14455 @group
14456 regexp ::= term @{| term@} -- alternation (term or term ...)
14457
14458 term ::= item @{item@}     -- concatenation (item then item)
14459
14460 item ::= elmt              -- match elmt
14461 item ::= elmt *            -- zero or more elmt's
14462 item ::= elmt +            -- one or more elmt's
14463 item ::= elmt ?            -- matches elmt or nothing
14464 @end group
14465 @group
14466 elmt ::= nschar            -- matches given character
14467 elmt ::= [nschar @{nschar@}]   -- matches any character listed
14468 elmt ::= [^^^ nschar @{nschar@}] -- matches any character not listed
14469 elmt ::= [char - char]     -- matches chars in given range
14470 elmt ::= \ char            -- matches given character
14471 elmt ::= .                 -- matches any single character
14472 elmt ::= ( regexp )        -- parens used for grouping
14473
14474 char ::= any character, including special characters
14475 nschar ::= any character except ()[].*+?^^^
14476 @end group
14477 @end smallexample
14478
14479 Following are a few examples :
14480
14481 @table @samp
14482 @item abcde|fghi
14483 will match any of the two strings 'abcde' and 'fghi'.
14484
14485 @item abc*d
14486 will match any string like 'abd', 'abcd', 'abccd', 'abcccd', and so on
14487
14488 @item [a-z]+
14489 will match any string which has only lowercase characters in it (and at
14490 least one character
14491
14492 @end table
14493 @end table
14494
14495 @node Examples of gnatxref Usage
14496 @section Examples of @code{gnatxref} Usage
14497
14498 @subsection General Usage
14499
14500 @noindent
14501 For the following examples, we will consider the following units :
14502
14503 @smallexample @c ada
14504 @group
14505 @cartouche
14506 main.ads:
14507 1: with Bar;
14508 2: package Main is
14509 3:     procedure Foo (B : in Integer);
14510 4:     C : Integer;
14511 5: private
14512 6:     D : Integer;
14513 7: end Main;
14514
14515 main.adb:
14516 1: package body Main is
14517 2:     procedure Foo (B : in Integer) is
14518 3:     begin
14519 4:        C := B;
14520 5:        D := B;
14521 6:        Bar.Print (B);
14522 7:        Bar.Print (C);
14523 8:     end Foo;
14524 9: end Main;
14525
14526 bar.ads:
14527 1: package Bar is
14528 2:     procedure Print (B : Integer);
14529 3: end bar;
14530 @end cartouche
14531 @end group
14532 @end smallexample
14533
14534 @table @code
14535
14536 @noindent
14537 The first thing to do is to recompile your application (for instance, in
14538 that case just by doing a @samp{gnatmake main}, so that GNAT generates
14539 the cross-referencing information.
14540 You can then issue any of the following commands:
14541
14542 @item gnatxref main.adb
14543 @code{gnatxref} generates cross-reference information for main.adb
14544 and every unit 'with'ed by main.adb.
14545
14546 The output would be:
14547 @smallexample
14548 @iftex
14549 @leftskip=0cm
14550 @end iftex
14551 B                                                      Type: Integer
14552   Decl: bar.ads           2:22
14553 B                                                      Type: Integer
14554   Decl: main.ads          3:20
14555   Body: main.adb          2:20
14556   Ref:  main.adb          4:13     5:13     6:19
14557 Bar                                                    Type: Unit
14558   Decl: bar.ads           1:9
14559   Ref:  main.adb          6:8      7:8
14560        main.ads           1:6
14561 C                                                      Type: Integer
14562   Decl: main.ads          4:5
14563   Modi: main.adb          4:8
14564   Ref:  main.adb          7:19
14565 D                                                      Type: Integer
14566   Decl: main.ads          6:5
14567   Modi: main.adb          5:8
14568 Foo                                                    Type: Unit
14569   Decl: main.ads          3:15
14570   Body: main.adb          2:15
14571 Main                                                    Type: Unit
14572   Decl: main.ads          2:9
14573   Body: main.adb          1:14
14574 Print                                                   Type: Unit
14575   Decl: bar.ads           2:15
14576   Ref:  main.adb          6:12     7:12
14577 @end smallexample
14578
14579 @noindent
14580 that is the entity @code{Main} is declared in main.ads, line 2, column 9,
14581 its body is in main.adb, line 1, column 14 and is not referenced any where.
14582
14583 The entity @code{Print} is declared in bar.ads, line 2, column 15 and it
14584 it referenced in main.adb, line 6 column 12 and line 7 column 12.
14585
14586 @item gnatxref package1.adb package2.ads
14587 @code{gnatxref} will generates cross-reference information for
14588 package1.adb, package2.ads and any other package 'with'ed by any
14589 of these.
14590
14591 @end table
14592
14593 @ifclear vms
14594 @subsection Using gnatxref with vi
14595
14596 @code{gnatxref} can generate a tags file output, which can be used
14597 directly from @file{vi}. Note that the standard version of @file{vi}
14598 will not work properly with overloaded symbols. Consider using another
14599 free implementation of @file{vi}, such as @file{vim}.
14600
14601 @smallexample
14602 $ gnatxref -v gnatfind.adb > tags
14603 @end smallexample
14604
14605 @noindent
14606 will generate the tags file for @code{gnatfind} itself (if the sources
14607 are in the search path!).
14608
14609 From @file{vi}, you can then use the command @samp{:tag @i{entity}}
14610 (replacing @i{entity} by whatever you are looking for), and vi will
14611 display a new file with the corresponding declaration of entity.
14612 @end ifclear
14613
14614 @node Examples of gnatfind Usage
14615 @section Examples of @code{gnatfind} Usage
14616
14617 @table @code
14618
14619 @item gnatfind ^-f^/FULL_PATHNAME^ xyz:main.adb
14620 Find declarations for all entities xyz referenced at least once in
14621 main.adb. The references are search in every library file in the search
14622 path.
14623
14624 The directories will be printed as well (as the @samp{^-f^/FULL_PATHNAME^}
14625 switch is set)
14626
14627 The output will look like:
14628 @smallexample
14629 ^directory/^[directory]^main.ads:106:14: xyz <= declaration
14630 ^directory/^[directory]^main.adb:24:10: xyz <= body
14631 ^directory/^[directory]^foo.ads:45:23: xyz <= declaration
14632 @end smallexample
14633
14634 @noindent
14635 that is to say, one of the entities xyz found in main.adb is declared at
14636 line 12 of main.ads (and its body is in main.adb), and another one is
14637 declared at line 45 of foo.ads
14638
14639 @item gnatfind ^-fs^/FULL_PATHNAME/SOURCE_LINE^ xyz:main.adb
14640 This is the same command as the previous one, instead @code{gnatfind} will
14641 display the content of the Ada source file lines.
14642
14643 The output will look like:
14644
14645 @smallexample
14646 ^directory/^[directory]^main.ads:106:14: xyz <= declaration
14647    procedure xyz;
14648 ^directory/^[directory]^main.adb:24:10: xyz <= body
14649    procedure xyz is
14650 ^directory/^[directory]^foo.ads:45:23: xyz <= declaration
14651    xyz : Integer;
14652 @end smallexample
14653
14654 @noindent
14655 This can make it easier to find exactly the location your are looking
14656 for.
14657
14658 @item gnatfind ^-r^/REFERENCES^ "*x*":main.ads:123 foo.adb
14659 Find references to all entities containing an x that are
14660 referenced on line 123 of main.ads.
14661 The references will be searched only in main.ads and foo.adb.
14662
14663 @item gnatfind main.ads:123
14664 Find declarations and bodies for all entities that are referenced on
14665 line 123 of main.ads.
14666
14667 This is the same as @code{gnatfind "*":main.adb:123}.
14668
14669 @item gnatfind ^mydir/^[mydir]^main.adb:123:45
14670 Find the declaration for the entity referenced at column 45 in
14671 line 123 of file main.adb in directory mydir. Note that it
14672 is usual to omit the identifier name when the column is given,
14673 since the column position identifies a unique reference.
14674
14675 The column has to be the beginning of the identifier, and should not
14676 point to any character in the middle of the identifier.
14677
14678 @end table
14679
14680 @c *********************************
14681 @node The GNAT Pretty-Printer gnatpp
14682 @chapter The GNAT Pretty-Printer @command{gnatpp}
14683 @findex gnatpp
14684 @cindex Pretty-Printer
14685
14686 @noindent
14687 ^The @command{gnatpp} tool^GNAT PRETTY^ is an ASIS-based utility
14688 for source reformatting / pretty-printing.
14689 It takes an Ada source file as input and generates a reformatted
14690 version as output.
14691 You can specify various style directives via switches; e.g.,
14692 identifier case conventions, rules of indentation, and comment layout.
14693
14694 To produce a reformatted file, @command{gnatpp} generates and uses the ASIS
14695 tree for the input source and thus requires the input to be syntactically and
14696 semantically legal.
14697 If this condition is not met, @command{gnatpp} will terminate with an
14698 error message; no output file will be generated.
14699
14700 If the compilation unit
14701 contained in the input source depends semantically upon units located
14702 outside the current directory, you have to provide the source search path
14703 when invoking @command{gnatpp}, if these units are contained in files with
14704 names that do not follow the GNAT file naming rules, you have to provide
14705 the configuration file describing the corresponding naming scheme;
14706 see the description of the @command{gnatpp}
14707 switches below. Another possibility is to use a project file and to
14708 call @command{gnatpp} through the @command{gnat} driver
14709
14710 The @command{gnatpp} command has the form
14711
14712 @smallexample
14713 $ gnatpp [@var{switches}] @var{filename}
14714 @end smallexample
14715
14716 @noindent
14717 where
14718 @itemize @bullet
14719 @item
14720 @var{switches} is an optional sequence of switches defining such properties as
14721 the formatting rules, the source search path, and the destination for the
14722 output source file
14723
14724 @item
14725 @var{filename} is the name (including the extension) of the source file to
14726 reformat; ``wildcards'' or several file names on the same gnatpp command are
14727 allowed.  The file name may contain path information; it does not have to
14728 follow the GNAT file naming rules
14729 @end itemize
14730
14731 @menu
14732 * Switches for gnatpp::
14733 * Formatting Rules::
14734 @end menu
14735
14736 @node Switches for gnatpp
14737 @section Switches for @command{gnatpp}
14738
14739 @noindent
14740 The following subsections describe the various switches accepted by
14741 @command{gnatpp}, organized by category.
14742
14743 @ifclear vms
14744 You specify a switch by supplying a name and generally also a value.
14745 In many cases the values for a switch with a given name are incompatible with
14746 each other
14747 (for example the switch that controls the casing of a reserved word may have
14748 exactly one value: upper case, lower case, or
14749 mixed case) and thus exactly one such switch can be in effect for an
14750 invocation of @command{gnatpp}.
14751 If more than one is supplied, the last one is used.
14752 However, some values for the same switch are mutually compatible.
14753 You may supply several such switches to @command{gnatpp}, but then
14754 each must be specified in full, with both the name and the value.
14755 Abbreviated forms (the name appearing once, followed by each value) are
14756 not permitted.
14757 For example, to set
14758 the alignment of the assignment delimiter both in declarations and in
14759 assignment statements, you must write @option{-A2A3}
14760 (or @option{-A2 -A3}), but not @option{-A23}.
14761 @end ifclear
14762
14763 @ifset vms
14764 In many cases the set of options for a given qualifier are incompatible with
14765 each other (for example the qualifier that controls the casing of a reserved
14766 word may have exactly one option, which specifies either upper case, lower
14767 case, or mixed case), and thus exactly one such option can be in effect for
14768 an invocation of @command{gnatpp}.
14769 If more than one is supplied, the last one is used.
14770 However, some qualifiers have options that are mutually compatible,
14771 and then you may then supply several such options when invoking
14772 @command{gnatpp}.
14773 @end ifset
14774
14775 In most cases, it is obvious whether or not the
14776 ^values for a switch with a given name^options for a given qualifier^
14777 are compatible with each other.
14778 When the semantics might not be evident, the summaries below explicitly
14779 indicate the effect.
14780
14781 @menu
14782 * Alignment Control::
14783 * Casing Control::
14784 * Construct Layout Control::
14785 * General Text Layout Control::
14786 * Other Formatting Options::
14787 * Setting the Source Search Path::
14788 * Output File Control::
14789 * Other gnatpp Switches::
14790 @end menu
14791
14792 @node Alignment Control
14793 @subsection Alignment Control
14794 @cindex Alignment control in @command{gnatpp}
14795
14796 @noindent
14797 Programs can be easier to read if certain constructs are vertically aligned.
14798 By default all alignments are set ON.
14799 Through the @option{^-A0^/ALIGN=OFF^} switch you may reset the default to
14800 OFF, and then use one or more of the other
14801 ^@option{-A@var{n}} switches^@option{/ALIGN} options^
14802 to activate alignment for specific constructs.
14803
14804 @table @option
14805 @cindex @option{^-A@var{n}^/ALIGN^} (@command{gnatpp})
14806
14807 @ifset vms
14808 @item /ALIGN=ON
14809 Set all alignments to ON
14810 @end ifset
14811
14812 @item ^-A0^/ALIGN=OFF^
14813 Set all alignments to OFF
14814
14815 @item ^-A1^/ALIGN=COLONS^
14816 Align @code{:} in declarations
14817
14818 @item ^-A2^/ALIGN=DECLARATIONS^
14819 Align @code{:=} in initializations in declarations
14820
14821 @item ^-A3^/ALIGN=STATEMENTS^
14822 Align @code{:=} in assignment statements
14823
14824 @item ^-A4^/ALIGN=ARROWS^
14825 Align @code{=>} in associations
14826
14827 @item ^-A5^/ALIGN=COMPONENT_CLAUSES^
14828 Align @code{at} keywords in the component clauses in record
14829 representation clauses
14830 @end table
14831
14832 @noindent
14833 The @option{^-A^/ALIGN^} switches are mutually compatible; any combination
14834 is allowed.
14835
14836 @node Casing Control
14837 @subsection Casing Control
14838 @cindex Casing control in @command{gnatpp}
14839
14840 @noindent
14841 @command{gnatpp} allows you to specify the casing for reserved words,
14842 pragma names, attribute designators and identifiers.
14843 For identifiers you may define a
14844 general rule for name casing but also override this rule
14845 via a set of dictionary files.
14846
14847 Three types of casing are supported: lower case, upper case, and mixed case.
14848 Lower and upper case are self-explanatory (but since some letters in
14849 Latin1 and other GNAT-supported character sets
14850 exist only in lower-case form, an upper case conversion will have no
14851 effect on them.)
14852 ``Mixed case'' means that the first letter, and also each letter immediately
14853 following an underscore, are converted to their uppercase forms;
14854 all the other letters are converted to their lowercase forms.
14855
14856 @table @option
14857 @cindex @option{^-a@var{x}^/ATTRIBUTE^} (@command{gnatpp})
14858 @item ^-aL^/ATTRIBUTE_CASING=LOWER_CASE^
14859 Attribute designators are lower case
14860
14861 @item ^-aU^/ATTRIBUTE_CASING=UPPER_CASE^
14862 Attribute designators are upper case
14863
14864 @item ^-aM^/ATTRIBUTE_CASING=MIXED_CASE^
14865 Attribute designators are mixed case (this is the default)
14866
14867 @cindex @option{^-k@var{x}^/KEYWORD_CASING^} (@command{gnatpp})
14868 @item ^-kL^/KEYWORD_CASING=LOWER_CASE^
14869 Keywords (technically, these are known in Ada as @emph{reserved words}) are
14870 lower case (this is the default)
14871
14872 @item ^-kU^/KEYWORD_CASING=UPPER_CASE^
14873 Keywords are upper case
14874
14875 @cindex @option{^-n@var{x}^/NAME_CASING^} (@command{gnatpp})
14876 @item ^-nD^/NAME_CASING=AS_DECLARED^
14877 Name casing for defining occurrences are as they appear in the source file
14878 (this is the default)
14879
14880 @item ^-nU^/NAME_CASING=UPPER_CASE^
14881 Names are in upper case
14882
14883 @item ^-nL^/NAME_CASING=LOWER_CASE^
14884 Names are in lower case
14885
14886 @item ^-nM^/NAME_CASING=MIXED_CASE^
14887 Names are in mixed case
14888
14889 @cindex @option{^-p@var{x}^/PRAGMA_CASING^} (@command{gnatpp})
14890 @item ^-pL^/PRAGMA_CASING=LOWER_CASE^
14891 Pragma names are lower case
14892
14893 @item ^-pU^/PRAGMA_CASING=UPPER_CASE^
14894 Pragma names are upper case
14895
14896 @item ^-pM^/PRAGMA_CASING=MIXED_CASE^
14897 Pragma names are mixed case (this is the default)
14898
14899 @item ^-D@var{file}^/DICTIONARY=@var{file}^
14900 @cindex @option{^-D^/DICTIONARY^} (@command{gnatpp})
14901 Use @var{file} as a @emph{dictionary file} that defines
14902 the casing for a set of specified names,
14903 thereby overriding the effect on these names by
14904 any explicit or implicit
14905 ^-n^/NAME_CASING^ switch.
14906 To supply more than one dictionary file,
14907 use ^several @option{-D} switches^a list of files as options^.
14908
14909 @noindent
14910 @option{gnatpp} implicitly uses a @emph{default dictionary file}
14911 to define the casing for the Ada predefined names and
14912 the names declared in the GNAT libraries.
14913
14914 @item ^-D-^/SPECIFIC_CASING^
14915 @cindex @option{^-D-^/SPECIFIC_CASING^} (@command{gnatpp})
14916 Do not use the default dictionary file;
14917 instead, use the casing
14918 defined by a @option{^-n^/NAME_CASING^} switch and any explicit
14919 dictionary file(s)
14920 @end table
14921
14922 @noindent
14923 The structure of a dictionary file, and details on the conventions
14924 used in the default dictionary file, are defined in @ref{Name Casing}.
14925
14926 The @option{^-D-^/SPECIFIC_CASING^} and
14927 @option{^-D@var{file}^/DICTIONARY=@var{file}^} switches are mutually
14928 compatible.
14929
14930 @node Construct Layout Control
14931 @subsection Construct Layout Control
14932 @cindex Layout control in @command{gnatpp}
14933
14934 @noindent
14935 This group of @command{gnatpp} switches controls the layout of comments and
14936 complex syntactic constructs.  See @ref{Formatting Comments} for details
14937 on their effect.
14938
14939 @table @option
14940 @cindex @option{^-c@var{n}^/COMMENTS_LAYOUT^} (@command{gnatpp})
14941 @item ^-c0^/COMMENTS_LAYOUT=UNTOUCHED^
14942 All the comments remain unchanged
14943
14944 @item ^-c1^/COMMENTS_LAYOUT=DEFAULT^
14945 GNAT-style comment line indentation (this is the default).
14946
14947 @item ^-c2^/COMMENTS_LAYOUT=STANDARD_INDENT^
14948 Reference-manual comment line indentation.
14949
14950 @item ^-c3^/COMMENTS_LAYOUT=GNAT_BEGINNING^
14951 GNAT-style comment beginning
14952
14953 @item ^-c4^/COMMENTS_LAYOUT=REFORMAT^
14954 Reformat comment blocks
14955
14956 @cindex @option{^-l@var{n}^/CONSTRUCT_LAYOUT^} (@command{gnatpp})
14957 @item ^-l1^/CONSTRUCT_LAYOUT=GNAT^
14958 GNAT-style layout (this is the default)
14959
14960 @item ^-l2^/CONSTRUCT_LAYOUT=COMPACT^
14961 Compact layout
14962
14963 @item ^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^
14964 Uncompact layout
14965
14966 @cindex @option{^-N^/NOTABS^} (@command{gnatpp})
14967 @item ^-N^/NOTABS^
14968 All the VT characters are removed from the comment text. All the HT characters
14969 are expanded with the sequences of space characters to get to the next tab
14970 stops.
14971
14972 @cindex @option{^--no-separate-is^/NO_SEPARATE_IS^} (@command{gnatpp})
14973 @item ^--no-separate-is^/NO_SEPARATE_IS^
14974 Do not place the keyword @code{is} on a separate line in a subprogram body in
14975 case if the specification occupies more then one line.
14976
14977 @end table
14978
14979 @ifclear vms
14980 @noindent
14981 The @option{-c1} and @option{-c2} switches are incompatible.
14982 The @option{-c3} and @option{-c4} switches are compatible with each other and
14983 also with @option{-c1} and @option{-c2}. The @option{-c0} switch disables all
14984 the other comment formatting switches.
14985
14986 The @option{-l1}, @option{-l2}, and @option{-l3} switches are incompatible.
14987 @end ifclear
14988
14989 @ifset vms
14990 @noindent
14991 For the @option{/COMMENTS_LAYOUT} qualifier:
14992 @itemize @bullet
14993 @item
14994 The @option{DEFAULT} and @option{STANDARD_INDENT} options are incompatible.
14995 @item
14996 The @option{GNAT_BEGINNING} and @option{REFORMAT} options are compatible with
14997 each other and also with @option{DEFAULT} and @option{STANDARD_INDENT}.
14998 @end itemize
14999
15000 @noindent
15001 The @option{GNAT}, @option{COMPACT}, and @option{UNCOMPACT} options for the
15002 @option{/CONSTRUCT_LAYOUT} qualifier are incompatible.
15003 @end ifset
15004
15005 @node General Text Layout Control
15006 @subsection General Text Layout Control
15007
15008 @noindent
15009 These switches allow control over line length and indentation.
15010
15011 @table @option
15012 @item ^-M@i{nnn}^/LINE_LENGTH_MAX=@i{nnn}^
15013 @cindex @option{^-M^/LINE_LENGTH^} (@command{gnatpp})
15014 Maximum line length, @i{nnn} from 32 ..256, the default value is 79
15015
15016 @item ^-i@i{nnn}^/INDENTATION_LEVEL=@i{nnn}^
15017 @cindex @option{^-i^/INDENTATION_LEVEL^} (@command{gnatpp})
15018 Indentation level, @i{nnn} from 1 .. 9, the default value is 3
15019
15020 @item ^-cl@i{nnn}^/CONTINUATION_INDENT=@i{nnn}^
15021 @cindex @option{^-cl^/CONTINUATION_INDENT^} (@command{gnatpp})
15022 Indentation level for continuation lines (relative to the line being
15023 continued), @i{nnn} from 1 .. 9.
15024 The default
15025 value is one less then the (normal) indentation level, unless the
15026 indentation is set to 1 (in which case the default value for continuation
15027 line indentation is also 1)
15028 @end table
15029
15030 @node Other Formatting Options
15031 @subsection Other Formatting Options
15032
15033 @noindent
15034 These switches control the inclusion of missing end/exit labels, and
15035 the indentation level in @b{case} statements.
15036
15037 @table @option
15038 @item ^-e^/NO_MISSED_LABELS^
15039 @cindex @option{^-e^/NO_MISSED_LABELS^} (@command{gnatpp})
15040 Do not insert missing end/exit labels. An end label is the name of
15041 a construct that may optionally be repeated at the end of the
15042 construct's declaration;
15043 e.g., the names of packages, subprograms, and tasks.
15044 An exit label is the name of a loop that may appear as target
15045 of an exit statement within the loop.
15046 By default, @command{gnatpp} inserts these end/exit labels when
15047 they are absent from the original source. This option suppresses such
15048 insertion, so that the formatted source reflects the original.
15049
15050 @item ^-ff^/FORM_FEED_AFTER_PRAGMA_PAGE^
15051 @cindex @option{^-ff^/FORM_FEED_AFTER_PRAGMA_PAGE^} (@command{gnatpp})
15052 Insert a Form Feed character after a pragma Page.
15053
15054 @item ^-T@i{nnn}^/MAX_INDENT=@i{nnn}^
15055 @cindex @option{^-T^/MAX_INDENT^} (@command{gnatpp})
15056 Do not use an additional indentation level for @b{case} alternatives
15057 and variants if there are @i{nnn} or more (the default
15058 value is 10).
15059 If @i{nnn} is 0, an additional indentation level is
15060 used for @b{case} alternatives and variants regardless of their number.
15061 @end table
15062
15063 @node Setting the Source Search Path
15064 @subsection Setting the Source Search Path
15065
15066 @noindent
15067 To define the search path for the input source file, @command{gnatpp}
15068 uses the same switches as the GNAT compiler, with the same effects.
15069
15070 @table @option
15071 @item ^-I^/SEARCH=^@var{dir}
15072 @cindex @option{^-I^/SEARCH^} (@code{gnatpp})
15073 The same as the corresponding gcc switch
15074
15075 @item ^-I-^/NOCURRENT_DIRECTORY^
15076 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatpp})
15077 The same as the corresponding gcc switch
15078
15079 @item ^-gnatec^/CONFIGURATION_PRAGMAS_FILE^=@var{path}
15080 @cindex @option{^-gnatec^/CONFIGURATION_PRAGMAS_FILE^} (@code{gnatpp})
15081 The same as the corresponding gcc switch
15082
15083 @item ^--RTS^/RUNTIME_SYSTEM^=@var{path}
15084 @cindex @option{^--RTS^/RUNTIME_SYSTEM^} (@code{gnatpp})
15085 The same as the corresponding gcc switch
15086
15087 @end table
15088
15089 @node Output File Control
15090 @subsection Output File Control
15091
15092 @noindent
15093 By default the output is sent to the file whose name is obtained by appending
15094 the ^@file{.pp}^@file{$PP}^ suffix to the name of the input file
15095 (if the file with this name already exists, it is unconditionally overwritten).
15096 Thus if the input file is @file{^my_ada_proc.adb^MY_ADA_PROC.ADB^} then
15097 @command{gnatpp} will produce @file{^my_ada_proc.adb.pp^MY_ADA_PROC.ADB$PP^}
15098 as output file.
15099 The output may be redirected by the following switches:
15100
15101 @table @option
15102 @item ^-pipe^/STANDARD_OUTPUT^
15103 @cindex @option{^-pipe^/STANDARD_OUTPUT^} (@code{gnatpp})
15104 Send the output to @code{Standard_Output}
15105
15106 @item ^-o @var{output_file}^/OUTPUT=@var{output_file}^
15107 @cindex @option{^-o^/OUTPUT^} (@code{gnatpp})
15108 Write the output into @var{output_file}.
15109 If @var{output_file} already exists, @command{gnatpp} terminates without
15110 reading or processing the input file.
15111
15112 @item ^-of ^/FORCED_OUTPUT=^@var{output_file}
15113 @cindex @option{^-of^/FORCED_OUTPUT^} (@code{gnatpp})
15114 Write the output into @var{output_file}, overwriting the existing file
15115 (if one is present).
15116
15117 @item ^-r^/REPLACE^
15118 @cindex @option{^-r^/REPLACE^} (@code{gnatpp})
15119 Replace the input source file with the reformatted output, and copy the
15120 original input source into the file whose name is obtained by appending the
15121 ^@file{.npp}^@file{$NPP}^ suffix to the name of the input file.
15122 If a file with this name already exists, @command{gnatpp} terminates without
15123 reading or processing the input file.
15124
15125 @item ^-rf^/OVERRIDING_REPLACE^
15126 @cindex @option{^-rf^/OVERRIDING_REPLACE^} (@code{gnatpp})
15127 Like @option{^-r^/REPLACE^} except that if the file with the specified name
15128 already exists, it is overwritten.
15129
15130 @item ^-rnb^/NO_BACKUP^
15131 @cindex @option{^-rnb^/NO_BACKUP^} (@code{gnatpp})
15132 Replace the input source file with the reformatted output without
15133 creating any backup copy of the input source.
15134
15135 @item ^--eol=@var{xxx}^/END_OF_LINE=@var{xxx}^
15136 @cindex @option{^--eol^/END_OF_LINE^} (@code{gnatpp})
15137 Specifies the format of the reformatted output file. The @var{xxx}
15138 ^string specified with the switch^option^ may be either
15139 @itemize @bullet
15140 @item ``@option{^dos^DOS^}'' MS DOS style, lines end with CR LF characters
15141 @item ``@option{^crlf^CRLF^}''
15142 the same as @option{^crlf^CRLF^}
15143 @item ``@option{^unix^UNIX^}'' UNIX style, lines end with LF character
15144 @item ``@option{^lf^LF^}''
15145 the same as @option{^unix^UNIX^}
15146 @end itemize
15147
15148 @end table
15149
15150 @noindent
15151 Options @option{^-pipe^/STANDARD_OUTPUT^},
15152 @option{^-o^/OUTPUT^} and
15153 @option{^-of^/FORCED_OUTPUT^} are allowed only if the call to gnatpp
15154 contains only one file to reformat.
15155 Option
15156 @option{^--eol^/END_OF_LINE^}
15157 cannot be used together
15158 with @option{^-pipe^/STANDARD_OUTPUT^} option.
15159
15160 @node Other gnatpp Switches
15161 @subsection Other @code{gnatpp} Switches
15162
15163 @noindent
15164 The additional @command{gnatpp} switches are defined in this subsection.
15165
15166 @table @option
15167 @item ^-files @var{filename}^/FILES=@var{output_file}^
15168 @cindex @option{^-files^/FILES^} (@code{gnatpp})
15169 Take the argument source files from the specified file. This file should be an
15170 ordinary textual file containing file names separated by spaces or
15171 line breaks. You can use this switch more then once in the same call to
15172 @command{gnatpp}. You also can combine this switch with explicit list of
15173 files.
15174
15175 @item ^-v^/VERBOSE^
15176 @cindex @option{^-v^/VERBOSE^} (@code{gnatpp})
15177 Verbose mode;
15178 @command{gnatpp} generates version information and then
15179 a trace of the actions it takes to produce or obtain the ASIS tree.
15180
15181 @item ^-w^/WARNINGS^
15182 @cindex @option{^-w^/WARNINGS^} (@code{gnatpp})
15183 Warning mode;
15184 @command{gnatpp} generates a warning whenever it cannot provide
15185 a required layout in the result source.
15186 @end table
15187
15188 @node Formatting Rules
15189 @section Formatting Rules
15190
15191 @noindent
15192 The following subsections show how @command{gnatpp} treats ``white space'',
15193 comments, program layout, and name casing.
15194 They provide the detailed descriptions of the switches shown above.
15195
15196 @menu
15197 * White Space and Empty Lines::
15198 * Formatting Comments::
15199 * Construct Layout::
15200 * Name Casing::
15201 @end menu
15202
15203 @node White Space and Empty Lines
15204 @subsection White Space and Empty Lines
15205
15206 @noindent
15207 @command{gnatpp} does not have an option to control space characters.
15208 It will add or remove spaces according to the style illustrated by the
15209 examples in the @cite{Ada Reference Manual}.
15210
15211 The only format effectors
15212 (see @cite{Ada Reference Manual}, paragraph 2.1(13))
15213 that will appear in the output file are platform-specific line breaks,
15214 and also format effectors within (but not at the end of) comments.
15215 In particular, each horizontal tab character that is not inside
15216 a comment will be treated as a space and thus will appear in the
15217 output file as zero or more spaces depending on
15218 the reformatting of the line in which it appears.
15219 The only exception is a Form Feed character, which is inserted after a
15220 pragma @code{Page} when @option{-ff} is set.
15221
15222 The output file will contain no lines with trailing ``white space'' (spaces,
15223 format effectors).
15224
15225 Empty lines in the original source are preserved
15226 only if they separate declarations or statements.
15227 In such contexts, a
15228 sequence of two or more empty lines is replaced by exactly one empty line.
15229 Note that a blank line will be removed if it separates two ``comment blocks''
15230 (a comment block is a sequence of whole-line comments).
15231 In order to preserve a visual separation between comment blocks, use an
15232 ``empty comment'' (a line comprising only hyphens) rather than an empty line.
15233 Likewise, if for some reason you wish to have a sequence of empty lines,
15234 use a sequence of empty comments instead.
15235
15236 @node Formatting Comments
15237 @subsection Formatting Comments
15238
15239 @noindent
15240 Comments in Ada code are of two kinds:
15241 @itemize @bullet
15242 @item
15243 a @emph{whole-line comment}, which appears by itself (possibly preceded by
15244 ``white space'') on a line
15245
15246 @item
15247 an @emph{end-of-line comment}, which follows some other Ada lexical element
15248 on the same line.
15249 @end itemize
15250
15251 @noindent
15252 The indentation of a whole-line comment is that of either
15253 the preceding or following line in
15254 the formatted source, depending on switch settings as will be described below.
15255
15256 For an end-of-line comment, @command{gnatpp} leaves the same number of spaces
15257 between the end of the preceding Ada lexical element and the beginning
15258 of the comment as appear in the original source,
15259 unless either the comment has to be split to
15260 satisfy the line length limitation, or else the next line contains a
15261 whole line comment that is considered a continuation of this end-of-line
15262 comment (because it starts at the same position).
15263 In the latter two
15264 cases, the start of the end-of-line comment is moved right to the nearest
15265 multiple of the indentation level.
15266 This may result in a ``line overflow'' (the right-shifted comment extending
15267 beyond the maximum line length), in which case the comment is split as
15268 described below.
15269
15270 There is a difference between @option{^-c1^/COMMENTS_LAYOUT=DEFAULT^}
15271 (GNAT-style comment line indentation)
15272 and @option{^-c2^/COMMENTS_LAYOUT=STANDARD_INDENT^}
15273 (reference-manual comment line indentation).
15274 With reference-manual style, a whole-line comment is indented as if it
15275 were a declaration or statement at the same place
15276 (i.e., according to the indentation of the preceding line(s)).
15277 With GNAT style, a whole-line comment that is immediately followed by an
15278 @b{if} or @b{case} statement alternative, a record variant, or the reserved
15279 word @b{begin}, is indented based on the construct that follows it.
15280
15281 For example:
15282 @smallexample @c ada
15283 @cartouche
15284 if A then
15285     null;
15286        -- some comment
15287 else
15288    null;
15289 end if;
15290 @end cartouche
15291 @end smallexample
15292
15293 @noindent
15294 Reference-manual indentation produces:
15295
15296 @smallexample @c ada
15297 @cartouche
15298 if A then
15299    null;
15300    --  some comment
15301 else
15302    null;
15303 end if;
15304 @end cartouche
15305 @end smallexample
15306
15307 @noindent
15308 while GNAT-style indentation produces:
15309
15310 @smallexample @c ada
15311 @cartouche
15312 if A then
15313    null;
15314 --  some comment
15315 else
15316    null;
15317 end if;
15318 @end cartouche
15319 @end smallexample
15320
15321 @noindent
15322 The @option{^-c3^/COMMENTS_LAYOUT=GNAT_BEGINNING^} switch
15323 (GNAT style comment beginning) has the following
15324 effect:
15325
15326 @itemize @bullet
15327 @item
15328 For each whole-line comment that does not end with two hyphens,
15329 @command{gnatpp} inserts spaces if necessary after the starting two hyphens
15330 to ensure that there are at least two spaces between these hyphens and the
15331 first non-blank character of the comment.
15332 @end itemize
15333
15334 @noindent
15335 For an end-of-line comment, if in the original source the next line is a
15336 whole-line comment that starts at the same position
15337 as the end-of-line comment,
15338 then the whole-line comment (and all whole-line comments
15339 that follow it and that start at the same position)
15340 will start at this position in the output file.
15341
15342 @noindent
15343 That is, if in the original source we have:
15344
15345 @smallexample @c ada
15346 @cartouche
15347 begin
15348 A := B + C;            --  B must be in the range Low1..High1
15349                        --  C must be in the range Low2..High2
15350              --B+C will be in the range Low1+Low2..High1+High2
15351 X := X + 1;
15352 @end cartouche
15353 @end smallexample
15354
15355 @noindent
15356 Then in the formatted source we get
15357
15358 @smallexample @c ada
15359 @cartouche
15360 begin
15361    A := B + C;            --  B must be in the range Low1..High1
15362                           --  C must be in the range Low2..High2
15363    --  B+C will be in the range Low1+Low2..High1+High2
15364    X := X + 1;
15365 @end cartouche
15366 @end smallexample
15367
15368 @noindent
15369 A comment that exceeds the line length limit will be split.
15370 Unless switch
15371 @option{^-c4^/COMMENTS_LAYOUT=REFORMAT^} (reformat comment blocks) is set and
15372 the line belongs to a reformattable block, splitting the line generates a
15373 @command{gnatpp} warning.
15374 The @option{^-c4^/COMMENTS_LAYOUT=REFORMAT^} switch specifies that whole-line
15375 comments may be reformatted in typical
15376 word processor style (that is, moving words between lines and putting as
15377 many words in a line as possible).
15378
15379 @node Construct Layout
15380 @subsection Construct Layout
15381
15382 @noindent
15383 In several cases the suggested layout in the Ada Reference Manual includes
15384 an extra level of indentation that many programmers prefer to avoid. The
15385 affected cases include:
15386
15387 @itemize @bullet
15388
15389 @item Record type declaration (RM 3.8)
15390
15391 @item Record representation clause (RM 13.5.1)
15392
15393 @item Loop statement in case if a loop has a statement identifier (RM 5.6)
15394
15395 @item Block statement in case if a block has a statement identifier (RM 5.6)
15396 @end itemize
15397
15398 @noindent
15399 In compact mode (when GNAT style layout or compact layout is set),
15400 the pretty printer uses one level of indentation instead
15401 of two. This is achieved in the record definition and record representation
15402 clause cases by putting the @code{record} keyword on the same line as the
15403 start of the declaration or representation clause, and in the block and loop
15404 case by putting the block or loop header on the same line as the statement
15405 identifier.
15406
15407 @noindent
15408 The difference between GNAT style @option{^-l1^/CONSTRUCT_LAYOUT=GNAT^}
15409 and compact @option{^-l2^/CONSTRUCT_LAYOUT=COMPACT^}
15410 layout on the one hand, and uncompact layout
15411 @option{^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^} on the other hand,
15412 can be illustrated by the following examples:
15413
15414 @iftex
15415 @cartouche
15416 @multitable @columnfractions .5 .5
15417 @item @i{GNAT style, compact layout} @tab @i{Uncompact layout}
15418
15419 @item
15420 @smallexample @c ada
15421 type q is record
15422    a : integer;
15423    b : integer;
15424 end record;
15425 @end smallexample
15426 @tab
15427 @smallexample @c ada
15428 type q is
15429    record
15430       a : integer;
15431       b : integer;
15432    end record;
15433 @end smallexample
15434
15435 @item
15436 @smallexample @c ada
15437 for q use record
15438    a at 0 range  0 .. 31;
15439    b at 4 range  0 .. 31;
15440 end record;
15441 @end smallexample
15442 @tab
15443 @smallexample @c ada
15444 for q use
15445    record
15446       a at 0 range  0 .. 31;
15447       b at 4 range  0 .. 31;
15448    end record;
15449 @end smallexample
15450
15451 @item
15452 @smallexample @c ada
15453 Block : declare
15454    A : Integer := 3;
15455 begin
15456    Proc (A, A);
15457 end Block;
15458 @end smallexample
15459 @tab
15460 @smallexample @c ada
15461 Block :
15462    declare
15463       A : Integer := 3;
15464    begin
15465       Proc (A, A);
15466    end Block;
15467 @end smallexample
15468
15469 @item
15470 @smallexample @c ada
15471 Clear : for J in 1 .. 10 loop
15472    A (J) := 0;
15473 end loop Clear;
15474 @end smallexample
15475 @tab
15476 @smallexample @c ada
15477 Clear :
15478    for J in 1 .. 10 loop
15479       A (J) := 0;
15480    end loop Clear;
15481 @end smallexample
15482 @end multitable
15483 @end cartouche
15484 @end iftex
15485
15486 @ifnottex
15487 @smallexample
15488 @cartouche
15489 GNAT style, compact layout              Uncompact layout
15490
15491 type q is record                        type q is
15492    a : integer;                            record
15493    b : integer;                               a : integer;
15494 end record;                                   b : integer;
15495                                            end record;
15496
15497 for q use record                        for q use
15498    a at 0 range  0 .. 31;                  record
15499    b at 4 range  0 .. 31;                     a at 0 range  0 .. 31;
15500 end record;                                   b at 4 range  0 .. 31;
15501                                            end record;
15502
15503 Block : declare                         Block :
15504    A : Integer := 3;                       declare
15505 begin                                         A : Integer := 3;
15506    Proc (A, A);                            begin
15507 end Block;                                    Proc (A, A);
15508                                            end Block;
15509
15510 Clear : for J in 1 .. 10 loop           Clear :
15511    A (J) := 0;                             for J in 1 .. 10 loop
15512 end loop Clear;                               A (J) := 0;
15513                                            end loop Clear;
15514 @end cartouche
15515 @end smallexample
15516 @end ifnottex
15517
15518 @noindent
15519 A further difference between GNAT style layout and compact layout is that
15520 GNAT style layout inserts empty lines as separation for
15521 compound statements, return statements and bodies.
15522
15523 @node Name Casing
15524 @subsection Name Casing
15525
15526 @noindent
15527 @command{gnatpp} always converts the usage occurrence of a (simple) name to
15528 the same casing as the corresponding defining identifier.
15529
15530 You control the casing for defining occurrences via the
15531 @option{^-n^/NAME_CASING^} switch.
15532 @ifclear vms
15533 With @option{-nD} (``as declared'', which is the default),
15534 @end ifclear
15535 @ifset vms
15536 With @option{/NAME_CASING=AS_DECLARED}, which is the default,
15537 @end ifset
15538 defining occurrences appear exactly as in the source file
15539 where they are declared.
15540 The other ^values for this switch^options for this qualifier^ ---
15541 @option{^-nU^UPPER_CASE^},
15542 @option{^-nL^LOWER_CASE^},
15543 @option{^-nM^MIXED_CASE^} ---
15544 result in
15545 ^upper, lower, or mixed case, respectively^the corresponding casing^.
15546 If @command{gnatpp} changes the casing of a defining
15547 occurrence, it analogously changes the casing of all the
15548 usage occurrences of this name.
15549
15550 If the defining occurrence of a name is not in the source compilation unit
15551 currently being processed by @command{gnatpp}, the casing of each reference to
15552 this name is changed according to the value of the @option{^-n^/NAME_CASING^}
15553 switch (subject to the dictionary file mechanism described below).
15554 Thus @command{gnatpp} acts as though the @option{^-n^/NAME_CASING^} switch
15555 had affected the
15556 casing for the defining occurrence of the name.
15557
15558 Some names may need to be spelled with casing conventions that are not
15559 covered by the upper-, lower-, and mixed-case transformations.
15560 You can arrange correct casing by placing such names in a
15561 @emph{dictionary file},
15562 and then supplying a @option{^-D^/DICTIONARY^} switch.
15563 The casing of names from dictionary files overrides
15564 any @option{^-n^/NAME_CASING^} switch.
15565
15566 To handle the casing of Ada predefined names and the names from GNAT libraries,
15567 @command{gnatpp} assumes a default dictionary file.
15568 The name of each predefined entity is spelled with the same casing as is used
15569 for the entity in the @cite{Ada Reference Manual}.
15570 The name of each entity in the GNAT libraries is spelled with the same casing
15571 as is used in the declaration of that entity.
15572
15573 The @w{@option{^-D-^/SPECIFIC_CASING^}} switch suppresses the use of the
15574 default dictionary file.
15575 Instead, the casing for predefined and GNAT-defined names will be established
15576 by the @option{^-n^/NAME_CASING^} switch or explicit dictionary files.
15577 For example, by default the names @code{Ada.Text_IO} and @code{GNAT.OS_Lib}
15578 will appear as just shown,
15579 even in the presence of a @option{^-nU^/NAME_CASING=UPPER_CASE^} switch.
15580 To ensure that even such names are rendered in uppercase,
15581 additionally supply the @w{@option{^-D-^/SPECIFIC_CASING^}} switch
15582 (or else, less conveniently, place these names in upper case in a dictionary
15583 file).
15584
15585 A dictionary file is
15586 a plain text file; each line in this file can be either a blank line
15587 (containing only space characters and ASCII.HT characters), an Ada comment
15588 line, or the specification of exactly one @emph{casing schema}.
15589
15590 A casing schema is a string that has the following syntax:
15591
15592 @smallexample
15593 @cartouche
15594   @var{casing_schema} ::= @var{identifier} | *@var{simple_identifier}*
15595
15596   @var{simple_identifier} ::= @var{letter}@{@var{letter_or_digit}@}
15597 @end cartouche
15598 @end smallexample
15599
15600 @noindent
15601 (See @cite{Ada Reference Manual}, Section 2.3) for the definition of the
15602 @var{identifier} lexical element and the @var{letter_or_digit} category.)
15603
15604 The casing schema string can be followed by white space and/or an Ada-style
15605 comment; any amount of white space is allowed before the string.
15606
15607 If a dictionary file is passed as
15608 @ifclear vms
15609 the value of a @option{-D@var{file}} switch
15610 @end ifclear
15611 @ifset vms
15612 an option to the @option{/DICTIONARY} qualifier
15613 @end ifset
15614 then for every
15615 simple name and every identifier, @command{gnatpp} checks if the dictionary
15616 defines the casing for the name or for some of its parts (the term ``subword''
15617 is used below to denote the part of a name which is delimited by ``_'' or by
15618 the beginning or end of the word and which does not contain any ``_'' inside):
15619
15620 @itemize @bullet
15621 @item
15622 if the whole name is in the dictionary, @command{gnatpp} uses for this name
15623 the casing defined by the dictionary; no subwords are checked for this word
15624
15625 @item
15626 for every subword @command{gnatpp} checks if the dictionary contains the
15627 corresponding string of the form @code{*@var{simple_identifier}*},
15628 and if it does, the casing of this @var{simple_identifier} is used
15629 for this subword
15630
15631 @item
15632 if the whole name does not contain any ``_'' inside, and if for this name
15633 the dictionary contains two entries - one of the form @var{identifier},
15634 and another - of the form *@var{simple_identifier}*, then the first one
15635 is applied to define the casing of this name
15636
15637 @item
15638 if more than one dictionary file is passed as @command{gnatpp} switches, each
15639 dictionary adds new casing exceptions and overrides all the existing casing
15640 exceptions set by the previous dictionaries
15641
15642 @item
15643 when @command{gnatpp} checks if the word or subword is in the dictionary,
15644 this check is not case sensitive
15645 @end itemize
15646
15647 @noindent
15648 For example, suppose we have the following source to reformat:
15649
15650 @smallexample @c ada
15651 @cartouche
15652 procedure test is
15653    name1 : integer := 1;
15654    name4_name3_name2 : integer := 2;
15655    name2_name3_name4 : Boolean;
15656    name1_var : Float;
15657 begin
15658    name2_name3_name4 := name4_name3_name2 > name1;
15659 end;
15660 @end cartouche
15661 @end smallexample
15662
15663 @noindent
15664 And suppose we have two dictionaries:
15665
15666 @smallexample
15667 @cartouche
15668 @i{dict1:}
15669    NAME1
15670    *NaMe3*
15671    *Name1*
15672 @end cartouche
15673
15674 @cartouche
15675 @i{dict2:}
15676   *NAME3*
15677 @end cartouche
15678 @end smallexample
15679
15680 @noindent
15681 If @command{gnatpp} is called with the following switches:
15682
15683 @smallexample
15684 @ifclear vms
15685 @command{gnatpp -nM -D dict1 -D dict2 test.adb}
15686 @end ifclear
15687 @ifset vms
15688 @command{gnatpp test.adb /NAME_CASING=MIXED_CASE /DICTIONARY=(dict1, dict2)}
15689 @end ifset
15690 @end smallexample
15691
15692 @noindent
15693 then we will get the following name casing in the @command{gnatpp} output:
15694
15695 @smallexample @c ada
15696 @cartouche
15697 procedure Test is
15698    NAME1             : Integer := 1;
15699    Name4_NAME3_Name2 : Integer := 2;
15700    Name2_NAME3_Name4 : Boolean;
15701    Name1_Var         : Float;
15702 begin
15703    Name2_NAME3_Name4 := Name4_NAME3_Name2 > NAME1;
15704 end Test;
15705 @end cartouche
15706 @end smallexample
15707
15708 @c *********************************
15709 @node The GNAT Metric Tool gnatmetric
15710 @chapter The GNAT Metric Tool @command{gnatmetric}
15711 @findex gnatmetric
15712 @cindex Metric tool
15713
15714 @noindent
15715 ^The @command{gnatmetric} tool^@command{GNAT METRIC}^ is an ASIS-based utility
15716 for computing various program metrics.
15717 It takes an Ada source file as input and generates a file containing the
15718 metrics data as output. Various switches control which
15719 metrics are computed and output.
15720
15721 @command{gnatmetric} generates and uses the ASIS
15722 tree for the input source and thus requires the input to be syntactically and
15723 semantically legal.
15724 If this condition is not met, @command{gnatmetric} will generate
15725 an error message; no metric information for this file will be
15726 computed and reported.
15727
15728 If the compilation unit contained in the input source depends semantically
15729 upon units in files located outside the current directory, you have to provide
15730 the source search path when invoking @command{gnatmetric}.
15731 If it depends semantically upon units that are contained
15732 in files with names that do not follow the GNAT file naming rules, you have to
15733 provide the configuration file describing the corresponding naming scheme (see
15734 the description of the @command{gnatmetric} switches below.)
15735 Alternatively, you may use a project file and invoke @command{gnatmetric}
15736 through the @command{gnat} driver.
15737
15738
15739 The @command{gnatmetric} command has the form
15740
15741 @smallexample
15742 $ gnatmetric [@i{switches}] @{@i{filename}@} [@i{-cargs gcc_switches}]
15743 @end smallexample
15744
15745 @noindent
15746 where
15747 @itemize @bullet
15748 @item
15749 @i{switches} specify the metrics to compute and define the destination for
15750 the output
15751
15752 @item
15753 Each @i{filename} is the name (including the extension) of a source
15754 file to process. ``Wildcards'' are allowed, and
15755 the file name may contain path information.
15756 If no @i{filename} is supplied, then the @i{switches} list must contain
15757 at least one
15758 @option{-files} switch (@pxref{Other gnatmetric Switches}).
15759 Including both a @option{-files} switch and one or more
15760 @i{filename} arguments is permitted.
15761
15762 @item
15763 @i{-cargs gcc_switches} is a list of switches for
15764 @command{gcc}. They will be passed on to all compiler invocations made by
15765 @command{gnatmetric} to generate the ASIS trees. Here you can provide
15766 @option{^-I^/INCLUDE_DIRS=^} switches to form the source search path,
15767 and use the @option{-gnatec} switch to set the configuration file.
15768 @end itemize
15769
15770 @menu
15771 * Switches for gnatmetric::
15772 @end menu
15773
15774 @node Switches for gnatmetric
15775 @section Switches for @command{gnatmetric}
15776
15777 @noindent
15778 The following subsections describe the various switches accepted by
15779 @command{gnatmetric}, organized by category.
15780
15781 @menu
15782 * Output Files Control::
15783 * Disable Metrics For Local Units::
15784 * Line Metrics Control::
15785 * Syntax Metrics Control::
15786 * Complexity Metrics Control::
15787 * Other gnatmetric Switches::
15788 @end menu
15789
15790 @node Output Files Control
15791 @subsection Output File Control
15792 @cindex Output file control in @command{gnatmetric}
15793
15794 @noindent
15795 @command{gnatmetric} has two output formats. It can generate a
15796 textual (human-readable) form, and also XML. By default only textual
15797 output is generated.
15798
15799 When generating the output in textual form, @command{gnatmetric} creates
15800 for each Ada source file a corresponding text file
15801 containing the computed metrics. By default, this file
15802 is placed in the same directory as where the source file is located, and
15803 its name is obtained
15804 by appending the ^@file{.metrix}^@file{$METRIX}^ suffix to the name of the
15805 input file.
15806
15807 All the output information generated in XML format is placed in a single
15808 file. By default this file is placed in the current directory and has the
15809 name ^@file{metrix.xml}^@file{METRIX$XML}^.
15810
15811 Some of the computed metrics are summed over the units passed to
15812 @command{gnatmetric}; for example, the total number of lines of code.
15813 By default this information is sent to @file{stdout}, but a file
15814 can be specified with the @option{-og} switch.
15815
15816 The following switches control the @command{gnatmetric} output:
15817
15818 @table @option
15819 @cindex @option{^-x^/XML^} (@command{gnatmetric})
15820 @item ^-x^/XML^
15821 Generate the XML output
15822
15823 @cindex @option{^-nt^/NO_TEXT^} (@command{gnatmetric})
15824 @item ^-nt^/NO_TEXT^
15825 Do not generate the output in text form (implies @option{^-x^/XML^})
15826
15827 @cindex @option{^-d^/DIRECTORY^} (@command{gnatmetric})
15828 @item ^-d @var{output_dir}^/DIRECTORY=@var{output_dir}^
15829 Put textual files with detailed metrics into @var{output_dir}
15830
15831 @cindex @option{^-o^/SUFFIX_DETAILS^} (@command{gnatmetric})
15832 @item ^-o @var{file_suffix}^/SUFFIX_DETAILS=@var{file_suffix}^
15833 Use @var{file_suffix}, instead of ^@file{.metrix}^@file{$METRIX}^
15834 in the name of the output file.
15835
15836 @cindex @option{^-og^/GLOBAL_OUTPUT^} (@command{gnatmetric})
15837 @item ^-og @var{file_name}^/GLOBAL_OUTPUT=@var{file_name}^
15838 Put global metrics into @var{file_name}
15839
15840 @cindex @option{^-ox^/XML_OUTPUT^} (@command{gnatmetric})
15841 @item ^-ox @var{file_name}^/XML_OUTPUT=@var{file_name}^
15842 Put the XML output into @var{file_name} (also implies @option{^-x^/XML^})
15843
15844 @cindex @option{^-sfn^/SHORT_SOURCE_FILE_NAME^} (@command{gnatmetric})
15845 @item ^-sfn^/SHORT_SOURCE_FILE_NAME^
15846 Use ``short'' source file names in the output.  (The @command{gnatmetric}
15847 output includes the name(s) of the Ada source file(s) from which the metrics
15848 are computed.  By default each name includes the absolute path. The
15849 @option{^-sfn^/SHORT_SOURCE_FILE_NAME^} switch causes @command{gnatmetric}
15850 to exclude all directory information from the file names that are output.)
15851
15852 @end table
15853
15854 @node Disable Metrics For Local Units
15855 @subsection Disable Metrics For Local Units
15856 @cindex Disable Metrics For Local Units in @command{gnatmetric}
15857
15858 @noindent
15859 @command{gnatmetric} relies on the GNAT compilation model @minus{}
15860 one compilation
15861 unit per one source file. It computes line metrics for the whole source
15862 file, and it also computes syntax
15863 and complexity metrics for the file's outermost unit.
15864
15865 By default, @command{gnatmetric} will also compute all metrics for certain
15866 kinds of locally declared program units:
15867
15868 @itemize @bullet
15869 @item
15870 subprogram (and generic subprogram) bodies;
15871
15872 @item
15873 package (and generic package) specifications and bodies;
15874
15875 @item
15876 task object and type specifications and bodies;
15877
15878 @item
15879 protected object and type specifications and bodies.
15880 @end itemize
15881
15882 @noindent
15883 These kinds of entities will be referred to as
15884 @emph{eligible local program units}, or simply @emph{eligible local units},
15885 @cindex Eligible local unit (for @command{gnatmetric})
15886 in the discussion below.
15887
15888 Note that a subprogram declaration, generic instantiation,
15889 or renaming declaration only receives metrics
15890 computation when it appear as the outermost entity
15891 in a source file.
15892
15893 Suppression of metrics computation for eligible local units can be
15894 obtained via the following switch:
15895
15896 @table @option
15897 @cindex @option{^-n@var{x}^/SUPPRESS^} (@command{gnatmetric})
15898 @item ^-nolocal^/SUPPRESS=LOCAL_DETAILS^
15899 Do not compute detailed metrics for eligible local program units
15900
15901 @end table
15902
15903 @node Line Metrics Control
15904 @subsection Line Metrics Control
15905 @cindex Line metrics control in @command{gnatmetric}
15906
15907 @noindent
15908 For any (legal) source file, and for each of its
15909 eligible local program units, @command{gnatmetric} computes the following
15910 metrics:
15911
15912 @itemize @bullet
15913 @item
15914 the total number of lines;
15915
15916 @item
15917 the total number of code lines (i.e., non-blank lines that are not comments)
15918
15919 @item
15920 the number of comment lines
15921
15922 @item
15923 the number of code lines containing end-of-line comments;
15924
15925 @item
15926 the number of empty lines and lines containing only space characters and/or
15927 format effectors (blank lines)
15928
15929 @end itemize
15930
15931 If @command{gnatmetric} is invoked on more than one source file, it sums the
15932 values of the line metrics for all the files being processed and then
15933 generates the cumulative results.
15934
15935 By default, all the line metrics are computed and reported. You can use the
15936 following switches to select the specific line metrics to be computed and
15937 reported (if any of these parameters is set, only explicitly specified line
15938 metrics are computed).
15939
15940 @table @option
15941 @cindex @option{^-la^/LINES_ALL^} (@command{gnatmetric})
15942 @item ^-la^/LINES_ALL^
15943 The number of all lines
15944
15945 @cindex @option{^-lcode^/CODE_LINES^} (@command{gnatmetric})
15946 @item ^-lcode^/CODE_LINES^
15947 The number of code lines
15948
15949 @cindex @option{^-lcomm^/COMENT_LINES^} (@command{gnatmetric})
15950 @item ^-lcomm^/COMENT_LINES^
15951 The number of comment lines
15952
15953 @cindex @option{^-leol^/MIXED_CODE_COMMENTS^} (@command{gnatmetric})
15954 @item ^-leol^/MIXED_CODE_COMMENTS^
15955 The number of code lines containing
15956 end-of-line comments
15957
15958 @cindex @option{^-lb^/BLANK_LINES^} (@command{gnatmetric})
15959 @item ^-lb^/BLANK_LINES^
15960 The number of blank lines
15961
15962 @end table
15963
15964
15965 @node Syntax Metrics Control
15966 @subsection Syntax Metrics Control
15967 @cindex Syntax metrics control in @command{gnatmetric}
15968
15969 @noindent
15970 @command{gnatmetric} computes various syntactic metrics for the
15971 outermost unit and for each eligible local unit:
15972
15973 @table @emph
15974 @item LSLOC (``Logical Source Lines Of Code'')
15975 The total number of declarations and the total number of statements
15976
15977 @item Maximal static nesting level of inner program units
15978 According to
15979 @cite{Ada 95 Language Reference Manual}, 10.1(1), ``A program unit is either a
15980 package, a task unit, a protected unit, a
15981 protected entry, a generic unit, or an explicitly declared subprogram other
15982 than an enumeration literal.''
15983
15984 @item Maximal nesting level of composite syntactic constructs
15985 This corresponds to the notion of the
15986 maximum nesting level in the GNAT built-in style checks
15987 (@pxref{Style Checking})
15988 @end table
15989
15990 @noindent
15991 For the outermost unit in the file, @command{gnatmetric} additionally computes
15992 the following metrics:
15993
15994 @table @emph
15995 @item Public subprograms
15996 This metric is computed for package specifications. It is the
15997 number of subprograms and generic subprograms declared in the visible
15998 part (including in nested packages, protected objects, and
15999 protected types).
16000
16001 @item All subprograms
16002 This metric is computed for bodies and subunits. The
16003 metric is equal to a total number of subprogram bodies in the compilation
16004 unit.
16005 Neither generic instantiations nor renamings-as-a-body nor body stubs
16006 are counted. Any subprogram body is counted, independently of its nesting
16007 level and enclosing constructs. Generic bodies and bodies of protected
16008 subprograms are counted in the same way as ``usual'' subprogram bodies.
16009
16010 @item Public types
16011 This metric is computed for package specifications and
16012 generic package declarations. It is the total number of types
16013 that can be referenced from outside this compilation unit, plus the
16014 number of types from all the visible parts of all the visible generic packages.
16015 Generic formal types are not counted.  Only types, not subtypes,
16016 are included.
16017
16018 @noindent
16019 Along with the total number of public types, the following
16020 types are counted and reported separately:
16021
16022 @itemize @bullet
16023 @item
16024 Abstract types
16025
16026 @item
16027 Root tagged types (abstract, non-abstract, private, non-private). Type
16028 extensions are @emph{not} counted
16029
16030 @item
16031 Private types (including private extensions)
16032
16033 @item
16034 Task types
16035
16036 @item
16037 Protected types
16038
16039 @end itemize
16040
16041 @item All types
16042 This metric is computed for any compilation unit. It is equal to the total
16043 number of the declarations of different types given in the compilation unit.
16044 The private and the corresponding full type declaration are counted as one
16045 type declaration. Incomplete type declarations and generic formal types
16046 are not counted.
16047 No distinction is made among different kinds of types (abstract,
16048 private etc.); the total number of types is computed and reported.
16049
16050 @end table
16051
16052 @noindent
16053 By default, all the syntax metrics are computed and reported. You can use the
16054 following switches to select specific syntax metrics;
16055 if any of these is set, only the explicitly specified metrics are computed.
16056
16057 @table @option
16058 @cindex @option{^-ed^/DECLARATION_TOTAL^} (@command{gnatmetric})
16059 @item ^-ed^/DECLARATION_TOTAL^
16060 The total number of declarations
16061
16062 @cindex @option{^-es^/STATEMENT_TOTAL^} (@command{gnatmetric})
16063 @item ^-es^/STATEMENT_TOTAL^
16064 The total number of statements
16065
16066 @cindex @option{^-eps^/^} (@command{gnatmetric})
16067 @item ^-eps^/INT_SUBPROGRAMS^
16068 The number of public subprograms in a compilation unit
16069
16070 @cindex @option{^-eas^/SUBPROGRAMS_ALL^} (@command{gnatmetric})
16071 @item ^-eas^/SUBPROGRAMS_ALL^
16072 The number of all the subprograms in a compilation unit
16073
16074 @cindex @option{^-ept^/INT_TYPES^} (@command{gnatmetric})
16075 @item ^-ept^/INT_TYPES^
16076 The number of public types in a compilation unit
16077
16078 @cindex @option{^-eat^/TYPES_ALL^} (@command{gnatmetric})
16079 @item ^-eat^/TYPES_ALL^
16080 The number of all the types in a compilation unit
16081
16082 @cindex @option{^-enu^/PROGRAM_NESTING_MAX^} (@command{gnatmetric})
16083 @item ^-enu^/PROGRAM_NESTING_MAX^
16084 The maximal program unit nesting level
16085
16086 @cindex @option{^-ec^/CONSTRUCT_NESTING_MAX^} (@command{gnatmetric})
16087 @item ^-ec^/CONSTRUCT_NESTING_MAX^
16088 The maximal construct nesting level
16089
16090 @end table
16091
16092 @node Complexity Metrics Control
16093 @subsection Complexity Metrics Control
16094 @cindex Complexity metrics control in @command{gnatmetric}
16095
16096 @noindent
16097 For a program unit that is an executable body (a subprogram body (including
16098 generic bodies), task body, entry body or a package body containing
16099 its own statement sequence ) @command{gnatmetric} computes the following
16100 complexity metrics:
16101
16102 @itemize @bullet
16103 @item
16104 McCabe cyclomatic complexity;
16105
16106 @item
16107 McCabe essential complexity;
16108
16109 @item
16110 maximal loop nesting level
16111
16112 @end itemize
16113
16114 @noindent
16115 The McCabe complexity metrics are defined
16116 in @url{www.mccabe.com/pdf/nist235r.pdf}
16117
16118 According to McCabe, both control statements and short-circuit control forms
16119 should be taken into account when computing cyclomatic complexity. For each
16120 body, we compute three metric values:
16121
16122 @itemize @bullet
16123 @item
16124 the complexity introduced by control
16125 statements only, without taking into account short-circuit forms,
16126
16127 @item
16128 the complexity introduced by short-circuit control forms only, and
16129
16130 @item
16131 the total
16132 cyclomatic complexity, which is the sum of these two values.
16133 @end itemize
16134
16135 @noindent
16136 When computing cyclomatic and essential complexity, @command{gnatmetric} skips
16137 the code in the exception handlers and in all the nested program units.
16138
16139 By default, all the complexity metrics are computed and reported.
16140 For more finely-grained control you can use
16141 the following switches:
16142
16143 @table @option
16144 @cindex @option{^-n@var{x}^/SUPPRESS^} (@command{gnatmetric})
16145
16146 @item ^-nocc^/SUPPRESS=CYCLOMATIC_COMPLEXITY^
16147 Do not compute the McCabe Cyclomatic Complexity
16148
16149 @item ^-noec^/SUPPRESS=ESSENTIAL_COMPLEXITY^
16150 Do not compute the Essential Complexity
16151
16152 @item ^-nonl^/SUPPRESS=MAXIMAL_LOOP_NESTING^
16153 Do not compute maximal loop nesting level
16154
16155 @item ^-ne^/SUPPRESS=EXITS_AS_GOTOS^
16156 Do not consider @code{exit} statements as @code{goto}s when
16157 computing Essential Complexity
16158
16159 @end table
16160
16161 @node Other gnatmetric Switches
16162 @subsection Other @code{gnatmetric} Switches
16163
16164 @noindent
16165 Additional @command{gnatmetric} switches are as follows:
16166
16167 @table @option
16168 @item ^-files @var{filename}^/FILES=@var{filename}^
16169 @cindex @option{^-files^/FILES^} (@code{gnatmetric})
16170 Take the argument source files from the specified file. This file should be an
16171 ordinary textual file containing file names separated by spaces or
16172 line breaks. You can use this switch more then once in the same call to
16173 @command{gnatmetric}. You also can combine this switch with
16174 an explicit list of files.
16175
16176 @item ^-v^/VERBOSE^
16177 @cindex @option{^-v^/VERBOSE^} (@code{gnatmetric})
16178 Verbose mode;
16179 @command{gnatmetric} generates version information and then
16180 a trace of sources being processed.
16181
16182 @item ^-dv^/DEBUG_OUTPUT^
16183 @cindex @option{^-dv^/DEBUG_OUTPUT^} (@code{gnatmetric})
16184 Debug mode;
16185 @command{gnatmetric} generates various messages useful to understand what
16186 happens during the metrics computation
16187
16188 @item ^-q^/QUIET^
16189 @cindex @option{^-q^/QUIET^} (@code{gnatmetric})
16190 Quiet mode.
16191 @end table
16192
16193 @c ***********************************
16194 @node File Name Krunching Using gnatkr
16195 @chapter File Name Krunching Using @code{gnatkr}
16196 @findex gnatkr
16197
16198 @noindent
16199 This chapter discusses the method used by the compiler to shorten
16200 the default file names chosen for Ada units so that they do not
16201 exceed the maximum length permitted. It also describes the
16202 @code{gnatkr} utility that can be used to determine the result of
16203 applying this shortening.
16204 @menu
16205 * About gnatkr::
16206 * Using gnatkr::
16207 * Krunching Method::
16208 * Examples of gnatkr Usage::
16209 @end menu
16210
16211 @node About gnatkr
16212 @section About @code{gnatkr}
16213
16214 @noindent
16215 The default file naming rule in GNAT
16216 is that the file name must be derived from
16217 the unit name. The exact default rule is as follows:
16218 @itemize @bullet
16219 @item
16220 Take the unit name and replace all dots by hyphens.
16221 @item
16222 If such a replacement occurs in the
16223 second character position of a name, and the first character is
16224 ^a, g, s, or i^A, G, S, or I^ then replace the dot by the character
16225 ^~ (tilde)^$ (dollar sign)^
16226 instead of a minus.
16227 @end itemize
16228 The reason for this exception is to avoid clashes
16229 with the standard names for children of System, Ada, Interfaces,
16230 and GNAT, which use the prefixes ^s- a- i- and g-^S- A- I- and G-^
16231 respectively.
16232
16233 The @option{^-gnatk^/FILE_NAME_MAX_LENGTH=^@var{nn}}
16234 switch of the compiler activates a ``krunching''
16235 circuit that limits file names to nn characters (where nn is a decimal
16236 integer). For example, using OpenVMS,
16237 where the maximum file name length is
16238 39, the value of nn is usually set to 39, but if you want to generate
16239 a set of files that would be usable if ported to a system with some
16240 different maximum file length, then a different value can be specified.
16241 The default value of 39 for OpenVMS need not be specified.
16242
16243 The @code{gnatkr} utility can be used to determine the krunched name for
16244 a given file, when krunched to a specified maximum length.
16245
16246 @node Using gnatkr
16247 @section Using @code{gnatkr}
16248
16249 @noindent
16250 The @code{gnatkr} command has the form
16251
16252 @ifclear vms
16253 @smallexample
16254 $ gnatkr @var{name} [@var{length}]
16255 @end smallexample
16256 @end ifclear
16257
16258 @ifset vms
16259 @smallexample
16260 $ gnatkr @var{name} /COUNT=nn
16261 @end smallexample
16262 @end ifset
16263
16264 @noindent
16265 @var{name} is the uncrunched file name, derived from the name of the unit
16266 in the standard manner described in the previous section (i.e. in particular
16267 all dots are replaced by hyphens). The file name may or may not have an
16268 extension (defined as a suffix of the form period followed by arbitrary
16269 characters other than period). If an extension is present then it will
16270 be preserved in the output. For example, when krunching @file{hellofile.ads}
16271 to eight characters, the result will be hellofil.ads.
16272
16273 Note: for compatibility with previous versions of @code{gnatkr} dots may
16274 appear in the name instead of hyphens, but the last dot will always be
16275 taken as the start of an extension. So if @code{gnatkr} is given an argument
16276 such as @file{Hello.World.adb} it will be treated exactly as if the first
16277 period had been a hyphen, and for example krunching to eight characters
16278 gives the result @file{hellworl.adb}.
16279
16280 Note that the result is always all lower case (except on OpenVMS where it is
16281 all upper case). Characters of the other case are folded as required.
16282
16283 @var{length} represents the length of the krunched name. The default
16284 when no argument is given is ^8^39^ characters. A length of zero stands for
16285 unlimited, in other words do not chop except for system files where the
16286 implied crunching length is always eight characters.
16287
16288 @noindent
16289 The output is the krunched name. The output has an extension only if the
16290 original argument was a file name with an extension.
16291
16292 @node Krunching Method
16293 @section Krunching Method
16294
16295 @noindent
16296 The initial file name is determined by the name of the unit that the file
16297 contains. The name is formed by taking the full expanded name of the
16298 unit and replacing the separating dots with hyphens and
16299 using ^lowercase^uppercase^
16300 for all letters, except that a hyphen in the second character position is
16301 replaced by a ^tilde^dollar sign^ if the first character is
16302 ^a, i, g, or s^A, I, G, or S^.
16303 The extension is @code{.ads} for a
16304 specification and @code{.adb} for a body.
16305 Krunching does not affect the extension, but the file name is shortened to
16306 the specified length by following these rules:
16307
16308 @itemize @bullet
16309 @item
16310 The name is divided into segments separated by hyphens, tildes or
16311 underscores and all hyphens, tildes, and underscores are
16312 eliminated. If this leaves the name short enough, we are done.
16313
16314 @item
16315 If the name is too long, the longest segment is located (left-most
16316 if there are two of equal length), and shortened by dropping
16317 its last character. This is repeated until the name is short enough.
16318
16319 As an example, consider the krunching of @*@file{our-strings-wide_fixed.adb}
16320 to fit the name into 8 characters as required by some operating systems.
16321
16322 @smallexample
16323 our-strings-wide_fixed 22
16324 our strings wide fixed 19
16325 our string  wide fixed 18
16326 our strin   wide fixed 17
16327 our stri    wide fixed 16
16328 our stri    wide fixe  15
16329 our str     wide fixe  14
16330 our str     wid  fixe  13
16331 our str     wid  fix   12
16332 ou  str     wid  fix   11
16333 ou  st      wid  fix   10
16334 ou  st      wi   fix   9
16335 ou  st      wi   fi    8
16336 Final file name: oustwifi.adb
16337 @end smallexample
16338
16339 @item
16340 The file names for all predefined units are always krunched to eight
16341 characters. The krunching of these predefined units uses the following
16342 special prefix replacements:
16343
16344 @table @file
16345 @item ada-
16346 replaced by @file{^a^A^-}
16347
16348 @item gnat-
16349 replaced by @file{^g^G^-}
16350
16351 @item interfaces-
16352 replaced by @file{^i^I^-}
16353
16354 @item system-
16355 replaced by @file{^s^S^-}
16356 @end table
16357
16358 These system files have a hyphen in the second character position. That
16359 is why normal user files replace such a character with a
16360 ^tilde^dollar sign^, to
16361 avoid confusion with system file names.
16362
16363 As an example of this special rule, consider
16364 @*@file{ada-strings-wide_fixed.adb}, which gets krunched as follows:
16365
16366 @smallexample
16367 ada-strings-wide_fixed 22
16368 a-  strings wide fixed 18
16369 a-  string  wide fixed 17
16370 a-  strin   wide fixed 16
16371 a-  stri    wide fixed 15
16372 a-  stri    wide fixe  14
16373 a-  str     wide fixe  13
16374 a-  str     wid  fixe  12
16375 a-  str     wid  fix   11
16376 a-  st      wid  fix   10
16377 a-  st      wi   fix   9
16378 a-  st      wi   fi    8
16379 Final file name: a-stwifi.adb
16380 @end smallexample
16381 @end itemize
16382
16383 Of course no file shortening algorithm can guarantee uniqueness over all
16384 possible unit names, and if file name krunching is used then it is your
16385 responsibility to ensure that no name clashes occur. The utility
16386 program @code{gnatkr} is supplied for conveniently determining the
16387 krunched name of a file.
16388
16389 @node Examples of gnatkr Usage
16390 @section Examples of @code{gnatkr} Usage
16391
16392 @smallexample
16393 @iftex
16394 @leftskip=0cm
16395 @end iftex
16396 @ifclear vms
16397 $ gnatkr very_long_unit_name.ads      --> velounna.ads
16398 $ gnatkr grandparent-parent-child.ads --> grparchi.ads
16399 $ gnatkr Grandparent.Parent.Child.ads --> grparchi.ads
16400 $ gnatkr grandparent-parent-child     --> grparchi
16401 @end ifclear
16402 $ gnatkr very_long_unit_name.ads/count=6 --> vlunna.ads
16403 $ gnatkr very_long_unit_name.ads/count=0 --> very_long_unit_name.ads
16404 @end smallexample
16405
16406 @node Preprocessing Using gnatprep
16407 @chapter Preprocessing Using @code{gnatprep}
16408 @findex gnatprep
16409
16410 @noindent
16411 The @code{gnatprep} utility provides
16412 a simple preprocessing capability for Ada programs.
16413 It is designed for use with GNAT, but is not dependent on any special
16414 features of GNAT.
16415
16416 @menu
16417 * Using gnatprep::
16418 * Switches for gnatprep::
16419 * Form of Definitions File::
16420 * Form of Input Text for gnatprep::
16421 @end menu
16422
16423 @node Using gnatprep
16424 @section Using @code{gnatprep}
16425
16426 @noindent
16427 To call @code{gnatprep} use
16428
16429 @smallexample
16430 $ gnatprep [-bcrsu] [-Dsymbol=value] infile outfile [deffile]
16431 @end smallexample
16432
16433 @noindent
16434 where
16435 @table @code
16436 @item infile
16437 is the full name of the input file, which is an Ada source
16438 file containing preprocessor directives.
16439
16440 @item outfile
16441 is the full name of the output file, which is an Ada source
16442 in standard Ada form. When used with GNAT, this file name will
16443 normally have an ads or adb suffix.
16444
16445 @item deffile
16446 is the full name of a text file containing definitions of
16447 symbols to be referenced by the preprocessor. This argument is
16448 optional, and can be replaced by the use of the @option{-D} switch.
16449
16450 @item switches
16451 is an optional sequence of switches as described in the next section.
16452 @end table
16453
16454 @node Switches for gnatprep
16455 @section Switches for @code{gnatprep}
16456
16457 @table @option
16458 @c !sort!
16459
16460 @item ^-b^/BLANK_LINES^
16461 @cindex @option{^-b^/BLANK_LINES^} (@command{gnatprep})
16462 Causes both preprocessor lines and the lines deleted by
16463 preprocessing to be replaced by blank lines in the output source file,
16464 preserving line numbers in the output file.
16465
16466 @item ^-c^/COMMENTS^
16467 @cindex @option{^-c^/COMMENTS^} (@command{gnatprep})
16468 Causes both preprocessor lines and the lines deleted
16469 by preprocessing to be retained in the output source as comments marked
16470 with the special string @code{"--! "}. This option will result in line numbers
16471 being preserved in the output file.
16472
16473 @item ^-C^/REPLACE_IN_COMMENTS^
16474 @cindex @option{^-C^/REPLACE_IN_COMMENTS^} (@command{gnatprep})
16475 Causes comments to be scanned. Normally comments are ignored by gnatprep.
16476 If this option is specified, then comments are scanned and any $symbol
16477 substitutions performed as in program text. This is particularly useful
16478 when structured comments are used (e.g. when writing programs in the
16479 SPARK dialect of Ada). Note that this switch is not available when
16480 doing integrated preprocessing (it would be useless in this context
16481 since comments are ignored by the compiler in any case).
16482
16483 @item ^-Dsymbol=value^/ASSOCIATE="symbol=value"^
16484 @cindex @option{^-D^/ASSOCIATE^} (@command{gnatprep})
16485 Defines a new symbol, associated with value. If no value is given on the
16486 command line, then symbol is considered to be @code{True}. This switch
16487 can be used in place of a definition file.
16488
16489 @ifset vms
16490 @item /REMOVE
16491 @cindex @option{/REMOVE} (@command{gnatprep})
16492 This is the default setting which causes lines deleted by preprocessing
16493 to be entirely removed from the output file.
16494 @end ifset
16495
16496 @item ^-r^/REFERENCE^
16497 @cindex @option{^-r^/REFERENCE^} (@command{gnatprep})
16498 Causes a @code{Source_Reference} pragma to be generated that
16499 references the original input file, so that error messages will use
16500 the file name of this original file. The use of this switch implies
16501 that preprocessor lines are not to be removed from the file, so its
16502 use will force @option{^-b^/BLANK_LINES^} mode if
16503 @option{^-c^/COMMENTS^}
16504 has not been specified explicitly.
16505
16506 Note that if the file to be preprocessed contains multiple units, then
16507 it will be necessary to @code{gnatchop} the output file from
16508 @code{gnatprep}. If a @code{Source_Reference} pragma is present
16509 in the preprocessed file, it will be respected by
16510 @code{gnatchop ^-r^/REFERENCE^}
16511 so that the final chopped files will correctly refer to the original
16512 input source file for @code{gnatprep}.
16513
16514 @item ^-s^/SYMBOLS^
16515 @cindex @option{^-s^/SYMBOLS^} (@command{gnatprep})
16516 Causes a sorted list of symbol names and values to be
16517 listed on the standard output file.
16518
16519 @item ^-u^/UNDEFINED^
16520 @cindex @option{^-u^/UNDEFINED^} (@command{gnatprep})
16521 Causes undefined symbols to be treated as having the value FALSE in the context
16522 of a preprocessor test. In the absence of this option, an undefined symbol in
16523 a @code{#if} or @code{#elsif} test will be treated as an error.
16524
16525 @end table
16526
16527 @ifclear vms
16528 @noindent
16529 Note: if neither @option{-b} nor @option{-c} is present,
16530 then preprocessor lines and
16531 deleted lines are completely removed from the output, unless -r is
16532 specified, in which case -b is assumed.
16533 @end ifclear
16534
16535 @node Form of Definitions File
16536 @section Form of Definitions File
16537
16538 @noindent
16539 The definitions file contains lines of the form
16540
16541 @smallexample
16542 symbol := value
16543 @end smallexample
16544
16545 @noindent
16546 where symbol is an identifier, following normal Ada (case-insensitive)
16547 rules for its syntax, and value is one of the following:
16548
16549 @itemize @bullet
16550 @item
16551 Empty, corresponding to a null substitution
16552 @item
16553 A string literal using normal Ada syntax
16554 @item
16555 Any sequence of characters from the set
16556 (letters, digits, period, underline).
16557 @end itemize
16558
16559 @noindent
16560 Comment lines may also appear in the definitions file, starting with
16561 the usual @code{--},
16562 and comments may be added to the definitions lines.
16563
16564 @node Form of Input Text for gnatprep
16565 @section Form of Input Text for @code{gnatprep}
16566
16567 @noindent
16568 The input text may contain preprocessor conditional inclusion lines,
16569 as well as general symbol substitution sequences.
16570
16571 The preprocessor conditional inclusion commands have the form
16572
16573 @smallexample
16574 @group
16575 @cartouche
16576 #if @i{expression} [then]
16577    lines
16578 #elsif @i{expression} [then]
16579    lines
16580 #elsif @i{expression} [then]
16581    lines
16582 ...
16583 #else
16584    lines
16585 #end if;
16586 @end cartouche
16587 @end group
16588 @end smallexample
16589
16590 @noindent
16591 In this example, @i{expression} is defined by the following grammar:
16592 @smallexample
16593 @i{expression} ::=  <symbol>
16594 @i{expression} ::=  <symbol> = "<value>"
16595 @i{expression} ::=  <symbol> = <symbol>
16596 @i{expression} ::=  <symbol> 'Defined
16597 @i{expression} ::=  not @i{expression}
16598 @i{expression} ::=  @i{expression} and @i{expression}
16599 @i{expression} ::=  @i{expression} or @i{expression}
16600 @i{expression} ::=  @i{expression} and then @i{expression}
16601 @i{expression} ::=  @i{expression} or else @i{expression}
16602 @i{expression} ::=  ( @i{expression} )
16603 @end smallexample
16604
16605 @noindent
16606 For the first test (@i{expression} ::= <symbol>) the symbol must have
16607 either the value true or false, that is to say the right-hand of the
16608 symbol definition must be one of the (case-insensitive) literals
16609 @code{True} or @code{False}. If the value is true, then the
16610 corresponding lines are included, and if the value is false, they are
16611 excluded.
16612
16613 The test (@i{expression} ::= <symbol> @code{'Defined}) is true only if
16614 the symbol has been defined in the definition file or by a @option{-D}
16615 switch on the command line. Otherwise, the test is false.
16616
16617 The equality tests are case insensitive, as are all the preprocessor lines.
16618
16619 If the symbol referenced is not defined in the symbol definitions file,
16620 then the effect depends on whether or not switch @option{-u}
16621 is specified. If so, then the symbol is treated as if it had the value
16622 false and the test fails. If this switch is not specified, then
16623 it is an error to reference an undefined symbol. It is also an error to
16624 reference a symbol that is defined with a value other than @code{True}
16625 or @code{False}.
16626
16627 The use of the @code{not} operator inverts the sense of this logical test, so
16628 that the lines are included only if the symbol is not defined.
16629 The @code{then} keyword is optional as shown
16630
16631 The @code{#} must be the first non-blank character on a line, but
16632 otherwise the format is free form. Spaces or tabs may appear between
16633 the @code{#} and the keyword. The keywords and the symbols are case
16634 insensitive as in normal Ada code. Comments may be used on a
16635 preprocessor line, but other than that, no other tokens may appear on a
16636 preprocessor line. Any number of @code{elsif} clauses can be present,
16637 including none at all. The @code{else} is optional, as in Ada.
16638
16639 The @code{#} marking the start of a preprocessor line must be the first
16640 non-blank character on the line, i.e. it must be preceded only by
16641 spaces or horizontal tabs.
16642
16643 Symbol substitution outside of preprocessor lines is obtained by using
16644 the sequence
16645
16646 @smallexample
16647 $symbol
16648 @end smallexample
16649
16650 @noindent
16651 anywhere within a source line, except in a comment or within a
16652 string literal. The identifier
16653 following the @code{$} must match one of the symbols defined in the symbol
16654 definition file, and the result is to substitute the value of the
16655 symbol in place of @code{$symbol} in the output file.
16656
16657 Note that although the substitution of strings within a string literal
16658 is not possible, it is possible to have a symbol whose defined value is
16659 a string literal. So instead of setting XYZ to @code{hello} and writing:
16660
16661 @smallexample
16662 Header : String := "$XYZ";
16663 @end smallexample
16664
16665 @noindent
16666 you should set XYZ to @code{"hello"} and write:
16667
16668 @smallexample
16669 Header : String := $XYZ;
16670 @end smallexample
16671
16672 @noindent
16673 and then the substitution will occur as desired.
16674
16675 @ifset vms
16676 @node The GNAT Run-Time Library Builder gnatlbr
16677 @chapter The GNAT Run-Time Library Builder @code{gnatlbr}
16678 @findex gnatlbr
16679 @cindex Library builder
16680
16681 @noindent
16682 @code{gnatlbr} is a tool for rebuilding the GNAT run time with user
16683 supplied configuration pragmas.
16684
16685 @menu
16686 * Running gnatlbr::
16687 * Switches for gnatlbr::
16688 * Examples of gnatlbr Usage::
16689 @end menu
16690
16691 @node Running gnatlbr
16692 @section Running @code{gnatlbr}
16693
16694 @noindent
16695 The @code{gnatlbr} command has the form
16696
16697 @smallexample
16698 $ GNAT LIBRARY /[CREATE | SET | DELETE]=directory [/CONFIG=file]
16699 @end smallexample
16700
16701 @node Switches for gnatlbr
16702 @section Switches for @code{gnatlbr}
16703
16704 @noindent
16705 @code{gnatlbr} recognizes the following switches:
16706
16707 @table @option
16708 @c !sort!
16709 @item /CREATE=directory
16710 @cindex @code{/CREATE} (@code{gnatlbr})
16711      Create the new run-time library in the specified directory.
16712
16713 @item /SET=directory
16714 @cindex @code{/SET} (@code{gnatlbr})
16715      Make the library in the specified directory the current run-time
16716      library.
16717
16718 @item /DELETE=directory
16719 @cindex @code{/DELETE} (@code{gnatlbr})
16720      Delete the run-time library in the specified directory.
16721
16722 @item /CONFIG=file
16723 @cindex @code{/CONFIG} (@code{gnatlbr})
16724      With /CREATE:
16725      Use the configuration pragmas in the specified file when building
16726      the library.
16727
16728      With /SET:
16729      Use the configuration pragmas in the specified file when compiling.
16730
16731 @end table
16732
16733 @node Examples of gnatlbr Usage
16734 @section Example of @code{gnatlbr} Usage
16735
16736 @smallexample
16737 Contents of VAXFLOAT.ADC:
16738 pragma Float_Representation (VAX_Float);
16739
16740 $ GNAT LIBRARY /CREATE=[.VAXFLOAT] /CONFIG=VAXFLOAT.ADC
16741
16742 GNAT LIBRARY rebuilds the run-time library in directory [.VAXFLOAT]
16743
16744 @end smallexample
16745 @end ifset
16746
16747 @node The GNAT Library Browser gnatls
16748 @chapter The GNAT Library Browser @code{gnatls}
16749 @findex gnatls
16750 @cindex Library browser
16751
16752 @noindent
16753 @code{gnatls} is a tool that outputs information about compiled
16754 units. It gives the relationship between objects, unit names and source
16755 files. It can also be used to check the source dependencies of a unit
16756 as well as various characteristics.
16757
16758 @menu
16759 * Running gnatls::
16760 * Switches for gnatls::
16761 * Examples of gnatls Usage::
16762 @end menu
16763
16764 @node Running gnatls
16765 @section Running @code{gnatls}
16766
16767 @noindent
16768 The @code{gnatls} command has the form
16769
16770 @smallexample
16771 $ gnatls switches @var{object_or_ali_file}
16772 @end smallexample
16773
16774 @noindent
16775 The main argument is the list of object or @file{ali} files
16776 (@pxref{The Ada Library Information Files})
16777 for which information is requested.
16778
16779 In normal mode, without additional option, @code{gnatls} produces a
16780 four-column listing. Each line represents information for a specific
16781 object. The first column gives the full path of the object, the second
16782 column gives the name of the principal unit in this object, the third
16783 column gives the status of the source and the fourth column gives the
16784 full path of the source representing this unit.
16785 Here is a simple example of use:
16786
16787 @smallexample
16788 $ gnatls *.o
16789 ^./^[]^demo1.o            demo1            DIF demo1.adb
16790 ^./^[]^demo2.o            demo2             OK demo2.adb
16791 ^./^[]^hello.o            h1                OK hello.adb
16792 ^./^[]^instr-child.o      instr.child      MOK instr-child.adb
16793 ^./^[]^instr.o            instr             OK instr.adb
16794 ^./^[]^tef.o              tef              DIF tef.adb
16795 ^./^[]^text_io_example.o  text_io_example   OK text_io_example.adb
16796 ^./^[]^tgef.o             tgef             DIF tgef.adb
16797 @end smallexample
16798
16799 @noindent
16800 The first line can be interpreted as follows: the main unit which is
16801 contained in
16802 object file @file{demo1.o} is demo1, whose main source is in
16803 @file{demo1.adb}. Furthermore, the version of the source used for the
16804 compilation of demo1 has been modified (DIF). Each source file has a status
16805 qualifier which can be:
16806
16807 @table @code
16808 @item OK (unchanged)
16809 The version of the source file used for the compilation of the
16810 specified unit corresponds exactly to the actual source file.
16811
16812 @item MOK (slightly modified)
16813 The version of the source file used for the compilation of the
16814 specified unit differs from the actual source file but not enough to
16815 require recompilation. If you use gnatmake with the qualifier
16816 @option{^-m (minimal recompilation)^/MINIMAL_RECOMPILATION^}, a file marked
16817 MOK will not be recompiled.
16818
16819 @item DIF (modified)
16820 No version of the source found on the path corresponds to the source
16821 used to build this object.
16822
16823 @item ??? (file not found)
16824 No source file was found for this unit.
16825
16826 @item HID (hidden,  unchanged version not first on PATH)
16827 The version of the source that corresponds exactly to the source used
16828 for compilation has been found on the path but it is hidden by another
16829 version of the same source that has been modified.
16830
16831 @end table
16832
16833 @node Switches for gnatls
16834 @section Switches for @code{gnatls}
16835
16836 @noindent
16837 @code{gnatls} recognizes the following switches:
16838
16839 @table @option
16840 @c !sort!
16841 @item ^-a^/ALL_UNITS^
16842 @cindex @option{^-a^/ALL_UNITS^} (@code{gnatls})
16843 Consider all units, including those of the predefined Ada library.
16844 Especially useful with @option{^-d^/DEPENDENCIES^}.
16845
16846 @item ^-d^/DEPENDENCIES^
16847 @cindex @option{^-d^/DEPENDENCIES^} (@code{gnatls})
16848 List sources from which specified units depend on.
16849
16850 @item ^-h^/OUTPUT=OPTIONS^
16851 @cindex @option{^-h^/OUTPUT=OPTIONS^} (@code{gnatls})
16852 Output the list of options.
16853
16854 @item ^-o^/OUTPUT=OBJECTS^
16855 @cindex @option{^-o^/OUTPUT=OBJECTS^} (@code{gnatls})
16856 Only output information about object files.
16857
16858 @item ^-s^/OUTPUT=SOURCES^
16859 @cindex @option{^-s^/OUTPUT=SOURCES^} (@code{gnatls})
16860 Only output information about source files.
16861
16862 @item ^-u^/OUTPUT=UNITS^
16863 @cindex @option{^-u^/OUTPUT=UNITS^} (@code{gnatls})
16864 Only output information about compilation units.
16865
16866 @item ^-files^/FILES^=@var{file}
16867 @cindex @option{^-files^/FILES^} (@code{gnatls})
16868 Take as arguments the files listed in text file @var{file}.
16869 Text file @var{file} may contain empty lines that are ignored.
16870 Each non empty line should contain the name of an existing file.
16871 Several such switches may be specified simultaneously.
16872
16873 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
16874 @itemx ^-aI^/SOURCE_SEARCH=^@var{dir}
16875 @itemx ^-I^/SEARCH=^@var{dir}
16876 @itemx  ^-I-^/NOCURRENT_DIRECTORY^
16877 @itemx -nostdinc
16878 @cindex @option{^-aO^/OBJECT_SEARCH^} (@code{gnatls})
16879 @cindex @option{^-aI^/SOURCE_SEARCH^} (@code{gnatls})
16880 @cindex @option{^-I^/SEARCH^} (@code{gnatls})
16881 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatls})
16882 Source path manipulation. Same meaning as the equivalent @command{gnatmake}
16883 flags (@pxref{Switches for gnatmake}).
16884
16885 @item --RTS=@var{rts-path}
16886 @cindex @option{--RTS} (@code{gnatls})
16887 Specifies the default location of the runtime library. Same meaning as the
16888 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
16889
16890 @item ^-v^/OUTPUT=VERBOSE^
16891 @cindex @option{^-v^/OUTPUT=VERBOSE^} (@code{gnatls})
16892 Verbose mode. Output the complete source, object and project paths. Do not use
16893 the default column layout but instead use long format giving as much as
16894 information possible on each requested units, including special
16895 characteristics such as:
16896
16897 @table @code
16898 @item  Preelaborable
16899 The unit is preelaborable in the Ada 95 sense.
16900
16901 @item No_Elab_Code
16902 No elaboration code has been produced by the compiler for this unit.
16903
16904 @item Pure
16905 The unit is pure in the Ada 95 sense.
16906
16907 @item Elaborate_Body
16908 The unit contains a pragma Elaborate_Body.
16909
16910 @item Remote_Types
16911 The unit contains a pragma Remote_Types.
16912
16913 @item Shared_Passive
16914 The unit contains a pragma Shared_Passive.
16915
16916 @item Predefined
16917 This unit is part of the predefined environment and cannot be modified
16918 by the user.
16919
16920 @item Remote_Call_Interface
16921 The unit contains a pragma Remote_Call_Interface.
16922
16923 @end table
16924
16925 @end table
16926
16927 @node Examples of gnatls Usage
16928 @section Example of @code{gnatls} Usage
16929 @ifclear vms
16930
16931 @noindent
16932 Example of using the verbose switch. Note how the source and
16933 object paths are affected by the -I switch.
16934
16935 @smallexample
16936 $ gnatls -v -I.. demo1.o
16937
16938 GNATLS 5.03w (20041123-34)
16939 Copyright 1997-2004 Free Software Foundation, Inc.
16940
16941 Source Search Path:
16942    <Current_Directory>
16943    ../
16944    /home/comar/local/adainclude/
16945
16946 Object Search Path:
16947    <Current_Directory>
16948    ../
16949    /home/comar/local/lib/gcc-lib/x86-linux/3.4.3/adalib/
16950
16951 Project Search Path:
16952    <Current_Directory>
16953    /home/comar/local/lib/gnat/
16954
16955 ./demo1.o
16956    Unit =>
16957      Name   => demo1
16958      Kind   => subprogram body
16959      Flags  => No_Elab_Code
16960      Source => demo1.adb    modified
16961 @end smallexample
16962
16963 @noindent
16964 The following is an example of use of the dependency list.
16965 Note the use of the -s switch
16966 which gives a straight list of source files. This can be useful for
16967 building specialized scripts.
16968
16969 @smallexample
16970 $ gnatls -d demo2.o
16971 ./demo2.o   demo2        OK demo2.adb
16972                          OK gen_list.ads
16973                          OK gen_list.adb
16974                          OK instr.ads
16975                          OK instr-child.ads
16976
16977 $ gnatls -d -s -a demo1.o
16978 demo1.adb
16979 /home/comar/local/adainclude/ada.ads
16980 /home/comar/local/adainclude/a-finali.ads
16981 /home/comar/local/adainclude/a-filico.ads
16982 /home/comar/local/adainclude/a-stream.ads
16983 /home/comar/local/adainclude/a-tags.ads
16984 gen_list.ads
16985 gen_list.adb
16986 /home/comar/local/adainclude/gnat.ads
16987 /home/comar/local/adainclude/g-io.ads
16988 instr.ads
16989 /home/comar/local/adainclude/system.ads
16990 /home/comar/local/adainclude/s-exctab.ads
16991 /home/comar/local/adainclude/s-finimp.ads
16992 /home/comar/local/adainclude/s-finroo.ads
16993 /home/comar/local/adainclude/s-secsta.ads
16994 /home/comar/local/adainclude/s-stalib.ads
16995 /home/comar/local/adainclude/s-stoele.ads
16996 /home/comar/local/adainclude/s-stratt.ads
16997 /home/comar/local/adainclude/s-tasoli.ads
16998 /home/comar/local/adainclude/s-unstyp.ads
16999 /home/comar/local/adainclude/unchconv.ads
17000 @end smallexample
17001 @end ifclear
17002
17003 @ifset vms
17004 @smallexample
17005 GNAT LIST /DEPENDENCIES /OUTPUT=SOURCES /ALL_UNITS DEMO1.ADB
17006
17007 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]ada.ads
17008 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-finali.ads
17009 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-filico.ads
17010 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-stream.ads
17011 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-tags.ads
17012 demo1.adb
17013 gen_list.ads
17014 gen_list.adb
17015 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]gnat.ads
17016 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]g-io.ads
17017 instr.ads
17018 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]system.ads
17019 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-exctab.ads
17020 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-finimp.ads
17021 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-finroo.ads
17022 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-secsta.ads
17023 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stalib.ads
17024 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stoele.ads
17025 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stratt.ads
17026 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-tasoli.ads
17027 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-unstyp.ads
17028 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]unchconv.ads
17029 @end smallexample
17030 @end ifset
17031
17032 @node Cleaning Up Using gnatclean
17033 @chapter Cleaning Up Using @code{gnatclean}
17034 @findex gnatclean
17035 @cindex Cleaning tool
17036
17037 @noindent
17038 @code{gnatclean} is a tool that allows the deletion of files produced by the
17039 compiler, binder and linker, including ALI files, object files, tree files,
17040 expanded source files, library files, interface copy source files, binder
17041 generated files and executable files.
17042
17043 @menu
17044 * Running gnatclean::
17045 * Switches for gnatclean::
17046 @c * Examples of gnatclean Usage::
17047 @end menu
17048
17049 @node Running gnatclean
17050 @section Running @code{gnatclean}
17051
17052 @noindent
17053 The @code{gnatclean} command has the form:
17054
17055 @smallexample
17056 $ gnatclean switches @var{names}
17057 @end smallexample
17058
17059 @noindent
17060 @var{names} is a list of source file names. Suffixes @code{.^ads^ADS^} and
17061 @code{^adb^ADB^} may be omitted. If a project file is specified using switch
17062 @code{^-P^/PROJECT_FILE=^}, then @var{names} may be completely omitted.
17063
17064 @noindent
17065 In normal mode, @code{gnatclean} delete the files produced by the compiler and,
17066 if switch @code{^-c^/COMPILER_FILES_ONLY^} is not specified, by the binder and
17067 the linker. In informative-only mode, specified by switch
17068 @code{^-n^/NODELETE^}, the list of files that would have been deleted in
17069 normal mode is listed, but no file is actually deleted.
17070
17071 @node Switches for gnatclean
17072 @section Switches for @code{gnatclean}
17073
17074 @noindent
17075 @code{gnatclean} recognizes the following switches:
17076
17077 @table @option
17078 @c !sort!
17079 @item ^-c^/COMPILER_FILES_ONLY^
17080 @cindex @option{^-c^/COMPILER_FILES_ONLY^} (@code{gnatclean})
17081 Only attempt to delete the files produced by the compiler, not those produced
17082 by the binder or the linker. The files that are not to be deleted are library
17083 files, interface copy files, binder generated files and executable files.
17084
17085 @item ^-D ^/DIRECTORY_OBJECTS=^@var{dir}
17086 @cindex @option{^-D^/DIRECTORY_OBJECTS^} (@code{gnatclean})
17087 Indicate that ALI and object files should normally be found in directory
17088 @var{dir}.
17089
17090 @item ^-F^/FULL_PATH_IN_BRIEF_MESSAGES^
17091 @cindex @option{^-F^/FULL_PATH_IN_BRIEF_MESSAGES^} (@code{gnatclean})
17092 When using project files, if some errors or warnings are detected during
17093 parsing and verbose mode is not in effect (no use of switch
17094 ^-v^/VERBOSE^), then error lines start with the full path name of the project
17095 file, rather than its simple file name.
17096
17097 @item ^-h^/HELP^
17098 @cindex @option{^-h^/HELP^} (@code{gnatclean})
17099 Output a message explaining the usage of @code{^gnatclean^gnatclean^}.
17100
17101 @item ^-n^/NODELETE^
17102 @cindex @option{^-n^/NODELETE^} (@code{gnatclean})
17103 Informative-only mode. Do not delete any files. Output the list of the files
17104 that would have been deleted if this switch was not specified.
17105
17106 @item ^-P^/PROJECT_FILE=^@var{project}
17107 @cindex @option{^-P^/PROJECT_FILE^} (@code{gnatclean})
17108 Use project file @var{project}. Only one such switch can be used.
17109 When cleaning a project file, the files produced by the compilation of the
17110 immediate sources or inherited sources of the project files are to be
17111 deleted. This is not depending on the presence or not of executable names
17112 on the command line.
17113
17114 @item ^-q^/QUIET^
17115 @cindex @option{^-q^/QUIET^} (@code{gnatclean})
17116 Quiet output. If there are no error, do not ouuput anything, except in
17117 verbose mode (switch ^-v^/VERBOSE^) or in informative-only mode
17118 (switch ^-n^/NODELETE^).
17119
17120 @item ^-r^/RECURSIVE^
17121 @cindex @option{^-r^/RECURSIVE^} (@code{gnatclean})
17122 When a project file is specified (using switch ^-P^/PROJECT_FILE=^),
17123 clean all imported and extended project files, recursively. If this switch
17124 is not specified, only the files related to the main project file are to be
17125 deleted. This switch has no effect if no project file is specified.
17126
17127 @item ^-v^/VERBOSE^
17128 @cindex @option{^-v^/VERBOSE^} (@code{gnatclean})
17129 Verbose mode.
17130
17131 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
17132 @cindex @option{^-vP^/MESSAGES_PROJECT_FILE^} (@code{gnatclean})
17133 Indicates the verbosity of the parsing of GNAT project files.
17134 @xref{Switches Related to Project Files}.
17135
17136 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
17137 @cindex @option{^-X^/EXTERNAL_REFERENCE^} (@code{gnatclean})
17138 Indicates that external variable @var{name} has the value @var{value}.
17139 The Project Manager will use this value for occurrences of
17140 @code{external(name)} when parsing the project file.
17141 @xref{Switches Related to Project Files}.
17142
17143 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
17144 @cindex @option{^-aO^/OBJECT_SEARCH^} (@code{gnatclean})
17145 When searching for ALI and object files, look in directory
17146 @var{dir}.
17147
17148 @item ^-I^/SEARCH=^@var{dir}
17149 @cindex @option{^-I^/SEARCH^} (@code{gnatclean})
17150 Equivalent to @option{^-aO^/OBJECT_SEARCH=^@var{dir}}.
17151
17152 @item ^-I-^/NOCURRENT_DIRECTORY^
17153 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatclean})
17154 @cindex Source files, suppressing search
17155 Do not look for ALI or object files in the directory
17156 where @code{gnatclean} was invoked.
17157
17158 @end table
17159
17160 @c @node Examples of gnatclean Usage
17161 @c @section Examples of @code{gnatclean} Usage
17162
17163 @ifclear vms
17164 @node GNAT and Libraries
17165 @chapter GNAT and Libraries
17166 @cindex Library, building, installing, using
17167
17168 @noindent
17169 This chapter describes how to build and use libraries with GNAT, and also shows
17170 how to recompile the GNAT run-time library. You should be familiar with the
17171 Project Manager facility (@pxref{GNAT Project Manager}) before reading this
17172 chapter.
17173
17174 @menu
17175 * Introduction to Libraries in GNAT::
17176 * General Ada Libraries::
17177 * Stand-alone Ada Libraries::
17178 * Rebuilding the GNAT Run-Time Library::
17179 @end menu
17180
17181 @node Introduction to Libraries in GNAT
17182 @section Introduction to Libraries in GNAT
17183
17184 @noindent
17185 A library is, conceptually, a collection of objects which does not have its
17186 own main thread of execution, but rather provides certain services to the
17187 applications that use it. A library can be either statically linked with the
17188 application, in which case its code is directly included in the application,
17189 or, on platforms that support it, be dynamically linked, in which case
17190 its code is shared by all applications making use of this library.
17191
17192 GNAT supports both types of libraries.
17193 In the static case, the compiled code can be provided in different ways. The
17194 simplest approach is to provide directly the set of objects resulting from
17195 compilation of the library source files. Alternatively, you can group the
17196 objects into an archive using whatever commands are provided by the operating
17197 system. For the latter case, the objects are grouped into a shared library.
17198
17199 In the GNAT environment, a library has three types of components:
17200 @itemize @bullet
17201 @item
17202 Source files.
17203 @item
17204 @file{ALI} files.
17205 @xref{The Ada Library Information Files}.
17206 @item
17207 Object files, an archive or a shared library.
17208 @end itemize
17209
17210 @noindent
17211 A GNAT library may expose all its source files, which is useful for
17212 documentation purposes. Alternatively, it may expose only the units needed by
17213 an external user to make use of the library. That is to say, the specs
17214 reflecting the library services along with all the units needed to compile
17215 those specs, which can include generic bodies or any body implementing an
17216 inlined routine. In the case of @emph{stand-alone libraries} those exposed
17217 units are called @emph{interface units} (@pxref{Stand-alone Ada Libraries}).
17218
17219 All compilation units comprising an application, including those in a library,
17220 need to be elaborated in an order partially defined by Ada's semantics. GNAT
17221 computes the elaboration order from the @file{ALI} files and this is why they
17222 constitute a mandatory part of GNAT libraries. Except in the case of
17223 @emph{stand-alone libraries}, where a specific library elaboration routine is
17224 produced independently of the application(s) using the library.
17225
17226 @node General Ada Libraries
17227 @section General Ada Libraries
17228
17229 @menu
17230 * Building a library::
17231 * Installing a library::
17232 * Using a library::
17233 @end menu
17234
17235 @node Building a library
17236 @subsection Building a library
17237
17238 @noindent
17239 The easiest way to build a library is to use the Project Manager,
17240 which supports a special type of project called a @emph{Library Project}
17241 (@pxref{Library Projects}).
17242
17243 A project is considered a library project, when two project-level attributes
17244 are defined in it: @code{Library_Name} and @code{Library_Dir}. In order to
17245 control different aspects of library configuration, additional optional
17246 project-level attributes can be specified:
17247 @table @code
17248 @item Library_Kind
17249 This attribute controls whether the library is to be static or dynamic
17250
17251 @item Library_Version
17252 This attribute specifies the library version; this value is used
17253 during dynamic linking of shared libraries to determine if the currently
17254 installed versions of the binaries are compatible.
17255
17256 @item Library_Options
17257 @item Library_GCC
17258 These attributes specify additional low-level options to be used during
17259 library generation, and redefine the actual application used to generate
17260 library.
17261 @end table
17262
17263 @noindent
17264 The GNAT Project Manager takes full care of the library maintenance task,
17265 including recompilation of the source files for which objects do not exist
17266 or are not up to date, assembly of the library archive, and installation of
17267 the library (i.e., copying associated source, object and @file{ALI} files
17268 to the specified location).
17269
17270 Here is a simple library project file:
17271 @smallexample @c ada
17272 project My_Lib is
17273    for Source_Dirs use ("src1", "src2");
17274    for Object_Dir use "obj";
17275    for Library_Name use "mylib";
17276    for Library_Dir use "lib";
17277    for Library_Kind use "dynamic";
17278 end My_lib;
17279 @end smallexample
17280
17281 @noindent
17282 and the compilation command to build and install the library:
17283
17284 @smallexample @c ada
17285   $ gnatmake -Pmy_lib
17286 @end smallexample
17287
17288 @noindent
17289 It is not entirely trivial to perform manually all the steps required to
17290 produce a library. We recommend that you use the GNAT Project Manager
17291 for this task. In special cases where this is not desired, the necessary
17292 steps are discussed below.
17293
17294 There are various possibilities for compiling the units that make up the
17295 library: for example with a Makefile (@pxref{Using the GNU make Utility}) or
17296 with a conventional script. For simple libraries, it is also possible to create
17297 a dummy main program which depends upon all the packages that comprise the
17298 interface of the library. This dummy main program can then be given to
17299 @command{gnatmake}, which will ensure that all necessary objects are built.
17300
17301 After this task is accomplished, you should follow the standard procedure
17302 of the underlying operating system to produce the static or shared library.
17303
17304 Here is an example of such a dummy program:
17305 @smallexample @c ada
17306 @group
17307 with My_Lib.Service1;
17308 with My_Lib.Service2;
17309 with My_Lib.Service3;
17310 procedure My_Lib_Dummy is
17311 begin
17312    null;
17313 end;
17314 @end group
17315 @end smallexample
17316
17317 @noindent
17318 Here are the generic commands that will build an archive or a shared library.
17319
17320 @smallexample
17321 # compiling the library
17322 $ gnatmake -c my_lib_dummy.adb
17323
17324 # we don't need the dummy object itself
17325 $ rm my_lib_dummy.o my_lib_dummy.ali
17326
17327 # create an archive with the remaining objects
17328 $ ar rc libmy_lib.a *.o
17329 # some systems may require "ranlib" to be run as well
17330
17331 # or create a shared library
17332 $ gcc -shared -o libmy_lib.so *.o
17333 # some systems may require the code to have been compiled with -fPIC
17334
17335 # remove the object files that are now in the library
17336 $ rm *.o
17337
17338 # Make the ALI files read-only so that gnatmake will not try to
17339 # regenerate the objects that are in the library
17340 $ chmod -w *.ali
17341 @end smallexample
17342
17343 @noindent
17344 Please note that the library must have a name of the form @file{libxxx.a} or
17345 @file{libxxx.so} (or @file{libxxx.dll} on Windows) in order to be accessed by
17346 the directive @option{-lxxx} at link time.
17347
17348 @node Installing a library
17349 @subsection Installing a library
17350 @cindex @code{ADA_PROJECT_PATH}
17351
17352 @noindent
17353 If you use project files, library installation is part of the library build
17354 process. Thus no further action is needed in order to make use of the
17355 libraries that are built as part of the general application build. A usable
17356 version of the library is installed in the directory specified by the
17357 @code{Library_Dir} attribute of the library project file.
17358
17359 You may want to install a library in a context different from where the library
17360 is built. This situation arises with third party suppliers, who may want
17361 to distribute a library in binary form where the user is not expected to be
17362 able to recompile the library. The simplest option in this case is to provide
17363 a project file slightly different from the one used to build the library, by
17364 using the @code{externally_built} attribute. For instance, the project
17365 file used to build the library in the previous section can be changed into the
17366 following one when the library is installed:
17367
17368 @smallexample @c projectfile
17369 project My_Lib is
17370    for Source_Dirs use ("src1", "src2");
17371    for Library_Name use "mylib";
17372    for Library_Dir use "lib";
17373    for Library_Kind use "dynamic";
17374    for Externally_Built use "true";
17375 end My_lib;
17376 @end smallexample
17377
17378 @noindent
17379 This project file assumes that the directories @file{src1},
17380 @file{src2}, and @file{lib} exist in
17381 the directory containing the project file. The @code{externally_built}
17382 attribute makes it clear to the GNAT builder that it should not attempt to
17383 recompile any of the units from this library. It allows the library provider to
17384 restrict the source set to the minimum necessary for clients to make use of the
17385 library as described in the first section of this chapter. It is the
17386 responsibility of the library provider to install the necessary sources, ALI
17387 files and libraries in the directories mentioned in the project file. For
17388 convenience, the user's library project file should be installed in a location
17389 that will be searched automatically by the GNAT
17390 builder. These are the directories referenced in the @code{ADA_PROJECT_PATH}
17391 environment variable (@pxref{Importing Projects}), and also the default GNAT
17392 library location that can be queried with @command{gnatls -v} and is usually of
17393 the form $gnat_install_root/lib/gnat.
17394
17395 When project files are not an option, it is also possible, but not recommended,
17396 to install the library so that the sources needed to use the library are on the
17397 Ada source path and the ALI files & libraries be on the Ada Object path (see
17398 @ref{Search Paths and the Run-Time Library (RTL)}. Alternatively, the system
17399 administrator can place general-purpose libraries in the default compiler
17400 paths, by specifying the libraries' location in the configuration files
17401 @file{ada_source_path} and @file{ada_object_path}. These configuration files
17402 must be located in the GNAT installation tree at the same place as the gcc spec
17403 file. The location of the gcc spec file can be determined as follows:
17404 @smallexample
17405 $ gcc -v
17406 @end smallexample
17407
17408 @noindent
17409 The configuration files mentioned above have a simple format: each line
17410 must contain one unique directory name.
17411 Those names are added to the corresponding path
17412 in their order of appearance in the file. The names can be either absolute
17413 or relative; in the latter case, they are relative to where theses files
17414 are located.
17415
17416 The files @file{ada_source_path} and @file{ada_object_path} might not be
17417 present in a
17418 GNAT installation, in which case, GNAT will look for its run-time library in
17419 the directories @file{adainclude} (for the sources) and @file{adalib} (for the
17420 objects and @file{ALI} files). When the files exist, the compiler does not
17421 look in @file{adainclude} and @file{adalib}, and thus the
17422 @file{ada_source_path} file
17423 must contain the location for the GNAT run-time sources (which can simply
17424 be @file{adainclude}). In the same way, the @file{ada_object_path} file must
17425 contain the location for the GNAT run-time objects (which can simply
17426 be @file{adalib}).
17427
17428 You can also specify a new default path to the run-time library at compilation
17429 time with the switch @option{--RTS=rts-path}. You can thus choose / change
17430 the run-time library you want your program to be compiled with. This switch is
17431 recognized by @command{gcc}, @command{gnatmake}, @command{gnatbind},
17432 @command{gnatls}, @command{gnatfind} and @command{gnatxref}.
17433
17434 It is possible to install a library before or after the standard GNAT
17435 library, by reordering the lines in the configuration files. In general, a
17436 library must be installed before the GNAT library if it redefines
17437 any part of it.
17438
17439 @node Using a library
17440 @subsection Using a library
17441
17442 @noindent Once again, the project facility greatly simplifies the use of
17443 libraries. In this context, using a library is just a matter of adding a
17444 @code{with} clause in the user project. For instance, to make use of the
17445 library @code{My_Lib} shown in examples in earlier sections, you can
17446 write:
17447
17448 @smallexample @c projectfile
17449 with "my_lib";
17450 project My_Proj is
17451   ...
17452 end My_Proj;
17453 @end smallexample
17454
17455 Even if you have a third-party, non-Ada library, you can still use GNAT's
17456 Project Manager facility to provide a wrapper for it. For example, the
17457 following project, when @code{with}ed by your main project, will link with the
17458 third-party library @file{liba.a}:
17459
17460 @smallexample @c projectfile
17461 @group
17462 project Liba is
17463    for Externally_Built use "true";
17464    for Library_Dir use "lib";
17465    for Library_Name use "a";
17466    for Library_Kind use "static";
17467 end Liba;
17468 @end group
17469 @end smallexample
17470 This is an alternative to the use of @code{pragma Linker_Options}. It is
17471 especially interesting in the context of systems with several interdependant
17472 static libraries where finding a proper linker order is not easy and best be
17473 left to the tools having visibility over project dependancy information.
17474
17475 @noindent
17476 In order to use an Ada library manually, you need to make sure that this
17477 library is on both your source and object path
17478 (see @ref{Search Paths and the Run-Time Library (RTL)}
17479 and @ref{Search Paths for gnatbind}). Furthermore, when the objects are grouped
17480 in an archive or a shared library, you need to specify the desired
17481 library at link time.
17482
17483 For example, you can use the library @file{mylib} installed in
17484 @file{/dir/my_lib_src} and @file{/dir/my_lib_obj} with the following commands:
17485
17486 @smallexample
17487 $ gnatmake -aI/dir/my_lib_src -aO/dir/my_lib_obj my_appl \
17488   -largs -lmy_lib
17489 @end smallexample
17490
17491 @noindent
17492 This can be expressed more simply:
17493 @smallexample
17494 $ gnatmake my_appl
17495 @end smallexample
17496 @noindent
17497 when the following conditions are met:
17498 @itemize @bullet
17499 @item
17500 @file{/dir/my_lib_src} has been added by the user to the environment
17501 variable @code{ADA_INCLUDE_PATH}, or by the administrator to the file
17502 @file{ada_source_path}
17503 @item
17504 @file{/dir/my_lib_obj} has been added by the user to the environment
17505 variable @code{ADA_OBJECTS_PATH}, or by the administrator to the file
17506 @file{ada_object_path}
17507 @item
17508 a pragma @code{Linker_Options} has been added to one of the sources.
17509 For example:
17510
17511 @smallexample @c ada
17512 pragma Linker_Options ("-lmy_lib");
17513 @end smallexample
17514 @end itemize
17515
17516 @node Stand-alone Ada Libraries
17517 @section Stand-alone Ada Libraries
17518 @cindex Stand-alone library, building, using
17519
17520 @menu
17521 * Introduction to Stand-alone Libraries::
17522 * Building a Stand-alone Library::
17523 * Creating a Stand-alone Library to be used in a non-Ada context::
17524 * Restrictions in Stand-alone Libraries::
17525 @end menu
17526
17527 @node Introduction to Stand-alone Libraries
17528 @subsection Introduction to Stand-alone Libraries
17529
17530 @noindent
17531 A Stand-alone Library (abbreviated ``SAL'') is a library that contains the
17532 necessary code to
17533 elaborate the Ada units that are included in the library. In contrast with
17534 an ordinary library, which consists of all sources, objects and @file{ALI}
17535 files of the
17536 library, a SAL may specify a restricted subset of compilation units
17537 to serve as a library interface. In this case, the fully
17538 self-sufficient set of files will normally consist of an objects
17539 archive, the sources of interface units' specs, and the @file{ALI}
17540 files of interface units.
17541 If an interface spec contains a generic unit or an inlined subprogram,
17542 the body's
17543 source must also be provided; if the units that must be provided in the source
17544 form depend on other units, the source and @file{ALI} files of those must
17545 also be provided.
17546
17547 The main purpose of a SAL is to minimize the recompilation overhead of client
17548 applications when a new version of the library is installed. Specifically,
17549 if the interface sources have not changed, client applications do not need to
17550 be recompiled. If, furthermore, a SAL is provided in the shared form and its
17551 version, controlled by @code{Library_Version} attribute, is not changed,
17552 then the clients do not need to be relinked.
17553
17554 SALs also allow the library providers to minimize the amount of library source
17555 text exposed to the clients.  Such ``information hiding'' might be useful or
17556 necessary for various reasons.
17557
17558 Stand-alone libraries are also well suited to be used in an executable whose
17559 main routine is not written in Ada.
17560
17561 @node Building a Stand-alone Library
17562 @subsection Building a Stand-alone Library
17563
17564 @noindent
17565 GNAT's Project facility provides a simple way of building and installing
17566 stand-alone libraries; see @ref{Stand-alone Library Projects}.
17567 To be a Stand-alone Library Project, in addition to the two attributes
17568 that make a project a Library Project (@code{Library_Name} and
17569 @code{Library_Dir}; see @ref{Library Projects}), the attribute
17570 @code{Library_Interface} must be defined.  For example:
17571
17572 @smallexample @c projectfile
17573 @group
17574    for Library_Dir use "lib_dir";
17575    for Library_Name use "dummy";
17576    for Library_Interface use ("int1", "int1.child");
17577 @end group
17578 @end smallexample
17579
17580 @noindent
17581 Attribute @code{Library_Interface} has a non-empty string list value,
17582 each string in the list designating a unit contained in an immediate source
17583 of the project file.
17584
17585 When a Stand-alone Library is built, first the binder is invoked to build
17586 a package whose name depends on the library name
17587 (@file{^b~dummy.ads/b^B$DUMMY.ADS/B^} in the example above).
17588 This binder-generated package includes initialization and
17589 finalization procedures whose
17590 names depend on the library name (@code{dummyinit} and @code{dummyfinal}
17591 in the example
17592 above). The object corresponding to this package is included in the library.
17593
17594 You must ensure timely (e.g., prior to any use of interfaces in the SAL)
17595 calling of these procedures if a static SAL is built, or if a shared SAL
17596 is built
17597 with the project-level attribute @code{Library_Auto_Init} set to
17598 @code{"false"}.
17599
17600 For a Stand-Alone Library, only the @file{ALI} files of the Interface Units
17601 (those that are listed in attribute @code{Library_Interface}) are copied to
17602 the Library Directory. As a consequence, only the Interface Units may be
17603 imported from Ada units outside of the library. If other units are imported,
17604 the binding phase will fail.
17605
17606 The attribute @code{Library_Src_Dir} may be specified for a
17607 Stand-Alone Library. @code{Library_Src_Dir} is a simple attribute that has a
17608 single string value. Its value must be the path (absolute or relative to the
17609 project directory) of an existing directory. This directory cannot be the
17610 object directory or one of the source directories, but it can be the same as
17611 the library directory. The sources of the Interface
17612 Units of the library that are needed by an Ada client of the library will be
17613 copied to the designated directory, called the Interface Copy directory.
17614 These sources include the specs of the Interface Units, but they may also
17615 include bodies and subunits, when pragmas @code{Inline} or @code{Inline_Always}
17616 are used, or when there is a generic unit in the spec. Before the sources
17617 are copied to the Interface Copy directory, an attempt is made to delete all
17618 files in the Interface Copy directory.
17619
17620 Building stand-alone libraries by hand is somewhat tedious, but for those
17621 occasions when it is necessary here are the steps that you need to perform:
17622 @itemize @bullet
17623 @item
17624 Compile all library sources.
17625
17626 @item
17627 Invoke the binder with the switch @option{-n} (No Ada main program),
17628 with all the @file{ALI} files of the interfaces, and
17629 with the switch @option{-L} to give specific names to the @code{init}
17630 and @code{final} procedures.  For example:
17631 @smallexample
17632   gnatbind -n int1.ali int2.ali -Lsal1
17633 @end smallexample
17634
17635 @item
17636 Compile the binder generated file:
17637 @smallexample
17638   gcc -c b~int2.adb
17639 @end smallexample
17640
17641 @item
17642 Link the dynamic library with all the necessary object files,
17643 indicating to the linker the names of the @code{init} (and possibly
17644 @code{final}) procedures for automatic initialization (and finalization).
17645 The built library should be placed in a directory different from
17646 the object directory.
17647
17648 @item
17649 Copy the @code{ALI} files of the interface to the library directory,
17650 add in this copy an indication that it is an interface to a SAL
17651 (i.e. add a word @option{SL} on the line in the @file{ALI} file that starts
17652 with letter ``P'') and make the modified copy of the @file{ALI} file
17653 read-only.
17654 @end itemize
17655
17656 @noindent
17657 Using SALs is not different from using other libraries
17658 (see @ref{Using a library}).
17659
17660 @node Creating a Stand-alone Library to be used in a non-Ada context
17661 @subsection Creating a Stand-alone Library to be used in a non-Ada context
17662
17663 @noindent
17664 It is easy to adapt the SAL build procedure discussed above for use of a SAL in
17665 a non-Ada context.
17666
17667 The only extra step required is to ensure that library interface subprograms
17668 are compatible with the main program, by means of @code{pragma Export}
17669 or @code{pragma Convention}.
17670
17671 Here is an example of simple library interface for use with C main program:
17672
17673 @smallexample @c ada
17674 package Interface is
17675
17676    procedure Do_Something;
17677    pragma Export (C, Do_Something, "do_something");
17678
17679    procedure Do_Something_Else;
17680    pragma Export (C, Do_Something_Else, "do_something_else");
17681
17682 end Interface;
17683 @end smallexample
17684
17685 @noindent
17686 On the foreign language side, you must provide a ``foreign'' view of the
17687 library interface; remember that it should contain elaboration routines in
17688 addition to interface subprograms.
17689
17690 The example below shows the content of @code{mylib_interface.h} (note
17691 that there is no rule for the naming of this file, any name can be used)
17692 @smallexample
17693 /* the library elaboration procedure */
17694 extern void mylibinit (void);
17695
17696 /* the library finalization procedure */
17697 extern void mylibfinal (void);
17698
17699 /* the interface exported by the library */
17700 extern void do_something (void);
17701 extern void do_something_else (void);
17702 @end smallexample
17703
17704 @noindent
17705 Libraries built as explained above can be used from any program, provided
17706 that the elaboration procedures (named @code{mylibinit} in the previous
17707 example) are called before the library services are used. Any number of
17708 libraries can be used simultaneously, as long as the elaboration
17709 procedure of each library is called.
17710
17711 Below is an example of a C program that uses the @code{mylib} library.
17712
17713 @smallexample
17714 #include "mylib_interface.h"
17715
17716 int
17717 main (void)
17718 @{
17719    /* First, elaborate the library before using it */
17720    mylibinit ();
17721
17722    /* Main program, using the library exported entities */
17723    do_something ();
17724    do_something_else ();
17725
17726    /* Library finalization at the end of the program */
17727    mylibfinal ();
17728    return 0;
17729 @}
17730 @end smallexample
17731
17732 @noindent
17733 Note that invoking any library finalization procedure generated by
17734 @code{gnatbind} shuts down the Ada run-time environment.
17735 Consequently, the
17736 finalization of all Ada libraries must be performed at the end of the program.
17737 No call to these libraries or to the Ada run-time library should be made
17738 after the finalization phase.
17739
17740 @node Restrictions in Stand-alone Libraries
17741 @subsection Restrictions in Stand-alone Libraries
17742
17743 @noindent
17744 The pragmas listed below should be used with caution inside libraries,
17745 as they can create incompatibilities with other Ada libraries:
17746 @itemize @bullet
17747 @item pragma @code{Locking_Policy}
17748 @item pragma @code{Queuing_Policy}
17749 @item pragma @code{Task_Dispatching_Policy}
17750 @item pragma @code{Unreserve_All_Interrupts}
17751 @end itemize
17752
17753 @noindent
17754 When using a library that contains such pragmas, the user must make sure
17755 that all libraries use the same pragmas with the same values. Otherwise,
17756 @code{Program_Error} will
17757 be raised during the elaboration of the conflicting
17758 libraries. The usage of these pragmas and its consequences for the user
17759 should therefore be well documented.
17760
17761 Similarly, the traceback in the exception occurrence mechanism should be
17762 enabled or disabled in a consistent manner across all libraries.
17763 Otherwise, Program_Error will be raised during the elaboration of the
17764 conflicting libraries.
17765
17766 If the @code{Version} or @code{Body_Version}
17767 attributes are used inside a library, then you need to
17768 perform a @code{gnatbind} step that specifies all @file{ALI} files in all
17769 libraries, so that version identifiers can be properly computed.
17770 In practice these attributes are rarely used, so this is unlikely
17771 to be a consideration.
17772
17773 @node  Rebuilding the GNAT Run-Time Library
17774 @section Rebuilding the GNAT Run-Time Library
17775 @cindex GNAT Run-Time Library, rebuilding
17776
17777 @noindent
17778 It may be useful to recompile the GNAT library in various contexts, the
17779 most important one being the use of partition-wide configuration pragmas
17780 such as @code{Normalize_Scalars}. A special Makefile called
17781 @code{Makefile.adalib} is provided to that effect and can be found in
17782 the directory containing the GNAT library. The location of this
17783 directory depends on the way the GNAT environment has been installed and can
17784 be determined by means of the command:
17785
17786 @smallexample
17787 $ gnatls -v
17788 @end smallexample
17789
17790 @noindent
17791 The last entry in the object search path usually contains the
17792 gnat library. This Makefile contains its own documentation and in
17793 particular the set of instructions needed to rebuild a new library and
17794 to use it.
17795
17796 @node Using the GNU make Utility
17797 @chapter Using the GNU @code{make} Utility
17798 @findex make
17799
17800 @noindent
17801 This chapter offers some examples of makefiles that solve specific
17802 problems. It does not explain how to write a makefile (see the GNU make
17803 documentation), nor does it try to replace the @command{gnatmake} utility
17804 (@pxref{The GNAT Make Program gnatmake}).
17805
17806 All the examples in this section are specific to the GNU version of
17807 make. Although @code{make} is a standard utility, and the basic language
17808 is the same, these examples use some advanced features found only in
17809 @code{GNU make}.
17810
17811 @menu
17812 * Using gnatmake in a Makefile::
17813 * Automatically Creating a List of Directories::
17814 * Generating the Command Line Switches::
17815 * Overcoming Command Line Length Limits::
17816 @end menu
17817
17818 @node Using gnatmake in a Makefile
17819 @section Using gnatmake in a Makefile
17820 @findex makefile
17821 @cindex GNU make
17822
17823 @noindent
17824 Complex project organizations can be handled in a very powerful way by
17825 using GNU make combined with gnatmake. For instance, here is a Makefile
17826 which allows you to build each subsystem of a big project into a separate
17827 shared library. Such a makefile allows you to significantly reduce the link
17828 time of very big applications while maintaining full coherence at
17829 each step of the build process.
17830
17831 The list of dependencies are handled automatically by
17832 @command{gnatmake}. The Makefile is simply used to call gnatmake in each of
17833 the appropriate directories.
17834
17835 Note that you should also read the example on how to automatically
17836 create the list of directories
17837 (@pxref{Automatically Creating a List of Directories})
17838 which might help you in case your project has a lot of subdirectories.
17839
17840 @smallexample
17841 @iftex
17842 @leftskip=0cm
17843 @font@heightrm=cmr8
17844 @heightrm
17845 @end iftex
17846 ## This Makefile is intended to be used with the following directory
17847 ## configuration:
17848 ##  - The sources are split into a series of csc (computer software components)
17849 ##    Each of these csc is put in its own directory.
17850 ##    Their name are referenced by the directory names.
17851 ##    They will be compiled into shared library (although this would also work
17852 ##    with static libraries
17853 ##  - The main program (and possibly other packages that do not belong to any
17854 ##    csc is put in the top level directory (where the Makefile is).
17855 ##       toplevel_dir __ first_csc  (sources) __ lib (will contain the library)
17856 ##                    \_ second_csc (sources) __ lib (will contain the library)
17857 ##                    \_ ...
17858 ## Although this Makefile is build for shared library, it is easy to modify
17859 ## to build partial link objects instead (modify the lines with -shared and
17860 ## gnatlink below)
17861 ##
17862 ## With this makefile, you can change any file in the system or add any new
17863 ## file, and everything will be recompiled correctly (only the relevant shared
17864 ## objects will be recompiled, and the main program will be re-linked).
17865
17866 # The list of computer software component for your project. This might be
17867 # generated automatically.
17868 CSC_LIST=aa bb cc
17869
17870 # Name of the main program (no extension)
17871 MAIN=main
17872
17873 # If we need to build objects with -fPIC, uncomment the following line
17874 #NEED_FPIC=-fPIC
17875
17876 # The following variable should give the directory containing libgnat.so
17877 # You can get this directory through 'gnatls -v'. This is usually the last
17878 # directory in the Object_Path.
17879 GLIB=...
17880
17881 # The directories for the libraries
17882 # (This macro expands the list of CSC to the list of shared libraries, you
17883 # could simply use the expanded form :
17884 # LIB_DIR=aa/lib/libaa.so bb/lib/libbb.so cc/lib/libcc.so
17885 LIB_DIR=$@{foreach dir,$@{CSC_LIST@},$@{dir@}/lib/lib$@{dir@}.so@}
17886
17887 $@{MAIN@}: objects $@{LIB_DIR@}
17888     gnatbind $@{MAIN@} $@{CSC_LIST:%=-aO%/lib@} -shared
17889     gnatlink $@{MAIN@} $@{CSC_LIST:%=-l%@}
17890
17891 objects::
17892     # recompile the sources
17893     gnatmake -c -i $@{MAIN@}.adb $@{NEED_FPIC@} $@{CSC_LIST:%=-I%@}
17894
17895 # Note: In a future version of GNAT, the following commands will be simplified
17896 # by a new tool, gnatmlib
17897 $@{LIB_DIR@}:
17898     mkdir -p $@{dir $@@ @}
17899     cd $@{dir $@@ @}; gcc -shared -o $@{notdir $@@ @} ../*.o -L$@{GLIB@} -lgnat
17900     cd $@{dir $@@ @}; cp -f ../*.ali .
17901
17902 # The dependencies for the modules
17903 # Note that we have to force the expansion of *.o, since in some cases
17904 # make won't be able to do it itself.
17905 aa/lib/libaa.so: $@{wildcard aa/*.o@}
17906 bb/lib/libbb.so: $@{wildcard bb/*.o@}
17907 cc/lib/libcc.so: $@{wildcard cc/*.o@}
17908
17909 # Make sure all of the shared libraries are in the path before starting the
17910 # program
17911 run::
17912     LD_LIBRARY_PATH=`pwd`/aa/lib:`pwd`/bb/lib:`pwd`/cc/lib ./$@{MAIN@}
17913
17914 clean::
17915     $@{RM@} -rf $@{CSC_LIST:%=%/lib@}
17916     $@{RM@} $@{CSC_LIST:%=%/*.ali@}
17917     $@{RM@} $@{CSC_LIST:%=%/*.o@}
17918     $@{RM@} *.o *.ali $@{MAIN@}
17919 @end smallexample
17920
17921 @node Automatically Creating a List of Directories
17922 @section Automatically Creating a List of Directories
17923
17924 @noindent
17925 In most makefiles, you will have to specify a list of directories, and
17926 store it in a variable. For small projects, it is often easier to
17927 specify each of them by hand, since you then have full control over what
17928 is the proper order for these directories, which ones should be
17929 included...
17930
17931 However, in larger projects, which might involve hundreds of
17932 subdirectories, it might be more convenient to generate this list
17933 automatically.
17934
17935 The example below presents two methods. The first one, although less
17936 general, gives you more control over the list. It involves wildcard
17937 characters, that are automatically expanded by @code{make}. Its
17938 shortcoming is that you need to explicitly specify some of the
17939 organization of your project, such as for instance the directory tree
17940 depth, whether some directories are found in a separate tree,...
17941
17942 The second method is the most general one. It requires an external
17943 program, called @code{find}, which is standard on all Unix systems. All
17944 the directories found under a given root directory will be added to the
17945 list.
17946
17947 @smallexample
17948 @iftex
17949 @leftskip=0cm
17950 @font@heightrm=cmr8
17951 @heightrm
17952 @end iftex
17953 # The examples below are based on the following directory hierarchy:
17954 # All the directories can contain any number of files
17955 # ROOT_DIRECTORY ->  a  ->  aa  ->  aaa
17956 #                       ->  ab
17957 #                       ->  ac
17958 #                ->  b  ->  ba  ->  baa
17959 #                       ->  bb
17960 #                       ->  bc
17961 # This Makefile creates a variable called DIRS, that can be reused any time
17962 # you need this list (see the other examples in this section)
17963
17964 # The root of your project's directory hierarchy
17965 ROOT_DIRECTORY=.
17966
17967 ####
17968 # First method: specify explicitly the list of directories
17969 # This allows you to specify any subset of all the directories you need.
17970 ####
17971
17972 DIRS := a/aa/ a/ab/ b/ba/
17973
17974 ####
17975 # Second method: use wildcards
17976 # Note that the argument(s) to wildcard below should end with a '/'.
17977 # Since wildcards also return file names, we have to filter them out
17978 # to avoid duplicate directory names.
17979 # We thus use make's @code{dir} and @code{sort} functions.
17980 # It sets DIRs to the following value (note that the directories aaa and baa
17981 # are not given, unless you change the arguments to wildcard).
17982 # DIRS= ./a/a/ ./b/ ./a/aa/ ./a/ab/ ./a/ac/ ./b/ba/ ./b/bb/ ./b/bc/
17983 ####
17984
17985 DIRS := $@{sort $@{dir $@{wildcard $@{ROOT_DIRECTORY@}/*/
17986                     $@{ROOT_DIRECTORY@}/*/*/@}@}@}
17987
17988 ####
17989 # Third method: use an external program
17990 # This command is much faster if run on local disks, avoiding NFS slowdowns.
17991 # This is the most complete command: it sets DIRs to the following value:
17992 # DIRS= ./a ./a/aa ./a/aa/aaa ./a/ab ./a/ac ./b ./b/ba ./b/ba/baa ./b/bb ./b/bc
17993 ####
17994
17995 DIRS := $@{shell find $@{ROOT_DIRECTORY@} -type d -print@}
17996
17997 @end smallexample
17998
17999 @node Generating the Command Line Switches
18000 @section Generating the Command Line Switches
18001
18002 @noindent
18003 Once you have created the list of directories as explained in the
18004 previous section (@pxref{Automatically Creating a List of Directories}),
18005 you can easily generate the command line arguments to pass to gnatmake.
18006
18007 For the sake of completeness, this example assumes that the source path
18008 is not the same as the object path, and that you have two separate lists
18009 of directories.
18010
18011 @smallexample
18012 # see "Automatically creating a list of directories" to create
18013 # these variables
18014 SOURCE_DIRS=
18015 OBJECT_DIRS=
18016
18017 GNATMAKE_SWITCHES := $@{patsubst %,-aI%,$@{SOURCE_DIRS@}@}
18018 GNATMAKE_SWITCHES += $@{patsubst %,-aO%,$@{OBJECT_DIRS@}@}
18019
18020 all:
18021         gnatmake $@{GNATMAKE_SWITCHES@} main_unit
18022 @end smallexample
18023
18024 @node Overcoming Command Line Length Limits
18025 @section Overcoming Command Line Length Limits
18026
18027 @noindent
18028 One problem that might be encountered on big projects is that many
18029 operating systems limit the length of the command line. It is thus hard to give
18030 gnatmake the list of source and object directories.
18031
18032 This example shows how you can set up environment variables, which will
18033 make @command{gnatmake} behave exactly as if the directories had been
18034 specified on the command line, but have a much higher length limit (or
18035 even none on most systems).
18036
18037 It assumes that you have created a list of directories in your Makefile,
18038 using one of the methods presented in
18039 @ref{Automatically Creating a List of Directories}.
18040 For the sake of completeness, we assume that the object
18041 path (where the ALI files are found) is different from the sources patch.
18042
18043 Note a small trick in the Makefile below: for efficiency reasons, we
18044 create two temporary variables (SOURCE_LIST and OBJECT_LIST), that are
18045 expanded immediately by @code{make}. This way we overcome the standard
18046 make behavior which is to expand the variables only when they are
18047 actually used.
18048
18049 On Windows, if you are using the standard Windows command shell, you must
18050 replace colons with semicolons in the assignments to these variables.
18051
18052 @smallexample
18053 @iftex
18054 @leftskip=0cm
18055 @font@heightrm=cmr8
18056 @heightrm
18057 @end iftex
18058 # In this example, we create both ADA_INCLUDE_PATH and ADA_OBJECT_PATH.
18059 # This is the same thing as putting the -I arguments on the command line.
18060 # (the equivalent of using -aI on the command line would be to define
18061 #  only ADA_INCLUDE_PATH, the equivalent of -aO is ADA_OBJECT_PATH).
18062 # You can of course have different values for these variables.
18063 #
18064 # Note also that we need to keep the previous values of these variables, since
18065 # they might have been set before running 'make' to specify where the GNAT
18066 # library is installed.
18067
18068 # see "Automatically creating a list of directories" to create these
18069 # variables
18070 SOURCE_DIRS=
18071 OBJECT_DIRS=
18072
18073 empty:=
18074 space:=$@{empty@} $@{empty@}
18075 SOURCE_LIST := $@{subst $@{space@},:,$@{SOURCE_DIRS@}@}
18076 OBJECT_LIST := $@{subst $@{space@},:,$@{OBJECT_DIRS@}@}
18077 ADA_INCLUDE_PATH += $@{SOURCE_LIST@}
18078 ADA_OBJECT_PATH += $@{OBJECT_LIST@}
18079 export ADA_INCLUDE_PATH
18080 export ADA_OBJECT_PATH
18081
18082 all:
18083         gnatmake main_unit
18084 @end smallexample
18085 @end ifclear
18086
18087 @node Memory Management Issues
18088 @chapter Memory Management Issues
18089
18090 @noindent
18091 This chapter describes some useful memory pools provided in the GNAT library
18092 and in particular the GNAT Debug Pool facility, which can be used to detect
18093 incorrect uses of access values (including ``dangling references'').
18094 @ifclear vms
18095 It also describes the @command{gnatmem} tool, which can be used to track down
18096 ``memory leaks''.
18097 @end ifclear
18098
18099 @menu
18100 * Some Useful Memory Pools::
18101 * The GNAT Debug Pool Facility::
18102 @ifclear vms
18103 * The gnatmem Tool::
18104 @end ifclear
18105 @end menu
18106
18107 @node Some Useful Memory Pools
18108 @section Some Useful Memory Pools
18109 @findex Memory Pool
18110 @cindex storage, pool
18111
18112 @noindent
18113 The @code{System.Pool_Global} package offers the Unbounded_No_Reclaim_Pool
18114 storage pool. Allocations use the standard system call @code{malloc} while
18115 deallocations use the standard system call @code{free}. No reclamation is
18116 performed when the pool goes out of scope. For performance reasons, the
18117 standard default Ada allocators/deallocators do not use any explicit storage
18118 pools but if they did, they could use this storage pool without any change in
18119 behavior. That is why this storage pool is used  when the user
18120 manages to make the default implicit allocator explicit as in this example:
18121 @smallexample @c ada
18122    type T1 is access Something;
18123     -- no Storage pool is defined for T2
18124    type T2 is access Something_Else;
18125    for T2'Storage_Pool use T1'Storage_Pool;
18126    -- the above is equivalent to
18127    for T2'Storage_Pool use System.Pool_Global.Global_Pool_Object;
18128 @end smallexample
18129
18130 @noindent
18131 The @code{System.Pool_Local} package offers the Unbounded_Reclaim_Pool storage
18132 pool. The allocation strategy is similar to @code{Pool_Local}'s
18133 except that the all
18134 storage allocated with this pool is reclaimed when the pool object goes out of
18135 scope. This pool provides a explicit mechanism similar to the implicit one
18136 provided by several Ada 83 compilers for allocations performed through a local
18137 access type and whose purpose was to reclaim memory when exiting the
18138 scope of a given local access. As an example, the following program does not
18139 leak memory even though it does not perform explicit deallocation:
18140
18141 @smallexample @c ada
18142 with System.Pool_Local;
18143 procedure Pooloc1 is
18144    procedure Internal is
18145       type A is access Integer;
18146       X : System.Pool_Local.Unbounded_Reclaim_Pool;
18147       for A'Storage_Pool use X;
18148       v : A;
18149    begin
18150       for I in  1 .. 50 loop
18151          v := new Integer;
18152       end loop;
18153    end Internal;
18154 begin
18155    for I in  1 .. 100 loop
18156       Internal;
18157    end loop;
18158 end Pooloc1;
18159 @end smallexample
18160
18161 @noindent
18162 The @code{System.Pool_Size} package implements the Stack_Bounded_Pool used when
18163 @code{Storage_Size} is specified for an access type.
18164 The whole storage for the pool is
18165 allocated at once, usually on the stack at the point where the access type is
18166 elaborated. It is automatically reclaimed when exiting the scope where the
18167 access type is defined. This package is not intended to be used directly by the
18168 user and it is implicitly used for each such declaration:
18169
18170 @smallexample @c ada
18171    type T1 is access Something;
18172    for T1'Storage_Size use 10_000;
18173 @end smallexample
18174
18175
18176 @node The GNAT Debug Pool Facility
18177 @section The GNAT Debug Pool Facility
18178 @findex Debug Pool
18179 @cindex storage, pool, memory corruption
18180
18181 @noindent
18182 The use of unchecked deallocation and unchecked conversion can easily
18183 lead to incorrect memory references. The problems generated by such
18184 references are usually difficult to tackle because the symptoms can be
18185 very remote from the origin of the problem. In such cases, it is
18186 very helpful to detect the problem as early as possible. This is the
18187 purpose of the Storage Pool provided by @code{GNAT.Debug_Pools}.
18188
18189 In order to use the GNAT specific debugging pool, the user must
18190 associate a debug pool object with each of the access types that may be
18191 related to suspected memory problems. See Ada Reference Manual 13.11.
18192 @smallexample @c ada
18193 type Ptr is access Some_Type;
18194 Pool : GNAT.Debug_Pools.Debug_Pool;
18195 for Ptr'Storage_Pool use Pool;
18196 @end smallexample
18197
18198 @noindent
18199 @code{GNAT.Debug_Pools} is derived from a GNAT-specific kind of
18200 pool: the @code{Checked_Pool}. Such pools, like standard Ada storage pools,
18201 allow the user to redefine allocation and deallocation strategies. They
18202 also provide a checkpoint for each dereference, through the use of
18203 the primitive operation @code{Dereference} which is implicitly called at
18204 each dereference of an access value.
18205
18206 Once an access type has been associated with a debug pool, operations on
18207 values of the type may raise four distinct exceptions,
18208 which correspond to four potential kinds of memory corruption:
18209 @itemize @bullet
18210 @item
18211 @code{GNAT.Debug_Pools.Accessing_Not_Allocated_Storage}
18212 @item
18213 @code{GNAT.Debug_Pools.Accessing_Deallocated_Storage}
18214 @item
18215 @code{GNAT.Debug_Pools.Freeing_Not_Allocated_Storage}
18216 @item
18217 @code{GNAT.Debug_Pools.Freeing_Deallocated_Storage }
18218 @end itemize
18219
18220 @noindent
18221 For types associated with a Debug_Pool, dynamic allocation is performed using
18222 the standard GNAT allocation routine. References to all allocated chunks of
18223 memory are kept in an internal dictionary. Several deallocation strategies are
18224 provided, whereupon the user can choose to release the memory to the system,
18225 keep it allocated for further invalid access checks, or fill it with an easily
18226 recognizable pattern for debug sessions. The memory pattern is the old IBM
18227 hexadecimal convention: @code{16#DEADBEEF#}.
18228
18229 See the documentation in the file g-debpoo.ads for more information on the
18230 various strategies.
18231
18232 Upon each dereference, a check is made that the access value denotes a
18233 properly allocated memory location. Here is a complete example of use of
18234 @code{Debug_Pools}, that includes typical instances of  memory corruption:
18235 @smallexample @c ada
18236 @iftex
18237 @leftskip=0cm
18238 @end iftex
18239 with Gnat.Io; use Gnat.Io;
18240 with Unchecked_Deallocation;
18241 with Unchecked_Conversion;
18242 with GNAT.Debug_Pools;
18243 with System.Storage_Elements;
18244 with Ada.Exceptions; use Ada.Exceptions;
18245 procedure Debug_Pool_Test is
18246
18247    type T is access Integer;
18248    type U is access all T;
18249
18250    P : GNAT.Debug_Pools.Debug_Pool;
18251    for T'Storage_Pool use P;
18252
18253    procedure Free is new Unchecked_Deallocation (Integer, T);
18254    function UC is new Unchecked_Conversion (U, T);
18255    A, B : aliased T;
18256
18257    procedure Info is new GNAT.Debug_Pools.Print_Info(Put_Line);
18258
18259 begin
18260    Info (P);
18261    A := new Integer;
18262    B := new Integer;
18263    B := A;
18264    Info (P);
18265    Free (A);
18266    begin
18267       Put_Line (Integer'Image(B.all));
18268    exception
18269       when E : others => Put_Line ("raised: " & Exception_Name (E));
18270    end;
18271    begin
18272       Free (B);
18273    exception
18274       when E : others => Put_Line ("raised: " & Exception_Name (E));
18275    end;
18276    B := UC(A'Access);
18277    begin
18278       Put_Line (Integer'Image(B.all));
18279    exception
18280       when E : others => Put_Line ("raised: " & Exception_Name (E));
18281    end;
18282    begin
18283       Free (B);
18284    exception
18285       when E : others => Put_Line ("raised: " & Exception_Name (E));
18286    end;
18287    Info (P);
18288 end Debug_Pool_Test;
18289 @end smallexample
18290
18291 @noindent
18292 The debug pool mechanism provides the following precise diagnostics on the
18293 execution of this erroneous program:
18294 @smallexample
18295 Debug Pool info:
18296   Total allocated bytes :  0
18297   Total deallocated bytes :  0
18298   Current Water Mark:  0
18299   High Water Mark:  0
18300
18301 Debug Pool info:
18302   Total allocated bytes :  8
18303   Total deallocated bytes :  0
18304   Current Water Mark:  8
18305   High Water Mark:  8
18306
18307 raised: GNAT.DEBUG_POOLS.ACCESSING_DEALLOCATED_STORAGE
18308 raised: GNAT.DEBUG_POOLS.FREEING_DEALLOCATED_STORAGE
18309 raised: GNAT.DEBUG_POOLS.ACCESSING_NOT_ALLOCATED_STORAGE
18310 raised: GNAT.DEBUG_POOLS.FREEING_NOT_ALLOCATED_STORAGE
18311 Debug Pool info:
18312   Total allocated bytes :  8
18313   Total deallocated bytes :  4
18314   Current Water Mark:  4
18315   High Water Mark:  8
18316 @end smallexample
18317
18318 @ifclear vms
18319 @node The gnatmem Tool
18320 @section The @command{gnatmem} Tool
18321 @findex gnatmem
18322
18323 @noindent
18324 The @code{gnatmem} utility monitors dynamic allocation and
18325 deallocation activity in a program, and displays information about
18326 incorrect deallocations and possible sources of memory leaks.
18327 It provides three type of information:
18328 @itemize @bullet
18329 @item
18330 General information concerning memory management, such as the total
18331 number of allocations and deallocations, the amount of allocated
18332 memory and the high water mark, i.e. the largest amount of allocated
18333 memory in the course of program execution.
18334
18335 @item
18336 Backtraces for all incorrect deallocations, that is to say deallocations
18337 which do not correspond to a valid allocation.
18338
18339 @item
18340 Information on each allocation that is potentially the origin of a memory
18341 leak.
18342 @end itemize
18343
18344 @menu
18345 * Running gnatmem::
18346 * Switches for gnatmem::
18347 * Example of gnatmem Usage::
18348 @end menu
18349
18350 @node Running gnatmem
18351 @subsection Running @code{gnatmem}
18352
18353 @noindent
18354 @code{gnatmem} makes use of the output created by the special version of
18355 allocation and deallocation routines that record call information. This
18356 allows to obtain accurate dynamic memory usage history at a minimal cost to
18357 the execution speed. Note however, that @code{gnatmem} is not supported on
18358 all platforms (currently, it is supported on AIX, HP-UX, GNU/Linux x86,
18359 32-bit Solaris (sparc and x86) and Windows NT/2000/XP (x86).
18360
18361 @noindent
18362 The @code{gnatmem} command has the form
18363
18364 @smallexample
18365    $ gnatmem [switches] user_program
18366 @end smallexample
18367
18368 @noindent
18369 The program must have been linked with the instrumented version of the
18370 allocation and deallocation routines. This is done by linking with the
18371 @file{libgmem.a} library. For correct symbolic backtrace information,
18372 the user program should be compiled with debugging options
18373 @ref{Switches for gcc}. For example to build @file{my_program}:
18374
18375 @smallexample
18376 $ gnatmake -g my_program -largs -lgmem
18377 @end smallexample
18378
18379 @noindent
18380 When running @file{my_program} the file @file{gmem.out} is produced. This file
18381 contains information about all allocations and deallocations done by the
18382 program. It is produced by the instrumented allocations and
18383 deallocations routines and will be used by @code{gnatmem}.
18384
18385 @noindent
18386 Gnatmem must be supplied with the @file{gmem.out} file and the executable to
18387 examine. If the location of @file{gmem.out} file was not explicitly supplied by
18388 @code{-i} switch, gnatmem will assume that this file can be found in the
18389 current directory. For example, after you have executed @file{my_program},
18390 @file{gmem.out} can be analyzed by @code{gnatmem} using the command:
18391
18392 @smallexample
18393 $ gnatmem my_program
18394 @end smallexample
18395
18396 @noindent
18397 This will produce the output with the following format:
18398
18399 *************** debut cc
18400 @smallexample
18401 $ gnatmem my_program
18402
18403 Global information
18404 ------------------
18405    Total number of allocations        :  45
18406    Total number of deallocations      :   6
18407    Final Water Mark (non freed mem)   :  11.29 Kilobytes
18408    High Water Mark                    :  11.40 Kilobytes
18409
18410 .
18411 .
18412 .
18413 Allocation Root # 2
18414 -------------------
18415  Number of non freed allocations    :  11
18416  Final Water Mark (non freed mem)   :   1.16 Kilobytes
18417  High Water Mark                    :   1.27 Kilobytes
18418  Backtrace                          :
18419    my_program.adb:23 my_program.alloc
18420 .
18421 .
18422 .
18423 @end smallexample
18424
18425 The first block of output gives general information. In this case, the
18426 Ada construct ``@code{@b{new}}'' was executed 45 times, and only 6 calls to an
18427 Unchecked_Deallocation routine occurred.
18428
18429 @noindent
18430 Subsequent paragraphs display  information on all allocation roots.
18431 An allocation root is a specific point in the execution of the program
18432 that generates some dynamic allocation, such as a ``@code{@b{new}}''
18433 construct. This root is represented by an execution backtrace (or subprogram
18434 call stack). By default the backtrace depth for allocations roots is 1, so
18435 that a root corresponds exactly to a source location. The backtrace can
18436 be made deeper, to make the root more specific.
18437
18438 @node Switches for gnatmem
18439 @subsection Switches for @code{gnatmem}
18440
18441 @noindent
18442 @code{gnatmem} recognizes the following switches:
18443
18444 @table @option
18445
18446 @item -q
18447 @cindex @option{-q} (@code{gnatmem})
18448 Quiet. Gives the minimum output needed to identify the origin of the
18449 memory leaks. Omits statistical information.
18450
18451 @item @var{N}
18452 @cindex @var{N} (@code{gnatmem})
18453 N is an integer literal (usually between 1 and 10) which controls the
18454 depth of the backtraces defining allocation root. The default value for
18455 N is 1. The deeper the backtrace, the more precise the localization of
18456 the root. Note that the total number of roots can depend on this
18457 parameter. This parameter must be specified @emph{before} the name of the
18458 executable to be analyzed, to avoid ambiguity.
18459
18460 @item -b n
18461 @cindex @option{-b} (@code{gnatmem})
18462 This switch has the same effect as just depth parameter.
18463
18464 @item -i @var{file}
18465 @cindex @option{-i} (@code{gnatmem})
18466 Do the @code{gnatmem} processing starting from @file{file}, rather than
18467 @file{gmem.out} in the current directory.
18468
18469 @item -m n
18470 @cindex @option{-m} (@code{gnatmem})
18471 This switch causes @code{gnatmem} to mask the allocation roots that have less
18472 than n leaks. The default value is 1. Specifying the value of 0 will allow to
18473 examine even the roots that didn't result in leaks.
18474
18475 @item -s order
18476 @cindex @option{-s} (@code{gnatmem})
18477 This switch causes @code{gnatmem} to sort the allocation roots according to the
18478 specified order of sort criteria, each identified by a single letter. The
18479 currently supported criteria are @code{n, h, w} standing respectively for
18480 number of unfreed allocations, high watermark, and final watermark
18481 corresponding to a specific root. The default order is @code{nwh}.
18482
18483 @end table
18484
18485 @node Example of gnatmem Usage
18486 @subsection Example of @code{gnatmem} Usage
18487
18488 @noindent
18489 The following example shows the use of @code{gnatmem}
18490 on a simple memory-leaking program.
18491 Suppose that we have the following Ada program:
18492
18493 @smallexample @c ada
18494 @group
18495 @cartouche
18496 with Unchecked_Deallocation;
18497 procedure Test_Gm is
18498
18499    type T is array (1..1000) of Integer;
18500    type Ptr is access T;
18501    procedure Free is new Unchecked_Deallocation (T, Ptr);
18502    A : Ptr;
18503
18504    procedure My_Alloc is
18505    begin
18506       A := new T;
18507    end My_Alloc;
18508
18509    procedure My_DeAlloc is
18510       B : Ptr := A;
18511    begin
18512       Free (B);
18513    end My_DeAlloc;
18514
18515 begin
18516    My_Alloc;
18517    for I in 1 .. 5 loop
18518       for J in I .. 5 loop
18519          My_Alloc;
18520       end loop;
18521       My_Dealloc;
18522    end loop;
18523 end;
18524 @end cartouche
18525 @end group
18526 @end smallexample
18527
18528 @noindent
18529 The program needs to be compiled with debugging option and linked with
18530 @code{gmem} library:
18531
18532 @smallexample
18533 $ gnatmake -g test_gm -largs -lgmem
18534 @end smallexample
18535
18536 @noindent
18537 Then we execute the program as usual:
18538
18539 @smallexample
18540 $ test_gm
18541 @end smallexample
18542
18543 @noindent
18544 Then @code{gnatmem} is invoked simply with
18545 @smallexample
18546 $ gnatmem test_gm
18547 @end smallexample
18548
18549 @noindent
18550 which produces the following output (result may vary on different platforms):
18551
18552 @smallexample
18553 Global information
18554 ------------------
18555    Total number of allocations        :  18
18556    Total number of deallocations      :   5
18557    Final Water Mark (non freed mem)   :  53.00 Kilobytes
18558    High Water Mark                    :  56.90 Kilobytes
18559
18560 Allocation Root # 1
18561 -------------------
18562  Number of non freed allocations    :  11
18563  Final Water Mark (non freed mem)   :  42.97 Kilobytes
18564  High Water Mark                    :  46.88 Kilobytes
18565  Backtrace                          :
18566    test_gm.adb:11 test_gm.my_alloc
18567
18568 Allocation Root # 2
18569 -------------------
18570  Number of non freed allocations    :   1
18571  Final Water Mark (non freed mem)   :  10.02 Kilobytes
18572  High Water Mark                    :  10.02 Kilobytes
18573  Backtrace                          :
18574    s-secsta.adb:81 system.secondary_stack.ss_init
18575
18576 Allocation Root # 3
18577 -------------------
18578  Number of non freed allocations    :   1
18579  Final Water Mark (non freed mem)   :  12 Bytes
18580  High Water Mark                    :  12 Bytes
18581  Backtrace                          :
18582    s-secsta.adb:181 system.secondary_stack.ss_init
18583 @end smallexample
18584
18585 @noindent
18586 Note that the GNAT run time contains itself a certain number of
18587 allocations that have no  corresponding deallocation,
18588 as shown here for root #2 and root
18589 #3. This is a normal behavior when the number of non freed allocations
18590 is one, it allocates dynamic data structures that the run time needs for
18591 the complete lifetime of the program. Note also that there is only one
18592 allocation root in the user program with a single line back trace:
18593 test_gm.adb:11 test_gm.my_alloc, whereas a careful analysis of the
18594 program shows that 'My_Alloc' is called at 2 different points in the
18595 source (line 21 and line 24). If those two allocation roots need to be
18596 distinguished, the backtrace depth parameter can be used:
18597
18598 @smallexample
18599 $ gnatmem 3 test_gm
18600 @end smallexample
18601
18602 @noindent
18603 which will give the following output:
18604
18605 @smallexample
18606 Global information
18607 ------------------
18608    Total number of allocations        :  18
18609    Total number of deallocations      :   5
18610    Final Water Mark (non freed mem)   :  53.00 Kilobytes
18611    High Water Mark                    :  56.90 Kilobytes
18612
18613 Allocation Root # 1
18614 -------------------
18615  Number of non freed allocations    :  10
18616  Final Water Mark (non freed mem)   :  39.06 Kilobytes
18617  High Water Mark                    :  42.97 Kilobytes
18618  Backtrace                          :
18619    test_gm.adb:11 test_gm.my_alloc
18620    test_gm.adb:24 test_gm
18621    b_test_gm.c:52 main
18622
18623 Allocation Root # 2
18624 -------------------
18625  Number of non freed allocations    :   1
18626  Final Water Mark (non freed mem)   :  10.02 Kilobytes
18627  High Water Mark                    :  10.02 Kilobytes
18628  Backtrace                          :
18629    s-secsta.adb:81  system.secondary_stack.ss_init
18630    s-secsta.adb:283 <system__secondary_stack___elabb>
18631    b_test_gm.c:33   adainit
18632
18633 Allocation Root # 3
18634 -------------------
18635  Number of non freed allocations    :   1
18636  Final Water Mark (non freed mem)   :   3.91 Kilobytes
18637  High Water Mark                    :   3.91 Kilobytes
18638  Backtrace                          :
18639    test_gm.adb:11 test_gm.my_alloc
18640    test_gm.adb:21 test_gm
18641    b_test_gm.c:52 main
18642
18643 Allocation Root # 4
18644 -------------------
18645  Number of non freed allocations    :   1
18646  Final Water Mark (non freed mem)   :  12 Bytes
18647  High Water Mark                    :  12 Bytes
18648  Backtrace                          :
18649    s-secsta.adb:181 system.secondary_stack.ss_init
18650    s-secsta.adb:283 <system__secondary_stack___elabb>
18651    b_test_gm.c:33   adainit
18652 @end smallexample
18653
18654 @noindent
18655 The allocation root #1 of the first example has been split in 2 roots #1
18656 and #3 thanks to the more precise associated backtrace.
18657
18658 @end ifclear
18659
18660 @node Creating Sample Bodies Using gnatstub
18661 @chapter Creating Sample Bodies Using @command{gnatstub}
18662 @findex gnatstub
18663
18664 @noindent
18665 @command{gnatstub} creates body stubs, that is, empty but compilable bodies
18666 for library unit declarations.
18667
18668 To create a body stub, @command{gnatstub} has to compile the library
18669 unit declaration. Therefore, bodies can be created only for legal
18670 library units. Moreover, if a library unit depends semantically upon
18671 units located outside the current directory, you have to provide
18672 the source search path when calling @command{gnatstub}, see the description
18673 of @command{gnatstub} switches below.
18674
18675 @menu
18676 * Running gnatstub::
18677 * Switches for gnatstub::
18678 @end menu
18679
18680 @node Running gnatstub
18681 @section Running @command{gnatstub}
18682
18683 @noindent
18684 @command{gnatstub} has the command-line interface of the form
18685
18686 @smallexample
18687 $ gnatstub [switches] filename [directory]
18688 @end smallexample
18689
18690 @noindent
18691 where
18692 @table @emph
18693 @item filename
18694 is the name of the source file that contains a library unit declaration
18695 for which a body must be created. The file name may contain the path
18696 information.
18697 The file name does not have to follow the GNAT file name conventions. If the
18698 name
18699 does not follow GNAT file naming conventions, the name of the body file must
18700 be provided
18701 explicitly as the value of the @option{^-o^/BODY=^@var{body-name}} option.
18702 If the file name follows the GNAT file naming
18703 conventions and the name of the body file is not provided,
18704 @command{gnatstub}
18705 creates the name
18706 of the body file from the argument file name by replacing the @file{.ads}
18707 suffix
18708 with the @file{.adb} suffix.
18709
18710 @item directory
18711 indicates the directory in which the body stub is to be placed (the default
18712 is the
18713 current directory)
18714
18715 @item switches
18716 is an optional sequence of switches as described in the next section
18717 @end table
18718
18719 @node Switches for gnatstub
18720 @section Switches for @command{gnatstub}
18721
18722 @table @option
18723 @c !sort!
18724
18725 @item ^-f^/FULL^
18726 @cindex @option{^-f^/FULL^} (@command{gnatstub})
18727 If the destination directory already contains a file with the name of the
18728 body file
18729 for the argument spec file, replace it with the generated body stub.
18730
18731 @item ^-hs^/HEADER=SPEC^
18732 @cindex @option{^-hs^/HEADER=SPEC^} (@command{gnatstub})
18733 Put the comment header (i.e., all the comments preceding the
18734 compilation unit) from the source of the library unit declaration
18735 into the body stub.
18736
18737 @item ^-hg^/HEADER=GENERAL^
18738 @cindex @option{^-hg^/HEADER=GENERAL^} (@command{gnatstub})
18739 Put a sample comment header into the body stub.
18740
18741 @ifclear vms
18742 @item -IDIR
18743 @cindex @option{-IDIR} (@command{gnatstub})
18744 @itemx -I-
18745 @cindex @option{-I-} (@command{gnatstub})
18746 @end ifclear
18747 @ifset vms
18748 @item /NOCURRENT_DIRECTORY
18749 @cindex @option{/NOCURRENT_DIRECTORY} (@command{gnatstub})
18750 @end ifset
18751 ^These switches have ^This switch has^ the same meaning as in calls to
18752 @command{gcc}.
18753 ^They define ^It defines ^ the source search path in the call to
18754 @command{gcc} issued
18755 by @command{gnatstub} to compile an argument source file.
18756
18757 @item ^-gnatec^/CONFIGURATION_PRAGMAS_FILE=^@var{PATH}
18758 @cindex @option{^-gnatec^/CONFIGURATION_PRAGMAS_FILE^} (@command{gnatstub})
18759 This switch has the same meaning as in calls to @command{gcc}.
18760 It defines the additional configuration file to be passed to the call to
18761 @command{gcc} issued
18762 by @command{gnatstub} to compile an argument source file.
18763
18764 @item ^-gnatyM^/MAX_LINE_LENGTH=^@var{n}
18765 @cindex @option{^-gnatyM^/MAX_LINE_LENGTH^} (@command{gnatstub})
18766 (@var{n} is a non-negative integer). Set the maximum line length in the
18767 body stub to @var{n}; the default is 79. The maximum value that can be
18768 specified is 32767. Note that in the special case of configuration
18769 pragma files, the maximum is always 32767 regardless of whether or
18770 not this switch appears.
18771
18772 @item ^-gnaty^/STYLE_CHECKS=^@var{n}
18773 @cindex @option{^-gnaty^/STYLE_CHECKS=^} (@command{gnatstub})
18774 (@var{n} is a non-negative integer from 1 to 9). Set the indentation level in
18775 the generated body sample to @var{n}.
18776 The default indentation is 3.
18777
18778 @item ^-gnatyo^/ORDERED_SUBPROGRAMS^
18779 @cindex @option{^-gnato^/ORDERED_SUBPROGRAMS^} (@command{gnatstub})
18780 Order local bodies alphabetically. (By default local bodies are ordered
18781 in the same way as the corresponding local specs in the argument spec file.)
18782
18783 @item ^-i^/INDENTATION=^@var{n}
18784 @cindex @option{^-i^/INDENTATION^} (@command{gnatstub})
18785 Same as @option{^-gnaty^/STYLE_CHECKS=^@var{n}}
18786
18787 @item ^-k^/TREE_FILE=SAVE^
18788 @cindex @option{^-k^/TREE_FILE=SAVE^} (@command{gnatstub})
18789 Do not remove the tree file (i.e., the snapshot of the compiler internal
18790 structures used by @command{gnatstub}) after creating the body stub.
18791
18792 @item ^-l^/LINE_LENGTH=^@var{n}
18793 @cindex @option{^-l^/LINE_LENGTH^} (@command{gnatstub})
18794 Same as @option{^-gnatyM^/MAX_LINE_LENGTH=^@var{n}}
18795
18796 @item ^-o^/BODY=^@var{body-name}
18797 @cindex @option{^-o^/BODY^} (@command{gnatstub})
18798 Body file name.  This should be set if the argument file name does not
18799 follow
18800 the GNAT file naming
18801 conventions. If this switch is omitted the default name for the body will be
18802 obtained
18803 from the argument file name according to the GNAT file naming conventions.
18804
18805 @item ^-q^/QUIET^
18806 @cindex @option{^-q^/QUIET^} (@command{gnatstub})
18807 Quiet mode: do not generate a confirmation when a body is
18808 successfully created, and do not generate a message when a body is not
18809 required for an
18810 argument unit.
18811
18812 @item ^-r^/TREE_FILE=REUSE^
18813 @cindex @option{^-r^/TREE_FILE=REUSE^} (@command{gnatstub})
18814 Reuse the tree file (if it exists) instead of creating it.  Instead of
18815 creating the tree file for the library unit declaration, @command{gnatstub}
18816 tries to find it in the current directory and use it for creating
18817 a body. If the tree file is not found, no body is created. This option
18818 also implies @option{^-k^/SAVE^}, whether or not
18819 the latter is set explicitly.
18820
18821 @item ^-t^/TREE_FILE=OVERWRITE^
18822 @cindex @option{^-t^/TREE_FILE=OVERWRITE^} (@command{gnatstub})
18823 Overwrite the existing tree file.  If the current directory already
18824 contains the file which, according to the GNAT file naming rules should
18825 be considered as a tree file for the argument source file,
18826 @command{gnatstub}
18827 will refuse to create the tree file needed to create a sample body
18828 unless this option is set.
18829
18830 @item ^-v^/VERBOSE^
18831 @cindex @option{^-v^/VERBOSE^} (@command{gnatstub})
18832 Verbose mode: generate version information.
18833
18834 @end table
18835
18836 @node Other Utility Programs
18837 @chapter Other Utility Programs
18838
18839 @noindent
18840 This chapter discusses some other utility programs available in the Ada
18841 environment.
18842
18843 @menu
18844 * Using Other Utility Programs with GNAT::
18845 * The External Symbol Naming Scheme of GNAT::
18846 @ifclear vms
18847 * Ada Mode for Glide::
18848 @end ifclear
18849 * Converting Ada Files to html with gnathtml::
18850 * Installing gnathtml::
18851 @ifset vms
18852 * LSE::
18853 * Profiling::
18854 @end ifset
18855 @end menu
18856
18857 @node Using Other Utility Programs with GNAT
18858 @section Using Other Utility Programs with GNAT
18859
18860 @noindent
18861 The object files generated by GNAT are in standard system format and in
18862 particular the debugging information uses this format. This means
18863 programs generated by GNAT can be used with existing utilities that
18864 depend on these formats.
18865
18866 @ifclear vms
18867 In general, any utility program that works with C will also often work with
18868 Ada programs generated by GNAT. This includes software utilities such as
18869 gprof (a profiling program), @code{gdb} (the FSF debugger), and utilities such
18870 as Purify.
18871 @end ifclear
18872
18873 @node The External Symbol Naming Scheme of GNAT
18874 @section The External Symbol Naming Scheme of GNAT
18875
18876 @noindent
18877 In order to interpret the output from GNAT, when using tools that are
18878 originally intended for use with other languages, it is useful to
18879 understand the conventions used to generate link names from the Ada
18880 entity names.
18881
18882 All link names are in all lowercase letters. With the exception of library
18883 procedure names, the mechanism used is simply to use the full expanded
18884 Ada name with dots replaced by double underscores. For example, suppose
18885 we have the following package spec:
18886
18887 @smallexample @c ada
18888 @group
18889 @cartouche
18890 package QRS is
18891    MN : Integer;
18892 end QRS;
18893 @end cartouche
18894 @end group
18895 @end smallexample
18896
18897 @noindent
18898 The variable @code{MN} has a full expanded Ada name of @code{QRS.MN}, so
18899 the corresponding link name is @code{qrs__mn}.
18900 @findex Export
18901 Of course if a @code{pragma Export} is used this may be overridden:
18902
18903 @smallexample @c ada
18904 @group
18905 @cartouche
18906 package Exports is
18907    Var1 : Integer;
18908    pragma Export (Var1, C, External_Name => "var1_name");
18909    Var2 : Integer;
18910    pragma Export (Var2, C, Link_Name => "var2_link_name");
18911 end Exports;
18912 @end cartouche
18913 @end group
18914 @end smallexample
18915
18916 @noindent
18917 In this case, the link name for @var{Var1} is whatever link name the
18918 C compiler would assign for the C function @var{var1_name}. This typically
18919 would be either @var{var1_name} or @var{_var1_name}, depending on operating
18920 system conventions, but other possibilities exist. The link name for
18921 @var{Var2} is @var{var2_link_name}, and this is not operating system
18922 dependent.
18923
18924 @findex _main
18925 One exception occurs for library level procedures. A potential ambiguity
18926 arises between the required name @code{_main} for the C main program,
18927 and the name we would otherwise assign to an Ada library level procedure
18928 called @code{Main} (which might well not be the main program).
18929
18930 To avoid this ambiguity, we attach the prefix @code{_ada_} to such
18931 names. So if we have a library level procedure such as
18932
18933 @smallexample @c ada
18934 @group
18935 @cartouche
18936 procedure Hello (S : String);
18937 @end cartouche
18938 @end group
18939 @end smallexample
18940
18941 @noindent
18942 the external name of this procedure will be @var{_ada_hello}.
18943
18944 @ifclear vms
18945 @node Ada Mode for Glide
18946 @section Ada Mode for @code{Glide}
18947 @cindex Ada mode (for Glide)
18948
18949 @noindent
18950 The Glide mode for programming in Ada (both Ada83 and Ada95) helps the
18951 user to understand and navigate existing code, and facilitates writing
18952 new code. It furthermore provides some utility functions for easier
18953 integration of standard Emacs features when programming in Ada.
18954
18955 Its general features include:
18956
18957 @itemize @bullet
18958 @item
18959 An Integrated Development Environment with functionality such as the
18960 following
18961
18962 @itemize @bullet
18963 @item
18964 ``Project files'' for configuration-specific aspects
18965 (e.g. directories and compilation options)
18966
18967 @item
18968 Compiling and stepping through error messages.
18969
18970 @item
18971 Running and debugging an applications within Glide.
18972 @end itemize
18973
18974 @item
18975 Pull-down menus
18976
18977 @item
18978 User configurability
18979 @end itemize
18980
18981 Some of the specific Ada mode features are:
18982
18983 @itemize @bullet
18984 @item
18985 Functions for easy and quick stepping through Ada code
18986
18987 @item
18988 Getting cross reference information for identifiers (e.g., finding a
18989 defining occurrence)
18990
18991 @item
18992 Displaying an index menu of types and subprograms, allowing
18993 direct selection for browsing
18994
18995 @item
18996 Automatic color highlighting of the various Ada entities
18997 @end itemize
18998
18999 Glide directly supports writing Ada code, via several facilities:
19000
19001 @itemize @bullet
19002 @item
19003 Switching between spec and body files with possible
19004 autogeneration of body files
19005
19006 @item
19007 Automatic formating of subprogram parameter lists
19008
19009 @item
19010 Automatic indentation according to Ada syntax
19011
19012 @item
19013 Automatic completion of identifiers
19014
19015 @item
19016 Automatic (and configurable) casing of identifiers, keywords, and attributes
19017
19018 @item
19019 Insertion of syntactic templates
19020
19021 @item
19022 Block commenting / uncommenting
19023 @end itemize
19024
19025 @noindent
19026 For more information, please refer to the online documentation
19027 available in the @code{Glide} @result{} @code{Help} menu.
19028 @end ifclear
19029
19030 @node Converting Ada Files to html with gnathtml
19031 @section Converting Ada Files to HTML with @code{gnathtml}
19032
19033 @noindent
19034 This @code{Perl} script allows Ada source files to be browsed using
19035 standard Web browsers. For installation procedure, see the section
19036 @xref{Installing gnathtml}.
19037
19038 Ada reserved keywords are highlighted in a bold font and Ada comments in
19039 a blue font. Unless your program was compiled with the gcc @option{-gnatx}
19040 switch to suppress the generation of cross-referencing information, user
19041 defined variables and types will appear in a different color; you will
19042 be able to click on any identifier and go to its declaration.
19043
19044 The command line is as follow:
19045 @smallexample
19046 $ perl gnathtml.pl [switches] ada-files
19047 @end smallexample
19048
19049 @noindent
19050 You can pass it as many Ada files as you want. @code{gnathtml} will generate
19051 an html file for every ada file, and a global file called @file{index.htm}.
19052 This file is an index of every identifier defined in the files.
19053
19054 The available switches are the following ones :
19055
19056 @table @option
19057 @item -83
19058 @cindex @option{-83} (@code{gnathtml})
19059 Only the subset on the Ada 83 keywords will be highlighted, not the full
19060 Ada 95 keywords set.
19061
19062 @item -cc @var{color}
19063 @cindex @option{-cc} (@code{gnathtml})
19064 This option allows you to change the color used for comments. The default
19065 value is green. The color argument can be any name accepted by html.
19066
19067 @item -d
19068 @cindex @option{-d} (@code{gnathtml})
19069 If the ada files depend on some other files (using for instance the
19070 @code{with} command, the latter will also be converted to html.
19071 Only the files in the user project will be converted to html, not the files
19072 in the run-time library itself.
19073
19074 @item -D
19075 @cindex @option{-D} (@code{gnathtml})
19076 This command is the same as @option{-d} above, but @command{gnathtml} will
19077 also look for files in the run-time library, and generate html files for them.
19078
19079 @item -ext @var{extension}
19080 @cindex @option{-ext} (@code{gnathtml})
19081 This option allows you to change the extension of the generated HTML files.
19082 If you do not specify an extension, it will default to @file{htm}.
19083
19084 @item -f
19085 @cindex @option{-f} (@code{gnathtml})
19086 By default, gnathtml will generate html links only for global entities
19087 ('with'ed units, global variables and types,...). If you specify the
19088 @option{-f} on the command line, then links will be generated for local
19089 entities too.
19090
19091 @item -l @var{number}
19092 @cindex @option{-l} (@code{gnathtml})
19093 If this switch is provided and @var{number} is not 0, then @code{gnathtml}
19094 will number the html files every @var{number} line.
19095
19096 @item -I @var{dir}
19097 @cindex @option{-I} (@code{gnathtml})
19098 Specify a directory to search for library files (@file{.ALI} files) and
19099 source files. You can provide several -I switches on the command line,
19100 and the directories will be parsed in the order of the command line.
19101
19102 @item -o @var{dir}
19103 @cindex @option{-o} (@code{gnathtml})
19104 Specify the output directory for html files. By default, gnathtml will
19105 saved the generated html files in a subdirectory named @file{html/}.
19106
19107 @item -p @var{file}
19108 @cindex @option{-p} (@code{gnathtml})
19109 If you are using Emacs and the most recent Emacs Ada mode, which provides
19110 a full Integrated Development Environment for compiling, checking,
19111 running and debugging applications, you may use @file{.gpr} files
19112 to give the directories where Emacs can find sources and object files.
19113
19114 Using this switch, you can tell gnathtml to use these files. This allows
19115 you to get an html version of your application, even if it is spread
19116 over multiple directories.
19117
19118 @item -sc @var{color}
19119 @cindex @option{-sc} (@code{gnathtml})
19120 This option allows you to change the color used for symbol definitions.
19121 The default value is red. The color argument can be any name accepted by html.
19122
19123 @item -t @var{file}
19124 @cindex @option{-t} (@code{gnathtml})
19125 This switch provides the name of a file. This file contains a list of
19126 file names to be converted, and the effect is exactly as though they had
19127 appeared explicitly on the command line. This
19128 is the recommended way to work around the command line length limit on some
19129 systems.
19130
19131 @end table
19132
19133 @node Installing gnathtml
19134 @section Installing @code{gnathtml}
19135
19136 @noindent
19137 @code{Perl} needs to be installed on your machine to run this script.
19138 @code{Perl} is freely available for almost every architecture and
19139 Operating System via the Internet.
19140
19141 On Unix systems, you  may want to modify  the  first line of  the script
19142 @code{gnathtml},  to explicitly  tell  the Operating  system  where Perl
19143 is. The syntax of this line is :
19144 @smallexample
19145 #!full_path_name_to_perl
19146 @end smallexample
19147
19148 @noindent
19149 Alternatively, you may run the script using the following command line:
19150
19151 @smallexample
19152 $ perl gnathtml.pl [switches] files
19153 @end smallexample
19154
19155 @ifset vms
19156 @node LSE
19157 @section LSE
19158 @findex LSE
19159
19160 @noindent
19161 The GNAT distribution provides an Ada 95 template for the HP Language
19162 Sensitive Editor (LSE), a component of DECset. In order to
19163 access it, invoke LSE with the qualifier /ENVIRONMENT=GNU:[LIB]ADA95.ENV.
19164
19165 @node Profiling
19166 @section Profiling
19167 @findex PCA
19168
19169 @noindent
19170 GNAT supports The HP Performance Coverage Analyzer (PCA), a component
19171 of DECset. To use it proceed as outlined under ``HELP PCA'', except for running
19172 the collection phase with the /DEBUG qualifier.
19173
19174 @smallexample
19175 $ GNAT MAKE /DEBUG <PROGRAM_NAME>
19176 $ DEFINE LIB$DEBUG PCA$COLLECTOR
19177 $ RUN/DEBUG <PROGRAM_NAME>
19178 @end smallexample
19179 @noindent
19180 @end ifset
19181
19182 @node Running and Debugging Ada Programs
19183 @chapter Running and Debugging Ada Programs
19184 @cindex Debugging
19185
19186 @noindent
19187 This chapter discusses how to debug Ada programs.
19188 @ifset vms
19189 It applies to the Alpha OpenVMS platform;
19190 the debugger for Integrity OpenVMS is scheduled for a subsequent release.
19191 @end ifset
19192
19193 An incorrect Ada program may be handled in three ways by the GNAT compiler:
19194
19195 @enumerate
19196 @item
19197 The illegality may be a violation of the static semantics of Ada. In
19198 that case GNAT diagnoses the constructs in the program that are illegal.
19199 It is then a straightforward matter for the user to modify those parts of
19200 the program.
19201
19202 @item
19203 The illegality may be a violation of the dynamic semantics of Ada. In
19204 that case the program compiles and executes, but may generate incorrect
19205 results, or may terminate abnormally with some exception.
19206
19207 @item
19208 When presented with a program that contains convoluted errors, GNAT
19209 itself may terminate abnormally without providing full diagnostics on
19210 the incorrect user program.
19211 @end enumerate
19212
19213 @menu
19214 * The GNAT Debugger GDB::
19215 * Running GDB::
19216 * Introduction to GDB Commands::
19217 * Using Ada Expressions::
19218 * Calling User-Defined Subprograms::
19219 * Using the Next Command in a Function::
19220 * Ada Exceptions::
19221 * Ada Tasks::
19222 * Debugging Generic Units::
19223 * GNAT Abnormal Termination or Failure to Terminate::
19224 * Naming Conventions for GNAT Source Files::
19225 * Getting Internal Debugging Information::
19226 * Stack Traceback::
19227 @end menu
19228
19229 @cindex Debugger
19230 @findex gdb
19231
19232 @node The GNAT Debugger GDB
19233 @section The GNAT Debugger GDB
19234
19235 @noindent
19236 @code{GDB} is a general purpose, platform-independent debugger that
19237 can be used to debug mixed-language programs compiled with @command{gcc},
19238 and in particular is capable of debugging Ada programs compiled with
19239 GNAT. The latest versions of @code{GDB} are Ada-aware and can handle
19240 complex Ada data structures.
19241
19242 The manual @cite{Debugging with GDB}
19243 @ifset vms
19244 , located in the GNU:[DOCS] directory,
19245 @end ifset
19246 contains full details on the usage of @code{GDB}, including a section on
19247 its usage on programs. This manual should be consulted for full
19248 details. The section that follows is a brief introduction to the
19249 philosophy and use of @code{GDB}.
19250
19251 When GNAT programs are compiled, the compiler optionally writes debugging
19252 information into the generated object file, including information on
19253 line numbers, and on declared types and variables. This information is
19254 separate from the generated code. It makes the object files considerably
19255 larger, but it does not add to the size of the actual executable that
19256 will be loaded into memory, and has no impact on run-time performance. The
19257 generation of debug information is triggered by the use of the
19258 ^-g^/DEBUG^ switch in the gcc or gnatmake command used to carry out
19259 the compilations. It is important to emphasize that the use of these
19260 options does not change the generated code.
19261
19262 The debugging information is written in standard system formats that
19263 are used by many tools, including debuggers and profilers. The format
19264 of the information is typically designed to describe C types and
19265 semantics, but GNAT implements a translation scheme which allows full
19266 details about Ada types and variables to be encoded into these
19267 standard C formats. Details of this encoding scheme may be found in
19268 the file exp_dbug.ads in the GNAT source distribution. However, the
19269 details of this encoding are, in general, of no interest to a user,
19270 since @code{GDB} automatically performs the necessary decoding.
19271
19272 When a program is bound and linked, the debugging information is
19273 collected from the object files, and stored in the executable image of
19274 the program. Again, this process significantly increases the size of
19275 the generated executable file, but it does not increase the size of
19276 the executable program itself. Furthermore, if this program is run in
19277 the normal manner, it runs exactly as if the debug information were
19278 not present, and takes no more actual memory.
19279
19280 However, if the program is run under control of @code{GDB}, the
19281 debugger is activated.  The image of the program is loaded, at which
19282 point it is ready to run.  If a run command is given, then the program
19283 will run exactly as it would have if @code{GDB} were not present. This
19284 is a crucial part of the @code{GDB} design philosophy.  @code{GDB} is
19285 entirely non-intrusive until a breakpoint is encountered.  If no
19286 breakpoint is ever hit, the program will run exactly as it would if no
19287 debugger were present. When a breakpoint is hit, @code{GDB} accesses
19288 the debugging information and can respond to user commands to inspect
19289 variables, and more generally to report on the state of execution.
19290
19291 @c **************
19292 @node Running GDB
19293 @section Running GDB
19294
19295 @noindent
19296 The debugger can be launched directly and simply from @code{glide} or
19297 through its graphical interface: @code{gvd}. It can also be used
19298 directly in text mode. Here is described the basic use of @code{GDB}
19299 in text mode. All the commands described below can be used in the
19300 @code{gvd} console window even though there is usually other more
19301 graphical ways to achieve the same goals.
19302
19303 @ifclear vms
19304 @noindent
19305 The command to run the graphical interface of the debugger is
19306 @smallexample
19307 $ gvd program
19308 @end smallexample
19309 @end ifclear
19310
19311 @noindent
19312 The command to run @code{GDB} in text mode is
19313
19314 @smallexample
19315 $ ^gdb program^$ GDB PROGRAM^
19316 @end smallexample
19317
19318 @noindent
19319 where @code{^program^PROGRAM^} is the name of the executable file. This
19320 activates the debugger and results in a prompt for debugger commands.
19321 The simplest command is simply @code{run}, which causes the program to run
19322 exactly as if the debugger were not present. The following section
19323 describes some of the additional commands that can be given to @code{GDB}.
19324
19325 @c *******************************
19326 @node Introduction to GDB Commands
19327 @section Introduction to GDB Commands
19328
19329 @noindent
19330 @code{GDB} contains a large repertoire of commands. The manual
19331 @cite{Debugging with GDB}
19332 @ifset vms
19333 , located in the GNU:[DOCS] directory,
19334 @end ifset
19335 includes extensive documentation on the use
19336 of these commands, together with examples of their use. Furthermore,
19337 the command @var{help} invoked from within @code{GDB} activates a simple help
19338 facility which summarizes the available commands and their options.
19339 In this section we summarize a few of the most commonly
19340 used commands to give an idea of what @code{GDB} is about. You should create
19341 a simple program with debugging information and experiment with the use of
19342 these @code{GDB} commands on the program as you read through the
19343 following section.
19344
19345 @table @code
19346 @item set args @var{arguments}
19347 The @var{arguments} list above is a list of arguments to be passed to
19348 the program on a subsequent run command, just as though the arguments
19349 had been entered on a normal invocation of the program. The @code{set args}
19350 command is not needed if the program does not require arguments.
19351
19352 @item run
19353 The @code{run} command causes execution of the program to start from
19354 the beginning. If the program is already running, that is to say if
19355 you are currently positioned at a breakpoint, then a prompt will ask
19356 for confirmation that you want to abandon the current execution and
19357 restart.
19358
19359 @item breakpoint @var{location}
19360 The breakpoint command sets a breakpoint, that is to say a point at which
19361 execution will halt and @code{GDB} will await further
19362 commands. @var{location} is
19363 either a line number within a file, given in the format @code{file:linenumber},
19364 or it is the name of a subprogram. If you request that a breakpoint be set on
19365 a subprogram that is overloaded, a prompt will ask you to specify on which of
19366 those subprograms you want to breakpoint. You can also
19367 specify that all of them should be breakpointed. If the program is run
19368 and execution encounters the breakpoint, then the program
19369 stops and @code{GDB} signals that the breakpoint was encountered by
19370 printing the line of code before which the program is halted.
19371
19372 @item breakpoint exception @var{name}
19373 A special form of the breakpoint command which breakpoints whenever
19374 exception @var{name} is raised.
19375 If @var{name} is omitted,
19376 then a breakpoint will occur when any exception is raised.
19377
19378 @item print @var{expression}
19379 This will print the value of the given expression. Most simple
19380 Ada expression formats are properly handled by @code{GDB}, so the expression
19381 can contain function calls, variables, operators, and attribute references.
19382
19383 @item continue
19384 Continues execution following a breakpoint, until the next breakpoint or the
19385 termination of the program.
19386
19387 @item step
19388 Executes a single line after a breakpoint. If the next statement
19389 is a subprogram call, execution continues into (the first statement of)
19390 the called subprogram.
19391
19392 @item next
19393 Executes a single line. If this line is a subprogram call, executes and
19394 returns from the call.
19395
19396 @item list
19397 Lists a few lines around the current source location. In practice, it
19398 is usually more convenient to have a separate edit window open with the
19399 relevant source file displayed. Successive applications of this command
19400 print subsequent lines. The command can be given an argument which is a
19401 line number, in which case it displays a few lines around the specified one.
19402
19403 @item backtrace
19404 Displays a backtrace of the call chain. This command is typically
19405 used after a breakpoint has occurred, to examine the sequence of calls that
19406 leads to the current breakpoint. The display includes one line for each
19407 activation record (frame) corresponding to an active subprogram.
19408
19409 @item up
19410 At a breakpoint, @code{GDB} can display the values of variables local
19411 to the current frame. The command @code{up} can be used to
19412 examine the contents of other active frames, by moving the focus up
19413 the stack, that is to say from callee to caller, one frame at a time.
19414
19415 @item down
19416 Moves the focus of @code{GDB} down from the frame currently being
19417 examined to the frame of its callee (the reverse of the previous command),
19418
19419 @item frame @var{n}
19420 Inspect the frame with the given number. The value 0 denotes the frame
19421 of the current breakpoint, that is to say the top of the call stack.
19422
19423 @end table
19424
19425 The above list is a very short introduction to the commands that
19426 @code{GDB} provides. Important additional capabilities, including conditional
19427 breakpoints, the ability to execute command sequences on a breakpoint,
19428 the ability to debug at the machine instruction level and many other
19429 features are described in detail in @cite{Debugging with GDB}.
19430 Note that most commands can be abbreviated
19431 (for example, c for continue, bt for backtrace).
19432
19433 @node Using Ada Expressions
19434 @section Using Ada Expressions
19435 @cindex Ada expressions
19436
19437 @noindent
19438 @code{GDB} supports a fairly large subset of Ada expression syntax, with some
19439 extensions. The philosophy behind the design of this subset is
19440
19441 @itemize @bullet
19442 @item
19443 That @code{GDB} should provide basic literals and access to operations for
19444 arithmetic, dereferencing, field selection, indexing, and subprogram calls,
19445 leaving more sophisticated computations to subprograms written into the
19446 program (which therefore may be called from @code{GDB}).
19447
19448 @item
19449 That type safety and strict adherence to Ada language restrictions
19450 are not particularly important to the @code{GDB} user.
19451
19452 @item
19453 That brevity is important to the @code{GDB} user.
19454 @end itemize
19455
19456 Thus, for brevity, the debugger acts as if there were
19457 implicit @code{with} and @code{use} clauses in effect for all user-written
19458 packages, thus making it unnecessary to fully qualify most names with
19459 their packages, regardless of context. Where this causes ambiguity,
19460 @code{GDB} asks the user's intent.
19461
19462 For details on the supported Ada syntax, see @cite{Debugging with GDB}.
19463
19464 @node Calling User-Defined Subprograms
19465 @section Calling User-Defined Subprograms
19466
19467 @noindent
19468 An important capability of @code{GDB} is the ability to call user-defined
19469 subprograms while debugging. This is achieved simply by entering
19470 a subprogram call statement in the form:
19471
19472 @smallexample
19473 call subprogram-name (parameters)
19474 @end smallexample
19475
19476 @noindent
19477 The keyword @code{call} can be omitted in the normal case where the
19478 @code{subprogram-name} does not coincide with any of the predefined
19479 @code{GDB} commands.
19480
19481 The effect is to invoke the given subprogram, passing it the
19482 list of parameters that is supplied. The parameters can be expressions and
19483 can include variables from the program being debugged. The
19484 subprogram must be defined
19485 at the library level within your program, and @code{GDB} will call the
19486 subprogram within the environment of your program execution (which
19487 means that the subprogram is free to access or even modify variables
19488 within your program).
19489
19490 The most important use of this facility is in allowing the inclusion of
19491 debugging routines that are tailored to particular data structures
19492 in your program. Such debugging routines can be written to provide a suitably
19493 high-level description of an abstract type, rather than a low-level dump
19494 of its physical layout. After all, the standard
19495 @code{GDB print} command only knows the physical layout of your
19496 types, not their abstract meaning. Debugging routines can provide information
19497 at the desired semantic level and are thus enormously useful.
19498
19499 For example, when debugging GNAT itself, it is crucial to have access to
19500 the contents of the tree nodes used to represent the program internally.
19501 But tree nodes are represented simply by an integer value (which in turn
19502 is an index into a table of nodes).
19503 Using the @code{print} command on a tree node would simply print this integer
19504 value, which is not very useful. But the PN routine (defined in file
19505 treepr.adb in the GNAT sources) takes a tree node as input, and displays
19506 a useful high level representation of the tree node, which includes the
19507 syntactic category of the node, its position in the source, the integers
19508 that denote descendant nodes and parent node, as well as varied
19509 semantic information. To study this example in more detail, you might want to
19510 look at the body of the PN procedure in the stated file.
19511
19512 @node Using the Next Command in a Function
19513 @section Using the Next Command in a Function
19514
19515 @noindent
19516 When you use the @code{next} command in a function, the current source
19517 location will advance to the next statement as usual. A special case
19518 arises in the case of a @code{return} statement.
19519
19520 Part of the code for a return statement is the ``epilog'' of the function.
19521 This is the code that returns to the caller. There is only one copy of
19522 this epilog code, and it is typically associated with the last return
19523 statement in the function if there is more than one return. In some
19524 implementations, this epilog is associated with the first statement
19525 of the function.
19526
19527 The result is that if you use the @code{next} command from a return
19528 statement that is not the last return statement of the function you
19529 may see a strange apparent jump to the last return statement or to
19530 the start of the function. You should simply ignore this odd jump.
19531 The value returned is always that from the first return statement
19532 that was stepped through.
19533
19534 @node Ada Exceptions
19535 @section Breaking on Ada Exceptions
19536 @cindex Exceptions
19537
19538 @noindent
19539 You can set breakpoints that trip when your program raises
19540 selected exceptions.
19541
19542 @table @code
19543 @item break exception
19544 Set a breakpoint that trips whenever (any task in the) program raises
19545 any exception.
19546
19547 @item break exception @var{name}
19548 Set a breakpoint that trips whenever (any task in the) program raises
19549 the exception @var{name}.
19550
19551 @item break exception unhandled
19552 Set a breakpoint that trips whenever (any task in the) program raises an
19553 exception for which there is no handler.
19554
19555 @item info exceptions
19556 @itemx info exceptions @var{regexp}
19557 The @code{info exceptions} command permits the user to examine all defined
19558 exceptions within Ada programs. With a regular expression, @var{regexp}, as
19559 argument, prints out only those exceptions whose name matches @var{regexp}.
19560 @end table
19561
19562 @node Ada Tasks
19563 @section Ada Tasks
19564 @cindex Tasks
19565
19566 @noindent
19567 @code{GDB} allows the following task-related commands:
19568
19569 @table @code
19570 @item info tasks
19571 This command shows a list of current Ada tasks, as in the following example:
19572
19573 @smallexample
19574 @iftex
19575 @leftskip=0cm
19576 @end iftex
19577 (gdb) info tasks
19578   ID       TID P-ID   Thread Pri State                 Name
19579    1   8088000   0   807e000  15 Child Activation Wait main_task
19580    2   80a4000   1   80ae000  15 Accept/Select Wait    b
19581    3   809a800   1   80a4800  15 Child Activation Wait a
19582 *  4   80ae800   3   80b8000  15 Running               c
19583 @end smallexample
19584
19585 @noindent
19586 In this listing, the asterisk before the first task indicates it to be the
19587 currently running task. The first column lists the task ID that is used
19588 to refer to tasks in the following commands.
19589
19590 @item break @var{linespec} task @var{taskid}
19591 @itemx break @var{linespec} task @var{taskid} if @dots{}
19592 @cindex Breakpoints and tasks
19593 These commands are like the @code{break @dots{} thread @dots{}}.
19594 @var{linespec} specifies source lines.
19595
19596 Use the qualifier @samp{task @var{taskid}} with a breakpoint command
19597 to specify that you only want @code{GDB} to stop the program when a
19598 particular Ada task reaches this breakpoint. @var{taskid} is one of the
19599 numeric task identifiers assigned by @code{GDB}, shown in the first
19600 column of the @samp{info tasks} display.
19601
19602 If you do not specify @samp{task @var{taskid}} when you set a
19603 breakpoint, the breakpoint applies to @emph{all} tasks of your
19604 program.
19605
19606 You can use the @code{task} qualifier on conditional breakpoints as
19607 well; in this case, place @samp{task @var{taskid}} before the
19608 breakpoint condition (before the @code{if}).
19609
19610 @item task @var{taskno}
19611 @cindex Task switching
19612
19613 This command allows to switch to the task referred by @var{taskno}. In
19614 particular, This allows to browse the backtrace of the specified
19615 task. It is advised to switch back to the original task before
19616 continuing execution otherwise the scheduling of the program may be
19617 perturbated.
19618 @end table
19619
19620 @noindent
19621 For more detailed information on the tasking support,
19622 see @cite{Debugging with GDB}.
19623
19624 @node Debugging Generic Units
19625 @section Debugging Generic Units
19626 @cindex Debugging Generic Units
19627 @cindex Generics
19628
19629 @noindent
19630 GNAT always uses code expansion for generic instantiation. This means that
19631 each time an instantiation occurs, a complete copy of the original code is
19632 made, with appropriate substitutions of formals by actuals.
19633
19634 It is not possible to refer to the original generic entities in
19635 @code{GDB}, but it is always possible to debug a particular instance of
19636 a generic, by using the appropriate expanded names. For example, if we have
19637
19638 @smallexample @c ada
19639 @group
19640 @cartouche
19641 procedure g is
19642
19643    generic package k is
19644       procedure kp (v1 : in out integer);
19645    end k;
19646
19647    package body k is
19648       procedure kp (v1 : in out integer) is
19649       begin
19650          v1 := v1 + 1;
19651       end kp;
19652    end k;
19653
19654    package k1 is new k;
19655    package k2 is new k;
19656
19657    var : integer := 1;
19658
19659 begin
19660    k1.kp (var);
19661    k2.kp (var);
19662    k1.kp (var);
19663    k2.kp (var);
19664 end;
19665 @end cartouche
19666 @end group
19667 @end smallexample
19668
19669 @noindent
19670 Then to break on a call to procedure kp in the k2 instance, simply
19671 use the command:
19672
19673 @smallexample
19674 (gdb) break g.k2.kp
19675 @end smallexample
19676
19677 @noindent
19678 When the breakpoint occurs, you can step through the code of the
19679 instance in the normal manner and examine the values of local variables, as for
19680 other units.
19681
19682 @node GNAT Abnormal Termination or Failure to Terminate
19683 @section GNAT Abnormal Termination or Failure to Terminate
19684 @cindex GNAT Abnormal Termination or Failure to Terminate
19685
19686 @noindent
19687 When presented with programs that contain serious errors in syntax
19688 or semantics,
19689 GNAT may on rare occasions  experience problems in operation, such
19690 as aborting with a
19691 segmentation fault or illegal memory access, raising an internal
19692 exception, terminating abnormally, or failing to terminate at all.
19693 In such cases, you can activate
19694 various features of GNAT that can help you pinpoint the construct in your
19695 program that is the likely source of the problem.
19696
19697 The following strategies are presented in increasing order of
19698 difficulty, corresponding to your experience in using GNAT and your
19699 familiarity with compiler internals.
19700
19701 @enumerate
19702 @item
19703 Run @command{gcc} with the @option{-gnatf}. This first
19704 switch causes all errors on a given line to be reported. In its absence,
19705 only the first error on a line is displayed.
19706
19707 The @option{-gnatdO} switch causes errors to be displayed as soon as they
19708 are encountered, rather than after compilation is terminated. If GNAT
19709 terminates prematurely or goes into an infinite loop, the last error
19710 message displayed may help to pinpoint the culprit.
19711
19712 @item
19713 Run @command{gcc} with the @option{^-v (verbose)^/VERBOSE^} switch. In this
19714 mode, @command{gcc} produces ongoing information about the progress of the
19715 compilation and provides the name of each procedure as code is
19716 generated. This switch allows you to find which Ada procedure was being
19717 compiled when it encountered a code generation problem.
19718
19719 @item
19720 @cindex @option{-gnatdc} switch
19721 Run @command{gcc} with the @option{-gnatdc} switch. This is a GNAT specific
19722 switch that does for the front-end what @option{^-v^VERBOSE^} does
19723 for the back end. The system prints the name of each unit,
19724 either a compilation unit or nested unit, as it is being analyzed.
19725 @item
19726 Finally, you can start
19727 @code{gdb} directly on the @code{gnat1} executable. @code{gnat1} is the
19728 front-end of GNAT, and can be run independently (normally it is just
19729 called from @command{gcc}). You can use @code{gdb} on @code{gnat1} as you
19730 would on a C program (but @pxref{The GNAT Debugger GDB} for caveats). The
19731 @code{where} command is the first line of attack; the variable
19732 @code{lineno} (seen by @code{print lineno}), used by the second phase of
19733 @code{gnat1} and by the @command{gcc} backend, indicates the source line at
19734 which the execution stopped, and @code{input_file name} indicates the name of
19735 the source file.
19736 @end enumerate
19737
19738 @node Naming Conventions for GNAT Source Files
19739 @section Naming Conventions for GNAT Source Files
19740
19741 @noindent
19742 In order to examine the workings of the GNAT system, the following
19743 brief description of its organization may be helpful:
19744
19745 @itemize @bullet
19746 @item
19747 Files with prefix @file{^sc^SC^} contain the lexical scanner.
19748
19749 @item
19750 All files prefixed with @file{^par^PAR^} are components of the parser. The
19751 numbers correspond to chapters of the Ada 95 Reference Manual. For example,
19752 parsing of select statements can be found in @file{par-ch9.adb}.
19753
19754 @item
19755 All files prefixed with @file{^sem^SEM^} perform semantic analysis. The
19756 numbers correspond to chapters of the Ada standard. For example, all
19757 issues involving context clauses can be found in @file{sem_ch10.adb}. In
19758 addition, some features of the language require sufficient special processing
19759 to justify their own semantic files: sem_aggr for aggregates, sem_disp for
19760 dynamic dispatching, etc.
19761
19762 @item
19763 All files prefixed with @file{^exp^EXP^} perform normalization and
19764 expansion of the intermediate representation (abstract syntax tree, or AST).
19765 these files use the same numbering scheme as the parser and semantics files.
19766 For example, the construction of record initialization procedures is done in
19767 @file{exp_ch3.adb}.
19768
19769 @item
19770 The files prefixed with @file{^bind^BIND^} implement the binder, which
19771 verifies the consistency of the compilation, determines an order of
19772 elaboration, and generates the bind file.
19773
19774 @item
19775 The files @file{atree.ads} and @file{atree.adb} detail the low-level
19776 data structures used by the front-end.
19777
19778 @item
19779 The files @file{sinfo.ads} and @file{sinfo.adb} detail the structure of
19780 the abstract syntax tree as produced by the parser.
19781
19782 @item
19783 The files @file{einfo.ads} and @file{einfo.adb} detail the attributes of
19784 all entities, computed during semantic analysis.
19785
19786 @item
19787 Library management issues are dealt with in files with prefix
19788 @file{^lib^LIB^}.
19789
19790 @item
19791 @findex Ada
19792 @cindex Annex A
19793 Ada files with the prefix @file{^a-^A-^} are children of @code{Ada}, as
19794 defined in Annex A.
19795
19796 @item
19797 @findex Interfaces
19798 @cindex Annex B
19799 Files with prefix @file{^i-^I-^} are children of @code{Interfaces}, as
19800 defined in Annex B.
19801
19802 @item
19803 @findex System
19804 Files with prefix @file{^s-^S-^} are children of @code{System}. This includes
19805 both language-defined children and GNAT run-time routines.
19806
19807 @item
19808 @findex GNAT
19809 Files with prefix @file{^g-^G-^} are children of @code{GNAT}. These are useful
19810 general-purpose packages, fully documented in their specifications. All
19811 the other @file{.c} files are modifications of common @command{gcc} files.
19812 @end itemize
19813
19814 @node Getting Internal Debugging Information
19815 @section Getting Internal Debugging Information
19816
19817 @noindent
19818 Most compilers have internal debugging switches and modes. GNAT
19819 does also, except GNAT internal debugging switches and modes are not
19820 secret. A summary and full description of all the compiler and binder
19821 debug flags are in the file @file{debug.adb}. You must obtain the
19822 sources of the compiler to see the full detailed effects of these flags.
19823
19824 The switches that print the source of the program (reconstructed from
19825 the internal tree) are of general interest for user programs, as are the
19826 options to print
19827 the full internal tree, and the entity table (the symbol table
19828 information). The reconstructed source provides a readable version of the
19829 program after the front-end has completed analysis and  expansion,
19830 and is useful when studying the performance of specific constructs.
19831 For example, constraint checks are indicated, complex aggregates
19832 are replaced with loops and assignments, and tasking primitives
19833 are replaced with run-time calls.
19834
19835 @node Stack Traceback
19836 @section Stack Traceback
19837 @cindex traceback
19838 @cindex stack traceback
19839 @cindex stack unwinding
19840
19841 @noindent
19842 Traceback is a mechanism to display the sequence of subprogram calls that
19843 leads to a specified execution point in a program. Often (but not always)
19844 the execution point is an instruction at which an exception has been raised.
19845 This mechanism is also known as @i{stack unwinding} because it obtains
19846 its information by scanning the run-time stack and recovering the activation
19847 records of all active subprograms. Stack unwinding is one of the most
19848 important tools for program debugging.
19849
19850 The first entry stored in traceback corresponds to the deepest calling level,
19851 that is to say the subprogram currently executing the instruction
19852 from which we want to obtain the traceback.
19853
19854 Note that there is no runtime performance penalty when stack traceback
19855 is enabled, and no exception is raised during program execution.
19856
19857 @menu
19858 * Non-Symbolic Traceback::
19859 * Symbolic Traceback::
19860 @end menu
19861
19862 @node Non-Symbolic Traceback
19863 @subsection Non-Symbolic Traceback
19864 @cindex traceback, non-symbolic
19865
19866 @noindent
19867 Note: this feature is not supported on all platforms. See
19868 @file{GNAT.Traceback spec in g-traceb.ads} for a complete list of supported
19869 platforms.
19870
19871 @menu
19872 * Tracebacks From an Unhandled Exception::
19873 * Tracebacks From Exception Occurrences (non-symbolic)::
19874 * Tracebacks From Anywhere in a Program (non-symbolic)::
19875 @end menu
19876
19877 @node Tracebacks From an Unhandled Exception
19878 @subsubsection Tracebacks From an Unhandled Exception
19879
19880 @noindent
19881 A runtime non-symbolic traceback is a list of addresses of call instructions.
19882 To enable this feature you must use the @option{-E}
19883 @code{gnatbind}'s option. With this option a stack traceback is stored as part
19884 of exception information. You can retrieve this information using the
19885 @code{addr2line} tool.
19886
19887 Here is a simple example:
19888
19889 @smallexample @c ada
19890 @cartouche
19891 procedure STB is
19892
19893    procedure P1 is
19894    begin
19895       raise Constraint_Error;
19896    end P1;
19897
19898    procedure P2 is
19899    begin
19900       P1;
19901    end P2;
19902
19903 begin
19904    P2;
19905 end STB;
19906 @end cartouche
19907 @end smallexample
19908
19909 @smallexample
19910 $ gnatmake stb -bargs -E
19911 $ stb
19912
19913 Execution terminated by unhandled exception
19914 Exception name: CONSTRAINT_ERROR
19915 Message: stb.adb:5
19916 Call stack traceback locations:
19917 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
19918 @end smallexample
19919
19920 @noindent
19921 As we see the traceback lists a sequence of addresses for the unhandled
19922 exception @code{CONSTRAINT_ERROR} raised in procedure P1. It is easy to
19923 guess that this exception come from procedure P1. To translate these
19924 addresses into the source lines where the calls appear, the
19925 @code{addr2line} tool, described below, is invaluable. The use of this tool
19926 requires the program to be compiled with debug information.
19927
19928 @smallexample
19929 $ gnatmake -g stb -bargs -E
19930 $ stb
19931
19932 Execution terminated by unhandled exception
19933 Exception name: CONSTRAINT_ERROR
19934 Message: stb.adb:5
19935 Call stack traceback locations:
19936 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
19937
19938 $ addr2line --exe=stb 0x401373 0x40138b 0x40139c 0x401335 0x4011c4
19939    0x4011f1 0x77e892a4
19940
19941 00401373 at d:/stb/stb.adb:5
19942 0040138B at d:/stb/stb.adb:10
19943 0040139C at d:/stb/stb.adb:14
19944 00401335 at d:/stb/b~stb.adb:104
19945 004011C4 at /build/.../crt1.c:200
19946 004011F1 at /build/.../crt1.c:222
19947 77E892A4 in ?? at ??:0
19948 @end smallexample
19949
19950 @noindent
19951 The @code{addr2line} tool has several other useful options:
19952
19953 @table @code
19954 @item --functions
19955 to get the function name corresponding to any location
19956
19957 @item --demangle=gnat
19958 to use the gnat decoding mode for the function names. Note that
19959 for binutils version 2.9.x the option is simply @option{--demangle}.
19960 @end table
19961
19962 @smallexample
19963 $ addr2line --exe=stb --functions --demangle=gnat 0x401373 0x40138b
19964    0x40139c 0x401335 0x4011c4 0x4011f1
19965
19966 00401373 in stb.p1 at d:/stb/stb.adb:5
19967 0040138B in stb.p2 at d:/stb/stb.adb:10
19968 0040139C in stb at d:/stb/stb.adb:14
19969 00401335 in main at d:/stb/b~stb.adb:104
19970 004011C4 in <__mingw_CRTStartup> at /build/.../crt1.c:200
19971 004011F1 in <mainCRTStartup> at /build/.../crt1.c:222
19972 @end smallexample
19973
19974 @noindent
19975 From this traceback we can see that the exception was raised in
19976 @file{stb.adb} at line 5, which was reached from a procedure call in
19977 @file{stb.adb} at line 10, and so on. The @file{b~std.adb} is the binder file,
19978 which contains the call to the main program.
19979 @xref{Running gnatbind}. The remaining entries are assorted runtime routines,
19980 and the output will vary from platform to platform.
19981
19982 It is also possible to use @code{GDB} with these traceback addresses to debug
19983 the program. For example, we can break at a given code location, as reported
19984 in the stack traceback:
19985
19986 @smallexample
19987 $ gdb -nw stb
19988 @ifclear vms
19989 @noindent
19990 Furthermore, this feature is not implemented inside Windows DLL. Only
19991 the non-symbolic traceback is reported in this case.
19992 @end ifclear
19993
19994 (gdb) break *0x401373
19995 Breakpoint 1 at 0x401373: file stb.adb, line 5.
19996 @end smallexample
19997
19998 @noindent
19999 It is important to note that the stack traceback addresses
20000 do not change when debug information is included. This is particularly useful
20001 because it makes it possible to release software without debug information (to
20002 minimize object size), get a field report that includes a stack traceback
20003 whenever an internal bug occurs, and then be able to retrieve the sequence
20004 of calls with the same program compiled with debug information.
20005
20006 @node Tracebacks From Exception Occurrences (non-symbolic)
20007 @subsubsection Tracebacks From Exception Occurrences
20008
20009 @noindent
20010 Non-symbolic tracebacks are obtained by using the @option{-E} binder argument.
20011 The stack traceback is attached to the exception information string, and can
20012 be retrieved in an exception handler within the Ada program, by means of the
20013 Ada95 facilities defined in @code{Ada.Exceptions}. Here is a simple example:
20014
20015 @smallexample @c ada
20016 with Ada.Text_IO;
20017 with Ada.Exceptions;
20018
20019 procedure STB is
20020
20021    use Ada;
20022    use Ada.Exceptions;
20023
20024    procedure P1 is
20025       K : Positive := 1;
20026    begin
20027       K := K - 1;
20028    exception
20029       when E : others =>
20030          Text_IO.Put_Line (Exception_Information (E));
20031    end P1;
20032
20033    procedure P2 is
20034    begin
20035       P1;
20036    end P2;
20037
20038 begin
20039    P2;
20040 end STB;
20041 @end smallexample
20042
20043 @noindent
20044 This program will output:
20045
20046 @smallexample
20047 $ stb
20048
20049 Exception name: CONSTRAINT_ERROR
20050 Message: stb.adb:12
20051 Call stack traceback locations:
20052 0x4015e4 0x401633 0x401644 0x401461 0x4011c4 0x4011f1 0x77e892a4
20053 @end smallexample
20054
20055 @node Tracebacks From Anywhere in a Program (non-symbolic)
20056 @subsubsection Tracebacks From Anywhere in a Program
20057
20058 @noindent
20059 It is also possible to retrieve a stack traceback from anywhere in a
20060 program. For this you need to
20061 use the @code{GNAT.Traceback} API. This package includes a procedure called
20062 @code{Call_Chain} that computes a complete stack traceback, as well as useful
20063 display procedures described below. It is not necessary to use the
20064 @option{-E gnatbind} option in this case, because the stack traceback mechanism
20065 is invoked explicitly.
20066
20067 @noindent
20068 In the following example we compute a traceback at a specific location in
20069 the program, and we display it using @code{GNAT.Debug_Utilities.Image} to
20070 convert addresses to strings:
20071
20072 @smallexample @c ada
20073 with Ada.Text_IO;
20074 with GNAT.Traceback;
20075 with GNAT.Debug_Utilities;
20076
20077 procedure STB is
20078
20079    use Ada;
20080    use GNAT;
20081    use GNAT.Traceback;
20082
20083    procedure P1 is
20084       TB  : Tracebacks_Array (1 .. 10);
20085       --  We are asking for a maximum of 10 stack frames.
20086       Len : Natural;
20087       --  Len will receive the actual number of stack frames returned.
20088    begin
20089       Call_Chain (TB, Len);
20090
20091       Text_IO.Put ("In STB.P1 : ");
20092
20093       for K in 1 .. Len loop
20094          Text_IO.Put (Debug_Utilities.Image (TB (K)));
20095          Text_IO.Put (' ');
20096       end loop;
20097
20098       Text_IO.New_Line;
20099    end P1;
20100
20101    procedure P2 is
20102    begin
20103       P1;
20104    end P2;
20105
20106 begin
20107    P2;
20108 end STB;
20109 @end smallexample
20110
20111 @smallexample
20112 $ gnatmake -g stb
20113 $ stb
20114
20115 In STB.P1 : 16#0040_F1E4# 16#0040_14F2# 16#0040_170B# 16#0040_171C#
20116 16#0040_1461# 16#0040_11C4# 16#0040_11F1# 16#77E8_92A4#
20117 @end smallexample
20118
20119 @noindent
20120 You can then get further information by invoking the @code{addr2line}
20121 tool as described earlier (note that the hexadecimal addresses
20122 need to be specified in C format, with a leading ``0x'').
20123
20124 @node Symbolic Traceback
20125 @subsection Symbolic Traceback
20126 @cindex traceback, symbolic
20127
20128 @noindent
20129 A symbolic traceback is a stack traceback in which procedure names are
20130 associated with each code location.
20131
20132 @noindent
20133 Note that this feature is not supported on all platforms. See
20134 @file{GNAT.Traceback.Symbolic spec in g-trasym.ads} for a complete
20135 list of currently supported platforms.
20136
20137 @noindent
20138 Note that the symbolic traceback requires that the program be compiled
20139 with debug information. If it is not compiled with debug information
20140 only the non-symbolic information will be valid.
20141
20142 @menu
20143 * Tracebacks From Exception Occurrences (symbolic)::
20144 * Tracebacks From Anywhere in a Program (symbolic)::
20145 @end menu
20146
20147 @node Tracebacks From Exception Occurrences (symbolic)
20148 @subsubsection Tracebacks From Exception Occurrences
20149
20150 @smallexample @c ada
20151 with Ada.Text_IO;
20152 with GNAT.Traceback.Symbolic;
20153
20154 procedure STB is
20155
20156    procedure P1 is
20157    begin
20158       raise Constraint_Error;
20159    end P1;
20160
20161    procedure P2 is
20162    begin
20163       P1;
20164    end P2;
20165
20166    procedure P3 is
20167    begin
20168       P2;
20169    end P3;
20170
20171 begin
20172    P3;
20173 exception
20174    when E : others =>
20175       Ada.Text_IO.Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
20176 end STB;
20177 @end smallexample
20178
20179 @smallexample
20180 $ gnatmake -g .\stb -bargs -E -largs -lgnat -laddr2line -lintl
20181 $ stb
20182
20183 0040149F in stb.p1 at stb.adb:8
20184 004014B7 in stb.p2 at stb.adb:13
20185 004014CF in stb.p3 at stb.adb:18
20186 004015DD in ada.stb at stb.adb:22
20187 00401461 in main at b~stb.adb:168
20188 004011C4 in __mingw_CRTStartup at crt1.c:200
20189 004011F1 in mainCRTStartup at crt1.c:222
20190 77E892A4 in ?? at ??:0
20191 @end smallexample
20192
20193 @noindent
20194 In the above example the ``.\'' syntax in the @command{gnatmake} command
20195 is currently required by @command{addr2line} for files that are in
20196 the current working directory.
20197 Moreover, the exact sequence of linker options may vary from platform
20198 to platform.
20199 The above @option{-largs} section is for Windows platforms. By contrast,
20200 under Unix there is no need for the @option{-largs} section.
20201 Differences across platforms are due to details of linker implementation.
20202
20203 @node Tracebacks From Anywhere in a Program (symbolic)
20204 @subsubsection Tracebacks From Anywhere in a Program
20205
20206 @noindent
20207 It is possible to get a symbolic stack traceback
20208 from anywhere in a program, just as for non-symbolic tracebacks.
20209 The first step is to obtain a non-symbolic
20210 traceback, and then call @code{Symbolic_Traceback} to compute the symbolic
20211 information. Here is an example:
20212
20213 @smallexample @c ada
20214 with Ada.Text_IO;
20215 with GNAT.Traceback;
20216 with GNAT.Traceback.Symbolic;
20217
20218 procedure STB is
20219
20220    use Ada;
20221    use GNAT.Traceback;
20222    use GNAT.Traceback.Symbolic;
20223
20224    procedure P1 is
20225       TB  : Tracebacks_Array (1 .. 10);
20226       --  We are asking for a maximum of 10 stack frames.
20227       Len : Natural;
20228       --  Len will receive the actual number of stack frames returned.
20229    begin
20230       Call_Chain (TB, Len);
20231       Text_IO.Put_Line (Symbolic_Traceback (TB (1 .. Len)));
20232    end P1;
20233
20234    procedure P2 is
20235    begin
20236       P1;
20237    end P2;
20238
20239 begin
20240    P2;
20241 end STB;
20242 @end smallexample
20243
20244 @ifset vms
20245 @node Compatibility with HP Ada
20246 @chapter Compatibility with HP Ada
20247 @cindex Compatibility
20248
20249 @noindent
20250 This section of the manual compares HP Ada for OpenVMS Alpha and GNAT
20251 OpenVMS Alpha. GNAT achieves a high level of compatibility
20252 with HP Ada, and it should generally be straightforward to port code
20253 from the HP Ada environment to GNAT. However, there are a few language
20254 and implementation differences of which the user must be aware. These
20255 differences are discussed in this section. In
20256 addition, the operating environment and command structure for the
20257 compiler are different, and these differences are also discussed.
20258
20259 Note that this discussion addresses specifically the implementation
20260 of Ada 83 for HP OpenVMS Alpha Systems. In cases where the implementation
20261 of HP Ada differs between OpenVMS Alpha Systems and OpenVMS VAX Systems,
20262 GNAT always follows the Alpha implementation.
20263
20264 @menu
20265 * Ada 95 Compatibility::
20266 * Differences in the Definition of Package System::
20267 * Language-Related Features::
20268 * The Package STANDARD::
20269 * The Package SYSTEM::
20270 * Tasking and Task-Related Features::
20271 * Implementation of Tasks in HP Ada for OpenVMS Alpha Systems::
20272 * Pragmas and Pragma-Related Features::
20273 * Library of Predefined Units::
20274 * Bindings::
20275 * Main Program Definition::
20276 * Implementation-Defined Attributes::
20277 * Compiler and Run-Time Interfacing::
20278 * Program Compilation and Library Management::
20279 * Input-Output::
20280 * Implementation Limits::
20281 @end menu
20282
20283 @node Ada 95 Compatibility
20284 @section Ada 95 Compatibility
20285
20286 @noindent
20287 GNAT is an Ada 95 compiler, and HP Ada is an Ada 83
20288 compiler. Ada 95 is almost completely upwards compatible
20289 with Ada 83, and therefore Ada 83 programs will compile
20290 and run under GNAT with
20291 no changes or only minor changes. The Ada 95 Reference
20292 Manual (ANSI/ISO/IEC-8652:1995) provides details on specific
20293 incompatibilities.
20294
20295 GNAT provides the switch /83 on the GNAT COMPILE command,
20296 as well as the pragma ADA_83, to force the compiler to
20297 operate in Ada 83 mode. This mode does not guarantee complete
20298 conformance to Ada 83, but in practice is sufficient to
20299 eliminate most sources of incompatibilities.
20300 In particular, it eliminates the recognition of the
20301 additional Ada 95 keywords, so that their use as identifiers
20302 in Ada83 program is legal, and handles the cases of packages
20303 with optional bodies, and generics that instantiate unconstrained
20304 types without the use of @code{(<>)}.
20305
20306 @node Differences in the Definition of Package System
20307 @section Differences in the Definition of Package System
20308
20309 @noindent
20310 Both the Ada 95 and Ada 83 reference manuals permit a compiler to add
20311 implementation-dependent declarations to package System. In normal mode,
20312 GNAT does not take advantage of this permission, and the version of System
20313 provided by GNAT exactly matches that in the Ada 95 Reference Manual.
20314
20315 However, HP Ada adds an extensive set of declarations to package System,
20316 as fully documented in the HP Ada manuals. To minimize changes required
20317 for programs that make use of these extensions, GNAT provides the pragma
20318 Extend_System for extending the definition of package System. By using:
20319
20320 @smallexample @c ada
20321 @group
20322 @cartouche
20323 pragma Extend_System (Aux_DEC);
20324 @end cartouche
20325 @end group
20326 @end smallexample
20327
20328 @noindent
20329 The set of definitions in System is extended to include those in package
20330 @code{System.Aux_DEC}.
20331 These definitions are incorporated directly into package
20332 System, as though they had been declared there in the first place. For a
20333 list of the declarations added, see the specification of this package,
20334 which can be found in the file @code{s-auxdec.ads} in the GNAT library.
20335 The pragma Extend_System is a configuration pragma, which means that
20336 it can be placed in the file @file{gnat.adc}, so that it will automatically
20337 apply to all subsequent compilations. See the section on Configuration
20338 Pragmas for further details.
20339
20340 An alternative approach that avoids the use of the non-standard
20341 Extend_System pragma is to add a context clause to the unit that
20342 references these facilities:
20343
20344 @smallexample @c ada
20345 @group
20346 @cartouche
20347 with System.Aux_DEC;
20348 use  System.Aux_DEC;
20349 @end cartouche
20350 @end group
20351 @end smallexample
20352
20353 @noindent
20354 The effect is not quite semantically identical to incorporating
20355 the declarations directly into package @code{System},
20356 but most programs will not notice a difference
20357 unless they use prefix notation (e.g. @code{System.Integer_8})
20358 to reference the
20359 entities directly in package @code{System}.
20360 For units containing such references,
20361 the prefixes must either be removed, or the pragma @code{Extend_System}
20362 must be used.
20363
20364 @node Language-Related Features
20365 @section Language-Related Features
20366
20367 @noindent
20368 The following sections highlight differences in types,
20369 representations of types, operations, alignment, and
20370 related topics.
20371
20372 @menu
20373 * Integer Types and Representations::
20374 * Floating-Point Types and Representations::
20375 * Pragmas Float_Representation and Long_Float::
20376 * Fixed-Point Types and Representations::
20377 * Record and Array Component Alignment::
20378 * Address Clauses::
20379 * Other Representation Clauses::
20380 @end menu
20381
20382 @node Integer Types and Representations
20383 @subsection Integer Types and Representations
20384
20385 @noindent
20386 The set of predefined integer types is identical in HP Ada and GNAT.
20387 Furthermore the representation of these integer types is also identical,
20388 including the capability of size clauses forcing biased representation.
20389
20390 In addition,
20391 HP Ada for OpenVMS Alpha systems has defined the
20392 following additional integer types in package System:
20393
20394 @itemize @bullet
20395
20396 @item
20397 INTEGER_8
20398
20399 @item
20400 INTEGER_16
20401
20402 @item
20403 INTEGER_32
20404
20405 @item
20406 INTEGER_64
20407
20408 @item
20409 LARGEST_INTEGER
20410 @end itemize
20411
20412 @noindent
20413 When using GNAT, the first four of these types may be obtained from the
20414 standard Ada 95 package @code{Interfaces}.
20415 Alternatively, by use of the pragma
20416 @code{Extend_System}, identical
20417 declarations can be referenced directly in package @code{System}.
20418 On both GNAT and HP Ada, the maximum integer size is 64 bits.
20419
20420 @node Floating-Point Types and Representations
20421 @subsection Floating-Point Types and Representations
20422 @cindex Floating-Point types
20423
20424 @noindent
20425 The set of predefined floating-point types is identical in HP Ada and GNAT.
20426 Furthermore the representation of these floating-point
20427 types is also identical. One important difference is that the default
20428 representation for HP Ada is VAX_Float, but the default representation
20429 for GNAT is IEEE.
20430
20431 Specific types may be declared to be VAX_Float or IEEE, using the pragma
20432 @code{Float_Representation} as described in the HP Ada documentation.
20433 For example, the declarations:
20434
20435 @smallexample @c ada
20436 @group
20437 @cartouche
20438 type F_Float is digits 6;
20439 pragma Float_Representation (VAX_Float, F_Float);
20440 @end cartouche
20441 @end group
20442 @end smallexample
20443
20444 @noindent
20445 declare a type F_Float that will be represented in VAX_Float format.
20446 This set of declarations actually appears in System.Aux_DEC, which provides
20447 the full set of additional floating-point declarations provided in
20448 the HP Ada version of package
20449 System. This and similar declarations may be accessed in a user program
20450 by using pragma @code{Extend_System}. The use of this
20451 pragma, and the related pragma @code{Long_Float} is described in further
20452 detail in the following section.
20453
20454 @node Pragmas Float_Representation and Long_Float
20455 @subsection Pragmas Float_Representation and Long_Float
20456
20457 @noindent
20458 HP Ada provides the pragma @code{Float_Representation}, which
20459 acts as a program library switch to allow control over
20460 the internal representation chosen for the predefined
20461 floating-point types declared in the package @code{Standard}.
20462 The format of this pragma is as follows:
20463
20464 @smallexample
20465 @group
20466 @cartouche
20467 @b{pragma} @code{Float_Representation}(VAX_Float | IEEE_Float);
20468 @end cartouche
20469 @end group
20470 @end smallexample
20471
20472 @noindent
20473 This pragma controls the representation of floating-point
20474 types as follows:
20475
20476 @itemize @bullet
20477 @item
20478 @code{VAX_Float} specifies that floating-point
20479 types are represented by default with the VAX system hardware types
20480 F-floating, D-floating, G-floating. Note that the H-floating
20481 type was available only on Vax systems, and is not available
20482 in either HP Ada or GNAT for Alpha systems.
20483
20484 @item
20485 @code{IEEE_Float} specifies that floating-point
20486 types are represented by default with the IEEE single and
20487 double floating-point types.
20488 @end itemize
20489
20490 @noindent
20491 GNAT provides an identical implementation of the pragma
20492 @code{Float_Representation}, except that it functions as a
20493 configuration pragma, as defined by Ada 95. Note that the
20494 notion of configuration pragma corresponds closely to the
20495 HP Ada notion of a program library switch.
20496
20497 When no pragma is used in GNAT, the default is IEEE_Float, which is different
20498 from HP Ada 83, where the default is VAX_Float. In addition, the
20499 predefined libraries in GNAT are built using IEEE_Float, so it is not
20500 advisable to change the format of numbers passed to standard library
20501 routines, and if necessary explicit type conversions may be needed.
20502
20503 The use of IEEE_Float is recommended in GNAT since it is more efficient,
20504 and (given that it conforms to an international standard) potentially more
20505 portable. The situation in which VAX_Float may be useful is in interfacing
20506 to existing code and data that expects the use of VAX_Float. There are
20507 two possibilities here. If the requirement for the use of VAX_Float is
20508 localized, then the best approach is to use the predefined VAX_Float
20509 types in package @code{System}, as extended by
20510 @code{Extend_System}. For example, use @code{System.F_Float}
20511 to specify the 32-bit @code{F-Float} format.
20512
20513 Alternatively, if an entire program depends heavily on the use of
20514 the @code{VAX_Float} and in particular assumes that the types in
20515 package @code{Standard} are in @code{Vax_Float} format, then it
20516 may be desirable to reconfigure GNAT to assume Vax_Float by default.
20517 This is done by using the GNAT LIBRARY command to rebuild the library, and
20518 then using the general form of the @code{Float_Representation}
20519 pragma to ensure that this default format is used throughout.
20520 The form of the GNAT LIBRARY command is:
20521
20522 @smallexample
20523 GNAT LIBRARY /CONFIG=@i{file} /CREATE=@i{directory}
20524 @end smallexample
20525
20526 @noindent
20527 where @i{file} contains the new configuration pragmas
20528 and @i{directory} is the directory to be created to contain
20529 the new library.
20530
20531 @noindent
20532 On OpenVMS systems, HP Ada provides the pragma @code{Long_Float}
20533 to allow control over the internal representation chosen
20534 for the predefined type @code{Long_Float} and for floating-point
20535 type declarations with digits specified in the range 7 .. 15.
20536 The format of this pragma is as follows:
20537
20538 @smallexample @c ada
20539 @cartouche
20540 pragma Long_Float (D_FLOAT | G_FLOAT);
20541 @end cartouche
20542 @end smallexample
20543
20544 @node Fixed-Point Types and Representations
20545 @subsection Fixed-Point Types and Representations
20546
20547 @noindent
20548 On HP Ada for OpenVMS Alpha systems, rounding is
20549 away from zero for both positive and negative numbers.
20550 Therefore, +0.5 rounds to 1 and -0.5 rounds to -1.
20551
20552 On GNAT for OpenVMS Alpha, the results of operations
20553 on fixed-point types are in accordance with the Ada 95
20554 rules. In particular, results of operations on decimal
20555 fixed-point types are truncated.
20556
20557 @node Record and Array Component Alignment
20558 @subsection Record and Array Component Alignment
20559
20560 @noindent
20561 On HP Ada for OpenVMS Alpha, all non composite components
20562 are aligned on natural boundaries. For example, 1-byte
20563 components are aligned on byte boundaries, 2-byte
20564 components on 2-byte boundaries, 4-byte components on 4-byte
20565 byte boundaries, and so on. The OpenVMS Alpha hardware
20566 runs more efficiently with naturally aligned data.
20567
20568 ON GNAT for OpenVMS Alpha, alignment rules are compatible
20569 with HP Ada for OpenVMS Alpha.
20570
20571 @node Address Clauses
20572 @subsection Address Clauses
20573
20574 @noindent
20575 In HP Ada and GNAT, address clauses are supported for
20576 objects and imported subprograms.
20577 The predefined type @code{System.Address} is a private type
20578 in both compilers, with the same representation (it is simply
20579 a machine pointer). Addition, subtraction, and comparison
20580 operations are available in the standard Ada 95 package
20581 @code{System.Storage_Elements}, or in package @code{System}
20582 if it is extended to include @code{System.Aux_DEC} using a
20583 pragma @code{Extend_System} as previously described.
20584
20585 Note that code that with's both this extended package @code{System}
20586 and the package @code{System.Storage_Elements} should not @code{use}
20587 both packages, or ambiguities will result. In general it is better
20588 not to mix these two sets of facilities. The Ada 95 package was
20589 designed specifically to provide the kind of features that HP Ada
20590 adds directly to package @code{System}.
20591
20592 GNAT is compatible with HP Ada in its handling of address
20593 clauses, except for some limitations in
20594 the form of address clauses for composite objects with
20595 initialization. Such address clauses are easily replaced
20596 by the use of an explicitly-defined constant as described
20597 in the Ada 95 Reference Manual (13.1(22)). For example, the sequence
20598 of declarations:
20599
20600 @smallexample @c ada
20601 @cartouche
20602 X, Y : Integer := Init_Func;
20603 Q : String (X .. Y) := "abc";
20604 ...
20605 for Q'Address use Compute_Address;
20606 @end cartouche
20607 @end smallexample
20608
20609 @noindent
20610 will be rejected by GNAT, since the address cannot be computed at the time
20611 that Q is declared. To achieve the intended effect, write instead:
20612
20613 @smallexample @c ada
20614 @group
20615 @cartouche
20616 X, Y : Integer := Init_Func;
20617 Q_Address : constant Address := Compute_Address;
20618 Q : String (X .. Y) := "abc";
20619 ...
20620 for Q'Address use Q_Address;
20621 @end cartouche
20622 @end group
20623 @end smallexample
20624
20625 @noindent
20626 which will be accepted by GNAT (and other Ada 95 compilers), and is also
20627 backwards compatible with Ada 83. A fuller description of the restrictions
20628 on address specifications is found in the GNAT Reference Manual.
20629
20630 @node Other Representation Clauses
20631 @subsection Other Representation Clauses
20632
20633 @noindent
20634 GNAT supports in a compatible manner all the representation
20635 clauses supported by HP Ada. In addition, it
20636 supports representation clause forms that are new in Ada 95
20637 including COMPONENT_SIZE and SIZE clauses for objects.
20638
20639 @node The Package STANDARD
20640 @section The Package STANDARD
20641
20642 @noindent
20643 The package STANDARD, as implemented by HP Ada, is fully
20644 described in the Reference Manual for the Ada Programming
20645 Language (ANSI/MIL-STD-1815A-1983) and in the HP Ada
20646 Language Reference Manual. As implemented by GNAT, the
20647 package STANDARD is described in the Ada 95 Reference
20648 Manual.
20649
20650 In addition, HP Ada supports the Latin-1 character set in
20651 the type CHARACTER. GNAT supports the Latin-1 character set
20652 in the type CHARACTER and also Unicode (ISO 10646 BMP) in
20653 the type WIDE_CHARACTER.
20654
20655 The floating-point types supported by GNAT are those
20656 supported by HP Ada, but defaults are different, and are controlled by
20657 pragmas. See @ref{Floating-Point Types and Representations} for details.
20658
20659 @node The Package SYSTEM
20660 @section The Package SYSTEM
20661
20662 @noindent
20663 HP Ada provides a system-specific version of the package
20664 SYSTEM for each platform on which the language ships.
20665 For the complete specification of the package SYSTEM, see
20666 Appendix F of the HP Ada Language Reference Manual.
20667
20668 On HP Ada, the package SYSTEM includes the following conversion functions:
20669 @itemize @bullet
20670 @item TO_ADDRESS(INTEGER)
20671
20672 @item  TO_ADDRESS(UNSIGNED_LONGWORD)
20673
20674 @item  TO_ADDRESS(universal_integer)
20675
20676 @item  TO_INTEGER(ADDRESS)
20677
20678 @item  TO_UNSIGNED_LONGWORD(ADDRESS)
20679
20680 @item  Function IMPORT_VALUE return UNSIGNED_LONGWORD and the
20681                  functions IMPORT_ADDRESS and IMPORT_LARGEST_VALUE
20682 @end itemize
20683
20684 @noindent
20685 By default, GNAT supplies a version of SYSTEM that matches
20686 the definition given in the Ada 95 Reference Manual.
20687 This
20688 is a subset of the HP system definitions, which is as
20689 close as possible to the original definitions. The only difference
20690 is that the definition of SYSTEM_NAME is different:
20691
20692 @smallexample @c ada
20693 @group
20694 @cartouche
20695 type Name is (SYSTEM_NAME_GNAT);
20696 System_Name : constant Name := SYSTEM_NAME_GNAT;
20697 @end cartouche
20698 @end group
20699 @end smallexample
20700
20701 @noindent
20702 Also, GNAT adds the new Ada 95 declarations for
20703 BIT_ORDER and DEFAULT_BIT_ORDER.
20704
20705 However, the use of the following pragma causes GNAT
20706 to extend the definition of package SYSTEM so that it
20707 encompasses the full set of HP-specific extensions,
20708 including the functions listed above:
20709
20710 @smallexample @c ada
20711 @cartouche
20712 pragma Extend_System (Aux_DEC);
20713 @end cartouche
20714 @end smallexample
20715
20716 @noindent
20717 The pragma Extend_System is a configuration pragma that
20718 is most conveniently placed in the @file{gnat.adc} file. See the
20719 GNAT Reference Manual for further details.
20720
20721 HP Ada does not allow the recompilation of the package
20722 SYSTEM. Instead HP Ada provides several pragmas (SYSTEM_
20723 NAME, STORAGE_UNIT, and MEMORY_SIZE) to modify values in
20724 the package SYSTEM. On OpenVMS Alpha systems, the pragma
20725 SYSTEM_NAME takes the enumeration literal OPENVMS_AXP as
20726 its single argument.
20727
20728 GNAT does permit the recompilation of package SYSTEM using
20729 a special switch (@option{-gnatg}) and this switch can be used if
20730 it is necessary to modify the definitions in SYSTEM. GNAT does
20731 not permit the specification of SYSTEM_NAME, STORAGE_UNIT
20732 or MEMORY_SIZE by any other means.
20733
20734 On GNAT systems, the pragma SYSTEM_NAME takes the
20735 enumeration literal SYSTEM_NAME_GNAT.
20736
20737 The definitions provided by the use of
20738
20739 @smallexample @c ada
20740 pragma Extend_System (AUX_Dec);
20741 @end smallexample
20742
20743 @noindent
20744 are virtually identical to those provided by the HP Ada 83 package
20745 System. One important difference is that the name of the TO_ADDRESS
20746 function for type UNSIGNED_LONGWORD is changed to TO_ADDRESS_LONG.
20747 See the GNAT Reference manual for a discussion of why this change was
20748 necessary.
20749
20750 @noindent
20751 The version of TO_ADDRESS taking a universal integer argument is in fact
20752 an extension to Ada 83 not strictly compatible with the reference manual.
20753 In GNAT, we are constrained to be exactly compatible with the standard,
20754 and this means we cannot provide this capability. In HP Ada 83, the
20755 point of this definition is to deal with a call like:
20756
20757 @smallexample @c ada
20758 TO_ADDRESS (16#12777#);
20759 @end smallexample
20760
20761 @noindent
20762 Normally, according to the Ada 83 standard, one would expect this to be
20763 ambiguous, since it matches both the INTEGER and UNSIGNED_LONGWORD forms
20764 of TO_ADDRESS. However, in HP Ada 83, there is no ambiguity, since the
20765 definition using universal_integer takes precedence.
20766
20767 In GNAT, since the version with universal_integer cannot be supplied, it is
20768 not possible to be 100% compatible. Since there are many programs using
20769 numeric constants for the argument to TO_ADDRESS, the decision in GNAT was
20770 to change the name of the function in the UNSIGNED_LONGWORD case, so the
20771 declarations provided in the GNAT version of AUX_Dec are:
20772
20773 @smallexample @c ada
20774 function To_Address (X : Integer) return Address;
20775 pragma Pure_Function (To_Address);
20776
20777 function To_Address_Long (X : Unsigned_Longword) return Address;
20778 pragma Pure_Function (To_Address_Long);
20779 @end smallexample
20780
20781 @noindent
20782 This means that programs using TO_ADDRESS for UNSIGNED_LONGWORD must
20783 change the name to TO_ADDRESS_LONG.
20784
20785 @node Tasking and Task-Related Features
20786 @section Tasking and Task-Related Features
20787
20788 @noindent
20789 The concepts relevant to a comparison of tasking on GNAT
20790 and on HP Ada for OpenVMS Alpha systems are discussed in
20791 the following sections.
20792
20793 For detailed information on concepts related to tasking in
20794 HP Ada, see the HP Ada Language Reference Manual and the
20795 relevant run-time reference manual.
20796
20797 @node Implementation of Tasks in HP Ada for OpenVMS Alpha Systems
20798 @section Implementation of Tasks in HP Ada for OpenVMS Alpha Systems
20799
20800 @noindent
20801 On OpenVMS Alpha systems, each Ada task (except a passive
20802 task) is implemented as a single stream of execution
20803 that is created and managed by the kernel. On these
20804 systems, HP Ada tasking support is based on DECthreads,
20805 an implementation of the POSIX standard for threads.
20806
20807 Also, on OpenVMS Alpha systems, HP Ada tasks and foreign
20808 code that calls DECthreads routines can be used together.
20809 The interaction between Ada tasks and DECthreads routines
20810 can have some benefits. For example when on OpenVMS Alpha,
20811 HP Ada can call C code that is already threaded.
20812 GNAT on OpenVMS Alpha uses the facilities of DECthreads,
20813 and Ada tasks are mapped to threads.
20814
20815 @menu
20816 * Assigning Task IDs::
20817 * Task IDs and Delays::
20818 * Task-Related Pragmas::
20819 * Scheduling and Task Priority::
20820 * The Task Stack::
20821 * External Interrupts::
20822 @end menu
20823
20824 @node Assigning Task IDs
20825 @subsection Assigning Task IDs
20826
20827 @noindent
20828 The HP Ada Run-Time Library always assigns %TASK 1 to
20829 the environment task that executes the main program. On
20830 OpenVMS Alpha systems, %TASK 0 is often used for tasks
20831 that have been created but are not yet activated.
20832
20833 On OpenVMS Alpha systems, task IDs are assigned at
20834 activation. On GNAT systems, task IDs are also assigned at
20835 task creation but do not have the same form or values as
20836 task ID values in HP Ada. There is no null task, and the
20837 environment task does not have a specific task ID value.
20838
20839 @node Task IDs and Delays
20840 @subsection Task IDs and Delays
20841
20842 @noindent
20843 On OpenVMS Alpha systems, tasking delays are implemented
20844 using Timer System Services. The Task ID is used for the
20845 identification of the timer request (the REQIDT parameter).
20846 If Timers are used in the application take care not to use
20847 0 for the identification, because cancelling such a timer
20848 will cancel all timers and may lead to unpredictable results.
20849
20850 @node Task-Related Pragmas
20851 @subsection Task-Related Pragmas
20852
20853 @noindent
20854 Ada supplies the pragma TASK_STORAGE, which allows
20855 specification of the size of the guard area for a task
20856 stack. (The guard area forms an area of memory that has no
20857 read or write access and thus helps in the detection of
20858 stack overflow.) On OpenVMS Alpha systems, if the pragma
20859 TASK_STORAGE specifies a value of zero, a minimal guard
20860 area is created. In the absence of a pragma TASK_STORAGE, a default guard
20861 area is created.
20862
20863 GNAT supplies the following task-related pragmas:
20864
20865 @itemize @bullet
20866 @item  TASK_INFO
20867
20868               This pragma appears within a task definition and
20869               applies to the task in which it appears. The argument
20870               must be of type SYSTEM.TASK_INFO.TASK_INFO_TYPE.
20871
20872 @item  TASK_STORAGE
20873
20874               GNAT implements pragma TASK_STORAGE in the same way as
20875               HP Ada.
20876               Both HP Ada and GNAT supply the pragmas PASSIVE,
20877               SUPPRESS, and VOLATILE.
20878 @end itemize
20879 @node Scheduling and Task Priority
20880 @subsection Scheduling and Task Priority
20881
20882 @noindent
20883 HP Ada implements the Ada language requirement that
20884 when two tasks are eligible for execution and they have
20885 different priorities, the lower priority task does not
20886 execute while the higher priority task is waiting. The HP
20887 Ada Run-Time Library keeps a task running until either the
20888 task is suspended or a higher priority task becomes ready.
20889
20890 On OpenVMS Alpha systems, the default strategy is round-
20891 robin with preemption. Tasks of equal priority take turns
20892 at the processor. A task is run for a certain period of
20893 time and then placed at the rear of the ready queue for
20894 its priority level.
20895
20896 HP Ada provides the implementation-defined pragma TIME_SLICE,
20897 which can be used to enable or disable round-robin
20898 scheduling of tasks with the same priority.
20899 See the relevant HP Ada run-time reference manual for
20900 information on using the pragmas to control HP Ada task
20901 scheduling.
20902
20903 GNAT follows the scheduling rules of Annex D (real-time
20904 Annex) of the Ada 95 Reference Manual. In general, this
20905 scheduling strategy is fully compatible with HP Ada
20906 although it provides some additional constraints (as
20907 fully documented in Annex D).
20908 GNAT implements time slicing control in a manner compatible with
20909 HP Ada 83, by means of the pragma Time_Slice, whose semantics are identical
20910 to the HP Ada 83 pragma of the same name.
20911 Note that it is not possible to mix GNAT tasking and
20912 HP Ada 83 tasking in the same program, since the two run times are
20913 not compatible.
20914
20915 @node The Task Stack
20916 @subsection The Task Stack
20917
20918 @noindent
20919 In HP Ada, a task stack is allocated each time a
20920 non passive task is activated. As soon as the task is
20921 terminated, the storage for the task stack is deallocated.
20922 If you specify a size of zero (bytes) with T'STORAGE_SIZE,
20923 a default stack size is used. Also, regardless of the size
20924 specified, some additional space is allocated for task
20925 management purposes. On OpenVMS Alpha systems, at least
20926 one page is allocated.
20927
20928 GNAT handles task stacks in a similar manner. According to
20929 the Ada 95 rules, it provides the pragma STORAGE_SIZE as
20930 an alternative method for controlling the task stack size.
20931 The specification of the attribute T'STORAGE_SIZE is also
20932 supported in a manner compatible with HP Ada.
20933
20934 @node External Interrupts
20935 @subsection External Interrupts
20936
20937 @noindent
20938 On HP Ada, external interrupts can be associated with task entries.
20939 GNAT is compatible with HP Ada in its handling of external interrupts.
20940
20941 @node Pragmas and Pragma-Related Features
20942 @section Pragmas and Pragma-Related Features
20943
20944 @noindent
20945 Both HP Ada and GNAT supply all language-defined pragmas
20946 as specified by the Ada 83 standard. GNAT also supplies all
20947 language-defined pragmas specified in the Ada 95 Reference Manual.
20948 In addition, GNAT implements the implementation-defined pragmas
20949 from HP Ada 83.
20950
20951 @itemize @bullet
20952 @item  AST_ENTRY
20953
20954 @item  COMMON_OBJECT
20955
20956 @item  COMPONENT_ALIGNMENT
20957
20958 @item  EXPORT_EXCEPTION
20959
20960 @item  EXPORT_FUNCTION
20961
20962 @item  EXPORT_OBJECT
20963
20964 @item  EXPORT_PROCEDURE
20965
20966 @item  EXPORT_VALUED_PROCEDURE
20967
20968 @item  FLOAT_REPRESENTATION
20969
20970 @item  IDENT
20971
20972 @item  IMPORT_EXCEPTION
20973
20974 @item  IMPORT_FUNCTION
20975
20976 @item  IMPORT_OBJECT
20977
20978 @item  IMPORT_PROCEDURE
20979
20980 @item  IMPORT_VALUED_PROCEDURE
20981
20982 @item  INLINE_GENERIC
20983
20984 @item  INTERFACE_NAME
20985
20986 @item  LONG_FLOAT
20987
20988 @item  MAIN_STORAGE
20989
20990 @item  PASSIVE
20991
20992 @item  PSET_OBJECT
20993
20994 @item  SHARE_GENERIC
20995
20996 @item  SUPPRESS_ALL
20997
20998 @item  TASK_STORAGE
20999
21000 @item  TIME_SLICE
21001
21002 @item  TITLE
21003 @end itemize
21004
21005 @noindent
21006 These pragmas are all fully implemented, with the exception of @code{Title},
21007 @code{Passive}, and @code{Share_Generic}, which are
21008 recognized, but which have no
21009 effect in GNAT. The effect of @code{Passive} may be obtained by the
21010 use of protected objects in Ada 95. In GNAT, all generics are inlined.
21011
21012 Unlike HP Ada, the GNAT 'EXPORT_@i{subprogram}' pragmas require
21013 a separate subprogram specification which must appear before the
21014 subprogram body.
21015
21016 GNAT also supplies a number of implementation-defined pragmas as follows:
21017 @itemize @bullet
21018 @item  C_PASS_BY_COPY
21019
21020 @item  EXTEND_SYSTEM
21021
21022 @item  SOURCE_FILE_NAME
21023
21024 @item  UNSUPPRESS
21025
21026 @item  WARNINGS
21027
21028 @item  ABORT_DEFER
21029
21030 @item  ADA_83
21031
21032 @item  ADA_95
21033
21034 @item  ADA_05
21035
21036 @item  ANNOTATE
21037
21038 @item  ASSERT
21039
21040 @item  CPP_CLASS
21041
21042 @item  CPP_CONSTRUCTOR
21043
21044 @item  CPP_DESTRUCTOR
21045
21046 @item  CPP_VIRTUAL
21047
21048 @item  CP_VTABLE
21049
21050 @item  DEBUG
21051
21052 @item  LINKER_ALIAS
21053
21054 @item  LINKER_SECTION
21055
21056 @item  MACHINE_ATTRIBUTE
21057
21058 @item  NO_RETURN
21059
21060 @item  PURE_FUNCTION
21061
21062 @item  SOURCE_REFERENCE
21063
21064 @item  TASK_INFO
21065
21066 @item  UNCHECKED_UNION
21067
21068 @item  UNIMPLEMENTED_UNIT
21069
21070 @item  UNIVERSAL_DATA
21071
21072 @item  WEAK_EXTERNAL
21073 @end itemize
21074
21075 @noindent
21076 For full details on these GNAT implementation-defined pragmas, see
21077 the GNAT Reference Manual.
21078
21079 @menu
21080 * Restrictions on the Pragma INLINE::
21081 * Restrictions on the Pragma INTERFACE::
21082 * Restrictions on the Pragma SYSTEM_NAME::
21083 @end menu
21084
21085 @node Restrictions on the Pragma INLINE
21086 @subsection Restrictions on the Pragma INLINE
21087
21088 @noindent
21089 HP Ada applies the following restrictions to the pragma INLINE:
21090 @itemize @bullet
21091 @item  Parameters cannot be a task type.
21092
21093 @item  Function results cannot be task types, unconstrained
21094 array types, or unconstrained types with discriminants.
21095
21096 @item  Bodies cannot declare the following:
21097 @itemize @bullet
21098 @item  Subprogram body or stub (imported subprogram is allowed)
21099
21100 @item  Tasks
21101
21102 @item  Generic declarations
21103
21104 @item  Instantiations
21105
21106 @item  Exceptions
21107
21108 @item  Access types (types derived from access types allowed)
21109
21110 @item  Array or record types
21111
21112 @item  Dependent tasks
21113
21114 @item  Direct recursive calls of subprogram or containing
21115 subprogram, directly or via a renaming
21116
21117 @end itemize
21118 @end itemize
21119
21120 @noindent
21121 In GNAT, the only restriction on pragma INLINE is that the
21122 body must occur before the call if both are in the same
21123 unit, and the size must be appropriately small. There are
21124 no other specific restrictions which cause subprograms to
21125 be incapable of being inlined.
21126
21127 @node  Restrictions on the Pragma INTERFACE
21128 @subsection  Restrictions on the Pragma INTERFACE
21129
21130 @noindent
21131 The following lists and describes the restrictions on the
21132 pragma INTERFACE on HP Ada and GNAT:
21133 @itemize @bullet
21134 @item  Languages accepted: Ada, Bliss, C, Fortran, Default.
21135 Default is the default on OpenVMS Alpha systems.
21136
21137 @item  Parameter passing: Language specifies default
21138 mechanisms but can be overridden with an EXPORT pragma.
21139
21140 @itemize @bullet
21141 @item  Ada: Use internal Ada rules.
21142
21143 @item  Bliss, C: Parameters must be mode @code{in}; cannot be
21144 record or task type. Result cannot be a string, an
21145 array, or a record.
21146
21147 @item  Fortran: Parameters cannot be a task. Result cannot
21148 be a string, an array, or a record.
21149 @end itemize
21150 @end itemize
21151
21152 @noindent
21153 GNAT is entirely upwards compatible with HP Ada, and in addition allows
21154 record parameters for all languages.
21155
21156 @node  Restrictions on the Pragma SYSTEM_NAME
21157 @subsection  Restrictions on the Pragma SYSTEM_NAME
21158
21159 @noindent
21160 For HP Ada for OpenVMS Alpha, the enumeration literal
21161 for the type NAME is OPENVMS_AXP. In GNAT, the enumeration
21162 literal for the type NAME is SYSTEM_NAME_GNAT.
21163
21164 @node  Library of Predefined Units
21165 @section  Library of Predefined Units
21166
21167 @noindent
21168 A library of predefined units is provided as part of the
21169 HP Ada and GNAT implementations. HP Ada does not provide
21170 the package MACHINE_CODE but instead recommends importing
21171 assembler code.
21172
21173 The GNAT versions of the HP Ada Run-Time Library (ADA$PREDEFINED:)
21174 units are taken from the OpenVMS Alpha version, not the OpenVMS VAX
21175 version. During GNAT installation, the HP Ada Predefined
21176 Library units are copied into the GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
21177 (aka DECLIB) directory and patched to remove Ada 95 incompatibilities
21178 and to make them interoperable with GNAT, @pxref{Changes to DECLIB}
21179 for details.
21180
21181 The GNAT RTL is contained in
21182 the GNU:[LIB.OPENVMS7_x.2_8_x.ADALIB] (aka ADALIB) directory and
21183 the default search path is set up to find DECLIB units in preference
21184 to ADALIB units with the same name (TEXT_IO, SEQUENTIAL_IO, and DIRECT_IO,
21185 for example).
21186
21187 However, it is possible to change the default so that the
21188 reverse is true, or even to mix them using child package
21189 notation. The HP Ada 83 units are available as DEC.xxx where xxx
21190 is the package name, and the Ada units are available in the
21191 standard manner defined for Ada 95, that is to say as DEC.xxx. To
21192 change the default, set ADA_INCLUDE_PATH and ADA_OBJECTS_PATH
21193 appropriately. For example, to change the default to use the Ada95
21194 versions do:
21195
21196 @smallexample
21197 $ DEFINE ADA_INCLUDE_PATH GNU:[LIB.OPENVMS7_1.2_8_1.ADAINCLUDE],-
21198                           GNU:[LIB.OPENVMS7_1.2_8_1.DECLIB]
21199 $ DEFINE ADA_OBJECTS_PATH GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB],-
21200                           GNU:[LIB.OPENVMS7_1.2_8_1.DECLIB]
21201 @end smallexample
21202
21203 @menu
21204 * Changes to DECLIB::
21205 @end menu
21206
21207 @node Changes to DECLIB
21208 @subsection Changes to DECLIB
21209
21210 @noindent
21211 The changes made to the HP Ada predefined library for GNAT and Ada 95
21212 compatibility are minor and include the following:
21213
21214 @itemize @bullet
21215 @item  Adjusting the location of pragmas and record representation
21216 clauses to obey Ada 95 rules
21217
21218 @item  Adding the proper notation to generic formal parameters
21219 that take unconstrained types in instantiation
21220
21221 @item  Adding pragma ELABORATE_BODY to package specifications
21222 that have package bodies not otherwise allowed
21223
21224 @item  Occurrences of the identifier @code{"PROTECTED"} are renamed to
21225 @code{"PROTECTD"}.
21226 Currently these are found only in the STARLET package spec.
21227 @end itemize
21228
21229 @noindent
21230 None of the above changes is visible to users.
21231
21232 @node Bindings
21233 @section Bindings
21234
21235 @noindent
21236 On OpenVMS Alpha, HP Ada provides the following strongly-typed bindings:
21237 @itemize @bullet
21238
21239 @item  Command Language Interpreter (CLI interface)
21240
21241 @item  DECtalk Run-Time Library (DTK interface)
21242
21243 @item  Librarian utility routines (LBR interface)
21244
21245 @item  General Purpose Run-Time Library (LIB interface)
21246
21247 @item  Math Run-Time Library (MTH interface)
21248
21249 @item  National Character Set Run-Time Library (NCS interface)
21250
21251 @item  Compiled Code Support Run-Time Library (OTS interface)
21252
21253 @item  Parallel Processing Run-Time Library (PPL interface)
21254
21255 @item  Screen Management Run-Time Library (SMG interface)
21256
21257 @item  Sort Run-Time Library (SOR interface)
21258
21259 @item  String Run-Time Library (STR interface)
21260
21261 @item STARLET System Library
21262 @findex Starlet
21263
21264 @item  X Window System Version 11R4 and 11R5 (X, XLIB interface)
21265
21266 @item  X Windows Toolkit (XT interface)
21267
21268 @item  X/Motif Version 1.1.3 and 1.2 (XM interface)
21269 @end itemize
21270
21271 @noindent
21272 GNAT provides implementations of these HP bindings in the DECLIB directory.
21273
21274 The X/Motif bindings used to build DECLIB are whatever versions are in the
21275 HP Ada @file{ADA$PREDEFINED} directory with extension @file{.ADC}.
21276 The build script will
21277 automatically add a pragma Linker_Options to packages @code{Xm}, @code{Xt},
21278 and @code{X_Lib}
21279 causing the default X/Motif sharable image libraries to be linked in. This
21280 is done via options files named @file{xm.opt}, @file{xt.opt}, and
21281 @file{x_lib.opt} (also located in the @file{DECLIB} directory).
21282
21283 It may be necessary to edit these options files to update or correct the
21284 library names if, for example, the newer X/Motif bindings from
21285 @file{ADA$EXAMPLES}
21286 had been (previous to installing GNAT) copied and renamed to supersede the
21287 default @file{ADA$PREDEFINED} versions.
21288
21289 @menu
21290 * Shared Libraries and Options Files::
21291 * Interfaces to C::
21292 @end menu
21293
21294 @node Shared Libraries and Options Files
21295 @subsection Shared Libraries and Options Files
21296
21297 @noindent
21298 When using the HP Ada
21299 predefined X and Motif bindings, the linking with their sharable images is
21300 done automatically by @command{GNAT LINK}.
21301 When using other X and Motif bindings, you need
21302 to add the corresponding sharable images to the command line for
21303 @code{GNAT LINK}. When linking with shared libraries, or with
21304 @file{.OPT} files, you must
21305 also add them to the command line for @command{GNAT LINK}.
21306
21307 A shared library to be used with GNAT is built in the same way as other
21308 libraries under VMS. The VMS Link command can be used in standard fashion.
21309
21310 @node Interfaces to C
21311 @subsection Interfaces to C
21312
21313 @noindent
21314 HP Ada
21315 provides the following Ada types and operations:
21316
21317 @itemize @bullet
21318 @item C types package (C_TYPES)
21319
21320 @item C strings (C_TYPES.NULL_TERMINATED)
21321
21322 @item Other_types (SHORT_INT)
21323 @end itemize
21324
21325 @noindent
21326 Interfacing to C with GNAT, one can use the above approach
21327 described for HP Ada or the facilities of Annex B of
21328 the Ada 95 Reference Manual (packages INTERFACES.C,
21329 INTERFACES.C.STRINGS and INTERFACES.C.POINTERS). For more
21330 information, see the section ``Interfacing to C'' in the
21331 @cite{GNAT Reference Manual}.
21332
21333 The @option{-gnatF} qualifier forces default and explicit
21334 @code{External_Name} parameters in pragmas Import and Export
21335 to be uppercased for compatibility with the default behavior
21336 of Compaq C. The qualifier has no effect on @code{Link_Name} parameters.
21337
21338 @node Main Program Definition
21339 @section Main Program Definition
21340
21341 @noindent
21342 The following section discusses differences in the
21343 definition of main programs on HP Ada and GNAT.
21344 On HP Ada, main programs are defined to meet the
21345 following conditions:
21346 @itemize @bullet
21347 @item  Procedure with no formal parameters (returns 0 upon
21348        normal completion)
21349
21350 @item  Procedure with no formal parameters (returns 42 when
21351        unhandled exceptions are raised)
21352
21353 @item  Function with no formal parameters whose returned value
21354        is of a discrete type
21355
21356 @item  Procedure with one OUT formal of a discrete type for
21357        which a specification of pragma EXPORT_VALUED_PROCEDURE is given.
21358
21359 @end itemize
21360
21361 @noindent
21362 When declared with the pragma EXPORT_VALUED_PROCEDURE,
21363 a main function or main procedure returns a discrete
21364 value whose size is less than 64 bits (32 on VAX systems),
21365 the value is zero- or sign-extended as appropriate.
21366 On GNAT, main programs are defined as follows:
21367 @itemize @bullet
21368 @item  Must be a non-generic, parameter-less subprogram that
21369 is either a procedure or function returning an Ada
21370 STANDARD.INTEGER (the predefined type)
21371
21372 @item  Cannot be a generic subprogram or an instantiation of a
21373 generic subprogram
21374 @end itemize
21375
21376 @node Implementation-Defined Attributes
21377 @section Implementation-Defined Attributes
21378
21379 @noindent
21380 GNAT provides all HP Ada implementation-defined
21381 attributes.
21382
21383 @node Compiler and Run-Time Interfacing
21384 @section Compiler and Run-Time Interfacing
21385
21386 @noindent
21387 HP Ada provides the following ways to pass options to the linker
21388 (ACS LINK):
21389 @itemize @bullet
21390 @item  /WAIT and /SUBMIT qualifiers
21391
21392 @item  /COMMAND qualifier
21393
21394 @item  /[NO]MAP qualifier
21395
21396 @item  /OUTPUT=file-spec
21397
21398 @item  /[NO]DEBUG and /[NO]TRACEBACK qualifiers
21399 @end itemize
21400
21401 @noindent
21402 To pass options to the linker, GNAT provides the following
21403 switches:
21404
21405 @itemize @bullet
21406 @item   @option{/EXECUTABLE=exec-name}
21407
21408 @item   @option{/VERBOSE qualifier}
21409
21410 @item   @option{/[NO]DEBUG} and @option{/[NO]TRACEBACK} qualifiers
21411 @end itemize
21412
21413 @noindent
21414 For more information on these switches, see
21415 @ref{Switches for gnatlink}.
21416 In HP Ada, the command-line switch @option{/OPTIMIZE} is available
21417 to control optimization. HP Ada also supplies the
21418 following pragmas:
21419 @itemize @bullet
21420 @item  @code{OPTIMIZE}
21421
21422 @item  @code{INLINE}
21423
21424 @item  @code{INLINE_GENERIC}
21425
21426 @item  @code{SUPPRESS_ALL}
21427
21428 @item  @code{PASSIVE}
21429 @end itemize
21430
21431 @noindent
21432 In GNAT, optimization is controlled strictly by command
21433 line parameters, as described in the corresponding section of this guide.
21434 The HP pragmas for control of optimization are
21435 recognized but ignored.
21436
21437 Note that in GNAT, the default is optimization off, whereas in HP Ada 83,
21438 the default is that optimization is turned on.
21439
21440 @node Program Compilation and Library Management
21441 @section Program Compilation and Library Management
21442
21443 @noindent
21444 HP Ada and GNAT provide a comparable set of commands to
21445 build programs. HP Ada also provides a program library,
21446 which is a concept that does not exist on GNAT. Instead,
21447 GNAT provides directories of sources that are compiled as
21448 needed.
21449
21450 The following table summarizes
21451 the HP Ada commands and provides
21452 equivalent GNAT commands. In this table, some GNAT
21453 equivalents reflect the fact that GNAT does not use the
21454 concept of a program library. Instead, it uses a model
21455 in which collections of source and object files are used
21456 in a manner consistent with other languages like C and
21457 Fortran. Therefore, standard system file commands are used
21458 to manipulate these elements. Those GNAT commands are marked with
21459 an asterisk.
21460 Note that, unlike HP Ada, none of the GNAT commands accepts wild cards.
21461
21462 @need 1500
21463 @multitable @columnfractions .35 .65
21464
21465 @item @emph{HP Ada Command}
21466 @tab @emph{GNAT Equivalent / Description}
21467
21468 @item @command{ADA}
21469 @tab @command{GNAT COMPILE}@*
21470 Invokes the compiler to compile one or more Ada source files.
21471
21472 @item @command{ACS ATTACH}@*
21473 @tab [No equivalent]@*
21474 Switches control of terminal from current process running the program
21475 library manager.
21476
21477 @item @command{ACS CHECK}
21478 @tab @command{GNAT MAKE /DEPENDENCY_LIST}@*
21479 Forms the execution closure of one
21480 or more compiled units and checks completeness and currency.
21481
21482 @item @command{ACS COMPILE}
21483 @tab @command{GNAT MAKE /ACTIONS=COMPILE}@*
21484 Forms the execution closure of one or
21485 more specified units, checks completeness and currency,
21486 identifies units that have revised source files, compiles same,
21487 and recompiles units that are or will become obsolete.
21488 Also completes incomplete generic instantiations.
21489
21490 @item @command{ACS COPY FOREIGN}
21491 @tab Copy (*)@*
21492 Copies a foreign object file into the program library as a
21493 library unit body.
21494
21495 @item @command{ACS COPY UNIT}
21496 @tab Copy (*)@*
21497 Copies a compiled unit from one program library to another.
21498
21499 @item @command{ACS CREATE LIBRARY}
21500 @tab Create /directory (*)@*
21501 Creates a program library.
21502
21503 @item @command{ACS CREATE SUBLIBRARY}
21504 @tab Create /directory (*)@*
21505 Creates a program sublibrary.
21506
21507 @item @command{ACS DELETE LIBRARY}
21508 @tab @*
21509 Deletes a program library and its contents.
21510
21511 @item @command{ACS DELETE SUBLIBRARY}
21512 @tab @*
21513 Deletes a program sublibrary and its contents.
21514
21515 @item @command{ACS DELETE UNIT}
21516 @tab Delete file (*)@*
21517 On OpenVMS systems, deletes one or more compiled units from
21518 the current program library.
21519
21520 @item @command{ACS DIRECTORY}
21521 @tab Directory (*)@*
21522 On OpenVMS systems, lists units contained in the current
21523 program library.
21524
21525 @item @command{ACS ENTER FOREIGN}
21526 @tab Copy (*)@*
21527 Allows the import of a foreign body as an Ada library
21528 specification and enters a reference to a pointer.
21529
21530 @item @command{ACS ENTER UNIT}
21531 @tab Copy (*)@*
21532 Enters a reference (pointer) from the current program library to
21533 a unit compiled into another program library.
21534
21535 @item @command{ACS EXIT}
21536 @tab [No equivalent]@*
21537 Exits from the program library manager.
21538
21539 @item @command{ACS EXPORT}
21540 @tab Copy (*)@*
21541 Creates an object file that contains system-specific object code
21542 for one or more units. With GNAT, object files can simply be copied
21543 into the desired directory.
21544
21545 @item @command{ACS EXTRACT SOURCE}
21546 @tab Copy (*)@*
21547 Allows access to the copied source file for each Ada compilation unit
21548
21549 @item @command{ACS HELP}
21550 @tab @command{HELP GNAT}@*
21551 Provides online help.
21552
21553 @item @command{ACS LINK}
21554 @tab @command{GNAT LINK}@*
21555 Links an object file containing Ada units into an executable file.
21556
21557 @item @command{ACS LOAD}
21558 @tab Copy (*)@*
21559 Loads (partially compiles) Ada units into the program library.
21560 Allows loading a program from a collection of files into a library
21561 without knowing the relationship among units.
21562
21563 @item @command{ACS MERGE}
21564 @tab Copy (*)@*
21565 Merges into the current program library, one or more units from
21566 another library where they were modified.
21567
21568 @item @command{ACS RECOMPILE}
21569 @tab @command{GNAT MAKE /ACTIONS=COMPILE}@*
21570 Recompiles from   external or copied source files any obsolete
21571 unit in the closure. Also, completes any incomplete generic
21572 instantiations.
21573
21574 @item @command{ACS REENTER}
21575 @tab @command{GNAT MAKE}@*
21576 Reenters current references to units compiled after last entered
21577 with the @command{ACS ENTER UNIT} command.
21578
21579 @item @command{ACS SET LIBRARY}
21580 @tab Set default (*)@*
21581 Defines a program library to be the compilation context as well
21582 as the target library for compiler output and commands in general.
21583
21584 @item @command{ACS SET PRAGMA}
21585 @tab Edit @file{gnat.adc} (*)@*
21586 Redefines specified  values of the library characteristics
21587 @code{LONG_ FLOAT}, @code{MEMORY_SIZE}, @code{SYSTEM_NAME},
21588 and @code{Float_Representation}.
21589
21590 @item @command{ACS SET SOURCE}
21591 @tab Define @code{ADA_INCLUDE_PATH} path (*)@*
21592 Defines the source file search list for the @command{ACS COMPILE} command.
21593
21594 @item @command{ACS SHOW LIBRARY}
21595 @tab Directory (*)@*
21596 Lists information about one or more program libraries.
21597
21598 @item @command{ACS SHOW PROGRAM}
21599 @tab [No equivalent]@*
21600 Lists information about the execution closure of one or
21601 more units in the program library.
21602
21603 @item @command{ACS SHOW SOURCE}
21604 @tab Show logical @code{ADA_INCLUDE_PATH}@*
21605 Shows the source file search used when compiling units.
21606
21607 @item @command{ACS SHOW VERSION}
21608 @tab Compile with @option{VERBOSE} option
21609 Displays the version number of the compiler and program library
21610 manager used.
21611
21612 @item @command{ACS SPAWN}
21613 @tab [No equivalent]@*
21614 Creates a subprocess of the current process (same as @command{DCL SPAWN}
21615 command).
21616
21617 @item @command{ACS VERIFY}
21618 @tab [No equivalent]@*
21619 Performs a series of consistency checks on a program library to
21620 determine whether the library structure and library files are in
21621 valid form.
21622 @end multitable
21623
21624 @noindent
21625
21626 @node Input-Output
21627 @section Input-Output
21628
21629 @noindent
21630 On OpenVMS Alpha systems, HP Ada uses OpenVMS Record
21631 Management Services (RMS) to perform operations on
21632 external files.
21633
21634 @noindent
21635 HP Ada and GNAT predefine an identical set of input-
21636 output packages. To make the use of the
21637 generic TEXT_IO operations more convenient, HP Ada
21638 provides predefined library packages that instantiate the
21639 integer and floating-point operations for the predefined
21640 integer and floating-point types as shown in the following table.
21641
21642 @multitable @columnfractions .45 .55
21643 @item @emph{Package Name} @tab Instantiation
21644
21645 @item @code{INTEGER_TEXT_IO}
21646 @tab @code{INTEGER_IO(INTEGER)}
21647
21648 @item @code{SHORT_INTEGER_TEXT_IO}
21649 @tab @code{INTEGER_IO(SHORT_INTEGER)}
21650
21651 @item @code{SHORT_SHORT_INTEGER_TEXT_IO}
21652 @tab @code{INTEGER_IO(SHORT_SHORT_INTEGER)}
21653
21654 @item @code{FLOAT_TEXT_IO}
21655 @tab @code{FLOAT_IO(FLOAT)}
21656
21657 @item @code{LONG_FLOAT_TEXT_IO}
21658 @tab @code{FLOAT_IO(LONG_FLOAT)}
21659 @end multitable
21660
21661 @noindent
21662 The HP Ada predefined packages and their operations
21663 are implemented using OpenVMS Alpha files and input-
21664 output facilities. HP Ada supports asynchronous input-
21665 output on OpenVMS Alpha. Familiarity with the following is
21666 recommended:
21667 @itemize @bullet
21668 @item  RMS file organizations and access methods
21669
21670 @item  OpenVMS file specifications and directories
21671
21672 @item  OpenVMS File Definition Language (FDL)
21673 @end itemize
21674
21675 @noindent
21676 GNAT provides I/O facilities that are completely
21677 compatible with HP Ada. The distribution includes the
21678 standard HP Ada versions of all I/O packages, operating
21679 in a manner compatible with HP Ada. In particular, the
21680 following packages are by default the HP Ada (Ada 83)
21681 versions of these packages rather than the renamings
21682 suggested in annex J of the Ada 95 Reference Manual:
21683 @itemize @bullet
21684 @item  @code{TEXT_IO}
21685
21686 @item  @code{SEQUENTIAL_IO}
21687
21688 @item  @code{DIRECT_IO}
21689 @end itemize
21690
21691 @noindent
21692 The use of the standard Ada 95 syntax for child packages (for
21693 example, @code{ADA.TEXT_IO}) retrieves the Ada 95 versions of these
21694 packages, as defined in the Ada 95 Reference Manual.
21695 GNAT provides HP-compatible predefined instantiations
21696 of the @code{TEXT_IO} packages, and also
21697 provides the standard predefined instantiations required
21698 by the Ada 95 Reference Manual.
21699
21700 For further information on how GNAT interfaces to the file
21701 system or how I/O is implemented in programs written in
21702 mixed languages, see the chapter ``Implementation of the
21703 Standard I/O'' in the @cite{GNAT Reference Manual}.
21704 This chapter covers the following:
21705 @itemize @bullet
21706 @item  Standard I/O packages
21707
21708 @item  @code{FORM} strings
21709
21710 @item  @code{ADA.DIRECT_IO}
21711
21712 @item  @code{ADA.SEQUENTIAL_IO}
21713
21714 @item  @code{ADA.TEXT_IO}
21715
21716 @item  Stream pointer positioning
21717
21718 @item  Reading and writing non-regular files
21719
21720 @item  @code{GET_IMMEDIATE}
21721
21722 @item  Treating @code{TEXT_IO} files as streams
21723
21724 @item  Shared files
21725
21726 @item  Open modes
21727 @end itemize
21728
21729 @node Implementation Limits
21730 @section Implementation Limits
21731
21732 @noindent
21733 The following table lists implementation limits for HP Ada
21734 and GNAT systems.
21735 @multitable @columnfractions .60 .20 .20
21736 @sp 1
21737 @item  @emph{Compilation Parameter}
21738 @tab   @emph{HP Ada}
21739 @tab   @emph{GNAT}
21740 @sp 1
21741
21742 @item  In a subprogram or entry  declaration, maximum number of
21743        formal parameters that are of an unconstrained record type
21744 @tab   32
21745 @tab   No set limit
21746 @sp 1
21747
21748 @item  Maximum identifier length (number of characters)
21749 @tab   255
21750 @tab   255
21751 @sp 1
21752
21753 @item  Maximum number of characters in a source line
21754 @tab   255
21755 @tab   255
21756 @sp 1
21757
21758 @item  Maximum collection size   (number of bytes)
21759 @tab   2**31-1
21760 @tab   2**31-1
21761 @sp 1
21762
21763 @item  Maximum number of discriminants for a record type
21764 @tab   245
21765 @tab   No set limit
21766 @sp 1
21767
21768 @item  Maximum number of formal parameters in an entry or
21769        subprogram declaration
21770 @tab   246
21771 @tab    No set limit
21772 @sp 1
21773
21774 @item  Maximum number of dimensions in an array type
21775 @tab   255
21776 @tab   No set limit
21777 @sp 1
21778
21779 @item  Maximum number of library  units and subunits in a compilation.
21780 @tab   4095
21781 @tab   No set limit
21782 @sp 1
21783
21784 @item  Maximum number of library units and subunits in an execution.
21785 @tab   16383
21786 @tab   No set limit
21787 @sp 1
21788
21789 @item  Maximum number of objects declared with the pragma @code{COMMON_OBJECT}
21790        or @code{PSECT_OBJECT}
21791 @tab   32757
21792 @tab   No set limit
21793 @sp 1
21794
21795 @item  Maximum number of enumeration literals in an enumeration type
21796        definition
21797 @tab   65535
21798 @tab   No set limit
21799 @sp 1
21800
21801 @item  Maximum number of lines in a source file
21802 @tab   65534
21803 @tab   No set limit
21804 @sp 1
21805
21806 @item  Maximum number of bits in any object
21807 @tab   2**31-1
21808 @tab   2**31-1
21809 @sp 1
21810
21811 @item  Maximum size of the static portion of a stack frame (approximate)
21812 @tab   2**31-1
21813 @tab   2**31-1
21814 @end multitable
21815
21816 @end ifset
21817
21818 @c **************************************
21819 @node Platform-Specific Information for the Run-Time Libraries
21820 @appendix Platform-Specific Information for the Run-Time Libraries
21821 @cindex Tasking and threads libraries
21822 @cindex Threads libraries and tasking
21823 @cindex Run-time libraries (platform-specific information)
21824
21825 @noindent
21826 The GNAT run-time implementation may vary with respect to both the
21827 underlying threads library and the exception handling scheme.
21828 For threads support, one or more of the following are supplied:
21829 @itemize @bullet
21830 @item @b{native threads library}, a binding to the thread package from
21831 the underlying operating system
21832
21833 @item @b{pthreads library} (Sparc Solaris only), a binding to the Solaris
21834 POSIX thread package
21835 @end itemize
21836
21837 @noindent
21838 For exception handling, either or both of two models are supplied:
21839 @itemize @bullet
21840 @item @b{Zero-Cost Exceptions} (``ZCX''),@footnote{
21841 Most programs should experience a substantial speed improvement by
21842 being compiled with a ZCX run-time.
21843 This is especially true for
21844 tasking applications or applications with many exception handlers.}
21845 @cindex Zero-Cost Exceptions
21846 @cindex ZCX (Zero-Cost Exceptions)
21847 which uses binder-generated tables that
21848 are interrogated at run time to locate a handler
21849
21850 @item @b{setjmp / longjmp} (``SJLJ''),
21851 @cindex setjmp/longjmp Exception Model
21852 @cindex SJLJ (setjmp/longjmp Exception Model)
21853 which uses dynamically-set data to establish
21854 the set of handlers
21855 @end itemize
21856
21857 @noindent
21858 This appendix summarizes which combinations of threads and exception support
21859 are supplied on various GNAT platforms.
21860 It then shows how to select a particular library either
21861 permanently or temporarily,
21862 explains the properties of (and tradeoffs among) the various threads
21863 libraries, and provides some additional
21864 information about several specific platforms.
21865
21866 @menu
21867 * Summary of Run-Time Configurations::
21868 * Specifying a Run-Time Library::
21869 * Choosing the Scheduling Policy::
21870 * Solaris-Specific Considerations::
21871 * IRIX-Specific Considerations::
21872 * Linux-Specific Considerations::
21873 * AIX-Specific Considerations::
21874 @end menu
21875
21876 @node Summary of Run-Time Configurations
21877 @section Summary of Run-Time Configurations
21878
21879 @multitable @columnfractions .30 .70
21880 @item @b{alpha-openvms}
21881 @item @code{@ @ }@i{rts-native (default)}
21882 @item @code{@ @ @ @ }Tasking    @tab native VMS threads
21883 @item @code{@ @ @ @ }Exceptions @tab ZCX
21884 @*
21885 @item @b{pa-hpux}
21886 @item @code{@ @ }@i{rts-native (default)}
21887 @item @code{@ @ @ @ }Tasking    @tab native HP threads library
21888 @item @code{@ @ @ @ }Exceptions @tab ZCX
21889 @*
21890 @item @code{@ @ }@i{rts-sjlj}
21891 @item @code{@ @ @ @ }Tasking    @tab native HP threads library
21892 @item @code{@ @ @ @ }Exceptions @tab SJLJ
21893 @*
21894 @item @b{sparc-solaris}  @tab
21895 @item @code{@ @ }@i{rts-native (default)}
21896 @item @code{@ @ @ @ }Tasking    @tab native Solaris threads library
21897 @item @code{@ @ @ @ }Exceptions @tab ZCX
21898 @*
21899 @item @code{@ @ }@i{rts-m64}
21900 @item @code{@ @ @ @ }Tasking     @tab native Solaris threads library
21901 @item @code{@ @ @ @ }Exceptions  @tab ZCX
21902 @item @code{@ @ @ @ }Constraints @tab Use only when compiling in 64-bit mode;
21903 @item    @tab Use only on Solaris 8 or later.
21904 @item    @tab @xref{Building and Debugging 64-bit Applications}, for details.
21905 @*
21906 @item @code{@ @ }@i{rts-pthread}
21907 @item @code{@ @ @ @ }Tasking    @tab pthreads library
21908 @item @code{@ @ @ @ }Exceptions @tab ZCX
21909 @*
21910 @item @code{@ @ }@i{rts-sjlj}
21911 @item @code{@ @ @ @ }Tasking    @tab native Solaris threads library
21912 @item @code{@ @ @ @ }Exceptions @tab SJLJ
21913 @*
21914 @item @b{x86-linux}
21915 @item @code{@ @ }@i{rts-native (default)}
21916 @item @code{@ @ @ @ }Tasking    @tab pthread library
21917 @item @code{@ @ @ @ }Exceptions @tab ZCX
21918 @*
21919 @item @code{@ @ }@i{rts-sjlj}
21920 @item @code{@ @ @ @ }Tasking    @tab pthread library
21921 @item @code{@ @ @ @ }Exceptions @tab SJLJ
21922 @*
21923 @item @b{x86-windows}
21924 @item @code{@ @ }@i{rts-native (default)}
21925 @item @code{@ @ @ @ }Tasking    @tab native Win32 threads
21926 @item @code{@ @ @ @ }Exceptions @tab SJLJ
21927 @*
21928 @end multitable
21929
21930 @node Specifying a Run-Time Library
21931 @section Specifying a Run-Time Library
21932
21933 @noindent
21934 The @file{adainclude} subdirectory containing the sources of the GNAT
21935 run-time library, and the @file{adalib} subdirectory containing the
21936 @file{ALI} files and the static and/or shared GNAT library, are located
21937 in the gcc target-dependent area:
21938
21939 @smallexample
21940 target=$prefix/lib/gcc-lib/gcc-@i{dumpmachine}/gcc-@i{dumpversion}/
21941 @end smallexample
21942
21943 @noindent
21944 As indicated above, on some platforms several run-time libraries are supplied.
21945 These libraries are installed in the target dependent area and
21946 contain a complete source and binary subdirectory. The detailed description
21947 below explains the differences between the different libraries in terms of
21948 their thread support.
21949
21950 The default run-time library (when GNAT is installed) is @emph{rts-native}.
21951 This default run time is selected by the means of soft links.
21952 For example on x86-linux:
21953
21954 @smallexample
21955 @group
21956  $(target-dir)
21957      |
21958      +--- adainclude----------+
21959      |                        |
21960      +--- adalib-----------+  |
21961      |                     |  |
21962      +--- rts-native       |  |
21963      |    |                |  |
21964      |    +--- adainclude <---+
21965      |    |                |
21966      |    +--- adalib <----+
21967      |
21968      +--- rts-sjlj
21969           |
21970           +--- adainclude
21971           |
21972           +--- adalib
21973 @end group
21974 @end smallexample
21975
21976 @noindent
21977 If the @i{rts-sjlj} library is to be selected on a permanent basis,
21978 these soft links can be modified with the following commands:
21979
21980 @smallexample
21981 $ cd $target
21982 $ rm -f adainclude adalib
21983 $ ln -s rts-sjlj/adainclude adainclude
21984 $ ln -s rts-sjlj/adalib adalib
21985 @end smallexample
21986
21987 @noindent
21988 Alternatively, you can specify @file{rts-sjlj/adainclude} in the file
21989 @file{$target/ada_source_path} and @file{rts-sjlj/adalib} in
21990 @file{$target/ada_object_path}.
21991
21992 Selecting another run-time library temporarily can be
21993 achieved by the regular mechanism for GNAT object or source path selection:
21994
21995 @itemize @bullet
21996 @item
21997 Set the environment variables:
21998
21999 @smallexample
22000 $ ADA_INCLUDE_PATH=$target/rts-sjlj/adainclude:$ADA_INCLUDE_PATH
22001 $ ADA_OBJECTS_PATH=$target/rts-sjlj/adalib:$ADA_OBJECTS_PATH
22002 $ export ADA_INCLUDE_PATH ADA_OBJECTS_PATH
22003 @end smallexample
22004
22005 @item
22006 Use @option{-aI$target/rts-sjlj/adainclude}
22007 and @option{-aO$target/rts-sjlj/adalib}
22008 on the @command{gnatmake} command line
22009
22010 @item
22011 Use the switch @option{--RTS}; e.g., @option{--RTS=sjlj}
22012 @cindex @option{--RTS} option
22013 @end itemize
22014
22015 @node Choosing the Scheduling Policy
22016 @section Choosing the Scheduling Policy
22017
22018 @noindent
22019 When using a POSIX threads implementation, you have a choice of several
22020 scheduling policies: @code{SCHED_FIFO},
22021 @cindex @code{SCHED_FIFO} scheduling policy
22022 @code{SCHED_RR}
22023 @cindex @code{SCHED_RR} scheduling policy
22024 and @code{SCHED_OTHER}.
22025 @cindex @code{SCHED_OTHER} scheduling policy
22026 Typically, the default is @code{SCHED_OTHER}, while using @code{SCHED_FIFO}
22027 or @code{SCHED_RR} requires special (e.g., root) privileges.
22028
22029 By default, GNAT uses the @code{SCHED_OTHER} policy. To specify
22030 @code{SCHED_FIFO},
22031 @cindex @code{SCHED_FIFO} scheduling policy
22032 you can use one of the following:
22033
22034 @itemize @bullet
22035 @item
22036 @code{pragma Time_Slice (0.0)}
22037 @cindex pragma Time_Slice
22038 @item
22039 the corresponding binder option @option{-T0}
22040 @cindex @option{-T0} option
22041 @item
22042 @code{pragma Task_Dispatching_Policy (FIFO_Within_Priorities)}
22043 @cindex pragma Task_Dispatching_Policy
22044 @end itemize
22045
22046 @noindent
22047 To specify @code{SCHED_RR},
22048 @cindex @code{SCHED_RR} scheduling policy
22049 you should use @code{pragma Time_Slice} with a
22050 value greater than @code{0.0}, or else use the corresponding @option{-T}
22051 binder option.
22052
22053 @node Solaris-Specific Considerations
22054 @section Solaris-Specific Considerations
22055 @cindex Solaris Sparc threads libraries
22056
22057 @noindent
22058 This section addresses some topics related to the various threads libraries
22059 on Sparc Solaris and then provides some information on building and
22060 debugging 64-bit applications.
22061
22062 @menu
22063 * Solaris Threads Issues::
22064 * Building and Debugging 64-bit Applications::
22065 @end menu
22066
22067 @node Solaris Threads Issues
22068 @subsection Solaris Threads Issues
22069
22070 @noindent
22071 GNAT under Solaris comes with an alternate tasking run-time library
22072 based on POSIX threads --- @emph{rts-pthread}.
22073 @cindex rts-pthread threads library
22074 This run-time library has the advantage of being mostly shared across all
22075 POSIX-compliant thread implementations, and it also provides under
22076 @w{Solaris 8} the @code{PTHREAD_PRIO_INHERIT}
22077 @cindex @code{PTHREAD_PRIO_INHERIT} policy (under rts-pthread)
22078 and @code{PTHREAD_PRIO_PROTECT}
22079 @cindex @code{PTHREAD_PRIO_PROTECT} policy (under rts-pthread)
22080 semantics that can be selected using the predefined pragma
22081 @code{Locking_Policy}
22082 @cindex pragma Locking_Policy (under rts-pthread)
22083 with respectively
22084 @code{Inheritance_Locking} and @code{Ceiling_Locking} as the policy.
22085 @cindex @code{Inheritance_Locking} (under rts-pthread)
22086 @cindex @code{Ceiling_Locking} (under rts-pthread)
22087
22088 As explained above, the native run-time library is based on the Solaris thread
22089 library (@code{libthread}) and is the default library.
22090
22091 When the Solaris threads library is used (this is the default), programs
22092 compiled with GNAT can automatically take advantage of
22093 and can thus execute on multiple processors.
22094 The user can alternatively specify a processor on which the program should run
22095 to emulate a single-processor system. The multiprocessor / uniprocessor choice
22096 is made by
22097 setting the environment variable @code{GNAT_PROCESSOR}
22098 @cindex @code{GNAT_PROCESSOR} environment variable (on Sparc Solaris)
22099 to one of the following:
22100
22101 @table @code
22102 @item -2
22103 Use the default configuration (run the program on all
22104          available processors) - this is the same as having
22105          @code{GNAT_PROCESSOR} unset
22106
22107 @item -1
22108 Let the run-time implementation choose one processor and run the program on
22109          that processor
22110
22111 @item 0 .. Last_Proc
22112 Run the program on the specified processor.
22113   @code{Last_Proc} is equal to @code{_SC_NPROCESSORS_CONF - 1}
22114 (where @code{_SC_NPROCESSORS_CONF} is a system variable).
22115 @end table
22116
22117 @node Building and Debugging 64-bit Applications
22118 @subsection Building and Debugging 64-bit Applications
22119
22120 @noindent
22121 In a 64-bit application, all the sources involved must be compiled with the
22122 @option{-m64} command-line option, and a specific GNAT library (compiled with
22123 this option) is required.
22124 The easiest way to build a 64bit application is to add
22125 @option{-m64 --RTS=m64} to the @command{gnatmake} flags.
22126
22127 To debug these applications, a special version of gdb called @command{gdb64}
22128 needs to be used.
22129
22130 To summarize, building and debugging a ``Hello World'' program in 64-bit mode
22131 amounts to:
22132
22133 @smallexample
22134      $ gnatmake -m64 -g --RTS=m64 hello.adb
22135      $ gdb64 hello
22136 @end smallexample
22137
22138 In addition, the following capabilities are not supported when using the
22139 @option{-m64} option:
22140
22141 @table @code
22142 @item -fstack-check does not work together with -m64.
22143 Any application combining these options crashes at startup time.
22144
22145 @item Call-chain backtrace computation does not work with -m64.
22146 Thus the gnatbind switch -E is not supported.
22147 @end table
22148
22149 @node IRIX-Specific Considerations
22150 @section IRIX-Specific Considerations
22151 @cindex IRIX thread library
22152
22153 @noindent
22154 On SGI IRIX, the thread library depends on which compiler is used.
22155 The @emph{o32 ABI} compiler comes with a run-time library based on the
22156 user-level @code{athread}
22157 library. Thus kernel-level capabilities such as nonblocking system
22158 calls or time slicing can only be achieved reliably by specifying different
22159 @code{sprocs} via the pragma @code{Task_Info}
22160 @cindex pragma Task_Info (and IRIX threads)
22161 and the
22162 @code{System.Task_Info} package.
22163 @cindex @code{System.Task_Info} package (and IRIX threads)
22164 See the @cite{GNAT Reference Manual} for further information.
22165
22166 The @emph{n32 ABI} compiler comes with a run-time library based on the
22167 kernel POSIX threads and thus does not have the limitations mentioned above.
22168
22169 @node Linux-Specific Considerations
22170 @section Linux-Specific Considerations
22171 @cindex Linux threads libraries
22172
22173 @noindent
22174 The default thread library under GNU/Linux has the following disadvantages
22175 compared to other native thread libraries:
22176
22177 @itemize @bullet
22178 @item The size of the task's stack is limited to 2 megabytes.
22179 @item  The signal model is not POSIX compliant, which means that to send a
22180   signal to the process, you need to send the signal to all threads,
22181   e.g. by using @code{killpg()}.
22182 @end itemize
22183
22184 @node AIX-Specific Considerations
22185 @section AIX-Specific Considerations
22186 @cindex AIX resolver library
22187
22188 @noindent
22189 On AIX, the resolver library initializes some internal structure on
22190 the first call to @code{get*by*} functions, which are used to implement
22191 @code{GNAT.Sockets.Get_Host_By_Name} and
22192 @code{GNAT.Sockets.Get_Host_By_Addrss}.
22193 If such initialization occurs within an Ada task, and the stack size for
22194 the task is the default size, a stack overflow may occur.
22195
22196 To avoid this overflow, the user should either ensure that the first call
22197 to @code{GNAT.Sockets.Get_Host_By_Name} or
22198 @code{GNAT.Sockets.Get_Host_By_Addrss}
22199 occurs in the environment task, or use @code{pragma Storage_Size} to
22200 specify a sufficiently large size for the stack of the task that contains
22201 this call.
22202
22203 @c *******************************
22204 @node Example of Binder Output File
22205 @appendix Example of Binder Output File
22206
22207 @noindent
22208 This Appendix displays the source code for @command{gnatbind}'s output
22209 file generated for a simple ``Hello World'' program.
22210 Comments have been added for clarification purposes.
22211
22212 @smallexample @c adanocomment
22213 @iftex
22214 @leftskip=0cm
22215 @end iftex
22216 --  The package is called Ada_Main unless this name is actually used
22217 --  as a unit name in the partition, in which case some other unique
22218 --  name is used.
22219
22220 with System;
22221 package ada_main is
22222
22223    Elab_Final_Code : Integer;
22224    pragma Import (C, Elab_Final_Code, "__gnat_inside_elab_final_code");
22225
22226    --  The main program saves the parameters (argument count,
22227    --  argument values, environment pointer) in global variables
22228    --  for later access by other units including
22229    --  Ada.Command_Line.
22230
22231    gnat_argc : Integer;
22232    gnat_argv : System.Address;
22233    gnat_envp : System.Address;
22234
22235    --  The actual variables are stored in a library routine. This
22236    --  is useful for some shared library situations, where there
22237    --  are problems if variables are not in the library.
22238
22239    pragma Import (C, gnat_argc);
22240    pragma Import (C, gnat_argv);
22241    pragma Import (C, gnat_envp);
22242
22243    --  The exit status is similarly an external location
22244
22245    gnat_exit_status : Integer;
22246    pragma Import (C, gnat_exit_status);
22247
22248    GNAT_Version : constant String :=
22249                     "GNAT Version: 3.15w (20010315)";
22250    pragma Export (C, GNAT_Version, "__gnat_version");
22251
22252    --  This is the generated adafinal routine that performs
22253    --  finalization at the end of execution. In the case where
22254    --  Ada is the main program, this main program makes a call
22255    --  to adafinal at program termination.
22256
22257    procedure adafinal;
22258    pragma Export (C, adafinal, "adafinal");
22259
22260    --  This is the generated adainit routine that performs
22261    --  initialization at the start of execution. In the case
22262    --  where Ada is the main program, this main program makes
22263    --  a call to adainit at program startup.
22264
22265    procedure adainit;
22266    pragma Export (C, adainit, "adainit");
22267
22268    --  This routine is called at the start of execution. It is
22269    --  a dummy routine that is used by the debugger to breakpoint
22270    --  at the start of execution.
22271
22272    procedure Break_Start;
22273    pragma Import (C, Break_Start, "__gnat_break_start");
22274
22275    --  This is the actual generated main program (it would be
22276    --  suppressed if the no main program switch were used). As
22277    --  required by standard system conventions, this program has
22278    --  the external name main.
22279
22280    function main
22281      (argc : Integer;
22282       argv : System.Address;
22283       envp : System.Address)
22284       return Integer;
22285    pragma Export (C, main, "main");
22286
22287    --  The following set of constants give the version
22288    --  identification values for every unit in the bound
22289    --  partition. This identification is computed from all
22290    --  dependent semantic units, and corresponds to the
22291    --  string that would be returned by use of the
22292    --  Body_Version or Version attributes.
22293
22294    type Version_32 is mod 2 ** 32;
22295    u00001 : constant Version_32 := 16#7880BEB3#;
22296    u00002 : constant Version_32 := 16#0D24CBD0#;
22297    u00003 : constant Version_32 := 16#3283DBEB#;
22298    u00004 : constant Version_32 := 16#2359F9ED#;
22299    u00005 : constant Version_32 := 16#664FB847#;
22300    u00006 : constant Version_32 := 16#68E803DF#;
22301    u00007 : constant Version_32 := 16#5572E604#;
22302    u00008 : constant Version_32 := 16#46B173D8#;
22303    u00009 : constant Version_32 := 16#156A40CF#;
22304    u00010 : constant Version_32 := 16#033DABE0#;
22305    u00011 : constant Version_32 := 16#6AB38FEA#;
22306    u00012 : constant Version_32 := 16#22B6217D#;
22307    u00013 : constant Version_32 := 16#68A22947#;
22308    u00014 : constant Version_32 := 16#18CC4A56#;
22309    u00015 : constant Version_32 := 16#08258E1B#;
22310    u00016 : constant Version_32 := 16#367D5222#;
22311    u00017 : constant Version_32 := 16#20C9ECA4#;
22312    u00018 : constant Version_32 := 16#50D32CB6#;
22313    u00019 : constant Version_32 := 16#39A8BB77#;
22314    u00020 : constant Version_32 := 16#5CF8FA2B#;
22315    u00021 : constant Version_32 := 16#2F1EB794#;
22316    u00022 : constant Version_32 := 16#31AB6444#;
22317    u00023 : constant Version_32 := 16#1574B6E9#;
22318    u00024 : constant Version_32 := 16#5109C189#;
22319    u00025 : constant Version_32 := 16#56D770CD#;
22320    u00026 : constant Version_32 := 16#02F9DE3D#;
22321    u00027 : constant Version_32 := 16#08AB6B2C#;
22322    u00028 : constant Version_32 := 16#3FA37670#;
22323    u00029 : constant Version_32 := 16#476457A0#;
22324    u00030 : constant Version_32 := 16#731E1B6E#;
22325    u00031 : constant Version_32 := 16#23C2E789#;
22326    u00032 : constant Version_32 := 16#0F1BD6A1#;
22327    u00033 : constant Version_32 := 16#7C25DE96#;
22328    u00034 : constant Version_32 := 16#39ADFFA2#;
22329    u00035 : constant Version_32 := 16#571DE3E7#;
22330    u00036 : constant Version_32 := 16#5EB646AB#;
22331    u00037 : constant Version_32 := 16#4249379B#;
22332    u00038 : constant Version_32 := 16#0357E00A#;
22333    u00039 : constant Version_32 := 16#3784FB72#;
22334    u00040 : constant Version_32 := 16#2E723019#;
22335    u00041 : constant Version_32 := 16#623358EA#;
22336    u00042 : constant Version_32 := 16#107F9465#;
22337    u00043 : constant Version_32 := 16#6843F68A#;
22338    u00044 : constant Version_32 := 16#63305874#;
22339    u00045 : constant Version_32 := 16#31E56CE1#;
22340    u00046 : constant Version_32 := 16#02917970#;
22341    u00047 : constant Version_32 := 16#6CCBA70E#;
22342    u00048 : constant Version_32 := 16#41CD4204#;
22343    u00049 : constant Version_32 := 16#572E3F58#;
22344    u00050 : constant Version_32 := 16#20729FF5#;
22345    u00051 : constant Version_32 := 16#1D4F93E8#;
22346    u00052 : constant Version_32 := 16#30B2EC3D#;
22347    u00053 : constant Version_32 := 16#34054F96#;
22348    u00054 : constant Version_32 := 16#5A199860#;
22349    u00055 : constant Version_32 := 16#0E7F912B#;
22350    u00056 : constant Version_32 := 16#5760634A#;
22351    u00057 : constant Version_32 := 16#5D851835#;
22352
22353    --  The following Export pragmas export the version numbers
22354    --  with symbolic names ending in B (for body) or S
22355    --  (for spec) so that they can be located in a link. The
22356    --  information provided here is sufficient to track down
22357    --  the exact versions of units used in a given build.
22358
22359    pragma Export (C, u00001, "helloB");
22360    pragma Export (C, u00002, "system__standard_libraryB");
22361    pragma Export (C, u00003, "system__standard_libraryS");
22362    pragma Export (C, u00004, "adaS");
22363    pragma Export (C, u00005, "ada__text_ioB");
22364    pragma Export (C, u00006, "ada__text_ioS");
22365    pragma Export (C, u00007, "ada__exceptionsB");
22366    pragma Export (C, u00008, "ada__exceptionsS");
22367    pragma Export (C, u00009, "gnatS");
22368    pragma Export (C, u00010, "gnat__heap_sort_aB");
22369    pragma Export (C, u00011, "gnat__heap_sort_aS");
22370    pragma Export (C, u00012, "systemS");
22371    pragma Export (C, u00013, "system__exception_tableB");
22372    pragma Export (C, u00014, "system__exception_tableS");
22373    pragma Export (C, u00015, "gnat__htableB");
22374    pragma Export (C, u00016, "gnat__htableS");
22375    pragma Export (C, u00017, "system__exceptionsS");
22376    pragma Export (C, u00018, "system__machine_state_operationsB");
22377    pragma Export (C, u00019, "system__machine_state_operationsS");
22378    pragma Export (C, u00020, "system__machine_codeS");
22379    pragma Export (C, u00021, "system__storage_elementsB");
22380    pragma Export (C, u00022, "system__storage_elementsS");
22381    pragma Export (C, u00023, "system__secondary_stackB");
22382    pragma Export (C, u00024, "system__secondary_stackS");
22383    pragma Export (C, u00025, "system__parametersB");
22384    pragma Export (C, u00026, "system__parametersS");
22385    pragma Export (C, u00027, "system__soft_linksB");
22386    pragma Export (C, u00028, "system__soft_linksS");
22387    pragma Export (C, u00029, "system__stack_checkingB");
22388    pragma Export (C, u00030, "system__stack_checkingS");
22389    pragma Export (C, u00031, "system__tracebackB");
22390    pragma Export (C, u00032, "system__tracebackS");
22391    pragma Export (C, u00033, "ada__streamsS");
22392    pragma Export (C, u00034, "ada__tagsB");
22393    pragma Export (C, u00035, "ada__tagsS");
22394    pragma Export (C, u00036, "system__string_opsB");
22395    pragma Export (C, u00037, "system__string_opsS");
22396    pragma Export (C, u00038, "interfacesS");
22397    pragma Export (C, u00039, "interfaces__c_streamsB");
22398    pragma Export (C, u00040, "interfaces__c_streamsS");
22399    pragma Export (C, u00041, "system__file_ioB");
22400    pragma Export (C, u00042, "system__file_ioS");
22401    pragma Export (C, u00043, "ada__finalizationB");
22402    pragma Export (C, u00044, "ada__finalizationS");
22403    pragma Export (C, u00045, "system__finalization_rootB");
22404    pragma Export (C, u00046, "system__finalization_rootS");
22405    pragma Export (C, u00047, "system__finalization_implementationB");
22406    pragma Export (C, u00048, "system__finalization_implementationS");
22407    pragma Export (C, u00049, "system__string_ops_concat_3B");
22408    pragma Export (C, u00050, "system__string_ops_concat_3S");
22409    pragma Export (C, u00051, "system__stream_attributesB");
22410    pragma Export (C, u00052, "system__stream_attributesS");
22411    pragma Export (C, u00053, "ada__io_exceptionsS");
22412    pragma Export (C, u00054, "system__unsigned_typesS");
22413    pragma Export (C, u00055, "system__file_control_blockS");
22414    pragma Export (C, u00056, "ada__finalization__list_controllerB");
22415    pragma Export (C, u00057, "ada__finalization__list_controllerS");
22416
22417    -- BEGIN ELABORATION ORDER
22418    -- ada (spec)
22419    -- gnat (spec)
22420    -- gnat.heap_sort_a (spec)
22421    -- gnat.heap_sort_a (body)
22422    -- gnat.htable (spec)
22423    -- gnat.htable (body)
22424    -- interfaces (spec)
22425    -- system (spec)
22426    -- system.machine_code (spec)
22427    -- system.parameters (spec)
22428    -- system.parameters (body)
22429    -- interfaces.c_streams (spec)
22430    -- interfaces.c_streams (body)
22431    -- system.standard_library (spec)
22432    -- ada.exceptions (spec)
22433    -- system.exception_table (spec)
22434    -- system.exception_table (body)
22435    -- ada.io_exceptions (spec)
22436    -- system.exceptions (spec)
22437    -- system.storage_elements (spec)
22438    -- system.storage_elements (body)
22439    -- system.machine_state_operations (spec)
22440    -- system.machine_state_operations (body)
22441    -- system.secondary_stack (spec)
22442    -- system.stack_checking (spec)
22443    -- system.soft_links (spec)
22444    -- system.soft_links (body)
22445    -- system.stack_checking (body)
22446    -- system.secondary_stack (body)
22447    -- system.standard_library (body)
22448    -- system.string_ops (spec)
22449    -- system.string_ops (body)
22450    -- ada.tags (spec)
22451    -- ada.tags (body)
22452    -- ada.streams (spec)
22453    -- system.finalization_root (spec)
22454    -- system.finalization_root (body)
22455    -- system.string_ops_concat_3 (spec)
22456    -- system.string_ops_concat_3 (body)
22457    -- system.traceback (spec)
22458    -- system.traceback (body)
22459    -- ada.exceptions (body)
22460    -- system.unsigned_types (spec)
22461    -- system.stream_attributes (spec)
22462    -- system.stream_attributes (body)
22463    -- system.finalization_implementation (spec)
22464    -- system.finalization_implementation (body)
22465    -- ada.finalization (spec)
22466    -- ada.finalization (body)
22467    -- ada.finalization.list_controller (spec)
22468    -- ada.finalization.list_controller (body)
22469    -- system.file_control_block (spec)
22470    -- system.file_io (spec)
22471    -- system.file_io (body)
22472    -- ada.text_io (spec)
22473    -- ada.text_io (body)
22474    -- hello (body)
22475    -- END ELABORATION ORDER
22476
22477 end ada_main;
22478
22479 --  The following source file name pragmas allow the generated file
22480 --  names to be unique for different main programs. They are needed
22481 --  since the package name will always be Ada_Main.
22482
22483 pragma Source_File_Name (ada_main, Spec_File_Name => "b~hello.ads");
22484 pragma Source_File_Name (ada_main, Body_File_Name => "b~hello.adb");
22485
22486 --  Generated package body for Ada_Main starts here
22487
22488 package body ada_main is
22489
22490    --  The actual finalization is performed by calling the
22491    --  library routine in System.Standard_Library.Adafinal
22492
22493    procedure Do_Finalize;
22494    pragma Import (C, Do_Finalize, "system__standard_library__adafinal");
22495
22496    -------------
22497    -- adainit --
22498    -------------
22499
22500 @findex adainit
22501    procedure adainit is
22502
22503       --  These booleans are set to True once the associated unit has
22504       --  been elaborated. It is also used to avoid elaborating the
22505       --  same unit twice.
22506
22507       E040 : Boolean;
22508       pragma Import (Ada, E040, "interfaces__c_streams_E");
22509
22510       E008 : Boolean;
22511       pragma Import (Ada, E008, "ada__exceptions_E");
22512
22513       E014 : Boolean;
22514       pragma Import (Ada, E014, "system__exception_table_E");
22515
22516       E053 : Boolean;
22517       pragma Import (Ada, E053, "ada__io_exceptions_E");
22518
22519       E017 : Boolean;
22520       pragma Import (Ada, E017, "system__exceptions_E");
22521
22522       E024 : Boolean;
22523       pragma Import (Ada, E024, "system__secondary_stack_E");
22524
22525       E030 : Boolean;
22526       pragma Import (Ada, E030, "system__stack_checking_E");
22527
22528       E028 : Boolean;
22529       pragma Import (Ada, E028, "system__soft_links_E");
22530
22531       E035 : Boolean;
22532       pragma Import (Ada, E035, "ada__tags_E");
22533
22534       E033 : Boolean;
22535       pragma Import (Ada, E033, "ada__streams_E");
22536
22537       E046 : Boolean;
22538       pragma Import (Ada, E046, "system__finalization_root_E");
22539
22540       E048 : Boolean;
22541       pragma Import (Ada, E048, "system__finalization_implementation_E");
22542
22543       E044 : Boolean;
22544       pragma Import (Ada, E044, "ada__finalization_E");
22545
22546       E057 : Boolean;
22547       pragma Import (Ada, E057, "ada__finalization__list_controller_E");
22548
22549       E055 : Boolean;
22550       pragma Import (Ada, E055, "system__file_control_block_E");
22551
22552       E042 : Boolean;
22553       pragma Import (Ada, E042, "system__file_io_E");
22554
22555       E006 : Boolean;
22556       pragma Import (Ada, E006, "ada__text_io_E");
22557
22558       --  Set_Globals is a library routine that stores away the
22559       --  value of the indicated set of global values in global
22560       --  variables within the library.
22561
22562       procedure Set_Globals
22563         (Main_Priority            : Integer;
22564          Time_Slice_Value         : Integer;
22565          WC_Encoding              : Character;
22566          Locking_Policy           : Character;
22567          Queuing_Policy           : Character;
22568          Task_Dispatching_Policy  : Character;
22569          Adafinal                 : System.Address;
22570          Unreserve_All_Interrupts : Integer;
22571          Exception_Tracebacks     : Integer);
22572 @findex __gnat_set_globals
22573       pragma Import (C, Set_Globals, "__gnat_set_globals");
22574
22575       --  SDP_Table_Build is a library routine used to build the
22576       --  exception tables. See unit Ada.Exceptions in files
22577       --  a-except.ads/adb for full details of how zero cost
22578       --  exception handling works. This procedure, the call to
22579       --  it, and the two following tables are all omitted if the
22580       --  build is in longjmp/setjump exception mode.
22581
22582 @findex SDP_Table_Build
22583 @findex Zero Cost Exceptions
22584       procedure SDP_Table_Build
22585         (SDP_Addresses   : System.Address;
22586          SDP_Count       : Natural;
22587          Elab_Addresses  : System.Address;
22588          Elab_Addr_Count : Natural);
22589       pragma Import (C, SDP_Table_Build, "__gnat_SDP_Table_Build");
22590
22591       --  Table of Unit_Exception_Table addresses. Used for zero
22592       --  cost exception handling to build the top level table.
22593
22594       ST : aliased constant array (1 .. 23) of System.Address := (
22595         Hello'UET_Address,
22596         Ada.Text_Io'UET_Address,
22597         Ada.Exceptions'UET_Address,
22598         Gnat.Heap_Sort_A'UET_Address,
22599         System.Exception_Table'UET_Address,
22600         System.Machine_State_Operations'UET_Address,
22601         System.Secondary_Stack'UET_Address,
22602         System.Parameters'UET_Address,
22603         System.Soft_Links'UET_Address,
22604         System.Stack_Checking'UET_Address,
22605         System.Traceback'UET_Address,
22606         Ada.Streams'UET_Address,
22607         Ada.Tags'UET_Address,
22608         System.String_Ops'UET_Address,
22609         Interfaces.C_Streams'UET_Address,
22610         System.File_Io'UET_Address,
22611         Ada.Finalization'UET_Address,
22612         System.Finalization_Root'UET_Address,
22613         System.Finalization_Implementation'UET_Address,
22614         System.String_Ops_Concat_3'UET_Address,
22615         System.Stream_Attributes'UET_Address,
22616         System.File_Control_Block'UET_Address,
22617         Ada.Finalization.List_Controller'UET_Address);
22618
22619       --  Table of addresses of elaboration routines. Used for
22620       --  zero cost exception handling to make sure these
22621       --  addresses are included in the top level procedure
22622       --  address table.
22623
22624       EA : aliased constant array (1 .. 23) of System.Address := (
22625         adainit'Code_Address,
22626         Do_Finalize'Code_Address,
22627         Ada.Exceptions'Elab_Spec'Address,
22628         System.Exceptions'Elab_Spec'Address,
22629         Interfaces.C_Streams'Elab_Spec'Address,
22630         System.Exception_Table'Elab_Body'Address,
22631         Ada.Io_Exceptions'Elab_Spec'Address,
22632         System.Stack_Checking'Elab_Spec'Address,
22633         System.Soft_Links'Elab_Body'Address,
22634         System.Secondary_Stack'Elab_Body'Address,
22635         Ada.Tags'Elab_Spec'Address,
22636         Ada.Tags'Elab_Body'Address,
22637         Ada.Streams'Elab_Spec'Address,
22638         System.Finalization_Root'Elab_Spec'Address,
22639         Ada.Exceptions'Elab_Body'Address,
22640         System.Finalization_Implementation'Elab_Spec'Address,
22641         System.Finalization_Implementation'Elab_Body'Address,
22642         Ada.Finalization'Elab_Spec'Address,
22643         Ada.Finalization.List_Controller'Elab_Spec'Address,
22644         System.File_Control_Block'Elab_Spec'Address,
22645         System.File_Io'Elab_Body'Address,
22646         Ada.Text_Io'Elab_Spec'Address,
22647         Ada.Text_Io'Elab_Body'Address);
22648
22649    --  Start of processing for adainit
22650
22651    begin
22652
22653       --  Call SDP_Table_Build to build the top level procedure
22654       --  table for zero cost exception handling (omitted in
22655       --  longjmp/setjump mode).
22656
22657       SDP_Table_Build (ST'Address, 23, EA'Address, 23);
22658
22659       --  Call Set_Globals to record various information for
22660       --  this partition.  The values are derived by the binder
22661       --  from information stored in the ali files by the compiler.
22662
22663 @findex __gnat_set_globals
22664       Set_Globals
22665         (Main_Priority            => -1,
22666          --  Priority of main program, -1 if no pragma Priority used
22667
22668          Time_Slice_Value         => -1,
22669          --  Time slice from Time_Slice pragma, -1 if none used
22670
22671          WC_Encoding              => 'b',
22672          --  Wide_Character encoding used, default is brackets
22673
22674          Locking_Policy           => ' ',
22675          --  Locking_Policy used, default of space means not
22676          --  specified, otherwise it is the first character of
22677          --  the policy name.
22678
22679          Queuing_Policy           => ' ',
22680          --  Queuing_Policy used, default of space means not
22681          --  specified, otherwise it is the first character of
22682          --  the policy name.
22683
22684          Task_Dispatching_Policy  => ' ',
22685          --  Task_Dispatching_Policy used, default of space means
22686          --  not specified, otherwise first character of the
22687          --  policy name.
22688
22689          Adafinal                 => System.Null_Address,
22690          --  Address of Adafinal routine, not used anymore
22691
22692          Unreserve_All_Interrupts => 0,
22693          --  Set true if pragma Unreserve_All_Interrupts was used
22694
22695          Exception_Tracebacks     => 0);
22696          --  Indicates if exception tracebacks are enabled
22697
22698       Elab_Final_Code := 1;
22699
22700       --  Now we have the elaboration calls for all units in the partition.
22701       --  The Elab_Spec and Elab_Body attributes generate references to the
22702       --  implicit elaboration procedures generated by the compiler for
22703       --  each unit that requires elaboration.
22704
22705       if not E040 then
22706          Interfaces.C_Streams'Elab_Spec;
22707       end if;
22708       E040 := True;
22709       if not E008 then
22710          Ada.Exceptions'Elab_Spec;
22711       end if;
22712       if not E014 then
22713          System.Exception_Table'Elab_Body;
22714          E014 := True;
22715       end if;
22716       if not E053 then
22717          Ada.Io_Exceptions'Elab_Spec;
22718          E053 := True;
22719       end if;
22720       if not E017 then
22721          System.Exceptions'Elab_Spec;
22722          E017 := True;
22723       end if;
22724       if not E030 then
22725          System.Stack_Checking'Elab_Spec;
22726       end if;
22727       if not E028 then
22728          System.Soft_Links'Elab_Body;
22729          E028 := True;
22730       end if;
22731       E030 := True;
22732       if not E024 then
22733          System.Secondary_Stack'Elab_Body;
22734          E024 := True;
22735       end if;
22736       if not E035 then
22737          Ada.Tags'Elab_Spec;
22738       end if;
22739       if not E035 then
22740          Ada.Tags'Elab_Body;
22741          E035 := True;
22742       end if;
22743       if not E033 then
22744          Ada.Streams'Elab_Spec;
22745          E033 := True;
22746       end if;
22747       if not E046 then
22748          System.Finalization_Root'Elab_Spec;
22749       end if;
22750       E046 := True;
22751       if not E008 then
22752          Ada.Exceptions'Elab_Body;
22753          E008 := True;
22754       end if;
22755       if not E048 then
22756          System.Finalization_Implementation'Elab_Spec;
22757       end if;
22758       if not E048 then
22759          System.Finalization_Implementation'Elab_Body;
22760          E048 := True;
22761       end if;
22762       if not E044 then
22763          Ada.Finalization'Elab_Spec;
22764       end if;
22765       E044 := True;
22766       if not E057 then
22767          Ada.Finalization.List_Controller'Elab_Spec;
22768       end if;
22769       E057 := True;
22770       if not E055 then
22771          System.File_Control_Block'Elab_Spec;
22772          E055 := True;
22773       end if;
22774       if not E042 then
22775          System.File_Io'Elab_Body;
22776          E042 := True;
22777       end if;
22778       if not E006 then
22779          Ada.Text_Io'Elab_Spec;
22780       end if;
22781       if not E006 then
22782          Ada.Text_Io'Elab_Body;
22783          E006 := True;
22784       end if;
22785
22786       Elab_Final_Code := 0;
22787    end adainit;
22788
22789    --------------
22790    -- adafinal --
22791    --------------
22792
22793 @findex adafinal
22794    procedure adafinal is
22795    begin
22796       Do_Finalize;
22797    end adafinal;
22798
22799    ----------
22800    -- main --
22801    ----------
22802
22803    --  main is actually a function, as in the ANSI C standard,
22804    --  defined to return the exit status. The three parameters
22805    --  are the argument count, argument values and environment
22806    --  pointer.
22807
22808 @findex Main Program
22809    function main
22810      (argc : Integer;
22811       argv : System.Address;
22812       envp : System.Address)
22813       return Integer
22814    is
22815       --  The initialize routine performs low level system
22816       --  initialization using a standard library routine which
22817       --  sets up signal handling and performs any other
22818       --  required setup. The routine can be found in file
22819       --  a-init.c.
22820
22821 @findex __gnat_initialize
22822       procedure initialize;
22823       pragma Import (C, initialize, "__gnat_initialize");
22824
22825       --  The finalize routine performs low level system
22826       --  finalization using a standard library routine. The
22827       --  routine is found in file a-final.c and in the standard
22828       --  distribution is a dummy routine that does nothing, so
22829       --  really this is a hook for special user finalization.
22830
22831 @findex __gnat_finalize
22832       procedure finalize;
22833       pragma Import (C, finalize, "__gnat_finalize");
22834
22835       --  We get to the main program of the partition by using
22836       --  pragma Import because if we try to with the unit and
22837       --  call it Ada style, then not only do we waste time
22838       --  recompiling it, but also, we don't really know the right
22839       --  switches (e.g. identifier character set) to be used
22840       --  to compile it.
22841
22842       procedure Ada_Main_Program;
22843       pragma Import (Ada, Ada_Main_Program, "_ada_hello");
22844
22845    --  Start of processing for main
22846
22847    begin
22848       --  Save global variables
22849
22850       gnat_argc := argc;
22851       gnat_argv := argv;
22852       gnat_envp := envp;
22853
22854       --  Call low level system initialization
22855
22856       Initialize;
22857
22858       --  Call our generated Ada initialization routine
22859
22860       adainit;
22861
22862       --  This is the point at which we want the debugger to get
22863       --  control
22864
22865       Break_Start;
22866
22867       --  Now we call the main program of the partition
22868
22869       Ada_Main_Program;
22870
22871       --  Perform Ada finalization
22872
22873       adafinal;
22874
22875       --  Perform low level system finalization
22876
22877       Finalize;
22878
22879       --  Return the proper exit status
22880       return (gnat_exit_status);
22881    end;
22882
22883 --  This section is entirely comments, so it has no effect on the
22884 --  compilation of the Ada_Main package. It provides the list of
22885 --  object files and linker options, as well as some standard
22886 --  libraries needed for the link. The gnatlink utility parses
22887 --  this b~hello.adb file to read these comment lines to generate
22888 --  the appropriate command line arguments for the call to the
22889 --  system linker. The BEGIN/END lines are used for sentinels for
22890 --  this parsing operation.
22891
22892 --  The exact file names will of course depend on the environment,
22893 --  host/target and location of files on the host system.
22894
22895 @findex Object file list
22896 -- BEGIN Object file/option list
22897    --   ./hello.o
22898    --   -L./
22899    --   -L/usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/
22900    --   /usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a
22901 -- END Object file/option list
22902
22903 end ada_main;
22904 @end smallexample
22905
22906 @noindent
22907 The Ada code in the above example is exactly what is generated by the
22908 binder. We have added comments to more clearly indicate the function
22909 of each part of the generated @code{Ada_Main} package.
22910
22911 The code is standard Ada in all respects, and can be processed by any
22912 tools that handle Ada. In particular, it is possible to use the debugger
22913 in Ada mode to debug the generated @code{Ada_Main} package. For example,
22914 suppose that for reasons that you do not understand, your program is crashing
22915 during elaboration of the body of @code{Ada.Text_IO}. To locate this bug,
22916 you can place a breakpoint on the call:
22917
22918 @smallexample @c ada
22919 Ada.Text_Io'Elab_Body;
22920 @end smallexample
22921
22922 @noindent
22923 and trace the elaboration routine for this package to find out where
22924 the problem might be (more usually of course you would be debugging
22925 elaboration code in your own application).
22926
22927 @node Elaboration Order Handling in GNAT
22928 @appendix Elaboration Order Handling in GNAT
22929 @cindex Order of elaboration
22930 @cindex Elaboration control
22931
22932 @menu
22933 * Elaboration Code in Ada 95::
22934 * Checking the Elaboration Order in Ada 95::
22935 * Controlling the Elaboration Order in Ada 95::
22936 * Controlling Elaboration in GNAT - Internal Calls::
22937 * Controlling Elaboration in GNAT - External Calls::
22938 * Default Behavior in GNAT - Ensuring Safety::
22939 * Treatment of Pragma Elaborate::
22940 * Elaboration Issues for Library Tasks::
22941 * Mixing Elaboration Models::
22942 * What to Do If the Default Elaboration Behavior Fails::
22943 * Elaboration for Access-to-Subprogram Values::
22944 * Summary of Procedures for Elaboration Control::
22945 * Other Elaboration Order Considerations::
22946 @end menu
22947
22948 @noindent
22949 This chapter describes the handling of elaboration code in Ada 95 and
22950 in GNAT, and discusses how the order of elaboration of program units can
22951 be controlled in GNAT, either automatically or with explicit programming
22952 features.
22953
22954 @node Elaboration Code in Ada 95
22955 @section Elaboration Code in Ada 95
22956
22957 @noindent
22958 Ada 95 provides rather general mechanisms for executing code at elaboration
22959 time, that is to say before the main program starts executing. Such code arises
22960 in three contexts:
22961
22962 @table @asis
22963 @item Initializers for variables.
22964 Variables declared at the library level, in package specs or bodies, can
22965 require initialization that is performed at elaboration time, as in:
22966 @smallexample @c ada
22967 @cartouche
22968 Sqrt_Half : Float := Sqrt (0.5);
22969 @end cartouche
22970 @end smallexample
22971
22972 @item Package initialization code
22973 Code in a @code{BEGIN-END} section at the outer level of a package body is
22974 executed as part of the package body elaboration code.
22975
22976 @item Library level task allocators
22977 Tasks that are declared using task allocators at the library level
22978 start executing immediately and hence can execute at elaboration time.
22979 @end table
22980
22981 @noindent
22982 Subprogram calls are possible in any of these contexts, which means that
22983 any arbitrary part of the program may be executed as part of the elaboration
22984 code. It is even possible to write a program which does all its work at
22985 elaboration time, with a null main program, although stylistically this
22986 would usually be considered an inappropriate way to structure
22987 a program.
22988
22989 An important concern arises in the context of elaboration code:
22990 we have to be sure that it is executed in an appropriate order. What we
22991 have is a series of elaboration code sections, potentially one section
22992 for each unit in the program. It is important that these execute
22993 in the correct order. Correctness here means that, taking the above
22994 example of the declaration of @code{Sqrt_Half},
22995 if some other piece of
22996 elaboration code references @code{Sqrt_Half},
22997 then it must run after the
22998 section of elaboration code that contains the declaration of
22999 @code{Sqrt_Half}.
23000
23001 There would never be any order of elaboration problem if we made a rule
23002 that whenever you @code{with} a unit, you must elaborate both the spec and body
23003 of that unit before elaborating the unit doing the @code{with}'ing:
23004
23005 @smallexample @c ada
23006 @group
23007 @cartouche
23008 with Unit_1;
23009 package Unit_2 is ...
23010 @end cartouche
23011 @end group
23012 @end smallexample
23013
23014 @noindent
23015 would require that both the body and spec of @code{Unit_1} be elaborated
23016 before the spec of @code{Unit_2}. However, a rule like that would be far too
23017 restrictive. In particular, it would make it impossible to have routines
23018 in separate packages that were mutually recursive.
23019
23020 You might think that a clever enough compiler could look at the actual
23021 elaboration code and determine an appropriate correct order of elaboration,
23022 but in the general case, this is not possible. Consider the following
23023 example.
23024
23025 In the body of @code{Unit_1}, we have a procedure @code{Func_1}
23026 that references
23027 the variable @code{Sqrt_1}, which is declared in the elaboration code
23028 of the body of @code{Unit_1}:
23029
23030 @smallexample @c ada
23031 @cartouche
23032 Sqrt_1 : Float := Sqrt (0.1);
23033 @end cartouche
23034 @end smallexample
23035
23036 @noindent
23037 The elaboration code of the body of @code{Unit_1} also contains:
23038
23039 @smallexample @c ada
23040 @group
23041 @cartouche
23042 if expression_1 = 1 then
23043    Q := Unit_2.Func_2;
23044 end if;
23045 @end cartouche
23046 @end group
23047 @end smallexample
23048
23049 @noindent
23050 @code{Unit_2} is exactly parallel,
23051 it has a procedure @code{Func_2} that references
23052 the variable @code{Sqrt_2}, which is declared in the elaboration code of
23053 the body @code{Unit_2}:
23054
23055 @smallexample @c ada
23056 @cartouche
23057 Sqrt_2 : Float := Sqrt (0.1);
23058 @end cartouche
23059 @end smallexample
23060
23061 @noindent
23062 The elaboration code of the body of @code{Unit_2} also contains:
23063
23064 @smallexample @c ada
23065 @group
23066 @cartouche
23067 if expression_2 = 2 then
23068    Q := Unit_1.Func_1;
23069 end if;
23070 @end cartouche
23071 @end group
23072 @end smallexample
23073
23074 @noindent
23075 Now the question is, which of the following orders of elaboration is
23076 acceptable:
23077
23078 @smallexample
23079 @group
23080 Spec of Unit_1
23081 Spec of Unit_2
23082 Body of Unit_1
23083 Body of Unit_2
23084 @end group
23085 @end smallexample
23086
23087 @noindent
23088 or
23089
23090 @smallexample
23091 @group
23092 Spec of Unit_2
23093 Spec of Unit_1
23094 Body of Unit_2
23095 Body of Unit_1
23096 @end group
23097 @end smallexample
23098
23099 @noindent
23100 If you carefully analyze the flow here, you will see that you cannot tell
23101 at compile time the answer to this question.
23102 If @code{expression_1} is not equal to 1,
23103 and @code{expression_2} is not equal to 2,
23104 then either order is acceptable, because neither of the function calls is
23105 executed. If both tests evaluate to true, then neither order is acceptable
23106 and in fact there is no correct order.
23107
23108 If one of the two expressions is true, and the other is false, then one
23109 of the above orders is correct, and the other is incorrect. For example,
23110 if @code{expression_1} = 1 and @code{expression_2} /= 2,
23111 then the call to @code{Func_2}
23112 will occur, but not the call to @code{Func_1.}
23113 This means that it is essential
23114 to elaborate the body of @code{Unit_1} before
23115 the body of @code{Unit_2}, so the first
23116 order of elaboration is correct and the second is wrong.
23117
23118 By making @code{expression_1} and @code{expression_2}
23119 depend on input data, or perhaps
23120 the time of day, we can make it impossible for the compiler or binder
23121 to figure out which of these expressions will be true, and hence it
23122 is impossible to guarantee a safe order of elaboration at run time.
23123
23124 @node Checking the Elaboration Order in Ada 95
23125 @section Checking the Elaboration Order in Ada 95
23126
23127 @noindent
23128 In some languages that involve the same kind of elaboration problems,
23129 e.g. Java and C++, the programmer is expected to worry about these
23130 ordering problems himself, and it is common to
23131 write a program in which an incorrect elaboration order  gives
23132 surprising results, because it references variables before they
23133 are initialized.
23134 Ada 95 is designed to be a safe language, and a programmer-beware approach is
23135 clearly not sufficient. Consequently, the language provides three lines
23136 of defense:
23137
23138 @table @asis
23139 @item Standard rules
23140 Some standard rules restrict the possible choice of elaboration
23141 order. In particular, if you @code{with} a unit, then its spec is always
23142 elaborated before the unit doing the @code{with}. Similarly, a parent
23143 spec is always elaborated before the child spec, and finally
23144 a spec is always elaborated before its corresponding body.
23145
23146 @item Dynamic elaboration checks
23147 @cindex Elaboration checks
23148 @cindex Checks, elaboration
23149 Dynamic checks are made at run time, so that if some entity is accessed
23150 before it is elaborated (typically  by means of a subprogram call)
23151 then the exception (@code{Program_Error}) is raised.
23152
23153 @item Elaboration control
23154 Facilities are provided for the programmer to specify the desired order
23155 of elaboration.
23156 @end table
23157
23158 Let's look at these facilities in more detail. First, the rules for
23159 dynamic checking. One possible rule would be simply to say that the
23160 exception is raised if you access a variable which has not yet been
23161 elaborated. The trouble with this approach is that it could require
23162 expensive checks on every variable reference. Instead Ada 95 has two
23163 rules which are a little more restrictive, but easier to check, and
23164 easier to state:
23165
23166 @table @asis
23167 @item Restrictions on calls
23168 A subprogram can only be called at elaboration time if its body
23169 has been elaborated. The rules for elaboration given above guarantee
23170 that the spec of the subprogram has been elaborated before the
23171 call, but not the body. If this rule is violated, then the
23172 exception @code{Program_Error} is raised.
23173
23174 @item Restrictions on instantiations
23175 A generic unit can only be instantiated if the body of the generic
23176 unit has been elaborated. Again, the rules for elaboration given above
23177 guarantee that the spec of the generic unit has been elaborated
23178 before the instantiation, but not the body. If this rule is
23179 violated, then the exception @code{Program_Error} is raised.
23180 @end table
23181
23182 @noindent
23183 The idea is that if the body has been elaborated, then any variables
23184 it references must have been elaborated; by checking for the body being
23185 elaborated we guarantee that none of its references causes any
23186 trouble. As we noted above, this is a little too restrictive, because a
23187 subprogram that has no non-local references in its body may in fact be safe
23188 to call. However, it really would be unsafe to rely on this, because
23189 it would mean that the caller was aware of details of the implementation
23190 in the body. This goes against the basic tenets of Ada.
23191
23192 A plausible implementation can be described as follows.
23193 A Boolean variable is associated with each subprogram
23194 and each generic unit. This variable is initialized to False, and is set to
23195 True at the point body is elaborated. Every call or instantiation checks the
23196 variable, and raises @code{Program_Error} if the variable is False.
23197
23198 Note that one might think that it would be good enough to have one Boolean
23199 variable for each package, but that would not deal with cases of trying
23200 to call a body in the same package as the call
23201 that has not been elaborated yet.
23202 Of course a compiler may be able to do enough analysis to optimize away
23203 some of the Boolean variables as unnecessary, and @code{GNAT} indeed
23204 does such optimizations, but still the easiest conceptual model is to
23205 think of there being one variable per subprogram.
23206
23207 @node Controlling the Elaboration Order in Ada 95
23208 @section Controlling the Elaboration Order in Ada 95
23209
23210 @noindent
23211 In the previous section we discussed the rules in Ada 95 which ensure
23212 that @code{Program_Error} is raised if an incorrect elaboration order is
23213 chosen. This prevents erroneous executions, but we need mechanisms to
23214 specify a correct execution and avoid the exception altogether.
23215 To achieve this, Ada 95 provides a number of features for controlling
23216 the order of elaboration. We discuss these features in this section.
23217
23218 First, there are several ways of indicating to the compiler that a given
23219 unit has no elaboration problems:
23220
23221 @table @asis
23222 @item packages that do not require a body
23223 In Ada 95, a library package that does not require a body does not permit
23224 a body. This means that if we have a such a package, as in:
23225
23226 @smallexample @c ada
23227 @group
23228 @cartouche
23229 package Definitions is
23230    generic
23231       type m is new integer;
23232    package Subp is
23233       type a is array (1 .. 10) of m;
23234       type b is array (1 .. 20) of m;
23235    end Subp;
23236 end Definitions;
23237 @end cartouche
23238 @end group
23239 @end smallexample
23240
23241 @noindent
23242 A package that @code{with}'s @code{Definitions} may safely instantiate
23243 @code{Definitions.Subp} because the compiler can determine that there
23244 definitely is no package body to worry about in this case
23245
23246 @item pragma Pure
23247 @cindex pragma Pure
23248 @findex Pure
23249 Places sufficient restrictions on a unit to guarantee that
23250 no call to any subprogram in the unit can result in an
23251 elaboration problem. This means that the compiler does not need
23252 to worry about the point of elaboration of such units, and in
23253 particular, does not need to check any calls to any subprograms
23254 in this unit.
23255
23256 @item pragma Preelaborate
23257 @findex Preelaborate
23258 @cindex pragma Preelaborate
23259 This pragma places slightly less stringent restrictions on a unit than
23260 does pragma Pure,
23261 but these restrictions are still sufficient to ensure that there
23262 are no elaboration problems with any calls to the unit.
23263
23264 @item pragma Elaborate_Body
23265 @findex Elaborate_Body
23266 @cindex pragma Elaborate_Body
23267 This pragma requires that the body of a unit be elaborated immediately
23268 after its spec. Suppose a unit @code{A} has such a pragma,
23269 and unit @code{B} does
23270 a @code{with} of unit @code{A}. Recall that the standard rules require
23271 the spec of unit @code{A}
23272 to be elaborated before the @code{with}'ing unit; given the pragma in
23273 @code{A}, we also know that the body of @code{A}
23274 will be elaborated before @code{B}, so
23275 that calls to @code{A} are safe and do not need a check.
23276 @end table
23277
23278 @noindent
23279 Note that,
23280 unlike pragma @code{Pure} and pragma @code{Preelaborate},
23281 the use of
23282 @code{Elaborate_Body} does not guarantee that the program is
23283 free of elaboration problems, because it may not be possible
23284 to satisfy the requested elaboration order.
23285 Let's go back to the example with @code{Unit_1} and @code{Unit_2}.
23286 If a programmer
23287 marks @code{Unit_1} as @code{Elaborate_Body},
23288 and not @code{Unit_2,} then the order of
23289 elaboration will be:
23290
23291 @smallexample
23292 @group
23293 Spec of Unit_2
23294 Spec of Unit_1
23295 Body of Unit_1
23296 Body of Unit_2
23297 @end group
23298 @end smallexample
23299
23300 @noindent
23301 Now that means that the call to @code{Func_1} in @code{Unit_2}
23302 need not be checked,
23303 it must be safe. But the call to @code{Func_2} in
23304 @code{Unit_1} may still fail if
23305 @code{Expression_1} is equal to 1,
23306 and the programmer must still take
23307 responsibility for this not being the case.
23308
23309 If all units carry a pragma @code{Elaborate_Body}, then all problems are
23310 eliminated, except for calls entirely within a body, which are
23311 in any case fully under programmer control. However, using the pragma
23312 everywhere is not always possible.
23313 In particular, for our @code{Unit_1}/@code{Unit_2} example, if
23314 we marked both of them as having pragma @code{Elaborate_Body}, then
23315 clearly there would be no possible elaboration order.
23316
23317 The above pragmas allow a server to guarantee safe use by clients, and
23318 clearly this is the preferable approach. Consequently a good rule in
23319 Ada 95 is to mark units as @code{Pure} or @code{Preelaborate} if possible,
23320 and if this is not possible,
23321 mark them as @code{Elaborate_Body} if possible.
23322 As we have seen, there are situations where neither of these
23323 three pragmas can be used.
23324 So we also provide methods for clients to control the
23325 order of elaboration of the servers on which they depend:
23326
23327 @table @asis
23328 @item pragma Elaborate (unit)
23329 @findex Elaborate
23330 @cindex pragma Elaborate
23331 This pragma is placed in the context clause, after a @code{with} clause,
23332 and it requires that the body of the named unit be elaborated before
23333 the unit in which the pragma occurs. The idea is to use this pragma
23334 if the current unit calls at elaboration time, directly or indirectly,
23335 some subprogram in the named unit.
23336
23337 @item pragma Elaborate_All (unit)
23338 @findex Elaborate_All
23339 @cindex pragma Elaborate_All
23340 This is a stronger version of the Elaborate pragma. Consider the
23341 following example:
23342
23343 @smallexample
23344 Unit A @code{with}'s unit B and calls B.Func in elab code
23345 Unit B @code{with}'s unit C, and B.Func calls C.Func
23346 @end smallexample
23347
23348 @noindent
23349 Now if we put a pragma @code{Elaborate (B)}
23350 in unit @code{A}, this ensures that the
23351 body of @code{B} is elaborated before the call, but not the
23352 body of @code{C}, so
23353 the call to @code{C.Func} could still cause @code{Program_Error} to
23354 be raised.
23355
23356 The effect of a pragma @code{Elaborate_All} is stronger, it requires
23357 not only that the body of the named unit be elaborated before the
23358 unit doing the @code{with}, but also the bodies of all units that the
23359 named unit uses, following @code{with} links transitively. For example,
23360 if we put a pragma @code{Elaborate_All (B)} in unit @code{A},
23361 then it requires
23362 not only that the body of @code{B} be elaborated before @code{A},
23363 but also the
23364 body of @code{C}, because @code{B} @code{with}'s @code{C}.
23365 @end table
23366
23367 @noindent
23368 We are now in a position to give a usage rule in Ada 95 for avoiding
23369 elaboration problems, at least if dynamic dispatching and access to
23370 subprogram values are not used. We will handle these cases separately
23371 later.
23372
23373 The rule is simple. If a unit has elaboration code that can directly or
23374 indirectly make a call to a subprogram in a @code{with}'ed unit, or instantiate
23375 a generic package in a @code{with}'ed unit,
23376 then if the @code{with}'ed unit does not have
23377 pragma @code{Pure} or @code{Preelaborate}, then the client should have
23378 a pragma @code{Elaborate_All}
23379 for the @code{with}'ed unit. By following this rule a client is
23380 assured that calls can be made without risk of an exception.
23381
23382 For generic subprogram instantiations, the rule can be relaxed to
23383 require only a pragma @code{Elaborate} since elaborating the body
23384 of a subprogram cannot cause any transitive elaboration (we are
23385 not calling the subprogram in this case, just elaborating its
23386 declaration).
23387
23388 If this rule is not followed, then a program may be in one of four
23389 states:
23390
23391 @table @asis
23392 @item No order exists
23393 No order of elaboration exists which follows the rules, taking into
23394 account any @code{Elaborate}, @code{Elaborate_All},
23395 or @code{Elaborate_Body} pragmas. In
23396 this case, an Ada 95 compiler must diagnose the situation at bind
23397 time, and refuse to build an executable program.
23398
23399 @item One or more orders exist, all incorrect
23400 One or more acceptable elaboration orders exists, and all of them
23401 generate an elaboration order problem. In this case, the binder
23402 can build an executable program, but @code{Program_Error} will be raised
23403 when the program is run.
23404
23405 @item Several orders exist, some right, some incorrect
23406 One or more acceptable elaboration orders exists, and some of them
23407 work, and some do not. The programmer has not controlled
23408 the order of elaboration, so the binder may or may not pick one of
23409 the correct orders, and the program may or may not raise an
23410 exception when it is run. This is the worst case, because it means
23411 that the program may fail when moved to another compiler, or even
23412 another version of the same compiler.
23413
23414 @item One or more orders exists, all correct
23415 One ore more acceptable elaboration orders exist, and all of them
23416 work. In this case the program runs successfully. This state of
23417 affairs can be guaranteed by following the rule we gave above, but
23418 may be true even if the rule is not followed.
23419 @end table
23420
23421 @noindent
23422 Note that one additional advantage of following our rules on the use
23423 of @code{Elaborate} and @code{Elaborate_All}
23424 is that the program continues to stay in the ideal (all orders OK) state
23425 even if maintenance
23426 changes some bodies of some units. Conversely, if a program that does
23427 not follow this rule happens to be safe at some point, this state of affairs
23428 may deteriorate silently as a result of maintenance changes.
23429
23430 You may have noticed that the above discussion did not mention
23431 the use of @code{Elaborate_Body}. This was a deliberate omission. If you
23432 @code{with} an @code{Elaborate_Body} unit, it still may be the case that
23433 code in the body makes calls to some other unit, so it is still necessary
23434 to use @code{Elaborate_All} on such units.
23435
23436 @node Controlling Elaboration in GNAT - Internal Calls
23437 @section Controlling Elaboration in GNAT - Internal Calls
23438
23439 @noindent
23440 In the case of internal calls, i.e. calls within a single package, the
23441 programmer has full control over the order of elaboration, and it is up
23442 to the programmer to elaborate declarations in an appropriate order. For
23443 example writing:
23444
23445 @smallexample @c ada
23446 @group
23447 @cartouche
23448 function One return Float;
23449
23450 Q : Float := One;
23451
23452 function One return Float is
23453 begin
23454      return 1.0;
23455 end One;
23456 @end cartouche
23457 @end group
23458 @end smallexample
23459
23460 @noindent
23461 will obviously raise @code{Program_Error} at run time, because function
23462 One will be called before its body is elaborated. In this case GNAT will
23463 generate a warning that the call will raise @code{Program_Error}:
23464
23465 @smallexample
23466 @group
23467 @cartouche
23468  1. procedure y is
23469  2.    function One return Float;
23470  3.
23471  4.    Q : Float := One;
23472                     |
23473     >>> warning: cannot call "One" before body is elaborated
23474     >>> warning: Program_Error will be raised at run time
23475
23476  5.
23477  6.    function One return Float is
23478  7.    begin
23479  8.         return 1.0;
23480  9.    end One;
23481 10.
23482 11. begin
23483 12.    null;
23484 13. end;
23485 @end cartouche
23486 @end group
23487 @end smallexample
23488
23489 @noindent
23490 Note that in this particular case, it is likely that the call is safe, because
23491 the function @code{One} does not access any global variables.
23492 Nevertheless in Ada 95, we do not want the validity of the check to depend on
23493 the contents of the body (think about the separate compilation case), so this
23494 is still wrong, as we discussed in the previous sections.
23495
23496 The error is easily corrected by rearranging the declarations so that the
23497 body of One appears before the declaration containing the call
23498 (note that in Ada 95,
23499 declarations can appear in any order, so there is no restriction that
23500 would prevent this reordering, and if we write:
23501
23502 @smallexample @c ada
23503 @group
23504 @cartouche
23505 function One return Float;
23506
23507 function One return Float is
23508 begin
23509      return 1.0;
23510 end One;
23511
23512 Q : Float := One;
23513 @end cartouche
23514 @end group
23515 @end smallexample
23516
23517 @noindent
23518 then all is well, no warning is generated, and no
23519 @code{Program_Error} exception
23520 will be raised.
23521 Things are more complicated when a chain of subprograms is executed:
23522
23523 @smallexample @c ada
23524 @group
23525 @cartouche
23526 function A return Integer;
23527 function B return Integer;
23528 function C return Integer;
23529
23530 function B return Integer is begin return A; end;
23531 function C return Integer is begin return B; end;
23532
23533 X : Integer := C;
23534
23535 function A return Integer is begin return 1; end;
23536 @end cartouche
23537 @end group
23538 @end smallexample
23539
23540 @noindent
23541 Now the call to @code{C}
23542 at elaboration time in the declaration of @code{X} is correct, because
23543 the body of @code{C} is already elaborated,
23544 and the call to @code{B} within the body of
23545 @code{C} is correct, but the call
23546 to @code{A} within the body of @code{B} is incorrect, because the body
23547 of @code{A} has not been elaborated, so @code{Program_Error}
23548 will be raised on the call to @code{A}.
23549 In this case GNAT will generate a
23550 warning that @code{Program_Error} may be
23551 raised at the point of the call. Let's look at the warning:
23552
23553 @smallexample
23554 @group
23555 @cartouche
23556  1. procedure x is
23557  2.    function A return Integer;
23558  3.    function B return Integer;
23559  4.    function C return Integer;
23560  5.
23561  6.    function B return Integer is begin return A; end;
23562                                                     |
23563     >>> warning: call to "A" before body is elaborated may
23564                  raise Program_Error
23565     >>> warning: "B" called at line 7
23566     >>> warning: "C" called at line 9
23567
23568  7.    function C return Integer is begin return B; end;
23569  8.
23570  9.    X : Integer := C;
23571 10.
23572 11.    function A return Integer is begin return 1; end;
23573 12.
23574 13. begin
23575 14.    null;
23576 15. end;
23577 @end cartouche
23578 @end group
23579 @end smallexample
23580
23581 @noindent
23582 Note that the message here says ``may raise'', instead of the direct case,
23583 where the message says ``will be raised''. That's because whether
23584 @code{A} is
23585 actually called depends in general on run-time flow of control.
23586 For example, if the body of @code{B} said
23587
23588 @smallexample @c ada
23589 @group
23590 @cartouche
23591 function B return Integer is
23592 begin
23593    if some-condition-depending-on-input-data then
23594       return A;
23595    else
23596       return 1;
23597    end if;
23598 end B;
23599 @end cartouche
23600 @end group
23601 @end smallexample
23602
23603 @noindent
23604 then we could not know until run time whether the incorrect call to A would
23605 actually occur, so @code{Program_Error} might
23606 or might not be raised. It is possible for a compiler to
23607 do a better job of analyzing bodies, to
23608 determine whether or not @code{Program_Error}
23609 might be raised, but it certainly
23610 couldn't do a perfect job (that would require solving the halting problem
23611 and is provably impossible), and because this is a warning anyway, it does
23612 not seem worth the effort to do the analysis. Cases in which it
23613 would be relevant are rare.
23614
23615 In practice, warnings of either of the forms given
23616 above will usually correspond to
23617 real errors, and should be examined carefully and eliminated.
23618 In the rare case where a warning is bogus, it can be suppressed by any of
23619 the following methods:
23620
23621 @itemize @bullet
23622 @item
23623 Compile with the @option{-gnatws} switch set
23624
23625 @item
23626 Suppress @code{Elaboration_Check} for the called subprogram
23627
23628 @item
23629 Use pragma @code{Warnings_Off} to turn warnings off for the call
23630 @end itemize
23631
23632 @noindent
23633 For the internal elaboration check case,
23634 GNAT by default generates the
23635 necessary run-time checks to ensure
23636 that @code{Program_Error} is raised if any
23637 call fails an elaboration check. Of course this can only happen if a
23638 warning has been issued as described above. The use of pragma
23639 @code{Suppress (Elaboration_Check)} may (but is not guaranteed to) suppress
23640 some of these checks, meaning that it may be possible (but is not
23641 guaranteed) for a program to be able to call a subprogram whose body
23642 is not yet elaborated, without raising a @code{Program_Error} exception.
23643
23644 @node Controlling Elaboration in GNAT - External Calls
23645 @section Controlling Elaboration in GNAT - External Calls
23646
23647 @noindent
23648 The previous section discussed the case in which the execution of a
23649 particular thread of elaboration code occurred entirely within a
23650 single unit. This is the easy case to handle, because a programmer
23651 has direct and total control over the order of elaboration, and
23652 furthermore, checks need only be generated in cases which are rare
23653 and which the compiler can easily detect.
23654 The situation is more complex when separate compilation is taken into account.
23655 Consider the following:
23656
23657 @smallexample @c ada
23658 @cartouche
23659 @group
23660 package Math is
23661    function Sqrt (Arg : Float) return Float;
23662 end Math;
23663
23664 package body Math is
23665    function Sqrt (Arg : Float) return Float is
23666    begin
23667          ...
23668    end Sqrt;
23669 end Math;
23670 @end group
23671 @group
23672 with Math;
23673 package Stuff is
23674    X : Float := Math.Sqrt (0.5);
23675 end Stuff;
23676
23677 with Stuff;
23678 procedure Main is
23679 begin
23680    ...
23681 end Main;
23682 @end group
23683 @end cartouche
23684 @end smallexample
23685
23686 @noindent
23687 where @code{Main} is the main program. When this program is executed, the
23688 elaboration code must first be executed, and one of the jobs of the
23689 binder is to determine the order in which the units of a program are
23690 to be elaborated. In this case we have four units: the spec and body
23691 of @code{Math},
23692 the spec of @code{Stuff} and the body of @code{Main}).
23693 In what order should the four separate sections of elaboration code
23694 be executed?
23695
23696 There are some restrictions in the order of elaboration that the binder
23697 can choose. In particular, if unit U has a @code{with}
23698 for a package @code{X}, then you
23699 are assured that the spec of @code{X}
23700 is elaborated before U , but you are
23701 not assured that the body of @code{X}
23702 is elaborated before U.
23703 This means that in the above case, the binder is allowed to choose the
23704 order:
23705
23706 @smallexample
23707 spec of Math
23708 spec of Stuff
23709 body of Math
23710 body of Main
23711 @end smallexample
23712
23713 @noindent
23714 but that's not good, because now the call to @code{Math.Sqrt}
23715 that happens during
23716 the elaboration of the @code{Stuff}
23717 spec happens before the body of @code{Math.Sqrt} is
23718 elaborated, and hence causes @code{Program_Error} exception to be raised.
23719 At first glance, one might say that the binder is misbehaving, because
23720 obviously you want to elaborate the body of something you @code{with}
23721 first, but
23722 that is not a general rule that can be followed in all cases. Consider
23723
23724 @smallexample @c ada
23725 @group
23726 @cartouche
23727 package X is ...
23728
23729 package Y is ...
23730
23731 with X;
23732 package body Y is ...
23733
23734 with Y;
23735 package body X is ...
23736 @end cartouche
23737 @end group
23738 @end smallexample
23739
23740 @noindent
23741 This is a common arrangement, and, apart from the order of elaboration
23742 problems that might arise in connection with elaboration code, this works fine.
23743 A rule that says that you must first elaborate the body of anything you
23744 @code{with} cannot work in this case:
23745 the body of @code{X} @code{with}'s @code{Y},
23746 which means you would have to
23747 elaborate the body of @code{Y} first, but that @code{with}'s @code{X},
23748 which means
23749 you have to elaborate the body of @code{X} first, but ... and we have a
23750 loop that cannot be broken.
23751
23752 It is true that the binder can in many cases guess an order of elaboration
23753 that is unlikely to cause a @code{Program_Error}
23754 exception to be raised, and it tries to do so (in the
23755 above example of @code{Math/Stuff/Spec}, the GNAT binder will
23756 by default
23757 elaborate the body of @code{Math} right after its spec, so all will be well).
23758
23759 However, a program that blindly relies on the binder to be helpful can
23760 get into trouble, as we discussed in the previous sections, so
23761 GNAT
23762 provides a number of facilities for assisting the programmer in
23763 developing programs that are robust with respect to elaboration order.
23764
23765 @node Default Behavior in GNAT - Ensuring Safety
23766 @section Default Behavior in GNAT - Ensuring Safety
23767
23768 @noindent
23769 The default behavior in GNAT ensures elaboration safety. In its
23770 default mode GNAT implements the
23771 rule we previously described as the right approach. Let's restate it:
23772
23773 @itemize
23774 @item
23775 @emph{If a unit has elaboration code that can directly or indirectly make a
23776 call to a subprogram in a @code{with}'ed unit, or instantiate a generic
23777 package in a @code{with}'ed unit, then if the @code{with}'ed unit
23778 does not have pragma @code{Pure} or
23779 @code{Preelaborate}, then the client should have an
23780 @code{Elaborate_All} pragma for the @code{with}'ed unit.}
23781
23782 @emph{In the case of instantiating a generic subprogram, it is always
23783 sufficient to have only an @code{Elaborate} pragma for the
23784 @code{with}'ed unit.}
23785 @end itemize
23786
23787 @noindent
23788 By following this rule a client is assured that calls and instantiations
23789 can be made without risk of an exception.
23790
23791 In this mode GNAT traces all calls that are potentially made from
23792 elaboration code, and puts in any missing implicit @code{Elaborate}
23793 and @code{Elaborate_All} pragmas.
23794 The advantage of this approach is that no elaboration problems
23795 are possible if the binder can find an elaboration order that is
23796 consistent with these implicit @code{Elaborate} and
23797 @code{Elaborate_All} pragmas. The
23798 disadvantage of this approach is that no such order may exist.
23799
23800 If the binder does not generate any diagnostics, then it means that it has
23801 found an elaboration order that is guaranteed to be safe. However, the binder
23802 may still be relying on implicitly generated @code{Elaborate} and
23803 @code{Elaborate_All} pragmas so portability to other compilers than GNAT is not
23804 guaranteed.
23805
23806 If it is important to guarantee portability, then the compilations should
23807 use the
23808 @option{-gnatwl}
23809 (warn on elaboration problems) switch. This will cause warning messages
23810 to be generated indicating the missing @code{Elaborate} and
23811 @code{Elaborate_All} pragmas.
23812 Consider the following source program:
23813
23814 @smallexample @c ada
23815 @group
23816 @cartouche
23817 with k;
23818 package j is
23819   m : integer := k.r;
23820 end;
23821 @end cartouche
23822 @end group
23823 @end smallexample
23824
23825 @noindent
23826 where it is clear that there
23827 should be a pragma @code{Elaborate_All}
23828 for unit @code{k}. An implicit pragma will be generated, and it is
23829 likely that the binder will be able to honor it. However, if you want
23830 to port this program to some other Ada compiler than GNAT.
23831 it is safer to include the pragma explicitly in the source. If this
23832 unit is compiled with the
23833 @option{-gnatwl}
23834 switch, then the compiler outputs a warning:
23835
23836 @smallexample
23837 @group
23838 @cartouche
23839 1. with k;
23840 2. package j is
23841 3.   m : integer := k.r;
23842                      |
23843    >>> warning: call to "r" may raise Program_Error
23844    >>> warning: missing pragma Elaborate_All for "k"
23845
23846 4. end;
23847 @end cartouche
23848 @end group
23849 @end smallexample
23850
23851 @noindent
23852 and these warnings can be used as a guide for supplying manually
23853 the missing pragmas. It is usually a bad idea to use this warning
23854 option during development. That's because it will warn you when
23855 you need to put in a pragma, but cannot warn you when it is time
23856 to take it out. So the use of pragma @code{Elaborate_All} may lead to
23857 unnecessary dependencies and even false circularities.
23858
23859 This default mode is more restrictive than the Ada Reference
23860 Manual, and it is possible to construct programs which will compile
23861 using the dynamic model described there, but will run into a
23862 circularity using the safer static model we have described.
23863
23864 Of course any Ada compiler must be able to operate in a mode
23865 consistent with the requirements of the Ada Reference Manual,
23866 and in particular must have the capability of implementing the
23867 standard dynamic model of elaboration with run-time checks.
23868
23869 In GNAT, this standard mode can be achieved either by the use of
23870 the @option{-gnatE} switch on the compiler (@command{gcc} or
23871 @command{gnatmake}) command, or by the use of the configuration pragma:
23872
23873 @smallexample @c ada
23874 pragma Elaboration_Checks (RM);
23875 @end smallexample
23876
23877 @noindent
23878 Either approach will cause the unit affected to be compiled using the
23879 standard dynamic run-time elaboration checks described in the Ada
23880 Reference Manual. The static model is generally preferable, since it
23881 is clearly safer to rely on compile and link time checks rather than
23882 run-time checks. However, in the case of legacy code, it may be
23883 difficult to meet the requirements of the static model. This
23884 issue is further discussed in
23885 @ref{What to Do If the Default Elaboration Behavior Fails}.
23886
23887 Note that the static model provides a strict subset of the allowed
23888 behavior and programs of the Ada Reference Manual, so if you do
23889 adhere to the static model and no circularities exist,
23890 then you are assured that your program will
23891 work using the dynamic model, providing that you remove any
23892 pragma Elaborate statements from the source.
23893
23894 @node Treatment of Pragma Elaborate
23895 @section Treatment of Pragma Elaborate
23896 @cindex Pragma Elaborate
23897
23898 @noindent
23899 The use of @code{pragma Elaborate}
23900 should generally be avoided in Ada 95 programs.
23901 The reason for this is that there is no guarantee that transitive calls
23902 will be properly handled. Indeed at one point, this pragma was placed
23903 in Annex J (Obsolescent Features), on the grounds that it is never useful.
23904
23905 Now that's a bit restrictive. In practice, the case in which
23906 @code{pragma Elaborate} is useful is when the caller knows that there
23907 are no transitive calls, or that the called unit contains all necessary
23908 transitive @code{pragma Elaborate} statements, and legacy code often
23909 contains such uses.
23910
23911 Strictly speaking the static mode in GNAT should ignore such pragmas,
23912 since there is no assurance at compile time that the necessary safety
23913 conditions are met. In practice, this would cause GNAT to be incompatible
23914 with correctly written Ada 83 code that had all necessary
23915 @code{pragma Elaborate} statements in place. Consequently, we made the
23916 decision that GNAT in its default mode will believe that if it encounters
23917 a @code{pragma Elaborate} then the programmer knows what they are doing,
23918 and it will trust that no elaboration errors can occur.
23919
23920 The result of this decision is two-fold. First to be safe using the
23921 static mode, you should remove all @code{pragma Elaborate} statements.
23922 Second, when fixing circularities in existing code, you can selectively
23923 use @code{pragma Elaborate} statements to convince the static mode of
23924 GNAT that it need not generate an implicit @code{pragma Elaborate_All}
23925 statement.
23926
23927 When using the static mode with @option{-gnatwl}, any use of
23928 @code{pragma Elaborate} will generate a warning about possible
23929 problems.
23930
23931 @node Elaboration Issues for Library Tasks
23932 @section Elaboration Issues for Library Tasks
23933 @cindex Library tasks, elaboration issues
23934 @cindex Elaboration of library tasks
23935
23936 @noindent
23937 In this section we examine special elaboration issues that arise for
23938 programs that declare library level tasks.
23939
23940 Generally the model of execution of an Ada program is that all units are
23941 elaborated, and then execution of the program starts. However, the
23942 declaration of library tasks definitely does not fit this model. The
23943 reason for this is that library tasks start as soon as they are declared
23944 (more precisely, as soon as the statement part of the enclosing package
23945 body is reached), that is to say before elaboration
23946 of the program is complete. This means that if such a task calls a
23947 subprogram, or an entry in another task, the callee may or may not be
23948 elaborated yet, and in the standard
23949 Reference Manual model of dynamic elaboration checks, you can even
23950 get timing dependent Program_Error exceptions, since there can be
23951 a race between the elaboration code and the task code.
23952
23953 The static model of elaboration in GNAT seeks to avoid all such
23954 dynamic behavior, by being conservative, and the conservative
23955 approach in this particular case is to assume that all the code
23956 in a task body is potentially executed at elaboration time if
23957 a task is declared at the library level.
23958
23959 This can definitely result in unexpected circularities. Consider
23960 the following example
23961
23962 @smallexample @c ada
23963 package Decls is
23964   task Lib_Task is
23965      entry Start;
23966   end Lib_Task;
23967
23968   type My_Int is new Integer;
23969
23970   function Ident (M : My_Int) return My_Int;
23971 end Decls;
23972
23973 with Utils;
23974 package body Decls is
23975   task body Lib_Task is
23976   begin
23977      accept Start;
23978      Utils.Put_Val (2);
23979   end Lib_Task;
23980
23981   function Ident (M : My_Int) return My_Int is
23982   begin
23983      return M;
23984   end Ident;
23985 end Decls;
23986
23987 with Decls;
23988 package Utils is
23989   procedure Put_Val (Arg : Decls.My_Int);
23990 end Utils;
23991
23992 with Text_IO;
23993 package body Utils is
23994   procedure Put_Val (Arg : Decls.My_Int) is
23995   begin
23996      Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
23997   end Put_Val;
23998 end Utils;
23999
24000 with Decls;
24001 procedure Main is
24002 begin
24003    Decls.Lib_Task.Start;
24004 end;
24005 @end smallexample
24006
24007 @noindent
24008 If the above example is compiled in the default static elaboration
24009 mode, then a circularity occurs. The circularity comes from the call
24010 @code{Utils.Put_Val} in the task body of @code{Decls.Lib_Task}. Since
24011 this call occurs in elaboration code, we need an implicit pragma
24012 @code{Elaborate_All} for @code{Utils}. This means that not only must
24013 the spec and body of @code{Utils} be elaborated before the body
24014 of @code{Decls}, but also the spec and body of any unit that is
24015 @code{with'ed} by the body of @code{Utils} must also be elaborated before
24016 the body of @code{Decls}. This is the transitive implication of
24017 pragma @code{Elaborate_All} and it makes sense, because in general
24018 the body of @code{Put_Val} might have a call to something in a
24019 @code{with'ed} unit.
24020
24021 In this case, the body of Utils (actually its spec) @code{with's}
24022 @code{Decls}. Unfortunately this means that the body of @code{Decls}
24023 must be elaborated before itself, in case there is a call from the
24024 body of @code{Utils}.
24025
24026 Here is the exact chain of events we are worrying about:
24027
24028 @enumerate
24029 @item
24030 In the body of @code{Decls} a call is made from within the body of a library
24031 task to a subprogram in the package @code{Utils}. Since this call may
24032 occur at elaboration time (given that the task is activated at elaboration
24033 time), we have to assume the worst, i.e. that the
24034 call does happen at elaboration time.
24035
24036 @item
24037 This means that the body and spec of @code{Util} must be elaborated before
24038 the body of @code{Decls} so that this call does not cause an access before
24039 elaboration.
24040
24041 @item
24042 Within the body of @code{Util}, specifically within the body of
24043 @code{Util.Put_Val} there may be calls to any unit @code{with}'ed
24044 by this package.
24045
24046 @item
24047 One such @code{with}'ed package is package @code{Decls}, so there
24048 might be a call to a subprogram in @code{Decls} in @code{Put_Val}.
24049 In fact there is such a call in this example, but we would have to
24050 assume that there was such a call even if it were not there, since
24051 we are not supposed to write the body of @code{Decls} knowing what
24052 is in the body of @code{Utils}; certainly in the case of the
24053 static elaboration model, the compiler does not know what is in
24054 other bodies and must assume the worst.
24055
24056 @item
24057 This means that the spec and body of @code{Decls} must also be
24058 elaborated before we elaborate the unit containing the call, but
24059 that unit is @code{Decls}! This means that the body of @code{Decls}
24060 must be elaborated before itself, and that's a circularity.
24061 @end enumerate
24062
24063 @noindent
24064 Indeed, if you add an explicit pragma @code{Elaborate_All} for @code{Utils} in
24065 the body of @code{Decls} you will get a true Ada Reference Manual
24066 circularity that makes the program illegal.
24067
24068 In practice, we have found that problems with the static model of
24069 elaboration in existing code often arise from library tasks, so
24070 we must address this particular situation.
24071
24072 Note that if we compile and run the program above, using the dynamic model of
24073 elaboration (that is to say use the @option{-gnatE} switch),
24074 then it compiles, binds,
24075 links, and runs, printing the expected result of 2. Therefore in some sense
24076 the circularity here is only apparent, and we need to capture
24077 the properties of this program that  distinguish it from other library-level
24078 tasks that have real elaboration problems.
24079
24080 We have four possible answers to this question:
24081
24082 @itemize @bullet
24083
24084 @item
24085 Use the dynamic model of elaboration.
24086
24087 If we use the @option{-gnatE} switch, then as noted above, the program works.
24088 Why is this? If we examine the task body, it is apparent that the task cannot
24089 proceed past the
24090 @code{accept} statement until after elaboration has been completed, because
24091 the corresponding entry call comes from the main program, not earlier.
24092 This is why the dynamic model works here. But that's really giving
24093 up on a precise analysis, and we prefer to take this approach only if we cannot
24094 solve the
24095 problem in any other manner. So let us examine two ways to reorganize
24096 the program to avoid the potential elaboration problem.
24097
24098 @item
24099 Split library tasks into separate packages.
24100
24101 Write separate packages, so that library tasks are isolated from
24102 other declarations as much as possible. Let us look at a variation on
24103 the above program.
24104
24105 @smallexample @c ada
24106 package Decls1 is
24107   task Lib_Task is
24108      entry Start;
24109   end Lib_Task;
24110 end Decls1;
24111
24112 with Utils;
24113 package body Decls1 is
24114   task body Lib_Task is
24115   begin
24116      accept Start;
24117      Utils.Put_Val (2);
24118   end Lib_Task;
24119 end Decls1;
24120
24121 package Decls2 is
24122   type My_Int is new Integer;
24123   function Ident (M : My_Int) return My_Int;
24124 end Decls2;
24125
24126 with Utils;
24127 package body Decls2 is
24128   function Ident (M : My_Int) return My_Int is
24129   begin
24130      return M;
24131   end Ident;
24132 end Decls2;
24133
24134 with Decls2;
24135 package Utils is
24136   procedure Put_Val (Arg : Decls2.My_Int);
24137 end Utils;
24138
24139 with Text_IO;
24140 package body Utils is
24141   procedure Put_Val (Arg : Decls2.My_Int) is
24142   begin
24143      Text_IO.Put_Line (Decls2.My_Int'Image (Decls2.Ident (Arg)));
24144   end Put_Val;
24145 end Utils;
24146
24147 with Decls1;
24148 procedure Main is
24149 begin
24150    Decls1.Lib_Task.Start;
24151 end;
24152 @end smallexample
24153
24154 @noindent
24155 All we have done is to split @code{Decls} into two packages, one
24156 containing the library task, and one containing everything else. Now
24157 there is no cycle, and the program compiles, binds, links and executes
24158 using the default static model of elaboration.
24159
24160 @item
24161 Declare separate task types.
24162
24163 A significant part of the problem arises because of the use of the
24164 single task declaration form. This means that the elaboration of
24165 the task type, and the elaboration of the task itself (i.e. the
24166 creation of the task) happen at the same time. A good rule
24167 of style in Ada 95 is to always create explicit task types. By
24168 following the additional step of placing task objects in separate
24169 packages from the task type declaration, many elaboration problems
24170 are avoided. Here is another modified example of the example program:
24171
24172 @smallexample @c ada
24173 package Decls is
24174   task type Lib_Task_Type is
24175      entry Start;
24176   end Lib_Task_Type;
24177
24178   type My_Int is new Integer;
24179
24180   function Ident (M : My_Int) return My_Int;
24181 end Decls;
24182
24183 with Utils;
24184 package body Decls is
24185   task body Lib_Task_Type is
24186   begin
24187      accept Start;
24188      Utils.Put_Val (2);
24189   end Lib_Task_Type;
24190
24191   function Ident (M : My_Int) return My_Int is
24192   begin
24193      return M;
24194   end Ident;
24195 end Decls;
24196
24197 with Decls;
24198 package Utils is
24199   procedure Put_Val (Arg : Decls.My_Int);
24200 end Utils;
24201
24202 with Text_IO;
24203 package body Utils is
24204   procedure Put_Val (Arg : Decls.My_Int) is
24205   begin
24206      Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
24207   end Put_Val;
24208 end Utils;
24209
24210 with Decls;
24211 package Declst is
24212    Lib_Task : Decls.Lib_Task_Type;
24213 end Declst;
24214
24215 with Declst;
24216 procedure Main is
24217 begin
24218    Declst.Lib_Task.Start;
24219 end;
24220 @end smallexample
24221
24222 @noindent
24223 What we have done here is to replace the @code{task} declaration in
24224 package @code{Decls} with a @code{task type} declaration. Then we
24225 introduce a separate package @code{Declst} to contain the actual
24226 task object. This separates the elaboration issues for
24227 the @code{task type}
24228 declaration, which causes no trouble, from the elaboration issues
24229 of the task object, which is also unproblematic, since it is now independent
24230 of the elaboration of  @code{Utils}.
24231 This separation of concerns also corresponds to
24232 a generally sound engineering principle of separating declarations
24233 from instances. This version of the program also compiles, binds, links,
24234 and executes, generating the expected output.
24235
24236 @item
24237 Use No_Entry_Calls_In_Elaboration_Code restriction.
24238 @cindex No_Entry_Calls_In_Elaboration_Code
24239
24240 The previous two approaches described how a program can be restructured
24241 to avoid the special problems caused by library task bodies. in practice,
24242 however, such restructuring may be difficult to apply to existing legacy code,
24243 so we must consider solutions that do not require massive rewriting.
24244
24245 Let us consider more carefully why our original sample program works
24246 under the dynamic model of elaboration. The reason is that the code
24247 in the task body blocks immediately on the @code{accept}
24248 statement. Now of course there is nothing to prohibit elaboration
24249 code from making entry calls (for example from another library level task),
24250 so we cannot tell in isolation that
24251 the task will not execute the accept statement  during elaboration.
24252
24253 However, in practice it is very unusual to see elaboration code
24254 make any entry calls, and the pattern of tasks starting
24255 at elaboration time and then immediately blocking on @code{accept} or
24256 @code{select} statements is very common. What this means is that
24257 the compiler is being too pessimistic when it analyzes the
24258 whole package body as though it might be executed at elaboration
24259 time.
24260
24261 If we know that the elaboration code contains no entry calls, (a very safe
24262 assumption most of the time, that could almost be made the default
24263 behavior), then we can compile all units of the program under control
24264 of the following configuration pragma:
24265
24266 @smallexample
24267 pragma Restrictions (No_Entry_Calls_In_Elaboration_Code);
24268 @end smallexample
24269
24270 @noindent
24271 This pragma can be placed in the @file{gnat.adc} file in the usual
24272 manner. If we take our original unmodified program and compile it
24273 in the presence of a @file{gnat.adc} containing the above pragma,
24274 then once again, we can compile, bind, link, and execute, obtaining
24275 the expected result. In the presence of this pragma, the compiler does
24276 not trace calls in a task body, that appear after the first @code{accept}
24277 or @code{select} statement, and therefore does not report a potential
24278 circularity in the original program.
24279
24280 The compiler will check to the extent it can that the above
24281 restriction is not violated, but it is not always possible to do a
24282 complete check at compile time, so it is important to use this
24283 pragma only if the stated restriction is in fact met, that is to say
24284 no task receives an entry call before elaboration of all units is completed.
24285
24286 @end itemize
24287
24288 @node Mixing Elaboration Models
24289 @section Mixing Elaboration Models
24290 @noindent
24291 So far, we have assumed that the entire program is either compiled
24292 using the dynamic model or static model, ensuring consistency. It
24293 is possible to mix the two models, but rules have to be followed
24294 if this mixing is done to ensure that elaboration checks are not
24295 omitted.
24296
24297 The basic rule is that @emph{a unit compiled with the static model cannot
24298 be @code{with'ed} by a unit compiled with the dynamic model}. The
24299 reason for this is that in the static model, a unit assumes that
24300 its clients guarantee to use (the equivalent of) pragma
24301 @code{Elaborate_All} so that no elaboration checks are required
24302 in inner subprograms, and this assumption is violated if the
24303 client is compiled with dynamic checks.
24304
24305 The precise rule is as follows. A unit that is compiled with dynamic
24306 checks can only @code{with} a unit that meets at least one of the
24307 following criteria:
24308
24309 @itemize @bullet
24310
24311 @item
24312 The @code{with'ed} unit is itself compiled with dynamic elaboration
24313 checks (that is with the @option{-gnatE} switch.
24314
24315 @item
24316 The @code{with'ed} unit is an internal GNAT implementation unit from
24317 the System, Interfaces, Ada, or GNAT hierarchies.
24318
24319 @item
24320 The @code{with'ed} unit has pragma Preelaborate or pragma Pure.
24321
24322 @item
24323 The @code{with'ing} unit (that is the client) has an explicit pragma
24324 @code{Elaborate_All} for the @code{with'ed} unit.
24325
24326 @end itemize
24327
24328 @noindent
24329 If this rule is violated, that is if a unit with dynamic elaboration
24330 checks @code{with's} a unit that does not meet one of the above four
24331 criteria, then the binder (@code{gnatbind}) will issue a warning
24332 similar to that in the following example:
24333
24334 @smallexample
24335 warning: "x.ads" has dynamic elaboration checks and with's
24336 warning:   "y.ads" which has static elaboration checks
24337 @end smallexample
24338
24339 @noindent
24340 These warnings indicate that the rule has been violated, and that as a result
24341 elaboration checks may be missed in the resulting executable file.
24342 This warning may be suppressed using the @option{-ws} binder switch
24343 in the usual manner.
24344
24345 One useful application of this mixing rule is in the case of a subsystem
24346 which does not itself @code{with} units from the remainder of the
24347 application. In this case, the entire subsystem can be compiled with
24348 dynamic checks to resolve a circularity in the subsystem, while
24349 allowing the main application that uses this subsystem to be compiled
24350 using the more reliable default static model.
24351
24352 @node What to Do If the Default Elaboration Behavior Fails
24353 @section What to Do If the Default Elaboration Behavior Fails
24354
24355 @noindent
24356 If the binder cannot find an acceptable order, it outputs detailed
24357 diagnostics. For example:
24358 @smallexample
24359 @group
24360 @iftex
24361 @leftskip=0cm
24362 @end iftex
24363 error: elaboration circularity detected
24364 info:   "proc (body)" must be elaborated before "pack (body)"
24365 info:     reason: Elaborate_All probably needed in unit "pack (body)"
24366 info:     recompile "pack (body)" with -gnatwl
24367 info:                             for full details
24368 info:       "proc (body)"
24369 info:         is needed by its spec:
24370 info:       "proc (spec)"
24371 info:         which is withed by:
24372 info:       "pack (body)"
24373 info:  "pack (body)" must be elaborated before "proc (body)"
24374 info:     reason: pragma Elaborate in unit "proc (body)"
24375 @end group
24376
24377 @end smallexample
24378
24379 @noindent
24380 In this case we have a cycle that the binder cannot break. On the one
24381 hand, there is an explicit pragma Elaborate in @code{proc} for
24382 @code{pack}. This means that the body of @code{pack} must be elaborated
24383 before the body of @code{proc}. On the other hand, there is elaboration
24384 code in @code{pack} that calls a subprogram in @code{proc}. This means
24385 that for maximum safety, there should really be a pragma
24386 Elaborate_All in @code{pack} for @code{proc} which would require that
24387 the body of @code{proc} be elaborated before the body of
24388 @code{pack}. Clearly both requirements cannot be satisfied.
24389 Faced with a circularity of this kind, you have three different options.
24390
24391 @table @asis
24392 @item Fix the program
24393 The most desirable option from the point of view of long-term maintenance
24394 is to rearrange the program so that the elaboration problems are avoided.
24395 One useful technique is to place the elaboration code into separate
24396 child packages. Another is to move some of the initialization code to
24397 explicitly called subprograms, where the program controls the order
24398 of initialization explicitly. Although this is the most desirable option,
24399 it may be impractical and involve too much modification, especially in
24400 the case of complex legacy code.
24401
24402 @item Perform dynamic checks
24403 If the compilations are done using the
24404 @option{-gnatE}
24405 (dynamic elaboration check) switch, then GNAT behaves in a quite different
24406 manner. Dynamic checks are generated for all calls that could possibly result
24407 in raising an exception. With this switch, the compiler does not generate
24408 implicit @code{Elaborate} or @code{Elaborate_All} pragmas. The behavior then is
24409 exactly as specified in the Ada 95 Reference Manual. The binder will generate
24410 an executable program that may or may not raise @code{Program_Error}, and then
24411 it is the programmer's job to ensure that it does not raise an exception. Note
24412 that it is important to compile all units with the switch, it cannot be used
24413 selectively.
24414
24415 @item Suppress checks
24416 The drawback of dynamic checks is that they generate a
24417 significant overhead at run time, both in space and time. If you
24418 are absolutely sure that your program cannot raise any elaboration
24419 exceptions, and you still want to use the dynamic elaboration model,
24420 then you can use the configuration pragma
24421 @code{Suppress (Elaboration_Check)} to suppress all such checks. For
24422 example this pragma could be placed in the @file{gnat.adc} file.
24423
24424 @item Suppress checks selectively
24425 When you know that certain calls or instantiations in elaboration code cannot
24426 possibly lead to an elaboration error, and the binder nevertheless complains
24427 about implicit @code{Elaborate} and @code{Elaborate_All} pragmas that lead to
24428 elaboration circularities, it is possible to remove those warnings locally and
24429 obtain a program that will bind. Clearly this can be unsafe, and it is the
24430 responsibility of the programmer to make sure that the resulting program has no
24431 elaboration anomalies. The pragma @code{Suppress (Elaboration_Check)} can be
24432 used with different granularity to suppress warnings and break elaboration
24433 circularities:
24434
24435 @itemize @bullet
24436 @item
24437 Place the pragma that names the called subprogram in the declarative part
24438 that contains the call.
24439
24440 @item
24441 Place the pragma in the declarative part, without naming an entity. This
24442 disables warnings on all calls in the corresponding  declarative region.
24443
24444 @item
24445 Place the pragma in the package spec that declares the called subprogram,
24446 and name the subprogram. This disables warnings on all elaboration calls to
24447 that subprogram.
24448
24449 @item
24450 Place the pragma in the package spec that declares the called subprogram,
24451 without naming any entity. This disables warnings on all elaboration calls to
24452 all subprograms declared in this spec.
24453
24454 @item Use Pragma Elaborate
24455 As previously described in section @xref{Treatment of Pragma Elaborate},
24456 GNAT in static mode assumes that a @code{pragma} Elaborate indicates correctly
24457 that no elaboration checks are required on calls to the designated unit.
24458 There may be cases in which the caller knows that no transitive calls
24459 can occur, so that a @code{pragma Elaborate} will be sufficient in a
24460 case where @code{pragma Elaborate_All} would cause a circularity.
24461 @end itemize
24462
24463 @noindent
24464 These five cases are listed in order of decreasing safety, and therefore
24465 require increasing programmer care in their application. Consider the
24466 following program:
24467
24468 @smallexample @c adanocomment
24469 package Pack1 is
24470   function F1 return Integer;
24471   X1 : Integer;
24472 end Pack1;
24473
24474 package Pack2 is
24475   function F2 return Integer;
24476   function Pure (x : integer) return integer;
24477   --  pragma Suppress (Elaboration_Check, On => Pure);  -- (3)
24478   --  pragma Suppress (Elaboration_Check);              -- (4)
24479 end Pack2;
24480
24481 with Pack2;
24482 package body Pack1 is
24483   function F1 return Integer is
24484   begin
24485     return 100;
24486   end F1;
24487   Val : integer := Pack2.Pure (11);    --  Elab. call (1)
24488 begin
24489   declare
24490     --  pragma Suppress(Elaboration_Check, Pack2.F2);   -- (1)
24491     --  pragma Suppress(Elaboration_Check);             -- (2)
24492   begin
24493     X1 := Pack2.F2 + 1;                --  Elab. call (2)
24494   end;
24495 end Pack1;
24496
24497 with Pack1;
24498 package body Pack2 is
24499   function F2 return Integer is
24500   begin
24501      return Pack1.F1;
24502   end F2;
24503   function Pure (x : integer) return integer is
24504   begin
24505      return x ** 3 - 3 * x;
24506   end;
24507 end Pack2;
24508
24509 with Pack1, Ada.Text_IO;
24510 procedure Proc3 is
24511 begin
24512   Ada.Text_IO.Put_Line(Pack1.X1'Img); -- 101
24513 end Proc3;
24514 @end smallexample
24515 In the absence of any pragmas, an attempt to bind this program produces
24516 the following diagnostics:
24517 @smallexample
24518 @group
24519 @iftex
24520 @leftskip=.5cm
24521 @end iftex
24522 error: elaboration circularity detected
24523 info:    "pack1 (body)" must be elaborated before "pack1 (body)"
24524 info:       reason: Elaborate_All probably needed in unit "pack1 (body)"
24525 info:       recompile "pack1 (body)" with -gnatwl for full details
24526 info:          "pack1 (body)"
24527 info:             must be elaborated along with its spec:
24528 info:          "pack1 (spec)"
24529 info:             which is withed by:
24530 info:          "pack2 (body)"
24531 info:             which must be elaborated along with its spec:
24532 info:          "pack2 (spec)"
24533 info:             which is withed by:
24534 info:          "pack1 (body)"
24535 @end group
24536 @end smallexample
24537 The sources of the circularity are the two calls to @code{Pack2.Pure} and
24538 @code{Pack2.F2} in the body of @code{Pack1}. We can see that the call to
24539 F2 is safe, even though F2 calls F1, because the call appears after the
24540 elaboration of the body of F1. Therefore the pragma (1) is safe, and will
24541 remove the warning on the call. It is also possible to use pragma (2)
24542 because there are no other potentially unsafe calls in the block.
24543
24544 @noindent
24545 The call to @code{Pure} is safe because this function does not depend on the
24546 state of @code{Pack2}. Therefore any call to this function is safe, and it
24547 is correct to place pragma (3) in the corresponding package spec.
24548
24549 @noindent
24550 Finally, we could place pragma (4) in the spec of @code{Pack2} to disable
24551 warnings on all calls to functions declared therein. Note that this is not
24552 necessarily safe, and requires more detailed examination of the subprogram
24553 bodies involved. In particular, a call to @code{F2} requires that @code{F1}
24554 be already elaborated.
24555 @end table
24556
24557 @noindent
24558 It is hard to generalize on which of these four approaches should be
24559 taken. Obviously if it is possible to fix the program so that the default
24560 treatment works, this is preferable, but this may not always be practical.
24561 It is certainly simple enough to use
24562 @option{-gnatE}
24563 but the danger in this case is that, even if the GNAT binder
24564 finds a correct elaboration order, it may not always do so,
24565 and certainly a binder from another Ada compiler might not. A
24566 combination of testing and analysis (for which the warnings generated
24567 with the
24568 @option{-gnatwl}
24569 switch can be useful) must be used to ensure that the program is free
24570 of errors. One switch that is useful in this testing is the
24571 @option{^-p (pessimistic elaboration order)^/PESSIMISTIC_ELABORATION_ORDER^}
24572 switch for
24573 @code{gnatbind}.
24574 Normally the binder tries to find an order that has the best chance of
24575 of avoiding elaboration problems. With this switch, the binder
24576 plays a devil's advocate role, and tries to choose the order that
24577 has the best chance of failing. If your program works even with this
24578 switch, then it has a better chance of being error free, but this is still
24579 not a guarantee.
24580
24581 For an example of this approach in action, consider the C-tests (executable
24582 tests) from the ACVC suite. If these are compiled and run with the default
24583 treatment, then all but one of them succeed without generating any error
24584 diagnostics from the binder. However, there is one test that fails, and
24585 this is not surprising, because the whole point of this test is to ensure
24586 that the compiler can handle cases where it is impossible to determine
24587 a correct order statically, and it checks that an exception is indeed
24588 raised at run time.
24589
24590 This one test must be compiled and run using the
24591 @option{-gnatE}
24592 switch, and then it passes. Alternatively, the entire suite can
24593 be run using this switch. It is never wrong to run with the dynamic
24594 elaboration switch if your code is correct, and we assume that the
24595 C-tests are indeed correct (it is less efficient, but efficiency is
24596 not a factor in running the ACVC tests.)
24597
24598 @node Elaboration for Access-to-Subprogram Values
24599 @section Elaboration for Access-to-Subprogram Values
24600 @cindex Access-to-subprogram
24601
24602 @noindent
24603 The introduction of access-to-subprogram types in Ada 95 complicates
24604 the handling of elaboration. The trouble is that it becomes
24605 impossible to tell at compile time which procedure
24606 is being called. This means that it is not possible for the binder
24607 to analyze the elaboration requirements in this case.
24608
24609 If at the point at which the access value is created
24610 (i.e., the evaluation of @code{P'Access} for a subprogram @code{P}),
24611 the body of the subprogram is
24612 known to have been elaborated, then the access value is safe, and its use
24613 does not require a check. This may be achieved by appropriate arrangement
24614 of the order of declarations if the subprogram is in the current unit,
24615 or, if the subprogram is in another unit, by using pragma
24616 @code{Pure}, @code{Preelaborate}, or @code{Elaborate_Body}
24617 on the referenced unit.
24618
24619 If the referenced body is not known to have been elaborated at the point
24620 the access value is created, then any use of the access value must do a
24621 dynamic check, and this dynamic check will fail and raise a
24622 @code{Program_Error} exception if the body has not been elaborated yet.
24623 GNAT will generate the necessary checks, and in addition, if the
24624 @option{-gnatwl}
24625 switch is set, will generate warnings that such checks are required.
24626
24627 The use of dynamic dispatching for tagged types similarly generates
24628 a requirement for dynamic checks, and premature calls to any primitive
24629 operation of a tagged type before the body of the operation has been
24630 elaborated, will result in the raising of @code{Program_Error}.
24631
24632 @node Summary of Procedures for Elaboration Control
24633 @section Summary of Procedures for Elaboration Control
24634 @cindex Elaboration control
24635
24636 @noindent
24637 First, compile your program with the default options, using none of
24638 the special elaboration control switches. If the binder successfully
24639 binds your program, then you can be confident that, apart from issues
24640 raised by the use of access-to-subprogram types and dynamic dispatching,
24641 the program is free of elaboration errors. If it is important that the
24642 program be portable, then use the
24643 @option{-gnatwl}
24644 switch to generate warnings about missing @code{Elaborate} or
24645 @code{Elaborate_All} pragmas, and supply the missing pragmas.
24646
24647 If the program fails to bind using the default static elaboration
24648 handling, then you can fix the program to eliminate the binder
24649 message, or recompile the entire program with the
24650 @option{-gnatE} switch to generate dynamic elaboration checks,
24651 and, if you are sure there really are no elaboration problems,
24652 use a global pragma @code{Suppress (Elaboration_Check)}.
24653
24654 @node Other Elaboration Order Considerations
24655 @section Other Elaboration Order Considerations
24656 @noindent
24657 This section has been entirely concerned with the issue of finding a valid
24658 elaboration order, as defined by the Ada Reference Manual. In a case
24659 where several elaboration orders are valid, the task is to find one
24660 of the possible valid elaboration orders (and the static model in GNAT
24661 will ensure that this is achieved).
24662
24663 The purpose of the elaboration rules in the Ada Reference Manual is to
24664 make sure that no entity is accessed before it has been elaborated. For
24665 a subprogram, this means that the spec and body must have been elaborated
24666 before the subprogram is called. For an object, this means that the object
24667 must have been elaborated before its value is read or written. A violation
24668 of either of these two requirements is an access before elaboration order,
24669 and this section has been all about avoiding such errors.
24670
24671 In the case where more than one order of elaboration is possible, in the
24672 sense that access before elaboration errors are avoided, then any one of
24673 the orders is ``correct'' in the sense that it meets the requirements of
24674 the Ada Reference Manual, and no such error occurs.
24675
24676 However, it may be the case for a given program, that there are
24677 constraints on the order of elaboration that come not from consideration
24678 of avoiding elaboration errors, but rather from extra-lingual logic
24679 requirements. Consider this example:
24680
24681 @smallexample @c ada
24682 with Init_Constants;
24683 package Constants is
24684    X : Integer := 0;
24685    Y : Integer := 0;
24686 end Constants;
24687
24688 package Init_Constants is
24689    procedure P; -- require a body
24690 end Init_Constants;
24691
24692 with Constants;
24693 package body Init_Constants is
24694    procedure P is begin null; end;
24695 begin
24696    Constants.X := 3;
24697    Constants.Y := 4;
24698 end Init_Constants;
24699
24700 with Constants;
24701 package Calc is
24702    Z : Integer := Constants.X + Constants.Y;
24703 end Calc;
24704
24705 with Calc;
24706 with Text_IO; use Text_IO;
24707 procedure Main is
24708 begin
24709    Put_Line (Calc.Z'Img);
24710 end Main;
24711 @end smallexample
24712
24713 @noindent
24714 In this example, there is more than one valid order of elaboration. For
24715 example both the following are correct orders:
24716
24717 @smallexample
24718 Init_Constants spec
24719 Constants spec
24720 Calc spec
24721 Init_Constants body
24722 Main body
24723
24724   and
24725
24726 Init_Constants spec
24727 Init_Constants body
24728 Constants spec
24729 Calc spec
24730 Main body
24731 @end smallexample
24732
24733 @noindent
24734 There is no language rule to prefer one or the other, both are correct
24735 from an order of elaboration point of view. But the programmatic effects
24736 of the two orders are very different. In the first, the elaboration routine
24737 of @code{Calc} initializes @code{Z} to zero, and then the main program
24738 runs with this value of zero. But in the second order, the elaboration
24739 routine of @code{Calc} runs after the body of Init_Constants has set
24740 @code{X} and @code{Y} and thus @code{Z} is set to 7 before @code{Main}
24741 runs.
24742
24743 One could perhaps by applying pretty clever non-artificial intelligence
24744 to the situation guess that it is more likely that the second order of
24745 elaboration is the one desired, but there is no formal linguistic reason
24746 to prefer one over the other. In fact in this particular case, GNAT will
24747 prefer the second order, because of the rule that bodies are elaborated
24748 as soon as possible, but it's just luck that this is what was wanted
24749 (if indeed the second order was preferred).
24750
24751 If the program cares about the order of elaboration routines in a case like
24752 this, it is important to specify the order required. In this particular
24753 case, that could have been achieved by adding to the spec of Calc:
24754
24755 @smallexample @c ada
24756 pragma Elaborate_All (Constants);
24757 @end smallexample
24758
24759 @noindent
24760 which requires that the body (if any) and spec of @code{Constants},
24761 as well as the body and spec of any unit @code{with}'ed by
24762 @code{Constants} be elaborated before @code{Calc} is elaborated.
24763
24764 Clearly no automatic method can always guess which alternative you require,
24765 and if you are working with legacy code that had constraints of this kind
24766 which were not properly specified by adding @code{Elaborate} or
24767 @code{Elaborate_All} pragmas, then indeed it is possible that two different
24768 compilers can choose different orders.
24769
24770 The @code{gnatbind}
24771 @option{^-p^/PESSIMISTIC_ELABORATION^} switch may be useful in smoking
24772 out problems. This switch causes bodies to be elaborated as late as possible
24773 instead of as early as possible. In the example above, it would have forced
24774 the choice of the first elaboration order. If you get different results
24775 when using this switch, and particularly if one set of results is right,
24776 and one is wrong as far as you are concerned, it shows that you have some
24777 missing @code{Elaborate} pragmas. For the example above, we have the
24778 following output:
24779
24780 @smallexample
24781 gnatmake -f -q main
24782 main
24783  7
24784 gnatmake -f -q main -bargs -p
24785 main
24786  0
24787 @end smallexample
24788
24789 @noindent
24790 It is of course quite unlikely that both these results are correct, so
24791 it is up to you in a case like this to investigate the source of the
24792 difference, by looking at the two elaboration orders that are chosen,
24793 and figuring out which is correct, and then adding the necessary
24794 @code{Elaborate} or @code{Elaborate_All} pragmas to ensure the desired order.
24795
24796 @node Inline Assembler
24797 @appendix Inline Assembler
24798
24799 @noindent
24800 If you need to write low-level software that interacts directly
24801 with the hardware, Ada provides two ways to incorporate assembly
24802 language code into your program.  First, you can import and invoke
24803 external routines written in assembly language, an Ada feature fully
24804 supported by GNAT.  However, for small sections of code it may be simpler
24805 or more efficient to include assembly language statements directly
24806 in your Ada source program, using the facilities of the implementation-defined
24807 package @code{System.Machine_Code}, which incorporates the gcc
24808 Inline Assembler.  The Inline Assembler approach offers a number of advantages,
24809 including the following:
24810
24811 @itemize @bullet
24812 @item No need to use non-Ada tools
24813 @item Consistent interface over different targets
24814 @item Automatic usage of the proper calling conventions
24815 @item Access to Ada constants and variables
24816 @item Definition of intrinsic routines
24817 @item Possibility of inlining a subprogram comprising assembler code
24818 @item Code optimizer can take Inline Assembler code into account
24819 @end itemize
24820
24821 This chapter presents a series of examples to show you how to use
24822 the Inline Assembler.  Although it focuses on the Intel x86,
24823 the general approach applies also to other processors.
24824 It is assumed that you are familiar with Ada
24825 and with assembly language programming.
24826
24827 @menu
24828 * Basic Assembler Syntax::
24829 * A Simple Example of Inline Assembler::
24830 * Output Variables in Inline Assembler::
24831 * Input Variables in Inline Assembler::
24832 * Inlining Inline Assembler Code::
24833 * Other Asm Functionality::
24834 @end menu
24835
24836 @c ---------------------------------------------------------------------------
24837 @node Basic Assembler Syntax
24838 @section Basic Assembler Syntax
24839
24840 @noindent
24841 The assembler used by GNAT and gcc is based not on the Intel assembly
24842 language, but rather on a language that descends from the AT&T Unix
24843 assembler @emph{as} (and which is often referred to as ``AT&T syntax'').
24844 The following table summarizes the main features of @emph{as} syntax
24845 and points out the differences from the Intel conventions.
24846 See the gcc @emph{as} and @emph{gas} (an @emph{as} macro
24847 pre-processor) documentation for further information.
24848
24849 @table @asis
24850 @item Register names
24851 gcc / @emph{as}: Prefix with ``%''; for example @code{%eax}
24852 @*
24853 Intel: No extra punctuation; for example @code{eax}
24854
24855 @item Immediate operand
24856 gcc / @emph{as}: Prefix with ``$''; for example @code{$4}
24857 @*
24858 Intel: No extra punctuation; for example @code{4}
24859
24860 @item Address
24861 gcc / @emph{as}: Prefix with ``$''; for example @code{$loc}
24862 @*
24863 Intel: No extra punctuation; for example @code{loc}
24864
24865 @item Memory contents
24866 gcc / @emph{as}: No extra punctuation; for example @code{loc}
24867 @*
24868 Intel: Square brackets; for example @code{[loc]}
24869
24870 @item Register contents
24871 gcc / @emph{as}: Parentheses; for example @code{(%eax)}
24872 @*
24873 Intel: Square brackets; for example @code{[eax]}
24874
24875 @item Hexadecimal numbers
24876 gcc / @emph{as}: Leading ``0x'' (C language syntax); for example @code{0xA0}
24877 @*
24878 Intel: Trailing ``h''; for example @code{A0h}
24879
24880 @item Operand size
24881 gcc / @emph{as}: Explicit in op code; for example @code{movw} to move
24882 a 16-bit word
24883 @*
24884 Intel: Implicit, deduced by assembler; for example @code{mov}
24885
24886 @item Instruction repetition
24887 gcc / @emph{as}: Split into two lines; for example
24888 @*
24889 @code{rep}
24890 @*
24891 @code{stosl}
24892 @*
24893 Intel: Keep on one line; for example @code{rep stosl}
24894
24895 @item Order of operands
24896 gcc / @emph{as}: Source first; for example @code{movw $4, %eax}
24897 @*
24898 Intel: Destination first; for example @code{mov eax, 4}
24899 @end table
24900
24901 @c ---------------------------------------------------------------------------
24902 @node A Simple Example of Inline Assembler
24903 @section A Simple Example of Inline Assembler
24904
24905 @noindent
24906 The following example will generate a single assembly language statement,
24907 @code{nop}, which does nothing.  Despite its lack of run-time effect,
24908 the example will be useful in illustrating the basics of
24909 the Inline Assembler facility.
24910
24911 @smallexample @c ada
24912 @group
24913 with System.Machine_Code; use System.Machine_Code;
24914 procedure Nothing is
24915 begin
24916    Asm ("nop");
24917 end Nothing;
24918 @end group
24919 @end smallexample
24920
24921 @code{Asm} is a procedure declared in package @code{System.Machine_Code};
24922 here it takes one parameter, a @emph{template string} that must be a static
24923 expression and that will form the generated instruction.
24924 @code{Asm} may be regarded as a compile-time procedure that parses
24925 the template string and additional parameters (none here),
24926 from which it generates a sequence of assembly language instructions.
24927
24928 The examples in this chapter will illustrate several of the forms
24929 for invoking @code{Asm}; a complete specification of the syntax
24930 is found in the @cite{GNAT Reference Manual}.
24931
24932 Under the standard GNAT conventions, the @code{Nothing} procedure
24933 should be in a file named @file{nothing.adb}.
24934 You can build the executable in the usual way:
24935 @smallexample
24936 gnatmake nothing
24937 @end smallexample
24938 However, the interesting aspect of this example is not its run-time behavior
24939 but rather the generated assembly code.
24940 To see this output, invoke the compiler as follows:
24941 @smallexample
24942    gcc -c -S -fomit-frame-pointer -gnatp @file{nothing.adb}
24943 @end smallexample
24944 where the options are:
24945
24946 @table @code
24947 @item -c
24948 compile only (no bind or link)
24949 @item -S
24950 generate assembler listing
24951 @item -fomit-frame-pointer
24952 do not set up separate stack frames
24953 @item -gnatp
24954 do not add runtime checks
24955 @end table
24956
24957 This gives a human-readable assembler version of the code. The resulting
24958 file will have the same name as the Ada source file, but with a @code{.s}
24959 extension. In our example, the file @file{nothing.s} has the following
24960 contents:
24961
24962 @smallexample
24963 @group
24964 .file "nothing.adb"
24965 gcc2_compiled.:
24966 ___gnu_compiled_ada:
24967 .text
24968    .align 4
24969 .globl __ada_nothing
24970 __ada_nothing:
24971 #APP
24972    nop
24973 #NO_APP
24974    jmp L1
24975    .align 2,0x90
24976 L1:
24977    ret
24978 @end group
24979 @end smallexample
24980
24981 The assembly code you included is clearly indicated by
24982 the compiler, between the @code{#APP} and @code{#NO_APP}
24983 delimiters. The character before the 'APP' and 'NOAPP'
24984 can differ on different targets. For example, GNU/Linux uses '#APP' while
24985 on NT you will see '/APP'.
24986
24987 If you make a mistake in your assembler code (such as using the
24988 wrong size modifier, or using a wrong operand for the instruction) GNAT
24989 will report this error in a temporary file, which will be deleted when
24990 the compilation is finished.  Generating an assembler file will help
24991 in such cases, since you can assemble this file separately using the
24992 @emph{as} assembler that comes with gcc.
24993
24994 Assembling the file using the command
24995
24996 @smallexample
24997 as @file{nothing.s}
24998 @end smallexample
24999 @noindent
25000 will give you error messages whose lines correspond to the assembler
25001 input file, so you can easily find and correct any mistakes you made.
25002 If there are no errors, @emph{as} will generate an object file
25003 @file{nothing.out}.
25004
25005 @c ---------------------------------------------------------------------------
25006 @node Output Variables in Inline Assembler
25007 @section Output Variables in Inline Assembler
25008
25009 @noindent
25010 The examples in this section, showing how to access the processor flags,
25011 illustrate how to specify the destination operands for assembly language
25012 statements.
25013
25014 @smallexample @c ada
25015 @group
25016 with Interfaces; use Interfaces;
25017 with Ada.Text_IO; use Ada.Text_IO;
25018 with System.Machine_Code; use System.Machine_Code;
25019 procedure Get_Flags is
25020    Flags : Unsigned_32;
25021    use ASCII;
25022 begin
25023    Asm ("pushfl"          & LF & HT & -- push flags on stack
25024         "popl %%eax"      & LF & HT & -- load eax with flags
25025         "movl %%eax, %0",             -- store flags in variable
25026         Outputs => Unsigned_32'Asm_Output ("=g", Flags));
25027    Put_Line ("Flags register:" & Flags'Img);
25028 end Get_Flags;
25029 @end group
25030 @end smallexample
25031
25032 In order to have a nicely aligned assembly listing, we have separated
25033 multiple assembler statements in the Asm template string with linefeed
25034 (ASCII.LF) and horizontal tab (ASCII.HT) characters.
25035 The resulting section of the assembly output file is:
25036
25037 @smallexample
25038 @group
25039 #APP
25040    pushfl
25041    popl %eax
25042    movl %eax, -40(%ebp)
25043 #NO_APP
25044 @end group
25045 @end smallexample
25046
25047 It would have been legal to write the Asm invocation as:
25048
25049 @smallexample
25050 Asm ("pushfl popl %%eax movl %%eax, %0")
25051 @end smallexample
25052
25053 but in the generated assembler file, this would come out as:
25054
25055 @smallexample
25056 #APP
25057    pushfl popl %eax movl %eax, -40(%ebp)
25058 #NO_APP
25059 @end smallexample
25060
25061 which is not so convenient for the human reader.
25062
25063 We use Ada comments
25064 at the end of each line to explain what the assembler instructions
25065 actually do.  This is a useful convention.
25066
25067 When writing Inline Assembler instructions, you need to precede each register
25068 and variable name with a percent sign.  Since the assembler already requires
25069 a percent sign at the beginning of a register name, you need two consecutive
25070 percent signs for such names in the Asm template string, thus @code{%%eax}.
25071 In the generated assembly code, one of the percent signs will be stripped off.
25072
25073 Names such as @code{%0}, @code{%1}, @code{%2}, etc., denote input or output
25074 variables: operands you later define using @code{Input} or @code{Output}
25075 parameters to @code{Asm}.
25076 An output variable is illustrated in
25077 the third statement in the Asm template string:
25078 @smallexample
25079 movl %%eax, %0
25080 @end smallexample
25081 The intent is to store the contents of the eax register in a variable that can
25082 be accessed in Ada.  Simply writing @code{movl %%eax, Flags} would not
25083 necessarily work, since the compiler might optimize by using a register
25084 to hold Flags, and the expansion of the @code{movl} instruction would not be
25085 aware of this optimization.  The solution is not to store the result directly
25086 but rather to advise the compiler to choose the correct operand form;
25087 that is the purpose of the @code{%0} output variable.
25088
25089 Information about the output variable is supplied in the @code{Outputs}
25090 parameter to @code{Asm}:
25091 @smallexample
25092 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
25093 @end smallexample
25094
25095 The output is defined by the @code{Asm_Output} attribute of the target type;
25096 the general format is
25097 @smallexample
25098 Type'Asm_Output (constraint_string, variable_name)
25099 @end smallexample
25100
25101 The constraint string directs the compiler how
25102 to store/access the associated variable.  In the example
25103 @smallexample
25104 Unsigned_32'Asm_Output ("=m", Flags);
25105 @end smallexample
25106 the @code{"m"} (memory) constraint tells the compiler that the variable
25107 @code{Flags} should be stored in a memory variable, thus preventing
25108 the optimizer from keeping it in a register.  In contrast,
25109 @smallexample
25110 Unsigned_32'Asm_Output ("=r", Flags);
25111 @end smallexample
25112 uses the @code{"r"} (register) constraint, telling the compiler to
25113 store the variable in a register.
25114
25115 If the constraint is preceded by the equal character (@strong{=}), it tells
25116 the compiler that the variable will be used to store data into it.
25117
25118 In the @code{Get_Flags} example, we used the @code{"g"} (global) constraint,
25119 allowing the optimizer to choose whatever it deems best.
25120
25121 There are a fairly large number of constraints, but the ones that are
25122 most useful (for the Intel x86 processor) are the following:
25123
25124 @table @code
25125 @item =
25126 output constraint
25127 @item g
25128 global (i.e. can be stored anywhere)
25129 @item m
25130 in memory
25131 @item I
25132 a constant
25133 @item a
25134 use eax
25135 @item b
25136 use ebx
25137 @item c
25138 use ecx
25139 @item d
25140 use edx
25141 @item S
25142 use esi
25143 @item D
25144 use edi
25145 @item r
25146 use one of eax, ebx, ecx or edx
25147 @item q
25148 use one of eax, ebx, ecx, edx, esi or edi
25149 @end table
25150
25151 The full set of constraints is described in the gcc and @emph{as}
25152 documentation; note that it is possible to combine certain constraints
25153 in one constraint string.
25154
25155 You specify the association of an output variable with an assembler operand
25156 through the @code{%}@emph{n} notation, where @emph{n} is a non-negative
25157 integer.  Thus in
25158 @smallexample @c ada
25159 @group
25160 Asm ("pushfl"          & LF & HT & -- push flags on stack
25161      "popl %%eax"      & LF & HT & -- load eax with flags
25162      "movl %%eax, %0",             -- store flags in variable
25163      Outputs => Unsigned_32'Asm_Output ("=g", Flags));
25164 @end group
25165 @end smallexample
25166 @noindent
25167 @code{%0} will be replaced in the expanded code by the appropriate operand,
25168 whatever
25169 the compiler decided for the @code{Flags} variable.
25170
25171 In general, you may have any number of output variables:
25172 @itemize @bullet
25173 @item
25174 Count the operands starting at 0; thus @code{%0}, @code{%1}, etc.
25175 @item
25176 Specify the @code{Outputs} parameter as a parenthesized comma-separated list
25177 of @code{Asm_Output} attributes
25178 @end itemize
25179
25180 For example:
25181 @smallexample @c ada
25182 @group
25183 Asm ("movl %%eax, %0" & LF & HT &
25184      "movl %%ebx, %1" & LF & HT &
25185      "movl %%ecx, %2",
25186      Outputs => (Unsigned_32'Asm_Output ("=g", Var_A),   --  %0 = Var_A
25187                  Unsigned_32'Asm_Output ("=g", Var_B),   --  %1 = Var_B
25188                  Unsigned_32'Asm_Output ("=g", Var_C))); --  %2 = Var_C
25189 @end group
25190 @end smallexample
25191 @noindent
25192 where @code{Var_A}, @code{Var_B}, and @code{Var_C} are variables
25193 in the Ada program.
25194
25195 As a variation on the @code{Get_Flags} example, we can use the constraints
25196 string to direct the compiler to store the eax register into the @code{Flags}
25197 variable, instead of including the store instruction explicitly in the
25198 @code{Asm} template string:
25199
25200 @smallexample @c ada
25201 @group
25202 with Interfaces; use Interfaces;
25203 with Ada.Text_IO; use Ada.Text_IO;
25204 with System.Machine_Code; use System.Machine_Code;
25205 procedure Get_Flags_2 is
25206    Flags : Unsigned_32;
25207    use ASCII;
25208 begin
25209    Asm ("pushfl"      & LF & HT & -- push flags on stack
25210         "popl %%eax",             -- save flags in eax
25211         Outputs => Unsigned_32'Asm_Output ("=a", Flags));
25212    Put_Line ("Flags register:" & Flags'Img);
25213 end Get_Flags_2;
25214 @end group
25215 @end smallexample
25216
25217 @noindent
25218 The @code{"a"} constraint tells the compiler that the @code{Flags}
25219 variable will come from the eax register. Here is the resulting code:
25220
25221 @smallexample
25222 @group
25223 #APP
25224    pushfl
25225    popl %eax
25226 #NO_APP
25227    movl %eax,-40(%ebp)
25228 @end group
25229 @end smallexample
25230
25231 @noindent
25232 The compiler generated the store of eax into Flags after
25233 expanding the assembler code.
25234
25235 Actually, there was no need to pop the flags into the eax register;
25236 more simply, we could just pop the flags directly into the program variable:
25237
25238 @smallexample @c ada
25239 @group
25240 with Interfaces; use Interfaces;
25241 with Ada.Text_IO; use Ada.Text_IO;
25242 with System.Machine_Code; use System.Machine_Code;
25243 procedure Get_Flags_3 is
25244    Flags : Unsigned_32;
25245    use ASCII;
25246 begin
25247    Asm ("pushfl"  & LF & HT & -- push flags on stack
25248         "pop %0",             -- save flags in Flags
25249         Outputs => Unsigned_32'Asm_Output ("=g", Flags));
25250    Put_Line ("Flags register:" & Flags'Img);
25251 end Get_Flags_3;
25252 @end group
25253 @end smallexample
25254
25255 @c ---------------------------------------------------------------------------
25256 @node Input Variables in Inline Assembler
25257 @section Input Variables in Inline Assembler
25258
25259 @noindent
25260 The example in this section illustrates how to specify the source operands
25261 for assembly language statements.
25262 The program simply increments its input value by 1:
25263
25264 @smallexample @c ada
25265 @group
25266 with Interfaces; use Interfaces;
25267 with Ada.Text_IO; use Ada.Text_IO;
25268 with System.Machine_Code; use System.Machine_Code;
25269 procedure Increment is
25270
25271    function Incr (Value : Unsigned_32) return Unsigned_32 is
25272       Result : Unsigned_32;
25273    begin
25274       Asm ("incl %0",
25275            Inputs  => Unsigned_32'Asm_Input ("a", Value),
25276            Outputs => Unsigned_32'Asm_Output ("=a", Result));
25277       return Result;
25278    end Incr;
25279
25280    Value : Unsigned_32;
25281
25282 begin
25283    Value := 5;
25284    Put_Line ("Value before is" & Value'Img);
25285    Value := Incr (Value);
25286    Put_Line ("Value after is" & Value'Img);
25287 end Increment;
25288 @end group
25289 @end smallexample
25290
25291 The @code{Outputs} parameter to @code{Asm} specifies
25292 that the result will be in the eax register and that it is to be stored
25293 in the @code{Result} variable.
25294
25295 The @code{Inputs} parameter looks much like the @code{Outputs} parameter,
25296 but with an @code{Asm_Input} attribute.
25297 The @code{"="} constraint, indicating an output value, is not present.
25298
25299 You can have multiple input variables, in the same way that you can have more
25300 than one output variable.
25301
25302 The parameter count (%0, %1) etc, now starts at the first input
25303 statement, and continues with the output statements.
25304 When both parameters use the same variable, the
25305 compiler will treat them as the same %n operand, which is the case here.
25306
25307 Just as the @code{Outputs} parameter causes the register to be stored into the
25308 target variable after execution of the assembler statements, so does the
25309 @code{Inputs} parameter cause its variable to be loaded into the register
25310 before execution of the assembler statements.
25311
25312 Thus the effect of the @code{Asm} invocation is:
25313 @enumerate
25314 @item load the 32-bit value of @code{Value} into eax
25315 @item execute the @code{incl %eax} instruction
25316 @item store the contents of eax into the @code{Result} variable
25317 @end enumerate
25318
25319 The resulting assembler file (with @option{-O2} optimization) contains:
25320 @smallexample
25321 @group
25322 _increment__incr.1:
25323    subl $4,%esp
25324    movl 8(%esp),%eax
25325 #APP
25326    incl %eax
25327 #NO_APP
25328    movl %eax,%edx
25329    movl %ecx,(%esp)
25330    addl $4,%esp
25331    ret
25332 @end group
25333 @end smallexample
25334
25335 @c ---------------------------------------------------------------------------
25336 @node Inlining Inline Assembler Code
25337 @section Inlining Inline Assembler Code
25338
25339 @noindent
25340 For a short subprogram such as the @code{Incr} function in the previous
25341 section, the overhead of the call and return (creating / deleting the stack
25342 frame) can be significant, compared to the amount of code in the subprogram
25343 body.  A solution is to apply Ada's @code{Inline} pragma to the subprogram,
25344 which directs the compiler to expand invocations of the subprogram at the
25345 point(s) of call, instead of setting up a stack frame for out-of-line calls.
25346 Here is the resulting program:
25347
25348 @smallexample @c ada
25349 @group
25350 with Interfaces; use Interfaces;
25351 with Ada.Text_IO; use Ada.Text_IO;
25352 with System.Machine_Code; use System.Machine_Code;
25353 procedure Increment_2 is
25354
25355    function Incr (Value : Unsigned_32) return Unsigned_32 is
25356       Result : Unsigned_32;
25357    begin
25358       Asm ("incl %0",
25359            Inputs  => Unsigned_32'Asm_Input ("a", Value),
25360            Outputs => Unsigned_32'Asm_Output ("=a", Result));
25361       return Result;
25362    end Incr;
25363    pragma Inline (Increment);
25364
25365    Value : Unsigned_32;
25366
25367 begin
25368    Value := 5;
25369    Put_Line ("Value before is" & Value'Img);
25370    Value := Increment (Value);
25371    Put_Line ("Value after is" & Value'Img);
25372 end Increment_2;
25373 @end group
25374 @end smallexample
25375
25376 Compile the program with both optimization (@option{-O2}) and inlining
25377 enabled (@option{-gnatpn} instead of @option{-gnatp}).
25378
25379 The @code{Incr} function is still compiled as usual, but at the
25380 point in @code{Increment} where our function used to be called:
25381
25382 @smallexample
25383 @group
25384 pushl %edi
25385 call _increment__incr.1
25386 @end group
25387 @end smallexample
25388
25389 @noindent
25390 the code for the function body directly appears:
25391
25392 @smallexample
25393 @group
25394 movl %esi,%eax
25395 #APP
25396    incl %eax
25397 #NO_APP
25398    movl %eax,%edx
25399 @end group
25400 @end smallexample
25401
25402 @noindent
25403 thus saving the overhead of stack frame setup and an out-of-line call.
25404
25405 @c ---------------------------------------------------------------------------
25406 @node Other Asm Functionality
25407 @section Other @code{Asm} Functionality
25408
25409 @noindent
25410 This section describes two important parameters to the @code{Asm}
25411 procedure: @code{Clobber}, which identifies register usage;
25412 and @code{Volatile}, which inhibits unwanted optimizations.
25413
25414 @menu
25415 * The Clobber Parameter::
25416 * The Volatile Parameter::
25417 @end menu
25418
25419 @c ---------------------------------------------------------------------------
25420 @node The Clobber Parameter
25421 @subsection The @code{Clobber} Parameter
25422
25423 @noindent
25424 One of the dangers of intermixing assembly language and a compiled language
25425 such as Ada is that the compiler needs to be aware of which registers are
25426 being used by the assembly code.  In some cases, such as the earlier examples,
25427 the constraint string is sufficient to indicate register usage (e.g.,
25428 @code{"a"} for
25429 the eax register).  But more generally, the compiler needs an explicit
25430 identification of the registers that are used by the Inline Assembly
25431 statements.
25432
25433 Using a register that the compiler doesn't know about
25434 could be a side effect of an instruction (like @code{mull}
25435 storing its result in both eax and edx).
25436 It can also arise from explicit register usage in your
25437 assembly code; for example:
25438 @smallexample
25439 @group
25440 Asm ("movl %0, %%ebx" & LF & HT &
25441      "movl %%ebx, %1",
25442      Inputs  => Unsigned_32'Asm_Input  ("g", Var_In),
25443      Outputs => Unsigned_32'Asm_Output ("=g", Var_Out));
25444 @end group
25445 @end smallexample
25446 @noindent
25447 where the compiler (since it does not analyze the @code{Asm} template string)
25448 does not know you are using the ebx register.
25449
25450 In such cases you need to supply the @code{Clobber} parameter to @code{Asm},
25451 to identify the registers that will be used by your assembly code:
25452
25453 @smallexample
25454 @group
25455 Asm ("movl %0, %%ebx" & LF & HT &
25456      "movl %%ebx, %1",
25457      Inputs  => Unsigned_32'Asm_Input  ("g", Var_In),
25458      Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
25459      Clobber => "ebx");
25460 @end group
25461 @end smallexample
25462
25463 The Clobber parameter is a static string expression specifying the
25464 register(s) you are using.  Note that register names are @emph{not} prefixed
25465 by a percent sign. Also, if more than one register is used then their names
25466 are separated by commas; e.g., @code{"eax, ebx"}
25467
25468 The @code{Clobber} parameter has several additional uses:
25469 @enumerate
25470 @item Use ``register'' name @code{cc} to indicate that flags might have changed
25471 @item Use ``register'' name @code{memory} if you changed a memory location
25472 @end enumerate
25473
25474 @c ---------------------------------------------------------------------------
25475 @node The Volatile Parameter
25476 @subsection The @code{Volatile} Parameter
25477 @cindex Volatile parameter
25478
25479 @noindent
25480 Compiler optimizations in the presence of Inline Assembler may sometimes have
25481 unwanted effects.  For example, when an @code{Asm} invocation with an input
25482 variable is inside a loop, the compiler might move the loading of the input
25483 variable outside the loop, regarding it as a one-time initialization.
25484
25485 If this effect is not desired, you can disable such optimizations by setting
25486 the @code{Volatile} parameter to @code{True}; for example:
25487
25488 @smallexample @c ada
25489 @group
25490 Asm ("movl %0, %%ebx" & LF & HT &
25491      "movl %%ebx, %1",
25492      Inputs   => Unsigned_32'Asm_Input  ("g", Var_In),
25493      Outputs  => Unsigned_32'Asm_Output ("=g", Var_Out),
25494      Clobber  => "ebx",
25495      Volatile => True);
25496 @end group
25497 @end smallexample
25498
25499 By default, @code{Volatile} is set to @code{False} unless there is no
25500 @code{Outputs} parameter.
25501
25502 Although setting @code{Volatile} to @code{True} prevents unwanted
25503 optimizations, it will also disable other optimizations that might be
25504 important for efficiency. In general, you should set @code{Volatile}
25505 to @code{True} only if the compiler's optimizations have created
25506 problems.
25507 @c END OF INLINE ASSEMBLER CHAPTER
25508 @c ===============================
25509
25510 @c ***********************************
25511 @c * Compatibility and Porting Guide *
25512 @c ***********************************
25513 @node Compatibility and Porting Guide
25514 @appendix Compatibility and Porting Guide
25515
25516 @noindent
25517 This chapter describes the compatibility issues that may arise between
25518 GNAT and other Ada 83 and Ada 95 compilation systems, and shows how GNAT
25519 can expedite porting
25520 applications developed in other Ada environments.
25521
25522 @menu
25523 * Compatibility with Ada 83::
25524 * Implementation-dependent characteristics::
25525 * Compatibility with Other Ada 95 Systems::
25526 * Representation Clauses::
25527 * Compatibility with HP Ada 83::
25528 @ifset vms
25529 * Transitioning from Alpha to Integrity OpenVMS::
25530 @end ifset
25531 @end menu
25532
25533 @node Compatibility with Ada 83
25534 @section Compatibility with Ada 83
25535 @cindex Compatibility (between Ada 83 and Ada 95)
25536
25537 @noindent
25538 Ada 95 is designed to be highly upwards compatible with Ada 83.  In
25539 particular, the design intention is that the difficulties associated
25540 with moving from Ada 83 to Ada 95 should be no greater than those
25541 that occur when moving from one Ada 83 system to another.
25542
25543 However, there are a number of points at which there are minor
25544 incompatibilities.  The @cite{Ada 95 Annotated Reference Manual} contains
25545 full details of these issues,
25546 and should be consulted for a complete treatment.
25547 In practice the
25548 following subsections treat the most likely issues to be encountered.
25549
25550 @menu
25551 * Legal Ada 83 programs that are illegal in Ada 95::
25552 * More deterministic semantics::
25553 * Changed semantics::
25554 * Other language compatibility issues::
25555 @end menu
25556
25557 @node Legal Ada 83 programs that are illegal in Ada 95
25558 @subsection Legal Ada 83 programs that are illegal in Ada 95
25559
25560 @table @asis
25561 @item Character literals
25562 Some uses of character literals are ambiguous.  Since Ada 95 has introduced
25563 @code{Wide_Character} as a new predefined character type, some uses of
25564 character literals that were legal in Ada 83 are illegal in Ada 95.
25565 For example:
25566 @smallexample @c ada
25567    for Char in 'A' .. 'Z' loop ... end loop;
25568 @end smallexample
25569 @noindent
25570 The problem is that @code{'A'} and @code{'Z'} could be from either
25571 @code{Character} or @code{Wide_Character}.  The simplest correction
25572 is to make the type explicit; e.g.:
25573 @smallexample @c ada
25574    for Char in Character range 'A' .. 'Z' loop ... end loop;
25575 @end smallexample
25576
25577 @item New reserved words
25578 The identifiers @code{abstract}, @code{aliased}, @code{protected},
25579 @code{requeue}, @code{tagged}, and @code{until} are reserved in Ada 95.
25580 Existing Ada 83 code using any of these identifiers must be edited to
25581 use some alternative name.
25582
25583 @item Freezing rules
25584 The rules in Ada 95 are slightly different with regard to the point at
25585 which entities are frozen, and representation pragmas and clauses are
25586 not permitted past the freeze point.  This shows up most typically in
25587 the form of an error message complaining that a representation item
25588 appears too late, and the appropriate corrective action is to move
25589 the item nearer to the declaration of the entity to which it refers.
25590
25591 A particular case is that representation pragmas
25592 @ifset vms
25593 (including the
25594 extended HP Ada 83 compatibility pragmas such as @code{Export_Procedure})
25595 @end ifset
25596 cannot be applied to a subprogram body.  If necessary, a separate subprogram
25597 declaration must be introduced to which the pragma can be applied.
25598
25599 @item Optional bodies for library packages
25600 In Ada 83, a package that did not require a package body was nevertheless
25601 allowed to have one.  This lead to certain surprises in compiling large
25602 systems (situations in which the body could be unexpectedly ignored by the
25603 binder).  In Ada 95, if a package does not require a body then it is not
25604 permitted to have a body.  To fix this problem, simply remove a redundant
25605 body if it is empty, or, if it is non-empty, introduce a dummy declaration
25606 into the spec that makes the body required.  One approach is to add a private
25607 part to the package declaration (if necessary), and define a parameterless
25608 procedure called @code{Requires_Body}, which must then be given a dummy
25609 procedure body in the package body, which then becomes required.
25610 Another approach (assuming that this does not introduce elaboration
25611 circularities) is to add an @code{Elaborate_Body} pragma to the package spec,
25612 since one effect of this pragma is to require the presence of a package body.
25613
25614 @item @code{Numeric_Error} is now the same as @code{Constraint_Error}
25615 In Ada 95, the exception @code{Numeric_Error} is a renaming of
25616 @code{Constraint_Error}.
25617 This means that it is illegal to have separate exception handlers for
25618 the two exceptions.  The fix is simply to remove the handler for the
25619 @code{Numeric_Error} case (since even in Ada 83, a compiler was free to raise
25620 @code{Constraint_Error} in place of @code{Numeric_Error} in all cases).
25621
25622 @item Indefinite subtypes in generics
25623 In Ada 83, it was permissible to pass an indefinite type (e.g.@: @code{String})
25624 as the actual for a generic formal private type, but then the instantiation
25625 would be illegal if there were any instances of declarations of variables
25626 of this type in the generic body.  In Ada 95, to avoid this clear violation
25627 of the methodological principle known as the ``contract model'',
25628 the generic declaration explicitly indicates whether
25629 or not such instantiations are permitted.  If a generic formal parameter
25630 has explicit unknown discriminants, indicated by using @code{(<>)} after the
25631 type name, then it can be instantiated with indefinite types, but no
25632 stand-alone variables can be declared of this type.  Any attempt to declare
25633 such a variable will result in an illegality at the time the generic is
25634 declared.  If the @code{(<>)} notation is not used, then it is illegal
25635 to instantiate the generic with an indefinite type.
25636 This is the potential incompatibility issue when porting Ada 83 code to Ada 95.
25637 It will show up as a compile time error, and
25638 the fix is usually simply to add the @code{(<>)} to the generic declaration.
25639 @end table
25640
25641 @node More deterministic semantics
25642 @subsection More deterministic semantics
25643
25644 @table @asis
25645 @item Conversions
25646 Conversions from real types to integer types round away from 0.  In Ada 83
25647 the conversion Integer(2.5) could deliver either 2 or 3 as its value.  This
25648 implementation freedom was intended to support unbiased rounding in
25649 statistical applications, but in practice it interfered with portability.
25650 In Ada 95 the conversion semantics are unambiguous, and rounding away from 0
25651 is required.  Numeric code may be affected by this change in semantics.
25652 Note, though, that this issue is no worse than already existed in Ada 83
25653 when porting code from one vendor to another.
25654
25655 @item Tasking
25656 The Real-Time Annex introduces a set of policies that define the behavior of
25657 features that were implementation dependent in Ada 83, such as the order in
25658 which open select branches are executed.
25659 @end table
25660
25661 @node Changed semantics
25662 @subsection Changed semantics
25663
25664 @noindent
25665 The worst kind of incompatibility is one where a program that is legal in
25666 Ada 83 is also legal in Ada 95 but can have an effect in Ada 95 that was not
25667 possible in Ada 83.  Fortunately this is extremely rare, but the one
25668 situation that you should be alert to is the change in the predefined type
25669 @code{Character} from 7-bit ASCII to 8-bit Latin-1.
25670
25671 @table @asis
25672 @item range of @code{Character}
25673 The range of @code{Standard.Character} is now the full 256 characters
25674 of Latin-1, whereas in most Ada 83 implementations it was restricted
25675 to 128 characters. Although some of the effects of
25676 this change will be manifest in compile-time rejection of legal
25677 Ada 83 programs it is possible for a working Ada 83 program to have
25678 a different effect in Ada 95, one that was not permitted in Ada 83.
25679 As an example, the expression
25680 @code{Character'Pos(Character'Last)} returned @code{127} in Ada 83 and now
25681 delivers @code{255} as its value.
25682 In general, you should look at the logic of any
25683 character-processing Ada 83 program and see whether it needs to be adapted
25684 to work correctly with Latin-1.  Note that the predefined Ada 95 API has a
25685 character handling package that may be relevant if code needs to be adapted
25686 to account for the additional Latin-1 elements.
25687 The desirable fix is to
25688 modify the program to accommodate the full character set, but in some cases
25689 it may be convenient to define a subtype or derived type of Character that
25690 covers only the restricted range.
25691 @cindex Latin-1
25692 @end table
25693
25694 @node Other language compatibility issues
25695 @subsection Other language compatibility issues
25696 @table @asis
25697 @item @option{-gnat83 switch}
25698 All implementations of GNAT provide a switch that causes GNAT to operate
25699 in Ada 83 mode.  In this mode, some but not all compatibility problems
25700 of the type described above are handled automatically.  For example, the
25701 new Ada 95 reserved words are treated simply as identifiers as in Ada 83.
25702 However,
25703 in practice, it is usually advisable to make the necessary modifications
25704 to the program to remove the need for using this switch.
25705 See @ref{Compiling Different Versions of Ada}.
25706
25707 @item Support for removed Ada 83 pragmas and attributes
25708 A number of pragmas and attributes from Ada 83 have been removed from Ada 95,
25709 generally because they have been replaced by other mechanisms.  Ada 95
25710 compilers are allowed, but not required, to implement these missing
25711 elements.  In contrast with some other Ada 95 compilers, GNAT implements all
25712 such pragmas and attributes, eliminating this compatibility concern.  These
25713 include @code{pragma Interface} and the floating point type attributes
25714 (@code{Emax}, @code{Mantissa}, etc.), among other items.
25715 @end table
25716
25717 @node Implementation-dependent characteristics
25718 @section Implementation-dependent characteristics
25719 @noindent
25720 Although the Ada language defines the semantics of each construct as
25721 precisely as practical, in some situations (for example for reasons of
25722 efficiency, or where the effect is heavily dependent on the host or target
25723 platform) the implementation is allowed some freedom.  In porting Ada 83
25724 code to GNAT, you need to be aware of whether / how the existing code
25725 exercised such implementation dependencies.  Such characteristics fall into
25726 several categories, and GNAT offers specific support in assisting the
25727 transition from certain Ada 83 compilers.
25728
25729 @menu
25730 * Implementation-defined pragmas::
25731 * Implementation-defined attributes::
25732 * Libraries::
25733 * Elaboration order::
25734 * Target-specific aspects::
25735 @end menu
25736
25737 @node Implementation-defined pragmas
25738 @subsection Implementation-defined pragmas
25739
25740 @noindent
25741 Ada compilers are allowed to supplement the language-defined pragmas, and
25742 these are a potential source of non-portability.  All GNAT-defined pragmas
25743 are described in the GNAT Reference Manual, and these include several that
25744 are specifically intended to correspond to other vendors' Ada 83 pragmas.
25745 For migrating from VADS, the pragma @code{Use_VADS_Size} may be useful.
25746 For
25747 compatibility with HP Ada 83, GNAT supplies the pragmas
25748 @code{Extend_System}, @code{Ident}, @code{Inline_Generic},
25749 @code{Interface_Name}, @code{Passive}, @code{Suppress_All},
25750 and @code{Volatile}.
25751 Other relevant pragmas include @code{External} and @code{Link_With}.
25752 Some vendor-specific
25753 Ada 83 pragmas (@code{Share_Generic}, @code{Subtitle}, and @code{Title}) are
25754 recognized, thus
25755 avoiding compiler rejection of units that contain such pragmas; they are not
25756 relevant in a GNAT context and hence are not otherwise implemented.
25757
25758 @node Implementation-defined attributes
25759 @subsection Implementation-defined attributes
25760
25761 Analogous to pragmas, the set of attributes may be extended by an
25762 implementation.  All GNAT-defined attributes are described in the
25763 @cite{GNAT Reference Manual}, and these include several that are specifically
25764 intended
25765 to correspond to other vendors' Ada 83 attributes.  For migrating from VADS,
25766 the attribute @code{VADS_Size} may be useful.  For compatibility with HP
25767 Ada 83, GNAT supplies the attributes @code{Bit}, @code{Machine_Size} and
25768 @code{Type_Class}.
25769
25770 @node Libraries
25771 @subsection Libraries
25772 @noindent
25773 Vendors may supply libraries to supplement the standard Ada API.  If Ada 83
25774 code uses vendor-specific libraries then there are several ways to manage
25775 this in Ada 95:
25776 @enumerate
25777 @item
25778 If the source code for the libraries (specifications and bodies) are
25779 available, then the libraries can be migrated in the same way as the
25780 application.
25781 @item
25782 If the source code for the specifications but not the bodies are
25783 available, then you can reimplement the bodies.
25784 @item
25785 Some new Ada 95 features obviate the need for library support.  For
25786 example most Ada 83 vendors supplied a package for unsigned integers.  The
25787 Ada 95 modular type feature is the preferred way to handle this need, so
25788 instead of migrating or reimplementing the unsigned integer package it may
25789 be preferable to retrofit the application using modular types.
25790 @end enumerate
25791
25792 @node Elaboration order
25793 @subsection Elaboration order
25794 @noindent
25795 The implementation can choose any elaboration order consistent with the unit
25796 dependency relationship.  This freedom means that some orders can result in
25797 Program_Error being raised due to an ``Access Before Elaboration'': an attempt
25798 to invoke a subprogram its body has been elaborated, or to instantiate a
25799 generic before the generic body has been elaborated.  By default GNAT
25800 attempts to choose a safe order (one that will not encounter access before
25801 elaboration problems) by implicitly inserting @code{Elaborate} or
25802 @code{Elaborate_All} pragmas where
25803 needed.  However, this can lead to the creation of elaboration circularities
25804 and a resulting rejection of the program by gnatbind.  This issue is
25805 thoroughly described in @ref{Elaboration Order Handling in GNAT}.
25806 In brief, there are several
25807 ways to deal with this situation:
25808
25809 @itemize @bullet
25810 @item
25811 Modify the program to eliminate the circularities, e.g. by moving
25812 elaboration-time code into explicitly-invoked procedures
25813 @item
25814 Constrain the elaboration order by including explicit @code{Elaborate_Body} or
25815 @code{Elaborate} pragmas, and then inhibit the generation of implicit
25816 @code{Elaborate_All}
25817 pragmas either globally (as an effect of the @option{-gnatE} switch) or locally
25818 (by selectively suppressing elaboration checks via pragma
25819 @code{Suppress(Elaboration_Check)} when it is safe to do so).
25820 @end itemize
25821
25822 @node Target-specific aspects
25823 @subsection Target-specific aspects
25824 @noindent
25825 Low-level applications need to deal with machine addresses, data
25826 representations, interfacing with assembler code, and similar issues.  If
25827 such an Ada 83 application is being ported to different target hardware (for
25828 example where the byte endianness has changed) then you will need to
25829 carefully examine the program logic; the porting effort will heavily depend
25830 on the robustness of the original design.  Moreover, Ada 95 is sometimes
25831 incompatible with typical Ada 83 compiler practices regarding implicit
25832 packing, the meaning of the Size attribute, and the size of access values.
25833 GNAT's approach to these issues is described in @ref{Representation Clauses}.
25834
25835 @node Compatibility with Other Ada 95 Systems
25836 @section Compatibility with Other Ada 95 Systems
25837
25838 @noindent
25839 Providing that programs avoid the use of implementation dependent and
25840 implementation defined features of Ada 95, as documented in the Ada 95
25841 reference manual, there should be a high degree of portability between
25842 GNAT and other Ada 95 systems.  The following are specific items which
25843 have proved troublesome in moving GNAT programs to other Ada 95
25844 compilers, but do not affect porting code to GNAT@.
25845
25846 @table @asis
25847 @item Ada 83 Pragmas and Attributes
25848 Ada 95 compilers are allowed, but not required, to implement the missing
25849 Ada 83 pragmas and attributes that are no longer defined in Ada 95.
25850 GNAT implements all such pragmas and attributes, eliminating this as
25851 a compatibility concern, but some other Ada 95 compilers reject these
25852 pragmas and attributes.
25853
25854 @item Special-needs Annexes
25855 GNAT implements the full set of special needs annexes.  At the
25856 current time, it is the only Ada 95 compiler to do so.  This means that
25857 programs making use of these features may not be portable to other Ada
25858 95 compilation systems.
25859
25860 @item Representation Clauses
25861 Some other Ada 95 compilers implement only the minimal set of
25862 representation clauses required by the Ada 95 reference manual.  GNAT goes
25863 far beyond this minimal set, as described in the next section.
25864 @end table
25865
25866 @node Representation Clauses
25867 @section Representation Clauses
25868
25869 @noindent
25870 The Ada 83 reference manual was quite vague in describing both the minimal
25871 required implementation of representation clauses, and also their precise
25872 effects.  The Ada 95 reference manual is much more explicit, but the minimal
25873 set of capabilities required in Ada 95 is quite limited.
25874
25875 GNAT implements the full required set of capabilities described in the
25876 Ada 95 reference manual, but also goes much beyond this, and in particular
25877 an effort has been made to be compatible with existing Ada 83 usage to the
25878 greatest extent possible.
25879
25880 A few cases exist in which Ada 83 compiler behavior is incompatible with
25881 requirements in the Ada 95 reference manual.  These are instances of
25882 intentional or accidental dependence on specific implementation dependent
25883 characteristics of these Ada 83 compilers.  The following is a list of
25884 the cases most likely to arise in existing legacy Ada 83 code.
25885
25886 @table @asis
25887 @item Implicit Packing
25888 Some Ada 83 compilers allowed a Size specification to cause implicit
25889 packing of an array or record.  This could cause expensive implicit
25890 conversions for change of representation in the presence of derived
25891 types, and the Ada design intends to avoid this possibility.
25892 Subsequent AI's were issued to make it clear that such implicit
25893 change of representation in response to a Size clause is inadvisable,
25894 and this recommendation is represented explicitly in the Ada 95 RM
25895 as implementation advice that is followed by GNAT@.
25896 The problem will show up as an error
25897 message rejecting the size clause.  The fix is simply to provide
25898 the explicit pragma @code{Pack}, or for more fine tuned control, provide
25899 a Component_Size clause.
25900
25901 @item Meaning of Size Attribute
25902 The Size attribute in Ada 95 for discrete types is defined as being the
25903 minimal number of bits required to hold values of the type.  For example,
25904 on a 32-bit machine, the size of Natural will typically be 31 and not
25905 32 (since no sign bit is required).  Some Ada 83 compilers gave 31, and
25906 some 32 in this situation.  This problem will usually show up as a compile
25907 time error, but not always.  It is a good idea to check all uses of the
25908 'Size attribute when porting Ada 83 code.  The GNAT specific attribute
25909 Object_Size can provide a useful way of duplicating the behavior of
25910 some Ada 83 compiler systems.
25911
25912 @item Size of Access Types
25913 A common assumption in Ada 83 code is that an access type is in fact a pointer,
25914 and that therefore it will be the same size as a System.Address value.  This
25915 assumption is true for GNAT in most cases with one exception.  For the case of
25916 a pointer to an unconstrained array type (where the bounds may vary from one
25917 value of the access type to another), the default is to use a ``fat pointer'',
25918 which is represented as two separate pointers, one to the bounds, and one to
25919 the array.  This representation has a number of advantages, including improved
25920 efficiency.  However, it may cause some difficulties in porting existing Ada 83
25921 code which makes the assumption that, for example, pointers fit in 32 bits on
25922 a machine with 32-bit addressing.
25923
25924 To get around this problem, GNAT also permits the use of ``thin pointers'' for
25925 access types in this case (where the designated type is an unconstrained array
25926 type).  These thin pointers are indeed the same size as a System.Address value.
25927 To specify a thin pointer, use a size clause for the type, for example:
25928
25929 @smallexample @c ada
25930 type X is access all String;
25931 for X'Size use Standard'Address_Size;
25932 @end smallexample
25933
25934 @noindent
25935 which will cause the type X to be represented using a single pointer.
25936 When using this representation, the bounds are right behind the array.
25937 This representation is slightly less efficient, and does not allow quite
25938 such flexibility in the use of foreign pointers or in using the
25939 Unrestricted_Access attribute to create pointers to non-aliased objects.
25940 But for any standard portable use of the access type it will work in
25941 a functionally correct manner and allow porting of existing code.
25942 Note that another way of forcing a thin pointer representation
25943 is to use a component size clause for the element size in an array,
25944 or a record representation clause for an access field in a record.
25945 @end table
25946
25947 @node Compatibility with HP Ada 83
25948 @section Compatibility with HP Ada 83
25949
25950 @noindent
25951 The VMS version of GNAT fully implements all the pragmas and attributes
25952 provided by HP Ada 83, as well as providing the standard HP Ada 83
25953 libraries, including Starlet.  In addition, data layouts and parameter
25954 passing conventions are highly compatible.  This means that porting
25955 existing HP Ada 83 code to GNAT in VMS systems should be easier than
25956 most other porting efforts.  The following are some of the most
25957 significant differences between GNAT and HP Ada 83.
25958
25959 @table @asis
25960 @item Default floating-point representation
25961 In GNAT, the default floating-point format is IEEE, whereas in HP Ada 83,
25962 it is VMS format.  GNAT does implement the necessary pragmas
25963 (Long_Float, Float_Representation) for changing this default.
25964
25965 @item System
25966 The package System in GNAT exactly corresponds to the definition in the
25967 Ada 95 reference manual, which means that it excludes many of the
25968 HP Ada 83 extensions.  However, a separate package Aux_DEC is provided
25969 that contains the additional definitions, and a special pragma,
25970 Extend_System allows this package to be treated transparently as an
25971 extension of package System.
25972
25973 @item To_Address
25974 The definitions provided by Aux_DEC are exactly compatible with those
25975 in the HP Ada 83 version of System, with one exception.
25976 HP Ada provides the following declarations:
25977
25978 @smallexample @c ada
25979 TO_ADDRESS (INTEGER)
25980 TO_ADDRESS (UNSIGNED_LONGWORD)
25981 TO_ADDRESS (universal_integer)
25982 @end smallexample
25983
25984 @noindent
25985 The version of TO_ADDRESS taking a universal integer argument is in fact
25986 an extension to Ada 83 not strictly compatible with the reference manual.
25987 In GNAT, we are constrained to be exactly compatible with the standard,
25988 and this means we cannot provide this capability.  In HP Ada 83, the
25989 point of this definition is to deal with a call like:
25990
25991 @smallexample @c ada
25992 TO_ADDRESS (16#12777#);
25993 @end smallexample
25994
25995 @noindent
25996 Normally, according to the Ada 83 standard, one would expect this to be
25997 ambiguous, since it matches both the INTEGER and UNSIGNED_LONGWORD forms
25998 of TO_ADDRESS@.  However, in HP Ada 83, there is no ambiguity, since the
25999 definition using universal_integer takes precedence.
26000
26001 In GNAT, since the version with universal_integer cannot be supplied, it is
26002 not possible to be 100% compatible.  Since there are many programs using
26003 numeric constants for the argument to TO_ADDRESS, the decision in GNAT was
26004 to change the name of the function in the UNSIGNED_LONGWORD case, so the
26005 declarations provided in the GNAT version of AUX_Dec are:
26006
26007 @smallexample @c ada
26008 function To_Address (X : Integer) return Address;
26009 pragma Pure_Function (To_Address);
26010
26011 function To_Address_Long (X : Unsigned_Longword)
26012  return Address;
26013 pragma Pure_Function (To_Address_Long);
26014 @end smallexample
26015
26016 @noindent
26017 This means that programs using TO_ADDRESS for UNSIGNED_LONGWORD must
26018 change the name to TO_ADDRESS_LONG@.
26019
26020 @item Task_Id values
26021 The Task_Id values assigned will be different in the two systems, and GNAT
26022 does not provide a specified value for the Task_Id of the environment task,
26023 which in GNAT is treated like any other declared task.
26024 @end table
26025
26026 For full details on these and other less significant compatibility issues,
26027 see appendix E of the HP publication entitled @cite{HP Ada, Technical
26028 Overview and Comparison on HP Platforms}.
26029
26030 For GNAT running on other than VMS systems, all the HP Ada 83 pragmas and
26031 attributes are recognized, although only a subset of them can sensibly
26032 be implemented.  The description of pragmas in this reference manual
26033 indicates whether or not they are applicable to non-VMS systems.
26034
26035
26036 @ifset vms
26037 @node Transitioning from Alpha to Integrity OpenVMS
26038 @section Transitioning from Alpha to Integrity OpenVMS
26039
26040 @menu
26041 * Introduction to transitioning::
26042 * Migration of 32 bit code::
26043 * Taking advantage of 64 bit addressing::
26044 * Technical details::
26045 @end menu
26046
26047 @node Introduction to transitioning
26048 @subsection Introduction to transitioning
26049
26050 @noindent
26051 This guide is meant to assist users of GNAT Pro
26052 for Alpha OpenVMS who are planning to transition to the IA64 architecture.
26053 GNAT Pro for Open VMS Integrity has been designed to meet
26054 three main goals:
26055
26056 @enumerate
26057 @item
26058 Providing a full conforming implementation of the Ada 95 language
26059
26060 @item
26061 Allowing maximum backward compatibility, thus easing migration of existing
26062 Ada source code
26063
26064 @item
26065 Supplying a path for exploiting the full IA64 address range
26066 @end enumerate
26067
26068 @noindent
26069 Ada's strong typing semantics has made it
26070 impractical to have different 32-bit and 64-bit modes. As soon as
26071 one object could possibly be outside the 32-bit address space, this
26072 would make it necessary for the @code{System.Address} type to be 64 bits.
26073 In particular, this would cause inconsistencies if 32-bit code is
26074 called from 64-bit code that raises an exception.
26075
26076 This issue has been resolved by always using 64-bit addressing
26077 at the system level, but allowing for automatic conversions between
26078 32-bit and 64-bit addresses where required. Thus users who
26079 do not currently require 64-bit addressing capabilities, can
26080 recompile their code with only minimal changes (and indeed
26081 if the code is written in portable Ada, with no assumptions about
26082 the size of the @code{Address} type, then no changes at all are necessary).
26083 At the same time,
26084 this approach provides a simple, gradual upgrade path to future
26085 use of larger memories than available for 32-bit systems.
26086 Also, newly written applications or libraries will by default
26087 be fully compatible with future systems exploiting 64-bit
26088 addressing capabilities present in IA64.
26089
26090 @ref{Migration of 32 bit code}, will focus on porting applications
26091 that do not require more than 2 GB of
26092 addressable memory. This code will be referred to as
26093 @emph{32-bit code}.
26094 For applications intending to exploit the full ia64 address space,
26095 @ref{Taking advantage of 64 bit addressing},
26096 will consider further changes that may be required.
26097 Such code is called @emph{64-bit code} in the
26098 remainder of this guide.
26099
26100
26101 @node Migration of 32 bit code
26102 @subsection Migration of 32-bit code
26103
26104 @menu
26105 * Address types::
26106 * Access types::
26107 * Unchecked conversions::
26108 * Predefined constants::
26109 * Single source compatibility::
26110 * Experience with source compatibility::
26111 @end menu
26112
26113 @node Address types
26114 @subsubsection Address types
26115
26116 @noindent
26117 To solve the problem of mixing 64-bit and 32-bit addressing,
26118 while maintaining maximum backward compatibility, the following
26119 approach has been taken:
26120
26121 @itemize @bullet
26122 @item
26123 @code{System.Address} always has a size of 64 bits
26124
26125 @item
26126 @code{System.Short_Address} is a 32-bit subtype of @code{System.Address}
26127 @end itemize
26128
26129
26130 @noindent
26131 Since @code{System.Short_Address} is a subtype of @code{System.Address},
26132 a @code{Short_Address}
26133 may be used where an @code{Address} is required, and vice versa, without
26134 needing explicit type conversions.
26135 By virtue of the Open VMS Integrity parameter passing conventions,
26136 even imported
26137 and exported subprograms that have 32-bit address parameters are
26138 compatible with those that have 64-bit address parameters.
26139 (See @ref{Making code 64 bit clean} for details.)
26140
26141 The areas that may need attention are those where record types have
26142 been defined that contain components of the type @code{System.Address}, and
26143 where objects of this type are passed to code expecting a record layout with
26144 32-bit addresses.
26145
26146 Different compilers on different platforms cannot be
26147 expected to represent the same type in the same way,
26148 since alignment constraints
26149 and other system-dependent properties affect the compiler's decision.
26150 For that reason, Ada code
26151 generally uses representation clauses to specify the expected
26152 layout where required.
26153
26154 If such a representation clause uses 32 bits for a component having
26155 the type @code{System.Address}, GNAT Pro for OpenVMS Integrity will detect
26156 that error and produce a specific diagnostic message.
26157 The developer should then determine whether the representation
26158 should be 64 bits or not and make either of two changes:
26159 change the size to 64 bits and leave the type as @code{System.Address}, or
26160 leave the size as 32 bits and change the type to @code{System.Short_Address}.
26161 Since @code{Short_Address} is a subtype of @code{Address}, no changes are
26162 required in any code setting or accessing the field; the compiler will
26163 automatically perform any needed conversions between address
26164 formats.
26165
26166 @node Access types
26167 @subsubsection Access types
26168
26169 @noindent
26170 By default, objects designated by access values are always
26171 allocated in the 32-bit
26172 address space. Thus legacy code will never contain
26173 any objects that are not addressable with 32-bit addresses, and
26174 the compiler will never raise exceptions as result of mixing
26175 32-bit and 64-bit addresses.
26176
26177 However, the access values themselves are represented in 64 bits, for optimum
26178 performance and future compatibility with 64-bit code. As was
26179 the case with @code{System.Address}, the compiler will give an error message
26180 if an object or record component has a representation clause that
26181 requires the access value to fit in 32 bits. In such a situation,
26182 an explicit size clause for the access type, specifying 32 bits,
26183 will have the desired effect.
26184
26185 General access types (declared with @code{access all}) can never be
26186 32 bits, as values of such types must be able to refer to any object
26187 of the  designated type,
26188 including objects residing outside the 32-bit address range.
26189 Existing Ada 83 code will not contain such type definitions,
26190 however, since general access types were introduced in Ada 95.
26191
26192 @node Unchecked conversions
26193 @subsubsection Unchecked conversions
26194
26195 @noindent
26196 In the case of an @code{Unchecked_Conversion} where the source type is a
26197 64-bit access type or the type @code{System.Address}, and the target
26198 type is a 32-bit type, the compiler will generate a warning.
26199 Even though the generated code will still perform the required
26200 conversions, it is highly recommended in these cases to use
26201 respectively a 32-bit access type or @code{System.Short_Address}
26202 as the source type.
26203
26204 @node Predefined constants
26205 @subsubsection Predefined constants
26206
26207 @noindent
26208 The following predefined constants have changed:
26209
26210 @multitable {@code{System.Address_Size}} {2**32} {2**64}
26211 @item   @b{Constant}               @tab @b{Old} @tab @b{New}
26212 @item   @code{System.Word_Size}    @tab 32      @tab 64
26213 @item   @code{System.Memory_Size}  @tab 2**32   @tab 2**64
26214 @item   @code{System.Address_Size} @tab 32      @tab 64
26215 @end multitable
26216
26217 @noindent
26218 If you need to refer to the specific
26219 memory size of a 32-bit implementation, instead of the
26220 actual memory size, use @code{System.Short_Memory_Size}
26221 rather than @code{System.Memory_Size}.
26222 Similarly, references to @code{System.Address_Size} may need
26223 to be replaced by @code{System.Short_Address'Size}.
26224 The program @command{gnatfind} may be useful for locating
26225 references to the above constants, so that you can verify that they
26226 are still correct.
26227
26228 @node Single source compatibility
26229 @subsubsection Single source compatibility
26230
26231 @noindent
26232 In order to allow the same source code to be compiled on
26233 both Alpha and IA64 platforms, GNAT Pro for Alpha/OpenVMS
26234 defines @code{System.Short_Address} and System.Short_Memory_Size
26235 as aliases of respectively @code{System.Address} and
26236 @code{System.Memory_Size}.
26237 (These aliases also leave the door open for a possible
26238 future ``upgrade'' of OpenVMS Alpha to a 64-bit address space.)
26239
26240 @node Experience with source compatibility
26241 @subsubsection Experience with source compatibility
26242
26243 @noindent
26244 The Security Server and STARLET provide an interesting ``test case''
26245 for source compatibility issues, since it is in such system code
26246 where assumptions about @code{Address} size might be expected to occur.
26247 Indeed, there were a small number of occasions in the Security Server
26248 file @file{jibdef.ads}
26249 where a representation clause for a record type specified
26250 32 bits for a component of type @code{Address}.
26251 All of these errors were detected by the compiler.
26252 The repair was obvious and immediate; to simply replace @code{Address} by
26253 @code{Short_Address}.
26254
26255 In the case of STARLET, there were several record types that should
26256 have had representation clauses but did not.  In these record types
26257 there was an implicit assumption that an @code{Address} value occupied
26258 32 bits.
26259 These compiled without error, but their usage resulted in run-time error
26260 returns from STARLET system calls.
26261 To assist in the compile-time detection of such situations, we
26262 plan to include a switch to generate a warning message when a
26263 record component is of type @code{Address}.
26264
26265
26266 @c ****************************************
26267 @node Taking advantage of 64 bit addressing
26268 @subsection Taking advantage of 64-bit addressing
26269
26270 @menu
26271 * Making code 64 bit clean::
26272 * Allocating memory from the 64 bit storage pool::
26273 * Restrictions on use of 64 bit objects::
26274 * Using 64 bit storage pools by default::
26275 * General access types::
26276 * STARLET and other predefined libraries::
26277 @end menu
26278
26279 @node Making code 64 bit clean
26280 @subsubsection Making code 64-bit clean
26281
26282 @noindent
26283 In order to prevent problems that may occur when (parts of) a
26284 system start using memory outside the 32-bit address range,
26285 we recommend some additional guidelines:
26286
26287 @itemize @bullet
26288 @item
26289 For imported subprograms that take parameters of the
26290 type @code{System.Address}, ensure that these subprograms can
26291 indeed handle 64-bit addresses. If not, or when in doubt,
26292 change the subprogram declaration to specify
26293 @code{System.Short_Address} instead.
26294
26295 @item
26296 Resolve all warnings related to size mismatches in
26297 unchecked conversions. Failing to do so causes
26298 erroneous execution if the source object is outside
26299 the 32-bit address space.
26300
26301 @item
26302 (optional) Explicitly use the 32-bit storage pool
26303 for access types used in a 32-bit context, or use
26304 generic access types where possible
26305 (@pxref{Restrictions on use of 64 bit objects}).
26306 @end itemize
26307
26308 @noindent
26309 If these rules are followed, the compiler will automatically insert
26310 any necessary checks to ensure that no addresses or access values
26311 passed to 32-bit code ever refer to objects outside the 32-bit
26312 address range.
26313 Any attempt to do this will raise @code{Constraint_Error}.
26314
26315 @node Allocating memory from the 64 bit storage pool
26316 @subsubsection Allocating memory from the 64-bit storage pool
26317
26318 @noindent
26319 For any access type @code{T} that potentially requires memory allocations
26320 beyond the 32-bit address space,
26321 use the following representation clause:
26322
26323 @smallexample @c ada
26324    for T'Storage_Pool use System.Pool_64;
26325 @end smallexample
26326
26327
26328 @node Restrictions on use of 64 bit objects
26329 @subsubsection Restrictions on use of 64-bit objects
26330
26331 @noindent
26332 Taking the address of an object allocated from a 64-bit storage pool,
26333 and then passing this address to a subprogram expecting
26334 @code{System.Short_Address},
26335 or assigning it to a variable of type @code{Short_Address}, will cause
26336 @code{Constraint_Error} to be raised. In case the code is not 64-bit clean
26337 (@pxref{Making code 64 bit clean}), or checks are suppressed,
26338 no exception is raised and execution
26339 will become erroneous.
26340
26341 @node Using 64 bit storage pools by default
26342 @subsubsection Using 64-bit storage pools by default
26343
26344 @noindent
26345 In some cases it may be desirable to have the compiler allocate
26346 from 64-bit storage pools by default. This may be the case for
26347 libraries that are 64-bit clean, but may be used in both 32-bit
26348 and 64-bit contexts. For these cases the following configuration
26349 pragma may be specified:
26350
26351 @smallexample @c ada
26352   pragma Pool_64_Default;
26353 @end smallexample
26354
26355 @noindent
26356 Any code compiled in the context of this pragma will by default
26357 use the @code{System.Pool_64} storage pool. This default may be overridden
26358 for a specific access type @code{T} by the representation clause:
26359
26360 @smallexample @c ada
26361    for T'Storage_Pool use System.Pool_32;
26362 @end smallexample
26363
26364 @noindent
26365 Any object whose address may be passed to a subprogram with a
26366 @code{Short_Address} argument, or assigned to a variable of type
26367 @code{Short_Address}, needs to be allocated from this pool.
26368
26369 @node General access types
26370 @subsubsection General access types
26371
26372 @noindent
26373 Objects designated by access values from a
26374 general access type (declared with @code{access all}) are never allocated
26375 from a 64-bit storage pool. Code that uses general access types will
26376 accept objects allocated in either 32-bit or 64-bit address spaces,
26377 but never allocate objects outside the 32-bit address space.
26378 Using general access types ensures maximum compatibility with both
26379 32-bit and 64-bit code.
26380
26381
26382 @node STARLET and other predefined libraries
26383 @subsubsection STARLET and other predefined libraries
26384
26385 @noindent
26386 All code that comes as part of GNAT is 64-bit clean, but the
26387 restrictions given in @ref{Restrictions on use of 64 bit objects},
26388 still apply. Look at the package
26389 specifications to see in which contexts objects allocated
26390 in 64-bit address space are acceptable.
26391
26392 @node Technical details
26393 @subsection Technical details
26394
26395 @noindent
26396 GNAT Pro for Open VMS Integrity takes advantage of the freedom given in the Ada
26397 standard with respect to the type of @code{System.Address}. Previous versions
26398 of GNAT Pro have defined this type as private and implemented it as
26399 a modular type.
26400
26401 In order to allow defining @code{System.Short_Address} as a proper subtype,
26402 and to match the implicit sign extension in parameter passing,
26403 in GNAT Pro for Open VMS Integrity, @code{System.Address} is defined as a
26404 visible (i.e., non-private) integer type.
26405 Standard operations on the type, such as the binary operators ``+'', ``-'',
26406 etc., that take @code{Address} operands and return an @code{Address} result,
26407 have been hidden by declaring these
26408 @code{abstract}, an Ada 95 feature that helps avoid the potential ambiguities
26409 that would otherwise result from overloading.
26410 (Note that, although @code{Address} is a visible integer type,
26411 good programming practice dictates against exploiting the type's
26412 integer properties such as literals, since this will compromise
26413 code portability.)
26414
26415 Defining @code{Address} as a visible integer type helps achieve
26416 maximum compatibility for existing Ada code,
26417 without sacrificing the capabilities of the IA64 architecture.
26418 @end ifset
26419
26420
26421 @c ************************************************
26422 @ifset unw
26423 @node Microsoft Windows Topics
26424 @appendix Microsoft Windows Topics
26425 @cindex Windows NT
26426 @cindex Windows 95
26427 @cindex Windows 98
26428
26429 @noindent
26430 This chapter describes topics that are specific to the Microsoft Windows
26431 platforms (NT, 2000, and XP Professional).
26432
26433 @menu
26434 * Using GNAT on Windows::
26435 * Using a network installation of GNAT::
26436 * CONSOLE and WINDOWS subsystems::
26437 * Temporary Files::
26438 * Mixed-Language Programming on Windows::
26439 * Windows Calling Conventions::
26440 * Introduction to Dynamic Link Libraries (DLLs)::
26441 * Using DLLs with GNAT::
26442 * Building DLLs with GNAT::
26443 * Building DLLs with GNAT Project files::
26444 * Building DLLs with gnatdll::
26445 * GNAT and Windows Resources::
26446 * Debugging a DLL::
26447 * GNAT and COM/DCOM Objects::
26448 @end menu
26449
26450 @node Using GNAT on Windows
26451 @section Using GNAT on Windows
26452
26453 @noindent
26454 One of the strengths of the GNAT technology is that its tool set
26455 (@command{gcc}, @command{gnatbind}, @command{gnatlink}, @command{gnatmake}, the
26456 @code{gdb} debugger, etc.) is used in the same way regardless of the
26457 platform.
26458
26459 On Windows this tool set is complemented by a number of Microsoft-specific
26460 tools that have been provided to facilitate interoperability with Windows
26461 when this is required. With these tools:
26462
26463 @itemize @bullet
26464
26465 @item
26466 You can build applications using the @code{CONSOLE} or @code{WINDOWS}
26467 subsystems.
26468
26469 @item
26470 You can use any Dynamically Linked Library (DLL) in your Ada code (both
26471 relocatable and non-relocatable DLLs are supported).
26472
26473 @item
26474 You can build Ada DLLs for use in other applications. These applications
26475 can be written in a language other than Ada (e.g., C, C++, etc). Again both
26476 relocatable and non-relocatable Ada DLLs are supported.
26477
26478 @item
26479 You can include Windows resources in your Ada application.
26480
26481 @item
26482 You can use or create COM/DCOM objects.
26483 @end itemize
26484
26485 @noindent
26486 Immediately below are listed all known general GNAT-for-Windows restrictions.
26487 Other restrictions about specific features like Windows Resources and DLLs
26488 are listed in separate sections below.
26489
26490 @itemize @bullet
26491
26492 @item
26493 It is not possible to use @code{GetLastError} and @code{SetLastError}
26494 when tasking, protected records, or exceptions are used. In these
26495 cases, in order to implement Ada semantics, the GNAT run-time system
26496 calls certain Win32 routines that set the last error variable to 0 upon
26497 success. It should be possible to use @code{GetLastError} and
26498 @code{SetLastError} when tasking, protected record, and exception
26499 features are not used, but it is not guaranteed to work.
26500
26501 @item
26502 It is not possible to link against Microsoft libraries except for
26503 import libraries. The library must be built to be compatible with
26504 @file{MSVCRT.LIB} (/MD Microsoft compiler option), @file{LIBC.LIB} and
26505 @file{LIBCMT.LIB} (/ML or /MT Microsoft compiler options) are known to
26506 not be compatible with the GNAT runtime. Even if the library is
26507 compatible with @file{MSVCRT.LIB} it is not guaranteed to work.
26508
26509 @item
26510 When the compilation environment is located on FAT32 drives, users may
26511 experience recompilations of the source files that have not changed if
26512 Daylight Saving Time (DST) state has changed since the last time files
26513 were compiled. NTFS drives do not have this problem.
26514
26515 @item
26516 No components of the GNAT toolset use any entries in the Windows
26517 registry. The only entries that can be created are file associations and
26518 PATH settings, provided the user has chosen to create them at installation
26519 time, as well as some minimal book-keeping information needed to correctly
26520 uninstall or integrate different GNAT products.
26521 @end itemize
26522
26523 @node Using a network installation of GNAT
26524 @section Using a network installation of GNAT
26525
26526 @noindent
26527 Make sure the system on which GNAT is installed is accessible from the
26528 current machine, i.e. the install location is shared over the network.
26529 Shared resources are accessed on Windows by means of UNC paths, which
26530 have the format @code{\\server\sharename\path}
26531
26532 In order to use such a network installation, simply add the UNC path of the
26533 @file{bin} directory of your GNAT installation in front of your PATH. For
26534 example, if GNAT is installed in @file{\GNAT} directory of a share location
26535 called @file{c-drive} on a machine @file{LOKI}, the following command will
26536 make it available:
26537
26538 @code{@ @ @ path \\loki\c-drive\gnat\bin;%path%}
26539
26540 Be aware that every compilation using the network installation results in the
26541 transfer of large amounts of data across the network and will likely cause
26542 serious performance penalty.
26543
26544 @node CONSOLE and WINDOWS subsystems
26545 @section CONSOLE and WINDOWS subsystems
26546 @cindex CONSOLE Subsystem
26547 @cindex WINDOWS Subsystem
26548 @cindex -mwindows
26549
26550 @noindent
26551 There are two main subsystems under Windows. The @code{CONSOLE} subsystem
26552 (which is the default subsystem) will always create a console when
26553 launching the application. This is not something desirable when the
26554 application has a Windows GUI. To get rid of this console the
26555 application must be using the @code{WINDOWS} subsystem. To do so
26556 the @option{-mwindows} linker option must be specified.
26557
26558 @smallexample
26559 $ gnatmake winprog -largs -mwindows
26560 @end smallexample
26561
26562 @node Temporary Files
26563 @section Temporary Files
26564 @cindex Temporary files
26565
26566 @noindent
26567 It is possible to control where temporary files gets created by setting
26568 the TMP environment variable. The file will be created:
26569
26570 @itemize
26571 @item Under the directory pointed to by the TMP environment variable if
26572 this directory exists.
26573
26574 @item Under c:\temp, if the TMP environment variable is not set (or not
26575 pointing to a directory) and if this directory exists.
26576
26577 @item Under the current working directory otherwise.
26578 @end itemize
26579
26580 @noindent
26581 This allows you to determine exactly where the temporary
26582 file will be created. This is particularly useful in networked
26583 environments where you may not have write access to some
26584 directories.
26585
26586 @node Mixed-Language Programming on Windows
26587 @section Mixed-Language Programming on Windows
26588
26589 @noindent
26590 Developing pure Ada applications on Windows is no different than on
26591 other GNAT-supported platforms. However, when developing or porting an
26592 application that contains a mix of Ada and C/C++, the choice of your
26593 Windows C/C++ development environment conditions your overall
26594 interoperability strategy.
26595
26596 If you use @command{gcc} to compile the non-Ada part of your application,
26597 there are no Windows-specific restrictions that affect the overall
26598 interoperability with your Ada code. If you plan to use
26599 Microsoft tools (e.g. Microsoft Visual C/C++), you should be aware of
26600 the following limitations:
26601
26602 @itemize @bullet
26603 @item
26604 You cannot link your Ada code with an object or library generated with
26605 Microsoft tools if these use the @code{.tls} section (Thread Local
26606 Storage section) since the GNAT linker does not yet support this section.
26607
26608 @item
26609 You cannot link your Ada code with an object or library generated with
26610 Microsoft tools if these use I/O routines other than those provided in
26611 the Microsoft DLL: @code{msvcrt.dll}. This is because the GNAT run time
26612 uses the services of @code{msvcrt.dll} for its I/Os. Use of other I/O
26613 libraries can cause a conflict with @code{msvcrt.dll} services. For
26614 instance Visual C++ I/O stream routines conflict with those in
26615 @code{msvcrt.dll}.
26616 @end itemize
26617
26618 @noindent
26619 If you do want to use the Microsoft tools for your non-Ada code and hit one
26620 of the above limitations, you have two choices:
26621
26622 @enumerate
26623 @item
26624 Encapsulate your non Ada code in a DLL to be linked with your Ada
26625 application. In this case, use the Microsoft or whatever environment to
26626 build the DLL and use GNAT to build your executable
26627 (@pxref{Using DLLs with GNAT}).
26628
26629 @item
26630 Or you can encapsulate your Ada code in a DLL to be linked with the
26631 other part of your application. In this case, use GNAT to build the DLL
26632 (@pxref{Building DLLs with GNAT}) and use the Microsoft or whatever
26633 environment to build your executable.
26634 @end enumerate
26635
26636 @node Windows Calling Conventions
26637 @section Windows Calling Conventions
26638 @findex Stdcall
26639 @findex APIENTRY
26640
26641 @menu
26642 * C Calling Convention::
26643 * Stdcall Calling Convention::
26644 * Win32 Calling Convention::
26645 * DLL Calling Convention::
26646 @end menu
26647
26648 @noindent
26649 When a subprogram @code{F} (caller) calls a subprogram @code{G}
26650 (callee), there are several ways to push @code{G}'s parameters on the
26651 stack and there are several possible scenarios to clean up the stack
26652 upon @code{G}'s return. A calling convention is an agreed upon software
26653 protocol whereby the responsibilities between the caller (@code{F}) and
26654 the callee (@code{G}) are clearly defined. Several calling conventions
26655 are available for Windows:
26656
26657 @itemize @bullet
26658 @item
26659 @code{C} (Microsoft defined)
26660
26661 @item
26662 @code{Stdcall} (Microsoft defined)
26663
26664 @item
26665 @code{Win32} (GNAT specific)
26666
26667 @item
26668 @code{DLL} (GNAT specific)
26669 @end itemize
26670
26671 @node C Calling Convention
26672 @subsection @code{C} Calling Convention
26673
26674 @noindent
26675 This is the default calling convention used when interfacing to C/C++
26676 routines compiled with either @command{gcc} or Microsoft Visual C++.
26677
26678 In the @code{C} calling convention subprogram parameters are pushed on the
26679 stack by the caller from right to left. The caller itself is in charge of
26680 cleaning up the stack after the call. In addition, the name of a routine
26681 with @code{C} calling convention is mangled by adding a leading underscore.
26682
26683 The name to use on the Ada side when importing (or exporting) a routine
26684 with @code{C} calling convention is the name of the routine. For
26685 instance the C function:
26686
26687 @smallexample
26688 int get_val (long);
26689 @end smallexample
26690
26691 @noindent
26692 should be imported from Ada as follows:
26693
26694 @smallexample @c ada
26695 @group
26696 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
26697 pragma Import (C, Get_Val, External_Name => "get_val");
26698 @end group
26699 @end smallexample
26700
26701 @noindent
26702 Note that in this particular case the @code{External_Name} parameter could
26703 have been omitted since, when missing, this parameter is taken to be the
26704 name of the Ada entity in lower case. When the @code{Link_Name} parameter
26705 is missing, as in the above example, this parameter is set to be the
26706 @code{External_Name} with a leading underscore.
26707
26708 When importing a variable defined in C, you should always use the @code{C}
26709 calling convention unless the object containing the variable is part of a
26710 DLL (in which case you should use the @code{Stdcall} calling
26711 convention, @pxref{Stdcall Calling Convention}).
26712
26713 @node Stdcall Calling Convention
26714 @subsection @code{Stdcall} Calling Convention
26715
26716 @noindent
26717 This convention, which was the calling convention used for Pascal
26718 programs, is used by Microsoft for all the routines in the Win32 API for
26719 efficiency reasons. It must be used to import any routine for which this
26720 convention was specified.
26721
26722 In the @code{Stdcall} calling convention subprogram parameters are pushed
26723 on the stack by the caller from right to left. The callee (and not the
26724 caller) is in charge of cleaning the stack on routine exit. In addition,
26725 the name of a routine with @code{Stdcall} calling convention is mangled by
26726 adding a leading underscore (as for the @code{C} calling convention) and a
26727 trailing @code{@@}@code{@i{nn}}, where @i{nn} is the overall size (in
26728 bytes) of the parameters passed to the routine.
26729
26730 The name to use on the Ada side when importing a C routine with a
26731 @code{Stdcall} calling convention is the name of the C routine. The leading
26732 underscore and trailing @code{@@}@code{@i{nn}} are added automatically by
26733 the compiler. For instance the Win32 function:
26734
26735 @smallexample
26736 @b{APIENTRY} int get_val (long);
26737 @end smallexample
26738
26739 @noindent
26740 should be imported from Ada as follows:
26741
26742 @smallexample @c ada
26743 @group
26744 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
26745 pragma Import (Stdcall, Get_Val);
26746 --  On the x86 a long is 4 bytes, so the Link_Name is "_get_val@@4"
26747 @end group
26748 @end smallexample
26749
26750 @noindent
26751 As for the @code{C} calling convention, when the @code{External_Name}
26752 parameter is missing, it is taken to be the name of the Ada entity in lower
26753 case. If instead of writing the above import pragma you write:
26754
26755 @smallexample @c ada
26756 @group
26757 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
26758 pragma Import (Stdcall, Get_Val, External_Name => "retrieve_val");
26759 @end group
26760 @end smallexample
26761
26762 @noindent
26763 then the imported routine is @code{_retrieve_val@@4}. However, if instead
26764 of specifying the @code{External_Name} parameter you specify the
26765 @code{Link_Name} as in the following example:
26766
26767 @smallexample @c ada
26768 @group
26769 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
26770 pragma Import (Stdcall, Get_Val, Link_Name => "retrieve_val");
26771 @end group
26772 @end smallexample
26773
26774 @noindent
26775 then the imported routine is @code{retrieve_val@@4}, that is, there is no
26776 trailing underscore but the appropriate @code{@@}@code{@i{nn}} is always
26777 added at the end of the @code{Link_Name} by the compiler.
26778
26779 @noindent
26780 Note, that in some special cases a DLL's entry point name lacks a trailing
26781 @code{@@}@code{@i{nn}} while the exported name generated for a call has it.
26782 The @code{gnatdll} tool, which creates the import library for the DLL, is able
26783 to handle those cases (@pxref{Using gnatdll} for the description of
26784 the switches).
26785
26786 @noindent
26787 It is also possible to import variables defined in a DLL by using an
26788 import pragma for a variable. As an example, if a DLL contains a
26789 variable defined as:
26790
26791 @smallexample
26792 int my_var;
26793 @end smallexample
26794
26795 @noindent
26796 then, to access this variable from Ada you should write:
26797
26798 @smallexample @c ada
26799 @group
26800 My_Var : Interfaces.C.int;
26801 pragma Import (Stdcall, My_Var);
26802 @end group
26803 @end smallexample
26804
26805 @noindent
26806 Note that to ease building cross-platform bindings this convention
26807 will be handled as a @code{C} calling convention on non Windows platforms.
26808
26809 @node Win32 Calling Convention
26810 @subsection @code{Win32} Calling Convention
26811
26812 @noindent
26813 This convention, which is GNAT-specific is fully equivalent to the
26814 @code{Stdcall} calling convention described above.
26815
26816 @node DLL Calling Convention
26817 @subsection @code{DLL} Calling Convention
26818
26819 @noindent
26820 This convention, which is GNAT-specific is fully equivalent to the
26821 @code{Stdcall} calling convention described above.
26822
26823 @node Introduction to Dynamic Link Libraries (DLLs)
26824 @section Introduction to Dynamic Link Libraries (DLLs)
26825 @findex DLL
26826
26827 @noindent
26828 A Dynamically Linked Library (DLL) is a library that can be shared by
26829 several applications running under Windows. A DLL can contain any number of
26830 routines and variables.
26831
26832 One advantage of DLLs is that you can change and enhance them without
26833 forcing all the applications that depend on them to be relinked or
26834 recompiled. However, you should be aware than all calls to DLL routines are
26835 slower since, as you will understand below, such calls are indirect.
26836
26837 To illustrate the remainder of this section, suppose that an application
26838 wants to use the services of a DLL @file{API.dll}. To use the services
26839 provided by @file{API.dll} you must statically link against the DLL or
26840 an import library which contains a jump table with an entry for each
26841 routine and variable exported by the DLL. In the Microsoft world this
26842 import library is called @file{API.lib}. When using GNAT this import
26843 library is called either @file{libAPI.a} or @file{libapi.a} (names are
26844 case insensitive).
26845
26846 After you have linked your application with the DLL or the import library
26847 and you run your application, here is what happens:
26848
26849 @enumerate
26850 @item
26851 Your application is loaded into memory.
26852
26853 @item
26854 The DLL @file{API.dll} is mapped into the address space of your
26855 application. This means that:
26856
26857 @itemize @bullet
26858 @item
26859 The DLL will use the stack of the calling thread.
26860
26861 @item
26862 The DLL will use the virtual address space of the calling process.
26863
26864 @item
26865 The DLL will allocate memory from the virtual address space of the calling
26866 process.
26867
26868 @item
26869 Handles (pointers) can be safely exchanged between routines in the DLL
26870 routines and routines in the application using the DLL.
26871 @end itemize
26872
26873 @item
26874 The entries in the jump table (from the import library @file{libAPI.a}
26875 or @file{API.lib} or automatically created when linking against a DLL)
26876 which is part of your application are initialized with the addresses
26877 of the routines and variables in @file{API.dll}.
26878
26879 @item
26880 If present in @file{API.dll}, routines @code{DllMain} or
26881 @code{DllMainCRTStartup} are invoked. These routines typically contain
26882 the initialization code needed for the well-being of the routines and
26883 variables exported by the DLL.
26884 @end enumerate
26885
26886 @noindent
26887 There is an additional point which is worth mentioning. In the Windows
26888 world there are two kind of DLLs: relocatable and non-relocatable
26889 DLLs. Non-relocatable DLLs can only be loaded at a very specific address
26890 in the target application address space. If the addresses of two
26891 non-relocatable DLLs overlap and these happen to be used by the same
26892 application, a conflict will occur and the application will run
26893 incorrectly. Hence, when possible, it is always preferable to use and
26894 build relocatable DLLs. Both relocatable and non-relocatable DLLs are
26895 supported by GNAT. Note that the @option{-s} linker option (see GNU Linker
26896 User's Guide) removes the debugging symbols from the DLL but the DLL can
26897 still be relocated.
26898
26899 As a side note, an interesting difference between Microsoft DLLs and
26900 Unix shared libraries, is the fact that on most Unix systems all public
26901 routines are exported by default in a Unix shared library, while under
26902 Windows it is possible (but not required) to list exported routines in
26903 a definition file (@pxref{The Definition File}).
26904
26905 @node Using DLLs with GNAT
26906 @section Using DLLs with GNAT
26907
26908 @menu
26909 * Creating an Ada Spec for the DLL Services::
26910 * Creating an Import Library::
26911 @end menu
26912
26913 @noindent
26914 To use the services of a DLL, say @file{API.dll}, in your Ada application
26915 you must have:
26916
26917 @enumerate
26918 @item
26919 The Ada spec for the routines and/or variables you want to access in
26920 @file{API.dll}. If not available this Ada spec must be built from the C/C++
26921 header files provided with the DLL.
26922
26923 @item
26924 The import library (@file{libAPI.a} or @file{API.lib}). As previously
26925 mentioned an import library is a statically linked library containing the
26926 import table which will be filled at load time to point to the actual
26927 @file{API.dll} routines. Sometimes you don't have an import library for the
26928 DLL you want to use. The following sections will explain how to build
26929 one. Note that this is optional.
26930
26931 @item
26932 The actual DLL, @file{API.dll}.
26933 @end enumerate
26934
26935 @noindent
26936 Once you have all the above, to compile an Ada application that uses the
26937 services of @file{API.dll} and whose main subprogram is @code{My_Ada_App},
26938 you simply issue the command
26939
26940 @smallexample
26941 $ gnatmake my_ada_app -largs -lAPI
26942 @end smallexample
26943
26944 @noindent
26945 The argument @option{-largs -lAPI} at the end of the @command{gnatmake} command
26946 tells the GNAT linker to look first for a library named @file{API.lib}
26947 (Microsoft-style name) and if not found for a library named @file{libAPI.a}
26948 (GNAT-style name). Note that if the Ada package spec for @file{API.dll}
26949 contains the following pragma
26950
26951 @smallexample @c ada
26952 pragma Linker_Options ("-lAPI");
26953 @end smallexample
26954
26955 @noindent
26956 you do not have to add @option{-largs -lAPI} at the end of the
26957 @command{gnatmake} command.
26958
26959 If any one of the items above is missing you will have to create it
26960 yourself. The following sections explain how to do so using as an
26961 example a fictitious DLL called @file{API.dll}.
26962
26963 @node Creating an Ada Spec for the DLL Services
26964 @subsection Creating an Ada Spec for the DLL Services
26965
26966 @noindent
26967 A DLL typically comes with a C/C++ header file which provides the
26968 definitions of the routines and variables exported by the DLL. The Ada
26969 equivalent of this header file is a package spec that contains definitions
26970 for the imported entities. If the DLL you intend to use does not come with
26971 an Ada spec you have to generate one such spec yourself. For example if
26972 the header file of @file{API.dll} is a file @file{api.h} containing the
26973 following two definitions:
26974
26975 @smallexample
26976 @group
26977 @cartouche
26978 int some_var;
26979 int get (char *);
26980 @end cartouche
26981 @end group
26982 @end smallexample
26983
26984 @noindent
26985 then the equivalent Ada spec could be:
26986
26987 @smallexample @c ada
26988 @group
26989 @cartouche
26990 with Interfaces.C.Strings;
26991 package API is
26992    use Interfaces;
26993
26994    Some_Var : C.int;
26995    function Get (Str : C.Strings.Chars_Ptr) return C.int;
26996
26997 private
26998    pragma Import (C, Get);
26999    pragma Import (DLL, Some_Var);
27000 end API;
27001 @end cartouche
27002 @end group
27003 @end smallexample
27004
27005 @noindent
27006 Note that a variable is
27007 @strong{always imported with a Stdcall convention}. A function
27008 can have @code{C} or @code{Stdcall} convention.
27009 (@pxref{Windows Calling Conventions}).
27010
27011 @node Creating an Import Library
27012 @subsection Creating an Import Library
27013 @cindex Import library
27014
27015 @menu
27016 * The Definition File::
27017 * GNAT-Style Import Library::
27018 * Microsoft-Style Import Library::
27019 @end menu
27020
27021 @noindent
27022 If a Microsoft-style import library @file{API.lib} or a GNAT-style
27023 import library @file{libAPI.a} is available with @file{API.dll} you
27024 can skip this section. You can also skip this section if
27025 @file{API.dll} is built with GNU tools as in this case it is possible
27026 to link directly against the DLL. Otherwise read on.
27027
27028 @node The Definition File
27029 @subsubsection The Definition File
27030 @cindex Definition file
27031 @findex .def
27032
27033 @noindent
27034 As previously mentioned, and unlike Unix systems, the list of symbols
27035 that are exported from a DLL must be provided explicitly in Windows.
27036 The main goal of a definition file is precisely that: list the symbols
27037 exported by a DLL. A definition file (usually a file with a @code{.def}
27038 suffix) has the following structure:
27039
27040 @smallexample
27041 @group
27042 @cartouche
27043 [LIBRARY @i{name}]
27044 [DESCRIPTION @i{string}]
27045 EXPORTS
27046    @i{symbol1}
27047    @i{symbol2}
27048    ...
27049 @end cartouche
27050 @end group
27051 @end smallexample
27052
27053 @table @code
27054 @item LIBRARY @i{name}
27055 This section, which is optional, gives the name of the DLL.
27056
27057 @item DESCRIPTION @i{string}
27058 This section, which is optional, gives a description string that will be
27059 embedded in the import library.
27060
27061 @item EXPORTS
27062 This section gives the list of exported symbols (procedures, functions or
27063 variables). For instance in the case of @file{API.dll} the @code{EXPORTS}
27064 section of @file{API.def} looks like:
27065
27066 @smallexample
27067 @group
27068 @cartouche
27069 EXPORTS
27070    some_var
27071    get
27072 @end cartouche
27073 @end group
27074 @end smallexample
27075 @end table
27076
27077 @noindent
27078 Note that you must specify the correct suffix (@code{@@}@code{@i{nn}})
27079 (@pxref{Windows Calling Conventions}) for a Stdcall
27080 calling convention function in the exported symbols list.
27081
27082 @noindent
27083 There can actually be other sections in a definition file, but these
27084 sections are not relevant to the discussion at hand.
27085
27086 @node GNAT-Style Import Library
27087 @subsubsection GNAT-Style Import Library
27088
27089 @noindent
27090 To create a static import library from @file{API.dll} with the GNAT tools
27091 you should proceed as follows:
27092
27093 @enumerate
27094 @item
27095 Create the definition file @file{API.def} (@pxref{The Definition File}).
27096 For that use the @code{dll2def} tool as follows:
27097
27098 @smallexample
27099 $ dll2def API.dll > API.def
27100 @end smallexample
27101
27102 @noindent
27103 @code{dll2def} is a very simple tool: it takes as input a DLL and prints
27104 to standard output the list of entry points in the DLL. Note that if
27105 some routines in the DLL have the @code{Stdcall} convention
27106 (@pxref{Windows Calling Conventions}) with stripped @code{@@}@i{nn}
27107 suffix then you'll have to edit @file{api.def} to add it, and specify
27108 @code{-k} to @code{gnatdll} when creating the import library.
27109
27110 @noindent
27111 Here are some hints to find the right @code{@@}@i{nn} suffix.
27112
27113 @enumerate
27114 @item
27115 If you have the Microsoft import library (.lib), it is possible to get
27116 the right symbols by using Microsoft @code{dumpbin} tool (see the
27117 corresponding Microsoft documentation for further details).
27118
27119 @smallexample
27120 $ dumpbin /exports api.lib
27121 @end smallexample
27122
27123 @item
27124 If you have a message about a missing symbol at link time the compiler
27125 tells you what symbol is expected. You just have to go back to the
27126 definition file and add the right suffix.
27127 @end enumerate
27128
27129 @item
27130 Build the import library @code{libAPI.a}, using @code{gnatdll}
27131 (@pxref{Using gnatdll}) as follows:
27132
27133 @smallexample
27134 $ gnatdll -e API.def -d API.dll
27135 @end smallexample
27136
27137 @noindent
27138 @code{gnatdll} takes as input a definition file @file{API.def} and the
27139 name of the DLL containing the services listed in the definition file
27140 @file{API.dll}. The name of the static import library generated is
27141 computed from the name of the definition file as follows: if the
27142 definition file name is @i{xyz}@code{.def}, the import library name will
27143 be @code{lib}@i{xyz}@code{.a}. Note that in the previous example option
27144 @option{-e} could have been removed because the name of the definition
27145 file (before the ``@code{.def}'' suffix) is the same as the name of the
27146 DLL (@pxref{Using gnatdll} for more information about @code{gnatdll}).
27147 @end enumerate
27148
27149 @node Microsoft-Style Import Library
27150 @subsubsection Microsoft-Style Import Library
27151
27152 @noindent
27153 With GNAT you can either use a GNAT-style or Microsoft-style import
27154 library. A Microsoft import library is needed only if you plan to make an
27155 Ada DLL available to applications developed with Microsoft
27156 tools (@pxref{Mixed-Language Programming on Windows}).
27157
27158 To create a Microsoft-style import library for @file{API.dll} you
27159 should proceed as follows:
27160
27161 @enumerate
27162 @item
27163 Create the definition file @file{API.def} from the DLL. For this use either
27164 the @code{dll2def} tool as described above or the Microsoft @code{dumpbin}
27165 tool (see the corresponding Microsoft documentation for further details).
27166
27167 @item
27168 Build the actual import library using Microsoft's @code{lib} utility:
27169
27170 @smallexample
27171 $ lib -machine:IX86 -def:API.def -out:API.lib
27172 @end smallexample
27173
27174 @noindent
27175 If you use the above command the definition file @file{API.def} must
27176 contain a line giving the name of the DLL:
27177
27178 @smallexample
27179 LIBRARY      "API"
27180 @end smallexample
27181
27182 @noindent
27183 See the Microsoft documentation for further details about the usage of
27184 @code{lib}.
27185 @end enumerate
27186
27187 @node Building DLLs with GNAT
27188 @section Building DLLs with GNAT
27189 @cindex DLLs, building
27190
27191 @noindent
27192 This section explain how to build DLLs using the GNAT built-in DLL
27193 support. With the following procedure it is straight forward to build
27194 and use DLLs with GNAT.
27195
27196 @enumerate
27197
27198 @item building object files
27199
27200 The first step is to build all objects files that are to be included
27201 into the DLL. This is done by using the standard @command{gnatmake} tool.
27202
27203 @item building the DLL
27204
27205 To build the DLL you must use @command{gcc}'s @code{-shared}
27206 option. It is quite simple to use this method:
27207
27208 @smallexample
27209 $ gcc -shared -o api.dll obj1.o obj2.o ...
27210 @end smallexample
27211
27212 It is important to note that in this case all symbols found in the
27213 object files are automatically exported. It is possible to restrict
27214 the set of symbols to export by passing to @command{gcc} a definition
27215 file, @pxref{The Definition File}. For example:
27216
27217 @smallexample
27218 $ gcc -shared -o api.dll api.def obj1.o obj2.o ...
27219 @end smallexample
27220
27221 If you use a definition file you must export the elaboration procedures
27222 for every package that required one. Elaboration procedures are named
27223 using the package name followed by "_E".
27224
27225 @item preparing DLL to be used
27226
27227 For the DLL to be used by client programs the bodies must be hidden
27228 from it and the .ali set with read-only attribute. This is very important
27229 otherwise GNAT will recompile all packages and will not actually use
27230 the code in the DLL. For example:
27231
27232 @smallexample
27233 $ mkdir apilib
27234 $ copy *.ads *.ali api.dll apilib
27235 $ attrib +R apilib\*.ali
27236 @end smallexample
27237
27238 @end enumerate
27239
27240 At this point it is possible to use the DLL by directly linking
27241 against it. Note that you must use the GNAT shared runtime when using
27242 GNAT shared libraries. This is achieved by using @code{-shared} binder's
27243 option.
27244
27245 @smallexample
27246 $ gnatmake main -Iapilib -bargs -shared -largs -Lapilib -lAPI
27247 @end smallexample
27248
27249 @node Building DLLs with GNAT Project files
27250 @section Building DLLs with GNAT Project files
27251 @cindex DLLs, building
27252
27253 @noindent
27254 There is nothing specific to Windows in this area. @pxref{Library Projects}.
27255
27256 @node Building DLLs with gnatdll
27257 @section Building DLLs with gnatdll
27258 @cindex DLLs, building
27259
27260 @menu
27261 * Limitations When Using Ada DLLs from Ada::
27262 * Exporting Ada Entities::
27263 * Ada DLLs and Elaboration::
27264 * Ada DLLs and Finalization::
27265 * Creating a Spec for Ada DLLs::
27266 * Creating the Definition File::
27267 * Using gnatdll::
27268 @end menu
27269
27270 @noindent
27271 Note that it is preferred to use the built-in GNAT DLL support
27272 (@pxref{Building DLLs with GNAT}) or GNAT Project files
27273 (@pxref{Building DLLs with GNAT Project files}) to build DLLs.
27274
27275 This section explains how to build DLLs containing Ada code using
27276 @code{gnatdll}. These DLLs will be referred to as Ada DLLs in the
27277 remainder of this section.
27278
27279 The steps required to build an Ada DLL that is to be used by Ada as well as
27280 non-Ada applications are as follows:
27281
27282 @enumerate
27283 @item
27284 You need to mark each Ada @i{entity} exported by the DLL with a @code{C} or
27285 @code{Stdcall} calling convention to avoid any Ada name mangling for the
27286 entities exported by the DLL (@pxref{Exporting Ada Entities}). You can
27287 skip this step if you plan to use the Ada DLL only from Ada applications.
27288
27289 @item
27290 Your Ada code must export an initialization routine which calls the routine
27291 @code{adainit} generated by @command{gnatbind} to perform the elaboration of
27292 the Ada code in the DLL (@pxref{Ada DLLs and Elaboration}). The initialization
27293 routine exported by the Ada DLL must be invoked by the clients of the DLL
27294 to initialize the DLL.
27295
27296 @item
27297 When useful, the DLL should also export a finalization routine which calls
27298 routine @code{adafinal} generated by @command{gnatbind} to perform the
27299 finalization of the Ada code in the DLL (@pxref{Ada DLLs and Finalization}).
27300 The finalization routine exported by the Ada DLL must be invoked by the
27301 clients of the DLL when the DLL services are no further needed.
27302
27303 @item
27304 You must provide a spec for the services exported by the Ada DLL in each
27305 of the programming languages to which you plan to make the DLL available.
27306
27307 @item
27308 You must provide a definition file listing the exported entities
27309 (@pxref{The Definition File}).
27310
27311 @item
27312 Finally you must use @code{gnatdll} to produce the DLL and the import
27313 library (@pxref{Using gnatdll}).
27314 @end enumerate
27315
27316 @noindent
27317 Note that a relocatable DLL stripped using the @code{strip}
27318 binutils tool will not be relocatable anymore. To build a DLL without
27319 debug information pass @code{-largs -s} to @code{gnatdll}. This
27320 restriction does not apply to a DLL built using a Library Project.
27321 @pxref{Library Projects}.
27322
27323 @node Limitations When Using Ada DLLs from Ada
27324 @subsection Limitations When Using Ada DLLs from Ada
27325
27326 @noindent
27327 When using Ada DLLs from Ada applications there is a limitation users
27328 should be aware of. Because on Windows the GNAT run time is not in a DLL of
27329 its own, each Ada DLL includes a part of the GNAT run time. Specifically,
27330 each Ada DLL includes the services of the GNAT run time that are necessary
27331 to the Ada code inside the DLL. As a result, when an Ada program uses an
27332 Ada DLL there are two independent GNAT run times: one in the Ada DLL and
27333 one in the main program.
27334
27335 It is therefore not possible to exchange GNAT run-time objects between the
27336 Ada DLL and the main Ada program. Example of GNAT run-time objects are file
27337 handles (e.g. @code{Text_IO.File_Type}), tasks types, protected objects
27338 types, etc.
27339
27340 It is completely safe to exchange plain elementary, array or record types,
27341 Windows object handles, etc.
27342
27343 @node Exporting Ada Entities
27344 @subsection Exporting Ada Entities
27345 @cindex Export table
27346
27347 @noindent
27348 Building a DLL is a way to encapsulate a set of services usable from any
27349 application. As a result, the Ada entities exported by a DLL should be
27350 exported with the @code{C} or @code{Stdcall} calling conventions to avoid
27351 any Ada name mangling. As an example here is an Ada package
27352 @code{API}, spec and body, exporting two procedures, a function, and a
27353 variable:
27354
27355 @smallexample @c ada
27356 @group
27357 @cartouche
27358 with Interfaces.C; use Interfaces;
27359 package API is
27360    Count : C.int := 0;
27361    function Factorial (Val : C.int) return C.int;
27362
27363    procedure Initialize_API;
27364    procedure Finalize_API;
27365    --  Initialization & Finalization routines. More in the next section.
27366 private
27367    pragma Export (C, Initialize_API);
27368    pragma Export (C, Finalize_API);
27369    pragma Export (C, Count);
27370    pragma Export (C, Factorial);
27371 end API;
27372 @end cartouche
27373 @end group
27374 @end smallexample
27375
27376 @smallexample @c ada
27377 @group
27378 @cartouche
27379 package body API is
27380    function Factorial (Val : C.int) return C.int is
27381       Fact : C.int := 1;
27382    begin
27383       Count := Count + 1;
27384       for K in 1 .. Val loop
27385          Fact := Fact * K;
27386       end loop;
27387       return Fact;
27388    end Factorial;
27389
27390    procedure Initialize_API is
27391       procedure Adainit;
27392       pragma Import (C, Adainit);
27393    begin
27394       Adainit;
27395    end Initialize_API;
27396
27397    procedure Finalize_API is
27398       procedure Adafinal;
27399       pragma Import (C, Adafinal);
27400    begin
27401       Adafinal;
27402    end Finalize_API;
27403 end API;
27404 @end cartouche
27405 @end group
27406 @end smallexample
27407
27408 @noindent
27409 If the Ada DLL you are building will only be used by Ada applications
27410 you do not have to export Ada entities with a @code{C} or @code{Stdcall}
27411 convention. As an example, the previous package could be written as
27412 follows:
27413
27414 @smallexample @c ada
27415 @group
27416 @cartouche
27417 package API is
27418    Count : Integer := 0;
27419    function Factorial (Val : Integer) return Integer;
27420
27421    procedure Initialize_API;
27422    procedure Finalize_API;
27423    --  Initialization and Finalization routines.
27424 end API;
27425 @end cartouche
27426 @end group
27427 @end smallexample
27428
27429 @smallexample @c ada
27430 @group
27431 @cartouche
27432 package body API is
27433    function Factorial (Val : Integer) return Integer is
27434       Fact : Integer := 1;
27435    begin
27436       Count := Count + 1;
27437       for K in 1 .. Val loop
27438          Fact := Fact * K;
27439       end loop;
27440       return Fact;
27441    end Factorial;
27442
27443    ...
27444    --  The remainder of this package body is unchanged.
27445 end API;
27446 @end cartouche
27447 @end group
27448 @end smallexample
27449
27450 @noindent
27451 Note that if you do not export the Ada entities with a @code{C} or
27452 @code{Stdcall} convention you will have to provide the mangled Ada names
27453 in the definition file of the Ada DLL
27454 (@pxref{Creating the Definition File}).
27455
27456 @node Ada DLLs and Elaboration
27457 @subsection Ada DLLs and Elaboration
27458 @cindex DLLs and elaboration
27459
27460 @noindent
27461 The DLL that you are building contains your Ada code as well as all the
27462 routines in the Ada library that are needed by it. The first thing a
27463 user of your DLL must do is elaborate the Ada code
27464 (@pxref{Elaboration Order Handling in GNAT}).
27465
27466 To achieve this you must export an initialization routine
27467 (@code{Initialize_API} in the previous example), which must be invoked
27468 before using any of the DLL services. This elaboration routine must call
27469 the Ada elaboration routine @code{adainit} generated by the GNAT binder
27470 (@pxref{Binding with Non-Ada Main Programs}). See the body of
27471 @code{Initialize_Api} for an example. Note that the GNAT binder is
27472 automatically invoked during the DLL build process by the @code{gnatdll}
27473 tool (@pxref{Using gnatdll}).
27474
27475 When a DLL is loaded, Windows systematically invokes a routine called
27476 @code{DllMain}. It would therefore be possible to call @code{adainit}
27477 directly from @code{DllMain} without having to provide an explicit
27478 initialization routine. Unfortunately, it is not possible to call
27479 @code{adainit} from the @code{DllMain} if your program has library level
27480 tasks because access to the @code{DllMain} entry point is serialized by
27481 the system (that is, only a single thread can execute ``through'' it at a
27482 time), which means that the GNAT run time will deadlock waiting for the
27483 newly created task to complete its initialization.
27484
27485 @node Ada DLLs and Finalization
27486 @subsection Ada DLLs and Finalization
27487 @cindex DLLs and finalization
27488
27489 @noindent
27490 When the services of an Ada DLL are no longer needed, the client code should
27491 invoke the DLL finalization routine, if available. The DLL finalization
27492 routine is in charge of releasing all resources acquired by the DLL. In the
27493 case of the Ada code contained in the DLL, this is achieved by calling
27494 routine @code{adafinal} generated by the GNAT binder
27495 (@pxref{Binding with Non-Ada Main Programs}).
27496 See the body of @code{Finalize_Api} for an
27497 example. As already pointed out the GNAT binder is automatically invoked
27498 during the DLL build process by the @code{gnatdll} tool
27499 (@pxref{Using gnatdll}).
27500
27501 @node Creating a Spec for Ada DLLs
27502 @subsection Creating a Spec for Ada DLLs
27503
27504 @noindent
27505 To use the services exported by the Ada DLL from another programming
27506 language (e.g. C), you have to translate the specs of the exported Ada
27507 entities in that language. For instance in the case of @code{API.dll},
27508 the corresponding C header file could look like:
27509
27510 @smallexample
27511 @group
27512 @cartouche
27513 extern int *_imp__count;
27514 #define count (*_imp__count)
27515 int factorial (int);
27516 @end cartouche
27517 @end group
27518 @end smallexample
27519
27520 @noindent
27521 It is important to understand that when building an Ada DLL to be used by
27522 other Ada applications, you need two different specs for the packages
27523 contained in the DLL: one for building the DLL and the other for using
27524 the DLL. This is because the @code{DLL} calling convention is needed to
27525 use a variable defined in a DLL, but when building the DLL, the variable
27526 must have either the @code{Ada} or @code{C} calling convention. As an
27527 example consider a DLL comprising the following package @code{API}:
27528
27529 @smallexample @c ada
27530 @group
27531 @cartouche
27532 package API is
27533    Count : Integer := 0;
27534    ...
27535    --  Remainder of the package omitted.
27536 end API;
27537 @end cartouche
27538 @end group
27539 @end smallexample
27540
27541 @noindent
27542 After producing a DLL containing package @code{API}, the spec that
27543 must be used to import @code{API.Count} from Ada code outside of the
27544 DLL is:
27545
27546 @smallexample @c ada
27547 @group
27548 @cartouche
27549 package API is
27550    Count : Integer;
27551    pragma Import (DLL, Count);
27552 end API;
27553 @end cartouche
27554 @end group
27555 @end smallexample
27556
27557 @node Creating the Definition File
27558 @subsection Creating the Definition File
27559
27560 @noindent
27561 The definition file is the last file needed to build the DLL. It lists
27562 the exported symbols. As an example, the definition file for a DLL
27563 containing only package @code{API} (where all the entities are exported
27564 with a @code{C} calling convention) is:
27565
27566 @smallexample
27567 @group
27568 @cartouche
27569 EXPORTS
27570     count
27571     factorial
27572     finalize_api
27573     initialize_api
27574 @end cartouche
27575 @end group
27576 @end smallexample
27577
27578 @noindent
27579 If the @code{C} calling convention is missing from package @code{API},
27580 then the definition file contains the mangled Ada names of the above
27581 entities, which in this case are:
27582
27583 @smallexample
27584 @group
27585 @cartouche
27586 EXPORTS
27587     api__count
27588     api__factorial
27589     api__finalize_api
27590     api__initialize_api
27591 @end cartouche
27592 @end group
27593 @end smallexample
27594
27595 @node Using gnatdll
27596 @subsection Using @code{gnatdll}
27597 @findex gnatdll
27598
27599 @menu
27600 * gnatdll Example::
27601 * gnatdll behind the Scenes::
27602 * Using dlltool::
27603 @end menu
27604
27605 @noindent
27606 @code{gnatdll} is a tool to automate the DLL build process once all the Ada
27607 and non-Ada sources that make up your DLL have been compiled.
27608 @code{gnatdll} is actually in charge of two distinct tasks: build the
27609 static import library for the DLL and the actual DLL. The form of the
27610 @code{gnatdll} command is
27611
27612 @smallexample
27613 @cartouche
27614 $ gnatdll [@var{switches}] @var{list-of-files} [-largs @var{opts}]
27615 @end cartouche
27616 @end smallexample
27617
27618 @noindent
27619 where @i{list-of-files} is a list of ALI and object files. The object
27620 file list must be the exact list of objects corresponding to the non-Ada
27621 sources whose services are to be included in the DLL. The ALI file list
27622 must be the exact list of ALI files for the corresponding Ada sources
27623 whose services are to be included in the DLL. If @i{list-of-files} is
27624 missing, only the static import library is generated.
27625
27626 @noindent
27627 You may specify any of the following switches to @code{gnatdll}:
27628
27629 @table @code
27630 @item -a[@var{address}]
27631 @cindex @option{-a} (@code{gnatdll})
27632 Build a non-relocatable DLL at @var{address}. If @var{address} is not
27633 specified the default address @var{0x11000000} will be used. By default,
27634 when this switch is missing, @code{gnatdll} builds relocatable DLL. We
27635 advise the reader to build relocatable DLL.
27636
27637 @item -b @var{address}
27638 @cindex @option{-b} (@code{gnatdll})
27639 Set the relocatable DLL base address. By default the address is
27640 @var{0x11000000}.
27641
27642 @item -bargs @var{opts}
27643 @cindex @option{-bargs} (@code{gnatdll})
27644 Binder options. Pass @var{opts} to the binder.
27645
27646 @item -d @var{dllfile}
27647 @cindex @option{-d} (@code{gnatdll})
27648 @var{dllfile} is the name of the DLL. This switch must be present for
27649 @code{gnatdll} to do anything. The name of the generated import library is
27650 obtained algorithmically from @var{dllfile} as shown in the following
27651 example: if @var{dllfile} is @code{xyz.dll}, the import library name is
27652 @code{libxyz.a}. The name of the definition file to use (if not specified
27653 by option @option{-e}) is obtained algorithmically from @var{dllfile}
27654 as shown in the following example:
27655 if @var{dllfile} is @code{xyz.dll}, the definition
27656 file used is @code{xyz.def}.
27657
27658 @item -e @var{deffile}
27659 @cindex @option{-e} (@code{gnatdll})
27660 @var{deffile} is the name of the definition file.
27661
27662 @item -g
27663 @cindex @option{-g} (@code{gnatdll})
27664 Generate debugging information. This information is stored in the object
27665 file and copied from there to the final DLL file by the linker,
27666 where it can be read by the debugger. You must use the
27667 @option{-g} switch if you plan on using the debugger or the symbolic
27668 stack traceback.
27669
27670 @item -h
27671 @cindex @option{-h} (@code{gnatdll})
27672 Help mode. Displays @code{gnatdll} switch usage information.
27673
27674 @item -Idir
27675 @cindex @option{-I} (@code{gnatdll})
27676 Direct @code{gnatdll} to search the @var{dir} directory for source and
27677 object files needed to build the DLL.
27678 (@pxref{Search Paths and the Run-Time Library (RTL)}).
27679
27680 @item -k
27681 @cindex @option{-k} (@code{gnatdll})
27682 Removes the @code{@@}@i{nn} suffix from the import library's exported
27683 names, but keeps them for the link names. You must specify this
27684 option if you want to use a @code{Stdcall} function in a DLL for which
27685 the @code{@@}@i{nn} suffix has been removed. This is the case for most
27686 of the Windows NT DLL for example. This option has no effect when
27687 @option{-n} option is specified.
27688
27689 @item -l @var{file}
27690 @cindex @option{-l} (@code{gnatdll})
27691 The list of ALI and object files used to build the DLL are listed in
27692 @var{file}, instead of being given in the command line. Each line in
27693 @var{file} contains the name of an ALI or object file.
27694
27695 @item -n
27696 @cindex @option{-n} (@code{gnatdll})
27697 No Import. Do not create the import library.
27698
27699 @item -q
27700 @cindex @option{-q} (@code{gnatdll})
27701 Quiet mode. Do not display unnecessary messages.
27702
27703 @item -v
27704 @cindex @option{-v} (@code{gnatdll})
27705 Verbose mode. Display extra information.
27706
27707 @item -largs @var{opts}
27708 @cindex @option{-largs} (@code{gnatdll})
27709 Linker options. Pass @var{opts} to the linker.
27710 @end table
27711
27712 @node gnatdll Example
27713 @subsubsection @code{gnatdll} Example
27714
27715 @noindent
27716 As an example the command to build a relocatable DLL from @file{api.adb}
27717 once @file{api.adb} has been compiled and @file{api.def} created is
27718
27719 @smallexample
27720 $ gnatdll -d api.dll api.ali
27721 @end smallexample
27722
27723 @noindent
27724 The above command creates two files: @file{libapi.a} (the import
27725 library) and @file{api.dll} (the actual DLL). If you want to create
27726 only the DLL, just type:
27727
27728 @smallexample
27729 $ gnatdll -d api.dll -n api.ali
27730 @end smallexample
27731
27732 @noindent
27733 Alternatively if you want to create just the import library, type:
27734
27735 @smallexample
27736 $ gnatdll -d api.dll
27737 @end smallexample
27738
27739 @node gnatdll behind the Scenes
27740 @subsubsection @code{gnatdll} behind the Scenes
27741
27742 @noindent
27743 This section details the steps involved in creating a DLL. @code{gnatdll}
27744 does these steps for you. Unless you are interested in understanding what
27745 goes on behind the scenes, you should skip this section.
27746
27747 We use the previous example of a DLL containing the Ada package @code{API},
27748 to illustrate the steps necessary to build a DLL. The starting point is a
27749 set of objects that will make up the DLL and the corresponding ALI
27750 files. In the case of this example this means that @file{api.o} and
27751 @file{api.ali} are available. To build a relocatable DLL, @code{gnatdll} does
27752 the following:
27753
27754 @enumerate
27755 @item
27756 @code{gnatdll} builds the base file (@file{api.base}). A base file gives
27757 the information necessary to generate relocation information for the
27758 DLL.
27759
27760 @smallexample
27761 @group
27762 $ gnatbind -n api
27763 $ gnatlink api -o api.jnk -mdll -Wl,--base-file,api.base
27764 @end group
27765 @end smallexample
27766
27767 @noindent
27768 In addition to the base file, the @command{gnatlink} command generates an
27769 output file @file{api.jnk} which can be discarded. The @option{-mdll} switch
27770 asks @command{gnatlink} to generate the routines @code{DllMain} and
27771 @code{DllMainCRTStartup} that are called by the Windows loader when the DLL
27772 is loaded into memory.
27773
27774 @item
27775 @code{gnatdll} uses @code{dlltool} (@pxref{Using dlltool}) to build the
27776 export table (@file{api.exp}). The export table contains the relocation
27777 information in a form which can be used during the final link to ensure
27778 that the Windows loader is able to place the DLL anywhere in memory.
27779
27780 @smallexample
27781 @group
27782 $ dlltool --dllname api.dll --def api.def --base-file api.base \
27783           --output-exp api.exp
27784 @end group
27785 @end smallexample
27786
27787 @item
27788 @code{gnatdll} builds the base file using the new export table. Note that
27789 @command{gnatbind} must be called once again since the binder generated file
27790 has been deleted during the previous call to @command{gnatlink}.
27791
27792 @smallexample
27793 @group
27794 $ gnatbind -n api
27795 $ gnatlink api -o api.jnk api.exp -mdll
27796       -Wl,--base-file,api.base
27797 @end group
27798 @end smallexample
27799
27800 @item
27801 @code{gnatdll} builds the new export table using the new base file and
27802 generates the DLL import library @file{libAPI.a}.
27803
27804 @smallexample
27805 @group
27806 $ dlltool --dllname api.dll --def api.def --base-file api.base \
27807           --output-exp api.exp --output-lib libAPI.a
27808 @end group
27809 @end smallexample
27810
27811 @item
27812 Finally @code{gnatdll} builds the relocatable DLL using the final export
27813 table.
27814
27815 @smallexample
27816 @group
27817 $ gnatbind -n api
27818 $ gnatlink api api.exp -o api.dll -mdll
27819 @end group
27820 @end smallexample
27821 @end enumerate
27822
27823 @node Using dlltool
27824 @subsubsection Using @code{dlltool}
27825
27826 @noindent
27827 @code{dlltool} is the low-level tool used by @code{gnatdll} to build
27828 DLLs and static import libraries. This section summarizes the most
27829 common @code{dlltool} switches. The form of the @code{dlltool} command
27830 is
27831
27832 @smallexample
27833 $ dlltool [@var{switches}]
27834 @end smallexample
27835
27836 @noindent
27837 @code{dlltool} switches include:
27838
27839 @table @option
27840 @item --base-file @var{basefile}
27841 @cindex @option{--base-file} (@command{dlltool})
27842 Read the base file @var{basefile} generated by the linker. This switch
27843 is used to create a relocatable DLL.
27844
27845 @item --def @var{deffile}
27846 @cindex @option{--def} (@command{dlltool})
27847 Read the definition file.
27848
27849 @item --dllname @var{name}
27850 @cindex @option{--dllname} (@command{dlltool})
27851 Gives the name of the DLL. This switch is used to embed the name of the
27852 DLL in the static import library generated by @code{dlltool} with switch
27853 @option{--output-lib}.
27854
27855 @item -k
27856 @cindex @option{-k} (@command{dlltool})
27857 Kill @code{@@}@i{nn} from exported names
27858 (@pxref{Windows Calling Conventions}
27859 for a discussion about @code{Stdcall}-style symbols.
27860
27861 @item --help
27862 @cindex @option{--help} (@command{dlltool})
27863 Prints the @code{dlltool} switches with a concise description.
27864
27865 @item --output-exp @var{exportfile}
27866 @cindex @option{--output-exp} (@command{dlltool})
27867 Generate an export file @var{exportfile}. The export file contains the
27868 export table (list of symbols in the DLL) and is used to create the DLL.
27869
27870 @item --output-lib @i{libfile}
27871 @cindex @option{--output-lib} (@command{dlltool})
27872 Generate a static import library @var{libfile}.
27873
27874 @item -v
27875 @cindex @option{-v} (@command{dlltool})
27876 Verbose mode.
27877
27878 @item --as @i{assembler-name}
27879 @cindex @option{--as} (@command{dlltool})
27880 Use @i{assembler-name} as the assembler. The default is @code{as}.
27881 @end table
27882
27883 @node GNAT and Windows Resources
27884 @section GNAT and Windows Resources
27885 @cindex Resources, windows
27886
27887 @menu
27888 * Building Resources::
27889 * Compiling Resources::
27890 * Using Resources::
27891 @end menu
27892
27893 @noindent
27894 Resources are an easy way to add Windows specific objects to your
27895 application. The objects that can be added as resources include:
27896
27897 @itemize @bullet
27898 @item
27899 menus
27900
27901 @item
27902 accelerators
27903
27904 @item
27905 dialog boxes
27906
27907 @item
27908 string tables
27909
27910 @item
27911 bitmaps
27912
27913 @item
27914 cursors
27915
27916 @item
27917 icons
27918
27919 @item
27920 fonts
27921 @end itemize
27922
27923 @noindent
27924 This section explains how to build, compile and use resources.
27925
27926 @node Building Resources
27927 @subsection Building Resources
27928 @cindex Resources, building
27929
27930 @noindent
27931 A resource file is an ASCII file. By convention resource files have an
27932 @file{.rc} extension.
27933 The easiest way to build a resource file is to use Microsoft tools
27934 such as @code{imagedit.exe} to build bitmaps, icons and cursors and
27935 @code{dlgedit.exe} to build dialogs.
27936 It is always possible to build an @file{.rc} file yourself by writing a
27937 resource script.
27938
27939 It is not our objective to explain how to write a resource file. A
27940 complete description of the resource script language can be found in the
27941 Microsoft documentation.
27942
27943 @node Compiling Resources
27944 @subsection Compiling Resources
27945 @findex rc
27946 @findex windres
27947 @cindex Resources, compiling
27948
27949 @noindent
27950 This section describes how to build a GNAT-compatible (COFF) object file
27951 containing the resources. This is done using the Resource Compiler
27952 @code{windres} as follows:
27953
27954 @smallexample
27955 $ windres -i myres.rc -o myres.o
27956 @end smallexample
27957
27958 @noindent
27959 By default @code{windres} will run @command{gcc} to preprocess the @file{.rc}
27960 file. You can specify an alternate preprocessor (usually named
27961 @file{cpp.exe}) using the @code{windres} @option{--preprocessor}
27962 parameter. A list of all possible options may be obtained by entering
27963 the command @code{windres} @option{--help}.
27964
27965 It is also possible to use the Microsoft resource compiler @code{rc.exe}
27966 to produce a @file{.res} file (binary resource file). See the
27967 corresponding Microsoft documentation for further details. In this case
27968 you need to use @code{windres} to translate the @file{.res} file to a
27969 GNAT-compatible object file as follows:
27970
27971 @smallexample
27972 $ windres -i myres.res -o myres.o
27973 @end smallexample
27974
27975 @node Using Resources
27976 @subsection Using Resources
27977 @cindex Resources, using
27978
27979 @noindent
27980 To include the resource file in your program just add the
27981 GNAT-compatible object file for the resource(s) to the linker
27982 arguments. With @command{gnatmake} this is done by using the @option{-largs}
27983 option:
27984
27985 @smallexample
27986 $ gnatmake myprog -largs myres.o
27987 @end smallexample
27988
27989 @node Debugging a DLL
27990 @section Debugging a DLL
27991 @cindex DLL debugging
27992
27993 @menu
27994 * Program and DLL Both Built with GCC/GNAT::
27995 * Program Built with Foreign Tools and DLL Built with GCC/GNAT::
27996 @end menu
27997
27998 @noindent
27999 Debugging a DLL is similar to debugging a standard program. But
28000 we have to deal with two different executable parts: the DLL and the
28001 program that uses it. We have the following four possibilities:
28002
28003 @enumerate 1
28004 @item
28005 The program and the DLL are built with @code{GCC/GNAT}.
28006 @item
28007 The program is built with foreign tools and the DLL is built with
28008 @code{GCC/GNAT}.
28009 @item
28010 The program is built with @code{GCC/GNAT} and the DLL is built with
28011 foreign tools.
28012 @item
28013 @end enumerate
28014
28015 @noindent
28016 In this section we address only cases one and two above.
28017 There is no point in trying to debug
28018 a DLL with @code{GNU/GDB}, if there is no GDB-compatible debugging
28019 information in it. To do so you must use a debugger compatible with the
28020 tools suite used to build the DLL.
28021
28022 @node Program and DLL Both Built with GCC/GNAT
28023 @subsection Program and DLL Both Built with GCC/GNAT
28024
28025 @noindent
28026 This is the simplest case. Both the DLL and the program have @code{GDB}
28027 compatible debugging information. It is then possible to break anywhere in
28028 the process. Let's suppose here that the main procedure is named
28029 @code{ada_main} and that in the DLL there is an entry point named
28030 @code{ada_dll}.
28031
28032 @noindent
28033 The DLL (@pxref{Introduction to Dynamic Link Libraries (DLLs)}) and
28034 program must have been built with the debugging information (see GNAT -g
28035 switch). Here are the step-by-step instructions for debugging it:
28036
28037 @enumerate 1
28038 @item Launch @code{GDB} on the main program.
28039
28040 @smallexample
28041 $ gdb -nw ada_main
28042 @end smallexample
28043
28044 @item Start the program and stop at the beginning of the main procedure
28045
28046 @smallexample
28047 (gdb) start
28048 @end smallexample
28049
28050 @noindent
28051 This step is required to be able to set a breakpoint inside the DLL. As long
28052 as the program is not run, the DLL is not loaded. This has the
28053 consequence that the DLL debugging information is also not loaded, so it is not
28054 possible to set a breakpoint in the DLL.
28055
28056 @item Set a breakpoint inside the DLL
28057
28058 @smallexample
28059 (gdb) break ada_dll
28060 (gdb) cont
28061 @end smallexample
28062
28063 @end enumerate
28064
28065 @noindent
28066 At this stage a breakpoint is set inside the DLL. From there on
28067 you can use the standard approach to debug the whole program
28068 (@pxref{Running and Debugging Ada Programs}).
28069
28070 @ignore
28071 @c This used to work, probably because the DLLs were non-relocatable
28072 @c keep this section around until the problem is sorted out.
28073
28074 To break on the @code{DllMain} routine it is not possible to follow
28075 the procedure above. At the time the program stop on @code{ada_main}
28076 the @code{DllMain} routine as already been called. Either you can use
28077 the procedure below @pxref{Debugging the DLL Directly} or this procedure:
28078
28079 @enumerate 1
28080 @item Launch @code{GDB} on the main program.
28081
28082 @smallexample
28083 $ gdb ada_main
28084 @end smallexample
28085
28086 @item Load DLL symbols
28087
28088 @smallexample
28089 (gdb) add-sym api.dll
28090 @end smallexample
28091
28092 @item Set a breakpoint inside the DLL
28093
28094 @smallexample
28095 (gdb) break ada_dll.adb:45
28096 @end smallexample
28097
28098 Note that at this point it is not possible to break using the routine symbol
28099 directly as the program is not yet running. The solution is to break
28100 on the proper line (break in @file{ada_dll.adb} line 45).
28101
28102 @item Start the program
28103
28104 @smallexample
28105 (gdb) run
28106 @end smallexample
28107
28108 @end enumerate
28109 @end ignore
28110
28111 @node Program Built with Foreign Tools and DLL Built with GCC/GNAT
28112 @subsection Program Built with Foreign Tools and DLL Built with GCC/GNAT
28113
28114 @menu
28115 * Debugging the DLL Directly::
28116 * Attaching to a Running Process::
28117 @end menu
28118
28119 @noindent
28120 In this case things are slightly more complex because it is not possible to
28121 start the main program and then break at the beginning to load the DLL and the
28122 associated DLL debugging information. It is not possible to break at the
28123 beginning of the program because there is no @code{GDB} debugging information,
28124 and therefore there is no direct way of getting initial control. This
28125 section addresses this issue by describing some methods that can be used
28126 to break somewhere in the DLL to debug it.
28127
28128 @noindent
28129 First suppose that the main procedure is named @code{main} (this is for
28130 example some C code built with Microsoft Visual C) and that there is a
28131 DLL named @code{test.dll} containing an Ada entry point named
28132 @code{ada_dll}.
28133
28134 @noindent
28135 The DLL (@pxref{Introduction to Dynamic Link Libraries (DLLs)}) must have
28136 been built with debugging information (see GNAT -g option).
28137
28138 @node Debugging the DLL Directly
28139 @subsubsection Debugging the DLL Directly
28140
28141 @enumerate 1
28142 @item
28143 Find out the executable starting address
28144
28145 @smallexample
28146 $ objdump --file-header main.exe
28147 @end smallexample
28148
28149 The starting address is reported on the last line. For example:
28150
28151 @smallexample
28152 main.exe:     file format pei-i386
28153 architecture: i386, flags 0x0000010a:
28154 EXEC_P, HAS_DEBUG, D_PAGED
28155 start address 0x00401010
28156 @end smallexample
28157
28158 @item
28159 Launch the debugger on the executable.
28160
28161 @smallexample
28162 $ gdb main.exe
28163 @end smallexample
28164
28165 @item
28166 Set a breakpoint at the starting address, and launch the program.
28167
28168 @smallexample
28169 $ (gdb) break *0x00401010
28170 $ (gdb) run
28171 @end smallexample
28172
28173 The program will stop at the given address.
28174
28175 @item
28176 Set a breakpoint on a DLL subroutine.
28177
28178 @smallexample
28179 (gdb) break ada_dll.adb:45
28180 @end smallexample
28181
28182 Or if you want to break using a symbol on the DLL, you need first to
28183 select the Ada language (language used by the DLL).
28184
28185 @smallexample
28186 (gdb) set language ada
28187 (gdb) break ada_dll
28188 @end smallexample
28189
28190 @item
28191 Continue the program.
28192
28193 @smallexample
28194 (gdb) cont
28195 @end smallexample
28196
28197 @noindent
28198 This will run the program until it reaches the breakpoint that has been
28199 set. From that point you can use the standard way to debug a program
28200 as described in (@pxref{Running and Debugging Ada Programs}).
28201
28202 @end enumerate
28203
28204 @noindent
28205 It is also possible to debug the DLL by attaching to a running process.
28206
28207 @node Attaching to a Running Process
28208 @subsubsection Attaching to a Running Process
28209 @cindex DLL debugging, attach to process
28210
28211 @noindent
28212 With @code{GDB} it is always possible to debug a running process by
28213 attaching to it. It is possible to debug a DLL this way. The limitation
28214 of this approach is that the DLL must run long enough to perform the
28215 attach operation. It may be useful for instance to insert a time wasting
28216 loop in the code of the DLL to meet this criterion.
28217
28218 @enumerate 1
28219
28220 @item Launch the main program @file{main.exe}.
28221
28222 @smallexample
28223 $ main
28224 @end smallexample
28225
28226 @item Use the Windows @i{Task Manager} to find the process ID. Let's say
28227 that the process PID for @file{main.exe} is 208.
28228
28229 @item Launch gdb.
28230
28231 @smallexample
28232 $ gdb
28233 @end smallexample
28234
28235 @item Attach to the running process to be debugged.
28236
28237 @smallexample
28238 (gdb) attach 208
28239 @end smallexample
28240
28241 @item Load the process debugging information.
28242
28243 @smallexample
28244 (gdb) symbol-file main.exe
28245 @end smallexample
28246
28247 @item Break somewhere in the DLL.
28248
28249 @smallexample
28250 (gdb) break ada_dll
28251 @end smallexample
28252
28253 @item Continue process execution.
28254
28255 @smallexample
28256 (gdb) cont
28257 @end smallexample
28258
28259 @end enumerate
28260
28261 @noindent
28262 This last step will resume the process execution, and stop at
28263 the breakpoint we have set. From there you can use the standard
28264 approach to debug a program as described in
28265 (@pxref{Running and Debugging Ada Programs}).
28266
28267 @node GNAT and COM/DCOM Objects
28268 @section GNAT and COM/DCOM Objects
28269 @findex COM
28270 @findex DCOM
28271
28272 @noindent
28273 This section is temporarily left blank.
28274
28275 @end ifset
28276
28277 @c **********************************
28278 @c * GNU Free Documentation License *
28279 @c **********************************
28280 @include fdl.texi
28281 @c GNU Free Documentation License
28282
28283 @node Index,,GNU Free Documentation License, Top
28284 @unnumbered Index
28285
28286 @printindex cp
28287
28288 @contents
28289 @c Put table of contents at end, otherwise it precedes the "title page" in
28290 @c the .txt version
28291 @c Edit the pdf file to move the contents to the beginning, after the title
28292 @c page
28293
28294 @bye