Debian Codegraph @

gcc/ada/aspects.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- A S P E C T S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2010-2025, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
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;
34
35 with GNAT.HTable;
36
37 package body Aspects is
38
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.
42
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,
57 others => False);
58
59 -- The following array indicates type aspects that are inherited and apply
60 -- to the class-wide type as well.
61
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,
69 others => False);
70
71 -------------------------------------
72 -- Hash Table for Aspect Id Values --
73 -------------------------------------
74
75 type AI_Hash_Range is range 0 .. 112;
76 -- Size of hash table headers
77
78 function AI_Hash (F : Name_Id) return AI_Hash_Range;
79 -- Hash function for hash table
80
81 function AI_Hash (F : Name_Id) return AI_Hash_Range is
82 begin
83 return AI_Hash_Range (F mod 113);
84 end AI_Hash;
85
86 package Aspect_Id_Hash_Table is new
87 GNAT.HTable.Simple_HTable
88 (Header_Num => AI_Hash_Range,
89 Element => Aspect_Id,
90 No_Element => No_Aspect,
91 Key => Name_Id,
92 Hash => AI_Hash,
93 Equal => "=");
94
95 --------------------------------
96 -- Aspects_On_Body_Or_Stub_OK --
97 --------------------------------
98
99 function Aspects_On_Body_Or_Stub_OK (N : Node_Id) return Boolean is
100 Aspect : Node_Id;
101 Aspects : List_Id;
102
103 begin
104 -- The routine should be invoked on a body [stub] with aspects
105
106 pragma Assert (Has_Aspects (N));
107 pragma Assert
108 (Nkind (N) in N_Body_Stub | N_Entry_Body | N_Package_Body |
109 N_Protected_Body | N_Subprogram_Body | N_Task_Body);
110
111 -- Look through all aspects and see whether they can be applied to a
112 -- body [stub].
113
114 Aspects := Aspect_Specifications (N);
115 Aspect := First (Aspects);
116 while Present (Aspect) loop
117 if not Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Aspect)) then
118 return False;
119 end if;
120
121 Next (Aspect);
122 end loop;
123
124 return True;
125 end Aspects_On_Body_Or_Stub_OK;
126
127 -----------------
128 -- Find_Aspect --
129 -----------------
130
131 function Find_Aspect
132 (Id : Entity_Id;
133 A : Aspect_Id;
134 Class_Present : Boolean := False;
135 Or_Rep_Item : Boolean := False) return Node_Id
136 is
137 Decl : Node_Id;
138 Item : Node_Id;
139 Owner : Entity_Id;
140 Spec : Node_Id;
141 Alternative_Rep_Item : Node_Id := Empty;
142 begin
143 Owner := Id;
144
145 -- Handle various cases of base or inherited aspects for types
146
147 if Is_Type (Id) then
148 if Base_Aspect (A) then
149 Owner := Base_Type (Owner);
150 end if;
151
152 if Is_Class_Wide_Type (Owner) and then Inherited_Aspect (A) then
153 Owner := Root_Type (Owner);
154 end if;
155
156 if Is_Private_Type (Owner)
157 and then Present (Full_View (Owner))
158 and then not Operational_Aspect (A)
159 then
160 Owner := Full_View (Owner);
161 end if;
162 end if;
163
164 -- Search the representation items for the desired aspect
165
166 Item := First_Rep_Item (Owner);
167 while Present (Item) loop
168 if Nkind (Item) = N_Aspect_Specification
169 and then Get_Aspect_Id (Item) = A
170 and then Class_Present = Sinfo.Nodes.Class_Present (Item)
171 then
172 return Item;
173
174 -- We could do something similar here for an N_Pragma node
175 -- when Get_Aspect_Id (Pragma_Name (Item)) = A, but let's
176 -- wait for a demonstrated need.
177
178 elsif Or_Rep_Item
179 and then not Class_Present
180 and then Nkind (Item) = N_Attribute_Definition_Clause
181 and then Get_Aspect_Id (Chars (Item)) = A
182 then
183 -- Remember this candidate in case we don't find anything better
184 Alternative_Rep_Item := Item;
185 end if;
186
187 Next_Rep_Item (Item);
188 end loop;
189
190 -- Note that not all aspects are added to the chain of representation
191 -- items. In such cases, search the list of aspect specifications. First
192 -- find the declaration node where the aspects reside. This is usually
193 -- the parent or the parent of the parent, after getting through the
194 -- additional indirection of the N_Defining_Program_Unit_Name if needed.
195
196 if No (Parent (Owner)) then
197 return Empty;
198 end if;
199
200 Decl := Parent (Owner);
201
202 if Nkind (Decl) = N_Defining_Program_Unit_Name then
203 Decl := Parent (Decl);
204 end if;
205
206 if not Permits_Aspect_Specifications (Decl) then
207 Decl := Parent (Decl);
208
209 if No (Decl) then
210 -- Perhaps this happens because the tree is under construction
211 -- and Parent (Decl) has not been set yet?
212
213 return Empty;
214 end if;
215 end if;
216
217 -- Search the list of aspect specifications for the desired aspect
218
219 if Permits_Aspect_Specifications (Decl) then
220 Spec := First (Aspect_Specifications (Decl));
221 while Present (Spec) loop
222 if Get_Aspect_Id (Spec) = A
223 and then Class_Present = Sinfo.Nodes.Class_Present (Spec)
224 then
225 return Spec;
226 end if;
227
228 declare
229 use User_Aspect_Support;
230 begin
231 if Get_Aspect_Id (Spec) = Aspect_User_Aspect
232 and then not Analyzed (Spec)
233 and then
234 Analyze_User_Aspect_Aspect_Specification_Hook /= null
235 then
236 Analyze_User_Aspect_Aspect_Specification_Hook.all (Spec);
237 end if;
238 end;
239
240 Next (Spec);
241 end loop;
242 end if;
243
244 -- The entity does not carry any aspects or the desired aspect was not
245 -- found. We have no N_Aspect_Specification node to return, but
246 -- Alternative_Rep_Item may have been set (if Or_Rep_Item is True).
247
248 return Alternative_Rep_Item;
249 end Find_Aspect;
250
251 --------------------------
252 -- Find_Value_Of_Aspect --
253 --------------------------
254
255 function Find_Value_Of_Aspect
256 (Id : Entity_Id;
257 A : Aspect_Id;
258 Class_Present : Boolean := False) return Node_Id
259 is
260 Spec : constant Node_Id := Find_Aspect (Id, A,
261 Class_Present => Class_Present);
262
263 begin
264 if Present (Spec) then
265 if A = Aspect_Default_Iterator
266 and then Present (Aspect_Rep_Item (Spec))
267 then
268 return Expression (Aspect_Rep_Item (Spec));
269 else
270 return Expression (Spec);
271 end if;
272 end if;
273
274 return Empty;
275 end Find_Value_Of_Aspect;
276
277 -------------------
278 -- Get_Aspect_Id --
279 -------------------
280
281 function Get_Aspect_Id (Name : Name_Id) return Aspect_Id is
282 begin
283 return Aspect_Id_Hash_Table.Get (Name);
284 end Get_Aspect_Id;
285
286 function Get_Aspect_Id (Aspect : Node_Id) return Aspect_Id is
287 begin
288 pragma Assert (Nkind (Aspect) = N_Aspect_Specification);
289 return Aspect_Id_Hash_Table.Get (Chars (Identifier (Aspect)));
290 end Get_Aspect_Id;
291
292 ----------------
293 -- Has_Aspect --
294 ----------------
295
296 function Has_Aspect
297 (Id : Entity_Id;
298 A : Aspect_Id;
299 Class_Present : Boolean := False) return Boolean
300 is
301 begin
302 return Present (Find_Aspect (Id, A, Class_Present => Class_Present));
303 end Has_Aspect;
304
305 function Has_Aspects (N : Node_Id) return Boolean
306 is (Atree.Present (N) and then
307 Permits_Aspect_Specifications (N) and then
308 Nlists.Present (Sinfo.Nodes.Aspect_Specifications (N)) and then
309 Nlists.Is_Non_Empty_List (Sinfo.Nodes.Aspect_Specifications (N)));
310
311 ------------------
312 -- Is_Aspect_Id --
313 ------------------
314
315 function Is_Aspect_Id (Aspect : Name_Id) return Boolean is
316 (Get_Aspect_Id (Aspect) /= No_Aspect);
317
318 function Is_Aspect_Id (Aspect : Node_Id) return Boolean is
319 (Get_Aspect_Id (Aspect) /= No_Aspect);
320
321 ------------------
322 -- Move_Aspects --
323 ------------------
324
325 procedure Move_Aspects (From : Node_Id; To : Node_Id) is
326 pragma Assert (not Has_Aspects (To));
327 begin
328 if Has_Aspects (From) then
329 Set_Aspect_Specifications (To, Aspect_Specifications (From));
330 Set_Aspect_Specifications (From, No_List);
331 end if;
332 end Move_Aspects;
333
334 ---------------------------
335 -- Move_Or_Merge_Aspects --
336 ---------------------------
337
338 procedure Move_Or_Merge_Aspects (From : Node_Id; To : Node_Id) is
339 procedure Relocate_Aspect (Asp : Node_Id);
340 -- Move aspect specification Asp to the aspect specifications of node To
341
342 ---------------------
343 -- Relocate_Aspect --
344 ---------------------
345
346 procedure Relocate_Aspect (Asp : Node_Id) is
347 Asps : List_Id;
348
349 begin
350 if Has_Aspects (To) then
351 Asps := Aspect_Specifications (To);
352
353 -- Create a new aspect specification list for node To
354
355 else
356 Asps := New_List;
357 Set_Aspect_Specifications (To, Asps);
358 end if;
359
360 -- Remove the aspect from its original owner and relocate it to node
361 -- To.
362
363 Remove (Asp);
364 Append (Asp, Asps);
365 end Relocate_Aspect;
366
367 -- Local variables
368
369 Asp : Node_Id;
370 Asp_Id : Aspect_Id;
371 Next_Asp : Node_Id;
372
373 -- Start of processing for Move_Or_Merge_Aspects
374
375 begin
376 if Has_Aspects (From) then
377 Asp := First (Aspect_Specifications (From));
378 while Present (Asp) loop
379
380 -- Store the next aspect now as a potential relocation will alter
381 -- the contents of the list.
382
383 Next_Asp := Next (Asp);
384
385 -- When moving or merging aspects from a subprogram body stub that
386 -- also acts as a spec, relocate only those aspects that may apply
387 -- to a body [stub]. Note that a precondition must also be moved
388 -- to the proper body as the pre/post machinery expects it to be
389 -- there.
390
391 if Nkind (From) = N_Subprogram_Body_Stub
392 and then No (Corresponding_Spec_Of_Stub (From))
393 then
394 Asp_Id := Get_Aspect_Id (Asp);
395
396 if Aspect_On_Body_Or_Stub_OK (Asp_Id)
397 or else Asp_Id = Aspect_Pre
398 or else Asp_Id = Aspect_Precondition
399 then
400 Relocate_Aspect (Asp);
401 end if;
402
403 -- When moving or merging aspects from a single concurrent type
404 -- declaration, relocate only those aspects that may apply to the
405 -- anonymous object created for the type.
406
407 -- Note: It is better to use Is_Single_Concurrent_Type_Declaration
408 -- here, but Aspects and Sem_Util have incompatible licenses.
409
410 elsif Nkind (Original_Node (From)) in
411 N_Single_Protected_Declaration | N_Single_Task_Declaration
412 then
413 Asp_Id := Get_Aspect_Id (Asp);
414
415 if Aspect_On_Anonymous_Object_OK (Asp_Id) then
416 Relocate_Aspect (Asp);
417 end if;
418
419 -- Default case - relocate the aspect to its new owner
420
421 else
422 Relocate_Aspect (Asp);
423 end if;
424
425 Asp := Next_Asp;
426 end loop;
427
428 -- The relocations may have left node From's aspect specifications
429 -- list empty. If this is the case, simply remove the aspects.
430
431 if Is_Empty_List (Aspect_Specifications (From)) then
432 Remove_Aspects (From);
433 end if;
434 end if;
435 end Move_Or_Merge_Aspects;
436
437 -------------------
438 -- Copy_Aspects --
439 -------------------
440
441 procedure Copy_Aspects (From : Node_Id; To : Node_Id) is
442 begin
443 if not Has_Aspects (From) then
444 return;
445 end if;
446
447 Set_Aspect_Specifications
448 (To, New_Copy_List (Aspect_Specifications (From)));
449 end Copy_Aspects;
450
451 -----------------------------------
452 -- Permits_Aspect_Specifications --
453 -----------------------------------
454
455 Has_Aspect_Specifications_Flag : constant array (Node_Kind) of Boolean :=
456 (N_Abstract_Subprogram_Declaration => True,
457 N_Component_Declaration => True,
458 N_Discriminant_Specification => True,
459 N_Entry_Body => True,
460 N_Entry_Declaration => True,
461 N_Exception_Declaration => True,
462 N_Exception_Renaming_Declaration => True,
463 N_Expression_Function => True,
464 N_Formal_Abstract_Subprogram_Declaration => True,
465 N_Formal_Concrete_Subprogram_Declaration => True,
466 N_Formal_Object_Declaration => True,
467 N_Formal_Package_Declaration => True,
468 N_Formal_Type_Declaration => True,
469 N_Full_Type_Declaration => True,
470 N_Function_Instantiation => True,
471 N_Generic_Package_Declaration => True,
472 N_Generic_Renaming_Declaration => True,
473 N_Generic_Subprogram_Declaration => True,
474 N_Object_Declaration => True,
475 N_Object_Renaming_Declaration => True,
476 N_Package_Body => True,
477 N_Package_Body_Stub => True,
478 N_Package_Declaration => True,
479 N_Package_Instantiation => True,
480 N_Package_Renaming_Declaration => True,
481 N_Package_Specification => True,
482 N_Parameter_Specification => True,
483 N_Private_Extension_Declaration => True,
484 N_Private_Type_Declaration => True,
485 N_Procedure_Instantiation => True,
486 N_Protected_Body => True,
487 N_Protected_Body_Stub => True,
488 N_Protected_Type_Declaration => True,
489 N_Single_Protected_Declaration => True,
490 N_Single_Task_Declaration => True,
491 N_Subprogram_Body => True,
492 N_Subprogram_Body_Stub => True,
493 N_Subprogram_Declaration => True,
494 N_Subprogram_Renaming_Declaration => True,
495 N_Subtype_Declaration => True,
496 N_Task_Body => True,
497 N_Task_Body_Stub => True,
498 N_Task_Type_Declaration => True,
499 others => False);
500
501 function Permits_Aspect_Specifications (N : Node_Id) return Boolean is
502 begin
503 pragma Assert (Present (N));
504 return Has_Aspect_Specifications_Flag (Nkind (N));
505 end Permits_Aspect_Specifications;
506
507 --------------------
508 -- Remove_Aspects --
509 --------------------
510
511 procedure Remove_Aspects (N : Node_Id) is
512 begin
513 if Has_Aspects (N) then
514 Set_Aspect_Specifications (N, No_List);
515 end if;
516 end Remove_Aspects;
517
518 -----------------
519 -- Same_Aspect --
520 -----------------
521
522 -- Table used for Same_Aspect, maps aspect to canonical aspect
523
524 type Aspect_To_Aspect_Mapping is array (Aspect_Id) of Aspect_Id;
525
526 function Init_Canonical_Aspect return Aspect_To_Aspect_Mapping;
527 -- Initialize the Canonical_Aspect mapping below
528
529 function Init_Canonical_Aspect return Aspect_To_Aspect_Mapping is
530 Result : Aspect_To_Aspect_Mapping;
531 begin
532 -- They all map to themselves...
533
534 for Aspect in Aspect_Id loop
535 Result (Aspect) := Aspect;
536 end loop;
537
538 -- ...except for these:
539
540 Result (Aspect_Dynamic_Predicate) := Aspect_Predicate;
541 Result (Aspect_Ghost_Predicate) := Aspect_Predicate;
542 Result (Aspect_Inline_Always) := Aspect_Inline;
543 Result (Aspect_Interrupt_Priority) := Aspect_Priority;
544 Result (Aspect_Postcondition) := Aspect_Post;
545 Result (Aspect_Precondition) := Aspect_Pre;
546 Result (Aspect_Shared) := Aspect_Atomic;
547 Result (Aspect_Static_Predicate) := Aspect_Predicate;
548 Result (Aspect_Type_Invariant) := Aspect_Invariant;
549
550 return Result;
551 end Init_Canonical_Aspect;
552
553 Canonical_Aspect : constant Aspect_To_Aspect_Mapping :=
554 Init_Canonical_Aspect;
555
556 function Same_Aspect (A1 : Aspect_Id; A2 : Aspect_Id) return Boolean is
557 begin
558 return Canonical_Aspect (A1) = Canonical_Aspect (A2);
559 end Same_Aspect;
560
561 package body User_Aspect_Support is
562
563 -- This is similar to the way that user-defined check names are
564 -- managed via package Checks.Check_Names; simple global state.
565
566 UAD_Pragma_Map_Size : constant := 511;
567
568 subtype UAD_Pragma_Map_Header is
569 Integer range 0 .. UAD_Pragma_Map_Size - 1;
570
571 function UAD_Pragma_Map_Hash (Chars : Name_Id)
572 return UAD_Pragma_Map_Header
573 is (UAD_Pragma_Map_Header (Chars mod UAD_Pragma_Map_Size));
574
575 package UAD_Pragma_Map is new GNAT.Htable.Simple_Htable
576 (Header_Num => UAD_Pragma_Map_Header,
577 Key => Name_Id,
578 Element => Opt_N_Pragma_Id,
579 No_Element => Empty,
580 Hash => UAD_Pragma_Map_Hash,
581 Equal => "=");
582
583 procedure Register_UAD_Pragma (UAD_Pragma : Node_Id) is
584 Aspect_Name : constant Name_Id :=
585 Chars (Expression
586 (First (Pragma_Argument_Associations (UAD_Pragma))));
587 begin
588 UAD_Pragma_Map.Set (Aspect_Name, UAD_Pragma);
589 end Register_UAD_Pragma;
590
591 function Registered_UAD_Pragma (Aspect_Name : Name_Id) return Node_Id is
592 begin
593 return UAD_Pragma_Map.Get (Aspect_Name);
594 end Registered_UAD_Pragma;
595 end User_Aspect_Support;
596
597 -- Package initialization sets up Aspect Id hash table
598
599 begin
600 for J in Aspect_Id loop
601 Aspect_Id_Hash_Table.Set (Aspect_Names (J), J);
602 end loop;
603 end Aspects;