]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/sem_ch6.adb
sem_ch3.adb (Analyze_Object_Declaration): New function Has_Delayed_Aspect...
[thirdparty/gcc.git] / gcc / ada / sem_ch6.adb
CommitLineData
996ae0b0
RK
1------------------------------------------------------------------------------
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- S E M _ C H 6 --
6-- --
7-- B o d y --
996ae0b0 8-- --
2c6336be 9-- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
996ae0b0
RK
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- --
b5c84c3c 13-- ware Foundation; either version 3, or (at your option) any later ver- --
996ae0b0
RK
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 --
b5c84c3c
RD
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. --
996ae0b0
RK
20-- --
21-- GNAT was originally developed by the GNAT team at New York University. --
71ff80dc 22-- Extensive contributions were provided by Ada Core Technologies Inc. --
996ae0b0
RK
23-- --
24------------------------------------------------------------------------------
25
80e59506 26with Aspects; use Aspects;
996ae0b0
RK
27with Atree; use Atree;
28with Checks; use Checks;
29with Debug; use Debug;
30with Einfo; use Einfo;
31with Elists; use Elists;
32with Errout; use Errout;
33with Expander; use Expander;
ec4867fa 34with Exp_Ch6; use Exp_Ch6;
996ae0b0 35with Exp_Ch7; use Exp_Ch7;
21d27997 36with Exp_Ch9; use Exp_Ch9;
616547fa 37with Exp_Dbug; use Exp_Dbug;
ce2b6ba5 38with Exp_Disp; use Exp_Disp;
e660dbf7 39with Exp_Tss; use Exp_Tss;
ec4867fa 40with Exp_Util; use Exp_Util;
fbf5a39b 41with Fname; use Fname;
996ae0b0 42with Freeze; use Freeze;
8636f52f 43with Ghost; use Ghost;
540d8610 44with Inline; use Inline;
41251c60 45with Itypes; use Itypes;
996ae0b0 46with Lib.Xref; use Lib.Xref;
ec4867fa 47with Layout; use Layout;
996ae0b0
RK
48with Namet; use Namet;
49with Lib; use Lib;
50with Nlists; use Nlists;
51with Nmake; use Nmake;
52with Opt; use Opt;
53with Output; use Output;
b20de9b9
AC
54with Restrict; use Restrict;
55with Rident; use Rident;
996ae0b0
RK
56with Rtsfind; use Rtsfind;
57with Sem; use Sem;
a4100e55 58with Sem_Aux; use Sem_Aux;
996ae0b0
RK
59with Sem_Cat; use Sem_Cat;
60with Sem_Ch3; use Sem_Ch3;
61with Sem_Ch4; use Sem_Ch4;
62with Sem_Ch5; use Sem_Ch5;
63with Sem_Ch8; use Sem_Ch8;
9bc856dd 64with Sem_Ch10; use Sem_Ch10;
996ae0b0 65with Sem_Ch12; use Sem_Ch12;
0f1a6a0b 66with Sem_Ch13; use Sem_Ch13;
dec6faf1 67with Sem_Dim; use Sem_Dim;
996ae0b0
RK
68with Sem_Disp; use Sem_Disp;
69with Sem_Dist; use Sem_Dist;
70with Sem_Elim; use Sem_Elim;
71with Sem_Eval; use Sem_Eval;
72with Sem_Mech; use Sem_Mech;
73with Sem_Prag; use Sem_Prag;
74with Sem_Res; use Sem_Res;
75with Sem_Util; use Sem_Util;
76with Sem_Type; use Sem_Type;
77with Sem_Warn; use Sem_Warn;
78with Sinput; use Sinput;
79with Stand; use Stand;
80with Sinfo; use Sinfo;
81with Sinfo.CN; use Sinfo.CN;
82with Snames; use Snames;
83with Stringt; use Stringt;
84with Style;
85with Stylesw; use Stylesw;
8417f4b2 86with Targparm; use Targparm;
996ae0b0
RK
87with Tbuild; use Tbuild;
88with Uintp; use Uintp;
89with Urealp; use Urealp;
90with Validsw; use Validsw;
91
92package body Sem_Ch6 is
93
c8ef728f 94 May_Hide_Profile : Boolean := False;
ec4867fa
ES
95 -- This flag is used to indicate that two formals in two subprograms being
96 -- checked for conformance differ only in that one is an access parameter
97 -- while the other is of a general access type with the same designated
98 -- type. In this case, if the rest of the signatures match, a call to
99 -- either subprogram may be ambiguous, which is worth a warning. The flag
100 -- is set in Compatible_Types, and the warning emitted in
101 -- New_Overloaded_Entity.
c8ef728f 102
996ae0b0
RK
103 -----------------------
104 -- Local Subprograms --
105 -----------------------
106
c9d70ab1
AC
107 procedure Analyze_Function_Return (N : Node_Id);
108 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
109 -- applies to a [generic] function.
110
111 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
112 -- Analyze a generic subprogram body. N is the body to be analyzed, and
113 -- Gen_Id is the defining entity Id for the corresponding spec.
114
4d8f3296
ES
115 procedure Analyze_Null_Procedure
116 (N : Node_Id;
117 Is_Completion : out Boolean);
9d2a2071 118 -- A null procedure can be a declaration or (Ada 2012) a completion
4d8f3296 119
5d37ba92 120 procedure Analyze_Return_Statement (N : Node_Id);
5b9c3fc4 121 -- Common processing for simple and extended return statements
ec4867fa 122
82c80734
RD
123 procedure Analyze_Return_Type (N : Node_Id);
124 -- Subsidiary to Process_Formals: analyze subtype mark in function
5b9c3fc4 125 -- specification in a context where the formals are visible and hide
82c80734
RD
126 -- outer homographs.
127
b1b543d2 128 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
13d923cc
RD
129 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
130 -- that we can use RETURN but not skip the debug output at the end.
b1b543d2 131
806f6d37
AC
132 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
133 -- Returns true if Subp can override a predefined operator.
134
996ae0b0 135 procedure Check_Conformance
41251c60
JM
136 (New_Id : Entity_Id;
137 Old_Id : Entity_Id;
138 Ctype : Conformance_Type;
139 Errmsg : Boolean;
140 Conforms : out Boolean;
141 Err_Loc : Node_Id := Empty;
142 Get_Inst : Boolean := False;
143 Skip_Controlling_Formals : Boolean := False);
996ae0b0
RK
144 -- Given two entities, this procedure checks that the profiles associated
145 -- with these entities meet the conformance criterion given by the third
146 -- parameter. If they conform, Conforms is set True and control returns
147 -- to the caller. If they do not conform, Conforms is set to False, and
148 -- in addition, if Errmsg is True on the call, proper messages are output
149 -- to complain about the conformance failure. If Err_Loc is non_Empty
150 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
151 -- error messages are placed on the appropriate part of the construct
152 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
153 -- against a formal access-to-subprogram type so Get_Instance_Of must
154 -- be called.
155
156 procedure Check_Subprogram_Order (N : Node_Id);
157 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
158 -- the alpha ordering rule for N if this ordering requirement applicable.
159
996ae0b0
RK
160 procedure Check_Returns
161 (HSS : Node_Id;
162 Mode : Character;
c8ef728f
ES
163 Err : out Boolean;
164 Proc : Entity_Id := Empty);
165 -- Called to check for missing return statements in a function body, or for
0a36105d 166 -- returns present in a procedure body which has No_Return set. HSS is the
c8ef728f
ES
167 -- handled statement sequence for the subprogram body. This procedure
168 -- checks all flow paths to make sure they either have return (Mode = 'F',
169 -- used for functions) or do not have a return (Mode = 'P', used for
170 -- No_Return procedures). The flag Err is set if there are any control
171 -- paths not explicitly terminated by a return in the function case, and is
172 -- True otherwise. Proc is the entity for the procedure case and is used
173 -- in posting the warning message.
996ae0b0 174
e5a58fac
AC
175 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
176 -- In Ada 2012, a primitive equality operator on an untagged record type
177 -- must appear before the type is frozen, and have the same visibility as
178 -- that of the type. This procedure checks that this rule is met, and
179 -- otherwise emits an error on the subprogram declaration and a warning
b2834fbd
AC
180 -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode,
181 -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier
182 -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility
183 -- is set, otherwise the call has no effect.
e5a58fac 184
996ae0b0 185 procedure Enter_Overloaded_Entity (S : Entity_Id);
82c80734
RD
186 -- This procedure makes S, a new overloaded entity, into the first visible
187 -- entity with that name.
996ae0b0 188
a5b62485
AC
189 function Is_Non_Overriding_Operation
190 (Prev_E : Entity_Id;
191 New_E : Entity_Id) return Boolean;
192 -- Enforce the rule given in 12.3(18): a private operation in an instance
193 -- overrides an inherited operation only if the corresponding operation
260359e3
AC
194 -- was overriding in the generic. This needs to be checked for primitive
195 -- operations of types derived (in the generic unit) from formal private
196 -- or formal derived types.
a5b62485 197
996ae0b0
RK
198 procedure Make_Inequality_Operator (S : Entity_Id);
199 -- Create the declaration for an inequality operator that is implicitly
200 -- created by a user-defined equality operator that yields a boolean.
201
996ae0b0
RK
202 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
203 -- Formal_Id is an formal parameter entity. This procedure deals with
e358346d
AC
204 -- setting the proper validity status for this entity, which depends on
205 -- the kind of parameter and the validity checking mode.
996ae0b0
RK
206
207 ---------------------------------------------
208 -- Analyze_Abstract_Subprogram_Declaration --
209 ---------------------------------------------
210
211 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
fbf5a39b
AC
212 Designator : constant Entity_Id :=
213 Analyze_Subprogram_Specification (Specification (N));
996ae0b0
RK
214 Scop : constant Entity_Id := Current_Scope;
215
216 begin
8636f52f
HK
217 -- The abstract subprogram declaration may be subject to pragma Ghost
218 -- with policy Ignore. Set the mode now to ensure that any nodes
219 -- generated during analysis and expansion are properly flagged as
220 -- ignored Ghost.
221
222 Set_Ghost_Mode (N);
ce5ba43a 223 Check_SPARK_05_Restriction ("abstract subprogram is not allowed", N);
38171f43 224
996ae0b0 225 Generate_Definition (Designator);
c9d70ab1 226
f937473f 227 Set_Is_Abstract_Subprogram (Designator);
996ae0b0
RK
228 New_Overloaded_Entity (Designator);
229 Check_Delayed_Subprogram (Designator);
230
fbf5a39b 231 Set_Categorization_From_Scope (Designator, Scop);
996ae0b0 232
8636f52f 233 -- An abstract subprogram declared within a Ghost region is rendered
c5cec2fe
AC
234 -- Ghost (SPARK RM 6.9(2)).
235
8636f52f 236 if Comes_From_Source (Designator) and then Ghost_Mode > None then
c5cec2fe
AC
237 Set_Is_Ghost_Entity (Designator);
238 end if;
239
996ae0b0
RK
240 if Ekind (Scope (Designator)) = E_Protected_Type then
241 Error_Msg_N
242 ("abstract subprogram not allowed in protected type", N);
5d37ba92
ES
243
244 -- Issue a warning if the abstract subprogram is neither a dispatching
245 -- operation nor an operation that overrides an inherited subprogram or
246 -- predefined operator, since this most likely indicates a mistake.
247
248 elsif Warn_On_Redundant_Constructs
249 and then not Is_Dispatching_Operation (Designator)
038140ed 250 and then not Present (Overridden_Operation (Designator))
5d37ba92
ES
251 and then (not Is_Operator_Symbol_Name (Chars (Designator))
252 or else Scop /= Scope (Etype (First_Formal (Designator))))
253 then
254 Error_Msg_N
dbfeb4fa 255 ("abstract subprogram is not dispatching or overriding?r?", N);
996ae0b0 256 end if;
fbf5a39b
AC
257
258 Generate_Reference_To_Formals (Designator);
361effb1 259 Check_Eliminated (Designator);
eaba57fb
RD
260
261 if Has_Aspects (N) then
262 Analyze_Aspect_Specifications (N, Designator);
263 end if;
996ae0b0
RK
264 end Analyze_Abstract_Subprogram_Declaration;
265
b0186f71
AC
266 ---------------------------------
267 -- Analyze_Expression_Function --
268 ---------------------------------
269
270 procedure Analyze_Expression_Function (N : Node_Id) is
271 Loc : constant Source_Ptr := Sloc (N);
272 LocX : constant Source_Ptr := Sloc (Expression (N));
0b5b2bbc 273 Expr : constant Node_Id := Expression (N);
d2d4b355
AC
274 Spec : constant Node_Id := Specification (N);
275
8a06151a 276 Def_Id : Entity_Id;
b0186f71 277
8a06151a 278 Prev : Entity_Id;
b0186f71 279 -- If the expression is a completion, Prev is the entity whose
d2d4b355
AC
280 -- declaration is completed. Def_Id is needed to analyze the spec.
281
282 New_Body : Node_Id;
d2d4b355 283 New_Spec : Node_Id;
b913199e 284 Ret : Node_Id;
b0186f71
AC
285
286 begin
287 -- This is one of the occasions on which we transform the tree during
afc8324d 288 -- semantic analysis. If this is a completion, transform the expression
d2b10647
ES
289 -- function into an equivalent subprogram body, and analyze it.
290
291 -- Expression functions are inlined unconditionally. The back-end will
292 -- determine whether this is possible.
293
294 Inline_Processing_Required := True;
b727a82b
AC
295
296 -- Create a specification for the generated body. Types and defauts in
297 -- the profile are copies of the spec, but new entities must be created
298 -- for the unit name and the formals.
299
300 New_Spec := New_Copy_Tree (Spec);
301 Set_Defining_Unit_Name (New_Spec,
302 Make_Defining_Identifier (Sloc (Defining_Unit_Name (Spec)),
303 Chars (Defining_Unit_Name (Spec))));
304
305 if Present (Parameter_Specifications (New_Spec)) then
306 declare
307 Formal_Spec : Node_Id;
a6abfd78
AC
308 Def : Entity_Id;
309
b727a82b
AC
310 begin
311 Formal_Spec := First (Parameter_Specifications (New_Spec));
b2afe274
AC
312
313 -- Create a new formal parameter at the same source position
314
b727a82b 315 while Present (Formal_Spec) loop
a6abfd78
AC
316 Def := Defining_Identifier (Formal_Spec);
317 Set_Defining_Identifier (Formal_Spec,
318 Make_Defining_Identifier (Sloc (Def),
319 Chars => Chars (Def)));
b727a82b
AC
320 Next (Formal_Spec);
321 end loop;
322 end;
323 end if;
324
51597c23 325 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
d2d4b355
AC
326
327 -- If there are previous overloadable entities with the same name,
328 -- check whether any of them is completed by the expression function.
b04d926e 329 -- In a generic context a formal subprogram has no completion.
d2d4b355 330
4b6f99f5
RD
331 if Present (Prev)
332 and then Is_Overloadable (Prev)
b04d926e
AC
333 and then not Is_Formal_Subprogram (Prev)
334 then
51597c23
AC
335 Def_Id := Analyze_Subprogram_Specification (Spec);
336 Prev := Find_Corresponding_Spec (N);
35e7063a
AC
337
338 -- The previous entity may be an expression function as well, in
339 -- which case the redeclaration is illegal.
340
341 if Present (Prev)
5073ad7a
AC
342 and then Nkind (Original_Node (Unit_Declaration_Node (Prev))) =
343 N_Expression_Function
35e7063a 344 then
bc5e261c
ES
345 Error_Msg_Sloc := Sloc (Prev);
346 Error_Msg_N ("& conflicts with declaration#", Def_Id);
35e7063a
AC
347 return;
348 end if;
d2d4b355 349 end if;
b0186f71 350
b913199e
AC
351 Ret := Make_Simple_Return_Statement (LocX, Expression (N));
352
b0186f71
AC
353 New_Body :=
354 Make_Subprogram_Body (Loc,
d2d4b355 355 Specification => New_Spec,
b0186f71
AC
356 Declarations => Empty_List,
357 Handled_Statement_Sequence =>
358 Make_Handled_Sequence_Of_Statements (LocX,
b913199e 359 Statements => New_List (Ret)));
b0186f71 360
e7f23f06
AC
361 -- If the expression completes a generic subprogram, we must create a
362 -- separate node for the body, because at instantiation the original
363 -- node of the generic copy must be a generic subprogram body, and
364 -- cannot be a expression function. Otherwise we just rewrite the
365 -- expression with the non-generic body.
366
6d7e5c54 367 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
e7f23f06 368 Insert_After (N, New_Body);
6d7e5c54 369
e7f23f06
AC
370 -- Propagate any aspects or pragmas that apply to the expression
371 -- function to the proper body when the expression function acts
372 -- as a completion.
373
374 if Has_Aspects (N) then
375 Move_Aspects (N, To => New_Body);
376 end if;
377
378 Relocate_Pragmas_To_Body (New_Body);
b0186f71 379
b0186f71 380 Rewrite (N, Make_Null_Statement (Loc));
d2d4b355 381 Set_Has_Completion (Prev, False);
b0186f71
AC
382 Analyze (N);
383 Analyze (New_Body);
d2b10647 384 Set_Is_Inlined (Prev);
b0186f71 385
e5c4e2bc
AC
386 -- If the expression function is a completion, the previous declaration
387 -- must come from source. We know already that appears in the current
388 -- scope. The entity itself may be internally created if within a body
389 -- to be inlined.
390
4b6f99f5
RD
391 elsif Present (Prev)
392 and then Comes_From_Source (Parent (Prev))
b04d926e
AC
393 and then not Is_Formal_Subprogram (Prev)
394 then
d2d4b355 395 Set_Has_Completion (Prev, False);
76264f60 396
c6d2191a
AC
397 -- An expression function that is a completion freezes the
398 -- expression. This means freezing the return type, and if it is
399 -- an access type, freezing its designated type as well.
1b31321b 400
c6d2191a 401 -- Note that we cannot defer this freezing to the analysis of the
1b31321b
AC
402 -- expression itself, because a freeze node might appear in a nested
403 -- scope, leading to an elaboration order issue in gigi.
c6d2191a
AC
404
405 Freeze_Before (N, Etype (Prev));
1b31321b 406
c6d2191a
AC
407 if Is_Access_Type (Etype (Prev)) then
408 Freeze_Before (N, Designated_Type (Etype (Prev)));
409 end if;
410
76264f60
AC
411 -- For navigation purposes, indicate that the function is a body
412
413 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
b0186f71 414 Rewrite (N, New_Body);
e7f23f06 415
c0cdbd39
AC
416 -- Correct the parent pointer of the aspect specification list to
417 -- reference the rewritten node.
418
419 if Has_Aspects (N) then
420 Set_Parent (Aspect_Specifications (N), N);
421 end if;
422
e7f23f06
AC
423 -- Propagate any pragmas that apply to the expression function to the
424 -- proper body when the expression function acts as a completion.
425 -- Aspects are automatically transfered because of node rewriting.
426
427 Relocate_Pragmas_To_Body (N);
d2b10647
ES
428 Analyze (N);
429
6d7e5c54
AC
430 -- Prev is the previous entity with the same name, but it is can
431 -- be an unrelated spec that is not completed by the expression
432 -- function. In that case the relevant entity is the one in the body.
433 -- Not clear that the backend can inline it in this case ???
434
435 if Has_Completion (Prev) then
436 Set_Is_Inlined (Prev);
31af8899
AC
437
438 -- The formals of the expression function are body formals,
439 -- and do not appear in the ali file, which will only contain
440 -- references to the formals of the original subprogram spec.
441
442 declare
443 F1 : Entity_Id;
444 F2 : Entity_Id;
445
446 begin
447 F1 := First_Formal (Def_Id);
448 F2 := First_Formal (Prev);
449
450 while Present (F1) loop
451 Set_Spec_Entity (F1, F2);
452 Next_Formal (F1);
453 Next_Formal (F2);
454 end loop;
455 end;
456
6d7e5c54
AC
457 else
458 Set_Is_Inlined (Defining_Entity (New_Body));
459 end if;
460
0b5b2bbc 461 -- If this is not a completion, create both a declaration and a body, so
6d7e5c54 462 -- that the expression can be inlined whenever possible.
d2b10647
ES
463
464 else
a52e6d7e
AC
465 -- An expression function that is not a completion is not a
466 -- subprogram declaration, and thus cannot appear in a protected
467 -- definition.
468
469 if Nkind (Parent (N)) = N_Protected_Definition then
470 Error_Msg_N
471 ("an expression function is not a legal protected operation", N);
472 end if;
473
b8e6830b 474 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
c0cdbd39
AC
475
476 -- Correct the parent pointer of the aspect specification list to
477 -- reference the rewritten node.
478
479 if Has_Aspects (N) then
480 Set_Parent (Aspect_Specifications (N), N);
481 end if;
482
b0186f71 483 Analyze (N);
b04d926e 484
e699b76e
AC
485 -- Within a generic pre-analyze the original expression for name
486 -- capture. The body is also generated but plays no role in
487 -- this because it is not part of the original source.
b04d926e
AC
488
489 if Inside_A_Generic then
490 declare
491 Id : constant Entity_Id := Defining_Entity (N);
b04d926e
AC
492
493 begin
494 Set_Has_Completion (Id);
b04d926e
AC
495 Push_Scope (Id);
496 Install_Formals (Id);
e699b76e 497 Preanalyze_Spec_Expression (Expr, Etype (Id));
b04d926e 498 End_Scope;
b04d926e
AC
499 end;
500 end if;
501
b8e6830b
AC
502 Set_Is_Inlined (Defining_Entity (N));
503
504 -- Establish the linkages between the spec and the body. These are
505 -- used when the expression function acts as the prefix of attribute
506 -- 'Access in order to freeze the original expression which has been
507 -- moved to the generated body.
508
509 Set_Corresponding_Body (N, Defining_Entity (New_Body));
510 Set_Corresponding_Spec (New_Body, Defining_Entity (N));
d2b10647 511
6d7e5c54
AC
512 -- To prevent premature freeze action, insert the new body at the end
513 -- of the current declarations, or at the end of the package spec.
b913199e 514 -- However, resolve usage names now, to prevent spurious visibility
ad4e3362
ES
515 -- on later entities. Note that the function can now be called in
516 -- the current declarative part, which will appear to be prior to
517 -- the presence of the body in the code. There are nevertheless no
518 -- order of elaboration issues because all name resolution has taken
519 -- place at the point of declaration.
6d7e5c54
AC
520
521 declare
e876c43a
AC
522 Decls : List_Id := List_Containing (N);
523 Par : constant Node_Id := Parent (Decls);
b8e6830b 524 Id : constant Entity_Id := Defining_Entity (N);
6d7e5c54
AC
525
526 begin
fce54763
AC
527 -- If this is a wrapper created for in an instance for a formal
528 -- subprogram, insert body after declaration, to be analyzed when
529 -- the enclosing instance is analyzed.
530
531 if GNATprove_Mode
532 and then Is_Generic_Actual_Subprogram (Defining_Entity (N))
6d7e5c54 533 then
fce54763
AC
534 Insert_After (N, New_Body);
535
536 else
537 if Nkind (Par) = N_Package_Specification
538 and then Decls = Visible_Declarations (Par)
539 and then Present (Private_Declarations (Par))
540 and then not Is_Empty_List (Private_Declarations (Par))
541 then
542 Decls := Private_Declarations (Par);
543 end if;
6d7e5c54 544
fce54763
AC
545 Insert_After (Last (Decls), New_Body);
546 Push_Scope (Id);
547 Install_Formals (Id);
3a8e3f63 548
fce54763
AC
549 -- Preanalyze the expression for name capture, except in an
550 -- instance, where this has been done during generic analysis,
551 -- and will be redone when analyzing the body.
845f06e2 552
fce54763
AC
553 declare
554 Expr : constant Node_Id := Expression (Ret);
4058ddcc 555
fce54763
AC
556 begin
557 Set_Parent (Expr, Ret);
4058ddcc 558
fce54763
AC
559 if not In_Instance then
560 Preanalyze_Spec_Expression (Expr, Etype (Id));
561 end if;
562 end;
3a8e3f63 563
fce54763
AC
564 End_Scope;
565 end if;
6d7e5c54 566 end;
b0186f71 567 end if;
0b5b2bbc
AC
568
569 -- If the return expression is a static constant, we suppress warning
570 -- messages on unused formals, which in most cases will be noise.
571
572 Set_Is_Trivial_Subprogram (Defining_Entity (New_Body),
573 Is_OK_Static_Expression (Expr));
b0186f71
AC
574 end Analyze_Expression_Function;
575
ec4867fa
ES
576 ----------------------------------------
577 -- Analyze_Extended_Return_Statement --
578 ----------------------------------------
579
580 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
581 begin
c86cf714 582 Check_Compiler_Unit ("extended return statement", N);
5d37ba92 583 Analyze_Return_Statement (N);
ec4867fa
ES
584 end Analyze_Extended_Return_Statement;
585
996ae0b0
RK
586 ----------------------------
587 -- Analyze_Function_Call --
588 ----------------------------
589
590 procedure Analyze_Function_Call (N : Node_Id) is
a7e68e7f
HK
591 Actuals : constant List_Id := Parameter_Associations (N);
592 Func_Nam : constant Node_Id := Name (N);
593 Actual : Node_Id;
594
996ae0b0 595 begin
a7e68e7f 596 Analyze (Func_Nam);
996ae0b0 597
3e7302c3
AC
598 -- A call of the form A.B (X) may be an Ada 2005 call, which is
599 -- rewritten as B (A, X). If the rewriting is successful, the call
600 -- has been analyzed and we just return.
82c80734 601
a7e68e7f
HK
602 if Nkind (Func_Nam) = N_Selected_Component
603 and then Name (N) /= Func_Nam
82c80734
RD
604 and then Is_Rewrite_Substitution (N)
605 and then Present (Etype (N))
606 then
607 return;
608 end if;
609
996ae0b0
RK
610 -- If error analyzing name, then set Any_Type as result type and return
611
a7e68e7f 612 if Etype (Func_Nam) = Any_Type then
996ae0b0
RK
613 Set_Etype (N, Any_Type);
614 return;
615 end if;
616
617 -- Otherwise analyze the parameters
618
e24329cd
YM
619 if Present (Actuals) then
620 Actual := First (Actuals);
996ae0b0
RK
621 while Present (Actual) loop
622 Analyze (Actual);
623 Check_Parameterless_Call (Actual);
624 Next (Actual);
625 end loop;
626 end if;
627
628 Analyze_Call (N);
996ae0b0
RK
629 end Analyze_Function_Call;
630
ec4867fa
ES
631 -----------------------------
632 -- Analyze_Function_Return --
633 -----------------------------
634
635 procedure Analyze_Function_Return (N : Node_Id) is
a7e68e7f
HK
636 Loc : constant Source_Ptr := Sloc (N);
637 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
638 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
ec4867fa 639
5d37ba92 640 R_Type : constant Entity_Id := Etype (Scope_Id);
ec4867fa
ES
641 -- Function result subtype
642
643 procedure Check_Limited_Return (Expr : Node_Id);
644 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
645 -- limited types. Used only for simple return statements.
646 -- Expr is the expression returned.
647
648 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
649 -- Check that the return_subtype_indication properly matches the result
650 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
651
652 --------------------------
653 -- Check_Limited_Return --
654 --------------------------
655
656 procedure Check_Limited_Return (Expr : Node_Id) is
657 begin
658 -- Ada 2005 (AI-318-02): Return-by-reference types have been
659 -- removed and replaced by anonymous access results. This is an
660 -- incompatibility with Ada 95. Not clear whether this should be
661 -- enforced yet or perhaps controllable with special switch. ???
662
ce72a9a3
AC
663 -- A limited interface that is not immutably limited is OK.
664
665 if Is_Limited_Interface (R_Type)
666 and then
667 not (Is_Task_Interface (R_Type)
668 or else Is_Protected_Interface (R_Type)
669 or else Is_Synchronized_Interface (R_Type))
670 then
671 null;
672
673 elsif Is_Limited_Type (R_Type)
674 and then not Is_Interface (R_Type)
ec4867fa
ES
675 and then Comes_From_Source (N)
676 and then not In_Instance_Body
2a31c32b 677 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
ec4867fa
ES
678 then
679 -- Error in Ada 2005
680
0791fbe9 681 if Ada_Version >= Ada_2005
ec4867fa
ES
682 and then not Debug_Flag_Dot_L
683 and then not GNAT_Mode
684 then
685 Error_Msg_N
3ccedacc
AC
686 ("(Ada 2005) cannot copy object of a limited type "
687 & "(RM-2005 6.5(5.5/2))", Expr);
e0ae93e2 688
51245e2d 689 if Is_Limited_View (R_Type) then
ec4867fa
ES
690 Error_Msg_N
691 ("\return by reference not permitted in Ada 2005", Expr);
692 end if;
693
694 -- Warn in Ada 95 mode, to give folks a heads up about this
695 -- incompatibility.
696
697 -- In GNAT mode, this is just a warning, to allow it to be
698 -- evilly turned off. Otherwise it is a real error.
699
9694c039
AC
700 -- In a generic context, simplify the warning because it makes
701 -- no sense to discuss pass-by-reference or copy.
702
ec4867fa 703 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
9694c039
AC
704 if Inside_A_Generic then
705 Error_Msg_N
885c4871 706 ("return of limited object not permitted in Ada 2005 "
dbfeb4fa 707 & "(RM-2005 6.5(5.5/2))?y?", Expr);
9694c039 708
51245e2d 709 elsif Is_Limited_View (R_Type) then
ec4867fa 710 Error_Msg_N
20261dc1 711 ("return by reference not permitted in Ada 2005 "
dbfeb4fa 712 & "(RM-2005 6.5(5.5/2))?y?", Expr);
ec4867fa
ES
713 else
714 Error_Msg_N
20261dc1 715 ("cannot copy object of a limited type in Ada 2005 "
dbfeb4fa 716 & "(RM-2005 6.5(5.5/2))?y?", Expr);
ec4867fa
ES
717 end if;
718
719 -- Ada 95 mode, compatibility warnings disabled
720
721 else
722 return; -- skip continuation messages below
723 end if;
724
9694c039
AC
725 if not Inside_A_Generic then
726 Error_Msg_N
727 ("\consider switching to return of access type", Expr);
728 Explain_Limited_Type (R_Type, Expr);
729 end if;
ec4867fa
ES
730 end if;
731 end Check_Limited_Return;
732
733 -------------------------------------
734 -- Check_Return_Subtype_Indication --
735 -------------------------------------
736
737 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
7665e4bd
AC
738 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
739
740 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
741 -- Subtype given in the extended return statement (must match R_Type)
ec4867fa
ES
742
743 Subtype_Ind : constant Node_Id :=
744 Object_Definition (Original_Node (Obj_Decl));
745
7f568bfa
AC
746 R_Type_Is_Anon_Access : constant Boolean :=
747 Ekind_In (R_Type,
748 E_Anonymous_Access_Subprogram_Type,
749 E_Anonymous_Access_Protected_Subprogram_Type,
750 E_Anonymous_Access_Type);
ec4867fa
ES
751 -- True if return type of the function is an anonymous access type
752 -- Can't we make Is_Anonymous_Access_Type in einfo ???
753
7f568bfa
AC
754 R_Stm_Type_Is_Anon_Access : constant Boolean :=
755 Ekind_In (R_Stm_Type,
756 E_Anonymous_Access_Subprogram_Type,
757 E_Anonymous_Access_Protected_Subprogram_Type,
758 E_Anonymous_Access_Type);
ec4867fa
ES
759 -- True if type of the return object is an anonymous access type
760
7f568bfa
AC
761 procedure Error_No_Match (N : Node_Id);
762 -- Output error messages for case where types do not statically
763 -- match. N is the location for the messages.
764
765 --------------------
766 -- Error_No_Match --
767 --------------------
768
769 procedure Error_No_Match (N : Node_Id) is
770 begin
771 Error_Msg_N
772 ("subtype must statically match function result subtype", N);
773
774 if not Predicates_Match (R_Stm_Type, R_Type) then
775 Error_Msg_Node_2 := R_Type;
776 Error_Msg_NE
3ccedacc 777 ("\predicate of& does not match predicate of&",
7f568bfa
AC
778 N, R_Stm_Type);
779 end if;
780 end Error_No_Match;
781
782 -- Start of processing for Check_Return_Subtype_Indication
783
ec4867fa 784 begin
7665e4bd 785 -- First, avoid cascaded errors
ec4867fa
ES
786
787 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
788 return;
789 end if;
790
791 -- "return access T" case; check that the return statement also has
792 -- "access T", and that the subtypes statically match:
53cf4600 793 -- if this is an access to subprogram the signatures must match.
ec4867fa
ES
794
795 if R_Type_Is_Anon_Access then
796 if R_Stm_Type_Is_Anon_Access then
53cf4600
ES
797 if
798 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
0a36105d 799 then
53cf4600
ES
800 if Base_Type (Designated_Type (R_Stm_Type)) /=
801 Base_Type (Designated_Type (R_Type))
802 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
803 then
7f568bfa 804 Error_No_Match (Subtype_Mark (Subtype_Ind));
53cf4600
ES
805 end if;
806
807 else
808 -- For two anonymous access to subprogram types, the
809 -- types themselves must be type conformant.
810
811 if not Conforming_Types
812 (R_Stm_Type, R_Type, Fully_Conformant)
813 then
7f568bfa 814 Error_No_Match (Subtype_Ind);
53cf4600 815 end if;
ec4867fa 816 end if;
0a36105d 817
ec4867fa
ES
818 else
819 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
820 end if;
821
6cce2156
GD
822 -- If the return object is of an anonymous access type, then report
823 -- an error if the function's result type is not also anonymous.
824
825 elsif R_Stm_Type_Is_Anon_Access
826 and then not R_Type_Is_Anon_Access
827 then
3ccedacc
AC
828 Error_Msg_N ("anonymous access not allowed for function with "
829 & "named access result", Subtype_Ind);
6cce2156 830
81d93365
AC
831 -- Subtype indication case: check that the return object's type is
832 -- covered by the result type, and that the subtypes statically match
833 -- when the result subtype is constrained. Also handle record types
834 -- with unknown discriminants for which we have built the underlying
835 -- record view. Coverage is needed to allow specific-type return
836 -- objects when the result type is class-wide (see AI05-32).
837
838 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
9013065b 839 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
212863c0
AC
840 and then
841 Covers
842 (Base_Type (R_Type),
843 Underlying_Record_View (Base_Type (R_Stm_Type))))
9013065b
AC
844 then
845 -- A null exclusion may be present on the return type, on the
846 -- function specification, on the object declaration or on the
847 -- subtype itself.
ec4867fa 848
21d27997
RD
849 if Is_Access_Type (R_Type)
850 and then
851 (Can_Never_Be_Null (R_Type)
852 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
853 Can_Never_Be_Null (R_Stm_Type)
854 then
7f568bfa 855 Error_No_Match (Subtype_Ind);
21d27997
RD
856 end if;
857
105b5e65 858 -- AI05-103: for elementary types, subtypes must statically match
8779dffa
AC
859
860 if Is_Constrained (R_Type)
861 or else Is_Access_Type (R_Type)
862 then
ec4867fa 863 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
7f568bfa 864 Error_No_Match (Subtype_Ind);
ec4867fa
ES
865 end if;
866 end if;
867
a8b346d2
RD
868 -- All remaining cases are illegal
869
870 -- Note: previous versions of this subprogram allowed the return
871 -- value to be the ancestor of the return type if the return type
872 -- was a null extension. This was plainly incorrect.
ff7139c3 873
ec4867fa
ES
874 else
875 Error_Msg_N
876 ("wrong type for return_subtype_indication", Subtype_Ind);
877 end if;
878 end Check_Return_Subtype_Indication;
879
880 ---------------------
881 -- Local Variables --
882 ---------------------
883
445e5888
AC
884 Expr : Node_Id;
885 Obj_Decl : Node_Id;
ec4867fa
ES
886
887 -- Start of processing for Analyze_Function_Return
888
889 begin
890 Set_Return_Present (Scope_Id);
891
5d37ba92 892 if Nkind (N) = N_Simple_Return_Statement then
ec4867fa 893 Expr := Expression (N);
4ee646da 894
e917aec2
RD
895 -- Guard against a malformed expression. The parser may have tried to
896 -- recover but the node is not analyzable.
4ee646da
AC
897
898 if Nkind (Expr) = N_Error then
899 Set_Etype (Expr, Any_Type);
900 Expander_Mode_Save_And_Set (False);
901 return;
902
903 else
0180fd26
AC
904 -- The resolution of a controlled [extension] aggregate associated
905 -- with a return statement creates a temporary which needs to be
906 -- finalized on function exit. Wrap the return statement inside a
907 -- block so that the finalization machinery can detect this case.
908 -- This early expansion is done only when the return statement is
909 -- not part of a handled sequence of statements.
910
911 if Nkind_In (Expr, N_Aggregate,
912 N_Extension_Aggregate)
913 and then Needs_Finalization (R_Type)
914 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
915 then
916 Rewrite (N,
917 Make_Block_Statement (Loc,
918 Handled_Statement_Sequence =>
919 Make_Handled_Sequence_Of_Statements (Loc,
920 Statements => New_List (Relocate_Node (N)))));
921
922 Analyze (N);
923 return;
924 end if;
925
4b963531
AC
926 Analyze (Expr);
927
928 -- Ada 2005 (AI-251): If the type of the returned object is
929 -- an access to an interface type then we add an implicit type
930 -- conversion to force the displacement of the "this" pointer to
931 -- reference the secondary dispatch table. We cannot delay the
932 -- generation of this implicit conversion until the expansion
933 -- because in this case the type resolution changes the decoration
934 -- of the expression node to match R_Type; by contrast, if the
935 -- returned object is a class-wide interface type then it is too
936 -- early to generate here the implicit conversion since the return
937 -- statement may be rewritten by the expander into an extended
938 -- return statement whose expansion takes care of adding the
939 -- implicit type conversion to displace the pointer to the object.
940
941 if Expander_Active
942 and then Serious_Errors_Detected = 0
943 and then Is_Access_Type (R_Type)
944 and then Nkind (Expr) /= N_Null
945 and then Is_Interface (Designated_Type (R_Type))
946 and then Is_Progenitor (Designated_Type (R_Type),
947 Designated_Type (Etype (Expr)))
948 then
73e5aa55 949 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
4b963531
AC
950 Analyze (Expr);
951 end if;
952
953 Resolve (Expr, R_Type);
4ee646da
AC
954 Check_Limited_Return (Expr);
955 end if;
ec4867fa 956
ad05f2e9 957 -- RETURN only allowed in SPARK as the last statement in function
607d0635 958
fe5d3068 959 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
607d0635
AC
960 and then
961 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
8d606a78 962 or else Present (Next (N)))
607d0635 963 then
ce5ba43a 964 Check_SPARK_05_Restriction
fe5d3068 965 ("RETURN should be the last statement in function", N);
607d0635
AC
966 end if;
967
ec4867fa 968 else
ce5ba43a 969 Check_SPARK_05_Restriction ("extended RETURN is not allowed", N);
445e5888 970 Obj_Decl := Last (Return_Object_Declarations (N));
607d0635 971
ec4867fa
ES
972 -- Analyze parts specific to extended_return_statement:
973
974 declare
de6cad7c 975 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
b9daa96e 976 HSS : constant Node_Id := Handled_Statement_Sequence (N);
ec4867fa
ES
977
978 begin
979 Expr := Expression (Obj_Decl);
980
981 -- Note: The check for OK_For_Limited_Init will happen in
982 -- Analyze_Object_Declaration; we treat it as a normal
983 -- object declaration.
984
cd1c668b 985 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
ec4867fa
ES
986 Analyze (Obj_Decl);
987
ec4867fa
ES
988 Check_Return_Subtype_Indication (Obj_Decl);
989
990 if Present (HSS) then
991 Analyze (HSS);
992
993 if Present (Exception_Handlers (HSS)) then
994
995 -- ???Has_Nested_Block_With_Handler needs to be set.
996 -- Probably by creating an actual N_Block_Statement.
997 -- Probably in Expand.
998
999 null;
1000 end if;
1001 end if;
1002
9337aa0a
AC
1003 -- Mark the return object as referenced, since the return is an
1004 -- implicit reference of the object.
1005
1006 Set_Referenced (Defining_Identifier (Obj_Decl));
1007
ec4867fa 1008 Check_References (Stm_Entity);
de6cad7c
AC
1009
1010 -- Check RM 6.5 (5.9/3)
1011
1012 if Has_Aliased then
1013 if Ada_Version < Ada_2012 then
dbfeb4fa
RD
1014
1015 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
1016 -- Can it really happen (extended return???)
1017
1018 Error_Msg_N
b785e0b8
AC
1019 ("aliased only allowed for limited return objects "
1020 & "in Ada 2012??", N);
de6cad7c 1021
51245e2d 1022 elsif not Is_Limited_View (R_Type) then
3ccedacc
AC
1023 Error_Msg_N
1024 ("aliased only allowed for limited return objects", N);
de6cad7c
AC
1025 end if;
1026 end if;
ec4867fa
ES
1027 end;
1028 end if;
1029
21d27997 1030 -- Case of Expr present
5d37ba92 1031
ec4867fa 1032 if Present (Expr)
21d27997 1033
8fde064e 1034 -- Defend against previous errors
21d27997
RD
1035
1036 and then Nkind (Expr) /= N_Empty
5d37ba92 1037 and then Present (Etype (Expr))
ec4867fa 1038 then
5d37ba92
ES
1039 -- Apply constraint check. Note that this is done before the implicit
1040 -- conversion of the expression done for anonymous access types to
f3d57416 1041 -- ensure correct generation of the null-excluding check associated
5d37ba92
ES
1042 -- with null-excluding expressions found in return statements.
1043
1044 Apply_Constraint_Check (Expr, R_Type);
1045
1046 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
1047 -- type, apply an implicit conversion of the expression to that type
1048 -- to force appropriate static and run-time accessibility checks.
ec4867fa 1049
0791fbe9 1050 if Ada_Version >= Ada_2005
ec4867fa
ES
1051 and then Ekind (R_Type) = E_Anonymous_Access_Type
1052 then
1053 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1054 Analyze_And_Resolve (Expr, R_Type);
b6b5cca8
AC
1055
1056 -- If this is a local anonymous access to subprogram, the
1057 -- accessibility check can be applied statically. The return is
1058 -- illegal if the access type of the return expression is declared
1059 -- inside of the subprogram (except if it is the subtype indication
1060 -- of an extended return statement).
1061
1062 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1063 if not Comes_From_Source (Current_Scope)
1064 or else Ekind (Current_Scope) = E_Return_Statement
1065 then
1066 null;
1067
1068 elsif
1069 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1070 then
1071 Error_Msg_N ("cannot return local access to subprogram", N);
1072 end if;
1ebc2612
AC
1073
1074 -- The expression cannot be of a formal incomplete type
1075
1076 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1077 and then Is_Generic_Type (Etype (Expr))
1078 then
1079 Error_Msg_N
1080 ("cannot return expression of a formal incomplete type", N);
ec4867fa
ES
1081 end if;
1082
21d27997
RD
1083 -- If the result type is class-wide, then check that the return
1084 -- expression's type is not declared at a deeper level than the
1085 -- function (RM05-6.5(5.6/2)).
1086
0791fbe9 1087 if Ada_Version >= Ada_2005
21d27997
RD
1088 and then Is_Class_Wide_Type (R_Type)
1089 then
1090 if Type_Access_Level (Etype (Expr)) >
1091 Subprogram_Access_Level (Scope_Id)
1092 then
1093 Error_Msg_N
3ccedacc
AC
1094 ("level of return expression type is deeper than "
1095 & "class-wide function!", Expr);
21d27997
RD
1096 end if;
1097 end if;
1098
4755cce9
JM
1099 -- Check incorrect use of dynamically tagged expression
1100
1101 if Is_Tagged_Type (R_Type) then
1102 Check_Dynamically_Tagged_Expression
1103 (Expr => Expr,
1104 Typ => R_Type,
1105 Related_Nod => N);
ec4867fa
ES
1106 end if;
1107
ec4867fa
ES
1108 -- ??? A real run-time accessibility check is needed in cases
1109 -- involving dereferences of access parameters. For now we just
1110 -- check the static cases.
1111
0791fbe9 1112 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
51245e2d 1113 and then Is_Limited_View (Etype (Scope_Id))
ec4867fa
ES
1114 and then Object_Access_Level (Expr) >
1115 Subprogram_Access_Level (Scope_Id)
1116 then
9694c039
AC
1117 -- Suppress the message in a generic, where the rewriting
1118 -- is irrelevant.
1119
1120 if Inside_A_Generic then
1121 null;
1122
1123 else
1124 Rewrite (N,
1125 Make_Raise_Program_Error (Loc,
1126 Reason => PE_Accessibility_Check_Failed));
1127 Analyze (N);
1128
43417b90 1129 Error_Msg_Warn := SPARK_Mode /= On;
4a28b181
AC
1130 Error_Msg_N ("cannot return a local value by reference<<", N);
1131 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
9694c039 1132 end if;
ec4867fa 1133 end if;
5d37ba92
ES
1134
1135 if Known_Null (Expr)
1136 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1137 and then Null_Exclusion_Present (Parent (Scope_Id))
1138 then
1139 Apply_Compile_Time_Constraint_Error
1140 (N => Expr,
1141 Msg => "(Ada 2005) null not allowed for "
dbfeb4fa 1142 & "null-excluding return??",
5d37ba92
ES
1143 Reason => CE_Null_Not_Allowed);
1144 end if;
445e5888
AC
1145
1146 -- RM 6.5 (5.4/3): accessibility checks also apply if the return object
1147 -- has no initializing expression.
1148
1149 elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then
1150 if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) >
1151 Subprogram_Access_Level (Scope_Id)
1152 then
1153 Error_Msg_N
1154 ("level of return expression type is deeper than "
1155 & "class-wide function!", Obj_Decl);
1156 end if;
ec4867fa
ES
1157 end if;
1158 end Analyze_Function_Return;
1159
996ae0b0
RK
1160 -------------------------------------
1161 -- Analyze_Generic_Subprogram_Body --
1162 -------------------------------------
1163
1164 procedure Analyze_Generic_Subprogram_Body
1165 (N : Node_Id;
1166 Gen_Id : Entity_Id)
1167 is
fbf5a39b 1168 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
996ae0b0 1169 Kind : constant Entity_Kind := Ekind (Gen_Id);
fbf5a39b 1170 Body_Id : Entity_Id;
996ae0b0 1171 New_N : Node_Id;
fbf5a39b 1172 Spec : Node_Id;
996ae0b0
RK
1173
1174 begin
82c80734
RD
1175 -- Copy body and disable expansion while analyzing the generic For a
1176 -- stub, do not copy the stub (which would load the proper body), this
1177 -- will be done when the proper body is analyzed.
996ae0b0
RK
1178
1179 if Nkind (N) /= N_Subprogram_Body_Stub then
1180 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1181 Rewrite (N, New_N);
1182 Start_Generic;
1183 end if;
1184
1185 Spec := Specification (N);
1186
1187 -- Within the body of the generic, the subprogram is callable, and
1188 -- behaves like the corresponding non-generic unit.
1189
fbf5a39b 1190 Body_Id := Defining_Entity (Spec);
996ae0b0
RK
1191
1192 if Kind = E_Generic_Procedure
1193 and then Nkind (Spec) /= N_Procedure_Specification
1194 then
fbf5a39b 1195 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
996ae0b0
RK
1196 return;
1197
1198 elsif Kind = E_Generic_Function
1199 and then Nkind (Spec) /= N_Function_Specification
1200 then
fbf5a39b 1201 Error_Msg_N ("invalid body for generic function ", Body_Id);
996ae0b0
RK
1202 return;
1203 end if;
1204
fbf5a39b 1205 Set_Corresponding_Body (Gen_Decl, Body_Id);
996ae0b0
RK
1206
1207 if Has_Completion (Gen_Id)
1208 and then Nkind (Parent (N)) /= N_Subunit
1209 then
1210 Error_Msg_N ("duplicate generic body", N);
1211 return;
1212 else
1213 Set_Has_Completion (Gen_Id);
1214 end if;
1215
1216 if Nkind (N) = N_Subprogram_Body_Stub then
1217 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1218 else
1219 Set_Corresponding_Spec (N, Gen_Id);
1220 end if;
1221
1222 if Nkind (Parent (N)) = N_Compilation_Unit then
1223 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1224 end if;
1225
1226 -- Make generic parameters immediately visible in the body. They are
1227 -- needed to process the formals declarations. Then make the formals
1228 -- visible in a separate step.
1229
0a36105d 1230 Push_Scope (Gen_Id);
996ae0b0
RK
1231
1232 declare
1233 E : Entity_Id;
1234 First_Ent : Entity_Id;
1235
1236 begin
1237 First_Ent := First_Entity (Gen_Id);
1238
1239 E := First_Ent;
1240 while Present (E) and then not Is_Formal (E) loop
1241 Install_Entity (E);
1242 Next_Entity (E);
1243 end loop;
1244
1245 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1246
1247 -- Now generic formals are visible, and the specification can be
1248 -- analyzed, for subsequent conformance check.
1249
fbf5a39b 1250 Body_Id := Analyze_Subprogram_Specification (Spec);
996ae0b0 1251
fbf5a39b 1252 -- Make formal parameters visible
996ae0b0
RK
1253
1254 if Present (E) then
1255
fbf5a39b
AC
1256 -- E is the first formal parameter, we loop through the formals
1257 -- installing them so that they will be visible.
996ae0b0
RK
1258
1259 Set_First_Entity (Gen_Id, E);
996ae0b0
RK
1260 while Present (E) loop
1261 Install_Entity (E);
1262 Next_Formal (E);
1263 end loop;
1264 end if;
1265
e895b435 1266 -- Visible generic entity is callable within its own body
996ae0b0 1267
ec4867fa
ES
1268 Set_Ekind (Gen_Id, Ekind (Body_Id));
1269 Set_Ekind (Body_Id, E_Subprogram_Body);
1270 Set_Convention (Body_Id, Convention (Gen_Id));
1271 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1272 Set_Scope (Body_Id, Scope (Gen_Id));
c5cec2fe
AC
1273
1274 -- Inherit the "ghostness" of the generic spec. Note that this
1275 -- property is not directly inherited as the body may be subject
1276 -- to a different Ghost assertion policy.
1277
8636f52f 1278 if Is_Ghost_Entity (Gen_Id) or else Ghost_Mode > None then
c5cec2fe
AC
1279 Set_Is_Ghost_Entity (Body_Id);
1280
1281 -- The Ghost policy in effect at the point of declaration and at
3c756b76 1282 -- the point of completion must match (SPARK RM 6.9(15)).
c5cec2fe
AC
1283
1284 Check_Ghost_Completion (Gen_Id, Body_Id);
1285 end if;
1286
fbf5a39b
AC
1287 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1288
1289 if Nkind (N) = N_Subprogram_Body_Stub then
1290
e895b435 1291 -- No body to analyze, so restore state of generic unit
fbf5a39b
AC
1292
1293 Set_Ekind (Gen_Id, Kind);
1294 Set_Ekind (Body_Id, Kind);
1295
1296 if Present (First_Ent) then
1297 Set_First_Entity (Gen_Id, First_Ent);
1298 end if;
1299
1300 End_Scope;
1301 return;
1302 end if;
996ae0b0 1303
82c80734
RD
1304 -- If this is a compilation unit, it must be made visible explicitly,
1305 -- because the compilation of the declaration, unlike other library
1306 -- unit declarations, does not. If it is not a unit, the following
1307 -- is redundant but harmless.
996ae0b0
RK
1308
1309 Set_Is_Immediately_Visible (Gen_Id);
fbf5a39b 1310 Reference_Body_Formals (Gen_Id, Body_Id);
996ae0b0 1311
ec4867fa
ES
1312 if Is_Child_Unit (Gen_Id) then
1313 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1314 end if;
1315
996ae0b0 1316 Set_Actual_Subtypes (N, Current_Scope);
483361a6 1317
579847c2
AC
1318 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1319 Set_SPARK_Pragma_Inherited (Body_Id, True);
1320
996ae0b0
RK
1321 Analyze_Declarations (Declarations (N));
1322 Check_Completion;
1323 Analyze (Handled_Statement_Sequence (N));
1324
1325 Save_Global_References (Original_Node (N));
1326
82c80734
RD
1327 -- Prior to exiting the scope, include generic formals again (if any
1328 -- are present) in the set of local entities.
996ae0b0
RK
1329
1330 if Present (First_Ent) then
1331 Set_First_Entity (Gen_Id, First_Ent);
1332 end if;
1333
fbf5a39b 1334 Check_References (Gen_Id);
996ae0b0
RK
1335 end;
1336
e6f69614 1337 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
996ae0b0
RK
1338 End_Scope;
1339 Check_Subprogram_Order (N);
1340
e895b435 1341 -- Outside of its body, unit is generic again
996ae0b0
RK
1342
1343 Set_Ekind (Gen_Id, Kind);
fbf5a39b 1344 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
5d37ba92
ES
1345
1346 if Style_Check then
1347 Style.Check_Identifier (Body_Id, Gen_Id);
1348 end if;
13d923cc 1349
996ae0b0 1350 End_Generic;
996ae0b0
RK
1351 end Analyze_Generic_Subprogram_Body;
1352
4d8f3296
ES
1353 ----------------------------
1354 -- Analyze_Null_Procedure --
1355 ----------------------------
1356
1357 procedure Analyze_Null_Procedure
1358 (N : Node_Id;
1359 Is_Completion : out Boolean)
1360 is
1361 Loc : constant Source_Ptr := Sloc (N);
1362 Spec : constant Node_Id := Specification (N);
1363 Designator : Entity_Id;
1364 Form : Node_Id;
1365 Null_Body : Node_Id := Empty;
1366 Prev : Entity_Id;
1367
1368 begin
1369 -- Capture the profile of the null procedure before analysis, for
1370 -- expansion at the freeze point and at each point of call. The body is
1371 -- used if the procedure has preconditions, or if it is a completion. In
1372 -- the first case the body is analyzed at the freeze point, in the other
1373 -- it replaces the null procedure declaration.
1374
1375 Null_Body :=
1376 Make_Subprogram_Body (Loc,
1377 Specification => New_Copy_Tree (Spec),
8c35b40a 1378 Declarations => New_List,
4d8f3296
ES
1379 Handled_Statement_Sequence =>
1380 Make_Handled_Sequence_Of_Statements (Loc,
1381 Statements => New_List (Make_Null_Statement (Loc))));
1382
1383 -- Create new entities for body and formals
1384
1385 Set_Defining_Unit_Name (Specification (Null_Body),
9d2a2071
AC
1386 Make_Defining_Identifier
1387 (Sloc (Defining_Entity (N)),
1388 Chars (Defining_Entity (N))));
4d8f3296
ES
1389
1390 Form := First (Parameter_Specifications (Specification (Null_Body)));
1391 while Present (Form) loop
1392 Set_Defining_Identifier (Form,
9d2a2071
AC
1393 Make_Defining_Identifier
1394 (Sloc (Defining_Identifier (Form)),
1395 Chars (Defining_Identifier (Form))));
4d8f3296
ES
1396 Next (Form);
1397 end loop;
1398
1399 -- Determine whether the null procedure may be a completion of a generic
1400 -- suprogram, in which case we use the new null body as the completion
1401 -- and set minimal semantic information on the original declaration,
1402 -- which is rewritten as a null statement.
1403
1404 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1405
1406 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1407 Insert_Before (N, Null_Body);
1408 Set_Ekind (Defining_Entity (N), Ekind (Prev));
4d8f3296
ES
1409
1410 Rewrite (N, Make_Null_Statement (Loc));
1411 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1412 Is_Completion := True;
1413 return;
1414
1415 else
4d8f3296
ES
1416 -- Resolve the types of the formals now, because the freeze point
1417 -- may appear in a different context, e.g. an instantiation.
1418
1419 Form := First (Parameter_Specifications (Specification (Null_Body)));
1420 while Present (Form) loop
1421 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1422 Find_Type (Parameter_Type (Form));
1423
1424 elsif
1425 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
1426 then
1427 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1428
1429 else
1430 -- The case of a null procedure with a formal that is an
1431 -- access_to_subprogram type, and that is used as an actual
1432 -- in an instantiation is left to the enthusiastic reader.
1433
1434 null;
1435 end if;
1436
1437 Next (Form);
1438 end loop;
1439 end if;
1440
1441 -- If there are previous overloadable entities with the same name,
1442 -- check whether any of them is completed by the null procedure.
1443
1444 if Present (Prev) and then Is_Overloadable (Prev) then
1445 Designator := Analyze_Subprogram_Specification (Spec);
1446 Prev := Find_Corresponding_Spec (N);
1447 end if;
1448
1449 if No (Prev) or else not Comes_From_Source (Prev) then
1450 Designator := Analyze_Subprogram_Specification (Spec);
1451 Set_Has_Completion (Designator);
1452
1453 -- Signal to caller that this is a procedure declaration
1454
1455 Is_Completion := False;
1456
1457 -- Null procedures are always inlined, but generic formal subprograms
1458 -- which appear as such in the internal instance of formal packages,
1459 -- need no completion and are not marked Inline.
1460
1461 if Expander_Active
1462 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1463 then
1464 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1465 Set_Body_To_Inline (N, Null_Body);
1466 Set_Is_Inlined (Designator);
1467 end if;
1468
1469 else
2fe258bf
AC
1470 -- The null procedure is a completion. We unconditionally rewrite
1471 -- this as a null body (even if expansion is not active), because
1472 -- there are various error checks that are applied on this body
1473 -- when it is analyzed (e.g. correct aspect placement).
4d8f3296 1474
a98480dd
AC
1475 if Has_Completion (Prev) then
1476 Error_Msg_Sloc := Sloc (Prev);
1477 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
1478 end if;
1479
4d8f3296 1480 Is_Completion := True;
2fe258bf
AC
1481 Rewrite (N, Null_Body);
1482 Analyze (N);
4d8f3296
ES
1483 end if;
1484 end Analyze_Null_Procedure;
1485
996ae0b0
RK
1486 -----------------------------
1487 -- Analyze_Operator_Symbol --
1488 -----------------------------
1489
82c80734
RD
1490 -- An operator symbol such as "+" or "and" may appear in context where the
1491 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1492 -- is just a string, as in (conjunction = "or"). In these cases the parser
1493 -- generates this node, and the semantics does the disambiguation. Other
1494 -- such case are actuals in an instantiation, the generic unit in an
1495 -- instantiation, and pragma arguments.
996ae0b0
RK
1496
1497 procedure Analyze_Operator_Symbol (N : Node_Id) is
1498 Par : constant Node_Id := Parent (N);
1499
1500 begin
1f0b1e48 1501 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
996ae0b0 1502 or else Nkind (Par) = N_Function_Instantiation
1f0b1e48 1503 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
996ae0b0 1504 or else (Nkind (Par) = N_Pragma_Argument_Association
1f0b1e48 1505 and then not Is_Pragma_String_Literal (Par))
996ae0b0 1506 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
800621e0
RD
1507 or else (Nkind (Par) = N_Attribute_Reference
1508 and then Attribute_Name (Par) /= Name_Value)
996ae0b0
RK
1509 then
1510 Find_Direct_Name (N);
1511
1512 else
1513 Change_Operator_Symbol_To_String_Literal (N);
1514 Analyze (N);
1515 end if;
1516 end Analyze_Operator_Symbol;
1517
1518 -----------------------------------
1519 -- Analyze_Parameter_Association --
1520 -----------------------------------
1521
1522 procedure Analyze_Parameter_Association (N : Node_Id) is
1523 begin
1524 Analyze (Explicit_Actual_Parameter (N));
1525 end Analyze_Parameter_Association;
1526
1527 ----------------------------
1528 -- Analyze_Procedure_Call --
1529 ----------------------------
1530
1531 procedure Analyze_Procedure_Call (N : Node_Id) is
1532 Loc : constant Source_Ptr := Sloc (N);
1533 P : constant Node_Id := Name (N);
1534 Actuals : constant List_Id := Parameter_Associations (N);
1535 Actual : Node_Id;
1536 New_N : Node_Id;
1537
1538 procedure Analyze_Call_And_Resolve;
1539 -- Do Analyze and Resolve calls for procedure call
cd5a9750 1540 -- At end, check illegal order dependence.
996ae0b0 1541
fbf5a39b
AC
1542 ------------------------------
1543 -- Analyze_Call_And_Resolve --
1544 ------------------------------
1545
996ae0b0
RK
1546 procedure Analyze_Call_And_Resolve is
1547 begin
1548 if Nkind (N) = N_Procedure_Call_Statement then
1549 Analyze_Call (N);
1550 Resolve (N, Standard_Void_Type);
1551 else
1552 Analyze (N);
1553 end if;
1554 end Analyze_Call_And_Resolve;
1555
1556 -- Start of processing for Analyze_Procedure_Call
1557
1558 begin
1559 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1560 -- a procedure call or an entry call. The prefix may denote an access
1561 -- to subprogram type, in which case an implicit dereference applies.
f3d57416 1562 -- If the prefix is an indexed component (without implicit dereference)
996ae0b0
RK
1563 -- then the construct denotes a call to a member of an entire family.
1564 -- If the prefix is a simple name, it may still denote a call to a
1565 -- parameterless member of an entry family. Resolution of these various
1566 -- interpretations is delicate.
1567
1568 Analyze (P);
1569
758c442c
GD
1570 -- If this is a call of the form Obj.Op, the call may have been
1571 -- analyzed and possibly rewritten into a block, in which case
1572 -- we are done.
1573
1574 if Analyzed (N) then
1575 return;
1576 end if;
1577
7415029d
AC
1578 -- If there is an error analyzing the name (which may have been
1579 -- rewritten if the original call was in prefix notation) then error
1580 -- has been emitted already, mark node and return.
996ae0b0 1581
21791d97 1582 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
996ae0b0
RK
1583 Set_Etype (N, Any_Type);
1584 return;
1585 end if;
1586
8636f52f
HK
1587 -- The name of the procedure call may reference an entity subject to
1588 -- pragma Ghost with policy Ignore. Set the mode now to ensure that any
1589 -- nodes generated during analysis and expansion are properly flagged as
1590 -- ignored Ghost.
1591
1592 Set_Ghost_Mode (N);
1593
996ae0b0
RK
1594 -- Otherwise analyze the parameters
1595
1596 if Present (Actuals) then
1597 Actual := First (Actuals);
1598
1599 while Present (Actual) loop
1600 Analyze (Actual);
1601 Check_Parameterless_Call (Actual);
1602 Next (Actual);
1603 end loop;
1604 end if;
1605
0bfc9a64 1606 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
996ae0b0
RK
1607
1608 if Nkind (P) = N_Attribute_Reference
b69cd36a
AC
1609 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1610 Name_Elab_Body,
1611 Name_Elab_Subp_Body)
996ae0b0
RK
1612 then
1613 if Present (Actuals) then
1614 Error_Msg_N
1615 ("no parameters allowed for this call", First (Actuals));
1616 return;
1617 end if;
1618
1619 Set_Etype (N, Standard_Void_Type);
1620 Set_Analyzed (N);
1621
1622 elsif Is_Entity_Name (P)
1623 and then Is_Record_Type (Etype (Entity (P)))
1624 and then Remote_AST_I_Dereference (P)
1625 then
1626 return;
1627
1628 elsif Is_Entity_Name (P)
1629 and then Ekind (Entity (P)) /= E_Entry_Family
1630 then
1631 if Is_Access_Type (Etype (P))
1632 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1633 and then No (Actuals)
1634 and then Comes_From_Source (N)
1635 then
ed2233dc 1636 Error_Msg_N ("missing explicit dereference in call", N);
996ae0b0
RK
1637 end if;
1638
1639 Analyze_Call_And_Resolve;
1640
1641 -- If the prefix is the simple name of an entry family, this is
1642 -- a parameterless call from within the task body itself.
1643
1644 elsif Is_Entity_Name (P)
1645 and then Nkind (P) = N_Identifier
1646 and then Ekind (Entity (P)) = E_Entry_Family
1647 and then Present (Actuals)
1648 and then No (Next (First (Actuals)))
1649 then
82c80734
RD
1650 -- Can be call to parameterless entry family. What appears to be the
1651 -- sole argument is in fact the entry index. Rewrite prefix of node
1652 -- accordingly. Source representation is unchanged by this
996ae0b0
RK
1653 -- transformation.
1654
1655 New_N :=
1656 Make_Indexed_Component (Loc,
1657 Prefix =>
1658 Make_Selected_Component (Loc,
1659 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1660 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1661 Expressions => Actuals);
1662 Set_Name (N, New_N);
1663 Set_Etype (New_N, Standard_Void_Type);
1664 Set_Parameter_Associations (N, No_List);
1665 Analyze_Call_And_Resolve;
1666
1667 elsif Nkind (P) = N_Explicit_Dereference then
1668 if Ekind (Etype (P)) = E_Subprogram_Type then
1669 Analyze_Call_And_Resolve;
1670 else
1671 Error_Msg_N ("expect access to procedure in call", P);
1672 end if;
1673
82c80734
RD
1674 -- The name can be a selected component or an indexed component that
1675 -- yields an access to subprogram. Such a prefix is legal if the call
1676 -- has parameter associations.
996ae0b0
RK
1677
1678 elsif Is_Access_Type (Etype (P))
1679 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1680 then
1681 if Present (Actuals) then
1682 Analyze_Call_And_Resolve;
1683 else
ed2233dc 1684 Error_Msg_N ("missing explicit dereference in call ", N);
996ae0b0
RK
1685 end if;
1686
82c80734
RD
1687 -- If not an access to subprogram, then the prefix must resolve to the
1688 -- name of an entry, entry family, or protected operation.
996ae0b0 1689
82c80734
RD
1690 -- For the case of a simple entry call, P is a selected component where
1691 -- the prefix is the task and the selector name is the entry. A call to
1692 -- a protected procedure will have the same syntax. If the protected
1693 -- object contains overloaded operations, the entity may appear as a
1694 -- function, the context will select the operation whose type is Void.
996ae0b0
RK
1695
1696 elsif Nkind (P) = N_Selected_Component
8fde064e
AC
1697 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1698 E_Procedure,
1699 E_Function)
996ae0b0
RK
1700 then
1701 Analyze_Call_And_Resolve;
1702
1703 elsif Nkind (P) = N_Selected_Component
1704 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1705 and then Present (Actuals)
1706 and then No (Next (First (Actuals)))
1707 then
82c80734
RD
1708 -- Can be call to parameterless entry family. What appears to be the
1709 -- sole argument is in fact the entry index. Rewrite prefix of node
1710 -- accordingly. Source representation is unchanged by this
996ae0b0
RK
1711 -- transformation.
1712
1713 New_N :=
1714 Make_Indexed_Component (Loc,
1715 Prefix => New_Copy (P),
1716 Expressions => Actuals);
1717 Set_Name (N, New_N);
1718 Set_Etype (New_N, Standard_Void_Type);
1719 Set_Parameter_Associations (N, No_List);
1720 Analyze_Call_And_Resolve;
1721
1722 -- For the case of a reference to an element of an entry family, P is
1723 -- an indexed component whose prefix is a selected component (task and
1724 -- entry family), and whose index is the entry family index.
1725
1726 elsif Nkind (P) = N_Indexed_Component
1727 and then Nkind (Prefix (P)) = N_Selected_Component
1728 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1729 then
1730 Analyze_Call_And_Resolve;
1731
1732 -- If the prefix is the name of an entry family, it is a call from
1733 -- within the task body itself.
1734
1735 elsif Nkind (P) = N_Indexed_Component
1736 and then Nkind (Prefix (P)) = N_Identifier
1737 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1738 then
1739 New_N :=
1740 Make_Selected_Component (Loc,
1741 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1742 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1743 Rewrite (Prefix (P), New_N);
1744 Analyze (P);
1745 Analyze_Call_And_Resolve;
1746
9f8d1e5c
AC
1747 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1748 -- procedure name, so the construct can only be a qualified expression.
1749
1750 elsif Nkind (P) = N_Qualified_Expression
1751 and then Ada_Version >= Ada_2012
1752 then
1753 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1754 Analyze (N);
1755
e895b435 1756 -- Anything else is an error
996ae0b0
RK
1757
1758 else
758c442c 1759 Error_Msg_N ("invalid procedure or entry call", N);
996ae0b0
RK
1760 end if;
1761 end Analyze_Procedure_Call;
1762
b0186f71
AC
1763 ------------------------------
1764 -- Analyze_Return_Statement --
1765 ------------------------------
1766
1767 procedure Analyze_Return_Statement (N : Node_Id) is
1768
1769 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1770 N_Extended_Return_Statement));
1771
1772 Returns_Object : constant Boolean :=
1773 Nkind (N) = N_Extended_Return_Statement
1774 or else
8fde064e
AC
1775 (Nkind (N) = N_Simple_Return_Statement
1776 and then Present (Expression (N)));
b0186f71
AC
1777 -- True if we're returning something; that is, "return <expression>;"
1778 -- or "return Result : T [:= ...]". False for "return;". Used for error
1779 -- checking: If Returns_Object is True, N should apply to a function
1780 -- body; otherwise N should apply to a procedure body, entry body,
1781 -- accept statement, or extended return statement.
1782
1783 function Find_What_It_Applies_To return Entity_Id;
1784 -- Find the entity representing the innermost enclosing body, accept
1785 -- statement, or extended return statement. If the result is a callable
1786 -- construct or extended return statement, then this will be the value
1787 -- of the Return_Applies_To attribute. Otherwise, the program is
1788 -- illegal. See RM-6.5(4/2).
1789
1790 -----------------------------
1791 -- Find_What_It_Applies_To --
1792 -----------------------------
1793
1794 function Find_What_It_Applies_To return Entity_Id is
1795 Result : Entity_Id := Empty;
1796
1797 begin
36b8f95f
AC
1798 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1799 -- and postconditions.
b0186f71
AC
1800
1801 for J in reverse 0 .. Scope_Stack.Last loop
1802 Result := Scope_Stack.Table (J).Entity;
11bc76df
AC
1803 exit when not Ekind_In (Result, E_Block, E_Loop)
1804 and then Chars (Result) /= Name_uPostconditions;
b0186f71
AC
1805 end loop;
1806
1807 pragma Assert (Present (Result));
1808 return Result;
1809 end Find_What_It_Applies_To;
1810
1811 -- Local declarations
1812
1813 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1814 Kind : constant Entity_Kind := Ekind (Scope_Id);
1815 Loc : constant Source_Ptr := Sloc (N);
1816 Stm_Entity : constant Entity_Id :=
1817 New_Internal_Entity
1818 (E_Return_Statement, Current_Scope, Loc, 'R');
1819
1820 -- Start of processing for Analyze_Return_Statement
1821
1822 begin
1823 Set_Return_Statement_Entity (N, Stm_Entity);
1824
1825 Set_Etype (Stm_Entity, Standard_Void_Type);
1826 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1827
1828 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1829 -- (4/2): an inner return statement will apply to this extended return.
1830
1831 if Nkind (N) = N_Extended_Return_Statement then
1832 Push_Scope (Stm_Entity);
1833 end if;
1834
1835 -- Check that pragma No_Return is obeyed. Don't complain about the
1836 -- implicitly-generated return that is placed at the end.
1837
1838 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1839 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1840 end if;
1841
1842 -- Warn on any unassigned OUT parameters if in procedure
1843
1844 if Ekind (Scope_Id) = E_Procedure then
1845 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1846 end if;
1847
1848 -- Check that functions return objects, and other things do not
1849
1850 if Kind = E_Function or else Kind = E_Generic_Function then
1851 if not Returns_Object then
1852 Error_Msg_N ("missing expression in return from function", N);
1853 end if;
1854
1855 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1856 if Returns_Object then
1857 Error_Msg_N ("procedure cannot return value (use function)", N);
1858 end if;
1859
1860 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1861 if Returns_Object then
1862 if Is_Protected_Type (Scope (Scope_Id)) then
1863 Error_Msg_N ("entry body cannot return value", N);
1864 else
1865 Error_Msg_N ("accept statement cannot return value", N);
1866 end if;
1867 end if;
1868
1869 elsif Kind = E_Return_Statement then
1870
1871 -- We are nested within another return statement, which must be an
1872 -- extended_return_statement.
1873
1874 if Returns_Object then
d0dcb2b1
AC
1875 if Nkind (N) = N_Extended_Return_Statement then
1876 Error_Msg_N
cc96a1b8 1877 ("extended return statement cannot be nested (use `RETURN;`)",
d0dcb2b1
AC
1878 N);
1879
1880 -- Case of a simple return statement with a value inside extended
1881 -- return statement.
1882
1883 else
1884 Error_Msg_N
3ccedacc
AC
1885 ("return nested in extended return statement cannot return "
1886 & "value (use `RETURN;`)", N);
d0dcb2b1 1887 end if;
b0186f71
AC
1888 end if;
1889
1890 else
1891 Error_Msg_N ("illegal context for return statement", N);
1892 end if;
1893
1894 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1895 Analyze_Function_Return (N);
1896
1897 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1898 Set_Return_Present (Scope_Id);
1899 end if;
1900
1901 if Nkind (N) = N_Extended_Return_Statement then
1902 End_Scope;
1903 end if;
1904
1905 Kill_Current_Values (Last_Assignment_Only => True);
1906 Check_Unreachable_Code (N);
dec6faf1
AC
1907
1908 Analyze_Dimension (N);
b0186f71
AC
1909 end Analyze_Return_Statement;
1910
5d37ba92
ES
1911 -------------------------------------
1912 -- Analyze_Simple_Return_Statement --
1913 -------------------------------------
ec4867fa 1914
5d37ba92 1915 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
996ae0b0 1916 begin
5d37ba92
ES
1917 if Present (Expression (N)) then
1918 Mark_Coextensions (N, Expression (N));
996ae0b0
RK
1919 end if;
1920
5d37ba92
ES
1921 Analyze_Return_Statement (N);
1922 end Analyze_Simple_Return_Statement;
996ae0b0 1923
82c80734
RD
1924 -------------------------
1925 -- Analyze_Return_Type --
1926 -------------------------
1927
1928 procedure Analyze_Return_Type (N : Node_Id) is
1929 Designator : constant Entity_Id := Defining_Entity (N);
1930 Typ : Entity_Id := Empty;
1931
1932 begin
ec4867fa
ES
1933 -- Normal case where result definition does not indicate an error
1934
41251c60
JM
1935 if Result_Definition (N) /= Error then
1936 if Nkind (Result_Definition (N)) = N_Access_Definition then
ce5ba43a 1937 Check_SPARK_05_Restriction
fe5d3068 1938 ("access result is not allowed", Result_Definition (N));
daec8eeb 1939
b1c11e0e
JM
1940 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1941
1942 declare
1943 AD : constant Node_Id :=
1944 Access_To_Subprogram_Definition (Result_Definition (N));
1945 begin
1946 if Present (AD) and then Protected_Present (AD) then
1947 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1948 else
1949 Typ := Access_Definition (N, Result_Definition (N));
1950 end if;
1951 end;
1952
41251c60
JM
1953 Set_Parent (Typ, Result_Definition (N));
1954 Set_Is_Local_Anonymous_Access (Typ);
1955 Set_Etype (Designator, Typ);
1956
b66c3ff4
AC
1957 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1958
1959 Null_Exclusion_Static_Checks (N);
1960
41251c60
JM
1961 -- Subtype_Mark case
1962
1963 else
1964 Find_Type (Result_Definition (N));
1965 Typ := Entity (Result_Definition (N));
1966 Set_Etype (Designator, Typ);
1967
2ba431e5 1968 -- Unconstrained array as result is not allowed in SPARK
daec8eeb 1969
8fde064e 1970 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
ce5ba43a 1971 Check_SPARK_05_Restriction
fe5d3068 1972 ("returning an unconstrained array is not allowed",
7394c8cc 1973 Result_Definition (N));
daec8eeb
YM
1974 end if;
1975
b66c3ff4
AC
1976 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1977
1978 Null_Exclusion_Static_Checks (N);
1979
1980 -- If a null exclusion is imposed on the result type, then create
1981 -- a null-excluding itype (an access subtype) and use it as the
1982 -- function's Etype. Note that the null exclusion checks are done
1983 -- right before this, because they don't get applied to types that
1984 -- do not come from source.
1985
8fde064e 1986 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
b66c3ff4
AC
1987 Set_Etype (Designator,
1988 Create_Null_Excluding_Itype
ff7139c3
AC
1989 (T => Typ,
1990 Related_Nod => N,
1991 Scope_Id => Scope (Current_Scope)));
1992
1993 -- The new subtype must be elaborated before use because
1994 -- it is visible outside of the function. However its base
1995 -- type may not be frozen yet, so the reference that will
1996 -- force elaboration must be attached to the freezing of
1997 -- the base type.
1998
212863c0
AC
1999 -- If the return specification appears on a proper body,
2000 -- the subtype will have been created already on the spec.
2001
ff7139c3 2002 if Is_Frozen (Typ) then
212863c0
AC
2003 if Nkind (Parent (N)) = N_Subprogram_Body
2004 and then Nkind (Parent (Parent (N))) = N_Subunit
2005 then
2006 null;
2007 else
2008 Build_Itype_Reference (Etype (Designator), Parent (N));
2009 end if;
2010
ff7139c3
AC
2011 else
2012 Ensure_Freeze_Node (Typ);
2013
2014 declare
212863c0 2015 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
ff7139c3
AC
2016 begin
2017 Set_Itype (IR, Etype (Designator));
2018 Append_Freeze_Actions (Typ, New_List (IR));
2019 end;
2020 end if;
2021
b66c3ff4
AC
2022 else
2023 Set_Etype (Designator, Typ);
2024 end if;
2025
41251c60 2026 if Ekind (Typ) = E_Incomplete_Type
0a36105d
JM
2027 and then Is_Value_Type (Typ)
2028 then
2029 null;
2030
2031 elsif Ekind (Typ) = E_Incomplete_Type
41251c60 2032 or else (Is_Class_Wide_Type (Typ)
4b6f99f5 2033 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
41251c60 2034 then
dd386db0
AC
2035 -- AI05-0151: Tagged incomplete types are allowed in all formal
2036 -- parts. Untagged incomplete types are not allowed in bodies.
b973629e
AC
2037 -- As a consequence, limited views cannot appear in a basic
2038 -- declaration that is itself within a body, because there is
2039 -- no point at which the non-limited view will become visible.
dd386db0
AC
2040
2041 if Ada_Version >= Ada_2012 then
b973629e
AC
2042 if From_Limited_With (Typ) and then In_Package_Body then
2043 Error_Msg_NE
2044 ("invalid use of incomplete type&",
3f80a182 2045 Result_Definition (N), Typ);
b973629e 2046
1ebc2612
AC
2047 -- The return type of a subprogram body cannot be of a
2048 -- formal incomplete type.
2049
2050 elsif Is_Generic_Type (Typ)
2051 and then Nkind (Parent (N)) = N_Subprogram_Body
2052 then
2053 Error_Msg_N
2054 ("return type cannot be a formal incomplete type",
2055 Result_Definition (N));
2056
2057 elsif Is_Class_Wide_Type (Typ)
2058 and then Is_Generic_Type (Root_Type (Typ))
2059 and then Nkind (Parent (N)) = N_Subprogram_Body
2060 then
2061 Error_Msg_N
2062 ("return type cannot be a formal incomplete type",
2063 Result_Definition (N));
2064
b973629e 2065 elsif Is_Tagged_Type (Typ) then
dd386db0
AC
2066 null;
2067
ed09416f
AC
2068 -- Use is legal in a thunk generated for an operation
2069 -- inherited from a progenitor.
2070
2071 elsif Is_Thunk (Designator)
2072 and then Present (Non_Limited_View (Typ))
2073 then
2074 null;
2075
5b6f12c7 2076 elsif Nkind (Parent (N)) = N_Subprogram_Body
31d922e3
AC
2077 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
2078 N_Entry_Body)
dd386db0
AC
2079 then
2080 Error_Msg_NE
2081 ("invalid use of untagged incomplete type&",
2082 Designator, Typ);
2083 end if;
2084
63be2a5a 2085 -- The type must be completed in the current package. This
31d922e3 2086 -- is checked at the end of the package declaration when
7b7a0c2b
AC
2087 -- Taft-amendment types are identified. If the return type
2088 -- is class-wide, there is no required check, the type can
2089 -- be a bona fide TAT.
63be2a5a
AC
2090
2091 if Ekind (Scope (Current_Scope)) = E_Package
c199ccf7 2092 and then In_Private_Part (Scope (Current_Scope))
7b7a0c2b 2093 and then not Is_Class_Wide_Type (Typ)
63be2a5a
AC
2094 then
2095 Append_Elmt (Designator, Private_Dependents (Typ));
2096 end if;
2097
dd386db0
AC
2098 else
2099 Error_Msg_NE
2100 ("invalid use of incomplete type&", Designator, Typ);
2101 end if;
41251c60 2102 end if;
82c80734
RD
2103 end if;
2104
ec4867fa
ES
2105 -- Case where result definition does indicate an error
2106
82c80734
RD
2107 else
2108 Set_Etype (Designator, Any_Type);
2109 end if;
2110 end Analyze_Return_Type;
2111
996ae0b0
RK
2112 -----------------------------
2113 -- Analyze_Subprogram_Body --
2114 -----------------------------
2115
b1b543d2
BD
2116 procedure Analyze_Subprogram_Body (N : Node_Id) is
2117 Loc : constant Source_Ptr := Sloc (N);
2118 Body_Spec : constant Node_Id := Specification (N);
2119 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2120
2121 begin
2122 if Debug_Flag_C then
2123 Write_Str ("==> subprogram body ");
2124 Write_Name (Chars (Body_Id));
2125 Write_Str (" from ");
2126 Write_Location (Loc);
2127 Write_Eol;
2128 Indent;
2129 end if;
2130
2131 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2132
2133 -- The real work is split out into the helper, so it can do "return;"
2134 -- without skipping the debug output:
2135
2136 Analyze_Subprogram_Body_Helper (N);
2137
2138 if Debug_Flag_C then
2139 Outdent;
2140 Write_Str ("<== subprogram body ");
2141 Write_Name (Chars (Body_Id));
2142 Write_Str (" from ");
2143 Write_Location (Loc);
2144 Write_Eol;
2145 end if;
2146 end Analyze_Subprogram_Body;
2147
ea3c0651
AC
2148 --------------------------------------
2149 -- Analyze_Subprogram_Body_Contract --
2150 --------------------------------------
2151
ab8843fa 2152 procedure Analyze_Subprogram_Body_Contract (Body_Id : Entity_Id) is
c9d70ab1 2153 Body_Decl : constant Node_Id := Unit_Declaration_Node (Body_Id);
ea3c0651 2154
c9d70ab1
AC
2155 procedure Analyze_Completion_Contract (Spec_Id : Entity_Id);
2156 -- Analyze all delayed pragmas chained on the contract of subprogram
2157 -- body Body_Id as if they appeared at the end of a declarative region.
2158 -- Spec_Id denotes the corresponding spec. The aspects in question are:
2159 -- Refined_Depends
2160 -- Refined_Global
2161 -- Note that pragma Refined_Post is analyzed immediately
fc999c5d 2162
c9d70ab1
AC
2163 ---------------------------------
2164 -- Analyze_Completion_Contract --
2165 ---------------------------------
c61ef416 2166
c9d70ab1
AC
2167 procedure Analyze_Completion_Contract (Spec_Id : Entity_Id) is
2168 Items : constant Node_Id := Contract (Body_Id);
2169 Prag : Node_Id;
2170 Prag_Nam : Name_Id;
2171 Ref_Depends : Node_Id := Empty;
2172 Ref_Global : Node_Id := Empty;
ab8843fa 2173
c9d70ab1
AC
2174 begin
2175 -- All subprograms carry a contract, but for some it is not
2176 -- significant and should not be processed.
ab8843fa 2177
c9d70ab1
AC
2178 if not Has_Significant_Contract (Spec_Id) then
2179 return;
2180
2181 elsif Present (Items) then
2182
2183 -- Locate and store pragmas Refined_Depends and Refined_Global
2184 -- since their order of analysis matters.
2c8d828a 2185
c9d70ab1
AC
2186 Prag := Classifications (Items);
2187 while Present (Prag) loop
2188 Prag_Nam := Pragma_Name (Prag);
54e28df2 2189
c9d70ab1
AC
2190 if Prag_Nam = Name_Refined_Depends then
2191 Ref_Depends := Prag;
2192
2193 elsif Prag_Nam = Name_Refined_Global then
2194 Ref_Global := Prag;
2195 end if;
2196
2197 Prag := Next_Pragma (Prag);
2198 end loop;
ab8843fa
HK
2199 end if;
2200
c9d70ab1
AC
2201 -- Analyze Refined_Global first as Refined_Depends may mention items
2202 -- classified in the global refinement.
ab8843fa 2203
c9d70ab1
AC
2204 if Present (Ref_Global) then
2205 Analyze_Refined_Global_In_Decl_Part (Ref_Global);
c9d70ab1 2206 end if;
ab8843fa 2207
c9d70ab1
AC
2208 -- Refined_Depends must be analyzed after Refined_Global in order to
2209 -- see the modes of all global refinements.
2210
2211 if Present (Ref_Depends) then
2212 Analyze_Refined_Depends_In_Decl_Part (Ref_Depends);
ab8843fa 2213 end if;
c9d70ab1
AC
2214 end Analyze_Completion_Contract;
2215
2216 -- Local variables
2217
2218 Mode : SPARK_Mode_Type;
2219 Spec_Id : Entity_Id;
2220
2221 -- Start of processing for Analyze_Subprogram_Body_Contract
2222
2223 begin
2224 -- When a subprogram body declaration is illegal, its defining entity is
2225 -- left unanalyzed. There is nothing left to do in this case because the
2226 -- body lacks a contract, or even a proper Ekind.
2227
2228 if Ekind (Body_Id) = E_Void then
2229 return;
ab8843fa 2230 end if;
54e28df2 2231
c9d70ab1
AC
2232 -- Due to the timing of contract analysis, delayed pragmas may be
2233 -- subject to the wrong SPARK_Mode, usually that of the enclosing
2234 -- context. To remedy this, restore the original SPARK_Mode of the
2235 -- related subprogram body.
54e28df2 2236
c9d70ab1 2237 Save_SPARK_Mode_And_Set (Body_Id, Mode);
39d3009f 2238
c9d70ab1
AC
2239 if Nkind (Body_Decl) = N_Subprogram_Body_Stub then
2240 Spec_Id := Corresponding_Spec_Of_Stub (Body_Decl);
2241 else
2242 Spec_Id := Corresponding_Spec (Body_Decl);
2243 end if;
39d3009f 2244
c9d70ab1
AC
2245 -- The subprogram body is a completion, analyze all delayed pragmas that
2246 -- apply. Note that when the body is stand alone, the pragmas are always
2247 -- analyzed on the spot.
39d3009f 2248
c9d70ab1
AC
2249 if Present (Spec_Id) then
2250 Analyze_Completion_Contract (Spec_Id);
54e28df2 2251 end if;
c61ef416 2252
c9d70ab1
AC
2253 -- Ensure that the contract cases or postconditions mention 'Result or
2254 -- define a post-state.
2255
2256 Check_Result_And_Post_State (Body_Id);
2257
fc999c5d
RD
2258 -- Restore the SPARK_Mode of the enclosing context after all delayed
2259 -- pragmas have been analyzed.
2260
c61ef416 2261 Restore_SPARK_Mode (Mode);
ea3c0651
AC
2262 end Analyze_Subprogram_Body_Contract;
2263
b1b543d2
BD
2264 ------------------------------------
2265 -- Analyze_Subprogram_Body_Helper --
2266 ------------------------------------
2267
996ae0b0
RK
2268 -- This procedure is called for regular subprogram bodies, generic bodies,
2269 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2270 -- specification matters, and is used to create a proper declaration for
2271 -- the subprogram, or to perform conformance checks.
2272
b1b543d2 2273 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
fbf5a39b
AC
2274 Loc : constant Source_Ptr := Sloc (N);
2275 Body_Spec : constant Node_Id := Specification (N);
2276 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2277 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
0868e09c 2278 Conformant : Boolean;
21d27997 2279 HSS : Node_Id;
21d27997
RD
2280 Prot_Typ : Entity_Id := Empty;
2281 Spec_Id : Entity_Id;
2282 Spec_Decl : Node_Id := Empty;
2283
2284 Last_Real_Spec_Entity : Entity_Id := Empty;
2285 -- When we analyze a separate spec, the entity chain ends up containing
2286 -- the formals, as well as any itypes generated during analysis of the
2287 -- default expressions for parameters, or the arguments of associated
2288 -- precondition/postcondition pragmas (which are analyzed in the context
2289 -- of the spec since they have visibility on formals).
2290 --
2291 -- These entities belong with the spec and not the body. However we do
2292 -- the analysis of the body in the context of the spec (again to obtain
2293 -- visibility to the formals), and all the entities generated during
2294 -- this analysis end up also chained to the entity chain of the spec.
2295 -- But they really belong to the body, and there is circuitry to move
2296 -- them from the spec to the body.
2297 --
2298 -- However, when we do this move, we don't want to move the real spec
2299 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2300 -- variable points to the last real spec entity, so we only move those
2301 -- chained beyond that point. It is initialized to Empty to deal with
2302 -- the case where there is no separate spec.
996ae0b0 2303
c8a3028c
AC
2304 procedure Analyze_Aspects_On_Body_Or_Stub;
2305 -- Analyze the aspect specifications of a subprogram body [stub]. It is
2306 -- assumed that N has aspects.
2307
ac072cb2
AC
2308 function Body_Has_Contract return Boolean;
2309 -- Check whether unanalyzed body has an aspect or pragma that may
1399d355 2310 -- generate a SPARK contract.
ac072cb2 2311
ec4867fa 2312 procedure Check_Anonymous_Return;
e50e1c5e 2313 -- Ada 2005: if a function returns an access type that denotes a task,
ec4867fa
ES
2314 -- or a type that contains tasks, we must create a master entity for
2315 -- the anonymous type, which typically will be used in an allocator
2316 -- in the body of the function.
2317
e660dbf7
JM
2318 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2319 -- Look ahead to recognize a pragma that may appear after the body.
2320 -- If there is a previous spec, check that it appears in the same
2321 -- declarative part. If the pragma is Inline_Always, perform inlining
2322 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2323 -- If the body acts as a spec, and inlining is required, we create a
2324 -- subprogram declaration for it, in order to attach the body to inline.
21d27997
RD
2325 -- If pragma does not appear after the body, check whether there is
2326 -- an inline pragma before any local declarations.
c37bb106 2327
7665e4bd
AC
2328 procedure Check_Missing_Return;
2329 -- Checks for a function with a no return statements, and also performs
8d606a78
RD
2330 -- the warning checks implemented by Check_Returns. In formal mode, also
2331 -- verify that a function ends with a RETURN and that a procedure does
2332 -- not contain any RETURN.
7665e4bd 2333
d44202ba
HK
2334 function Disambiguate_Spec return Entity_Id;
2335 -- When a primitive is declared between the private view and the full
2336 -- view of a concurrent type which implements an interface, a special
2337 -- mechanism is used to find the corresponding spec of the primitive
2338 -- body.
2339
5dcab3ca
AC
2340 procedure Exchange_Limited_Views (Subp_Id : Entity_Id);
2341 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2342 -- incomplete types coming from a limited context and swap their limited
2343 -- views with the non-limited ones.
2344
d44202ba
HK
2345 function Is_Private_Concurrent_Primitive
2346 (Subp_Id : Entity_Id) return Boolean;
2347 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2348 -- type that implements an interface and has a private view.
2349
76a69663
ES
2350 procedure Set_Trivial_Subprogram (N : Node_Id);
2351 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2352 -- subprogram whose body is being analyzed. N is the statement node
2353 -- causing the flag to be set, if the following statement is a return
2354 -- of an entity, we mark the entity as set in source to suppress any
2355 -- warning on the stylized use of function stubs with a dummy return.
2356
758c442c
GD
2357 procedure Verify_Overriding_Indicator;
2358 -- If there was a previous spec, the entity has been entered in the
2359 -- current scope previously. If the body itself carries an overriding
2360 -- indicator, check that it is consistent with the known status of the
2361 -- entity.
2362
c8a3028c
AC
2363 -------------------------------------
2364 -- Analyze_Aspects_On_Body_Or_Stub --
2365 -------------------------------------
2366
2367 procedure Analyze_Aspects_On_Body_Or_Stub is
2368 procedure Diagnose_Misplaced_Aspects;
2369 -- Subprogram body [stub] N has aspects, but they are not properly
2370 -- placed. Provide precise diagnostics depending on the aspects
2371 -- involved.
2372
2373 --------------------------------
2374 -- Diagnose_Misplaced_Aspects --
2375 --------------------------------
2376
2377 procedure Diagnose_Misplaced_Aspects is
2378 Asp : Node_Id;
2379 Asp_Nam : Name_Id;
2380 Asp_Id : Aspect_Id;
2381 -- The current aspect along with its name and id
2382
2383 procedure SPARK_Aspect_Error (Ref_Nam : Name_Id);
2384 -- Emit an error message concerning SPARK aspect Asp. Ref_Nam is
2385 -- the name of the refined version of the aspect.
2386
2387 ------------------------
2388 -- SPARK_Aspect_Error --
2389 ------------------------
2390
2391 procedure SPARK_Aspect_Error (Ref_Nam : Name_Id) is
2392 begin
2393 -- The corresponding spec already contains the aspect in
2394 -- question and the one appearing on the body must be the
2395 -- refined form:
2396
2397 -- procedure P with Global ...;
2398 -- procedure P with Global ... is ... end P;
2399 -- ^
2400 -- Refined_Global
2401
2402 if Has_Aspect (Spec_Id, Asp_Id) then
2403 Error_Msg_Name_1 := Asp_Nam;
2404
2405 -- Subunits cannot carry aspects that apply to a subprogram
2406 -- declaration.
2407
2408 if Nkind (Parent (N)) = N_Subunit then
2409 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
2410
2411 else
2412 Error_Msg_Name_2 := Ref_Nam;
2413 Error_Msg_N ("aspect % should be %", Asp);
2414 end if;
2415
2416 -- Otherwise the aspect must appear in the spec, not in the
2417 -- body:
2418
2419 -- procedure P;
2420 -- procedure P with Global ... is ... end P;
2421
2422 else
2423 Error_Msg_N
2424 ("aspect specification must appear in subprogram "
2425 & "declaration", Asp);
2426 end if;
2427 end SPARK_Aspect_Error;
2428
2429 -- Start of processing for Diagnose_Misplaced_Aspects
2430
2431 begin
2432 -- Iterate over the aspect specifications and emit specific errors
2433 -- where applicable.
2434
2435 Asp := First (Aspect_Specifications (N));
2436 while Present (Asp) loop
2437 Asp_Nam := Chars (Identifier (Asp));
2438 Asp_Id := Get_Aspect_Id (Asp_Nam);
2439
2440 -- Do not emit errors on aspects that can appear on a
2441 -- subprogram body. This scenario occurs when the aspect
2442 -- specification list contains both misplaced and properly
2443 -- placed aspects.
2444
2445 if Aspect_On_Body_Or_Stub_OK (Asp_Id) then
2446 null;
2447
2448 -- Special diagnostics for SPARK aspects
2449
2450 elsif Asp_Nam = Name_Depends then
2451 SPARK_Aspect_Error (Name_Refined_Depends);
2452
2453 elsif Asp_Nam = Name_Global then
2454 SPARK_Aspect_Error (Name_Refined_Global);
2455
2456 elsif Asp_Nam = Name_Post then
2457 SPARK_Aspect_Error (Name_Refined_Post);
2458
2459 else
2460 Error_Msg_N
2461 ("aspect specification must appear in subprogram "
2462 & "declaration", Asp);
2463 end if;
2464
2465 Next (Asp);
2466 end loop;
2467 end Diagnose_Misplaced_Aspects;
2468
2469 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
2470
2471 begin
2472 -- Language-defined aspects cannot be associated with a subprogram
2473 -- body [stub] if the subprogram has a spec. Certain implementation
2474 -- defined aspects are allowed to break this rule (for list, see
2475 -- table Aspect_On_Body_Or_Stub_OK).
2476
2477 if Present (Spec_Id) and then not Aspects_On_Body_Or_Stub_OK (N) then
2478 Diagnose_Misplaced_Aspects;
2479 else
2480 Analyze_Aspect_Specifications (N, Body_Id);
2481 end if;
2482 end Analyze_Aspects_On_Body_Or_Stub;
2483
ac072cb2
AC
2484 -----------------------
2485 -- Body_Has_Contract --
2486 -----------------------
2487
2488 function Body_Has_Contract return Boolean is
2489 Decls : constant List_Id := Declarations (N);
2490 A_Spec : Node_Id;
2491 A : Aspect_Id;
2492 Decl : Node_Id;
2493 P_Id : Pragma_Id;
2494
2495 begin
2496 -- Check for unanalyzed aspects in the body that will
2497 -- generate a contract.
2498
2499 if Present (Aspect_Specifications (N)) then
2500 A_Spec := First (Aspect_Specifications (N));
2501 while Present (A_Spec) loop
2502 A := Get_Aspect_Id (Chars (Identifier (A_Spec)));
2503
1399d355
AC
2504 if A = Aspect_Contract_Cases or else
2505 A = Aspect_Depends or else
2506 A = Aspect_Global or else
2507 A = Aspect_Pre or else
2508 A = Aspect_Precondition or else
2509 A = Aspect_Post or else
2510 A = Aspect_Postcondition
ac072cb2
AC
2511 then
2512 return True;
2513 end if;
2514
2515 Next (A_Spec);
2516 end loop;
2517 end if;
2518
1399d355 2519 -- Check for pragmas that may generate a contract
ac072cb2
AC
2520
2521 if Present (Decls) then
2522 Decl := First (Decls);
2523 while Present (Decl) loop
2524 if Nkind (Decl) = N_Pragma then
2525 P_Id := Get_Pragma_Id (Pragma_Name (Decl));
2526
1399d355
AC
2527 if P_Id = Pragma_Contract_Cases or else
2528 P_Id = Pragma_Depends or else
2529 P_Id = Pragma_Global or else
2530 P_Id = Pragma_Pre or else
2531 P_Id = Pragma_Precondition or else
2532 P_Id = Pragma_Post or else
2533 P_Id = Pragma_Postcondition
ac072cb2
AC
2534 then
2535 return True;
2536 end if;
2537 end if;
2538
2539 Next (Decl);
2540 end loop;
2541 end if;
2542
2543 return False;
2544 end Body_Has_Contract;
2545
ec4867fa
ES
2546 ----------------------------
2547 -- Check_Anonymous_Return --
2548 ----------------------------
2549
2550 procedure Check_Anonymous_Return is
2551 Decl : Node_Id;
a523b302 2552 Par : Node_Id;
ec4867fa
ES
2553 Scop : Entity_Id;
2554
2555 begin
2556 if Present (Spec_Id) then
2557 Scop := Spec_Id;
2558 else
2559 Scop := Body_Id;
2560 end if;
2561
2562 if Ekind (Scop) = E_Function
2563 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
a523b302 2564 and then not Is_Thunk (Scop)
4b963531
AC
2565
2566 -- Skip internally built functions which handle the case of
2567 -- a null access (see Expand_Interface_Conversion)
2568
2569 and then not (Is_Interface (Designated_Type (Etype (Scop)))
4b6f99f5 2570 and then not Comes_From_Source (Parent (Scop)))
4b963531 2571
a523b302
JM
2572 and then (Has_Task (Designated_Type (Etype (Scop)))
2573 or else
4b6f99f5
RD
2574 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2575 and then
2576 Is_Limited_Record (Designated_Type (Etype (Scop)))))
ec4867fa 2577 and then Expander_Active
b20de9b9 2578
8fde064e 2579 -- Avoid cases with no tasking support
b20de9b9
AC
2580
2581 and then RTE_Available (RE_Current_Master)
2582 and then not Restriction_Active (No_Task_Hierarchy)
ec4867fa
ES
2583 then
2584 Decl :=
2585 Make_Object_Declaration (Loc,
2586 Defining_Identifier =>
2587 Make_Defining_Identifier (Loc, Name_uMaster),
2588 Constant_Present => True,
2589 Object_Definition =>
e4494292 2590 New_Occurrence_Of (RTE (RE_Master_Id), Loc),
ec4867fa
ES
2591 Expression =>
2592 Make_Explicit_Dereference (Loc,
e4494292 2593 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
ec4867fa
ES
2594
2595 if Present (Declarations (N)) then
2596 Prepend (Decl, Declarations (N));
2597 else
2598 Set_Declarations (N, New_List (Decl));
2599 end if;
2600
2601 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2602 Set_Has_Master_Entity (Scop);
a523b302
JM
2603
2604 -- Now mark the containing scope as a task master
2605
2606 Par := N;
2607 while Nkind (Par) /= N_Compilation_Unit loop
2608 Par := Parent (Par);
2609 pragma Assert (Present (Par));
2610
2611 -- If we fall off the top, we are at the outer level, and
2612 -- the environment task is our effective master, so nothing
2613 -- to mark.
2614
2615 if Nkind_In
2616 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2617 then
2618 Set_Is_Task_Master (Par, True);
2619 exit;
2620 end if;
2621 end loop;
ec4867fa
ES
2622 end if;
2623 end Check_Anonymous_Return;
2624
e660dbf7
JM
2625 -------------------------
2626 -- Check_Inline_Pragma --
2627 -------------------------
758c442c 2628
e660dbf7
JM
2629 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2630 Prag : Node_Id;
2631 Plist : List_Id;
0fb2ea01 2632
21d27997 2633 function Is_Inline_Pragma (N : Node_Id) return Boolean;
30783513 2634 -- True when N is a pragma Inline or Inline_Always that applies
33931112 2635 -- to this subprogram.
21d27997
RD
2636
2637 -----------------------
2638 -- Is_Inline_Pragma --
2639 -----------------------
2640
2641 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2642 begin
2643 return
2644 Nkind (N) = N_Pragma
2645 and then
8fde064e 2646 (Pragma_Name (N) = Name_Inline_Always
4b6f99f5
RD
2647 or else (Front_End_Inlining
2648 and then Pragma_Name (N) = Name_Inline))
21d27997 2649 and then
8fde064e
AC
2650 Chars
2651 (Expression (First (Pragma_Argument_Associations (N)))) =
2652 Chars (Body_Id);
21d27997
RD
2653 end Is_Inline_Pragma;
2654
2655 -- Start of processing for Check_Inline_Pragma
2656
c37bb106 2657 begin
e660dbf7
JM
2658 if not Expander_Active then
2659 return;
2660 end if;
2661
2662 if Is_List_Member (N)
2663 and then Present (Next (N))
21d27997 2664 and then Is_Inline_Pragma (Next (N))
c37bb106
AC
2665 then
2666 Prag := Next (N);
2667
21d27997
RD
2668 elsif Nkind (N) /= N_Subprogram_Body_Stub
2669 and then Present (Declarations (N))
2670 and then Is_Inline_Pragma (First (Declarations (N)))
2671 then
2672 Prag := First (Declarations (N));
2673
e660dbf7
JM
2674 else
2675 Prag := Empty;
c37bb106 2676 end if;
e660dbf7
JM
2677
2678 if Present (Prag) then
2679 if Present (Spec_Id) then
30196a76 2680 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
e660dbf7
JM
2681 Analyze (Prag);
2682 end if;
2683
2684 else
d39d6bb8 2685 -- Create a subprogram declaration, to make treatment uniform
e660dbf7
JM
2686
2687 declare
2688 Subp : constant Entity_Id :=
30196a76 2689 Make_Defining_Identifier (Loc, Chars (Body_Id));
e660dbf7 2690 Decl : constant Node_Id :=
30196a76
RD
2691 Make_Subprogram_Declaration (Loc,
2692 Specification =>
2693 New_Copy_Tree (Specification (N)));
2694
e660dbf7
JM
2695 begin
2696 Set_Defining_Unit_Name (Specification (Decl), Subp);
2697
2698 if Present (First_Formal (Body_Id)) then
21d27997 2699 Plist := Copy_Parameter_List (Body_Id);
e660dbf7
JM
2700 Set_Parameter_Specifications
2701 (Specification (Decl), Plist);
2702 end if;
2703
2704 Insert_Before (N, Decl);
2705 Analyze (Decl);
2706 Analyze (Prag);
2707 Set_Has_Pragma_Inline (Subp);
2708
76a69663 2709 if Pragma_Name (Prag) = Name_Inline_Always then
e660dbf7 2710 Set_Is_Inlined (Subp);
21d27997 2711 Set_Has_Pragma_Inline_Always (Subp);
e660dbf7
JM
2712 end if;
2713
158d55fa
AC
2714 -- Prior to copying the subprogram body to create a template
2715 -- for it for subsequent inlining, remove the pragma from
2716 -- the current body so that the copy that will produce the
2717 -- new body will start from a completely unanalyzed tree.
2718
2719 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2720 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2721 end if;
2722
e660dbf7
JM
2723 Spec := Subp;
2724 end;
2725 end if;
2726 end if;
2727 end Check_Inline_Pragma;
2728
7665e4bd
AC
2729 --------------------------
2730 -- Check_Missing_Return --
2731 --------------------------
2732
2733 procedure Check_Missing_Return is
2734 Id : Entity_Id;
2735 Missing_Ret : Boolean;
2736
2737 begin
2738 if Nkind (Body_Spec) = N_Function_Specification then
2739 if Present (Spec_Id) then
2740 Id := Spec_Id;
2741 else
2742 Id := Body_Id;
2743 end if;
2744
fe5d3068 2745 if Return_Present (Id) then
7665e4bd
AC
2746 Check_Returns (HSS, 'F', Missing_Ret);
2747
2748 if Missing_Ret then
2749 Set_Has_Missing_Return (Id);
2750 end if;
2751
2aca76d6
AC
2752 elsif Is_Generic_Subprogram (Id)
2753 or else not Is_Machine_Code_Subprogram (Id)
7665e4bd
AC
2754 then
2755 Error_Msg_N ("missing RETURN statement in function body", N);
2756 end if;
2757
fe5d3068 2758 -- If procedure with No_Return, check returns
607d0635 2759
fe5d3068
YM
2760 elsif Nkind (Body_Spec) = N_Procedure_Specification
2761 and then Present (Spec_Id)
2762 and then No_Return (Spec_Id)
607d0635 2763 then
fe5d3068
YM
2764 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2765 end if;
2766
ad05f2e9 2767 -- Special checks in SPARK mode
fe5d3068
YM
2768
2769 if Nkind (Body_Spec) = N_Function_Specification then
7394c8cc 2770
ad05f2e9 2771 -- In SPARK mode, last statement of a function should be a return
fe5d3068
YM
2772
2773 declare
2774 Stat : constant Node_Id := Last_Source_Statement (HSS);
2775 begin
2776 if Present (Stat)
7394c8cc
AC
2777 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2778 N_Extended_Return_Statement)
fe5d3068 2779 then
ce5ba43a 2780 Check_SPARK_05_Restriction
fe5d3068
YM
2781 ("last statement in function should be RETURN", Stat);
2782 end if;
2783 end;
2784
ad05f2e9 2785 -- In SPARK mode, verify that a procedure has no return
fe5d3068
YM
2786
2787 elsif Nkind (Body_Spec) = N_Procedure_Specification then
607d0635
AC
2788 if Present (Spec_Id) then
2789 Id := Spec_Id;
2790 else
2791 Id := Body_Id;
2792 end if;
2793
8d606a78
RD
2794 -- Would be nice to point to return statement here, can we
2795 -- borrow the Check_Returns procedure here ???
2796
607d0635 2797 if Return_Present (Id) then
ce5ba43a 2798 Check_SPARK_05_Restriction
fe5d3068 2799 ("procedure should not have RETURN", N);
607d0635 2800 end if;
7665e4bd
AC
2801 end if;
2802 end Check_Missing_Return;
2803
d44202ba
HK
2804 -----------------------
2805 -- Disambiguate_Spec --
2806 -----------------------
2807
2808 function Disambiguate_Spec return Entity_Id is
2809 Priv_Spec : Entity_Id;
2810 Spec_N : Entity_Id;
2811
2812 procedure Replace_Types (To_Corresponding : Boolean);
2813 -- Depending on the flag, replace the type of formal parameters of
2814 -- Body_Id if it is a concurrent type implementing interfaces with
2815 -- the corresponding record type or the other way around.
2816
2817 procedure Replace_Types (To_Corresponding : Boolean) is
2818 Formal : Entity_Id;
2819 Formal_Typ : Entity_Id;
2820
2821 begin
2822 Formal := First_Formal (Body_Id);
2823 while Present (Formal) loop
2824 Formal_Typ := Etype (Formal);
2825
df3e68b1
HK
2826 if Is_Class_Wide_Type (Formal_Typ) then
2827 Formal_Typ := Root_Type (Formal_Typ);
2828 end if;
2829
d44202ba
HK
2830 -- From concurrent type to corresponding record
2831
2832 if To_Corresponding then
2833 if Is_Concurrent_Type (Formal_Typ)
2834 and then Present (Corresponding_Record_Type (Formal_Typ))
4b6f99f5
RD
2835 and then
2836 Present (Interfaces
2837 (Corresponding_Record_Type (Formal_Typ)))
d44202ba
HK
2838 then
2839 Set_Etype (Formal,
2840 Corresponding_Record_Type (Formal_Typ));
2841 end if;
2842
2843 -- From corresponding record to concurrent type
2844
2845 else
2846 if Is_Concurrent_Record_Type (Formal_Typ)
2847 and then Present (Interfaces (Formal_Typ))
2848 then
2849 Set_Etype (Formal,
2850 Corresponding_Concurrent_Type (Formal_Typ));
2851 end if;
2852 end if;
2853
2854 Next_Formal (Formal);
2855 end loop;
2856 end Replace_Types;
2857
2858 -- Start of processing for Disambiguate_Spec
2859
2860 begin
2861 -- Try to retrieve the specification of the body as is. All error
2862 -- messages are suppressed because the body may not have a spec in
2863 -- its current state.
2864
2865 Spec_N := Find_Corresponding_Spec (N, False);
2866
2867 -- It is possible that this is the body of a primitive declared
2868 -- between a private and a full view of a concurrent type. The
2869 -- controlling parameter of the spec carries the concurrent type,
2870 -- not the corresponding record type as transformed by Analyze_
2871 -- Subprogram_Specification. In such cases, we undo the change
2872 -- made by the analysis of the specification and try to find the
2873 -- spec again.
766d7add 2874
8198b93d
HK
2875 -- Note that wrappers already have their corresponding specs and
2876 -- bodies set during their creation, so if the candidate spec is
16b05213 2877 -- a wrapper, then we definitely need to swap all types to their
8198b93d 2878 -- original concurrent status.
d44202ba 2879
8198b93d
HK
2880 if No (Spec_N)
2881 or else Is_Primitive_Wrapper (Spec_N)
2882 then
d44202ba
HK
2883 -- Restore all references of corresponding record types to the
2884 -- original concurrent types.
2885
2886 Replace_Types (To_Corresponding => False);
2887 Priv_Spec := Find_Corresponding_Spec (N, False);
2888
2889 -- The current body truly belongs to a primitive declared between
2890 -- a private and a full view. We leave the modified body as is,
2891 -- and return the true spec.
2892
2893 if Present (Priv_Spec)
2894 and then Is_Private_Primitive (Priv_Spec)
2895 then
2896 return Priv_Spec;
2897 end if;
2898
2899 -- In case that this is some sort of error, restore the original
2900 -- state of the body.
2901
2902 Replace_Types (To_Corresponding => True);
2903 end if;
2904
2905 return Spec_N;
2906 end Disambiguate_Spec;
2907
5dcab3ca
AC
2908 ----------------------------
2909 -- Exchange_Limited_Views --
2910 ----------------------------
2911
2912 procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is
2913 procedure Detect_And_Exchange (Id : Entity_Id);
2914 -- Determine whether Id's type denotes an incomplete type associated
2915 -- with a limited with clause and exchange the limited view with the
2916 -- non-limited one.
2917
2918 -------------------------
2919 -- Detect_And_Exchange --
2920 -------------------------
2921
2922 procedure Detect_And_Exchange (Id : Entity_Id) is
2923 Typ : constant Entity_Id := Etype (Id);
5dcab3ca 2924 begin
e23e04db 2925 if From_Limited_With (Typ) and then Has_Non_Limited_View (Typ) then
5dcab3ca
AC
2926 Set_Etype (Id, Non_Limited_View (Typ));
2927 end if;
2928 end Detect_And_Exchange;
2929
2930 -- Local variables
2931
2932 Formal : Entity_Id;
2933
2934 -- Start of processing for Exchange_Limited_Views
2935
2936 begin
2937 if No (Subp_Id) then
2938 return;
2939
2940 -- Do not process subprogram bodies as they already use the non-
2941 -- limited view of types.
2942
2943 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2944 return;
2945 end if;
2946
2947 -- Examine all formals and swap views when applicable
2948
2949 Formal := First_Formal (Subp_Id);
2950 while Present (Formal) loop
2951 Detect_And_Exchange (Formal);
2952
2953 Next_Formal (Formal);
2954 end loop;
2955
2956 -- Process the return type of a function
2957
2958 if Ekind (Subp_Id) = E_Function then
2959 Detect_And_Exchange (Subp_Id);
2960 end if;
2961 end Exchange_Limited_Views;
2962
d44202ba
HK
2963 -------------------------------------
2964 -- Is_Private_Concurrent_Primitive --
2965 -------------------------------------
2966
2967 function Is_Private_Concurrent_Primitive
2968 (Subp_Id : Entity_Id) return Boolean
2969 is
2970 Formal_Typ : Entity_Id;
2971
2972 begin
2973 if Present (First_Formal (Subp_Id)) then
2974 Formal_Typ := Etype (First_Formal (Subp_Id));
2975
2976 if Is_Concurrent_Record_Type (Formal_Typ) then
df3e68b1
HK
2977 if Is_Class_Wide_Type (Formal_Typ) then
2978 Formal_Typ := Root_Type (Formal_Typ);
2979 end if;
2980
d44202ba
HK
2981 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2982 end if;
2983
2984 -- The type of the first formal is a concurrent tagged type with
2985 -- a private view.
2986
2987 return
2988 Is_Concurrent_Type (Formal_Typ)
2989 and then Is_Tagged_Type (Formal_Typ)
2990 and then Has_Private_Declaration (Formal_Typ);
2991 end if;
2992
2993 return False;
2994 end Is_Private_Concurrent_Primitive;
2995
76a69663
ES
2996 ----------------------------
2997 -- Set_Trivial_Subprogram --
2998 ----------------------------
2999
3000 procedure Set_Trivial_Subprogram (N : Node_Id) is
3001 Nxt : constant Node_Id := Next (N);
3002
3003 begin
3004 Set_Is_Trivial_Subprogram (Body_Id);
3005
3006 if Present (Spec_Id) then
3007 Set_Is_Trivial_Subprogram (Spec_Id);
3008 end if;
3009
3010 if Present (Nxt)
3011 and then Nkind (Nxt) = N_Simple_Return_Statement
3012 and then No (Next (Nxt))
3013 and then Present (Expression (Nxt))
3014 and then Is_Entity_Name (Expression (Nxt))
3015 then
3016 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
3017 end if;
3018 end Set_Trivial_Subprogram;
3019
758c442c
GD
3020 ---------------------------------
3021 -- Verify_Overriding_Indicator --
3022 ---------------------------------
3023
3024 procedure Verify_Overriding_Indicator is
3025 begin
21d27997
RD
3026 if Must_Override (Body_Spec) then
3027 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
4b6f99f5 3028 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
21d27997
RD
3029 then
3030 null;
3031
038140ed 3032 elsif not Present (Overridden_Operation (Spec_Id)) then
ed2233dc 3033 Error_Msg_NE
21d27997 3034 ("subprogram& is not overriding", Body_Spec, Spec_Id);
23e28b42
AC
3035
3036 -- Overriding indicators aren't allowed for protected subprogram
3037 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3038 -- this to a warning if -gnatd.E is enabled.
3039
3040 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3041 Error_Msg_Warn := Error_To_Warning;
3042 Error_Msg_N
b785e0b8 3043 ("<<overriding indicator not allowed for protected "
23e28b42 3044 & "subprogram body", Body_Spec);
21d27997 3045 end if;
758c442c 3046
5d37ba92 3047 elsif Must_Not_Override (Body_Spec) then
038140ed 3048 if Present (Overridden_Operation (Spec_Id)) then
ed2233dc 3049 Error_Msg_NE
5d37ba92 3050 ("subprogram& overrides inherited operation",
76a69663 3051 Body_Spec, Spec_Id);
5d37ba92 3052
21d27997 3053 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
4b6f99f5 3054 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
21d27997 3055 then
ed2233dc 3056 Error_Msg_NE
3ccedacc 3057 ("subprogram& overrides predefined operator ",
21d27997
RD
3058 Body_Spec, Spec_Id);
3059
23e28b42
AC
3060 -- Overriding indicators aren't allowed for protected subprogram
3061 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3062 -- this to a warning if -gnatd.E is enabled.
3063
3064 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3065 Error_Msg_Warn := Error_To_Warning;
5d37ba92 3066
23e28b42 3067 Error_Msg_N
3ccedacc
AC
3068 ("<<overriding indicator not allowed "
3069 & "for protected subprogram body", Body_Spec);
23e28b42
AC
3070
3071 -- If this is not a primitive operation, then the overriding
3072 -- indicator is altogether illegal.
3073
3074 elsif not Is_Primitive (Spec_Id) then
ed2233dc 3075 Error_Msg_N
3ccedacc
AC
3076 ("overriding indicator only allowed "
3077 & "if subprogram is primitive", Body_Spec);
5d37ba92 3078 end if;
235f4375 3079
23e28b42
AC
3080 -- If checking the style rule and the operation overrides, then
3081 -- issue a warning about a missing overriding_indicator. Protected
3082 -- subprogram bodies are excluded from this style checking, since
3083 -- they aren't primitives (even though their declarations can
3084 -- override) and aren't allowed to have an overriding_indicator.
3085
806f6d37 3086 elsif Style_Check
038140ed 3087 and then Present (Overridden_Operation (Spec_Id))
23e28b42 3088 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
235f4375
AC
3089 then
3090 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3091 Style.Missing_Overriding (N, Body_Id);
806f6d37
AC
3092
3093 elsif Style_Check
3094 and then Can_Override_Operator (Spec_Id)
3095 and then not Is_Predefined_File_Name
3096 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
3097 then
3098 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3099 Style.Missing_Overriding (N, Body_Id);
758c442c
GD
3100 end if;
3101 end Verify_Overriding_Indicator;
3102
b1b543d2 3103 -- Start of processing for Analyze_Subprogram_Body_Helper
0fb2ea01 3104
996ae0b0 3105 begin
82c80734
RD
3106 -- Generic subprograms are handled separately. They always have a
3107 -- generic specification. Determine whether current scope has a
3108 -- previous declaration.
996ae0b0 3109
82c80734
RD
3110 -- If the subprogram body is defined within an instance of the same
3111 -- name, the instance appears as a package renaming, and will be hidden
3112 -- within the subprogram.
996ae0b0
RK
3113
3114 if Present (Prev_Id)
3115 and then not Is_Overloadable (Prev_Id)
3116 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3117 or else Comes_From_Source (Prev_Id))
3118 then
fbf5a39b 3119 if Is_Generic_Subprogram (Prev_Id) then
996ae0b0 3120 Spec_Id := Prev_Id;
8636f52f
HK
3121
3122 -- The corresponding spec may be subject to pragma Ghost with
3123 -- policy Ignore. Set the mode now to ensure that any nodes
3124 -- generated during analysis and expansion are properly flagged
3125 -- as ignored Ghost.
3126
3127 Set_Ghost_Mode (N, Spec_Id);
996ae0b0
RK
3128 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3129 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3130
3131 Analyze_Generic_Subprogram_Body (N, Spec_Id);
7665e4bd
AC
3132
3133 if Nkind (N) = N_Subprogram_Body then
3134 HSS := Handled_Statement_Sequence (N);
3135 Check_Missing_Return;
3136 end if;
3137
996ae0b0
RK
3138 return;
3139
3140 else
82c80734
RD
3141 -- Previous entity conflicts with subprogram name. Attempting to
3142 -- enter name will post error.
996ae0b0
RK
3143
3144 Enter_Name (Body_Id);
3145 return;
3146 end if;
3147
82c80734
RD
3148 -- Non-generic case, find the subprogram declaration, if one was seen,
3149 -- or enter new overloaded entity in the current scope. If the
3150 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3151 -- part of the context of one of its subunits. No need to redo the
3152 -- analysis.
996ae0b0 3153
8fde064e 3154 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
996ae0b0
RK
3155 return;
3156
3157 else
fbf5a39b 3158 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
996ae0b0
RK
3159
3160 if Nkind (N) = N_Subprogram_Body_Stub
3161 or else No (Corresponding_Spec (N))
3162 then
d44202ba
HK
3163 if Is_Private_Concurrent_Primitive (Body_Id) then
3164 Spec_Id := Disambiguate_Spec;
8636f52f
HK
3165
3166 -- The corresponding spec may be subject to pragma Ghost with
3167 -- policy Ignore. Set the mode now to ensure that any nodes
3168 -- generated during analysis and expansion are properly flagged
3169 -- as ignored Ghost.
3170
3171 Set_Ghost_Mode (N, Spec_Id);
3172
d44202ba
HK
3173 else
3174 Spec_Id := Find_Corresponding_Spec (N);
b6c8e5be 3175
8636f52f
HK
3176 -- The corresponding spec may be subject to pragma Ghost with
3177 -- policy Ignore. Set the mode now to ensure that any nodes
3178 -- generated during analysis and expansion are properly flagged
3179 -- as ignored Ghost.
3180
3181 Set_Ghost_Mode (N, Spec_Id);
3182
b6c8e5be
AC
3183 -- In GNATprove mode, if the body has no previous spec, create
3184 -- one so that the inlining machinery can operate properly.
3185 -- Transfer aspects, if any, to the new spec, so that they
3186 -- are legal and can be processed ahead of the body.
3187 -- We make two copies of the given spec, one for the new
3188 -- declaration, and one for the body.
3189
5a271a7f 3190 if No (Spec_Id) and then GNATprove_Mode
480156b2 3191
480156b2
AC
3192 -- Inlining does not apply during pre-analysis of code
3193
b6c8e5be 3194 and then Full_Analysis
480156b2
AC
3195
3196 -- Inlining only applies to full bodies, not stubs
3197
7b2888e6 3198 and then Nkind (N) /= N_Subprogram_Body_Stub
480156b2
AC
3199
3200 -- Inlining only applies to bodies in the source code, not to
3201 -- those generated by the compiler. In particular, expression
3202 -- functions, whose body is generated by the compiler, are
3203 -- treated specially by GNATprove.
3204
b6c8e5be 3205 and then Comes_From_Source (Body_Id)
480156b2
AC
3206
3207 -- This cannot be done for a compilation unit, which is not
3208 -- in a context where we can insert a new spec.
3209
b6c8e5be 3210 and then Is_List_Member (N)
480156b2
AC
3211
3212 -- Inlining only applies to subprograms without contracts,
3213 -- as a contract is a sign that GNATprove should perform a
3214 -- modular analysis of the subprogram instead of a contextual
3215 -- analysis at each call site. The same test is performed in
3216 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3217 -- here in another form (because the contract has not
3218 -- been attached to the body) to avoid frontend errors in
3219 -- case pragmas are used instead of aspects, because the
3220 -- corresponding pragmas in the body would not be transferred
3221 -- to the spec, leading to legality errors.
3222
ac072cb2 3223 and then not Body_Has_Contract
b6c8e5be
AC
3224 then
3225 declare
3226 Body_Spec : constant Node_Id :=
662c2ad4 3227 Copy_Separate_Tree (Specification (N));
eb1ee757
AC
3228 New_Decl : constant Node_Id :=
3229 Make_Subprogram_Declaration (Loc,
3230 Copy_Separate_Tree (Specification (N)));
3231
2178830b 3232 SPARK_Mode_Aspect : Node_Id;
eb1ee757
AC
3233 Aspects : List_Id;
3234 Prag, Aspect : Node_Id;
b6c8e5be
AC
3235
3236 begin
3237 Insert_Before (N, New_Decl);
3238 Move_Aspects (From => N, To => New_Decl);
2178830b
AC
3239
3240 -- Mark the newly moved aspects as not analyzed, so that
3241 -- their effect on New_Decl is properly analyzed.
3242
3243 Aspect := First (Aspect_Specifications (New_Decl));
3244 while Present (Aspect) loop
3245 Set_Analyzed (Aspect, False);
3246 Next (Aspect);
3247 end loop;
3248
b6c8e5be 3249 Analyze (New_Decl);
2178830b
AC
3250
3251 -- The analysis of the generated subprogram declaration
eb1ee757 3252 -- may have introduced pragmas that need to be analyzed.
2178830b
AC
3253
3254 Prag := Next (New_Decl);
3255 while Prag /= N loop
3256 Analyze (Prag);
3257 Next (Prag);
3258 end loop;
3259
b6c8e5be
AC
3260 Spec_Id := Defining_Entity (New_Decl);
3261
793c5f05
AC
3262 -- As Body_Id originally comes from source, mark the new
3263 -- Spec_Id as such, which is required so that calls to
3264 -- this subprogram are registered in the local effects
3265 -- stored in ALI files for GNATprove.
3266
3267 Set_Comes_From_Source (Spec_Id, True);
3268
2178830b
AC
3269 -- If aspect SPARK_Mode was specified on the body, it
3270 -- needs to be repeated on the generated decl and the
3271 -- body. Since the original aspect was moved to the
3272 -- generated decl, copy it for the body.
3273
3274 if Has_Aspect (Spec_Id, Aspect_SPARK_Mode) then
3275 SPARK_Mode_Aspect :=
3276 New_Copy (Find_Aspect (Spec_Id, Aspect_SPARK_Mode));
3277 Set_Analyzed (SPARK_Mode_Aspect, False);
eb1ee757 3278 Aspects := New_List (SPARK_Mode_Aspect);
2178830b
AC
3279 Set_Aspect_Specifications (N, Aspects);
3280 end if;
3281
b6c8e5be
AC
3282 Set_Specification (N, Body_Spec);
3283 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3284 Set_Corresponding_Spec (N, Spec_Id);
3285 end;
3286 end if;
d44202ba 3287 end if;
996ae0b0
RK
3288
3289 -- If this is a duplicate body, no point in analyzing it
3290
3291 if Error_Posted (N) then
3292 return;
3293 end if;
3294
82c80734
RD
3295 -- A subprogram body should cause freezing of its own declaration,
3296 -- but if there was no previous explicit declaration, then the
3297 -- subprogram will get frozen too late (there may be code within
3298 -- the body that depends on the subprogram having been frozen,
3299 -- such as uses of extra formals), so we force it to be frozen
76a69663 3300 -- here. Same holds if the body and spec are compilation units.
cd1c668b
ES
3301 -- Finally, if the return type is an anonymous access to protected
3302 -- subprogram, it must be frozen before the body because its
3303 -- expansion has generated an equivalent type that is used when
3304 -- elaborating the body.
996ae0b0 3305
885c4871 3306 -- An exception in the case of Ada 2012, AI05-177: The bodies
ebb6faaa
AC
3307 -- created for expression functions do not freeze.
3308
3309 if No (Spec_Id)
3310 and then Nkind (Original_Node (N)) /= N_Expression_Function
3311 then
996ae0b0
RK
3312 Freeze_Before (N, Body_Id);
3313
3314 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3315 Freeze_Before (N, Spec_Id);
cd1c668b
ES
3316
3317 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3318 Freeze_Before (N, Etype (Body_Id));
996ae0b0 3319 end if;
a38ff9b1 3320
996ae0b0
RK
3321 else
3322 Spec_Id := Corresponding_Spec (N);
8636f52f
HK
3323
3324 -- The corresponding spec may be subject to pragma Ghost with
3325 -- policy Ignore. Set the mode now to ensure that any nodes
3326 -- generated during analysis and expansion are properly flagged
3327 -- as ignored Ghost.
3328
3329 Set_Ghost_Mode (N, Spec_Id);
996ae0b0
RK
3330 end if;
3331 end if;
3332
799d0e05
AC
3333 -- Previously we scanned the body to look for nested subprograms, and
3334 -- rejected an inline directive if nested subprograms were present,
3335 -- because the back-end would generate conflicting symbols for the
c8957aae 3336 -- nested bodies. This is now unnecessary.
07fc65c4 3337
c8957aae 3338 -- Look ahead to recognize a pragma Inline that appears after the body
84f4072a 3339
e660dbf7
JM
3340 Check_Inline_Pragma (Spec_Id);
3341
701b7fbb
RD
3342 -- Deal with special case of a fully private operation in the body of
3343 -- the protected type. We must create a declaration for the subprogram,
3344 -- in order to attach the protected subprogram that will be used in
3345 -- internal calls. We exclude compiler generated bodies from the
3346 -- expander since the issue does not arise for those cases.
07fc65c4 3347
996ae0b0
RK
3348 if No (Spec_Id)
3349 and then Comes_From_Source (N)
3350 and then Is_Protected_Type (Current_Scope)
3351 then
47bfea3a 3352 Spec_Id := Build_Private_Protected_Declaration (N);
701b7fbb 3353 end if;
996ae0b0 3354
5334d18f 3355 -- If a separate spec is present, then deal with freezing issues
7ca78bba 3356
701b7fbb 3357 if Present (Spec_Id) then
996ae0b0 3358 Spec_Decl := Unit_Declaration_Node (Spec_Id);
758c442c 3359 Verify_Overriding_Indicator;
5d37ba92
ES
3360
3361 -- In general, the spec will be frozen when we start analyzing the
3362 -- body. However, for internally generated operations, such as
3363 -- wrapper functions for inherited operations with controlling
164e06c6
AC
3364 -- results, the spec may not have been frozen by the time we expand
3365 -- the freeze actions that include the bodies. In particular, extra
3366 -- formals for accessibility or for return-in-place may need to be
3367 -- generated. Freeze nodes, if any, are inserted before the current
3368 -- body. These freeze actions are also needed in ASIS mode to enable
3369 -- the proper back-annotations.
5d37ba92
ES
3370
3371 if not Is_Frozen (Spec_Id)
7134062a 3372 and then (Expander_Active or ASIS_Mode)
5d37ba92
ES
3373 then
3374 -- Force the generation of its freezing node to ensure proper
3375 -- management of access types in the backend.
3376
3377 -- This is definitely needed for some cases, but it is not clear
3378 -- why, to be investigated further???
3379
3380 Set_Has_Delayed_Freeze (Spec_Id);
6b958cec 3381 Freeze_Before (N, Spec_Id);
5d37ba92 3382 end if;
996ae0b0
RK
3383 end if;
3384
3385 -- Place subprogram on scope stack, and make formals visible. If there
3386 -- is a spec, the visible entity remains that of the spec.
3387
3388 if Present (Spec_Id) then
07fc65c4 3389 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
758c442c
GD
3390
3391 if Is_Child_Unit (Spec_Id) then
3392 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3393 end if;
3394
fbf5a39b
AC
3395 if Style_Check then
3396 Style.Check_Identifier (Body_Id, Spec_Id);
3397 end if;
996ae0b0
RK
3398
3399 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3400 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3401
f937473f 3402 if Is_Abstract_Subprogram (Spec_Id) then
ed2233dc 3403 Error_Msg_N ("an abstract subprogram cannot have a body", N);
996ae0b0 3404 return;
21d27997 3405
996ae0b0
RK
3406 else
3407 Set_Convention (Body_Id, Convention (Spec_Id));
3408 Set_Has_Completion (Spec_Id);
3409
c5cec2fe
AC
3410 -- Inherit the "ghostness" of the subprogram spec. Note that this
3411 -- property is not directly inherited as the body may be subject
3412 -- to a different Ghost assertion policy.
3413
8636f52f 3414 if Is_Ghost_Entity (Spec_Id) or else Ghost_Mode > None then
c5cec2fe
AC
3415 Set_Is_Ghost_Entity (Body_Id);
3416
3417 -- The Ghost policy in effect at the point of declaration and
3c756b76 3418 -- at the point of completion must match (SPARK RM 6.9(15)).
c5cec2fe
AC
3419
3420 Check_Ghost_Completion (Spec_Id, Body_Id);
3421 end if;
3422
996ae0b0 3423 if Is_Protected_Type (Scope (Spec_Id)) then
21d27997 3424 Prot_Typ := Scope (Spec_Id);
996ae0b0
RK
3425 end if;
3426
3427 -- If this is a body generated for a renaming, do not check for
3428 -- full conformance. The check is redundant, because the spec of
3429 -- the body is a copy of the spec in the renaming declaration,
3430 -- and the test can lead to spurious errors on nested defaults.
3431
3432 if Present (Spec_Decl)
996ae0b0 3433 and then not Comes_From_Source (N)
93a81b02
GB
3434 and then
3435 (Nkind (Original_Node (Spec_Decl)) =
4b6f99f5 3436 N_Subprogram_Renaming_Declaration
466c2127
AC
3437 or else (Present (Corresponding_Body (Spec_Decl))
3438 and then
3439 Nkind (Unit_Declaration_Node
3440 (Corresponding_Body (Spec_Decl))) =
3441 N_Subprogram_Renaming_Declaration))
996ae0b0
RK
3442 then
3443 Conformant := True;
cabe9abc
AC
3444
3445 -- Conversely, the spec may have been generated for specless body
3446 -- with an inline pragma.
3447
3448 elsif Comes_From_Source (N)
3449 and then not Comes_From_Source (Spec_Id)
3450 and then Has_Pragma_Inline (Spec_Id)
3451 then
3452 Conformant := True;
76a69663 3453
996ae0b0
RK
3454 else
3455 Check_Conformance
3456 (Body_Id, Spec_Id,
76a69663 3457 Fully_Conformant, True, Conformant, Body_Id);
996ae0b0
RK
3458 end if;
3459
3460 -- If the body is not fully conformant, we have to decide if we
3461 -- should analyze it or not. If it has a really messed up profile
3462 -- then we probably should not analyze it, since we will get too
3463 -- many bogus messages.
3464
3465 -- Our decision is to go ahead in the non-fully conformant case
3466 -- only if it is at least mode conformant with the spec. Note
3467 -- that the call to Check_Fully_Conformant has issued the proper
3468 -- error messages to complain about the lack of conformance.
3469
3470 if not Conformant
3471 and then not Mode_Conformant (Body_Id, Spec_Id)
3472 then
3473 return;
3474 end if;
3475 end if;
3476
996ae0b0 3477 if Spec_Id /= Body_Id then
fbf5a39b 3478 Reference_Body_Formals (Spec_Id, Body_Id);
996ae0b0
RK
3479 end if;
3480
579847c2
AC
3481 Set_Ekind (Body_Id, E_Subprogram_Body);
3482
e28072cd
AC
3483 if Nkind (N) = N_Subprogram_Body_Stub then
3484 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
3485
3486 -- Regular body
3487
3488 else
996ae0b0 3489 Set_Corresponding_Spec (N, Spec_Id);
758c442c 3490
5d37ba92
ES
3491 -- Ada 2005 (AI-345): If the operation is a primitive operation
3492 -- of a concurrent type, the type of the first parameter has been
3493 -- replaced with the corresponding record, which is the proper
3494 -- run-time structure to use. However, within the body there may
3495 -- be uses of the formals that depend on primitive operations
3496 -- of the type (in particular calls in prefixed form) for which
3497 -- we need the original concurrent type. The operation may have
3498 -- several controlling formals, so the replacement must be done
3499 -- for all of them.
758c442c
GD
3500
3501 if Comes_From_Source (Spec_Id)
3502 and then Present (First_Entity (Spec_Id))
3503 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
3504 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
15918371
AC
3505 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
3506 and then Present (Corresponding_Concurrent_Type
3507 (Etype (First_Entity (Spec_Id))))
758c442c 3508 then
5d37ba92
ES
3509 declare
3510 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
3511 Form : Entity_Id;
3512
3513 begin
3514 Form := First_Formal (Spec_Id);
3515 while Present (Form) loop
3516 if Etype (Form) = Typ then
3517 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
3518 end if;
3519
3520 Next_Formal (Form);
3521 end loop;
3522 end;
758c442c
GD
3523 end if;
3524
21d27997
RD
3525 -- Make the formals visible, and place subprogram on scope stack.
3526 -- This is also the point at which we set Last_Real_Spec_Entity
3527 -- to mark the entities which will not be moved to the body.
758c442c 3528
996ae0b0 3529 Install_Formals (Spec_Id);
21d27997 3530 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
616547fa
AC
3531
3532 -- Within an instance, add local renaming declarations so that
a5a809b2
AC
3533 -- gdb can retrieve the values of actuals more easily. This is
3534 -- only relevant if generating code (and indeed we definitely
3535 -- do not want these definitions -gnatc mode, because that would
3536 -- confuse ASIS).
616547fa
AC
3537
3538 if Is_Generic_Instance (Spec_Id)
3539 and then Is_Wrapper_Package (Current_Scope)
a5a809b2 3540 and then Expander_Active
616547fa
AC
3541 then
3542 Build_Subprogram_Instance_Renamings (N, Current_Scope);
3543 end if;
3544
0a36105d 3545 Push_Scope (Spec_Id);
996ae0b0
RK
3546
3547 -- Make sure that the subprogram is immediately visible. For
3548 -- child units that have no separate spec this is indispensable.
3549 -- Otherwise it is safe albeit redundant.
3550
3551 Set_Is_Immediately_Visible (Spec_Id);
3552 end if;
3553
3554 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
ec4867fa 3555 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
c9d70ab1 3556 Set_Scope (Body_Id, Scope (Spec_Id));
996ae0b0
RK
3557
3558 -- Case of subprogram body with no previous spec
3559
3560 else
3e5daac4
AC
3561 -- Check for style warning required
3562
996ae0b0 3563 if Style_Check
3e5daac4
AC
3564
3565 -- Only apply check for source level subprograms for which checks
3566 -- have not been suppressed.
3567
996ae0b0
RK
3568 and then Comes_From_Source (Body_Id)
3569 and then not Suppress_Style_Checks (Body_Id)
3e5daac4
AC
3570
3571 -- No warnings within an instance
3572
996ae0b0 3573 and then not In_Instance
3e5daac4 3574
b0186f71 3575 -- No warnings for expression functions
3e5daac4 3576
b0186f71 3577 and then Nkind (Original_Node (N)) /= N_Expression_Function
996ae0b0
RK
3578 then
3579 Style.Body_With_No_Spec (N);
3580 end if;
3581
3582 New_Overloaded_Entity (Body_Id);
3583
3584 if Nkind (N) /= N_Subprogram_Body_Stub then
3585 Set_Acts_As_Spec (N);
3586 Generate_Definition (Body_Id);
fbf5a39b
AC
3587 Generate_Reference
3588 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
996ae0b0 3589 Install_Formals (Body_Id);
e949ee22 3590
4a854847 3591 Push_Scope (Body_Id);
996ae0b0 3592 end if;
dbe36d67
AC
3593
3594 -- For stubs and bodies with no previous spec, generate references to
3595 -- formals.
3596
3597 Generate_Reference_To_Formals (Body_Id);
996ae0b0
RK
3598 end if;
3599
5216b599
AC
3600 -- Set SPARK_Mode from context
3601
3602 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
3603 Set_SPARK_Pragma_Inherited (Body_Id, True);
3604
76a69663
ES
3605 -- If the return type is an anonymous access type whose designated type
3606 -- is the limited view of a class-wide type and the non-limited view is
3607 -- available, update the return type accordingly.
ec4867fa 3608
8fde064e 3609 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
ec4867fa 3610 declare
ec4867fa 3611 Etyp : Entity_Id;
0a36105d 3612 Rtyp : Entity_Id;
ec4867fa
ES
3613
3614 begin
0a36105d
JM
3615 Rtyp := Etype (Current_Scope);
3616
3617 if Ekind (Rtyp) = E_Anonymous_Access_Type then
3618 Etyp := Directly_Designated_Type (Rtyp);
3619
7b56a91b
AC
3620 if Is_Class_Wide_Type (Etyp)
3621 and then From_Limited_With (Etyp)
3622 then
0a36105d
JM
3623 Set_Directly_Designated_Type
3624 (Etype (Current_Scope), Available_View (Etyp));
3625 end if;
3626 end if;
ec4867fa
ES
3627 end;
3628 end if;
3629
996ae0b0
RK
3630 -- If this is the proper body of a stub, we must verify that the stub
3631 -- conforms to the body, and to the previous spec if one was present.
dbe36d67 3632 -- We know already that the body conforms to that spec. This test is
996ae0b0
RK
3633 -- only required for subprograms that come from source.
3634
3635 if Nkind (Parent (N)) = N_Subunit
3636 and then Comes_From_Source (N)
3637 and then not Error_Posted (Body_Id)
e895b435
ES
3638 and then Nkind (Corresponding_Stub (Parent (N))) =
3639 N_Subprogram_Body_Stub
996ae0b0
RK
3640 then
3641 declare
fbf5a39b
AC
3642 Old_Id : constant Entity_Id :=
3643 Defining_Entity
3644 (Specification (Corresponding_Stub (Parent (N))));
3645
996ae0b0 3646 Conformant : Boolean := False;
996ae0b0
RK
3647
3648 begin
3649 if No (Spec_Id) then
3650 Check_Fully_Conformant (Body_Id, Old_Id);
3651
3652 else
3653 Check_Conformance
3654 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3655
3656 if not Conformant then
3657
dbe36d67
AC
3658 -- The stub was taken to be a new declaration. Indicate that
3659 -- it lacks a body.
996ae0b0
RK
3660
3661 Set_Has_Completion (Old_Id, False);
3662 end if;
3663 end if;
3664 end;
3665 end if;
3666
3667 Set_Has_Completion (Body_Id);
3668 Check_Eliminated (Body_Id);
3669
3670 if Nkind (N) = N_Subprogram_Body_Stub then
c8a3028c
AC
3671
3672 -- Analyze any aspect specifications that appear on the subprogram
3673 -- body stub.
3674
3675 if Has_Aspects (N) then
3676 Analyze_Aspects_On_Body_Or_Stub;
3677 end if;
3678
3679 -- Stop the analysis now as the stub cannot be inlined, plus it does
3680 -- not have declarative or statement lists.
3681
996ae0b0 3682 return;
84f4072a 3683 end if;
996ae0b0 3684
b94b6c56 3685 -- Handle frontend inlining
84f4072a 3686
b94b6c56
RD
3687 -- Note: Normally we don't do any inlining if expansion is off, since
3688 -- we won't generate code in any case. An exception arises in GNATprove
2d180af1
YM
3689 -- mode where we want to expand some calls in place, even with expansion
3690 -- disabled, since the inlining eases formal verification.
ecad37f3 3691
6c26bac2
AC
3692 if not GNATprove_Mode
3693 and then Expander_Active
3694 and then Serious_Errors_Detected = 0
3695 and then Present (Spec_Id)
3696 and then Has_Pragma_Inline (Spec_Id)
3697 then
3698 -- Legacy implementation (relying on frontend inlining)
84f4072a 3699
6c26bac2 3700 if not Back_End_Inlining then
17ce1f52
AC
3701 if (Has_Pragma_Inline_Always (Spec_Id)
3702 and then not Opt.Disable_FE_Inline_Always)
3703 or else
3704 (Has_Pragma_Inline (Spec_Id) and then Front_End_Inlining
3705 and then not Opt.Disable_FE_Inline)
6c26bac2
AC
3706 then
3707 Build_Body_To_Inline (N, Spec_Id);
3708 end if;
f087ea44 3709
88f7d2d1 3710 -- New implementation (relying on backend inlining)
f087ea44 3711
6c26bac2
AC
3712 else
3713 if Has_Pragma_Inline_Always (Spec_Id)
3714 or else Optimization_Level > 0
3715 then
3716 -- Handle function returning an unconstrained type
f087ea44 3717
6c26bac2
AC
3718 if Comes_From_Source (Body_Id)
3719 and then Ekind (Spec_Id) = E_Function
3720 and then Returns_Unconstrained_Type (Spec_Id)
5c5e108f
AC
3721
3722 -- If function builds in place, i.e. returns a limited type,
3723 -- inlining cannot be done.
3724
3725 and then not Is_Limited_Type (Etype (Spec_Id))
6c26bac2 3726 then
16b10ccc 3727 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
84f4072a 3728
6c26bac2
AC
3729 else
3730 declare
b80a2b4b
AC
3731 Subp_Body : constant Node_Id :=
3732 Unit_Declaration_Node (Body_Id);
6c26bac2 3733 Subp_Decl : constant List_Id := Declarations (Subp_Body);
84f4072a 3734
6c26bac2
AC
3735 begin
3736 -- Do not pass inlining to the backend if the subprogram
3737 -- has declarations or statements which cannot be inlined
3738 -- by the backend. This check is done here to emit an
3739 -- error instead of the generic warning message reported
3740 -- by the GCC backend (ie. "function might not be
3741 -- inlinable").
3742
3743 if Present (Subp_Decl)
3744 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
3745 then
3746 null;
3747
3748 elsif Has_Excluded_Statement
3749 (Spec_Id,
3750 Statements
3751 (Handled_Statement_Sequence (Subp_Body)))
3752 then
3753 null;
3754
3755 -- If the backend inlining is available then at this
3756 -- stage we only have to mark the subprogram as inlined.
3757 -- The expander will take care of registering it in the
3758 -- table of subprograms inlined by the backend a part of
3759 -- processing calls to it (cf. Expand_Call)
3760
3761 else
3762 Set_Is_Inlined (Spec_Id);
3763 end if;
3764 end;
3765 end if;
3766 end if;
3767 end if;
2d180af1
YM
3768
3769 -- In GNATprove mode, inline only when there is a separate subprogram
3770 -- declaration for now, as inlining of subprogram bodies acting as
3771 -- declarations, or subprogram stubs, are not supported by frontend
3772 -- inlining. This inlining should occur after analysis of the body, so
3773 -- that it is known whether the value of SPARK_Mode applicable to the
3774 -- body, which can be defined by a pragma inside the body.
3775
3776 elsif GNATprove_Mode
2d180af1
YM
3777 and then Full_Analysis
3778 and then not Inside_A_Generic
3779 and then Present (Spec_Id)
3780 and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration
3781 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
ac072cb2 3782 and then not Body_Has_Contract
2d180af1 3783 then
6c26bac2 3784 Build_Body_To_Inline (N, Spec_Id);
996ae0b0
RK
3785 end if;
3786
0ab80019 3787 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
dbe36d67 3788 -- of the specification we have to install the private withed units.
21d27997 3789 -- This holds for child units as well.
9bc856dd
AC
3790
3791 if Is_Compilation_Unit (Body_Id)
21d27997 3792 or else Nkind (Parent (N)) = N_Compilation_Unit
9bc856dd
AC
3793 then
3794 Install_Private_With_Clauses (Body_Id);
3795 end if;
3796
ec4867fa
ES
3797 Check_Anonymous_Return;
3798
fdce4bb7
JM
3799 -- Set the Protected_Formal field of each extra formal of the protected
3800 -- subprogram to reference the corresponding extra formal of the
3801 -- subprogram that implements it. For regular formals this occurs when
3802 -- the protected subprogram's declaration is expanded, but the extra
3803 -- formals don't get created until the subprogram is frozen. We need to
3804 -- do this before analyzing the protected subprogram's body so that any
3805 -- references to the original subprogram's extra formals will be changed
3806 -- refer to the implementing subprogram's formals (see Expand_Formal).
3807
3808 if Present (Spec_Id)
3809 and then Is_Protected_Type (Scope (Spec_Id))
3810 and then Present (Protected_Body_Subprogram (Spec_Id))
3811 then
3812 declare
3813 Impl_Subp : constant Entity_Id :=
3814 Protected_Body_Subprogram (Spec_Id);
3815 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3816 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
fdce4bb7
JM
3817 begin
3818 while Present (Prot_Ext_Formal) loop
3819 pragma Assert (Present (Impl_Ext_Formal));
fdce4bb7 3820 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
fdce4bb7
JM
3821 Next_Formal_With_Extras (Prot_Ext_Formal);
3822 Next_Formal_With_Extras (Impl_Ext_Formal);
3823 end loop;
3824 end;
3825 end if;
3826
0868e09c 3827 -- Now we can go on to analyze the body
996ae0b0
RK
3828
3829 HSS := Handled_Statement_Sequence (N);
3830 Set_Actual_Subtypes (N, Current_Scope);
21d27997 3831
f3d0f304 3832 -- Add a declaration for the Protection object, renaming declarations
21d27997
RD
3833 -- for discriminals and privals and finally a declaration for the entry
3834 -- family index (if applicable). This form of early expansion is done
3835 -- when the Expander is active because Install_Private_Data_Declarations
81bf2382 3836 -- references entities which were created during regular expansion. The
3b8056a5
AC
3837 -- subprogram entity must come from source, and not be an internally
3838 -- generated subprogram.
21d27997 3839
4460a9bc 3840 if Expander_Active
21d27997
RD
3841 and then Present (Prot_Typ)
3842 and then Present (Spec_Id)
3b8056a5 3843 and then Comes_From_Source (Spec_Id)
21d27997
RD
3844 and then not Is_Eliminated (Spec_Id)
3845 then
3846 Install_Private_Data_Declarations
3847 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3848 end if;
3849
5dcab3ca
AC
3850 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3851 -- may now appear in parameter and result profiles. Since the analysis
3852 -- of a subprogram body may use the parameter and result profile of the
3853 -- spec, swap any limited views with their non-limited counterpart.
3854
3855 if Ada_Version >= Ada_2012 then
3856 Exchange_Limited_Views (Spec_Id);
3857 end if;
3858
c8a3028c
AC
3859 -- Analyze any aspect specifications that appear on the subprogram body
3860
3861 if Has_Aspects (N) then
3862 Analyze_Aspects_On_Body_Or_Stub;
3863 end if;
3864
996ae0b0 3865 Analyze_Declarations (Declarations (N));
21d27997 3866
f3124d8f 3867 -- Verify that the SPARK_Mode of the body agrees with that of its spec
f90d14ac 3868
f1c7be38 3869 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
f90d14ac
AC
3870 if Present (SPARK_Pragma (Spec_Id)) then
3871 if Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Spec_Id)) = Off
3872 and then
3873 Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Body_Id)) = On
3874 then
3875 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3876 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
3877 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
3878 Error_Msg_NE
f3124d8f 3879 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
f90d14ac
AC
3880 end if;
3881
3882 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
3883 null;
3884
3885 else
3886 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3ccedacc 3887 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
f90d14ac 3888 Error_Msg_Sloc := Sloc (Spec_Id);
f3124d8f
HK
3889 Error_Msg_NE
3890 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
f90d14ac
AC
3891 end if;
3892 end if;
3893
c9d70ab1
AC
3894 -- When a subprogram body appears inside a package, its contract is
3895 -- analyzed at the end of the package body declarations. This is due
3896 -- to the delay with respect of the package contract upon which the
3897 -- body contract may depend. When the subprogram body is stand alone
3898 -- and acts as a compilation unit, this delay is not necessary.
3899
3900 if Nkind (Parent (N)) = N_Compilation_Unit then
3901 Analyze_Subprogram_Body_Contract (Body_Id);
3902 end if;
3903
3904 -- Deal with preconditions, [refined] postconditions, Contract_Cases,
3905 -- invariants and predicates associated with body and its spec. Since
3906 -- there is no routine Expand_Declarations which would otherwise deal
3907 -- with the contract expansion, generate all necessary mechanisms to
3908 -- verify the contract assertions now.
3909
3910 Expand_Subprogram_Contract (N);
3911
ac43e11e
AC
3912 -- If SPARK_Mode for body is not On, disable frontend inlining for this
3913 -- subprogram in GNATprove mode, as its body should not be analyzed.
3914
3915 if SPARK_Mode /= On
3916 and then GNATprove_Mode
ac43e11e
AC
3917 and then Present (Spec_Id)
3918 and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration
3919 then
3920 Set_Body_To_Inline (Parent (Parent (Spec_Id)), Empty);
4bd4bb7f 3921 Set_Is_Inlined_Always (Spec_Id, False);
ac43e11e
AC
3922 end if;
3923
21d27997
RD
3924 -- Check completion, and analyze the statements
3925
996ae0b0 3926 Check_Completion;
33931112 3927 Inspect_Deferred_Constant_Completion (Declarations (N));
996ae0b0 3928 Analyze (HSS);
21d27997
RD
3929
3930 -- Deal with end of scope processing for the body
3931
07fc65c4 3932 Process_End_Label (HSS, 't', Current_Scope);
996ae0b0
RK
3933 End_Scope;
3934 Check_Subprogram_Order (N);
c37bb106 3935 Set_Analyzed (Body_Id);
996ae0b0
RK
3936
3937 -- If we have a separate spec, then the analysis of the declarations
3938 -- caused the entities in the body to be chained to the spec id, but
3939 -- we want them chained to the body id. Only the formal parameters
3940 -- end up chained to the spec id in this case.
3941
3942 if Present (Spec_Id) then
3943
d39d6bb8 3944 -- We must conform to the categorization of our spec
996ae0b0 3945
d39d6bb8 3946 Validate_Categorization_Dependency (N, Spec_Id);
996ae0b0 3947
d39d6bb8
RD
3948 -- And if this is a child unit, the parent units must conform
3949
3950 if Is_Child_Unit (Spec_Id) then
996ae0b0
RK
3951 Validate_Categorization_Dependency
3952 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3953 end if;
3954
21d27997
RD
3955 -- Here is where we move entities from the spec to the body
3956
3957 -- Case where there are entities that stay with the spec
3958
3959 if Present (Last_Real_Spec_Entity) then
3960
dbe36d67
AC
3961 -- No body entities (happens when the only real spec entities come
3962 -- from precondition and postcondition pragmas).
21d27997
RD
3963
3964 if No (Last_Entity (Body_Id)) then
3965 Set_First_Entity
3966 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
3967
3968 -- Body entities present (formals), so chain stuff past them
3969
3970 else
3971 Set_Next_Entity
3972 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
3973 end if;
3974
3975 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
996ae0b0 3976 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
21d27997
RD
3977 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
3978
dbe36d67
AC
3979 -- Case where there are no spec entities, in this case there can be
3980 -- no body entities either, so just move everything.
996ae0b0 3981
a921e83c
AC
3982 -- If the body is generated for an expression function, it may have
3983 -- been preanalyzed already, if 'access was applied to it.
3984
996ae0b0 3985 else
a921e83c
AC
3986 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
3987 N_Expression_Function
3988 then
3989 pragma Assert (No (Last_Entity (Body_Id)));
3990 null;
3991 end if;
3992
996ae0b0
RK
3993 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
3994 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3995 Set_First_Entity (Spec_Id, Empty);
3996 Set_Last_Entity (Spec_Id, Empty);
3997 end if;
3998 end if;
3999
7665e4bd 4000 Check_Missing_Return;
996ae0b0 4001
82c80734 4002 -- Now we are going to check for variables that are never modified in
76a69663
ES
4003 -- the body of the procedure. But first we deal with a special case
4004 -- where we want to modify this check. If the body of the subprogram
4005 -- starts with a raise statement or its equivalent, or if the body
cf3e6845
AC
4006 -- consists entirely of a null statement, then it is pretty obvious that
4007 -- it is OK to not reference the parameters. For example, this might be
4008 -- the following common idiom for a stubbed function: statement of the
4009 -- procedure raises an exception. In particular this deals with the
4010 -- common idiom of a stubbed function, which appears something like:
fbf5a39b
AC
4011
4012 -- function F (A : Integer) return Some_Type;
4013 -- X : Some_Type;
4014 -- begin
4015 -- raise Program_Error;
4016 -- return X;
4017 -- end F;
4018
76a69663
ES
4019 -- Here the purpose of X is simply to satisfy the annoying requirement
4020 -- in Ada that there be at least one return, and we certainly do not
a90bd866 4021 -- want to go posting warnings on X that it is not initialized. On
76a69663
ES
4022 -- the other hand, if X is entirely unreferenced that should still
4023 -- get a warning.
4024
4025 -- What we do is to detect these cases, and if we find them, flag the
4026 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
4027 -- suppress unwanted warnings. For the case of the function stub above
4028 -- we have a special test to set X as apparently assigned to suppress
4029 -- the warning.
996ae0b0
RK
4030
4031 declare
800621e0 4032 Stm : Node_Id;
996ae0b0
RK
4033
4034 begin
0a36105d
JM
4035 -- Skip initial labels (for one thing this occurs when we are in
4036 -- front end ZCX mode, but in any case it is irrelevant), and also
4037 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
fbf5a39b 4038
800621e0 4039 Stm := First (Statements (HSS));
0a36105d
JM
4040 while Nkind (Stm) = N_Label
4041 or else Nkind (Stm) in N_Push_xxx_Label
4042 loop
996ae0b0 4043 Next (Stm);
0a36105d 4044 end loop;
996ae0b0 4045
fbf5a39b
AC
4046 -- Do the test on the original statement before expansion
4047
4048 declare
4049 Ostm : constant Node_Id := Original_Node (Stm);
4050
4051 begin
76a69663 4052 -- If explicit raise statement, turn on flag
fbf5a39b
AC
4053
4054 if Nkind (Ostm) = N_Raise_Statement then
76a69663
ES
4055 Set_Trivial_Subprogram (Stm);
4056
f3d57416 4057 -- If null statement, and no following statements, turn on flag
76a69663
ES
4058
4059 elsif Nkind (Stm) = N_Null_Statement
4060 and then Comes_From_Source (Stm)
4061 and then No (Next (Stm))
4062 then
4063 Set_Trivial_Subprogram (Stm);
fbf5a39b
AC
4064
4065 -- Check for explicit call cases which likely raise an exception
4066
4067 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
4068 if Is_Entity_Name (Name (Ostm)) then
4069 declare
4070 Ent : constant Entity_Id := Entity (Name (Ostm));
4071
4072 begin
4073 -- If the procedure is marked No_Return, then likely it
4074 -- raises an exception, but in any case it is not coming
76a69663 4075 -- back here, so turn on the flag.
fbf5a39b 4076
f46faa08
AC
4077 if Present (Ent)
4078 and then Ekind (Ent) = E_Procedure
fbf5a39b
AC
4079 and then No_Return (Ent)
4080 then
76a69663 4081 Set_Trivial_Subprogram (Stm);
fbf5a39b
AC
4082 end if;
4083 end;
4084 end if;
4085 end if;
4086 end;
996ae0b0
RK
4087 end;
4088
4089 -- Check for variables that are never modified
4090
4091 declare
4092 E1, E2 : Entity_Id;
4093
4094 begin
fbf5a39b 4095 -- If there is a separate spec, then transfer Never_Set_In_Source
996ae0b0
RK
4096 -- flags from out parameters to the corresponding entities in the
4097 -- body. The reason we do that is we want to post error flags on
4098 -- the body entities, not the spec entities.
4099
4100 if Present (Spec_Id) then
4101 E1 := First_Entity (Spec_Id);
996ae0b0
RK
4102 while Present (E1) loop
4103 if Ekind (E1) = E_Out_Parameter then
4104 E2 := First_Entity (Body_Id);
fbf5a39b 4105 while Present (E2) loop
996ae0b0
RK
4106 exit when Chars (E1) = Chars (E2);
4107 Next_Entity (E2);
4108 end loop;
4109
fbf5a39b
AC
4110 if Present (E2) then
4111 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
4112 end if;
996ae0b0
RK
4113 end if;
4114
4115 Next_Entity (E1);
4116 end loop;
4117 end if;
4118
2aca76d6 4119 -- Check references in body
0868e09c 4120
2aca76d6 4121 Check_References (Body_Id);
996ae0b0 4122 end;
5a271a7f
RD
4123
4124 -- Check for nested subprogram, and mark outer level subprogram if so
4125
4126 declare
4127 Ent : Entity_Id;
4128
4129 begin
4130 if Present (Spec_Id) then
4131 Ent := Spec_Id;
4132 else
4133 Ent := Body_Id;
4134 end if;
4135
4136 loop
4137 Ent := Enclosing_Subprogram (Ent);
4138 exit when No (Ent) or else Is_Subprogram (Ent);
4139 end loop;
4140
4141 if Present (Ent) then
4142 Set_Has_Nested_Subprogram (Ent);
4143 end if;
4144 end;
b1b543d2 4145 end Analyze_Subprogram_Body_Helper;
996ae0b0 4146
5afe5d2d
HK
4147 ---------------------------------
4148 -- Analyze_Subprogram_Contract --
4149 ---------------------------------
4150
c9d70ab1
AC
4151 procedure Analyze_Subprogram_Contract (Subp_Id : Entity_Id) is
4152 procedure Save_Global_References_In_List (First_Prag : Node_Id);
4153 -- Save all global references in contract-related source pragma found in
4154 -- the list starting from pragma First_Prag.
4155
4156 ------------------------------------
4157 -- Save_Global_References_In_List --
4158 ------------------------------------
4159
4160 procedure Save_Global_References_In_List (First_Prag : Node_Id) is
4161 Prag : Node_Id;
4162
4163 begin
4164 Prag := First_Prag;
4165 while Present (Prag) loop
4166 if Comes_From_Source (Prag)
4167 and then Nam_In (Pragma_Name (Prag), Name_Contract_Cases,
4168 Name_Depends,
4169 Name_Extensions_Visible,
4170 Name_Global,
4171 Name_Postcondition,
4172 Name_Precondition,
4173 Name_Test_Case)
4174 then
4175 Save_Global_References (Original_Node (Prag));
4176 end if;
4177
4178 Prag := Next_Pragma (Prag);
4179 end loop;
4180 end Save_Global_References_In_List;
4181
4182 -- Local variables
4183
4184 Items : constant Node_Id := Contract (Subp_Id);
4185 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
4186 Depends : Node_Id := Empty;
4187 Global : Node_Id := Empty;
4188 Mode : SPARK_Mode_Type;
4189 Prag : Node_Id;
4190 Prag_Nam : Name_Id;
4191 Restore_Scope : Boolean := False;
4192
4193 -- Start of processing for Analyze_Subprogram_Contract
5afe5d2d
HK
4194
4195 begin
c9d70ab1
AC
4196 -- All subprograms carry a contract, but for some it is not significant
4197 -- and should not be processed.
4198
4199 if not Has_Significant_Contract (Subp_Id) then
4200 return;
4201 end if;
4202
fc999c5d
RD
4203 -- Due to the timing of contract analysis, delayed pragmas may be
4204 -- subject to the wrong SPARK_Mode, usually that of the enclosing
4205 -- context. To remedy this, restore the original SPARK_Mode of the
4206 -- related subprogram body.
4207
c9d70ab1
AC
4208 Save_SPARK_Mode_And_Set (Subp_Id, Mode);
4209
4210 -- Ensure that the formal parameters are visible when analyzing all
4211 -- contract items.
4212
4213 if not In_Open_Scopes (Subp_Id) then
4214 Restore_Scope := True;
4215 Push_Scope (Subp_Id);
4216
4217 if Is_Generic_Subprogram (Subp_Id) then
4218 Install_Generic_Formals (Subp_Id);
4219 else
4220 Install_Formals (Subp_Id);
4221 end if;
4222 end if;
c61ef416 4223
5afe5d2d
HK
4224 if Present (Items) then
4225
4226 -- Analyze pre- and postconditions
4227
4228 Prag := Pre_Post_Conditions (Items);
4229 while Present (Prag) loop
c9d70ab1 4230 Analyze_Pre_Post_Condition_In_Decl_Part (Prag);
5afe5d2d
HK
4231 Prag := Next_Pragma (Prag);
4232 end loop;
4233
4234 -- Analyze contract-cases and test-cases
4235
4236 Prag := Contract_Test_Cases (Items);
4237 while Present (Prag) loop
c9d70ab1 4238 Prag_Nam := Pragma_Name (Prag);
ea3c0651 4239
c9d70ab1 4240 if Prag_Nam = Name_Contract_Cases then
5afe5d2d 4241 Analyze_Contract_Cases_In_Decl_Part (Prag);
5afe5d2d 4242 else
c9d70ab1
AC
4243 pragma Assert (Prag_Nam = Name_Test_Case);
4244 Analyze_Test_Case_In_Decl_Part (Prag);
5afe5d2d
HK
4245 end if;
4246
4247 Prag := Next_Pragma (Prag);
4248 end loop;
4249
4250 -- Analyze classification pragmas
4251
6c3c671e 4252 Prag := Classifications (Items);
5afe5d2d 4253 while Present (Prag) loop
c9d70ab1 4254 Prag_Nam := Pragma_Name (Prag);
ea3c0651 4255
c9d70ab1 4256 if Prag_Nam = Name_Depends then
54e28df2 4257 Depends := Prag;
039538bc 4258
c9d70ab1 4259 elsif Prag_Nam = Name_Global then
54e28df2 4260 Global := Prag;
039538bc
AC
4261
4262 -- Note that pragma Extensions_Visible has already been analyzed
4263
5afe5d2d
HK
4264 end if;
4265
4266 Prag := Next_Pragma (Prag);
4267 end loop;
54e28df2
HK
4268
4269 -- Analyze Global first as Depends may mention items classified in
4270 -- the global categorization.
4271
4272 if Present (Global) then
4273 Analyze_Global_In_Decl_Part (Global);
4274 end if;
4275
4276 -- Depends must be analyzed after Global in order to see the modes of
4277 -- all global items.
4278
4279 if Present (Depends) then
4280 Analyze_Depends_In_Decl_Part (Depends);
4281 end if;
5afe5d2d 4282
c9d70ab1
AC
4283 -- Ensure that the contract cases or postconditions mention 'Result
4284 -- or define a post-state.
5afe5d2d 4285
c9d70ab1
AC
4286 Check_Result_And_Post_State (Subp_Id);
4287 end if;
5afe5d2d 4288
c9d70ab1
AC
4289 -- The aspects and contract-related source pragmas associated with a
4290 -- generic subprogram are treated separately from the declaration as
4291 -- they need to be analyzed when the subprogram contract is analyzed.
4292 -- Once this is done, global references can be successfully saved.
5afe5d2d 4293
c9d70ab1 4294 if Nkind (Subp_Decl) = N_Generic_Subprogram_Declaration then
f660fba6 4295
c9d70ab1
AC
4296 -- Save all global references found in the aspect specifications of
4297 -- the parameter profile of the generic subprogram.
f660fba6 4298
c9d70ab1 4299 Save_Global_References_In_Aspects (Original_Node (Subp_Decl));
f660fba6 4300
c9d70ab1
AC
4301 -- Save all global references found in contract-related source
4302 -- pragmas. These pragmas usually appear after the declaration of
4303 -- the generic subprogram, either in the same declarative part or
4304 -- in the Pragmas_After list when the generic subprogram is a
4305 -- compilation unit.
4306
4307 if Present (Items) then
4308 Save_Global_References_In_List (Pre_Post_Conditions (Items));
4309 Save_Global_References_In_List (Contract_Test_Cases (Items));
4310 Save_Global_References_In_List (Classifications (Items));
5afe5d2d
HK
4311 end if;
4312 end if;
c61ef416 4313
c9d70ab1
AC
4314 if Restore_Scope then
4315 End_Scope;
4316 end if;
4317
fc999c5d
RD
4318 -- Restore the SPARK_Mode of the enclosing context after all delayed
4319 -- pragmas have been analyzed.
4320
c61ef416 4321 Restore_SPARK_Mode (Mode);
5afe5d2d
HK
4322 end Analyze_Subprogram_Contract;
4323
996ae0b0
RK
4324 ------------------------------------
4325 -- Analyze_Subprogram_Declaration --
4326 ------------------------------------
4327
4328 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
579847c2 4329 Scop : constant Entity_Id := Current_Scope;
5d5832bc 4330 Designator : Entity_Id;
579847c2 4331
4d8f3296
ES
4332 Is_Completion : Boolean;
4333 -- Indicates whether a null procedure declaration is a completion
996ae0b0
RK
4334
4335 begin
8636f52f
HK
4336 -- The subprogram declaration may be subject to pragma Ghost with policy
4337 -- Ignore. Set the mode now to ensure that any nodes generated during
4338 -- analysis and expansion are properly flagged as ignored Ghost.
4339
4340 Set_Ghost_Mode (N);
4341
2ba431e5 4342 -- Null procedures are not allowed in SPARK
daec8eeb 4343
fe5d3068 4344 if Nkind (Specification (N)) = N_Procedure_Specification
daec8eeb
YM
4345 and then Null_Present (Specification (N))
4346 then
ce5ba43a 4347 Check_SPARK_05_Restriction ("null procedure is not allowed", N);
718deaf1 4348
b741083a
ES
4349 -- Null procedures are allowed in protected types, following
4350 -- the recent AI12-0147.
4351
4352 if Is_Protected_Type (Current_Scope)
4353 and then Ada_Version < Ada_2012
4354 then
4d8f3296
ES
4355 Error_Msg_N ("protected operation cannot be a null procedure", N);
4356 end if;
718deaf1 4357
4d8f3296 4358 Analyze_Null_Procedure (N, Is_Completion);
718deaf1 4359
4d8f3296 4360 if Is_Completion then
718deaf1 4361
4d8f3296 4362 -- The null procedure acts as a body, nothing further is needed.
5d5832bc 4363
4d8f3296 4364 return;
5d5832bc
AC
4365 end if;
4366 end if;
4367
beacce02 4368 Designator := Analyze_Subprogram_Specification (Specification (N));
31af8899
AC
4369
4370 -- A reference may already have been generated for the unit name, in
4371 -- which case the following call is redundant. However it is needed for
4372 -- declarations that are the rewriting of an expression function.
4373
5d5832bc
AC
4374 Generate_Definition (Designator);
4375
f90d14ac
AC
4376 -- Set SPARK mode from current context (may be overwritten later with
4377 -- explicit pragma).
4a854847 4378
f90d14ac 4379 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
8636f52f 4380 Set_SPARK_Pragma_Inherited (Designator);
579847c2 4381
8636f52f 4382 -- A subprogram declared within a Ghost region is automatically Ghost
c5cec2fe
AC
4383 -- (SPARK RM 6.9(2)).
4384
8636f52f 4385 if Comes_From_Source (Designator) and then Ghost_Mode > None then
c5cec2fe
AC
4386 Set_Is_Ghost_Entity (Designator);
4387 end if;
4388
b1b543d2
BD
4389 if Debug_Flag_C then
4390 Write_Str ("==> subprogram spec ");
4391 Write_Name (Chars (Designator));
4392 Write_Str (" from ");
4393 Write_Location (Sloc (N));
4394 Write_Eol;
4395 Indent;
4396 end if;
4397
996ae0b0 4398 Validate_RCI_Subprogram_Declaration (N);
996ae0b0
RK
4399 New_Overloaded_Entity (Designator);
4400 Check_Delayed_Subprogram (Designator);
fbf5a39b 4401
cf3e6845
AC
4402 -- If the type of the first formal of the current subprogram is a non-
4403 -- generic tagged private type, mark the subprogram as being a private
4404 -- primitive. Ditto if this is a function with controlling result, and
4405 -- the return type is currently private. In both cases, the type of the
4406 -- controlling argument or result must be in the current scope for the
4407 -- operation to be primitive.
6ca063eb
AC
4408
4409 if Has_Controlling_Result (Designator)
4410 and then Is_Private_Type (Etype (Designator))
b7d5e87b 4411 and then Scope (Etype (Designator)) = Current_Scope
6ca063eb
AC
4412 and then not Is_Generic_Actual_Type (Etype (Designator))
4413 then
4414 Set_Is_Private_Primitive (Designator);
d44202ba 4415
6ca063eb 4416 elsif Present (First_Formal (Designator)) then
d44202ba
HK
4417 declare
4418 Formal_Typ : constant Entity_Id :=
4419 Etype (First_Formal (Designator));
4420 begin
4421 Set_Is_Private_Primitive (Designator,
4422 Is_Tagged_Type (Formal_Typ)
b7d5e87b 4423 and then Scope (Formal_Typ) = Current_Scope
d44202ba
HK
4424 and then Is_Private_Type (Formal_Typ)
4425 and then not Is_Generic_Actual_Type (Formal_Typ));
4426 end;
4427 end if;
4428
ec4867fa
ES
4429 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
4430 -- or null.
4431
0791fbe9 4432 if Ada_Version >= Ada_2005
ec4867fa
ES
4433 and then Comes_From_Source (N)
4434 and then Is_Dispatching_Operation (Designator)
4435 then
4436 declare
4437 E : Entity_Id;
4438 Etyp : Entity_Id;
4439
4440 begin
4441 if Has_Controlling_Result (Designator) then
4442 Etyp := Etype (Designator);
4443
4444 else
4445 E := First_Entity (Designator);
4446 while Present (E)
4447 and then Is_Formal (E)
4448 and then not Is_Controlling_Formal (E)
4449 loop
4450 Next_Entity (E);
4451 end loop;
4452
4453 Etyp := Etype (E);
4454 end if;
4455
4456 if Is_Access_Type (Etyp) then
4457 Etyp := Directly_Designated_Type (Etyp);
4458 end if;
4459
4460 if Is_Interface (Etyp)
f937473f 4461 and then not Is_Abstract_Subprogram (Designator)
ec4867fa 4462 and then not (Ekind (Designator) = E_Procedure
8fde064e 4463 and then Null_Present (Specification (N)))
ec4867fa
ES
4464 then
4465 Error_Msg_Name_1 := Chars (Defining_Entity (N));
033eaf85
AC
4466
4467 -- Specialize error message based on procedures vs. functions,
4468 -- since functions can't be null subprograms.
4469
4470 if Ekind (Designator) = E_Procedure then
4471 Error_Msg_N
4472 ("interface procedure % must be abstract or null", N);
4473 else
3f80a182
AC
4474 Error_Msg_N
4475 ("interface function % must be abstract", N);
033eaf85 4476 end if;
ec4867fa
ES
4477 end if;
4478 end;
4479 end if;
4480
fbf5a39b
AC
4481 -- What is the following code for, it used to be
4482
4483 -- ??? Set_Suppress_Elaboration_Checks
4484 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
4485
4486 -- The following seems equivalent, but a bit dubious
4487
4488 if Elaboration_Checks_Suppressed (Designator) then
4489 Set_Kill_Elaboration_Checks (Designator);
4490 end if;
996ae0b0 4491
8fde064e 4492 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
fbf5a39b 4493 Set_Categorization_From_Scope (Designator, Scop);
8fde064e 4494
996ae0b0 4495 else
e895b435 4496 -- For a compilation unit, check for library-unit pragmas
996ae0b0 4497
0a36105d 4498 Push_Scope (Designator);
996ae0b0
RK
4499 Set_Categorization_From_Pragmas (N);
4500 Validate_Categorization_Dependency (N, Designator);
4501 Pop_Scope;
4502 end if;
4503
4504 -- For a compilation unit, set body required. This flag will only be
4505 -- reset if a valid Import or Interface pragma is processed later on.
4506
4507 if Nkind (Parent (N)) = N_Compilation_Unit then
4508 Set_Body_Required (Parent (N), True);
758c442c 4509
0791fbe9 4510 if Ada_Version >= Ada_2005
758c442c
GD
4511 and then Nkind (Specification (N)) = N_Procedure_Specification
4512 and then Null_Present (Specification (N))
4513 then
4514 Error_Msg_N
4515 ("null procedure cannot be declared at library level", N);
4516 end if;
996ae0b0
RK
4517 end if;
4518
fbf5a39b 4519 Generate_Reference_To_Formals (Designator);
996ae0b0 4520 Check_Eliminated (Designator);
fbf5a39b 4521
b1b543d2
BD
4522 if Debug_Flag_C then
4523 Outdent;
4524 Write_Str ("<== subprogram spec ");
4525 Write_Name (Chars (Designator));
4526 Write_Str (" from ");
4527 Write_Location (Sloc (N));
4528 Write_Eol;
4529 end if;
0f1a6a0b 4530
1a265e78
AC
4531 if Is_Protected_Type (Current_Scope) then
4532
4533 -- Indicate that this is a protected operation, because it may be
4534 -- used in subsequent declarations within the protected type.
4535
4536 Set_Convention (Designator, Convention_Protected);
4537 end if;
4538
beacce02 4539 List_Inherited_Pre_Post_Aspects (Designator);
eaba57fb
RD
4540
4541 if Has_Aspects (N) then
4542 Analyze_Aspect_Specifications (N, Designator);
4543 end if;
996ae0b0
RK
4544 end Analyze_Subprogram_Declaration;
4545
fbf5a39b
AC
4546 --------------------------------------
4547 -- Analyze_Subprogram_Specification --
4548 --------------------------------------
4549
4550 -- Reminder: N here really is a subprogram specification (not a subprogram
4551 -- declaration). This procedure is called to analyze the specification in
4552 -- both subprogram bodies and subprogram declarations (specs).
4553
4554 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
4555 Designator : constant Entity_Id := Defining_Entity (N);
21d27997 4556 Formals : constant List_Id := Parameter_Specifications (N);
fbf5a39b 4557
758c442c
GD
4558 -- Start of processing for Analyze_Subprogram_Specification
4559
fbf5a39b 4560 begin
2ba431e5 4561 -- User-defined operator is not allowed in SPARK, except as a renaming
38171f43 4562
db72f10a
AC
4563 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
4564 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
4565 then
ce5ba43a
AC
4566 Check_SPARK_05_Restriction
4567 ("user-defined operator is not allowed", N);
38171f43
AC
4568 end if;
4569
31af8899
AC
4570 -- Proceed with analysis. Do not emit a cross-reference entry if the
4571 -- specification comes from an expression function, because it may be
4572 -- the completion of a previous declaration. It is is not, the cross-
4573 -- reference entry will be emitted for the new subprogram declaration.
4574
4575 if Nkind (Parent (N)) /= N_Expression_Function then
4576 Generate_Definition (Designator);
4577 end if;
38171f43 4578
fbf5a39b
AC
4579 if Nkind (N) = N_Function_Specification then
4580 Set_Ekind (Designator, E_Function);
4581 Set_Mechanism (Designator, Default_Mechanism);
fbf5a39b
AC
4582 else
4583 Set_Ekind (Designator, E_Procedure);
4584 Set_Etype (Designator, Standard_Void_Type);
4585 end if;
4586
4bd4bb7f
AC
4587 -- Flag Is_Inlined_Always is True by default, and reversed to False for
4588 -- those subprograms which could be inlined in GNATprove mode (because
4589 -- Body_To_Inline is non-Empty) but cannot be inlined.
4590
4591 if GNATprove_Mode then
4592 Set_Is_Inlined_Always (Designator);
4593 end if;
4594
800621e0 4595 -- Introduce new scope for analysis of the formals and the return type
82c80734
RD
4596
4597 Set_Scope (Designator, Current_Scope);
4598
fbf5a39b 4599 if Present (Formals) then
0a36105d 4600 Push_Scope (Designator);
fbf5a39b 4601 Process_Formals (Formals, N);
758c442c 4602
0929eaeb
AC
4603 -- Check dimensions in N for formals with default expression
4604
4605 Analyze_Dimension_Formals (N, Formals);
4606
a38ff9b1
ES
4607 -- Ada 2005 (AI-345): If this is an overriding operation of an
4608 -- inherited interface operation, and the controlling type is
4609 -- a synchronized type, replace the type with its corresponding
4610 -- record, to match the proper signature of an overriding operation.
69cb258c
AC
4611 -- Same processing for an access parameter whose designated type is
4612 -- derived from a synchronized interface.
758c442c 4613
0791fbe9 4614 if Ada_Version >= Ada_2005 then
d44202ba
HK
4615 declare
4616 Formal : Entity_Id;
4617 Formal_Typ : Entity_Id;
4618 Rec_Typ : Entity_Id;
69cb258c 4619 Desig_Typ : Entity_Id;
0a36105d 4620
d44202ba
HK
4621 begin
4622 Formal := First_Formal (Designator);
4623 while Present (Formal) loop
4624 Formal_Typ := Etype (Formal);
0a36105d 4625
d44202ba
HK
4626 if Is_Concurrent_Type (Formal_Typ)
4627 and then Present (Corresponding_Record_Type (Formal_Typ))
4628 then
4629 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
4630
4631 if Present (Interfaces (Rec_Typ)) then
4632 Set_Etype (Formal, Rec_Typ);
4633 end if;
69cb258c
AC
4634
4635 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
4636 Desig_Typ := Designated_Type (Formal_Typ);
4637
4638 if Is_Concurrent_Type (Desig_Typ)
4639 and then Present (Corresponding_Record_Type (Desig_Typ))
4640 then
4641 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
4642
4643 if Present (Interfaces (Rec_Typ)) then
4644 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
4645 end if;
4646 end if;
d44202ba
HK
4647 end if;
4648
4649 Next_Formal (Formal);
4650 end loop;
4651 end;
758c442c
GD
4652 end if;
4653
fbf5a39b 4654 End_Scope;
82c80734 4655
b66c3ff4
AC
4656 -- The subprogram scope is pushed and popped around the processing of
4657 -- the return type for consistency with call above to Process_Formals
4658 -- (which itself can call Analyze_Return_Type), and to ensure that any
4659 -- itype created for the return type will be associated with the proper
4660 -- scope.
4661
82c80734 4662 elsif Nkind (N) = N_Function_Specification then
b66c3ff4 4663 Push_Scope (Designator);
82c80734 4664 Analyze_Return_Type (N);
b66c3ff4 4665 End_Scope;
fbf5a39b
AC
4666 end if;
4667
e606088a
AC
4668 -- Function case
4669
fbf5a39b 4670 if Nkind (N) = N_Function_Specification then
e606088a
AC
4671
4672 -- Deal with operator symbol case
4673
fbf5a39b
AC
4674 if Nkind (Designator) = N_Defining_Operator_Symbol then
4675 Valid_Operator_Definition (Designator);
4676 end if;
4677
4678 May_Need_Actuals (Designator);
4679
fe63b1b1
ES
4680 -- Ada 2005 (AI-251): If the return type is abstract, verify that
4681 -- the subprogram is abstract also. This does not apply to renaming
1adaea16
AC
4682 -- declarations, where abstractness is inherited, and to subprogram
4683 -- bodies generated for stream operations, which become renamings as
4684 -- bodies.
2bfb1b72 4685
fe63b1b1
ES
4686 -- In case of primitives associated with abstract interface types
4687 -- the check is applied later (see Analyze_Subprogram_Declaration).
ec4867fa 4688
1adaea16 4689 if not Nkind_In (Original_Node (Parent (N)),
3f80a182
AC
4690 N_Subprogram_Renaming_Declaration,
4691 N_Abstract_Subprogram_Declaration,
4692 N_Formal_Abstract_Subprogram_Declaration)
fbf5a39b 4693 then
2e79de51
AC
4694 if Is_Abstract_Type (Etype (Designator))
4695 and then not Is_Interface (Etype (Designator))
4696 then
4697 Error_Msg_N
4698 ("function that returns abstract type must be abstract", N);
4699
e606088a 4700 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
2e79de51
AC
4701 -- access result whose designated type is abstract.
4702
4703 elsif Nkind (Result_Definition (N)) = N_Access_Definition
4704 and then
4705 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
4706 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
dbe945f1 4707 and then Ada_Version >= Ada_2012
2e79de51
AC
4708 then
4709 Error_Msg_N ("function whose access result designates "
3f80a182 4710 & "abstract type must be abstract", N);
2e79de51 4711 end if;
fbf5a39b
AC
4712 end if;
4713 end if;
4714
4715 return Designator;
4716 end Analyze_Subprogram_Specification;
4717
996ae0b0
RK
4718 -----------------------
4719 -- Check_Conformance --
4720 -----------------------
4721
4722 procedure Check_Conformance
41251c60
JM
4723 (New_Id : Entity_Id;
4724 Old_Id : Entity_Id;
4725 Ctype : Conformance_Type;
4726 Errmsg : Boolean;
4727 Conforms : out Boolean;
4728 Err_Loc : Node_Id := Empty;
4729 Get_Inst : Boolean := False;
4730 Skip_Controlling_Formals : Boolean := False)
996ae0b0 4731 is
996ae0b0 4732 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
c27f2f15
RD
4733 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
4734 -- If Errmsg is True, then processing continues to post an error message
4735 -- for conformance error on given node. Two messages are output. The
4736 -- first message points to the previous declaration with a general "no
4737 -- conformance" message. The second is the detailed reason, supplied as
4738 -- Msg. The parameter N provide information for a possible & insertion
4739 -- in the message, and also provides the location for posting the
4740 -- message in the absence of a specified Err_Loc location.
996ae0b0
RK
4741
4742 -----------------------
4743 -- Conformance_Error --
4744 -----------------------
4745
4746 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
4747 Enode : Node_Id;
4748
4749 begin
4750 Conforms := False;
4751
4752 if Errmsg then
4753 if No (Err_Loc) then
4754 Enode := N;
4755 else
4756 Enode := Err_Loc;
4757 end if;
4758
4759 Error_Msg_Sloc := Sloc (Old_Id);
4760
4761 case Ctype is
4762 when Type_Conformant =>
483c78cb 4763 Error_Msg_N -- CODEFIX
996ae0b0
RK
4764 ("not type conformant with declaration#!", Enode);
4765
4766 when Mode_Conformant =>
19590d70 4767 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
ed2233dc 4768 Error_Msg_N
19590d70
GD
4769 ("not mode conformant with operation inherited#!",
4770 Enode);
4771 else
ed2233dc 4772 Error_Msg_N
19590d70
GD
4773 ("not mode conformant with declaration#!", Enode);
4774 end if;
996ae0b0
RK
4775
4776 when Subtype_Conformant =>
19590d70 4777 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
ed2233dc 4778 Error_Msg_N
19590d70
GD
4779 ("not subtype conformant with operation inherited#!",
4780 Enode);
4781 else
ed2233dc 4782 Error_Msg_N
19590d70
GD
4783 ("not subtype conformant with declaration#!", Enode);
4784 end if;
996ae0b0
RK
4785
4786 when Fully_Conformant =>
19590d70 4787 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
483c78cb 4788 Error_Msg_N -- CODEFIX
19590d70
GD
4789 ("not fully conformant with operation inherited#!",
4790 Enode);
4791 else
483c78cb 4792 Error_Msg_N -- CODEFIX
19590d70
GD
4793 ("not fully conformant with declaration#!", Enode);
4794 end if;
996ae0b0
RK
4795 end case;
4796
4797 Error_Msg_NE (Msg, Enode, N);
4798 end if;
4799 end Conformance_Error;
4800
ec4867fa
ES
4801 -- Local Variables
4802
4803 Old_Type : constant Entity_Id := Etype (Old_Id);
4804 New_Type : constant Entity_Id := Etype (New_Id);
4805 Old_Formal : Entity_Id;
4806 New_Formal : Entity_Id;
4807 Access_Types_Match : Boolean;
4808 Old_Formal_Base : Entity_Id;
4809 New_Formal_Base : Entity_Id;
4810
996ae0b0
RK
4811 -- Start of processing for Check_Conformance
4812
4813 begin
4814 Conforms := True;
4815
82c80734
RD
4816 -- We need a special case for operators, since they don't appear
4817 -- explicitly.
996ae0b0
RK
4818
4819 if Ctype = Type_Conformant then
4820 if Ekind (New_Id) = E_Operator
4821 and then Operator_Matches_Spec (New_Id, Old_Id)
4822 then
4823 return;
4824 end if;
4825 end if;
4826
4827 -- If both are functions/operators, check return types conform
4828
4829 if Old_Type /= Standard_Void_Type
3cb9a885
AC
4830 and then
4831 New_Type /= Standard_Void_Type
996ae0b0 4832 then
fceeaab6
ES
4833 -- If we are checking interface conformance we omit controlling
4834 -- arguments and result, because we are only checking the conformance
4835 -- of the remaining parameters.
4836
4837 if Has_Controlling_Result (Old_Id)
4838 and then Has_Controlling_Result (New_Id)
4839 and then Skip_Controlling_Formals
4840 then
4841 null;
4842
4843 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
7f568bfa
AC
4844 if Ctype >= Subtype_Conformant
4845 and then not Predicates_Match (Old_Type, New_Type)
4846 then
4847 Conformance_Error
4848 ("\predicate of return type does not match!", New_Id);
4849 else
4850 Conformance_Error
4851 ("\return type does not match!", New_Id);
4852 end if;
4853
996ae0b0
RK
4854 return;
4855 end if;
4856
41251c60 4857 -- Ada 2005 (AI-231): In case of anonymous access types check the
0a36105d 4858 -- null-exclusion and access-to-constant attributes match.
41251c60 4859
0791fbe9 4860 if Ada_Version >= Ada_2005
41251c60
JM
4861 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
4862 and then
8fde064e
AC
4863 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
4864 or else Is_Access_Constant (Etype (Old_Type)) /=
4865 Is_Access_Constant (Etype (New_Type)))
41251c60 4866 then
5d37ba92 4867 Conformance_Error ("\return type does not match!", New_Id);
41251c60
JM
4868 return;
4869 end if;
4870
996ae0b0
RK
4871 -- If either is a function/operator and the other isn't, error
4872
4873 elsif Old_Type /= Standard_Void_Type
4874 or else New_Type /= Standard_Void_Type
4875 then
5d37ba92 4876 Conformance_Error ("\functions can only match functions!", New_Id);
996ae0b0
RK
4877 return;
4878 end if;
4879
0a36105d 4880 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
996ae0b0
RK
4881 -- If this is a renaming as body, refine error message to indicate that
4882 -- the conflict is with the original declaration. If the entity is not
4883 -- frozen, the conventions don't have to match, the one of the renamed
4884 -- entity is inherited.
4885
4886 if Ctype >= Subtype_Conformant then
996ae0b0 4887 if Convention (Old_Id) /= Convention (New_Id) then
996ae0b0
RK
4888 if not Is_Frozen (New_Id) then
4889 null;
4890
4891 elsif Present (Err_Loc)
4892 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
4893 and then Present (Corresponding_Spec (Err_Loc))
4894 then
4895 Error_Msg_Name_1 := Chars (New_Id);
4896 Error_Msg_Name_2 :=
4897 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5d37ba92 4898 Conformance_Error ("\prior declaration for% has convention %!");
996ae0b0
RK
4899
4900 else
5d37ba92 4901 Conformance_Error ("\calling conventions do not match!");
996ae0b0
RK
4902 end if;
4903
4904 return;
4905
4906 elsif Is_Formal_Subprogram (Old_Id)
4907 or else Is_Formal_Subprogram (New_Id)
4908 then
5d37ba92 4909 Conformance_Error ("\formal subprograms not allowed!");
996ae0b0 4910 return;
c5cec2fe
AC
4911
4912 -- Pragma Ghost behaves as a convention in the context of subtype
10274386
AC
4913 -- conformance (SPARK RM 6.9(5)). Do not check internally generated
4914 -- subprograms as their spec may reside in a Ghost region and their
4915 -- body not, or vice versa.
c5cec2fe 4916
10274386
AC
4917 elsif Comes_From_Source (Old_Id)
4918 and then Comes_From_Source (New_Id)
4919 and then Is_Ghost_Entity (Old_Id) /= Is_Ghost_Entity (New_Id)
4920 then
c5cec2fe
AC
4921 Conformance_Error ("\ghost modes do not match!");
4922 return;
996ae0b0
RK
4923 end if;
4924 end if;
4925
4926 -- Deal with parameters
4927
4928 -- Note: we use the entity information, rather than going directly
4929 -- to the specification in the tree. This is not only simpler, but
4930 -- absolutely necessary for some cases of conformance tests between
a90bd866 4931 -- operators, where the declaration tree simply does not exist.
996ae0b0
RK
4932
4933 Old_Formal := First_Formal (Old_Id);
4934 New_Formal := First_Formal (New_Id);
996ae0b0 4935 while Present (Old_Formal) and then Present (New_Formal) loop
41251c60
JM
4936 if Is_Controlling_Formal (Old_Formal)
4937 and then Is_Controlling_Formal (New_Formal)
4938 and then Skip_Controlling_Formals
4939 then
a2dc5812
AC
4940 -- The controlling formals will have different types when
4941 -- comparing an interface operation with its match, but both
4942 -- or neither must be access parameters.
4943
4944 if Is_Access_Type (Etype (Old_Formal))
4945 =
4946 Is_Access_Type (Etype (New_Formal))
4947 then
4948 goto Skip_Controlling_Formal;
4949 else
4950 Conformance_Error
4951 ("\access parameter does not match!", New_Formal);
4952 end if;
41251c60
JM
4953 end if;
4954
21791d97 4955 -- Ada 2012: Mode conformance also requires that formal parameters
2a290fec
AC
4956 -- be both aliased, or neither.
4957
21791d97 4958 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
2a290fec
AC
4959 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
4960 Conformance_Error
4961 ("\aliased parameter mismatch!", New_Formal);
4962 end if;
4963 end if;
4964
fbf5a39b
AC
4965 if Ctype = Fully_Conformant then
4966
4967 -- Names must match. Error message is more accurate if we do
4968 -- this before checking that the types of the formals match.
4969
4970 if Chars (Old_Formal) /= Chars (New_Formal) then
3ccedacc 4971 Conformance_Error ("\name& does not match!", New_Formal);
fbf5a39b
AC
4972
4973 -- Set error posted flag on new formal as well to stop
4974 -- junk cascaded messages in some cases.
4975
4976 Set_Error_Posted (New_Formal);
4977 return;
4978 end if;
40b93859
RD
4979
4980 -- Null exclusion must match
4981
4982 if Null_Exclusion_Present (Parent (Old_Formal))
4983 /=
4984 Null_Exclusion_Present (Parent (New_Formal))
4985 then
4986 -- Only give error if both come from source. This should be
4987 -- investigated some time, since it should not be needed ???
4988
4989 if Comes_From_Source (Old_Formal)
4990 and then
4991 Comes_From_Source (New_Formal)
4992 then
4993 Conformance_Error
3ccedacc 4994 ("\null exclusion for& does not match", New_Formal);
40b93859
RD
4995
4996 -- Mark error posted on the new formal to avoid duplicated
4997 -- complaint about types not matching.
4998
4999 Set_Error_Posted (New_Formal);
5000 end if;
5001 end if;
fbf5a39b 5002 end if;
996ae0b0 5003
ec4867fa
ES
5004 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5005 -- case occurs whenever a subprogram is being renamed and one of its
5006 -- parameters imposes a null exclusion. For example:
5007
5008 -- type T is null record;
5009 -- type Acc_T is access T;
5010 -- subtype Acc_T_Sub is Acc_T;
5011
5012 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5013 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5014 -- renames P;
5015
5016 Old_Formal_Base := Etype (Old_Formal);
5017 New_Formal_Base := Etype (New_Formal);
5018
5019 if Get_Inst then
5020 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5021 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5022 end if;
5023
0791fbe9 5024 Access_Types_Match := Ada_Version >= Ada_2005
ec4867fa 5025
8fde064e
AC
5026 -- Ensure that this rule is only applied when New_Id is a
5027 -- renaming of Old_Id.
ec4867fa 5028
5d37ba92
ES
5029 and then Nkind (Parent (Parent (New_Id))) =
5030 N_Subprogram_Renaming_Declaration
ec4867fa
ES
5031 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
5032 and then Present (Entity (Name (Parent (Parent (New_Id)))))
5033 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
5034
8fde064e 5035 -- Now handle the allowed access-type case
ec4867fa
ES
5036
5037 and then Is_Access_Type (Old_Formal_Base)
5038 and then Is_Access_Type (New_Formal_Base)
5d37ba92 5039
8fde064e
AC
5040 -- The type kinds must match. The only exception occurs with
5041 -- multiple generics of the form:
5d37ba92 5042
8fde064e
AC
5043 -- generic generic
5044 -- type F is private; type A is private;
5045 -- type F_Ptr is access F; type A_Ptr is access A;
5046 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
5047 -- package F_Pack is ... package A_Pack is
5048 -- package F_Inst is
5049 -- new F_Pack (A, A_Ptr, A_P);
5d37ba92 5050
8fde064e
AC
5051 -- When checking for conformance between the parameters of A_P
5052 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
5053 -- because the compiler has transformed A_Ptr into a subtype of
5054 -- F_Ptr. We catch this case in the code below.
5d37ba92
ES
5055
5056 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
4b6f99f5
RD
5057 or else
5058 (Is_Generic_Type (Old_Formal_Base)
5059 and then Is_Generic_Type (New_Formal_Base)
5060 and then Is_Internal (New_Formal_Base)
5061 and then Etype (Etype (New_Formal_Base)) =
5062 Old_Formal_Base))
5063 and then Directly_Designated_Type (Old_Formal_Base) =
5064 Directly_Designated_Type (New_Formal_Base)
ec4867fa
ES
5065 and then ((Is_Itype (Old_Formal_Base)
5066 and then Can_Never_Be_Null (Old_Formal_Base))
4b6f99f5
RD
5067 or else
5068 (Is_Itype (New_Formal_Base)
5069 and then Can_Never_Be_Null (New_Formal_Base)));
ec4867fa 5070
996ae0b0
RK
5071 -- Types must always match. In the visible part of an instance,
5072 -- usual overloading rules for dispatching operations apply, and
5073 -- we check base types (not the actual subtypes).
5074
5075 if In_Instance_Visible_Part
5076 and then Is_Dispatching_Operation (New_Id)
5077 then
5078 if not Conforming_Types
ec4867fa
ES
5079 (T1 => Base_Type (Etype (Old_Formal)),
5080 T2 => Base_Type (Etype (New_Formal)),
5081 Ctype => Ctype,
5082 Get_Inst => Get_Inst)
5083 and then not Access_Types_Match
996ae0b0 5084 then
5d37ba92 5085 Conformance_Error ("\type of & does not match!", New_Formal);
996ae0b0
RK
5086 return;
5087 end if;
5088
5089 elsif not Conforming_Types
5d37ba92
ES
5090 (T1 => Old_Formal_Base,
5091 T2 => New_Formal_Base,
ec4867fa
ES
5092 Ctype => Ctype,
5093 Get_Inst => Get_Inst)
5094 and then not Access_Types_Match
996ae0b0 5095 then
c27f2f15
RD
5096 -- Don't give error message if old type is Any_Type. This test
5097 -- avoids some cascaded errors, e.g. in case of a bad spec.
5098
5099 if Errmsg and then Old_Formal_Base = Any_Type then
5100 Conforms := False;
5101 else
7f568bfa
AC
5102 if Ctype >= Subtype_Conformant
5103 and then
5104 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
5105 then
5106 Conformance_Error
5107 ("\predicate of & does not match!", New_Formal);
5108 else
5109 Conformance_Error
5110 ("\type of & does not match!", New_Formal);
5111 end if;
c27f2f15
RD
5112 end if;
5113
996ae0b0
RK
5114 return;
5115 end if;
5116
5117 -- For mode conformance, mode must match
5118
5d37ba92
ES
5119 if Ctype >= Mode_Conformant then
5120 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
dd54644b
JM
5121 if not Ekind_In (New_Id, E_Function, E_Procedure)
5122 or else not Is_Primitive_Wrapper (New_Id)
5123 then
5124 Conformance_Error ("\mode of & does not match!", New_Formal);
c199ccf7 5125
dd54644b
JM
5126 else
5127 declare
c199ccf7 5128 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
dd54644b 5129 begin
3ccedacc 5130 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
dd54644b 5131 then
2c6336be 5132 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
dd54644b
JM
5133 else
5134 Conformance_Error
5135 ("\mode of & does not match!", New_Formal);
5136 end if;
5137 end;
5138 end if;
5139
5d37ba92
ES
5140 return;
5141
5142 -- Part of mode conformance for access types is having the same
5143 -- constant modifier.
5144
5145 elsif Access_Types_Match
5146 and then Is_Access_Constant (Old_Formal_Base) /=
5147 Is_Access_Constant (New_Formal_Base)
5148 then
5149 Conformance_Error
5150 ("\constant modifier does not match!", New_Formal);
5151 return;
5152 end if;
996ae0b0
RK
5153 end if;
5154
0a36105d 5155 if Ctype >= Subtype_Conformant then
996ae0b0 5156
0a36105d
JM
5157 -- Ada 2005 (AI-231): In case of anonymous access types check
5158 -- the null-exclusion and access-to-constant attributes must
c7b9d548
AC
5159 -- match. For null exclusion, we test the types rather than the
5160 -- formals themselves, since the attribute is only set reliably
5161 -- on the formals in the Ada 95 case, and we exclude the case
5162 -- where Old_Formal is marked as controlling, to avoid errors
5163 -- when matching completing bodies with dispatching declarations
5164 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
996ae0b0 5165
0791fbe9 5166 if Ada_Version >= Ada_2005
0a36105d
JM
5167 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
5168 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
5169 and then
c7b9d548
AC
5170 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
5171 Can_Never_Be_Null (Etype (New_Formal))
5172 and then
5173 not Is_Controlling_Formal (Old_Formal))
0a36105d
JM
5174 or else
5175 Is_Access_Constant (Etype (Old_Formal)) /=
5176 Is_Access_Constant (Etype (New_Formal)))
40b93859
RD
5177
5178 -- Do not complain if error already posted on New_Formal. This
5179 -- avoids some redundant error messages.
5180
5181 and then not Error_Posted (New_Formal)
0a36105d
JM
5182 then
5183 -- It is allowed to omit the null-exclusion in case of stream
5184 -- attribute subprograms. We recognize stream subprograms
5185 -- through their TSS-generated suffix.
996ae0b0 5186
0a36105d
JM
5187 declare
5188 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
3ada950b 5189
0a36105d
JM
5190 begin
5191 if TSS_Name /= TSS_Stream_Read
5192 and then TSS_Name /= TSS_Stream_Write
5193 and then TSS_Name /= TSS_Stream_Input
5194 and then TSS_Name /= TSS_Stream_Output
5195 then
3ada950b 5196 -- Here we have a definite conformance error. It is worth
71fb4dc8 5197 -- special casing the error message for the case of a
3ada950b
AC
5198 -- controlling formal (which excludes null).
5199
5200 if Is_Controlling_Formal (New_Formal) then
5201 Error_Msg_Node_2 := Scope (New_Formal);
5202 Conformance_Error
3ccedacc
AC
5203 ("\controlling formal & of & excludes null, "
5204 & "declaration must exclude null as well",
5205 New_Formal);
3ada950b
AC
5206
5207 -- Normal case (couldn't we give more detail here???)
5208
5209 else
5210 Conformance_Error
5211 ("\type of & does not match!", New_Formal);
5212 end if;
5213
0a36105d
JM
5214 return;
5215 end if;
5216 end;
5217 end if;
5218 end if;
41251c60 5219
0a36105d 5220 -- Full conformance checks
41251c60 5221
0a36105d 5222 if Ctype = Fully_Conformant then
e660dbf7 5223
0a36105d 5224 -- We have checked already that names match
e660dbf7 5225
0a36105d 5226 if Parameter_Mode (Old_Formal) = E_In_Parameter then
41251c60
JM
5227
5228 -- Check default expressions for in parameters
5229
996ae0b0
RK
5230 declare
5231 NewD : constant Boolean :=
5232 Present (Default_Value (New_Formal));
5233 OldD : constant Boolean :=
5234 Present (Default_Value (Old_Formal));
5235 begin
5236 if NewD or OldD then
5237
82c80734
RD
5238 -- The old default value has been analyzed because the
5239 -- current full declaration will have frozen everything
0a36105d
JM
5240 -- before. The new default value has not been analyzed,
5241 -- so analyze it now before we check for conformance.
996ae0b0
RK
5242
5243 if NewD then
0a36105d 5244 Push_Scope (New_Id);
21d27997 5245 Preanalyze_Spec_Expression
fbf5a39b 5246 (Default_Value (New_Formal), Etype (New_Formal));
996ae0b0
RK
5247 End_Scope;
5248 end if;
5249
5250 if not (NewD and OldD)
5251 or else not Fully_Conformant_Expressions
5252 (Default_Value (Old_Formal),
5253 Default_Value (New_Formal))
5254 then
5255 Conformance_Error
5d37ba92 5256 ("\default expression for & does not match!",
996ae0b0
RK
5257 New_Formal);
5258 return;
5259 end if;
5260 end if;
5261 end;
5262 end if;
5263 end if;
5264
5265 -- A couple of special checks for Ada 83 mode. These checks are
0a36105d 5266 -- skipped if either entity is an operator in package Standard,
996ae0b0
RK
5267 -- or if either old or new instance is not from the source program.
5268
0ab80019 5269 if Ada_Version = Ada_83
996ae0b0
RK
5270 and then Sloc (Old_Id) > Standard_Location
5271 and then Sloc (New_Id) > Standard_Location
5272 and then Comes_From_Source (Old_Id)
5273 and then Comes_From_Source (New_Id)
5274 then
5275 declare
5276 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
5277 New_Param : constant Node_Id := Declaration_Node (New_Formal);
5278
5279 begin
5280 -- Explicit IN must be present or absent in both cases. This
5281 -- test is required only in the full conformance case.
5282
5283 if In_Present (Old_Param) /= In_Present (New_Param)
5284 and then Ctype = Fully_Conformant
5285 then
5286 Conformance_Error
5d37ba92 5287 ("\(Ada 83) IN must appear in both declarations",
996ae0b0
RK
5288 New_Formal);
5289 return;
5290 end if;
5291
5292 -- Grouping (use of comma in param lists) must be the same
5293 -- This is where we catch a misconformance like:
5294
0a36105d 5295 -- A, B : Integer
996ae0b0
RK
5296 -- A : Integer; B : Integer
5297
5298 -- which are represented identically in the tree except
5299 -- for the setting of the flags More_Ids and Prev_Ids.
5300
5301 if More_Ids (Old_Param) /= More_Ids (New_Param)
5302 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
5303 then
5304 Conformance_Error
5d37ba92 5305 ("\grouping of & does not match!", New_Formal);
996ae0b0
RK
5306 return;
5307 end if;
5308 end;
5309 end if;
5310
41251c60
JM
5311 -- This label is required when skipping controlling formals
5312
5313 <<Skip_Controlling_Formal>>
5314
996ae0b0
RK
5315 Next_Formal (Old_Formal);
5316 Next_Formal (New_Formal);
5317 end loop;
5318
5319 if Present (Old_Formal) then
5d37ba92 5320 Conformance_Error ("\too few parameters!");
996ae0b0
RK
5321 return;
5322
5323 elsif Present (New_Formal) then
5d37ba92 5324 Conformance_Error ("\too many parameters!", New_Formal);
996ae0b0
RK
5325 return;
5326 end if;
996ae0b0
RK
5327 end Check_Conformance;
5328
ec4867fa
ES
5329 -----------------------
5330 -- Check_Conventions --
5331 -----------------------
5332
5333 procedure Check_Conventions (Typ : Entity_Id) is
ce2b6ba5 5334 Ifaces_List : Elist_Id;
0a36105d 5335
ce2b6ba5 5336 procedure Check_Convention (Op : Entity_Id);
0a36105d
JM
5337 -- Verify that the convention of inherited dispatching operation Op is
5338 -- consistent among all subprograms it overrides. In order to minimize
5339 -- the search, Search_From is utilized to designate a specific point in
5340 -- the list rather than iterating over the whole list once more.
ec4867fa
ES
5341
5342 ----------------------
5343 -- Check_Convention --
5344 ----------------------
5345
ce2b6ba5 5346 procedure Check_Convention (Op : Entity_Id) is
c5cec2fe 5347 Op_Conv : constant Convention_Id := Convention (Op);
9f6aaa5c 5348 Iface_Conv : Convention_Id;
ce2b6ba5
JM
5349 Iface_Elmt : Elmt_Id;
5350 Iface_Prim_Elmt : Elmt_Id;
5351 Iface_Prim : Entity_Id;
ec4867fa 5352
ce2b6ba5
JM
5353 begin
5354 Iface_Elmt := First_Elmt (Ifaces_List);
5355 while Present (Iface_Elmt) loop
5356 Iface_Prim_Elmt :=
9f6aaa5c 5357 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
ce2b6ba5
JM
5358 while Present (Iface_Prim_Elmt) loop
5359 Iface_Prim := Node (Iface_Prim_Elmt);
c5cec2fe 5360 Iface_Conv := Convention (Iface_Prim);
ce2b6ba5
JM
5361
5362 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
9f6aaa5c 5363 and then Iface_Conv /= Op_Conv
ce2b6ba5 5364 then
ed2233dc 5365 Error_Msg_N
ce2b6ba5 5366 ("inconsistent conventions in primitive operations", Typ);
ec4867fa 5367
ce2b6ba5 5368 Error_Msg_Name_1 := Chars (Op);
9f6aaa5c 5369 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
ce2b6ba5 5370 Error_Msg_Sloc := Sloc (Op);
ec4867fa 5371
7a963087 5372 if Comes_From_Source (Op) or else No (Alias (Op)) then
038140ed 5373 if not Present (Overridden_Operation (Op)) then
ed2233dc 5374 Error_Msg_N ("\\primitive % defined #", Typ);
ce2b6ba5 5375 else
ed2233dc 5376 Error_Msg_N
3ccedacc
AC
5377 ("\\overriding operation % with "
5378 & "convention % defined #", Typ);
ce2b6ba5 5379 end if;
ec4867fa 5380
ce2b6ba5
JM
5381 else pragma Assert (Present (Alias (Op)));
5382 Error_Msg_Sloc := Sloc (Alias (Op));
3ccedacc
AC
5383 Error_Msg_N ("\\inherited operation % with "
5384 & "convention % defined #", Typ);
ce2b6ba5 5385 end if;
ec4867fa 5386
ce2b6ba5 5387 Error_Msg_Name_1 := Chars (Op);
9f6aaa5c
AC
5388 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
5389 Error_Msg_Sloc := Sloc (Iface_Prim);
3ccedacc
AC
5390 Error_Msg_N ("\\overridden operation % with "
5391 & "convention % defined #", Typ);
ec4867fa 5392
ce2b6ba5 5393 -- Avoid cascading errors
ec4867fa 5394
ce2b6ba5
JM
5395 return;
5396 end if;
ec4867fa 5397
ce2b6ba5
JM
5398 Next_Elmt (Iface_Prim_Elmt);
5399 end loop;
ec4867fa 5400
ce2b6ba5 5401 Next_Elmt (Iface_Elmt);
ec4867fa
ES
5402 end loop;
5403 end Check_Convention;
5404
5405 -- Local variables
5406
5407 Prim_Op : Entity_Id;
5408 Prim_Op_Elmt : Elmt_Id;
5409
5410 -- Start of processing for Check_Conventions
5411
5412 begin
ce2b6ba5
JM
5413 if not Has_Interfaces (Typ) then
5414 return;
5415 end if;
5416
5417 Collect_Interfaces (Typ, Ifaces_List);
5418
0a36105d
JM
5419 -- The algorithm checks every overriding dispatching operation against
5420 -- all the corresponding overridden dispatching operations, detecting
f3d57416 5421 -- differences in conventions.
ec4867fa
ES
5422
5423 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
5424 while Present (Prim_Op_Elmt) loop
5425 Prim_Op := Node (Prim_Op_Elmt);
5426
0a36105d 5427 -- A small optimization: skip the predefined dispatching operations
ce2b6ba5 5428 -- since they always have the same convention.
ec4867fa 5429
ce2b6ba5
JM
5430 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
5431 Check_Convention (Prim_Op);
ec4867fa
ES
5432 end if;
5433
5434 Next_Elmt (Prim_Op_Elmt);
5435 end loop;
5436 end Check_Conventions;
5437
996ae0b0
RK
5438 ------------------------------
5439 -- Check_Delayed_Subprogram --
5440 ------------------------------
5441
5442 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
5443 F : Entity_Id;
5444
5445 procedure Possible_Freeze (T : Entity_Id);
5446 -- T is the type of either a formal parameter or of the return type.
5447 -- If T is not yet frozen and needs a delayed freeze, then the
4a13695c
AC
5448 -- subprogram itself must be delayed. If T is the limited view of an
5449 -- incomplete type the subprogram must be frozen as well, because
5450 -- T may depend on local types that have not been frozen yet.
996ae0b0 5451
82c80734
RD
5452 ---------------------
5453 -- Possible_Freeze --
5454 ---------------------
5455
996ae0b0
RK
5456 procedure Possible_Freeze (T : Entity_Id) is
5457 begin
4a13695c 5458 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
996ae0b0
RK
5459 Set_Has_Delayed_Freeze (Designator);
5460
5461 elsif Is_Access_Type (T)
5462 and then Has_Delayed_Freeze (Designated_Type (T))
5463 and then not Is_Frozen (Designated_Type (T))
5464 then
5465 Set_Has_Delayed_Freeze (Designator);
e358346d 5466
7b56a91b
AC
5467 elsif Ekind (T) = E_Incomplete_Type
5468 and then From_Limited_With (T)
5469 then
e358346d 5470 Set_Has_Delayed_Freeze (Designator);
406935b6 5471
9aff36e9
RD
5472 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
5473 -- of a subprogram or entry declaration.
406935b6
AC
5474
5475 elsif Ekind (T) = E_Incomplete_Type
5476 and then Ada_Version >= Ada_2012
5477 then
5478 Set_Has_Delayed_Freeze (Designator);
996ae0b0 5479 end if;
4a13695c 5480
996ae0b0
RK
5481 end Possible_Freeze;
5482
5483 -- Start of processing for Check_Delayed_Subprogram
5484
5485 begin
76e3504f
AC
5486 -- All subprograms, including abstract subprograms, may need a freeze
5487 -- node if some formal type or the return type needs one.
996ae0b0 5488
76e3504f
AC
5489 Possible_Freeze (Etype (Designator));
5490 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
996ae0b0 5491
76e3504f
AC
5492 -- Need delayed freeze if any of the formal types themselves need
5493 -- a delayed freeze and are not yet frozen.
996ae0b0 5494
76e3504f
AC
5495 F := First_Formal (Designator);
5496 while Present (F) loop
5497 Possible_Freeze (Etype (F));
5498 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
5499 Next_Formal (F);
5500 end loop;
996ae0b0
RK
5501
5502 -- Mark functions that return by reference. Note that it cannot be
5503 -- done for delayed_freeze subprograms because the underlying
5504 -- returned type may not be known yet (for private types)
5505
8fde064e 5506 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
996ae0b0
RK
5507 declare
5508 Typ : constant Entity_Id := Etype (Designator);
5509 Utyp : constant Entity_Id := Underlying_Type (Typ);
996ae0b0 5510 begin
51245e2d 5511 if Is_Limited_View (Typ) then
996ae0b0 5512 Set_Returns_By_Ref (Designator);
048e5cef 5513 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
996ae0b0
RK
5514 Set_Returns_By_Ref (Designator);
5515 end if;
5516 end;
5517 end if;
5518 end Check_Delayed_Subprogram;
5519
5520 ------------------------------------
5521 -- Check_Discriminant_Conformance --
5522 ------------------------------------
5523
5524 procedure Check_Discriminant_Conformance
5525 (N : Node_Id;
5526 Prev : Entity_Id;
5527 Prev_Loc : Node_Id)
5528 is
5529 Old_Discr : Entity_Id := First_Discriminant (Prev);
5530 New_Discr : Node_Id := First (Discriminant_Specifications (N));
5531 New_Discr_Id : Entity_Id;
5532 New_Discr_Type : Entity_Id;
5533
5534 procedure Conformance_Error (Msg : String; N : Node_Id);
82c80734
RD
5535 -- Post error message for conformance error on given node. Two messages
5536 -- are output. The first points to the previous declaration with a
5537 -- general "no conformance" message. The second is the detailed reason,
5538 -- supplied as Msg. The parameter N provide information for a possible
5539 -- & insertion in the message.
996ae0b0
RK
5540
5541 -----------------------
5542 -- Conformance_Error --
5543 -----------------------
5544
5545 procedure Conformance_Error (Msg : String; N : Node_Id) is
5546 begin
5547 Error_Msg_Sloc := Sloc (Prev_Loc);
483c78cb
RD
5548 Error_Msg_N -- CODEFIX
5549 ("not fully conformant with declaration#!", N);
996ae0b0
RK
5550 Error_Msg_NE (Msg, N, N);
5551 end Conformance_Error;
5552
5553 -- Start of processing for Check_Discriminant_Conformance
5554
5555 begin
5556 while Present (Old_Discr) and then Present (New_Discr) loop
996ae0b0
RK
5557 New_Discr_Id := Defining_Identifier (New_Discr);
5558
82c80734
RD
5559 -- The subtype mark of the discriminant on the full type has not
5560 -- been analyzed so we do it here. For an access discriminant a new
5561 -- type is created.
996ae0b0
RK
5562
5563 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
5564 New_Discr_Type :=
5565 Access_Definition (N, Discriminant_Type (New_Discr));
5566
5567 else
5568 Analyze (Discriminant_Type (New_Discr));
5569 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
e50e1c5e
AC
5570
5571 -- Ada 2005: if the discriminant definition carries a null
5572 -- exclusion, create an itype to check properly for consistency
5573 -- with partial declaration.
5574
5575 if Is_Access_Type (New_Discr_Type)
8fde064e 5576 and then Null_Exclusion_Present (New_Discr)
e50e1c5e
AC
5577 then
5578 New_Discr_Type :=
5579 Create_Null_Excluding_Itype
5580 (T => New_Discr_Type,
5581 Related_Nod => New_Discr,
5582 Scope_Id => Current_Scope);
5583 end if;
996ae0b0
RK
5584 end if;
5585
5586 if not Conforming_Types
5587 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
5588 then
5589 Conformance_Error ("type of & does not match!", New_Discr_Id);
5590 return;
fbf5a39b 5591 else
82c80734
RD
5592 -- Treat the new discriminant as an occurrence of the old one,
5593 -- for navigation purposes, and fill in some semantic
fbf5a39b
AC
5594 -- information, for completeness.
5595
5596 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
5597 Set_Etype (New_Discr_Id, Etype (Old_Discr));
5598 Set_Scope (New_Discr_Id, Scope (Old_Discr));
996ae0b0
RK
5599 end if;
5600
5601 -- Names must match
5602
5603 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
5604 Conformance_Error ("name & does not match!", New_Discr_Id);
5605 return;
5606 end if;
5607
5608 -- Default expressions must match
5609
5610 declare
5611 NewD : constant Boolean :=
5612 Present (Expression (New_Discr));
5613 OldD : constant Boolean :=
5614 Present (Expression (Parent (Old_Discr)));
5615
5616 begin
5617 if NewD or OldD then
5618
5619 -- The old default value has been analyzed and expanded,
5620 -- because the current full declaration will have frozen
82c80734
RD
5621 -- everything before. The new default values have not been
5622 -- expanded, so expand now to check conformance.
996ae0b0
RK
5623
5624 if NewD then
21d27997 5625 Preanalyze_Spec_Expression
996ae0b0
RK
5626 (Expression (New_Discr), New_Discr_Type);
5627 end if;
5628
5629 if not (NewD and OldD)
5630 or else not Fully_Conformant_Expressions
5631 (Expression (Parent (Old_Discr)),
5632 Expression (New_Discr))
5633
5634 then
5635 Conformance_Error
5636 ("default expression for & does not match!",
5637 New_Discr_Id);
5638 return;
5639 end if;
5640 end if;
5641 end;
5642
5643 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
5644
0ab80019 5645 if Ada_Version = Ada_83 then
996ae0b0
RK
5646 declare
5647 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
5648
5649 begin
5650 -- Grouping (use of comma in param lists) must be the same
5651 -- This is where we catch a misconformance like:
5652
60370fb1 5653 -- A, B : Integer
996ae0b0
RK
5654 -- A : Integer; B : Integer
5655
5656 -- which are represented identically in the tree except
5657 -- for the setting of the flags More_Ids and Prev_Ids.
5658
5659 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
5660 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
5661 then
5662 Conformance_Error
5663 ("grouping of & does not match!", New_Discr_Id);
5664 return;
5665 end if;
5666 end;
5667 end if;
5668
5669 Next_Discriminant (Old_Discr);
5670 Next (New_Discr);
5671 end loop;
5672
5673 if Present (Old_Discr) then
5674 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
5675 return;
5676
5677 elsif Present (New_Discr) then
5678 Conformance_Error
5679 ("too many discriminants!", Defining_Identifier (New_Discr));
5680 return;
5681 end if;
5682 end Check_Discriminant_Conformance;
5683
5684 ----------------------------
5685 -- Check_Fully_Conformant --
5686 ----------------------------
5687
5688 procedure Check_Fully_Conformant
5689 (New_Id : Entity_Id;
5690 Old_Id : Entity_Id;
5691 Err_Loc : Node_Id := Empty)
5692 is
5693 Result : Boolean;
81db9d77 5694 pragma Warnings (Off, Result);
996ae0b0
RK
5695 begin
5696 Check_Conformance
5697 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
5698 end Check_Fully_Conformant;
5699
5700 ---------------------------
5701 -- Check_Mode_Conformant --
5702 ---------------------------
5703
5704 procedure Check_Mode_Conformant
5705 (New_Id : Entity_Id;
5706 Old_Id : Entity_Id;
5707 Err_Loc : Node_Id := Empty;
5708 Get_Inst : Boolean := False)
5709 is
5710 Result : Boolean;
81db9d77 5711 pragma Warnings (Off, Result);
996ae0b0
RK
5712 begin
5713 Check_Conformance
5714 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
5715 end Check_Mode_Conformant;
5716
fbf5a39b 5717 --------------------------------
758c442c 5718 -- Check_Overriding_Indicator --
fbf5a39b
AC
5719 --------------------------------
5720
758c442c 5721 procedure Check_Overriding_Indicator
ec4867fa 5722 (Subp : Entity_Id;
5d37ba92
ES
5723 Overridden_Subp : Entity_Id;
5724 Is_Primitive : Boolean)
fbf5a39b 5725 is
758c442c
GD
5726 Decl : Node_Id;
5727 Spec : Node_Id;
fbf5a39b
AC
5728
5729 begin
ec4867fa 5730 -- No overriding indicator for literals
fbf5a39b 5731
ec4867fa 5732 if Ekind (Subp) = E_Enumeration_Literal then
758c442c 5733 return;
fbf5a39b 5734
ec4867fa
ES
5735 elsif Ekind (Subp) = E_Entry then
5736 Decl := Parent (Subp);
5737
53b10ce9
AC
5738 -- No point in analyzing a malformed operator
5739
5740 elsif Nkind (Subp) = N_Defining_Operator_Symbol
5741 and then Error_Posted (Subp)
5742 then
5743 return;
5744
758c442c
GD
5745 else
5746 Decl := Unit_Declaration_Node (Subp);
5747 end if;
fbf5a39b 5748
800621e0
RD
5749 if Nkind_In (Decl, N_Subprogram_Body,
5750 N_Subprogram_Body_Stub,
5751 N_Subprogram_Declaration,
5752 N_Abstract_Subprogram_Declaration,
5753 N_Subprogram_Renaming_Declaration)
758c442c
GD
5754 then
5755 Spec := Specification (Decl);
ec4867fa
ES
5756
5757 elsif Nkind (Decl) = N_Entry_Declaration then
5758 Spec := Decl;
5759
758c442c
GD
5760 else
5761 return;
5762 end if;
fbf5a39b 5763
e7d72fb9
AC
5764 -- The overriding operation is type conformant with the overridden one,
5765 -- but the names of the formals are not required to match. If the names
6823270c 5766 -- appear permuted in the overriding operation, this is a possible
e7d72fb9
AC
5767 -- source of confusion that is worth diagnosing. Controlling formals
5768 -- often carry names that reflect the type, and it is not worthwhile
5769 -- requiring that their names match.
5770
c9e7bd8e 5771 if Present (Overridden_Subp)
e7d72fb9
AC
5772 and then Nkind (Subp) /= N_Defining_Operator_Symbol
5773 then
5774 declare
5775 Form1 : Entity_Id;
5776 Form2 : Entity_Id;
5777
5778 begin
5779 Form1 := First_Formal (Subp);
5780 Form2 := First_Formal (Overridden_Subp);
5781
c9e7bd8e
AC
5782 -- If the overriding operation is a synchronized operation, skip
5783 -- the first parameter of the overridden operation, which is
6823270c
AC
5784 -- implicit in the new one. If the operation is declared in the
5785 -- body it is not primitive and all formals must match.
c9e7bd8e 5786
6823270c
AC
5787 if Is_Concurrent_Type (Scope (Subp))
5788 and then Is_Tagged_Type (Scope (Subp))
5789 and then not Has_Completion (Scope (Subp))
5790 then
c9e7bd8e
AC
5791 Form2 := Next_Formal (Form2);
5792 end if;
5793
e7d72fb9
AC
5794 if Present (Form1) then
5795 Form1 := Next_Formal (Form1);
5796 Form2 := Next_Formal (Form2);
5797 end if;
5798
5799 while Present (Form1) loop
5800 if not Is_Controlling_Formal (Form1)
5801 and then Present (Next_Formal (Form2))
5802 and then Chars (Form1) = Chars (Next_Formal (Form2))
5803 then
5804 Error_Msg_Node_2 := Alias (Overridden_Subp);
5805 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
ed2233dc 5806 Error_Msg_NE
19d846a0 5807 ("& does not match corresponding formal of&#",
e7d72fb9
AC
5808 Form1, Form1);
5809 exit;
5810 end if;
5811
5812 Next_Formal (Form1);
5813 Next_Formal (Form2);
5814 end loop;
5815 end;
5816 end if;
5817
676e8420
AC
5818 -- If there is an overridden subprogram, then check that there is no
5819 -- "not overriding" indicator, and mark the subprogram as overriding.
51bf9bdf
AC
5820 -- This is not done if the overridden subprogram is marked as hidden,
5821 -- which can occur for the case of inherited controlled operations
5822 -- (see Derive_Subprogram), unless the inherited subprogram's parent
5823 -- subprogram is not itself hidden. (Note: This condition could probably
5824 -- be simplified, leaving out the testing for the specific controlled
5825 -- cases, but it seems safer and clearer this way, and echoes similar
5826 -- special-case tests of this kind in other places.)
5827
fd0d899b 5828 if Present (Overridden_Subp)
51bf9bdf
AC
5829 and then (not Is_Hidden (Overridden_Subp)
5830 or else
b69cd36a
AC
5831 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
5832 Name_Adjust,
5833 Name_Finalize)
f0709ca6
AC
5834 and then Present (Alias (Overridden_Subp))
5835 and then not Is_Hidden (Alias (Overridden_Subp))))
fd0d899b 5836 then
ec4867fa
ES
5837 if Must_Not_Override (Spec) then
5838 Error_Msg_Sloc := Sloc (Overridden_Subp);
fbf5a39b 5839
ec4867fa 5840 if Ekind (Subp) = E_Entry then
ed2233dc 5841 Error_Msg_NE
5d37ba92 5842 ("entry & overrides inherited operation #", Spec, Subp);
ec4867fa 5843 else
ed2233dc 5844 Error_Msg_NE
5d37ba92 5845 ("subprogram & overrides inherited operation #", Spec, Subp);
ec4867fa 5846 end if;
21d27997 5847
bd603506 5848 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
24a120ac
AC
5849 -- as an extension of Root_Controlled, and thus has a useless Adjust
5850 -- operation. This operation should not be inherited by other limited
5851 -- controlled types. An explicit Adjust for them is not overriding.
5852
5853 elsif Must_Override (Spec)
5854 and then Chars (Overridden_Subp) = Name_Adjust
5855 and then Is_Limited_Type (Etype (First_Formal (Subp)))
5856 and then Present (Alias (Overridden_Subp))
bd603506
RD
5857 and then
5858 Is_Predefined_File_Name
5859 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
24a120ac
AC
5860 then
5861 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5862
21d27997 5863 elsif Is_Subprogram (Subp) then
2fe829ae
ES
5864 if Is_Init_Proc (Subp) then
5865 null;
5866
5867 elsif No (Overridden_Operation (Subp)) then
1c1289e7
AC
5868
5869 -- For entities generated by Derive_Subprograms the overridden
5870 -- operation is the inherited primitive (which is available
5871 -- through the attribute alias)
5872
5873 if (Is_Dispatching_Operation (Subp)
f9673bb0 5874 or else Is_Dispatching_Operation (Overridden_Subp))
1c1289e7 5875 and then not Comes_From_Source (Overridden_Subp)
f9673bb0
AC
5876 and then Find_Dispatching_Type (Overridden_Subp) =
5877 Find_Dispatching_Type (Subp)
1c1289e7
AC
5878 and then Present (Alias (Overridden_Subp))
5879 and then Comes_From_Source (Alias (Overridden_Subp))
5880 then
039538bc
AC
5881 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
5882 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
2fe829ae 5883
1c1289e7 5884 else
039538bc
AC
5885 Set_Overridden_Operation (Subp, Overridden_Subp);
5886 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
1c1289e7
AC
5887 end if;
5888 end if;
ec4867fa 5889 end if;
f937473f 5890
618fb570
AC
5891 -- If primitive flag is set or this is a protected operation, then
5892 -- the operation is overriding at the point of its declaration, so
5893 -- warn if necessary. Otherwise it may have been declared before the
5894 -- operation it overrides and no check is required.
3c25856a
AC
5895
5896 if Style_Check
618fb570
AC
5897 and then not Must_Override (Spec)
5898 and then (Is_Primitive
5899 or else Ekind (Scope (Subp)) = E_Protected_Type)
3c25856a 5900 then
235f4375
AC
5901 Style.Missing_Overriding (Decl, Subp);
5902 end if;
5903
53b10ce9
AC
5904 -- If Subp is an operator, it may override a predefined operation, if
5905 -- it is defined in the same scope as the type to which it applies.
676e8420 5906 -- In that case Overridden_Subp is empty because of our implicit
5d37ba92
ES
5907 -- representation for predefined operators. We have to check whether the
5908 -- signature of Subp matches that of a predefined operator. Note that
5909 -- first argument provides the name of the operator, and the second
5910 -- argument the signature that may match that of a standard operation.
21d27997
RD
5911 -- If the indicator is overriding, then the operator must match a
5912 -- predefined signature, because we know already that there is no
5913 -- explicit overridden operation.
f937473f 5914
21d27997 5915 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
806f6d37 5916 if Must_Not_Override (Spec) then
f937473f 5917
806f6d37
AC
5918 -- If this is not a primitive or a protected subprogram, then
5919 -- "not overriding" is illegal.
618fb570 5920
806f6d37
AC
5921 if not Is_Primitive
5922 and then Ekind (Scope (Subp)) /= E_Protected_Type
5923 then
3ccedacc
AC
5924 Error_Msg_N ("overriding indicator only allowed "
5925 & "if subprogram is primitive", Subp);
618fb570 5926
806f6d37
AC
5927 elsif Can_Override_Operator (Subp) then
5928 Error_Msg_NE
5929 ("subprogram& overrides predefined operator ", Spec, Subp);
5930 end if;
f937473f 5931
806f6d37
AC
5932 elsif Must_Override (Spec) then
5933 if No (Overridden_Operation (Subp))
5934 and then not Can_Override_Operator (Subp)
5935 then
5936 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5937 end if;
5d37ba92 5938
806f6d37
AC
5939 elsif not Error_Posted (Subp)
5940 and then Style_Check
5941 and then Can_Override_Operator (Subp)
5942 and then
5943 not Is_Predefined_File_Name
5944 (Unit_File_Name (Get_Source_Unit (Subp)))
5945 then
5946 -- If style checks are enabled, indicate that the indicator is
5947 -- missing. However, at the point of declaration, the type of
5948 -- which this is a primitive operation may be private, in which
5949 -- case the indicator would be premature.
235f4375 5950
806f6d37
AC
5951 if Has_Private_Declaration (Etype (Subp))
5952 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
53b10ce9 5953 then
806f6d37
AC
5954 null;
5955 else
5956 Style.Missing_Overriding (Decl, Subp);
5d5832bc 5957 end if;
806f6d37 5958 end if;
21d27997
RD
5959
5960 elsif Must_Override (Spec) then
5961 if Ekind (Subp) = E_Entry then
ed2233dc 5962 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
5d37ba92 5963 else
ed2233dc 5964 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
758c442c 5965 end if;
5d37ba92
ES
5966
5967 -- If the operation is marked "not overriding" and it's not primitive
5968 -- then an error is issued, unless this is an operation of a task or
5969 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
5970 -- has been specified have already been checked above.
5971
5972 elsif Must_Not_Override (Spec)
5973 and then not Is_Primitive
5974 and then Ekind (Subp) /= E_Entry
5975 and then Ekind (Scope (Subp)) /= E_Protected_Type
5976 then
ed2233dc 5977 Error_Msg_N
5d37ba92
ES
5978 ("overriding indicator only allowed if subprogram is primitive",
5979 Subp);
5d37ba92 5980 return;
fbf5a39b 5981 end if;
758c442c 5982 end Check_Overriding_Indicator;
fbf5a39b 5983
996ae0b0
RK
5984 -------------------
5985 -- Check_Returns --
5986 -------------------
5987
0a36105d
JM
5988 -- Note: this procedure needs to know far too much about how the expander
5989 -- messes with exceptions. The use of the flag Exception_Junk and the
5990 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
5991 -- works, but is not very clean. It would be better if the expansion
5992 -- routines would leave Original_Node working nicely, and we could use
5993 -- Original_Node here to ignore all the peculiar expander messing ???
5994
996ae0b0
RK
5995 procedure Check_Returns
5996 (HSS : Node_Id;
5997 Mode : Character;
c8ef728f
ES
5998 Err : out Boolean;
5999 Proc : Entity_Id := Empty)
996ae0b0
RK
6000 is
6001 Handler : Node_Id;
6002
6003 procedure Check_Statement_Sequence (L : List_Id);
6004 -- Internal recursive procedure to check a list of statements for proper
6005 -- termination by a return statement (or a transfer of control or a
6006 -- compound statement that is itself internally properly terminated).
6007
6008 ------------------------------
6009 -- Check_Statement_Sequence --
6010 ------------------------------
6011
6012 procedure Check_Statement_Sequence (L : List_Id) is
6013 Last_Stm : Node_Id;
0a36105d 6014 Stm : Node_Id;
996ae0b0
RK
6015 Kind : Node_Kind;
6016
7b27e183
AC
6017 function Assert_False return Boolean;
6018 -- Returns True if Last_Stm is a pragma Assert (False) that has been
6019 -- rewritten as a null statement when assertions are off. The assert
6020 -- is not active, but it is still enough to kill the warning.
6021
6022 ------------------
6023 -- Assert_False --
6024 ------------------
6025
6026 function Assert_False return Boolean is
6027 Orig : constant Node_Id := Original_Node (Last_Stm);
6028
6029 begin
6030 if Nkind (Orig) = N_Pragma
6031 and then Pragma_Name (Orig) = Name_Assert
6032 and then not Error_Posted (Orig)
6033 then
6034 declare
6035 Arg : constant Node_Id :=
6036 First (Pragma_Argument_Associations (Orig));
6037 Exp : constant Node_Id := Expression (Arg);
6038 begin
6039 return Nkind (Exp) = N_Identifier
6040 and then Chars (Exp) = Name_False;
6041 end;
6042
6043 else
6044 return False;
6045 end if;
6046 end Assert_False;
6047
6048 -- Local variables
6049
996ae0b0
RK
6050 Raise_Exception_Call : Boolean;
6051 -- Set True if statement sequence terminated by Raise_Exception call
6052 -- or a Reraise_Occurrence call.
6053
7b27e183
AC
6054 -- Start of processing for Check_Statement_Sequence
6055
996ae0b0
RK
6056 begin
6057 Raise_Exception_Call := False;
6058
6059 -- Get last real statement
6060
6061 Last_Stm := Last (L);
6062
0a36105d
JM
6063 -- Deal with digging out exception handler statement sequences that
6064 -- have been transformed by the local raise to goto optimization.
6065 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
6066 -- optimization has occurred, we are looking at something like:
6067
6068 -- begin
6069 -- original stmts in block
6070
6071 -- exception \
6072 -- when excep1 => |
6073 -- goto L1; | omitted if No_Exception_Propagation
6074 -- when excep2 => |
6075 -- goto L2; /
6076 -- end;
6077
6078 -- goto L3; -- skip handler when exception not raised
6079
6080 -- <<L1>> -- target label for local exception
6081 -- begin
6082 -- estmts1
6083 -- end;
6084
6085 -- goto L3;
6086
6087 -- <<L2>>
6088 -- begin
6089 -- estmts2
6090 -- end;
6091
6092 -- <<L3>>
6093
6094 -- and what we have to do is to dig out the estmts1 and estmts2
6095 -- sequences (which were the original sequences of statements in
6096 -- the exception handlers) and check them.
6097
8fde064e 6098 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
0a36105d
JM
6099 Stm := Last_Stm;
6100 loop
6101 Prev (Stm);
6102 exit when No (Stm);
6103 exit when Nkind (Stm) /= N_Block_Statement;
6104 exit when not Exception_Junk (Stm);
6105 Prev (Stm);
6106 exit when No (Stm);
6107 exit when Nkind (Stm) /= N_Label;
6108 exit when not Exception_Junk (Stm);
6109 Check_Statement_Sequence
6110 (Statements (Handled_Statement_Sequence (Next (Stm))));
6111
6112 Prev (Stm);
6113 Last_Stm := Stm;
6114 exit when No (Stm);
6115 exit when Nkind (Stm) /= N_Goto_Statement;
6116 exit when not Exception_Junk (Stm);
6117 end loop;
6118 end if;
6119
996ae0b0
RK
6120 -- Don't count pragmas
6121
6122 while Nkind (Last_Stm) = N_Pragma
6123
6124 -- Don't count call to SS_Release (can happen after Raise_Exception)
6125
6126 or else
6127 (Nkind (Last_Stm) = N_Procedure_Call_Statement
6128 and then
6129 Nkind (Name (Last_Stm)) = N_Identifier
6130 and then
6131 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
6132
6133 -- Don't count exception junk
6134
6135 or else
800621e0
RD
6136 (Nkind_In (Last_Stm, N_Goto_Statement,
6137 N_Label,
6138 N_Object_Declaration)
8fde064e 6139 and then Exception_Junk (Last_Stm))
0a36105d
JM
6140 or else Nkind (Last_Stm) in N_Push_xxx_Label
6141 or else Nkind (Last_Stm) in N_Pop_xxx_Label
e3b3266c
AC
6142
6143 -- Inserted code, such as finalization calls, is irrelevant: we only
6144 -- need to check original source.
6145
6146 or else Is_Rewrite_Insertion (Last_Stm)
996ae0b0
RK
6147 loop
6148 Prev (Last_Stm);
6149 end loop;
6150
6151 -- Here we have the "real" last statement
6152
6153 Kind := Nkind (Last_Stm);
6154
6155 -- Transfer of control, OK. Note that in the No_Return procedure
6156 -- case, we already diagnosed any explicit return statements, so
6157 -- we can treat them as OK in this context.
6158
6159 if Is_Transfer (Last_Stm) then
6160 return;
6161
6162 -- Check cases of explicit non-indirect procedure calls
6163
6164 elsif Kind = N_Procedure_Call_Statement
6165 and then Is_Entity_Name (Name (Last_Stm))
6166 then
6167 -- Check call to Raise_Exception procedure which is treated
6168 -- specially, as is a call to Reraise_Occurrence.
6169
6170 -- We suppress the warning in these cases since it is likely that
6171 -- the programmer really does not expect to deal with the case
6172 -- of Null_Occurrence, and thus would find a warning about a
6173 -- missing return curious, and raising Program_Error does not
6174 -- seem such a bad behavior if this does occur.
6175
c8ef728f
ES
6176 -- Note that in the Ada 2005 case for Raise_Exception, the actual
6177 -- behavior will be to raise Constraint_Error (see AI-329).
6178
996ae0b0
RK
6179 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
6180 or else
6181 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
6182 then
6183 Raise_Exception_Call := True;
6184
6185 -- For Raise_Exception call, test first argument, if it is
6186 -- an attribute reference for a 'Identity call, then we know
6187 -- that the call cannot possibly return.
6188
6189 declare
6190 Arg : constant Node_Id :=
6191 Original_Node (First_Actual (Last_Stm));
996ae0b0
RK
6192 begin
6193 if Nkind (Arg) = N_Attribute_Reference
6194 and then Attribute_Name (Arg) = Name_Identity
6195 then
6196 return;
6197 end if;
6198 end;
6199 end if;
6200
6201 -- If statement, need to look inside if there is an else and check
6202 -- each constituent statement sequence for proper termination.
6203
6204 elsif Kind = N_If_Statement
6205 and then Present (Else_Statements (Last_Stm))
6206 then
6207 Check_Statement_Sequence (Then_Statements (Last_Stm));
6208 Check_Statement_Sequence (Else_Statements (Last_Stm));
6209
6210 if Present (Elsif_Parts (Last_Stm)) then
6211 declare
6212 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
6213
6214 begin
6215 while Present (Elsif_Part) loop
6216 Check_Statement_Sequence (Then_Statements (Elsif_Part));
6217 Next (Elsif_Part);
6218 end loop;
6219 end;
6220 end if;
6221
6222 return;
6223
6224 -- Case statement, check each case for proper termination
6225
6226 elsif Kind = N_Case_Statement then
6227 declare
6228 Case_Alt : Node_Id;
996ae0b0
RK
6229 begin
6230 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
6231 while Present (Case_Alt) loop
6232 Check_Statement_Sequence (Statements (Case_Alt));
6233 Next_Non_Pragma (Case_Alt);
6234 end loop;
6235 end;
6236
6237 return;
6238
6239 -- Block statement, check its handled sequence of statements
6240
6241 elsif Kind = N_Block_Statement then
6242 declare
6243 Err1 : Boolean;
6244
6245 begin
6246 Check_Returns
6247 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
6248
6249 if Err1 then
6250 Err := True;
6251 end if;
6252
6253 return;
6254 end;
6255
6256 -- Loop statement. If there is an iteration scheme, we can definitely
6257 -- fall out of the loop. Similarly if there is an exit statement, we
6258 -- can fall out. In either case we need a following return.
6259
6260 elsif Kind = N_Loop_Statement then
6261 if Present (Iteration_Scheme (Last_Stm))
6262 or else Has_Exit (Entity (Identifier (Last_Stm)))
6263 then
6264 null;
6265
f3d57416
RW
6266 -- A loop with no exit statement or iteration scheme is either
6267 -- an infinite loop, or it has some other exit (raise/return).
996ae0b0
RK
6268 -- In either case, no warning is required.
6269
6270 else
6271 return;
6272 end if;
6273
6274 -- Timed entry call, check entry call and delay alternatives
6275
6276 -- Note: in expanded code, the timed entry call has been converted
6277 -- to a set of expanded statements on which the check will work
6278 -- correctly in any case.
6279
6280 elsif Kind = N_Timed_Entry_Call then
6281 declare
6282 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6283 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
6284
6285 begin
6286 -- If statement sequence of entry call alternative is missing,
6287 -- then we can definitely fall through, and we post the error
6288 -- message on the entry call alternative itself.
6289
6290 if No (Statements (ECA)) then
6291 Last_Stm := ECA;
6292
6293 -- If statement sequence of delay alternative is missing, then
6294 -- we can definitely fall through, and we post the error
6295 -- message on the delay alternative itself.
6296
6297 -- Note: if both ECA and DCA are missing the return, then we
6298 -- post only one message, should be enough to fix the bugs.
6299 -- If not we will get a message next time on the DCA when the
a90bd866 6300 -- ECA is fixed.
996ae0b0
RK
6301
6302 elsif No (Statements (DCA)) then
6303 Last_Stm := DCA;
6304
6305 -- Else check both statement sequences
6306
6307 else
6308 Check_Statement_Sequence (Statements (ECA));
6309 Check_Statement_Sequence (Statements (DCA));
6310 return;
6311 end if;
6312 end;
6313
6314 -- Conditional entry call, check entry call and else part
6315
6316 -- Note: in expanded code, the conditional entry call has been
6317 -- converted to a set of expanded statements on which the check
6318 -- will work correctly in any case.
6319
6320 elsif Kind = N_Conditional_Entry_Call then
6321 declare
6322 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6323
6324 begin
6325 -- If statement sequence of entry call alternative is missing,
6326 -- then we can definitely fall through, and we post the error
6327 -- message on the entry call alternative itself.
6328
6329 if No (Statements (ECA)) then
6330 Last_Stm := ECA;
6331
6332 -- Else check statement sequence and else part
6333
6334 else
6335 Check_Statement_Sequence (Statements (ECA));
6336 Check_Statement_Sequence (Else_Statements (Last_Stm));
6337 return;
6338 end if;
6339 end;
6340 end if;
6341
6342 -- If we fall through, issue appropriate message
6343
6344 if Mode = 'F' then
7b27e183
AC
6345
6346 -- Kill warning if last statement is a raise exception call,
6347 -- or a pragma Assert (False). Note that with assertions enabled,
6348 -- such a pragma has been converted into a raise exception call
6349 -- already, so the Assert_False is for the assertions off case.
6350
6351 if not Raise_Exception_Call and then not Assert_False then
b465ef6f
AC
6352
6353 -- In GNATprove mode, it is an error to have a missing return
6354
43417b90 6355 Error_Msg_Warn := SPARK_Mode /= On;
7b27e183
AC
6356
6357 -- Issue error message or warning
6358
4a28b181
AC
6359 Error_Msg_N
6360 ("RETURN statement missing following this statement<<!",
6361 Last_Stm);
6362 Error_Msg_N
6363 ("\Program_Error ]<<!", Last_Stm);
996ae0b0
RK
6364 end if;
6365
6366 -- Note: we set Err even though we have not issued a warning
6367 -- because we still have a case of a missing return. This is
6368 -- an extremely marginal case, probably will never be noticed
6369 -- but we might as well get it right.
6370
6371 Err := True;
6372
c8ef728f
ES
6373 -- Otherwise we have the case of a procedure marked No_Return
6374
996ae0b0 6375 else
800621e0 6376 if not Raise_Exception_Call then
4a28b181
AC
6377 if GNATprove_Mode then
6378 Error_Msg_N
6379 ("implied return after this statement "
6380 & "would have raised Program_Error", Last_Stm);
6381 else
6382 Error_Msg_N
6383 ("implied return after this statement "
6384 & "will raise Program_Error??", Last_Stm);
6385 end if;
6386
43417b90 6387 Error_Msg_Warn := SPARK_Mode /= On;
800621e0 6388 Error_Msg_NE
4a28b181 6389 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
800621e0 6390 end if;
c8ef728f
ES
6391
6392 declare
6393 RE : constant Node_Id :=
6394 Make_Raise_Program_Error (Sloc (Last_Stm),
6395 Reason => PE_Implicit_Return);
6396 begin
6397 Insert_After (Last_Stm, RE);
6398 Analyze (RE);
6399 end;
996ae0b0
RK
6400 end if;
6401 end Check_Statement_Sequence;
6402
6403 -- Start of processing for Check_Returns
6404
6405 begin
6406 Err := False;
6407 Check_Statement_Sequence (Statements (HSS));
6408
6409 if Present (Exception_Handlers (HSS)) then
6410 Handler := First_Non_Pragma (Exception_Handlers (HSS));
6411 while Present (Handler) loop
6412 Check_Statement_Sequence (Statements (Handler));
6413 Next_Non_Pragma (Handler);
6414 end loop;
6415 end if;
6416 end Check_Returns;
6417
6418 ----------------------------
6419 -- Check_Subprogram_Order --
6420 ----------------------------
6421
6422 procedure Check_Subprogram_Order (N : Node_Id) is
6423
6424 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
dc36a7e3
RD
6425 -- This is used to check if S1 > S2 in the sense required by this test,
6426 -- for example nameab < namec, but name2 < name10.
996ae0b0 6427
82c80734
RD
6428 -----------------------------
6429 -- Subprogram_Name_Greater --
6430 -----------------------------
6431
996ae0b0
RK
6432 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
6433 L1, L2 : Positive;
6434 N1, N2 : Natural;
6435
6436 begin
67336960
AC
6437 -- Deal with special case where names are identical except for a
6438 -- numerical suffix. These are handled specially, taking the numeric
6439 -- ordering from the suffix into account.
996ae0b0
RK
6440
6441 L1 := S1'Last;
6442 while S1 (L1) in '0' .. '9' loop
6443 L1 := L1 - 1;
6444 end loop;
6445
6446 L2 := S2'Last;
6447 while S2 (L2) in '0' .. '9' loop
6448 L2 := L2 - 1;
6449 end loop;
6450
67336960 6451 -- If non-numeric parts non-equal, do straight compare
996ae0b0 6452
67336960
AC
6453 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
6454 return S1 > S2;
996ae0b0
RK
6455
6456 -- If non-numeric parts equal, compare suffixed numeric parts. Note
6457 -- that a missing suffix is treated as numeric zero in this test.
6458
6459 else
6460 N1 := 0;
6461 while L1 < S1'Last loop
6462 L1 := L1 + 1;
6463 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
6464 end loop;
6465
6466 N2 := 0;
6467 while L2 < S2'Last loop
6468 L2 := L2 + 1;
6469 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
6470 end loop;
6471
6472 return N1 > N2;
6473 end if;
6474 end Subprogram_Name_Greater;
6475
6476 -- Start of processing for Check_Subprogram_Order
6477
6478 begin
6479 -- Check body in alpha order if this is option
6480
fbf5a39b 6481 if Style_Check
bc202b70 6482 and then Style_Check_Order_Subprograms
996ae0b0
RK
6483 and then Nkind (N) = N_Subprogram_Body
6484 and then Comes_From_Source (N)
6485 and then In_Extended_Main_Source_Unit (N)
6486 then
6487 declare
6488 LSN : String_Ptr
6489 renames Scope_Stack.Table
6490 (Scope_Stack.Last).Last_Subprogram_Name;
6491
6492 Body_Id : constant Entity_Id :=
6493 Defining_Entity (Specification (N));
6494
6495 begin
6496 Get_Decoded_Name_String (Chars (Body_Id));
6497
6498 if LSN /= null then
6499 if Subprogram_Name_Greater
6500 (LSN.all, Name_Buffer (1 .. Name_Len))
6501 then
6502 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
6503 end if;
6504
6505 Free (LSN);
6506 end if;
6507
6508 LSN := new String'(Name_Buffer (1 .. Name_Len));
6509 end;
6510 end if;
6511 end Check_Subprogram_Order;
6512
6513 ------------------------------
6514 -- Check_Subtype_Conformant --
6515 ------------------------------
6516
6517 procedure Check_Subtype_Conformant
ce2b6ba5
JM
6518 (New_Id : Entity_Id;
6519 Old_Id : Entity_Id;
6520 Err_Loc : Node_Id := Empty;
f307415a
AC
6521 Skip_Controlling_Formals : Boolean := False;
6522 Get_Inst : Boolean := False)
996ae0b0
RK
6523 is
6524 Result : Boolean;
81db9d77 6525 pragma Warnings (Off, Result);
996ae0b0
RK
6526 begin
6527 Check_Conformance
ce2b6ba5 6528 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
f307415a
AC
6529 Skip_Controlling_Formals => Skip_Controlling_Formals,
6530 Get_Inst => Get_Inst);
996ae0b0
RK
6531 end Check_Subtype_Conformant;
6532
6533 ---------------------------
6534 -- Check_Type_Conformant --
6535 ---------------------------
6536
6537 procedure Check_Type_Conformant
6538 (New_Id : Entity_Id;
6539 Old_Id : Entity_Id;
6540 Err_Loc : Node_Id := Empty)
6541 is
6542 Result : Boolean;
81db9d77 6543 pragma Warnings (Off, Result);
996ae0b0
RK
6544 begin
6545 Check_Conformance
6546 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
6547 end Check_Type_Conformant;
6548
806f6d37
AC
6549 ---------------------------
6550 -- Can_Override_Operator --
6551 ---------------------------
6552
6553 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
6554 Typ : Entity_Id;
f146302c 6555
806f6d37
AC
6556 begin
6557 if Nkind (Subp) /= N_Defining_Operator_Symbol then
6558 return False;
6559
6560 else
6561 Typ := Base_Type (Etype (First_Formal (Subp)));
6562
f146302c
AC
6563 -- Check explicitly that the operation is a primitive of the type
6564
806f6d37 6565 return Operator_Matches_Spec (Subp, Subp)
f146302c 6566 and then not Is_Generic_Type (Typ)
806f6d37
AC
6567 and then Scope (Subp) = Scope (Typ)
6568 and then not Is_Class_Wide_Type (Typ);
6569 end if;
6570 end Can_Override_Operator;
6571
996ae0b0
RK
6572 ----------------------
6573 -- Conforming_Types --
6574 ----------------------
6575
6576 function Conforming_Types
6577 (T1 : Entity_Id;
6578 T2 : Entity_Id;
6579 Ctype : Conformance_Type;
d05ef0ab 6580 Get_Inst : Boolean := False) return Boolean
996ae0b0
RK
6581 is
6582 Type_1 : Entity_Id := T1;
6583 Type_2 : Entity_Id := T2;
af4b9434 6584 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
996ae0b0
RK
6585
6586 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
0a36105d
JM
6587 -- If neither T1 nor T2 are generic actual types, or if they are in
6588 -- different scopes (e.g. parent and child instances), then verify that
6589 -- the base types are equal. Otherwise T1 and T2 must be on the same
6590 -- subtype chain. The whole purpose of this procedure is to prevent
6591 -- spurious ambiguities in an instantiation that may arise if two
6592 -- distinct generic types are instantiated with the same actual.
6593
5d37ba92
ES
6594 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
6595 -- An access parameter can designate an incomplete type. If the
6596 -- incomplete type is the limited view of a type from a limited_
6597 -- with_clause, check whether the non-limited view is available. If
6598 -- it is a (non-limited) incomplete type, get the full view.
6599
0a36105d
JM
6600 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
6601 -- Returns True if and only if either T1 denotes a limited view of T2
6602 -- or T2 denotes a limited view of T1. This can arise when the limited
6603 -- with view of a type is used in a subprogram declaration and the
6604 -- subprogram body is in the scope of a regular with clause for the
6605 -- same unit. In such a case, the two type entities can be considered
6606 -- identical for purposes of conformance checking.
996ae0b0
RK
6607
6608 ----------------------
6609 -- Base_Types_Match --
6610 ----------------------
6611
6612 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
8fde064e
AC
6613 BT1 : constant Entity_Id := Base_Type (T1);
6614 BT2 : constant Entity_Id := Base_Type (T2);
6615
996ae0b0
RK
6616 begin
6617 if T1 = T2 then
6618 return True;
6619
8fde064e 6620 elsif BT1 = BT2 then
996ae0b0 6621
0a36105d 6622 -- The following is too permissive. A more precise test should
996ae0b0
RK
6623 -- check that the generic actual is an ancestor subtype of the
6624 -- other ???.
586ecbf3 6625
70f4ad20
AC
6626 -- See code in Find_Corresponding_Spec that applies an additional
6627 -- filter to handle accidental amiguities in instances.
996ae0b0
RK
6628
6629 return not Is_Generic_Actual_Type (T1)
07fc65c4
GB
6630 or else not Is_Generic_Actual_Type (T2)
6631 or else Scope (T1) /= Scope (T2);
996ae0b0 6632
8fde064e 6633 -- If T2 is a generic actual type it is declared as the subtype of
2995860f
AC
6634 -- the actual. If that actual is itself a subtype we need to use its
6635 -- own base type to check for compatibility.
8fde064e
AC
6636
6637 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
6638 return True;
6639
6640 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
6641 return True;
6642
0a36105d
JM
6643 else
6644 return False;
6645 end if;
6646 end Base_Types_Match;
aa720a54 6647
5d37ba92
ES
6648 --------------------------
6649 -- Find_Designated_Type --
6650 --------------------------
6651
6652 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
6653 Desig : Entity_Id;
6654
6655 begin
6656 Desig := Directly_Designated_Type (T);
6657
6658 if Ekind (Desig) = E_Incomplete_Type then
6659
6660 -- If regular incomplete type, get full view if available
6661
6662 if Present (Full_View (Desig)) then
6663 Desig := Full_View (Desig);
6664
6665 -- If limited view of a type, get non-limited view if available,
6666 -- and check again for a regular incomplete type.
6667
6668 elsif Present (Non_Limited_View (Desig)) then
6669 Desig := Get_Full_View (Non_Limited_View (Desig));
6670 end if;
6671 end if;
6672
6673 return Desig;
6674 end Find_Designated_Type;
6675
0a36105d
JM
6676 -------------------------------
6677 -- Matches_Limited_With_View --
6678 -------------------------------
6679
6680 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
6681 begin
6682 -- In some cases a type imported through a limited_with clause, and
6683 -- its nonlimited view are both visible, for example in an anonymous
28fa5430
AC
6684 -- access-to-class-wide type in a formal, or when building the body
6685 -- for a subprogram renaming after the subprogram has been frozen.
6686 -- In these cases Both entities designate the same type. In addition,
6687 -- if one of them is an actual in an instance, it may be a subtype of
6688 -- the non-limited view of the other.
6689
6690 if From_Limited_With (T1)
6691 and then (T2 = Available_View (T1)
6692 or else Is_Subtype_Of (T2, Available_View (T1)))
6693 then
aa720a54
AC
6694 return True;
6695
28fa5430
AC
6696 elsif From_Limited_With (T2)
6697 and then (T1 = Available_View (T2)
6698 or else Is_Subtype_Of (T1, Available_View (T2)))
6699 then
41251c60 6700 return True;
3e24afaa 6701
7b56a91b
AC
6702 elsif From_Limited_With (T1)
6703 and then From_Limited_With (T2)
3e24afaa
AC
6704 and then Available_View (T1) = Available_View (T2)
6705 then
6706 return True;
41251c60 6707
996ae0b0
RK
6708 else
6709 return False;
6710 end if;
0a36105d 6711 end Matches_Limited_With_View;
996ae0b0 6712
ec4867fa 6713 -- Start of processing for Conforming_Types
758c442c 6714
996ae0b0 6715 begin
8fde064e
AC
6716 -- The context is an instance association for a formal access-to-
6717 -- subprogram type; the formal parameter types require mapping because
6718 -- they may denote other formal parameters of the generic unit.
996ae0b0
RK
6719
6720 if Get_Inst then
6721 Type_1 := Get_Instance_Of (T1);
6722 Type_2 := Get_Instance_Of (T2);
6723 end if;
6724
0a36105d
JM
6725 -- If one of the types is a view of the other introduced by a limited
6726 -- with clause, treat these as conforming for all purposes.
996ae0b0 6727
0a36105d
JM
6728 if Matches_Limited_With_View (T1, T2) then
6729 return True;
6730
6731 elsif Base_Types_Match (Type_1, Type_2) then
996ae0b0
RK
6732 return Ctype <= Mode_Conformant
6733 or else Subtypes_Statically_Match (Type_1, Type_2);
6734
6735 elsif Is_Incomplete_Or_Private_Type (Type_1)
6736 and then Present (Full_View (Type_1))
6737 and then Base_Types_Match (Full_View (Type_1), Type_2)
6738 then
6739 return Ctype <= Mode_Conformant
6740 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
6741
6742 elsif Ekind (Type_2) = E_Incomplete_Type
6743 and then Present (Full_View (Type_2))
6744 and then Base_Types_Match (Type_1, Full_View (Type_2))
6745 then
6746 return Ctype <= Mode_Conformant
6747 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
fbf5a39b
AC
6748
6749 elsif Is_Private_Type (Type_2)
6750 and then In_Instance
6751 and then Present (Full_View (Type_2))
6752 and then Base_Types_Match (Type_1, Full_View (Type_2))
6753 then
6754 return Ctype <= Mode_Conformant
6755 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
996ae0b0
RK
6756 end if;
6757
0a36105d 6758 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
466c2127
AC
6759 -- treated recursively because they carry a signature. As far as
6760 -- conformance is concerned, convention plays no role, and either
6761 -- or both could be access to protected subprograms.
af4b9434
AC
6762
6763 Are_Anonymous_Access_To_Subprogram_Types :=
466c2127
AC
6764 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
6765 E_Anonymous_Access_Protected_Subprogram_Type)
f937473f 6766 and then
466c2127
AC
6767 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
6768 E_Anonymous_Access_Protected_Subprogram_Type);
af4b9434 6769
996ae0b0 6770 -- Test anonymous access type case. For this case, static subtype
5d37ba92
ES
6771 -- matching is required for mode conformance (RM 6.3.1(15)). We check
6772 -- the base types because we may have built internal subtype entities
6773 -- to handle null-excluding types (see Process_Formals).
996ae0b0 6774
5d37ba92
ES
6775 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
6776 and then
6777 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
8fde064e
AC
6778
6779 -- Ada 2005 (AI-254)
6780
6781 or else Are_Anonymous_Access_To_Subprogram_Types
996ae0b0
RK
6782 then
6783 declare
6784 Desig_1 : Entity_Id;
6785 Desig_2 : Entity_Id;
6786
6787 begin
885c4871 6788 -- In Ada 2005, access constant indicators must match for
5d37ba92 6789 -- subtype conformance.
9dcb52e1 6790
0791fbe9 6791 if Ada_Version >= Ada_2005
5d37ba92
ES
6792 and then Ctype >= Subtype_Conformant
6793 and then
6794 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
6795 then
6796 return False;
996ae0b0
RK
6797 end if;
6798
5d37ba92 6799 Desig_1 := Find_Designated_Type (Type_1);
5d37ba92 6800 Desig_2 := Find_Designated_Type (Type_2);
996ae0b0 6801
5d37ba92 6802 -- If the context is an instance association for a formal
82c80734
RD
6803 -- access-to-subprogram type; formal access parameter designated
6804 -- types require mapping because they may denote other formal
6805 -- parameters of the generic unit.
996ae0b0
RK
6806
6807 if Get_Inst then
6808 Desig_1 := Get_Instance_Of (Desig_1);
6809 Desig_2 := Get_Instance_Of (Desig_2);
6810 end if;
6811
82c80734
RD
6812 -- It is possible for a Class_Wide_Type to be introduced for an
6813 -- incomplete type, in which case there is a separate class_ wide
6814 -- type for the full view. The types conform if their Etypes
6815 -- conform, i.e. one may be the full view of the other. This can
6816 -- only happen in the context of an access parameter, other uses
6817 -- of an incomplete Class_Wide_Type are illegal.
996ae0b0 6818
fbf5a39b 6819 if Is_Class_Wide_Type (Desig_1)
4adf3c50
AC
6820 and then
6821 Is_Class_Wide_Type (Desig_2)
996ae0b0
RK
6822 then
6823 return
fbf5a39b
AC
6824 Conforming_Types
6825 (Etype (Base_Type (Desig_1)),
6826 Etype (Base_Type (Desig_2)), Ctype);
af4b9434
AC
6827
6828 elsif Are_Anonymous_Access_To_Subprogram_Types then
0791fbe9 6829 if Ada_Version < Ada_2005 then
758c442c
GD
6830 return Ctype = Type_Conformant
6831 or else
af4b9434
AC
6832 Subtypes_Statically_Match (Desig_1, Desig_2);
6833
758c442c
GD
6834 -- We must check the conformance of the signatures themselves
6835
6836 else
6837 declare
6838 Conformant : Boolean;
6839 begin
6840 Check_Conformance
6841 (Desig_1, Desig_2, Ctype, False, Conformant);
6842 return Conformant;
6843 end;
6844 end if;
6845
996ae0b0
RK
6846 else
6847 return Base_Type (Desig_1) = Base_Type (Desig_2)
6848 and then (Ctype = Type_Conformant
8fde064e
AC
6849 or else
6850 Subtypes_Statically_Match (Desig_1, Desig_2));
996ae0b0
RK
6851 end if;
6852 end;
6853
6854 -- Otherwise definitely no match
6855
6856 else
c8ef728f
ES
6857 if ((Ekind (Type_1) = E_Anonymous_Access_Type
6858 and then Is_Access_Type (Type_2))
6859 or else (Ekind (Type_2) = E_Anonymous_Access_Type
8fde064e 6860 and then Is_Access_Type (Type_1)))
c8ef728f
ES
6861 and then
6862 Conforming_Types
6863 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
6864 then
6865 May_Hide_Profile := True;
6866 end if;
6867
996ae0b0
RK
6868 return False;
6869 end if;
996ae0b0
RK
6870 end Conforming_Types;
6871
6872 --------------------------
6873 -- Create_Extra_Formals --
6874 --------------------------
6875
6876 procedure Create_Extra_Formals (E : Entity_Id) is
6877 Formal : Entity_Id;
ec4867fa 6878 First_Extra : Entity_Id := Empty;
996ae0b0
RK
6879 Last_Extra : Entity_Id;
6880 Formal_Type : Entity_Id;
6881 P_Formal : Entity_Id := Empty;
6882
ec4867fa
ES
6883 function Add_Extra_Formal
6884 (Assoc_Entity : Entity_Id;
6885 Typ : Entity_Id;
6886 Scope : Entity_Id;
6887 Suffix : String) return Entity_Id;
6888 -- Add an extra formal to the current list of formals and extra formals.
6889 -- The extra formal is added to the end of the list of extra formals,
6890 -- and also returned as the result. These formals are always of mode IN.
6891 -- The new formal has the type Typ, is declared in Scope, and its name
6892 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
cd5a9750
AC
6893 -- The following suffixes are currently used. They should not be changed
6894 -- without coordinating with CodePeer, which makes use of these to
6895 -- provide better messages.
6896
d92eccc3
AC
6897 -- O denotes the Constrained bit.
6898 -- L denotes the accessibility level.
cd5a9750
AC
6899 -- BIP_xxx denotes an extra formal for a build-in-place function. See
6900 -- the full list in exp_ch6.BIP_Formal_Kind.
996ae0b0 6901
fbf5a39b
AC
6902 ----------------------
6903 -- Add_Extra_Formal --
6904 ----------------------
6905
ec4867fa
ES
6906 function Add_Extra_Formal
6907 (Assoc_Entity : Entity_Id;
6908 Typ : Entity_Id;
6909 Scope : Entity_Id;
6910 Suffix : String) return Entity_Id
6911 is
996ae0b0 6912 EF : constant Entity_Id :=
ec4867fa
ES
6913 Make_Defining_Identifier (Sloc (Assoc_Entity),
6914 Chars => New_External_Name (Chars (Assoc_Entity),
f937473f 6915 Suffix => Suffix));
996ae0b0
RK
6916
6917 begin
82c80734
RD
6918 -- A little optimization. Never generate an extra formal for the
6919 -- _init operand of an initialization procedure, since it could
6920 -- never be used.
996ae0b0
RK
6921
6922 if Chars (Formal) = Name_uInit then
6923 return Empty;
6924 end if;
6925
6926 Set_Ekind (EF, E_In_Parameter);
6927 Set_Actual_Subtype (EF, Typ);
6928 Set_Etype (EF, Typ);
ec4867fa 6929 Set_Scope (EF, Scope);
996ae0b0
RK
6930 Set_Mechanism (EF, Default_Mechanism);
6931 Set_Formal_Validity (EF);
6932
ec4867fa
ES
6933 if No (First_Extra) then
6934 First_Extra := EF;
6935 Set_Extra_Formals (Scope, First_Extra);
6936 end if;
6937
6938 if Present (Last_Extra) then
6939 Set_Extra_Formal (Last_Extra, EF);
6940 end if;
6941
996ae0b0 6942 Last_Extra := EF;
ec4867fa 6943
996ae0b0
RK
6944 return EF;
6945 end Add_Extra_Formal;
6946
6947 -- Start of processing for Create_Extra_Formals
6948
6949 begin
8fde064e
AC
6950 -- We never generate extra formals if expansion is not active because we
6951 -- don't need them unless we are generating code.
f937473f
RD
6952
6953 if not Expander_Active then
6954 return;
6955 end if;
6956
e2441021
AC
6957 -- No need to generate extra formals in interface thunks whose target
6958 -- primitive has no extra formals.
6959
6960 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
6961 return;
6962 end if;
6963
82c80734 6964 -- If this is a derived subprogram then the subtypes of the parent
16b05213 6965 -- subprogram's formal parameters will be used to determine the need
82c80734 6966 -- for extra formals.
996ae0b0
RK
6967
6968 if Is_Overloadable (E) and then Present (Alias (E)) then
6969 P_Formal := First_Formal (Alias (E));
6970 end if;
6971
6972 Last_Extra := Empty;
6973 Formal := First_Formal (E);
6974 while Present (Formal) loop
6975 Last_Extra := Formal;
6976 Next_Formal (Formal);
6977 end loop;
6978
f937473f 6979 -- If Extra_formals were already created, don't do it again. This
82c80734
RD
6980 -- situation may arise for subprogram types created as part of
6981 -- dispatching calls (see Expand_Dispatching_Call)
996ae0b0 6982
8fde064e 6983 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
996ae0b0
RK
6984 return;
6985 end if;
6986
19590d70
GD
6987 -- If the subprogram is a predefined dispatching subprogram then don't
6988 -- generate any extra constrained or accessibility level formals. In
6989 -- general we suppress these for internal subprograms (by not calling
6990 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
6991 -- generated stream attributes do get passed through because extra
6992 -- build-in-place formals are needed in some cases (limited 'Input).
6993
bac7206d 6994 if Is_Predefined_Internal_Operation (E) then
63585f75 6995 goto Test_For_Func_Result_Extras;
19590d70
GD
6996 end if;
6997
996ae0b0 6998 Formal := First_Formal (E);
996ae0b0
RK
6999 while Present (Formal) loop
7000
7001 -- Create extra formal for supporting the attribute 'Constrained.
7002 -- The case of a private type view without discriminants also
7003 -- requires the extra formal if the underlying type has defaulted
7004 -- discriminants.
7005
7006 if Ekind (Formal) /= E_In_Parameter then
7007 if Present (P_Formal) then
7008 Formal_Type := Etype (P_Formal);
7009 else
7010 Formal_Type := Etype (Formal);
7011 end if;
7012
5d09245e
AC
7013 -- Do not produce extra formals for Unchecked_Union parameters.
7014 -- Jump directly to the end of the loop.
7015
7016 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
7017 goto Skip_Extra_Formal_Generation;
7018 end if;
7019
996ae0b0
RK
7020 if not Has_Discriminants (Formal_Type)
7021 and then Ekind (Formal_Type) in Private_Kind
7022 and then Present (Underlying_Type (Formal_Type))
7023 then
7024 Formal_Type := Underlying_Type (Formal_Type);
7025 end if;
7026
5e5db3b4
GD
7027 -- Suppress the extra formal if formal's subtype is constrained or
7028 -- indefinite, or we're compiling for Ada 2012 and the underlying
7029 -- type is tagged and limited. In Ada 2012, a limited tagged type
7030 -- can have defaulted discriminants, but 'Constrained is required
7031 -- to return True, so the formal is never needed (see AI05-0214).
7032 -- Note that this ensures consistency of calling sequences for
7033 -- dispatching operations when some types in a class have defaults
7034 -- on discriminants and others do not (and requiring the extra
7035 -- formal would introduce distributed overhead).
7036
b5bdffcc
AC
7037 -- If the type does not have a completion yet, treat as prior to
7038 -- Ada 2012 for consistency.
7039
996ae0b0 7040 if Has_Discriminants (Formal_Type)
f937473f
RD
7041 and then not Is_Constrained (Formal_Type)
7042 and then not Is_Indefinite_Subtype (Formal_Type)
5e5db3b4 7043 and then (Ada_Version < Ada_2012
b5bdffcc
AC
7044 or else No (Underlying_Type (Formal_Type))
7045 or else not
7046 (Is_Limited_Type (Formal_Type)
7047 and then
7048 (Is_Tagged_Type
7049 (Underlying_Type (Formal_Type)))))
996ae0b0
RK
7050 then
7051 Set_Extra_Constrained
d92eccc3 7052 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
996ae0b0
RK
7053 end if;
7054 end if;
7055
0a36105d
JM
7056 -- Create extra formal for supporting accessibility checking. This
7057 -- is done for both anonymous access formals and formals of named
7058 -- access types that are marked as controlling formals. The latter
7059 -- case can occur when Expand_Dispatching_Call creates a subprogram
7060 -- type and substitutes the types of access-to-class-wide actuals
7061 -- for the anonymous access-to-specific-type of controlling formals.
5d37ba92
ES
7062 -- Base_Type is applied because in cases where there is a null
7063 -- exclusion the formal may have an access subtype.
996ae0b0
RK
7064
7065 -- This is suppressed if we specifically suppress accessibility
f937473f 7066 -- checks at the package level for either the subprogram, or the
fbf5a39b
AC
7067 -- package in which it resides. However, we do not suppress it
7068 -- simply if the scope has accessibility checks suppressed, since
7069 -- this could cause trouble when clients are compiled with a
7070 -- different suppression setting. The explicit checks at the
7071 -- package level are safe from this point of view.
996ae0b0 7072
5d37ba92 7073 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
0a36105d 7074 or else (Is_Controlling_Formal (Formal)
5d37ba92 7075 and then Is_Access_Type (Base_Type (Etype (Formal)))))
996ae0b0 7076 and then not
fbf5a39b 7077 (Explicit_Suppress (E, Accessibility_Check)
996ae0b0 7078 or else
fbf5a39b 7079 Explicit_Suppress (Scope (E), Accessibility_Check))
996ae0b0 7080 and then
c8ef728f 7081 (No (P_Formal)
996ae0b0
RK
7082 or else Present (Extra_Accessibility (P_Formal)))
7083 then
811c6a85 7084 Set_Extra_Accessibility
d92eccc3 7085 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
996ae0b0
RK
7086 end if;
7087
5d09245e
AC
7088 -- This label is required when skipping extra formal generation for
7089 -- Unchecked_Union parameters.
7090
7091 <<Skip_Extra_Formal_Generation>>
7092
f937473f
RD
7093 if Present (P_Formal) then
7094 Next_Formal (P_Formal);
7095 end if;
7096
996ae0b0
RK
7097 Next_Formal (Formal);
7098 end loop;
ec4867fa 7099
63585f75
SB
7100 <<Test_For_Func_Result_Extras>>
7101
7102 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
7103 -- function call is ... determined by the point of call ...".
7104
7105 if Needs_Result_Accessibility_Level (E) then
7106 Set_Extra_Accessibility_Of_Result
7107 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
7108 end if;
19590d70 7109
ec4867fa 7110 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
f937473f
RD
7111 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
7112
0791fbe9 7113 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
ec4867fa 7114 declare
f937473f 7115 Result_Subt : constant Entity_Id := Etype (E);
1a36a0cd 7116 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
2fcc44fa 7117 Formal_Typ : Entity_Id;
f937473f 7118
2fcc44fa 7119 Discard : Entity_Id;
f937473f 7120 pragma Warnings (Off, Discard);
ec4867fa
ES
7121
7122 begin
f937473f 7123 -- In the case of functions with unconstrained result subtypes,
9a1bc6d5
AC
7124 -- add a 4-state formal indicating whether the return object is
7125 -- allocated by the caller (1), or should be allocated by the
7126 -- callee on the secondary stack (2), in the global heap (3), or
7127 -- in a user-defined storage pool (4). For the moment we just use
7128 -- Natural for the type of this formal. Note that this formal
7129 -- isn't usually needed in the case where the result subtype is
7130 -- constrained, but it is needed when the function has a tagged
7131 -- result, because generally such functions can be called in a
7132 -- dispatching context and such calls must be handled like calls
7133 -- to a class-wide function.
0a36105d 7134
1bb6e262 7135 if Needs_BIP_Alloc_Form (E) then
f937473f
RD
7136 Discard :=
7137 Add_Extra_Formal
7138 (E, Standard_Natural,
7139 E, BIP_Formal_Suffix (BIP_Alloc_Form));
200b7162 7140
8417f4b2 7141 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
3e452820
AC
7142 -- use a user-defined pool. This formal is not added on
7143 -- .NET/JVM/ZFP as those targets do not support pools.
200b7162 7144
ea10ca9c
AC
7145 if VM_Target = No_VM
7146 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
3e452820 7147 then
8417f4b2
AC
7148 Discard :=
7149 Add_Extra_Formal
7150 (E, RTE (RE_Root_Storage_Pool_Ptr),
7151 E, BIP_Formal_Suffix (BIP_Storage_Pool));
7152 end if;
f937473f 7153 end if;
ec4867fa 7154
df3e68b1 7155 -- In the case of functions whose result type needs finalization,
ca5af305 7156 -- add an extra formal which represents the finalization master.
df3e68b1 7157
ca5af305 7158 if Needs_BIP_Finalization_Master (E) then
f937473f
RD
7159 Discard :=
7160 Add_Extra_Formal
ca5af305
AC
7161 (E, RTE (RE_Finalization_Master_Ptr),
7162 E, BIP_Formal_Suffix (BIP_Finalization_Master));
f937473f
RD
7163 end if;
7164
94bbf008
AC
7165 -- When the result type contains tasks, add two extra formals: the
7166 -- master of the tasks to be created, and the caller's activation
7167 -- chain.
f937473f 7168
1a36a0cd 7169 if Has_Task (Full_Subt) then
f937473f
RD
7170 Discard :=
7171 Add_Extra_Formal
7172 (E, RTE (RE_Master_Id),
af89615f 7173 E, BIP_Formal_Suffix (BIP_Task_Master));
f937473f
RD
7174 Discard :=
7175 Add_Extra_Formal
7176 (E, RTE (RE_Activation_Chain_Access),
7177 E, BIP_Formal_Suffix (BIP_Activation_Chain));
7178 end if;
ec4867fa 7179
f937473f
RD
7180 -- All build-in-place functions get an extra formal that will be
7181 -- passed the address of the return object within the caller.
ec4867fa 7182
1a36a0cd
AC
7183 Formal_Typ :=
7184 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
ec4867fa 7185
1a36a0cd
AC
7186 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
7187 Set_Etype (Formal_Typ, Formal_Typ);
7188 Set_Depends_On_Private
7189 (Formal_Typ, Has_Private_Component (Formal_Typ));
7190 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
7191 Set_Is_Access_Constant (Formal_Typ, False);
ec4867fa 7192
1a36a0cd
AC
7193 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
7194 -- the designated type comes from the limited view (for back-end
7195 -- purposes).
ec4867fa 7196
7b56a91b
AC
7197 Set_From_Limited_With
7198 (Formal_Typ, From_Limited_With (Result_Subt));
f937473f 7199
1a36a0cd
AC
7200 Layout_Type (Formal_Typ);
7201
7202 Discard :=
7203 Add_Extra_Formal
7204 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
ec4867fa
ES
7205 end;
7206 end if;
996ae0b0
RK
7207 end Create_Extra_Formals;
7208
7209 -----------------------------
7210 -- Enter_Overloaded_Entity --
7211 -----------------------------
7212
7213 procedure Enter_Overloaded_Entity (S : Entity_Id) is
7214 E : Entity_Id := Current_Entity_In_Scope (S);
7215 C_E : Entity_Id := Current_Entity (S);
7216
7217 begin
7218 if Present (E) then
7219 Set_Has_Homonym (E);
7220 Set_Has_Homonym (S);
7221 end if;
7222
7223 Set_Is_Immediately_Visible (S);
7224 Set_Scope (S, Current_Scope);
7225
7226 -- Chain new entity if front of homonym in current scope, so that
7227 -- homonyms are contiguous.
7228
8fde064e 7229 if Present (E) and then E /= C_E then
996ae0b0
RK
7230 while Homonym (C_E) /= E loop
7231 C_E := Homonym (C_E);
7232 end loop;
7233
7234 Set_Homonym (C_E, S);
7235
7236 else
7237 E := C_E;
7238 Set_Current_Entity (S);
7239 end if;
7240
7241 Set_Homonym (S, E);
7242
2352eadb
AC
7243 if Is_Inherited_Operation (S) then
7244 Append_Inherited_Subprogram (S);
7245 else
7246 Append_Entity (S, Current_Scope);
7247 end if;
7248
996ae0b0
RK
7249 Set_Public_Status (S);
7250
7251 if Debug_Flag_E then
7252 Write_Str ("New overloaded entity chain: ");
7253 Write_Name (Chars (S));
996ae0b0 7254
82c80734 7255 E := S;
996ae0b0
RK
7256 while Present (E) loop
7257 Write_Str (" "); Write_Int (Int (E));
7258 E := Homonym (E);
7259 end loop;
7260
7261 Write_Eol;
7262 end if;
7263
7264 -- Generate warning for hiding
7265
7266 if Warn_On_Hiding
7267 and then Comes_From_Source (S)
7268 and then In_Extended_Main_Source_Unit (S)
7269 then
7270 E := S;
7271 loop
7272 E := Homonym (E);
7273 exit when No (E);
7274
7fc53871
AC
7275 -- Warn unless genuine overloading. Do not emit warning on
7276 -- hiding predefined operators in Standard (these are either an
7277 -- (artifact of our implicit declarations, or simple noise) but
7278 -- keep warning on a operator defined on a local subtype, because
7279 -- of the real danger that different operators may be applied in
7280 -- various parts of the program.
996ae0b0 7281
1f250383
AC
7282 -- Note that if E and S have the same scope, there is never any
7283 -- hiding. Either the two conflict, and the program is illegal,
7284 -- or S is overriding an implicit inherited subprogram.
7285
7286 if Scope (E) /= Scope (S)
7287 and then (not Is_Overloadable (E)
8d606a78 7288 or else Subtype_Conformant (E, S))
f937473f
RD
7289 and then (Is_Immediately_Visible (E)
7290 or else
7291 Is_Potentially_Use_Visible (S))
996ae0b0 7292 then
7fc53871
AC
7293 if Scope (E) /= Standard_Standard then
7294 Error_Msg_Sloc := Sloc (E);
3ccedacc 7295 Error_Msg_N ("declaration of & hides one #?h?", S);
7fc53871
AC
7296
7297 elsif Nkind (S) = N_Defining_Operator_Symbol
7298 and then
1f250383 7299 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
7fc53871
AC
7300 then
7301 Error_Msg_N
dbfeb4fa 7302 ("declaration of & hides predefined operator?h?", S);
7fc53871 7303 end if;
996ae0b0
RK
7304 end if;
7305 end loop;
7306 end if;
7307 end Enter_Overloaded_Entity;
7308
e5a58fac
AC
7309 -----------------------------
7310 -- Check_Untagged_Equality --
7311 -----------------------------
7312
7313 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
7314 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
7315 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
7316 Obj_Decl : Node_Id;
7317
7318 begin
7c0c194b
AC
7319 -- This check applies only if we have a subprogram declaration with an
7320 -- untagged record type.
b2834fbd
AC
7321
7322 if Nkind (Decl) /= N_Subprogram_Declaration
7323 or else not Is_Record_Type (Typ)
7324 or else Is_Tagged_Type (Typ)
e5a58fac 7325 then
b2834fbd
AC
7326 return;
7327 end if;
e5a58fac 7328
b2834fbd
AC
7329 -- In Ada 2012 case, we will output errors or warnings depending on
7330 -- the setting of debug flag -gnatd.E.
7331
7332 if Ada_Version >= Ada_2012 then
7333 Error_Msg_Warn := Debug_Flag_Dot_EE;
7334
7335 -- In earlier versions of Ada, nothing to do unless we are warning on
7336 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
7337
7338 else
7339 if not Warn_On_Ada_2012_Compatibility then
7340 return;
7341 end if;
7342 end if;
7343
7344 -- Cases where the type has already been frozen
e5a58fac 7345
b2834fbd
AC
7346 if Is_Frozen (Typ) then
7347
7348 -- If the type is not declared in a package, or if we are in the body
7349 -- of the package or in some other scope, the new operation is not
7350 -- primitive, and therefore legal, though suspicious. Should we
7351 -- generate a warning in this case ???
7352
7353 if Ekind (Scope (Typ)) /= E_Package
7354 or else Scope (Typ) /= Current_Scope
7355 then
7356 return;
7357
7358 -- If the type is a generic actual (sub)type, the operation is not
7359 -- primitive either because the base type is declared elsewhere.
7360
7361 elsif Is_Generic_Actual_Type (Typ) then
7362 return;
7363
7364 -- Here we have a definite error of declaration after freezing
7365
7366 else
7367 if Ada_Version >= Ada_2012 then
ae6ede77 7368 Error_Msg_NE
3ccedacc 7369 ("equality operator must be declared before type & is "
b2834fbd
AC
7370 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
7371
7372 -- In Ada 2012 mode with error turned to warning, output one
7373 -- more warning to warn that the equality operation may not
7374 -- compose. This is the consequence of ignoring the error.
7375
7376 if Error_Msg_Warn then
7377 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
7378 end if;
21a5b575
AC
7379
7380 else
7381 Error_Msg_NE
b2834fbd
AC
7382 ("equality operator must be declared before type& is "
7383 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
7384 end if;
7385
7386 -- If we are in the package body, we could just move the
7387 -- declaration to the package spec, so add a message saying that.
7388
7389 if In_Package_Body (Scope (Typ)) then
7390 if Ada_Version >= Ada_2012 then
7391 Error_Msg_N
7392 ("\move declaration to package spec<<", Eq_Op);
7393 else
7394 Error_Msg_N
7395 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
7396 end if;
21a5b575 7397
b2834fbd
AC
7398 -- Otherwise try to find the freezing point
7399
7400 else
21a5b575 7401 Obj_Decl := Next (Parent (Typ));
dbfeb4fa 7402 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
21a5b575
AC
7403 if Nkind (Obj_Decl) = N_Object_Declaration
7404 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
7405 then
b2834fbd
AC
7406 -- Freezing point, output warnings
7407
7408 if Ada_Version >= Ada_2012 then
7409 Error_Msg_NE
7410 ("type& is frozen by declaration??", Obj_Decl, Typ);
7411 Error_Msg_N
7412 ("\an equality operator cannot be declared after "
7413 & "this point??",
7414 Obj_Decl);
7415 else
7416 Error_Msg_NE
7417 ("type& is frozen by declaration (Ada 2012)?y?",
7418 Obj_Decl, Typ);
7419 Error_Msg_N
7420 ("\an equality operator cannot be declared after "
7421 & "this point (Ada 2012)?y?",
7422 Obj_Decl);
7423 end if;
7424
21a5b575
AC
7425 exit;
7426 end if;
7427
7428 Next (Obj_Decl);
7429 end loop;
7430 end if;
b2834fbd 7431 end if;
e5a58fac 7432
b2834fbd
AC
7433 -- Here if type is not frozen yet. It is illegal to have a primitive
7434 -- equality declared in the private part if the type is visible.
21a5b575 7435
b2834fbd
AC
7436 elsif not In_Same_List (Parent (Typ), Decl)
7437 and then not Is_Limited_Type (Typ)
7438 then
7439 -- Shouldn't we give an RM reference here???
21a5b575 7440
b2834fbd
AC
7441 if Ada_Version >= Ada_2012 then
7442 Error_Msg_N
7443 ("equality operator appears too late<<", Eq_Op);
7444 else
7445 Error_Msg_N
7446 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
e5a58fac 7447 end if;
b2834fbd
AC
7448
7449 -- No error detected
7450
7451 else
7452 return;
e5a58fac
AC
7453 end if;
7454 end Check_Untagged_Equality;
7455
996ae0b0
RK
7456 -----------------------------
7457 -- Find_Corresponding_Spec --
7458 -----------------------------
7459
d44202ba
HK
7460 function Find_Corresponding_Spec
7461 (N : Node_Id;
7462 Post_Error : Boolean := True) return Entity_Id
7463 is
996ae0b0
RK
7464 Spec : constant Node_Id := Specification (N);
7465 Designator : constant Entity_Id := Defining_Entity (Spec);
7466
7467 E : Entity_Id;
7468
70f4ad20
AC
7469 function Different_Generic_Profile (E : Entity_Id) return Boolean;
7470 -- Even if fully conformant, a body may depend on a generic actual when
7471 -- the spec does not, or vice versa, in which case they were distinct
7472 -- entities in the generic.
7473
7474 -------------------------------
7475 -- Different_Generic_Profile --
7476 -------------------------------
7477
7478 function Different_Generic_Profile (E : Entity_Id) return Boolean is
7479 F1, F2 : Entity_Id;
7480
2995860f
AC
7481 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
7482 -- Check that the types of corresponding formals have the same
7483 -- generic actual if any. We have to account for subtypes of a
7484 -- generic formal, declared between a spec and a body, which may
73999267
AC
7485 -- appear distinct in an instance but matched in the generic, and
7486 -- the subtype may be used either in the spec or the body of the
7487 -- subprogram being checked.
2995860f
AC
7488
7489 -------------------------
7490 -- Same_Generic_Actual --
7491 -------------------------
7492
7493 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
73999267
AC
7494
7495 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
7496 -- Predicate to check whether S1 is a subtype of S2 in the source
7497 -- of the instance.
7498
7499 -------------------------
7500 -- Is_Declared_Subtype --
7501 -------------------------
7502
7503 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
7504 begin
7505 return Comes_From_Source (Parent (S1))
7506 and then Nkind (Parent (S1)) = N_Subtype_Declaration
7507 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
7508 and then Entity (Subtype_Indication (Parent (S1))) = S2;
7509 end Is_Declared_Subtype;
7510
7511 -- Start of processing for Same_Generic_Actual
7512
2995860f
AC
7513 begin
7514 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
73999267
AC
7515 or else Is_Declared_Subtype (T1, T2)
7516 or else Is_Declared_Subtype (T2, T1);
2995860f
AC
7517 end Same_Generic_Actual;
7518
7519 -- Start of processing for Different_Generic_Profile
7520
70f4ad20 7521 begin
2995860f
AC
7522 if not In_Instance then
7523 return False;
7524
7525 elsif Ekind (E) = E_Function
7526 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
70f4ad20
AC
7527 then
7528 return True;
7529 end if;
7530
7531 F1 := First_Formal (Designator);
7532 F2 := First_Formal (E);
70f4ad20 7533 while Present (F1) loop
2995860f 7534 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
70f4ad20
AC
7535 return True;
7536 end if;
7537
7538 Next_Formal (F1);
7539 Next_Formal (F2);
7540 end loop;
7541
7542 return False;
7543 end Different_Generic_Profile;
7544
7545 -- Start of processing for Find_Corresponding_Spec
7546
996ae0b0
RK
7547 begin
7548 E := Current_Entity (Designator);
996ae0b0
RK
7549 while Present (E) loop
7550
7551 -- We are looking for a matching spec. It must have the same scope,
7552 -- and the same name, and either be type conformant, or be the case
7553 -- of a library procedure spec and its body (which belong to one
7554 -- another regardless of whether they are type conformant or not).
7555
7556 if Scope (E) = Current_Scope then
fbf5a39b
AC
7557 if Current_Scope = Standard_Standard
7558 or else (Ekind (E) = Ekind (Designator)
586ecbf3 7559 and then Type_Conformant (E, Designator))
996ae0b0
RK
7560 then
7561 -- Within an instantiation, we know that spec and body are
70f4ad20
AC
7562 -- subtype conformant, because they were subtype conformant in
7563 -- the generic. We choose the subtype-conformant entity here as
7564 -- well, to resolve spurious ambiguities in the instance that
7565 -- were not present in the generic (i.e. when two different
7566 -- types are given the same actual). If we are looking for a
7567 -- spec to match a body, full conformance is expected.
996ae0b0
RK
7568
7569 if In_Instance then
c05ba1f1
AC
7570
7571 -- Inherit the convention and "ghostness" of the matching
7572 -- spec to ensure proper full and subtype conformance.
7573
996ae0b0
RK
7574 Set_Convention (Designator, Convention (E));
7575
c05ba1f1
AC
7576 if Is_Ghost_Entity (E) then
7577 Set_Is_Ghost_Entity (Designator);
7578 end if;
7579
0187b60e
AC
7580 -- Skip past subprogram bodies and subprogram renamings that
7581 -- may appear to have a matching spec, but that aren't fully
7582 -- conformant with it. That can occur in cases where an
7583 -- actual type causes unrelated homographs in the instance.
7584
7585 if Nkind_In (N, N_Subprogram_Body,
7586 N_Subprogram_Renaming_Declaration)
996ae0b0 7587 and then Present (Homonym (E))
c7b9d548 7588 and then not Fully_Conformant (Designator, E)
996ae0b0
RK
7589 then
7590 goto Next_Entity;
7591
c7b9d548 7592 elsif not Subtype_Conformant (Designator, E) then
996ae0b0 7593 goto Next_Entity;
70f4ad20
AC
7594
7595 elsif Different_Generic_Profile (E) then
7596 goto Next_Entity;
996ae0b0
RK
7597 end if;
7598 end if;
7599
25ebc085
AC
7600 -- Ada 2012 (AI05-0165): For internally generated bodies of
7601 -- null procedures locate the internally generated spec. We
7602 -- enforce mode conformance since a tagged type may inherit
7603 -- from interfaces several null primitives which differ only
7604 -- in the mode of the formals.
7605
7606 if not (Comes_From_Source (E))
7607 and then Is_Null_Procedure (E)
7608 and then not Mode_Conformant (Designator, E)
7609 then
7610 null;
7611
4d8f3296
ES
7612 -- For null procedures coming from source that are completions,
7613 -- analysis of the generated body will establish the link.
7614
7615 elsif Comes_From_Source (E)
7616 and then Nkind (Spec) = N_Procedure_Specification
7617 and then Null_Present (Spec)
7618 then
7619 return E;
7620
25ebc085 7621 elsif not Has_Completion (E) then
996ae0b0
RK
7622 if Nkind (N) /= N_Subprogram_Body_Stub then
7623 Set_Corresponding_Spec (N, E);
7624 end if;
7625
7626 Set_Has_Completion (E);
7627 return E;
7628
7629 elsif Nkind (Parent (N)) = N_Subunit then
7630
7631 -- If this is the proper body of a subunit, the completion
7632 -- flag is set when analyzing the stub.
7633
7634 return E;
7635
70f4ad20
AC
7636 -- If E is an internal function with a controlling result that
7637 -- was created for an operation inherited by a null extension,
7638 -- it may be overridden by a body without a previous spec (one
2995860f 7639 -- more reason why these should be shunned). In that case we
70f4ad20
AC
7640 -- remove the generated body if present, because the current
7641 -- one is the explicit overriding.
81db9d77
ES
7642
7643 elsif Ekind (E) = E_Function
0791fbe9 7644 and then Ada_Version >= Ada_2005
81db9d77
ES
7645 and then not Comes_From_Source (E)
7646 and then Has_Controlling_Result (E)
7647 and then Is_Null_Extension (Etype (E))
7648 and then Comes_From_Source (Spec)
7649 then
7650 Set_Has_Completion (E, False);
7651
1366997b
AC
7652 if Expander_Active
7653 and then Nkind (Parent (E)) = N_Function_Specification
7654 then
81db9d77
ES
7655 Remove
7656 (Unit_Declaration_Node
1366997b
AC
7657 (Corresponding_Body (Unit_Declaration_Node (E))));
7658
81db9d77
ES
7659 return E;
7660
1366997b
AC
7661 -- If expansion is disabled, or if the wrapper function has
7662 -- not been generated yet, this a late body overriding an
7663 -- inherited operation, or it is an overriding by some other
7664 -- declaration before the controlling result is frozen. In
7665 -- either case this is a declaration of a new entity.
81db9d77
ES
7666
7667 else
7668 return Empty;
7669 end if;
7670
d44202ba
HK
7671 -- If the body already exists, then this is an error unless
7672 -- the previous declaration is the implicit declaration of a
756ef2a0
AC
7673 -- derived subprogram. It is also legal for an instance to
7674 -- contain type conformant overloadable declarations (but the
7675 -- generic declaration may not), per 8.3(26/2).
996ae0b0
RK
7676
7677 elsif No (Alias (E))
7678 and then not Is_Intrinsic_Subprogram (E)
7679 and then not In_Instance
d44202ba 7680 and then Post_Error
996ae0b0
RK
7681 then
7682 Error_Msg_Sloc := Sloc (E);
8dbd1460 7683
07fc65c4
GB
7684 if Is_Imported (E) then
7685 Error_Msg_NE
7686 ("body not allowed for imported subprogram & declared#",
7687 N, E);
7688 else
7689 Error_Msg_NE ("duplicate body for & declared#", N, E);
7690 end if;
996ae0b0
RK
7691 end if;
7692
d44202ba
HK
7693 -- Child units cannot be overloaded, so a conformance mismatch
7694 -- between body and a previous spec is an error.
7695
996ae0b0
RK
7696 elsif Is_Child_Unit (E)
7697 and then
7698 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
7699 and then
5d37ba92 7700 Nkind (Parent (Unit_Declaration_Node (Designator))) =
d44202ba
HK
7701 N_Compilation_Unit
7702 and then Post_Error
996ae0b0 7703 then
996ae0b0
RK
7704 Error_Msg_N
7705 ("body of child unit does not match previous declaration", N);
7706 end if;
7707 end if;
7708
7709 <<Next_Entity>>
7710 E := Homonym (E);
7711 end loop;
7712
7713 -- On exit, we know that no previous declaration of subprogram exists
7714
7715 return Empty;
7716 end Find_Corresponding_Spec;
7717
7718 ----------------------
7719 -- Fully_Conformant --
7720 ----------------------
7721
7722 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
7723 Result : Boolean;
996ae0b0
RK
7724 begin
7725 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
7726 return Result;
7727 end Fully_Conformant;
7728
7729 ----------------------------------
7730 -- Fully_Conformant_Expressions --
7731 ----------------------------------
7732
7733 function Fully_Conformant_Expressions
7734 (Given_E1 : Node_Id;
d05ef0ab 7735 Given_E2 : Node_Id) return Boolean
996ae0b0
RK
7736 is
7737 E1 : constant Node_Id := Original_Node (Given_E1);
7738 E2 : constant Node_Id := Original_Node (Given_E2);
7739 -- We always test conformance on original nodes, since it is possible
7740 -- for analysis and/or expansion to make things look as though they
7741 -- conform when they do not, e.g. by converting 1+2 into 3.
7742
7743 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
7744 renames Fully_Conformant_Expressions;
7745
7746 function FCL (L1, L2 : List_Id) return Boolean;
70f4ad20
AC
7747 -- Compare elements of two lists for conformance. Elements have to be
7748 -- conformant, and actuals inserted as default parameters do not match
7749 -- explicit actuals with the same value.
996ae0b0
RK
7750
7751 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
e895b435 7752 -- Compare an operator node with a function call
996ae0b0
RK
7753
7754 ---------
7755 -- FCL --
7756 ---------
7757
7758 function FCL (L1, L2 : List_Id) return Boolean is
7759 N1, N2 : Node_Id;
7760
7761 begin
7762 if L1 = No_List then
7763 N1 := Empty;
7764 else
7765 N1 := First (L1);
7766 end if;
7767
7768 if L2 = No_List then
7769 N2 := Empty;
7770 else
7771 N2 := First (L2);
7772 end if;
7773
70f4ad20 7774 -- Compare two lists, skipping rewrite insertions (we want to compare
a90bd866 7775 -- the original trees, not the expanded versions).
996ae0b0
RK
7776
7777 loop
7778 if Is_Rewrite_Insertion (N1) then
7779 Next (N1);
7780 elsif Is_Rewrite_Insertion (N2) then
7781 Next (N2);
7782 elsif No (N1) then
7783 return No (N2);
7784 elsif No (N2) then
7785 return False;
7786 elsif not FCE (N1, N2) then
7787 return False;
7788 else
7789 Next (N1);
7790 Next (N2);
7791 end if;
7792 end loop;
7793 end FCL;
7794
7795 ---------
7796 -- FCO --
7797 ---------
7798
7799 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
7800 Actuals : constant List_Id := Parameter_Associations (Call_Node);
7801 Act : Node_Id;
7802
7803 begin
7804 if No (Actuals)
7805 or else Entity (Op_Node) /= Entity (Name (Call_Node))
7806 then
7807 return False;
7808
7809 else
7810 Act := First (Actuals);
7811
7812 if Nkind (Op_Node) in N_Binary_Op then
996ae0b0
RK
7813 if not FCE (Left_Opnd (Op_Node), Act) then
7814 return False;
7815 end if;
7816
7817 Next (Act);
7818 end if;
7819
7820 return Present (Act)
7821 and then FCE (Right_Opnd (Op_Node), Act)
7822 and then No (Next (Act));
7823 end if;
7824 end FCO;
7825
7826 -- Start of processing for Fully_Conformant_Expressions
7827
7828 begin
7829 -- Non-conformant if paren count does not match. Note: if some idiot
7830 -- complains that we don't do this right for more than 3 levels of
a90bd866 7831 -- parentheses, they will be treated with the respect they deserve.
996ae0b0
RK
7832
7833 if Paren_Count (E1) /= Paren_Count (E2) then
7834 return False;
7835
82c80734
RD
7836 -- If same entities are referenced, then they are conformant even if
7837 -- they have different forms (RM 8.3.1(19-20)).
996ae0b0
RK
7838
7839 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
7840 if Present (Entity (E1)) then
7841 return Entity (E1) = Entity (E2)
7842 or else (Chars (Entity (E1)) = Chars (Entity (E2))
7843 and then Ekind (Entity (E1)) = E_Discriminant
7844 and then Ekind (Entity (E2)) = E_In_Parameter);
7845
7846 elsif Nkind (E1) = N_Expanded_Name
7847 and then Nkind (E2) = N_Expanded_Name
7848 and then Nkind (Selector_Name (E1)) = N_Character_Literal
7849 and then Nkind (Selector_Name (E2)) = N_Character_Literal
7850 then
7851 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
7852
7853 else
7854 -- Identifiers in component associations don't always have
7855 -- entities, but their names must conform.
7856
7857 return Nkind (E1) = N_Identifier
7858 and then Nkind (E2) = N_Identifier
7859 and then Chars (E1) = Chars (E2);
7860 end if;
7861
7862 elsif Nkind (E1) = N_Character_Literal
7863 and then Nkind (E2) = N_Expanded_Name
7864 then
7865 return Nkind (Selector_Name (E2)) = N_Character_Literal
7866 and then Chars (E1) = Chars (Selector_Name (E2));
7867
7868 elsif Nkind (E2) = N_Character_Literal
7869 and then Nkind (E1) = N_Expanded_Name
7870 then
7871 return Nkind (Selector_Name (E1)) = N_Character_Literal
7872 and then Chars (E2) = Chars (Selector_Name (E1));
7873
8fde064e 7874 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
996ae0b0
RK
7875 return FCO (E1, E2);
7876
8fde064e 7877 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
996ae0b0
RK
7878 return FCO (E2, E1);
7879
7880 -- Otherwise we must have the same syntactic entity
7881
7882 elsif Nkind (E1) /= Nkind (E2) then
7883 return False;
7884
7885 -- At this point, we specialize by node type
7886
7887 else
7888 case Nkind (E1) is
7889
7890 when N_Aggregate =>
7891 return
7892 FCL (Expressions (E1), Expressions (E2))
19d846a0
RD
7893 and then
7894 FCL (Component_Associations (E1),
7895 Component_Associations (E2));
996ae0b0
RK
7896
7897 when N_Allocator =>
7898 if Nkind (Expression (E1)) = N_Qualified_Expression
7899 or else
7900 Nkind (Expression (E2)) = N_Qualified_Expression
7901 then
7902 return FCE (Expression (E1), Expression (E2));
7903
7904 -- Check that the subtype marks and any constraints
7905 -- are conformant
7906
7907 else
7908 declare
7909 Indic1 : constant Node_Id := Expression (E1);
7910 Indic2 : constant Node_Id := Expression (E2);
7911 Elt1 : Node_Id;
7912 Elt2 : Node_Id;
7913
7914 begin
7915 if Nkind (Indic1) /= N_Subtype_Indication then
7916 return
7917 Nkind (Indic2) /= N_Subtype_Indication
7918 and then Entity (Indic1) = Entity (Indic2);
7919
7920 elsif Nkind (Indic2) /= N_Subtype_Indication then
7921 return
7922 Nkind (Indic1) /= N_Subtype_Indication
7923 and then Entity (Indic1) = Entity (Indic2);
7924
7925 else
7926 if Entity (Subtype_Mark (Indic1)) /=
7927 Entity (Subtype_Mark (Indic2))
7928 then
7929 return False;
7930 end if;
7931
7932 Elt1 := First (Constraints (Constraint (Indic1)));
7933 Elt2 := First (Constraints (Constraint (Indic2)));
996ae0b0
RK
7934 while Present (Elt1) and then Present (Elt2) loop
7935 if not FCE (Elt1, Elt2) then
7936 return False;
7937 end if;
7938
7939 Next (Elt1);
7940 Next (Elt2);
7941 end loop;
7942
7943 return True;
7944 end if;
7945 end;
7946 end if;
7947
7948 when N_Attribute_Reference =>
7949 return
7950 Attribute_Name (E1) = Attribute_Name (E2)
7951 and then FCL (Expressions (E1), Expressions (E2));
7952
7953 when N_Binary_Op =>
7954 return
7955 Entity (E1) = Entity (E2)
7956 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
7957 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
7958
514d0fc5 7959 when N_Short_Circuit | N_Membership_Test =>
996ae0b0
RK
7960 return
7961 FCE (Left_Opnd (E1), Left_Opnd (E2))
7962 and then
7963 FCE (Right_Opnd (E1), Right_Opnd (E2));
7964
19d846a0
RD
7965 when N_Case_Expression =>
7966 declare
7967 Alt1 : Node_Id;
7968 Alt2 : Node_Id;
7969
7970 begin
7971 if not FCE (Expression (E1), Expression (E2)) then
7972 return False;
7973
7974 else
7975 Alt1 := First (Alternatives (E1));
7976 Alt2 := First (Alternatives (E2));
7977 loop
7978 if Present (Alt1) /= Present (Alt2) then
7979 return False;
7980 elsif No (Alt1) then
7981 return True;
7982 end if;
7983
7984 if not FCE (Expression (Alt1), Expression (Alt2))
7985 or else not FCL (Discrete_Choices (Alt1),
7986 Discrete_Choices (Alt2))
7987 then
7988 return False;
7989 end if;
7990
7991 Next (Alt1);
7992 Next (Alt2);
7993 end loop;
7994 end if;
7995 end;
7996
996ae0b0
RK
7997 when N_Character_Literal =>
7998 return
7999 Char_Literal_Value (E1) = Char_Literal_Value (E2);
8000
8001 when N_Component_Association =>
8002 return
8003 FCL (Choices (E1), Choices (E2))
19d846a0
RD
8004 and then
8005 FCE (Expression (E1), Expression (E2));
996ae0b0 8006
996ae0b0
RK
8007 when N_Explicit_Dereference =>
8008 return
8009 FCE (Prefix (E1), Prefix (E2));
8010
8011 when N_Extension_Aggregate =>
8012 return
8013 FCL (Expressions (E1), Expressions (E2))
8014 and then Null_Record_Present (E1) =
8015 Null_Record_Present (E2)
8016 and then FCL (Component_Associations (E1),
8017 Component_Associations (E2));
8018
8019 when N_Function_Call =>
8020 return
8021 FCE (Name (E1), Name (E2))
19d846a0
RD
8022 and then
8023 FCL (Parameter_Associations (E1),
8024 Parameter_Associations (E2));
996ae0b0 8025
9b16cb57
RD
8026 when N_If_Expression =>
8027 return
8028 FCL (Expressions (E1), Expressions (E2));
8029
996ae0b0
RK
8030 when N_Indexed_Component =>
8031 return
8032 FCE (Prefix (E1), Prefix (E2))
19d846a0
RD
8033 and then
8034 FCL (Expressions (E1), Expressions (E2));
996ae0b0
RK
8035
8036 when N_Integer_Literal =>
8037 return (Intval (E1) = Intval (E2));
8038
8039 when N_Null =>
8040 return True;
8041
8042 when N_Operator_Symbol =>
8043 return
8044 Chars (E1) = Chars (E2);
8045
8046 when N_Others_Choice =>
8047 return True;
8048
8049 when N_Parameter_Association =>
8050 return
996ae0b0
RK
8051 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
8052 and then FCE (Explicit_Actual_Parameter (E1),
8053 Explicit_Actual_Parameter (E2));
8054
8055 when N_Qualified_Expression =>
8056 return
8057 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
19d846a0
RD
8058 and then
8059 FCE (Expression (E1), Expression (E2));
996ae0b0 8060
2010d078
AC
8061 when N_Quantified_Expression =>
8062 if not FCE (Condition (E1), Condition (E2)) then
8063 return False;
8064 end if;
8065
8066 if Present (Loop_Parameter_Specification (E1))
8067 and then Present (Loop_Parameter_Specification (E2))
8068 then
8069 declare
8070 L1 : constant Node_Id :=
8071 Loop_Parameter_Specification (E1);
8072 L2 : constant Node_Id :=
8073 Loop_Parameter_Specification (E2);
8074
8075 begin
8076 return
8077 Reverse_Present (L1) = Reverse_Present (L2)
8078 and then
8079 FCE (Defining_Identifier (L1),
8080 Defining_Identifier (L2))
8081 and then
8082 FCE (Discrete_Subtype_Definition (L1),
8083 Discrete_Subtype_Definition (L2));
8084 end;
8085
804670f1
AC
8086 elsif Present (Iterator_Specification (E1))
8087 and then Present (Iterator_Specification (E2))
8088 then
2010d078
AC
8089 declare
8090 I1 : constant Node_Id := Iterator_Specification (E1);
8091 I2 : constant Node_Id := Iterator_Specification (E2);
8092
8093 begin
8094 return
8095 FCE (Defining_Identifier (I1),
8096 Defining_Identifier (I2))
8097 and then
8098 Of_Present (I1) = Of_Present (I2)
8099 and then
8100 Reverse_Present (I1) = Reverse_Present (I2)
8101 and then FCE (Name (I1), Name (I2))
8102 and then FCE (Subtype_Indication (I1),
8103 Subtype_Indication (I2));
8104 end;
804670f1
AC
8105
8106 -- The quantified expressions used different specifications to
8107 -- walk their respective ranges.
8108
8109 else
8110 return False;
2010d078
AC
8111 end if;
8112
996ae0b0
RK
8113 when N_Range =>
8114 return
8115 FCE (Low_Bound (E1), Low_Bound (E2))
19d846a0
RD
8116 and then
8117 FCE (High_Bound (E1), High_Bound (E2));
996ae0b0
RK
8118
8119 when N_Real_Literal =>
8120 return (Realval (E1) = Realval (E2));
8121
8122 when N_Selected_Component =>
8123 return
8124 FCE (Prefix (E1), Prefix (E2))
19d846a0
RD
8125 and then
8126 FCE (Selector_Name (E1), Selector_Name (E2));
996ae0b0
RK
8127
8128 when N_Slice =>
8129 return
8130 FCE (Prefix (E1), Prefix (E2))
19d846a0
RD
8131 and then
8132 FCE (Discrete_Range (E1), Discrete_Range (E2));
996ae0b0
RK
8133
8134 when N_String_Literal =>
8135 declare
8136 S1 : constant String_Id := Strval (E1);
8137 S2 : constant String_Id := Strval (E2);
8138 L1 : constant Nat := String_Length (S1);
8139 L2 : constant Nat := String_Length (S2);
8140
8141 begin
8142 if L1 /= L2 then
8143 return False;
8144
8145 else
8146 for J in 1 .. L1 loop
8147 if Get_String_Char (S1, J) /=
8148 Get_String_Char (S2, J)
8149 then
8150 return False;
8151 end if;
8152 end loop;
8153
8154 return True;
8155 end if;
8156 end;
8157
8158 when N_Type_Conversion =>
8159 return
8160 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
19d846a0
RD
8161 and then
8162 FCE (Expression (E1), Expression (E2));
996ae0b0
RK
8163
8164 when N_Unary_Op =>
8165 return
8166 Entity (E1) = Entity (E2)
19d846a0
RD
8167 and then
8168 FCE (Right_Opnd (E1), Right_Opnd (E2));
996ae0b0
RK
8169
8170 when N_Unchecked_Type_Conversion =>
8171 return
8172 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
19d846a0
RD
8173 and then
8174 FCE (Expression (E1), Expression (E2));
996ae0b0
RK
8175
8176 -- All other node types cannot appear in this context. Strictly
8177 -- we should raise a fatal internal error. Instead we just ignore
8178 -- the nodes. This means that if anyone makes a mistake in the
2995860f
AC
8179 -- expander and mucks an expression tree irretrievably, the result
8180 -- will be a failure to detect a (probably very obscure) case
8181 -- of non-conformance, which is better than bombing on some
996ae0b0
RK
8182 -- case where two expressions do in fact conform.
8183
8184 when others =>
8185 return True;
8186
8187 end case;
8188 end if;
8189 end Fully_Conformant_Expressions;
8190
fbf5a39b
AC
8191 ----------------------------------------
8192 -- Fully_Conformant_Discrete_Subtypes --
8193 ----------------------------------------
8194
8195 function Fully_Conformant_Discrete_Subtypes
8196 (Given_S1 : Node_Id;
d05ef0ab 8197 Given_S2 : Node_Id) return Boolean
fbf5a39b
AC
8198 is
8199 S1 : constant Node_Id := Original_Node (Given_S1);
8200 S2 : constant Node_Id := Original_Node (Given_S2);
8201
8202 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
82c80734
RD
8203 -- Special-case for a bound given by a discriminant, which in the body
8204 -- is replaced with the discriminal of the enclosing type.
fbf5a39b
AC
8205
8206 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
e895b435 8207 -- Check both bounds
fbf5a39b 8208
5d37ba92
ES
8209 -----------------------
8210 -- Conforming_Bounds --
8211 -----------------------
8212
fbf5a39b
AC
8213 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
8214 begin
8215 if Is_Entity_Name (B1)
8216 and then Is_Entity_Name (B2)
8217 and then Ekind (Entity (B1)) = E_Discriminant
8218 then
8219 return Chars (B1) = Chars (B2);
8220
8221 else
8222 return Fully_Conformant_Expressions (B1, B2);
8223 end if;
8224 end Conforming_Bounds;
8225
5d37ba92
ES
8226 -----------------------
8227 -- Conforming_Ranges --
8228 -----------------------
8229
fbf5a39b
AC
8230 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
8231 begin
8232 return
8233 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
8234 and then
8235 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
8236 end Conforming_Ranges;
8237
8238 -- Start of processing for Fully_Conformant_Discrete_Subtypes
8239
8240 begin
8241 if Nkind (S1) /= Nkind (S2) then
8242 return False;
8243
8244 elsif Is_Entity_Name (S1) then
8245 return Entity (S1) = Entity (S2);
8246
8247 elsif Nkind (S1) = N_Range then
8248 return Conforming_Ranges (S1, S2);
8249
8250 elsif Nkind (S1) = N_Subtype_Indication then
8251 return
8252 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
8253 and then
8254 Conforming_Ranges
8255 (Range_Expression (Constraint (S1)),
8256 Range_Expression (Constraint (S2)));
8257 else
8258 return True;
8259 end if;
8260 end Fully_Conformant_Discrete_Subtypes;
8261
996ae0b0
RK
8262 --------------------
8263 -- Install_Entity --
8264 --------------------
8265
8266 procedure Install_Entity (E : Entity_Id) is
8267 Prev : constant Entity_Id := Current_Entity (E);
996ae0b0
RK
8268 begin
8269 Set_Is_Immediately_Visible (E);
8270 Set_Current_Entity (E);
8271 Set_Homonym (E, Prev);
8272 end Install_Entity;
8273
8274 ---------------------
8275 -- Install_Formals --
8276 ---------------------
8277
8278 procedure Install_Formals (Id : Entity_Id) is
8279 F : Entity_Id;
996ae0b0
RK
8280 begin
8281 F := First_Formal (Id);
996ae0b0
RK
8282 while Present (F) loop
8283 Install_Entity (F);
8284 Next_Formal (F);
8285 end loop;
8286 end Install_Formals;
8287
ce2b6ba5
JM
8288 -----------------------------
8289 -- Is_Interface_Conformant --
8290 -----------------------------
8291
8292 function Is_Interface_Conformant
8293 (Tagged_Type : Entity_Id;
8294 Iface_Prim : Entity_Id;
8295 Prim : Entity_Id) return Boolean
8296 is
9e92ad49
AC
8297 -- The operation may in fact be an inherited (implicit) operation
8298 -- rather than the original interface primitive, so retrieve the
8299 -- ultimate ancestor.
8300
8301 Iface : constant Entity_Id :=
8302 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
fceeaab6
ES
8303 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
8304
25ebc085
AC
8305 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
8306 -- Return the controlling formal of Prim
8307
59e6b23c
AC
8308 ------------------------
8309 -- Controlling_Formal --
8310 ------------------------
8311
25ebc085 8312 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
15918371 8313 E : Entity_Id;
59e6b23c 8314
25ebc085 8315 begin
15918371 8316 E := First_Entity (Prim);
25ebc085
AC
8317 while Present (E) loop
8318 if Is_Formal (E) and then Is_Controlling_Formal (E) then
8319 return E;
8320 end if;
8321
8322 Next_Entity (E);
8323 end loop;
8324
8325 return Empty;
8326 end Controlling_Formal;
8327
8328 -- Local variables
8329
8330 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
8331 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
8332
8333 -- Start of processing for Is_Interface_Conformant
8334
ce2b6ba5
JM
8335 begin
8336 pragma Assert (Is_Subprogram (Iface_Prim)
8337 and then Is_Subprogram (Prim)
8338 and then Is_Dispatching_Operation (Iface_Prim)
8339 and then Is_Dispatching_Operation (Prim));
8340
fceeaab6 8341 pragma Assert (Is_Interface (Iface)
ce2b6ba5
JM
8342 or else (Present (Alias (Iface_Prim))
8343 and then
8344 Is_Interface
8345 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
8346
8347 if Prim = Iface_Prim
8348 or else not Is_Subprogram (Prim)
8349 or else Ekind (Prim) /= Ekind (Iface_Prim)
8350 or else not Is_Dispatching_Operation (Prim)
8351 or else Scope (Prim) /= Scope (Tagged_Type)
fceeaab6 8352 or else No (Typ)
8a49a499 8353 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
ce2b6ba5
JM
8354 or else not Primitive_Names_Match (Iface_Prim, Prim)
8355 then
8356 return False;
8357
25ebc085
AC
8358 -- The mode of the controlling formals must match
8359
8360 elsif Present (Iface_Ctrl_F)
15918371
AC
8361 and then Present (Prim_Ctrl_F)
8362 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
25ebc085
AC
8363 then
8364 return False;
8365
8366 -- Case of a procedure, or a function whose result type matches the
8367 -- result type of the interface primitive, or a function that has no
8368 -- controlling result (I or access I).
ce2b6ba5
JM
8369
8370 elsif Ekind (Iface_Prim) = E_Procedure
8371 or else Etype (Prim) = Etype (Iface_Prim)
fceeaab6 8372 or else not Has_Controlling_Result (Prim)
ce2b6ba5 8373 then
b4d7b435
AC
8374 return Type_Conformant
8375 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
ce2b6ba5 8376
2995860f
AC
8377 -- Case of a function returning an interface, or an access to one. Check
8378 -- that the return types correspond.
ce2b6ba5 8379
fceeaab6
ES
8380 elsif Implements_Interface (Typ, Iface) then
8381 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
9a3c9940
RD
8382 /=
8383 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
fceeaab6
ES
8384 then
8385 return False;
fceeaab6
ES
8386 else
8387 return
9e92ad49 8388 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
ce2b6ba5 8389 Skip_Controlling_Formals => True);
fceeaab6 8390 end if;
ce2b6ba5 8391
fceeaab6
ES
8392 else
8393 return False;
ce2b6ba5 8394 end if;
ce2b6ba5
JM
8395 end Is_Interface_Conformant;
8396
996ae0b0
RK
8397 ---------------------------------
8398 -- Is_Non_Overriding_Operation --
8399 ---------------------------------
8400
8401 function Is_Non_Overriding_Operation
8402 (Prev_E : Entity_Id;
d05ef0ab 8403 New_E : Entity_Id) return Boolean
996ae0b0
RK
8404 is
8405 Formal : Entity_Id;
8406 F_Typ : Entity_Id;
8407 G_Typ : Entity_Id := Empty;
8408
8409 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
5d37ba92
ES
8410 -- If F_Type is a derived type associated with a generic actual subtype,
8411 -- then return its Generic_Parent_Type attribute, else return Empty.
996ae0b0
RK
8412
8413 function Types_Correspond
8414 (P_Type : Entity_Id;
d05ef0ab 8415 N_Type : Entity_Id) return Boolean;
82c80734
RD
8416 -- Returns true if and only if the types (or designated types in the
8417 -- case of anonymous access types) are the same or N_Type is derived
8418 -- directly or indirectly from P_Type.
996ae0b0
RK
8419
8420 -----------------------------
8421 -- Get_Generic_Parent_Type --
8422 -----------------------------
8423
8424 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
8425 G_Typ : Entity_Id;
702d2020 8426 Defn : Node_Id;
996ae0b0
RK
8427 Indic : Node_Id;
8428
8429 begin
8430 if Is_Derived_Type (F_Typ)
8431 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
8432 then
82c80734
RD
8433 -- The tree must be traversed to determine the parent subtype in
8434 -- the generic unit, which unfortunately isn't always available
8435 -- via semantic attributes. ??? (Note: The use of Original_Node
8436 -- is needed for cases where a full derived type has been
8437 -- rewritten.)
996ae0b0 8438
702d2020
AC
8439 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
8440 if Nkind (Defn) = N_Derived_Type_Definition then
8441 Indic := Subtype_Indication (Defn);
996ae0b0 8442
702d2020
AC
8443 if Nkind (Indic) = N_Subtype_Indication then
8444 G_Typ := Entity (Subtype_Mark (Indic));
8445 else
8446 G_Typ := Entity (Indic);
8447 end if;
996ae0b0 8448
702d2020
AC
8449 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
8450 and then Present (Generic_Parent_Type (Parent (G_Typ)))
8451 then
8452 return Generic_Parent_Type (Parent (G_Typ));
8453 end if;
996ae0b0
RK
8454 end if;
8455 end if;
8456
8457 return Empty;
8458 end Get_Generic_Parent_Type;
8459
8460 ----------------------
8461 -- Types_Correspond --
8462 ----------------------
8463
8464 function Types_Correspond
8465 (P_Type : Entity_Id;
d05ef0ab 8466 N_Type : Entity_Id) return Boolean
996ae0b0
RK
8467 is
8468 Prev_Type : Entity_Id := Base_Type (P_Type);
8469 New_Type : Entity_Id := Base_Type (N_Type);
8470
8471 begin
8472 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
8473 Prev_Type := Designated_Type (Prev_Type);
8474 end if;
8475
8476 if Ekind (New_Type) = E_Anonymous_Access_Type then
8477 New_Type := Designated_Type (New_Type);
8478 end if;
8479
8480 if Prev_Type = New_Type then
8481 return True;
8482
8483 elsif not Is_Class_Wide_Type (New_Type) then
8484 while Etype (New_Type) /= New_Type loop
8485 New_Type := Etype (New_Type);
8486 if New_Type = Prev_Type then
8487 return True;
8488 end if;
8489 end loop;
8490 end if;
8491 return False;
8492 end Types_Correspond;
8493
8494 -- Start of processing for Is_Non_Overriding_Operation
8495
8496 begin
82c80734
RD
8497 -- In the case where both operations are implicit derived subprograms
8498 -- then neither overrides the other. This can only occur in certain
8499 -- obscure cases (e.g., derivation from homographs created in a generic
8500 -- instantiation).
996ae0b0
RK
8501
8502 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
8503 return True;
8504
8505 elsif Ekind (Current_Scope) = E_Package
8506 and then Is_Generic_Instance (Current_Scope)
8507 and then In_Private_Part (Current_Scope)
8508 and then Comes_From_Source (New_E)
8509 then
702d2020
AC
8510 -- We examine the formals and result type of the inherited operation,
8511 -- to determine whether their type is derived from (the instance of)
8512 -- a generic type. The first such formal or result type is the one
8513 -- tested.
996ae0b0
RK
8514
8515 Formal := First_Formal (Prev_E);
996ae0b0
RK
8516 while Present (Formal) loop
8517 F_Typ := Base_Type (Etype (Formal));
8518
8519 if Ekind (F_Typ) = E_Anonymous_Access_Type then
8520 F_Typ := Designated_Type (F_Typ);
8521 end if;
8522
8523 G_Typ := Get_Generic_Parent_Type (F_Typ);
702d2020 8524 exit when Present (G_Typ);
996ae0b0
RK
8525
8526 Next_Formal (Formal);
8527 end loop;
8528
c8ef728f 8529 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
996ae0b0
RK
8530 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
8531 end if;
8532
8533 if No (G_Typ) then
8534 return False;
8535 end if;
8536
8dbd1460
AC
8537 -- If the generic type is a private type, then the original operation
8538 -- was not overriding in the generic, because there was no primitive
8539 -- operation to override.
996ae0b0
RK
8540
8541 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
8542 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
8dbd1460 8543 N_Formal_Private_Type_Definition
996ae0b0
RK
8544 then
8545 return True;
8546
8547 -- The generic parent type is the ancestor of a formal derived
8548 -- type declaration. We need to check whether it has a primitive
8549 -- operation that should be overridden by New_E in the generic.
8550
8551 else
8552 declare
8553 P_Formal : Entity_Id;
8554 N_Formal : Entity_Id;
8555 P_Typ : Entity_Id;
8556 N_Typ : Entity_Id;
8557 P_Prim : Entity_Id;
8558 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
8559
8560 begin
8561 while Present (Prim_Elt) loop
8562 P_Prim := Node (Prim_Elt);
fbf5a39b 8563
996ae0b0
RK
8564 if Chars (P_Prim) = Chars (New_E)
8565 and then Ekind (P_Prim) = Ekind (New_E)
8566 then
8567 P_Formal := First_Formal (P_Prim);
8568 N_Formal := First_Formal (New_E);
8569 while Present (P_Formal) and then Present (N_Formal) loop
8570 P_Typ := Etype (P_Formal);
8571 N_Typ := Etype (N_Formal);
8572
8573 if not Types_Correspond (P_Typ, N_Typ) then
8574 exit;
8575 end if;
8576
8577 Next_Entity (P_Formal);
8578 Next_Entity (N_Formal);
8579 end loop;
8580
82c80734
RD
8581 -- Found a matching primitive operation belonging to the
8582 -- formal ancestor type, so the new subprogram is
8583 -- overriding.
996ae0b0 8584
c8ef728f
ES
8585 if No (P_Formal)
8586 and then No (N_Formal)
996ae0b0
RK
8587 and then (Ekind (New_E) /= E_Function
8588 or else
8fde064e
AC
8589 Types_Correspond
8590 (Etype (P_Prim), Etype (New_E)))
996ae0b0
RK
8591 then
8592 return False;
8593 end if;
8594 end if;
8595
8596 Next_Elmt (Prim_Elt);
8597 end loop;
8598
2995860f
AC
8599 -- If no match found, then the new subprogram does not override
8600 -- in the generic (nor in the instance).
996ae0b0 8601
260359e3
AC
8602 -- If the type in question is not abstract, and the subprogram
8603 -- is, this will be an error if the new operation is in the
8604 -- private part of the instance. Emit a warning now, which will
8605 -- make the subsequent error message easier to understand.
8606
8607 if not Is_Abstract_Type (F_Typ)
8608 and then Is_Abstract_Subprogram (Prev_E)
8609 and then In_Private_Part (Current_Scope)
8610 then
8611 Error_Msg_Node_2 := F_Typ;
8612 Error_Msg_NE
3ccedacc
AC
8613 ("private operation& in generic unit does not override "
8614 & "any primitive operation of& (RM 12.3 (18))??",
260359e3
AC
8615 New_E, New_E);
8616 end if;
8617
996ae0b0
RK
8618 return True;
8619 end;
8620 end if;
8621 else
8622 return False;
8623 end if;
8624 end Is_Non_Overriding_Operation;
8625
beacce02
AC
8626 -------------------------------------
8627 -- List_Inherited_Pre_Post_Aspects --
8628 -------------------------------------
8629
8630 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
8631 begin
e606088a 8632 if Opt.List_Inherited_Aspects
b9696ffb 8633 and then Is_Subprogram_Or_Generic_Subprogram (E)
beacce02
AC
8634 then
8635 declare
c9d70ab1
AC
8636 Subps : constant Subprogram_List := Inherited_Subprograms (E);
8637 Items : Node_Id;
8638 Prag : Node_Id;
beacce02
AC
8639
8640 begin
c9d70ab1
AC
8641 for Index in Subps'Range loop
8642 Items := Contract (Subps (Index));
8643
8644 if Present (Items) then
8645 Prag := Pre_Post_Conditions (Items);
8646 while Present (Prag) loop
8647 Error_Msg_Sloc := Sloc (Prag);
8648
8649 if Class_Present (Prag)
8650 and then not Split_PPC (Prag)
8651 then
8652 if Pragma_Name (Prag) = Name_Precondition then
8653 Error_Msg_N
8654 ("info: & inherits `Pre''Class` aspect from "
8655 & "#?L?", E);
8656 else
8657 Error_Msg_N
8658 ("info: & inherits `Post''Class` aspect from "
8659 & "#?L?", E);
8660 end if;
beacce02 8661 end if;
beacce02 8662
c9d70ab1
AC
8663 Prag := Next_Pragma (Prag);
8664 end loop;
8665 end if;
beacce02
AC
8666 end loop;
8667 end;
8668 end if;
8669 end List_Inherited_Pre_Post_Aspects;
8670
996ae0b0
RK
8671 ------------------------------
8672 -- Make_Inequality_Operator --
8673 ------------------------------
8674
8675 -- S is the defining identifier of an equality operator. We build a
8676 -- subprogram declaration with the right signature. This operation is
8677 -- intrinsic, because it is always expanded as the negation of the
8678 -- call to the equality function.
8679
8680 procedure Make_Inequality_Operator (S : Entity_Id) is
8681 Loc : constant Source_Ptr := Sloc (S);
8682 Decl : Node_Id;
8683 Formals : List_Id;
8684 Op_Name : Entity_Id;
8685
c8ef728f
ES
8686 FF : constant Entity_Id := First_Formal (S);
8687 NF : constant Entity_Id := Next_Formal (FF);
996ae0b0
RK
8688
8689 begin
c8ef728f 8690 -- Check that equality was properly defined, ignore call if not
996ae0b0 8691
c8ef728f 8692 if No (NF) then
996ae0b0
RK
8693 return;
8694 end if;
8695
c8ef728f
ES
8696 declare
8697 A : constant Entity_Id :=
8698 Make_Defining_Identifier (Sloc (FF),
8699 Chars => Chars (FF));
8700
5d37ba92
ES
8701 B : constant Entity_Id :=
8702 Make_Defining_Identifier (Sloc (NF),
8703 Chars => Chars (NF));
c8ef728f
ES
8704
8705 begin
8706 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
8707
8708 Formals := New_List (
8709 Make_Parameter_Specification (Loc,
8710 Defining_Identifier => A,
8711 Parameter_Type =>
e4494292 8712 New_Occurrence_Of (Etype (First_Formal (S)),
c8ef728f
ES
8713 Sloc (Etype (First_Formal (S))))),
8714
8715 Make_Parameter_Specification (Loc,
8716 Defining_Identifier => B,
8717 Parameter_Type =>
e4494292 8718 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
c8ef728f
ES
8719 Sloc (Etype (Next_Formal (First_Formal (S)))))));
8720
8721 Decl :=
8722 Make_Subprogram_Declaration (Loc,
8723 Specification =>
8724 Make_Function_Specification (Loc,
8725 Defining_Unit_Name => Op_Name,
8726 Parameter_Specifications => Formals,
8727 Result_Definition =>
e4494292 8728 New_Occurrence_Of (Standard_Boolean, Loc)));
c8ef728f
ES
8729
8730 -- Insert inequality right after equality if it is explicit or after
8731 -- the derived type when implicit. These entities are created only
2995860f
AC
8732 -- for visibility purposes, and eventually replaced in the course
8733 -- of expansion, so they do not need to be attached to the tree and
8734 -- seen by the back-end. Keeping them internal also avoids spurious
c8ef728f
ES
8735 -- freezing problems. The declaration is inserted in the tree for
8736 -- analysis, and removed afterwards. If the equality operator comes
8737 -- from an explicit declaration, attach the inequality immediately
8738 -- after. Else the equality is inherited from a derived type
8739 -- declaration, so insert inequality after that declaration.
8740
8741 if No (Alias (S)) then
8742 Insert_After (Unit_Declaration_Node (S), Decl);
8743 elsif Is_List_Member (Parent (S)) then
8744 Insert_After (Parent (S), Decl);
8745 else
8746 Insert_After (Parent (Etype (First_Formal (S))), Decl);
8747 end if;
996ae0b0 8748
c8ef728f
ES
8749 Mark_Rewrite_Insertion (Decl);
8750 Set_Is_Intrinsic_Subprogram (Op_Name);
8751 Analyze (Decl);
8752 Remove (Decl);
8753 Set_Has_Completion (Op_Name);
8754 Set_Corresponding_Equality (Op_Name, S);
f937473f 8755 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
c8ef728f 8756 end;
996ae0b0
RK
8757 end Make_Inequality_Operator;
8758
8759 ----------------------
8760 -- May_Need_Actuals --
8761 ----------------------
8762
8763 procedure May_Need_Actuals (Fun : Entity_Id) is
8764 F : Entity_Id;
8765 B : Boolean;
8766
8767 begin
8768 F := First_Formal (Fun);
8769 B := True;
996ae0b0
RK
8770 while Present (F) loop
8771 if No (Default_Value (F)) then
8772 B := False;
8773 exit;
8774 end if;
8775
8776 Next_Formal (F);
8777 end loop;
8778
8779 Set_Needs_No_Actuals (Fun, B);
8780 end May_Need_Actuals;
8781
8782 ---------------------
8783 -- Mode_Conformant --
8784 ---------------------
8785
8786 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8787 Result : Boolean;
996ae0b0
RK
8788 begin
8789 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
8790 return Result;
8791 end Mode_Conformant;
8792
8793 ---------------------------
8794 -- New_Overloaded_Entity --
8795 ---------------------------
8796
8797 procedure New_Overloaded_Entity
8798 (S : Entity_Id;
8799 Derived_Type : Entity_Id := Empty)
8800 is
ec4867fa 8801 Overridden_Subp : Entity_Id := Empty;
758c442c
GD
8802 -- Set if the current scope has an operation that is type-conformant
8803 -- with S, and becomes hidden by S.
8804
5d37ba92
ES
8805 Is_Primitive_Subp : Boolean;
8806 -- Set to True if the new subprogram is primitive
8807
fbf5a39b
AC
8808 E : Entity_Id;
8809 -- Entity that S overrides
8810
996ae0b0 8811 Prev_Vis : Entity_Id := Empty;
ec4867fa
ES
8812 -- Predecessor of E in Homonym chain
8813
5d37ba92
ES
8814 procedure Check_For_Primitive_Subprogram
8815 (Is_Primitive : out Boolean;
8816 Is_Overriding : Boolean := False);
8817 -- If the subprogram being analyzed is a primitive operation of the type
8818 -- of a formal or result, set the Has_Primitive_Operations flag on the
8819 -- type, and set Is_Primitive to True (otherwise set to False). Set the
8820 -- corresponding flag on the entity itself for later use.
8821
ec4867fa
ES
8822 procedure Check_Synchronized_Overriding
8823 (Def_Id : Entity_Id;
ec4867fa
ES
8824 Overridden_Subp : out Entity_Id);
8825 -- First determine if Def_Id is an entry or a subprogram either defined
8826 -- in the scope of a task or protected type, or is a primitive of such
8827 -- a type. Check whether Def_Id overrides a subprogram of an interface
8828 -- implemented by the synchronized type, return the overridden entity
8829 -- or Empty.
758c442c 8830
996ae0b0
RK
8831 function Is_Private_Declaration (E : Entity_Id) return Boolean;
8832 -- Check that E is declared in the private part of the current package,
8833 -- or in the package body, where it may hide a previous declaration.
fbf5a39b 8834 -- We can't use In_Private_Part by itself because this flag is also
996ae0b0
RK
8835 -- set when freezing entities, so we must examine the place of the
8836 -- declaration in the tree, and recognize wrapper packages as well.
8837
2ddc2000
AC
8838 function Is_Overriding_Alias
8839 (Old_E : Entity_Id;
8840 New_E : Entity_Id) return Boolean;
8841 -- Check whether new subprogram and old subprogram are both inherited
8842 -- from subprograms that have distinct dispatch table entries. This can
2995860f
AC
8843 -- occur with derivations from instances with accidental homonyms. The
8844 -- function is conservative given that the converse is only true within
8845 -- instances that contain accidental overloadings.
2ddc2000 8846
5d37ba92
ES
8847 ------------------------------------
8848 -- Check_For_Primitive_Subprogram --
8849 ------------------------------------
996ae0b0 8850
5d37ba92
ES
8851 procedure Check_For_Primitive_Subprogram
8852 (Is_Primitive : out Boolean;
8853 Is_Overriding : Boolean := False)
ec4867fa 8854 is
996ae0b0
RK
8855 Formal : Entity_Id;
8856 F_Typ : Entity_Id;
07fc65c4 8857 B_Typ : Entity_Id;
996ae0b0
RK
8858
8859 function Visible_Part_Type (T : Entity_Id) return Boolean;
8dbd1460
AC
8860 -- Returns true if T is declared in the visible part of the current
8861 -- package scope; otherwise returns false. Assumes that T is declared
8862 -- in a package.
996ae0b0
RK
8863
8864 procedure Check_Private_Overriding (T : Entity_Id);
8865 -- Checks that if a primitive abstract subprogram of a visible
8dbd1460
AC
8866 -- abstract type is declared in a private part, then it must override
8867 -- an abstract subprogram declared in the visible part. Also checks
8868 -- that if a primitive function with a controlling result is declared
8869 -- in a private part, then it must override a function declared in
8870 -- the visible part.
996ae0b0
RK
8871
8872 ------------------------------
8873 -- Check_Private_Overriding --
8874 ------------------------------
8875
8876 procedure Check_Private_Overriding (T : Entity_Id) is
acf624f2 8877
aaeb3b3a
AC
8878 function Overrides_Visible_Function
8879 (Partial_View : Entity_Id) return Boolean;
acf624f2
BD
8880 -- True if S overrides a function in the visible part. The
8881 -- overridden function could be explicitly or implicitly declared.
8882
aaeb3b3a
AC
8883 function Overrides_Visible_Function
8884 (Partial_View : Entity_Id) return Boolean
8885 is
acf624f2
BD
8886 begin
8887 if not Is_Overriding or else not Has_Homonym (S) then
8888 return False;
8889 end if;
8890
aaeb3b3a 8891 if not Present (Partial_View) then
acf624f2
BD
8892 return True;
8893 end if;
8894
8895 -- Search through all the homonyms H of S in the current
8896 -- package spec, and return True if we find one that matches.
8897 -- Note that Parent (H) will be the declaration of the
aaeb3b3a 8898 -- partial view of T for a match.
acf624f2
BD
8899
8900 declare
8901 H : Entity_Id := S;
8902 begin
8903 loop
8904 H := Homonym (H);
8905 exit when not Present (H) or else Scope (H) /= Scope (S);
8906
8907 if Nkind_In
8908 (Parent (H),
8909 N_Private_Extension_Declaration,
8910 N_Private_Type_Declaration)
aaeb3b3a 8911 and then Defining_Identifier (Parent (H)) = Partial_View
acf624f2
BD
8912 then
8913 return True;
8914 end if;
8915 end loop;
8916 end;
8917
8918 return False;
8919 end Overrides_Visible_Function;
8920
8921 -- Start of processing for Check_Private_Overriding
8922
996ae0b0 8923 begin
51c16e29 8924 if Is_Package_Or_Generic_Package (Current_Scope)
996ae0b0
RK
8925 and then In_Private_Part (Current_Scope)
8926 and then Visible_Part_Type (T)
8927 and then not In_Instance
8928 then
f937473f
RD
8929 if Is_Abstract_Type (T)
8930 and then Is_Abstract_Subprogram (S)
8931 and then (not Is_Overriding
8dbd1460 8932 or else not Is_Abstract_Subprogram (E))
996ae0b0 8933 then
3ccedacc
AC
8934 Error_Msg_N ("abstract subprograms must be visible "
8935 & "(RM 3.9.3(10))!", S);
758c442c 8936
aaeb3b3a
AC
8937 elsif Ekind (S) = E_Function then
8938 declare
8939 Partial_View : constant Entity_Id :=
8940 Incomplete_Or_Partial_View (T);
2e79de51 8941
aaeb3b3a
AC
8942 begin
8943 if not Overrides_Visible_Function (Partial_View) then
8944
8945 -- Here, S is "function ... return T;" declared in
8946 -- the private part, not overriding some visible
8947 -- operation. That's illegal in the tagged case
8948 -- (but not if the private type is untagged).
8949
8950 if ((Present (Partial_View)
8951 and then Is_Tagged_Type (Partial_View))
8952 or else (not Present (Partial_View)
8953 and then Is_Tagged_Type (T)))
8954 and then T = Base_Type (Etype (S))
8955 then
8956 Error_Msg_N
8957 ("private function with tagged result must"
8958 & " override visible-part function", S);
8959 Error_Msg_N
8960 ("\move subprogram to the visible part"
8961 & " (RM 3.9.3(10))", S);
8962
8963 -- AI05-0073: extend this test to the case of a
8964 -- function with a controlling access result.
8965
8966 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
8967 and then Is_Tagged_Type (Designated_Type (Etype (S)))
8968 and then
8969 not Is_Class_Wide_Type
8970 (Designated_Type (Etype (S)))
8971 and then Ada_Version >= Ada_2012
8972 then
8973 Error_Msg_N
8974 ("private function with controlling access "
8975 & "result must override visible-part function",
8976 S);
8977 Error_Msg_N
8978 ("\move subprogram to the visible part"
8979 & " (RM 3.9.3(10))", S);
8980 end if;
8981 end if;
8982 end;
996ae0b0
RK
8983 end if;
8984 end if;
8985 end Check_Private_Overriding;
8986
8987 -----------------------
8988 -- Visible_Part_Type --
8989 -----------------------
8990
8991 function Visible_Part_Type (T : Entity_Id) return Boolean is
07fc65c4
GB
8992 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
8993 N : Node_Id;
996ae0b0
RK
8994
8995 begin
8dbd1460
AC
8996 -- If the entity is a private type, then it must be declared in a
8997 -- visible part.
996ae0b0
RK
8998
8999 if Ekind (T) in Private_Kind then
9000 return True;
9001 end if;
9002
9003 -- Otherwise, we traverse the visible part looking for its
9004 -- corresponding declaration. We cannot use the declaration
9005 -- node directly because in the private part the entity of a
9006 -- private type is the one in the full view, which does not
9007 -- indicate that it is the completion of something visible.
9008
07fc65c4 9009 N := First (Visible_Declarations (Specification (P)));
996ae0b0
RK
9010 while Present (N) loop
9011 if Nkind (N) = N_Full_Type_Declaration
9012 and then Present (Defining_Identifier (N))
9013 and then T = Defining_Identifier (N)
9014 then
9015 return True;
9016
800621e0
RD
9017 elsif Nkind_In (N, N_Private_Type_Declaration,
9018 N_Private_Extension_Declaration)
996ae0b0
RK
9019 and then Present (Defining_Identifier (N))
9020 and then T = Full_View (Defining_Identifier (N))
9021 then
9022 return True;
9023 end if;
9024
9025 Next (N);
9026 end loop;
9027
9028 return False;
9029 end Visible_Part_Type;
9030
5d37ba92 9031 -- Start of processing for Check_For_Primitive_Subprogram
996ae0b0
RK
9032
9033 begin
5d37ba92
ES
9034 Is_Primitive := False;
9035
996ae0b0
RK
9036 if not Comes_From_Source (S) then
9037 null;
9038
5d37ba92 9039 -- If subprogram is at library level, it is not primitive operation
15ce9ca2
AC
9040
9041 elsif Current_Scope = Standard_Standard then
9042 null;
9043
b9b2405f 9044 elsif (Is_Package_Or_Generic_Package (Current_Scope)
996ae0b0 9045 and then not In_Package_Body (Current_Scope))
82c80734 9046 or else Is_Overriding
996ae0b0 9047 then
07fc65c4 9048 -- For function, check return type
996ae0b0 9049
07fc65c4 9050 if Ekind (S) = E_Function then
5d37ba92
ES
9051 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
9052 F_Typ := Designated_Type (Etype (S));
9053 else
9054 F_Typ := Etype (S);
9055 end if;
9056
9057 B_Typ := Base_Type (F_Typ);
07fc65c4 9058
5d37ba92
ES
9059 if Scope (B_Typ) = Current_Scope
9060 and then not Is_Class_Wide_Type (B_Typ)
9061 and then not Is_Generic_Type (B_Typ)
9062 then
9063 Is_Primitive := True;
07fc65c4 9064 Set_Has_Primitive_Operations (B_Typ);
5d37ba92 9065 Set_Is_Primitive (S);
07fc65c4
GB
9066 Check_Private_Overriding (B_Typ);
9067 end if;
996ae0b0
RK
9068 end if;
9069
07fc65c4 9070 -- For all subprograms, check formals
996ae0b0 9071
07fc65c4 9072 Formal := First_Formal (S);
996ae0b0
RK
9073 while Present (Formal) loop
9074 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
9075 F_Typ := Designated_Type (Etype (Formal));
9076 else
9077 F_Typ := Etype (Formal);
9078 end if;
9079
07fc65c4
GB
9080 B_Typ := Base_Type (F_Typ);
9081
ec4867fa
ES
9082 if Ekind (B_Typ) = E_Access_Subtype then
9083 B_Typ := Base_Type (B_Typ);
9084 end if;
9085
5d37ba92
ES
9086 if Scope (B_Typ) = Current_Scope
9087 and then not Is_Class_Wide_Type (B_Typ)
9088 and then not Is_Generic_Type (B_Typ)
9089 then
9090 Is_Primitive := True;
9091 Set_Is_Primitive (S);
07fc65c4
GB
9092 Set_Has_Primitive_Operations (B_Typ);
9093 Check_Private_Overriding (B_Typ);
996ae0b0
RK
9094 end if;
9095
9096 Next_Formal (Formal);
9097 end loop;
1aee1fb3
AC
9098
9099 -- Special case: An equality function can be redefined for a type
9100 -- occurring in a declarative part, and won't otherwise be treated as
9101 -- a primitive because it doesn't occur in a package spec and doesn't
9102 -- override an inherited subprogram. It's important that we mark it
9103 -- primitive so it can be returned by Collect_Primitive_Operations
9104 -- and be used in composing the equality operation of later types
9105 -- that have a component of the type.
9106
9107 elsif Chars (S) = Name_Op_Eq
9108 and then Etype (S) = Standard_Boolean
9109 then
9110 B_Typ := Base_Type (Etype (First_Formal (S)));
9111
9112 if Scope (B_Typ) = Current_Scope
9113 and then
9114 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
9115 and then not Is_Limited_Type (B_Typ)
9116 then
9117 Is_Primitive := True;
9118 Set_Is_Primitive (S);
9119 Set_Has_Primitive_Operations (B_Typ);
9120 Check_Private_Overriding (B_Typ);
9121 end if;
996ae0b0 9122 end if;
5d37ba92
ES
9123 end Check_For_Primitive_Subprogram;
9124
9125 -----------------------------------
9126 -- Check_Synchronized_Overriding --
9127 -----------------------------------
9128
9129 procedure Check_Synchronized_Overriding
9130 (Def_Id : Entity_Id;
5d37ba92
ES
9131 Overridden_Subp : out Entity_Id)
9132 is
5d37ba92
ES
9133 Ifaces_List : Elist_Id;
9134 In_Scope : Boolean;
9135 Typ : Entity_Id;
9136
8aa15e3b
JM
9137 function Matches_Prefixed_View_Profile
9138 (Prim_Params : List_Id;
9139 Iface_Params : List_Id) return Boolean;
9140 -- Determine whether a subprogram's parameter profile Prim_Params
9141 -- matches that of a potentially overridden interface subprogram
9142 -- Iface_Params. Also determine if the type of first parameter of
9143 -- Iface_Params is an implemented interface.
9144
8aa15e3b
JM
9145 -----------------------------------
9146 -- Matches_Prefixed_View_Profile --
9147 -----------------------------------
9148
9149 function Matches_Prefixed_View_Profile
9150 (Prim_Params : List_Id;
9151 Iface_Params : List_Id) return Boolean
9152 is
9153 Iface_Id : Entity_Id;
9154 Iface_Param : Node_Id;
9155 Iface_Typ : Entity_Id;
9156 Prim_Id : Entity_Id;
9157 Prim_Param : Node_Id;
9158 Prim_Typ : Entity_Id;
9159
9160 function Is_Implemented
9161 (Ifaces_List : Elist_Id;
9162 Iface : Entity_Id) return Boolean;
9163 -- Determine if Iface is implemented by the current task or
9164 -- protected type.
9165
9166 --------------------
9167 -- Is_Implemented --
9168 --------------------
9169
9170 function Is_Implemented
9171 (Ifaces_List : Elist_Id;
9172 Iface : Entity_Id) return Boolean
9173 is
9174 Iface_Elmt : Elmt_Id;
9175
9176 begin
9177 Iface_Elmt := First_Elmt (Ifaces_List);
9178 while Present (Iface_Elmt) loop
9179 if Node (Iface_Elmt) = Iface then
9180 return True;
9181 end if;
9182
9183 Next_Elmt (Iface_Elmt);
9184 end loop;
9185
9186 return False;
9187 end Is_Implemented;
9188
9189 -- Start of processing for Matches_Prefixed_View_Profile
9190
9191 begin
9192 Iface_Param := First (Iface_Params);
9193 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
9194
9195 if Is_Access_Type (Iface_Typ) then
9196 Iface_Typ := Designated_Type (Iface_Typ);
9197 end if;
9198
9199 Prim_Param := First (Prim_Params);
9200
9201 -- The first parameter of the potentially overridden subprogram
9202 -- must be an interface implemented by Prim.
9203
9204 if not Is_Interface (Iface_Typ)
9205 or else not Is_Implemented (Ifaces_List, Iface_Typ)
9206 then
9207 return False;
9208 end if;
9209
9210 -- The checks on the object parameters are done, move onto the
9211 -- rest of the parameters.
9212
9213 if not In_Scope then
9214 Prim_Param := Next (Prim_Param);
9215 end if;
9216
9217 Iface_Param := Next (Iface_Param);
9218 while Present (Iface_Param) and then Present (Prim_Param) loop
9219 Iface_Id := Defining_Identifier (Iface_Param);
9220 Iface_Typ := Find_Parameter_Type (Iface_Param);
9221
8aa15e3b
JM
9222 Prim_Id := Defining_Identifier (Prim_Param);
9223 Prim_Typ := Find_Parameter_Type (Prim_Param);
9224
15e4986c
JM
9225 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
9226 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
9227 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
9228 then
9229 Iface_Typ := Designated_Type (Iface_Typ);
9230 Prim_Typ := Designated_Type (Prim_Typ);
8aa15e3b
JM
9231 end if;
9232
9233 -- Case of multiple interface types inside a parameter profile
9234
9235 -- (Obj_Param : in out Iface; ...; Param : Iface)
9236
9237 -- If the interface type is implemented, then the matching type
9238 -- in the primitive should be the implementing record type.
9239
9240 if Ekind (Iface_Typ) = E_Record_Type
9241 and then Is_Interface (Iface_Typ)
9242 and then Is_Implemented (Ifaces_List, Iface_Typ)
9243 then
9244 if Prim_Typ /= Typ then
9245 return False;
9246 end if;
9247
9248 -- The two parameters must be both mode and subtype conformant
9249
9250 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
9251 or else not
9252 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
9253 then
9254 return False;
9255 end if;
9256
9257 Next (Iface_Param);
9258 Next (Prim_Param);
9259 end loop;
9260
9261 -- One of the two lists contains more parameters than the other
9262
9263 if Present (Iface_Param) or else Present (Prim_Param) then
9264 return False;
9265 end if;
9266
9267 return True;
9268 end Matches_Prefixed_View_Profile;
9269
9270 -- Start of processing for Check_Synchronized_Overriding
9271
5d37ba92
ES
9272 begin
9273 Overridden_Subp := Empty;
9274
8aa15e3b
JM
9275 -- Def_Id must be an entry or a subprogram. We should skip predefined
9276 -- primitives internally generated by the frontend; however at this
9277 -- stage predefined primitives are still not fully decorated. As a
9278 -- minor optimization we skip here internally generated subprograms.
5d37ba92 9279
8aa15e3b
JM
9280 if (Ekind (Def_Id) /= E_Entry
9281 and then Ekind (Def_Id) /= E_Function
9282 and then Ekind (Def_Id) /= E_Procedure)
9283 or else not Comes_From_Source (Def_Id)
5d37ba92
ES
9284 then
9285 return;
9286 end if;
9287
9288 -- Search for the concurrent declaration since it contains the list
9289 -- of all implemented interfaces. In this case, the subprogram is
9290 -- declared within the scope of a protected or a task type.
9291
9292 if Present (Scope (Def_Id))
9293 and then Is_Concurrent_Type (Scope (Def_Id))
9294 and then not Is_Generic_Actual_Type (Scope (Def_Id))
9295 then
9296 Typ := Scope (Def_Id);
9297 In_Scope := True;
9298
8aa15e3b 9299 -- The enclosing scope is not a synchronized type and the subprogram
4adf3c50 9300 -- has no formals.
8aa15e3b
JM
9301
9302 elsif No (First_Formal (Def_Id)) then
9303 return;
5d37ba92 9304
8aa15e3b 9305 -- The subprogram has formals and hence it may be a primitive of a
4adf3c50 9306 -- concurrent type.
5d37ba92 9307
8aa15e3b
JM
9308 else
9309 Typ := Etype (First_Formal (Def_Id));
9310
9311 if Is_Access_Type (Typ) then
9312 Typ := Directly_Designated_Type (Typ);
8c3dd7a8
JM
9313 end if;
9314
8aa15e3b
JM
9315 if Is_Concurrent_Type (Typ)
9316 and then not Is_Generic_Actual_Type (Typ)
5d37ba92 9317 then
5d37ba92
ES
9318 In_Scope := False;
9319
9320 -- This case occurs when the concurrent type is declared within
9321 -- a generic unit. As a result the corresponding record has been
9322 -- built and used as the type of the first formal, we just have
9323 -- to retrieve the corresponding concurrent type.
9324
8aa15e3b 9325 elsif Is_Concurrent_Record_Type (Typ)
dd54644b 9326 and then not Is_Class_Wide_Type (Typ)
8aa15e3b 9327 and then Present (Corresponding_Concurrent_Type (Typ))
5d37ba92 9328 then
8aa15e3b 9329 Typ := Corresponding_Concurrent_Type (Typ);
5d37ba92
ES
9330 In_Scope := False;
9331
9332 else
9333 return;
9334 end if;
8aa15e3b
JM
9335 end if;
9336
9337 -- There is no overriding to check if is an inherited operation in a
9338 -- type derivation on for a generic actual.
9339
9340 Collect_Interfaces (Typ, Ifaces_List);
9341
9342 if Is_Empty_Elmt_List (Ifaces_List) then
5d37ba92
ES
9343 return;
9344 end if;
9345
8aa15e3b
JM
9346 -- Determine whether entry or subprogram Def_Id overrides a primitive
9347 -- operation that belongs to one of the interfaces in Ifaces_List.
5d37ba92 9348
8aa15e3b
JM
9349 declare
9350 Candidate : Entity_Id := Empty;
9351 Hom : Entity_Id := Empty;
8aa15e3b
JM
9352 Subp : Entity_Id := Empty;
9353
9354 begin
4adf3c50 9355 -- Traverse the homonym chain, looking for a potentially
8aa15e3b
JM
9356 -- overridden subprogram that belongs to an implemented
9357 -- interface.
9358
9359 Hom := Current_Entity_In_Scope (Def_Id);
9360 while Present (Hom) loop
9361 Subp := Hom;
9362
15e4986c
JM
9363 if Subp = Def_Id
9364 or else not Is_Overloadable (Subp)
9365 or else not Is_Primitive (Subp)
9366 or else not Is_Dispatching_Operation (Subp)
79afa047 9367 or else not Present (Find_Dispatching_Type (Subp))
15e4986c 9368 or else not Is_Interface (Find_Dispatching_Type (Subp))
8aa15e3b 9369 then
15e4986c 9370 null;
8aa15e3b 9371
15e4986c 9372 -- Entries and procedures can override abstract or null
4adf3c50 9373 -- interface procedures.
8aa15e3b 9374
15e4986c 9375 elsif (Ekind (Def_Id) = E_Procedure
8fde064e 9376 or else Ekind (Def_Id) = E_Entry)
8aa15e3b 9377 and then Ekind (Subp) = E_Procedure
8aa15e3b
JM
9378 and then Matches_Prefixed_View_Profile
9379 (Parameter_Specifications (Parent (Def_Id)),
9380 Parameter_Specifications (Parent (Subp)))
9381 then
9382 Candidate := Subp;
9383
15e4986c
JM
9384 -- For an overridden subprogram Subp, check whether the mode
9385 -- of its first parameter is correct depending on the kind
9386 -- of synchronized type.
8aa15e3b 9387
15e4986c
JM
9388 declare
9389 Formal : constant Node_Id := First_Formal (Candidate);
9390
9391 begin
9392 -- In order for an entry or a protected procedure to
9393 -- override, the first parameter of the overridden
9394 -- routine must be of mode "out", "in out" or
9395 -- access-to-variable.
9396
8fde064e 9397 if Ekind_In (Candidate, E_Entry, E_Procedure)
15e4986c
JM
9398 and then Is_Protected_Type (Typ)
9399 and then Ekind (Formal) /= E_In_Out_Parameter
9400 and then Ekind (Formal) /= E_Out_Parameter
8fde064e
AC
9401 and then Nkind (Parameter_Type (Parent (Formal))) /=
9402 N_Access_Definition
15e4986c
JM
9403 then
9404 null;
9405
9406 -- All other cases are OK since a task entry or routine
9407 -- does not have a restriction on the mode of the first
9408 -- parameter of the overridden interface routine.
9409
9410 else
9411 Overridden_Subp := Candidate;
9412 return;
9413 end if;
9414 end;
8aa15e3b
JM
9415
9416 -- Functions can override abstract interface functions
9417
9418 elsif Ekind (Def_Id) = E_Function
9419 and then Ekind (Subp) = E_Function
8aa15e3b
JM
9420 and then Matches_Prefixed_View_Profile
9421 (Parameter_Specifications (Parent (Def_Id)),
9422 Parameter_Specifications (Parent (Subp)))
9423 and then Etype (Result_Definition (Parent (Def_Id))) =
9424 Etype (Result_Definition (Parent (Subp)))
9425 then
273123a4
AC
9426 Candidate := Subp;
9427
9428 -- If an inherited subprogram is implemented by a protected
9429 -- function, then the first parameter of the inherited
9430 -- subprogram shall be of mode in, but not an
9431 -- access-to-variable parameter (RM 9.4(11/9)
9432
9433 if Present (First_Formal (Subp))
9434 and then Ekind (First_Formal (Subp)) = E_In_Parameter
9435 and then
9436 (not Is_Access_Type (Etype (First_Formal (Subp)))
9437 or else
9438 Is_Access_Constant (Etype (First_Formal (Subp))))
9439 then
9440 Overridden_Subp := Subp;
9441 return;
9442 end if;
8aa15e3b
JM
9443 end if;
9444
9445 Hom := Homonym (Hom);
9446 end loop;
9447
4adf3c50
AC
9448 -- After examining all candidates for overriding, we are left with
9449 -- the best match which is a mode incompatible interface routine.
8aa15e3b 9450
273123a4
AC
9451 if In_Scope and then Present (Candidate) then
9452 Error_Msg_PT (Def_Id, Candidate);
5d37ba92 9453 end if;
8aa15e3b
JM
9454
9455 Overridden_Subp := Candidate;
9456 return;
9457 end;
5d37ba92
ES
9458 end Check_Synchronized_Overriding;
9459
9460 ----------------------------
9461 -- Is_Private_Declaration --
9462 ----------------------------
9463
9464 function Is_Private_Declaration (E : Entity_Id) return Boolean is
9465 Priv_Decls : List_Id;
9466 Decl : constant Node_Id := Unit_Declaration_Node (E);
9467
9468 begin
9469 if Is_Package_Or_Generic_Package (Current_Scope)
9470 and then In_Private_Part (Current_Scope)
9471 then
9472 Priv_Decls :=
d12b19fa 9473 Private_Declarations (Package_Specification (Current_Scope));
5d37ba92
ES
9474
9475 return In_Package_Body (Current_Scope)
9476 or else
9477 (Is_List_Member (Decl)
a4901c08 9478 and then List_Containing (Decl) = Priv_Decls)
5d37ba92 9479 or else (Nkind (Parent (Decl)) = N_Package_Specification
a4901c08
AC
9480 and then not
9481 Is_Compilation_Unit
9482 (Defining_Entity (Parent (Decl)))
9483 and then List_Containing (Parent (Parent (Decl))) =
9484 Priv_Decls);
5d37ba92
ES
9485 else
9486 return False;
9487 end if;
9488 end Is_Private_Declaration;
996ae0b0 9489
2ddc2000
AC
9490 --------------------------
9491 -- Is_Overriding_Alias --
9492 --------------------------
9493
9494 function Is_Overriding_Alias
9495 (Old_E : Entity_Id;
9496 New_E : Entity_Id) return Boolean
9497 is
9498 AO : constant Entity_Id := Alias (Old_E);
9499 AN : constant Entity_Id := Alias (New_E);
2ddc2000
AC
9500 begin
9501 return Scope (AO) /= Scope (AN)
9502 or else No (DTC_Entity (AO))
9503 or else No (DTC_Entity (AN))
9504 or else DT_Position (AO) = DT_Position (AN);
9505 end Is_Overriding_Alias;
9506
996ae0b0
RK
9507 -- Start of processing for New_Overloaded_Entity
9508
9509 begin
fbf5a39b
AC
9510 -- We need to look for an entity that S may override. This must be a
9511 -- homonym in the current scope, so we look for the first homonym of
9512 -- S in the current scope as the starting point for the search.
9513
9514 E := Current_Entity_In_Scope (S);
9515
947430d5
AC
9516 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
9517 -- They are directly added to the list of primitive operations of
9518 -- Derived_Type, unless this is a rederivation in the private part
9519 -- of an operation that was already derived in the visible part of
9520 -- the current package.
9521
0791fbe9 9522 if Ada_Version >= Ada_2005
947430d5
AC
9523 and then Present (Derived_Type)
9524 and then Present (Alias (S))
9525 and then Is_Dispatching_Operation (Alias (S))
9526 and then Present (Find_Dispatching_Type (Alias (S)))
9527 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
9528 then
9529 -- For private types, when the full-view is processed we propagate to
9530 -- the full view the non-overridden entities whose attribute "alias"
9531 -- references an interface primitive. These entities were added by
9532 -- Derive_Subprograms to ensure that interface primitives are
9533 -- covered.
9534
9535 -- Inside_Freeze_Actions is non zero when S corresponds with an
9536 -- internal entity that links an interface primitive with its
9537 -- covering primitive through attribute Interface_Alias (see
4adf3c50 9538 -- Add_Internal_Interface_Entities).
947430d5
AC
9539
9540 if Inside_Freezing_Actions = 0
9541 and then Is_Package_Or_Generic_Package (Current_Scope)
9542 and then In_Private_Part (Current_Scope)
9543 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
9544 and then Nkind (Parent (S)) = N_Full_Type_Declaration
9545 and then Full_View (Defining_Identifier (Parent (E)))
9546 = Defining_Identifier (Parent (S))
9547 and then Alias (E) = Alias (S)
9548 then
9549 Check_Operation_From_Private_View (S, E);
9550 Set_Is_Dispatching_Operation (S);
9551
9552 -- Common case
9553
9554 else
9555 Enter_Overloaded_Entity (S);
9556 Check_Dispatching_Operation (S, Empty);
9557 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9558 end if;
9559
9560 return;
9561 end if;
9562
fbf5a39b
AC
9563 -- If there is no homonym then this is definitely not overriding
9564
996ae0b0
RK
9565 if No (E) then
9566 Enter_Overloaded_Entity (S);
9567 Check_Dispatching_Operation (S, Empty);
5d37ba92 9568 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
996ae0b0 9569
2995860f
AC
9570 -- If subprogram has an explicit declaration, check whether it has an
9571 -- overriding indicator.
758c442c 9572
ec4867fa 9573 if Comes_From_Source (S) then
8aa15e3b 9574 Check_Synchronized_Overriding (S, Overridden_Subp);
ea034236
AC
9575
9576 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
9577 -- it may have overridden some hidden inherited primitive. Update
308e6f3a 9578 -- Overridden_Subp to avoid spurious errors when checking the
ea034236
AC
9579 -- overriding indicator.
9580
9581 if Ada_Version >= Ada_2012
9582 and then No (Overridden_Subp)
9583 and then Is_Dispatching_Operation (S)
038140ed 9584 and then Present (Overridden_Operation (S))
ea034236
AC
9585 then
9586 Overridden_Subp := Overridden_Operation (S);
9587 end if;
9588
5d37ba92
ES
9589 Check_Overriding_Indicator
9590 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
758c442c
GD
9591 end if;
9592
fbf5a39b
AC
9593 -- If there is a homonym that is not overloadable, then we have an
9594 -- error, except for the special cases checked explicitly below.
9595
996ae0b0
RK
9596 elsif not Is_Overloadable (E) then
9597
9598 -- Check for spurious conflict produced by a subprogram that has the
9599 -- same name as that of the enclosing generic package. The conflict
9600 -- occurs within an instance, between the subprogram and the renaming
9601 -- declaration for the package. After the subprogram, the package
9602 -- renaming declaration becomes hidden.
9603
9604 if Ekind (E) = E_Package
9605 and then Present (Renamed_Object (E))
9606 and then Renamed_Object (E) = Current_Scope
9607 and then Nkind (Parent (Renamed_Object (E))) =
9608 N_Package_Specification
9609 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
9610 then
9611 Set_Is_Hidden (E);
9612 Set_Is_Immediately_Visible (E, False);
9613 Enter_Overloaded_Entity (S);
9614 Set_Homonym (S, Homonym (E));
9615 Check_Dispatching_Operation (S, Empty);
5d37ba92 9616 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
996ae0b0
RK
9617
9618 -- If the subprogram is implicit it is hidden by the previous
82c80734
RD
9619 -- declaration. However if it is dispatching, it must appear in the
9620 -- dispatch table anyway, because it can be dispatched to even if it
9621 -- cannot be called directly.
996ae0b0 9622
4adf3c50 9623 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
996ae0b0
RK
9624 Set_Scope (S, Current_Scope);
9625
9626 if Is_Dispatching_Operation (Alias (S)) then
9627 Check_Dispatching_Operation (S, Empty);
9628 end if;
9629
9630 return;
9631
9632 else
9633 Error_Msg_Sloc := Sloc (E);
996ae0b0 9634
f3d57416 9635 -- Generate message, with useful additional warning if in generic
996ae0b0
RK
9636
9637 if Is_Generic_Unit (E) then
5d37ba92
ES
9638 Error_Msg_N ("previous generic unit cannot be overloaded", S);
9639 Error_Msg_N ("\& conflicts with declaration#", S);
9640 else
9641 Error_Msg_N ("& conflicts with declaration#", S);
996ae0b0
RK
9642 end if;
9643
9644 return;
9645 end if;
9646
fbf5a39b
AC
9647 -- E exists and is overloadable
9648
996ae0b0 9649 else
8aa15e3b 9650 Check_Synchronized_Overriding (S, Overridden_Subp);
758c442c 9651
82c80734
RD
9652 -- Loop through E and its homonyms to determine if any of them is
9653 -- the candidate for overriding by S.
996ae0b0
RK
9654
9655 while Present (E) loop
fbf5a39b
AC
9656
9657 -- Definitely not interesting if not in the current scope
9658
996ae0b0
RK
9659 if Scope (E) /= Current_Scope then
9660 null;
9661
aca90db9
AC
9662 -- A function can overload the name of an abstract state. The
9663 -- state can be viewed as a function with a profile that cannot
9664 -- be matched by anything.
9665
9666 elsif Ekind (S) = E_Function
9667 and then Ekind (E) = E_Abstract_State
9668 then
9669 Enter_Overloaded_Entity (S);
9670 return;
9671
2995860f
AC
9672 -- Ada 2012 (AI05-0165): For internally generated bodies of null
9673 -- procedures locate the internally generated spec. We enforce
9674 -- mode conformance since a tagged type may inherit from
9675 -- interfaces several null primitives which differ only in
9676 -- the mode of the formals.
25ebc085
AC
9677
9678 elsif not Comes_From_Source (S)
9679 and then Is_Null_Procedure (S)
9680 and then not Mode_Conformant (E, S)
9681 then
9682 null;
9683
fbf5a39b
AC
9684 -- Check if we have type conformance
9685
ec4867fa 9686 elsif Type_Conformant (E, S) then
c8ef728f 9687
82c80734
RD
9688 -- If the old and new entities have the same profile and one
9689 -- is not the body of the other, then this is an error, unless
9690 -- one of them is implicitly declared.
996ae0b0
RK
9691
9692 -- There are some cases when both can be implicit, for example
9693 -- when both a literal and a function that overrides it are
f3d57416 9694 -- inherited in a derivation, or when an inherited operation
ec4867fa 9695 -- of a tagged full type overrides the inherited operation of
f3d57416 9696 -- a private extension. Ada 83 had a special rule for the
885c4871 9697 -- literal case. In Ada 95, the later implicit operation hides
82c80734
RD
9698 -- the former, and the literal is always the former. In the
9699 -- odd case where both are derived operations declared at the
9700 -- same point, both operations should be declared, and in that
9701 -- case we bypass the following test and proceed to the next
df46b832
AC
9702 -- part. This can only occur for certain obscure cases in
9703 -- instances, when an operation on a type derived from a formal
9704 -- private type does not override a homograph inherited from
9705 -- the actual. In subsequent derivations of such a type, the
9706 -- DT positions of these operations remain distinct, if they
9707 -- have been set.
996ae0b0
RK
9708
9709 if Present (Alias (S))
9710 and then (No (Alias (E))
9711 or else Comes_From_Source (E)
2ddc2000 9712 or else Is_Abstract_Subprogram (S)
df46b832
AC
9713 or else
9714 (Is_Dispatching_Operation (E)
84c0a895 9715 and then Is_Overriding_Alias (E, S)))
df46b832 9716 and then Ekind (E) /= E_Enumeration_Literal
996ae0b0 9717 then
82c80734
RD
9718 -- When an derived operation is overloaded it may be due to
9719 -- the fact that the full view of a private extension
996ae0b0
RK
9720 -- re-inherits. It has to be dealt with.
9721
e660dbf7 9722 if Is_Package_Or_Generic_Package (Current_Scope)
996ae0b0
RK
9723 and then In_Private_Part (Current_Scope)
9724 then
9725 Check_Operation_From_Private_View (S, E);
9726 end if;
9727
038140ed
AC
9728 -- In any case the implicit operation remains hidden by the
9729 -- existing declaration, which is overriding. Indicate that
9730 -- E overrides the operation from which S is inherited.
996ae0b0 9731
038140ed 9732 if Present (Alias (S)) then
039538bc
AC
9733 Set_Overridden_Operation (E, Alias (S));
9734 Inherit_Subprogram_Contract (E, Alias (S));
9735
038140ed 9736 else
039538bc
AC
9737 Set_Overridden_Operation (E, S);
9738 Inherit_Subprogram_Contract (E, S);
038140ed 9739 end if;
758c442c
GD
9740
9741 if Comes_From_Source (E) then
5d37ba92 9742 Check_Overriding_Indicator (E, S, Is_Primitive => False);
758c442c
GD
9743 end if;
9744
996ae0b0
RK
9745 return;
9746
26a43556
AC
9747 -- Within an instance, the renaming declarations for actual
9748 -- subprograms may become ambiguous, but they do not hide each
9749 -- other.
996ae0b0
RK
9750
9751 elsif Ekind (E) /= E_Entry
9752 and then not Comes_From_Source (E)
9753 and then not Is_Generic_Instance (E)
9754 and then (Present (Alias (E))
9755 or else Is_Intrinsic_Subprogram (E))
9756 and then (not In_Instance
9757 or else No (Parent (E))
9758 or else Nkind (Unit_Declaration_Node (E)) /=
8dbd1460 9759 N_Subprogram_Renaming_Declaration)
996ae0b0 9760 then
26a43556
AC
9761 -- A subprogram child unit is not allowed to override an
9762 -- inherited subprogram (10.1.1(20)).
996ae0b0
RK
9763
9764 if Is_Child_Unit (S) then
9765 Error_Msg_N
9766 ("child unit overrides inherited subprogram in parent",
9767 S);
9768 return;
9769 end if;
9770
9771 if Is_Non_Overriding_Operation (E, S) then
9772 Enter_Overloaded_Entity (S);
8dbd1460 9773
c8ef728f 9774 if No (Derived_Type)
996ae0b0
RK
9775 or else Is_Tagged_Type (Derived_Type)
9776 then
9777 Check_Dispatching_Operation (S, Empty);
9778 end if;
9779
9780 return;
9781 end if;
9782
9783 -- E is a derived operation or an internal operator which
9784 -- is being overridden. Remove E from further visibility.
9785 -- Furthermore, if E is a dispatching operation, it must be
9786 -- replaced in the list of primitive operations of its type
9787 -- (see Override_Dispatching_Operation).
9788
ec4867fa 9789 Overridden_Subp := E;
758c442c 9790
996ae0b0
RK
9791 declare
9792 Prev : Entity_Id;
9793
9794 begin
9795 Prev := First_Entity (Current_Scope);
8fde064e 9796 while Present (Prev) and then Next_Entity (Prev) /= E loop
996ae0b0
RK
9797 Next_Entity (Prev);
9798 end loop;
9799
9800 -- It is possible for E to be in the current scope and
9801 -- yet not in the entity chain. This can only occur in a
9802 -- generic context where E is an implicit concatenation
9803 -- in the formal part, because in a generic body the
9804 -- entity chain starts with the formals.
9805
948ed277
AC
9806 -- In GNATprove mode, a wrapper for an operation with
9807 -- axiomatization may be a homonym of another declaration
9808 -- for an actual subprogram (needs refinement ???).
9809
9810 if No (Prev) then
9811 if In_Instance
9812 and then GNATprove_Mode
9813 and then
9814 Nkind (Original_Node (Unit_Declaration_Node (S))) =
9815 N_Subprogram_Renaming_Declaration
9816 then
9817 return;
9818 else
9819 pragma Assert (Chars (E) = Name_Op_Concat);
9820 null;
9821 end if;
9822 end if;
996ae0b0
RK
9823
9824 -- E must be removed both from the entity_list of the
948ed277 9825 -- current scope, and from the visibility chain.
996ae0b0
RK
9826
9827 if Debug_Flag_E then
9828 Write_Str ("Override implicit operation ");
9829 Write_Int (Int (E));
9830 Write_Eol;
9831 end if;
9832
9833 -- If E is a predefined concatenation, it stands for four
9834 -- different operations. As a result, a single explicit
9835 -- declaration does not hide it. In a possible ambiguous
9836 -- situation, Disambiguate chooses the user-defined op,
9837 -- so it is correct to retain the previous internal one.
9838
9839 if Chars (E) /= Name_Op_Concat
9840 or else Ekind (E) /= E_Operator
9841 then
9842 -- For nondispatching derived operations that are
9843 -- overridden by a subprogram declared in the private
8dbd1460
AC
9844 -- part of a package, we retain the derived subprogram
9845 -- but mark it as not immediately visible. If the
9846 -- derived operation was declared in the visible part
9847 -- then this ensures that it will still be visible
9848 -- outside the package with the proper signature
9849 -- (calls from outside must also be directed to this
9850 -- version rather than the overriding one, unlike the
9851 -- dispatching case). Calls from inside the package
9852 -- will still resolve to the overriding subprogram
9853 -- since the derived one is marked as not visible
9854 -- within the package.
996ae0b0
RK
9855
9856 -- If the private operation is dispatching, we achieve
9857 -- the overriding by keeping the implicit operation
9865d858 9858 -- but setting its alias to be the overriding one. In
996ae0b0
RK
9859 -- this fashion the proper body is executed in all
9860 -- cases, but the original signature is used outside
9861 -- of the package.
9862
9863 -- If the overriding is not in the private part, we
9864 -- remove the implicit operation altogether.
9865
9866 if Is_Private_Declaration (S) then
996ae0b0
RK
9867 if not Is_Dispatching_Operation (E) then
9868 Set_Is_Immediately_Visible (E, False);
9869 else
e895b435 9870 -- Work done in Override_Dispatching_Operation,
a46cde68 9871 -- so nothing else needs to be done here.
996ae0b0
RK
9872
9873 null;
9874 end if;
996ae0b0 9875
fbf5a39b
AC
9876 else
9877 -- Find predecessor of E in Homonym chain
996ae0b0
RK
9878
9879 if E = Current_Entity (E) then
9880 Prev_Vis := Empty;
9881 else
9882 Prev_Vis := Current_Entity (E);
9883 while Homonym (Prev_Vis) /= E loop
9884 Prev_Vis := Homonym (Prev_Vis);
9885 end loop;
9886 end if;
9887
9888 if Prev_Vis /= Empty then
9889
9890 -- Skip E in the visibility chain
9891
9892 Set_Homonym (Prev_Vis, Homonym (E));
9893
9894 else
9895 Set_Name_Entity_Id (Chars (E), Homonym (E));
9896 end if;
9897
9898 Set_Next_Entity (Prev, Next_Entity (E));
9899
9900 if No (Next_Entity (Prev)) then
9901 Set_Last_Entity (Current_Scope, Prev);
9902 end if;
996ae0b0
RK
9903 end if;
9904 end if;
9905
9906 Enter_Overloaded_Entity (S);
1c1289e7
AC
9907
9908 -- For entities generated by Derive_Subprograms the
9909 -- overridden operation is the inherited primitive
9910 -- (which is available through the attribute alias).
9911
9912 if not (Comes_From_Source (E))
9913 and then Is_Dispatching_Operation (E)
f9673bb0
AC
9914 and then Find_Dispatching_Type (E) =
9915 Find_Dispatching_Type (S)
1c1289e7
AC
9916 and then Present (Alias (E))
9917 and then Comes_From_Source (Alias (E))
9918 then
039538bc
AC
9919 Set_Overridden_Operation (S, Alias (E));
9920 Inherit_Subprogram_Contract (S, Alias (E));
2fe829ae 9921
6320f5e1
AC
9922 -- Normal case of setting entity as overridden
9923
9924 -- Note: Static_Initialization and Overridden_Operation
9925 -- attributes use the same field in subprogram entities.
9926 -- Static_Initialization is only defined for internal
9927 -- initialization procedures, where Overridden_Operation
9928 -- is irrelevant. Therefore the setting of this attribute
9929 -- must check whether the target is an init_proc.
9930
2fe829ae 9931 elsif not Is_Init_Proc (S) then
039538bc
AC
9932 Set_Overridden_Operation (S, E);
9933 Inherit_Subprogram_Contract (S, E);
1c1289e7
AC
9934 end if;
9935
5d37ba92 9936 Check_Overriding_Indicator (S, E, Is_Primitive => True);
996ae0b0 9937
fc53fe76 9938 -- If S is a user-defined subprogram or a null procedure
38ef8ebe
AC
9939 -- expanded to override an inherited null procedure, or a
9940 -- predefined dispatching primitive then indicate that E
038140ed 9941 -- overrides the operation from which S is inherited.
fc53fe76
AC
9942
9943 if Comes_From_Source (S)
9944 or else
9945 (Present (Parent (S))
9946 and then
9947 Nkind (Parent (S)) = N_Procedure_Specification
9948 and then
9949 Null_Present (Parent (S)))
38ef8ebe
AC
9950 or else
9951 (Present (Alias (E))
f16e8df9
RD
9952 and then
9953 Is_Predefined_Dispatching_Operation (Alias (E)))
fc53fe76 9954 then
c8ef728f 9955 if Present (Alias (E)) then
039538bc
AC
9956 Set_Overridden_Operation (S, Alias (E));
9957 Inherit_Subprogram_Contract (S, Alias (E));
41251c60
JM
9958 end if;
9959 end if;
9960
996ae0b0 9961 if Is_Dispatching_Operation (E) then
fbf5a39b 9962
82c80734 9963 -- An overriding dispatching subprogram inherits the
f9673bb0 9964 -- convention of the overridden subprogram (AI-117).
996ae0b0
RK
9965
9966 Set_Convention (S, Convention (E));
41251c60
JM
9967 Check_Dispatching_Operation (S, E);
9968
996ae0b0
RK
9969 else
9970 Check_Dispatching_Operation (S, Empty);
9971 end if;
9972
5d37ba92
ES
9973 Check_For_Primitive_Subprogram
9974 (Is_Primitive_Subp, Is_Overriding => True);
996ae0b0
RK
9975 goto Check_Inequality;
9976 end;
9977
9978 -- Apparent redeclarations in instances can occur when two
9979 -- formal types get the same actual type. The subprograms in
9980 -- in the instance are legal, even if not callable from the
9981 -- outside. Calls from within are disambiguated elsewhere.
9982 -- For dispatching operations in the visible part, the usual
9983 -- rules apply, and operations with the same profile are not
9984 -- legal (B830001).
9985
9986 elsif (In_Instance_Visible_Part
9987 and then not Is_Dispatching_Operation (E))
9988 or else In_Instance_Not_Visible
9989 then
9990 null;
9991
9992 -- Here we have a real error (identical profile)
9993
9994 else
9995 Error_Msg_Sloc := Sloc (E);
9996
9997 -- Avoid cascaded errors if the entity appears in
9998 -- subsequent calls.
9999
10000 Set_Scope (S, Current_Scope);
10001
5d37ba92
ES
10002 -- Generate error, with extra useful warning for the case
10003 -- of a generic instance with no completion.
996ae0b0
RK
10004
10005 if Is_Generic_Instance (S)
10006 and then not Has_Completion (E)
10007 then
10008 Error_Msg_N
5d37ba92
ES
10009 ("instantiation cannot provide body for&", S);
10010 Error_Msg_N ("\& conflicts with declaration#", S);
10011 else
10012 Error_Msg_N ("& conflicts with declaration#", S);
996ae0b0
RK
10013 end if;
10014
10015 return;
10016 end if;
10017
10018 else
c8ef728f
ES
10019 -- If one subprogram has an access parameter and the other
10020 -- a parameter of an access type, calls to either might be
10021 -- ambiguous. Verify that parameters match except for the
10022 -- access parameter.
10023
10024 if May_Hide_Profile then
10025 declare
ec4867fa
ES
10026 F1 : Entity_Id;
10027 F2 : Entity_Id;
8dbd1460 10028
c8ef728f
ES
10029 begin
10030 F1 := First_Formal (S);
10031 F2 := First_Formal (E);
10032 while Present (F1) and then Present (F2) loop
10033 if Is_Access_Type (Etype (F1)) then
10034 if not Is_Access_Type (Etype (F2))
10035 or else not Conforming_Types
10036 (Designated_Type (Etype (F1)),
10037 Designated_Type (Etype (F2)),
10038 Type_Conformant)
10039 then
10040 May_Hide_Profile := False;
10041 end if;
10042
10043 elsif
10044 not Conforming_Types
10045 (Etype (F1), Etype (F2), Type_Conformant)
10046 then
10047 May_Hide_Profile := False;
10048 end if;
10049
10050 Next_Formal (F1);
10051 Next_Formal (F2);
10052 end loop;
10053
10054 if May_Hide_Profile
10055 and then No (F1)
10056 and then No (F2)
10057 then
dbfeb4fa 10058 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
c8ef728f
ES
10059 end if;
10060 end;
10061 end if;
996ae0b0
RK
10062 end if;
10063
996ae0b0
RK
10064 E := Homonym (E);
10065 end loop;
10066
10067 -- On exit, we know that S is a new entity
10068
10069 Enter_Overloaded_Entity (S);
5d37ba92
ES
10070 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10071 Check_Overriding_Indicator
10072 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
996ae0b0 10073
c4d67e2d 10074 -- Overloading is not allowed in SPARK, except for operators
8ed68165 10075
c4d67e2d
AC
10076 if Nkind (S) /= N_Defining_Operator_Symbol then
10077 Error_Msg_Sloc := Sloc (Homonym (S));
ce5ba43a 10078 Check_SPARK_05_Restriction
c4d67e2d
AC
10079 ("overloading not allowed with entity#", S);
10080 end if;
8ed68165 10081
82c80734
RD
10082 -- If S is a derived operation for an untagged type then by
10083 -- definition it's not a dispatching operation (even if the parent
e917aec2
RD
10084 -- operation was dispatching), so Check_Dispatching_Operation is not
10085 -- called in that case.
996ae0b0 10086
c8ef728f 10087 if No (Derived_Type)
996ae0b0
RK
10088 or else Is_Tagged_Type (Derived_Type)
10089 then
10090 Check_Dispatching_Operation (S, Empty);
10091 end if;
10092 end if;
10093
82c80734
RD
10094 -- If this is a user-defined equality operator that is not a derived
10095 -- subprogram, create the corresponding inequality. If the operation is
10096 -- dispatching, the expansion is done elsewhere, and we do not create
10097 -- an explicit inequality operation.
996ae0b0
RK
10098
10099 <<Check_Inequality>>
10100 if Chars (S) = Name_Op_Eq
10101 and then Etype (S) = Standard_Boolean
10102 and then Present (Parent (S))
10103 and then not Is_Dispatching_Operation (S)
10104 then
10105 Make_Inequality_Operator (S);
b2834fbd 10106 Check_Untagged_Equality (S);
996ae0b0 10107 end if;
996ae0b0
RK
10108 end New_Overloaded_Entity;
10109
10110 ---------------------
10111 -- Process_Formals --
10112 ---------------------
10113
10114 procedure Process_Formals
07fc65c4 10115 (T : List_Id;
996ae0b0
RK
10116 Related_Nod : Node_Id)
10117 is
10118 Param_Spec : Node_Id;
10119 Formal : Entity_Id;
10120 Formal_Type : Entity_Id;
10121 Default : Node_Id;
10122 Ptype : Entity_Id;
10123
800621e0
RD
10124 Num_Out_Params : Nat := 0;
10125 First_Out_Param : Entity_Id := Empty;
21d27997 10126 -- Used for setting Is_Only_Out_Parameter
800621e0 10127
7b56a91b 10128 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
950d217a
AC
10129 -- Determine whether an access type designates a type coming from a
10130 -- limited view.
10131
07fc65c4 10132 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
82c80734
RD
10133 -- Check whether the default has a class-wide type. After analysis the
10134 -- default has the type of the formal, so we must also check explicitly
10135 -- for an access attribute.
07fc65c4 10136
7b56a91b
AC
10137 ----------------------------------
10138 -- Designates_From_Limited_With --
10139 ----------------------------------
950d217a 10140
7b56a91b 10141 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
950d217a
AC
10142 Desig : Entity_Id := Typ;
10143
10144 begin
10145 if Is_Access_Type (Desig) then
10146 Desig := Directly_Designated_Type (Desig);
10147 end if;
10148
10149 if Is_Class_Wide_Type (Desig) then
10150 Desig := Root_Type (Desig);
10151 end if;
10152
10153 return
7b56a91b
AC
10154 Ekind (Desig) = E_Incomplete_Type
10155 and then From_Limited_With (Desig);
10156 end Designates_From_Limited_With;
950d217a 10157
07fc65c4
GB
10158 ---------------------------
10159 -- Is_Class_Wide_Default --
10160 ---------------------------
10161
10162 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
10163 begin
10164 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
10165 or else (Nkind (D) = N_Attribute_Reference
0f853035
YM
10166 and then Attribute_Name (D) = Name_Access
10167 and then Is_Class_Wide_Type (Etype (Prefix (D))));
07fc65c4
GB
10168 end Is_Class_Wide_Default;
10169
10170 -- Start of processing for Process_Formals
10171
996ae0b0
RK
10172 begin
10173 -- In order to prevent premature use of the formals in the same formal
10174 -- part, the Ekind is left undefined until all default expressions are
10175 -- analyzed. The Ekind is established in a separate loop at the end.
10176
10177 Param_Spec := First (T);
996ae0b0 10178 while Present (Param_Spec) loop
996ae0b0 10179 Formal := Defining_Identifier (Param_Spec);
5d37ba92 10180 Set_Never_Set_In_Source (Formal, True);
996ae0b0
RK
10181 Enter_Name (Formal);
10182
10183 -- Case of ordinary parameters
10184
10185 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
10186 Find_Type (Parameter_Type (Param_Spec));
10187 Ptype := Parameter_Type (Param_Spec);
10188
10189 if Ptype = Error then
10190 goto Continue;
10191 end if;
10192
10193 Formal_Type := Entity (Ptype);
10194
ec4867fa
ES
10195 if Is_Incomplete_Type (Formal_Type)
10196 or else
10197 (Is_Class_Wide_Type (Formal_Type)
8fde064e 10198 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
996ae0b0 10199 then
93bcda23
AC
10200 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
10201 -- primitive operations, as long as their completion is
10202 -- in the same declarative part. If in the private part
10203 -- this means that the type cannot be a Taft-amendment type.
cec29135
ES
10204 -- Check is done on package exit. For access to subprograms,
10205 -- the use is legal for Taft-amendment types.
fbf5a39b 10206
6eddd7b4
AC
10207 -- Ada 2012: tagged incomplete types are allowed as generic
10208 -- formal types. They do not introduce dependencies and the
10209 -- corresponding generic subprogram does not have a delayed
5b6f12c7
AC
10210 -- freeze, because it does not need a freeze node. However,
10211 -- it is still the case that untagged incomplete types cannot
10212 -- be Taft-amendment types and must be completed in private
10213 -- part, so the subprogram must appear in the list of private
a0a10853
AC
10214 -- dependents of the type. If the type is class-wide, it is
10215 -- not a primitive, but the freezing of the subprogram must
10216 -- also be delayed to force the creation of a freeze node.
5b6f12c7
AC
10217
10218 if Is_Tagged_Type (Formal_Type)
b973629e 10219 or else (Ada_Version >= Ada_2012
1ebc2612
AC
10220 and then not From_Limited_With (Formal_Type)
10221 and then not Is_Generic_Type (Formal_Type))
5b6f12c7 10222 then
93bcda23 10223 if Ekind (Scope (Current_Scope)) = E_Package
6eddd7b4 10224 and then not Is_Generic_Type (Formal_Type)
93bcda23 10225 then
cec29135
ES
10226 if not Nkind_In
10227 (Parent (T), N_Access_Function_Definition,
10228 N_Access_Procedure_Definition)
10229 then
a0a10853
AC
10230 if not Is_Class_Wide_Type (Formal_Type) then
10231 Append_Elmt (Current_Scope,
10232 Private_Dependents (Base_Type (Formal_Type)));
10233 end if;
4637729f
AC
10234
10235 -- Freezing is delayed to ensure that Register_Prim
10236 -- will get called for this operation, which is needed
10237 -- in cases where static dispatch tables aren't built.
10238 -- (Note that the same is done for controlling access
10239 -- parameter cases in function Access_Definition.)
10240
13fa2acb
AC
10241 if not Is_Thunk (Current_Scope) then
10242 Set_Has_Delayed_Freeze (Current_Scope);
10243 end if;
cec29135 10244 end if;
93bcda23 10245 end if;
fbf5a39b 10246
0a36105d
JM
10247 -- Special handling of Value_Type for CIL case
10248
10249 elsif Is_Value_Type (Formal_Type) then
10250 null;
10251
800621e0
RD
10252 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
10253 N_Access_Procedure_Definition)
996ae0b0 10254 then
dd386db0
AC
10255 -- AI05-0151: Tagged incomplete types are allowed in all
10256 -- formal parts. Untagged incomplete types are not allowed
b973629e
AC
10257 -- in bodies. Limited views of either kind are not allowed
10258 -- if there is no place at which the non-limited view can
10259 -- become available.
a18e3d62 10260
1ebc2612
AC
10261 -- Incomplete formal untagged types are not allowed in
10262 -- subprogram bodies (but are legal in their declarations).
10263
10264 if Is_Generic_Type (Formal_Type)
10265 and then not Is_Tagged_Type (Formal_Type)
10266 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
10267 then
10268 Error_Msg_N
10269 ("invalid use of formal incomplete type", Param_Spec);
dd386db0 10270
1ebc2612 10271 elsif Ada_Version >= Ada_2012 then
b973629e
AC
10272 if Is_Tagged_Type (Formal_Type)
10273 and then (not From_Limited_With (Formal_Type)
10274 or else not In_Package_Body)
10275 then
dd386db0
AC
10276 null;
10277
0f1a6a0b 10278 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
b973629e 10279 N_Accept_Alternative,
0f1a6a0b
AC
10280 N_Entry_Body,
10281 N_Subprogram_Body)
dd386db0
AC
10282 then
10283 Error_Msg_NE
10284 ("invalid use of untagged incomplete type&",
0f1a6a0b 10285 Ptype, Formal_Type);
dd386db0
AC
10286 end if;
10287
10288 else
10289 Error_Msg_NE
10290 ("invalid use of incomplete type&",
0f1a6a0b 10291 Param_Spec, Formal_Type);
dd386db0
AC
10292
10293 -- Further checks on the legality of incomplete types
10294 -- in formal parts are delayed until the freeze point
10295 -- of the enclosing subprogram or access to subprogram.
10296 end if;
996ae0b0
RK
10297 end if;
10298
10299 elsif Ekind (Formal_Type) = E_Void then
0f1a6a0b
AC
10300 Error_Msg_NE
10301 ("premature use of&",
10302 Parameter_Type (Param_Spec), Formal_Type);
996ae0b0
RK
10303 end if;
10304
fecbd779
AC
10305 -- Ada 2012 (AI-142): Handle aliased parameters
10306
10307 if Ada_Version >= Ada_2012
10308 and then Aliased_Present (Param_Spec)
10309 then
10310 Set_Is_Aliased (Formal);
10311 end if;
10312
0ab80019 10313 -- Ada 2005 (AI-231): Create and decorate an internal subtype
7324bf49 10314 -- declaration corresponding to the null-excluding type of the
d8db0bca
JM
10315 -- formal in the enclosing scope. Finally, replace the parameter
10316 -- type of the formal with the internal subtype.
7324bf49 10317
0791fbe9 10318 if Ada_Version >= Ada_2005
41251c60 10319 and then Null_Exclusion_Present (Param_Spec)
7324bf49 10320 then
ec4867fa 10321 if not Is_Access_Type (Formal_Type) then
ed2233dc 10322 Error_Msg_N
0a36105d
JM
10323 ("`NOT NULL` allowed only for an access type", Param_Spec);
10324
ec4867fa
ES
10325 else
10326 if Can_Never_Be_Null (Formal_Type)
10327 and then Comes_From_Source (Related_Nod)
10328 then
ed2233dc 10329 Error_Msg_NE
0a36105d 10330 ("`NOT NULL` not allowed (& already excludes null)",
0f1a6a0b 10331 Param_Spec, Formal_Type);
ec4867fa 10332 end if;
41251c60 10333
ec4867fa
ES
10334 Formal_Type :=
10335 Create_Null_Excluding_Itype
10336 (T => Formal_Type,
10337 Related_Nod => Related_Nod,
10338 Scope_Id => Scope (Current_Scope));
0a36105d 10339
fcf848c4
AC
10340 -- If the designated type of the itype is an itype that is
10341 -- not frozen yet, we set the Has_Delayed_Freeze attribute
10342 -- on the access subtype, to prevent order-of-elaboration
10343 -- issues in the backend.
0a36105d
JM
10344
10345 -- Example:
10346 -- type T is access procedure;
10347 -- procedure Op (O : not null T);
10348
fcf848c4
AC
10349 if Is_Itype (Directly_Designated_Type (Formal_Type))
10350 and then
10351 not Is_Frozen (Directly_Designated_Type (Formal_Type))
10352 then
0a36105d
JM
10353 Set_Has_Delayed_Freeze (Formal_Type);
10354 end if;
ec4867fa 10355 end if;
7324bf49
AC
10356 end if;
10357
996ae0b0
RK
10358 -- An access formal type
10359
10360 else
10361 Formal_Type :=
10362 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
7324bf49 10363
f937473f
RD
10364 -- No need to continue if we already notified errors
10365
10366 if not Present (Formal_Type) then
10367 return;
10368 end if;
10369
0ab80019 10370 -- Ada 2005 (AI-254)
7324bf49 10371
af4b9434
AC
10372 declare
10373 AD : constant Node_Id :=
10374 Access_To_Subprogram_Definition
10375 (Parameter_Type (Param_Spec));
10376 begin
10377 if Present (AD) and then Protected_Present (AD) then
10378 Formal_Type :=
10379 Replace_Anonymous_Access_To_Protected_Subprogram
f937473f 10380 (Param_Spec);
af4b9434
AC
10381 end if;
10382 end;
996ae0b0
RK
10383 end if;
10384
10385 Set_Etype (Formal, Formal_Type);
0f853035 10386
fecbd779
AC
10387 -- Deal with default expression if present
10388
fbf5a39b 10389 Default := Expression (Param_Spec);
996ae0b0
RK
10390
10391 if Present (Default) then
ce5ba43a 10392 Check_SPARK_05_Restriction
fe5d3068 10393 ("default expression is not allowed", Default);
38171f43 10394
996ae0b0 10395 if Out_Present (Param_Spec) then
ed2233dc 10396 Error_Msg_N
996ae0b0
RK
10397 ("default initialization only allowed for IN parameters",
10398 Param_Spec);
10399 end if;
10400
10401 -- Do the special preanalysis of the expression (see section on
10402 -- "Handling of Default Expressions" in the spec of package Sem).
10403
21d27997 10404 Preanalyze_Spec_Expression (Default, Formal_Type);
996ae0b0 10405
f29b857f
ES
10406 -- An access to constant cannot be the default for
10407 -- an access parameter that is an access to variable.
2eb160f2
ST
10408
10409 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10410 and then not Is_Access_Constant (Formal_Type)
10411 and then Is_Access_Type (Etype (Default))
10412 and then Is_Access_Constant (Etype (Default))
10413 then
f29b857f 10414 Error_Msg_N
84c0a895
AC
10415 ("formal that is access to variable cannot be initialized "
10416 & "with an access-to-constant expression", Default);
2eb160f2
ST
10417 end if;
10418
d8db0bca
JM
10419 -- Check that the designated type of an access parameter's default
10420 -- is not a class-wide type unless the parameter's designated type
10421 -- is also class-wide.
996ae0b0
RK
10422
10423 if Ekind (Formal_Type) = E_Anonymous_Access_Type
7b56a91b 10424 and then not Designates_From_Limited_With (Formal_Type)
07fc65c4 10425 and then Is_Class_Wide_Default (Default)
996ae0b0
RK
10426 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
10427 then
07fc65c4
GB
10428 Error_Msg_N
10429 ("access to class-wide expression not allowed here", Default);
996ae0b0 10430 end if;
4755cce9
JM
10431
10432 -- Check incorrect use of dynamically tagged expressions
10433
10434 if Is_Tagged_Type (Formal_Type) then
10435 Check_Dynamically_Tagged_Expression
10436 (Expr => Default,
10437 Typ => Formal_Type,
10438 Related_Nod => Default);
10439 end if;
996ae0b0
RK
10440 end if;
10441
41251c60
JM
10442 -- Ada 2005 (AI-231): Static checks
10443
0791fbe9 10444 if Ada_Version >= Ada_2005
41251c60
JM
10445 and then Is_Access_Type (Etype (Formal))
10446 and then Can_Never_Be_Null (Etype (Formal))
10447 then
10448 Null_Exclusion_Static_Checks (Param_Spec);
10449 end if;
10450
f1bd0415
AC
10451 -- The following checks are relevant when SPARK_Mode is on as these
10452 -- are not standard Ada legality rules.
6c3c671e 10453
ea26c8e4
HK
10454 if SPARK_Mode = On then
10455 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
f1bd0415 10456
ea26c8e4
HK
10457 -- A function cannot have a parameter of mode IN OUT or OUT
10458 -- (SPARK RM 6.1).
f1bd0415 10459
ea26c8e4
HK
10460 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
10461 Error_Msg_N
10462 ("function cannot have parameter of mode `OUT` or "
10463 & "`IN OUT`", Formal);
f1bd0415 10464
d780e54f
AC
10465 -- A function cannot have an effectively volatile formal
10466 -- parameter (SPARK RM 7.1.3(10)).
ea26c8e4 10467
d780e54f 10468 elsif Is_Effectively_Volatile (Formal) then
ea26c8e4
HK
10469 Error_Msg_N
10470 ("function cannot have a volatile formal parameter",
10471 Formal);
10472 end if;
10473
d780e54f
AC
10474 -- A procedure cannot have an effectively volatile formal
10475 -- parameter of mode IN because it behaves as a constant
10476 -- (SPARK RM 7.1.3(6)).
ea26c8e4
HK
10477
10478 elsif Ekind (Scope (Formal)) = E_Procedure
10479 and then Ekind (Formal) = E_In_Parameter
d780e54f 10480 and then Is_Effectively_Volatile (Formal)
ea26c8e4 10481 then
f1bd0415 10482 Error_Msg_N
ea26c8e4 10483 ("formal parameter of mode `IN` cannot be volatile", Formal);
f1bd0415 10484 end if;
6c3c671e
AC
10485 end if;
10486
996ae0b0
RK
10487 <<Continue>>
10488 Next (Param_Spec);
10489 end loop;
10490
82c80734
RD
10491 -- If this is the formal part of a function specification, analyze the
10492 -- subtype mark in the context where the formals are visible but not
10493 -- yet usable, and may hide outer homographs.
10494
10495 if Nkind (Related_Nod) = N_Function_Specification then
10496 Analyze_Return_Type (Related_Nod);
10497 end if;
10498
996ae0b0
RK
10499 -- Now set the kind (mode) of each formal
10500
10501 Param_Spec := First (T);
996ae0b0
RK
10502 while Present (Param_Spec) loop
10503 Formal := Defining_Identifier (Param_Spec);
10504 Set_Formal_Mode (Formal);
10505
10506 if Ekind (Formal) = E_In_Parameter then
10507 Set_Default_Value (Formal, Expression (Param_Spec));
10508
10509 if Present (Expression (Param_Spec)) then
10510 Default := Expression (Param_Spec);
10511
10512 if Is_Scalar_Type (Etype (Default)) then
5ebfaacf
AC
10513 if Nkind (Parameter_Type (Param_Spec)) /=
10514 N_Access_Definition
996ae0b0
RK
10515 then
10516 Formal_Type := Entity (Parameter_Type (Param_Spec));
996ae0b0 10517 else
5ebfaacf
AC
10518 Formal_Type :=
10519 Access_Definition
10520 (Related_Nod, Parameter_Type (Param_Spec));
996ae0b0
RK
10521 end if;
10522
10523 Apply_Scalar_Range_Check (Default, Formal_Type);
10524 end if;
2820d220 10525 end if;
800621e0
RD
10526
10527 elsif Ekind (Formal) = E_Out_Parameter then
10528 Num_Out_Params := Num_Out_Params + 1;
10529
10530 if Num_Out_Params = 1 then
10531 First_Out_Param := Formal;
10532 end if;
10533
10534 elsif Ekind (Formal) = E_In_Out_Parameter then
10535 Num_Out_Params := Num_Out_Params + 1;
996ae0b0
RK
10536 end if;
10537
4172a8e3
AC
10538 -- Skip remaining processing if formal type was in error
10539
10540 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
10541 goto Next_Parameter;
10542 end if;
10543
fecbd779
AC
10544 -- Force call by reference if aliased
10545
10546 if Is_Aliased (Formal) then
10547 Set_Mechanism (Formal, By_Reference);
5ebfaacf
AC
10548
10549 -- Warn if user asked this to be passed by copy
10550
10551 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10552 Error_Msg_N
b785e0b8 10553 ("cannot pass aliased parameter & by copy??", Formal);
5ebfaacf
AC
10554 end if;
10555
10556 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
10557
10558 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10559 Set_Mechanism (Formal, By_Copy);
10560
10561 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
10562 Set_Mechanism (Formal, By_Reference);
fecbd779
AC
10563 end if;
10564
4172a8e3 10565 <<Next_Parameter>>
996ae0b0
RK
10566 Next (Param_Spec);
10567 end loop;
800621e0
RD
10568
10569 if Present (First_Out_Param) and then Num_Out_Params = 1 then
10570 Set_Is_Only_Out_Parameter (First_Out_Param);
10571 end if;
996ae0b0
RK
10572 end Process_Formals;
10573
fbf5a39b
AC
10574 ----------------------------
10575 -- Reference_Body_Formals --
10576 ----------------------------
10577
10578 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
10579 Fs : Entity_Id;
10580 Fb : Entity_Id;
10581
10582 begin
10583 if Error_Posted (Spec) then
10584 return;
10585 end if;
10586
0a36105d
JM
10587 -- Iterate over both lists. They may be of different lengths if the two
10588 -- specs are not conformant.
10589
fbf5a39b
AC
10590 Fs := First_Formal (Spec);
10591 Fb := First_Formal (Bod);
0a36105d 10592 while Present (Fs) and then Present (Fb) loop
fbf5a39b
AC
10593 Generate_Reference (Fs, Fb, 'b');
10594
10595 if Style_Check then
10596 Style.Check_Identifier (Fb, Fs);
10597 end if;
10598
10599 Set_Spec_Entity (Fb, Fs);
10600 Set_Referenced (Fs, False);
10601 Next_Formal (Fs);
10602 Next_Formal (Fb);
10603 end loop;
10604 end Reference_Body_Formals;
10605
996ae0b0
RK
10606 -------------------------
10607 -- Set_Actual_Subtypes --
10608 -------------------------
10609
10610 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
84c0a895
AC
10611 Decl : Node_Id;
10612 Formal : Entity_Id;
10613 T : Entity_Id;
10614 First_Stmt : Node_Id := Empty;
10615 AS_Needed : Boolean;
996ae0b0
RK
10616
10617 begin
f3d57416 10618 -- If this is an empty initialization procedure, no need to create
fbf5a39b
AC
10619 -- actual subtypes (small optimization).
10620
8fde064e 10621 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
fbf5a39b
AC
10622 return;
10623 end if;
10624
996ae0b0
RK
10625 Formal := First_Formal (Subp);
10626 while Present (Formal) loop
10627 T := Etype (Formal);
10628
e895b435 10629 -- We never need an actual subtype for a constrained formal
996ae0b0
RK
10630
10631 if Is_Constrained (T) then
10632 AS_Needed := False;
10633
82c80734 10634 -- If we have unknown discriminants, then we do not need an actual
a90bd866 10635 -- subtype, or more accurately we cannot figure it out. Note that
82c80734 10636 -- all class-wide types have unknown discriminants.
996ae0b0
RK
10637
10638 elsif Has_Unknown_Discriminants (T) then
10639 AS_Needed := False;
10640
82c80734
RD
10641 -- At this stage we have an unconstrained type that may need an
10642 -- actual subtype. For sure the actual subtype is needed if we have
10643 -- an unconstrained array type.
996ae0b0
RK
10644
10645 elsif Is_Array_Type (T) then
10646 AS_Needed := True;
10647
d8db0bca
JM
10648 -- The only other case needing an actual subtype is an unconstrained
10649 -- record type which is an IN parameter (we cannot generate actual
10650 -- subtypes for the OUT or IN OUT case, since an assignment can
10651 -- change the discriminant values. However we exclude the case of
10652 -- initialization procedures, since discriminants are handled very
10653 -- specially in this context, see the section entitled "Handling of
10654 -- Discriminants" in Einfo.
10655
10656 -- We also exclude the case of Discrim_SO_Functions (functions used
10657 -- in front end layout mode for size/offset values), since in such
10658 -- functions only discriminants are referenced, and not only are such
10659 -- subtypes not needed, but they cannot always be generated, because
10660 -- of order of elaboration issues.
996ae0b0
RK
10661
10662 elsif Is_Record_Type (T)
10663 and then Ekind (Formal) = E_In_Parameter
10664 and then Chars (Formal) /= Name_uInit
5d09245e 10665 and then not Is_Unchecked_Union (T)
996ae0b0
RK
10666 and then not Is_Discrim_SO_Function (Subp)
10667 then
10668 AS_Needed := True;
10669
10670 -- All other cases do not need an actual subtype
10671
10672 else
10673 AS_Needed := False;
10674 end if;
10675
10676 -- Generate actual subtypes for unconstrained arrays and
10677 -- unconstrained discriminated records.
10678
10679 if AS_Needed then
7324bf49 10680 if Nkind (N) = N_Accept_Statement then
fbf5a39b 10681
57a8057a 10682 -- If expansion is active, the formal is replaced by a local
fbf5a39b
AC
10683 -- variable that renames the corresponding entry of the
10684 -- parameter block, and it is this local variable that may
da94696d 10685 -- require an actual subtype.
fbf5a39b 10686
4460a9bc 10687 if Expander_Active then
fbf5a39b
AC
10688 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
10689 else
10690 Decl := Build_Actual_Subtype (T, Formal);
10691 end if;
10692
996ae0b0
RK
10693 if Present (Handled_Statement_Sequence (N)) then
10694 First_Stmt :=
10695 First (Statements (Handled_Statement_Sequence (N)));
10696 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
10697 Mark_Rewrite_Insertion (Decl);
10698 else
82c80734
RD
10699 -- If the accept statement has no body, there will be no
10700 -- reference to the actuals, so no need to compute actual
10701 -- subtypes.
996ae0b0
RK
10702
10703 return;
10704 end if;
10705
10706 else
fbf5a39b 10707 Decl := Build_Actual_Subtype (T, Formal);
996ae0b0
RK
10708 Prepend (Decl, Declarations (N));
10709 Mark_Rewrite_Insertion (Decl);
10710 end if;
10711
82c80734
RD
10712 -- The declaration uses the bounds of an existing object, and
10713 -- therefore needs no constraint checks.
2820d220 10714
7324bf49 10715 Analyze (Decl, Suppress => All_Checks);
2820d220 10716
996ae0b0
RK
10717 -- We need to freeze manually the generated type when it is
10718 -- inserted anywhere else than in a declarative part.
10719
10720 if Present (First_Stmt) then
10721 Insert_List_Before_And_Analyze (First_Stmt,
c159409f 10722 Freeze_Entity (Defining_Identifier (Decl), N));
fcadacf7
ES
10723
10724 -- Ditto if the type has a dynamic predicate, because the
10725 -- generated function will mention the actual subtype.
10726
10727 elsif Has_Dynamic_Predicate_Aspect (T) then
10728 Insert_List_Before_And_Analyze (Decl,
10729 Freeze_Entity (Defining_Identifier (Decl), N));
996ae0b0
RK
10730 end if;
10731
fbf5a39b 10732 if Nkind (N) = N_Accept_Statement
4460a9bc 10733 and then Expander_Active
fbf5a39b
AC
10734 then
10735 Set_Actual_Subtype (Renamed_Object (Formal),
10736 Defining_Identifier (Decl));
10737 else
10738 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
10739 end if;
996ae0b0
RK
10740 end if;
10741
10742 Next_Formal (Formal);
10743 end loop;
10744 end Set_Actual_Subtypes;
10745
10746 ---------------------
10747 -- Set_Formal_Mode --
10748 ---------------------
10749
10750 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
10751 Spec : constant Node_Id := Parent (Formal_Id);
10752
10753 begin
10754 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
10755 -- since we ensure that corresponding actuals are always valid at the
10756 -- point of the call.
10757
10758 if Out_Present (Spec) then
fc999c5d
RD
10759 if Ekind_In (Scope (Formal_Id), E_Function, E_Generic_Function) then
10760
b4ca2d2c 10761 -- [IN] OUT parameters allowed for functions in Ada 2012
c56a9ba4
AC
10762
10763 if Ada_Version >= Ada_2012 then
e6425869
AC
10764
10765 -- Even in Ada 2012 operators can only have IN parameters
10766
10767 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
10768 Error_Msg_N ("operators can only have IN parameters", Spec);
10769 end if;
10770
c56a9ba4
AC
10771 if In_Present (Spec) then
10772 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10773 else
10774 Set_Ekind (Formal_Id, E_Out_Parameter);
10775 end if;
10776
fc999c5d
RD
10777 Set_Has_Out_Or_In_Out_Parameter (Scope (Formal_Id), True);
10778
b4ca2d2c
AC
10779 -- But not in earlier versions of Ada
10780
c56a9ba4
AC
10781 else
10782 Error_Msg_N ("functions can only have IN parameters", Spec);
10783 Set_Ekind (Formal_Id, E_In_Parameter);
10784 end if;
996ae0b0
RK
10785
10786 elsif In_Present (Spec) then
10787 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10788
10789 else
fbf5a39b
AC
10790 Set_Ekind (Formal_Id, E_Out_Parameter);
10791 Set_Never_Set_In_Source (Formal_Id, True);
10792 Set_Is_True_Constant (Formal_Id, False);
10793 Set_Current_Value (Formal_Id, Empty);
996ae0b0
RK
10794 end if;
10795
10796 else
10797 Set_Ekind (Formal_Id, E_In_Parameter);
10798 end if;
10799
fbf5a39b 10800 -- Set Is_Known_Non_Null for access parameters since the language
82c80734
RD
10801 -- guarantees that access parameters are always non-null. We also set
10802 -- Can_Never_Be_Null, since there is no way to change the value.
fbf5a39b
AC
10803
10804 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
2820d220 10805
885c4871 10806 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
2813bb6b 10807 -- null; In Ada 2005, only if then null_exclusion is explicit.
2820d220 10808
0791fbe9 10809 if Ada_Version < Ada_2005
2813bb6b 10810 or else Can_Never_Be_Null (Etype (Formal_Id))
2820d220
AC
10811 then
10812 Set_Is_Known_Non_Null (Formal_Id);
10813 Set_Can_Never_Be_Null (Formal_Id);
10814 end if;
2813bb6b 10815
41251c60
JM
10816 -- Ada 2005 (AI-231): Null-exclusion access subtype
10817
2813bb6b
ES
10818 elsif Is_Access_Type (Etype (Formal_Id))
10819 and then Can_Never_Be_Null (Etype (Formal_Id))
10820 then
2813bb6b 10821 Set_Is_Known_Non_Null (Formal_Id);
a1d72281
EB
10822
10823 -- We can also set Can_Never_Be_Null (thus preventing some junk
10824 -- access checks) for the case of an IN parameter, which cannot
10825 -- be changed, or for an IN OUT parameter, which can be changed but
10826 -- not to a null value. But for an OUT parameter, the initial value
10827 -- passed in can be null, so we can't set this flag in that case.
10828
10829 if Ekind (Formal_Id) /= E_Out_Parameter then
10830 Set_Can_Never_Be_Null (Formal_Id);
10831 end if;
fbf5a39b
AC
10832 end if;
10833
996ae0b0
RK
10834 Set_Mechanism (Formal_Id, Default_Mechanism);
10835 Set_Formal_Validity (Formal_Id);
10836 end Set_Formal_Mode;
10837
10838 -------------------------
10839 -- Set_Formal_Validity --
10840 -------------------------
10841
10842 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
10843 begin
82c80734
RD
10844 -- If no validity checking, then we cannot assume anything about the
10845 -- validity of parameters, since we do not know there is any checking
10846 -- of the validity on the call side.
996ae0b0
RK
10847
10848 if not Validity_Checks_On then
10849 return;
10850
fbf5a39b
AC
10851 -- If validity checking for parameters is enabled, this means we are
10852 -- not supposed to make any assumptions about argument values.
10853
10854 elsif Validity_Check_Parameters then
10855 return;
10856
10857 -- If we are checking in parameters, we will assume that the caller is
10858 -- also checking parameters, so we can assume the parameter is valid.
10859
996ae0b0
RK
10860 elsif Ekind (Formal_Id) = E_In_Parameter
10861 and then Validity_Check_In_Params
10862 then
10863 Set_Is_Known_Valid (Formal_Id, True);
10864
fbf5a39b
AC
10865 -- Similar treatment for IN OUT parameters
10866
996ae0b0
RK
10867 elsif Ekind (Formal_Id) = E_In_Out_Parameter
10868 and then Validity_Check_In_Out_Params
10869 then
10870 Set_Is_Known_Valid (Formal_Id, True);
10871 end if;
10872 end Set_Formal_Validity;
10873
10874 ------------------------
10875 -- Subtype_Conformant --
10876 ------------------------
10877
ce2b6ba5
JM
10878 function Subtype_Conformant
10879 (New_Id : Entity_Id;
10880 Old_Id : Entity_Id;
10881 Skip_Controlling_Formals : Boolean := False) return Boolean
10882 is
996ae0b0 10883 Result : Boolean;
996ae0b0 10884 begin
ce2b6ba5
JM
10885 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
10886 Skip_Controlling_Formals => Skip_Controlling_Formals);
996ae0b0
RK
10887 return Result;
10888 end Subtype_Conformant;
10889
10890 ---------------------
10891 -- Type_Conformant --
10892 ---------------------
10893
41251c60
JM
10894 function Type_Conformant
10895 (New_Id : Entity_Id;
10896 Old_Id : Entity_Id;
10897 Skip_Controlling_Formals : Boolean := False) return Boolean
10898 is
996ae0b0 10899 Result : Boolean;
996ae0b0 10900 begin
c8ef728f 10901 May_Hide_Profile := False;
41251c60
JM
10902 Check_Conformance
10903 (New_Id, Old_Id, Type_Conformant, False, Result,
10904 Skip_Controlling_Formals => Skip_Controlling_Formals);
996ae0b0
RK
10905 return Result;
10906 end Type_Conformant;
10907
10908 -------------------------------
10909 -- Valid_Operator_Definition --
10910 -------------------------------
10911
10912 procedure Valid_Operator_Definition (Designator : Entity_Id) is
10913 N : Integer := 0;
10914 F : Entity_Id;
10915 Id : constant Name_Id := Chars (Designator);
10916 N_OK : Boolean;
10917
10918 begin
10919 F := First_Formal (Designator);
996ae0b0
RK
10920 while Present (F) loop
10921 N := N + 1;
10922
10923 if Present (Default_Value (F)) then
ed2233dc 10924 Error_Msg_N
996ae0b0
RK
10925 ("default values not allowed for operator parameters",
10926 Parent (F));
220d1fd9
AC
10927
10928 -- For function instantiations that are operators, we must check
10929 -- separately that the corresponding generic only has in-parameters.
84c0a895
AC
10930 -- For subprogram declarations this is done in Set_Formal_Mode. Such
10931 -- an error could not arise in earlier versions of the language.
220d1fd9
AC
10932
10933 elsif Ekind (F) /= E_In_Parameter then
84c0a895 10934 Error_Msg_N ("operators can only have IN parameters", F);
996ae0b0
RK
10935 end if;
10936
10937 Next_Formal (F);
10938 end loop;
10939
10940 -- Verify that user-defined operators have proper number of arguments
10941 -- First case of operators which can only be unary
10942
b69cd36a 10943 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
996ae0b0
RK
10944 N_OK := (N = 1);
10945
10946 -- Case of operators which can be unary or binary
10947
b69cd36a 10948 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
996ae0b0
RK
10949 N_OK := (N in 1 .. 2);
10950
10951 -- All other operators can only be binary
10952
10953 else
10954 N_OK := (N = 2);
10955 end if;
10956
10957 if not N_OK then
10958 Error_Msg_N
10959 ("incorrect number of arguments for operator", Designator);
10960 end if;
10961
10962 if Id = Name_Op_Ne
10963 and then Base_Type (Etype (Designator)) = Standard_Boolean
10964 and then not Is_Intrinsic_Subprogram (Designator)
10965 then
10966 Error_Msg_N
84c0a895 10967 ("explicit definition of inequality not allowed", Designator);
996ae0b0
RK
10968 end if;
10969 end Valid_Operator_Definition;
10970
10971end Sem_Ch6;