separate (Par)
procedure Labl is
Enclosing_Body_Or_Block : Node_Id;
Label_Decl_Node : Node_Id;
Defining_Ident_Node : Node_Id;
Next_Label_Elmt : Elmt_Id;
Label_Node : Node_Id;
function Find_Enclosing_Body_Or_Block (N : Node_Id) return Node_Id;
function Find_Enclosing_Body (N : Node_Id) return Node_Id;
procedure Check_Distinct_Labels;
procedure Find_Natural_Loops;
procedure Check_Distinct_Labels is
Label_Id : constant Node_Id := Identifier (Label_Node);
Enclosing_Body : constant Node_Id :=
Find_Enclosing_Body (Enclosing_Body_Or_Block);
Next_Other_Label_Elmt : Elmt_Id := First_Elmt (Label_List);
Other_Label : Node_Id;
begin
while Present (Next_Other_Label_Elmt) loop
Other_Label := Node (Next_Other_Label_Elmt);
exit when Label_Node = Other_Label;
if Chars (Label_Id) = Chars (Identifier (Other_Label))
and then Enclosing_Body = Find_Enclosing_Body (Other_Label)
then
Error_Msg_Sloc := Sloc (Other_Label);
Error_Msg_N ("& conflicts with label#", Label_Id);
exit;
end if;
Next_Elmt (Next_Other_Label_Elmt);
end loop;
end Check_Distinct_Labels;
function Find_Enclosing_Body (N : Node_Id) return Node_Id is
Result : Node_Id := N;
begin
while Present (Result)
and then Nkind (Result) /= N_Entry_Body
and then Nkind (Result) /= N_Task_Body
and then Nkind (Result) /= N_Package_Body
and then Nkind (Result) /= N_Subprogram_Body
loop
Result := Parent (Result);
end loop;
return Result;
end Find_Enclosing_Body;
function Find_Enclosing_Body_Or_Block (N : Node_Id) return Node_Id is
Result : Node_Id := Parent (N);
begin
while Present (Result)
and then Nkind (Result) /= N_Accept_Statement
and then Nkind (Result) /= N_Entry_Body
and then Nkind (Result) /= N_Task_Body
and then Nkind (Result) /= N_Package_Body
and then Nkind (Result) /= N_Subprogram_Body
and then Nkind (Result) /= N_Block_Statement
loop
Result := Parent (Result);
end loop;
return Result;
end Find_Enclosing_Body_Or_Block;
procedure Find_Natural_Loops is
Node_List : constant Elist_Id := New_Elmt_List;
N : Elmt_Id;
Succ : Elmt_Id;
function Goto_Id (Goto_Node : Node_Id) return Name_Id;
function Matches
(Label_Node : Node_Id;
Goto_Node : Node_Id) return Boolean;
procedure Merge;
procedure No_Header (N : Elmt_Id);
procedure Process_Goto (N : Elmt_Id);
procedure Rewrite_As_Loop
(Loop_Header : Node_Id;
Loop_End : Node_Id);
procedure Try_Loop (N : Elmt_Id);
function Goto_Id (Goto_Node : Node_Id) return Name_Id is
begin
if Nkind (Name (Goto_Node)) = N_Identifier then
return Chars (Name (Goto_Node));
elsif Nkind (Name (Goto_Node)) = N_Selected_Component then
return Chars (Selector_Name (Name (Goto_Node)));
else
return Name_Null;
end if;
end Goto_Id;
function Matches
(Label_Node : Node_Id;
Goto_Node : Node_Id) return Boolean
is
begin
return Chars (Identifier (Label_Node)) = Goto_Id (Goto_Node)
and then Find_Enclosing_Body (Label_Node) =
Find_Enclosing_Body (Goto_Node);
end Matches;
procedure Merge is
L1 : Elmt_Id;
G1 : Elmt_Id;
begin
L1 := First_Elmt (Label_List);
G1 := First_Elmt (Goto_List);
while Present (L1)
and then Present (G1)
loop
if Sloc (Node (L1)) < Sloc (Node (G1)) then
if Nkind (Node (L1)) /= N_Loop_Statement
and then Nkind (Node (L1)) /= N_Block_Statement
then
Append_Elmt (Node (L1), Node_List);
end if;
Next_Elmt (L1);
else
Append_Elmt (Node (G1), Node_List);
Next_Elmt (G1);
end if;
end loop;
while Present (L1) loop
Append_Elmt (Node (L1), Node_List);
Next_Elmt (L1);
end loop;
while Present (G1) loop
Append_Elmt (Node (G1), Node_List);
Next_Elmt (G1);
end loop;
end Merge;
procedure No_Header (N : Elmt_Id) is
S1, S2 : Elmt_Id;
begin
S1 := Next_Elmt (N);
while Present (S1) loop
S2 := Next_Elmt (S1);
if Nkind (Node (S1)) = N_Goto_Statement
and then Matches (Node (N), Node (S1))
then
Remove_Elmt (Node_List, S1);
end if;
S1 := S2;
end loop;
end No_Header;
procedure Process_Goto (N : Elmt_Id) is
Goto1 : constant Node_Id := Node (N);
Goto2 : Node_Id;
S, S1 : Elmt_Id;
begin
S := Next_Elmt (N);
while Present (S) loop
S1 := Next_Elmt (S);
Goto2 := Node (S);
if Nkind (Goto2) = N_Goto_Statement
and then Goto_Id (Goto1) = Goto_Id (Goto2)
and then Find_Enclosing_Body (Goto1) =
Find_Enclosing_Body (Goto2)
then
Remove_Elmt (Node_List, S);
end if;
S := S1;
end loop;
end Process_Goto;
procedure Rewrite_As_Loop
(Loop_Header : Node_Id;
Loop_End : Node_Id)
is
Loop_Body : constant List_Id := New_List;
Loop_Stmt : constant Node_Id :=
New_Node (N_Loop_Statement, Sloc (Loop_Header));
Stat : Node_Id;
Next_Stat : Node_Id;
begin
Stat := Next (Loop_Header);
while Stat /= Loop_End loop
Next_Stat := Next (Stat);
Remove (Stat);
Append (Stat, Loop_Body);
Stat := Next_Stat;
end loop;
Set_Statements (Loop_Stmt, Loop_Body);
Set_Identifier (Loop_Stmt, Identifier (Loop_Header));
Remove (Loop_Header);
Rewrite (Loop_End, Loop_Stmt);
Error_Msg_N
("code between label and backwards goto rewritten as loop?",
Loop_End);
end Rewrite_As_Loop;
procedure Try_Loop (N : Elmt_Id) is
Source : Elmt_Id;
Found : Boolean := False;
S1 : Elmt_Id;
begin
S1 := Next_Elmt (N);
while Present (S1) loop
if Nkind (Node (S1)) = N_Goto_Statement
and then Matches (Node (N), Node (S1))
then
if not Found then
if Parent (Node (N)) = Parent (Node (S1)) then
Source := S1;
Found := True;
else
No_Header (N);
return;
end if;
else
No_Header (N);
return;
end if;
elsif Nkind (Node (S1)) = N_Label
and then not Found
then
No_Header (N);
return;
else
null;
end if;
Next_Elmt (S1);
end loop;
if Found then
Rewrite_As_Loop (Node (N), Node (Source));
Remove_Elmt (Node_List, N);
Remove_Elmt (Node_List, Source);
end if;
end Try_Loop;
begin
Merge;
N := First_Elmt (Node_List);
while Present (N) loop
Succ := Next_Elmt (N);
if Nkind (Node (N)) = N_Label then
if No (Succ) then
exit;
elsif Nkind (Node (Succ)) = N_Label then
Try_Loop (Succ);
Succ := Next_Elmt (N);
if Nkind (Node (Succ)) = N_Label then
No_Header (N);
else
Try_Loop (N);
Succ := Next_Elmt (N);
end if;
elsif Nkind (Node (Succ)) = N_Goto_Statement then
Try_Loop (N);
Succ := Next_Elmt (N);
end if;
elsif Nkind (Node (N)) = N_Goto_Statement then
Process_Goto (N);
Succ := Next_Elmt (N);
end if;
N := Succ;
end loop;
end Find_Natural_Loops;
begin
Next_Label_Elmt := First_Elmt (Label_List);
while Present (Next_Label_Elmt) loop
Label_Node := Node (Next_Label_Elmt);
if not Comes_From_Source (Label_Node) then
goto Next_Label;
end if;
Enclosing_Body_Or_Block := Find_Enclosing_Body_Or_Block (Label_Node);
if Present (Enclosing_Body_Or_Block) then
Check_Distinct_Labels;
Label_Decl_Node :=
New_Node (N_Implicit_Label_Declaration, Sloc (Label_Node));
Defining_Ident_Node :=
New_Entity (N_Defining_Identifier, Sloc (Identifier (Label_Node)));
Set_Chars (Defining_Ident_Node, Chars (Identifier (Label_Node)));
Set_Defining_Identifier (Label_Decl_Node, Defining_Ident_Node);
Set_Label_Construct (Label_Decl_Node, Label_Node);
Set_Comes_From_Source
(Defining_Ident_Node, Comes_From_Source (Identifier (Label_Node)));
if not Present (Declarations (Enclosing_Body_Or_Block)) then
Set_Declarations (Enclosing_Body_Or_Block, New_List);
end if;
Append (Label_Decl_Node, Declarations (Enclosing_Body_Or_Block));
end if;
<<Next_Label>>
Next_Elmt (Next_Label_Elmt);
end loop;
Find_Natural_Loops;
end Labl;