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