* gcc.c-torture/execute/20020307-1.c: New test.
[official-gcc.git] / gcc / ada / exp_tss.adb
blob6e3722c5202f1d2563dd8f734493e148b2a151c0
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ T S S --
6 -- --
7 -- B o d y --
8 -- --
9 -- $Revision: 1.26 $
10 -- --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
12 -- --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
23 -- --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 -- --
27 ------------------------------------------------------------------------------
29 with Atree; use Atree;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Exp_Util; use Exp_Util;
33 with Lib; use Lib;
34 with Sem_Util; use Sem_Util;
35 with Sinfo; use Sinfo;
36 with Snames; use Snames;
38 package body Exp_Tss is
40 --------------------
41 -- Base_Init_Proc --
42 --------------------
44 function Base_Init_Proc (Typ : Entity_Id) return Entity_Id is
45 Full_Type : E;
46 Proc : Entity_Id;
48 begin
49 pragma Assert (Ekind (Typ) in Type_Kind);
51 if Is_Private_Type (Typ) then
52 Full_Type := Underlying_Type (Base_Type (Typ));
53 else
54 Full_Type := Typ;
55 end if;
57 if No (Full_Type) then
58 return Empty;
59 elsif Is_Concurrent_Type (Full_Type)
60 and then Present (Corresponding_Record_Type (Base_Type (Full_Type)))
61 then
62 return Init_Proc (Corresponding_Record_Type (Base_Type (Full_Type)));
64 else
65 Proc := Init_Proc (Base_Type (Full_Type));
67 if No (Proc)
68 and then Is_Composite_Type (Full_Type)
69 and then Is_Derived_Type (Full_Type)
70 then
71 return Init_Proc (Root_Type (Full_Type));
72 else
73 return Proc;
74 end if;
75 end if;
76 end Base_Init_Proc;
78 --------------
79 -- Copy_TSS --
80 --------------
82 -- Note: internally this routine is also used to initially set up
83 -- a TSS entry for a new type (case of being called from Set_TSS)
85 procedure Copy_TSS (TSS : Entity_Id; Typ : Entity_Id) is
86 FN : Node_Id;
88 begin
89 Ensure_Freeze_Node (Typ);
90 FN := Freeze_Node (Typ);
92 if No (TSS_Elist (FN)) then
93 Set_TSS_Elist (FN, New_Elmt_List);
94 end if;
96 -- We prepend here, so that a second call overrides the first, it
97 -- is not clear that this is required, but it seems reasonable.
99 Prepend_Elmt (TSS, TSS_Elist (FN));
100 end Copy_TSS;
102 ---------------------------------
103 -- Has_Non_Null_Base_Init_Proc --
104 ---------------------------------
106 function Has_Non_Null_Base_Init_Proc (Typ : Entity_Id) return Boolean is
107 BIP : constant Entity_Id := Base_Init_Proc (Typ);
109 begin
110 return Present (BIP) and then not Is_Null_Init_Proc (BIP);
111 end Has_Non_Null_Base_Init_Proc;
113 ---------------
114 -- Init_Proc --
115 ---------------
117 function Init_Proc (Typ : Entity_Id) return Entity_Id is
118 begin
119 return TSS (Typ, Name_uInit_Proc);
120 end Init_Proc;
122 -------------------
123 -- Set_Init_Proc --
124 -------------------
126 procedure Set_Init_Proc (Typ : Entity_Id; Init : Entity_Id) is
127 begin
128 Set_TSS (Typ, Init);
129 end Set_Init_Proc;
131 -------------
132 -- Set_TSS --
133 -------------
135 procedure Set_TSS (Typ : Entity_Id; TSS : Entity_Id) is
136 Subprog_Body : constant Node_Id := Unit_Declaration_Node (TSS);
138 begin
139 -- Case of insertion location is in unit defining the type
141 if In_Same_Code_Unit (Typ, TSS) then
142 Append_Freeze_Action (Typ, Subprog_Body);
144 -- Otherwise, we are using an already existing TSS in another unit
146 else
147 null;
148 end if;
150 Copy_TSS (TSS, Typ);
151 end Set_TSS;
153 ---------
154 -- TSS --
155 ---------
157 function TSS (Typ : Entity_Id; Nam : Name_Id) return Entity_Id is
158 FN : constant Node_Id := Freeze_Node (Typ);
159 Elmt : Elmt_Id;
160 Subp : Entity_Id;
162 begin
163 if No (FN) then
164 return Empty;
166 elsif No (TSS_Elist (FN)) then
167 return Empty;
169 else
170 Elmt := First_Elmt (TSS_Elist (FN));
172 while Present (Elmt) loop
173 if Chars (Node (Elmt)) = Nam then
174 Subp := Node (Elmt);
176 -- For stream subprograms, the TSS entity may be a renaming-
177 -- as-body of an already generated entity. Use that one rather
178 -- the one introduced by the renaming, which is an artifact of
179 -- current stream handling.
181 if Nkind (Parent (Parent (Subp))) =
182 N_Subprogram_Renaming_Declaration
183 and then
184 Present (Corresponding_Spec (Parent (Parent (Subp))))
185 then
186 return Corresponding_Spec (Parent (Parent (Subp)));
187 else
188 return Subp;
189 end if;
191 else
192 Next_Elmt (Elmt);
193 end if;
194 end loop;
195 end if;
197 return Empty;
198 end TSS;
200 end Exp_Tss;