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