1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2010-2023, 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 Einfo
.Entities
; use Einfo
.Entities
;
29 with Einfo
.Utils
; use Einfo
.Utils
;
30 with Nlists
; use Nlists
;
31 with Sinfo
; use Sinfo
;
32 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
33 with Sinfo
.Utils
; use Sinfo
.Utils
;
37 package body Aspects
is
39 -- The following array indicates aspects that a subtype inherits from its
40 -- base type. True means that the subtype inherits the aspect from its base
41 -- type. False means it is not inherited.
43 Base_Aspect
: constant array (Aspect_Id
) of Boolean :=
44 (Aspect_Atomic
=> True,
45 Aspect_Atomic_Components
=> True,
46 Aspect_Constant_Indexing
=> True,
47 Aspect_Default_Iterator
=> True,
48 Aspect_Discard_Names
=> True,
49 Aspect_Independent_Components
=> True,
50 Aspect_Iterator_Element
=> True,
51 Aspect_Stable_Properties
=> True,
52 Aspect_Type_Invariant
=> True,
53 Aspect_Unchecked_Union
=> True,
54 Aspect_Variable_Indexing
=> True,
55 Aspect_Volatile
=> True,
56 Aspect_Volatile_Full_Access
=> True,
59 -- The following array indicates type aspects that are inherited and apply
60 -- to the class-wide type as well.
62 Inherited_Aspect
: constant array (Aspect_Id
) of Boolean :=
63 (Aspect_Constant_Indexing
=> True,
64 Aspect_Default_Iterator
=> True,
65 Aspect_Implicit_Dereference
=> True,
66 Aspect_Iterator_Element
=> True,
67 Aspect_Remote_Types
=> True,
68 Aspect_Variable_Indexing
=> True,
71 ------------------------------------------
72 -- Hash Table for Aspect Specifications --
73 ------------------------------------------
75 type AS_Hash_Range
is range 0 .. 510;
76 -- Size of hash table headers
78 function AS_Hash
(F
: Node_Id
) return AS_Hash_Range
;
79 -- Hash function for hash table
81 function AS_Hash
(F
: Node_Id
) return AS_Hash_Range
is
83 return AS_Hash_Range
(F
mod 511);
86 package Aspect_Specifications_Hash_Table
is new
87 GNAT
.HTable
.Simple_HTable
88 (Header_Num
=> AS_Hash_Range
,
90 No_Element
=> No_List
,
95 -------------------------------------
96 -- Hash Table for Aspect Id Values --
97 -------------------------------------
99 type AI_Hash_Range
is range 0 .. 112;
100 -- Size of hash table headers
102 function AI_Hash
(F
: Name_Id
) return AI_Hash_Range
;
103 -- Hash function for hash table
105 function AI_Hash
(F
: Name_Id
) return AI_Hash_Range
is
107 return AI_Hash_Range
(F
mod 113);
110 package Aspect_Id_Hash_Table
is new
111 GNAT
.HTable
.Simple_HTable
112 (Header_Num
=> AI_Hash_Range
,
113 Element
=> Aspect_Id
,
114 No_Element
=> No_Aspect
,
119 ---------------------------
120 -- Aspect_Specifications --
121 ---------------------------
123 function Aspect_Specifications
(N
: Node_Id
) return List_Id
is
125 if Has_Aspects
(N
) then
126 return Aspect_Specifications_Hash_Table
.Get
(N
);
130 end Aspect_Specifications
;
132 --------------------------------
133 -- Aspects_On_Body_Or_Stub_OK --
134 --------------------------------
136 function Aspects_On_Body_Or_Stub_OK
(N
: Node_Id
) return Boolean is
141 -- The routine should be invoked on a body [stub] with aspects
143 pragma Assert
(Has_Aspects
(N
));
145 (Nkind
(N
) in N_Body_Stub | N_Entry_Body | N_Package_Body |
146 N_Protected_Body | N_Subprogram_Body | N_Task_Body
);
148 -- Look through all aspects and see whether they can be applied to a
151 Aspects
:= Aspect_Specifications
(N
);
152 Aspect
:= First
(Aspects
);
153 while Present
(Aspect
) loop
154 if not Aspect_On_Body_Or_Stub_OK
(Get_Aspect_Id
(Aspect
)) then
162 end Aspects_On_Body_Or_Stub_OK
;
164 ----------------------
165 -- Exchange_Aspects --
166 ----------------------
168 procedure Exchange_Aspects
(N1
: Node_Id
; N2
: Node_Id
) is
171 (Permits_Aspect_Specifications
(N1
)
172 and then Permits_Aspect_Specifications
(N2
));
174 -- Perform the exchange only when both nodes have lists to be swapped
176 if Has_Aspects
(N1
) and then Has_Aspects
(N2
) then
178 L1
: constant List_Id
:= Aspect_Specifications
(N1
);
179 L2
: constant List_Id
:= Aspect_Specifications
(N2
);
183 Aspect_Specifications_Hash_Table
.Set
(N1
, L2
);
184 Aspect_Specifications_Hash_Table
.Set
(N2
, L1
);
187 end Exchange_Aspects
;
196 Class_Present
: Boolean := False;
197 Or_Rep_Item
: Boolean := False) return Node_Id
203 Alternative_Rep_Item
: Node_Id
:= Empty
;
207 -- Handle various cases of base or inherited aspects for types
210 if Base_Aspect
(A
) then
211 Owner
:= Base_Type
(Owner
);
214 if Is_Class_Wide_Type
(Owner
) and then Inherited_Aspect
(A
) then
215 Owner
:= Root_Type
(Owner
);
218 if Is_Private_Type
(Owner
)
219 and then Present
(Full_View
(Owner
))
220 and then not Operational_Aspect
(A
)
222 Owner
:= Full_View
(Owner
);
226 -- Search the representation items for the desired aspect
228 Item
:= First_Rep_Item
(Owner
);
229 while Present
(Item
) loop
230 if Nkind
(Item
) = N_Aspect_Specification
231 and then Get_Aspect_Id
(Item
) = A
232 and then Class_Present
= Sinfo
.Nodes
.Class_Present
(Item
)
236 -- We could do something similar here for an N_Pragma node
237 -- when Get_Aspect_Id (Pragma_Name (Item)) = A, but let's
238 -- wait for a demonstrated need.
241 and then not Class_Present
242 and then Nkind
(Item
) = N_Attribute_Definition_Clause
243 and then Get_Aspect_Id
(Chars
(Item
)) = A
245 -- Remember this candidate in case we don't find anything better
246 Alternative_Rep_Item
:= Item
;
249 Next_Rep_Item
(Item
);
252 -- Note that not all aspects are added to the chain of representation
253 -- items. In such cases, search the list of aspect specifications. First
254 -- find the declaration node where the aspects reside. This is usually
255 -- the parent or the parent of the parent.
257 if No
(Parent
(Owner
)) then
261 Decl
:= Parent
(Owner
);
262 if not Permits_Aspect_Specifications
(Decl
) then
263 Decl
:= Parent
(Decl
);
266 -- Search the list of aspect specifications for the desired aspect
268 if Permits_Aspect_Specifications
(Decl
) then
269 Spec
:= First
(Aspect_Specifications
(Decl
));
270 while Present
(Spec
) loop
271 if Get_Aspect_Id
(Spec
) = A
272 and then Class_Present
= Sinfo
.Nodes
.Class_Present
(Spec
)
281 -- The entity does not carry any aspects or the desired aspect was not
282 -- found. We have no N_Aspect_Specification node to return, but
283 -- Alternative_Rep_Item may have been set (if Or_Rep_Item is True).
285 return Alternative_Rep_Item
;
288 --------------------------
289 -- Find_Value_Of_Aspect --
290 --------------------------
292 function Find_Value_Of_Aspect
295 Class_Present
: Boolean := False) return Node_Id
297 Spec
: constant Node_Id
:= Find_Aspect
(Id
, A
,
298 Class_Present
=> Class_Present
);
301 if Present
(Spec
) then
302 if A
= Aspect_Default_Iterator
303 and then Present
(Aspect_Rep_Item
(Spec
))
305 return Expression
(Aspect_Rep_Item
(Spec
));
307 return Expression
(Spec
);
312 end Find_Value_Of_Aspect
;
318 function Get_Aspect_Id
(Name
: Name_Id
) return Aspect_Id
is
320 return Aspect_Id_Hash_Table
.Get
(Name
);
323 function Get_Aspect_Id
(Aspect
: Node_Id
) return Aspect_Id
is
325 pragma Assert
(Nkind
(Aspect
) = N_Aspect_Specification
);
326 return Aspect_Id_Hash_Table
.Get
(Chars
(Identifier
(Aspect
)));
336 Class_Present
: Boolean := False) return Boolean
339 return Present
(Find_Aspect
(Id
, A
, Class_Present
=> Class_Present
));
346 function Is_Aspect_Id
(Aspect
: Name_Id
) return Boolean is
347 (Get_Aspect_Id
(Aspect
) /= No_Aspect
);
349 function Is_Aspect_Id
(Aspect
: Node_Id
) return Boolean is
350 (Get_Aspect_Id
(Aspect
) /= No_Aspect
);
356 procedure Move_Aspects
(From
: Node_Id
; To
: Node_Id
) is
357 pragma Assert
(not Has_Aspects
(To
));
359 if Has_Aspects
(From
) then
360 Set_Aspect_Specifications
(To
, Aspect_Specifications
(From
));
361 Aspect_Specifications_Hash_Table
.Remove
(From
);
362 Set_Has_Aspects
(From
, False);
366 ---------------------------
367 -- Move_Or_Merge_Aspects --
368 ---------------------------
370 procedure Move_Or_Merge_Aspects
(From
: Node_Id
; To
: Node_Id
) is
371 procedure Relocate_Aspect
(Asp
: Node_Id
);
372 -- Move aspect specification Asp to the aspect specifications of node To
374 ---------------------
375 -- Relocate_Aspect --
376 ---------------------
378 procedure Relocate_Aspect
(Asp
: Node_Id
) is
382 if Has_Aspects
(To
) then
383 Asps
:= Aspect_Specifications
(To
);
385 -- Create a new aspect specification list for node To
389 Set_Aspect_Specifications
(To
, Asps
);
392 -- Remove the aspect from its original owner and relocate it to node
405 -- Start of processing for Move_Or_Merge_Aspects
408 if Has_Aspects
(From
) then
409 Asp
:= First
(Aspect_Specifications
(From
));
410 while Present
(Asp
) loop
412 -- Store the next aspect now as a potential relocation will alter
413 -- the contents of the list.
415 Next_Asp
:= Next
(Asp
);
417 -- When moving or merging aspects from a subprogram body stub that
418 -- also acts as a spec, relocate only those aspects that may apply
419 -- to a body [stub]. Note that a precondition must also be moved
420 -- to the proper body as the pre/post machinery expects it to be
423 if Nkind
(From
) = N_Subprogram_Body_Stub
424 and then No
(Corresponding_Spec_Of_Stub
(From
))
426 Asp_Id
:= Get_Aspect_Id
(Asp
);
428 if Aspect_On_Body_Or_Stub_OK
(Asp_Id
)
429 or else Asp_Id
= Aspect_Pre
430 or else Asp_Id
= Aspect_Precondition
432 Relocate_Aspect
(Asp
);
435 -- When moving or merging aspects from a single concurrent type
436 -- declaration, relocate only those aspects that may apply to the
437 -- anonymous object created for the type.
439 -- Note: It is better to use Is_Single_Concurrent_Type_Declaration
440 -- here, but Aspects and Sem_Util have incompatible licenses.
442 elsif Nkind
(Original_Node
(From
)) in
443 N_Single_Protected_Declaration | N_Single_Task_Declaration
445 Asp_Id
:= Get_Aspect_Id
(Asp
);
447 if Aspect_On_Anonymous_Object_OK
(Asp_Id
) then
448 Relocate_Aspect
(Asp
);
451 -- Default case - relocate the aspect to its new owner
454 Relocate_Aspect
(Asp
);
460 -- The relocations may have left node From's aspect specifications
461 -- list empty. If this is the case, simply remove the aspects.
463 if Is_Empty_List
(Aspect_Specifications
(From
)) then
464 Remove_Aspects
(From
);
467 end Move_Or_Merge_Aspects
;
469 -----------------------------------
470 -- Permits_Aspect_Specifications --
471 -----------------------------------
473 Has_Aspect_Specifications_Flag
: constant array (Node_Kind
) of Boolean :=
474 (N_Abstract_Subprogram_Declaration
=> True,
475 N_Component_Declaration
=> True,
476 N_Entry_Body
=> True,
477 N_Entry_Declaration
=> True,
478 N_Exception_Declaration
=> True,
479 N_Exception_Renaming_Declaration
=> True,
480 N_Expression_Function
=> True,
481 N_Formal_Abstract_Subprogram_Declaration
=> True,
482 N_Formal_Concrete_Subprogram_Declaration
=> True,
483 N_Formal_Object_Declaration
=> True,
484 N_Formal_Package_Declaration
=> True,
485 N_Formal_Type_Declaration
=> True,
486 N_Full_Type_Declaration
=> True,
487 N_Function_Instantiation
=> True,
488 N_Generic_Package_Declaration
=> True,
489 N_Generic_Renaming_Declaration
=> True,
490 N_Generic_Subprogram_Declaration
=> True,
491 N_Object_Declaration
=> True,
492 N_Object_Renaming_Declaration
=> True,
493 N_Package_Body
=> True,
494 N_Package_Body_Stub
=> True,
495 N_Package_Declaration
=> True,
496 N_Package_Instantiation
=> True,
497 N_Package_Specification
=> True,
498 N_Package_Renaming_Declaration
=> True,
499 N_Parameter_Specification
=> True,
500 N_Private_Extension_Declaration
=> True,
501 N_Private_Type_Declaration
=> True,
502 N_Procedure_Instantiation
=> True,
503 N_Protected_Body
=> True,
504 N_Protected_Body_Stub
=> True,
505 N_Protected_Type_Declaration
=> True,
506 N_Single_Protected_Declaration
=> True,
507 N_Single_Task_Declaration
=> True,
508 N_Subprogram_Body
=> True,
509 N_Subprogram_Body_Stub
=> True,
510 N_Subprogram_Declaration
=> True,
511 N_Subprogram_Renaming_Declaration
=> True,
512 N_Subtype_Declaration
=> True,
514 N_Task_Body_Stub
=> True,
515 N_Task_Type_Declaration
=> True,
518 function Permits_Aspect_Specifications
(N
: Node_Id
) return Boolean is
520 pragma Assert
(Present
(N
));
521 return Has_Aspect_Specifications_Flag
(Nkind
(N
));
522 end Permits_Aspect_Specifications
;
528 procedure Remove_Aspects
(N
: Node_Id
) is
530 if Has_Aspects
(N
) then
531 Aspect_Specifications_Hash_Table
.Remove
(N
);
532 Set_Has_Aspects
(N
, False);
540 -- Table used for Same_Aspect, maps aspect to canonical aspect
542 type Aspect_To_Aspect_Mapping
is array (Aspect_Id
) of Aspect_Id
;
544 function Init_Canonical_Aspect
return Aspect_To_Aspect_Mapping
;
545 -- Initialize the Canonical_Aspect mapping below
547 function Init_Canonical_Aspect
return Aspect_To_Aspect_Mapping
is
548 Result
: Aspect_To_Aspect_Mapping
;
550 -- They all map to themselves...
552 for Aspect
in Aspect_Id
loop
553 Result
(Aspect
) := Aspect
;
556 -- ...except for these:
558 Result
(Aspect_Dynamic_Predicate
) := Aspect_Predicate
;
559 Result
(Aspect_Ghost_Predicate
) := Aspect_Predicate
;
560 Result
(Aspect_Inline_Always
) := Aspect_Inline
;
561 Result
(Aspect_Interrupt_Priority
) := Aspect_Priority
;
562 Result
(Aspect_Postcondition
) := Aspect_Post
;
563 Result
(Aspect_Precondition
) := Aspect_Pre
;
564 Result
(Aspect_Shared
) := Aspect_Atomic
;
565 Result
(Aspect_Static_Predicate
) := Aspect_Predicate
;
566 Result
(Aspect_Type_Invariant
) := Aspect_Invariant
;
569 end Init_Canonical_Aspect
;
571 Canonical_Aspect
: constant Aspect_To_Aspect_Mapping
:=
572 Init_Canonical_Aspect
;
574 function Same_Aspect
(A1
: Aspect_Id
; A2
: Aspect_Id
) return Boolean is
576 return Canonical_Aspect
(A1
) = Canonical_Aspect
(A2
);
579 -------------------------------
580 -- Set_Aspect_Specifications --
581 -------------------------------
583 procedure Set_Aspect_Specifications
(N
: Node_Id
; L
: List_Id
) is
585 pragma Assert
(Permits_Aspect_Specifications
(N
));
586 pragma Assert
(not Has_Aspects
(N
));
587 pragma Assert
(L
/= No_List
);
591 Aspect_Specifications_Hash_Table
.Set
(N
, L
);
592 end Set_Aspect_Specifications
;
594 -- Package initialization sets up Aspect Id hash table
597 for J
in Aspect_Id
loop
598 Aspect_Id_Hash_Table
.Set
(Aspect_Names
(J
), J
);