From patchwork Wed Jan 5 11:33:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre-Marie de Rodat X-Patchwork-Id: 1575602 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=TCD+5pY/; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JTSNb5GF9z9sCD for ; Wed, 5 Jan 2022 22:45:50 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0031E385841A for ; Wed, 5 Jan 2022 11:45:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0031E385841A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1641383148; bh=sgC5YMozVkb73Bl/4WIzZpumDk7uNSrakt/VCzUMldU=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=TCD+5pY/wiS3C641+Z4MlLNP1w70r9i+KGjWP6QoLxMzdxs3pLsOAz20knEO1nS6y QY5LUGTKc7yFUniLRCveSjxPV6c/xJIGr/1G40MRyoyGo6D8T8AIE2Gp3Gd6tg2jDE 9tFJcV0pvzY8IARIKm2xstzmktXwsMetNbie8kpU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by sourceware.org (Postfix) with ESMTPS id B1F863858427 for ; Wed, 5 Jan 2022 11:33:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B1F863858427 Received: by mail-wm1-x32a.google.com with SMTP id m20so1121443wmi.0 for ; Wed, 05 Jan 2022 03:33:36 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=sgC5YMozVkb73Bl/4WIzZpumDk7uNSrakt/VCzUMldU=; b=p0TdFbgj/PKknuKHTrAAoQAE661sC/C5qmz7FRGviNYr4cvdUI7i4k9FMEozROAQk1 n8TIJl45RUDGPvrKs/BNCTDqVZuVdGj2y3FulHrYGWDpzWyKUaK67BywdMmNEO3DxAaZ ezXFKB+jq7toaMy3/HusVOzZZ0EaBX7/kAPSdHARlSerGrJtPXxuyxysYOx0BXFPyPqu anN1m7kzMH652PgnqQiBB3duwRH1KybK12Z5wwR+neuz6HSIuI2Zl3uppt3wOGKSiceZ en+PeB+j8IDnd9LcqLDToAGdJw+noayhSu2KCjItkFuenY6+KtkQl7cyvNoA1Bn4n5yU 3wIA== X-Gm-Message-State: AOAM530LjJdVT3EbewtX/sOxJn+1KdTvv2M4YIYH2nH95+zGDcQHb2xr 1nrKh6pHCeyOAB0EssiijKzV+ThIEnZxVw== X-Google-Smtp-Source: ABdhPJwmC7YhL4Ch4V19ua/qcozoHBoHVHlhkz7FG8Ggs1cai0tWDDMgDxg5gIdXApV9AdkWvRhSow== X-Received: by 2002:a05:600c:4408:: with SMTP id u8mr2450952wmn.177.1641382415810; Wed, 05 Jan 2022 03:33:35 -0800 (PST) Received: from adacore.com ([45.147.211.82]) by smtp.gmail.com with ESMTPSA id n15sm40156860wru.66.2022.01.05.03.33.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jan 2022 03:33:35 -0800 (PST) Date: Wed, 5 Jan 2022 11:33:34 +0000 To: gcc-patches@gcc.gnu.org Subject: [Ada] Introduce expression functions for contract of Scan_Exponent Message-ID: <20220105113334.GA2714123@adacore.com> MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SCC_5_SHORT_WORD_LINES, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Pierre-Marie de Rodat via Gcc-patches From: Pierre-Marie de Rodat Reply-To: Pierre-Marie de Rodat Cc: Claire Dross Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Introduce expression functions to make the contract of Scan_Exponent more readable. They can be reused in the contracts of scan functions for scalar types. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * libgnat/s-valuti.ads (Starts_As_Exponent_Format_Ghost): Ghost function to determine if a string is recognized as something which might be an exponent. (Is_Opt_Exponent_Format_Ghost): Ghost function to determine if a string has the correct format for an optional exponent. (Scan_Exponent): Use ghost functions to factorize contracts. diff --git a/gcc/ada/libgnat/s-valuti.ads b/gcc/ada/libgnat/s-valuti.ads --- a/gcc/ada/libgnat/s-valuti.ads +++ b/gcc/ada/libgnat/s-valuti.ads @@ -213,6 +213,50 @@ is -- natural number, consisting in a sequence of figures possibly separated -- by single underscores. It may be followed by other characters. + function Starts_As_Exponent_Format_Ghost + (Str : String; + Real : Boolean) return Boolean + is + (Str'Length > 1 + and then Str (Str'First) in 'E' | 'e' + and then + (declare + Plus_Sign : constant Boolean := Str (Str'First + 1) = '+'; + Minus_Sign : constant Boolean := Str (Str'First + 1) = '-'; + Sign : constant Boolean := Plus_Sign or Minus_Sign; + begin + (if Minus_Sign then Real) + and then (if Sign then Str'Length > 2) + and then + (declare + Start : constant Natural := + (if Sign then Str'First + 2 else Str'First + 1); + begin + Str (Start) in '0' .. '9'))) + with + Ghost; + -- Ghost function that determines if Str is recognized as something which + -- might be an exponent, ie. it starts with an 'e', capitalized or not, + -- followed by an optional sign which can only be '-' if we are working on + -- real numbers (Real is True), and then a digit in decimal notation. + + function Is_Opt_Exponent_Format_Ghost + (Str : String; + Real : Boolean) return Boolean + is + (not Starts_As_Exponent_Format_Ghost (Str, Real) + or else + (declare + Start : constant Natural := + (if Str (Str'First + 1) in '+' | '-' then Str'First + 2 + else Str'First + 1); + begin Is_Natural_Format_Ghost (Str (Start .. Str'Last)))) + with + Ghost; + -- Ghost function that determines if Str has the correct format for an + -- optional exponent, that is, either it does not start as an exponent, or + -- it is in a correct format for a natural number. + function Scan_Natural_Ghost (Str : String; P : Natural; @@ -238,50 +282,22 @@ is Pre => -- Ptr.all .. Max is either an empty range, or a valid range in Str (Ptr.all > Max or else (Ptr.all >= Str'First and then Max <= Str'Last)) - and then - Max < Natural'Last - and then - (if Ptr.all < Max and then Str (Ptr.all) in 'E' | 'e' then - (declare - Plus_Sign : constant Boolean := Str (Ptr.all + 1) = '+'; - Minus_Sign : constant Boolean := Str (Ptr.all + 1) = '-'; - Sign : constant Boolean := Plus_Sign or Minus_Sign; - begin - (if Minus_Sign and not Real then True - elsif Sign - and then (Ptr.all > Max - 2 - or else Str (Ptr.all + 2) not in '0' .. '9') - then True - else - (declare - Start : constant Natural := - (if Sign then Ptr.all + 2 else Ptr.all + 1); - begin - Is_Natural_Format_Ghost (Str (Start .. Max)))))), + and then Max < Natural'Last + and then Is_Opt_Exponent_Format_Ghost (Str (Ptr.all .. Max), Real), Post => - (if Ptr.all'Old < Max and then Str (Ptr.all'Old) in 'E' | 'e' then + (if Starts_As_Exponent_Format_Ghost (Str (Ptr.all'Old .. Max), Real) + then (declare Plus_Sign : constant Boolean := Str (Ptr.all'Old + 1) = '+'; Minus_Sign : constant Boolean := Str (Ptr.all'Old + 1) = '-'; Sign : constant Boolean := Plus_Sign or Minus_Sign; - Unchanged : constant Boolean := - Exp = 0 and Ptr.all = Ptr.all'Old; + Start : constant Natural := + (if Sign then Ptr.all'Old + 2 else Ptr.all'Old + 1); + Value : constant Natural := + Scan_Natural_Ghost (Str (Start .. Max), Start, 0); begin - (if Minus_Sign and not Real then Unchanged - elsif Sign - and then (Ptr.all'Old > Max - 2 - or else Str (Ptr.all'Old + 2) not in '0' .. '9') - then Unchanged - else - (declare - Start : constant Natural := - (if Sign then Ptr.all'Old + 2 else Ptr.all'Old + 1); - Value : constant Natural := - Scan_Natural_Ghost (Str (Start .. Max), Start, 0); - begin - Exp = (if Minus_Sign then -Value else Value)))) - else - Exp = 0 and Ptr.all = Ptr.all'Old); + Exp = (if Minus_Sign then -Value else Value)) + else Exp = 0 and Ptr.all = Ptr.all'Old); -- Called to scan a possible exponent. Str, Ptr, Max are as described above -- for Scan_Sign. If Ptr.all < Max and Str (Ptr.all) = 'E' or 'e', then an -- exponent is scanned out, with the exponent value returned in Exp, and