<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/102290>102290</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[C++20] [Modules] Review the design of DeclContext and Owning Modules
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang:modules
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ChuanqiXu9
</td>
</tr>
</table>
<pre>
(From the discussion in https://github.com/llvm/llvm-project/pull/102115)
# Status quo
Currently, the owning module of a declaration will be set when created in:
https://github.com/llvm/llvm-project/blob/47bf996abe4fafa8192c78c472d68c6519349e90/clang/lib/AST/DeclBase.cpp#L106-L108
But the TU doesn't have a parent context, so it doesn't have owning module at first. Also to ease the process of assigning modules, we will assign the global module fragment, the current named module and the private module fragment as the owning module for the TU: https://github.com/llvm/llvm-project/blob/9dae7fcc927de7ae9878d4c32cb15770271fa8e3/clang/lib/Sema/SemaModule.cpp#L178 , https://github.com/llvm/llvm-project/blob/9dae7fcc927de7ae9878d4c32cb15770271fa8e3/clang/lib/Sema/SemaModule.cpp#L472, https://github.com/llvm/llvm-project/blob/9dae7fcc927de7ae9878d4c32cb15770271fa8e3/clang/lib/Sema/SemaModule.cpp#L568
. Then when we create a new declaration in the GMF or the module purview, we will assign the correct module linkage for the new declaration.
It looks like the root of the problem to change the owning module for the TU decl. But I want to note that the current mechanism works well actually for a long time.
# Possible solutions
I think, the most appropriate solution is to create more decl contexts and not touch the TU decl. For example, when we see a `module;` declaration, we should create and push a GlobalModuleFragmentDecl (which is also DeclContext) to the current decl context stack. Similarly, when we see the module declaration, we should create and push a ModuleDecl. Then we may solve the problem more naturally.
# Related Questions
Then it is an interesting question that, whether or not should the TU be attached to a module? From the specification, the translation units are classified into module units and non-module unit. So maybe it looks better to introduce ModuleUnitDecl as derived class for the TransaltionUnitDecl. Is it?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJz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