| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- P A R . C H 7 -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 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. -- |
| 20 |
-- -- |
| 21 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 22 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 23 |
-- -- |
| 24 |
------------------------------------------------------------------------------ |
| 25 |
|
| 26 |
pragma Style_Checks (All_Checks); |
| 27 |
-- Turn off subprogram body ordering check. Subprograms are in order |
| 28 |
-- by RM section rather than alphabetical |
| 29 |
|
| 30 |
separate (Par) |
| 31 |
package body Ch7 is |
| 32 |
|
| 33 |
--------------------------------------------- |
| 34 |
-- 7.1 Package (also 8.5.3, 10.1.3, 12.3) -- |
| 35 |
--------------------------------------------- |
| 36 |
|
| 37 |
-- This routine scans out a package declaration, package body, or a |
| 38 |
-- renaming declaration or generic instantiation starting with PACKAGE |
| 39 |
|
| 40 |
-- PACKAGE_DECLARATION ::= |
| 41 |
-- PACKAGE_SPECIFICATION; |
| 42 |
|
| 43 |
-- PACKAGE_SPECIFICATION ::= |
| 44 |
-- package DEFINING_PROGRAM_UNIT_NAME |
| 45 |
-- [ASPECT_SPECIFICATIONS] |
| 46 |
-- is |
| 47 |
-- {BASIC_DECLARATIVE_ITEM} |
| 48 |
-- [private |
| 49 |
-- {BASIC_DECLARATIVE_ITEM}] |
| 50 |
-- end [[PARENT_UNIT_NAME .] IDENTIFIER] |
| 51 |
|
| 52 |
-- PACKAGE_BODY ::= |
| 53 |
-- package body DEFINING_PROGRAM_UNIT_NAME |
| 54 |
-- [ASPECT_SPECIFICATIONS] |
| 55 |
-- is |
| 56 |
-- DECLARATIVE_PART |
| 57 |
-- [begin |
| 58 |
-- HANDLED_SEQUENCE_OF_STATEMENTS] |
| 59 |
-- end [[PARENT_UNIT_NAME .] IDENTIFIER] |
| 60 |
|
| 61 |
-- PACKAGE_RENAMING_DECLARATION ::= |
| 62 |
-- package DEFINING_IDENTIFIER renames package_NAME |
| 63 |
-- [ASPECT_SPECIFICATIONS]; |
| 64 |
|
| 65 |
-- PACKAGE_BODY_STUB ::= |
| 66 |
-- package body DEFINING_IDENTIFIER is separate |
| 67 |
-- [ASPECT_SPECIFICATIONS]; |
| 68 |
|
| 69 |
-- PACKAGE_INSTANTIATION ::= |
| 70 |
-- package DEFINING_PROGRAM_UNIT_NAME is |
| 71 |
-- new generic_package_NAME [GENERIC_ACTUAL_PART] |
| 72 |
-- [ASPECT_SPECIFICATIONS]; |
| 73 |
|
| 74 |
-- Note: in all contexts where a package specification is required, there |
| 75 |
-- is a terminating semicolon. This semicolon is scanned out in the case |
| 76 |
-- where Pf_Flags is set to Pf_Spcn, even though it is not strictly part |
| 77 |
-- of the package specification (it's just too much trouble, and really |
| 78 |
-- quite unnecessary, to deal with scanning out an END where the semicolon |
| 79 |
-- after the END is not considered to be part of the END. |
| 80 |
|
| 81 |
-- The caller has checked that the initial token is PACKAGE |
| 82 |
|
| 83 |
-- Error recovery: cannot raise Error_Resync |
| 84 |
|
| 85 |
function P_Package (Pf_Flags : Pf_Rec) return Node_Id is |
| 86 |
Package_Node : Node_Id; |
| 87 |
Specification_Node : Node_Id; |
| 88 |
Name_Node : Node_Id; |
| 89 |
Package_Sloc : Source_Ptr; |
| 90 |
|
| 91 |
Aspect_Sloc : Source_Ptr := No_Location; |
| 92 |
-- Save location of WITH for scanned aspects. Left set to No_Location |
| 93 |
-- if no aspects scanned before the IS keyword. |
| 94 |
|
| 95 |
Is_Sloc : Source_Ptr; |
| 96 |
-- Save location of IS token for package declaration |
| 97 |
|
| 98 |
Dummy_Node : constant Node_Id := |
| 99 |
New_Node (N_Package_Specification, Token_Ptr); |
| 100 |
-- Dummy node to attach aspect specifications to until we properly |
| 101 |
-- figure out where they eventually belong. |
| 102 |
|
| 103 |
begin |
| 104 |
Push_Scope_Stack; |
| 105 |
Scopes (Scope.Last).Etyp := E_Name; |
| 106 |
Scopes (Scope.Last).Ecol := Start_Column; |
| 107 |
Scopes (Scope.Last).Lreq := False; |
| 108 |
|
| 109 |
Package_Sloc := Token_Ptr; |
| 110 |
Scan; -- past PACKAGE |
| 111 |
|
| 112 |
if Token = Tok_Type then |
| 113 |
Error_Msg_SC -- CODEFIX |
| 114 |
("TYPE not allowed here"); |
| 115 |
Scan; -- past TYPE |
| 116 |
end if; |
| 117 |
|
| 118 |
-- Case of package body. Note that we demand a package body if that |
| 119 |
-- is the only possibility (even if the BODY keyword is not present) |
| 120 |
|
| 121 |
if Token = Tok_Body or else Pf_Flags = Pf_Pbod_Pexp then |
| 122 |
if not Pf_Flags.Pbod then |
| 123 |
Error_Msg_SC ("package body cannot appear here!"); |
| 124 |
end if; |
| 125 |
|
| 126 |
T_Body; |
| 127 |
Scopes (Scope.Last).Sloc := Token_Ptr; |
| 128 |
Name_Node := P_Defining_Program_Unit_Name; |
| 129 |
Scopes (Scope.Last).Labl := Name_Node; |
| 130 |
|
| 131 |
if Aspect_Specifications_Present then |
| 132 |
Aspect_Sloc := Token_Ptr; |
| 133 |
P_Aspect_Specifications (Dummy_Node, Semicolon => False); |
| 134 |
end if; |
| 135 |
|
| 136 |
TF_Is; |
| 137 |
|
| 138 |
if Separate_Present then |
| 139 |
if not Pf_Flags.Stub then |
| 140 |
Error_Msg_SC ("body stub cannot appear here!"); |
| 141 |
end if; |
| 142 |
|
| 143 |
Scan; -- past SEPARATE |
| 144 |
|
| 145 |
Package_Node := New_Node (N_Package_Body_Stub, Package_Sloc); |
| 146 |
Set_Defining_Identifier (Package_Node, Name_Node); |
| 147 |
|
| 148 |
if Has_Aspects (Dummy_Node) then |
| 149 |
Error_Msg |
| 150 |
("aspect specifications must come after SEPARATE", |
| 151 |
Aspect_Sloc); |
| 152 |
end if; |
| 153 |
|
| 154 |
P_Aspect_Specifications (Package_Node, Semicolon => False); |
| 155 |
TF_Semicolon; |
| 156 |
Pop_Scope_Stack; |
| 157 |
|
| 158 |
else |
| 159 |
Package_Node := New_Node (N_Package_Body, Package_Sloc); |
| 160 |
Set_Defining_Unit_Name (Package_Node, Name_Node); |
| 161 |
|
| 162 |
-- Move the aspect specifications to the body node |
| 163 |
|
| 164 |
Move_Aspects (From => Dummy_Node, To => Package_Node); |
| 165 |
|
| 166 |
Parse_Decls_Begin_End (Package_Node); |
| 167 |
end if; |
| 168 |
|
| 169 |
-- Cases other than Package_Body |
| 170 |
|
| 171 |
else |
| 172 |
Scopes (Scope.Last).Sloc := Token_Ptr; |
| 173 |
Name_Node := P_Defining_Program_Unit_Name; |
| 174 |
Scopes (Scope.Last).Labl := Name_Node; |
| 175 |
|
| 176 |
-- Case of renaming declaration |
| 177 |
|
| 178 |
Check_Misspelling_Of (Tok_Renames); |
| 179 |
|
| 180 |
if Token = Tok_Renames then |
| 181 |
if not Pf_Flags.Rnam then |
| 182 |
Error_Msg_SC ("renaming declaration cannot appear here!"); |
| 183 |
end if; |
| 184 |
|
| 185 |
Scan; -- past RENAMES; |
| 186 |
|
| 187 |
Package_Node := |
| 188 |
New_Node (N_Package_Renaming_Declaration, Package_Sloc); |
| 189 |
Set_Defining_Unit_Name (Package_Node, Name_Node); |
| 190 |
Set_Name (Package_Node, P_Qualified_Simple_Name); |
| 191 |
|
| 192 |
No_Constraint; |
| 193 |
P_Aspect_Specifications (Package_Node, Semicolon => False); |
| 194 |
TF_Semicolon; |
| 195 |
Pop_Scope_Stack; |
| 196 |
|
| 197 |
-- Generic package instantiation or package declaration |
| 198 |
|
| 199 |
else |
| 200 |
if Aspect_Specifications_Present then |
| 201 |
Aspect_Sloc := Token_Ptr; |
| 202 |
P_Aspect_Specifications (Dummy_Node, Semicolon => False); |
| 203 |
end if; |
| 204 |
|
| 205 |
Is_Sloc := Token_Ptr; |
| 206 |
TF_Is; |
| 207 |
|
| 208 |
-- Case of generic instantiation |
| 209 |
|
| 210 |
if Token = Tok_New then |
| 211 |
if not Pf_Flags.Gins then |
| 212 |
Error_Msg_SC |
| 213 |
("generic instantiation cannot appear here!"); |
| 214 |
end if; |
| 215 |
|
| 216 |
if Aspect_Sloc /= No_Location then |
| 217 |
Error_Msg |
| 218 |
("misplaced aspects for package instantiation", |
| 219 |
Aspect_Sloc); |
| 220 |
end if; |
| 221 |
|
| 222 |
Scan; -- past NEW |
| 223 |
|
| 224 |
Package_Node := |
| 225 |
New_Node (N_Package_Instantiation, Package_Sloc); |
| 226 |
Set_Defining_Unit_Name (Package_Node, Name_Node); |
| 227 |
Set_Name (Package_Node, P_Qualified_Simple_Name); |
| 228 |
Set_Generic_Associations |
| 229 |
(Package_Node, P_Generic_Actual_Part_Opt); |
| 230 |
|
| 231 |
if Aspect_Sloc /= No_Location |
| 232 |
and then not Aspect_Specifications_Present |
| 233 |
then |
| 234 |
Error_Msg_SC ("info: aspect specifications belong here"); |
| 235 |
Move_Aspects (From => Dummy_Node, To => Package_Node); |
| 236 |
end if; |
| 237 |
|
| 238 |
P_Aspect_Specifications (Package_Node, Semicolon => True); |
| 239 |
Pop_Scope_Stack; |
| 240 |
|
| 241 |
-- Case of package declaration or package specification |
| 242 |
|
| 243 |
else |
| 244 |
Specification_Node := |
| 245 |
New_Node (N_Package_Specification, Package_Sloc); |
| 246 |
|
| 247 |
Set_Defining_Unit_Name (Specification_Node, Name_Node); |
| 248 |
Set_Visible_Declarations |
| 249 |
(Specification_Node, |
| 250 |
P_Basic_Declarative_Items (Declare_Expression => False)); |
| 251 |
|
| 252 |
if Token = Tok_Private then |
| 253 |
Error_Msg_Col := Scopes (Scope.Last).Ecol; |
| 254 |
|
| 255 |
if RM_Column_Check then |
| 256 |
if Token_Is_At_Start_Of_Line |
| 257 |
and then Start_Column /= Error_Msg_Col |
| 258 |
then |
| 259 |
Error_Msg_SC |
| 260 |
("(style) PRIVATE in wrong column, should be@?l?"); |
| 261 |
end if; |
| 262 |
end if; |
| 263 |
|
| 264 |
Scan; -- past PRIVATE |
| 265 |
|
| 266 |
Set_Private_Declarations |
| 267 |
(Specification_Node, |
| 268 |
P_Basic_Declarative_Items (Declare_Expression => False)); |
| 269 |
|
| 270 |
-- Deal gracefully with multiple PRIVATE parts |
| 271 |
|
| 272 |
while Token = Tok_Private loop |
| 273 |
Error_Msg_SC |
| 274 |
("only one private part allowed per package"); |
| 275 |
Scan; -- past PRIVATE |
| 276 |
Append_List |
| 277 |
(P_Basic_Declarative_Items |
| 278 |
(Declare_Expression => False), |
| 279 |
Private_Declarations (Specification_Node)); |
| 280 |
end loop; |
| 281 |
end if; |
| 282 |
|
| 283 |
if Pf_Flags = Pf_Spcn then |
| 284 |
Package_Node := Specification_Node; |
| 285 |
else |
| 286 |
Package_Node := |
| 287 |
New_Node (N_Package_Declaration, Package_Sloc); |
| 288 |
Set_Specification (Package_Node, Specification_Node); |
| 289 |
end if; |
| 290 |
|
| 291 |
if Token = Tok_Begin then |
| 292 |
Error_Msg_SC ("begin block not allowed in package spec"); |
| 293 |
Scan; -- past BEGIN |
| 294 |
Discard_Junk_List (P_Sequence_Of_Statements (SS_None)); |
| 295 |
end if; |
| 296 |
|
| 297 |
End_Statements (Specification_Node, Empty, Is_Sloc); |
| 298 |
Move_Aspects (From => Dummy_Node, To => Package_Node); |
| 299 |
end if; |
| 300 |
end if; |
| 301 |
end if; |
| 302 |
|
| 303 |
return Package_Node; |
| 304 |
end P_Package; |
| 305 |
|
| 306 |
------------------------------ |
| 307 |
-- 7.1 Package Declaration -- |
| 308 |
------------------------------ |
| 309 |
|
| 310 |
-- Parsed by P_Package (7.1) |
| 311 |
|
| 312 |
-------------------------------- |
| 313 |
-- 7.1 Package Specification -- |
| 314 |
-------------------------------- |
| 315 |
|
| 316 |
-- Parsed by P_Package (7.1) |
| 317 |
|
| 318 |
----------------------- |
| 319 |
-- 7.1 Package Body -- |
| 320 |
----------------------- |
| 321 |
|
| 322 |
-- Parsed by P_Package (7.1) |
| 323 |
|
| 324 |
----------------------------------- |
| 325 |
-- 7.3 Private Type Declaration -- |
| 326 |
----------------------------------- |
| 327 |
|
| 328 |
-- Parsed by P_Type_Declaration (3.2.1) |
| 329 |
|
| 330 |
---------------------------------------- |
| 331 |
-- 7.3 Private Extension Declaration -- |
| 332 |
---------------------------------------- |
| 333 |
|
| 334 |
-- Parsed by P_Type_Declaration (3.2.1) |
| 335 |
|
| 336 |
end Ch7; |