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