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