1 /* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000 Free Software Foundation, Inc.
5 Contributed by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 /* Handle method declarations. */
39 /* Various flags to control the mangling process. */
45 /* The thing we are presently mangling is part of a template type,
46 rather than a fully instantiated type. Therefore, we may see
47 complex expressions where we would normally expect to see a
48 simple integer constant. */
49 mf_maybe_uninstantiated = 1,
50 /* When mangling a numeric value, use the form `_XX_' (instead of
51 just `XX') if the value has more than one digit. */
52 mf_use_underscores_around_value = 2,
55 typedef enum mangling_flags mangling_flags;
57 /* TREE_LIST of the current inline functions that need to be
59 struct pending_inline *pending_inlines;
61 #define obstack_chunk_alloc xmalloc
62 #define obstack_chunk_free free
64 /* Obstack where we build text strings for overloading, etc. */
65 static struct obstack scratch_obstack;
66 static char *scratch_firstobj;
68 static void icat PARAMS ((HOST_WIDE_INT));
69 static void dicat PARAMS ((HOST_WIDE_INT, HOST_WIDE_INT));
70 static int old_backref_index PARAMS ((tree));
71 static int flush_repeats PARAMS ((int, tree));
72 static void build_overload_identifier PARAMS ((tree));
73 static void build_overload_nested_name PARAMS ((tree));
74 static void mangle_expression PARAMS ((tree));
75 static void build_overload_int PARAMS ((tree, mangling_flags));
76 static void build_overload_identifier PARAMS ((tree));
77 static void build_qualified_name PARAMS ((tree));
78 static void build_overload_value PARAMS ((tree, tree, mangling_flags));
79 static void issue_nrepeats PARAMS ((int, tree));
80 static char *build_mangled_name PARAMS ((tree,int,int));
81 static void process_modifiers PARAMS ((tree));
82 static void process_overload_item PARAMS ((tree,int));
83 static void do_build_assign_ref PARAMS ((tree));
84 static void do_build_copy_constructor PARAMS ((tree));
85 static void build_template_template_parm_names PARAMS ((tree));
86 static void build_template_parm_names PARAMS ((tree, tree));
87 static void build_underscore_int PARAMS ((int));
88 static void start_squangling PARAMS ((void));
89 static void end_squangling PARAMS ((void));
90 static int check_ktype PARAMS ((tree, int));
91 static int issue_ktype PARAMS ((tree));
92 static void build_overload_scope_ref PARAMS ((tree));
93 static void build_mangled_template_parm_index PARAMS ((const char *, tree));
94 #if HOST_BITS_PER_WIDE_INT >= 64
95 static void build_mangled_C99_name PARAMS ((int));
97 static int is_back_referenceable_type PARAMS ((tree));
98 static int check_btype PARAMS ((tree));
99 static void build_mangled_name_for_type PARAMS ((tree));
100 static void build_mangled_name_for_type_with_Gcode PARAMS ((tree, int));
102 # define OB_INIT() (scratch_firstobj ? (obstack_free (&scratch_obstack, scratch_firstobj), 0) : 0)
103 # define OB_PUTC(C) (obstack_1grow (&scratch_obstack, (C)))
104 # define OB_PUTC2(C1,C2) \
105 (obstack_1grow (&scratch_obstack, (C1)), obstack_1grow (&scratch_obstack, (C2)))
106 # define OB_PUTS(S) (obstack_grow (&scratch_obstack, (S), sizeof (S) - 1))
107 # define OB_PUTID(ID) \
108 (obstack_grow (&scratch_obstack, IDENTIFIER_POINTER (ID), \
109 IDENTIFIER_LENGTH (ID)))
110 # define OB_PUTCP(S) (obstack_grow (&scratch_obstack, (S), strlen (S)))
111 # define OB_FINISH() (obstack_1grow (&scratch_obstack, '\0'))
112 # define OB_LAST() (obstack_next_free (&scratch_obstack)[-1])
114 /* type tables for K and B type compression */
115 static varray_type btypelist;
116 static varray_type ktypelist;
118 /* number of each type seen */
119 static size_t maxbtype;
120 static size_t maxktype;
122 /* Array of types seen so far in top-level call to `build_mangled_name'.
123 Allocated and deallocated by caller. */
124 static varray_type typevec;
126 /* Number of types interned by `build_mangled_name' so far. */
127 static size_t maxtype;
129 /* Called once to initialize method.c. */
134 gcc_obstack_init (&scratch_obstack);
135 scratch_firstobj = (char *)obstack_alloc (&scratch_obstack, 0);
136 ggc_add_tree_varray_root (&btypelist, 1);
137 ggc_add_tree_varray_root (&ktypelist, 1);
138 ggc_add_tree_varray_root (&typevec, 1);
143 /* This must be large enough to hold any printed integer or floating-point
145 static char digit_buffer[128];
148 /* Here is where overload code starts. */
150 /* Nonzero if we should not try folding parameter types. */
153 /* Nonzero if an underscore is required before adding a digit to the
154 mangled name currently being built. */
155 static int numeric_output_need_bar;
160 /* This function is obsoleted by the new ABI. */
161 my_friendly_assert (!flag_new_abi, 200005222);
163 if (flag_do_squangling)
168 VARRAY_TREE_INIT (btypelist, 50, "btypelist");
169 VARRAY_TREE_INIT (ktypelist, 50, "ktypelist");
176 if (flag_do_squangling)
178 VARRAY_FREE (ktypelist);
179 VARRAY_FREE (btypelist);
185 /* Code to concatenate an asciified integer to a string. */
191 unsigned HOST_WIDE_INT ui;
193 /* Handle this case first, to go really quickly. For many common values,
194 the result of ui/10 below is 1. */
212 OB_PUTC ('0' + (ui % 10));
217 HOST_WIDE_INT lo, hi;
219 unsigned HOST_WIDE_INT ulo, uhi, qlo, qhi;
228 uhi = (lo == 0 ? -hi : -hi-1);
232 && ulo < ((unsigned HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT - 1)))
237 /* Divide 2^HOST_WIDE_INT*uhi+ulo by 10. */
240 qlo = uhi * (((unsigned HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT - 1)) / 5);
243 ulo += uhi * (((unsigned HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT - 1)) % 5)
247 /* Quotient is 2^HOST_WIDE_INT*qhi+qlo, remainder is ulo. */
252 /* Returns the index of TYPE in the typevec, or -1 if it's not there. */
255 old_backref_index (type)
260 if (! is_back_referenceable_type (type))
263 /* The entry for this parm is at maxtype-1, so don't look there for
264 something to repeat. */
265 for (tindex = 0; tindex < maxtype - 1; ++tindex)
266 if (same_type_p (VARRAY_TREE (typevec, tindex), type))
269 if (tindex == maxtype - 1)
275 /* Old mangling style: If TYPE has already been used in the parameter list,
276 emit a backward reference and return non-zero; otherwise, return 0.
278 NREPEATS is the number of repeats we've recorded of this type, or 0 if
279 this is the first time we've seen it and we're just looking to see if
280 it had been used before. */
283 flush_repeats (nrepeats, type)
287 int tindex = old_backref_index (type);
291 my_friendly_assert (nrepeats == 0, 990316);
311 /* Returns nonzero iff this is a type to which we will want to make
312 back-references (using the `B' code). */
315 is_back_referenceable_type (type)
318 /* For some reason, the Java folks don't want back refs on these. */
319 if (TYPE_FOR_JAVA (type))
322 switch (TREE_CODE (type))
325 if (!flag_do_squangling)
326 /* Even though the mangling of this is just `b', we did
327 historically generate back-references for it. */
334 /* These types have single-character manglings, so there's no
335 point in generating back-references. */
338 case TEMPLATE_TYPE_PARM:
339 /* It would be a bit complex to demangle signatures correctly if
340 we generated back-references to these, and the manglings of
341 type parameters are short. */
349 /* Issue the squangling code indicating NREPEATS repetitions of TYPE,
350 which was the last parameter type output. */
353 issue_nrepeats (nrepeats, type)
357 if (nrepeats == 1 && !is_back_referenceable_type (type))
358 /* For types whose manglings are short, don't bother using the
359 repetition code if there's only one repetition, since the
360 repetition code will be about as long as the ordinary mangling. */
361 build_mangled_name_for_type (type);
371 /* Check to see if a tree node has been entered into the Kcode typelist.
372 If not, add it. Returns -1 if it isn't found, otherwise returns the
376 check_ktype (node, add)
381 tree localnode = node;
383 if (ktypelist == NULL)
386 if (TREE_CODE (node) == TYPE_DECL)
387 localnode = TREE_TYPE (node);
389 for (x = 0; x < maxktype; x++)
391 if (same_type_p (localnode, VARRAY_TREE (ktypelist, x)))
394 /* Didn't find it, so add it here. */
397 if (VARRAY_SIZE (ktypelist) <= maxktype)
398 VARRAY_GROW (ktypelist,
399 VARRAY_SIZE (ktypelist) * 3 / 2);
400 VARRAY_TREE (ktypelist, maxktype) = localnode;
412 kindex = check_ktype (decl, FALSE);
424 /* Build a representation for DECL, which may be an entity not at
425 global scope. If so, a marker indicating that the name is
426 qualified has already been output, but the qualifying context has
430 build_overload_nested_name (decl)
435 if (ktypelist && issue_ktype (decl))
438 if (decl == global_namespace)
441 context = CP_DECL_CONTEXT (decl);
443 /* try to issue a K type, and if we can't continue the normal path */
444 if (!(ktypelist && issue_ktype (context)))
446 /* For a template type parameter, we want to output an 'Xn'
447 rather than 'T' or some such. */
448 if (TREE_CODE (context) == TEMPLATE_TYPE_PARM
449 || TREE_CODE (context) == TEMPLATE_TEMPLATE_PARM
450 || TREE_CODE (context) == BOUND_TEMPLATE_TEMPLATE_PARM)
451 build_mangled_name_for_type (context);
454 if (TYPE_P (context))
455 context = TYPE_NAME (context);
456 build_overload_nested_name (context);
460 if (TREE_CODE (decl) == FUNCTION_DECL)
462 static int static_labelno;
464 tree name = DECL_ASSEMBLER_NAME (decl);
467 ASM_FORMAT_PRIVATE_NAME (label, IDENTIFIER_POINTER (name), static_labelno);
470 if (numeric_output_need_bar)
472 icat (strlen (label));
474 numeric_output_need_bar = 1;
476 else if (TREE_CODE (decl) == NAMESPACE_DECL)
477 build_overload_identifier (DECL_NAME (decl));
479 build_overload_identifier (decl);
482 /* Output the decimal representation of I. If I > 9, the decimal
483 representation is preceeded and followed by an underscore. */
486 build_underscore_int (i)
497 build_overload_scope_ref (value)
501 numeric_output_need_bar = 0;
502 build_mangled_name_for_type (TREE_OPERAND (value, 0));
503 build_overload_identifier (TREE_OPERAND (value, 1));
506 /* VALUE is a complex expression. Produce an appropriate mangling.
507 (We are forced to mangle complex expressions when dealing with
508 templates, and an expression involving template parameters appears
509 in the type of a function parameter.) */
512 mangle_expression (value)
515 if (TREE_CODE (value) == SCOPE_REF)
517 build_overload_scope_ref (value);
522 numeric_output_need_bar = 0;
524 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (value))))
527 int operands = TREE_CODE_LENGTH (TREE_CODE (value));
530 name = operator_name_info[TREE_CODE (value)].mangled_name;
532 /* On some erroneous inputs, we can get here with VALUE a
533 LOOKUP_EXPR. We must survive this routine in order to issue
534 a sensible error message, so we fall through to the case
538 for (i = 0; i < operands; ++i)
543 /* We just outputted either the `E' or the name of the
545 numeric_output_need_bar = 0;
548 /* Skip the leading underscores. */
551 operand = TREE_OPERAND (value, i);
552 tc = TREE_CODE (operand);
554 if (TREE_CODE_CLASS (tc) == 't')
555 /* We can get here with sizeof, e.g.:
557 template <class T> void f(A<sizeof(T)>); */
558 build_mangled_name_for_type (operand);
560 build_overload_value (TREE_TYPE (operand),
562 mf_maybe_uninstantiated);
567 /* We don't ever want this output, but it's
568 inconvenient not to be able to build the string.
569 This should cause assembler errors we'll notice. */
573 sprintf (digit_buffer, " *%d", n++);
574 OB_PUTCP (digit_buffer);
578 numeric_output_need_bar = 0;
581 /* Encoding for an INTEGER_CST value. */
584 build_overload_int (value, flags)
586 mangling_flags flags;
588 int multiple_words_p = 0;
589 int multiple_digits_p = 0;
591 if ((flags & mf_maybe_uninstantiated) && TREE_CODE (value) != INTEGER_CST)
593 mangle_expression (value);
597 /* Unless we were looking at an uninstantiated template, integers
598 should always be represented by constants. */
599 my_friendly_assert (TREE_CODE (value) == INTEGER_CST, 243);
601 /* If value doesn't fit in a single HOST_WIDE_INT, we must use a
602 special output routine that can deal with this. */
603 if (! host_integerp (value, 0))
605 multiple_words_p = 1;
606 /* And there is certainly going to be more than one digit. */
607 multiple_digits_p = 1;
610 multiple_digits_p = ((HOST_WIDE_INT) TREE_INT_CST_LOW (value) > 9
611 || (HOST_WIDE_INT) TREE_INT_CST_LOW (value) < -9);
613 /* If necessary, add a leading underscore. */
614 if (multiple_digits_p && (flags & mf_use_underscores_around_value))
617 /* Output the number itself. */
618 if (multiple_words_p)
619 dicat (TREE_INT_CST_LOW (value), TREE_INT_CST_HIGH (value));
621 icat (TREE_INT_CST_LOW (value));
623 if (flags & mf_use_underscores_around_value)
625 if (multiple_digits_p)
627 /* Whether or not there were multiple digits, we don't need an
628 underscore. We've either terminated the number with an
629 underscore, or else it only had one digit. */
630 numeric_output_need_bar = 0;
633 /* We just output a numeric value. */
634 numeric_output_need_bar = 1;
638 /* Output S followed by a representation of the TEMPLATE_PARM_INDEX
639 supplied in INDEX. */
642 build_mangled_template_parm_index (s, index)
647 build_underscore_int (TEMPLATE_PARM_IDX (index));
648 /* We use the LEVEL, not the ORIG_LEVEL, because the mangling is a
649 representation of the function from the point of view of its
651 build_underscore_int (TEMPLATE_PARM_LEVEL (index));
655 /* Mangling for C99 integer types (and Cygnus extensions for 128-bit
656 and other types) is based on the letter "I" followed by the hex
657 representations of the bitsize for the type in question. For
658 encodings that result in larger than two digits, a leading and
659 trailing underscore is added.
670 int256_t = 100 = I_100_
671 int512_t = 200 = I_200_
673 Given an integer in decimal format, mangle according to this scheme. */
675 #if HOST_BITS_PER_WIDE_INT >= 64
677 build_mangled_C99_name (bits)
680 char mangled[10] = "";
683 sprintf (mangled, "I_%x_", bits);
685 sprintf (mangled, "I%.2x", bits);
692 build_overload_value (type, value, flags)
694 mangling_flags flags;
696 my_friendly_assert (TYPE_P (type), 0);
698 while (TREE_CODE (value) == NON_LVALUE_EXPR
699 || TREE_CODE (value) == NOP_EXPR)
700 value = TREE_OPERAND (value, 0);
702 if (numeric_output_need_bar)
705 numeric_output_need_bar = 0;
708 if (TREE_CODE (value) == TEMPLATE_PARM_INDEX)
710 build_mangled_template_parm_index ("Y", value);
714 if (TYPE_PTRMEM_P (type))
716 if (TREE_CODE (value) != PTRMEM_CST)
717 /* We should have already rejected this pointer to member,
718 since it is not a constant. */
719 my_friendly_abort (0);
721 /* Get the actual FIELD_DECL. */
722 value = PTRMEM_CST_MEMBER (value);
723 my_friendly_assert (TREE_CODE (value) == FIELD_DECL, 0);
725 /* Output the name of the field. */
726 build_overload_identifier (DECL_NAME (value));
729 else if (INTEGRAL_TYPE_P (type))
731 build_overload_int (value, flags);
735 /* The only case where we use the extra underscores here is when
736 forming the mangling for an integral non-type template argument.
737 If that didn't happen, stop now. */
738 flags &= ~mf_use_underscores_around_value;
740 switch (TREE_CODE (type))
745 char *bufp = digit_buffer;
747 /* We must handle non-constants in templates. */
748 if (TREE_CODE (value) != REAL_CST)
750 mangle_expression (value);
754 val = TREE_REAL_CST (value);
755 if (REAL_VALUE_ISNAN (val))
757 sprintf (bufp, "NaN");
761 if (REAL_VALUE_NEGATIVE (val))
763 val = REAL_VALUE_NEGATE (val);
766 if (REAL_VALUE_ISINF (val))
768 sprintf (bufp, "Infinity");
772 REAL_VALUE_TO_DECIMAL (val, "%.20e", bufp);
773 bufp = (char *) strchr (bufp, 'e');
775 strcat (digit_buffer, "e0");
801 #ifdef NO_DOT_IN_LABEL
802 bufp = (char *) strchr (bufp, '.');
808 OB_PUTCP (digit_buffer);
809 numeric_output_need_bar = 1;
813 if (TREE_CODE (value) == INTEGER_CST)
815 build_overload_int (value, flags);
818 else if (TREE_CODE (value) == TEMPLATE_PARM_INDEX)
820 build_mangled_template_parm_index ("", value);
821 numeric_output_need_bar = 1;
825 value = TREE_OPERAND (value, 0);
830 if (TREE_CODE (value) == ADDR_EXPR)
831 value = TREE_OPERAND (value, 0);
833 if (TREE_CODE (value) == VAR_DECL)
835 my_friendly_assert (DECL_NAME (value) != 0, 245);
836 build_overload_identifier (DECL_ASSEMBLER_NAME (value));
839 else if (TREE_CODE (value) == FUNCTION_DECL)
841 my_friendly_assert (DECL_NAME (value) != 0, 246);
842 build_overload_identifier (DECL_ASSEMBLER_NAME (value));
845 else if (TREE_CODE (value) == SCOPE_REF)
846 build_overload_scope_ref (value);
848 my_friendly_abort (71);
849 break; /* not really needed */
859 my_friendly_assert (TYPE_PTRMEMFUNC_P (type), 0);
861 /* We'll get a ADDR_EXPR of a SCOPE_REF here if we're
862 mangling, an instantiation of something like:
864 template <class T, void (T::*fp)()> class C {};
865 template <class T> C<T, &T::f> x();
867 We mangle the return type of the function, and that
868 contains template parameters. */
869 if (TREE_CODE (value) == ADDR_EXPR
870 && TREE_CODE (TREE_OPERAND (value, 0)) == SCOPE_REF)
872 build_overload_scope_ref (TREE_OPERAND (value, 0));
876 my_friendly_assert (TREE_CODE (value) == PTRMEM_CST, 0);
878 expand_ptrmemfunc_cst (value, &delta, &idx, &pfn, &delta2);
879 fn = PTRMEM_CST_MEMBER (value);
880 build_overload_int (delta, flags);
884 build_overload_int (idx, flags);
887 else if (DECL_VIRTUAL_P (fn))
889 build_overload_int (DECL_VINDEX (fn), flags);
893 if (!DECL_VIRTUAL_P (fn))
895 numeric_output_need_bar = 0;
896 build_overload_identifier (DECL_ASSEMBLER_NAME (fn));
898 else if (!flag_new_abi)
901 build_overload_int (delta2, flags);
907 sorry ("conversion of %s as template parameter",
908 tree_code_name [(int) TREE_CODE (type)]);
909 my_friendly_abort (72);
914 /* Add encodings for the declaration of template template parameters.
915 PARMLIST must be a TREE_VEC. */
918 build_template_template_parm_names (parmlist)
923 my_friendly_assert (TREE_CODE (parmlist) == TREE_VEC, 990228);
924 nparms = TREE_VEC_LENGTH (parmlist);
926 for (i = 0; i < nparms; i++)
928 tree parm = TREE_VALUE (TREE_VEC_ELT (parmlist, i));
929 if (TREE_CODE (parm) == TYPE_DECL)
931 /* This parameter is a type. */
934 else if (TREE_CODE (parm) == TEMPLATE_DECL)
936 /* This parameter is a template. */
938 build_template_template_parm_names (DECL_INNERMOST_TEMPLATE_PARMS (parm));
941 /* It's a PARM_DECL. */
942 build_mangled_name_for_type (TREE_TYPE (parm));
947 /* Add encodings for the vector of template parameters in PARMLIST,
948 given the vector of arguments to be substituted in ARGLIST. */
951 build_template_parm_names (parmlist, arglist)
956 tree inner_args = INNERMOST_TEMPLATE_ARGS (arglist);
958 nparms = TREE_VEC_LENGTH (parmlist);
960 for (i = 0; i < nparms; i++)
962 tree parm = TREE_VALUE (TREE_VEC_ELT (parmlist, i));
963 tree arg = TREE_VEC_ELT (inner_args, i);
964 if (TREE_CODE (parm) == TYPE_DECL)
966 /* This parameter is a type. */
968 build_mangled_name_for_type (arg);
970 else if (TREE_CODE (parm) == TEMPLATE_DECL)
972 /* This parameter is a template. */
973 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
974 /* Output parameter declaration, argument index and level. */
975 build_mangled_name_for_type (arg);
978 /* A TEMPLATE_DECL node, output the parameter declaration
982 build_template_template_parm_names
983 (DECL_INNERMOST_TEMPLATE_PARMS (parm));
984 icat (IDENTIFIER_LENGTH (DECL_NAME (arg)));
985 OB_PUTID (DECL_NAME (arg));
990 parm = tsubst (parm, inner_args, /*complain=*/1, NULL_TREE);
991 /* It's a PARM_DECL. */
992 build_mangled_name_for_type (TREE_TYPE (parm));
993 build_overload_value (TREE_TYPE (parm), arg,
994 ((mf_maybe_uninstantiated
995 * uses_template_parms (arglist))
996 | mf_use_underscores_around_value));
1001 /* Output the representation for NAME, which is either a TYPE_DECL or
1005 build_overload_identifier (name)
1008 if (TREE_CODE (name) == TYPE_DECL
1009 && CLASS_TYPE_P (TREE_TYPE (name))
1010 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (name))
1011 && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (TREE_TYPE (name)))
1012 || (TREE_CODE (CP_DECL_CONTEXT (CLASSTYPE_TI_TEMPLATE
1013 (TREE_TYPE (name))))
1016 /* NAME is the TYPE_DECL for a template specialization. */
1017 tree template, parmlist, arglist, tname;
1018 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (name));
1019 arglist = CLASSTYPE_TI_ARGS (TREE_TYPE (name));
1020 tname = DECL_NAME (template);
1021 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
1023 icat (IDENTIFIER_LENGTH (tname));
1025 build_template_parm_names (parmlist, arglist);
1029 if (TREE_CODE (name) == TYPE_DECL)
1030 name = DECL_NAME (name);
1031 if (numeric_output_need_bar)
1034 numeric_output_need_bar = 0;
1036 icat (IDENTIFIER_LENGTH (name));
1041 /* Given DECL, either a class TYPE, TYPE_DECL or FUNCTION_DECL, produce
1042 the mangling for it. Used by build_mangled_name and build_static_name. */
1045 build_qualified_name (decl)
1052 decl = TYPE_NAME (decl);
1054 /* If DECL_ASSEMBLER_NAME has been set properly, use it. */
1055 if (TREE_CODE (decl) == TYPE_DECL
1056 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl) && !flag_do_squangling)
1058 tree id = DECL_ASSEMBLER_NAME (decl);
1060 if (ISDIGIT (IDENTIFIER_POINTER (id) [IDENTIFIER_LENGTH (id) - 1]))
1061 numeric_output_need_bar = 1;
1066 /* If we can't find a Ktype, do it the hard way. */
1067 if (check_ktype (context, FALSE) == -1)
1069 /* Count type and namespace scopes. */
1072 context = CP_DECL_CONTEXT (context);
1073 if (context == global_namespace)
1076 if (check_ktype (context, FALSE) != -1)
1079 if (TYPE_P (context))
1080 context = TYPE_NAME (context);
1087 build_underscore_int (i);
1088 numeric_output_need_bar = 0;
1090 build_overload_nested_name (decl);
1093 /* Output the mangled representation for TYPE. If EXTRA_GCODE is
1094 non-zero, mangled names for structure/union types are intentionally
1095 mangled differently from the method described in the ARM. */
1098 build_mangled_name_for_type_with_Gcode (type, extra_Gcode)
1102 if (TYPE_PTRMEMFUNC_P (type))
1103 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
1104 process_modifiers (type);
1105 process_overload_item (type, extra_Gcode);
1108 /* Like build_mangled_name_for_type_with_Gcode, but never outputs the
1112 build_mangled_name_for_type (type)
1115 build_mangled_name_for_type_with_Gcode (type, 0);
1118 /* Given a list of parameters in PARMTYPES, create an unambiguous
1119 overload string. Should distinguish any type that C (or C++) can
1120 distinguish. I.e., pointers to functions are treated correctly.
1122 Caller must deal with whether a final `e' goes on the end or not.
1124 Any default conversions must take place before this function
1127 BEGIN and END control initialization and finalization of the
1128 obstack where we build the string. */
1131 build_overload_name (parmtypes, begin, end)
1137 /* This function is obsoleted by the new ABI. */
1138 my_friendly_assert (!flag_new_abi, 200005221);
1140 start_squangling ();
1141 ret = build_mangled_name (parmtypes, begin, end);
1146 /* Output the mangled representation for PARMTYPES. If PARMTYPES is a
1147 TREE_LIST, then it is a list of parameter types. Otherwise,
1148 PARMTYPES must be a single type. */
1151 build_mangled_name (parmtypes, begin, end)
1155 /* This function is obsoleted by the new ABI. */
1156 my_friendly_assert (!flag_new_abi, 200004105);
1161 if (TREE_CODE (parmtypes) != TREE_LIST)
1162 /* There is only one type. */
1163 build_mangled_name_for_type (parmtypes);
1166 /* There are several types in a parameter list. */
1168 int old_style_repeats = !flag_do_squangling && !nofold && typevec;
1169 tree last_type = NULL_TREE;
1171 for (; parmtypes && parmtypes != void_list_node;
1172 parmtypes = TREE_CHAIN (parmtypes))
1174 /* We used to call canonical_type_variant here, but that isn't
1175 good enough; it doesn't handle pointers to typedef types. So
1176 we can't just set TREE_USED to say we've seen a type already;
1177 we have to check each of the earlier types with same_type_p. */
1178 tree parmtype = TREE_VALUE (parmtypes);
1180 if (old_style_repeats)
1182 /* Every argument gets counted. */
1183 my_friendly_assert (maxtype < VARRAY_SIZE (typevec), 387);
1184 VARRAY_TREE (typevec, maxtype) = parmtype;
1188 if (last_type && same_type_p (parmtype, last_type))
1190 if (flag_do_squangling
1191 || (old_style_repeats
1192 && is_back_referenceable_type (parmtype)))
1194 /* The next type is the same as this one. Keep
1195 track of the repetition, and output the repeat
1201 else if (nrepeats != 0)
1203 /* Indicate how many times the previous parameter was
1205 if (old_style_repeats)
1206 flush_repeats (nrepeats, last_type);
1208 issue_nrepeats (nrepeats, last_type);
1212 last_type = parmtype;
1214 /* Note that for bug-compatibility with 2.7.2, we can't build up
1215 repeats of types other than the most recent one. So we call
1216 flush_repeats every round, if we get this far. */
1217 if (old_style_repeats && flush_repeats (0, parmtype))
1220 /* Output the PARMTYPE. */
1221 build_mangled_name_for_type_with_Gcode (parmtype, 1);
1224 /* Output the repeat count for the last parameter, if
1228 if (old_style_repeats)
1229 flush_repeats (nrepeats, last_type);
1231 issue_nrepeats (nrepeats, last_type);
1236 /* The parameter list ends in an ellipsis. */
1242 return (char *) obstack_base (&scratch_obstack);
1245 /* Emit modifiers such as constant, read-only, and volatile. */
1248 process_modifiers (parmtype)
1251 /* Note that here we do not use CP_TYPE_CONST_P and friends because
1252 we describe types recursively; we will get the `const' in
1253 `const int ()[10]' when processing the `const int' part. */
1254 if (TYPE_READONLY (parmtype))
1256 if (TREE_CODE (parmtype) == INTEGER_TYPE
1257 && ! same_type_p (parmtype, char_type_node)
1258 && ! same_type_p (parmtype, wchar_type_node)
1259 && (TYPE_MAIN_VARIANT (parmtype)
1260 == unsigned_type (TYPE_MAIN_VARIANT (parmtype)))
1261 && ! TYPE_FOR_JAVA (parmtype))
1263 if (TYPE_VOLATILE (parmtype))
1265 /* It would be better to use `R' for `restrict', but that's already
1266 used for reference types. And `r' is used for `long double'. */
1267 if (TYPE_RESTRICT (parmtype))
1271 /* Check to see if TYPE has been entered into the Bcode typelist. If
1272 so, return 1 and emit a backreference to TYPE. Otherwise, add TYPE
1273 to the list of back-referenceable types and return 0. */
1281 if (btypelist == NULL)
1284 if (!is_back_referenceable_type (type))
1287 for (x = 0; x < maxbtype; x++)
1288 if (same_type_p (type, VARRAY_TREE (btypelist, x)))
1297 if (VARRAY_SIZE (btypelist) <= maxbtype)
1298 /* Enlarge the table. */
1299 VARRAY_GROW (btypelist,
1300 VARRAY_SIZE (btypelist) * 3 / 2);
1302 /* Register the TYPE. */
1303 VARRAY_TREE (btypelist, maxbtype) = type;
1309 /* Emit the correct code for various node types. */
1312 process_overload_item (parmtype, extra_Gcode)
1318 numeric_output_need_bar = 0;
1320 /* Our caller should have already handed any qualifiers, so pull out the
1321 TYPE_MAIN_VARIANT to avoid typedef confusion. Except we can't do that
1322 for arrays, because they are transparent to qualifiers. Sigh. */
1323 if (TREE_CODE (parmtype) == ARRAY_TYPE)
1324 parmtype = canonical_type_variant (parmtype);
1326 parmtype = TYPE_MAIN_VARIANT (parmtype);
1328 /* These tree types are considered modifiers for B code squangling,
1329 and therefore should not get entries in the Btypelist. They are,
1330 however, repeatable types. */
1332 switch (TREE_CODE (parmtype))
1334 case REFERENCE_TYPE:
1341 if (TYPE_DOMAIN (parmtype) == NULL_TREE)
1345 tree length = array_type_nelts (parmtype);
1346 if (TREE_CODE (length) != INTEGER_CST || flag_do_squangling)
1348 length = fold (build (PLUS_EXPR, TREE_TYPE (length),
1349 length, integer_one_node));
1350 STRIP_NOPS (length);
1352 build_overload_value (sizetype, length, 1);
1354 if (numeric_output_need_bar && ! flag_do_squangling)
1362 build_mangled_name_for_type (TREE_TYPE (parmtype));
1370 if (flag_do_squangling && check_btype (parmtype))
1371 /* If PARMTYPE is already in the list of back-referenceable types,
1372 then check_btype will output the appropriate reference, and
1373 there's nothing more to do. */
1376 switch (TREE_CODE (parmtype))
1380 build_mangled_name_for_type (TYPE_OFFSET_BASETYPE (parmtype));
1382 build_mangled_name_for_type (TREE_TYPE (parmtype));
1388 tree parms = TYPE_ARG_TYPES (parmtype);
1390 /* Rather than implementing a reentrant TYPEVEC, we turn off
1391 repeat codes here, unless we're squangling. Squangling
1392 doesn't make use of the TYPEVEC, so there's no reentrancy
1394 int old_nofold = nofold;
1395 if (!flag_do_squangling)
1398 if (TREE_CODE (parmtype) == METHOD_TYPE)
1400 /* Mark this as a method. */
1402 /* Output the class of which this method is a member. */
1403 build_mangled_name_for_type (TYPE_METHOD_BASETYPE (parmtype));
1404 /* Output any qualifiers for the `this' parameter. */
1405 process_modifiers (TREE_TYPE (TREE_VALUE (parms)));
1408 /* Output the parameter types. */
1410 if (parms == NULL_TREE)
1412 else if (parms == void_list_node)
1415 build_mangled_name (parms, 0, 0);
1417 /* Output the return type. */
1419 build_mangled_name_for_type (TREE_TYPE (parmtype));
1421 nofold = old_nofold;
1427 if (parmtype == integer_type_node
1428 || parmtype == unsigned_type_node
1429 || parmtype == java_int_type_node)
1431 else if (parmtype == long_integer_type_node
1432 || parmtype == long_unsigned_type_node)
1434 else if (parmtype == short_integer_type_node
1435 || parmtype == short_unsigned_type_node
1436 || parmtype == java_short_type_node)
1438 else if (parmtype == signed_char_type_node)
1443 else if (parmtype == char_type_node
1444 || parmtype == unsigned_char_type_node
1445 || parmtype == java_byte_type_node)
1447 else if (parmtype == wchar_type_node
1448 || parmtype == java_char_type_node)
1450 else if (parmtype == long_long_integer_type_node
1451 || parmtype == long_long_unsigned_type_node
1452 || parmtype == java_long_type_node)
1454 else if (parmtype == java_boolean_type_node)
1456 /* Handle intSI_type_node and such like their C++ equivalents. */
1457 else if (tmp = type_for_mode (TYPE_MODE (parmtype),
1458 TREE_UNSIGNED (parmtype)),
1464 #if HOST_BITS_PER_WIDE_INT >= 64
1467 int bits = TREE_INT_CST_LOW (TYPE_SIZE (parmtype));
1468 build_mangled_C99_name (bits);
1472 my_friendly_abort (73);
1481 if (parmtype == long_double_type_node)
1483 else if (parmtype == double_type_node
1484 || parmtype == java_double_type_node)
1486 else if (parmtype == float_type_node
1487 || parmtype == java_float_type_node)
1489 else my_friendly_abort (74);
1494 build_mangled_name_for_type (TREE_TYPE (parmtype));
1499 build_mangled_name_for_type (TREE_TYPE (parmtype));
1506 case ERROR_MARK: /* not right, but nothing is anyway */
1509 /* have to do these */
1514 OB_PUTC ('G'); /* make it look incompatible with AT&T */
1515 /* drop through into next case */
1519 tree name = TYPE_NAME (parmtype);
1521 my_friendly_assert (TREE_CODE (name) == TYPE_DECL, 248);
1523 build_qualified_name (name);
1528 /* This will take some work. */
1532 case BOUND_TEMPLATE_TEMPLATE_PARM:
1533 /* Find and output the original template parameter
1535 build_mangled_template_parm_index ("tzX",
1536 TEMPLATE_TYPE_PARM_INDEX
1538 build_template_parm_names
1539 (DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (parmtype)),
1540 TYPE_TI_ARGS (parmtype));
1543 case TEMPLATE_TEMPLATE_PARM:
1544 build_mangled_template_parm_index ("ZzX",
1545 TEMPLATE_TYPE_PARM_INDEX
1547 build_template_template_parm_names
1548 (DECL_INNERMOST_TEMPLATE_PARMS (TYPE_STUB_DECL (parmtype)));
1551 case TEMPLATE_TYPE_PARM:
1552 build_mangled_template_parm_index ("X",
1553 TEMPLATE_TYPE_PARM_INDEX
1558 /* When mangling the type of a function template whose
1559 declaration looks like:
1561 template <class T> void foo(typename T::U)
1563 we have to mangle these. */
1564 build_qualified_name (parmtype);
1568 my_friendly_abort (75);
1573 /* Produce the mangling for a variable named NAME in CONTEXT, which can
1574 be either a class TYPE or a FUNCTION_DECL. */
1577 build_static_name (context, name)
1580 /* This function is obsoleted by the new ABI. */
1581 my_friendly_assert (!flag_new_abi, 200004106);
1584 numeric_output_need_bar = 0;
1585 start_squangling ();
1588 build_qualified_name (context);
1591 OB_PUTS ("__static_");
1592 build_qualified_name (context);
1599 return get_identifier ((char *)obstack_base (&scratch_obstack));
1602 /* FOR_METHOD should be 1 if the declaration in question is for a member
1603 of a class (including a static member) and 2 if the declaration is
1604 for a constructor. */
1606 build_decl_overload_real (decl, parms, ret_type, tparms, targs,
1616 enum tree_code operator_code;
1618 /* This function is obsoleted by the new ABI. */
1619 my_friendly_assert (!flag_new_abi, 20000410);
1621 operator_code = DECL_OVERLOADED_OPERATOR_P (decl);
1622 if (!DECL_CONV_FN_P (decl) && operator_code)
1624 /* member operators new and delete look like methods at this
1626 if (! for_method && CP_DECL_CONTEXT (decl) == global_namespace
1627 && parms != NULL_TREE && TREE_CODE (parms) == TREE_LIST
1628 && TREE_CHAIN (parms) == void_list_node)
1629 switch (operator_code)
1632 return get_identifier ("__builtin_delete");
1633 case VEC_DELETE_EXPR:
1634 return get_identifier ("__builtin_vec_delete");
1636 return get_identifier ("__builtin_new");
1638 return get_identifier ("__builtin_vec_new");
1643 if (DECL_ASSIGNMENT_OPERATOR_P (decl))
1644 name = assignment_operator_name_info[(int) operator_code].mangled_name;
1646 name = operator_name_info[(int) operator_code].mangled_name;
1649 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1651 start_squangling ();
1653 if (for_method != 2)
1655 /* Otherwise, we can divine that this is a constructor,
1656 and figure out its name without any extra encoding. */
1658 OB_PUTC2 ('_', '_');
1659 numeric_output_need_bar = 0;
1664 build_template_parm_names (tparms, targs);
1667 else if (!for_method && CP_DECL_CONTEXT (decl) == global_namespace)
1670 if (!for_method && CP_DECL_CONTEXT (decl) != global_namespace)
1671 /* qualify with namespace */
1672 build_qualified_name (CP_DECL_CONTEXT (decl));
1674 if (parms == NULL_TREE)
1676 else if (parms == void_list_node)
1680 if (!flag_do_squangling)
1682 /* Allocate typevec array. */
1683 size_t typevec_size = list_length (parms);
1685 if (!for_method && CP_DECL_CONTEXT (decl) != global_namespace)
1686 /* The namespace of a global function needs one slot. */
1688 VARRAY_TREE_INIT (typevec, typevec_size, "typevec");
1694 tree this_type = TREE_TYPE (TREE_VALUE (parms));
1696 build_mangled_name_for_type (this_type);
1698 if (!flag_do_squangling)
1700 my_friendly_assert (maxtype < VARRAY_SIZE (typevec), 387);
1701 VARRAY_TREE (typevec, maxtype) = this_type;
1705 if (TREE_CHAIN (parms))
1706 build_mangled_name (TREE_CHAIN (parms), 0, 0);
1712 /* the namespace qualifier for a global function
1713 will count as type */
1714 if (CP_DECL_CONTEXT (decl) != global_namespace
1715 && !flag_do_squangling)
1717 my_friendly_assert (maxtype < VARRAY_SIZE (typevec), 387);
1718 VARRAY_TREE (typevec, maxtype) = CP_DECL_CONTEXT (decl);
1721 build_mangled_name (parms, 0, 0);
1724 if (!flag_do_squangling)
1725 /* Deallocate typevec array. */
1726 VARRAY_FREE (typevec);
1729 if (ret_type != NULL_TREE && for_method != 2)
1731 /* Add the return type. */
1733 build_mangled_name_for_type (ret_type);
1739 tree n = get_identifier (obstack_base (&scratch_obstack));
1744 /* Set the mangled name (DECL_ASSEMBLER_NAME) for DECL. */
1747 set_mangled_name_for_decl (decl)
1752 if (processing_template_decl)
1753 /* There's no need to mangle the name of a template function. */
1758 DECL_ASSEMBLER_NAME (decl) = mangle_decl (decl);
1762 parm_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1764 if (DECL_STATIC_FUNCTION_P (decl))
1766 hash_tree_chain (build_pointer_type (DECL_CONTEXT (decl)),
1769 /* The only member functions whose type is a FUNCTION_TYPE, rather
1770 than a METHOD_TYPE, should be static members. */
1771 my_friendly_assert (!DECL_CONTEXT (decl)
1772 || !IS_AGGR_TYPE_CODE (TREE_CODE (DECL_CONTEXT (decl)))
1773 || TREE_CODE (TREE_TYPE (decl)) != FUNCTION_TYPE,
1776 DECL_ASSEMBLER_NAME (decl)
1777 = build_decl_overload_real (decl, parm_types, NULL_TREE,
1778 NULL_TREE, NULL_TREE,
1779 DECL_FUNCTION_MEMBER_P (decl)
1780 + DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl));
1783 /* Build an overload name for the type expression TYPE. */
1786 build_typename_overload (type)
1791 /* This function is obsoleted by the new ABI. */
1792 my_friendly_assert (!flag_new_abi, 200004108);
1795 OB_PUTS (OPERATOR_TYPENAME_FORMAT);
1797 start_squangling ();
1798 build_mangled_name (type, 0, 1);
1799 id = get_identifier (obstack_base (&scratch_obstack));
1800 IDENTIFIER_OPNAME_P (id) = 1;
1801 IDENTIFIER_TYPENAME_P (id) = 1;
1802 TREE_TYPE (id) = type;
1808 build_overload_with_type (name, type)
1811 /* This function is obsoleted by the new ABI. */
1812 my_friendly_assert (!flag_new_abi, 200004109);
1818 start_squangling ();
1819 build_mangled_name (type, 0, 1);
1821 return get_identifier (obstack_base (&scratch_obstack));
1825 get_id_2 (name, name2)
1829 /* This function is obsoleted by the new ABI. */
1830 my_friendly_assert (!flag_new_abi, 20000411);
1836 return get_identifier (obstack_base (&scratch_obstack));
1839 /* Returns the name of a construction vtable group. TYPE is the most
1840 derived class in the hierarhcy. BINFO is the most derived class in
1841 the construction vtable group. */
1844 get_ctor_vtbl_name (type, binfo)
1848 /* This function is obsoleted by the new ABI. */
1849 my_friendly_assert (!flag_new_abi, 200005220);
1851 start_squangling ();
1853 OB_PUTCP (CTOR_VTBL_NAME_PREFIX);
1854 build_mangled_name (type, 0, 0);
1856 build_mangled_name (BINFO_TYPE (binfo), 0, 0);
1858 build_overload_int (BINFO_OFFSET (binfo), mf_none);
1861 return get_identifier (obstack_base (&scratch_obstack));
1864 /* Returns a DECL_ASSEMBLER_NAME for the destructor of type TYPE. */
1867 build_destructor_name (type)
1870 return build_overload_with_type (get_identifier (DESTRUCTOR_DECL_PREFIX),
1874 /* Given a tree_code CODE, and some arguments (at least one),
1875 attempt to use an overloaded operator on the arguments.
1877 For unary operators, only the first argument need be checked.
1878 For binary operators, both arguments may need to be checked.
1880 Member functions can convert class references to class pointers,
1881 for one-level deep indirection. More than that is not supported.
1882 Operators [](), ()(), and ->() must be member functions.
1884 We call function call building calls with LOOKUP_COMPLAIN if they
1885 are our only hope. This is true when we see a vanilla operator
1886 applied to something of aggregate type. If this fails, we are free
1887 to return `error_mark_node', because we will have reported the
1890 Operators NEW and DELETE overload in funny ways: operator new takes
1891 a single `size' parameter, and operator delete takes a pointer to the
1892 storage being deleted. When overloading these operators, success is
1893 assumed. If there is a failure, report an error message and return
1894 `error_mark_node'. */
1898 build_opfncall (code, flags, xarg1, xarg2, arg3)
1899 enum tree_code code;
1901 tree xarg1, xarg2, arg3;
1903 return build_new_op (code, flags, xarg1, xarg2, arg3);
1906 /* This function takes an identifier, ID, and attempts to figure out what
1907 it means. There are a number of possible scenarios, presented in increasing
1910 1) not in a class's scope
1911 2) in class's scope, member name of the class's method
1912 3) in class's scope, but not a member name of the class
1913 4) in class's scope, member name of a class's variable
1915 NAME is $1 from the bison rule. It is an IDENTIFIER_NODE.
1916 VALUE is $$ from the bison rule. It is the value returned by lookup_name ($1)
1918 As a last ditch, try to look up the name as a label and return that
1921 Values which are declared as being of REFERENCE_TYPE are
1922 automatically dereferenced here (as a hack to make the
1923 compiler faster). */
1926 hack_identifier (value, name)
1931 if (value == error_mark_node)
1933 if (current_class_name)
1935 tree fields = lookup_fnfields (TYPE_BINFO (current_class_type), name, 1);
1936 if (fields == error_mark_node)
1937 return error_mark_node;
1942 fndecl = TREE_VALUE (fields);
1943 my_friendly_assert (TREE_CODE (fndecl) == FUNCTION_DECL, 251);
1944 /* I could not trigger this code. MvL */
1945 my_friendly_abort (980325);
1947 if (DECL_CHAIN (fndecl) == NULL_TREE)
1949 warning ("methods cannot be converted to function pointers");
1954 error ("ambiguous request for method pointer `%s'",
1955 IDENTIFIER_POINTER (name));
1956 return error_mark_node;
1961 if (flag_labels_ok && IDENTIFIER_LABEL_VALUE (name))
1963 return IDENTIFIER_LABEL_VALUE (name);
1965 return error_mark_node;
1968 type = TREE_TYPE (value);
1969 if (TREE_CODE (value) == FIELD_DECL)
1971 if (current_class_ptr == NULL_TREE)
1973 if (current_function_decl
1974 && DECL_STATIC_FUNCTION_P (current_function_decl))
1975 cp_error ("invalid use of member `%D' in static member function",
1978 /* We can get here when processing a bad default
1980 struct S { int a; void f(int i = a); } */
1981 cp_error ("invalid use of member `%D'", value);
1983 return error_mark_node;
1985 TREE_USED (current_class_ptr) = 1;
1987 /* Mark so that if we are in a constructor, and then find that
1988 this field was initialized by a base initializer,
1989 we can emit an error message. */
1990 TREE_USED (value) = 1;
1991 value = build_component_ref (current_class_ref, name, NULL_TREE, 1);
1993 else if ((TREE_CODE (value) == FUNCTION_DECL
1994 && DECL_FUNCTION_MEMBER_P (value))
1995 || (TREE_CODE (value) == OVERLOAD
1996 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (value))))
2000 if (TREE_CODE (value) == OVERLOAD)
2001 value = OVL_CURRENT (value);
2003 decl = maybe_dummy_object (DECL_CONTEXT (value), 0);
2004 value = build_component_ref (decl, name, NULL_TREE, 1);
2006 else if (really_overloaded_fn (value))
2008 else if (TREE_CODE (value) == OVERLOAD)
2009 /* not really overloaded function */
2010 mark_used (OVL_FUNCTION (value));
2011 else if (TREE_CODE (value) == TREE_LIST)
2013 /* Ambiguous reference to base members, possibly other cases?. */
2015 while (t && TREE_CODE (t) == TREE_LIST)
2017 mark_used (TREE_VALUE (t));
2021 else if (TREE_CODE (value) == NAMESPACE_DECL)
2023 cp_error ("use of namespace `%D' as expression", value);
2024 return error_mark_node;
2026 else if (DECL_CLASS_TEMPLATE_P (value))
2028 cp_error ("use of class template `%T' as expression", value);
2029 return error_mark_node;
2034 if (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == PARM_DECL
2035 || TREE_CODE (value) == RESULT_DECL)
2037 tree context = decl_function_context (value);
2038 if (context != NULL_TREE && context != current_function_decl
2039 && ! TREE_STATIC (value))
2041 cp_error ("use of %s from containing function",
2042 (TREE_CODE (value) == VAR_DECL
2043 ? "`auto' variable" : "parameter"));
2044 cp_error_at (" `%#D' declared here", value);
2045 value = error_mark_node;
2049 if (DECL_P (value) && DECL_NONLOCAL (value))
2051 if (DECL_CLASS_SCOPE_P (value)
2052 && DECL_CONTEXT (value) != current_class_type)
2055 path = currently_open_derived_class (DECL_CONTEXT (value));
2056 enforce_access (path, value);
2059 else if (TREE_CODE (value) == TREE_LIST
2060 && TREE_TYPE (value) == error_mark_node)
2062 error ("request for member `%s' is ambiguous in multiple inheritance lattice",
2063 IDENTIFIER_POINTER (name));
2064 print_candidates (value);
2065 return error_mark_node;
2068 if (! processing_template_decl)
2069 value = convert_from_reference (value);
2074 /* Return a thunk to FUNCTION. For a virtual thunk, DELTA is the
2075 offset to this used to locate the vptr, and VCALL_INDEX is used to
2076 look up the eventual subobject location. For a non-virtual thunk,
2077 DELTA is the offset to this and VCALL_INDEX is zero. */
2080 make_thunk (function, delta, vcall_index, generate_with_vtable_p)
2084 int generate_with_vtable_p;
2092 /* Scale the VCALL_INDEX to be in terms of bytes. */
2095 = size_binop (MULT_EXPR,
2098 TYPE_SIZE_UNIT (vtable_entry_type)));
2100 vcall_offset = NULL_TREE;
2102 d = tree_low_cst (delta, 0);
2104 if (TREE_CODE (function) != ADDR_EXPR)
2106 func_decl = TREE_OPERAND (function, 0);
2107 if (TREE_CODE (func_decl) != FUNCTION_DECL)
2111 thunk_id = mangle_thunk (TREE_OPERAND (function, 0),
2112 delta, vcall_offset);
2116 OB_PUTS ("__thunk_");
2127 icat (tree_low_cst (vcall_index, 0));
2130 OB_PUTID (DECL_ASSEMBLER_NAME (func_decl));
2132 thunk_id = get_identifier (obstack_base (&scratch_obstack));
2135 thunk = IDENTIFIER_GLOBAL_VALUE (thunk_id);
2136 if (thunk && !DECL_THUNK_P (thunk))
2138 cp_error ("implementation-reserved name `%D' used", thunk_id);
2140 SET_IDENTIFIER_GLOBAL_VALUE (thunk_id, thunk);
2142 if (thunk == NULL_TREE)
2144 thunk = build_decl (FUNCTION_DECL, thunk_id, TREE_TYPE (func_decl));
2145 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (func_decl);
2146 copy_lang_decl (func_decl);
2147 DECL_CONTEXT (thunk) = DECL_CONTEXT (func_decl);
2148 TREE_READONLY (thunk) = TREE_READONLY (func_decl);
2149 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (func_decl);
2150 comdat_linkage (thunk);
2151 SET_DECL_THUNK_P (thunk);
2152 DECL_INITIAL (thunk) = function;
2153 THUNK_DELTA (thunk) = d;
2154 THUNK_VCALL_OFFSET (thunk) = vcall_offset;
2155 THUNK_GENERATE_WITH_VTABLE_P (thunk) = generate_with_vtable_p;
2156 /* The thunk itself is not a constructor or destructor, even if
2157 the thing it is thunking to is. */
2158 DECL_INTERFACE_KNOWN (thunk) = 1;
2159 DECL_NOT_REALLY_EXTERN (thunk) = 1;
2160 DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
2161 DECL_DESTRUCTOR_P (thunk) = 0;
2162 DECL_CONSTRUCTOR_P (thunk) = 0;
2163 DECL_EXTERNAL (thunk) = 1;
2164 DECL_ARTIFICIAL (thunk) = 1;
2165 DECL_VTT_PARM (thunk) = NULL_TREE;
2166 /* Even if this thunk is a member of a local class, we don't
2167 need a static chain. */
2168 DECL_NO_STATIC_CHAIN (thunk) = 1;
2169 /* The THUNK is not a pending inline, even if the FUNC_DECL is. */
2170 DECL_PENDING_INLINE_P (thunk) = 0;
2171 /* Nor has it been deferred. */
2172 DECL_DEFERRED_FN (thunk) = 0;
2173 /* So that finish_file can write out any thunks that need to be: */
2174 pushdecl_top_level (thunk);
2175 /* Create RTL for this thunk so that its address can be taken. */
2176 make_function_rtl (thunk);
2181 /* Emit the definition of a C++ multiple inheritance vtable thunk. */
2184 use_thunk (thunk_fndecl, emit_p)
2192 HOST_WIDE_INT delta;
2194 if (TREE_ASM_WRITTEN (thunk_fndecl))
2197 fnaddr = DECL_INITIAL (thunk_fndecl);
2198 if (TREE_CODE (DECL_INITIAL (thunk_fndecl)) != ADDR_EXPR)
2199 /* We already turned this thunk into an ordinary function.
2200 There's no need to process this thunk again. (We can't just
2201 clear DECL_THUNK_P because that will confuse
2202 FNADDR_FROM_VTABLE_ENTRY and friends.) */
2205 /* Thunks are always addressable; they only appear in vtables. */
2206 TREE_ADDRESSABLE (thunk_fndecl) = 1;
2208 /* Figure out what function is being thunked to. It's referenced in
2209 this translation unit. */
2210 function = TREE_OPERAND (fnaddr, 0);
2211 TREE_ADDRESSABLE (function) = 1;
2212 mark_used (function);
2213 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (function)) = 1;
2217 delta = THUNK_DELTA (thunk_fndecl);
2218 vcall_offset = THUNK_VCALL_OFFSET (thunk_fndecl);
2220 /* And, if we need to emit the thunk, it's used. */
2221 mark_used (thunk_fndecl);
2222 /* This thunk is actually defined. */
2223 DECL_EXTERNAL (thunk_fndecl) = 0;
2225 if (flag_syntax_only)
2227 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
2231 push_to_top_level ();
2233 #ifdef ASM_OUTPUT_MI_THUNK
2237 current_function_decl = thunk_fndecl;
2238 DECL_RESULT (thunk_fndecl)
2239 = build_decl (RESULT_DECL, 0, integer_type_node);
2240 fnname = XSTR (XEXP (DECL_RTL (thunk_fndecl), 0), 0);
2241 init_function_start (thunk_fndecl, input_filename, lineno);
2242 current_function_is_thunk = 1;
2243 assemble_start_function (thunk_fndecl, fnname);
2244 ASM_OUTPUT_MI_THUNK (asm_out_file, thunk_fndecl, delta, function);
2245 assemble_end_function (thunk_fndecl, fnname);
2246 current_function_decl = 0;
2248 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
2251 #endif /* ASM_OUTPUT_MI_THUNK */
2253 /* If we don't have the necessary macro for efficient thunks, generate a
2254 thunk function that just makes a call to the real function.
2255 Unfortunately, this doesn't work for varargs. */
2259 if (varargs_function_p (function))
2260 cp_error ("generic thunk code fails for method `%#D' which uses `...'",
2263 /* Set up clone argument trees for the thunk. */
2265 for (a = DECL_ARGUMENTS (function); a; a = TREE_CHAIN (a))
2267 tree x = copy_node (a);
2269 DECL_CONTEXT (x) = thunk_fndecl;
2273 DECL_ARGUMENTS (thunk_fndecl) = a;
2274 DECL_RESULT (thunk_fndecl) = NULL_TREE;
2276 start_function (NULL_TREE, thunk_fndecl, NULL_TREE, SF_PRE_PARSED);
2278 /* Adjust the this pointer by the constant. */
2279 t = ssize_int (delta);
2280 t = fold (build (PLUS_EXPR, TREE_TYPE (a), a, t));
2281 /* If there's a vcall offset, look up that value in the vtable and
2282 adjust the `this' pointer again. */
2283 if (vcall_offset && !integer_zerop (vcall_offset))
2289 /* The vptr is always at offset zero in the object. */
2290 t = build1 (NOP_EXPR,
2291 build_pointer_type (build_pointer_type
2292 (vtable_entry_type)),
2294 /* Form the vtable address. */
2295 t = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (t)), t);
2296 /* Find the entry with the vcall offset. */
2297 t = build (PLUS_EXPR, TREE_TYPE (t), t, vcall_offset);
2298 /* Calculate the offset itself. */
2299 t = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (t)), t);
2300 /* Adjust the `this' pointer. */
2301 t = fold (build (PLUS_EXPR,
2302 TREE_TYPE (orig_this),
2307 /* Build up the call to the real function. */
2308 t = tree_cons (NULL_TREE, t, NULL_TREE);
2309 for (a = TREE_CHAIN (a); a; a = TREE_CHAIN (a))
2310 t = tree_cons (NULL_TREE, a, t);
2312 t = build_call (function, t);
2313 if (VOID_TYPE_P (TREE_TYPE (t)))
2314 finish_expr_stmt (t);
2316 finish_return_stmt (t);
2318 /* The back-end expects DECL_INITIAL to contain a BLOCK, so we
2320 DECL_INITIAL (thunk_fndecl) = make_node (BLOCK);
2321 BLOCK_VARS (DECL_INITIAL (thunk_fndecl))
2322 = DECL_ARGUMENTS (thunk_fndecl);
2323 expand_body (finish_function (0));
2326 pop_from_top_level ();
2329 /* Code for synthesizing methods which have default semantics defined. */
2331 /* Generate code for default X(X&) constructor. */
2334 do_build_copy_constructor (fndecl)
2337 tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
2340 if (DECL_HAS_IN_CHARGE_PARM_P (fndecl))
2341 parm = TREE_CHAIN (parm);
2342 parm = convert_from_reference (parm);
2344 if (TYPE_HAS_TRIVIAL_INIT_REF (current_class_type)
2345 && is_empty_class (current_class_type))
2346 /* Don't copy the padding byte; it might not have been allocated
2347 if *this is a base subobject. */;
2348 else if (TYPE_HAS_TRIVIAL_INIT_REF (current_class_type))
2350 t = build (INIT_EXPR, void_type_node, current_class_ref, parm);
2351 finish_expr_stmt (t);
2355 tree fields = TYPE_FIELDS (current_class_type);
2356 int n_bases = CLASSTYPE_N_BASECLASSES (current_class_type);
2357 tree binfos = TYPE_BINFO_BASETYPES (current_class_type);
2358 tree member_init_list = NULL_TREE;
2359 tree base_init_list = NULL_TREE;
2360 int cvquals = CP_TYPE_QUALS (TREE_TYPE (parm));
2363 /* Initialize all the base-classes. */
2364 for (t = CLASSTYPE_VBASECLASSES (current_class_type); t;
2367 = tree_cons (BINFO_TYPE (TREE_VALUE (t)), parm,
2369 for (i = 0; i < n_bases; ++i)
2371 t = TREE_VEC_ELT (binfos, i);
2372 if (TREE_VIA_VIRTUAL (t))
2376 = tree_cons (BINFO_TYPE (t), parm, base_init_list);
2379 for (; fields; fields = TREE_CHAIN (fields))
2382 tree field = fields;
2384 if (TREE_CODE (field) != FIELD_DECL)
2388 if (DECL_NAME (field))
2390 if (VFIELD_NAME_P (DECL_NAME (field)))
2392 if (VBASE_NAME_P (DECL_NAME (field)))
2395 /* True for duplicate members. */
2396 if (IDENTIFIER_CLASS_VALUE (DECL_NAME (field)) != field)
2399 else if ((t = TREE_TYPE (field)) != NULL_TREE
2400 && ANON_AGGR_TYPE_P (t)
2401 && TYPE_FIELDS (t) != NULL_TREE)
2402 /* Just use the field; anonymous types can't have
2403 nontrivial copy ctors or assignment ops. */;
2407 init = build (COMPONENT_REF,
2408 build_qualified_type (TREE_TYPE (field), cvquals),
2410 init = build_tree_list (NULL_TREE, init);
2413 = tree_cons (field, init, member_init_list);
2415 member_init_list = nreverse (member_init_list);
2416 base_init_list = nreverse (base_init_list);
2417 setup_vtbl_ptr (member_init_list, base_init_list);
2422 do_build_assign_ref (fndecl)
2425 tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
2428 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
2429 parm = convert_from_reference (parm);
2431 if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type)
2432 && is_empty_class (current_class_type))
2433 /* Don't copy the padding byte; it might not have been allocated
2434 if *this is a base subobject. */;
2435 else if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type))
2437 tree t = build (MODIFY_EXPR, void_type_node, current_class_ref, parm);
2438 finish_expr_stmt (t);
2442 tree fields = TYPE_FIELDS (current_class_type);
2443 int n_bases = CLASSTYPE_N_BASECLASSES (current_class_type);
2444 tree binfos = TYPE_BINFO_BASETYPES (current_class_type);
2445 int cvquals = CP_TYPE_QUALS (TREE_TYPE (parm));
2448 for (i = 0; i < n_bases; ++i)
2450 tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
2451 tree p = build_qualified_type (basetype, cvquals);
2453 p = convert_to_reference
2454 (build_reference_type (p), parm,
2455 CONV_IMPLICIT, LOOKUP_COMPLAIN, NULL_TREE);
2456 p = convert_from_reference (p);
2457 p = build_member_call (basetype, ansi_assopname (NOP_EXPR),
2458 build_tree_list (NULL_TREE, p));
2459 finish_expr_stmt (p);
2461 for (; fields; fields = TREE_CHAIN (fields))
2464 tree field = fields;
2466 if (TREE_CODE (field) != FIELD_DECL)
2469 if (CP_TYPE_CONST_P (TREE_TYPE (field)))
2471 cp_error ("non-static const member `%#D', can't use default assignment operator", field);
2474 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
2476 cp_error ("non-static reference member `%#D', can't use default assignment operator", field);
2480 comp = current_class_ref;
2483 if (DECL_NAME (field))
2485 if (VFIELD_NAME_P (DECL_NAME (field)))
2487 if (VBASE_NAME_P (DECL_NAME (field)))
2490 /* True for duplicate members. */
2491 if (IDENTIFIER_CLASS_VALUE (DECL_NAME (field)) != field)
2494 else if ((t = TREE_TYPE (field)) != NULL_TREE
2495 && ANON_AGGR_TYPE_P (t)
2496 && TYPE_FIELDS (t) != NULL_TREE)
2497 /* Just use the field; anonymous types can't have
2498 nontrivial copy ctors or assignment ops. */;
2502 comp = build (COMPONENT_REF, TREE_TYPE (field), comp, field);
2503 init = build (COMPONENT_REF,
2504 build_qualified_type (TREE_TYPE (field), cvquals),
2507 finish_expr_stmt (build_modify_expr (comp, NOP_EXPR, init));
2510 finish_return_stmt (current_class_ref);
2511 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
2515 synthesize_method (fndecl)
2518 int nested = (current_function_decl != NULL_TREE);
2519 tree context = decl_function_context (fndecl);
2523 import_export_decl (fndecl);
2525 /* If we've been asked to synthesize a clone, just synthesize the
2526 cloned function instead. Doing so will automatically fill in the
2527 body for the clone. */
2528 if (DECL_CLONED_FUNCTION_P (fndecl))
2530 synthesize_method (DECL_CLONED_FUNCTION (fndecl));
2535 push_to_top_level ();
2537 push_function_context_to (context);
2539 /* Put the function definition at the position where it is needed,
2540 rather than within the body of the class. That way, an error
2541 during the generation of the implicit body points at the place
2542 where the attempt to generate the function occurs, giving the
2543 user a hint as to why we are attempting to generate the
2545 DECL_SOURCE_LINE (fndecl) = lineno;
2546 DECL_SOURCE_FILE (fndecl) = input_filename;
2548 interface_unknown = 1;
2549 start_function (NULL_TREE, fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
2552 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
2554 do_build_assign_ref (fndecl);
2557 else if (DECL_DESTRUCTOR_P (fndecl))
2558 setup_vtbl_ptr (NULL_TREE, NULL_TREE);
2561 tree arg_chain = FUNCTION_ARG_CHAIN (fndecl);
2562 if (DECL_HAS_IN_CHARGE_PARM_P (fndecl))
2563 arg_chain = TREE_CHAIN (arg_chain);
2564 if (arg_chain != void_list_node)
2565 do_build_copy_constructor (fndecl);
2566 else if (TYPE_NEEDS_CONSTRUCTING (current_class_type))
2567 setup_vtbl_ptr (NULL_TREE, NULL_TREE);
2570 /* If we haven't yet generated the body of the function, just
2571 generate an empty compound statement. */
2575 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
2576 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
2579 expand_body (finish_function (0));
2581 extract_interface_info ();
2583 pop_from_top_level ();
2585 pop_function_context_from (context);
2588 /* Implicitly declare the special function indicated by KIND, as a
2589 member of TYPE. For copy constructors and assignment operators,
2590 CONST_P indicates whether these functions should take a const
2591 reference argument or a non-const reference. */
2594 implicitly_declare_fn (kind, type, const_p)
2595 special_function_kind kind;
2599 tree declspecs = NULL_TREE;
2600 tree fn, args = NULL_TREE;
2603 tree name = constructor_name (TYPE_IDENTIFIER (type));
2608 case sfk_destructor:
2609 name = build_parse_node (BIT_NOT_EXPR, name);
2610 args = void_list_node;
2613 case sfk_constructor:
2614 /* Default constructor. */
2615 args = void_list_node;
2618 case sfk_copy_constructor:
2620 type = build_qualified_type (type, TYPE_QUAL_CONST);
2621 argtype = build_reference_type (type);
2622 args = tree_cons (NULL_TREE,
2623 build_tree_list (hash_tree_chain (argtype, NULL_TREE),
2624 get_identifier ("_ctor_arg")),
2628 case sfk_assignment_operator:
2630 declspecs = build_tree_list (NULL_TREE, type);
2633 type = build_qualified_type (type, TYPE_QUAL_CONST);
2635 name = ansi_assopname (NOP_EXPR);
2637 argtype = build_reference_type (type);
2638 args = tree_cons (NULL_TREE,
2639 build_tree_list (hash_tree_chain (argtype, NULL_TREE),
2640 get_identifier ("_ctor_arg")),
2645 my_friendly_abort (59);
2648 TREE_PARMLIST (args) = 1;
2651 tree declarator = make_call_declarator (name, args, NULL_TREE, NULL_TREE);
2653 declarator = build_parse_node (ADDR_EXPR, declarator);
2655 fn = grokfield (declarator, declspecs, NULL_TREE, NULL_TREE, NULL_TREE);
2658 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 20000408);
2660 if (kind != sfk_constructor && kind != sfk_destructor)
2661 DECL_ARTIFICIAL (TREE_CHAIN (DECL_ARGUMENTS (fn))) = 1;
2662 DECL_ARTIFICIAL (fn) = 1;
2663 DECL_NOT_REALLY_EXTERN (fn) = 1;
2664 DECL_THIS_INLINE (fn) = 1;
2665 DECL_INLINE (fn) = 1;