1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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
42 -- [ASPECT_SPECIFICATIONS];
44 -- PACKAGE_SPECIFICATION ::=
45 -- package DEFINING_PROGRAM_UNIT_NAME is
46 -- {BASIC_DECLARATIVE_ITEM}
48 -- {BASIC_DECLARATIVE_ITEM}]
49 -- end [[PARENT_UNIT_NAME .] IDENTIFIER]
52 -- package body DEFINING_PROGRAM_UNIT_NAME is
55 -- HANDLED_SEQUENCE_OF_STATEMENTS]
56 -- end [[PARENT_UNIT_NAME .] IDENTIFIER]
58 -- PACKAGE_RENAMING_DECLARATION ::=
59 -- package DEFINING_IDENTIFIER renames package_NAME;
61 -- PACKAGE_BODY_STUB ::=
62 -- package body DEFINING_IDENTIFIER is separate;
64 -- PACKAGE_INSTANTIATION ::=
65 -- package DEFINING_PROGRAM_UNIT_NAME is
66 -- new generic_package_NAME [GENERIC_ACTUAL_PART]
67 -- [ASPECT_SPECIFICATIONS];
69 -- The value in Pf_Flags indicates which of these possible declarations
70 -- is acceptable to the caller:
72 -- Pf_Flags.Spcn Set if specification OK
73 -- Pf_Flags.Decl Set if declaration OK
74 -- Pf_Flags.Gins Set if generic instantiation OK
75 -- Pf_Flags.Pbod Set if proper body OK
76 -- Pf_Flags.Rnam Set if renaming declaration OK
77 -- Pf_Flags.Stub Set if body stub OK
79 -- If an inappropriate form is encountered, it is scanned out but an error
80 -- message indicating that it is appearing in an inappropriate context is
81 -- issued. The only possible settings for Pf_Flags are those defined as
82 -- constants in package Par.
84 -- Note: in all contexts where a package specification is required, there
85 -- is a terminating semicolon. This semicolon is scanned out in the case
86 -- where Pf_Flags is set to Pf_Spcn, even though it is not strictly part
87 -- of the package specification (it's just too much trouble, and really
88 -- quite unnecessary, to deal with scanning out an END where the semicolon
89 -- after the END is not considered to be part of the END.
91 -- The caller has checked that the initial token is PACKAGE
93 -- Error recovery: cannot raise Error_Resync
97 Decl
: Node_Id
:= Empty
) return Node_Id
99 Package_Node
: Node_Id
;
100 Specification_Node
: Node_Id
;
102 Package_Sloc
: Source_Ptr
;
106 Scope
.Table
(Scope
.Last
).Etyp
:= E_Name
;
107 Scope
.Table
(Scope
.Last
).Ecol
:= Start_Column
;
108 Scope
.Table
(Scope
.Last
).Lreq
:= False;
110 Package_Sloc
:= Token_Ptr
;
111 Scan
; -- past PACKAGE
113 if Token
= Tok_Type
then
114 Error_Msg_SC
-- CODEFIX
115 ("TYPE not allowed here");
119 -- Case of package body. Note that we demand a package body if that
120 -- is the only possibility (even if the BODY keyword is not present)
122 if Token
= Tok_Body
or else Pf_Flags
= Pf_Pbod_Pexp
then
123 if not Pf_Flags
.Pbod
then
124 Error_Msg_SC
("package body cannot appear here!");
128 Name_Node
:= P_Defining_Program_Unit_Name
;
129 Scope
.Table
(Scope
.Last
).Labl
:= Name_Node
;
132 if Separate_Present
then
133 if not Pf_Flags
.Stub
then
134 Error_Msg_SC
("body stub cannot appear here!");
137 Scan
; -- past SEPARATE
141 Package_Node
:= New_Node
(N_Package_Body_Stub
, Package_Sloc
);
142 Set_Defining_Identifier
(Package_Node
, Name_Node
);
145 Package_Node
:= New_Node
(N_Package_Body
, Package_Sloc
);
146 Set_Defining_Unit_Name
(Package_Node
, Name_Node
);
147 Parse_Decls_Begin_End
(Package_Node
);
152 -- Cases other than Package_Body
155 Name_Node
:= P_Defining_Program_Unit_Name
;
156 Scope
.Table
(Scope
.Last
).Labl
:= Name_Node
;
158 -- Case of renaming declaration
160 Check_Misspelling_Of
(Tok_Renames
);
162 if Token
= Tok_Renames
then
163 if not Pf_Flags
.Rnam
then
164 Error_Msg_SC
("renaming declaration cannot appear here!");
167 Scan
; -- past RENAMES;
170 New_Node
(N_Package_Renaming_Declaration
, Package_Sloc
);
171 Set_Defining_Unit_Name
(Package_Node
, Name_Node
);
172 Set_Name
(Package_Node
, P_Qualified_Simple_Name
);
182 -- Case of generic instantiation
184 if Token
= Tok_New
then
185 if not Pf_Flags
.Gins
then
187 ("generic instantiation cannot appear here!");
193 New_Node
(N_Package_Instantiation
, Package_Sloc
);
194 Set_Defining_Unit_Name
(Package_Node
, Name_Node
);
195 Set_Name
(Package_Node
, P_Qualified_Simple_Name
);
196 Set_Generic_Associations
197 (Package_Node
, P_Generic_Actual_Part_Opt
);
198 P_Aspect_Specifications
(Package_Node
);
201 -- Case of package declaration or package specification
204 Specification_Node
:=
205 New_Node
(N_Package_Specification
, Package_Sloc
);
207 Set_Defining_Unit_Name
(Specification_Node
, Name_Node
);
208 Set_Visible_Declarations
209 (Specification_Node
, P_Basic_Declarative_Items
);
211 if Token
= Tok_Private
then
212 Error_Msg_Col
:= Scope
.Table
(Scope
.Last
).Ecol
;
214 if RM_Column_Check
then
215 if Token_Is_At_Start_Of_Line
216 and then Start_Column
/= Error_Msg_Col
219 ("(style) PRIVATE in wrong column, should be@");
223 Scan
; -- past PRIVATE
224 Set_Private_Declarations
225 (Specification_Node
, P_Basic_Declarative_Items
);
227 -- Deal gracefully with multiple PRIVATE parts
229 while Token
= Tok_Private
loop
231 ("only one private part allowed per package");
232 Scan
; -- past PRIVATE
233 Append_List
(P_Basic_Declarative_Items
,
234 Private_Declarations
(Specification_Node
));
238 if Pf_Flags
= Pf_Spcn
then
239 Package_Node
:= Specification_Node
;
242 New_Node
(N_Package_Declaration
, Package_Sloc
);
243 Set_Specification
(Package_Node
, Specification_Node
);
246 if Token
= Tok_Begin
then
247 Error_Msg_SC
("begin block not allowed in package spec");
249 Discard_Junk_List
(P_Sequence_Of_Statements
(SS_None
));
252 if Nkind
(Package_Node
) = N_Package_Declaration
then
253 End_Statements
(Specification_Node
, Package_Node
);
255 End_Statements
(Specification_Node
, Decl
);
264 ------------------------------
265 -- 7.1 Package Declaration --
266 ------------------------------
268 -- Parsed by P_Package (7.1)
270 --------------------------------
271 -- 7.1 Package Specification --
272 --------------------------------
274 -- Parsed by P_Package (7.1)
276 -----------------------
277 -- 7.1 Package Body --
278 -----------------------
280 -- Parsed by P_Package (7.1)
282 -----------------------------------
283 -- 7.3 Private Type Declaration --
284 -----------------------------------
286 -- Parsed by P_Type_Declaration (3.2.1)
288 ----------------------------------------
289 -- 7.3 Private Extension Declaration --
290 ----------------------------------------
292 -- Parsed by P_Type_Declaration (3.2.1)