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