1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2021, 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 pragma Style_Checks
(All_Checks
);
27 -- Turn off subprogram body ordering check. Subprograms are in order
28 -- by RM section rather than alphabetical
33 ---------------------------------------------
34 -- 7.1 Package (also 8.5.3, 10.1.3, 12.3) --
35 ---------------------------------------------
37 -- This routine scans out a package declaration, package body, or a
38 -- renaming declaration or generic instantiation starting with PACKAGE
40 -- PACKAGE_DECLARATION ::=
41 -- PACKAGE_SPECIFICATION;
43 -- PACKAGE_SPECIFICATION ::=
44 -- package DEFINING_PROGRAM_UNIT_NAME
45 -- [ASPECT_SPECIFICATIONS]
47 -- {BASIC_DECLARATIVE_ITEM}
49 -- {BASIC_DECLARATIVE_ITEM}]
50 -- end [[PARENT_UNIT_NAME .] IDENTIFIER]
53 -- package body DEFINING_PROGRAM_UNIT_NAME
54 -- [ASPECT_SPECIFICATIONS]
58 -- HANDLED_SEQUENCE_OF_STATEMENTS]
59 -- end [[PARENT_UNIT_NAME .] IDENTIFIER]
61 -- PACKAGE_RENAMING_DECLARATION ::=
62 -- package DEFINING_IDENTIFIER renames package_NAME
63 -- [ASPECT_SPECIFICATIONS];
65 -- PACKAGE_BODY_STUB ::=
66 -- package body DEFINING_IDENTIFIER is separate
67 -- [ASPECT_SPECIFICATIONS];
69 -- PACKAGE_INSTANTIATION ::=
70 -- package DEFINING_PROGRAM_UNIT_NAME is
71 -- new generic_package_NAME [GENERIC_ACTUAL_PART]
72 -- [ASPECT_SPECIFICATIONS];
74 -- The value in Pf_Flags indicates which of these possible declarations
75 -- is acceptable to the caller:
77 -- Pf_Flags.Spcn Set if specification OK
78 -- Pf_Flags.Decl Set if declaration OK
79 -- Pf_Flags.Gins Set if generic instantiation OK
80 -- Pf_Flags.Pbod Set if proper body OK
81 -- Pf_Flags.Rnam Set if renaming declaration OK
82 -- Pf_Flags.Stub Set if body stub OK
84 -- If an inappropriate form is encountered, it is scanned out but an error
85 -- message indicating that it is appearing in an inappropriate context is
86 -- issued. The only possible settings for Pf_Flags are those defined as
87 -- constants in package Par.
89 -- Note: in all contexts where a package specification is required, there
90 -- is a terminating semicolon. This semicolon is scanned out in the case
91 -- where Pf_Flags is set to Pf_Spcn, even though it is not strictly part
92 -- of the package specification (it's just too much trouble, and really
93 -- quite unnecessary, to deal with scanning out an END where the semicolon
94 -- after the END is not considered to be part of the END.
96 -- The caller has checked that the initial token is PACKAGE
98 -- Error recovery: cannot raise Error_Resync
100 function P_Package
(Pf_Flags
: Pf_Rec
) return Node_Id
is
101 Package_Node
: Node_Id
;
102 Specification_Node
: Node_Id
;
104 Package_Sloc
: Source_Ptr
;
106 Aspect_Sloc
: Source_Ptr
:= No_Location
;
107 -- Save location of WITH for scanned aspects. Left set to No_Location
108 -- if no aspects scanned before the IS keyword.
110 Is_Sloc
: Source_Ptr
;
111 -- Save location of IS token for package declaration
113 Dummy_Node
: constant Node_Id
:=
114 New_Node
(N_Package_Specification
, Token_Ptr
);
115 -- Dummy node to attach aspect specifications to until we properly
116 -- figure out where they eventually belong.
120 Scopes
(Scope
.Last
).Etyp
:= E_Name
;
121 Scopes
(Scope
.Last
).Ecol
:= Start_Column
;
122 Scopes
(Scope
.Last
).Lreq
:= False;
124 Package_Sloc
:= Token_Ptr
;
125 Scan
; -- past PACKAGE
127 if Token
= Tok_Type
then
128 Error_Msg_SC
-- CODEFIX
129 ("TYPE not allowed here");
133 -- Case of package body. Note that we demand a package body if that
134 -- is the only possibility (even if the BODY keyword is not present)
136 if Token
= Tok_Body
or else Pf_Flags
= Pf_Pbod_Pexp
then
137 if not Pf_Flags
.Pbod
then
138 Error_Msg_SC
("package body cannot appear here!");
142 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
143 Name_Node
:= P_Defining_Program_Unit_Name
;
144 Scopes
(Scope
.Last
).Labl
:= Name_Node
;
145 Current_Node
:= Name_Node
;
147 if Aspect_Specifications_Present
then
148 Aspect_Sloc
:= Token_Ptr
;
149 P_Aspect_Specifications
(Dummy_Node
, Semicolon
=> False);
154 if Separate_Present
then
155 if not Pf_Flags
.Stub
then
156 Error_Msg_SC
("body stub cannot appear here!");
159 Scan
; -- past SEPARATE
161 Package_Node
:= New_Node
(N_Package_Body_Stub
, Package_Sloc
);
162 Set_Defining_Identifier
(Package_Node
, Name_Node
);
164 if Has_Aspects
(Dummy_Node
) then
166 ("aspect specifications must come after SEPARATE",
170 P_Aspect_Specifications
(Package_Node
, Semicolon
=> False);
175 Package_Node
:= New_Node
(N_Package_Body
, Package_Sloc
);
176 Set_Defining_Unit_Name
(Package_Node
, Name_Node
);
178 -- Move the aspect specifications to the body node
180 if Has_Aspects
(Dummy_Node
) then
181 Move_Aspects
(From
=> Dummy_Node
, To
=> Package_Node
);
184 Parse_Decls_Begin_End
(Package_Node
);
187 -- Cases other than Package_Body
190 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
191 Name_Node
:= P_Defining_Program_Unit_Name
;
192 Scopes
(Scope
.Last
).Labl
:= Name_Node
;
193 Current_Node
:= Name_Node
;
195 -- Case of renaming declaration
197 Check_Misspelling_Of
(Tok_Renames
);
199 if Token
= Tok_Renames
then
200 if not Pf_Flags
.Rnam
then
201 Error_Msg_SC
("renaming declaration cannot appear here!");
204 Scan
; -- past RENAMES;
207 New_Node
(N_Package_Renaming_Declaration
, Package_Sloc
);
208 Set_Defining_Unit_Name
(Package_Node
, Name_Node
);
209 Set_Name
(Package_Node
, P_Qualified_Simple_Name
);
212 P_Aspect_Specifications
(Package_Node
, Semicolon
=> False);
216 -- Generic package instantiation or package declaration
219 if Aspect_Specifications_Present
then
220 Aspect_Sloc
:= Token_Ptr
;
221 P_Aspect_Specifications
(Dummy_Node
, Semicolon
=> False);
224 Is_Sloc
:= Token_Ptr
;
227 -- Case of generic instantiation
229 if Token
= Tok_New
then
230 if not Pf_Flags
.Gins
then
232 ("generic instantiation cannot appear here!");
235 if Aspect_Sloc
/= No_Location
then
237 ("misplaced aspects for package instantiation",
244 New_Node
(N_Package_Instantiation
, Package_Sloc
);
245 Set_Defining_Unit_Name
(Package_Node
, Name_Node
);
246 Set_Name
(Package_Node
, P_Qualified_Simple_Name
);
247 Set_Generic_Associations
248 (Package_Node
, P_Generic_Actual_Part_Opt
);
250 if Aspect_Sloc
/= No_Location
251 and then not Aspect_Specifications_Present
253 Error_Msg_SC
("info: aspect specifications belong here??");
254 Move_Aspects
(From
=> Dummy_Node
, To
=> Package_Node
);
257 P_Aspect_Specifications
(Package_Node
);
260 -- Case of package declaration or package specification
263 Specification_Node
:=
264 New_Node
(N_Package_Specification
, Package_Sloc
);
266 Set_Defining_Unit_Name
(Specification_Node
, Name_Node
);
267 Set_Visible_Declarations
269 P_Basic_Declarative_Items
(Declare_Expression
=> False));
271 if Token
= Tok_Private
then
272 Error_Msg_Col
:= Scopes
(Scope
.Last
).Ecol
;
274 if RM_Column_Check
then
275 if Token_Is_At_Start_Of_Line
276 and then Start_Column
/= Error_Msg_Col
279 ("(style) PRIVATE in wrong column, should be@");
283 Scan
; -- past PRIVATE
285 Set_Private_Declarations
287 P_Basic_Declarative_Items
(Declare_Expression
=> False));
289 -- Deal gracefully with multiple PRIVATE parts
291 while Token
= Tok_Private
loop
293 ("only one private part allowed per package");
294 Scan
; -- past PRIVATE
296 (P_Basic_Declarative_Items
297 (Declare_Expression
=> False),
298 Private_Declarations
(Specification_Node
));
302 if Pf_Flags
= Pf_Spcn
then
303 Package_Node
:= Specification_Node
;
306 New_Node
(N_Package_Declaration
, Package_Sloc
);
307 Set_Specification
(Package_Node
, Specification_Node
);
310 if Token
= Tok_Begin
then
311 Error_Msg_SC
("begin block not allowed in package spec");
313 Discard_Junk_List
(P_Sequence_Of_Statements
(SS_None
));
316 End_Statements
(Specification_Node
, Empty
, Is_Sloc
);
317 Move_Aspects
(From
=> Dummy_Node
, To
=> Package_Node
);
325 ------------------------------
326 -- 7.1 Package Declaration --
327 ------------------------------
329 -- Parsed by P_Package (7.1)
331 --------------------------------
332 -- 7.1 Package Specification --
333 --------------------------------
335 -- Parsed by P_Package (7.1)
337 -----------------------
338 -- 7.1 Package Body --
339 -----------------------
341 -- Parsed by P_Package (7.1)
343 -----------------------------------
344 -- 7.3 Private Type Declaration --
345 -----------------------------------
347 -- Parsed by P_Type_Declaration (3.2.1)
349 ----------------------------------------
350 -- 7.3 Private Extension Declaration --
351 ----------------------------------------
353 -- Parsed by P_Type_Declaration (3.2.1)