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_ch13.adb < prev    next >
Text File  |  1996-09-28  |  52KB  |  1,520 lines

  1. -----------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT COMPILER COMPONENTS                         --
  4. --                                                                          --
  5. --                             S E M _ C H 1 3                              --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.170 $                            --
  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 Errout;   use Errout;
  28. with Features; use Features;
  29. with Freeze;   use Freeze;
  30. with Lib;      use Lib;
  31. with Nlists;   use Nlists;
  32. with Nmake;    use Nmake;
  33. with Output;   use Output;
  34. with Rtsfind;  use Rtsfind;
  35. with Sem;      use Sem;
  36. with Sem_Ch3;  use Sem_Ch3;
  37. with Sem_Ch8;  use Sem_Ch8;
  38. with Sem_Eval; use Sem_Eval;
  39. with Sem_Res;  use Sem_Res;
  40. with Sem_Util; use Sem_Util;
  41. with Stand;    use Stand;
  42. with Sinfo;    use Sinfo;
  43. with Sinput;   use Sinput;
  44. with Snames;   use Snames;
  45. with Ttypes;   use Ttypes;
  46. with Uintp;    use Uintp;
  47. with Urealp;   use Urealp;
  48.  
  49. package body Sem_Ch13 is
  50.  
  51.    -----------------------
  52.    -- Local Subprograms --
  53.    -----------------------
  54.  
  55.    function Already_Frozen (T : Entity_Id; N : Node_Id) return Boolean;
  56.    --  Called at the start of processing a representation clause. Used to
  57.    --  check that type T, referenced by representation clause N, is not
  58.    --  already frozen. If the type is not frozen, then False is returned,
  59.    --  and the caller can proceed. If the type is frozen, then an error
  60.    --  message is issued and True is returned (which is a signal to the
  61.    --  caller to abandon processing of the too late rep clause).
  62.  
  63.    procedure Check_Size (N : Node_Id; T : Entity_Id; Siz : Uint);
  64.    --  Called when size S is specified for subtype T. This subprogram checks
  65.    --  that the size is appropriate, posting errors on node N as required.
  66.    --  For non-elementary types, a check is only made if an explicit size
  67.    --  has been given for the type (and the specified size must match)
  68.  
  69.    --------------------
  70.    -- Already_Frozen --
  71.    --------------------
  72.  
  73.    function Already_Frozen (T : Entity_Id; N : Node_Id) return Boolean is
  74.       S : Entity_Id;
  75.  
  76.    begin
  77.       if Is_Frozen (T) then
  78.          Error_Msg_N  ("rep clause appears too late", N);
  79.  
  80.          S := First_Subtype (T);
  81.  
  82.          if Present (Freeze_Node (S)) then
  83.             Error_Msg_NE
  84.               ("?no more rep clauses for }", Freeze_Node (S), T);
  85.          end if;
  86.  
  87.          return True;
  88.  
  89.       else
  90.          return False;
  91.       end if;
  92.    end Already_Frozen;
  93.  
  94.    -----------------------
  95.    -- Analyze_At_Clause --
  96.    -----------------------
  97.  
  98.    --  An at clause is replaced by the corresponding Address attribute
  99.    --  definition clause that is the preferred approach in Ada 95.
  100.  
  101.    procedure Analyze_At_Clause (N : Node_Id) is
  102.    begin
  103.       Rewrite_Substitute_Tree (N,
  104.         Make_Attribute_Definition_Clause (Sloc (N),
  105.           Name  => Identifier (N),
  106.           Chars => Name_Address,
  107.           Expression => Expression (N)));
  108.       Analyze (N);
  109.    end Analyze_At_Clause;
  110.  
  111.    -----------------------------------------
  112.    -- Analyze_Attribute_Definition_Clause --
  113.    -----------------------------------------
  114.  
  115.    procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
  116.       Nam  : constant Node_Id := Name (N);
  117.       Attr : constant Name_Id := Chars (N);
  118.       Expr : constant Node_Id := Expression (N);
  119.       Id   : constant Attribute_Id := Get_Attribute_Id (Attr);
  120.       Typ  : Node_Id;
  121.       Ent  : Entity_Id;
  122.  
  123.    begin
  124.       Analyze (Nam);
  125.       Ent := Entity (Nam);
  126.  
  127.       --  Rep clause applies to full view of incomplete type or private type
  128.       --  if we have one (if not, this is a premature use of the type).
  129.  
  130.       Ent := Underlying_Type (Ent);
  131.  
  132.       if No (Ent) then
  133.          Error_Msg_N ("premature reference to incomplete/private type", Nam);
  134.          return;
  135.       end if;
  136.  
  137.       --  Ignore rep clause for junk entity
  138.  
  139.       if Etype (Nam) = Any_Type then
  140.          return;
  141.       end if;
  142.  
  143.       --  Require first named subtype
  144.  
  145.       if Is_Type (Ent) and then not Is_First_Subtype (Ent) then
  146.          Error_Msg_N ("cannot specify attribute for subtype", Nam);
  147.          return;
  148.       end if;
  149.  
  150.       --  Check not already frozen
  151.  
  152.       if Already_Frozen (Ent, Nam) then
  153.          return;
  154.       end if;
  155.  
  156.       --  Switch on particular attribute
  157.  
  158.       case Id is
  159.  
  160.          -------------
  161.          -- Address --
  162.          -------------
  163.  
  164.          --  Address attribute definition clause
  165.  
  166.          when Attribute_Address => Address : begin
  167.             Note_Feature (New_Representation_Clauses, Sloc (N));
  168.  
  169.             if Present (Address_Clause (Ent)) then
  170.                Error_Msg_N ("address already given for &", Nam);
  171.  
  172.             elsif Ekind (Ent) not in Subprogram_Kind
  173.               and then Ekind (Ent) /= E_Variable
  174.               and then Ekind (Ent) /= E_Constant
  175.               and then
  176.                 (Ekind (Ent) /= E_Entry
  177.                   or else not Is_Task_Type (Scope (Ent)))
  178.  
  179.             then
  180.                Error_Msg_N ("address cannot be given for &", Nam);
  181.  
  182.             else
  183.                Analyze (Expr);
  184.                Resolve (Expr, RTE (RE_Address));
  185.  
  186.                --  Only allowable expression is prior defined constant
  187.  
  188.                if Nkind (Expr) = N_Identifier then
  189.                   declare
  190.                      Entx : constant Entity_Id  := Entity (Expr);
  191.                      Locx : constant Source_Ptr := Sloc (Entx);
  192.                      Loce : constant Source_Ptr := Sloc (Ent);
  193.  
  194.                   begin
  195.                      --  The entity must be a constant, and its location must
  196.                      --  be either less than the source location of the entity
  197.                      --  being given an address (meaning that it is declared
  198.                      --  either before the entity in the current unit, or in
  199.                      --  another unit), or greater than the last source
  200.                      --  location of the current unit, which means that it
  201.                      --  is in some other unit.
  202.  
  203.                      if (Ekind (Entx) = E_Constant
  204.                            or else Ekind (Entx) = E_In_Parameter)
  205.                         and then
  206.                           (Locx < Loce
  207.                              or else
  208.                            Locx > Source_Last
  209.                                     (Source_Index (Current_Sem_Unit)))
  210.                      then
  211.                         Set_Address_Clause (Ent, N);
  212.                         return;
  213.                      end if;
  214.                   end;
  215.                end if;
  216.  
  217.                Error_Msg_NE ("invalid address clause for &!", N, Ent);
  218.                Error_Msg_N  ("must be prior defined constant!", N);
  219.             end if;
  220.          end Address;
  221.  
  222.          ---------------
  223.          -- Alignment --
  224.          ---------------
  225.  
  226.          --  Alignment attribute definition clause
  227.  
  228.          when Attribute_Alignment => Alignment : declare
  229.             Align : Uint := Static_Integer (Expr);
  230.  
  231.          begin
  232.             Note_Feature (New_Representation_Clauses, Sloc (N));
  233.  
  234.             if not Is_Type (Ent)
  235.               and then Ekind (Ent) /= E_Variable
  236.               and then Ekind (Ent) /= E_Constant
  237.             then
  238.                Error_Msg_N ("alignment cannot be given for &", Nam);
  239.  
  240.             elsif Has_Alignment_Clause (Ent) then
  241.                Error_Msg_Sloc := Sloc (Alignment_Clause (Ent));
  242.                Error_Msg_N ("alignment clause previously given#", N);
  243.  
  244.             elsif Align /= No_Uint then
  245.                if Align < 0 then
  246.                   Error_Msg_N ("negative alignment not allowed", Expr);
  247.  
  248.                elsif Align > Maximum_Alignment then
  249.                   Error_Msg_Uint_1 := UI_From_Int (Maximum_Alignment);
  250.                   Error_Msg_N
  251.                     ("?alignment exceeds ^ (maximum allowed for target)", N);
  252.  
  253.                else
  254.                   Set_Alignment_Clause (Ent, N);
  255.                   Set_Has_Alignment_Clause (Ent);
  256.                end if;
  257.             end if;
  258.          end Alignment;
  259.  
  260.          ---------------
  261.          -- Bit_Order --
  262.          ---------------
  263.  
  264.          --  Bit_Order attribute definition clause
  265.  
  266.          when Attribute_Bit_Order => Bit_Order : declare
  267.          begin
  268.             Note_Feature (New_Representation_Clauses, Sloc (N));
  269.  
  270.             if not Is_Record_Type (Ent) then
  271.                Error_Msg_N ("& definition requires record type", Nam);
  272.  
  273.             else
  274.                Analyze (Expr);
  275.                Resolve (Expr, RTE (RE_Bit_Order));
  276.  
  277.                if Etype (Expr) = Any_Type then
  278.                   return;
  279.  
  280.                elsif not Is_Static_Expression (Expr) then
  281.                   Error_Msg_N ("& requires static expression", Expr);
  282.  
  283.                else
  284.                   if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
  285.                      Error_Msg_N ("unsupported value for & attribute", Expr);
  286.                   end if;
  287.                end if;
  288.             end if;
  289.          end Bit_Order;
  290.  
  291.          --------------------
  292.          -- Component_Size --
  293.          --------------------
  294.  
  295.          --  Component_Size attribute definition clause
  296.  
  297.          when Attribute_Component_Size => Component_Size : declare
  298.             Component_Size : constant Uint      := Static_Integer (Expr);
  299.             Btype          : constant Entity_Id := Base_Type (Ent);
  300.  
  301.          begin
  302.             Note_Feature (New_Representation_Clauses, Sloc (N));
  303.  
  304.             if Has_Component_Size_Clause (Btype) then
  305.                Error_Msg_Sloc := Sloc (Component_Size_Clause (Btype));
  306.                Error_Msg_N
  307.                  ("component size clase for& previously given#", Nam);
  308.  
  309.             elsif not Is_Array_Type (Ent) then
  310.                Error_Msg_N ("component size requires array type", Nam);
  311.  
  312.             elsif Component_Size /= No_Uint then
  313.                Check_Size (Expr, Component_Type (Btype), Component_Size);
  314.  
  315.                --  Note that Gigi is in charge of checking that the size we
  316.                --  are assigning is acceptable, and will generate the error
  317.                --  message if the size is inappropriate.
  318.  
  319.                Set_Component_Size_Clause (Btype, N);
  320.                Set_Has_Component_Size_Clause (Btype);
  321.                Set_Has_Non_Standard_Rep (Btype);
  322.             end if;
  323.          end Component_Size;
  324.  
  325.          -----------
  326.          -- Input --
  327.          -----------
  328.  
  329.          when Attribute_Input => Input : declare
  330.             Subp  : Entity_Id;
  331.  
  332.             function Has_Good_Profile (Subp : Entity_Id) return Boolean;
  333.             --  return true if the entity is a function with the good
  334.             --  profile for the input attribute.
  335.  
  336.             function Has_Good_Profile (Subp : Entity_Id) return Boolean is
  337.                F  : Entity_Id;
  338.                Ok : Boolean := False;
  339.  
  340.             begin
  341.                if Ekind (Subp) = E_Function then
  342.                   F := First_Formal (Subp);
  343.  
  344.                   if Present (F) and then No (Next_Formal (F)) then
  345.                      if Ekind (Etype (F)) = E_Anonymous_Access_Type
  346.                        and then Designated_Type (Etype (F)) =
  347.                          Class_Wide_Type (RTE (RE_Root_Stream_Type))
  348.                      then
  349.                         Ok := Base_Type (Etype (Subp)) = Base_Type (Ent);
  350.                      end if;
  351.                   end if;
  352.                end if;
  353.                return Ok;
  354.             end Has_Good_Profile;
  355.  
  356.          begin
  357.             Note_Feature (New_Representation_Clauses, Sloc (N));
  358.  
  359.             if not Is_Type (Ent) then
  360.                Error_Msg_N ("local name must be a subtype", Nam);
  361.                return;
  362.             end if;
  363.  
  364.             Subp := Current_Entity (Expr);   -- beginning of homonym chain.
  365.  
  366.             while Present (Subp) loop
  367.                exit when Has_Good_Profile (Subp);
  368.                Subp := Homonym (Subp);
  369.             end loop;
  370.  
  371.             if Present (Subp) then
  372.                Set_Entity (Expr, Subp);
  373.                Set_Etype (Expr, Etype (Subp));
  374.             else
  375.                Error_Msg_N ("incorrect expression for input attribute", Expr);
  376.                return;
  377.             end if;
  378.          end Input;
  379.  
  380.          -------------------
  381.          -- Machine_Radix --
  382.          -------------------
  383.  
  384.          --  Machine radix attribute definition clause
  385.  
  386.          when Attribute_Machine_Radix => Machine_Radix : declare
  387.             Radix : constant Uint := Static_Integer (Expr);
  388.  
  389.          begin
  390.             Note_Feature (New_Representation_Clauses, Sloc (N));
  391.  
  392.             if not Is_Decimal_Fixed_Point_Type (Ent) then
  393.                Error_Msg_N ("decimal fixed-point type expected for &", Nam);
  394.  
  395.             elsif Has_Machine_Radix_Clause (Ent) then
  396.                Error_Msg_Sloc := Sloc (Alignment_Clause (Ent));
  397.                Error_Msg_N ("machine radix clause previously given#", N);
  398.  
  399.             elsif Radix /= No_Uint then
  400.                Set_Has_Machine_Radix_Clause (Ent);
  401.                Set_Has_Non_Standard_Rep (Ent);
  402.  
  403.                if Radix = 2 then
  404.                   null;
  405.                elsif Radix = 10 then
  406.                   Set_Machine_Radix_10 (Ent);
  407.                else
  408.                   Error_Msg_N ("machine radix value must be 2 or 10", Expr);
  409.                end if;
  410.             end if;
  411.          end Machine_Radix;
  412.  
  413.          ------------
  414.          -- Output --
  415.          ------------
  416.  
  417.          when Attribute_Output => Output : declare
  418.             Subp        : Entity_Id;
  419.  
  420.             function Has_Good_Profile (Subp : Entity_Id) return Boolean;
  421.             --  return true if the entity is a procedure with the good
  422.             --  profile for the output attribute.
  423.  
  424.             function Has_Good_Profile (Subp : Entity_Id) return Boolean is
  425.                F  : Entity_Id;
  426.                Ok : Boolean := False;
  427.  
  428.             begin
  429.                if Ekind (Subp) = E_Procedure then
  430.                   F := First_Formal (Subp);
  431.  
  432.                   if Present (F) then
  433.                      if Ekind (Etype (F)) = E_Anonymous_Access_Type
  434.                        and then Designated_Type (Etype (F)) =
  435.                          Class_Wide_Type (RTE (RE_Root_Stream_Type))
  436.                      then
  437.                         F := Next_Formal (F);
  438.                         Ok :=  Present (F)
  439.                           and then Parameter_Mode (F) = E_In_Parameter
  440.                           and then Base_Type (Etype (F)) = Base_Type (Ent)
  441.                           and then No (Next_Formal (F));
  442.                      end if;
  443.                   end if;
  444.                end if;
  445.                return Ok;
  446.             end Has_Good_Profile;
  447.  
  448.          begin
  449.             Note_Feature (New_Representation_Clauses, Sloc (N));
  450.  
  451.             if not Is_Type (Ent) then
  452.                Error_Msg_N ("local name must be a subtype", Nam);
  453.                return;
  454.             end if;
  455.  
  456.             Subp := Current_Entity (Expr);   -- beginning of homonym chain.
  457.  
  458.             while Present (Subp) loop
  459.                exit when Has_Good_Profile (Subp);
  460.                Subp := Homonym (Subp);
  461.             end loop;
  462.  
  463.             if Present (Subp) then
  464.                Set_Entity (Expr, Subp);
  465.                Set_Etype (Expr, Etype (Subp));
  466.             else
  467.                Error_Msg_N ("incorrect expression for read attribute", Expr);
  468.                return;
  469.             end if;
  470.          end Output;
  471.  
  472.          ----------
  473.          -- Read --
  474.          ----------
  475.  
  476.          when Attribute_Read => Read : declare
  477.             Subp        : Entity_Id;
  478.  
  479.             function Has_Good_Profile (Subp : Entity_Id) return Boolean;
  480.             --  return true if the entity is a procedure with the good
  481.             --  profile for the read attribute.
  482.  
  483.             function Has_Good_Profile (Subp : Entity_Id) return Boolean is
  484.                F     : Entity_Id;
  485.                Ok    : Boolean := False;
  486.  
  487.             begin
  488.                if Ekind (Subp) = E_Procedure then
  489.                   F := First_Formal (Subp);
  490.  
  491.                   if Present (F) then
  492.                      if Ekind (Etype (F)) = E_Anonymous_Access_Type
  493.                        and then Designated_Type (Etype (F)) =
  494.                         Class_Wide_Type (RTE (RE_Root_Stream_Type))
  495.                      then
  496.                         F := Next_Formal (F);
  497.                         Ok :=  Present (F)
  498.                           and then Parameter_Mode (F) = E_Out_Parameter
  499.                           and then Base_Type (Etype (F)) = Base_Type (Ent)
  500.                           and then No (Next_Formal (F));
  501.                      end if;
  502.                   end if;
  503.                end if;
  504.                return Ok;
  505.             end Has_Good_Profile;
  506.  
  507.          begin
  508.             Note_Feature (New_Representation_Clauses, Sloc (N));
  509.  
  510.             if not Is_Type (Ent) then
  511.                Error_Msg_N ("local name must be a subtype", Nam);
  512.                return;
  513.             end if;
  514.  
  515.             Subp := Current_Entity (Expr);   -- beginning of homonym chain.
  516.  
  517.             while Present (Subp) loop
  518.                exit when Has_Good_Profile (Subp);
  519.                Subp := Homonym (Subp);
  520.             end loop;
  521.  
  522.             if Present (Subp) then
  523.                Set_Entity (Expr, Subp);
  524.                Set_Etype (Expr, Etype (Subp));
  525.             else
  526.                Error_Msg_N ("incorrect expression for read attribute", Expr);
  527.                return;
  528.             end if;
  529.          end Read;
  530.  
  531.          ----------
  532.          -- Size --
  533.          ----------
  534.  
  535.          --  Size attribute definition clause
  536.  
  537.          when Attribute_Size => Size : declare
  538.             Size : constant Uint := Static_Integer (Expr);
  539.  
  540.          begin
  541.             if Has_Size_Clause (Ent) then
  542.                Error_Msg_N ("size already given for &", Nam);
  543.  
  544.             elsif not Is_Type (Ent)
  545.               and then Ekind (Ent) /= E_Variable
  546.               and then Ekind (Ent) /= E_Constant
  547.             then
  548.                Error_Msg_N ("size cannot be given for &", Nam);
  549.  
  550.             elsif Size /= No_Uint then
  551.                --  Check size, note that Gigi is in charge of checking
  552.                --  that the size of an array or record type is OK.
  553.  
  554.                Check_Size (Expr, Ent, Size);
  555.                Set_Esize (Ent, Size);
  556.                Set_Has_Size_Clause (Ent);
  557.             end if;
  558.          end Size;
  559.  
  560.          -----------
  561.          -- Small --
  562.          -----------
  563.  
  564.          --  Small attribute definition clause
  565.  
  566.          when Attribute_Small => Small : declare
  567.             Int_Type      : Entity_Id;
  568.             Implicit_Base : constant Entity_Id := Base_Type (Ent);
  569.             Small         : Ureal;
  570.             Size_Min      : Nat;
  571.  
  572.          begin
  573.             Analyze (Expr);
  574.             Resolve (Expr, Any_Real);
  575.  
  576.             if Etype (Expr) = Any_Type then
  577.                return;
  578.  
  579.             elsif not Is_Static_Expression (Expr) then
  580.                Error_Msg_N ("small requires static expression", Expr);
  581.                return;
  582.  
  583.             else
  584.                Small := Expr_Value_R (Expr);
  585.             end if;
  586.  
  587.             if not Is_Ordinary_Fixed_Point_Type (Ent) then
  588.                Error_Msg_N
  589.                  ("small requires an ordinary fixed point type", Nam);
  590.  
  591.             elsif Has_Small_Clause (Ent) then
  592.                Error_Msg_N ("small already given for &", Nam);
  593.  
  594.             elsif Small < Ureal_Fine_Delta then
  595.                Error_Msg_N
  596.                  ("small value must not be less than Fine_Delta", Nam);
  597.  
  598.             elsif Small > Delta_Value (Ent) then
  599.                Error_Msg_N
  600.                  ("small value must not be greater then delta value", Nam);
  601.  
  602.             else
  603.                Set_Small_Value (Ent, Small);
  604.                Set_Small_Value (Implicit_Base, Small);
  605.                Set_Has_Small_Clause (Ent);
  606.                Set_Has_Small_Clause (Implicit_Base);
  607.                Set_Has_Non_Standard_Rep (Ent);
  608.  
  609.                Size_Min := Minimum_Size (Implicit_Base);
  610.  
  611.                if Size_Min <=  8 then
  612.                   Set_Esize (Implicit_Base, Uint_8);
  613.  
  614.                elsif Size_Min <= 16 then
  615.                   Set_Esize (Implicit_Base, Uint_16);
  616.  
  617.                elsif Size_Min <= 32 then
  618.                   Set_Esize (Implicit_Base, Uint_32);
  619.  
  620.                elsif Size_Min <= 64 then
  621.                   Set_Esize (Implicit_Base, Uint_64);
  622.  
  623.                else
  624.                   Set_Esize (Implicit_Base, Uint_64);
  625.                   Error_Msg_N
  626.                     ("fixed type requires too many bits", Nam);
  627.                end if;
  628.  
  629.                --  If previous size clause given, then simply check that
  630.                --  it is consistent with the new small value given.
  631.  
  632.                if Has_Size_Clause (Ent) then
  633.                   if Esize (Ent) < Minimum_Size (Ent) then
  634.                      Error_Msg_N
  635.                        ("small value incompatible with previously given size",
  636.                         Nam);
  637.                   end if;
  638.  
  639.                --  If no previous size clause, then size of first subtype
  640.                --  is set to the size of the implicit base type.
  641.  
  642.                else
  643.                   Set_Esize (Ent, Esize (Implicit_Base));
  644.                end if;
  645.             end if;
  646.          end Small;
  647.  
  648.          ------------------
  649.          -- Storage_Size --
  650.          ------------------
  651.  
  652.          --  Storage_Size attribute definition clause
  653.  
  654.          when Attribute_Storage_Size => Storage_Size : declare
  655.             Btype : constant Entity_Id := Base_Type (Ent);
  656.  
  657.          begin
  658.             if Has_Storage_Size_Clause (Btype) then
  659.                Error_Msg_N ("storage size already given for &", Nam);
  660.  
  661.             elsif not Is_Access_Type (Ent)
  662.               and then Ekind (Ent) /= E_Task_Type
  663.             then
  664.                Error_Msg_N ("storage size cannot be given for &", Nam);
  665.  
  666.             else
  667.                Analyze (Expr);
  668.                Resolve (Expr, Any_Integer);
  669.  
  670.                if Is_Access_Type (Ent)
  671.                  and then Present (Associated_Storage_Pool (Ent))
  672.                then
  673.                   Error_Msg_N ("storage pool already given for &", Nam);
  674.                   return;
  675.                else
  676.                   Set_Has_Storage_Size_Clause (Btype);
  677.                end if;
  678.             end if;
  679.          end Storage_Size;
  680.  
  681.          ------------------
  682.          -- Storage_Pool --
  683.          ------------------
  684.  
  685.          --  Storage_Pool attribute definition clause
  686.  
  687.          when Attribute_Storage_Pool => Storage_Pool : declare
  688.             Pool : Entity_Id;
  689.  
  690.          begin
  691.             Note_Feature (New_Representation_Clauses, Sloc (N));
  692.             Note_Feature (User_Defined_Storage_Pools, Sloc (N));
  693.  
  694.             if Ekind (Ent) /= E_Access_Type
  695.               and then Ekind (Ent) /= E_General_Access_Type
  696.             then
  697.                Error_Msg_N (
  698.                  "storage pool can only be given for access types", Nam);
  699.                return;
  700.  
  701.             elsif Has_Storage_Size_Clause (Ent) then
  702.                Error_Msg_N ("storage size already given for &", Nam);
  703.                return;
  704.  
  705.             elsif Present (Associated_Storage_Pool (Ent)) then
  706.                Error_Msg_N ("storage pool already given for &", Nam);
  707.                return;
  708.             end if;
  709.  
  710.             Analyze (Expr);
  711.             Resolve (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
  712.  
  713.             if Is_Entity_Name (Expr) then
  714.                Pool := Associated_Storage_Pool (Entity (Prefix (Expr)));
  715.  
  716.                if Present (Etype (Pool))
  717.                  and then Etype (Pool) /= RTE (RE_Stack_Bounded_Pool)
  718.                  and then Etype (Pool) /= RTE (RE_Unbounded_Reclaim_Pool)
  719.                then
  720.                   Set_Associated_Storage_Pool (Ent, Pool);
  721.                else
  722.                   Error_Msg_N ("Non sharable GNAT Pool", Expr);
  723.                end if;
  724.  
  725.             else
  726.                Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
  727.                return;
  728.             end if;
  729.          end Storage_Pool;
  730.  
  731.          -----------
  732.          -- Write --
  733.          -----------
  734.  
  735.          --  Write attribute definition clause
  736.          --  check for class-wide case will be performed later
  737.  
  738.          when Attribute_Write => Write : declare
  739.             Subp        : Entity_Id;
  740.  
  741.             function Has_Good_Profile (Subp : Entity_Id) return Boolean;
  742.             --  return true if the entity is a procedure with the good
  743.             --  profile for the write attribute.
  744.  
  745.             function Has_Good_Profile (Subp : Entity_Id) return Boolean is
  746.                F     : Entity_Id;
  747.                Ok    : Boolean := False;
  748.  
  749.             begin
  750.                if Ekind (Subp) = E_Procedure then
  751.                   F := First_Formal (Subp);
  752.  
  753.                   if Present (F) then
  754.                      if Ekind (Etype (F)) = E_Anonymous_Access_Type
  755.                        and then Designated_Type (Etype (F)) =
  756.                          Class_Wide_Type (RTE (RE_Root_Stream_Type))
  757.                      then
  758.                         F := Next_Formal (F);
  759.                         Ok :=  Present (F)
  760.                           and then Parameter_Mode (F) = E_In_Parameter
  761.                           and then Base_Type (Etype (F)) = Base_Type (Ent)
  762.                           and then No (Next_Formal (F));
  763.                      end if;
  764.                   end if;
  765.                end if;
  766.                return Ok;
  767.             end Has_Good_Profile;
  768.  
  769.          begin
  770.             Note_Feature (New_Representation_Clauses, Sloc (N));
  771.  
  772.             if not Is_Type (Ent) then
  773.                Error_Msg_N ("local name must be a subtype", Nam);
  774.                return;
  775.             end if;
  776.  
  777.             Subp := Current_Entity (Expr);   -- beginning of homonym chain.
  778.  
  779.             while Present (Subp) loop
  780.                exit when Has_Good_Profile (Subp);
  781.                Subp := Homonym (Subp);
  782.             end loop;
  783.  
  784.             if Present (Subp) then
  785.                Set_Entity (Expr, Subp);
  786.                Set_Etype (Expr, Etype (Subp));
  787.             else
  788.                Error_Msg_N ("incorrect expression for write attribute", Expr);
  789.                return;
  790.             end if;
  791.          end Write;
  792.  
  793.          --  All other attributes cannot be set
  794.  
  795.          when others =>
  796.             Error_Msg_N
  797.               ("attribute& cannot be set with definition clause", N);
  798.  
  799.       end case;
  800.    end Analyze_Attribute_Definition_Clause;
  801.  
  802.    ----------------------------
  803.    -- Analyze_Code_Statement --
  804.    ----------------------------
  805.  
  806.    procedure Analyze_Code_Statement (N : Node_Id) is
  807.    begin
  808.       Unimplemented (N, "code statement");
  809.    end Analyze_Code_Statement;
  810.  
  811.    -----------------------------------------------
  812.    -- Analyze_Enumeration_Representation_Clause --
  813.    -----------------------------------------------
  814.  
  815.    procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
  816.       Loc      : constant Source_Ptr := Sloc (N);
  817.       Ident    : constant Node_Id    := Identifier (N);
  818.       Aggr     : constant Node_Id    := Array_Aggregate (N);
  819.       Enumtype : Entity_Id;
  820.       Elit     : Entity_Id;
  821.       Expr     : Node_Id;
  822.       Assoc    : Node_Id;
  823.       Choice   : Node_Id;
  824.       Val      : Uint;
  825.       Err      : Boolean := False;
  826.  
  827.       Lo  : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
  828.       Hi  : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
  829.       Min : Uint;
  830.       Max : Uint;
  831.  
  832.    begin
  833.       --  First some basic error checks
  834.  
  835.       Find_Type (Ident);
  836.       Enumtype := Entity (Ident);
  837.  
  838.       if not Is_Enumeration_Type (Enumtype) then
  839.          Error_Msg_NE ("enumeration type required, found}", Ident, Enumtype);
  840.          return;
  841.       end if;
  842.  
  843.       if not Is_First_Subtype (Enumtype) then
  844.          Error_Msg_N ("cannot give enumeration rep clause for subtype", Ident);
  845.          return;
  846.  
  847.       elsif Has_Enumeration_Rep_Clause (Enumtype) then
  848.          Error_Msg_N ("duplicate enumeration rep clause ignored", N);
  849.          return;
  850.  
  851.       elsif Already_Frozen (Enumtype, Ident) then
  852.          return;
  853.  
  854.       elsif Root_Type (Enumtype) = Standard_Character
  855.         or else Root_Type (Enumtype) = Standard_Wide_Character
  856.         or else Root_Type (Enumtype) = Standard_Boolean
  857.       then
  858.          Error_Msg_N ("enumeration rep clause not allowed for this type", N);
  859.  
  860.       else
  861.          Set_Has_Enumeration_Rep_Clause (Enumtype);
  862.          Set_Has_Non_Standard_Rep (Enumtype);
  863.       end if;
  864.  
  865.       --  Now we process the aggregate. Note that we don't use the normal
  866.       --  aggregate code for this purpose, because we don't want any of the
  867.       --  normal expansion activities, and a number of special semantic
  868.       --  rules apply (including the component type being any integer type)
  869.  
  870.       --  Badent signals that we found some incorrect entries processing
  871.       --  the list. The final checks for completeness and ordering are
  872.       --  skipped in this case.
  873.  
  874.       Elit := First_Literal (Enumtype);
  875.  
  876.       --  First the positional entries if any
  877.  
  878.       if Present (Expressions (Aggr)) then
  879.          Expr := First (Expressions (Aggr));
  880.          while Present (Expr) loop
  881.  
  882.             if No (Elit) then
  883.                Error_Msg_N ("too many entries in aggregate", Expr);
  884.                return;
  885.             end if;
  886.  
  887.             Val := Static_Integer (Expr);
  888.  
  889.             if Val = No_Uint then
  890.                Err := True;
  891.  
  892.             elsif Val < Lo or else Hi < Val then
  893.                Error_Msg_N ("value outside permitted range", Expr);
  894.                Err := True;
  895.             end if;
  896.  
  897.             Set_Enumeration_Rep (Elit, Val);
  898.             Set_Enumeration_Rep_Expr (Elit, Expr);
  899.             Expr := Next (Expr);
  900.             Elit := Next (Elit);
  901.          end loop;
  902.       end if;
  903.  
  904.       --  Now process the named entries if present
  905.  
  906.       if Present (Component_Associations (Aggr)) then
  907.          Assoc := First (Component_Associations (Aggr));
  908.          while Present (Assoc) loop
  909.             Choice := First (Choices (Assoc));
  910.  
  911.             if Present (Next (Choice)) then
  912.                Error_Msg_N
  913.                  ("multiple choice not allowed here", Next (Choice));
  914.                Err := True;
  915.             end if;
  916.  
  917.             if Nkind (Choice) = N_Others_Choice then
  918.                Error_Msg_N ("others choice not allowed here", Choice);
  919.                Err := True;
  920.  
  921.             elsif Nkind (Choice) = N_Range then
  922.                --  ??? should allow zero/one element range here
  923.                Error_Msg_N ("range not allowed here", Choice);
  924.                Err := True;
  925.  
  926.             else
  927.                Analyze (Choice);
  928.                Resolve (Choice, Enumtype);
  929.  
  930.                if Is_Entity_Name (Choice)
  931.                  and then Is_Type (Entity (Choice))
  932.                then
  933.                   Error_Msg_N ("subtype name not allowed here", Choice);
  934.                   Err := True;
  935.                   --  ??? should allow static subtype with zero/one entry
  936.  
  937.                elsif Etype (Choice) = Base_Type (Enumtype) then
  938.                   if not Is_Static_Expression (Choice) then
  939.                      Error_Msg_N
  940.                        ("non-static expression used for choice", Choice);
  941.                      Err := True;
  942.                   else
  943.                      Elit := Expr_Value_E (Choice);
  944.  
  945.                      if Present (Enumeration_Rep_Expr (Elit)) then
  946.                         Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit));
  947.                         Error_Msg_NE
  948.                           ("representation for& previously given#",
  949.                            Choice, Elit);
  950.                         Err := True;
  951.                      end if;
  952.  
  953.                      Set_Enumeration_Rep_Expr (Elit, Choice);
  954.  
  955.                      Val := Static_Integer (Expression (Assoc));
  956.  
  957.                      if Val = No_Uint then
  958.                         Err := True;
  959.                      elsif Val < Lo or else Hi < Val then
  960.                         Error_Msg_N ("value outside permitted range", Expr);
  961.                         Err := True;
  962.                      end if;
  963.  
  964.                      Set_Enumeration_Rep (Elit, Val);
  965.                   end if;
  966.                end if;
  967.             end if;
  968.  
  969.             Assoc := Next (Assoc);
  970.          end loop;
  971.       end if;
  972.  
  973.       --  Aggregate is fully processed. Now we check that a full set of
  974.       --  representations was given, and that they are in range and in order.
  975.       --  These checks are only done if no other errors occurred.
  976.  
  977.       if not Err then
  978.          Min  := No_Uint;
  979.          Max  := No_Uint;
  980.  
  981.          Elit := First_Literal (Enumtype);
  982.          while Present (Elit) loop
  983.             if No (Enumeration_Rep_Expr (Elit)) then
  984.                Error_Msg_NE ("missing representation for&!", N, Elit);
  985.  
  986.             else
  987.                Val := Enumeration_Rep (Elit);
  988.  
  989.                if Min = No_Uint then
  990.                   Min := Val;
  991.                end if;
  992.  
  993.                if Val /= No_Uint then
  994.                   if Max /= No_Uint and then Val <= Max then
  995.                      Error_Msg_NE
  996.                        ("enumeration value for& not ordered!",
  997.                                        Enumeration_Rep_Expr (Elit), Elit);
  998.                   end if;
  999.  
  1000.                   Max := Val;
  1001.                end if;
  1002.  
  1003.             end if;
  1004.  
  1005.             Elit := Next (Elit);
  1006.          end loop;
  1007.       end if;
  1008.  
  1009.       if Has_Size_Clause (Enumtype) then
  1010.          if Esize (Enumtype) >= Minimum_Size (Enumtype) then
  1011.             return;
  1012.          else
  1013.             Error_Msg_N ("previously given size is too small", N);
  1014.          end if;
  1015.       end if;
  1016.  
  1017.       --  If we don't have a given size, or if the size given was too
  1018.       --  small, then compute an appropriate size for the values given.
  1019.  
  1020.       Determine_Enum_Representation (Enumtype);
  1021.  
  1022.    end Analyze_Enumeration_Representation_Clause;
  1023.  
  1024.    ----------------------------
  1025.    -- Analyze_Free_Statement --
  1026.    ----------------------------
  1027.  
  1028.    procedure Analyze_Free_Statement (N : Node_Id) is
  1029.    begin
  1030.       Analyze (Expression (N));
  1031.    end Analyze_Free_Statement;
  1032.  
  1033.    ------------------------------------------
  1034.    -- Analyze_Record_Representation_Clause --
  1035.    ------------------------------------------
  1036.  
  1037.    procedure Analyze_Record_Representation_Clause (N : Node_Id) is
  1038.       Loc     : constant Source_Ptr := Sloc (N);
  1039.       Ident   : constant Node_Id    := Identifier (N);
  1040.       Rectype : Entity_Id;
  1041.       Mod_Val : Uint;
  1042.       CC      : Node_Id;
  1043.       Posit   : Uint;
  1044.       Fbit    : Uint;
  1045.       Lbit    : Uint;
  1046.       Adjust  : Uint;
  1047.       Hbit    : Uint := Uint_0;
  1048.       Comp    : Entity_Id;
  1049.  
  1050.    begin
  1051.       --  First some basic error checks
  1052.  
  1053.       Find_Type (Ident);
  1054.       Rectype := Entity (Ident);
  1055.  
  1056.       if not Is_Record_Type (Rectype) then
  1057.          Error_Msg_NE ("record type required, found}", Ident, Rectype);
  1058.          return;
  1059.       end if;
  1060.  
  1061.       if not Is_First_Subtype (Rectype) then
  1062.          Error_Msg_N ("cannot give record rep clause for subtype", Ident);
  1063.          return;
  1064.  
  1065.       elsif Has_Record_Rep_Clause (Rectype) then
  1066.          Error_Msg_N ("duplicate record rep clause ignored", N);
  1067.          return;
  1068.  
  1069.       elsif Already_Frozen (Rectype, Ident) then
  1070.          return;
  1071.  
  1072.       else
  1073.          Set_Has_Record_Rep_Clause (Rectype);
  1074.          Set_Has_Non_Standard_Rep  (Rectype);
  1075.          Set_Has_Specified_Layout  (Rectype);
  1076.       end if;
  1077.  
  1078.       if Present (Mod_Clause (N)) then
  1079.          Mod_Val := Static_Integer (Expression (Mod_Clause (N)));
  1080.       end if;
  1081.  
  1082.       --  Clear any existing component clauses for the type (this happens
  1083.       --  with derived types, where we are now overriding the original)
  1084.  
  1085.       Comp := First_Entity (Rectype);
  1086.       while Present (Comp) loop
  1087.          if Ekind (Comp) = E_Component
  1088.            or else Ekind (Comp) = E_Discriminant
  1089.          then
  1090.             Set_Component_Clause (Comp, Empty);
  1091.          end if;
  1092.  
  1093.          Comp := Next_Entity (Comp);
  1094.       end loop;
  1095.  
  1096.       --  Process the component clauses
  1097.  
  1098.       CC := First (Component_Clauses (N));
  1099.  
  1100.       while Present (CC) loop
  1101.  
  1102.          Posit := Static_Integer (Position  (CC));
  1103.          Fbit  := Static_Integer (First_Bit (CC));
  1104.          Lbit  := Static_Integer (Last_Bit  (CC));
  1105.  
  1106.          if Posit /= No_Uint
  1107.           and then Fbit /= No_Uint
  1108.           and then Lbit /= No_Uint
  1109.          then
  1110.  
  1111.             if Posit < 0 then
  1112.                Error_Msg_N ("position cannot be negative", Position (CC));
  1113.  
  1114.             elsif Fbit < 0 then
  1115.                Error_Msg_N ("first bit cannot be negative", First_Bit (CC));
  1116.  
  1117.             --  Values look OK, so find the corresponding record component
  1118.  
  1119.             else
  1120.                Comp := First_Entity (Rectype);
  1121.                while Present (Comp) loop
  1122.                   exit when Chars (Comp) = Chars (Component_Name (CC));
  1123.                   Comp := Next_Entity (Comp);
  1124.                end loop;
  1125.  
  1126.                if No (Comp) then
  1127.                   Error_Msg_N
  1128.                     ("component clause is for non-existent field", N);
  1129.  
  1130.                elsif Present (Component_Clause (Comp)) then
  1131.                   Error_Msg_Sloc := Sloc (Component_Clause (Comp));
  1132.                   Error_Msg_N ("component clause previously given#", CC);
  1133.  
  1134.                else
  1135.                   --  Update Fbit and Lbit to the actual bit number.
  1136.  
  1137.                   Fbit := Fbit + UI_From_Int (System_Storage_Unit) * Posit;
  1138.                   Lbit := Lbit + UI_From_Int (System_Storage_Unit) * Posit;
  1139.  
  1140.                   if Has_Size_Clause (Rectype)
  1141.                     and then Esize (Rectype) <= Lbit
  1142.                   then
  1143.                      Error_Msg_N ("bit number out of range of specified size",
  1144.                        Last_Bit (CC));
  1145.                   else
  1146.                      Set_Component_Clause (Comp, CC);
  1147.                      Set_Component_First_Bit (Comp, Fbit);
  1148.                      Set_Esize (Comp, 1 + (Lbit - Fbit));
  1149.  
  1150.                      if Hbit < Lbit then
  1151.                         Hbit := Lbit;
  1152.                      end if;
  1153.  
  1154.                      Check_Size (Component_Name (CC),
  1155.                        Etype (Comp), Esize (Comp));
  1156.  
  1157.                      if Esize (Comp) < 0 then
  1158.                         Error_Msg_N ("component size is negative", CC);
  1159.                      end if;
  1160.                   end if;
  1161.                end if;
  1162.             end if;
  1163.          end if;
  1164.  
  1165.          CC := Next (CC);
  1166.       end loop;
  1167.  
  1168.       --  Now that we have processed all the component clauses, check for
  1169.       --  overlap. We have to leave this till last, since the components
  1170.       --  can appear in any arbitrary order in the representation clause.
  1171.  
  1172.       Overlap_Check : declare
  1173.          C1_Ent, C2_Ent : Entity_Id;
  1174.          --  Entities of components being checked for overlap
  1175.  
  1176.          Clist : Node_Id;
  1177.          --  Component_List node whose Component_Items are being checked
  1178.  
  1179.          Citem : Node_Id;
  1180.          --  Component being checked
  1181.  
  1182.       begin
  1183.          C1_Ent := First_Entity (Rectype);
  1184.  
  1185.          --  Loop through all components in record. For each component check
  1186.          --  for overlap with any of the preceding elements on the component
  1187.          --  list containing the component, and also, if the component is in
  1188.          --  a variant, check against components outside the case structure.
  1189.          --  This latter test is repeated recursively up the variant tree.
  1190.  
  1191.          Main_Component_Loop : while Present (C1_Ent) loop
  1192.             if Ekind (C1_Ent) /= E_Component
  1193.               and then Ekind (C1_Ent) /= E_Discriminant
  1194.             then
  1195.                goto Continue_Main_Component_Loop;
  1196.             end if;
  1197.  
  1198.             Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
  1199.  
  1200.             --  Loop through component lists that need checking. We check the
  1201.             --  current component list and all lists in variants above us.
  1202.  
  1203.             Component_List_Loop : loop
  1204.  
  1205.                --  Loop through items in one component list or in the
  1206.                --  discriminant specification list.
  1207.  
  1208.                if Nkind (Clist) = N_Full_Type_Declaration then
  1209.  
  1210.                   if Present (Discriminant_Specifications (Clist)) then
  1211.                      Citem := First (Discriminant_Specifications (Clist));
  1212.                   else
  1213.                      Citem := Empty;
  1214.                   end if;
  1215.  
  1216.                else
  1217.                   Citem := First (Component_Items (Clist));
  1218.                end if;
  1219.  
  1220.                Component_Loop : while Present (Citem) loop
  1221.                   if Nkind (Citem) = N_Component_Declaration
  1222.                     or else Nkind (Citem) = N_Discriminant_Specification
  1223.                   then
  1224.                      C2_Ent := Defining_Identifier (Citem);
  1225.  
  1226.                      --  Exit loop if we hit current component (saves a factor
  1227.                      --  of 2 comparisons, since we only compare one direction)
  1228.  
  1229.                      exit Component_Loop when C1_Ent = C2_Ent;
  1230.  
  1231.                      --  Do the comparison
  1232.  
  1233.                      if Present (Component_Clause (C1_Ent))
  1234.                        and then Present (Component_Clause (C2_Ent))
  1235.                      then
  1236.                         declare
  1237.                            S1 : constant Uint := Component_First_Bit (C1_Ent);
  1238.                            S2 : constant Uint := Component_First_Bit (C2_Ent);
  1239.                            E1 : constant Uint := S1 + Esize (C1_Ent);
  1240.                            E2 : constant Uint := S2 + Esize (C2_Ent);
  1241.  
  1242.                         begin
  1243.                            if E2 <= S1 or else E1 <= S2 then
  1244.                               null;
  1245.                            else
  1246.                               Error_Msg_Node_2 :=
  1247.                                 Component_Name (Component_Clause (C2_Ent));
  1248.                               Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
  1249.                               Error_Msg_Node_1 :=
  1250.                                 Component_Name (Component_Clause (C1_Ent));
  1251.                               Error_Msg_N
  1252.                                 ("component& overlaps & #",
  1253.                                  Component_Name (Component_Clause (C1_Ent)));
  1254.                            end if;
  1255.                         end;
  1256.                      end if;
  1257.                   end if;
  1258.  
  1259.                   Citem := Next (Citem);
  1260.                end loop Component_Loop;
  1261.  
  1262.                --  Check for variants above us (the parent of the Clist can be
  1263.                --  a variant, in which case its parent is a variant part, and
  1264.                --  the parent of the variant part is a component list whose
  1265.                --  components must all be checked against the current component
  1266.                --  for overlap.
  1267.  
  1268.                if Nkind (Parent (Clist)) = N_Variant then
  1269.                   Clist := Parent (Parent (Parent (Clist)));
  1270.  
  1271.                --  Check for possible discriminant part in record, this is
  1272.                --  treated essentially as another level in the recursion. For
  1273.                --  this case we have the parent of the component list is the
  1274.                --  record definition, and its parent is the full type
  1275.                --  declaration which contains the discriminant specifications.
  1276.  
  1277.                elsif Nkind (Parent (Clist)) = N_Record_Definition then
  1278.                   Clist := Parent (Parent ((Clist)));
  1279.  
  1280.                --  If neither of these two cases, we are at the top of the tree
  1281.  
  1282.                else
  1283.                   exit Component_List_Loop;
  1284.                end if;
  1285.             end loop Component_List_Loop;
  1286.  
  1287.             <<Continue_Main_Component_Loop>>
  1288.                C1_Ent := Next_Entity (C1_Ent);
  1289.  
  1290.          end loop Main_Component_Loop;
  1291.  
  1292.       end Overlap_Check;
  1293.  
  1294.       Set_Esize (Rectype, Hbit + 1);
  1295.  
  1296.    end Analyze_Record_Representation_Clause;
  1297.  
  1298.    ----------------
  1299.    -- Check_Size --
  1300.    ----------------
  1301.  
  1302.    procedure Check_Size (N : Node_Id; T : Entity_Id; Siz : Uint) is
  1303.       UT : constant Entity_Id := Underlying_Type (T);
  1304.       M  : Uint;
  1305.  
  1306.    begin
  1307.       --  Immediate return if size is same as standard size or if composite
  1308.       --  item with no size available (i.e. none was given explicitly) or
  1309.       --  generic type, or type with previous errors.
  1310.  
  1311.       if No (UT) or else Esize (UT) = 0 or else Siz = Esize (UT) then
  1312.          return;
  1313.  
  1314.       --  If type has record representation clause, the saved size if the
  1315.       --  mimimum size.
  1316.  
  1317.       elsif Is_Record_Type (UT) and then Has_Record_Rep_Clause (UT) then
  1318.          if Siz < Esize (UT) then
  1319.             Error_Msg_Uint_1 := Esize (UT);
  1320.             Error_Msg_NE ("size for& too small, minimum allowed is ^", N, T);
  1321.          end if;
  1322.  
  1323.       --  Types for which the only permitted size is the standard size
  1324.  
  1325.       elsif Is_Floating_Point_Type (UT)
  1326.         or else Is_Access_Type (UT)
  1327.         or else Is_Composite_Type (UT)
  1328.       then
  1329.          Error_Msg_Uint_1 := Esize (UT);
  1330.          Error_Msg_NE ("incorrect size for&, must be exactly ^", N, T);
  1331.  
  1332.       --  For remaining types, maximum size is Long_Long_Integer size
  1333.  
  1334.       elsif Siz > Standard_Long_Long_Integer_Size then
  1335.          Error_Msg_Uint_1 := UI_From_Int (Standard_Long_Long_Integer_Size);
  1336.          Error_Msg_NE ("size for& too large, maximum allowed is ^", N, T);
  1337.  
  1338.       --  Cases for which a minimum check is required
  1339.  
  1340.       else
  1341.          M := UI_From_Int (Minimum_Size (UT));
  1342.  
  1343.          if Siz < M then
  1344.             Error_Msg_Uint_1 := M;
  1345.             Error_Msg_NE ("size for& too small, minimum allowed is ^", N, T);
  1346.          end if;
  1347.       end if;
  1348.    end Check_Size;
  1349.  
  1350.    ------------------
  1351.    -- Minimum_Size --
  1352.    ------------------
  1353.  
  1354.    function Minimum_Size (T : Entity_Id) return Nat is
  1355.       Lo, Hi   : Uint;
  1356.       LoR, HiR : Ureal;
  1357.       B        : Uint;
  1358.       S        : Nat;
  1359.  
  1360.       function Get_Enum_Rep (N : Node_Id) return Uint;
  1361.       --  N is an enumeration literal reference. This function returns
  1362.       --  the corresponding enumeration representation, dealing with the
  1363.       --  special case of Standard.Character or Standard.Wide_Character
  1364.       --  where no entity is present (in which case the representation
  1365.       --  is simply that of the character literal itself).
  1366.  
  1367.       function Get_Enum_Rep (N : Node_Id) return Uint is
  1368.       begin
  1369.          if Present (Entity (N)) then
  1370.             return Enumeration_Rep (Entity (N));
  1371.          else
  1372.             return UI_From_Int (Int (Char_Literal_Value (N)));
  1373.          end if;
  1374.       end Get_Enum_Rep;
  1375.  
  1376.    --  Start of processing for Minimum_Size
  1377.  
  1378.    begin
  1379.       --  Enumeration types
  1380.  
  1381.       if Is_Enumeration_Type (T) then
  1382.          if Is_Entity_Name (Type_Low_Bound (T)) then
  1383.             Lo := Get_Enum_Rep (Type_Low_Bound (T));
  1384.          else
  1385.             Lo := Get_Enum_Rep (Type_Low_Bound (Base_Type (T)));
  1386.          end if;
  1387.  
  1388.          if Is_Entity_Name (Type_High_Bound (T)) then
  1389.             Hi := Get_Enum_Rep (Type_High_Bound (T));
  1390.          else
  1391.             Hi := Get_Enum_Rep (Type_High_Bound (Base_Type (T)));
  1392.          end if;
  1393.  
  1394.       --  Integer types
  1395.  
  1396.       elsif Is_Integer_Type (T) then
  1397.          if Is_Static_Expression (Type_Low_Bound (T)) then
  1398.             Lo := Expr_Value (Type_Low_Bound (T));
  1399.          else
  1400.             Lo := Expr_Value (Type_Low_Bound (Base_Type (T)));
  1401.          end if;
  1402.  
  1403.          if Is_Static_Expression (Type_High_Bound (T)) then
  1404.             Hi := Expr_Value (Type_High_Bound (T));
  1405.          else
  1406.             Hi := Expr_Value (Type_High_Bound (Base_Type (T)));
  1407.          end if;
  1408.  
  1409.       --  Fixed-point types. We can't simply use Expr_Value to get the
  1410.       --  Corresponding_Integer_Value values of the bounds, since these
  1411.       --  do not get set till the type is frozen, and this routine can
  1412.       --  be called before the type is frozen.
  1413.  
  1414.       elsif Is_Fixed_Point_Type (T) then
  1415.          if Is_Static_Expression (Type_Low_Bound (T)) then
  1416.             LoR := Expr_Value_R (Type_Low_Bound (T));
  1417.          else
  1418.             LoR := Expr_Value_R (Type_Low_Bound (Base_Type (T)));
  1419.          end if;
  1420.  
  1421.          if Is_Static_Expression (Type_High_Bound (T)) then
  1422.             HiR := Expr_Value_R (Type_High_Bound (T));
  1423.          else
  1424.             HiR := Expr_Value_R (Type_High_Bound (Base_Type (T)));
  1425.          end if;
  1426.  
  1427.          Lo := UR_To_Uint (LoR / Small_Value (T));
  1428.          Hi := UR_To_Uint (HiR / Small_Value (T));
  1429.  
  1430.       --  No other types allowed
  1431.  
  1432.       else
  1433.          pragma Assert (False);
  1434.          null;
  1435.       end if;
  1436.  
  1437.       --  Signed case
  1438.  
  1439.       if Lo < 0 then
  1440.          S := 1;
  1441.          B := Uint_1;
  1442.  
  1443.          while Lo < -B or else Hi >= B loop
  1444.             S := S + 1;
  1445.             B := B + B;
  1446.          end loop;
  1447.  
  1448.       --  Unsigned case
  1449.  
  1450.       else
  1451.          S := 0;
  1452.          B := Uint_1;
  1453.  
  1454.          while Hi > B loop
  1455.             S := S + 1;
  1456.             B := B + B;
  1457.          end loop;
  1458.       end if;
  1459.  
  1460.       return S;
  1461.    end Minimum_Size;
  1462.  
  1463.    --------------------------------------
  1464.    -- Validate_Unchecked_Conversion --
  1465.    --------------------------------------
  1466.  
  1467.    procedure Validate_Unchecked_Conversion (N : Node_Id; Act_Unit : Entity_Id)
  1468.    is
  1469.       Source : Entity_Id;
  1470.       Target : Entity_Id;
  1471.  
  1472.       procedure No_Unconstrained_Type (T : Node_Id);
  1473.       --  Issue error if type T is an unconstrained type
  1474.  
  1475.       procedure No_Unconstrained_Type (T : Node_Id) is
  1476.       begin
  1477.          if Is_Indefinite_Subtype (T) then
  1478.             Error_Msg_NE
  1479.               ("unconstrained } not allowed in unchecked conversion",
  1480.                N, T);
  1481.          end if;
  1482.       end No_Unconstrained_Type;
  1483.  
  1484.    --  Start of processing for Validate_Unchecked_Conversion
  1485.  
  1486.    begin
  1487.       --  If we are dealing with private types, then do the check on their
  1488.       --  fully declared counterparts if the full declarations have been
  1489.       --  encountered (they don't have to be visible, but they must exist!)
  1490.  
  1491.       Source := Etype (First_Formal (Act_Unit));
  1492.  
  1493.       if Is_Private_Type (Source)
  1494.         and then Present (Underlying_Type (Source))
  1495.       then
  1496.          Source := Underlying_Type (Source);
  1497.       end if;
  1498.  
  1499.       Target := Etype (Act_Unit);
  1500.  
  1501.       if Is_Private_Type (Target)
  1502.         and then Present (Underlying_Type (Target))
  1503.       then
  1504.          Target := Underlying_Type (Target);
  1505.       end if;
  1506.  
  1507.       No_Unconstrained_Type (Source);
  1508.       No_Unconstrained_Type (Target);
  1509.  
  1510.       if Esize (Source) /= 0
  1511.         and then Esize (Target) /= 0
  1512.         and then Esize (Source) /= Esize (Target)
  1513.       then
  1514.          Error_Msg_N
  1515.            ("types for unchecked conversion have different sizes", N);
  1516.       end if;
  1517.    end Validate_Unchecked_Conversion;
  1518.  
  1519. end Sem_Ch13;
  1520.