clang 23.0.0git
HLSLExternalSemaSource.cpp
Go to the documentation of this file.
1//===--- HLSLExternalSemaSource.cpp - HLSL Sema Source --------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9//
10//===----------------------------------------------------------------------===//
11
15#include "clang/AST/Attr.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/Type.h"
22#include "clang/Sema/Lookup.h"
23#include "clang/Sema/Sema.h"
24#include "clang/Sema/SemaHLSL.h"
26#include "llvm/ADT/SmallVector.h"
27
28using namespace clang;
29using namespace llvm::hlsl;
30
32
34 SemaPtr = &S;
35 ASTContext &AST = SemaPtr->getASTContext();
36 // If the translation unit has external storage force external decls to load.
39
40 IdentifierInfo &HLSL = AST.Idents.get("hlsl", tok::TokenKind::identifier);
42 NamespaceDecl *PrevDecl = nullptr;
44 PrevDecl = Result.getAsSingle<NamespaceDecl>();
45 HLSLNamespace = NamespaceDecl::Create(
46 AST, AST.getTranslationUnitDecl(), /*Inline=*/false, SourceLocation(),
47 SourceLocation(), &HLSL, PrevDecl, /*Nested=*/false);
48 HLSLNamespace->setImplicit(true);
49 HLSLNamespace->setHasExternalLexicalStorage();
50 AST.getTranslationUnitDecl()->addDecl(HLSLNamespace);
51
52 // Force external decls in the HLSL namespace to load from the PCH.
53 (void)HLSLNamespace->getCanonicalDecl()->decls_begin();
54 defineTrivialHLSLTypes();
55 defineHLSLTypesWithForwardDeclarations();
56
57 // This adds a `using namespace hlsl` directive. In DXC, we don't put HLSL's
58 // built in types inside a namespace, but we are planning to change that in
59 // the near future. In order to be source compatible older versions of HLSL
60 // will need to implicitly use the hlsl namespace. For now in clang everything
61 // will get added to the namespace, and we can remove the using directive for
62 // future language versions to match HLSL's evolution.
65 NestedNameSpecifierLoc(), SourceLocation(), HLSLNamespace,
67
69}
70
71void HLSLExternalSemaSource::defineHLSLVectorAlias() {
72 ASTContext &AST = SemaPtr->getASTContext();
73
74 llvm::SmallVector<NamedDecl *> TemplateParams;
75
76 auto *TypeParam = TemplateTypeParmDecl::Create(
77 AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 0,
78 &AST.Idents.get("element", tok::TokenKind::identifier), false, false);
79 TypeParam->setDefaultArgument(
82
83 TemplateParams.emplace_back(TypeParam);
84
85 auto *SizeParam = NonTypeTemplateParmDecl::Create(
86 AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 1,
87 &AST.Idents.get("element_count", tok::TokenKind::identifier), AST.IntTy,
88 false, AST.getTrivialTypeSourceInfo(AST.IntTy));
89 llvm::APInt Val(AST.getIntWidth(AST.IntTy), 4);
90 TemplateArgument Default(AST, llvm::APSInt(std::move(Val)), AST.IntTy,
91 /*IsDefaulted=*/true);
92 SizeParam->setDefaultArgument(
94 SourceLocation(), SizeParam));
95 TemplateParams.emplace_back(SizeParam);
96
97 auto *ParamList =
99 TemplateParams, SourceLocation(), nullptr);
100
101 IdentifierInfo &II = AST.Idents.get("vector", tok::TokenKind::identifier);
102
104 AST.getTemplateTypeParmType(0, 0, false, TypeParam),
106 AST, NestedNameSpecifierLoc(), SourceLocation(), SizeParam, false,
107 DeclarationNameInfo(SizeParam->getDeclName(), SourceLocation()),
108 AST.IntTy, VK_LValue),
110
111 auto *Record = TypeAliasDecl::Create(AST, HLSLNamespace, SourceLocation(),
112 SourceLocation(), &II,
113 AST.getTrivialTypeSourceInfo(AliasType));
114 Record->setImplicit(true);
115
116 auto *Template =
117 TypeAliasTemplateDecl::Create(AST, HLSLNamespace, SourceLocation(),
118 Record->getIdentifier(), ParamList, Record);
119
120 Record->setDescribedAliasTemplate(Template);
121 Template->setImplicit(true);
122 Template->setLexicalDeclContext(Record->getDeclContext());
123 HLSLNamespace->addDecl(Template);
124}
125
126void HLSLExternalSemaSource::defineHLSLMatrixAlias() {
127 ASTContext &AST = SemaPtr->getASTContext();
128 llvm::SmallVector<NamedDecl *> TemplateParams;
129
130 auto *TypeParam = TemplateTypeParmDecl::Create(
131 AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 0,
132 &AST.Idents.get("element", tok::TokenKind::identifier), false, false);
133 TypeParam->setDefaultArgument(
134 AST, SemaPtr->getTrivialTemplateArgumentLoc(
136
137 TemplateParams.emplace_back(TypeParam);
138
139 // these should be 64 bit to be consistent with other clang matrices.
140 auto *RowsParam = NonTypeTemplateParmDecl::Create(
141 AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 1,
142 &AST.Idents.get("rows_count", tok::TokenKind::identifier), AST.IntTy,
143 false, AST.getTrivialTypeSourceInfo(AST.IntTy));
144 llvm::APInt RVal(AST.getIntWidth(AST.IntTy), 4);
145 TemplateArgument RDefault(AST, llvm::APSInt(std::move(RVal)), AST.IntTy,
146 /*IsDefaulted=*/true);
147 RowsParam->setDefaultArgument(
148 AST, SemaPtr->getTrivialTemplateArgumentLoc(RDefault, AST.IntTy,
149 SourceLocation(), RowsParam));
150 TemplateParams.emplace_back(RowsParam);
151
152 auto *ColsParam = NonTypeTemplateParmDecl::Create(
153 AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 2,
154 &AST.Idents.get("cols_count", tok::TokenKind::identifier), AST.IntTy,
155 false, AST.getTrivialTypeSourceInfo(AST.IntTy));
156 llvm::APInt CVal(AST.getIntWidth(AST.IntTy), 4);
157 TemplateArgument CDefault(AST, llvm::APSInt(std::move(CVal)), AST.IntTy,
158 /*IsDefaulted=*/true);
159 ColsParam->setDefaultArgument(
160 AST, SemaPtr->getTrivialTemplateArgumentLoc(CDefault, AST.IntTy,
161 SourceLocation(), ColsParam));
162 TemplateParams.emplace_back(ColsParam);
163
164 const unsigned MaxMatDim = SemaPtr->getLangOpts().MaxMatrixDimension;
165
166 auto *MaxRow = IntegerLiteral::Create(
167 AST, llvm::APInt(AST.getIntWidth(AST.IntTy), MaxMatDim), AST.IntTy,
169 auto *MaxCol = IntegerLiteral::Create(
170 AST, llvm::APInt(AST.getIntWidth(AST.IntTy), MaxMatDim), AST.IntTy,
172
173 auto *RowsRef = DeclRefExpr::Create(
174 AST, NestedNameSpecifierLoc(), SourceLocation(), RowsParam,
175 /*RefersToEnclosingVariableOrCapture*/ false,
176 DeclarationNameInfo(RowsParam->getDeclName(), SourceLocation()),
177 AST.IntTy, VK_LValue);
178 auto *ColsRef = DeclRefExpr::Create(
179 AST, NestedNameSpecifierLoc(), SourceLocation(), ColsParam,
180 /*RefersToEnclosingVariableOrCapture*/ false,
181 DeclarationNameInfo(ColsParam->getDeclName(), SourceLocation()),
182 AST.IntTy, VK_LValue);
183
184 auto *RowsLE = BinaryOperator::Create(AST, RowsRef, MaxRow, BO_LE, AST.BoolTy,
187 auto *ColsLE = BinaryOperator::Create(AST, ColsRef, MaxCol, BO_LE, AST.BoolTy,
190
192 AST, RowsLE, ColsLE, BO_LAnd, AST.BoolTy, VK_PRValue, OK_Ordinary,
194
195 auto *ParamList = TemplateParameterList::Create(
196 AST, SourceLocation(), SourceLocation(), TemplateParams, SourceLocation(),
198
199 IdentifierInfo &II = AST.Idents.get("matrix", tok::TokenKind::identifier);
200
202 AST.getTemplateTypeParmType(0, 0, false, TypeParam),
204 AST, NestedNameSpecifierLoc(), SourceLocation(), RowsParam, false,
205 DeclarationNameInfo(RowsParam->getDeclName(), SourceLocation()),
206 AST.IntTy, VK_LValue),
208 AST, NestedNameSpecifierLoc(), SourceLocation(), ColsParam, false,
209 DeclarationNameInfo(ColsParam->getDeclName(), SourceLocation()),
210 AST.IntTy, VK_LValue),
212
213 auto *Record = TypeAliasDecl::Create(AST, HLSLNamespace, SourceLocation(),
214 SourceLocation(), &II,
215 AST.getTrivialTypeSourceInfo(AliasType));
216 Record->setImplicit(true);
217
218 auto *Template =
219 TypeAliasTemplateDecl::Create(AST, HLSLNamespace, SourceLocation(),
220 Record->getIdentifier(), ParamList, Record);
221
222 Record->setDescribedAliasTemplate(Template);
223 Template->setImplicit(true);
224 Template->setLexicalDeclContext(Record->getDeclContext());
225 HLSLNamespace->addDecl(Template);
226}
227
228void HLSLExternalSemaSource::defineTrivialHLSLTypes() {
229 defineHLSLVectorAlias();
230 defineHLSLMatrixAlias();
231}
232
233/// Set up common members and attributes for buffer types
235 ResourceClass RC, bool IsROV,
236 bool RawBuffer, bool HasCounter) {
238 .addBufferHandles(RC, IsROV, RawBuffer, HasCounter)
243}
244
245/// Set up common members and attributes for sampler types
254
255/// Set up common members and attributes for texture types
279
280// Add a partial specialization for a template. The `TextureTemplate` is
281// `Texture<element_type>`, and it will be specialized for vectors:
282// `Texture<vector<element_type, element_count>>`.
285 ClassTemplateDecl *TextureTemplate) {
286 ASTContext &AST = S.getASTContext();
287
288 // Create the template parameters: element_type and element_count.
289 auto *ElementType = TemplateTypeParmDecl::Create(
290 AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 0,
291 &AST.Idents.get("element_type"), false, false);
292 auto *ElementCount = NonTypeTemplateParmDecl::Create(
293 AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 1,
294 &AST.Idents.get("element_count"), AST.IntTy, false,
296
297 auto *TemplateParams = TemplateParameterList::Create(
298 AST, SourceLocation(), SourceLocation(), {ElementType, ElementCount},
299 SourceLocation(), nullptr);
300
301 // Create the dependent vector type: vector<element_type, element_count>.
303 AST.getTemplateTypeParmType(0, 0, false, ElementType),
305 AST, NestedNameSpecifierLoc(), SourceLocation(), ElementCount, false,
306 DeclarationNameInfo(ElementCount->getDeclName(), SourceLocation()),
307 AST.IntTy, VK_LValue),
309
310 // Create the partial specialization declaration.
311 QualType CanonInjectedTST =
315
317 AST, TagDecl::TagKind::Class, HLSLNamespace, SourceLocation(),
318 SourceLocation(), TemplateParams, TextureTemplate,
320 CanQualType::CreateUnsafe(CanonInjectedTST), nullptr);
321
322 // Set the template arguments as written.
324 TemplateArgumentLoc ArgLoc =
326 TemplateArgumentListInfo ArgsInfo =
328 ArgsInfo.addArgument(ArgLoc);
329 PartialSpec->setTemplateArgsAsWritten(
331
332 PartialSpec->setImplicit(true);
333 PartialSpec->setLexicalDeclContext(HLSLNamespace);
334 PartialSpec->setHasExternalLexicalStorage();
335
336 // Add the partial specialization to the namespace and the class template.
337 HLSLNamespace->addDecl(PartialSpec);
338 TextureTemplate->AddPartialSpecialization(PartialSpec, nullptr);
339
340 return PartialSpec;
341}
342
343// This function is responsible for constructing the constraint expression for
344// this concept:
345// template<typename T> concept is_typed_resource_element_compatible =
346// __is_typed_resource_element_compatible<T>;
349 ASTContext &Context = S.getASTContext();
350
351 // Obtain the QualType for 'bool'
352 QualType BoolTy = Context.BoolTy;
353
354 // Create a QualType that points to this TemplateTypeParmDecl
355 QualType TType = Context.getTypeDeclType(T);
356
357 // Create a TypeSourceInfo for the template type parameter 'T'
358 TypeSourceInfo *TTypeSourceInfo =
359 Context.getTrivialTypeSourceInfo(TType, NameLoc);
360
361 TypeTraitExpr *TypedResExpr = TypeTraitExpr::Create(
362 Context, BoolTy, NameLoc, UTT_IsTypedResourceElementCompatible,
363 {TTypeSourceInfo}, NameLoc, true);
364
365 return TypedResExpr;
366}
367
368// This function is responsible for constructing the constraint expression for
369// this concept:
370// template<typename T> concept is_structured_resource_element_compatible =
371// !__is_intangible<T> && sizeof(T) >= 1;
373 SourceLocation NameLoc,
375 ASTContext &Context = S.getASTContext();
376
377 // Obtain the QualType for 'bool'
378 QualType BoolTy = Context.BoolTy;
379
380 // Create a QualType that points to this TemplateTypeParmDecl
381 QualType TType = Context.getTypeDeclType(T);
382
383 // Create a TypeSourceInfo for the template type parameter 'T'
384 TypeSourceInfo *TTypeSourceInfo =
385 Context.getTrivialTypeSourceInfo(TType, NameLoc);
386
387 TypeTraitExpr *IsIntangibleExpr =
388 TypeTraitExpr::Create(Context, BoolTy, NameLoc, UTT_IsIntangibleType,
389 {TTypeSourceInfo}, NameLoc, true);
390
391 // negate IsIntangibleExpr
392 UnaryOperator *NotIntangibleExpr = UnaryOperator::Create(
393 Context, IsIntangibleExpr, UO_LNot, BoolTy, VK_LValue, OK_Ordinary,
394 NameLoc, false, FPOptionsOverride());
395
396 // element types also may not be of 0 size
397 UnaryExprOrTypeTraitExpr *SizeOfExpr = new (Context) UnaryExprOrTypeTraitExpr(
398 UETT_SizeOf, TTypeSourceInfo, BoolTy, NameLoc, NameLoc);
399
400 // Create a BinaryOperator that checks if the size of the type is not equal to
401 // 1 Empty structs have a size of 1 in HLSL, so we need to check for that
403 Context, llvm::APInt(Context.getTypeSize(Context.getSizeType()), 1, true),
404 Context.getSizeType(), NameLoc);
405
406 BinaryOperator *SizeGEQOneExpr =
407 BinaryOperator::Create(Context, SizeOfExpr, rhs, BO_GE, BoolTy, VK_LValue,
408 OK_Ordinary, NameLoc, FPOptionsOverride());
409
410 // Combine the two constraints
412 Context, NotIntangibleExpr, SizeGEQOneExpr, BO_LAnd, BoolTy, VK_LValue,
413 OK_Ordinary, NameLoc, FPOptionsOverride());
414
415 return CombinedExpr;
416}
417
419 bool isTypedBuffer) {
420 ASTContext &Context = S.getASTContext();
421 DeclContext *DC = NSD->getDeclContext();
422 SourceLocation DeclLoc = SourceLocation();
423
424 IdentifierInfo &ElementTypeII = Context.Idents.get("element_type");
426 Context, NSD->getDeclContext(), DeclLoc, DeclLoc,
427 /*D=*/0,
428 /*P=*/0,
429 /*Id=*/&ElementTypeII,
430 /*Typename=*/true,
431 /*ParameterPack=*/false);
432
433 T->setDeclContext(DC);
434 T->setReferenced();
435
436 // Create and Attach Template Parameter List to ConceptDecl
438 Context, DeclLoc, DeclLoc, {T}, DeclLoc, nullptr);
439
440 DeclarationName DeclName;
441 Expr *ConstraintExpr = nullptr;
442
443 if (isTypedBuffer) {
444 DeclName = DeclarationName(
445 &Context.Idents.get("__is_typed_resource_element_compatible"));
446 ConstraintExpr = constructTypedBufferConstraintExpr(S, DeclLoc, T);
447 } else {
448 DeclName = DeclarationName(
449 &Context.Idents.get("__is_structured_resource_element_compatible"));
450 ConstraintExpr = constructStructuredBufferConstraintExpr(S, DeclLoc, T);
451 }
452
453 // Create a ConceptDecl
454 ConceptDecl *CD =
455 ConceptDecl::Create(Context, NSD->getDeclContext(), DeclLoc, DeclName,
456 ConceptParams, ConstraintExpr);
457
458 // Attach the template parameter list to the ConceptDecl
459 CD->setTemplateParameters(ConceptParams);
460
461 // Add the concept declaration to the Translation Unit Decl
462 NSD->getDeclContext()->addDecl(CD);
463
464 return CD;
465}
466
467void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
468 ASTContext &AST = SemaPtr->getASTContext();
469 CXXRecordDecl *Decl;
470 ConceptDecl *TypedBufferConcept = constructBufferConceptDecl(
471 *SemaPtr, HLSLNamespace, /*isTypedBuffer*/ true);
472 ConceptDecl *StructuredBufferConcept = constructBufferConceptDecl(
473 *SemaPtr, HLSLNamespace, /*isTypedBuffer*/ false);
474
475 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "Buffer")
476 .addSimpleTemplateParams({"element_type"}, TypedBufferConcept)
477 .finalizeForwardDeclaration();
478
479 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
480 setupBufferType(Decl, *SemaPtr, ResourceClass::SRV, /*IsROV=*/false,
481 /*RawBuffer=*/false, /*HasCounter=*/false)
486 });
487
488 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWBuffer")
489 .addSimpleTemplateParams({"element_type"}, TypedBufferConcept)
490 .finalizeForwardDeclaration();
491
492 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
493 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/false,
494 /*RawBuffer=*/false, /*HasCounter=*/false)
499 });
500
501 Decl =
502 BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RasterizerOrderedBuffer")
503 .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
504 .finalizeForwardDeclaration();
505 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
506 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/true,
507 /*RawBuffer=*/false, /*HasCounter=*/false)
512 });
513
514 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "StructuredBuffer")
515 .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
516 .finalizeForwardDeclaration();
517 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
518 setupBufferType(Decl, *SemaPtr, ResourceClass::SRV, /*IsROV=*/false,
519 /*RawBuffer=*/true, /*HasCounter=*/false)
524 });
525
526 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWStructuredBuffer")
527 .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
528 .finalizeForwardDeclaration();
529 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
530 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/false,
531 /*RawBuffer=*/true, /*HasCounter=*/true)
538 });
539
540 Decl =
541 BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "AppendStructuredBuffer")
542 .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
543 .finalizeForwardDeclaration();
544 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
545 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/false,
546 /*RawBuffer=*/true, /*HasCounter=*/true)
550 });
551
552 Decl =
553 BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "ConsumeStructuredBuffer")
554 .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
555 .finalizeForwardDeclaration();
556 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
557 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/false,
558 /*RawBuffer=*/true, /*HasCounter=*/true)
562 });
563
564 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace,
565 "RasterizerOrderedStructuredBuffer")
566 .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
567 .finalizeForwardDeclaration();
568 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
569 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/true,
570 /*RawBuffer=*/true, /*HasCounter=*/true)
577 });
578
579 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "ByteAddressBuffer")
580 .finalizeForwardDeclaration();
581 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
582 setupBufferType(Decl, *SemaPtr, ResourceClass::SRV, /*IsROV=*/false,
583 /*RawBuffer=*/true, /*HasCounter=*/false)
587 });
588 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWByteAddressBuffer")
589 .finalizeForwardDeclaration();
590 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
591 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/false,
592 /*RawBuffer=*/true, /*HasCounter=*/false)
597 });
598 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace,
599 "RasterizerOrderedByteAddressBuffer")
600 .finalizeForwardDeclaration();
601 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
602 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/true,
603 /*RawBuffer=*/true, /*HasCounter=*/false)
606 });
607
608 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "SamplerState")
609 .finalizeForwardDeclaration();
610 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
611 setupSamplerType(Decl, *SemaPtr).completeDefinition();
612 });
613
614 Decl =
615 BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "SamplerComparisonState")
616 .finalizeForwardDeclaration();
617 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
618 setupSamplerType(Decl, *SemaPtr).completeDefinition();
619 });
620
621 QualType Float4Ty = AST.getExtVectorType(AST.FloatTy, 4);
622 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "Texture2D")
623 .addSimpleTemplateParams({"element_type"}, {Float4Ty},
624 TypedBufferConcept)
625 .finalizeForwardDeclaration();
626
627 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
628 setupTextureType(Decl, *SemaPtr, ResourceClass::SRV, /*IsROV=*/false,
629 ResourceDimension::Dim2D)
631 });
632
633 auto *PartialSpec = addVectorTexturePartialSpecialization(
634 *SemaPtr, HLSLNamespace, Decl->getDescribedClassTemplate());
635 onCompletion(PartialSpec, [this](CXXRecordDecl *Decl) {
636 setupTextureType(Decl, *SemaPtr, ResourceClass::SRV, /*IsROV=*/false,
637 ResourceDimension::Dim2D)
639 });
640}
641
642void HLSLExternalSemaSource::onCompletion(CXXRecordDecl *Record,
643 CompletionFunction Fn) {
644 if (!Record->isCompleteDefinition())
645 Completions.insert(std::make_pair(Record->getCanonicalDecl(), Fn));
646}
647
649 if (!isa<CXXRecordDecl>(Tag))
650 return;
651 auto Record = cast<CXXRecordDecl>(Tag);
652
653 // If this is a specialization, we need to get the underlying templated
654 // declaration and complete that.
655 if (auto TDecl = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
657 ClassTemplateDecl *Template = TDecl->getSpecializedTemplate();
659 Template->getPartialSpecializations(Partials);
660 ClassTemplatePartialSpecializationDecl *MatchedPartial = nullptr;
661 for (auto *Partial : Partials) {
662 sema::TemplateDeductionInfo Info(TDecl->getLocation());
663 if (SemaPtr->DeduceTemplateArguments(Partial, TDecl->getTemplateArgs(),
664 Info) ==
666 MatchedPartial = Partial;
667 break;
668 }
669 }
670 if (MatchedPartial)
671 Record = MatchedPartial;
672 else
673 Record = Template->getTemplatedDecl();
674 }
675 }
676 Record = Record->getCanonicalDecl();
677 auto It = Completions.find(Record);
678 if (It == Completions.end())
679 return;
680 It->second(Record);
681 Completions.erase(It);
682}
Defines the clang::ASTContext interface.
llvm::dxil::ResourceClass ResourceClass
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the C++ template declaration subclasses.
static BuiltinTypeDeclBuilder setupBufferType(CXXRecordDecl *Decl, Sema &S, ResourceClass RC, bool IsROV, bool RawBuffer, bool HasCounter)
Set up common members and attributes for buffer types.
static BuiltinTypeDeclBuilder setupSamplerType(CXXRecordDecl *Decl, Sema &S)
Set up common members and attributes for sampler types.
static ConceptDecl * constructBufferConceptDecl(Sema &S, NamespaceDecl *NSD, bool isTypedBuffer)
static Expr * constructTypedBufferConstraintExpr(Sema &S, SourceLocation NameLoc, TemplateTypeParmDecl *T)
static BuiltinTypeDeclBuilder setupTextureType(CXXRecordDecl *Decl, Sema &S, ResourceClass RC, bool IsROV, ResourceDimension Dim)
Set up common members and attributes for texture types.
static ClassTemplatePartialSpecializationDecl * addVectorTexturePartialSpecialization(Sema &S, NamespaceDecl *HLSLNamespace, ClassTemplateDecl *TextureTemplate)
static Expr * constructStructuredBufferConstraintExpr(Sema &S, SourceLocation NameLoc, TemplateTypeParmDecl *T)
llvm::MachO::Record Record
Definition MachO.h:31
This file declares semantic analysis for HLSL constructs.
Defines the clang::SourceLocation class and associated facilities.
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
TranslationUnitDecl * getTranslationUnitDecl() const
QualType getDependentSizedMatrixType(QualType ElementType, Expr *RowExpr, Expr *ColumnExpr, SourceLocation AttrLoc) const
Return the unique reference to the matrix type of the specified element type and size.
unsigned getIntWidth(QualType T) const
static CanQualType getCanonicalType(QualType T)
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
CanQualType FloatTy
IdentifierTable & Idents
Definition ASTContext.h:798
CanQualType BoolTy
QualType getTemplateTypeParmType(unsigned Depth, unsigned Index, bool ParameterPack, TemplateTypeParmDecl *ParmDecl=nullptr) const
Retrieve the template type parameter type for a template parameter or parameter pack with the given d...
TypeSourceInfo * getTrivialTypeSourceInfo(QualType T, SourceLocation Loc=SourceLocation()) const
Allocate a TypeSourceInfo where all locations have been initialized to a given location,...
CanQualType IntTy
QualType getTemplateSpecializationType(ElaboratedTypeKeyword Keyword, TemplateName T, ArrayRef< TemplateArgument > SpecifiedArgs, ArrayRef< TemplateArgument > CanonicalArgs, QualType Underlying=QualType()) const
QualType getExtVectorType(QualType VectorType, unsigned NumElts) const
Return the unique reference to an extended vector type of the specified element type and size.
QualType getDependentSizedExtVectorType(QualType VectorType, Expr *SizeExpr, SourceLocation AttrLoc) const
A builtin binary operation expression such as "x + y" or "x <= y".
Definition Expr.h:4041
static BinaryOperator * Create(const ASTContext &C, Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, ExprValueKind VK, ExprObjectKind OK, SourceLocation opLoc, FPOptionsOverride FPFeatures)
Definition Expr.cpp:5094
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
static CanQual< Type > CreateUnsafe(QualType Other)
Declaration of a class template.
void AddPartialSpecialization(ClassTemplatePartialSpecializationDecl *D, void *InsertPos)
Insert the specified partial specialization knowing that it is not already in.
static ClassTemplatePartialSpecializationDecl * Create(ASTContext &Context, TagKind TK, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, TemplateParameterList *Params, ClassTemplateDecl *SpecializedTemplate, ArrayRef< TemplateArgument > Args, CanQualType CanonInjectedTST, ClassTemplatePartialSpecializationDecl *PrevDecl)
Declaration of a C++20 concept.
static ConceptDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, Expr *ConstraintExpr=nullptr)
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1462
void addDecl(Decl *D)
Add the declaration D into this context.
bool hasExternalLexicalStorage() const
Whether this DeclContext has external storage containing additional declarations that are lexically i...
Definition DeclBase.h:2701
decl_iterator decls_begin() const
static DeclRefExpr * Create(const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc, ValueDecl *D, bool RefersToEnclosingVariableOrCapture, SourceLocation NameLoc, QualType T, ExprValueKind VK, NamedDecl *FoundD=nullptr, const TemplateArgumentListInfo *TemplateArgs=nullptr, NonOdrUseReason NOUR=NOUR_None)
Definition Expr.cpp:488
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
DeclContext * getDeclContext()
Definition DeclBase.h:456
The name of a declaration.
This represents one expression.
Definition Expr.h:112
Represents difference between two FPOptions values.
void CompleteType(TagDecl *Tag) override
Complete an incomplete HLSL builtin type.
void InitializeSema(Sema &S) override
Initialize the semantic source with the Sema instance being used to perform semantic analysis on the ...
One of these records is kept for each identifier that is lexed.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
static IntegerLiteral * Create(const ASTContext &C, const llvm::APInt &V, QualType type, SourceLocation l)
Returns a new integer literal with value 'V' and type 'type'.
Definition Expr.cpp:975
Represents the results of name lookup.
Definition Lookup.h:147
Represent a C++ namespace.
Definition Decl.h:592
static NamespaceDecl * Create(ASTContext &C, DeclContext *DC, bool Inline, SourceLocation StartLoc, SourceLocation IdLoc, IdentifierInfo *Id, NamespaceDecl *PrevDecl, bool Nested)
Definition DeclCXX.cpp:3366
A C++ nested-name-specifier augmented with source location information.
static NonTypeTemplateParmDecl * Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, unsigned D, unsigned P, const IdentifierInfo *Id, QualType T, bool ParameterPack, TypeSourceInfo *TInfo)
A (possibly-)qualified type.
Definition TypeBase.h:937
C++2a [expr.prim.req]: A requires-expression provides a concise way to express requirements on templa...
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:868
TemplateArgumentLoc getTrivialTemplateArgumentLoc(const TemplateArgument &Arg, QualType NTTPType, SourceLocation Loc, NamedDecl *TemplateParam=nullptr)
Allocate a TemplateArgumentLoc where all locations have been initialized to the given location.
@ LookupNamespaceName
Look up a namespace name within a C++ using directive or namespace alias definition,...
Definition Sema.h:9429
ASTContext & getASTContext() const
Definition Sema.h:939
const LangOptions & getLangOpts() const
Definition Sema.h:932
bool LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx, bool InUnqualifiedLookup=false)
Perform qualified name lookup into a given context.
Encodes a location in the source.
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3732
A convenient class for passing around template argument information.
void addArgument(const TemplateArgumentLoc &Loc)
Location wrapper for a TemplateArgument.
Represents a template argument.
void setTemplateParameters(TemplateParameterList *TParams)
Represents a C++ template name within the type system.
Stores a list of template parameters for a TemplateDecl and its derived classes.
static TemplateParameterList * Create(const ASTContext &C, SourceLocation TemplateLoc, SourceLocation LAngleLoc, ArrayRef< NamedDecl * > Params, SourceLocation RAngleLoc, Expr *RequiresClause)
Declaration of a template type parameter.
static TemplateTypeParmDecl * Create(const ASTContext &C, DeclContext *DC, SourceLocation KeyLoc, SourceLocation NameLoc, unsigned D, unsigned P, IdentifierInfo *Id, bool Typename, bool ParameterPack, bool HasTypeConstraint=false, UnsignedOrNone NumExpanded=std::nullopt)
static TypeAliasDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, const IdentifierInfo *Id, TypeSourceInfo *TInfo)
Definition Decl.cpp:5814
static TypeAliasTemplateDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, NamedDecl *Decl)
Create a function template node.
A container of type source information.
Definition TypeBase.h:8402
A type trait used in the implementation of various C++11 and Library TR1 trait templates.
Definition ExprCXX.h:2897
static TypeTraitExpr * Create(const ASTContext &C, QualType T, SourceLocation Loc, TypeTrait Kind, ArrayRef< TypeSourceInfo * > Args, SourceLocation RParenLoc, bool Value)
Create a new type trait expression.
Definition ExprCXX.cpp:1912
UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) expression operand.
Definition Expr.h:2628
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition Expr.h:2247
static UnaryOperator * Create(const ASTContext &C, Expr *input, Opcode opc, QualType type, ExprValueKind VK, ExprObjectKind OK, SourceLocation l, bool CanOverflow, FPOptionsOverride FPFeatures)
Definition Expr.cpp:5151
Represents a C++ using-declaration.
Definition DeclCXX.h:3607
static UsingDirectiveDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc, SourceLocation NamespaceLoc, NestedNameSpecifierLoc QualifierLoc, SourceLocation IdentLoc, NamedDecl *Nominated, DeclContext *CommonAncestor)
Definition DeclCXX.cpp:3322
Represents a GCC generic vector type.
Definition TypeBase.h:4225
BuiltinTypeDeclBuilder & addDefaultHandleConstructor(AccessSpecifier Access=AccessSpecifier::AS_public)
BuiltinTypeDeclBuilder & addCopyAssignmentOperator(AccessSpecifier Access=AccessSpecifier::AS_public)
BuiltinTypeDeclBuilder & addArraySubscriptOperators(ResourceDimension Dim=ResourceDimension::Unknown)
BuiltinTypeDeclBuilder & addSampleGradMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addGetDimensionsMethodForBuffer()
BuiltinTypeDeclBuilder & addGatherCmpMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addSampleBiasMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addBufferHandles(ResourceClass RC, bool IsROV, bool RawBuffer, bool HasCounter, AccessSpecifier Access=AccessSpecifier::AS_private)
BuiltinTypeDeclBuilder & addByteAddressBufferStoreMethods()
BuiltinTypeDeclBuilder & addTextureHandle(ResourceClass RC, bool IsROV, ResourceDimension RD, AccessSpecifier Access=AccessSpecifier::AS_private)
BuiltinTypeDeclBuilder & addCopyConstructor(AccessSpecifier Access=AccessSpecifier::AS_public)
BuiltinTypeDeclBuilder & addTextureLoadMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addSampleLevelMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addMipsMember(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addByteAddressBufferLoadMethods()
BuiltinTypeDeclBuilder & addSampleMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addStaticInitializationFunctions(bool HasCounter)
BuiltinTypeDeclBuilder & addCalculateLodMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addGetDimensionsMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addSampleCmpLevelZeroMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addGatherMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addSampleCmpMethods(ResourceDimension Dim)
Provides information about an attempted template argument deduction, whose success or failure was des...
std::variant< struct RequiresDecl, struct HeaderDecl, struct UmbrellaDirDecl, struct ModuleDecl, struct ExcludeDecl, struct ExportDecl, struct ExportAsDecl, struct ExternModuleDecl, struct UseDecl, struct LinkDecl, struct ConfigMacrosDecl, struct ConflictDecl > Decl
All declarations that can appear in a module declaration.
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ OK_Ordinary
An ordinary object is located at an address in memory.
Definition Specifiers.h:151
@ Result
The result type of a method or function.
Definition TypeBase.h:905
@ Template
We are parsing a template declaration.
Definition Parser.h:81
@ VK_PRValue
A pr-value expression (in the C++11 taxonomy) produces a temporary value.
Definition Specifiers.h:135
@ VK_LValue
An l-value expression is a reference to an object with independent storage.
Definition Specifiers.h:139
@ Success
Template argument deduction was successful.
Definition Sema.h:371
U cast(CodeGen::Address addr)
Definition Address.h:327
@ Class
The "class" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:5967
static const ASTTemplateArgumentListInfo * Create(const ASTContext &C, const TemplateArgumentListInfo &List)
DeclarationNameInfo - A collector data type for bundling together a DeclarationName and the correspon...