Debian Codegraph @

src/backend/nodes/gen_node_support.pl

raw
1 #!/usr/bin/perl
2 #----------------------------------------------------------------------
3 #
4 # Generate node support files:
5 # - nodetags.h
6 # - copyfuncs
7 # - equalfuncs
8 # - readfuncs
9 # - outfuncs
10 #
11 # Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
12 # Portions Copyright (c) 1994, Regents of the University of California
13 #
14 # src/backend/nodes/gen_node_support.pl
15 #
16 #----------------------------------------------------------------------
17
18 use strict;
19 use warnings FATAL => 'all';
20
21 use File::Basename;
22 use Getopt::Long;
23
24 use FindBin;
25 use lib "$FindBin::RealBin/../catalog";
26
27 use Catalog; # for RenameTempFile
28
29 my $output_path = '.';
30
31 GetOptions('outdir:s' => \$output_path)
32 or die "$0: wrong arguments";
33
34
35 # Test whether first argument is element of the list in the second
36 # argument
37 sub elem
38 {
39 my $x = shift;
40 return grep { $_ eq $x } @_;
41 }
42
43
44 # This list defines the canonical set of header files to be read by this
45 # script, and the order they are to be processed in. We must have a stable
46 # processing order, else the NodeTag enum's order will vary, with catastrophic
47 # consequences for ABI stability across different builds.
48 #
49 # Currently, the various build systems also have copies of this list,
50 # so that they can do dependency checking properly. In future we may be
51 # able to make this list the only copy. For now, we just check that
52 # it matches the list of files passed on the command line.
53 my @all_input_files = qw(
54 nodes/nodes.h
55 nodes/primnodes.h
56 nodes/parsenodes.h
57 nodes/pathnodes.h
58 nodes/plannodes.h
59 nodes/execnodes.h
60 access/amapi.h
61 access/cmptype.h
62 access/sdir.h
63 access/tableam.h
64 access/tsmapi.h
65 commands/event_trigger.h
66 commands/trigger.h
67 executor/tuptable.h
68 foreign/fdwapi.h
69 nodes/bitmapset.h
70 nodes/extensible.h
71 nodes/lockoptions.h
72 nodes/miscnodes.h
73 nodes/replnodes.h
74 nodes/supportnodes.h
75 nodes/value.h
76 utils/rel.h
77 );
78
79 # Nodes from these input files are automatically treated as nodetag_only.
80 # In the future we might add explicit pg_node_attr labeling to some of these
81 # files and remove them from this list, but for now this is the path of least
82 # resistance.
83 my @nodetag_only_files = qw(
84 nodes/execnodes.h
85 access/amapi.h
86 access/sdir.h
87 access/tableam.h
88 access/tsmapi.h
89 commands/event_trigger.h
90 commands/trigger.h
91 executor/tuptable.h
92 foreign/fdwapi.h
93 nodes/lockoptions.h
94 nodes/miscnodes.h
95 nodes/replnodes.h
96 nodes/supportnodes.h
97 );
98
99 # ARM ABI STABILITY CHECK HERE:
100 #
101 # In stable branches, set $last_nodetag to the name of the last node type
102 # that should receive an auto-generated nodetag number, and $last_nodetag_no
103 # to its number. (Find these values in the last line of the current
104 # nodetags.h file.) The script will then complain if those values don't
105 # match reality, providing a cross-check that we haven't broken ABI by
106 # adding or removing nodetags.
107 # In HEAD, these variables should be left undef, since we don't promise
108 # ABI stability during development.
109
110 my $last_nodetag = 'WindowObjectData';
111 my $last_nodetag_no = 479;
112
113 # output file names
114 my @output_files;
115
116 # collect node names
117 my @node_types = qw(Node);
118 # collect info for each node type
119 my %node_type_info;
120
121 # node types we don't want copy support for
122 my @no_copy;
123 # node types we don't want equal support for
124 my @no_equal;
125 # node types we don't want query jumble support for
126 my @no_query_jumble;
127 # node types we don't want read support for
128 my @no_read;
129 # node types we don't want read/write support for
130 my @no_read_write;
131 # node types that have handmade read/write support
132 my @special_read_write;
133 # node types we don't want any support functions for, just node tags
134 my @nodetag_only;
135
136 # types that are copied by straight assignment
137 my @scalar_types = qw(
138 bits32 bool char double int int8 int16 int32 int64 long uint8 uint16 uint32 uint64
139 AclMode AttrNumber Cardinality Cost Index Oid RelFileNumber Selectivity Size StrategyNumber SubTransactionId TimeLineID XLogRecPtr
140 );
141
142 # collect enum types
143 my @enum_types;
144
145 # collect types that are abstract (hence no node tag, no support functions)
146 my @abstract_types = qw(Node);
147
148 # Special cases that either don't have their own struct or the struct
149 # is not in a header file. We generate node tags for them, but
150 # they otherwise don't participate in node support.
151 my @extra_tags = qw(
152 IntList OidList XidList
153 AllocSetContext GenerationContext SlabContext BumpContext
154 TIDBitmap
155 WindowObjectData
156 );
157
158 # This is a regular node, but we skip parsing it from its header file
159 # since we won't use its internal structure here anyway.
160 push @node_types, qw(List);
161 # Lists are specially treated in all five support files, too.
162 # (Ideally we'd mark List as "special copy/equal" not "no copy/equal".
163 # But until there's other use-cases for that, just hot-wire the tests
164 # that would need to distinguish.)
165 push @no_copy, qw(List);
166 push @no_equal, qw(List);
167 push @no_query_jumble, qw(List);
168 push @special_read_write, qw(List);
169
170 # Nodes with custom copy/equal implementations are skipped from
171 # .funcs.c but need case statements in .switch.c.
172 my @custom_copy_equal;
173
174 # Similarly for custom read/write implementations.
175 my @custom_read_write;
176
177 # Similarly for custom query jumble implementation.
178 my @custom_query_jumble;
179
180 # Track node types with manually assigned NodeTag numbers.
181 my %manual_nodetag_number;
182
183 # This is a struct, so we can copy it by assignment. Equal support is
184 # currently not required.
185 push @scalar_types, qw(QualCost);
186
187
188 ## check that we have the expected number of files on the command line
189 die "wrong number of input files, expected:\n@all_input_files\ngot:\n@ARGV\n"
190 if ($#ARGV != $#all_input_files);
191
192 ## read input
193
194 my $next_input_file = 0;
195 foreach my $infile (@ARGV)
196 {
197 my $in_struct;
198 my $in_struct_lineno;
199 my $subline;
200 my $is_node_struct;
201 my $supertype;
202 my $supertype_field;
203
204 my $node_attrs = '';
205 my $node_attrs_lineno;
206 my @my_fields;
207 my %my_field_types;
208 my %my_field_attrs;
209
210 # open file with name from command line, which may have a path prefix
211 open my $ifh, '<', $infile or die "could not open \"$infile\": $!";
212
213 # now shorten filename for use below
214 $infile =~ s!.*src/include/!!;
215
216 # check it against next member of @all_input_files
217 die "wrong input file ordering, expected @all_input_files\n"
218 if ($infile ne $all_input_files[$next_input_file]);
219 $next_input_file++;
220
221 my $raw_file_content = do { local $/; <$ifh> };
222
223 # strip C comments, preserving newlines so we can count lines correctly
224 my $file_content = '';
225 while ($raw_file_content =~ m{^(.*?)(/\*.*?\*/)(.*)$}s)
226 {
227 $file_content .= $1;
228 my $comment = $2;
229 $raw_file_content = $3;
230 $comment =~ tr/\n//cd;
231 $file_content .= $comment;
232 }
233 $file_content .= $raw_file_content;
234
235 my $lineno = 0;
236 my $prevline = '';
237 foreach my $line (split /\n/, $file_content)
238 {
239 # per-physical-line processing
240 $lineno++;
241 chomp $line;
242 $line =~ s/\s*$//;
243 next if $line eq '';
244 next if $line =~ /^#(define|ifdef|endif)/;
245
246 # within a struct, don't process until we have whole logical line
247 if ($in_struct && $subline > 0)
248 {
249 if ($line =~ /;$/)
250 {
251 # found the end, re-attach any previous line(s)
252 $line = $prevline . $line;
253 $prevline = '';
254 }
255 elsif ($prevline eq ''
256 && $line =~ /^\s*pg_node_attr\(([\w(), ]*)\)$/)
257 {
258 # special case: node-attributes line doesn't end with semi
259 }
260 else
261 {
262 # set it aside for a moment
263 $prevline .= $line . ' ';
264 next;
265 }
266 }
267
268 # we are analyzing a struct definition
269 if ($in_struct)
270 {
271 $subline++;
272
273 # first line should have opening brace
274 if ($subline == 1)
275 {
276 $is_node_struct = 0;
277 $supertype = undef;
278 next if $line eq '{';
279 die "$infile:$lineno: expected opening brace\n";
280 }
281 # second line could be node attributes
282 elsif ($subline == 2
283 && $line =~ /^\s*pg_node_attr\(([\w(), ]*)\)$/)
284 {
285 $node_attrs = $1;
286 $node_attrs_lineno = $lineno;
287 # hack: don't count the line
288 $subline--;
289 next;
290 }
291 # next line should have node tag or supertype
292 elsif ($subline == 2)
293 {
294 if ($line =~ /^\s*NodeTag\s+type;/)
295 {
296 $is_node_struct = 1;
297 next;
298 }
299 elsif ($line =~ /\s*(\w+)\s+(\w+);/ and elem $1, @node_types)
300 {
301 $is_node_struct = 1;
302 $supertype = $1;
303 $supertype_field = $2;
304 next;
305 }
306 }
307
308 # end of struct
309 if ($line =~ /^\}\s*(?:\Q$in_struct\E\s*)?;$/)
310 {
311 if ($is_node_struct)
312 {
313 # This is the end of a node struct definition.
314 # Save everything we have collected.
315
316 foreach my $attr (split /,\s*/, $node_attrs)
317 {
318 if ($attr eq 'abstract')
319 {
320 push @abstract_types, $in_struct;
321 }
322 elsif ($attr eq 'custom_copy_equal')
323 {
324 push @custom_copy_equal, $in_struct;
325 }
326 elsif ($attr eq 'custom_read_write')
327 {
328 push @custom_read_write, $in_struct;
329 }
330 elsif ($attr eq 'custom_query_jumble')
331 {
332 push @custom_query_jumble, $in_struct;
333 }
334 elsif ($attr eq 'no_copy')
335 {
336 push @no_copy, $in_struct;
337 }
338 elsif ($attr eq 'no_equal')
339 {
340 push @no_equal, $in_struct;
341 }
342 elsif ($attr eq 'no_copy_equal')
343 {
344 push @no_copy, $in_struct;
345 push @no_equal, $in_struct;
346 }
347 elsif ($attr eq 'no_query_jumble')
348 {
349 push @no_query_jumble, $in_struct;
350 }
351 elsif ($attr eq 'no_read')
352 {
353 push @no_read, $in_struct;
354 }
355 elsif ($attr eq 'nodetag_only')
356 {
357 push @nodetag_only, $in_struct;
358 }
359 elsif ($attr eq 'special_read_write')
360 {
361 push @special_read_write, $in_struct;
362 }
363 elsif ($attr =~ /^nodetag_number\((\d+)\)$/)
364 {
365 $manual_nodetag_number{$in_struct} = $1;
366 }
367 else
368 {
369 die
370 "$infile:$node_attrs_lineno: unrecognized attribute \"$attr\"\n";
371 }
372 }
373
374 # node name
375 push @node_types, $in_struct;
376
377 # field names, types, attributes
378 my @f = @my_fields;
379 my %ft = %my_field_types;
380 my %fa = %my_field_attrs;
381
382 # If there is a supertype, add those fields, too.
383 if ($supertype)
384 {
385 my @superfields;
386 foreach
387 my $sf (@{ $node_type_info{$supertype}->{fields} })
388 {
389 my $fn = "${supertype_field}.$sf";
390 push @superfields, $fn;
391 $ft{$fn} =
392 $node_type_info{$supertype}->{field_types}{$sf};
393 if ($node_type_info{$supertype}
394 ->{field_attrs}{$sf})
395 {
396 # Copy any attributes, adjusting array_size field references
397 my @newa = @{ $node_type_info{$supertype}
398 ->{field_attrs}{$sf} };
399 foreach my $a (@newa)
400 {
401 $a =~
402 s/array_size\((\w+)\)/array_size(${supertype_field}.$1)/;
403 }
404 $fa{$fn} = \@newa;
405 }
406 }
407 unshift @f, @superfields;
408 }
409 # save in global info structure
410 $node_type_info{$in_struct}->{fields} = \@f;
411 $node_type_info{$in_struct}->{field_types} = \%ft;
412 $node_type_info{$in_struct}->{field_attrs} = \%fa;
413
414 # Propagate nodetag_only marking from files to nodes
415 push @nodetag_only, $in_struct
416 if (elem $infile, @nodetag_only_files);
417
418 # Propagate some node attributes from supertypes
419 if ($supertype)
420 {
421 push @no_copy, $in_struct
422 if elem $supertype, @no_copy;
423 push @no_equal, $in_struct
424 if elem $supertype, @no_equal;
425 push @no_read, $in_struct
426 if elem $supertype, @no_read;
427 push @no_query_jumble, $in_struct
428 if elem $supertype, @no_query_jumble;
429 }
430 }
431
432 # start new cycle
433 $in_struct = undef;
434 $node_attrs = '';
435 @my_fields = ();
436 %my_field_types = ();
437 %my_field_attrs = ();
438 }
439 # normal struct field
440 elsif ($line =~
441 /^\s*(.+)\s*\b(\w+)(\[[\w\s+]+\])?\s*(?:pg_node_attr\(([\w(), ]*)\))?;/
442 )
443 {
444 if ($is_node_struct)
445 {
446 my $type = $1;
447 my $name = $2;
448 my $array_size = $3;
449 my $attrs = $4;
450
451 # strip "const"
452 $type =~ s/^const\s*//;
453 # strip trailing space
454 $type =~ s/\s*$//;
455 # strip space between type and "*" (pointer) */
456 $type =~ s/\s+\*$/*/;
457 # strip space between type and "**" (array of pointers) */
458 $type =~ s/\s+\*\*$/**/;
459
460 die
461 "$infile:$lineno: cannot parse data type in \"$line\"\n"
462 if $type eq '';
463
464 my @attrs;
465 if ($attrs)
466 {
467 @attrs = split /,\s*/, $attrs;
468 foreach my $attr (@attrs)
469 {
470 if ( $attr !~ /^array_size\(\w+\)$/
471 && $attr !~ /^copy_as\(\w+\)$/
472 && $attr !~ /^read_as\(\w+\)$/
473 && !elem $attr,
474 qw(copy_as_scalar
475 custom_query_jumble
476 equal_as_scalar
477 equal_ignore
478 equal_ignore_if_zero
479 query_jumble_ignore
480 query_jumble_location
481 query_jumble_squash
482 read_write_ignore
483 write_only_relids
484 write_only_nondefault_pathtarget
485 write_only_req_outer))
486 {
487 die
488 "$infile:$lineno: unrecognized attribute \"$attr\"\n";
489 }
490 }
491 }
492
493 $type = $type . $array_size if $array_size;
494 push @my_fields, $name;
495 $my_field_types{$name} = $type;
496 $my_field_attrs{$name} = \@attrs;
497 }
498 }
499 # function pointer field
500 elsif ($line =~
501 /^\s*([\w\s*]+)\s*\(\*(\w+)\)\s*\((.*)\)\s*(?:pg_node_attr\(([\w(), ]*)\))?;/
502 )
503 {
504 if ($is_node_struct)
505 {
506 my $type = $1;
507 my $name = $2;
508 my $args = $3;
509 my $attrs = $4;
510
511 my @attrs;
512 if ($attrs)
513 {
514 @attrs = split /,\s*/, $attrs;
515 foreach my $attr (@attrs)
516 {
517 if ( $attr !~ /^copy_as\(\w+\)$/
518 && $attr !~ /^read_as\(\w+\)$/
519 && !elem $attr,
520 qw(equal_ignore read_write_ignore))
521 {
522 die
523 "$infile:$lineno: unrecognized attribute \"$attr\"\n";
524 }
525 }
526 }
527
528 push @my_fields, $name;
529 $my_field_types{$name} = 'function pointer';
530 $my_field_attrs{$name} = \@attrs;
531 }
532 }
533 else
534 {
535 # We're not too picky about what's outside structs,
536 # but we'd better understand everything inside.
537 die "$infile:$lineno: could not parse \"$line\"\n";
538 }
539 }
540 # not in a struct
541 else
542 {
543 # start of a struct?
544 if ($line =~ /^(?:typedef )?struct (\w+)$/ && $1 ne 'Node')
545 {
546 $in_struct = $1;
547 $in_struct_lineno = $lineno;
548 $subline = 0;
549 }
550 # one node type typedef'ed directly from another
551 elsif ($line =~ /^typedef (\w+) (\w+);$/ and elem $1, @node_types)
552 {
553 my $alias_of = $1;
554 my $n = $2;
555
556 # copy everything over
557 push @node_types, $n;
558 my @f = @{ $node_type_info{$alias_of}->{fields} };
559 my %ft = %{ $node_type_info{$alias_of}->{field_types} };
560 my %fa = %{ $node_type_info{$alias_of}->{field_attrs} };
561 $node_type_info{$n}->{fields} = \@f;
562 $node_type_info{$n}->{field_types} = \%ft;
563 $node_type_info{$n}->{field_attrs} = \%fa;
564 }
565 # collect enum names
566 elsif ($line =~ /^typedef enum (\w+)(\s*\/\*.*)?$/)
567 {
568 push @enum_types, $1;
569 }
570 }
571 }
572
573 if ($in_struct)
574 {
575 die
576 "$infile:$in_struct_lineno: could not find closing brace for struct \"$in_struct\"\n";
577 }
578
579 close $ifh;
580 } # for each file
581
582
583 ## write output
584
585 my $tmpext = ".tmp$$";
586
587 # opening boilerplate for output files
588 my $header_comment =
589 '/*-------------------------------------------------------------------------
590 *
591 * %s
592 * Generated node infrastructure code
593 *
594 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
595 * Portions Copyright (c) 1994, Regents of the University of California
596 *
597 * NOTES
598 * ******************************
599 * *** DO NOT EDIT THIS FILE! ***
600 * ******************************
601 *
602 * It has been GENERATED by src/backend/nodes/gen_node_support.pl
603 *
604 *-------------------------------------------------------------------------
605 */
606 ';
607
608
609 # nodetags.h
610
611 push @output_files, 'nodetags.h';
612 open my $nt, '>', "$output_path/nodetags.h$tmpext"
613 or die "$output_path/nodetags.h$tmpext: $!";
614
615 printf $nt $header_comment, 'nodetags.h';
616
617 my $tagno = 0;
618 my $last_tag = undef;
619 foreach my $n (@node_types, @extra_tags)
620 {
621 next if elem $n, @abstract_types;
622 if (defined $manual_nodetag_number{$n})
623 {
624 # do not change $tagno or $last_tag
625 print $nt "\tT_${n} = $manual_nodetag_number{$n},\n";
626 }
627 else
628 {
629 $tagno++;
630 $last_tag = $n;
631 print $nt "\tT_${n} = $tagno,\n";
632 }
633 }
634
635 # verify that last auto-assigned nodetag stays stable
636 die "ABI stability break: last nodetag is $last_tag not $last_nodetag\n"
637 if (defined $last_nodetag && $last_nodetag ne $last_tag);
638 die
639 "ABI stability break: last nodetag number is $tagno not $last_nodetag_no\n"
640 if (defined $last_nodetag_no && $last_nodetag_no != $tagno);
641
642 close $nt;
643
644
645 # make #include lines necessary to pull in all the struct definitions
646 my $node_includes = '';
647 foreach my $infile (sort @ARGV)
648 {
649 $infile =~ s!.*src/include/!!;
650 $node_includes .= qq{#include "$infile"\n};
651 }
652
653
654 # copyfuncs.c, equalfuncs.c
655
656 push @output_files, 'copyfuncs.funcs.c';
657 open my $cff, '>', "$output_path/copyfuncs.funcs.c$tmpext" or die $!;
658 push @output_files, 'equalfuncs.funcs.c';
659 open my $eff, '>', "$output_path/equalfuncs.funcs.c$tmpext" or die $!;
660 push @output_files, 'copyfuncs.switch.c';
661 open my $cfs, '>', "$output_path/copyfuncs.switch.c$tmpext" or die $!;
662 push @output_files, 'equalfuncs.switch.c';
663 open my $efs, '>', "$output_path/equalfuncs.switch.c$tmpext" or die $!;
664
665 printf $cff $header_comment, 'copyfuncs.funcs.c';
666 printf $eff $header_comment, 'equalfuncs.funcs.c';
667 printf $cfs $header_comment, 'copyfuncs.switch.c';
668 printf $efs $header_comment, 'equalfuncs.switch.c';
669
670 # add required #include lines to each file set
671 print $cff $node_includes;
672 print $eff $node_includes;
673
674 foreach my $n (@node_types)
675 {
676 next if elem $n, @abstract_types;
677 next if elem $n, @nodetag_only;
678 my $struct_no_copy = (elem $n, @no_copy);
679 my $struct_no_equal = (elem $n, @no_equal);
680 next if $struct_no_copy && $struct_no_equal;
681
682 print $cfs "\t\tcase T_${n}:\n"
683 . "\t\t\tretval = _copy${n}(from);\n"
684 . "\t\t\tbreak;\n"
685 unless $struct_no_copy;
686
687 print $efs "\t\tcase T_${n}:\n"
688 . "\t\t\tretval = _equal${n}(a, b);\n"
689 . "\t\t\tbreak;\n"
690 unless $struct_no_equal;
691
692 next if elem $n, @custom_copy_equal;
693
694 print $cff "
695 static $n *
696 _copy${n}(const $n *from)
697 {
698 \t${n} *newnode = makeNode($n);
699
700 " unless $struct_no_copy;
701
702 print $eff "
703 static bool
704 _equal${n}(const $n *a, const $n *b)
705 {
706 " unless $struct_no_equal;
707
708 # track already-processed fields to support field order checks
709 my %previous_fields;
710
711 # print instructions for each field
712 foreach my $f (@{ $node_type_info{$n}->{fields} })
713 {
714 my $t = $node_type_info{$n}->{field_types}{$f};
715 my @a = @{ $node_type_info{$n}->{field_attrs}{$f} };
716 my $copy_ignore = $struct_no_copy;
717 my $equal_ignore = $struct_no_equal;
718
719 # extract per-field attributes
720 my $array_size_field;
721 my $copy_as_field;
722 my $copy_as_scalar = 0;
723 my $equal_as_scalar = 0;
724 foreach my $a (@a)
725 {
726 if ($a =~ /^array_size\(([\w.]+)\)$/)
727 {
728 $array_size_field = $1;
729 # insist that we copy or compare the array size first!
730 die
731 "array size field $array_size_field for field $n.$f must precede $f\n"
732 if (!$previous_fields{$array_size_field});
733 }
734 elsif ($a =~ /^copy_as\(([\w.]+)\)$/)
735 {
736 $copy_as_field = $1;
737 }
738 elsif ($a eq 'copy_as_scalar')
739 {
740 $copy_as_scalar = 1;
741 }
742 elsif ($a eq 'equal_as_scalar')
743 {
744 $equal_as_scalar = 1;
745 }
746 elsif ($a eq 'equal_ignore')
747 {
748 $equal_ignore = 1;
749 }
750 }
751
752 # override type-specific copy method if requested
753 if (defined $copy_as_field)
754 {
755 print $cff "\tnewnode->$f = $copy_as_field;\n"
756 unless $copy_ignore;
757 $copy_ignore = 1;
758 }
759 elsif ($copy_as_scalar)
760 {
761 print $cff "\tCOPY_SCALAR_FIELD($f);\n"
762 unless $copy_ignore;
763 $copy_ignore = 1;
764 }
765
766 # override type-specific equal method if requested
767 if ($equal_as_scalar)
768 {
769 print $eff "\tCOMPARE_SCALAR_FIELD($f);\n"
770 unless $equal_ignore;
771 $equal_ignore = 1;
772 }
773
774 # select instructions by field type
775 if ($t eq 'char*')
776 {
777 print $cff "\tCOPY_STRING_FIELD($f);\n" unless $copy_ignore;
778 print $eff "\tCOMPARE_STRING_FIELD($f);\n" unless $equal_ignore;
779 }
780 elsif ($t eq 'Bitmapset*' || $t eq 'Relids')
781 {
782 print $cff "\tCOPY_BITMAPSET_FIELD($f);\n" unless $copy_ignore;
783 print $eff "\tCOMPARE_BITMAPSET_FIELD($f);\n"
784 unless $equal_ignore;
785 }
786 elsif ($t eq 'ParseLoc')
787 {
788 print $cff "\tCOPY_LOCATION_FIELD($f);\n" unless $copy_ignore;
789 print $eff "\tCOMPARE_LOCATION_FIELD($f);\n" unless $equal_ignore;
790 }
791 elsif (elem $t, @scalar_types or elem $t, @enum_types)
792 {
793 print $cff "\tCOPY_SCALAR_FIELD($f);\n" unless $copy_ignore;
794 if (elem 'equal_ignore_if_zero', @a)
795 {
796 print $eff
797 "\tif (a->$f != b->$f && a->$f != 0 && b->$f != 0)\n\t\treturn false;\n";
798 }
799 else
800 {
801 # All CoercionForm fields are treated as equal_ignore
802 print $eff "\tCOMPARE_SCALAR_FIELD($f);\n"
803 unless $equal_ignore || $t eq 'CoercionForm';
804 }
805 }
806 # arrays of scalar types
807 elsif ($t =~ /^(\w+)\*$/ and elem $1, @scalar_types)
808 {
809 my $tt = $1;
810 if (!defined $array_size_field)
811 {
812 die "no array size defined for $n.$f of type $t\n";
813 }
814 if ($node_type_info{$n}->{field_types}{$array_size_field} eq
815 'List*')
816 {
817 print $cff
818 "\tCOPY_POINTER_FIELD($f, list_length(from->$array_size_field) * sizeof($tt));\n"
819 unless $copy_ignore;
820 print $eff
821 "\tCOMPARE_POINTER_FIELD($f, list_length(a->$array_size_field) * sizeof($tt));\n"
822 unless $equal_ignore;
823 }
824 else
825 {
826 print $cff
827 "\tCOPY_POINTER_FIELD($f, from->$array_size_field * sizeof($tt));\n"
828 unless $copy_ignore;
829 print $eff
830 "\tCOMPARE_POINTER_FIELD($f, a->$array_size_field * sizeof($tt));\n"
831 unless $equal_ignore;
832 }
833 }
834 elsif ($t eq 'function pointer')
835 {
836 # we can copy and compare as a scalar
837 print $cff "\tCOPY_SCALAR_FIELD($f);\n" unless $copy_ignore;
838 print $eff "\tCOMPARE_SCALAR_FIELD($f);\n" unless $equal_ignore;
839 }
840 # node type
841 elsif (($t =~ /^(\w+)\*$/ or $t =~ /^struct\s+(\w+)\*$/)
842 and elem $1, @node_types)
843 {
844 die
845 "node type \"$1\" lacks copy support, which is required for struct \"$n\" field \"$f\"\n"
846 if (elem $1, @no_copy or elem $1, @nodetag_only)
847 and $1 ne 'List'
848 and !$copy_ignore;
849 die
850 "node type \"$1\" lacks equal support, which is required for struct \"$n\" field \"$f\"\n"
851 if (elem $1, @no_equal or elem $1, @nodetag_only)
852 and $1 ne 'List'
853 and !$equal_ignore;
854
855 print $cff "\tCOPY_NODE_FIELD($f);\n" unless $copy_ignore;
856 print $eff "\tCOMPARE_NODE_FIELD($f);\n" unless $equal_ignore;
857 }
858 # array (inline)
859 elsif ($t =~ /^\w+\[\w+\]$/)
860 {
861 print $cff "\tCOPY_ARRAY_FIELD($f);\n" unless $copy_ignore;
862 print $eff "\tCOMPARE_ARRAY_FIELD($f);\n" unless $equal_ignore;
863 }
864 elsif ($t eq 'struct CustomPathMethods*'
865 || $t eq 'struct CustomScanMethods*')
866 {
867 # Fields of these types are required to be a pointer to a
868 # static table of callback functions. So we don't copy
869 # the table itself, just reference the original one.
870 print $cff "\tCOPY_SCALAR_FIELD($f);\n" unless $copy_ignore;
871 print $eff "\tCOMPARE_SCALAR_FIELD($f);\n" unless $equal_ignore;
872 }
873 else
874 {
875 die
876 "could not handle type \"$t\" in struct \"$n\" field \"$f\"\n";
877 }
878
879 $previous_fields{$f} = 1;
880 }
881
882 print $cff "
883 \treturn newnode;
884 }
885 " unless $struct_no_copy;
886 print $eff "
887 \treturn true;
888 }
889 " unless $struct_no_equal;
890 }
891
892 close $cff;
893 close $eff;
894 close $cfs;
895 close $efs;
896
897
898 # outfuncs.c, readfuncs.c
899
900 push @output_files, 'outfuncs.funcs.c';
901 open my $off, '>', "$output_path/outfuncs.funcs.c$tmpext" or die $!;
902 push @output_files, 'readfuncs.funcs.c';
903 open my $rff, '>', "$output_path/readfuncs.funcs.c$tmpext" or die $!;
904 push @output_files, 'outfuncs.switch.c';
905 open my $ofs, '>', "$output_path/outfuncs.switch.c$tmpext" or die $!;
906 push @output_files, 'readfuncs.switch.c';
907 open my $rfs, '>', "$output_path/readfuncs.switch.c$tmpext" or die $!;
908
909 printf $off $header_comment, 'outfuncs.funcs.c';
910 printf $rff $header_comment, 'readfuncs.funcs.c';
911 printf $ofs $header_comment, 'outfuncs.switch.c';
912 printf $rfs $header_comment, 'readfuncs.switch.c';
913
914 print $off $node_includes;
915 print $rff $node_includes;
916
917 foreach my $n (@node_types)
918 {
919 next if elem $n, @abstract_types;
920 next if elem $n, @nodetag_only;
921 next if elem $n, @no_read_write;
922 next if elem $n, @special_read_write;
923
924 my $no_read = (elem $n, @no_read);
925
926 # output format starts with upper case node type name
927 my $N = uc $n;
928
929 print $ofs "\t\t\tcase T_${n}:\n"
930 . "\t\t\t\t_out${n}(str, obj);\n"
931 . "\t\t\t\tbreak;\n";
932
933 print $rfs "\tif (MATCH(\"$N\", "
934 . length($N) . "))\n"
935 . "\t\treturn (Node *) _read${n}();\n"
936 unless $no_read;
937
938 next if elem $n, @custom_read_write;
939
940 print $off "
941 static void
942 _out${n}(StringInfo str, const $n *node)
943 {
944 \tWRITE_NODE_TYPE(\"$N\");
945
946 ";
947
948 if (!$no_read)
949 {
950 my $macro =
951 (@{ $node_type_info{$n}->{fields} } > 0)
952 ? 'READ_LOCALS'
953 : 'READ_LOCALS_NO_FIELDS';
954 print $rff "
955 static $n *
956 _read${n}(void)
957 {
958 \t$macro($n);
959
960 ";
961 }
962
963 # track already-processed fields to support field order checks
964 # (this isn't quite redundant with the previous loop, since
965 # we may be considering structs that lack copy/equal support)
966 my %previous_fields;
967
968 # print instructions for each field
969 foreach my $f (@{ $node_type_info{$n}->{fields} })
970 {
971 my $t = $node_type_info{$n}->{field_types}{$f};
972 my @a = @{ $node_type_info{$n}->{field_attrs}{$f} };
973
974 # extract per-field attributes
975 my $array_size_field;
976 my $read_as_field;
977 my $read_write_ignore = 0;
978 foreach my $a (@a)
979 {
980 if ($a =~ /^array_size\(([\w.]+)\)$/)
981 {
982 $array_size_field = $1;
983 # insist that we read the array size first!
984 die
985 "array size field $array_size_field for field $n.$f must precede $f\n"
986 if (!$previous_fields{$array_size_field} && !$no_read);
987 }
988 elsif ($a =~ /^read_as\(([\w.]+)\)$/)
989 {
990 $read_as_field = $1;
991 }
992 elsif ($a eq 'read_write_ignore')
993 {
994 $read_write_ignore = 1;
995 }
996 }
997
998 if ($read_write_ignore)
999 {
1000 # nothing to do if no_read
1001 next if $no_read;
1002 # for read_write_ignore with read_as(), emit the appropriate
1003 # assignment on the read side and move on.
1004 if (defined $read_as_field)
1005 {
1006 print $rff "\tlocal_node->$f = $read_as_field;\n";
1007 next;
1008 }
1009 # else, bad specification
1010 die "$n.$f must not be marked read_write_ignore\n";
1011 }
1012
1013 # select instructions by field type
1014 if ($t eq 'bool')
1015 {
1016 print $off "\tWRITE_BOOL_FIELD($f);\n";
1017 print $rff "\tREAD_BOOL_FIELD($f);\n" unless $no_read;
1018 }
1019 elsif ($t eq 'ParseLoc')
1020 {
1021 print $off "\tWRITE_LOCATION_FIELD($f);\n";
1022 print $rff "\tREAD_LOCATION_FIELD($f);\n" unless $no_read;
1023 }
1024 elsif ($t eq 'int'
1025 || $t eq 'int16'
1026 || $t eq 'int32'
1027 || $t eq 'AttrNumber'
1028 || $t eq 'StrategyNumber')
1029 {
1030 print $off "\tWRITE_INT_FIELD($f);\n";
1031 print $rff "\tREAD_INT_FIELD($f);\n" unless $no_read;
1032 }
1033 elsif ($t eq 'uint32'
1034 || $t eq 'bits32'
1035 || $t eq 'BlockNumber'
1036 || $t eq 'Index'
1037 || $t eq 'SubTransactionId')
1038 {
1039 print $off "\tWRITE_UINT_FIELD($f);\n";
1040 print $rff "\tREAD_UINT_FIELD($f);\n" unless $no_read;
1041 }
1042 elsif ($t eq 'int64')
1043 {
1044 print $off "\tWRITE_INT64_FIELD($f);\n";
1045 print $rff "\tREAD_INT64_FIELD($f);\n" unless $no_read;
1046 }
1047 elsif ($t eq 'uint64'
1048 || $t eq 'AclMode')
1049 {
1050 print $off "\tWRITE_UINT64_FIELD($f);\n";
1051 print $rff "\tREAD_UINT64_FIELD($f);\n" unless $no_read;
1052 }
1053 elsif ($t eq 'Oid' || $t eq 'RelFileNumber')
1054 {
1055 print $off "\tWRITE_OID_FIELD($f);\n";
1056 print $rff "\tREAD_OID_FIELD($f);\n" unless $no_read;
1057 }
1058 elsif ($t eq 'long')
1059 {
1060 print $off "\tWRITE_LONG_FIELD($f);\n";
1061 print $rff "\tREAD_LONG_FIELD($f);\n" unless $no_read;
1062 }
1063 elsif ($t eq 'char')
1064 {
1065 print $off "\tWRITE_CHAR_FIELD($f);\n";
1066 print $rff "\tREAD_CHAR_FIELD($f);\n" unless $no_read;
1067 }
1068 elsif ($t eq 'double')
1069 {
1070 print $off "\tWRITE_FLOAT_FIELD($f);\n";
1071 print $rff "\tREAD_FLOAT_FIELD($f);\n" unless $no_read;
1072 }
1073 elsif ($t eq 'Cardinality')
1074 {
1075 print $off "\tWRITE_FLOAT_FIELD($f);\n";
1076 print $rff "\tREAD_FLOAT_FIELD($f);\n" unless $no_read;
1077 }
1078 elsif ($t eq 'Cost')
1079 {
1080 print $off "\tWRITE_FLOAT_FIELD($f);\n";
1081 print $rff "\tREAD_FLOAT_FIELD($f);\n" unless $no_read;
1082 }
1083 elsif ($t eq 'QualCost')
1084 {
1085 print $off "\tWRITE_FLOAT_FIELD($f.startup);\n";
1086 print $off "\tWRITE_FLOAT_FIELD($f.per_tuple);\n";
1087 print $rff "\tREAD_FLOAT_FIELD($f.startup);\n" unless $no_read;
1088 print $rff "\tREAD_FLOAT_FIELD($f.per_tuple);\n" unless $no_read;
1089 }
1090 elsif ($t eq 'Selectivity')
1091 {
1092 print $off "\tWRITE_FLOAT_FIELD($f);\n";
1093 print $rff "\tREAD_FLOAT_FIELD($f);\n" unless $no_read;
1094 }
1095 elsif ($t eq 'char*')
1096 {
1097 print $off "\tWRITE_STRING_FIELD($f);\n";
1098 print $rff "\tREAD_STRING_FIELD($f);\n" unless $no_read;
1099 }
1100 elsif ($t eq 'Bitmapset*' || $t eq 'Relids')
1101 {
1102 print $off "\tWRITE_BITMAPSET_FIELD($f);\n";
1103 print $rff "\tREAD_BITMAPSET_FIELD($f);\n" unless $no_read;
1104 }
1105 elsif (elem $t, @enum_types)
1106 {
1107 print $off "\tWRITE_ENUM_FIELD($f, $t);\n";
1108 print $rff "\tREAD_ENUM_FIELD($f, $t);\n" unless $no_read;
1109 }
1110 # arrays of scalar types
1111 elsif ($t =~ /^(\w+)(\*|\[\w+\])$/ and elem $1, @scalar_types)
1112 {
1113 my $tt = uc $1;
1114 if (!defined $array_size_field)
1115 {
1116 die "no array size defined for $n.$f of type $t\n";
1117 }
1118 if ($node_type_info{$n}->{field_types}{$array_size_field} eq
1119 'List*')
1120 {
1121 print $off
1122 "\tWRITE_${tt}_ARRAY($f, list_length(node->$array_size_field));\n";
1123 print $rff
1124 "\tREAD_${tt}_ARRAY($f, list_length(local_node->$array_size_field));\n"
1125 unless $no_read;
1126 }
1127 else
1128 {
1129 print $off
1130 "\tWRITE_${tt}_ARRAY($f, node->$array_size_field);\n";
1131 print $rff
1132 "\tREAD_${tt}_ARRAY($f, local_node->$array_size_field);\n"
1133 unless $no_read;
1134 }
1135 }
1136 elsif ($t eq 'function pointer')
1137 {
1138 # We don't print these, and we can't read them either
1139 die "cannot read function pointer in struct \"$n\" field \"$f\"\n"
1140 unless $no_read;
1141 }
1142 # Special treatments of several Path node fields
1143 elsif ($t eq 'RelOptInfo*' && elem 'write_only_relids', @a)
1144 {
1145 print $off
1146 "\tappendStringInfoString(str, \" :parent_relids \");\n"
1147 . "\toutBitmapset(str, node->$f->relids);\n";
1148 }
1149 elsif ($t eq 'PathTarget*' && elem 'write_only_nondefault_pathtarget',
1150 @a)
1151 {
1152 (my $f2 = $f) =~ s/pathtarget/parent/;
1153 print $off "\tif (node->$f != node->$f2->reltarget)\n"
1154 . "\t\tWRITE_NODE_FIELD($f);\n";
1155 }
1156 elsif ($t eq 'ParamPathInfo*' && elem 'write_only_req_outer', @a)
1157 {
1158 print $off
1159 "\tappendStringInfoString(str, \" :required_outer \");\n"
1160 . "\tif (node->$f)\n"
1161 . "\t\toutBitmapset(str, node->$f->ppi_req_outer);\n"
1162 . "\telse\n"
1163 . "\t\toutBitmapset(str, NULL);\n";
1164 }
1165 # node type
1166 elsif (($t =~ /^(\w+)\*$/ or $t =~ /^struct\s+(\w+)\*$/)
1167 and elem $1, @node_types)
1168 {
1169 die
1170 "node type \"$1\" lacks write support, which is required for struct \"$n\" field \"$f\"\n"
1171 if (elem $1, @no_read_write or elem $1, @nodetag_only);
1172 die
1173 "node type \"$1\" lacks read support, which is required for struct \"$n\" field \"$f\"\n"
1174 if (elem $1, @no_read or elem $1, @nodetag_only)
1175 and !$no_read;
1176
1177 print $off "\tWRITE_NODE_FIELD($f);\n";
1178 print $rff "\tREAD_NODE_FIELD($f);\n" unless $no_read;
1179 }
1180 # arrays of node pointers (currently supported for write only)
1181 elsif (($t =~ /^(\w+)\*\*$/ or $t =~ /^struct\s+(\w+)\*\*$/)
1182 and elem($1, @node_types))
1183 {
1184 if (!defined $array_size_field)
1185 {
1186 die "no array size defined for $n.$f of type $t\n";
1187 }
1188 if ($node_type_info{$n}->{field_types}{$array_size_field} eq
1189 'List*')
1190 {
1191 print $off
1192 "\tWRITE_NODE_ARRAY($f, list_length(node->$array_size_field));\n";
1193 print $rff
1194 "\tREAD_NODE_ARRAY($f, list_length(local_node->$array_size_field));\n"
1195 unless $no_read;
1196 }
1197 else
1198 {
1199 print $off
1200 "\tWRITE_NODE_ARRAY($f, node->$array_size_field);\n";
1201 print $rff
1202 "\tREAD_NODE_ARRAY($f, local_node->$array_size_field);\n"
1203 unless $no_read;
1204 }
1205 }
1206 elsif ($t eq 'struct CustomPathMethods*'
1207 || $t eq 'struct CustomScanMethods*')
1208 {
1209 print $off q{
1210 /* CustomName is a key to lookup CustomScanMethods */
1211 appendStringInfoString(str, " :methods ");
1212 outToken(str, node->methods->CustomName);
1213 };
1214 print $rff q!
1215 {
1216 /* Lookup CustomScanMethods by CustomName */
1217 char *custom_name;
1218 const CustomScanMethods *methods;
1219 token = pg_strtok(&length); /* skip methods: */
1220 token = pg_strtok(&length); /* CustomName */
1221 custom_name = nullable_string(token, length);
1222 methods = GetCustomScanMethods(custom_name, false);
1223 local_node->methods = methods;
1224 }
1225 ! unless $no_read;
1226 }
1227 else
1228 {
1229 die
1230 "could not handle type \"$t\" in struct \"$n\" field \"$f\"\n";
1231 }
1232
1233 # for read_as() without read_write_ignore, we have to read the value
1234 # that outfuncs.c wrote and then overwrite it.
1235 if (defined $read_as_field)
1236 {
1237 print $rff "\tlocal_node->$f = $read_as_field;\n" unless $no_read;
1238 }
1239
1240 $previous_fields{$f} = 1;
1241 }
1242
1243 print $off "}
1244 ";
1245 print $rff "
1246 \tREAD_DONE();
1247 }
1248 " unless $no_read;
1249 }
1250
1251 close $off;
1252 close $rff;
1253 close $ofs;
1254 close $rfs;
1255
1256
1257 # queryjumblefuncs.c
1258
1259 push @output_files, 'queryjumblefuncs.funcs.c';
1260 open my $jff, '>', "$output_path/queryjumblefuncs.funcs.c$tmpext" or die $!;
1261 push @output_files, 'queryjumblefuncs.switch.c';
1262 open my $jfs, '>', "$output_path/queryjumblefuncs.switch.c$tmpext" or die $!;
1263
1264 printf $jff $header_comment, 'queryjumblefuncs.funcs.c';
1265 printf $jfs $header_comment, 'queryjumblefuncs.switch.c';
1266
1267 print $jff $node_includes;
1268
1269 foreach my $n (@node_types)
1270 {
1271 next if elem $n, @abstract_types;
1272 next if elem $n, @nodetag_only;
1273 my $struct_no_query_jumble = (elem $n, @no_query_jumble);
1274
1275 print $jfs "\t\t\tcase T_${n}:\n"
1276 . "\t\t\t\t_jumble${n}(jstate, expr);\n"
1277 . "\t\t\t\tbreak;\n"
1278 unless $struct_no_query_jumble;
1279
1280 next if elem $n, @custom_query_jumble;
1281
1282 print $jff "
1283 static void
1284 _jumble${n}(JumbleState *jstate, Node *node)
1285 {
1286 \t${n} *expr = (${n} *) node;\n
1287 " unless $struct_no_query_jumble;
1288
1289 # print instructions for each field
1290 foreach my $f (@{ $node_type_info{$n}->{fields} })
1291 {
1292 my $t = $node_type_info{$n}->{field_types}{$f};
1293 my @a = @{ $node_type_info{$n}->{field_attrs}{$f} };
1294 my $query_jumble_ignore = $struct_no_query_jumble;
1295 my $query_jumble_custom = 0;
1296 my $query_jumble_location = 0;
1297 my $query_jumble_squash = 0;
1298
1299 # extract per-field attributes
1300 foreach my $a (@a)
1301 {
1302 if ($a eq 'custom_query_jumble')
1303 {
1304 $query_jumble_custom = 1;
1305 }
1306 if ($a eq 'query_jumble_ignore')
1307 {
1308 $query_jumble_ignore = 1;
1309 }
1310 elsif ($a eq 'query_jumble_location')
1311 {
1312 $query_jumble_location = 1;
1313 }
1314 elsif ($a eq 'query_jumble_squash')
1315 {
1316 $query_jumble_squash = 1;
1317 }
1318 }
1319
1320 if ($query_jumble_custom)
1321 {
1322 # Custom function that applies to one field of a node.
1323 print $jff "\tJUMBLE_CUSTOM($n, $f);\n"
1324 unless $query_jumble_ignore;
1325 }
1326 elsif (($t =~ /^(\w+)\*$/ or $t =~ /^struct\s+(\w+)\*$/)
1327 and elem $1, @node_types)
1328 {
1329 # Node type. Squash constants if requested.
1330 if ($query_jumble_squash)
1331 {
1332 print $jff "\tJUMBLE_ELEMENTS($f, node);\n"
1333 unless $query_jumble_ignore;
1334 }
1335 else
1336 {
1337 print $jff "\tJUMBLE_NODE($f);\n"
1338 unless $query_jumble_ignore;
1339 }
1340 }
1341 elsif ($t eq 'ParseLoc')
1342 {
1343 # Track the node's location only if directly requested.
1344 if ($query_jumble_location)
1345 {
1346 print $jff "\tJUMBLE_LOCATION($f);\n"
1347 unless $query_jumble_ignore;
1348 }
1349 }
1350 elsif ($t eq 'char*')
1351 {
1352 print $jff "\tJUMBLE_STRING($f);\n"
1353 unless $query_jumble_ignore;
1354 }
1355 else
1356 {
1357 print $jff "\tJUMBLE_FIELD($f);\n"
1358 unless $query_jumble_ignore;
1359 }
1360 }
1361
1362 # Some nodes have no attributes like CheckPointStmt,
1363 # so tweak things for empty contents.
1364 if (scalar(@{ $node_type_info{$n}->{fields} }) == 0)
1365 {
1366 print $jff "\t(void) expr;\n"
1367 unless $struct_no_query_jumble;
1368 }
1369
1370 print $jff "}
1371 " unless $struct_no_query_jumble;
1372 }
1373
1374 close $jff;
1375 close $jfs;
1376
1377 # now rename the temporary files to their final names
1378 foreach my $file (@output_files)
1379 {
1380 Catalog::RenameTempFile("$output_path/$file", $tmpext);
1381 }
1382
1383
1384 # Automatically clean up any temp files if the script fails.
1385 END
1386 {
1387 # take care not to change the script's exit value
1388 my $exit_code = $?;
1389
1390 if ($exit_code != 0)
1391 {
1392 foreach my $file (@output_files)
1393 {
1394 unlink("$output_path/$file$tmpext");
1395 }
1396 }
1397
1398 $? = $exit_code;
1399 }