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 into PP a set of MAX_ADA_MACROS MACROS (C/C++) as Ada constants when
123 print_ada_macros (pretty_printer
*pp
, cpp_hashnode
**macros
, int max_ada_macros
)
125 int j
, num_macros
= 0, prev_line
= -1;
127 for (j
= 0; j
< max_ada_macros
; j
++)
129 cpp_hashnode
*node
= macros
[j
];
130 const cpp_macro
*macro
= node
->value
.macro
;
132 int supported
= 1, prev_is_one
= 0, buffer_len
, param_len
;
133 int is_string
= 0, is_char
= 0;
135 unsigned char *s
, *params
, *buffer
, *buf_param
, *char_one
= NULL
;
137 macro_length (macro
, &supported
, &buffer_len
, ¶m_len
);
138 s
= buffer
= XALLOCAVEC (unsigned char, buffer_len
);
139 params
= buf_param
= XALLOCAVEC (unsigned char, param_len
);
146 for (i
= 0; i
< macro
->paramc
; i
++)
148 cpp_hashnode
*param
= macro
->params
[i
];
150 memcpy (buf_param
, NODE_NAME (param
), NODE_LEN (param
));
151 buf_param
+= NODE_LEN (param
);
153 if (i
+ 1 < macro
->paramc
)
158 else if (macro
->variadic
)
168 for (i
= 0; supported
&& i
< macro
->count
; i
++)
170 cpp_token
*token
= ¯o
->exp
.tokens
[i
];
173 if (token
->flags
& PREV_WHITE
)
176 if (token
->flags
& STRINGIFY_ARG
|| token
->flags
& PASTE_LEFT
)
186 cpp_hashnode
*param
=
187 macro
->params
[token
->val
.macro_arg
.arg_no
- 1];
188 memcpy (buffer
, NODE_NAME (param
), NODE_LEN (param
));
189 buffer
+= NODE_LEN (param
);
193 case CPP_EQ_EQ
: *buffer
++ = '='; break;
194 case CPP_GREATER
: *buffer
++ = '>'; break;
195 case CPP_LESS
: *buffer
++ = '<'; break;
196 case CPP_PLUS
: *buffer
++ = '+'; break;
197 case CPP_MINUS
: *buffer
++ = '-'; break;
198 case CPP_MULT
: *buffer
++ = '*'; break;
199 case CPP_DIV
: *buffer
++ = '/'; break;
200 case CPP_COMMA
: *buffer
++ = ','; break;
201 case CPP_OPEN_SQUARE
:
202 case CPP_OPEN_PAREN
: *buffer
++ = '('; break;
203 case CPP_CLOSE_SQUARE
: /* fallthrough */
204 case CPP_CLOSE_PAREN
: *buffer
++ = ')'; break;
205 case CPP_DEREF
: /* fallthrough */
206 case CPP_SCOPE
: /* fallthrough */
207 case CPP_DOT
: *buffer
++ = '.'; break;
209 case CPP_EQ
: *buffer
++ = ':'; *buffer
++ = '='; break;
210 case CPP_NOT_EQ
: *buffer
++ = '/'; *buffer
++ = '='; break;
211 case CPP_GREATER_EQ
: *buffer
++ = '>'; *buffer
++ = '='; break;
212 case CPP_LESS_EQ
: *buffer
++ = '<'; *buffer
++ = '='; break;
215 *buffer
++ = 'n'; *buffer
++ = 'o'; *buffer
++ = 't'; break;
217 *buffer
++ = 'm'; *buffer
++ = 'o'; *buffer
++ = 'd'; break;
219 *buffer
++ = 'a'; *buffer
++ = 'n'; *buffer
++ = 'd'; break;
221 *buffer
++ = 'o'; *buffer
++ = 'r'; break;
223 *buffer
++ = 'x'; *buffer
++ = 'o'; *buffer
++ = 'r'; break;
225 strcpy ((char *) buffer
, " and then ");
229 strcpy ((char *) buffer
, " or else ");
235 is_one
= prev_is_one
;
238 case CPP_COMMENT
: break;
251 if (!macro
->fun_like
)
254 buffer
= cpp_spell_token (parse_in
, token
, buffer
, false);
264 c
= cpp_interpret_charconst (parse_in
, token
,
265 &chars_seen
, &ignored
);
266 if (c
>= 32 && c
<= 126)
269 *buffer
++ = (char) c
;
275 ((char *) buffer
, "Character'Val (%d)", (int) c
);
276 buffer
+= chars_seen
;
284 /* Replace "1 << N" by "2 ** N" */
311 case CPP_CLOSE_BRACE
:
315 case CPP_MINUS_MINUS
:
319 case CPP_HEADER_NAME
:
322 case CPP_OBJC_STRING
:
324 if (!macro
->fun_like
)
327 buffer
= cpp_spell_token (parse_in
, token
, buffer
, false);
331 prev_is_one
= is_one
;
338 if (macro
->fun_like
&& supported
)
340 char *start
= (char *) s
;
343 pp_string (pp
, " -- arg-macro: ");
345 if (*start
== '(' && buffer
[-1] == ')')
350 pp_string (pp
, "function ");
354 pp_string (pp
, "procedure ");
357 pp_string (pp
, (const char *) NODE_NAME (node
));
359 pp_string (pp
, (char *) params
);
361 pp_string (pp
, " -- ");
365 pp_string (pp
, "return ");
366 pp_string (pp
, start
);
370 pp_string (pp
, start
);
376 expanded_location sloc
= expand_location (macro
->line
);
378 if (sloc
.line
!= prev_line
+ 1 && prev_line
> 0)
382 prev_line
= sloc
.line
;
385 ada_name
= to_ada_name ((const char *) NODE_NAME (node
), NULL
);
386 pp_string (pp
, ada_name
);
388 pp_string (pp
, " : ");
391 pp_string (pp
, "aliased constant String");
393 pp_string (pp
, "aliased constant Character");
395 pp_string (pp
, "constant");
397 pp_string (pp
, " := ");
398 pp_string (pp
, (char *) s
);
401 pp_string (pp
, " & ASCII.NUL");
403 pp_string (pp
, "; -- ");
404 pp_string (pp
, sloc
.file
);
406 pp_scalar (pp
, "%d", sloc
.line
);
411 pp_string (pp
, " -- unsupported macro: ");
412 pp_string (pp
, (const char *) cpp_macro_definition (parse_in
, node
));
421 static const char *source_file
;
422 static int max_ada_macros
;
424 /* Callback used to count the number of relevant macros from
425 cpp_forall_identifiers. PFILE and V are not used. NODE is the current macro
429 count_ada_macro (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*node
,
430 void *v ATTRIBUTE_UNUSED
)
432 const cpp_macro
*macro
= node
->value
.macro
;
434 if (node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
)
436 && *NODE_NAME (node
) != '_'
437 && LOCATION_FILE (macro
->line
) == source_file
)
443 static int store_ada_macro_index
;
445 /* Callback used to store relevant macros from cpp_forall_identifiers.
446 PFILE is not used. NODE is the current macro to store if relevant.
447 MACROS is an array of cpp_hashnode* used to store NODE. */
450 store_ada_macro (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
451 cpp_hashnode
*node
, void *macros
)
453 const cpp_macro
*macro
= node
->value
.macro
;
455 if (node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
)
457 && *NODE_NAME (node
) != '_'
458 && LOCATION_FILE (macro
->line
) == source_file
)
459 ((cpp_hashnode
**) macros
)[store_ada_macro_index
++] = node
;
464 /* Callback used to compare (during qsort) macros. NODE1 and NODE2 are the
465 two macro nodes to compare. */
468 compare_macro (const void *node1
, const void *node2
)
470 typedef const cpp_hashnode
*const_hnode
;
472 const_hnode n1
= *(const const_hnode
*) node1
;
473 const_hnode n2
= *(const const_hnode
*) node2
;
475 return n1
->value
.macro
->line
- n2
->value
.macro
->line
;
478 /* Dump in PP all relevant macros appearing in FILE. */
481 dump_ada_macros (pretty_printer
*pp
, const char* file
)
483 cpp_hashnode
**macros
;
485 /* Initialize file-scope variables. */
487 store_ada_macro_index
= 0;
490 /* Count all potentially relevant macros, and then sort them by sloc. */
491 cpp_forall_identifiers (parse_in
, count_ada_macro
, NULL
);
492 macros
= XALLOCAVEC (cpp_hashnode
*, max_ada_macros
);
493 cpp_forall_identifiers (parse_in
, store_ada_macro
, macros
);
494 qsort (macros
, max_ada_macros
, sizeof (cpp_hashnode
*), compare_macro
);
496 print_ada_macros (pp
, macros
, max_ada_macros
);
499 /* Current source file being handled. */
501 static const char *source_file_base
;
503 /* Return sloc of DECL, using sloc of last field if LAST is true. */
506 decl_sloc (const_tree decl
, bool last
)
510 /* Compare the declaration of struct-like types based on the sloc of their
511 last field (if LAST is true), so that more nested types collate before
513 if (TREE_CODE (decl
) == TYPE_DECL
514 && !DECL_ORIGINAL_TYPE (decl
)
515 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl
))
516 && (field
= TYPE_FIELDS (TREE_TYPE (decl
))))
519 while (DECL_CHAIN (field
))
520 field
= DECL_CHAIN (field
);
521 return DECL_SOURCE_LOCATION (field
);
524 return DECL_SOURCE_LOCATION (decl
);
527 /* Compare two locations LHS and RHS. */
530 compare_location (location_t lhs
, location_t rhs
)
532 expanded_location xlhs
= expand_location (lhs
);
533 expanded_location xrhs
= expand_location (rhs
);
535 if (xlhs
.file
!= xrhs
.file
)
536 return filename_cmp (xlhs
.file
, xrhs
.file
);
538 if (xlhs
.line
!= xrhs
.line
)
539 return xlhs
.line
- xrhs
.line
;
541 if (xlhs
.column
!= xrhs
.column
)
542 return xlhs
.column
- xrhs
.column
;
547 /* Compare two declarations (LP and RP) by their source location. */
550 compare_node (const void *lp
, const void *rp
)
552 const_tree lhs
= *((const tree
*) lp
);
553 const_tree rhs
= *((const tree
*) rp
);
555 return compare_location (decl_sloc (lhs
, true), decl_sloc (rhs
, true));
558 /* Compare two comments (LP and RP) by their source location. */
561 compare_comment (const void *lp
, const void *rp
)
563 const cpp_comment
*lhs
= (const cpp_comment
*) lp
;
564 const cpp_comment
*rhs
= (const cpp_comment
*) rp
;
566 return compare_location (lhs
->sloc
, rhs
->sloc
);
569 static tree
*to_dump
= NULL
;
570 static int to_dump_count
= 0;
572 /* Collect a list of declarations from T relevant to SOURCE_FILE to be dumped
573 by a subsequent call to dump_ada_nodes. */
576 collect_ada_nodes (tree t
, const char *source_file
)
579 int i
= to_dump_count
;
581 /* Count the likely relevant nodes: do not dump builtins (they are irrelevant
582 in the context of bindings) and namespaces (we do not handle them properly
584 for (n
= t
; n
; n
= TREE_CHAIN (n
))
585 if (!DECL_IS_BUILTIN (n
)
586 && TREE_CODE (n
) != NAMESPACE_DECL
587 && LOCATION_FILE (decl_sloc (n
, false)) == source_file
)
590 /* Allocate sufficient storage for all nodes. */
591 to_dump
= XRESIZEVEC (tree
, to_dump
, to_dump_count
);
593 /* Store the relevant nodes. */
594 for (n
= t
; n
; n
= TREE_CHAIN (n
))
595 if (!DECL_IS_BUILTIN (n
)
596 && TREE_CODE (n
) != NAMESPACE_DECL
597 && LOCATION_FILE (decl_sloc (n
, false)) == source_file
)
601 /* Call back for walk_tree to clear the TREE_VISITED flag of TP. */
604 unmark_visited_r (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
605 void *data ATTRIBUTE_UNUSED
)
607 if (TREE_VISITED (*tp
))
608 TREE_VISITED (*tp
) = 0;
615 /* Dump nodes into PP relevant to SOURCE_FILE, as collected by previous calls
616 to collect_ada_nodes. */
619 dump_ada_nodes (pretty_printer
*pp
, const char *source_file
)
622 cpp_comment_table
*comments
;
624 /* Sort the table of declarations to dump by sloc. */
625 qsort (to_dump
, to_dump_count
, sizeof (tree
), compare_node
);
627 /* Fetch the table of comments. */
628 comments
= cpp_get_comments (parse_in
);
630 /* Sort the comments table by sloc. */
631 if (comments
->count
> 1)
632 qsort (comments
->entries
, comments
->count
, sizeof (cpp_comment
),
635 /* Interleave comments and declarations in line number order. */
639 /* Advance j until comment j is in this file. */
640 while (j
!= comments
->count
641 && LOCATION_FILE (comments
->entries
[j
].sloc
) != source_file
)
644 /* Advance j until comment j is not a duplicate. */
645 while (j
< comments
->count
- 1
646 && !compare_comment (&comments
->entries
[j
],
647 &comments
->entries
[j
+ 1]))
650 /* Write decls until decl i collates after comment j. */
651 while (i
!= to_dump_count
)
653 if (j
== comments
->count
654 || LOCATION_LINE (decl_sloc (to_dump
[i
], false))
655 < LOCATION_LINE (comments
->entries
[j
].sloc
))
656 print_generic_ada_decl (pp
, to_dump
[i
++], source_file
);
661 /* Write comment j, if there is one. */
662 if (j
!= comments
->count
)
663 print_comment (pp
, comments
->entries
[j
++].comment
);
665 } while (i
!= to_dump_count
|| j
!= comments
->count
);
667 /* Clear the TREE_VISITED flag over each subtree we've dumped. */
668 for (i
= 0; i
< to_dump_count
; i
++)
669 walk_tree (&to_dump
[i
], unmark_visited_r
, NULL
, NULL
);
671 /* Finalize the to_dump table. */
680 /* Print a COMMENT to the output stream PP. */
683 print_comment (pretty_printer
*pp
, const char *comment
)
685 int len
= strlen (comment
);
686 char *str
= XALLOCAVEC (char, len
+ 1);
688 bool extra_newline
= false;
690 memcpy (str
, comment
, len
+ 1);
692 /* Trim C/C++ comment indicators. */
693 if (str
[len
- 2] == '*' && str
[len
- 1] == '/')
700 tok
= strtok (str
, "\n");
702 pp_string (pp
, " --");
705 tok
= strtok (NULL
, "\n");
707 /* Leave a blank line after multi-line comments. */
709 extra_newline
= true;
716 /* Print declaration DECL to PP in Ada syntax. The current source file being
717 handled is SOURCE_FILE. */
720 print_generic_ada_decl (pretty_printer
*pp
, tree decl
, const char *source_file
)
722 source_file_base
= source_file
;
724 if (print_ada_declaration (pp
, decl
, 0, INDENT_INCR
))
731 /* Dump a newline and indent BUFFER by SPC chars. */
734 newline_and_indent (pretty_printer
*buffer
, int spc
)
740 struct with
{ char *s
; const char *in_file
; int limited
; };
741 static struct with
*withs
= NULL
;
742 static int withs_max
= 4096;
743 static int with_len
= 0;
745 /* Record a "with" clause on package S (a limited with if LIMITED_ACCESS is
746 true), if not already done. */
749 append_withs (const char *s
, int limited_access
)
754 withs
= XNEWVEC (struct with
, withs_max
);
756 if (with_len
== withs_max
)
759 withs
= XRESIZEVEC (struct with
, withs
, withs_max
);
762 for (i
= 0; i
< with_len
; i
++)
763 if (!strcmp (s
, withs
[i
].s
)
764 && source_file_base
== withs
[i
].in_file
)
766 withs
[i
].limited
&= limited_access
;
770 withs
[with_len
].s
= xstrdup (s
);
771 withs
[with_len
].in_file
= source_file_base
;
772 withs
[with_len
].limited
= limited_access
;
776 /* Reset "with" clauses. */
779 reset_ada_withs (void)
786 for (i
= 0; i
< with_len
; i
++)
794 /* Dump "with" clauses in F. */
797 dump_ada_withs (FILE *f
)
801 fprintf (f
, "with Interfaces.C; use Interfaces.C;\n");
803 for (i
= 0; i
< with_len
; i
++)
805 (f
, "%swith %s;\n", withs
[i
].limited
? "limited " : "", withs
[i
].s
);
808 /* Return suitable Ada package name from FILE. */
811 get_ada_package (const char *file
)
819 s
= strstr (file
, "/include/");
823 base
= lbasename (file
);
825 if (ada_specs_parent
== NULL
)
828 plen
= strlen (ada_specs_parent
) + 1;
830 res
= XNEWVEC (char, plen
+ strlen (base
) + 1);
831 if (ada_specs_parent
!= NULL
) {
832 strcpy (res
, ada_specs_parent
);
836 for (i
= plen
; *base
; base
++, i
++)
848 res
[i
] = (i
== 0 || res
[i
- 1] == '.' || res
[i
- 1] == '_') ? 'u' : '_';
860 static const char *ada_reserved
[] = {
861 "abort", "abs", "abstract", "accept", "access", "aliased", "all", "and",
862 "array", "at", "begin", "body", "case", "constant", "declare", "delay",
863 "delta", "digits", "do", "else", "elsif", "end", "entry", "exception",
864 "exit", "for", "function", "generic", "goto", "if", "in", "interface", "is",
865 "limited", "loop", "mod", "new", "not", "null", "others", "out", "of", "or",
866 "overriding", "package", "pragma", "private", "procedure", "protected",
867 "raise", "range", "record", "rem", "renames", "requeue", "return", "reverse",
868 "select", "separate", "subtype", "synchronized", "tagged", "task",
869 "terminate", "then", "type", "until", "use", "when", "while", "with", "xor",
872 /* ??? would be nice to specify this list via a config file, so that users
873 can create their own dictionary of conflicts. */
874 static const char *c_duplicates
[] = {
875 /* system will cause troubles with System.Address. */
878 /* The following values have other definitions with same name/other
884 "rl_readline_version",
890 /* Return a declaration tree corresponding to TYPE. */
893 get_underlying_decl (tree type
)
895 tree decl
= NULL_TREE
;
897 if (type
== NULL_TREE
)
900 /* type is a declaration. */
904 /* type is a typedef. */
905 if (TYPE_P (type
) && TYPE_NAME (type
) && DECL_P (TYPE_NAME (type
)))
906 decl
= TYPE_NAME (type
);
908 /* TYPE_STUB_DECL has been set for type. */
909 if (TYPE_P (type
) && TYPE_STUB_DECL (type
) &&
910 DECL_P (TYPE_STUB_DECL (type
)))
911 decl
= TYPE_STUB_DECL (type
);
916 /* Return whether TYPE has static fields. */
919 has_static_fields (const_tree type
)
923 if (!type
|| !RECORD_OR_UNION_TYPE_P (type
))
926 for (tmp
= TYPE_FIELDS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
927 if (DECL_NAME (tmp
) && TREE_STATIC (tmp
))
933 /* Return whether TYPE corresponds to an Ada tagged type (has a dispatch
937 is_tagged_type (const_tree type
)
941 if (!type
|| !RECORD_OR_UNION_TYPE_P (type
))
944 /* TYPE_METHODS is only set on the main variant. */
945 type
= TYPE_MAIN_VARIANT (type
);
947 for (tmp
= TYPE_METHODS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
948 if (TREE_CODE (tmp
) == FUNCTION_DECL
&& DECL_VINDEX (tmp
))
954 /* Return whether TYPE has non-trivial methods, i.e. methods that do something
955 for the objects of TYPE. In C++, all classes have implicit special methods,
956 e.g. constructors and destructors, but they can be trivial if the type is
957 sufficiently simple. */
960 has_nontrivial_methods (tree type
)
964 if (!type
|| !RECORD_OR_UNION_TYPE_P (type
))
967 /* Only C++ types can have methods. */
971 /* A non-trivial type has non-trivial special methods. */
972 if (!cpp_check (type
, IS_TRIVIAL
))
975 /* TYPE_METHODS is only set on the main variant. */
976 type
= TYPE_MAIN_VARIANT (type
);
978 /* If there are user-defined methods, they are deemed non-trivial. */
979 for (tmp
= TYPE_METHODS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
980 if (!DECL_ARTIFICIAL (tmp
))
986 /* Generate a legal Ada name from a C NAME, returning a malloc'd string.
987 SPACE_FOUND, if not NULL, is used to indicate whether a space was found in
991 to_ada_name (const char *name
, int *space_found
)
994 int len
= strlen (name
);
997 char *s
= XNEWVEC (char, len
* 2 + 5);
1001 *space_found
= false;
1003 /* Add trailing "c_" if name is an Ada reserved word. */
1004 for (names
= ada_reserved
; *names
; names
++)
1005 if (!strcasecmp (name
, *names
))
1014 /* Add trailing "c_" if name is an potential case sensitive duplicate. */
1015 for (names
= c_duplicates
; *names
; names
++)
1016 if (!strcmp (name
, *names
))
1024 for (j
= 0; name
[j
] == '_'; j
++)
1029 else if (*name
== '.' || *name
== '$')
1039 /* Replace unsuitable characters for Ada identifiers. */
1041 for (; j
< len
; j
++)
1046 *space_found
= true;
1050 /* ??? missing some C++ operators. */
1054 if (name
[j
+ 1] == '=')
1069 if (name
[j
+ 1] == '=')
1087 s
[len2
++] = name
[j
] == '&' ? 'a' : name
[j
] == '|' ? 'o' : 'x';
1089 if (name
[j
+ 1] == '=')
1102 if (s
[len2
- 1] != '_')
1105 switch (name
[j
+ 1]) {
1108 switch (name
[j
- 1]) {
1109 case '+': s
[len2
++] = 'p'; break; /* + */
1110 case '-': s
[len2
++] = 'm'; break; /* - */
1111 case '*': s
[len2
++] = 't'; break; /* * */
1112 case '/': s
[len2
++] = 'd'; break; /* / */
1118 switch (name
[j
- 1]) {
1119 case '+': s
[len2
++] = 'p'; break; /* += */
1120 case '-': s
[len2
++] = 'm'; break; /* -= */
1121 case '*': s
[len2
++] = 't'; break; /* *= */
1122 case '/': s
[len2
++] = 'd'; break; /* /= */
1156 c
= name
[j
] == '<' ? 'l' : 'g';
1159 switch (name
[j
+ 1]) {
1185 if (len2
&& s
[len2
- 1] == '_')
1190 s
[len2
++] = name
[j
];
1193 if (s
[len2
- 1] == '_')
1201 /* Return true if DECL refers to a C++ class type for which a
1202 separate enclosing package has been or should be generated. */
1205 separate_class_package (tree decl
)
1207 tree type
= TREE_TYPE (decl
);
1208 return has_nontrivial_methods (type
) || has_static_fields (type
);
1211 static bool package_prefix
= true;
1213 /* Dump in BUFFER the name of an identifier NODE of type TYPE, following Ada
1214 syntax. LIMITED_ACCESS indicates whether NODE can be accessed via a limited
1215 'with' clause rather than a regular 'with' clause. */
1218 pp_ada_tree_identifier (pretty_printer
*buffer
, tree node
, tree type
,
1221 const char *name
= IDENTIFIER_POINTER (node
);
1222 int space_found
= false;
1223 char *s
= to_ada_name (name
, &space_found
);
1226 /* If the entity is a type and comes from another file, generate "package"
1228 decl
= get_underlying_decl (type
);
1232 expanded_location xloc
= expand_location (decl_sloc (decl
, false));
1234 if (xloc
.file
&& xloc
.line
)
1236 if (xloc
.file
!= source_file_base
)
1238 switch (TREE_CODE (type
))
1243 case FIXED_POINT_TYPE
:
1245 case REFERENCE_TYPE
:
1253 char *s1
= get_ada_package (xloc
.file
);
1254 append_withs (s1
, limited_access
);
1255 pp_string (buffer
, s1
);
1264 /* Generate the additional package prefix for C++ classes. */
1265 if (separate_class_package (decl
))
1267 pp_string (buffer
, "Class_");
1268 pp_string (buffer
, s
);
1276 if (!strcmp (s
, "short_int"))
1277 pp_string (buffer
, "short");
1278 else if (!strcmp (s
, "short_unsigned_int"))
1279 pp_string (buffer
, "unsigned_short");
1280 else if (!strcmp (s
, "unsigned_int"))
1281 pp_string (buffer
, "unsigned");
1282 else if (!strcmp (s
, "long_int"))
1283 pp_string (buffer
, "long");
1284 else if (!strcmp (s
, "long_unsigned_int"))
1285 pp_string (buffer
, "unsigned_long");
1286 else if (!strcmp (s
, "long_long_int"))
1287 pp_string (buffer
, "Long_Long_Integer");
1288 else if (!strcmp (s
, "long_long_unsigned_int"))
1292 append_withs ("Interfaces.C.Extensions", false);
1293 pp_string (buffer
, "Extensions.unsigned_long_long");
1296 pp_string (buffer
, "unsigned_long_long");
1299 pp_string(buffer
, s
);
1301 if (!strcmp (s
, "bool"))
1305 append_withs ("Interfaces.C.Extensions", false);
1306 pp_string (buffer
, "Extensions.bool");
1309 pp_string (buffer
, "bool");
1312 pp_string(buffer
, s
);
1317 /* Dump in BUFFER the assembly name of T. */
1320 pp_asm_name (pretty_printer
*buffer
, tree t
)
1322 tree name
= DECL_ASSEMBLER_NAME (t
);
1323 char *ada_name
= XALLOCAVEC (char, IDENTIFIER_LENGTH (name
) + 1), *s
;
1324 const char *ident
= IDENTIFIER_POINTER (name
);
1326 for (s
= ada_name
; *ident
; ident
++)
1330 else if (*ident
!= '*')
1335 pp_string (buffer
, ada_name
);
1338 /* Dump in BUFFER the name of a DECL node if set, following Ada syntax.
1339 LIMITED_ACCESS indicates whether NODE can be accessed via a limited
1340 'with' clause rather than a regular 'with' clause. */
1343 dump_ada_decl_name (pretty_printer
*buffer
, tree decl
, int limited_access
)
1345 if (DECL_NAME (decl
))
1346 pp_ada_tree_identifier (buffer
, DECL_NAME (decl
), decl
, limited_access
);
1349 tree type_name
= TYPE_NAME (TREE_TYPE (decl
));
1353 pp_string (buffer
, "anon");
1354 if (TREE_CODE (decl
) == FIELD_DECL
)
1355 pp_scalar (buffer
, "%d", DECL_UID (decl
));
1357 pp_scalar (buffer
, "%d", TYPE_UID (TREE_TYPE (decl
)));
1359 else if (TREE_CODE (type_name
) == IDENTIFIER_NODE
)
1360 pp_ada_tree_identifier (buffer
, type_name
, decl
, limited_access
);
1364 /* Dump in BUFFER a name based on both T1 and T2 followed by a suffix. */
1367 dump_ada_double_name (pretty_printer
*buffer
, tree t1
, tree t2
)
1370 pp_ada_tree_identifier (buffer
, DECL_NAME (t1
), t1
, false);
1373 pp_string (buffer
, "anon");
1374 pp_scalar (buffer
, "%d", TYPE_UID (TREE_TYPE (t1
)));
1377 pp_underscore (buffer
);
1380 pp_ada_tree_identifier (buffer
, DECL_NAME (t2
), t2
, false);
1383 pp_string (buffer
, "anon");
1384 pp_scalar (buffer
, "%d", TYPE_UID (TREE_TYPE (t2
)));
1387 switch (TREE_CODE (TREE_TYPE (t2
)))
1390 pp_string (buffer
, "_array");
1393 pp_string (buffer
, "_struct");
1396 pp_string (buffer
, "_union");
1399 pp_string (buffer
, "_unknown");
1404 /* Dump in BUFFER pragma Import C/CPP on a given node T. */
1407 dump_ada_import (pretty_printer
*buffer
, tree t
)
1409 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t
));
1410 int is_stdcall
= TREE_CODE (t
) == FUNCTION_DECL
&&
1411 lookup_attribute ("stdcall", TYPE_ATTRIBUTES (TREE_TYPE (t
)));
1414 pp_string (buffer
, "pragma Import (Stdcall, ");
1415 else if (name
[0] == '_' && name
[1] == 'Z')
1416 pp_string (buffer
, "pragma Import (CPP, ");
1418 pp_string (buffer
, "pragma Import (C, ");
1420 dump_ada_decl_name (buffer
, t
, false);
1421 pp_string (buffer
, ", \"");
1424 pp_string (buffer
, IDENTIFIER_POINTER (DECL_NAME (t
)));
1426 pp_asm_name (buffer
, t
);
1428 pp_string (buffer
, "\");");
1431 /* Check whether T and its type have different names, and append "the_"
1432 otherwise in BUFFER. */
1435 check_name (pretty_printer
*buffer
, tree t
)
1438 tree tmp
= TREE_TYPE (t
);
1440 while (TREE_CODE (tmp
) == POINTER_TYPE
&& !TYPE_NAME (tmp
))
1441 tmp
= TREE_TYPE (tmp
);
1443 if (TREE_CODE (tmp
) != FUNCTION_TYPE
)
1445 if (TREE_CODE (tmp
) == IDENTIFIER_NODE
)
1446 s
= IDENTIFIER_POINTER (tmp
);
1447 else if (!TYPE_NAME (tmp
))
1449 else if (TREE_CODE (TYPE_NAME (tmp
)) == IDENTIFIER_NODE
)
1450 s
= IDENTIFIER_POINTER (TYPE_NAME (tmp
));
1452 s
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (tmp
)));
1454 if (!strcasecmp (IDENTIFIER_POINTER (DECL_NAME (t
)), s
))
1455 pp_string (buffer
, "the_");
1459 /* Dump in BUFFER a function declaration FUNC with Ada syntax.
1460 IS_METHOD indicates whether FUNC is a C++ method.
1461 IS_CONSTRUCTOR whether FUNC is a C++ constructor.
1462 IS_DESTRUCTOR whether FUNC is a C++ destructor.
1463 SPC is the current indentation level. */
1466 dump_ada_function_declaration (pretty_printer
*buffer
, tree func
,
1467 int is_method
, int is_constructor
,
1468 int is_destructor
, int spc
)
1471 const tree node
= TREE_TYPE (func
);
1473 int num
= 0, num_args
= 0, have_args
= true, have_ellipsis
= false;
1475 /* Compute number of arguments. */
1476 arg
= TYPE_ARG_TYPES (node
);
1480 while (TREE_CHAIN (arg
) && arg
!= error_mark_node
)
1483 arg
= TREE_CHAIN (arg
);
1486 if (TREE_CODE (TREE_VALUE (arg
)) != VOID_TYPE
)
1489 have_ellipsis
= true;
1500 newline_and_indent (buffer
, spc
+ 1);
1505 pp_left_paren (buffer
);
1508 if (TREE_CODE (func
) == FUNCTION_DECL
)
1509 arg
= DECL_ARGUMENTS (func
);
1513 if (arg
== NULL_TREE
)
1516 arg
= TYPE_ARG_TYPES (node
);
1518 if (arg
&& TREE_CODE (TREE_VALUE (arg
)) == VOID_TYPE
)
1523 arg
= TREE_CHAIN (arg
);
1525 /* Print the argument names (if available) & types. */
1527 for (num
= 1; num
<= num_args
; num
++)
1531 if (DECL_NAME (arg
))
1533 check_name (buffer
, arg
);
1534 pp_ada_tree_identifier (buffer
, DECL_NAME (arg
), 0, false);
1535 pp_string (buffer
, " : ");
1539 sprintf (buf
, "arg%d : ", num
);
1540 pp_string (buffer
, buf
);
1543 dump_generic_ada_node (buffer
, TREE_TYPE (arg
), node
, spc
, 0, true);
1547 sprintf (buf
, "arg%d : ", num
);
1548 pp_string (buffer
, buf
);
1549 dump_generic_ada_node (buffer
, TREE_VALUE (arg
), node
, spc
, 0, true);
1552 if (TREE_TYPE (arg
) && TREE_TYPE (TREE_TYPE (arg
))
1553 && is_tagged_type (TREE_TYPE (TREE_TYPE (arg
))))
1556 || (num
!= 1 || (!DECL_VINDEX (func
) && !is_constructor
)))
1557 pp_string (buffer
, "'Class");
1560 arg
= TREE_CHAIN (arg
);
1564 pp_semicolon (buffer
);
1567 newline_and_indent (buffer
, spc
+ INDENT_INCR
);
1575 pp_string (buffer
, " -- , ...");
1576 newline_and_indent (buffer
, spc
+ INDENT_INCR
);
1580 pp_right_paren (buffer
);
1584 /* Dump in BUFFER all the domains associated with an array NODE,
1585 using Ada syntax. SPC is the current indentation level. */
1588 dump_ada_array_domains (pretty_printer
*buffer
, tree node
, int spc
)
1591 pp_left_paren (buffer
);
1593 for (; TREE_CODE (node
) == ARRAY_TYPE
; node
= TREE_TYPE (node
))
1595 tree domain
= TYPE_DOMAIN (node
);
1599 tree min
= TYPE_MIN_VALUE (domain
);
1600 tree max
= TYPE_MAX_VALUE (domain
);
1603 pp_string (buffer
, ", ");
1607 dump_generic_ada_node (buffer
, min
, NULL_TREE
, spc
, 0, true);
1608 pp_string (buffer
, " .. ");
1610 /* If the upper bound is zero, gcc may generate a NULL_TREE
1611 for TYPE_MAX_VALUE rather than an integer_cst. */
1613 dump_generic_ada_node (buffer
, max
, NULL_TREE
, spc
, 0, true);
1615 pp_string (buffer
, "0");
1618 pp_string (buffer
, "size_t");
1620 pp_right_paren (buffer
);
1623 /* Dump in BUFFER file:line information related to NODE. */
1626 dump_sloc (pretty_printer
*buffer
, tree node
)
1628 expanded_location xloc
;
1633 xloc
= expand_location (DECL_SOURCE_LOCATION (node
));
1634 else if (EXPR_HAS_LOCATION (node
))
1635 xloc
= expand_location (EXPR_LOCATION (node
));
1639 pp_string (buffer
, xloc
.file
);
1641 pp_decimal_int (buffer
, xloc
.line
);
1645 /* Return true if T designates a one dimension array of "char". */
1648 is_char_array (tree t
)
1653 /* Retrieve array's type. */
1655 while (TREE_CODE (TREE_TYPE (tmp
)) == ARRAY_TYPE
)
1658 tmp
= TREE_TYPE (tmp
);
1661 tmp
= TREE_TYPE (tmp
);
1662 return num_dim
== 1 && TREE_CODE (tmp
) == INTEGER_TYPE
1663 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (tmp
))), "char");
1666 /* Dump in BUFFER an array type T in Ada syntax. Assume that the "type"
1667 keyword and name have already been printed. PARENT is the parent node of T.
1668 SPC is the indentation level. */
1671 dump_ada_array_type (pretty_printer
*buffer
, tree t
, tree parent
, int spc
)
1673 const bool char_array
= is_char_array (t
);
1676 /* Special case char arrays. */
1679 pp_string (buffer
, "Interfaces.C.char_array ");
1682 pp_string (buffer
, "array ");
1684 /* Print the dimensions. */
1685 dump_ada_array_domains (buffer
, TREE_TYPE (t
), spc
);
1687 /* Retrieve the element type. */
1688 tmp
= TREE_TYPE (t
);
1689 while (TREE_CODE (tmp
) == ARRAY_TYPE
)
1690 tmp
= TREE_TYPE (tmp
);
1692 /* Print array's type. */
1695 pp_string (buffer
, " of ");
1697 if (TREE_CODE (tmp
) != POINTER_TYPE
)
1698 pp_string (buffer
, "aliased ");
1700 if (TYPE_NAME (tmp
) || !RECORD_OR_UNION_TYPE_P (tmp
))
1701 dump_generic_ada_node (buffer
, tmp
, TREE_TYPE (t
), spc
, false, true);
1703 dump_ada_double_name (buffer
, parent
, get_underlying_decl (tmp
));
1707 /* Dump in BUFFER type names associated with a template, each prepended with
1708 '_'. TYPES is the TREE_PURPOSE of a DECL_TEMPLATE_INSTANTIATIONS. SPC is
1709 the indentation level. */
1712 dump_template_types (pretty_printer
*buffer
, tree types
, int spc
)
1715 size_t len
= TREE_VEC_LENGTH (types
);
1717 for (i
= 0; i
< len
; i
++)
1719 tree elem
= TREE_VEC_ELT (types
, i
);
1720 pp_underscore (buffer
);
1721 if (!dump_generic_ada_node (buffer
, elem
, 0, spc
, false, true))
1723 pp_string (buffer
, "unknown");
1724 pp_scalar (buffer
, "%lu", (unsigned long) TREE_HASH (elem
));
1729 /* Dump in BUFFER the contents of all class instantiations associated with
1730 a given template T. SPC is the indentation level. */
1733 dump_ada_template (pretty_printer
*buffer
, tree t
, int spc
)
1735 /* DECL_SIZE_UNIT is DECL_TEMPLATE_INSTANTIATIONS in this context. */
1736 tree inst
= DECL_SIZE_UNIT (t
);
1737 /* This emulates DECL_TEMPLATE_RESULT in this context. */
1738 struct tree_template_decl
{
1739 struct tree_decl_common common
;
1743 tree result
= ((struct tree_template_decl
*) t
)->result
;
1746 /* Don't look at template declarations declaring something coming from
1747 another file. This can occur for template friend declarations. */
1748 if (LOCATION_FILE (decl_sloc (result
, false))
1749 != LOCATION_FILE (decl_sloc (t
, false)))
1752 for (; inst
&& inst
!= error_mark_node
; inst
= TREE_CHAIN (inst
))
1754 tree types
= TREE_PURPOSE (inst
);
1755 tree instance
= TREE_VALUE (inst
);
1757 if (TREE_VEC_LENGTH (types
) == 0)
1760 if (!RECORD_OR_UNION_TYPE_P (instance
) || !TYPE_METHODS (instance
))
1763 /* We are interested in concrete template instantiations only: skip
1764 partially specialized nodes. */
1765 if (RECORD_OR_UNION_TYPE_P (instance
)
1767 && cpp_check (instance
, HAS_DEPENDENT_TEMPLATE_ARGS
))
1772 pp_string (buffer
, "package ");
1773 package_prefix
= false;
1774 dump_generic_ada_node (buffer
, instance
, t
, spc
, false, true);
1775 dump_template_types (buffer
, types
, spc
);
1776 pp_string (buffer
, " is");
1778 newline_and_indent (buffer
, spc
);
1780 TREE_VISITED (get_underlying_decl (instance
)) = 1;
1781 pp_string (buffer
, "type ");
1782 dump_generic_ada_node (buffer
, instance
, t
, spc
, false, true);
1783 package_prefix
= true;
1785 if (is_tagged_type (instance
))
1786 pp_string (buffer
, " is tagged limited ");
1788 pp_string (buffer
, " is limited ");
1790 dump_generic_ada_node (buffer
, instance
, t
, spc
, false, false);
1791 pp_newline (buffer
);
1793 newline_and_indent (buffer
, spc
);
1795 pp_string (buffer
, "end;");
1796 newline_and_indent (buffer
, spc
);
1797 pp_string (buffer
, "use ");
1798 package_prefix
= false;
1799 dump_generic_ada_node (buffer
, instance
, t
, spc
, false, true);
1800 dump_template_types (buffer
, types
, spc
);
1801 package_prefix
= true;
1802 pp_semicolon (buffer
);
1803 pp_newline (buffer
);
1804 pp_newline (buffer
);
1807 return num_inst
> 0;
1810 /* Return true if NODE is a simple enum types, that can be mapped to an
1811 Ada enum type directly. */
1814 is_simple_enum (tree node
)
1816 HOST_WIDE_INT count
= 0;
1819 for (value
= TYPE_VALUES (node
); value
; value
= TREE_CHAIN (value
))
1821 tree int_val
= TREE_VALUE (value
);
1823 if (TREE_CODE (int_val
) != INTEGER_CST
)
1824 int_val
= DECL_INITIAL (int_val
);
1826 if (!tree_fits_shwi_p (int_val
))
1828 else if (tree_to_shwi (int_val
) != count
)
1837 static bool bitfield_used
= false;
1839 /* Recursively dump in BUFFER Ada declarations corresponding to NODE of type
1840 TYPE. SPC is the indentation level. LIMITED_ACCESS indicates whether NODE
1841 can be referenced via a "limited with" clause. NAME_ONLY indicates whether
1842 we should only dump the name of NODE, instead of its full declaration. */
1845 dump_generic_ada_node (pretty_printer
*buffer
, tree node
, tree type
, int spc
,
1846 int limited_access
, bool name_only
)
1848 if (node
== NULL_TREE
)
1851 switch (TREE_CODE (node
))
1854 pp_string (buffer
, "<<< error >>>");
1857 case IDENTIFIER_NODE
:
1858 pp_ada_tree_identifier (buffer
, node
, type
, limited_access
);
1862 pp_string (buffer
, "--- unexpected node: TREE_LIST");
1866 dump_generic_ada_node
1867 (buffer
, BINFO_TYPE (node
), type
, spc
, limited_access
, name_only
);
1870 pp_string (buffer
, "--- unexpected node: TREE_VEC");
1876 append_withs ("System", false);
1877 pp_string (buffer
, "System.Address");
1880 pp_string (buffer
, "address");
1884 pp_string (buffer
, "<vector>");
1888 pp_string (buffer
, "<complex>");
1893 dump_generic_ada_node (buffer
, TYPE_NAME (node
), node
, spc
, 0, true);
1896 tree value
= TYPE_VALUES (node
);
1898 if (is_simple_enum (node
))
1902 newline_and_indent (buffer
, spc
- 1);
1903 pp_left_paren (buffer
);
1904 for (; value
; value
= TREE_CHAIN (value
))
1911 newline_and_indent (buffer
, spc
);
1914 pp_ada_tree_identifier
1915 (buffer
, TREE_PURPOSE (value
), node
, false);
1917 pp_string (buffer
, ");");
1919 newline_and_indent (buffer
, spc
);
1920 pp_string (buffer
, "pragma Convention (C, ");
1921 dump_generic_ada_node
1922 (buffer
, DECL_NAME (type
) ? type
: TYPE_NAME (node
), type
,
1924 pp_right_paren (buffer
);
1928 pp_string (buffer
, "unsigned");
1929 for (; value
; value
= TREE_CHAIN (value
))
1931 pp_semicolon (buffer
);
1932 newline_and_indent (buffer
, spc
);
1934 pp_ada_tree_identifier
1935 (buffer
, TREE_PURPOSE (value
), node
, false);
1936 pp_string (buffer
, " : constant ");
1938 dump_generic_ada_node
1939 (buffer
, DECL_NAME (type
) ? type
: TYPE_NAME (node
), type
,
1942 pp_string (buffer
, " := ");
1943 dump_generic_ada_node
1945 TREE_CODE (TREE_VALUE (value
)) == INTEGER_CST
?
1946 TREE_VALUE (value
) : DECL_INITIAL (TREE_VALUE (value
)),
1947 node
, spc
, false, true);
1955 case FIXED_POINT_TYPE
:
1958 enum tree_code_class tclass
;
1960 tclass
= TREE_CODE_CLASS (TREE_CODE (node
));
1962 if (tclass
== tcc_declaration
)
1964 if (DECL_NAME (node
))
1965 pp_ada_tree_identifier
1966 (buffer
, DECL_NAME (node
), 0, limited_access
);
1968 pp_string (buffer
, "<unnamed type decl>");
1970 else if (tclass
== tcc_type
)
1972 if (TYPE_NAME (node
))
1974 if (TREE_CODE (TYPE_NAME (node
)) == IDENTIFIER_NODE
)
1975 pp_ada_tree_identifier (buffer
, TYPE_NAME (node
),
1976 node
, limited_access
);
1977 else if (TREE_CODE (TYPE_NAME (node
)) == TYPE_DECL
1978 && DECL_NAME (TYPE_NAME (node
)))
1979 dump_ada_decl_name (buffer
, TYPE_NAME (node
), limited_access
);
1981 pp_string (buffer
, "<unnamed type>");
1983 else if (TREE_CODE (node
) == INTEGER_TYPE
)
1985 append_withs ("Interfaces.C.Extensions", false);
1986 bitfield_used
= true;
1988 if (TYPE_PRECISION (node
) == 1)
1989 pp_string (buffer
, "Extensions.Unsigned_1");
1992 pp_string (buffer
, (TYPE_UNSIGNED (node
)
1993 ? "Extensions.Unsigned_"
1994 : "Extensions.Signed_"));
1995 pp_decimal_int (buffer
, TYPE_PRECISION (node
));
1999 pp_string (buffer
, "<unnamed type>");
2005 case REFERENCE_TYPE
:
2006 if (name_only
&& TYPE_NAME (node
))
2007 dump_generic_ada_node
2008 (buffer
, TYPE_NAME (node
), node
, spc
, limited_access
, true);
2010 else if (TREE_CODE (TREE_TYPE (node
)) == FUNCTION_TYPE
)
2012 tree fnode
= TREE_TYPE (node
);
2015 if (VOID_TYPE_P (TREE_TYPE (fnode
)))
2017 is_function
= false;
2018 pp_string (buffer
, "access procedure");
2023 pp_string (buffer
, "access function");
2026 dump_ada_function_declaration
2027 (buffer
, node
, false, false, false, spc
+ INDENT_INCR
);
2031 pp_string (buffer
, " return ");
2032 dump_generic_ada_node
2033 (buffer
, TREE_TYPE (fnode
), type
, spc
, 0, true);
2036 /* If we are dumping the full type, it means we are part of a
2037 type definition and need also a Convention C pragma. */
2040 pp_semicolon (buffer
);
2041 newline_and_indent (buffer
, spc
);
2042 pp_string (buffer
, "pragma Convention (C, ");
2043 dump_generic_ada_node
2044 (buffer
, type
, 0, spc
, false, true);
2045 pp_right_paren (buffer
);
2050 int is_access
= false;
2051 unsigned int quals
= TYPE_QUALS (TREE_TYPE (node
));
2053 if (VOID_TYPE_P (TREE_TYPE (node
)))
2056 pp_string (buffer
, "new ");
2059 append_withs ("System", false);
2060 pp_string (buffer
, "System.Address");
2063 pp_string (buffer
, "address");
2067 if (TREE_CODE (node
) == POINTER_TYPE
2068 && TREE_CODE (TREE_TYPE (node
)) == INTEGER_TYPE
2070 (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME
2071 (TREE_TYPE (node
)))), "char"))
2074 pp_string (buffer
, "new ");
2078 pp_string (buffer
, "Interfaces.C.Strings.chars_ptr");
2079 append_withs ("Interfaces.C.Strings", false);
2082 pp_string (buffer
, "chars_ptr");
2086 /* For now, handle all access-to-access or
2087 access-to-unknown-structs as opaque system.address. */
2089 tree type_name
= TYPE_NAME (TREE_TYPE (node
));
2090 const_tree typ2
= !type
||
2091 DECL_P (type
) ? type
: TYPE_NAME (type
);
2092 const_tree underlying_type
=
2093 get_underlying_decl (TREE_TYPE (node
));
2095 if (TREE_CODE (TREE_TYPE (node
)) == POINTER_TYPE
2096 /* Pointer to pointer. */
2098 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (node
))
2099 && (!underlying_type
2100 || !TYPE_FIELDS (TREE_TYPE (underlying_type
))))
2101 /* Pointer to opaque structure. */
2103 || underlying_type
== NULL_TREE
2105 && !TREE_VISITED (underlying_type
)
2106 && !TREE_VISITED (type_name
)
2107 && !is_tagged_type (TREE_TYPE (node
))
2108 && DECL_SOURCE_FILE (underlying_type
)
2109 == source_file_base
)
2110 || (type_name
&& typ2
2111 && DECL_P (underlying_type
)
2113 && decl_sloc (underlying_type
, true)
2114 > decl_sloc (typ2
, true)
2115 && DECL_SOURCE_FILE (underlying_type
)
2116 == DECL_SOURCE_FILE (typ2
)))
2120 append_withs ("System", false);
2122 pp_string (buffer
, "new ");
2123 pp_string (buffer
, "System.Address");
2126 pp_string (buffer
, "address");
2130 if (!package_prefix
)
2131 pp_string (buffer
, "access");
2132 else if (AGGREGATE_TYPE_P (TREE_TYPE (node
)))
2134 if (!type
|| TREE_CODE (type
) != FUNCTION_DECL
)
2136 pp_string (buffer
, "access ");
2139 if (quals
& TYPE_QUAL_CONST
)
2140 pp_string (buffer
, "constant ");
2141 else if (!name_only
)
2142 pp_string (buffer
, "all ");
2144 else if (quals
& TYPE_QUAL_CONST
)
2145 pp_string (buffer
, "in ");
2149 pp_string (buffer
, "access ");
2150 /* ??? should be configurable: access or in out. */
2156 pp_string (buffer
, "access ");
2159 pp_string (buffer
, "all ");
2162 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (node
)) && type_name
)
2163 dump_generic_ada_node
2165 TREE_TYPE (node
), spc
, is_access
, true);
2167 dump_generic_ada_node
2168 (buffer
, TREE_TYPE (node
), TREE_TYPE (node
),
2177 dump_generic_ada_node
2178 (buffer
, TYPE_NAME (node
), node
, spc
, limited_access
, true);
2180 dump_ada_array_type (buffer
, node
, type
, spc
);
2187 if (TYPE_NAME (node
))
2188 dump_generic_ada_node
2189 (buffer
, TYPE_NAME (node
), node
, spc
, limited_access
, true);
2192 pp_string (buffer
, "anon_");
2193 pp_scalar (buffer
, "%d", TYPE_UID (node
));
2197 print_ada_struct_decl (buffer
, node
, type
, spc
, true);
2201 /* We treat the upper half of the sizetype range as negative. This
2202 is consistent with the internal treatment and makes it possible
2203 to generate the (0 .. -1) range for flexible array members. */
2204 if (TREE_TYPE (node
) == sizetype
)
2205 node
= fold_convert (ssizetype
, node
);
2206 if (tree_fits_shwi_p (node
))
2207 pp_wide_integer (buffer
, tree_to_shwi (node
));
2208 else if (tree_fits_uhwi_p (node
))
2209 pp_unsigned_wide_integer (buffer
, tree_to_uhwi (node
));
2212 wide_int val
= node
;
2214 if (wi::neg_p (val
))
2219 sprintf (pp_buffer (buffer
)->digit_buffer
,
2220 "16#%" HOST_WIDE_INT_PRINT
"x",
2221 val
.elt (val
.get_len () - 1));
2222 for (i
= val
.get_len () - 2; i
>= 0; i
--)
2223 sprintf (pp_buffer (buffer
)->digit_buffer
,
2224 HOST_WIDE_INT_PRINT_PADDED_HEX
, val
.elt (i
));
2225 pp_string (buffer
, pp_buffer (buffer
)->digit_buffer
);
2238 dump_ada_decl_name (buffer
, node
, limited_access
);
2242 if (DECL_IS_BUILTIN (node
))
2244 /* Don't print the declaration of built-in types. */
2248 /* If we're in the middle of a declaration, defaults to
2252 append_withs ("System", false);
2253 pp_string (buffer
, "System.Address");
2256 pp_string (buffer
, "address");
2262 dump_ada_decl_name (buffer
, node
, limited_access
);
2265 if (is_tagged_type (TREE_TYPE (node
)))
2267 tree tmp
= TYPE_FIELDS (TREE_TYPE (node
));
2270 /* Look for ancestors. */
2271 for (; tmp
; tmp
= TREE_CHAIN (tmp
))
2273 if (!DECL_NAME (tmp
) && is_tagged_type (TREE_TYPE (tmp
)))
2277 pp_string (buffer
, "limited new ");
2281 pp_string (buffer
, " and ");
2284 (buffer
, TYPE_NAME (TREE_TYPE (tmp
)), false);
2288 pp_string (buffer
, first
? "tagged limited " : " with ");
2290 else if (has_nontrivial_methods (TREE_TYPE (node
)))
2291 pp_string (buffer
, "limited ");
2293 dump_generic_ada_node
2294 (buffer
, TREE_TYPE (node
), type
, spc
, false, false);
2301 case NAMESPACE_DECL
:
2302 dump_ada_decl_name (buffer
, node
, false);
2306 /* Ignore other nodes (e.g. expressions). */
2313 /* Dump in BUFFER NODE's methods. SPC is the indentation level. Return 1 if
2314 methods were printed, 0 otherwise.
2316 We do it in 2 passes: first, the regular methods, i.e. non-static member
2317 functions, are output immediately within the package created for the class
2318 so that they are considered as primitive operations in Ada; second, the
2319 static member functions are output in a nested package so that they are
2320 _not_ considered as primitive operations in Ada.
2322 This approach is necessary because the formers have the implicit 'this'
2323 pointer whereas the latters don't and, on 32-bit x86/Windows, the calling
2324 conventions for the 'this' pointer are special. Therefore, the compiler
2325 needs to be able to differentiate regular methods (with 'this' pointer)
2326 from static member functions that take a pointer to the class as first
2330 print_ada_methods (pretty_printer
*buffer
, tree node
, int spc
)
2332 bool has_static_methods
= false;
2336 if (!has_nontrivial_methods (node
))
2339 pp_semicolon (buffer
);
2341 /* First pass: the regular methods. */
2343 for (t
= TYPE_METHODS (node
); t
; t
= TREE_CHAIN (t
))
2345 if (TREE_CODE (TREE_TYPE (t
)) != METHOD_TYPE
)
2347 has_static_methods
= true;
2353 pp_newline (buffer
);
2354 pp_newline (buffer
);
2357 res
= print_ada_declaration (buffer
, t
, node
, spc
);
2360 if (!has_static_methods
)
2363 pp_newline (buffer
);
2364 newline_and_indent (buffer
, spc
);
2366 /* Second pass: the static member functions. */
2367 pp_string (buffer
, "package Static is");
2368 pp_newline (buffer
);
2372 for (t
= TYPE_METHODS (node
); t
; t
= TREE_CHAIN (t
))
2374 if (TREE_CODE (TREE_TYPE (t
)) == METHOD_TYPE
)
2379 pp_newline (buffer
);
2380 pp_newline (buffer
);
2383 res
= print_ada_declaration (buffer
, t
, node
, spc
);
2387 newline_and_indent (buffer
, spc
);
2388 pp_string (buffer
, "end;");
2390 /* In order to save the clients from adding a second use clause for the
2391 nested package, we generate renamings for the static member functions
2392 in the package created for the class. */
2393 for (t
= TYPE_METHODS (node
); t
; t
= TREE_CHAIN (t
))
2397 if (TREE_CODE (TREE_TYPE (t
)) == METHOD_TYPE
)
2400 pp_newline (buffer
);
2401 newline_and_indent (buffer
, spc
);
2403 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (t
))))
2405 pp_string (buffer
, "procedure ");
2406 is_function
= false;
2410 pp_string (buffer
, "function ");
2414 dump_ada_decl_name (buffer
, t
, false);
2415 dump_ada_function_declaration (buffer
, t
, false, false, false, spc
);
2419 pp_string (buffer
, " return ");
2420 dump_generic_ada_node (buffer
, TREE_TYPE (TREE_TYPE (t
)), node
,
2424 pp_string (buffer
, " renames Static.");
2425 dump_ada_decl_name (buffer
, t
, false);
2426 pp_semicolon (buffer
);
2432 static void dump_nested_type (pretty_printer
*, tree
, tree
, tree
, int);
2434 /* Dump in BUFFER anonymous types nested inside T's definition.
2435 PARENT is the parent node of T.
2436 FORWARD indicates whether a forward declaration of T should be generated.
2437 SPC is the indentation level.
2439 In C anonymous nested tagged types have no name whereas in C++ they have
2440 one. In C their TYPE_DECL is at top level whereas in C++ it is nested.
2441 In both languages untagged types (pointers and arrays) have no name.
2442 In C++ the nested TYPE_DECLs can come after their associated FIELD_DECL.
2444 Therefore, in order to have a common processing for both languages, we
2445 disregard anonymous TYPE_DECLs at top level and here we make a first
2446 pass on the nested TYPE_DECLs and a second pass on the unnamed types. */
2449 dump_nested_types (pretty_printer
*buffer
, tree t
, tree parent
, bool forward
,
2454 /* Avoid recursing over the same tree. */
2455 if (TREE_VISITED (t
))
2458 /* Find possible anonymous pointers/arrays/structs/unions recursively. */
2459 type
= TREE_TYPE (t
);
2460 if (type
== NULL_TREE
)
2465 pp_string (buffer
, "type ");
2466 dump_generic_ada_node (buffer
, t
, t
, spc
, false, true);
2467 pp_semicolon (buffer
);
2468 newline_and_indent (buffer
, spc
);
2469 TREE_VISITED (t
) = 1;
2472 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
2473 if (TREE_CODE (field
) == TYPE_DECL
2474 && DECL_NAME (field
) != DECL_NAME (t
)
2475 && TYPE_NAME (TREE_TYPE (field
)) != TYPE_NAME (type
))
2476 dump_nested_type (buffer
, field
, t
, parent
, spc
);
2478 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
2479 if (!TYPE_NAME (TREE_TYPE (field
)))
2480 dump_nested_type (buffer
, field
, t
, parent
, spc
);
2482 TREE_VISITED (t
) = 1;
2485 /* Dump in BUFFER the anonymous type of FIELD inside T.
2486 PARENT is the parent node of T.
2487 FORWARD indicates whether a forward declaration of T should be generated.
2488 SPC is the indentation level. */
2491 dump_nested_type (pretty_printer
*buffer
, tree field
, tree t
, tree parent
,
2494 tree field_type
= TREE_TYPE (field
);
2497 switch (TREE_CODE (field_type
))
2500 tmp
= TREE_TYPE (field_type
);
2502 if (TREE_CODE (tmp
) == FUNCTION_TYPE
)
2503 for (tmp
= TREE_TYPE (tmp
);
2504 tmp
&& TREE_CODE (tmp
) == POINTER_TYPE
;
2505 tmp
= TREE_TYPE (tmp
))
2508 decl
= get_underlying_decl (tmp
);
2511 && decl_sloc (decl
, true) > decl_sloc (t
, true)
2512 && DECL_SOURCE_FILE (decl
) == DECL_SOURCE_FILE (t
)
2513 && !TREE_VISITED (decl
)
2514 && !DECL_IS_BUILTIN (decl
)
2515 && (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl
))
2516 || TYPE_FIELDS (TREE_TYPE (decl
))))
2518 /* Generate forward declaration. */
2519 pp_string (buffer
, "type ");
2520 dump_generic_ada_node (buffer
, decl
, 0, spc
, false, true);
2521 pp_semicolon (buffer
);
2522 newline_and_indent (buffer
, spc
);
2523 TREE_VISITED (decl
) = 1;
2528 tmp
= TREE_TYPE (field_type
);
2529 while (TREE_CODE (tmp
) == ARRAY_TYPE
)
2530 tmp
= TREE_TYPE (tmp
);
2531 decl
= get_underlying_decl (tmp
);
2534 && !DECL_NAME (decl
)
2535 && !TREE_VISITED (decl
))
2537 /* Generate full declaration. */
2538 dump_nested_type (buffer
, decl
, t
, parent
, spc
);
2539 TREE_VISITED (decl
) = 1;
2542 /* Special case char arrays. */
2543 if (is_char_array (field
))
2544 pp_string (buffer
, "sub");
2546 pp_string (buffer
, "type ");
2547 dump_ada_double_name (buffer
, parent
, field
);
2548 pp_string (buffer
, " is ");
2549 dump_ada_array_type (buffer
, field
, parent
, spc
);
2550 pp_semicolon (buffer
);
2551 newline_and_indent (buffer
, spc
);
2556 if (TYPE_NAME (TREE_TYPE (t
)) && !TREE_VISITED (t
))
2558 pp_string (buffer
, "type ");
2559 dump_generic_ada_node (buffer
, t
, parent
, spc
, false, true);
2560 pp_semicolon (buffer
);
2561 newline_and_indent (buffer
, spc
);
2564 TREE_VISITED (t
) = 1;
2565 dump_nested_types (buffer
, field
, t
, false, spc
);
2567 pp_string (buffer
, "type ");
2569 if (TYPE_NAME (field_type
))
2571 dump_generic_ada_node (buffer
, field_type
, 0, spc
, false, true);
2572 if (TREE_CODE (field_type
) == UNION_TYPE
)
2573 pp_string (buffer
, " (discr : unsigned := 0)");
2574 pp_string (buffer
, " is ");
2575 print_ada_struct_decl (buffer
, field_type
, t
, spc
, false);
2577 pp_string (buffer
, "pragma Convention (C_Pass_By_Copy, ");
2578 dump_generic_ada_node (buffer
, field_type
, 0, spc
, false, true);
2579 pp_string (buffer
, ");");
2580 newline_and_indent (buffer
, spc
);
2582 if (TREE_CODE (field_type
) == UNION_TYPE
)
2584 pp_string (buffer
, "pragma Unchecked_Union (");
2585 dump_generic_ada_node (buffer
, field_type
, 0, spc
, false, true);
2586 pp_string (buffer
, ");");
2591 dump_ada_double_name (buffer
, parent
, field
);
2592 if (TREE_CODE (field_type
) == UNION_TYPE
)
2593 pp_string (buffer
, " (discr : unsigned := 0)");
2594 pp_string (buffer
, " is ");
2595 print_ada_struct_decl (buffer
, field_type
, t
, spc
, false);
2597 pp_string (buffer
, "pragma Convention (C_Pass_By_Copy, ");
2598 dump_ada_double_name (buffer
, parent
, field
);
2599 pp_string (buffer
, ");");
2600 newline_and_indent (buffer
, spc
);
2602 if (TREE_CODE (field_type
) == UNION_TYPE
)
2604 pp_string (buffer
, "pragma Unchecked_Union (");
2605 dump_ada_double_name (buffer
, parent
, field
);
2606 pp_string (buffer
, ");");
2615 /* Dump in BUFFER constructor spec corresponding to T. */
2618 print_constructor (pretty_printer
*buffer
, tree t
)
2620 tree decl_name
= DECL_NAME (DECL_ORIGIN (t
));
2622 pp_string (buffer
, "New_");
2623 pp_ada_tree_identifier (buffer
, decl_name
, t
, false);
2626 /* Dump in BUFFER destructor spec corresponding to T. */
2629 print_destructor (pretty_printer
*buffer
, tree t
)
2631 tree decl_name
= DECL_NAME (DECL_ORIGIN (t
));
2633 pp_string (buffer
, "Delete_");
2634 pp_ada_tree_identifier (buffer
, decl_name
, t
, false);
2637 /* Return the name of type T. */
2642 tree n
= TYPE_NAME (t
);
2644 if (TREE_CODE (n
) == IDENTIFIER_NODE
)
2645 return IDENTIFIER_POINTER (n
);
2647 return IDENTIFIER_POINTER (DECL_NAME (n
));
2650 /* Print in BUFFER the declaration of a variable T of type TYPE in Ada syntax.
2651 SPC is the indentation level. Return 1 if a declaration was printed,
2655 print_ada_declaration (pretty_printer
*buffer
, tree t
, tree type
, int spc
)
2657 int is_var
= 0, need_indent
= 0;
2658 int is_class
= false;
2659 tree name
= TYPE_NAME (TREE_TYPE (t
));
2660 tree decl_name
= DECL_NAME (t
);
2661 tree orig
= NULL_TREE
;
2663 if (cpp_check
&& cpp_check (t
, IS_TEMPLATE
))
2664 return dump_ada_template (buffer
, t
, spc
);
2666 if (TREE_CODE (t
) == CONST_DECL
&& TREE_CODE (TREE_TYPE (t
)) == ENUMERAL_TYPE
)
2667 /* Skip enumeral values: will be handled as part of the type itself. */
2670 if (TREE_CODE (t
) == TYPE_DECL
)
2672 orig
= DECL_ORIGINAL_TYPE (t
);
2674 if (orig
&& TYPE_STUB_DECL (orig
))
2676 tree stub
= TYPE_STUB_DECL (orig
);
2677 tree typ
= TREE_TYPE (stub
);
2679 if (TYPE_NAME (typ
))
2681 /* If types have same representation, and same name (ignoring
2682 casing), then ignore the second type. */
2683 if (type_name (typ
) == type_name (TREE_TYPE (t
))
2684 || !strcasecmp (type_name (typ
), type_name (TREE_TYPE (t
))))
2689 if (RECORD_OR_UNION_TYPE_P (typ
) && !TYPE_FIELDS (typ
))
2691 pp_string (buffer
, "-- skipped empty struct ");
2692 dump_generic_ada_node (buffer
, t
, type
, spc
, false, true);
2696 if (!TREE_VISITED (stub
)
2697 && DECL_SOURCE_FILE (stub
) == source_file_base
)
2698 dump_nested_types (buffer
, stub
, stub
, true, spc
);
2700 pp_string (buffer
, "subtype ");
2701 dump_generic_ada_node (buffer
, t
, type
, spc
, false, true);
2702 pp_string (buffer
, " is ");
2703 dump_generic_ada_node (buffer
, typ
, type
, spc
, false, true);
2704 pp_semicolon (buffer
);
2710 /* Skip unnamed or anonymous structs/unions/enum types. */
2711 if (!orig
&& !decl_name
&& !name
2712 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (t
))
2713 || TREE_CODE (TREE_TYPE (t
)) == ENUMERAL_TYPE
))
2716 /* Skip anonymous enum types (duplicates of real types). */
2718 && TREE_CODE (TREE_TYPE (t
)) == ENUMERAL_TYPE
2720 && (*IDENTIFIER_POINTER (decl_name
) == '.'
2721 || *IDENTIFIER_POINTER (decl_name
) == '$'))
2726 switch (TREE_CODE (TREE_TYPE (t
)))
2730 /* Skip empty structs (typically forward references to real
2732 if (!TYPE_FIELDS (TREE_TYPE (t
)))
2734 pp_string (buffer
, "-- skipped empty struct ");
2735 dump_generic_ada_node (buffer
, t
, type
, spc
, false, true);
2740 && (*IDENTIFIER_POINTER (decl_name
) == '.'
2741 || *IDENTIFIER_POINTER (decl_name
) == '$'))
2743 pp_string (buffer
, "-- skipped anonymous struct ");
2744 dump_generic_ada_node (buffer
, t
, type
, spc
, false, true);
2745 TREE_VISITED (t
) = 1;
2749 if (orig
&& TYPE_NAME (orig
) && orig
!= TREE_TYPE (t
))
2750 pp_string (buffer
, "subtype ");
2753 dump_nested_types (buffer
, t
, t
, false, spc
);
2755 if (separate_class_package (t
))
2758 pp_string (buffer
, "package Class_");
2759 dump_generic_ada_node (buffer
, t
, type
, spc
, false, true);
2760 pp_string (buffer
, " is");
2762 newline_and_indent (buffer
, spc
);
2765 pp_string (buffer
, "type ");
2771 case REFERENCE_TYPE
:
2772 if ((orig
&& TYPE_NAME (orig
) && orig
!= TREE_TYPE (t
))
2773 || is_char_array (t
))
2774 pp_string (buffer
, "subtype ");
2776 pp_string (buffer
, "type ");
2780 pp_string (buffer
, "-- skipped function type ");
2781 dump_generic_ada_node (buffer
, t
, type
, spc
, false, true);
2786 if ((orig
&& TYPE_NAME (orig
) && orig
!= TREE_TYPE (t
))
2787 || !is_simple_enum (TREE_TYPE (t
)))
2788 pp_string (buffer
, "subtype ");
2790 pp_string (buffer
, "type ");
2794 pp_string (buffer
, "subtype ");
2796 TREE_VISITED (t
) = 1;
2802 && *IDENTIFIER_POINTER (decl_name
) == '_')
2808 /* Print the type and name. */
2809 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
2814 /* Print variable's name. */
2815 dump_generic_ada_node (buffer
, t
, type
, spc
, false, true);
2817 if (TREE_CODE (t
) == TYPE_DECL
)
2819 pp_string (buffer
, " is ");
2821 if (orig
&& TYPE_NAME (orig
) && orig
!= TREE_TYPE (t
))
2822 dump_generic_ada_node
2823 (buffer
, TYPE_NAME (orig
), type
, spc
, false, true);
2825 dump_ada_array_type (buffer
, t
, type
, spc
);
2829 tree tmp
= TYPE_NAME (TREE_TYPE (t
));
2831 if (spc
== INDENT_INCR
|| TREE_STATIC (t
))
2834 pp_string (buffer
, " : ");
2836 if (TREE_CODE (TREE_TYPE (TREE_TYPE (t
))) != POINTER_TYPE
)
2837 pp_string (buffer
, "aliased ");
2840 dump_generic_ada_node (buffer
, tmp
, type
, spc
, false, true);
2842 dump_ada_double_name (buffer
, type
, t
);
2844 dump_ada_array_type (buffer
, t
, type
, spc
);
2847 else if (TREE_CODE (t
) == FUNCTION_DECL
)
2849 bool is_function
, is_abstract_class
= false;
2850 bool is_method
= TREE_CODE (TREE_TYPE (t
)) == METHOD_TYPE
;
2851 tree decl_name
= DECL_NAME (t
);
2852 bool is_abstract
= false;
2853 bool is_constructor
= false;
2854 bool is_destructor
= false;
2855 bool is_copy_constructor
= false;
2856 bool is_move_constructor
= false;
2863 is_abstract
= cpp_check (t
, IS_ABSTRACT
);
2864 is_constructor
= cpp_check (t
, IS_CONSTRUCTOR
);
2865 is_destructor
= cpp_check (t
, IS_DESTRUCTOR
);
2866 is_copy_constructor
= cpp_check (t
, IS_COPY_CONSTRUCTOR
);
2867 is_move_constructor
= cpp_check (t
, IS_MOVE_CONSTRUCTOR
);
2870 /* Skip copy constructors and C++11 move constructors: some are internal
2871 only and those that are not cannot be called easily from Ada. */
2872 if (is_copy_constructor
|| is_move_constructor
)
2875 if (is_constructor
|| is_destructor
)
2877 /* ??? Skip implicit constructors/destructors for now. */
2878 if (DECL_ARTIFICIAL (t
))
2881 /* Only consider constructors/destructors for complete objects. */
2882 if (strncmp (IDENTIFIER_POINTER (decl_name
), "__comp", 6) != 0)
2886 /* If this function has an entry in the vtable, we cannot omit it. */
2887 else if (!DECL_VINDEX (t
) && *IDENTIFIER_POINTER (decl_name
) == '_')
2890 pp_string (buffer
, "-- skipped func ");
2891 pp_string (buffer
, IDENTIFIER_POINTER (decl_name
));
2898 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (t
))) && !is_constructor
)
2900 pp_string (buffer
, "procedure ");
2901 is_function
= false;
2905 pp_string (buffer
, "function ");
2910 print_constructor (buffer
, t
);
2911 else if (is_destructor
)
2912 print_destructor (buffer
, t
);
2914 dump_ada_decl_name (buffer
, t
, false);
2916 dump_ada_function_declaration
2917 (buffer
, t
, is_method
, is_constructor
, is_destructor
, spc
);
2921 pp_string (buffer
, " return ");
2923 = is_constructor
? DECL_CONTEXT (t
) : TREE_TYPE (TREE_TYPE (t
));
2924 dump_generic_ada_node (buffer
, ret_type
, type
, spc
, false, true);
2928 && RECORD_OR_UNION_TYPE_P (type
)
2929 && TYPE_METHODS (type
))
2933 for (tmp
= TYPE_METHODS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
2934 if (cpp_check (tmp
, IS_ABSTRACT
))
2936 is_abstract_class
= true;
2941 if (is_abstract
|| is_abstract_class
)
2942 pp_string (buffer
, " is abstract");
2944 pp_semicolon (buffer
);
2945 pp_string (buffer
, " -- ");
2946 dump_sloc (buffer
, t
);
2948 if (is_abstract
|| !DECL_ASSEMBLER_NAME (t
))
2951 newline_and_indent (buffer
, spc
);
2955 pp_string (buffer
, "pragma CPP_Constructor (");
2956 print_constructor (buffer
, t
);
2957 pp_string (buffer
, ", \"");
2958 pp_asm_name (buffer
, t
);
2959 pp_string (buffer
, "\");");
2961 else if (is_destructor
)
2963 pp_string (buffer
, "pragma Import (CPP, ");
2964 print_destructor (buffer
, t
);
2965 pp_string (buffer
, ", \"");
2966 pp_asm_name (buffer
, t
);
2967 pp_string (buffer
, "\");");
2971 dump_ada_import (buffer
, t
);
2976 else if (TREE_CODE (t
) == TYPE_DECL
&& !DECL_ORIGINAL_TYPE (t
))
2978 int is_interface
= 0;
2979 int is_abstract_record
= 0;
2984 /* Anonymous structs/unions */
2985 dump_generic_ada_node (buffer
, TREE_TYPE (t
), t
, spc
, false, true);
2987 if (TREE_CODE (TREE_TYPE (t
)) == UNION_TYPE
)
2989 pp_string (buffer
, " (discr : unsigned := 0)");
2992 pp_string (buffer
, " is ");
2994 /* Check whether we have an Ada interface compatible class. */
2996 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t
))
2997 && TYPE_METHODS (TREE_TYPE (t
)))
3002 /* Check that there are no fields other than the virtual table. */
3003 for (tmp
= TYPE_FIELDS (TREE_TYPE (t
)); tmp
; tmp
= TREE_CHAIN (tmp
))
3005 if (TREE_CODE (tmp
) == TYPE_DECL
)
3010 if (num_fields
== 1)
3013 /* Also check that there are only pure virtual methods. Since the
3014 class is empty, we can skip implicit constructors/destructors. */
3015 for (tmp
= TYPE_METHODS (TREE_TYPE (t
)); tmp
; tmp
= TREE_CHAIN (tmp
))
3017 if (DECL_ARTIFICIAL (tmp
))
3019 if (cpp_check (tmp
, IS_ABSTRACT
))
3020 is_abstract_record
= 1;
3026 TREE_VISITED (t
) = 1;
3029 pp_string (buffer
, "limited interface; -- ");
3030 dump_sloc (buffer
, t
);
3031 newline_and_indent (buffer
, spc
);
3032 pp_string (buffer
, "pragma Import (CPP, ");
3033 dump_generic_ada_node
3034 (buffer
, TYPE_NAME (TREE_TYPE (t
)), type
, spc
, false, true);
3035 pp_right_paren (buffer
);
3037 print_ada_methods (buffer
, TREE_TYPE (t
), spc
);
3041 if (is_abstract_record
)
3042 pp_string (buffer
, "abstract ");
3043 dump_generic_ada_node (buffer
, t
, t
, spc
, false, false);
3051 if (TREE_CODE (t
) == FIELD_DECL
&& DECL_NAME (t
))
3052 check_name (buffer
, t
);
3054 /* Print variable/type's name. */
3055 dump_generic_ada_node (buffer
, t
, t
, spc
, false, true);
3057 if (TREE_CODE (t
) == TYPE_DECL
)
3059 tree orig
= DECL_ORIGINAL_TYPE (t
);
3060 int is_subtype
= orig
&& TYPE_NAME (orig
) && orig
!= TREE_TYPE (t
);
3062 if (!is_subtype
&& TREE_CODE (TREE_TYPE (t
)) == UNION_TYPE
)
3063 pp_string (buffer
, " (discr : unsigned := 0)");
3065 pp_string (buffer
, " is ");
3067 dump_generic_ada_node (buffer
, orig
, t
, spc
, false, is_subtype
);
3071 if (spc
== INDENT_INCR
|| TREE_STATIC (t
))
3074 pp_string (buffer
, " : ");
3076 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (t
)))
3078 pp_string (buffer
, "aliased ");
3080 if (TYPE_NAME (TREE_TYPE (t
)))
3081 dump_generic_ada_node
3082 (buffer
, TREE_TYPE (t
), t
, spc
, false, true);
3084 dump_ada_double_name (buffer
, type
, t
);
3088 if (TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
3089 && (TYPE_NAME (TREE_TYPE (t
))
3090 || TREE_CODE (TREE_TYPE (t
)) != INTEGER_TYPE
))
3091 pp_string (buffer
, "aliased ");
3093 dump_generic_ada_node
3094 (buffer
, TREE_TYPE (t
), TREE_TYPE (t
), spc
, false, true);
3102 newline_and_indent (buffer
, spc
);
3103 pp_string (buffer
, "end;");
3104 newline_and_indent (buffer
, spc
);
3105 pp_string (buffer
, "use Class_");
3106 dump_generic_ada_node (buffer
, t
, type
, spc
, false, true);
3107 pp_semicolon (buffer
);
3108 pp_newline (buffer
);
3110 /* All needed indentation/newline performed already, so return 0. */
3115 pp_string (buffer
, "; -- ");
3116 dump_sloc (buffer
, t
);
3121 newline_and_indent (buffer
, spc
);
3122 dump_ada_import (buffer
, t
);
3128 /* Prints in BUFFER a structure NODE of type TYPE: name, fields, and methods
3129 with Ada syntax. SPC is the indentation level. If DISPLAY_CONVENTION is
3130 true, also print the pragma Convention for NODE. */
3133 print_ada_struct_decl (pretty_printer
*buffer
, tree node
, tree type
, int spc
,
3134 bool display_convention
)
3137 const bool is_union
= (TREE_CODE (node
) == UNION_TYPE
);
3140 int field_spc
= spc
+ INDENT_INCR
;
3143 bitfield_used
= false;
3145 if (TYPE_FIELDS (node
))
3147 /* Print the contents of the structure. */
3148 pp_string (buffer
, "record");
3152 newline_and_indent (buffer
, spc
+ INDENT_INCR
);
3153 pp_string (buffer
, "case discr is");
3154 field_spc
= spc
+ INDENT_INCR
* 3;
3157 pp_newline (buffer
);
3159 /* Print the non-static fields of the structure. */
3160 for (tmp
= TYPE_FIELDS (node
); tmp
; tmp
= TREE_CHAIN (tmp
))
3162 /* Add parent field if needed. */
3163 if (!DECL_NAME (tmp
))
3165 if (!is_tagged_type (TREE_TYPE (tmp
)))
3167 if (!TYPE_NAME (TREE_TYPE (tmp
)))
3168 print_ada_declaration (buffer
, tmp
, type
, field_spc
);
3174 pp_string (buffer
, "parent : aliased ");
3177 sprintf (buf
, "field_%d : aliased ", field_num
+ 1);
3178 pp_string (buffer
, buf
);
3181 (buffer
, TYPE_NAME (TREE_TYPE (tmp
)), false);
3182 pp_semicolon (buffer
);
3184 pp_newline (buffer
);
3188 else if (TREE_CODE (tmp
) != TYPE_DECL
&& !TREE_STATIC (tmp
))
3190 /* Skip internal virtual table field. */
3191 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (tmp
)), "_vptr", 5))
3195 if (TREE_CHAIN (tmp
)
3196 && TREE_TYPE (TREE_CHAIN (tmp
)) != node
3197 && TREE_CODE (TREE_CHAIN (tmp
)) != TYPE_DECL
)
3198 sprintf (buf
, "when %d =>", field_num
);
3200 sprintf (buf
, "when others =>");
3202 INDENT (spc
+ INDENT_INCR
* 2);
3203 pp_string (buffer
, buf
);
3204 pp_newline (buffer
);
3207 if (print_ada_declaration (buffer
, tmp
, type
, field_spc
))
3209 pp_newline (buffer
);
3218 INDENT (spc
+ INDENT_INCR
);
3219 pp_string (buffer
, "end case;");
3220 pp_newline (buffer
);
3225 INDENT (spc
+ INDENT_INCR
);
3226 pp_string (buffer
, "null;");
3227 pp_newline (buffer
);
3231 pp_string (buffer
, "end record;");
3234 pp_string (buffer
, "null record;");
3236 newline_and_indent (buffer
, spc
);
3238 if (!display_convention
)
3241 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (type
)))
3243 if (has_nontrivial_methods (TREE_TYPE (type
)))
3244 pp_string (buffer
, "pragma Import (CPP, ");
3246 pp_string (buffer
, "pragma Convention (C_Pass_By_Copy, ");
3249 pp_string (buffer
, "pragma Convention (C, ");
3251 package_prefix
= false;
3252 dump_generic_ada_node (buffer
, TREE_TYPE (type
), type
, spc
, false, true);
3253 package_prefix
= true;
3254 pp_right_paren (buffer
);
3258 pp_semicolon (buffer
);
3259 newline_and_indent (buffer
, spc
);
3260 pp_string (buffer
, "pragma Unchecked_Union (");
3262 dump_generic_ada_node (buffer
, TREE_TYPE (type
), type
, spc
, false, true);
3263 pp_right_paren (buffer
);
3268 pp_semicolon (buffer
);
3269 newline_and_indent (buffer
, spc
);
3270 pp_string (buffer
, "pragma Pack (");
3271 dump_generic_ada_node
3272 (buffer
, TREE_TYPE (type
), type
, spc
, false, true);
3273 pp_right_paren (buffer
);
3274 bitfield_used
= false;
3277 need_semicolon
= !print_ada_methods (buffer
, node
, spc
);
3279 /* Print the static fields of the structure, if any. */
3280 for (tmp
= TYPE_FIELDS (node
); tmp
; tmp
= TREE_CHAIN (tmp
))
3282 if (DECL_NAME (tmp
) && TREE_STATIC (tmp
))
3286 need_semicolon
= false;
3287 pp_semicolon (buffer
);
3289 pp_newline (buffer
);
3290 pp_newline (buffer
);
3291 print_ada_declaration (buffer
, tmp
, type
, spc
);
3296 /* Dump all the declarations in SOURCE_FILE to an Ada spec.
3297 COLLECT_ALL_REFS is a front-end callback used to collect all relevant
3298 nodes for SOURCE_FILE. CHECK is used to perform C++ queries on nodes. */
3301 dump_ads (const char *source_file
,
3302 void (*collect_all_refs
)(const char *),
3303 int (*check
)(tree
, cpp_operation
))
3310 pkg_name
= get_ada_package (source_file
);
3312 /* Construct the .ads filename and package name. */
3313 ads_name
= xstrdup (pkg_name
);
3315 for (s
= ads_name
; *s
; s
++)
3321 ads_name
= reconcat (ads_name
, ads_name
, ".ads", NULL
);
3323 /* Write out the .ads file. */
3324 f
= fopen (ads_name
, "w");
3329 pp_needs_newline (&pp
) = true;
3330 pp
.buffer
->stream
= f
;
3332 /* Dump all relevant macros. */
3333 dump_ada_macros (&pp
, source_file
);
3335 /* Reset the table of withs for this file. */
3338 (*collect_all_refs
) (source_file
);
3340 /* Dump all references. */
3342 dump_ada_nodes (&pp
, source_file
);
3344 /* Requires Ada 2005 syntax, so generate corresponding pragma.
3345 Also, disable style checks since this file is auto-generated. */
3346 fprintf (f
, "pragma Ada_2005;\npragma Style_Checks (Off);\n\n");
3351 fprintf (f
, "\npackage %s is\n\n", pkg_name
);
3352 pp_write_text_to_stream (&pp
);
3353 /* ??? need to free pp */
3354 fprintf (f
, "end %s;\n", pkg_name
);
3362 static const char **source_refs
= NULL
;
3363 static int source_refs_used
= 0;
3364 static int source_refs_allocd
= 0;
3366 /* Add an entry for FILENAME to the table SOURCE_REFS. */
3369 collect_source_ref (const char *filename
)
3376 if (source_refs_allocd
== 0)
3378 source_refs_allocd
= 1024;
3379 source_refs
= XNEWVEC (const char *, source_refs_allocd
);
3382 for (i
= 0; i
< source_refs_used
; i
++)
3383 if (filename
== source_refs
[i
])
3386 if (source_refs_used
== source_refs_allocd
)
3388 source_refs_allocd
*= 2;
3389 source_refs
= XRESIZEVEC (const char *, source_refs
, source_refs_allocd
);
3392 source_refs
[source_refs_used
++] = filename
;
3395 /* Main entry point: dump all Ada specs corresponding to SOURCE_REFS
3396 using callbacks COLLECT_ALL_REFS and CHECK.
3397 COLLECT_ALL_REFS is a front-end callback used to collect all relevant
3398 nodes for a given source file.
3399 CHECK is used to perform C++ queries on nodes, or NULL for the C
3403 dump_ada_specs (void (*collect_all_refs
)(const char *),
3404 int (*check
)(tree
, cpp_operation
))
3408 /* Iterate over the list of files to dump specs for */
3409 for (i
= 0; i
< source_refs_used
; i
++)
3410 dump_ads (source_refs
[i
], collect_all_refs
, check
);
3412 /* Free files table. */