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