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