OSDN Git Service

2009-08-28 Sebastian Pop <sebastian.pop@amd.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / i-cobol.adb
index 7dc1f85..ed5b0ab 100644 (file)
@@ -6,39 +6,37 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1992-2002 Free Software Foundation, Inc.          --
+--          Copyright (C) 1992-2009, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
--- ware  Foundation;  either version 2,  or (at your option) any later ver- --
+-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
--- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
--- for  more details.  You should have  received  a copy of the GNU General --
--- Public License  distributed with GNAT;  see file COPYING.  If not, write --
--- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
--- MA 02111-1307, USA.                                                      --
+-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
 --                                                                          --
--- As a special exception,  if other files  instantiate  generics from this --
--- unit, or you link  this unit with other files  to produce an executable, --
--- this  unit  does not  by itself cause  the resulting  executable  to  be --
--- covered  by the  GNU  General  Public  License.  This exception does not --
--- however invalidate  any other reasons why  the executable file  might be --
--- covered by the  GNU Public License.                                      --
+-- As a special exception under Section 7 of GPL version 3, you are granted --
+-- additional permissions described in the GCC Runtime Library Exception,   --
+-- version 3.1, as published by the Free Software Foundation.               --
+--                                                                          --
+-- You should have received a copy of the GNU General Public License and    --
+-- a copy of the GCC Runtime Library Exception along with this program;     --
+-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
+-- <http://www.gnu.org/licenses/>.                                          --
 --                                                                          --
 -- GNAT was originally developed  by the GNAT team at  New York University. --
 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
 --                                                                          --
 ------------------------------------------------------------------------------
 
---  The body of Interfaces.COBOL is implementation independent (i.e. the
---  same version is used with all versions of GNAT). The specialization
---  to a particular COBOL format is completely contained in the private
---  part ot the spec.
+--  The body of Interfaces.COBOL is implementation independent (i.e. the same
+--  version is used with all versions of GNAT). The specialization to a
+--  particular COBOL format is completely contained in the private part of
+--  the spec.
 
 with Interfaces; use Interfaces;
 with System;     use System;
-with Unchecked_Conversion;
+with Ada.Unchecked_Conversion;
 
 package body Interfaces.COBOL is
 
@@ -52,22 +50,22 @@ package body Interfaces.COBOL is
    subtype B8 is Byte_Array (1 .. 8);
    --  Representations for 1,2,4,8 byte binary values
 
-   function To_B1 is new Unchecked_Conversion (Integer_8,  B1);
-   function To_B2 is new Unchecked_Conversion (Integer_16, B2);
-   function To_B4 is new Unchecked_Conversion (Integer_32, B4);
-   function To_B8 is new Unchecked_Conversion (Integer_64, B8);
+   function To_B1 is new Ada.Unchecked_Conversion (Integer_8,  B1);
+   function To_B2 is new Ada.Unchecked_Conversion (Integer_16, B2);
+   function To_B4 is new Ada.Unchecked_Conversion (Integer_32, B4);
+   function To_B8 is new Ada.Unchecked_Conversion (Integer_64, B8);
    --  Conversions from native binary to external binary
 
-   function From_B1 is new Unchecked_Conversion (B1, Integer_8);
-   function From_B2 is new Unchecked_Conversion (B2, Integer_16);
-   function From_B4 is new Unchecked_Conversion (B4, Integer_32);
-   function From_B8 is new Unchecked_Conversion (B8, Integer_64);
+   function From_B1 is new Ada.Unchecked_Conversion (B1, Integer_8);
+   function From_B2 is new Ada.Unchecked_Conversion (B2, Integer_16);
+   function From_B4 is new Ada.Unchecked_Conversion (B4, Integer_32);
+   function From_B8 is new Ada.Unchecked_Conversion (B8, Integer_64);
    --  Conversions from external binary to signed native binary
 
-   function From_B1U is new Unchecked_Conversion (B1, Unsigned_8);
-   function From_B2U is new Unchecked_Conversion (B2, Unsigned_16);
-   function From_B4U is new Unchecked_Conversion (B4, Unsigned_32);
-   function From_B8U is new Unchecked_Conversion (B8, Unsigned_64);
+   function From_B1U is new Ada.Unchecked_Conversion (B1, Unsigned_8);
+   function From_B2U is new Ada.Unchecked_Conversion (B2, Unsigned_16);
+   function From_B4U is new Ada.Unchecked_Conversion (B4, Unsigned_32);
+   function From_B8U is new Ada.Unchecked_Conversion (B8, Unsigned_64);
    --  Conversions from external binary to unsigned native binary
 
    -----------------------
@@ -76,8 +74,7 @@ package body Interfaces.COBOL is
 
    function Binary_To_Decimal
      (Item   : Byte_Array;
-      Format : Binary_Format)
-      return   Integer_64;
+      Format : Binary_Format) return Integer_64;
    --  This function converts a numeric value in the given format to its
    --  corresponding integer value. This is the non-generic implementation
    --  of Decimal_Conversions.To_Decimal. The generic routine does the
@@ -85,8 +82,7 @@ package body Interfaces.COBOL is
 
    function Numeric_To_Decimal
      (Item   : Numeric;
-      Format : Display_Format)
-      return   Integer_64;
+      Format : Display_Format) return Integer_64;
    --  This function converts a numeric value in the given format to its
    --  corresponding integer value. This is the non-generic implementation
    --  of Decimal_Conversions.To_Decimal. The generic routine does the
@@ -94,8 +90,7 @@ package body Interfaces.COBOL is
 
    function Packed_To_Decimal
      (Item   : Packed_Decimal;
-      Format : Packed_Format)
-      return   Integer_64;
+      Format : Packed_Format) return Integer_64;
    --  This function converts a packed value in the given format to its
    --  corresponding integer value. This is the non-generic implementation
    --  of Decimal_Conversions.To_Decimal. The generic routine does the
@@ -107,8 +102,7 @@ package body Interfaces.COBOL is
    function To_Display
      (Item   : Integer_64;
       Format : Display_Format;
-      Length : Natural)
-      return   Numeric;
+      Length : Natural) return Numeric;
    --  This function converts the given integer value into display format,
    --  using the given format, with the length in bytes of the result given
    --  by the last parameter. This is the non-generic implementation of
@@ -118,8 +112,7 @@ package body Interfaces.COBOL is
    function To_Packed
      (Item   : Integer_64;
       Format : Packed_Format;
-      Length : Natural)
-      return   Packed_Decimal;
+      Length : Natural) return Packed_Decimal;
    --  This function converts the given integer value into packed format,
    --  using the given format, with the length in digits of the result given
    --  by the last parameter. This is the non-generic implementation of
@@ -128,15 +121,13 @@ package body Interfaces.COBOL is
 
    function Valid_Numeric
      (Item   : Numeric;
-      Format : Display_Format)
-      return   Boolean;
+      Format : Display_Format) return Boolean;
    --  This is the non-generic implementation of Decimal_Conversions.Valid
    --  for the display case.
 
    function Valid_Packed
      (Item   : Packed_Decimal;
-      Format : Packed_Format)
-      return   Boolean;
+      Format : Packed_Format) return Boolean;
    --  This is the non-generic implementation of Decimal_Conversions.Valid
    --  for the packed case.
 
@@ -146,8 +137,7 @@ package body Interfaces.COBOL is
 
    function Binary_To_Decimal
      (Item   : Byte_Array;
-      Format : Binary_Format)
-      return   Integer_64
+      Format : Binary_Format) return Integer_64
    is
       Len : constant Natural := Item'Length;
 
@@ -212,7 +202,7 @@ package body Interfaces.COBOL is
    -- Numeric_To_Decimal --
    ------------------------
 
-   --  The following assumptions are made in the coding of this routine
+   --  The following assumptions are made in the coding of this routine:
 
    --    The range of COBOL_Digits is compact and the ten values
    --    represent the digits 0-9 in sequence
@@ -225,12 +215,11 @@ package body Interfaces.COBOL is
 
    --    The COBOL_Minus_Digits set is disjoint from COBOL_Digits
 
-   --  These assumptions are true for all COBOL representations we know of.
+   --  These assumptions are true for all COBOL representations we know of
 
    function Numeric_To_Decimal
      (Item   : Numeric;
-      Format : Display_Format)
-      return   Integer_64
+      Format : Display_Format) return Integer_64
    is
       pragma Unsuppress (Range_Check);
       Sign   : COBOL_Character := COBOL_Plus;
@@ -288,8 +277,7 @@ package body Interfaces.COBOL is
 
    function Packed_To_Decimal
      (Item   : Packed_Decimal;
-      Format : Packed_Format)
-      return   Integer_64
+      Format : Packed_Format) return Integer_64
    is
       pragma Unsuppress (Range_Check);
       Result : Integer_64 := 0;
@@ -347,7 +335,7 @@ package body Interfaces.COBOL is
       --  Here a swap is needed
 
       declare
-         Len  : constant Natural := B'Length;
+         Len : constant Natural := B'Length;
 
       begin
          for J in 1 .. Len / 2 loop
@@ -449,8 +437,7 @@ package body Interfaces.COBOL is
    function To_Display
      (Item   : Integer_64;
       Format : Display_Format;
-      Length : Natural)
-      return   Numeric
+      Length : Natural) return Numeric
    is
       Result : Numeric (1 .. Length);
       Val    : Integer_64 := Item;
@@ -463,10 +450,15 @@ package body Interfaces.COBOL is
       --  Used for the nonseparate formats to embed the appropriate sign
       --  at the specified location (i.e. at Result (Loc))
 
+      -------------
+      -- Convert --
+      -------------
+
       procedure Convert (First, Last : Natural) is
-         J : Natural := Last;
+         J : Natural;
 
       begin
+         J := Last;
          while J >= First loop
             Result (J) :=
               COBOL_Character'Val
@@ -489,6 +481,10 @@ package body Interfaces.COBOL is
          raise Conversion_Error;
       end Convert;
 
+      ----------------
+      -- Embed_Sign --
+      ----------------
+
       procedure Embed_Sign (Loc : Natural) is
          Digit : Natural range 0 .. 9;
 
@@ -560,8 +556,7 @@ package body Interfaces.COBOL is
    function To_Packed
      (Item   : Integer_64;
       Format : Packed_Format;
-      Length : Natural)
-      return   Packed_Decimal
+      Length : Natural) return Packed_Decimal
    is
       Result : Packed_Decimal (1 .. Length);
       Val    : Integer_64;
@@ -571,6 +566,10 @@ package body Interfaces.COBOL is
       --  storing the result in Result (First .. Last). Raise Conversion_Error
       --  if the value is too large to fit.
 
+      -------------
+      -- Convert --
+      -------------
+
       procedure Convert (First, Last : Natural) is
          J : Natural := Last;
 
@@ -628,10 +627,13 @@ package body Interfaces.COBOL is
 
    function Valid_Numeric
      (Item   : Numeric;
-      Format : Display_Format)
-      return   Boolean
+      Format : Display_Format) return Boolean
    is
    begin
+      if Item'Length = 0 then
+         return False;
+      end if;
+
       --  All character positions except first and last must be Digits.
       --  This is true for all the formats.
 
@@ -677,8 +679,7 @@ package body Interfaces.COBOL is
 
    function Valid_Packed
      (Item   : Packed_Decimal;
-      Format : Packed_Format)
-      return   Boolean
+      Format : Packed_Format) return Boolean
    is
    begin
       case Packed_Representation is
@@ -715,18 +716,14 @@ package body Interfaces.COBOL is
       --  Note that the tests here are all compile time tests
 
       function Length (Format : Binary_Format) return Natural is
-         pragma Warnings (Off, Format);
-
+         pragma Unreferenced (Format);
       begin
          if Num'Digits <= 2 then
             return 1;
-
          elsif Num'Digits <= 4 then
             return 2;
-
          elsif Num'Digits <= 9 then
             return 4;
-
          else -- Num'Digits in 10 .. 18
             return 8;
          end if;
@@ -752,11 +749,9 @@ package body Interfaces.COBOL is
       --  Note that the tests here are all compile time checks
 
       function Length
-        (Format : Packed_Format)
-         return   Natural
+        (Format : Packed_Format) return Natural
       is
-         pragma Warnings (Off, Format);
-
+         pragma Unreferenced (Format);
       begin
          case Packed_Representation is
             when IBM =>
@@ -770,8 +765,7 @@ package body Interfaces.COBOL is
 
       function To_Binary
         (Item   : Num;
-         Format : Binary_Format)
-         return   Byte_Array
+         Format : Binary_Format) return Byte_Array
       is
       begin
          --  Note: all these tests are compile time tests
@@ -820,7 +814,6 @@ package body Interfaces.COBOL is
          pragma Unsuppress (Range_Check);
       begin
          return Binary'Integer_Value (Item);
-
       exception
          when Constraint_Error =>
             raise Conversion_Error;
@@ -832,14 +825,11 @@ package body Interfaces.COBOL is
 
       function To_Decimal
         (Item   : Byte_Array;
-         Format : Binary_Format)
-         return   Num
+         Format : Binary_Format) return Num
       is
          pragma Unsuppress (Range_Check);
-
       begin
          return Num'Fixed_Value (Binary_To_Decimal (Item, Format));
-
       exception
          when Constraint_Error =>
             raise Conversion_Error;
@@ -851,10 +841,8 @@ package body Interfaces.COBOL is
 
       function To_Decimal (Item : Binary) return Num is
          pragma Unsuppress (Range_Check);
-
       begin
          return Num'Fixed_Value (Item);
-
       exception
          when Constraint_Error =>
             raise Conversion_Error;
@@ -866,14 +854,12 @@ package body Interfaces.COBOL is
 
       function To_Decimal
         (Item   : Numeric;
-         Format : Display_Format)
-         return   Num
+         Format : Display_Format) return Num
       is
          pragma Unsuppress (Range_Check);
 
       begin
          return Num'Fixed_Value (Numeric_To_Decimal (Item, Format));
-
       exception
          when Constraint_Error =>
             raise Conversion_Error;
@@ -885,10 +871,8 @@ package body Interfaces.COBOL is
 
       function To_Decimal (Item : Long_Binary) return Num is
          pragma Unsuppress (Range_Check);
-
       begin
          return Num'Fixed_Value (Item);
-
       exception
          when Constraint_Error =>
             raise Conversion_Error;
@@ -900,14 +884,11 @@ package body Interfaces.COBOL is
 
       function To_Decimal
         (Item   : Packed_Decimal;
-         Format : Packed_Format)
-         return   Num
+         Format : Packed_Format) return Num
       is
          pragma Unsuppress (Range_Check);
-
       begin
          return Num'Fixed_Value (Packed_To_Decimal (Item, Format));
-
       exception
          when Constraint_Error =>
             raise Conversion_Error;
@@ -919,18 +900,15 @@ package body Interfaces.COBOL is
 
       function To_Display
         (Item   : Num;
-         Format : Display_Format)
-         return   Numeric
+         Format : Display_Format) return Numeric
       is
          pragma Unsuppress (Range_Check);
-
       begin
          return
            To_Display
              (Integer_64'Integer_Value (Item),
               Format,
               Length (Format));
-
       exception
          when Constraint_Error =>
             raise Conversion_Error;
@@ -942,10 +920,8 @@ package body Interfaces.COBOL is
 
       function To_Long_Binary (Item : Num) return Long_Binary is
          pragma Unsuppress (Range_Check);
-
       begin
          return Long_Binary'Integer_Value (Item);
-
       exception
          when Constraint_Error =>
             raise Conversion_Error;
@@ -957,18 +933,15 @@ package body Interfaces.COBOL is
 
       function To_Packed
         (Item   : Num;
-         Format : Packed_Format)
-         return   Packed_Decimal
+         Format : Packed_Format) return Packed_Decimal
       is
          pragma Unsuppress (Range_Check);
-
       begin
          return
            To_Packed
              (Integer_64'Integer_Value (Item),
               Format,
               Length (Format));
-
       exception
          when Constraint_Error =>
             raise Conversion_Error;
@@ -980,16 +953,13 @@ package body Interfaces.COBOL is
 
       function Valid
         (Item   : Byte_Array;
-         Format : Binary_Format)
-         return   Boolean
+         Format : Binary_Format) return Boolean
       is
          Val : Num;
          pragma Unreferenced (Val);
-
       begin
          Val := To_Decimal (Item, Format);
          return True;
-
       exception
          when Conversion_Error =>
             return False;
@@ -1001,8 +971,7 @@ package body Interfaces.COBOL is
 
       function Valid
         (Item   : Numeric;
-         Format : Display_Format)
-         return   Boolean
+         Format : Display_Format) return Boolean
       is
       begin
          return Valid_Numeric (Item, Format);
@@ -1014,8 +983,7 @@ package body Interfaces.COBOL is
 
       function Valid
         (Item   : Packed_Decimal;
-         Format : Packed_Format)
-         return   Boolean
+         Format : Packed_Format) return Boolean
       is
       begin
          return Valid_Packed (Item, Format);