1 /* Print GENERIC declaration (functions, variables, types) trees coming from
2 the C and C++ front-ends as well as macros in Ada syntax.
3 Copyright (C) 2010-2016 Free Software Foundation, Inc.
4 Adapted from tree-pretty-print.c by Arnaud Charlet <charlet@adacore.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
27 #include "c-ada-spec.h"
28 #include "fold-const.h"
30 #include "cpp-id-data.h"
32 /* Local functions, macros and variables. */
33 static int dump_generic_ada_node (pretty_printer
*, tree
, tree
, int, int,
35 static int print_ada_declaration (pretty_printer
*, tree
, tree
, int);
36 static void print_ada_struct_decl (pretty_printer
*, tree
, tree
, int, bool);
37 static void dump_sloc (pretty_printer
*buffer
, tree node
);
38 static void print_comment (pretty_printer
*, const char *);
39 static void print_generic_ada_decl (pretty_printer
*, tree
, const char *);
40 static char *get_ada_package (const char *);
41 static void dump_ada_nodes (pretty_printer
*, const char *);
42 static void reset_ada_withs (void);
43 static void dump_ada_withs (FILE *);
44 static void dump_ads (const char *, void (*)(const char *),
45 int (*)(tree
, cpp_operation
));
46 static char *to_ada_name (const char *, int *);
47 static bool separate_class_package (tree
);
49 #define INDENT(SPACE) \
50 do { int i; for (i = 0; i<SPACE; i++) pp_space (buffer); } while (0)
54 /* Global hook used to perform C++ queries on nodes. */
55 static int (*cpp_check
) (tree
, cpp_operation
) = NULL
;
58 /* Given a cpp MACRO, compute the max length BUFFER_LEN of the macro, as well
59 as max length PARAM_LEN of arguments for fun_like macros, and also set
60 SUPPORTED to 0 if the macro cannot be mapped to an Ada construct. */
63 macro_length (const cpp_macro
*macro
, int *supported
, int *buffer_len
,
76 for (i
= 0; i
< macro
->paramc
; i
++)
78 cpp_hashnode
*param
= macro
->params
[i
];
80 *param_len
+= NODE_LEN (param
);
82 if (i
+ 1 < macro
->paramc
)
84 *param_len
+= 2; /* ", " */
86 else if (macro
->variadic
)
92 *param_len
+= 2; /* ")\0" */
95 for (j
= 0; j
< macro
->count
; j
++)
97 cpp_token
*token
= ¯o
->exp
.tokens
[j
];
99 if (token
->flags
& PREV_WHITE
)
102 if (token
->flags
& STRINGIFY_ARG
|| token
->flags
& PASTE_LEFT
)
108 if (token
->type
== CPP_MACRO_ARG
)
110 NODE_LEN (macro
->params
[token
->val
.macro_arg
.arg_no
- 1]);
112 /* Include enough extra space to handle e.g. special characters. */
113 *buffer_len
+= (cpp_token_len (token
) + 1) * 8;
119 /* Dump all digits/hex chars from NUMBER to BUFFER and return a pointer
120 to the character after the last character written. */
122 static unsigned char *
123 dump_number (unsigned char *number
, unsigned char *buffer
)
125 while (*number
!= '\0'
130 *buffer
++ = *number
++;
135 /* Handle escape character C and convert to an Ada character into BUFFER.
136 Return a pointer to the character after the last character written, or
137 NULL if the escape character is not supported. */
139 static unsigned char *
140 handle_escape_character (unsigned char *buffer
, char c
)
150 strcpy ((char *) buffer
, "\" & ASCII.LF & \"");
155 strcpy ((char *) buffer
, "\" & ASCII.CR & \"");
160 strcpy ((char *) buffer
, "\" & ASCII.HT & \"");
171 /* Dump into PP a set of MAX_ADA_MACROS MACROS (C/C++) as Ada constants when
175 print_ada_macros (pretty_printer
*pp
, cpp_hashnode
**macros
, int max_ada_macros
)
177 int j
, num_macros
= 0, prev_line
= -1;
179 for (j
= 0; j
< max_ada_macros
; j
++)
181 cpp_hashnode
*node
= macros
[j
];
182 const cpp_macro
*macro
= node
->value
.macro
;
184 int supported
= 1, prev_is_one
= 0, buffer_len
, param_len
;
185 int is_string
= 0, is_char
= 0;
187 unsigned char *s
, *params
, *buffer
, *buf_param
, *char_one
= NULL
, *tmp
;
189 macro_length (macro
, &supported
, &buffer_len
, ¶m_len
);
190 s
= buffer
= XALLOCAVEC (unsigned char, buffer_len
);
191 params
= buf_param
= XALLOCAVEC (unsigned char, param_len
);
198 for (i
= 0; i
< macro
->paramc
; i
++)
200 cpp_hashnode
*param
= macro
->params
[i
];
202 memcpy (buf_param
, NODE_NAME (param
), NODE_LEN (param
));
203 buf_param
+= NODE_LEN (param
);
205 if (i
+ 1 < macro
->paramc
)
210 else if (macro
->variadic
)
220 for (i
= 0; supported
&& i
< macro
->count
; i
++)
222 cpp_token
*token
= ¯o
->exp
.tokens
[i
];
225 if (token
->flags
& PREV_WHITE
)
228 if (token
->flags
& STRINGIFY_ARG
|| token
->flags
& PASTE_LEFT
)
238 cpp_hashnode
*param
=
239 macro
->params
[token
->val
.macro_arg
.arg_no
- 1];
240 memcpy (buffer
, NODE_NAME (param
), NODE_LEN (param
));
241 buffer
+= NODE_LEN (param
);
245 case CPP_EQ_EQ
: *buffer
++ = '='; break;
246 case CPP_GREATER
: *buffer
++ = '>'; break;
247 case CPP_LESS
: *buffer
++ = '<'; break;
248 case CPP_PLUS
: *buffer
++ = '+'; break;
249 case CPP_MINUS
: *buffer
++ = '-'; break;
250 case CPP_MULT
: *buffer
++ = '*'; break;
251 case CPP_DIV
: *buffer
++ = '/'; break;
252 case CPP_COMMA
: *buffer
++ = ','; break;
253 case CPP_OPEN_SQUARE
:
254 case CPP_OPEN_PAREN
: *buffer
++ = '('; break;
255 case CPP_CLOSE_SQUARE
: /* fallthrough */
256 case CPP_CLOSE_PAREN
: *buffer
++ = ')'; break;
257 case CPP_DEREF
: /* fallthrough */
258 case CPP_SCOPE
: /* fallthrough */
259 case CPP_DOT
: *buffer
++ = '.'; break;
261 case CPP_EQ
: *buffer
++ = ':'; *buffer
++ = '='; break;
262 case CPP_NOT_EQ
: *buffer
++ = '/'; *buffer
++ = '='; break;
263 case CPP_GREATER_EQ
: *buffer
++ = '>'; *buffer
++ = '='; break;
264 case CPP_LESS_EQ
: *buffer
++ = '<'; *buffer
++ = '='; break;
267 *buffer
++ = 'n'; *buffer
++ = 'o'; *buffer
++ = 't'; break;
269 *buffer
++ = 'm'; *buffer
++ = 'o'; *buffer
++ = 'd'; break;
271 *buffer
++ = 'a'; *buffer
++ = 'n'; *buffer
++ = 'd'; break;
273 *buffer
++ = 'o'; *buffer
++ = 'r'; break;
275 *buffer
++ = 'x'; *buffer
++ = 'o'; *buffer
++ = 'r'; break;
277 strcpy ((char *) buffer
, " and then ");
281 strcpy ((char *) buffer
, " or else ");
287 is_one
= prev_is_one
;
290 case CPP_COMMENT
: break;
301 if (!macro
->fun_like
)
304 buffer
= cpp_spell_token (parse_in
, token
, buffer
, false);
310 const unsigned char *s
= token
->val
.str
.text
;
316 buffer
= handle_escape_character (buffer
, *s
);
335 c
= cpp_interpret_charconst (parse_in
, token
,
336 &chars_seen
, &ignored
);
337 if (c
>= 32 && c
<= 126)
340 *buffer
++ = (char) c
;
346 ((char *) buffer
, "Character'Val (%d)", (int) c
);
347 buffer
+= chars_seen
;
353 tmp
= cpp_token_as_text (parse_in
, token
);
373 buffer
= dump_number (tmp
+ 2, buffer
);
381 buffer
= dump_number (tmp
+ 2, buffer
);
386 /* Dump floating constants unmodified. */
387 if (strchr ((const char *)tmp
, '.'))
388 buffer
= dump_number (tmp
, buffer
);
393 buffer
= dump_number (tmp
+ 1, buffer
);
401 if (tmp
[1] == '\0' || tmp
[1] == 'l' || tmp
[1] == 'u'
402 || tmp
[1] == 'L' || tmp
[1] == 'U')
409 buffer
= dump_number (tmp
, buffer
);
413 buffer
= dump_number (tmp
, buffer
);
421 /* Replace "1 << N" by "2 ** N" */
448 case CPP_CLOSE_BRACE
:
452 case CPP_MINUS_MINUS
:
456 case CPP_HEADER_NAME
:
459 case CPP_OBJC_STRING
:
461 if (!macro
->fun_like
)
464 buffer
= cpp_spell_token (parse_in
, token
, buffer
, false);
468 prev_is_one
= is_one
;
475 if (macro
->fun_like
&& supported
)
477 char *start
= (char *) s
;
480 pp_string (pp
, " -- arg-macro: ");
482 if (*start
== '(' && buffer
[-1] == ')')
487 pp_string (pp
, "function ");
491 pp_string (pp
, "procedure ");
494 pp_string (pp
, (const char *) NODE_NAME (node
));
496 pp_string (pp
, (char *) params
);
498 pp_string (pp
, " -- ");
502 pp_string (pp
, "return ");
503 pp_string (pp
, start
);
507 pp_string (pp
, start
);
513 expanded_location sloc
= expand_location (macro
->line
);
515 if (sloc
.line
!= prev_line
+ 1 && prev_line
> 0)
519 prev_line
= sloc
.line
;
522 ada_name
= to_ada_name ((const char *) NODE_NAME (node
), NULL
);
523 pp_string (pp
, ada_name
);
525 pp_string (pp
, " : ");
528 pp_string (pp
, "aliased constant String");
530 pp_string (pp
, "aliased constant Character");
532 pp_string (pp
, "constant");
534 pp_string (pp
, " := ");
535 pp_string (pp
, (char *) s
);
538 pp_string (pp
, " & ASCII.NUL");
540 pp_string (pp
, "; -- ");
541 pp_string (pp
, sloc
.file
);
543 pp_scalar (pp
, "%d", sloc
.line
);
548 pp_string (pp
, " -- unsupported macro: ");
549 pp_string (pp
, (const char *) cpp_macro_definition (parse_in
, node
));
558 static const char *source_file
;
559 static int max_ada_macros
;
561 /* Callback used to count the number of relevant macros from
562 cpp_forall_identifiers. PFILE and V are not used. NODE is the current macro
566 count_ada_macro (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*node
,
567 void *v ATTRIBUTE_UNUSED
)
569 const cpp_macro
*macro
= node
->value
.macro
;
571 if (node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
)
573 && *NODE_NAME (node
) != '_'
574 && LOCATION_FILE (macro
->line
) == source_file
)
580 static int store_ada_macro_index
;
582 /* Callback used to store relevant macros from cpp_forall_identifiers.
583 PFILE is not used. NODE is the current macro to store if relevant.
584 MACROS is an array of cpp_hashnode* used to store NODE. */
587 store_ada_macro (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
588 cpp_hashnode
*node
, void *macros
)
590 const cpp_macro
*macro
= node
->value
.macro
;
592 if (node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
)
594 && *NODE_NAME (node
) != '_'
595 && LOCATION_FILE (macro
->line
) == source_file
)
596 ((cpp_hashnode
**) macros
)[store_ada_macro_index
++] = node
;
601 /* Callback used to compare (during qsort) macros. NODE1 and NODE2 are the
602 two macro nodes to compare. */
605 compare_macro (const void *node1
, const void *node2
)
607 typedef const cpp_hashnode
*const_hnode
;
609 const_hnode n1
= *(const const_hnode
*) node1
;
610 const_hnode n2
= *(const const_hnode
*) node2
;
612 return n1
->value
.macro
->line
- n2
->value
.macro
->line
;
615 /* Dump in PP all relevant macros appearing in FILE. */
618 dump_ada_macros (pretty_printer
*pp
, const char* file
)
620 cpp_hashnode
**macros
;
622 /* Initialize file-scope variables. */
624 store_ada_macro_index
= 0;
627 /* Count all potentially relevant macros, and then sort them by sloc. */
628 cpp_forall_identifiers (parse_in
, count_ada_macro
, NULL
);
629 macros
= XALLOCAVEC (cpp_hashnode
*, max_ada_macros
);
630 cpp_forall_identifiers (parse_in
, store_ada_macro
, macros
);
631 qsort (macros
, max_ada_macros
, sizeof (cpp_hashnode
*), compare_macro
);
633 print_ada_macros (pp
, macros
, max_ada_macros
);
636 /* Current source file being handled. */
638 static const char *source_file_base
;
640 /* Return sloc of DECL, using sloc of last field if LAST is true. */
643 decl_sloc (const_tree decl
, bool last
)
647 /* Compare the declaration of struct-like types based on the sloc of their
648 last field (if LAST is true), so that more nested types collate before
650 if (TREE_CODE (decl
) == TYPE_DECL
651 && !DECL_ORIGINAL_TYPE (decl
)
652 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl
))
653 && (field
= TYPE_FIELDS (TREE_TYPE (decl
))))
656 while (DECL_CHAIN (field
))
657 field
= DECL_CHAIN (field
);
658 return DECL_SOURCE_LOCATION (field
);
661 return DECL_SOURCE_LOCATION (decl
);
664 /* Compare two locations LHS and RHS. */
667 compare_location (location_t lhs
, location_t rhs
)
669 expanded_location xlhs
= expand_location (lhs
);
670 expanded_location xrhs
= expand_location (rhs
);
672 if (xlhs
.file
!= xrhs
.file
)
673 return filename_cmp (xlhs
.file
, xrhs
.file
);
675 if (xlhs
.line
!= xrhs
.line
)
676 return xlhs
.line
- xrhs
.line
;
678 if (xlhs
.column
!= xrhs
.column
)
679 return xlhs
.column
- xrhs
.column
;
684 /* Compare two declarations (LP and RP) by their source location. */
687 compare_node (const void *lp
, const void *rp
)
689 const_tree lhs
= *((const tree
*) lp
);
690 const_tree rhs
= *((const tree
*) rp
);
692 return compare_location (decl_sloc (lhs
, true), decl_sloc (rhs
, true));
695 /* Compare two comments (LP and RP) by their source location. */
698 compare_comment (const void *lp
, const void *rp
)
700 const cpp_comment
*lhs
= (const cpp_comment
*) lp
;
701 const cpp_comment
*rhs
= (const cpp_comment
*) rp
;
703 return compare_location (lhs
->sloc
, rhs
->sloc
);
706 static tree
*to_dump
= NULL
;
707 static int to_dump_count
= 0;
709 /* Collect a list of declarations from T relevant to SOURCE_FILE to be dumped
710 by a subsequent call to dump_ada_nodes. */
713 collect_ada_nodes (tree t
, const char *source_file
)
716 int i
= to_dump_count
;
718 /* Count the likely relevant nodes: do not dump builtins (they are irrelevant
719 in the context of bindings) and namespaces (we do not handle them properly
721 for (n
= t
; n
; n
= TREE_CHAIN (n
))
722 if (!DECL_IS_BUILTIN (n
)
723 && TREE_CODE (n
) != NAMESPACE_DECL
724 && LOCATION_FILE (decl_sloc (n
, false)) == source_file
)
727 /* Allocate sufficient storage for all nodes. */
728 to_dump
= XRESIZEVEC (tree
, to_dump
, to_dump_count
);
730 /* Store the relevant nodes. */
731 for (n
= t
; n
; n
= TREE_CHAIN (n
))
732 if (!DECL_IS_BUILTIN (n
)
733 && TREE_CODE (n
) != NAMESPACE_DECL
734 && LOCATION_FILE (decl_sloc (n
, false)) == source_file
)
738 /* Call back for walk_tree to clear the TREE_VISITED flag of TP. */
741 unmark_visited_r (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
742 void *data ATTRIBUTE_UNUSED
)
744 if (TREE_VISITED (*tp
))
745 TREE_VISITED (*tp
) = 0;
752 /* Dump nodes into PP relevant to SOURCE_FILE, as collected by previous calls
753 to collect_ada_nodes. */
756 dump_ada_nodes (pretty_printer
*pp
, const char *source_file
)
759 cpp_comment_table
*comments
;
761 /* Sort the table of declarations to dump by sloc. */
762 qsort (to_dump
, to_dump_count
, sizeof (tree
), compare_node
);
764 /* Fetch the table of comments. */
765 comments
= cpp_get_comments (parse_in
);
767 /* Sort the comments table by sloc. */
768 if (comments
->count
> 1)
769 qsort (comments
->entries
, comments
->count
, sizeof (cpp_comment
),
772 /* Interleave comments and declarations in line number order. */
776 /* Advance j until comment j is in this file. */
777 while (j
!= comments
->count
778 && LOCATION_FILE (comments
->entries
[j
].sloc
) != source_file
)
781 /* Advance j until comment j is not a duplicate. */
782 while (j
< comments
->count
- 1
783 && !compare_comment (&comments
->entries
[j
],
784 &comments
->entries
[j
+ 1]))
787 /* Write decls until decl i collates after comment j. */
788 while (i
!= to_dump_count
)
790 if (j
== comments
->count
791 || LOCATION_LINE (decl_sloc (to_dump
[i
], false))
792 < LOCATION_LINE (comments
->entries
[j
].sloc
))
793 print_generic_ada_decl (pp
, to_dump
[i
++], source_file
);
798 /* Write comment j, if there is one. */
799 if (j
!= comments
->count
)
800 print_comment (pp
, comments
->entries
[j
++].comment
);
802 } while (i
!= to_dump_count
|| j
!= comments
->count
);
804 /* Clear the TREE_VISITED flag over each subtree we've dumped. */
805 for (i
= 0; i
< to_dump_count
; i
++)
806 walk_tree (&to_dump
[i
], unmark_visited_r
, NULL
, NULL
);
808 /* Finalize the to_dump table. */
817 /* Print a COMMENT to the output stream PP. */
820 print_comment (pretty_printer
*pp
, const char *comment
)
822 int len
= strlen (comment
);
823 char *str
= XALLOCAVEC (char, len
+ 1);
825 bool extra_newline
= false;
827 memcpy (str
, comment
, len
+ 1);
829 /* Trim C/C++ comment indicators. */
830 if (str
[len
- 2] == '*' && str
[len
- 1] == '/')
837 tok
= strtok (str
, "\n");
839 pp_string (pp
, " --");
842 tok
= strtok (NULL
, "\n");
844 /* Leave a blank line after multi-line comments. */
846 extra_newline
= true;
853 /* Print declaration DECL to PP in Ada syntax. The current source file being
854 handled is SOURCE_FILE. */
857 print_generic_ada_decl (pretty_printer
*pp
, tree decl
, const char *source_file
)
859 source_file_base
= source_file
;
861 if (print_ada_declaration (pp
, decl
, 0, INDENT_INCR
))
868 /* Dump a newline and indent BUFFER by SPC chars. */
871 newline_and_indent (pretty_printer
*buffer
, int spc
)
877 struct with
{ char *s
; const char *in_file
; int limited
; };
878 static struct with
*withs
= NULL
;
879 static int withs_max
= 4096;
880 static int with_len
= 0;
882 /* Record a "with" clause on package S (a limited with if LIMITED_ACCESS is
883 true), if not already done. */
886 append_withs (const char *s
, int limited_access
)
891 withs
= XNEWVEC (struct with
, withs_max
);
893 if (with_len
== withs_max
)
896 withs
= XRESIZEVEC (struct with
, withs
, withs_max
);
899 for (i
= 0; i
< with_len
; i
++)
900 if (!strcmp (s
, withs
[i
].s
)
901 && source_file_base
== withs
[i
].in_file
)
903 withs
[i
].limited
&= limited_access
;
907 withs
[with_len
].s
= xstrdup (s
);
908 withs
[with_len
].in_file
= source_file_base
;
909 withs
[with_len
].limited
= limited_access
;
913 /* Reset "with" clauses. */
916 reset_ada_withs (void)
923 for (i
= 0; i
< with_len
; i
++)
931 /* Dump "with" clauses in F. */
934 dump_ada_withs (FILE *f
)
938 fprintf (f
, "with Interfaces.C; use Interfaces.C;\n");
940 for (i
= 0; i
< with_len
; i
++)
942 (f
, "%swith %s;\n", withs
[i
].limited
? "limited " : "", withs
[i
].s
);
945 /* Return suitable Ada package name from FILE. */
948 get_ada_package (const char *file
)
956 s
= strstr (file
, "/include/");
960 base
= lbasename (file
);
962 if (ada_specs_parent
== NULL
)
965 plen
= strlen (ada_specs_parent
) + 1;
967 res
= XNEWVEC (char, plen
+ strlen (base
) + 1);
968 if (ada_specs_parent
!= NULL
) {
969 strcpy (res
, ada_specs_parent
);
973 for (i
= plen
; *base
; base
++, i
++)
985 res
[i
] = (i
== 0 || res
[i
- 1] == '.' || res
[i
- 1] == '_') ? 'u' : '_';
997 static const char *ada_reserved
[] = {
998 "abort", "abs", "abstract", "accept", "access", "aliased", "all", "and",
999 "array", "at", "begin", "body", "case", "constant", "declare", "delay",
1000 "delta", "digits", "do", "else", "elsif", "end", "entry", "exception",
1001 "exit", "for", "function", "generic", "goto", "if", "in", "interface", "is",
1002 "limited", "loop", "mod", "new", "not", "null", "others", "out", "of", "or",
1003 "overriding", "package", "pragma", "private", "procedure", "protected",
1004 "raise", "range", "record", "rem", "renames", "requeue", "return", "reverse",
1005 "select", "separate", "subtype", "synchronized", "tagged", "task",
1006 "terminate", "then", "type", "until", "use", "when", "while", "with", "xor",
1009 /* ??? would be nice to specify this list via a config file, so that users
1010 can create their own dictionary of conflicts. */
1011 static const char *c_duplicates
[] = {
1012 /* system will cause troubles with System.Address. */
1015 /* The following values have other definitions with same name/other
1021 "rl_readline_version",
1027 /* Return a declaration tree corresponding to TYPE. */
1030 get_underlying_decl (tree type
)
1035 /* type is a declaration. */
1039 /* type is a typedef. */
1040 if (TYPE_P (type
) && TYPE_NAME (type
) && DECL_P (TYPE_NAME (type
)))
1041 return TYPE_NAME (type
);
1043 /* TYPE_STUB_DECL has been set for type. */
1044 if (TYPE_P (type
) && TYPE_STUB_DECL (type
))
1045 return TYPE_STUB_DECL (type
);
1050 /* Return whether TYPE has static fields. */
1053 has_static_fields (const_tree type
)
1057 if (!type
|| !RECORD_OR_UNION_TYPE_P (type
))
1060 for (tmp
= TYPE_FIELDS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
1061 if (DECL_NAME (tmp
) && TREE_STATIC (tmp
))
1067 /* Return whether TYPE corresponds to an Ada tagged type (has a dispatch
1071 is_tagged_type (const_tree type
)
1075 if (!type
|| !RECORD_OR_UNION_TYPE_P (type
))
1078 /* TYPE_METHODS is only set on the main variant. */
1079 type
= TYPE_MAIN_VARIANT (type
);
1081 for (tmp
= TYPE_METHODS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
1082 if (TREE_CODE (tmp
) == FUNCTION_DECL
&& DECL_VINDEX (tmp
))
1088 /* Return whether TYPE has non-trivial methods, i.e. methods that do something
1089 for the objects of TYPE. In C++, all classes have implicit special methods,
1090 e.g. constructors and destructors, but they can be trivial if the type is
1091 sufficiently simple. */
1094 has_nontrivial_methods (tree type
)
1098 if (!type
|| !RECORD_OR_UNION_TYPE_P (type
))
1101 /* Only C++ types can have methods. */
1105 /* A non-trivial type has non-trivial special methods. */
1106 if (!cpp_check (type
, IS_TRIVIAL
))
1109 /* TYPE_METHODS is only set on the main variant. */
1110 type
= TYPE_MAIN_VARIANT (type
);
1112 /* If there are user-defined methods, they are deemed non-trivial. */
1113 for (tmp
= TYPE_METHODS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
1114 if (!DECL_ARTIFICIAL (tmp
))
1120 /* Generate a legal Ada name from a C NAME, returning a malloc'd string.
1121 SPACE_FOUND, if not NULL, is used to indicate whether a space was found in
1125 to_ada_name (const char *name
, int *space_found
)
1128 int len
= strlen (name
);
1131 char *s
= XNEWVEC (char, len
* 2 + 5);
1135 *space_found
= false;
1137 /* Add trailing "c_" if name is an Ada reserved word. */
1138 for (names
= ada_reserved
; *names
; names
++)
1139 if (!strcasecmp (name
, *names
))
1148 /* Add trailing "c_" if name is an potential case sensitive duplicate. */
1149 for (names
= c_duplicates
; *names
; names
++)
1150 if (!strcmp (name
, *names
))
1158 for (j
= 0; name
[j
] == '_'; j
++)
1163 else if (*name
== '.' || *name
== '$')
1173 /* Replace unsuitable characters for Ada identifiers. */
1175 for (; j
< len
; j
++)
1180 *space_found
= true;
1184 /* ??? missing some C++ operators. */
1188 if (name
[j
+ 1] == '=')
1203 if (name
[j
+ 1] == '=')
1221 s
[len2
++] = name
[j
] == '&' ? 'a' : name
[j
] == '|' ? 'o' : 'x';
1223 if (name
[j
+ 1] == '=')
1236 if (s
[len2
- 1] != '_')
1239 switch (name
[j
+ 1]) {
1242 switch (name
[j
- 1]) {
1243 case '+': s
[len2
++] = 'p'; break; /* + */
1244 case '-': s
[len2
++] = 'm'; break; /* - */
1245 case '*': s
[len2
++] = 't'; break; /* * */
1246 case '/': s
[len2
++] = 'd'; break; /* / */
1252 switch (name
[j
- 1]) {
1253 case '+': s
[len2
++] = 'p'; break; /* += */
1254 case '-': s
[len2
++] = 'm'; break; /* -= */
1255 case '*': s
[len2
++] = 't'; break; /* *= */
1256 case '/': s
[len2
++] = 'd'; break; /* /= */
1290 c
= name
[j
] == '<' ? 'l' : 'g';
1293 switch (name
[j
+ 1]) {
1319 if (len2
&& s
[len2
- 1] == '_')
1324 s
[len2
++] = name
[j
];
1327 if (s
[len2
- 1] == '_')
1335 /* Return true if DECL refers to a C++ class type for which a
1336 separate enclosing package has been or should be generated. */
1339 separate_class_package (tree decl
)
1341 tree type
= TREE_TYPE (decl
);
1342 return has_nontrivial_methods (type
) || has_static_fields (type
);
1345 static bool package_prefix
= true;
1347 /* Dump in BUFFER the name of an identifier NODE of type TYPE, following Ada
1348 syntax. LIMITED_ACCESS indicates whether NODE can be accessed via a limited
1349 'with' clause rather than a regular 'with' clause. */
1352 pp_ada_tree_identifier (pretty_printer
*buffer
, tree node
, tree type
,
1355 const char *name
= IDENTIFIER_POINTER (node
);
1356 int space_found
= false;
1357 char *s
= to_ada_name (name
, &space_found
);
1360 /* If the entity is a type and comes from another file, generate "package"
1362 decl
= get_underlying_decl (type
);
1366 expanded_location xloc
= expand_location (decl_sloc (decl
, false));
1368 if (xloc
.file
&& xloc
.line
)
1370 if (xloc
.file
!= source_file_base
)
1372 switch (TREE_CODE (type
))
1377 case FIXED_POINT_TYPE
:
1379 case REFERENCE_TYPE
:
1387 char *s1
= get_ada_package (xloc
.file
);
1388 append_withs (s1
, limited_access
);
1389 pp_string (buffer
, s1
);
1398 /* Generate the additional package prefix for C++ classes. */
1399 if (separate_class_package (decl
))
1401 pp_string (buffer
, "Class_");
1402 pp_string (buffer
, s
);
1410 if (!strcmp (s
, "short_int"))
1411 pp_string (buffer
, "short");
1412 else if (!strcmp (s
, "short_unsigned_int"))
1413 pp_string (buffer
, "unsigned_short");
1414 else if (!strcmp (s
, "unsigned_int"))
1415 pp_string (buffer
, "unsigned");
1416 else if (!strcmp (s
, "long_int"))
1417 pp_string (buffer
, "long");
1418 else if (!strcmp (s
, "long_unsigned_int"))
1419 pp_string (buffer
, "unsigned_long");
1420 else if (!strcmp (s
, "long_long_int"))
1421 pp_string (buffer
, "Long_Long_Integer");
1422 else if (!strcmp (s
, "long_long_unsigned_int"))
1426 append_withs ("Interfaces.C.Extensions", false);
1427 pp_string (buffer
, "Extensions.unsigned_long_long");
1430 pp_string (buffer
, "unsigned_long_long");
1433 pp_string(buffer
, s
);
1435 if (!strcmp (s
, "bool"))
1439 append_withs ("Interfaces.C.Extensions", false);
1440 pp_string (buffer
, "Extensions.bool");
1443 pp_string (buffer
, "bool");
1446 pp_string(buffer
, s
);
1451 /* Dump in BUFFER the assembly name of T. */
1454 pp_asm_name (pretty_printer
*buffer
, tree t
)
1456 tree name
= DECL_ASSEMBLER_NAME (t
);
1457 char *ada_name
= XALLOCAVEC (char, IDENTIFIER_LENGTH (name
) + 1), *s
;
1458 const char *ident
= IDENTIFIER_POINTER (name
);
1460 for (s
= ada_name
; *ident
; ident
++)
1464 else if (*ident
!= '*')
1469 pp_string (buffer
, ada_name
);
1472 /* Dump in BUFFER the name of a DECL node if set, following Ada syntax.
1473 LIMITED_ACCESS indicates whether NODE can be accessed via a limited
1474 'with' clause rather than a regular 'with' clause. */
1477 dump_ada_decl_name (pretty_printer
*buffer
, tree decl
, int limited_access
)
1479 if (DECL_NAME (decl
))
1480 pp_ada_tree_identifier (buffer
, DECL_NAME (decl
), decl
, limited_access
);
1483 tree type_name
= TYPE_NAME (TREE_TYPE (decl
));
1487 pp_string (buffer
, "anon");
1488 if (TREE_CODE (decl
) == FIELD_DECL
)
1489 pp_scalar (buffer
, "%d", DECL_UID (decl
));
1491 pp_scalar (buffer
, "%d", TYPE_UID (TREE_TYPE (decl
)));
1493 else if (TREE_CODE (type_name
) == IDENTIFIER_NODE
)
1494 pp_ada_tree_identifier (buffer
, type_name
, decl
, limited_access
);
1498 /* Dump in BUFFER a name based on both T1 and T2 followed by a suffix. */
1501 dump_ada_double_name (pretty_printer
*buffer
, tree t1
, tree t2
)
1504 pp_ada_tree_identifier (buffer
, DECL_NAME (t1
), t1
, false);
1507 pp_string (buffer
, "anon");
1508 pp_scalar (buffer
, "%d", TYPE_UID (TREE_TYPE (t1
)));
1511 pp_underscore (buffer
);
1514 pp_ada_tree_identifier (buffer
, DECL_NAME (t2
), t2
, false);
1517 pp_string (buffer
, "anon");
1518 pp_scalar (buffer
, "%d", TYPE_UID (TREE_TYPE (t2
)));
1521 switch (TREE_CODE (TREE_TYPE (t2
)))
1524 pp_string (buffer
, "_array");
1527 pp_string (buffer
, "_struct");
1530 pp_string (buffer
, "_union");
1533 pp_string (buffer
, "_unknown");
1538 /* Dump in BUFFER pragma Import C/CPP on a given node T. */
1541 dump_ada_import (pretty_printer
*buffer
, tree t
)
1543 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t
));
1544 int is_stdcall
= TREE_CODE (t
) == FUNCTION_DECL
&&
1545 lookup_attribute ("stdcall", TYPE_ATTRIBUTES (TREE_TYPE (t
)));
1548 pp_string (buffer
, "pragma Import (Stdcall, ");
1549 else if (name
[0] == '_' && name
[1] == 'Z')
1550 pp_string (buffer
, "pragma Import (CPP, ");
1552 pp_string (buffer
, "pragma Import (C, ");
1554 dump_ada_decl_name (buffer
, t
, false);
1555 pp_string (buffer
, ", \"");
1558 pp_string (buffer
, IDENTIFIER_POINTER (DECL_NAME (t
)));
1560 pp_asm_name (buffer
, t
);
1562 pp_string (buffer
, "\");");
1565 /* Check whether T and its type have different names, and append "the_"
1566 otherwise in BUFFER. */
1569 check_name (pretty_printer
*buffer
, tree t
)
1572 tree tmp
= TREE_TYPE (t
);
1574 while (TREE_CODE (tmp
) == POINTER_TYPE
&& !TYPE_NAME (tmp
))
1575 tmp
= TREE_TYPE (tmp
);
1577 if (TREE_CODE (tmp
) != FUNCTION_TYPE
)
1579 if (TREE_CODE (tmp
) == IDENTIFIER_NODE
)
1580 s
= IDENTIFIER_POINTER (tmp
);
1581 else if (!TYPE_NAME (tmp
))
1583 else if (TREE_CODE (TYPE_NAME (tmp
)) == IDENTIFIER_NODE
)
1584 s
= IDENTIFIER_POINTER (TYPE_NAME (tmp
));
1586 s
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (tmp
)));
1588 if (!strcasecmp (IDENTIFIER_POINTER (DECL_NAME (t
)), s
))
1589 pp_string (buffer
, "the_");
1593 /* Dump in BUFFER a function declaration FUNC with Ada syntax.
1594 IS_METHOD indicates whether FUNC is a C++ method.
1595 IS_CONSTRUCTOR whether FUNC is a C++ constructor.
1596 IS_DESTRUCTOR whether FUNC is a C++ destructor.
1597 SPC is the current indentation level. */
1600 dump_ada_function_declaration (pretty_printer
*buffer
, tree func
,
1601 int is_method
, int is_constructor
,
1602 int is_destructor
, int spc
)
1605 const tree node
= TREE_TYPE (func
);
1607 int num
= 0, num_args
= 0, have_args
= true, have_ellipsis
= false;
1609 /* Compute number of arguments. */
1610 arg
= TYPE_ARG_TYPES (node
);
1614 while (TREE_CHAIN (arg
) && arg
!= error_mark_node
)
1617 arg
= TREE_CHAIN (arg
);
1620 if (TREE_CODE (TREE_VALUE (arg
)) != VOID_TYPE
)
1623 have_ellipsis
= true;
1634 newline_and_indent (buffer
, spc
+ 1);
1639 pp_left_paren (buffer
);
1642 if (TREE_CODE (func
) == FUNCTION_DECL
)
1643 arg
= DECL_ARGUMENTS (func
);
1647 if (arg
== NULL_TREE
)
1650 arg
= TYPE_ARG_TYPES (node
);
1652 if (arg
&& TREE_CODE (TREE_VALUE (arg
)) == VOID_TYPE
)
1657 arg
= TREE_CHAIN (arg
);
1659 /* Print the argument names (if available) & types. */
1661 for (num
= 1; num
<= num_args
; num
++)
1665 if (DECL_NAME (arg
))
1667 check_name (buffer
, arg
);
1668 pp_ada_tree_identifier (buffer
, DECL_NAME (arg
), 0, false);
1669 pp_string (buffer
, " : ");
1673 sprintf (buf
, "arg%d : ", num
);
1674 pp_string (buffer
, buf
);
1677 dump_generic_ada_node (buffer
, TREE_TYPE (arg
), node
, spc
, 0, true);
1681 sprintf (buf
, "arg%d : ", num
);
1682 pp_string (buffer
, buf
);
1683 dump_generic_ada_node (buffer
, TREE_VALUE (arg
), node
, spc
, 0, true);
1686 if (TREE_TYPE (arg
) && TREE_TYPE (TREE_TYPE (arg
))
1687 && is_tagged_type (TREE_TYPE (TREE_TYPE (arg
))))
1690 || (num
!= 1 || (!DECL_VINDEX (func
) && !is_constructor
)))
1691 pp_string (buffer
, "'Class");
1694 arg
= TREE_CHAIN (arg
);
1698 pp_semicolon (buffer
);
1701 newline_and_indent (buffer
, spc
+ INDENT_INCR
);
1709 pp_string (buffer
, " -- , ...");
1710 newline_and_indent (buffer
, spc
+ INDENT_INCR
);
1714 pp_right_paren (buffer
);
1718 /* Dump in BUFFER all the domains associated with an array NODE,
1719 using Ada syntax. SPC is the current indentation level. */
1722 dump_ada_array_domains (pretty_printer
*buffer
, tree node
, int spc
)
1725 pp_left_paren (buffer
);
1727 for (; TREE_CODE (node
) == ARRAY_TYPE
; node
= TREE_TYPE (node
))
1729 tree domain
= TYPE_DOMAIN (node
);
1733 tree min
= TYPE_MIN_VALUE (domain
);
1734 tree max
= TYPE_MAX_VALUE (domain
);
1737 pp_string (buffer
, ", ");
1741 dump_generic_ada_node (buffer
, min
, NULL_TREE
, spc
, 0, true);
1742 pp_string (buffer
, " .. ");
1744 /* If the upper bound is zero, gcc may generate a NULL_TREE
1745 for TYPE_MAX_VALUE rather than an integer_cst. */
1747 dump_generic_ada_node (buffer
, max
, NULL_TREE
, spc
, 0, true);
1749 pp_string (buffer
, "0");
1752 pp_string (buffer
, "size_t");
1754 pp_right_paren (buffer
);
1757 /* Dump in BUFFER file:line information related to NODE. */
1760 dump_sloc (pretty_printer
*buffer
, tree node
)
1762 expanded_location xloc
;
1767 xloc
= expand_location (DECL_SOURCE_LOCATION (node
));
1768 else if (EXPR_HAS_LOCATION (node
))
1769 xloc
= expand_location (EXPR_LOCATION (node
));
1773 pp_string (buffer
, xloc
.file
);
1775 pp_decimal_int (buffer
, xloc
.line
);
1779 /* Return true if T designates a one dimension array of "char". */
1782 is_char_array (tree t
)
1787 /* Retrieve array's type. */
1789 while (TREE_CODE (TREE_TYPE (tmp
)) == ARRAY_TYPE
)
1792 tmp
= TREE_TYPE (tmp
);
1795 tmp
= TREE_TYPE (tmp
);
1796 return num_dim
== 1 && TREE_CODE (tmp
) == INTEGER_TYPE
1797 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (tmp
))), "char");
1800 /* Dump in BUFFER an array type T in Ada syntax. Assume that the "type"
1801 keyword and name have already been printed. PARENT is the parent node of T.
1802 SPC is the indentation level. */
1805 dump_ada_array_type (pretty_printer
*buffer
, tree t
, tree parent
, int spc
)
1807 const bool char_array
= is_char_array (t
);
1810 /* Special case char arrays. */
1813 pp_string (buffer
, "Interfaces.C.char_array ");
1816 pp_string (buffer
, "array ");
1818 /* Print the dimensions. */
1819 dump_ada_array_domains (buffer
, TREE_TYPE (t
), spc
);
1821 /* Retrieve the element type. */
1822 tmp
= TREE_TYPE (t
);
1823 while (TREE_CODE (tmp
) == ARRAY_TYPE
)
1824 tmp
= TREE_TYPE (tmp
);
1826 /* Print array's type. */
1829 pp_string (buffer
, " of ");
1831 if (TREE_CODE (tmp
) != POINTER_TYPE
)
1832 pp_string (buffer
, "aliased ");
1834 if (TYPE_NAME (tmp
) || !RECORD_OR_UNION_TYPE_P (tmp
))
1835 dump_generic_ada_node (buffer
, tmp
, TREE_TYPE (t
), spc
, false, true);
1837 dump_ada_double_name (buffer
, parent
, get_underlying_decl (tmp
));
1841 /* Dump in BUFFER type names associated with a template, each prepended with
1842 '_'. TYPES is the TREE_PURPOSE of a DECL_TEMPLATE_INSTANTIATIONS. SPC is
1843 the indentation level. */
1846 dump_template_types (pretty_printer
*buffer
, tree types
, int spc
)
1849 size_t len
= TREE_VEC_LENGTH (types
);
1851 for (i
= 0; i
< len
; i
++)
1853 tree elem
= TREE_VEC_ELT (types
, i
);
1854 pp_underscore (buffer
);
1855 if (!dump_generic_ada_node (buffer
, elem
, 0, spc
, false, true))
1857 pp_string (buffer
, "unknown");
1858 pp_scalar (buffer
, "%lu", (unsigned long) TREE_HASH (elem
));
1863 /* Dump in BUFFER the contents of all class instantiations associated with
1864 a given template T. SPC is the indentation level. */
1867 dump_ada_template (pretty_printer
*buffer
, tree t
, int spc
)
1869 /* DECL_SIZE_UNIT is DECL_TEMPLATE_INSTANTIATIONS in this context. */
1870 tree inst
= DECL_SIZE_UNIT (t
);
1871 /* This emulates DECL_TEMPLATE_RESULT in this context. */
1872 struct tree_template_decl
{
1873 struct tree_decl_common common
;
1877 tree result
= ((struct tree_template_decl
*) t
)->result
;
1880 /* Don't look at template declarations declaring something coming from
1881 another file. This can occur for template friend declarations. */
1882 if (LOCATION_FILE (decl_sloc (result
, false))
1883 != LOCATION_FILE (decl_sloc (t
, false)))
1886 for (; inst
&& inst
!= error_mark_node
; inst
= TREE_CHAIN (inst
))
1888 tree types
= TREE_PURPOSE (inst
);
1889 tree instance
= TREE_VALUE (inst
);
1891 if (TREE_VEC_LENGTH (types
) == 0)
1894 if (!RECORD_OR_UNION_TYPE_P (instance
) || !TYPE_METHODS (instance
))
1897 /* We are interested in concrete template instantiations only: skip
1898 partially specialized nodes. */
1899 if (RECORD_OR_UNION_TYPE_P (instance
)
1901 && cpp_check (instance
, HAS_DEPENDENT_TEMPLATE_ARGS
))
1906 pp_string (buffer
, "package ");
1907 package_prefix
= false;
1908 dump_generic_ada_node (buffer
, instance
, t
, spc
, false, true);
1909 dump_template_types (buffer
, types
, spc
);
1910 pp_string (buffer
, " is");
1912 newline_and_indent (buffer
, spc
);
1914 TREE_VISITED (get_underlying_decl (instance
)) = 1;
1915 pp_string (buffer
, "type ");
1916 dump_generic_ada_node (buffer
, instance
, t
, spc
, false, true);
1917 package_prefix
= true;
1919 if (is_tagged_type (instance
))
1920 pp_string (buffer
, " is tagged limited ");
1922 pp_string (buffer
, " is limited ");
1924 dump_generic_ada_node (buffer
, instance
, t
, spc
, false, false);
1925 pp_newline (buffer
);
1927 newline_and_indent (buffer
, spc
);
1929 pp_string (buffer
, "end;");
1930 newline_and_indent (buffer
, spc
);
1931 pp_string (buffer
, "use ");
1932 package_prefix
= false;
1933 dump_generic_ada_node (buffer
, instance
, t
, spc
, false, true);
1934 dump_template_types (buffer
, types
, spc
);
1935 package_prefix
= true;
1936 pp_semicolon (buffer
);
1937 pp_newline (buffer
);
1938 pp_newline (buffer
);
1941 return num_inst
> 0;
1944 /* Return true if NODE is a simple enum types, that can be mapped to an
1945 Ada enum type directly. */
1948 is_simple_enum (tree node
)
1950 HOST_WIDE_INT count
= 0;
1953 for (value
= TYPE_VALUES (node
); value
; value
= TREE_CHAIN (value
))
1955 tree int_val
= TREE_VALUE (value
);
1957 if (TREE_CODE (int_val
) != INTEGER_CST
)
1958 int_val
= DECL_INITIAL (int_val
);
1960 if (!tree_fits_shwi_p (int_val
))
1962 else if (tree_to_shwi (int_val
) != count
)
1971 static bool bitfield_used
= false;
1973 /* Recursively dump in BUFFER Ada declarations corresponding to NODE of type
1974 TYPE. SPC is the indentation level. LIMITED_ACCESS indicates whether NODE
1975 can be referenced via a "limited with" clause. NAME_ONLY indicates whether
1976 we should only dump the name of NODE, instead of its full declaration. */
1979 dump_generic_ada_node (pretty_printer
*buffer
, tree node
, tree type
, int spc
,
1980 int limited_access
, bool name_only
)
1982 if (node
== NULL_TREE
)
1985 switch (TREE_CODE (node
))
1988 pp_string (buffer
, "<<< error >>>");
1991 case IDENTIFIER_NODE
:
1992 pp_ada_tree_identifier (buffer
, node
, type
, limited_access
);
1996 pp_string (buffer
, "--- unexpected node: TREE_LIST");
2000 dump_generic_ada_node
2001 (buffer
, BINFO_TYPE (node
), type
, spc
, limited_access
, name_only
);
2005 pp_string (buffer
, "--- unexpected node: TREE_VEC");
2011 append_withs ("System", false);
2012 pp_string (buffer
, "System.Address");
2015 pp_string (buffer
, "address");
2019 pp_string (buffer
, "<vector>");
2023 pp_string (buffer
, "<complex>");
2028 dump_generic_ada_node (buffer
, TYPE_NAME (node
), node
, spc
, 0, true);
2031 tree value
= TYPE_VALUES (node
);
2033 if (is_simple_enum (node
))
2037 newline_and_indent (buffer
, spc
- 1);
2038 pp_left_paren (buffer
);
2039 for (; value
; value
= TREE_CHAIN (value
))
2046 newline_and_indent (buffer
, spc
);
2049 pp_ada_tree_identifier
2050 (buffer
, TREE_PURPOSE (value
), node
, false);
2052 pp_string (buffer
, ");");
2054 newline_and_indent (buffer
, spc
);
2055 pp_string (buffer
, "pragma Convention (C, ");
2056 dump_generic_ada_node
2057 (buffer
, DECL_NAME (type
) ? type
: TYPE_NAME (node
), type
,
2059 pp_right_paren (buffer
);
2063 pp_string (buffer
, "unsigned");
2064 for (; value
; value
= TREE_CHAIN (value
))
2066 pp_semicolon (buffer
);
2067 newline_and_indent (buffer
, spc
);
2069 pp_ada_tree_identifier
2070 (buffer
, TREE_PURPOSE (value
), node
, false);
2071 pp_string (buffer
, " : constant ");
2073 dump_generic_ada_node
2074 (buffer
, DECL_NAME (type
) ? type
: TYPE_NAME (node
), type
,
2077 pp_string (buffer
, " := ");
2078 dump_generic_ada_node
2080 TREE_CODE (TREE_VALUE (value
)) == INTEGER_CST
?
2081 TREE_VALUE (value
) : DECL_INITIAL (TREE_VALUE (value
)),
2082 node
, spc
, false, true);
2090 case FIXED_POINT_TYPE
:
2093 enum tree_code_class tclass
;
2095 tclass
= TREE_CODE_CLASS (TREE_CODE (node
));
2097 if (tclass
== tcc_declaration
)
2099 if (DECL_NAME (node
))
2100 pp_ada_tree_identifier
2101 (buffer
, DECL_NAME (node
), 0, limited_access
);
2103 pp_string (buffer
, "<unnamed type decl>");
2105 else if (tclass
== tcc_type
)
2107 if (TYPE_NAME (node
))
2109 if (TREE_CODE (TYPE_NAME (node
)) == IDENTIFIER_NODE
)
2110 pp_ada_tree_identifier (buffer
, TYPE_NAME (node
),
2111 node
, limited_access
);
2112 else if (TREE_CODE (TYPE_NAME (node
)) == TYPE_DECL
2113 && DECL_NAME (TYPE_NAME (node
)))
2114 dump_ada_decl_name (buffer
, TYPE_NAME (node
), limited_access
);
2116 pp_string (buffer
, "<unnamed type>");
2118 else if (TREE_CODE (node
) == INTEGER_TYPE
)
2120 append_withs ("Interfaces.C.Extensions", false);
2121 bitfield_used
= true;
2123 if (TYPE_PRECISION (node
) == 1)
2124 pp_string (buffer
, "Extensions.Unsigned_1");
2127 pp_string (buffer
, (TYPE_UNSIGNED (node
)
2128 ? "Extensions.Unsigned_"
2129 : "Extensions.Signed_"));
2130 pp_decimal_int (buffer
, TYPE_PRECISION (node
));
2134 pp_string (buffer
, "<unnamed type>");
2140 case REFERENCE_TYPE
:
2141 if (name_only
&& TYPE_NAME (node
))
2142 dump_generic_ada_node
2143 (buffer
, TYPE_NAME (node
), node
, spc
, limited_access
, true);
2145 else if (TREE_CODE (TREE_TYPE (node
)) == FUNCTION_TYPE
)
2147 tree fnode
= TREE_TYPE (node
);
2150 if (VOID_TYPE_P (TREE_TYPE (fnode
)))
2152 is_function
= false;
2153 pp_string (buffer
, "access procedure");
2158 pp_string (buffer
, "access function");
2161 dump_ada_function_declaration
2162 (buffer
, node
, false, false, false, spc
+ INDENT_INCR
);
2166 pp_string (buffer
, " return ");
2167 dump_generic_ada_node
2168 (buffer
, TREE_TYPE (fnode
), type
, spc
, 0, true);
2171 /* If we are dumping the full type, it means we are part of a
2172 type definition and need also a Convention C pragma. */
2175 pp_semicolon (buffer
);
2176 newline_and_indent (buffer
, spc
);
2177 pp_string (buffer
, "pragma Convention (C, ");
2178 dump_generic_ada_node
2179 (buffer
, type
, 0, spc
, false, true);
2180 pp_right_paren (buffer
);
2185 int is_access
= false;
2186 unsigned int quals
= TYPE_QUALS (TREE_TYPE (node
));
2188 if (VOID_TYPE_P (TREE_TYPE (node
)))
2191 pp_string (buffer
, "new ");
2194 append_withs ("System", false);
2195 pp_string (buffer
, "System.Address");
2198 pp_string (buffer
, "address");
2202 if (TREE_CODE (node
) == POINTER_TYPE
2203 && TREE_CODE (TREE_TYPE (node
)) == INTEGER_TYPE
2205 (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME
2206 (TREE_TYPE (node
)))), "char"))
2209 pp_string (buffer
, "new ");
2213 pp_string (buffer
, "Interfaces.C.Strings.chars_ptr");
2214 append_withs ("Interfaces.C.Strings", false);
2217 pp_string (buffer
, "chars_ptr");
2221 tree type_name
= TYPE_NAME (TREE_TYPE (node
));
2222 tree decl
= get_underlying_decl (TREE_TYPE (node
));
2223 tree enclosing_decl
= get_underlying_decl (type
);
2225 /* For now, handle access-to-access, access-to-empty-struct
2226 or access-to-incomplete as opaque system.address. */
2227 if (TREE_CODE (TREE_TYPE (node
)) == POINTER_TYPE
2228 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (node
))
2229 && !TYPE_FIELDS (TREE_TYPE (node
)))
2232 && !TREE_VISITED (decl
)
2233 && DECL_SOURCE_FILE (decl
) == source_file_base
)
2235 && !TREE_VISITED (decl
)
2236 && DECL_SOURCE_FILE (decl
)
2237 == DECL_SOURCE_FILE (enclosing_decl
)
2238 && decl_sloc (decl
, true)
2239 > decl_sloc (enclosing_decl
, true)))
2243 append_withs ("System", false);
2245 pp_string (buffer
, "new ");
2246 pp_string (buffer
, "System.Address");
2249 pp_string (buffer
, "address");
2253 if (!package_prefix
)
2254 pp_string (buffer
, "access");
2255 else if (AGGREGATE_TYPE_P (TREE_TYPE (node
)))
2257 if (!type
|| TREE_CODE (type
) != FUNCTION_DECL
)
2259 pp_string (buffer
, "access ");
2262 if (quals
& TYPE_QUAL_CONST
)
2263 pp_string (buffer
, "constant ");
2264 else if (!name_only
)
2265 pp_string (buffer
, "all ");
2267 else if (quals
& TYPE_QUAL_CONST
)
2268 pp_string (buffer
, "in ");
2272 pp_string (buffer
, "access ");
2273 /* ??? should be configurable: access or in out. */
2279 pp_string (buffer
, "access ");
2282 pp_string (buffer
, "all ");
2285 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (node
)) && type_name
)
2286 dump_generic_ada_node (buffer
, type_name
, TREE_TYPE (node
),
2287 spc
, is_access
, true);
2289 dump_generic_ada_node (buffer
, TREE_TYPE (node
),
2290 TREE_TYPE (node
), spc
, 0, true);
2298 dump_generic_ada_node
2299 (buffer
, TYPE_NAME (node
), node
, spc
, limited_access
, true);
2301 dump_ada_array_type (buffer
, node
, type
, spc
);
2308 if (TYPE_NAME (node
))
2309 dump_generic_ada_node
2310 (buffer
, TYPE_NAME (node
), node
, spc
, limited_access
, true);
2313 pp_string (buffer
, "anon_");
2314 pp_scalar (buffer
, "%d", TYPE_UID (node
));
2318 print_ada_struct_decl (buffer
, node
, type
, spc
, true);
2322 /* We treat the upper half of the sizetype range as negative. This
2323 is consistent with the internal treatment and makes it possible
2324 to generate the (0 .. -1) range for flexible array members. */
2325 if (TREE_TYPE (node
) == sizetype
)
2326 node
= fold_convert (ssizetype
, node
);
2327 if (tree_fits_shwi_p (node
))
2328 pp_wide_integer (buffer
, tree_to_shwi (node
));
2329 else if (tree_fits_uhwi_p (node
))
2330 pp_unsigned_wide_integer (buffer
, tree_to_uhwi (node
));
2333 wide_int val
= node
;
2335 if (wi::neg_p (val
))
2340 sprintf (pp_buffer (buffer
)->digit_buffer
,
2341 "16#%" HOST_WIDE_INT_PRINT
"x",
2342 val
.elt (val
.get_len () - 1));
2343 for (i
= val
.get_len () - 2; i
>= 0; i
--)
2344 sprintf (pp_buffer (buffer
)->digit_buffer
,
2345 HOST_WIDE_INT_PRINT_PADDED_HEX
, val
.elt (i
));
2346 pp_string (buffer
, pp_buffer (buffer
)->digit_buffer
);
2359 dump_ada_decl_name (buffer
, node
, limited_access
);
2363 if (DECL_IS_BUILTIN (node
))
2365 /* Don't print the declaration of built-in types. */
2369 /* If we're in the middle of a declaration, defaults to
2373 append_withs ("System", false);
2374 pp_string (buffer
, "System.Address");
2377 pp_string (buffer
, "address");
2383 dump_ada_decl_name (buffer
, node
, limited_access
);
2386 if (is_tagged_type (TREE_TYPE (node
)))
2388 tree tmp
= TYPE_FIELDS (TREE_TYPE (node
));
2391 /* Look for ancestors. */
2392 for (; tmp
; tmp
= TREE_CHAIN (tmp
))
2394 if (!DECL_NAME (tmp
) && is_tagged_type (TREE_TYPE (tmp
)))
2398 pp_string (buffer
, "limited new ");
2402 pp_string (buffer
, " and ");
2405 (buffer
, TYPE_NAME (TREE_TYPE (tmp
)), false);
2409 pp_string (buffer
, first
? "tagged limited " : " with ");
2411 else if (has_nontrivial_methods (TREE_TYPE (node
)))
2412 pp_string (buffer
, "limited ");
2414 dump_generic_ada_node
2415 (buffer
, TREE_TYPE (node
), type
, spc
, false, false);
2422 case NAMESPACE_DECL
:
2423 dump_ada_decl_name (buffer
, node
, false);
2427 /* Ignore other nodes (e.g. expressions). */
2434 /* Dump in BUFFER NODE's methods. SPC is the indentation level. Return 1 if
2435 methods were printed, 0 otherwise.
2437 We do it in 2 passes: first, the regular methods, i.e. non-static member
2438 functions, are output immediately within the package created for the class
2439 so that they are considered as primitive operations in Ada; second, the
2440 static member functions are output in a nested package so that they are
2441 _not_ considered as primitive operations in Ada.
2443 This approach is necessary because the formers have the implicit 'this'
2444 pointer whereas the latters don't and, on 32-bit x86/Windows, the calling
2445 conventions for the 'this' pointer are special. Therefore, the compiler
2446 needs to be able to differentiate regular methods (with 'this' pointer)
2447 from static member functions that take a pointer to the class as first
2451 print_ada_methods (pretty_printer
*buffer
, tree node
, int spc
)
2453 bool has_static_methods
= false;
2457 if (!has_nontrivial_methods (node
))
2460 pp_semicolon (buffer
);
2462 /* First pass: the regular methods. */
2464 for (t
= TYPE_METHODS (node
); t
; t
= TREE_CHAIN (t
))
2466 if (TREE_CODE (TREE_TYPE (t
)) != METHOD_TYPE
)
2468 has_static_methods
= true;
2474 pp_newline (buffer
);
2475 pp_newline (buffer
);
2478 res
= print_ada_declaration (buffer
, t
, node
, spc
);
2481 if (!has_static_methods
)
2484 pp_newline (buffer
);
2485 newline_and_indent (buffer
, spc
);
2487 /* Second pass: the static member functions. */
2488 pp_string (buffer
, "package Static is");
2489 pp_newline (buffer
);
2493 for (t
= TYPE_METHODS (node
); t
; t
= TREE_CHAIN (t
))
2495 if (TREE_CODE (TREE_TYPE (t
)) == METHOD_TYPE
)
2500 pp_newline (buffer
);
2501 pp_newline (buffer
);
2504 res
= print_ada_declaration (buffer
, t
, node
, spc
);
2508 newline_and_indent (buffer
, spc
);
2509 pp_string (buffer
, "end;");
2511 /* In order to save the clients from adding a second use clause for the
2512 nested package, we generate renamings for the static member functions
2513 in the package created for the class. */
2514 for (t
= TYPE_METHODS (node
); t
; t
= TREE_CHAIN (t
))
2518 if (TREE_CODE (TREE_TYPE (t
)) == METHOD_TYPE
)
2521 pp_newline (buffer
);
2522 newline_and_indent (buffer
, spc
);
2524 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (t
))))
2526 pp_string (buffer
, "procedure ");
2527 is_function
= false;
2531 pp_string (buffer
, "function ");
2535 dump_ada_decl_name (buffer
, t
, false);
2536 dump_ada_function_declaration (buffer
, t
, false, false, false, spc
);
2540 pp_string (buffer
, " return ");
2541 dump_generic_ada_node (buffer
, TREE_TYPE (TREE_TYPE (t
)), node
,
2545 pp_string (buffer
, " renames Static.");
2546 dump_ada_decl_name (buffer
, t
, false);
2547 pp_semicolon (buffer
);
2553 static void dump_nested_type (pretty_printer
*, tree
, tree
, tree
, int);
2555 /* Dump in BUFFER anonymous types nested inside T's definition.
2556 PARENT is the parent node of T.
2557 FORWARD indicates whether a forward declaration of T should be generated.
2558 SPC is the indentation level.
2560 In C anonymous nested tagged types have no name whereas in C++ they have
2561 one. In C their TYPE_DECL is at top level whereas in C++ it is nested.
2562 In both languages untagged types (pointers and arrays) have no name.
2563 In C++ the nested TYPE_DECLs can come after their associated FIELD_DECL.
2565 Therefore, in order to have a common processing for both languages, we
2566 disregard anonymous TYPE_DECLs at top level and here we make a first
2567 pass on the nested TYPE_DECLs and a second pass on the unnamed types. */
2570 dump_nested_types (pretty_printer
*buffer
, tree t
, tree parent
, bool forward
,
2575 /* Avoid recursing over the same tree. */
2576 if (TREE_VISITED (t
))
2579 /* Find possible anonymous pointers/arrays/structs/unions recursively. */
2580 type
= TREE_TYPE (t
);
2581 if (type
== NULL_TREE
)
2586 pp_string (buffer
, "type ");
2587 dump_generic_ada_node (buffer
, t
, t
, spc
, false, true);
2588 pp_semicolon (buffer
);
2589 newline_and_indent (buffer
, spc
);
2590 TREE_VISITED (t
) = 1;
2593 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
2594 if (TREE_CODE (field
) == TYPE_DECL
2595 && DECL_NAME (field
) != DECL_NAME (t
)
2596 && TYPE_NAME (TREE_TYPE (field
)) != TYPE_NAME (type
))
2597 dump_nested_type (buffer
, field
, t
, parent
, spc
);
2599 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
2600 if (!TYPE_NAME (TREE_TYPE (field
)))
2601 dump_nested_type (buffer
, field
, t
, parent
, spc
);
2603 TREE_VISITED (t
) = 1;
2606 /* Dump in BUFFER the anonymous type of FIELD inside T.
2607 PARENT is the parent node of T.
2608 FORWARD indicates whether a forward declaration of T should be generated.
2609 SPC is the indentation level. */
2612 dump_nested_type (pretty_printer
*buffer
, tree field
, tree t
, tree parent
,
2615 tree field_type
= TREE_TYPE (field
);
2618 switch (TREE_CODE (field_type
))
2621 tmp
= TREE_TYPE (field_type
);
2623 if (TREE_CODE (tmp
) == FUNCTION_TYPE
)
2624 for (tmp
= TREE_TYPE (tmp
);
2625 tmp
&& TREE_CODE (tmp
) == POINTER_TYPE
;
2626 tmp
= TREE_TYPE (tmp
))
2629 decl
= get_underlying_decl (tmp
);
2631 && !DECL_IS_BUILTIN (decl
)
2632 && (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl
))
2633 || TYPE_FIELDS (TREE_TYPE (decl
)))
2634 && !TREE_VISITED (decl
)
2635 && DECL_SOURCE_FILE (decl
) == DECL_SOURCE_FILE (t
)
2636 && decl_sloc (decl
, true) > decl_sloc (t
, true))
2638 /* Generate forward declaration. */
2639 pp_string (buffer
, "type ");
2640 dump_generic_ada_node (buffer
, decl
, 0, spc
, false, true);
2641 pp_semicolon (buffer
);
2642 newline_and_indent (buffer
, spc
);
2643 TREE_VISITED (decl
) = 1;
2648 tmp
= TREE_TYPE (field_type
);
2649 while (TREE_CODE (tmp
) == ARRAY_TYPE
)
2650 tmp
= TREE_TYPE (tmp
);
2651 decl
= get_underlying_decl (tmp
);
2652 if (decl
&& !DECL_NAME (decl
) && !TREE_VISITED (decl
))
2654 /* Generate full declaration. */
2655 dump_nested_type (buffer
, decl
, t
, parent
, spc
);
2656 TREE_VISITED (decl
) = 1;
2659 /* Special case char arrays. */
2660 if (is_char_array (field
))
2661 pp_string (buffer
, "sub");
2663 pp_string (buffer
, "type ");
2664 dump_ada_double_name (buffer
, parent
, field
);
2665 pp_string (buffer
, " is ");
2666 dump_ada_array_type (buffer
, field
, parent
, spc
);
2667 pp_semicolon (buffer
);
2668 newline_and_indent (buffer
, spc
);
2673 if (TYPE_NAME (TREE_TYPE (t
)) && !TREE_VISITED (t
))
2675 pp_string (buffer
, "type ");
2676 dump_generic_ada_node (buffer
, t
, parent
, spc
, false, true);
2677 pp_semicolon (buffer
);
2678 newline_and_indent (buffer
, spc
);
2681 TREE_VISITED (t
) = 1;
2682 dump_nested_types (buffer
, field
, t
, false, spc
);
2684 pp_string (buffer
, "type ");
2686 if (TYPE_NAME (field_type
))
2688 dump_generic_ada_node (buffer
, field_type
, 0, spc
, false, true);
2689 if (TREE_CODE (field_type
) == UNION_TYPE
)
2690 pp_string (buffer
, " (discr : unsigned := 0)");
2691 pp_string (buffer
, " is ");
2692 print_ada_struct_decl (buffer
, field_type
, t
, spc
, false);
2694 pp_string (buffer
, "pragma Convention (C_Pass_By_Copy, ");
2695 dump_generic_ada_node (buffer
, field_type
, 0, spc
, false, true);
2696 pp_string (buffer
, ");");
2697 newline_and_indent (buffer
, spc
);
2699 if (TREE_CODE (field_type
) == UNION_TYPE
)
2701 pp_string (buffer
, "pragma Unchecked_Union (");
2702 dump_generic_ada_node (buffer
, field_type
, 0, spc
, false, true);
2703 pp_string (buffer
, ");");
2708 dump_ada_double_name (buffer
, parent
, field
);
2709 if (TREE_CODE (field_type
) == UNION_TYPE
)
2710 pp_string (buffer
, " (discr : unsigned := 0)");
2711 pp_string (buffer
, " is ");
2712 print_ada_struct_decl (buffer
, field_type
, t
, spc
, false);
2714 pp_string (buffer
, "pragma Convention (C_Pass_By_Copy, ");
2715 dump_ada_double_name (buffer
, parent
, field
);
2716 pp_string (buffer
, ");");
2717 newline_and_indent (buffer
, spc
);
2719 if (TREE_CODE (field_type
) == UNION_TYPE
)
2721 pp_string (buffer
, "pragma Unchecked_Union (");
2722 dump_ada_double_name (buffer
, parent
, field
);
2723 pp_string (buffer
, ");");
2732 /* Dump in BUFFER constructor spec corresponding to T. */
2735 print_constructor (pretty_printer
*buffer
, tree t
)
2737 tree decl_name
= DECL_NAME (DECL_ORIGIN (t
));
2739 pp_string (buffer
, "New_");
2740 pp_ada_tree_identifier (buffer
, decl_name
, t
, false);
2743 /* Dump in BUFFER destructor spec corresponding to T. */
2746 print_destructor (pretty_printer
*buffer
, tree t
)
2748 tree decl_name
= DECL_NAME (DECL_ORIGIN (t
));
2750 pp_string (buffer
, "Delete_");
2751 pp_ada_tree_identifier (buffer
, decl_name
, t
, false);
2754 /* Return the name of type T. */
2759 tree n
= TYPE_NAME (t
);
2761 if (TREE_CODE (n
) == IDENTIFIER_NODE
)
2762 return IDENTIFIER_POINTER (n
);
2764 return IDENTIFIER_POINTER (DECL_NAME (n
));
2767 /* Print in BUFFER the declaration of a variable T of type TYPE in Ada syntax.
2768 SPC is the indentation level. Return 1 if a declaration was printed,
2772 print_ada_declaration (pretty_printer
*buffer
, tree t
, tree type
, int spc
)
2774 int is_var
= 0, need_indent
= 0;
2775 int is_class
= false;
2776 tree name
= TYPE_NAME (TREE_TYPE (t
));
2777 tree decl_name
= DECL_NAME (t
);
2778 tree orig
= NULL_TREE
;
2780 if (cpp_check
&& cpp_check (t
, IS_TEMPLATE
))
2781 return dump_ada_template (buffer
, t
, spc
);
2783 if (TREE_CODE (t
) == CONST_DECL
&& TREE_CODE (TREE_TYPE (t
)) == ENUMERAL_TYPE
)
2784 /* Skip enumeral values: will be handled as part of the type itself. */
2787 if (TREE_CODE (t
) == TYPE_DECL
)
2789 orig
= DECL_ORIGINAL_TYPE (t
);
2791 if (orig
&& TYPE_STUB_DECL (orig
))
2793 tree stub
= TYPE_STUB_DECL (orig
);
2794 tree typ
= TREE_TYPE (stub
);
2796 if (TYPE_NAME (typ
))
2798 /* If types have same representation, and same name (ignoring
2799 casing), then ignore the second type. */
2800 if (type_name (typ
) == type_name (TREE_TYPE (t
))
2801 || !strcasecmp (type_name (typ
), type_name (TREE_TYPE (t
))))
2803 TREE_VISITED (t
) = 1;
2809 if (RECORD_OR_UNION_TYPE_P (typ
) && !TYPE_FIELDS (typ
))
2811 pp_string (buffer
, "-- skipped empty struct ");
2812 dump_generic_ada_node (buffer
, t
, type
, spc
, false, true);
2816 if (!TREE_VISITED (stub
)
2817 && DECL_SOURCE_FILE (stub
) == source_file_base
)
2818 dump_nested_types (buffer
, stub
, stub
, true, spc
);
2820 pp_string (buffer
, "subtype ");
2821 dump_generic_ada_node (buffer
, t
, type
, spc
, false, true);
2822 pp_string (buffer
, " is ");
2823 dump_generic_ada_node (buffer
, typ
, type
, spc
, false, true);
2824 pp_semicolon (buffer
);
2827 TREE_VISITED (t
) = 1;
2832 /* Skip unnamed or anonymous structs/unions/enum types. */
2833 if (!orig
&& !decl_name
&& !name
2834 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (t
))
2835 || TREE_CODE (TREE_TYPE (t
)) == ENUMERAL_TYPE
))
2838 /* Skip anonymous enum types (duplicates of real types). */
2840 && TREE_CODE (TREE_TYPE (t
)) == ENUMERAL_TYPE
2842 && (*IDENTIFIER_POINTER (decl_name
) == '.'
2843 || *IDENTIFIER_POINTER (decl_name
) == '$'))
2848 switch (TREE_CODE (TREE_TYPE (t
)))
2852 /* Skip empty structs (typically forward references to real
2854 if (!TYPE_FIELDS (TREE_TYPE (t
)))
2856 pp_string (buffer
, "-- skipped empty struct ");
2857 dump_generic_ada_node (buffer
, t
, type
, spc
, false, true);
2862 && (*IDENTIFIER_POINTER (decl_name
) == '.'
2863 || *IDENTIFIER_POINTER (decl_name
) == '$'))
2865 pp_string (buffer
, "-- skipped anonymous struct ");
2866 dump_generic_ada_node (buffer
, t
, type
, spc
, false, true);
2867 TREE_VISITED (t
) = 1;
2871 if (orig
&& TYPE_NAME (orig
) && orig
!= TREE_TYPE (t
))
2872 pp_string (buffer
, "subtype ");
2875 dump_nested_types (buffer
, t
, t
, false, spc
);
2877 if (separate_class_package (t
))
2880 pp_string (buffer
, "package Class_");
2881 dump_generic_ada_node (buffer
, t
, type
, spc
, false, true);
2882 pp_string (buffer
, " is");
2884 newline_and_indent (buffer
, spc
);
2887 pp_string (buffer
, "type ");
2893 case REFERENCE_TYPE
:
2894 if ((orig
&& TYPE_NAME (orig
) && orig
!= TREE_TYPE (t
))
2895 || is_char_array (t
))
2896 pp_string (buffer
, "subtype ");
2898 pp_string (buffer
, "type ");
2902 pp_string (buffer
, "-- skipped function type ");
2903 dump_generic_ada_node (buffer
, t
, type
, spc
, false, true);
2907 if ((orig
&& TYPE_NAME (orig
) && orig
!= TREE_TYPE (t
))
2908 || !is_simple_enum (TREE_TYPE (t
)))
2909 pp_string (buffer
, "subtype ");
2911 pp_string (buffer
, "type ");
2915 pp_string (buffer
, "subtype ");
2917 TREE_VISITED (t
) = 1;
2923 && *IDENTIFIER_POINTER (decl_name
) == '_')
2929 /* Print the type and name. */
2930 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
2935 /* Print variable's name. */
2936 dump_generic_ada_node (buffer
, t
, type
, spc
, false, true);
2938 if (TREE_CODE (t
) == TYPE_DECL
)
2940 pp_string (buffer
, " is ");
2942 if (orig
&& TYPE_NAME (orig
) && orig
!= TREE_TYPE (t
))
2943 dump_generic_ada_node
2944 (buffer
, TYPE_NAME (orig
), type
, spc
, false, true);
2946 dump_ada_array_type (buffer
, t
, type
, spc
);
2950 tree tmp
= TYPE_NAME (TREE_TYPE (t
));
2952 if (spc
== INDENT_INCR
|| TREE_STATIC (t
))
2955 pp_string (buffer
, " : ");
2957 if (TREE_CODE (TREE_TYPE (TREE_TYPE (t
))) != POINTER_TYPE
)
2958 pp_string (buffer
, "aliased ");
2961 dump_generic_ada_node (buffer
, tmp
, type
, spc
, false, true);
2963 dump_ada_double_name (buffer
, type
, t
);
2965 dump_ada_array_type (buffer
, t
, type
, spc
);
2968 else if (TREE_CODE (t
) == FUNCTION_DECL
)
2970 bool is_function
, is_abstract_class
= false;
2971 bool is_method
= TREE_CODE (TREE_TYPE (t
)) == METHOD_TYPE
;
2972 tree decl_name
= DECL_NAME (t
);
2973 bool is_abstract
= false;
2974 bool is_constructor
= false;
2975 bool is_destructor
= false;
2976 bool is_copy_constructor
= false;
2977 bool is_move_constructor
= false;
2984 is_abstract
= cpp_check (t
, IS_ABSTRACT
);
2985 is_constructor
= cpp_check (t
, IS_CONSTRUCTOR
);
2986 is_destructor
= cpp_check (t
, IS_DESTRUCTOR
);
2987 is_copy_constructor
= cpp_check (t
, IS_COPY_CONSTRUCTOR
);
2988 is_move_constructor
= cpp_check (t
, IS_MOVE_CONSTRUCTOR
);
2991 /* Skip copy constructors and C++11 move constructors: some are internal
2992 only and those that are not cannot be called easily from Ada. */
2993 if (is_copy_constructor
|| is_move_constructor
)
2996 if (is_constructor
|| is_destructor
)
2998 /* ??? Skip implicit constructors/destructors for now. */
2999 if (DECL_ARTIFICIAL (t
))
3002 /* Only consider constructors/destructors for complete objects. */
3003 if (strncmp (IDENTIFIER_POINTER (decl_name
), "__comp", 6) != 0)
3007 /* If this function has an entry in the vtable, we cannot omit it. */
3008 else if (!DECL_VINDEX (t
) && *IDENTIFIER_POINTER (decl_name
) == '_')
3011 pp_string (buffer
, "-- skipped func ");
3012 pp_string (buffer
, IDENTIFIER_POINTER (decl_name
));
3019 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (t
))) && !is_constructor
)
3021 pp_string (buffer
, "procedure ");
3022 is_function
= false;
3026 pp_string (buffer
, "function ");
3031 print_constructor (buffer
, t
);
3032 else if (is_destructor
)
3033 print_destructor (buffer
, t
);
3035 dump_ada_decl_name (buffer
, t
, false);
3037 dump_ada_function_declaration
3038 (buffer
, t
, is_method
, is_constructor
, is_destructor
, spc
);
3042 pp_string (buffer
, " return ");
3044 = is_constructor
? DECL_CONTEXT (t
) : TREE_TYPE (TREE_TYPE (t
));
3045 dump_generic_ada_node (buffer
, ret_type
, type
, spc
, false, true);
3049 && RECORD_OR_UNION_TYPE_P (type
)
3050 && TYPE_METHODS (type
))
3054 for (tmp
= TYPE_METHODS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
3055 if (cpp_check (tmp
, IS_ABSTRACT
))
3057 is_abstract_class
= true;
3062 if (is_abstract
|| is_abstract_class
)
3063 pp_string (buffer
, " is abstract");
3065 pp_semicolon (buffer
);
3066 pp_string (buffer
, " -- ");
3067 dump_sloc (buffer
, t
);
3069 if (is_abstract
|| !DECL_ASSEMBLER_NAME (t
))
3072 newline_and_indent (buffer
, spc
);
3076 pp_string (buffer
, "pragma CPP_Constructor (");
3077 print_constructor (buffer
, t
);
3078 pp_string (buffer
, ", \"");
3079 pp_asm_name (buffer
, t
);
3080 pp_string (buffer
, "\");");
3082 else if (is_destructor
)
3084 pp_string (buffer
, "pragma Import (CPP, ");
3085 print_destructor (buffer
, t
);
3086 pp_string (buffer
, ", \"");
3087 pp_asm_name (buffer
, t
);
3088 pp_string (buffer
, "\");");
3092 dump_ada_import (buffer
, t
);
3097 else if (TREE_CODE (t
) == TYPE_DECL
&& !DECL_ORIGINAL_TYPE (t
))
3099 int is_interface
= 0;
3100 int is_abstract_record
= 0;
3105 /* Anonymous structs/unions */
3106 dump_generic_ada_node (buffer
, TREE_TYPE (t
), t
, spc
, false, true);
3108 if (TREE_CODE (TREE_TYPE (t
)) == UNION_TYPE
)
3110 pp_string (buffer
, " (discr : unsigned := 0)");
3113 pp_string (buffer
, " is ");
3115 /* Check whether we have an Ada interface compatible class. */
3117 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t
))
3118 && TYPE_METHODS (TREE_TYPE (t
)))
3123 /* Check that there are no fields other than the virtual table. */
3124 for (tmp
= TYPE_FIELDS (TREE_TYPE (t
)); tmp
; tmp
= TREE_CHAIN (tmp
))
3126 if (TREE_CODE (tmp
) == TYPE_DECL
)
3131 if (num_fields
== 1)
3134 /* Also check that there are only pure virtual methods. Since the
3135 class is empty, we can skip implicit constructors/destructors. */
3136 for (tmp
= TYPE_METHODS (TREE_TYPE (t
)); tmp
; tmp
= TREE_CHAIN (tmp
))
3138 if (DECL_ARTIFICIAL (tmp
))
3140 if (cpp_check (tmp
, IS_ABSTRACT
))
3141 is_abstract_record
= 1;
3147 TREE_VISITED (t
) = 1;
3150 pp_string (buffer
, "limited interface; -- ");
3151 dump_sloc (buffer
, t
);
3152 newline_and_indent (buffer
, spc
);
3153 pp_string (buffer
, "pragma Import (CPP, ");
3154 dump_generic_ada_node
3155 (buffer
, TYPE_NAME (TREE_TYPE (t
)), type
, spc
, false, true);
3156 pp_right_paren (buffer
);
3158 print_ada_methods (buffer
, TREE_TYPE (t
), spc
);
3162 if (is_abstract_record
)
3163 pp_string (buffer
, "abstract ");
3164 dump_generic_ada_node (buffer
, t
, t
, spc
, false, false);
3172 if (TREE_CODE (t
) == FIELD_DECL
&& DECL_NAME (t
))
3173 check_name (buffer
, t
);
3175 /* Print variable/type's name. */
3176 dump_generic_ada_node (buffer
, t
, t
, spc
, false, true);
3178 if (TREE_CODE (t
) == TYPE_DECL
)
3180 tree orig
= DECL_ORIGINAL_TYPE (t
);
3181 int is_subtype
= orig
&& TYPE_NAME (orig
) && orig
!= TREE_TYPE (t
);
3183 if (!is_subtype
&& TREE_CODE (TREE_TYPE (t
)) == UNION_TYPE
)
3184 pp_string (buffer
, " (discr : unsigned := 0)");
3186 pp_string (buffer
, " is ");
3188 dump_generic_ada_node (buffer
, orig
, t
, spc
, false, is_subtype
);
3192 if (spc
== INDENT_INCR
|| TREE_STATIC (t
))
3195 pp_string (buffer
, " : ");
3197 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (t
)))
3199 pp_string (buffer
, "aliased ");
3201 if (TYPE_NAME (TREE_TYPE (t
)))
3202 dump_generic_ada_node
3203 (buffer
, TREE_TYPE (t
), t
, spc
, false, true);
3205 dump_ada_double_name (buffer
, type
, t
);
3209 if (TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
3210 && (TYPE_NAME (TREE_TYPE (t
))
3211 || TREE_CODE (TREE_TYPE (t
)) != INTEGER_TYPE
))
3212 pp_string (buffer
, "aliased ");
3214 dump_generic_ada_node
3215 (buffer
, TREE_TYPE (t
), TREE_TYPE (t
), spc
, false, true);
3223 newline_and_indent (buffer
, spc
);
3224 pp_string (buffer
, "end;");
3225 newline_and_indent (buffer
, spc
);
3226 pp_string (buffer
, "use Class_");
3227 dump_generic_ada_node (buffer
, t
, type
, spc
, false, true);
3228 pp_semicolon (buffer
);
3229 pp_newline (buffer
);
3231 /* All needed indentation/newline performed already, so return 0. */
3236 pp_string (buffer
, "; -- ");
3237 dump_sloc (buffer
, t
);
3242 newline_and_indent (buffer
, spc
);
3243 dump_ada_import (buffer
, t
);
3249 /* Prints in BUFFER a structure NODE of type TYPE: name, fields, and methods
3250 with Ada syntax. SPC is the indentation level. If DISPLAY_CONVENTION is
3251 true, also print the pragma Convention for NODE. */
3254 print_ada_struct_decl (pretty_printer
*buffer
, tree node
, tree type
, int spc
,
3255 bool display_convention
)
3258 const bool is_union
= (TREE_CODE (node
) == UNION_TYPE
);
3261 int field_spc
= spc
+ INDENT_INCR
;
3264 bitfield_used
= false;
3266 if (TYPE_FIELDS (node
))
3268 /* Print the contents of the structure. */
3269 pp_string (buffer
, "record");
3273 newline_and_indent (buffer
, spc
+ INDENT_INCR
);
3274 pp_string (buffer
, "case discr is");
3275 field_spc
= spc
+ INDENT_INCR
* 3;
3278 pp_newline (buffer
);
3280 /* Print the non-static fields of the structure. */
3281 for (tmp
= TYPE_FIELDS (node
); tmp
; tmp
= TREE_CHAIN (tmp
))
3283 /* Add parent field if needed. */
3284 if (!DECL_NAME (tmp
))
3286 if (!is_tagged_type (TREE_TYPE (tmp
)))
3288 if (!TYPE_NAME (TREE_TYPE (tmp
)))
3289 print_ada_declaration (buffer
, tmp
, type
, field_spc
);
3295 pp_string (buffer
, "parent : aliased ");
3298 sprintf (buf
, "field_%d : aliased ", field_num
+ 1);
3299 pp_string (buffer
, buf
);
3302 (buffer
, TYPE_NAME (TREE_TYPE (tmp
)), false);
3303 pp_semicolon (buffer
);
3305 pp_newline (buffer
);
3309 else if (TREE_CODE (tmp
) != TYPE_DECL
&& !TREE_STATIC (tmp
))
3311 /* Skip internal virtual table field. */
3312 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (tmp
)), "_vptr", 5))
3316 if (TREE_CHAIN (tmp
)
3317 && TREE_TYPE (TREE_CHAIN (tmp
)) != node
3318 && TREE_CODE (TREE_CHAIN (tmp
)) != TYPE_DECL
)
3319 sprintf (buf
, "when %d =>", field_num
);
3321 sprintf (buf
, "when others =>");
3323 INDENT (spc
+ INDENT_INCR
* 2);
3324 pp_string (buffer
, buf
);
3325 pp_newline (buffer
);
3328 if (print_ada_declaration (buffer
, tmp
, type
, field_spc
))
3330 pp_newline (buffer
);
3339 INDENT (spc
+ INDENT_INCR
);
3340 pp_string (buffer
, "end case;");
3341 pp_newline (buffer
);
3346 INDENT (spc
+ INDENT_INCR
);
3347 pp_string (buffer
, "null;");
3348 pp_newline (buffer
);
3352 pp_string (buffer
, "end record;");
3355 pp_string (buffer
, "null record;");
3357 newline_and_indent (buffer
, spc
);
3359 if (!display_convention
)
3362 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (type
)))
3364 if (has_nontrivial_methods (TREE_TYPE (type
)))
3365 pp_string (buffer
, "pragma Import (CPP, ");
3367 pp_string (buffer
, "pragma Convention (C_Pass_By_Copy, ");
3370 pp_string (buffer
, "pragma Convention (C, ");
3372 package_prefix
= false;
3373 dump_generic_ada_node (buffer
, TREE_TYPE (type
), type
, spc
, false, true);
3374 package_prefix
= true;
3375 pp_right_paren (buffer
);
3379 pp_semicolon (buffer
);
3380 newline_and_indent (buffer
, spc
);
3381 pp_string (buffer
, "pragma Unchecked_Union (");
3383 dump_generic_ada_node (buffer
, TREE_TYPE (type
), type
, spc
, false, true);
3384 pp_right_paren (buffer
);
3389 pp_semicolon (buffer
);
3390 newline_and_indent (buffer
, spc
);
3391 pp_string (buffer
, "pragma Pack (");
3392 dump_generic_ada_node
3393 (buffer
, TREE_TYPE (type
), type
, spc
, false, true);
3394 pp_right_paren (buffer
);
3395 bitfield_used
= false;
3398 need_semicolon
= !print_ada_methods (buffer
, node
, spc
);
3400 /* Print the static fields of the structure, if any. */
3401 for (tmp
= TYPE_FIELDS (node
); tmp
; tmp
= TREE_CHAIN (tmp
))
3403 if (DECL_NAME (tmp
) && TREE_STATIC (tmp
))
3407 need_semicolon
= false;
3408 pp_semicolon (buffer
);
3410 pp_newline (buffer
);
3411 pp_newline (buffer
);
3412 print_ada_declaration (buffer
, tmp
, type
, spc
);
3417 /* Dump all the declarations in SOURCE_FILE to an Ada spec.
3418 COLLECT_ALL_REFS is a front-end callback used to collect all relevant
3419 nodes for SOURCE_FILE. CHECK is used to perform C++ queries on nodes. */
3422 dump_ads (const char *source_file
,
3423 void (*collect_all_refs
)(const char *),
3424 int (*check
)(tree
, cpp_operation
))
3431 pkg_name
= get_ada_package (source_file
);
3433 /* Construct the .ads filename and package name. */
3434 ads_name
= xstrdup (pkg_name
);
3436 for (s
= ads_name
; *s
; s
++)
3442 ads_name
= reconcat (ads_name
, ads_name
, ".ads", NULL
);
3444 /* Write out the .ads file. */
3445 f
= fopen (ads_name
, "w");
3450 pp_needs_newline (&pp
) = true;
3451 pp
.buffer
->stream
= f
;
3453 /* Dump all relevant macros. */
3454 dump_ada_macros (&pp
, source_file
);
3456 /* Reset the table of withs for this file. */
3459 (*collect_all_refs
) (source_file
);
3461 /* Dump all references. */
3463 dump_ada_nodes (&pp
, source_file
);
3465 /* Requires Ada 2005 syntax, so generate corresponding pragma.
3466 Also, disable style checks since this file is auto-generated. */
3467 fprintf (f
, "pragma Ada_2005;\npragma Style_Checks (Off);\n\n");
3472 fprintf (f
, "\npackage %s is\n\n", pkg_name
);
3473 pp_write_text_to_stream (&pp
);
3474 /* ??? need to free pp */
3475 fprintf (f
, "end %s;\n", pkg_name
);
3483 static const char **source_refs
= NULL
;
3484 static int source_refs_used
= 0;
3485 static int source_refs_allocd
= 0;
3487 /* Add an entry for FILENAME to the table SOURCE_REFS. */
3490 collect_source_ref (const char *filename
)
3497 if (source_refs_allocd
== 0)
3499 source_refs_allocd
= 1024;
3500 source_refs
= XNEWVEC (const char *, source_refs_allocd
);
3503 for (i
= 0; i
< source_refs_used
; i
++)
3504 if (filename
== source_refs
[i
])
3507 if (source_refs_used
== source_refs_allocd
)
3509 source_refs_allocd
*= 2;
3510 source_refs
= XRESIZEVEC (const char *, source_refs
, source_refs_allocd
);
3513 source_refs
[source_refs_used
++] = filename
;
3516 /* Main entry point: dump all Ada specs corresponding to SOURCE_REFS
3517 using callbacks COLLECT_ALL_REFS and CHECK.
3518 COLLECT_ALL_REFS is a front-end callback used to collect all relevant
3519 nodes for a given source file.
3520 CHECK is used to perform C++ queries on nodes, or NULL for the C
3524 dump_ada_specs (void (*collect_all_refs
)(const char *),
3525 int (*check
)(tree
, cpp_operation
))
3529 /* Iterate over the list of files to dump specs for */
3530 for (i
= 0; i
< source_refs_used
; i
++)
3531 dump_ads (source_refs
[i
], collect_all_refs
, check
);
3533 /* Free files table. */