This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Compiling GCC With a C++ Compiler (g++)
Gabriel Dos Reis <gdr@cs.tamu.edu> writes:
> Zack Weinberg <zack@codesourcery.com> writes:
> | Gabriel Dos Reis <gdr@cs.tamu.edu> writes:
> | > And how do you create it?
> |
> | By casting away the const, as is done in build_string. Or by
> | constructing the data in memory before giving it the type with the
> | const qualifier.
>
> Constructing the data in memory before giving it the type with the const
> qualifier, is morally what C++ constructors do.
Yes. However, a C++ constructor cannot be used here because that's
not part of the intersection of C and C++. A way needs to be found to
declare and initialize the structure without losing the const and
without leaving the intersection of C and C++.
If the declaration were not ill-formed in C++, then we could find a
variant of the build_string code
> memset (s, 0, sizeof (struct tree_common));
> TREE_SET_CODE (s, STRING_CST);
> TREE_STRING_LENGTH (s) = len;
> memcpy ((char *) TREE_STRING_POINTER (s), str, len);
> ((char *) TREE_STRING_POINTER (s))[len] = '\0';
that wasn't undefined behavior, and make everybody happy. For
instance, I think this is well-defined in both C and C++
[GATHER_STATISTICS logic left out for clarity]
tree
build_string (int len, const char *str)
{
char *sc = ggc_alloc (sizeof (struct tree_string) + len);
tree s = (tree) sc;
memset (s, 0, sizeof (struct tree_common));
TREE_SET_CODE (s, STRING_CST);
TREE_STRING_LENGTH (s) = len;
sc += offsetof (struct tree_string, str);
memcpy (sc, str, len);
sc[len] = '\0';
return s;
}
However, we need a way to write the structure declaration too. You
would know better than me how that might be accomplished. (Remember,
only the intersection of C and C++ can be used.)
zw