home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gnat-2.06-src.tgz / tar.out / fsf / gnat / ada / sem_ch12.adb < prev    next >
Text File  |  1996-09-28  |  154KB  |  4,380 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT COMPILER COMPONENTS                         --
  4. --                                                                          --
  5. --                             S E M _ C H 1 2                              --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.302 $                            --
  10. --                                                                          --
  11. --        Copyright (c) 1992,1993,1994,1995 NYU, All Rights Reserved        --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. --
  22. --                                                                          --
  23. ------------------------------------------------------------------------------
  24.  
  25. with Atree;    use Atree;
  26. with Einfo;    use Einfo;
  27. with Elists;   use Elists;
  28. with Errout;   use Errout;
  29. with Expander; use Expander;
  30. with Exp_Ch7;  use Exp_Ch7;
  31. with Features; use Features;
  32. with Freeze;   use Freeze;
  33. with Inline;   use Inline;
  34. with Lib;      use Lib;
  35. with Lib.Load; use Lib.Load;
  36. with Nlists;   use Nlists;
  37. with Nmake;    use Nmake;
  38. with Opt;      use Opt;
  39. with Output;   use Output;
  40. with Rtsfind;  use Rtsfind;
  41. with Sem;      use Sem;
  42. with Sem_Ch3;  use Sem_Ch3;
  43. with Sem_Ch6;  use Sem_Ch6;
  44. with Sem_Ch7;  use Sem_Ch7;
  45. with Sem_Ch8;  use Sem_Ch8;
  46. with Sem_Ch10; use Sem_Ch10;
  47. with Sem_Ch13; use Sem_Ch13;
  48. with Sem_Dist; use Sem_Dist;
  49. with Sem_Eval; use Sem_Eval;
  50. with Sem_Res;  use Sem_Res;
  51. with Sem_Type; use Sem_Type;
  52. with Sem_Util; use Sem_Util;
  53. with Stand;    use Stand;
  54. with Sinfo;    use Sinfo;
  55. with Sinfo.CN; use Sinfo.CN;
  56. with Snames;   use Snames;
  57. with Stringt;  use Stringt;
  58. with Uname;    use Uname;
  59. with Table;
  60. with Tbuild;   use Tbuild;
  61. with Uintp;    use Uintp;
  62. with Urealp;   use Urealp;
  63.  
  64. with System.Parameters;
  65.  
  66. package body Sem_Ch12 is
  67.  
  68.    use Atree.Unchecked_Access;
  69.    --  This package performs untyped traversals of the tree, therefore it
  70.    --  needs direct access to the fields of a node.
  71.  
  72.    -----------------------------------------------------------
  73.    --  Implementation of generic analysis and instantiation --
  74.    -----------------------------------------------------------
  75.  
  76.    --  GNAT implements generics by macro expansion. No attempt is made to
  77.    --  share generic instantions (for now). Analysis of a generic definition
  78.    --  does not perform any expansion action, but the expander must be called
  79.    --  on the tree for each instantiation, because the expansion may of course
  80.    --  depend  on the generic actuals. All of this is best achieved as follows:
  81.    --
  82.    --  a) Semantic analysis of a generic unit is performed on  a copy of the
  83.    --  tree for the  generic unit. All tree modifications that follow analysis
  84.    --  do not affect the original tree. Links are kept between the original
  85.    --  tree and the copy,  in order to recognize non-local references within
  86.    --  the generic, and propagate them to each instance (recall that name
  87.    --  resolution is done on the generic declaration: generics are not really
  88.    --  macros!). This is summarized in the following diagram:
  89.    --
  90.    --              .-----------.               .----------.
  91.    --              |  semantic |<--------------|  generic |
  92.    --              |    copy   |               |    unit  |
  93.    --              |           |==============>|          |
  94.    --              |___________|    global     |__________|
  95.    --                             references     |   |  |
  96.    --                                            |   |  |
  97.    --                                          .-----|--|.
  98.    --                                          |  .-----|---.
  99.    --                                          |  |  .----------.
  100.    --                                          |  |  |  generic |
  101.    --                                          |__|  |          |
  102.    --                                             |__| instance |
  103.    --                                                |__________|
  104.    --
  105.    --  b) Each instantiation copies the original tree, and inserts into it a
  106.    --  series of declarations that describe the mapping between generic formals
  107.    --  and actuals. For example, a generic In OUT parameter is  an object
  108.    --  renaming of the corresponing actual, etc. Generic IN parameters are
  109.    --  constant declarations.
  110.    --
  111.    --  c) In order to give the right visibility for these renamings, we use
  112.    --  a different scheme for package and subprogram instantiations. For
  113.    --  packages, the list of renamings is inserted into the package
  114.    --  specification, before the visible declarations of the package. The
  115.    --  renamings are analyzed before any of the text of the instance, and are
  116.    --  thus visible at the right place. Furthermore, outside of the instance,
  117.    --  the generic parameters are visible and denote their corresponding
  118.    --  actuals.
  119.  
  120.    --  For subprograms, we create a container package to hold the renamings
  121.    --  and the subprogram instance itself. Analysis of the package makes the
  122.    --  renaming declarations visible to the subprogram. after analyzing the
  123.    --  package, the defining entity for the subprogram is touched-up so that
  124.    --  it appears declared in the current scope, and not inside the container
  125.    --  package.
  126.  
  127.    --  If the instantiation is a compilation unit, the container package is
  128.    --  given the same name as the subprogram instance. This ensures that
  129.    --  the elaboration procedure called by the binder, using the compilation
  130.    --  unit name, calls in fact the elaboration procedure for the package.
  131.  
  132.    --  Not surprisingly, private types complicate this approach. By saving in
  133.    --  the original generic object the non-local references, we guarantee that
  134.    --  the proper entities are referenced at the point of instantiation.
  135.    --  However, for private types, this by itself does not insure that the
  136.    --  proper VIEW of the entity is used (the full type may be visible at the
  137.    --  point of generic definition, but not at instantiation, or viceversa).
  138.    --  In  order to reference the proper view, we special-case any reference
  139.    --  to private types in the generic object, by saving boths views, one in
  140.    --  the generic and one in the semantic copy. At time of instantiation, we
  141.    --  check whether the two views are consistent, and exchange declarations if
  142.    --  necessary, in  order to restore the correct visibility. Similarly, if
  143.    --  the instance view is private when the generic view was not, we perform
  144.    --  the exchange.  After completing the instantiation,  we restore the
  145.    --  current visibility. The flag Has_Private_View marks identifiers in the
  146.    --  the generic unit that require checking.
  147.  
  148.    --  Visibility within nested generic units requires special handling.
  149.    --  Consider the following scheme:
  150.    --
  151.    --  type Global is ...         --  outside of generic unit.
  152.    --  generic ...
  153.    --  package Outer is
  154.    --     ...
  155.    --     type Semi_Global is ... --  global to inner.
  156.    --
  157.    --     generic ...                                         -- 1
  158.    --     procedure inner (X1 : Global;  X2 : Semi_Global);
  159.    --
  160.    --     procedure in2 is new inner (...);                   -- 4
  161.    --  end Outer;
  162.  
  163.    --  package New_Outer is new Outer (...);                  -- 2
  164.    --  procedure New_Inner is new New_Outer.Inner (...);      -- 3
  165.  
  166.    --  The semantic analysis of Outer captures all occurrences of Global.
  167.    --  The semantic analysis of Inner (at 1)  captures both occurrences of
  168.    --  Global and Semi_Global.
  169.  
  170.    --  At point 2 (instantiation of Outer), we also produce a generic copy
  171.    --  of Inner, even though Inner is at that point not being instantiated.
  172.    --  (This is just part of the semantic analysis of New_Outer).
  173.    --  Critically, references to Global within Inner must be preserved, while
  174.    --  references to Semi_Global should not preserved, because they must now
  175.    --  resolve to an entity within New_Outer. To distinguish between these, we
  176.    --  use a global variable, Current_Instantiated_Parent, which is set when
  177.    --  performing a generic copy during instantiation (at 2). This variable is
  178.    --  used when performing a generic copy that is not an instantiation, but
  179.    --  that is nested within one, as the occurrence of 1 within 2. The analysis
  180.    --  of a nested generic only preserves references that are global to the
  181.    --  enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
  182.    --  determine whether a reference is external to the given parent.
  183.  
  184.    --  The instantiation at point 3 requires no special treatment. The method
  185.    --  works as well for further nestings of generic units, but of course the
  186.    --  variable Current_Instantiated_Parent must be stacked because nested
  187.    --  instantiations can occur, e.g. the occurrence of 4 within 2.
  188.  
  189.    Current_Instantiated_Parent : Entity_Id := Empty;
  190.  
  191.    -----------------------
  192.    -- Local subprograms --
  193.    -----------------------
  194.  
  195.    procedure Abandon_Instantiation (N : Node_Id);
  196.    --  Posts an error message "instnatiation abandoned" at the indicated
  197.    --  node and then raises the exception Instantiation_Error to do it.
  198.  
  199.    procedure Analyze_Formal_Array_Type
  200.      (T   : in out Entity_Id;
  201.       Def : Node_Id);
  202.    --  A formal array type is treated like an array type declaration, and
  203.    --  invokes Array_Type_Declaration (sem_ch3) whose first parameter is
  204.    --  in-out, because in the case of an anonymous type the entity is
  205.    --  actually created in the procedure.
  206.  
  207.    --  The following procedures treat other kinds of formal parameters.
  208.  
  209.    procedure Analyze_Formal_Derived_Type
  210.      (N   : Node_Id;
  211.       T   : Entity_Id;
  212.       Def : Node_Id);
  213.  
  214.    procedure Analyze_Formal_Decimal_Fixed_Point (T : Entity_Id; Def : Node_Id);
  215.    procedure Analyze_Formal_Discrete_Type       (T : Entity_Id; Def : Node_Id);
  216.    procedure Analyze_Formal_Floating_Type       (T : Entity_Id; Def : Node_Id);
  217.    procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
  218.    procedure Analyze_Formal_Modular_Type        (T : Entity_Id; Def : Node_Id);
  219.    procedure Analyze_Formal_Ordinary_Fixed_Point_Type
  220.                                                 (T : Entity_Id; Def : Node_Id);
  221.  
  222.    procedure Analyze_Formal_Private_Type
  223.      (N   : Node_Id;
  224.       T   : Entity_Id;
  225.       Def : Node_Id);
  226.  
  227.    procedure Analyze_Generic_Formal_Part        (N : Node_Id);
  228.    procedure Analyze_Generic_Access_Type        (T : Entity_Id; Def : Node_Id);
  229.  
  230.    function Analyze_Associations
  231.      (Formals : List_Id;
  232.       Actuals : List_Id;
  233.       F_Copy  : List_Id)
  234.       return    List_Id;
  235.    --  At instantiation time, build the list of associations between formals
  236.    --  and actuals. Each association becomes a renaming declaration for the
  237.    --  formal entity. F_Copy is the analyzed list of formals in the generic
  238.    --  copy. It is used to apply legality checks to the actuals.
  239.  
  240.    procedure Analyze_Subprogram_Instantiation
  241.      (N : Node_Id;
  242.       K : Entity_Kind);
  243.  
  244.    procedure Build_Instance_Compilation_Unit_Nodes
  245.      (N        : Node_Id;
  246.       Act_Body : Node_Id;
  247.       Act_Decl : Node_Id);
  248.    --  This procedure is used in the case where the generic instance of a
  249.    --  subprogram body or package body is a library unit. In this case, the
  250.    --  original library unit node for the generic instantiation must be
  251.    --  replaced by the resulting generic body, and a link made to a new
  252.    --  compilation unit node for the generic declaration. The argument N is
  253.    --  the original generic instantiation. Act_Body and Act_Decl are the body
  254.    --  and declaration of the instance (either package body and declaration
  255.    --  nodes or subprogram body and declaration nodes depending on the case).
  256.    --  On return, the node N has been rewritten with the actual body.
  257.  
  258.    procedure Check_Formal_Packages (P_Id : Entity_Id);
  259.    --  Apply the following to all formal packages in generic associations.
  260.  
  261.    procedure Check_Formal_Package_Instance
  262.      (Actual   : Node_Id;
  263.       Form_Pkg : Entity_Id;
  264.       Act_Pkg  : Entity_Id);
  265.    --  Verify that the actuals of the actual instance match the actuals of
  266.    --  the template for a formal package that is not declared with a box.
  267.  
  268.    procedure Check_Private_View (N : Node_Id);
  269.    --  Check whether the type of a generic entity has a different view between
  270.    --  the point of generic analysis and the point of instantiation. If the
  271.    --  view has changed, then at the point of instantiation we restore the
  272.    --  correct view to perform semantic analysis of the instance, and reset
  273.    --  the current view after instantiation.
  274.  
  275.    procedure Check_Generic_Actuals (Instance : Entity_Id);
  276.    --  Similar to previous one. Check the actuals in the instantiation,
  277.    --  whose views can change between the point of instantiation and the point
  278.    --  of instantiation of the body. In addition, mark the generic renamings
  279.    --  as generic actuals, so that they are not compatible with other actuals.
  280.    --  Recurse on an actual that is a formal package whose declaration has
  281.    --  a box.
  282.  
  283.    procedure Check_Generic_Child_Unit
  284.      (Gen_Id           : Node_Id;
  285.       Parent_Installed : in out Boolean);
  286.    --  If the name of the generic unit in an instantiation is a selected
  287.    --  component, then the prefix may be an instance and the selector may
  288.    --  designate a child unit. Retrieve the parent generic and search for
  289.    --  the child unit that must be declared within.
  290.  
  291.    function Get_Instance_Of  (A : Entity_Id) return Entity_Id;
  292.    --  Retrieve actual associated with given generic parameter.
  293.    --  If A is uninstantiated or not a generic parameter, return A.
  294.  
  295.    procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
  296.    --  Associate analyzed generic parameter with corresponding
  297.    --  instance. Used for semantic checks at instantiation time.
  298.  
  299.    procedure Install_Body
  300.      (Act_Body : Node_Id;
  301.       N        : Node_Id;
  302.       Gen_Body : Node_Id;
  303.       Gen_Decl : Node_Id);
  304.    --  If the instantiation happens textually before the body of the generic,
  305.    --  the instantiation of the body must be placed after the generic body,
  306.    --  and not at the point of instantiation. Such early instantiations can
  307.    --  happen if the generic and the instance appear in  a package declaration
  308.    --  because the generic body can only appear in the corresponding package
  309.    --  body. Early instantiations can also appear if generic, instance and
  310.    --  body are all in the declarative part of a subprogram or entry.
  311.  
  312.    procedure Install_Parent (P : Entity_Id);
  313.    --  When compiling an instance of a child unit the parent (which is
  314.    --  itself an instance) is an enclosing scope that must be made
  315.    --  immediately visible.
  316.  
  317.    procedure Remove_Parent;
  318.    --  Reverse effect after instantiation of child is complete.
  319.  
  320.    --  The functions Instantiate_XXX perform various legality checks and build
  321.    --  the declarations for instantiated generic parameters.
  322.    --  Need to describe what the parameters are ???
  323.  
  324.    function Instantiate_Object
  325.       (Formal          : Node_Id;
  326.        Actual          : Node_Id;
  327.        Analyzed_Formal : Node_Id)
  328.       return Node_Id;
  329.  
  330.    function Instantiate_Type
  331.      (Formal          : Node_Id;
  332.       Actual          : Node_Id;
  333.       Analyzed_Formal : Node_Id)
  334.       return Node_Id;
  335.  
  336.    function Instantiate_Formal_Subprogram
  337.      (Formal          : Node_Id;
  338.       Actual          : Node_Id;
  339.       Analyzed_Formal : Node_Id)
  340.       return Node_Id;
  341.  
  342.    function Instantiate_Formal_Package
  343.      (Formal          : Node_Id;
  344.       Actual          : Node_Id;
  345.       Analyzed_Formal : Node_Id)
  346.       return Node_Id;
  347.    --  If the formal package is declared with a box, special visibility rules
  348.    --  apply to its formals: they are in the visible part of the package. This
  349.    --  is true in the declarative region of the formal package, that is to say
  350.    --  in the enclosing generic or instantiation. For an instantiation, the
  351.    --  parameters of the formal package are made visible in an explicit step.
  352.    --  Furthermore, if the actual is a visible use_clause, these formals must
  353.    --  be made potentially use_visible as well. On exit from the enclosing
  354.    --  instantiation, the reverse must be done.
  355.  
  356.    --  For a formal package declared without a box, there are conformance rules
  357.    --  that apply to the actuals in the generic declaration and the actuals of
  358.    --  the actual package in the enclosing instantiation. The simplest way to
  359.    --  apply these rules is to repeat the instantiation of the formal package
  360.    --  in the context of the enclosing instance, and compare the generic
  361.    --  associations of this instantiation with those of the actual package.
  362.  
  363.    function Is_In_Main_Unit (N : Node_Id) return Boolean;
  364.    --  Test if given node is in the main unit
  365.  
  366.    procedure Load_Parent_Of_Generic (N : Entity_Id; Spec : Node_Id);
  367.    --  If the generic appears in a separate non-generic library unit,
  368.    --  load the corresponding body to retrieve the body of the generic.
  369.  
  370.    procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
  371.    --  If a generic is a compilation unit, its instantiation has semantic
  372.    --  dependences on the context units of the generic. Eventually these
  373.    --  dependences will be reflected in actual ali files for generic units.
  374.    --  In the meantime, the simplest is to attach the with clauses of the
  375.    --  generic compilation to the compilation that contains the instance.
  376.  
  377.    function Associated_Node (N : Node_Id) return Node_Id;
  378.    --  Nodes in a generic unit that have an entity field are linked to the
  379.    --  corresponding nodes in the semantic copy, so that non-local references
  380.    --  in the copy can be marked in the original generic nodes. The link
  381.    --  overlaps the Entity field of the node, and must be reset correctly
  382.    --  after collecting global references.
  383.  
  384.    procedure Move_Freeze_Nodes
  385.      (Out_Of : Entity_Id;
  386.       After  : Node_Id;
  387.       L      : List_Id);
  388.    --  Freeze nodes can be generated in the analysis of a generic unit, but
  389.    --  will not be seen by the back-end. It is necessary to move those nodes
  390.    --  to the enclosing scope if they freeze an outer entity. We place them
  391.    --  at the end of the enclosing generic package, which is semantically
  392.    --  neutral.
  393.  
  394.    procedure Set_Associated_Node
  395.      (Gen_Node  : Node_Id;
  396.       Copy_Node : Node_Id);
  397.    --  Establish the link between an identifier in the generic unit, and the
  398.    --  corresponding node in the semantic copy.
  399.  
  400.    -------------------------------------------
  401.    -- Data structures for generic renamings --
  402.    -------------------------------------------
  403.  
  404.    --  Need more documentation of what Assoc and the table are for ???
  405.  
  406.    type Assoc is record
  407.       Gen_Id : Entity_Id;
  408.       Act_Id : Entity_Id;
  409.    end record;
  410.  
  411.    package Generic_Renamings is new Table
  412.      (Table_Component_Type => Assoc,
  413.       Table_Index_Type     => Int,
  414.       Table_Low_Bound      => 0,
  415.       Table_Initial        => 10,
  416.       Table_Increment      => 10,
  417.       Table_Name           => "Generic_Renamings");
  418.  
  419.    Exchanged_Views : Elist_Id;
  420.    --  This list holds the private views that have been exchanged during
  421.    --  instantiation to restore the visibility of the generic declaration.
  422.    --  (see comments above). After instantiation, the current visibility is
  423.    --  reestablished by means of a traversal of this list.
  424.  
  425.    procedure Restore_Private_Views
  426.      (Pack_Id    : Entity_Id;
  427.       Is_Package : Boolean := True);
  428.    --  Restore the private views of external types, and unmark the generic
  429.    --  renamings of actuals, so that they become comptible subtypes again.
  430.    --  For subprograms, Pack_Id is the package constructed to hold the
  431.    --  renamings.
  432.  
  433.    ------------------------------------
  434.    -- Structures for Error Reporting --
  435.    ------------------------------------
  436.  
  437.    Instantiation_Node  : Node_Id;
  438.    --  Used by subprograms that validate instantiation of formal parameters
  439.    --  where there might be no actual on which to place the error message.
  440.  
  441.    Instantiation_Error : exception;
  442.    --  When there is a semantic error in the generic parameter matching,
  443.    --  there is no point in continuing the instantiation, because the
  444.    --  number of cascaded errors is unpredictable. This exception aborts
  445.    --  the instantiation process altogether.
  446.  
  447.    ---------------------------
  448.    -- Abandon_Instantiation --
  449.    ---------------------------
  450.  
  451.    procedure Abandon_Instantiation (N : Node_Id) is
  452.    begin
  453.       Error_Msg_N ("instantiation abandoned!", N);
  454.       raise Instantiation_Error;
  455.    end Abandon_Instantiation;
  456.  
  457.    ------------------------------------------
  458.    -- Analyze_Generic_Package_Declaration  --
  459.    ------------------------------------------
  460.  
  461.    procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
  462.       Id          : Entity_Id;
  463.       New_N       : Node_Id;
  464.       Save_Parent : Node_Id;
  465.  
  466.    begin
  467.       Validate_RCI_Nested_Generic_Declaration (N);
  468.  
  469.       --  Create copy of generic unit, and save for instantiation.
  470.       --  If the unit is a child unit, do not copy the specifications
  471.       --  for the parent,  which are not part of the generic tree.
  472.  
  473.       Save_Parent := Parent_Spec (N);
  474.       Set_Parent_Spec (N, Empty);
  475.  
  476.       New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
  477.       Set_Parent_Spec (New_N, Save_Parent);
  478.       Rewrite_Substitute_Tree (N, New_N);
  479.       Id  := Defining_Unit_Simple_Name (Specification (N));
  480.  
  481.       --  Expansion is not applied to generic units.
  482.  
  483.       Expander_Mode_Save_And_Set (False);
  484.  
  485.       Enter_Name (Id);
  486.       Set_Ekind (Id, E_Generic_Package);
  487.       Set_Etype (Id, Standard_Void_Type);
  488.       New_Scope (Id);
  489.  
  490.       Set_Categorization_From_Following_Pragmas (N);
  491.  
  492.       --  Entities declared in Pure unit should be set Is_Pure
  493.       --  Since 'Partition_Id cannot be applied to such an entity
  494.  
  495.       Set_Is_Pure (Id, Is_Pure (Current_Scope));
  496.  
  497.       Analyze_Generic_Formal_Part (N);
  498.  
  499.       --  After processing the generic formals, analysis proceeds
  500.       --  as for a non-generic package.
  501.  
  502.       Analyze (Specification (N));
  503.  
  504.       Validate_Categorization_Dependency (N, Id);
  505.  
  506.       Save_Global_References (Original_Node (N));
  507.       Expander_Mode_Restore;
  508.       End_Package_Scope (Id);
  509.  
  510.       if Nkind (Parent (N)) /= N_Compilation_Unit then
  511.          Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
  512.          Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
  513.       end if;
  514.  
  515.    end Analyze_Generic_Package_Declaration;
  516.  
  517.    ---------------------------------------------
  518.    --  Analyze_Generic_Subprogram_Declaration --
  519.    ---------------------------------------------
  520.  
  521.    procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
  522.       Spec        : Node_Id;
  523.       Id          : Entity_Id;
  524.       Formals     : List_Id;
  525.       New_N       : Node_Id;
  526.       Save_Parent : Node_Id;
  527.  
  528.    begin
  529.       --  The visible part of an RCI unit must not contain a
  530.       --  nested generic_declaration. (RM E.2.3(11)).
  531.  
  532.       if Inside_Remote_Call_Interface_Unit
  533.         and then Nkind (Parent (N)) /= N_Compilation_Unit
  534.       then
  535.          Error_Msg_N
  536.            ("nested generic declaration not allowed in rci unit", N);
  537.       end if;
  538.  
  539.       --  Create copy of generic unit,and save for instantiation.
  540.       --  If the unit is a child unit, do not copy the specifications
  541.       --  for the parent, which are not part of the generic tree.
  542.  
  543.       Save_Parent := Parent_Spec (N);
  544.       Set_Parent_Spec (N, Empty);
  545.  
  546.       New_N := Copy_Generic_Node (N, Empty,  Instantiating => False);
  547.       Set_Parent_Spec (New_N, Save_Parent);
  548.       Rewrite_Substitute_Tree (N, New_N);
  549.  
  550.       Spec := Specification (N);
  551.       Id := Defining_Unit_Simple_Name (Spec);
  552.  
  553.       if Nkind (Id) = N_Defining_Operator_Symbol then
  554.          Error_Msg_N
  555.            ("operator symbol not allowed for generic subprogram", Id);
  556.       end if;
  557.  
  558.       --  Expansion is not applied to generic units.
  559.  
  560.       Expander_Mode_Save_And_Set (False);
  561.  
  562.       Enter_Name (Id);
  563.  
  564.       if Nkind (Spec) = N_Function_Specification then
  565.          Set_Ekind (Id, E_Generic_Function);
  566.       else
  567.          Set_Ekind (Id, E_Generic_Procedure);
  568.          Set_Etype (Id, Standard_Void_Type);
  569.       end if;
  570.  
  571.       New_Scope (Id);
  572.  
  573.       Set_Categorization_From_Following_Pragmas (N);
  574.  
  575.       --  Entities declared in Pure unit should be set Is_Pure
  576.       --  Since 'Partition_Id cannot be applied to such an entity
  577.  
  578.       Set_Is_Pure (Id, Is_Pure (Current_Scope));
  579.  
  580.       Analyze_Generic_Formal_Part (N);
  581.  
  582.       Formals := Parameter_Specifications (Spec);
  583.  
  584.       if Present (Formals) then
  585.          Process_Formals (Id, Formals, Spec);
  586.       end if;
  587.  
  588.       Validate_Categorization_Dependency (N, Id);
  589.  
  590.       if Nkind (Spec) = N_Function_Specification then
  591.          Find_Type (Subtype_Mark (Spec));
  592.          Set_Etype (Id, Entity (Subtype_Mark (Spec)));
  593.       end if;
  594.  
  595.       Save_Global_References (Original_Node (N));
  596.       Expander_Mode_Restore;
  597.       End_Scope;
  598.  
  599.    end Analyze_Generic_Subprogram_Declaration;
  600.  
  601.    ----------------------------------
  602.    --  Analyze_Generic_Formal_Part --
  603.    ----------------------------------
  604.  
  605.    procedure Analyze_Generic_Formal_Part (N : Node_Id) is
  606.       Gen_Parm_Decl : Node_Id;
  607.  
  608.    begin
  609.       --  The generic formals are processed in the scope of the generic
  610.       --  unit, where they are immediately visible. The scope is installed
  611.       --  by the caller.
  612.  
  613.       Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
  614.  
  615.       while Present (Gen_Parm_Decl) loop
  616.          Analyze (Gen_Parm_Decl);
  617.          Gen_Parm_Decl := Next (Gen_Parm_Decl);
  618.       end loop;
  619.    end Analyze_Generic_Formal_Part;
  620.  
  621.    ----------------------
  622.    --  Is_In_Main_Unit --
  623.    ----------------------
  624.  
  625.    function Is_In_Main_Unit (N : Node_Id) return Boolean is
  626.       Unum : constant Unit_Number_Type := Get_Sloc_Unit_Number (Sloc (N));
  627.       Current_Unit : Node_Id;
  628.  
  629.    begin
  630.       if Unum = Main_Unit then
  631.          return True;
  632.       elsif Nkind (N) = N_Compilation_Unit then
  633.          return False;
  634.       end if;
  635.  
  636.       Current_Unit := Parent (N);
  637.       while Present (Current_Unit)
  638.         and then Nkind (Current_Unit) /= N_Compilation_Unit
  639.       loop
  640.          Current_Unit := Parent (Current_Unit);
  641.       end loop;
  642.  
  643.       --  The instantiation node is in the main unit, or else the current
  644.       --  node (perhaps as the result of nested instantiations) is in the
  645.       --  main unit, or in the declaration of the main unit, which in this
  646.       --  last case must be a body.
  647.  
  648.       return Unum = Main_Unit
  649.         or else Current_Unit = Cunit (Main_Unit)
  650.         or else Current_Unit = Library_Unit (Cunit (Main_Unit))
  651.         or else (Present (Library_Unit (Current_Unit))
  652.                   and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
  653.    end Is_In_Main_Unit;
  654.  
  655.    -----------------------------------
  656.    -- Analyze_Package_Instantiation --
  657.    -----------------------------------
  658.  
  659.    procedure Analyze_Package_Instantiation (N : Node_Id) is
  660.       Loc              : constant Source_Ptr := Sloc (N);
  661.       Actuals          : constant List_Id    := Generic_Associations (N);
  662.       Gen_Id           : constant Node_Id    := Name (N);
  663.  
  664.       Act_Decl         : Node_Id;
  665.       Act_Decl_Id      : Entity_Id;
  666.       Act_Spec         : Node_Id;
  667.       Act_Tree         : Node_Id;
  668.  
  669.       Gen_Decl         : Node_Id;
  670.       Gen_Unit         : Entity_Id;
  671.  
  672.       Parent_Installed : Boolean := False;
  673.       Renaming_List    : List_Id;
  674.       Unit_Renaming    : Node_Id;
  675.       Needs_Body       : Boolean;
  676.  
  677.       Save_Instantiated_Parent : Entity_Id;
  678.       Save_Exchanged_Views     : Elist_Id;
  679.  
  680.    begin
  681.       --  Very first thing: apply the special kludge for Text_IO processing
  682.       --  in case we are instantiating one of the children of [Wide_]Text_IO.
  683.  
  684.       Text_IO_Kludge (Name (N));
  685.  
  686.       --  Make node global for error reporting.
  687.  
  688.       Instantiation_Node := N;
  689.  
  690.       if Nkind (N) = N_Package_Instantiation then
  691.          Act_Decl_Id := New_Copy (Defining_Unit_Simple_Name (N));
  692.  
  693.       else
  694.          --  Instantiation of a formal package.
  695.  
  696.          Act_Decl_Id := Defining_Identifier (N);
  697.       end if;
  698.  
  699.       Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
  700.       Gen_Unit := Entity (Gen_Id);
  701.  
  702.       --  If renaming, indicate this is an instantiation of renamed unit
  703.  
  704.       if Present (Renamed_Object (Gen_Unit))
  705.         and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
  706.       then
  707.          Gen_Unit := Renamed_Object (Gen_Unit);
  708.          Set_Entity (Gen_Id, Gen_Unit);
  709.       end if;
  710.  
  711.       --  Verify that it is the name of a generic package
  712.  
  713.       if Etype (Gen_Unit) = Any_Type then
  714.          return;
  715.       end if;
  716.  
  717.       if Ekind (Gen_Unit) /= E_Generic_Package then
  718.          Error_Msg_N
  719.             ("expect name of generic package in instantiation", Gen_Id);
  720.  
  721.       elsif In_Open_Scopes (Gen_Unit) then
  722.          Error_Msg_NE
  723.             ("instantiation of & within itself", N, Gen_Id);
  724.  
  725.       else
  726.          Gen_Decl := Get_Declaration_Node (Gen_Unit);
  727.  
  728.          --  Initialize renamings map, for error checking, and the list
  729.          --  that holds private entities whose views have changed between
  730.          --  generic definition and instantiation.
  731.  
  732.          Save_Exchanged_Views := Exchanged_Views;
  733.          Exchanged_Views := New_Elmt_List;
  734.          Generic_Renamings.Set_Last (0);
  735.  
  736.          --  Copy original generic tree, to produce text for instantiation.
  737.  
  738.          Save_Instantiated_Parent := Current_Instantiated_Parent;
  739.          Current_Instantiated_Parent := Gen_Unit;
  740.  
  741.          Act_Tree := Copy_Generic_Node
  742.                    (Original_Node (Gen_Decl), Empty, Instantiating => True);
  743.  
  744.          Act_Spec := Specification (Act_Tree);
  745.          Renaming_List := Analyze_Associations
  746.            (Generic_Formal_Declarations (Act_Tree),
  747.             Actuals,
  748.             Generic_Formal_Declarations (Gen_Decl));
  749.          Set_Defining_Unit_Name (Act_Spec, Act_Decl_Id);
  750.          Set_Generic_Parent (Act_Spec, Gen_Unit);
  751.  
  752.          --  References to the generic in its own declaration or its body
  753.          --  are references to the instance. Add a renaming declaration for
  754.          --  the generic unit itself. This declaration, as well as the renaming
  755.          --  declarations for the generic formals, must remain private to the
  756.          --  unit: the formals, because this is the language semantics, and
  757.          --  the unit because its use is an artifact of the implementation.
  758.  
  759.          Unit_Renaming :=
  760.            Make_Package_Renaming_Declaration (Loc,
  761.              Defining_Unit_Name =>
  762.                Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
  763.              Name => New_Reference_To (Act_Decl_Id, Loc));
  764.  
  765.          Append (Unit_Renaming, Renaming_List);
  766.  
  767.          --  The renaming declarations are the first local declarations of
  768.          --  the new unit.
  769.  
  770.          if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
  771.             Insert_List_Before
  772.               (First (Visible_Declarations (Act_Spec)), Renaming_List);
  773.          else
  774.             Set_Visible_Declarations (Act_Spec, Renaming_List);
  775.          end if;
  776.  
  777.          Act_Decl := Make_Package_Declaration (Loc,
  778.            Specification => Act_Spec);
  779.  
  780.          --  Save the instantiation node, for subsequent instantiation
  781.          --  of the body, if there is one and we are generating code for
  782.          --  the current unit. Mark the unit as having a body, to avoid
  783.          --  a premature error message.
  784.  
  785.          Needs_Body :=
  786.            (Unit_Requires_Body (Gen_Unit)
  787.                or else Present (Corresponding_Body (Gen_Decl)))
  788.              and then Is_In_Main_Unit (N)
  789.              and then (Expander_Active or Xref_Analyze);
  790.  
  791.          if Needs_Body then
  792.             Pending_Instantiations.Increment_Last;
  793.  
  794.             --  Here is a defence against a ludicrous number of instantiations
  795.             --  which can be caused by a nested set of instantiation attempts.
  796.  
  797.             if Pending_Instantiations.Last >
  798.                  System.Parameters.Max_Instantiations
  799.             then
  800.                Error_Msg_N ("too many instantiations", N);
  801.                raise Unrecoverable_Error;
  802.             end if;
  803.  
  804.             --  If OK, then make entry in table
  805.  
  806.             Pending_Instantiations.Table (Pending_Instantiations.Last) :=
  807.                                                                 (N, Act_Decl);
  808.          end if;
  809.  
  810.          Set_Categorization_From_Following_Pragmas (Act_Decl);
  811.  
  812.          if Nkind (Parent (N)) /= N_Compilation_Unit then
  813.             Mark_Rewrite_Insertion (Act_Decl);
  814.             Insert_Before (N, Act_Decl);
  815.             Analyze (Act_Decl);
  816.  
  817.          else
  818.             --  Place declaration on current node so context is complete
  819.             --  for analysis (including nested instantiations).
  820.  
  821.             Set_Unit (Parent (N), Act_Decl);
  822.             Set_Parent_Spec (Act_Decl, Parent_Spec (N));
  823.             Analyze (Act_Decl);
  824.             Set_Unit (Parent (N), N);
  825.             Set_Body_Required (Parent (N), False);
  826.          end if;
  827.  
  828.          Current_Instantiated_Parent := Save_Instantiated_Parent;
  829.  
  830.          if not Needs_Body
  831.            and then Nkind (Parent (N)) = N_Compilation_Unit
  832.          then
  833.             Rewrite_Substitute_Tree (N, Act_Decl);
  834.          end if;
  835.  
  836.          Set_Has_Completion (Act_Decl_Id);
  837.          Check_Formal_Packages (Act_Decl_Id);
  838.  
  839.          Restore_Private_Views (Act_Decl_Id);
  840.          Exchanged_Views := Save_Exchanged_Views;
  841.          Inherit_Context (Gen_Decl, N);
  842.  
  843.          if Parent_Installed then
  844.             Remove_Parent;
  845.          end if;
  846.  
  847.       end if;
  848.  
  849.       Validate_Categorization_Dependency (N, Act_Decl_Id);
  850.  
  851.    exception
  852.       when Instantiation_Error =>
  853.          null;
  854.  
  855.    end Analyze_Package_Instantiation;
  856.  
  857.    ------------------------------
  858.    -- Instantiate_Package_Body --
  859.    ------------------------------
  860.  
  861.    procedure Instantiate_Package_Body
  862.      (N        : Node_Id;
  863.       Act_Decl : Node_Id)
  864.    is
  865.       Gen_Id        : constant Node_Id   := Name (N);
  866.       Gen_Unit      : constant Entity_Id := Entity (Name (N));
  867.       Gen_Decl      : constant Node_Id   := Get_Declaration_Node (Gen_Unit);
  868.       Act_Decl_Id   : constant Entity_Id :=
  869.                         Defining_Unit_Name (Specification (Act_Decl));
  870.       Gen_Body      : Node_Id;
  871.       Gen_Body_Id   : Node_Id;
  872.       Act_Body      : Node_Id;
  873.       Act_Body_Id   : Entity_Id;
  874.  
  875.       Save_Instantiated_Parent : Entity_Id;
  876.       Save_Exchanged_Views     : Elist_Id;
  877.  
  878.    begin
  879.       Gen_Body_Id := Corresponding_Body (Gen_Decl);
  880.  
  881.       if No (Gen_Body_Id) then
  882.          Load_Parent_Of_Generic (N, Specification (Gen_Decl));
  883.          Gen_Body_Id := Corresponding_Body (Gen_Decl);
  884.       end if;
  885.  
  886.       if Present (Gen_Body_Id) then
  887.          Save_Instantiated_Parent := Current_Instantiated_Parent;
  888.          Current_Instantiated_Parent := Gen_Unit;
  889.          Save_Exchanged_Views := Exchanged_Views;
  890.          Exchanged_Views := New_Elmt_List;
  891.          Gen_Body := Get_Declaration_Node (Gen_Body_Id);
  892.          Act_Body := Copy_Generic_Node
  893.                 (Original_Node (Gen_Body), Empty, Instantiating => True);
  894.          Act_Body_Id := Defining_Unit_Simple_Name (Act_Body);
  895.          Set_Chars (Act_Body_Id, Chars (Act_Decl_Id));
  896.          Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
  897.          Check_Generic_Actuals (Act_Decl_Id);
  898.  
  899.          --  If it is a child unit, make the parent instance (which is an
  900.          --  instance of the parent of the generic) visible. The parent
  901.          --  instance is the prefix of the name of the generic unit.
  902.  
  903.          if Ekind (Scope (Gen_Unit)) = E_Generic_Package
  904.            and then Nkind (Gen_Id) = N_Expanded_Name
  905.          then
  906.             Install_Parent (Entity (Prefix (Gen_Id)));
  907.          end if;
  908.  
  909.          --  If the instantiation is a library unit, and this is the main
  910.          --  unit, then build the resulting compilation unit nodes for the
  911.          --  instance. If this is a compilation unit but it is not the main
  912.          --  unit, then it is the body of a unit in the context, that is being
  913.          --  compiled because it is encloses some inlined unit or another
  914.          --  generic unit being instantiated. In that case, this body is not
  915.          --  part of the current compilation, and is not attached to the tree.
  916.  
  917.          if Nkind (Parent (N)) = N_Compilation_Unit then
  918.  
  919.             if Parent (N) = Cunit (Main_Unit) then
  920.                Build_Instance_Compilation_Unit_Nodes (N, Act_Body, Act_Decl);
  921.             else
  922.                null;
  923.             end if;
  924.  
  925.          --  If the instantiation is not a library unit, then place the
  926.          --  body either at the instantiation node, or after the generic
  927.          --  body itself.
  928.  
  929.          else
  930.             Install_Body (Act_Body, N, Gen_Body, Gen_Decl);
  931.          end if;
  932.  
  933.          Analyze (Act_Body);
  934.          Inherit_Context (Gen_Body, N);
  935.          Current_Instantiated_Parent := Save_Instantiated_Parent;
  936.          Restore_Private_Views (Act_Decl_Id);
  937.          Exchanged_Views := Save_Exchanged_Views;
  938.  
  939.          --  If the body instance contains finalizable objects, and the
  940.          --  enclosing scope does not, we must create the finalization chain
  941.          --  and the finalization procedure now.  We must find the right
  942.          --  enclosing scope, reinstall it, and expand the required actions.
  943.  
  944.          declare
  945.             P : Entity_Id;
  946.  
  947.          begin
  948.             if Parent (N) /= Cunit (Main_Unit) then
  949.                P := Enclosing_Dynamic_Scope (Act_Decl_Id);
  950.  
  951.                if Present (Finalization_Chain_Entity (P))
  952.                  and then No (Parent (Finalization_Chain_Entity (P)))
  953.                then
  954.                   New_Scope (P);
  955.                   Expand_Cleanup_Actions (Get_Declaration_Node (P));
  956.                   End_Scope;
  957.                end if;
  958.             end if;
  959.          end;
  960.  
  961.       elsif Unit_Requires_Body (Gen_Unit) then
  962.          Error_Msg_NE ("cannot find body of generic package &", N, Gen_Unit);
  963.  
  964.       --  Case of package that does not need a body
  965.  
  966.       else
  967.          --  If the instantiation of the declaration is a library unit,
  968.          --  rewrite the original package instantiation as a package
  969.          --  declaration in the compilation unit node.
  970.  
  971.          if Nkind (Parent (N)) = N_Compilation_Unit then
  972.             Set_Parent_Spec (Act_Decl, Parent_Spec (N));
  973.             Rewrite_Substitute_Tree (N, Act_Decl);
  974.  
  975.          --  If the instantiation is not a library unit, then append the
  976.          --  declaration to the list of implicitly generated entities.
  977.  
  978.          else
  979.             Mark_Rewrite_Insertion (Act_Decl);
  980.             Insert_Before (N, Act_Decl);
  981.          end if;
  982.       end if;
  983.    end Instantiate_Package_Body;
  984.  
  985.    ---------------------------------
  986.    -- Instantiate_Subprogram_Body --
  987.    ---------------------------------
  988.  
  989.    procedure Instantiate_Subprogram_Body
  990.      (N        : Node_Id;
  991.       Act_Decl : Node_Id)
  992.    is
  993.       Loc           : constant Source_Ptr := Sloc (N);
  994.       Gen_Unit      : constant Entity_Id  := Entity (Name (N));
  995.       Gen_Decl      : constant Node_Id    := Get_Declaration_Node (Gen_Unit);
  996.       Act_Decl_Id   : constant Entity_Id  :=
  997.                         Defining_Unit_Name (Specification (Act_Decl));
  998.       Gen_Body      : Node_Id;
  999.       Gen_Body_Id   : Node_Id;
  1000.       Act_Body      : Node_Id;
  1001.       Act_Body_Id   : Entity_Id;
  1002.       Pack_Id       : Entity_Id := Defining_Unit_Name (Parent (Act_Decl));
  1003.       Pack_Body     : Node_Id;
  1004.       Unit_Renaming : Node_Id;
  1005.  
  1006.       Save_Instantiated_Parent : Entity_Id;
  1007.       Save_Exchanged_Views     : Elist_Id;
  1008.  
  1009.    begin
  1010.       Gen_Body_Id := Corresponding_Body (Gen_Decl);
  1011.  
  1012.       if No (Gen_Body_Id) then
  1013.          Load_Parent_Of_Generic (N, Specification (Gen_Decl));
  1014.          Gen_Body_Id := Corresponding_Body (Gen_Decl);
  1015.       end if;
  1016.  
  1017.       if Present (Gen_Body_Id) then
  1018.          Save_Exchanged_Views := Exchanged_Views;
  1019.          Exchanged_Views := New_Elmt_List;
  1020.          Save_Instantiated_Parent := Current_Instantiated_Parent;
  1021.          Current_Instantiated_Parent := Gen_Unit;
  1022.          Gen_Body := Get_Declaration_Node (Gen_Body_Id);
  1023.          Act_Body := Copy_Generic_Node
  1024.                 (Original_Node (Gen_Body), Empty, Instantiating => True);
  1025.          Act_Body_Id := Defining_Unit_Simple_Name (Specification (Act_Body));
  1026.          Set_Chars (Act_Body_Id, Chars (Act_Decl_Id));
  1027.          Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
  1028.          Check_Generic_Actuals (Pack_Id);
  1029.  
  1030.          --  Inside its body, a reference to the generic unit is a reference
  1031.          --  to the instance. The corresponding renaming is the first
  1032.          --  declaration in the body.
  1033.  
  1034.          Unit_Renaming :=
  1035.            Make_Subprogram_Renaming_Declaration (Loc,
  1036.              Specification =>
  1037.                Copy_Generic_Node (
  1038.                  Specification (Original_Node (Gen_Body)),
  1039.                  Empty,
  1040.                  Instantiating => True),
  1041.                 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
  1042.  
  1043.          --  The subprogram body is placed in the body of a dummy package
  1044.          --  body, whose spec contains the subprogram declaration as well
  1045.          --  as the renaming declarations for the generic parameters.
  1046.  
  1047.          Pack_Body := Make_Package_Body (Loc,
  1048.            Defining_Unit_Name => New_Copy (Pack_Id),
  1049.            Declarations       => New_List (Unit_Renaming, Act_Body));
  1050.  
  1051.          Set_Corresponding_Spec (Pack_Body, Pack_Id);
  1052.  
  1053.          --  If the instantiation is a library unit, then build
  1054.          --  the resulting compilation unit nodes for the instance
  1055.          --  The declaration of the enclosing package is the grandparent
  1056.          --  of the subprogram declaration. First replace the instantiation
  1057.          --  node as the unit of the corresponding compilation.
  1058.  
  1059.          if Nkind (Parent (N)) = N_Compilation_Unit then
  1060.             Set_Unit (Parent (N), N);
  1061.             Build_Instance_Compilation_Unit_Nodes (N, Pack_Body,
  1062.                Parent (Parent (Act_Decl)));
  1063.  
  1064.          --  If the instantiation is not a library unit, then place the
  1065.          --  body either at the instantiation node,  or after the generic
  1066.          --  body itself.
  1067.  
  1068.          else
  1069.             Install_Body (Pack_Body, N, Gen_Body, Gen_Decl);
  1070.          end if;
  1071.  
  1072.          Analyze (Pack_Body);
  1073.          Inherit_Context (Gen_Body, N);
  1074.          Current_Instantiated_Parent := Save_Instantiated_Parent;
  1075.          Restore_Private_Views (Pack_Id, False);
  1076.          Exchanged_Views := Save_Exchanged_Views;
  1077.  
  1078.       else
  1079.          --  Body not found. Error was emitted already.
  1080.  
  1081.          null;
  1082.       end if;
  1083.  
  1084.    end Instantiate_Subprogram_Body;
  1085.  
  1086.    ------------------
  1087.    -- Install_Body --
  1088.    ------------------
  1089.  
  1090.    procedure Install_Body
  1091.      (Act_Body : Node_Id;
  1092.       N        : Node_Id;
  1093.       Gen_Body : Node_Id;
  1094.       Gen_Decl : Node_Id)
  1095.    is
  1096.       Act_Unit  : constant Node_Id :=
  1097.                     Unit (Cunit (Get_Sloc_Unit_Number (Sloc (N))));
  1098.       Gen_Unit  : constant Node_Id :=
  1099.                     Unit (Cunit (Get_Sloc_Unit_Number (Sloc (Gen_Decl))));
  1100.       Body_Unit : constant Node_Id :=
  1101.                     Unit (Cunit (Get_Sloc_Unit_Number (Sloc (Gen_Body))));
  1102.  
  1103.       function True_Sloc (N : Node_Id) return Source_Ptr;
  1104.       --  If the instance is nested inside a generic unit, the Sloc of the
  1105.       --  instance indicates the place of the original definition, not the
  1106.       --  point of the current enclosing instance. Pending a better usage of
  1107.       --  Slocs to indicate instantiation places, we determine the place of
  1108.       --  origin of a node by finding the maximum sloc of any ancestor node.
  1109.  
  1110.       function True_Sloc (N : Node_Id) return Source_Ptr is
  1111.          Res : Source_Ptr;
  1112.          N1  : Node_Id;
  1113.  
  1114.       begin
  1115.          Res := Sloc (N);
  1116.          N1 := N;
  1117.          while Present (N1) and then N1 /= Act_Unit loop
  1118.             if Sloc (N1) > Res then
  1119.                Res := Sloc (N1);
  1120.             end if;
  1121.  
  1122.             N1 := Parent (N1);
  1123.          end loop;
  1124.  
  1125.          return Res;
  1126.       end True_Sloc;
  1127.  
  1128.    --  Start of processing for Install_Body
  1129.  
  1130.    begin
  1131.       --  If the instantiation and the generic definition appear in the
  1132.       --  same package declaration, this is an early instantiation.
  1133.       --  If they appear in the same declarative part, it is an early
  1134.       --  instantiation only if the generic body appears textually later.
  1135.  
  1136.       if Gen_Unit = Act_Unit
  1137.         and then ((Nkind (Gen_Unit) = N_Package_Declaration)
  1138.                     or else Nkind (Gen_Unit) = N_Generic_Package_Declaration
  1139.                     or else (Gen_Unit = Body_Unit
  1140.                                and then True_Sloc (N) < Sloc (Gen_Body)))
  1141.       then
  1142.          Insert_After (Gen_Body, Act_Body);
  1143.       else
  1144.          Insert_Before (N, Act_Body);
  1145.       end if;
  1146.  
  1147.       Mark_Rewrite_Insertion (Act_Body);
  1148.    end Install_Body;
  1149.  
  1150.    --------------------
  1151.    -- Install_Parent --
  1152.    --------------------
  1153.  
  1154.    procedure Install_Parent (P : Entity_Id) is
  1155.       S : Entity_Id := Current_Scope;
  1156.  
  1157.    begin
  1158.       --  We need to install the parent instance to compile the instantiation
  1159.       --  of the child, but the child instance must appear in the current
  1160.       --  scope. Given that we cannot place the parent above the current
  1161.       --  scope in the scope stack, we duplicate the current scope and unstack
  1162.       --  both after the instantiation is complete.
  1163.  
  1164.       New_Scope (P);
  1165.       Set_Is_Immediately_Visible (P);
  1166.       Install_Visible_Declarations (P);
  1167.       Install_Private_Declarations (P);
  1168.  
  1169.       New_Scope (S);
  1170.    end Install_Parent;
  1171.  
  1172.    -------------------
  1173.    -- Remove_Parent --
  1174.    -------------------
  1175.  
  1176.    procedure Remove_Parent is
  1177.    begin
  1178.       --  After child instantiation is complete, remove from scope stack
  1179.       --  the extra copy of the current scope, and then remove parent
  1180.       --  instance.
  1181.  
  1182.       Pop_Scope;
  1183.       End_Package_Scope (Current_Scope);
  1184.    end Remove_Parent;
  1185.  
  1186.    -------------------------------------
  1187.    -- Analyze_Procedure_Instantiation --
  1188.    -------------------------------------
  1189.  
  1190.    procedure Analyze_Procedure_Instantiation (N : Node_Id) is
  1191.    begin
  1192.       Analyze_Subprogram_Instantiation (N, E_Procedure);
  1193.    end Analyze_Procedure_Instantiation;
  1194.  
  1195.    ------------------------------------
  1196.    -- Analyze_Function_Instantiation --
  1197.    ------------------------------------
  1198.  
  1199.    procedure Analyze_Function_Instantiation (N : Node_Id) is
  1200.    begin
  1201.       Analyze_Subprogram_Instantiation (N, E_Function);
  1202.    end Analyze_Function_Instantiation;
  1203.  
  1204.    ------------------------------------
  1205.    -- Analyze_Subprogram_Instantiation --
  1206.    ------------------------------------
  1207.  
  1208.    procedure Analyze_Subprogram_Instantiation
  1209.      (N : Node_Id;
  1210.       K : Entity_Kind)
  1211.    is
  1212.       Loc              : constant Source_Ptr := Sloc (N);
  1213.       Actuals          : constant List_Id    := Generic_Associations (N);
  1214.       Gen_Id           : constant Node_Id    := Name (N);
  1215.  
  1216.       Act_Decl_Id      : Entity_Id := New_Copy (Defining_Unit_Simple_Name (N));
  1217.       Act_Decl         : Node_Id;
  1218.       Act_Spec         : Node_Id;
  1219.       Act_Tree         : Node_Id;
  1220.  
  1221.       Gen_Unit         : Entity_Id;
  1222.       Gen_Decl         : Node_Id;
  1223.       Pack_Id          : Entity_Id;
  1224.       Parent_Installed : Boolean := False;
  1225.       Renaming_List    : List_Id;
  1226.       Spec             : Node_Id;
  1227.  
  1228.       Save_Exchanged_Views : Elist_Id;
  1229.  
  1230.       procedure Analyze_Instance_And_Renamings;
  1231.       --  The instance must be analyzed in a context that includes the
  1232.       --  mappings of generic parameters into actuals. We create a package
  1233.       --  declaration for this purpose. After analysis,  we reset the scope
  1234.       --  of the instance to be the current one, rather than the bogus package.
  1235.  
  1236.       procedure Analyze_Instance_And_Renamings is
  1237.          Pack_Decl : Node_Id;
  1238.  
  1239.       begin
  1240.          if Nkind (Parent (N)) = N_Compilation_Unit then
  1241.  
  1242.             --  The container package has the same name as the instantiation,
  1243.             --  to insure that the binder calls the elaboration procedure
  1244.             --  with the right name.
  1245.  
  1246.             Pack_Id := Make_Defining_Identifier (Loc, Chars (Act_Decl_Id));
  1247.  
  1248.          else
  1249.             Pack_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
  1250.          end if;
  1251.  
  1252.          Pack_Decl := Make_Package_Declaration (Loc,
  1253.            Specification => Make_Package_Specification (Loc,
  1254.              Defining_Unit_Name  => Pack_Id,
  1255.              Visible_Declarations => Renaming_List));
  1256.  
  1257.          if Nkind (Parent (N)) /= N_Compilation_Unit then
  1258.             Mark_Rewrite_Insertion (Pack_Decl);
  1259.             Insert_Before (N, Pack_Decl);
  1260.             Set_Has_Completion (Pack_Id);
  1261.  
  1262.          else
  1263.             --  Place declaration on current node so context is complete
  1264.             --  for analysis (including nested instantiations), and for
  1265.             --  use in a context_clause (see Analyze_With_Clause).
  1266.  
  1267.             Set_Unit (Parent (N), Pack_Decl);
  1268.          end if;
  1269.  
  1270.          Analyze (Pack_Decl);
  1271.          Check_Formal_Packages (Pack_Id);
  1272.  
  1273.          --  Body of the enclosing package is supplied when instantiating
  1274.          --  the subprogram body, after semantic  analysis is completed.
  1275.  
  1276.          --  Insert subprogram entity into current scope, so that
  1277.          --  visiblity is correct for callers. First remove subprogram
  1278.          --  from visibility, so that subsequent insertion works properly.
  1279.  
  1280.          declare
  1281.             Prev : Entity_Id;
  1282.  
  1283.          begin
  1284.             Prev := First_Entity (Pack_Id);
  1285.  
  1286.             while Present (Prev) loop
  1287.                exit when Next_Entity (Prev) = Act_Decl_Id;
  1288.                Prev := Next_Entity (Prev);
  1289.             end loop;
  1290.  
  1291.             if Act_Decl_Id = First_Entity (Pack_Id) then
  1292.                Set_First_Entity (Pack_Id, Empty);
  1293.                Set_Last_Entity  (Pack_Id, Empty);
  1294.             else
  1295.                Set_Next_Entity (Prev, Next_Entity (Act_Decl_Id));
  1296.                Set_Last_Entity (Pack_Id, Prev);
  1297.             end if;
  1298.          end;
  1299.  
  1300.          if Nkind (Parent (N)) = N_Compilation_Unit then
  1301.  
  1302.             --  Skip package as well.
  1303.  
  1304.             Set_Name_Entity_Id
  1305.                (Chars (Act_Decl_Id), Homonym (Homonym (Act_Decl_Id)));
  1306.  
  1307.          else
  1308.             declare
  1309.                Prev : Entity_Id := Current_Entity (Act_Decl_Id);
  1310.             begin
  1311.                while Present (Prev)
  1312.                  and then Homonym (Prev) /= Act_Decl_Id
  1313.                loop
  1314.                   Prev := Homonym (Prev);
  1315.                end loop;
  1316.  
  1317.                if No (Prev) then
  1318.                   Set_Name_Entity_Id (Chars (Act_Decl_Id),
  1319.                                     Homonym (Act_Decl_Id));
  1320.                else
  1321.                   Set_Homonym (Prev, Homonym (Act_Decl_Id));
  1322.                end if;
  1323.             end;
  1324.          end if;
  1325.  
  1326.          New_Overloaded_Entity (Act_Decl_Id);
  1327.  
  1328.       end Analyze_Instance_And_Renamings;
  1329.  
  1330.    --  Start of processing for Analyze_Subprogram_Instantiation
  1331.  
  1332.    begin
  1333.       --  Make node global for error reporting.
  1334.  
  1335.       Instantiation_Node := N;
  1336.  
  1337.       Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
  1338.       Gen_Unit := Entity (Gen_Id);
  1339.  
  1340.       --  If renaming, indicate that this is instantiation of renamed unit
  1341.  
  1342.       if Present (Renamed_Object (Gen_Unit))
  1343.         and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
  1344.         or else  Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
  1345.       then
  1346.          Gen_Unit := Renamed_Object (Gen_Unit);
  1347.          Set_Entity (Gen_Id, Gen_Unit);
  1348.       end if;
  1349.  
  1350.       if Etype (Gen_Unit) = Any_Type then return; end if;
  1351.  
  1352.       --  Verify that it is a generic subprogram of the right kind.
  1353.  
  1354.       if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
  1355.          Error_Msg_N
  1356.             ("expect name of generic procedure in instantiation", Gen_Id);
  1357.  
  1358.       elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
  1359.          Error_Msg_N
  1360.             ("expect name of generic function in instantiation", Gen_Id);
  1361.  
  1362.       elsif In_Open_Scopes (Gen_Unit) then
  1363.          Error_Msg_NE
  1364.            ("instantiation of & within itself", N, Gen_Id);
  1365.  
  1366.       else
  1367.          Gen_Decl := Get_Declaration_Node (Gen_Unit);
  1368.          Spec     := Specification (Gen_Decl);
  1369.  
  1370.          --  Initialize renamings map, for error checking.
  1371.  
  1372.          Save_Exchanged_Views := Exchanged_Views;
  1373.          Exchanged_Views := New_Elmt_List;
  1374.          Generic_Renamings.Set_Last (0);
  1375.  
  1376.          --  Copy original generic tree, to produce text for instantiation.
  1377.  
  1378.          Act_Tree := Copy_Generic_Node
  1379.                    (Original_Node (Gen_Decl), Empty, Instantiating => True);
  1380.  
  1381.          Act_Spec := Specification (Act_Tree);
  1382.          Renaming_List := Analyze_Associations
  1383.            (Generic_Formal_Declarations (Act_Tree),
  1384.             Actuals,
  1385.             Generic_Formal_Declarations (Gen_Decl));
  1386.  
  1387.          Set_Defining_Unit_Name (Act_Spec, Act_Decl_Id);
  1388.          Set_Generic_Parent (Act_Spec, Gen_Unit);
  1389.          Act_Decl :=
  1390.            Make_Subprogram_Declaration (Loc,
  1391.              Specification => Act_Spec);
  1392.  
  1393.          Set_Categorization_From_Following_Pragmas (Act_Decl);
  1394.  
  1395.          Append (Act_Decl, Renaming_List);
  1396.          Set_Has_Completion (Act_Decl_Id);
  1397.          Analyze_Instance_And_Renamings;
  1398.  
  1399.          --  If the generic is marked Import (Intrinsic), then so is the
  1400.          --  instance. This indicates that there is no body to instantiate.
  1401.          --  Other pragmas might also be inherited ???
  1402.  
  1403.          if Is_Intrinsic_Subprogram (Gen_Unit) then
  1404.             Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
  1405.  
  1406.             if Chars (Gen_Unit) = Name_Unchecked_Conversion then
  1407.                Validate_Unchecked_Conversion (N, Act_Decl_Id);
  1408.             end if;
  1409.          end if;
  1410.  
  1411.          if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
  1412.             Inherit_Context (Gen_Decl, N);
  1413.             Restore_Private_Views (Pack_Id, False);
  1414.  
  1415.             --  If the context requires a full instantiation, mark node for
  1416.             --  subsequent construction of the body.
  1417.  
  1418.             if Is_In_Main_Unit (N)
  1419.               and then (Expander_Active or Xref_Analyze)
  1420.             then
  1421.                Pending_Instantiations.Increment_Last;
  1422.                Pending_Instantiations.Table (Pending_Instantiations.Last) :=
  1423.                  (N, Act_Decl);
  1424.             end if;
  1425.          end if;
  1426.  
  1427.          Exchanged_Views := Save_Exchanged_Views;
  1428.  
  1429.          --  Subject to change, pending on if other pragmas are inherited ???
  1430.  
  1431.          Validate_Categorization_Dependency (N, Act_Decl_Id);
  1432.  
  1433.          if Parent_Installed then
  1434.             Remove_Parent;
  1435.          end if;
  1436.       end if;
  1437.  
  1438.    exception
  1439.       when Instantiation_Error =>
  1440.          null;
  1441.  
  1442.    end Analyze_Subprogram_Instantiation;
  1443.  
  1444.    ----------------------------
  1445.    -- Load_Parent_Of_Generic --
  1446.    ----------------------------
  1447.  
  1448.    procedure Load_Parent_Of_Generic (N : Entity_Id; Spec : Node_Id) is
  1449.       Comp_Unit   : constant Node_Id :=
  1450.                     Cunit (Get_Sloc_Unit_Number (Sloc (Spec)));
  1451.       True_Parent : Node_Id;
  1452.       Inst_Node   : Node_Id;
  1453.  
  1454.    begin
  1455.       if Get_Sloc_Unit_Number (Sloc (N)) /=
  1456.          Get_Sloc_Unit_Number (Sloc (Spec))
  1457.         or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
  1458.       then
  1459.          --  Find body of parent of spec, and analyze it. A special case
  1460.          --  arises when the parent is an instantiation, that is to say when
  1461.          --  we are currently instantiating a nested generic. In that case,
  1462.          --  there is no separate file for the body of the enclosing instance.
  1463.          --  Instead, the enclosing body must be instantiated as if it were
  1464.          --  a pending instantiation, in order to produce the body for the
  1465.          --  nested generic we require now.
  1466.  
  1467.          True_Parent := Parent (Spec);
  1468.          Inst_Node   := Empty;
  1469.  
  1470.          while Present (True_Parent)
  1471.            and then Nkind (True_Parent) /= N_Compilation_Unit
  1472.          loop
  1473.             if Nkind (True_Parent) = N_Package_Declaration
  1474.               and then Nkind (Original_Node (True_Parent))
  1475.                                             = N_Package_Instantiation
  1476.             then
  1477.                --  Parent is a compilation unit that is an instantiation.
  1478.                --  Instantiation node has been replaced with package decl.
  1479.  
  1480.                Inst_Node := Original_Node (True_Parent);
  1481.                exit;
  1482.  
  1483.             elsif Nkind (True_Parent) = N_Package_Declaration
  1484.               and then Present (Generic_Parent (Specification (True_Parent)))
  1485.             then
  1486.                --  Parent is an instantiation within another specification.
  1487.                --  Declaration for instance has been inserted before original
  1488.                --  instantiation node. A direct link would be preferable?
  1489.  
  1490.                Inst_Node := Next (True_Parent);
  1491.  
  1492.                while Nkind (Inst_Node) /= N_Package_Instantiation loop
  1493.                   Inst_Node := Next (Inst_Node);
  1494.                end loop;
  1495.  
  1496.                exit;
  1497.             else
  1498.                True_Parent := Parent (True_Parent);
  1499.             end if;
  1500.          end loop;
  1501.  
  1502.          if Present (Inst_Node) then
  1503.  
  1504.             if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
  1505.  
  1506.                --  Instantiation node and declaration of instantiated package
  1507.                --  were exchanged when only the declaration was needed.
  1508.                --  Restore instantiation node before proceeding with body.
  1509.  
  1510.                Set_Unit (Parent (True_Parent), Inst_Node);
  1511.             end if;
  1512.  
  1513.             --  Now complete instantiation of enclosing body.
  1514.  
  1515.             Instantiate_Package_Body (Inst_Node, True_Parent);
  1516.  
  1517.          else
  1518.             Load_Needed_Body (Comp_Unit);
  1519.          end if;
  1520.       end if;
  1521.    end Load_Parent_Of_Generic;
  1522.  
  1523.    ---------------------
  1524.    -- Inherit_Context --
  1525.    ---------------------
  1526.  
  1527.    procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
  1528.       Current_Context : List_Id;
  1529.       Current_Unit    : Node_Id;
  1530.       Item            : Node_Id;
  1531.       New_I           : Node_Id;
  1532.  
  1533.    begin
  1534.  
  1535.       if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
  1536.  
  1537.          --  The inherited context is attached to the enclosing compilation
  1538.          --  unit. This is either the main unit, or the declaration for the
  1539.          --  main unit (in case the instantation appears within the package
  1540.          --  declaration and the main unit is its body).
  1541.  
  1542.          Current_Unit := Parent (Inst);
  1543.  
  1544.          while Present (Current_Unit)
  1545.            and then Nkind (Current_Unit) /= N_Compilation_Unit
  1546.          loop
  1547.             Current_Unit := Parent (Current_Unit);
  1548.          end loop;
  1549.  
  1550.          Current_Context := Context_Items (Current_Unit);
  1551.  
  1552.          Item := First (Context_Items (Parent (Gen_Decl)));
  1553.          while Present (Item) loop
  1554.  
  1555.             if Nkind (Item) = N_With_Clause then
  1556.                New_I := New_Copy (Item);
  1557.                Set_Implicit_With (New_I, True);
  1558.                Append (New_I, Current_Context);
  1559.             end if;
  1560.  
  1561.             Item := Next (Item);
  1562.          end loop;
  1563.       end if;
  1564.    end Inherit_Context;
  1565.  
  1566.    --------------------------
  1567.    -- Analyze_Associations --
  1568.    --------------------------
  1569.  
  1570.    function Analyze_Associations
  1571.      (Formals : List_Id;
  1572.       Actuals : List_Id;
  1573.       F_Copy  : List_Id)
  1574.       return    List_Id
  1575.    is
  1576.       Actual          : Node_Id;
  1577.       Assoc           : List_Id := New_List;
  1578.       Formal          : Node_Id;
  1579.       Analyzed_Formal : Node_Id;
  1580.       Match           : Node_Id;
  1581.       Named           : Node_Id;
  1582.       First_Named     : Node_Id := Empty;
  1583.       Num_Matched     : Int := 0;
  1584.       Num_Actuals     : Int := 0;
  1585.  
  1586.       function Matching_Actual (F : Entity_Id) return Node_Id;
  1587.       --  Find actual that corresponds to a given a formal parameter. If the
  1588.       --  actuals are positional, return the next one, if any. If the actuals
  1589.       --  are named, scan the parameter associations to find the right one.
  1590.  
  1591.       procedure Set_Analyzed_Formal;
  1592.       --  Find the node in the generic copy that corresponds to a given formal.
  1593.       --  The semantic information on this node is used to perform legality
  1594.       --  checks on the actuals. Because semantic analysis can introduce some
  1595.       --  anonymous entities or modify the declaration node itself, the
  1596.       --  correspondence between the two lists is not one-one.
  1597.  
  1598.       ---------------------
  1599.       -- Matching_Actual --
  1600.       ---------------------
  1601.  
  1602.       function Matching_Actual (F : Entity_Id) return Node_Id is
  1603.          Found : Node_Id;
  1604.  
  1605.       begin
  1606.          --  End of list of purely positional parameters
  1607.  
  1608.          if No (Actual) then
  1609.             Found := Empty;
  1610.  
  1611.          --  Case of positional parameter correspond to current formal
  1612.  
  1613.          elsif No (Selector_Name (Actual)) then
  1614.             Found := Explicit_Generic_Actual_Parameter (Actual);
  1615.             Num_Matched := Num_Matched + 1;
  1616.             Actual := Next (Actual);
  1617.  
  1618.          --  Otherwise scan list of named actuals to find the one with the
  1619.          --  desired name. All remaining actuals have explicit names.
  1620.  
  1621.          else
  1622.             Found := Empty;
  1623.  
  1624.             while Present (Actual) loop
  1625.                if Chars (Selector_Name (Actual)) = Chars (F) then
  1626.                   Found := Explicit_Generic_Actual_Parameter (Actual);
  1627.                   Num_Matched := Num_Matched + 1;
  1628.                   exit;
  1629.                end if;
  1630.  
  1631.                Actual := Next (Actual);
  1632.             end loop;
  1633.  
  1634.             --  Reset for subsequent searches.
  1635.  
  1636.             Actual := First_Named;
  1637.          end if;
  1638.  
  1639.          return Found;
  1640.       end Matching_Actual;
  1641.  
  1642.       -------------------------
  1643.       -- Set_Analyzed_Formal --
  1644.       -------------------------
  1645.  
  1646.       procedure Set_Analyzed_Formal is
  1647.       begin
  1648.          while Present (Analyzed_Formal) loop
  1649.  
  1650.             case Nkind (Formal) is
  1651.  
  1652.                when N_Formal_Subprogram_Declaration =>
  1653.                   exit when Nkind (Analyzed_Formal)
  1654.                     = N_Formal_Subprogram_Declaration
  1655.                     and then Chars
  1656.                         (Defining_Unit_Name (Specification (Formal)))
  1657.                     = Chars
  1658.                         (Defining_Unit_Name (Specification (Analyzed_Formal)));
  1659.  
  1660.                when N_Formal_Package_Declaration =>
  1661.                   exit when
  1662.                     Nkind (Analyzed_Formal) = N_Formal_Package_Declaration
  1663.                     or else
  1664.                     Nkind (Analyzed_Formal) = N_Generic_Package_Declaration;
  1665.  
  1666.                when N_Use_Package_Clause | N_Use_Type_Clause => exit;
  1667.  
  1668.                when others =>
  1669.                   exit when
  1670.                     Nkind (Analyzed_Formal) /= N_Formal_Subprogram_Declaration
  1671.                       and then Nkind (Analyzed_Formal) /= N_Implicit_Types
  1672.                       and then Chars (Defining_Identifier (Formal)) =
  1673.                                Chars (Defining_Identifier (Analyzed_Formal));
  1674.             end case;
  1675.  
  1676.             Analyzed_Formal := Next (Analyzed_Formal);
  1677.          end loop;
  1678.  
  1679.       end Set_Analyzed_Formal;
  1680.  
  1681.    --  Start of processing for Analyze_Associations
  1682.  
  1683.    begin
  1684.       --  If named associations are present, save the first named association
  1685.       --  (it may of course be Empty) to facilitate subsequent name search.
  1686.  
  1687.       if Present (Actuals) then
  1688.          First_Named := First (Actuals);
  1689.  
  1690.          while Present (First_Named)
  1691.            and then No (Selector_Name (First_Named))
  1692.          loop
  1693.             Num_Actuals := Num_Actuals + 1;
  1694.             First_Named := Next (First_Named);
  1695.          end loop;
  1696.       end if;
  1697.  
  1698.       Named := First_Named;
  1699.       while Present (Named) loop
  1700.          if No (Selector_Name (Named)) then
  1701.             Error_Msg_N ("invalid positional actual after named one", Named);
  1702.             Abandon_Instantiation (Named);
  1703.          end if;
  1704.  
  1705.          Num_Actuals := Num_Actuals + 1;
  1706.          Named := Next (Named);
  1707.       end loop;
  1708.  
  1709.       if Present (Formals) then
  1710.          Formal := First (Formals);
  1711.          Analyzed_Formal := First (F_Copy);
  1712.  
  1713.          if Present (Actuals) then
  1714.             Actual := First (Actuals);
  1715.  
  1716.          --  All formals should have default values
  1717.  
  1718.          else
  1719.             Actual := Empty;
  1720.          end if;
  1721.  
  1722.          while Present (Formal) loop
  1723.             Set_Analyzed_Formal;
  1724.  
  1725.             case Nkind (Formal) is
  1726.                when N_Formal_Object_Declaration =>
  1727.                   Match := Matching_Actual (Defining_Identifier (Formal));
  1728.                   Append (Instantiate_Object (Formal, Match, Analyzed_Formal),
  1729.                     Assoc);
  1730.  
  1731.                when N_Formal_Type_Declaration =>
  1732.                   Match := Matching_Actual (Defining_Identifier (Formal));
  1733.                   if No (Match) then
  1734.                      Error_Msg_NE ("missing actual for instantiation of &",
  1735.                         Instantiation_Node, Defining_Identifier (Formal));
  1736.                      Abandon_Instantiation (Instantiation_Node);
  1737.  
  1738.                   else
  1739.                      Analyze (Match);
  1740.                      Append_To (Assoc,
  1741.                        Instantiate_Type (Formal, Match, Analyzed_Formal));
  1742.  
  1743.                      --  Even though the internal type appears as a subtype
  1744.                      --  of the actual, it inherits all operations and they
  1745.                      --  are immediately visible. This is equivalent to a use
  1746.                      --  type clause on  the actual.
  1747.  
  1748.                      if Is_First_Subtype (Entity (Match)) then
  1749.                         Append_To (Assoc,
  1750.                           Make_Use_Type_Clause (Sloc (Match),
  1751.                             Subtype_Marks => New_List (New_Occurrence_Of
  1752.                               (Base_Type (Entity (Match)), Sloc (Match)))));
  1753.                      end if;
  1754.                   end if;
  1755.  
  1756.                   --  A remote access-to-class-wide type must not be an
  1757.                   --  actual parameter for a generic formal (RM E.2.3(22))
  1758.  
  1759.                   Validate_Remote_Access_To_Class_Wide_Type (Match);
  1760.  
  1761.                when N_Formal_Subprogram_Declaration =>
  1762.                   Append_To (Assoc,
  1763.                     Instantiate_Formal_Subprogram
  1764.                       (Formal,
  1765.                        Matching_Actual
  1766.                          (Defining_Unit_Name (Specification (Formal))),
  1767.                        Analyzed_Formal));
  1768.  
  1769.                when N_Formal_Package_Declaration =>
  1770.                   Match := Matching_Actual (Defining_Identifier (Formal));
  1771.  
  1772.                   if No (Match) then
  1773.                      Error_Msg_NE
  1774.                        ("missing actual for instantiation of&",
  1775.                         Instantiation_Node,
  1776.                         Defining_Identifier (Formal));
  1777.  
  1778.                      Abandon_Instantiation (Instantiation_Node);
  1779.  
  1780.                   else
  1781.                      Analyze (Match);
  1782.                      Append
  1783.                        (Instantiate_Formal_Package
  1784.                          (Formal, Match, Analyzed_Formal),
  1785.                         Assoc);
  1786.  
  1787.                      --  If the formal is not declared with a box, reanalyze
  1788.                      --  it as an instantiation, to verify the matching rules
  1789.                      --  of 12.7. The actual checks are performed after the
  1790.                      --  generic associations have been analyzed.
  1791.  
  1792.                      if not Box_Present (Formal) then
  1793.                         declare
  1794.                            F_Pack : constant Entity_Id :=
  1795.                                       Make_Defining_Identifier (Sloc (Match),
  1796.                                         Chars => New_Internal_Name  ('P'));
  1797.                            Decl   : Node_Id;
  1798.  
  1799.                         begin
  1800.                            Decl :=
  1801.                              Make_Package_Instantiation (Sloc (Match),
  1802.                                Defining_Unit_Name => F_Pack,
  1803.                                Name => New_Occurrence_Of
  1804.                                  (Entity (Name (Formal)), Sloc (Match)),
  1805.                                Generic_Associations =>
  1806.                                  Generic_Associations (Formal));
  1807.                            Append (Decl, Assoc);
  1808.                         end;
  1809.                      end if;
  1810.                   end if;
  1811.  
  1812.                when N_Use_Package_Clause =>
  1813.                   Append (Copy_Generic_Node (Formal, Empty, True),  Assoc);
  1814.  
  1815.                when N_Use_Type_Clause =>
  1816.                   Append (Copy_Generic_Node (Formal, Empty, True),  Assoc);
  1817.  
  1818.                when others => pragma Assert (False); null;
  1819.  
  1820.             end case;
  1821.  
  1822.             Formal := Next (Formal);
  1823.             Analyzed_Formal := Next (Analyzed_Formal);
  1824.          end loop;
  1825.  
  1826.          if Num_Actuals > Num_Matched then
  1827.             Error_Msg_N
  1828.               ("unmatched actuals in instantiation", Instantiation_Node);
  1829.          end if;
  1830.  
  1831.       elsif Present (Actuals) then
  1832.          Error_Msg_N
  1833.            ("too many actuals in generic instantiation", Instantiation_Node);
  1834.       end if;
  1835.  
  1836.       return Assoc;
  1837.    end Analyze_Associations;
  1838.  
  1839.    -------------------------------
  1840.    -- Analyze_Formal_Array_Type --
  1841.    -------------------------------
  1842.  
  1843.    procedure Analyze_Formal_Array_Type
  1844.      (T   : in out Entity_Id;
  1845.       Def : Node_Id)
  1846.    is
  1847.       J : Node_Id;
  1848.  
  1849.    begin
  1850.       --  Treated like a non-generic array declaration, with
  1851.       --  additional semantic checks.
  1852.  
  1853.       Enter_Name (T);
  1854.  
  1855.       if Nkind (Def) = N_Constrained_Array_Definition then
  1856.          J := First (Discrete_Subtype_Definitions (Def));
  1857.  
  1858.          while Present (J) loop
  1859.             if Nkind (J) = N_Subtype_Indication
  1860.               or else Nkind (J) = N_Range
  1861.               or else Nkind (J) = N_Attribute_Reference
  1862.             then
  1863.                Error_Msg_N ("only a subtype mark is allowed in a formal", Def);
  1864.             end if;
  1865.  
  1866.             J := Next_Index (J);
  1867.          end loop;
  1868.       end if;
  1869.  
  1870.       Array_Type_Declaration (T, Def);
  1871.  
  1872.       if Is_Incomplete_Or_Private_Type (Component_Type (T))
  1873.         and then No (Full_View (Component_Type (T)))
  1874.         and then not Is_Generic_Type (Component_Type (T))
  1875.       then
  1876.          Error_Msg_N ("premature usage of incomplete type", Def);
  1877.  
  1878.       elsif Is_Internal (Component_Type (T)) then
  1879.          Error_Msg_N
  1880.            ("only a subtype mark is allowed in a formal", Def);
  1881.       end if;
  1882.  
  1883.    end Analyze_Formal_Array_Type;
  1884.  
  1885.    ----------------------------------------
  1886.    -- Analyze_Formal_Decimal_Fixed_Point --
  1887.    ----------------------------------------
  1888.  
  1889.    --  As for other generic types,  we create a valid type representation
  1890.    --  with legal but arbitrary attributes, whose values are never considered
  1891.    --  static. For all scalar types we introduce an anonymous base type, with
  1892.    --  the same attributes. We choose the corresponding integer type to be
  1893.    --  Standard_Integer.
  1894.  
  1895.    procedure Analyze_Formal_Decimal_Fixed_Point
  1896.      (T   : Entity_Id;
  1897.       Def : Node_Id)
  1898.    is
  1899.       Loc       : constant Source_Ptr := Sloc (Def);
  1900.       Base      : constant Entity_Id :=
  1901.                     New_Internal_Entity
  1902.                       (E_Decimal_Fixed_Point_Type,
  1903.                        Current_Scope, Sloc (Def), 'G');
  1904.       Int_Base  : constant Entity_Id := Standard_Integer;
  1905.       Delta_Val : constant Ureal := Ureal_1;
  1906.       Digs_Val  : constant Uint  := Uint_6;
  1907.  
  1908.    begin
  1909.       Note_Feature (Generic_Formal_Decimal_Types, Loc);
  1910.  
  1911.       Enter_Name (T);
  1912.  
  1913.       Set_Etype            (Base, Base);
  1914.       Set_Esize            (Base, Esize (Int_Base));
  1915.       Set_Alignment_Clause (Base, Alignment_Clause (Int_Base));
  1916.       Set_Digits_Value     (Base, Digs_Val);
  1917.       Set_Delta_Value      (Base, Delta_Val);
  1918.       Set_Small_Value      (Base, Delta_Val);
  1919.       Set_Scalar_Range     (Base, Scalar_Range (Int_Base));
  1920.  
  1921.       Set_Ekind            (T, E_Decimal_Fixed_Point_Subtype);
  1922.       Set_Etype            (T, Base);
  1923.       Set_Esize            (T, Esize (Int_Base));
  1924.       Set_Alignment_Clause (T, Alignment_Clause (Int_Base));
  1925.       Set_Digits_Value     (T, Digs_Val);
  1926.       Set_Delta_Value      (T, Delta_Val);
  1927.       Set_Small_Value      (T, Delta_Val);
  1928.       Set_Scalar_Range     (T, Scalar_Range (Int_Base));
  1929.  
  1930.    end Analyze_Formal_Decimal_Fixed_Point;
  1931.  
  1932.    ---------------------------------
  1933.    -- Analyze_Formal_Derived_Type --
  1934.    ---------------------------------
  1935.  
  1936.    procedure Analyze_Formal_Derived_Type
  1937.      (N   : Node_Id;
  1938.       T   : Entity_Id;
  1939.       Def : Node_Id)
  1940.    is
  1941.       Loc     : constant Source_Ptr := Sloc (Def);
  1942.       New_N   : Node_Id;
  1943.       New_Def : Node_Id;
  1944.  
  1945.    begin
  1946.       Note_Feature (Generic_Formal_Derived_Types, Loc);
  1947.       Set_Is_Generic_Type (T);
  1948.  
  1949.       if Private_Present (Def) then
  1950.          New_N :=
  1951.            Make_Private_Extension_Declaration (Loc,
  1952.              Defining_Identifier           => T,
  1953.              Discriminant_Specifications   => Discriminant_Specifications (N),
  1954.              Unknown_Discriminants_Present =>
  1955.                                             Unknown_Discriminants_Present (N),
  1956.              Subtype_Indication            => Subtype_Mark (Def));
  1957.  
  1958.          Set_Abstract_Present (New_N, Abstract_Present (Def));
  1959.  
  1960.       else
  1961.          New_N :=
  1962.            Make_Full_Type_Declaration (Loc,
  1963.              Defining_Identifier => T,
  1964.              Discriminant_Specifications =>
  1965.                Discriminant_Specifications (Parent (T)),
  1966.               Type_Definition =>
  1967.                 Make_Derived_Type_Definition (Loc,
  1968.                   Subtype_Indication => Subtype_Mark (Def)));
  1969.  
  1970.          Set_Abstract_Present
  1971.            (Type_Definition (New_N), Abstract_Present (Def));
  1972.       end if;
  1973.  
  1974.       Rewrite_Substitute_Tree (N,  New_N);
  1975.       Analyze (N);
  1976.    end Analyze_Formal_Derived_Type;
  1977.  
  1978.    ----------------------------------
  1979.    -- Analyze_Formal_Discrete_Type --
  1980.    ----------------------------------
  1981.  
  1982.    --  The operations defined for a discrete types are those of an
  1983.    --  enumeration type. The size is set to an arbitrary value, for use
  1984.    --  in analyzing the generic unit.
  1985.  
  1986.    procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
  1987.       Loc    : constant Source_Ptr := Sloc (Def);
  1988.       Bounds : Node_Id;
  1989.       Lo     : Node_Id;
  1990.       Hi     : Node_Id;
  1991.  
  1992.    begin
  1993.       Enter_Name (T);
  1994.       Set_Ekind (T, E_Enumeration_Type);
  1995.       Set_Etype (T, T);
  1996.       Set_Esize (T, Uint_0);
  1997.  
  1998.       --  For semantic analysis,  the bounds of the type must be set to some
  1999.       --  non-static value. The simplest is to create attribute nodes for
  2000.       --  those bounds, that refer to the type itself. These bounds are never
  2001.       --  analyzed but serve as place-holders.
  2002.  
  2003.       Lo :=
  2004.         Make_Attribute_Reference (Loc,
  2005.           Attribute_Name => Name_First,
  2006.           Prefix => New_Reference_To (T, Loc));
  2007.       Set_Etype (Lo, T);
  2008.  
  2009.       Hi :=
  2010.         Make_Attribute_Reference (Loc,
  2011.           Attribute_Name => Name_Last,
  2012.           Prefix => New_Reference_To (T, Loc));
  2013.       Set_Etype (Hi, T);
  2014.  
  2015.       Set_Scalar_Range (T,
  2016.         Make_Range (Loc,
  2017.           Low_Bound => Lo,
  2018.           High_Bound => Hi));
  2019.  
  2020.    end Analyze_Formal_Discrete_Type;
  2021.  
  2022.    ----------------------------------
  2023.    -- Analyze_Formal_Floating_Type --
  2024.    ---------------------------------
  2025.  
  2026.    procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
  2027.       --  the various semantic attributes are taken from the predefined type
  2028.       --  Float, just so that all of them are initialized. Their values are
  2029.       --  never used because no constant folding or expansion takes place in
  2030.       --  the generic itself.
  2031.  
  2032.       Base : constant Entity_Id :=
  2033.         New_Internal_Entity
  2034.           (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
  2035.  
  2036.    begin
  2037.       Enter_Name (T);
  2038.       Set_Ekind        (T, E_Floating_Point_Subtype);
  2039.       Set_Etype        (T, Base);
  2040.       Set_Esize        (T, Esize (Standard_Float));
  2041.       Set_Digits_Value (T, Digits_Value (Standard_Float));
  2042.       Set_Scalar_Range (T, Scalar_Range (Standard_Float));
  2043.  
  2044.       Set_Is_Generic_Type (Base);
  2045.       Set_Etype           (Base, Base);
  2046.       Set_Esize           (Base, Esize (Standard_Float));
  2047.       Set_Digits_Value    (Base, Digits_Value (Standard_Float));
  2048.       Set_Scalar_Range    (Base, Scalar_Range (Standard_Float));
  2049.    end Analyze_Formal_Floating_Type;
  2050.  
  2051.    ---------------------------------
  2052.    -- Analyze_Formal_Modular_Type --
  2053.    ---------------------------------
  2054.  
  2055.    procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
  2056.    begin
  2057.  
  2058.       --  Apart from their entity kind, generic modular types are treated
  2059.       --  like signed integer types, and have the same attributes.
  2060.  
  2061.       Analyze_Formal_Signed_Integer_Type (T, Def);
  2062.       Set_Ekind (T, E_Modular_Integer_Subtype);
  2063.       Set_Ekind (Etype (T), E_Modular_Integer_Type);
  2064.  
  2065.    end Analyze_Formal_Modular_Type;
  2066.  
  2067.    ---------------------------------------
  2068.    -- Analyze_Formal_Object_Declaration --
  2069.    ---------------------------------------
  2070.  
  2071.    procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
  2072.       E  : constant Node_Id := Expression (N);
  2073.       Id : Node_Id;
  2074.       K  : Entity_Kind;
  2075.       T  : Node_Id;
  2076.  
  2077.    begin
  2078.       --  Determine the mode of the formal object
  2079.  
  2080.       if Out_Present (N) then
  2081.          K := E_Generic_In_Out_Parameter;
  2082.  
  2083.          if not In_Present (N) then
  2084.             Error_Msg_N ("formal generic objects cannot have mode OUT", N);
  2085.          end if;
  2086.  
  2087.       else
  2088.          K := E_Generic_In_Parameter;
  2089.       end if;
  2090.  
  2091.       Find_Type (Subtype_Mark (N));
  2092.       T  := Entity (Subtype_Mark (N));
  2093.       Id := Defining_Identifier (N);
  2094.       Enter_Name (Id);
  2095.  
  2096.       if K = E_Generic_In_Parameter then
  2097.          if Is_Limited_Type (T) then
  2098.             Error_Msg_N
  2099.              ("generic formal of mode IN must not be of limited type", N);
  2100.          end if;
  2101.  
  2102.          if Present (E) then
  2103.  
  2104.             --  This is wrong, should set In_Default_Expression ???
  2105.  
  2106.             Analyze (E);
  2107.             Resolve (E, T);
  2108.  
  2109.          end if;
  2110.  
  2111.          Set_Ekind (Id, K);
  2112.          Set_Etype (Id, T);
  2113.  
  2114.       --  Case of generic IN OUT parameter.
  2115.  
  2116.       else
  2117.          --  If the formal has an unconstrained type, construct its
  2118.          --  actual subtype, as is done for subprogram formals. In this
  2119.          --  fashion, all its uses can refer to specific bounds.
  2120.  
  2121.          Set_Ekind (Id, K);
  2122.          Set_Etype (Id, T);
  2123.  
  2124.          if (Is_Array_Type (T)
  2125.               and then not Is_Constrained (T))
  2126.            or else
  2127.             (Ekind (T) = E_Record_Type
  2128.               and then Has_Discriminants (T))
  2129.          then
  2130.             declare
  2131.                Decl : Node_Id := Build_Actual_Subtype (T, Id);
  2132.  
  2133.             begin
  2134.                Insert_Before (N, Decl);
  2135.                Analyze (Decl);
  2136.                Set_Actual_Subtype (Id, Defining_Identifier (Decl));
  2137.             end;
  2138.          else
  2139.             Set_Actual_Subtype (Id, T);
  2140.          end if;
  2141.  
  2142.          if Present (E) then
  2143.             Error_Msg_N
  2144.              ("initialization not allowed for `IN OUT` formals", N);
  2145.          end if;
  2146.       end if;
  2147.  
  2148.    end Analyze_Formal_Object_Declaration;
  2149.  
  2150.    ----------------------------------------------
  2151.    -- Analyze_Formal_Ordinary_Fixed_Point_Type --
  2152.    ----------------------------------------------
  2153.  
  2154.    procedure Analyze_Formal_Ordinary_Fixed_Point_Type
  2155.      (T   : Entity_Id;
  2156.       Def : Node_Id)
  2157.    is
  2158.       Base : constant Entity_Id :=
  2159.         New_Internal_Entity
  2160.           (E_Ordinary_Fixed_Point_Type, Current_Scope, Sloc (Def), 'G');
  2161.    begin
  2162.       --  The semantic attributes are set for completeness only, their
  2163.       --  values will never be used, because all properties of the type are
  2164.       --  non-static.
  2165.  
  2166.       Enter_Name (T);
  2167.       Set_Ekind            (T, E_Ordinary_Fixed_Point_Subtype);
  2168.       Set_Etype            (T, Base);
  2169.       Set_Esize            (T, Esize (Standard_Integer));
  2170.       Set_Small_Value      (T, Ureal_1);
  2171.       Set_Delta_Value      (T, Ureal_1);
  2172.       Set_Scalar_Range     (T, Scalar_Range (Standard_Integer));
  2173.  
  2174.       Set_Is_Generic_Type (Base);
  2175.       Set_Etype           (Base, Base);
  2176.       Set_Esize           (Base, Esize (Standard_Integer));
  2177.       Set_Small_Value     (Base, Ureal_1);
  2178.       Set_Delta_Value     (Base, Ureal_1);
  2179.       Set_Scalar_Range    (Base, Scalar_Range (Standard_Integer));
  2180.    end Analyze_Formal_Ordinary_Fixed_Point_Type;
  2181.  
  2182.    ----------------------------
  2183.    -- Analyze_Formal_Package --
  2184.    ----------------------------
  2185.  
  2186.    procedure Analyze_Formal_Package (N : Node_Id) is
  2187.       Formal           : Entity_Id := Defining_Identifier (N);
  2188.       Gen_Id           : constant Node_Id   := Name (N);
  2189.       Gen_Decl         : Node_Id;
  2190.       Gen_Unit         : Entity_Id;
  2191.       New_N            : Node_Id;
  2192.       Parent_Installed : Boolean := False;
  2193.  
  2194.    begin
  2195.       Note_Feature (Generic_Formal_Packages, Sloc (N));
  2196.  
  2197.       Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
  2198.       Gen_Unit := Entity (Gen_Id);
  2199.  
  2200.       if Ekind (Gen_Unit) /= E_Generic_Package then
  2201.          Error_Msg_N ("expect generic package name", Gen_Id);
  2202.          return;
  2203.       end if;
  2204.  
  2205.       --  Check for a formal package that is a package renaming.
  2206.  
  2207.       if Present (Renamed_Object (Gen_Unit)) then
  2208.          Gen_Unit := Renamed_Object (Gen_Unit);
  2209.       end if;
  2210.  
  2211.       --  The formal package is treated like a regular instance, but only
  2212.       --  the specification needs to be instantiated, to make entities visible.
  2213.  
  2214.       if not Box_Present (N) then
  2215.          Analyze_Package_Instantiation (N);
  2216.  
  2217.       else
  2218.          --  If there are no generic associations, the generic parameters
  2219.          --  appear as local entities and are instantiated like them. We copy
  2220.          --  the generic package declaration as if it were an instantiation,
  2221.          --  and analyze it like a regular package, except that we treat the
  2222.          --  formals as additional visible components.
  2223.  
  2224.          Gen_Decl := Get_Declaration_Node (Gen_Unit);
  2225.          New_N := Copy_Generic_Node
  2226.                    (Original_Node (Gen_Decl), Empty, Instantiating => True);
  2227.          Set_Defining_Unit_Name (Specification (New_N), Formal);
  2228.          Rewrite_Substitute_Tree (N, New_N);
  2229.  
  2230.          Formal := Defining_Unit_Name (Specification (N));
  2231.  
  2232.          Enter_Name (Formal);
  2233.          Set_Ekind  (Formal, E_Generic_Package);
  2234.          Set_Etype  (Formal, Standard_Void_Type);
  2235.          New_Scope  (Formal);
  2236.  
  2237.          Analyze_Generic_Formal_Part (N);
  2238.          Analyze (Specification (N));
  2239.          End_Package_Scope (Formal);
  2240.  
  2241.          --  Inside the generic unit, the formal package is a regular
  2242.          --  package, but no body is needed for it. Note that after
  2243.          --  instantiation, the defining_unit_name we need is in the
  2244.          --  new tree and not in the original. (see Package_Instantiation).
  2245.          --  A generic formal package is an instance, and can be used as
  2246.          --  an actual for an inner instance. Mark its generic parent.
  2247.  
  2248.          Set_Ekind (Formal,  E_Package);
  2249.          Set_Generic_Parent (Specification (N), Gen_Unit);
  2250.          Set_Has_Completion (Formal, True);
  2251.       end if;
  2252.  
  2253.       if Parent_Installed then
  2254.          Remove_Parent;
  2255.       end if;
  2256.  
  2257.    end Analyze_Formal_Package;
  2258.  
  2259.    ---------------------------------
  2260.    -- Analyze_Formal_Private_Type --
  2261.    ---------------------------------
  2262.  
  2263.    procedure Analyze_Formal_Private_Type
  2264.      (N   : Node_Id;
  2265.       T   : Entity_Id;
  2266.       Def : Node_Id)
  2267.    is
  2268.    begin
  2269.       New_Private_Type (N, T, Def);
  2270.  
  2271.       --  Set the size to an arbitrary but legal value.
  2272.  
  2273.       Set_Esize (T, Esize (Standard_Integer));
  2274.    end Analyze_Formal_Private_Type;
  2275.  
  2276.    ----------------------------------------
  2277.    -- Analyze_Formal_Signed_Integer_Type --
  2278.    ----------------------------------------
  2279.  
  2280.    procedure Analyze_Formal_Signed_Integer_Type
  2281.      (T   : Entity_Id;
  2282.       Def : Node_Id)
  2283.    is
  2284.       Base : constant Entity_Id :=
  2285.         New_Internal_Entity
  2286.           (E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
  2287.  
  2288.    begin
  2289.       Enter_Name (T);
  2290.  
  2291.       Set_Ekind        (T, E_Signed_Integer_Subtype);
  2292.       Set_Etype        (T, Base);
  2293.       Set_Esize        (T, Esize (Standard_Integer));
  2294.       Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
  2295.  
  2296.       Set_Is_Generic_Type (Base);
  2297.       Set_Esize           (Base, Esize (Standard_Integer));
  2298.       Set_Etype           (Base, Base);
  2299.       Set_Scalar_Range    (Base, Scalar_Range (Standard_Integer));
  2300.    end Analyze_Formal_Signed_Integer_Type;
  2301.  
  2302.    -------------------------------
  2303.    -- Analyze_Formal_Subprogram --
  2304.    -------------------------------
  2305.  
  2306.    procedure Analyze_Formal_Subprogram (N : Node_Id) is
  2307.       Spec : constant Node_Id   := Specification (N);
  2308.       Def  : constant Node_Id   := Default_Name (N);
  2309.       Nam  : constant Entity_Id := Defining_Unit_Name (Spec);
  2310.       Subp : Entity_Id;
  2311.  
  2312.    begin
  2313.       Analyze_Subprogram_Declaration (N);
  2314.       Set_Has_Completion (Nam);
  2315.  
  2316.       --  Default name is resolved at the point of instantiation
  2317.  
  2318.       if Box_Present (N) then
  2319.          null;
  2320.  
  2321.       --  Else default is bound at the point of generic declaration
  2322.  
  2323.       elsif Present (Def) then
  2324.          if Nkind (Def) = N_Operator_Symbol then
  2325.             Find_Direct_Name (Def);
  2326.  
  2327.          elsif Nkind (Def) /= N_Attribute_Reference then
  2328.             Analyze (Def);
  2329.  
  2330.          else
  2331.             --  For an attribute reference, analyze the prefix. Whether the
  2332.             --  attribute is legal will be determined at instantiation time.
  2333.  
  2334.             Analyze (Prefix (Def));
  2335.             return;
  2336.          end if;
  2337.  
  2338.          --  Default name may be overloaded, in which case the interpretation
  2339.          --  with the correct profile must be  selected, as for a renaming.
  2340.  
  2341.          if Etype (Def) = Any_Type then
  2342.             return;
  2343.  
  2344.          elsif not Is_Overloadable (Entity (Def)) then
  2345.             Error_Msg_N ("expect valid subprogram name as default", Def);
  2346.             return;
  2347.  
  2348.          elsif not Is_Overloaded (Def) then
  2349.             Subp := Entity (Def);
  2350.  
  2351.             if Subp = Nam then
  2352.                Error_Msg_N ("premature usage of formal subprogram", Def);
  2353.  
  2354.             elsif not Entity_Matches_Spec (Subp, Nam) then
  2355.                Error_Msg_N ("no visible entity matches specification", Def);
  2356.             end if;
  2357.  
  2358.          else
  2359.             declare
  2360.                I    : Interp_Index;
  2361.                I1   : Interp_Index;
  2362.                It   : Interp;
  2363.                It1  : Interp;
  2364.  
  2365.             begin
  2366.                Subp := Any_Id;
  2367.                Get_First_Interp (Def, I, It);
  2368.  
  2369.                while Present (It.Nam) loop
  2370.  
  2371.                   if Entity_Matches_Spec (It.Nam, Nam) then
  2372.                      if Subp /= Any_Id then
  2373.                         It1 := Disambiguate (Def, I1, I, Etype (Subp));
  2374.  
  2375.                         if It1 = No_Interp then
  2376.                            Error_Msg_N ("ambiguous default subprogram",  Def);
  2377.                         else
  2378.                            Subp := It1.Nam;
  2379.                         end if;
  2380.  
  2381.                         exit;
  2382.  
  2383.                      else
  2384.                         I1  := I;
  2385.                         Subp := It.Nam;
  2386.                      end if;
  2387.                   end if;
  2388.  
  2389.                   Get_Next_Interp (I, It);
  2390.                end loop;
  2391.             end;
  2392.  
  2393.             if Subp /= Any_Id then
  2394.                Set_Entity (Def, Subp);
  2395.  
  2396.                if Subp = Nam then
  2397.                   Error_Msg_N ("premature usage of formal subprogram", Def);
  2398.  
  2399.                elsif Ekind (Subp) /= E_Operator then
  2400.                   Check_Mode_Conformant (Subp, Nam);
  2401.                end if;
  2402.  
  2403.             else
  2404.                Error_Msg_N ("no visible subprogram matches specification", N);
  2405.             end if;
  2406.          end if;
  2407.       end if;
  2408.    end Analyze_Formal_Subprogram;
  2409.  
  2410.    -------------------------------------
  2411.    -- Analyze_Formal_Type_Declaration --
  2412.    -------------------------------------
  2413.  
  2414.    procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
  2415.       Def : constant Node_Id := Formal_Type_Definition (N);
  2416.       T   : Entity_Id;
  2417.  
  2418.    begin
  2419.       T := Defining_Identifier (N);
  2420.  
  2421.       if Present (Discriminant_Specifications (N))
  2422.         and then Nkind (Def) /= N_Formal_Private_Type_Definition
  2423.       then
  2424.          Error_Msg_N
  2425.            ("discriminants not allowed for this formal type",
  2426.             Defining_Identifier (First (Discriminant_Specifications (N))));
  2427.       end if;
  2428.  
  2429.       --  Enter the new name, and branch to specific routine.
  2430.  
  2431.       case Nkind (Def) is
  2432.          when N_Formal_Private_Type_Definition
  2433.                         => Analyze_Formal_Private_Type (N, T, Def);
  2434.  
  2435.          when N_Formal_Derived_Type_Definition
  2436.                         => Analyze_Formal_Derived_Type (N, T, Def);
  2437.  
  2438.          when N_Formal_Discrete_Type_Definition
  2439.                         => Analyze_Formal_Discrete_Type (T, Def);
  2440.  
  2441.          when N_Formal_Signed_Integer_Type_Definition
  2442.                         => Analyze_Formal_Signed_Integer_Type (T, Def);
  2443.  
  2444.          when N_Formal_Modular_Type_Definition
  2445.                         => Analyze_Formal_Modular_Type (T, Def);
  2446.  
  2447.          when N_Formal_Floating_Point_Definition
  2448.                         => Analyze_Formal_Floating_Type (T, Def);
  2449.  
  2450.          when N_Formal_Ordinary_Fixed_Point_Definition
  2451.                         => Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
  2452.  
  2453.          when N_Formal_Decimal_Fixed_Point_Definition
  2454.                         => Analyze_Formal_Decimal_Fixed_Point (T, Def);
  2455.  
  2456.          when N_Array_Type_Definition
  2457.                         => Analyze_Formal_Array_Type (T, Def);
  2458.  
  2459.          when N_Access_To_Object_Definition |
  2460.               N_Access_Function_Definition  |
  2461.               N_Access_Procedure_Definition
  2462.                         => Analyze_Generic_Access_Type (T, Def);
  2463.  
  2464.          when others =>
  2465.             pragma Assert (False); null;
  2466.  
  2467.       end case;
  2468.  
  2469.       Set_Is_Generic_Type (T);
  2470.  
  2471.    end Analyze_Formal_Type_Declaration;
  2472.  
  2473.    ---------------------------------
  2474.    -- Analyze_Generic_Access_Type --
  2475.    ---------------------------------
  2476.  
  2477.    procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
  2478.    begin
  2479.       Enter_Name (T);
  2480.  
  2481.       if Nkind (Def) = N_Access_To_Object_Definition then
  2482.          Access_Type_Declaration (T, Def);
  2483.  
  2484.          if Is_Incomplete_Or_Private_Type (Designated_Type (T))
  2485.            and then No (Full_View (Designated_Type (T)))
  2486.            and then not Is_Generic_Type (Designated_Type (T))
  2487.          then
  2488.             Error_Msg_N ("premature usage of incomplete type", Def);
  2489.  
  2490.          elsif Is_Internal (Designated_Type (T)) then
  2491.             Error_Msg_N
  2492.               ("only a subtype mark is allowed in a formal", Def);
  2493.          end if;
  2494.  
  2495.       else
  2496.          Access_Subprogram_Declaration (T, Def);
  2497.       end if;
  2498.    end Analyze_Generic_Access_Type;
  2499.  
  2500.    ---------------------
  2501.    -- Associated_Node --
  2502.    ---------------------
  2503.  
  2504.    function Associated_Node (N : Node_Id) return Node_Id is
  2505.       Assoc : Node_Id := Node4 (N);
  2506.       --  ??? what is Node4 being used for here?
  2507.  
  2508.    begin
  2509.       if Nkind (Assoc) /= Nkind (N) then
  2510.          return Assoc;
  2511.       else
  2512.          --  If the node is part of an inner generic, it may itself have been
  2513.          --  remapped into a further generic copy. Node4 is otherwise used for
  2514.          --  the entity of the node, and will be of a different node kind, or
  2515.          --  else N has been rewritten as a literal or function call.
  2516.  
  2517.          while Present (Node4 (Assoc))
  2518.            and then Nkind (Node4 (Assoc)) = Nkind (Assoc)
  2519.          loop
  2520.             Assoc := Node4 (Assoc);
  2521.          end loop;
  2522.  
  2523.          --  Follow and additional link in case the final node was rewritten.
  2524.          --  This can only happen with nested generic units.
  2525.  
  2526.          if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
  2527.            and then Present (Node4 (Assoc))
  2528.            and then (Nkind (Node4 (Assoc)) = N_Function_Call
  2529.                        or else Nkind (Node4 (Assoc)) = N_Explicit_Dereference
  2530.                        or else Nkind (Node4 (Assoc)) = N_Integer_Literal
  2531.                        or else Nkind (Node4 (Assoc)) = N_Real_Literal
  2532.                        or else Nkind (Node4 (Assoc)) = N_String_Literal)
  2533.          then
  2534.             Assoc := Node4 (Assoc);
  2535.          end if;
  2536.  
  2537.          return Assoc;
  2538.       end if;
  2539.    end Associated_Node;
  2540.  
  2541.    ---------------------------------------------
  2542.    --  Build_Instance_Compilation_Unit_Nodes  --
  2543.    ---------------------------------------------
  2544.  
  2545.    procedure Build_Instance_Compilation_Unit_Nodes
  2546.      (N        : Node_Id;
  2547.       Act_Body : Node_Id;
  2548.       Act_Decl : Node_Id)
  2549.    is
  2550.       Decl_Cunit : Node_Id;
  2551.       Body_Cunit : Node_Id;
  2552.       Citem      : Node_Id;
  2553.  
  2554.    begin
  2555.       --  A new compilation unit node is built for the instance declaration
  2556.  
  2557.       Decl_Cunit := New_Node (N_Compilation_Unit, Sloc (N));
  2558.       Set_Context_Items (Decl_Cunit, Empty_List);
  2559.       Set_Unit          (Decl_Cunit, Act_Decl);
  2560.       Set_Parent_Spec   (Act_Decl, Parent_Spec (N));
  2561.       Set_Body_Required (Decl_Cunit, True);
  2562.  
  2563.       --  We use the original instantiation compilation unit as the resulting
  2564.       --  compilation unit of the instance, since this is the main unit.
  2565.  
  2566.       Rewrite_Substitute_Tree (N, Act_Body);
  2567.       Body_Cunit := Parent (N);
  2568.  
  2569.       --  The two compilation unit nodes are linked by the Library_Unit field
  2570.  
  2571.       Set_Library_Unit  (Decl_Cunit, Body_Cunit);
  2572.       Set_Library_Unit  (Body_Cunit, Decl_Cunit);
  2573.  
  2574.       --  The context clause items on the instantiation, which are now
  2575.       --  attached to the body compilation unit (since the body overwrote
  2576.       --  the orginal instantiation node), semantically belong on the spec,
  2577.       --  so copy them there. It's harmless to leave them on the body as well.
  2578.       --  In fact one could argue that they belong in both places.
  2579.  
  2580.       Citem := First (Context_Items (Body_Cunit));
  2581.       while Present (Citem) loop
  2582.          Append (New_Copy (Citem), Context_Items (Decl_Cunit));
  2583.          Citem := Next (Citem);
  2584.       end loop;
  2585.  
  2586.       --  Make entry in Units table, so that binder can generate call to
  2587.       --  elaboration procedure for body, if any.
  2588.  
  2589.       Make_Instance_Unit (Body_Cunit);
  2590.  
  2591.    end Build_Instance_Compilation_Unit_Nodes;
  2592.  
  2593.    ---------------------------
  2594.    -- Check_Generic_Actuals --
  2595.    ---------------------------
  2596.  
  2597.    --  The visibility of the actuals may be different between the
  2598.    --  point of generic instantiation and the instantiation of the body.
  2599.  
  2600.    procedure Check_Generic_Actuals (Instance : Entity_Id) is
  2601.       E : Entity_Id;
  2602.  
  2603.    begin
  2604.       E := First_Entity (Instance);
  2605.  
  2606.       while Present (E) loop
  2607.          if Nkind (Parent (E)) = N_Subtype_Declaration
  2608.            and then Scope (Etype (E)) /= Instance
  2609.            and then Is_Entity_Name (Subtype_Indication (Parent (E)))
  2610.          then
  2611.             Check_Private_View (Subtype_Indication (Parent (E)));
  2612.             Set_Is_Generic_Actual_Type (E, True);
  2613.             Set_Is_Private (E, False);
  2614.  
  2615.          elsif Ekind (E) = E_Package then
  2616.  
  2617.             --  If this is the renaming for the current instance, we're done.
  2618.             --  Otherwise it is a formal package. If the corresponding formal
  2619.             --  was declared with a box, the (instantiations of the) generic
  2620.             --  formal part are also visible. Otherwise, ignore the entity
  2621.             --  created to validate the actuals.
  2622.  
  2623.             if Renamed_Object (E) = Instance then
  2624.                exit;
  2625.  
  2626.             elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
  2627.                null;
  2628.  
  2629.             elsif Box_Present (Parent (Associated_Formal_Package (E))) then
  2630.                Check_Generic_Actuals (Renamed_Object (E));
  2631.             end if;
  2632.          end if;
  2633.  
  2634.          E := Next_Entity (E);
  2635.       end loop;
  2636.  
  2637.    end Check_Generic_Actuals;
  2638.  
  2639.    ------------------------
  2640.    -- Check_Private_View --
  2641.    ------------------------
  2642.  
  2643.    procedure Check_Private_View (N : Node_Id) is
  2644.       T : constant Entity_Id := Etype (N);
  2645.  
  2646.    begin
  2647.  
  2648.       if Present (T) then
  2649.          if Is_Private_Type (T)
  2650.            and then not Has_Private_View (N)
  2651.            and then Present (Full_View (T))
  2652.          then
  2653.             --  In the generic, the full type was visible. Save the
  2654.             --  private entity, for subsequent exchange.
  2655.  
  2656.             Append_Elmt (Full_View (T), Exchanged_Views);
  2657.             if Base_Type (T) /= T
  2658.               and then Is_Private_Type (Base_Type (T))
  2659.             then
  2660.                Append_Elmt (Full_View (Base_Type (T)), Exchanged_Views);
  2661.                Exchange_Declarations (Base_Type (T));
  2662.             end if;
  2663.  
  2664.             Exchange_Declarations (T);
  2665.  
  2666.          elsif Has_Private_View (N)
  2667.            and then not Is_Private_Type (T)
  2668.          then
  2669.             --  Only the private declaration was visible in the generic.
  2670.  
  2671.             Append_Elmt (T, Exchanged_Views);
  2672.             Exchange_Declarations (Etype (Associated_Node (N)));
  2673.          end if;
  2674.       end if;
  2675.    end Check_Private_View;
  2676.  
  2677.    -----------------------
  2678.    -- Copy_Generic_Node --
  2679.    -----------------------
  2680.  
  2681.    function Copy_Generic_Node
  2682.      (N             : Node_Id;
  2683.       Parent_Id     : Node_Id;
  2684.       Instantiating : Boolean)
  2685.       return          Node_Id
  2686.    is
  2687.       New_N : Node_Id;
  2688.  
  2689.       function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
  2690.       --  Check the given value of one of the Fields referenced by the
  2691.       --  current node to determine whether to copy it recursively. The
  2692.       --  field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
  2693.       --  value (Sloc, Uint, Char) in which case it need not be copied.
  2694.  
  2695.       function Copy_Generic_List
  2696.         (L         : List_Id;
  2697.          Parent_Id : Node_Id)
  2698.          return List_Id;
  2699.       --  Apply Copy_Node recursively to the members of a node list.
  2700.  
  2701.       function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
  2702.       --  Make copy of element list.
  2703.  
  2704.       -----------------------------
  2705.       -- Copy_Generic_Descendant --
  2706.       -----------------------------
  2707.  
  2708.       function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
  2709.       begin
  2710.          if D in Node_Range then
  2711.             if D = Union_Id (Empty) then
  2712.                return D;
  2713.             else
  2714.                return Union_Id (Copy_Generic_Node
  2715.                                (Node_Id (D), New_N, Instantiating));
  2716.             end if;
  2717.  
  2718.          elsif D in List_Range then
  2719.             if D = Union_Id (No_List) then
  2720.                return Union_Id (D);
  2721.             else
  2722.                return Union_Id (Copy_Generic_List (List_Id (D), New_N));
  2723.             end if;
  2724.  
  2725.          elsif D in Elist_Range then
  2726.             if D = Union_Id (No_Elist) then
  2727.                return Union_Id (D);
  2728.             else
  2729.                return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
  2730.             end if;
  2731.  
  2732.          else
  2733.             --  Field is not Id of copyable structure: return as is
  2734.  
  2735.             return D;
  2736.          end if;
  2737.       end Copy_Generic_Descendant;
  2738.  
  2739.       -----------------------
  2740.       -- Copy_Generic_List --
  2741.       -----------------------
  2742.  
  2743.       function Copy_Generic_List
  2744.         (L         : List_Id;
  2745.          Parent_Id : Node_Id)
  2746.          return      List_Id
  2747.       is
  2748.          N      : Node_Id;
  2749.          New_L  : List_Id := New_List;
  2750.  
  2751.       begin
  2752.          Set_Parent (New_L, Parent_Id);
  2753.          N := First (L);
  2754.  
  2755.          while Present (N) loop
  2756.             Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
  2757.             N := Next (N);
  2758.          end loop;
  2759.  
  2760.          return New_L;
  2761.       end Copy_Generic_List;
  2762.  
  2763.       ------------------------
  2764.       -- Copy_Generic_Elist --
  2765.       ------------------------
  2766.  
  2767.       function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
  2768.          M : Elmt_Id;
  2769.          L : Elist_Id := New_Elmt_List;
  2770.  
  2771.       begin
  2772.          M := First_Elmt (E);
  2773.  
  2774.          while Present (M) loop
  2775.             Append_Elmt
  2776.               (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
  2777.             M := Next_Elmt (M);
  2778.          end loop;
  2779.  
  2780.          return L;
  2781.       end Copy_Generic_Elist;
  2782.  
  2783.    --  Start of processing for Copy_Generic_Node
  2784.  
  2785.    begin
  2786.       if N = Empty then
  2787.          return N;
  2788.       end if;
  2789.  
  2790.       New_N := New_Copy (N);
  2791.  
  2792.       if not Is_List_Member (N) then
  2793.          Set_Parent (New_N, Parent_Id);
  2794.       end if;
  2795.  
  2796.       --  If defining identifier, then all fields have been copied already
  2797.  
  2798.       if Nkind (New_N) in N_Entity then
  2799.          null;
  2800.  
  2801.       elsif    (Nkind (New_N) = N_Identifier
  2802.         or else Nkind (New_N) = N_Character_Literal
  2803.         or else Nkind (New_N) = N_Expanded_Name
  2804.         or else Nkind (New_N) = N_Operator_Symbol
  2805.         or else Nkind (New_N) in N_Op)
  2806.       then
  2807.          if not Instantiating then
  2808.  
  2809.             --  Link both nodes in order to assign subsequently the
  2810.             --  entity of the copy to the original node, in case this
  2811.             --  is a global reference.
  2812.  
  2813.             Set_Associated_Node (N, New_N);
  2814.  
  2815.             --  If we are within an instantiation, this is a nested generic
  2816.             --  that has already been analyzed at the point of definition. We
  2817.             --  must preserve references that were global to the enclosing
  2818.             --  parent at that point. Other occurrences, whether global or
  2819.             --  local to the current generic, must be resolved anew, so we
  2820.             --  reset the entity in the generic copy. A global reference has
  2821.             --  a smaller depth than the parent, or else the same depth in
  2822.             --  case both are distinct compilation units.
  2823.             --  It is also possible for Current_Instantiated_Parent to be
  2824.             --  defined, and for this not to be a nested generic, namely
  2825.             --  if the unit is loaded through Rtsfind. In that case, the
  2826.             --  entity of New_N is only a link to the associated node, and
  2827.             --  not a defining occurrence.
  2828.  
  2829.             if No (Current_Instantiated_Parent)
  2830.               or else  No (Entity (New_N))
  2831.               or else
  2832.                 not (Nkind (Entity (New_N)) = N_Defining_Identifier
  2833.                   or Nkind (Entity (New_N)) = N_Defining_Character_Literal
  2834.                   or Nkind (Entity (New_N)) = N_Defining_Operator_Symbol)
  2835.               or else No (Scope (Entity (New_N)))
  2836.               or else Scope (Entity (New_N)) = Current_Instantiated_Parent
  2837.               or else (Scope_Depth (Scope (Entity (New_N))) >
  2838.                       Scope_Depth (Current_Instantiated_Parent)
  2839.                 and then Get_Sloc_Unit_Number (Sloc (New_N))
  2840.                  = Get_Sloc_Unit_Number (Sloc (Current_Instantiated_Parent)))
  2841.  
  2842.             then
  2843.                Set_Associated_Node (New_N, Empty);
  2844.             end if;
  2845.  
  2846.          else
  2847.             --  If the associated node is still defined, the entity in
  2848.             --  it is global, and must be copied to the instance.
  2849.  
  2850.             if Present (Associated_Node (N)) then
  2851.                if Nkind (Associated_Node (N)) = Nkind (N) then
  2852.                   Set_Entity (New_N, Entity (Associated_Node (N)));
  2853.  
  2854.                   Check_Private_View (N);
  2855.  
  2856.                elsif Nkind (Associated_Node (N)) = N_Function_Call then
  2857.  
  2858.                   --  Name resolves to a call to parameterless function.
  2859.  
  2860.                   Set_Entity (New_N, Entity (Name (Associated_Node (N))));
  2861.  
  2862.                else
  2863.                   Set_Entity (New_N, Empty);
  2864.                end if;
  2865.             end if;
  2866.  
  2867.          end if;
  2868.  
  2869.          if Nkind (N) = N_Expanded_Name
  2870.            or else Nkind (N) in N_Op
  2871.          then
  2872.             --  Complete the copy of remaining descendants.
  2873.  
  2874.             Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
  2875.             Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
  2876.          end if;
  2877.  
  2878.       else
  2879.          --  For all remaining nodes, copy recursively their descendants.
  2880.  
  2881.          Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
  2882.          Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
  2883.          Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
  2884.          Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
  2885.          Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
  2886.  
  2887.          if (Nkind (N) = N_Package_Body_Stub
  2888.               or else Nkind (N) = N_Protected_Body_Stub
  2889.               or else Nkind (N) = N_Subprogram_Body_Stub
  2890.               or else Nkind (N) = N_Task_Body_Stub)
  2891.            and then not Instantiating
  2892.          then
  2893.             --  Subunits of generic units must be loaded and analyzed at the
  2894.             --  point the stubs occur. A more permissive system might defer
  2895.             --  this analysis to the point of instantiation, but this seems
  2896.             --  too complicated for now.
  2897.  
  2898.             declare
  2899.                Context      : List_Id;
  2900.                Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
  2901.                Subunit      : Node_Id;
  2902.                New_Subunit  : Node_Id;
  2903.                Parent_Unit  : Node_Id;
  2904.                Unum         : Unit_Number_Type;
  2905.                New_Body     : Node_Id;
  2906.                Lib          : Node_Id;
  2907.  
  2908.             begin
  2909.                Unum := Load_Unit (Subunit_Name, True, N);
  2910.                Subunit :=  Cunit (Unum);
  2911.  
  2912.                --  We must create a generic copy of the subunit, in order
  2913.                --  to perform semantic analysis on it, and we must replace
  2914.                --  the stub in the original generic unit with the subunit,
  2915.                --  in order to preserve non-local references within.
  2916.  
  2917.                --  Only the proper body needs to be copied. Library_Unit and
  2918.                --  context clause are simply inherited by the generic copy.
  2919.                --  Note that the copy (which may be recursive if there are
  2920.                --  nested subunits) must be done first, before attaching it
  2921.                --  to the enclosing generic.
  2922.  
  2923.                New_Body := Copy_Generic_Node (Proper_Body (Unit (Subunit)),
  2924.                                            Empty, Instantiating => False);
  2925.  
  2926.                --  Now place the original proper body in the original
  2927.                --  generic unit.
  2928.  
  2929.                Rewrite_Substitute_Tree (N, Proper_Body (Unit (Subunit)));
  2930.  
  2931.                --  Finally replace the body of the subunit with its copy,
  2932.                --  and make this new subunit into the library unit of the
  2933.                --  generic copy, which does not have stubs any longer.
  2934.  
  2935.                Set_Proper_Body (Unit (Subunit), New_Body);
  2936.                Set_Library_Unit (New_N, Subunit);
  2937.                Inherit_Context (Unit (Subunit), N);
  2938.  
  2939.             end;
  2940.  
  2941.          end if;
  2942.       end if;
  2943.  
  2944.       return New_N;
  2945.    end Copy_Generic_Node;
  2946.  
  2947.    ------------------------------
  2948.    -- Check_Generic_Child_Unit --
  2949.    ------------------------------
  2950.  
  2951.    procedure Check_Generic_Child_Unit
  2952.      (Gen_Id           : Node_Id;
  2953.       Parent_Installed : in out Boolean)
  2954.    is
  2955.       Gen_Par : Entity_Id;
  2956.       E       : Entity_Id;
  2957.       S       : Node_Id;
  2958.       Found   : Boolean;
  2959.  
  2960.    begin
  2961.       --  If the name of the generic is given by a selected component, it
  2962.       --  may be the name of a generic child unit, and the prefix the name
  2963.       --  of an instance of the parent, in which case the child unit must
  2964.       --  be visible. If the instance is not in scope,  it must be placed there
  2965.       --  and removed after instantiation.
  2966.  
  2967.       if Nkind (Gen_Id) = N_Selected_Component then
  2968.          S := Selector_Name (Gen_Id);
  2969.          Analyze (Prefix (Gen_Id));
  2970.          Gen_Par := Entity (Prefix (Gen_Id));
  2971.  
  2972.          if Ekind (Gen_Par) = E_Package
  2973.            and then Nkind (Parent (Gen_Par)) = N_Package_Specification
  2974.            and then Present (Generic_Parent (Parent (Gen_Par)))
  2975.          then
  2976.             --  The prefix denotes an instantiation. The entity itself
  2977.             --  may be a nested generic, or a child unit.
  2978.  
  2979.             E := First_Entity (Generic_Parent (Parent (Gen_Par)));
  2980.             Found := False;
  2981.  
  2982.             while Present (E) loop
  2983.                if Chars (E) = Chars (S) then
  2984.                   Found := True;
  2985.                   exit;
  2986.                end if;
  2987.  
  2988.                E := Next_Entity (E);
  2989.             end loop;
  2990.  
  2991.             if Found
  2992.               and then Is_Child_Unit (E)
  2993.             then
  2994.                Change_Selected_Component_To_Expanded_Name (Gen_Id);
  2995.                Set_Entity (Gen_Id, E);
  2996.                Set_Etype (Gen_Id, Etype (E));
  2997.                Set_Entity (S, E);
  2998.                Set_Etype (S, Etype (E));
  2999.  
  3000.                if not In_Open_Scopes (Gen_Par) then
  3001.                   Install_Parent (Gen_Par);
  3002.                   Parent_Installed := True;
  3003.                end if;
  3004.  
  3005.             else
  3006.                --  If the generic parent does not contain an entity that
  3007.                --  corresponds to the selector, the instance doesn't either.
  3008.                --  Analyzing the node will yield the appropriate error message.
  3009.                --  If the entity is not a child unit, then it is an inner
  3010.                --  generic in the parent.
  3011.  
  3012.                Analyze (Gen_Id);
  3013.             end if;
  3014.  
  3015.          else
  3016.             Analyze (Gen_Id);
  3017.          end if;
  3018.  
  3019.       else
  3020.          Analyze (Gen_Id);
  3021.       end if;
  3022.    end Check_Generic_Child_Unit;
  3023.  
  3024.    ---------------------
  3025.    -- Get_Instance_Of --
  3026.    ---------------------
  3027.  
  3028.    function Get_Instance_Of (A : Entity_Id) return Entity_Id is
  3029.    begin
  3030.       for J in 0 .. Generic_Renamings.Last - 1 loop
  3031.          if Chars (A) = Chars (Generic_Renamings.Table (J).Gen_Id) then
  3032.             return Generic_Renamings.Table (J).Act_Id;
  3033.          end if;
  3034.       end loop;
  3035.  
  3036.       --  On exit, entity is not instantiated: not a generic parameter,
  3037.       --  or else parameter of an inner generic unit.
  3038.  
  3039.       return A;
  3040.    end Get_Instance_Of;
  3041.  
  3042.    ------------------------
  3043.    -- Instantiate_Object --
  3044.    ------------------------
  3045.  
  3046.    function Instantiate_Object
  3047.      (Formal : Node_Id;
  3048.       Actual : Node_Id;
  3049.       Analyzed_Formal : Node_Id)
  3050.       return   Node_Id
  3051.    is
  3052.       Formal_Id : constant Entity_Id  := Defining_Identifier (Formal);
  3053.       Type_Id   : constant Node_Id    := Subtype_Mark (Formal);
  3054.       Loc       : constant Source_Ptr := Sloc (Actual);
  3055.       Decl_Node : Node_Id;
  3056.  
  3057.    begin
  3058.       if Get_Instance_Of (Formal_Id) /= Formal_Id then
  3059.          Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
  3060.       end if;
  3061.  
  3062.       if Out_Present (Formal) then
  3063.  
  3064.          if No (Actual) then
  3065.             Error_Msg_NE
  3066.               ("missing actual for instantiation of &",
  3067.                Instantiation_Node, Formal_Id);
  3068.             Abandon_Instantiation (Instantiation_Node);
  3069.          end if;
  3070.  
  3071.          --  An IN OUT generic actual must be a name. The instantiation
  3072.          --  is a renaming declaration.
  3073.  
  3074.          Analyze (Actual);
  3075.          Decl_Node :=
  3076.            Make_Object_Renaming_Declaration (Loc,
  3077.              Defining_Identifier => New_Copy (Formal_Id),
  3078.              Subtype_Mark        => New_Copy (Type_Id),
  3079.              Name                => New_Copy (Actual));
  3080.  
  3081.          --  The following check is not entirely correct for the
  3082.          --  (very rare) case of an overloaded actual.  ???
  3083.  
  3084.          if not Is_Variable (Actual) or else Paren_Count (Actual) > 0 then
  3085.             Error_Msg_NE
  3086.               ("actual for& must be a variable", Actual, Formal_Id);
  3087.          end if;
  3088.  
  3089.          --  Propagate interpretations, in case actual is overloaded.
  3090.  
  3091.          Save_Interps (Actual, Name (Decl_Node));
  3092.  
  3093.       else
  3094.          --  The instantiation of a generic formal in-parameter
  3095.          --  is a constant declaration.
  3096.  
  3097.          if Present (Actual) then
  3098.             Analyze (Actual);
  3099.             Decl_Node := Make_Object_Declaration (Loc,
  3100.               Defining_Identifier => New_Copy (Formal_Id),
  3101.               Constant_Present => True,
  3102.               Object_Definition => New_Copy (Type_Id),
  3103.               Expression => New_Copy (Actual));
  3104.  
  3105.             --  Propagate interpretations, in case actual is overloaded.
  3106.  
  3107.             Save_Interps (Actual, Expression (Decl_Node));
  3108.             Freeze_Before (Instantiation_Node, Etype (Expression (Decl_Node)));
  3109.  
  3110.          elsif Present (Expression (Formal)) then
  3111.  
  3112.             --  Use default to construct declaration.
  3113.  
  3114.             Decl_Node := Make_Object_Declaration (Loc,
  3115.               Defining_Identifier => New_Copy (Formal_Id),
  3116.               Constant_Present => True,
  3117.               Object_Definition => New_Copy (Type_Id),
  3118.               Expression => New_Copy (Expression (Formal)));
  3119.          else
  3120.             Error_Msg_NE
  3121.               ("missing actual for instantiation of &",
  3122.                Instantiation_Node, Formal_Id);
  3123.             Abandon_Instantiation (Instantiation_Node);
  3124.          end if;
  3125.  
  3126.       end if;
  3127.  
  3128.       return Decl_Node;
  3129.    end Instantiate_Object;
  3130.  
  3131.    --------------------------------
  3132.    -- Instantiate_Formal_Package --
  3133.    --------------------------------
  3134.  
  3135.    function Instantiate_Formal_Package
  3136.      (Formal          : Node_Id;
  3137.       Actual          : Node_Id;
  3138.       Analyzed_Formal : Node_Id)
  3139.       return   Node_Id
  3140.    is
  3141.       Act_Pkg     : Entity_Id;
  3142.       Formal_Pack : Entity_Id;
  3143.       Loc         : constant Source_Ptr := Sloc (Actual);
  3144.       Nod         : Node_Id;
  3145.  
  3146.    begin
  3147.       Analyze (Actual);
  3148.  
  3149.       if not Is_Entity_Name (Actual)
  3150.         or else  Ekind (Entity (Actual)) /= E_Package
  3151.       then
  3152.          Error_Msg_N
  3153.            ("expect package instance to instantiate formal", Actual);
  3154.          Abandon_Instantiation (Actual);
  3155.  
  3156.       else
  3157.          Act_Pkg := Entity (Actual);
  3158.  
  3159.          --  The actual may be a renamed package,  or an outer generic
  3160.          --  formal package whose instantiation is converted into a renaming.
  3161.  
  3162.          if Present (Renamed_Object (Act_Pkg)) then
  3163.             Act_Pkg := Renamed_Object (Act_Pkg);
  3164.          end if;
  3165.  
  3166.          if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
  3167.             Formal_Pack := Entity (Name (Formal));
  3168.          else
  3169.             Formal_Pack :=
  3170.               Generic_Parent (Specification (Analyzed_Formal));
  3171.          end if;
  3172.  
  3173.          if Generic_Parent (Parent (Act_Pkg)) /= Formal_Pack  then
  3174.             Error_Msg_N
  3175.               ("expect package instance to instantiate formal", Actual);
  3176.             Abandon_Instantiation (Actual);
  3177.          end if;
  3178.  
  3179.          Set_Instance_Of (Defining_Identifier (Formal), Act_Pkg);
  3180.  
  3181.          --  If the formal F has a box, then the generic declarations are
  3182.          --  visible in the generic G. In an instance of G, the corresponding
  3183.          --  entities in the actual for F (which are the actuals for the
  3184.          --  instantiation of the generic that F denotes) must also be made
  3185.          --  visible for analysis of the current instance. On exit from the
  3186.          --  current instance, those entities are made private again. If the
  3187.          --  actual is currently in use, these entities are also use-visible.
  3188.  
  3189.          if Box_Present (Formal) then
  3190.             declare
  3191.                E : Entity_Id := First_Entity (Act_Pkg);
  3192.  
  3193.             begin
  3194.                while Present (E)
  3195.                  and then E /= First_Private_Entity (Act_Pkg)
  3196.                loop
  3197.                   Set_Is_Private (E, False);
  3198.                   Set_Is_Potentially_Use_Visible (E, In_Use (Act_Pkg));
  3199.                   E := Next_Entity (E);
  3200.                end loop;
  3201.             end;
  3202.          end if;
  3203.  
  3204.          Nod :=
  3205.            Make_Package_Renaming_Declaration (Loc,
  3206.              Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
  3207.              Name               => New_Reference_To (Act_Pkg, Loc));
  3208.  
  3209.             Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
  3210.               Defining_Identifier (Formal));
  3211.          return Nod;
  3212.       end if;
  3213.  
  3214.    end Instantiate_Formal_Package;
  3215.  
  3216.    ---------------------------
  3217.    -- Check_Formal_Packages --
  3218.    ---------------------------
  3219.  
  3220.    procedure Check_Formal_Packages (P_Id : Entity_Id) is
  3221.       E        : Entity_Id;
  3222.       Formal_P : Entity_Id;
  3223.  
  3224.    begin
  3225.       E := First_Entity (P_Id);
  3226.       while Present (E) loop
  3227.          if Ekind (E) = E_Package then
  3228.             if Renamed_Object (E) = P_Id then
  3229.                exit;
  3230.  
  3231.             elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
  3232.                null;
  3233.  
  3234.             elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
  3235.                Formal_P := Next_Entity (E);
  3236.                Check_Formal_Package_Instance
  3237.                  (Parent (Formal_P), Formal_P, E);
  3238.             end if;
  3239.          end if;
  3240.  
  3241.          E := Next_Entity (E);
  3242.       end loop;
  3243.    end Check_Formal_Packages;
  3244.  
  3245.    -----------------------------------
  3246.    -- Check_Formal_Package_Instance --
  3247.    -----------------------------------
  3248.  
  3249.    --  If the formal has specific parameters, they must match those of the
  3250.    --  actual. both of them are instances, and the renaming declarations
  3251.    --  for their formal parameters appear in the same order in both.
  3252.    --  The analyzed formal has been analyzed in the context of the current
  3253.    --  instance.
  3254.  
  3255.    procedure Check_Formal_Package_Instance
  3256.      (Actual   : Node_Id;
  3257.       Form_Pkg : Entity_Id;
  3258.       Act_Pkg  : Entity_Id)
  3259.    is
  3260.       E1 : Entity_Id := First_Entity (Act_Pkg);
  3261.       E2 : Entity_Id := First_Entity (Form_Pkg);
  3262.       Expr1       : Node_Id;
  3263.       Expr2       : Node_Id;
  3264.  
  3265.       procedure Check_Mismatch (B : Boolean);
  3266.  
  3267.       --  Common error routine for mismatch between the parameters of
  3268.       --  the actual instance and those of the formal package.
  3269.  
  3270.       procedure Check_Mismatch (B : Boolean) is
  3271.       begin
  3272.          if B then
  3273.             Error_Msg_NE (
  3274.               "actual for & in actual instance does not match formal",
  3275.               Actual, E1);
  3276.          end if;
  3277.       end Check_Mismatch;
  3278.  
  3279.    --  Start of processing for Check_Formal_Package_Instance
  3280.  
  3281.    begin
  3282.       while Present (E1)
  3283.         and then Present (E2)
  3284.       loop
  3285.          exit when Ekind (E1) = E_Package
  3286.            and then Renamed_Entity (E1) = Renamed_Entity (Act_Pkg);
  3287.  
  3288.          if Is_Type (E1) then
  3289.  
  3290.             --  Subtypes must statically match. E1 and E2 are the
  3291.             --  local entities that are subtypes of the actuals.
  3292.  
  3293.             Check_Mismatch
  3294.               (not Is_Type (E2)
  3295.                 or else not Subtypes_Statically_Match
  3296.                 (Etype (E1), (Etype (E2))));
  3297.  
  3298.          elsif Ekind (E1) = E_Constant then
  3299.  
  3300.             --  IN parameters must denote the same static value, or
  3301.             --  the same constant, or the literal null.
  3302.  
  3303.             Expr1 := Expression (Parent (E1));
  3304.  
  3305.             if Ekind (E2) /= E_Constant then
  3306.                Check_Mismatch (True);
  3307.             else
  3308.                Expr2 := Expression (Parent (E2));
  3309.             end if;
  3310.  
  3311.             if Is_Static_Expression (Expr1) then
  3312.                if not Is_Static_Expression (Expr2) then
  3313.                   Check_Mismatch (True);
  3314.  
  3315.                elsif Is_Integer_Type (Etype (E1)) then
  3316.  
  3317.                   declare
  3318.                      V1 : Uint := Expr_Value (Expr1);
  3319.                      V2 : Uint := Expr_Value (Expr2);
  3320.                   begin
  3321.                      Check_Mismatch (V1 /= V2);
  3322.                   end;
  3323.  
  3324.                elsif Is_Real_Type (Etype (E1)) then
  3325.  
  3326.                   declare
  3327.                      V1 : Ureal := Expr_Value_R (Expr1);
  3328.                      V2 : Ureal := Expr_Value_R (Expr2);
  3329.                   begin
  3330.                      Check_Mismatch (V1 /= V2);
  3331.                   end;
  3332.  
  3333.                elsif Is_String_Type (Etype (E1))
  3334.                  and then Nkind (Expr1) = N_String_Literal
  3335.                then
  3336.  
  3337.                   if Nkind (Expr2) /= N_String_Literal then
  3338.                      Check_Mismatch (True);
  3339.                   else
  3340.                      Check_Mismatch
  3341.                        (String_Equal (Strval (Expr1), Strval (Expr2)));
  3342.                   end if;
  3343.                end if;
  3344.  
  3345.             elsif Is_Entity_Name (Expr1) then
  3346.                Check_Mismatch
  3347.                  (not Is_Entity_Name (Expr2)
  3348.                  or else Entity (Expr1) /= Entity (Expr2));
  3349.  
  3350.             elsif Nkind (Expr1) = N_Null then
  3351.                Check_Mismatch (Nkind (Expr1) /= N_Null);
  3352.  
  3353.             else
  3354.                Check_Mismatch (True);
  3355.             end if;
  3356.  
  3357.          elsif Ekind (E1) = E_Variable
  3358.            or else Ekind (E1) = E_Package
  3359.          then
  3360.             Check_Mismatch
  3361.               (Ekind (E1) /= Ekind (E2)
  3362.                 or else Renamed_Object (E1) /= Renamed_Object (E2));
  3363.  
  3364.          elsif Is_Overloadable (E1) then
  3365.  
  3366.             --  Verify that the names of the  entities match.
  3367.             --  What if actual is an attribute ???
  3368.  
  3369.             Check_Mismatch
  3370.               (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
  3371.  
  3372.          else
  3373.             pragma Assert (False);
  3374.             null;
  3375.          end if;
  3376.  
  3377.          E1 := Next_Entity (E1);
  3378.          E2 := Next_Entity (E2);
  3379.       end loop;
  3380.    end Check_Formal_Package_Instance;
  3381.  
  3382.    -----------------------------------
  3383.    -- Instantiate_Formal_Subprogram --
  3384.    -----------------------------------
  3385.  
  3386.    function Instantiate_Formal_Subprogram
  3387.      (Formal          : Node_Id;
  3388.       Actual          : Node_Id;
  3389.       Analyzed_Formal : Node_Id)
  3390.       return Node_Id
  3391.    is
  3392.       Loc        : Source_Ptr := Sloc (Instantiation_Node);
  3393.       Formal_Sub : constant Entity_Id :=
  3394.                      Defining_Unit_Name (Specification (Formal));
  3395.       Analyzed_S : constant Entity_Id :=
  3396.                      Defining_Unit_Name (Specification (Analyzed_Formal));
  3397.       Decl_Node  : Node_Id;
  3398.       Nam        : Node_Id;
  3399.       New_Spec   : Node_Id := New_Copy (Specification (Formal));
  3400.  
  3401.       procedure Valid_Actual_Subprogram (Act : Node_Id);
  3402.       --  Perform legality check and raise exception on failure.
  3403.  
  3404.       procedure Valid_Actual_Subprogram (Act : Node_Id) is
  3405.       begin
  3406.          if not Is_Entity_Name (Act)
  3407.            and then Nkind (Act) /= N_Operator_Symbol
  3408.            and then Nkind (Act) /= N_Attribute_Reference
  3409.          then
  3410.             if Etype (Act) /= Any_Type then
  3411.                Error_Msg_NE
  3412.                  ("Expect subprogram name to instantiate &",
  3413.                   Instantiation_Node, Formal_Sub);
  3414.             end if;
  3415.  
  3416.             --  In any case, instantiation cannot continue.
  3417.  
  3418.             Abandon_Instantiation (Instantiation_Node);
  3419.          end if;
  3420.       end Valid_Actual_Subprogram;
  3421.  
  3422.    --  Start of processing for Instantiate_Formal_Subprogram
  3423.  
  3424.    begin
  3425.       --  Find entity of actual. If the actual is an attribute reference, it
  3426.       --  cannot be resolved here (its formal is missing) but is handled
  3427.       --  instead in Attribute_Renaming. If the actual is overloaded, it is
  3428.       --  fully resolved subsequently, when the renaming declaration for the
  3429.       --  formal is analyzed.
  3430.  
  3431.       if Present (Actual) then
  3432.          Loc := Sloc (Actual);
  3433.  
  3434.          if Nkind (Actual) = N_Operator_Symbol then
  3435.             Find_Direct_Name (Actual);
  3436.  
  3437.          elsif Nkind (Actual) /= N_Attribute_Reference then
  3438.             Analyze (Actual);
  3439.          end if;
  3440.  
  3441.          Valid_Actual_Subprogram (Actual);
  3442.          Nam := Actual;
  3443.  
  3444.       elsif Present (Default_Name (Formal)) then
  3445.  
  3446.          if Nkind (Default_Name (Formal)) /= N_Attribute_Reference then
  3447.             Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
  3448.          else
  3449.             Nam := New_Copy (Default_Name (Formal));
  3450.             Set_Sloc (Nam, Loc);
  3451.          end if;
  3452.  
  3453.       elsif Box_Present (Formal) then
  3454.  
  3455.          --  Actual is resolved at the point of instantiation.
  3456.  
  3457.          Nam := Make_Identifier (Loc, Chars (Formal_Sub));
  3458.  
  3459.       else
  3460.          Error_Msg_NE
  3461.            ("missing actual for instantiation of &",
  3462.                                  Instantiation_Node, Formal_Sub);
  3463.          Abandon_Instantiation (Instantiation_Node);
  3464.       end if;
  3465.  
  3466.       Decl_Node :=
  3467.         Make_Subprogram_Renaming_Declaration (Loc,
  3468.           Specification => New_Spec,
  3469.           Name => Nam);
  3470.  
  3471.       --  The generic instantiation freezes the actual. This can only be
  3472.       --  done once the actual is resolved, in the analysis of the renaming
  3473.       --  declaration. To indicate that must be done, we set the corresponding
  3474.       --  spec of the node to point to the formal subprogram declaration.
  3475.  
  3476.       Set_Corresponding_Spec (Decl_Node, Analyzed_Formal);
  3477.  
  3478.       --  We cannot analyze the renaming declaration, and thus find the
  3479.       --  actual, until the all the actuals are assembled in the instance.
  3480.       --  For subsequent checks of other actuals, indicate the node that
  3481.       --  will hold the instance of this formal.
  3482.  
  3483.       Set_Instance_Of (Analyzed_S, Nam);
  3484.       return Decl_Node;
  3485.    end Instantiate_Formal_Subprogram;
  3486.  
  3487.    ----------------------
  3488.    -- Instantiate_Type --
  3489.    ----------------------
  3490.  
  3491.    function Instantiate_Type
  3492.      (Formal          : Node_Id;
  3493.       Actual          : Node_Id;
  3494.       Analyzed_Formal : Node_Id)
  3495.       return   Node_Id
  3496.    is
  3497.       Loc       : constant Source_Ptr := Sloc (Actual);
  3498.       Gen_T     : constant Entity_Id  := Defining_Identifier (Formal);
  3499.       A_Gen_T   : constant Entity_Id  := Defining_Identifier (Analyzed_Formal);
  3500.       Def       : constant Node_Id    := Formal_Type_Definition (Formal);
  3501.       Act_T     : Entity_Id;
  3502.       Decl_Node : Node_Id;
  3503.  
  3504.       procedure Validate_Array_Type_Instance;
  3505.       procedure Validate_Access_Subprogram_Instance;
  3506.       procedure Validate_Access_Type_Instance;
  3507.       procedure Validate_Derived_Type_Instance;
  3508.       procedure Validate_Private_Type_Instance;
  3509.       --  These procedures perform validation tests for the named case
  3510.  
  3511.       function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
  3512.       --  Check that base types are the same and that the subtypes match
  3513.       --  Statically. Used in several of the above.
  3514.  
  3515.       --------------------
  3516.       -- Subtypes_Match --
  3517.       --------------------
  3518.  
  3519.       function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
  3520.          T : constant Entity_Id := Get_Instance_Of (Gen_T);
  3521.  
  3522.       begin
  3523.          return (Base_Type (T) = Base_Type (Act_T)
  3524.                   and then Is_Constrained (T) = Is_Constrained (Act_T)
  3525.                   and then Subtypes_Statically_Match (T, Act_T))
  3526.  
  3527.            or else (Is_Class_Wide_Type (Gen_T)
  3528.                      and then Is_Class_Wide_Type (Act_T)
  3529.                      and then
  3530.                        Subtypes_Match
  3531.                          (Get_Instance_Of (Etype (Gen_T)), Etype (Act_T)));
  3532.       end Subtypes_Match;
  3533.  
  3534.       ----------------------------------
  3535.       -- Validate_Array_Type_Instance --
  3536.       ----------------------------------
  3537.  
  3538.       procedure Validate_Array_Type_Instance is
  3539.          I1 : Node_Id;
  3540.          I2 : Node_Id;
  3541.          T2 : Entity_Id;
  3542.  
  3543.          function Formal_Dimensions return Int;
  3544.          --  Count number of dimensions in array type formal
  3545.  
  3546.          function Formal_Dimensions return Int is
  3547.             Num   : Int := 0;
  3548.             Index : Node_Id;
  3549.  
  3550.          begin
  3551.             if Nkind (Def) = N_Constrained_Array_Definition then
  3552.                Index := First (Discrete_Subtype_Definitions (Def));
  3553.             else
  3554.                Index := First (Subtype_Marks (Def));
  3555.             end if;
  3556.  
  3557.             while Present (Index) loop
  3558.                Num := Num + 1;
  3559.                Index := Next_Index (Index);
  3560.             end loop;
  3561.  
  3562.             return Num;
  3563.          end Formal_Dimensions;
  3564.  
  3565.       begin
  3566.          if not Is_Array_Type (Act_T) then
  3567.             Error_Msg_NE
  3568.               ("expect array type in instantiation of &", Actual, Gen_T);
  3569.             Abandon_Instantiation (Actual);
  3570.  
  3571.          elsif Nkind (Def) = N_Constrained_Array_Definition then
  3572.             if not (Is_Constrained (Act_T)) then
  3573.                Error_Msg_NE
  3574.                  ("expect constrained array in instantiation of &",
  3575.                   Actual, Gen_T);
  3576.                Abandon_Instantiation (Actual);
  3577.             end if;
  3578.  
  3579.          else
  3580.             if Is_Constrained (Act_T) then
  3581.                Error_Msg_NE
  3582.                  ("expect unconstrained array in instantiation of &",
  3583.                   Actual, Gen_T);
  3584.                Abandon_Instantiation (Actual);
  3585.             end if;
  3586.          end if;
  3587.  
  3588.          if Formal_Dimensions /= Number_Dimensions (Act_T) then
  3589.             Error_Msg_NE
  3590.               ("dimensions of actual do not match formal &", Actual, Gen_T);
  3591.             Abandon_Instantiation (Actual);
  3592.          end if;
  3593.  
  3594.          I1 := First_Index (A_Gen_T);
  3595.          I2 := First_Index (Act_T);
  3596.  
  3597.          for I in 1 .. Formal_Dimensions loop
  3598.  
  3599.             --  If the indices of the actual were given by a subtype_mark,
  3600.             --  the index was transformed into a range attribute. Retrieve
  3601.             --  the original type mark for checking.
  3602.  
  3603.             if Is_Entity_Name (Original_Node (I2)) then
  3604.                T2 := Entity (Original_Node (I2));
  3605.             else
  3606.                T2 := Etype (I2);
  3607.             end if;
  3608.  
  3609.             if not Subtypes_Match (Etype (I1), T2) then
  3610.                Error_Msg_NE
  3611.                  ("index types of actual do not match those of formal &",
  3612.                     Actual, Gen_T);
  3613.                Abandon_Instantiation (Actual);
  3614.             end if;
  3615.  
  3616.             I1 := Next_Index (I1);
  3617.             I2 := Next_Index (I2);
  3618.          end loop;
  3619.  
  3620.          if not Subtypes_Match
  3621.             (Component_Type (A_Gen_T), Component_Type (Act_T))
  3622.          then
  3623.             Error_Msg_NE
  3624.               ("component subtype of actual does not match that of formal &",
  3625.                  Actual, Gen_T);
  3626.             Abandon_Instantiation (Actual);
  3627.          end if;
  3628.  
  3629.       end Validate_Array_Type_Instance;
  3630.  
  3631.       -----------------------------------
  3632.       -- Validate_Access_Type_Instance --
  3633.       -----------------------------------
  3634.  
  3635.       procedure Validate_Access_Type_Instance is
  3636.          Desig_Type : Entity_Id := Get_Instance_Of (Designated_Type (A_Gen_T));
  3637.       begin
  3638.          if not Is_Access_Type (Act_T) then
  3639.             Error_Msg_NE
  3640.               ("expect access type in instantiation of &", Actual,  Gen_T);
  3641.             Abandon_Instantiation (Actual);
  3642.          end if;
  3643.  
  3644.          if not Subtypes_Match
  3645.            (Desig_Type, Designated_Type (Act_T))
  3646.          then
  3647.             Error_Msg_NE
  3648.               ("designated type of actual does not match that of formal &",
  3649.                  Actual, Gen_T);
  3650.             Abandon_Instantiation (Actual);
  3651.  
  3652.          elsif Is_Access_Type (Designated_Type (Act_T))
  3653.            and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
  3654.                /= Is_Constrained (Designated_Type (Desig_Type))
  3655.          then
  3656.             Error_Msg_NE
  3657.               ("designated type of actual does not match that of formal &",
  3658.                  Actual, Gen_T);
  3659.             Abandon_Instantiation (Actual);
  3660.          end if;
  3661.       end Validate_Access_Type_Instance;
  3662.  
  3663.       ----------------------------------
  3664.       -- Validate_Subprogram_Instance --
  3665.       ----------------------------------
  3666.  
  3667.       procedure Validate_Access_Subprogram_Instance is
  3668.       begin
  3669.          if not Is_Access_Type (Act_T)
  3670.            or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
  3671.          then
  3672.             Error_Msg_NE
  3673.               ("expect access type in instantiation of &", Actual,  Gen_T);
  3674.             Abandon_Instantiation (Actual);
  3675.          end if;
  3676.       end Validate_Access_Subprogram_Instance;
  3677.  
  3678.       ------------------------------------
  3679.       -- Validate_Private_Type_Instance --
  3680.       ------------------------------------
  3681.  
  3682.       procedure Validate_Private_Type_Instance is
  3683.          Formal_Discr : Entity_Id;
  3684.          Actual_Discr : Entity_Id;
  3685.          Formal_Subt  : Entity_Id;
  3686.  
  3687.       begin
  3688.          if Is_Limited_Type (Act_T)
  3689.            and then not Is_Limited_Type (A_Gen_T)
  3690.          then
  3691.             Error_Msg_NE
  3692.               ("actual for non-limited  & cannot be a limited type", Actual,
  3693.                Gen_T);
  3694.             Abandon_Instantiation (Actual);
  3695.  
  3696.          elsif Is_Indefinite_Subtype (Act_T)
  3697.             and then not Is_Indefinite_Subtype (A_Gen_T)
  3698.             and then Ada_95
  3699.          then
  3700.             Error_Msg_NE
  3701.               ("actual for & must be a definite subtype", Actual, Gen_T);
  3702.  
  3703.          elsif not Is_Tagged_Type (Act_T)
  3704.            and then Is_Tagged_Type (A_Gen_T)
  3705.          then
  3706.             Error_Msg_NE
  3707.               ("actual for & must be a tagged type", Actual, Gen_T);
  3708.  
  3709.          elsif Has_Discriminants (A_Gen_T) then
  3710.             if not Has_Discriminants (Act_T) then
  3711.                Error_Msg_NE
  3712.                  ("actual for & must have discriminants", Actual, Gen_T);
  3713.                Abandon_Instantiation (Actual);
  3714.  
  3715.             elsif Is_Constrained (Act_T) then
  3716.                Error_Msg_NE
  3717.                  ("actual for & must be unconstrained", Actual, Gen_T);
  3718.                Abandon_Instantiation (Actual);
  3719.  
  3720.             else
  3721.                Formal_Discr := First_Discriminant (A_Gen_T);
  3722.                Actual_Discr := First_Discriminant (Act_T);
  3723.  
  3724.                while Formal_Discr /= Empty loop
  3725.                   if Actual_Discr = Empty then
  3726.                      Error_Msg_NE
  3727.                        ("discriminants on actual do not match formal",
  3728.                         Actual, Gen_T);
  3729.                      Abandon_Instantiation (Actual);
  3730.                   end if;
  3731.  
  3732.                   Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
  3733.  
  3734.                   if Base_Type (Formal_Subt)
  3735.                     /= Base_Type (Etype (Actual_Discr))
  3736.                   then
  3737.                      Error_Msg_NE
  3738.                        ("types of actual discriminants must match formal",
  3739.                         Actual, Gen_T);
  3740.                      Abandon_Instantiation (Actual);
  3741.  
  3742.                   elsif not Subtypes_Statically_Match
  3743.                               (Formal_Subt, Etype (Actual_Discr))
  3744.                     and then Ada_95
  3745.                   then
  3746.                      Error_Msg_NE
  3747.                        ("subtypes of actual discriminants must match formal",
  3748.                         Actual, Gen_T);
  3749.                      Abandon_Instantiation (Actual);
  3750.                   end if;
  3751.  
  3752.                   Formal_Discr := Next_Discriminant (Formal_Discr);
  3753.                   Actual_Discr := Next_Discriminant (Actual_Discr);
  3754.                end loop;
  3755.  
  3756.                if Actual_Discr /= Empty then
  3757.                   Error_Msg_NE
  3758.                     ("discriminants on actual do not match formal",
  3759.                      Actual, Gen_T);
  3760.                   Abandon_Instantiation (Actual);
  3761.                end if;
  3762.             end if;
  3763.  
  3764.          end if;
  3765.  
  3766.       end Validate_Private_Type_Instance;
  3767.  
  3768.       ------------------------------------
  3769.       -- Validate_Derived_Type_Instance --
  3770.       ------------------------------------
  3771.  
  3772.       procedure Validate_Derived_Type_Instance is
  3773.          Ancestor : Entity_Id := Get_Instance_Of (Root_Type (A_Gen_T));
  3774.  
  3775.       begin
  3776.          if not Is_Ancestor (Ancestor, Act_T) then
  3777.             Error_Msg_NE
  3778.                ("expect type derived from & in instantiation",
  3779.                 Actual, Ancestor);
  3780.             Abandon_Instantiation (Actual);
  3781.          end if;
  3782.       end Validate_Derived_Type_Instance;
  3783.  
  3784.    --  Start of processing for Instantiate_Type
  3785.  
  3786.    begin
  3787.       if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
  3788.          Error_Msg_N ("duplicate instantiation of generic type", Actual);
  3789.          return Error;
  3790.  
  3791.       elsif not Is_Entity_Name (Actual)
  3792.         or else not Is_Type (Entity (Actual))
  3793.       then
  3794.          Error_Msg_NE
  3795.            ("expect valid subtype mark to instantiate &", Actual, Gen_T);
  3796.          Abandon_Instantiation (Actual);
  3797.  
  3798.       else
  3799.          Act_T := Entity (Actual);
  3800.  
  3801.          if Ekind (Act_T) = E_Incomplete_Type then
  3802.             if No (Underlying_Type (Act_T)) then
  3803.                Error_Msg_N ("premature use of incomplete type", Actual);
  3804.             else
  3805.                Act_T := Full_View (Act_T);
  3806.             end if;
  3807.  
  3808.          elsif Is_Private_Type (Act_T)
  3809.            and then not Is_Generic_Type (Act_T)
  3810.            and then not Is_Derived_Type (Act_T)
  3811.            and then No (Full_View (Root_Type (Act_T)))
  3812.          then
  3813.             Error_Msg_N ("premature use of private type", Actual);
  3814.  
  3815.          elsif Has_Private_Component (Act_T) then
  3816.             Error_Msg_N
  3817.               ("premature use of type with private component", Actual);
  3818.          end if;
  3819.  
  3820.          Set_Instance_Of (A_Gen_T, Act_T);
  3821.  
  3822.          if not Is_Abstract (A_Gen_T)
  3823.            and then Is_Abstract (Act_T)
  3824.          then
  3825.             Error_Msg_N
  3826.               ("actual of non-abstract formal cannot be abstract", Actual);
  3827.          end if;
  3828.  
  3829.          if Is_Scalar_Type (Gen_T) then
  3830.             Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
  3831.          end if;
  3832.       end if;
  3833.  
  3834.       Freeze_Before (Instantiation_Node, Act_T);
  3835.  
  3836.       case Nkind (Def) is
  3837.          when N_Formal_Private_Type_Definition =>
  3838.             Validate_Private_Type_Instance;
  3839.  
  3840.          when N_Formal_Derived_Type_Definition =>
  3841.             Validate_Derived_Type_Instance;
  3842.  
  3843.          when N_Formal_Discrete_Type_Definition =>
  3844.             if not Is_Discrete_Type (Act_T) then
  3845.                Error_Msg_NE
  3846.                  ("expect discrete type in instantiation of&", Actual, Gen_T);
  3847.                Abandon_Instantiation (Actual);
  3848.             end if;
  3849.  
  3850.          when N_Formal_Signed_Integer_Type_Definition =>
  3851.             if not Is_Signed_Integer_Type (Act_T) then
  3852.                Error_Msg_NE
  3853.                  ("expect signed integer type in instantiation of&",
  3854.                   Actual, Gen_T);
  3855.                Abandon_Instantiation (Actual);
  3856.             end if;
  3857.  
  3858.          when N_Formal_Modular_Type_Definition =>
  3859.             if not Is_Modular_Integer_Type (Act_T) then
  3860.                Error_Msg_NE
  3861.                  ("expect modular type in instantiation of &", Actual, Gen_T);
  3862.                Abandon_Instantiation (Actual);
  3863.             end if;
  3864.  
  3865.          when N_Formal_Floating_Point_Definition =>
  3866.             if not Is_Floating_Point_Type (Act_T) then
  3867.                Error_Msg_NE
  3868.                  ("expect float type in instantiation of &", Actual, Gen_T);
  3869.                Abandon_Instantiation (Actual);
  3870.             end if;
  3871.  
  3872.          when N_Formal_Ordinary_Fixed_Point_Definition =>
  3873.             if not Is_Fixed_Point_Type (Act_T) then
  3874.                Error_Msg_NE
  3875.                  ("expect fixed point type in instantiation of &",
  3876.                   Actual, Gen_T);
  3877.                Abandon_Instantiation (Actual);
  3878.             end if;
  3879.  
  3880.          when N_Formal_Decimal_Fixed_Point_Definition =>
  3881.             if not Is_Decimal_Fixed_Point_Type (Act_T) then
  3882.                Error_Msg_NE
  3883.                  ("expect decimal type in instantiation of &",
  3884.                   Actual, Gen_T);
  3885.                Abandon_Instantiation (Actual);
  3886.             end if;
  3887.  
  3888.          when N_Array_Type_Definition =>
  3889.             Validate_Array_Type_Instance;
  3890.  
  3891.          when N_Access_To_Object_Definition =>
  3892.             Validate_Access_Type_Instance;
  3893.  
  3894.          when N_Access_Function_Definition |
  3895.               N_Access_Procedure_Definition =>
  3896.             Validate_Access_Subprogram_Instance;
  3897.  
  3898.          when others =>
  3899.             pragma Assert (False); null;
  3900.  
  3901.       end case;
  3902.  
  3903.       Decl_Node :=
  3904.         Make_Subtype_Declaration (Loc,
  3905.           Defining_Identifier => New_Copy (Gen_T),
  3906.           Subtype_Indication  => New_Reference_To (Act_T, Loc));
  3907.  
  3908.       if Is_Private_Type (Act_T) then
  3909.          Set_Has_Private_View (Subtype_Indication (Decl_Node));
  3910.       end if;
  3911.  
  3912.       return Decl_Node;
  3913.    end Instantiate_Type;
  3914.  
  3915.    -----------------------
  3916.    -- Move_Freeze_Nodes --
  3917.    -----------------------
  3918.  
  3919.    procedure Move_Freeze_Nodes
  3920.      (Out_Of : Entity_Id;
  3921.       After  : Node_Id;
  3922.       L      : List_Id)
  3923.    is
  3924.       Decl      : Node_Id;
  3925.       Next_Decl : Node_Id;
  3926.       Next_Node : Node_Id := After;
  3927.       Spec      : Node_Id;
  3928.  
  3929.    begin
  3930.       if No (L) then
  3931.          return;
  3932.       end if;
  3933.  
  3934.       --  First remove the freeze nodes that may appear before all other
  3935.       --  declarations.
  3936.  
  3937.       Decl := First (L);
  3938.  
  3939.       while Present (Decl)
  3940.         and then Nkind (Decl) = N_Freeze_Entity
  3941.         and then Scope_Depth (Scope (Entity (Decl))) < Scope_Depth (Out_Of)
  3942.       loop
  3943.          Decl := Remove_Head (L);
  3944.          Insert_After (Next_Node, Decl);
  3945.          Set_Analyzed (Decl, False);
  3946.          Next_Node := Decl;
  3947.          Decl := First (L);
  3948.       end loop;
  3949.  
  3950.       --  Next scan the list of declarations and remove each freeze node that
  3951.       --  appears ahead of the current node.
  3952.  
  3953.       while Present (Decl) loop
  3954.          while Present (Next (Decl))
  3955.            and then Nkind (Next (Decl)) = N_Freeze_Entity
  3956.            and then Scope_Depth (Scope (Entity (Next (Decl))))
  3957.                  < Scope_Depth (Out_Of)
  3958.          loop
  3959.             Next_Decl := Remove_Next (Decl);
  3960.             Insert_After (Next_Node, Next_Decl);
  3961.             Set_Analyzed (Next_Decl, False);
  3962.             Next_Node := Next_Decl;
  3963.          end loop;
  3964.  
  3965.          --  If the declaration is a nested package or concurrent type, then
  3966.          --  recurse. Nested generic packages will have been processed from the
  3967.          --  inside out.
  3968.  
  3969.          if Nkind (Decl) = N_Package_Specification then
  3970.             Spec := Decl;
  3971.  
  3972.          elsif Nkind (Decl) = N_Task_Type_Declaration then
  3973.             Spec := Task_Definition (Decl);
  3974.  
  3975.          elsif Nkind (Decl) = N_Protected_Type_Declaration then
  3976.             Spec := Protected_Definition (Decl);
  3977.  
  3978.          else
  3979.             Spec := Empty;
  3980.          end if;
  3981.  
  3982.          if Present (Spec) then
  3983.             Move_Freeze_Nodes (Out_Of, After, Visible_Declarations (Spec));
  3984.             Move_Freeze_Nodes (Out_Of, After, Private_Declarations (Spec));
  3985.          end if;
  3986.  
  3987.          Decl := Next (Decl);
  3988.  
  3989.       end loop;
  3990.    end Move_Freeze_Nodes;
  3991.  
  3992.    ---------------------------
  3993.    -- Restore_Private_Views --
  3994.    ---------------------------
  3995.  
  3996.    procedure Restore_Private_Views
  3997.      (Pack_Id    : Entity_Id;
  3998.       Is_Package : Boolean := True)
  3999.    is
  4000.       M : Elmt_Id;
  4001.       E : Entity_Id;
  4002.  
  4003.    begin
  4004.       M := First_Elmt (Exchanged_Views);
  4005.       while Present (M) loop
  4006.          Exchange_Declarations (Node (M));
  4007.          M := Next_Elmt (M);
  4008.       end loop;
  4009.  
  4010.       --  Make the generic formal parameters private, and make the formal
  4011.       --  types into subtypes of the actuals again.
  4012.  
  4013.       E := First_Entity (Pack_Id);
  4014.  
  4015.       while Present (E) loop
  4016.          Set_Is_Private (E, True);
  4017.  
  4018.          if Nkind (Parent (E)) = N_Subtype_Declaration then
  4019.             Set_Is_Generic_Actual_Type (E, False);
  4020.  
  4021.          elsif Ekind (E) = E_Package then
  4022.  
  4023.             --  The end of the renaming list is the renaming of the generic
  4024.             --  package itself. If the instance is a subprogram, all entities
  4025.             --  in the corresponding package are renamings. If this entity is
  4026.             --  a formal package, make its own formals private as well. The
  4027.             --  actual in this case is itself the renaming of an instantation.
  4028.             --  If the entity is not a package renaming, it is the entity
  4029.             --  created to validate formal package actuals: ignore.
  4030.  
  4031.             if Is_Package
  4032.               and then Renamed_Object (E) = Pack_Id
  4033.             then
  4034.                exit;
  4035.  
  4036.             elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
  4037.                null;
  4038.  
  4039.             else
  4040.                declare
  4041.                   Act_P : Entity_Id := Renamed_Object (E);
  4042.                   Id    : Entity_Id := First_Entity (Act_P);
  4043.  
  4044.                begin
  4045.                   while Present (Id)
  4046.                     and then Id /= First_Private_Entity (Act_P)
  4047.                   loop
  4048.                      Set_Is_Private (Id, True);
  4049.                      Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
  4050.                      exit when Ekind (Id) = E_Package
  4051.                        and then Renamed_Object (Id) = Act_P;
  4052.  
  4053.                      Id := Next_Entity (Id);
  4054.                   end loop;
  4055.                end;
  4056.                null;
  4057.             end if;
  4058.          end if;
  4059.  
  4060.          E := Next_Entity (E);
  4061.       end loop;
  4062.    end Restore_Private_Views;
  4063.  
  4064.    ----------------------------
  4065.    -- Save_Global_References --
  4066.    ----------------------------
  4067.  
  4068.    procedure Save_Global_References (N : Node_Id) is
  4069.       Gen_Scope : Entity_Id;
  4070.       E         : Entity_Id;
  4071.       N2        : Node_Id;
  4072.  
  4073.       function Is_Global (E : Entity_Id) return Boolean;
  4074.       --  Check whether entity is defined outside of generic unit.
  4075.       --  Examine the scope of an entity, and the scope of the scope,
  4076.       --  etc, until we find either Standard, in which case the entity
  4077.       --  is global, or the generic unit itself, which indicates that
  4078.       --  the entity is local. If the entity is the generic unit itself,
  4079.       --  as in the case of a recursive call, or the enclosing generic unit,
  4080.       --  if different from the current scope, then it is local as well,
  4081.       --  because it will be replaced at the point of instantiation.
  4082.  
  4083.       procedure Reset_Entity (N : Node_Id);
  4084.       --  Save semantic information on global entity, so that it is not
  4085.       --  resolved again at instantiation time.
  4086.  
  4087.       procedure Save_Global_Descendant (D : Union_Id);
  4088.       --  Apply Save_Global_References recursively to the descendents of
  4089.       --  current node.
  4090.  
  4091.       procedure Save_References (N : Node_Id);
  4092.       --  This is the recursive procedure that does the work, once the
  4093.       --  enclosing generic scope has been established.
  4094.  
  4095.       ---------------
  4096.       -- Is_Global --
  4097.       ---------------
  4098.  
  4099.       function Is_Global (E : Entity_Id) return Boolean is
  4100.          Se  : Entity_Id := Scope (E);
  4101.  
  4102.       begin
  4103.          if E = Gen_Scope then
  4104.             return False;
  4105.          elsif E = Standard_Standard then
  4106.             return True;
  4107.          else
  4108.             while Se /= Gen_Scope loop
  4109.                if Se = Standard_Standard then
  4110.                   return true;
  4111.                else
  4112.                   Se := Scope (Se);
  4113.                end if;
  4114.             end loop;
  4115.  
  4116.             return False;
  4117.          end if;
  4118.       end Is_Global;
  4119.  
  4120.       ----------------------------
  4121.       -- Save_Global_Descendant --
  4122.       ----------------------------
  4123.  
  4124.       procedure Save_Global_Descendant (D : Union_Id) is
  4125.          N1 : Node_Id;
  4126.  
  4127.       begin
  4128.          if D in Node_Range then
  4129.             if D = Union_Id (Empty) then
  4130.                null;
  4131.  
  4132.             elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
  4133.                Save_References (Node_Id (D));
  4134.             end if;
  4135.  
  4136.          elsif D in List_Range then
  4137.             if D = Union_Id (No_List)
  4138.               or else Is_Empty_List (List_Id (D))
  4139.             then
  4140.                null;
  4141.  
  4142.             else
  4143.                N1 := First (List_Id (D));
  4144.                while Present (N1) loop
  4145.                   Save_References (N1);
  4146.                   N1 := Next (N1);
  4147.                end loop;
  4148.             end if;
  4149.  
  4150.          --  Element list or other non-node field, nothing to do
  4151.  
  4152.          else
  4153.             null;
  4154.          end if;
  4155.       end Save_Global_Descendant;
  4156.  
  4157.       ------------------
  4158.       -- Reset_Entity --
  4159.       ------------------
  4160.  
  4161.       procedure Reset_Entity (N : Node_Id) is
  4162.  
  4163.          procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
  4164.          --  The type of N2 is global to the generic unit. Save the
  4165.          --  type in the generic node.
  4166.  
  4167.          procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
  4168.          begin
  4169.             Set_Etype (N, Etype (N2));
  4170.  
  4171.             if Is_Private_Type (Etype (N)) then
  4172.                Set_Has_Private_View (N);
  4173.  
  4174.                if  Present (Full_View (Etype (N2))) then
  4175.                   Set_Etype (N2, Full_View (Etype (N2)));
  4176.                end if;
  4177.             end if;
  4178.          end Set_Global_Type;
  4179.  
  4180.       --  Start of processing for Reset_Entity
  4181.  
  4182.       begin
  4183.          N2 := Associated_Node (N);
  4184.          E := Entity (N2);
  4185.  
  4186.          if Present (E) then
  4187.             if Is_Global (E) then
  4188.                Set_Global_Type (N, N2);
  4189.             else
  4190.                --  Entity is local.  Mark generic node as unresolved.
  4191.                --  Note that now it does not have an entity.
  4192.                Set_Associated_Node (N, Empty);
  4193.                Set_Etype  (N, Empty);
  4194.             end if;
  4195.  
  4196.          elsif Nkind (Parent (N)) = N_Selected_Component
  4197.            and then Nkind (Parent (N2)) = N_Expanded_Name
  4198.            and then Is_Global (Entity (Parent (N2)))
  4199.          then
  4200.             Change_Selected_Component_To_Expanded_Name (Parent (N));
  4201.             Set_Associated_Node (Parent (N), Parent (N2));
  4202.             Set_Global_Type (Parent (N), Parent (N2));
  4203.  
  4204.             Save_Global_Descendant (Field2 (N));
  4205.             Save_Global_Descendant (Field3 (N));
  4206.  
  4207.          else
  4208.             --  Entity is local.  Reset in generic unit,  so that node
  4209.             --  is resolved anew at the point of instantiation.
  4210.  
  4211.             Set_Associated_Node (N, Empty);
  4212.             Set_Etype (N, Empty);
  4213.          end if;
  4214.       end Reset_Entity;
  4215.  
  4216.       ----------------------
  4217.       --  Save_References --
  4218.       ----------------------
  4219.  
  4220.       --  This is the recursive procedure that does the work, once the
  4221.       --  enclosing generic scope has been established.
  4222.  
  4223.       procedure Save_References (N : Node_Id) is
  4224.  
  4225.       begin
  4226.          if N = Empty then
  4227.             null;
  4228.  
  4229.          elsif (Nkind (N) = N_Character_Literal
  4230.            or else Nkind (N) = N_Operator_Symbol)
  4231.            and then Nkind (N) = Nkind (Associated_Node (N))
  4232.          then
  4233.             Reset_Entity (N);
  4234.  
  4235.          elsif Nkind (N) in N_Op then
  4236.  
  4237.             if Nkind (N) = Nkind (Associated_Node (N)) then
  4238.                Reset_Entity (N);
  4239.  
  4240.             else
  4241.                --  Node may be transformed into call to a user-defined operator
  4242.  
  4243.                N2 := Associated_Node (N);
  4244.  
  4245.                if Nkind (N2) = N_Function_Call then
  4246.                   E := Entity (Name (N2));
  4247.  
  4248.                   if Present (E)
  4249.                     and then Is_Global (E)
  4250.                   then
  4251.                      Set_Etype (N, Etype (N2));
  4252.                   else
  4253.                      Set_Associated_Node (N, Empty);
  4254.                      Set_Etype (N, Empty);
  4255.                   end if;
  4256.  
  4257.                elsif Nkind (N2) = N_Integer_Literal
  4258.                  or else Nkind (N2) = N_Real_Literal
  4259.                  or else Nkind (N2) = N_String_Literal
  4260.                then
  4261.  
  4262.                   --  Operation was constant-folded, perform the same
  4263.                   --  replacement in generic.
  4264.  
  4265.                   Rewrite_Substitute_Tree (N, New_Copy (N2));
  4266.                   Set_Analyzed (N, False);
  4267.                end if;
  4268.             end if;
  4269.  
  4270.             --  Complete the check on operands.
  4271.  
  4272.             Save_Global_Descendant (Field2 (N));
  4273.             Save_Global_Descendant (Field3 (N));
  4274.  
  4275.          elsif Nkind (N) = N_Identifier then
  4276.             if Nkind (N) = Nkind (Associated_Node (N)) then
  4277.                Reset_Entity (N);
  4278.             else
  4279.                N2 := Associated_Node (N);
  4280.  
  4281.                if Nkind (N2) = N_Function_Call then
  4282.                   E := Entity (Name (N2));
  4283.  
  4284.                   --  Name resolves to a call to parameterless function.
  4285.                   --  If original entity is global,  mark node as resolved.
  4286.  
  4287.                   if Present (E)
  4288.                     and then Is_Global (E)
  4289.                   then
  4290.                      Set_Etype (N, Etype (N2));
  4291.                   else
  4292.                      Set_Associated_Node (N, Empty);
  4293.                      Set_Etype (N, Empty);
  4294.                   end if;
  4295.  
  4296.                elsif Nkind (N2) = N_Integer_Literal
  4297.                  or else Nkind (N2) = N_Real_Literal
  4298.                  or else Nkind (N2) = N_String_Literal
  4299.                then
  4300.                   --  Name resolves to named number that is constant-folded,
  4301.                   --  or to string literal from concatenation.
  4302.                   --  Perform the same replacement in generic.
  4303.  
  4304.                   Rewrite_Substitute_Tree (N,  New_Copy (N2));
  4305.                   Set_Analyzed (N, False);
  4306.  
  4307.                elsif Nkind (N2) = N_Explicit_Dereference then
  4308.  
  4309.                   --  Check whether entity of prefix is global.
  4310.  
  4311.                   if Present (Entity (Prefix (N2)))
  4312.                     and then Is_Global (Entity (Prefix (N2)))
  4313.                   then
  4314.                      Rewrite_Substitute_Tree (N,  New_Copy (N2));
  4315.                      Set_Analyzed (N, False);
  4316.                   else
  4317.                      Set_Associated_Node (N, Empty);
  4318.                      Set_Etype (N, Empty);
  4319.                   end if;
  4320.  
  4321.                else
  4322.                   null;
  4323.                end if;
  4324.             end if;
  4325.  
  4326.          elsif Nkind (N) in N_Entity then
  4327.             null;
  4328.  
  4329.          else
  4330.             Save_Global_Descendant (Field1 (N));
  4331.             Save_Global_Descendant (Field2 (N));
  4332.             Save_Global_Descendant (Field3 (N));
  4333.             Save_Global_Descendant (Field4 (N));
  4334.             Save_Global_Descendant (Field5 (N));
  4335.  
  4336.          end if;
  4337.       end Save_References;
  4338.    begin
  4339.  
  4340.       --  Start of processing for Save_Global_References
  4341.  
  4342.       Gen_Scope := Current_Scope;
  4343.  
  4344.       --  If the generic unit is a child unit, references to entities in
  4345.       --  the parent are treated as local, because they will be resolved
  4346.       --  anew in the context of the instance of the parent.
  4347.  
  4348.       while Is_Child_Unit (Gen_Scope)
  4349.         and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
  4350.       loop
  4351.          Gen_Scope := Scope (Gen_Scope);
  4352.       end loop;
  4353.  
  4354.       Save_References (N);
  4355.    end Save_Global_References;
  4356.  
  4357.    -------------------------
  4358.    -- Set_Associated_Node --
  4359.    -------------------------
  4360.  
  4361.    procedure Set_Associated_Node
  4362.      (Gen_Node  : Node_Id;
  4363.       Copy_Node : Node_Id)
  4364.    is
  4365.    begin
  4366.       Set_Node4 (Gen_Node,  Copy_Node);
  4367.    end Set_Associated_Node;
  4368.  
  4369.    ---------------------
  4370.    -- Set_Instance_Of --
  4371.    ---------------------
  4372.  
  4373.    procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
  4374.    begin
  4375.       Generic_Renamings.Table (Generic_Renamings.Last) := (A, B);
  4376.       Generic_Renamings.Increment_Last;
  4377.    end Set_Instance_Of;
  4378.  
  4379. end Sem_Ch12;
  4380.