1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                              R T S F I N D                               --
6--                                                                          --
7--                                 B o d y                                  --
8--                                                                          --
9--          Copyright (C) 1992-2020, Free Software Foundation, Inc.         --
10--                                                                          --
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.  See the GNU General Public License --
17-- for  more details.  You should have  received  a copy of the GNU General --
18-- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19-- http://www.gnu.org/licenses for a complete copy of the license.          --
20--                                                                          --
21-- GNAT was originally developed  by the GNAT team at  New York University. --
22-- Extensive contributions were provided by Ada Core Technologies Inc.      --
23--                                                                          --
24------------------------------------------------------------------------------
25
26with Atree;    use Atree;
27with Casing;   use Casing;
28with Csets;    use Csets;
29with Debug;    use Debug;
30with Einfo;    use Einfo;
31with Elists;   use Elists;
32with Errout;   use Errout;
33with Exp_Dist;
34with Fname;    use Fname;
35with Fname.UF; use Fname.UF;
36with Ghost;    use Ghost;
37with Lib;      use Lib;
38with Lib.Load; use Lib.Load;
39with Namet;    use Namet;
40with Nlists;   use Nlists;
41with Nmake;    use Nmake;
42with Output;   use Output;
43with Opt;      use Opt;
44with Restrict; use Restrict;
45with Sem;      use Sem;
46with Sem_Aux;  use Sem_Aux;
47with Sem_Ch7;  use Sem_Ch7;
48with Sem_Dist; use Sem_Dist;
49with Sem_Util; use Sem_Util;
50with Sinfo;    use Sinfo;
51with Stand;    use Stand;
52with Snames;   use Snames;
53with Tbuild;   use Tbuild;
54with Uname;    use Uname;
55
56package body Rtsfind is
57
58   RTE_Available_Call : Boolean := False;
59   --  Set True during call to RTE from RTE_Available (or from call to
60   --  RTE_Record_Component from RTE_Record_Component_Available). Tells
61   --  the called subprogram to set RTE_Is_Available to False rather than
62   --  generating an error message.
63
64   RTE_Is_Available : Boolean;
65   --  Set True by RTE_Available on entry. When RTE_Available_Call is set
66   --  True, set False if RTE would otherwise generate an error message.
67
68   ----------------
69   -- Unit table --
70   ----------------
71
72   --  The unit table has one entry for each unit included in the definition
73   --  of the type RTU_Id in the spec. The table entries are initialized in
74   --  Initialize to set the Entity field to Empty, indicating that the
75   --  corresponding unit has not yet been loaded. The fields are set when
76   --  a unit is loaded to contain the defining entity for the unit, the
77   --  unit name, and the unit number.
78
79   --  Note that a unit can be loaded either by a call to find an entity
80   --  within the unit (e.g. RTE), or by an explicit with of the unit. In
81   --  the latter case it is critical to make a call to Set_RTU_Loaded to
82   --  ensure that the entry in this table reflects the load.
83
84   --  A unit retrieved through rtsfind  may end up in the context of several
85   --  other units, in addition to the main unit. These additional with_clauses
86   --  are needed to generate a proper traversal order for CodePeer. To
87   --  minimize somewhat the redundancy created by numerous calls to rtsfind
88   --  from different units, we keep track of the list of implicit with_clauses
89   --  already created for the current loaded unit.
90
91   type RT_Unit_Table_Record is record
92      Entity               : Entity_Id;
93      Uname                : Unit_Name_Type;
94      First_Implicit_With  : Node_Id;
95      Unum                 : Unit_Number_Type;
96   end record;
97
98   RT_Unit_Table : array (RTU_Id) of RT_Unit_Table_Record;
99
100   --------------------------
101   -- Runtime Entity Table --
102   --------------------------
103
104   --  There is one entry in the runtime entity table for each entity that is
105   --  included in the definition of the RE_Id type in the spec. The entries
106   --  are set by Initialize_Rtsfind to contain Empty, indicating that the
107   --  entity has not yet been located. Once the entity is located for the
108   --  first time, its ID is stored in this array, so that subsequent calls
109   --  for the same entity can be satisfied immediately.
110
111   --  NOTE: In order to avoid conflicts between record components and subprgs
112   --        that have the same name (i.e. subprogram External_Tag and
113   --        component External_Tag of package Ada.Tags) this table is not used
114   --        with Record_Components.
115
116   RE_Table : array (RE_Id) of Entity_Id;
117
118   --------------------------------
119   -- Generation of with_clauses --
120   --------------------------------
121
122   --  When a unit is implicitly loaded as a result of a call to RTE, it is
123   --  necessary to create one or two implicit with_clauses. We add such
124   --  with_clauses to the extended main unit if needed, and also to whatever
125   --  unit needs them, which is not necessarily the main unit. The former
126   --  ensures that the object is correctly loaded by the binder. The latter
127   --  is necessary for CodePeer.
128
129   --  The field First_Implicit_With in the unit table record are used to
130   --  avoid creating duplicate with_clauses.
131
132   ----------------------------------------------
133   -- Table of Predefined RE_Id Error Messages --
134   ----------------------------------------------
135
136   --  If an attempt is made to load an entity, given an RE_Id value, and the
137   --  entity is not available in the current configuration, an error message
138   --  is given (see Entity_Not_Defined below). The general form of such an
139   --  error message is for example:
140
141   --    entity "System.Pack_43.Bits_43" not defined
142
143   --  The following table defines a set of RE_Id image values for which this
144   --  error message is specialized and replaced by specific text indicating
145   --  the exact message to be output. For example, in the case above, for the
146   --  RE_Id value RE_Bits_43, we do indeed specialize the message, and the
147   --  above generic message is replaced by:
148
149   --    packed component size of 43 is not supported
150
151   type CString_Ptr is access constant String;
152
153   type PRE_Id_Entry is record
154      Str : CString_Ptr;
155      --  Pointer to string with the RE_Id image. The sequence ?? may appear
156      --  in which case it will match any characters in the RE_Id image value.
157      --  This is used to avoid the need for dozens of entries for RE_Bits_??.
158
159      Msg : CString_Ptr;
160      --  Pointer to string with the corresponding error text. The sequence
161      --  ?? may appear, in which case, it is replaced by the corresponding
162      --  sequence ?? in the Str value (if the first ? is zero, then it is
163      --  omitted from the message).
164   end record;
165
166   Str1 : aliased constant String := "RE_BITS_??";
167   Str2 : aliased constant String := "RE_GET_??";
168   Str3 : aliased constant String := "RE_SET_??";
169   Str4 : aliased constant String := "RE_CALL_SIMPLE";
170
171   MsgPack : aliased constant String :=
172              "packed component size of ?? is not supported";
173   MsgRV   : aliased constant String :=
174              "task rendezvous is not supported";
175
176   PRE_Id_Table : constant array (Natural range <>) of PRE_Id_Entry :=
177                    (1 => (Str1'Access, MsgPack'Access),
178                     2 => (Str2'Access, MsgPack'Access),
179                     3 => (Str3'Access, MsgPack'Access),
180                     4 => (Str4'Access, MsgRV'Access));
181   --  We will add entries to this table as we find cases where it is a good
182   --  idea to do so. By no means all the RE_Id values need entries, because
183   --  the expander often gives clear messages before it makes the Rtsfind
184   --  call expecting to find the entity.
185
186   -----------------------
187   -- Local Subprograms --
188   -----------------------
189
190   function Check_CRT (E : RE_Id; Eid : Entity_Id) return Entity_Id;
191   --  Check entity Eid to ensure that configurable run-time restrictions are
192   --  met. May generate an error message (if RTE_Available_Call is false) and
193   --  raise RE_Not_Available if entity E does not exist (e.g. Eid is Empty).
194   --  Also check that entity is not overloaded.
195
196   procedure Entity_Not_Defined (Id : RE_Id);
197   --  Outputs error messages for an entity that is not defined in the run-time
198   --  library (the form of the error message is tailored for no run time or
199   --  configurable run time mode as required). See also table of pre-defined
200   --  messages for entities above (RE_Id_Messages).
201
202   function Get_Unit_Name (U_Id : RTU_Id) return Unit_Name_Type;
203   --  Retrieves the Unit Name given a unit id represented by its enumeration
204   --  value in RTU_Id.
205
206   procedure Load_Fail (S : String; U_Id : RTU_Id; Id : RE_Id);
207   pragma No_Return (Load_Fail);
208   --  Internal procedure called if we can't successfully locate or process a
209   --  run-time unit. The parameters give information about the error message
210   --  to be given. S is a reason for failing to compile the file and U_Id is
211   --  the unit id. RE_Id is the RE_Id originally passed to RTE. The message in
212   --  S is one of the following:
213   --
214   --     "not found"
215   --     "had parser errors"
216   --     "had semantic errors"
217   --
218   --  The "not found" case is treated specially in that it is considered
219   --  a normal situation in configurable run-time mode, and generates
220   --  a warning, but is otherwise ignored.
221
222   procedure Load_RTU
223     (U_Id        : RTU_Id;
224      Id          : RE_Id   := RE_Null;
225      Use_Setting : Boolean := False);
226   --  Load the unit whose Id is given if not already loaded. The unit is
227   --  loaded and analyzed, and the entry in RT_Unit_Table is updated to
228   --  reflect the load. Use_Setting is used to indicate the initial setting
229   --  for the Is_Potentially_Use_Visible flag of the entity for the loaded
230   --  unit (if it is indeed loaded). A value of False means nothing special
231   --  need be done. A value of True indicates that this flag must be set to
232   --  True. It is needed only in the Check_Text_IO_Special_Unit procedure,
233   --  which may materialize an entity of Text_IO (or [Wide_]Wide_Text_IO) that
234   --  was previously unknown. Id is the RE_Id value of the entity which was
235   --  originally requested. Id is used only for error message detail, and if
236   --  it is RE_Null, then the attempt to output the entity name is ignored.
237
238   function Make_Unit_Name
239     (U : RT_Unit_Table_Record;
240      N : Node_Id) return Node_Id;
241   --  If the unit is a child unit, build fully qualified name for use in
242   --  With_Clause.
243
244   procedure Maybe_Add_With (U : in out RT_Unit_Table_Record);
245   --  If necessary, add an implicit with_clause from the current unit to the
246   --  one represented by U.
247
248   procedure Output_Entity_Name (Id : RE_Id; Msg : String);
249   --  Output continuation error message giving qualified name of entity
250   --  corresponding to Id, appending the string given by Msg.
251
252   function RE_Chars (E : RE_Id) return Name_Id;
253   --  Given a RE_Id value returns the Chars of the corresponding entity
254
255   procedure RTE_Error_Msg (Msg : String);
256   --  Generates a message by calling Error_Msg_N specifying Current_Error_Node
257   --  as the node location using the given Msg text. Special processing in the
258   --  case where RTE_Available_Call is set. In this case, no message is output
259   --  and instead RTE_Is_Available is set to False. Note that this can only be
260   --  used if you are sure that the message comes directly or indirectly from
261   --  a call to the RTE function.
262
263   ---------------
264   -- Check_CRT --
265   ---------------
266
267   function Check_CRT (E : RE_Id; Eid : Entity_Id) return Entity_Id is
268      U_Id : constant RTU_Id := RE_Unit_Table (E);
269
270   begin
271      if No (Eid) then
272         if RTE_Available_Call then
273            RTE_Is_Available := False;
274         else
275            Entity_Not_Defined (E);
276         end if;
277
278         raise RE_Not_Available;
279
280      --  Entity is available
281
282      else
283         --  If in No_Run_Time mode and entity is neither in the current unit
284         --  nor in one of the specially permitted units, raise the exception.
285
286         if No_Run_Time_Mode
287           and then not OK_No_Run_Time_Unit (U_Id)
288
289           --  If the entity being referenced is defined in the current scope,
290           --  using it is always fine as such usage can never introduce any
291           --  dependency on an additional unit. The presence of this test
292           --  helps generating meaningful error messages for CRT violations.
293
294           and then Scope (Eid) /= Current_Scope
295         then
296            Entity_Not_Defined (E);
297            raise RE_Not_Available;
298         end if;
299
300         --  Check entity is not overloaded, checking for special exceptions
301
302         if Has_Homonym (Eid)
303           and then E /= RE_Save_Occurrence
304         then
305            Set_Standard_Error;
306            Write_Str ("Run-time configuration error (");
307            Write_Str ("rtsfind entity """);
308            Get_Decoded_Name_String (Chars (Eid));
309            Set_Casing (Mixed_Case);
310            Write_Str (Name_Buffer (1 .. Name_Len));
311            Write_Str (""" is overloaded)");
312            Write_Eol;
313            raise Unrecoverable_Error;
314         end if;
315
316         --  Otherwise entity is accessible
317
318         return Eid;
319      end if;
320   end Check_CRT;
321
322   --------------------------------
323   -- Check_Text_IO_Special_Unit --
324   --------------------------------
325
326   procedure Check_Text_IO_Special_Unit (Nam : Node_Id) is
327      Chrs : Name_Id;
328
329      type Name_Map_Type is array (Text_IO_Package_Name) of RTU_Id;
330
331      Name_Map : constant Name_Map_Type := Name_Map_Type'(
332        Name_Decimal_IO     => Ada_Text_IO_Decimal_IO,
333        Name_Enumeration_IO => Ada_Text_IO_Enumeration_IO,
334        Name_Fixed_IO       => Ada_Text_IO_Fixed_IO,
335        Name_Float_IO       => Ada_Text_IO_Float_IO,
336        Name_Integer_IO     => Ada_Text_IO_Integer_IO,
337        Name_Modular_IO     => Ada_Text_IO_Modular_IO);
338
339      Wide_Name_Map : constant Name_Map_Type := Name_Map_Type'(
340        Name_Decimal_IO     => Ada_Wide_Text_IO_Decimal_IO,
341        Name_Enumeration_IO => Ada_Wide_Text_IO_Enumeration_IO,
342        Name_Fixed_IO       => Ada_Wide_Text_IO_Fixed_IO,
343        Name_Float_IO       => Ada_Wide_Text_IO_Float_IO,
344        Name_Integer_IO     => Ada_Wide_Text_IO_Integer_IO,
345        Name_Modular_IO     => Ada_Wide_Text_IO_Modular_IO);
346
347      Wide_Wide_Name_Map : constant Name_Map_Type := Name_Map_Type'(
348        Name_Decimal_IO     => Ada_Wide_Wide_Text_IO_Decimal_IO,
349        Name_Enumeration_IO => Ada_Wide_Wide_Text_IO_Enumeration_IO,
350        Name_Fixed_IO       => Ada_Wide_Wide_Text_IO_Fixed_IO,
351        Name_Float_IO       => Ada_Wide_Wide_Text_IO_Float_IO,
352        Name_Integer_IO     => Ada_Wide_Wide_Text_IO_Integer_IO,
353        Name_Modular_IO     => Ada_Wide_Wide_Text_IO_Modular_IO);
354
355      To_Load : RTU_Id;
356      --  Unit to be loaded, from one of the above maps
357
358   begin
359      --  Nothing to do if name is not an identifier or a selected component
360      --  whose selector_name is an identifier.
361
362      if Nkind (Nam) = N_Identifier then
363         Chrs := Chars (Nam);
364
365      elsif Nkind (Nam) = N_Selected_Component
366        and then Nkind (Selector_Name (Nam)) = N_Identifier
367      then
368         Chrs := Chars (Selector_Name (Nam));
369
370      else
371         return;
372      end if;
373
374      --  Nothing to do if name is not one of the Text_IO subpackages
375      --  Otherwise look through loaded units, and if we find Text_IO
376      --  or [Wide_]Wide_Text_IO already loaded, then load the proper child.
377
378      if Chrs in Text_IO_Package_Name then
379         for U in Main_Unit .. Last_Unit loop
380            Get_Name_String (Unit_File_Name (U));
381
382            if Name_Len = 12 then
383
384               --  Here is where we do the loads if we find one of the units
385               --  Ada.Text_IO or Ada.[Wide_]Wide_Text_IO. An interesting
386               --  detail is that these units may already be used (i.e. their
387               --  In_Use flags may be set). Normally when the In_Use flag is
388               --  set, the Is_Potentially_Use_Visible flag of all entities in
389               --  the package is set, but the new entity we are mysteriously
390               --  adding was not there to have its flag set at the time. So
391               --  that's why we pass the extra parameter to RTU_Find, to make
392               --  sure the flag does get set now. Given that those generic
393               --  packages are in fact child units, we must indicate that
394               --  they are visible.
395
396               if Name_Buffer (1 .. 12) = "a-textio.ads" then
397                  To_Load := Name_Map (Chrs);
398
399               elsif Name_Buffer (1 .. 12) = "a-witeio.ads" then
400                  To_Load := Wide_Name_Map (Chrs);
401
402               elsif Name_Buffer (1 .. 12) = "a-ztexio.ads" then
403                  To_Load := Wide_Wide_Name_Map (Chrs);
404
405               else
406                  goto Continue;
407               end if;
408
409               Load_RTU (To_Load, Use_Setting => In_Use (Cunit_Entity (U)));
410               Set_Is_Visible_Lib_Unit (RT_Unit_Table (To_Load).Entity);
411
412               --  Prevent creation of an implicit 'with' from (for example)
413               --  Ada.Wide_Text_IO.Integer_IO to Ada.Text_IO.Integer_IO,
414               --  because these could create cycles. First check whether the
415               --  simple names match ("integer_io" = "integer_io"), and then
416               --  check whether the parent is indeed one of the
417               --  [[Wide_]Wide_]Text_IO packages.
418
419               if Chrs = Chars (Cunit_Entity (Current_Sem_Unit)) then
420                  declare
421                     Parent_Name : constant Unit_Name_Type :=
422                       Get_Parent_Spec_Name
423                         (Unit_Name (Current_Sem_Unit));
424
425                  begin
426                     if Present (Parent_Name) then
427                        Get_Name_String (Parent_Name);
428
429                        declare
430                           P : String renames Name_Buffer (1 .. Name_Len);
431                        begin
432                           if P = "ada.text_io%s"      or else
433                             P = "ada.wide_text_io%s" or else
434                             P = "ada.wide_wide_text_io%s"
435                           then
436                              goto Continue;
437                           end if;
438                        end;
439                     end if;
440                  end;
441               end if;
442
443               --  Add an implicit with clause from the current unit to the
444               --  [[Wide_]Wide_]Text_IO child (if necessary).
445
446               Maybe_Add_With (RT_Unit_Table (To_Load));
447            end if;
448
449            <<Continue>> null;
450         end loop;
451      end if;
452
453   exception
454         --  Generate error message if run-time unit not available
455
456      when RE_Not_Available =>
457         Error_Msg_N ("& not available", Nam);
458   end Check_Text_IO_Special_Unit;
459
460   ------------------------
461   -- Entity_Not_Defined --
462   ------------------------
463
464   procedure Entity_Not_Defined (Id : RE_Id) is
465   begin
466      if No_Run_Time_Mode then
467
468         --  If the error occurs when compiling the body of a predefined
469         --  unit for inlining purposes, the body must be illegal in this
470         --  mode, and there is no point in continuing.
471
472         if In_Predefined_Unit (Current_Error_Node) then
473            Error_Msg_N
474              ("construct not allowed in no run time mode!",
475                 Current_Error_Node);
476            raise Unrecoverable_Error;
477
478         else
479            RTE_Error_Msg ("|construct not allowed in no run time mode");
480         end if;
481
482      elsif Configurable_Run_Time_Mode then
483         RTE_Error_Msg ("|construct not allowed in this configuration>");
484      else
485         RTE_Error_Msg ("run-time configuration error");
486      end if;
487
488      --  See if this entry is to be found in the PRE_Id table that provides
489      --  specialized messages for some RE_Id values.
490
491      for J in PRE_Id_Table'Range loop
492         declare
493            TStr : constant String := PRE_Id_Table (J).Str.all;
494            RStr : constant String := RE_Id'Image (Id);
495            TMsg : String          := PRE_Id_Table (J).Msg.all;
496            LMsg : Natural         := TMsg'Length;
497
498         begin
499            if TStr'Length = RStr'Length then
500               for J in TStr'Range loop
501                  if TStr (J) /= RStr (J) and then TStr (J) /= '?' then
502                     goto Continue;
503                  end if;
504               end loop;
505
506               for J in TMsg'First .. TMsg'Last - 1 loop
507                  if TMsg (J) = '?' then
508                     for K in 1 .. TStr'Last loop
509                        if TStr (K) = '?' then
510                           if RStr (K) = '0' then
511                              TMsg (J) := RStr (K + 1);
512                              TMsg (J + 1 .. LMsg - 1) := TMsg (J + 2 .. LMsg);
513                              LMsg := LMsg - 1;
514                           else
515                              TMsg (J .. J + 1) := RStr (K .. K + 1);
516                           end if;
517
518                           exit;
519                        end if;
520                     end loop;
521                  end if;
522               end loop;
523
524               RTE_Error_Msg (TMsg (1 .. LMsg));
525               return;
526            end if;
527         end;
528
529         <<Continue>> null;
530      end loop;
531
532      --  We did not find an entry in the table, so output the generic entity
533      --  not found message, where the name of the entity corresponds to the
534      --  given RE_Id value.
535
536      Output_Entity_Name (Id, "not defined");
537   end Entity_Not_Defined;
538
539   -------------------
540   -- Get_Unit_Name --
541   -------------------
542
543   --  The following subtypes include all the proper descendants of each unit
544   --  that has such descendants. For example, Ada_Calendar_Descendant includes
545   --  all the descendents of Ada.Calendar (except Ada.Calendar itself). These
546   --  are used by Get_Unit_Name to know where to change "_" to ".", and by
547   --  Is_Text_IO_Special_Package to detect the special generic pseudo-children
548   --  of [[Wide_]Wide_]Text_IO.
549
550   subtype Ada_Descendant is RTU_Id
551     range Ada_Calendar .. Ada_Wide_Wide_Text_IO_Modular_IO;
552
553   subtype Ada_Calendar_Descendant is Ada_Descendant
554     range Ada_Calendar_Delays .. Ada_Calendar_Delays;
555
556   subtype Ada_Dispatching_Descendant is Ada_Descendant
557     range Ada_Dispatching_EDF .. Ada_Dispatching_EDF;
558
559   subtype Ada_Interrupts_Descendant is Ada_Descendant range
560     Ada_Interrupts_Names .. Ada_Interrupts_Names;
561
562   subtype Ada_Numerics_Descendant is Ada_Descendant
563     range Ada_Numerics_Generic_Elementary_Functions ..
564           Ada_Numerics_Generic_Elementary_Functions;
565
566   subtype Ada_Real_Time_Descendant is Ada_Descendant
567     range Ada_Real_Time_Delays .. Ada_Real_Time_Timing_Events;
568
569   subtype Ada_Streams_Descendant is Ada_Descendant
570     range Ada_Streams_Stream_IO .. Ada_Streams_Stream_IO;
571
572   subtype Ada_Strings_Descendant is Ada_Descendant
573     range Ada_Strings_Superbounded .. Ada_Strings_Text_Output_Buffers;
574
575   subtype Ada_Strings_Text_Output_Descendant is Ada_Strings_Descendant
576     range Ada_Strings_Text_Output_Utils .. Ada_Strings_Text_Output_Buffers;
577
578   subtype Ada_Text_IO_Descendant is Ada_Descendant
579     range Ada_Text_IO_Decimal_IO .. Ada_Text_IO_Modular_IO;
580
581   subtype Ada_Wide_Text_IO_Descendant is Ada_Descendant
582     range Ada_Wide_Text_IO_Decimal_IO .. Ada_Wide_Text_IO_Modular_IO;
583
584   subtype Ada_Wide_Wide_Text_IO_Descendant is Ada_Descendant
585     range Ada_Wide_Wide_Text_IO_Decimal_IO ..
586           Ada_Wide_Wide_Text_IO_Modular_IO;
587
588   subtype CUDA_Descendant is RTU_Id
589     range CUDA_Driver_Types .. CUDA_Vector_Types;
590
591   subtype Interfaces_Descendant is RTU_Id
592     range Interfaces_C .. Interfaces_C_Strings;
593
594   subtype Interfaces_C_Descendant is Interfaces_Descendant
595     range Interfaces_C_Strings .. Interfaces_C_Strings;
596
597   subtype System_Descendant is RTU_Id
598     range System_Address_Image .. System_Tasking_Stages;
599
600   subtype System_Dim_Descendant is System_Descendant
601     range System_Dim_Float_IO .. System_Dim_Integer_IO;
602
603   subtype System_Multiprocessors_Descendant is System_Descendant
604     range System_Multiprocessors_Dispatching_Domains ..
605       System_Multiprocessors_Dispatching_Domains;
606
607   subtype System_Storage_Pools_Descendant is System_Descendant
608     range System_Storage_Pools_Subpools .. System_Storage_Pools_Subpools;
609
610   subtype System_Strings_Descendant is System_Descendant
611     range System_Strings_Stream_Ops .. System_Strings_Stream_Ops;
612
613   subtype System_Tasking_Descendant is System_Descendant
614     range System_Tasking_Async_Delays .. System_Tasking_Stages;
615
616   subtype System_Tasking_Protected_Objects_Descendant is
617     System_Tasking_Descendant
618     range System_Tasking_Protected_Objects_Entries ..
619       System_Tasking_Protected_Objects_Single_Entry;
620
621   subtype System_Tasking_Restricted_Descendant is System_Tasking_Descendant
622     range System_Tasking_Restricted_Stages ..
623       System_Tasking_Restricted_Stages;
624
625   subtype System_Tasking_Async_Delays_Descendant is System_Tasking_Descendant
626     range System_Tasking_Async_Delays_Enqueue_Calendar ..
627       System_Tasking_Async_Delays_Enqueue_RT;
628
629   function Get_Unit_Name (U_Id : RTU_Id) return Unit_Name_Type is
630      Uname_Chars : constant String := RTU_Id'Image (U_Id);
631   begin
632      Name_Len := Uname_Chars'Length;
633      Name_Buffer (1 .. Name_Len) := Uname_Chars;
634      Set_Casing (All_Lower_Case);
635
636      if U_Id in Ada_Descendant then
637         Name_Buffer (4) := '.';
638
639         if U_Id in Ada_Calendar_Descendant then
640            Name_Buffer (13) := '.';
641
642         elsif U_Id in Ada_Dispatching_Descendant then
643            Name_Buffer (16) := '.';
644
645         elsif U_Id in Ada_Interrupts_Descendant then
646            Name_Buffer (15) := '.';
647
648         elsif U_Id in Ada_Numerics_Descendant then
649            Name_Buffer (13) := '.';
650
651         elsif U_Id in Ada_Real_Time_Descendant then
652            Name_Buffer (14) := '.';
653
654         elsif U_Id in Ada_Streams_Descendant then
655            Name_Buffer (12) := '.';
656
657         elsif U_Id in Ada_Strings_Descendant then
658            Name_Buffer (12) := '.';
659
660            if U_Id in Ada_Strings_Text_Output_Descendant then
661               Name_Buffer (24) := '.';
662            end if;
663
664         elsif U_Id in Ada_Text_IO_Descendant then
665            Name_Buffer (12) := '.';
666
667         elsif U_Id in Ada_Wide_Text_IO_Descendant then
668            Name_Buffer (17) := '.';
669
670         elsif U_Id in Ada_Wide_Wide_Text_IO_Descendant then
671            Name_Buffer (22) := '.';
672         end if;
673
674      elsif U_Id in CUDA_Descendant then
675         Name_Buffer (5) := '.';
676
677      elsif U_Id in Interfaces_Descendant then
678         Name_Buffer (11) := '.';
679
680         if U_Id in Interfaces_C_Descendant then
681            Name_Buffer (13) := '.';
682         end if;
683
684      elsif U_Id in System_Descendant then
685         Name_Buffer (7) := '.';
686
687         if U_Id in System_Dim_Descendant then
688            Name_Buffer (11) := '.';
689         end if;
690
691         if U_Id in System_Multiprocessors_Descendant then
692            Name_Buffer (23) := '.';
693         end if;
694
695         if U_Id in System_Storage_Pools_Descendant then
696            Name_Buffer (21) := '.';
697         end if;
698
699         if U_Id in System_Strings_Descendant then
700            Name_Buffer (15) := '.';
701         end if;
702
703         if U_Id in System_Tasking_Descendant then
704            Name_Buffer (15) := '.';
705         end if;
706
707         if U_Id in System_Tasking_Restricted_Descendant then
708            Name_Buffer (26) := '.';
709         end if;
710
711         if U_Id in System_Tasking_Protected_Objects_Descendant then
712            Name_Buffer (33) := '.';
713         end if;
714
715         if U_Id in System_Tasking_Async_Delays_Descendant then
716            Name_Buffer (28) := '.';
717         end if;
718      end if;
719
720      --  Add %s at end for spec
721
722      Name_Buffer (Name_Len + 1) := '%';
723      Name_Buffer (Name_Len + 2) := 's';
724      Name_Len := Name_Len + 2;
725
726      return Name_Find;
727   end Get_Unit_Name;
728
729   ----------------
730   -- Initialize --
731   ----------------
732
733   procedure Initialize is
734   begin
735      --  Initialize the unit table
736
737      for J in RTU_Id loop
738         RT_Unit_Table (J).Entity := Empty;
739         RT_Unit_Table (J).First_Implicit_With := Empty;
740      end loop;
741
742      for J in RE_Id loop
743         RE_Table (J) := Empty;
744      end loop;
745
746      RTE_Is_Available := False;
747   end Initialize;
748
749   ------------
750   -- Is_RTE --
751   ------------
752
753   function Is_RTE (Ent : Entity_Id; E : RE_Id) return Boolean is
754      E_Unit_Name   : Unit_Name_Type;
755      Ent_Unit_Name : Unit_Name_Type;
756
757      S  : Entity_Id;
758      E1 : Entity_Id;
759      E2 : Entity_Id;
760
761   begin
762      if No (Ent) then
763         return False;
764
765      --  If E has already a corresponding entity, check it directly,
766      --  going to full views if they exist to deal with the incomplete
767      --  and private type cases properly.
768
769      elsif Present (RE_Table (E)) then
770         E1 := Ent;
771
772         if Is_Type (E1) and then Present (Full_View (E1)) then
773            E1 := Full_View (E1);
774         end if;
775
776         E2 := RE_Table (E);
777
778         if Is_Type (E2) and then Present (Full_View (E2)) then
779            E2 := Full_View (E2);
780         end if;
781
782         return E1 = E2;
783      end if;
784
785      --  If the unit containing E is not loaded, we already know that the
786      --  entity we have cannot have come from this unit.
787
788      E_Unit_Name := Get_Unit_Name (RE_Unit_Table (E));
789
790      if not Is_Loaded (E_Unit_Name) then
791         return False;
792      end if;
793
794      --  Here the unit containing the entity is loaded. We have not made
795      --  an explicit call to RTE to get the entity in question, but we may
796      --  have obtained a reference to it indirectly from some other entity
797      --  in the same unit, or some other unit that references it.
798
799      --  Get the defining unit of the entity
800
801      S := Scope (Ent);
802
803      if No (S) or else Ekind (S) /= E_Package then
804         return False;
805      end if;
806
807      Ent_Unit_Name := Get_Unit_Name (Unit_Declaration_Node (S));
808
809      --  If the defining unit of the entity we are testing is not the
810      --  unit containing E, then they cannot possibly match.
811
812      if Ent_Unit_Name /= E_Unit_Name then
813         return False;
814      end if;
815
816      --  If the units match, then compare the names (remember that no
817      --  overloading is permitted in entities fetched using Rtsfind).
818
819      if RE_Chars (E) = Chars (Ent) then
820         RE_Table (E) := Ent;
821
822         --  If front-end inlining is enabled, we may be within a body that
823         --  contains inlined functions, which has not been retrieved through
824         --  rtsfind, and therefore is not yet recorded in the RT_Unit_Table.
825         --  Add the unit information now, it must be fully available.
826
827         declare
828            U : RT_Unit_Table_Record
829                  renames RT_Unit_Table (RE_Unit_Table (E));
830         begin
831            if No (U.Entity) then
832               U.Entity := S;
833               U.Uname  := E_Unit_Name;
834               U.Unum   := Get_Source_Unit (S);
835            end if;
836         end;
837
838         return True;
839      else
840         return False;
841      end if;
842   end Is_RTE;
843
844   ------------
845   -- Is_RTU --
846   ------------
847
848   function Is_RTU (Ent : Entity_Id;  U : RTU_Id) return Boolean is
849      E : constant Entity_Id := RT_Unit_Table (U).Entity;
850   begin
851      return Present (E) and then E = Ent;
852   end Is_RTU;
853
854   --------------------------------
855   -- Is_Text_IO_Special_Package --
856   --------------------------------
857
858   function Is_Text_IO_Special_Package (E : Entity_Id) return Boolean is
859   begin
860      pragma Assert (Is_Package_Or_Generic_Package (E));
861
862      --  ??? detection with a scope climbing might be more efficient
863
864      for U in Ada_Text_IO_Descendant loop
865         if Is_RTU (E, U) then
866            return True;
867         end if;
868      end loop;
869
870      for U in Ada_Wide_Text_IO_Descendant loop
871         if Is_RTU (E, U) then
872            return True;
873         end if;
874      end loop;
875
876      for U in Ada_Wide_Wide_Text_IO_Descendant loop
877         if Is_RTU (E, U) then
878            return True;
879         end if;
880      end loop;
881
882      return False;
883   end Is_Text_IO_Special_Package;
884
885   -----------------------------
886   -- Is_Text_IO_Special_Unit --
887   -----------------------------
888
889   function Is_Text_IO_Special_Unit (Nam : Node_Id) return Boolean is
890      Prf : Node_Id;
891      Sel : Node_Id;
892
893   begin
894      if Nkind (Nam) /= N_Expanded_Name then
895         return False;
896      end if;
897
898      Prf := Prefix (Nam);
899      Sel := Selector_Name (Nam);
900
901      if Nkind (Sel) /= N_Expanded_Name
902        or else Nkind (Prf) /= N_Identifier
903        or else Chars (Prf) /= Name_Ada
904      then
905         return False;
906      end if;
907
908      Prf := Prefix (Sel);
909      Sel := Selector_Name (Sel);
910
911      return
912        Nkind (Prf) = N_Identifier
913          and then
914            Chars (Prf) in Name_Text_IO
915                         | Name_Wide_Text_IO
916                         | Name_Wide_Wide_Text_IO
917          and then Nkind (Sel) = N_Identifier
918          and then Chars (Sel) in Text_IO_Package_Name;
919   end Is_Text_IO_Special_Unit;
920
921   ---------------
922   -- Load_Fail --
923   ---------------
924
925   procedure Load_Fail (S : String; U_Id : RTU_Id; Id : RE_Id) is
926      M : String (1 .. 100);
927      P : Natural := 0;
928
929   begin
930      --  Output header message
931
932      if Configurable_Run_Time_Mode then
933         RTE_Error_Msg ("construct not allowed in configurable run-time mode");
934      else
935         RTE_Error_Msg ("run-time library configuration error");
936      end if;
937
938      --  Output file name and reason string
939
940      M (1 .. 6) := "\file ";
941      P := 6;
942
943      Get_Name_String
944        (Get_File_Name (RT_Unit_Table (U_Id).Uname, Subunit => False));
945      M (P + 1 .. P + Name_Len) := Name_Buffer (1 .. Name_Len);
946      P := P + Name_Len;
947
948      M (P + 1) := ' ';
949      P := P + 1;
950
951      M (P + 1 .. P + S'Length) := S;
952      P := P + S'Length;
953
954      RTE_Error_Msg (M (1 .. P));
955
956      --  Output entity name
957
958      Output_Entity_Name (Id, "not available");
959
960      --  In configurable run time mode, we raise RE_Not_Available, and the
961      --  caller is expected to deal gracefully with this. In the case of a
962      --  call to RTE_Available, this exception will be caught in Rtsfind,
963      --  and result in a returned value of False for the call.
964
965      if Configurable_Run_Time_Mode then
966         raise RE_Not_Available;
967
968      --  Here we have a load failure in normal full run time mode. See if we
969      --  are in the context of an RTE_Available call. If so, we just raise
970      --  RE_Not_Available. This can happen if a unit is unavailable, which
971      --  happens for example in the VM case, where the run-time is not
972      --  complete, but we do not regard it as a configurable run-time.
973      --  If the caller has done an explicit call to RTE_Available, then
974      --  clearly the caller is prepared to deal with a result of False.
975
976      elsif RTE_Available_Call then
977         RTE_Is_Available := False;
978         raise RE_Not_Available;
979
980      --  If we are not in the context of an RTE_Available call, we are really
981      --  trying to load an entity that is not there, and that should never
982      --  happen, so in this case we signal a fatal error.
983
984      else
985         raise Unrecoverable_Error;
986      end if;
987   end Load_Fail;
988
989   --------------
990   -- Load_RTU --
991   --------------
992
993   --  WARNING: This routine manages Ghost and SPARK regions. Return statements
994   --  must be replaced by gotos which jump to the end of the routine in order
995   --  to restore the Ghost and SPARK modes.
996
997   procedure Load_RTU
998     (U_Id        : RTU_Id;
999      Id          : RE_Id   := RE_Null;
1000      Use_Setting : Boolean := False)
1001   is
1002      U        : RT_Unit_Table_Record renames RT_Unit_Table (U_Id);
1003      Priv_Par : constant Elist_Id := New_Elmt_List;
1004      Lib_Unit : Node_Id;
1005
1006      procedure Save_Private_Visibility;
1007      --  If the current unit is the body of child unit or the spec of a
1008      --  private child unit, the private declarations of the parent(s) are
1009      --  visible. If the unit to be loaded is another public sibling, its
1010      --  compilation will affect the visibility of the common ancestors.
1011      --  Indicate those that must be restored.
1012
1013      procedure Restore_Private_Visibility;
1014      --  Restore the visibility of ancestors after compiling RTU
1015
1016      --------------------------------
1017      -- Restore_Private_Visibility --
1018      --------------------------------
1019
1020      procedure Restore_Private_Visibility is
1021         E_Par : Elmt_Id;
1022
1023      begin
1024         E_Par := First_Elmt (Priv_Par);
1025         while Present (E_Par) loop
1026            if not In_Private_Part (Node (E_Par)) then
1027               Install_Private_Declarations (Node (E_Par));
1028            end if;
1029
1030            Next_Elmt (E_Par);
1031         end loop;
1032      end Restore_Private_Visibility;
1033
1034      -----------------------------
1035      -- Save_Private_Visibility --
1036      -----------------------------
1037
1038      procedure Save_Private_Visibility is
1039         Par : Entity_Id;
1040
1041      begin
1042         Par := Scope (Current_Scope);
1043         while Present (Par)
1044           and then Par /= Standard_Standard
1045         loop
1046            if Ekind (Par) = E_Package
1047              and then Is_Compilation_Unit (Par)
1048              and then In_Private_Part (Par)
1049            then
1050               Append_Elmt (Par, Priv_Par);
1051            end if;
1052
1053            Par := Scope (Par);
1054         end loop;
1055      end Save_Private_Visibility;
1056
1057      --  Local variables
1058
1059      Saved_GM  : constant Ghost_Mode_Type := Ghost_Mode;
1060      Saved_IGR : constant Node_Id         := Ignored_Ghost_Region;
1061      Saved_ISMP : constant Boolean        :=
1062                     Ignore_SPARK_Mode_Pragmas_In_Instance;
1063      Saved_SM  : constant SPARK_Mode_Type := SPARK_Mode;
1064      Saved_SMP : constant Node_Id         := SPARK_Mode_Pragma;
1065      --  Save Ghost and SPARK mode-related data to restore on exit
1066
1067   --  Start of processing for Load_RTU
1068
1069   begin
1070      --  Nothing to do if unit is already loaded
1071
1072      if Present (U.Entity) then
1073         return;
1074      end if;
1075
1076      --  Provide a clean environment for the unit
1077
1078      Ignore_SPARK_Mode_Pragmas_In_Instance := False;
1079      Install_Ghost_Region (None, Empty);
1080      Install_SPARK_Mode   (None, Empty);
1081
1082      --  Otherwise we need to load the unit, First build unit name from the
1083      --  enumeration literal name in type RTU_Id.
1084
1085      U.Uname                := Get_Unit_Name (U_Id);
1086      U.First_Implicit_With  := Empty;
1087
1088      --  Now do the load call, note that setting Error_Node to Empty is a
1089      --  signal to Load_Unit that we will regard a failure to find the file as
1090      --  a fatal error, and that it should not output any kind of diagnostics,
1091      --  since we will take care of it here.
1092
1093      --  We save style checking switches and turn off style checking for
1094      --  loading the unit, since we don't want any style checking.
1095
1096      declare
1097         Save_Style_Check : constant Boolean := Style_Check;
1098      begin
1099         Style_Check := False;
1100         U.Unum :=
1101           Load_Unit
1102             (Load_Name  => U.Uname,
1103              Required   => False,
1104              Subunit    => False,
1105              Error_Node => Empty);
1106         Style_Check := Save_Style_Check;
1107      end;
1108
1109      --  Check for bad unit load
1110
1111      if U.Unum = No_Unit then
1112         Load_Fail ("not found", U_Id, Id);
1113      elsif Fatal_Error (U.Unum) = Error_Detected then
1114         Load_Fail ("had parser errors", U_Id, Id);
1115      end if;
1116
1117      --  Make sure that the unit is analyzed
1118
1119      declare
1120         Was_Analyzed : constant Boolean :=
1121                          Analyzed (Cunit (Current_Sem_Unit));
1122
1123      begin
1124         --  Pretend that the current unit is analyzed, in case it is System
1125         --  or some such. This allows us to put some declarations, such as
1126         --  exceptions and packed arrays of Boolean, into System even though
1127         --  expanding them requires System...
1128
1129         --  This is a bit odd but works fine. If the RTS unit does not depend
1130         --  in any way on the current unit, then it never gets back into the
1131         --  current unit's tree, and the change we make to the current unit
1132         --  tree is never noticed by anyone (it is undone in a moment). That
1133         --  is the normal situation.
1134
1135         --  If the RTS Unit *does* depend on the current unit, for instance,
1136         --  when you are compiling System, then you had better have finished
1137         --  analyzing the part of System that is depended on before you try to
1138         --  load the RTS Unit. This means having the code in System ordered in
1139         --  an appropriate manner.
1140
1141         Set_Analyzed (Cunit (Current_Sem_Unit), True);
1142
1143         if not Analyzed (Cunit (U.Unum)) then
1144
1145            --  If the unit is already loaded through a limited_with_clause,
1146            --  the relevant entities must already be available. We do not
1147            --  want to load and analyze the unit because this would create
1148            --  a real semantic dependence when the purpose of the limited_with
1149            --  is precisely to avoid such.
1150
1151            if From_Limited_With (Cunit_Entity (U.Unum)) then
1152               null;
1153
1154            else
1155               Save_Private_Visibility;
1156               Semantics (Cunit (U.Unum));
1157               Restore_Private_Visibility;
1158
1159               if Fatal_Error (U.Unum) = Error_Detected then
1160                  Load_Fail ("had semantic errors", U_Id, Id);
1161               end if;
1162            end if;
1163         end if;
1164
1165         --  Undo the pretence
1166
1167         Set_Analyzed (Cunit (Current_Sem_Unit), Was_Analyzed);
1168      end;
1169
1170      Lib_Unit := Unit (Cunit (U.Unum));
1171      U.Entity := Defining_Entity (Lib_Unit);
1172
1173      if Use_Setting then
1174         Set_Is_Potentially_Use_Visible (U.Entity, True);
1175      end if;
1176
1177      Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
1178      Restore_Ghost_Region (Saved_GM, Saved_IGR);
1179      Restore_SPARK_Mode   (Saved_SM, Saved_SMP);
1180   end Load_RTU;
1181
1182   --------------------
1183   -- Make_Unit_Name --
1184   --------------------
1185
1186   function Make_Unit_Name
1187     (U : RT_Unit_Table_Record;
1188      N : Node_Id) return Node_Id is
1189
1190      Nam  : Node_Id;
1191      Scop : Entity_Id;
1192
1193   begin
1194      Nam  := New_Occurrence_Of (U.Entity, Standard_Location);
1195      Scop := Scope (U.Entity);
1196
1197      if Nkind (N) = N_Defining_Program_Unit_Name then
1198         while Scop /= Standard_Standard loop
1199            Nam :=
1200              Make_Expanded_Name (Standard_Location,
1201                Chars  => Chars (U.Entity),
1202                Prefix => New_Occurrence_Of (Scop, Standard_Location),
1203                Selector_Name => Nam);
1204            Set_Entity (Nam, U.Entity);
1205
1206            Scop := Scope (Scop);
1207         end loop;
1208      end if;
1209
1210      return Nam;
1211   end Make_Unit_Name;
1212
1213   --------------------
1214   -- Maybe_Add_With --
1215   --------------------
1216
1217   procedure Maybe_Add_With (U : in out RT_Unit_Table_Record) is
1218   begin
1219      --  We do not need to generate a with_clause for a call issued from
1220      --  RTE_Component_Available. However, for CodePeer, we need these
1221      --  additional with's, because for a sequence like "if RTE_Available (X)
1222      --  then ... RTE (X)" the RTE call fails to create some necessary with's.
1223
1224      if RTE_Available_Call and not Generate_SCIL then
1225         return;
1226      end if;
1227
1228      --  Avoid creating directly self-referential with clauses
1229
1230      if Current_Sem_Unit = U.Unum then
1231         return;
1232      end if;
1233
1234      --  Add the with_clause, if we have not already added an implicit with
1235      --  for this unit to the current compilation unit.
1236
1237      declare
1238         LibUnit : constant Node_Id := Unit (Cunit (U.Unum));
1239         Clause  : Node_Id;
1240         Withn   : Node_Id;
1241
1242      begin
1243         Clause := U.First_Implicit_With;
1244         while Present (Clause) loop
1245            if Parent (Clause) = Cunit (Current_Sem_Unit) then
1246               return;
1247            end if;
1248
1249            Clause := Next_Implicit_With (Clause);
1250         end loop;
1251
1252         Withn :=
1253           Make_With_Clause (Standard_Location,
1254             Name =>
1255               Make_Unit_Name
1256                 (U, Defining_Unit_Name (Specification (LibUnit))));
1257
1258         Set_Corresponding_Spec  (Withn, U.Entity);
1259         Set_First_Name          (Withn);
1260         Set_Implicit_With       (Withn);
1261         Set_Library_Unit        (Withn, Cunit (U.Unum));
1262         Set_Next_Implicit_With  (Withn, U.First_Implicit_With);
1263
1264         U.First_Implicit_With := Withn;
1265
1266         Mark_Rewrite_Insertion (Withn);
1267         Append (Withn, Context_Items (Cunit (Current_Sem_Unit)));
1268         Check_Restriction_No_Dependence (Name (Withn), Current_Error_Node);
1269      end;
1270   end Maybe_Add_With;
1271
1272   ------------------------
1273   -- Output_Entity_Name --
1274   ------------------------
1275
1276   procedure Output_Entity_Name (Id : RE_Id; Msg : String) is
1277      M : String (1 .. 2048);
1278      P : Natural := 0;
1279      --  M (1 .. P) is current message to be output
1280
1281      RE_Image : constant String := RE_Id'Image (Id);
1282      S : Natural;
1283      --  RE_Image (S .. RE_Image'Last) is the name of the entity without the
1284      --  "RE_" or "RO_XX_" prefix.
1285
1286   begin
1287      if Id = RE_Null then
1288         return;
1289      end if;
1290
1291      M (1 .. 9) := "\entity """;
1292      P := 9;
1293
1294      --  Add unit name to message, excluding %s or %b at end
1295
1296      Get_Name_String (Get_Unit_Name (RE_Unit_Table (Id)));
1297      Name_Len := Name_Len - 2;
1298      Set_Casing (Mixed_Case);
1299      M (P + 1 .. P + Name_Len) := Name_Buffer (1 .. Name_Len);
1300      P := P + Name_Len;
1301
1302      --  Add a qualifying period
1303
1304      M (P + 1) := '.';
1305      P := P + 1;
1306
1307      --  Strip "RE"
1308
1309      if RE_Image (2) = 'E' then
1310         S := 4;
1311
1312      --  Strip "RO_XX"
1313
1314      else
1315         S := 7;
1316      end if;
1317
1318      --  Add entity name and closing quote to message
1319
1320      Name_Len := RE_Image'Length - S + 1;
1321      Name_Buffer (1 .. Name_Len) := RE_Image (S .. RE_Image'Last);
1322      Set_Casing (Mixed_Case);
1323      M (P + 1 .. P + Name_Len) := Name_Buffer (1 .. Name_Len);
1324      P := P + Name_Len;
1325      M (P + 1) := '"';
1326      P := P + 1;
1327
1328      --  Add message
1329
1330      M (P + 1) := ' ';
1331      P := P + 1;
1332      M (P + 1 .. P + Msg'Length) := Msg;
1333      P := P + Msg'Length;
1334
1335      --  Output message at current error node location
1336
1337      RTE_Error_Msg (M (1 .. P));
1338   end Output_Entity_Name;
1339
1340   --------------
1341   -- RE_Chars --
1342   --------------
1343
1344   function RE_Chars (E : RE_Id) return Name_Id is
1345      RE_Name_Chars : constant String := RE_Id'Image (E);
1346
1347   begin
1348      --  Copy name skipping initial RE_ or RO_XX characters
1349
1350      if RE_Name_Chars (1 .. 2) = "RE" then
1351         for J in 4 .. RE_Name_Chars'Last loop
1352            Name_Buffer (J - 3) := Fold_Lower (RE_Name_Chars (J));
1353         end loop;
1354
1355         Name_Len := RE_Name_Chars'Length - 3;
1356
1357      else
1358         for J in 7 .. RE_Name_Chars'Last loop
1359            Name_Buffer (J - 6) := Fold_Lower (RE_Name_Chars (J));
1360         end loop;
1361
1362         Name_Len := RE_Name_Chars'Length - 6;
1363      end if;
1364
1365      return Name_Find;
1366   end RE_Chars;
1367
1368   ---------
1369   -- RTE --
1370   ---------
1371
1372   function RTE (E : RE_Id) return Entity_Id is
1373      procedure Check_RPC;
1374      --  Reject programs that make use of distribution features not supported
1375      --  on the current target. Also check that the PCS is compatible with the
1376      --  code generator version. On such targets (Vxworks, others?) we provide
1377      --  a minimal body for System.Rpc that only supplies an implementation of
1378      --  Partition_Id.
1379
1380      function Find_Local_Entity (E : RE_Id) return Entity_Id;
1381      --  This function is used when entity E is in this compilation's main
1382      --  unit. It gets the value from the already compiled declaration.
1383
1384      ---------------
1385      -- Check_RPC --
1386      ---------------
1387
1388      procedure Check_RPC is
1389      begin
1390         --  Bypass this check if debug flag -gnatdR set
1391
1392         if Debug_Flag_RR then
1393            return;
1394         end if;
1395
1396         --  Otherwise we need the check if we are going after one of the
1397         --  critical entities in System.RPC / System.Partition_Interface.
1398
1399         if E = RE_Do_Rpc
1400              or else
1401            E = RE_Do_Apc
1402              or else
1403            E = RE_Params_Stream_Type
1404              or else
1405            E = RE_Request_Access
1406         then
1407            --  If generating RCI stubs, check that we have a real PCS
1408
1409            if (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
1410                  or else
1411                Distribution_Stub_Mode = Generate_Caller_Stub_Body)
1412              and then Get_PCS_Name = Name_No_DSA
1413            then
1414               Set_Standard_Error;
1415               Write_Str ("distribution feature not supported");
1416               Write_Eol;
1417               raise Unrecoverable_Error;
1418
1419            --  In all cases, check Exp_Dist and System.Partition_Interface
1420            --  consistency.
1421
1422            elsif Get_PCS_Version /=
1423                    Exp_Dist.PCS_Version_Number (Get_PCS_Name)
1424            then
1425               Set_Standard_Error;
1426               Write_Str ("PCS version mismatch: expander ");
1427               Write_Int (Exp_Dist.PCS_Version_Number (Get_PCS_Name));
1428               Write_Str (", PCS (");
1429               Write_Name (Get_PCS_Name);
1430               Write_Str (") ");
1431               Write_Int (Get_PCS_Version);
1432               Write_Eol;
1433               raise Unrecoverable_Error;
1434            end if;
1435         end if;
1436      end Check_RPC;
1437
1438      -----------------------
1439      -- Find_Local_Entity --
1440      -----------------------
1441
1442      function Find_Local_Entity (E : RE_Id) return Entity_Id is
1443         RE_Str : constant String := RE_Id'Image (E);
1444         Nam    : Name_Id;
1445         Ent    : Entity_Id;
1446
1447         Save_Nam : constant String := Name_Buffer (1 .. Name_Len);
1448         --  Save name buffer and length over call
1449
1450      begin
1451         Name_Len := Natural'Max (0, RE_Str'Length - 3);
1452         Name_Buffer (1 .. Name_Len) :=
1453           RE_Str (RE_Str'First + 3 .. RE_Str'Last);
1454
1455         Nam := Name_Find;
1456         Ent := Entity_Id (Get_Name_Table_Int (Nam));
1457
1458         Name_Len := Save_Nam'Length;
1459         Name_Buffer (1 .. Name_Len) := Save_Nam;
1460
1461         return Ent;
1462      end Find_Local_Entity;
1463
1464      --  Local variables
1465
1466      U_Id : constant RTU_Id := RE_Unit_Table (E);
1467      U    : RT_Unit_Table_Record renames RT_Unit_Table (U_Id);
1468
1469      Ename    : Name_Id;
1470      Lib_Unit : Node_Id;
1471      Pkg_Ent  : Entity_Id;
1472
1473      Save_Front_End_Inlining : constant Boolean := Front_End_Inlining;
1474      --  This flag is used to disable front-end inlining when RTE is invoked.
1475      --  This prevents the analysis of other runtime bodies when a particular
1476      --  spec is loaded through Rtsfind. This is both efficient, and prevents
1477      --  spurious visibility conflicts between use-visible user entities, and
1478      --  entities in run-time packages.
1479
1480   --  Start of processing for RTE
1481
1482   begin
1483      --  Doing a rtsfind in system.ads is special, as we cannot do this
1484      --  when compiling System itself. So if we are compiling system then
1485      --  we should already have acquired and processed the declaration
1486      --  of the entity. The test is to see if this compilation's main unit
1487      --  is System. If so, return the value from the already compiled
1488      --  declaration and otherwise do a regular find.
1489
1490      --  Not pleasant, but these kinds of annoying recursion scenarios when
1491      --  writing an Ada compiler in Ada have to be broken somewhere.
1492
1493      if Present (Main_Unit_Entity)
1494        and then Chars (Main_Unit_Entity) = Name_System
1495        and then Analyzed (Main_Unit_Entity)
1496        and then not Is_Child_Unit (Main_Unit_Entity)
1497      then
1498         return Check_CRT (E, Find_Local_Entity (E));
1499      end if;
1500
1501      Front_End_Inlining := False;
1502
1503      --  Load unit if unit not previously loaded
1504
1505      if No (RE_Table (E)) then
1506         Load_RTU (U_Id, Id => E);
1507         Lib_Unit := Unit (Cunit (U.Unum));
1508
1509         --  In the subprogram case, we are all done, the entity we want
1510         --  is the entity for the subprogram itself. Note that we do not
1511         --  bother to check that it is the entity that was requested.
1512         --  the only way that could fail to be the case is if runtime is
1513         --  hopelessly misconfigured, and it isn't worth testing for this.
1514
1515         if Nkind (Lib_Unit) = N_Subprogram_Declaration then
1516            RE_Table (E) := U.Entity;
1517
1518         --  Otherwise we must have the package case. First check package
1519         --  entity itself (e.g. RTE_Name for System.Interrupts.Name)
1520
1521         else
1522            pragma Assert (Nkind (Lib_Unit) = N_Package_Declaration);
1523            Ename := RE_Chars (E);
1524
1525            --  First we search the package entity chain. If the package
1526            --  only has a limited view, scan the corresponding list of
1527            --  incomplete types.
1528
1529            if From_Limited_With (U.Entity) then
1530               Pkg_Ent := First_Entity (Limited_View (U.Entity));
1531            else
1532               Pkg_Ent := First_Entity (U.Entity);
1533            end if;
1534
1535            while Present (Pkg_Ent) loop
1536               if Ename = Chars (Pkg_Ent) then
1537                  RE_Table (E) := Pkg_Ent;
1538                  Check_RPC;
1539                  goto Found;
1540               end if;
1541
1542               Next_Entity (Pkg_Ent);
1543            end loop;
1544
1545            --  If we did not find the entity in the package entity chain,
1546            --  then check if the package entity itself matches. Note that
1547            --  we do this check after searching the entity chain, since
1548            --  the rule is that in case of ambiguity, we prefer the entity
1549            --  defined within the package, rather than the package itself.
1550
1551            if Ename = Chars (U.Entity) then
1552               RE_Table (E) := U.Entity;
1553            end if;
1554
1555            --  If we didn't find the entity we want, something is wrong.
1556            --  We just leave RE_Table (E) set to Empty and the appropriate
1557            --  action will be taken by Check_CRT when we exit.
1558
1559         end if;
1560      end if;
1561
1562   <<Found>>
1563
1564      --  Record whether the secondary stack is in use in order to generate
1565      --  the proper binder code. No action is taken when the secondary stack
1566      --  is pulled within an ignored Ghost context because all this code will
1567      --  disappear.
1568
1569      if U_Id = System_Secondary_Stack and then Ghost_Mode /= Ignore then
1570         Sec_Stack_Used := True;
1571      end if;
1572
1573      Maybe_Add_With (U);
1574      Front_End_Inlining := Save_Front_End_Inlining;
1575
1576      return Check_CRT (E, RE_Table (E));
1577   end RTE;
1578
1579   -------------------
1580   -- RTE_Available --
1581   -------------------
1582
1583   function RTE_Available (E : RE_Id) return Boolean is
1584      Dummy : Entity_Id;
1585      pragma Warnings (Off, Dummy);
1586
1587      Result : Boolean;
1588
1589      Save_RTE_Available_Call : constant Boolean := RTE_Available_Call;
1590      Save_RTE_Is_Available   : constant Boolean := RTE_Is_Available;
1591      --  These are saved recursively because the call to load a unit
1592      --  caused by an upper level call may perform a recursive call
1593      --  to this routine during analysis of the corresponding unit.
1594
1595   begin
1596      RTE_Available_Call := True;
1597      RTE_Is_Available := True;
1598      Dummy := RTE (E);
1599      Result := RTE_Is_Available;
1600      RTE_Available_Call := Save_RTE_Available_Call;
1601      RTE_Is_Available   := Save_RTE_Is_Available;
1602      return Result;
1603
1604   exception
1605      when RE_Not_Available =>
1606         RTE_Available_Call := Save_RTE_Available_Call;
1607         RTE_Is_Available   := Save_RTE_Is_Available;
1608         return False;
1609   end RTE_Available;
1610
1611   --------------------------
1612   -- RTE_Record_Component --
1613   --------------------------
1614
1615   function RTE_Record_Component (E : RE_Id) return Entity_Id is
1616      U_Id     : constant RTU_Id := RE_Unit_Table (E);
1617      U        : RT_Unit_Table_Record renames RT_Unit_Table (U_Id);
1618      E1       : Entity_Id;
1619      Ename    : Name_Id;
1620      Found_E  : Entity_Id;
1621      Lib_Unit : Node_Id;
1622      Pkg_Ent  : Entity_Id;
1623
1624      --  The following flag is used to disable front-end inlining when
1625      --  RTE_Record_Component is invoked. This prevents the analysis of other
1626      --  runtime bodies when a particular spec is loaded through Rtsfind. This
1627      --  is both efficient, and it prevents spurious visibility conflicts
1628      --  between use-visible user entities, and entities in run-time packages.
1629
1630      Save_Front_End_Inlining : Boolean;
1631
1632   begin
1633      --  Note: Contrary to subprogram RTE, there is no need to do any special
1634      --  management with package system.ads because it has no record type
1635      --  declarations.
1636
1637      Save_Front_End_Inlining := Front_End_Inlining;
1638      Front_End_Inlining      := False;
1639
1640      --  Load unit if unit not previously loaded
1641
1642      if not Present (U.Entity) then
1643         Load_RTU (U_Id, Id => E);
1644      end if;
1645
1646      Lib_Unit := Unit (Cunit (U.Unum));
1647
1648      pragma Assert (Nkind (Lib_Unit) = N_Package_Declaration);
1649      Ename := RE_Chars (E);
1650
1651      --  Search the entity in the components of record type declarations
1652      --  found in the package entity chain.
1653
1654      Found_E := Empty;
1655      Pkg_Ent := First_Entity (U.Entity);
1656      Search : while Present (Pkg_Ent) loop
1657         if Is_Record_Type (Pkg_Ent) then
1658            E1 := First_Entity (Pkg_Ent);
1659            while Present (E1) loop
1660               if Ename = Chars (E1) then
1661                  pragma Assert (not Present (Found_E));
1662                  Found_E := E1;
1663               end if;
1664
1665               Next_Entity (E1);
1666            end loop;
1667         end if;
1668
1669         Next_Entity (Pkg_Ent);
1670      end loop Search;
1671
1672      --  If we didn't find the entity we want, something is wrong. The
1673      --  appropriate action will be taken by Check_CRT when we exit.
1674
1675      Maybe_Add_With (U);
1676
1677      Front_End_Inlining := Save_Front_End_Inlining;
1678      return Check_CRT (E, Found_E);
1679   end RTE_Record_Component;
1680
1681   ------------------------------------
1682   -- RTE_Record_Component_Available --
1683   ------------------------------------
1684
1685   function RTE_Record_Component_Available (E : RE_Id) return Boolean is
1686      Dummy : Entity_Id;
1687      pragma Warnings (Off, Dummy);
1688
1689      Result : Boolean;
1690
1691      Save_RTE_Available_Call : constant Boolean := RTE_Available_Call;
1692      Save_RTE_Is_Available   : constant Boolean := RTE_Is_Available;
1693      --  These are saved recursively because the call to load a unit
1694      --  caused by an upper level call may perform a recursive call
1695      --  to this routine during analysis of the corresponding unit.
1696
1697   begin
1698      RTE_Available_Call := True;
1699      RTE_Is_Available := True;
1700      Dummy := RTE_Record_Component (E);
1701      Result := RTE_Is_Available;
1702      RTE_Available_Call := Save_RTE_Available_Call;
1703      RTE_Is_Available   := Save_RTE_Is_Available;
1704      return Result;
1705
1706   exception
1707      when RE_Not_Available =>
1708         RTE_Available_Call := Save_RTE_Available_Call;
1709         RTE_Is_Available   := Save_RTE_Is_Available;
1710         return False;
1711   end RTE_Record_Component_Available;
1712
1713   -------------------
1714   -- RTE_Error_Msg --
1715   -------------------
1716
1717   procedure RTE_Error_Msg (Msg : String) is
1718   begin
1719      if RTE_Available_Call then
1720         RTE_Is_Available := False;
1721      else
1722         Error_Msg_N (Msg, Current_Error_Node);
1723
1724         --  Bump count of violations if we are in configurable run-time
1725         --  mode and this is not a continuation message.
1726
1727         if Configurable_Run_Time_Mode and then Msg (Msg'First) /= '\' then
1728            Configurable_Run_Time_Violations :=
1729              Configurable_Run_Time_Violations + 1;
1730         end if;
1731      end if;
1732   end RTE_Error_Msg;
1733
1734   ----------------
1735   -- RTU_Entity --
1736   ----------------
1737
1738   function RTU_Entity (U : RTU_Id) return Entity_Id is
1739   begin
1740      return RT_Unit_Table (U).Entity;
1741   end RTU_Entity;
1742
1743   ----------------
1744   -- RTU_Loaded --
1745   ----------------
1746
1747   function RTU_Loaded (U : RTU_Id) return Boolean is
1748   begin
1749      return Present (RT_Unit_Table (U).Entity);
1750   end RTU_Loaded;
1751
1752   --------------------
1753   -- Set_RTU_Loaded --
1754   --------------------
1755
1756   procedure Set_RTU_Loaded (N : Node_Id) is
1757      Loc   : constant Source_Ptr       := Sloc (N);
1758      Unum  : constant Unit_Number_Type := Get_Source_Unit (Loc);
1759      Uname : constant Unit_Name_Type   := Unit_Name (Unum);
1760      E     : constant Entity_Id        :=
1761                Defining_Entity (Unit (Cunit (Unum)));
1762   begin
1763      pragma Assert (Is_Predefined_Unit (Unum));
1764
1765      --  Loop through entries in RTU table looking for matching entry
1766
1767      for U_Id in RTU_Id'Range loop
1768
1769         --  Here we have a match
1770
1771         if Get_Unit_Name (U_Id) = Uname then
1772            declare
1773               U : RT_Unit_Table_Record renames RT_Unit_Table (U_Id);
1774               --  The RT_Unit_Table entry that may need updating
1775
1776            begin
1777               --  If entry is not set, set it now, and indicate that it was
1778               --  loaded through an explicit context clause.
1779
1780               if No (U.Entity) then
1781                  U := (Entity               => E,
1782                        Uname                => Get_Unit_Name (U_Id),
1783                        Unum                 => Unum,
1784                        First_Implicit_With  => Empty);
1785               end if;
1786
1787               return;
1788            end;
1789         end if;
1790      end loop;
1791   end Set_RTU_Loaded;
1792
1793   -------------------------
1794   -- SPARK_Implicit_Load --
1795   -------------------------
1796
1797   procedure SPARK_Implicit_Load (E : RE_Id) is
1798      Unused : Entity_Id;
1799
1800   begin
1801      pragma Assert (GNATprove_Mode);
1802
1803      --  Force loading of a predefined unit
1804
1805      Unused := RTE (E);
1806   end SPARK_Implicit_Load;
1807
1808end Rtsfind;
1809