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