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) return Node_Id
206 -- Handle various cases of base or inherited aspects for types
209 if Base_Aspect
(A
) then
210 Owner
:= Base_Type
(Owner
);
213 if Is_Class_Wide_Type
(Owner
) and then Inherited_Aspect
(A
) then
214 Owner
:= Root_Type
(Owner
);
217 if Is_Private_Type
(Owner
)
218 and then Present
(Full_View
(Owner
))
219 and then not Operational_Aspect
(A
)
221 Owner
:= Full_View
(Owner
);
225 -- Search the representation items for the desired aspect
227 Item
:= First_Rep_Item
(Owner
);
228 while Present
(Item
) loop
229 if Nkind
(Item
) = N_Aspect_Specification
230 and then Get_Aspect_Id
(Item
) = A
231 and then Class_Present
= Sinfo
.Nodes
.Class_Present
(Item
)
236 Next_Rep_Item
(Item
);
239 -- Note that not all aspects are added to the chain of representation
240 -- items. In such cases, search the list of aspect specifications. First
241 -- find the declaration node where the aspects reside. This is usually
242 -- the parent or the parent of the parent.
244 if No
(Parent
(Owner
)) then
248 Decl
:= Parent
(Owner
);
249 if not Permits_Aspect_Specifications
(Decl
) then
250 Decl
:= Parent
(Decl
);
253 -- Search the list of aspect specifications for the desired aspect
255 if Permits_Aspect_Specifications
(Decl
) then
256 Spec
:= First
(Aspect_Specifications
(Decl
));
257 while Present
(Spec
) loop
258 if Get_Aspect_Id
(Spec
) = A
259 and then Class_Present
= Sinfo
.Nodes
.Class_Present
(Spec
)
268 -- The entity does not carry any aspects or the desired aspect was not
274 --------------------------
275 -- Find_Value_Of_Aspect --
276 --------------------------
278 function Find_Value_Of_Aspect
281 Class_Present
: Boolean := False) return Node_Id
283 Spec
: constant Node_Id
:= Find_Aspect
(Id
, A
,
284 Class_Present
=> Class_Present
);
287 if Present
(Spec
) then
288 if A
= Aspect_Default_Iterator
289 and then Present
(Aspect_Rep_Item
(Spec
))
291 return Expression
(Aspect_Rep_Item
(Spec
));
293 return Expression
(Spec
);
298 end Find_Value_Of_Aspect
;
304 function Get_Aspect_Id
(Name
: Name_Id
) return Aspect_Id
is
306 return Aspect_Id_Hash_Table
.Get
(Name
);
309 function Get_Aspect_Id
(Aspect
: Node_Id
) return Aspect_Id
is
311 pragma Assert
(Nkind
(Aspect
) = N_Aspect_Specification
);
312 return Aspect_Id_Hash_Table
.Get
(Chars
(Identifier
(Aspect
)));
322 Class_Present
: Boolean := False) return Boolean
325 return Present
(Find_Aspect
(Id
, A
, Class_Present
=> Class_Present
));
332 function Is_Aspect_Id
(Aspect
: Name_Id
) return Boolean is
333 (Get_Aspect_Id
(Aspect
) /= No_Aspect
);
335 function Is_Aspect_Id
(Aspect
: Node_Id
) return Boolean is
336 (Get_Aspect_Id
(Aspect
) /= No_Aspect
);
342 procedure Move_Aspects
(From
: Node_Id
; To
: Node_Id
) is
343 pragma Assert
(not Has_Aspects
(To
));
345 if Has_Aspects
(From
) then
346 Set_Aspect_Specifications
(To
, Aspect_Specifications
(From
));
347 Aspect_Specifications_Hash_Table
.Remove
(From
);
348 Set_Has_Aspects
(From
, False);
352 ---------------------------
353 -- Move_Or_Merge_Aspects --
354 ---------------------------
356 procedure Move_Or_Merge_Aspects
(From
: Node_Id
; To
: Node_Id
) is
357 procedure Relocate_Aspect
(Asp
: Node_Id
);
358 -- Move aspect specification Asp to the aspect specifications of node To
360 ---------------------
361 -- Relocate_Aspect --
362 ---------------------
364 procedure Relocate_Aspect
(Asp
: Node_Id
) is
368 if Has_Aspects
(To
) then
369 Asps
:= Aspect_Specifications
(To
);
371 -- Create a new aspect specification list for node To
375 Set_Aspect_Specifications
(To
, Asps
);
378 -- Remove the aspect from its original owner and relocate it to node
391 -- Start of processing for Move_Or_Merge_Aspects
394 if Has_Aspects
(From
) then
395 Asp
:= First
(Aspect_Specifications
(From
));
396 while Present
(Asp
) loop
398 -- Store the next aspect now as a potential relocation will alter
399 -- the contents of the list.
401 Next_Asp
:= Next
(Asp
);
403 -- When moving or merging aspects from a subprogram body stub that
404 -- also acts as a spec, relocate only those aspects that may apply
405 -- to a body [stub]. Note that a precondition must also be moved
406 -- to the proper body as the pre/post machinery expects it to be
409 if Nkind
(From
) = N_Subprogram_Body_Stub
410 and then No
(Corresponding_Spec_Of_Stub
(From
))
412 Asp_Id
:= Get_Aspect_Id
(Asp
);
414 if Aspect_On_Body_Or_Stub_OK
(Asp_Id
)
415 or else Asp_Id
= Aspect_Pre
416 or else Asp_Id
= Aspect_Precondition
418 Relocate_Aspect
(Asp
);
421 -- When moving or merging aspects from a single concurrent type
422 -- declaration, relocate only those aspects that may apply to the
423 -- anonymous object created for the type.
425 -- Note: It is better to use Is_Single_Concurrent_Type_Declaration
426 -- here, but Aspects and Sem_Util have incompatible licenses.
428 elsif Nkind
(Original_Node
(From
)) in
429 N_Single_Protected_Declaration | N_Single_Task_Declaration
431 Asp_Id
:= Get_Aspect_Id
(Asp
);
433 if Aspect_On_Anonymous_Object_OK
(Asp_Id
) then
434 Relocate_Aspect
(Asp
);
437 -- Default case - relocate the aspect to its new owner
440 Relocate_Aspect
(Asp
);
446 -- The relocations may have left node From's aspect specifications
447 -- list empty. If this is the case, simply remove the aspects.
449 if Is_Empty_List
(Aspect_Specifications
(From
)) then
450 Remove_Aspects
(From
);
453 end Move_Or_Merge_Aspects
;
455 -----------------------------------
456 -- Permits_Aspect_Specifications --
457 -----------------------------------
459 Has_Aspect_Specifications_Flag
: constant array (Node_Kind
) of Boolean :=
460 (N_Abstract_Subprogram_Declaration
=> True,
461 N_Component_Declaration
=> True,
462 N_Entry_Body
=> True,
463 N_Entry_Declaration
=> True,
464 N_Exception_Declaration
=> True,
465 N_Exception_Renaming_Declaration
=> True,
466 N_Expression_Function
=> True,
467 N_Formal_Abstract_Subprogram_Declaration
=> True,
468 N_Formal_Concrete_Subprogram_Declaration
=> True,
469 N_Formal_Object_Declaration
=> True,
470 N_Formal_Package_Declaration
=> True,
471 N_Formal_Type_Declaration
=> True,
472 N_Full_Type_Declaration
=> True,
473 N_Function_Instantiation
=> True,
474 N_Generic_Package_Declaration
=> True,
475 N_Generic_Renaming_Declaration
=> True,
476 N_Generic_Subprogram_Declaration
=> True,
477 N_Object_Declaration
=> True,
478 N_Object_Renaming_Declaration
=> True,
479 N_Package_Body
=> True,
480 N_Package_Body_Stub
=> True,
481 N_Package_Declaration
=> True,
482 N_Package_Instantiation
=> True,
483 N_Package_Specification
=> True,
484 N_Package_Renaming_Declaration
=> True,
485 N_Parameter_Specification
=> True,
486 N_Private_Extension_Declaration
=> True,
487 N_Private_Type_Declaration
=> True,
488 N_Procedure_Instantiation
=> True,
489 N_Protected_Body
=> True,
490 N_Protected_Body_Stub
=> True,
491 N_Protected_Type_Declaration
=> True,
492 N_Single_Protected_Declaration
=> True,
493 N_Single_Task_Declaration
=> True,
494 N_Subprogram_Body
=> True,
495 N_Subprogram_Body_Stub
=> True,
496 N_Subprogram_Declaration
=> True,
497 N_Subprogram_Renaming_Declaration
=> True,
498 N_Subtype_Declaration
=> True,
500 N_Task_Body_Stub
=> True,
501 N_Task_Type_Declaration
=> True,
504 function Permits_Aspect_Specifications
(N
: Node_Id
) return Boolean is
506 pragma Assert
(Present
(N
));
507 return Has_Aspect_Specifications_Flag
(Nkind
(N
));
508 end Permits_Aspect_Specifications
;
514 procedure Remove_Aspects
(N
: Node_Id
) is
516 if Has_Aspects
(N
) then
517 Aspect_Specifications_Hash_Table
.Remove
(N
);
518 Set_Has_Aspects
(N
, False);
526 -- Table used for Same_Aspect, maps aspect to canonical aspect
528 type Aspect_To_Aspect_Mapping
is array (Aspect_Id
) of Aspect_Id
;
530 function Init_Canonical_Aspect
return Aspect_To_Aspect_Mapping
;
531 -- Initialize the Canonical_Aspect mapping below
533 function Init_Canonical_Aspect
return Aspect_To_Aspect_Mapping
is
534 Result
: Aspect_To_Aspect_Mapping
;
536 -- They all map to themselves...
538 for Aspect
in Aspect_Id
loop
539 Result
(Aspect
) := Aspect
;
542 -- ...except for these:
544 Result
(Aspect_Dynamic_Predicate
) := Aspect_Predicate
;
545 Result
(Aspect_Ghost_Predicate
) := Aspect_Predicate
;
546 Result
(Aspect_Inline_Always
) := Aspect_Inline
;
547 Result
(Aspect_Interrupt_Priority
) := Aspect_Priority
;
548 Result
(Aspect_Postcondition
) := Aspect_Post
;
549 Result
(Aspect_Precondition
) := Aspect_Pre
;
550 Result
(Aspect_Shared
) := Aspect_Atomic
;
551 Result
(Aspect_Static_Predicate
) := Aspect_Predicate
;
552 Result
(Aspect_Type_Invariant
) := Aspect_Invariant
;
555 end Init_Canonical_Aspect
;
557 Canonical_Aspect
: constant Aspect_To_Aspect_Mapping
:=
558 Init_Canonical_Aspect
;
560 function Same_Aspect
(A1
: Aspect_Id
; A2
: Aspect_Id
) return Boolean is
562 return Canonical_Aspect
(A1
) = Canonical_Aspect
(A2
);
565 -------------------------------
566 -- Set_Aspect_Specifications --
567 -------------------------------
569 procedure Set_Aspect_Specifications
(N
: Node_Id
; L
: List_Id
) is
571 pragma Assert
(Permits_Aspect_Specifications
(N
));
572 pragma Assert
(not Has_Aspects
(N
));
573 pragma Assert
(L
/= No_List
);
577 Aspect_Specifications_Hash_Table
.Set
(N
, L
);
578 end Set_Aspect_Specifications
;
580 -- Package initialization sets up Aspect Id hash table
583 for J
in Aspect_Id
loop
584 Aspect_Id_Hash_Table
.Set
(Aspect_Names
(J
), J
);