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