From patchwork Mon Jul 23 08:20:20 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 172574 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 7A1F72C030A for ; Mon, 23 Jul 2012 18:21:08 +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=1343636468; 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=DMdcvWZIxmCajGwD+KB2 LA6shaA=; b=a59m3bCg3S6AKjKrUYm3ydQznh60qtrlokwx83g40NaZ3GeM99Pk yci2o/FaBun344I08zurqBMoo3Ga9IKEwk/zjo4up2HNKJpKsmMoAhKnLh1XDEh5 H6eQEOxFZ8aqsk5xeVNx/1WyxPtVx+op2euneyrlE1nVvTYt/Q1E1us= 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=xEy/82KPXgWStJD+d3OWVYT6WVpZQWK7LccM2pcBfsIbmkz1KATjFRtKHc+QSL Gi5PN/py91y1qnxkmT15z5QxZCFjdrg58ADjhKK5sy3dVoi/PP7L0nrJcoB4dNNX 4MWTQJ3IviQEE9vAGxrd8+T7xYkF6pCvEfU1WnKZU2ZL0=; Received: (qmail 32348 invoked by alias); 23 Jul 2012 08:20:43 -0000 Received: (qmail 32323 invoked by uid 22791); 23 Jul 2012 08:20:35 -0000 X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL, BAYES_00, RCVD_IN_HOSTKARMA_NO, TW_TM 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, 23 Jul 2012 08:20:22 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 401421C6B4D; Mon, 23 Jul 2012 04:20:20 -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 qg7j904bKsaX; Mon, 23 Jul 2012 04:20:20 -0400 (EDT) Received: from kwai.gnat.com (kwai.gnat.com [205.232.38.4]) by rock.gnat.com (Postfix) with ESMTP id 213DB1C6A3B; Mon, 23 Jul 2012 04:20:20 -0400 (EDT) Received: by kwai.gnat.com (Postfix, from userid 4192) id 1FBEF92BF6; Mon, 23 Jul 2012 04:20:20 -0400 (EDT) Date: Mon, 23 Jul 2012 04:20:20 -0400 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Hristian Kirtchev Subject: [Ada] Clean up of heap objects in the context of accessibility failures Message-ID: <20120723082020.GA13632@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 reimplements the way accessibility checks are performed on heap- allocated class-wide objects. The checks now contain clean up code which finalizes (if applicable) and deallocates the object. Tested on x86_64-pc-linux-gnu, committed on trunk 2012-07-23 Hristian Kirtchev * exp_ch4.adb (Apply_Accessibility_Check): Reimplemented. The check is now more complex and contains optional finalization part and mandatory deallocation part. Index: exp_ch4.adb =================================================================== --- exp_ch4.adb (revision 189768) +++ exp_ch4.adb (working copy) @@ -659,7 +659,7 @@ -- Ada 2005 (AI-344): For an allocator with a class-wide designated -- type, generate an accessibility check to verify that the level of the -- type of the created object is not deeper than the level of the access - -- type. If the type of the qualified expression is class- wide, then + -- type. If the type of the qualified expression is class-wide, then -- always generate the check (except in the case where it is known to be -- unnecessary, see comment below). Otherwise, only generate the check -- if the level of the qualified expression type is statically deeper @@ -690,7 +690,11 @@ (Ref : Node_Id; Built_In_Place : Boolean := False) is - New_Node : Node_Id; + Pool_Id : constant Entity_Id := Associated_Storage_Pool (PtrT); + Cond : Node_Id; + Free_Stmt : Node_Id; + Obj_Ref : Node_Id; + Stmts : List_Id; begin if Ada_Version >= Ada_2005 @@ -701,6 +705,8 @@ or else (Is_Class_Wide_Type (Etype (Exp)) and then Scope (PtrT) /= Current_Scope)) + and then + (Tagged_Type_Expansion or else VM_Target /= No_VM) then -- If the allocator was built in place, Ref is already a reference -- to the access object initialized to the result of the allocator @@ -712,39 +718,109 @@ if Built_In_Place then Remove_Side_Effects (Ref); - New_Node := New_Copy (Ref); + Obj_Ref := New_Copy (Ref); else - New_Node := New_Reference_To (Ref, Loc); + Obj_Ref := New_Reference_To (Ref, Loc); end if; - New_Node := + -- Step 1: Create the object clean up code + + Stmts := New_List; + + -- Create an explicit free statement to clean up the allocated + -- object in case the accessibility check fails. Generate: + + -- Free (Obj_Ref); + + Free_Stmt := Make_Free_Statement (Loc, New_Copy (Obj_Ref)); + Set_Storage_Pool (Free_Stmt, Pool_Id); + + Append_To (Stmts, Free_Stmt); + + -- Finalize the object (if applicable), but wrap the call inside + -- a block to ensure that the object would still be deallocated in + -- case the finalization fails. Generate: + + -- begin + -- [Deep_]Finalize (Obj_Ref.all); + -- exception + -- when others => + -- Free (Obj_Ref); + -- raise; + -- end; + + if Needs_Finalization (DesigT) then + Prepend_To (Stmts, + Make_Block_Statement (Loc, + Handled_Statement_Sequence => + Make_Handled_Sequence_Of_Statements (Loc, + Statements => New_List ( + Make_Final_Call ( + Obj_Ref => + Make_Explicit_Dereference (Loc, + Prefix => New_Copy (Obj_Ref)), + Typ => DesigT)), + + Exception_Handlers => New_List ( + Make_Exception_Handler (Loc, + Exception_Choices => New_List ( + Make_Others_Choice (Loc)), + Statements => New_List ( + New_Copy_Tree (Free_Stmt), + Make_Raise_Statement (Loc))))))); + end if; + + -- Signal the accessibility failure through a Program_Error + + Append_To (Stmts, + Make_Raise_Program_Error (Loc, + Condition => New_Reference_To (Standard_True, Loc), + Reason => PE_Accessibility_Check_Failed)); + + -- Step 2: Create the accessibility comparison + + -- Generate: + -- Ref'Tag + + Obj_Ref := Make_Attribute_Reference (Loc, - Prefix => New_Node, + Prefix => Obj_Ref, Attribute_Name => Name_Tag); + -- For tagged types, determine the accessibility level by looking + -- at the type specific data of the dispatch table. Generate: + + -- Type_Specific_Data (Address (Ref'Tag)).Access_Level + if Tagged_Type_Expansion then - New_Node := Build_Get_Access_Level (Loc, New_Node); + Cond := Build_Get_Access_Level (Loc, Obj_Ref); - elsif VM_Target /= No_VM then - New_Node := - Make_Function_Call (Loc, - Name => New_Reference_To (RTE (RE_Get_Access_Level), Loc), - Parameter_Associations => New_List (New_Node)); + -- Use a runtime call to determine the accessibility level when + -- compiling on virtual machine targets. Generate: - -- Cannot generate the runtime check + -- Get_Access_Level (Ref'Tag) else - return; + Cond := + Make_Function_Call (Loc, + Name => + New_Reference_To (RTE (RE_Get_Access_Level), Loc), + Parameter_Associations => New_List (Obj_Ref)); end if; + Cond := + Make_Op_Gt (Loc, + Left_Opnd => Cond, + Right_Opnd => + Make_Integer_Literal (Loc, Type_Access_Level (PtrT))); + + -- Due to the complexity and side effects of the check, utilize an + -- if statement instead of the regular Program_Error circuitry. + Insert_Action (N, - Make_Raise_Program_Error (Loc, - Condition => - Make_Op_Gt (Loc, - Left_Opnd => New_Node, - Right_Opnd => - Make_Integer_Literal (Loc, Type_Access_Level (PtrT))), - Reason => PE_Accessibility_Check_Failed)); + Make_If_Statement (Loc, + Condition => Cond, + Then_Statements => Stmts)); end if; end Apply_Accessibility_Check;