2013-11-25 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ada / aspects.adb
blob091af77ef77e156f4ab7b13b5d66233cc7f798eb
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-2013, 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. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 with Atree; use Atree;
33 with Einfo; use Einfo;
34 with Nlists; use Nlists;
35 with Sinfo; use Sinfo;
36 with Tree_IO; use Tree_IO;
38 with GNAT.HTable; use GNAT.HTable;
40 package body Aspects is
42 -- The following array indicates aspects that a subtype inherits from its
43 -- base type. True means that the subtype inherits the aspect from its base
44 -- type. False means it is not inherited.
46 Base_Aspect : constant array (Aspect_Id) of Boolean :=
47 (Aspect_Atomic => True,
48 Aspect_Atomic_Components => True,
49 Aspect_Constant_Indexing => True,
50 Aspect_Default_Iterator => True,
51 Aspect_Discard_Names => True,
52 Aspect_Independent_Components => True,
53 Aspect_Iterator_Element => True,
54 Aspect_Type_Invariant => True,
55 Aspect_Unchecked_Union => True,
56 Aspect_Variable_Indexing => True,
57 Aspect_Volatile => True,
58 others => False);
60 -- The following array indicates type aspects that are inherited and apply
61 -- to the class-wide type as well.
63 Inherited_Aspect : constant array (Aspect_Id) of Boolean :=
64 (Aspect_Constant_Indexing => True,
65 Aspect_Default_Iterator => True,
66 Aspect_Implicit_Dereference => True,
67 Aspect_Iterator_Element => True,
68 Aspect_Remote_Types => True,
69 Aspect_Variable_Indexing => True,
70 others => False);
72 procedure Set_Aspect_Specifications_No_Check (N : Node_Id; L : List_Id);
73 -- Same as Set_Aspect_Specifications, but does not contain the assertion
74 -- that checks that N does not already have aspect specifications. This
75 -- subprogram is supposed to be used as a part of Tree_Read. When reading
76 -- tree, first read nodes with their basic properties (as Atree.Tree_Read),
77 -- this includes reading the Has_Aspects flag for each node, then we reed
78 -- all the list tables and only after that we call Tree_Read for Aspects.
79 -- That is, when reading the tree, the list of aspects is attached to the
80 -- node that already has Has_Aspects flag set ON.
82 ------------------------------------------
83 -- Hash Table for Aspect Specifications --
84 ------------------------------------------
86 type AS_Hash_Range is range 0 .. 510;
87 -- Size of hash table headers
89 function AS_Hash (F : Node_Id) return AS_Hash_Range;
90 -- Hash function for hash table
92 function AS_Hash (F : Node_Id) return AS_Hash_Range is
93 begin
94 return AS_Hash_Range (F mod 511);
95 end AS_Hash;
97 package Aspect_Specifications_Hash_Table is new
98 GNAT.HTable.Simple_HTable
99 (Header_Num => AS_Hash_Range,
100 Element => List_Id,
101 No_Element => No_List,
102 Key => Node_Id,
103 Hash => AS_Hash,
104 Equal => "=");
106 -------------------------------------
107 -- Hash Table for Aspect Id Values --
108 -------------------------------------
110 type AI_Hash_Range is range 0 .. 112;
111 -- Size of hash table headers
113 function AI_Hash (F : Name_Id) return AI_Hash_Range;
114 -- Hash function for hash table
116 function AI_Hash (F : Name_Id) return AI_Hash_Range is
117 begin
118 return AI_Hash_Range (F mod 113);
119 end AI_Hash;
121 package Aspect_Id_Hash_Table is new
122 GNAT.HTable.Simple_HTable
123 (Header_Num => AI_Hash_Range,
124 Element => Aspect_Id,
125 No_Element => No_Aspect,
126 Key => Name_Id,
127 Hash => AI_Hash,
128 Equal => "=");
130 ---------------------------
131 -- Aspect_Specifications --
132 ---------------------------
134 function Aspect_Specifications (N : Node_Id) return List_Id is
135 begin
136 if Has_Aspects (N) then
137 return Aspect_Specifications_Hash_Table.Get (N);
138 else
139 return No_List;
140 end if;
141 end Aspect_Specifications;
143 --------------------------------
144 -- Aspects_On_Body_Or_Stub_OK --
145 --------------------------------
147 function Aspects_On_Body_Or_Stub_OK (N : Node_Id) return Boolean is
148 Aspect : Node_Id;
149 Aspects : List_Id;
151 begin
152 -- The routine should be invoked on a body [stub] with aspects
154 pragma Assert (Has_Aspects (N));
155 pragma Assert (Nkind (N) in N_Body_Stub
156 or else Nkind_In (N, N_Package_Body,
157 N_Protected_Body,
158 N_Subprogram_Body,
159 N_Task_Body));
161 -- Look through all aspects and see whether they can be applied to a
162 -- body [stub].
164 Aspects := Aspect_Specifications (N);
165 Aspect := First (Aspects);
166 while Present (Aspect) loop
167 if not Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Aspect)) then
168 return False;
169 end if;
171 Next (Aspect);
172 end loop;
174 return True;
175 end Aspects_On_Body_Or_Stub_OK;
177 -----------------
178 -- Find_Aspect --
179 -----------------
181 function Find_Aspect (Id : Entity_Id; A : Aspect_Id) return Node_Id is
182 Decl : Node_Id;
183 Item : Node_Id;
184 Owner : Entity_Id;
185 Spec : Node_Id;
187 begin
188 Owner := Id;
190 -- Handle various cases of base or inherited aspects for types
192 if Is_Type (Id) then
193 if Base_Aspect (A) then
194 Owner := Base_Type (Owner);
195 end if;
197 if Is_Class_Wide_Type (Owner) and then Inherited_Aspect (A) then
198 Owner := Root_Type (Owner);
199 end if;
201 if Is_Private_Type (Owner) and then Present (Full_View (Owner)) then
202 Owner := Full_View (Owner);
203 end if;
204 end if;
206 -- Search the representation items for the desired aspect
208 Item := First_Rep_Item (Owner);
209 while Present (Item) loop
210 if Nkind (Item) = N_Aspect_Specification
211 and then Get_Aspect_Id (Item) = A
212 then
213 return Item;
214 end if;
216 Next_Rep_Item (Item);
217 end loop;
219 -- Note that not all aspects are added to the chain of representation
220 -- items. In such cases, search the list of aspect specifications. First
221 -- find the declaration node where the aspects reside. This is usually
222 -- the parent or the parent of the parent.
224 Decl := Parent (Owner);
225 if not Permits_Aspect_Specifications (Decl) then
226 Decl := Parent (Decl);
227 end if;
229 -- Search the list of aspect specifications for the desired aspect
231 if Permits_Aspect_Specifications (Decl) then
232 Spec := First (Aspect_Specifications (Decl));
233 while Present (Spec) loop
234 if Get_Aspect_Id (Spec) = A then
235 return Spec;
236 end if;
238 Next (Spec);
239 end loop;
240 end if;
242 -- The entity does not carry any aspects or the desired aspect was not
243 -- found.
245 return Empty;
246 end Find_Aspect;
248 --------------------------
249 -- Find_Value_Of_Aspect --
250 --------------------------
252 function Find_Value_Of_Aspect
253 (Id : Entity_Id;
254 A : Aspect_Id) return Node_Id
256 Spec : constant Node_Id := Find_Aspect (Id, A);
258 begin
259 if Present (Spec) then
260 if A = Aspect_Default_Iterator then
261 return Expression (Aspect_Rep_Item (Spec));
262 else
263 return Expression (Spec);
264 end if;
265 end if;
267 return Empty;
268 end Find_Value_Of_Aspect;
270 -------------------
271 -- Get_Aspect_Id --
272 -------------------
274 function Get_Aspect_Id (Name : Name_Id) return Aspect_Id is
275 begin
276 return Aspect_Id_Hash_Table.Get (Name);
277 end Get_Aspect_Id;
279 function Get_Aspect_Id (Aspect : Node_Id) return Aspect_Id is
280 begin
281 pragma Assert (Nkind (Aspect) = N_Aspect_Specification);
282 return Aspect_Id_Hash_Table.Get (Chars (Identifier (Aspect)));
283 end Get_Aspect_Id;
285 ----------------
286 -- Has_Aspect --
287 ----------------
289 function Has_Aspect (Id : Entity_Id; A : Aspect_Id) return Boolean is
290 begin
291 return Present (Find_Aspect (Id, A));
292 end Has_Aspect;
294 ------------------
295 -- Move_Aspects --
296 ------------------
298 procedure Move_Aspects (From : Node_Id; To : Node_Id) is
299 pragma Assert (not Has_Aspects (To));
300 begin
301 if Has_Aspects (From) then
302 Set_Aspect_Specifications (To, Aspect_Specifications (From));
303 Aspect_Specifications_Hash_Table.Remove (From);
304 Set_Has_Aspects (From, False);
305 end if;
306 end Move_Aspects;
308 ---------------------------
309 -- Move_Or_Merge_Aspects --
310 ---------------------------
312 procedure Move_Or_Merge_Aspects (From : Node_Id; To : Node_Id) is
313 begin
314 if Has_Aspects (From) then
316 -- Merge the aspects of From into To. Make sure that From has no
317 -- aspects after the merge takes place.
319 if Has_Aspects (To) then
320 Append_List
321 (List => Aspect_Specifications (From),
322 To => Aspect_Specifications (To));
323 Remove_Aspects (From);
325 -- Otherwise simply move the aspects
327 else
328 Move_Aspects (From => From, To => To);
329 end if;
330 end if;
331 end Move_Or_Merge_Aspects;
333 -----------------------------------
334 -- Permits_Aspect_Specifications --
335 -----------------------------------
337 Has_Aspect_Specifications_Flag : constant array (Node_Kind) of Boolean :=
338 (N_Abstract_Subprogram_Declaration => True,
339 N_Component_Declaration => True,
340 N_Entry_Declaration => True,
341 N_Exception_Declaration => True,
342 N_Exception_Renaming_Declaration => True,
343 N_Expression_Function => True,
344 N_Formal_Abstract_Subprogram_Declaration => True,
345 N_Formal_Concrete_Subprogram_Declaration => True,
346 N_Formal_Object_Declaration => True,
347 N_Formal_Package_Declaration => True,
348 N_Formal_Type_Declaration => True,
349 N_Full_Type_Declaration => True,
350 N_Function_Instantiation => True,
351 N_Generic_Package_Declaration => True,
352 N_Generic_Renaming_Declaration => True,
353 N_Generic_Subprogram_Declaration => True,
354 N_Object_Declaration => True,
355 N_Object_Renaming_Declaration => True,
356 N_Package_Body => True,
357 N_Package_Body_Stub => True,
358 N_Package_Declaration => True,
359 N_Package_Instantiation => True,
360 N_Package_Specification => True,
361 N_Package_Renaming_Declaration => True,
362 N_Private_Extension_Declaration => True,
363 N_Private_Type_Declaration => True,
364 N_Procedure_Instantiation => True,
365 N_Protected_Body => True,
366 N_Protected_Body_Stub => True,
367 N_Protected_Type_Declaration => True,
368 N_Single_Protected_Declaration => True,
369 N_Single_Task_Declaration => True,
370 N_Subprogram_Body => True,
371 N_Subprogram_Body_Stub => True,
372 N_Subprogram_Declaration => True,
373 N_Subprogram_Renaming_Declaration => True,
374 N_Subtype_Declaration => True,
375 N_Task_Body => True,
376 N_Task_Body_Stub => True,
377 N_Task_Type_Declaration => True,
378 others => False);
380 function Permits_Aspect_Specifications (N : Node_Id) return Boolean is
381 begin
382 return Has_Aspect_Specifications_Flag (Nkind (N));
383 end Permits_Aspect_Specifications;
385 --------------------
386 -- Remove_Aspects --
387 --------------------
389 procedure Remove_Aspects (N : Node_Id) is
390 begin
391 if Has_Aspects (N) then
392 Aspect_Specifications_Hash_Table.Remove (N);
393 Set_Has_Aspects (N, False);
394 end if;
395 end Remove_Aspects;
397 -----------------
398 -- Same_Aspect --
399 -----------------
401 -- Table used for Same_Aspect, maps aspect to canonical aspect
403 Canonical_Aspect : constant array (Aspect_Id) of Aspect_Id :=
404 (No_Aspect => No_Aspect,
405 Aspect_Abstract_State => Aspect_Abstract_State,
406 Aspect_Ada_2005 => Aspect_Ada_2005,
407 Aspect_Ada_2012 => Aspect_Ada_2005,
408 Aspect_Address => Aspect_Address,
409 Aspect_Alignment => Aspect_Alignment,
410 Aspect_All_Calls_Remote => Aspect_All_Calls_Remote,
411 Aspect_Asynchronous => Aspect_Asynchronous,
412 Aspect_Atomic => Aspect_Atomic,
413 Aspect_Atomic_Components => Aspect_Atomic_Components,
414 Aspect_Attach_Handler => Aspect_Attach_Handler,
415 Aspect_Bit_Order => Aspect_Bit_Order,
416 Aspect_Compiler_Unit => Aspect_Compiler_Unit,
417 Aspect_Component_Size => Aspect_Component_Size,
418 Aspect_Constant_Indexing => Aspect_Constant_Indexing,
419 Aspect_Contract_Cases => Aspect_Contract_Cases,
420 Aspect_Convention => Aspect_Convention,
421 Aspect_CPU => Aspect_CPU,
422 Aspect_Default_Component_Value => Aspect_Default_Component_Value,
423 Aspect_Default_Iterator => Aspect_Default_Iterator,
424 Aspect_Default_Value => Aspect_Default_Value,
425 Aspect_Depends => Aspect_Depends,
426 Aspect_Dimension => Aspect_Dimension,
427 Aspect_Dimension_System => Aspect_Dimension_System,
428 Aspect_Discard_Names => Aspect_Discard_Names,
429 Aspect_Dispatching_Domain => Aspect_Dispatching_Domain,
430 Aspect_Dynamic_Predicate => Aspect_Predicate,
431 Aspect_Elaborate_Body => Aspect_Elaborate_Body,
432 Aspect_Export => Aspect_Export,
433 Aspect_External_Name => Aspect_External_Name,
434 Aspect_External_Tag => Aspect_External_Tag,
435 Aspect_Favor_Top_Level => Aspect_Favor_Top_Level,
436 Aspect_Global => Aspect_Global,
437 Aspect_Implicit_Dereference => Aspect_Implicit_Dereference,
438 Aspect_Import => Aspect_Import,
439 Aspect_Independent => Aspect_Independent,
440 Aspect_Independent_Components => Aspect_Independent_Components,
441 Aspect_Inline => Aspect_Inline,
442 Aspect_Inline_Always => Aspect_Inline,
443 Aspect_Initial_Condition => Aspect_Initial_Condition,
444 Aspect_Initializes => Aspect_Initializes,
445 Aspect_Input => Aspect_Input,
446 Aspect_Interrupt_Handler => Aspect_Interrupt_Handler,
447 Aspect_Interrupt_Priority => Aspect_Priority,
448 Aspect_Invariant => Aspect_Invariant,
449 Aspect_Iterator_Element => Aspect_Iterator_Element,
450 Aspect_Link_Name => Aspect_Link_Name,
451 Aspect_Lock_Free => Aspect_Lock_Free,
452 Aspect_Machine_Radix => Aspect_Machine_Radix,
453 Aspect_No_Return => Aspect_No_Return,
454 Aspect_Object_Size => Aspect_Object_Size,
455 Aspect_Output => Aspect_Output,
456 Aspect_Pack => Aspect_Pack,
457 Aspect_Persistent_BSS => Aspect_Persistent_BSS,
458 Aspect_Post => Aspect_Post,
459 Aspect_Postcondition => Aspect_Post,
460 Aspect_Pre => Aspect_Pre,
461 Aspect_Precondition => Aspect_Pre,
462 Aspect_Predicate => Aspect_Predicate,
463 Aspect_Preelaborate => Aspect_Preelaborate,
464 Aspect_Preelaborate_05 => Aspect_Preelaborate_05,
465 Aspect_Preelaborable_Initialization => Aspect_Preelaborable_Initialization,
466 Aspect_Priority => Aspect_Priority,
467 Aspect_Pure => Aspect_Pure,
468 Aspect_Pure_05 => Aspect_Pure_05,
469 Aspect_Pure_12 => Aspect_Pure_12,
470 Aspect_Pure_Function => Aspect_Pure_Function,
471 Aspect_Refined_Depends => Aspect_Refined_Depends,
472 Aspect_Refined_Global => Aspect_Refined_Global,
473 Aspect_Refined_Post => Aspect_Refined_Post,
474 Aspect_Refined_State => Aspect_Refined_State,
475 Aspect_Remote_Access_Type => Aspect_Remote_Access_Type,
476 Aspect_Remote_Call_Interface => Aspect_Remote_Call_Interface,
477 Aspect_Remote_Types => Aspect_Remote_Types,
478 Aspect_Read => Aspect_Read,
479 Aspect_Relative_Deadline => Aspect_Relative_Deadline,
480 Aspect_Scalar_Storage_Order => Aspect_Scalar_Storage_Order,
481 Aspect_Shared => Aspect_Atomic,
482 Aspect_Shared_Passive => Aspect_Shared_Passive,
483 Aspect_Simple_Storage_Pool => Aspect_Simple_Storage_Pool,
484 Aspect_Simple_Storage_Pool_Type => Aspect_Simple_Storage_Pool_Type,
485 Aspect_Size => Aspect_Size,
486 Aspect_Small => Aspect_Small,
487 Aspect_SPARK_Mode => Aspect_SPARK_Mode,
488 Aspect_Static_Predicate => Aspect_Predicate,
489 Aspect_Storage_Pool => Aspect_Storage_Pool,
490 Aspect_Storage_Size => Aspect_Storage_Size,
491 Aspect_Stream_Size => Aspect_Stream_Size,
492 Aspect_Suppress => Aspect_Suppress,
493 Aspect_Suppress_Debug_Info => Aspect_Suppress_Debug_Info,
494 Aspect_Synchronization => Aspect_Synchronization,
495 Aspect_Test_Case => Aspect_Test_Case,
496 Aspect_Type_Invariant => Aspect_Invariant,
497 Aspect_Unchecked_Union => Aspect_Unchecked_Union,
498 Aspect_Universal_Aliasing => Aspect_Universal_Aliasing,
499 Aspect_Universal_Data => Aspect_Universal_Data,
500 Aspect_Unmodified => Aspect_Unmodified,
501 Aspect_Unreferenced => Aspect_Unreferenced,
502 Aspect_Unreferenced_Objects => Aspect_Unreferenced_Objects,
503 Aspect_Unsuppress => Aspect_Unsuppress,
504 Aspect_Variable_Indexing => Aspect_Variable_Indexing,
505 Aspect_Value_Size => Aspect_Value_Size,
506 Aspect_Volatile => Aspect_Volatile,
507 Aspect_Volatile_Components => Aspect_Volatile_Components,
508 Aspect_Warnings => Aspect_Warnings,
509 Aspect_Write => Aspect_Write);
511 function Same_Aspect (A1 : Aspect_Id; A2 : Aspect_Id) return Boolean is
512 begin
513 return Canonical_Aspect (A1) = Canonical_Aspect (A2);
514 end Same_Aspect;
516 -------------------------------
517 -- Set_Aspect_Specifications --
518 -------------------------------
520 procedure Set_Aspect_Specifications (N : Node_Id; L : List_Id) is
521 begin
522 pragma Assert (Permits_Aspect_Specifications (N));
523 pragma Assert (not Has_Aspects (N));
524 pragma Assert (L /= No_List);
526 Set_Has_Aspects (N);
527 Set_Parent (L, N);
528 Aspect_Specifications_Hash_Table.Set (N, L);
529 end Set_Aspect_Specifications;
531 ----------------------------------------
532 -- Set_Aspect_Specifications_No_Check --
533 ----------------------------------------
535 procedure Set_Aspect_Specifications_No_Check (N : Node_Id; L : List_Id) is
536 begin
537 pragma Assert (Permits_Aspect_Specifications (N));
538 pragma Assert (L /= No_List);
540 Set_Has_Aspects (N);
541 Set_Parent (L, N);
542 Aspect_Specifications_Hash_Table.Set (N, L);
543 end Set_Aspect_Specifications_No_Check;
545 ---------------
546 -- Tree_Read --
547 ---------------
549 procedure Tree_Read is
550 Node : Node_Id;
551 List : List_Id;
552 begin
553 loop
554 Tree_Read_Int (Int (Node));
555 Tree_Read_Int (Int (List));
556 exit when List = No_List;
557 Set_Aspect_Specifications_No_Check (Node, List);
558 end loop;
559 end Tree_Read;
561 ----------------
562 -- Tree_Write --
563 ----------------
565 procedure Tree_Write is
566 Node : Node_Id := Empty;
567 List : List_Id;
568 begin
569 Aspect_Specifications_Hash_Table.Get_First (Node, List);
570 loop
571 Tree_Write_Int (Int (Node));
572 Tree_Write_Int (Int (List));
573 exit when List = No_List;
574 Aspect_Specifications_Hash_Table.Get_Next (Node, List);
575 end loop;
576 end Tree_Write;
578 -- Package initialization sets up Aspect Id hash table
580 begin
581 for J in Aspect_Id loop
582 Aspect_Id_Hash_Table.Set (Aspect_Names (J), J);
583 end loop;
584 end Aspects;