------------------------------------------------------------------------------ -- -- -- GNAT COMPILER COMPONENTS -- -- -- -- S E M _ C H 6 -- -- -- -- B o d y -- -- -- -- Copyright (C) 1992-2014, Free Software Foundation, Inc. -- -- -- -- GNAT is free software; you can redistribute it and/or modify it under -- -- terms of the GNU General Public License as published by the Free Soft- -- -- ware Foundation; either version 3, or (at your option) any later ver- -- -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- -- for more details. You should have received a copy of the GNU General -- -- Public License distributed with GNAT; see file COPYING3. If not, go to -- -- http://www.gnu.org/licenses for a complete copy of the license. -- -- -- -- GNAT was originally developed by the GNAT team at New York University. -- -- Extensive contributions were provided by Ada Core Technologies Inc. -- -- -- ------------------------------------------------------------------------------ with Aspects; use Aspects; with Atree; use Atree; with Checks; use Checks; with Debug; use Debug; with Einfo; use Einfo; with Elists; use Elists; with Errout; use Errout; with Expander; use Expander; with Exp_Ch6; use Exp_Ch6; with Exp_Ch7; use Exp_Ch7; with Exp_Ch9; use Exp_Ch9; with Exp_Dbug; use Exp_Dbug; with Exp_Disp; use Exp_Disp; with Exp_Tss; use Exp_Tss; with Exp_Util; use Exp_Util; with Fname; use Fname; with Freeze; use Freeze; with Itypes; use Itypes; with Lib.Xref; use Lib.Xref; with Layout; use Layout; with Namet; use Namet; with Lib; use Lib; with Nlists; use Nlists; with Nmake; use Nmake; with Opt; use Opt; with Output; use Output; with Restrict; use Restrict; with Rident; use Rident; with Rtsfind; use Rtsfind; with Sem; use Sem; with Sem_Aux; use Sem_Aux; with Sem_Cat; use Sem_Cat; with Sem_Ch3; use Sem_Ch3; with Sem_Ch4; use Sem_Ch4; with Sem_Ch5; use Sem_Ch5; with Sem_Ch8; use Sem_Ch8; with Sem_Ch10; use Sem_Ch10; with Sem_Ch12; use Sem_Ch12; with Sem_Ch13; use Sem_Ch13; with Sem_Dim; use Sem_Dim; with Sem_Disp; use Sem_Disp; with Sem_Dist; use Sem_Dist; with Sem_Elim; use Sem_Elim; with Sem_Eval; use Sem_Eval; with Sem_Mech; use Sem_Mech; with Sem_Prag; use Sem_Prag; with Sem_Res; use Sem_Res; with Sem_Util; use Sem_Util; with Sem_Type; use Sem_Type; with Sem_Warn; use Sem_Warn; with Sinput; use Sinput; with Stand; use Stand; with Sinfo; use Sinfo; with Sinfo.CN; use Sinfo.CN; with Snames; use Snames; with Stringt; use Stringt; with Style; with Stylesw; use Stylesw; with Targparm; use Targparm; with Tbuild; use Tbuild; with Uintp; use Uintp; with Urealp; use Urealp; with Validsw; use Validsw; package body Sem_Ch6 is May_Hide_Profile : Boolean := False; -- This flag is used to indicate that two formals in two subprograms being -- checked for conformance differ only in that one is an access parameter -- while the other is of a general access type with the same designated -- type. In this case, if the rest of the signatures match, a call to -- either subprogram may be ambiguous, which is worth a warning. The flag -- is set in Compatible_Types, and the warning emitted in -- New_Overloaded_Entity. ----------------------- -- Local Subprograms -- ----------------------- procedure Analyze_Null_Procedure (N : Node_Id; Is_Completion : out Boolean); -- A null procedure can be a declaration or (Ada 2012) a completion. procedure Analyze_Return_Statement (N : Node_Id); -- Common processing for simple and extended return statements procedure Analyze_Function_Return (N : Node_Id); -- Subsidiary to Analyze_Return_Statement. Called when the return statement -- applies to a [generic] function. procedure Analyze_Return_Type (N : Node_Id); -- Subsidiary to Process_Formals: analyze subtype mark in function -- specification in a context where the formals are visible and hide -- outer homographs. procedure Analyze_Subprogram_Body_Helper (N : Node_Id); -- Does all the real work of Analyze_Subprogram_Body. This is split out so -- that we can use RETURN but not skip the debug output at the end. procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id); -- Analyze a generic subprogram body. N is the body to be analyzed, and -- Gen_Id is the defining entity Id for the corresponding spec. procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id); -- If a subprogram has pragma Inline and inlining is active, use generic -- machinery to build an unexpanded body for the subprogram. This body is -- subsequently used for inline expansions at call sites. If subprogram can -- be inlined (depending on size and nature of local declarations) this -- function returns true. Otherwise subprogram body is treated normally. -- If proper warnings are enabled and the subprogram contains a construct -- that cannot be inlined, the offending construct is flagged accordingly. function Can_Override_Operator (Subp : Entity_Id) return Boolean; -- Returns true if Subp can override a predefined operator. procedure Check_And_Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id; Body_Id : Entity_Id); -- Spec_Id and Body_Id are the entities of the specification and body of -- the subprogram body N. If N can be inlined by the frontend (supported -- cases documented in Check_Body_To_Inline) then build the body-to-inline -- associated with N and attach it to the declaration node of Spec_Id. procedure Check_Conformance (New_Id : Entity_Id; Old_Id : Entity_Id; Ctype : Conformance_Type; Errmsg : Boolean; Conforms : out Boolean; Err_Loc : Node_Id := Empty; Get_Inst : Boolean := False; Skip_Controlling_Formals : Boolean := False); -- Given two entities, this procedure checks that the profiles associated -- with these entities meet the conformance criterion given by the third -- parameter. If they conform, Conforms is set True and control returns -- to the caller. If they do not conform, Conforms is set to False, and -- in addition, if Errmsg is True on the call, proper messages are output -- to complain about the conformance failure. If Err_Loc is non_Empty -- the error messages are placed on Err_Loc, if Err_Loc is empty, then -- error messages are placed on the appropriate part of the construct -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance -- against a formal access-to-subprogram type so Get_Instance_Of must -- be called. procedure Check_Subprogram_Order (N : Node_Id); -- N is the N_Subprogram_Body node for a subprogram. This routine applies -- the alpha ordering rule for N if this ordering requirement applicable. procedure Check_Returns (HSS : Node_Id; Mode : Character; Err : out Boolean; Proc : Entity_Id := Empty); -- Called to check for missing return statements in a function body, or for -- returns present in a procedure body which has No_Return set. HSS is the -- handled statement sequence for the subprogram body. This procedure -- checks all flow paths to make sure they either have return (Mode = 'F', -- used for functions) or do not have a return (Mode = 'P', used for -- No_Return procedures). The flag Err is set if there are any control -- paths not explicitly terminated by a return in the function case, and is -- True otherwise. Proc is the entity for the procedure case and is used -- in posting the warning message. procedure Check_Untagged_Equality (Eq_Op : Entity_Id); -- In Ada 2012, a primitive equality operator on an untagged record type -- must appear before the type is frozen, and have the same visibility as -- that of the type. This procedure checks that this rule is met, and -- otherwise emits an error on the subprogram declaration and a warning -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode, -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility -- is set, otherwise the call has no effect. procedure Enter_Overloaded_Entity (S : Entity_Id); -- This procedure makes S, a new overloaded entity, into the first visible -- entity with that name. function Is_Non_Overriding_Operation (Prev_E : Entity_Id; New_E : Entity_Id) return Boolean; -- Enforce the rule given in 12.3(18): a private operation in an instance -- overrides an inherited operation only if the corresponding operation -- was overriding in the generic. This needs to be checked for primitive -- operations of types derived (in the generic unit) from formal private -- or formal derived types. procedure Make_Inequality_Operator (S : Entity_Id); -- Create the declaration for an inequality operator that is implicitly -- created by a user-defined equality operator that yields a boolean. procedure Set_Formal_Validity (Formal_Id : Entity_Id); -- Formal_Id is an formal parameter entity. This procedure deals with -- setting the proper validity status for this entity, which depends on -- the kind of parameter and the validity checking mode. --------------------------------------------- -- Analyze_Abstract_Subprogram_Declaration -- --------------------------------------------- procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is Designator : constant Entity_Id := Analyze_Subprogram_Specification (Specification (N)); Scop : constant Entity_Id := Current_Scope; begin Check_SPARK_Restriction ("abstract subprogram is not allowed", N); Generate_Definition (Designator); Set_Contract (Designator, Make_Contract (Sloc (Designator))); Set_Is_Abstract_Subprogram (Designator); New_Overloaded_Entity (Designator); Check_Delayed_Subprogram (Designator); Set_Categorization_From_Scope (Designator, Scop); if Ekind (Scope (Designator)) = E_Protected_Type then Error_Msg_N ("abstract subprogram not allowed in protected type", N); -- Issue a warning if the abstract subprogram is neither a dispatching -- operation nor an operation that overrides an inherited subprogram or -- predefined operator, since this most likely indicates a mistake. elsif Warn_On_Redundant_Constructs and then not Is_Dispatching_Operation (Designator) and then not Present (Overridden_Operation (Designator)) and then (not Is_Operator_Symbol_Name (Chars (Designator)) or else Scop /= Scope (Etype (First_Formal (Designator)))) then Error_Msg_N ("abstract subprogram is not dispatching or overriding?r?", N); end if; Generate_Reference_To_Formals (Designator); Check_Eliminated (Designator); if Has_Aspects (N) then Analyze_Aspect_Specifications (N, Designator); end if; end Analyze_Abstract_Subprogram_Declaration; --------------------------------- -- Analyze_Expression_Function -- --------------------------------- procedure Analyze_Expression_Function (N : Node_Id) is Loc : constant Source_Ptr := Sloc (N); LocX : constant Source_Ptr := Sloc (Expression (N)); Expr : constant Node_Id := Expression (N); Spec : constant Node_Id := Specification (N); Def_Id : Entity_Id; Prev : Entity_Id; -- If the expression is a completion, Prev is the entity whose -- declaration is completed. Def_Id is needed to analyze the spec. New_Body : Node_Id; New_Decl : Node_Id; New_Spec : Node_Id; Ret : Node_Id; begin -- This is one of the occasions on which we transform the tree during -- semantic analysis. If this is a completion, transform the expression -- function into an equivalent subprogram body, and analyze it. -- Expression functions are inlined unconditionally. The back-end will -- determine whether this is possible. Inline_Processing_Required := True; -- Create a specification for the generated body. Types and defauts in -- the profile are copies of the spec, but new entities must be created -- for the unit name and the formals. New_Spec := New_Copy_Tree (Spec); Set_Defining_Unit_Name (New_Spec, Make_Defining_Identifier (Sloc (Defining_Unit_Name (Spec)), Chars (Defining_Unit_Name (Spec)))); if Present (Parameter_Specifications (New_Spec)) then declare Formal_Spec : Node_Id; Def : Entity_Id; begin Formal_Spec := First (Parameter_Specifications (New_Spec)); -- Create a new formal parameter at the same source position while Present (Formal_Spec) loop Def := Defining_Identifier (Formal_Spec); Set_Defining_Identifier (Formal_Spec, Make_Defining_Identifier (Sloc (Def), Chars => Chars (Def))); Next (Formal_Spec); end loop; end; end if; Prev := Current_Entity_In_Scope (Defining_Entity (Spec)); -- If there are previous overloadable entities with the same name, -- check whether any of them is completed by the expression function. if Present (Prev) and then Is_Overloadable (Prev) then Def_Id := Analyze_Subprogram_Specification (Spec); Prev := Find_Corresponding_Spec (N); end if; Ret := Make_Simple_Return_Statement (LocX, Expression (N)); New_Body := Make_Subprogram_Body (Loc, Specification => New_Spec, Declarations => Empty_List, Handled_Statement_Sequence => Make_Handled_Sequence_Of_Statements (LocX, Statements => New_List (Ret))); -- If the expression completes a generic subprogram, we must create a -- separate node for the body, because at instantiation the original -- node of the generic copy must be a generic subprogram body, and -- cannot be a expression function. Otherwise we just rewrite the -- expression with the non-generic body. if Present (Prev) and then Ekind (Prev) = E_Generic_Function then Insert_After (N, New_Body); -- Propagate any aspects or pragmas that apply to the expression -- function to the proper body when the expression function acts -- as a completion. if Has_Aspects (N) then Move_Aspects (N, To => New_Body); end if; Relocate_Pragmas_To_Body (New_Body); Rewrite (N, Make_Null_Statement (Loc)); Set_Has_Completion (Prev, False); Analyze (N); Analyze (New_Body); Set_Is_Inlined (Prev); elsif Present (Prev) and then Comes_From_Source (Prev) then Set_Has_Completion (Prev, False); -- An expression function that is a completion freezes the -- expression. This means freezing the return type, and if it is -- an access type, freezing its designated type as well. -- Note that we cannot defer this freezing to the analysis of the -- expression itself, because a freeze node might appear in a nested -- scope, leading to an elaboration order issue in gigi. Freeze_Before (N, Etype (Prev)); if Is_Access_Type (Etype (Prev)) then Freeze_Before (N, Designated_Type (Etype (Prev))); end if; -- For navigation purposes, indicate that the function is a body Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True); Rewrite (N, New_Body); -- Correct the parent pointer of the aspect specification list to -- reference the rewritten node. if Has_Aspects (N) then Set_Parent (Aspect_Specifications (N), N); end if; -- Propagate any pragmas that apply to the expression function to the -- proper body when the expression function acts as a completion. -- Aspects are automatically transfered because of node rewriting. Relocate_Pragmas_To_Body (N); Analyze (N); -- Prev is the previous entity with the same name, but it is can -- be an unrelated spec that is not completed by the expression -- function. In that case the relevant entity is the one in the body. -- Not clear that the backend can inline it in this case ??? if Has_Completion (Prev) then Set_Is_Inlined (Prev); -- The formals of the expression function are body formals, -- and do not appear in the ali file, which will only contain -- references to the formals of the original subprogram spec. declare F1 : Entity_Id; F2 : Entity_Id; begin F1 := First_Formal (Def_Id); F2 := First_Formal (Prev); while Present (F1) loop Set_Spec_Entity (F1, F2); Next_Formal (F1); Next_Formal (F2); end loop; end; else Set_Is_Inlined (Defining_Entity (New_Body)); end if; -- If this is not a completion, create both a declaration and a body, so -- that the expression can be inlined whenever possible. else -- An expression function that is not a completion is not a -- subprogram declaration, and thus cannot appear in a protected -- definition. if Nkind (Parent (N)) = N_Protected_Definition then Error_Msg_N ("an expression function is not a legal protected operation", N); end if; New_Decl := Make_Subprogram_Declaration (Loc, Specification => Spec); Rewrite (N, New_Decl); -- Correct the parent pointer of the aspect specification list to -- reference the rewritten node. if Has_Aspects (N) then Set_Parent (Aspect_Specifications (N), N); end if; Analyze (N); Set_Is_Inlined (Defining_Entity (New_Decl)); -- To prevent premature freeze action, insert the new body at the end -- of the current declarations, or at the end of the package spec. -- However, resolve usage names now, to prevent spurious visibility -- on later entities. Note that the function can now be called in -- the current declarative part, which will appear to be prior to -- the presence of the body in the code. There are nevertheless no -- order of elaboration issues because all name resolution has taken -- place at the point of declaration. declare Decls : List_Id := List_Containing (N); Par : constant Node_Id := Parent (Decls); Id : constant Entity_Id := Defining_Entity (New_Decl); begin if Nkind (Par) = N_Package_Specification and then Decls = Visible_Declarations (Par) and then Present (Private_Declarations (Par)) and then not Is_Empty_List (Private_Declarations (Par)) then Decls := Private_Declarations (Par); end if; Insert_After (Last (Decls), New_Body); Push_Scope (Id); Install_Formals (Id); -- Preanalyze the expression for name capture, except in an -- instance, where this has been done during generic analysis, -- and will be redone when analyzing the body. declare Expr : constant Node_Id := Expression (Ret); begin Set_Parent (Expr, Ret); if not In_Instance then Preanalyze_Spec_Expression (Expr, Etype (Id)); end if; end; End_Scope; end; end if; -- If the return expression is a static constant, we suppress warning -- messages on unused formals, which in most cases will be noise. Set_Is_Trivial_Subprogram (Defining_Entity (New_Body), Is_OK_Static_Expression (Expr)); end Analyze_Expression_Function; ---------------------------------------- -- Analyze_Extended_Return_Statement -- ---------------------------------------- procedure Analyze_Extended_Return_Statement (N : Node_Id) is begin Analyze_Return_Statement (N); end Analyze_Extended_Return_Statement; ---------------------------- -- Analyze_Function_Call -- ---------------------------- procedure Analyze_Function_Call (N : Node_Id) is Actuals : constant List_Id := Parameter_Associations (N); Func_Nam : constant Node_Id := Name (N); Actual : Node_Id; begin Analyze (Func_Nam); -- A call of the form A.B (X) may be an Ada 2005 call, which is -- rewritten as B (A, X). If the rewriting is successful, the call -- has been analyzed and we just return. if Nkind (Func_Nam) = N_Selected_Component and then Name (N) /= Func_Nam and then Is_Rewrite_Substitution (N) and then Present (Etype (N)) then return; end if; -- If error analyzing name, then set Any_Type as result type and return if Etype (Func_Nam) = Any_Type then Set_Etype (N, Any_Type); return; end if; -- Otherwise analyze the parameters if Present (Actuals) then Actual := First (Actuals); while Present (Actual) loop Analyze (Actual); Check_Parameterless_Call (Actual); Next (Actual); end loop; end if; Analyze_Call (N); end Analyze_Function_Call; ----------------------------- -- Analyze_Function_Return -- ----------------------------- procedure Analyze_Function_Return (N : Node_Id) is Loc : constant Source_Ptr := Sloc (N); Stm_Entity : constant Entity_Id := Return_Statement_Entity (N); Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity); R_Type : constant Entity_Id := Etype (Scope_Id); -- Function result subtype procedure Check_Limited_Return (Expr : Node_Id); -- Check the appropriate (Ada 95 or Ada 2005) rules for returning -- limited types. Used only for simple return statements. -- Expr is the expression returned. procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id); -- Check that the return_subtype_indication properly matches the result -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2). -------------------------- -- Check_Limited_Return -- -------------------------- procedure Check_Limited_Return (Expr : Node_Id) is begin -- Ada 2005 (AI-318-02): Return-by-reference types have been -- removed and replaced by anonymous access results. This is an -- incompatibility with Ada 95. Not clear whether this should be -- enforced yet or perhaps controllable with special switch. ??? -- A limited interface that is not immutably limited is OK. if Is_Limited_Interface (R_Type) and then not (Is_Task_Interface (R_Type) or else Is_Protected_Interface (R_Type) or else Is_Synchronized_Interface (R_Type)) then null; elsif Is_Limited_Type (R_Type) and then not Is_Interface (R_Type) and then Comes_From_Source (N) and then not In_Instance_Body and then not OK_For_Limited_Init_In_05 (R_Type, Expr) then -- Error in Ada 2005 if Ada_Version >= Ada_2005 and then not Debug_Flag_Dot_L and then not GNAT_Mode then Error_Msg_N ("(Ada 2005) cannot copy object of a limited type " & "(RM-2005 6.5(5.5/2))", Expr); if Is_Limited_View (R_Type) then Error_Msg_N ("\return by reference not permitted in Ada 2005", Expr); end if; -- Warn in Ada 95 mode, to give folks a heads up about this -- incompatibility. -- In GNAT mode, this is just a warning, to allow it to be -- evilly turned off. Otherwise it is a real error. -- In a generic context, simplify the warning because it makes -- no sense to discuss pass-by-reference or copy. elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then if Inside_A_Generic then Error_Msg_N ("return of limited object not permitted in Ada 2005 " & "(RM-2005 6.5(5.5/2))?y?", Expr); elsif Is_Limited_View (R_Type) then Error_Msg_N ("return by reference not permitted in Ada 2005 " & "(RM-2005 6.5(5.5/2))?y?", Expr); else Error_Msg_N ("cannot copy object of a limited type in Ada 2005 " & "(RM-2005 6.5(5.5/2))?y?", Expr); end if; -- Ada 95 mode, compatibility warnings disabled else return; -- skip continuation messages below end if; if not Inside_A_Generic then Error_Msg_N ("\consider switching to return of access type", Expr); Explain_Limited_Type (R_Type, Expr); end if; end if; end Check_Limited_Return; ------------------------------------- -- Check_Return_Subtype_Indication -- ------------------------------------- procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl); R_Stm_Type : constant Entity_Id := Etype (Return_Obj); -- Subtype given in the extended return statement (must match R_Type) Subtype_Ind : constant Node_Id := Object_Definition (Original_Node (Obj_Decl)); R_Type_Is_Anon_Access : constant Boolean := Ekind_In (R_Type, E_Anonymous_Access_Subprogram_Type, E_Anonymous_Access_Protected_Subprogram_Type, E_Anonymous_Access_Type); -- True if return type of the function is an anonymous access type -- Can't we make Is_Anonymous_Access_Type in einfo ??? R_Stm_Type_Is_Anon_Access : constant Boolean := Ekind_In (R_Stm_Type, E_Anonymous_Access_Subprogram_Type, E_Anonymous_Access_Protected_Subprogram_Type, E_Anonymous_Access_Type); -- True if type of the return object is an anonymous access type procedure Error_No_Match (N : Node_Id); -- Output error messages for case where types do not statically -- match. N is the location for the messages. -------------------- -- Error_No_Match -- -------------------- procedure Error_No_Match (N : Node_Id) is begin Error_Msg_N ("subtype must statically match function result subtype", N); if not Predicates_Match (R_Stm_Type, R_Type) then Error_Msg_Node_2 := R_Type; Error_Msg_NE ("\predicate of & does not match predicate of &", N, R_Stm_Type); end if; end Error_No_Match; -- Start of processing for Check_Return_Subtype_Indication begin -- First, avoid cascaded errors if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then return; end if; -- "return access T" case; check that the return statement also has -- "access T", and that the subtypes statically match: -- if this is an access to subprogram the signatures must match. if R_Type_Is_Anon_Access then if R_Stm_Type_Is_Anon_Access then if Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type then if Base_Type (Designated_Type (R_Stm_Type)) /= Base_Type (Designated_Type (R_Type)) or else not Subtypes_Statically_Match (R_Stm_Type, R_Type) then Error_No_Match (Subtype_Mark (Subtype_Ind)); end if; else -- For two anonymous access to subprogram types, the -- types themselves must be type conformant. if not Conforming_Types (R_Stm_Type, R_Type, Fully_Conformant) then Error_No_Match (Subtype_Ind); end if; end if; else Error_Msg_N ("must use anonymous access type", Subtype_Ind); end if; -- If the return object is of an anonymous access type, then report -- an error if the function's result type is not also anonymous. elsif R_Stm_Type_Is_Anon_Access and then not R_Type_Is_Anon_Access then Error_Msg_N ("anonymous access not allowed for function with " & "named access result", Subtype_Ind); -- Subtype indication case: check that the return object's type is -- covered by the result type, and that the subtypes statically match -- when the result subtype is constrained. Also handle record types -- with unknown discriminants for which we have built the underlying -- record view. Coverage is needed to allow specific-type return -- objects when the result type is class-wide (see AI05-32). elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type)) or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type)) and then Covers (Base_Type (R_Type), Underlying_Record_View (Base_Type (R_Stm_Type)))) then -- A null exclusion may be present on the return type, on the -- function specification, on the object declaration or on the -- subtype itself. if Is_Access_Type (R_Type) and then (Can_Never_Be_Null (R_Type) or else Null_Exclusion_Present (Parent (Scope_Id))) /= Can_Never_Be_Null (R_Stm_Type) then Error_No_Match (Subtype_Ind); end if; -- AI05-103: for elementary types, subtypes must statically match if Is_Constrained (R_Type) or else Is_Access_Type (R_Type) then if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then Error_No_Match (Subtype_Ind); end if; end if; elsif Etype (Base_Type (R_Type)) = R_Stm_Type and then Is_Null_Extension (Base_Type (R_Type)) then null; else Error_Msg_N ("wrong type for return_subtype_indication", Subtype_Ind); end if; end Check_Return_Subtype_Indication; --------------------- -- Local Variables -- --------------------- Expr : Node_Id; -- Start of processing for Analyze_Function_Return begin Set_Return_Present (Scope_Id); if Nkind (N) = N_Simple_Return_Statement then Expr := Expression (N); -- Guard against a malformed expression. The parser may have tried to -- recover but the node is not analyzable. if Nkind (Expr) = N_Error then Set_Etype (Expr, Any_Type); Expander_Mode_Save_And_Set (False); return; else -- The resolution of a controlled [extension] aggregate associated -- with a return statement creates a temporary which needs to be -- finalized on function exit. Wrap the return statement inside a -- block so that the finalization machinery can detect this case. -- This early expansion is done only when the return statement is -- not part of a handled sequence of statements. if Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate) and then Needs_Finalization (R_Type) and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements then Rewrite (N, Make_Block_Statement (Loc, Handled_Statement_Sequence => Make_Handled_Sequence_Of_Statements (Loc, Statements => New_List (Relocate_Node (N))))); Analyze (N); return; end if; Analyze_And_Resolve (Expr, R_Type); Check_Limited_Return (Expr); end if; -- RETURN only allowed in SPARK as the last statement in function if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements and then (Nkind (Parent (Parent (N))) /= N_Subprogram_Body or else Present (Next (N))) then Check_SPARK_Restriction ("RETURN should be the last statement in function", N); end if; else Check_SPARK_Restriction ("extended RETURN is not allowed", N); -- Analyze parts specific to extended_return_statement: declare Obj_Decl : constant Node_Id := Last (Return_Object_Declarations (N)); Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl); HSS : constant Node_Id := Handled_Statement_Sequence (N); begin Expr := Expression (Obj_Decl); -- Note: The check for OK_For_Limited_Init will happen in -- Analyze_Object_Declaration; we treat it as a normal -- object declaration. Set_Is_Return_Object (Defining_Identifier (Obj_Decl)); Analyze (Obj_Decl); Check_Return_Subtype_Indication (Obj_Decl); if Present (HSS) then Analyze (HSS); if Present (Exception_Handlers (HSS)) then -- ???Has_Nested_Block_With_Handler needs to be set. -- Probably by creating an actual N_Block_Statement. -- Probably in Expand. null; end if; end if; -- Mark the return object as referenced, since the return is an -- implicit reference of the object. Set_Referenced (Defining_Identifier (Obj_Decl)); Check_References (Stm_Entity); -- Check RM 6.5 (5.9/3) if Has_Aliased then if Ada_Version < Ada_2012 then -- Shouldn't this test Warn_On_Ada_2012_Compatibility ??? -- Can it really happen (extended return???) Error_Msg_N ("aliased only allowed for limited" & " return objects in Ada 2012?", N); elsif not Is_Limited_View (R_Type) then Error_Msg_N ("aliased only allowed for limited" & " return objects", N); end if; end if; end; end if; -- Case of Expr present if Present (Expr) -- Defend against previous errors and then Nkind (Expr) /= N_Empty and then Present (Etype (Expr)) then -- Apply constraint check. Note that this is done before the implicit -- conversion of the expression done for anonymous access types to -- ensure correct generation of the null-excluding check associated -- with null-excluding expressions found in return statements. Apply_Constraint_Check (Expr, R_Type); -- Ada 2005 (AI-318-02): When the result type is an anonymous access -- type, apply an implicit conversion of the expression to that type -- to force appropriate static and run-time accessibility checks. if Ada_Version >= Ada_2005 and then Ekind (R_Type) = E_Anonymous_Access_Type then Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr))); Analyze_And_Resolve (Expr, R_Type); -- If this is a local anonymous access to subprogram, the -- accessibility check can be applied statically. The return is -- illegal if the access type of the return expression is declared -- inside of the subprogram (except if it is the subtype indication -- of an extended return statement). elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then if not Comes_From_Source (Current_Scope) or else Ekind (Current_Scope) = E_Return_Statement then null; elsif Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id) then Error_Msg_N ("cannot return local access to subprogram", N); end if; end if; -- If the result type is class-wide, then check that the return -- expression's type is not declared at a deeper level than the -- function (RM05-6.5(5.6/2)). if Ada_Version >= Ada_2005 and then Is_Class_Wide_Type (R_Type) then if Type_Access_Level (Etype (Expr)) > Subprogram_Access_Level (Scope_Id) then Error_Msg_N ("level of return expression type is deeper than " & "class-wide function!", Expr); end if; end if; -- Check incorrect use of dynamically tagged expression if Is_Tagged_Type (R_Type) then Check_Dynamically_Tagged_Expression (Expr => Expr, Typ => R_Type, Related_Nod => N); end if; -- ??? A real run-time accessibility check is needed in cases -- involving dereferences of access parameters. For now we just -- check the static cases. if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L) and then Is_Limited_View (Etype (Scope_Id)) and then Object_Access_Level (Expr) > Subprogram_Access_Level (Scope_Id) then -- Suppress the message in a generic, where the rewriting -- is irrelevant. if Inside_A_Generic then null; else Rewrite (N, Make_Raise_Program_Error (Loc, Reason => PE_Accessibility_Check_Failed)); Analyze (N); Error_Msg_Warn := SPARK_Mode /= On; Error_Msg_N ("cannot return a local value by reference<<", N); Error_Msg_NE ("\& [<<", N, Standard_Program_Error); end if; end if; if Known_Null (Expr) and then Nkind (Parent (Scope_Id)) = N_Function_Specification and then Null_Exclusion_Present (Parent (Scope_Id)) then Apply_Compile_Time_Constraint_Error (N => Expr, Msg => "(Ada 2005) null not allowed for " & "null-excluding return??", Reason => CE_Null_Not_Allowed); end if; end if; end Analyze_Function_Return; ------------------------------------- -- Analyze_Generic_Subprogram_Body -- ------------------------------------- procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id) is Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id); Kind : constant Entity_Kind := Ekind (Gen_Id); Body_Id : Entity_Id; New_N : Node_Id; Spec : Node_Id; begin -- Copy body and disable expansion while analyzing the generic For a -- stub, do not copy the stub (which would load the proper body), this -- will be done when the proper body is analyzed. if Nkind (N) /= N_Subprogram_Body_Stub then New_N := Copy_Generic_Node (N, Empty, Instantiating => False); Rewrite (N, New_N); Start_Generic; end if; Spec := Specification (N); -- Within the body of the generic, the subprogram is callable, and -- behaves like the corresponding non-generic unit. Body_Id := Defining_Entity (Spec); if Kind = E_Generic_Procedure and then Nkind (Spec) /= N_Procedure_Specification then Error_Msg_N ("invalid body for generic procedure ", Body_Id); return; elsif Kind = E_Generic_Function and then Nkind (Spec) /= N_Function_Specification then Error_Msg_N ("invalid body for generic function ", Body_Id); return; end if; Set_Corresponding_Body (Gen_Decl, Body_Id); if Has_Completion (Gen_Id) and then Nkind (Parent (N)) /= N_Subunit then Error_Msg_N ("duplicate generic body", N); return; else Set_Has_Completion (Gen_Id); end if; if Nkind (N) = N_Subprogram_Body_Stub then Set_Ekind (Defining_Entity (Specification (N)), Kind); else Set_Corresponding_Spec (N, Gen_Id); end if; if Nkind (Parent (N)) = N_Compilation_Unit then Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N)); end if; -- Make generic parameters immediately visible in the body. They are -- needed to process the formals declarations. Then make the formals -- visible in a separate step. Push_Scope (Gen_Id); declare E : Entity_Id; First_Ent : Entity_Id; begin First_Ent := First_Entity (Gen_Id); E := First_Ent; while Present (E) and then not Is_Formal (E) loop Install_Entity (E); Next_Entity (E); end loop; Set_Use (Generic_Formal_Declarations (Gen_Decl)); -- Now generic formals are visible, and the specification can be -- analyzed, for subsequent conformance check. Body_Id := Analyze_Subprogram_Specification (Spec); -- Make formal parameters visible if Present (E) then -- E is the first formal parameter, we loop through the formals -- installing them so that they will be visible. Set_First_Entity (Gen_Id, E); while Present (E) loop Install_Entity (E); Next_Formal (E); end loop; end if; -- Visible generic entity is callable within its own body Set_Ekind (Gen_Id, Ekind (Body_Id)); Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id))); Set_Ekind (Body_Id, E_Subprogram_Body); Set_Convention (Body_Id, Convention (Gen_Id)); Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id)); Set_Scope (Body_Id, Scope (Gen_Id)); Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id); if Nkind (N) = N_Subprogram_Body_Stub then -- No body to analyze, so restore state of generic unit Set_Ekind (Gen_Id, Kind); Set_Ekind (Body_Id, Kind); if Present (First_Ent) then Set_First_Entity (Gen_Id, First_Ent); end if; End_Scope; return; end if; -- If this is a compilation unit, it must be made visible explicitly, -- because the compilation of the declaration, unlike other library -- unit declarations, does not. If it is not a unit, the following -- is redundant but harmless. Set_Is_Immediately_Visible (Gen_Id); Reference_Body_Formals (Gen_Id, Body_Id); if Is_Child_Unit (Gen_Id) then Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False); end if; Set_Actual_Subtypes (N, Current_Scope); -- Deal with [refined] preconditions, postconditions, Contract_Cases, -- invariants and predicates associated with the body and its spec. -- Note that this is not pure expansion as Expand_Subprogram_Contract -- prepares the contract assertions for generic subprograms or for -- ASIS. Do not generate contract checks in SPARK mode. if not GNATprove_Mode then Expand_Subprogram_Contract (N, Gen_Id, Body_Id); end if; -- If the generic unit carries pre- or post-conditions, copy them -- to the original generic tree, so that they are properly added -- to any instantiation. declare Orig : constant Node_Id := Original_Node (N); Cond : Node_Id; begin Cond := First (Declarations (N)); while Present (Cond) loop if Nkind (Cond) = N_Pragma and then Pragma_Name (Cond) = Name_Check then Prepend (New_Copy_Tree (Cond), Declarations (Orig)); elsif Nkind (Cond) = N_Pragma and then Pragma_Name (Cond) = Name_Postcondition then Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id)); Prepend (New_Copy_Tree (Cond), Declarations (Orig)); else exit; end if; Next (Cond); end loop; end; Check_SPARK_Mode_In_Generic (N); Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma); Set_SPARK_Pragma_Inherited (Body_Id, True); Analyze_Declarations (Declarations (N)); Check_Completion; Analyze (Handled_Statement_Sequence (N)); Save_Global_References (Original_Node (N)); -- Prior to exiting the scope, include generic formals again (if any -- are present) in the set of local entities. if Present (First_Ent) then Set_First_Entity (Gen_Id, First_Ent); end if; Check_References (Gen_Id); end; Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope); End_Scope; Check_Subprogram_Order (N); -- Outside of its body, unit is generic again Set_Ekind (Gen_Id, Kind); Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False); if Style_Check then Style.Check_Identifier (Body_Id, Gen_Id); end if; End_Generic; end Analyze_Generic_Subprogram_Body; ---------------------------- -- Analyze_Null_Procedure -- ---------------------------- procedure Analyze_Null_Procedure (N : Node_Id; Is_Completion : out Boolean) is Loc : constant Source_Ptr := Sloc (N); Spec : constant Node_Id := Specification (N); Designator : Entity_Id; Form : Node_Id; Null_Body : Node_Id := Empty; Prev : Entity_Id; begin -- Capture the profile of the null procedure before analysis, for -- expansion at the freeze point and at each point of call. The body is -- used if the procedure has preconditions, or if it is a completion. In -- the first case the body is analyzed at the freeze point, in the other -- it replaces the null procedure declaration. Null_Body := Make_Subprogram_Body (Loc, Specification => New_Copy_Tree (Spec), Declarations => New_List, Handled_Statement_Sequence => Make_Handled_Sequence_Of_Statements (Loc, Statements => New_List (Make_Null_Statement (Loc)))); -- Create new entities for body and formals Set_Defining_Unit_Name (Specification (Null_Body), Make_Defining_Identifier (Loc, Chars (Defining_Entity (N)))); Form := First (Parameter_Specifications (Specification (Null_Body))); while Present (Form) loop Set_Defining_Identifier (Form, Make_Defining_Identifier (Loc, Chars (Defining_Identifier (Form)))); Next (Form); end loop; -- Determine whether the null procedure may be a completion of a generic -- suprogram, in which case we use the new null body as the completion -- and set minimal semantic information on the original declaration, -- which is rewritten as a null statement. Prev := Current_Entity_In_Scope (Defining_Entity (Spec)); if Present (Prev) and then Is_Generic_Subprogram (Prev) then Insert_Before (N, Null_Body); Set_Ekind (Defining_Entity (N), Ekind (Prev)); Set_Contract (Defining_Entity (N), Make_Contract (Loc)); Rewrite (N, Make_Null_Statement (Loc)); Analyze_Generic_Subprogram_Body (Null_Body, Prev); Is_Completion := True; return; else -- Resolve the types of the formals now, because the freeze point -- may appear in a different context, e.g. an instantiation. Form := First (Parameter_Specifications (Specification (Null_Body))); while Present (Form) loop if Nkind (Parameter_Type (Form)) /= N_Access_Definition then Find_Type (Parameter_Type (Form)); elsif No (Access_To_Subprogram_Definition (Parameter_Type (Form))) then Find_Type (Subtype_Mark (Parameter_Type (Form))); else -- The case of a null procedure with a formal that is an -- access_to_subprogram type, and that is used as an actual -- in an instantiation is left to the enthusiastic reader. null; end if; Next (Form); end loop; end if; -- If there are previous overloadable entities with the same name, -- check whether any of them is completed by the null procedure. if Present (Prev) and then Is_Overloadable (Prev) then Designator := Analyze_Subprogram_Specification (Spec); Prev := Find_Corresponding_Spec (N); end if; if No (Prev) or else not Comes_From_Source (Prev) then Designator := Analyze_Subprogram_Specification (Spec); Set_Has_Completion (Designator); -- Signal to caller that this is a procedure declaration Is_Completion := False; -- Null procedures are always inlined, but generic formal subprograms -- which appear as such in the internal instance of formal packages, -- need no completion and are not marked Inline. if Expander_Active and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration then Set_Corresponding_Body (N, Defining_Entity (Null_Body)); Set_Body_To_Inline (N, Null_Body); Set_Is_Inlined (Designator); end if; else -- The null procedure is a completion Is_Completion := True; if Expander_Active then Rewrite (N, Null_Body); Analyze (N); else Designator := Analyze_Subprogram_Specification (Spec); Set_Has_Completion (Designator); Set_Has_Completion (Prev); end if; end if; end Analyze_Null_Procedure; ----------------------------- -- Analyze_Operator_Symbol -- ----------------------------- -- An operator symbol such as "+" or "and" may appear in context where the -- literal denotes an entity name, such as "+"(x, y) or in context when it -- is just a string, as in (conjunction = "or"). In these cases the parser -- generates this node, and the semantics does the disambiguation. Other -- such case are actuals in an instantiation, the generic unit in an -- instantiation, and pragma arguments. procedure Analyze_Operator_Symbol (N : Node_Id) is Par : constant Node_Id := Parent (N); begin if (Nkind (Par) = N_Function_Call and then N = Name (Par)) or else Nkind (Par) = N_Function_Instantiation or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par)) or else (Nkind (Par) = N_Pragma_Argument_Association and then not Is_Pragma_String_Literal (Par)) or else Nkind (Par) = N_Subprogram_Renaming_Declaration or else (Nkind (Par) = N_Attribute_Reference and then Attribute_Name (Par) /= Name_Value) then Find_Direct_Name (N); else Change_Operator_Symbol_To_String_Literal (N); Analyze (N); end if; end Analyze_Operator_Symbol; ----------------------------------- -- Analyze_Parameter_Association -- ----------------------------------- procedure Analyze_Parameter_Association (N : Node_Id) is begin Analyze (Explicit_Actual_Parameter (N)); end Analyze_Parameter_Association; ---------------------------- -- Analyze_Procedure_Call -- ---------------------------- procedure Analyze_Procedure_Call (N : Node_Id) is Loc : constant Source_Ptr := Sloc (N); P : constant Node_Id := Name (N); Actuals : constant List_Id := Parameter_Associations (N); Actual : Node_Id; New_N : Node_Id; procedure Analyze_Call_And_Resolve; -- Do Analyze and Resolve calls for procedure call -- At end, check illegal order dependence. ------------------------------ -- Analyze_Call_And_Resolve -- ------------------------------ procedure Analyze_Call_And_Resolve is begin if Nkind (N) = N_Procedure_Call_Statement then Analyze_Call (N); Resolve (N, Standard_Void_Type); else Analyze (N); end if; end Analyze_Call_And_Resolve; -- Start of processing for Analyze_Procedure_Call begin -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote -- a procedure call or an entry call. The prefix may denote an access -- to subprogram type, in which case an implicit dereference applies. -- If the prefix is an indexed component (without implicit dereference) -- then the construct denotes a call to a member of an entire family. -- If the prefix is a simple name, it may still denote a call to a -- parameterless member of an entry family. Resolution of these various -- interpretations is delicate. Analyze (P); -- If this is a call of the form Obj.Op, the call may have been -- analyzed and possibly rewritten into a block, in which case -- we are done. if Analyzed (N) then return; end if; -- If there is an error analyzing the name (which may have been -- rewritten if the original call was in prefix notation) then error -- has been emitted already, mark node and return. if Error_Posted (N) or else Etype (Name (N)) = Any_Type then Set_Etype (N, Any_Type); return; end if; -- Otherwise analyze the parameters if Present (Actuals) then Actual := First (Actuals); while Present (Actual) loop Analyze (Actual); Check_Parameterless_Call (Actual); Next (Actual); end loop; end if; -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls if Nkind (P) = N_Attribute_Reference and then Nam_In (Attribute_Name (P), Name_Elab_Spec, Name_Elab_Body, Name_Elab_Subp_Body) then if Present (Actuals) then Error_Msg_N ("no parameters allowed for this call", First (Actuals)); return; end if; Set_Etype (N, Standard_Void_Type); Set_Analyzed (N); elsif Is_Entity_Name (P) and then Is_Record_Type (Etype (Entity (P))) and then Remote_AST_I_Dereference (P) then return; elsif Is_Entity_Name (P) and then Ekind (Entity (P)) /= E_Entry_Family then if Is_Access_Type (Etype (P)) and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type and then No (Actuals) and then Comes_From_Source (N) then Error_Msg_N ("missing explicit dereference in call", N); end if; Analyze_Call_And_Resolve; -- If the prefix is the simple name of an entry family, this is -- a parameterless call from within the task body itself. elsif Is_Entity_Name (P) and then Nkind (P) = N_Identifier and then Ekind (Entity (P)) = E_Entry_Family and then Present (Actuals) and then No (Next (First (Actuals))) then -- Can be call to parameterless entry family. What appears to be the -- sole argument is in fact the entry index. Rewrite prefix of node -- accordingly. Source representation is unchanged by this -- transformation. New_N := Make_Indexed_Component (Loc, Prefix => Make_Selected_Component (Loc, Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc), Selector_Name => New_Occurrence_Of (Entity (P), Loc)), Expressions => Actuals); Set_Name (N, New_N); Set_Etype (New_N, Standard_Void_Type); Set_Parameter_Associations (N, No_List); Analyze_Call_And_Resolve; elsif Nkind (P) = N_Explicit_Dereference then if Ekind (Etype (P)) = E_Subprogram_Type then Analyze_Call_And_Resolve; else Error_Msg_N ("expect access to procedure in call", P); end if; -- The name can be a selected component or an indexed component that -- yields an access to subprogram. Such a prefix is legal if the call -- has parameter associations. elsif Is_Access_Type (Etype (P)) and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type then if Present (Actuals) then Analyze_Call_And_Resolve; else Error_Msg_N ("missing explicit dereference in call ", N); end if; -- If not an access to subprogram, then the prefix must resolve to the -- name of an entry, entry family, or protected operation. -- For the case of a simple entry call, P is a selected component where -- the prefix is the task and the selector name is the entry. A call to -- a protected procedure will have the same syntax. If the protected -- object contains overloaded operations, the entity may appear as a -- function, the context will select the operation whose type is Void. elsif Nkind (P) = N_Selected_Component and then Ekind_In (Entity (Selector_Name (P)), E_Entry, E_Procedure, E_Function) then Analyze_Call_And_Resolve; elsif Nkind (P) = N_Selected_Component and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family and then Present (Actuals) and then No (Next (First (Actuals))) then -- Can be call to parameterless entry family. What appears to be the -- sole argument is in fact the entry index. Rewrite prefix of node -- accordingly. Source representation is unchanged by this -- transformation. New_N := Make_Indexed_Component (Loc, Prefix => New_Copy (P), Expressions => Actuals); Set_Name (N, New_N); Set_Etype (New_N, Standard_Void_Type); Set_Parameter_Associations (N, No_List); Analyze_Call_And_Resolve; -- For the case of a reference to an element of an entry family, P is -- an indexed component whose prefix is a selected component (task and -- entry family), and whose index is the entry family index. elsif Nkind (P) = N_Indexed_Component and then Nkind (Prefix (P)) = N_Selected_Component and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family then Analyze_Call_And_Resolve; -- If the prefix is the name of an entry family, it is a call from -- within the task body itself. elsif Nkind (P) = N_Indexed_Component and then Nkind (Prefix (P)) = N_Identifier and then Ekind (Entity (Prefix (P))) = E_Entry_Family then New_N := Make_Selected_Component (Loc, Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc), Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc)); Rewrite (Prefix (P), New_N); Analyze (P); Analyze_Call_And_Resolve; -- In Ada 2012. a qualified expression is a name, but it cannot be a -- procedure name, so the construct can only be a qualified expression. elsif Nkind (P) = N_Qualified_Expression and then Ada_Version >= Ada_2012 then Rewrite (N, Make_Code_Statement (Loc, Expression => P)); Analyze (N); -- Anything else is an error else Error_Msg_N ("invalid procedure or entry call", N); end if; end Analyze_Procedure_Call; ------------------------------ -- Analyze_Return_Statement -- ------------------------------ procedure Analyze_Return_Statement (N : Node_Id) is pragma Assert (Nkind_In (N, N_Simple_Return_Statement, N_Extended_Return_Statement)); Returns_Object : constant Boolean := Nkind (N) = N_Extended_Return_Statement or else (Nkind (N) = N_Simple_Return_Statement and then Present (Expression (N))); -- True if we're returning something; that is, "return ;" -- or "return Result : T [:= ...]". False for "return;". Used for error -- checking: If Returns_Object is True, N should apply to a function -- body; otherwise N should apply to a procedure body, entry body, -- accept statement, or extended return statement. function Find_What_It_Applies_To return Entity_Id; -- Find the entity representing the innermost enclosing body, accept -- statement, or extended return statement. If the result is a callable -- construct or extended return statement, then this will be the value -- of the Return_Applies_To attribute. Otherwise, the program is -- illegal. See RM-6.5(4/2). ----------------------------- -- Find_What_It_Applies_To -- ----------------------------- function Find_What_It_Applies_To return Entity_Id is Result : Entity_Id := Empty; begin -- Loop outward through the Scope_Stack, skipping blocks, loops, -- and postconditions. for J in reverse 0 .. Scope_Stack.Last loop Result := Scope_Stack.Table (J).Entity; exit when not Ekind_In (Result, E_Block, E_Loop) and then Chars (Result) /= Name_uPostconditions; end loop; pragma Assert (Present (Result)); return Result; end Find_What_It_Applies_To; -- Local declarations Scope_Id : constant Entity_Id := Find_What_It_Applies_To; Kind : constant Entity_Kind := Ekind (Scope_Id); Loc : constant Source_Ptr := Sloc (N); Stm_Entity : constant Entity_Id := New_Internal_Entity (E_Return_Statement, Current_Scope, Loc, 'R'); -- Start of processing for Analyze_Return_Statement begin Set_Return_Statement_Entity (N, Stm_Entity); Set_Etype (Stm_Entity, Standard_Void_Type); Set_Return_Applies_To (Stm_Entity, Scope_Id); -- Place Return entity on scope stack, to simplify enforcement of 6.5 -- (4/2): an inner return statement will apply to this extended return. if Nkind (N) = N_Extended_Return_Statement then Push_Scope (Stm_Entity); end if; -- Check that pragma No_Return is obeyed. Don't complain about the -- implicitly-generated return that is placed at the end. if No_Return (Scope_Id) and then Comes_From_Source (N) then Error_Msg_N ("RETURN statement not allowed (No_Return)", N); end if; -- Warn on any unassigned OUT parameters if in procedure if Ekind (Scope_Id) = E_Procedure then Warn_On_Unassigned_Out_Parameter (N, Scope_Id); end if; -- Check that functions return objects, and other things do not if Kind = E_Function or else Kind = E_Generic_Function then if not Returns_Object then Error_Msg_N ("missing expression in return from function", N); end if; elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then if Returns_Object then Error_Msg_N ("procedure cannot return value (use function)", N); end if; elsif Kind = E_Entry or else Kind = E_Entry_Family then if Returns_Object then if Is_Protected_Type (Scope (Scope_Id)) then Error_Msg_N ("entry body cannot return value", N); else Error_Msg_N ("accept statement cannot return value", N); end if; end if; elsif Kind = E_Return_Statement then -- We are nested within another return statement, which must be an -- extended_return_statement. if Returns_Object then if Nkind (N) = N_Extended_Return_Statement then Error_Msg_N ("extended return statement cannot be nested (use `RETURN;`)", N); -- Case of a simple return statement with a value inside extended -- return statement. else Error_Msg_N ("return nested in extended return statement cannot return " & "value (use `RETURN;`)", N); end if; end if; else Error_Msg_N ("illegal context for return statement", N); end if; if Ekind_In (Kind, E_Function, E_Generic_Function) then Analyze_Function_Return (N); elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then Set_Return_Present (Scope_Id); end if; if Nkind (N) = N_Extended_Return_Statement then End_Scope; end if; Kill_Current_Values (Last_Assignment_Only => True); Check_Unreachable_Code (N); Analyze_Dimension (N); end Analyze_Return_Statement; ------------------------------------- -- Analyze_Simple_Return_Statement -- ------------------------------------- procedure Analyze_Simple_Return_Statement (N : Node_Id) is begin if Present (Expression (N)) then Mark_Coextensions (N, Expression (N)); end if; Analyze_Return_Statement (N); end Analyze_Simple_Return_Statement; ------------------------- -- Analyze_Return_Type -- ------------------------- procedure Analyze_Return_Type (N : Node_Id) is Designator : constant Entity_Id := Defining_Entity (N); Typ : Entity_Id := Empty; begin -- Normal case where result definition does not indicate an error if Result_Definition (N) /= Error then if Nkind (Result_Definition (N)) = N_Access_Definition then Check_SPARK_Restriction ("access result is not allowed", Result_Definition (N)); -- Ada 2005 (AI-254): Handle anonymous access to subprograms declare AD : constant Node_Id := Access_To_Subprogram_Definition (Result_Definition (N)); begin if Present (AD) and then Protected_Present (AD) then Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N); else Typ := Access_Definition (N, Result_Definition (N)); end if; end; Set_Parent (Typ, Result_Definition (N)); Set_Is_Local_Anonymous_Access (Typ); Set_Etype (Designator, Typ); -- Ada 2005 (AI-231): Ensure proper usage of null exclusion Null_Exclusion_Static_Checks (N); -- Subtype_Mark case else Find_Type (Result_Definition (N)); Typ := Entity (Result_Definition (N)); Set_Etype (Designator, Typ); -- Unconstrained array as result is not allowed in SPARK if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then Check_SPARK_Restriction ("returning an unconstrained array is not allowed", Result_Definition (N)); end if; -- Ada 2005 (AI-231): Ensure proper usage of null exclusion Null_Exclusion_Static_Checks (N); -- If a null exclusion is imposed on the result type, then create -- a null-excluding itype (an access subtype) and use it as the -- function's Etype. Note that the null exclusion checks are done -- right before this, because they don't get applied to types that -- do not come from source. if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then Set_Etype (Designator, Create_Null_Excluding_Itype (T => Typ, Related_Nod => N, Scope_Id => Scope (Current_Scope))); -- The new subtype must be elaborated before use because -- it is visible outside of the function. However its base -- type may not be frozen yet, so the reference that will -- force elaboration must be attached to the freezing of -- the base type. -- If the return specification appears on a proper body, -- the subtype will have been created already on the spec. if Is_Frozen (Typ) then if Nkind (Parent (N)) = N_Subprogram_Body and then Nkind (Parent (Parent (N))) = N_Subunit then null; else Build_Itype_Reference (Etype (Designator), Parent (N)); end if; else Ensure_Freeze_Node (Typ); declare IR : constant Node_Id := Make_Itype_Reference (Sloc (N)); begin Set_Itype (IR, Etype (Designator)); Append_Freeze_Actions (Typ, New_List (IR)); end; end if; else Set_Etype (Designator, Typ); end if; if Ekind (Typ) = E_Incomplete_Type and then Is_Value_Type (Typ) then null; elsif Ekind (Typ) = E_Incomplete_Type or else (Is_Class_Wide_Type (Typ) and then Ekind (Root_Type (Typ)) = E_Incomplete_Type) then -- AI05-0151: Tagged incomplete types are allowed in all formal -- parts. Untagged incomplete types are not allowed in bodies. if Ada_Version >= Ada_2012 then if Is_Tagged_Type (Typ) then null; elsif Nkind (Parent (N)) = N_Subprogram_Body or else Nkind_In (Parent (Parent (N)), N_Accept_Statement, N_Entry_Body) then Error_Msg_NE ("invalid use of untagged incomplete type&", Designator, Typ); end if; -- The type must be completed in the current package. This -- is checked at the end of the package declaration when -- Taft-amendment types are identified. If the return type -- is class-wide, there is no required check, the type can -- be a bona fide TAT. if Ekind (Scope (Current_Scope)) = E_Package and then In_Private_Part (Scope (Current_Scope)) and then not Is_Class_Wide_Type (Typ) then Append_Elmt (Designator, Private_Dependents (Typ)); end if; else Error_Msg_NE ("invalid use of incomplete type&", Designator, Typ); end if; end if; end if; -- Case where result definition does indicate an error else Set_Etype (Designator, Any_Type); end if; end Analyze_Return_Type; ----------------------------- -- Analyze_Subprogram_Body -- ----------------------------- procedure Analyze_Subprogram_Body (N : Node_Id) is Loc : constant Source_Ptr := Sloc (N); Body_Spec : constant Node_Id := Specification (N); Body_Id : constant Entity_Id := Defining_Entity (Body_Spec); begin if Debug_Flag_C then Write_Str ("==> subprogram body "); Write_Name (Chars (Body_Id)); Write_Str (" from "); Write_Location (Loc); Write_Eol; Indent; end if; Trace_Scope (N, Body_Id, " Analyze subprogram: "); -- The real work is split out into the helper, so it can do "return;" -- without skipping the debug output: Analyze_Subprogram_Body_Helper (N); if Debug_Flag_C then Outdent; Write_Str ("<== subprogram body "); Write_Name (Chars (Body_Id)); Write_Str (" from "); Write_Location (Loc); Write_Eol; end if; end Analyze_Subprogram_Body; -------------------------------------- -- Analyze_Subprogram_Body_Contract -- -------------------------------------- procedure Analyze_Subprogram_Body_Contract (Body_Id : Entity_Id) is Body_Decl : constant Node_Id := Parent (Parent (Body_Id)); Spec_Id : constant Entity_Id := Corresponding_Spec (Body_Decl); Prag : Node_Id; Ref_Depends : Node_Id := Empty; Ref_Global : Node_Id := Empty; begin -- When a subprogram body declaration is illegal, its defining entity is -- left unanalyzed. There is nothing left to do in this case because the -- body lacks a contract. if not Analyzed (Body_Id) then return; end if; -- Locate and store pragmas Refined_Depends and Refined_Global since -- their order of analysis matters. Prag := Classifications (Contract (Body_Id)); while Present (Prag) loop if Pragma_Name (Prag) = Name_Refined_Depends then Ref_Depends := Prag; elsif Pragma_Name (Prag) = Name_Refined_Global then Ref_Global := Prag; end if; Prag := Next_Pragma (Prag); end loop; -- Analyze Refined_Global first as Refined_Depends may mention items -- classified in the global refinement. if Present (Ref_Global) then Analyze_Refined_Global_In_Decl_Part (Ref_Global); -- When the corresponding Global aspect/pragma references a state with -- visible refinement, the body requires Refined_Global. Refinement is -- not required when SPARK checks are suppressed. elsif Present (Spec_Id) then Prag := Get_Pragma (Spec_Id, Pragma_Global); if SPARK_Mode /= Off and then Present (Prag) and then Contains_Refined_State (Prag) then Error_Msg_NE ("body of subprogram & requires global refinement", Body_Decl, Spec_Id); end if; end if; -- Refined_Depends must be analyzed after Refined_Global in order to see -- the modes of all global refinements. if Present (Ref_Depends) then Analyze_Refined_Depends_In_Decl_Part (Ref_Depends); -- When the corresponding Depends aspect/pragma references a state with -- visible refinement, the body requires Refined_Depends. Refinement is -- not required when SPARK checks are suppressed. elsif Present (Spec_Id) then Prag := Get_Pragma (Spec_Id, Pragma_Depends); if SPARK_Mode /= Off and then Present (Prag) and then Contains_Refined_State (Prag) then Error_Msg_NE ("body of subprogram & requires dependance refinement", Body_Decl, Spec_Id); end if; end if; end Analyze_Subprogram_Body_Contract; ------------------------------------ -- Analyze_Subprogram_Body_Helper -- ------------------------------------ -- This procedure is called for regular subprogram bodies, generic bodies, -- and for subprogram stubs of both kinds. In the case of stubs, only the -- specification matters, and is used to create a proper declaration for -- the subprogram, or to perform conformance checks. procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is Loc : constant Source_Ptr := Sloc (N); Body_Spec : constant Node_Id := Specification (N); Body_Id : Entity_Id := Defining_Entity (Body_Spec); Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id); Conformant : Boolean; HSS : Node_Id; Prot_Typ : Entity_Id := Empty; Spec_Id : Entity_Id; Spec_Decl : Node_Id := Empty; Last_Real_Spec_Entity : Entity_Id := Empty; -- When we analyze a separate spec, the entity chain ends up containing -- the formals, as well as any itypes generated during analysis of the -- default expressions for parameters, or the arguments of associated -- precondition/postcondition pragmas (which are analyzed in the context -- of the spec since they have visibility on formals). -- -- These entities belong with the spec and not the body. However we do -- the analysis of the body in the context of the spec (again to obtain -- visibility to the formals), and all the entities generated during -- this analysis end up also chained to the entity chain of the spec. -- But they really belong to the body, and there is circuitry to move -- them from the spec to the body. -- -- However, when we do this move, we don't want to move the real spec -- entities (first para above) to the body. The Last_Real_Spec_Entity -- variable points to the last real spec entity, so we only move those -- chained beyond that point. It is initialized to Empty to deal with -- the case where there is no separate spec. procedure Check_Anonymous_Return; -- Ada 2005: if a function returns an access type that denotes a task, -- or a type that contains tasks, we must create a master entity for -- the anonymous type, which typically will be used in an allocator -- in the body of the function. procedure Check_Inline_Pragma (Spec : in out Node_Id); -- Look ahead to recognize a pragma that may appear after the body. -- If there is a previous spec, check that it appears in the same -- declarative part. If the pragma is Inline_Always, perform inlining -- unconditionally, otherwise only if Front_End_Inlining is requested. -- If the body acts as a spec, and inlining is required, we create a -- subprogram declaration for it, in order to attach the body to inline. -- If pragma does not appear after the body, check whether there is -- an inline pragma before any local declarations. procedure Check_Missing_Return; -- Checks for a function with a no return statements, and also performs -- the warning checks implemented by Check_Returns. In formal mode, also -- verify that a function ends with a RETURN and that a procedure does -- not contain any RETURN. procedure Diagnose_Misplaced_Aspect_Specifications; -- It is known that subprogram body N has aspects, but they are not -- properly placed. Provide specific error messages depending on the -- aspects involved. function Disambiguate_Spec return Entity_Id; -- When a primitive is declared between the private view and the full -- view of a concurrent type which implements an interface, a special -- mechanism is used to find the corresponding spec of the primitive -- body. procedure Exchange_Limited_Views (Subp_Id : Entity_Id); -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains -- incomplete types coming from a limited context and swap their limited -- views with the non-limited ones. function Is_Private_Concurrent_Primitive (Subp_Id : Entity_Id) return Boolean; -- Determine whether subprogram Subp_Id is a primitive of a concurrent -- type that implements an interface and has a private view. procedure Set_Trivial_Subprogram (N : Node_Id); -- Sets the Is_Trivial_Subprogram flag in both spec and body of the -- subprogram whose body is being analyzed. N is the statement node -- causing the flag to be set, if the following statement is a return -- of an entity, we mark the entity as set in source to suppress any -- warning on the stylized use of function stubs with a dummy return. procedure Verify_Overriding_Indicator; -- If there was a previous spec, the entity has been entered in the -- current scope previously. If the body itself carries an overriding -- indicator, check that it is consistent with the known status of the -- entity. ---------------------------- -- Check_Anonymous_Return -- ---------------------------- procedure Check_Anonymous_Return is Decl : Node_Id; Par : Node_Id; Scop : Entity_Id; begin if Present (Spec_Id) then Scop := Spec_Id; else Scop := Body_Id; end if; if Ekind (Scop) = E_Function and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type and then not Is_Thunk (Scop) and then (Has_Task (Designated_Type (Etype (Scop))) or else (Is_Class_Wide_Type (Designated_Type (Etype (Scop))) and then Is_Limited_Record (Designated_Type (Etype (Scop))))) and then Expander_Active -- Avoid cases with no tasking support and then RTE_Available (RE_Current_Master) and then not Restriction_Active (No_Task_Hierarchy) then Decl := Make_Object_Declaration (Loc, Defining_Identifier => Make_Defining_Identifier (Loc, Name_uMaster), Constant_Present => True, Object_Definition => New_Occurrence_Of (RTE (RE_Master_Id), Loc), Expression => Make_Explicit_Dereference (Loc, New_Occurrence_Of (RTE (RE_Current_Master), Loc))); if Present (Declarations (N)) then Prepend (Decl, Declarations (N)); else Set_Declarations (N, New_List (Decl)); end if; Set_Master_Id (Etype (Scop), Defining_Identifier (Decl)); Set_Has_Master_Entity (Scop); -- Now mark the containing scope as a task master Par := N; while Nkind (Par) /= N_Compilation_Unit loop Par := Parent (Par); pragma Assert (Present (Par)); -- If we fall off the top, we are at the outer level, and -- the environment task is our effective master, so nothing -- to mark. if Nkind_In (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body) then Set_Is_Task_Master (Par, True); exit; end if; end loop; end if; end Check_Anonymous_Return; ------------------------- -- Check_Inline_Pragma -- ------------------------- procedure Check_Inline_Pragma (Spec : in out Node_Id) is Prag : Node_Id; Plist : List_Id; function Is_Inline_Pragma (N : Node_Id) return Boolean; -- True when N is a pragma Inline or Inline_Always that applies -- to this subprogram. ----------------------- -- Is_Inline_Pragma -- ----------------------- function Is_Inline_Pragma (N : Node_Id) return Boolean is begin return Nkind (N) = N_Pragma and then (Pragma_Name (N) = Name_Inline_Always or else (Front_End_Inlining and then Pragma_Name (N) = Name_Inline)) and then Chars (Expression (First (Pragma_Argument_Associations (N)))) = Chars (Body_Id); end Is_Inline_Pragma; -- Start of processing for Check_Inline_Pragma begin if not Expander_Active then return; end if; if Is_List_Member (N) and then Present (Next (N)) and then Is_Inline_Pragma (Next (N)) then Prag := Next (N); elsif Nkind (N) /= N_Subprogram_Body_Stub and then Present (Declarations (N)) and then Is_Inline_Pragma (First (Declarations (N))) then Prag := First (Declarations (N)); else Prag := Empty; end if; if Present (Prag) then if Present (Spec_Id) then if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then Analyze (Prag); end if; else -- Create a subprogram declaration, to make treatment uniform declare Subp : constant Entity_Id := Make_Defining_Identifier (Loc, Chars (Body_Id)); Decl : constant Node_Id := Make_Subprogram_Declaration (Loc, Specification => New_Copy_Tree (Specification (N))); begin Set_Defining_Unit_Name (Specification (Decl), Subp); if Present (First_Formal (Body_Id)) then Plist := Copy_Parameter_List (Body_Id); Set_Parameter_Specifications (Specification (Decl), Plist); end if; Insert_Before (N, Decl); Analyze (Decl); Analyze (Prag); Set_Has_Pragma_Inline (Subp); if Pragma_Name (Prag) = Name_Inline_Always then Set_Is_Inlined (Subp); Set_Has_Pragma_Inline_Always (Subp); end if; -- Prior to copying the subprogram body to create a template -- for it for subsequent inlining, remove the pragma from -- the current body so that the copy that will produce the -- new body will start from a completely unanalyzed tree. if Nkind (Parent (Prag)) = N_Subprogram_Body then Rewrite (Prag, Make_Null_Statement (Sloc (Prag))); end if; Spec := Subp; end; end if; end if; end Check_Inline_Pragma; -------------------------- -- Check_Missing_Return -- -------------------------- procedure Check_Missing_Return is Id : Entity_Id; Missing_Ret : Boolean; begin if Nkind (Body_Spec) = N_Function_Specification then if Present (Spec_Id) then Id := Spec_Id; else Id := Body_Id; end if; if Return_Present (Id) then Check_Returns (HSS, 'F', Missing_Ret); if Missing_Ret then Set_Has_Missing_Return (Id); end if; elsif Is_Generic_Subprogram (Id) or else not Is_Machine_Code_Subprogram (Id) then Error_Msg_N ("missing RETURN statement in function body", N); end if; -- If procedure with No_Return, check returns elsif Nkind (Body_Spec) = N_Procedure_Specification and then Present (Spec_Id) and then No_Return (Spec_Id) then Check_Returns (HSS, 'P', Missing_Ret, Spec_Id); end if; -- Special checks in SPARK mode if Nkind (Body_Spec) = N_Function_Specification then -- In SPARK mode, last statement of a function should be a return declare Stat : constant Node_Id := Last_Source_Statement (HSS); begin if Present (Stat) and then not Nkind_In (Stat, N_Simple_Return_Statement, N_Extended_Return_Statement) then Check_SPARK_Restriction ("last statement in function should be RETURN", Stat); end if; end; -- In SPARK mode, verify that a procedure has no return elsif Nkind (Body_Spec) = N_Procedure_Specification then if Present (Spec_Id) then Id := Spec_Id; else Id := Body_Id; end if; -- Would be nice to point to return statement here, can we -- borrow the Check_Returns procedure here ??? if Return_Present (Id) then Check_SPARK_Restriction ("procedure should not have RETURN", N); end if; end if; end Check_Missing_Return; ---------------------------------------------- -- Diagnose_Misplaced_Aspect_Specifications -- ---------------------------------------------- procedure Diagnose_Misplaced_Aspect_Specifications is Asp : Node_Id; Asp_Nam : Name_Id; Asp_Id : Aspect_Id; -- The current aspect along with its name and id procedure SPARK_Aspect_Error (Ref_Nam : Name_Id); -- Emit an error message concerning SPARK aspect Asp. Ref_Nam is the -- name of the refined version of the aspect. ------------------------ -- SPARK_Aspect_Error -- ------------------------ procedure SPARK_Aspect_Error (Ref_Nam : Name_Id) is begin -- The corresponding spec already contains the aspect in question -- and the one appearing on the body must be the refined form: -- procedure P with Global ...; -- procedure P with Global ... is ... end P; -- ^ -- Refined_Global if Has_Aspect (Spec_Id, Asp_Id) then Error_Msg_Name_1 := Asp_Nam; -- Subunits cannot carry aspects that apply to a subprogram -- declaration. if Nkind (Parent (N)) = N_Subunit then Error_Msg_N ("aspect % cannot apply to a subunit", Asp); else Error_Msg_Name_2 := Ref_Nam; Error_Msg_N ("aspect % should be %", Asp); end if; -- Otherwise the aspect must appear in the spec, not in the body: -- procedure P; -- procedure P with Global ... is ... end P; else Error_Msg_N ("aspect specification must appear in subprogram declaration", Asp); end if; end SPARK_Aspect_Error; -- Start of processing for Diagnose_Misplaced_Aspect_Specifications begin -- Iterate over the aspect specifications and emit specific errors -- where applicable. Asp := First (Aspect_Specifications (N)); while Present (Asp) loop Asp_Nam := Chars (Identifier (Asp)); Asp_Id := Get_Aspect_Id (Asp_Nam); -- Do not emit errors on aspects that can appear on a subprogram -- body. This scenario occurs when the aspect specification list -- contains both misplaced and properly placed aspects. if Aspect_On_Body_Or_Stub_OK (Asp_Id) then null; -- Special diagnostics for SPARK aspects elsif Asp_Nam = Name_Depends then SPARK_Aspect_Error (Name_Refined_Depends); elsif Asp_Nam = Name_Global then SPARK_Aspect_Error (Name_Refined_Global); elsif Asp_Nam = Name_Post then SPARK_Aspect_Error (Name_Refined_Post); else Error_Msg_N ("aspect specification must appear in subprogram declaration", Asp); end if; Next (Asp); end loop; end Diagnose_Misplaced_Aspect_Specifications; ----------------------- -- Disambiguate_Spec -- ----------------------- function Disambiguate_Spec return Entity_Id is Priv_Spec : Entity_Id; Spec_N : Entity_Id; procedure Replace_Types (To_Corresponding : Boolean); -- Depending on the flag, replace the type of formal parameters of -- Body_Id if it is a concurrent type implementing interfaces with -- the corresponding record type or the other way around. procedure Replace_Types (To_Corresponding : Boolean) is Formal : Entity_Id; Formal_Typ : Entity_Id; begin Formal := First_Formal (Body_Id); while Present (Formal) loop Formal_Typ := Etype (Formal); if Is_Class_Wide_Type (Formal_Typ) then Formal_Typ := Root_Type (Formal_Typ); end if; -- From concurrent type to corresponding record if To_Corresponding then if Is_Concurrent_Type (Formal_Typ) and then Present (Corresponding_Record_Type (Formal_Typ)) and then Present (Interfaces ( Corresponding_Record_Type (Formal_Typ))) then Set_Etype (Formal, Corresponding_Record_Type (Formal_Typ)); end if; -- From corresponding record to concurrent type else if Is_Concurrent_Record_Type (Formal_Typ) and then Present (Interfaces (Formal_Typ)) then Set_Etype (Formal, Corresponding_Concurrent_Type (Formal_Typ)); end if; end if; Next_Formal (Formal); end loop; end Replace_Types; -- Start of processing for Disambiguate_Spec begin -- Try to retrieve the specification of the body as is. All error -- messages are suppressed because the body may not have a spec in -- its current state. Spec_N := Find_Corresponding_Spec (N, False); -- It is possible that this is the body of a primitive declared -- between a private and a full view of a concurrent type. The -- controlling parameter of the spec carries the concurrent type, -- not the corresponding record type as transformed by Analyze_ -- Subprogram_Specification. In such cases, we undo the change -- made by the analysis of the specification and try to find the -- spec again. -- Note that wrappers already have their corresponding specs and -- bodies set during their creation, so if the candidate spec is -- a wrapper, then we definitely need to swap all types to their -- original concurrent status. if No (Spec_N) or else Is_Primitive_Wrapper (Spec_N) then -- Restore all references of corresponding record types to the -- original concurrent types. Replace_Types (To_Corresponding => False); Priv_Spec := Find_Corresponding_Spec (N, False); -- The current body truly belongs to a primitive declared between -- a private and a full view. We leave the modified body as is, -- and return the true spec. if Present (Priv_Spec) and then Is_Private_Primitive (Priv_Spec) then return Priv_Spec; end if; -- In case that this is some sort of error, restore the original -- state of the body. Replace_Types (To_Corresponding => True); end if; return Spec_N; end Disambiguate_Spec; ---------------------------- -- Exchange_Limited_Views -- ---------------------------- procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is procedure Detect_And_Exchange (Id : Entity_Id); -- Determine whether Id's type denotes an incomplete type associated -- with a limited with clause and exchange the limited view with the -- non-limited one. ------------------------- -- Detect_And_Exchange -- ------------------------- procedure Detect_And_Exchange (Id : Entity_Id) is Typ : constant Entity_Id := Etype (Id); begin if Ekind (Typ) = E_Incomplete_Type and then From_Limited_With (Typ) and then Present (Non_Limited_View (Typ)) then Set_Etype (Id, Non_Limited_View (Typ)); end if; end Detect_And_Exchange; -- Local variables Formal : Entity_Id; -- Start of processing for Exchange_Limited_Views begin if No (Subp_Id) then return; -- Do not process subprogram bodies as they already use the non- -- limited view of types. elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then return; end if; -- Examine all formals and swap views when applicable Formal := First_Formal (Subp_Id); while Present (Formal) loop Detect_And_Exchange (Formal); Next_Formal (Formal); end loop; -- Process the return type of a function if Ekind (Subp_Id) = E_Function then Detect_And_Exchange (Subp_Id); end if; end Exchange_Limited_Views; ------------------------------------- -- Is_Private_Concurrent_Primitive -- ------------------------------------- function Is_Private_Concurrent_Primitive (Subp_Id : Entity_Id) return Boolean is Formal_Typ : Entity_Id; begin if Present (First_Formal (Subp_Id)) then Formal_Typ := Etype (First_Formal (Subp_Id)); if Is_Concurrent_Record_Type (Formal_Typ) then if Is_Class_Wide_Type (Formal_Typ) then Formal_Typ := Root_Type (Formal_Typ); end if; Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ); end if; -- The type of the first formal is a concurrent tagged type with -- a private view. return Is_Concurrent_Type (Formal_Typ) and then Is_Tagged_Type (Formal_Typ) and then Has_Private_Declaration (Formal_Typ); end if; return False; end Is_Private_Concurrent_Primitive; ---------------------------- -- Set_Trivial_Subprogram -- ---------------------------- procedure Set_Trivial_Subprogram (N : Node_Id) is Nxt : constant Node_Id := Next (N); begin Set_Is_Trivial_Subprogram (Body_Id); if Present (Spec_Id) then Set_Is_Trivial_Subprogram (Spec_Id); end if; if Present (Nxt) and then Nkind (Nxt) = N_Simple_Return_Statement and then No (Next (Nxt)) and then Present (Expression (Nxt)) and then Is_Entity_Name (Expression (Nxt)) then Set_Never_Set_In_Source (Entity (Expression (Nxt)), False); end if; end Set_Trivial_Subprogram; --------------------------------- -- Verify_Overriding_Indicator -- --------------------------------- procedure Verify_Overriding_Indicator is begin if Must_Override (Body_Spec) then if Nkind (Spec_Id) = N_Defining_Operator_Symbol and then Operator_Matches_Spec (Spec_Id, Spec_Id) then null; elsif not Present (Overridden_Operation (Spec_Id)) then Error_Msg_NE ("subprogram& is not overriding", Body_Spec, Spec_Id); -- Overriding indicators aren't allowed for protected subprogram -- bodies (see the Confirmation in Ada Comment AC95-00213). Change -- this to a warning if -gnatd.E is enabled. elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then Error_Msg_Warn := Error_To_Warning; Error_Msg_N ("