From patchwork Tue Apr 25 12:58:57 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 754810 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wC3F951v4z9s8Y for ; Tue, 25 Apr 2017 22:59:08 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="aE7g+0xb"; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=STtN8zVicT4dbKnJCht4sl7hu3Cb/cdtzXxOVqr5/OBIws+LFr BaXDajZPktGOvoTEBwmKnLWCz50KgTFOJ0acTFGQVMn0oQ+kxzPzLPZwYdUY5sXl w+XxPyyW+wL4kY7i8kVaa+Fl3PO24+dARAfJGdAbaqe/x5AJYSLXcJjqM= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=8LXhXncsopo6W3LfFwUOd9b7xdE=; b=aE7g+0xbL8yjzcjToJ8C mdL7P+C/XglahZDJ2fPLXQqp3dvtZnF0mupXo/D9S4ii9fXJy/wKYw/0j05NYlUP TqQA7q4CGxRR2sqnlUf7gkxqftbnDZk8sMSb7ef/1wTaERsO6R9SWOlwmY8jr5wj BVdkGbu1gEm5LNfzXkgXvwE= Received: (qmail 76890 invoked by alias); 25 Apr 2017 12:58:59 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 76876 invoked by uid 89); 25 Apr 2017 12:58:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.1 required=5.0 tests=BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=yf, sf X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 25 Apr 2017 12:58:57 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id CCA5F129BAF; Tue, 25 Apr 2017 08:58:57 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id GB0UqHSr92Pg; Tue, 25 Apr 2017 08:58:57 -0400 (EDT) Received: from tron.gnat.com (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) by rock.gnat.com (Postfix) with ESMTP id B8CD429DD4; Tue, 25 Apr 2017 08:58:57 -0400 (EDT) Received: by tron.gnat.com (Postfix, from userid 4192) id B2FB03F0; Tue, 25 Apr 2017 08:58:57 -0400 (EDT) Date: Tue, 25 Apr 2017 08:58:57 -0400 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Ed Schonberg Subject: [Ada] Missing predicate functions for private types. Message-ID: <20170425125857.GA37277@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) This patch fixes an omission in the generation of predicate functions for private types whose full view derives from a subtype with predicates. Executing the following: gnatmake -gnata -q predicate_check predicate_check must yield; OK derived subtype OK original subtype --- with Text_IO; use Text_IO; with Ada.Assertions; use Ada.Assertions; procedure Predicate_Check with SPARK_Mode is type R is -- new Integer; record F : Integer := 42; end record; package Nested is subtype S is R with Predicate => S.F = 42; -- subtype S is R with Predicate => S = 42; procedure P (X : in out S) is null; type T is private; procedure P (X : in out T); private type T is new S; end Nested; package body Nested is procedure P (X : in out T) is begin X.F := X.F * 7; end; end Nested; X : Nested.T; Y : Nested.S; begin Y.F := Y.F * 3; begin Nested.P (X); Put_Line ("should not be here"); exception when Assertion_Error => Put_Line ("OK derived subtype"); end; begin Nested.P (Y); Put_Line ("should not be here"); exception when Assertion_Error => Put_Line ("OK original subtype"); end; end Predicate_Check; Tested on x86_64-pc-linux-gnu, committed on trunk 2017-04-25 Ed Schonberg * sem_aux.adb (Nearest_Ancestor): Use original node of type declaration to locate nearest ancestor, because derived type declarations for record types are rewritten as record declarations. * sem_ch13.adb (Add_Call): Use an unchecked conversion to handle properly derivations that are completions of private types. (Add_Predicates): If type is private, examine rep. items of full view, which may include inherited predicates. (Build_Predicate_Functions): Ditto. Index: sem_aux.adb =================================================================== --- sem_aux.adb (revision 247177) +++ sem_aux.adb (working copy) @@ -6,7 +6,7 @@ -- -- -- B o d y -- -- -- --- Copyright (C) 1992-2016, Free Software Foundation, Inc. -- +-- Copyright (C) 1992-2017, 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- -- @@ -1295,7 +1295,10 @@ ---------------------- function Nearest_Ancestor (Typ : Entity_Id) return Entity_Id is - D : constant Node_Id := Declaration_Node (Typ); + D : constant Node_Id := Original_Node (Declaration_Node (Typ)); + -- We use the original node of the declaration, because derived + -- types from record subtypes are rewritten as record declarations, + -- and it is the original declaration that carries the ancestor. begin -- If we have a subtype declaration, get the ancestor subtype Index: sem_ch13.adb =================================================================== --- sem_ch13.adb (revision 247218) +++ sem_ch13.adb (working copy) @@ -6,7 +6,7 @@ -- -- -- B o d y -- -- -- --- Copyright (C) 1992-2016, Free Software Foundation, Inc. -- +-- Copyright (C) 1992-2017, 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- -- @@ -8309,11 +8309,15 @@ if Present (T) and then Present (Predicate_Function (T)) then Set_Has_Predicates (Typ); - -- Build the call to the predicate function of T + -- Build the call to the predicate function of T. The type may be + -- derived, so use an unchecked conversion for the actual. Exp := Make_Predicate_Call - (T, Convert_To (T, Make_Identifier (Loc, Object_Name))); + (Typ => T, + Expr => + Unchecked_Convert_To (T, + Make_Identifier (Loc, Object_Name))); -- "and"-in the call to evolving expression @@ -8456,6 +8460,14 @@ begin Ritem := First_Rep_Item (Typ); + + -- If the type is private, check whether full view has inherited + -- predicates. + + if Is_Private_Type (Typ) and then No (Ritem) then + Ritem := First_Rep_Item (Full_View (Typ)); + end if; + while Present (Ritem) loop if Nkind (Ritem) = N_Pragma and then Pragma_Name (Ritem) = Name_Predicate @@ -8562,8 +8574,16 @@ -- ones for the current type, as required by AI12-0071-1. declare - Atyp : constant Entity_Id := Nearest_Ancestor (Typ); + Atyp : Entity_Id; begin + Atyp := Nearest_Ancestor (Typ); + + -- The type may be private but the full view may inherit predicates + + if No (Atyp) and then Is_Private_Type (Typ) then + Atyp := Nearest_Ancestor (Full_View (Typ)); + end if; + if Present (Atyp) then Add_Call (Atyp); end if;