2013-05-03 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ada / par-ch7.adb
blobd52a13d6c5b2fd10cd2136076ff1899ad3c203a6
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-2013, 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 ------------------------------------------------------------------------------
26 pragma Style_Checks (All_Checks);
27 -- Turn off subprogram body ordering check. Subprograms are in order
28 -- by RM section rather than alphabetical
30 separate (Par)
31 package body Ch7 is
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}
47 -- [private
48 -- {BASIC_DECLARATIVE_ITEM}]
49 -- end [[PARENT_UNIT_NAME .] IDENTIFIER]
51 -- PACKAGE_BODY ::=
52 -- package body DEFINING_PROGRAM_UNIT_NAME is
53 -- DECLARATIVE_PART
54 -- [begin
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
95 function P_Package (Pf_Flags : Pf_Rec) return Node_Id is
96 Package_Node : Node_Id;
97 Specification_Node : Node_Id;
98 Name_Node : Node_Id;
99 Package_Sloc : Source_Ptr;
101 Aspect_Sloc : Source_Ptr := No_Location;
102 -- Save location of WITH for scanned aspects. Left set to No_Location
103 -- if no aspects scanned before the IS keyword.
105 Is_Sloc : Source_Ptr;
106 -- Save location of IS token for package declaration
108 Dummy_Node : constant Node_Id :=
109 New_Node (N_Package_Specification, Token_Ptr);
110 -- Dummy node to attach aspect specifications to until we properly
111 -- figure out where they eventually belong.
113 Body_Is_Hidden_In_SPARK : Boolean;
114 Private_Part_Is_Hidden_In_SPARK : Boolean;
115 Hidden_Region_Start : Source_Ptr;
117 begin
118 Push_Scope_Stack;
119 Scope.Table (Scope.Last).Etyp := E_Name;
120 Scope.Table (Scope.Last).Ecol := Start_Column;
121 Scope.Table (Scope.Last).Lreq := False;
123 Package_Sloc := Token_Ptr;
124 Scan; -- past PACKAGE
126 if Token = Tok_Type then
127 Error_Msg_SC -- CODEFIX
128 ("TYPE not allowed here");
129 Scan; -- past TYPE
130 end if;
132 -- Case of package body. Note that we demand a package body if that
133 -- is the only possibility (even if the BODY keyword is not present)
135 if Token = Tok_Body or else Pf_Flags = Pf_Pbod_Pexp then
136 if not Pf_Flags.Pbod then
137 Error_Msg_SC ("package body cannot appear here!");
138 end if;
140 T_Body;
141 Scope.Table (Scope.Last).Sloc := Token_Ptr;
142 Name_Node := P_Defining_Program_Unit_Name;
143 Scope.Table (Scope.Last).Labl := Name_Node;
144 TF_Is;
146 if Separate_Present then
147 if not Pf_Flags.Stub then
148 Error_Msg_SC ("body stub cannot appear here!");
149 end if;
151 Scan; -- past SEPARATE
152 TF_Semicolon;
153 Pop_Scope_Stack;
155 Package_Node := New_Node (N_Package_Body_Stub, Package_Sloc);
156 Set_Defining_Identifier (Package_Node, Name_Node);
158 else
159 Package_Node := New_Node (N_Package_Body, Package_Sloc);
160 Set_Defining_Unit_Name (Package_Node, Name_Node);
162 -- In SPARK, a HIDE directive can be placed at the beginning of a
163 -- package implementation, thus hiding the package body from SPARK
164 -- tool-set. No violation of the SPARK restriction should be
165 -- issued on nodes in a hidden part, which is obtained by marking
166 -- such hidden parts.
168 if Token = Tok_SPARK_Hide then
169 Body_Is_Hidden_In_SPARK := True;
170 Hidden_Region_Start := Token_Ptr;
171 Scan; -- past HIDE directive
172 else
173 Body_Is_Hidden_In_SPARK := False;
174 end if;
176 Parse_Decls_Begin_End (Package_Node);
178 if Body_Is_Hidden_In_SPARK then
179 Set_Hidden_Part_In_SPARK (Hidden_Region_Start, Token_Ptr);
180 end if;
181 end if;
183 -- Cases other than Package_Body
185 else
186 Scope.Table (Scope.Last).Sloc := Token_Ptr;
187 Name_Node := P_Defining_Program_Unit_Name;
188 Scope.Table (Scope.Last).Labl := Name_Node;
190 -- Case of renaming declaration
192 Check_Misspelling_Of (Tok_Renames);
194 if Token = Tok_Renames then
195 if not Pf_Flags.Rnam then
196 Error_Msg_SC ("renaming declaration cannot appear here!");
197 end if;
199 Scan; -- past RENAMES;
201 Package_Node :=
202 New_Node (N_Package_Renaming_Declaration, Package_Sloc);
203 Set_Defining_Unit_Name (Package_Node, Name_Node);
204 Set_Name (Package_Node, P_Qualified_Simple_Name);
206 No_Constraint;
207 TF_Semicolon;
208 Pop_Scope_Stack;
210 -- Generic package instantiation or package declaration
212 else
213 if Aspect_Specifications_Present then
214 Aspect_Sloc := Token_Ptr;
215 P_Aspect_Specifications (Dummy_Node, Semicolon => False);
216 end if;
218 Is_Sloc := Token_Ptr;
219 TF_Is;
221 -- Case of generic instantiation
223 if Token = Tok_New then
224 if not Pf_Flags.Gins then
225 Error_Msg_SC
226 ("generic instantiation cannot appear here!");
227 end if;
229 if Aspect_Sloc /= No_Location then
230 Error_Msg
231 ("misplaced aspects for package instantiation",
232 Aspect_Sloc);
233 end if;
235 Scan; -- past NEW
237 Package_Node :=
238 New_Node (N_Package_Instantiation, Package_Sloc);
239 Set_Defining_Unit_Name (Package_Node, Name_Node);
240 Set_Name (Package_Node, P_Qualified_Simple_Name);
241 Set_Generic_Associations
242 (Package_Node, P_Generic_Actual_Part_Opt);
244 if Aspect_Sloc /= No_Location
245 and then not Aspect_Specifications_Present
246 then
247 Error_Msg_SC ("\info: aspect specifications belong here");
248 Move_Aspects (From => Dummy_Node, To => Package_Node);
249 end if;
251 P_Aspect_Specifications (Package_Node);
252 Pop_Scope_Stack;
254 -- Case of package declaration or package specification
256 else
257 Specification_Node :=
258 New_Node (N_Package_Specification, Package_Sloc);
260 Set_Defining_Unit_Name (Specification_Node, Name_Node);
261 Set_Visible_Declarations
262 (Specification_Node, P_Basic_Declarative_Items);
264 if Token = Tok_Private then
265 Error_Msg_Col := Scope.Table (Scope.Last).Ecol;
267 if RM_Column_Check then
268 if Token_Is_At_Start_Of_Line
269 and then Start_Column /= Error_Msg_Col
270 then
271 Error_Msg_SC
272 ("(style) PRIVATE in wrong column, should be@");
273 end if;
274 end if;
276 Scan; -- past PRIVATE
278 if Token = Tok_SPARK_Hide then
279 Private_Part_Is_Hidden_In_SPARK := True;
280 Hidden_Region_Start := Token_Ptr;
281 Scan; -- past HIDE directive
282 else
283 Private_Part_Is_Hidden_In_SPARK := False;
284 end if;
286 Set_Private_Declarations
287 (Specification_Node, P_Basic_Declarative_Items);
289 -- In SPARK, a HIDE directive can be placed at the beginning
290 -- of a private part, thus hiding all declarations in the
291 -- private part from SPARK tool-set. No violation of the
292 -- SPARK restriction should be issued on nodes in a hidden
293 -- part, which is obtained by marking such hidden parts.
295 if Private_Part_Is_Hidden_In_SPARK then
296 Set_Hidden_Part_In_SPARK (Hidden_Region_Start, Token_Ptr);
297 end if;
299 -- Deal gracefully with multiple PRIVATE parts
301 while Token = Tok_Private loop
302 Error_Msg_SC
303 ("only one private part allowed per package");
304 Scan; -- past PRIVATE
305 Append_List (P_Basic_Declarative_Items,
306 Private_Declarations (Specification_Node));
307 end loop;
308 end if;
310 if Pf_Flags = Pf_Spcn then
311 Package_Node := Specification_Node;
312 else
313 Package_Node :=
314 New_Node (N_Package_Declaration, Package_Sloc);
315 Set_Specification (Package_Node, Specification_Node);
316 end if;
318 if Token = Tok_Begin then
319 Error_Msg_SC ("begin block not allowed in package spec");
320 Scan; -- past BEGIN
321 Discard_Junk_List (P_Sequence_Of_Statements (SS_None));
322 end if;
324 End_Statements (Specification_Node, Empty, Is_Sloc);
325 Move_Aspects (From => Dummy_Node, To => Package_Node);
326 end if;
327 end if;
328 end if;
330 return Package_Node;
331 end P_Package;
333 ------------------------------
334 -- 7.1 Package Declaration --
335 ------------------------------
337 -- Parsed by P_Package (7.1)
339 --------------------------------
340 -- 7.1 Package Specification --
341 --------------------------------
343 -- Parsed by P_Package (7.1)
345 -----------------------
346 -- 7.1 Package Body --
347 -----------------------
349 -- Parsed by P_Package (7.1)
351 -----------------------------------
352 -- 7.3 Private Type Declaration --
353 -----------------------------------
355 -- Parsed by P_Type_Declaration (3.2.1)
357 ----------------------------------------
358 -- 7.3 Private Extension Declaration --
359 ----------------------------------------
361 -- Parsed by P_Type_Declaration (3.2.1)
363 end Ch7;