ParseHLSL.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //===--- ParseHLSL.cpp - HLSL-specific parsing support --------------------===//
  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. // This file implements the parsing logic for HLSL language features.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/AST/Attr.h"
  13. #include "clang/Basic/AttributeCommonInfo.h"
  14. #include "clang/Parse/ParseDiagnostic.h"
  15. #include "clang/Parse/Parser.h"
  16. #include "clang/Parse/RAIIObjectsForParser.h"
  17. using namespace clang;
  18. static bool validateDeclsInsideHLSLBuffer(Parser::DeclGroupPtrTy DG,
  19. SourceLocation BufferLoc,
  20. bool IsCBuffer, Parser &P) {
  21. // The parse is failed, just return false.
  22. if (!DG)
  23. return false;
  24. DeclGroupRef Decls = DG.get();
  25. bool IsValid = true;
  26. // Only allow function, variable, record decls inside HLSLBuffer.
  27. for (DeclGroupRef::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
  28. Decl *D = *I;
  29. if (isa<CXXRecordDecl, RecordDecl, FunctionDecl, VarDecl>(D))
  30. continue;
  31. // FIXME: support nested HLSLBuffer and namespace inside HLSLBuffer.
  32. if (isa<HLSLBufferDecl, NamespaceDecl>(D)) {
  33. P.Diag(D->getLocation(), diag::err_invalid_declaration_in_hlsl_buffer)
  34. << IsCBuffer;
  35. IsValid = false;
  36. continue;
  37. }
  38. IsValid = false;
  39. P.Diag(D->getLocation(), diag::err_invalid_declaration_in_hlsl_buffer)
  40. << IsCBuffer;
  41. }
  42. return IsValid;
  43. }
  44. Decl *Parser::ParseHLSLBuffer(SourceLocation &DeclEnd) {
  45. assert((Tok.is(tok::kw_cbuffer) || Tok.is(tok::kw_tbuffer)) &&
  46. "Not a cbuffer or tbuffer!");
  47. bool IsCBuffer = Tok.is(tok::kw_cbuffer);
  48. SourceLocation BufferLoc = ConsumeToken(); // Eat the 'cbuffer' or 'tbuffer'.
  49. if (!Tok.is(tok::identifier)) {
  50. Diag(Tok, diag::err_expected) << tok::identifier;
  51. return nullptr;
  52. }
  53. IdentifierInfo *Identifier = Tok.getIdentifierInfo();
  54. SourceLocation IdentifierLoc = ConsumeToken();
  55. ParsedAttributes Attrs(AttrFactory);
  56. MaybeParseHLSLSemantics(Attrs, nullptr);
  57. ParseScope BufferScope(this, Scope::DeclScope);
  58. BalancedDelimiterTracker T(*this, tok::l_brace);
  59. if (T.consumeOpen()) {
  60. Diag(Tok, diag::err_expected) << tok::l_brace;
  61. return nullptr;
  62. }
  63. Decl *D = Actions.ActOnStartHLSLBuffer(getCurScope(), IsCBuffer, BufferLoc,
  64. Identifier, IdentifierLoc,
  65. T.getOpenLocation());
  66. while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
  67. // FIXME: support attribute on constants inside cbuffer/tbuffer.
  68. ParsedAttributes DeclAttrs(AttrFactory);
  69. ParsedAttributes EmptyDeclSpecAttrs(AttrFactory);
  70. DeclGroupPtrTy Result =
  71. ParseExternalDeclaration(DeclAttrs, EmptyDeclSpecAttrs);
  72. if (!validateDeclsInsideHLSLBuffer(Result, IdentifierLoc, IsCBuffer,
  73. *this)) {
  74. T.skipToEnd();
  75. DeclEnd = T.getCloseLocation();
  76. BufferScope.Exit();
  77. Actions.ActOnFinishHLSLBuffer(D, DeclEnd);
  78. return nullptr;
  79. }
  80. }
  81. T.consumeClose();
  82. DeclEnd = T.getCloseLocation();
  83. BufferScope.Exit();
  84. Actions.ActOnFinishHLSLBuffer(D, DeclEnd);
  85. Actions.ProcessDeclAttributeList(Actions.CurScope, D, Attrs);
  86. return D;
  87. }
  88. static void fixSeparateAttrArgAndNumber(StringRef ArgStr, SourceLocation ArgLoc,
  89. Token Tok, ArgsVector &ArgExprs,
  90. Parser &P, ASTContext &Ctx,
  91. Preprocessor &PP) {
  92. StringRef Num = StringRef(Tok.getLiteralData(), Tok.getLength());
  93. SourceLocation EndNumLoc = Tok.getEndLoc();
  94. P.ConsumeToken(); // consume constant.
  95. std::string FixedArg = ArgStr.str() + Num.str();
  96. P.Diag(ArgLoc, diag::err_hlsl_separate_attr_arg_and_number)
  97. << FixedArg
  98. << FixItHint::CreateReplacement(SourceRange(ArgLoc, EndNumLoc), FixedArg);
  99. ArgsUnion &Slot = ArgExprs.back();
  100. Slot = IdentifierLoc::create(Ctx, ArgLoc, PP.getIdentifierInfo(FixedArg));
  101. }
  102. void Parser::ParseHLSLSemantics(ParsedAttributes &Attrs,
  103. SourceLocation *EndLoc) {
  104. // FIXME: HLSLSemantic is shared for Semantic and resource binding which is
  105. // confusing. Need a better name to avoid misunderstanding. Issue
  106. // https://github.com/llvm/llvm-project/issues/57882
  107. assert(Tok.is(tok::colon) && "Not a HLSL Semantic");
  108. ConsumeToken();
  109. IdentifierInfo *II = nullptr;
  110. if (Tok.is(tok::kw_register))
  111. II = PP.getIdentifierInfo("register");
  112. else if (Tok.is(tok::identifier))
  113. II = Tok.getIdentifierInfo();
  114. if (!II) {
  115. Diag(Tok.getLocation(), diag::err_expected_semantic_identifier);
  116. return;
  117. }
  118. SourceLocation Loc = ConsumeToken();
  119. if (EndLoc)
  120. *EndLoc = Tok.getLocation();
  121. ParsedAttr::Kind AttrKind =
  122. ParsedAttr::getParsedKind(II, nullptr, ParsedAttr::AS_HLSLSemantic);
  123. ArgsVector ArgExprs;
  124. switch (AttrKind) {
  125. case ParsedAttr::AT_HLSLResourceBinding: {
  126. if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after)) {
  127. SkipUntil(tok::r_paren, StopAtSemi); // skip through )
  128. return;
  129. }
  130. if (!Tok.is(tok::identifier)) {
  131. Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
  132. SkipUntil(tok::r_paren, StopAtSemi); // skip through )
  133. return;
  134. }
  135. StringRef SlotStr = Tok.getIdentifierInfo()->getName();
  136. SourceLocation SlotLoc = Tok.getLocation();
  137. ArgExprs.push_back(ParseIdentifierLoc());
  138. // Add numeric_constant for fix-it.
  139. if (SlotStr.size() == 1 && Tok.is(tok::numeric_constant))
  140. fixSeparateAttrArgAndNumber(SlotStr, SlotLoc, Tok, ArgExprs, *this,
  141. Actions.Context, PP);
  142. if (Tok.is(tok::comma)) {
  143. ConsumeToken(); // consume comma
  144. if (!Tok.is(tok::identifier)) {
  145. Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
  146. SkipUntil(tok::r_paren, StopAtSemi); // skip through )
  147. return;
  148. }
  149. StringRef SpaceStr = Tok.getIdentifierInfo()->getName();
  150. SourceLocation SpaceLoc = Tok.getLocation();
  151. ArgExprs.push_back(ParseIdentifierLoc());
  152. // Add numeric_constant for fix-it.
  153. if (SpaceStr.equals("space") && Tok.is(tok::numeric_constant))
  154. fixSeparateAttrArgAndNumber(SpaceStr, SpaceLoc, Tok, ArgExprs, *this,
  155. Actions.Context, PP);
  156. }
  157. if (ExpectAndConsume(tok::r_paren, diag::err_expected)) {
  158. SkipUntil(tok::r_paren, StopAtSemi); // skip through )
  159. return;
  160. }
  161. } break;
  162. case ParsedAttr::UnknownAttribute:
  163. Diag(Loc, diag::err_unknown_hlsl_semantic) << II;
  164. return;
  165. case ParsedAttr::AT_HLSLSV_GroupIndex:
  166. case ParsedAttr::AT_HLSLSV_DispatchThreadID:
  167. break;
  168. default:
  169. llvm_unreachable("invalid HLSL Semantic");
  170. break;
  171. }
  172. Attrs.addNew(II, Loc, nullptr, SourceLocation(), ArgExprs.data(),
  173. ArgExprs.size(), ParsedAttr::AS_HLSLSemantic);
  174. }