1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- This package contains routines for accessing and outputting the library
33 -- information. It contains the routine to load subsidiary units.
36 with Namet; use Namet;
38 with Types; use Types;
42 type Compiler_State_Type is (Parsing, Analyzing);
43 Compiler_State : Compiler_State_Type;
44 -- Indicates current state of compilation. This is used to implement the
45 -- function In_Extended_Main_Source_Unit.
47 Parsing_Main_Extended_Source : Boolean := False;
48 -- Set True if we are currently parsing a file that is part of the main
49 -- extended source (the main unit, its spec, or one of its subunits). This
50 -- flag to implement In_Extended_Main_Source_Unit.
52 Analysing_Subunit_Of_Main : Boolean := False;
53 -- Set to True when analyzing a subunit of the main source. When True, if
54 -- the subunit is preprocessed and -gnateG is specified, then the
55 -- preprocessed file (.prep) is written.
57 --------------------------------------------
58 -- General Approach to Library Management --
59 --------------------------------------------
61 -- As described in GNote #1, when a unit is compiled, all its subsidiary
62 -- units are recompiled, including the following:
64 -- (a) Corresponding spec for a body
65 -- (b) Parent spec of a child library spec
67 -- (d) Parent body of a subunit
68 -- (e) Subunits corresponding to any specified stubs
69 -- (f) Bodies of inlined subprograms that are called
70 -- (g) Bodies of generic subprograms or packages that are instantiated
71 -- (h) Bodies of packages containing either of the above two items
72 -- (i) Specs and bodies of runtime units
73 -- (j) Parent specs for with'ed child library units
75 -- If a unit is being compiled only for syntax checking, then no subsidiary
76 -- units are loaded, the syntax check applies only to the main unit,
77 -- i.e. the one contained in the source submitted to the library.
79 -- If a unit is being compiled for syntax and semantic checking, then only
80 -- cases (a)-(d) loads are performed, since the full semantic checking can
81 -- be carried out without needing (e)-(i) loads. In this case no object
82 -- file, or library information file, is generated, so the missing units
83 -- do not affect the results.
85 -- Specifications of library subprograms, subunits, and generic specs
86 -- and bodies, can only be compiled in syntax/semantic checking mode,
87 -- since no code is ever generated directly for these units. In the case
88 -- of subunits, only the compilation of the ultimate parent unit generates
89 -- actual code. If a subunit is submitted to the compiler in syntax/
90 -- semantic checking mode, the parent (or parents in the nested case) are
91 -- semantically checked only up to the point of the corresponding stub.
93 -- If code is being generated, then all the above units are required,
94 -- although the need for bodies of inlined procedures can be suppressed
95 -- by the use of a switch that sets the mode to ignore pragma Inline
98 -- The two main sections of the front end, Par and Sem, are recursive.
99 -- Compilation proceeds unit by unit making recursive calls as necessary.
100 -- The process is controlled from the GNAT main program, which makes calls
101 -- to Par and Sem sequence for the main unit.
103 -- Par parses the given unit, and then, after the parse is complete, uses
104 -- the Par.Load subprogram to load all its subsidiary units in categories
105 -- (a)-(d) above, installing pointers to the loaded units in the parse
106 -- tree, as described in a later section of this spec. If any of these
107 -- required units is missing, a fatal error is signalled, so that no
108 -- attempt is made to run Sem in such cases, since it is assumed that
109 -- too many cascaded errors would result, and the confusion would not
112 -- Following the call to Par on the main unit, the entire tree of required
113 -- units is thus loaded, and Sem is called on the main unit. The parameter
114 -- passed to Sem is the unit to be analyzed. The visibility table, which
115 -- is a single global structure, starts out containing only the entries
116 -- for the visible entities in Standard. Every call to Sem establishes a
117 -- new scope stack table, pushing an entry for Standard on entry to provide
118 -- the proper initial scope environment.
120 -- Sem first proceeds to perform semantic analysis on the currently loaded
123 -- In the case of a body (case (a) above), Sem analyzes the corresponding
124 -- spec, using a recursive call to Sem. As is always expected to be the
125 -- case with calls to Sem, any entities installed in the visibility table
126 -- are removed on exit from Sem, so that these entities have to be
127 -- reinstalled on return to continue the analysis of the body which of
128 -- course needs visibility of these entities.
130 -- In the case of the parent of a child spec (case (b) above), a similar
131 -- call is made to Sem to analyze the parent. Again, on return, the
132 -- entities from the analyzed parent spec have to be installed in the
133 -- visibility table of the caller (the child unit), which must have
134 -- visibility to the entities in its parent spec.
136 -- For with'ed specs (case (c) above), a recursive call to Sem is made
137 -- to analyze each spec in turn. After all the spec's have been analyzed,
138 -- but not till that point, the entities from all the with'ed units are
139 -- reinstalled in the visibility table so that the caller can proceed
140 -- with the analysis of the unit doing the with's with the necessary
141 -- entities made either potentially use visible or visible by selection
144 -- Case (d) arises when Sem is passed a subunit to analyze. This means
145 -- that the main unit is a subunit, and the unit passed to Sem is either
146 -- the main unit, or one of its ancestors that is still a subunit. Since
147 -- analysis must start at the top of the tree, Sem essentially cancels
148 -- the current call by immediately making a call to analyze the parent
149 -- (when this call is finished it immediately returns, so logically this
150 -- call is like a goto). The subunit will then be analyzed at the proper
151 -- time as described for the stub case. Note that we also turn off the
152 -- indication that code should be generated in this case, since the only
153 -- time we generate code for subunits is when compiling the main parent.
155 -- Case (e), subunits corresponding to stubs, are handled as the stubs
156 -- are encountered. There are three sub-cases:
158 -- If the subunit has already been loaded, then this means that the
159 -- main unit was a subunit, and we are back on our way down to it
160 -- after following the initial processing described for case (d).
161 -- In this case we analyze this particular subunit, as described
162 -- for the case where we are generating code, but when we get back
163 -- we are all done, since the rest of the parent is irrelevant. To
164 -- get out of the parent, we raise the exception Subunit_Found, which
165 -- is handled at the outer level of Sem.
167 -- The cases where the subunit has not already been loaded correspond
168 -- to cases where the main unit was a parent. In this case the action
169 -- depends on whether or not we are generating code. If we are not
170 -- generating code, then this is the case where we can simply ignore
171 -- the subunit, since in checking mode we don't even want to insist
172 -- that the subunit exist, much less waste time checking it.
174 -- If we are generating code, then we need to load and analyze
175 -- all subunits. This is achieved with a call to Lib.Load to load
176 -- and parse the unit, followed by processing that installs the
177 -- context clause of the subunit, analyzes the subunit, and then
178 -- removes the context clause (from the visibility chains of the
179 -- parent). Note that we do *not* do a recursive call to Sem in
180 -- this case, precisely because we need to do the analysis of the
181 -- subunit with the current visibility table and scope stack.
183 -- Case (f) applies only to subprograms for which a pragma Inline is
184 -- given, providing that the compiler is operating in the mode where
185 -- pragma Inline's are activated. When the expander encounters a call
186 -- to such a subprogram, it loads the body of the subprogram if it has
187 -- not already been loaded, and calls Sem to process it.
189 -- Case (g) is similar to case (f), except that the body of a generic
190 -- is unconditionally required, regardless of compiler mode settings.
191 -- As in the subprogram case, when the expander encounters a generic
192 -- instantiation, it loads the generic body of the subprogram if it
193 -- has not already been loaded, and calls Sem to process it.
195 -- Case (h) arises when a package contains either an inlined subprogram
196 -- which is called, or a generic which is instantiated. In this case the
197 -- body of the package must be loaded and analyzed with a call to Sem.
199 -- Case (i) is handled by adding implicit with clauses to the context
200 -- clauses of all units that potentially reference the relevant runtime
201 -- entities. Note that since we have the full set of units available,
202 -- the parser can always determine the set of runtime units that is
203 -- needed. These with clauses do not have associated use clauses, so
204 -- all references to the entities must be by selection. Once the with
205 -- clauses have been added, subsequent processing is as for normal
208 -- Case (j) is also handled by adding appropriate implicit with clauses
209 -- to any unit that withs a child unit. Again there is no use clause,
210 -- and subsequent processing proceeds as for an explicit with clause.
212 -- Sem thus completes the loading of all required units, except those
213 -- required for inline subprogram bodies or inlined generics. If any
214 -- of these load attempts fails, then the expander will not be called,
215 -- even if code was to be generated. If the load attempts all succeed
216 -- then the expander is called, though the attempt to generate code may
217 -- still fail if an error occurs during a load attempt for an inlined
218 -- body or a generic body.
220 -------------------------------------------
221 -- Special Handling of Subprogram Bodies --
222 -------------------------------------------
224 -- A subprogram body (in an adb file) may stand for both a spec and a body.
225 -- A simple model (and one that was adopted through version 2.07) is simply
226 -- to assume that such an adb file acts as its own spec if no ads file is
229 -- However, this is not correct. RM 10.1.4(4) requires that such a body
230 -- act as a spec unless a subprogram declaration of the same name is
231 -- already present. The correct interpretation of this in GNAT library
232 -- terms is to ignore an existing ads file of the same name unless this
233 -- ads file contains a subprogram declaration with the same name.
235 -- If there is an ads file with a unit other than a subprogram declaration
236 -- with the same name, then a fatal message is output, noting that this
237 -- irrelevant file must be deleted before the body can be compiled. See
238 -- ACVC test CA1020D to see how this processing is required.
244 Current_Sem_Unit : Unit_Number_Type := Main_Unit;
245 -- Unit number of unit currently being analyzed/expanded. This is set when
246 -- ever a new unit is entered, saving and restoring the old value, so that
247 -- it always reflects the unit currently being analyzed. The initial value
248 -- of Main_Unit ensures that a proper value is set initially, and in
249 -- particular for analysis of configuration pragmas in gnat.adc.
251 Main_Unit_Entity : Entity_Id;
252 -- Entity of main unit, same as Cunit_Entity (Main_Unit) except where
253 -- Main_Unit is a body with a separate spec, in which case it is the
254 -- entity for the spec.
260 -- The units table has an entry for each unit (source file) read in by the
261 -- current compilation. The table is indexed by the unit number value,
262 -- The first entry in the table, subscript Main_Unit, is for the main file.
263 -- Each entry in this units table contains the following data.
266 -- The name of the source file containing the unit. Set when the entry
267 -- is created by a call to Lib.Load, and then cannot be changed.
270 -- The index in the source file table of the corresponding source file.
271 -- Set when the entry is created by a call to Lib.Load and then cannot
275 -- The index of the unit within the file for multiple unit per file
276 -- mode. Set to zero in normal single unit per file mode.
279 -- This is copied from the Sloc field of the Enode argument passed
280 -- to Load_Unit. It refers to the enclosing construct which caused
281 -- this unit to be loaded, e.g. most typically the with clause that
282 -- referenced the unit, and is used for error handling in Par.Load.
285 -- This is the expected unit name for a file other than the main unit,
286 -- since these are cases where we load the unit using Lib.Load and we
287 -- know the unit that is expected. It must be the same as Unit_Name
288 -- if it is set (see test in Par.Load). Expected_Unit is set to
289 -- No_Name for the main unit.
292 -- The name of the unit. Initialized to No_Name by Lib.Load, and then
293 -- set by the parser when the unit is parsed to the unit name actually
294 -- found in the file (which should, in the absence of errors) be the
295 -- same name as Expected_Unit.
298 -- Pointer to the N_Compilation_Unit node. Initially set to Empty by
299 -- Lib.Load, and then reset to the required node by the parser when
300 -- the unit is parsed.
303 -- Pointer to the entity node for the compilation unit. Initially set
304 -- to Empty by Lib.Load, and then reset to the required entity by the
305 -- parser when the unit is parsed.
308 -- This is the number of the unit within the generated dependency
309 -- lines (D lines in the ALI file) which are sorted into alphabetical
310 -- order. The number is ones origin, so a value of 2 refers to the
311 -- second generated D line. The Dependency_Number values are set
312 -- as the D lines are generated, and are used to generate proper
313 -- unit references in the generated xref information and SCO output.
316 -- A flag indicating if this unit was compiled with dynamic elaboration
317 -- checks specified (as the result of using the -gnatE compilation
318 -- option or a pragma Elaboration_Checks (Dynamic).
321 -- A flag that is initialized to False, and gets set to True if a fatal
322 -- error occurs during the processing of a unit. A fatal error is one
323 -- defined as serious enough to stop the next phase of the compiler
324 -- from running (i.e. fatal error during parsing stops semantics,
325 -- fatal error during semantics stops code generation). Note that
326 -- currently, errors of any kind cause Fatal_Error to be set, but
327 -- eventually perhaps only errors labeled as Fatal_Errors should be
328 -- this severe if we decide to try Sem on sources with minor errors.
331 -- This flag is set True for all units in the current file for which
332 -- code is to be generated. This includes the unit explicitly compiled,
333 -- together with its specification, and any subunits.
336 -- A Boolean flag, initially set to False when a unit entry is created,
337 -- and set to True if the unit defines a remote access to class wide
338 -- (RACW) object. This is used for controlling generation of the RA
339 -- attribute in the ali file.
342 -- A Boolean flag, initially set False by default, set to True if a
343 -- pragma Compiler_Unit appears in the unit.
346 -- N_String_Literal node from a valid pragma Ident that applies to
347 -- this unit. If no Ident pragma applies to the unit, then Empty.
350 -- A flag that is used to catch circular WITH dependencies. It is set
351 -- True when an entry is initially created in the file table, and set
352 -- False when the load is completed, or ends with an error.
355 -- This field is used to indicate the priority of a possible main
356 -- program, as set by a pragma Priority. A value of -1 indicates
357 -- that the default priority is to be used (and is also used for
358 -- entries that do not correspond to possible main programs).
361 -- This flag is set if a subprogram unit has an allocator after the
362 -- BEGIN (it is used to set the AB flag in the M ALI line).
365 -- This is a character field containing L if Optimize_Alignment mode
366 -- was set locally, and O/T/S for Off/Time/Space default if not.
369 -- This field holds a serial number used by New_Internal_Name to
370 -- generate unique temporary numbers on a unit by unit basis. The
371 -- only access to this field is via the Increment_Serial_Number
372 -- routine which increments the current value and returns it. This
373 -- serial number is separate for each unit.
376 -- This field holds the version of the unit, which is computed as
377 -- the exclusive or of the checksums of this unit, and all its
378 -- semantically dependent units. Access to the version number field
379 -- is not direct, but is done through the routines described below.
380 -- When a unit table entry is created, this field is initialized to
381 -- the checksum of the corresponding source file. Version_Update is
382 -- then called to reflect the contributions of any unit on which this
383 -- unit is semantically dependent.
385 -- The units table is reset to empty at the start of the compilation of
386 -- each main unit by Lib.Initialize. Entries are then added by calls to
387 -- the Lib.Load procedure. The following subprograms are used to access
388 -- and modify entries in the Units table. Individual entries are accessed
389 -- using a unit number value which ranges from Main_Unit (the first entry,
390 -- which is always for the current main unit) to Last_Unit.
392 Default_Main_Priority : constant Int := -1;
393 -- Value used in Main_Priority field to indicate default main priority
395 function Cunit (U : Unit_Number_Type) return Node_Id;
396 function Cunit_Entity (U : Unit_Number_Type) return Entity_Id;
397 function Dependency_Num (U : Unit_Number_Type) return Nat;
398 function Dynamic_Elab (U : Unit_Number_Type) return Boolean;
399 function Error_Location (U : Unit_Number_Type) return Source_Ptr;
400 function Expected_Unit (U : Unit_Number_Type) return Unit_Name_Type;
401 function Fatal_Error (U : Unit_Number_Type) return Boolean;
402 function Generate_Code (U : Unit_Number_Type) return Boolean;
403 function Ident_String (U : Unit_Number_Type) return Node_Id;
404 function Has_Allocator (U : Unit_Number_Type) return Boolean;
405 function Has_RACW (U : Unit_Number_Type) return Boolean;
406 function Is_Compiler_Unit (U : Unit_Number_Type) return Boolean;
407 function Loading (U : Unit_Number_Type) return Boolean;
408 function Main_Priority (U : Unit_Number_Type) return Int;
409 function Munit_Index (U : Unit_Number_Type) return Nat;
410 function OA_Setting (U : Unit_Number_Type) return Character;
411 function Source_Index (U : Unit_Number_Type) return Source_File_Index;
412 function Unit_File_Name (U : Unit_Number_Type) return File_Name_Type;
413 function Unit_Name (U : Unit_Number_Type) return Unit_Name_Type;
414 -- Get value of named field from given units table entry
416 procedure Set_Cunit (U : Unit_Number_Type; N : Node_Id);
417 procedure Set_Cunit_Entity (U : Unit_Number_Type; E : Entity_Id);
418 procedure Set_Dynamic_Elab (U : Unit_Number_Type; B : Boolean := True);
419 procedure Set_Error_Location (U : Unit_Number_Type; W : Source_Ptr);
420 procedure Set_Fatal_Error (U : Unit_Number_Type; B : Boolean := True);
421 procedure Set_Generate_Code (U : Unit_Number_Type; B : Boolean := True);
422 procedure Set_Has_RACW (U : Unit_Number_Type; B : Boolean := True);
423 procedure Set_Has_Allocator (U : Unit_Number_Type; B : Boolean := True);
424 procedure Set_Is_Compiler_Unit (U : Unit_Number_Type; B : Boolean := True);
425 procedure Set_Ident_String (U : Unit_Number_Type; N : Node_Id);
426 procedure Set_Loading (U : Unit_Number_Type; B : Boolean := True);
427 procedure Set_Main_Priority (U : Unit_Number_Type; P : Int);
428 procedure Set_OA_Setting (U : Unit_Number_Type; C : Character);
429 procedure Set_Unit_Name (U : Unit_Number_Type; N : Unit_Name_Type);
430 -- Set value of named field for given units table entry. Note that we
431 -- do not have an entry for each possible field, since some of the fields
432 -- can only be set by specialized interfaces (defined below).
434 function Version_Get (U : Unit_Number_Type) return Word_Hex_String;
435 -- Returns the version as a string with 8 hex digits (upper case letters)
437 function Last_Unit return Unit_Number_Type;
438 -- Unit number of last allocated unit
440 function Num_Units return Nat;
441 -- Number of units currently in unit table
443 procedure Remove_Unit (U : Unit_Number_Type);
444 -- Remove unit U from unit table. Currently this is effective only
445 -- if U is the last unit currently stored in the unit table.
447 function Entity_Is_In_Main_Unit (E : Entity_Id) return Boolean;
448 -- Returns True if the entity E is declared in the main unit, or, in
449 -- its corresponding spec, or one of its subunits. Entities declared
450 -- within generic instantiations return True if the instantiation is
451 -- itself "in the main unit" by this definition. Otherwise False.
453 function Get_Source_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type;
454 pragma Inline (Get_Source_Unit);
455 function Get_Source_Unit (S : Source_Ptr) return Unit_Number_Type;
456 -- Return unit number of file identified by given source pointer value.
457 -- This call must always succeed, since any valid source pointer value
458 -- belongs to some previously loaded module. If the given source pointer
459 -- value is within an instantiation, this function returns the unit number
460 -- of the template, i.e. the unit containing the source code corresponding
461 -- to the given Source_Ptr value. The version taking a Node_Id argument, N,
462 -- simply applies the function to Sloc (N).
464 function Get_Code_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type;
465 pragma Inline (Get_Code_Unit);
466 function Get_Code_Unit (S : Source_Ptr) return Unit_Number_Type;
467 -- This is like Get_Source_Unit, except that in the instantiation case,
468 -- it uses the location of the top level instantiation, rather than the
469 -- template, so it returns the unit number containing the code that
470 -- corresponds to the node N, or the source location S.
472 function In_Same_Source_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean;
473 pragma Inline (In_Same_Source_Unit);
474 -- Determines if the two nodes or entities N1 and N2 are in the same
475 -- source unit, the criterion being that Get_Source_Unit yields the
476 -- same value for each argument.
478 function In_Same_Code_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean;
479 pragma Inline (In_Same_Code_Unit);
480 -- Determines if the two nodes or entities N1 and N2 are in the same
481 -- code unit, the criterion being that Get_Code_Unit yields the same
482 -- value for each argument.
484 function In_Same_Extended_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean;
485 pragma Inline (In_Same_Extended_Unit);
486 -- Determines if two nodes or entities N1 and N2 are in the same
487 -- extended unit, where an extended unit is defined as a unit and all
488 -- its subunits (considered recursively, i.e. subunits of subunits are
489 -- included). Returns true if S1 and S2 are in the same extended unit
490 -- and False otherwise.
492 function In_Same_Extended_Unit (S1, S2 : Source_Ptr) return Boolean;
493 pragma Inline (In_Same_Extended_Unit);
494 -- Determines if the two source locations S1 and S2 are in the same
495 -- extended unit, where an extended unit is defined as a unit and all
496 -- its subunits (considered recursively, i.e. subunits of subunits are
497 -- included). Returns true if S1 and S2 are in the same extended unit
498 -- and False otherwise.
500 function In_Extended_Main_Code_Unit
501 (N : Node_Or_Entity_Id) return Boolean;
502 -- Return True if the node is in the generated code of the extended main
503 -- unit, defined as the main unit, its specification (if any), and all
504 -- its subunits (considered recursively). Units for which this enquiry
505 -- returns True are those for which code will be generated. Nodes from
506 -- instantiations are included in the extended main unit for this call.
507 -- If the main unit is itself a subunit, then the extended main unit
508 -- includes its parent unit, and the parent unit spec if it is separate.
510 function In_Extended_Main_Code_Unit (Loc : Source_Ptr) return Boolean;
511 -- Same function as above, but argument is a source pointer rather
514 function In_Extended_Main_Source_Unit
515 (N : Node_Or_Entity_Id) return Boolean;
516 -- Return True if the node is in the source text of the extended main
517 -- unit, defined as the main unit, its specification (if any), and all
518 -- its subunits (considered recursively). Units for which this enquiry
519 -- returns True are those for which code will be generated. This differs
520 -- from In_Extended_Main_Code_Unit only in that instantiations are not
521 -- included for the purposes of this call. If the main unit is itself
522 -- a subunit, then the extended main unit includes its parent unit,
523 -- and the parent unit spec if it is separate.
525 function In_Extended_Main_Source_Unit (Loc : Source_Ptr) return Boolean;
526 -- Same function as above, but argument is a source pointer
528 function In_Predefined_Unit (N : Node_Or_Entity_Id) return Boolean;
529 -- Returns True if the given node or entity appears within the source text
530 -- of a predefined unit (i.e. within Ada, Interfaces, System or within one
531 -- of the descendent packages of one of these three packages).
533 function In_Predefined_Unit (S : Source_Ptr) return Boolean;
534 -- Same function as above but argument is a source pointer
536 function Earlier_In_Extended_Unit (S1, S2 : Source_Ptr) return Boolean;
537 -- Given two Sloc values for which In_Same_Extended_Unit is true, determine
538 -- if S1 appears before S2. Returns True if S1 appears before S2, and False
539 -- otherwise. The result is undefined if S1 and S2 are not in the same
540 -- extended unit. Note: this routine will not give reliable results if
541 -- called after Sprint has been called with -gnatD set.
543 function Compilation_Switches_Last return Nat;
544 -- Return the count of stored compilation switches
546 function Get_Compilation_Switch (N : Pos) return String_Ptr;
547 -- Return the Nth stored compilation switch, or null if less than N
548 -- switches have been stored. Used by ASIS and back ends written in Ada.
550 function Get_Cunit_Unit_Number (N : Node_Id) return Unit_Number_Type;
551 -- Return unit number of the unit whose N_Compilation_Unit node is the
552 -- one passed as an argument. This must always succeed since the node
553 -- could not have been built without making a unit table entry.
555 function Get_Cunit_Entity_Unit_Number
556 (E : Entity_Id) return Unit_Number_Type;
557 -- Return unit number of the unit whose compilation unit spec entity is
558 -- the one passed as an argument. This must always succeed since the
559 -- entity could not have been built without making a unit table entry.
561 function Increment_Serial_Number return Nat;
562 -- Increment Serial_Number field for current unit, and return the
563 -- incremented value.
565 procedure Synchronize_Serial_Number;
566 -- This function increments the Serial_Number field for the current unit
567 -- but does not return the incremented value. This is used when there
568 -- is a situation where one path of control increments a serial number
569 -- (using Increment_Serial_Number), and the other path does not and it is
570 -- important to keep the serial numbers synchronized in the two cases (e.g.
571 -- when the references in a package and a client must be kept consistent).
573 procedure Replace_Linker_Option_String
575 Match_String : String);
576 -- Replace an existing Linker_Option if the prefix Match_String matches,
577 -- otherwise call Store_Linker_Option_String.
579 procedure Store_Compilation_Switch (Switch : String);
580 -- Called to register a compilation switch, either front-end or back-end,
581 -- which may influence the generated output file(s). Switch is the text of
582 -- the switch to store (except that -fRTS gets changed back to --RTS).
584 procedure Enable_Switch_Storing;
585 -- Enable registration of switches by Store_Compilation_Switch. Used to
586 -- avoid registering switches added automatically by the gcc driver at the
587 -- beginning of the command line.
589 procedure Disable_Switch_Storing;
590 -- Disable registration of switches by Store_Compilation_Switch. Used to
591 -- avoid registering switches added automatically by the gcc driver at the
592 -- end of the command line.
594 procedure Store_Linker_Option_String (S : String_Id);
595 -- This procedure is called to register the string from a pragma
596 -- Linker_Option. The argument is the Id of the string to register.
598 procedure Store_Note (N : Node_Id);
599 -- This procedure is called to register a pragma N for which a notes
600 -- entry is required.
602 procedure Initialize;
603 -- Initialize internal tables
606 -- Lock internal tables before calling back end
609 -- Unlock internal tables, in cases where the back end needs to modify them
612 -- Initializes internal tables from current tree file using the relevant
613 -- Table.Tree_Read routines.
615 procedure Tree_Write;
616 -- Writes out internal tables to current tree file using the relevant
617 -- Table.Tree_Write routines.
619 function Is_Loaded (Uname : Unit_Name_Type) return Boolean;
620 -- Determines if unit with given name is already loaded, i.e. there is
621 -- already an entry in the file table with this unit name for which the
622 -- corresponding file was found and parsed. Note that the Fatal_Error flag
623 -- of this entry must be checked before proceeding with further processing.
625 procedure Version_Referenced (S : String_Id);
626 -- This routine is called from Exp_Attr to register the use of a Version
627 -- or Body_Version attribute. The argument is the external name used to
628 -- access the version string.
630 procedure List (File_Names_Only : Boolean := False);
631 -- Lists units in active library (i.e. generates output consisting of a
632 -- sorted listing of the units represented in File table, except for the
633 -- main unit). If File_Names_Only is set to True, then the list includes
634 -- only file names, and no other information. Otherwise the unit name and
635 -- time stamp are also output. File_Names_Only also restricts the list to
636 -- exclude any predefined files.
638 function Generic_May_Lack_ALI (Sfile : File_Name_Type) return Boolean;
639 -- Generic units must be separately compiled. Since we always use
640 -- macro substitution for generics, the resulting object file is a dummy
641 -- one with no code, but the ALI file has the normal form, and we need
642 -- this ALI file so that the binder can work out a correct order of
645 -- However, ancient versions of GNAT used to not generate code or ALI
646 -- files for generic units, and this would yield complex order of
647 -- elaboration issues. These were fixed in GNAT 3.10. The support for not
648 -- compiling language-defined library generics was retained nonetheless
649 -- to facilitate bootstrap. Specifically, it is convenient to have
650 -- the same list of files to be compiled for all stages. So, if the
651 -- bootstrap compiler does not generate code for a given file, then
652 -- the stage1 compiler (and binder) also must deal with the case of
653 -- that file not being compiled. The predicate Generic_May_Lack_ALI is
654 -- True for those generic units for which missing ALI files are allowed.
657 pragma Inline (Cunit);
658 pragma Inline (Cunit_Entity);
659 pragma Inline (Dependency_Num);
660 pragma Inline (Fatal_Error);
661 pragma Inline (Generate_Code);
662 pragma Inline (Has_Allocator);
663 pragma Inline (Has_RACW);
664 pragma Inline (Is_Compiler_Unit);
665 pragma Inline (Increment_Serial_Number);
666 pragma Inline (Loading);
667 pragma Inline (Main_Priority);
668 pragma Inline (Munit_Index);
669 pragma Inline (OA_Setting);
670 pragma Inline (Set_Cunit);
671 pragma Inline (Set_Cunit_Entity);
672 pragma Inline (Set_Fatal_Error);
673 pragma Inline (Set_Generate_Code);
674 pragma Inline (Set_Has_Allocator);
675 pragma Inline (Set_Has_RACW);
676 pragma Inline (Set_Loading);
677 pragma Inline (Set_Main_Priority);
678 pragma Inline (Set_OA_Setting);
679 pragma Inline (Set_Unit_Name);
680 pragma Inline (Source_Index);
681 pragma Inline (Unit_File_Name);
682 pragma Inline (Unit_Name);
684 type Unit_Record is record
685 Unit_File_Name : File_Name_Type;
686 Unit_Name : Unit_Name_Type;
688 Expected_Unit : Unit_Name_Type;
689 Source_Index : Source_File_Index;
691 Cunit_Entity : Entity_Id;
692 Dependency_Num : Int;
693 Ident_String : Node_Id;
697 Error_Location : Source_Ptr;
698 Fatal_Error : Boolean;
699 Generate_Code : Boolean;
701 Is_Compiler_Unit : Boolean;
702 Dynamic_Elab : Boolean;
704 Has_Allocator : Boolean;
705 OA_Setting : Character;
708 -- The following representation clause ensures that the above record
709 -- has no holes. We do this so that when instances of this record are
710 -- written by Tree_Gen, we do not write uninitialized values to the file.
712 for Unit_Record use record
713 Unit_File_Name at 0 range 0 .. 31;
714 Unit_Name at 4 range 0 .. 31;
715 Munit_Index at 8 range 0 .. 31;
716 Expected_Unit at 12 range 0 .. 31;
717 Source_Index at 16 range 0 .. 31;
718 Cunit at 20 range 0 .. 31;
719 Cunit_Entity at 24 range 0 .. 31;
720 Dependency_Num at 28 range 0 .. 31;
721 Ident_String at 32 range 0 .. 31;
722 Main_Priority at 36 range 0 .. 31;
723 Serial_Number at 40 range 0 .. 31;
724 Version at 44 range 0 .. 31;
725 Error_Location at 48 range 0 .. 31;
726 Fatal_Error at 52 range 0 .. 7;
727 Generate_Code at 53 range 0 .. 7;
728 Has_RACW at 54 range 0 .. 7;
729 Dynamic_Elab at 55 range 0 .. 7;
730 Is_Compiler_Unit at 56 range 0 .. 7;
731 OA_Setting at 57 range 0 .. 7;
732 Loading at 58 range 0 .. 7;
733 Has_Allocator at 59 range 0 .. 7;
736 for Unit_Record'Size use 60 * 8;
737 -- This ensures that we did not leave out any fields
739 package Units is new Table.Table (
740 Table_Component_Type => Unit_Record,
741 Table_Index_Type => Unit_Number_Type,
742 Table_Low_Bound => Main_Unit,
743 Table_Initial => Alloc.Units_Initial,
744 Table_Increment => Alloc.Units_Increment,
745 Table_Name => "Units");
747 -- The following table stores strings from pragma Linker_Option lines
749 type Linker_Option_Entry is record
751 -- The string for the linker option line
753 Unit : Unit_Number_Type;
754 -- The unit from which the linker option comes
757 package Linker_Option_Lines is new Table.Table (
758 Table_Component_Type => Linker_Option_Entry,
759 Table_Index_Type => Integer,
760 Table_Low_Bound => 1,
761 Table_Initial => Alloc.Linker_Option_Lines_Initial,
762 Table_Increment => Alloc.Linker_Option_Lines_Increment,
763 Table_Name => "Linker_Option_Lines");
765 -- The following table stores references to pragmas that generate Notes
767 type Notes_Entry is record
768 Pragma_Node : Node_Id;
769 Unit : Unit_Number_Type;
772 package Notes is new Table.Table (
773 Table_Component_Type => Notes_Entry,
774 Table_Index_Type => Integer,
775 Table_Low_Bound => 1,
776 Table_Initial => Alloc.Notes_Initial,
777 Table_Increment => Alloc.Notes_Increment,
778 Table_Name => "Notes");
780 -- The following table records the compilation switches used to compile
781 -- the main unit. The table includes only switches. It excludes -o
782 -- switches as well as artifacts of the gcc/gnat1 interface such as
783 -- -quiet, -dumpbase, or -auxbase.
785 -- This table is set as part of the compiler argument scanning in
786 -- Back_End. It can also be reset in -gnatc mode from the data in an
787 -- existing ali file, and is read and written by the Tree_Read and
788 -- Tree_Write routines for ASIS.
790 package Compilation_Switches is new Table.Table (
791 Table_Component_Type => String_Ptr,
792 Table_Index_Type => Nat,
793 Table_Low_Bound => 1,
795 Table_Increment => 100,
796 Table_Name => "Compilation_Switches");
798 Load_Msg_Sloc : Source_Ptr;
799 -- Location for placing error messages (a token in the main source text)
800 -- This is set from Sloc (Enode) by Load only in the case where this Sloc
801 -- is in the main source file. This ensures that not found messages and
802 -- circular dependency messages reference the original with in this source.
804 type Unit_Ref_Table is array (Pos range <>) of Unit_Number_Type;
805 -- Type to hold list of indirect references to unit number table
807 type Load_Stack_Entry is record
808 Unit_Number : Unit_Number_Type;
812 -- The Load_Stack table contains a list of unit numbers (indices into the
813 -- unit table) of units being loaded on a single dependency chain, and a
814 -- flag to indicate whether this unit is loaded through a limited_with
815 -- clause. The First entry is the main unit. The second entry, if present
816 -- is a unit on which the first unit depends, etc. This stack is used to
817 -- generate error messages showing the dependency chain if a file is not
818 -- found, or whether a true circular dependency exists. The Load_Unit
819 -- function makes an entry in this table when it is called, and removes
820 -- the entry just before it returns.
822 package Load_Stack is new Table.Table (
823 Table_Component_Type => Load_Stack_Entry,
824 Table_Index_Type => Int,
825 Table_Low_Bound => 0,
826 Table_Initial => Alloc.Load_Stack_Initial,
827 Table_Increment => Alloc.Load_Stack_Increment,
828 Table_Name => "Load_Stack");
830 procedure Sort (Tbl : in out Unit_Ref_Table);
831 -- This procedure sorts the given unit reference table in order of
832 -- ascending unit names, where the ordering relation is as described
833 -- by the comparison routines provided by package Uname.
835 -- The Version_Ref table records Body_Version and Version attribute
836 -- references. The entries are simply the strings for the external
837 -- names that correspond to the referenced values.
839 package Version_Ref is new Table.Table (
840 Table_Component_Type => String_Id,
841 Table_Index_Type => Nat,
842 Table_Low_Bound => 1,
844 Table_Increment => 100,
845 Table_Name => "Version_Ref");