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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Einfo; use Einfo;
29 with Errout; use Errout;
30 with Namet; use Namet;
31 with Nlists; use Nlists;
32 with Sem_Prag; use Sem_Prag;
33 with Sinput; use Sinput;
34 with Sinfo; use Sinfo;
35 with Snames; use Snames;
36 with Stand; use Stand;
37 with Stringt; use Stringt;
40 with GNAT.HTable; use GNAT.HTable;
42 package body Sem_Elim is
44 No_Elimination : Boolean;
45 -- Set True if no Eliminate pragmas active
51 -- A single pragma Eliminate is represented by the following record
54 type Access_Elim_Data is access Elim_Data;
56 type Names is array (Nat range <>) of Name_Id;
57 -- Type used to represent set of names. Used for names in Unit_Name
58 -- and also the set of names in Argument_Types.
60 type Access_Names is access Names;
62 type Elim_Data is record
64 Unit_Name : Access_Names;
65 -- Unit name, broken down into a set of names (e.g. A.B.C is
66 -- represented as Name_Id values for A, B, C in sequence).
68 Entity_Name : Name_Id;
69 -- Entity name if Entity parameter if present. If no Entity parameter
70 -- was supplied, then Entity_Node is set to Empty, and the Entity_Name
71 -- field contains the last identifier name in the Unit_Name.
73 Entity_Scope : Access_Names;
74 -- Static scope of the entity within the compilation unit represented by
77 Entity_Node : Node_Id;
78 -- Save node of entity argument, for posting error messages. Set
79 -- to Empty if there is no entity argument.
81 Parameter_Types : Access_Names;
82 -- Set to set of names given for parameter types. If no parameter
83 -- types argument is present, this argument is set to null.
85 Result_Type : Name_Id;
86 -- Result type name if Result_Types parameter present, No_Name if not
88 Source_Location : Name_Id;
89 -- String describing the source location of subprogram defining name if
90 -- Source_Location parameter present, No_Name if not
92 Hash_Link : Access_Elim_Data;
93 -- Link for hash table use
95 Homonym : Access_Elim_Data;
96 -- Pointer to next entry with same key
99 -- Node_Id for Eliminate pragma
107 -- Setup hash table using the Entity_Name field as the hash key
109 subtype Element is Elim_Data;
110 subtype Elmt_Ptr is Access_Elim_Data;
112 subtype Key is Name_Id;
114 type Header_Num is range 0 .. 1023;
116 Null_Ptr : constant Elmt_Ptr := null;
118 ----------------------
119 -- Hash_Subprograms --
120 ----------------------
122 package Hash_Subprograms is
124 function Equal (F1, F2 : Key) return Boolean;
125 pragma Inline (Equal);
127 function Get_Key (E : Elmt_Ptr) return Key;
128 pragma Inline (Get_Key);
130 function Hash (F : Key) return Header_Num;
131 pragma Inline (Hash);
133 function Next (E : Elmt_Ptr) return Elmt_Ptr;
134 pragma Inline (Next);
136 procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
137 pragma Inline (Set_Next);
139 end Hash_Subprograms;
141 package body Hash_Subprograms is
147 function Equal (F1, F2 : Key) return Boolean is
156 function Get_Key (E : Elmt_Ptr) return Key is
158 return E.Entity_Name;
165 function Hash (F : Key) return Header_Num is
167 return Header_Num (Int (F) mod 1024);
174 function Next (E : Elmt_Ptr) return Elmt_Ptr is
183 procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr) is
187 end Hash_Subprograms;
193 -- The following table records the data for each pragmas, using the
194 -- entity name as the hash key for retrieval. Entries in this table
195 -- are set by Process_Eliminate_Pragma and read by Check_Eliminated.
197 package Elim_Hash_Table is new Static_HTable (
198 Header_Num => Header_Num,
200 Elmt_Ptr => Elmt_Ptr,
201 Null_Ptr => Null_Ptr,
202 Set_Next => Hash_Subprograms.Set_Next,
203 Next => Hash_Subprograms.Next,
205 Get_Key => Hash_Subprograms.Get_Key,
206 Hash => Hash_Subprograms.Hash,
207 Equal => Hash_Subprograms.Equal);
209 -- The following table records entities for subprograms that are
210 -- eliminated, and corresponding eliminate pragmas that caused the
211 -- elimination. Entries in this table are set by Check_Eliminated
212 -- and read by Eliminate_Error_Msg.
214 type Elim_Entity_Entry is record
219 package Elim_Entities is new Table.Table (
220 Table_Component_Type => Elim_Entity_Entry,
221 Table_Index_Type => Name_Id'Base,
222 Table_Low_Bound => First_Name_Id,
224 Table_Increment => 200,
225 Table_Name => "Elim_Entries");
227 ----------------------
228 -- Check_Eliminated --
229 ----------------------
231 procedure Check_Eliminated (E : Entity_Id) is
232 Elmt : Access_Elim_Data;
236 function Original_Chars (S : Entity_Id) return Name_Id;
237 -- If the candidate subprogram is a protected operation of a single
238 -- protected object, the scope of the operation is the created
239 -- protected type, and we have to retrieve the original name of
246 function Original_Chars (S : Entity_Id) return Name_Id is
248 if Ekind (S) /= E_Protected_Type
249 or else Comes_From_Source (S)
253 return Chars (Defining_Identifier (Original_Node (Parent (S))));
257 -- Start of processing for Check_Eliminated
260 if No_Elimination then
263 -- Elimination of objects and types is not implemented yet
265 elsif Ekind (E) not in Subprogram_Kind then
269 -- Loop through homonyms for this key
271 Elmt := Elim_Hash_Table.Get (Chars (E));
272 while Elmt /= null loop
274 procedure Set_Eliminated;
275 -- Set current subprogram entity as eliminated
281 procedure Set_Eliminated is
283 -- Never try to eliminate dispatching operation, since we
284 -- can't properly process the eliminated result. This could
285 -- be fixed, but is not worth it.
287 if not Is_Dispatching_Operation (E) then
288 Set_Is_Eliminated (E);
289 Elim_Entities.Append ((Prag => Elmt.Prag, Subp => E));
294 -- First we check that the name of the entity matches
296 if Elmt.Entity_Name /= Chars (E) then
300 -- Then we need to see if the static scope matches within the
303 -- At the moment, gnatelim does not consider block statements as
304 -- scopes (even if a block is named)
307 while Ekind (Scop) = E_Block loop
308 Scop := Scope (Scop);
311 if Elmt.Entity_Scope /= null then
312 for J in reverse Elmt.Entity_Scope'Range loop
313 if Elmt.Entity_Scope (J) /= Original_Chars (Scop) then
317 Scop := Scope (Scop);
318 while Ekind (Scop) = E_Block loop
319 Scop := Scope (Scop);
322 if not Is_Compilation_Unit (Scop) and then J = 1 then
328 -- Now see if compilation unit matches
330 for J in reverse Elmt.Unit_Name'Range loop
331 if Elmt.Unit_Name (J) /= Chars (Scop) then
335 Scop := Scope (Scop);
336 while Ekind (Scop) = E_Block loop
337 Scop := Scope (Scop);
340 if Scop /= Standard_Standard and then J = 1 then
345 if Scop /= Standard_Standard then
349 -- Check for case of given entity is a library level subprogram
350 -- and we have the single parameter Eliminate case, a match!
352 if Is_Compilation_Unit (E)
353 and then Is_Subprogram (E)
354 and then No (Elmt.Entity_Node)
359 -- Check for case of type or object with two parameter case
361 elsif (Is_Type (E) or else Is_Object (E))
362 and then Elmt.Result_Type = No_Name
363 and then Elmt.Parameter_Types = null
368 -- Check for case of subprogram
370 elsif Ekind (E) = E_Function
371 or else Ekind (E) = E_Procedure
373 -- If Source_Location present, then see if it matches
375 if Elmt.Source_Location /= No_Name then
376 Get_Name_String (Elmt.Source_Location);
379 Sloc_Trace : constant String :=
380 Name_Buffer (1 .. Name_Len);
382 Idx : Natural := Sloc_Trace'First;
383 -- Index in Sloc_Trace, if equals to 0, then we have
384 -- completely traversed Sloc_Trace
386 Last : constant Natural := Sloc_Trace'Last;
389 Sindex : Source_File_Index;
391 function File_Name_Match return Boolean;
392 -- This function is supposed to be called when Idx points
393 -- to the beginning of the new file name, and Name_Buffer
394 -- is set to contain the name of the proper source file
395 -- from the chain corresponding to the Sloc of E. First
396 -- it checks that these two files have the same name. If
397 -- this check is successful, moves Idx to point to the
398 -- beginning of the column number.
400 function Line_Num_Match return Boolean;
401 -- This function is supposed to be called when Idx points
402 -- to the beginning of the column number, and P is
403 -- set to point to the proper Sloc the chain
404 -- corresponding to the Sloc of E. First it checks that
405 -- the line number Idx points on and the line number
406 -- corresponding to P are the same. If this check is
407 -- successful, moves Idx to point to the beginning of
408 -- the next file name in Sloc_Trace. If there is no file
409 -- name any more, Idx is set to 0.
411 function Different_Trace_Lengths return Boolean;
412 -- From Idx and P, defines if there are in both traces
413 -- more element(s) in the instantiation chains. Returns
414 -- False if one trace contains more element(s), but
415 -- another does not. If both traces contains more
416 -- elements (that is, the function returns False), moves
417 -- P ahead in the chain corresponding to E, recomputes
418 -- Sindex and sets the name of the corresponding file in
421 function Skip_Spaces return Natural;
422 -- If Sloc_Trace (Idx) is not space character, returns
423 -- Idx. Otherwise returns the index of the nearest
424 -- non-space character in Sloc_Trace to the right of
425 -- Idx. Returns 0 if there is no such character.
427 -----------------------------
428 -- Different_Trace_Lengths --
429 -----------------------------
431 function Different_Trace_Lengths return Boolean is
433 P := Instantiation (Sindex);
435 if (P = No_Location and then Idx /= 0)
437 (P /= No_Location and then Idx = 0)
442 if P /= No_Location then
443 Sindex := Get_Source_File_Index (P);
444 Get_Name_String (File_Name (Sindex));
449 end Different_Trace_Lengths;
451 ---------------------
452 -- File_Name_Match --
453 ---------------------
455 function File_Name_Match return Boolean is
464 -- Find first colon. If no colon, then return False.
465 -- If there is a colon, Tmp_Idx is set to point just
470 if Tmp_Idx >= Last then
472 elsif Sloc_Trace (Tmp_Idx + 1) = ':' then
475 Tmp_Idx := Tmp_Idx + 1;
479 -- Find last non-space before this colon. If there
480 -- is no no space character before this colon, then
481 -- return False. Otherwise, End_Idx set to point to
482 -- this non-space character.
486 if End_Idx < Idx then
488 elsif Sloc_Trace (End_Idx) /= ' ' then
491 End_Idx := End_Idx - 1;
495 -- Now see if file name matches what is in Name_Buffer
496 -- and if so, step Idx past it and return True. If the
497 -- name does not match, return False.
499 if Sloc_Trace (Idx .. End_Idx) =
500 Name_Buffer (1 .. Name_Len)
514 function Line_Num_Match return Boolean is
523 and then Sloc_Trace (Idx) in '0' .. '9'
526 (Character'Pos (Sloc_Trace (Idx)) -
527 Character'Pos ('0'));
531 if Get_Physical_Line_Number (P) =
532 Physical_Line_Number (N)
534 while Idx <= Last and then
535 Sloc_Trace (Idx) /= '['
540 if Idx <= Last and then
541 Sloc_Trace (Idx) = '['
560 function Skip_Spaces return Natural is
565 while Sloc_Trace (Res) = ' ' loop
579 Sindex := Get_Source_File_Index (P);
580 Get_Name_String (File_Name (Sindex));
584 if not File_Name_Match then
586 elsif not Line_Num_Match then
590 if Different_Trace_Lengths then
597 -- If we have a Result_Type, then we must have a function
598 -- with the proper result type
600 if Elmt.Result_Type /= No_Name then
601 if Ekind (E) /= E_Function
602 or else Chars (Etype (E)) /= Elmt.Result_Type
608 -- If we have Parameter_Types, they must match
610 if Elmt.Parameter_Types /= null then
611 Form := First_Formal (E);
614 and then Elmt.Parameter_Types'Length = 1
615 and then Elmt.Parameter_Types (1) = No_Name
617 -- Parameterless procedure matches
621 elsif Elmt.Parameter_Types = null then
625 for J in Elmt.Parameter_Types'Range loop
628 Chars (Etype (Form)) /= Elmt.Parameter_Types (J)
636 if Present (Form) then
642 -- If we fall through, this is match
650 Elmt := Elmt.Homonym;
654 end Check_Eliminated;
656 -------------------------
657 -- Eliminate_Error_Msg --
658 -------------------------
660 procedure Eliminate_Error_Msg (N : Node_Id; E : Entity_Id) is
662 for J in Elim_Entities.First .. Elim_Entities.Last loop
663 if E = Elim_Entities.Table (J).Subp then
664 Error_Msg_Sloc := Sloc (Elim_Entities.Table (J).Prag);
665 Error_Msg_NE ("cannot call subprogram & eliminated #", N, E);
670 -- Should never fall through, since entry should be in table
673 end Eliminate_Error_Msg;
679 procedure Initialize is
681 Elim_Hash_Table.Reset;
683 No_Elimination := True;
686 ------------------------------
687 -- Process_Eliminate_Pragma --
688 ------------------------------
690 procedure Process_Eliminate_Pragma
691 (Pragma_Node : Node_Id;
692 Arg_Unit_Name : Node_Id;
693 Arg_Entity : Node_Id;
694 Arg_Parameter_Types : Node_Id;
695 Arg_Result_Type : Node_Id;
696 Arg_Source_Location : Node_Id)
698 Data : constant Access_Elim_Data := new Elim_Data;
699 -- Build result data here
701 Elmt : Access_Elim_Data;
703 Num_Names : Nat := 0;
704 -- Number of names in unit name
710 function OK_Selected_Component (N : Node_Id) return Boolean;
711 -- Test if N is a selected component with all identifiers, or a
712 -- selected component whose selector is an operator symbol. As a
713 -- side effect if result is True, sets Num_Names to the number
714 -- of names present (identifiers and operator if any).
716 ---------------------------
717 -- OK_Selected_Component --
718 ---------------------------
720 function OK_Selected_Component (N : Node_Id) return Boolean is
722 if Nkind (N) = N_Identifier
723 or else Nkind (N) = N_Operator_Symbol
725 Num_Names := Num_Names + 1;
728 elsif Nkind (N) = N_Selected_Component then
729 return OK_Selected_Component (Prefix (N))
730 and then OK_Selected_Component (Selector_Name (N));
735 end OK_Selected_Component;
737 -- Start of processing for Process_Eliminate_Pragma
740 Data.Prag := Pragma_Node;
741 Error_Msg_Name_1 := Name_Eliminate;
743 -- Process Unit_Name argument
745 if Nkind (Arg_Unit_Name) = N_Identifier then
746 Data.Unit_Name := new Names'(1 => Chars (Arg_Unit_Name));
749 elsif OK_Selected_Component (Arg_Unit_Name) then
750 Data.Unit_Name := new Names (1 .. Num_Names);
752 Arg_Uname := Arg_Unit_Name;
753 for J in reverse 2 .. Num_Names loop
754 Data.Unit_Name (J) := Chars (Selector_Name (Arg_Uname));
755 Arg_Uname := Prefix (Arg_Uname);
758 Data.Unit_Name (1) := Chars (Arg_Uname);
762 ("wrong form for Unit_Name parameter of pragma%", Arg_Unit_Name);
766 -- Process Entity argument
768 if Present (Arg_Entity) then
771 if Nkind (Arg_Entity) = N_Identifier
772 or else Nkind (Arg_Entity) = N_Operator_Symbol
774 Data.Entity_Name := Chars (Arg_Entity);
775 Data.Entity_Node := Arg_Entity;
776 Data.Entity_Scope := null;
778 elsif OK_Selected_Component (Arg_Entity) then
779 Data.Entity_Scope := new Names (1 .. Num_Names - 1);
780 Data.Entity_Name := Chars (Selector_Name (Arg_Entity));
781 Data.Entity_Node := Arg_Entity;
783 Arg_Ent := Prefix (Arg_Entity);
784 for J in reverse 2 .. Num_Names - 1 loop
785 Data.Entity_Scope (J) := Chars (Selector_Name (Arg_Ent));
786 Arg_Ent := Prefix (Arg_Ent);
789 Data.Entity_Scope (1) := Chars (Arg_Ent);
791 elsif Is_Config_Static_String (Arg_Entity) then
792 Data.Entity_Name := Name_Find;
793 Data.Entity_Node := Arg_Entity;
799 Data.Entity_Node := Empty;
800 Data.Entity_Name := Data.Unit_Name (Num_Names);
803 -- Process Parameter_Types argument
805 if Present (Arg_Parameter_Types) then
807 -- Here for aggregate case
809 if Nkind (Arg_Parameter_Types) = N_Aggregate then
810 Data.Parameter_Types :=
812 (1 .. List_Length (Expressions (Arg_Parameter_Types)));
814 Lit := First (Expressions (Arg_Parameter_Types));
815 for J in Data.Parameter_Types'Range loop
816 if Is_Config_Static_String (Lit) then
817 Data.Parameter_Types (J) := Name_Find;
824 -- Otherwise we must have case of one name, which looks like a
825 -- parenthesized literal rather than an aggregate.
827 elsif Paren_Count (Arg_Parameter_Types) /= 1 then
829 ("wrong form for argument of pragma Eliminate",
830 Arg_Parameter_Types);
833 elsif Is_Config_Static_String (Arg_Parameter_Types) then
834 String_To_Name_Buffer (Strval (Arg_Parameter_Types));
838 -- Parameterless procedure
840 Data.Parameter_Types := new Names'(1 => No_Name);
843 Data.Parameter_Types := new Names'(1 => Name_Find);
851 -- Process Result_Types argument
853 if Present (Arg_Result_Type) then
854 if Is_Config_Static_String (Arg_Result_Type) then
855 Data.Result_Type := Name_Find;
860 -- Here if no Result_Types argument
863 Data.Result_Type := No_Name;
866 -- Process Source_Location argument
868 if Present (Arg_Source_Location) then
869 if Is_Config_Static_String (Arg_Source_Location) then
870 Data.Source_Location := Name_Find;
875 Data.Source_Location := No_Name;
878 Elmt := Elim_Hash_Table.Get (Hash_Subprograms.Get_Key (Data));
880 -- If we already have an entry with this same key, then link
881 -- it into the chain of entries for this key.
884 Data.Homonym := Elmt.Homonym;
885 Elmt.Homonym := Data;
887 -- Otherwise create a new entry
890 Elim_Hash_Table.Set (Data);
893 No_Elimination := False;
894 end Process_Eliminate_Pragma;