This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: C++ PATCH to implement C++20 P1143R2, constinit (PR c++/91360)


On 8/27/19 11:24 AM, Marek Polacek wrote:
On Fri, Aug 23, 2019 at 03:10:37PM -0700, Jason Merrill wrote:
+/* True if DECL is declared 'constinit'.  */
+#define DECL_DECLARED_CONSTINIT_P(DECL) \
+  DECL_LANG_FLAG_0 (VAR_DECL_CHECK (STRIP_TEMPLATE (DECL)))

Hmm, given that 'constinit' only affects the declaration, do we really need
a flag on the VAR_DECL?

I was able to do without DECL_DECLARED_CONSTINIT_P.  To achieve that I
introduced LOOKUP_CONSTINIT (yes, it's an abomination; we should probably
extirpate those LOOKUP_ macros).

But that wasn't enough for variable templates, so I also had to introduce
TINFO_VAR_DECLARED_CONSTINIT.  I suppose those DECL_TEMPLATE_INFO aren't
as precious as the VAR_DECL bits.

Hmm, the existing code limiting the unification of constexpr to class-scope
variables seems wrong:

constexpr float pi = 3.14;
extern const float pi;
constexpr float x = pi; // should be OK

Thanks for fixing this meanwhile.

Is this any better?

Bootstrapped/regtested on x86_64-linux.

2019-08-27  Marek Polacek  <polacek@redhat.com>

	PR c++/91360 - Implement C++20 P1143R2: constinit.
	* c-common.c (c_common_reswords): Add constinit and __constinit.
	(keyword_is_decl_specifier): Handle RID_CONSTINIT.
	* c-common.h (enum rid): Add RID_CONSTINIT, RID_FIRST_CXX20, and
	RID_LAST_CXX20.
	(D_CXX20): Define.
	* c-cppbuiltin.c (c_cpp_builtins): Define __cpp_constinit.
	* c-format.c (cxx_keywords): Add "constinit".
	* c.opt (Wc++2a-compat, Wc++20-compat): New options.

	* cp-tree.h (TINFO_VAR_DECLARED_CONSTINIT): Define.
	(LOOKUP_CONSTINIT): Define.
	(enum cp_decl_spec): Add ds_constinit.
	* decl.c (check_tag_decl): Give an error for constinit in type
	declarations.
	(check_initializer): Also check LOOKUP_CONSTINIT.
	(cp_finish_decl): Add checking for a constinit declaration.  Set
	TINFO_VAR_DECLARED_CONSTINIT.
	(grokdeclarator): Add checking for a declaration with the constinit
	specifier.
	* lex.c (init_reswords): Handle D_CXX20.
	* parser.c (cp_lexer_get_preprocessor_token): Pass a better location
	to warning_at.  Warn about C++20 keywords.
	(cp_keyword_starts_decl_specifier_p): Handle RID_CONSTINIT.
	(cp_parser_diagnose_invalid_type_name): Add an inform about constinit.
	(cp_parser_decomposition_declaration): Maybe pass LOOKUP_CONSTINIT to
	cp_finish_decl.
	(cp_parser_decl_specifier_seq): Handle RID_CONSTINIT.
	(cp_parser_init_declarator): Maybe pass LOOKUP_CONSTINIT to
	cp_finish_decl.
	(set_and_check_decl_spec_loc): Add "constinit".
	* pt.c (tsubst_decl): Set TINFO_VAR_DECLARED_CONSTINIT.
	(instantiate_decl): Maybe pass LOOKUP_CONSTINIT to cp_finish_decl.
	* typeck2.c (store_init_value): If a constinit variable wasn't
	initialized using a constant initializer, give an error.

OK, thanks.

Jason


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]