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