PageRenderTime 62ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/gcc/ada/i-cobol.adb

https://bitbucket.org/codefirex/toolchain_gcc-4.9
Ada | 994 lines | 611 code | 187 blank | 196 comment | 47 complexity | 91213361b0a98fc4a6c7ff68fa3e54c2 MD5 | raw file
  1. ------------------------------------------------------------------------------
  2. -- --
  3. -- GNAT RUN-TIME COMPONENTS --
  4. -- --
  5. -- I N T E R F A C E S . C O B O L --
  6. -- --
  7. -- B o d y --
  8. -- --
  9. -- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
  10. -- --
  11. -- GNAT is free software; you can redistribute it and/or modify it under --
  12. -- terms of the GNU General Public License as published by the Free Soft- --
  13. -- ware Foundation; either version 3, or (at your option) any later ver- --
  14. -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
  15. -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
  16. -- or FITNESS FOR A PARTICULAR PURPOSE. --
  17. -- --
  18. -- As a special exception under Section 7 of GPL version 3, you are granted --
  19. -- additional permissions described in the GCC Runtime Library Exception, --
  20. -- version 3.1, as published by the Free Software Foundation. --
  21. -- --
  22. -- You should have received a copy of the GNU General Public License and --
  23. -- a copy of the GCC Runtime Library Exception along with this program; --
  24. -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
  25. -- <http://www.gnu.org/licenses/>. --
  26. -- --
  27. -- GNAT was originally developed by the GNAT team at New York University. --
  28. -- Extensive contributions were provided by Ada Core Technologies Inc. --
  29. -- --
  30. ------------------------------------------------------------------------------
  31. -- The body of Interfaces.COBOL is implementation independent (i.e. the same
  32. -- version is used with all versions of GNAT). The specialization to a
  33. -- particular COBOL format is completely contained in the private part of
  34. -- the spec.
  35. with Interfaces; use Interfaces;
  36. with System; use System;
  37. with Ada.Unchecked_Conversion;
  38. package body Interfaces.COBOL is
  39. -----------------------------------------------
  40. -- Declarations for External Binary Handling --
  41. -----------------------------------------------
  42. subtype B1 is Byte_Array (1 .. 1);
  43. subtype B2 is Byte_Array (1 .. 2);
  44. subtype B4 is Byte_Array (1 .. 4);
  45. subtype B8 is Byte_Array (1 .. 8);
  46. -- Representations for 1,2,4,8 byte binary values
  47. function To_B1 is new Ada.Unchecked_Conversion (Integer_8, B1);
  48. function To_B2 is new Ada.Unchecked_Conversion (Integer_16, B2);
  49. function To_B4 is new Ada.Unchecked_Conversion (Integer_32, B4);
  50. function To_B8 is new Ada.Unchecked_Conversion (Integer_64, B8);
  51. -- Conversions from native binary to external binary
  52. function From_B1 is new Ada.Unchecked_Conversion (B1, Integer_8);
  53. function From_B2 is new Ada.Unchecked_Conversion (B2, Integer_16);
  54. function From_B4 is new Ada.Unchecked_Conversion (B4, Integer_32);
  55. function From_B8 is new Ada.Unchecked_Conversion (B8, Integer_64);
  56. -- Conversions from external binary to signed native binary
  57. function From_B1U is new Ada.Unchecked_Conversion (B1, Unsigned_8);
  58. function From_B2U is new Ada.Unchecked_Conversion (B2, Unsigned_16);
  59. function From_B4U is new Ada.Unchecked_Conversion (B4, Unsigned_32);
  60. function From_B8U is new Ada.Unchecked_Conversion (B8, Unsigned_64);
  61. -- Conversions from external binary to unsigned native binary
  62. -----------------------
  63. -- Local Subprograms --
  64. -----------------------
  65. function Binary_To_Decimal
  66. (Item : Byte_Array;
  67. Format : Binary_Format) return Integer_64;
  68. -- This function converts a numeric value in the given format to its
  69. -- corresponding integer value. This is the non-generic implementation
  70. -- of Decimal_Conversions.To_Decimal. The generic routine does the
  71. -- final conversion to the fixed-point format.
  72. function Numeric_To_Decimal
  73. (Item : Numeric;
  74. Format : Display_Format) return Integer_64;
  75. -- This function converts a numeric value in the given format to its
  76. -- corresponding integer value. This is the non-generic implementation
  77. -- of Decimal_Conversions.To_Decimal. The generic routine does the
  78. -- final conversion to the fixed-point format.
  79. function Packed_To_Decimal
  80. (Item : Packed_Decimal;
  81. Format : Packed_Format) return Integer_64;
  82. -- This function converts a packed value in the given format to its
  83. -- corresponding integer value. This is the non-generic implementation
  84. -- of Decimal_Conversions.To_Decimal. The generic routine does the
  85. -- final conversion to the fixed-point format.
  86. procedure Swap (B : in out Byte_Array; F : Binary_Format);
  87. -- Swaps the bytes if required by the binary format F
  88. function To_Display
  89. (Item : Integer_64;
  90. Format : Display_Format;
  91. Length : Natural) return Numeric;
  92. -- This function converts the given integer value into display format,
  93. -- using the given format, with the length in bytes of the result given
  94. -- by the last parameter. This is the non-generic implementation of
  95. -- Decimal_Conversions.To_Display. The conversion of the item from its
  96. -- original decimal format to Integer_64 is done by the generic routine.
  97. function To_Packed
  98. (Item : Integer_64;
  99. Format : Packed_Format;
  100. Length : Natural) return Packed_Decimal;
  101. -- This function converts the given integer value into packed format,
  102. -- using the given format, with the length in digits of the result given
  103. -- by the last parameter. This is the non-generic implementation of
  104. -- Decimal_Conversions.To_Display. The conversion of the item from its
  105. -- original decimal format to Integer_64 is done by the generic routine.
  106. function Valid_Numeric
  107. (Item : Numeric;
  108. Format : Display_Format) return Boolean;
  109. -- This is the non-generic implementation of Decimal_Conversions.Valid
  110. -- for the display case.
  111. function Valid_Packed
  112. (Item : Packed_Decimal;
  113. Format : Packed_Format) return Boolean;
  114. -- This is the non-generic implementation of Decimal_Conversions.Valid
  115. -- for the packed case.
  116. -----------------------
  117. -- Binary_To_Decimal --
  118. -----------------------
  119. function Binary_To_Decimal
  120. (Item : Byte_Array;
  121. Format : Binary_Format) return Integer_64
  122. is
  123. Len : constant Natural := Item'Length;
  124. begin
  125. if Len = 1 then
  126. if Format in Binary_Unsigned_Format then
  127. return Integer_64 (From_B1U (Item));
  128. else
  129. return Integer_64 (From_B1 (Item));
  130. end if;
  131. elsif Len = 2 then
  132. declare
  133. R : B2 := Item;
  134. begin
  135. Swap (R, Format);
  136. if Format in Binary_Unsigned_Format then
  137. return Integer_64 (From_B2U (R));
  138. else
  139. return Integer_64 (From_B2 (R));
  140. end if;
  141. end;
  142. elsif Len = 4 then
  143. declare
  144. R : B4 := Item;
  145. begin
  146. Swap (R, Format);
  147. if Format in Binary_Unsigned_Format then
  148. return Integer_64 (From_B4U (R));
  149. else
  150. return Integer_64 (From_B4 (R));
  151. end if;
  152. end;
  153. elsif Len = 8 then
  154. declare
  155. R : B8 := Item;
  156. begin
  157. Swap (R, Format);
  158. if Format in Binary_Unsigned_Format then
  159. return Integer_64 (From_B8U (R));
  160. else
  161. return Integer_64 (From_B8 (R));
  162. end if;
  163. end;
  164. -- Length is not 1, 2, 4 or 8
  165. else
  166. raise Conversion_Error;
  167. end if;
  168. end Binary_To_Decimal;
  169. ------------------------
  170. -- Numeric_To_Decimal --
  171. ------------------------
  172. -- The following assumptions are made in the coding of this routine:
  173. -- The range of COBOL_Digits is compact and the ten values
  174. -- represent the digits 0-9 in sequence
  175. -- The range of COBOL_Plus_Digits is compact and the ten values
  176. -- represent the digits 0-9 in sequence with a plus sign.
  177. -- The range of COBOL_Minus_Digits is compact and the ten values
  178. -- represent the digits 0-9 in sequence with a minus sign.
  179. -- The COBOL_Minus_Digits set is disjoint from COBOL_Digits
  180. -- These assumptions are true for all COBOL representations we know of
  181. function Numeric_To_Decimal
  182. (Item : Numeric;
  183. Format : Display_Format) return Integer_64
  184. is
  185. pragma Unsuppress (Range_Check);
  186. Sign : COBOL_Character := COBOL_Plus;
  187. Result : Integer_64 := 0;
  188. begin
  189. if not Valid_Numeric (Item, Format) then
  190. raise Conversion_Error;
  191. end if;
  192. for J in Item'Range loop
  193. declare
  194. K : constant COBOL_Character := Item (J);
  195. begin
  196. if K in COBOL_Digits then
  197. Result := Result * 10 +
  198. (COBOL_Character'Pos (K) -
  199. COBOL_Character'Pos (COBOL_Digits'First));
  200. elsif K in COBOL_Plus_Digits then
  201. Result := Result * 10 +
  202. (COBOL_Character'Pos (K) -
  203. COBOL_Character'Pos (COBOL_Plus_Digits'First));
  204. elsif K in COBOL_Minus_Digits then
  205. Result := Result * 10 +
  206. (COBOL_Character'Pos (K) -
  207. COBOL_Character'Pos (COBOL_Minus_Digits'First));
  208. Sign := COBOL_Minus;
  209. -- Only remaining possibility is COBOL_Plus or COBOL_Minus
  210. else
  211. Sign := K;
  212. end if;
  213. end;
  214. end loop;
  215. if Sign = COBOL_Plus then
  216. return Result;
  217. else
  218. return -Result;
  219. end if;
  220. exception
  221. when Constraint_Error =>
  222. raise Conversion_Error;
  223. end Numeric_To_Decimal;
  224. -----------------------
  225. -- Packed_To_Decimal --
  226. -----------------------
  227. function Packed_To_Decimal
  228. (Item : Packed_Decimal;
  229. Format : Packed_Format) return Integer_64
  230. is
  231. pragma Unsuppress (Range_Check);
  232. Result : Integer_64 := 0;
  233. Sign : constant Decimal_Element := Item (Item'Last);
  234. begin
  235. if not Valid_Packed (Item, Format) then
  236. raise Conversion_Error;
  237. end if;
  238. case Packed_Representation is
  239. when IBM =>
  240. for J in Item'First .. Item'Last - 1 loop
  241. Result := Result * 10 + Integer_64 (Item (J));
  242. end loop;
  243. if Sign = 16#0B# or else Sign = 16#0D# then
  244. return -Result;
  245. else
  246. return +Result;
  247. end if;
  248. end case;
  249. exception
  250. when Constraint_Error =>
  251. raise Conversion_Error;
  252. end Packed_To_Decimal;
  253. ----------
  254. -- Swap --
  255. ----------
  256. procedure Swap (B : in out Byte_Array; F : Binary_Format) is
  257. Little_Endian : constant Boolean :=
  258. System.Default_Bit_Order = System.Low_Order_First;
  259. begin
  260. -- Return if no swap needed
  261. case F is
  262. when H | HU =>
  263. if not Little_Endian then
  264. return;
  265. end if;
  266. when L | LU =>
  267. if Little_Endian then
  268. return;
  269. end if;
  270. when N | NU =>
  271. return;
  272. end case;
  273. -- Here a swap is needed
  274. declare
  275. Len : constant Natural := B'Length;
  276. begin
  277. for J in 1 .. Len / 2 loop
  278. declare
  279. Temp : constant Byte := B (J);
  280. begin
  281. B (J) := B (Len + 1 - J);
  282. B (Len + 1 - J) := Temp;
  283. end;
  284. end loop;
  285. end;
  286. end Swap;
  287. -----------------------
  288. -- To_Ada (function) --
  289. -----------------------
  290. function To_Ada (Item : Alphanumeric) return String is
  291. Result : String (Item'Range);
  292. begin
  293. for J in Item'Range loop
  294. Result (J) := COBOL_To_Ada (Item (J));
  295. end loop;
  296. return Result;
  297. end To_Ada;
  298. ------------------------
  299. -- To_Ada (procedure) --
  300. ------------------------
  301. procedure To_Ada
  302. (Item : Alphanumeric;
  303. Target : out String;
  304. Last : out Natural)
  305. is
  306. Last_Val : Integer;
  307. begin
  308. if Item'Length > Target'Length then
  309. raise Constraint_Error;
  310. end if;
  311. Last_Val := Target'First - 1;
  312. for J in Item'Range loop
  313. Last_Val := Last_Val + 1;
  314. Target (Last_Val) := COBOL_To_Ada (Item (J));
  315. end loop;
  316. Last := Last_Val;
  317. end To_Ada;
  318. -------------------------
  319. -- To_COBOL (function) --
  320. -------------------------
  321. function To_COBOL (Item : String) return Alphanumeric is
  322. Result : Alphanumeric (Item'Range);
  323. begin
  324. for J in Item'Range loop
  325. Result (J) := Ada_To_COBOL (Item (J));
  326. end loop;
  327. return Result;
  328. end To_COBOL;
  329. --------------------------
  330. -- To_COBOL (procedure) --
  331. --------------------------
  332. procedure To_COBOL
  333. (Item : String;
  334. Target : out Alphanumeric;
  335. Last : out Natural)
  336. is
  337. Last_Val : Integer;
  338. begin
  339. if Item'Length > Target'Length then
  340. raise Constraint_Error;
  341. end if;
  342. Last_Val := Target'First - 1;
  343. for J in Item'Range loop
  344. Last_Val := Last_Val + 1;
  345. Target (Last_Val) := Ada_To_COBOL (Item (J));
  346. end loop;
  347. Last := Last_Val;
  348. end To_COBOL;
  349. ----------------
  350. -- To_Display --
  351. ----------------
  352. function To_Display
  353. (Item : Integer_64;
  354. Format : Display_Format;
  355. Length : Natural) return Numeric
  356. is
  357. Result : Numeric (1 .. Length);
  358. Val : Integer_64 := Item;
  359. procedure Convert (First, Last : Natural);
  360. -- Convert the number in Val into COBOL_Digits, storing the result
  361. -- in Result (First .. Last). Raise Conversion_Error if too large.
  362. procedure Embed_Sign (Loc : Natural);
  363. -- Used for the nonseparate formats to embed the appropriate sign
  364. -- at the specified location (i.e. at Result (Loc))
  365. -------------
  366. -- Convert --
  367. -------------
  368. procedure Convert (First, Last : Natural) is
  369. J : Natural;
  370. begin
  371. J := Last;
  372. while J >= First loop
  373. Result (J) :=
  374. COBOL_Character'Val
  375. (COBOL_Character'Pos (COBOL_Digits'First) +
  376. Integer (Val mod 10));
  377. Val := Val / 10;
  378. if Val = 0 then
  379. for K in First .. J - 1 loop
  380. Result (J) := COBOL_Digits'First;
  381. end loop;
  382. return;
  383. else
  384. J := J - 1;
  385. end if;
  386. end loop;
  387. raise Conversion_Error;
  388. end Convert;
  389. ----------------
  390. -- Embed_Sign --
  391. ----------------
  392. procedure Embed_Sign (Loc : Natural) is
  393. Digit : Natural range 0 .. 9;
  394. begin
  395. Digit := COBOL_Character'Pos (Result (Loc)) -
  396. COBOL_Character'Pos (COBOL_Digits'First);
  397. if Item >= 0 then
  398. Result (Loc) :=
  399. COBOL_Character'Val
  400. (COBOL_Character'Pos (COBOL_Plus_Digits'First) + Digit);
  401. else
  402. Result (Loc) :=
  403. COBOL_Character'Val
  404. (COBOL_Character'Pos (COBOL_Minus_Digits'First) + Digit);
  405. end if;
  406. end Embed_Sign;
  407. -- Start of processing for To_Display
  408. begin
  409. case Format is
  410. when Unsigned =>
  411. if Val < 0 then
  412. raise Conversion_Error;
  413. else
  414. Convert (1, Length);
  415. end if;
  416. when Leading_Separate =>
  417. if Val < 0 then
  418. Result (1) := COBOL_Minus;
  419. Val := -Val;
  420. else
  421. Result (1) := COBOL_Plus;
  422. end if;
  423. Convert (2, Length);
  424. when Trailing_Separate =>
  425. if Val < 0 then
  426. Result (Length) := COBOL_Minus;
  427. Val := -Val;
  428. else
  429. Result (Length) := COBOL_Plus;
  430. end if;
  431. Convert (1, Length - 1);
  432. when Leading_Nonseparate =>
  433. Val := abs Val;
  434. Convert (1, Length);
  435. Embed_Sign (1);
  436. when Trailing_Nonseparate =>
  437. Val := abs Val;
  438. Convert (1, Length);
  439. Embed_Sign (Length);
  440. end case;
  441. return Result;
  442. end To_Display;
  443. ---------------
  444. -- To_Packed --
  445. ---------------
  446. function To_Packed
  447. (Item : Integer_64;
  448. Format : Packed_Format;
  449. Length : Natural) return Packed_Decimal
  450. is
  451. Result : Packed_Decimal (1 .. Length);
  452. Val : Integer_64;
  453. procedure Convert (First, Last : Natural);
  454. -- Convert the number in Val into a sequence of Decimal_Element values,
  455. -- storing the result in Result (First .. Last). Raise Conversion_Error
  456. -- if the value is too large to fit.
  457. -------------
  458. -- Convert --
  459. -------------
  460. procedure Convert (First, Last : Natural) is
  461. J : Natural := Last;
  462. begin
  463. while J >= First loop
  464. Result (J) := Decimal_Element (Val mod 10);
  465. Val := Val / 10;
  466. if Val = 0 then
  467. for K in First .. J - 1 loop
  468. Result (K) := 0;
  469. end loop;
  470. return;
  471. else
  472. J := J - 1;
  473. end if;
  474. end loop;
  475. raise Conversion_Error;
  476. end Convert;
  477. -- Start of processing for To_Packed
  478. begin
  479. case Packed_Representation is
  480. when IBM =>
  481. if Format = Packed_Unsigned then
  482. if Item < 0 then
  483. raise Conversion_Error;
  484. else
  485. Result (Length) := 16#F#;
  486. Val := Item;
  487. end if;
  488. elsif Item >= 0 then
  489. Result (Length) := 16#C#;
  490. Val := Item;
  491. else -- Item < 0
  492. Result (Length) := 16#D#;
  493. Val := -Item;
  494. end if;
  495. Convert (1, Length - 1);
  496. return Result;
  497. end case;
  498. end To_Packed;
  499. -------------------
  500. -- Valid_Numeric --
  501. -------------------
  502. function Valid_Numeric
  503. (Item : Numeric;
  504. Format : Display_Format) return Boolean
  505. is
  506. begin
  507. if Item'Length = 0 then
  508. return False;
  509. end if;
  510. -- All character positions except first and last must be Digits.
  511. -- This is true for all the formats.
  512. for J in Item'First + 1 .. Item'Last - 1 loop
  513. if Item (J) not in COBOL_Digits then
  514. return False;
  515. end if;
  516. end loop;
  517. case Format is
  518. when Unsigned =>
  519. return Item (Item'First) in COBOL_Digits
  520. and then Item (Item'Last) in COBOL_Digits;
  521. when Leading_Separate =>
  522. return (Item (Item'First) = COBOL_Plus or else
  523. Item (Item'First) = COBOL_Minus)
  524. and then Item (Item'Last) in COBOL_Digits;
  525. when Trailing_Separate =>
  526. return Item (Item'First) in COBOL_Digits
  527. and then
  528. (Item (Item'Last) = COBOL_Plus or else
  529. Item (Item'Last) = COBOL_Minus);
  530. when Leading_Nonseparate =>
  531. return (Item (Item'First) in COBOL_Plus_Digits or else
  532. Item (Item'First) in COBOL_Minus_Digits)
  533. and then Item (Item'Last) in COBOL_Digits;
  534. when Trailing_Nonseparate =>
  535. return Item (Item'First) in COBOL_Digits
  536. and then
  537. (Item (Item'Last) in COBOL_Plus_Digits or else
  538. Item (Item'Last) in COBOL_Minus_Digits);
  539. end case;
  540. end Valid_Numeric;
  541. ------------------
  542. -- Valid_Packed --
  543. ------------------
  544. function Valid_Packed
  545. (Item : Packed_Decimal;
  546. Format : Packed_Format) return Boolean
  547. is
  548. begin
  549. case Packed_Representation is
  550. when IBM =>
  551. for J in Item'First .. Item'Last - 1 loop
  552. if Item (J) > 9 then
  553. return False;
  554. end if;
  555. end loop;
  556. -- For unsigned, sign digit must be F
  557. if Format = Packed_Unsigned then
  558. return Item (Item'Last) = 16#F#;
  559. -- For signed, accept all standard and non-standard signs
  560. else
  561. return Item (Item'Last) in 16#A# .. 16#F#;
  562. end if;
  563. end case;
  564. end Valid_Packed;
  565. -------------------------
  566. -- Decimal_Conversions --
  567. -------------------------
  568. package body Decimal_Conversions is
  569. ---------------------
  570. -- Length (binary) --
  571. ---------------------
  572. -- Note that the tests here are all compile time tests
  573. function Length (Format : Binary_Format) return Natural is
  574. pragma Unreferenced (Format);
  575. begin
  576. if Num'Digits <= 2 then
  577. return 1;
  578. elsif Num'Digits <= 4 then
  579. return 2;
  580. elsif Num'Digits <= 9 then
  581. return 4;
  582. else -- Num'Digits in 10 .. 18
  583. return 8;
  584. end if;
  585. end Length;
  586. ----------------------
  587. -- Length (display) --
  588. ----------------------
  589. function Length (Format : Display_Format) return Natural is
  590. begin
  591. if Format = Leading_Separate or else Format = Trailing_Separate then
  592. return Num'Digits + 1;
  593. else
  594. return Num'Digits;
  595. end if;
  596. end Length;
  597. ---------------------
  598. -- Length (packed) --
  599. ---------------------
  600. -- Note that the tests here are all compile time checks
  601. function Length
  602. (Format : Packed_Format) return Natural
  603. is
  604. pragma Unreferenced (Format);
  605. begin
  606. case Packed_Representation is
  607. when IBM =>
  608. return (Num'Digits + 2) / 2 * 2;
  609. end case;
  610. end Length;
  611. ---------------
  612. -- To_Binary --
  613. ---------------
  614. function To_Binary
  615. (Item : Num;
  616. Format : Binary_Format) return Byte_Array
  617. is
  618. begin
  619. -- Note: all these tests are compile time tests
  620. if Num'Digits <= 2 then
  621. return To_B1 (Integer_8'Integer_Value (Item));
  622. elsif Num'Digits <= 4 then
  623. declare
  624. R : B2 := To_B2 (Integer_16'Integer_Value (Item));
  625. begin
  626. Swap (R, Format);
  627. return R;
  628. end;
  629. elsif Num'Digits <= 9 then
  630. declare
  631. R : B4 := To_B4 (Integer_32'Integer_Value (Item));
  632. begin
  633. Swap (R, Format);
  634. return R;
  635. end;
  636. else -- Num'Digits in 10 .. 18
  637. declare
  638. R : B8 := To_B8 (Integer_64'Integer_Value (Item));
  639. begin
  640. Swap (R, Format);
  641. return R;
  642. end;
  643. end if;
  644. exception
  645. when Constraint_Error =>
  646. raise Conversion_Error;
  647. end To_Binary;
  648. ---------------------------------
  649. -- To_Binary (internal binary) --
  650. ---------------------------------
  651. function To_Binary (Item : Num) return Binary is
  652. pragma Unsuppress (Range_Check);
  653. begin
  654. return Binary'Integer_Value (Item);
  655. exception
  656. when Constraint_Error =>
  657. raise Conversion_Error;
  658. end To_Binary;
  659. -------------------------
  660. -- To_Decimal (binary) --
  661. -------------------------
  662. function To_Decimal
  663. (Item : Byte_Array;
  664. Format : Binary_Format) return Num
  665. is
  666. pragma Unsuppress (Range_Check);
  667. begin
  668. return Num'Fixed_Value (Binary_To_Decimal (Item, Format));
  669. exception
  670. when Constraint_Error =>
  671. raise Conversion_Error;
  672. end To_Decimal;
  673. ----------------------------------
  674. -- To_Decimal (internal binary) --
  675. ----------------------------------
  676. function To_Decimal (Item : Binary) return Num is
  677. pragma Unsuppress (Range_Check);
  678. begin
  679. return Num'Fixed_Value (Item);
  680. exception
  681. when Constraint_Error =>
  682. raise Conversion_Error;
  683. end To_Decimal;
  684. --------------------------
  685. -- To_Decimal (display) --
  686. --------------------------
  687. function To_Decimal
  688. (Item : Numeric;
  689. Format : Display_Format) return Num
  690. is
  691. pragma Unsuppress (Range_Check);
  692. begin
  693. return Num'Fixed_Value (Numeric_To_Decimal (Item, Format));
  694. exception
  695. when Constraint_Error =>
  696. raise Conversion_Error;
  697. end To_Decimal;
  698. ---------------------------------------
  699. -- To_Decimal (internal long binary) --
  700. ---------------------------------------
  701. function To_Decimal (Item : Long_Binary) return Num is
  702. pragma Unsuppress (Range_Check);
  703. begin
  704. return Num'Fixed_Value (Item);
  705. exception
  706. when Constraint_Error =>
  707. raise Conversion_Error;
  708. end To_Decimal;
  709. -------------------------
  710. -- To_Decimal (packed) --
  711. -------------------------
  712. function To_Decimal
  713. (Item : Packed_Decimal;
  714. Format : Packed_Format) return Num
  715. is
  716. pragma Unsuppress (Range_Check);
  717. begin
  718. return Num'Fixed_Value (Packed_To_Decimal (Item, Format));
  719. exception
  720. when Constraint_Error =>
  721. raise Conversion_Error;
  722. end To_Decimal;
  723. ----------------
  724. -- To_Display --
  725. ----------------
  726. function To_Display
  727. (Item : Num;
  728. Format : Display_Format) return Numeric
  729. is
  730. pragma Unsuppress (Range_Check);
  731. begin
  732. return
  733. To_Display
  734. (Integer_64'Integer_Value (Item),
  735. Format,
  736. Length (Format));
  737. exception
  738. when Constraint_Error =>
  739. raise Conversion_Error;
  740. end To_Display;
  741. --------------------
  742. -- To_Long_Binary --
  743. --------------------
  744. function To_Long_Binary (Item : Num) return Long_Binary is
  745. pragma Unsuppress (Range_Check);
  746. begin
  747. return Long_Binary'Integer_Value (Item);
  748. exception
  749. when Constraint_Error =>
  750. raise Conversion_Error;
  751. end To_Long_Binary;
  752. ---------------
  753. -- To_Packed --
  754. ---------------
  755. function To_Packed
  756. (Item : Num;
  757. Format : Packed_Format) return Packed_Decimal
  758. is
  759. pragma Unsuppress (Range_Check);
  760. begin
  761. return
  762. To_Packed
  763. (Integer_64'Integer_Value (Item),
  764. Format,
  765. Length (Format));
  766. exception
  767. when Constraint_Error =>
  768. raise Conversion_Error;
  769. end To_Packed;
  770. --------------------
  771. -- Valid (binary) --
  772. --------------------
  773. function Valid
  774. (Item : Byte_Array;
  775. Format : Binary_Format) return Boolean
  776. is
  777. Val : Num;
  778. pragma Unreferenced (Val);
  779. begin
  780. Val := To_Decimal (Item, Format);
  781. return True;
  782. exception
  783. when Conversion_Error =>
  784. return False;
  785. end Valid;
  786. ---------------------
  787. -- Valid (display) --
  788. ---------------------
  789. function Valid
  790. (Item : Numeric;
  791. Format : Display_Format) return Boolean
  792. is
  793. begin
  794. return Valid_Numeric (Item, Format);
  795. end Valid;
  796. --------------------
  797. -- Valid (packed) --
  798. --------------------
  799. function Valid
  800. (Item : Packed_Decimal;
  801. Format : Packed_Format) return Boolean
  802. is
  803. begin
  804. return Valid_Packed (Item, Format);
  805. end Valid;
  806. end Decimal_Conversions;
  807. end Interfaces.COBOL;