1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1997-2007, 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 Errout; use Errout;
29 with Namet; use Namet;
30 with Nlists; use Nlists;
31 with Sem_Prag; use Sem_Prag;
32 with Sinput; use Sinput;
33 with Sinfo; use Sinfo;
34 with Snames; use Snames;
35 with Stand; use Stand;
36 with Stringt; use Stringt;
39 with GNAT.HTable; use GNAT.HTable;
41 package body Sem_Elim is
43 No_Elimination : Boolean;
44 -- Set True if no Eliminate pragmas active
50 -- A single pragma Eliminate is represented by the following record
53 type Access_Elim_Data is access Elim_Data;
55 type Names is array (Nat range <>) of Name_Id;
56 -- Type used to represent set of names. Used for names in Unit_Name
57 -- and also the set of names in Argument_Types.
59 type Access_Names is access Names;
61 type Elim_Data is record
63 Unit_Name : Access_Names;
64 -- Unit name, broken down into a set of names (e.g. A.B.C is
65 -- represented as Name_Id values for A, B, C in sequence).
67 Entity_Name : Name_Id;
68 -- Entity name if Entity parameter if present. If no Entity parameter
69 -- was supplied, then Entity_Node is set to Empty, and the Entity_Name
70 -- field contains the last identifier name in the Unit_Name.
72 Entity_Scope : Access_Names;
73 -- Static scope of the entity within the compilation unit represented by
76 Entity_Node : Node_Id;
77 -- Save node of entity argument, for posting error messages. Set
78 -- to Empty if there is no entity argument.
80 Parameter_Types : Access_Names;
81 -- Set to set of names given for parameter types. If no parameter
82 -- types argument is present, this argument is set to null.
84 Result_Type : Name_Id;
85 -- Result type name if Result_Types parameter present, No_Name if not
87 Source_Location : Name_Id;
88 -- String describing the source location of subprogram defining name if
89 -- Source_Location parameter present, No_Name if not
91 Hash_Link : Access_Elim_Data;
92 -- Link for hash table use
94 Homonym : Access_Elim_Data;
95 -- Pointer to next entry with same key
98 -- Node_Id for Eliminate pragma
106 -- Setup hash table using the Entity_Name field as the hash key
108 subtype Element is Elim_Data;
109 subtype Elmt_Ptr is Access_Elim_Data;
111 subtype Key is Name_Id;
113 type Header_Num is range 0 .. 1023;
115 Null_Ptr : constant Elmt_Ptr := null;
117 ----------------------
118 -- Hash_Subprograms --
119 ----------------------
121 package Hash_Subprograms is
123 function Equal (F1, F2 : Key) return Boolean;
124 pragma Inline (Equal);
126 function Get_Key (E : Elmt_Ptr) return Key;
127 pragma Inline (Get_Key);
129 function Hash (F : Key) return Header_Num;
130 pragma Inline (Hash);
132 function Next (E : Elmt_Ptr) return Elmt_Ptr;
133 pragma Inline (Next);
135 procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
136 pragma Inline (Set_Next);
138 end Hash_Subprograms;
140 package body Hash_Subprograms is
146 function Equal (F1, F2 : Key) return Boolean is
155 function Get_Key (E : Elmt_Ptr) return Key is
157 return E.Entity_Name;
164 function Hash (F : Key) return Header_Num is
166 return Header_Num (Int (F) mod 1024);
173 function Next (E : Elmt_Ptr) return Elmt_Ptr is
182 procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr) is
186 end Hash_Subprograms;
192 -- The following table records the data for each pragmas, using the
193 -- entity name as the hash key for retrieval. Entries in this table
194 -- are set by Process_Eliminate_Pragma and read by Check_Eliminated.
196 package Elim_Hash_Table is new Static_HTable (
197 Header_Num => Header_Num,
199 Elmt_Ptr => Elmt_Ptr,
200 Null_Ptr => Null_Ptr,
201 Set_Next => Hash_Subprograms.Set_Next,
202 Next => Hash_Subprograms.Next,
204 Get_Key => Hash_Subprograms.Get_Key,
205 Hash => Hash_Subprograms.Hash,
206 Equal => Hash_Subprograms.Equal);
208 -- The following table records entities for subprograms that are
209 -- eliminated, and corresponding eliminate pragmas that caused the
210 -- elimination. Entries in this table are set by Check_Eliminated
211 -- and read by Eliminate_Error_Msg.
213 type Elim_Entity_Entry is record
218 package Elim_Entities is new Table.Table (
219 Table_Component_Type => Elim_Entity_Entry,
220 Table_Index_Type => Name_Id'Base,
221 Table_Low_Bound => First_Name_Id,
223 Table_Increment => 200,
224 Table_Name => "Elim_Entries");
226 ----------------------
227 -- Check_Eliminated --
228 ----------------------
230 procedure Check_Eliminated (E : Entity_Id) is
231 Elmt : Access_Elim_Data;
235 function Original_Chars (S : Entity_Id) return Name_Id;
236 -- If the candidate subprogram is a protected operation of a single
237 -- protected object, the scope of the operation is the created
238 -- protected type, and we have to retrieve the original name of
245 function Original_Chars (S : Entity_Id) return Name_Id is
247 if Ekind (S) /= E_Protected_Type
248 or else Comes_From_Source (S)
252 return Chars (Defining_Identifier (Original_Node (Parent (S))));
256 -- Start of processing for Check_Eliminated
259 if No_Elimination then
262 -- Elimination of objects and types is not implemented yet
264 elsif Ekind (E) not in Subprogram_Kind then
268 -- Loop through homonyms for this key
270 Elmt := Elim_Hash_Table.Get (Chars (E));
271 while Elmt /= null loop
273 procedure Set_Eliminated;
274 -- Set current subprogram entity as eliminated
280 procedure Set_Eliminated is
282 -- Never try to eliminate dispatching operation, since we
283 -- can't properly process the eliminated result. This could
284 -- be fixed, but is not worth it.
286 if not Is_Dispatching_Operation (E) then
287 Set_Is_Eliminated (E);
288 Elim_Entities.Append ((Prag => Elmt.Prag, Subp => E));
293 -- First we check that the name of the entity matches
295 if Elmt.Entity_Name /= Chars (E) then
299 -- Then we need to see if the static scope matches within the
302 -- At the moment, gnatelim does not consider block statements as
303 -- scopes (even if a block is named)
306 while Ekind (Scop) = E_Block loop
307 Scop := Scope (Scop);
310 if Elmt.Entity_Scope /= null then
311 for J in reverse Elmt.Entity_Scope'Range loop
312 if Elmt.Entity_Scope (J) /= Original_Chars (Scop) then
316 Scop := Scope (Scop);
317 while Ekind (Scop) = E_Block loop
318 Scop := Scope (Scop);
321 if not Is_Compilation_Unit (Scop) and then J = 1 then
327 -- Now see if compilation unit matches
329 for J in reverse Elmt.Unit_Name'Range loop
330 if Elmt.Unit_Name (J) /= Chars (Scop) then
334 Scop := Scope (Scop);
335 while Ekind (Scop) = E_Block loop
336 Scop := Scope (Scop);
339 if Scop /= Standard_Standard and then J = 1 then
344 if Scop /= Standard_Standard then
348 -- Check for case of given entity is a library level subprogram
349 -- and we have the single parameter Eliminate case, a match!
351 if Is_Compilation_Unit (E)
352 and then Is_Subprogram (E)
353 and then No (Elmt.Entity_Node)
358 -- Check for case of type or object with two parameter case
360 elsif (Is_Type (E) or else Is_Object (E))
361 and then Elmt.Result_Type = No_Name
362 and then Elmt.Parameter_Types = null
367 -- Check for case of subprogram
369 elsif Ekind (E) = E_Function
370 or else Ekind (E) = E_Procedure
372 -- If Source_Location present, then see if it matches
374 if Elmt.Source_Location /= No_Name then
375 Get_Name_String (Elmt.Source_Location);
378 Sloc_Trace : constant String :=
379 Name_Buffer (1 .. Name_Len);
381 Idx : Natural := Sloc_Trace'First;
382 -- Index in Sloc_Trace, if equals to 0, then we have
383 -- completely traversed Sloc_Trace
385 Last : constant Natural := Sloc_Trace'Last;
388 Sindex : Source_File_Index;
390 function File_Name_Match return Boolean;
391 -- This function is supposed to be called when Idx points
392 -- to the beginning of the new file name, and Name_Buffer
393 -- is set to contain the name of the proper source file
394 -- from the chain corresponding to the Sloc of E. First
395 -- it checks that these two files have the same name. If
396 -- this check is successful, moves Idx to point to the
397 -- beginning of the column number.
399 function Line_Num_Match return Boolean;
400 -- This function is supposed to be called when Idx points
401 -- to the beginning of the column number, and P is
402 -- set to point to the proper Sloc the chain
403 -- corresponding to the Sloc of E. First it checks that
404 -- the line number Idx points on and the line number
405 -- corresponding to P are the same. If this check is
406 -- successful, moves Idx to point to the beginning of
407 -- the next file name in Sloc_Trace. If there is no file
408 -- name any more, Idx is set to 0.
410 function Different_Trace_Lengths return Boolean;
411 -- From Idx and P, defines if there are in both traces
412 -- more element(s) in the instantiation chains. Returns
413 -- False if one trace contains more element(s), but
414 -- another does not. If both traces contains more
415 -- elements (that is, the function returns False), moves
416 -- P ahead in the chain corresponding to E, recomputes
417 -- Sindex and sets the name of the corresponding file in
420 function Skip_Spaces return Natural;
421 -- If Sloc_Trace (Idx) is not space character, returns
422 -- Idx. Otherwise returns the index of the nearest
423 -- non-space character in Sloc_Trace to the right of
424 -- Idx. Returns 0 if there is no such character.
426 -----------------------------
427 -- Different_Trace_Lengths --
428 -----------------------------
430 function Different_Trace_Lengths return Boolean is
432 P := Instantiation (Sindex);
434 if (P = No_Location and then Idx /= 0)
436 (P /= No_Location and then Idx = 0)
441 if P /= No_Location then
442 Sindex := Get_Source_File_Index (P);
443 Get_Name_String (File_Name (Sindex));
448 end Different_Trace_Lengths;
450 ---------------------
451 -- File_Name_Match --
452 ---------------------
454 function File_Name_Match return Boolean is
463 -- Find first colon. If no colon, then return False.
464 -- If there is a colon, Tmp_Idx is set to point just
469 if Tmp_Idx >= Last then
471 elsif Sloc_Trace (Tmp_Idx + 1) = ':' then
474 Tmp_Idx := Tmp_Idx + 1;
478 -- Find last non-space before this colon. If there
479 -- is no no space character before this colon, then
480 -- return False. Otherwise, End_Idx set to point to
481 -- this non-space character.
485 if End_Idx < Idx then
487 elsif Sloc_Trace (End_Idx) /= ' ' then
490 End_Idx := End_Idx - 1;
494 -- Now see if file name matches what is in Name_Buffer
495 -- and if so, step Idx past it and return True. If the
496 -- name does not match, return False.
498 if Sloc_Trace (Idx .. End_Idx) =
499 Name_Buffer (1 .. Name_Len)
513 function Line_Num_Match return Boolean is
522 and then Sloc_Trace (Idx) in '0' .. '9'
525 (Character'Pos (Sloc_Trace (Idx)) -
526 Character'Pos ('0'));
530 if Get_Physical_Line_Number (P) =
531 Physical_Line_Number (N)
533 while Idx <= Last and then
534 Sloc_Trace (Idx) /= '['
539 if Idx <= Last and then
540 Sloc_Trace (Idx) = '['
559 function Skip_Spaces return Natural is
564 while Sloc_Trace (Res) = ' ' loop
578 Sindex := Get_Source_File_Index (P);
579 Get_Name_String (File_Name (Sindex));
583 if not File_Name_Match then
585 elsif not Line_Num_Match then
589 if Different_Trace_Lengths then
596 -- If we have a Result_Type, then we must have a function
597 -- with the proper result type
599 if Elmt.Result_Type /= No_Name then
600 if Ekind (E) /= E_Function
601 or else Chars (Etype (E)) /= Elmt.Result_Type
607 -- If we have Parameter_Types, they must match
609 if Elmt.Parameter_Types /= null then
610 Form := First_Formal (E);
613 and then Elmt.Parameter_Types'Length = 1
614 and then Elmt.Parameter_Types (1) = No_Name
616 -- Parameterless procedure matches
620 elsif Elmt.Parameter_Types = null then
624 for J in Elmt.Parameter_Types'Range loop
627 Chars (Etype (Form)) /= Elmt.Parameter_Types (J)
635 if Present (Form) then
641 -- If we fall through, this is match
649 Elmt := Elmt.Homonym;
653 end Check_Eliminated;
655 -------------------------
656 -- Eliminate_Error_Msg --
657 -------------------------
659 procedure Eliminate_Error_Msg (N : Node_Id; E : Entity_Id) is
661 for J in Elim_Entities.First .. Elim_Entities.Last loop
662 if E = Elim_Entities.Table (J).Subp then
663 Error_Msg_Sloc := Sloc (Elim_Entities.Table (J).Prag);
664 Error_Msg_NE ("cannot call subprogram & eliminated #", N, E);
669 -- Should never fall through, since entry should be in table
672 end Eliminate_Error_Msg;
678 procedure Initialize is
680 Elim_Hash_Table.Reset;
682 No_Elimination := True;
685 ------------------------------
686 -- Process_Eliminate_Pragma --
687 ------------------------------
689 procedure Process_Eliminate_Pragma
690 (Pragma_Node : Node_Id;
691 Arg_Unit_Name : Node_Id;
692 Arg_Entity : Node_Id;
693 Arg_Parameter_Types : Node_Id;
694 Arg_Result_Type : Node_Id;
695 Arg_Source_Location : Node_Id)
697 Data : constant Access_Elim_Data := new Elim_Data;
698 -- Build result data here
700 Elmt : Access_Elim_Data;
702 Num_Names : Nat := 0;
703 -- Number of names in unit name
709 function OK_Selected_Component (N : Node_Id) return Boolean;
710 -- Test if N is a selected component with all identifiers, or a
711 -- selected component whose selector is an operator symbol. As a
712 -- side effect if result is True, sets Num_Names to the number
713 -- of names present (identifiers and operator if any).
715 ---------------------------
716 -- OK_Selected_Component --
717 ---------------------------
719 function OK_Selected_Component (N : Node_Id) return Boolean is
721 if Nkind (N) = N_Identifier
722 or else Nkind (N) = N_Operator_Symbol
724 Num_Names := Num_Names + 1;
727 elsif Nkind (N) = N_Selected_Component then
728 return OK_Selected_Component (Prefix (N))
729 and then OK_Selected_Component (Selector_Name (N));
734 end OK_Selected_Component;
736 -- Start of processing for Process_Eliminate_Pragma
739 Data.Prag := Pragma_Node;
740 Error_Msg_Name_1 := Name_Eliminate;
742 -- Process Unit_Name argument
744 if Nkind (Arg_Unit_Name) = N_Identifier then
745 Data.Unit_Name := new Names'(1 => Chars (Arg_Unit_Name));
748 elsif OK_Selected_Component (Arg_Unit_Name) then
749 Data.Unit_Name := new Names (1 .. Num_Names);
751 Arg_Uname := Arg_Unit_Name;
752 for J in reverse 2 .. Num_Names loop
753 Data.Unit_Name (J) := Chars (Selector_Name (Arg_Uname));
754 Arg_Uname := Prefix (Arg_Uname);
757 Data.Unit_Name (1) := Chars (Arg_Uname);
761 ("wrong form for Unit_Name parameter of pragma%", Arg_Unit_Name);
765 -- Process Entity argument
767 if Present (Arg_Entity) then
770 if Nkind (Arg_Entity) = N_Identifier
771 or else Nkind (Arg_Entity) = N_Operator_Symbol
773 Data.Entity_Name := Chars (Arg_Entity);
774 Data.Entity_Node := Arg_Entity;
775 Data.Entity_Scope := null;
777 elsif OK_Selected_Component (Arg_Entity) then
778 Data.Entity_Scope := new Names (1 .. Num_Names - 1);
779 Data.Entity_Name := Chars (Selector_Name (Arg_Entity));
780 Data.Entity_Node := Arg_Entity;
782 Arg_Ent := Prefix (Arg_Entity);
783 for J in reverse 2 .. Num_Names - 1 loop
784 Data.Entity_Scope (J) := Chars (Selector_Name (Arg_Ent));
785 Arg_Ent := Prefix (Arg_Ent);
788 Data.Entity_Scope (1) := Chars (Arg_Ent);
790 elsif Is_Config_Static_String (Arg_Entity) then
791 Data.Entity_Name := Name_Find;
792 Data.Entity_Node := Arg_Entity;
798 Data.Entity_Node := Empty;
799 Data.Entity_Name := Data.Unit_Name (Num_Names);
802 -- Process Parameter_Types argument
804 if Present (Arg_Parameter_Types) then
806 -- Here for aggregate case
808 if Nkind (Arg_Parameter_Types) = N_Aggregate then
809 Data.Parameter_Types :=
811 (1 .. List_Length (Expressions (Arg_Parameter_Types)));
813 Lit := First (Expressions (Arg_Parameter_Types));
814 for J in Data.Parameter_Types'Range loop
815 if Is_Config_Static_String (Lit) then
816 Data.Parameter_Types (J) := Name_Find;
823 -- Otherwise we must have case of one name, which looks like a
824 -- parenthesized literal rather than an aggregate.
826 elsif Paren_Count (Arg_Parameter_Types) /= 1 then
828 ("wrong form for argument of pragma Eliminate",
829 Arg_Parameter_Types);
832 elsif Is_Config_Static_String (Arg_Parameter_Types) then
833 String_To_Name_Buffer (Strval (Arg_Parameter_Types));
837 -- Parameterless procedure
839 Data.Parameter_Types := new Names'(1 => No_Name);
842 Data.Parameter_Types := new Names'(1 => Name_Find);
850 -- Process Result_Types argument
852 if Present (Arg_Result_Type) then
853 if Is_Config_Static_String (Arg_Result_Type) then
854 Data.Result_Type := Name_Find;
859 -- Here if no Result_Types argument
862 Data.Result_Type := No_Name;
865 -- Process Source_Location argument
867 if Present (Arg_Source_Location) then
868 if Is_Config_Static_String (Arg_Source_Location) then
869 Data.Source_Location := Name_Find;
874 Data.Source_Location := No_Name;
877 Elmt := Elim_Hash_Table.Get (Hash_Subprograms.Get_Key (Data));
879 -- If we already have an entry with this same key, then link
880 -- it into the chain of entries for this key.
883 Data.Homonym := Elmt.Homonym;
884 Elmt.Homonym := Data;
886 -- Otherwise create a new entry
889 Elim_Hash_Table.Set (Data);
892 No_Elimination := False;
893 end Process_Eliminate_Pragma;