1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Exp_Util; use Exp_Util;
32 with Sem_Util; use Sem_Util;
33 with Sinfo; use Sinfo;
35 package body Exp_Tss is
41 function Base_Init_Proc (Typ : Entity_Id) return Entity_Id is
46 pragma Assert (Is_Type (Typ));
48 if Is_Private_Type (Typ) then
49 Full_Type := Underlying_Type (Base_Type (Typ));
54 if No (Full_Type) then
56 elsif Is_Concurrent_Type (Full_Type)
57 and then Present (Corresponding_Record_Type (Base_Type (Full_Type)))
59 return Init_Proc (Corresponding_Record_Type (Base_Type (Full_Type)));
62 Proc := Init_Proc (Base_Type (Full_Type));
65 and then Is_Composite_Type (Full_Type)
66 and then Is_Derived_Type (Full_Type)
68 return Init_Proc (Root_Type (Full_Type));
79 -- Note: internally this routine is also used to initially set up
80 -- a TSS entry for a new type (case of being called from Set_TSS)
82 procedure Copy_TSS (TSS : Entity_Id; Typ : Entity_Id) is
86 Ensure_Freeze_Node (Typ);
87 FN := Freeze_Node (Typ);
89 if No (TSS_Elist (FN)) then
90 Set_TSS_Elist (FN, New_Elmt_List);
93 -- We prepend here, so that a second call overrides the first, it
94 -- is not clear that this is required, but it seems reasonable.
96 Prepend_Elmt (TSS, TSS_Elist (FN));
99 ------------------------
100 -- Find_Inherited_TSS --
101 ------------------------
103 function Find_Inherited_TSS
105 Nam : TSS_Name_Type) return Entity_Id
107 Btyp : Entity_Id := Typ;
112 Btyp := Base_Type (Btyp);
113 Proc := TSS (Btyp, Nam);
115 exit when Present (Proc)
116 or else not Is_Derived_Type (Btyp);
118 -- If Typ is a derived type, it may inherit attributes from some
121 Btyp := Etype (Btyp);
126 -- If nothing else, use the TSS of the root type
128 Proc := TSS (Base_Type (Underlying_Type (Typ)), Nam);
132 end Find_Inherited_TSS;
134 -----------------------
135 -- Get_TSS_Name_Type --
136 -----------------------
138 function Get_TSS_Name (E : Entity_Id) return TSS_Name_Type is
144 Get_Last_Two_Chars (Chars (E), C1, C2);
146 if C1 in 'A' .. 'Z' and then C2 in 'A' .. 'Z' then
149 for J in TSS_Names'Range loop
150 if Nm = TSS_Names (J) then
159 ---------------------------------
160 -- Has_Non_Null_Base_Init_Proc --
161 ---------------------------------
163 function Has_Non_Null_Base_Init_Proc (Typ : Entity_Id) return Boolean is
164 BIP : constant Entity_Id := Base_Init_Proc (Typ);
167 return Present (BIP) and then not Is_Null_Init_Proc (BIP);
168 end Has_Non_Null_Base_Init_Proc;
174 function Init_Proc (Typ : Entity_Id) return Entity_Id is
175 FN : constant Node_Id := Freeze_Node (Typ);
182 elsif No (TSS_Elist (FN)) then
186 Elmt := First_Elmt (TSS_Elist (FN));
187 while Present (Elmt) loop
188 if Is_Init_Proc (Node (Elmt)) then
203 function Is_Init_Proc (E : Entity_Id) return Boolean is
207 Get_Last_Two_Chars (Chars (E), C1, C2);
208 return C1 = TSS_Init_Proc (1) and then C2 = TSS_Init_Proc (2);
215 function Is_TSS (E : Entity_Id; Nam : TSS_Name_Type) return Boolean is
219 Get_Last_Two_Chars (Chars (E), C1, C2);
220 return C1 = Nam (1) and then C2 = Nam (2);
223 function Is_TSS (N : Name_Id; Nam : TSS_Name_Type) return Boolean is
227 Get_Last_Two_Chars (N, C1, C2);
228 return C1 = Nam (1) and then C2 = Nam (2);
231 -------------------------
232 -- Make_Init_Proc_Name --
233 -------------------------
235 function Make_Init_Proc_Name (Typ : Entity_Id) return Name_Id is
237 return Make_TSS_Name (Typ, TSS_Init_Proc);
238 end Make_Init_Proc_Name;
244 function Make_TSS_Name
246 Nam : TSS_Name_Type) return Name_Id
249 Get_Name_String (Chars (Typ));
250 Add_Char_To_Name_Buffer (Nam (1));
251 Add_Char_To_Name_Buffer (Nam (2));
255 -------------------------
256 -- Make_TSS_Name_Local --
257 -------------------------
259 function Make_TSS_Name_Local
261 Nam : TSS_Name_Type) return Name_Id
264 Get_Name_String (Chars (Typ));
265 Add_Char_To_Name_Buffer ('_');
266 Add_Nat_To_Name_Buffer (Increment_Serial_Number);
267 Add_Char_To_Name_Buffer (Nam (1));
268 Add_Char_To_Name_Buffer (Nam (2));
270 end Make_TSS_Name_Local;
276 function Same_TSS (E1, E2 : Entity_Id) return Boolean is
283 Get_Last_Two_Chars (Chars (E1), E1C1, E1C2);
284 Get_Last_Two_Chars (Chars (E2), E2C1, E2C2);
300 procedure Set_Init_Proc (Typ : Entity_Id; Init : Entity_Id) is
309 procedure Set_TSS (Typ : Entity_Id; TSS : Entity_Id) is
310 Subprog_Body : constant Node_Id := Unit_Declaration_Node (TSS);
313 -- Case of insertion location is in unit defining the type
315 if In_Same_Code_Unit (Typ, TSS) then
316 Append_Freeze_Action (Typ, Subprog_Body);
318 -- Otherwise, we are using an already existing TSS in another unit
331 function TSS (Typ : Entity_Id; Nam : TSS_Name_Type) return Entity_Id is
332 FN : constant Node_Id := Freeze_Node (Typ);
340 elsif No (TSS_Elist (FN)) then
344 Elmt := First_Elmt (TSS_Elist (FN));
345 while Present (Elmt) loop
346 if Is_TSS (Node (Elmt), Nam) then
349 -- For stream subprograms, the TSS entity may be a renaming-
350 -- as-body of an already generated entity. Use that one rather
351 -- the one introduced by the renaming, which is an artifact of
352 -- current stream handling.
354 if Nkind (Parent (Parent (Subp))) =
355 N_Subprogram_Renaming_Declaration
357 Present (Corresponding_Spec (Parent (Parent (Subp))))
359 return Corresponding_Spec (Parent (Parent (Subp)));
373 function TSS (Typ : Entity_Id; Nam : Name_Id) return Entity_Id is
374 FN : constant Node_Id := Freeze_Node (Typ);
382 elsif No (TSS_Elist (FN)) then
386 Elmt := First_Elmt (TSS_Elist (FN));
387 while Present (Elmt) loop
388 if Chars (Node (Elmt)) = Nam then
391 -- For stream subprograms, the TSS entity may be a renaming-
392 -- as-body of an already generated entity. Use that one rather
393 -- the one introduced by the renaming, which is an artifact of
394 -- current stream handling.
396 if Nkind (Parent (Parent (Subp))) =
397 N_Subprogram_Renaming_Declaration
399 Present (Corresponding_Spec (Parent (Parent (Subp))))
401 return Corresponding_Spec (Parent (Parent (Subp)));