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