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