This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.
| Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
|---|---|---|
| Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |
| Other format: | [Raw text] | |
Tested on i686-linux, committed on HEAD
These changes refine the tests for emitting the accessibility checks
on class-wide return statements and allocators required by AI-344.
Checks aren't needed when a return expression or qualified expression
of an allocator is of a specific type known not to be deeper than the
class-wide type of the function (or designated type of the allocator).
The checks are also now elided when accessibility checks are suppressed.
The changes in Expand_N_Return_Statement fix the test case below
because the class-wide function C.G returning the result
of calling a specific-type function no longer gets a check emitted and
consequently is optimized to avoid performing a copy and unnecessary
adjust operation on the function result that was unmatched by a
corresponding finalize. However, the Adjust/Finalize problem can still
occur for cases where a class-wide function has a class-wide return
expression (a problem that will be filed and addressed separately).
Test case:
gnatmake -gnat05 p
--
Expected output:
A 2
F 1
A 2
F 1
F 0
--
with Ada.Text_IO;
with Ada.Unchecked_Deallocation;
package body C is
procedure Free is new Ada.Unchecked_Deallocation (Integer, Integer_Access);
function F return Tagged_Type is
begin
return (Content => (Ada.Finalization.Controlled
with Pointer => new Intege r'(1)));
end;
function G return Tagged_Type'Class is begin return F; end;
procedure Adjust (X : in out Content_Type) is
begin
if X.Pointer /= null then
X.Pointer.all := X.Pointer.all + 1;
Ada.Text_IO.Put_Line ('A' & X.Pointer.all'Img);
end if;
end;
procedure Finalize (X : in out Content_Type) is
begin
if X.Pointer /= null then
X.Pointer.all := X.Pointer.all - 1;
Ada.Text_IO.Put_Line ('F' & X.Pointer.all'Img);
if X.Pointer.all = 0 then Free (X.Pointer); end if;
end if;
end;
end;
with Ada.Finalization;
package C is
type Integer_Access is access Integer;
type Content_Type is new Ada.Finalization.Controlled with record
Pointer : Integer_Access;
end record;
procedure Adjust (X : in out Content_Type);
procedure Finalize (X : in out Content_Type);
type Tagged_Type is tagged record
Content : Content_Type;
end record;
function F return Tagged_Type;
function G return Tagged_Type'Class;
end;
with C;
procedure P is
X : C.Tagged_Type'Class := C.G;
begin
null;
end;
In addition, also implement following clean up: using a membership test
X in Y is a wrong way to test for X having an
invalid representation (here Y is the subtype of X). The compiler can
(and GNAT does) assume that X is valid and elides the test. The proper
way to do this is to use the 'Valid attribute. This patch recognizes
dubious membership tests and replaces them with apprpriate 'Valid
test, generating warning messages.
procedure K is
X : Natural;
Y : Integer;
for Y'Address use X'Address;
begin
Y := -1;
if X in Natural then
raise Program_Error;
end if;
end K;
generates the warnings:
k.adb:7:09: warning: explicit membership test may be optimized away
k.adb:7:09: warning: use 'Valid attribute instead
and the resulting program executes silently without raising PE.
2005-09-01 Robert Dewar <dewar@adacore.com>
Gary Dismukes <dismukes@adacore.com>
Javier Miranda <miranda@adacore.com>
* exp_ch4.adb (Expand_N_In): Replace test of expression in its own
type by valid test and generate warning.
(Tagged_Membership): Generate call to the run-time
subprogram IW_Membership in case of "Iface_CW_Typ in Typ'Class"
Change formal name Subtype_Mark to Result_Definition in several calls to
Make_Function_Specification.
(Expand_Allocator_Expression): Add tests for suppression of the AI-344
check for proper accessibility of the operand of a class-wide allocator.
The check can be left out if checks are suppressed or if the expression
has a specific tagged type whose level is known to be safe.
* exp_ch5.adb (Expand_N_Assignment_Statement): Simplify the code that
generates the run-time check associated with null-excluding entities.
(Expand_N_Return_Statement): Add tests to determine if the accessibility
check on the level of the return expression of a class-wide function
can be elided. The check usually isn't needed if the expression has a
specific type (unless it's a conversion or a formal parameter). Also
add a test for whether accessibility checks are suppressed. Augment
the comments to describe the conditions for performing the check.
Attachment:
difs.3
Description: Text document
| Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
|---|---|---|
| Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |