File: ada_trees-rep_clauses.adb

package info (click to toggle)
asis 2019-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,848 kB
  • sloc: ada: 156,772; makefile: 296; sh: 81; xml: 48; csh: 10
file content (266 lines) | stat: -rw-r--r-- 10,329 bytes parent folder | download | duplicates (2)
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
------------------------------------------------------------------------------
--                                                                          --
--                            GNAT2XML COMPONENTS                           --
--                                                                          --
--       G N A T 2 X M L . A D A _ T R E E S . R E P _ C L A U S E S        --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                   Copyright (C) 2014-2018, AdaCore                       --
--                                                                          --
-- Gnat2xml is free software; you can redistribute it and/or modify it      --
-- under terms of the  GNU General Public License  as published by the Free --
-- Software Foundation;  either version 2,  or  (at your option)  any later --
-- version. Gnat2xml is distributed  in the hope  that it will be useful,   --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MER-      --
-- CHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General  --
-- Public License for more details. You should have received a copy of the  --
-- GNU General Public License distributed with GNAT; see file COPYING. If   --
-- not, write to the Free Software Foundation, 59 Temple Place Suite 330,   --
-- Boston, MA 02111-1307, USA.                                              --
-- The gnat2xml tool was derived from the Avatox sources.                   --
------------------------------------------------------------------------------

pragma Ada_2012;

with Asis.Data_Decomposition;
with Asis.Data_Decomposition.Debug; use Asis.Data_Decomposition.Debug;
with Asis.Declarations;             use Asis.Declarations;

with Ada_Trees.Asis_To_Tree;
with Ada_Trees.Factory; use Ada_Trees.Factory;
with Pp.Scanner;

package body Ada_Trees.Rep_Clauses is
   use Ada_Tree_Vectors;

   package DD renames Asis.Data_Decomposition;

   function "+" (S : String) return Name_Id renames Name_Find;
   function Nil return Asis.Text.Span is (Asis.Text.Nil_Span);

   --  The following Do_... procedures are called by Append_Rep_Clauses (except
   --  Do_Rec_Comp_Clause, which is called by Do_Record_Clause) to generate a
   --  particular kind of clause. Def_Id is the defining id of the type, and
   --  Defn is the type definition.

   procedure Do_Size_Clause
     (Clauses : in out Ada_Tree_Vector;
      Def_Id  :        Ada_Tree;
      Defn    :        Asis.Element);

   procedure Do_Comp_Size_Clause
     (Clauses : in out Ada_Tree_Vector;
      Def_Id  :        Ada_Tree;
      Defn    :        Asis.Element);

   procedure Do_Rec_Comp_Clause
     (Comp_Clauses : in out Ada_Tree_Vector;
      Component    :        DD.Record_Component);

   procedure Do_Record_Clause
     (Clauses : in out Ada_Tree_Vector;
      Def_Id  :        Ada_Tree;
      Defn    :        Asis.Element);

   procedure Do_Comp_Size_Clause
     (Clauses : in out Ada_Tree_Vector;
      Def_Id  :        Ada_Tree;
      Defn    :        Asis.Element)
   is
      function Component_Size
        (Type_Definition : Asis.Type_Definition) return Asis.ASIS_Natural is
         (DD.Size (DD.Array_Components (Type_Definition)));
      --  There is no direct support in DD for the Component_Size attribute,
      --  but we can get that information from the Size of the
      --  Array_Components.

      Component_Size_Clause : constant Ada_Tree :=
        Attribute_Definition_Clause
          (Representation_Clause_Name =>
             Component_Size_Attribute
               (Expr_Type => +"null",
                Prefix    =>
                  Identifier
                    (Expr_Type => +"null",
                     Ref_Name  => Def_Id.Def_Name,
                     Ref       => Def_Id.Def,
                     Sloc      => Nil),
                Attribute_Designator_Identifier =>
                  Identifier
                    (Expr_Type => +"null",
                     Ref_Name  => +"Component_Size",
                     Ref       => +"null",
                     Sloc      => Nil),
                Sloc => Nil),
           Representation_Clause_Expression =>
             Integer_Literal
               (Expr_Type => +"universal integer",
                Lit_Val   => +Image (Component_Size (Defn)),
                Sloc      => Nil),
           Sloc => Nil);
   begin
      Append (Clauses, Component_Size_Clause);
   end Do_Comp_Size_Clause;

   procedure Do_Rec_Comp_Clause
     (Comp_Clauses : in out Ada_Tree_Vector;
      Component    :        DD.Record_Component)
   is
      Comp_Decl : constant Asis.Declaration :=
        DD.Component_Declaration (Component);
      pragma Assert
        (Ekind (Comp_Decl) in
           A_Discriminant_Specification | A_Component_Declaration);
      Comp_Def : constant Asis.Defining_Name := Names (Comp_Decl) (1);
      pragma Assert (Ekind (Comp_Def) = A_Defining_Identifier);

      Comp_Clause : constant Component_Clause_Tree :=
        Component_Clause
          (Representation_Clause_Name =>
             Identifier
               (Expr_Type => +"null",
                Ref_Name  => Asis_To_Tree.Def_Name_Value (Comp_Def),
                Ref       => Asis_To_Tree.Def_Value (Comp_Def),
                Sloc      => Nil),
           Component_Clause_Position =>
             Integer_Literal
               (Expr_Type => +"universal integer",
                Lit_Val   => +Image (DD.Position (Component)),
                Sloc      => Nil),
           Component_Clause_Range =>
             Discrete_Simple_Expression_Range
               (Lower_Bound =>
                  Integer_Literal
                    (Expr_Type => +"universal integer",
                     Lit_Val   => +Image (DD.First_Bit (Component)),
                     Sloc      => Nil),
                Upper_Bound =>
                  Integer_Literal
                    (Expr_Type => +"universal integer",
                     Lit_Val   => +Image (DD.Last_Bit (Component)),
                     Sloc      => Nil),
                Sloc => Nil),
           Sloc => Nil);
   begin
      Append (Comp_Clauses, Comp_Clause);
   end Do_Rec_Comp_Clause;

   procedure Do_Record_Clause
     (Clauses : in out Ada_Tree_Vector;
      Def_Id  :        Ada_Tree;
      Defn    :        Asis.Element)
   is
      Comps : constant DD.Record_Component_List := DD.Record_Components (Defn);
      Comp_Clauses : Ada_Tree_Vector;
   begin
      for Comp of Comps loop
         Do_Rec_Comp_Clause (Comp_Clauses, Comp);
      end loop;

      Append
        (Clauses,
         Record_Representation_Clause
           (Representation_Clause_Name =>
              Identifier
                (Expr_Type => +"null",
                 Ref_Name  => Def_Id.Def_Name,
                 Ref       => Def_Id.Def,
                 Sloc      => Nil),
            Mod_Clause_Expression => Nil,
            Component_Clauses     =>
              Component_Clause_List (To_Array (Comp_Clauses), Sloc => Nil),
            Sloc => Nil));
   end Do_Record_Clause;

   procedure Do_Size_Clause
     (Clauses : in out Ada_Tree_Vector;
      Def_Id  :        Ada_Tree;
      Defn    :        Asis.Element)
   is
      Size_Clause : constant Ada_Tree :=
        Attribute_Definition_Clause
          (Representation_Clause_Name =>
             Size_Attribute
               (Expr_Type => +"null",
                Prefix    =>
                  Identifier
                    (Expr_Type => +"null",
                     Ref_Name  => Def_Id.Def_Name,
                     Ref       => Def_Id.Def,
                     Sloc      => Nil),
                Attribute_Designator_Identifier =>
                  Identifier
                    (Expr_Type => +"null",
                     Ref_Name  => +"Size",
                     Ref       => +"null",
                     Sloc      => Nil),
                Sloc => Nil),
           Representation_Clause_Expression =>
             Integer_Literal
               (Expr_Type => +"universal integer",
                Lit_Val   => +Image (DD.Size (Defn)),
                Sloc      => Nil),
           Sloc => Nil);
   begin
      Append (Clauses, Size_Clause);
   end Do_Size_Clause;

   procedure Append_Rep_Clauses
     (L : in out Ada_Tree_Vector;
      E :        Asis.Element;
      T :        Ada_Tree)
   is
      --  This generates the following kinds of representation clauses, where
      --  appropriate:
      --
      --     Size clause
      --     Component_Size clause
      --     record_representation_clause

      pragma Assert
        ((Ekind (E) = An_Ordinary_Type_Declaration)
          = (T.Kind = An_Ordinary_Type_Declaration));

      Defn    : Asis.Element; -- the type definition
      Def_Id  : Ada_Tree_Base; -- the defining id of the type
      Clauses : Ada_Tree_Vector;
      --  Build up the rep clauses in this, and append this to L at the end

   begin
      if Ekind (E) = An_Ordinary_Type_Declaration then
         Def_Id := Names (T).Subtrees (1);
         Defn   := Type_Declaration_View (E);

         if DD.Type_Model_Kind (Defn) in DD.A_Simple_Static_Model then
            --  Generate Size clause

            Do_Size_Clause (Clauses, Def_Id, Defn);

            --  Generate Component_Size clause if it's an array

            if Ekind (Defn) in
                An_Unconstrained_Array_Definition |
                  A_Constrained_Array_Definition
              or else Is_Derived_From_Array (Defn)
            then
               Do_Comp_Size_Clause (Clauses, Def_Id, Defn);
            end if;

            --  Generate record_representation_clause if it's a record

            if Ekind (Defn) = A_Record_Type_Definition
              or else Is_Derived_From_Record (Defn)
            then
               Do_Record_Clause (Clauses, Def_Id, Defn);
            end if;

            pragma Assert (not Is_Empty (Clauses));
            Append (L, Comment (W_Name_Find (Scanner.Gen_Plus)));
            Append (L, To_Array (Clauses));
            Append (L, Comment (W_Name_Find (Scanner.Gen_Minus)));
         end if;
      end if;
   end Append_Rep_Clauses;

end Ada_Trees.Rep_Clauses;