Debian Codegraph @

gcc/ada/gen_il-gen.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G E N _ I L . G E N --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2020-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Ada.Containers; use type Ada.Containers.Count_Type;
27 with Ada.Text_IO;
28
29 package body Gen_IL.Gen is
30
31 Statistics_Enabled : constant Boolean := False;
32 -- Change to True or False to enable/disable statistics printed by
33 -- Atree. Should normally be False, for efficiency. Also compile with
34 -- -gnatd.A to get the statistics printed. Enabling these statistics
35 -- makes the compiler about 20% slower.
36
37 Num_Header_Slots : constant := 3;
38 -- Number of header slots; the first Num_Header_Slots slots are stored in
39 -- the header; the rest are dynamically allocated in the Slots table. We
40 -- need to subtract this off when accessing dynamic slots. The constant
41 -- Seinfo.N_Head will contain this value. Fields that are allocated in the
42 -- header slots are quicker to access.
43 --
44 -- This number can be adjusted for efficiency. We choose 3 because the
45 -- minimum node size is 3 slots, and because that causes the size of type
46 -- Node_Header to be a power of 2. We can't make it zero, however, because
47 -- C doesn't allow zero-length arrays.
48
49 N_Head : constant String := Image (Field_Offset'(Num_Header_Slots));
50 -- String form of the above
51
52 Enable_Assertions : constant Boolean := True;
53 -- True to enable predicates on the _Id types, and preconditions on getters
54 -- and setters.
55
56 Overlay_Fields : constant Boolean := True;
57 -- False to allocate every field so it doesn't overlay any other fields,
58 -- which results in enormous nodes. For experimenting and debugging.
59 -- Should be True in normal operation, for efficiency.
60
61 SS : constant := 32; -- slot size in bits
62 SSS : constant String := Image (Bit_Offset'(SS));
63
64 Inline : constant String := "Inline";
65 -- For experimenting with Inline_Always
66
67 Syntactic : Fields_Per_Node_Type :=
68 (others => (others => False));
69
70 Nodes_And_Entities : constant Type_Vector := Node_Kind & Entity_Kind;
71 All_Entities : constant Type_Vector := To_Vector (Entity_Kind, Length => 1);
72
73 procedure Create_Type
74 (T : Node_Or_Entity_Type;
75 Parent : Opt_Abstract_Type;
76 Fields : Field_Sequence;
77 Nmake_Assert : String);
78 -- Called by the Create_..._Type procedures exported by this package to
79 -- create an entry in the Types_Table.
80
81 procedure Create_Union_Type
82 (Root : Root_Type; T : Abstract_Type; Children : Type_Array);
83 -- Called by Create_Node_Union_Type and Create_Entity_Union_Type to create
84 -- a union type.
85
86 function Create_Field
87 (Field : Field_Enum;
88 Field_Type : Type_Enum;
89 Default_Value : Field_Default_Value;
90 Type_Only : Type_Only_Enum;
91 Pre, Pre_Get, Pre_Set : String;
92 Is_Syntactic : Boolean) return Field_Desc;
93 -- Called by the Create_..._Field functions exported by this package to
94 -- create an entry in the Field_Table. See Create_Syntactic_Field and
95 -- Create_Semantic_Field for additional doc.
96
97 procedure Check_Type (T : Node_Or_Entity_Type);
98 -- Check some "legality" rules for types in the Gen_IL little language
99
100 ----------------
101 -- Check_Type --
102 ----------------
103
104 procedure Check_Type (T : Node_Or_Entity_Type) is
105 Im : constant String := Node_Or_Entity_Type'Image (T);
106 begin
107 if Type_Table (T) /= null then
108 raise Illegal with "duplicate creation of type " & Image (T);
109 end if;
110
111 if T not in Root_Type then
112 case T is
113 when Node_Type =>
114 if Im'Length < 2 or else Im (1 .. 2) /= "N_" then
115 raise Illegal with "Node type names must start with ""N_""";
116 end if;
117
118 when Concrete_Entity =>
119 if Im'Length < 2 or else Im (1 .. 2) /= "E_" then
120 raise Illegal with
121 "Concrete entity type names must start with ""E_""";
122 end if;
123
124 when others => null;
125 -- No special prefix for abstract entities
126 end case;
127 end if;
128 end Check_Type;
129
130 -----------------
131 -- Create_Type --
132 -----------------
133
134 procedure Create_Type
135 (T : Node_Or_Entity_Type;
136 Parent : Opt_Abstract_Type;
137 Fields : Field_Sequence;
138 Nmake_Assert : String)
139 is
140 begin
141 Check_Type (T);
142
143 if T not in Root_Type then
144 if Type_Table (Parent) = null then
145 raise Illegal with
146 "undefined parent type for " &
147 Image (T) & " (parent is " & Image (Parent) & ")";
148 end if;
149
150 if Type_Table (Parent).Is_Union then
151 raise Illegal with
152 "parent type for " &
153 Image (T) & " must not be union (" & Image (Parent) & ")";
154 end if;
155 end if;
156
157 Type_Table (T) :=
158 new Type_Info'
159 (Is_Union => False, Parent => Parent,
160 Children | Concrete_Descendants => Type_Vectors.Empty_Vector,
161 First | Last | Imm_Fields | Fields => <>, -- filled in later
162 Nmake_Assert => new String'(Nmake_Assert));
163
164 if Parent /= No_Type then
165 Append (Type_Table (Parent).Children, T);
166 end if;
167
168 -- Check that syntactic fields precede semantic fields. Note that this
169 -- check is happening before we compute inherited fields.
170 -- Exempt Chars and Actions from this rule, for now.
171
172 declare
173 Semantic_Seen : Boolean := False;
174 begin
175 for J in Fields'Range loop
176 if Fields (J).Is_Syntactic then
177 if Semantic_Seen then
178 raise Illegal with
179 "syntactic fields must precede semantic ones " & Image (T);
180 end if;
181
182 else
183 if Fields (J).F not in Chars | Actions then
184 Semantic_Seen := True;
185 end if;
186 end if;
187 end loop;
188 end;
189
190 -- Check that node fields are in nodes, and entity fields are in
191 -- entities.
192
193 for J in Fields'Range loop
194 declare
195 Field : constant Field_Enum := Fields (J).F;
196 Error_Prefix : constant String :=
197 "Field " & Image (T) & "." & Image (Field) & " not in ";
198 begin
199 case T is
200 when Node_Type =>
201 if Field not in Node_Field then
202 raise Illegal with Error_Prefix & "Node_Field";
203 end if;
204
205 when Entity_Type =>
206 if Field not in Entity_Field then
207 raise Illegal with Error_Prefix & "Entity_Field";
208 end if;
209
210 when Type_Boundaries =>
211 raise Program_Error; -- dummy types shouldn't have fields
212 end case;
213 end;
214 end loop;
215
216 -- Compute the Have_This_Field component of fields, the Fields component
217 -- of the current type, and Syntactic table.
218
219 for J in Fields'Range loop
220 declare
221 Field : constant Field_Enum := Fields (J).F;
222 Is_Syntactic : constant Boolean := Fields (J).Is_Syntactic;
223
224 begin
225 Append (Field_Table (Field).Have_This_Field, T);
226 Append (Type_Table (T).Imm_Fields, Field);
227
228 pragma Assert (not Syntactic (T) (Field));
229 Syntactic (T) (Field) := Is_Syntactic;
230 end;
231 end loop;
232 end Create_Type;
233
234 -- Other than constraint checks on T at the call site, and the lack of a
235 -- parent for root types, the following six all do the same thing.
236
237 ---------------------------
238 -- Create_Root_Node_Type --
239 ---------------------------
240
241 procedure Create_Root_Node_Type
242 (T : Abstract_Node;
243 Fields : Field_Sequence := No_Fields) is
244 begin
245 Create_Type (T, Parent => No_Type, Fields => Fields, Nmake_Assert => "");
246 end Create_Root_Node_Type;
247
248 -------------------------------
249 -- Create_Abstract_Node_Type --
250 -------------------------------
251
252 procedure Create_Abstract_Node_Type
253 (T : Abstract_Node; Parent : Abstract_Type;
254 Fields : Field_Sequence := No_Fields)
255 is
256 begin
257 Create_Type (T, Parent, Fields, Nmake_Assert => "");
258 end Create_Abstract_Node_Type;
259
260 -------------------------------
261 -- Create_Concrete_Node_Type --
262 -------------------------------
263
264 procedure Create_Concrete_Node_Type
265 (T : Concrete_Node; Parent : Abstract_Type;
266 Fields : Field_Sequence := No_Fields;
267 Nmake_Assert : String := "")
268 is
269 begin
270 Create_Type (T, Parent, Fields, Nmake_Assert);
271 end Create_Concrete_Node_Type;
272
273 -----------------------------
274 -- Create_Root_Entity_Type --
275 -----------------------------
276
277 procedure Create_Root_Entity_Type
278 (T : Abstract_Entity;
279 Fields : Field_Sequence := No_Fields) is
280 begin
281 Create_Type (T, Parent => No_Type, Fields => Fields, Nmake_Assert => "");
282 end Create_Root_Entity_Type;
283
284 ---------------------------------
285 -- Create_Abstract_Entity_Type --
286 ---------------------------------
287
288 procedure Create_Abstract_Entity_Type
289 (T : Abstract_Entity; Parent : Abstract_Type;
290 Fields : Field_Sequence := No_Fields)
291 is
292 begin
293 Create_Type (T, Parent, Fields, Nmake_Assert => "");
294 end Create_Abstract_Entity_Type;
295
296 ---------------------------------
297 -- Create_Concrete_Entity_Type --
298 ---------------------------------
299
300 procedure Create_Concrete_Entity_Type
301 (T : Concrete_Entity; Parent : Abstract_Type;
302 Fields : Field_Sequence := No_Fields)
303 is
304 begin
305 Create_Type (T, Parent, Fields, Nmake_Assert => "");
306 end Create_Concrete_Entity_Type;
307
308 ------------------
309 -- Create_Field --
310 ------------------
311
312 function Create_Field
313 (Field : Field_Enum;
314 Field_Type : Type_Enum;
315 Default_Value : Field_Default_Value;
316 Type_Only : Type_Only_Enum;
317 Pre, Pre_Get, Pre_Set : String;
318 Is_Syntactic : Boolean) return Field_Desc
319 is
320 begin
321 -- Note that this function has the side effect of update the
322 -- Field_Table.
323
324 pragma Assert (if Default_Value /= No_Default then Is_Syntactic);
325 pragma Assert (if Type_Only /= No_Type_Only then not Is_Syntactic);
326
327 -- First time this field has been seen; create an entry in the
328 -- Field_Table.
329
330 if Field_Table (Field) = null then
331 Field_Table (Field) := new Field_Info'
332 (Type_Vectors.Empty_Vector, Field_Type, Default_Value, Type_Only,
333 Pre => new String'(Pre),
334 Pre_Get => new String'(Pre_Get),
335 Pre_Set => new String'(Pre_Set),
336 Offset => Unknown_Offset);
337
338 -- The Field_Table entry has already been created by the 'then' part
339 -- above. Now we're seeing the same field being "created" again in a
340 -- different type. Here we check consistency of this new Create_Field
341 -- call with the old one.
342
343 else
344 if Field_Type /= Field_Table (Field).Field_Type then
345 raise Illegal with
346 "mismatched field types for " & Image (Field);
347 end if;
348
349 -- Check that default values for syntactic fields match. This check
350 -- could be stricter; it currently allows a field to have No_Default
351 -- in one type, but something else in another type. In that case, we
352 -- use the "something else" for all types.
353 --
354 -- Note that the order of calls does not matter; a default value
355 -- always overrides a No_Default value.
356
357 if Is_Syntactic then
358 if Default_Value /= Field_Table (Field).Default_Value then
359 if Field_Table (Field).Default_Value = No_Default then
360 Field_Table (Field).Default_Value := Default_Value;
361 else
362 raise Illegal with
363 "mismatched default values for " & Image (Field);
364 end if;
365 end if;
366 end if;
367
368 if Type_Only /= Field_Table (Field).Type_Only then
369 raise Illegal with "mismatched Type_Only for " & Image (Field);
370 end if;
371
372 if Pre /= Field_Table (Field).Pre.all then
373 raise Illegal with
374 "mismatched extra preconditions for " & Image (Field);
375 end if;
376
377 if Pre_Get /= Field_Table (Field).Pre_Get.all then
378 raise Illegal with
379 "mismatched extra getter-only preconditions for " &
380 Image (Field);
381 end if;
382
383 if Pre_Set /= Field_Table (Field).Pre_Set.all then
384 raise Illegal with
385 "mismatched extra setter-only preconditions for " &
386 Image (Field);
387 end if;
388 end if;
389
390 return (Field, Is_Syntactic);
391 end Create_Field;
392
393 ----------------------------
394 -- Create_Syntactic_Field --
395 ----------------------------
396
397 function Create_Syntactic_Field
398 (Field : Node_Field;
399 Field_Type : Type_Enum;
400 Default_Value : Field_Default_Value := No_Default;
401 Pre, Pre_Get, Pre_Set : String := "") return Field_Desc
402 is
403 begin
404 return Create_Field
405 (Field, Field_Type, Default_Value, No_Type_Only,
406 Pre, Pre_Get, Pre_Set,
407 Is_Syntactic => True);
408 end Create_Syntactic_Field;
409
410 ---------------------------
411 -- Create_Semantic_Field --
412 ---------------------------
413
414 function Create_Semantic_Field
415 (Field : Field_Enum;
416 Field_Type : Type_Enum;
417 Type_Only : Type_Only_Enum := No_Type_Only;
418 Pre, Pre_Get, Pre_Set : String := "") return Field_Desc
419 is
420 begin
421 return Create_Field
422 (Field, Field_Type, No_Default, Type_Only,
423 Pre, Pre_Get, Pre_Set,
424 Is_Syntactic => False);
425 end Create_Semantic_Field;
426
427 -----------------------
428 -- Create_Union_Type --
429 -----------------------
430
431 procedure Create_Union_Type
432 (Root : Root_Type; T : Abstract_Type; Children : Type_Array)
433 is
434 Children_Seen : Type_Set := (others => False);
435
436 begin
437 Check_Type (T);
438
439 if Children'Length <= 1 then
440 raise Illegal with Image (T) & " must have two or more children";
441 end if;
442
443 for Child of Children loop
444 if Children_Seen (Child) then
445 raise Illegal with
446 Image (T) & " has duplicate child " & Image (Child);
447 end if;
448
449 Children_Seen (Child) := True;
450
451 if Type_Table (Child) = null then
452 raise Illegal with
453 "undefined child type for " &
454 Image (T) & " (child is " & Image (Child) & ")";
455 end if;
456 end loop;
457
458 Type_Table (T) :=
459 new Type_Info'
460 (Is_Union => True, Parent => Root,
461 Children | Concrete_Descendants => Type_Vectors.Empty_Vector);
462
463 for Child of Children loop
464 Append (Type_Table (T).Children, Child);
465 end loop;
466 end Create_Union_Type;
467
468 ----------------------------
469 -- Create_Node_Union_Type --
470 ----------------------------
471
472 procedure Create_Node_Union_Type
473 (T : Abstract_Node; Children : Type_Array) is
474 begin
475 Create_Union_Type (Node_Kind, T, Children);
476 end Create_Node_Union_Type;
477
478 ------------------------------
479 -- Create_Entity_Union_Type --
480 ------------------------------
481
482 procedure Create_Entity_Union_Type
483 (T : Abstract_Entity; Children : Type_Array) is
484 begin
485 Create_Union_Type (Entity_Kind, T, Children);
486 end Create_Entity_Union_Type;
487
488 -------------
489 -- Compile --
490 -------------
491
492 procedure Compile is
493 Fields_Per_Node : Fields_Per_Node_Type := (others => (others => False));
494 -- Mapping from node types to sets of fields that exist in that node
495 -- type. For abstract types, it's the set of fields that exist in
496 -- all descendants. For union types, currently not used.
497
498 Type_Bit_Size : array (Concrete_Type) of Bit_Offset := (others => 0);
499 Min_Node_Bit_Size : Bit_Offset := Bit_Offset'Last;
500 Max_Node_Bit_Size : Bit_Offset := 0;
501 Min_Entity_Bit_Size : Bit_Offset := Bit_Offset'Last;
502 Max_Entity_Bit_Size : Bit_Offset := 0;
503 -- Above are in units of bits; following are in units of slots:
504 Min_Node_Size : Field_Offset := Field_Offset'Last;
505 Max_Node_Size : Field_Offset := 0;
506 Min_Entity_Size : Field_Offset := Field_Offset'Last;
507 Max_Entity_Size : Field_Offset := 0;
508
509 Node_Field_Types_Used, Entity_Field_Types_Used : Type_Set;
510
511 Setter_Needs_Parent : Field_Set :=
512 (Actions | Expression | Then_Actions | Else_Actions => True,
513 others => False);
514 -- Set of fields where the setter should set the Parent. True for
515 -- syntactic fields of type Node_Id and List_Id, but with some
516 -- exceptions. Expression is syntactic AND semantic, and the Parent
517 -- is needed. Default_Expression is also both, but the Parent is not
518 -- needed. Then_Actions and Else_Actions are not syntactic, but the
519 -- Parent is needed.
520 --
521 -- Computed in Check_For_Syntactic_Field_Mismatch.
522
523 procedure Check_Completeness;
524 -- Check that every type and field has been declared
525
526 procedure Compute_Ranges;
527 -- Compute the range of Node_Kind/Entity_Kind values. The result is
528 -- stored in the First and Last components in the Type_Table.
529
530 procedure Inherit_Fields;
531 -- Compute which fields are in which nodes. Implements inheritance of
532 -- fields. Set the Fields component of each Type_Info to include
533 -- inherited ones. Check for misc errors.
534
535 procedure Compute_Fields_Per_Node;
536 -- Set the Is_Syntactic component in the Type_Table to the set of fields
537 -- that are syntactic in that node kind. Set the Fields_Per_Node
538 -- table. Check for misc errors.
539
540 procedure Compute_Field_Offsets;
541 -- Compute the offsets of each field. The results are stored in the
542 -- Offset components in the Field_Table.
543
544 procedure Compute_Type_Sizes;
545 -- Compute the size of each node and entity type, which is one more than
546 -- the maximum bit offset of all fields of the type. Results are
547 -- returned in the above Type_Bit_Size and Min_.../Max_... variables.
548
549 procedure Check_For_Syntactic_Field_Mismatch;
550 -- Check that fields are either all syntactic or all semantic in all
551 -- nodes in which they exist, except for some fields that already
552 -- violate this rule.
553 --
554 -- Also sets Setter_Needs_Parent.
555
556 function Field_Types_Used (First, Last : Field_Enum) return Type_Set;
557 -- Returns the union of the types of all the fields in the range First
558 -- .. Last. Only Special_Type; if the declared type of a field is a
559 -- descendant of Node_Kind or Entity_Kind, then the low-level getter for
560 -- Node_Id can be used.
561
562 procedure Put_Seinfo;
563 -- Print out the Seinfo package, which is with'ed by both Sinfo.Nodes
564 -- and Einfo.Entities.
565
566 procedure Put_Nodes;
567 -- Print out the Sinfo.Nodes package spec and body
568
569 procedure Put_Entities;
570 -- Print out the Einfo.Entities package spec and body
571
572 procedure Put_Type_And_Subtypes
573 (S : in out Sink; Root : Root_Type);
574 -- Called by Put_Nodes and Put_Entities to print out the main type
575 -- and subtype declarations in Sinfo.Nodes and Einfo.Entities.
576
577 procedure Put_Subp_Decls (S : in out Sink; Root : Root_Type);
578 -- Called by Put_Nodes and Put_Entities to print out the subprogram
579 -- declarations in Sinfo.Nodes and Einfo.Entities.
580
581 procedure Put_Subp_Bodies (S : in out Sink; Root : Root_Type);
582 -- Called by Put_Nodes and Put_Entities to print out the subprogram
583 -- bodies in Sinfo.Nodes and Einfo.Entities.
584
585 function Node_To_Fetch_From (F : Field_Enum) return String;
586 -- Name of the Node from which a getter should fetch the value.
587 -- Normally, we fetch from the node or entity passed in (i.e. formal
588 -- parameter N). But if Type_Only was specified, we need to fetch the
589 -- corresponding base (etc) type.
590
591 procedure Put_Getter_Spec (S : in out Sink; F : Field_Enum);
592 procedure Put_Setter_Spec (S : in out Sink; F : Field_Enum);
593 procedure Put_Getter_Decl (S : in out Sink; F : Field_Enum);
594 procedure Put_Setter_Decl (S : in out Sink; F : Field_Enum);
595 procedure Put_Getter_Setter_Locals
596 (S : in out Sink; F : Field_Enum; Get : Boolean);
597 procedure Put_Getter_Body (S : in out Sink; F : Field_Enum);
598 procedure Put_Setter_Body (S : in out Sink; F : Field_Enum);
599 -- Print out the specification, declaration, or body of a getter or
600 -- setter for the given field.
601
602 procedure Put_Precondition
603 (S : in out Sink; F : Field_Enum);
604 -- Print out the precondition, if any, for a getter or setter for the
605 -- given field.
606
607 procedure Put_Casts
608 (S : in out Sink; T : Type_Enum);
609 -- Print out the Cast functions for a given type
610
611 procedure Put_Traversed_Fields (S : in out Sink);
612 -- Called by Put_Nodes to print out the Traversed_Fields table in
613 -- Sinfo.Nodes.
614
615 procedure Put_Tables (S : in out Sink; Root : Root_Type);
616 -- Called by Put_Nodes and Put_Entities to print out the various tables
617 -- in Sinfo.Nodes and Einfo.Entities.
618
619 procedure Put_Nmake;
620 -- Print out the Nmake package spec and body, containing
621 -- Make_... functions for each concrete node type.
622
623 procedure Put_Make_Decls (S : in out Sink; Root : Root_Type);
624 -- Called by Put_Nmake to print out the Make_... function declarations
625
626 procedure Put_Make_Bodies (S : in out Sink; Root : Root_Type);
627 -- Called by Put_Nmake to print out the Make_... function bodies
628
629 procedure Put_Make_Spec
630 (S : in out Sink; Root : Root_Type; T : Concrete_Type);
631 -- Called by Put_Make_Decls and Put_Make_Bodies to print out the spec of
632 -- a single Make_... function.
633
634 procedure Put_Seinfo_Tables;
635 -- This puts information about both sinfo and einfo.
636 -- Not actually needed by the compiler.
637
638 procedure Put_Sinfo_Dot_H;
639 -- Print out the sinfo.h file
640
641 procedure Put_Einfo_Dot_H;
642 -- Print out the einfo.h file
643
644 procedure Put_C_Type_And_Subtypes
645 (S : in out Sink; Root : Root_Type);
646 -- Used by Put_Sinfo_Dot_H and Put_Einfo_Dot_H to print out the C code
647 -- corresponding to the Ada Node_Kind, Entity_Kind, and subtypes
648 -- thereof.
649
650 procedure Put_C_Getters
651 (S : in out Sink; Root : Root_Type);
652 -- Used by Put_Sinfo_Dot_H and Put_Einfo_Dot_H to print out high-level
653 -- getters.
654
655 procedure Put_C_Getter
656 (S : in out Sink; F : Field_Enum);
657 -- Used by Put_C_Getters to print out one high-level getter.
658
659 procedure Put_Union_Membership
660 (S : in out Sink; Root : Root_Type; Only_Prototypes : Boolean);
661 -- Used by Put_Sinfo_Dot_H and Put_Einfo_Dot_H to print out functions to
662 -- test membership in a union type.
663
664 ------------------------
665 -- Check_Completeness --
666 ------------------------
667
668 procedure Check_Completeness is
669 begin
670 for T in Node_Or_Entity_Type loop
671 if Type_Table (T) = null and then T not in Type_Boundaries then
672 raise Illegal with "Missing type declaration for " & Image (T);
673 end if;
674 end loop;
675
676 for F in Field_Enum loop
677 if Field_Table (F) = null
678 and then F /= Between_Node_And_Entity_Fields
679 then
680 raise Illegal with "Missing field declaration for " & Image (F);
681 end if;
682 end loop;
683 end Check_Completeness;
684
685 --------------------
686 -- Compute_Ranges --
687 --------------------
688
689 procedure Compute_Ranges is
690
691 procedure Do_One_Type (T : Node_Or_Entity_Type);
692 -- Compute the range for one type. Passed to Iterate_Types to process
693 -- all of them.
694
695 procedure Add_Concrete_Descendant_To_Ancestors
696 (Ancestor : Abstract_Type; Descendant : Concrete_Type);
697 -- Add Descendant to the Concrete_Descendants of each of its
698 -- ancestors.
699
700 procedure Add_Concrete_Descendant_To_Ancestors
701 (Ancestor : Abstract_Type; Descendant : Concrete_Type) is
702 begin
703 if Ancestor not in Root_Type then
704 Add_Concrete_Descendant_To_Ancestors
705 (Type_Table (Ancestor).Parent, Descendant);
706 end if;
707
708 Append (Type_Table (Ancestor).Concrete_Descendants, Descendant);
709 end Add_Concrete_Descendant_To_Ancestors;
710
711 procedure Do_One_Type (T : Node_Or_Entity_Type) is
712 begin
713 case T is
714 when Concrete_Type =>
715 pragma Annotate (Codepeer, Modified, Type_Table);
716 Type_Table (T).First := T;
717 Type_Table (T).Last := T;
718 Add_Concrete_Descendant_To_Ancestors
719 (Type_Table (T).Parent, T);
720 -- Parent cannot be No_Type here, because T is a concrete
721 -- type, and therefore not a root type.
722
723 when Abstract_Type =>
724 declare
725 Children : Type_Vector renames Type_Table (T).Children;
726 begin
727 -- Ensure that an abstract type is not a leaf in the type
728 -- hierarchy.
729
730 if Is_Empty (Children) then
731 raise Illegal with Image (T) & " has no children";
732 end if;
733
734 -- We could support abstract types with only one child,
735 -- but what's the point of having such a type?
736
737 if Last_Index (Children) = 1 then
738 raise Illegal with Image (T) & " has only one child";
739 end if;
740
741 Type_Table (T).First := Type_Table (Children (1)).First;
742 Type_Table (T).Last :=
743 Type_Table (Children (Last_Index (Children))).Last;
744
745 -- We know that each abstract type has at least two
746 -- children. The concrete types must be ordered so
747 -- that each abstract type is a contiguous subrange.
748
749 if Type_Table (T).First >= Type_Table (T).Last then
750 raise Illegal with
751 Image (T) & " children out of order";
752 end if;
753 end;
754
755 when Between_Abstract_Entity_And_Concrete_Node_Types =>
756 raise Program_Error;
757 end case;
758 end Do_One_Type;
759 begin
760 Iterate_Types (Node_Kind, Post => Do_One_Type'Access);
761 Iterate_Types (Entity_Kind, Post => Do_One_Type'Access);
762 end Compute_Ranges;
763
764 --------------------
765 -- Inherit_Fields --
766 --------------------
767
768 procedure Inherit_Fields is
769 procedure Inherit_Fields_For_One_Type (T : Node_Or_Entity_Type);
770 -- Compute Fields for one node type
771
772 function Get_Syntactic_Fields
773 (T : Node_Or_Entity_Type) return Field_Set;
774 -- Compute the set of fields that are syntactic for a given type.
775 -- Note that a field can be syntactic in some node types, but
776 -- semantic in others.
777
778 function Get_Syntactic_Fields
779 (T : Node_Or_Entity_Type) return Field_Set
780 is
781 Parent_Is_Syntactic : constant Field_Set :=
782 (if T in Root_Type then (Field_Enum => False)
783 else Get_Syntactic_Fields (Type_Table (T).Parent));
784 begin
785 return Parent_Is_Syntactic or Syntactic (T);
786 end Get_Syntactic_Fields;
787
788 procedure Inherit_Fields_For_One_Type (T : Node_Or_Entity_Type) is
789 begin
790 pragma Assert (Is_Empty (Type_Table (T).Fields));
791
792 if T not in Root_Type then
793 Append
794 (Type_Table (T).Fields,
795 Type_Table (Type_Table (T).Parent).Fields);
796 end if;
797
798 Append (Type_Table (T).Fields, Type_Table (T).Imm_Fields);
799 Syntactic (T) := Get_Syntactic_Fields (T);
800 end Inherit_Fields_For_One_Type;
801
802 begin -- Inherit_Fields
803
804 Iterate_Types (Node_Kind, Pre => Inherit_Fields_For_One_Type'Access);
805
806 -- The node fields defined for all three N_Entity kinds should be the
807 -- same:
808
809 if Type_Table (N_Defining_Character_Literal).Fields /=
810 Type_Table (N_Defining_Identifier).Fields
811 then
812 raise Illegal with
813 "fields for N_Defining_Identifier and " &
814 "N_Defining_Character_Literal must match";
815 end if;
816
817 if Type_Table (N_Defining_Operator_Symbol).Fields /=
818 Type_Table (N_Defining_Identifier).Fields
819 then
820 raise Illegal with
821 "fields for N_Defining_Identifier and " &
822 "N_Defining_Operator_Symbol must match";
823 end if;
824
825 -- Copy node fields from N_Entity nodes to entities, so they have
826 -- slots allocated (but the getters and setters are only in
827 -- Sinfo.Nodes).
828
829 Type_Table (Entity_Kind).Imm_Fields :=
830 Type_Table (N_Defining_Identifier).Fields &
831 Type_Table (Entity_Kind).Imm_Fields;
832
833 Iterate_Types
834 (Entity_Kind, Pre => Inherit_Fields_For_One_Type'Access);
835 end Inherit_Fields;
836
837 -----------------------------
838 -- Compute_Fields_Per_Node --
839 -----------------------------
840
841 procedure Compute_Fields_Per_Node is
842 Duplicate_Fields_Error, Could_Be_Inherited_Error : Boolean := False;
843
844 procedure Compute_Fields_For_One_Type (T : Node_Or_Entity_Type);
845 -- Do the computations for one type
846
847 procedure Check_Potential_Inheritance (T : Node_Or_Entity_Type);
848 -- Check whether fields could be inherited from T, instead of
849 -- defining them separately for descendants.
850
851 procedure Compute_Fields_For_One_Type (T : Node_Or_Entity_Type) is
852 begin
853 case T is
854 when Concrete_Type =>
855 for F of Type_Table (T).Fields loop
856 if Fields_Per_Node (T) (F) then
857 Ada.Text_IO.Put_Line
858 ("duplicate field " & Image (T) & " " & Image (F));
859 Duplicate_Fields_Error := True;
860 end if;
861
862 Fields_Per_Node (T) (F) := True;
863 end loop;
864
865 when Abstract_Type =>
866 -- Fields_Per_Node for an abstract type is the set of fields
867 -- that exist in ALL children; that is, the intersection of
868 -- the Fields_Per_Node for the children; hence "and" below.
869
870 pragma Assert (not Is_Empty (Type_Table (T).Children));
871 -- Otherwise, the following loop won't work
872
873 pragma Assert (Fields_Per_Node (T) = (Field_Enum => False));
874 Fields_Per_Node (T) := (Field_Enum => True);
875
876 for Child of Type_Table (T).Children loop
877 pragma Assert
878 (Fields_Per_Node (Child) /= (Field_Enum => False));
879 Fields_Per_Node (T) :=
880 Fields_Per_Node (T) and Fields_Per_Node (Child);
881 end loop;
882
883 when Between_Abstract_Entity_And_Concrete_Node_Types =>
884 raise Program_Error;
885 end case;
886 end Compute_Fields_For_One_Type;
887
888 procedure Check_Potential_Inheritance (T : Node_Or_Entity_Type) is
889
890 function Exception_To_Inheritance_Rule
891 (T : Node_Or_Entity_Type; F : Field_Enum) return Boolean is
892 -- True if we should allow this case as an exception to
893 -- the Could_Be_Inherited_Error rule; if this is False,
894 -- we complain. This is somewhat ad hoc. The most common
895 -- reason is to keep syntactic fields in order.
896 -- For example, Left_Opnd comes before Right_Opnd,
897 -- which wouldn't be the case if Right_Opnd were
898 -- inherited from N_Op.
899 ((T = N_Op and then F = Right_Opnd)
900 or else (T = N_Renaming_Declaration and then F = Name)
901 or else (T = N_Generic_Renaming_Declaration and then F = Name)
902 or else F in Defining_Unit_Name
903 | Aspect_Specifications
904 | At_End_Proc
905 | Handled_Statement_Sequence
906 | Declarations
907 | Generic_Formal_Declarations
908 | Specification
909 | Component_Definition
910 | Renamed_Or_Alias
911 or else T in N_Subprogram_Specification
912 | N_Access_Function_Definition
913 | N_Access_To_Subprogram_Definition
914 | Void_Or_Type_Kind);
915
916 begin
917 if T in Abstract_Type then
918 for F in Field_Enum loop
919 if Fields_Per_Node (T) (F)
920 and then not
921 (for some FF of Type_Table (T).Fields => F = FF)
922 and then not Exception_To_Inheritance_Rule (T, F)
923 then
924 Ada.Text_IO.Put_Line
925 (Image (F) & " could be inherited from " & Image (T) &
926 "; this field is present in all descendants");
927 Could_Be_Inherited_Error := True;
928 end if;
929 end loop;
930 end if;
931 end Check_Potential_Inheritance;
932
933 begin -- Compute_Fields_Per_Node
934
935 -- First walk the types bottom-up, and for each type, call
936 -- Compute_Fields_For_One_Type. Then walk top-down, calling
937 -- Check_Potential_Inheritance to check for cases where
938 -- inheritance could be used.
939
940 Iterate_Types (Node_Kind, Post => Compute_Fields_For_One_Type'Access);
941
942 -- The node fields defined for all three N_Entity kinds should be the
943 -- same:
944
945 if Fields_Per_Node (N_Defining_Character_Literal) /=
946 Fields_Per_Node (N_Defining_Identifier)
947 then
948 raise Illegal with
949 "Fields of N_Defining_Character_Literal must match " &
950 "N_Defining_Identifier";
951 end if;
952
953 if Fields_Per_Node (N_Defining_Operator_Symbol) /=
954 Fields_Per_Node (N_Defining_Identifier)
955 then
956 raise Illegal with
957 "Fields of N_Defining_Operator_Symbol must match " &
958 "N_Defining_Identifier";
959 end if;
960
961 Iterate_Types
962 (Entity_Kind, Post => Compute_Fields_For_One_Type'Access);
963
964 if Duplicate_Fields_Error then
965 raise Illegal with "duplicate fields found";
966 end if;
967
968 for CT in Concrete_Node loop
969 pragma Assert (Fields_Per_Node (CT) /= (Field_Enum => False));
970 end loop;
971 for CT in Concrete_Entity loop
972 pragma Assert (Fields_Per_Node (CT) /= (Field_Enum => False));
973 end loop;
974 for AbT in Abstract_Node loop
975 pragma Assert
976 (Type_Table (AbT).Is_Union = -- if and only if
977 (Fields_Per_Node (AbT) = (Field_Enum => False)));
978 end loop;
979 for AbT in Abstract_Entity loop
980 pragma Assert
981 (Type_Table (AbT).Is_Union = -- if and only if
982 (Fields_Per_Node (AbT) = (Field_Enum => False)));
983 end loop;
984
985 Iterate_Types (Node_Kind, Pre => Check_Potential_Inheritance'Access);
986 Iterate_Types
987 (Entity_Kind, Pre => Check_Potential_Inheritance'Access);
988
989 if Could_Be_Inherited_Error then
990 raise Illegal with "some fields could be inherited";
991 -- If you get this error, then either move the relevant fields
992 -- upward in the type hierarchy, or add a case to the
993 -- Exception_To_Inheritance_Rule function above.
994 -- We don't always want to use inheritance when it's possible;
995 -- for example, we might want to control the order of fields.
996 end if;
997 end Compute_Fields_Per_Node;
998
999 function Field_Size (T : Type_Enum) return Bit_Offset is
1000 (case T is
1001 when Flag => 1,
1002
1003 when Small_Paren_Count_Type | Component_Alignment_Kind => 2,
1004
1005 when Node_Kind_Type | Entity_Kind_Type | Convention_Id => 8,
1006
1007 when Mechanism_Type
1008 | List_Id
1009 | Elist_Id
1010 | Name_Id
1011 | String_Id
1012 | Uint
1013 | Uint_Subtype
1014 | Ureal
1015 | Source_File_Index
1016 | Source_Ptr
1017 | Union_Id
1018 | Node_Id
1019 | Node_Or_Entity_Type => 32,
1020
1021 when Between_Special_And_Abstract_Node_Types => -- can't happen
1022 Bit_Offset'Last);
1023 -- Size in bits of a a field of type T. It must be a power of 2, and
1024 -- must match the size of the type in GNAT, which sometimes requires
1025 -- a Size clause in GNAT.
1026 --
1027 -- Note that this is not the same as Type_Bit_Size of the field's
1028 -- type. For one thing, Type_Bit_Size only covers concrete node and
1029 -- entity types, which does not include most of the above. For
1030 -- another thing, Type_Bit_Size includes the full size of all the
1031 -- fields, whereas a field of a node or entity type is just a 32-bit
1032 -- Node_Id or Entity_Id; i.e. it is indirect.
1033
1034 function Field_Size (F : Field_Enum) return Bit_Offset is
1035 (Field_Size (Field_Table (F).Field_Type));
1036
1037 function To_Bit_Offset (F : Field_Enum; Offset : Field_Offset'Base)
1038 return Bit_Offset'Base is
1039 (Bit_Offset'Base (Offset) * Field_Size (F));
1040 function First_Bit (F : Field_Enum; Offset : Field_Offset)
1041 return Bit_Offset is
1042 (To_Bit_Offset (F, Offset));
1043 function Last_Bit (F : Field_Enum; Offset : Field_Offset)
1044 return Bit_Offset is
1045 (To_Bit_Offset (F, Offset + 1) - 1);
1046
1047 function To_Size_In_Slots (Size_In_Bits : Bit_Offset)
1048 return Field_Offset is
1049 ((Field_Offset (Size_In_Bits) + (SS - 1)) / SS);
1050
1051 function Type_Size_In_Slots (T : Concrete_Type) return Field_Offset is
1052 (To_Size_In_Slots (Type_Bit_Size (T))); -- rounded up to slot boundary
1053
1054 function Type_Bit_Size_Aligned (T : Concrete_Type) return Bit_Offset is
1055 (Bit_Offset (Type_Size_In_Slots (T)) * SS); -- multiple of slot size
1056
1057 ---------------------------
1058 -- Compute_Field_Offsets --
1059 ---------------------------
1060
1061 procedure Compute_Field_Offsets is
1062 type Offset_Set_Unconstrained is array (Bit_Offset range <>)
1063 of Boolean with Pack;
1064 subtype Offset_Set is Offset_Set_Unconstrained (Bit_Offset);
1065 Offset_Sets : array (Concrete_Type) of Offset_Set :=
1066 (others => (others => False));
1067
1068 function All_False
1069 (F : Field_Enum; Offset : Field_Offset)
1070 return Offset_Set_Unconstrained is
1071 (First_Bit (F, Offset) .. Last_Bit (F, Offset) => False);
1072
1073 function All_True
1074 (F : Field_Enum; Offset : Field_Offset)
1075 return Offset_Set_Unconstrained is
1076 (First_Bit (F, Offset) .. Last_Bit (F, Offset) => True);
1077
1078 function Offset_OK
1079 (F : Field_Enum; Offset : Field_Offset) return Boolean;
1080 -- True if it is OK to choose this offset; that is, if this offset is
1081 -- not in use for any type that has the field. If Overlay_Fields is
1082 -- False, then "any type that has the field" --> "any type, whether
1083 -- or not it has the field".
1084
1085 procedure Set_Offset_In_Use
1086 (F : Field_Enum; Offset : Field_Offset);
1087 -- Mark the offset as "in use"
1088
1089 procedure Choose_Offset (F : Field_Enum);
1090 -- Choose an offset for this field
1091
1092 function Offset_OK
1093 (F : Field_Enum; Offset : Field_Offset) return Boolean is
1094 begin
1095 for T in Concrete_Type loop
1096 if Fields_Per_Node (T) (F) or else not Overlay_Fields then
1097 declare
1098 Bits : Offset_Set_Unconstrained renames
1099 Offset_Sets (T)
1100 (First_Bit (F, Offset) .. Last_Bit (F, Offset));
1101 begin
1102 if Bits /= All_False (F, Offset) then
1103 return False;
1104 end if;
1105 end;
1106 end if;
1107 end loop;
1108
1109 return True;
1110 end Offset_OK;
1111
1112 procedure Set_Offset_In_Use
1113 (F : Field_Enum; Offset : Field_Offset) is
1114 begin
1115 for T in Concrete_Type loop
1116 if Fields_Per_Node (T) (F) then
1117 declare
1118 Bits : Offset_Set_Unconstrained renames
1119 Offset_Sets (T)
1120 (First_Bit (F, Offset) .. Last_Bit (F, Offset));
1121 begin
1122 pragma Assert (Bits = All_False (F, Offset));
1123 Bits := All_True (F, Offset);
1124 end;
1125 end if;
1126 end loop;
1127 end Set_Offset_In_Use;
1128
1129 procedure Choose_Offset (F : Field_Enum) is
1130 begin
1131 for Offset in Field_Offset loop
1132 if Offset_OK (F, Offset) then
1133 Set_Offset_In_Use (F, Offset);
1134
1135 Field_Table (F).Offset := Offset;
1136 return;
1137 end if;
1138 end loop;
1139
1140 raise Illegal with "No available field offset for " & Image (F) &
1141 "; need to increase Gen_IL.Internals.Bit_Offset'Last (" &
1142 Image (Gen_IL.Internals.Bit_Offset'Last) & " is too small)";
1143 end Choose_Offset;
1144
1145 Weighted_Node_Frequency : array (Field_Enum) of Type_Count :=
1146 (others => 0);
1147 -- Number of concrete types that have each field
1148
1149 function More_Types_Have_Field (F1, F2 : Field_Enum) return Boolean is
1150 (Weighted_Node_Frequency (F1) > Weighted_Node_Frequency (F2));
1151 -- True if F1 appears in more concrete types than F2
1152
1153 function Sort_Less (F1, F2 : Field_Enum) return Boolean is
1154 (if Weighted_Node_Frequency (F1) = Weighted_Node_Frequency (F2) then
1155 F1 < F2
1156 else More_Types_Have_Field (F1, F2));
1157
1158 package Sorting is new Field_Vectors.Generic_Sorting
1159 ("<" => Sort_Less);
1160
1161 Fields : Field_Vector;
1162
1163 -- Start of processing for Compute_Field_Offsets
1164
1165 begin
1166 -- Compute the number of concrete types that have each field,
1167 -- weighted by the frequency of such nodes.
1168
1169 for T in Concrete_Type loop
1170 for F in Field_Enum loop
1171 if Fields_Per_Node (T) (F) then
1172 Weighted_Node_Frequency (F) :=
1173 Weighted_Node_Frequency (F) + Type_Frequency (T);
1174 end if;
1175 end loop;
1176 end loop;
1177
1178 -- Collect all the fields in Fields
1179
1180 for F in Node_Field loop
1181 Append (Fields, F);
1182 end loop;
1183
1184 for F in Entity_Field loop
1185 Append (Fields, F);
1186 end loop;
1187
1188 -- Sort Fields based on how many concrete types have the field.
1189 -- This is for efficiency; we want to choose the offsets of the most
1190 -- common fields first, so they get low numbers.
1191
1192 Sorting.Sort (Fields);
1193
1194 -- Go through all the fields, and choose the lowest offset that is
1195 -- free in all types that have the field. This is basically a
1196 -- graph-coloring algorithm on the interference graph. The
1197 -- interference graph is an undirected graph with the fields being
1198 -- nodes (not nodes in the compiler!) in the graph, and an edge
1199 -- between a pair of fields if they appear in the same node in the
1200 -- compiler. The "colors" are fields offsets, except that a
1201 -- complication compared to standard graph coloring is that fields
1202 -- are different sizes.
1203
1204 -- First choose offsets for some heavily-used fields, so they will
1205 -- get low offsets, so they will wind up in the node header for
1206 -- faster access.
1207
1208 Choose_Offset (Nkind);
1209 pragma Assert (Field_Table (Nkind).Offset = 0);
1210 Choose_Offset (Ekind);
1211 pragma Assert (Field_Table (Ekind).Offset = 1);
1212 Choose_Offset (Homonym);
1213 pragma Assert (Field_Table (Homonym).Offset = 1);
1214 Choose_Offset (Is_Immediately_Visible);
1215 pragma Assert (Field_Table (Is_Immediately_Visible).Offset = 16);
1216 Choose_Offset (From_Limited_With);
1217 pragma Assert (Field_Table (From_Limited_With).Offset = 17);
1218 Choose_Offset (Is_Potentially_Use_Visible);
1219 pragma Assert (Field_Table (Is_Potentially_Use_Visible).Offset = 18);
1220 Choose_Offset (Is_Generic_Instance);
1221 pragma Assert (Field_Table (Is_Generic_Instance).Offset = 19);
1222 Choose_Offset (Scope);
1223 pragma Assert (Field_Table (Scope).Offset = 2);
1224
1225 -- Then loop through them all, skipping the ones we did above
1226
1227 for F of Fields loop
1228 if Field_Table (F).Offset = Unknown_Offset then
1229 Choose_Offset (F);
1230 end if;
1231 end loop;
1232
1233 end Compute_Field_Offsets;
1234
1235 ------------------------
1236 -- Compute_Type_Sizes --
1237 ------------------------
1238
1239 procedure Compute_Type_Sizes is
1240 begin
1241 for T in Concrete_Type loop
1242 declare
1243 Max_Offset : Bit_Offset := 0;
1244
1245 begin
1246 for F in Field_Enum loop
1247 if Fields_Per_Node (T) (F) then
1248 Max_Offset :=
1249 Bit_Offset'Max
1250 (Max_Offset,
1251 To_Bit_Offset (F, Field_Table (F).Offset));
1252 end if;
1253 end loop;
1254
1255 -- No type can be smaller than the header slots
1256
1257 Type_Bit_Size (T) :=
1258 Bit_Offset'Max (Max_Offset + 1, SS * Num_Header_Slots);
1259 end;
1260 end loop;
1261
1262 for T in Concrete_Node loop
1263 Min_Node_Bit_Size :=
1264 Bit_Offset'Min (Min_Node_Bit_Size, Type_Bit_Size (T));
1265 Max_Node_Bit_Size :=
1266 Bit_Offset'Max (Max_Node_Bit_Size, Type_Bit_Size (T));
1267 end loop;
1268
1269 for T in Concrete_Entity loop
1270 Min_Entity_Bit_Size :=
1271 Bit_Offset'Min (Min_Entity_Bit_Size, Type_Bit_Size (T));
1272 Max_Entity_Bit_Size :=
1273 Bit_Offset'Max (Max_Entity_Bit_Size, Type_Bit_Size (T));
1274 end loop;
1275
1276 Min_Node_Size := To_Size_In_Slots (Min_Node_Bit_Size);
1277 Max_Node_Size := To_Size_In_Slots (Max_Node_Bit_Size);
1278 Min_Entity_Size := To_Size_In_Slots (Min_Entity_Bit_Size);
1279 Max_Entity_Size := To_Size_In_Slots (Max_Entity_Bit_Size);
1280 end Compute_Type_Sizes;
1281
1282 ----------------------------------------
1283 -- Check_For_Syntactic_Field_Mismatch --
1284 ----------------------------------------
1285
1286 procedure Check_For_Syntactic_Field_Mismatch is
1287 begin
1288 for F in Field_Enum loop
1289 if F /= Between_Node_And_Entity_Fields then
1290 declare
1291 Syntactic_Seen, Semantic_Seen : Boolean := False;
1292 Have_Field : Type_Vector renames
1293 Field_Table (F).Have_This_Field;
1294
1295 begin
1296 for J in 1 .. Last_Index (Have_Field) loop
1297 if Syntactic (Have_Field (J)) (F) then
1298 Syntactic_Seen := True;
1299 else
1300 Semantic_Seen := True;
1301 end if;
1302 end loop;
1303
1304 -- The following fields violate this rule. We might want to
1305 -- simplify by getting rid of these cases, but we allow them
1306 -- for now. At least, we don't want to add any new cases of
1307 -- syntactic/semantic mismatch.
1308
1309 if F in Chars | Actions | Expression | Default_Expression
1310 then
1311 pragma Assert (Syntactic_Seen and Semantic_Seen);
1312
1313 else
1314 if Syntactic_Seen and Semantic_Seen then
1315 raise Illegal with
1316 "syntactic/semantic mismatch for " & Image (F);
1317 end if;
1318
1319 if Field_Table (F).Field_Type in Traversed_Field_Type
1320 and then Syntactic_Seen
1321 then
1322 Setter_Needs_Parent (F) := True;
1323 end if;
1324 end if;
1325 end;
1326 end if;
1327 end loop;
1328 end Check_For_Syntactic_Field_Mismatch;
1329
1330 ----------------------
1331 -- Field_Types_Used --
1332 ----------------------
1333
1334 function Field_Types_Used (First, Last : Field_Enum) return Type_Set is
1335 Result : Type_Set := (others => False);
1336 begin
1337 for F in First .. Last loop
1338 if Field_Table (F).Field_Type in Node_Or_Entity_Type then
1339 Result (Node_Id) := True;
1340
1341 -- Subtypes of Uint all use the same Cast for Uint
1342
1343 elsif Field_Table (F).Field_Type in Uint_Subtype then
1344 Result (Uint) := True;
1345
1346 else
1347 Result (Field_Table (F).Field_Type) := True;
1348 end if;
1349 end loop;
1350
1351 return Result;
1352 end Field_Types_Used;
1353
1354 pragma Style_Checks ("M120");
1355 -- Lines of the form Put (S, "..."); are more readable if we relax the
1356 -- line length. We really just want the "..." to be short enough.
1357
1358 ---------------------------
1359 -- Put_Type_And_Subtypes --
1360 ---------------------------
1361
1362 procedure Put_Type_And_Subtypes
1363 (S : in out Sink; Root : Root_Type)
1364 is
1365
1366 procedure Put_Enum_Type;
1367 -- Print out the enumeration type declaration for a root type
1368 -- (Node_Kind or Entity_Kind).
1369
1370 procedure Put_Kind_Subtype (T : Node_Or_Entity_Type);
1371 -- Print out a subrange (of type Node_Kind or Entity_Kind) for a
1372 -- given nonroot abstract type.
1373
1374 procedure Put_Id_Subtype (T : Node_Or_Entity_Type);
1375 -- Print out a subtype (of type Node_Id or Entity_Id) for a given
1376 -- nonroot abstract type.
1377
1378 procedure Put_Opt_Subtype (T : Node_Or_Entity_Type);
1379 -- Print out an "optional" subtype; that is, one that allows
1380 -- Empty. Their names start with "Opt_".
1381
1382 procedure Put_Enum_Type is
1383 procedure Put_Enum_Lit (T : Node_Or_Entity_Type);
1384 -- Print out one enumeration literal in the declaration of
1385 -- Node_Kind or Entity_Kind.
1386
1387 First_Time : Boolean := True;
1388
1389 procedure Put_Enum_Lit (T : Node_Or_Entity_Type) is
1390 begin
1391 if T in Concrete_Type then
1392 if First_Time then
1393 First_Time := False;
1394 else
1395 Put (S, "," & LF);
1396 end if;
1397
1398 Put (S, Image (T));
1399 end if;
1400 end Put_Enum_Lit;
1401
1402 type Dummy is array
1403 (First_Concrete (Root) .. Last_Concrete (Root)) of Boolean;
1404 Num_Types : constant Root_Int := Dummy'Length;
1405
1406 begin
1407 Put (S, "type " & Image (Root) & " is -- " &
1408 Image (Num_Types) & " " & Image (Root) & "s" & LF);
1409 Increase_Indent (S, 2);
1410 Put (S, "(");
1411 Increase_Indent (S, 1);
1412 Iterate_Types (Root, Pre => Put_Enum_Lit'Access);
1413 Decrease_Indent (S, 1);
1414 Put (S, LF & ") with Size => 8; -- " & Image (Root) & LF & LF);
1415 Decrease_Indent (S, 2);
1416 end Put_Enum_Type;
1417
1418 procedure Put_Kind_Subtype (T : Node_Or_Entity_Type) is
1419 begin
1420 if T in Abstract_Type then
1421 if Type_Table (T).Is_Union then
1422 pragma Assert (Type_Table (T).Parent = Root);
1423
1424 Put (S, "subtype " & Image (T) & " is" & LF);
1425 Increase_Indent (S, 2);
1426 Put (S, Image (Root) & " with Predicate =>" & LF);
1427 Increase_Indent (S, 2);
1428 Put (S, Image (T) & " in" & LF);
1429 Put_Types_With_Bars (S, Type_Table (T).Children);
1430 Decrease_Indent (S, 2);
1431 Put (S, ";" & LF);
1432 Decrease_Indent (S, 2);
1433
1434 elsif Type_Table (T).Parent /= No_Type then
1435 Put (S, "subtype " & Image (T) & " is " &
1436 Image (Type_Table (T).Parent) & " range" & LF);
1437 Increase_Indent (S, 2);
1438 Put (S, Image (Type_Table (T).First) & " .. " &
1439 Image (Type_Table (T).Last) & ";" & LF);
1440 Decrease_Indent (S, 2);
1441
1442 Increase_Indent (S, 3);
1443
1444 for J in 1 .. Type_Table (T).Concrete_Descendants.Last_Index loop
1445 Put (S, "-- " &
1446 Image (Type_Table (T).Concrete_Descendants (J)) & LF);
1447 end loop;
1448
1449 Decrease_Indent (S, 3);
1450 end if;
1451 end if;
1452 end Put_Kind_Subtype;
1453
1454 procedure Put_Id_Subtype (T : Node_Or_Entity_Type) is
1455 begin
1456 if Type_Table (T).Parent /= No_Type then
1457 Put (S, "subtype " & Id_Image (T) & " is" & LF);
1458 Increase_Indent (S, 2);
1459 Put (S, Id_Image (Type_Table (T).Parent));
1460
1461 if Enable_Assertions then
1462 Put (S, " with Predicate =>" & LF);
1463 Increase_Indent (S, 2);
1464 Put (S, "K (" & Id_Image (T) & ") in " & Image (T));
1465 Decrease_Indent (S, 2);
1466 end if;
1467
1468 Put (S, ";" & LF);
1469 Decrease_Indent (S, 2);
1470 end if;
1471 end Put_Id_Subtype;
1472
1473 procedure Put_Opt_Subtype (T : Node_Or_Entity_Type) is
1474 begin
1475 if Type_Table (T).Parent /= No_Type then
1476 Put (S, "subtype Opt_" & Id_Image (T) & " is" & LF);
1477 Increase_Indent (S, 2);
1478 Put (S, Id_Image (Root));
1479
1480 -- Assert that the Opt_XXX subtype is empty or in the XXX
1481 -- subtype.
1482
1483 if Enable_Assertions then
1484 Put (S, " with Predicate =>" & LF);
1485 Increase_Indent (S, 2);
1486 Put (S, "Opt_" & Id_Image (T) & " = Empty or else" & LF);
1487 Put (S, "Opt_" & Id_Image (T) & " in " & Id_Image (T));
1488 Decrease_Indent (S, 2);
1489 end if;
1490
1491 Put (S, ";" & LF);
1492 Decrease_Indent (S, 2);
1493 end if;
1494 end Put_Opt_Subtype;
1495
1496 begin -- Put_Type_And_Subtypes
1497 Put_Enum_Type;
1498
1499 -- Put the getter for Nkind and Ekind here, earlier than the other
1500 -- getters, because it is needed in predicates of the following
1501 -- subtypes.
1502
1503 case Root is
1504 when Node_Kind =>
1505 Put_Getter_Decl (S, Nkind);
1506 Put (S, "function K (N : Node_Id) return Node_Kind renames " & Image (Nkind) & ";" & LF);
1507 Put (S, "-- Shorthand for use in predicates and preconditions below" & LF);
1508 Put (S, "-- There is no procedure Set_Nkind." & LF);
1509 Put (S, "-- See Init_Nkind and Mutate_Nkind in Atree." & LF & LF);
1510
1511 when Entity_Kind =>
1512 Put_Getter_Decl (S, Ekind);
1513 Put (S, "function K (N : Entity_Id) return Entity_Kind renames Ekind;" & LF);
1514 Put (S, "-- Shorthand for use in predicates and preconditions below" & LF);
1515 Put (S, "-- There is no procedure Set_Ekind here." & LF);
1516 Put (S, "-- See Mutate_Ekind in Atree." & LF & LF);
1517
1518 when others => raise Program_Error;
1519 end case;
1520
1521 Put (S, "-- Subtypes of " & Image (Root) & " for each abstract type:" & LF & LF);
1522
1523 Put (S, "pragma Style_Checks (""M200"");" & LF);
1524 Iterate_Types (Root, Pre => Put_Kind_Subtype'Access);
1525
1526 Put (S, LF & "-- Subtypes of " & Id_Image (Root) &
1527 " with specified " & Image (Root) & "." & LF);
1528 Put (S, "-- These may be used in place of " & Id_Image (Root) &
1529 " for better documentation," & LF);
1530 Put (S, "-- and if assertions are enabled, for run-time checking." & LF & LF);
1531
1532 Iterate_Types (Root, Pre => Put_Id_Subtype'Access);
1533
1534 Put (S, LF & "-- Union types (nonhierarchical subtypes of " &
1535 Id_Image (Root) & ")" & LF & LF);
1536
1537 for T in First_Abstract (Root) .. Last_Abstract (Root) loop
1538 if Type_Table (T) /= null and then Type_Table (T).Is_Union then
1539 Put_Kind_Subtype (T);
1540 Put_Id_Subtype (T);
1541 end if;
1542 end loop;
1543
1544 Put (S, LF & "-- Optional subtypes of " & Id_Image (Root) & "." &
1545 " These allow Empty." & LF & LF);
1546
1547 Iterate_Types (Root, Pre => Put_Opt_Subtype'Access);
1548
1549 Put (S, LF & "-- Optional union types:" & LF & LF);
1550
1551 for T in First_Abstract (Root) .. Last_Abstract (Root) loop
1552 if Type_Table (T) /= null and then Type_Table (T).Is_Union then
1553 Put_Opt_Subtype (T);
1554 end if;
1555 end loop;
1556
1557 Put (S, LF & "subtype Flag is Boolean;" & LF & LF);
1558 end Put_Type_And_Subtypes;
1559
1560 -------------------------------------------
1561 -- Put_Casts --
1562 -------------------------------------------
1563
1564 procedure Put_Casts
1565 (S : in out Sink; T : Type_Enum)
1566 is
1567 Pre : constant String :=
1568 "function Cast is new Ada.Unchecked_Conversion (";
1569 Lo_Type : constant String := "Field_Size_" & Image (Field_Size (T)) & "_Bit";
1570 Hi_Type : constant String := Get_Set_Id_Image (T);
1571 begin
1572 if T not in Uint_Subtype then
1573 if T not in Node_Kind_Type | Entity_Kind_Type then
1574 Put (S, Pre & Hi_Type & ", " & Lo_Type & ");" & LF);
1575 end if;
1576
1577 Put (S, Pre & Lo_Type & ", " & Hi_Type & ");" & LF);
1578 end if;
1579 end Put_Casts;
1580
1581 ----------------------
1582 -- Put_Precondition --
1583 ----------------------
1584
1585 procedure Put_Precondition
1586 (S : in out Sink; F : Field_Enum)
1587 is
1588 -- If the field is present in all entities, we want to assert that
1589 -- N in N_Entity_Id. If the field is present in only some entities,
1590 -- we don't need that, because we are fetching Ekind in that case,
1591 -- which will assert N in N_Entity_Id.
1592
1593 Is_Entity : constant String :=
1594 (if Field_Table (F).Have_This_Field = All_Entities then
1595 "N in N_Entity_Id"
1596 else "");
1597 begin
1598 -- If this is an entity field, then we should assert that N is an
1599 -- entity. We need "N in A | B | ..." unless this is embodied in a
1600 -- subtype predicate.
1601 --
1602 -- We can't put the extra "Pre => ..." specified on the call to
1603 -- Create_..._Field as part of the precondition, because some of
1604 -- them call things that are not visible here.
1605
1606 if Enable_Assertions then
1607 if Length (Field_Table (F).Have_This_Field) = 1
1608 or else Field_Table (F).Have_This_Field = Nodes_And_Entities
1609 then
1610 if Is_Entity /= "" then
1611 Increase_Indent (S, 1);
1612 Put (S, ", Pre =>" & LF);
1613 Put (S, Is_Entity);
1614 Decrease_Indent (S, 1);
1615 end if;
1616
1617 else
1618 Put (S, ", Pre =>" & LF);
1619 Increase_Indent (S, 1);
1620 Put (S, "N in ");
1621 Put_Type_Ids_With_Bars (S, Field_Table (F).Have_This_Field);
1622
1623 pragma Assert (Is_Entity = "");
1624
1625 Decrease_Indent (S, 1);
1626 end if;
1627 end if;
1628 end Put_Precondition;
1629
1630 function Root_Type_For_Field (F : Field_Enum) return Root_Type is
1631 (case F is
1632 when Node_Field => Node_Kind,
1633 when Entity_Field => Entity_Kind,
1634 when Between_Node_And_Entity_Fields => Node_Kind); -- can't happen
1635
1636 function N_Type (F : Field_Enum) return String is
1637 (if Length (Field_Table (F).Have_This_Field) = 1 then
1638 Id_Image (Field_Table (F).Have_This_Field (1))
1639 else Id_Image (Root_Type_For_Field (F)));
1640 -- Name of the parameter type of the N parameter of the getter and
1641 -- setter for field F. If there's only one Have_This_Field, use that;
1642 -- the predicate will check for the right Kind. Otherwise, we use
1643 -- Node_Id or Entity_Id, and the getter and setter will have
1644 -- preconditions.
1645
1646 procedure Put_Get_Set_Incr
1647 (S : in out Sink; F : Field_Enum; Get_Or_Set : String)
1648 with Pre => Get_Or_Set in "Get" | "Set";
1649 -- If statistics are enabled, put the appropriate increment statement
1650
1651 ----------------------
1652 -- Put_Get_Set_Incr --
1653 ----------------------
1654
1655 procedure Put_Get_Set_Incr
1656 (S : in out Sink; F : Field_Enum; Get_Or_Set : String) is
1657 begin
1658 if Statistics_Enabled then
1659 Put (S, "Atree." & Get_Or_Set & "_Count (" & F_Image (F) &
1660 ") := Atree." & Get_Or_Set & "_Count (" &
1661 F_Image (F) & ") + 1;" & LF);
1662 end if;
1663 end Put_Get_Set_Incr;
1664
1665 ------------------------
1666 -- Node_To_Fetch_From --
1667 ------------------------
1668
1669 function Node_To_Fetch_From (F : Field_Enum) return String is
1670 begin
1671 return
1672 (case Field_Table (F).Type_Only is
1673 when No_Type_Only => "N",
1674 when Base_Type_Only => "Base_Type (N)",
1675 when Impl_Base_Type_Only => "Implementation_Base_Type (N)",
1676 when Root_Type_Only => "Root_Type (N)");
1677 end Node_To_Fetch_From;
1678
1679 ---------------------
1680 -- Put_Getter_Spec --
1681 ---------------------
1682
1683 procedure Put_Getter_Spec (S : in out Sink; F : Field_Enum) is
1684 begin
1685 Put (S, "function " & Image (F));
1686 Put (S, " (N : " & N_Type (F) & ") return " &
1687 Get_Set_Id_Image (Field_Table (F).Field_Type));
1688 end Put_Getter_Spec;
1689
1690 ---------------------
1691 -- Put_Getter_Decl --
1692 ---------------------
1693
1694 procedure Put_Getter_Decl (S : in out Sink; F : Field_Enum) is
1695 begin
1696 Put_Getter_Spec (S, F);
1697 Put (S, " with " & Inline);
1698 Increase_Indent (S, 2);
1699 Put_Precondition (S, F);
1700 Decrease_Indent (S, 2);
1701 Put (S, ";" & LF);
1702 end Put_Getter_Decl;
1703
1704 ------------------------------
1705 -- Put_Getter_Setter_Locals --
1706 ------------------------------
1707
1708 procedure Put_Getter_Setter_Locals
1709 (S : in out Sink; F : Field_Enum; Get : Boolean)
1710 is
1711 Rec : Field_Info renames Field_Table (F).all;
1712
1713 F_Size : constant Bit_Offset := Field_Size (Rec.Field_Type);
1714 Off : constant Field_Offset := Rec.Offset;
1715 F_Per_Slot : constant Field_Offset :=
1716 SS / Field_Offset (Field_Size (Rec.Field_Type));
1717 Slot_Off : constant Field_Offset := Off / F_Per_Slot;
1718 In_NH : constant Boolean := Slot_Off < Num_Header_Slots;
1719
1720 N : constant String :=
1721 (if Get then Node_To_Fetch_From (F) else "N");
1722
1723 begin
1724 Put (S, " is" & LF);
1725 Increase_Indent (S, 3);
1726 Put (S, "-- " & Image (F_Per_Slot) & " " & Image (F_Size) &
1727 "-bit fields per " & SSS & "-bit slot." & LF);
1728 Put (S, "-- Offset " & Image (Off) & " = " &
1729 Image (Slot_Off) & " slots + " & Image (Off mod F_Per_Slot) &
1730 " fields in slot." & LF & LF);
1731
1732 Put (S, "Off : constant := " & Image (Off) & ";" & LF);
1733 Put (S, "F_Size : constant := " & Image (F_Size) & ";" & LF);
1734
1735 if Field_Size (Rec.Field_Type) /= SS then
1736 Put (S, "Mask : constant := 2**F_Size - 1;" & LF);
1737 end if;
1738
1739 Put (S, "F_Per_Slot : constant Field_Offset := Slot_Size / F_Size;" & LF);
1740 Put (S, "Slot_Off : constant Field_Offset := Off / F_Per_Slot;" & LF);
1741
1742 if In_NH then
1743 Put (S, "S : Slot renames Node_Offsets.Table (" & N & ").Slots (Slot_Off);" & LF);
1744 else
1745 Put (S, "S : Slot renames Slots.Table (Node_Offsets.Table (" & N & ").Offset + Slot_Off);" & LF);
1746 end if;
1747
1748 if Field_Size (Rec.Field_Type) /= SS then
1749 Put (S, "V : constant Natural := Natural ((Off mod F_Per_Slot) * F_Size);" & LF);
1750 Put (S, LF);
1751 end if;
1752 end Put_Getter_Setter_Locals;
1753
1754 ---------------------
1755 -- Put_Getter_Body --
1756 ---------------------
1757
1758 procedure Put_Getter_Body (S : in out Sink; F : Field_Enum) is
1759 Rec : Field_Info renames Field_Table (F).all;
1760 F_Size : constant Bit_Offset := Field_Size (Rec.Field_Type);
1761 T : constant String := Get_Set_Id_Image (Rec.Field_Type);
1762 begin
1763 -- Note that we store the result in a local constant below, so that
1764 -- the "Pre => ..." can refer to it. The constant is called Val so
1765 -- that it has the same name as the formal of the setter, so the
1766 -- "Pre => ..." can refer to it by the same name in both getter
1767 -- and setter.
1768
1769 Put_Getter_Spec (S, F);
1770 Put_Getter_Setter_Locals (S, F, Get => True);
1771
1772 Put (S, "Raw : constant Field_Size_" & Image (F_Size) & "_Bit :=" & LF);
1773 Increase_Indent (S, 2);
1774 Put (S, "Field_Size_" & Image (F_Size) & "_Bit (");
1775
1776 if Field_Size (Rec.Field_Type) /= SS then
1777 Put (S, "Shift_Right (S, V) and Mask);" & LF);
1778 else
1779 Put (S, "S);" & LF);
1780 end if;
1781
1782 Decrease_Indent (S, 2);
1783
1784 Put (S, "Val : constant " & T & " :=");
1785
1786 if Field_Has_Special_Default (Rec.Field_Type) then
1787 pragma Assert (Field_Size (Rec.Field_Type) = 32);
1788 Put (S, LF);
1789 Increase_Indent (S, 2);
1790 Put (S, "(if Raw = 0 then " & Special_Default (Rec.Field_Type) &
1791 " else " & "Cast (Raw));");
1792 Decrease_Indent (S, 2);
1793
1794 else
1795 Put (S, " Cast (Raw);");
1796 end if;
1797
1798 Put (S, LF);
1799
1800 Decrease_Indent (S, 3);
1801 Put (S, "begin" & LF);
1802 Increase_Indent (S, 3);
1803
1804 Put (S, "-- pragma Debug (Validate_Node_And_Offset (NN, Slot_Off));" & LF);
1805 -- Comment out the validation, because it's too slow, and because the
1806 -- relevant routines in Atree are not visible.
1807
1808 if Rec.Pre.all /= "" then
1809 Put (S, "pragma Assert (" & Rec.Pre.all & ");" & LF);
1810 end if;
1811
1812 if Rec.Pre_Get.all /= "" then
1813 Put (S, "pragma Assert (" & Rec.Pre_Get.all & ");" & LF);
1814 end if;
1815
1816 Put_Get_Set_Incr (S, F, "Get");
1817 Put (S, "return Val;" & LF);
1818 Decrease_Indent (S, 3);
1819 Put (S, "end " & Image (F) & ";" & LF & LF);
1820 end Put_Getter_Body;
1821
1822 ---------------------
1823 -- Put_Setter_Spec --
1824 ---------------------
1825
1826 procedure Put_Setter_Spec (S : in out Sink; F : Field_Enum) is
1827 Rec : Field_Info renames Field_Table (F).all;
1828 Default : constant String :=
1829 (if Rec.Field_Type = Flag then " := True" else "");
1830 begin
1831 Put (S, "procedure Set_" & Image (F));
1832 Put (S, " (N : " & N_Type (F) & "; Val : " &
1833 Get_Set_Id_Image (Rec.Field_Type) & Default & ")");
1834 end Put_Setter_Spec;
1835
1836 ---------------------
1837 -- Put_Setter_Decl --
1838 ---------------------
1839
1840 procedure Put_Setter_Decl (S : in out Sink; F : Field_Enum) is
1841 begin
1842 Put_Setter_Spec (S, F);
1843 Put (S, " with " & Inline);
1844 Increase_Indent (S, 2);
1845 Put_Precondition (S, F);
1846 Decrease_Indent (S, 2);
1847 Put (S, ";" & LF);
1848 end Put_Setter_Decl;
1849
1850 ---------------------
1851 -- Put_Setter_Body --
1852 ---------------------
1853
1854 procedure Put_Setter_Body (S : in out Sink; F : Field_Enum) is
1855 Rec : Field_Info renames Field_Table (F).all;
1856 F_Size : constant Bit_Offset := Field_Size (Rec.Field_Type);
1857
1858 -- If Type_Only was specified in the call to Create_Semantic_Field,
1859 -- then we assert that the node is a base type. We cannot assert that
1860 -- it is an implementation base type or a root type.
1861
1862 Type_Only_Assertion : constant String :=
1863 (case Rec.Type_Only is
1864 when No_Type_Only => "",
1865 when Base_Type_Only | Impl_Base_Type_Only | Root_Type_Only =>
1866 "Is_Base_Type (N)");
1867 begin
1868 Put_Setter_Spec (S, F);
1869 Put_Getter_Setter_Locals (S, F, Get => False);
1870
1871 Put (S, "Raw : constant Field_Size_" & Image (F_Size) & "_Bit := Cast (Val);" & LF);
1872
1873 Decrease_Indent (S, 3);
1874 Put (S, "begin" & LF);
1875 Increase_Indent (S, 3);
1876
1877 Put (S, "-- pragma Debug (Validate_Node_And_Offset_Write (N, Slot_Off));" & LF);
1878 -- Comment out the validation, because it's too slow, and because the
1879 -- relevant routines in Atree are not visible.
1880
1881 if Rec.Pre.all /= "" then
1882 Put (S, "pragma Assert (" & Rec.Pre.all & ");" & LF);
1883 end if;
1884
1885 if Rec.Pre_Set.all /= "" then
1886 Put (S, "pragma Assert (" & Rec.Pre_Set.all & ");" & LF);
1887 end if;
1888
1889 if Type_Only_Assertion /= "" then
1890 Put (S, "pragma Assert (" & Type_Only_Assertion & ");" & LF);
1891 end if;
1892
1893 if Setter_Needs_Parent (F) then
1894 declare
1895 Err : constant String :=
1896 (if Rec.Field_Type = List_Id then "Error_List" else "Error");
1897 begin
1898 Put (S, "if Present (Val) and then Val /= " & Err & " then" & LF);
1899 Increase_Indent (S, 3);
1900 Put (S, "pragma Warnings (Off, ""actuals for this call may be in wrong order"");" & LF);
1901 Put (S, "Set_Parent (Val, N);" & LF);
1902 Put (S, "pragma Warnings (On, ""actuals for this call may be in wrong order"");" & LF);
1903 Decrease_Indent (S, 3);
1904 Put (S, "end if;" & LF & LF);
1905 end;
1906 end if;
1907
1908 if Field_Size (Rec.Field_Type) /= SS then
1909 Put (S, "S := (S and not Shift_Left (Mask, V)) or Shift_Left (Slot (Raw), V);" & LF);
1910
1911 else
1912 Put (S, "S := Slot (Raw);" & LF);
1913 end if;
1914
1915 Put_Get_Set_Incr (S, F, "Set");
1916
1917 Decrease_Indent (S, 3);
1918 Put (S, "end Set_" & Image (F) & ";" & LF & LF);
1919 end Put_Setter_Body;
1920
1921 --------------------
1922 -- Put_Subp_Decls --
1923 --------------------
1924
1925 procedure Put_Subp_Decls (S : in out Sink; Root : Root_Type) is
1926 -- Note that there are several fields that are defined for both nodes
1927 -- and entities, such as Nkind. These are allocated slots in both,
1928 -- but here we only put out getters and setters in Sinfo.Nodes, not
1929 -- Einfo.Entities.
1930
1931 begin
1932 Put (S, "-- Getters and setters for fields" & LF);
1933
1934 for F in First_Field (Root) .. Last_Field (Root) loop
1935 -- Nkind/Ekind getter is already done (see Put_Type_And_Subtypes),
1936 -- and there is no setter for these.
1937
1938 if F = Nkind then
1939 Put (S, LF & "-- Nkind getter is above" & LF);
1940
1941 elsif F = Ekind then
1942 Put (S, LF & "-- Ekind getter is above" & LF);
1943
1944 else
1945 Put_Getter_Decl (S, F);
1946 Put_Setter_Decl (S, F);
1947 end if;
1948
1949 Put (S, LF);
1950 end loop;
1951 end Put_Subp_Decls;
1952
1953 ---------------------
1954 -- Put_Subp_Bodies --
1955 ---------------------
1956
1957 procedure Put_Subp_Bodies (S : in out Sink; Root : Root_Type) is
1958 begin
1959 Put (S, LF & "-- Getters and setters for fields" & LF & LF);
1960
1961 for F in First_Field (Root) .. Last_Field (Root) loop
1962 Put_Getter_Body (S, F);
1963
1964 if F not in Nkind | Ekind then
1965 Put_Setter_Body (S, F);
1966 end if;
1967 end loop;
1968 end Put_Subp_Bodies;
1969
1970 --------------------------
1971 -- Put_Traversed_Fields --
1972 --------------------------
1973
1974 procedure Put_Traversed_Fields (S : in out Sink) is
1975
1976 function Is_Traversed_Field
1977 (T : Concrete_Node; F : Field_Enum) return Boolean;
1978 -- True if F is a field that should be traversed by Traverse_Func. In
1979 -- particular, True if F is a syntactic field of T, and is of a
1980 -- Node_Id or List_Id type.
1981
1982 function Init_Max_Traversed_Fields return Field_Offset;
1983 -- Compute the maximum number of syntactic fields that are of type
1984 -- Node_Id or List_Id over all node types.
1985
1986 procedure Put_Aggregate (T : Node_Or_Entity_Type);
1987 -- Print out the subaggregate for one type
1988
1989 function Is_Traversed_Field
1990 (T : Concrete_Node; F : Field_Enum) return Boolean is
1991 begin
1992 return Syntactic (T) (F)
1993 and then Field_Table (F).Field_Type in Traversed_Field_Type;
1994 end Is_Traversed_Field;
1995
1996 First_Time : Boolean := True;
1997
1998 procedure Put_Aggregate (T : Node_Or_Entity_Type) is
1999 Left_Opnd_Skipped : Boolean := False;
2000 begin
2001 if T in Concrete_Node then
2002 if First_Time then
2003 First_Time := False;
2004 else
2005 Put (S, "," & LF);
2006 end if;
2007
2008 Put (S, Image (T) & " => (");
2009 Increase_Indent (S, 2);
2010
2011 for FI in 1 .. Last_Index (Type_Table (T).Fields) loop
2012 declare
2013 F : constant Field_Enum := Type_Table (T).Fields (FI);
2014
2015 begin
2016 if Is_Traversed_Field (T, F) then
2017 if F = Left_Opnd then
2018 Left_Opnd_Skipped := True; -- see comment below
2019
2020 else
2021 Put (S, Image (Field_Table (F).Offset) & ", ");
2022 end if;
2023 end if;
2024 end;
2025 end loop;
2026
2027 -- We always put the Left_Opnd field of N_Op_Concat last. See
2028 -- comments in Atree.Traverse_Func for the reason. We might as
2029 -- well do that for all Left_Opnd fields; the old version did
2030 -- that.
2031
2032 if Left_Opnd_Skipped then
2033 Put (S, Image (Field_Table (Left_Opnd).Offset) & ", ");
2034 end if;
2035
2036 Put (S, "others => No_Field_Offset");
2037
2038 Decrease_Indent (S, 2);
2039 Put (S, ")");
2040 end if;
2041 end Put_Aggregate;
2042
2043 function Init_Max_Traversed_Fields return Field_Offset is
2044 Result : Field_Offset := 0;
2045 begin
2046 for T in Concrete_Node loop
2047 declare
2048 Num_Traversed_Fields : Field_Offset := 0; -- in type T
2049
2050 begin
2051 for FI in 1 .. Last_Index (Type_Table (T).Fields) loop
2052 declare
2053 F : constant Field_Enum := Type_Table (T).Fields (FI);
2054
2055 begin
2056 if Is_Traversed_Field (T, F) then
2057 Num_Traversed_Fields := Num_Traversed_Fields + 1;
2058 end if;
2059 end;
2060 end loop;
2061
2062 if Num_Traversed_Fields > Result then
2063 Result := Num_Traversed_Fields;
2064 end if;
2065 end;
2066 end loop;
2067
2068 return Result;
2069 end Init_Max_Traversed_Fields;
2070
2071 Max_Traversed_Fields : constant Field_Offset :=
2072 Init_Max_Traversed_Fields;
2073
2074 begin
2075 Put (S, "-- Table of fields that should be traversed by Traverse subprograms." & LF);
2076 Put (S, "-- Each entry is an array of offsets in slots of fields to be" & LF);
2077 Put (S, "-- traversed, terminated by a sentinel equal to No_Field_Offset." & LF & LF);
2078
2079 Put (S, "subtype Traversed_Offset_Array is Offset_Array (0 .. " &
2080 Image (Max_Traversed_Fields - 1) & " + 1);" & LF);
2081 Put (S, "Traversed_Fields : constant array (Node_Kind) of Traversed_Offset_Array :=" & LF);
2082 -- One extra for the sentinel
2083
2084 Increase_Indent (S, 2);
2085 Put (S, "(");
2086 Increase_Indent (S, 1);
2087 Iterate_Types (Node_Kind, Pre => Put_Aggregate'Access);
2088 Decrease_Indent (S, 1);
2089 Put (S, ");" & LF & LF);
2090 Decrease_Indent (S, 2);
2091 end Put_Traversed_Fields;
2092
2093 ----------------
2094 -- Put_Tables --
2095 ----------------
2096
2097 procedure Put_Tables (S : in out Sink; Root : Root_Type) is
2098
2099 First_Time : Boolean := True;
2100
2101 procedure Put_Size (T : Node_Or_Entity_Type);
2102 procedure Put_Size (T : Node_Or_Entity_Type) is
2103 begin
2104 if T in Concrete_Type then
2105 if First_Time then
2106 First_Time := False;
2107 else
2108 Put (S, "," & LF);
2109 end if;
2110
2111 Put (S, Image (T) & " => " & Image (Type_Size_In_Slots (T)));
2112 end if;
2113 end Put_Size;
2114
2115 procedure Put_Field_Array (T : Concrete_Type);
2116
2117 procedure Put_Field_Array (T : Concrete_Type) is
2118 First_Time : Boolean := True;
2119 begin
2120 for F in First_Field (Root) .. Last_Field (Root) loop
2121 if Fields_Per_Node (T) (F) then
2122 if First_Time then
2123 First_Time := False;
2124 else
2125 Put (S, "," & LF);
2126 end if;
2127
2128 Put (S, F_Image (F));
2129 end if;
2130 end loop;
2131 end Put_Field_Array;
2132
2133 Field_Enum_Type_Name : constant String :=
2134 (case Root is
2135 when Node_Kind => "Node_Field",
2136 when others => "Entity_Field"); -- Entity_Kind
2137
2138 begin
2139 Put (S, "-- Table of sizes in " & SSS & "-bit slots for given " &
2140 Image (Root) & ", for use by Atree:" & LF);
2141
2142 case Root is
2143 when Node_Kind =>
2144 Put (S, LF & "Min_Node_Size : constant Field_Offset := " &
2145 Image (Min_Node_Size) & ";" & LF);
2146 Put (S, "Max_Node_Size : constant Field_Offset := " &
2147 Image (Max_Node_Size) & ";" & LF & LF);
2148
2149 when Entity_Kind =>
2150 Put (S, LF & "Min_Entity_Size : constant Field_Offset := " &
2151 Image (Min_Entity_Size) & ";" & LF);
2152 Put (S, "Max_Entity_Size : constant Field_Offset := " &
2153 Image (Max_Entity_Size) & ";" & LF & LF);
2154 when others => raise Program_Error;
2155 end case;
2156
2157 Put (S, "Size : constant array (" & Image (Root) &
2158 ") of Field_Offset :=" & LF);
2159 Increase_Indent (S, 2);
2160 Put (S, "(");
2161 Increase_Indent (S, 1);
2162
2163 Iterate_Types (Root, Pre => Put_Size'Access);
2164
2165 Decrease_Indent (S, 1);
2166 Put (S, "); -- Size" & LF);
2167 Decrease_Indent (S, 2);
2168
2169 if Root = Node_Kind then
2170 declare
2171 type Node_Dummy is array (Node_Field) of Boolean;
2172 type Entity_Dummy is array (Entity_Field) of Boolean;
2173 Num_Fields : constant Root_Int :=
2174 Node_Dummy'Length + Entity_Dummy'Length;
2175 First_Time : Boolean := True;
2176 begin
2177 Put (S, LF & "-- Enumeration of all " & Image (Num_Fields)
2178 & " fields:" & LF & LF);
2179
2180 Put (S, "type Node_Or_Entity_Field is" & LF);
2181 Increase_Indent (S, 2);
2182 Put (S, "(");
2183 Increase_Indent (S, 1);
2184
2185 for F in Node_Field loop
2186 if First_Time then
2187 First_Time := False;
2188 else
2189 Put (S, "," & LF);
2190 end if;
2191
2192 Put (S, F_Image (F));
2193 end loop;
2194
2195 for F in Entity_Field loop
2196 Put (S, "," & LF);
2197 Put (S, F_Image (F));
2198 end loop;
2199
2200 Decrease_Indent (S, 1);
2201 Put (S, "); -- Node_Or_Entity_Field" & LF);
2202 Decrease_Indent (S, 2);
2203 end;
2204 end if;
2205
2206 Put (S, LF & "subtype " & Field_Enum_Type_Name & " is" & LF);
2207 Increase_Indent (S, 2);
2208 Put (S, "Node_Or_Entity_Field range " & F_Image (First_Field (Root)) &
2209 " .. " & F_Image (Last_Field (Root)) & ";" & LF);
2210 Decrease_Indent (S, 2);
2211
2212 Put (S, LF & "type " & Field_Enum_Type_Name & "_Index is new Pos;" & LF);
2213 Put (S, "type " & Field_Enum_Type_Name & "_Array is array (" &
2214 Field_Enum_Type_Name & "_Index range <>) of " &
2215 Field_Enum_Type_Name & ";" & LF);
2216 Put (S, "type " & Field_Enum_Type_Name &
2217 "_Array_Ref is not null access constant " & Field_Enum_Type_Name &
2218 "_Array;" & LF);
2219 Put (S, "subtype A is " & Field_Enum_Type_Name & "_Array;" & LF);
2220 -- Short name to make allocators below more readable
2221
2222 declare
2223 First_Time : Boolean := True;
2224
2225 procedure Do_One_Type (T : Node_Or_Entity_Type);
2226 procedure Do_One_Type (T : Node_Or_Entity_Type) is
2227 begin
2228 if T in Concrete_Type then
2229 if First_Time then
2230 First_Time := False;
2231 else
2232 Put (S, "," & LF);
2233 end if;
2234
2235 Put (S, Image (T) & " =>" & LF);
2236 Increase_Indent (S, 2);
2237 Put (S, "new A'(");
2238 Increase_Indent (S, 6);
2239 Increase_Indent (S, 1);
2240
2241 Put_Field_Array (T);
2242
2243 Decrease_Indent (S, 1);
2244 Put (S, ")");
2245 Decrease_Indent (S, 6);
2246 Decrease_Indent (S, 2);
2247 end if;
2248 end Do_One_Type;
2249 begin
2250 Put (S, LF & "-- Table mapping " & Image (Root) &
2251 "s to the sequence of fields that exist in that " &
2252 Image (Root) & ":" & LF & LF);
2253
2254 Put (S, Field_Enum_Type_Name & "_Table : constant array (" &
2255 Image (Root) & ") of " & Field_Enum_Type_Name &
2256 "_Array_Ref :=" & LF);
2257
2258 Increase_Indent (S, 2);
2259 Put (S, "(");
2260 Increase_Indent (S, 1);
2261
2262 Iterate_Types (Root, Pre => Do_One_Type'Access);
2263
2264 Decrease_Indent (S, 1);
2265 Put (S, "); -- " & Field_Enum_Type_Name & "_Table" & LF);
2266 Decrease_Indent (S, 2);
2267 end;
2268
2269 if Root = Node_Kind then
2270 declare
2271 First_Time : Boolean := True;
2272 FS, FB, LB : Bit_Offset;
2273 -- Field size in bits, first bit, and last bit for the previous
2274 -- time around the loop. Used to print a comment after ",".
2275
2276 procedure One_Comp (F : Field_Enum);
2277
2278 --------------
2279 -- One_Comp --
2280 --------------
2281
2282 procedure One_Comp (F : Field_Enum) is
2283 pragma Annotate (Codepeer, Modified, Field_Table);
2284 Offset : constant Field_Offset := Field_Table (F).Offset;
2285 begin
2286 if First_Time then
2287 First_Time := False;
2288 else
2289 Put (S, ",");
2290
2291 -- Print comment showing field's bits, except for 1-bit
2292 -- fields.
2293
2294 if FS /= 1 then
2295 Put (S, " -- *" & Image (FS) & " = bits " &
2296 Image (FB) & ".." & Image (LB));
2297 end if;
2298
2299 Put (S, LF);
2300 end if;
2301
2302 Put (S, F_Image (F) & " => (" &
2303 Image (Field_Table (F).Field_Type) & "_Field, " &
2304 Image (Offset) & ", " &
2305 Image (Field_Table (F).Type_Only) & ")");
2306
2307 FS := Field_Size (F);
2308 FB := First_Bit (F, Offset);
2309 LB := Last_Bit (F, Offset);
2310 end One_Comp;
2311
2312 begin
2313 Put (S, LF & "-- Table mapping fields to kind and offset:" & LF & LF);
2314
2315 Put (S, "Field_Descriptors : constant array (" &
2316 "Node_Or_Entity_Field) of Field_Descriptor :=" & LF);
2317
2318 Increase_Indent (S, 2);
2319 Put (S, "(");
2320 Increase_Indent (S, 1);
2321
2322 for F in Node_Field loop
2323 One_Comp (F);
2324 end loop;
2325
2326 for F in Entity_Field loop
2327 One_Comp (F);
2328 end loop;
2329
2330 Decrease_Indent (S, 1);
2331 Put (S, "); -- Field_Descriptors" & LF);
2332 Decrease_Indent (S, 2);
2333 end;
2334 end if;
2335
2336 end Put_Tables;
2337
2338 ----------------
2339 -- Put_Seinfo --
2340 ----------------
2341
2342 procedure Put_Seinfo is
2343 S : Sink;
2344 begin
2345 Create_File (S, "seinfo.ads");
2346 Put (S, "with Types; use Types;" & LF);
2347 Put (S, LF & "package Seinfo is" & LF & LF);
2348 Increase_Indent (S, 3);
2349
2350 Put (S, "-- This package is automatically generated." & LF & LF);
2351
2352 Put (S, "-- Common declarations visible in both Sinfo.Nodes and Einfo.Entities." & LF);
2353
2354 Put (S, LF & "type Field_Kind is" & LF);
2355 Increase_Indent (S, 2);
2356 Put (S, "(");
2357 Increase_Indent (S, 1);
2358
2359 declare
2360 First_Time : Boolean := True;
2361 begin
2362 for T in Special_Type loop
2363 if First_Time then
2364 First_Time := False;
2365 else
2366 Put (S, "," & LF);
2367 end if;
2368
2369 Put (S, Image (T) & "_Field");
2370 end loop;
2371 end;
2372
2373 Decrease_Indent (S, 1);
2374 Decrease_Indent (S, 2);
2375 Put (S, ");" & LF);
2376
2377 Put (S, LF & "Field_Size : constant array (Field_Kind) of Field_Size_In_Bits :=" & LF);
2378 Increase_Indent (S, 2);
2379 Put (S, "(");
2380 Increase_Indent (S, 1);
2381
2382 declare
2383 First_Time : Boolean := True;
2384 begin
2385 for T in Special_Type loop
2386 if First_Time then
2387 First_Time := False;
2388 else
2389 Put (S, "," & LF);
2390 end if;
2391
2392 Put (S, Image (T) & "_Field => " & Image (Field_Size (T)));
2393 end loop;
2394 end;
2395
2396 Decrease_Indent (S, 1);
2397 Decrease_Indent (S, 2);
2398 Put (S, ");" & LF & LF);
2399
2400 Put (S, "type Type_Only_Enum is" & LF);
2401 Increase_Indent (S, 2);
2402 Put (S, "(");
2403
2404 declare
2405 First_Time : Boolean := True;
2406 begin
2407 for TO in Type_Only_Enum loop
2408 if First_Time then
2409 First_Time := False;
2410 else
2411 Put (S, ", ");
2412 end if;
2413
2414 Put (S, Image (TO));
2415 end loop;
2416 end;
2417
2418 Decrease_Indent (S, 2);
2419 Put (S, ");" & LF & LF);
2420
2421 Put (S, "type Field_Descriptor is record" & LF);
2422 Increase_Indent (S, 3);
2423 Put (S, "Kind : Field_Kind;" & LF);
2424 Put (S, "Offset : Field_Offset;" & LF);
2425 Put (S, "Type_Only : Type_Only_Enum;" & LF);
2426 Decrease_Indent (S, 3);
2427 Put (S, "end record;" & LF & LF);
2428
2429 -- Print out the node header types. Note that the Offset field is of
2430 -- the base type, because we are using zero-origin addressing in
2431 -- Atree.
2432
2433 Put (S, "N_Head : constant Field_Offset := " & N_Head & ";" & LF & LF);
2434
2435 Put (S, "Atree_Statistics_Enabled : constant Boolean := " &
2436 Capitalize (Boolean'Image (Statistics_Enabled)) & ";" & LF);
2437
2438 Decrease_Indent (S, 3);
2439 Put (S, LF & "end Seinfo;" & LF);
2440 end Put_Seinfo;
2441
2442 ---------------
2443 -- Put_Nodes --
2444 ---------------
2445
2446 procedure Put_Nodes is
2447 S : Sink;
2448 B : Sink;
2449
2450 begin
2451 Create_File (S, "sinfo-nodes.ads");
2452 Create_File (B, "sinfo-nodes.adb");
2453 Put (S, "with Seinfo; use Seinfo;" & LF);
2454 Put (S, "pragma Warnings (Off);" & LF);
2455 -- With's included in case they are needed; so we don't have to keep
2456 -- switching back and forth.
2457 Put (S, "with Output; use Output;" & LF);
2458 Put (S, "pragma Warnings (On);" & LF);
2459
2460 Put (S, LF & "package Sinfo.Nodes is" & LF & LF);
2461 Increase_Indent (S, 3);
2462
2463 Put (S, "-- This package is automatically generated." & LF & LF);
2464
2465 Put_Type_Hierarchy (S, Node_Kind);
2466
2467 Put_Type_And_Subtypes (S, Node_Kind);
2468
2469 Put (S, "pragma Assert (Node_Kind'Pos (N_Unused_At_Start) = 0);" & LF & LF);
2470 Put (S, "pragma Assert (Node_Kind'Last = N_Unused_At_End);" & LF & LF);
2471
2472 Put_Subp_Decls (S, Node_Kind);
2473
2474 Put_Traversed_Fields (S);
2475
2476 Put_Tables (S, Node_Kind);
2477
2478 Decrease_Indent (S, 3);
2479 Put (S, LF & "end Sinfo.Nodes;" & LF);
2480
2481 Put (B, "with Ada.Unchecked_Conversion;" & LF);
2482 Put (B, "with Atree; use Atree; use Atree.Atree_Private_Part;" & LF);
2483 Put (B, "with Nlists; use Nlists;" & LF);
2484 Put (B, "pragma Warnings (Off);" & LF);
2485 Put (B, "with Einfo.Utils; use Einfo.Utils;" & LF);
2486 Put (B, "with Sinfo.Utils; use Sinfo.Utils;" & LF);
2487 Put (B, "pragma Warnings (On);" & LF);
2488
2489 Put (B, LF & "package body Sinfo.Nodes is" & LF & LF);
2490 Increase_Indent (B, 3);
2491
2492 Put (B, "-- This package is automatically generated." & LF & LF);
2493
2494 Put (B, "pragma Style_Checks (""M200"");" & LF);
2495
2496 for T in Special_Type loop
2497 if Node_Field_Types_Used (T) then
2498 Put_Casts (B, T);
2499 end if;
2500 end loop;
2501
2502 Put_Subp_Bodies (B, Node_Kind);
2503
2504 Decrease_Indent (B, 3);
2505 Put (B, "end Sinfo.Nodes;" & LF);
2506
2507 end Put_Nodes;
2508
2509 ------------------
2510 -- Put_Entities --
2511 ------------------
2512
2513 procedure Put_Entities is
2514 S : Sink;
2515 B : Sink;
2516 begin
2517 Create_File (S, "einfo-entities.ads");
2518 Create_File (B, "einfo-entities.adb");
2519 Put (S, "with Sinfo.Nodes; use Sinfo.Nodes;" & LF);
2520
2521 Put (S, LF & "package Einfo.Entities is" & LF & LF);
2522 Increase_Indent (S, 3);
2523
2524 Put (S, "-- This package is automatically generated." & LF & LF);
2525
2526 Put_Type_Hierarchy (S, Entity_Kind);
2527
2528 Put_Type_And_Subtypes (S, Entity_Kind);
2529
2530 Put_Subp_Decls (S, Entity_Kind);
2531
2532 Put_Tables (S, Entity_Kind);
2533
2534 Decrease_Indent (S, 3);
2535 Put (S, LF & "end Einfo.Entities;" & LF);
2536
2537 Put (B, "with Ada.Unchecked_Conversion;" & LF);
2538 Put (B, "with Atree; use Atree; use Atree.Atree_Private_Part;" & LF);
2539 Put (B, "with Einfo.Utils; use Einfo.Utils;" & LF);
2540 -- This forms a cycle between packages (via bodies, which is OK)
2541
2542 Put (B, LF & "package body Einfo.Entities is" & LF & LF);
2543 Increase_Indent (B, 3);
2544
2545 Put (B, "-- This package is automatically generated." & LF & LF);
2546
2547 Put (B, "pragma Style_Checks (""M200"");" & LF);
2548
2549 for T in Special_Type loop
2550 if Entity_Field_Types_Used (T) then
2551 Put_Casts (B, T);
2552 end if;
2553 end loop;
2554
2555 Put_Subp_Bodies (B, Entity_Kind);
2556
2557 Decrease_Indent (B, 3);
2558 Put (B, "end Einfo.Entities;" & LF);
2559
2560 end Put_Entities;
2561
2562 -------------------
2563 -- Put_Make_Spec --
2564 -------------------
2565
2566 procedure Put_Make_Spec
2567 (S : in out Sink; Root : Root_Type; T : Concrete_Type)
2568 is
2569 begin
2570 Put (S, "function Make_" & Image_Sans_N (T) & "" & LF);
2571 Increase_Indent (S, 2);
2572 Put (S, "(Sloc : Source_Ptr");
2573 Increase_Indent (S, 1);
2574
2575 for F of Type_Table (T).Fields loop
2576 pragma Assert (Fields_Per_Node (T) (F));
2577
2578 if Syntactic (T) (F) then
2579 declare
2580 Typ : constant String :=
2581 (if Field_Table (F).Field_Type = Flag then "Boolean"
2582 else Image (Field_Table (F).Field_Type));
2583
2584 -- All Flag fields have a default, which is False by
2585 -- default.
2586
2587 Default : constant String :=
2588 (if Field_Table (F).Default_Value = No_Default then
2589 (if Field_Table (F).Field_Type = Flag then " := False" else "")
2590 else " := " & Value_Image (Field_Table (F).Default_Value));
2591
2592 begin
2593 Put (S, ";" & LF);
2594 Put (S, Image (F));
2595 Put (S, " : " & Typ & Default);
2596 end;
2597 end if;
2598 end loop;
2599
2600 Put (S, ")" & LF);
2601 Put (S, "return " & Node_Or_Entity (Root) & "_Id");
2602 Decrease_Indent (S, 2);
2603 Decrease_Indent (S, 1);
2604 end Put_Make_Spec;
2605
2606 --------------------
2607 -- Put_Make_Decls --
2608 --------------------
2609
2610 procedure Put_Make_Decls (S : in out Sink; Root : Root_Type) is
2611 begin
2612 for T in First_Concrete (Root) .. Last_Concrete (Root) loop
2613 if T not in N_Unused_At_Start | N_Unused_At_End then
2614 Put_Make_Spec (S, Root, T);
2615 Put (S, ";" & LF);
2616 Put (S, "pragma " & Inline & " (Make_" &
2617 Image_Sans_N (T) & ");" & LF & LF);
2618 end if;
2619 end loop;
2620 end Put_Make_Decls;
2621
2622 ---------------------
2623 -- Put_Make_Bodies --
2624 ---------------------
2625
2626 procedure Put_Make_Bodies (S : in out Sink; Root : Root_Type) is
2627 begin
2628 for T in First_Concrete (Root) .. Last_Concrete (Root) loop
2629 if T not in N_Unused_At_Start | N_Unused_At_End then
2630 Put_Make_Spec (S, Root, T);
2631 Put (S, LF & "is" & LF);
2632
2633 Increase_Indent (S, 3);
2634 Put (S, "N : constant Node_Id :=" & LF);
2635
2636 if T in Entity_Node then
2637 Put (S, " New_Entity (" & Image (T) & ", Sloc);" & LF);
2638
2639 else
2640 Put (S, " New_Node (" & Image (T) & ", Sloc);" & LF);
2641 end if;
2642
2643 Decrease_Indent (S, 3);
2644
2645 Put (S, "begin" & LF);
2646
2647 Increase_Indent (S, 3);
2648 for F of Type_Table (T).Fields loop
2649 pragma Assert (Fields_Per_Node (T) (F));
2650
2651 if Syntactic (T) (F) then
2652 declare
2653 NWidth : constant := 28;
2654 -- This constant comes from the old Xnmake, which wraps
2655 -- the Set_... call if the field name is that long or
2656 -- longer.
2657
2658 F_Name : constant String := Image (F);
2659
2660 begin
2661 if F_Name'Length < NWidth then
2662 Put (S, "Set_" & F_Name & " (N, " & F_Name & ");" & LF);
2663
2664 -- Wrap the line
2665
2666 else
2667 Put (S, "Set_" & F_Name & "" & LF);
2668 Increase_Indent (S, 2);
2669 Put (S, "(N, " & F_Name & ");" & LF);
2670 Decrease_Indent (S, 2);
2671 end if;
2672 end;
2673 end if;
2674 end loop;
2675
2676 if Is_Descendant (N_Op, T) then
2677 -- Special cases for N_Op nodes: fill in the Chars and Entity
2678 -- fields even though they were not passed in.
2679
2680 declare
2681 Op : constant String := Image_Sans_N (T);
2682 -- This will be something like "Op_And" or "Op_Add"
2683
2684 Op_Name_With_Op : constant String :=
2685 (if T = N_Op_Plus then "Op_Add"
2686 elsif T = N_Op_Minus then "Op_Subtract"
2687 else Op);
2688 -- Special cases for unary operators that have the same name
2689 -- as a binary operator; we use the binary operator name in
2690 -- that case.
2691
2692 Slid : constant String (1 .. Op_Name_With_Op'Length) :=
2693 Op_Name_With_Op;
2694 pragma Assert (Slid (1 .. 3) = "Op_");
2695
2696 Op_Name : constant String :=
2697 (if T in N_Op_Rotate_Left |
2698 N_Op_Rotate_Right |
2699 N_Op_Shift_Left |
2700 N_Op_Shift_Right |
2701 N_Op_Shift_Right_Arithmetic
2702 then Slid (4 .. Slid'Last)
2703 else Slid);
2704 -- Special cases for shifts and rotates; the node kind has
2705 -- "Op_", but the Name_Id constant does not.
2706
2707 begin
2708 Put (S, "Set_Chars (N, Name_" & Op_Name & ");" & LF);
2709 Put (S, "Set_Entity (N, Standard_" & Op & ");" & LF);
2710 end;
2711 end if;
2712
2713 if Type_Table (T).Nmake_Assert.all /= "" then
2714 Put (S, "pragma Assert (" &
2715 Type_Table (T).Nmake_Assert.all & ");" & LF);
2716 end if;
2717
2718 Put (S, "return N;" & LF);
2719 Decrease_Indent (S, 3);
2720
2721 Put (S, "end Make_" & Image_Sans_N (T) & ";" & LF & LF);
2722 end if;
2723 end loop;
2724 end Put_Make_Bodies;
2725
2726 ---------------
2727 -- Put_Nmake --
2728 ---------------
2729
2730 -- Documentation for the Nmake package, generated by Put_Nmake below.
2731
2732 -- The Nmake package contains a set of routines used to construct tree
2733 -- nodes using a functional style. There is one routine for each node
2734 -- type defined in Gen_IL.Gen.Gen_Nodes with the general interface:
2735
2736 -- function Make_xxx (Sloc : Source_Ptr,
2737 -- Field_Name_1 : Field_Name_1_Type [:= default]
2738 -- Field_Name_2 : Field_Name_2_Type [:= default]
2739 -- ...)
2740 -- return Node_Id
2741
2742 -- Only syntactic fields are included.
2743
2744 -- Default values are provided as specified in Gen_Nodes, except that if
2745 -- no default is specified for a flag field, it has a default of False.
2746
2747 -- Warning: since calls to Make_xxx routines are normal function calls, the
2748 -- arguments can be evaluated in any order. This means that at most one such
2749 -- argument can have side effects (e.g. be a call to a parse routine).
2750
2751 procedure Put_Nmake is
2752 S : Sink;
2753 B : Sink;
2754
2755 begin
2756 Create_File (S, "nmake.ads");
2757 Create_File (B, "nmake.adb");
2758 Put (S, "with Namet; use Namet;" & LF);
2759 Put (S, "with Nlists; use Nlists;" & LF);
2760 Put (S, "with Types; use Types;" & LF);
2761 Put (S, "with Uintp; use Uintp;" & LF);
2762 Put (S, "with Urealp; use Urealp;" & LF);
2763
2764 Put (S, LF & "package Nmake is" & LF & LF);
2765 Increase_Indent (S, 3);
2766
2767 Put (S, "-- This package is automatically generated." & LF & LF);
2768 Put (S, "-- See Put_Nmake in gen_il-gen.adb for documentation." & LF & LF);
2769
2770 Put_Make_Decls (S, Node_Kind);
2771
2772 Decrease_Indent (S, 3);
2773 Put (S, "end Nmake;" & LF);
2774
2775 Put (B, "with Atree; use Atree;" & LF);
2776 Put (B, "with Sinfo.Nodes; use Sinfo.Nodes;" & LF);
2777 Put (B, "with Sinfo.Utils; use Sinfo.Utils;" & LF);
2778 Put (B, "with Snames; use Snames;" & LF);
2779 Put (B, "with Stand; use Stand;" & LF);
2780
2781 Put (B, LF & "package body Nmake is" & LF & LF);
2782 Increase_Indent (B, 3);
2783
2784 Put (B, "-- This package is automatically generated." & LF & LF);
2785 Put (B, "pragma Style_Checks (""M200"");" & LF);
2786
2787 Put_Make_Bodies (B, Node_Kind);
2788
2789 Decrease_Indent (B, 3);
2790 Put (B, "end Nmake;" & LF);
2791 end Put_Nmake;
2792
2793 -----------------------
2794 -- Put_Seinfo_Tables --
2795 -----------------------
2796
2797 procedure Put_Seinfo_Tables is
2798 S : Sink;
2799 B : Sink;
2800
2801 Type_Layout : Concrete_Type_Layout_Array;
2802
2803 function Get_Last_Bit
2804 (T : Concrete_Type; F : Opt_Field_Enum; First_Bit : Bit_Offset)
2805 return Bit_Offset;
2806 function First_Bit_Image (First_Bit : Bit_Offset) return String;
2807 function Last_Bit_Image (Last_Bit : Bit_Offset) return String;
2808
2809 procedure Put_Field_List (Bit : Bit_Offset);
2810 -- Print out the list of fields that are allocated (in part, for
2811 -- fields bigger than one bit) at the given bit offset. This allows
2812 -- us to see which fields are overlaid with each other, which should
2813 -- only happen if the sets of types with those fields are disjoint.
2814
2815 function Get_Last_Bit
2816 (T : Concrete_Type; F : Opt_Field_Enum; First_Bit : Bit_Offset)
2817 return Bit_Offset is
2818 begin
2819 return Result : Bit_Offset do
2820 if F = No_Field then
2821 -- We don't have a field size for No_Field, so just look at
2822 -- the bits up to the next slot boundary.
2823
2824 Result := First_Bit;
2825
2826 while (Result + 1) mod SS /= 0
2827 and then Type_Layout (T) (Result + 1) = No_Field
2828 loop
2829 Result := Result + 1;
2830 end loop;
2831
2832 else
2833 Result := First_Bit + Field_Size (F) - 1;
2834 end if;
2835 end return;
2836 end Get_Last_Bit;
2837
2838 function First_Bit_Image (First_Bit : Bit_Offset) return String is
2839 W : constant Bit_Offset := First_Bit / SS;
2840 B : constant Bit_Offset := First_Bit mod SS;
2841 pragma Assert (W * SS + B = First_Bit);
2842 begin
2843 return
2844 Image (W) & "*" & SSS & (if B = 0 then "" else " + " & Image (B));
2845 end First_Bit_Image;
2846
2847 function Last_Bit_Image (Last_Bit : Bit_Offset) return String is
2848 W : constant Bit_Offset := (Last_Bit + 1) / SS;
2849 begin
2850 if W * SS - 1 = Last_Bit then
2851 return Image (W) & "*" & SSS & " - 1";
2852 else
2853 return First_Bit_Image (Last_Bit);
2854 end if;
2855 end Last_Bit_Image;
2856
2857 function Image_Or_Waste (F : Opt_Field_Enum) return String is
2858 (if F = No_Field then "Wasted_Bits" else Image (F));
2859
2860 Num_Wasted_Bits : Bit_Offset'Base := 0;
2861
2862 Type_Layout_Size : Bit_Offset'Base := Type_Layout'Size;
2863 -- Total size of Type_Layout, including the Field_Arrays its
2864 -- components point to.
2865
2866 procedure Put_Field_List (Bit : Bit_Offset) is
2867 First_Time : Boolean := True;
2868 begin
2869 for F in Field_Enum loop
2870 if F /= Between_Node_And_Entity_Fields
2871 and then Bit in First_Bit (F, Field_Table (F).Offset)
2872 .. Last_Bit (F, Field_Table (F).Offset)
2873 then
2874 if First_Time then
2875 First_Time := False;
2876 else
2877 Put (B, "," & LF);
2878 end if;
2879
2880 Put (B, Image (F));
2881 end if;
2882 end loop;
2883 end Put_Field_List;
2884
2885 begin -- Put_Seinfo_Tables
2886 Create_File (S, "seinfo_tables.ads");
2887 Create_File (B, "seinfo_tables.adb");
2888
2889 for T in Concrete_Type loop
2890 Type_Layout (T) := new Field_Array'
2891 (0 .. Type_Bit_Size_Aligned (T) - 1 => No_Field);
2892 Type_Layout_Size := Type_Layout_Size + Type_Layout (T).all'Size;
2893
2894 for F in Field_Enum loop
2895 if Fields_Per_Node (T) (F) then
2896 declare
2897 Off : constant Field_Offset := Field_Table (F).Offset;
2898 subtype Bit_Range is Bit_Offset
2899 range First_Bit (F, Off) .. Last_Bit (F, Off);
2900 begin
2901 pragma Assert
2902 (Type_Layout (T) (Bit_Range) = (Bit_Range => No_Field));
2903 Type_Layout (T) (Bit_Range) := (others => F);
2904 end;
2905 end if;
2906 end loop;
2907 end loop;
2908
2909 for T in Concrete_Type loop
2910 for B in 0 .. Type_Bit_Size_Aligned (T) - 1 loop
2911 if Type_Layout (T) (B) = No_Field then
2912 Num_Wasted_Bits := Num_Wasted_Bits + 1;
2913 end if;
2914 end loop;
2915 end loop;
2916
2917 Put (S, LF & "package Seinfo_Tables is" & LF & LF);
2918 Increase_Indent (S, 3);
2919
2920 Put (S, "-- This package is automatically generated." & LF & LF);
2921
2922 Put (S, "-- This package is not used by the compiler." & LF);
2923 Put (S, "-- The body contains tables that are intended to be used by humans to" & LF);
2924 Put (S, "-- help understand the layout of various data structures." & LF);
2925 Put (S, "-- Search for ""--"" to find major sections of code." & LF & LF);
2926
2927 Put (S, "pragma Elaborate_Body;" & LF);
2928
2929 Decrease_Indent (S, 3);
2930 Put (S, LF & "end Seinfo_Tables;" & LF);
2931
2932 Put (B, "with Gen_IL.Types; use Gen_IL.Types;" & LF);
2933 Put (B, "with Gen_IL.Fields; use Gen_IL.Fields;" & LF);
2934 Put (B, "with Gen_IL.Internals; use Gen_IL.Internals;" & LF);
2935
2936 Put (B, LF & "package body Seinfo_Tables is" & LF & LF);
2937 Increase_Indent (B, 3);
2938
2939 Put (B, "-- This package is automatically generated." & LF & LF);
2940
2941 Put (B, "Num_Wasted_Bits : Bit_Offset'Base := " & Image (Num_Wasted_Bits) &
2942 " with Unreferenced;" & LF);
2943
2944 Put (B, LF & "Wasted_Bits : constant Opt_Field_Enum := No_Field;" & LF);
2945
2946 Put (B, LF & "-- Table showing the layout of each Node_Or_Entity_Type. For each" & LF);
2947 Put (B, "-- concrete type, we show the bits used by each field. Each field" & LF);
2948 Put (B, "-- uses the same bit range in all types. This table is not used by" & LF);
2949 Put (B, "-- the compiler; it is for information only." & LF & LF);
2950
2951 Put (B, "-- Wasted_Bits are unused bits between fields, and padding at the end" & LF);
2952 Put (B, "-- to round up to a multiple of the slot size." & LF);
2953
2954 Put (B, LF & "-- Type_Layout is " & Image (Type_Layout_Size / 8) & " bytes." & LF);
2955
2956 Put (B, LF & "pragma Style_Checks (Off);" & LF);
2957 Put (B, "Type_Layout : constant Concrete_Type_Layout_Array := " & LF);
2958 Increase_Indent (B, 2);
2959 Put (B, "-- Concrete node types:" & LF);
2960 Put (B, "(");
2961 Increase_Indent (B, 1);
2962
2963 declare
2964 First_Time : Boolean := True;
2965
2966 begin
2967 for T in Concrete_Type loop
2968 if First_Time then
2969 First_Time := False;
2970 else
2971 Put (B, "," & LF & LF);
2972 end if;
2973
2974 if T = Concrete_Entity'First then
2975 Put (B, "-- Concrete entity types:" & LF & LF);
2976 end if;
2977
2978 Put (B, Image (T) & " => new Field_Array'" & LF);
2979
2980 Increase_Indent (B, 2);
2981 Put (B, "(");
2982 Increase_Indent (B, 1);
2983
2984 declare
2985 First_Time : Boolean := True;
2986 First_Bit : Bit_Offset := 0;
2987 F : Opt_Field_Enum;
2988
2989 function Node_Field_Of_Entity return String is
2990 (if T in Entity_Type and then F in Node_Field then
2991 " -- N" else "");
2992 -- A comment to put out for fields of entities that are
2993 -- shared with nodes, such as Chars.
2994
2995 begin
2996 while First_Bit < Type_Bit_Size_Aligned (T) loop
2997 if First_Time then
2998 First_Time := False;
2999 else
3000 Put (B, "," & Node_Field_Of_Entity & LF);
3001 end if;
3002
3003 F := Type_Layout (T) (First_Bit);
3004
3005 declare
3006 Last_Bit : constant Bit_Offset :=
3007 Get_Last_Bit (T, F, First_Bit);
3008 begin
3009 pragma Assert
3010 (Type_Layout (T) (First_Bit .. Last_Bit) =
3011 (First_Bit .. Last_Bit => F));
3012
3013 if Last_Bit = First_Bit then
3014 Put (B, First_Bit_Image (First_Bit) & " => " &
3015 Image_Or_Waste (F));
3016 else
3017 pragma Assert
3018 (if F /= No_Field then
3019 First_Bit mod Field_Size (F) = 0);
3020 Put (B, First_Bit_Image (First_Bit) & " .. " &
3021 Last_Bit_Image (Last_Bit) & " => " &
3022 Image_Or_Waste (F));
3023 end if;
3024
3025 First_Bit := Last_Bit + 1;
3026 end;
3027 end loop;
3028 end;
3029
3030 Decrease_Indent (B, 1);
3031 Put (B, ")");
3032 Decrease_Indent (B, 2);
3033 end loop;
3034 end;
3035
3036 Decrease_Indent (B, 1);
3037 Put (B, ") -- Type_Layout" & LF);
3038 Increase_Indent (B, 6);
3039 Put (B, "with Export, Convention => Ada;" & LF);
3040 Decrease_Indent (B, 6);
3041 Decrease_Indent (B, 2);
3042
3043 Put (B, LF & "-- Table mapping bit offsets to the set of fields at that offset" & LF & LF);
3044 Put (B, "Bit_Used : constant Offset_To_Fields_Mapping :=" & LF);
3045
3046 Increase_Indent (B, 2);
3047 Put (B, "(");
3048 Increase_Indent (B, 1);
3049
3050 declare
3051 First_Time : Boolean := True;
3052 begin
3053 for Bit in 0 .. Bit_Offset'Max
3054 (Max_Node_Bit_Size, Max_Entity_Bit_Size)
3055 loop
3056 if First_Time then
3057 First_Time := False;
3058 else
3059 Put (B, "," & LF & LF);
3060 end if;
3061
3062 Put (B, First_Bit_Image (Bit) & " => new Field_Array'" & LF);
3063
3064 -- Use [...] notation here, to get around annoying Ada
3065 -- limitations on empty and singleton aggregates. This code is
3066 -- not used in the compiler, so there are no bootstrap issues.
3067
3068 Increase_Indent (B, 2);
3069 Put (B, "[");
3070 Increase_Indent (B, 1);
3071
3072 Put_Field_List (Bit);
3073
3074 Decrease_Indent (B, 1);
3075 Put (B, "]");
3076 Decrease_Indent (B, 2);
3077 end loop;
3078 end;
3079
3080 Decrease_Indent (B, 1);
3081 Put (B, "); -- Bit_Used" & LF);
3082 Decrease_Indent (B, 2);
3083
3084 Decrease_Indent (B, 3);
3085 Put (B, LF & "end Seinfo_Tables;" & LF);
3086
3087 end Put_Seinfo_Tables;
3088
3089 -----------------------------
3090 -- Put_C_Type_And_Subtypes --
3091 -----------------------------
3092
3093 procedure Put_C_Type_And_Subtypes
3094 (S : in out Sink; Root : Root_Type) is
3095
3096 Cur_Pos : Root_Nat := 0;
3097 -- Current Node_Kind'Pos or Entity_Kind'Pos to be printed
3098
3099 procedure Put_Enum_Lit (T : Node_Or_Entity_Type);
3100 -- Print out the enumerator corresponding to the Ada enumeration literal
3101 -- for T in Node_Kind and Entity_Kind (i.e. concrete types).
3102 -- This looks like "Some_Kind = <pos>", where Some_Kind
3103 -- is the Node_Kind or Entity_Kind enumeration literal, and
3104 -- <pos> is Node_Kind'Pos or Entity_Kind'Pos of that literal.
3105
3106 procedure Put_Kind_Subtype (T : Node_Or_Entity_Type);
3107 -- Print out the SUBTYPE macro call corresponding to an abstract
3108 -- type.
3109
3110 procedure Put_Enum_Lit (T : Node_Or_Entity_Type) is
3111 begin
3112 if T in Concrete_Type then
3113 Put (S, " " & Image (T) & " = " & Image (Cur_Pos) & "," & LF);
3114 Cur_Pos := Cur_Pos + 1;
3115 end if;
3116 end Put_Enum_Lit;
3117
3118 procedure Put_Kind_Subtype (T : Node_Or_Entity_Type) is
3119 begin
3120 if T in Abstract_Type and then Type_Table (T).Parent /= No_Type then
3121 Put (S, "SUBTYPE (" & Image (T) & ", " &
3122 Image (Type_Table (T).Parent) & "," & LF);
3123 Increase_Indent (S, 3);
3124 Put (S, Image (Type_Table (T).First) & "," & LF);
3125 Put (S, Image (Type_Table (T).Last) & ")" & LF);
3126 Decrease_Indent (S, 3);
3127 end if;
3128 end Put_Kind_Subtype;
3129
3130 begin
3131 Put_Union_Membership (S, Root, Only_Prototypes => True);
3132
3133 Put (S, "enum " & Node_Or_Entity (Root) & "_Kind : unsigned int {" & LF);
3134 Iterate_Types (Root, Pre => Put_Enum_Lit'Access);
3135 Put (S, "};" & LF);
3136
3137 Put (S, "#define Number_" & Node_Or_Entity (Root) & "_Kinds " &
3138 Image (Cur_Pos) & "" & LF & LF);
3139
3140 Iterate_Types (Root, Pre => Put_Kind_Subtype'Access);
3141
3142 Put_Union_Membership (S, Root, Only_Prototypes => False);
3143 end Put_C_Type_And_Subtypes;
3144
3145 ------------------
3146 -- Put_C_Getter --
3147 ------------------
3148
3149 procedure Put_C_Getter
3150 (S : in out Sink; F : Field_Enum)
3151 is
3152 Rec : Field_Info renames Field_Table (F).all;
3153
3154 Off : constant Field_Offset := Rec.Offset;
3155 F_Size : constant Bit_Offset := Field_Size (Rec.Field_Type);
3156 F_Per_Slot : constant Field_Offset :=
3157 SS / Field_Offset (Field_Size (Rec.Field_Type));
3158 Slot_Off : constant Field_Offset := Off / F_Per_Slot;
3159 In_NH : constant Boolean := Slot_Off < Num_Header_Slots;
3160
3161 N : constant String := Node_To_Fetch_From (F);
3162 begin
3163 Put (S, "INLINE " & Get_Set_Id_Image (Rec.Field_Type) &
3164 " " & Image (F) & " (Node_Id N)" & LF);
3165
3166 Put (S, "{" & LF);
3167 Increase_Indent (S, 3);
3168 Put (S, "const Field_Offset Off = " & Image (Rec.Offset) & ";" & LF);
3169 Put (S, "const Field_Offset F_Size = " & Image (F_Size) & ";" & LF);
3170
3171 if Field_Size (Rec.Field_Type) /= SS then
3172 Put (S, "const any_slot Mask = (1 << F_Size) - 1;" & LF);
3173 end if;
3174
3175 Put (S, "const Field_Offset F_Per_Slot = Slot_Size / F_Size;" & LF);
3176 Put (S, "const Field_Offset Slot_Off = Off / F_Per_Slot;" & LF);
3177 Put (S, LF);
3178 if In_NH then
3179 Put (S, "any_slot slot = Node_Offsets_Ptr[" & N & "].Slots[Slot_Off];" & LF);
3180 else
3181 Put (S, "any_slot slot = *(Slots_Ptr + Node_Offsets_Ptr[" & N &
3182 "].Offset + Slot_Off);" & LF);
3183 end if;
3184
3185 if Field_Size (Rec.Field_Type) /= SS then
3186 Put (S, "unsigned int Raw = (slot >> (Off % F_Per_Slot) * F_Size) & Mask;" & LF);
3187 else
3188 Put (S, "unsigned int Raw = slot;" & LF);
3189 end if;
3190
3191 Put (S, Get_Set_Id_Image (Rec.Field_Type) & " val = (" &
3192 Get_Set_Id_Image (Rec.Field_Type) & ") ");
3193
3194 if Field_Has_Special_Default (Rec.Field_Type) then
3195 Increase_Indent (S, 2);
3196 Put (S, "(Raw? Raw : " & Special_Default (Rec.Field_Type) & ")");
3197 Decrease_Indent (S, 2);
3198
3199 else
3200 Put (S, "Raw");
3201 end if;
3202
3203 Put (S, ";" & LF);
3204
3205 Put (S, "return val;" & LF);
3206 Decrease_Indent (S, 3);
3207 Put (S, "}" & LF & LF);
3208 end Put_C_Getter;
3209
3210 -------------------
3211 -- Put_C_Getters --
3212 -------------------
3213
3214 procedure Put_C_Getters
3215 (S : in out Sink; Root : Root_Type)
3216 is
3217 begin
3218 Put (S, "// Getters for fields" & LF & LF);
3219
3220 for F in First_Field (Root) .. Last_Field (Root) loop
3221 Put_C_Getter (S, F);
3222 end loop;
3223 end Put_C_Getters;
3224
3225 --------------------------
3226 -- Put_Union_Membership --
3227 --------------------------
3228
3229 procedure Put_Union_Membership
3230 (S : in out Sink; Root : Root_Type; Only_Prototypes : Boolean) is
3231
3232 procedure Put_Ors (T : Abstract_Type);
3233 -- Print the "or" (i.e. "||") of tests whether kind is in each child
3234 -- type.
3235
3236 procedure Put_Ors (T : Abstract_Type) is
3237 First_Time : Boolean := True;
3238 begin
3239 for Child of Type_Table (T).Children loop
3240 if First_Time then
3241 First_Time := False;
3242 else
3243 Put (S, " ||" & LF);
3244 end if;
3245
3246 -- Unions, other abstract types, and concrete types each have
3247 -- their own way of testing membership in the C++ code.
3248
3249 if Child in Abstract_Type then
3250 if Type_Table (Child).Is_Union then
3251 Put (S, "Is_In_" & Image (Child) & " (kind)");
3252
3253 else
3254 Put (S, "IN (kind, " & Image (Child) & ")");
3255 end if;
3256
3257 else
3258 Put (S, "kind == " & Image (Child));
3259 end if;
3260 end loop;
3261 end Put_Ors;
3262
3263 begin
3264 if not Only_Prototypes then
3265 Put (S, LF & "// Membership tests for union types" & LF & LF);
3266 end if;
3267
3268 for T in First_Abstract (Root) .. Last_Abstract (Root) loop
3269 if Type_Table (T) /= null and then Type_Table (T).Is_Union then
3270 Put (S, "INLINE Boolean" & LF);
3271 Put (S, "Is_In_" & Image (T) & " (" &
3272 Node_Or_Entity (Root) & "_Kind kind)" &
3273 (if Only_Prototypes then ";" else "") & LF);
3274
3275 if not Only_Prototypes then
3276 Put (S, "{" & LF);
3277 Increase_Indent (S, 3);
3278 Put (S, "return" & LF);
3279 Increase_Indent (S, 3);
3280 Put_Ors (T);
3281 Decrease_Indent (S, 3);
3282 Decrease_Indent (S, 3);
3283 Put (S, ";" & LF & "}" & LF);
3284 end if;
3285
3286 Put (S, "" & LF);
3287 end if;
3288 end loop;
3289 end Put_Union_Membership;
3290
3291 ---------------------
3292 -- Put_Sinfo_Dot_H --
3293 ---------------------
3294
3295 procedure Put_Sinfo_Dot_H is
3296 S : Sink;
3297
3298 begin
3299 Create_File (S, "sinfo.h");
3300 Put (S, "#ifdef __cplusplus" & LF);
3301 Put (S, "extern ""C"" {" & LF);
3302 Put (S, "#endif" & LF & LF);
3303
3304 Put (S, "typedef Boolean Flag;" & LF & LF);
3305
3306 Put (S, "#define N_Head " & N_Head & LF);
3307 Put (S, "" & LF);
3308 Put (S, "typedef struct Node_Header {" & LF);
3309 Increase_Indent (S, 2);
3310 Put (S, "any_slot Slots[N_Head];" & LF);
3311 Put (S, "Field_Offset Offset;" & LF);
3312 Decrease_Indent (S, 2);
3313 Put (S, "} Node_Header;" & LF & LF);
3314
3315 Put (S, "extern Node_Header *Node_Offsets_Ptr;" & LF);
3316 Put (S, "extern any_slot *Slots_Ptr;" & LF & LF);
3317
3318 Put_C_Type_And_Subtypes (S, Node_Kind);
3319
3320 Put (S, "// Getters corresponding to instantiations of Atree.Get_n_Bit_Field"
3321 & LF & LF);
3322
3323 Put_C_Getters (S, Node_Kind);
3324
3325 Put (S, "#ifdef __cplusplus" & LF);
3326 Put (S, "}" & LF);
3327 Put (S, "#endif" & LF);
3328 end Put_Sinfo_Dot_H;
3329
3330 ---------------------
3331 -- Put_Einfo_Dot_H --
3332 ---------------------
3333
3334 procedure Put_Einfo_Dot_H is
3335 S : Sink;
3336
3337 procedure Put_Membership_Query_Spec (T : Node_Or_Entity_Type);
3338 procedure Put_Membership_Query_Defn (T : Node_Or_Entity_Type);
3339 -- Print out the Is_... function for T that calls the IN macro on the
3340 -- SUBTYPE.
3341
3342 procedure Put_Membership_Query_Spec (T : Node_Or_Entity_Type) is
3343 Im : constant String := Image (T);
3344 pragma Assert (Im (Im'Last - 4 .. Im'Last) = "_Kind");
3345 Im2 : constant String := Im (Im'First .. Im'Last - 5);
3346 Typ : constant String :=
3347 (if Is_Descendant (Type_Kind, T)
3348 and then T /= Type_Kind
3349 then "_Type"
3350 else "");
3351 begin
3352 pragma Assert (not Type_Table (T).Is_Union);
3353
3354 Put (S, "INLINE B Is_" & Im2 & Typ & " (E Id)");
3355 end Put_Membership_Query_Spec;
3356
3357 procedure Put_Membership_Query_Defn (T : Node_Or_Entity_Type) is
3358 begin
3359 if T in Abstract_Type and T not in Root_Type then
3360 Put_Membership_Query_Spec (T);
3361 Put (S, "" & LF);
3362 Increase_Indent (S, 3);
3363 Put (S, "{ return IN (Ekind (Id), " & Image (T) & "); }" & LF);
3364 Decrease_Indent (S, 3);
3365 end if;
3366 end Put_Membership_Query_Defn;
3367
3368 begin
3369 Create_File (S, "einfo.h");
3370 Put (S, "#ifdef __cplusplus" & LF);
3371 Put (S, "extern ""C"" {" & LF);
3372 Put (S, "#endif" & LF & LF);
3373
3374 Put (S, "typedef Boolean Flag;" & LF & LF);
3375
3376 Put_C_Type_And_Subtypes (S, Entity_Kind);
3377
3378 Put_C_Getters (S, Entity_Kind);
3379
3380 Put (S, "// Abstract type queries" & LF & LF);
3381
3382 Iterate_Types (Entity_Kind, Pre => Put_Membership_Query_Defn'Access);
3383
3384 Put (S, LF & "#ifdef __cplusplus" & LF);
3385 Put (S, "}" & LF);
3386 Put (S, "#endif" & LF);
3387 end Put_Einfo_Dot_H;
3388
3389 begin -- Compile
3390
3391 Check_Completeness;
3392
3393 Compute_Ranges;
3394 Inherit_Fields;
3395 Compute_Fields_Per_Node;
3396 Compute_Field_Offsets;
3397 Compute_Type_Sizes;
3398 Check_For_Syntactic_Field_Mismatch;
3399
3400 Verify_Type_Table;
3401
3402 Node_Field_Types_Used :=
3403 Field_Types_Used (Node_Field'First, Node_Field'Last);
3404 Entity_Field_Types_Used :=
3405 Field_Types_Used (Entity_Field'First, Entity_Field'Last);
3406
3407 Put_Seinfo;
3408
3409 Put_Nodes;
3410
3411 Put_Entities;
3412
3413 Put_Nmake;
3414
3415 Put_Seinfo_Tables;
3416
3417 Put_Sinfo_Dot_H;
3418 Put_Einfo_Dot_H;
3419
3420 end Compile;
3421
3422 --------
3423 -- Sy --
3424 --------
3425
3426 function Sy
3427 (Field : Node_Field;
3428 Field_Type : Type_Enum;
3429 Default_Value : Field_Default_Value := No_Default;
3430 Pre, Pre_Get, Pre_Set : String := "") return Field_Sequence is
3431 begin
3432 return
3433 (1 => Create_Syntactic_Field
3434 (Field, Field_Type, Default_Value, Pre, Pre_Get, Pre_Set));
3435 end Sy;
3436
3437 --------
3438 -- Sm --
3439 --------
3440
3441 function Sm
3442 (Field : Field_Enum;
3443 Field_Type : Type_Enum;
3444 Type_Only : Type_Only_Enum := No_Type_Only;
3445 Pre, Pre_Get, Pre_Set : String := "") return Field_Sequence is
3446 begin
3447 return (1 => Create_Semantic_Field
3448 (Field, Field_Type, Type_Only, Pre, Pre_Get, Pre_Set));
3449 end Sm;
3450
3451 end Gen_IL.Gen;