]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++/modules: Handle forward-declared class types
authorNathaniel Shead <nathanieloshead@gmail.com>
Mon, 21 Oct 2024 11:55:46 +0000 (22:55 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Tue, 22 Oct 2024 11:34:30 +0000 (22:34 +1100)
commit9f9afc65bb7823db84ceeb7981965916c4a583db
tree7e9422f47cf7ccffc21f724f8060335e5f5bd51c
parentd464a52d0678dfea523a60efe8b792ba1b8d40db
c++/modules: Handle forward-declared class types

In some cases we can access members of a namespace-scope class without
ever having performed name-lookup on it; this can occur when a
forward-declaration of the class is used as a return type, for
instance, or with PIMPL.

One possible approach would be to do name lookup in complete_type to
force lazy loading to occur, but this seems overly expensive for a
relatively rare case.  Instead, this patch generalises the existing
pending-entity support to handle this case as well.

Unfortunately this does mean that almost every class definition will be
added to the pending-entity table, and almost always unnecessarily, but
I don't see a good way to avoid this.

gcc/cp/ChangeLog:

* module.cc (depset::DB_IS_MEMBER_BIT): Rename to...
(depset::DB_IS_PENDING_BIT): ...this.
(depset::is_member): Remove.
(depset::is_pending_entity): New function.
(depset::hash::make_dependency): Mark definitions of
namespace-scope types as maybe-pending entities.
(depset::hash::add_class_entities): Rename DB_IS_MEMBER_BIT to
DB_IS_PENDING_BIT.
(depset::hash::find_dependencies): Use is_pending_entity
instead of is_member.
(module_state::write_pendings): Likewise; adjust comment.

gcc/testsuite/ChangeLog:

* g++.dg/modules/inst-4_b.C: Adjust pending-entity count.
* g++.dg/modules/member-def-1_c.C: Likewise.
* g++.dg/modules/member-def-2_c.C: Likewise.
* g++.dg/modules/tpl-spec-3_b.C: Likewise.
* g++.dg/modules/tpl-spec-4_b.C: Likewise.
* g++.dg/modules/tpl-spec-5_b.C: Likewise.
* g++.dg/modules/class-9_a.H: New test.
* g++.dg/modules/class-9_b.H: New test.
* g++.dg/modules/class-9_c.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/module.cc
gcc/testsuite/g++.dg/modules/class-9_a.H [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/class-9_b.H [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/class-9_c.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/inst-4_b.C
gcc/testsuite/g++.dg/modules/member-def-1_c.C
gcc/testsuite/g++.dg/modules/member-def-2_c.C
gcc/testsuite/g++.dg/modules/tpl-spec-3_b.C
gcc/testsuite/g++.dg/modules/tpl-spec-4_b.C
gcc/testsuite/g++.dg/modules/tpl-spec-5_b.C