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
275
276// Add a partial specialization for a template. The `TextureTemplate` is
277// `Texture<element_type>`, and it will be specialized for vectors:
278// `Texture<vector<element_type, element_count>>`.
281 ClassTemplateDecl *TextureTemplate) {
282 ASTContext &AST = S.getASTContext();
283
284 // Create the template parameters: element_type and element_count.
285 auto *ElementType = TemplateTypeParmDecl::Create(
286 AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 0,
287 &AST.Idents.get("element_type"), false, false);
288 auto *ElementCount = NonTypeTemplateParmDecl::Create(
289 AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 1,
290 &AST.Idents.get("element_count"), AST.IntTy, false,
292
293 auto *TemplateParams = TemplateParameterList::Create(
294 AST, SourceLocation(), SourceLocation(), {ElementType, ElementCount},
295 SourceLocation(), nullptr);
296
297 // Create the dependent vector type: vector<element_type, element_count>.
299 AST.getTemplateTypeParmType(0, 0, false, ElementType),
301 AST, NestedNameSpecifierLoc(), SourceLocation(), ElementCount, false,
302 DeclarationNameInfo(ElementCount->getDeclName(), SourceLocation()),
303 AST.IntTy, VK_LValue),
305
306 // Create the partial specialization declaration.
307 QualType CanonInjectedTST =
311
313 AST, TagDecl::TagKind::Class, HLSLNamespace, SourceLocation(),
314 SourceLocation(), TemplateParams, TextureTemplate,
316 CanQualType::CreateUnsafe(CanonInjectedTST), nullptr);
317
318 // Set the template arguments as written.
320 TemplateArgumentLoc ArgLoc =
322 TemplateArgumentListInfo ArgsInfo =
324 ArgsInfo.addArgument(ArgLoc);
325 PartialSpec->setTemplateArgsAsWritten(
327
328 PartialSpec->setImplicit(true);
329 PartialSpec->setLexicalDeclContext(HLSLNamespace);
330 PartialSpec->setHasExternalLexicalStorage();
331
332 // Add the partial specialization to the namespace and the class template.
333 HLSLNamespace->addDecl(PartialSpec);
334 TextureTemplate->AddPartialSpecialization(PartialSpec, nullptr);
335
336 return PartialSpec;
337}
338
339// This function is responsible for constructing the constraint expression for
340// this concept:
341// template<typename T> concept is_typed_resource_element_compatible =
342// __is_typed_resource_element_compatible<T>;
345 ASTContext &Context = S.getASTContext();
346
347 // Obtain the QualType for 'bool'
348 QualType BoolTy = Context.BoolTy;
349
350 // Create a QualType that points to this TemplateTypeParmDecl
351 QualType TType = Context.getTypeDeclType(T);
352
353 // Create a TypeSourceInfo for the template type parameter 'T'
354 TypeSourceInfo *TTypeSourceInfo =
355 Context.getTrivialTypeSourceInfo(TType, NameLoc);
356
357 TypeTraitExpr *TypedResExpr = TypeTraitExpr::Create(
358 Context, BoolTy, NameLoc, UTT_IsTypedResourceElementCompatible,
359 {TTypeSourceInfo}, NameLoc, true);
360
361 return TypedResExpr;
362}
363
364// This function is responsible for constructing the constraint expression for
365// this concept:
366// template<typename T> concept is_structured_resource_element_compatible =
367// !__is_intangible<T> && sizeof(T) >= 1;
369 SourceLocation NameLoc,
371 ASTContext &Context = S.getASTContext();
372
373 // Obtain the QualType for 'bool'
374 QualType BoolTy = Context.BoolTy;
375
376 // Create a QualType that points to this TemplateTypeParmDecl
377 QualType TType = Context.getTypeDeclType(T);
378
379 // Create a TypeSourceInfo for the template type parameter 'T'
380 TypeSourceInfo *TTypeSourceInfo =
381 Context.getTrivialTypeSourceInfo(TType, NameLoc);
382
383 TypeTraitExpr *IsIntangibleExpr =
384 TypeTraitExpr::Create(Context, BoolTy, NameLoc, UTT_IsIntangibleType,
385 {TTypeSourceInfo}, NameLoc, true);
386
387 // negate IsIntangibleExpr
388 UnaryOperator *NotIntangibleExpr = UnaryOperator::Create(
389 Context, IsIntangibleExpr, UO_LNot, BoolTy, VK_LValue, OK_Ordinary,
390 NameLoc, false, FPOptionsOverride());
391
392 // element types also may not be of 0 size
393 UnaryExprOrTypeTraitExpr *SizeOfExpr = new (Context) UnaryExprOrTypeTraitExpr(
394 UETT_SizeOf, TTypeSourceInfo, BoolTy, NameLoc, NameLoc);
395
396 // Create a BinaryOperator that checks if the size of the type is not equal to
397 // 1 Empty structs have a size of 1 in HLSL, so we need to check for that
399 Context, llvm::APInt(Context.getTypeSize(Context.getSizeType()), 1, true),
400 Context.getSizeType(), NameLoc);
401
402 BinaryOperator *SizeGEQOneExpr =
403 BinaryOperator::Create(Context, SizeOfExpr, rhs, BO_GE, BoolTy, VK_LValue,
404 OK_Ordinary, NameLoc, FPOptionsOverride());
405
406 // Combine the two constraints
408 Context, NotIntangibleExpr, SizeGEQOneExpr, BO_LAnd, BoolTy, VK_LValue,
409 OK_Ordinary, NameLoc, FPOptionsOverride());
410
411 return CombinedExpr;
412}
413
415 bool isTypedBuffer) {
416 ASTContext &Context = S.getASTContext();
417 DeclContext *DC = NSD->getDeclContext();
418 SourceLocation DeclLoc = SourceLocation();
419
420 IdentifierInfo &ElementTypeII = Context.Idents.get("element_type");
422 Context, NSD->getDeclContext(), DeclLoc, DeclLoc,
423 /*D=*/0,
424 /*P=*/0,
425 /*Id=*/&ElementTypeII,
426 /*Typename=*/true,
427 /*ParameterPack=*/false);
428
429 T->setDeclContext(DC);
430 T->setReferenced();
431
432 // Create and Attach Template Parameter List to ConceptDecl
434 Context, DeclLoc, DeclLoc, {T}, DeclLoc, nullptr);
435
436 DeclarationName DeclName;
437 Expr *ConstraintExpr = nullptr;
438
439 if (isTypedBuffer) {
440 DeclName = DeclarationName(
441 &Context.Idents.get("__is_typed_resource_element_compatible"));
442 ConstraintExpr = constructTypedBufferConstraintExpr(S, DeclLoc, T);
443 } else {
444 DeclName = DeclarationName(
445 &Context.Idents.get("__is_structured_resource_element_compatible"));
446 ConstraintExpr = constructStructuredBufferConstraintExpr(S, DeclLoc, T);
447 }
448
449 // Create a ConceptDecl
450 ConceptDecl *CD =
451 ConceptDecl::Create(Context, NSD->getDeclContext(), DeclLoc, DeclName,
452 ConceptParams, ConstraintExpr);
453
454 // Attach the template parameter list to the ConceptDecl
455 CD->setTemplateParameters(ConceptParams);
456
457 // Add the concept declaration to the Translation Unit Decl
458 NSD->getDeclContext()->addDecl(CD);
459
460 return CD;
461}
462
463void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
464 ASTContext &AST = SemaPtr->getASTContext();
465 CXXRecordDecl *Decl;
466 ConceptDecl *TypedBufferConcept = constructBufferConceptDecl(
467 *SemaPtr, HLSLNamespace, /*isTypedBuffer*/ true);
468 ConceptDecl *StructuredBufferConcept = constructBufferConceptDecl(
469 *SemaPtr, HLSLNamespace, /*isTypedBuffer*/ false);
470
471 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "Buffer")
472 .addSimpleTemplateParams({"element_type"}, TypedBufferConcept)
473 .finalizeForwardDeclaration();
474
475 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
476 setupBufferType(Decl, *SemaPtr, ResourceClass::SRV, /*IsROV=*/false,
477 /*RawBuffer=*/false, /*HasCounter=*/false)
482 });
483
484 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWBuffer")
485 .addSimpleTemplateParams({"element_type"}, TypedBufferConcept)
486 .finalizeForwardDeclaration();
487
488 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
489 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/false,
490 /*RawBuffer=*/false, /*HasCounter=*/false)
495 });
496
497 Decl =
498 BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RasterizerOrderedBuffer")
499 .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
500 .finalizeForwardDeclaration();
501 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
502 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/true,
503 /*RawBuffer=*/false, /*HasCounter=*/false)
508 });
509
510 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "StructuredBuffer")
511 .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
512 .finalizeForwardDeclaration();
513 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
514 setupBufferType(Decl, *SemaPtr, ResourceClass::SRV, /*IsROV=*/false,
515 /*RawBuffer=*/true, /*HasCounter=*/false)
520 });
521
522 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWStructuredBuffer")
523 .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
524 .finalizeForwardDeclaration();
525 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
526 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/false,
527 /*RawBuffer=*/true, /*HasCounter=*/true)
534 });
535
536 Decl =
537 BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "AppendStructuredBuffer")
538 .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
539 .finalizeForwardDeclaration();
540 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
541 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/false,
542 /*RawBuffer=*/true, /*HasCounter=*/true)
546 });
547
548 Decl =
549 BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "ConsumeStructuredBuffer")
550 .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
551 .finalizeForwardDeclaration();
552 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
553 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/false,
554 /*RawBuffer=*/true, /*HasCounter=*/true)
558 });
559
560 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace,
561 "RasterizerOrderedStructuredBuffer")
562 .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
563 .finalizeForwardDeclaration();
564 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
565 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/true,
566 /*RawBuffer=*/true, /*HasCounter=*/true)
573 });
574
575 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "ByteAddressBuffer")
576 .finalizeForwardDeclaration();
577 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
578 setupBufferType(Decl, *SemaPtr, ResourceClass::SRV, /*IsROV=*/false,
579 /*RawBuffer=*/true, /*HasCounter=*/false)
583 });
584 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWByteAddressBuffer")
585 .finalizeForwardDeclaration();
586 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
587 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/false,
588 /*RawBuffer=*/true, /*HasCounter=*/false)
593 });
594 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace,
595 "RasterizerOrderedByteAddressBuffer")
596 .finalizeForwardDeclaration();
597 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
598 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/true,
599 /*RawBuffer=*/true, /*HasCounter=*/false)
602 });
603
604 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "SamplerState")
605 .finalizeForwardDeclaration();
606 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
607 setupSamplerType(Decl, *SemaPtr).completeDefinition();
608 });
609
610 Decl =
611 BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "SamplerComparisonState")
612 .finalizeForwardDeclaration();
613 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
614 setupSamplerType(Decl, *SemaPtr).completeDefinition();
615 });
616
617 QualType Float4Ty = AST.getExtVectorType(AST.FloatTy, 4);
618 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "Texture2D")
619 .addSimpleTemplateParams({"element_type"}, {Float4Ty},
620 TypedBufferConcept)
621 .finalizeForwardDeclaration();
622
623 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
624 setupTextureType(Decl, *SemaPtr, ResourceClass::SRV, /*IsROV=*/false,
625 ResourceDimension::Dim2D)
627 });
628
629 auto *PartialSpec = addVectorTexturePartialSpecialization(
630 *SemaPtr, HLSLNamespace, Decl->getDescribedClassTemplate());
631 onCompletion(PartialSpec, [this](CXXRecordDecl *Decl) {
632 setupTextureType(Decl, *SemaPtr, ResourceClass::SRV, /*IsROV=*/false,
633 ResourceDimension::Dim2D)
635 });
636}
637
638void HLSLExternalSemaSource::onCompletion(CXXRecordDecl *Record,
639 CompletionFunction Fn) {
640 if (!Record->isCompleteDefinition())
641 Completions.insert(std::make_pair(Record->getCanonicalDecl(), Fn));
642}
643
645 if (!isa<CXXRecordDecl>(Tag))
646 return;
647 auto Record = cast<CXXRecordDecl>(Tag);
648
649 // If this is a specialization, we need to get the underlying templated
650 // declaration and complete that.
651 if (auto TDecl = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
653 ClassTemplateDecl *Template = TDecl->getSpecializedTemplate();
655 Template->getPartialSpecializations(Partials);
656 ClassTemplatePartialSpecializationDecl *MatchedPartial = nullptr;
657 for (auto *Partial : Partials) {
658 sema::TemplateDeductionInfo Info(TDecl->getLocation());
659 if (SemaPtr->DeduceTemplateArguments(Partial, TDecl->getTemplateArgs(),
660 Info) ==
662 MatchedPartial = Partial;
663 break;
664 }
665 }
666 if (MatchedPartial)
667 Record = MatchedPartial;
668 else
669 Record = Template->getTemplatedDecl();
670 }
671 }
672 Record = Record->getCanonicalDecl();
673 auto It = Completions.find(Record);
674 if (It == Completions.end())
675 return;
676 It->second(Record);
677 Completions.erase(It);
678}
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:5076
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:1449
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:2688
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:448
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:3332
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:9408
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:3717
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:5813
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:1914
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:5133
Represents a C++ using-declaration.
Definition DeclCXX.h:3594
static UsingDirectiveDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc, SourceLocation NamespaceLoc, NestedNameSpecifierLoc QualifierLoc, SourceLocation IdentLoc, NamedDecl *Nominated, DeclContext *CommonAncestor)
Definition DeclCXX.cpp:3288
Represents a GCC generic vector type.
Definition TypeBase.h:4225
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 & addTextureLoadMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addSampleLevelMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addByteAddressBufferLoadMethods()
BuiltinTypeDeclBuilder & addSampleMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addStaticInitializationFunctions(bool HasCounter)
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...