Chuanqi Xu c5e4afe673
[C++20] [Modules] Support module level lookup (#122887) (#123281)
Close https://github.com/llvm/llvm-project/issues/90154

This patch is also an optimization to the lookup process to utilize the
information provided by `export` keyword.

Previously, in the lookup process, the `export` keyword only takes part
in the check part, it doesn't get involved in the lookup process. That
said, previously, in a name lookup for 'name', we would load all of
declarations with the name 'name' and check if these declarations are
valid or not. It works well. But it is inefficient since it may load
declarations that may not be wanted.

Note that this patch actually did a trick in the lookup process instead
of bring module information to DeclarationName or considering module
information when deciding if two declarations are the same. So it may
not be a surprise to me if there are missing cases. But it is not a
regression. It should be already the case. Issue reports are welcomed.

In this patch, I tried to split the big lookup table into a lookup table
as before and a module local lookup table, which takes a combination of
the ID of the DeclContext and hash value of the primary module name as
the key. And refactored `DeclContext::lookup()` method to take the
module information. So that a lookup in a DeclContext won't load
declarations that are local to **other** modules.

And also I think it is already beneficial to split the big lookup table
since it may reduce the conflicts during lookups in the hash table.

BTW, this patch introduced a **regression** for a reachability rule in
C++20 but it was false-negative. See
'clang/test/CXX/module/module.interface/p7.cpp' for details.

This patch is not expected to introduce any other
regressions for non-c++20-modules users since the module local lookup
table should be empty for them.
2025-01-17 13:41:44 +08:00

67 lines
2.0 KiB
C++

// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: split-file %s %t
//
// RUN: %clang_cc1 -std=c++20 %t/p7.cppm -emit-module-interface -o %t/p7.pcm
// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %t/Use.cpp -verify
//--- p7.cppm
export module p7;
struct reachable {
constexpr static int sv = 43;
int value = 44;
static int getValue() { return 43; }
int get() { return 44; }
template <typename T>
static bool templ_get(T t) { return false; }
typedef int typedef_type;
using using_type = int;
template <typename T>
using templ_using_type = int;
bool operator()() {
return false;
}
enum E { a,
b };
};
export auto getReachable() {
return reachable{};
}
export enum E1 { e1 };
enum E2 { e2 };
export using E2U = E2;
enum E3 { e3 };
export E3 func();
//--- Use.cpp
import p7;
void test() {
auto reachable = getReachable();
int a = decltype(reachable)::sv;
int b = decltype(reachable)::getValue();
int c = reachable.value;
int d = reachable.get();
int e = decltype(reachable)::a;
int f = reachable.templ_get(a);
typename decltype(reachable)::typedef_type g;
typename decltype(reachable)::using_type h;
typename decltype(reachable)::template templ_using_type<int> j;
auto value = reachable();
}
void test2() {
auto a = E1::e1; // OK, namespace-scope name E1 is visible and e1 is reachable
auto b = e1; // OK, namespace-scope name e1 is visible
auto c = E2::e2; // expected-error {{use of undeclared identifier 'E2'}}
auto d = e2; // expected-error {{use of undeclared identifier 'e2'}}
auto e = E2U::e2; // OK, namespace-scope name E2U is visible and E2::e2 is reachable
auto f = E3::e3; // expected-error {{use of undeclared identifier 'E3'}}
auto g = e3; // expected-error {{use of undeclared identifier 'e3'}}
auto h = decltype(func())::e3; // OK, namespace-scope name f is visible and E3::e3 is reachable
}