1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, 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 Restrict
; use Restrict
;
32 with Rident
; use Rident
;
33 with Sem_Util
; use Sem_Util
;
34 with Sinfo
; use Sinfo
;
36 package body Exp_Tss
is
42 function Base_Init_Proc
(Typ
: Entity_Id
) return Entity_Id
is
47 pragma Assert
(Is_Type
(Typ
));
49 if Is_Private_Type
(Typ
) then
50 Full_Type
:= Underlying_Type
(Base_Type
(Typ
));
55 if No
(Full_Type
) then
57 elsif Is_Concurrent_Type
(Full_Type
)
58 and then Present
(Corresponding_Record_Type
(Base_Type
(Full_Type
)))
60 return Init_Proc
(Corresponding_Record_Type
(Base_Type
(Full_Type
)));
63 Proc
:= Init_Proc
(Base_Type
(Full_Type
));
66 and then Is_Composite_Type
(Full_Type
)
67 and then Is_Derived_Type
(Full_Type
)
69 return Init_Proc
(Root_Type
(Full_Type
));
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)
83 procedure Copy_TSS
(TSS
: Entity_Id
; Typ
: Entity_Id
) is
87 Ensure_Freeze_Node
(Typ
);
88 FN
:= Freeze_Node
(Typ
);
90 if No
(TSS_Elist
(FN
)) then
91 Set_TSS_Elist
(FN
, New_Elmt_List
);
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.
97 Prepend_Elmt
(TSS
, TSS_Elist
(FN
));
100 ------------------------
101 -- Find_Inherited_TSS --
102 ------------------------
104 function Find_Inherited_TSS
106 Nam
: TSS_Name_Type
) return Entity_Id
108 Btyp
: Entity_Id
:= Typ
;
113 Btyp
:= Base_Type
(Btyp
);
114 Proc
:= TSS
(Btyp
, Nam
);
116 exit when Present
(Proc
)
117 or else not Is_Derived_Type
(Btyp
);
119 -- If Typ is a derived type, it may inherit attributes from some
122 Btyp
:= Etype
(Btyp
);
127 -- If nothing else, use the TSS of the root type
129 Proc
:= TSS
(Base_Type
(Underlying_Type
(Typ
)), Nam
);
133 end Find_Inherited_TSS
;
135 -----------------------
136 -- Get_TSS_Name_Type --
137 -----------------------
139 function Get_TSS_Name
(E
: Entity_Id
) return TSS_Name_Type
is
145 Get_Last_Two_Chars
(Chars
(E
), C1
, C2
);
147 if C1
in 'A' .. 'Z' and then C2
in 'A' .. 'Z' then
150 for J
in TSS_Names
'Range loop
151 if Nm
= TSS_Names
(J
) then
160 ---------------------------------
161 -- Has_Non_Null_Base_Init_Proc --
162 ---------------------------------
164 -- Note: if a base Init_Proc is present, and No_Default_Initialization is
165 -- present, then we must avoid testing for a null init proc, since there
166 -- is no init proc present in this case.
168 function Has_Non_Null_Base_Init_Proc
(Typ
: Entity_Id
) return Boolean is
169 BIP
: constant Entity_Id
:= Base_Init_Proc
(Typ
);
172 and then (Restriction_Active
(No_Default_Initialization
)
173 or else not Is_Null_Init_Proc
(BIP
));
174 end Has_Non_Null_Base_Init_Proc
;
180 function Init_Proc
(Typ
: Entity_Id
) return Entity_Id
is
181 FN
: constant Node_Id
:= Freeze_Node
(Typ
);
188 elsif No
(TSS_Elist
(FN
)) then
192 Elmt
:= First_Elmt
(TSS_Elist
(FN
));
193 while Present
(Elmt
) loop
194 if Is_Init_Proc
(Node
(Elmt
)) then
209 function Is_Init_Proc
(E
: Entity_Id
) return Boolean is
213 Get_Last_Two_Chars
(Chars
(E
), C1
, C2
);
214 return C1
= TSS_Init_Proc
(1) and then C2
= TSS_Init_Proc
(2);
221 function Is_TSS
(E
: Entity_Id
; Nam
: TSS_Name_Type
) return Boolean is
225 Get_Last_Two_Chars
(Chars
(E
), C1
, C2
);
226 return C1
= Nam
(1) and then C2
= Nam
(2);
229 function Is_TSS
(N
: Name_Id
; Nam
: TSS_Name_Type
) return Boolean is
233 Get_Last_Two_Chars
(N
, C1
, C2
);
234 return C1
= Nam
(1) and then C2
= Nam
(2);
237 -------------------------
238 -- Make_Init_Proc_Name --
239 -------------------------
241 function Make_Init_Proc_Name
(Typ
: Entity_Id
) return Name_Id
is
243 return Make_TSS_Name
(Typ
, TSS_Init_Proc
);
244 end Make_Init_Proc_Name
;
250 function Make_TSS_Name
252 Nam
: TSS_Name_Type
) return Name_Id
255 Get_Name_String
(Chars
(Typ
));
256 Add_Char_To_Name_Buffer
(Nam
(1));
257 Add_Char_To_Name_Buffer
(Nam
(2));
261 -------------------------
262 -- Make_TSS_Name_Local --
263 -------------------------
265 function Make_TSS_Name_Local
267 Nam
: TSS_Name_Type
) return Name_Id
270 Get_Name_String
(Chars
(Typ
));
271 Add_Char_To_Name_Buffer
('_');
272 Add_Nat_To_Name_Buffer
(Increment_Serial_Number
);
273 Add_Char_To_Name_Buffer
(Nam
(1));
274 Add_Char_To_Name_Buffer
(Nam
(2));
276 end Make_TSS_Name_Local
;
282 function Same_TSS
(E1
, E2
: Entity_Id
) return Boolean is
289 Get_Last_Two_Chars
(Chars
(E1
), E1C1
, E1C2
);
290 Get_Last_Two_Chars
(Chars
(E2
), E2C1
, E2C2
);
306 procedure Set_Init_Proc
(Typ
: Entity_Id
; Init
: Entity_Id
) is
315 procedure Set_TSS
(Typ
: Entity_Id
; TSS
: Entity_Id
) is
317 -- Make sure body of subprogram is frozen
319 -- Skip this for Init_Proc with No_Default_Initialization, since the
320 -- Init proc is a dummy void entity in this case to be ignored.
322 if Is_Init_Proc
(TSS
)
323 and then Restriction_Active
(No_Default_Initialization
)
327 -- Skip this if not in the same code unit (since it means we are using
328 -- an already existing TSS in another unit)
330 elsif not In_Same_Code_Unit
(Typ
, TSS
) then
333 -- Otherwise make sure body is frozen
336 Append_Freeze_Action
(Typ
, Unit_Declaration_Node
(TSS
));
348 function TSS
(Typ
: Entity_Id
; Nam
: TSS_Name_Type
) return Entity_Id
is
349 FN
: constant Node_Id
:= Freeze_Node
(Typ
);
357 elsif No
(TSS_Elist
(FN
)) then
361 Elmt
:= First_Elmt
(TSS_Elist
(FN
));
362 while Present
(Elmt
) loop
363 if Is_TSS
(Node
(Elmt
), Nam
) then
366 -- For stream subprograms, the TSS entity may be a renaming-
367 -- as-body of an already generated entity. Use that one rather
368 -- the one introduced by the renaming, which is an artifact of
369 -- current stream handling.
371 if Nkind
(Parent
(Parent
(Subp
))) =
372 N_Subprogram_Renaming_Declaration
374 Present
(Corresponding_Spec
(Parent
(Parent
(Subp
))))
376 return Corresponding_Spec
(Parent
(Parent
(Subp
)));
390 function TSS
(Typ
: Entity_Id
; Nam
: Name_Id
) return Entity_Id
is
391 FN
: constant Node_Id
:= Freeze_Node
(Typ
);
399 elsif No
(TSS_Elist
(FN
)) then
403 Elmt
:= First_Elmt
(TSS_Elist
(FN
));
404 while Present
(Elmt
) loop
405 if Chars
(Node
(Elmt
)) = Nam
then
408 -- For stream subprograms, the TSS entity may be a renaming-
409 -- as-body of an already generated entity. Use that one rather
410 -- the one introduced by the renaming, which is an artifact of
411 -- current stream handling.
413 if Nkind
(Parent
(Parent
(Subp
))) =
414 N_Subprogram_Renaming_Declaration
416 Present
(Corresponding_Spec
(Parent
(Parent
(Subp
))))
418 return Corresponding_Spec
(Parent
(Parent
(Subp
)));