ParseHLSL.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //===--- ParseHLSL.cpp - HLSL Parsing -------------------------------------===//
  2. ///////////////////////////////////////////////////////////////////////////////
  3. // //
  4. // ParseHLSL.cpp //
  5. // Copyright (C) Microsoft Corporation. All rights reserved. //
  6. // This file is distributed under the University of Illinois Open Source //
  7. // License. See LICENSE.TXT for details. //
  8. // //
  9. // This file implements the HLSLportions of the Parser interfaces. //
  10. // //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "clang/Parse/Parser.h"
  13. #include "RAIIObjectsForParser.h"
  14. #include "clang/Basic/CharInfo.h"
  15. #include "clang/Basic/OperatorKinds.h"
  16. #include "clang/AST/ASTContext.h"
  17. #include "clang/AST/DeclTemplate.h"
  18. #include "clang/Parse/ParseDiagnostic.h"
  19. #include "clang/Sema/DeclSpec.h"
  20. #include "clang/Sema/ParsedTemplate.h"
  21. #include "clang/Sema/PrettyDeclStackTrace.h"
  22. #include "clang/Sema/Scope.h"
  23. #include "clang/Sema/SemaDiagnostic.h"
  24. #include "llvm/ADT/SmallString.h"
  25. using namespace clang;
  26. Decl *Parser::ParseCTBuffer(unsigned Context, SourceLocation &DeclEnd,
  27. ParsedAttributesWithRange &CTBAttrs,
  28. SourceLocation InlineLoc) {
  29. assert((Tok.is(tok::kw_cbuffer) || Tok.is(tok::kw_tbuffer)) &&
  30. "Not a cbuffer or tbuffer!");
  31. bool isCBuffer = Tok.is(tok::kw_cbuffer);
  32. SourceLocation BufferLoc = ConsumeToken(); // eat the 'cbuffer or tbuffer'.
  33. if (!Tok.is(tok::identifier)) {
  34. Diag(Tok, diag::err_expected) << tok::identifier;
  35. return nullptr;
  36. }
  37. IdentifierInfo *identifier = Tok.getIdentifierInfo();
  38. SourceLocation identifierLoc = ConsumeToken(); // consume identifier
  39. std::vector<hlsl::UnusualAnnotation *> hlslAttrs;
  40. MaybeParseHLSLAttributes(hlslAttrs);
  41. ParseScope BufferScope(this, Scope::DeclScope);
  42. BalancedDelimiterTracker T(*this, tok::l_brace);
  43. if (T.consumeOpen()) {
  44. Diag(Tok, diag::err_expected) << tok::l_brace;
  45. return nullptr;
  46. }
  47. Decl *decl = Actions.ActOnStartHLSLBuffer(getCurScope(), isCBuffer, BufferLoc,
  48. identifier, identifierLoc,
  49. hlslAttrs, T.getOpenLocation());
  50. // Process potential C++11 attribute specifiers
  51. Actions.ProcessDeclAttributeList(getCurScope(), decl, CTBAttrs.getList());
  52. while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
  53. ParsedAttributesWithRange attrs(AttrFactory);
  54. MaybeParseCXX11Attributes(attrs);
  55. MaybeParseHLSLAttributes(attrs);
  56. MaybeParseMicrosoftAttributes(attrs);
  57. ParseExternalDeclaration(attrs);
  58. }
  59. T.consumeClose();
  60. DeclEnd = T.getCloseLocation();
  61. BufferScope.Exit();
  62. Actions.ActOnFinishHLSLBuffer(decl, DeclEnd);
  63. return decl;
  64. }
  65. Decl *Parser::ParseConstBuffer(unsigned Context, SourceLocation &DeclEnd,
  66. ParsedAttributesWithRange &attrs,
  67. SourceLocation InlineLoc) {
  68. bool isCBuffer = Tok.is(tok::kw_ConstantBuffer);
  69. assert((isCBuffer || Tok.is(tok::kw_TextureBuffer)) && "Not a ConstantBuffer or TextureBuffer!");
  70. SourceLocation BufferLoc = ConsumeToken(); // eat the 'ConstantBuffer'.
  71. if (!Tok.is(tok::less)) {
  72. Diag(Tok, diag::err_expected) << tok::less;
  73. return nullptr;
  74. }
  75. ConsumeToken(); // eat the <
  76. ParsingDeclSpec PDS(*this);
  77. ParseDeclarationSpecifiers(PDS);
  78. if (!Tok.is(tok::greater)) {
  79. Diag(Tok, diag::err_expected) << tok::greater;
  80. return nullptr;
  81. }
  82. ConsumeToken(); // eat the >
  83. PDS.takeAttributesFrom(attrs);
  84. Actions.ActOnStartHLSLBufferView();
  85. Parser::DeclGroupPtrTy dcl = ParseDeclGroup(PDS, Declarator::FileContext);
  86. // Check if the register type is valid
  87. NamedDecl *namedDecl = cast<NamedDecl>(dcl.get().getSingleDecl());
  88. ArrayRef<hlsl::UnusualAnnotation*> annotations = namedDecl->getUnusualAnnotations();
  89. for (hlsl::UnusualAnnotation* annotation : annotations) {
  90. if (annotation->getKind() == hlsl::UnusualAnnotation::UnusualAnnotationKind::UA_RegisterAssignment) {
  91. hlsl::RegisterAssignment *regAssignment = (hlsl::RegisterAssignment *)(annotation);
  92. if (isCBuffer && regAssignment->RegisterType != 'b' && regAssignment->RegisterType != 'B') {
  93. Diag(namedDecl->getLocation(), diag::err_hlsl_unsupported_cbuffer_register);
  94. }
  95. else if (!isCBuffer && regAssignment->RegisterType != 't' && regAssignment->RegisterType != 'T') {
  96. Diag(namedDecl->getLocation(), diag::err_hlsl_unsupported_tbuffer_register);
  97. }
  98. }
  99. }
  100. Decl *decl = Actions.ActOnHLSLBufferView(getCurScope(), BufferLoc, dcl, isCBuffer);
  101. return decl;
  102. }
  103. /// ParseHLSLAttributeSpecifier - Parse an HLSL attribute-specifier.
  104. ///
  105. /// [HLSL] attribute-specifier:
  106. /// '[' attribute[opt] ']'
  107. ///
  108. /// [HLSL] attribute:
  109. /// attribute-token attribute-argument-clause[opt]
  110. ///
  111. /// [HLSL] attribute-token:
  112. /// identifier
  113. ///
  114. /// [HLSL] attribute-argument-clause:
  115. /// '(' attribute-params ')'
  116. ///
  117. /// [HLSL] attribute-params:
  118. /// constant-expr
  119. /// attribute-params ',' constant-expr
  120. ///
  121. void Parser::ParseHLSLAttributeSpecifier(ParsedAttributes &attrs,
  122. SourceLocation *endLoc) {
  123. assert(getLangOpts().HLSL);
  124. assert(Tok.is(tok::l_square) && "Not an HLSL attribute list");
  125. ConsumeBracket();
  126. llvm::SmallDenseMap<IdentifierInfo *, SourceLocation, 4> SeenAttrs;
  127. // '[]' is valid.
  128. if (Tok.is(tok::r_square)) {
  129. *endLoc = ConsumeBracket();
  130. return;
  131. }
  132. if (!Tok.isAnyIdentifier()) {
  133. Diag(Tok, diag::err_expected) << tok::identifier;
  134. SkipUntil(tok::r_square);
  135. return;
  136. }
  137. SourceLocation AttrLoc;
  138. IdentifierInfo *AttrName = 0;
  139. AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
  140. assert(AttrName != nullptr && "already called isAnyIdenfier before");
  141. // Parse attribute arguments
  142. if (Tok.is(tok::l_paren)) {
  143. if (AttrName == &this->Actions.getASTContext().Idents.get("clipplanes")) {
  144. ArgsVector ArgExprs;
  145. ConsumeParen();
  146. for (;;) {
  147. ExprResult ArgExpr(ParseAssignmentExpression());
  148. if (ArgExpr.isInvalid()) {
  149. SkipUntil(tok::r_paren, StopAtSemi);
  150. return;
  151. }
  152. ArgExprs.push_back(ArgExpr.get());
  153. // Consume a comma and process the next argument.
  154. if (!Tok.is(tok::comma)) {
  155. break;
  156. }
  157. ConsumeToken();
  158. }
  159. SourceLocation EndLoc = Tok.getLocation();
  160. if (ExpectAndConsume(tok::r_paren, diag::err_expected))
  161. SkipUntil(tok::r_paren);
  162. attrs.addNew(AttrName, SourceRange(AttrLoc, EndLoc), nullptr,
  163. SourceLocation(), ArgExprs.data(), ArgExprs.size(),
  164. AttributeList::AS_CXX11);
  165. } else {
  166. ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc, nullptr,
  167. SourceLocation(), AttributeList::AS_CXX11, nullptr);
  168. }
  169. } else {
  170. attrs.addNew(AttrName, AttrLoc, nullptr, SourceLocation(), 0, 0,
  171. AttributeList::AS_CXX11);
  172. }
  173. if (endLoc)
  174. *endLoc = Tok.getLocation();
  175. if (ExpectAndConsume(tok::r_square, diag::err_expected))
  176. SkipUntil(tok::r_square);
  177. }
  178. /// ParseHLSLAttributes - Parse an HLSL attribute-specifier-seq.
  179. ///
  180. /// attribute-specifier-seq:
  181. /// attribute-specifier-seq[opt] attribute-specifier
  182. void Parser::ParseHLSLAttributes(ParsedAttributesWithRange &attrs,
  183. SourceLocation *endLoc) {
  184. assert(getLangOpts().HLSL);
  185. SourceLocation StartLoc = Tok.getLocation(), Loc;
  186. if (!endLoc)
  187. endLoc = &Loc;
  188. do {
  189. ParseHLSLAttributeSpecifier(attrs, endLoc);
  190. } while (Tok.is(tok::l_square));
  191. attrs.Range = SourceRange(StartLoc, *endLoc);
  192. }