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