From patchwork Mon May 29 08:29:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Marc_Poulhi=C3=A8s?= X-Patchwork-Id: 100175 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1367949vqr; Mon, 29 May 2023 01:49:16 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ56pJflu7oScey/Sukq74H4H0Qmovm/5VhVB/EBaJKmSsOIkmSgPFcHqZ6cCK71O0wVLn9F X-Received: by 2002:a05:6402:1655:b0:514:75ff:6a86 with SMTP id s21-20020a056402165500b0051475ff6a86mr6942566edx.29.1685350156237; Mon, 29 May 2023 01:49:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685350156; cv=none; d=google.com; s=arc-20160816; b=HJ3SwooKGsFTlP5x/QiYlqbJuPzWkkIQ5Pj6KCK6vV2F1M4A2EeCcY+IHCN7xWg+Lg PJTYhZ4n/SY72T+UBXHHduoQJITwuH6fd2xxq76J4VcgNNlUZiAdX+gElkg9cH8GAYKf sq6/0Y7CSHLbZ0JAGBg3xsJl6yI0kT69SJUCdCp4KQUcu9s6D/PeIymhpv8KmCwbQyzp Q36Honj/OFmORLl17QO2kaECqdYxXh24q5L2Y4RbA1yC+8CSNwBLeUTVyQEE7Bz4yhId SGDThm2ubvkaciMpLRrPWy8mz4pSq8qgzF6MKIjJblICkzTSxvb2VNKszLer3JCOeEU5 ZUGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=dd4yhs5TY9H7fGKeGd5xMxcjv/BEa1f+VatdX1veZG0=; b=MqvjqQRHGXX7NzzfjBhAAxuZA78xqBnejvEIDbt8KojRhG9NtA7scyDq9kXOqVXSlM jcgVTq2gw4tkQGI+K0CGPe+OZfzYh52E43ZWIsCSfSfiAWNhus6YmghJMDXZfO+pPplt V6UqCPt6ZF0XFyxUmIizvcNhwkmGUmlMcOab/9VnozkQRgXwkcGGQ6U4utwlRFuKpN8G lF4ZarAzJ8cYmJOPweUgMMk3z7n0HcPn8XUmAVaiTXo/JnsK+w5kKwnhw6TI40k4YfzB SuX6KLtc3dMENZ1cuEYNwR0hlI3NxVp9952yDw1+AE5l64FRkAMZ60i5xECieD0UWsmW ItIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=YGUThxsL; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id c5-20020aa7df05000000b0050da1ef2edasi4291823edy.340.2023.05.29.01.49.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 May 2023 01:49:16 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=YGUThxsL; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 690073AA9016 for ; Mon, 29 May 2023 08:40:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 690073AA9016 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685349610; bh=dd4yhs5TY9H7fGKeGd5xMxcjv/BEa1f+VatdX1veZG0=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=YGUThxsLYi/aa+G1gv3I1hWRBZ4zGIlgpWdGvwOCQG0W0UJhsrCND5l36mn4ZJiEi MO7zAshEk4gESQQUJmq69VNzzTCbq8HGwlZYRjArsILsVqfqnqQ45yMHdmlhYhXpum VibAlDA1FQPxoHpr2OLYeBrtZZIkOCA+Cj7jZQZY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by sourceware.org (Postfix) with ESMTPS id A758F3858436 for ; Mon, 29 May 2023 08:29:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A758F3858436 Received: by mail-wr1-x42f.google.com with SMTP id ffacd0b85a97d-30959c0dfd6so2922618f8f.3 for ; Mon, 29 May 2023 01:29:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685348991; x=1687940991; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=dd4yhs5TY9H7fGKeGd5xMxcjv/BEa1f+VatdX1veZG0=; b=dNNlOWz3Xu6fDeNk5fJ8kOg/n+gHnXJ8yU2DsLltEm8y8F15blBlxlu0EpGPqhtzRU H6jZMulFSg8e0nx7so0PDFEto/XUB9FsA16zAZcapjWmnXvncmD5vQMhm68lhHlHvm9s NxsFZd5amoDbWx5D8z9PlNmgt2w1jjrR/5EHpdiSesl1TBSqyPmgvTAXw7QHrSRYZyih MB5YFVJ2v7X1tfHYqeafquhfUAjHx+rMSV1atpC4rPsKNPIySn858141WtO5i3JzAKHW HmPyAJfUd9IB/MEPlALfumSodtXiCJCV+6P/OGV+Nfuss8FQwjvMUqqFFPRCZPRbYohy yNhA== X-Gm-Message-State: AC+VfDw0LdJL+Y/PWKjvLkV1f3Vb9M5G5NG8MbtA2nbtS9RzZx3e2duG tE9uKpaW1F6VvnKZAv0izv8yygMePMcM7sYQIdXAaw== X-Received: by 2002:adf:d086:0:b0:30a:e7b6:52d6 with SMTP id y6-20020adfd086000000b0030ae7b652d6mr3691698wrh.52.1685348991198; Mon, 29 May 2023 01:29:51 -0700 (PDT) Received: from poulhies-Precision-5550.telnowedge.local (lmontsouris-659-1-24-67.w81-250.abo.wanadoo.fr. [81.250.175.67]) by smtp.gmail.com with ESMTPSA id m12-20020a7bcb8c000000b003f60faa4612sm13514446wmi.22.2023.05.29.01.29.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 May 2023 01:29:50 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: Eric Botcazou Subject: [COMMITTED] ada: Fix bogus error on conditional expression with only user-defined literals Date: Mon, 29 May 2023 10:29:49 +0200 Message-Id: <20230529082949.2411068-1-poulhies@adacore.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: =?utf-8?q?Marc_Poulhi=C3=A8s_via_Gcc-patches?= From: =?utf-8?q?Marc_Poulhi=C3=A8s?= Reply-To: =?utf-8?q?Marc_Poulhi=C3=A8s?= Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767217725134710258?= X-GMAIL-MSGID: =?utf-8?q?1767217725134710258?= From: Eric Botcazou This implements the recursive resolution of conditional expressions whose dependent expressions are (all) user-defined literals the same way it is implemented for operators. gcc/ada/ * sem_res.adb (Has_Applicable_User_Defined_Literal): Make it clear that the predicate also checks the node itself. (Try_User_Defined_Literal): Move current implementation to... Deal only with literals, named numbers and conditional expressions whose dependent expressions are literals or named numbers. (Try_User_Defined_Literal_For_Operator): ...this. Remove multiple return False statements and put a single one at the end. (Resolve): Call Try_User_Defined_Literal instead of directly Has_Applicable_User_Defined_Literal for all nodes. Call Try_User_Defined_Literal_For_Operator for operator nodes. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/sem_res.adb | 127 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 98 insertions(+), 29 deletions(-) diff --git a/gcc/ada/sem_res.adb b/gcc/ada/sem_res.adb index 8a5f87b80ed..899b5b5c522 100644 --- a/gcc/ada/sem_res.adb +++ b/gcc/ada/sem_res.adb @@ -111,10 +111,9 @@ package body Sem_Res is function Has_Applicable_User_Defined_Literal (N : Node_Id; Typ : Entity_Id) return Boolean; - -- If N is a literal or a named number, check whether Typ - -- has a user-defined literal aspect that can apply to N. - -- If present, replace N with a call to the corresponding - -- function and return True. + -- Check whether N is a literal or a named number, and whether Typ has a + -- user-defined literal aspect that may apply to N. In this case, replace + -- N with a call to the corresponding function and return True. procedure Check_Discriminant_Use (N : Node_Id); -- Enforce the restrictions on the use of discriminants when constraining @@ -306,11 +305,20 @@ package body Sem_Res is function Try_User_Defined_Literal (N : Node_Id; Typ : Entity_Id) return Boolean; - -- If an operator node has a literal operand, check whether the type - -- of the context, or the type of the other operand has a user-defined - -- literal aspect that can be applied to the literal to resolve the node. - -- If such aspect exists, replace literal with a call to the - -- corresponding function and return True, return false otherwise. + -- If the node is a literal or a named number or a conditional expression + -- whose dependent expressions are all literals or named numbers, and the + -- context type has a user-defined literal aspect, then rewrite the node + -- or its leaf nodes as calls to the corresponding function, which plays + -- the role of an implicit conversion. + + function Try_User_Defined_Literal_For_Operator + (N : Node_Id; + Typ : Entity_Id) return Boolean; + -- If an operator node has a literal operand, check whether the type of the + -- context, or that of the other operand has a user-defined literal aspect + -- that can be applied to the literal to resolve the node. If such aspect + -- exists, replace literal with a call to the corresponding function and + -- return True, return false otherwise. function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id; -- A universal_fixed expression in an universal context is unambiguous if @@ -600,6 +608,7 @@ package body Sem_Res is Analyze_And_Resolve (N, Typ); return True; + else return False; end if; @@ -3061,14 +3070,11 @@ package body Sem_Res is end; end if; - -- If node is a literal and context type has a user-defined - -- literal aspect, rewrite node as a call to the corresponding - -- function, which plays the role of an implicit conversion. + -- Check whether the node is a literal or a named number or a + -- conditional expression whose dependent expressions are all + -- literals or named numbers. - if Nkind (N) in N_Numeric_Or_String_Literal | N_Identifier - and then Has_Applicable_User_Defined_Literal (N, Typ) - then - Analyze_And_Resolve (N, Typ); + if Try_User_Defined_Literal (N, Typ) then return; end if; @@ -3179,7 +3185,7 @@ package body Sem_Res is -- its operands may be a user-defined literal. elsif Nkind (N) in N_Op and then No (Entity (N)) then - if Try_User_Defined_Literal (N, Typ) then + if Try_User_Defined_Literal_For_Operator (N, Typ) then return; else Unresolved_Operator (N); @@ -13322,6 +13328,78 @@ package body Sem_Res is (N : Node_Id; Typ : Entity_Id) return Boolean is + begin + if Has_Applicable_User_Defined_Literal (N, Typ) then + return True; + + elsif Nkind (N) = N_If_Expression then + -- Both dependent expressions must have the same type as the context + + declare + Condition : constant Node_Id := First (Expressions (N)); + Then_Expr : constant Node_Id := Next (Condition); + Else_Expr : constant Node_Id := Next (Then_Expr); + + begin + if Has_Applicable_User_Defined_Literal (Then_Expr, Typ) then + Resolve (Else_Expr, Typ); + Analyze_And_Resolve (N, Typ); + return True; + + elsif Has_Applicable_User_Defined_Literal (Else_Expr, Typ) then + Resolve (Then_Expr, Typ); + Analyze_And_Resolve (N, Typ); + return True; + end if; + end; + + elsif Nkind (N) = N_Case_Expression then + -- All dependent expressions must have the same type as the context + + declare + Alt : Node_Id; + + begin + Alt := First (Alternatives (N)); + + while Present (Alt) loop + if Has_Applicable_User_Defined_Literal (Expression (Alt), Typ) + then + declare + Other_Alt : Node_Id; + + begin + Other_Alt := First (Alternatives (N)); + + while Present (Other_Alt) loop + if Other_Alt /= Alt then + Resolve (Expression (Other_Alt), Typ); + end if; + + Next (Other_Alt); + end loop; + + Analyze_And_Resolve (N, Typ); + return True; + end; + end if; + + Next (Alt); + end loop; + end; + end if; + + return False; + end Try_User_Defined_Literal; + + ------------------------------------------- + -- Try_User_Defined_Literal_For_Operator -- + ------------------------------------------- + + function Try_User_Defined_Literal_For_Operator + (N : Node_Id; + Typ : Entity_Id) return Boolean + is begin if Nkind (N) in N_Op_Add | N_Op_Divide @@ -13348,9 +13426,6 @@ package body Sem_Res is Resolve (Right_Opnd (N), Typ); Analyze_And_Resolve (N, Typ); return True; - - else - return False; end if; elsif Nkind (N) in N_Binary_Op then @@ -13374,9 +13449,6 @@ package body Sem_Res is then Analyze_And_Resolve (N, Typ); return True; - - else - return False; end if; elsif Nkind (N) in N_Unary_Op @@ -13384,13 +13456,10 @@ package body Sem_Res is then Analyze_And_Resolve (N, Typ); return True; - - else - -- Other operators - - return False; end if; - end Try_User_Defined_Literal; + + return False; + end Try_User_Defined_Literal_For_Operator; ----------------------------- -- Unique_Fixed_Point_Type --