[PATCH] D134772: [Clang] Make constraints of auto part of NTTP instead of AutoType
Yuanfang Chen via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 27 15:47:01 PDT 2022
ychen created this revision.
ychen added reviewers: mizvekov, saar.raz, rsmith.
Herald added subscribers: arphaman, martong, kristof.beyls.
Herald added a reviewer: shafik.
Herald added a project: All.
ychen requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
While working on D128750 <https://reviews.llvm.org/D128750>, it is found that AutoType is not uniqued as expected in the partial ordering. This is because
AutoType contains the constraints for the NTTP and the NTTP itself has a reference to the constraint in AutoType.
Due to this restriction, the partial ordering between `template<C auto>` and `template<D auto>` could not be handled the same way
as the partial ordering between `template<int>` and `template<int>`. By P2113 <https://reviews.llvm.org/P2113>, they should work the same way and if it doesn't,
the implementation of P2113 <https://reviews.llvm.org/P2113> would be much harder if at all possible. After this patch, NTTP constraints operates the same as
TTP constraints, which is to say, constraints are represented separately from the type. This is also easier to understand and maintain.
Note that this patch only changes how `constrained auto` is represented in NTTP. Nothing is changed when
`constrained auto` is used as function parameters, as variable declarations etc.
- Add a TypeConstraint to NonTypeTemplateParmDecl in the same way as TemplateTypeParmDecl
- During parsing, parse the constraint as a part of NTTP. This also means the constraint inside AutoType would be empty (and later in partial ordering, AutoType uniquing would work due to this).
- Looking at how the constraint for TemplateTypeParmDecl is handled, add similar/same handling for the constraint for NonTypeTemplateParmDecl.
For (TTP `W` is added for comparsion purposes.)
template <typename T> concept C = true;
template<C auto V, C W> struct A;
AST before this patch:
`-ClassTemplateDecl 0x9091c60 <line:2:1, col:32> col:32 A
|-NonTypeTemplateParmDecl 0x9091818 <col:10, col:17> col:17 referenced 'C auto' depth 0 index 0 V ========> C is part of AutoType
| `-ConceptSpecializationExpr 0x9091948 <col:10> 'bool' Concept 0x90916e8 'C'
| `-TemplateArgument <col:17> type 'decltype(V)'
| `-DecltypeType 0x90918e0 'decltype(V)' dependent
| `-DeclRefExpr 0x9091878 <col:17> 'C auto' NonTypeTemplateParm 0x9091818 'V' 'C auto'
|-TemplateTypeParmDecl 0x90919d0 <col:20, col:22> col:22 Concept 0x90916e8 'C' depth 0 index 1 W
| `-ConceptSpecializationExpr 0x9091b08 <col:20> 'bool' Concept 0x90916e8 'C'
| `-TemplateArgument <col:22> type 'W'
| `-TemplateTypeParmType 0x9091aa0 'W' dependent depth 0 index 1
| `-TemplateTypeParm 0x90919d0 'W'
`-CXXRecordDecl 0x9091bb0 <col:25, col:32> col:32 struct A
AST after this patch:
`-ClassTemplateDecl 0x887dd40 <line:2:1, col:32> col:32 A
|-NonTypeTemplateParmDecl 0x887d8b8 <col:12, col:17> col:17 referenced Concept 0x887d788 'C' 'auto' depth 0 index 0 V ========> C is part of NTTP
| `-ConceptSpecializationExpr 0x887da28 <col:10> 'bool' Concept 0x887d788 'C'
| `-TemplateArgument <col:17> type 'decltype(V)'
| `-DecltypeType 0x887d9c0 'decltype(V)' dependent
| `-DeclRefExpr 0x887d960 <col:17> 'auto' NonTypeTemplateParm 0x887d8b8 'V' 'auto'
|-TemplateTypeParmDecl 0x887dab0 <col:20, col:22> col:22 Concept 0x887d788 'C' depth 0 index 1 W
| `-ConceptSpecializationExpr 0x887dbe8 <col:20> 'bool' Concept 0x887d788 'C'
| `-TemplateArgument <col:22> type 'W'
| `-TemplateTypeParmType 0x887db80 'W' dependent depth 0 index 1
| `-TemplateTypeParm 0x887dab0 'W'
`-CXXRecordDecl 0x887dc90 <col:25, col:32> col:32 struct A
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D134772
Files:
clang/include/clang/AST/ASTNodeTraverser.h
clang/include/clang/AST/DeclTemplate.h
clang/include/clang/AST/RecursiveASTVisitor.h
clang/include/clang/Sema/Sema.h
clang/lib/AST/ASTContext.cpp
clang/lib/AST/ASTImporter.cpp
clang/lib/AST/DeclPrinter.cpp
clang/lib/AST/DeclTemplate.cpp
clang/lib/AST/ODRHash.cpp
clang/lib/AST/TextNodeDumper.cpp
clang/lib/Index/IndexDecl.cpp
clang/lib/Parse/ParseTemplate.cpp
clang/lib/Sema/HLSLExternalSemaSource.cpp
clang/lib/Sema/SemaCodeComplete.cpp
clang/lib/Sema/SemaTemplate.cpp
clang/lib/Sema/SemaTemplateInstantiate.cpp
clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
clang/lib/Serialization/ASTReaderDecl.cpp
clang/lib/Serialization/ASTWriterDecl.cpp
clang/test/SemaTemplate/instantiate-expanded-type-constraint.cpp
clang/tools/libclang/CIndex.cpp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134772.463353.patch
Type: text/x-patch
Size: 51094 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220927/9c47fcfd/attachment-0001.bin>
More information about the cfe-commits
mailing list