1 /* Output Go language descriptions of types.
2 Copyright (C) 2008-2014 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <iant@google.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This file is used during the build process to emit Go language
22 descriptions of declarations from C header files. It uses the
23 debug info hooks to emit the descriptions. The Go language
24 descriptions then become part of the Go runtime support
27 All global names are output with a leading underscore, so that they
28 are all hidden in Go. */
32 #include "coretypes.h"
33 #include "diagnostic-core.h"
39 #include "wide-int-print.h"
40 #include "stor-layout.h"
43 /* We dump this information from the debug hooks. This gives us a
44 stable and maintainable API to hook into. In order to work
45 correctly when -g is used, we build our own hooks structure which
46 wraps the hooks we need to change. */
48 /* Our debug hooks. This is initialized by dump_go_spec_init. */
50 static struct gcc_debug_hooks go_debug_hooks
;
52 /* The real debug hooks. */
54 static const struct gcc_debug_hooks
*real_debug_hooks
;
56 /* The file where we should write information. */
58 static FILE *go_dump_file
;
60 /* A queue of decls to output. */
62 static GTY(()) vec
<tree
, va_gc
> *queue
;
64 /* A hash table of macros we have seen. */
66 static htab_t macro_hash
;
68 /* The type of a value in macro_hash. */
70 struct macro_hash_value
72 /* The name stored in the hash table. */
74 /* The value of the macro. */
78 /* Returns the number of units necessary to represent an integer with the given
79 PRECISION (in bits). */
81 static inline unsigned int
82 precision_to_units (unsigned int precision
)
84 return (precision
+ BITS_PER_UNIT
- 1) / BITS_PER_UNIT
;
87 /* Calculate the hash value for an entry in the macro hash table. */
90 macro_hash_hashval (const void *val
)
92 const struct macro_hash_value
*mhval
= (const struct macro_hash_value
*) val
;
93 return htab_hash_string (mhval
->name
);
96 /* Compare values in the macro hash table for equality. */
99 macro_hash_eq (const void *v1
, const void *v2
)
101 const struct macro_hash_value
*mhv1
= (const struct macro_hash_value
*) v1
;
102 const struct macro_hash_value
*mhv2
= (const struct macro_hash_value
*) v2
;
103 return strcmp (mhv1
->name
, mhv2
->name
) == 0;
106 /* Free values deleted from the macro hash table. */
109 macro_hash_del (void *v
)
111 struct macro_hash_value
*mhv
= (struct macro_hash_value
*) v
;
112 XDELETEVEC (mhv
->name
);
113 XDELETEVEC (mhv
->value
);
117 /* For the string hash tables. */
120 string_hash_eq (const void *y1
, const void *y2
)
122 return strcmp ((const char *) y1
, (const char *) y2
) == 0;
125 /* A macro definition. */
128 go_define (unsigned int lineno
, const char *buffer
)
131 const char *name_end
;
137 struct macro_hash_value
*mhval
;
142 real_debug_hooks
->define (lineno
, buffer
);
144 /* Skip macro functions. */
145 for (p
= buffer
; *p
!= '\0' && *p
!= ' '; ++p
)
158 copy
= XNEWVEC (char, name_end
- buffer
+ 1);
159 memcpy (copy
, buffer
, name_end
- buffer
);
160 copy
[name_end
- buffer
] = '\0';
162 mhval
= XNEW (struct macro_hash_value
);
166 hashval
= htab_hash_string (copy
);
167 slot
= htab_find_slot_with_hash (macro_hash
, mhval
, hashval
, NO_INSERT
);
169 /* For simplicity, we force all names to be hidden by adding an
170 initial underscore, and let the user undo this as needed. */
171 out_len
= strlen (p
) * 2 + 1;
172 out_buffer
= XNEWVEC (char, out_len
);
175 need_operand
= false;
180 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
181 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
182 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
183 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
185 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
186 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
187 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
188 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
192 /* The start of an identifier. Technically we should also
193 worry about UTF-8 identifiers, but they are not a
194 problem for practical uses of -fdump-go-spec so we
195 don't worry about them. */
198 struct macro_hash_value idval
;
204 while (ISALNUM (*p
) || *p
== '_')
206 n
= XALLOCAVEC (char, p
- start
+ 1);
207 memcpy (n
, start
, p
- start
);
211 if (htab_find (macro_hash
, &idval
) == NULL
)
213 /* This is a reference to a name which was not defined
219 memcpy (q
, start
, p
- start
);
223 need_operand
= false;
231 case '0': case '1': case '2': case '3': case '4':
232 case '5': case '6': case '7': case '8': case '9':
239 if (*p
== '0' && (p
[1] == 'x' || p
[1] == 'X'))
244 while (ISDIGIT (*p
) || *p
== '.' || *p
== 'e' || *p
== 'E'
246 && ((*p
>= 'a' && *p
<= 'f')
247 || (*p
>= 'A' && *p
<= 'F'))))
249 memcpy (q
, start
, p
- start
);
251 while (*p
== 'u' || *p
== 'U' || *p
== 'l' || *p
== 'L'
252 || *p
== 'f' || *p
== 'F'
253 || *p
== 'd' || *p
== 'D')
255 /* Go doesn't use any of these trailing type
260 /* We'll pick up the exponent, if any, as an
264 need_operand
= false;
273 /* Always OK, not part of an operand, presumed to start an
277 need_operand
= false;
281 /* OK if we don't need an operand, and presumed to indicate
290 /* Always OK, but not part of an operand. */
295 case '*': case '/': case '%': case '|': case '&': case '^':
296 /* Must be a binary operator. */
308 /* Must be a binary operator. */
320 /* Must be a binary operator. */
329 /* Must be a unary operator. */
337 /* Must be a binary operand, may be << or >> or <= or >=. */
341 if (*p
== *(p
- 1) || *p
== '=')
348 /* Must be a unary operand, must be translated for Go. */
385 case '0': case '1': case '2': case '3':
386 case '4': case '5': case '6': case '7':
388 while (*p
>= '0' && *p
<= '7')
393 /* Go octal characters are always 3
402 while (ISXDIGIT (*p
))
407 /* Go hex characters are always 2 digits. */
412 case 'a': case 'b': case 'f': case 'n': case 'r':
413 case 't': case 'v': case '\\': case '\'': case '"':
424 if (quote
== '\'' && count
!= 1)
428 need_operand
= false;
441 gcc_assert ((size_t) (q
- out_buffer
) < out_len
);
444 mhval
->value
= out_buffer
;
448 slot
= htab_find_slot_with_hash (macro_hash
, mhval
, hashval
, INSERT
);
449 gcc_assert (slot
!= NULL
&& *slot
== NULL
);
454 macro_hash_del (*slot
);
462 fprintf (go_dump_file
, "// unknowndefine %s\n", buffer
);
464 htab_clear_slot (macro_hash
, slot
);
465 XDELETEVEC (out_buffer
);
472 go_undef (unsigned int lineno
, const char *buffer
)
474 struct macro_hash_value mhval
;
477 real_debug_hooks
->undef (lineno
, buffer
);
479 mhval
.name
= CONST_CAST (char *, buffer
);
481 slot
= htab_find_slot (macro_hash
, &mhval
, NO_INSERT
);
483 htab_clear_slot (macro_hash
, slot
);
486 /* A function or variable decl. */
491 if (!TREE_PUBLIC (decl
)
492 || DECL_IS_BUILTIN (decl
)
493 || DECL_NAME (decl
) == NULL_TREE
)
495 vec_safe_push (queue
, decl
);
498 /* A function decl. */
501 go_function_decl (tree decl
)
503 real_debug_hooks
->function_decl (decl
);
507 /* A global variable decl. */
510 go_global_decl (tree decl
)
512 real_debug_hooks
->global_decl (decl
);
516 /* A type declaration. */
519 go_type_decl (tree decl
, int local
)
521 real_debug_hooks
->type_decl (decl
, local
);
523 if (local
|| DECL_IS_BUILTIN (decl
))
525 if (DECL_NAME (decl
) == NULL_TREE
526 && (TYPE_NAME (TREE_TYPE (decl
)) == NULL_TREE
527 || TREE_CODE (TYPE_NAME (TREE_TYPE (decl
))) != IDENTIFIER_NODE
)
528 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
)
530 vec_safe_push (queue
, decl
);
533 /* A container for the data we pass around when generating information
534 at the end of the compilation. */
536 struct godump_container
538 /* DECLs that we have already seen. */
539 hash_set
<tree
> decls_seen
;
541 /* Types which may potentially have to be defined as dummy
543 hash_set
<const char *> pot_dummy_types
;
548 /* Global type definitions. */
554 /* Obstack used to write out a type definition. */
555 struct obstack type_obstack
;
558 /* Append an IDENTIFIER_NODE to OB. */
561 go_append_string (struct obstack
*ob
, tree id
)
563 obstack_grow (ob
, IDENTIFIER_POINTER (id
), IDENTIFIER_LENGTH (id
));
566 /* Given an integer PRECISION in bits, returns a constant string that is the
567 matching go int or uint type (depending on the IS_UNSIGNED flag). Returns a
568 NULL pointer if there is no matching go type. */
571 go_get_uinttype_for_precision (unsigned int precision
, bool is_unsigned
)
576 return is_unsigned
? "uint8" : "int8";
578 return is_unsigned
? "uint16" : "int16";
580 return is_unsigned
? "uint32" : "int32";
582 return is_unsigned
? "uint64" : "int64";
588 /* Append an artificial variable name with the suffix _INDEX to OB. Returns
592 go_append_artificial_name (struct obstack
*ob
, unsigned int index
)
596 /* FIXME: identifier may not be unique. */
597 obstack_grow (ob
, "Godump_", 7);
598 snprintf (buf
, sizeof buf
, "%u", index
);
599 obstack_grow (ob
, buf
, strlen (buf
));
604 /* Append the variable name from DECL to OB. If the name is in the
605 KEYWORD_HASH, prepend an '_'. */
608 go_append_decl_name (struct obstack
*ob
, tree decl
, htab_t keyword_hash
)
610 const char *var_name
;
613 /* Start variable name with an underscore if a keyword. */
614 var_name
= IDENTIFIER_POINTER (DECL_NAME (decl
));
615 slot
= htab_find_slot (keyword_hash
, var_name
, NO_INSERT
);
617 obstack_1grow (ob
, '_');
618 go_append_string (ob
, DECL_NAME (decl
));
621 /* Appends a byte array with the necessary number of elements and the name
622 "Godump_INDEX_pad" to pad from FROM_OFFSET to TO_OFFSET to OB assuming that
623 the next field is automatically aligned to ALIGN_UNITS. Returns INDEX + 1,
624 or INDEX if no padding had to be appended. The resulting offset where the
625 next field is allocated is returned through RET_OFFSET. */
628 go_append_padding (struct obstack
*ob
, unsigned int from_offset
,
629 unsigned int to_offset
, unsigned int align_units
,
630 unsigned int index
, unsigned int *ret_offset
)
632 if (from_offset
% align_units
> 0)
633 from_offset
+= align_units
- (from_offset
% align_units
);
634 gcc_assert (to_offset
>= from_offset
);
635 if (to_offset
> from_offset
)
639 index
= go_append_artificial_name (ob
, index
);
640 snprintf (buf
, sizeof buf
, "_pad [%u]byte; ", to_offset
- from_offset
);
641 obstack_grow (ob
, buf
, strlen (buf
));
643 *ret_offset
= to_offset
;
648 /* Appends an array of type TYPE_STRING with zero elements and the name
649 "Godump_INDEX_align" to OB. If TYPE_STRING is a null pointer, ERROR_STRING
650 is appended instead of the type. Returns INDEX + 1. */
653 go_force_record_alignment (struct obstack
*ob
, const char *type_string
,
654 unsigned int index
, const char *error_string
)
656 index
= go_append_artificial_name (ob
, index
);
657 obstack_grow (ob
, "_align ", 7);
658 if (type_string
== NULL
)
659 obstack_grow (ob
, error_string
, strlen (error_string
));
662 obstack_grow (ob
, "[0]", 3);
663 obstack_grow (ob
, type_string
, strlen (type_string
));
665 obstack_grow (ob
, "; ", 2);
670 /* Write the Go version of TYPE to CONTAINER->TYPE_OBSTACK.
671 USE_TYPE_NAME is true if we can simply use a type name here without
672 needing to define it. IS_FUNC_OK is true if we can output a func
673 type here; the "func" keyword will already have been added.
674 Return true if the type can be represented in Go, false otherwise.
675 P_ART_I is used for indexing artificial elements in nested structures and
676 should always be a NULL pointer when called, except by certain recursive
677 calls from go_format_type() itself. */
680 go_format_type (struct godump_container
*container
, tree type
,
681 bool use_type_name
, bool is_func_ok
, unsigned int *p_art_i
)
685 unsigned int art_i_dummy
;
690 p_art_i
= &art_i_dummy
;
693 ob
= &container
->type_obstack
;
695 if (TYPE_NAME (type
) != NULL_TREE
696 && (container
->decls_seen
.contains (type
)
697 || container
->decls_seen
.contains (TYPE_NAME (type
)))
698 && (AGGREGATE_TYPE_P (type
)
699 || POINTER_TYPE_P (type
)
700 || TREE_CODE (type
) == FUNCTION_TYPE
))
705 name
= TYPE_IDENTIFIER (type
);
707 slot
= htab_find_slot (container
->invalid_hash
, IDENTIFIER_POINTER (name
),
712 obstack_1grow (ob
, '_');
713 go_append_string (ob
, name
);
717 container
->decls_seen
.add (type
);
719 switch (TREE_CODE (type
))
722 obstack_grow (ob
, "int", 3);
729 slot
= htab_find_slot (container
->invalid_hash
,
730 IDENTIFIER_POINTER (DECL_NAME (type
)),
735 obstack_1grow (ob
, '_');
736 go_append_string (ob
, DECL_NAME (type
));
745 s
= go_get_uinttype_for_precision (TYPE_PRECISION (type
),
746 TYPE_UNSIGNED (type
));
749 snprintf (buf
, sizeof buf
, "INVALID-int-%u%s",
750 TYPE_PRECISION (type
),
751 TYPE_UNSIGNED (type
) ? "u" : "");
755 obstack_grow (ob
, s
, strlen (s
));
764 switch (TYPE_PRECISION (type
))
773 snprintf (buf
, sizeof buf
, "INVALID-float-%u",
774 TYPE_PRECISION (type
));
779 obstack_grow (ob
, s
, strlen (s
));
784 obstack_grow (ob
, "bool", 4);
789 && TYPE_NAME (TREE_TYPE (type
)) != NULL_TREE
790 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (type
))
791 || (POINTER_TYPE_P (TREE_TYPE (type
))
792 && (TREE_CODE (TREE_TYPE (TREE_TYPE (type
)))
798 name
= TYPE_IDENTIFIER (TREE_TYPE (type
));
800 slot
= htab_find_slot (container
->invalid_hash
,
801 IDENTIFIER_POINTER (name
), NO_INSERT
);
805 obstack_grow (ob
, "*_", 2);
806 go_append_string (ob
, name
);
808 /* The pointer here can be used without the struct or union
809 definition. So this struct or union is a potential dummy
811 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (type
)))
812 container
->pot_dummy_types
.add (IDENTIFIER_POINTER (name
));
816 if (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
817 obstack_grow (ob
, "func", 4);
819 obstack_1grow (ob
, '*');
820 if (VOID_TYPE_P (TREE_TYPE (type
)))
821 obstack_grow (ob
, "byte", 4);
824 if (!go_format_type (container
, TREE_TYPE (type
), use_type_name
,
831 obstack_1grow (ob
, '[');
832 if (TYPE_DOMAIN (type
) != NULL_TREE
833 && TREE_CODE (TYPE_DOMAIN (type
)) == INTEGER_TYPE
834 && TYPE_MIN_VALUE (TYPE_DOMAIN (type
)) != NULL_TREE
835 && TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type
))) == INTEGER_CST
836 && tree_int_cst_sgn (TYPE_MIN_VALUE (TYPE_DOMAIN (type
))) == 0
837 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) != NULL_TREE
838 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))) == INTEGER_CST
839 && tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))))
843 snprintf (buf
, sizeof buf
, HOST_WIDE_INT_PRINT_DEC
"+1",
844 tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))));
845 obstack_grow (ob
, buf
, strlen (buf
));
848 obstack_1grow (ob
, '0');
849 obstack_1grow (ob
, ']');
850 if (!go_format_type (container
, TREE_TYPE (type
), use_type_name
, false,
857 unsigned int prev_field_end
;
858 unsigned int most_strict_known_alignment
;
861 /* FIXME: Why is this necessary? Without it we can get a core
862 dump on the s390x headers, or from a file containing simply
863 "typedef struct S T;". */
867 most_strict_known_alignment
= 1;
868 obstack_grow (ob
, "struct { ", 9);
869 for (field
= TYPE_FIELDS (type
);
871 field
= TREE_CHAIN (field
))
875 if (TREE_CODE (field
) != FIELD_DECL
)
878 if (DECL_BIT_FIELD (field
))
883 unsigned int decl_align_unit
;
884 unsigned int decl_offset
;
886 decl_align_unit
= DECL_ALIGN_UNIT (field
);
888 TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field
))
890 (TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field
)));
891 if (decl_align_unit
> most_strict_known_alignment
)
892 most_strict_known_alignment
= decl_align_unit
;
893 *p_art_i
= go_append_padding
894 (ob
, prev_field_end
, decl_offset
, decl_align_unit
, *p_art_i
,
896 if (DECL_SIZE_UNIT (field
))
897 prev_field_end
+= TREE_INT_CST_LOW (DECL_SIZE_UNIT (field
));
899 if (DECL_NAME (field
) == NULL
)
900 *p_art_i
= go_append_artificial_name (ob
, *p_art_i
);
902 go_append_decl_name (ob
, field
, container
->keyword_hash
);
903 obstack_1grow (ob
, ' ');
905 /* Do not expand type if a record or union type or a
907 if (TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
908 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
))
909 || (POINTER_TYPE_P (TREE_TYPE (field
))
910 && (TREE_CODE (TREE_TYPE (TREE_TYPE (field
)))
916 name
= TYPE_IDENTIFIER (TREE_TYPE (field
));
918 slot
= htab_find_slot (container
->invalid_hash
,
919 IDENTIFIER_POINTER (name
),
924 obstack_1grow (ob
, '_');
925 go_append_string (ob
, name
);
929 if (!go_format_type (container
, TREE_TYPE (field
), true,
933 obstack_grow (ob
, "; ", 2);
938 /* Alignment and padding as necessary. */
940 unsigned int type_align_unit
;
942 type_align_unit
= TYPE_ALIGN_UNIT (type
);
944 *p_art_i
= go_append_padding
945 (ob
, prev_field_end
, TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type
)),
946 type_align_unit
, *p_art_i
, &prev_field_end
);
947 if (most_strict_known_alignment
< type_align_unit
)
952 /* Enforce proper record alignment. */
953 s
= go_get_uinttype_for_precision
954 (TYPE_ALIGN (type
), TYPE_UNSIGNED (type
));
957 snprintf (buf
, sizeof buf
, "INVALID-int-%u%s",
958 TYPE_ALIGN (type
), TYPE_UNSIGNED (type
) ? "u" : "");
962 *p_art_i
= go_force_record_alignment (ob
, s
, *p_art_i
, buf
);
965 obstack_1grow (ob
, '}');
972 unsigned int sz_units
;
975 sz_units
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type
));
976 s
= go_get_uinttype_for_precision (TYPE_ALIGN (type
), true);
977 obstack_grow (ob
, "struct { ", 9);
981 s
= "INVALID-union-alignment";
982 obstack_grow (ob
, s
, strlen (s
));
989 field
= TYPE_FIELDS (type
);
990 /* Use the same index as the byte field's artificial name for
992 if (field
!= NULL_TREE
&& DECL_NAME (field
) != NULL
)
993 go_append_decl_name (ob
, field
, container
->keyword_hash
);
995 *p_art_i
= go_append_artificial_name (ob
, *p_art_i
);
996 snprintf (buf
, sizeof buf
, " [%u]byte; ", sz_units
);
997 obstack_grow (ob
, buf
, strlen (buf
));
998 if (TYPE_ALIGN_UNIT (type
) > 1)
999 *p_art_i
= go_force_record_alignment (ob
, s
, *p_art_i
, NULL
);
1001 obstack_1grow (ob
, '}');
1010 function_args_iterator iter
;
1013 /* Go has no way to write a type which is a function but not a
1014 pointer to a function. */
1017 obstack_grow (ob
, "func*", 5);
1021 obstack_1grow (ob
, '(');
1022 is_varargs
= stdarg_p (type
);
1024 FOREACH_FUNCTION_ARGS (type
, arg_type
, iter
)
1026 if (VOID_TYPE_P (arg_type
))
1029 obstack_grow (ob
, ", ", 2);
1030 if (!go_format_type (container
, arg_type
, true, false, NULL
))
1036 if (prototype_p (type
))
1037 obstack_grow (ob
, ", ", 2);
1038 obstack_grow (ob
, "...interface{}", 14);
1040 obstack_1grow (ob
, ')');
1042 result
= TREE_TYPE (type
);
1043 if (!VOID_TYPE_P (result
))
1045 obstack_1grow (ob
, ' ');
1046 if (!go_format_type (container
, result
, use_type_name
, false, NULL
))
1053 obstack_grow (ob
, "INVALID-type", 12);
1061 /* Output the type which was built on the type obstack, and then free
1065 go_output_type (struct godump_container
*container
)
1069 ob
= &container
->type_obstack
;
1070 obstack_1grow (ob
, '\0');
1071 fputs ((char *) obstack_base (ob
), go_dump_file
);
1072 obstack_free (ob
, obstack_base (ob
));
1075 /* Output a function declaration. */
1078 go_output_fndecl (struct godump_container
*container
, tree decl
)
1080 if (!go_format_type (container
, TREE_TYPE (decl
), false, true, NULL
))
1081 fprintf (go_dump_file
, "// ");
1082 fprintf (go_dump_file
, "func _%s ",
1083 IDENTIFIER_POINTER (DECL_NAME (decl
)));
1084 go_output_type (container
);
1085 fprintf (go_dump_file
, " __asm__(\"%s\")\n",
1086 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
)));
1089 /* Output a typedef or something like a struct definition. */
1092 go_output_typedef (struct godump_container
*container
, tree decl
)
1094 /* If we have an enum type, output the enum constants
1096 if (TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
1097 && TYPE_SIZE (TREE_TYPE (decl
)) != 0
1098 && !container
->decls_seen
.contains (TREE_TYPE (decl
))
1099 && (TYPE_CANONICAL (TREE_TYPE (decl
)) == NULL_TREE
1100 || !container
->decls_seen
.contains
1101 (TYPE_CANONICAL (TREE_TYPE (decl
)))))
1105 for (element
= TYPE_VALUES (TREE_TYPE (decl
));
1106 element
!= NULL_TREE
;
1107 element
= TREE_CHAIN (element
))
1110 struct macro_hash_value
*mhval
;
1112 char buf
[WIDE_INT_PRINT_BUFFER_SIZE
];
1114 name
= IDENTIFIER_POINTER (TREE_PURPOSE (element
));
1116 /* Sometimes a name will be defined as both an enum constant
1117 and a macro. Avoid duplicate definition errors by
1118 treating enum constants as macros. */
1119 mhval
= XNEW (struct macro_hash_value
);
1120 mhval
->name
= xstrdup (name
);
1121 mhval
->value
= NULL
;
1122 slot
= htab_find_slot (macro_hash
, mhval
, INSERT
);
1124 macro_hash_del (*slot
);
1126 if (tree_fits_shwi_p (TREE_VALUE (element
)))
1127 snprintf (buf
, sizeof buf
, HOST_WIDE_INT_PRINT_DEC
,
1128 tree_to_shwi (TREE_VALUE (element
)));
1129 else if (tree_fits_uhwi_p (TREE_VALUE (element
)))
1130 snprintf (buf
, sizeof buf
, HOST_WIDE_INT_PRINT_UNSIGNED
,
1131 tree_to_uhwi (TREE_VALUE (element
)));
1133 print_hex (element
, buf
);
1135 mhval
->value
= xstrdup (buf
);
1138 container
->decls_seen
.add (TREE_TYPE (decl
));
1139 if (TYPE_CANONICAL (TREE_TYPE (decl
)) != NULL_TREE
)
1140 container
->decls_seen
.add (TYPE_CANONICAL (TREE_TYPE (decl
)));
1143 if (DECL_NAME (decl
) != NULL_TREE
)
1148 type
= IDENTIFIER_POINTER (DECL_NAME (decl
));
1149 /* If type defined already, skip. */
1150 slot
= htab_find_slot (container
->type_hash
, type
, INSERT
);
1153 *slot
= CONST_CAST (void *, (const void *) type
);
1155 if (!go_format_type (container
, TREE_TYPE (decl
), false, false, NULL
))
1157 fprintf (go_dump_file
, "// ");
1158 slot
= htab_find_slot (container
->invalid_hash
, type
, INSERT
);
1159 *slot
= CONST_CAST (void *, (const void *) type
);
1161 fprintf (go_dump_file
, "type _%s ",
1162 IDENTIFIER_POINTER (DECL_NAME (decl
)));
1163 go_output_type (container
);
1165 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl
)))
1167 HOST_WIDE_INT size
= int_size_in_bytes (TREE_TYPE (decl
));
1170 fprintf (go_dump_file
,
1171 "\nconst _sizeof_%s = " HOST_WIDE_INT_PRINT_DEC
,
1172 IDENTIFIER_POINTER (DECL_NAME (decl
)),
1176 container
->decls_seen
.add (decl
);
1178 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl
)))
1184 type
= IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE ((decl
))));
1185 /* If type defined already, skip. */
1186 slot
= htab_find_slot (container
->type_hash
, type
, INSERT
);
1189 *slot
= CONST_CAST (void *, (const void *) type
);
1191 if (!go_format_type (container
, TREE_TYPE (decl
), false, false, NULL
))
1193 fprintf (go_dump_file
, "// ");
1194 slot
= htab_find_slot (container
->invalid_hash
, type
, INSERT
);
1195 *slot
= CONST_CAST (void *, (const void *) type
);
1197 fprintf (go_dump_file
, "type _%s ",
1198 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl
))));
1199 go_output_type (container
);
1201 size
= int_size_in_bytes (TREE_TYPE (decl
));
1203 fprintf (go_dump_file
,
1204 "\nconst _sizeof_%s = " HOST_WIDE_INT_PRINT_DEC
,
1205 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl
))),
1211 fprintf (go_dump_file
, "\n");
1214 /* Output a variable. */
1217 go_output_var (struct godump_container
*container
, tree decl
)
1223 if (container
->decls_seen
.contains (decl
)
1224 || container
->decls_seen
.contains (DECL_NAME (decl
)))
1226 container
->decls_seen
.add (decl
);
1227 container
->decls_seen
.add (DECL_NAME (decl
));
1229 type_name
= TYPE_NAME (TREE_TYPE (decl
));
1231 if (type_name
!= NULL_TREE
&& TREE_CODE (type_name
) == IDENTIFIER_NODE
)
1233 else if (type_name
!= NULL_TREE
&& TREE_CODE (type_name
) == TYPE_DECL
1234 && DECL_SOURCE_LOCATION (type_name
) != BUILTINS_LOCATION
1235 && DECL_NAME (type_name
))
1236 id
= DECL_NAME (type_name
);
1238 && (!htab_find_slot (container
->type_hash
, IDENTIFIER_POINTER (id
),
1240 || htab_find_slot (container
->invalid_hash
, IDENTIFIER_POINTER (id
),
1243 if (id
!= NULL_TREE
)
1247 ob
= &container
->type_obstack
;
1248 obstack_1grow (ob
, '_');
1249 go_append_string (ob
, id
);
1250 is_valid
= htab_find_slot (container
->type_hash
, IDENTIFIER_POINTER (id
),
1254 is_valid
= go_format_type (container
, TREE_TYPE (decl
), true, false, NULL
);
1256 && htab_find_slot (container
->type_hash
,
1257 IDENTIFIER_POINTER (DECL_NAME (decl
)),
1260 /* There is already a type with this name, probably from a
1261 struct tag. Prefer the type to the variable. */
1265 fprintf (go_dump_file
, "// ");
1267 fprintf (go_dump_file
, "var _%s ",
1268 IDENTIFIER_POINTER (DECL_NAME (decl
)));
1269 go_output_type (container
);
1270 fprintf (go_dump_file
, "\n");
1272 /* Sometimes an extern variable is declared with an unknown struct
1274 if (type_name
!= NULL_TREE
&& RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl
)))
1276 if (TREE_CODE (type_name
) == IDENTIFIER_NODE
)
1277 container
->pot_dummy_types
.add (IDENTIFIER_POINTER (type_name
));
1278 else if (TREE_CODE (type_name
) == TYPE_DECL
)
1279 container
->pot_dummy_types
.add
1280 (IDENTIFIER_POINTER (DECL_NAME (type_name
)));
1284 /* Output the final value of a preprocessor macro or enum constant.
1285 This is called via htab_traverse_noresize. */
1288 go_print_macro (void **slot
, void *arg ATTRIBUTE_UNUSED
)
1290 struct macro_hash_value
*mhval
= (struct macro_hash_value
*) *slot
;
1291 fprintf (go_dump_file
, "const _%s = %s\n", mhval
->name
, mhval
->value
);
1295 /* Build a hash table with the Go keywords. */
1297 static const char * const keywords
[] = {
1298 "__asm__", "break", "case", "chan", "const", "continue", "default",
1299 "defer", "else", "fallthrough", "for", "func", "go", "goto", "if",
1300 "import", "interface", "map", "package", "range", "return", "select",
1301 "struct", "switch", "type", "var"
1305 keyword_hash_init (struct godump_container
*container
)
1308 size_t count
= sizeof (keywords
) / sizeof (keywords
[0]);
1311 for (i
= 0; i
< count
; i
++)
1313 slot
= htab_find_slot (container
->keyword_hash
, keywords
[i
], INSERT
);
1314 *slot
= CONST_CAST (void *, (const void *) keywords
[i
]);
1318 /* Traversing the pot_dummy_types and seeing which types are present
1319 in the global types hash table and creating dummy definitions if
1320 not found. This function is invoked by hash_set::traverse. */
1323 find_dummy_types (const char *const &ptr
, godump_container
*adata
)
1325 struct godump_container
*data
= (struct godump_container
*) adata
;
1326 const char *type
= (const char *) ptr
;
1330 slot
= htab_find_slot (data
->type_hash
, type
, NO_INSERT
);
1331 islot
= htab_find_slot (data
->invalid_hash
, type
, NO_INSERT
);
1332 if (slot
== NULL
|| islot
!= NULL
)
1333 fprintf (go_dump_file
, "type _%s struct {}\n", type
);
1337 /* Output symbols. */
1340 go_finish (const char *filename
)
1342 struct godump_container container
;
1346 real_debug_hooks
->finish (filename
);
1348 container
.type_hash
= htab_create (100, htab_hash_string
,
1349 string_hash_eq
, NULL
);
1350 container
.invalid_hash
= htab_create (10, htab_hash_string
,
1351 string_hash_eq
, NULL
);
1352 container
.keyword_hash
= htab_create (50, htab_hash_string
,
1353 string_hash_eq
, NULL
);
1354 obstack_init (&container
.type_obstack
);
1356 keyword_hash_init (&container
);
1358 FOR_EACH_VEC_SAFE_ELT (queue
, ix
, decl
)
1360 switch (TREE_CODE (decl
))
1363 go_output_fndecl (&container
, decl
);
1367 go_output_typedef (&container
, decl
);
1371 go_output_var (&container
, decl
);
1379 htab_traverse_noresize (macro_hash
, go_print_macro
, NULL
);
1381 /* To emit dummy definitions. */
1382 container
.pot_dummy_types
.traverse
<godump_container
*, find_dummy_types
>
1385 htab_delete (container
.type_hash
);
1386 htab_delete (container
.invalid_hash
);
1387 htab_delete (container
.keyword_hash
);
1388 obstack_free (&container
.type_obstack
, NULL
);
1392 if (fclose (go_dump_file
) != 0)
1393 error ("could not close Go dump file: %m");
1394 go_dump_file
= NULL
;
1397 /* Set up our hooks. */
1399 const struct gcc_debug_hooks
*
1400 dump_go_spec_init (const char *filename
, const struct gcc_debug_hooks
*hooks
)
1402 go_dump_file
= fopen (filename
, "w");
1403 if (go_dump_file
== NULL
)
1405 error ("could not open Go dump file %qs: %m", filename
);
1409 go_debug_hooks
= *hooks
;
1410 real_debug_hooks
= hooks
;
1412 go_debug_hooks
.finish
= go_finish
;
1413 go_debug_hooks
.define
= go_define
;
1414 go_debug_hooks
.undef
= go_undef
;
1415 go_debug_hooks
.function_decl
= go_function_decl
;
1416 go_debug_hooks
.global_decl
= go_global_decl
;
1417 go_debug_hooks
.type_decl
= go_type_decl
;
1419 macro_hash
= htab_create (100, macro_hash_hashval
, macro_hash_eq
,
1422 return &go_debug_hooks
;
1425 #include "gt-godump.h"