Skip to content

Commit 0367f80

Browse files
committed
Support of discriminants and components of protected types/objects
1 parent 691a913 commit 0367f80

File tree

5 files changed

+454
-8
lines changed

5 files changed

+454
-8
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
------------------------------------------------------------------------------
2+
-- GNAT Documentation Generation Tool --
3+
-- --
4+
-- Copyright (C) 2022, AdaCore --
5+
-- --
6+
-- This is free software; you can redistribute it and/or modify it under --
7+
-- terms of the GNU General Public License as published by the Free Soft- --
8+
-- ware Foundation; either version 3, or (at your option) any later ver- --
9+
-- sion. This software is distributed in the hope that it will be useful, --
10+
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
11+
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
12+
-- License for more details. You should have received a copy of the GNU --
13+
-- General Public License distributed with this software; see file --
14+
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
15+
-- of the license. --
16+
------------------------------------------------------------------------------
17+
18+
package body GNATdoc.Comments.Builders.Protecteds is
19+
20+
use Libadalang.Analysis;
21+
use Libadalang.Common;
22+
23+
-----------
24+
-- Build --
25+
-----------
26+
27+
procedure Build
28+
(Self : in out Protected_Components_Builder;
29+
Documentation : not null GNATdoc.Comments.Structured_Comment_Access;
30+
Options : GNATdoc.Comments.Options.Extractor_Options;
31+
Node : Libadalang.Analysis.Basic_Decl'Class)
32+
is
33+
function Process (Node : Ada_Node'Class) return Visit_Status;
34+
35+
-------------
36+
-- Process --
37+
-------------
38+
39+
function Process (Node : Ada_Node'Class) return Visit_Status is
40+
begin
41+
case Node.Kind is
42+
when Ada_Known_Discriminant_Part | Ada_Discriminant_Spec_List
43+
| Ada_Private_Part | Ada_Decl_List
44+
=>
45+
-- Nodes that contains significant nodes inside thier
46+
-- subtrees.
47+
48+
return Into;
49+
50+
when Ada_Pragma_Node =>
51+
-- Nodes that doesn't contains significant information.
52+
53+
return Over;
54+
55+
when Ada_Component_Decl =>
56+
Self.Process_Component_Declaration (Node.As_Component_Decl);
57+
58+
for Name of Node.As_Component_Decl.F_Ids loop
59+
Self.Process_Defining_Name (Field, Name);
60+
end loop;
61+
62+
return Over;
63+
64+
when Ada_Discriminant_Spec =>
65+
Self.Process_Component_Declaration (Node.As_Discriminant_Spec);
66+
67+
for Name of Node.As_Discriminant_Spec.F_Ids loop
68+
Self.Process_Defining_Name (Field, Name);
69+
end loop;
70+
71+
return Over;
72+
73+
when Ada_Subp_Decl | Ada_Entry_Decl =>
74+
Self.Restart_Component_Group (Node.Sloc_Range.Start_Line);
75+
76+
return Over;
77+
78+
when others =>
79+
raise Program_Error with Ada_Node_Kind_Type'Image (Node.Kind);
80+
end case;
81+
end Process;
82+
83+
Discriminants : constant Discriminant_Part :=
84+
(case Node.Kind is
85+
when Ada_Protected_Type_Decl =>
86+
Node.As_Protected_Type_Decl.F_Discriminants,
87+
when others => No_Discriminant_Part);
88+
Definition : constant Protected_Def :=
89+
(case Node.Kind is
90+
when Ada_Single_Protected_Decl =>
91+
Node.As_Single_Protected_Decl.F_Definition,
92+
when Ada_Protected_Type_Decl =>
93+
Node.As_Protected_Type_Decl.F_Definition,
94+
when others => No_Protected_Def);
95+
96+
begin
97+
Self.Initialize (Documentation, Options, Node);
98+
99+
-- Process discriminants of the protected type declaration.
100+
101+
if not Discriminants.Is_Null then
102+
Discriminants.Traverse (Process'Access);
103+
104+
-- Detect first line of the next declartion after discriminants part
105+
106+
declare
107+
Token : Token_Reference := Discriminants.Token_End;
108+
109+
begin
110+
loop
111+
Token := Next (Token);
112+
113+
exit when Token = No_Token;
114+
115+
case Kind (Data (Token)) is
116+
when Ada_Whitespace | Ada_Comment =>
117+
-- Ignore whitespace separators and comments
118+
119+
null;
120+
121+
when Ada_Is | Ada_With =>
122+
-- Ignore 'is' and 'with' keyword that starts aspects
123+
-- specification, interface list of public parts of
124+
-- protected specification: they may be left on the
125+
-- same line with last discriminant.
126+
127+
null;
128+
129+
when others =>
130+
exit;
131+
end case;
132+
end loop;
133+
134+
if Token /= No_Token then
135+
Self.Restart_Component_Group
136+
(Sloc_Range (Data (Token)).End_Line);
137+
end if;
138+
end;
139+
end if;
140+
141+
-- Components of the private type can be declared in the private part
142+
-- only. Public part can contains subprograms/entries only, thus ignore
143+
-- hole public part.
144+
--
145+
-- ??? Should it be controlled by the "generate private" option ???
146+
147+
if not Definition.F_Private_Part.Is_Null then
148+
Definition.F_Private_Part.Traverse (Process'Access);
149+
Self.Restart_Component_Group (Node.Sloc_Range.End_Line);
150+
end if;
151+
152+
Self.Fill_Structured_Comment (Node, Options.Pattern);
153+
end Build;
154+
155+
end GNATdoc.Comments.Builders.Protecteds;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
------------------------------------------------------------------------------
2+
-- GNAT Documentation Generation Tool --
3+
-- --
4+
-- Copyright (C) 2022, AdaCore --
5+
-- --
6+
-- This is free software; you can redistribute it and/or modify it under --
7+
-- terms of the GNU General Public License as published by the Free Soft- --
8+
-- ware Foundation; either version 3, or (at your option) any later ver- --
9+
-- sion. This software is distributed in the hope that it will be useful, --
10+
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
11+
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
12+
-- License for more details. You should have received a copy of the GNU --
13+
-- General Public License distributed with this software; see file --
14+
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
15+
-- of the license. --
16+
------------------------------------------------------------------------------
17+
18+
-- Structured comment builder for protected types and objects. It creates
19+
-- sections for components (and discriminants).
20+
21+
with Libadalang.Analysis;
22+
with Libadalang.Common;
23+
24+
package GNATdoc.Comments.Builders.Protecteds is
25+
26+
use all type Libadalang.Common.Ada_Node_Kind_Type;
27+
28+
type Protected_Components_Builder is
29+
new Abstract_Components_Builder with private;
30+
31+
procedure Build
32+
(Self : in out Protected_Components_Builder;
33+
Documentation : not null GNATdoc.Comments.Structured_Comment_Access;
34+
Options : GNATdoc.Comments.Options.Extractor_Options;
35+
Node : Libadalang.Analysis.Basic_Decl'Class)
36+
with Pre =>
37+
Node.Kind in Ada_Single_Protected_Decl | Ada_Protected_Type_Decl;
38+
39+
private
40+
41+
type Protected_Components_Builder is
42+
new Abstract_Components_Builder with null record;
43+
44+
end GNATdoc.Comments.Builders.Protecteds;

source/gnatdoc-comments-extractor.adb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ with VSS.Strings.Conversions; use VSS.Strings.Conversions;
3131

3232
with GNATdoc.Comments.Builders.Enumerations;
3333
with GNATdoc.Comments.Builders.Generics;
34+
with GNATdoc.Comments.Builders.Protecteds;
3435
with GNATdoc.Comments.Builders.Records;
3536
with GNATdoc.Comments.Builders.Subprograms;
3637
with GNATdoc.Comments.Utilities; use GNATdoc.Comments.Utilities;
@@ -999,8 +1000,13 @@ package body GNATdoc.Comments.Extractor is
9991000
Is_Or_With_Token : Token_Reference;
10001001
Leading_Section : Section_Access;
10011002
Intermediate_Upper_Section : Section_Access;
1003+
Component_Builder :
1004+
GNATdoc.Comments.Builders.Protecteds.Protected_Components_Builder;
10021005

10031006
begin
1007+
Component_Builder.Build
1008+
(Documentation'Unchecked_Access, Options, Node);
1009+
10041010
Extract_Leading_Section
10051011
(Node.Token_Start, Options, True, Documentation, Leading_Section);
10061012

testsuite/extractor/protecteds.ads

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,51 @@ package Protecteds is
2727
-- Intermediate description of the protected type PT_Intermediate.
2828
end PT_Intermediate;
2929

30+
protected type PT_Discriminant_Component_Short
31+
(Discriminant : Integer) -- Short description of the discriminant
32+
is
33+
private
34+
Component : Float; -- Short description of the component
35+
end PT_Discriminant_Component_Short;
36+
37+
protected type PT_Discriminant_Component_Longer
38+
(Discriminant : Integer)
39+
-- Longer description of the discriminant
40+
is
41+
private
42+
Component : Float;
43+
-- Longer description of the component
44+
end PT_Discriminant_Component_Longer;
45+
46+
protected type PT_Discriminant_Longer
47+
(Discriminant : Integer)
48+
-- Longer description of the discriminant
49+
is
50+
end PT_Discriminant_Longer;
51+
52+
protected P_Private_Components_Subprograms is
53+
-- Protected type declaration with mix of components/subprograms in private part.
54+
55+
private
56+
57+
X : Integer;
58+
-- Description of the component X
59+
60+
procedure P;
61+
-- Description of the procedure P
62+
63+
Y : Integer;
64+
-- Description of the component Y
65+
66+
function F return Integer;
67+
-- Description of the function F
68+
69+
Z : Integer;
70+
-- Description of the component Z
71+
72+
entry E;
73+
-- Description of the entry E
74+
75+
end P_Private_Components_Subprograms;
76+
3077
end Protecteds;

0 commit comments

Comments
 (0)