Parser.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. //===--- Parser.cpp - Matcher expression parser -----*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. ///
  10. /// \file
  11. /// \brief Recursive parser implementation for the matcher expression grammar.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/ASTMatchers/Dynamic/Parser.h"
  15. #include "clang/ASTMatchers/Dynamic/Registry.h"
  16. #include "clang/Basic/CharInfo.h"
  17. #include "llvm/ADT/Optional.h"
  18. #include "llvm/ADT/Twine.h"
  19. #include "llvm/Support/ManagedStatic.h"
  20. #include <string>
  21. #include <vector>
  22. namespace clang {
  23. namespace ast_matchers {
  24. namespace dynamic {
  25. /// \brief Simple structure to hold information for one token from the parser.
  26. struct Parser::TokenInfo {
  27. /// \brief Different possible tokens.
  28. enum TokenKind {
  29. TK_Eof,
  30. TK_OpenParen,
  31. TK_CloseParen,
  32. TK_Comma,
  33. TK_Period,
  34. TK_Literal,
  35. TK_Ident,
  36. TK_InvalidChar,
  37. TK_Error,
  38. TK_CodeCompletion
  39. };
  40. /// \brief Some known identifiers.
  41. static const char* const ID_Bind;
  42. TokenInfo() : Text(), Kind(TK_Eof), Range(), Value() {}
  43. StringRef Text;
  44. TokenKind Kind;
  45. SourceRange Range;
  46. VariantValue Value;
  47. };
  48. const char* const Parser::TokenInfo::ID_Bind = "bind";
  49. /// \brief Simple tokenizer for the parser.
  50. class Parser::CodeTokenizer {
  51. public:
  52. explicit CodeTokenizer(StringRef MatcherCode, Diagnostics *Error)
  53. : Code(MatcherCode), StartOfLine(MatcherCode), Line(1), Error(Error),
  54. CodeCompletionLocation(nullptr) {
  55. NextToken = getNextToken();
  56. }
  57. CodeTokenizer(StringRef MatcherCode, Diagnostics *Error,
  58. unsigned CodeCompletionOffset)
  59. : Code(MatcherCode), StartOfLine(MatcherCode), Line(1), Error(Error),
  60. CodeCompletionLocation(MatcherCode.data() + CodeCompletionOffset) {
  61. NextToken = getNextToken();
  62. }
  63. /// \brief Returns but doesn't consume the next token.
  64. const TokenInfo &peekNextToken() const { return NextToken; }
  65. /// \brief Consumes and returns the next token.
  66. TokenInfo consumeNextToken() {
  67. TokenInfo ThisToken = NextToken;
  68. NextToken = getNextToken();
  69. return ThisToken;
  70. }
  71. TokenInfo::TokenKind nextTokenKind() const { return NextToken.Kind; }
  72. private:
  73. TokenInfo getNextToken() {
  74. consumeWhitespace();
  75. TokenInfo Result;
  76. Result.Range.Start = currentLocation();
  77. if (CodeCompletionLocation && CodeCompletionLocation <= Code.data()) {
  78. Result.Kind = TokenInfo::TK_CodeCompletion;
  79. Result.Text = StringRef(CodeCompletionLocation, 0);
  80. CodeCompletionLocation = nullptr;
  81. return Result;
  82. }
  83. if (Code.empty()) {
  84. Result.Kind = TokenInfo::TK_Eof;
  85. Result.Text = "";
  86. return Result;
  87. }
  88. switch (Code[0]) {
  89. case ',':
  90. Result.Kind = TokenInfo::TK_Comma;
  91. Result.Text = Code.substr(0, 1);
  92. Code = Code.drop_front();
  93. break;
  94. case '.':
  95. Result.Kind = TokenInfo::TK_Period;
  96. Result.Text = Code.substr(0, 1);
  97. Code = Code.drop_front();
  98. break;
  99. case '(':
  100. Result.Kind = TokenInfo::TK_OpenParen;
  101. Result.Text = Code.substr(0, 1);
  102. Code = Code.drop_front();
  103. break;
  104. case ')':
  105. Result.Kind = TokenInfo::TK_CloseParen;
  106. Result.Text = Code.substr(0, 1);
  107. Code = Code.drop_front();
  108. break;
  109. case '"':
  110. case '\'':
  111. // Parse a string literal.
  112. consumeStringLiteral(&Result);
  113. break;
  114. case '0': case '1': case '2': case '3': case '4':
  115. case '5': case '6': case '7': case '8': case '9':
  116. // Parse an unsigned literal.
  117. consumeUnsignedLiteral(&Result);
  118. break;
  119. default:
  120. if (isAlphanumeric(Code[0])) {
  121. // Parse an identifier
  122. size_t TokenLength = 1;
  123. while (1) {
  124. // A code completion location in/immediately after an identifier will
  125. // cause the portion of the identifier before the code completion
  126. // location to become a code completion token.
  127. if (CodeCompletionLocation == Code.data() + TokenLength) {
  128. CodeCompletionLocation = nullptr;
  129. Result.Kind = TokenInfo::TK_CodeCompletion;
  130. Result.Text = Code.substr(0, TokenLength);
  131. Code = Code.drop_front(TokenLength);
  132. return Result;
  133. }
  134. if (TokenLength == Code.size() || !isAlphanumeric(Code[TokenLength]))
  135. break;
  136. ++TokenLength;
  137. }
  138. Result.Kind = TokenInfo::TK_Ident;
  139. Result.Text = Code.substr(0, TokenLength);
  140. Code = Code.drop_front(TokenLength);
  141. } else {
  142. Result.Kind = TokenInfo::TK_InvalidChar;
  143. Result.Text = Code.substr(0, 1);
  144. Code = Code.drop_front(1);
  145. }
  146. break;
  147. }
  148. Result.Range.End = currentLocation();
  149. return Result;
  150. }
  151. /// \brief Consume an unsigned literal.
  152. void consumeUnsignedLiteral(TokenInfo *Result) {
  153. unsigned Length = 1;
  154. if (Code.size() > 1) {
  155. // Consume the 'x' or 'b' radix modifier, if present.
  156. switch (toLowercase(Code[1])) {
  157. case 'x': case 'b': Length = 2;
  158. }
  159. }
  160. while (Length < Code.size() && isHexDigit(Code[Length]))
  161. ++Length;
  162. Result->Text = Code.substr(0, Length);
  163. Code = Code.drop_front(Length);
  164. unsigned Value;
  165. if (!Result->Text.getAsInteger(0, Value)) {
  166. Result->Kind = TokenInfo::TK_Literal;
  167. Result->Value = Value;
  168. } else {
  169. SourceRange Range;
  170. Range.Start = Result->Range.Start;
  171. Range.End = currentLocation();
  172. Error->addError(Range, Error->ET_ParserUnsignedError) << Result->Text;
  173. Result->Kind = TokenInfo::TK_Error;
  174. }
  175. }
  176. /// \brief Consume a string literal.
  177. ///
  178. /// \c Code must be positioned at the start of the literal (the opening
  179. /// quote). Consumed until it finds the same closing quote character.
  180. void consumeStringLiteral(TokenInfo *Result) {
  181. bool InEscape = false;
  182. const char Marker = Code[0];
  183. for (size_t Length = 1, Size = Code.size(); Length != Size; ++Length) {
  184. if (InEscape) {
  185. InEscape = false;
  186. continue;
  187. }
  188. if (Code[Length] == '\\') {
  189. InEscape = true;
  190. continue;
  191. }
  192. if (Code[Length] == Marker) {
  193. Result->Kind = TokenInfo::TK_Literal;
  194. Result->Text = Code.substr(0, Length + 1);
  195. Result->Value = Code.substr(1, Length - 1);
  196. Code = Code.drop_front(Length + 1);
  197. return;
  198. }
  199. }
  200. StringRef ErrorText = Code;
  201. Code = Code.drop_front(Code.size());
  202. SourceRange Range;
  203. Range.Start = Result->Range.Start;
  204. Range.End = currentLocation();
  205. Error->addError(Range, Error->ET_ParserStringError) << ErrorText;
  206. Result->Kind = TokenInfo::TK_Error;
  207. }
  208. /// \brief Consume all leading whitespace from \c Code.
  209. void consumeWhitespace() {
  210. while (!Code.empty() && isWhitespace(Code[0])) {
  211. if (Code[0] == '\n') {
  212. ++Line;
  213. StartOfLine = Code.drop_front();
  214. }
  215. Code = Code.drop_front();
  216. }
  217. }
  218. SourceLocation currentLocation() {
  219. SourceLocation Location;
  220. Location.Line = Line;
  221. Location.Column = Code.data() - StartOfLine.data() + 1;
  222. return Location;
  223. }
  224. StringRef Code;
  225. StringRef StartOfLine;
  226. unsigned Line;
  227. Diagnostics *Error;
  228. TokenInfo NextToken;
  229. const char *CodeCompletionLocation;
  230. };
  231. Parser::Sema::~Sema() {}
  232. std::vector<ArgKind> Parser::Sema::getAcceptedCompletionTypes(
  233. llvm::ArrayRef<std::pair<MatcherCtor, unsigned>> Context) {
  234. return std::vector<ArgKind>();
  235. }
  236. std::vector<MatcherCompletion>
  237. Parser::Sema::getMatcherCompletions(llvm::ArrayRef<ArgKind> AcceptedTypes) {
  238. return std::vector<MatcherCompletion>();
  239. }
  240. struct Parser::ScopedContextEntry {
  241. Parser *P;
  242. ScopedContextEntry(Parser *P, MatcherCtor C) : P(P) {
  243. P->ContextStack.push_back(std::make_pair(C, 0u));
  244. }
  245. ~ScopedContextEntry() {
  246. P->ContextStack.pop_back();
  247. }
  248. void nextArg() {
  249. ++P->ContextStack.back().second;
  250. }
  251. };
  252. /// \brief Parse expressions that start with an identifier.
  253. ///
  254. /// This function can parse named values and matchers.
  255. /// In case of failure it will try to determine the user's intent to give
  256. /// an appropriate error message.
  257. bool Parser::parseIdentifierPrefixImpl(VariantValue *Value) {
  258. const TokenInfo NameToken = Tokenizer->consumeNextToken();
  259. if (Tokenizer->nextTokenKind() != TokenInfo::TK_OpenParen) {
  260. // Parse as a named value.
  261. if (const VariantValue NamedValue =
  262. NamedValues ? NamedValues->lookup(NameToken.Text)
  263. : VariantValue()) {
  264. *Value = NamedValue;
  265. return true;
  266. }
  267. // If the syntax is correct and the name is not a matcher either, report
  268. // unknown named value.
  269. if ((Tokenizer->nextTokenKind() == TokenInfo::TK_Comma ||
  270. Tokenizer->nextTokenKind() == TokenInfo::TK_CloseParen ||
  271. Tokenizer->nextTokenKind() == TokenInfo::TK_Eof) &&
  272. !S->lookupMatcherCtor(NameToken.Text)) {
  273. Error->addError(NameToken.Range, Error->ET_RegistryValueNotFound)
  274. << NameToken.Text;
  275. return false;
  276. }
  277. // Otherwise, fallback to the matcher parser.
  278. }
  279. // Parse as a matcher expression.
  280. return parseMatcherExpressionImpl(NameToken, Value);
  281. }
  282. /// \brief Parse and validate a matcher expression.
  283. /// \return \c true on success, in which case \c Value has the matcher parsed.
  284. /// If the input is malformed, or some argument has an error, it
  285. /// returns \c false.
  286. bool Parser::parseMatcherExpressionImpl(const TokenInfo &NameToken,
  287. VariantValue *Value) {
  288. assert(NameToken.Kind == TokenInfo::TK_Ident);
  289. const TokenInfo OpenToken = Tokenizer->consumeNextToken();
  290. if (OpenToken.Kind != TokenInfo::TK_OpenParen) {
  291. Error->addError(OpenToken.Range, Error->ET_ParserNoOpenParen)
  292. << OpenToken.Text;
  293. return false;
  294. }
  295. llvm::Optional<MatcherCtor> Ctor = S->lookupMatcherCtor(NameToken.Text);
  296. if (!Ctor) {
  297. Error->addError(NameToken.Range, Error->ET_RegistryMatcherNotFound)
  298. << NameToken.Text;
  299. // Do not return here. We need to continue to give completion suggestions.
  300. }
  301. std::vector<ParserValue> Args;
  302. TokenInfo EndToken;
  303. {
  304. ScopedContextEntry SCE(this, Ctor ? *Ctor : nullptr);
  305. while (Tokenizer->nextTokenKind() != TokenInfo::TK_Eof) {
  306. if (Tokenizer->nextTokenKind() == TokenInfo::TK_CloseParen) {
  307. // End of args.
  308. EndToken = Tokenizer->consumeNextToken();
  309. break;
  310. }
  311. if (Args.size() > 0) {
  312. // We must find a , token to continue.
  313. const TokenInfo CommaToken = Tokenizer->consumeNextToken();
  314. if (CommaToken.Kind != TokenInfo::TK_Comma) {
  315. Error->addError(CommaToken.Range, Error->ET_ParserNoComma)
  316. << CommaToken.Text;
  317. return false;
  318. }
  319. }
  320. Diagnostics::Context Ctx(Diagnostics::Context::MatcherArg, Error,
  321. NameToken.Text, NameToken.Range,
  322. Args.size() + 1);
  323. ParserValue ArgValue;
  324. ArgValue.Text = Tokenizer->peekNextToken().Text;
  325. ArgValue.Range = Tokenizer->peekNextToken().Range;
  326. if (!parseExpressionImpl(&ArgValue.Value)) {
  327. return false;
  328. }
  329. Args.push_back(ArgValue);
  330. SCE.nextArg();
  331. }
  332. }
  333. if (EndToken.Kind == TokenInfo::TK_Eof) {
  334. Error->addError(OpenToken.Range, Error->ET_ParserNoCloseParen);
  335. return false;
  336. }
  337. std::string BindID;
  338. if (Tokenizer->peekNextToken().Kind == TokenInfo::TK_Period) {
  339. // Parse .bind("foo")
  340. Tokenizer->consumeNextToken(); // consume the period.
  341. const TokenInfo BindToken = Tokenizer->consumeNextToken();
  342. if (BindToken.Kind == TokenInfo::TK_CodeCompletion) {
  343. addCompletion(BindToken, MatcherCompletion("bind(\"", "bind", 1));
  344. return false;
  345. }
  346. const TokenInfo OpenToken = Tokenizer->consumeNextToken();
  347. const TokenInfo IDToken = Tokenizer->consumeNextToken();
  348. const TokenInfo CloseToken = Tokenizer->consumeNextToken();
  349. // TODO: We could use different error codes for each/some to be more
  350. // explicit about the syntax error.
  351. if (BindToken.Kind != TokenInfo::TK_Ident ||
  352. BindToken.Text != TokenInfo::ID_Bind) {
  353. Error->addError(BindToken.Range, Error->ET_ParserMalformedBindExpr);
  354. return false;
  355. }
  356. if (OpenToken.Kind != TokenInfo::TK_OpenParen) {
  357. Error->addError(OpenToken.Range, Error->ET_ParserMalformedBindExpr);
  358. return false;
  359. }
  360. if (IDToken.Kind != TokenInfo::TK_Literal || !IDToken.Value.isString()) {
  361. Error->addError(IDToken.Range, Error->ET_ParserMalformedBindExpr);
  362. return false;
  363. }
  364. if (CloseToken.Kind != TokenInfo::TK_CloseParen) {
  365. Error->addError(CloseToken.Range, Error->ET_ParserMalformedBindExpr);
  366. return false;
  367. }
  368. BindID = IDToken.Value.getString();
  369. }
  370. if (!Ctor)
  371. return false;
  372. // Merge the start and end infos.
  373. Diagnostics::Context Ctx(Diagnostics::Context::ConstructMatcher, Error,
  374. NameToken.Text, NameToken.Range);
  375. SourceRange MatcherRange = NameToken.Range;
  376. MatcherRange.End = EndToken.Range.End;
  377. VariantMatcher Result = S->actOnMatcherExpression(
  378. *Ctor, MatcherRange, BindID, Args, Error);
  379. if (Result.isNull()) return false;
  380. *Value = Result;
  381. return true;
  382. }
  383. // If the prefix of this completion matches the completion token, add it to
  384. // Completions minus the prefix.
  385. void Parser::addCompletion(const TokenInfo &CompToken,
  386. const MatcherCompletion& Completion) {
  387. if (StringRef(Completion.TypedText).startswith(CompToken.Text) &&
  388. Completion.Specificity > 0) {
  389. Completions.emplace_back(Completion.TypedText.substr(CompToken.Text.size()),
  390. Completion.MatcherDecl, Completion.Specificity);
  391. }
  392. }
  393. std::vector<MatcherCompletion> Parser::getNamedValueCompletions(
  394. ArrayRef<ArgKind> AcceptedTypes) {
  395. if (!NamedValues) return std::vector<MatcherCompletion>();
  396. std::vector<MatcherCompletion> Result;
  397. for (const auto &Entry : *NamedValues) {
  398. unsigned Specificity;
  399. if (Entry.getValue().isConvertibleTo(AcceptedTypes, &Specificity)) {
  400. std::string Decl =
  401. (Entry.getValue().getTypeAsString() + " " + Entry.getKey()).str();
  402. Result.emplace_back(Entry.getKey(), Decl, Specificity);
  403. }
  404. }
  405. return Result;
  406. }
  407. void Parser::addExpressionCompletions() {
  408. const TokenInfo CompToken = Tokenizer->consumeNextToken();
  409. assert(CompToken.Kind == TokenInfo::TK_CodeCompletion);
  410. // We cannot complete code if there is an invalid element on the context
  411. // stack.
  412. for (ContextStackTy::iterator I = ContextStack.begin(),
  413. E = ContextStack.end();
  414. I != E; ++I) {
  415. if (!I->first)
  416. return;
  417. }
  418. auto AcceptedTypes = S->getAcceptedCompletionTypes(ContextStack);
  419. for (const auto &Completion : S->getMatcherCompletions(AcceptedTypes)) {
  420. addCompletion(CompToken, Completion);
  421. }
  422. for (const auto &Completion : getNamedValueCompletions(AcceptedTypes)) {
  423. addCompletion(CompToken, Completion);
  424. }
  425. }
  426. /// \brief Parse an <Expresssion>
  427. bool Parser::parseExpressionImpl(VariantValue *Value) {
  428. switch (Tokenizer->nextTokenKind()) {
  429. case TokenInfo::TK_Literal:
  430. *Value = Tokenizer->consumeNextToken().Value;
  431. return true;
  432. case TokenInfo::TK_Ident:
  433. return parseIdentifierPrefixImpl(Value);
  434. case TokenInfo::TK_CodeCompletion:
  435. addExpressionCompletions();
  436. return false;
  437. case TokenInfo::TK_Eof:
  438. Error->addError(Tokenizer->consumeNextToken().Range,
  439. Error->ET_ParserNoCode);
  440. return false;
  441. case TokenInfo::TK_Error:
  442. // This error was already reported by the tokenizer.
  443. return false;
  444. case TokenInfo::TK_OpenParen:
  445. case TokenInfo::TK_CloseParen:
  446. case TokenInfo::TK_Comma:
  447. case TokenInfo::TK_Period:
  448. case TokenInfo::TK_InvalidChar:
  449. const TokenInfo Token = Tokenizer->consumeNextToken();
  450. Error->addError(Token.Range, Error->ET_ParserInvalidToken) << Token.Text;
  451. return false;
  452. }
  453. llvm_unreachable("Unknown token kind.");
  454. }
  455. static llvm::ManagedStatic<Parser::RegistrySema> DefaultRegistrySema;
  456. Parser::Parser(CodeTokenizer *Tokenizer, Sema *S,
  457. const NamedValueMap *NamedValues, Diagnostics *Error)
  458. : Tokenizer(Tokenizer), S(S ? S : &*DefaultRegistrySema),
  459. NamedValues(NamedValues), Error(Error) {}
  460. Parser::RegistrySema::~RegistrySema() {}
  461. llvm::Optional<MatcherCtor>
  462. Parser::RegistrySema::lookupMatcherCtor(StringRef MatcherName) {
  463. return Registry::lookupMatcherCtor(MatcherName);
  464. }
  465. VariantMatcher Parser::RegistrySema::actOnMatcherExpression(
  466. MatcherCtor Ctor, SourceRange NameRange, StringRef BindID,
  467. ArrayRef<ParserValue> Args, Diagnostics *Error) {
  468. if (BindID.empty()) {
  469. return Registry::constructMatcher(Ctor, NameRange, Args, Error);
  470. } else {
  471. return Registry::constructBoundMatcher(Ctor, NameRange, BindID, Args,
  472. Error);
  473. }
  474. }
  475. std::vector<ArgKind> Parser::RegistrySema::getAcceptedCompletionTypes(
  476. ArrayRef<std::pair<MatcherCtor, unsigned>> Context) {
  477. return Registry::getAcceptedCompletionTypes(Context);
  478. }
  479. std::vector<MatcherCompletion> Parser::RegistrySema::getMatcherCompletions(
  480. ArrayRef<ArgKind> AcceptedTypes) {
  481. return Registry::getMatcherCompletions(AcceptedTypes);
  482. }
  483. bool Parser::parseExpression(StringRef Code, Sema *S,
  484. const NamedValueMap *NamedValues,
  485. VariantValue *Value, Diagnostics *Error) {
  486. CodeTokenizer Tokenizer(Code, Error);
  487. if (!Parser(&Tokenizer, S, NamedValues, Error).parseExpressionImpl(Value))
  488. return false;
  489. if (Tokenizer.peekNextToken().Kind != TokenInfo::TK_Eof) {
  490. Error->addError(Tokenizer.peekNextToken().Range,
  491. Error->ET_ParserTrailingCode);
  492. return false;
  493. }
  494. return true;
  495. }
  496. std::vector<MatcherCompletion>
  497. Parser::completeExpression(StringRef Code, unsigned CompletionOffset, Sema *S,
  498. const NamedValueMap *NamedValues) {
  499. Diagnostics Error;
  500. CodeTokenizer Tokenizer(Code, &Error, CompletionOffset);
  501. Parser P(&Tokenizer, S, NamedValues, &Error);
  502. VariantValue Dummy;
  503. P.parseExpressionImpl(&Dummy);
  504. // Sort by specificity, then by name.
  505. std::sort(P.Completions.begin(), P.Completions.end(),
  506. [](const MatcherCompletion &A, const MatcherCompletion &B) {
  507. if (A.Specificity != B.Specificity)
  508. return A.Specificity > B.Specificity;
  509. return A.TypedText < B.TypedText;
  510. });
  511. return P.Completions;
  512. }
  513. llvm::Optional<DynTypedMatcher>
  514. Parser::parseMatcherExpression(StringRef Code, Sema *S,
  515. const NamedValueMap *NamedValues,
  516. Diagnostics *Error) {
  517. VariantValue Value;
  518. if (!parseExpression(Code, S, NamedValues, &Value, Error))
  519. return llvm::Optional<DynTypedMatcher>();
  520. if (!Value.isMatcher()) {
  521. Error->addError(SourceRange(), Error->ET_ParserNotAMatcher);
  522. return llvm::Optional<DynTypedMatcher>();
  523. }
  524. llvm::Optional<DynTypedMatcher> Result =
  525. Value.getMatcher().getSingleMatcher();
  526. if (!Result.hasValue()) {
  527. Error->addError(SourceRange(), Error->ET_ParserOverloadedType)
  528. << Value.getTypeAsString();
  529. }
  530. return Result;
  531. }
  532. } // namespace dynamic
  533. } // namespace ast_matchers
  534. } // namespace clang