From patchwork Mon Aug 6 08:02:51 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 175291 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]) by ozlabs.org (Postfix) with SMTP id 528142C0098 for ; Mon, 6 Aug 2012 18:03:19 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1344844999; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:Date:From:To:Cc:Subject:Message-ID: MIME-Version:Content-Type:Content-Disposition:User-Agent: Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:Sender:Delivered-To; bh=EY2HHBRm/3ARSPv0ivUt Wpk1rJw=; b=KgwlT2c5bU9STF4ekgpWp8y9K1orHD81jNSFsG55I+jMQID1yDLh BNeLrmiHjBFmMyiEjDTMb5Ix9SNlW2BSjG1isT5eXMRW5bWGDKeeU06fuLBhPs0x N37XUYbP5hP0EpGIE0dGyxBHLOQLiGX37lfRxN0lDg+NYFXxuk8VUxc= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Received:Received:Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type:Content-Disposition:User-Agent:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=XognfJUgVal7/3rgp3eV61B909xmjLmYFhLqEPIH7GGQm/1FfMTBQ8rJvZljap Mlosuv1343Jdw4PeeF9AE6IgKJR3qOkITAYAhP3kJ+XEqovZn4MH2imPRk01EG7r zQdVLRC4LU+utaMeouA3Ej07BK48Az5gBpFvt1lmTZqHE=; Received: (qmail 16887 invoked by alias); 6 Aug 2012 08:03:15 -0000 Received: (qmail 16877 invoked by uid 22791); 6 Aug 2012 08:03:14 -0000 X-SWARE-Spam-Status: No, hits=-1.9 required=5.0 tests=AWL, BAYES_00, RCVD_IN_HOSTKARMA_NO X-Spam-Check-By: sourceware.org Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 06 Aug 2012 08:02:52 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 9B1071C660D; Mon, 6 Aug 2012 04:02:51 -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 E+KaqEIhnE4d; Mon, 6 Aug 2012 04:02:51 -0400 (EDT) Received: from kwai.gnat.com (kwai.gnat.com [205.232.38.4]) by rock.gnat.com (Postfix) with ESMTP id 82E831C6482; Mon, 6 Aug 2012 04:02:51 -0400 (EDT) Received: by kwai.gnat.com (Postfix, from userid 4192) id 7F0B292BF6; Mon, 6 Aug 2012 04:02:51 -0400 (EDT) Date: Mon, 6 Aug 2012 04:02:51 -0400 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Hristian Kirtchev Subject: [Ada] Incorrect parameter mechanism due to convention C_Pass_By_Copy Message-ID: <20120806080251.GA32158@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) 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 This patch corrects the Ada-C parameter passing mechanism for record types. OUT and IN OUT parameters are now unconditionally passed by reference regardless of whether C_Pass_By_Copy is in effect. IN parameters subject to the convention are passed by copy, otherwise they are passed by reference. ------------ -- Source -- ------------ -- utils.ads with Interfaces.C; package Utils is type Record_Type is record A : Interfaces.C.int; end record; pragma Convention (C_Pass_By_Copy, Record_Type); procedure Get_Data (Result : out Interfaces.C.int; In_Param : Record_Type; Out_Param : out Record_Type); pragma Export (C, Get_Data, "get_data"); pragma Export_Valued_Procedure (Get_Data); end Utils; -- utils.adb with Ada.Text_IO; use Ada.Text_IO; package body Utils is procedure Get_Data (Result : out Interfaces.C.int; In_Param : Record_Type; Out_Param : out Record_Type) is begin Put_Line (" In data:"); Put_Line (" record field :" & In_Param.A'Img); Result := 0; Out_Param.A := 42; Put_Line (" Returning data:"); Put_Line (" return code :" & Result'Img); Put_Line (" record field :" & Out_Param.A'Img); end Get_Data; end Utils; -- driver.c #include #include struct record_t { int a; }; extern int get_data(struct record_t in_param, struct record_t *out_param); int main() { int ret; struct record_t in_data, out_data; printf("Initializing Ada Runtime\n"); adainit(); in_data.a = 4; printf("Passing data\n"); printf(" record field : %d\n", in_data.a); ret = get_data(in_data,&out_data); printf("Returned data\n"); printf(" return code : %d\n", ret); printf(" record field : %d\n", out_data.a); printf("Expected value: 42\n"); printf("Finalizing Ada Runtime\n"); adafinal(); } ---------------------------- -- Compilation and output -- ---------------------------- $ gcc -c driver.c $ gnatmake -q -z utils -o driver -bargs -n -largs driver.o $ ./driver Initializing Ada Runtime Passing data record field : 4 In data: record field : 4 Returning data: return code : 0 record field : 42 Returned data return code : 0 record field : 42 Expected value: 42 Finalizing Ada Runtime Tested on x86_64-pc-linux-gnu, committed on trunk 2012-08-06 Hristian Kirtchev * sem_mech.adb (Set_Mechanisms): OUT and IN OUT parameters are now unconditionally passed by reference. IN parameters subject to convention C_Pass_By_Copy are passed by copy, otherwise they are passed by reference. Index: sem_mech.adb =================================================================== --- sem_mech.adb (revision 190155) +++ sem_mech.adb (working copy) @@ -6,7 +6,7 @@ -- -- -- B o d y -- -- -- --- Copyright (C) 1996-2011, Free Software Foundation, Inc. -- +-- Copyright (C) 1996-2012, 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- -- @@ -352,13 +352,13 @@ -- Access parameters (RM B.3(68)) -- Access to subprogram types (RM B.3(71)) - -- Note: in the case of access parameters, it is the - -- pointer that is passed by value. In GNAT access - -- parameters are treated as IN parameters of an - -- anonymous access type, so this falls out free. + -- Note: in the case of access parameters, it is the pointer + -- that is passed by value. In GNAT access parameters are + -- treated as IN parameters of an anonymous access type, so + -- this falls out free. - -- The bottom line is that all IN elementary types - -- are passed by copy in GNAT. + -- The bottom line is that all IN elementary types are + -- passed by copy in GNAT. if Is_Elementary_Type (Typ) then if Ekind (Formal) = E_In_Parameter then @@ -385,10 +385,21 @@ if Convention (Typ) /= Convention_C then Set_Mechanism (Formal, By_Reference); - -- If convention C_Pass_By_Copy was specified for - -- the record type, then we pass by copy. + -- OUT and IN OUT parameters of record types are passed + -- by reference regardless of pragmas (RM B.3 (69/2)). - elsif C_Pass_By_Copy (Typ) then + elsif Ekind_In (Formal, E_Out_Parameter, + E_In_Out_Parameter) + then + Set_Mechanism (Formal, By_Reference); + + -- IN parameters of record types are passed by copy only + -- when the related type has convention C_Pass_By_Copy + -- (RM B.3 (68.1/2)). + + elsif Ekind (Formal) = E_In_Parameter + and then C_Pass_By_Copy (Typ) + then Set_Mechanism (Formal, By_Copy); -- Otherwise, for a C convention record, we set the