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