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