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