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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Einfo
; use Einfo
;
28 with Elists
; use Elists
;
29 with Exp_Util
; use Exp_Util
;
31 with Sem_Util
; use Sem_Util
;
32 with Sinfo
; use Sinfo
;
34 package body Exp_Tss
is
40 function Base_Init_Proc
(Typ
: Entity_Id
) return Entity_Id
is
45 pragma Assert
(Is_Type
(Typ
));
47 if Is_Private_Type
(Typ
) then
48 Full_Type
:= Underlying_Type
(Base_Type
(Typ
));
53 if No
(Full_Type
) then
55 elsif Is_Concurrent_Type
(Full_Type
)
56 and then Present
(Corresponding_Record_Type
(Base_Type
(Full_Type
)))
58 return Init_Proc
(Corresponding_Record_Type
(Base_Type
(Full_Type
)));
61 Proc
:= Init_Proc
(Base_Type
(Full_Type
));
64 and then Is_Composite_Type
(Full_Type
)
65 and then Is_Derived_Type
(Full_Type
)
67 return Init_Proc
(Root_Type
(Full_Type
));
78 -- Note: internally this routine is also used to initially set up
79 -- a TSS entry for a new type (case of being called from Set_TSS)
81 procedure Copy_TSS
(TSS
: Entity_Id
; Typ
: Entity_Id
) is
85 Ensure_Freeze_Node
(Typ
);
86 FN
:= Freeze_Node
(Typ
);
88 if No
(TSS_Elist
(FN
)) then
89 Set_TSS_Elist
(FN
, New_Elmt_List
);
92 -- We prepend here, so that a second call overrides the first, it
93 -- is not clear that this is required, but it seems reasonable.
95 Prepend_Elmt
(TSS
, TSS_Elist
(FN
));
98 ------------------------
99 -- Find_Inherited_TSS --
100 ------------------------
102 function Find_Inherited_TSS
104 Nam
: TSS_Name_Type
) return Entity_Id
106 Btyp
: Entity_Id
:= Typ
;
111 Btyp
:= Base_Type
(Btyp
);
112 Proc
:= TSS
(Btyp
, Nam
);
114 exit when Present
(Proc
)
115 or else not Is_Derived_Type
(Btyp
);
117 -- If Typ is a derived type, it may inherit attributes from some
120 Btyp
:= Etype
(Btyp
);
125 -- If nothing else, use the TSS of the root type
127 Proc
:= TSS
(Base_Type
(Underlying_Type
(Typ
)), Nam
);
131 end Find_Inherited_TSS
;
133 -----------------------
134 -- Get_TSS_Name_Type --
135 -----------------------
137 function Get_TSS_Name
(E
: Entity_Id
) return TSS_Name_Type
is
143 Get_Last_Two_Chars
(Chars
(E
), C1
, C2
);
145 if C1
in 'A' .. 'Z' and then C2
in 'A' .. 'Z' then
148 for J
in TSS_Names
'Range loop
149 if Nm
= TSS_Names
(J
) then
158 ---------------------------------
159 -- Has_Non_Null_Base_Init_Proc --
160 ---------------------------------
162 function Has_Non_Null_Base_Init_Proc
(Typ
: Entity_Id
) return Boolean is
163 BIP
: constant Entity_Id
:= Base_Init_Proc
(Typ
);
166 return Present
(BIP
) and then not Is_Null_Init_Proc
(BIP
);
167 end Has_Non_Null_Base_Init_Proc
;
173 function Init_Proc
(Typ
: Entity_Id
) return Entity_Id
is
174 FN
: constant Node_Id
:= Freeze_Node
(Typ
);
181 elsif No
(TSS_Elist
(FN
)) then
185 Elmt
:= First_Elmt
(TSS_Elist
(FN
));
186 while Present
(Elmt
) loop
187 if Is_Init_Proc
(Node
(Elmt
)) then
202 function Is_Init_Proc
(E
: Entity_Id
) return Boolean is
206 Get_Last_Two_Chars
(Chars
(E
), C1
, C2
);
207 return C1
= TSS_Init_Proc
(1) and then C2
= TSS_Init_Proc
(2);
214 function Is_TSS
(E
: Entity_Id
; Nam
: TSS_Name_Type
) return Boolean is
218 Get_Last_Two_Chars
(Chars
(E
), C1
, C2
);
219 return C1
= Nam
(1) and then C2
= Nam
(2);
222 function Is_TSS
(N
: Name_Id
; Nam
: TSS_Name_Type
) return Boolean is
226 Get_Last_Two_Chars
(N
, C1
, C2
);
227 return C1
= Nam
(1) and then C2
= Nam
(2);
230 -------------------------
231 -- Make_Init_Proc_Name --
232 -------------------------
234 function Make_Init_Proc_Name
(Typ
: Entity_Id
) return Name_Id
is
236 return Make_TSS_Name
(Typ
, TSS_Init_Proc
);
237 end Make_Init_Proc_Name
;
243 function Make_TSS_Name
245 Nam
: TSS_Name_Type
) return Name_Id
248 Get_Name_String
(Chars
(Typ
));
249 Add_Char_To_Name_Buffer
(Nam
(1));
250 Add_Char_To_Name_Buffer
(Nam
(2));
254 -------------------------
255 -- Make_TSS_Name_Local --
256 -------------------------
258 function Make_TSS_Name_Local
260 Nam
: TSS_Name_Type
) return Name_Id
263 Get_Name_String
(Chars
(Typ
));
264 Add_Char_To_Name_Buffer
('_');
265 Add_Nat_To_Name_Buffer
(Increment_Serial_Number
);
266 Add_Char_To_Name_Buffer
(Nam
(1));
267 Add_Char_To_Name_Buffer
(Nam
(2));
269 end Make_TSS_Name_Local
;
275 function Same_TSS
(E1
, E2
: Entity_Id
) return Boolean is
282 Get_Last_Two_Chars
(Chars
(E1
), E1C1
, E1C2
);
283 Get_Last_Two_Chars
(Chars
(E2
), E2C1
, E2C2
);
299 procedure Set_Init_Proc
(Typ
: Entity_Id
; Init
: Entity_Id
) is
308 procedure Set_TSS
(Typ
: Entity_Id
; TSS
: Entity_Id
) is
309 Subprog_Body
: constant Node_Id
:= Unit_Declaration_Node
(TSS
);
312 -- Case of insertion location is in unit defining the type
314 if In_Same_Code_Unit
(Typ
, TSS
) then
315 Append_Freeze_Action
(Typ
, Subprog_Body
);
317 -- Otherwise, we are using an already existing TSS in another unit
330 function TSS
(Typ
: Entity_Id
; Nam
: TSS_Name_Type
) return Entity_Id
is
331 FN
: constant Node_Id
:= Freeze_Node
(Typ
);
339 elsif No
(TSS_Elist
(FN
)) then
343 Elmt
:= First_Elmt
(TSS_Elist
(FN
));
344 while Present
(Elmt
) loop
345 if Is_TSS
(Node
(Elmt
), Nam
) then
348 -- For stream subprograms, the TSS entity may be a renaming-
349 -- as-body of an already generated entity. Use that one rather
350 -- the one introduced by the renaming, which is an artifact of
351 -- current stream handling.
353 if Nkind
(Parent
(Parent
(Subp
))) =
354 N_Subprogram_Renaming_Declaration
356 Present
(Corresponding_Spec
(Parent
(Parent
(Subp
))))
358 return Corresponding_Spec
(Parent
(Parent
(Subp
)));
372 function TSS
(Typ
: Entity_Id
; Nam
: Name_Id
) return Entity_Id
is
373 FN
: constant Node_Id
:= Freeze_Node
(Typ
);
381 elsif No
(TSS_Elist
(FN
)) then
385 Elmt
:= First_Elmt
(TSS_Elist
(FN
));
386 while Present
(Elmt
) loop
387 if Chars
(Node
(Elmt
)) = Nam
then
390 -- For stream subprograms, the TSS entity may be a renaming-
391 -- as-body of an already generated entity. Use that one rather
392 -- the one introduced by the renaming, which is an artifact of
393 -- current stream handling.
395 if Nkind
(Parent
(Parent
(Subp
))) =
396 N_Subprogram_Renaming_Declaration
398 Present
(Corresponding_Spec
(Parent
(Parent
(Subp
))))
400 return Corresponding_Spec
(Parent
(Parent
(Subp
)));