ParseHLSL.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. /// ParseHLSLAttributeSpecifier - Parse an HLSL attribute-specifier.
  66. ///
  67. /// [HLSL] attribute-specifier:
  68. /// '[' attribute[opt] ']'
  69. ///
  70. /// [HLSL] attribute:
  71. /// attribute-token attribute-argument-clause[opt]
  72. ///
  73. /// [HLSL] attribute-token:
  74. /// identifier
  75. ///
  76. /// [HLSL] attribute-argument-clause:
  77. /// '(' attribute-params ')'
  78. ///
  79. /// [HLSL] attribute-params:
  80. /// constant-expr
  81. /// attribute-params ',' constant-expr
  82. ///
  83. void Parser::ParseHLSLAttributeSpecifier(ParsedAttributes &attrs,
  84. SourceLocation *endLoc) {
  85. assert(getLangOpts().HLSL);
  86. assert(Tok.is(tok::l_square) && "Not an HLSL attribute list");
  87. ConsumeBracket();
  88. llvm::SmallDenseMap<IdentifierInfo *, SourceLocation, 4> SeenAttrs;
  89. // '[]' is valid.
  90. if (Tok.is(tok::r_square)) {
  91. *endLoc = ConsumeBracket();
  92. return;
  93. }
  94. if (!Tok.isAnyIdentifier()) {
  95. Diag(Tok, diag::err_expected) << tok::identifier;
  96. SkipUntil(tok::r_square);
  97. return;
  98. }
  99. SourceLocation AttrLoc;
  100. IdentifierInfo *AttrName = 0;
  101. AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
  102. assert(AttrName != nullptr && "already called isAnyIdenfier before");
  103. // Parse attribute arguments
  104. if (Tok.is(tok::l_paren)) {
  105. ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc, nullptr,
  106. SourceLocation(), AttributeList::AS_CXX11, nullptr);
  107. } else {
  108. attrs.addNew(AttrName, AttrLoc, nullptr, SourceLocation(), 0, 0,
  109. AttributeList::AS_CXX11);
  110. }
  111. if (endLoc)
  112. *endLoc = Tok.getLocation();
  113. if (ExpectAndConsume(tok::r_square, diag::err_expected))
  114. SkipUntil(tok::r_square);
  115. }
  116. /// ParseHLSLAttributes - Parse an HLSL attribute-specifier-seq.
  117. ///
  118. /// attribute-specifier-seq:
  119. /// attribute-specifier-seq[opt] attribute-specifier
  120. void Parser::ParseHLSLAttributes(ParsedAttributesWithRange &attrs,
  121. SourceLocation *endLoc) {
  122. assert(getLangOpts().HLSL);
  123. SourceLocation StartLoc = Tok.getLocation(), Loc;
  124. if (!endLoc)
  125. endLoc = &Loc;
  126. do {
  127. ParseHLSLAttributeSpecifier(attrs, endLoc);
  128. } while (Tok.is(tok::l_square));
  129. attrs.Range = SourceRange(StartLoc, *endLoc);
  130. }