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