/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/HLSLExternalSemaSource.cpp
Line | Count | Source |
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 | | |
12 | | #include "clang/Sema/HLSLExternalSemaSource.h" |
13 | | #include "clang/AST/ASTContext.h" |
14 | | #include "clang/AST/DeclCXX.h" |
15 | | #include "clang/Basic/AttrKinds.h" |
16 | | #include "clang/Sema/Sema.h" |
17 | | |
18 | | using namespace clang; |
19 | | |
20 | 27 | HLSLExternalSemaSource::~HLSLExternalSemaSource() {} |
21 | | |
22 | 31 | void HLSLExternalSemaSource::InitializeSema(Sema &S) { |
23 | 31 | SemaPtr = &S; |
24 | 31 | ASTContext &AST = SemaPtr->getASTContext(); |
25 | 31 | IdentifierInfo &HLSL = AST.Idents.get("hlsl", tok::TokenKind::identifier); |
26 | 31 | HLSLNamespace = |
27 | 31 | NamespaceDecl::Create(AST, AST.getTranslationUnitDecl(), false, |
28 | 31 | SourceLocation(), SourceLocation(), &HLSL, nullptr); |
29 | 31 | HLSLNamespace->setImplicit(true); |
30 | 31 | AST.getTranslationUnitDecl()->addDecl(HLSLNamespace); |
31 | 31 | defineHLSLVectorAlias(); |
32 | | |
33 | | // This adds a `using namespace hlsl` directive. In DXC, we don't put HLSL's |
34 | | // built in types inside a namespace, but we are planning to change that in |
35 | | // the near future. In order to be source compatible older versions of HLSL |
36 | | // will need to implicitly use the hlsl namespace. For now in clang everything |
37 | | // will get added to the namespace, and we can remove the using directive for |
38 | | // future language versions to match HLSL's evolution. |
39 | 31 | auto *UsingDecl = UsingDirectiveDecl::Create( |
40 | 31 | AST, AST.getTranslationUnitDecl(), SourceLocation(), SourceLocation(), |
41 | 31 | NestedNameSpecifierLoc(), SourceLocation(), HLSLNamespace, |
42 | 31 | AST.getTranslationUnitDecl()); |
43 | | |
44 | 31 | AST.getTranslationUnitDecl()->addDecl(UsingDecl); |
45 | 31 | } |
46 | | |
47 | 31 | void HLSLExternalSemaSource::defineHLSLVectorAlias() { |
48 | 31 | ASTContext &AST = SemaPtr->getASTContext(); |
49 | | |
50 | 31 | llvm::SmallVector<NamedDecl *> TemplateParams; |
51 | | |
52 | 31 | auto *TypeParam = TemplateTypeParmDecl::Create( |
53 | 31 | AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 0, |
54 | 31 | &AST.Idents.get("element", tok::TokenKind::identifier), false, false); |
55 | 31 | TypeParam->setDefaultArgument(AST.getTrivialTypeSourceInfo(AST.FloatTy)); |
56 | | |
57 | 31 | TemplateParams.emplace_back(TypeParam); |
58 | | |
59 | 31 | auto *SizeParam = NonTypeTemplateParmDecl::Create( |
60 | 31 | AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 1, |
61 | 31 | &AST.Idents.get("element_count", tok::TokenKind::identifier), AST.IntTy, |
62 | 31 | false, AST.getTrivialTypeSourceInfo(AST.IntTy)); |
63 | 31 | Expr *LiteralExpr = |
64 | 31 | IntegerLiteral::Create(AST, llvm::APInt(AST.getIntWidth(AST.IntTy), 4), |
65 | 31 | AST.IntTy, SourceLocation()); |
66 | 31 | SizeParam->setDefaultArgument(LiteralExpr); |
67 | 31 | TemplateParams.emplace_back(SizeParam); |
68 | | |
69 | 31 | auto *ParamList = |
70 | 31 | TemplateParameterList::Create(AST, SourceLocation(), SourceLocation(), |
71 | 31 | TemplateParams, SourceLocation(), nullptr); |
72 | | |
73 | 31 | IdentifierInfo &II = AST.Idents.get("vector", tok::TokenKind::identifier); |
74 | | |
75 | 31 | QualType AliasType = AST.getDependentSizedExtVectorType( |
76 | 31 | AST.getTemplateTypeParmType(0, 0, false, TypeParam), |
77 | 31 | DeclRefExpr::Create( |
78 | 31 | AST, NestedNameSpecifierLoc(), SourceLocation(), SizeParam, false, |
79 | 31 | DeclarationNameInfo(SizeParam->getDeclName(), SourceLocation()), |
80 | 31 | AST.IntTy, VK_LValue), |
81 | 31 | SourceLocation()); |
82 | | |
83 | 31 | auto *Record = TypeAliasDecl::Create(AST, HLSLNamespace, SourceLocation(), |
84 | 31 | SourceLocation(), &II, |
85 | 31 | AST.getTrivialTypeSourceInfo(AliasType)); |
86 | 31 | Record->setImplicit(true); |
87 | | |
88 | 31 | auto *Template = |
89 | 31 | TypeAliasTemplateDecl::Create(AST, HLSLNamespace, SourceLocation(), |
90 | 31 | Record->getIdentifier(), ParamList, Record); |
91 | | |
92 | 31 | Record->setDescribedAliasTemplate(Template); |
93 | 31 | Template->setImplicit(true); |
94 | 31 | Template->setLexicalDeclContext(Record->getDeclContext()); |
95 | 31 | HLSLNamespace->addDecl(Template); |
96 | 31 | } |