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