summaryrefslogtreecommitdiff
path: root/gcc/testsuite/ada/acats/tests/c7/c760011.a
blob: 8df37fa3c8b329ae3d120d9b57f5aa5e03cb3131 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
-- C760011.A
--
--                             Grant of Unlimited Rights
--
--     Under contracts F33600-87-D-0337, F33600-84-D-0280, MDA903-79-C-0687,
--     F08630-91-C-0015, and DCA100-97-D-0025, the U.S. Government obtained 
--     unlimited rights in the software and documentation contained herein.
--     Unlimited rights are defined in DFAR 252.227-7013(a)(19).  By making 
--     this public release, the Government intends to confer upon all 
--     recipients unlimited rights  equal to those held by the Government.  
--     These rights include rights to use, duplicate, release or disclose the 
--     released technical data and computer software in whole or in part, in 
--     any manner and for any purpose whatsoever, and to have or permit others 
--     to do so.
--
--                                    DISCLAIMER
--
--     ALL MATERIALS OR INFORMATION HEREIN RELEASED, MADE AVAILABLE OR
--     DISCLOSED ARE AS IS.  THE GOVERNMENT MAKES NO EXPRESS OR IMPLIED 
--     WARRANTY AS TO ANY MATTER WHATSOEVER, INCLUDING THE CONDITIONS OF THE
--     SOFTWARE, DOCUMENTATION OR OTHER INFORMATION RELEASED, MADE AVAILABLE 
--     OR DISCLOSED, OR THE OWNERSHIP, MERCHANTABILITY, OR FITNESS FOR A
--     PARTICULAR PURPOSE OF SAID MATERIAL.
--*
--
-- OBJECTIVE:
--      Check that the anonymous objects of a controlled type associated with
--      function results and aggregates are finalized no later than the
--      end of the innermost enclosing declarative_item or statement.  Also
--      check this for function calls and aggregates of a noncontrolled type
--      with controlled components.
--
-- TEST DESCRIPTION:
--      This test defines a controlled type with a discriminant, the
--      discriminant is use as an index into a global table to indicate that
--      the object has been finalized.  The controlled type is used as the
--      component of a non-controlled type, and the non-controlled type is
--      used for the same set of tests.  Following is a table of the tests
--      performed and their associated tag character.
--
--      7.6(21) allows for the optimizations that remove these temporary
--      objects from ever existing.  As such this test checks that in the
--      case the object was initialized (the only access we have to
--      determining if it ever existed) it must subsequently be finalized.
--
-- CASE TABLE:
--      A - aggregate test, controlled
--      B - aggregate test, controlled
--      C - aggregate test, non_controlled
--      D - function test, controlled
--      E - function test, non_controlled
--      F - formal parameter function test, controlled
--      G - formal parameter aggregate test, controlled
--      H - formal parameter function test, non_controlled
--      I - formal parameter aggregate test, non_controlled
--
--      X - scratch object, not consequential to the objective
--      Y - scratch object, not consequential to the objective
--      Z - scratch object, not consequential to the objective
--
--
-- CHANGE HISTORY:
--      22 MAY 95   SAIC    Initial version
--      24 APR 96   SAIC    Minor doc fixes, visibility patch
--      14 NOV 96   SAIC    Revised for release 2.1
--
--!

------------------------------------------------------------------- C760011_0

with Ada.Finalization;
package C760011_0 is
  type Tracking_Array is array(Character range 'A'..'Z') of Boolean;

  Initialized : Tracking_Array := (others => False);
  Finalized   : Tracking_Array := (others => False);

  type Controlled_Type(Tag : Character) is
    new Ada.Finalization.Controlled with record
      TC_Component : String(1..4) := "ACVC";
    end record;
  procedure Initialize( It: in out Controlled_Type );
  procedure Finalize  ( It: in out Controlled_Type );
  function Create(With_Tag: Character) return Controlled_Type;

  type Non_Controlled(Tag : Character := 'Y') is record
    Controlled_Component : Controlled_Type(Tag);
  end record;
  procedure Initialize( It: in out Non_Controlled );
  procedure Finalize  ( It: in out Non_Controlled );
  function Create(With_Tag: Character) return Non_Controlled;

  Under_Debug : constant Boolean := False;  -- construction lines

end C760011_0;

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

with Report;
package body C760011_0 is

  procedure Initialize( It: in out Controlled_Type ) is
  begin
    It.TC_Component := (others => It.Tag);
    if It.Tag in Tracking_Array'Range then 
      Initialized(It.Tag) := True;
    end if;
    if Under_Debug then
      Report.Comment("Initializing Tag: " & It.Tag );
    end if;
  end Initialize;
  
  procedure Finalize( It: in out Controlled_Type ) is
  begin
    if Under_Debug then
      Report.Comment("Finalizing for Tag: " & It.Tag );
    end if;
    if It.Tag in Finalized'Range then 
      Finalized(It.Tag) := True;
    end if;
  end Finalize;

  function Create(With_Tag: Character) return Controlled_Type is
  begin
    return Controlled_Type'(Ada.Finalization.Controlled
                            with Tag          => With_Tag,
                                 TC_Component => "*CON" );
  end Create;

  procedure Initialize( It: in out Non_Controlled ) is
  begin
    Report.Failed("Called Initialize for Non_Controlled");
  end Initialize;
  
  procedure Finalize( It: in out Non_Controlled ) is
  begin
    Report.Failed("Called Finalize for Non_Controlled");
  end Finalize;

  function Create(With_Tag: Character) return Non_Controlled is
  begin
    return Non_Controlled'(Tag => With_Tag, Controlled_Component => (
                                  Ada.Finalization.Controlled
                                    with Tag          => With_Tag,
                                         TC_Component => "#NON" ) );
  end Create;

end C760011_0;

--------------------------------------------------------------------- C760011

with Report;
with TCTouch;
with C760011_0;
with Ada.Finalization; -- needed to be able to create extension aggregates
procedure C760011 is

  use type C760011_0.Controlled_Type;
  use type C760011_0.Controlled_Type'Class;
  use type C760011_0.Non_Controlled;

  subtype AFC is Ada.Finalization.Controlled;

  procedure Check_Result( Tag : Character; Message : String ) is
  -- make allowance for 7.6(21) optimizations
  begin
    if C760011_0.Initialized(Tag) then
      TCTouch.Assert(C760011_0.Finalized(Tag),Message);
    elsif C760011_0.Under_Debug then
      Report.Comment("Optimized away: " & Tag );
    end if;
  end Check_Result;

  procedure Subtest_1 is


    procedure Subtest_1_Local_1 is
      An_Object : C760011_0.Controlled_Type'Class
                := C760011_0.Controlled_Type'(AFC with 'X', "ONE*");
                   -- initialize An_Object
    begin
      if C760011_0.Controlled_Type(An_Object)   
         = C760011_0.Controlled_Type'(AFC with 'A', "ONE*") then
        Report.Failed("Comparison bad");  -- A = X !!!
      end if;
    end Subtest_1_Local_1;
    -- An_Object must be Finalized by this point.

    procedure Subtest_1_Local_2 is
      An_Object : C760011_0.Controlled_Type('B');
    begin
      An_Object := (AFC with 'B',  "TWO!" );
      if Report.Ident_Char(An_Object.Tag) /= 'B' then
        Report.Failed("Subtest_1_Local_2 Optimization Foil: Bad Data!");
      end if;
    exception
      when others => Report.Failed("Bad controlled assignment");
    end Subtest_1_Local_2;
    -- An_Object must be Finalized by this point.

    procedure Subtest_1_Local_3 is
      An_Object : C760011_0.Non_Controlled('C');
    begin
      TCTouch.Assert_Not(C760011_0.Finalized('C'),
                           "Non_Controlled declaration C");
      An_Object := C760011_0.Non_Controlled'('C', Controlled_Component
                                     => (AFC with 'C', "TEE!"));
      if Report.Ident_Char(An_Object.Tag) /= 'C' then
        Report.Failed("Subtest_1_Local_3 Optimization Foil: Bad Data!");
      end if;
    end Subtest_1_Local_3;
    -- Only controlled components of An_Object must be finalized; it is an
    -- error to call Finalize for An_Object

  begin
    Subtest_1_Local_1;
    Check_Result( 'A', "Aggregate in subprogram 1" );

    Subtest_1_Local_2;
    Check_Result( 'B', "Aggregate in subprogram 2" );

    Subtest_1_Local_3;
    Check_Result( 'C', "Embedded aggregate in subprogram 3" );
  end Subtest_1;


  procedure Subtest_2 is
    -- using 'Z' for both evades order issues
    Con_Object : C760011_0.Controlled_Type('Z');
    Non_Object : C760011_0.Non_Controlled('Z');
  begin
    if Report.Ident_Bool( Con_Object = C760011_0.Create('D') ) then
      Report.Failed("Con_Object catastrophe");
    end if;
     -- Controlled function result should be finalized by now
    Check_Result( 'D', "Function Result" );

    if Report.Ident_Bool( Non_Object = C760011_0.Create('E') ) then
      Report.Failed("Non_Object catastrophe");
    end if;
     -- Controlled component of function result should be finalized by now
    Check_Result( 'E', "Function Result" );
  end Subtest_2;


  procedure Subtest_3(Con : in C760011_0.Controlled_Type) is
  begin
    if Con.Tag not in 'F'..'G' then  
      Report.Failed("Bad value passed to subtest 3 " & Con.Tag & ' ' 
                    & Report.Ident_Str(Con.TC_Component));
    end if;
  end Subtest_3;


  procedure Subtest_4(Non : in C760011_0.Non_Controlled) is
  begin
    if Non.Tag not in 'H'..'I' then  
      Report.Failed("Bad value passed to subtest 4 "
                   & Non.Tag & ' ' 
                   & Report.Ident_Str(Non.Controlled_Component.TC_Component));
    end if;
  end Subtest_4;


begin  -- Main test procedure.

  Report.Test ("C760011", "Check that anonymous objects of controlled " &
                          "types or types containing controlled types " &
                          "are finalized no later than the end of the " &
                          "innermost enclosing declarative_item or " &
                          "statement" );

  Subtest_1;

  Subtest_2;

  Subtest_3(C760011_0.Create('F'));
  Check_Result( 'F', "Function as formal F" );

  Subtest_3(C760011_0.Controlled_Type'(AFC with 'G',"GIGI"));
  Check_Result( 'G', "Aggregate as formal G" );

  Subtest_4(C760011_0.Create('H'));
  Check_Result( 'H', "Function as formal H" );

  Subtest_4(C760011_0.Non_Controlled'('I', (AFC with 'I',"IAGO")));
  Check_Result( 'I', "Aggregate as formal I" );

  Report.Result;

end C760011;