OSDN Git Service

2005-03-17 Thomas Quinot <quinot@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / rtsfind.adb
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-2005, 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 2,  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 COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Atree;    use Atree;
28 with Casing;   use Casing;
29 with Csets;    use Csets;
30 with Debug;    use Debug;
31 with Einfo;    use Einfo;
32 with Elists;   use Elists;
33 with Errout;   use Errout;
34 with Fname;    use Fname;
35 with Fname.UF; use Fname.UF;
36 with Lib;      use Lib;
37 with Lib.Load; use Lib.Load;
38 with Namet;    use Namet;
39 with Nlists;   use Nlists;
40 with Nmake;    use Nmake;
41 with Output;   use Output;
42 with Opt;      use Opt;
43 with Restrict; use Restrict;
44 with Sem;      use Sem;
45 with Sem_Ch7;  use Sem_Ch7;
46 with Sem_Dist; use Sem_Dist;
47 with Sem_Util; use Sem_Util;
48 with Sinfo;    use Sinfo;
49 with Stand;    use Stand;
50 with Snames;   use Snames;
51 with Tbuild;   use Tbuild;
52 with Uname;    use Uname;
53
54 package body Rtsfind is
55
56    RTE_Available_Call : Boolean := False;
57    --  Set True during call to RTE from RTE_Available. Tells RTE to set
58    --  RTE_Is_Available to False rather than generating an error message.
59
60    RTE_Is_Available : Boolean;
61    --  Set True by RTE_Available on entry. When RTE_Available_Call is set
62    --  True, set False if RTE would otherwise generate an error message.
63
64    ----------------
65    -- Unit table --
66    ----------------
67
68    --  The unit table has one entry for each unit included in the definition
69    --  of the type RTU_Id in the spec. The table entries are initialized in
70    --  Initialize to set the Entity field to Empty, indicating that the
71    --  corresponding unit has not yet been loaded. The fields are set when
72    --  a unit is loaded to contain the defining entity for the unit, the
73    --  unit name, and the unit number.
74
75    --  Note that a unit can be loaded either by a call to find an entity
76    --  within the unit (e.g. RTE), or by an explicit with of the unit. In
77    --  the latter case it is critical to make a call to Set_RTU_Loaded to
78    --  ensure that the entry in this table reflects the load.
79
80    type RT_Unit_Table_Record is record
81       Entity : Entity_Id;
82       Uname  : Unit_Name_Type;
83       Unum   : Unit_Number_Type;
84       Withed : Boolean;
85    end record;
86
87    RT_Unit_Table : array (RTU_Id) of RT_Unit_Table_Record;
88
89    --------------------------
90    -- Runtime Entity Table --
91    --------------------------
92
93    --  There is one entry in the runtime entity table for each entity that is
94    --  included in the definition of the RE_Id type in the spec. The entries
95    --  are set by Initialize_Rtsfind to contain Empty, indicating that the
96    --  entity has not yet been located. Once the entity is located for the
97    --  first time, its ID is stored in this array, so that subsequent calls
98    --  for the same entity can be satisfied immediately.
99
100    RE_Table : array (RE_Id) of Entity_Id;
101
102    --------------------------
103    -- Generation of WITH's --
104    --------------------------
105
106    --  When a unit is implicitly loaded as a result of a call to RTE, it
107    --  is necessary to create an implicit WITH to ensure that the object
108    --  is correctly loaded by the binder. Such WITH statements are only
109    --  required when the request is from the extended main unit (if a
110    --  client needs a WITH, that will be taken care of when the client
111    --  is compiled).
112
113    --  We always attach the WITH to the main unit. This is not perfectly
114    --  accurate in terms of elaboration requirements, but it is close
115    --  enough, since the units that are accessed using rtsfind do not
116    --  have delicate elaboration requirements.
117
118    --  The flag Withed in the unit table record is initially set to False.
119    --  It is set True if a WITH has been generated for the main unit for
120    --  the corresponding unit.
121
122    -----------------------
123    -- Local Subprograms --
124    -----------------------
125
126    procedure Entity_Not_Defined (Id : RE_Id);
127    --  Outputs error messages for an entity that is not defined in the
128    --  run-time library (the form of the error message is tailored for
129    --  no run time/configurable run time mode as required).
130
131    procedure Load_Fail (S : String; U_Id : RTU_Id; Id : RE_Id);
132    --  Internal procedure called if we can't sucessfully locate or
133    --  process a run-time unit. The parameters give information about
134    --  the error message to be given. S is a reason for failing to
135    --  compile the file and U_Id is the unit id. RE_Id is the RE_Id
136    --  originally passed to RTE. The message in S is one of the
137    --  following:
138    --
139    --     "not found"
140    --     "had parser errors"
141    --     "had semantic errors"
142    --
143    --  The "not found" case is treated specially in that it is considered
144    --  a normal situation in configurable run-time mode (and the message in
145    --  this case is suppressed unless we are operating in All_Errors_Mode).
146
147    function Get_Unit_Name (U_Id : RTU_Id) return Unit_Name_Type;
148    --  Retrieves the Unit Name given a unit id represented by its
149    --  enumeration value in RTU_Id.
150
151    procedure Load_RTU
152      (U_Id        : RTU_Id;
153       Id          : RE_Id   := RE_Null;
154       Use_Setting : Boolean := False);
155    --  Load the unit whose Id is given if not already loaded. The unit is
156    --  loaded, analyzed, and added to the WITH list, and the entry in
157    --  RT_Unit_Table is updated to reflect the load. Use_Setting is used
158    --  to indicate the initial setting for the Is_Potentially_Use_Visible
159    --  flag of the entity for the loaded unit (if it is indeed loaded).
160    --  A value of False means nothing special need be done. A value of
161    --  True indicates that this flag must be set to True. It is needed
162    --  only in the Text_IO_Kludge procedure, which may materialize an
163    --  entity of Text_IO (or [Wide_]Wide_Text_IO) that was previously unknown.
164    --  Id is the RE_Id value of the entity which was originally requested.
165    --  Id is used only for error message detail, and if it is RE_Null, then
166    --  the attempt to output the entity name is ignored.
167
168    procedure Output_Entity_Name (Id : RE_Id; Msg : String);
169    --  Output continuation error message giving qualified name of entity
170    --  corresponding to Id, appending the string given by Msg. This call
171    --  is only effective in All_Errors mode.
172
173    function RE_Chars (E : RE_Id) return Name_Id;
174    --  Given a RE_Id value returns the Chars of the corresponding entity.
175
176    procedure RTE_Error_Msg (Msg : String);
177    --  Generates a message by calling Error_Msg_N specifying Current_Error_Node
178    --  as the node location using the given Msg text. Special processing in the
179    --  case where RTE_Available_Call is set. In this case, no message is output
180    --  and instead RTE_Is_Available is set to False. Note that this can only be
181    --  used if you are sure that the message comes directly or indirectly from
182    --  a call to the RTE function.
183
184    ------------------------
185    -- Entity_Not_Defined --
186    ------------------------
187
188    procedure Entity_Not_Defined (Id : RE_Id) is
189    begin
190       if No_Run_Time_Mode then
191
192          --  If the error occurs when compiling the body of a predefined
193          --  unit for inlining purposes, the body must be illegal in this
194          --  mode, and there is no point in continuing.
195
196          if Is_Predefined_File_Name
197            (Unit_File_Name (Get_Source_Unit (Sloc (Current_Error_Node))))
198          then
199             Error_Msg_N
200               ("construct not allowed in no run time mode!",
201                  Current_Error_Node);
202             raise Unrecoverable_Error;
203
204          else
205             RTE_Error_Msg ("|construct not allowed in no run time mode");
206          end if;
207
208       elsif Configurable_Run_Time_Mode then
209          RTE_Error_Msg ("|construct not allowed in this configuration>");
210       else
211          RTE_Error_Msg ("run-time configuration error");
212       end if;
213
214       Output_Entity_Name (Id, "not defined");
215    end Entity_Not_Defined;
216
217    -------------------
218    -- Get_Unit_Name --
219    -------------------
220
221    function Get_Unit_Name (U_Id : RTU_Id) return Unit_Name_Type is
222       Uname_Chars : constant String := RTU_Id'Image (U_Id);
223
224    begin
225       Name_Len := Uname_Chars'Length;
226       Name_Buffer (1 .. Name_Len) := Uname_Chars;
227       Set_Casing (All_Lower_Case);
228
229       if U_Id in Ada_Child then
230          Name_Buffer (4) := '.';
231
232          if U_Id in Ada_Calendar_Child then
233             Name_Buffer (13) := '.';
234
235          elsif U_Id in Ada_Finalization_Child then
236             Name_Buffer (17) := '.';
237
238          elsif U_Id in Ada_Interrupts_Child then
239             Name_Buffer (15) := '.';
240
241          elsif U_Id in Ada_Real_Time_Child then
242             Name_Buffer (14) := '.';
243
244          elsif U_Id in Ada_Streams_Child then
245             Name_Buffer (12) := '.';
246
247          elsif U_Id in Ada_Text_IO_Child then
248             Name_Buffer (12) := '.';
249
250          elsif U_Id in Ada_Wide_Text_IO_Child then
251             Name_Buffer (17) := '.';
252
253          elsif U_Id in Ada_Wide_Wide_Text_IO_Child then
254             Name_Buffer (22) := '.';
255          end if;
256
257       elsif U_Id in Interfaces_Child then
258          Name_Buffer (11) := '.';
259
260       elsif U_Id in System_Child then
261          Name_Buffer (7) := '.';
262
263          if U_Id in System_Tasking_Child then
264             Name_Buffer (15) := '.';
265          end if;
266
267          if U_Id in System_Tasking_Restricted_Child then
268             Name_Buffer (26) := '.';
269          end if;
270
271          if U_Id in System_Tasking_Protected_Objects_Child then
272             Name_Buffer (33) := '.';
273          end if;
274
275          if U_Id in System_Tasking_Async_Delays_Child then
276             Name_Buffer (28) := '.';
277          end if;
278       end if;
279
280       --  Add %s at end for spec
281
282       Name_Buffer (Name_Len + 1) := '%';
283       Name_Buffer (Name_Len + 2) := 's';
284       Name_Len := Name_Len + 2;
285
286       return Name_Find;
287    end Get_Unit_Name;
288
289    ----------------
290    -- Initialize --
291    ----------------
292
293    procedure Initialize is
294    begin
295       --  Initialize the unit table
296
297       for J in RTU_Id loop
298          RT_Unit_Table (J).Entity := Empty;
299       end loop;
300
301       for J in RE_Id loop
302          RE_Table (J) := Empty;
303       end loop;
304
305       RTE_Is_Available := False;
306    end Initialize;
307
308    ------------
309    -- Is_RTE --
310    ------------
311
312    function Is_RTE (Ent : Entity_Id; E : RE_Id) return Boolean is
313       E_Unit_Name   : Unit_Name_Type;
314       Ent_Unit_Name : Unit_Name_Type;
315
316       S  : Entity_Id;
317       E1 : Entity_Id;
318       E2 : Entity_Id;
319
320    begin
321       if No (Ent) then
322          return False;
323
324       --  If E has already a corresponding entity, check it directly,
325       --  going to full views if they exist to deal with the incomplete
326       --  and private type cases properly.
327
328       elsif Present (RE_Table (E)) then
329          E1 := Ent;
330
331          if Is_Type (E1) and then Present (Full_View (E1)) then
332             E1 := Full_View (E1);
333          end if;
334
335          E2 := RE_Table (E);
336
337          if Is_Type (E2) and then Present (Full_View (E2)) then
338             E2 := Full_View (E2);
339          end if;
340
341          return E1 = E2;
342       end if;
343
344       --  If the unit containing E is not loaded, we already know that
345       --  the entity we have cannot have come from this unit.
346
347       E_Unit_Name := Get_Unit_Name (RE_Unit_Table (E));
348
349       if not Is_Loaded (E_Unit_Name) then
350          return False;
351       end if;
352
353       --  Here the unit containing the entity is loaded. We have not made
354       --  an explicit call to RTE to get the entity in question, but we may
355       --  have obtained a reference to it indirectly from some other entity
356       --  in the same unit, or some other unit that references it.
357
358       --  Get the defining unit of the entity
359
360       S := Scope (Ent);
361
362       if Ekind (S) /= E_Package then
363          return False;
364       end if;
365
366       Ent_Unit_Name := Get_Unit_Name (Unit_Declaration_Node (S));
367
368       --  If the defining unit of the entity we are testing is not the
369       --  unit containing E, then they cannot possibly match.
370
371       if Ent_Unit_Name /= E_Unit_Name then
372          return False;
373       end if;
374
375       --  If the units match, then compare the names (remember that no
376       --  overloading is permitted in entities fetched using Rtsfind).
377
378       if RE_Chars (E) = Chars (Ent) then
379          RE_Table (E) := Ent;
380
381          --  If front-end inlining is enabled, we may be within a body that
382          --  contains inlined functions, which has not been retrieved through
383          --  rtsfind, and therefore is not yet recorded in the RT_Unit_Table.
384          --  Add the unit information now, it must be fully available.
385
386          declare
387             U : RT_Unit_Table_Record
388                   renames  RT_Unit_Table (RE_Unit_Table (E));
389          begin
390             if No (U.Entity) then
391                U.Entity := S;
392                U.Uname  := E_Unit_Name;
393                U.Unum   := Get_Source_Unit (S);
394             end if;
395          end;
396
397          return True;
398       else
399          return False;
400       end if;
401    end Is_RTE;
402
403    ------------
404    -- Is_RTU --
405    ------------
406
407    function Is_RTU (Ent : Entity_Id;  U : RTU_Id) return Boolean is
408       E : constant Entity_Id := RT_Unit_Table (U).Entity;
409    begin
410       return Present (E) and then E = Ent;
411    end Is_RTU;
412
413    ----------------------------
414    -- Is_Text_IO_Kludge_Unit --
415    ----------------------------
416
417    function Is_Text_IO_Kludge_Unit (Nam : Node_Id) return Boolean is
418       Prf : Node_Id;
419       Sel : Node_Id;
420
421    begin
422       if Nkind (Nam) /= N_Expanded_Name then
423          return False;
424       end if;
425
426       Prf := Prefix (Nam);
427       Sel := Selector_Name (Nam);
428
429       if Nkind (Sel) /= N_Expanded_Name
430         or else Nkind (Prf) /= N_Identifier
431         or else Chars (Prf) /= Name_Ada
432       then
433          return False;
434       end if;
435
436       Prf := Prefix (Sel);
437       Sel := Selector_Name (Sel);
438
439       return
440         Nkind (Prf) = N_Identifier
441           and then
442            (Chars (Prf) = Name_Text_IO
443               or else
444             Chars (Prf) = Name_Wide_Text_IO
445               or else
446             Chars (Prf) = Name_Wide_Wide_Text_IO)
447           and then
448         Nkind (Sel) = N_Identifier
449           and then
450         Chars (Sel) in Text_IO_Package_Name;
451    end Is_Text_IO_Kludge_Unit;
452
453    ---------------
454    -- Load_Fail --
455    ---------------
456
457    procedure Load_Fail (S : String; U_Id : RTU_Id; Id : RE_Id) is
458       M : String (1 .. 100);
459       P : Natural := 0;
460
461    begin
462       --  Output header message
463
464       if Configurable_Run_Time_Mode then
465          RTE_Error_Msg ("construct not allowed in configurable run-time mode");
466       else
467          RTE_Error_Msg ("run-time library configuration error");
468       end if;
469
470       --  Output file name and reason string
471
472       if S /= "not found"
473         or else not Configurable_Run_Time_Mode
474         or else All_Errors_Mode
475       then
476          M (1 .. 6) := "\file ";
477          P := 6;
478
479          Get_Name_String
480            (Get_File_Name (RT_Unit_Table (U_Id).Uname, Subunit => False));
481          M (P + 1 .. P + Name_Len) := Name_Buffer (1 .. Name_Len);
482          P := P + Name_Len;
483
484          M (P + 1) := ' ';
485          P := P + 1;
486
487          M (P + 1 .. P + S'Length) := S;
488          P := P + S'Length;
489
490          RTE_Error_Msg (M (1 .. P));
491
492          --  Output entity name
493
494          Output_Entity_Name (Id, "not available");
495       end if;
496
497       raise RE_Not_Available;
498    end Load_Fail;
499
500    --------------
501    -- Load_RTU --
502    --------------
503
504    procedure Load_RTU
505      (U_Id        : RTU_Id;
506       Id          : RE_Id   := RE_Null;
507       Use_Setting : Boolean := False)
508    is
509       U        : RT_Unit_Table_Record renames RT_Unit_Table (U_Id);
510       Priv_Par : constant Elist_Id := New_Elmt_List;
511       Lib_Unit : Node_Id;
512
513       procedure Save_Private_Visibility;
514       --  If the current unit is the body of child unit or the spec of a
515       --  private child unit, the private declarations of the parent (s)
516       --  are visible. If the unit to be loaded is another public sibling,
517       --  its compilation will affect the visibility of the common ancestors.
518       --  Indicate those that must be restored.
519
520       procedure Restore_Private_Visibility;
521       --  Restore the visibility of ancestors after compiling RTU.
522
523       --------------------------------
524       -- Restore_Private_Visibility --
525       --------------------------------
526
527       procedure Restore_Private_Visibility is
528          E_Par : Elmt_Id;
529
530       begin
531          E_Par := First_Elmt (Priv_Par);
532
533          while Present (E_Par) loop
534             if not In_Private_Part (Node (E_Par)) then
535                Install_Private_Declarations (Node (E_Par));
536             end if;
537
538             Next_Elmt (E_Par);
539          end loop;
540       end Restore_Private_Visibility;
541
542       -----------------------------
543       -- Save_Private_Visibility --
544       -----------------------------
545
546       procedure Save_Private_Visibility is
547          Par : Entity_Id;
548
549       begin
550          Par := Scope (Current_Scope);
551
552          while Present (Par)
553            and then Par /= Standard_Standard
554          loop
555             if Ekind (Par) = E_Package
556               and then Is_Compilation_Unit (Par)
557               and then In_Private_Part (Par)
558             then
559                Append_Elmt (Par, Priv_Par);
560             end if;
561
562             Par := Scope (Par);
563          end loop;
564       end Save_Private_Visibility;
565
566    --  Start of processing for Load_RTU
567
568    begin
569       --  Nothing to do if unit is already loaded
570
571       if Present (U.Entity) then
572          return;
573       end if;
574
575       --  Note if secondary stack is used
576
577       if U_Id = System_Secondary_Stack then
578          Opt.Sec_Stack_Used := True;
579       end if;
580
581       --  Otherwise we need to load the unit, First build unit name
582       --  from the enumeration literal name in type RTU_Id.
583
584       U.Uname  := Get_Unit_Name (U_Id);
585       U.Withed := False;
586
587       declare
588          Loaded : Boolean;
589          pragma Warnings (Off, Loaded);
590       begin
591          Loaded := Is_Loaded (U.Uname);
592       end;
593
594       --  Now do the load call, note that setting Error_Node to Empty is
595       --  a signal to Load_Unit that we will regard a failure to find the
596       --  file as a fatal error, and that it should not output any kind
597       --  of diagnostics, since we will take care of it here.
598
599       U.Unum :=
600         Load_Unit
601           (Load_Name  => U.Uname,
602            Required   => False,
603            Subunit    => False,
604            Error_Node => Empty);
605
606       if U.Unum = No_Unit then
607          Load_Fail ("not found", U_Id, Id);
608       elsif Fatal_Error (U.Unum) then
609          Load_Fail ("had parser errors", U_Id, Id);
610       end if;
611
612       --  Make sure that the unit is analyzed
613
614       declare
615          Was_Analyzed : constant Boolean :=
616                           Analyzed (Cunit (Current_Sem_Unit));
617
618       begin
619          --  Pretend that the current unit is analyzed, in case it is System
620          --  or some such. This allows us to put some declarations, such as
621          --  exceptions and packed arrays of Boolean, into System even though
622          --  expanding them requires System...
623
624          --  This is a bit odd but works fine. If the RTS unit does not depend
625          --  in any way on the current unit, then it never gets back into the
626          --  current unit's tree, and the change we make to the current unit
627          --  tree is never noticed by anyone (it is undone in a moment). That
628          --  is the normal situation.
629
630          --  If the RTS Unit *does* depend on the current unit, for instance,
631          --  when you are compiling System, then you had better have finished
632          --  analyzing the part of System that is depended on before you try
633          --  to load the RTS Unit. This means having the System ordered in an
634          --  appropriate manner.
635
636          Set_Analyzed (Cunit (Current_Sem_Unit), True);
637
638          if not Analyzed (Cunit (U.Unum)) then
639             Save_Private_Visibility;
640             Semantics (Cunit (U.Unum));
641             Restore_Private_Visibility;
642
643             if Fatal_Error (U.Unum) then
644                Load_Fail ("had semantic errors", U_Id, Id);
645             end if;
646          end if;
647
648          --  Undo the pretence
649
650          Set_Analyzed (Cunit (Current_Sem_Unit), Was_Analyzed);
651       end;
652
653       Lib_Unit := Unit (Cunit (U.Unum));
654       U.Entity := Defining_Entity (Lib_Unit);
655
656       if Use_Setting then
657          Set_Is_Potentially_Use_Visible (U.Entity, True);
658       end if;
659    end Load_RTU;
660
661    -----------------------
662    -- Output_Entity_Name --
663    ------------------------
664
665    procedure Output_Entity_Name (Id : RE_Id; Msg : String) is
666       M : String (1 .. 2048);
667       P : Natural := 0;
668       --  M (1 .. P) is current message to be output
669
670       RE_Image : constant String := RE_Id'Image (Id);
671
672    begin
673       if Id = RE_Null or else not All_Errors_Mode then
674          return;
675       end if;
676
677       M (1 .. 9) := "\entity """;
678       P := 9;
679
680       --  Add unit name to message, excluding %s or %b at end
681
682       Get_Name_String (Get_Unit_Name (RE_Unit_Table (Id)));
683       Name_Len := Name_Len - 2;
684       Set_Casing (Mixed_Case);
685       M (P + 1 .. P + Name_Len) := Name_Buffer (1 .. Name_Len);
686       P := P + Name_Len;
687
688       --  Add a qualifying period
689
690       M (P + 1) := '.';
691       P := P + 1;
692
693       --  Add entity name and closing quote to message
694
695       Name_Len := RE_Image'Length - 3;
696       Name_Buffer (1 .. Name_Len) := RE_Image (4 .. RE_Image'Length);
697       Set_Casing (Mixed_Case);
698       M (P + 1 .. P + Name_Len) := Name_Buffer (1 .. Name_Len);
699       P := P + Name_Len;
700       M (P + 1) := '"';
701       P := P + 1;
702
703       --  Add message
704
705       M (P + 1) := ' ';
706       P := P + 1;
707       M (P + 1 .. P + Msg'Length) := Msg;
708       P := P + Msg'Length;
709
710       --  Output message at current error node location
711
712       RTE_Error_Msg (M (1 .. P));
713    end Output_Entity_Name;
714
715    --------------
716    -- RE_Chars --
717    --------------
718
719    function RE_Chars (E : RE_Id) return Name_Id is
720       RE_Name_Chars : constant String := RE_Id'Image (E);
721
722    begin
723       --  Copy name skipping initial RE_ or RO_XX characters
724
725       if RE_Name_Chars (1 .. 2) = "RE" then
726          for J in 4 .. RE_Name_Chars'Last loop
727             Name_Buffer (J - 3) := Fold_Lower (RE_Name_Chars (J));
728          end loop;
729
730          Name_Len := RE_Name_Chars'Length - 3;
731
732       else
733          for J in 7 .. RE_Name_Chars'Last loop
734             Name_Buffer (J - 6) := Fold_Lower (RE_Name_Chars (J));
735          end loop;
736
737          Name_Len := RE_Name_Chars'Length - 6;
738       end if;
739
740       return Name_Find;
741    end RE_Chars;
742
743    ---------
744    -- RTE --
745    ---------
746
747    function RTE (E : RE_Id) return Entity_Id is
748       U_Id : constant RTU_Id := RE_Unit_Table (E);
749       U    : RT_Unit_Table_Record renames RT_Unit_Table (U_Id);
750
751       Lib_Unit : Node_Id;
752       Pkg_Ent  : Entity_Id;
753       Ename    : Name_Id;
754
755       --  The following flag is used to disable front-end inlining when RTE
756       --  is invoked. This prevents the analysis of other runtime bodies when
757       --  a particular spec is loaded through Rtsfind. This is both efficient,
758       --  and it prevents spurious visibility conflicts between use-visible
759       --  user entities, and entities in run-time packages.
760
761       --  In configurable run-time mode, subprograms marked Inlined_Always must
762       --  be inlined, so in the case we retain the Front_End_Inlining mode.
763
764       Save_Front_End_Inlining : Boolean;
765
766       function Check_CRT (Eid : Entity_Id) return Entity_Id;
767       --  Check entity Eid to ensure that configurable run-time restrictions
768       --  are met. May generate an error message and raise RE_Not_Available
769       --  if the entity E does not exist (i.e. Eid is Empty)
770
771       procedure Check_RPC;
772       --  Reject programs that make use of distribution features not supported
773       --  on the current target. On such targets (VMS, Vxworks, others?) we
774       --  only provide a minimal body for System.Rpc that only supplies an
775       --  implementation of partition_id.
776
777       function Find_Local_Entity (E : RE_Id) return Entity_Id;
778       --  This function is used when entity E is in this compilation's main
779       --  unit. It gets the value from the already compiled declaration.
780
781       function Make_Unit_Name (N : Node_Id) return Node_Id;
782       --  If the unit is a child unit, build fully qualified name for use
783       --  in With_Clause.
784
785       ---------------
786       -- Check_CRT --
787       ---------------
788
789       function Check_CRT (Eid : Entity_Id) return Entity_Id is
790       begin
791          if No (Eid) then
792             Entity_Not_Defined (E);
793             raise RE_Not_Available;
794
795          --  Entity is available
796
797          else
798             --  If in No_Run_Time mode and entity is not in one of the
799             --  specially permitted units, raise the exception.
800
801             if No_Run_Time_Mode
802                  and then not OK_No_Run_Time_Unit (U_Id)
803             then
804                Entity_Not_Defined (E);
805                raise RE_Not_Available;
806             end if;
807
808             --  Otherwise entity is accessible
809
810             return Eid;
811          end if;
812       end Check_CRT;
813
814       ---------------
815       -- Check_RPC --
816       ---------------
817
818       procedure Check_RPC is
819       begin
820          --  Bypass this check if debug flag -gnatdR set
821
822          if Debug_Flag_RR then
823             return;
824          end if;
825
826          --  Otherwise we need the check if we are going after one of
827          --  the critical entities in System.RPC in stubs mode.
828
829          --  ??? Should we do this for other s-parint/s-polint entities
830          --  too?
831
832          if (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
833                       or else
834                         Distribution_Stub_Mode = Generate_Caller_Stub_Body)
835            and then (E = RE_Do_Rpc
836                        or else
837                      E = RE_Do_Apc
838                        or else
839                      E = RE_Params_Stream_Type
840                        or else
841                      E = RE_Request_Access)
842            and then Get_PCS_Name = Name_No_DSA
843          then
844             Set_Standard_Error;
845             Write_Str ("distribution feature not supported");
846             Write_Eol;
847             raise Unrecoverable_Error;
848          end if;
849       end Check_RPC;
850
851       ------------------------
852       -- Find_System_Entity --
853       ------------------------
854
855       function Find_Local_Entity (E : RE_Id) return Entity_Id is
856          RE_Str : String renames RE_Id'Image (E);
857          Ent    : Entity_Id;
858
859          Save_Nam : constant String := Name_Buffer (1 .. Name_Len);
860          --  Save name buffer and length over call
861
862       begin
863          Name_Len := Natural'Max (0, RE_Str'Length - 3);
864          Name_Buffer (1 .. Name_Len) :=
865            RE_Str (RE_Str'First + 3 .. RE_Str'Last);
866
867          Ent := Entity_Id (Get_Name_Table_Info (Name_Find));
868
869          Name_Len := Save_Nam'Length;
870          Name_Buffer (1 .. Name_Len) := Save_Nam;
871
872          return Ent;
873       end Find_Local_Entity;
874
875       --------------------
876       -- Make_Unit_Name --
877       --------------------
878
879       function Make_Unit_Name (N : Node_Id) return Node_Id is
880          Nam  : Node_Id;
881          Scop : Entity_Id;
882
883       begin
884          Nam  := New_Reference_To (U.Entity, Standard_Location);
885          Scop := Scope (U.Entity);
886
887          if Nkind (N) = N_Defining_Program_Unit_Name then
888             while Scop /= Standard_Standard loop
889                Nam :=
890                  Make_Expanded_Name (Standard_Location,
891                    Chars  => Chars (U.Entity),
892                    Prefix => New_Reference_To (Scop, Standard_Location),
893                    Selector_Name => Nam);
894                Set_Entity (Nam, U.Entity);
895
896                Scop := Scope (Scop);
897             end loop;
898          end if;
899
900          return Nam;
901       end Make_Unit_Name;
902
903    --  Start of processing for RTE
904
905    begin
906       --  Doing a rtsfind in system.ads is special, as we cannot do this
907       --  when compiling System itself. So if we are compiling system then
908       --  we should already have acquired and processed the declaration
909       --  of the entity. The test is to see if this compilation's main unit
910       --  is System. If so, return the value from the already compiled
911       --  declaration and otherwise do a regular find.
912
913       --  Not pleasant, but these kinds of annoying recursion when
914       --  writing an Ada compiler in Ada have to be broken somewhere!
915
916       if Present (Main_Unit_Entity)
917         and then Chars (Main_Unit_Entity) = Name_System
918         and then Analyzed (Main_Unit_Entity)
919         and then not Is_Child_Unit (Main_Unit_Entity)
920       then
921          return Check_CRT (Find_Local_Entity (E));
922       end if;
923
924       Save_Front_End_Inlining := Front_End_Inlining;
925       Front_End_Inlining := Configurable_Run_Time_Mode;
926
927       --  Load unit if unit not previously loaded
928
929       if No (RE_Table (E)) then
930          Load_RTU (U_Id, Id => E);
931          Lib_Unit := Unit (Cunit (U.Unum));
932
933          --  In the subprogram case, we are all done, the entity we want
934          --  is the entity for the subprogram itself. Note that we do not
935          --  bother to check that it is the entity that was requested.
936          --  the only way that could fail to be the case is if runtime is
937          --  hopelessly misconfigured, and it isn't worth testing for this.
938
939          if Nkind (Lib_Unit) = N_Subprogram_Declaration then
940             RE_Table (E) := U.Entity;
941
942          --  Otherwise we must have the package case. First check package
943          --  entity itself (e.g. RTE_Name for System.Interrupts.Name)
944
945          else
946             pragma Assert (Nkind (Lib_Unit) = N_Package_Declaration);
947             Ename := RE_Chars (E);
948
949             --  First we search the package entity chain
950
951                Pkg_Ent := First_Entity (U.Entity);
952                while Present (Pkg_Ent) loop
953                   if Ename = Chars (Pkg_Ent) then
954                      RE_Table (E) := Pkg_Ent;
955                      Check_RPC;
956                      goto Found;
957                   end if;
958
959                   Next_Entity (Pkg_Ent);
960                end loop;
961
962             --  If we did not find the entity in the package entity chain,
963             --  then check if the package entity itself matches. Note that
964             --  we do this check after searching the entity chain, since
965             --  the rule is that in case of ambiguity, we prefer the entity
966             --  defined within the package, rather than the package itself.
967
968             if Ename = Chars (U.Entity) then
969                RE_Table (E) := U.Entity;
970             end if;
971
972             --  If we didn't find the entity we want, something is wrong.
973             --  We just leave RE_Table (E) set to Empty and the appropriate
974             --  action will be taken by Check_CRT when we exit.
975
976          end if;
977       end if;
978
979       --  See if we have to generate a WITH for this entity. We generate
980       --  a WITH if the current unit is part of the extended main code
981       --  unit, and if we have not already added the with. The WITH is
982       --  added to the appropriate unit (the current one). We do not need
983       --  to generate a WITH for an ????
984
985    <<Found>>
986       if (not U.Withed)
987         and then
988           In_Extended_Main_Code_Unit (Cunit_Entity (Current_Sem_Unit))
989         and then not RTE_Available_Call
990       then
991          U.Withed := True;
992
993          declare
994             Withn    : Node_Id;
995             Lib_Unit : Node_Id;
996
997          begin
998             Lib_Unit := Unit (Cunit (U.Unum));
999             Withn :=
1000               Make_With_Clause (Standard_Location,
1001                 Name =>
1002                   Make_Unit_Name
1003                     (Defining_Unit_Name (Specification (Lib_Unit))));
1004             Set_Library_Unit          (Withn, Cunit (U.Unum));
1005             Set_Corresponding_Spec    (Withn, U.Entity);
1006             Set_First_Name            (Withn, True);
1007             Set_Implicit_With         (Withn, True);
1008
1009             Mark_Rewrite_Insertion (Withn);
1010             Append (Withn, Context_Items (Cunit (Current_Sem_Unit)));
1011             Check_Restriction_No_Dependence (Name (Withn), Current_Error_Node);
1012          end;
1013       end if;
1014
1015       Front_End_Inlining := Save_Front_End_Inlining;
1016       return Check_CRT (RE_Table (E));
1017    end RTE;
1018
1019    -------------------
1020    -- RTE_Available --
1021    -------------------
1022
1023    function RTE_Available (E : RE_Id) return Boolean is
1024       Dummy : Entity_Id;
1025       pragma Warnings (Off, Dummy);
1026
1027       Result : Boolean;
1028
1029       Save_RTE_Available_Call : constant Boolean := RTE_Available_Call;
1030       Save_RTE_Is_Available   : constant Boolean := RTE_Is_Available;
1031       --  These are saved recursively because the call to load a unit
1032       --  caused by an upper level call may perform a recursive call
1033       --  to this routine during analysis of the corresponding unit.
1034
1035    begin
1036       RTE_Available_Call := True;
1037       RTE_Is_Available := True;
1038       Dummy := RTE (E);
1039       Result := RTE_Is_Available;
1040       RTE_Available_Call := Save_RTE_Available_Call;
1041       RTE_Is_Available   := Save_RTE_Is_Available;
1042       return Result;
1043
1044    exception
1045       when RE_Not_Available =>
1046          RTE_Available_Call := Save_RTE_Available_Call;
1047          RTE_Is_Available   := Save_RTE_Is_Available;
1048          return False;
1049    end RTE_Available;
1050
1051    -------------------
1052    -- RTE_Error_Msg --
1053    -------------------
1054
1055    procedure RTE_Error_Msg (Msg : String) is
1056    begin
1057       if RTE_Available_Call then
1058          RTE_Is_Available := False;
1059       else
1060          Error_Msg_N (Msg, Current_Error_Node);
1061
1062          --  Bump count of violations if we are in configurable run-time
1063          --  mode and this is not a continuation message.
1064
1065          if Configurable_Run_Time_Mode and then Msg (1) /= '\' then
1066             Configurable_Run_Time_Violations :=
1067               Configurable_Run_Time_Violations + 1;
1068          end if;
1069       end if;
1070    end RTE_Error_Msg;
1071
1072    ----------------
1073    -- RTU_Loaded --
1074    ----------------
1075
1076    function RTU_Loaded (U : RTU_Id) return Boolean is
1077    begin
1078       return Present (RT_Unit_Table (U).Entity);
1079    end RTU_Loaded;
1080
1081    --------------------
1082    -- Set_RTU_Loaded --
1083    --------------------
1084
1085    procedure Set_RTU_Loaded (N : Node_Id) is
1086       Loc   : constant Source_Ptr       := Sloc (N);
1087       Unum  : constant Unit_Number_Type := Get_Source_Unit (Loc);
1088       Uname : constant Unit_Name_Type   := Unit_Name (Unum);
1089       E     : constant Entity_Id        :=
1090                 Defining_Entity (Unit (Cunit (Unum)));
1091    begin
1092       pragma Assert (Is_Predefined_File_Name (Unit_File_Name (Unum)));
1093
1094       --  Loop through entries in RTU table looking for matching entry
1095
1096       for U_Id in RTU_Id'Range loop
1097
1098          --  Here we have a match
1099
1100          if Get_Unit_Name (U_Id) = Uname then
1101             declare
1102                U : RT_Unit_Table_Record renames RT_Unit_Table (U_Id);
1103                --  The RT_Unit_Table entry that may need updating
1104
1105             begin
1106                --  If entry is not set, set it now
1107
1108                if not Present (U.Entity) then
1109                   U.Entity := E;
1110                   U.Uname  := Get_Unit_Name (U_Id);
1111                   U.Unum   := Unum;
1112                   U.Withed := False;
1113                end if;
1114
1115                return;
1116             end;
1117          end if;
1118       end loop;
1119    end Set_RTU_Loaded;
1120
1121    --------------------
1122    -- Text_IO_Kludge --
1123    --------------------
1124
1125    procedure Text_IO_Kludge (Nam : Node_Id) is
1126       Chrs : Name_Id;
1127
1128       type Name_Map_Type is array (Text_IO_Package_Name) of RTU_Id;
1129
1130       Name_Map : constant Name_Map_Type := Name_Map_Type'(
1131         Name_Decimal_IO     => Ada_Text_IO_Decimal_IO,
1132         Name_Enumeration_IO => Ada_Text_IO_Enumeration_IO,
1133         Name_Fixed_IO       => Ada_Text_IO_Fixed_IO,
1134         Name_Float_IO       => Ada_Text_IO_Float_IO,
1135         Name_Integer_IO     => Ada_Text_IO_Integer_IO,
1136         Name_Modular_IO     => Ada_Text_IO_Modular_IO);
1137
1138       Wide_Name_Map : constant Name_Map_Type := Name_Map_Type'(
1139         Name_Decimal_IO     => Ada_Wide_Text_IO_Decimal_IO,
1140         Name_Enumeration_IO => Ada_Wide_Text_IO_Enumeration_IO,
1141         Name_Fixed_IO       => Ada_Wide_Text_IO_Fixed_IO,
1142         Name_Float_IO       => Ada_Wide_Text_IO_Float_IO,
1143         Name_Integer_IO     => Ada_Wide_Text_IO_Integer_IO,
1144         Name_Modular_IO     => Ada_Wide_Text_IO_Modular_IO);
1145
1146       Wide_Wide_Name_Map : constant Name_Map_Type := Name_Map_Type'(
1147         Name_Decimal_IO     => Ada_Wide_Wide_Text_IO_Decimal_IO,
1148         Name_Enumeration_IO => Ada_Wide_Wide_Text_IO_Enumeration_IO,
1149         Name_Fixed_IO       => Ada_Wide_Wide_Text_IO_Fixed_IO,
1150         Name_Float_IO       => Ada_Wide_Wide_Text_IO_Float_IO,
1151         Name_Integer_IO     => Ada_Wide_Wide_Text_IO_Integer_IO,
1152         Name_Modular_IO     => Ada_Wide_Wide_Text_IO_Modular_IO);
1153
1154    begin
1155       --  Nothing to do if name is not identifier or a selected component
1156       --  whose selector_name is not an identifier.
1157
1158       if Nkind (Nam) = N_Identifier then
1159          Chrs := Chars (Nam);
1160
1161       elsif Nkind (Nam) = N_Selected_Component
1162         and then Nkind (Selector_Name (Nam)) = N_Identifier
1163       then
1164          Chrs := Chars (Selector_Name (Nam));
1165
1166       else
1167          return;
1168       end if;
1169
1170       --  Nothing to do if name is not one of the Text_IO subpackages
1171       --  Otherwise look through loaded units, and if we find Text_IO
1172       --  or [Wide_]Wide_Text_IO already loaded, then load the proper child.
1173
1174       if Chrs in Text_IO_Package_Name then
1175          for U in Main_Unit .. Last_Unit loop
1176             Get_Name_String (Unit_File_Name (U));
1177
1178             if Name_Len = 12 then
1179
1180                --  Here is where we do the loads if we find one of the units
1181                --  Ada.Text_IO or Ada.[Wide_]Wide_Text_IO. An interesting
1182                --  detail is that these units may already be used (i.e. their
1183                --  In_Use flags may be set). Normally when the In_Use flag is
1184                --  set, the Is_Potentially_Use_Visible flag of all entities in
1185                --  the package is set, but the new entity we are mysteriously
1186                --  adding was not there to have its flag set at the time. So
1187                --  that's why we pass the extra parameter to RTU_Find, to make
1188                --  sure the flag does get set now. Given that those generic
1189                --  packages are in fact child units, we must indicate that
1190                --  they are visible.
1191
1192                if Name_Buffer (1 .. 12) = "a-textio.ads" then
1193                   Load_RTU
1194                     (Name_Map (Chrs),
1195                      Use_Setting => In_Use (Cunit_Entity (U)));
1196                   Set_Is_Visible_Child_Unit
1197                     (RT_Unit_Table (Name_Map (Chrs)).Entity);
1198
1199                elsif Name_Buffer (1 .. 12) = "a-witeio.ads" then
1200                   Load_RTU
1201                     (Wide_Name_Map (Chrs),
1202                      Use_Setting => In_Use (Cunit_Entity (U)));
1203                   Set_Is_Visible_Child_Unit
1204                     (RT_Unit_Table (Wide_Name_Map (Chrs)).Entity);
1205
1206                elsif Name_Buffer (1 .. 12) = "a-ztexio.ads" then
1207                   Load_RTU
1208                     (Wide_Wide_Name_Map (Chrs),
1209                      Use_Setting => In_Use (Cunit_Entity (U)));
1210                   Set_Is_Visible_Child_Unit
1211                     (RT_Unit_Table (Wide_Wide_Name_Map (Chrs)).Entity);
1212                end if;
1213             end if;
1214          end loop;
1215       end if;
1216    end Text_IO_Kludge;
1217
1218 end Rtsfind;