OSDN Git Service

* 1aexcept.adb, 1aexcept.ads, 1ic.ads, 1ssecsta.adb,
[pf3gnuchains/gcc-fork.git] / gcc / ada / exp_tss.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              E X P _ T S S                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2001 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 Einfo;    use Einfo;
29 with Elists;   use Elists;
30 with Exp_Util; use Exp_Util;
31 with Lib;      use Lib;
32 with Sem_Util; use Sem_Util;
33 with Sinfo;    use Sinfo;
34 with Snames;   use Snames;
35
36 package body Exp_Tss is
37
38    --------------------
39    -- Base_Init_Proc --
40    --------------------
41
42    function Base_Init_Proc (Typ : Entity_Id) return Entity_Id is
43       Full_Type : E;
44       Proc      : Entity_Id;
45
46    begin
47       pragma Assert (Ekind (Typ) in Type_Kind);
48
49       if Is_Private_Type (Typ) then
50          Full_Type := Underlying_Type (Base_Type (Typ));
51       else
52          Full_Type := Typ;
53       end if;
54
55       if No (Full_Type) then
56          return Empty;
57       elsif Is_Concurrent_Type (Full_Type)
58         and then Present (Corresponding_Record_Type (Base_Type (Full_Type)))
59       then
60          return Init_Proc (Corresponding_Record_Type (Base_Type (Full_Type)));
61
62       else
63          Proc := Init_Proc (Base_Type (Full_Type));
64
65          if No (Proc)
66            and then Is_Composite_Type (Full_Type)
67            and then Is_Derived_Type (Full_Type)
68          then
69             return Init_Proc (Root_Type (Full_Type));
70          else
71             return Proc;
72          end if;
73       end if;
74    end Base_Init_Proc;
75
76    --------------
77    -- Copy_TSS --
78    --------------
79
80    --  Note: internally this routine is also used to initially set up
81    --  a TSS entry for a new type (case of being called from Set_TSS)
82
83    procedure Copy_TSS (TSS : Entity_Id; Typ : Entity_Id) is
84       FN : Node_Id;
85
86    begin
87       Ensure_Freeze_Node (Typ);
88       FN := Freeze_Node (Typ);
89
90       if No (TSS_Elist (FN)) then
91          Set_TSS_Elist (FN, New_Elmt_List);
92       end if;
93
94       --  We prepend here, so that a second call overrides the first, it
95       --  is not clear that this is required, but it seems reasonable.
96
97       Prepend_Elmt (TSS, TSS_Elist (FN));
98    end Copy_TSS;
99
100    ---------------------------------
101    -- Has_Non_Null_Base_Init_Proc --
102    ---------------------------------
103
104    function Has_Non_Null_Base_Init_Proc (Typ : Entity_Id) return Boolean is
105       BIP : constant Entity_Id := Base_Init_Proc (Typ);
106
107    begin
108       return Present (BIP) and then not Is_Null_Init_Proc (BIP);
109    end Has_Non_Null_Base_Init_Proc;
110
111    ---------------
112    -- Init_Proc --
113    ---------------
114
115    function Init_Proc (Typ : Entity_Id) return Entity_Id is
116    begin
117       return TSS (Typ, Name_uInit_Proc);
118    end Init_Proc;
119
120    -------------------
121    -- Set_Init_Proc --
122    -------------------
123
124    procedure Set_Init_Proc (Typ : Entity_Id; Init : Entity_Id) is
125    begin
126       Set_TSS (Typ, Init);
127    end Set_Init_Proc;
128
129    -------------
130    -- Set_TSS --
131    -------------
132
133    procedure Set_TSS (Typ : Entity_Id; TSS : Entity_Id) is
134       Subprog_Body : constant Node_Id := Unit_Declaration_Node (TSS);
135
136    begin
137       --  Case of insertion location is in unit defining the type
138
139       if In_Same_Code_Unit (Typ, TSS) then
140          Append_Freeze_Action (Typ, Subprog_Body);
141
142       --  Otherwise, we are using an already existing TSS in another unit
143
144       else
145          null;
146       end if;
147
148       Copy_TSS (TSS, Typ);
149    end Set_TSS;
150
151    ---------
152    -- TSS --
153    ---------
154
155    function TSS (Typ : Entity_Id; Nam : Name_Id) return Entity_Id is
156       FN   : constant Node_Id := Freeze_Node (Typ);
157       Elmt : Elmt_Id;
158       Subp : Entity_Id;
159
160    begin
161       if No (FN) then
162          return Empty;
163
164       elsif No (TSS_Elist (FN)) then
165          return Empty;
166
167       else
168          Elmt := First_Elmt (TSS_Elist (FN));
169
170          while Present (Elmt) loop
171             if Chars (Node (Elmt)) = Nam then
172                Subp := Node (Elmt);
173
174                --  For stream subprograms, the TSS entity may be a renaming-
175                --  as-body of an already generated entity. Use that one rather
176                --  the one introduced by the renaming, which is an artifact of
177                --  current stream handling.
178
179                if Nkind (Parent (Parent (Subp))) =
180                                            N_Subprogram_Renaming_Declaration
181                  and then
182                    Present (Corresponding_Spec (Parent (Parent (Subp))))
183                then
184                   return Corresponding_Spec (Parent (Parent (Subp)));
185                else
186                   return Subp;
187                end if;
188
189             else
190                Next_Elmt (Elmt);
191             end if;
192          end loop;
193       end if;
194
195       return Empty;
196    end TSS;
197
198 end Exp_Tss;