OSDN Git Service

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